1 /* Machine independent support for QNX Neutrino /proc (process file system)
2 for GDB. Written by Colin Burgess at QNX Software Systems Limited.
4 Copyright 2003 Free Software Foundation, Inc.
6 Contributed by QNX Software Systems Ltd.
8 This file is part of GDB.
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.
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.
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., 59 Temple Place - Suite 330,
23 Boston, MA 02111-1307, USA. */
29 #include <sys/debug.h>
30 #include <sys/procfs.h>
31 #include <sys/neutrino.h>
32 #include <sys/syspage.h>
34 #include <sys/netmgr.h>
36 #include "gdb_string.h"
41 #include "gdbthread.h"
47 #define _DEBUG_FLAG_TRACE (_DEBUG_FLAG_TRACE_EXEC|_DEBUG_FLAG_TRACE_RD|\
48 _DEBUG_FLAG_TRACE_WR|_DEBUG_FLAG_TRACE_MODIFY)
50 static struct target_ops procfs_ops;
54 static void (*ofunc) ();
56 static procfs_run run;
58 static void procfs_open (char *, int);
60 static int procfs_can_run (void);
62 static ptid_t procfs_wait (ptid_t, struct target_waitstatus *);
64 static int procfs_xfer_memory (CORE_ADDR, char *, int, int,
65 struct mem_attrib *attrib,
68 static void procfs_fetch_registers (int);
70 static void notice_signals (void);
72 static void init_procfs_ops (void);
74 static ptid_t do_attach (ptid_t ptid);
76 static int procfs_can_use_hw_breakpoint (int, int, int);
78 static int procfs_insert_hw_breakpoint (CORE_ADDR, char *);
80 static int procfs_remove_hw_breakpoint (CORE_ADDR addr, char *);
82 static int procfs_insert_hw_watchpoint (CORE_ADDR addr, int len, int type);
84 static int procfs_remove_hw_watchpoint (CORE_ADDR addr, int len, int type);
86 static int procfs_stopped_by_watchpoint (void);
88 /* These two globals are only ever set in procfs_open(), but are
89 referenced elsewhere. 'nto_procfs_node' is a flag used to say
90 whether we are local, or we should get the current node descriptor
91 for the remote QNX node. */
92 static char nto_procfs_path[PATH_MAX] = { "/proc" };
93 static unsigned nto_procfs_node = ND_LOCAL_NODE;
95 /* Return the current QNX Node, or error out. This is a simple
96 wrapper for the netmgr_strtond() function. The reason this
97 is required is because QNX node descriptors are transient so
98 we have to re-acquire them every time. */
104 if (ND_NODE_CMP(nto_procfs_node, ND_LOCAL_NODE) == 0)
105 return ND_LOCAL_NODE;
107 node = netmgr_strtond(nto_procfs_path,0);
109 error ("Lost the QNX node. Debug session probably over.");
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
119 procfs_open (char *arg, int from_tty)
125 procfs_sysinfo *sysinfo;
127 /* Set the default node used for spawning to this one,
128 and only override it if there is a valid arg. */
130 nto_procfs_node = ND_LOCAL_NODE;
131 nodestr = arg ? xstrdup (arg) : arg;
137 nto_procfs_node = netmgr_strtond (nodestr, &endstr);
138 if (nto_procfs_node == -1)
140 if (errno == ENOTSUP)
141 printf_filtered ("QNX Net Manager not found.\n");
142 printf_filtered ("Invalid QNX node %s: error %d (%s).\n", nodestr,
143 errno, strerror (errno));
146 nto_procfs_node = ND_LOCAL_NODE;
150 if (*(endstr - 1) == '/')
156 sprintf (nto_procfs_path, "%s%s", nodestr ? nodestr : "", "/proc");
160 fd = open (nto_procfs_path, O_RDONLY);
163 printf_filtered ("Error opening %s : %d (%s)\n", nto_procfs_path, errno,
165 error ("Invalid procfs arg");
168 sysinfo = (void *) buffer;
169 if (devctl (fd, DCMD_PROC_SYSINFO, sysinfo, sizeof buffer, 0) != EOK)
171 printf_filtered ("Error getting size: %d (%s)\n", errno,
174 error ("Devctl failed.");
178 total_size = sysinfo->total_size;
179 sysinfo = alloca (total_size);
182 printf_filtered ("Memory error: %d (%s)\n", errno,
185 error ("alloca failed.");
189 if (devctl (fd, DCMD_PROC_SYSINFO, sysinfo, total_size, 0) != EOK)
191 printf_filtered ("Error getting sysinfo: %d (%s)\n", errno,
194 error ("Devctl failed.");
199 nto_map_arch_to_cputype (TARGET_ARCHITECTURE->arch_name))
202 error ("Invalid target CPU.");
208 printf_filtered ("Debugging using %s\n", nto_procfs_path);
212 procfs_set_thread (ptid_t ptid)
216 tid = ptid_get_tid (ptid);
217 devctl (ctl_fd, DCMD_PROC_CURTHREAD, &tid, sizeof (tid), 0);
220 /* Return nonzero if the thread TH is still alive. */
222 procfs_thread_alive (ptid_t ptid)
226 tid = ptid_get_tid (ptid);
227 if (devctl (ctl_fd, DCMD_PROC_CURTHREAD, &tid, sizeof (tid), 0) == EOK)
233 procfs_find_new_threads (void)
235 procfs_status status;
242 pid = ptid_get_pid (inferior_ptid);
244 for (status.tid = 1;; ++status.tid)
246 if (devctl (ctl_fd, DCMD_PROC_TIDSTATUS, &status, sizeof (status), 0)
247 != EOK && status.tid != 0)
249 ptid = ptid_build (pid, 0, status.tid);
250 if (!in_thread_list (ptid))
257 procfs_pidlist (char *args, int from_tty)
260 struct dirent *dirp = NULL;
263 procfs_info *pidinfo = NULL;
264 procfs_debuginfo *info = NULL;
265 procfs_status *status = NULL;
266 pid_t num_threads = 0;
270 dp = opendir (nto_procfs_path);
273 printf ("failed to opendir \"%s\" - %d (%s)", nto_procfs_path, errno,
278 /* Start scan at first pid. */
283 /* Get the right pid and procfs path for the pid. */
292 sprintf (buf, "%s/%s/as", nto_procfs_path, dirp->d_name);
293 pid = atoi (dirp->d_name);
297 /* Open the procfs path. */
298 fd = open (buf, O_RDONLY);
301 printf ("failed to open %s - %d (%s)\n", buf, errno,
307 pidinfo = (procfs_info *) buf;
308 if (devctl (fd, DCMD_PROC_INFO, pidinfo, sizeof (buf), 0) != EOK)
310 printf ("devctl DCMD_PROC_INFO failed - %d (%s)\n", errno,
314 num_threads = pidinfo->num_threads;
316 info = (procfs_debuginfo *) buf;
317 if (devctl (fd, DCMD_PROC_MAPDEBUG_BASE, info, sizeof (buf), 0) != EOK)
318 strcpy (name, "unavailable");
320 strcpy (name, info->path);
322 /* Collect state info on all the threads. */
323 status = (procfs_status *) buf;
324 for (status->tid = 1; status->tid <= num_threads; status->tid++)
326 if (devctl (fd, DCMD_PROC_TIDSTATUS, status, sizeof (buf), 0) != EOK
329 if (status->tid != 0)
330 printf_filtered ("%s - %d/%d\n", name, pid, status->tid);
334 while (dirp != NULL);
342 procfs_meminfo (char *args, int from_tty)
344 procfs_mapinfo *mapinfos = NULL;
345 static int num_mapinfos = 0;
346 procfs_mapinfo *mapinfo_p, *mapinfo_p2;
347 int flags = ~0, err, num, i, j;
351 procfs_debuginfo info;
352 char buff[_POSIX_PATH_MAX];
360 unsigned debug_vaddr;
361 unsigned long long offset;
366 unsigned long long ino;
373 /* Get the number of map entrys. */
374 err = devctl (ctl_fd, DCMD_PROC_MAPINFO, NULL, 0, &num);
377 printf ("failed devctl num mapinfos - %d (%s)\n", err, strerror (err));
381 mapinfos = xmalloc (num * sizeof (procfs_mapinfo));
384 mapinfo_p = mapinfos;
386 /* Fill the map entrys. */
387 err = devctl (ctl_fd, DCMD_PROC_MAPINFO, mapinfo_p, num
388 * sizeof (procfs_mapinfo), &num);
391 printf ("failed devctl mapinfos - %d (%s)\n", err, strerror (err));
396 num = min (num, num_mapinfos);
398 /* Run through the list of mapinfos, and store the data and text info
399 so we can print it at the bottom of the loop. */
400 for (mapinfo_p = mapinfos, i = 0; i < num; i++, mapinfo_p++)
402 if (!(mapinfo_p->flags & flags))
405 if (mapinfo_p->ino == 0) /* Already visited. */
408 map.info.vaddr = mapinfo_p->vaddr;
410 err = devctl (ctl_fd, DCMD_PROC_MAPDEBUG, &map, sizeof (map), 0);
414 memset (&printme, 0, sizeof printme);
415 printme.dev = mapinfo_p->dev;
416 printme.ino = mapinfo_p->ino;
417 printme.text.addr = mapinfo_p->vaddr;
418 printme.text.size = mapinfo_p->size;
419 printme.text.flags = mapinfo_p->flags;
420 printme.text.offset = mapinfo_p->offset;
421 printme.text.debug_vaddr = map.info.vaddr;
422 strcpy (printme.name, map.info.path);
424 /* Check for matching data. */
425 for (mapinfo_p2 = mapinfos, j = 0; j < num; j++, mapinfo_p2++)
427 if (mapinfo_p2->vaddr != mapinfo_p->vaddr
428 && mapinfo_p2->ino == mapinfo_p->ino
429 && mapinfo_p2->dev == mapinfo_p->dev)
431 map.info.vaddr = mapinfo_p2->vaddr;
433 devctl (ctl_fd, DCMD_PROC_MAPDEBUG, &map, sizeof (map), 0);
437 if (strcmp (map.info.path, printme.name))
440 /* Lower debug_vaddr is always text, if nessessary, swap. */
441 if ((int) map.info.vaddr < (int) printme.text.debug_vaddr)
443 memcpy (&(printme.data), &(printme.text),
444 sizeof (printme.data));
445 printme.text.addr = mapinfo_p2->vaddr;
446 printme.text.size = mapinfo_p2->size;
447 printme.text.flags = mapinfo_p2->flags;
448 printme.text.offset = mapinfo_p2->offset;
449 printme.text.debug_vaddr = map.info.vaddr;
453 printme.data.addr = mapinfo_p2->vaddr;
454 printme.data.size = mapinfo_p2->size;
455 printme.data.flags = mapinfo_p2->flags;
456 printme.data.offset = mapinfo_p2->offset;
457 printme.data.debug_vaddr = map.info.vaddr;
464 printf_filtered ("%s\n", printme.name);
465 printf_filtered ("\ttext=%08x bytes @ 0x%08x\n", printme.text.size,
467 printf_filtered ("\t\tflags=%08x\n", printme.text.flags);
468 printf_filtered ("\t\tdebug=%08x\n", printme.text.debug_vaddr);
469 printf_filtered ("\t\toffset=%016llx\n", printme.text.offset);
470 if (printme.data.size)
472 printf_filtered ("\tdata=%08x bytes @ 0x%08x\n", printme.data.size,
474 printf_filtered ("\t\tflags=%08x\n", printme.data.flags);
475 printf_filtered ("\t\tdebug=%08x\n", printme.data.debug_vaddr);
476 printf_filtered ("\t\toffset=%016llx\n", printme.data.offset);
478 printf_filtered ("\tdev=0x%x\n", printme.dev);
479 printf_filtered ("\tino=0x%x\n", (unsigned int) printme.ino);
485 /* Print status information about what we're accessing. */
487 procfs_files_info (struct target_ops *ignore)
489 printf_unfiltered ("\tUsing the running image of %s %s via %s.\n",
490 attach_flag ? "attached" : "child",
491 target_pid_to_str (inferior_ptid), nto_procfs_path);
494 /* Mark our target-struct as eligible for stray "run" and "attach" commands. */
501 /* Attach to process PID, then initialize for debugging it. */
503 procfs_attach (char *args, int from_tty)
509 error_no_arg ("process-id to attach");
513 if (pid == getpid ())
514 error ("Attaching GDB to itself is not a good idea...");
518 exec_file = (char *) get_exec_file (0);
521 printf_unfiltered ("Attaching to program `%s', %s\n", exec_file,
522 target_pid_to_str (pid_to_ptid (pid)));
524 printf_unfiltered ("Attaching to %s\n",
525 target_pid_to_str (pid_to_ptid (pid)));
527 gdb_flush (gdb_stdout);
529 inferior_ptid = do_attach (pid_to_ptid (pid));
530 push_target (&procfs_ops);
534 procfs_post_attach (pid_t pid)
536 #ifdef SOLIB_CREATE_INFERIOR_HOOK
538 SOLIB_CREATE_INFERIOR_HOOK (pid);
543 do_attach (ptid_t ptid)
545 procfs_status status;
546 struct sigevent event;
549 sprintf (path, "%s/%d/as", nto_procfs_path, PIDGET (ptid));
550 ctl_fd = open (path, O_RDWR);
552 error ("Couldn't open proc file %s, error %d (%s)", path, errno,
554 if (devctl (ctl_fd, DCMD_PROC_STOP, &status, sizeof (status), 0) != EOK)
555 error ("Couldn't stop process");
557 /* Define a sigevent for process stopped notification. */
558 event.sigev_notify = SIGEV_SIGNAL_THREAD;
559 event.sigev_signo = SIGUSR1;
560 event.sigev_code = 0;
561 event.sigev_value.sival_ptr = NULL;
562 event.sigev_priority = -1;
563 devctl (ctl_fd, DCMD_PROC_EVENT, &event, sizeof (event), 0);
565 if (devctl (ctl_fd, DCMD_PROC_STATUS, &status, sizeof (status), 0) == EOK
566 && status.flags & _DEBUG_FLAG_STOPPED)
567 SignalKill (nto_node(), PIDGET (ptid), 0, SIGCONT, 0, 0);
569 nto_init_solib_absolute_prefix ();
573 /* Ask the user what to do when an interrupt is received. */
577 target_terminal_ours ();
579 if (query ("Interrupted while waiting for the program.\n\
580 Give up (and stop debugging it)? "))
582 target_mourn_inferior ();
583 throw_exception (RETURN_QUIT);
586 target_terminal_inferior ();
589 /* The user typed ^C twice. */
591 nto_interrupt_twice (int signo)
593 signal (signo, ofunc);
595 signal (signo, nto_interrupt_twice);
599 nto_interrupt (int signo)
601 /* If this doesn't work, try more severe steps. */
602 signal (signo, nto_interrupt_twice);
608 procfs_wait (ptid_t ptid, struct target_waitstatus *ourstatus)
612 procfs_status status;
613 static int exit_signo = 0; /* To track signals that cause termination. */
615 ourstatus->kind = TARGET_WAITKIND_SPURIOUS;
617 if (ptid_equal (inferior_ptid, null_ptid))
619 ourstatus->kind = TARGET_WAITKIND_STOPPED;
620 ourstatus->value.sig = TARGET_SIGNAL_0;
626 sigaddset (&set, SIGUSR1);
628 devctl (ctl_fd, DCMD_PROC_STATUS, &status, sizeof (status), 0);
629 while (!(status.flags & _DEBUG_FLAG_ISTOP))
631 ofunc = (void (*)()) signal (SIGINT, nto_interrupt);
632 sigwaitinfo (&set, &info);
633 signal (SIGINT, ofunc);
634 devctl (ctl_fd, DCMD_PROC_STATUS, &status, sizeof (status), 0);
637 if (status.flags & _DEBUG_FLAG_SSTEP)
639 ourstatus->kind = TARGET_WAITKIND_STOPPED;
640 ourstatus->value.sig = TARGET_SIGNAL_TRAP;
642 /* Was it a breakpoint? */
643 else if (status.flags & _DEBUG_FLAG_TRACE)
645 ourstatus->kind = TARGET_WAITKIND_STOPPED;
646 ourstatus->value.sig = TARGET_SIGNAL_TRAP;
648 else if (status.flags & _DEBUG_FLAG_ISTOP)
652 case _DEBUG_WHY_SIGNALLED:
653 ourstatus->kind = TARGET_WAITKIND_STOPPED;
654 ourstatus->value.sig =
655 target_signal_from_host (status.info.si_signo);
658 case _DEBUG_WHY_FAULTED:
659 ourstatus->kind = TARGET_WAITKIND_STOPPED;
660 if (status.info.si_signo == SIGTRAP)
662 ourstatus->value.sig = 0;
667 ourstatus->value.sig =
668 target_signal_from_host (status.info.si_signo);
669 exit_signo = ourstatus->value.sig;
673 case _DEBUG_WHY_TERMINATED:
677 waitpid (PIDGET (inferior_ptid), &waitval, WNOHANG);
680 /* Abnormal death. */
681 ourstatus->kind = TARGET_WAITKIND_SIGNALLED;
682 ourstatus->value.sig = exit_signo;
687 ourstatus->kind = TARGET_WAITKIND_EXITED;
688 ourstatus->value.integer = WEXITSTATUS (waitval);
694 case _DEBUG_WHY_REQUESTED:
695 /* We are assuming a requested stop is due to a SIGINT. */
696 ourstatus->kind = TARGET_WAITKIND_STOPPED;
697 ourstatus->value.sig = TARGET_SIGNAL_INT;
703 return inferior_ptid;
706 /* Read the current values of the inferior's registers, both the
707 general register set and floating point registers (if supported)
708 and update gdb's idea of their current values. */
710 procfs_fetch_registers (int regno)
716 procfs_altreg altreg;
721 procfs_set_thread (inferior_ptid);
722 if (devctl (ctl_fd, DCMD_PROC_GETGREG, ®, sizeof (reg), ®size) == EOK)
723 nto_supply_gregset ((char *) ®.greg);
724 if (devctl (ctl_fd, DCMD_PROC_GETFPREG, ®, sizeof (reg), ®size)
726 nto_supply_fpregset ((char *) ®.fpreg);
727 if (devctl (ctl_fd, DCMD_PROC_GETALTREG, ®, sizeof (reg), ®size)
729 nto_supply_altregset ((char *) ®.altreg);
732 /* Copy LEN bytes to/from inferior's memory starting at MEMADDR
733 from/to debugger memory starting at MYADDR. Copy from inferior
734 if DOWRITE is zero or to inferior if DOWRITE is nonzero.
736 Returns the length copied, which is either the LEN argument or
737 zero. This xfer function does not do partial moves, since procfs_ops
738 doesn't allow memory operations to cross below us in the target stack
741 procfs_xfer_memory (CORE_ADDR memaddr, char *myaddr, int len, int dowrite,
742 struct mem_attrib *attrib, struct target_ops *target)
746 if (lseek (ctl_fd, (off_t) memaddr, SEEK_SET) == (off_t) memaddr)
749 nbytes = write (ctl_fd, myaddr, len);
751 nbytes = read (ctl_fd, myaddr, len);
758 /* Take a program previously attached to and detaches it.
759 The program resumes execution and will no longer stop
760 on signals, etc. We'd better not have left any breakpoints
761 in the program or it'll die when it hits one. */
763 procfs_detach (char *args, int from_tty)
769 char *exec_file = get_exec_file (0);
772 printf_unfiltered ("Detaching from program: %s %s\n",
773 exec_file, target_pid_to_str (inferior_ptid));
774 gdb_flush (gdb_stdout);
777 siggnal = atoi (args);
780 SignalKill (nto_node(), PIDGET (inferior_ptid), 0, siggnal, 0, 0);
785 inferior_ptid = null_ptid;
787 unpush_target (&procfs_ops); /* Pop out of handling an inferior. */
791 procfs_breakpoint (CORE_ADDR addr, int type, int size)
798 errno = devctl (ctl_fd, DCMD_PROC_BREAK, &brk, sizeof (brk), 0);
805 procfs_insert_breakpoint (CORE_ADDR addr, char *contents_cache)
807 return procfs_breakpoint (addr, _DEBUG_BREAK_EXEC, 0);
811 procfs_remove_breakpoint (CORE_ADDR addr, char *contents_cache)
813 return procfs_breakpoint (addr, _DEBUG_BREAK_EXEC, -1);
817 procfs_insert_hw_breakpoint (CORE_ADDR addr, char *contents_cache)
819 return procfs_breakpoint (addr, _DEBUG_BREAK_EXEC | _DEBUG_BREAK_HW, 0);
823 procfs_remove_hw_breakpoint (CORE_ADDR addr, char *contents_cache)
825 return procfs_breakpoint (addr, _DEBUG_BREAK_EXEC | _DEBUG_BREAK_HW, -1);
829 procfs_resume (ptid_t ptid, int step, enum target_signal signo)
832 procfs_status status;
834 if (ptid_equal (inferior_ptid, null_ptid))
837 procfs_set_thread (ptid_equal (ptid, minus_one_ptid) ? inferior_ptid :
840 run.flags = _DEBUG_RUN_FAULT | _DEBUG_RUN_TRACE;
842 run.flags |= _DEBUG_RUN_STEP;
844 sigemptyset ((sigset_t *) &run.fault);
845 sigaddset ((sigset_t *) &run.fault, FLTBPT);
846 sigaddset ((sigset_t *) &run.fault, FLTTRACE);
847 sigaddset ((sigset_t *) &run.fault, FLTILL);
848 sigaddset ((sigset_t *) &run.fault, FLTPRIV);
849 sigaddset ((sigset_t *) &run.fault, FLTBOUNDS);
850 sigaddset ((sigset_t *) &run.fault, FLTIOVF);
851 sigaddset ((sigset_t *) &run.fault, FLTIZDIV);
852 sigaddset ((sigset_t *) &run.fault, FLTFPE);
853 /* Peter V will be changing this at some point. */
854 sigaddset ((sigset_t *) &run.fault, FLTPAGE);
856 run.flags |= _DEBUG_RUN_ARM;
858 sigemptyset (&run.trace);
860 signal_to_pass = target_signal_to_host (signo);
864 devctl (ctl_fd, DCMD_PROC_STATUS, &status, sizeof (status), 0);
865 signal_to_pass = target_signal_to_host (signo);
866 if (status.why & (_DEBUG_WHY_SIGNALLED | _DEBUG_WHY_FAULTED))
868 if (signal_to_pass != status.info.si_signo)
870 SignalKill (nto_node(), PIDGET (inferior_ptid), 0, signal_to_pass,
872 run.flags |= _DEBUG_RUN_CLRFLT | _DEBUG_RUN_CLRSIG;
874 else /* Let it kill the program without telling us. */
875 sigdelset (&run.trace, signal_to_pass);
879 run.flags |= _DEBUG_RUN_CLRSIG | _DEBUG_RUN_CLRFLT;
881 errno = devctl (ctl_fd, DCMD_PROC_RUN, &run, sizeof (run), 0);
884 perror ("run error!\n");
890 procfs_mourn_inferior ()
892 if (!ptid_equal (inferior_ptid, null_ptid))
894 SignalKill (nto_node(), PIDGET (inferior_ptid), 0, SIGKILL, 0, 0);
897 inferior_ptid = null_ptid;
899 unpush_target (&procfs_ops);
900 generic_mourn_inferior ();
904 /* This function breaks up an argument string into an argument
905 vector suitable for passing to execvp().
906 E.g., on "run a b c d" this routine would get as input
907 the string "a b c d", and as output it would fill in argv with
908 the four arguments "a", "b", "c", "d". The only additional
909 functionality is simple quoting. The gdb command:
911 will fill in argv with the three args "a", "b c d", "e". */
913 breakup_args (char *scratch, char **argv)
915 char *pp, *cp = scratch;
920 /* Scan past leading separators. */
922 while (*cp == ' ' || *cp == '\t' || *cp == '\n')
925 /* Break if at end of string. */
933 quoting = strchr (cp, '"') ? 1 : 0;
938 /* Scan for next arg separator. */
941 cp = strchr (pp, '"');
942 if ((cp == NULL) || (!quoting))
943 cp = strchr (pp, ' ');
945 cp = strchr (pp, '\t');
947 cp = strchr (pp, '\n');
949 /* No separators => end of string => break. */
956 /* Replace the separator with a terminator. */
960 /* Execv requires a null-terminated arg vector. */
965 procfs_create_inferior (char *exec_file, char *allargs, char **env)
967 struct inheritance inherit;
971 char *in = "", *out = "", *err = "";
975 argv = xmalloc (((strlen (allargs) + 1) / (unsigned) 2 + 2) *
977 argv[0] = get_exec_file (1);
986 args = xstrdup (allargs);
987 breakup_args (args, exec_file ? &argv[1] : &argv[0]);
989 argv = nto_parse_redirection (argv, &in, &out, &err);
991 fds[0] = STDIN_FILENO;
992 fds[1] = STDOUT_FILENO;
993 fds[2] = STDERR_FILENO;
995 /* If the user specified I/O via gdb's --tty= arg, use it, but only
996 if the i/o is not also being specified via redirection. */
997 if (inferior_io_terminal)
1000 in = inferior_io_terminal;
1002 out = inferior_io_terminal;
1004 err = inferior_io_terminal;
1009 fd = open (in, O_RDONLY);
1017 fd = open (out, O_WRONLY);
1025 fd = open (err, O_WRONLY);
1032 /* Clear any pending SIGUSR1's but keep the behavior the same. */
1033 signal (SIGUSR1, signal (SIGUSR1, SIG_IGN));
1036 sigaddset (&set, SIGUSR1);
1037 sigprocmask (SIG_UNBLOCK, &set, NULL);
1039 memset (&inherit, 0, sizeof (inherit));
1041 if (ND_NODE_CMP (nto_procfs_node, ND_LOCAL_NODE) != 0)
1043 inherit.nd = nto_node();
1044 inherit.flags |= SPAWN_SETND;
1045 inherit.flags &= ~SPAWN_EXEC;
1047 inherit.flags |= SPAWN_SETGROUP | SPAWN_HOLD;
1048 inherit.pgroup = SPAWN_NEWPGROUP;
1049 pid = spawnp (argv[0], 3, fds, &inherit, argv,
1050 ND_NODE_CMP (nto_procfs_node, ND_LOCAL_NODE) == 0 ? env : 0);
1053 sigprocmask (SIG_BLOCK, &set, NULL);
1056 error ("Error spawning %s: %d (%s)", argv[0], errno, strerror (errno));
1058 if (fds[0] != STDIN_FILENO)
1060 if (fds[1] != STDOUT_FILENO)
1062 if (fds[2] != STDERR_FILENO)
1065 inferior_ptid = do_attach (pid_to_ptid (pid));
1068 flags = _DEBUG_FLAG_KLC; /* Kill-on-Last-Close flag. */
1069 errn = devctl (ctl_fd, DCMD_PROC_SET_FLAG, &flags, sizeof (flags), 0);
1072 /* FIXME: expected warning? */
1073 /* warning( "Failed to set Kill-on-Last-Close flag: errno = %d(%s)\n",
1074 errn, strerror(errn) ); */
1076 push_target (&procfs_ops);
1077 target_terminal_init ();
1079 #ifdef SOLIB_CREATE_INFERIOR_HOOK
1080 if (exec_bfd != NULL
1081 || (symfile_objfile != NULL && symfile_objfile->obfd != NULL))
1082 SOLIB_CREATE_INFERIOR_HOOK (pid);
1089 devctl (ctl_fd, DCMD_PROC_STOP, NULL, 0, 0);
1093 procfs_kill_inferior ()
1095 target_mourn_inferior ();
1098 /* Store register REGNO, or all registers if REGNO == -1, from the contents
1101 procfs_prepare_to_store ()
1105 /* Fill buf with regset and return devctl cmd to do the setting. Return
1106 -1 if we fail to get the regset. Store size of regset in regsize. */
1108 get_regset (int regset, char *buf, int bufsize, int *regsize)
1110 int dev_get, dev_set;
1113 case NTO_REG_GENERAL:
1114 dev_get = DCMD_PROC_GETGREG;
1115 dev_set = DCMD_PROC_SETGREG;
1119 dev_get = DCMD_PROC_GETFPREG;
1120 dev_set = DCMD_PROC_SETFPREG;
1124 dev_get = DCMD_PROC_GETALTREG;
1125 dev_set = DCMD_PROC_SETALTREG;
1128 case NTO_REG_SYSTEM:
1132 if (devctl (ctl_fd, dev_get, &buf, bufsize, regsize) != EOK)
1139 procfs_store_registers (int regno)
1145 procfs_altreg altreg;
1149 int len, regset, regsize, dev_set, err;
1152 if (ptid_equal (inferior_ptid, null_ptid))
1154 procfs_set_thread (inferior_ptid);
1158 for (regset = NTO_REG_GENERAL; regset < NTO_REG_END; regset++)
1160 dev_set = get_regset (regset, (char *) ®,
1161 sizeof (reg), ®size);
1165 if (nto_regset_fill (regset, (char *) ®) == -1)
1168 err = devctl (ctl_fd, dev_set, ®, regsize, 0);
1170 fprintf_unfiltered (gdb_stderr,
1171 "Warning unable to write regset %d: %s\n",
1172 regno, strerror (err));
1177 regset = nto_regset_id (regno);
1181 dev_set = get_regset (regset, (char *) ®, sizeof (reg), ®size);
1185 len = nto_register_area (regno, regset, &off);
1190 regcache_collect (regno, (char *) ® + off);
1192 err = devctl (ctl_fd, dev_set, ®, regsize, 0);
1194 fprintf_unfiltered (gdb_stderr,
1195 "Warning unable to write regset %d: %s\n", regno,
1201 notice_signals (void)
1205 for (signo = 1; signo < NSIG; signo++)
1207 if (signal_stop_state (target_signal_from_host (signo)) == 0
1208 && signal_print_state (target_signal_from_host (signo)) == 0
1209 && signal_pass_state (target_signal_from_host (signo)) == 1)
1210 sigdelset (&run.trace, signo);
1212 sigaddset (&run.trace, signo);
1216 /* When the user changes the state of gdb's signal handling via the
1217 "handle" command, this function gets called to see if any change
1218 in the /proc interface is required. It is also called internally
1219 by other /proc interface functions to initialize the state of
1220 the traced signal set. */
1222 procfs_notice_signals (ptid_t ptid)
1224 sigemptyset (&run.trace);
1228 static struct tidinfo *
1229 procfs_thread_info (pid_t pid, short tid)
1236 procfs_pid_to_str (ptid_t ptid)
1238 static char buf[1024];
1240 struct tidinfo *tip;
1242 pid = ptid_get_pid (ptid);
1243 tid = ptid_get_tid (ptid);
1245 n = sprintf (buf, "process %d", pid);
1248 tip = procfs_thread_info (pid, tid);
1250 sprintf (&buf[n], " (state = 0x%02x)", tip->state);
1259 procfs_ops.to_shortname = "procfs";
1260 procfs_ops.to_longname = "QNX Neutrino procfs child process";
1262 "QNX Neutrino procfs child process (started by the \"run\" command).\n\
1263 target procfs <node>";
1264 procfs_ops.to_open = procfs_open;
1265 procfs_ops.to_attach = procfs_attach;
1266 procfs_ops.to_post_attach = procfs_post_attach;
1267 procfs_ops.to_detach = procfs_detach;
1268 procfs_ops.to_resume = procfs_resume;
1269 procfs_ops.to_wait = procfs_wait;
1270 procfs_ops.to_fetch_registers = procfs_fetch_registers;
1271 procfs_ops.to_store_registers = procfs_store_registers;
1272 procfs_ops.to_prepare_to_store = procfs_prepare_to_store;
1273 procfs_ops.to_xfer_memory = procfs_xfer_memory;
1274 procfs_ops.to_files_info = procfs_files_info;
1275 procfs_ops.to_insert_breakpoint = procfs_insert_breakpoint;
1276 procfs_ops.to_remove_breakpoint = procfs_remove_breakpoint;
1277 procfs_ops.to_can_use_hw_breakpoint = procfs_can_use_hw_breakpoint;
1278 procfs_ops.to_insert_hw_breakpoint = procfs_insert_hw_breakpoint;
1279 procfs_ops.to_remove_hw_breakpoint = procfs_remove_breakpoint;
1280 procfs_ops.to_insert_watchpoint = procfs_insert_hw_watchpoint;
1281 procfs_ops.to_remove_watchpoint = procfs_remove_hw_watchpoint;
1282 procfs_ops.to_stopped_by_watchpoint = procfs_stopped_by_watchpoint;
1283 procfs_ops.to_terminal_init = terminal_init_inferior;
1284 procfs_ops.to_terminal_inferior = terminal_inferior;
1285 procfs_ops.to_terminal_ours_for_output = terminal_ours_for_output;
1286 procfs_ops.to_terminal_ours = terminal_ours;
1287 procfs_ops.to_terminal_info = child_terminal_info;
1288 procfs_ops.to_kill = procfs_kill_inferior;
1289 procfs_ops.to_create_inferior = procfs_create_inferior;
1290 procfs_ops.to_mourn_inferior = procfs_mourn_inferior;
1291 procfs_ops.to_can_run = procfs_can_run;
1292 procfs_ops.to_notice_signals = procfs_notice_signals;
1293 procfs_ops.to_thread_alive = procfs_thread_alive;
1294 procfs_ops.to_find_new_threads = procfs_find_new_threads;
1295 procfs_ops.to_pid_to_str = procfs_pid_to_str;
1296 procfs_ops.to_stop = procfs_stop;
1297 procfs_ops.to_stratum = process_stratum;
1298 procfs_ops.to_has_all_memory = 1;
1299 procfs_ops.to_has_memory = 1;
1300 procfs_ops.to_has_stack = 1;
1301 procfs_ops.to_has_registers = 1;
1302 procfs_ops.to_has_execution = 1;
1303 procfs_ops.to_magic = OPS_MAGIC;
1304 procfs_ops.to_have_continuable_watchpoint = 1;
1307 #define OSTYPE_NTO 1
1310 _initialize_procfs ()
1315 add_target (&procfs_ops);
1317 /* We use SIGUSR1 to gain control after we block waiting for a process.
1318 We use sigwaitevent to wait. */
1320 sigaddset (&set, SIGUSR1);
1321 sigprocmask (SIG_BLOCK, &set, NULL);
1323 /* Set up trace and fault sets, as gdb expects them. */
1324 sigemptyset (&run.trace);
1327 /* Stuff some information. */
1328 nto_cpuinfo_flags = SYSPAGE_ENTRY (cpuinfo)->flags;
1329 nto_cpuinfo_valid = 1;
1331 add_info ("pidlist", procfs_pidlist, "pidlist");
1332 add_info ("meminfo", procfs_meminfo, "memory information");
1337 procfs_hw_watchpoint (int addr, int len, int type)
1344 brk.type = _DEBUG_BREAK_RD;
1346 case 2: /* Read/Write. */
1347 brk.type = _DEBUG_BREAK_RW;
1349 default: /* Modify. */
1350 /* FIXME: brk.type = _DEBUG_BREAK_RWM gives EINVAL for some reason. */
1351 brk.type = _DEBUG_BREAK_RW;
1353 brk.type |= _DEBUG_BREAK_HW; /* Always ask for HW. */
1357 errno = devctl (ctl_fd, DCMD_PROC_BREAK, &brk, sizeof (brk), 0);
1360 perror ("Failed to set hardware watchpoint");
1367 procfs_can_use_hw_breakpoint (int type, int cnt, int othertype)
1373 procfs_remove_hw_watchpoint (CORE_ADDR addr, int len, int type)
1375 return procfs_hw_watchpoint (addr, -1, type);
1379 procfs_insert_hw_watchpoint (CORE_ADDR addr, int len, int type)
1381 return procfs_hw_watchpoint (addr, len, type);
1385 procfs_stopped_by_watchpoint (void)