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