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-2016 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>
39 #include "gdbthread.h"
44 #include "inf-child.h"
45 #include "common/filestuff.h"
48 #define _DEBUG_FLAG_TRACE (_DEBUG_FLAG_TRACE_EXEC|_DEBUG_FLAG_TRACE_RD|\
49 _DEBUG_FLAG_TRACE_WR|_DEBUG_FLAG_TRACE_MODIFY)
53 static sighandler_t ofunc;
55 static procfs_run run;
57 static ptid_t do_attach (ptid_t ptid);
59 static int procfs_can_use_hw_breakpoint (struct target_ops *self,
60 enum bptype, int, int);
62 static int procfs_insert_hw_watchpoint (struct target_ops *self,
63 CORE_ADDR addr, int len,
64 enum target_hw_bp_type type,
65 struct expression *cond);
67 static int procfs_remove_hw_watchpoint (struct target_ops *self,
68 CORE_ADDR addr, int len,
69 enum target_hw_bp_type type,
70 struct expression *cond);
72 static int procfs_stopped_by_watchpoint (struct target_ops *ops);
74 /* These two globals are only ever set in procfs_open_1, but are
75 referenced elsewhere. 'nto_procfs_node' is a flag used to say
76 whether we are local, or we should get the current node descriptor
77 for the remote QNX node. */
79 static unsigned nto_procfs_node = ND_LOCAL_NODE;
81 /* Return the current QNX Node, or error out. This is a simple
82 wrapper for the netmgr_strtond() function. The reason this
83 is required is because QNX node descriptors are transient so
84 we have to re-acquire them every time. */
90 if (ND_NODE_CMP (nto_procfs_node, ND_LOCAL_NODE) == 0
94 node = netmgr_strtond (nodestr, 0);
96 error (_("Lost the QNX node. Debug session probably over."));
101 static enum gdb_osabi
102 procfs_is_nto_target (bfd *abfd)
104 return GDB_OSABI_QNXNTO;
107 /* This is called when we call 'target native' or 'target procfs
108 <arg>' from the (gdb) prompt. For QNX6 (nto), the only valid arg
109 will be a QNX node string, eg: "/net/some_node". If arg is not a
110 valid QNX node, we will default to local. */
112 procfs_open_1 (struct target_ops *ops, const char *arg, int from_tty)
117 procfs_sysinfo *sysinfo;
118 struct cleanup *cleanups;
119 char nto_procfs_path[PATH_MAX];
121 /* Offer to kill previous inferiors before opening this target. */
122 target_preopen (from_tty);
124 nto_is_nto_target = procfs_is_nto_target;
126 /* Set the default node used for spawning to this one,
127 and only override it if there is a valid arg. */
132 nto_procfs_node = ND_LOCAL_NODE;
133 nodestr = (arg != NULL) ? xstrdup (arg) : NULL;
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",
159 (nodestr != NULL) ? nodestr : "", "/proc");
161 fd = open (nto_procfs_path, O_RDONLY);
164 printf_filtered ("Error opening %s : %d (%s)\n", nto_procfs_path, errno,
165 safe_strerror (errno));
166 error (_("Invalid procfs arg"));
168 cleanups = make_cleanup_close (fd);
170 sysinfo = (void *) buffer;
171 if (devctl (fd, DCMD_PROC_SYSINFO, sysinfo, sizeof buffer, 0) != EOK)
173 printf_filtered ("Error getting size: %d (%s)\n", errno,
174 safe_strerror (errno));
175 error (_("Devctl failed."));
179 total_size = sysinfo->total_size;
180 sysinfo = alloca (total_size);
183 printf_filtered ("Memory error: %d (%s)\n", errno,
184 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));
193 error (_("Devctl failed."));
198 nto_map_arch_to_cputype (gdbarch_bfd_arch_info
199 (target_gdbarch ())->arch_name))
200 error (_("Invalid target CPU."));
204 do_cleanups (cleanups);
206 inf_child_open_target (ops, arg, from_tty);
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->priv)
259 new_thread->priv = xmalloc (offsetof (struct private_thread_info,
262 memcpy (new_thread->priv->name, newname, newnamelen + 1);
264 else if (strcmp (newname, new_thread->priv->name) != 0)
266 /* Reallocate if neccessary. */
267 int oldnamelen = strlen (new_thread->priv->name);
269 if (oldnamelen < newnamelen)
270 new_thread->priv = xrealloc (new_thread->priv,
271 offsetof (struct private_thread_info,
274 memcpy (new_thread->priv->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->priv;
313 #endif /* _NTO_VERSION */
317 procfs_update_thread_list (struct target_ops *ops)
319 procfs_status status;
323 struct thread_info *new_thread;
330 pid = ptid_get_pid (inferior_ptid);
334 for (tid = 1;; ++tid)
336 if (status.tid == tid
337 && (devctl (ctl_fd, DCMD_PROC_TIDSTATUS, &status, sizeof (status), 0)
340 if (status.tid != tid)
341 /* The reason why this would not be equal is that devctl might have
342 returned different tid, meaning the requested tid no longer exists
343 (e.g. thread exited). */
345 ptid = ptid_build (pid, 0, tid);
346 new_thread = find_thread_ptid (ptid);
348 new_thread = add_thread (ptid);
349 update_thread_private_data (new_thread, tid, status.state, 0);
356 do_closedir_cleanup (void *dir)
362 procfs_pidlist (char *args, int from_tty)
365 struct dirent *dirp = NULL;
367 procfs_info *pidinfo = NULL;
368 procfs_debuginfo *info = NULL;
369 procfs_status *status = NULL;
370 pid_t num_threads = 0;
373 struct cleanup *cleanups;
374 char procfs_dir[PATH_MAX];
376 snprintf (procfs_dir, sizeof (procfs_dir), "%s%s",
377 (nodestr != NULL) ? nodestr : "", "/proc");
379 dp = opendir (procfs_dir);
382 fprintf_unfiltered (gdb_stderr, "failed to opendir \"%s\" - %d (%s)",
383 procfs_dir, errno, safe_strerror (errno));
387 cleanups = make_cleanup (do_closedir_cleanup, dp);
389 /* Start scan at first pid. */
395 struct cleanup *inner_cleanup;
397 /* Get the right pid and procfs path for the pid. */
403 do_cleanups (cleanups);
406 snprintf (buf, sizeof (buf), "%s%s/%s/as",
407 (nodestr != NULL) ? nodestr : "",
408 "/proc", dirp->d_name);
409 pid = atoi (dirp->d_name);
413 /* Open the procfs path. */
414 fd = open (buf, O_RDONLY);
417 fprintf_unfiltered (gdb_stderr, "failed to open %s - %d (%s)\n",
418 buf, errno, safe_strerror (errno));
421 inner_cleanup = make_cleanup_close (fd);
423 pidinfo = (procfs_info *) buf;
424 if (devctl (fd, DCMD_PROC_INFO, pidinfo, sizeof (buf), 0) != EOK)
426 fprintf_unfiltered (gdb_stderr,
427 "devctl DCMD_PROC_INFO failed - %d (%s)\n",
428 errno, safe_strerror (errno));
431 num_threads = pidinfo->num_threads;
433 info = (procfs_debuginfo *) buf;
434 if (devctl (fd, DCMD_PROC_MAPDEBUG_BASE, info, sizeof (buf), 0) != EOK)
435 strcpy (name, "unavailable");
437 strcpy (name, info->path);
439 /* Collect state info on all the threads. */
440 status = (procfs_status *) buf;
441 for (status->tid = 1; status->tid <= num_threads; status->tid++)
444 = devctl (fd, DCMD_PROC_TIDSTATUS, status, sizeof (buf), 0);
445 printf_filtered ("%s - %d", name, pid);
446 if (err == EOK && status->tid != 0)
447 printf_filtered ("/%d\n", status->tid);
450 printf_filtered ("\n");
455 do_cleanups (inner_cleanup);
457 while (dirp != NULL);
459 do_cleanups (cleanups);
464 procfs_meminfo (char *args, int from_tty)
466 procfs_mapinfo *mapinfos = NULL;
467 static int num_mapinfos = 0;
468 procfs_mapinfo *mapinfo_p, *mapinfo_p2;
469 int flags = ~0, err, num, i, j;
473 procfs_debuginfo info;
474 char buff[_POSIX_PATH_MAX];
482 unsigned debug_vaddr;
483 unsigned long long offset;
488 unsigned long long ino;
495 /* Get the number of map entrys. */
496 err = devctl (ctl_fd, DCMD_PROC_MAPINFO, NULL, 0, &num);
499 printf ("failed devctl num mapinfos - %d (%s)\n", err,
500 safe_strerror (err));
504 mapinfos = XNEWVEC (procfs_mapinfo, num);
507 mapinfo_p = mapinfos;
509 /* Fill the map entrys. */
510 err = devctl (ctl_fd, DCMD_PROC_MAPINFO, mapinfo_p, num
511 * sizeof (procfs_mapinfo), &num);
514 printf ("failed devctl mapinfos - %d (%s)\n", err, safe_strerror (err));
519 num = min (num, num_mapinfos);
521 /* Run through the list of mapinfos, and store the data and text info
522 so we can print it at the bottom of the loop. */
523 for (mapinfo_p = mapinfos, i = 0; i < num; i++, mapinfo_p++)
525 if (!(mapinfo_p->flags & flags))
528 if (mapinfo_p->ino == 0) /* Already visited. */
531 map.info.vaddr = mapinfo_p->vaddr;
533 err = devctl (ctl_fd, DCMD_PROC_MAPDEBUG, &map, sizeof (map), 0);
537 memset (&printme, 0, sizeof printme);
538 printme.dev = mapinfo_p->dev;
539 printme.ino = mapinfo_p->ino;
540 printme.text.addr = mapinfo_p->vaddr;
541 printme.text.size = mapinfo_p->size;
542 printme.text.flags = mapinfo_p->flags;
543 printme.text.offset = mapinfo_p->offset;
544 printme.text.debug_vaddr = map.info.vaddr;
545 strcpy (printme.name, map.info.path);
547 /* Check for matching data. */
548 for (mapinfo_p2 = mapinfos, j = 0; j < num; j++, mapinfo_p2++)
550 if (mapinfo_p2->vaddr != mapinfo_p->vaddr
551 && mapinfo_p2->ino == mapinfo_p->ino
552 && mapinfo_p2->dev == mapinfo_p->dev)
554 map.info.vaddr = mapinfo_p2->vaddr;
556 devctl (ctl_fd, DCMD_PROC_MAPDEBUG, &map, sizeof (map), 0);
560 if (strcmp (map.info.path, printme.name))
563 /* Lower debug_vaddr is always text, if nessessary, swap. */
564 if ((int) map.info.vaddr < (int) printme.text.debug_vaddr)
566 memcpy (&(printme.data), &(printme.text),
567 sizeof (printme.data));
568 printme.text.addr = mapinfo_p2->vaddr;
569 printme.text.size = mapinfo_p2->size;
570 printme.text.flags = mapinfo_p2->flags;
571 printme.text.offset = mapinfo_p2->offset;
572 printme.text.debug_vaddr = map.info.vaddr;
576 printme.data.addr = mapinfo_p2->vaddr;
577 printme.data.size = mapinfo_p2->size;
578 printme.data.flags = mapinfo_p2->flags;
579 printme.data.offset = mapinfo_p2->offset;
580 printme.data.debug_vaddr = map.info.vaddr;
587 printf_filtered ("%s\n", printme.name);
588 printf_filtered ("\ttext=%08x bytes @ 0x%08x\n", printme.text.size,
590 printf_filtered ("\t\tflags=%08x\n", printme.text.flags);
591 printf_filtered ("\t\tdebug=%08x\n", printme.text.debug_vaddr);
592 printf_filtered ("\t\toffset=%s\n", phex (printme.text.offset, 8));
593 if (printme.data.size)
595 printf_filtered ("\tdata=%08x bytes @ 0x%08x\n", printme.data.size,
597 printf_filtered ("\t\tflags=%08x\n", printme.data.flags);
598 printf_filtered ("\t\tdebug=%08x\n", printme.data.debug_vaddr);
599 printf_filtered ("\t\toffset=%s\n", phex (printme.data.offset, 8));
601 printf_filtered ("\tdev=0x%x\n", printme.dev);
602 printf_filtered ("\tino=0x%x\n", (unsigned int) printme.ino);
608 /* Print status information about what we're accessing. */
610 procfs_files_info (struct target_ops *ignore)
612 struct inferior *inf = current_inferior ();
614 printf_unfiltered ("\tUsing the running image of %s %s via %s.\n",
615 inf->attach_flag ? "attached" : "child",
616 target_pid_to_str (inferior_ptid),
617 (nodestr != NULL) ? nodestr : "local node");
620 /* Target to_pid_to_exec_file implementation. */
623 procfs_pid_to_exec_file (struct target_ops *ops, const int pid)
626 static char proc_path[PATH_MAX];
629 /* Read exe file name. */
630 snprintf (proc_path, sizeof (proc_path), "%s/proc/%d/exefile",
631 (nodestr != NULL) ? nodestr : "", pid);
632 proc_fd = open (proc_path, O_RDONLY);
636 rd = read (proc_fd, proc_path, sizeof (proc_path) - 1);
643 proc_path[rd] = '\0';
647 /* Attach to process PID, then initialize for debugging it. */
649 procfs_attach (struct target_ops *ops, const char *args, int from_tty)
653 struct inferior *inf;
655 pid = parse_pid_to_attach (args);
657 if (pid == getpid ())
658 error (_("Attaching GDB to itself is not a good idea..."));
662 exec_file = (char *) get_exec_file (0);
665 printf_unfiltered ("Attaching to program `%s', %s\n", exec_file,
666 target_pid_to_str (pid_to_ptid (pid)));
668 printf_unfiltered ("Attaching to %s\n",
669 target_pid_to_str (pid_to_ptid (pid)));
671 gdb_flush (gdb_stdout);
673 inferior_ptid = do_attach (pid_to_ptid (pid));
674 inf = current_inferior ();
675 inferior_appeared (inf, pid);
676 inf->attach_flag = 1;
678 if (!target_is_pushed (ops))
681 procfs_update_thread_list (ops);
685 procfs_post_attach (struct target_ops *self, pid_t pid)
688 solib_create_inferior_hook (0);
692 do_attach (ptid_t ptid)
694 procfs_status status;
695 struct sigevent event;
698 snprintf (path, PATH_MAX - 1, "%s%s/%d/as",
699 (nodestr != NULL) ? nodestr : "", "/proc", ptid_get_pid (ptid));
700 ctl_fd = open (path, O_RDWR);
702 error (_("Couldn't open proc file %s, error %d (%s)"), path, errno,
703 safe_strerror (errno));
704 if (devctl (ctl_fd, DCMD_PROC_STOP, &status, sizeof (status), 0) != EOK)
705 error (_("Couldn't stop process"));
707 /* Define a sigevent for process stopped notification. */
708 event.sigev_notify = SIGEV_SIGNAL_THREAD;
709 event.sigev_signo = SIGUSR1;
710 event.sigev_code = 0;
711 event.sigev_value.sival_ptr = NULL;
712 event.sigev_priority = -1;
713 devctl (ctl_fd, DCMD_PROC_EVENT, &event, sizeof (event), 0);
715 if (devctl (ctl_fd, DCMD_PROC_STATUS, &status, sizeof (status), 0) == EOK
716 && status.flags & _DEBUG_FLAG_STOPPED)
717 SignalKill (nto_node (), ptid_get_pid (ptid), 0, SIGCONT, 0, 0);
718 nto_init_solib_absolute_prefix ();
719 return ptid_build (ptid_get_pid (ptid), 0, status.tid);
722 /* Ask the user what to do when an interrupt is received. */
724 interrupt_query (void)
726 target_terminal_ours ();
728 if (query (_("Interrupted while waiting for the program.\n\
729 Give up (and stop debugging it)? ")))
731 target_mourn_inferior ();
735 target_terminal_inferior ();
738 /* The user typed ^C twice. */
740 nto_handle_sigint_twice (int signo)
742 signal (signo, ofunc);
744 signal (signo, nto_handle_sigint_twice);
748 nto_handle_sigint (int signo)
750 /* If this doesn't work, try more severe steps. */
751 signal (signo, nto_handle_sigint_twice);
753 target_interrupt (inferior_ptid);
757 procfs_wait (struct target_ops *ops,
758 ptid_t ptid, struct target_waitstatus *ourstatus, int options)
762 procfs_status status;
763 static int exit_signo = 0; /* To track signals that cause termination. */
765 ourstatus->kind = TARGET_WAITKIND_SPURIOUS;
767 if (ptid_equal (inferior_ptid, null_ptid))
769 ourstatus->kind = TARGET_WAITKIND_STOPPED;
770 ourstatus->value.sig = GDB_SIGNAL_0;
776 sigaddset (&set, SIGUSR1);
778 devctl (ctl_fd, DCMD_PROC_STATUS, &status, sizeof (status), 0);
779 while (!(status.flags & _DEBUG_FLAG_ISTOP))
781 ofunc = signal (SIGINT, nto_handle_sigint);
782 sigwaitinfo (&set, &info);
783 signal (SIGINT, ofunc);
784 devctl (ctl_fd, DCMD_PROC_STATUS, &status, sizeof (status), 0);
787 nto_inferior_data (NULL)->stopped_flags = status.flags;
788 nto_inferior_data (NULL)->stopped_pc = status.ip;
790 if (status.flags & _DEBUG_FLAG_SSTEP)
792 ourstatus->kind = TARGET_WAITKIND_STOPPED;
793 ourstatus->value.sig = GDB_SIGNAL_TRAP;
795 /* Was it a breakpoint? */
796 else if (status.flags & _DEBUG_FLAG_TRACE)
798 ourstatus->kind = TARGET_WAITKIND_STOPPED;
799 ourstatus->value.sig = GDB_SIGNAL_TRAP;
801 else if (status.flags & _DEBUG_FLAG_ISTOP)
805 case _DEBUG_WHY_SIGNALLED:
806 ourstatus->kind = TARGET_WAITKIND_STOPPED;
807 ourstatus->value.sig =
808 gdb_signal_from_host (status.info.si_signo);
811 case _DEBUG_WHY_FAULTED:
812 ourstatus->kind = TARGET_WAITKIND_STOPPED;
813 if (status.info.si_signo == SIGTRAP)
815 ourstatus->value.sig = 0;
820 ourstatus->value.sig =
821 gdb_signal_from_host (status.info.si_signo);
822 exit_signo = ourstatus->value.sig;
826 case _DEBUG_WHY_TERMINATED:
830 waitpid (ptid_get_pid (inferior_ptid), &waitval, WNOHANG);
833 /* Abnormal death. */
834 ourstatus->kind = TARGET_WAITKIND_SIGNALLED;
835 ourstatus->value.sig = exit_signo;
840 ourstatus->kind = TARGET_WAITKIND_EXITED;
841 ourstatus->value.integer = WEXITSTATUS (waitval);
847 case _DEBUG_WHY_REQUESTED:
848 /* We are assuming a requested stop is due to a SIGINT. */
849 ourstatus->kind = TARGET_WAITKIND_STOPPED;
850 ourstatus->value.sig = GDB_SIGNAL_INT;
856 return ptid_build (status.pid, 0, status.tid);
859 /* Read the current values of the inferior's registers, both the
860 general register set and floating point registers (if supported)
861 and update gdb's idea of their current values. */
863 procfs_fetch_registers (struct target_ops *ops,
864 struct regcache *regcache, int regno)
870 procfs_altreg altreg;
875 procfs_set_thread (inferior_ptid);
876 if (devctl (ctl_fd, DCMD_PROC_GETGREG, ®, sizeof (reg), ®size) == EOK)
877 nto_supply_gregset (regcache, (char *) ®.greg);
878 if (devctl (ctl_fd, DCMD_PROC_GETFPREG, ®, sizeof (reg), ®size)
880 nto_supply_fpregset (regcache, (char *) ®.fpreg);
881 if (devctl (ctl_fd, DCMD_PROC_GETALTREG, ®, sizeof (reg), ®size)
883 nto_supply_altregset (regcache, (char *) ®.altreg);
886 /* Helper for procfs_xfer_partial that handles memory transfers.
887 Arguments are like target_xfer_partial. */
889 static enum target_xfer_status
890 procfs_xfer_memory (gdb_byte *readbuf, const gdb_byte *writebuf,
891 ULONGEST memaddr, ULONGEST len, ULONGEST *xfered_len)
895 if (lseek (ctl_fd, (off_t) memaddr, SEEK_SET) != (off_t) memaddr)
896 return TARGET_XFER_E_IO;
898 if (writebuf != NULL)
899 nbytes = write (ctl_fd, writebuf, len);
901 nbytes = read (ctl_fd, readbuf, len);
903 return TARGET_XFER_E_IO;
904 *xfered_len = nbytes;
905 return TARGET_XFER_OK;
908 /* Target to_xfer_partial implementation. */
910 static enum target_xfer_status
911 procfs_xfer_partial (struct target_ops *ops, enum target_object object,
912 const char *annex, gdb_byte *readbuf,
913 const gdb_byte *writebuf, ULONGEST offset, ULONGEST len,
914 ULONGEST *xfered_len)
918 case TARGET_OBJECT_MEMORY:
919 return procfs_xfer_memory (readbuf, writebuf, offset, len, xfered_len);
920 case TARGET_OBJECT_AUXV:
924 CORE_ADDR initial_stack;
925 debug_process_t procinfo;
926 /* For 32-bit architecture, size of auxv_t is 8 bytes. */
927 const unsigned int sizeof_auxv_t = sizeof (auxv_t);
928 const unsigned int sizeof_tempbuf = 20 * sizeof_auxv_t;
930 gdb_byte *const tempbuf = alloca (sizeof_tempbuf);
933 return TARGET_XFER_E_IO;
935 err = devctl (ctl_fd, DCMD_PROC_INFO, &procinfo,
938 return TARGET_XFER_E_IO;
940 initial_stack = procinfo.initial_stack;
942 /* procfs is always 'self-hosted', no byte-order manipulation. */
943 tempread = nto_read_auxv_from_initial_stack (initial_stack, tempbuf,
946 tempread = min (tempread, len) - offset;
947 memcpy (readbuf, tempbuf + offset, tempread);
948 *xfered_len = tempread;
949 return tempread ? TARGET_XFER_OK : TARGET_XFER_EOF;
953 return ops->beneath->to_xfer_partial (ops->beneath, object, annex,
954 readbuf, writebuf, offset, len,
959 /* Take a program previously attached to and detaches it.
960 The program resumes execution and will no longer stop
961 on signals, etc. We'd better not have left any breakpoints
962 in the program or it'll die when it hits one. */
964 procfs_detach (struct target_ops *ops, const char *args, int from_tty)
971 char *exec_file = get_exec_file (0);
974 printf_unfiltered ("Detaching from program: %s %s\n",
975 exec_file, target_pid_to_str (inferior_ptid));
976 gdb_flush (gdb_stdout);
979 siggnal = atoi (args);
982 SignalKill (nto_node (), ptid_get_pid (inferior_ptid), 0, siggnal, 0, 0);
987 pid = ptid_get_pid (inferior_ptid);
988 inferior_ptid = null_ptid;
989 detach_inferior (pid);
991 inf_child_maybe_unpush_target (ops);
995 procfs_breakpoint (CORE_ADDR addr, int type, int size)
1002 errno = devctl (ctl_fd, DCMD_PROC_BREAK, &brk, sizeof (brk), 0);
1009 procfs_insert_breakpoint (struct target_ops *ops, struct gdbarch *gdbarch,
1010 struct bp_target_info *bp_tgt)
1012 bp_tgt->placed_address = bp_tgt->reqstd_address;
1013 return procfs_breakpoint (bp_tgt->placed_address, _DEBUG_BREAK_EXEC, 0);
1017 procfs_remove_breakpoint (struct target_ops *ops, struct gdbarch *gdbarch,
1018 struct bp_target_info *bp_tgt)
1020 return procfs_breakpoint (bp_tgt->placed_address, _DEBUG_BREAK_EXEC, -1);
1024 procfs_insert_hw_breakpoint (struct target_ops *self, struct gdbarch *gdbarch,
1025 struct bp_target_info *bp_tgt)
1027 bp_tgt->placed_address = bp_tgt->reqstd_address;
1028 return procfs_breakpoint (bp_tgt->placed_address,
1029 _DEBUG_BREAK_EXEC | _DEBUG_BREAK_HW, 0);
1033 procfs_remove_hw_breakpoint (struct target_ops *self,
1034 struct gdbarch *gdbarch,
1035 struct bp_target_info *bp_tgt)
1037 return procfs_breakpoint (bp_tgt->placed_address,
1038 _DEBUG_BREAK_EXEC | _DEBUG_BREAK_HW, -1);
1042 procfs_resume (struct target_ops *ops,
1043 ptid_t ptid, int step, enum gdb_signal signo)
1046 procfs_status status;
1047 sigset_t *run_fault = (sigset_t *) (void *) &run.fault;
1049 if (ptid_equal (inferior_ptid, null_ptid))
1052 procfs_set_thread (ptid_equal (ptid, minus_one_ptid) ? inferior_ptid :
1055 run.flags = _DEBUG_RUN_FAULT | _DEBUG_RUN_TRACE;
1057 run.flags |= _DEBUG_RUN_STEP;
1059 sigemptyset (run_fault);
1060 sigaddset (run_fault, FLTBPT);
1061 sigaddset (run_fault, FLTTRACE);
1062 sigaddset (run_fault, FLTILL);
1063 sigaddset (run_fault, FLTPRIV);
1064 sigaddset (run_fault, FLTBOUNDS);
1065 sigaddset (run_fault, FLTIOVF);
1066 sigaddset (run_fault, FLTIZDIV);
1067 sigaddset (run_fault, FLTFPE);
1068 /* Peter V will be changing this at some point. */
1069 sigaddset (run_fault, FLTPAGE);
1071 run.flags |= _DEBUG_RUN_ARM;
1073 signal_to_pass = gdb_signal_to_host (signo);
1077 devctl (ctl_fd, DCMD_PROC_STATUS, &status, sizeof (status), 0);
1078 signal_to_pass = gdb_signal_to_host (signo);
1079 if (status.why & (_DEBUG_WHY_SIGNALLED | _DEBUG_WHY_FAULTED))
1081 if (signal_to_pass != status.info.si_signo)
1083 SignalKill (nto_node (), ptid_get_pid (inferior_ptid), 0,
1084 signal_to_pass, 0, 0);
1085 run.flags |= _DEBUG_RUN_CLRFLT | _DEBUG_RUN_CLRSIG;
1087 else /* Let it kill the program without telling us. */
1088 sigdelset (&run.trace, signal_to_pass);
1092 run.flags |= _DEBUG_RUN_CLRSIG | _DEBUG_RUN_CLRFLT;
1094 errno = devctl (ctl_fd, DCMD_PROC_RUN, &run, sizeof (run), 0);
1097 perror (_("run error!\n"));
1103 procfs_mourn_inferior (struct target_ops *ops)
1105 if (!ptid_equal (inferior_ptid, null_ptid))
1107 SignalKill (nto_node (), ptid_get_pid (inferior_ptid), 0, SIGKILL, 0, 0);
1110 inferior_ptid = null_ptid;
1111 init_thread_list ();
1112 inf_child_mourn_inferior (ops);
1115 /* This function breaks up an argument string into an argument
1116 vector suitable for passing to execvp().
1117 E.g., on "run a b c d" this routine would get as input
1118 the string "a b c d", and as output it would fill in argv with
1119 the four arguments "a", "b", "c", "d". The only additional
1120 functionality is simple quoting. The gdb command:
1122 will fill in argv with the three args "a", "b c d", "e". */
1124 breakup_args (char *scratch, char **argv)
1126 char *pp, *cp = scratch;
1131 /* Scan past leading separators. */
1133 while (*cp == ' ' || *cp == '\t' || *cp == '\n')
1136 /* Break if at end of string. */
1144 quoting = strchr (cp, '"') ? 1 : 0;
1149 /* Scan for next arg separator. */
1152 cp = strchr (pp, '"');
1153 if ((cp == NULL) || (!quoting))
1154 cp = strchr (pp, ' ');
1156 cp = strchr (pp, '\t');
1158 cp = strchr (pp, '\n');
1160 /* No separators => end of string => break. */
1167 /* Replace the separator with a terminator. */
1171 /* Execv requires a null-terminated arg vector. */
1176 procfs_create_inferior (struct target_ops *ops, char *exec_file,
1177 char *allargs, char **env, int from_tty)
1179 struct inheritance inherit;
1183 const char *in = "", *out = "", *err = "";
1186 const char *inferior_io_terminal = get_inferior_io_terminal ();
1187 struct inferior *inf;
1189 argv = xmalloc (((strlen (allargs) + 1) / (unsigned) 2 + 2) *
1191 argv[0] = get_exec_file (1);
1195 argv[0] = exec_file;
1200 args = xstrdup (allargs);
1201 breakup_args (args, (exec_file != NULL) ? &argv[1] : &argv[0]);
1203 argv = nto_parse_redirection (argv, &in, &out, &err);
1205 fds[0] = STDIN_FILENO;
1206 fds[1] = STDOUT_FILENO;
1207 fds[2] = STDERR_FILENO;
1209 /* If the user specified I/O via gdb's --tty= arg, use it, but only
1210 if the i/o is not also being specified via redirection. */
1211 if (inferior_io_terminal)
1214 in = inferior_io_terminal;
1216 out = inferior_io_terminal;
1218 err = inferior_io_terminal;
1223 fd = open (in, O_RDONLY);
1231 fd = open (out, O_WRONLY);
1239 fd = open (err, O_WRONLY);
1246 /* Clear any pending SIGUSR1's but keep the behavior the same. */
1247 signal (SIGUSR1, signal (SIGUSR1, SIG_IGN));
1250 sigaddset (&set, SIGUSR1);
1251 sigprocmask (SIG_UNBLOCK, &set, NULL);
1253 memset (&inherit, 0, sizeof (inherit));
1255 if (ND_NODE_CMP (nto_procfs_node, ND_LOCAL_NODE) != 0)
1257 inherit.nd = nto_node ();
1258 inherit.flags |= SPAWN_SETND;
1259 inherit.flags &= ~SPAWN_EXEC;
1261 inherit.flags |= SPAWN_SETGROUP | SPAWN_HOLD;
1262 inherit.pgroup = SPAWN_NEWPGROUP;
1263 pid = spawnp (argv[0], 3, fds, &inherit, argv,
1264 ND_NODE_CMP (nto_procfs_node, ND_LOCAL_NODE) == 0 ? env : 0);
1267 sigprocmask (SIG_BLOCK, &set, NULL);
1270 error (_("Error spawning %s: %d (%s)"), argv[0], errno,
1271 safe_strerror (errno));
1273 if (fds[0] != STDIN_FILENO)
1275 if (fds[1] != STDOUT_FILENO)
1277 if (fds[2] != STDERR_FILENO)
1280 inferior_ptid = do_attach (pid_to_ptid (pid));
1281 procfs_update_thread_list (ops);
1283 inf = current_inferior ();
1284 inferior_appeared (inf, pid);
1285 inf->attach_flag = 0;
1287 flags = _DEBUG_FLAG_KLC; /* Kill-on-Last-Close flag. */
1288 errn = devctl (ctl_fd, DCMD_PROC_SET_FLAG, &flags, sizeof (flags), 0);
1291 /* FIXME: expected warning? */
1292 /* warning( "Failed to set Kill-on-Last-Close flag: errno = %d(%s)\n",
1293 errn, strerror(errn) ); */
1295 if (!target_is_pushed (ops))
1297 target_terminal_init ();
1299 if (exec_bfd != NULL
1300 || (symfile_objfile != NULL && symfile_objfile->obfd != NULL))
1301 solib_create_inferior_hook (0);
1305 procfs_interrupt (struct target_ops *self, ptid_t ptid)
1307 devctl (ctl_fd, DCMD_PROC_STOP, NULL, 0, 0);
1311 procfs_kill_inferior (struct target_ops *ops)
1313 target_mourn_inferior ();
1316 /* Fill buf with regset and return devctl cmd to do the setting. Return
1317 -1 if we fail to get the regset. Store size of regset in regsize. */
1319 get_regset (int regset, char *buf, int bufsize, int *regsize)
1321 int dev_get, dev_set;
1324 case NTO_REG_GENERAL:
1325 dev_get = DCMD_PROC_GETGREG;
1326 dev_set = DCMD_PROC_SETGREG;
1330 dev_get = DCMD_PROC_GETFPREG;
1331 dev_set = DCMD_PROC_SETFPREG;
1335 dev_get = DCMD_PROC_GETALTREG;
1336 dev_set = DCMD_PROC_SETALTREG;
1339 case NTO_REG_SYSTEM:
1343 if (devctl (ctl_fd, dev_get, buf, bufsize, regsize) != EOK)
1350 procfs_store_registers (struct target_ops *ops,
1351 struct regcache *regcache, int regno)
1357 procfs_altreg altreg;
1361 int len, regset, regsize, dev_set, err;
1364 if (ptid_equal (inferior_ptid, null_ptid))
1366 procfs_set_thread (inferior_ptid);
1370 for (regset = NTO_REG_GENERAL; regset < NTO_REG_END; regset++)
1372 dev_set = get_regset (regset, (char *) ®,
1373 sizeof (reg), ®size);
1377 if (nto_regset_fill (regcache, regset, (char *) ®) == -1)
1380 err = devctl (ctl_fd, dev_set, ®, regsize, 0);
1382 fprintf_unfiltered (gdb_stderr,
1383 "Warning unable to write regset %d: %s\n",
1384 regno, safe_strerror (err));
1389 regset = nto_regset_id (regno);
1393 dev_set = get_regset (regset, (char *) ®, sizeof (reg), ®size);
1397 len = nto_register_area (get_regcache_arch (regcache),
1398 regno, regset, &off);
1403 regcache_raw_collect (regcache, regno, (char *) ® + off);
1405 err = devctl (ctl_fd, dev_set, ®, regsize, 0);
1407 fprintf_unfiltered (gdb_stderr,
1408 "Warning unable to write regset %d: %s\n", regno,
1409 safe_strerror (err));
1413 /* Set list of signals to be handled in the target. */
1416 procfs_pass_signals (struct target_ops *self,
1417 int numsigs, unsigned char *pass_signals)
1421 sigfillset (&run.trace);
1423 for (signo = 1; signo < NSIG; signo++)
1425 int target_signo = gdb_signal_from_host (signo);
1426 if (target_signo < numsigs && pass_signals[target_signo])
1427 sigdelset (&run.trace, signo);
1432 procfs_pid_to_str (struct target_ops *ops, ptid_t ptid)
1434 static char buf[1024];
1436 struct tidinfo *tip;
1438 pid = ptid_get_pid (ptid);
1439 tid = ptid_get_tid (ptid);
1441 n = snprintf (buf, 1023, "process %d", pid);
1444 tip = procfs_thread_info (pid, tid);
1446 snprintf (&buf[n], 1023, " (state = 0x%02x)", tip->state);
1452 /* to_can_run implementation for "target procfs". Note this really
1453 means "can this target be the default run target", which there can
1454 be only one, and we make it be "target native" like other ports.
1455 "target procfs <node>" wouldn't make sense as default run target, as
1459 procfs_can_run (struct target_ops *self)
1464 /* "target procfs". */
1465 static struct target_ops nto_procfs_ops;
1467 /* "target native". */
1468 static struct target_ops *nto_native_ops;
1470 /* to_open implementation for "target procfs". */
1473 procfs_open (const char *arg, int from_tty)
1475 procfs_open_1 (&nto_procfs_ops, arg, from_tty);
1478 /* to_open implementation for "target native". */
1481 procfs_native_open (const char *arg, int from_tty)
1483 procfs_open_1 (nto_native_ops, arg, from_tty);
1486 /* Create the "native" and "procfs" targets. */
1489 init_procfs_targets (void)
1491 struct target_ops *t = inf_child_target ();
1493 /* Leave to_shortname as "native". */
1494 t->to_longname = "QNX Neutrino local process";
1495 t->to_doc = "QNX Neutrino local process (started by the \"run\" command).";
1496 t->to_open = procfs_native_open;
1497 t->to_attach = procfs_attach;
1498 t->to_post_attach = procfs_post_attach;
1499 t->to_detach = procfs_detach;
1500 t->to_resume = procfs_resume;
1501 t->to_wait = procfs_wait;
1502 t->to_fetch_registers = procfs_fetch_registers;
1503 t->to_store_registers = procfs_store_registers;
1504 t->to_xfer_partial = procfs_xfer_partial;
1505 t->to_files_info = procfs_files_info;
1506 t->to_insert_breakpoint = procfs_insert_breakpoint;
1507 t->to_remove_breakpoint = procfs_remove_breakpoint;
1508 t->to_can_use_hw_breakpoint = procfs_can_use_hw_breakpoint;
1509 t->to_insert_hw_breakpoint = procfs_insert_hw_breakpoint;
1510 t->to_remove_hw_breakpoint = procfs_remove_hw_breakpoint;
1511 t->to_insert_watchpoint = procfs_insert_hw_watchpoint;
1512 t->to_remove_watchpoint = procfs_remove_hw_watchpoint;
1513 t->to_stopped_by_watchpoint = procfs_stopped_by_watchpoint;
1514 t->to_kill = procfs_kill_inferior;
1515 t->to_create_inferior = procfs_create_inferior;
1516 t->to_mourn_inferior = procfs_mourn_inferior;
1517 t->to_pass_signals = procfs_pass_signals;
1518 t->to_thread_alive = procfs_thread_alive;
1519 t->to_update_thread_list = procfs_update_thread_list;
1520 t->to_pid_to_str = procfs_pid_to_str;
1521 t->to_interrupt = procfs_interrupt;
1522 t->to_have_continuable_watchpoint = 1;
1523 t->to_extra_thread_info = nto_extra_thread_info;
1524 t->to_pid_to_exec_file = procfs_pid_to_exec_file;
1528 /* Register "target native". This is the default run target. */
1531 /* Register "target procfs <node>". */
1532 nto_procfs_ops = *t;
1533 nto_procfs_ops.to_shortname = "procfs";
1534 nto_procfs_ops.to_can_run = procfs_can_run;
1535 t->to_longname = "QNX Neutrino local or remote process";
1536 t->to_doc = "QNX Neutrino process. target procfs <node>";
1537 t->to_open = procfs_open;
1539 add_target (&nto_procfs_ops);
1542 #define OSTYPE_NTO 1
1544 extern initialize_file_ftype _initialize_procfs;
1547 _initialize_procfs (void)
1551 init_procfs_targets ();
1553 /* We use SIGUSR1 to gain control after we block waiting for a process.
1554 We use sigwaitevent to wait. */
1556 sigaddset (&set, SIGUSR1);
1557 sigprocmask (SIG_BLOCK, &set, NULL);
1559 /* Initially, make sure all signals are reported. */
1560 sigfillset (&run.trace);
1562 /* Stuff some information. */
1563 nto_cpuinfo_flags = SYSPAGE_ENTRY (cpuinfo)->flags;
1564 nto_cpuinfo_valid = 1;
1566 add_info ("pidlist", procfs_pidlist, _("pidlist"));
1567 add_info ("meminfo", procfs_meminfo, _("memory information"));
1569 nto_is_nto_target = procfs_is_nto_target;
1574 procfs_hw_watchpoint (int addr, int len, enum target_hw_bp_type type)
1581 brk.type = _DEBUG_BREAK_RD;
1584 brk.type = _DEBUG_BREAK_RW;
1586 default: /* Modify. */
1587 /* FIXME: brk.type = _DEBUG_BREAK_RWM gives EINVAL for some reason. */
1588 brk.type = _DEBUG_BREAK_RW;
1590 brk.type |= _DEBUG_BREAK_HW; /* Always ask for HW. */
1594 errno = devctl (ctl_fd, DCMD_PROC_BREAK, &brk, sizeof (brk), 0);
1597 perror (_("Failed to set hardware watchpoint"));
1604 procfs_can_use_hw_breakpoint (struct target_ops *self,
1606 int cnt, int othertype)
1612 procfs_remove_hw_watchpoint (struct target_ops *self,
1613 CORE_ADDR addr, int len,
1614 enum target_hw_bp_type type,
1615 struct expression *cond)
1617 return procfs_hw_watchpoint (addr, -1, type);
1621 procfs_insert_hw_watchpoint (struct target_ops *self,
1622 CORE_ADDR addr, int len,
1623 enum target_hw_bp_type type,
1624 struct expression *cond)
1626 return procfs_hw_watchpoint (addr, len, type);
1630 procfs_stopped_by_watchpoint (struct target_ops *ops)
1632 /* NOTE: nto_stopped_by_watchpoint will be called ONLY while we are
1633 stopped due to a SIGTRAP. This assumes gdb works in 'all-stop' mode;
1634 future gdb versions will likely run in 'non-stop' mode in which case
1635 we will have to store/examine statuses per thread in question.
1636 Until then, this will work fine. */
1638 struct inferior *inf = current_inferior ();
1639 struct nto_inferior_data *inf_data;
1641 gdb_assert (inf != NULL);
1643 inf_data = nto_inferior_data (inf);
1645 return inf_data->stopped_flags
1646 & (_DEBUG_FLAG_TRACE_RD
1647 | _DEBUG_FLAG_TRACE_WR
1648 | _DEBUG_FLAG_TRACE_MODIFY);