- return linux_xfer_siginfo (ops, object, annex, readbuf, writebuf,
- offset, len);
-
- /* The target is connected but no live inferior is selected. Pass
- this request down to a lower stratum (e.g., the executable
- file). */
- if (object == TARGET_OBJECT_MEMORY && ptid_equal (inferior_ptid, null_ptid))
- return 0;
-
- old_chain = save_inferior_ptid ();
-
- if (is_lwp (inferior_ptid))
- inferior_ptid = pid_to_ptid (GET_LWP (inferior_ptid));
-
- xfer = linux_ops->to_xfer_partial (ops, object, annex, readbuf, writebuf,
- offset, len);
-
- do_cleanups (old_chain);
- return xfer;
-}
-
-static int
-linux_thread_alive (ptid_t ptid)
-{
- int err, tmp_errno;
-
- gdb_assert (is_lwp (ptid));
-
- /* Send signal 0 instead of anything ptrace, because ptracing a
- running thread errors out claiming that the thread doesn't
- exist. */
- err = kill_lwp (GET_LWP (ptid), 0);
- tmp_errno = errno;
- if (debug_linux_nat)
- fprintf_unfiltered (gdb_stdlog,
- "LLTA: KILL(SIG0) %s (%s)\n",
- target_pid_to_str (ptid),
- err ? safe_strerror (tmp_errno) : "OK");
-
- if (err != 0)
- return 0;
-
- return 1;
-}
-
-static int
-linux_nat_thread_alive (struct target_ops *ops, ptid_t ptid)
-{
- return linux_thread_alive (ptid);
-}
-
-static char *
-linux_nat_pid_to_str (struct target_ops *ops, ptid_t ptid)
-{
- static char buf[64];
-
- if (is_lwp (ptid)
- && (GET_PID (ptid) != GET_LWP (ptid)
- || num_lwps (GET_PID (ptid)) > 1))
- {
- snprintf (buf, sizeof (buf), "LWP %ld", GET_LWP (ptid));
- return buf;
- }
-
- return normal_pid_to_str (ptid);
-}
-
-static char *
-linux_nat_thread_name (struct thread_info *thr)
-{
- int pid = ptid_get_pid (thr->ptid);
- long lwp = ptid_get_lwp (thr->ptid);
-#define FORMAT "/proc/%d/task/%ld/comm"
- char buf[sizeof (FORMAT) + 30];
- FILE *comm_file;
- char *result = NULL;
-
- snprintf (buf, sizeof (buf), FORMAT, pid, lwp);
- comm_file = fopen (buf, "r");
- if (comm_file)
- {
- /* Not exported by the kernel, so we define it here. */
-#define COMM_LEN 16
- static char line[COMM_LEN + 1];
-
- if (fgets (line, sizeof (line), comm_file))
- {
- char *nl = strchr (line, '\n');
-
- if (nl)
- *nl = '\0';
- if (*line != '\0')
- result = line;
- }
-
- fclose (comm_file);
- }
-
-#undef COMM_LEN
-#undef FORMAT
-
- return result;
-}
-
-/* Accepts an integer PID; Returns a string representing a file that
- can be opened to get the symbols for the child process. */
-
-static char *
-linux_child_pid_to_exec_file (int pid)
-{
- char *name1, *name2;
-
- name1 = xmalloc (MAXPATHLEN);
- name2 = xmalloc (MAXPATHLEN);
- make_cleanup (xfree, name1);
- make_cleanup (xfree, name2);
- memset (name2, 0, MAXPATHLEN);
-
- sprintf (name1, "/proc/%d/exe", pid);
- if (readlink (name1, name2, MAXPATHLEN) > 0)
- return name2;
- else
- return name1;
-}
-
-/* Service function for corefiles and info proc. */
-
-static int
-read_mapping (FILE *mapfile,
- long long *addr,
- long long *endaddr,
- char *permissions,
- long long *offset,
- char *device, long long *inode, char *filename)
-{
- int ret = fscanf (mapfile, "%llx-%llx %s %llx %s %llx",
- addr, endaddr, permissions, offset, device, inode);
-
- filename[0] = '\0';
- if (ret > 0 && ret != EOF)
- {
- /* Eat everything up to EOL for the filename. This will prevent
- weird filenames (such as one with embedded whitespace) from
- confusing this code. It also makes this code more robust in
- respect to annotations the kernel may add after the filename.
-
- Note the filename is used for informational purposes
- only. */
- ret += fscanf (mapfile, "%[^\n]\n", filename);
- }
-
- return (ret != 0 && ret != EOF);
-}
-
-/* Fills the "to_find_memory_regions" target vector. Lists the memory
- regions in the inferior for a corefile. */
-
-static int
-linux_nat_find_memory_regions (find_memory_region_ftype func, void *obfd)
-{
- int pid = PIDGET (inferior_ptid);
- char mapsfilename[MAXPATHLEN];
- FILE *mapsfile;
- long long addr, endaddr, size, offset, inode;
- char permissions[8], device[8], filename[MAXPATHLEN];
- int read, write, exec;
- struct cleanup *cleanup;
-
- /* Compose the filename for the /proc memory map, and open it. */
- sprintf (mapsfilename, "/proc/%d/maps", pid);
- if ((mapsfile = fopen (mapsfilename, "r")) == NULL)
- error (_("Could not open %s."), mapsfilename);
- cleanup = make_cleanup_fclose (mapsfile);
-
- if (info_verbose)
- fprintf_filtered (gdb_stdout,
- "Reading memory regions from %s\n", mapsfilename);
-
- /* Now iterate until end-of-file. */
- while (read_mapping (mapsfile, &addr, &endaddr, &permissions[0],
- &offset, &device[0], &inode, &filename[0]))
- {
- size = endaddr - addr;
-
- /* Get the segment's permissions. */
- read = (strchr (permissions, 'r') != 0);
- write = (strchr (permissions, 'w') != 0);
- exec = (strchr (permissions, 'x') != 0);
-
- if (info_verbose)
- {
- fprintf_filtered (gdb_stdout,
- "Save segment, %s bytes at %s (%c%c%c)",
- plongest (size), paddress (target_gdbarch, addr),
- read ? 'r' : ' ',
- write ? 'w' : ' ', exec ? 'x' : ' ');
- if (filename[0])
- fprintf_filtered (gdb_stdout, " for %s", filename);
- fprintf_filtered (gdb_stdout, "\n");
- }
-
- /* Invoke the callback function to create the corefile
- segment. */
- func (addr, size, read, write, exec, obfd);
- }
- do_cleanups (cleanup);
- return 0;
-}
-
-static int
-find_signalled_thread (struct thread_info *info, void *data)
-{
- if (info->suspend.stop_signal != TARGET_SIGNAL_0
- && ptid_get_pid (info->ptid) == ptid_get_pid (inferior_ptid))
- return 1;
-
- return 0;
-}
-
-static enum target_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 TARGET_SIGNAL_0;
-}
-
-/* Records the thread's register state for the corefile note
- section. */
-
-static char *
-linux_nat_do_thread_registers (bfd *obfd, ptid_t ptid,
- char *note_data, int *note_size,
- enum target_signal stop_signal)
-{
- unsigned long lwp = ptid_get_lwp (ptid);
- struct gdbarch *gdbarch = target_gdbarch;
- struct regcache *regcache = get_thread_arch_regcache (ptid, gdbarch);
- const struct regset *regset;
- int core_regset_p;
- struct cleanup *old_chain;
- struct core_regset_section *sect_list;
- char *gdb_regset;
-
- old_chain = save_inferior_ptid ();
- inferior_ptid = ptid;
- target_fetch_registers (regcache, -1);
- do_cleanups (old_chain);
-
- core_regset_p = gdbarch_regset_from_core_section_p (gdbarch);
- sect_list = gdbarch_core_regset_sections (gdbarch);
-
- /* The loop below uses the new struct core_regset_section, which stores
- the supported section names and sizes for the core file. Note that
- note PRSTATUS needs to be treated specially. But the other notes are
- structurally the same, so they can benefit from the new struct. */
- if (core_regset_p && sect_list != NULL)
- while (sect_list->sect_name != NULL)
- {
- regset = gdbarch_regset_from_core_section (gdbarch,
- sect_list->sect_name,
- sect_list->size);
- gdb_assert (regset && regset->collect_regset);
- gdb_regset = xmalloc (sect_list->size);
- regset->collect_regset (regset, regcache, -1,
- gdb_regset, sect_list->size);
-
- if (strcmp (sect_list->sect_name, ".reg") == 0)
- note_data = (char *) elfcore_write_prstatus
- (obfd, note_data, note_size,
- lwp, target_signal_to_host (stop_signal),
- gdb_regset);
- else
- note_data = (char *) elfcore_write_register_note
- (obfd, note_data, note_size,
- sect_list->sect_name, gdb_regset,
- sect_list->size);
- xfree (gdb_regset);
- sect_list++;
- }
-
- /* For architectures that does not have the struct core_regset_section
- implemented, we use the old method. When all the architectures have
- the new support, the code below should be deleted. */
- else
- {
- gdb_gregset_t gregs;
- gdb_fpregset_t fpregs;
-
- if (core_regset_p
- && (regset = gdbarch_regset_from_core_section (gdbarch, ".reg",
- sizeof (gregs)))
- != NULL && regset->collect_regset != NULL)
- regset->collect_regset (regset, regcache, -1,
- &gregs, sizeof (gregs));
- else
- fill_gregset (regcache, &gregs, -1);
-
- note_data = (char *) elfcore_write_prstatus
- (obfd, note_data, note_size, lwp, target_signal_to_host (stop_signal),
- &gregs);
-
- if (core_regset_p
- && (regset = gdbarch_regset_from_core_section (gdbarch, ".reg2",
- sizeof (fpregs)))
- != NULL && regset->collect_regset != NULL)
- regset->collect_regset (regset, regcache, -1,
- &fpregs, sizeof (fpregs));
- else
- fill_fpregset (regcache, &fpregs, -1);
-
- note_data = (char *) elfcore_write_prfpreg (obfd,
- note_data,
- note_size,
- &fpregs, sizeof (fpregs));
- }
-
- return note_data;
-}
-
-struct linux_nat_corefile_thread_data
-{
- bfd *obfd;
- char *note_data;
- int *note_size;
- int num_notes;
- enum target_signal stop_signal;
-};
-
-/* Called by gdbthread.c once per thread. Records the thread's
- register state for the corefile note section. */
-
-static int
-linux_nat_corefile_thread_callback (struct lwp_info *ti, void *data)
-{
- struct linux_nat_corefile_thread_data *args = data;
-
- args->note_data = linux_nat_do_thread_registers (args->obfd,
- ti->ptid,
- args->note_data,
- args->note_size,
- args->stop_signal);
- args->num_notes++;
-
- return 0;
-}
-
-/* Enumerate spufs IDs for process PID. */
-
-static void
-iterate_over_spus (int pid, void (*callback) (void *, int), void *data)
-{
- char path[128];
- DIR *dir;
- struct dirent *entry;
-
- xsnprintf (path, sizeof path, "/proc/%d/fd", pid);
- dir = opendir (path);
- if (!dir)
- return;
-
- rewinddir (dir);
- while ((entry = readdir (dir)) != NULL)
- {
- struct stat st;
- struct statfs stfs;
- int fd;