binutils/
[binutils-gdb.git] / gdb / linux-tdep.c
blobeb8ea2b664de4cc9d7aa05f59695a3b3f602d975
1 /* Target-dependent code for GNU/Linux, architecture independent.
3 Copyright (C) 2009-2013 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20 #include "defs.h"
21 #include "gdbtypes.h"
22 #include "linux-tdep.h"
23 #include "auxv.h"
24 #include "target.h"
25 #include "gdbthread.h"
26 #include "gdbcore.h"
27 #include "regcache.h"
28 #include "regset.h"
29 #include "elf/common.h"
30 #include "elf-bfd.h" /* for elfcore_write_* */
31 #include "inferior.h"
32 #include "cli/cli-utils.h"
33 #include "arch-utils.h"
34 #include "gdb_obstack.h"
35 #include "cli/cli-utils.h"
37 #include <ctype.h>
39 /* This enum represents the signals' numbers on a generic architecture
40 running the Linux kernel. The definition of "generic" comes from
41 the file <include/uapi/asm-generic/signal.h>, from the Linux kernel
42 tree, which is the "de facto" implementation of signal numbers to
43 be used by new architecture ports.
45 For those architectures which have differences between the generic
46 standard (e.g., Alpha), we define the different signals (and *only*
47 those) in the specific target-dependent file (e.g.,
48 alpha-linux-tdep.c, for Alpha). Please refer to the architecture's
49 tdep file for more information.
51 ARM deserves a special mention here. On the file
52 <arch/arm/include/uapi/asm/signal.h>, it defines only one different
53 (and ARM-only) signal, which is SIGSWI, with the same number as
54 SIGRTMIN. This signal is used only for a very specific target,
55 called ArthurOS (from RISCOS). Therefore, we do not handle it on
56 the ARM-tdep file, and we can safely use the generic signal handler
57 here for ARM targets.
59 As stated above, this enum is derived from
60 <include/uapi/asm-generic/signal.h>, from the Linux kernel
61 tree. */
63 enum
65 LINUX_SIGHUP = 1,
66 LINUX_SIGINT = 2,
67 LINUX_SIGQUIT = 3,
68 LINUX_SIGILL = 4,
69 LINUX_SIGTRAP = 5,
70 LINUX_SIGABRT = 6,
71 LINUX_SIGIOT = 6,
72 LINUX_SIGBUS = 7,
73 LINUX_SIGFPE = 8,
74 LINUX_SIGKILL = 9,
75 LINUX_SIGUSR1 = 10,
76 LINUX_SIGSEGV = 11,
77 LINUX_SIGUSR2 = 12,
78 LINUX_SIGPIPE = 13,
79 LINUX_SIGALRM = 14,
80 LINUX_SIGTERM = 15,
81 LINUX_SIGSTKFLT = 16,
82 LINUX_SIGCHLD = 17,
83 LINUX_SIGCONT = 18,
84 LINUX_SIGSTOP = 19,
85 LINUX_SIGTSTP = 20,
86 LINUX_SIGTTIN = 21,
87 LINUX_SIGTTOU = 22,
88 LINUX_SIGURG = 23,
89 LINUX_SIGXCPU = 24,
90 LINUX_SIGXFSZ = 25,
91 LINUX_SIGVTALRM = 26,
92 LINUX_SIGPROF = 27,
93 LINUX_SIGWINCH = 28,
94 LINUX_SIGIO = 29,
95 LINUX_SIGPOLL = LINUX_SIGIO,
96 LINUX_SIGPWR = 30,
97 LINUX_SIGSYS = 31,
98 LINUX_SIGUNUSED = 31,
100 LINUX_SIGRTMIN = 32,
101 LINUX_SIGRTMAX = 64,
104 static struct gdbarch_data *linux_gdbarch_data_handle;
106 struct linux_gdbarch_data
108 struct type *siginfo_type;
111 static void *
112 init_linux_gdbarch_data (struct gdbarch *gdbarch)
114 return GDBARCH_OBSTACK_ZALLOC (gdbarch, struct linux_gdbarch_data);
117 static struct linux_gdbarch_data *
118 get_linux_gdbarch_data (struct gdbarch *gdbarch)
120 return gdbarch_data (gdbarch, linux_gdbarch_data_handle);
123 /* This function is suitable for architectures that don't
124 extend/override the standard siginfo structure. */
126 struct type *
127 linux_get_siginfo_type (struct gdbarch *gdbarch)
129 struct linux_gdbarch_data *linux_gdbarch_data;
130 struct type *int_type, *uint_type, *long_type, *void_ptr_type;
131 struct type *uid_type, *pid_type;
132 struct type *sigval_type, *clock_type;
133 struct type *siginfo_type, *sifields_type;
134 struct type *type;
136 linux_gdbarch_data = get_linux_gdbarch_data (gdbarch);
137 if (linux_gdbarch_data->siginfo_type != NULL)
138 return linux_gdbarch_data->siginfo_type;
140 int_type = arch_integer_type (gdbarch, gdbarch_int_bit (gdbarch),
141 0, "int");
142 uint_type = arch_integer_type (gdbarch, gdbarch_int_bit (gdbarch),
143 1, "unsigned int");
144 long_type = arch_integer_type (gdbarch, gdbarch_long_bit (gdbarch),
145 0, "long");
146 void_ptr_type = lookup_pointer_type (builtin_type (gdbarch)->builtin_void);
148 /* sival_t */
149 sigval_type = arch_composite_type (gdbarch, NULL, TYPE_CODE_UNION);
150 TYPE_NAME (sigval_type) = xstrdup ("sigval_t");
151 append_composite_type_field (sigval_type, "sival_int", int_type);
152 append_composite_type_field (sigval_type, "sival_ptr", void_ptr_type);
154 /* __pid_t */
155 pid_type = arch_type (gdbarch, TYPE_CODE_TYPEDEF,
156 TYPE_LENGTH (int_type), "__pid_t");
157 TYPE_TARGET_TYPE (pid_type) = int_type;
158 TYPE_TARGET_STUB (pid_type) = 1;
160 /* __uid_t */
161 uid_type = arch_type (gdbarch, TYPE_CODE_TYPEDEF,
162 TYPE_LENGTH (uint_type), "__uid_t");
163 TYPE_TARGET_TYPE (uid_type) = uint_type;
164 TYPE_TARGET_STUB (uid_type) = 1;
166 /* __clock_t */
167 clock_type = arch_type (gdbarch, TYPE_CODE_TYPEDEF,
168 TYPE_LENGTH (long_type), "__clock_t");
169 TYPE_TARGET_TYPE (clock_type) = long_type;
170 TYPE_TARGET_STUB (clock_type) = 1;
172 /* _sifields */
173 sifields_type = arch_composite_type (gdbarch, NULL, TYPE_CODE_UNION);
176 const int si_max_size = 128;
177 int si_pad_size;
178 int size_of_int = gdbarch_int_bit (gdbarch) / HOST_CHAR_BIT;
180 /* _pad */
181 if (gdbarch_ptr_bit (gdbarch) == 64)
182 si_pad_size = (si_max_size / size_of_int) - 4;
183 else
184 si_pad_size = (si_max_size / size_of_int) - 3;
185 append_composite_type_field (sifields_type, "_pad",
186 init_vector_type (int_type, si_pad_size));
189 /* _kill */
190 type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
191 append_composite_type_field (type, "si_pid", pid_type);
192 append_composite_type_field (type, "si_uid", uid_type);
193 append_composite_type_field (sifields_type, "_kill", type);
195 /* _timer */
196 type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
197 append_composite_type_field (type, "si_tid", int_type);
198 append_composite_type_field (type, "si_overrun", int_type);
199 append_composite_type_field (type, "si_sigval", sigval_type);
200 append_composite_type_field (sifields_type, "_timer", type);
202 /* _rt */
203 type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
204 append_composite_type_field (type, "si_pid", pid_type);
205 append_composite_type_field (type, "si_uid", uid_type);
206 append_composite_type_field (type, "si_sigval", sigval_type);
207 append_composite_type_field (sifields_type, "_rt", type);
209 /* _sigchld */
210 type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
211 append_composite_type_field (type, "si_pid", pid_type);
212 append_composite_type_field (type, "si_uid", uid_type);
213 append_composite_type_field (type, "si_status", int_type);
214 append_composite_type_field (type, "si_utime", clock_type);
215 append_composite_type_field (type, "si_stime", clock_type);
216 append_composite_type_field (sifields_type, "_sigchld", type);
218 /* _sigfault */
219 type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
220 append_composite_type_field (type, "si_addr", void_ptr_type);
221 append_composite_type_field (sifields_type, "_sigfault", type);
223 /* _sigpoll */
224 type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
225 append_composite_type_field (type, "si_band", long_type);
226 append_composite_type_field (type, "si_fd", int_type);
227 append_composite_type_field (sifields_type, "_sigpoll", type);
229 /* struct siginfo */
230 siginfo_type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
231 TYPE_NAME (siginfo_type) = xstrdup ("siginfo");
232 append_composite_type_field (siginfo_type, "si_signo", int_type);
233 append_composite_type_field (siginfo_type, "si_errno", int_type);
234 append_composite_type_field (siginfo_type, "si_code", int_type);
235 append_composite_type_field_aligned (siginfo_type,
236 "_sifields", sifields_type,
237 TYPE_LENGTH (long_type));
239 linux_gdbarch_data->siginfo_type = siginfo_type;
241 return siginfo_type;
244 /* Return true if the target is running on uClinux instead of normal
245 Linux kernel. */
248 linux_is_uclinux (void)
250 CORE_ADDR dummy;
252 return (target_auxv_search (&current_target, AT_NULL, &dummy) > 0
253 && target_auxv_search (&current_target, AT_PAGESZ, &dummy) == 0);
256 static int
257 linux_has_shared_address_space (struct gdbarch *gdbarch)
259 return linux_is_uclinux ();
262 /* This is how we want PTIDs from core files to be printed. */
264 static char *
265 linux_core_pid_to_str (struct gdbarch *gdbarch, ptid_t ptid)
267 static char buf[80];
269 if (ptid_get_lwp (ptid) != 0)
271 snprintf (buf, sizeof (buf), "LWP %ld", ptid_get_lwp (ptid));
272 return buf;
275 return normal_pid_to_str (ptid);
278 /* Service function for corefiles and info proc. */
280 static void
281 read_mapping (const char *line,
282 ULONGEST *addr, ULONGEST *endaddr,
283 const char **permissions, size_t *permissions_len,
284 ULONGEST *offset,
285 const char **device, size_t *device_len,
286 ULONGEST *inode,
287 const char **filename)
289 const char *p = line;
291 *addr = strtoulst (p, &p, 16);
292 if (*p == '-')
293 p++;
294 *endaddr = strtoulst (p, &p, 16);
296 p = skip_spaces_const (p);
297 *permissions = p;
298 while (*p && !isspace (*p))
299 p++;
300 *permissions_len = p - *permissions;
302 *offset = strtoulst (p, &p, 16);
304 p = skip_spaces_const (p);
305 *device = p;
306 while (*p && !isspace (*p))
307 p++;
308 *device_len = p - *device;
310 *inode = strtoulst (p, &p, 10);
312 p = skip_spaces_const (p);
313 *filename = p;
316 /* Implement the "info proc" command. */
318 static void
319 linux_info_proc (struct gdbarch *gdbarch, char *args,
320 enum info_proc_what what)
322 /* A long is used for pid instead of an int to avoid a loss of precision
323 compiler warning from the output of strtoul. */
324 long pid;
325 int cmdline_f = (what == IP_MINIMAL || what == IP_CMDLINE || what == IP_ALL);
326 int cwd_f = (what == IP_MINIMAL || what == IP_CWD || what == IP_ALL);
327 int exe_f = (what == IP_MINIMAL || what == IP_EXE || what == IP_ALL);
328 int mappings_f = (what == IP_MAPPINGS || what == IP_ALL);
329 int status_f = (what == IP_STATUS || what == IP_ALL);
330 int stat_f = (what == IP_STAT || what == IP_ALL);
331 char filename[100];
332 char *data;
333 int target_errno;
335 if (args && isdigit (args[0]))
336 pid = strtoul (args, &args, 10);
337 else
339 if (!target_has_execution)
340 error (_("No current process: you must name one."));
341 if (current_inferior ()->fake_pid_p)
342 error (_("Can't determine the current process's PID: you must name one."));
344 pid = current_inferior ()->pid;
347 args = skip_spaces (args);
348 if (args && args[0])
349 error (_("Too many parameters: %s"), args);
351 printf_filtered (_("process %ld\n"), pid);
352 if (cmdline_f)
354 xsnprintf (filename, sizeof filename, "/proc/%ld/cmdline", pid);
355 data = target_fileio_read_stralloc (filename);
356 if (data)
358 struct cleanup *cleanup = make_cleanup (xfree, data);
359 printf_filtered ("cmdline = '%s'\n", data);
360 do_cleanups (cleanup);
362 else
363 warning (_("unable to open /proc file '%s'"), filename);
365 if (cwd_f)
367 xsnprintf (filename, sizeof filename, "/proc/%ld/cwd", pid);
368 data = target_fileio_readlink (filename, &target_errno);
369 if (data)
371 struct cleanup *cleanup = make_cleanup (xfree, data);
372 printf_filtered ("cwd = '%s'\n", data);
373 do_cleanups (cleanup);
375 else
376 warning (_("unable to read link '%s'"), filename);
378 if (exe_f)
380 xsnprintf (filename, sizeof filename, "/proc/%ld/exe", pid);
381 data = target_fileio_readlink (filename, &target_errno);
382 if (data)
384 struct cleanup *cleanup = make_cleanup (xfree, data);
385 printf_filtered ("exe = '%s'\n", data);
386 do_cleanups (cleanup);
388 else
389 warning (_("unable to read link '%s'"), filename);
391 if (mappings_f)
393 xsnprintf (filename, sizeof filename, "/proc/%ld/maps", pid);
394 data = target_fileio_read_stralloc (filename);
395 if (data)
397 struct cleanup *cleanup = make_cleanup (xfree, data);
398 char *line;
400 printf_filtered (_("Mapped address spaces:\n\n"));
401 if (gdbarch_addr_bit (gdbarch) == 32)
403 printf_filtered ("\t%10s %10s %10s %10s %s\n",
404 "Start Addr",
405 " End Addr",
406 " Size", " Offset", "objfile");
408 else
410 printf_filtered (" %18s %18s %10s %10s %s\n",
411 "Start Addr",
412 " End Addr",
413 " Size", " Offset", "objfile");
416 for (line = strtok (data, "\n"); line; line = strtok (NULL, "\n"))
418 ULONGEST addr, endaddr, offset, inode;
419 const char *permissions, *device, *filename;
420 size_t permissions_len, device_len;
422 read_mapping (line, &addr, &endaddr,
423 &permissions, &permissions_len,
424 &offset, &device, &device_len,
425 &inode, &filename);
427 if (gdbarch_addr_bit (gdbarch) == 32)
429 printf_filtered ("\t%10s %10s %10s %10s %s\n",
430 paddress (gdbarch, addr),
431 paddress (gdbarch, endaddr),
432 hex_string (endaddr - addr),
433 hex_string (offset),
434 *filename? filename : "");
436 else
438 printf_filtered (" %18s %18s %10s %10s %s\n",
439 paddress (gdbarch, addr),
440 paddress (gdbarch, endaddr),
441 hex_string (endaddr - addr),
442 hex_string (offset),
443 *filename? filename : "");
447 do_cleanups (cleanup);
449 else
450 warning (_("unable to open /proc file '%s'"), filename);
452 if (status_f)
454 xsnprintf (filename, sizeof filename, "/proc/%ld/status", pid);
455 data = target_fileio_read_stralloc (filename);
456 if (data)
458 struct cleanup *cleanup = make_cleanup (xfree, data);
459 puts_filtered (data);
460 do_cleanups (cleanup);
462 else
463 warning (_("unable to open /proc file '%s'"), filename);
465 if (stat_f)
467 xsnprintf (filename, sizeof filename, "/proc/%ld/stat", pid);
468 data = target_fileio_read_stralloc (filename);
469 if (data)
471 struct cleanup *cleanup = make_cleanup (xfree, data);
472 const char *p = data;
474 printf_filtered (_("Process: %s\n"),
475 pulongest (strtoulst (p, &p, 10)));
477 p = skip_spaces_const (p);
478 if (*p == '(')
480 const char *ep = strchr (p, ')');
481 if (ep != NULL)
483 printf_filtered ("Exec file: %.*s\n",
484 (int) (ep - p - 1), p + 1);
485 p = ep + 1;
489 p = skip_spaces_const (p);
490 if (*p)
491 printf_filtered (_("State: %c\n"), *p++);
493 if (*p)
494 printf_filtered (_("Parent process: %s\n"),
495 pulongest (strtoulst (p, &p, 10)));
496 if (*p)
497 printf_filtered (_("Process group: %s\n"),
498 pulongest (strtoulst (p, &p, 10)));
499 if (*p)
500 printf_filtered (_("Session id: %s\n"),
501 pulongest (strtoulst (p, &p, 10)));
502 if (*p)
503 printf_filtered (_("TTY: %s\n"),
504 pulongest (strtoulst (p, &p, 10)));
505 if (*p)
506 printf_filtered (_("TTY owner process group: %s\n"),
507 pulongest (strtoulst (p, &p, 10)));
509 if (*p)
510 printf_filtered (_("Flags: %s\n"),
511 hex_string (strtoulst (p, &p, 10)));
512 if (*p)
513 printf_filtered (_("Minor faults (no memory page): %s\n"),
514 pulongest (strtoulst (p, &p, 10)));
515 if (*p)
516 printf_filtered (_("Minor faults, children: %s\n"),
517 pulongest (strtoulst (p, &p, 10)));
518 if (*p)
519 printf_filtered (_("Major faults (memory page faults): %s\n"),
520 pulongest (strtoulst (p, &p, 10)));
521 if (*p)
522 printf_filtered (_("Major faults, children: %s\n"),
523 pulongest (strtoulst (p, &p, 10)));
524 if (*p)
525 printf_filtered (_("utime: %s\n"),
526 pulongest (strtoulst (p, &p, 10)));
527 if (*p)
528 printf_filtered (_("stime: %s\n"),
529 pulongest (strtoulst (p, &p, 10)));
530 if (*p)
531 printf_filtered (_("utime, children: %s\n"),
532 pulongest (strtoulst (p, &p, 10)));
533 if (*p)
534 printf_filtered (_("stime, children: %s\n"),
535 pulongest (strtoulst (p, &p, 10)));
536 if (*p)
537 printf_filtered (_("jiffies remaining in current "
538 "time slice: %s\n"),
539 pulongest (strtoulst (p, &p, 10)));
540 if (*p)
541 printf_filtered (_("'nice' value: %s\n"),
542 pulongest (strtoulst (p, &p, 10)));
543 if (*p)
544 printf_filtered (_("jiffies until next timeout: %s\n"),
545 pulongest (strtoulst (p, &p, 10)));
546 if (*p)
547 printf_filtered (_("jiffies until next SIGALRM: %s\n"),
548 pulongest (strtoulst (p, &p, 10)));
549 if (*p)
550 printf_filtered (_("start time (jiffies since "
551 "system boot): %s\n"),
552 pulongest (strtoulst (p, &p, 10)));
553 if (*p)
554 printf_filtered (_("Virtual memory size: %s\n"),
555 pulongest (strtoulst (p, &p, 10)));
556 if (*p)
557 printf_filtered (_("Resident set size: %s\n"),
558 pulongest (strtoulst (p, &p, 10)));
559 if (*p)
560 printf_filtered (_("rlim: %s\n"),
561 pulongest (strtoulst (p, &p, 10)));
562 if (*p)
563 printf_filtered (_("Start of text: %s\n"),
564 hex_string (strtoulst (p, &p, 10)));
565 if (*p)
566 printf_filtered (_("End of text: %s\n"),
567 hex_string (strtoulst (p, &p, 10)));
568 if (*p)
569 printf_filtered (_("Start of stack: %s\n"),
570 hex_string (strtoulst (p, &p, 10)));
571 #if 0 /* Don't know how architecture-dependent the rest is...
572 Anyway the signal bitmap info is available from "status". */
573 if (*p)
574 printf_filtered (_("Kernel stack pointer: %s\n"),
575 hex_string (strtoulst (p, &p, 10)));
576 if (*p)
577 printf_filtered (_("Kernel instr pointer: %s\n"),
578 hex_string (strtoulst (p, &p, 10)));
579 if (*p)
580 printf_filtered (_("Pending signals bitmap: %s\n"),
581 hex_string (strtoulst (p, &p, 10)));
582 if (*p)
583 printf_filtered (_("Blocked signals bitmap: %s\n"),
584 hex_string (strtoulst (p, &p, 10)));
585 if (*p)
586 printf_filtered (_("Ignored signals bitmap: %s\n"),
587 hex_string (strtoulst (p, &p, 10)));
588 if (*p)
589 printf_filtered (_("Catched signals bitmap: %s\n"),
590 hex_string (strtoulst (p, &p, 10)));
591 if (*p)
592 printf_filtered (_("wchan (system call): %s\n"),
593 hex_string (strtoulst (p, &p, 10)));
594 #endif
595 do_cleanups (cleanup);
597 else
598 warning (_("unable to open /proc file '%s'"), filename);
602 /* Implement "info proc mappings" for a corefile. */
604 static void
605 linux_core_info_proc_mappings (struct gdbarch *gdbarch, char *args)
607 asection *section;
608 ULONGEST count, page_size;
609 unsigned char *descdata, *filenames, *descend, *contents;
610 size_t note_size;
611 unsigned int addr_size_bits, addr_size;
612 struct cleanup *cleanup;
613 struct gdbarch *core_gdbarch = gdbarch_from_bfd (core_bfd);
614 /* We assume this for reading 64-bit core files. */
615 gdb_static_assert (sizeof (ULONGEST) >= 8);
617 section = bfd_get_section_by_name (core_bfd, ".note.linuxcore.file");
618 if (section == NULL)
620 warning (_("unable to find mappings in core file"));
621 return;
624 addr_size_bits = gdbarch_addr_bit (core_gdbarch);
625 addr_size = addr_size_bits / 8;
626 note_size = bfd_get_section_size (section);
628 if (note_size < 2 * addr_size)
629 error (_("malformed core note - too short for header"));
631 contents = xmalloc (note_size);
632 cleanup = make_cleanup (xfree, contents);
633 if (!bfd_get_section_contents (core_bfd, section, contents, 0, note_size))
634 error (_("could not get core note contents"));
636 descdata = contents;
637 descend = descdata + note_size;
639 if (descdata[note_size - 1] != '\0')
640 error (_("malformed note - does not end with \\0"));
642 count = bfd_get (addr_size_bits, core_bfd, descdata);
643 descdata += addr_size;
645 page_size = bfd_get (addr_size_bits, core_bfd, descdata);
646 descdata += addr_size;
648 if (note_size < 2 * addr_size + count * 3 * addr_size)
649 error (_("malformed note - too short for supplied file count"));
651 printf_filtered (_("Mapped address spaces:\n\n"));
652 if (gdbarch_addr_bit (gdbarch) == 32)
654 printf_filtered ("\t%10s %10s %10s %10s %s\n",
655 "Start Addr",
656 " End Addr",
657 " Size", " Offset", "objfile");
659 else
661 printf_filtered (" %18s %18s %10s %10s %s\n",
662 "Start Addr",
663 " End Addr",
664 " Size", " Offset", "objfile");
667 filenames = descdata + count * 3 * addr_size;
668 while (--count > 0)
670 ULONGEST start, end, file_ofs;
672 if (filenames == descend)
673 error (_("malformed note - filenames end too early"));
675 start = bfd_get (addr_size_bits, core_bfd, descdata);
676 descdata += addr_size;
677 end = bfd_get (addr_size_bits, core_bfd, descdata);
678 descdata += addr_size;
679 file_ofs = bfd_get (addr_size_bits, core_bfd, descdata);
680 descdata += addr_size;
682 file_ofs *= page_size;
684 if (gdbarch_addr_bit (gdbarch) == 32)
685 printf_filtered ("\t%10s %10s %10s %10s %s\n",
686 paddress (gdbarch, start),
687 paddress (gdbarch, end),
688 hex_string (end - start),
689 hex_string (file_ofs),
690 filenames);
691 else
692 printf_filtered (" %18s %18s %10s %10s %s\n",
693 paddress (gdbarch, start),
694 paddress (gdbarch, end),
695 hex_string (end - start),
696 hex_string (file_ofs),
697 filenames);
699 filenames += 1 + strlen ((char *) filenames);
702 do_cleanups (cleanup);
705 /* Implement "info proc" for a corefile. */
707 static void
708 linux_core_info_proc (struct gdbarch *gdbarch, char *args,
709 enum info_proc_what what)
711 int exe_f = (what == IP_MINIMAL || what == IP_EXE || what == IP_ALL);
712 int mappings_f = (what == IP_MAPPINGS || what == IP_ALL);
714 if (exe_f)
716 const char *exe;
718 exe = bfd_core_file_failing_command (core_bfd);
719 if (exe != NULL)
720 printf_filtered ("exe = '%s'\n", exe);
721 else
722 warning (_("unable to find command name in core file"));
725 if (mappings_f)
726 linux_core_info_proc_mappings (gdbarch, args);
728 if (!exe_f && !mappings_f)
729 error (_("unable to handle request"));
732 typedef int linux_find_memory_region_ftype (ULONGEST vaddr, ULONGEST size,
733 ULONGEST offset, ULONGEST inode,
734 int read, int write,
735 int exec, int modified,
736 const char *filename,
737 void *data);
739 /* List memory regions in the inferior for a corefile. */
741 static int
742 linux_find_memory_regions_full (struct gdbarch *gdbarch,
743 linux_find_memory_region_ftype *func,
744 void *obfd)
746 char mapsfilename[100];
747 char *data;
749 /* We need to know the real target PID to access /proc. */
750 if (current_inferior ()->fake_pid_p)
751 return 1;
753 xsnprintf (mapsfilename, sizeof mapsfilename,
754 "/proc/%d/smaps", current_inferior ()->pid);
755 data = target_fileio_read_stralloc (mapsfilename);
756 if (data == NULL)
758 /* Older Linux kernels did not support /proc/PID/smaps. */
759 xsnprintf (mapsfilename, sizeof mapsfilename,
760 "/proc/%d/maps", current_inferior ()->pid);
761 data = target_fileio_read_stralloc (mapsfilename);
763 if (data)
765 struct cleanup *cleanup = make_cleanup (xfree, data);
766 char *line;
768 line = strtok (data, "\n");
769 while (line)
771 ULONGEST addr, endaddr, offset, inode;
772 const char *permissions, *device, *filename;
773 size_t permissions_len, device_len;
774 int read, write, exec;
775 int modified = 0, has_anonymous = 0;
777 read_mapping (line, &addr, &endaddr, &permissions, &permissions_len,
778 &offset, &device, &device_len, &inode, &filename);
780 /* Decode permissions. */
781 read = (memchr (permissions, 'r', permissions_len) != 0);
782 write = (memchr (permissions, 'w', permissions_len) != 0);
783 exec = (memchr (permissions, 'x', permissions_len) != 0);
785 /* Try to detect if region was modified by parsing smaps counters. */
786 for (line = strtok (NULL, "\n");
787 line && line[0] >= 'A' && line[0] <= 'Z';
788 line = strtok (NULL, "\n"))
790 char keyword[64 + 1];
792 if (sscanf (line, "%64s", keyword) != 1)
794 warning (_("Error parsing {s,}maps file '%s'"), mapsfilename);
795 break;
797 if (strcmp (keyword, "Anonymous:") == 0)
798 has_anonymous = 1;
799 if (strcmp (keyword, "Shared_Dirty:") == 0
800 || strcmp (keyword, "Private_Dirty:") == 0
801 || strcmp (keyword, "Swap:") == 0
802 || strcmp (keyword, "Anonymous:") == 0)
804 unsigned long number;
806 if (sscanf (line, "%*s%lu", &number) != 1)
808 warning (_("Error parsing {s,}maps file '%s' number"),
809 mapsfilename);
810 break;
812 if (number != 0)
813 modified = 1;
817 /* Older Linux kernels did not support the "Anonymous:" counter.
818 If it is missing, we can't be sure - dump all the pages. */
819 if (!has_anonymous)
820 modified = 1;
822 /* Invoke the callback function to create the corefile segment. */
823 func (addr, endaddr - addr, offset, inode,
824 read, write, exec, modified, filename, obfd);
827 do_cleanups (cleanup);
828 return 0;
831 return 1;
834 /* A structure for passing information through
835 linux_find_memory_regions_full. */
837 struct linux_find_memory_regions_data
839 /* The original callback. */
841 find_memory_region_ftype func;
843 /* The original datum. */
845 void *obfd;
848 /* A callback for linux_find_memory_regions that converts between the
849 "full"-style callback and find_memory_region_ftype. */
851 static int
852 linux_find_memory_regions_thunk (ULONGEST vaddr, ULONGEST size,
853 ULONGEST offset, ULONGEST inode,
854 int read, int write, int exec, int modified,
855 const char *filename, void *arg)
857 struct linux_find_memory_regions_data *data = arg;
859 return data->func (vaddr, size, read, write, exec, modified, data->obfd);
862 /* A variant of linux_find_memory_regions_full that is suitable as the
863 gdbarch find_memory_regions method. */
865 static int
866 linux_find_memory_regions (struct gdbarch *gdbarch,
867 find_memory_region_ftype func, void *obfd)
869 struct linux_find_memory_regions_data data;
871 data.func = func;
872 data.obfd = obfd;
874 return linux_find_memory_regions_full (gdbarch,
875 linux_find_memory_regions_thunk,
876 &data);
879 /* Determine which signal stopped execution. */
881 static int
882 find_signalled_thread (struct thread_info *info, void *data)
884 if (info->suspend.stop_signal != GDB_SIGNAL_0
885 && ptid_get_pid (info->ptid) == ptid_get_pid (inferior_ptid))
886 return 1;
888 return 0;
891 static enum gdb_signal
892 find_stop_signal (void)
894 struct thread_info *info =
895 iterate_over_threads (find_signalled_thread, NULL);
897 if (info)
898 return info->suspend.stop_signal;
899 else
900 return GDB_SIGNAL_0;
903 /* Generate corefile notes for SPU contexts. */
905 static char *
906 linux_spu_make_corefile_notes (bfd *obfd, char *note_data, int *note_size)
908 static const char *spu_files[] =
910 "object-id",
911 "mem",
912 "regs",
913 "fpcr",
914 "lslr",
915 "decr",
916 "decr_status",
917 "signal1",
918 "signal1_type",
919 "signal2",
920 "signal2_type",
921 "event_mask",
922 "event_status",
923 "mbox_info",
924 "ibox_info",
925 "wbox_info",
926 "dma_info",
927 "proxydma_info",
930 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
931 gdb_byte *spu_ids;
932 LONGEST i, j, size;
934 /* Determine list of SPU ids. */
935 size = target_read_alloc (&current_target, TARGET_OBJECT_SPU,
936 NULL, &spu_ids);
938 /* Generate corefile notes for each SPU file. */
939 for (i = 0; i < size; i += 4)
941 int fd = extract_unsigned_integer (spu_ids + i, 4, byte_order);
943 for (j = 0; j < sizeof (spu_files) / sizeof (spu_files[0]); j++)
945 char annex[32], note_name[32];
946 gdb_byte *spu_data;
947 LONGEST spu_len;
949 xsnprintf (annex, sizeof annex, "%d/%s", fd, spu_files[j]);
950 spu_len = target_read_alloc (&current_target, TARGET_OBJECT_SPU,
951 annex, &spu_data);
952 if (spu_len > 0)
954 xsnprintf (note_name, sizeof note_name, "SPU/%s", annex);
955 note_data = elfcore_write_note (obfd, note_data, note_size,
956 note_name, NT_SPU,
957 spu_data, spu_len);
958 xfree (spu_data);
960 if (!note_data)
962 xfree (spu_ids);
963 return NULL;
969 if (size > 0)
970 xfree (spu_ids);
972 return note_data;
975 /* This is used to pass information from
976 linux_make_mappings_corefile_notes through
977 linux_find_memory_regions_full. */
979 struct linux_make_mappings_data
981 /* Number of files mapped. */
982 ULONGEST file_count;
984 /* The obstack for the main part of the data. */
985 struct obstack *data_obstack;
987 /* The filename obstack. */
988 struct obstack *filename_obstack;
990 /* The architecture's "long" type. */
991 struct type *long_type;
994 static linux_find_memory_region_ftype linux_make_mappings_callback;
996 /* A callback for linux_find_memory_regions_full that updates the
997 mappings data for linux_make_mappings_corefile_notes. */
999 static int
1000 linux_make_mappings_callback (ULONGEST vaddr, ULONGEST size,
1001 ULONGEST offset, ULONGEST inode,
1002 int read, int write, int exec, int modified,
1003 const char *filename, void *data)
1005 struct linux_make_mappings_data *map_data = data;
1006 gdb_byte buf[sizeof (ULONGEST)];
1008 if (*filename == '\0' || inode == 0)
1009 return 0;
1011 ++map_data->file_count;
1013 pack_long (buf, map_data->long_type, vaddr);
1014 obstack_grow (map_data->data_obstack, buf, TYPE_LENGTH (map_data->long_type));
1015 pack_long (buf, map_data->long_type, vaddr + size);
1016 obstack_grow (map_data->data_obstack, buf, TYPE_LENGTH (map_data->long_type));
1017 pack_long (buf, map_data->long_type, offset);
1018 obstack_grow (map_data->data_obstack, buf, TYPE_LENGTH (map_data->long_type));
1020 obstack_grow_str0 (map_data->filename_obstack, filename);
1022 return 0;
1025 /* Write the file mapping data to the core file, if possible. OBFD is
1026 the output BFD. NOTE_DATA is the current note data, and NOTE_SIZE
1027 is a pointer to the note size. Returns the new NOTE_DATA and
1028 updates NOTE_SIZE. */
1030 static char *
1031 linux_make_mappings_corefile_notes (struct gdbarch *gdbarch, bfd *obfd,
1032 char *note_data, int *note_size)
1034 struct cleanup *cleanup;
1035 struct obstack data_obstack, filename_obstack;
1036 struct linux_make_mappings_data mapping_data;
1037 struct type *long_type
1038 = arch_integer_type (gdbarch, gdbarch_long_bit (gdbarch), 0, "long");
1039 gdb_byte buf[sizeof (ULONGEST)];
1041 obstack_init (&data_obstack);
1042 cleanup = make_cleanup_obstack_free (&data_obstack);
1043 obstack_init (&filename_obstack);
1044 make_cleanup_obstack_free (&filename_obstack);
1046 mapping_data.file_count = 0;
1047 mapping_data.data_obstack = &data_obstack;
1048 mapping_data.filename_obstack = &filename_obstack;
1049 mapping_data.long_type = long_type;
1051 /* Reserve space for the count. */
1052 obstack_blank (&data_obstack, TYPE_LENGTH (long_type));
1053 /* We always write the page size as 1 since we have no good way to
1054 determine the correct value. */
1055 pack_long (buf, long_type, 1);
1056 obstack_grow (&data_obstack, buf, TYPE_LENGTH (long_type));
1058 linux_find_memory_regions_full (gdbarch, linux_make_mappings_callback,
1059 &mapping_data);
1061 if (mapping_data.file_count != 0)
1063 /* Write the count to the obstack. */
1064 pack_long ((gdb_byte *) obstack_base (&data_obstack),
1065 long_type, mapping_data.file_count);
1067 /* Copy the filenames to the data obstack. */
1068 obstack_grow (&data_obstack, obstack_base (&filename_obstack),
1069 obstack_object_size (&filename_obstack));
1071 note_data = elfcore_write_note (obfd, note_data, note_size,
1072 "CORE", NT_FILE,
1073 obstack_base (&data_obstack),
1074 obstack_object_size (&data_obstack));
1077 do_cleanups (cleanup);
1078 return note_data;
1081 /* Records the thread's register state for the corefile note
1082 section. */
1084 static char *
1085 linux_collect_thread_registers (const struct regcache *regcache,
1086 ptid_t ptid, bfd *obfd,
1087 char *note_data, int *note_size,
1088 enum gdb_signal stop_signal)
1090 struct gdbarch *gdbarch = get_regcache_arch (regcache);
1091 struct core_regset_section *sect_list;
1092 unsigned long lwp;
1094 sect_list = gdbarch_core_regset_sections (gdbarch);
1095 gdb_assert (sect_list);
1097 /* For remote targets the LWP may not be available, so use the TID. */
1098 lwp = ptid_get_lwp (ptid);
1099 if (!lwp)
1100 lwp = ptid_get_tid (ptid);
1102 while (sect_list->sect_name != NULL)
1104 const struct regset *regset;
1105 char *buf;
1107 regset = gdbarch_regset_from_core_section (gdbarch,
1108 sect_list->sect_name,
1109 sect_list->size);
1110 gdb_assert (regset && regset->collect_regset);
1112 buf = xmalloc (sect_list->size);
1113 regset->collect_regset (regset, regcache, -1, buf, sect_list->size);
1115 /* PRSTATUS still needs to be treated specially. */
1116 if (strcmp (sect_list->sect_name, ".reg") == 0)
1117 note_data = (char *) elfcore_write_prstatus
1118 (obfd, note_data, note_size, lwp,
1119 gdb_signal_to_host (stop_signal), buf);
1120 else
1121 note_data = (char *) elfcore_write_register_note
1122 (obfd, note_data, note_size,
1123 sect_list->sect_name, buf, sect_list->size);
1124 xfree (buf);
1125 sect_list++;
1127 if (!note_data)
1128 return NULL;
1131 return note_data;
1134 /* Fetch the siginfo data for the current thread, if it exists. If
1135 there is no data, or we could not read it, return NULL. Otherwise,
1136 return a newly malloc'd buffer holding the data and fill in *SIZE
1137 with the size of the data. The caller is responsible for freeing
1138 the data. */
1140 static gdb_byte *
1141 linux_get_siginfo_data (struct gdbarch *gdbarch, LONGEST *size)
1143 struct type *siginfo_type;
1144 gdb_byte *buf;
1145 LONGEST bytes_read;
1146 struct cleanup *cleanups;
1148 if (!gdbarch_get_siginfo_type_p (gdbarch))
1149 return NULL;
1151 siginfo_type = gdbarch_get_siginfo_type (gdbarch);
1153 buf = xmalloc (TYPE_LENGTH (siginfo_type));
1154 cleanups = make_cleanup (xfree, buf);
1156 bytes_read = target_read (&current_target, TARGET_OBJECT_SIGNAL_INFO, NULL,
1157 buf, 0, TYPE_LENGTH (siginfo_type));
1158 if (bytes_read == TYPE_LENGTH (siginfo_type))
1160 discard_cleanups (cleanups);
1161 *size = bytes_read;
1163 else
1165 do_cleanups (cleanups);
1166 buf = NULL;
1169 return buf;
1172 struct linux_corefile_thread_data
1174 struct gdbarch *gdbarch;
1175 int pid;
1176 bfd *obfd;
1177 char *note_data;
1178 int *note_size;
1179 int num_notes;
1180 enum gdb_signal stop_signal;
1181 linux_collect_thread_registers_ftype collect;
1184 /* Called by gdbthread.c once per thread. Records the thread's
1185 register state for the corefile note section. */
1187 static int
1188 linux_corefile_thread_callback (struct thread_info *info, void *data)
1190 struct linux_corefile_thread_data *args = data;
1192 if (ptid_get_pid (info->ptid) == args->pid)
1194 struct cleanup *old_chain;
1195 struct regcache *regcache;
1196 gdb_byte *siginfo_data;
1197 LONGEST siginfo_size;
1199 regcache = get_thread_arch_regcache (info->ptid, args->gdbarch);
1201 old_chain = save_inferior_ptid ();
1202 inferior_ptid = info->ptid;
1203 target_fetch_registers (regcache, -1);
1204 siginfo_data = linux_get_siginfo_data (args->gdbarch, &siginfo_size);
1205 do_cleanups (old_chain);
1207 old_chain = make_cleanup (xfree, siginfo_data);
1209 args->note_data = args->collect (regcache, info->ptid, args->obfd,
1210 args->note_data, args->note_size,
1211 args->stop_signal);
1212 args->num_notes++;
1214 if (siginfo_data != NULL)
1216 args->note_data = elfcore_write_note (args->obfd,
1217 args->note_data,
1218 args->note_size,
1219 "CORE", NT_SIGINFO,
1220 siginfo_data, siginfo_size);
1221 args->num_notes++;
1224 do_cleanups (old_chain);
1227 return !args->note_data;
1230 /* Fill the PRPSINFO structure with information about the process being
1231 debugged. Returns 1 in case of success, 0 for failures. Please note that
1232 even if the structure cannot be entirely filled (e.g., GDB was unable to
1233 gather information about the process UID/GID), this function will still
1234 return 1 since some information was already recorded. It will only return
1235 0 iff nothing can be gathered. */
1237 static int
1238 linux_fill_prpsinfo (struct elf_internal_linux_prpsinfo *p)
1240 /* The filename which we will use to obtain some info about the process.
1241 We will basically use this to store the `/proc/PID/FILENAME' file. */
1242 char filename[100];
1243 /* The full name of the program which generated the corefile. */
1244 char *fname;
1245 /* The basename of the executable. */
1246 const char *basename;
1247 /* The arguments of the program. */
1248 char *psargs;
1249 char *infargs;
1250 /* The contents of `/proc/PID/stat' and `/proc/PID/status' files. */
1251 char *proc_stat, *proc_status;
1252 /* Temporary buffer. */
1253 char *tmpstr;
1254 /* The valid states of a process, according to the Linux kernel. */
1255 const char valid_states[] = "RSDTZW";
1256 /* The program state. */
1257 const char *prog_state;
1258 /* The state of the process. */
1259 char pr_sname;
1260 /* The PID of the program which generated the corefile. */
1261 pid_t pid;
1262 /* Process flags. */
1263 unsigned int pr_flag;
1264 /* Process nice value. */
1265 long pr_nice;
1266 /* The number of fields read by `sscanf'. */
1267 int n_fields = 0;
1268 /* Cleanups. */
1269 struct cleanup *c;
1270 int i;
1272 gdb_assert (p != NULL);
1274 /* Obtaining PID and filename. */
1275 pid = ptid_get_pid (inferior_ptid);
1276 xsnprintf (filename, sizeof (filename), "/proc/%d/cmdline", (int) pid);
1277 fname = target_fileio_read_stralloc (filename);
1279 if (fname == NULL || *fname == '\0')
1281 /* No program name was read, so we won't be able to retrieve more
1282 information about the process. */
1283 xfree (fname);
1284 return 0;
1287 c = make_cleanup (xfree, fname);
1288 memset (p, 0, sizeof (*p));
1290 /* Defining the PID. */
1291 p->pr_pid = pid;
1293 /* Copying the program name. Only the basename matters. */
1294 basename = lbasename (fname);
1295 strncpy (p->pr_fname, basename, sizeof (p->pr_fname));
1296 p->pr_fname[sizeof (p->pr_fname) - 1] = '\0';
1298 infargs = get_inferior_args ();
1300 psargs = xstrdup (fname);
1301 if (infargs != NULL)
1302 psargs = reconcat (psargs, psargs, " ", infargs, NULL);
1304 make_cleanup (xfree, psargs);
1306 strncpy (p->pr_psargs, psargs, sizeof (p->pr_psargs));
1307 p->pr_psargs[sizeof (p->pr_psargs) - 1] = '\0';
1309 xsnprintf (filename, sizeof (filename), "/proc/%d/stat", (int) pid);
1310 proc_stat = target_fileio_read_stralloc (filename);
1311 make_cleanup (xfree, proc_stat);
1313 if (proc_stat == NULL || *proc_stat == '\0')
1315 /* Despite being unable to read more information about the
1316 process, we return 1 here because at least we have its
1317 command line, PID and arguments. */
1318 do_cleanups (c);
1319 return 1;
1322 /* Ok, we have the stats. It's time to do a little parsing of the
1323 contents of the buffer, so that we end up reading what we want.
1325 The following parsing mechanism is strongly based on the
1326 information generated by the `fs/proc/array.c' file, present in
1327 the Linux kernel tree. More details about how the information is
1328 displayed can be obtained by seeing the manpage of proc(5),
1329 specifically under the entry of `/proc/[pid]/stat'. */
1331 /* Getting rid of the PID, since we already have it. */
1332 while (isdigit (*proc_stat))
1333 ++proc_stat;
1335 proc_stat = skip_spaces (proc_stat);
1337 /* Getting rid of the executable name, since we already have it. We
1338 know that this name will be in parentheses, so we can safely look
1339 for the close-paren. */
1340 while (*proc_stat != ')')
1341 ++proc_stat;
1342 ++proc_stat;
1344 proc_stat = skip_spaces (proc_stat);
1346 n_fields = sscanf (proc_stat,
1347 "%c" /* Process state. */
1348 "%d%d%d" /* Parent PID, group ID, session ID. */
1349 "%*d%*d" /* tty_nr, tpgid (not used). */
1350 "%u" /* Flags. */
1351 "%*s%*s%*s%*s" /* minflt, cminflt, majflt,
1352 cmajflt (not used). */
1353 "%*s%*s%*s%*s" /* utime, stime, cutime,
1354 cstime (not used). */
1355 "%*s" /* Priority (not used). */
1356 "%ld", /* Nice. */
1357 &pr_sname,
1358 &p->pr_ppid, &p->pr_pgrp, &p->pr_sid,
1359 &pr_flag,
1360 &pr_nice);
1362 if (n_fields != 6)
1364 /* Again, we couldn't read the complementary information about
1365 the process state. However, we already have minimal
1366 information, so we just return 1 here. */
1367 do_cleanups (c);
1368 return 1;
1371 /* Filling the structure fields. */
1372 prog_state = strchr (valid_states, pr_sname);
1373 if (prog_state != NULL)
1374 p->pr_state = prog_state - valid_states;
1375 else
1377 /* Zero means "Running". */
1378 p->pr_state = 0;
1381 p->pr_sname = p->pr_state > 5 ? '.' : pr_sname;
1382 p->pr_zomb = p->pr_sname == 'Z';
1383 p->pr_nice = pr_nice;
1384 p->pr_flag = pr_flag;
1386 /* Finally, obtaining the UID and GID. For that, we read and parse the
1387 contents of the `/proc/PID/status' file. */
1388 xsnprintf (filename, sizeof (filename), "/proc/%d/status", (int) pid);
1389 proc_status = target_fileio_read_stralloc (filename);
1390 make_cleanup (xfree, proc_status);
1392 if (proc_status == NULL || *proc_status == '\0')
1394 /* Returning 1 since we already have a bunch of information. */
1395 do_cleanups (c);
1396 return 1;
1399 /* Extracting the UID. */
1400 tmpstr = strstr (proc_status, "Uid:");
1401 if (tmpstr != NULL)
1403 /* Advancing the pointer to the beginning of the UID. */
1404 tmpstr += sizeof ("Uid:");
1405 while (*tmpstr != '\0' && !isdigit (*tmpstr))
1406 ++tmpstr;
1408 if (isdigit (*tmpstr))
1409 p->pr_uid = strtol (tmpstr, &tmpstr, 10);
1412 /* Extracting the GID. */
1413 tmpstr = strstr (proc_status, "Gid:");
1414 if (tmpstr != NULL)
1416 /* Advancing the pointer to the beginning of the GID. */
1417 tmpstr += sizeof ("Gid:");
1418 while (*tmpstr != '\0' && !isdigit (*tmpstr))
1419 ++tmpstr;
1421 if (isdigit (*tmpstr))
1422 p->pr_gid = strtol (tmpstr, &tmpstr, 10);
1425 do_cleanups (c);
1427 return 1;
1430 /* Fills the "to_make_corefile_note" target vector. Builds the note
1431 section for a corefile, and returns it in a malloc buffer. */
1433 char *
1434 linux_make_corefile_notes (struct gdbarch *gdbarch, bfd *obfd, int *note_size,
1435 linux_collect_thread_registers_ftype collect)
1437 struct linux_corefile_thread_data thread_args;
1438 struct elf_internal_linux_prpsinfo prpsinfo;
1439 char *note_data = NULL;
1440 gdb_byte *auxv;
1441 int auxv_len;
1443 if (linux_fill_prpsinfo (&prpsinfo))
1445 if (gdbarch_elfcore_write_linux_prpsinfo_p (gdbarch))
1447 note_data = gdbarch_elfcore_write_linux_prpsinfo (gdbarch, obfd,
1448 note_data, note_size,
1449 &prpsinfo);
1451 else
1453 if (gdbarch_ptr_bit (gdbarch) == 64)
1454 note_data = elfcore_write_linux_prpsinfo64 (obfd,
1455 note_data, note_size,
1456 &prpsinfo);
1457 else
1458 note_data = elfcore_write_linux_prpsinfo32 (obfd,
1459 note_data, note_size,
1460 &prpsinfo);
1464 /* Thread register information. */
1465 thread_args.gdbarch = gdbarch;
1466 thread_args.pid = ptid_get_pid (inferior_ptid);
1467 thread_args.obfd = obfd;
1468 thread_args.note_data = note_data;
1469 thread_args.note_size = note_size;
1470 thread_args.num_notes = 0;
1471 thread_args.stop_signal = find_stop_signal ();
1472 thread_args.collect = collect;
1473 iterate_over_threads (linux_corefile_thread_callback, &thread_args);
1474 note_data = thread_args.note_data;
1475 if (!note_data)
1476 return NULL;
1478 /* Auxillary vector. */
1479 auxv_len = target_read_alloc (&current_target, TARGET_OBJECT_AUXV,
1480 NULL, &auxv);
1481 if (auxv_len > 0)
1483 note_data = elfcore_write_note (obfd, note_data, note_size,
1484 "CORE", NT_AUXV, auxv, auxv_len);
1485 xfree (auxv);
1487 if (!note_data)
1488 return NULL;
1491 /* SPU information. */
1492 note_data = linux_spu_make_corefile_notes (obfd, note_data, note_size);
1493 if (!note_data)
1494 return NULL;
1496 /* File mappings. */
1497 note_data = linux_make_mappings_corefile_notes (gdbarch, obfd,
1498 note_data, note_size);
1500 make_cleanup (xfree, note_data);
1501 return note_data;
1504 static char *
1505 linux_make_corefile_notes_1 (struct gdbarch *gdbarch, bfd *obfd, int *note_size)
1507 /* FIXME: uweigand/2011-10-06: Once all GNU/Linux architectures have been
1508 converted to gdbarch_core_regset_sections, we no longer need to fall back
1509 to the target method at this point. */
1511 if (!gdbarch_core_regset_sections (gdbarch))
1512 return target_make_corefile_notes (obfd, note_size);
1513 else
1514 return linux_make_corefile_notes (gdbarch, obfd, note_size,
1515 linux_collect_thread_registers);
1518 /* Implementation of `gdbarch_gdb_signal_from_target', as defined in
1519 gdbarch.h. This function is not static because it is exported to
1520 other -tdep files. */
1522 enum gdb_signal
1523 linux_gdb_signal_from_target (struct gdbarch *gdbarch, int signal)
1525 switch (signal)
1527 case 0:
1528 return GDB_SIGNAL_0;
1530 case LINUX_SIGHUP:
1531 return GDB_SIGNAL_HUP;
1533 case LINUX_SIGINT:
1534 return GDB_SIGNAL_INT;
1536 case LINUX_SIGQUIT:
1537 return GDB_SIGNAL_QUIT;
1539 case LINUX_SIGILL:
1540 return GDB_SIGNAL_ILL;
1542 case LINUX_SIGTRAP:
1543 return GDB_SIGNAL_TRAP;
1545 case LINUX_SIGABRT:
1546 return GDB_SIGNAL_ABRT;
1548 case LINUX_SIGBUS:
1549 return GDB_SIGNAL_BUS;
1551 case LINUX_SIGFPE:
1552 return GDB_SIGNAL_FPE;
1554 case LINUX_SIGKILL:
1555 return GDB_SIGNAL_KILL;
1557 case LINUX_SIGUSR1:
1558 return GDB_SIGNAL_USR1;
1560 case LINUX_SIGSEGV:
1561 return GDB_SIGNAL_SEGV;
1563 case LINUX_SIGUSR2:
1564 return GDB_SIGNAL_USR2;
1566 case LINUX_SIGPIPE:
1567 return GDB_SIGNAL_PIPE;
1569 case LINUX_SIGALRM:
1570 return GDB_SIGNAL_ALRM;
1572 case LINUX_SIGTERM:
1573 return GDB_SIGNAL_TERM;
1575 case LINUX_SIGCHLD:
1576 return GDB_SIGNAL_CHLD;
1578 case LINUX_SIGCONT:
1579 return GDB_SIGNAL_CONT;
1581 case LINUX_SIGSTOP:
1582 return GDB_SIGNAL_STOP;
1584 case LINUX_SIGTSTP:
1585 return GDB_SIGNAL_TSTP;
1587 case LINUX_SIGTTIN:
1588 return GDB_SIGNAL_TTIN;
1590 case LINUX_SIGTTOU:
1591 return GDB_SIGNAL_TTOU;
1593 case LINUX_SIGURG:
1594 return GDB_SIGNAL_URG;
1596 case LINUX_SIGXCPU:
1597 return GDB_SIGNAL_XCPU;
1599 case LINUX_SIGXFSZ:
1600 return GDB_SIGNAL_XFSZ;
1602 case LINUX_SIGVTALRM:
1603 return GDB_SIGNAL_VTALRM;
1605 case LINUX_SIGPROF:
1606 return GDB_SIGNAL_PROF;
1608 case LINUX_SIGWINCH:
1609 return GDB_SIGNAL_WINCH;
1611 /* No way to differentiate between SIGIO and SIGPOLL.
1612 Therefore, we just handle the first one. */
1613 case LINUX_SIGIO:
1614 return GDB_SIGNAL_IO;
1616 case LINUX_SIGPWR:
1617 return GDB_SIGNAL_PWR;
1619 case LINUX_SIGSYS:
1620 return GDB_SIGNAL_SYS;
1622 /* SIGRTMIN and SIGRTMAX are not continuous in <gdb/signals.def>,
1623 therefore we have to handle them here. */
1624 case LINUX_SIGRTMIN:
1625 return GDB_SIGNAL_REALTIME_32;
1627 case LINUX_SIGRTMAX:
1628 return GDB_SIGNAL_REALTIME_64;
1631 if (signal >= LINUX_SIGRTMIN + 1 && signal <= LINUX_SIGRTMAX - 1)
1633 int offset = signal - LINUX_SIGRTMIN + 1;
1635 return (enum gdb_signal) ((int) GDB_SIGNAL_REALTIME_33 + offset);
1638 return GDB_SIGNAL_UNKNOWN;
1641 /* Implementation of `gdbarch_gdb_signal_to_target', as defined in
1642 gdbarch.h. This function is not static because it is exported to
1643 other -tdep files. */
1646 linux_gdb_signal_to_target (struct gdbarch *gdbarch,
1647 enum gdb_signal signal)
1649 switch (signal)
1651 case GDB_SIGNAL_0:
1652 return 0;
1654 case GDB_SIGNAL_HUP:
1655 return LINUX_SIGHUP;
1657 case GDB_SIGNAL_INT:
1658 return LINUX_SIGINT;
1660 case GDB_SIGNAL_QUIT:
1661 return LINUX_SIGQUIT;
1663 case GDB_SIGNAL_ILL:
1664 return LINUX_SIGILL;
1666 case GDB_SIGNAL_TRAP:
1667 return LINUX_SIGTRAP;
1669 case GDB_SIGNAL_ABRT:
1670 return LINUX_SIGABRT;
1672 case GDB_SIGNAL_FPE:
1673 return LINUX_SIGFPE;
1675 case GDB_SIGNAL_KILL:
1676 return LINUX_SIGKILL;
1678 case GDB_SIGNAL_BUS:
1679 return LINUX_SIGBUS;
1681 case GDB_SIGNAL_SEGV:
1682 return LINUX_SIGSEGV;
1684 case GDB_SIGNAL_SYS:
1685 return LINUX_SIGSYS;
1687 case GDB_SIGNAL_PIPE:
1688 return LINUX_SIGPIPE;
1690 case GDB_SIGNAL_ALRM:
1691 return LINUX_SIGALRM;
1693 case GDB_SIGNAL_TERM:
1694 return LINUX_SIGTERM;
1696 case GDB_SIGNAL_URG:
1697 return LINUX_SIGURG;
1699 case GDB_SIGNAL_STOP:
1700 return LINUX_SIGSTOP;
1702 case GDB_SIGNAL_TSTP:
1703 return LINUX_SIGTSTP;
1705 case GDB_SIGNAL_CONT:
1706 return LINUX_SIGCONT;
1708 case GDB_SIGNAL_CHLD:
1709 return LINUX_SIGCHLD;
1711 case GDB_SIGNAL_TTIN:
1712 return LINUX_SIGTTIN;
1714 case GDB_SIGNAL_TTOU:
1715 return LINUX_SIGTTOU;
1717 case GDB_SIGNAL_IO:
1718 return LINUX_SIGIO;
1720 case GDB_SIGNAL_XCPU:
1721 return LINUX_SIGXCPU;
1723 case GDB_SIGNAL_XFSZ:
1724 return LINUX_SIGXFSZ;
1726 case GDB_SIGNAL_VTALRM:
1727 return LINUX_SIGVTALRM;
1729 case GDB_SIGNAL_PROF:
1730 return LINUX_SIGPROF;
1732 case GDB_SIGNAL_WINCH:
1733 return LINUX_SIGWINCH;
1735 case GDB_SIGNAL_USR1:
1736 return LINUX_SIGUSR1;
1738 case GDB_SIGNAL_USR2:
1739 return LINUX_SIGUSR2;
1741 case GDB_SIGNAL_PWR:
1742 return LINUX_SIGPWR;
1744 case GDB_SIGNAL_POLL:
1745 return LINUX_SIGPOLL;
1747 /* GDB_SIGNAL_REALTIME_32 is not continuous in <gdb/signals.def>,
1748 therefore we have to handle it here. */
1749 case GDB_SIGNAL_REALTIME_32:
1750 return LINUX_SIGRTMIN;
1752 /* Same comment applies to _64. */
1753 case GDB_SIGNAL_REALTIME_64:
1754 return LINUX_SIGRTMAX;
1757 /* GDB_SIGNAL_REALTIME_33 to _64 are continuous. */
1758 if (signal >= GDB_SIGNAL_REALTIME_33
1759 && signal <= GDB_SIGNAL_REALTIME_63)
1761 int offset = signal - GDB_SIGNAL_REALTIME_33;
1763 return LINUX_SIGRTMIN + 1 + offset;
1766 return -1;
1769 /* To be called from the various GDB_OSABI_LINUX handlers for the
1770 various GNU/Linux architectures and machine types. */
1772 void
1773 linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
1775 set_gdbarch_core_pid_to_str (gdbarch, linux_core_pid_to_str);
1776 set_gdbarch_info_proc (gdbarch, linux_info_proc);
1777 set_gdbarch_core_info_proc (gdbarch, linux_core_info_proc);
1778 set_gdbarch_find_memory_regions (gdbarch, linux_find_memory_regions);
1779 set_gdbarch_make_corefile_notes (gdbarch, linux_make_corefile_notes_1);
1780 set_gdbarch_has_shared_address_space (gdbarch,
1781 linux_has_shared_address_space);
1782 set_gdbarch_gdb_signal_from_target (gdbarch,
1783 linux_gdb_signal_from_target);
1784 set_gdbarch_gdb_signal_to_target (gdbarch,
1785 linux_gdb_signal_to_target);
1788 /* Provide a prototype to silence -Wmissing-prototypes. */
1789 extern initialize_file_ftype _initialize_linux_tdep;
1791 void
1792 _initialize_linux_tdep (void)
1794 linux_gdbarch_data_handle =
1795 gdbarch_data_register_post_init (init_linux_gdbarch_data);