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