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-2015 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 3 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, see <http://www.gnu.org/licenses/>. */
27 #include <sys/debug.h>
28 #include <sys/procfs.h>
29 #include <sys/neutrino.h>
30 #include <sys/syspage.h>
32 #include <sys/netmgr.h>
37 #include "gdbthread.h"
42 #include "inf-child.h"
45 #define _DEBUG_FLAG_TRACE (_DEBUG_FLAG_TRACE_EXEC|_DEBUG_FLAG_TRACE_RD|\
46 _DEBUG_FLAG_TRACE_WR|_DEBUG_FLAG_TRACE_MODIFY)
50 static void (*ofunc) ();
52 static procfs_run run;
54 static ptid_t do_attach (ptid_t ptid);
56 static int procfs_can_use_hw_breakpoint (struct target_ops *self,
57 enum target_hw_bp_type, int, int);
59 static int procfs_insert_hw_watchpoint (struct target_ops *self,
60 CORE_ADDR addr, int len,
61 enum target_hw_bp_type type,
62 struct expression *cond);
64 static int procfs_remove_hw_watchpoint (struct target_ops *self,
65 CORE_ADDR addr, int len,
66 enum target_hw_bp_type type,
67 struct expression *cond);
69 static int procfs_stopped_by_watchpoint (struct target_ops *ops);
71 /* These two globals are only ever set in procfs_open_1, but are
72 referenced elsewhere. 'nto_procfs_node' is a flag used to say
73 whether we are local, or we should get the current node descriptor
74 for the remote QNX node. */
75 static char nto_procfs_path[PATH_MAX] = { "/proc" };
76 static unsigned nto_procfs_node = ND_LOCAL_NODE;
78 /* Return the current QNX Node, or error out. This is a simple
79 wrapper for the netmgr_strtond() function. The reason this
80 is required is because QNX node descriptors are transient so
81 we have to re-acquire them every time. */
87 if (ND_NODE_CMP (nto_procfs_node, ND_LOCAL_NODE) == 0)
90 node = netmgr_strtond (nto_procfs_path, 0);
92 error (_("Lost the QNX node. Debug session probably over."));
98 procfs_is_nto_target (bfd *abfd)
100 return GDB_OSABI_QNXNTO;
103 /* This is called when we call 'target native' or 'target procfs
104 <arg>' from the (gdb) prompt. For QNX6 (nto), the only valid arg
105 will be a QNX node string, eg: "/net/some_node". If arg is not a
106 valid QNX node, we will default to local. */
108 procfs_open_1 (struct target_ops *ops, const char *arg, int from_tty)
114 procfs_sysinfo *sysinfo;
115 struct cleanup *cleanups;
117 /* Offer to kill previous inferiors before opening this target. */
118 target_preopen (from_tty);
120 nto_is_nto_target = procfs_is_nto_target;
122 /* Set the default node used for spawning to this one,
123 and only override it if there is a valid arg. */
125 nto_procfs_node = ND_LOCAL_NODE;
126 nodestr = arg ? xstrdup (arg) : arg;
132 nto_procfs_node = netmgr_strtond (nodestr, &endstr);
133 if (nto_procfs_node == -1)
135 if (errno == ENOTSUP)
136 printf_filtered ("QNX Net Manager not found.\n");
137 printf_filtered ("Invalid QNX node %s: error %d (%s).\n", nodestr,
138 errno, safe_strerror (errno));
141 nto_procfs_node = ND_LOCAL_NODE;
145 if (*(endstr - 1) == '/')
151 snprintf (nto_procfs_path, PATH_MAX - 1, "%s%s", nodestr ? nodestr : "",
156 fd = open (nto_procfs_path, O_RDONLY);
159 printf_filtered ("Error opening %s : %d (%s)\n", nto_procfs_path, errno,
160 safe_strerror (errno));
161 error (_("Invalid procfs arg"));
163 cleanups = make_cleanup_close (fd);
165 sysinfo = (void *) buffer;
166 if (devctl (fd, DCMD_PROC_SYSINFO, sysinfo, sizeof buffer, 0) != EOK)
168 printf_filtered ("Error getting size: %d (%s)\n", errno,
169 safe_strerror (errno));
170 error (_("Devctl failed."));
174 total_size = sysinfo->total_size;
175 sysinfo = alloca (total_size);
178 printf_filtered ("Memory error: %d (%s)\n", errno,
179 safe_strerror (errno));
180 error (_("alloca failed."));
184 if (devctl (fd, DCMD_PROC_SYSINFO, sysinfo, total_size, 0) != EOK)
186 printf_filtered ("Error getting sysinfo: %d (%s)\n", errno,
187 safe_strerror (errno));
188 error (_("Devctl failed."));
193 nto_map_arch_to_cputype (gdbarch_bfd_arch_info
194 (target_gdbarch ())->arch_name))
195 error (_("Invalid target CPU."));
199 do_cleanups (cleanups);
201 inf_child_open_target (ops, arg, from_tty);
202 printf_filtered ("Debugging using %s\n", nto_procfs_path);
206 procfs_set_thread (ptid_t ptid)
210 tid = ptid_get_tid (ptid);
211 devctl (ctl_fd, DCMD_PROC_CURTHREAD, &tid, sizeof (tid), 0);
214 /* Return nonzero if the thread TH is still alive. */
216 procfs_thread_alive (struct target_ops *ops, ptid_t ptid)
220 procfs_status status;
223 tid = ptid_get_tid (ptid);
224 pid = ptid_get_pid (ptid);
226 if (kill (pid, 0) == -1)
230 if ((err = devctl (ctl_fd, DCMD_PROC_TIDSTATUS,
231 &status, sizeof (status), 0)) != EOK)
234 /* Thread is alive or dead but not yet joined,
235 or dead and there is an alive (or dead unjoined) thread with
238 If the tid is not the same as requested, requested tid is dead. */
239 return (status.tid == tid) && (status.state != STATE_DEAD);
243 update_thread_private_data_name (struct thread_info *new_thread,
247 struct private_thread_info *pti;
249 gdb_assert (newname != NULL);
250 gdb_assert (new_thread != NULL);
251 newnamelen = strlen (newname);
252 if (!new_thread->priv)
254 new_thread->priv = xmalloc (offsetof (struct private_thread_info,
257 memcpy (new_thread->priv->name, newname, newnamelen + 1);
259 else if (strcmp (newname, new_thread->priv->name) != 0)
261 /* Reallocate if neccessary. */
262 int oldnamelen = strlen (new_thread->priv->name);
264 if (oldnamelen < newnamelen)
265 new_thread->priv = xrealloc (new_thread->priv,
266 offsetof (struct private_thread_info,
269 memcpy (new_thread->priv->name, newname, newnamelen + 1);
274 update_thread_private_data (struct thread_info *new_thread,
275 pthread_t tid, int state, int flags)
277 struct private_thread_info *pti;
279 struct _thread_name *tn;
280 procfs_threadctl tctl;
282 #if _NTO_VERSION > 630
283 gdb_assert (new_thread != NULL);
285 if (devctl (ctl_fd, DCMD_PROC_INFO, &pidinfo,
286 sizeof(pidinfo), 0) != EOK)
289 memset (&tctl, 0, sizeof (tctl));
290 tctl.cmd = _NTO_TCTL_NAME;
291 tn = (struct _thread_name *) (&tctl.data);
293 /* Fetch name for the given thread. */
295 tn->name_buf_len = sizeof (tctl.data) - sizeof (*tn);
296 tn->new_name_len = -1; /* Getting, not setting. */
297 if (devctl (ctl_fd, DCMD_PROC_THREADCTL, &tctl, sizeof (tctl), NULL) != EOK)
298 tn->name_buf[0] = '\0';
300 tn->name_buf[_NTO_THREAD_NAME_MAX] = '\0';
302 update_thread_private_data_name (new_thread, tn->name_buf);
304 pti = (struct private_thread_info *) new_thread->priv;
308 #endif /* _NTO_VERSION */
312 procfs_update_thread_list (struct target_ops *ops)
314 procfs_status status;
318 struct thread_info *new_thread;
325 pid = ptid_get_pid (inferior_ptid);
329 for (tid = 1;; ++tid)
331 if (status.tid == tid
332 && (devctl (ctl_fd, DCMD_PROC_TIDSTATUS, &status, sizeof (status), 0)
335 if (status.tid != tid)
336 /* The reason why this would not be equal is that devctl might have
337 returned different tid, meaning the requested tid no longer exists
338 (e.g. thread exited). */
340 ptid = ptid_build (pid, 0, tid);
341 new_thread = find_thread_ptid (ptid);
343 new_thread = add_thread (ptid);
344 update_thread_private_data (new_thread, tid, status.state, 0);
351 do_closedir_cleanup (void *dir)
357 procfs_pidlist (char *args, int from_tty)
360 struct dirent *dirp = NULL;
362 procfs_info *pidinfo = NULL;
363 procfs_debuginfo *info = NULL;
364 procfs_status *status = NULL;
365 pid_t num_threads = 0;
368 struct cleanup *cleanups;
370 dp = opendir (nto_procfs_path);
373 fprintf_unfiltered (gdb_stderr, "failed to opendir \"%s\" - %d (%s)",
374 nto_procfs_path, errno, safe_strerror (errno));
378 cleanups = make_cleanup (do_closedir_cleanup, dp);
380 /* Start scan at first pid. */
386 struct cleanup *inner_cleanup;
388 /* Get the right pid and procfs path for the pid. */
394 do_cleanups (cleanups);
397 snprintf (buf, 511, "%s/%s/as", nto_procfs_path, dirp->d_name);
398 pid = atoi (dirp->d_name);
402 /* Open the procfs path. */
403 fd = open (buf, O_RDONLY);
406 fprintf_unfiltered (gdb_stderr, "failed to open %s - %d (%s)\n",
407 buf, errno, safe_strerror (errno));
408 do_cleanups (cleanups);
411 inner_cleanup = make_cleanup_close (fd);
413 pidinfo = (procfs_info *) buf;
414 if (devctl (fd, DCMD_PROC_INFO, pidinfo, sizeof (buf), 0) != EOK)
416 fprintf_unfiltered (gdb_stderr,
417 "devctl DCMD_PROC_INFO failed - %d (%s)\n",
418 errno, safe_strerror (errno));
421 num_threads = pidinfo->num_threads;
423 info = (procfs_debuginfo *) buf;
424 if (devctl (fd, DCMD_PROC_MAPDEBUG_BASE, info, sizeof (buf), 0) != EOK)
425 strcpy (name, "unavailable");
427 strcpy (name, info->path);
429 /* Collect state info on all the threads. */
430 status = (procfs_status *) buf;
431 for (status->tid = 1; status->tid <= num_threads; status->tid++)
433 if (devctl (fd, DCMD_PROC_TIDSTATUS, status, sizeof (buf), 0) != EOK
436 if (status->tid != 0)
437 printf_filtered ("%s - %d/%d\n", name, pid, status->tid);
440 do_cleanups (inner_cleanup);
442 while (dirp != NULL);
444 do_cleanups (cleanups);
449 procfs_meminfo (char *args, int from_tty)
451 procfs_mapinfo *mapinfos = NULL;
452 static int num_mapinfos = 0;
453 procfs_mapinfo *mapinfo_p, *mapinfo_p2;
454 int flags = ~0, err, num, i, j;
458 procfs_debuginfo info;
459 char buff[_POSIX_PATH_MAX];
467 unsigned debug_vaddr;
468 unsigned long long offset;
473 unsigned long long ino;
480 /* Get the number of map entrys. */
481 err = devctl (ctl_fd, DCMD_PROC_MAPINFO, NULL, 0, &num);
484 printf ("failed devctl num mapinfos - %d (%s)\n", err,
485 safe_strerror (err));
489 mapinfos = xmalloc (num * sizeof (procfs_mapinfo));
492 mapinfo_p = mapinfos;
494 /* Fill the map entrys. */
495 err = devctl (ctl_fd, DCMD_PROC_MAPINFO, mapinfo_p, num
496 * sizeof (procfs_mapinfo), &num);
499 printf ("failed devctl mapinfos - %d (%s)\n", err, safe_strerror (err));
504 num = min (num, num_mapinfos);
506 /* Run through the list of mapinfos, and store the data and text info
507 so we can print it at the bottom of the loop. */
508 for (mapinfo_p = mapinfos, i = 0; i < num; i++, mapinfo_p++)
510 if (!(mapinfo_p->flags & flags))
513 if (mapinfo_p->ino == 0) /* Already visited. */
516 map.info.vaddr = mapinfo_p->vaddr;
518 err = devctl (ctl_fd, DCMD_PROC_MAPDEBUG, &map, sizeof (map), 0);
522 memset (&printme, 0, sizeof printme);
523 printme.dev = mapinfo_p->dev;
524 printme.ino = mapinfo_p->ino;
525 printme.text.addr = mapinfo_p->vaddr;
526 printme.text.size = mapinfo_p->size;
527 printme.text.flags = mapinfo_p->flags;
528 printme.text.offset = mapinfo_p->offset;
529 printme.text.debug_vaddr = map.info.vaddr;
530 strcpy (printme.name, map.info.path);
532 /* Check for matching data. */
533 for (mapinfo_p2 = mapinfos, j = 0; j < num; j++, mapinfo_p2++)
535 if (mapinfo_p2->vaddr != mapinfo_p->vaddr
536 && mapinfo_p2->ino == mapinfo_p->ino
537 && mapinfo_p2->dev == mapinfo_p->dev)
539 map.info.vaddr = mapinfo_p2->vaddr;
541 devctl (ctl_fd, DCMD_PROC_MAPDEBUG, &map, sizeof (map), 0);
545 if (strcmp (map.info.path, printme.name))
548 /* Lower debug_vaddr is always text, if nessessary, swap. */
549 if ((int) map.info.vaddr < (int) printme.text.debug_vaddr)
551 memcpy (&(printme.data), &(printme.text),
552 sizeof (printme.data));
553 printme.text.addr = mapinfo_p2->vaddr;
554 printme.text.size = mapinfo_p2->size;
555 printme.text.flags = mapinfo_p2->flags;
556 printme.text.offset = mapinfo_p2->offset;
557 printme.text.debug_vaddr = map.info.vaddr;
561 printme.data.addr = mapinfo_p2->vaddr;
562 printme.data.size = mapinfo_p2->size;
563 printme.data.flags = mapinfo_p2->flags;
564 printme.data.offset = mapinfo_p2->offset;
565 printme.data.debug_vaddr = map.info.vaddr;
572 printf_filtered ("%s\n", printme.name);
573 printf_filtered ("\ttext=%08x bytes @ 0x%08x\n", printme.text.size,
575 printf_filtered ("\t\tflags=%08x\n", printme.text.flags);
576 printf_filtered ("\t\tdebug=%08x\n", printme.text.debug_vaddr);
577 printf_filtered ("\t\toffset=%s\n", phex (printme.text.offset, 8));
578 if (printme.data.size)
580 printf_filtered ("\tdata=%08x bytes @ 0x%08x\n", printme.data.size,
582 printf_filtered ("\t\tflags=%08x\n", printme.data.flags);
583 printf_filtered ("\t\tdebug=%08x\n", printme.data.debug_vaddr);
584 printf_filtered ("\t\toffset=%s\n", phex (printme.data.offset, 8));
586 printf_filtered ("\tdev=0x%x\n", printme.dev);
587 printf_filtered ("\tino=0x%x\n", (unsigned int) printme.ino);
593 /* Print status information about what we're accessing. */
595 procfs_files_info (struct target_ops *ignore)
597 struct inferior *inf = current_inferior ();
599 printf_unfiltered ("\tUsing the running image of %s %s via %s.\n",
600 inf->attach_flag ? "attached" : "child",
601 target_pid_to_str (inferior_ptid), nto_procfs_path);
604 /* Attach to process PID, then initialize for debugging it. */
606 procfs_attach (struct target_ops *ops, const char *args, int from_tty)
610 struct inferior *inf;
612 pid = parse_pid_to_attach (args);
614 if (pid == getpid ())
615 error (_("Attaching GDB to itself is not a good idea..."));
619 exec_file = (char *) get_exec_file (0);
622 printf_unfiltered ("Attaching to program `%s', %s\n", exec_file,
623 target_pid_to_str (pid_to_ptid (pid)));
625 printf_unfiltered ("Attaching to %s\n",
626 target_pid_to_str (pid_to_ptid (pid)));
628 gdb_flush (gdb_stdout);
630 inferior_ptid = do_attach (pid_to_ptid (pid));
631 inf = current_inferior ();
632 inferior_appeared (inf, pid);
633 inf->attach_flag = 1;
635 if (!target_is_pushed (ops))
638 procfs_update_thread_list (ops);
642 procfs_post_attach (struct target_ops *self, pid_t pid)
645 solib_create_inferior_hook (0);
649 do_attach (ptid_t ptid)
651 procfs_status status;
652 struct sigevent event;
655 snprintf (path, PATH_MAX - 1, "%s/%d/as", nto_procfs_path,
656 ptid_get_pid (ptid));
657 ctl_fd = open (path, O_RDWR);
659 error (_("Couldn't open proc file %s, error %d (%s)"), path, errno,
660 safe_strerror (errno));
661 if (devctl (ctl_fd, DCMD_PROC_STOP, &status, sizeof (status), 0) != EOK)
662 error (_("Couldn't stop process"));
664 /* Define a sigevent for process stopped notification. */
665 event.sigev_notify = SIGEV_SIGNAL_THREAD;
666 event.sigev_signo = SIGUSR1;
667 event.sigev_code = 0;
668 event.sigev_value.sival_ptr = NULL;
669 event.sigev_priority = -1;
670 devctl (ctl_fd, DCMD_PROC_EVENT, &event, sizeof (event), 0);
672 if (devctl (ctl_fd, DCMD_PROC_STATUS, &status, sizeof (status), 0) == EOK
673 && status.flags & _DEBUG_FLAG_STOPPED)
674 SignalKill (nto_node (), ptid_get_pid (ptid), 0, SIGCONT, 0, 0);
675 nto_init_solib_absolute_prefix ();
676 return ptid_build (ptid_get_pid (ptid), 0, status.tid);
679 /* Ask the user what to do when an interrupt is received. */
681 interrupt_query (void)
683 target_terminal_ours ();
685 if (query (_("Interrupted while waiting for the program.\n\
686 Give up (and stop debugging it)? ")))
688 target_mourn_inferior ();
692 target_terminal_inferior ();
695 /* The user typed ^C twice. */
697 nto_interrupt_twice (int signo)
699 signal (signo, ofunc);
701 signal (signo, nto_interrupt_twice);
705 nto_interrupt (int signo)
707 /* If this doesn't work, try more severe steps. */
708 signal (signo, nto_interrupt_twice);
710 target_stop (inferior_ptid);
714 procfs_wait (struct target_ops *ops,
715 ptid_t ptid, struct target_waitstatus *ourstatus, int options)
719 procfs_status status;
720 static int exit_signo = 0; /* To track signals that cause termination. */
722 ourstatus->kind = TARGET_WAITKIND_SPURIOUS;
724 if (ptid_equal (inferior_ptid, null_ptid))
726 ourstatus->kind = TARGET_WAITKIND_STOPPED;
727 ourstatus->value.sig = GDB_SIGNAL_0;
733 sigaddset (&set, SIGUSR1);
735 devctl (ctl_fd, DCMD_PROC_STATUS, &status, sizeof (status), 0);
736 while (!(status.flags & _DEBUG_FLAG_ISTOP))
738 ofunc = (void (*)()) signal (SIGINT, nto_interrupt);
739 sigwaitinfo (&set, &info);
740 signal (SIGINT, ofunc);
741 devctl (ctl_fd, DCMD_PROC_STATUS, &status, sizeof (status), 0);
744 if (status.flags & _DEBUG_FLAG_SSTEP)
746 ourstatus->kind = TARGET_WAITKIND_STOPPED;
747 ourstatus->value.sig = GDB_SIGNAL_TRAP;
749 /* Was it a breakpoint? */
750 else if (status.flags & _DEBUG_FLAG_TRACE)
752 ourstatus->kind = TARGET_WAITKIND_STOPPED;
753 ourstatus->value.sig = GDB_SIGNAL_TRAP;
755 else if (status.flags & _DEBUG_FLAG_ISTOP)
759 case _DEBUG_WHY_SIGNALLED:
760 ourstatus->kind = TARGET_WAITKIND_STOPPED;
761 ourstatus->value.sig =
762 gdb_signal_from_host (status.info.si_signo);
765 case _DEBUG_WHY_FAULTED:
766 ourstatus->kind = TARGET_WAITKIND_STOPPED;
767 if (status.info.si_signo == SIGTRAP)
769 ourstatus->value.sig = 0;
774 ourstatus->value.sig =
775 gdb_signal_from_host (status.info.si_signo);
776 exit_signo = ourstatus->value.sig;
780 case _DEBUG_WHY_TERMINATED:
784 waitpid (ptid_get_pid (inferior_ptid), &waitval, WNOHANG);
787 /* Abnormal death. */
788 ourstatus->kind = TARGET_WAITKIND_SIGNALLED;
789 ourstatus->value.sig = exit_signo;
794 ourstatus->kind = TARGET_WAITKIND_EXITED;
795 ourstatus->value.integer = WEXITSTATUS (waitval);
801 case _DEBUG_WHY_REQUESTED:
802 /* We are assuming a requested stop is due to a SIGINT. */
803 ourstatus->kind = TARGET_WAITKIND_STOPPED;
804 ourstatus->value.sig = GDB_SIGNAL_INT;
810 return ptid_build (status.pid, 0, status.tid);
813 /* Read the current values of the inferior's registers, both the
814 general register set and floating point registers (if supported)
815 and update gdb's idea of their current values. */
817 procfs_fetch_registers (struct target_ops *ops,
818 struct regcache *regcache, int regno)
824 procfs_altreg altreg;
829 procfs_set_thread (inferior_ptid);
830 if (devctl (ctl_fd, DCMD_PROC_GETGREG, ®, sizeof (reg), ®size) == EOK)
831 nto_supply_gregset (regcache, (char *) ®.greg);
832 if (devctl (ctl_fd, DCMD_PROC_GETFPREG, ®, sizeof (reg), ®size)
834 nto_supply_fpregset (regcache, (char *) ®.fpreg);
835 if (devctl (ctl_fd, DCMD_PROC_GETALTREG, ®, sizeof (reg), ®size)
837 nto_supply_altregset (regcache, (char *) ®.altreg);
840 /* Helper for procfs_xfer_partial that handles memory transfers.
841 Arguments are like target_xfer_partial. */
843 static enum target_xfer_status
844 procfs_xfer_memory (gdb_byte *readbuf, const gdb_byte *writebuf,
845 ULONGEST memaddr, ULONGEST len, ULONGEST *xfered_len)
849 if (lseek (ctl_fd, (off_t) memaddr, SEEK_SET) != (off_t) memaddr)
850 return TARGET_XFER_E_IO;
852 if (writebuf != NULL)
853 nbytes = write (ctl_fd, writebuf, len);
855 nbytes = read (ctl_fd, readbuf, len);
857 return TARGET_XFER_E_IO;
858 *xfered_len = nbytes;
859 return TARGET_XFER_OK;
862 /* Target to_xfer_partial implementation. */
864 static enum target_xfer_status
865 procfs_xfer_partial (struct target_ops *ops, enum target_object object,
866 const char *annex, gdb_byte *readbuf,
867 const gdb_byte *writebuf, ULONGEST offset, ULONGEST len,
868 ULONGEST *xfered_len)
872 case TARGET_OBJECT_MEMORY:
873 return procfs_xfer_memory (readbuf, writebuf, offset, len, xfered_len);
875 return ops->beneath->to_xfer_partial (ops->beneath, object, annex,
876 readbuf, writebuf, offset, len);
880 /* Take a program previously attached to and detaches it.
881 The program resumes execution and will no longer stop
882 on signals, etc. We'd better not have left any breakpoints
883 in the program or it'll die when it hits one. */
885 procfs_detach (struct target_ops *ops, const char *args, int from_tty)
892 char *exec_file = get_exec_file (0);
895 printf_unfiltered ("Detaching from program: %s %s\n",
896 exec_file, target_pid_to_str (inferior_ptid));
897 gdb_flush (gdb_stdout);
900 siggnal = atoi (args);
903 SignalKill (nto_node (), ptid_get_pid (inferior_ptid), 0, siggnal, 0, 0);
908 pid = ptid_get_pid (inferior_ptid);
909 inferior_ptid = null_ptid;
910 detach_inferior (pid);
912 inf_child_maybe_unpush_target (ops);
916 procfs_breakpoint (CORE_ADDR addr, int type, int size)
923 errno = devctl (ctl_fd, DCMD_PROC_BREAK, &brk, sizeof (brk), 0);
930 procfs_insert_breakpoint (struct target_ops *ops, struct gdbarch *gdbarch,
931 struct bp_target_info *bp_tgt)
933 bp_tgt->placed_address = bp_tgt->reqstd_address;
934 return procfs_breakpoint (bp_tgt->placed_address, _DEBUG_BREAK_EXEC, 0);
938 procfs_remove_breakpoint (struct target_ops *ops, struct gdbarch *gdbarch,
939 struct bp_target_info *bp_tgt)
941 return procfs_breakpoint (bp_tgt->placed_address, _DEBUG_BREAK_EXEC, -1);
945 procfs_insert_hw_breakpoint (struct target_ops *self, struct gdbarch *gdbarch,
946 struct bp_target_info *bp_tgt)
948 bp_tgt->placed_address = bp_tgt->reqstd_address;
949 return procfs_breakpoint (bp_tgt->placed_address,
950 _DEBUG_BREAK_EXEC | _DEBUG_BREAK_HW, 0);
954 procfs_remove_hw_breakpoint (struct target_ops *self,
955 struct gdbarch *gdbarch,
956 struct bp_target_info *bp_tgt)
958 return procfs_breakpoint (bp_tgt->placed_address,
959 _DEBUG_BREAK_EXEC | _DEBUG_BREAK_HW, -1);
963 procfs_resume (struct target_ops *ops,
964 ptid_t ptid, int step, enum gdb_signal signo)
967 procfs_status status;
968 sigset_t *run_fault = (sigset_t *) (void *) &run.fault;
970 if (ptid_equal (inferior_ptid, null_ptid))
973 procfs_set_thread (ptid_equal (ptid, minus_one_ptid) ? inferior_ptid :
976 run.flags = _DEBUG_RUN_FAULT | _DEBUG_RUN_TRACE;
978 run.flags |= _DEBUG_RUN_STEP;
980 sigemptyset (run_fault);
981 sigaddset (run_fault, FLTBPT);
982 sigaddset (run_fault, FLTTRACE);
983 sigaddset (run_fault, FLTILL);
984 sigaddset (run_fault, FLTPRIV);
985 sigaddset (run_fault, FLTBOUNDS);
986 sigaddset (run_fault, FLTIOVF);
987 sigaddset (run_fault, FLTIZDIV);
988 sigaddset (run_fault, FLTFPE);
989 /* Peter V will be changing this at some point. */
990 sigaddset (run_fault, FLTPAGE);
992 run.flags |= _DEBUG_RUN_ARM;
994 signal_to_pass = gdb_signal_to_host (signo);
998 devctl (ctl_fd, DCMD_PROC_STATUS, &status, sizeof (status), 0);
999 signal_to_pass = gdb_signal_to_host (signo);
1000 if (status.why & (_DEBUG_WHY_SIGNALLED | _DEBUG_WHY_FAULTED))
1002 if (signal_to_pass != status.info.si_signo)
1004 SignalKill (nto_node (), ptid_get_pid (inferior_ptid), 0,
1005 signal_to_pass, 0, 0);
1006 run.flags |= _DEBUG_RUN_CLRFLT | _DEBUG_RUN_CLRSIG;
1008 else /* Let it kill the program without telling us. */
1009 sigdelset (&run.trace, signal_to_pass);
1013 run.flags |= _DEBUG_RUN_CLRSIG | _DEBUG_RUN_CLRFLT;
1015 errno = devctl (ctl_fd, DCMD_PROC_RUN, &run, sizeof (run), 0);
1018 perror (_("run error!\n"));
1024 procfs_mourn_inferior (struct target_ops *ops)
1026 if (!ptid_equal (inferior_ptid, null_ptid))
1028 SignalKill (nto_node (), ptid_get_pid (inferior_ptid), 0, SIGKILL, 0, 0);
1031 inferior_ptid = null_ptid;
1032 init_thread_list ();
1033 inf_child_mourn_inferior (ops);
1036 /* This function breaks up an argument string into an argument
1037 vector suitable for passing to execvp().
1038 E.g., on "run a b c d" this routine would get as input
1039 the string "a b c d", and as output it would fill in argv with
1040 the four arguments "a", "b", "c", "d". The only additional
1041 functionality is simple quoting. The gdb command:
1043 will fill in argv with the three args "a", "b c d", "e". */
1045 breakup_args (char *scratch, char **argv)
1047 char *pp, *cp = scratch;
1052 /* Scan past leading separators. */
1054 while (*cp == ' ' || *cp == '\t' || *cp == '\n')
1057 /* Break if at end of string. */
1065 quoting = strchr (cp, '"') ? 1 : 0;
1070 /* Scan for next arg separator. */
1073 cp = strchr (pp, '"');
1074 if ((cp == NULL) || (!quoting))
1075 cp = strchr (pp, ' ');
1077 cp = strchr (pp, '\t');
1079 cp = strchr (pp, '\n');
1081 /* No separators => end of string => break. */
1088 /* Replace the separator with a terminator. */
1092 /* Execv requires a null-terminated arg vector. */
1097 procfs_create_inferior (struct target_ops *ops, char *exec_file,
1098 char *allargs, char **env, int from_tty)
1100 struct inheritance inherit;
1104 const char *in = "", *out = "", *err = "";
1107 const char *inferior_io_terminal = get_inferior_io_terminal ();
1108 struct inferior *inf;
1110 argv = xmalloc (((strlen (allargs) + 1) / (unsigned) 2 + 2) *
1112 argv[0] = get_exec_file (1);
1116 argv[0] = exec_file;
1121 args = xstrdup (allargs);
1122 breakup_args (args, exec_file ? &argv[1] : &argv[0]);
1124 argv = nto_parse_redirection (argv, &in, &out, &err);
1126 fds[0] = STDIN_FILENO;
1127 fds[1] = STDOUT_FILENO;
1128 fds[2] = STDERR_FILENO;
1130 /* If the user specified I/O via gdb's --tty= arg, use it, but only
1131 if the i/o is not also being specified via redirection. */
1132 if (inferior_io_terminal)
1135 in = inferior_io_terminal;
1137 out = inferior_io_terminal;
1139 err = inferior_io_terminal;
1144 fd = open (in, O_RDONLY);
1152 fd = open (out, O_WRONLY);
1160 fd = open (err, O_WRONLY);
1167 /* Clear any pending SIGUSR1's but keep the behavior the same. */
1168 signal (SIGUSR1, signal (SIGUSR1, SIG_IGN));
1171 sigaddset (&set, SIGUSR1);
1172 sigprocmask (SIG_UNBLOCK, &set, NULL);
1174 memset (&inherit, 0, sizeof (inherit));
1176 if (ND_NODE_CMP (nto_procfs_node, ND_LOCAL_NODE) != 0)
1178 inherit.nd = nto_node ();
1179 inherit.flags |= SPAWN_SETND;
1180 inherit.flags &= ~SPAWN_EXEC;
1182 inherit.flags |= SPAWN_SETGROUP | SPAWN_HOLD;
1183 inherit.pgroup = SPAWN_NEWPGROUP;
1184 pid = spawnp (argv[0], 3, fds, &inherit, argv,
1185 ND_NODE_CMP (nto_procfs_node, ND_LOCAL_NODE) == 0 ? env : 0);
1188 sigprocmask (SIG_BLOCK, &set, NULL);
1191 error (_("Error spawning %s: %d (%s)"), argv[0], errno,
1192 safe_strerror (errno));
1194 if (fds[0] != STDIN_FILENO)
1196 if (fds[1] != STDOUT_FILENO)
1198 if (fds[2] != STDERR_FILENO)
1201 inferior_ptid = do_attach (pid_to_ptid (pid));
1202 procfs_update_thread_list (ops);
1204 inf = current_inferior ();
1205 inferior_appeared (inf, pid);
1206 inf->attach_flag = 0;
1208 flags = _DEBUG_FLAG_KLC; /* Kill-on-Last-Close flag. */
1209 errn = devctl (ctl_fd, DCMD_PROC_SET_FLAG, &flags, sizeof (flags), 0);
1212 /* FIXME: expected warning? */
1213 /* warning( "Failed to set Kill-on-Last-Close flag: errno = %d(%s)\n",
1214 errn, strerror(errn) ); */
1216 if (!target_is_pushed (ops))
1218 target_terminal_init ();
1220 if (exec_bfd != NULL
1221 || (symfile_objfile != NULL && symfile_objfile->obfd != NULL))
1222 solib_create_inferior_hook (0);
1226 procfs_stop (struct target_ops *self, ptid_t ptid)
1228 devctl (ctl_fd, DCMD_PROC_STOP, NULL, 0, 0);
1232 procfs_kill_inferior (struct target_ops *ops)
1234 target_mourn_inferior ();
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));
1334 /* Set list of signals to be handled in the target. */
1337 procfs_pass_signals (struct target_ops *self,
1338 int numsigs, unsigned char *pass_signals)
1342 sigfillset (&run.trace);
1344 for (signo = 1; signo < NSIG; signo++)
1346 int target_signo = gdb_signal_from_host (signo);
1347 if (target_signo < numsigs && pass_signals[target_signo])
1348 sigdelset (&run.trace, signo);
1352 static struct tidinfo *
1353 procfs_thread_info (pid_t pid, short tid)
1360 procfs_pid_to_str (struct target_ops *ops, ptid_t ptid)
1362 static char buf[1024];
1364 struct tidinfo *tip;
1366 pid = ptid_get_pid (ptid);
1367 tid = ptid_get_tid (ptid);
1369 n = snprintf (buf, 1023, "process %d", pid);
1372 tip = procfs_thread_info (pid, tid);
1374 snprintf (&buf[n], 1023, " (state = 0x%02x)", tip->state);
1380 /* to_can_run implementation for "target procfs". Note this really
1381 means "can this target be the default run target", which there can
1382 be only one, and we make it be "target native" like other ports.
1383 "target procfs <node>" wouldn't make sense as default run target, as
1387 procfs_can_run (struct target_ops *self)
1392 /* "target procfs". */
1393 static struct target_ops nto_procfs_ops;
1395 /* "target native". */
1396 static struct target_ops *nto_native_ops;
1398 /* to_open implementation for "target procfs". */
1401 procfs_open (const char *arg, int from_tty)
1403 procfs_open_1 (&nto_procfs_ops, arg, from_tty);
1406 /* to_open implementation for "target native". */
1409 procfs_native_open (const char *arg, int from_tty)
1411 procfs_open_1 (nto_native_ops, arg, from_tty);
1414 /* Create the "native" and "procfs" targets. */
1417 init_procfs_targets (void)
1419 struct target_ops *t = inf_child_target ();
1421 /* Leave to_shortname as "native". */
1422 t->to_longname = "QNX Neutrino local process";
1423 t->to_doc = "QNX Neutrino local process (started by the \"run\" command).";
1424 t->to_open = procfs_native_open;
1425 t->to_attach = procfs_attach;
1426 t->to_post_attach = procfs_post_attach;
1427 t->to_detach = procfs_detach;
1428 t->to_resume = procfs_resume;
1429 t->to_wait = procfs_wait;
1430 t->to_fetch_registers = procfs_fetch_registers;
1431 t->to_store_registers = procfs_store_registers;
1432 t->to_xfer_partial = procfs_xfer_partial;
1433 t->to_files_info = procfs_files_info;
1434 t->to_insert_breakpoint = procfs_insert_breakpoint;
1435 t->to_remove_breakpoint = procfs_remove_breakpoint;
1436 t->to_can_use_hw_breakpoint = procfs_can_use_hw_breakpoint;
1437 t->to_insert_hw_breakpoint = procfs_insert_hw_breakpoint;
1438 t->to_remove_hw_breakpoint = procfs_remove_hw_breakpoint;
1439 t->to_insert_watchpoint = procfs_insert_hw_watchpoint;
1440 t->to_remove_watchpoint = procfs_remove_hw_watchpoint;
1441 t->to_stopped_by_watchpoint = procfs_stopped_by_watchpoint;
1442 t->to_kill = procfs_kill_inferior;
1443 t->to_create_inferior = procfs_create_inferior;
1444 t->to_mourn_inferior = procfs_mourn_inferior;
1445 t->to_pass_signals = procfs_pass_signals;
1446 t->to_thread_alive = procfs_thread_alive;
1447 t->to_update_thread_list = procfs_update_thread_list;
1448 t->to_pid_to_str = procfs_pid_to_str;
1449 t->to_stop = procfs_stop;
1450 t->to_have_continuable_watchpoint = 1;
1451 t->to_extra_thread_info = nto_extra_thread_info;
1455 /* Register "target native". This is the default run target. */
1458 /* Register "target procfs <node>". */
1459 nto_procfs_ops = *t;
1460 nto_procfs_ops.to_shortname = "procfs";
1461 nto_procfs_ops.to_can_run = procfs_can_run;
1462 t->to_longname = "QNX Neutrino local or remote process";
1463 t->to_doc = "QNX Neutrino process. target procfs <node>";
1464 t->to_open = procfs_open;
1466 add_target (&nto_procfs_ops);
1469 #define OSTYPE_NTO 1
1472 _initialize_procfs (void)
1476 init_procfs_targets ();
1478 /* We use SIGUSR1 to gain control after we block waiting for a process.
1479 We use sigwaitevent to wait. */
1481 sigaddset (&set, SIGUSR1);
1482 sigprocmask (SIG_BLOCK, &set, NULL);
1484 /* Initially, make sure all signals are reported. */
1485 sigfillset (&run.trace);
1487 /* Stuff some information. */
1488 nto_cpuinfo_flags = SYSPAGE_ENTRY (cpuinfo)->flags;
1489 nto_cpuinfo_valid = 1;
1491 add_info ("pidlist", procfs_pidlist, _("pidlist"));
1492 add_info ("meminfo", procfs_meminfo, _("memory information"));
1494 nto_is_nto_target = procfs_is_nto_target;
1499 procfs_hw_watchpoint (int addr, int len, enum target_hw_bp_type type)
1506 brk.type = _DEBUG_BREAK_RD;
1509 brk.type = _DEBUG_BREAK_RW;
1511 default: /* Modify. */
1512 /* FIXME: brk.type = _DEBUG_BREAK_RWM gives EINVAL for some reason. */
1513 brk.type = _DEBUG_BREAK_RW;
1515 brk.type |= _DEBUG_BREAK_HW; /* Always ask for HW. */
1519 errno = devctl (ctl_fd, DCMD_PROC_BREAK, &brk, sizeof (brk), 0);
1522 perror (_("Failed to set hardware watchpoint"));
1529 procfs_can_use_hw_breakpoint (struct target_ops *self,
1531 int cnt, int othertype)
1537 procfs_remove_hw_watchpoint (struct target_ops *self,
1538 CORE_ADDR addr, int len,
1539 enum target_hw_bp_type type,
1540 struct expression *cond)
1542 return procfs_hw_watchpoint (addr, -1, type);
1546 procfs_insert_hw_watchpoint (struct target_ops *self,
1547 CORE_ADDR addr, int len,
1548 enum target_hw_bp_type type,
1549 struct expression *cond)
1551 return procfs_hw_watchpoint (addr, len, type);
1555 procfs_stopped_by_watchpoint (struct target_ops *ops)