#include "linux-tdep.h"
#include "auxv.h"
#include "target.h"
+#include "gdbthread.h"
+#include "gdbcore.h"
+#include "regcache.h"
+#include "regset.h"
#include "elf/common.h"
+#include "elf-bfd.h" /* for elfcore_write_* */
#include "inferior.h"
#include "cli/cli-utils.h"
return siginfo_type;
}
-int
-linux_has_shared_address_space (void)
+static int
+linux_has_shared_address_space (struct gdbarch *gdbarch)
{
/* Determine whether we are running on uClinux or normal Linux
kernel. */
{
struct cleanup *cleanup = make_cleanup (xfree, data);
const char *p = data;
- const char *ep;
- ULONGEST val;
printf_filtered (_("Process: %s\n"),
pulongest (strtoulst (p, &p, 10)));
while (*p && isspace (*p))
p++;
- if (*p == '(' && (ep = strchr (p, ')')) != NULL)
+ if (*p == '(')
{
- printf_filtered ("Exec file: %.*s\n", (int) (ep - p - 1), p + 1);
- p = ep + 1;
+ const char *ep = strchr (p, ')');
+ if (ep != NULL)
+ {
+ printf_filtered ("Exec file: %.*s\n",
+ (int) (ep - p - 1), p + 1);
+ p = ep + 1;
+ }
}
while (*p && isspace (*p))
}
}
+/* List memory regions in the inferior for a corefile. */
+
+static int
+linux_find_memory_regions (struct gdbarch *gdbarch,
+ find_memory_region_ftype func, void *obfd)
+{
+ char filename[100];
+ gdb_byte *data;
+
+ /* We need to know the real target PID to access /proc. */
+ if (current_inferior ()->fake_pid_p)
+ return 1;
+
+ xsnprintf (filename, sizeof filename,
+ "/proc/%d/smaps", current_inferior ()->pid);
+ data = target_fileio_read_stralloc (filename);
+ if (data == NULL)
+ {
+ /* Older Linux kernels did not support /proc/PID/smaps. */
+ xsnprintf (filename, sizeof filename,
+ "/proc/%d/maps", current_inferior ()->pid);
+ data = target_fileio_read_stralloc (filename);
+ }
+ if (data)
+ {
+ struct cleanup *cleanup = make_cleanup (xfree, data);
+ char *line;
+
+ line = strtok (data, "\n");
+ while (line)
+ {
+ ULONGEST addr, endaddr, offset, inode;
+ const char *permissions, *device, *filename;
+ size_t permissions_len, device_len;
+ int read, write, exec;
+ int modified = 0, has_anonymous = 0;
+
+ read_mapping (line, &addr, &endaddr, &permissions, &permissions_len,
+ &offset, &device, &device_len, &inode, &filename);
+
+ /* Decode permissions. */
+ read = (memchr (permissions, 'r', permissions_len) != 0);
+ write = (memchr (permissions, 'w', permissions_len) != 0);
+ exec = (memchr (permissions, 'x', permissions_len) != 0);
+
+ /* Try to detect if region was modified by parsing smaps counters. */
+ for (line = strtok (NULL, "\n");
+ line && line[0] >= 'A' && line[0] <= 'Z';
+ line = strtok (NULL, "\n"))
+ {
+ char keyword[64 + 1];
+ unsigned long number;
+
+ if (sscanf (line, "%64s%lu kB\n", keyword, &number) != 2)
+ {
+ warning (_("Error parsing {s,}maps file '%s'"), filename);
+ break;
+ }
+ if (strcmp (keyword, "Anonymous:") == 0)
+ has_anonymous = 1;
+ if (number != 0 && (strcmp (keyword, "Shared_Dirty:") == 0
+ || strcmp (keyword, "Private_Dirty:") == 0
+ || strcmp (keyword, "Swap:") == 0
+ || strcmp (keyword, "Anonymous:") == 0))
+ modified = 1;
+ }
+
+ /* Older Linux kernels did not support the "Anonymous:" counter.
+ If it is missing, we can't be sure - dump all the pages. */
+ if (!has_anonymous)
+ modified = 1;
+
+ /* Invoke the callback function to create the corefile segment. */
+ func (addr, endaddr - addr, read, write, exec, modified, obfd);
+ }
+
+ do_cleanups (cleanup);
+ return 0;
+ }
+
+ return 1;
+}
+
+/* Determine which signal stopped execution. */
+
+static int
+find_signalled_thread (struct thread_info *info, void *data)
+{
+ if (info->suspend.stop_signal != GDB_SIGNAL_0
+ && ptid_get_pid (info->ptid) == ptid_get_pid (inferior_ptid))
+ return 1;
+
+ return 0;
+}
+
+static enum gdb_signal
+find_stop_signal (void)
+{
+ struct thread_info *info =
+ iterate_over_threads (find_signalled_thread, NULL);
+
+ if (info)
+ return info->suspend.stop_signal;
+ else
+ return GDB_SIGNAL_0;
+}
+
+/* Generate corefile notes for SPU contexts. */
+
+static char *
+linux_spu_make_corefile_notes (bfd *obfd, char *note_data, int *note_size)
+{
+ static const char *spu_files[] =
+ {
+ "object-id",
+ "mem",
+ "regs",
+ "fpcr",
+ "lslr",
+ "decr",
+ "decr_status",
+ "signal1",
+ "signal1_type",
+ "signal2",
+ "signal2_type",
+ "event_mask",
+ "event_status",
+ "mbox_info",
+ "ibox_info",
+ "wbox_info",
+ "dma_info",
+ "proxydma_info",
+ };
+
+ enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch);
+ gdb_byte *spu_ids;
+ LONGEST i, j, size;
+
+ /* Determine list of SPU ids. */
+ size = target_read_alloc (¤t_target, TARGET_OBJECT_SPU,
+ NULL, &spu_ids);
+
+ /* Generate corefile notes for each SPU file. */
+ for (i = 0; i < size; i += 4)
+ {
+ int fd = extract_unsigned_integer (spu_ids + i, 4, byte_order);
+
+ for (j = 0; j < sizeof (spu_files) / sizeof (spu_files[0]); j++)
+ {
+ char annex[32], note_name[32];
+ gdb_byte *spu_data;
+ LONGEST spu_len;
+
+ xsnprintf (annex, sizeof annex, "%d/%s", fd, spu_files[j]);
+ spu_len = target_read_alloc (¤t_target, TARGET_OBJECT_SPU,
+ annex, &spu_data);
+ if (spu_len > 0)
+ {
+ xsnprintf (note_name, sizeof note_name, "SPU/%s", annex);
+ note_data = elfcore_write_note (obfd, note_data, note_size,
+ note_name, NT_SPU,
+ spu_data, spu_len);
+ xfree (spu_data);
+
+ if (!note_data)
+ {
+ xfree (spu_ids);
+ return NULL;
+ }
+ }
+ }
+ }
+
+ if (size > 0)
+ xfree (spu_ids);
+
+ return note_data;
+}
+
+/* Records the thread's register state for the corefile note
+ section. */
+
+static char *
+linux_collect_thread_registers (const struct regcache *regcache,
+ ptid_t ptid, bfd *obfd,
+ char *note_data, int *note_size,
+ enum gdb_signal stop_signal)
+{
+ struct gdbarch *gdbarch = get_regcache_arch (regcache);
+ struct core_regset_section *sect_list;
+ unsigned long lwp;
+
+ sect_list = gdbarch_core_regset_sections (gdbarch);
+ gdb_assert (sect_list);
+
+ /* For remote targets the LWP may not be available, so use the TID. */
+ lwp = ptid_get_lwp (ptid);
+ if (!lwp)
+ lwp = ptid_get_tid (ptid);
+
+ while (sect_list->sect_name != NULL)
+ {
+ const struct regset *regset;
+ char *buf;
+
+ regset = gdbarch_regset_from_core_section (gdbarch,
+ sect_list->sect_name,
+ sect_list->size);
+ gdb_assert (regset && regset->collect_regset);
+
+ buf = xmalloc (sect_list->size);
+ regset->collect_regset (regset, regcache, -1, buf, sect_list->size);
+
+ /* PRSTATUS still needs to be treated specially. */
+ if (strcmp (sect_list->sect_name, ".reg") == 0)
+ note_data = (char *) elfcore_write_prstatus
+ (obfd, note_data, note_size, lwp,
+ gdb_signal_to_host (stop_signal), buf);
+ else
+ note_data = (char *) elfcore_write_register_note
+ (obfd, note_data, note_size,
+ sect_list->sect_name, buf, sect_list->size);
+ xfree (buf);
+ sect_list++;
+
+ if (!note_data)
+ return NULL;
+ }
+
+ return note_data;
+}
+
+/* Fetch the siginfo data for the current thread, if it exists. If
+ there is no data, or we could not read it, return NULL. Otherwise,
+ return a newly malloc'd buffer holding the data and fill in *SIZE
+ with the size of the data. The caller is responsible for freeing
+ the data. */
+
+static gdb_byte *
+linux_get_siginfo_data (struct gdbarch *gdbarch, LONGEST *size)
+{
+ struct type *siginfo_type;
+ gdb_byte *buf;
+ LONGEST bytes_read;
+ struct cleanup *cleanups;
+
+ if (!gdbarch_get_siginfo_type_p (gdbarch))
+ return NULL;
+
+ siginfo_type = gdbarch_get_siginfo_type (gdbarch);
+
+ buf = xmalloc (TYPE_LENGTH (siginfo_type));
+ cleanups = make_cleanup (xfree, buf);
+
+ bytes_read = target_read (¤t_target, TARGET_OBJECT_SIGNAL_INFO, NULL,
+ buf, 0, TYPE_LENGTH (siginfo_type));
+ if (bytes_read == TYPE_LENGTH (siginfo_type))
+ {
+ discard_cleanups (cleanups);
+ *size = bytes_read;
+ }
+ else
+ {
+ do_cleanups (cleanups);
+ buf = NULL;
+ }
+
+ return buf;
+}
+
+struct linux_corefile_thread_data
+{
+ struct gdbarch *gdbarch;
+ int pid;
+ bfd *obfd;
+ char *note_data;
+ int *note_size;
+ int num_notes;
+ enum gdb_signal stop_signal;
+ linux_collect_thread_registers_ftype collect;
+};
+
+/* Called by gdbthread.c once per thread. Records the thread's
+ register state for the corefile note section. */
+
+static int
+linux_corefile_thread_callback (struct thread_info *info, void *data)
+{
+ struct linux_corefile_thread_data *args = data;
+
+ if (ptid_get_pid (info->ptid) == args->pid)
+ {
+ struct cleanup *old_chain;
+ struct regcache *regcache;
+ gdb_byte *siginfo_data;
+ LONGEST siginfo_size;
+
+ regcache = get_thread_arch_regcache (info->ptid, args->gdbarch);
+
+ old_chain = save_inferior_ptid ();
+ inferior_ptid = info->ptid;
+ target_fetch_registers (regcache, -1);
+ siginfo_data = linux_get_siginfo_data (args->gdbarch, &siginfo_size);
+ do_cleanups (old_chain);
+
+ old_chain = make_cleanup (xfree, siginfo_data);
+
+ args->note_data = args->collect (regcache, info->ptid, args->obfd,
+ args->note_data, args->note_size,
+ args->stop_signal);
+ args->num_notes++;
+
+ if (siginfo_data != NULL)
+ {
+ args->note_data = elfcore_write_note (args->obfd,
+ args->note_data,
+ args->note_size,
+ "CORE", NT_SIGINFO,
+ siginfo_data, siginfo_size);
+ args->num_notes++;
+ }
+
+ do_cleanups (old_chain);
+ }
+
+ return !args->note_data;
+}
+
+/* Fills the "to_make_corefile_note" target vector. Builds the note
+ section for a corefile, and returns it in a malloc buffer. */
+
+char *
+linux_make_corefile_notes (struct gdbarch *gdbarch, bfd *obfd, int *note_size,
+ linux_collect_thread_registers_ftype collect)
+{
+ struct linux_corefile_thread_data thread_args;
+ char *note_data = NULL;
+ gdb_byte *auxv;
+ int auxv_len;
+
+ /* Process information. */
+ if (get_exec_file (0))
+ {
+ const char *fname = lbasename (get_exec_file (0));
+ char *psargs = xstrdup (fname);
+
+ if (get_inferior_args ())
+ psargs = reconcat (psargs, psargs, " ", get_inferior_args (),
+ (char *) NULL);
+
+ note_data = elfcore_write_prpsinfo (obfd, note_data, note_size,
+ fname, psargs);
+ xfree (psargs);
+
+ if (!note_data)
+ return NULL;
+ }
+
+ /* Thread register information. */
+ thread_args.gdbarch = gdbarch;
+ thread_args.pid = ptid_get_pid (inferior_ptid);
+ thread_args.obfd = obfd;
+ thread_args.note_data = note_data;
+ thread_args.note_size = note_size;
+ thread_args.num_notes = 0;
+ thread_args.stop_signal = find_stop_signal ();
+ thread_args.collect = collect;
+ iterate_over_threads (linux_corefile_thread_callback, &thread_args);
+ note_data = thread_args.note_data;
+ if (!note_data)
+ return NULL;
+
+ /* Auxillary vector. */
+ auxv_len = target_read_alloc (¤t_target, TARGET_OBJECT_AUXV,
+ NULL, &auxv);
+ if (auxv_len > 0)
+ {
+ note_data = elfcore_write_note (obfd, note_data, note_size,
+ "CORE", NT_AUXV, auxv, auxv_len);
+ xfree (auxv);
+
+ if (!note_data)
+ return NULL;
+ }
+
+ /* SPU information. */
+ note_data = linux_spu_make_corefile_notes (obfd, note_data, note_size);
+ if (!note_data)
+ return NULL;
+
+ make_cleanup (xfree, note_data);
+ return note_data;
+}
+
+static char *
+linux_make_corefile_notes_1 (struct gdbarch *gdbarch, bfd *obfd, int *note_size)
+{
+ /* FIXME: uweigand/2011-10-06: Once all GNU/Linux architectures have been
+ converted to gdbarch_core_regset_sections, we no longer need to fall back
+ to the target method at this point. */
+
+ if (!gdbarch_core_regset_sections (gdbarch))
+ return target_make_corefile_notes (obfd, note_size);
+ else
+ return linux_make_corefile_notes (gdbarch, obfd, note_size,
+ linux_collect_thread_registers);
+}
+
/* To be called from the various GDB_OSABI_LINUX handlers for the
various GNU/Linux architectures and machine types. */
{
set_gdbarch_core_pid_to_str (gdbarch, linux_core_pid_to_str);
set_gdbarch_info_proc (gdbarch, linux_info_proc);
+ set_gdbarch_find_memory_regions (gdbarch, linux_find_memory_regions);
+ set_gdbarch_make_corefile_notes (gdbarch, linux_make_corefile_notes_1);
+ set_gdbarch_has_shared_address_space (gdbarch,
+ linux_has_shared_address_space);
}
+/* Provide a prototype to silence -Wmissing-prototypes. */
+extern initialize_file_ftype _initialize_linux_tdep;
+
void
_initialize_linux_tdep (void)
{