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