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 static ptid_t do_attach (ptid_t ptid);
60 static int procfs_can_use_hw_breakpoint (struct target_ops *self,
61 enum bptype, int, int);
63 static int procfs_insert_hw_watchpoint (struct target_ops *self,
64 CORE_ADDR addr, int len,
65 enum target_hw_bp_type type,
66 struct expression *cond);
68 static int procfs_remove_hw_watchpoint (struct target_ops *self,
69 CORE_ADDR addr, int len,
70 enum target_hw_bp_type type,
71 struct expression *cond);
73 static int procfs_stopped_by_watchpoint (struct target_ops *ops);
75 /* These two globals are only ever set in procfs_open_1, but are
76 referenced elsewhere. 'nto_procfs_node' is a flag used to say
77 whether we are local, or we should get the current node descriptor
78 for the remote QNX node. */
80 static unsigned nto_procfs_node = ND_LOCAL_NODE;
82 /* Return the current QNX Node, or error out. This is a simple
83 wrapper for the netmgr_strtond() function. The reason this
84 is required is because QNX node descriptors are transient so
85 we have to re-acquire them every time. */
91 if (ND_NODE_CMP (nto_procfs_node, ND_LOCAL_NODE) == 0
95 node = netmgr_strtond (nodestr, 0);
97 error (_("Lost the QNX node. Debug session probably over."));
102 static enum gdb_osabi
103 procfs_is_nto_target (bfd *abfd)
105 return GDB_OSABI_QNXNTO;
108 /* This is called when we call 'target native' or 'target procfs
109 <arg>' from the (gdb) prompt. For QNX6 (nto), the only valid arg
110 will be a QNX node string, eg: "/net/some_node". If arg is not a
111 valid QNX node, we will default to local. */
113 procfs_open_1 (struct target_ops *ops, const char *arg, int from_tty)
118 procfs_sysinfo *sysinfo;
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 scoped_fd 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"));
169 sysinfo = (void *) buffer;
170 if (devctl (fd.get (), DCMD_PROC_SYSINFO, sysinfo, sizeof buffer, 0) != EOK)
172 printf_filtered ("Error getting size: %d (%s)\n", errno,
173 safe_strerror (errno));
174 error (_("Devctl failed."));
178 total_size = sysinfo->total_size;
179 sysinfo = alloca (total_size);
182 printf_filtered ("Memory error: %d (%s)\n", errno,
183 safe_strerror (errno));
184 error (_("alloca failed."));
188 if (devctl (fd.get (), DCMD_PROC_SYSINFO, sysinfo, total_size, 0)
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."));
205 inf_child_open_target (ops, arg, from_tty);
206 printf_filtered ("Debugging using %s\n", nto_procfs_path);
210 procfs_set_thread (ptid_t ptid)
214 tid = ptid_get_tid (ptid);
215 devctl (ctl_fd, DCMD_PROC_CURTHREAD, &tid, sizeof (tid), 0);
218 /* Return nonzero if the thread TH is still alive. */
220 procfs_thread_alive (struct target_ops *ops, ptid_t ptid)
224 procfs_status status;
227 tid = ptid_get_tid (ptid);
228 pid = ptid_get_pid (ptid);
230 if (kill (pid, 0) == -1)
234 if ((err = devctl (ctl_fd, DCMD_PROC_TIDSTATUS,
235 &status, sizeof (status), 0)) != EOK)
238 /* Thread is alive or dead but not yet joined,
239 or dead and there is an alive (or dead unjoined) thread with
242 If the tid is not the same as requested, requested tid is dead. */
243 return (status.tid == tid) && (status.state != STATE_DEAD);
247 update_thread_private_data_name (struct thread_info *new_thread,
250 nto_thread_info *pti = get_nto_thread_info (new_thread);
252 gdb_assert (newname != NULL);
253 gdb_assert (new_thread != NULL);
257 pti = new nto_thread_info;
258 new_thread->priv.reset (pti);
265 update_thread_private_data (struct thread_info *new_thread,
266 pthread_t tid, int state, int flags)
269 struct _thread_name *tn;
270 procfs_threadctl tctl;
272 #if _NTO_VERSION > 630
273 gdb_assert (new_thread != NULL);
275 if (devctl (ctl_fd, DCMD_PROC_INFO, &pidinfo,
276 sizeof(pidinfo), 0) != EOK)
279 memset (&tctl, 0, sizeof (tctl));
280 tctl.cmd = _NTO_TCTL_NAME;
281 tn = (struct _thread_name *) (&tctl.data);
283 /* Fetch name for the given thread. */
285 tn->name_buf_len = sizeof (tctl.data) - sizeof (*tn);
286 tn->new_name_len = -1; /* Getting, not setting. */
287 if (devctl (ctl_fd, DCMD_PROC_THREADCTL, &tctl, sizeof (tctl), NULL) != EOK)
288 tn->name_buf[0] = '\0';
290 tn->name_buf[_NTO_THREAD_NAME_MAX] = '\0';
292 update_thread_private_data_name (new_thread, tn->name_buf);
294 nto_thread_info *pti = get_nto_thread_info (new_thread);
298 #endif /* _NTO_VERSION */
302 procfs_update_thread_list (struct target_ops *ops)
304 procfs_status status;
308 struct thread_info *new_thread;
315 pid = ptid_get_pid (inferior_ptid);
319 for (tid = 1;; ++tid)
321 if (status.tid == tid
322 && (devctl (ctl_fd, DCMD_PROC_TIDSTATUS, &status, sizeof (status), 0)
325 if (status.tid != tid)
326 /* The reason why this would not be equal is that devctl might have
327 returned different tid, meaning the requested tid no longer exists
328 (e.g. thread exited). */
330 ptid = ptid_build (pid, 0, tid);
331 new_thread = find_thread_ptid (ptid);
333 new_thread = add_thread (ptid);
334 update_thread_private_data (new_thread, tid, status.state, 0);
341 do_closedir_cleanup (void *dir)
347 procfs_pidlist (const char *args, int from_tty)
350 struct dirent *dirp = NULL;
352 procfs_info *pidinfo = NULL;
353 procfs_debuginfo *info = NULL;
354 procfs_status *status = NULL;
355 pid_t num_threads = 0;
358 struct cleanup *cleanups;
359 char procfs_dir[PATH_MAX];
361 snprintf (procfs_dir, sizeof (procfs_dir), "%s%s",
362 (nodestr != NULL) ? nodestr : "", "/proc");
364 dp = opendir (procfs_dir);
367 fprintf_unfiltered (gdb_stderr, "failed to opendir \"%s\" - %d (%s)",
368 procfs_dir, errno, safe_strerror (errno));
372 cleanups = make_cleanup (do_closedir_cleanup, dp);
374 /* Start scan at first pid. */
379 /* Get the right pid and procfs path for the pid. */
385 do_cleanups (cleanups);
388 snprintf (buf, sizeof (buf), "%s%s/%s/as",
389 (nodestr != NULL) ? nodestr : "",
390 "/proc", dirp->d_name);
391 pid = atoi (dirp->d_name);
395 /* Open the procfs path. */
396 scoped_fd fd (open (buf, O_RDONLY));
399 fprintf_unfiltered (gdb_stderr, "failed to open %s - %d (%s)\n",
400 buf, errno, safe_strerror (errno));
404 pidinfo = (procfs_info *) buf;
405 if (devctl (fd.get (), DCMD_PROC_INFO, pidinfo, sizeof (buf), 0) != EOK)
407 fprintf_unfiltered (gdb_stderr,
408 "devctl DCMD_PROC_INFO failed - %d (%s)\n",
409 errno, safe_strerror (errno));
412 num_threads = pidinfo->num_threads;
414 info = (procfs_debuginfo *) buf;
415 if (devctl (fd.get (), DCMD_PROC_MAPDEBUG_BASE, info, sizeof (buf), 0)
417 strcpy (name, "unavailable");
419 strcpy (name, info->path);
421 /* Collect state info on all the threads. */
422 status = (procfs_status *) buf;
423 for (status->tid = 1; status->tid <= num_threads; status->tid++)
426 = devctl (fd.get (), DCMD_PROC_TIDSTATUS, status, sizeof (buf), 0);
427 printf_filtered ("%s - %d", name, pid);
428 if (err == EOK && status->tid != 0)
429 printf_filtered ("/%d\n", status->tid);
432 printf_filtered ("\n");
437 while (dirp != NULL);
439 do_cleanups (cleanups);
444 procfs_meminfo (const char *args, int from_tty)
446 procfs_mapinfo *mapinfos = NULL;
447 static int num_mapinfos = 0;
448 procfs_mapinfo *mapinfo_p, *mapinfo_p2;
449 int flags = ~0, err, num, i, j;
453 procfs_debuginfo info;
454 char buff[_POSIX_PATH_MAX];
462 unsigned debug_vaddr;
463 unsigned long long offset;
468 unsigned long long ino;
475 /* Get the number of map entrys. */
476 err = devctl (ctl_fd, DCMD_PROC_MAPINFO, NULL, 0, &num);
479 printf ("failed devctl num mapinfos - %d (%s)\n", err,
480 safe_strerror (err));
484 mapinfos = XNEWVEC (procfs_mapinfo, num);
487 mapinfo_p = mapinfos;
489 /* Fill the map entrys. */
490 err = devctl (ctl_fd, DCMD_PROC_MAPINFO, mapinfo_p, num
491 * sizeof (procfs_mapinfo), &num);
494 printf ("failed devctl mapinfos - %d (%s)\n", err, safe_strerror (err));
499 num = std::min (num, num_mapinfos);
501 /* Run through the list of mapinfos, and store the data and text info
502 so we can print it at the bottom of the loop. */
503 for (mapinfo_p = mapinfos, i = 0; i < num; i++, mapinfo_p++)
505 if (!(mapinfo_p->flags & flags))
508 if (mapinfo_p->ino == 0) /* Already visited. */
511 map.info.vaddr = mapinfo_p->vaddr;
513 err = devctl (ctl_fd, DCMD_PROC_MAPDEBUG, &map, sizeof (map), 0);
517 memset (&printme, 0, sizeof printme);
518 printme.dev = mapinfo_p->dev;
519 printme.ino = mapinfo_p->ino;
520 printme.text.addr = mapinfo_p->vaddr;
521 printme.text.size = mapinfo_p->size;
522 printme.text.flags = mapinfo_p->flags;
523 printme.text.offset = mapinfo_p->offset;
524 printme.text.debug_vaddr = map.info.vaddr;
525 strcpy (printme.name, map.info.path);
527 /* Check for matching data. */
528 for (mapinfo_p2 = mapinfos, j = 0; j < num; j++, mapinfo_p2++)
530 if (mapinfo_p2->vaddr != mapinfo_p->vaddr
531 && mapinfo_p2->ino == mapinfo_p->ino
532 && mapinfo_p2->dev == mapinfo_p->dev)
534 map.info.vaddr = mapinfo_p2->vaddr;
536 devctl (ctl_fd, DCMD_PROC_MAPDEBUG, &map, sizeof (map), 0);
540 if (strcmp (map.info.path, printme.name))
543 /* Lower debug_vaddr is always text, if nessessary, swap. */
544 if ((int) map.info.vaddr < (int) printme.text.debug_vaddr)
546 memcpy (&(printme.data), &(printme.text),
547 sizeof (printme.data));
548 printme.text.addr = mapinfo_p2->vaddr;
549 printme.text.size = mapinfo_p2->size;
550 printme.text.flags = mapinfo_p2->flags;
551 printme.text.offset = mapinfo_p2->offset;
552 printme.text.debug_vaddr = map.info.vaddr;
556 printme.data.addr = mapinfo_p2->vaddr;
557 printme.data.size = mapinfo_p2->size;
558 printme.data.flags = mapinfo_p2->flags;
559 printme.data.offset = mapinfo_p2->offset;
560 printme.data.debug_vaddr = map.info.vaddr;
567 printf_filtered ("%s\n", printme.name);
568 printf_filtered ("\ttext=%08x bytes @ 0x%08x\n", printme.text.size,
570 printf_filtered ("\t\tflags=%08x\n", printme.text.flags);
571 printf_filtered ("\t\tdebug=%08x\n", printme.text.debug_vaddr);
572 printf_filtered ("\t\toffset=%s\n", phex (printme.text.offset, 8));
573 if (printme.data.size)
575 printf_filtered ("\tdata=%08x bytes @ 0x%08x\n", printme.data.size,
577 printf_filtered ("\t\tflags=%08x\n", printme.data.flags);
578 printf_filtered ("\t\tdebug=%08x\n", printme.data.debug_vaddr);
579 printf_filtered ("\t\toffset=%s\n", phex (printme.data.offset, 8));
581 printf_filtered ("\tdev=0x%x\n", printme.dev);
582 printf_filtered ("\tino=0x%x\n", (unsigned int) printme.ino);
588 /* Print status information about what we're accessing. */
590 procfs_files_info (struct target_ops *ignore)
592 struct inferior *inf = current_inferior ();
594 printf_unfiltered ("\tUsing the running image of %s %s via %s.\n",
595 inf->attach_flag ? "attached" : "child",
596 target_pid_to_str (inferior_ptid),
597 (nodestr != NULL) ? nodestr : "local node");
600 /* Target to_pid_to_exec_file implementation. */
603 procfs_pid_to_exec_file (struct target_ops *ops, const int pid)
606 static char proc_path[PATH_MAX];
609 /* Read exe file name. */
610 snprintf (proc_path, sizeof (proc_path), "%s/proc/%d/exefile",
611 (nodestr != NULL) ? nodestr : "", pid);
612 proc_fd = open (proc_path, O_RDONLY);
616 rd = read (proc_fd, proc_path, sizeof (proc_path) - 1);
623 proc_path[rd] = '\0';
627 /* Attach to process PID, then initialize for debugging it. */
629 procfs_attach (struct target_ops *ops, const char *args, int from_tty)
633 struct inferior *inf;
635 pid = parse_pid_to_attach (args);
637 if (pid == getpid ())
638 error (_("Attaching GDB to itself is not a good idea..."));
642 exec_file = (char *) get_exec_file (0);
645 printf_unfiltered ("Attaching to program `%s', %s\n", exec_file,
646 target_pid_to_str (pid_to_ptid (pid)));
648 printf_unfiltered ("Attaching to %s\n",
649 target_pid_to_str (pid_to_ptid (pid)));
651 gdb_flush (gdb_stdout);
653 inferior_ptid = do_attach (pid_to_ptid (pid));
654 inf = current_inferior ();
655 inferior_appeared (inf, pid);
656 inf->attach_flag = 1;
658 if (!target_is_pushed (ops))
661 procfs_update_thread_list (ops);
665 procfs_post_attach (struct target_ops *self, pid_t pid)
668 solib_create_inferior_hook (0);
672 do_attach (ptid_t ptid)
674 procfs_status status;
675 struct sigevent event;
678 snprintf (path, PATH_MAX - 1, "%s%s/%d/as",
679 (nodestr != NULL) ? nodestr : "", "/proc", ptid_get_pid (ptid));
680 ctl_fd = open (path, O_RDWR);
682 error (_("Couldn't open proc file %s, error %d (%s)"), path, errno,
683 safe_strerror (errno));
684 if (devctl (ctl_fd, DCMD_PROC_STOP, &status, sizeof (status), 0) != EOK)
685 error (_("Couldn't stop process"));
687 /* Define a sigevent for process stopped notification. */
688 event.sigev_notify = SIGEV_SIGNAL_THREAD;
689 event.sigev_signo = SIGUSR1;
690 event.sigev_code = 0;
691 event.sigev_value.sival_ptr = NULL;
692 event.sigev_priority = -1;
693 devctl (ctl_fd, DCMD_PROC_EVENT, &event, sizeof (event), 0);
695 if (devctl (ctl_fd, DCMD_PROC_STATUS, &status, sizeof (status), 0) == EOK
696 && status.flags & _DEBUG_FLAG_STOPPED)
697 SignalKill (nto_node (), ptid_get_pid (ptid), 0, SIGCONT, 0, 0);
698 nto_init_solib_absolute_prefix ();
699 return ptid_build (ptid_get_pid (ptid), 0, status.tid);
702 /* Ask the user what to do when an interrupt is received. */
704 interrupt_query (void)
706 if (query (_("Interrupted while waiting for the program.\n\
707 Give up (and stop debugging it)? ")))
709 target_mourn_inferior (inferior_ptid);
714 /* The user typed ^C twice. */
716 nto_handle_sigint_twice (int signo)
718 signal (signo, ofunc);
720 signal (signo, nto_handle_sigint_twice);
724 nto_handle_sigint (int signo)
726 /* If this doesn't work, try more severe steps. */
727 signal (signo, nto_handle_sigint_twice);
733 procfs_wait (struct target_ops *ops,
734 ptid_t ptid, struct target_waitstatus *ourstatus, int options)
738 procfs_status status;
739 static int exit_signo = 0; /* To track signals that cause termination. */
741 ourstatus->kind = TARGET_WAITKIND_SPURIOUS;
743 if (ptid_equal (inferior_ptid, null_ptid))
745 ourstatus->kind = TARGET_WAITKIND_STOPPED;
746 ourstatus->value.sig = GDB_SIGNAL_0;
752 sigaddset (&set, SIGUSR1);
754 devctl (ctl_fd, DCMD_PROC_STATUS, &status, sizeof (status), 0);
755 while (!(status.flags & _DEBUG_FLAG_ISTOP))
757 ofunc = signal (SIGINT, nto_handle_sigint);
758 sigwaitinfo (&set, &info);
759 signal (SIGINT, ofunc);
760 devctl (ctl_fd, DCMD_PROC_STATUS, &status, sizeof (status), 0);
763 nto_inferior_data (NULL)->stopped_flags = status.flags;
764 nto_inferior_data (NULL)->stopped_pc = status.ip;
766 if (status.flags & _DEBUG_FLAG_SSTEP)
768 ourstatus->kind = TARGET_WAITKIND_STOPPED;
769 ourstatus->value.sig = GDB_SIGNAL_TRAP;
771 /* Was it a breakpoint? */
772 else if (status.flags & _DEBUG_FLAG_TRACE)
774 ourstatus->kind = TARGET_WAITKIND_STOPPED;
775 ourstatus->value.sig = GDB_SIGNAL_TRAP;
777 else if (status.flags & _DEBUG_FLAG_ISTOP)
781 case _DEBUG_WHY_SIGNALLED:
782 ourstatus->kind = TARGET_WAITKIND_STOPPED;
783 ourstatus->value.sig =
784 gdb_signal_from_host (status.info.si_signo);
787 case _DEBUG_WHY_FAULTED:
788 ourstatus->kind = TARGET_WAITKIND_STOPPED;
789 if (status.info.si_signo == SIGTRAP)
791 ourstatus->value.sig = 0;
796 ourstatus->value.sig =
797 gdb_signal_from_host (status.info.si_signo);
798 exit_signo = ourstatus->value.sig;
802 case _DEBUG_WHY_TERMINATED:
806 waitpid (ptid_get_pid (inferior_ptid), &waitval, WNOHANG);
809 /* Abnormal death. */
810 ourstatus->kind = TARGET_WAITKIND_SIGNALLED;
811 ourstatus->value.sig = exit_signo;
816 ourstatus->kind = TARGET_WAITKIND_EXITED;
817 ourstatus->value.integer = WEXITSTATUS (waitval);
823 case _DEBUG_WHY_REQUESTED:
824 /* We are assuming a requested stop is due to a SIGINT. */
825 ourstatus->kind = TARGET_WAITKIND_STOPPED;
826 ourstatus->value.sig = GDB_SIGNAL_INT;
832 return ptid_build (status.pid, 0, status.tid);
835 /* Read the current values of the inferior's registers, both the
836 general register set and floating point registers (if supported)
837 and update gdb's idea of their current values. */
839 procfs_fetch_registers (struct target_ops *ops,
840 struct regcache *regcache, int regno)
846 procfs_altreg altreg;
851 procfs_set_thread (regcache_get_ptid (regcache));
852 if (devctl (ctl_fd, DCMD_PROC_GETGREG, ®, sizeof (reg), ®size) == EOK)
853 nto_supply_gregset (regcache, (char *) ®.greg);
854 if (devctl (ctl_fd, DCMD_PROC_GETFPREG, ®, sizeof (reg), ®size)
856 nto_supply_fpregset (regcache, (char *) ®.fpreg);
857 if (devctl (ctl_fd, DCMD_PROC_GETALTREG, ®, sizeof (reg), ®size)
859 nto_supply_altregset (regcache, (char *) ®.altreg);
862 /* Helper for procfs_xfer_partial that handles memory transfers.
863 Arguments are like target_xfer_partial. */
865 static enum target_xfer_status
866 procfs_xfer_memory (gdb_byte *readbuf, const gdb_byte *writebuf,
867 ULONGEST memaddr, ULONGEST len, ULONGEST *xfered_len)
871 if (lseek (ctl_fd, (off_t) memaddr, SEEK_SET) != (off_t) memaddr)
872 return TARGET_XFER_E_IO;
874 if (writebuf != NULL)
875 nbytes = write (ctl_fd, writebuf, len);
877 nbytes = read (ctl_fd, readbuf, len);
879 return TARGET_XFER_E_IO;
880 *xfered_len = nbytes;
881 return TARGET_XFER_OK;
884 /* Target to_xfer_partial implementation. */
886 static enum target_xfer_status
887 procfs_xfer_partial (struct target_ops *ops, enum target_object object,
888 const char *annex, gdb_byte *readbuf,
889 const gdb_byte *writebuf, ULONGEST offset, ULONGEST len,
890 ULONGEST *xfered_len)
894 case TARGET_OBJECT_MEMORY:
895 return procfs_xfer_memory (readbuf, writebuf, offset, len, xfered_len);
896 case TARGET_OBJECT_AUXV:
900 CORE_ADDR initial_stack;
901 debug_process_t procinfo;
902 /* For 32-bit architecture, size of auxv_t is 8 bytes. */
903 const unsigned int sizeof_auxv_t = sizeof (auxv_t);
904 const unsigned int sizeof_tempbuf = 20 * sizeof_auxv_t;
906 gdb_byte *const tempbuf = alloca (sizeof_tempbuf);
909 return TARGET_XFER_E_IO;
911 err = devctl (ctl_fd, DCMD_PROC_INFO, &procinfo,
914 return TARGET_XFER_E_IO;
916 initial_stack = procinfo.initial_stack;
918 /* procfs is always 'self-hosted', no byte-order manipulation. */
919 tempread = nto_read_auxv_from_initial_stack (initial_stack, tempbuf,
922 tempread = std::min (tempread, len) - offset;
923 memcpy (readbuf, tempbuf + offset, tempread);
924 *xfered_len = tempread;
925 return tempread ? TARGET_XFER_OK : TARGET_XFER_EOF;
929 return ops->beneath->to_xfer_partial (ops->beneath, object, annex,
930 readbuf, writebuf, offset, len,
935 /* Take a program previously attached to and detaches it.
936 The program resumes execution and will no longer stop
937 on signals, etc. We'd better not have left any breakpoints
938 in the program or it'll die when it hits one. */
940 procfs_detach (struct target_ops *ops, inferior *inf, int from_tty)
944 target_announce_detach ();
947 SignalKill (nto_node (), ptid_get_pid (inferior_ptid), 0, 0, 0, 0);
952 pid = ptid_get_pid (inferior_ptid);
953 inferior_ptid = null_ptid;
954 detach_inferior (pid);
956 inf_child_maybe_unpush_target (ops);
960 procfs_breakpoint (CORE_ADDR addr, int type, int size)
967 errno = devctl (ctl_fd, DCMD_PROC_BREAK, &brk, sizeof (brk), 0);
974 procfs_insert_breakpoint (struct target_ops *ops, struct gdbarch *gdbarch,
975 struct bp_target_info *bp_tgt)
977 bp_tgt->placed_address = bp_tgt->reqstd_address;
978 return procfs_breakpoint (bp_tgt->placed_address, _DEBUG_BREAK_EXEC, 0);
982 procfs_remove_breakpoint (struct target_ops *ops, struct gdbarch *gdbarch,
983 struct bp_target_info *bp_tgt,
984 enum remove_bp_reason reason)
986 return procfs_breakpoint (bp_tgt->placed_address, _DEBUG_BREAK_EXEC, -1);
990 procfs_insert_hw_breakpoint (struct target_ops *self, struct gdbarch *gdbarch,
991 struct bp_target_info *bp_tgt)
993 bp_tgt->placed_address = bp_tgt->reqstd_address;
994 return procfs_breakpoint (bp_tgt->placed_address,
995 _DEBUG_BREAK_EXEC | _DEBUG_BREAK_HW, 0);
999 procfs_remove_hw_breakpoint (struct target_ops *self,
1000 struct gdbarch *gdbarch,
1001 struct bp_target_info *bp_tgt)
1003 return procfs_breakpoint (bp_tgt->placed_address,
1004 _DEBUG_BREAK_EXEC | _DEBUG_BREAK_HW, -1);
1008 procfs_resume (struct target_ops *ops,
1009 ptid_t ptid, int step, enum gdb_signal signo)
1012 procfs_status status;
1013 sigset_t *run_fault = (sigset_t *) (void *) &run.fault;
1015 if (ptid_equal (inferior_ptid, null_ptid))
1018 procfs_set_thread (ptid_equal (ptid, minus_one_ptid) ? inferior_ptid :
1021 run.flags = _DEBUG_RUN_FAULT | _DEBUG_RUN_TRACE;
1023 run.flags |= _DEBUG_RUN_STEP;
1025 sigemptyset (run_fault);
1026 sigaddset (run_fault, FLTBPT);
1027 sigaddset (run_fault, FLTTRACE);
1028 sigaddset (run_fault, FLTILL);
1029 sigaddset (run_fault, FLTPRIV);
1030 sigaddset (run_fault, FLTBOUNDS);
1031 sigaddset (run_fault, FLTIOVF);
1032 sigaddset (run_fault, FLTIZDIV);
1033 sigaddset (run_fault, FLTFPE);
1034 /* Peter V will be changing this at some point. */
1035 sigaddset (run_fault, FLTPAGE);
1037 run.flags |= _DEBUG_RUN_ARM;
1039 signal_to_pass = gdb_signal_to_host (signo);
1043 devctl (ctl_fd, DCMD_PROC_STATUS, &status, sizeof (status), 0);
1044 signal_to_pass = gdb_signal_to_host (signo);
1045 if (status.why & (_DEBUG_WHY_SIGNALLED | _DEBUG_WHY_FAULTED))
1047 if (signal_to_pass != status.info.si_signo)
1049 SignalKill (nto_node (), ptid_get_pid (inferior_ptid), 0,
1050 signal_to_pass, 0, 0);
1051 run.flags |= _DEBUG_RUN_CLRFLT | _DEBUG_RUN_CLRSIG;
1053 else /* Let it kill the program without telling us. */
1054 sigdelset (&run.trace, signal_to_pass);
1058 run.flags |= _DEBUG_RUN_CLRSIG | _DEBUG_RUN_CLRFLT;
1060 errno = devctl (ctl_fd, DCMD_PROC_RUN, &run, sizeof (run), 0);
1063 perror (_("run error!\n"));
1069 procfs_mourn_inferior (struct target_ops *ops)
1071 if (!ptid_equal (inferior_ptid, null_ptid))
1073 SignalKill (nto_node (), ptid_get_pid (inferior_ptid), 0, SIGKILL, 0, 0);
1076 inferior_ptid = null_ptid;
1077 init_thread_list ();
1078 inf_child_mourn_inferior (ops);
1081 /* This function breaks up an argument string into an argument
1082 vector suitable for passing to execvp().
1083 E.g., on "run a b c d" this routine would get as input
1084 the string "a b c d", and as output it would fill in argv with
1085 the four arguments "a", "b", "c", "d". The only additional
1086 functionality is simple quoting. The gdb command:
1088 will fill in argv with the three args "a", "b c d", "e". */
1090 breakup_args (char *scratch, char **argv)
1092 char *pp, *cp = scratch;
1097 /* Scan past leading separators. */
1099 while (*cp == ' ' || *cp == '\t' || *cp == '\n')
1102 /* Break if at end of string. */
1110 quoting = strchr (cp, '"') ? 1 : 0;
1115 /* Scan for next arg separator. */
1118 cp = strchr (pp, '"');
1119 if ((cp == NULL) || (!quoting))
1120 cp = strchr (pp, ' ');
1122 cp = strchr (pp, '\t');
1124 cp = strchr (pp, '\n');
1126 /* No separators => end of string => break. */
1133 /* Replace the separator with a terminator. */
1137 /* Execv requires a null-terminated arg vector. */
1142 procfs_create_inferior (struct target_ops *ops, const char *exec_file,
1143 const std::string &allargs,
1144 char **env, int from_tty)
1146 struct inheritance inherit;
1150 const char *in = "", *out = "", *err = "";
1153 const char *inferior_io_terminal = get_inferior_io_terminal ();
1154 struct inferior *inf;
1156 argv = xmalloc ((allargs.size () / (unsigned) 2 + 2) *
1158 argv[0] = get_exec_file (1);
1162 argv[0] = exec_file;
1167 args = xstrdup (allargs.c_str ());
1168 breakup_args (args, (exec_file != NULL) ? &argv[1] : &argv[0]);
1170 argv = nto_parse_redirection (argv, &in, &out, &err);
1172 fds[0] = STDIN_FILENO;
1173 fds[1] = STDOUT_FILENO;
1174 fds[2] = STDERR_FILENO;
1176 /* If the user specified I/O via gdb's --tty= arg, use it, but only
1177 if the i/o is not also being specified via redirection. */
1178 if (inferior_io_terminal)
1181 in = inferior_io_terminal;
1183 out = inferior_io_terminal;
1185 err = inferior_io_terminal;
1190 fd = open (in, O_RDONLY);
1198 fd = open (out, O_WRONLY);
1206 fd = open (err, O_WRONLY);
1213 /* Clear any pending SIGUSR1's but keep the behavior the same. */
1214 signal (SIGUSR1, signal (SIGUSR1, SIG_IGN));
1217 sigaddset (&set, SIGUSR1);
1218 sigprocmask (SIG_UNBLOCK, &set, NULL);
1220 memset (&inherit, 0, sizeof (inherit));
1222 if (ND_NODE_CMP (nto_procfs_node, ND_LOCAL_NODE) != 0)
1224 inherit.nd = nto_node ();
1225 inherit.flags |= SPAWN_SETND;
1226 inherit.flags &= ~SPAWN_EXEC;
1228 inherit.flags |= SPAWN_SETGROUP | SPAWN_HOLD;
1229 inherit.pgroup = SPAWN_NEWPGROUP;
1230 pid = spawnp (argv[0], 3, fds, &inherit, argv,
1231 ND_NODE_CMP (nto_procfs_node, ND_LOCAL_NODE) == 0 ? env : 0);
1234 sigprocmask (SIG_BLOCK, &set, NULL);
1237 error (_("Error spawning %s: %d (%s)"), argv[0], errno,
1238 safe_strerror (errno));
1240 if (fds[0] != STDIN_FILENO)
1242 if (fds[1] != STDOUT_FILENO)
1244 if (fds[2] != STDERR_FILENO)
1247 inferior_ptid = do_attach (pid_to_ptid (pid));
1248 procfs_update_thread_list (ops);
1250 inf = current_inferior ();
1251 inferior_appeared (inf, pid);
1252 inf->attach_flag = 0;
1254 flags = _DEBUG_FLAG_KLC; /* Kill-on-Last-Close flag. */
1255 errn = devctl (ctl_fd, DCMD_PROC_SET_FLAG, &flags, sizeof (flags), 0);
1258 /* FIXME: expected warning? */
1259 /* warning( "Failed to set Kill-on-Last-Close flag: errno = %d(%s)\n",
1260 errn, strerror(errn) ); */
1262 if (!target_is_pushed (ops))
1264 target_terminal::init ();
1266 if (exec_bfd != NULL
1267 || (symfile_objfile != NULL && symfile_objfile->obfd != NULL))
1268 solib_create_inferior_hook (0);
1272 procfs_interrupt (struct target_ops *self)
1274 devctl (ctl_fd, DCMD_PROC_STOP, NULL, 0, 0);
1278 procfs_kill_inferior (struct target_ops *ops)
1280 target_mourn_inferior (inferior_ptid);
1283 /* Fill buf with regset and return devctl cmd to do the setting. Return
1284 -1 if we fail to get the regset. Store size of regset in regsize. */
1286 get_regset (int regset, char *buf, int bufsize, int *regsize)
1288 int dev_get, dev_set;
1291 case NTO_REG_GENERAL:
1292 dev_get = DCMD_PROC_GETGREG;
1293 dev_set = DCMD_PROC_SETGREG;
1297 dev_get = DCMD_PROC_GETFPREG;
1298 dev_set = DCMD_PROC_SETFPREG;
1302 dev_get = DCMD_PROC_GETALTREG;
1303 dev_set = DCMD_PROC_SETALTREG;
1306 case NTO_REG_SYSTEM:
1310 if (devctl (ctl_fd, dev_get, buf, bufsize, regsize) != EOK)
1317 procfs_store_registers (struct target_ops *ops,
1318 struct regcache *regcache, int regno)
1324 procfs_altreg altreg;
1328 int len, regset, regsize, dev_set, err;
1330 ptid_t ptid = regcache_get_ptid (regcache);
1332 if (ptid_equal (ptid, null_ptid))
1334 procfs_set_thread (ptid);
1338 for (regset = NTO_REG_GENERAL; regset < NTO_REG_END; regset++)
1340 dev_set = get_regset (regset, (char *) ®,
1341 sizeof (reg), ®size);
1345 if (nto_regset_fill (regcache, regset, (char *) ®) == -1)
1348 err = devctl (ctl_fd, dev_set, ®, regsize, 0);
1350 fprintf_unfiltered (gdb_stderr,
1351 "Warning unable to write regset %d: %s\n",
1352 regno, safe_strerror (err));
1357 regset = nto_regset_id (regno);
1361 dev_set = get_regset (regset, (char *) ®, sizeof (reg), ®size);
1365 len = nto_register_area (regcache->arch (),
1366 regno, regset, &off);
1371 regcache_raw_collect (regcache, regno, (char *) ® + off);
1373 err = devctl (ctl_fd, dev_set, ®, regsize, 0);
1375 fprintf_unfiltered (gdb_stderr,
1376 "Warning unable to write regset %d: %s\n", regno,
1377 safe_strerror (err));
1381 /* Set list of signals to be handled in the target. */
1384 procfs_pass_signals (struct target_ops *self,
1385 int numsigs, unsigned char *pass_signals)
1389 sigfillset (&run.trace);
1391 for (signo = 1; signo < NSIG; signo++)
1393 int target_signo = gdb_signal_from_host (signo);
1394 if (target_signo < numsigs && pass_signals[target_signo])
1395 sigdelset (&run.trace, signo);
1400 procfs_pid_to_str (struct target_ops *ops, ptid_t ptid)
1402 static char buf[1024];
1404 struct tidinfo *tip;
1406 pid = ptid_get_pid (ptid);
1407 tid = ptid_get_tid (ptid);
1409 n = snprintf (buf, 1023, "process %d", pid);
1412 tip = procfs_thread_info (pid, tid);
1414 snprintf (&buf[n], 1023, " (state = 0x%02x)", tip->state);
1420 /* to_can_run implementation for "target procfs". Note this really
1421 means "can this target be the default run target", which there can
1422 be only one, and we make it be "target native" like other ports.
1423 "target procfs <node>" wouldn't make sense as default run target, as
1427 procfs_can_run (struct target_ops *self)
1432 /* "target procfs". */
1433 static struct target_ops nto_procfs_ops;
1435 /* "target native". */
1436 static struct target_ops *nto_native_ops;
1438 /* to_open implementation for "target procfs". */
1441 procfs_open (const char *arg, int from_tty)
1443 procfs_open_1 (&nto_procfs_ops, arg, from_tty);
1446 /* to_open implementation for "target native". */
1449 procfs_native_open (const char *arg, int from_tty)
1451 procfs_open_1 (nto_native_ops, arg, from_tty);
1454 /* Create the "native" and "procfs" targets. */
1457 init_procfs_targets (void)
1459 struct target_ops *t = inf_child_target ();
1461 /* Leave to_shortname as "native". */
1462 t->to_longname = "QNX Neutrino local process";
1463 t->to_doc = "QNX Neutrino local process (started by the \"run\" command).";
1464 t->to_open = procfs_native_open;
1465 t->to_attach = procfs_attach;
1466 t->to_post_attach = procfs_post_attach;
1467 t->to_detach = procfs_detach;
1468 t->to_resume = procfs_resume;
1469 t->to_wait = procfs_wait;
1470 t->to_fetch_registers = procfs_fetch_registers;
1471 t->to_store_registers = procfs_store_registers;
1472 t->to_xfer_partial = procfs_xfer_partial;
1473 t->to_files_info = procfs_files_info;
1474 t->to_insert_breakpoint = procfs_insert_breakpoint;
1475 t->to_remove_breakpoint = procfs_remove_breakpoint;
1476 t->to_can_use_hw_breakpoint = procfs_can_use_hw_breakpoint;
1477 t->to_insert_hw_breakpoint = procfs_insert_hw_breakpoint;
1478 t->to_remove_hw_breakpoint = procfs_remove_hw_breakpoint;
1479 t->to_insert_watchpoint = procfs_insert_hw_watchpoint;
1480 t->to_remove_watchpoint = procfs_remove_hw_watchpoint;
1481 t->to_stopped_by_watchpoint = procfs_stopped_by_watchpoint;
1482 t->to_kill = procfs_kill_inferior;
1483 t->to_create_inferior = procfs_create_inferior;
1484 t->to_mourn_inferior = procfs_mourn_inferior;
1485 t->to_pass_signals = procfs_pass_signals;
1486 t->to_thread_alive = procfs_thread_alive;
1487 t->to_update_thread_list = procfs_update_thread_list;
1488 t->to_pid_to_str = procfs_pid_to_str;
1489 t->to_interrupt = procfs_interrupt;
1490 t->to_have_continuable_watchpoint = 1;
1491 t->to_extra_thread_info = nto_extra_thread_info;
1492 t->to_pid_to_exec_file = procfs_pid_to_exec_file;
1496 /* Register "target native". This is the default run target. */
1499 /* Register "target procfs <node>". */
1500 nto_procfs_ops = *t;
1501 nto_procfs_ops.to_shortname = "procfs";
1502 nto_procfs_ops.to_can_run = procfs_can_run;
1503 t->to_longname = "QNX Neutrino local or remote process";
1504 t->to_doc = "QNX Neutrino process. target procfs <node>";
1505 t->to_open = procfs_open;
1507 add_target (&nto_procfs_ops);
1510 #define OSTYPE_NTO 1
1513 _initialize_procfs (void)
1517 init_procfs_targets ();
1519 /* We use SIGUSR1 to gain control after we block waiting for a process.
1520 We use sigwaitevent to wait. */
1522 sigaddset (&set, SIGUSR1);
1523 sigprocmask (SIG_BLOCK, &set, NULL);
1525 /* Initially, make sure all signals are reported. */
1526 sigfillset (&run.trace);
1528 /* Stuff some information. */
1529 nto_cpuinfo_flags = SYSPAGE_ENTRY (cpuinfo)->flags;
1530 nto_cpuinfo_valid = 1;
1532 add_info ("pidlist", procfs_pidlist, _("pidlist"));
1533 add_info ("meminfo", procfs_meminfo, _("memory information"));
1535 nto_is_nto_target = procfs_is_nto_target;
1540 procfs_hw_watchpoint (int addr, int len, enum target_hw_bp_type type)
1547 brk.type = _DEBUG_BREAK_RD;
1550 brk.type = _DEBUG_BREAK_RW;
1552 default: /* Modify. */
1553 /* FIXME: brk.type = _DEBUG_BREAK_RWM gives EINVAL for some reason. */
1554 brk.type = _DEBUG_BREAK_RW;
1556 brk.type |= _DEBUG_BREAK_HW; /* Always ask for HW. */
1560 errno = devctl (ctl_fd, DCMD_PROC_BREAK, &brk, sizeof (brk), 0);
1563 perror (_("Failed to set hardware watchpoint"));
1570 procfs_can_use_hw_breakpoint (struct target_ops *self,
1572 int cnt, int othertype)
1578 procfs_remove_hw_watchpoint (struct target_ops *self,
1579 CORE_ADDR addr, int len,
1580 enum target_hw_bp_type type,
1581 struct expression *cond)
1583 return procfs_hw_watchpoint (addr, -1, type);
1587 procfs_insert_hw_watchpoint (struct target_ops *self,
1588 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 procfs_stopped_by_watchpoint (struct target_ops *ops)
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);