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>
33 #include "gdb_dirent.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, safe_strerror (errno));
146 nto_procfs_node = ND_LOCAL_NODE;
150 if (*(endstr - 1) == '/')
156 snprintf (nto_procfs_path, PATH_MAX - 1, "%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,
164 safe_strerror (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,
172 safe_strerror (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,
183 safe_strerror (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,
192 safe_strerror (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 fprintf_unfiltered (gdb_stderr, "failed to opendir \"%s\" - %d (%s)",
274 nto_procfs_path, errno, safe_strerror (errno));
278 /* Start scan at first pid. */
283 /* Get the right pid and procfs path for the pid. */
292 snprintf (buf, 511, "%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 fprintf_unfiltered (gdb_stderr, "failed to open %s - %d (%s)\n",
302 buf, errno, safe_strerror (errno));
307 pidinfo = (procfs_info *) buf;
308 if (devctl (fd, DCMD_PROC_INFO, pidinfo, sizeof (buf), 0) != EOK)
310 fprintf_unfiltered (gdb_stderr,
311 "devctl DCMD_PROC_INFO failed - %d (%s)\n", errno,
312 safe_strerror (errno));
315 num_threads = pidinfo->num_threads;
317 info = (procfs_debuginfo *) buf;
318 if (devctl (fd, DCMD_PROC_MAPDEBUG_BASE, info, sizeof (buf), 0) != EOK)
319 strcpy (name, "unavailable");
321 strcpy (name, info->path);
323 /* Collect state info on all the threads. */
324 status = (procfs_status *) buf;
325 for (status->tid = 1; status->tid <= num_threads; status->tid++)
327 if (devctl (fd, DCMD_PROC_TIDSTATUS, status, sizeof (buf), 0) != EOK
330 if (status->tid != 0)
331 printf_filtered ("%s - %d/%d\n", name, pid, status->tid);
335 while (dirp != NULL);
343 procfs_meminfo (char *args, int from_tty)
345 procfs_mapinfo *mapinfos = NULL;
346 static int num_mapinfos = 0;
347 procfs_mapinfo *mapinfo_p, *mapinfo_p2;
348 int flags = ~0, err, num, i, j;
352 procfs_debuginfo info;
353 char buff[_POSIX_PATH_MAX];
361 unsigned debug_vaddr;
362 unsigned long long offset;
367 unsigned long long ino;
374 /* Get the number of map entrys. */
375 err = devctl (ctl_fd, DCMD_PROC_MAPINFO, NULL, 0, &num);
378 printf ("failed devctl num mapinfos - %d (%s)\n", err, safe_strerror (err));
382 mapinfos = xmalloc (num * sizeof (procfs_mapinfo));
385 mapinfo_p = mapinfos;
387 /* Fill the map entrys. */
388 err = devctl (ctl_fd, DCMD_PROC_MAPINFO, mapinfo_p, num
389 * sizeof (procfs_mapinfo), &num);
392 printf ("failed devctl mapinfos - %d (%s)\n", err, safe_strerror (err));
397 num = min (num, num_mapinfos);
399 /* Run through the list of mapinfos, and store the data and text info
400 so we can print it at the bottom of the loop. */
401 for (mapinfo_p = mapinfos, i = 0; i < num; i++, mapinfo_p++)
403 if (!(mapinfo_p->flags & flags))
406 if (mapinfo_p->ino == 0) /* Already visited. */
409 map.info.vaddr = mapinfo_p->vaddr;
411 err = devctl (ctl_fd, DCMD_PROC_MAPDEBUG, &map, sizeof (map), 0);
415 memset (&printme, 0, sizeof printme);
416 printme.dev = mapinfo_p->dev;
417 printme.ino = mapinfo_p->ino;
418 printme.text.addr = mapinfo_p->vaddr;
419 printme.text.size = mapinfo_p->size;
420 printme.text.flags = mapinfo_p->flags;
421 printme.text.offset = mapinfo_p->offset;
422 printme.text.debug_vaddr = map.info.vaddr;
423 strcpy (printme.name, map.info.path);
425 /* Check for matching data. */
426 for (mapinfo_p2 = mapinfos, j = 0; j < num; j++, mapinfo_p2++)
428 if (mapinfo_p2->vaddr != mapinfo_p->vaddr
429 && mapinfo_p2->ino == mapinfo_p->ino
430 && mapinfo_p2->dev == mapinfo_p->dev)
432 map.info.vaddr = mapinfo_p2->vaddr;
434 devctl (ctl_fd, DCMD_PROC_MAPDEBUG, &map, sizeof (map), 0);
438 if (strcmp (map.info.path, printme.name))
441 /* Lower debug_vaddr is always text, if nessessary, swap. */
442 if ((int) map.info.vaddr < (int) printme.text.debug_vaddr)
444 memcpy (&(printme.data), &(printme.text),
445 sizeof (printme.data));
446 printme.text.addr = mapinfo_p2->vaddr;
447 printme.text.size = mapinfo_p2->size;
448 printme.text.flags = mapinfo_p2->flags;
449 printme.text.offset = mapinfo_p2->offset;
450 printme.text.debug_vaddr = map.info.vaddr;
454 printme.data.addr = mapinfo_p2->vaddr;
455 printme.data.size = mapinfo_p2->size;
456 printme.data.flags = mapinfo_p2->flags;
457 printme.data.offset = mapinfo_p2->offset;
458 printme.data.debug_vaddr = map.info.vaddr;
465 printf_filtered ("%s\n", printme.name);
466 printf_filtered ("\ttext=%08x bytes @ 0x%08x\n", printme.text.size,
468 printf_filtered ("\t\tflags=%08x\n", printme.text.flags);
469 printf_filtered ("\t\tdebug=%08x\n", printme.text.debug_vaddr);
470 printf_filtered ("\t\toffset=%016llx\n", printme.text.offset);
471 if (printme.data.size)
473 printf_filtered ("\tdata=%08x bytes @ 0x%08x\n", printme.data.size,
475 printf_filtered ("\t\tflags=%08x\n", printme.data.flags);
476 printf_filtered ("\t\tdebug=%08x\n", printme.data.debug_vaddr);
477 printf_filtered ("\t\toffset=%016llx\n", printme.data.offset);
479 printf_filtered ("\tdev=0x%x\n", printme.dev);
480 printf_filtered ("\tino=0x%x\n", (unsigned int) printme.ino);
486 /* Print status information about what we're accessing. */
488 procfs_files_info (struct target_ops *ignore)
490 printf_unfiltered ("\tUsing the running image of %s %s via %s.\n",
491 attach_flag ? "attached" : "child",
492 target_pid_to_str (inferior_ptid), nto_procfs_path);
495 /* Mark our target-struct as eligible for stray "run" and "attach" commands. */
497 procfs_can_run (void)
502 /* Attach to process PID, then initialize for debugging it. */
504 procfs_attach (char *args, int from_tty)
510 error_no_arg ("process-id to attach");
514 if (pid == getpid ())
515 error ("Attaching GDB to itself is not a good idea...");
519 exec_file = (char *) get_exec_file (0);
522 printf_unfiltered ("Attaching to program `%s', %s\n", exec_file,
523 target_pid_to_str (pid_to_ptid (pid)));
525 printf_unfiltered ("Attaching to %s\n",
526 target_pid_to_str (pid_to_ptid (pid)));
528 gdb_flush (gdb_stdout);
530 inferior_ptid = do_attach (pid_to_ptid (pid));
531 push_target (&procfs_ops);
535 procfs_post_attach (pid_t pid)
537 #ifdef SOLIB_CREATE_INFERIOR_HOOK
539 SOLIB_CREATE_INFERIOR_HOOK (pid);
544 do_attach (ptid_t ptid)
546 procfs_status status;
547 struct sigevent event;
550 snprintf (path, PATH_MAX - 1, "%s/%d/as", nto_procfs_path, PIDGET (ptid));
551 ctl_fd = open (path, O_RDWR);
553 error ("Couldn't open proc file %s, error %d (%s)", path, errno,
554 safe_strerror (errno));
555 if (devctl (ctl_fd, DCMD_PROC_STOP, &status, sizeof (status), 0) != EOK)
556 error ("Couldn't stop process");
558 /* Define a sigevent for process stopped notification. */
559 event.sigev_notify = SIGEV_SIGNAL_THREAD;
560 event.sigev_signo = SIGUSR1;
561 event.sigev_code = 0;
562 event.sigev_value.sival_ptr = NULL;
563 event.sigev_priority = -1;
564 devctl (ctl_fd, DCMD_PROC_EVENT, &event, sizeof (event), 0);
566 if (devctl (ctl_fd, DCMD_PROC_STATUS, &status, sizeof (status), 0) == EOK
567 && status.flags & _DEBUG_FLAG_STOPPED)
568 SignalKill (nto_node(), PIDGET (ptid), 0, SIGCONT, 0, 0);
570 nto_init_solib_absolute_prefix ();
574 /* Ask the user what to do when an interrupt is received. */
576 interrupt_query (void)
578 target_terminal_ours ();
580 if (query ("Interrupted while waiting for the program.\n\
581 Give up (and stop debugging it)? "))
583 target_mourn_inferior ();
584 throw_exception (RETURN_QUIT);
587 target_terminal_inferior ();
590 /* The user typed ^C twice. */
592 nto_interrupt_twice (int signo)
594 signal (signo, ofunc);
596 signal (signo, nto_interrupt_twice);
600 nto_interrupt (int signo)
602 /* If this doesn't work, try more severe steps. */
603 signal (signo, nto_interrupt_twice);
609 procfs_wait (ptid_t ptid, struct target_waitstatus *ourstatus)
613 procfs_status status;
614 static int exit_signo = 0; /* To track signals that cause termination. */
616 ourstatus->kind = TARGET_WAITKIND_SPURIOUS;
618 if (ptid_equal (inferior_ptid, null_ptid))
620 ourstatus->kind = TARGET_WAITKIND_STOPPED;
621 ourstatus->value.sig = TARGET_SIGNAL_0;
627 sigaddset (&set, SIGUSR1);
629 devctl (ctl_fd, DCMD_PROC_STATUS, &status, sizeof (status), 0);
630 while (!(status.flags & _DEBUG_FLAG_ISTOP))
632 ofunc = (void (*)()) signal (SIGINT, nto_interrupt);
633 sigwaitinfo (&set, &info);
634 signal (SIGINT, ofunc);
635 devctl (ctl_fd, DCMD_PROC_STATUS, &status, sizeof (status), 0);
638 if (status.flags & _DEBUG_FLAG_SSTEP)
640 ourstatus->kind = TARGET_WAITKIND_STOPPED;
641 ourstatus->value.sig = TARGET_SIGNAL_TRAP;
643 /* Was it a breakpoint? */
644 else if (status.flags & _DEBUG_FLAG_TRACE)
646 ourstatus->kind = TARGET_WAITKIND_STOPPED;
647 ourstatus->value.sig = TARGET_SIGNAL_TRAP;
649 else if (status.flags & _DEBUG_FLAG_ISTOP)
653 case _DEBUG_WHY_SIGNALLED:
654 ourstatus->kind = TARGET_WAITKIND_STOPPED;
655 ourstatus->value.sig =
656 target_signal_from_host (status.info.si_signo);
659 case _DEBUG_WHY_FAULTED:
660 ourstatus->kind = TARGET_WAITKIND_STOPPED;
661 if (status.info.si_signo == SIGTRAP)
663 ourstatus->value.sig = 0;
668 ourstatus->value.sig =
669 target_signal_from_host (status.info.si_signo);
670 exit_signo = ourstatus->value.sig;
674 case _DEBUG_WHY_TERMINATED:
678 waitpid (PIDGET (inferior_ptid), &waitval, WNOHANG);
681 /* Abnormal death. */
682 ourstatus->kind = TARGET_WAITKIND_SIGNALLED;
683 ourstatus->value.sig = exit_signo;
688 ourstatus->kind = TARGET_WAITKIND_EXITED;
689 ourstatus->value.integer = WEXITSTATUS (waitval);
695 case _DEBUG_WHY_REQUESTED:
696 /* We are assuming a requested stop is due to a SIGINT. */
697 ourstatus->kind = TARGET_WAITKIND_STOPPED;
698 ourstatus->value.sig = TARGET_SIGNAL_INT;
704 return inferior_ptid;
707 /* Read the current values of the inferior's registers, both the
708 general register set and floating point registers (if supported)
709 and update gdb's idea of their current values. */
711 procfs_fetch_registers (int regno)
717 procfs_altreg altreg;
722 procfs_set_thread (inferior_ptid);
723 if (devctl (ctl_fd, DCMD_PROC_GETGREG, ®, sizeof (reg), ®size) == EOK)
724 nto_supply_gregset ((char *) ®.greg);
725 if (devctl (ctl_fd, DCMD_PROC_GETFPREG, ®, sizeof (reg), ®size)
727 nto_supply_fpregset ((char *) ®.fpreg);
728 if (devctl (ctl_fd, DCMD_PROC_GETALTREG, ®, sizeof (reg), ®size)
730 nto_supply_altregset ((char *) ®.altreg);
733 /* Copy LEN bytes to/from inferior's memory starting at MEMADDR
734 from/to debugger memory starting at MYADDR. Copy from inferior
735 if DOWRITE is zero or to inferior if DOWRITE is nonzero.
737 Returns the length copied, which is either the LEN argument or
738 zero. This xfer function does not do partial moves, since procfs_ops
739 doesn't allow memory operations to cross below us in the target stack
742 procfs_xfer_memory (CORE_ADDR memaddr, char *myaddr, int len, int dowrite,
743 struct mem_attrib *attrib, struct target_ops *target)
747 if (lseek (ctl_fd, (off_t) memaddr, SEEK_SET) == (off_t) memaddr)
750 nbytes = write (ctl_fd, myaddr, len);
752 nbytes = read (ctl_fd, myaddr, len);
759 /* Take a program previously attached to and detaches it.
760 The program resumes execution and will no longer stop
761 on signals, etc. We'd better not have left any breakpoints
762 in the program or it'll die when it hits one. */
764 procfs_detach (char *args, int from_tty)
770 char *exec_file = get_exec_file (0);
773 printf_unfiltered ("Detaching from program: %s %s\n",
774 exec_file, target_pid_to_str (inferior_ptid));
775 gdb_flush (gdb_stdout);
778 siggnal = atoi (args);
781 SignalKill (nto_node(), PIDGET (inferior_ptid), 0, siggnal, 0, 0);
786 inferior_ptid = null_ptid;
788 unpush_target (&procfs_ops); /* Pop out of handling an inferior. */
792 procfs_breakpoint (CORE_ADDR addr, int type, int size)
799 errno = devctl (ctl_fd, DCMD_PROC_BREAK, &brk, sizeof (brk), 0);
806 procfs_insert_breakpoint (CORE_ADDR addr, char *contents_cache)
808 return procfs_breakpoint (addr, _DEBUG_BREAK_EXEC, 0);
812 procfs_remove_breakpoint (CORE_ADDR addr, char *contents_cache)
814 return procfs_breakpoint (addr, _DEBUG_BREAK_EXEC, -1);
818 procfs_insert_hw_breakpoint (CORE_ADDR addr, char *contents_cache)
820 return procfs_breakpoint (addr, _DEBUG_BREAK_EXEC | _DEBUG_BREAK_HW, 0);
824 procfs_remove_hw_breakpoint (CORE_ADDR addr, char *contents_cache)
826 return procfs_breakpoint (addr, _DEBUG_BREAK_EXEC | _DEBUG_BREAK_HW, -1);
830 procfs_resume (ptid_t ptid, int step, enum target_signal signo)
833 procfs_status status;
835 if (ptid_equal (inferior_ptid, null_ptid))
838 procfs_set_thread (ptid_equal (ptid, minus_one_ptid) ? inferior_ptid :
841 run.flags = _DEBUG_RUN_FAULT | _DEBUG_RUN_TRACE;
843 run.flags |= _DEBUG_RUN_STEP;
845 sigemptyset ((sigset_t *) &run.fault);
846 sigaddset ((sigset_t *) &run.fault, FLTBPT);
847 sigaddset ((sigset_t *) &run.fault, FLTTRACE);
848 sigaddset ((sigset_t *) &run.fault, FLTILL);
849 sigaddset ((sigset_t *) &run.fault, FLTPRIV);
850 sigaddset ((sigset_t *) &run.fault, FLTBOUNDS);
851 sigaddset ((sigset_t *) &run.fault, FLTIOVF);
852 sigaddset ((sigset_t *) &run.fault, FLTIZDIV);
853 sigaddset ((sigset_t *) &run.fault, FLTFPE);
854 /* Peter V will be changing this at some point. */
855 sigaddset ((sigset_t *) &run.fault, FLTPAGE);
857 run.flags |= _DEBUG_RUN_ARM;
859 sigemptyset (&run.trace);
861 signal_to_pass = target_signal_to_host (signo);
865 devctl (ctl_fd, DCMD_PROC_STATUS, &status, sizeof (status), 0);
866 signal_to_pass = target_signal_to_host (signo);
867 if (status.why & (_DEBUG_WHY_SIGNALLED | _DEBUG_WHY_FAULTED))
869 if (signal_to_pass != status.info.si_signo)
871 SignalKill (nto_node(), PIDGET (inferior_ptid), 0, signal_to_pass,
873 run.flags |= _DEBUG_RUN_CLRFLT | _DEBUG_RUN_CLRSIG;
875 else /* Let it kill the program without telling us. */
876 sigdelset (&run.trace, signal_to_pass);
880 run.flags |= _DEBUG_RUN_CLRSIG | _DEBUG_RUN_CLRFLT;
882 errno = devctl (ctl_fd, DCMD_PROC_RUN, &run, sizeof (run), 0);
885 perror ("run error!\n");
891 procfs_mourn_inferior (void)
893 if (!ptid_equal (inferior_ptid, null_ptid))
895 SignalKill (nto_node(), PIDGET (inferior_ptid), 0, SIGKILL, 0, 0);
898 inferior_ptid = null_ptid;
900 unpush_target (&procfs_ops);
901 generic_mourn_inferior ();
905 /* This function breaks up an argument string into an argument
906 vector suitable for passing to execvp().
907 E.g., on "run a b c d" this routine would get as input
908 the string "a b c d", and as output it would fill in argv with
909 the four arguments "a", "b", "c", "d". The only additional
910 functionality is simple quoting. The gdb command:
912 will fill in argv with the three args "a", "b c d", "e". */
914 breakup_args (char *scratch, char **argv)
916 char *pp, *cp = scratch;
921 /* Scan past leading separators. */
923 while (*cp == ' ' || *cp == '\t' || *cp == '\n')
926 /* Break if at end of string. */
934 quoting = strchr (cp, '"') ? 1 : 0;
939 /* Scan for next arg separator. */
942 cp = strchr (pp, '"');
943 if ((cp == NULL) || (!quoting))
944 cp = strchr (pp, ' ');
946 cp = strchr (pp, '\t');
948 cp = strchr (pp, '\n');
950 /* No separators => end of string => break. */
957 /* Replace the separator with a terminator. */
961 /* Execv requires a null-terminated arg vector. */
966 procfs_create_inferior (char *exec_file, char *allargs, char **env)
968 struct inheritance inherit;
972 char *in = "", *out = "", *err = "";
976 argv = xmalloc (((strlen (allargs) + 1) / (unsigned) 2 + 2) *
978 argv[0] = get_exec_file (1);
987 args = xstrdup (allargs);
988 breakup_args (args, exec_file ? &argv[1] : &argv[0]);
990 argv = nto_parse_redirection (argv, &in, &out, &err);
992 fds[0] = STDIN_FILENO;
993 fds[1] = STDOUT_FILENO;
994 fds[2] = STDERR_FILENO;
996 /* If the user specified I/O via gdb's --tty= arg, use it, but only
997 if the i/o is not also being specified via redirection. */
998 if (inferior_io_terminal)
1001 in = inferior_io_terminal;
1003 out = inferior_io_terminal;
1005 err = inferior_io_terminal;
1010 fd = open (in, O_RDONLY);
1018 fd = open (out, O_WRONLY);
1026 fd = open (err, O_WRONLY);
1033 /* Clear any pending SIGUSR1's but keep the behavior the same. */
1034 signal (SIGUSR1, signal (SIGUSR1, SIG_IGN));
1037 sigaddset (&set, SIGUSR1);
1038 sigprocmask (SIG_UNBLOCK, &set, NULL);
1040 memset (&inherit, 0, sizeof (inherit));
1042 if (ND_NODE_CMP (nto_procfs_node, ND_LOCAL_NODE) != 0)
1044 inherit.nd = nto_node();
1045 inherit.flags |= SPAWN_SETND;
1046 inherit.flags &= ~SPAWN_EXEC;
1048 inherit.flags |= SPAWN_SETGROUP | SPAWN_HOLD;
1049 inherit.pgroup = SPAWN_NEWPGROUP;
1050 pid = spawnp (argv[0], 3, fds, &inherit, argv,
1051 ND_NODE_CMP (nto_procfs_node, ND_LOCAL_NODE) == 0 ? env : 0);
1054 sigprocmask (SIG_BLOCK, &set, NULL);
1057 error ("Error spawning %s: %d (%s)", argv[0], errno, safe_strerror (errno));
1059 if (fds[0] != STDIN_FILENO)
1061 if (fds[1] != STDOUT_FILENO)
1063 if (fds[2] != STDERR_FILENO)
1066 inferior_ptid = do_attach (pid_to_ptid (pid));
1069 flags = _DEBUG_FLAG_KLC; /* Kill-on-Last-Close flag. */
1070 errn = devctl (ctl_fd, DCMD_PROC_SET_FLAG, &flags, sizeof (flags), 0);
1073 /* FIXME: expected warning? */
1074 /* warning( "Failed to set Kill-on-Last-Close flag: errno = %d(%s)\n",
1075 errn, strerror(errn) ); */
1077 push_target (&procfs_ops);
1078 target_terminal_init ();
1080 #ifdef SOLIB_CREATE_INFERIOR_HOOK
1081 if (exec_bfd != NULL
1082 || (symfile_objfile != NULL && symfile_objfile->obfd != NULL))
1083 SOLIB_CREATE_INFERIOR_HOOK (pid);
1090 devctl (ctl_fd, DCMD_PROC_STOP, NULL, 0, 0);
1094 procfs_kill_inferior (void)
1096 target_mourn_inferior ();
1099 /* Store register REGNO, or all registers if REGNO == -1, from the contents
1102 procfs_prepare_to_store (void)
1106 /* Fill buf with regset and return devctl cmd to do the setting. Return
1107 -1 if we fail to get the regset. Store size of regset in regsize. */
1109 get_regset (int regset, char *buf, int bufsize, int *regsize)
1111 int dev_get, dev_set;
1114 case NTO_REG_GENERAL:
1115 dev_get = DCMD_PROC_GETGREG;
1116 dev_set = DCMD_PROC_SETGREG;
1120 dev_get = DCMD_PROC_GETFPREG;
1121 dev_set = DCMD_PROC_SETFPREG;
1125 dev_get = DCMD_PROC_GETALTREG;
1126 dev_set = DCMD_PROC_SETALTREG;
1129 case NTO_REG_SYSTEM:
1133 if (devctl (ctl_fd, dev_get, &buf, bufsize, regsize) != EOK)
1140 procfs_store_registers (int regno)
1146 procfs_altreg altreg;
1150 int len, regset, regsize, dev_set, err;
1153 if (ptid_equal (inferior_ptid, null_ptid))
1155 procfs_set_thread (inferior_ptid);
1159 for (regset = NTO_REG_GENERAL; regset < NTO_REG_END; regset++)
1161 dev_set = get_regset (regset, (char *) ®,
1162 sizeof (reg), ®size);
1166 if (nto_regset_fill (regset, (char *) ®) == -1)
1169 err = devctl (ctl_fd, dev_set, ®, regsize, 0);
1171 fprintf_unfiltered (gdb_stderr,
1172 "Warning unable to write regset %d: %s\n",
1173 regno, safe_strerror (err));
1178 regset = nto_regset_id (regno);
1182 dev_set = get_regset (regset, (char *) ®, sizeof (reg), ®size);
1186 len = nto_register_area (regno, regset, &off);
1191 regcache_collect (regno, (char *) ® + off);
1193 err = devctl (ctl_fd, dev_set, ®, regsize, 0);
1195 fprintf_unfiltered (gdb_stderr,
1196 "Warning unable to write regset %d: %s\n", regno,
1197 safe_strerror (err));
1202 notice_signals (void)
1206 for (signo = 1; signo < NSIG; signo++)
1208 if (signal_stop_state (target_signal_from_host (signo)) == 0
1209 && signal_print_state (target_signal_from_host (signo)) == 0
1210 && signal_pass_state (target_signal_from_host (signo)) == 1)
1211 sigdelset (&run.trace, signo);
1213 sigaddset (&run.trace, signo);
1217 /* When the user changes the state of gdb's signal handling via the
1218 "handle" command, this function gets called to see if any change
1219 in the /proc interface is required. It is also called internally
1220 by other /proc interface functions to initialize the state of
1221 the traced signal set. */
1223 procfs_notice_signals (ptid_t ptid)
1225 sigemptyset (&run.trace);
1229 static struct tidinfo *
1230 procfs_thread_info (pid_t pid, short tid)
1237 procfs_pid_to_str (ptid_t ptid)
1239 static char buf[1024];
1241 struct tidinfo *tip;
1243 pid = ptid_get_pid (ptid);
1244 tid = ptid_get_tid (ptid);
1246 n = snprintf (buf, 1023, "process %d", pid);
1249 tip = procfs_thread_info (pid, tid);
1251 snprintf (&buf[n], 1023, " (state = 0x%02x)", tip->state);
1258 init_procfs_ops (void)
1260 procfs_ops.to_shortname = "procfs";
1261 procfs_ops.to_longname = "QNX Neutrino procfs child process";
1263 "QNX Neutrino procfs child process (started by the \"run\" command).\n\
1264 target procfs <node>";
1265 procfs_ops.to_open = procfs_open;
1266 procfs_ops.to_attach = procfs_attach;
1267 procfs_ops.to_post_attach = procfs_post_attach;
1268 procfs_ops.to_detach = procfs_detach;
1269 procfs_ops.to_resume = procfs_resume;
1270 procfs_ops.to_wait = procfs_wait;
1271 procfs_ops.to_fetch_registers = procfs_fetch_registers;
1272 procfs_ops.to_store_registers = procfs_store_registers;
1273 procfs_ops.to_prepare_to_store = procfs_prepare_to_store;
1274 procfs_ops.to_xfer_memory = procfs_xfer_memory;
1275 procfs_ops.to_files_info = procfs_files_info;
1276 procfs_ops.to_insert_breakpoint = procfs_insert_breakpoint;
1277 procfs_ops.to_remove_breakpoint = procfs_remove_breakpoint;
1278 procfs_ops.to_can_use_hw_breakpoint = procfs_can_use_hw_breakpoint;
1279 procfs_ops.to_insert_hw_breakpoint = procfs_insert_hw_breakpoint;
1280 procfs_ops.to_remove_hw_breakpoint = procfs_remove_breakpoint;
1281 procfs_ops.to_insert_watchpoint = procfs_insert_hw_watchpoint;
1282 procfs_ops.to_remove_watchpoint = procfs_remove_hw_watchpoint;
1283 procfs_ops.to_stopped_by_watchpoint = procfs_stopped_by_watchpoint;
1284 procfs_ops.to_terminal_init = terminal_init_inferior;
1285 procfs_ops.to_terminal_inferior = terminal_inferior;
1286 procfs_ops.to_terminal_ours_for_output = terminal_ours_for_output;
1287 procfs_ops.to_terminal_ours = terminal_ours;
1288 procfs_ops.to_terminal_info = child_terminal_info;
1289 procfs_ops.to_kill = procfs_kill_inferior;
1290 procfs_ops.to_create_inferior = procfs_create_inferior;
1291 procfs_ops.to_mourn_inferior = procfs_mourn_inferior;
1292 procfs_ops.to_can_run = procfs_can_run;
1293 procfs_ops.to_notice_signals = procfs_notice_signals;
1294 procfs_ops.to_thread_alive = procfs_thread_alive;
1295 procfs_ops.to_find_new_threads = procfs_find_new_threads;
1296 procfs_ops.to_pid_to_str = procfs_pid_to_str;
1297 procfs_ops.to_stop = procfs_stop;
1298 procfs_ops.to_stratum = process_stratum;
1299 procfs_ops.to_has_all_memory = 1;
1300 procfs_ops.to_has_memory = 1;
1301 procfs_ops.to_has_stack = 1;
1302 procfs_ops.to_has_registers = 1;
1303 procfs_ops.to_has_execution = 1;
1304 procfs_ops.to_magic = OPS_MAGIC;
1305 procfs_ops.to_have_continuable_watchpoint = 1;
1308 #define OSTYPE_NTO 1
1311 _initialize_procfs (void)
1316 add_target (&procfs_ops);
1318 /* We use SIGUSR1 to gain control after we block waiting for a process.
1319 We use sigwaitevent to wait. */
1321 sigaddset (&set, SIGUSR1);
1322 sigprocmask (SIG_BLOCK, &set, NULL);
1324 /* Set up trace and fault sets, as gdb expects them. */
1325 sigemptyset (&run.trace);
1328 /* Stuff some information. */
1329 nto_cpuinfo_flags = SYSPAGE_ENTRY (cpuinfo)->flags;
1330 nto_cpuinfo_valid = 1;
1332 add_info ("pidlist", procfs_pidlist, "pidlist");
1333 add_info ("meminfo", procfs_meminfo, "memory information");
1338 procfs_hw_watchpoint (int addr, int len, int type)
1345 brk.type = _DEBUG_BREAK_RD;
1347 case 2: /* Read/Write. */
1348 brk.type = _DEBUG_BREAK_RW;
1350 default: /* Modify. */
1351 /* FIXME: brk.type = _DEBUG_BREAK_RWM gives EINVAL for some reason. */
1352 brk.type = _DEBUG_BREAK_RW;
1354 brk.type |= _DEBUG_BREAK_HW; /* Always ask for HW. */
1358 errno = devctl (ctl_fd, DCMD_PROC_BREAK, &brk, sizeof (brk), 0);
1361 perror ("Failed to set hardware watchpoint");
1368 procfs_can_use_hw_breakpoint (int type, int cnt, int othertype)
1374 procfs_remove_hw_watchpoint (CORE_ADDR addr, int len, int type)
1376 return procfs_hw_watchpoint (addr, -1, type);
1380 procfs_insert_hw_watchpoint (CORE_ADDR addr, int len, int type)
1382 return procfs_hw_watchpoint (addr, len, type);
1386 procfs_stopped_by_watchpoint (void)