Add target_ops argument to to_remove_watchpoint
[binutils-gdb.git] / gdb / nto-procfs.c
1 /* Machine independent support for QNX Neutrino /proc (process file system)
2 for GDB. Written by Colin Burgess at QNX Software Systems Limited.
3
4 Copyright (C) 2003-2014 Free Software Foundation, Inc.
5
6 Contributed by QNX Software Systems Ltd.
7
8 This file is part of GDB.
9
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3 of the License, or
13 (at your option) any later version.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>. */
22
23 #include "defs.h"
24
25 #include <fcntl.h>
26 #include <spawn.h>
27 #include <sys/debug.h>
28 #include <sys/procfs.h>
29 #include <sys/neutrino.h>
30 #include <sys/syspage.h>
31 #include <dirent.h>
32 #include <sys/netmgr.h>
33
34 #include "exceptions.h"
35 #include <string.h>
36 #include "gdbcore.h"
37 #include "inferior.h"
38 #include "target.h"
39 #include "objfiles.h"
40 #include "gdbthread.h"
41 #include "nto-tdep.h"
42 #include "command.h"
43 #include "regcache.h"
44 #include "solib.h"
45
46 #define NULL_PID 0
47 #define _DEBUG_FLAG_TRACE (_DEBUG_FLAG_TRACE_EXEC|_DEBUG_FLAG_TRACE_RD|\
48 _DEBUG_FLAG_TRACE_WR|_DEBUG_FLAG_TRACE_MODIFY)
49
50 static struct target_ops procfs_ops;
51
52 int ctl_fd;
53
54 static void (*ofunc) ();
55
56 static procfs_run run;
57
58 static void procfs_open (char *, int);
59
60 static int procfs_can_run (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 init_procfs_ops (void);
67
68 static ptid_t do_attach (ptid_t ptid);
69
70 static int procfs_can_use_hw_breakpoint (struct target_ops *self,
71 int, int, int);
72
73 static int procfs_insert_hw_watchpoint (CORE_ADDR addr, int len, int type,
74 struct expression *cond);
75
76 static int procfs_remove_hw_watchpoint (struct target_ops *self,
77 CORE_ADDR addr, int len, int type,
78 struct expression *cond);
79
80 static int procfs_stopped_by_watchpoint (struct target_ops *ops);
81
82 /* These two globals are only ever set in procfs_open(), but are
83 referenced elsewhere. 'nto_procfs_node' is a flag used to say
84 whether we are local, or we should get the current node descriptor
85 for the remote QNX node. */
86 static char nto_procfs_path[PATH_MAX] = { "/proc" };
87 static unsigned nto_procfs_node = ND_LOCAL_NODE;
88
89 /* Return the current QNX Node, or error out. This is a simple
90 wrapper for the netmgr_strtond() function. The reason this
91 is required is because QNX node descriptors are transient so
92 we have to re-acquire them every time. */
93 static unsigned
94 nto_node (void)
95 {
96 unsigned node;
97
98 if (ND_NODE_CMP (nto_procfs_node, ND_LOCAL_NODE) == 0)
99 return ND_LOCAL_NODE;
100
101 node = netmgr_strtond (nto_procfs_path, 0);
102 if (node == -1)
103 error (_("Lost the QNX node. Debug session probably over."));
104
105 return (node);
106 }
107
108 static enum gdb_osabi
109 procfs_is_nto_target (bfd *abfd)
110 {
111 return GDB_OSABI_QNXNTO;
112 }
113
114 /* This is called when we call 'target procfs <arg>' from the (gdb) prompt.
115 For QNX6 (nto), the only valid arg will be a QNX node string,
116 eg: "/net/some_node". If arg is not a valid QNX node, we will
117 default to local. */
118 static void
119 procfs_open (char *arg, int from_tty)
120 {
121 char *nodestr;
122 char *endstr;
123 char buffer[50];
124 int fd, total_size;
125 procfs_sysinfo *sysinfo;
126 struct cleanup *cleanups;
127
128 nto_is_nto_target = procfs_is_nto_target;
129
130 /* Set the default node used for spawning to this one,
131 and only override it if there is a valid arg. */
132
133 nto_procfs_node = ND_LOCAL_NODE;
134 nodestr = arg ? xstrdup (arg) : arg;
135
136 init_thread_list ();
137
138 if (nodestr)
139 {
140 nto_procfs_node = netmgr_strtond (nodestr, &endstr);
141 if (nto_procfs_node == -1)
142 {
143 if (errno == ENOTSUP)
144 printf_filtered ("QNX Net Manager not found.\n");
145 printf_filtered ("Invalid QNX node %s: error %d (%s).\n", nodestr,
146 errno, safe_strerror (errno));
147 xfree (nodestr);
148 nodestr = NULL;
149 nto_procfs_node = ND_LOCAL_NODE;
150 }
151 else if (*endstr)
152 {
153 if (*(endstr - 1) == '/')
154 *(endstr - 1) = 0;
155 else
156 *endstr = 0;
157 }
158 }
159 snprintf (nto_procfs_path, PATH_MAX - 1, "%s%s", nodestr ? nodestr : "",
160 "/proc");
161 if (nodestr)
162 xfree (nodestr);
163
164 fd = open (nto_procfs_path, O_RDONLY);
165 if (fd == -1)
166 {
167 printf_filtered ("Error opening %s : %d (%s)\n", nto_procfs_path, errno,
168 safe_strerror (errno));
169 error (_("Invalid procfs arg"));
170 }
171 cleanups = make_cleanup_close (fd);
172
173 sysinfo = (void *) buffer;
174 if (devctl (fd, DCMD_PROC_SYSINFO, sysinfo, sizeof buffer, 0) != EOK)
175 {
176 printf_filtered ("Error getting size: %d (%s)\n", errno,
177 safe_strerror (errno));
178 error (_("Devctl failed."));
179 }
180 else
181 {
182 total_size = sysinfo->total_size;
183 sysinfo = alloca (total_size);
184 if (!sysinfo)
185 {
186 printf_filtered ("Memory error: %d (%s)\n", errno,
187 safe_strerror (errno));
188 error (_("alloca failed."));
189 }
190 else
191 {
192 if (devctl (fd, DCMD_PROC_SYSINFO, sysinfo, total_size, 0) != EOK)
193 {
194 printf_filtered ("Error getting sysinfo: %d (%s)\n", errno,
195 safe_strerror (errno));
196 error (_("Devctl failed."));
197 }
198 else
199 {
200 if (sysinfo->type !=
201 nto_map_arch_to_cputype (gdbarch_bfd_arch_info
202 (target_gdbarch ())->arch_name))
203 error (_("Invalid target CPU."));
204 }
205 }
206 }
207 do_cleanups (cleanups);
208 printf_filtered ("Debugging using %s\n", nto_procfs_path);
209 }
210
211 static void
212 procfs_set_thread (ptid_t ptid)
213 {
214 pid_t tid;
215
216 tid = ptid_get_tid (ptid);
217 devctl (ctl_fd, DCMD_PROC_CURTHREAD, &tid, sizeof (tid), 0);
218 }
219
220 /* Return nonzero if the thread TH is still alive. */
221 static int
222 procfs_thread_alive (struct target_ops *ops, ptid_t ptid)
223 {
224 pid_t tid;
225 pid_t pid;
226 procfs_status status;
227 int err;
228
229 tid = ptid_get_tid (ptid);
230 pid = ptid_get_pid (ptid);
231
232 if (kill (pid, 0) == -1)
233 return 0;
234
235 status.tid = tid;
236 if ((err = devctl (ctl_fd, DCMD_PROC_TIDSTATUS,
237 &status, sizeof (status), 0)) != EOK)
238 return 0;
239
240 /* Thread is alive or dead but not yet joined,
241 or dead and there is an alive (or dead unjoined) thread with
242 higher tid.
243
244 If the tid is not the same as requested, requested tid is dead. */
245 return (status.tid == tid) && (status.state != STATE_DEAD);
246 }
247
248 static void
249 update_thread_private_data_name (struct thread_info *new_thread,
250 const char *newname)
251 {
252 int newnamelen;
253 struct private_thread_info *pti;
254
255 gdb_assert (newname != NULL);
256 gdb_assert (new_thread != NULL);
257 newnamelen = strlen (newname);
258 if (!new_thread->private)
259 {
260 new_thread->private = xmalloc (offsetof (struct private_thread_info,
261 name)
262 + newnamelen + 1);
263 memcpy (new_thread->private->name, newname, newnamelen + 1);
264 }
265 else if (strcmp (newname, new_thread->private->name) != 0)
266 {
267 /* Reallocate if neccessary. */
268 int oldnamelen = strlen (new_thread->private->name);
269
270 if (oldnamelen < newnamelen)
271 new_thread->private = xrealloc (new_thread->private,
272 offsetof (struct private_thread_info,
273 name)
274 + newnamelen + 1);
275 memcpy (new_thread->private->name, newname, newnamelen + 1);
276 }
277 }
278
279 static void
280 update_thread_private_data (struct thread_info *new_thread,
281 pthread_t tid, int state, int flags)
282 {
283 struct private_thread_info *pti;
284 procfs_info pidinfo;
285 struct _thread_name *tn;
286 procfs_threadctl tctl;
287
288 #if _NTO_VERSION > 630
289 gdb_assert (new_thread != NULL);
290
291 if (devctl (ctl_fd, DCMD_PROC_INFO, &pidinfo,
292 sizeof(pidinfo), 0) != EOK)
293 return;
294
295 memset (&tctl, 0, sizeof (tctl));
296 tctl.cmd = _NTO_TCTL_NAME;
297 tn = (struct _thread_name *) (&tctl.data);
298
299 /* Fetch name for the given thread. */
300 tctl.tid = tid;
301 tn->name_buf_len = sizeof (tctl.data) - sizeof (*tn);
302 tn->new_name_len = -1; /* Getting, not setting. */
303 if (devctl (ctl_fd, DCMD_PROC_THREADCTL, &tctl, sizeof (tctl), NULL) != EOK)
304 tn->name_buf[0] = '\0';
305
306 tn->name_buf[_NTO_THREAD_NAME_MAX] = '\0';
307
308 update_thread_private_data_name (new_thread, tn->name_buf);
309
310 pti = (struct private_thread_info *) new_thread->private;
311 pti->tid = tid;
312 pti->state = state;
313 pti->flags = flags;
314 #endif /* _NTO_VERSION */
315 }
316
317 static void
318 procfs_find_new_threads (struct target_ops *ops)
319 {
320 procfs_status status;
321 pid_t pid;
322 ptid_t ptid;
323 pthread_t tid;
324 struct thread_info *new_thread;
325
326 if (ctl_fd == -1)
327 return;
328
329 pid = ptid_get_pid (inferior_ptid);
330
331 status.tid = 1;
332
333 for (tid = 1;; ++tid)
334 {
335 if (status.tid == tid
336 && (devctl (ctl_fd, DCMD_PROC_TIDSTATUS, &status, sizeof (status), 0)
337 != EOK))
338 break;
339 if (status.tid != tid)
340 /* The reason why this would not be equal is that devctl might have
341 returned different tid, meaning the requested tid no longer exists
342 (e.g. thread exited). */
343 continue;
344 ptid = ptid_build (pid, 0, tid);
345 new_thread = find_thread_ptid (ptid);
346 if (!new_thread)
347 new_thread = add_thread (ptid);
348 update_thread_private_data (new_thread, tid, status.state, 0);
349 status.tid++;
350 }
351 return;
352 }
353
354 static void
355 do_closedir_cleanup (void *dir)
356 {
357 closedir (dir);
358 }
359
360 void
361 procfs_pidlist (char *args, int from_tty)
362 {
363 DIR *dp = NULL;
364 struct dirent *dirp = NULL;
365 char buf[512];
366 procfs_info *pidinfo = NULL;
367 procfs_debuginfo *info = NULL;
368 procfs_status *status = NULL;
369 pid_t num_threads = 0;
370 pid_t pid;
371 char name[512];
372 struct cleanup *cleanups;
373
374 dp = opendir (nto_procfs_path);
375 if (dp == NULL)
376 {
377 fprintf_unfiltered (gdb_stderr, "failed to opendir \"%s\" - %d (%s)",
378 nto_procfs_path, errno, safe_strerror (errno));
379 return;
380 }
381
382 cleanups = make_cleanup (do_closedir_cleanup, dp);
383
384 /* Start scan at first pid. */
385 rewinddir (dp);
386
387 do
388 {
389 int fd;
390 struct cleanup *inner_cleanup;
391
392 /* Get the right pid and procfs path for the pid. */
393 do
394 {
395 dirp = readdir (dp);
396 if (dirp == NULL)
397 {
398 do_cleanups (cleanups);
399 return;
400 }
401 snprintf (buf, 511, "%s/%s/as", nto_procfs_path, dirp->d_name);
402 pid = atoi (dirp->d_name);
403 }
404 while (pid == 0);
405
406 /* Open the procfs path. */
407 fd = open (buf, O_RDONLY);
408 if (fd == -1)
409 {
410 fprintf_unfiltered (gdb_stderr, "failed to open %s - %d (%s)\n",
411 buf, errno, safe_strerror (errno));
412 do_cleanups (cleanups);
413 return;
414 }
415 inner_cleanup = make_cleanup_close (fd);
416
417 pidinfo = (procfs_info *) buf;
418 if (devctl (fd, DCMD_PROC_INFO, pidinfo, sizeof (buf), 0) != EOK)
419 {
420 fprintf_unfiltered (gdb_stderr,
421 "devctl DCMD_PROC_INFO failed - %d (%s)\n",
422 errno, safe_strerror (errno));
423 break;
424 }
425 num_threads = pidinfo->num_threads;
426
427 info = (procfs_debuginfo *) buf;
428 if (devctl (fd, DCMD_PROC_MAPDEBUG_BASE, info, sizeof (buf), 0) != EOK)
429 strcpy (name, "unavailable");
430 else
431 strcpy (name, info->path);
432
433 /* Collect state info on all the threads. */
434 status = (procfs_status *) buf;
435 for (status->tid = 1; status->tid <= num_threads; status->tid++)
436 {
437 if (devctl (fd, DCMD_PROC_TIDSTATUS, status, sizeof (buf), 0) != EOK
438 && status->tid != 0)
439 break;
440 if (status->tid != 0)
441 printf_filtered ("%s - %d/%d\n", name, pid, status->tid);
442 }
443
444 do_cleanups (inner_cleanup);
445 }
446 while (dirp != NULL);
447
448 do_cleanups (cleanups);
449 return;
450 }
451
452 void
453 procfs_meminfo (char *args, int from_tty)
454 {
455 procfs_mapinfo *mapinfos = NULL;
456 static int num_mapinfos = 0;
457 procfs_mapinfo *mapinfo_p, *mapinfo_p2;
458 int flags = ~0, err, num, i, j;
459
460 struct
461 {
462 procfs_debuginfo info;
463 char buff[_POSIX_PATH_MAX];
464 } map;
465
466 struct info
467 {
468 unsigned addr;
469 unsigned size;
470 unsigned flags;
471 unsigned debug_vaddr;
472 unsigned long long offset;
473 };
474
475 struct printinfo
476 {
477 unsigned long long ino;
478 unsigned dev;
479 struct info text;
480 struct info data;
481 char name[256];
482 } printme;
483
484 /* Get the number of map entrys. */
485 err = devctl (ctl_fd, DCMD_PROC_MAPINFO, NULL, 0, &num);
486 if (err != EOK)
487 {
488 printf ("failed devctl num mapinfos - %d (%s)\n", err,
489 safe_strerror (err));
490 return;
491 }
492
493 mapinfos = xmalloc (num * sizeof (procfs_mapinfo));
494
495 num_mapinfos = num;
496 mapinfo_p = mapinfos;
497
498 /* Fill the map entrys. */
499 err = devctl (ctl_fd, DCMD_PROC_MAPINFO, mapinfo_p, num
500 * sizeof (procfs_mapinfo), &num);
501 if (err != EOK)
502 {
503 printf ("failed devctl mapinfos - %d (%s)\n", err, safe_strerror (err));
504 xfree (mapinfos);
505 return;
506 }
507
508 num = min (num, num_mapinfos);
509
510 /* Run through the list of mapinfos, and store the data and text info
511 so we can print it at the bottom of the loop. */
512 for (mapinfo_p = mapinfos, i = 0; i < num; i++, mapinfo_p++)
513 {
514 if (!(mapinfo_p->flags & flags))
515 mapinfo_p->ino = 0;
516
517 if (mapinfo_p->ino == 0) /* Already visited. */
518 continue;
519
520 map.info.vaddr = mapinfo_p->vaddr;
521
522 err = devctl (ctl_fd, DCMD_PROC_MAPDEBUG, &map, sizeof (map), 0);
523 if (err != EOK)
524 continue;
525
526 memset (&printme, 0, sizeof printme);
527 printme.dev = mapinfo_p->dev;
528 printme.ino = mapinfo_p->ino;
529 printme.text.addr = mapinfo_p->vaddr;
530 printme.text.size = mapinfo_p->size;
531 printme.text.flags = mapinfo_p->flags;
532 printme.text.offset = mapinfo_p->offset;
533 printme.text.debug_vaddr = map.info.vaddr;
534 strcpy (printme.name, map.info.path);
535
536 /* Check for matching data. */
537 for (mapinfo_p2 = mapinfos, j = 0; j < num; j++, mapinfo_p2++)
538 {
539 if (mapinfo_p2->vaddr != mapinfo_p->vaddr
540 && mapinfo_p2->ino == mapinfo_p->ino
541 && mapinfo_p2->dev == mapinfo_p->dev)
542 {
543 map.info.vaddr = mapinfo_p2->vaddr;
544 err =
545 devctl (ctl_fd, DCMD_PROC_MAPDEBUG, &map, sizeof (map), 0);
546 if (err != EOK)
547 continue;
548
549 if (strcmp (map.info.path, printme.name))
550 continue;
551
552 /* Lower debug_vaddr is always text, if nessessary, swap. */
553 if ((int) map.info.vaddr < (int) printme.text.debug_vaddr)
554 {
555 memcpy (&(printme.data), &(printme.text),
556 sizeof (printme.data));
557 printme.text.addr = mapinfo_p2->vaddr;
558 printme.text.size = mapinfo_p2->size;
559 printme.text.flags = mapinfo_p2->flags;
560 printme.text.offset = mapinfo_p2->offset;
561 printme.text.debug_vaddr = map.info.vaddr;
562 }
563 else
564 {
565 printme.data.addr = mapinfo_p2->vaddr;
566 printme.data.size = mapinfo_p2->size;
567 printme.data.flags = mapinfo_p2->flags;
568 printme.data.offset = mapinfo_p2->offset;
569 printme.data.debug_vaddr = map.info.vaddr;
570 }
571 mapinfo_p2->ino = 0;
572 }
573 }
574 mapinfo_p->ino = 0;
575
576 printf_filtered ("%s\n", printme.name);
577 printf_filtered ("\ttext=%08x bytes @ 0x%08x\n", printme.text.size,
578 printme.text.addr);
579 printf_filtered ("\t\tflags=%08x\n", printme.text.flags);
580 printf_filtered ("\t\tdebug=%08x\n", printme.text.debug_vaddr);
581 printf_filtered ("\t\toffset=%s\n", phex (printme.text.offset, 8));
582 if (printme.data.size)
583 {
584 printf_filtered ("\tdata=%08x bytes @ 0x%08x\n", printme.data.size,
585 printme.data.addr);
586 printf_filtered ("\t\tflags=%08x\n", printme.data.flags);
587 printf_filtered ("\t\tdebug=%08x\n", printme.data.debug_vaddr);
588 printf_filtered ("\t\toffset=%s\n", phex (printme.data.offset, 8));
589 }
590 printf_filtered ("\tdev=0x%x\n", printme.dev);
591 printf_filtered ("\tino=0x%x\n", (unsigned int) printme.ino);
592 }
593 xfree (mapinfos);
594 return;
595 }
596
597 /* Print status information about what we're accessing. */
598 static void
599 procfs_files_info (struct target_ops *ignore)
600 {
601 struct inferior *inf = current_inferior ();
602
603 printf_unfiltered ("\tUsing the running image of %s %s via %s.\n",
604 inf->attach_flag ? "attached" : "child",
605 target_pid_to_str (inferior_ptid), nto_procfs_path);
606 }
607
608 /* Mark our target-struct as eligible for stray "run" and "attach"
609 commands. */
610 static int
611 procfs_can_run (void)
612 {
613 return 1;
614 }
615
616 /* Attach to process PID, then initialize for debugging it. */
617 static void
618 procfs_attach (struct target_ops *ops, char *args, int from_tty)
619 {
620 char *exec_file;
621 int pid;
622 struct inferior *inf;
623
624 pid = parse_pid_to_attach (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 (struct target_ops *self, pid_t pid)
654 {
655 if (exec_bfd)
656 solib_create_inferior_hook (0);
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,
667 ptid_get_pid (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 (), ptid_get_pid (ptid), 0, SIGCONT, 0, 0);
686 nto_init_solib_absolute_prefix ();
687 return ptid_build (ptid_get_pid (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 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 = GDB_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 = GDB_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 = GDB_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 gdb_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 gdb_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 (ptid_get_pid (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 = GDB_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, const 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 (), ptid_get_pid (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 target_ops *ops, 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 target_ops *ops, 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 target_ops *self, 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 gdb_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 signal_to_pass = gdb_signal_to_host (signo);
989
990 if (signal_to_pass)
991 {
992 devctl (ctl_fd, DCMD_PROC_STATUS, &status, sizeof (status), 0);
993 signal_to_pass = gdb_signal_to_host (signo);
994 if (status.why & (_DEBUG_WHY_SIGNALLED | _DEBUG_WHY_FAULTED))
995 {
996 if (signal_to_pass != status.info.si_signo)
997 {
998 SignalKill (nto_node (), ptid_get_pid (inferior_ptid), 0,
999 signal_to_pass, 0, 0);
1000 run.flags |= _DEBUG_RUN_CLRFLT | _DEBUG_RUN_CLRSIG;
1001 }
1002 else /* Let it kill the program without telling us. */
1003 sigdelset (&run.trace, signal_to_pass);
1004 }
1005 }
1006 else
1007 run.flags |= _DEBUG_RUN_CLRSIG | _DEBUG_RUN_CLRFLT;
1008
1009 errno = devctl (ctl_fd, DCMD_PROC_RUN, &run, sizeof (run), 0);
1010 if (errno != EOK)
1011 {
1012 perror (_("run error!\n"));
1013 return;
1014 }
1015 }
1016
1017 static void
1018 procfs_mourn_inferior (struct target_ops *ops)
1019 {
1020 if (!ptid_equal (inferior_ptid, null_ptid))
1021 {
1022 SignalKill (nto_node (), ptid_get_pid (inferior_ptid), 0, SIGKILL, 0, 0);
1023 close (ctl_fd);
1024 }
1025 inferior_ptid = null_ptid;
1026 init_thread_list ();
1027 unpush_target (&procfs_ops);
1028 generic_mourn_inferior ();
1029 }
1030
1031 /* This function breaks up an argument string into an argument
1032 vector suitable for passing to execvp().
1033 E.g., on "run a b c d" this routine would get as input
1034 the string "a b c d", and as output it would fill in argv with
1035 the four arguments "a", "b", "c", "d". The only additional
1036 functionality is simple quoting. The gdb command:
1037 run a "b c d" f
1038 will fill in argv with the three args "a", "b c d", "e". */
1039 static void
1040 breakup_args (char *scratch, char **argv)
1041 {
1042 char *pp, *cp = scratch;
1043 char quoting = 0;
1044
1045 for (;;)
1046 {
1047 /* Scan past leading separators. */
1048 quoting = 0;
1049 while (*cp == ' ' || *cp == '\t' || *cp == '\n')
1050 cp++;
1051
1052 /* Break if at end of string. */
1053 if (*cp == '\0')
1054 break;
1055
1056 /* Take an arg. */
1057 if (*cp == '"')
1058 {
1059 cp++;
1060 quoting = strchr (cp, '"') ? 1 : 0;
1061 }
1062
1063 *argv++ = cp;
1064
1065 /* Scan for next arg separator. */
1066 pp = cp;
1067 if (quoting)
1068 cp = strchr (pp, '"');
1069 if ((cp == NULL) || (!quoting))
1070 cp = strchr (pp, ' ');
1071 if (cp == NULL)
1072 cp = strchr (pp, '\t');
1073 if (cp == NULL)
1074 cp = strchr (pp, '\n');
1075
1076 /* No separators => end of string => break. */
1077 if (cp == NULL)
1078 {
1079 pp = cp;
1080 break;
1081 }
1082
1083 /* Replace the separator with a terminator. */
1084 *cp++ = '\0';
1085 }
1086
1087 /* Execv requires a null-terminated arg vector. */
1088 *argv = NULL;
1089 }
1090
1091 static void
1092 procfs_create_inferior (struct target_ops *ops, char *exec_file,
1093 char *allargs, char **env, int from_tty)
1094 {
1095 struct inheritance inherit;
1096 pid_t pid;
1097 int flags, errn;
1098 char **argv, *args;
1099 const char *in = "", *out = "", *err = "";
1100 int fd, fds[3];
1101 sigset_t set;
1102 const char *inferior_io_terminal = get_inferior_io_terminal ();
1103 struct inferior *inf;
1104
1105 argv = xmalloc (((strlen (allargs) + 1) / (unsigned) 2 + 2) *
1106 sizeof (*argv));
1107 argv[0] = get_exec_file (1);
1108 if (!argv[0])
1109 {
1110 if (exec_file)
1111 argv[0] = exec_file;
1112 else
1113 return;
1114 }
1115
1116 args = xstrdup (allargs);
1117 breakup_args (args, exec_file ? &argv[1] : &argv[0]);
1118
1119 argv = nto_parse_redirection (argv, &in, &out, &err);
1120
1121 fds[0] = STDIN_FILENO;
1122 fds[1] = STDOUT_FILENO;
1123 fds[2] = STDERR_FILENO;
1124
1125 /* If the user specified I/O via gdb's --tty= arg, use it, but only
1126 if the i/o is not also being specified via redirection. */
1127 if (inferior_io_terminal)
1128 {
1129 if (!in[0])
1130 in = inferior_io_terminal;
1131 if (!out[0])
1132 out = inferior_io_terminal;
1133 if (!err[0])
1134 err = inferior_io_terminal;
1135 }
1136
1137 if (in[0])
1138 {
1139 fd = open (in, O_RDONLY);
1140 if (fd == -1)
1141 perror (in);
1142 else
1143 fds[0] = fd;
1144 }
1145 if (out[0])
1146 {
1147 fd = open (out, O_WRONLY);
1148 if (fd == -1)
1149 perror (out);
1150 else
1151 fds[1] = fd;
1152 }
1153 if (err[0])
1154 {
1155 fd = open (err, O_WRONLY);
1156 if (fd == -1)
1157 perror (err);
1158 else
1159 fds[2] = fd;
1160 }
1161
1162 /* Clear any pending SIGUSR1's but keep the behavior the same. */
1163 signal (SIGUSR1, signal (SIGUSR1, SIG_IGN));
1164
1165 sigemptyset (&set);
1166 sigaddset (&set, SIGUSR1);
1167 sigprocmask (SIG_UNBLOCK, &set, NULL);
1168
1169 memset (&inherit, 0, sizeof (inherit));
1170
1171 if (ND_NODE_CMP (nto_procfs_node, ND_LOCAL_NODE) != 0)
1172 {
1173 inherit.nd = nto_node ();
1174 inherit.flags |= SPAWN_SETND;
1175 inherit.flags &= ~SPAWN_EXEC;
1176 }
1177 inherit.flags |= SPAWN_SETGROUP | SPAWN_HOLD;
1178 inherit.pgroup = SPAWN_NEWPGROUP;
1179 pid = spawnp (argv[0], 3, fds, &inherit, argv,
1180 ND_NODE_CMP (nto_procfs_node, ND_LOCAL_NODE) == 0 ? env : 0);
1181 xfree (args);
1182
1183 sigprocmask (SIG_BLOCK, &set, NULL);
1184
1185 if (pid == -1)
1186 error (_("Error spawning %s: %d (%s)"), argv[0], errno,
1187 safe_strerror (errno));
1188
1189 if (fds[0] != STDIN_FILENO)
1190 close (fds[0]);
1191 if (fds[1] != STDOUT_FILENO)
1192 close (fds[1]);
1193 if (fds[2] != STDERR_FILENO)
1194 close (fds[2]);
1195
1196 inferior_ptid = do_attach (pid_to_ptid (pid));
1197 procfs_find_new_threads (ops);
1198
1199 inf = current_inferior ();
1200 inferior_appeared (inf, pid);
1201 inf->attach_flag = 0;
1202
1203 flags = _DEBUG_FLAG_KLC; /* Kill-on-Last-Close flag. */
1204 errn = devctl (ctl_fd, DCMD_PROC_SET_FLAG, &flags, sizeof (flags), 0);
1205 if (errn != EOK)
1206 {
1207 /* FIXME: expected warning? */
1208 /* warning( "Failed to set Kill-on-Last-Close flag: errno = %d(%s)\n",
1209 errn, strerror(errn) ); */
1210 }
1211 push_target (ops);
1212 target_terminal_init ();
1213
1214 if (exec_bfd != NULL
1215 || (symfile_objfile != NULL && symfile_objfile->obfd != NULL))
1216 solib_create_inferior_hook (0);
1217 }
1218
1219 static void
1220 procfs_stop (ptid_t ptid)
1221 {
1222 devctl (ctl_fd, DCMD_PROC_STOP, NULL, 0, 0);
1223 }
1224
1225 static void
1226 procfs_kill_inferior (struct target_ops *ops)
1227 {
1228 target_mourn_inferior ();
1229 }
1230
1231 /* Store register REGNO, or all registers if REGNO == -1, from the contents
1232 of REGISTERS. */
1233 static void
1234 procfs_prepare_to_store (struct target_ops *self, struct regcache *regcache)
1235 {
1236 }
1237
1238 /* Fill buf with regset and return devctl cmd to do the setting. Return
1239 -1 if we fail to get the regset. Store size of regset in regsize. */
1240 static int
1241 get_regset (int regset, char *buf, int bufsize, int *regsize)
1242 {
1243 int dev_get, dev_set;
1244 switch (regset)
1245 {
1246 case NTO_REG_GENERAL:
1247 dev_get = DCMD_PROC_GETGREG;
1248 dev_set = DCMD_PROC_SETGREG;
1249 break;
1250
1251 case NTO_REG_FLOAT:
1252 dev_get = DCMD_PROC_GETFPREG;
1253 dev_set = DCMD_PROC_SETFPREG;
1254 break;
1255
1256 case NTO_REG_ALT:
1257 dev_get = DCMD_PROC_GETALTREG;
1258 dev_set = DCMD_PROC_SETALTREG;
1259 break;
1260
1261 case NTO_REG_SYSTEM:
1262 default:
1263 return -1;
1264 }
1265 if (devctl (ctl_fd, dev_get, buf, bufsize, regsize) != EOK)
1266 return -1;
1267
1268 return dev_set;
1269 }
1270
1271 void
1272 procfs_store_registers (struct target_ops *ops,
1273 struct regcache *regcache, int regno)
1274 {
1275 union
1276 {
1277 procfs_greg greg;
1278 procfs_fpreg fpreg;
1279 procfs_altreg altreg;
1280 }
1281 reg;
1282 unsigned off;
1283 int len, regset, regsize, dev_set, err;
1284 char *data;
1285
1286 if (ptid_equal (inferior_ptid, null_ptid))
1287 return;
1288 procfs_set_thread (inferior_ptid);
1289
1290 if (regno == -1)
1291 {
1292 for (regset = NTO_REG_GENERAL; regset < NTO_REG_END; regset++)
1293 {
1294 dev_set = get_regset (regset, (char *) &reg,
1295 sizeof (reg), &regsize);
1296 if (dev_set == -1)
1297 continue;
1298
1299 if (nto_regset_fill (regcache, regset, (char *) &reg) == -1)
1300 continue;
1301
1302 err = devctl (ctl_fd, dev_set, &reg, regsize, 0);
1303 if (err != EOK)
1304 fprintf_unfiltered (gdb_stderr,
1305 "Warning unable to write regset %d: %s\n",
1306 regno, safe_strerror (err));
1307 }
1308 }
1309 else
1310 {
1311 regset = nto_regset_id (regno);
1312 if (regset == -1)
1313 return;
1314
1315 dev_set = get_regset (regset, (char *) &reg, sizeof (reg), &regsize);
1316 if (dev_set == -1)
1317 return;
1318
1319 len = nto_register_area (get_regcache_arch (regcache),
1320 regno, regset, &off);
1321
1322 if (len < 1)
1323 return;
1324
1325 regcache_raw_collect (regcache, regno, (char *) &reg + off);
1326
1327 err = devctl (ctl_fd, dev_set, &reg, regsize, 0);
1328 if (err != EOK)
1329 fprintf_unfiltered (gdb_stderr,
1330 "Warning unable to write regset %d: %s\n", regno,
1331 safe_strerror (err));
1332 }
1333 }
1334
1335 /* Set list of signals to be handled in the target. */
1336
1337 static void
1338 procfs_pass_signals (int numsigs, unsigned char *pass_signals)
1339 {
1340 int signo;
1341
1342 sigfillset (&run.trace);
1343
1344 for (signo = 1; signo < NSIG; signo++)
1345 {
1346 int target_signo = gdb_signal_from_host (signo);
1347 if (target_signo < numsigs && pass_signals[target_signo])
1348 sigdelset (&run.trace, signo);
1349 }
1350 }
1351
1352 static struct tidinfo *
1353 procfs_thread_info (pid_t pid, short tid)
1354 {
1355 /* NYI */
1356 return NULL;
1357 }
1358
1359 static char *
1360 procfs_pid_to_str (struct target_ops *ops, ptid_t ptid)
1361 {
1362 static char buf[1024];
1363 int pid, tid, n;
1364 struct tidinfo *tip;
1365
1366 pid = ptid_get_pid (ptid);
1367 tid = ptid_get_tid (ptid);
1368
1369 n = snprintf (buf, 1023, "process %d", pid);
1370
1371 #if 0 /* NYI */
1372 tip = procfs_thread_info (pid, tid);
1373 if (tip != NULL)
1374 snprintf (&buf[n], 1023, " (state = 0x%02x)", tip->state);
1375 #endif
1376
1377 return buf;
1378 }
1379
1380 static void
1381 init_procfs_ops (void)
1382 {
1383 procfs_ops.to_shortname = "procfs";
1384 procfs_ops.to_longname = "QNX Neutrino procfs child process";
1385 procfs_ops.to_doc =
1386 "QNX Neutrino procfs child process (started by the \"run\" command).\n\
1387 target procfs <node>";
1388 procfs_ops.to_open = procfs_open;
1389 procfs_ops.to_attach = procfs_attach;
1390 procfs_ops.to_post_attach = procfs_post_attach;
1391 procfs_ops.to_detach = procfs_detach;
1392 procfs_ops.to_resume = procfs_resume;
1393 procfs_ops.to_wait = procfs_wait;
1394 procfs_ops.to_fetch_registers = procfs_fetch_registers;
1395 procfs_ops.to_store_registers = procfs_store_registers;
1396 procfs_ops.to_prepare_to_store = procfs_prepare_to_store;
1397 procfs_ops.deprecated_xfer_memory = procfs_xfer_memory;
1398 procfs_ops.to_files_info = procfs_files_info;
1399 procfs_ops.to_insert_breakpoint = procfs_insert_breakpoint;
1400 procfs_ops.to_remove_breakpoint = procfs_remove_breakpoint;
1401 procfs_ops.to_can_use_hw_breakpoint = procfs_can_use_hw_breakpoint;
1402 procfs_ops.to_insert_hw_breakpoint = procfs_insert_hw_breakpoint;
1403 procfs_ops.to_remove_hw_breakpoint = procfs_remove_breakpoint;
1404 procfs_ops.to_insert_watchpoint = procfs_insert_hw_watchpoint;
1405 procfs_ops.to_remove_watchpoint = procfs_remove_hw_watchpoint;
1406 procfs_ops.to_stopped_by_watchpoint = procfs_stopped_by_watchpoint;
1407 procfs_ops.to_terminal_init = terminal_init_inferior;
1408 procfs_ops.to_terminal_inferior = terminal_inferior;
1409 procfs_ops.to_terminal_ours_for_output = terminal_ours_for_output;
1410 procfs_ops.to_terminal_ours = terminal_ours;
1411 procfs_ops.to_terminal_info = child_terminal_info;
1412 procfs_ops.to_kill = procfs_kill_inferior;
1413 procfs_ops.to_create_inferior = procfs_create_inferior;
1414 procfs_ops.to_mourn_inferior = procfs_mourn_inferior;
1415 procfs_ops.to_can_run = procfs_can_run;
1416 procfs_ops.to_pass_signals = procfs_pass_signals;
1417 procfs_ops.to_thread_alive = procfs_thread_alive;
1418 procfs_ops.to_find_new_threads = procfs_find_new_threads;
1419 procfs_ops.to_pid_to_str = procfs_pid_to_str;
1420 procfs_ops.to_stop = procfs_stop;
1421 procfs_ops.to_stratum = process_stratum;
1422 procfs_ops.to_has_all_memory = default_child_has_all_memory;
1423 procfs_ops.to_has_memory = default_child_has_memory;
1424 procfs_ops.to_has_stack = default_child_has_stack;
1425 procfs_ops.to_has_registers = default_child_has_registers;
1426 procfs_ops.to_has_execution = default_child_has_execution;
1427 procfs_ops.to_magic = OPS_MAGIC;
1428 procfs_ops.to_have_continuable_watchpoint = 1;
1429 procfs_ops.to_extra_thread_info = nto_extra_thread_info;
1430 }
1431
1432 #define OSTYPE_NTO 1
1433
1434 void
1435 _initialize_procfs (void)
1436 {
1437 sigset_t set;
1438
1439 init_procfs_ops ();
1440 add_target (&procfs_ops);
1441
1442 /* We use SIGUSR1 to gain control after we block waiting for a process.
1443 We use sigwaitevent to wait. */
1444 sigemptyset (&set);
1445 sigaddset (&set, SIGUSR1);
1446 sigprocmask (SIG_BLOCK, &set, NULL);
1447
1448 /* Initially, make sure all signals are reported. */
1449 sigfillset (&run.trace);
1450
1451 /* Stuff some information. */
1452 nto_cpuinfo_flags = SYSPAGE_ENTRY (cpuinfo)->flags;
1453 nto_cpuinfo_valid = 1;
1454
1455 add_info ("pidlist", procfs_pidlist, _("pidlist"));
1456 add_info ("meminfo", procfs_meminfo, _("memory information"));
1457
1458 nto_is_nto_target = procfs_is_nto_target;
1459 }
1460
1461
1462 static int
1463 procfs_hw_watchpoint (int addr, int len, int type)
1464 {
1465 procfs_break brk;
1466
1467 switch (type)
1468 {
1469 case 1: /* Read. */
1470 brk.type = _DEBUG_BREAK_RD;
1471 break;
1472 case 2: /* Read/Write. */
1473 brk.type = _DEBUG_BREAK_RW;
1474 break;
1475 default: /* Modify. */
1476 /* FIXME: brk.type = _DEBUG_BREAK_RWM gives EINVAL for some reason. */
1477 brk.type = _DEBUG_BREAK_RW;
1478 }
1479 brk.type |= _DEBUG_BREAK_HW; /* Always ask for HW. */
1480 brk.addr = addr;
1481 brk.size = len;
1482
1483 errno = devctl (ctl_fd, DCMD_PROC_BREAK, &brk, sizeof (brk), 0);
1484 if (errno != EOK)
1485 {
1486 perror (_("Failed to set hardware watchpoint"));
1487 return -1;
1488 }
1489 return 0;
1490 }
1491
1492 static int
1493 procfs_can_use_hw_breakpoint (struct target_ops *self,
1494 int type, int cnt, int othertype)
1495 {
1496 return 1;
1497 }
1498
1499 static int
1500 procfs_remove_hw_watchpoint (struct target_ops *self,
1501 CORE_ADDR addr, int len, int type,
1502 struct expression *cond)
1503 {
1504 return procfs_hw_watchpoint (addr, -1, type);
1505 }
1506
1507 static int
1508 procfs_insert_hw_watchpoint (CORE_ADDR addr, int len, int type,
1509 struct expression *cond)
1510 {
1511 return procfs_hw_watchpoint (addr, len, type);
1512 }
1513
1514 static int
1515 procfs_stopped_by_watchpoint (struct target_ops *ops)
1516 {
1517 return 0;
1518 }