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 (C) 2003, 2006, 2007, 2008, 2009, 2010, 2011
5 Free Software Foundation, Inc.
7 Contributed by QNX Software Systems Ltd.
9 This file is part of GDB.
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 3 of the License, or
14 (at your option) any later version.
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with this program. If not, see <http://www.gnu.org/licenses/>. */
28 #include <sys/debug.h>
29 #include <sys/procfs.h>
30 #include <sys/neutrino.h>
31 #include <sys/syspage.h>
32 #include "gdb_dirent.h"
33 #include <sys/netmgr.h>
35 #include "exceptions.h"
36 #include "gdb_string.h"
41 #include "gdbthread.h"
48 #define _DEBUG_FLAG_TRACE (_DEBUG_FLAG_TRACE_EXEC|_DEBUG_FLAG_TRACE_RD|\
49 _DEBUG_FLAG_TRACE_WR|_DEBUG_FLAG_TRACE_MODIFY)
51 static struct target_ops procfs_ops;
55 static void (*ofunc) ();
57 static procfs_run run;
59 static void procfs_open (char *, int);
61 static int procfs_can_run (void);
63 static int procfs_xfer_memory (CORE_ADDR, gdb_byte *, int, int,
64 struct mem_attrib *attrib,
67 static void notice_signals (void);
69 static void init_procfs_ops (void);
71 static ptid_t do_attach (ptid_t ptid);
73 static int procfs_can_use_hw_breakpoint (int, int, int);
75 static int procfs_insert_hw_watchpoint (CORE_ADDR addr, int len, int type);
77 static int procfs_remove_hw_watchpoint (CORE_ADDR addr, int len, int type);
79 static int procfs_stopped_by_watchpoint (void);
81 /* These two globals are only ever set in procfs_open(), but are
82 referenced elsewhere. 'nto_procfs_node' is a flag used to say
83 whether we are local, or we should get the current node descriptor
84 for the remote QNX node. */
85 static char nto_procfs_path[PATH_MAX] = { "/proc" };
86 static unsigned nto_procfs_node = ND_LOCAL_NODE;
88 /* Return the current QNX Node, or error out. This is a simple
89 wrapper for the netmgr_strtond() function. The reason this
90 is required is because QNX node descriptors are transient so
91 we have to re-acquire them every time. */
97 if (ND_NODE_CMP (nto_procfs_node, ND_LOCAL_NODE) == 0)
100 node = netmgr_strtond (nto_procfs_path, 0);
102 error (_("Lost the QNX node. Debug session probably over."));
107 static enum gdb_osabi
108 procfs_is_nto_target (bfd *abfd)
110 return GDB_OSABI_QNXNTO;
113 /* This is called when we call 'target procfs <arg>' from the (gdb) prompt.
114 For QNX6 (nto), the only valid arg will be a QNX node string,
115 eg: "/net/some_node". If arg is not a valid QNX node, we will
118 procfs_open (char *arg, int from_tty)
124 procfs_sysinfo *sysinfo;
125 struct cleanup *cleanups;
127 nto_is_nto_target = procfs_is_nto_target;
129 /* Set the default node used for spawning to this one,
130 and only override it if there is a valid arg. */
132 nto_procfs_node = ND_LOCAL_NODE;
133 nodestr = arg ? xstrdup (arg) : arg;
139 nto_procfs_node = netmgr_strtond (nodestr, &endstr);
140 if (nto_procfs_node == -1)
142 if (errno == ENOTSUP)
143 printf_filtered ("QNX Net Manager not found.\n");
144 printf_filtered ("Invalid QNX node %s: error %d (%s).\n", nodestr,
145 errno, safe_strerror (errno));
148 nto_procfs_node = ND_LOCAL_NODE;
152 if (*(endstr - 1) == '/')
158 snprintf (nto_procfs_path, PATH_MAX - 1, "%s%s", nodestr ? nodestr : "",
163 fd = open (nto_procfs_path, O_RDONLY);
166 printf_filtered ("Error opening %s : %d (%s)\n", nto_procfs_path, errno,
167 safe_strerror (errno));
168 error (_("Invalid procfs arg"));
170 cleanups = make_cleanup_close (fd);
172 sysinfo = (void *) buffer;
173 if (devctl (fd, DCMD_PROC_SYSINFO, sysinfo, sizeof buffer, 0) != EOK)
175 printf_filtered ("Error getting size: %d (%s)\n", errno,
176 safe_strerror (errno));
177 error (_("Devctl failed."));
181 total_size = sysinfo->total_size;
182 sysinfo = alloca (total_size);
185 printf_filtered ("Memory error: %d (%s)\n", errno,
186 safe_strerror (errno));
187 error (_("alloca failed."));
191 if (devctl (fd, DCMD_PROC_SYSINFO, sysinfo, total_size, 0) != EOK)
193 printf_filtered ("Error getting sysinfo: %d (%s)\n", errno,
194 safe_strerror (errno));
195 error (_("Devctl failed."));
200 nto_map_arch_to_cputype (gdbarch_bfd_arch_info
201 (target_gdbarch)->arch_name))
202 error (_("Invalid target CPU."));
206 do_cleanups (cleanups);
207 printf_filtered ("Debugging using %s\n", nto_procfs_path);
211 procfs_set_thread (ptid_t ptid)
215 tid = ptid_get_tid (ptid);
216 devctl (ctl_fd, DCMD_PROC_CURTHREAD, &tid, sizeof (tid), 0);
219 /* Return nonzero if the thread TH is still alive. */
221 procfs_thread_alive (struct target_ops *ops, ptid_t ptid)
225 procfs_status status;
228 tid = ptid_get_tid (ptid);
229 pid = ptid_get_pid (ptid);
231 if (kill (pid, 0) == -1)
235 if ((err = devctl (ctl_fd, DCMD_PROC_TIDSTATUS,
236 &status, sizeof (status), 0)) != EOK)
239 /* Thread is alive or dead but not yet joined,
240 or dead and there is an alive (or dead unjoined) thread with
243 If the tid is not the same as requested, requested tid is dead. */
244 return (status.tid == tid) && (status.state != STATE_DEAD);
248 update_thread_private_data_name (struct thread_info *new_thread,
252 struct private_thread_info *pti;
254 gdb_assert (newname != NULL);
255 gdb_assert (new_thread != NULL);
256 newnamelen = strlen (newname);
257 if (!new_thread->private)
259 new_thread->private = xmalloc (offsetof (struct private_thread_info,
262 memcpy (new_thread->private->name, newname, newnamelen + 1);
264 else if (strcmp (newname, new_thread->private->name) != 0)
266 /* Reallocate if neccessary. */
267 int oldnamelen = strlen (new_thread->private->name);
269 if (oldnamelen < newnamelen)
270 new_thread->private = xrealloc (new_thread->private,
271 offsetof (struct private_thread_info,
274 memcpy (new_thread->private->name, newname, newnamelen + 1);
279 update_thread_private_data (struct thread_info *new_thread,
280 pthread_t tid, int state, int flags)
282 struct private_thread_info *pti;
284 struct _thread_name *tn;
285 procfs_threadctl tctl;
287 #if _NTO_VERSION > 630
288 gdb_assert (new_thread != NULL);
290 if (devctl (ctl_fd, DCMD_PROC_INFO, &pidinfo,
291 sizeof(pidinfo), 0) != EOK)
294 memset (&tctl, 0, sizeof (tctl));
295 tctl.cmd = _NTO_TCTL_NAME;
296 tn = (struct _thread_name *) (&tctl.data);
298 /* Fetch name for the given thread. */
300 tn->name_buf_len = sizeof (tctl.data) - sizeof (*tn);
301 tn->new_name_len = -1; /* Getting, not setting. */
302 if (devctl (ctl_fd, DCMD_PROC_THREADCTL, &tctl, sizeof (tctl), NULL) != EOK)
303 tn->name_buf[0] = '\0';
305 tn->name_buf[_NTO_THREAD_NAME_MAX] = '\0';
307 update_thread_private_data_name (new_thread, tn->name_buf);
309 pti = (struct private_thread_info *) new_thread->private;
313 #endif /* _NTO_VERSION */
317 procfs_find_new_threads (struct target_ops *ops)
319 procfs_status status;
323 struct thread_info *new_thread;
328 pid = ptid_get_pid (inferior_ptid);
332 for (tid = 1;; ++tid)
334 if (status.tid == tid
335 && (devctl (ctl_fd, DCMD_PROC_TIDSTATUS, &status, sizeof (status), 0)
338 if (status.tid != tid)
339 /* The reason why this would not be equal is that devctl might have
340 returned different tid, meaning the requested tid no longer exists
341 (e.g. thread exited). */
343 ptid = ptid_build (pid, 0, tid);
344 new_thread = find_thread_ptid (ptid);
346 new_thread = add_thread (ptid);
347 update_thread_private_data (new_thread, tid, status.state, 0);
354 do_closedir_cleanup (void *dir)
360 procfs_pidlist (char *args, int from_tty)
363 struct dirent *dirp = NULL;
365 procfs_info *pidinfo = NULL;
366 procfs_debuginfo *info = NULL;
367 procfs_status *status = NULL;
368 pid_t num_threads = 0;
371 struct cleanup *cleanups;
373 dp = opendir (nto_procfs_path);
376 fprintf_unfiltered (gdb_stderr, "failed to opendir \"%s\" - %d (%s)",
377 nto_procfs_path, errno, safe_strerror (errno));
381 cleanups = make_cleanup (do_closedir_cleanup, dp);
383 /* Start scan at first pid. */
389 struct cleanup *inner_cleanup;
391 /* Get the right pid and procfs path for the pid. */
397 do_cleanups (cleanups);
400 snprintf (buf, 511, "%s/%s/as", nto_procfs_path, dirp->d_name);
401 pid = atoi (dirp->d_name);
405 /* Open the procfs path. */
406 fd = open (buf, O_RDONLY);
409 fprintf_unfiltered (gdb_stderr, "failed to open %s - %d (%s)\n",
410 buf, errno, safe_strerror (errno));
411 do_cleanups (cleanups);
414 inner_cleanup = make_cleanup_close (fd);
416 pidinfo = (procfs_info *) buf;
417 if (devctl (fd, DCMD_PROC_INFO, pidinfo, sizeof (buf), 0) != EOK)
419 fprintf_unfiltered (gdb_stderr,
420 "devctl DCMD_PROC_INFO failed - %d (%s)\n",
421 errno, safe_strerror (errno));
424 num_threads = pidinfo->num_threads;
426 info = (procfs_debuginfo *) buf;
427 if (devctl (fd, DCMD_PROC_MAPDEBUG_BASE, info, sizeof (buf), 0) != EOK)
428 strcpy (name, "unavailable");
430 strcpy (name, info->path);
432 /* Collect state info on all the threads. */
433 status = (procfs_status *) buf;
434 for (status->tid = 1; status->tid <= num_threads; status->tid++)
436 if (devctl (fd, DCMD_PROC_TIDSTATUS, status, sizeof (buf), 0) != EOK
439 if (status->tid != 0)
440 printf_filtered ("%s - %d/%d\n", name, pid, status->tid);
443 do_cleanups (inner_cleanup);
445 while (dirp != NULL);
447 do_cleanups (cleanups);
452 procfs_meminfo (char *args, int from_tty)
454 procfs_mapinfo *mapinfos = NULL;
455 static int num_mapinfos = 0;
456 procfs_mapinfo *mapinfo_p, *mapinfo_p2;
457 int flags = ~0, err, num, i, j;
461 procfs_debuginfo info;
462 char buff[_POSIX_PATH_MAX];
470 unsigned debug_vaddr;
471 unsigned long long offset;
476 unsigned long long ino;
483 /* Get the number of map entrys. */
484 err = devctl (ctl_fd, DCMD_PROC_MAPINFO, NULL, 0, &num);
487 printf ("failed devctl num mapinfos - %d (%s)\n", err,
488 safe_strerror (err));
492 mapinfos = xmalloc (num * sizeof (procfs_mapinfo));
495 mapinfo_p = mapinfos;
497 /* Fill the map entrys. */
498 err = devctl (ctl_fd, DCMD_PROC_MAPINFO, mapinfo_p, num
499 * sizeof (procfs_mapinfo), &num);
502 printf ("failed devctl mapinfos - %d (%s)\n", err, safe_strerror (err));
507 num = min (num, num_mapinfos);
509 /* Run through the list of mapinfos, and store the data and text info
510 so we can print it at the bottom of the loop. */
511 for (mapinfo_p = mapinfos, i = 0; i < num; i++, mapinfo_p++)
513 if (!(mapinfo_p->flags & flags))
516 if (mapinfo_p->ino == 0) /* Already visited. */
519 map.info.vaddr = mapinfo_p->vaddr;
521 err = devctl (ctl_fd, DCMD_PROC_MAPDEBUG, &map, sizeof (map), 0);
525 memset (&printme, 0, sizeof printme);
526 printme.dev = mapinfo_p->dev;
527 printme.ino = mapinfo_p->ino;
528 printme.text.addr = mapinfo_p->vaddr;
529 printme.text.size = mapinfo_p->size;
530 printme.text.flags = mapinfo_p->flags;
531 printme.text.offset = mapinfo_p->offset;
532 printme.text.debug_vaddr = map.info.vaddr;
533 strcpy (printme.name, map.info.path);
535 /* Check for matching data. */
536 for (mapinfo_p2 = mapinfos, j = 0; j < num; j++, mapinfo_p2++)
538 if (mapinfo_p2->vaddr != mapinfo_p->vaddr
539 && mapinfo_p2->ino == mapinfo_p->ino
540 && mapinfo_p2->dev == mapinfo_p->dev)
542 map.info.vaddr = mapinfo_p2->vaddr;
544 devctl (ctl_fd, DCMD_PROC_MAPDEBUG, &map, sizeof (map), 0);
548 if (strcmp (map.info.path, printme.name))
551 /* Lower debug_vaddr is always text, if nessessary, swap. */
552 if ((int) map.info.vaddr < (int) printme.text.debug_vaddr)
554 memcpy (&(printme.data), &(printme.text),
555 sizeof (printme.data));
556 printme.text.addr = mapinfo_p2->vaddr;
557 printme.text.size = mapinfo_p2->size;
558 printme.text.flags = mapinfo_p2->flags;
559 printme.text.offset = mapinfo_p2->offset;
560 printme.text.debug_vaddr = map.info.vaddr;
564 printme.data.addr = mapinfo_p2->vaddr;
565 printme.data.size = mapinfo_p2->size;
566 printme.data.flags = mapinfo_p2->flags;
567 printme.data.offset = mapinfo_p2->offset;
568 printme.data.debug_vaddr = map.info.vaddr;
575 printf_filtered ("%s\n", printme.name);
576 printf_filtered ("\ttext=%08x bytes @ 0x%08x\n", printme.text.size,
578 printf_filtered ("\t\tflags=%08x\n", printme.text.flags);
579 printf_filtered ("\t\tdebug=%08x\n", printme.text.debug_vaddr);
580 printf_filtered ("\t\toffset=%s\n", phex (printme.text.offset, 8));
581 if (printme.data.size)
583 printf_filtered ("\tdata=%08x bytes @ 0x%08x\n", printme.data.size,
585 printf_filtered ("\t\tflags=%08x\n", printme.data.flags);
586 printf_filtered ("\t\tdebug=%08x\n", printme.data.debug_vaddr);
587 printf_filtered ("\t\toffset=%s\n", phex (printme.data.offset, 8));
589 printf_filtered ("\tdev=0x%x\n", printme.dev);
590 printf_filtered ("\tino=0x%x\n", (unsigned int) printme.ino);
596 /* Print status information about what we're accessing. */
598 procfs_files_info (struct target_ops *ignore)
600 struct inferior *inf = current_inferior ();
602 printf_unfiltered ("\tUsing the running image of %s %s via %s.\n",
603 inf->attach_flag ? "attached" : "child",
604 target_pid_to_str (inferior_ptid), nto_procfs_path);
607 /* Mark our target-struct as eligible for stray "run" and "attach" commands. */
609 procfs_can_run (void)
614 /* Attach to process PID, then initialize for debugging it. */
616 procfs_attach (struct target_ops *ops, char *args, int from_tty)
620 struct inferior *inf;
622 pid = parse_pid_to_attach (args);
624 if (pid == getpid ())
625 error (_("Attaching GDB to itself is not a good idea..."));
629 exec_file = (char *) get_exec_file (0);
632 printf_unfiltered ("Attaching to program `%s', %s\n", exec_file,
633 target_pid_to_str (pid_to_ptid (pid)));
635 printf_unfiltered ("Attaching to %s\n",
636 target_pid_to_str (pid_to_ptid (pid)));
638 gdb_flush (gdb_stdout);
640 inferior_ptid = do_attach (pid_to_ptid (pid));
641 inf = current_inferior ();
642 inferior_appeared (inf, pid);
643 inf->attach_flag = 1;
647 procfs_find_new_threads (ops);
651 procfs_post_attach (pid_t pid)
654 solib_create_inferior_hook (0);
658 do_attach (ptid_t ptid)
660 procfs_status status;
661 struct sigevent event;
664 snprintf (path, PATH_MAX - 1, "%s/%d/as", nto_procfs_path, PIDGET (ptid));
665 ctl_fd = open (path, O_RDWR);
667 error (_("Couldn't open proc file %s, error %d (%s)"), path, errno,
668 safe_strerror (errno));
669 if (devctl (ctl_fd, DCMD_PROC_STOP, &status, sizeof (status), 0) != EOK)
670 error (_("Couldn't stop process"));
672 /* Define a sigevent for process stopped notification. */
673 event.sigev_notify = SIGEV_SIGNAL_THREAD;
674 event.sigev_signo = SIGUSR1;
675 event.sigev_code = 0;
676 event.sigev_value.sival_ptr = NULL;
677 event.sigev_priority = -1;
678 devctl (ctl_fd, DCMD_PROC_EVENT, &event, sizeof (event), 0);
680 if (devctl (ctl_fd, DCMD_PROC_STATUS, &status, sizeof (status), 0) == EOK
681 && status.flags & _DEBUG_FLAG_STOPPED)
682 SignalKill (nto_node (), PIDGET (ptid), 0, SIGCONT, 0, 0);
683 nto_init_solib_absolute_prefix ();
684 return ptid_build (PIDGET (ptid), 0, status.tid);
687 /* Ask the user what to do when an interrupt is received. */
689 interrupt_query (void)
691 target_terminal_ours ();
693 if (query (_("Interrupted while waiting for the program.\n\
694 Give up (and stop debugging it)? ")))
696 target_mourn_inferior ();
697 deprecated_throw_reason (RETURN_QUIT);
700 target_terminal_inferior ();
703 /* The user typed ^C twice. */
705 nto_interrupt_twice (int signo)
707 signal (signo, ofunc);
709 signal (signo, nto_interrupt_twice);
713 nto_interrupt (int signo)
715 /* If this doesn't work, try more severe steps. */
716 signal (signo, nto_interrupt_twice);
718 target_stop (inferior_ptid);
722 procfs_wait (struct target_ops *ops,
723 ptid_t ptid, struct target_waitstatus *ourstatus, int options)
727 procfs_status status;
728 static int exit_signo = 0; /* To track signals that cause termination. */
730 ourstatus->kind = TARGET_WAITKIND_SPURIOUS;
732 if (ptid_equal (inferior_ptid, null_ptid))
734 ourstatus->kind = TARGET_WAITKIND_STOPPED;
735 ourstatus->value.sig = TARGET_SIGNAL_0;
741 sigaddset (&set, SIGUSR1);
743 devctl (ctl_fd, DCMD_PROC_STATUS, &status, sizeof (status), 0);
744 while (!(status.flags & _DEBUG_FLAG_ISTOP))
746 ofunc = (void (*)()) signal (SIGINT, nto_interrupt);
747 sigwaitinfo (&set, &info);
748 signal (SIGINT, ofunc);
749 devctl (ctl_fd, DCMD_PROC_STATUS, &status, sizeof (status), 0);
752 if (status.flags & _DEBUG_FLAG_SSTEP)
754 ourstatus->kind = TARGET_WAITKIND_STOPPED;
755 ourstatus->value.sig = TARGET_SIGNAL_TRAP;
757 /* Was it a breakpoint? */
758 else if (status.flags & _DEBUG_FLAG_TRACE)
760 ourstatus->kind = TARGET_WAITKIND_STOPPED;
761 ourstatus->value.sig = TARGET_SIGNAL_TRAP;
763 else if (status.flags & _DEBUG_FLAG_ISTOP)
767 case _DEBUG_WHY_SIGNALLED:
768 ourstatus->kind = TARGET_WAITKIND_STOPPED;
769 ourstatus->value.sig =
770 target_signal_from_host (status.info.si_signo);
773 case _DEBUG_WHY_FAULTED:
774 ourstatus->kind = TARGET_WAITKIND_STOPPED;
775 if (status.info.si_signo == SIGTRAP)
777 ourstatus->value.sig = 0;
782 ourstatus->value.sig =
783 target_signal_from_host (status.info.si_signo);
784 exit_signo = ourstatus->value.sig;
788 case _DEBUG_WHY_TERMINATED:
792 waitpid (PIDGET (inferior_ptid), &waitval, WNOHANG);
795 /* Abnormal death. */
796 ourstatus->kind = TARGET_WAITKIND_SIGNALLED;
797 ourstatus->value.sig = exit_signo;
802 ourstatus->kind = TARGET_WAITKIND_EXITED;
803 ourstatus->value.integer = WEXITSTATUS (waitval);
809 case _DEBUG_WHY_REQUESTED:
810 /* We are assuming a requested stop is due to a SIGINT. */
811 ourstatus->kind = TARGET_WAITKIND_STOPPED;
812 ourstatus->value.sig = TARGET_SIGNAL_INT;
818 return ptid_build (status.pid, 0, status.tid);
821 /* Read the current values of the inferior's registers, both the
822 general register set and floating point registers (if supported)
823 and update gdb's idea of their current values. */
825 procfs_fetch_registers (struct target_ops *ops,
826 struct regcache *regcache, int regno)
832 procfs_altreg altreg;
837 procfs_set_thread (inferior_ptid);
838 if (devctl (ctl_fd, DCMD_PROC_GETGREG, ®, sizeof (reg), ®size) == EOK)
839 nto_supply_gregset (regcache, (char *) ®.greg);
840 if (devctl (ctl_fd, DCMD_PROC_GETFPREG, ®, sizeof (reg), ®size)
842 nto_supply_fpregset (regcache, (char *) ®.fpreg);
843 if (devctl (ctl_fd, DCMD_PROC_GETALTREG, ®, sizeof (reg), ®size)
845 nto_supply_altregset (regcache, (char *) ®.altreg);
848 /* Copy LEN bytes to/from inferior's memory starting at MEMADDR
849 from/to debugger memory starting at MYADDR. Copy from inferior
850 if DOWRITE is zero or to inferior if DOWRITE is nonzero.
852 Returns the length copied, which is either the LEN argument or
853 zero. This xfer function does not do partial moves, since procfs_ops
854 doesn't allow memory operations to cross below us in the target stack
857 procfs_xfer_memory (CORE_ADDR memaddr, gdb_byte *myaddr, int len, int dowrite,
858 struct mem_attrib *attrib, struct target_ops *target)
862 if (lseek (ctl_fd, (off_t) memaddr, SEEK_SET) == (off_t) memaddr)
865 nbytes = write (ctl_fd, myaddr, len);
867 nbytes = read (ctl_fd, myaddr, len);
874 /* Take a program previously attached to and detaches it.
875 The program resumes execution and will no longer stop
876 on signals, etc. We'd better not have left any breakpoints
877 in the program or it'll die when it hits one. */
879 procfs_detach (struct target_ops *ops, char *args, int from_tty)
886 char *exec_file = get_exec_file (0);
889 printf_unfiltered ("Detaching from program: %s %s\n",
890 exec_file, target_pid_to_str (inferior_ptid));
891 gdb_flush (gdb_stdout);
894 siggnal = atoi (args);
897 SignalKill (nto_node (), PIDGET (inferior_ptid), 0, siggnal, 0, 0);
902 pid = ptid_get_pid (inferior_ptid);
903 inferior_ptid = null_ptid;
904 detach_inferior (pid);
906 unpush_target (&procfs_ops); /* Pop out of handling an inferior. */
910 procfs_breakpoint (CORE_ADDR addr, int type, int size)
917 errno = devctl (ctl_fd, DCMD_PROC_BREAK, &brk, sizeof (brk), 0);
924 procfs_insert_breakpoint (struct gdbarch *gdbarch,
925 struct bp_target_info *bp_tgt)
927 return procfs_breakpoint (bp_tgt->placed_address, _DEBUG_BREAK_EXEC, 0);
931 procfs_remove_breakpoint (struct gdbarch *gdbarch,
932 struct bp_target_info *bp_tgt)
934 return procfs_breakpoint (bp_tgt->placed_address, _DEBUG_BREAK_EXEC, -1);
938 procfs_insert_hw_breakpoint (struct gdbarch *gdbarch,
939 struct bp_target_info *bp_tgt)
941 return procfs_breakpoint (bp_tgt->placed_address,
942 _DEBUG_BREAK_EXEC | _DEBUG_BREAK_HW, 0);
946 procfs_remove_hw_breakpoint (struct gdbarch *gdbarch,
947 struct bp_target_info *bp_tgt)
949 return procfs_breakpoint (bp_tgt->placed_address,
950 _DEBUG_BREAK_EXEC | _DEBUG_BREAK_HW, -1);
954 procfs_resume (struct target_ops *ops,
955 ptid_t ptid, int step, enum target_signal signo)
958 procfs_status status;
959 sigset_t *run_fault = (sigset_t *) (void *) &run.fault;
961 if (ptid_equal (inferior_ptid, null_ptid))
964 procfs_set_thread (ptid_equal (ptid, minus_one_ptid) ? inferior_ptid :
967 run.flags = _DEBUG_RUN_FAULT | _DEBUG_RUN_TRACE;
969 run.flags |= _DEBUG_RUN_STEP;
971 sigemptyset (run_fault);
972 sigaddset (run_fault, FLTBPT);
973 sigaddset (run_fault, FLTTRACE);
974 sigaddset (run_fault, FLTILL);
975 sigaddset (run_fault, FLTPRIV);
976 sigaddset (run_fault, FLTBOUNDS);
977 sigaddset (run_fault, FLTIOVF);
978 sigaddset (run_fault, FLTIZDIV);
979 sigaddset (run_fault, FLTFPE);
980 /* Peter V will be changing this at some point. */
981 sigaddset (run_fault, FLTPAGE);
983 run.flags |= _DEBUG_RUN_ARM;
985 sigemptyset (&run.trace);
987 signal_to_pass = target_signal_to_host (signo);
991 devctl (ctl_fd, DCMD_PROC_STATUS, &status, sizeof (status), 0);
992 signal_to_pass = target_signal_to_host (signo);
993 if (status.why & (_DEBUG_WHY_SIGNALLED | _DEBUG_WHY_FAULTED))
995 if (signal_to_pass != status.info.si_signo)
997 SignalKill (nto_node (), PIDGET (inferior_ptid), 0,
998 signal_to_pass, 0, 0);
999 run.flags |= _DEBUG_RUN_CLRFLT | _DEBUG_RUN_CLRSIG;
1001 else /* Let it kill the program without telling us. */
1002 sigdelset (&run.trace, signal_to_pass);
1006 run.flags |= _DEBUG_RUN_CLRSIG | _DEBUG_RUN_CLRFLT;
1008 errno = devctl (ctl_fd, DCMD_PROC_RUN, &run, sizeof (run), 0);
1011 perror ("run error!\n");
1017 procfs_mourn_inferior (struct target_ops *ops)
1019 if (!ptid_equal (inferior_ptid, null_ptid))
1021 SignalKill (nto_node (), PIDGET (inferior_ptid), 0, SIGKILL, 0, 0);
1024 inferior_ptid = null_ptid;
1025 init_thread_list ();
1026 unpush_target (&procfs_ops);
1027 generic_mourn_inferior ();
1030 /* This function breaks up an argument string into an argument
1031 vector suitable for passing to execvp().
1032 E.g., on "run a b c d" this routine would get as input
1033 the string "a b c d", and as output it would fill in argv with
1034 the four arguments "a", "b", "c", "d". The only additional
1035 functionality is simple quoting. The gdb command:
1037 will fill in argv with the three args "a", "b c d", "e". */
1039 breakup_args (char *scratch, char **argv)
1041 char *pp, *cp = scratch;
1046 /* Scan past leading separators. */
1048 while (*cp == ' ' || *cp == '\t' || *cp == '\n')
1051 /* Break if at end of string. */
1059 quoting = strchr (cp, '"') ? 1 : 0;
1064 /* Scan for next arg separator. */
1067 cp = strchr (pp, '"');
1068 if ((cp == NULL) || (!quoting))
1069 cp = strchr (pp, ' ');
1071 cp = strchr (pp, '\t');
1073 cp = strchr (pp, '\n');
1075 /* No separators => end of string => break. */
1082 /* Replace the separator with a terminator. */
1086 /* Execv requires a null-terminated arg vector. */
1091 procfs_create_inferior (struct target_ops *ops, char *exec_file,
1092 char *allargs, char **env, int from_tty)
1094 struct inheritance inherit;
1098 const char *in = "", *out = "", *err = "";
1101 const char *inferior_io_terminal = get_inferior_io_terminal ();
1102 struct inferior *inf;
1104 argv = xmalloc (((strlen (allargs) + 1) / (unsigned) 2 + 2) *
1106 argv[0] = get_exec_file (1);
1110 argv[0] = exec_file;
1115 args = xstrdup (allargs);
1116 breakup_args (args, exec_file ? &argv[1] : &argv[0]);
1118 argv = nto_parse_redirection (argv, &in, &out, &err);
1120 fds[0] = STDIN_FILENO;
1121 fds[1] = STDOUT_FILENO;
1122 fds[2] = STDERR_FILENO;
1124 /* If the user specified I/O via gdb's --tty= arg, use it, but only
1125 if the i/o is not also being specified via redirection. */
1126 if (inferior_io_terminal)
1129 in = inferior_io_terminal;
1131 out = inferior_io_terminal;
1133 err = inferior_io_terminal;
1138 fd = open (in, O_RDONLY);
1146 fd = open (out, O_WRONLY);
1154 fd = open (err, O_WRONLY);
1161 /* Clear any pending SIGUSR1's but keep the behavior the same. */
1162 signal (SIGUSR1, signal (SIGUSR1, SIG_IGN));
1165 sigaddset (&set, SIGUSR1);
1166 sigprocmask (SIG_UNBLOCK, &set, NULL);
1168 memset (&inherit, 0, sizeof (inherit));
1170 if (ND_NODE_CMP (nto_procfs_node, ND_LOCAL_NODE) != 0)
1172 inherit.nd = nto_node ();
1173 inherit.flags |= SPAWN_SETND;
1174 inherit.flags &= ~SPAWN_EXEC;
1176 inherit.flags |= SPAWN_SETGROUP | SPAWN_HOLD;
1177 inherit.pgroup = SPAWN_NEWPGROUP;
1178 pid = spawnp (argv[0], 3, fds, &inherit, argv,
1179 ND_NODE_CMP (nto_procfs_node, ND_LOCAL_NODE) == 0 ? env : 0);
1182 sigprocmask (SIG_BLOCK, &set, NULL);
1185 error (_("Error spawning %s: %d (%s)"), argv[0], errno,
1186 safe_strerror (errno));
1188 if (fds[0] != STDIN_FILENO)
1190 if (fds[1] != STDOUT_FILENO)
1192 if (fds[2] != STDERR_FILENO)
1195 inferior_ptid = do_attach (pid_to_ptid (pid));
1196 procfs_find_new_threads (ops);
1198 inf = current_inferior ();
1199 inferior_appeared (inf, pid);
1200 inf->attach_flag = 0;
1202 flags = _DEBUG_FLAG_KLC; /* Kill-on-Last-Close flag. */
1203 errn = devctl (ctl_fd, DCMD_PROC_SET_FLAG, &flags, sizeof (flags), 0);
1206 /* FIXME: expected warning? */
1207 /* warning( "Failed to set Kill-on-Last-Close flag: errno = %d(%s)\n",
1208 errn, strerror(errn) ); */
1211 target_terminal_init ();
1213 if (exec_bfd != NULL
1214 || (symfile_objfile != NULL && symfile_objfile->obfd != NULL))
1215 solib_create_inferior_hook (0);
1219 procfs_stop (ptid_t ptid)
1221 devctl (ctl_fd, DCMD_PROC_STOP, NULL, 0, 0);
1225 procfs_kill_inferior (struct target_ops *ops)
1227 target_mourn_inferior ();
1230 /* Store register REGNO, or all registers if REGNO == -1, from the contents
1233 procfs_prepare_to_store (struct regcache *regcache)
1237 /* Fill buf with regset and return devctl cmd to do the setting. Return
1238 -1 if we fail to get the regset. Store size of regset in regsize. */
1240 get_regset (int regset, char *buf, int bufsize, int *regsize)
1242 int dev_get, dev_set;
1245 case NTO_REG_GENERAL:
1246 dev_get = DCMD_PROC_GETGREG;
1247 dev_set = DCMD_PROC_SETGREG;
1251 dev_get = DCMD_PROC_GETFPREG;
1252 dev_set = DCMD_PROC_SETFPREG;
1256 dev_get = DCMD_PROC_GETALTREG;
1257 dev_set = DCMD_PROC_SETALTREG;
1260 case NTO_REG_SYSTEM:
1264 if (devctl (ctl_fd, dev_get, buf, bufsize, regsize) != EOK)
1271 procfs_store_registers (struct target_ops *ops,
1272 struct regcache *regcache, int regno)
1278 procfs_altreg altreg;
1282 int len, regset, regsize, dev_set, err;
1285 if (ptid_equal (inferior_ptid, null_ptid))
1287 procfs_set_thread (inferior_ptid);
1291 for (regset = NTO_REG_GENERAL; regset < NTO_REG_END; regset++)
1293 dev_set = get_regset (regset, (char *) ®,
1294 sizeof (reg), ®size);
1298 if (nto_regset_fill (regcache, regset, (char *) ®) == -1)
1301 err = devctl (ctl_fd, dev_set, ®, regsize, 0);
1303 fprintf_unfiltered (gdb_stderr,
1304 "Warning unable to write regset %d: %s\n",
1305 regno, safe_strerror (err));
1310 regset = nto_regset_id (regno);
1314 dev_set = get_regset (regset, (char *) ®, sizeof (reg), ®size);
1318 len = nto_register_area (get_regcache_arch (regcache),
1319 regno, regset, &off);
1324 regcache_raw_collect (regcache, regno, (char *) ® + off);
1326 err = devctl (ctl_fd, dev_set, ®, regsize, 0);
1328 fprintf_unfiltered (gdb_stderr,
1329 "Warning unable to write regset %d: %s\n", regno,
1330 safe_strerror (err));
1335 notice_signals (void)
1339 for (signo = 1; signo < NSIG; signo++)
1341 if (signal_stop_state (target_signal_from_host (signo)) == 0
1342 && signal_print_state (target_signal_from_host (signo)) == 0
1343 && signal_pass_state (target_signal_from_host (signo)) == 1)
1344 sigdelset (&run.trace, signo);
1346 sigaddset (&run.trace, signo);
1350 /* When the user changes the state of gdb's signal handling via the
1351 "handle" command, this function gets called to see if any change
1352 in the /proc interface is required. It is also called internally
1353 by other /proc interface functions to initialize the state of
1354 the traced signal set. */
1356 procfs_notice_signals (ptid_t ptid)
1358 sigemptyset (&run.trace);
1362 static struct tidinfo *
1363 procfs_thread_info (pid_t pid, short tid)
1370 procfs_pid_to_str (struct target_ops *ops, ptid_t ptid)
1372 static char buf[1024];
1374 struct tidinfo *tip;
1376 pid = ptid_get_pid (ptid);
1377 tid = ptid_get_tid (ptid);
1379 n = snprintf (buf, 1023, "process %d", pid);
1382 tip = procfs_thread_info (pid, tid);
1384 snprintf (&buf[n], 1023, " (state = 0x%02x)", tip->state);
1391 init_procfs_ops (void)
1393 procfs_ops.to_shortname = "procfs";
1394 procfs_ops.to_longname = "QNX Neutrino procfs child process";
1396 "QNX Neutrino procfs child process (started by the \"run\" command).\n\
1397 target procfs <node>";
1398 procfs_ops.to_open = procfs_open;
1399 procfs_ops.to_attach = procfs_attach;
1400 procfs_ops.to_post_attach = procfs_post_attach;
1401 procfs_ops.to_detach = procfs_detach;
1402 procfs_ops.to_resume = procfs_resume;
1403 procfs_ops.to_wait = procfs_wait;
1404 procfs_ops.to_fetch_registers = procfs_fetch_registers;
1405 procfs_ops.to_store_registers = procfs_store_registers;
1406 procfs_ops.to_prepare_to_store = procfs_prepare_to_store;
1407 procfs_ops.deprecated_xfer_memory = procfs_xfer_memory;
1408 procfs_ops.to_files_info = procfs_files_info;
1409 procfs_ops.to_insert_breakpoint = procfs_insert_breakpoint;
1410 procfs_ops.to_remove_breakpoint = procfs_remove_breakpoint;
1411 procfs_ops.to_can_use_hw_breakpoint = procfs_can_use_hw_breakpoint;
1412 procfs_ops.to_insert_hw_breakpoint = procfs_insert_hw_breakpoint;
1413 procfs_ops.to_remove_hw_breakpoint = procfs_remove_breakpoint;
1414 procfs_ops.to_insert_watchpoint = procfs_insert_hw_watchpoint;
1415 procfs_ops.to_remove_watchpoint = procfs_remove_hw_watchpoint;
1416 procfs_ops.to_stopped_by_watchpoint = procfs_stopped_by_watchpoint;
1417 procfs_ops.to_terminal_init = terminal_init_inferior;
1418 procfs_ops.to_terminal_inferior = terminal_inferior;
1419 procfs_ops.to_terminal_ours_for_output = terminal_ours_for_output;
1420 procfs_ops.to_terminal_ours = terminal_ours;
1421 procfs_ops.to_terminal_info = child_terminal_info;
1422 procfs_ops.to_kill = procfs_kill_inferior;
1423 procfs_ops.to_create_inferior = procfs_create_inferior;
1424 procfs_ops.to_mourn_inferior = procfs_mourn_inferior;
1425 procfs_ops.to_can_run = procfs_can_run;
1426 procfs_ops.to_notice_signals = procfs_notice_signals;
1427 procfs_ops.to_thread_alive = procfs_thread_alive;
1428 procfs_ops.to_find_new_threads = procfs_find_new_threads;
1429 procfs_ops.to_pid_to_str = procfs_pid_to_str;
1430 procfs_ops.to_stop = procfs_stop;
1431 procfs_ops.to_stratum = process_stratum;
1432 procfs_ops.to_has_all_memory = default_child_has_all_memory;
1433 procfs_ops.to_has_memory = default_child_has_memory;
1434 procfs_ops.to_has_stack = default_child_has_stack;
1435 procfs_ops.to_has_registers = default_child_has_registers;
1436 procfs_ops.to_has_execution = default_child_has_execution;
1437 procfs_ops.to_magic = OPS_MAGIC;
1438 procfs_ops.to_have_continuable_watchpoint = 1;
1439 procfs_ops.to_extra_thread_info = nto_extra_thread_info;
1442 #define OSTYPE_NTO 1
1445 _initialize_procfs (void)
1450 add_target (&procfs_ops);
1452 /* We use SIGUSR1 to gain control after we block waiting for a process.
1453 We use sigwaitevent to wait. */
1455 sigaddset (&set, SIGUSR1);
1456 sigprocmask (SIG_BLOCK, &set, NULL);
1458 /* Set up trace and fault sets, as gdb expects them. */
1459 sigemptyset (&run.trace);
1461 /* Stuff some information. */
1462 nto_cpuinfo_flags = SYSPAGE_ENTRY (cpuinfo)->flags;
1463 nto_cpuinfo_valid = 1;
1465 add_info ("pidlist", procfs_pidlist, _("pidlist"));
1466 add_info ("meminfo", procfs_meminfo, _("memory information"));
1468 nto_is_nto_target = procfs_is_nto_target;
1473 procfs_hw_watchpoint (int addr, int len, int type)
1480 brk.type = _DEBUG_BREAK_RD;
1482 case 2: /* Read/Write. */
1483 brk.type = _DEBUG_BREAK_RW;
1485 default: /* Modify. */
1486 /* FIXME: brk.type = _DEBUG_BREAK_RWM gives EINVAL for some reason. */
1487 brk.type = _DEBUG_BREAK_RW;
1489 brk.type |= _DEBUG_BREAK_HW; /* Always ask for HW. */
1493 errno = devctl (ctl_fd, DCMD_PROC_BREAK, &brk, sizeof (brk), 0);
1496 perror ("Failed to set hardware watchpoint");
1503 procfs_can_use_hw_breakpoint (int type, int cnt, int othertype)
1509 procfs_remove_hw_watchpoint (CORE_ADDR addr, int len, int type,
1510 struct expression *cond)
1512 return procfs_hw_watchpoint (addr, -1, type);
1516 procfs_insert_hw_watchpoint (CORE_ADDR addr, int len, int type,
1517 struct expression *cond)
1519 return procfs_hw_watchpoint (addr, len, type);
1523 procfs_stopped_by_watchpoint (void)