gdb/nto-procfs.c - gdb

Global variables defined

Functions defined

Macros defined

Source code

  1. /* Machine independent support for QNX Neutrino /proc (process file system)
  2.    for GDB.  Written by Colin Burgess at QNX Software Systems Limited.

  3.    Copyright (C) 2003-2015 Free Software Foundation, Inc.

  4.    Contributed by QNX Software Systems Ltd.

  5.    This file is part of GDB.

  6.    This program is free software; you can redistribute it and/or modify
  7.    it under the terms of the GNU General Public License as published by
  8.    the Free Software Foundation; either version 3 of the License, or
  9.    (at your option) any later version.

  10.    This program is distributed in the hope that it will be useful,
  11.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.    GNU General Public License for more details.

  14.    You should have received a copy of the GNU General Public License
  15.    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */

  16. #include "defs.h"

  17. #include <fcntl.h>
  18. #include <spawn.h>
  19. #include <sys/debug.h>
  20. #include <sys/procfs.h>
  21. #include <sys/neutrino.h>
  22. #include <sys/syspage.h>
  23. #include <dirent.h>
  24. #include <sys/netmgr.h>
  25. #include "gdbcore.h"
  26. #include "inferior.h"
  27. #include "target.h"
  28. #include "objfiles.h"
  29. #include "gdbthread.h"
  30. #include "nto-tdep.h"
  31. #include "command.h"
  32. #include "regcache.h"
  33. #include "solib.h"
  34. #include "inf-child.h"

  35. #define NULL_PID                0
  36. #define _DEBUG_FLAG_TRACE        (_DEBUG_FLAG_TRACE_EXEC|_DEBUG_FLAG_TRACE_RD|\
  37.                 _DEBUG_FLAG_TRACE_WR|_DEBUG_FLAG_TRACE_MODIFY)

  38. int ctl_fd;

  39. static void (*ofunc) ();

  40. static procfs_run run;

  41. static ptid_t do_attach (ptid_t ptid);

  42. static int procfs_can_use_hw_breakpoint (struct target_ops *self,
  43.                                          int, int, int);

  44. static int procfs_insert_hw_watchpoint (struct target_ops *self,
  45.                                         CORE_ADDR addr, int len, int type,
  46.                                         struct expression *cond);

  47. static int procfs_remove_hw_watchpoint (struct target_ops *self,
  48.                                         CORE_ADDR addr, int len, int type,
  49.                                         struct expression *cond);

  50. static int procfs_stopped_by_watchpoint (struct target_ops *ops);

  51. /* These two globals are only ever set in procfs_open_1, but are
  52.    referenced elsewhere.  'nto_procfs_node' is a flag used to say
  53.    whether we are local, or we should get the current node descriptor
  54.    for the remote QNX node.  */
  55. static char nto_procfs_path[PATH_MAX] = { "/proc" };
  56. static unsigned nto_procfs_node = ND_LOCAL_NODE;

  57. /* Return the current QNX Node, or error out.  This is a simple
  58.    wrapper for the netmgr_strtond() function.  The reason this
  59.    is required is because QNX node descriptors are transient so
  60.    we have to re-acquire them every time.  */
  61. static unsigned
  62. nto_node (void)
  63. {
  64.   unsigned node;

  65.   if (ND_NODE_CMP (nto_procfs_node, ND_LOCAL_NODE) == 0)
  66.     return ND_LOCAL_NODE;

  67.   node = netmgr_strtond (nto_procfs_path, 0);
  68.   if (node == -1)
  69.     error (_("Lost the QNX node.  Debug session probably over."));

  70.   return (node);
  71. }

  72. static enum gdb_osabi
  73. procfs_is_nto_target (bfd *abfd)
  74. {
  75.   return GDB_OSABI_QNXNTO;
  76. }

  77. /* This is called when we call 'target native' or 'target procfs
  78.    <arg>' from the (gdb) prompt.  For QNX6 (nto), the only valid arg
  79.    will be a QNX node string, eg: "/net/some_node".  If arg is not a
  80.    valid QNX node, we will default to local.  */
  81. static void
  82. procfs_open_1 (struct target_ops *ops, const char *arg, int from_tty)
  83. {
  84.   char *nodestr;
  85.   char *endstr;
  86.   char buffer[50];
  87.   int fd, total_size;
  88.   procfs_sysinfo *sysinfo;
  89.   struct cleanup *cleanups;

  90.   /* Offer to kill previous inferiors before opening this target.  */
  91.   target_preopen (from_tty);

  92.   nto_is_nto_target = procfs_is_nto_target;

  93.   /* Set the default node used for spawning to this one,
  94.      and only override it if there is a valid arg.  */

  95.   nto_procfs_node = ND_LOCAL_NODE;
  96.   nodestr = arg ? xstrdup (arg) : arg;

  97.   init_thread_list ();

  98.   if (nodestr)
  99.     {
  100.       nto_procfs_node = netmgr_strtond (nodestr, &endstr);
  101.       if (nto_procfs_node == -1)
  102.         {
  103.           if (errno == ENOTSUP)
  104.             printf_filtered ("QNX Net Manager not found.\n");
  105.           printf_filtered ("Invalid QNX node %s: error %d (%s).\n", nodestr,
  106.                            errno, safe_strerror (errno));
  107.           xfree (nodestr);
  108.           nodestr = NULL;
  109.           nto_procfs_node = ND_LOCAL_NODE;
  110.         }
  111.       else if (*endstr)
  112.         {
  113.           if (*(endstr - 1) == '/')
  114.             *(endstr - 1) = 0;
  115.           else
  116.             *endstr = 0;
  117.         }
  118.     }
  119.   snprintf (nto_procfs_path, PATH_MAX - 1, "%s%s", nodestr ? nodestr : "",
  120.             "/proc");
  121.   if (nodestr)
  122.     xfree (nodestr);

  123.   fd = open (nto_procfs_path, O_RDONLY);
  124.   if (fd == -1)
  125.     {
  126.       printf_filtered ("Error opening %s : %d (%s)\n", nto_procfs_path, errno,
  127.                        safe_strerror (errno));
  128.       error (_("Invalid procfs arg"));
  129.     }
  130.   cleanups = make_cleanup_close (fd);

  131.   sysinfo = (void *) buffer;
  132.   if (devctl (fd, DCMD_PROC_SYSINFO, sysinfo, sizeof buffer, 0) != EOK)
  133.     {
  134.       printf_filtered ("Error getting size: %d (%s)\n", errno,
  135.                        safe_strerror (errno));
  136.       error (_("Devctl failed."));
  137.     }
  138.   else
  139.     {
  140.       total_size = sysinfo->total_size;
  141.       sysinfo = alloca (total_size);
  142.       if (!sysinfo)
  143.         {
  144.           printf_filtered ("Memory error: %d (%s)\n", errno,
  145.                            safe_strerror (errno));
  146.           error (_("alloca failed."));
  147.         }
  148.       else
  149.         {
  150.           if (devctl (fd, DCMD_PROC_SYSINFO, sysinfo, total_size, 0) != EOK)
  151.             {
  152.               printf_filtered ("Error getting sysinfo: %d (%s)\n", errno,
  153.                                safe_strerror (errno));
  154.               error (_("Devctl failed."));
  155.             }
  156.           else
  157.             {
  158.               if (sysinfo->type !=
  159.                   nto_map_arch_to_cputype (gdbarch_bfd_arch_info
  160.                                            (target_gdbarch ())->arch_name))
  161.                 error (_("Invalid target CPU."));
  162.             }
  163.         }
  164.     }
  165.   do_cleanups (cleanups);

  166.   inf_child_open_target (ops, arg, from_tty);
  167.   printf_filtered ("Debugging using %s\n", nto_procfs_path);
  168. }

  169. static void
  170. procfs_set_thread (ptid_t ptid)
  171. {
  172.   pid_t tid;

  173.   tid = ptid_get_tid (ptid);
  174.   devctl (ctl_fd, DCMD_PROC_CURTHREAD, &tid, sizeof (tid), 0);
  175. }

  176. /*  Return nonzero if the thread TH is still alive.  */
  177. static int
  178. procfs_thread_alive (struct target_ops *ops, ptid_t ptid)
  179. {
  180.   pid_t tid;
  181.   pid_t pid;
  182.   procfs_status status;
  183.   int err;

  184.   tid = ptid_get_tid (ptid);
  185.   pid = ptid_get_pid (ptid);

  186.   if (kill (pid, 0) == -1)
  187.     return 0;

  188.   status.tid = tid;
  189.   if ((err = devctl (ctl_fd, DCMD_PROC_TIDSTATUS,
  190.                      &status, sizeof (status), 0)) != EOK)
  191.     return 0;

  192.   /* Thread is alive or dead but not yet joined,
  193.      or dead and there is an alive (or dead unjoined) thread with
  194.      higher tid.

  195.      If the tid is not the same as requested, requested tid is dead.  */
  196.   return (status.tid == tid) && (status.state != STATE_DEAD);
  197. }

  198. static void
  199. update_thread_private_data_name (struct thread_info *new_thread,
  200.                                  const char *newname)
  201. {
  202.   int newnamelen;
  203.   struct private_thread_info *pti;

  204.   gdb_assert (newname != NULL);
  205.   gdb_assert (new_thread != NULL);
  206.   newnamelen = strlen (newname);
  207.   if (!new_thread->private)
  208.     {
  209.       new_thread->private = xmalloc (offsetof (struct private_thread_info,
  210.                                                name)
  211.                                      + newnamelen + 1);
  212.       memcpy (new_thread->private->name, newname, newnamelen + 1);
  213.     }
  214.   else if (strcmp (newname, new_thread->private->name) != 0)
  215.     {
  216.       /* Reallocate if neccessary.  */
  217.       int oldnamelen = strlen (new_thread->private->name);

  218.       if (oldnamelen < newnamelen)
  219.         new_thread->private = xrealloc (new_thread->private,
  220.                                         offsetof (struct private_thread_info,
  221.                                                   name)
  222.                                         + newnamelen + 1);
  223.       memcpy (new_thread->private->name, newname, newnamelen + 1);
  224.     }
  225. }

  226. static void
  227. update_thread_private_data (struct thread_info *new_thread,
  228.                             pthread_t tid, int state, int flags)
  229. {
  230.   struct private_thread_info *pti;
  231.   procfs_info pidinfo;
  232.   struct _thread_name *tn;
  233.   procfs_threadctl tctl;

  234. #if _NTO_VERSION > 630
  235.   gdb_assert (new_thread != NULL);

  236.   if (devctl (ctl_fd, DCMD_PROC_INFO, &pidinfo,
  237.               sizeof(pidinfo), 0) != EOK)
  238.     return;

  239.   memset (&tctl, 0, sizeof (tctl));
  240.   tctl.cmd = _NTO_TCTL_NAME;
  241.   tn = (struct _thread_name *) (&tctl.data);

  242.   /* Fetch name for the given thread.  */
  243.   tctl.tid = tid;
  244.   tn->name_buf_len = sizeof (tctl.data) - sizeof (*tn);
  245.   tn->new_name_len = -1; /* Getting, not setting.  */
  246.   if (devctl (ctl_fd, DCMD_PROC_THREADCTL, &tctl, sizeof (tctl), NULL) != EOK)
  247.     tn->name_buf[0] = '\0';

  248.   tn->name_buf[_NTO_THREAD_NAME_MAX] = '\0';

  249.   update_thread_private_data_name (new_thread, tn->name_buf);

  250.   pti = (struct private_thread_info *) new_thread->private;
  251.   pti->tid = tid;
  252.   pti->state = state;
  253.   pti->flags = flags;
  254. #endif /* _NTO_VERSION */
  255. }

  256. static void
  257. procfs_update_thread_list (struct target_ops *ops)
  258. {
  259.   procfs_status status;
  260.   pid_t pid;
  261.   ptid_t ptid;
  262.   pthread_t tid;
  263.   struct thread_info *new_thread;

  264.   if (ctl_fd == -1)
  265.     return;

  266.   prune_threads ();

  267.   pid = ptid_get_pid (inferior_ptid);

  268.   status.tid = 1;

  269.   for (tid = 1;; ++tid)
  270.     {
  271.       if (status.tid == tid
  272.           && (devctl (ctl_fd, DCMD_PROC_TIDSTATUS, &status, sizeof (status), 0)
  273.               != EOK))
  274.         break;
  275.       if (status.tid != tid)
  276.         /* The reason why this would not be equal is that devctl might have
  277.            returned different tid, meaning the requested tid no longer exists
  278.            (e.g. thread exited).  */
  279.         continue;
  280.       ptid = ptid_build (pid, 0, tid);
  281.       new_thread = find_thread_ptid (ptid);
  282.       if (!new_thread)
  283.         new_thread = add_thread (ptid);
  284.       update_thread_private_data (new_thread, tid, status.state, 0);
  285.       status.tid++;
  286.     }
  287.   return;
  288. }

  289. static void
  290. do_closedir_cleanup (void *dir)
  291. {
  292.   closedir (dir);
  293. }

  294. void
  295. procfs_pidlist (char *args, int from_tty)
  296. {
  297.   DIR *dp = NULL;
  298.   struct dirent *dirp = NULL;
  299.   char buf[512];
  300.   procfs_info *pidinfo = NULL;
  301.   procfs_debuginfo *info = NULL;
  302.   procfs_status *status = NULL;
  303.   pid_t num_threads = 0;
  304.   pid_t pid;
  305.   char name[512];
  306.   struct cleanup *cleanups;

  307.   dp = opendir (nto_procfs_path);
  308.   if (dp == NULL)
  309.     {
  310.       fprintf_unfiltered (gdb_stderr, "failed to opendir \"%s\" - %d (%s)",
  311.                           nto_procfs_path, errno, safe_strerror (errno));
  312.       return;
  313.     }

  314.   cleanups = make_cleanup (do_closedir_cleanup, dp);

  315.   /* Start scan at first pid.  */
  316.   rewinddir (dp);

  317.   do
  318.     {
  319.       int fd;
  320.       struct cleanup *inner_cleanup;

  321.       /* Get the right pid and procfs path for the pid.  */
  322.       do
  323.         {
  324.           dirp = readdir (dp);
  325.           if (dirp == NULL)
  326.             {
  327.               do_cleanups (cleanups);
  328.               return;
  329.             }
  330.           snprintf (buf, 511, "%s/%s/as", nto_procfs_path, dirp->d_name);
  331.           pid = atoi (dirp->d_name);
  332.         }
  333.       while (pid == 0);

  334.       /* Open the procfs path.  */
  335.       fd = open (buf, O_RDONLY);
  336.       if (fd == -1)
  337.         {
  338.           fprintf_unfiltered (gdb_stderr, "failed to open %s - %d (%s)\n",
  339.                               buf, errno, safe_strerror (errno));
  340.           do_cleanups (cleanups);
  341.           return;
  342.         }
  343.       inner_cleanup = make_cleanup_close (fd);

  344.       pidinfo = (procfs_info *) buf;
  345.       if (devctl (fd, DCMD_PROC_INFO, pidinfo, sizeof (buf), 0) != EOK)
  346.         {
  347.           fprintf_unfiltered (gdb_stderr,
  348.                               "devctl DCMD_PROC_INFO failed - %d (%s)\n",
  349.                               errno, safe_strerror (errno));
  350.           break;
  351.         }
  352.       num_threads = pidinfo->num_threads;

  353.       info = (procfs_debuginfo *) buf;
  354.       if (devctl (fd, DCMD_PROC_MAPDEBUG_BASE, info, sizeof (buf), 0) != EOK)
  355.         strcpy (name, "unavailable");
  356.       else
  357.         strcpy (name, info->path);

  358.       /* Collect state info on all the threads.  */
  359.       status = (procfs_status *) buf;
  360.       for (status->tid = 1; status->tid <= num_threads; status->tid++)
  361.         {
  362.           if (devctl (fd, DCMD_PROC_TIDSTATUS, status, sizeof (buf), 0) != EOK
  363.               && status->tid != 0)
  364.             break;
  365.           if (status->tid != 0)
  366.             printf_filtered ("%s - %d/%d\n", name, pid, status->tid);
  367.         }

  368.       do_cleanups (inner_cleanup);
  369.     }
  370.   while (dirp != NULL);

  371.   do_cleanups (cleanups);
  372.   return;
  373. }

  374. void
  375. procfs_meminfo (char *args, int from_tty)
  376. {
  377.   procfs_mapinfo *mapinfos = NULL;
  378.   static int num_mapinfos = 0;
  379.   procfs_mapinfo *mapinfo_p, *mapinfo_p2;
  380.   int flags = ~0, err, num, i, j;

  381.   struct
  382.   {
  383.     procfs_debuginfo info;
  384.     char buff[_POSIX_PATH_MAX];
  385.   } map;

  386.   struct info
  387.   {
  388.     unsigned addr;
  389.     unsigned size;
  390.     unsigned flags;
  391.     unsigned debug_vaddr;
  392.     unsigned long long offset;
  393.   };

  394.   struct printinfo
  395.   {
  396.     unsigned long long ino;
  397.     unsigned dev;
  398.     struct info text;
  399.     struct info data;
  400.     char name[256];
  401.   } printme;

  402.   /* Get the number of map entrys.  */
  403.   err = devctl (ctl_fd, DCMD_PROC_MAPINFO, NULL, 0, &num);
  404.   if (err != EOK)
  405.     {
  406.       printf ("failed devctl num mapinfos - %d (%s)\n", err,
  407.               safe_strerror (err));
  408.       return;
  409.     }

  410.   mapinfos = xmalloc (num * sizeof (procfs_mapinfo));

  411.   num_mapinfos = num;
  412.   mapinfo_p = mapinfos;

  413.   /* Fill the map entrys.  */
  414.   err = devctl (ctl_fd, DCMD_PROC_MAPINFO, mapinfo_p, num
  415.                 * sizeof (procfs_mapinfo), &num);
  416.   if (err != EOK)
  417.     {
  418.       printf ("failed devctl mapinfos - %d (%s)\n", err, safe_strerror (err));
  419.       xfree (mapinfos);
  420.       return;
  421.     }

  422.   num = min (num, num_mapinfos);

  423.   /* Run through the list of mapinfos, and store the data and text info
  424.      so we can print it at the bottom of the loop.  */
  425.   for (mapinfo_p = mapinfos, i = 0; i < num; i++, mapinfo_p++)
  426.     {
  427.       if (!(mapinfo_p->flags & flags))
  428.         mapinfo_p->ino = 0;

  429.       if (mapinfo_p->ino == 0)        /* Already visited.  */
  430.         continue;

  431.       map.info.vaddr = mapinfo_p->vaddr;

  432.       err = devctl (ctl_fd, DCMD_PROC_MAPDEBUG, &map, sizeof (map), 0);
  433.       if (err != EOK)
  434.         continue;

  435.       memset (&printme, 0, sizeof printme);
  436.       printme.dev = mapinfo_p->dev;
  437.       printme.ino = mapinfo_p->ino;
  438.       printme.text.addr = mapinfo_p->vaddr;
  439.       printme.text.size = mapinfo_p->size;
  440.       printme.text.flags = mapinfo_p->flags;
  441.       printme.text.offset = mapinfo_p->offset;
  442.       printme.text.debug_vaddr = map.info.vaddr;
  443.       strcpy (printme.name, map.info.path);

  444.       /* Check for matching data.  */
  445.       for (mapinfo_p2 = mapinfos, j = 0; j < num; j++, mapinfo_p2++)
  446.         {
  447.           if (mapinfo_p2->vaddr != mapinfo_p->vaddr
  448.               && mapinfo_p2->ino == mapinfo_p->ino
  449.               && mapinfo_p2->dev == mapinfo_p->dev)
  450.             {
  451.               map.info.vaddr = mapinfo_p2->vaddr;
  452.               err =
  453.                 devctl (ctl_fd, DCMD_PROC_MAPDEBUG, &map, sizeof (map), 0);
  454.               if (err != EOK)
  455.                 continue;

  456.               if (strcmp (map.info.path, printme.name))
  457.                 continue;

  458.               /* Lower debug_vaddr is always text, if nessessary, swap.  */
  459.               if ((int) map.info.vaddr < (int) printme.text.debug_vaddr)
  460.                 {
  461.                   memcpy (&(printme.data), &(printme.text),
  462.                           sizeof (printme.data));
  463.                   printme.text.addr = mapinfo_p2->vaddr;
  464.                   printme.text.size = mapinfo_p2->size;
  465.                   printme.text.flags = mapinfo_p2->flags;
  466.                   printme.text.offset = mapinfo_p2->offset;
  467.                   printme.text.debug_vaddr = map.info.vaddr;
  468.                 }
  469.               else
  470.                 {
  471.                   printme.data.addr = mapinfo_p2->vaddr;
  472.                   printme.data.size = mapinfo_p2->size;
  473.                   printme.data.flags = mapinfo_p2->flags;
  474.                   printme.data.offset = mapinfo_p2->offset;
  475.                   printme.data.debug_vaddr = map.info.vaddr;
  476.                 }
  477.               mapinfo_p2->ino = 0;
  478.             }
  479.         }
  480.       mapinfo_p->ino = 0;

  481.       printf_filtered ("%s\n", printme.name);
  482.       printf_filtered ("\ttext=%08x bytes @ 0x%08x\n", printme.text.size,
  483.                        printme.text.addr);
  484.       printf_filtered ("\t\tflags=%08x\n", printme.text.flags);
  485.       printf_filtered ("\t\tdebug=%08x\n", printme.text.debug_vaddr);
  486.       printf_filtered ("\t\toffset=%s\n", phex (printme.text.offset, 8));
  487.       if (printme.data.size)
  488.         {
  489.           printf_filtered ("\tdata=%08x bytes @ 0x%08x\n", printme.data.size,
  490.                            printme.data.addr);
  491.           printf_filtered ("\t\tflags=%08x\n", printme.data.flags);
  492.           printf_filtered ("\t\tdebug=%08x\n", printme.data.debug_vaddr);
  493.           printf_filtered ("\t\toffset=%s\n", phex (printme.data.offset, 8));
  494.         }
  495.       printf_filtered ("\tdev=0x%x\n", printme.dev);
  496.       printf_filtered ("\tino=0x%x\n", (unsigned int) printme.ino);
  497.     }
  498.   xfree (mapinfos);
  499.   return;
  500. }

  501. /* Print status information about what we're accessing.  */
  502. static void
  503. procfs_files_info (struct target_ops *ignore)
  504. {
  505.   struct inferior *inf = current_inferior ();

  506.   printf_unfiltered ("\tUsing the running image of %s %s via %s.\n",
  507.                      inf->attach_flag ? "attached" : "child",
  508.                      target_pid_to_str (inferior_ptid), nto_procfs_path);
  509. }

  510. /* Attach to process PID, then initialize for debugging it.  */
  511. static void
  512. procfs_attach (struct target_ops *ops, const char *args, int from_tty)
  513. {
  514.   char *exec_file;
  515.   int pid;
  516.   struct inferior *inf;

  517.   pid = parse_pid_to_attach (args);

  518.   if (pid == getpid ())
  519.     error (_("Attaching GDB to itself is not a good idea..."));

  520.   if (from_tty)
  521.     {
  522.       exec_file = (char *) get_exec_file (0);

  523.       if (exec_file)
  524.         printf_unfiltered ("Attaching to program `%s', %s\n", exec_file,
  525.                            target_pid_to_str (pid_to_ptid (pid)));
  526.       else
  527.         printf_unfiltered ("Attaching to %s\n",
  528.                            target_pid_to_str (pid_to_ptid (pid)));

  529.       gdb_flush (gdb_stdout);
  530.     }
  531.   inferior_ptid = do_attach (pid_to_ptid (pid));
  532.   inf = current_inferior ();
  533.   inferior_appeared (inf, pid);
  534.   inf->attach_flag = 1;

  535.   if (!target_is_pushed (ops))
  536.     push_target (ops);

  537.   procfs_update_thread_list (ops);
  538. }

  539. static void
  540. procfs_post_attach (struct target_ops *self, pid_t pid)
  541. {
  542.   if (exec_bfd)
  543.     solib_create_inferior_hook (0);
  544. }

  545. static ptid_t
  546. do_attach (ptid_t ptid)
  547. {
  548.   procfs_status status;
  549.   struct sigevent event;
  550.   char path[PATH_MAX];

  551.   snprintf (path, PATH_MAX - 1, "%s/%d/as", nto_procfs_path,
  552.             ptid_get_pid (ptid));
  553.   ctl_fd = open (path, O_RDWR);
  554.   if (ctl_fd == -1)
  555.     error (_("Couldn't open proc file %s, error %d (%s)"), path, errno,
  556.            safe_strerror (errno));
  557.   if (devctl (ctl_fd, DCMD_PROC_STOP, &status, sizeof (status), 0) != EOK)
  558.     error (_("Couldn't stop process"));

  559.   /* Define a sigevent for process stopped notification.  */
  560.   event.sigev_notify = SIGEV_SIGNAL_THREAD;
  561.   event.sigev_signo = SIGUSR1;
  562.   event.sigev_code = 0;
  563.   event.sigev_value.sival_ptr = NULL;
  564.   event.sigev_priority = -1;
  565.   devctl (ctl_fd, DCMD_PROC_EVENT, &event, sizeof (event), 0);

  566.   if (devctl (ctl_fd, DCMD_PROC_STATUS, &status, sizeof (status), 0) == EOK
  567.       && status.flags & _DEBUG_FLAG_STOPPED)
  568.     SignalKill (nto_node (), ptid_get_pid (ptid), 0, SIGCONT, 0, 0);
  569.   nto_init_solib_absolute_prefix ();
  570.   return ptid_build (ptid_get_pid (ptid), 0, status.tid);
  571. }

  572. /* Ask the user what to do when an interrupt is received.  */
  573. static void
  574. interrupt_query (void)
  575. {
  576.   target_terminal_ours ();

  577.   if (query (_("Interrupted while waiting for the program.\n\
  578. Give up (and stop debugging it)? ")))
  579.     {
  580.       target_mourn_inferior ();
  581.       quit ();
  582.     }

  583.   target_terminal_inferior ();
  584. }

  585. /* The user typed ^C twice.  */
  586. static void
  587. nto_interrupt_twice (int signo)
  588. {
  589.   signal (signo, ofunc);
  590.   interrupt_query ();
  591.   signal (signo, nto_interrupt_twice);
  592. }

  593. static void
  594. nto_interrupt (int signo)
  595. {
  596.   /* If this doesn't work, try more severe steps.  */
  597.   signal (signo, nto_interrupt_twice);

  598.   target_stop (inferior_ptid);
  599. }

  600. static ptid_t
  601. procfs_wait (struct target_ops *ops,
  602.              ptid_t ptid, struct target_waitstatus *ourstatus, int options)
  603. {
  604.   sigset_t set;
  605.   siginfo_t info;
  606.   procfs_status status;
  607.   static int exit_signo = 0;        /* To track signals that cause termination.  */

  608.   ourstatus->kind = TARGET_WAITKIND_SPURIOUS;

  609.   if (ptid_equal (inferior_ptid, null_ptid))
  610.     {
  611.       ourstatus->kind = TARGET_WAITKIND_STOPPED;
  612.       ourstatus->value.sig = GDB_SIGNAL_0;
  613.       exit_signo = 0;
  614.       return null_ptid;
  615.     }

  616.   sigemptyset (&set);
  617.   sigaddset (&set, SIGUSR1);

  618.   devctl (ctl_fd, DCMD_PROC_STATUS, &status, sizeof (status), 0);
  619.   while (!(status.flags & _DEBUG_FLAG_ISTOP))
  620.     {
  621.       ofunc = (void (*)()) signal (SIGINT, nto_interrupt);
  622.       sigwaitinfo (&set, &info);
  623.       signal (SIGINT, ofunc);
  624.       devctl (ctl_fd, DCMD_PROC_STATUS, &status, sizeof (status), 0);
  625.     }

  626.   if (status.flags & _DEBUG_FLAG_SSTEP)
  627.     {
  628.       ourstatus->kind = TARGET_WAITKIND_STOPPED;
  629.       ourstatus->value.sig = GDB_SIGNAL_TRAP;
  630.     }
  631.   /* Was it a breakpoint?  */
  632.   else if (status.flags & _DEBUG_FLAG_TRACE)
  633.     {
  634.       ourstatus->kind = TARGET_WAITKIND_STOPPED;
  635.       ourstatus->value.sig = GDB_SIGNAL_TRAP;
  636.     }
  637.   else if (status.flags & _DEBUG_FLAG_ISTOP)
  638.     {
  639.       switch (status.why)
  640.         {
  641.         case _DEBUG_WHY_SIGNALLED:
  642.           ourstatus->kind = TARGET_WAITKIND_STOPPED;
  643.           ourstatus->value.sig =
  644.             gdb_signal_from_host (status.info.si_signo);
  645.           exit_signo = 0;
  646.           break;
  647.         case _DEBUG_WHY_FAULTED:
  648.           ourstatus->kind = TARGET_WAITKIND_STOPPED;
  649.           if (status.info.si_signo == SIGTRAP)
  650.             {
  651.               ourstatus->value.sig = 0;
  652.               exit_signo = 0;
  653.             }
  654.           else
  655.             {
  656.               ourstatus->value.sig =
  657.                 gdb_signal_from_host (status.info.si_signo);
  658.               exit_signo = ourstatus->value.sig;
  659.             }
  660.           break;

  661.         case _DEBUG_WHY_TERMINATED:
  662.           {
  663.             int waitval = 0;

  664.             waitpid (ptid_get_pid (inferior_ptid), &waitval, WNOHANG);
  665.             if (exit_signo)
  666.               {
  667.                 /* Abnormal death.  */
  668.                 ourstatus->kind = TARGET_WAITKIND_SIGNALLED;
  669.                 ourstatus->value.sig = exit_signo;
  670.               }
  671.             else
  672.               {
  673.                 /* Normal death.  */
  674.                 ourstatus->kind = TARGET_WAITKIND_EXITED;
  675.                 ourstatus->value.integer = WEXITSTATUS (waitval);
  676.               }
  677.             exit_signo = 0;
  678.             break;
  679.           }

  680.         case _DEBUG_WHY_REQUESTED:
  681.           /* We are assuming a requested stop is due to a SIGINT.  */
  682.           ourstatus->kind = TARGET_WAITKIND_STOPPED;
  683.           ourstatus->value.sig = GDB_SIGNAL_INT;
  684.           exit_signo = 0;
  685.           break;
  686.         }
  687.     }

  688.   return ptid_build (status.pid, 0, status.tid);
  689. }

  690. /* Read the current values of the inferior's registers, both the
  691.    general register set and floating point registers (if supported)
  692.    and update gdb's idea of their current values.  */
  693. static void
  694. procfs_fetch_registers (struct target_ops *ops,
  695.                         struct regcache *regcache, int regno)
  696. {
  697.   union
  698.   {
  699.     procfs_greg greg;
  700.     procfs_fpreg fpreg;
  701.     procfs_altreg altreg;
  702.   }
  703.   reg;
  704.   int regsize;

  705.   procfs_set_thread (inferior_ptid);
  706.   if (devctl (ctl_fd, DCMD_PROC_GETGREG, &reg, sizeof (reg), &regsize) == EOK)
  707.     nto_supply_gregset (regcache, (char *) &reg.greg);
  708.   if (devctl (ctl_fd, DCMD_PROC_GETFPREG, &reg, sizeof (reg), &regsize)
  709.       == EOK)
  710.     nto_supply_fpregset (regcache, (char *) &reg.fpreg);
  711.   if (devctl (ctl_fd, DCMD_PROC_GETALTREG, &reg, sizeof (reg), &regsize)
  712.       == EOK)
  713.     nto_supply_altregset (regcache, (char *) &reg.altreg);
  714. }

  715. /* Helper for procfs_xfer_partial that handles memory transfers.
  716.    Arguments are like target_xfer_partial.  */

  717. static enum target_xfer_status
  718. procfs_xfer_memory (gdb_byte *readbuf, const gdb_byte *writebuf,
  719.                     ULONGEST memaddr, ULONGEST len, ULONGEST *xfered_len)
  720. {
  721.   int nbytes;

  722.   if (lseek (ctl_fd, (off_t) memaddr, SEEK_SET) != (off_t) memaddr)
  723.     return TARGET_XFER_E_IO;

  724.   if (writebuf != NULL)
  725.     nbytes = write (ctl_fd, writebuf, len);
  726.   else
  727.     nbytes = read (ctl_fd, readbuf, len);
  728.   if (nbytes <= 0)
  729.     return TARGET_XFER_E_IO;
  730.   *xfered_len = nbytes;
  731.   return TARGET_XFER_OK;
  732. }

  733. /* Target to_xfer_partial implementation.  */

  734. static enum target_xfer_status
  735. procfs_xfer_partial (struct target_ops *ops, enum target_object object,
  736.                      const char *annex, gdb_byte *readbuf,
  737.                      const gdb_byte *writebuf, ULONGEST offset, ULONGEST len,
  738.                      ULONGEST *xfered_len)
  739. {
  740.   switch (object)
  741.     {
  742.     case TARGET_OBJECT_MEMORY:
  743.       return procfs_xfer_memory (readbuf, writebuf, offset, len, xfered_len);
  744.     default:
  745.       return ops->beneath->to_xfer_partial (ops->beneath, object, annex,
  746.                                             readbuf, writebuf, offset, len);
  747.     }
  748. }

  749. /* Take a program previously attached to and detaches it.
  750.    The program resumes execution and will no longer stop
  751.    on signals, etc.  We'd better not have left any breakpoints
  752.    in the program or it'll die when it hits one.  */
  753. static void
  754. procfs_detach (struct target_ops *ops, const char *args, int from_tty)
  755. {
  756.   int siggnal = 0;
  757.   int pid;

  758.   if (from_tty)
  759.     {
  760.       char *exec_file = get_exec_file (0);
  761.       if (exec_file == 0)
  762.         exec_file = "";
  763.       printf_unfiltered ("Detaching from program: %s %s\n",
  764.                          exec_file, target_pid_to_str (inferior_ptid));
  765.       gdb_flush (gdb_stdout);
  766.     }
  767.   if (args)
  768.     siggnal = atoi (args);

  769.   if (siggnal)
  770.     SignalKill (nto_node (), ptid_get_pid (inferior_ptid), 0, siggnal, 0, 0);

  771.   close (ctl_fd);
  772.   ctl_fd = -1;

  773.   pid = ptid_get_pid (inferior_ptid);
  774.   inferior_ptid = null_ptid;
  775.   detach_inferior (pid);
  776.   init_thread_list ();
  777.   inf_child_maybe_unpush_target (ops);
  778. }

  779. static int
  780. procfs_breakpoint (CORE_ADDR addr, int type, int size)
  781. {
  782.   procfs_break brk;

  783.   brk.type = type;
  784.   brk.addr = addr;
  785.   brk.size = size;
  786.   errno = devctl (ctl_fd, DCMD_PROC_BREAK, &brk, sizeof (brk), 0);
  787.   if (errno != EOK)
  788.     return 1;
  789.   return 0;
  790. }

  791. static int
  792. procfs_insert_breakpoint (struct target_ops *ops, struct gdbarch *gdbarch,
  793.                           struct bp_target_info *bp_tgt)
  794. {
  795.   bp_tgt->placed_address = bp_tgt->reqstd_address;
  796.   return procfs_breakpoint (bp_tgt->placed_address, _DEBUG_BREAK_EXEC, 0);
  797. }

  798. static int
  799. procfs_remove_breakpoint (struct target_ops *ops, struct gdbarch *gdbarch,
  800.                           struct bp_target_info *bp_tgt)
  801. {
  802.   return procfs_breakpoint (bp_tgt->placed_address, _DEBUG_BREAK_EXEC, -1);
  803. }

  804. static int
  805. procfs_insert_hw_breakpoint (struct target_ops *self, struct gdbarch *gdbarch,
  806.                              struct bp_target_info *bp_tgt)
  807. {
  808.   bp_tgt->placed_address = bp_tgt->reqstd_address;
  809.   return procfs_breakpoint (bp_tgt->placed_address,
  810.                             _DEBUG_BREAK_EXEC | _DEBUG_BREAK_HW, 0);
  811. }

  812. static int
  813. procfs_remove_hw_breakpoint (struct target_ops *self,
  814.                              struct gdbarch *gdbarch,
  815.                              struct bp_target_info *bp_tgt)
  816. {
  817.   return procfs_breakpoint (bp_tgt->placed_address,
  818.                             _DEBUG_BREAK_EXEC | _DEBUG_BREAK_HW, -1);
  819. }

  820. static void
  821. procfs_resume (struct target_ops *ops,
  822.                ptid_t ptid, int step, enum gdb_signal signo)
  823. {
  824.   int signal_to_pass;
  825.   procfs_status status;
  826.   sigset_t *run_fault = (sigset_t *) (void *) &run.fault;

  827.   if (ptid_equal (inferior_ptid, null_ptid))
  828.     return;

  829.   procfs_set_thread (ptid_equal (ptid, minus_one_ptid) ? inferior_ptid :
  830.                      ptid);

  831.   run.flags = _DEBUG_RUN_FAULT | _DEBUG_RUN_TRACE;
  832.   if (step)
  833.     run.flags |= _DEBUG_RUN_STEP;

  834.   sigemptyset (run_fault);
  835.   sigaddset (run_fault, FLTBPT);
  836.   sigaddset (run_fault, FLTTRACE);
  837.   sigaddset (run_fault, FLTILL);
  838.   sigaddset (run_fault, FLTPRIV);
  839.   sigaddset (run_fault, FLTBOUNDS);
  840.   sigaddset (run_fault, FLTIOVF);
  841.   sigaddset (run_fault, FLTIZDIV);
  842.   sigaddset (run_fault, FLTFPE);
  843.   /* Peter V will be changing this at some point.  */
  844.   sigaddset (run_fault, FLTPAGE);

  845.   run.flags |= _DEBUG_RUN_ARM;

  846.   signal_to_pass = gdb_signal_to_host (signo);

  847.   if (signal_to_pass)
  848.     {
  849.       devctl (ctl_fd, DCMD_PROC_STATUS, &status, sizeof (status), 0);
  850.       signal_to_pass = gdb_signal_to_host (signo);
  851.       if (status.why & (_DEBUG_WHY_SIGNALLED | _DEBUG_WHY_FAULTED))
  852.         {
  853.           if (signal_to_pass != status.info.si_signo)
  854.             {
  855.               SignalKill (nto_node (), ptid_get_pid (inferior_ptid), 0,
  856.                           signal_to_pass, 0, 0);
  857.               run.flags |= _DEBUG_RUN_CLRFLT | _DEBUG_RUN_CLRSIG;
  858.             }
  859.           else                /* Let it kill the program without telling us.  */
  860.             sigdelset (&run.trace, signal_to_pass);
  861.         }
  862.     }
  863.   else
  864.     run.flags |= _DEBUG_RUN_CLRSIG | _DEBUG_RUN_CLRFLT;

  865.   errno = devctl (ctl_fd, DCMD_PROC_RUN, &run, sizeof (run), 0);
  866.   if (errno != EOK)
  867.     {
  868.       perror (_("run error!\n"));
  869.       return;
  870.     }
  871. }

  872. static void
  873. procfs_mourn_inferior (struct target_ops *ops)
  874. {
  875.   if (!ptid_equal (inferior_ptid, null_ptid))
  876.     {
  877.       SignalKill (nto_node (), ptid_get_pid (inferior_ptid), 0, SIGKILL, 0, 0);
  878.       close (ctl_fd);
  879.     }
  880.   inferior_ptid = null_ptid;
  881.   init_thread_list ();
  882.   inf_child_mourn_inferior (ops);
  883. }

  884. /* This function breaks up an argument string into an argument
  885.    vector suitable for passing to execvp().
  886.    E.g., on "run a b c d" this routine would get as input
  887.    the string "a b c d", and as output it would fill in argv with
  888.    the four arguments "a", "b", "c", "d".  The only additional
  889.    functionality is simple quoting.  The gdb command:
  890.           run a "b c d" f
  891.    will fill in argv with the three args "a", "b c d", "e".  */
  892. static void
  893. breakup_args (char *scratch, char **argv)
  894. {
  895.   char *pp, *cp = scratch;
  896.   char quoting = 0;

  897.   for (;;)
  898.     {
  899.       /* Scan past leading separators.  */
  900.       quoting = 0;
  901.       while (*cp == ' ' || *cp == '\t' || *cp == '\n')
  902.         cp++;

  903.       /* Break if at end of string.  */
  904.       if (*cp == '\0')
  905.         break;

  906.       /* Take an arg.  */
  907.       if (*cp == '"')
  908.         {
  909.           cp++;
  910.           quoting = strchr (cp, '"') ? 1 : 0;
  911.         }

  912.       *argv++ = cp;

  913.       /* Scan for next arg separator.  */
  914.       pp = cp;
  915.       if (quoting)
  916.         cp = strchr (pp, '"');
  917.       if ((cp == NULL) || (!quoting))
  918.         cp = strchr (pp, ' ');
  919.       if (cp == NULL)
  920.         cp = strchr (pp, '\t');
  921.       if (cp == NULL)
  922.         cp = strchr (pp, '\n');

  923.       /* No separators => end of string => break.  */
  924.       if (cp == NULL)
  925.         {
  926.           pp = cp;
  927.           break;
  928.         }

  929.       /* Replace the separator with a terminator.  */
  930.       *cp++ = '\0';
  931.     }

  932.   /* Execv requires a null-terminated arg vector.  */
  933.   *argv = NULL;
  934. }

  935. static void
  936. procfs_create_inferior (struct target_ops *ops, char *exec_file,
  937.                         char *allargs, char **env, int from_tty)
  938. {
  939.   struct inheritance inherit;
  940.   pid_t pid;
  941.   int flags, errn;
  942.   char **argv, *args;
  943.   const char *in = "", *out = "", *err = "";
  944.   int fd, fds[3];
  945.   sigset_t set;
  946.   const char *inferior_io_terminal = get_inferior_io_terminal ();
  947.   struct inferior *inf;

  948.   argv = xmalloc (((strlen (allargs) + 1) / (unsigned) 2 + 2) *
  949.                   sizeof (*argv));
  950.   argv[0] = get_exec_file (1);
  951.   if (!argv[0])
  952.     {
  953.       if (exec_file)
  954.         argv[0] = exec_file;
  955.       else
  956.         return;
  957.     }

  958.   args = xstrdup (allargs);
  959.   breakup_args (args, exec_file ? &argv[1] : &argv[0]);

  960.   argv = nto_parse_redirection (argv, &in, &out, &err);

  961.   fds[0] = STDIN_FILENO;
  962.   fds[1] = STDOUT_FILENO;
  963.   fds[2] = STDERR_FILENO;

  964.   /* If the user specified I/O via gdb's --tty= arg, use it, but only
  965.      if the i/o is not also being specified via redirection.  */
  966.   if (inferior_io_terminal)
  967.     {
  968.       if (!in[0])
  969.         in = inferior_io_terminal;
  970.       if (!out[0])
  971.         out = inferior_io_terminal;
  972.       if (!err[0])
  973.         err = inferior_io_terminal;
  974.     }

  975.   if (in[0])
  976.     {
  977.       fd = open (in, O_RDONLY);
  978.       if (fd == -1)
  979.         perror (in);
  980.       else
  981.         fds[0] = fd;
  982.     }
  983.   if (out[0])
  984.     {
  985.       fd = open (out, O_WRONLY);
  986.       if (fd == -1)
  987.         perror (out);
  988.       else
  989.         fds[1] = fd;
  990.     }
  991.   if (err[0])
  992.     {
  993.       fd = open (err, O_WRONLY);
  994.       if (fd == -1)
  995.         perror (err);
  996.       else
  997.         fds[2] = fd;
  998.     }

  999.   /* Clear any pending SIGUSR1's but keep the behavior the same.  */
  1000.   signal (SIGUSR1, signal (SIGUSR1, SIG_IGN));

  1001.   sigemptyset (&set);
  1002.   sigaddset (&set, SIGUSR1);
  1003.   sigprocmask (SIG_UNBLOCK, &set, NULL);

  1004.   memset (&inherit, 0, sizeof (inherit));

  1005.   if (ND_NODE_CMP (nto_procfs_node, ND_LOCAL_NODE) != 0)
  1006.     {
  1007.       inherit.nd = nto_node ();
  1008.       inherit.flags |= SPAWN_SETND;
  1009.       inherit.flags &= ~SPAWN_EXEC;
  1010.     }
  1011.   inherit.flags |= SPAWN_SETGROUP | SPAWN_HOLD;
  1012.   inherit.pgroup = SPAWN_NEWPGROUP;
  1013.   pid = spawnp (argv[0], 3, fds, &inherit, argv,
  1014.                 ND_NODE_CMP (nto_procfs_node, ND_LOCAL_NODE) == 0 ? env : 0);
  1015.   xfree (args);

  1016.   sigprocmask (SIG_BLOCK, &set, NULL);

  1017.   if (pid == -1)
  1018.     error (_("Error spawning %s: %d (%s)"), argv[0], errno,
  1019.            safe_strerror (errno));

  1020.   if (fds[0] != STDIN_FILENO)
  1021.     close (fds[0]);
  1022.   if (fds[1] != STDOUT_FILENO)
  1023.     close (fds[1]);
  1024.   if (fds[2] != STDERR_FILENO)
  1025.     close (fds[2]);

  1026.   inferior_ptid = do_attach (pid_to_ptid (pid));
  1027.   procfs_update_thread_list (ops);

  1028.   inf = current_inferior ();
  1029.   inferior_appeared (inf, pid);
  1030.   inf->attach_flag = 0;

  1031.   flags = _DEBUG_FLAG_KLC;        /* Kill-on-Last-Close flag.  */
  1032.   errn = devctl (ctl_fd, DCMD_PROC_SET_FLAG, &flags, sizeof (flags), 0);
  1033.   if (errn != EOK)
  1034.     {
  1035.       /* FIXME: expected warning?  */
  1036.       /* warning( "Failed to set Kill-on-Last-Close flag: errno = %d(%s)\n",
  1037.          errn, strerror(errn) ); */
  1038.     }
  1039.   if (!target_is_pushed (ops))
  1040.     push_target (ops);
  1041.   target_terminal_init ();

  1042.   if (exec_bfd != NULL
  1043.       || (symfile_objfile != NULL && symfile_objfile->obfd != NULL))
  1044.     solib_create_inferior_hook (0);
  1045. }

  1046. static void
  1047. procfs_stop (struct target_ops *self, ptid_t ptid)
  1048. {
  1049.   devctl (ctl_fd, DCMD_PROC_STOP, NULL, 0, 0);
  1050. }

  1051. static void
  1052. procfs_kill_inferior (struct target_ops *ops)
  1053. {
  1054.   target_mourn_inferior ();
  1055. }

  1056. /* Fill buf with regset and return devctl cmd to do the setting.  Return
  1057.    -1 if we fail to get the regset.  Store size of regset in regsize.  */
  1058. static int
  1059. get_regset (int regset, char *buf, int bufsize, int *regsize)
  1060. {
  1061.   int dev_get, dev_set;
  1062.   switch (regset)
  1063.     {
  1064.     case NTO_REG_GENERAL:
  1065.       dev_get = DCMD_PROC_GETGREG;
  1066.       dev_set = DCMD_PROC_SETGREG;
  1067.       break;

  1068.     case NTO_REG_FLOAT:
  1069.       dev_get = DCMD_PROC_GETFPREG;
  1070.       dev_set = DCMD_PROC_SETFPREG;
  1071.       break;

  1072.     case NTO_REG_ALT:
  1073.       dev_get = DCMD_PROC_GETALTREG;
  1074.       dev_set = DCMD_PROC_SETALTREG;
  1075.       break;

  1076.     case NTO_REG_SYSTEM:
  1077.     default:
  1078.       return -1;
  1079.     }
  1080.   if (devctl (ctl_fd, dev_get, buf, bufsize, regsize) != EOK)
  1081.     return -1;

  1082.   return dev_set;
  1083. }

  1084. void
  1085. procfs_store_registers (struct target_ops *ops,
  1086.                         struct regcache *regcache, int regno)
  1087. {
  1088.   union
  1089.   {
  1090.     procfs_greg greg;
  1091.     procfs_fpreg fpreg;
  1092.     procfs_altreg altreg;
  1093.   }
  1094.   reg;
  1095.   unsigned off;
  1096.   int len, regset, regsize, dev_set, err;
  1097.   char *data;

  1098.   if (ptid_equal (inferior_ptid, null_ptid))
  1099.     return;
  1100.   procfs_set_thread (inferior_ptid);

  1101.   if (regno == -1)
  1102.     {
  1103.       for (regset = NTO_REG_GENERAL; regset < NTO_REG_END; regset++)
  1104.         {
  1105.           dev_set = get_regset (regset, (char *) &reg,
  1106.                                 sizeof (reg), &regsize);
  1107.           if (dev_set == -1)
  1108.             continue;

  1109.           if (nto_regset_fill (regcache, regset, (char *) &reg) == -1)
  1110.             continue;

  1111.           err = devctl (ctl_fd, dev_set, &reg, regsize, 0);
  1112.           if (err != EOK)
  1113.             fprintf_unfiltered (gdb_stderr,
  1114.                                 "Warning unable to write regset %d: %s\n",
  1115.                                 regno, safe_strerror (err));
  1116.         }
  1117.     }
  1118.   else
  1119.     {
  1120.       regset = nto_regset_id (regno);
  1121.       if (regset == -1)
  1122.         return;

  1123.       dev_set = get_regset (regset, (char *) &reg, sizeof (reg), &regsize);
  1124.       if (dev_set == -1)
  1125.         return;

  1126.       len = nto_register_area (get_regcache_arch (regcache),
  1127.                                regno, regset, &off);

  1128.       if (len < 1)
  1129.         return;

  1130.       regcache_raw_collect (regcache, regno, (char *) &reg + off);

  1131.       err = devctl (ctl_fd, dev_set, &reg, regsize, 0);
  1132.       if (err != EOK)
  1133.         fprintf_unfiltered (gdb_stderr,
  1134.                             "Warning unable to write regset %d: %s\n", regno,
  1135.                             safe_strerror (err));
  1136.     }
  1137. }

  1138. /* Set list of signals to be handled in the target.  */

  1139. static void
  1140. procfs_pass_signals (struct target_ops *self,
  1141.                      int numsigs, unsigned char *pass_signals)
  1142. {
  1143.   int signo;

  1144.   sigfillset (&run.trace);

  1145.   for (signo = 1; signo < NSIG; signo++)
  1146.     {
  1147.       int target_signo = gdb_signal_from_host (signo);
  1148.       if (target_signo < numsigs && pass_signals[target_signo])
  1149.         sigdelset (&run.trace, signo);
  1150.     }
  1151. }

  1152. static struct tidinfo *
  1153. procfs_thread_info (pid_t pid, short tid)
  1154. {
  1155. /* NYI */
  1156.   return NULL;
  1157. }

  1158. static char *
  1159. procfs_pid_to_str (struct target_ops *ops, ptid_t ptid)
  1160. {
  1161.   static char buf[1024];
  1162.   int pid, tid, n;
  1163.   struct tidinfo *tip;

  1164.   pid = ptid_get_pid (ptid);
  1165.   tid = ptid_get_tid (ptid);

  1166.   n = snprintf (buf, 1023, "process %d", pid);

  1167. #if 0                                /* NYI */
  1168.   tip = procfs_thread_info (pid, tid);
  1169.   if (tip != NULL)
  1170.     snprintf (&buf[n], 1023, " (state = 0x%02x)", tip->state);
  1171. #endif

  1172.   return buf;
  1173. }

  1174. /* to_can_run implementation for "target procfs".  Note this really
  1175.   means "can this target be the default run target", which there can
  1176.   be only one, and we make it be "target native" like other ports.
  1177.   "target procfs <node>" wouldn't make sense as default run target, as
  1178.   it needs <node>.  */

  1179. static int
  1180. procfs_can_run (struct target_ops *self)
  1181. {
  1182.   return 0;
  1183. }

  1184. /* "target procfs".  */
  1185. static struct target_ops nto_procfs_ops;

  1186. /* "target native".  */
  1187. static struct target_ops *nto_native_ops;

  1188. /* to_open implementation for "target procfs".  */

  1189. static void
  1190. procfs_open (const char *arg, int from_tty)
  1191. {
  1192.   procfs_open_1 (&nto_procfs_ops, arg, from_tty);
  1193. }

  1194. /* to_open implementation for "target native".  */

  1195. static void
  1196. procfs_native_open (const char *arg, int from_tty)
  1197. {
  1198.   procfs_open_1 (nto_native_ops, arg, from_tty);
  1199. }

  1200. /* Create the "native" and "procfs" targets.  */

  1201. static void
  1202. init_procfs_targets (void)
  1203. {
  1204.   struct target_ops *t = inf_child_target ();

  1205.   /* Leave to_shortname as "native".  */
  1206.   t->to_longname = "QNX Neutrino local process";
  1207.   t->to_doc = "QNX Neutrino local process (started by the \"run\" command).";
  1208.   t->to_open = procfs_native_open;
  1209.   t->to_attach = procfs_attach;
  1210.   t->to_post_attach = procfs_post_attach;
  1211.   t->to_detach = procfs_detach;
  1212.   t->to_resume = procfs_resume;
  1213.   t->to_wait = procfs_wait;
  1214.   t->to_fetch_registers = procfs_fetch_registers;
  1215.   t->to_store_registers = procfs_store_registers;
  1216.   t->to_xfer_partial = procfs_xfer_partial;
  1217.   t->to_files_info = procfs_files_info;
  1218.   t->to_insert_breakpoint = procfs_insert_breakpoint;
  1219.   t->to_remove_breakpoint = procfs_remove_breakpoint;
  1220.   t->to_can_use_hw_breakpoint = procfs_can_use_hw_breakpoint;
  1221.   t->to_insert_hw_breakpoint = procfs_insert_hw_breakpoint;
  1222.   t->to_remove_hw_breakpoint = procfs_remove_hw_breakpoint;
  1223.   t->to_insert_watchpoint = procfs_insert_hw_watchpoint;
  1224.   t->to_remove_watchpoint = procfs_remove_hw_watchpoint;
  1225.   t->to_stopped_by_watchpoint = procfs_stopped_by_watchpoint;
  1226.   t->to_kill = procfs_kill_inferior;
  1227.   t->to_create_inferior = procfs_create_inferior;
  1228.   t->to_mourn_inferior = procfs_mourn_inferior;
  1229.   t->to_pass_signals = procfs_pass_signals;
  1230.   t->to_thread_alive = procfs_thread_alive;
  1231.   t->to_update_thread_list = procfs_update_thread_list;
  1232.   t->to_pid_to_str = procfs_pid_to_str;
  1233.   t->to_stop = procfs_stop;
  1234.   t->to_have_continuable_watchpoint = 1;
  1235.   t->to_extra_thread_info = nto_extra_thread_info;

  1236.   nto_native_ops = t;

  1237.   /* Register "target native".  This is the default run target.  */
  1238.   add_target (t);

  1239.   /* Register "target procfs <node>".  */
  1240.   nto_procfs_ops = *t;
  1241.   nto_procfs_ops.to_shortname = "procfs";
  1242.   nto_procfs_ops.to_can_run = procfs_can_run;
  1243.   t->to_longname = "QNX Neutrino local or remote process";
  1244.   t->to_doc = "QNX Neutrino process.  target procfs <node>";
  1245.   t->to_open = procfs_open;

  1246.   add_target (&nto_procfs_ops);
  1247. }

  1248. #define OSTYPE_NTO 1

  1249. void
  1250. _initialize_procfs (void)
  1251. {
  1252.   sigset_t set;

  1253.   init_procfs_targets ();

  1254.   /* We use SIGUSR1 to gain control after we block waiting for a process.
  1255.      We use sigwaitevent to wait.  */
  1256.   sigemptyset (&set);
  1257.   sigaddset (&set, SIGUSR1);
  1258.   sigprocmask (SIG_BLOCK, &set, NULL);

  1259.   /* Initially, make sure all signals are reported.  */
  1260.   sigfillset (&run.trace);

  1261.   /* Stuff some information.  */
  1262.   nto_cpuinfo_flags = SYSPAGE_ENTRY (cpuinfo)->flags;
  1263.   nto_cpuinfo_valid = 1;

  1264.   add_info ("pidlist", procfs_pidlist, _("pidlist"));
  1265.   add_info ("meminfo", procfs_meminfo, _("memory information"));

  1266.   nto_is_nto_target = procfs_is_nto_target;
  1267. }


  1268. static int
  1269. procfs_hw_watchpoint (int addr, int len, int type)
  1270. {
  1271.   procfs_break brk;

  1272.   switch (type)
  1273.     {
  1274.     case 1:                        /* Read.  */
  1275.       brk.type = _DEBUG_BREAK_RD;
  1276.       break;
  1277.     case 2:                        /* Read/Write.  */
  1278.       brk.type = _DEBUG_BREAK_RW;
  1279.       break;
  1280.     default:                        /* Modify.  */
  1281. /* FIXME: brk.type = _DEBUG_BREAK_RWM gives EINVAL for some reason.  */
  1282.       brk.type = _DEBUG_BREAK_RW;
  1283.     }
  1284.   brk.type |= _DEBUG_BREAK_HW;        /* Always ask for HW.  */
  1285.   brk.addr = addr;
  1286.   brk.size = len;

  1287.   errno = devctl (ctl_fd, DCMD_PROC_BREAK, &brk, sizeof (brk), 0);
  1288.   if (errno != EOK)
  1289.     {
  1290.       perror (_("Failed to set hardware watchpoint"));
  1291.       return -1;
  1292.     }
  1293.   return 0;
  1294. }

  1295. static int
  1296. procfs_can_use_hw_breakpoint (struct target_ops *self,
  1297.                               int type, int cnt, int othertype)
  1298. {
  1299.   return 1;
  1300. }

  1301. static int
  1302. procfs_remove_hw_watchpoint (struct target_ops *self,
  1303.                              CORE_ADDR addr, int len, int type,
  1304.                              struct expression *cond)
  1305. {
  1306.   return procfs_hw_watchpoint (addr, -1, type);
  1307. }

  1308. static int
  1309. procfs_insert_hw_watchpoint (struct target_ops *self,
  1310.                              CORE_ADDR addr, int len, int type,
  1311.                              struct expression *cond)
  1312. {
  1313.   return procfs_hw_watchpoint (addr, len, type);
  1314. }

  1315. static int
  1316. procfs_stopped_by_watchpoint (struct target_ops *ops)
  1317. {
  1318.   return 0;
  1319. }