+ do_cleanups (cleanup);
+}
+
+static LONGEST
+linux_nat_xfer_osdata (struct target_ops *ops, enum target_object object,
+ const char *annex, gdb_byte *readbuf,
+ const gdb_byte *writebuf, ULONGEST offset, LONGEST len)
+{
+ /* We make the process list snapshot when the object starts to be
+ read. */
+ static const char *buf;
+ static LONGEST len_avail = -1;
+ static struct obstack obstack;
+
+ DIR *dirp;
+
+ gdb_assert (object == TARGET_OBJECT_OSDATA);
+
+ if (!annex)
+ {
+ if (offset == 0)
+ {
+ if (len_avail != -1 && len_avail != 0)
+ obstack_free (&obstack, NULL);
+ len_avail = 0;
+ buf = NULL;
+ obstack_init (&obstack);
+ obstack_grow_str (&obstack, "<osdata type=\"types\">\n");
+
+ obstack_xml_printf (&obstack,
+ "<item>"
+ "<column name=\"Type\">processes</column>"
+ "<column name=\"Description\">"
+ "Listing of all processes</column>"
+ "</item>");
+
+ obstack_grow_str0 (&obstack, "</osdata>\n");
+ buf = obstack_finish (&obstack);
+ len_avail = strlen (buf);
+ }
+
+ if (offset >= len_avail)
+ {
+ /* Done. Get rid of the obstack. */
+ obstack_free (&obstack, NULL);
+ buf = NULL;
+ len_avail = 0;
+ return 0;
+ }
+
+ if (len > len_avail - offset)
+ len = len_avail - offset;
+ memcpy (readbuf, buf + offset, len);
+
+ return len;
+ }
+
+ if (strcmp (annex, "processes") != 0)
+ return 0;
+
+ gdb_assert (readbuf && !writebuf);
+
+ if (offset == 0)
+ {
+ if (len_avail != -1 && len_avail != 0)
+ obstack_free (&obstack, NULL);
+ len_avail = 0;
+ buf = NULL;
+ obstack_init (&obstack);
+ obstack_grow_str (&obstack, "<osdata type=\"processes\">\n");
+
+ dirp = opendir ("/proc");
+ if (dirp)
+ {
+ struct dirent *dp;
+
+ while ((dp = readdir (dirp)) != NULL)
+ {
+ struct stat statbuf;
+ char procentry[sizeof ("/proc/4294967295")];
+
+ if (!isdigit (dp->d_name[0])
+ || NAMELEN (dp) > sizeof ("4294967295") - 1)
+ continue;
+
+ sprintf (procentry, "/proc/%s", dp->d_name);
+ if (stat (procentry, &statbuf) == 0
+ && S_ISDIR (statbuf.st_mode))
+ {
+ char *pathname;
+ FILE *f;
+ char cmd[MAXPATHLEN + 1];
+ struct passwd *entry;
+
+ pathname = xstrprintf ("/proc/%s/cmdline", dp->d_name);
+ entry = getpwuid (statbuf.st_uid);
+
+ if ((f = fopen (pathname, "r")) != NULL)
+ {
+ size_t length = fread (cmd, 1, sizeof (cmd) - 1, f);
+
+ if (length > 0)
+ {
+ int i;
+
+ for (i = 0; i < length; i++)
+ if (cmd[i] == '\0')
+ cmd[i] = ' ';
+ cmd[length] = '\0';
+
+ obstack_xml_printf (
+ &obstack,
+ "<item>"
+ "<column name=\"pid\">%s</column>"
+ "<column name=\"user\">%s</column>"
+ "<column name=\"command\">%s</column>"
+ "</item>",
+ dp->d_name,
+ entry ? entry->pw_name : "?",
+ cmd);
+ }
+ fclose (f);
+ }
+
+ xfree (pathname);
+ }
+ }
+
+ closedir (dirp);
+ }
+
+ obstack_grow_str0 (&obstack, "</osdata>\n");
+ buf = obstack_finish (&obstack);
+ len_avail = strlen (buf);
+ }
+
+ if (offset >= len_avail)
+ {
+ /* Done. Get rid of the obstack. */
+ obstack_free (&obstack, NULL);
+ buf = NULL;
+ len_avail = 0;
+ return 0;
+ }
+
+ if (len > len_avail - offset)
+ len = len_avail - offset;
+ memcpy (readbuf, buf + offset, len);
+
+ return len;
+}
+
+static LONGEST
+linux_xfer_partial (struct target_ops *ops, enum target_object object,
+ const char *annex, gdb_byte *readbuf,
+ const gdb_byte *writebuf, ULONGEST offset, LONGEST len)
+{
+ LONGEST xfer;
+
+ if (object == TARGET_OBJECT_AUXV)
+ return memory_xfer_auxv (ops, object, annex, readbuf, writebuf,
+ offset, len);
+
+ if (object == TARGET_OBJECT_OSDATA)
+ return linux_nat_xfer_osdata (ops, object, annex, readbuf, writebuf,
+ offset, len);
+
+ if (object == TARGET_OBJECT_SPU)
+ return linux_proc_xfer_spu (ops, object, annex, readbuf, writebuf,
+ offset, len);
+
+ /* GDB calculates all the addresses in possibly larget width of the address.
+ Address width needs to be masked before its final use - either by
+ linux_proc_xfer_partial or inf_ptrace_xfer_partial.
+
+ Compare ADDR_BIT first to avoid a compiler warning on shift overflow. */
+
+ if (object == TARGET_OBJECT_MEMORY)
+ {
+ int addr_bit = gdbarch_addr_bit (target_gdbarch);
+
+ if (addr_bit < (sizeof (ULONGEST) * HOST_CHAR_BIT))
+ offset &= ((ULONGEST) 1 << addr_bit) - 1;
+ }
+
+ xfer = linux_proc_xfer_partial (ops, object, annex, readbuf, writebuf,
+ offset, len);
+ if (xfer != 0)
+ return xfer;
+
+ return super_xfer_partial (ops, object, annex, readbuf, writebuf,
+ offset, len);
+}
+
+/* Create a prototype generic GNU/Linux target. The client can override
+ it with local methods. */
+
+static void
+linux_target_install_ops (struct target_ops *t)
+{
+ t->to_insert_fork_catchpoint = linux_child_insert_fork_catchpoint;
+ t->to_remove_fork_catchpoint = linux_child_remove_fork_catchpoint;
+ t->to_insert_vfork_catchpoint = linux_child_insert_vfork_catchpoint;
+ t->to_remove_vfork_catchpoint = linux_child_remove_vfork_catchpoint;
+ t->to_insert_exec_catchpoint = linux_child_insert_exec_catchpoint;
+ t->to_remove_exec_catchpoint = linux_child_remove_exec_catchpoint;
+ t->to_set_syscall_catchpoint = linux_child_set_syscall_catchpoint;
+ t->to_pid_to_exec_file = linux_child_pid_to_exec_file;
+ t->to_post_startup_inferior = linux_child_post_startup_inferior;
+ t->to_post_attach = linux_child_post_attach;
+ t->to_follow_fork = linux_child_follow_fork;
+ t->to_find_memory_regions = linux_nat_find_memory_regions;
+ t->to_make_corefile_notes = linux_nat_make_corefile_notes;
+
+ super_xfer_partial = t->to_xfer_partial;
+ t->to_xfer_partial = linux_xfer_partial;
+}
+
+struct target_ops *
+linux_target (void)
+{
+ struct target_ops *t;
+
+ t = inf_ptrace_target ();
+ linux_target_install_ops (t);
+
+ return t;
+}
+
+struct target_ops *
+linux_trad_target (CORE_ADDR (*register_u_offset)(struct gdbarch *, int, int))
+{
+ struct target_ops *t;
+
+ t = inf_ptrace_trad_target (register_u_offset);
+ linux_target_install_ops (t);
+
+ return t;
+}
+
+/* target_is_async_p implementation. */
+
+static int
+linux_nat_is_async_p (void)
+{
+ /* NOTE: palves 2008-03-21: We're only async when the user requests
+ it explicitly with the "set target-async" command.
+ Someday, linux will always be async. */
+ return target_async_permitted;
+}
+
+/* target_can_async_p implementation. */
+
+static int
+linux_nat_can_async_p (void)
+{
+ /* NOTE: palves 2008-03-21: We're only async when the user requests
+ it explicitly with the "set target-async" command.
+ Someday, linux will always be async. */
+ return target_async_permitted;
+}
+
+static int
+linux_nat_supports_non_stop (void)
+{
+ return 1;
+}
+
+/* True if we want to support multi-process. To be removed when GDB
+ supports multi-exec. */
+
+int linux_multi_process = 1;
+
+static int
+linux_nat_supports_multi_process (void)
+{
+ return linux_multi_process;
+}
+
+static int async_terminal_is_ours = 1;
+
+/* target_terminal_inferior implementation. */
+
+static void
+linux_nat_terminal_inferior (void)
+{
+ if (!target_is_async_p ())
+ {
+ /* Async mode is disabled. */
+ terminal_inferior ();
+ return;
+ }
+
+ terminal_inferior ();
+
+ /* Calls to target_terminal_*() are meant to be idempotent. */
+ if (!async_terminal_is_ours)
+ return;
+
+ delete_file_handler (input_fd);
+ async_terminal_is_ours = 0;
+ set_sigint_trap ();
+}
+
+/* target_terminal_ours implementation. */
+
+static void
+linux_nat_terminal_ours (void)
+{
+ if (!target_is_async_p ())
+ {
+ /* Async mode is disabled. */
+ terminal_ours ();
+ return;
+ }
+
+ /* GDB should never give the terminal to the inferior if the
+ inferior is running in the background (run&, continue&, etc.),
+ but claiming it sure should. */
+ terminal_ours ();
+
+ if (async_terminal_is_ours)
+ return;
+
+ clear_sigint_trap ();
+ add_file_handler (input_fd, stdin_event_handler, 0);
+ async_terminal_is_ours = 1;
+}
+
+static void (*async_client_callback) (enum inferior_event_type event_type,
+ void *context);
+static void *async_client_context;
+
+/* SIGCHLD handler that serves two purposes: In non-stop/async mode,
+ so we notice when any child changes state, and notify the
+ event-loop; it allows us to use sigsuspend in linux_nat_wait_1
+ above to wait for the arrival of a SIGCHLD. */
+
+static void
+sigchld_handler (int signo)
+{
+ int old_errno = errno;
+
+ if (debug_linux_nat)
+ ui_file_write_async_safe (gdb_stdlog,
+ "sigchld\n", sizeof ("sigchld\n") - 1);
+
+ if (signo == SIGCHLD
+ && linux_nat_event_pipe[0] != -1)
+ async_file_mark (); /* Let the event loop know that there are
+ events to handle. */
+
+ errno = old_errno;
+}
+
+/* Callback registered with the target events file descriptor. */
+
+static void
+handle_target_event (int error, gdb_client_data client_data)
+{
+ (*async_client_callback) (INF_REG_EVENT, async_client_context);
+}
+
+/* Create/destroy the target events pipe. Returns previous state. */
+
+static int
+linux_async_pipe (int enable)
+{
+ int previous = (linux_nat_event_pipe[0] != -1);
+
+ if (previous != enable)
+ {
+ sigset_t prev_mask;
+
+ block_child_signals (&prev_mask);
+
+ if (enable)
+ {
+ if (pipe (linux_nat_event_pipe) == -1)
+ internal_error (__FILE__, __LINE__,
+ "creating event pipe failed.");
+
+ fcntl (linux_nat_event_pipe[0], F_SETFL, O_NONBLOCK);
+ fcntl (linux_nat_event_pipe[1], F_SETFL, O_NONBLOCK);
+ }
+ else
+ {
+ close (linux_nat_event_pipe[0]);
+ close (linux_nat_event_pipe[1]);
+ linux_nat_event_pipe[0] = -1;
+ linux_nat_event_pipe[1] = -1;
+ }
+
+ restore_child_signals_mask (&prev_mask);
+ }
+
+ return previous;
+}
+
+/* target_async implementation. */
+
+static void
+linux_nat_async (void (*callback) (enum inferior_event_type event_type,
+ void *context), void *context)
+{
+ if (callback != NULL)
+ {
+ async_client_callback = callback;
+ async_client_context = context;
+ if (!linux_async_pipe (1))
+ {
+ add_file_handler (linux_nat_event_pipe[0],
+ handle_target_event, NULL);
+ /* There may be pending events to handle. Tell the event loop
+ to poll them. */
+ async_file_mark ();
+ }
+ }
+ else
+ {
+ async_client_callback = callback;
+ async_client_context = context;
+ delete_file_handler (linux_nat_event_pipe[0]);
+ linux_async_pipe (0);
+ }
+ return;
+}
+
+/* Stop an LWP, and push a TARGET_SIGNAL_0 stop status if no other
+ event came out. */
+
+static int
+linux_nat_stop_lwp (struct lwp_info *lwp, void *data)
+{
+ if (!lwp->stopped)
+ {
+ ptid_t ptid = lwp->ptid;
+
+ if (debug_linux_nat)
+ fprintf_unfiltered (gdb_stdlog,
+ "LNSL: running -> suspending %s\n",
+ target_pid_to_str (lwp->ptid));
+
+
+ stop_callback (lwp, NULL);
+ stop_wait_callback (lwp, NULL);
+
+ /* If the lwp exits while we try to stop it, there's nothing
+ else to do. */
+ lwp = find_lwp_pid (ptid);
+ if (lwp == NULL)
+ return 0;
+
+ /* If we didn't collect any signal other than SIGSTOP while
+ stopping the LWP, push a SIGNAL_0 event. In either case, the
+ event-loop will end up calling target_wait which will collect
+ these. */
+ if (lwp->status == 0)
+ lwp->status = W_STOPCODE (0);
+ async_file_mark ();
+ }
+ else
+ {
+ /* Already known to be stopped; do nothing. */
+
+ if (debug_linux_nat)
+ {
+ if (find_thread_ptid (lwp->ptid)->stop_requested)
+ fprintf_unfiltered (gdb_stdlog,
+ "LNSL: already stopped/stop_requested %s\n",
+ target_pid_to_str (lwp->ptid));
+ else
+ fprintf_unfiltered (gdb_stdlog,
+ "LNSL: already stopped/no "
+ "stop_requested yet %s\n",
+ target_pid_to_str (lwp->ptid));
+ }
+ }
+ return 0;
+}
+
+static void
+linux_nat_stop (ptid_t ptid)
+{
+ if (non_stop)
+ iterate_over_lwps (ptid, linux_nat_stop_lwp, NULL);
+ else
+ linux_ops->to_stop (ptid);
+}
+
+static void
+linux_nat_close (int quitting)
+{
+ /* Unregister from the event loop. */
+ if (target_is_async_p ())
+ target_async (NULL, 0);
+
+ if (linux_ops->to_close)
+ linux_ops->to_close (quitting);
+}
+
+/* When requests are passed down from the linux-nat layer to the
+ single threaded inf-ptrace layer, ptids of (lwpid,0,0) form are
+ used. The address space pointer is stored in the inferior object,
+ but the common code that is passed such ptid can't tell whether
+ lwpid is a "main" process id or not (it assumes so). We reverse
+ look up the "main" process id from the lwp here. */
+
+struct address_space *
+linux_nat_thread_address_space (struct target_ops *t, ptid_t ptid)
+{
+ struct lwp_info *lwp;
+ struct inferior *inf;
+ int pid;
+
+ pid = GET_LWP (ptid);
+ if (GET_LWP (ptid) == 0)
+ {
+ /* An (lwpid,0,0) ptid. Look up the lwp object to get at the
+ tgid. */
+ lwp = find_lwp_pid (ptid);
+ pid = GET_PID (lwp->ptid);
+ }
+ else
+ {
+ /* A (pid,lwpid,0) ptid. */
+ pid = GET_PID (ptid);
+ }
+
+ inf = find_inferior_pid (pid);
+ gdb_assert (inf != NULL);
+ return inf->aspace;
+}
+
+int
+linux_nat_core_of_thread_1 (ptid_t ptid)
+{
+ struct cleanup *back_to;
+ char *filename;
+ FILE *f;
+ char *content = NULL;
+ char *p;
+ char *ts = 0;
+ int content_read = 0;
+ int i;
+ int core;
+
+ filename = xstrprintf ("/proc/%d/task/%ld/stat",
+ GET_PID (ptid), GET_LWP (ptid));
+ back_to = make_cleanup (xfree, filename);
+
+ f = fopen (filename, "r");
+ if (!f)
+ {
+ do_cleanups (back_to);
+ return -1;
+ }
+
+ make_cleanup_fclose (f);
+
+ for (;;)
+ {
+ int n;
+
+ content = xrealloc (content, content_read + 1024);
+ n = fread (content + content_read, 1, 1024, f);
+ content_read += n;
+ if (n < 1024)
+ {
+ content[content_read] = '\0';
+ break;
+ }
+ }
+
+ make_cleanup (xfree, content);
+
+ p = strchr (content, '(');
+
+ /* Skip ")". */
+ if (p != NULL)
+ p = strchr (p, ')');
+ if (p != NULL)
+ p++;
+
+ /* If the first field after program name has index 0, then core number is
+ the field with index 36. There's no constant for that anywhere. */
+ if (p != NULL)
+ p = strtok_r (p, " ", &ts);
+ for (i = 0; p != NULL && i != 36; ++i)
+ p = strtok_r (NULL, " ", &ts);
+
+ if (p == NULL || sscanf (p, "%d", &core) == 0)
+ core = -1;
+
+ do_cleanups (back_to);
+
+ return core;
+}
+
+/* Return the cached value of the processor core for thread PTID. */
+
+int
+linux_nat_core_of_thread (struct target_ops *ops, ptid_t ptid)
+{
+ struct lwp_info *info = find_lwp_pid (ptid);
+
+ if (info)
+ return info->core;
+ return -1;