fix memory errors with demangled name hash
[platform/upstream/binutils.git] / gdb / nto-procfs.c
1 /* Machine independent support for QNX Neutrino /proc (process file system)
2    for GDB.  Written by Colin Burgess at QNX Software Systems Limited.
3
4    Copyright (C) 2003-2014 Free Software Foundation, Inc.
5
6    Contributed by QNX Software Systems Ltd.
7
8    This file is part of GDB.
9
10    This program is free software; you can redistribute it and/or modify
11    it under the terms of the GNU General Public License as published by
12    the Free Software Foundation; either version 3 of the License, or
13    (at your option) any later version.
14
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License for more details.
19
20    You should have received a copy of the GNU General Public License
21    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
22
23 #include "defs.h"
24
25 #include <fcntl.h>
26 #include <spawn.h>
27 #include <sys/debug.h>
28 #include <sys/procfs.h>
29 #include <sys/neutrino.h>
30 #include <sys/syspage.h>
31 #include <dirent.h>
32 #include <sys/netmgr.h>
33
34 #include "exceptions.h"
35 #include <string.h>
36 #include "gdbcore.h"
37 #include "inferior.h"
38 #include "target.h"
39 #include "objfiles.h"
40 #include "gdbthread.h"
41 #include "nto-tdep.h"
42 #include "command.h"
43 #include "regcache.h"
44 #include "solib.h"
45 #include "inf-child.h"
46
47 #define NULL_PID                0
48 #define _DEBUG_FLAG_TRACE       (_DEBUG_FLAG_TRACE_EXEC|_DEBUG_FLAG_TRACE_RD|\
49                 _DEBUG_FLAG_TRACE_WR|_DEBUG_FLAG_TRACE_MODIFY)
50
51 int ctl_fd;
52
53 static void (*ofunc) ();
54
55 static procfs_run run;
56
57 static ptid_t do_attach (ptid_t ptid);
58
59 static int procfs_can_use_hw_breakpoint (struct target_ops *self,
60                                          int, int, int);
61
62 static int procfs_insert_hw_watchpoint (struct target_ops *self,
63                                         CORE_ADDR addr, int len, int type,
64                                         struct expression *cond);
65
66 static int procfs_remove_hw_watchpoint (struct target_ops *self,
67                                         CORE_ADDR addr, int len, int type,
68                                         struct expression *cond);
69
70 static int procfs_stopped_by_watchpoint (struct target_ops *ops);
71
72 /* These two globals are only ever set in procfs_open_1, but are
73    referenced elsewhere.  'nto_procfs_node' is a flag used to say
74    whether we are local, or we should get the current node descriptor
75    for the remote QNX node.  */
76 static char nto_procfs_path[PATH_MAX] = { "/proc" };
77 static unsigned nto_procfs_node = ND_LOCAL_NODE;
78
79 /* Return the current QNX Node, or error out.  This is a simple
80    wrapper for the netmgr_strtond() function.  The reason this
81    is required is because QNX node descriptors are transient so
82    we have to re-acquire them every time.  */
83 static unsigned
84 nto_node (void)
85 {
86   unsigned node;
87
88   if (ND_NODE_CMP (nto_procfs_node, ND_LOCAL_NODE) == 0)
89     return ND_LOCAL_NODE;
90
91   node = netmgr_strtond (nto_procfs_path, 0);
92   if (node == -1)
93     error (_("Lost the QNX node.  Debug session probably over."));
94
95   return (node);
96 }
97
98 static enum gdb_osabi
99 procfs_is_nto_target (bfd *abfd)
100 {
101   return GDB_OSABI_QNXNTO;
102 }
103
104 /* This is called when we call 'target native' or 'target procfs
105    <arg>' from the (gdb) prompt.  For QNX6 (nto), the only valid arg
106    will be a QNX node string, eg: "/net/some_node".  If arg is not a
107    valid QNX node, we will default to local.  */
108 static void
109 procfs_open_1 (struct target_ops *ops, char *arg, int from_tty)
110 {
111   char *nodestr;
112   char *endstr;
113   char buffer[50];
114   int fd, total_size;
115   procfs_sysinfo *sysinfo;
116   struct cleanup *cleanups;
117
118   /* Offer to kill previous inferiors before opening this target.  */
119   target_preopen (from_tty);
120
121   nto_is_nto_target = procfs_is_nto_target;
122
123   /* Set the default node used for spawning to this one,
124      and only override it if there is a valid arg.  */
125
126   nto_procfs_node = ND_LOCAL_NODE;
127   nodestr = arg ? xstrdup (arg) : arg;
128
129   init_thread_list ();
130
131   if (nodestr)
132     {
133       nto_procfs_node = netmgr_strtond (nodestr, &endstr);
134       if (nto_procfs_node == -1)
135         {
136           if (errno == ENOTSUP)
137             printf_filtered ("QNX Net Manager not found.\n");
138           printf_filtered ("Invalid QNX node %s: error %d (%s).\n", nodestr,
139                            errno, safe_strerror (errno));
140           xfree (nodestr);
141           nodestr = NULL;
142           nto_procfs_node = ND_LOCAL_NODE;
143         }
144       else if (*endstr)
145         {
146           if (*(endstr - 1) == '/')
147             *(endstr - 1) = 0;
148           else
149             *endstr = 0;
150         }
151     }
152   snprintf (nto_procfs_path, PATH_MAX - 1, "%s%s", nodestr ? nodestr : "",
153             "/proc");
154   if (nodestr)
155     xfree (nodestr);
156
157   fd = open (nto_procfs_path, O_RDONLY);
158   if (fd == -1)
159     {
160       printf_filtered ("Error opening %s : %d (%s)\n", nto_procfs_path, errno,
161                        safe_strerror (errno));
162       error (_("Invalid procfs arg"));
163     }
164   cleanups = make_cleanup_close (fd);
165
166   sysinfo = (void *) buffer;
167   if (devctl (fd, DCMD_PROC_SYSINFO, sysinfo, sizeof buffer, 0) != EOK)
168     {
169       printf_filtered ("Error getting size: %d (%s)\n", errno,
170                        safe_strerror (errno));
171       error (_("Devctl failed."));
172     }
173   else
174     {
175       total_size = sysinfo->total_size;
176       sysinfo = alloca (total_size);
177       if (!sysinfo)
178         {
179           printf_filtered ("Memory error: %d (%s)\n", errno,
180                            safe_strerror (errno));
181           error (_("alloca failed."));
182         }
183       else
184         {
185           if (devctl (fd, DCMD_PROC_SYSINFO, sysinfo, total_size, 0) != EOK)
186             {
187               printf_filtered ("Error getting sysinfo: %d (%s)\n", errno,
188                                safe_strerror (errno));
189               error (_("Devctl failed."));
190             }
191           else
192             {
193               if (sysinfo->type !=
194                   nto_map_arch_to_cputype (gdbarch_bfd_arch_info
195                                            (target_gdbarch ())->arch_name))
196                 error (_("Invalid target CPU."));
197             }
198         }
199     }
200   do_cleanups (cleanups);
201
202   inf_child_open_target (ops, arg, from_tty);
203   printf_filtered ("Debugging using %s\n", nto_procfs_path);
204 }
205
206 static void
207 procfs_set_thread (ptid_t ptid)
208 {
209   pid_t tid;
210
211   tid = ptid_get_tid (ptid);
212   devctl (ctl_fd, DCMD_PROC_CURTHREAD, &tid, sizeof (tid), 0);
213 }
214
215 /*  Return nonzero if the thread TH is still alive.  */
216 static int
217 procfs_thread_alive (struct target_ops *ops, ptid_t ptid)
218 {
219   pid_t tid;
220   pid_t pid;
221   procfs_status status;
222   int err;
223
224   tid = ptid_get_tid (ptid);
225   pid = ptid_get_pid (ptid);
226
227   if (kill (pid, 0) == -1)
228     return 0;
229
230   status.tid = tid;
231   if ((err = devctl (ctl_fd, DCMD_PROC_TIDSTATUS,
232                      &status, sizeof (status), 0)) != EOK)
233     return 0;
234
235   /* Thread is alive or dead but not yet joined,
236      or dead and there is an alive (or dead unjoined) thread with
237      higher tid.
238
239      If the tid is not the same as requested, requested tid is dead.  */
240   return (status.tid == tid) && (status.state != STATE_DEAD);
241 }
242
243 static void
244 update_thread_private_data_name (struct thread_info *new_thread,
245                                  const char *newname)
246 {
247   int newnamelen;
248   struct private_thread_info *pti;
249
250   gdb_assert (newname != NULL);
251   gdb_assert (new_thread != NULL);
252   newnamelen = strlen (newname);
253   if (!new_thread->private)
254     {
255       new_thread->private = xmalloc (offsetof (struct private_thread_info,
256                                                name)
257                                      + newnamelen + 1);
258       memcpy (new_thread->private->name, newname, newnamelen + 1);
259     }
260   else if (strcmp (newname, new_thread->private->name) != 0)
261     {
262       /* Reallocate if neccessary.  */
263       int oldnamelen = strlen (new_thread->private->name);
264
265       if (oldnamelen < newnamelen)
266         new_thread->private = xrealloc (new_thread->private,
267                                         offsetof (struct private_thread_info,
268                                                   name)
269                                         + newnamelen + 1);
270       memcpy (new_thread->private->name, newname, newnamelen + 1);
271     }
272 }
273
274 static void 
275 update_thread_private_data (struct thread_info *new_thread, 
276                             pthread_t tid, int state, int flags)
277 {
278   struct private_thread_info *pti;
279   procfs_info pidinfo;
280   struct _thread_name *tn;
281   procfs_threadctl tctl;
282
283 #if _NTO_VERSION > 630
284   gdb_assert (new_thread != NULL);
285
286   if (devctl (ctl_fd, DCMD_PROC_INFO, &pidinfo,
287               sizeof(pidinfo), 0) != EOK)
288     return;
289
290   memset (&tctl, 0, sizeof (tctl));
291   tctl.cmd = _NTO_TCTL_NAME;
292   tn = (struct _thread_name *) (&tctl.data);
293
294   /* Fetch name for the given thread.  */
295   tctl.tid = tid;
296   tn->name_buf_len = sizeof (tctl.data) - sizeof (*tn);
297   tn->new_name_len = -1; /* Getting, not setting.  */
298   if (devctl (ctl_fd, DCMD_PROC_THREADCTL, &tctl, sizeof (tctl), NULL) != EOK)
299     tn->name_buf[0] = '\0';
300
301   tn->name_buf[_NTO_THREAD_NAME_MAX] = '\0';
302
303   update_thread_private_data_name (new_thread, tn->name_buf);
304
305   pti = (struct private_thread_info *) new_thread->private;
306   pti->tid = tid;
307   pti->state = state;
308   pti->flags = flags;
309 #endif /* _NTO_VERSION */
310 }
311
312 static void
313 procfs_find_new_threads (struct target_ops *ops)
314 {
315   procfs_status status;
316   pid_t pid;
317   ptid_t ptid;
318   pthread_t tid;
319   struct thread_info *new_thread;
320
321   if (ctl_fd == -1)
322     return;
323
324   pid = ptid_get_pid (inferior_ptid);
325
326   status.tid = 1;
327
328   for (tid = 1;; ++tid)
329     {
330       if (status.tid == tid 
331           && (devctl (ctl_fd, DCMD_PROC_TIDSTATUS, &status, sizeof (status), 0)
332               != EOK))
333         break;
334       if (status.tid != tid)
335         /* The reason why this would not be equal is that devctl might have 
336            returned different tid, meaning the requested tid no longer exists
337            (e.g. thread exited).  */
338         continue;
339       ptid = ptid_build (pid, 0, tid);
340       new_thread = find_thread_ptid (ptid);
341       if (!new_thread)
342         new_thread = add_thread (ptid);
343       update_thread_private_data (new_thread, tid, status.state, 0);
344       status.tid++;
345     }
346   return;
347 }
348
349 static void
350 do_closedir_cleanup (void *dir)
351 {
352   closedir (dir);
353 }
354
355 void
356 procfs_pidlist (char *args, int from_tty)
357 {
358   DIR *dp = NULL;
359   struct dirent *dirp = NULL;
360   char buf[512];
361   procfs_info *pidinfo = NULL;
362   procfs_debuginfo *info = NULL;
363   procfs_status *status = NULL;
364   pid_t num_threads = 0;
365   pid_t pid;
366   char name[512];
367   struct cleanup *cleanups;
368
369   dp = opendir (nto_procfs_path);
370   if (dp == NULL)
371     {
372       fprintf_unfiltered (gdb_stderr, "failed to opendir \"%s\" - %d (%s)",
373                           nto_procfs_path, errno, safe_strerror (errno));
374       return;
375     }
376
377   cleanups = make_cleanup (do_closedir_cleanup, dp);
378
379   /* Start scan at first pid.  */
380   rewinddir (dp);
381
382   do
383     {
384       int fd;
385       struct cleanup *inner_cleanup;
386
387       /* Get the right pid and procfs path for the pid.  */
388       do
389         {
390           dirp = readdir (dp);
391           if (dirp == NULL)
392             {
393               do_cleanups (cleanups);
394               return;
395             }
396           snprintf (buf, 511, "%s/%s/as", nto_procfs_path, dirp->d_name);
397           pid = atoi (dirp->d_name);
398         }
399       while (pid == 0);
400
401       /* Open the procfs path.  */
402       fd = open (buf, O_RDONLY);
403       if (fd == -1)
404         {
405           fprintf_unfiltered (gdb_stderr, "failed to open %s - %d (%s)\n",
406                               buf, errno, safe_strerror (errno));
407           do_cleanups (cleanups);
408           return;
409         }
410       inner_cleanup = make_cleanup_close (fd);
411
412       pidinfo = (procfs_info *) buf;
413       if (devctl (fd, DCMD_PROC_INFO, pidinfo, sizeof (buf), 0) != EOK)
414         {
415           fprintf_unfiltered (gdb_stderr,
416                               "devctl DCMD_PROC_INFO failed - %d (%s)\n",
417                               errno, safe_strerror (errno));
418           break;
419         }
420       num_threads = pidinfo->num_threads;
421
422       info = (procfs_debuginfo *) buf;
423       if (devctl (fd, DCMD_PROC_MAPDEBUG_BASE, info, sizeof (buf), 0) != EOK)
424         strcpy (name, "unavailable");
425       else
426         strcpy (name, info->path);
427
428       /* Collect state info on all the threads.  */
429       status = (procfs_status *) buf;
430       for (status->tid = 1; status->tid <= num_threads; status->tid++)
431         {
432           if (devctl (fd, DCMD_PROC_TIDSTATUS, status, sizeof (buf), 0) != EOK
433               && status->tid != 0)
434             break;
435           if (status->tid != 0)
436             printf_filtered ("%s - %d/%d\n", name, pid, status->tid);
437         }
438
439       do_cleanups (inner_cleanup);
440     }
441   while (dirp != NULL);
442
443   do_cleanups (cleanups);
444   return;
445 }
446
447 void
448 procfs_meminfo (char *args, int from_tty)
449 {
450   procfs_mapinfo *mapinfos = NULL;
451   static int num_mapinfos = 0;
452   procfs_mapinfo *mapinfo_p, *mapinfo_p2;
453   int flags = ~0, err, num, i, j;
454
455   struct
456   {
457     procfs_debuginfo info;
458     char buff[_POSIX_PATH_MAX];
459   } map;
460
461   struct info
462   {
463     unsigned addr;
464     unsigned size;
465     unsigned flags;
466     unsigned debug_vaddr;
467     unsigned long long offset;
468   };
469
470   struct printinfo
471   {
472     unsigned long long ino;
473     unsigned dev;
474     struct info text;
475     struct info data;
476     char name[256];
477   } printme;
478
479   /* Get the number of map entrys.  */
480   err = devctl (ctl_fd, DCMD_PROC_MAPINFO, NULL, 0, &num);
481   if (err != EOK)
482     {
483       printf ("failed devctl num mapinfos - %d (%s)\n", err,
484               safe_strerror (err));
485       return;
486     }
487
488   mapinfos = xmalloc (num * sizeof (procfs_mapinfo));
489
490   num_mapinfos = num;
491   mapinfo_p = mapinfos;
492
493   /* Fill the map entrys.  */
494   err = devctl (ctl_fd, DCMD_PROC_MAPINFO, mapinfo_p, num
495                 * sizeof (procfs_mapinfo), &num);
496   if (err != EOK)
497     {
498       printf ("failed devctl mapinfos - %d (%s)\n", err, safe_strerror (err));
499       xfree (mapinfos);
500       return;
501     }
502
503   num = min (num, num_mapinfos);
504
505   /* Run through the list of mapinfos, and store the data and text info
506      so we can print it at the bottom of the loop.  */
507   for (mapinfo_p = mapinfos, i = 0; i < num; i++, mapinfo_p++)
508     {
509       if (!(mapinfo_p->flags & flags))
510         mapinfo_p->ino = 0;
511
512       if (mapinfo_p->ino == 0)  /* Already visited.  */
513         continue;
514
515       map.info.vaddr = mapinfo_p->vaddr;
516
517       err = devctl (ctl_fd, DCMD_PROC_MAPDEBUG, &map, sizeof (map), 0);
518       if (err != EOK)
519         continue;
520
521       memset (&printme, 0, sizeof printme);
522       printme.dev = mapinfo_p->dev;
523       printme.ino = mapinfo_p->ino;
524       printme.text.addr = mapinfo_p->vaddr;
525       printme.text.size = mapinfo_p->size;
526       printme.text.flags = mapinfo_p->flags;
527       printme.text.offset = mapinfo_p->offset;
528       printme.text.debug_vaddr = map.info.vaddr;
529       strcpy (printme.name, map.info.path);
530
531       /* Check for matching data.  */
532       for (mapinfo_p2 = mapinfos, j = 0; j < num; j++, mapinfo_p2++)
533         {
534           if (mapinfo_p2->vaddr != mapinfo_p->vaddr
535               && mapinfo_p2->ino == mapinfo_p->ino
536               && mapinfo_p2->dev == mapinfo_p->dev)
537             {
538               map.info.vaddr = mapinfo_p2->vaddr;
539               err =
540                 devctl (ctl_fd, DCMD_PROC_MAPDEBUG, &map, sizeof (map), 0);
541               if (err != EOK)
542                 continue;
543
544               if (strcmp (map.info.path, printme.name))
545                 continue;
546
547               /* Lower debug_vaddr is always text, if nessessary, swap.  */
548               if ((int) map.info.vaddr < (int) printme.text.debug_vaddr)
549                 {
550                   memcpy (&(printme.data), &(printme.text),
551                           sizeof (printme.data));
552                   printme.text.addr = mapinfo_p2->vaddr;
553                   printme.text.size = mapinfo_p2->size;
554                   printme.text.flags = mapinfo_p2->flags;
555                   printme.text.offset = mapinfo_p2->offset;
556                   printme.text.debug_vaddr = map.info.vaddr;
557                 }
558               else
559                 {
560                   printme.data.addr = mapinfo_p2->vaddr;
561                   printme.data.size = mapinfo_p2->size;
562                   printme.data.flags = mapinfo_p2->flags;
563                   printme.data.offset = mapinfo_p2->offset;
564                   printme.data.debug_vaddr = map.info.vaddr;
565                 }
566               mapinfo_p2->ino = 0;
567             }
568         }
569       mapinfo_p->ino = 0;
570
571       printf_filtered ("%s\n", printme.name);
572       printf_filtered ("\ttext=%08x bytes @ 0x%08x\n", printme.text.size,
573                        printme.text.addr);
574       printf_filtered ("\t\tflags=%08x\n", printme.text.flags);
575       printf_filtered ("\t\tdebug=%08x\n", printme.text.debug_vaddr);
576       printf_filtered ("\t\toffset=%s\n", phex (printme.text.offset, 8));
577       if (printme.data.size)
578         {
579           printf_filtered ("\tdata=%08x bytes @ 0x%08x\n", printme.data.size,
580                            printme.data.addr);
581           printf_filtered ("\t\tflags=%08x\n", printme.data.flags);
582           printf_filtered ("\t\tdebug=%08x\n", printme.data.debug_vaddr);
583           printf_filtered ("\t\toffset=%s\n", phex (printme.data.offset, 8));
584         }
585       printf_filtered ("\tdev=0x%x\n", printme.dev);
586       printf_filtered ("\tino=0x%x\n", (unsigned int) printme.ino);
587     }
588   xfree (mapinfos);
589   return;
590 }
591
592 /* Print status information about what we're accessing.  */
593 static void
594 procfs_files_info (struct target_ops *ignore)
595 {
596   struct inferior *inf = current_inferior ();
597
598   printf_unfiltered ("\tUsing the running image of %s %s via %s.\n",
599                      inf->attach_flag ? "attached" : "child",
600                      target_pid_to_str (inferior_ptid), nto_procfs_path);
601 }
602
603 /* Attach to process PID, then initialize for debugging it.  */
604 static void
605 procfs_attach (struct target_ops *ops, const char *args, int from_tty)
606 {
607   char *exec_file;
608   int pid;
609   struct inferior *inf;
610
611   pid = parse_pid_to_attach (args);
612
613   if (pid == getpid ())
614     error (_("Attaching GDB to itself is not a good idea..."));
615
616   if (from_tty)
617     {
618       exec_file = (char *) get_exec_file (0);
619
620       if (exec_file)
621         printf_unfiltered ("Attaching to program `%s', %s\n", exec_file,
622                            target_pid_to_str (pid_to_ptid (pid)));
623       else
624         printf_unfiltered ("Attaching to %s\n",
625                            target_pid_to_str (pid_to_ptid (pid)));
626
627       gdb_flush (gdb_stdout);
628     }
629   inferior_ptid = do_attach (pid_to_ptid (pid));
630   inf = current_inferior ();
631   inferior_appeared (inf, pid);
632   inf->attach_flag = 1;
633
634   if (!target_is_pushed (ops))
635     push_target (ops);
636
637   procfs_find_new_threads (ops);
638 }
639
640 static void
641 procfs_post_attach (struct target_ops *self, pid_t pid)
642 {
643   if (exec_bfd)
644     solib_create_inferior_hook (0);
645 }
646
647 static ptid_t
648 do_attach (ptid_t ptid)
649 {
650   procfs_status status;
651   struct sigevent event;
652   char path[PATH_MAX];
653
654   snprintf (path, PATH_MAX - 1, "%s/%d/as", nto_procfs_path,
655             ptid_get_pid (ptid));
656   ctl_fd = open (path, O_RDWR);
657   if (ctl_fd == -1)
658     error (_("Couldn't open proc file %s, error %d (%s)"), path, errno,
659            safe_strerror (errno));
660   if (devctl (ctl_fd, DCMD_PROC_STOP, &status, sizeof (status), 0) != EOK)
661     error (_("Couldn't stop process"));
662
663   /* Define a sigevent for process stopped notification.  */
664   event.sigev_notify = SIGEV_SIGNAL_THREAD;
665   event.sigev_signo = SIGUSR1;
666   event.sigev_code = 0;
667   event.sigev_value.sival_ptr = NULL;
668   event.sigev_priority = -1;
669   devctl (ctl_fd, DCMD_PROC_EVENT, &event, sizeof (event), 0);
670
671   if (devctl (ctl_fd, DCMD_PROC_STATUS, &status, sizeof (status), 0) == EOK
672       && status.flags & _DEBUG_FLAG_STOPPED)
673     SignalKill (nto_node (), ptid_get_pid (ptid), 0, SIGCONT, 0, 0);
674   nto_init_solib_absolute_prefix ();
675   return ptid_build (ptid_get_pid (ptid), 0, status.tid);
676 }
677
678 /* Ask the user what to do when an interrupt is received.  */
679 static void
680 interrupt_query (void)
681 {
682   target_terminal_ours ();
683
684   if (query (_("Interrupted while waiting for the program.\n\
685 Give up (and stop debugging it)? ")))
686     {
687       target_mourn_inferior ();
688       quit ();
689     }
690
691   target_terminal_inferior ();
692 }
693
694 /* The user typed ^C twice.  */
695 static void
696 nto_interrupt_twice (int signo)
697 {
698   signal (signo, ofunc);
699   interrupt_query ();
700   signal (signo, nto_interrupt_twice);
701 }
702
703 static void
704 nto_interrupt (int signo)
705 {
706   /* If this doesn't work, try more severe steps.  */
707   signal (signo, nto_interrupt_twice);
708
709   target_stop (inferior_ptid);
710 }
711
712 static ptid_t
713 procfs_wait (struct target_ops *ops,
714              ptid_t ptid, struct target_waitstatus *ourstatus, int options)
715 {
716   sigset_t set;
717   siginfo_t info;
718   procfs_status status;
719   static int exit_signo = 0;    /* To track signals that cause termination.  */
720
721   ourstatus->kind = TARGET_WAITKIND_SPURIOUS;
722
723   if (ptid_equal (inferior_ptid, null_ptid))
724     {
725       ourstatus->kind = TARGET_WAITKIND_STOPPED;
726       ourstatus->value.sig = GDB_SIGNAL_0;
727       exit_signo = 0;
728       return null_ptid;
729     }
730
731   sigemptyset (&set);
732   sigaddset (&set, SIGUSR1);
733
734   devctl (ctl_fd, DCMD_PROC_STATUS, &status, sizeof (status), 0);
735   while (!(status.flags & _DEBUG_FLAG_ISTOP))
736     {
737       ofunc = (void (*)()) signal (SIGINT, nto_interrupt);
738       sigwaitinfo (&set, &info);
739       signal (SIGINT, ofunc);
740       devctl (ctl_fd, DCMD_PROC_STATUS, &status, sizeof (status), 0);
741     }
742
743   if (status.flags & _DEBUG_FLAG_SSTEP)
744     {
745       ourstatus->kind = TARGET_WAITKIND_STOPPED;
746       ourstatus->value.sig = GDB_SIGNAL_TRAP;
747     }
748   /* Was it a breakpoint?  */
749   else if (status.flags & _DEBUG_FLAG_TRACE)
750     {
751       ourstatus->kind = TARGET_WAITKIND_STOPPED;
752       ourstatus->value.sig = GDB_SIGNAL_TRAP;
753     }
754   else if (status.flags & _DEBUG_FLAG_ISTOP)
755     {
756       switch (status.why)
757         {
758         case _DEBUG_WHY_SIGNALLED:
759           ourstatus->kind = TARGET_WAITKIND_STOPPED;
760           ourstatus->value.sig =
761             gdb_signal_from_host (status.info.si_signo);
762           exit_signo = 0;
763           break;
764         case _DEBUG_WHY_FAULTED:
765           ourstatus->kind = TARGET_WAITKIND_STOPPED;
766           if (status.info.si_signo == SIGTRAP)
767             {
768               ourstatus->value.sig = 0;
769               exit_signo = 0;
770             }
771           else
772             {
773               ourstatus->value.sig =
774                 gdb_signal_from_host (status.info.si_signo);
775               exit_signo = ourstatus->value.sig;
776             }
777           break;
778
779         case _DEBUG_WHY_TERMINATED:
780           {
781             int waitval = 0;
782
783             waitpid (ptid_get_pid (inferior_ptid), &waitval, WNOHANG);
784             if (exit_signo)
785               {
786                 /* Abnormal death.  */
787                 ourstatus->kind = TARGET_WAITKIND_SIGNALLED;
788                 ourstatus->value.sig = exit_signo;
789               }
790             else
791               {
792                 /* Normal death.  */
793                 ourstatus->kind = TARGET_WAITKIND_EXITED;
794                 ourstatus->value.integer = WEXITSTATUS (waitval);
795               }
796             exit_signo = 0;
797             break;
798           }
799
800         case _DEBUG_WHY_REQUESTED:
801           /* We are assuming a requested stop is due to a SIGINT.  */
802           ourstatus->kind = TARGET_WAITKIND_STOPPED;
803           ourstatus->value.sig = GDB_SIGNAL_INT;
804           exit_signo = 0;
805           break;
806         }
807     }
808
809   return ptid_build (status.pid, 0, status.tid);
810 }
811
812 /* Read the current values of the inferior's registers, both the
813    general register set and floating point registers (if supported)
814    and update gdb's idea of their current values.  */
815 static void
816 procfs_fetch_registers (struct target_ops *ops,
817                         struct regcache *regcache, int regno)
818 {
819   union
820   {
821     procfs_greg greg;
822     procfs_fpreg fpreg;
823     procfs_altreg altreg;
824   }
825   reg;
826   int regsize;
827
828   procfs_set_thread (inferior_ptid);
829   if (devctl (ctl_fd, DCMD_PROC_GETGREG, &reg, sizeof (reg), &regsize) == EOK)
830     nto_supply_gregset (regcache, (char *) &reg.greg);
831   if (devctl (ctl_fd, DCMD_PROC_GETFPREG, &reg, sizeof (reg), &regsize)
832       == EOK)
833     nto_supply_fpregset (regcache, (char *) &reg.fpreg);
834   if (devctl (ctl_fd, DCMD_PROC_GETALTREG, &reg, sizeof (reg), &regsize)
835       == EOK)
836     nto_supply_altregset (regcache, (char *) &reg.altreg);
837 }
838
839 /* Helper for procfs_xfer_partial that handles memory transfers.
840    Arguments are like target_xfer_partial.  */
841
842 static enum target_xfer_status
843 procfs_xfer_memory (gdb_byte *readbuf, const gdb_byte *writebuf,
844                     ULONGEST memaddr, ULONGEST len, ULONGEST *xfered_len)
845 {
846   int nbytes;
847
848   if (lseek (ctl_fd, (off_t) memaddr, SEEK_SET) != (off_t) memaddr)
849     return TARGET_XFER_E_IO;
850
851   if (writebuf != NULL)
852     nbytes = write (ctl_fd, writebuf, len);
853   else
854     nbytes = read (ctl_fd, readbuf, len);
855   if (nbytes <= 0)
856     return TARGET_XFER_E_IO;
857   *xfered_len = nbytes;
858   return TARGET_XFER_OK;
859 }
860
861 /* Target to_xfer_partial implementation.  */
862
863 static enum target_xfer_status
864 procfs_xfer_partial (struct target_ops *ops, enum target_object object,
865                      const char *annex, gdb_byte *readbuf,
866                      const gdb_byte *writebuf, ULONGEST offset, ULONGEST len,
867                      ULONGEST *xfered_len)
868 {
869   switch (object)
870     {
871     case TARGET_OBJECT_MEMORY:
872       return procfs_xfer_memory (readbuf, writebuf, offset, len, xfered_len);
873     default:
874       return ops->beneath->to_xfer_partial (ops->beneath, object, annex,
875                                             readbuf, writebuf, offset, len);
876     }
877 }
878
879 /* Take a program previously attached to and detaches it.
880    The program resumes execution and will no longer stop
881    on signals, etc.  We'd better not have left any breakpoints
882    in the program or it'll die when it hits one.  */
883 static void
884 procfs_detach (struct target_ops *ops, const char *args, int from_tty)
885 {
886   int siggnal = 0;
887   int pid;
888
889   if (from_tty)
890     {
891       char *exec_file = get_exec_file (0);
892       if (exec_file == 0)
893         exec_file = "";
894       printf_unfiltered ("Detaching from program: %s %s\n",
895                          exec_file, target_pid_to_str (inferior_ptid));
896       gdb_flush (gdb_stdout);
897     }
898   if (args)
899     siggnal = atoi (args);
900
901   if (siggnal)
902     SignalKill (nto_node (), ptid_get_pid (inferior_ptid), 0, siggnal, 0, 0);
903
904   close (ctl_fd);
905   ctl_fd = -1;
906
907   pid = ptid_get_pid (inferior_ptid);
908   inferior_ptid = null_ptid;
909   detach_inferior (pid);
910   init_thread_list ();
911   inf_child_maybe_unpush_target (ops);
912 }
913
914 static int
915 procfs_breakpoint (CORE_ADDR addr, int type, int size)
916 {
917   procfs_break brk;
918
919   brk.type = type;
920   brk.addr = addr;
921   brk.size = size;
922   errno = devctl (ctl_fd, DCMD_PROC_BREAK, &brk, sizeof (brk), 0);
923   if (errno != EOK)
924     return 1;
925   return 0;
926 }
927
928 static int
929 procfs_insert_breakpoint (struct target_ops *ops, struct gdbarch *gdbarch,
930                           struct bp_target_info *bp_tgt)
931 {
932   return procfs_breakpoint (bp_tgt->placed_address, _DEBUG_BREAK_EXEC, 0);
933 }
934
935 static int
936 procfs_remove_breakpoint (struct target_ops *ops, struct gdbarch *gdbarch,
937                           struct bp_target_info *bp_tgt)
938 {
939   return procfs_breakpoint (bp_tgt->placed_address, _DEBUG_BREAK_EXEC, -1);
940 }
941
942 static int
943 procfs_insert_hw_breakpoint (struct target_ops *self, struct gdbarch *gdbarch,
944                              struct bp_target_info *bp_tgt)
945 {
946   return procfs_breakpoint (bp_tgt->placed_address,
947                             _DEBUG_BREAK_EXEC | _DEBUG_BREAK_HW, 0);
948 }
949
950 static int
951 procfs_remove_hw_breakpoint (struct target_ops *self,
952                              struct gdbarch *gdbarch,
953                              struct bp_target_info *bp_tgt)
954 {
955   return procfs_breakpoint (bp_tgt->placed_address,
956                             _DEBUG_BREAK_EXEC | _DEBUG_BREAK_HW, -1);
957 }
958
959 static void
960 procfs_resume (struct target_ops *ops,
961                ptid_t ptid, int step, enum gdb_signal signo)
962 {
963   int signal_to_pass;
964   procfs_status status;
965   sigset_t *run_fault = (sigset_t *) (void *) &run.fault;
966
967   if (ptid_equal (inferior_ptid, null_ptid))
968     return;
969
970   procfs_set_thread (ptid_equal (ptid, minus_one_ptid) ? inferior_ptid :
971                      ptid);
972
973   run.flags = _DEBUG_RUN_FAULT | _DEBUG_RUN_TRACE;
974   if (step)
975     run.flags |= _DEBUG_RUN_STEP;
976
977   sigemptyset (run_fault);
978   sigaddset (run_fault, FLTBPT);
979   sigaddset (run_fault, FLTTRACE);
980   sigaddset (run_fault, FLTILL);
981   sigaddset (run_fault, FLTPRIV);
982   sigaddset (run_fault, FLTBOUNDS);
983   sigaddset (run_fault, FLTIOVF);
984   sigaddset (run_fault, FLTIZDIV);
985   sigaddset (run_fault, FLTFPE);
986   /* Peter V will be changing this at some point.  */
987   sigaddset (run_fault, FLTPAGE);
988
989   run.flags |= _DEBUG_RUN_ARM;
990
991   signal_to_pass = gdb_signal_to_host (signo);
992
993   if (signal_to_pass)
994     {
995       devctl (ctl_fd, DCMD_PROC_STATUS, &status, sizeof (status), 0);
996       signal_to_pass = gdb_signal_to_host (signo);
997       if (status.why & (_DEBUG_WHY_SIGNALLED | _DEBUG_WHY_FAULTED))
998         {
999           if (signal_to_pass != status.info.si_signo)
1000             {
1001               SignalKill (nto_node (), ptid_get_pid (inferior_ptid), 0,
1002                           signal_to_pass, 0, 0);
1003               run.flags |= _DEBUG_RUN_CLRFLT | _DEBUG_RUN_CLRSIG;
1004             }
1005           else          /* Let it kill the program without telling us.  */
1006             sigdelset (&run.trace, signal_to_pass);
1007         }
1008     }
1009   else
1010     run.flags |= _DEBUG_RUN_CLRSIG | _DEBUG_RUN_CLRFLT;
1011
1012   errno = devctl (ctl_fd, DCMD_PROC_RUN, &run, sizeof (run), 0);
1013   if (errno != EOK)
1014     {
1015       perror (_("run error!\n"));
1016       return;
1017     }
1018 }
1019
1020 static void
1021 procfs_mourn_inferior (struct target_ops *ops)
1022 {
1023   if (!ptid_equal (inferior_ptid, null_ptid))
1024     {
1025       SignalKill (nto_node (), ptid_get_pid (inferior_ptid), 0, SIGKILL, 0, 0);
1026       close (ctl_fd);
1027     }
1028   inferior_ptid = null_ptid;
1029   init_thread_list ();
1030   inf_child_mourn_inferior (ops);
1031 }
1032
1033 /* This function breaks up an argument string into an argument
1034    vector suitable for passing to execvp().
1035    E.g., on "run a b c d" this routine would get as input
1036    the string "a b c d", and as output it would fill in argv with
1037    the four arguments "a", "b", "c", "d".  The only additional
1038    functionality is simple quoting.  The gdb command:
1039         run a "b c d" f
1040    will fill in argv with the three args "a", "b c d", "e".  */
1041 static void
1042 breakup_args (char *scratch, char **argv)
1043 {
1044   char *pp, *cp = scratch;
1045   char quoting = 0;
1046
1047   for (;;)
1048     {
1049       /* Scan past leading separators.  */
1050       quoting = 0;
1051       while (*cp == ' ' || *cp == '\t' || *cp == '\n')
1052         cp++;
1053
1054       /* Break if at end of string.  */
1055       if (*cp == '\0')
1056         break;
1057
1058       /* Take an arg.  */
1059       if (*cp == '"')
1060         {
1061           cp++;
1062           quoting = strchr (cp, '"') ? 1 : 0;
1063         }
1064
1065       *argv++ = cp;
1066
1067       /* Scan for next arg separator.  */
1068       pp = cp;
1069       if (quoting)
1070         cp = strchr (pp, '"');
1071       if ((cp == NULL) || (!quoting))
1072         cp = strchr (pp, ' ');
1073       if (cp == NULL)
1074         cp = strchr (pp, '\t');
1075       if (cp == NULL)
1076         cp = strchr (pp, '\n');
1077
1078       /* No separators => end of string => break.  */
1079       if (cp == NULL)
1080         {
1081           pp = cp;
1082           break;
1083         }
1084
1085       /* Replace the separator with a terminator.  */
1086       *cp++ = '\0';
1087     }
1088
1089   /* Execv requires a null-terminated arg vector.  */
1090   *argv = NULL;
1091 }
1092
1093 static void
1094 procfs_create_inferior (struct target_ops *ops, char *exec_file,
1095                         char *allargs, char **env, int from_tty)
1096 {
1097   struct inheritance inherit;
1098   pid_t pid;
1099   int flags, errn;
1100   char **argv, *args;
1101   const char *in = "", *out = "", *err = "";
1102   int fd, fds[3];
1103   sigset_t set;
1104   const char *inferior_io_terminal = get_inferior_io_terminal ();
1105   struct inferior *inf;
1106
1107   argv = xmalloc (((strlen (allargs) + 1) / (unsigned) 2 + 2) *
1108                   sizeof (*argv));
1109   argv[0] = get_exec_file (1);
1110   if (!argv[0])
1111     {
1112       if (exec_file)
1113         argv[0] = exec_file;
1114       else
1115         return;
1116     }
1117
1118   args = xstrdup (allargs);
1119   breakup_args (args, exec_file ? &argv[1] : &argv[0]);
1120
1121   argv = nto_parse_redirection (argv, &in, &out, &err);
1122
1123   fds[0] = STDIN_FILENO;
1124   fds[1] = STDOUT_FILENO;
1125   fds[2] = STDERR_FILENO;
1126
1127   /* If the user specified I/O via gdb's --tty= arg, use it, but only
1128      if the i/o is not also being specified via redirection.  */
1129   if (inferior_io_terminal)
1130     {
1131       if (!in[0])
1132         in = inferior_io_terminal;
1133       if (!out[0])
1134         out = inferior_io_terminal;
1135       if (!err[0])
1136         err = inferior_io_terminal;
1137     }
1138
1139   if (in[0])
1140     {
1141       fd = open (in, O_RDONLY);
1142       if (fd == -1)
1143         perror (in);
1144       else
1145         fds[0] = fd;
1146     }
1147   if (out[0])
1148     {
1149       fd = open (out, O_WRONLY);
1150       if (fd == -1)
1151         perror (out);
1152       else
1153         fds[1] = fd;
1154     }
1155   if (err[0])
1156     {
1157       fd = open (err, O_WRONLY);
1158       if (fd == -1)
1159         perror (err);
1160       else
1161         fds[2] = fd;
1162     }
1163
1164   /* Clear any pending SIGUSR1's but keep the behavior the same.  */
1165   signal (SIGUSR1, signal (SIGUSR1, SIG_IGN));
1166
1167   sigemptyset (&set);
1168   sigaddset (&set, SIGUSR1);
1169   sigprocmask (SIG_UNBLOCK, &set, NULL);
1170
1171   memset (&inherit, 0, sizeof (inherit));
1172
1173   if (ND_NODE_CMP (nto_procfs_node, ND_LOCAL_NODE) != 0)
1174     {
1175       inherit.nd = nto_node ();
1176       inherit.flags |= SPAWN_SETND;
1177       inherit.flags &= ~SPAWN_EXEC;
1178     }
1179   inherit.flags |= SPAWN_SETGROUP | SPAWN_HOLD;
1180   inherit.pgroup = SPAWN_NEWPGROUP;
1181   pid = spawnp (argv[0], 3, fds, &inherit, argv,
1182                 ND_NODE_CMP (nto_procfs_node, ND_LOCAL_NODE) == 0 ? env : 0);
1183   xfree (args);
1184
1185   sigprocmask (SIG_BLOCK, &set, NULL);
1186
1187   if (pid == -1)
1188     error (_("Error spawning %s: %d (%s)"), argv[0], errno,
1189            safe_strerror (errno));
1190
1191   if (fds[0] != STDIN_FILENO)
1192     close (fds[0]);
1193   if (fds[1] != STDOUT_FILENO)
1194     close (fds[1]);
1195   if (fds[2] != STDERR_FILENO)
1196     close (fds[2]);
1197
1198   inferior_ptid = do_attach (pid_to_ptid (pid));
1199   procfs_find_new_threads (ops);
1200
1201   inf = current_inferior ();
1202   inferior_appeared (inf, pid);
1203   inf->attach_flag = 0;
1204
1205   flags = _DEBUG_FLAG_KLC;      /* Kill-on-Last-Close flag.  */
1206   errn = devctl (ctl_fd, DCMD_PROC_SET_FLAG, &flags, sizeof (flags), 0);
1207   if (errn != EOK)
1208     {
1209       /* FIXME: expected warning?  */
1210       /* warning( "Failed to set Kill-on-Last-Close flag: errno = %d(%s)\n",
1211          errn, strerror(errn) ); */
1212     }
1213   if (!target_is_pushed (ops))
1214     push_target (ops);
1215   target_terminal_init ();
1216
1217   if (exec_bfd != NULL
1218       || (symfile_objfile != NULL && symfile_objfile->obfd != NULL))
1219     solib_create_inferior_hook (0);
1220 }
1221
1222 static void
1223 procfs_stop (struct target_ops *self, ptid_t ptid)
1224 {
1225   devctl (ctl_fd, DCMD_PROC_STOP, NULL, 0, 0);
1226 }
1227
1228 static void
1229 procfs_kill_inferior (struct target_ops *ops)
1230 {
1231   target_mourn_inferior ();
1232 }
1233
1234 /* Fill buf with regset and return devctl cmd to do the setting.  Return
1235    -1 if we fail to get the regset.  Store size of regset in regsize.  */
1236 static int
1237 get_regset (int regset, char *buf, int bufsize, int *regsize)
1238 {
1239   int dev_get, dev_set;
1240   switch (regset)
1241     {
1242     case NTO_REG_GENERAL:
1243       dev_get = DCMD_PROC_GETGREG;
1244       dev_set = DCMD_PROC_SETGREG;
1245       break;
1246
1247     case NTO_REG_FLOAT:
1248       dev_get = DCMD_PROC_GETFPREG;
1249       dev_set = DCMD_PROC_SETFPREG;
1250       break;
1251
1252     case NTO_REG_ALT:
1253       dev_get = DCMD_PROC_GETALTREG;
1254       dev_set = DCMD_PROC_SETALTREG;
1255       break;
1256
1257     case NTO_REG_SYSTEM:
1258     default:
1259       return -1;
1260     }
1261   if (devctl (ctl_fd, dev_get, buf, bufsize, regsize) != EOK)
1262     return -1;
1263
1264   return dev_set;
1265 }
1266
1267 void
1268 procfs_store_registers (struct target_ops *ops,
1269                         struct regcache *regcache, int regno)
1270 {
1271   union
1272   {
1273     procfs_greg greg;
1274     procfs_fpreg fpreg;
1275     procfs_altreg altreg;
1276   }
1277   reg;
1278   unsigned off;
1279   int len, regset, regsize, dev_set, err;
1280   char *data;
1281
1282   if (ptid_equal (inferior_ptid, null_ptid))
1283     return;
1284   procfs_set_thread (inferior_ptid);
1285
1286   if (regno == -1)
1287     {
1288       for (regset = NTO_REG_GENERAL; regset < NTO_REG_END; regset++)
1289         {
1290           dev_set = get_regset (regset, (char *) &reg,
1291                                 sizeof (reg), &regsize);
1292           if (dev_set == -1)
1293             continue;
1294
1295           if (nto_regset_fill (regcache, regset, (char *) &reg) == -1)
1296             continue;
1297
1298           err = devctl (ctl_fd, dev_set, &reg, regsize, 0);
1299           if (err != EOK)
1300             fprintf_unfiltered (gdb_stderr,
1301                                 "Warning unable to write regset %d: %s\n",
1302                                 regno, safe_strerror (err));
1303         }
1304     }
1305   else
1306     {
1307       regset = nto_regset_id (regno);
1308       if (regset == -1)
1309         return;
1310
1311       dev_set = get_regset (regset, (char *) &reg, sizeof (reg), &regsize);
1312       if (dev_set == -1)
1313         return;
1314
1315       len = nto_register_area (get_regcache_arch (regcache),
1316                                regno, regset, &off);
1317
1318       if (len < 1)
1319         return;
1320
1321       regcache_raw_collect (regcache, regno, (char *) &reg + off);
1322
1323       err = devctl (ctl_fd, dev_set, &reg, regsize, 0);
1324       if (err != EOK)
1325         fprintf_unfiltered (gdb_stderr,
1326                             "Warning unable to write regset %d: %s\n", regno,
1327                             safe_strerror (err));
1328     }
1329 }
1330
1331 /* Set list of signals to be handled in the target.  */
1332
1333 static void
1334 procfs_pass_signals (struct target_ops *self,
1335                      int numsigs, unsigned char *pass_signals)
1336 {
1337   int signo;
1338
1339   sigfillset (&run.trace);
1340
1341   for (signo = 1; signo < NSIG; signo++)
1342     {
1343       int target_signo = gdb_signal_from_host (signo);
1344       if (target_signo < numsigs && pass_signals[target_signo])
1345         sigdelset (&run.trace, signo);
1346     }
1347 }
1348
1349 static struct tidinfo *
1350 procfs_thread_info (pid_t pid, short tid)
1351 {
1352 /* NYI */
1353   return NULL;
1354 }
1355
1356 static char *
1357 procfs_pid_to_str (struct target_ops *ops, ptid_t ptid)
1358 {
1359   static char buf[1024];
1360   int pid, tid, n;
1361   struct tidinfo *tip;
1362
1363   pid = ptid_get_pid (ptid);
1364   tid = ptid_get_tid (ptid);
1365
1366   n = snprintf (buf, 1023, "process %d", pid);
1367
1368 #if 0                           /* NYI */
1369   tip = procfs_thread_info (pid, tid);
1370   if (tip != NULL)
1371     snprintf (&buf[n], 1023, " (state = 0x%02x)", tip->state);
1372 #endif
1373
1374   return buf;
1375 }
1376
1377 /* to_can_run implementation for "target procfs".  Note this really
1378   means "can this target be the default run target", which there can
1379   be only one, and we make it be "target native" like other ports.
1380   "target procfs <node>" wouldn't make sense as default run target, as
1381   it needs <node>.  */
1382
1383 static int
1384 procfs_can_run (struct target_ops *self)
1385 {
1386   return 0;
1387 }
1388
1389 /* "target procfs".  */
1390 static struct target_ops nto_procfs_ops;
1391
1392 /* "target native".  */
1393 static struct target_ops *nto_native_ops;
1394
1395 /* to_open implementation for "target procfs".  */
1396
1397 static void
1398 procfs_open (char *arg, int from_tty)
1399 {
1400   procfs_open_1 (&nto_procfs_ops, arg, from_tty);
1401 }
1402
1403 /* to_open implementation for "target native".  */
1404
1405 static void
1406 procfs_native_open (char *arg, int from_tty)
1407 {
1408   procfs_open_1 (nto_native_ops, arg, from_tty);
1409 }
1410
1411 /* Create the "native" and "procfs" targets.  */
1412
1413 static void
1414 init_procfs_targets (void)
1415 {
1416   struct target_ops *t = inf_child_target ();
1417
1418   /* Leave to_shortname as "native".  */
1419   t->to_longname = "QNX Neutrino local process";
1420   t->to_doc = "QNX Neutrino local process (started by the \"run\" command).";
1421   t->to_open = procfs_native_open;
1422   t->to_attach = procfs_attach;
1423   t->to_post_attach = procfs_post_attach;
1424   t->to_detach = procfs_detach;
1425   t->to_resume = procfs_resume;
1426   t->to_wait = procfs_wait;
1427   t->to_fetch_registers = procfs_fetch_registers;
1428   t->to_store_registers = procfs_store_registers;
1429   t->to_xfer_partial = procfs_xfer_partial;
1430   t->to_files_info = procfs_files_info;
1431   t->to_insert_breakpoint = procfs_insert_breakpoint;
1432   t->to_remove_breakpoint = procfs_remove_breakpoint;
1433   t->to_can_use_hw_breakpoint = procfs_can_use_hw_breakpoint;
1434   t->to_insert_hw_breakpoint = procfs_insert_hw_breakpoint;
1435   t->to_remove_hw_breakpoint = procfs_remove_hw_breakpoint;
1436   t->to_insert_watchpoint = procfs_insert_hw_watchpoint;
1437   t->to_remove_watchpoint = procfs_remove_hw_watchpoint;
1438   t->to_stopped_by_watchpoint = procfs_stopped_by_watchpoint;
1439   t->to_kill = procfs_kill_inferior;
1440   t->to_create_inferior = procfs_create_inferior;
1441   t->to_mourn_inferior = procfs_mourn_inferior;
1442   t->to_pass_signals = procfs_pass_signals;
1443   t->to_thread_alive = procfs_thread_alive;
1444   t->to_find_new_threads = procfs_find_new_threads;
1445   t->to_pid_to_str = procfs_pid_to_str;
1446   t->to_stop = procfs_stop;
1447   t->to_have_continuable_watchpoint = 1;
1448   t->to_extra_thread_info = nto_extra_thread_info;
1449
1450   nto_native_ops = t;
1451
1452   /* Register "target native".  This is the default run target.  */
1453   add_target (t);
1454
1455   /* Register "target procfs <node>".  */
1456   nto_procfs_ops = *t;
1457   nto_procfs_ops.to_shortname = "procfs";
1458   nto_procfs_ops.to_can_run = procfs_can_run;
1459   t->to_longname = "QNX Neutrino local or remote process";
1460   t->to_doc = "QNX Neutrino process.  target procfs <node>";
1461   t->to_open = procfs_open;
1462
1463   add_target (&nto_procfs_ops);
1464 }
1465
1466 #define OSTYPE_NTO 1
1467
1468 void
1469 _initialize_procfs (void)
1470 {
1471   sigset_t set;
1472
1473   init_procfs_targets ();
1474
1475   /* We use SIGUSR1 to gain control after we block waiting for a process.
1476      We use sigwaitevent to wait.  */
1477   sigemptyset (&set);
1478   sigaddset (&set, SIGUSR1);
1479   sigprocmask (SIG_BLOCK, &set, NULL);
1480
1481   /* Initially, make sure all signals are reported.  */
1482   sigfillset (&run.trace);
1483
1484   /* Stuff some information.  */
1485   nto_cpuinfo_flags = SYSPAGE_ENTRY (cpuinfo)->flags;
1486   nto_cpuinfo_valid = 1;
1487
1488   add_info ("pidlist", procfs_pidlist, _("pidlist"));
1489   add_info ("meminfo", procfs_meminfo, _("memory information"));
1490
1491   nto_is_nto_target = procfs_is_nto_target;
1492 }
1493
1494
1495 static int
1496 procfs_hw_watchpoint (int addr, int len, int type)
1497 {
1498   procfs_break brk;
1499
1500   switch (type)
1501     {
1502     case 1:                     /* Read.  */
1503       brk.type = _DEBUG_BREAK_RD;
1504       break;
1505     case 2:                     /* Read/Write.  */
1506       brk.type = _DEBUG_BREAK_RW;
1507       break;
1508     default:                    /* Modify.  */
1509 /* FIXME: brk.type = _DEBUG_BREAK_RWM gives EINVAL for some reason.  */
1510       brk.type = _DEBUG_BREAK_RW;
1511     }
1512   brk.type |= _DEBUG_BREAK_HW;  /* Always ask for HW.  */
1513   brk.addr = addr;
1514   brk.size = len;
1515
1516   errno = devctl (ctl_fd, DCMD_PROC_BREAK, &brk, sizeof (brk), 0);
1517   if (errno != EOK)
1518     {
1519       perror (_("Failed to set hardware watchpoint"));
1520       return -1;
1521     }
1522   return 0;
1523 }
1524
1525 static int
1526 procfs_can_use_hw_breakpoint (struct target_ops *self,
1527                               int type, int cnt, int othertype)
1528 {
1529   return 1;
1530 }
1531
1532 static int
1533 procfs_remove_hw_watchpoint (struct target_ops *self,
1534                              CORE_ADDR addr, int len, int type,
1535                              struct expression *cond)
1536 {
1537   return procfs_hw_watchpoint (addr, -1, type);
1538 }
1539
1540 static int
1541 procfs_insert_hw_watchpoint (struct target_ops *self,
1542                              CORE_ADDR addr, int len, int type,
1543                              struct expression *cond)
1544 {
1545   return procfs_hw_watchpoint (addr, len, type);
1546 }
1547
1548 static int
1549 procfs_stopped_by_watchpoint (struct target_ops *ops)
1550 {
1551   return 0;
1552 }