1 /* Target-dependent code for GNU/Linux, architecture independent.
3 Copyright (C) 2009-2012 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/>. */
22 #include "linux-tdep.h"
25 #include "gdbthread.h"
29 #include "elf/common.h"
30 #include "elf-bfd.h" /* for elfcore_write_* */
32 #include "cli/cli-utils.h"
33 #include "arch-utils.h"
34 #include "gdb_obstack.h"
38 static struct gdbarch_data *linux_gdbarch_data_handle;
40 struct linux_gdbarch_data
42 struct type *siginfo_type;
46 init_linux_gdbarch_data (struct gdbarch *gdbarch)
48 return GDBARCH_OBSTACK_ZALLOC (gdbarch, struct linux_gdbarch_data);
51 static struct linux_gdbarch_data *
52 get_linux_gdbarch_data (struct gdbarch *gdbarch)
54 return gdbarch_data (gdbarch, linux_gdbarch_data_handle);
57 /* This function is suitable for architectures that don't
58 extend/override the standard siginfo structure. */
61 linux_get_siginfo_type (struct gdbarch *gdbarch)
63 struct linux_gdbarch_data *linux_gdbarch_data;
64 struct type *int_type, *uint_type, *long_type, *void_ptr_type;
65 struct type *uid_type, *pid_type;
66 struct type *sigval_type, *clock_type;
67 struct type *siginfo_type, *sifields_type;
70 linux_gdbarch_data = get_linux_gdbarch_data (gdbarch);
71 if (linux_gdbarch_data->siginfo_type != NULL)
72 return linux_gdbarch_data->siginfo_type;
74 int_type = arch_integer_type (gdbarch, gdbarch_int_bit (gdbarch),
76 uint_type = arch_integer_type (gdbarch, gdbarch_int_bit (gdbarch),
78 long_type = arch_integer_type (gdbarch, gdbarch_long_bit (gdbarch),
80 void_ptr_type = lookup_pointer_type (builtin_type (gdbarch)->builtin_void);
83 sigval_type = arch_composite_type (gdbarch, NULL, TYPE_CODE_UNION);
84 TYPE_NAME (sigval_type) = xstrdup ("sigval_t");
85 append_composite_type_field (sigval_type, "sival_int", int_type);
86 append_composite_type_field (sigval_type, "sival_ptr", void_ptr_type);
89 pid_type = arch_type (gdbarch, TYPE_CODE_TYPEDEF,
90 TYPE_LENGTH (int_type), "__pid_t");
91 TYPE_TARGET_TYPE (pid_type) = int_type;
92 TYPE_TARGET_STUB (pid_type) = 1;
95 uid_type = arch_type (gdbarch, TYPE_CODE_TYPEDEF,
96 TYPE_LENGTH (uint_type), "__uid_t");
97 TYPE_TARGET_TYPE (uid_type) = uint_type;
98 TYPE_TARGET_STUB (uid_type) = 1;
101 clock_type = arch_type (gdbarch, TYPE_CODE_TYPEDEF,
102 TYPE_LENGTH (long_type), "__clock_t");
103 TYPE_TARGET_TYPE (clock_type) = long_type;
104 TYPE_TARGET_STUB (clock_type) = 1;
107 sifields_type = arch_composite_type (gdbarch, NULL, TYPE_CODE_UNION);
110 const int si_max_size = 128;
112 int size_of_int = gdbarch_int_bit (gdbarch) / HOST_CHAR_BIT;
115 if (gdbarch_ptr_bit (gdbarch) == 64)
116 si_pad_size = (si_max_size / size_of_int) - 4;
118 si_pad_size = (si_max_size / size_of_int) - 3;
119 append_composite_type_field (sifields_type, "_pad",
120 init_vector_type (int_type, si_pad_size));
124 type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
125 append_composite_type_field (type, "si_pid", pid_type);
126 append_composite_type_field (type, "si_uid", uid_type);
127 append_composite_type_field (sifields_type, "_kill", type);
130 type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
131 append_composite_type_field (type, "si_tid", int_type);
132 append_composite_type_field (type, "si_overrun", int_type);
133 append_composite_type_field (type, "si_sigval", sigval_type);
134 append_composite_type_field (sifields_type, "_timer", type);
137 type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
138 append_composite_type_field (type, "si_pid", pid_type);
139 append_composite_type_field (type, "si_uid", uid_type);
140 append_composite_type_field (type, "si_sigval", sigval_type);
141 append_composite_type_field (sifields_type, "_rt", type);
144 type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
145 append_composite_type_field (type, "si_pid", pid_type);
146 append_composite_type_field (type, "si_uid", uid_type);
147 append_composite_type_field (type, "si_status", int_type);
148 append_composite_type_field (type, "si_utime", clock_type);
149 append_composite_type_field (type, "si_stime", clock_type);
150 append_composite_type_field (sifields_type, "_sigchld", type);
153 type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
154 append_composite_type_field (type, "si_addr", void_ptr_type);
155 append_composite_type_field (sifields_type, "_sigfault", type);
158 type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
159 append_composite_type_field (type, "si_band", long_type);
160 append_composite_type_field (type, "si_fd", int_type);
161 append_composite_type_field (sifields_type, "_sigpoll", type);
164 siginfo_type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
165 TYPE_NAME (siginfo_type) = xstrdup ("siginfo");
166 append_composite_type_field (siginfo_type, "si_signo", int_type);
167 append_composite_type_field (siginfo_type, "si_errno", int_type);
168 append_composite_type_field (siginfo_type, "si_code", int_type);
169 append_composite_type_field_aligned (siginfo_type,
170 "_sifields", sifields_type,
171 TYPE_LENGTH (long_type));
173 linux_gdbarch_data->siginfo_type = siginfo_type;
179 linux_has_shared_address_space (struct gdbarch *gdbarch)
181 /* Determine whether we are running on uClinux or normal Linux
184 int target_is_uclinux;
187 = (target_auxv_search (¤t_target, AT_NULL, &dummy) > 0
188 && target_auxv_search (¤t_target, AT_PAGESZ, &dummy) == 0);
190 return target_is_uclinux;
193 /* This is how we want PTIDs from core files to be printed. */
196 linux_core_pid_to_str (struct gdbarch *gdbarch, ptid_t ptid)
200 if (ptid_get_lwp (ptid) != 0)
202 snprintf (buf, sizeof (buf), "LWP %ld", ptid_get_lwp (ptid));
206 return normal_pid_to_str (ptid);
209 /* Service function for corefiles and info proc. */
212 read_mapping (const char *line,
213 ULONGEST *addr, ULONGEST *endaddr,
214 const char **permissions, size_t *permissions_len,
216 const char **device, size_t *device_len,
218 const char **filename)
220 const char *p = line;
222 *addr = strtoulst (p, &p, 16);
225 *endaddr = strtoulst (p, &p, 16);
227 while (*p && isspace (*p))
230 while (*p && !isspace (*p))
232 *permissions_len = p - *permissions;
234 *offset = strtoulst (p, &p, 16);
236 while (*p && isspace (*p))
239 while (*p && !isspace (*p))
241 *device_len = p - *device;
243 *inode = strtoulst (p, &p, 10);
245 while (*p && isspace (*p))
250 /* Implement the "info proc" command. */
253 linux_info_proc (struct gdbarch *gdbarch, char *args,
254 enum info_proc_what what)
256 /* A long is used for pid instead of an int to avoid a loss of precision
257 compiler warning from the output of strtoul. */
259 int cmdline_f = (what == IP_MINIMAL || what == IP_CMDLINE || what == IP_ALL);
260 int cwd_f = (what == IP_MINIMAL || what == IP_CWD || what == IP_ALL);
261 int exe_f = (what == IP_MINIMAL || what == IP_EXE || what == IP_ALL);
262 int mappings_f = (what == IP_MAPPINGS || what == IP_ALL);
263 int status_f = (what == IP_STATUS || what == IP_ALL);
264 int stat_f = (what == IP_STAT || what == IP_ALL);
269 if (args && isdigit (args[0]))
270 pid = strtoul (args, &args, 10);
273 if (!target_has_execution)
274 error (_("No current process: you must name one."));
275 if (current_inferior ()->fake_pid_p)
276 error (_("Can't determine the current process's PID: you must name one."));
278 pid = current_inferior ()->pid;
281 args = skip_spaces (args);
283 error (_("Too many parameters: %s"), args);
285 printf_filtered (_("process %ld\n"), pid);
288 xsnprintf (filename, sizeof filename, "/proc/%ld/cmdline", pid);
289 data = target_fileio_read_stralloc (filename);
292 struct cleanup *cleanup = make_cleanup (xfree, data);
293 printf_filtered ("cmdline = '%s'\n", data);
294 do_cleanups (cleanup);
297 warning (_("unable to open /proc file '%s'"), filename);
301 xsnprintf (filename, sizeof filename, "/proc/%ld/cwd", pid);
302 data = target_fileio_readlink (filename, &target_errno);
305 struct cleanup *cleanup = make_cleanup (xfree, data);
306 printf_filtered ("cwd = '%s'\n", data);
307 do_cleanups (cleanup);
310 warning (_("unable to read link '%s'"), filename);
314 xsnprintf (filename, sizeof filename, "/proc/%ld/exe", pid);
315 data = target_fileio_readlink (filename, &target_errno);
318 struct cleanup *cleanup = make_cleanup (xfree, data);
319 printf_filtered ("exe = '%s'\n", data);
320 do_cleanups (cleanup);
323 warning (_("unable to read link '%s'"), filename);
327 xsnprintf (filename, sizeof filename, "/proc/%ld/maps", pid);
328 data = target_fileio_read_stralloc (filename);
331 struct cleanup *cleanup = make_cleanup (xfree, data);
334 printf_filtered (_("Mapped address spaces:\n\n"));
335 if (gdbarch_addr_bit (gdbarch) == 32)
337 printf_filtered ("\t%10s %10s %10s %10s %s\n",
340 " Size", " Offset", "objfile");
344 printf_filtered (" %18s %18s %10s %10s %s\n",
347 " Size", " Offset", "objfile");
350 for (line = strtok (data, "\n"); line; line = strtok (NULL, "\n"))
352 ULONGEST addr, endaddr, offset, inode;
353 const char *permissions, *device, *filename;
354 size_t permissions_len, device_len;
356 read_mapping (line, &addr, &endaddr,
357 &permissions, &permissions_len,
358 &offset, &device, &device_len,
361 if (gdbarch_addr_bit (gdbarch) == 32)
363 printf_filtered ("\t%10s %10s %10s %10s %s\n",
364 paddress (gdbarch, addr),
365 paddress (gdbarch, endaddr),
366 hex_string (endaddr - addr),
368 *filename? filename : "");
372 printf_filtered (" %18s %18s %10s %10s %s\n",
373 paddress (gdbarch, addr),
374 paddress (gdbarch, endaddr),
375 hex_string (endaddr - addr),
377 *filename? filename : "");
381 do_cleanups (cleanup);
384 warning (_("unable to open /proc file '%s'"), filename);
388 xsnprintf (filename, sizeof filename, "/proc/%ld/status", pid);
389 data = target_fileio_read_stralloc (filename);
392 struct cleanup *cleanup = make_cleanup (xfree, data);
393 puts_filtered (data);
394 do_cleanups (cleanup);
397 warning (_("unable to open /proc file '%s'"), filename);
401 xsnprintf (filename, sizeof filename, "/proc/%ld/stat", pid);
402 data = target_fileio_read_stralloc (filename);
405 struct cleanup *cleanup = make_cleanup (xfree, data);
406 const char *p = data;
408 printf_filtered (_("Process: %s\n"),
409 pulongest (strtoulst (p, &p, 10)));
411 while (*p && isspace (*p))
415 const char *ep = strchr (p, ')');
418 printf_filtered ("Exec file: %.*s\n",
419 (int) (ep - p - 1), p + 1);
424 while (*p && isspace (*p))
427 printf_filtered (_("State: %c\n"), *p++);
430 printf_filtered (_("Parent process: %s\n"),
431 pulongest (strtoulst (p, &p, 10)));
433 printf_filtered (_("Process group: %s\n"),
434 pulongest (strtoulst (p, &p, 10)));
436 printf_filtered (_("Session id: %s\n"),
437 pulongest (strtoulst (p, &p, 10)));
439 printf_filtered (_("TTY: %s\n"),
440 pulongest (strtoulst (p, &p, 10)));
442 printf_filtered (_("TTY owner process group: %s\n"),
443 pulongest (strtoulst (p, &p, 10)));
446 printf_filtered (_("Flags: %s\n"),
447 hex_string (strtoulst (p, &p, 10)));
449 printf_filtered (_("Minor faults (no memory page): %s\n"),
450 pulongest (strtoulst (p, &p, 10)));
452 printf_filtered (_("Minor faults, children: %s\n"),
453 pulongest (strtoulst (p, &p, 10)));
455 printf_filtered (_("Major faults (memory page faults): %s\n"),
456 pulongest (strtoulst (p, &p, 10)));
458 printf_filtered (_("Major faults, children: %s\n"),
459 pulongest (strtoulst (p, &p, 10)));
461 printf_filtered (_("utime: %s\n"),
462 pulongest (strtoulst (p, &p, 10)));
464 printf_filtered (_("stime: %s\n"),
465 pulongest (strtoulst (p, &p, 10)));
467 printf_filtered (_("utime, children: %s\n"),
468 pulongest (strtoulst (p, &p, 10)));
470 printf_filtered (_("stime, children: %s\n"),
471 pulongest (strtoulst (p, &p, 10)));
473 printf_filtered (_("jiffies remaining in current "
475 pulongest (strtoulst (p, &p, 10)));
477 printf_filtered (_("'nice' value: %s\n"),
478 pulongest (strtoulst (p, &p, 10)));
480 printf_filtered (_("jiffies until next timeout: %s\n"),
481 pulongest (strtoulst (p, &p, 10)));
483 printf_filtered (_("jiffies until next SIGALRM: %s\n"),
484 pulongest (strtoulst (p, &p, 10)));
486 printf_filtered (_("start time (jiffies since "
487 "system boot): %s\n"),
488 pulongest (strtoulst (p, &p, 10)));
490 printf_filtered (_("Virtual memory size: %s\n"),
491 pulongest (strtoulst (p, &p, 10)));
493 printf_filtered (_("Resident set size: %s\n"),
494 pulongest (strtoulst (p, &p, 10)));
496 printf_filtered (_("rlim: %s\n"),
497 pulongest (strtoulst (p, &p, 10)));
499 printf_filtered (_("Start of text: %s\n"),
500 hex_string (strtoulst (p, &p, 10)));
502 printf_filtered (_("End of text: %s\n"),
503 hex_string (strtoulst (p, &p, 10)));
505 printf_filtered (_("Start of stack: %s\n"),
506 hex_string (strtoulst (p, &p, 10)));
507 #if 0 /* Don't know how architecture-dependent the rest is...
508 Anyway the signal bitmap info is available from "status". */
510 printf_filtered (_("Kernel stack pointer: %s\n"),
511 hex_string (strtoulst (p, &p, 10)));
513 printf_filtered (_("Kernel instr pointer: %s\n"),
514 hex_string (strtoulst (p, &p, 10)));
516 printf_filtered (_("Pending signals bitmap: %s\n"),
517 hex_string (strtoulst (p, &p, 10)));
519 printf_filtered (_("Blocked signals bitmap: %s\n"),
520 hex_string (strtoulst (p, &p, 10)));
522 printf_filtered (_("Ignored signals bitmap: %s\n"),
523 hex_string (strtoulst (p, &p, 10)));
525 printf_filtered (_("Catched signals bitmap: %s\n"),
526 hex_string (strtoulst (p, &p, 10)));
528 printf_filtered (_("wchan (system call): %s\n"),
529 hex_string (strtoulst (p, &p, 10)));
531 do_cleanups (cleanup);
534 warning (_("unable to open /proc file '%s'"), filename);
538 /* Implement "info proc mappings" for a corefile. */
541 linux_core_info_proc_mappings (struct gdbarch *gdbarch, char *args)
544 ULONGEST count, page_size;
545 unsigned char *descdata, *filenames, *descend, *contents;
547 unsigned int addr_size_bits, addr_size;
548 struct cleanup *cleanup;
549 struct gdbarch *core_gdbarch = gdbarch_from_bfd (core_bfd);
550 /* We assume this for reading 64-bit core files. */
551 gdb_static_assert (sizeof (ULONGEST) >= 8);
553 section = bfd_get_section_by_name (core_bfd, ".note.linuxcore.file");
556 warning (_("unable to find mappings in core file"));
560 addr_size_bits = gdbarch_addr_bit (core_gdbarch);
561 addr_size = addr_size_bits / 8;
562 note_size = bfd_get_section_size (section);
564 if (note_size < 2 * addr_size)
565 error (_("malformed core note - too short for header"));
567 contents = xmalloc (note_size);
568 cleanup = make_cleanup (xfree, contents);
569 if (!bfd_get_section_contents (core_bfd, section, contents, 0, note_size))
570 error (_("could not get core note contents"));
573 descend = descdata + note_size;
575 if (descdata[note_size - 1] != '\0')
576 error (_("malformed note - does not end with \\0"));
578 count = bfd_get (addr_size_bits, core_bfd, descdata);
579 descdata += addr_size;
581 page_size = bfd_get (addr_size_bits, core_bfd, descdata);
582 descdata += addr_size;
584 if (note_size < 2 * addr_size + count * 3 * addr_size)
585 error (_("malformed note - too short for supplied file count"));
587 printf_filtered (_("Mapped address spaces:\n\n"));
588 if (gdbarch_addr_bit (gdbarch) == 32)
590 printf_filtered ("\t%10s %10s %10s %10s %s\n",
593 " Size", " Offset", "objfile");
597 printf_filtered (" %18s %18s %10s %10s %s\n",
600 " Size", " Offset", "objfile");
603 filenames = descdata + count * 3 * addr_size;
606 ULONGEST start, end, file_ofs;
608 if (filenames == descend)
609 error (_("malformed note - filenames end too early"));
611 start = bfd_get (addr_size_bits, core_bfd, descdata);
612 descdata += addr_size;
613 end = bfd_get (addr_size_bits, core_bfd, descdata);
614 descdata += addr_size;
615 file_ofs = bfd_get (addr_size_bits, core_bfd, descdata);
616 descdata += addr_size;
618 file_ofs *= page_size;
620 if (gdbarch_addr_bit (gdbarch) == 32)
621 printf_filtered ("\t%10s %10s %10s %10s %s\n",
622 paddress (gdbarch, start),
623 paddress (gdbarch, end),
624 hex_string (end - start),
625 hex_string (file_ofs),
628 printf_filtered (" %18s %18s %10s %10s %s\n",
629 paddress (gdbarch, start),
630 paddress (gdbarch, end),
631 hex_string (end - start),
632 hex_string (file_ofs),
635 filenames += 1 + strlen ((char *) filenames);
638 do_cleanups (cleanup);
641 /* Implement "info proc" for a corefile. */
644 linux_core_info_proc (struct gdbarch *gdbarch, char *args,
645 enum info_proc_what what)
647 int exe_f = (what == IP_MINIMAL || what == IP_EXE || what == IP_ALL);
648 int mappings_f = (what == IP_MAPPINGS || what == IP_ALL);
654 exe = bfd_core_file_failing_command (core_bfd);
656 printf_filtered ("exe = '%s'\n", exe);
658 warning (_("unable to find command name in core file"));
662 linux_core_info_proc_mappings (gdbarch, args);
664 if (!exe_f && !mappings_f)
665 error (_("unable to handle request"));
668 typedef int linux_find_memory_region_ftype (ULONGEST vaddr, ULONGEST size,
669 ULONGEST offset, ULONGEST inode,
671 int exec, int modified,
672 const char *filename,
675 /* List memory regions in the inferior for a corefile. */
678 linux_find_memory_regions_full (struct gdbarch *gdbarch,
679 linux_find_memory_region_ftype *func,
685 /* We need to know the real target PID to access /proc. */
686 if (current_inferior ()->fake_pid_p)
689 xsnprintf (filename, sizeof filename,
690 "/proc/%d/smaps", current_inferior ()->pid);
691 data = target_fileio_read_stralloc (filename);
694 /* Older Linux kernels did not support /proc/PID/smaps. */
695 xsnprintf (filename, sizeof filename,
696 "/proc/%d/maps", current_inferior ()->pid);
697 data = target_fileio_read_stralloc (filename);
701 struct cleanup *cleanup = make_cleanup (xfree, data);
704 line = strtok (data, "\n");
707 ULONGEST addr, endaddr, offset, inode;
708 const char *permissions, *device, *filename;
709 size_t permissions_len, device_len;
710 int read, write, exec;
711 int modified = 0, has_anonymous = 0;
713 read_mapping (line, &addr, &endaddr, &permissions, &permissions_len,
714 &offset, &device, &device_len, &inode, &filename);
716 /* Decode permissions. */
717 read = (memchr (permissions, 'r', permissions_len) != 0);
718 write = (memchr (permissions, 'w', permissions_len) != 0);
719 exec = (memchr (permissions, 'x', permissions_len) != 0);
721 /* Try to detect if region was modified by parsing smaps counters. */
722 for (line = strtok (NULL, "\n");
723 line && line[0] >= 'A' && line[0] <= 'Z';
724 line = strtok (NULL, "\n"))
726 char keyword[64 + 1];
727 unsigned long number;
729 if (sscanf (line, "%64s%lu kB\n", keyword, &number) != 2)
731 warning (_("Error parsing {s,}maps file '%s'"), filename);
734 if (strcmp (keyword, "Anonymous:") == 0)
736 if (number != 0 && (strcmp (keyword, "Shared_Dirty:") == 0
737 || strcmp (keyword, "Private_Dirty:") == 0
738 || strcmp (keyword, "Swap:") == 0
739 || strcmp (keyword, "Anonymous:") == 0))
743 /* Older Linux kernels did not support the "Anonymous:" counter.
744 If it is missing, we can't be sure - dump all the pages. */
748 /* Invoke the callback function to create the corefile segment. */
749 func (addr, endaddr - addr, offset, inode,
750 read, write, exec, modified, filename, obfd);
753 do_cleanups (cleanup);
760 /* A structure for passing information through
761 linux_find_memory_regions_full. */
763 struct linux_find_memory_regions_data
765 /* The original callback. */
767 find_memory_region_ftype func;
769 /* The original datum. */
774 /* A callback for linux_find_memory_regions that converts between the
775 "full"-style callback and find_memory_region_ftype. */
778 linux_find_memory_regions_thunk (ULONGEST vaddr, ULONGEST size,
779 ULONGEST offset, ULONGEST inode,
780 int read, int write, int exec, int modified,
781 const char *filename, void *arg)
783 struct linux_find_memory_regions_data *data = arg;
785 return data->func (vaddr, size, read, write, exec, modified, data->obfd);
788 /* A variant of linux_find_memory_regions_full that is suitable as the
789 gdbarch find_memory_regions method. */
792 linux_find_memory_regions (struct gdbarch *gdbarch,
793 find_memory_region_ftype func, void *obfd)
795 struct linux_find_memory_regions_data data;
800 return linux_find_memory_regions_full (gdbarch,
801 linux_find_memory_regions_thunk,
805 /* Determine which signal stopped execution. */
808 find_signalled_thread (struct thread_info *info, void *data)
810 if (info->suspend.stop_signal != GDB_SIGNAL_0
811 && ptid_get_pid (info->ptid) == ptid_get_pid (inferior_ptid))
817 static enum gdb_signal
818 find_stop_signal (void)
820 struct thread_info *info =
821 iterate_over_threads (find_signalled_thread, NULL);
824 return info->suspend.stop_signal;
829 /* Generate corefile notes for SPU contexts. */
832 linux_spu_make_corefile_notes (bfd *obfd, char *note_data, int *note_size)
834 static const char *spu_files[] =
856 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
860 /* Determine list of SPU ids. */
861 size = target_read_alloc (¤t_target, TARGET_OBJECT_SPU,
864 /* Generate corefile notes for each SPU file. */
865 for (i = 0; i < size; i += 4)
867 int fd = extract_unsigned_integer (spu_ids + i, 4, byte_order);
869 for (j = 0; j < sizeof (spu_files) / sizeof (spu_files[0]); j++)
871 char annex[32], note_name[32];
875 xsnprintf (annex, sizeof annex, "%d/%s", fd, spu_files[j]);
876 spu_len = target_read_alloc (¤t_target, TARGET_OBJECT_SPU,
880 xsnprintf (note_name, sizeof note_name, "SPU/%s", annex);
881 note_data = elfcore_write_note (obfd, note_data, note_size,
901 /* This is used to pass information from
902 linux_make_mappings_corefile_notes through
903 linux_find_memory_regions_full. */
905 struct linux_make_mappings_data
907 /* Number of files mapped. */
910 /* The obstack for the main part of the data. */
911 struct obstack *data_obstack;
913 /* The filename obstack. */
914 struct obstack *filename_obstack;
916 /* The architecture's "long" type. */
917 struct type *long_type;
920 static linux_find_memory_region_ftype linux_make_mappings_callback;
922 /* A callback for linux_find_memory_regions_full that updates the
923 mappings data for linux_make_mappings_corefile_notes. */
926 linux_make_mappings_callback (ULONGEST vaddr, ULONGEST size,
927 ULONGEST offset, ULONGEST inode,
928 int read, int write, int exec, int modified,
929 const char *filename, void *data)
931 struct linux_make_mappings_data *map_data = data;
932 gdb_byte buf[sizeof (ULONGEST)];
934 if (*filename == '\0' || inode == 0)
937 ++map_data->file_count;
939 pack_long (buf, map_data->long_type, vaddr);
940 obstack_grow (map_data->data_obstack, buf, TYPE_LENGTH (map_data->long_type));
941 pack_long (buf, map_data->long_type, vaddr + size);
942 obstack_grow (map_data->data_obstack, buf, TYPE_LENGTH (map_data->long_type));
943 pack_long (buf, map_data->long_type, offset);
944 obstack_grow (map_data->data_obstack, buf, TYPE_LENGTH (map_data->long_type));
946 obstack_grow_str0 (map_data->filename_obstack, filename);
951 /* Write the file mapping data to the core file, if possible. OBFD is
952 the output BFD. NOTE_DATA is the current note data, and NOTE_SIZE
953 is a pointer to the note size. Returns the new NOTE_DATA and
954 updates NOTE_SIZE. */
957 linux_make_mappings_corefile_notes (struct gdbarch *gdbarch, bfd *obfd,
958 char *note_data, int *note_size)
960 struct cleanup *cleanup;
961 struct obstack data_obstack, filename_obstack;
962 struct linux_make_mappings_data mapping_data;
963 struct type *long_type
964 = arch_integer_type (gdbarch, gdbarch_long_bit (gdbarch), 0, "long");
965 gdb_byte buf[sizeof (ULONGEST)];
967 obstack_init (&data_obstack);
968 cleanup = make_cleanup_obstack_free (&data_obstack);
969 obstack_init (&filename_obstack);
970 make_cleanup_obstack_free (&filename_obstack);
972 mapping_data.file_count = 0;
973 mapping_data.data_obstack = &data_obstack;
974 mapping_data.filename_obstack = &filename_obstack;
975 mapping_data.long_type = long_type;
977 /* Reserve space for the count. */
978 obstack_blank (&data_obstack, TYPE_LENGTH (long_type));
979 /* We always write the page size as 1 since we have no good way to
980 determine the correct value. */
981 pack_long (buf, long_type, 1);
982 obstack_grow (&data_obstack, buf, TYPE_LENGTH (long_type));
984 linux_find_memory_regions_full (gdbarch, linux_make_mappings_callback,
987 if (mapping_data.file_count != 0)
989 /* Write the count to the obstack. */
990 pack_long (obstack_base (&data_obstack), long_type,
991 mapping_data.file_count);
993 /* Copy the filenames to the data obstack. */
994 obstack_grow (&data_obstack, obstack_base (&filename_obstack),
995 obstack_object_size (&filename_obstack));
997 note_data = elfcore_write_note (obfd, note_data, note_size,
999 obstack_base (&data_obstack),
1000 obstack_object_size (&data_obstack));
1003 do_cleanups (cleanup);
1007 /* Records the thread's register state for the corefile note
1011 linux_collect_thread_registers (const struct regcache *regcache,
1012 ptid_t ptid, bfd *obfd,
1013 char *note_data, int *note_size,
1014 enum gdb_signal stop_signal)
1016 struct gdbarch *gdbarch = get_regcache_arch (regcache);
1017 struct core_regset_section *sect_list;
1020 sect_list = gdbarch_core_regset_sections (gdbarch);
1021 gdb_assert (sect_list);
1023 /* For remote targets the LWP may not be available, so use the TID. */
1024 lwp = ptid_get_lwp (ptid);
1026 lwp = ptid_get_tid (ptid);
1028 while (sect_list->sect_name != NULL)
1030 const struct regset *regset;
1033 regset = gdbarch_regset_from_core_section (gdbarch,
1034 sect_list->sect_name,
1036 gdb_assert (regset && regset->collect_regset);
1038 buf = xmalloc (sect_list->size);
1039 regset->collect_regset (regset, regcache, -1, buf, sect_list->size);
1041 /* PRSTATUS still needs to be treated specially. */
1042 if (strcmp (sect_list->sect_name, ".reg") == 0)
1043 note_data = (char *) elfcore_write_prstatus
1044 (obfd, note_data, note_size, lwp,
1045 gdb_signal_to_host (stop_signal), buf);
1047 note_data = (char *) elfcore_write_register_note
1048 (obfd, note_data, note_size,
1049 sect_list->sect_name, buf, sect_list->size);
1060 /* Fetch the siginfo data for the current thread, if it exists. If
1061 there is no data, or we could not read it, return NULL. Otherwise,
1062 return a newly malloc'd buffer holding the data and fill in *SIZE
1063 with the size of the data. The caller is responsible for freeing
1067 linux_get_siginfo_data (struct gdbarch *gdbarch, LONGEST *size)
1069 struct type *siginfo_type;
1072 struct cleanup *cleanups;
1074 if (!gdbarch_get_siginfo_type_p (gdbarch))
1077 siginfo_type = gdbarch_get_siginfo_type (gdbarch);
1079 buf = xmalloc (TYPE_LENGTH (siginfo_type));
1080 cleanups = make_cleanup (xfree, buf);
1082 bytes_read = target_read (¤t_target, TARGET_OBJECT_SIGNAL_INFO, NULL,
1083 buf, 0, TYPE_LENGTH (siginfo_type));
1084 if (bytes_read == TYPE_LENGTH (siginfo_type))
1086 discard_cleanups (cleanups);
1091 do_cleanups (cleanups);
1098 struct linux_corefile_thread_data
1100 struct gdbarch *gdbarch;
1106 enum gdb_signal stop_signal;
1107 linux_collect_thread_registers_ftype collect;
1110 /* Called by gdbthread.c once per thread. Records the thread's
1111 register state for the corefile note section. */
1114 linux_corefile_thread_callback (struct thread_info *info, void *data)
1116 struct linux_corefile_thread_data *args = data;
1118 if (ptid_get_pid (info->ptid) == args->pid)
1120 struct cleanup *old_chain;
1121 struct regcache *regcache;
1122 gdb_byte *siginfo_data;
1123 LONGEST siginfo_size;
1125 regcache = get_thread_arch_regcache (info->ptid, args->gdbarch);
1127 old_chain = save_inferior_ptid ();
1128 inferior_ptid = info->ptid;
1129 target_fetch_registers (regcache, -1);
1130 siginfo_data = linux_get_siginfo_data (args->gdbarch, &siginfo_size);
1131 do_cleanups (old_chain);
1133 old_chain = make_cleanup (xfree, siginfo_data);
1135 args->note_data = args->collect (regcache, info->ptid, args->obfd,
1136 args->note_data, args->note_size,
1140 if (siginfo_data != NULL)
1142 args->note_data = elfcore_write_note (args->obfd,
1146 siginfo_data, siginfo_size);
1150 do_cleanups (old_chain);
1153 return !args->note_data;
1156 /* Fills the "to_make_corefile_note" target vector. Builds the note
1157 section for a corefile, and returns it in a malloc buffer. */
1160 linux_make_corefile_notes (struct gdbarch *gdbarch, bfd *obfd, int *note_size,
1161 linux_collect_thread_registers_ftype collect)
1163 struct linux_corefile_thread_data thread_args;
1164 char *note_data = NULL;
1168 /* Process information. */
1169 if (get_exec_file (0))
1171 const char *fname = lbasename (get_exec_file (0));
1172 char *psargs = xstrdup (fname);
1174 if (get_inferior_args ())
1175 psargs = reconcat (psargs, psargs, " ", get_inferior_args (),
1178 note_data = elfcore_write_prpsinfo (obfd, note_data, note_size,
1186 /* Thread register information. */
1187 thread_args.gdbarch = gdbarch;
1188 thread_args.pid = ptid_get_pid (inferior_ptid);
1189 thread_args.obfd = obfd;
1190 thread_args.note_data = note_data;
1191 thread_args.note_size = note_size;
1192 thread_args.num_notes = 0;
1193 thread_args.stop_signal = find_stop_signal ();
1194 thread_args.collect = collect;
1195 iterate_over_threads (linux_corefile_thread_callback, &thread_args);
1196 note_data = thread_args.note_data;
1200 /* Auxillary vector. */
1201 auxv_len = target_read_alloc (¤t_target, TARGET_OBJECT_AUXV,
1205 note_data = elfcore_write_note (obfd, note_data, note_size,
1206 "CORE", NT_AUXV, auxv, auxv_len);
1213 /* SPU information. */
1214 note_data = linux_spu_make_corefile_notes (obfd, note_data, note_size);
1218 /* File mappings. */
1219 note_data = linux_make_mappings_corefile_notes (gdbarch, obfd,
1220 note_data, note_size);
1222 make_cleanup (xfree, note_data);
1227 linux_make_corefile_notes_1 (struct gdbarch *gdbarch, bfd *obfd, int *note_size)
1229 /* FIXME: uweigand/2011-10-06: Once all GNU/Linux architectures have been
1230 converted to gdbarch_core_regset_sections, we no longer need to fall back
1231 to the target method at this point. */
1233 if (!gdbarch_core_regset_sections (gdbarch))
1234 return target_make_corefile_notes (obfd, note_size);
1236 return linux_make_corefile_notes (gdbarch, obfd, note_size,
1237 linux_collect_thread_registers);
1240 /* To be called from the various GDB_OSABI_LINUX handlers for the
1241 various GNU/Linux architectures and machine types. */
1244 linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
1246 set_gdbarch_core_pid_to_str (gdbarch, linux_core_pid_to_str);
1247 set_gdbarch_info_proc (gdbarch, linux_info_proc);
1248 set_gdbarch_core_info_proc (gdbarch, linux_core_info_proc);
1249 set_gdbarch_find_memory_regions (gdbarch, linux_find_memory_regions);
1250 set_gdbarch_make_corefile_notes (gdbarch, linux_make_corefile_notes_1);
1251 set_gdbarch_has_shared_address_space (gdbarch,
1252 linux_has_shared_address_space);
1255 /* Provide a prototype to silence -Wmissing-prototypes. */
1256 extern initialize_file_ftype _initialize_linux_tdep;
1259 _initialize_linux_tdep (void)
1261 linux_gdbarch_data_handle =
1262 gdbarch_data_register_post_init (init_linux_gdbarch_data);