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-2018 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"
46 #include "common/scoped_fd.h"
49 #define _DEBUG_FLAG_TRACE (_DEBUG_FLAG_TRACE_EXEC|_DEBUG_FLAG_TRACE_RD|\
50 _DEBUG_FLAG_TRACE_WR|_DEBUG_FLAG_TRACE_MODIFY)
54 static sighandler_t ofunc;
56 static procfs_run run;
58 /* Create the "native" and "procfs" targets. */
60 struct nto_procfs_target : public inf_child_target
62 void open (const char *arg, int from_tty) override;
64 void attach (const char *, int) override = 0;
66 void post_attach (int);
68 void detach (inferior *, int) override;
70 void resume (ptid_t, int, enum gdb_signal) override;
72 ptid_t wait (ptid_t, struct target_waitstatus *, int) override;
74 void fetch_registers (struct regcache *, int) override;
75 void store_registers (struct regcache *, int) override;
77 enum target_xfer_status xfer_partial (enum target_object object,
80 const gdb_byte *writebuf,
81 ULONGEST offset, ULONGEST len,
82 ULONGEST *xfered_len) override;
84 void files_info () override;
86 int insert_breakpoint (struct gdbarch *, struct bp_target_info *) override;
88 int remove_breakpoint (struct gdbarch *, struct bp_target_info *,
89 enum remove_bp_reason) override;
91 int can_use_hw_breakpoint (enum bptype, int, int) override;
93 int insert_hw_breakpoint (struct gdbarch *, struct bp_target_info *) override;
95 int remove_hw_breakpoint (struct gdbarch *, struct bp_target_info *) override;
97 int insert_watchpoint (CORE_ADDR, int, enum target_hw_bp_type,
98 struct expression *) override;
100 int remove_watchpoint (CORE_ADDR, int, enum target_hw_bp_type,
101 struct expression *) override;
103 bool stopped_by_watchpoint () override;
105 void kill () override;
107 void create_inferior (const char *, const std::string &,
108 char **, int) override;
110 void mourn_inferior () override;
112 void pass_signals (int, unsigned char *) override;
114 bool thread_alive (ptid_t ptid) override;
116 void update_thread_list () override;
118 const char *pid_to_str (ptid_t) override;
120 void interrupt () override;
122 const char *extra_thread_info (struct thread_info *) override;
124 char *pid_to_exec_file (int pid) override;
127 /* For "target native". */
129 static const target_info nto_native_target_info = {
131 N_("QNX Neutrino local process"),
132 N_("QNX Neutrino local process (started by the \"run\" command).")
135 class nto_procfs_target_native final : public nto_procfs_target
137 const target_info &info () const override
138 { return nto_native_target_info; }
141 /* For "target procfs <node>". */
143 static const target_info nto_procfs_target_info = {
145 N_("QNX Neutrino local or remote process"),
146 N_("QNX Neutrino process. target procfs NODE")
149 struct nto_procfs_target_procfs final : public nto_procfs_target
151 const target_info &info () const override
152 { return nto_procfs_target_info; }
155 static ptid_t do_attach (ptid_t ptid);
157 /* These two globals are only ever set in procfs_open_1, but are
158 referenced elsewhere. 'nto_procfs_node' is a flag used to say
159 whether we are local, or we should get the current node descriptor
160 for the remote QNX node. */
161 static char *nodestr;
162 static unsigned nto_procfs_node = ND_LOCAL_NODE;
164 /* Return the current QNX Node, or error out. This is a simple
165 wrapper for the netmgr_strtond() function. The reason this
166 is required is because QNX node descriptors are transient so
167 we have to re-acquire them every time. */
173 if (ND_NODE_CMP (nto_procfs_node, ND_LOCAL_NODE) == 0
175 return ND_LOCAL_NODE;
177 node = netmgr_strtond (nodestr, 0);
179 error (_("Lost the QNX node. Debug session probably over."));
184 static enum gdb_osabi
185 procfs_is_nto_target (bfd *abfd)
187 return GDB_OSABI_QNXNTO;
190 /* This is called when we call 'target native' or 'target procfs
191 <arg>' from the (gdb) prompt. For QNX6 (nto), the only valid arg
192 will be a QNX node string, eg: "/net/some_node". If arg is not a
193 valid QNX node, we will default to local. */
195 nto_procfs_target::open (const char *arg, int from_tty)
200 procfs_sysinfo *sysinfo;
201 char nto_procfs_path[PATH_MAX];
203 /* Offer to kill previous inferiors before opening this target. */
204 target_preopen (from_tty);
206 nto_is_nto_target = procfs_is_nto_target;
208 /* Set the default node used for spawning to this one,
209 and only override it if there is a valid arg. */
214 nto_procfs_node = ND_LOCAL_NODE;
215 nodestr = (arg != NULL) ? xstrdup (arg) : NULL;
221 nto_procfs_node = netmgr_strtond (nodestr, &endstr);
222 if (nto_procfs_node == -1)
224 if (errno == ENOTSUP)
225 printf_filtered ("QNX Net Manager not found.\n");
226 printf_filtered ("Invalid QNX node %s: error %d (%s).\n", nodestr,
227 errno, safe_strerror (errno));
230 nto_procfs_node = ND_LOCAL_NODE;
234 if (*(endstr - 1) == '/')
240 snprintf (nto_procfs_path, PATH_MAX - 1, "%s%s",
241 (nodestr != NULL) ? nodestr : "", "/proc");
243 scoped_fd fd (open (nto_procfs_path, O_RDONLY));
246 printf_filtered ("Error opening %s : %d (%s)\n", nto_procfs_path, errno,
247 safe_strerror (errno));
248 error (_("Invalid procfs arg"));
251 sysinfo = (void *) buffer;
252 if (devctl (fd.get (), DCMD_PROC_SYSINFO, sysinfo, sizeof buffer, 0) != EOK)
254 printf_filtered ("Error getting size: %d (%s)\n", errno,
255 safe_strerror (errno));
256 error (_("Devctl failed."));
260 total_size = sysinfo->total_size;
261 sysinfo = alloca (total_size);
264 printf_filtered ("Memory error: %d (%s)\n", errno,
265 safe_strerror (errno));
266 error (_("alloca failed."));
270 if (devctl (fd.get (), DCMD_PROC_SYSINFO, sysinfo, total_size, 0)
273 printf_filtered ("Error getting sysinfo: %d (%s)\n", errno,
274 safe_strerror (errno));
275 error (_("Devctl failed."));
280 nto_map_arch_to_cputype (gdbarch_bfd_arch_info
281 (target_gdbarch ())->arch_name))
282 error (_("Invalid target CPU."));
287 inf_child_target::open (arg, from_tty);
288 printf_filtered ("Debugging using %s\n", nto_procfs_path);
292 procfs_set_thread (ptid_t ptid)
297 devctl (ctl_fd, DCMD_PROC_CURTHREAD, &tid, sizeof (tid), 0);
300 /* Return true if the thread TH is still alive. */
303 nto_procfs_target::thread_alive (ptid_t ptid)
307 procfs_status status;
313 if (kill (pid, 0) == -1)
317 if ((err = devctl (ctl_fd, DCMD_PROC_TIDSTATUS,
318 &status, sizeof (status), 0)) != EOK)
321 /* Thread is alive or dead but not yet joined,
322 or dead and there is an alive (or dead unjoined) thread with
325 If the tid is not the same as requested, requested tid is dead. */
326 return (status.tid == tid) && (status.state != STATE_DEAD);
330 update_thread_private_data_name (struct thread_info *new_thread,
333 nto_thread_info *pti = get_nto_thread_info (new_thread);
335 gdb_assert (newname != NULL);
336 gdb_assert (new_thread != NULL);
340 pti = new nto_thread_info;
341 new_thread->priv.reset (pti);
348 update_thread_private_data (struct thread_info *new_thread,
349 pthread_t tid, int state, int flags)
352 struct _thread_name *tn;
353 procfs_threadctl tctl;
355 #if _NTO_VERSION > 630
356 gdb_assert (new_thread != NULL);
358 if (devctl (ctl_fd, DCMD_PROC_INFO, &pidinfo,
359 sizeof(pidinfo), 0) != EOK)
362 memset (&tctl, 0, sizeof (tctl));
363 tctl.cmd = _NTO_TCTL_NAME;
364 tn = (struct _thread_name *) (&tctl.data);
366 /* Fetch name for the given thread. */
368 tn->name_buf_len = sizeof (tctl.data) - sizeof (*tn);
369 tn->new_name_len = -1; /* Getting, not setting. */
370 if (devctl (ctl_fd, DCMD_PROC_THREADCTL, &tctl, sizeof (tctl), NULL) != EOK)
371 tn->name_buf[0] = '\0';
373 tn->name_buf[_NTO_THREAD_NAME_MAX] = '\0';
375 update_thread_private_data_name (new_thread, tn->name_buf);
377 nto_thread_info *pti = get_nto_thread_info (new_thread);
381 #endif /* _NTO_VERSION */
385 nto_procfs_target::update_thread_list ()
387 procfs_status status;
391 struct thread_info *new_thread;
398 pid = inferior_ptid.pid ();
402 for (tid = 1;; ++tid)
404 if (status.tid == tid
405 && (devctl (ctl_fd, DCMD_PROC_TIDSTATUS, &status, sizeof (status), 0)
408 if (status.tid != tid)
409 /* The reason why this would not be equal is that devctl might have
410 returned different tid, meaning the requested tid no longer exists
411 (e.g. thread exited). */
413 ptid = ptid_t (pid, 0, tid);
414 new_thread = find_thread_ptid (ptid);
416 new_thread = add_thread (ptid);
417 update_thread_private_data (new_thread, tid, status.state, 0);
424 procfs_pidlist (const char *args, int from_tty)
426 struct dirent *dirp = NULL;
428 procfs_info *pidinfo = NULL;
429 procfs_debuginfo *info = NULL;
430 procfs_status *status = NULL;
431 pid_t num_threads = 0;
434 char procfs_dir[PATH_MAX];
436 snprintf (procfs_dir, sizeof (procfs_dir), "%s%s",
437 (nodestr != NULL) ? nodestr : "", "/proc");
439 gdb_dir_up dp (opendir (procfs_dir));
442 fprintf_unfiltered (gdb_stderr, "failed to opendir \"%s\" - %d (%s)",
443 procfs_dir, errno, safe_strerror (errno));
447 /* Start scan at first pid. */
448 rewinddir (dp.get ());
452 /* Get the right pid and procfs path for the pid. */
455 dirp = readdir (dp.get ());
458 snprintf (buf, sizeof (buf), "%s%s/%s/as",
459 (nodestr != NULL) ? nodestr : "",
460 "/proc", dirp->d_name);
461 pid = atoi (dirp->d_name);
465 /* Open the procfs path. */
466 scoped_fd fd (open (buf, O_RDONLY));
469 fprintf_unfiltered (gdb_stderr, "failed to open %s - %d (%s)\n",
470 buf, errno, safe_strerror (errno));
474 pidinfo = (procfs_info *) buf;
475 if (devctl (fd.get (), DCMD_PROC_INFO, pidinfo, sizeof (buf), 0) != EOK)
477 fprintf_unfiltered (gdb_stderr,
478 "devctl DCMD_PROC_INFO failed - %d (%s)\n",
479 errno, safe_strerror (errno));
482 num_threads = pidinfo->num_threads;
484 info = (procfs_debuginfo *) buf;
485 if (devctl (fd.get (), DCMD_PROC_MAPDEBUG_BASE, info, sizeof (buf), 0)
487 strcpy (name, "unavailable");
489 strcpy (name, info->path);
491 /* Collect state info on all the threads. */
492 status = (procfs_status *) buf;
493 for (status->tid = 1; status->tid <= num_threads; status->tid++)
496 = devctl (fd.get (), DCMD_PROC_TIDSTATUS, status, sizeof (buf), 0);
497 printf_filtered ("%s - %d", name, pid);
498 if (err == EOK && status->tid != 0)
499 printf_filtered ("/%d\n", status->tid);
502 printf_filtered ("\n");
507 while (dirp != NULL);
511 procfs_meminfo (const char *args, int from_tty)
513 procfs_mapinfo *mapinfos = NULL;
514 static int num_mapinfos = 0;
515 procfs_mapinfo *mapinfo_p, *mapinfo_p2;
516 int flags = ~0, err, num, i, j;
520 procfs_debuginfo info;
521 char buff[_POSIX_PATH_MAX];
529 unsigned debug_vaddr;
530 unsigned long long offset;
535 unsigned long long ino;
542 /* Get the number of map entrys. */
543 err = devctl (ctl_fd, DCMD_PROC_MAPINFO, NULL, 0, &num);
546 printf ("failed devctl num mapinfos - %d (%s)\n", err,
547 safe_strerror (err));
551 mapinfos = XNEWVEC (procfs_mapinfo, num);
554 mapinfo_p = mapinfos;
556 /* Fill the map entrys. */
557 err = devctl (ctl_fd, DCMD_PROC_MAPINFO, mapinfo_p, num
558 * sizeof (procfs_mapinfo), &num);
561 printf ("failed devctl mapinfos - %d (%s)\n", err, safe_strerror (err));
566 num = std::min (num, num_mapinfos);
568 /* Run through the list of mapinfos, and store the data and text info
569 so we can print it at the bottom of the loop. */
570 for (mapinfo_p = mapinfos, i = 0; i < num; i++, mapinfo_p++)
572 if (!(mapinfo_p->flags & flags))
575 if (mapinfo_p->ino == 0) /* Already visited. */
578 map.info.vaddr = mapinfo_p->vaddr;
580 err = devctl (ctl_fd, DCMD_PROC_MAPDEBUG, &map, sizeof (map), 0);
584 memset (&printme, 0, sizeof printme);
585 printme.dev = mapinfo_p->dev;
586 printme.ino = mapinfo_p->ino;
587 printme.text.addr = mapinfo_p->vaddr;
588 printme.text.size = mapinfo_p->size;
589 printme.text.flags = mapinfo_p->flags;
590 printme.text.offset = mapinfo_p->offset;
591 printme.text.debug_vaddr = map.info.vaddr;
592 strcpy (printme.name, map.info.path);
594 /* Check for matching data. */
595 for (mapinfo_p2 = mapinfos, j = 0; j < num; j++, mapinfo_p2++)
597 if (mapinfo_p2->vaddr != mapinfo_p->vaddr
598 && mapinfo_p2->ino == mapinfo_p->ino
599 && mapinfo_p2->dev == mapinfo_p->dev)
601 map.info.vaddr = mapinfo_p2->vaddr;
603 devctl (ctl_fd, DCMD_PROC_MAPDEBUG, &map, sizeof (map), 0);
607 if (strcmp (map.info.path, printme.name))
610 /* Lower debug_vaddr is always text, if nessessary, swap. */
611 if ((int) map.info.vaddr < (int) printme.text.debug_vaddr)
613 memcpy (&(printme.data), &(printme.text),
614 sizeof (printme.data));
615 printme.text.addr = mapinfo_p2->vaddr;
616 printme.text.size = mapinfo_p2->size;
617 printme.text.flags = mapinfo_p2->flags;
618 printme.text.offset = mapinfo_p2->offset;
619 printme.text.debug_vaddr = map.info.vaddr;
623 printme.data.addr = mapinfo_p2->vaddr;
624 printme.data.size = mapinfo_p2->size;
625 printme.data.flags = mapinfo_p2->flags;
626 printme.data.offset = mapinfo_p2->offset;
627 printme.data.debug_vaddr = map.info.vaddr;
634 printf_filtered ("%s\n", printme.name);
635 printf_filtered ("\ttext=%08x bytes @ 0x%08x\n", printme.text.size,
637 printf_filtered ("\t\tflags=%08x\n", printme.text.flags);
638 printf_filtered ("\t\tdebug=%08x\n", printme.text.debug_vaddr);
639 printf_filtered ("\t\toffset=%s\n", phex (printme.text.offset, 8));
640 if (printme.data.size)
642 printf_filtered ("\tdata=%08x bytes @ 0x%08x\n", printme.data.size,
644 printf_filtered ("\t\tflags=%08x\n", printme.data.flags);
645 printf_filtered ("\t\tdebug=%08x\n", printme.data.debug_vaddr);
646 printf_filtered ("\t\toffset=%s\n", phex (printme.data.offset, 8));
648 printf_filtered ("\tdev=0x%x\n", printme.dev);
649 printf_filtered ("\tino=0x%x\n", (unsigned int) printme.ino);
655 /* Print status information about what we're accessing. */
657 nto_procfs_target::files_info ()
659 struct inferior *inf = current_inferior ();
661 printf_unfiltered ("\tUsing the running image of %s %s via %s.\n",
662 inf->attach_flag ? "attached" : "child",
663 target_pid_to_str (inferior_ptid),
664 (nodestr != NULL) ? nodestr : "local node");
667 /* Target to_pid_to_exec_file implementation. */
670 nto_procfs_target::pid_to_exec_file (const int pid)
673 static char proc_path[PATH_MAX];
676 /* Read exe file name. */
677 snprintf (proc_path, sizeof (proc_path), "%s/proc/%d/exefile",
678 (nodestr != NULL) ? nodestr : "", pid);
679 proc_fd = open (proc_path, O_RDONLY);
683 rd = read (proc_fd, proc_path, sizeof (proc_path) - 1);
690 proc_path[rd] = '\0';
694 /* Attach to process PID, then initialize for debugging it. */
696 nto_procfs_target::attach (const char *args, int from_tty)
700 struct inferior *inf;
702 pid = parse_pid_to_attach (args);
704 if (pid == getpid ())
705 error (_("Attaching GDB to itself is not a good idea..."));
709 exec_file = (char *) get_exec_file (0);
712 printf_unfiltered ("Attaching to program `%s', %s\n", exec_file,
713 target_pid_to_str (ptid_t (pid)));
715 printf_unfiltered ("Attaching to %s\n",
716 target_pid_to_str (ptid_t (pid)));
718 gdb_flush (gdb_stdout);
720 inferior_ptid = do_attach (ptid_t (pid));
721 inf = current_inferior ();
722 inferior_appeared (inf, pid);
723 inf->attach_flag = 1;
725 if (!target_is_pushed (ops))
728 procfs_update_thread_list (ops);
732 nto_procfs_target::post_attach (pid_t pid)
735 solib_create_inferior_hook (0);
739 do_attach (ptid_t ptid)
741 procfs_status status;
742 struct sigevent event;
745 snprintf (path, PATH_MAX - 1, "%s%s/%d/as",
746 (nodestr != NULL) ? nodestr : "", "/proc", ptid.pid ());
747 ctl_fd = open (path, O_RDWR);
749 error (_("Couldn't open proc file %s, error %d (%s)"), path, errno,
750 safe_strerror (errno));
751 if (devctl (ctl_fd, DCMD_PROC_STOP, &status, sizeof (status), 0) != EOK)
752 error (_("Couldn't stop process"));
754 /* Define a sigevent for process stopped notification. */
755 event.sigev_notify = SIGEV_SIGNAL_THREAD;
756 event.sigev_signo = SIGUSR1;
757 event.sigev_code = 0;
758 event.sigev_value.sival_ptr = NULL;
759 event.sigev_priority = -1;
760 devctl (ctl_fd, DCMD_PROC_EVENT, &event, sizeof (event), 0);
762 if (devctl (ctl_fd, DCMD_PROC_STATUS, &status, sizeof (status), 0) == EOK
763 && status.flags & _DEBUG_FLAG_STOPPED)
764 SignalKill (nto_node (), ptid.pid (), 0, SIGCONT, 0, 0);
765 nto_init_solib_absolute_prefix ();
766 return ptid_t (ptid.pid (), 0, status.tid);
769 /* Ask the user what to do when an interrupt is received. */
771 interrupt_query (void)
773 if (query (_("Interrupted while waiting for the program.\n\
774 Give up (and stop debugging it)? ")))
776 target_mourn_inferior (inferior_ptid);
781 /* The user typed ^C twice. */
783 nto_handle_sigint_twice (int signo)
785 signal (signo, ofunc);
787 signal (signo, nto_handle_sigint_twice);
791 nto_handle_sigint (int signo)
793 /* If this doesn't work, try more severe steps. */
794 signal (signo, nto_handle_sigint_twice);
800 nto_procfs_target::wait (ptid_t ptid, struct target_waitstatus *ourstatus,
805 procfs_status status;
806 static int exit_signo = 0; /* To track signals that cause termination. */
808 ourstatus->kind = TARGET_WAITKIND_SPURIOUS;
810 if (inferior_ptid == null_ptid)
812 ourstatus->kind = TARGET_WAITKIND_STOPPED;
813 ourstatus->value.sig = GDB_SIGNAL_0;
819 sigaddset (&set, SIGUSR1);
821 devctl (ctl_fd, DCMD_PROC_STATUS, &status, sizeof (status), 0);
822 while (!(status.flags & _DEBUG_FLAG_ISTOP))
824 ofunc = signal (SIGINT, nto_handle_sigint);
825 sigwaitinfo (&set, &info);
826 signal (SIGINT, ofunc);
827 devctl (ctl_fd, DCMD_PROC_STATUS, &status, sizeof (status), 0);
830 nto_inferior_data (NULL)->stopped_flags = status.flags;
831 nto_inferior_data (NULL)->stopped_pc = status.ip;
833 if (status.flags & _DEBUG_FLAG_SSTEP)
835 ourstatus->kind = TARGET_WAITKIND_STOPPED;
836 ourstatus->value.sig = GDB_SIGNAL_TRAP;
838 /* Was it a breakpoint? */
839 else if (status.flags & _DEBUG_FLAG_TRACE)
841 ourstatus->kind = TARGET_WAITKIND_STOPPED;
842 ourstatus->value.sig = GDB_SIGNAL_TRAP;
844 else if (status.flags & _DEBUG_FLAG_ISTOP)
848 case _DEBUG_WHY_SIGNALLED:
849 ourstatus->kind = TARGET_WAITKIND_STOPPED;
850 ourstatus->value.sig =
851 gdb_signal_from_host (status.info.si_signo);
854 case _DEBUG_WHY_FAULTED:
855 ourstatus->kind = TARGET_WAITKIND_STOPPED;
856 if (status.info.si_signo == SIGTRAP)
858 ourstatus->value.sig = 0;
863 ourstatus->value.sig =
864 gdb_signal_from_host (status.info.si_signo);
865 exit_signo = ourstatus->value.sig;
869 case _DEBUG_WHY_TERMINATED:
873 waitpid (inferior_ptid.pid (), &waitval, WNOHANG);
876 /* Abnormal death. */
877 ourstatus->kind = TARGET_WAITKIND_SIGNALLED;
878 ourstatus->value.sig = exit_signo;
883 ourstatus->kind = TARGET_WAITKIND_EXITED;
884 ourstatus->value.integer = WEXITSTATUS (waitval);
890 case _DEBUG_WHY_REQUESTED:
891 /* We are assuming a requested stop is due to a SIGINT. */
892 ourstatus->kind = TARGET_WAITKIND_STOPPED;
893 ourstatus->value.sig = GDB_SIGNAL_INT;
899 return ptid_t (status.pid, 0, status.tid);
902 /* Read the current values of the inferior's registers, both the
903 general register set and floating point registers (if supported)
904 and update gdb's idea of their current values. */
906 nto_procfs_target::fetch_registers (struct regcache *regcache, int regno)
912 procfs_altreg altreg;
917 procfs_set_thread (regcache->ptid ());
918 if (devctl (ctl_fd, DCMD_PROC_GETGREG, ®, sizeof (reg), ®size) == EOK)
919 nto_supply_gregset (regcache, (char *) ®.greg);
920 if (devctl (ctl_fd, DCMD_PROC_GETFPREG, ®, sizeof (reg), ®size)
922 nto_supply_fpregset (regcache, (char *) ®.fpreg);
923 if (devctl (ctl_fd, DCMD_PROC_GETALTREG, ®, sizeof (reg), ®size)
925 nto_supply_altregset (regcache, (char *) ®.altreg);
928 /* Helper for procfs_xfer_partial that handles memory transfers.
929 Arguments are like target_xfer_partial. */
931 static enum target_xfer_status
932 procfs_xfer_memory (gdb_byte *readbuf, const gdb_byte *writebuf,
933 ULONGEST memaddr, ULONGEST len, ULONGEST *xfered_len)
937 if (lseek (ctl_fd, (off_t) memaddr, SEEK_SET) != (off_t) memaddr)
938 return TARGET_XFER_E_IO;
940 if (writebuf != NULL)
941 nbytes = write (ctl_fd, writebuf, len);
943 nbytes = read (ctl_fd, readbuf, len);
945 return TARGET_XFER_E_IO;
946 *xfered_len = nbytes;
947 return TARGET_XFER_OK;
950 /* Target to_xfer_partial implementation. */
952 enum target_xfer_status
953 nto_procfs_target::xfer_partial (enum target_object object,
954 const char *annex, gdb_byte *readbuf,
955 const gdb_byte *writebuf, ULONGEST offset,
956 ULONGEST len, ULONGEST *xfered_len)
960 case TARGET_OBJECT_MEMORY:
961 return procfs_xfer_memory (readbuf, writebuf, offset, len, xfered_len);
962 case TARGET_OBJECT_AUXV:
966 CORE_ADDR initial_stack;
967 debug_process_t procinfo;
968 /* For 32-bit architecture, size of auxv_t is 8 bytes. */
969 const unsigned int sizeof_auxv_t = sizeof (auxv_t);
970 const unsigned int sizeof_tempbuf = 20 * sizeof_auxv_t;
972 gdb_byte *const tempbuf = alloca (sizeof_tempbuf);
975 return TARGET_XFER_E_IO;
977 err = devctl (ctl_fd, DCMD_PROC_INFO, &procinfo,
980 return TARGET_XFER_E_IO;
982 initial_stack = procinfo.initial_stack;
984 /* procfs is always 'self-hosted', no byte-order manipulation. */
985 tempread = nto_read_auxv_from_initial_stack (initial_stack, tempbuf,
988 tempread = std::min (tempread, len) - offset;
989 memcpy (readbuf, tempbuf + offset, tempread);
990 *xfered_len = tempread;
991 return tempread ? TARGET_XFER_OK : TARGET_XFER_EOF;
995 return this->beneath ()->xfer_partial (object, annex,
996 readbuf, writebuf, offset, len,
1001 /* Take a program previously attached to and detaches it.
1002 The program resumes execution and will no longer stop
1003 on signals, etc. We'd better not have left any breakpoints
1004 in the program or it'll die when it hits one. */
1006 nto_procfs_target::detach (inferior *inf, int from_tty)
1010 target_announce_detach ();
1013 SignalKill (nto_node (), inferior_ptid.pid (), 0, 0, 0, 0);
1018 pid = inferior_ptid.pid ();
1019 inferior_ptid = null_ptid;
1020 detach_inferior (pid);
1021 init_thread_list ();
1022 inf_child_maybe_unpush_target (ops);
1026 procfs_breakpoint (CORE_ADDR addr, int type, int size)
1033 errno = devctl (ctl_fd, DCMD_PROC_BREAK, &brk, sizeof (brk), 0);
1040 nto_procfs_target::insert_breakpoint (struct gdbarch *gdbarch,
1041 struct bp_target_info *bp_tgt)
1043 bp_tgt->placed_address = bp_tgt->reqstd_address;
1044 return procfs_breakpoint (bp_tgt->placed_address, _DEBUG_BREAK_EXEC, 0);
1048 nto_procfs_target::remove_breakpoint (struct gdbarch *gdbarch,
1049 struct bp_target_info *bp_tgt,
1050 enum remove_bp_reason reason)
1052 return procfs_breakpoint (bp_tgt->placed_address, _DEBUG_BREAK_EXEC, -1);
1056 nto_procfs_target::insert_hw_breakpoint (struct gdbarch *gdbarch,
1057 struct bp_target_info *bp_tgt)
1059 bp_tgt->placed_address = bp_tgt->reqstd_address;
1060 return procfs_breakpoint (bp_tgt->placed_address,
1061 _DEBUG_BREAK_EXEC | _DEBUG_BREAK_HW, 0);
1065 nto_procfs_target::remove_hw_breakpoint (struct gdbarch *gdbarch,
1066 struct bp_target_info *bp_tgt)
1068 return procfs_breakpoint (bp_tgt->placed_address,
1069 _DEBUG_BREAK_EXEC | _DEBUG_BREAK_HW, -1);
1073 nto_procfs_target::resume (ptid_t ptid, int step, enum gdb_signal signo)
1076 procfs_status status;
1077 sigset_t *run_fault = (sigset_t *) (void *) &run.fault;
1079 if (inferior_ptid == null_ptid)
1082 procfs_set_thread (ptid == minus_one_ptid ? inferior_ptid :
1085 run.flags = _DEBUG_RUN_FAULT | _DEBUG_RUN_TRACE;
1087 run.flags |= _DEBUG_RUN_STEP;
1089 sigemptyset (run_fault);
1090 sigaddset (run_fault, FLTBPT);
1091 sigaddset (run_fault, FLTTRACE);
1092 sigaddset (run_fault, FLTILL);
1093 sigaddset (run_fault, FLTPRIV);
1094 sigaddset (run_fault, FLTBOUNDS);
1095 sigaddset (run_fault, FLTIOVF);
1096 sigaddset (run_fault, FLTIZDIV);
1097 sigaddset (run_fault, FLTFPE);
1098 /* Peter V will be changing this at some point. */
1099 sigaddset (run_fault, FLTPAGE);
1101 run.flags |= _DEBUG_RUN_ARM;
1103 signal_to_pass = gdb_signal_to_host (signo);
1107 devctl (ctl_fd, DCMD_PROC_STATUS, &status, sizeof (status), 0);
1108 signal_to_pass = gdb_signal_to_host (signo);
1109 if (status.why & (_DEBUG_WHY_SIGNALLED | _DEBUG_WHY_FAULTED))
1111 if (signal_to_pass != status.info.si_signo)
1113 SignalKill (nto_node (), inferior_ptid.pid (), 0,
1114 signal_to_pass, 0, 0);
1115 run.flags |= _DEBUG_RUN_CLRFLT | _DEBUG_RUN_CLRSIG;
1117 else /* Let it kill the program without telling us. */
1118 sigdelset (&run.trace, signal_to_pass);
1122 run.flags |= _DEBUG_RUN_CLRSIG | _DEBUG_RUN_CLRFLT;
1124 errno = devctl (ctl_fd, DCMD_PROC_RUN, &run, sizeof (run), 0);
1127 perror (_("run error!\n"));
1133 nto_procfs_target::mourn_inferior ()
1135 if (inferior_ptid != null_ptid)
1137 SignalKill (nto_node (), inferior_ptid.pid (), 0, SIGKILL, 0, 0);
1140 inferior_ptid = null_ptid;
1141 init_thread_list ();
1142 inf_child_mourn_inferior (ops);
1145 /* This function breaks up an argument string into an argument
1146 vector suitable for passing to execvp().
1147 E.g., on "run a b c d" this routine would get as input
1148 the string "a b c d", and as output it would fill in argv with
1149 the four arguments "a", "b", "c", "d". The only additional
1150 functionality is simple quoting. The gdb command:
1152 will fill in argv with the three args "a", "b c d", "e". */
1154 breakup_args (char *scratch, char **argv)
1156 char *pp, *cp = scratch;
1161 /* Scan past leading separators. */
1163 while (*cp == ' ' || *cp == '\t' || *cp == '\n')
1166 /* Break if at end of string. */
1174 quoting = strchr (cp, '"') ? 1 : 0;
1179 /* Scan for next arg separator. */
1182 cp = strchr (pp, '"');
1183 if ((cp == NULL) || (!quoting))
1184 cp = strchr (pp, ' ');
1186 cp = strchr (pp, '\t');
1188 cp = strchr (pp, '\n');
1190 /* No separators => end of string => break. */
1197 /* Replace the separator with a terminator. */
1201 /* Execv requires a null-terminated arg vector. */
1206 nto_procfs_target::create_inferior (const char *exec_file,
1207 const std::string &allargs,
1208 char **env, int from_tty)
1210 struct inheritance inherit;
1214 const char *in = "", *out = "", *err = "";
1217 const char *inferior_io_terminal = get_inferior_io_terminal ();
1218 struct inferior *inf;
1220 argv = xmalloc ((allargs.size () / (unsigned) 2 + 2) *
1222 argv[0] = get_exec_file (1);
1226 argv[0] = exec_file;
1231 args = xstrdup (allargs.c_str ());
1232 breakup_args (args, (exec_file != NULL) ? &argv[1] : &argv[0]);
1234 argv = nto_parse_redirection (argv, &in, &out, &err);
1236 fds[0] = STDIN_FILENO;
1237 fds[1] = STDOUT_FILENO;
1238 fds[2] = STDERR_FILENO;
1240 /* If the user specified I/O via gdb's --tty= arg, use it, but only
1241 if the i/o is not also being specified via redirection. */
1242 if (inferior_io_terminal)
1245 in = inferior_io_terminal;
1247 out = inferior_io_terminal;
1249 err = inferior_io_terminal;
1254 fd = open (in, O_RDONLY);
1262 fd = open (out, O_WRONLY);
1270 fd = open (err, O_WRONLY);
1277 /* Clear any pending SIGUSR1's but keep the behavior the same. */
1278 signal (SIGUSR1, signal (SIGUSR1, SIG_IGN));
1281 sigaddset (&set, SIGUSR1);
1282 sigprocmask (SIG_UNBLOCK, &set, NULL);
1284 memset (&inherit, 0, sizeof (inherit));
1286 if (ND_NODE_CMP (nto_procfs_node, ND_LOCAL_NODE) != 0)
1288 inherit.nd = nto_node ();
1289 inherit.flags |= SPAWN_SETND;
1290 inherit.flags &= ~SPAWN_EXEC;
1292 inherit.flags |= SPAWN_SETGROUP | SPAWN_HOLD;
1293 inherit.pgroup = SPAWN_NEWPGROUP;
1294 pid = spawnp (argv[0], 3, fds, &inherit, argv,
1295 ND_NODE_CMP (nto_procfs_node, ND_LOCAL_NODE) == 0 ? env : 0);
1298 sigprocmask (SIG_BLOCK, &set, NULL);
1301 error (_("Error spawning %s: %d (%s)"), argv[0], errno,
1302 safe_strerror (errno));
1304 if (fds[0] != STDIN_FILENO)
1306 if (fds[1] != STDOUT_FILENO)
1308 if (fds[2] != STDERR_FILENO)
1311 inferior_ptid = do_attach (ptid_t (pid));
1312 procfs_update_thread_list (ops);
1314 inf = current_inferior ();
1315 inferior_appeared (inf, pid);
1316 inf->attach_flag = 0;
1318 flags = _DEBUG_FLAG_KLC; /* Kill-on-Last-Close flag. */
1319 errn = devctl (ctl_fd, DCMD_PROC_SET_FLAG, &flags, sizeof (flags), 0);
1322 /* FIXME: expected warning? */
1323 /* warning( "Failed to set Kill-on-Last-Close flag: errno = %d(%s)\n",
1324 errn, strerror(errn) ); */
1326 if (!target_is_pushed (ops))
1328 target_terminal::init ();
1330 if (exec_bfd != NULL
1331 || (symfile_objfile != NULL && symfile_objfile->obfd != NULL))
1332 solib_create_inferior_hook (0);
1336 nto_procfs_target::interrupt ()
1338 devctl (ctl_fd, DCMD_PROC_STOP, NULL, 0, 0);
1342 nto_procfs_target::kill ()
1344 target_mourn_inferior (inferior_ptid);
1347 /* Fill buf with regset and return devctl cmd to do the setting. Return
1348 -1 if we fail to get the regset. Store size of regset in regsize. */
1350 get_regset (int regset, char *buf, int bufsize, int *regsize)
1352 int dev_get, dev_set;
1355 case NTO_REG_GENERAL:
1356 dev_get = DCMD_PROC_GETGREG;
1357 dev_set = DCMD_PROC_SETGREG;
1361 dev_get = DCMD_PROC_GETFPREG;
1362 dev_set = DCMD_PROC_SETFPREG;
1366 dev_get = DCMD_PROC_GETALTREG;
1367 dev_set = DCMD_PROC_SETALTREG;
1370 case NTO_REG_SYSTEM:
1374 if (devctl (ctl_fd, dev_get, buf, bufsize, regsize) != EOK)
1381 nto_procfs_target::store_registers (struct regcache *regcache, int regno)
1387 procfs_altreg altreg;
1391 int len, regset, regsize, dev_set, err;
1393 ptid_t ptid = regcache->ptid ();
1395 if (ptid == null_ptid)
1397 procfs_set_thread (ptid);
1401 for (regset = NTO_REG_GENERAL; regset < NTO_REG_END; regset++)
1403 dev_set = get_regset (regset, (char *) ®,
1404 sizeof (reg), ®size);
1408 if (nto_regset_fill (regcache, regset, (char *) ®) == -1)
1411 err = devctl (ctl_fd, dev_set, ®, regsize, 0);
1413 fprintf_unfiltered (gdb_stderr,
1414 "Warning unable to write regset %d: %s\n",
1415 regno, safe_strerror (err));
1420 regset = nto_regset_id (regno);
1424 dev_set = get_regset (regset, (char *) ®, sizeof (reg), ®size);
1428 len = nto_register_area (regcache->arch (),
1429 regno, regset, &off);
1434 regcache->raw_collect (regno, (char *) ® + off);
1436 err = devctl (ctl_fd, dev_set, ®, regsize, 0);
1438 fprintf_unfiltered (gdb_stderr,
1439 "Warning unable to write regset %d: %s\n", regno,
1440 safe_strerror (err));
1444 /* Set list of signals to be handled in the target. */
1447 nto_procfs_target::pass_signals (int numsigs, unsigned char *pass_signals)
1451 sigfillset (&run.trace);
1453 for (signo = 1; signo < NSIG; signo++)
1455 int target_signo = gdb_signal_from_host (signo);
1456 if (target_signo < numsigs && pass_signals[target_signo])
1457 sigdelset (&run.trace, signo);
1462 nto_procfs_target::pid_to_str (ptid_t ptid)
1464 static char buf[1024];
1466 struct tidinfo *tip;
1471 n = snprintf (buf, 1023, "process %d", pid);
1474 tip = procfs_thread_info (pid, tid);
1476 snprintf (&buf[n], 1023, " (state = 0x%02x)", tip->state);
1482 /* to_can_run implementation for "target procfs". Note this really
1483 means "can this target be the default run target", which there can
1484 be only one, and we make it be "target native" like other ports.
1485 "target procfs <node>" wouldn't make sense as default run target, as
1489 nto_procfs_target::can_run ()
1494 /* "target procfs". */
1495 static nto_procfs_target_procfs nto_procfs_ops;
1497 /* "target native". */
1498 static nto_procfs_target_native nto_native_ops;
1500 /* Create the "native" and "procfs" targets. */
1503 init_procfs_targets (void)
1505 /* Register "target native". This is the default run target. */
1506 add_target (nto_native_target_info, inf_child_open_target);
1507 set_native_target (&nto_native_ops);
1509 /* Register "target procfs <node>". */
1510 add_target (nto_procfs_target_info, inf_child_open_target);
1513 #define OSTYPE_NTO 1
1516 _initialize_procfs (void)
1520 init_procfs_targets ();
1522 /* We use SIGUSR1 to gain control after we block waiting for a process.
1523 We use sigwaitevent to wait. */
1525 sigaddset (&set, SIGUSR1);
1526 sigprocmask (SIG_BLOCK, &set, NULL);
1528 /* Initially, make sure all signals are reported. */
1529 sigfillset (&run.trace);
1531 /* Stuff some information. */
1532 nto_cpuinfo_flags = SYSPAGE_ENTRY (cpuinfo)->flags;
1533 nto_cpuinfo_valid = 1;
1535 add_info ("pidlist", procfs_pidlist, _("pidlist"));
1536 add_info ("meminfo", procfs_meminfo, _("memory information"));
1538 nto_is_nto_target = procfs_is_nto_target;
1543 procfs_hw_watchpoint (int addr, int len, enum target_hw_bp_type type)
1550 brk.type = _DEBUG_BREAK_RD;
1553 brk.type = _DEBUG_BREAK_RW;
1555 default: /* Modify. */
1556 /* FIXME: brk.type = _DEBUG_BREAK_RWM gives EINVAL for some reason. */
1557 brk.type = _DEBUG_BREAK_RW;
1559 brk.type |= _DEBUG_BREAK_HW; /* Always ask for HW. */
1563 errno = devctl (ctl_fd, DCMD_PROC_BREAK, &brk, sizeof (brk), 0);
1566 perror (_("Failed to set hardware watchpoint"));
1573 nto_procfs_target::can_use_hw_breakpoint (enum bptype type,
1574 int cnt, int othertype)
1580 nto_procfs_target::remove_hw_watchpoint (CORE_ADDR addr, int len,
1581 enum target_hw_bp_type type,
1582 struct expression *cond)
1584 return procfs_hw_watchpoint (addr, -1, type);
1588 nto_procfs_target::insert_hw_watchpoint (CORE_ADDR addr, int len,
1589 enum target_hw_bp_type type,
1590 struct expression *cond)
1592 return procfs_hw_watchpoint (addr, len, type);
1596 nto_procfs_target::stopped_by_watchpoint ()
1598 /* NOTE: nto_stopped_by_watchpoint will be called ONLY while we are
1599 stopped due to a SIGTRAP. This assumes gdb works in 'all-stop' mode;
1600 future gdb versions will likely run in 'non-stop' mode in which case
1601 we will have to store/examine statuses per thread in question.
1602 Until then, this will work fine. */
1604 struct inferior *inf = current_inferior ();
1605 struct nto_inferior_data *inf_data;
1607 gdb_assert (inf != NULL);
1609 inf_data = nto_inferior_data (inf);
1611 return inf_data->stopped_flags
1612 & (_DEBUG_FLAG_TRACE_RD
1613 | _DEBUG_FLAG_TRACE_WR
1614 | _DEBUG_FLAG_TRACE_MODIFY);