OSDN Git Service

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