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