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-2014 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>
34 #include "exceptions.h"
40 #include "gdbthread.h"
47 #define _DEBUG_FLAG_TRACE (_DEBUG_FLAG_TRACE_EXEC|_DEBUG_FLAG_TRACE_RD|\
48 _DEBUG_FLAG_TRACE_WR|_DEBUG_FLAG_TRACE_MODIFY)
50 static struct target_ops procfs_ops
;
54 static void (*ofunc
) ();
56 static procfs_run run
;
58 static void procfs_open (char *, int);
60 static int procfs_can_run (struct target_ops
*self
);
62 static void init_procfs_ops (void);
64 static ptid_t
do_attach (ptid_t ptid
);
66 static int procfs_can_use_hw_breakpoint (struct target_ops
*self
,
69 static int procfs_insert_hw_watchpoint (struct target_ops
*self
,
70 CORE_ADDR addr
, int len
, int type
,
71 struct expression
*cond
);
73 static int procfs_remove_hw_watchpoint (struct target_ops
*self
,
74 CORE_ADDR addr
, int len
, int type
,
75 struct expression
*cond
);
77 static int procfs_stopped_by_watchpoint (struct target_ops
*ops
);
79 /* These two globals are only ever set in procfs_open(), but are
80 referenced elsewhere. 'nto_procfs_node' is a flag used to say
81 whether we are local, or we should get the current node descriptor
82 for the remote QNX node. */
83 static char nto_procfs_path
[PATH_MAX
] = { "/proc" };
84 static unsigned nto_procfs_node
= ND_LOCAL_NODE
;
86 /* Return the current QNX Node, or error out. This is a simple
87 wrapper for the netmgr_strtond() function. The reason this
88 is required is because QNX node descriptors are transient so
89 we have to re-acquire them every time. */
95 if (ND_NODE_CMP (nto_procfs_node
, ND_LOCAL_NODE
) == 0)
98 node
= netmgr_strtond (nto_procfs_path
, 0);
100 error (_("Lost the QNX node. Debug session probably over."));
105 static enum gdb_osabi
106 procfs_is_nto_target (bfd
*abfd
)
108 return GDB_OSABI_QNXNTO
;
111 /* This is called when we call 'target procfs <arg>' from the (gdb) prompt.
112 For QNX6 (nto), the only valid arg will be a QNX node string,
113 eg: "/net/some_node". If arg is not a valid QNX node, we will
116 procfs_open (char *arg
, int from_tty
)
122 procfs_sysinfo
*sysinfo
;
123 struct cleanup
*cleanups
;
125 nto_is_nto_target
= procfs_is_nto_target
;
127 /* Set the default node used for spawning to this one,
128 and only override it if there is a valid arg. */
130 nto_procfs_node
= ND_LOCAL_NODE
;
131 nodestr
= arg
? xstrdup (arg
) : arg
;
137 nto_procfs_node
= netmgr_strtond (nodestr
, &endstr
);
138 if (nto_procfs_node
== -1)
140 if (errno
== ENOTSUP
)
141 printf_filtered ("QNX Net Manager not found.\n");
142 printf_filtered ("Invalid QNX node %s: error %d (%s).\n", nodestr
,
143 errno
, safe_strerror (errno
));
146 nto_procfs_node
= ND_LOCAL_NODE
;
150 if (*(endstr
- 1) == '/')
156 snprintf (nto_procfs_path
, PATH_MAX
- 1, "%s%s", nodestr
? nodestr
: "",
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
);
205 printf_filtered ("Debugging using %s\n", nto_procfs_path
);
209 procfs_set_thread (ptid_t ptid
)
213 tid
= ptid_get_tid (ptid
);
214 devctl (ctl_fd
, DCMD_PROC_CURTHREAD
, &tid
, sizeof (tid
), 0);
217 /* Return nonzero if the thread TH is still alive. */
219 procfs_thread_alive (struct target_ops
*ops
, ptid_t ptid
)
223 procfs_status status
;
226 tid
= ptid_get_tid (ptid
);
227 pid
= ptid_get_pid (ptid
);
229 if (kill (pid
, 0) == -1)
233 if ((err
= devctl (ctl_fd
, DCMD_PROC_TIDSTATUS
,
234 &status
, sizeof (status
), 0)) != EOK
)
237 /* Thread is alive or dead but not yet joined,
238 or dead and there is an alive (or dead unjoined) thread with
241 If the tid is not the same as requested, requested tid is dead. */
242 return (status
.tid
== tid
) && (status
.state
!= STATE_DEAD
);
246 update_thread_private_data_name (struct thread_info
*new_thread
,
250 struct private_thread_info
*pti
;
252 gdb_assert (newname
!= NULL
);
253 gdb_assert (new_thread
!= NULL
);
254 newnamelen
= strlen (newname
);
255 if (!new_thread
->private)
257 new_thread
->private = xmalloc (offsetof (struct private_thread_info
,
260 memcpy (new_thread
->private->name
, newname
, newnamelen
+ 1);
262 else if (strcmp (newname
, new_thread
->private->name
) != 0)
264 /* Reallocate if neccessary. */
265 int oldnamelen
= strlen (new_thread
->private->name
);
267 if (oldnamelen
< newnamelen
)
268 new_thread
->private = xrealloc (new_thread
->private,
269 offsetof (struct private_thread_info
,
272 memcpy (new_thread
->private->name
, newname
, newnamelen
+ 1);
277 update_thread_private_data (struct thread_info
*new_thread
,
278 pthread_t tid
, int state
, int flags
)
280 struct private_thread_info
*pti
;
282 struct _thread_name
*tn
;
283 procfs_threadctl tctl
;
285 #if _NTO_VERSION > 630
286 gdb_assert (new_thread
!= NULL
);
288 if (devctl (ctl_fd
, DCMD_PROC_INFO
, &pidinfo
,
289 sizeof(pidinfo
), 0) != EOK
)
292 memset (&tctl
, 0, sizeof (tctl
));
293 tctl
.cmd
= _NTO_TCTL_NAME
;
294 tn
= (struct _thread_name
*) (&tctl
.data
);
296 /* Fetch name for the given thread. */
298 tn
->name_buf_len
= sizeof (tctl
.data
) - sizeof (*tn
);
299 tn
->new_name_len
= -1; /* Getting, not setting. */
300 if (devctl (ctl_fd
, DCMD_PROC_THREADCTL
, &tctl
, sizeof (tctl
), NULL
) != EOK
)
301 tn
->name_buf
[0] = '\0';
303 tn
->name_buf
[_NTO_THREAD_NAME_MAX
] = '\0';
305 update_thread_private_data_name (new_thread
, tn
->name_buf
);
307 pti
= (struct private_thread_info
*) new_thread
->private;
311 #endif /* _NTO_VERSION */
315 procfs_find_new_threads (struct target_ops
*ops
)
317 procfs_status status
;
321 struct thread_info
*new_thread
;
326 pid
= ptid_get_pid (inferior_ptid
);
330 for (tid
= 1;; ++tid
)
332 if (status
.tid
== tid
333 && (devctl (ctl_fd
, DCMD_PROC_TIDSTATUS
, &status
, sizeof (status
), 0)
336 if (status
.tid
!= tid
)
337 /* The reason why this would not be equal is that devctl might have
338 returned different tid, meaning the requested tid no longer exists
339 (e.g. thread exited). */
341 ptid
= ptid_build (pid
, 0, tid
);
342 new_thread
= find_thread_ptid (ptid
);
344 new_thread
= add_thread (ptid
);
345 update_thread_private_data (new_thread
, tid
, status
.state
, 0);
352 do_closedir_cleanup (void *dir
)
358 procfs_pidlist (char *args
, int from_tty
)
361 struct dirent
*dirp
= NULL
;
363 procfs_info
*pidinfo
= NULL
;
364 procfs_debuginfo
*info
= NULL
;
365 procfs_status
*status
= NULL
;
366 pid_t num_threads
= 0;
369 struct cleanup
*cleanups
;
371 dp
= opendir (nto_procfs_path
);
374 fprintf_unfiltered (gdb_stderr
, "failed to opendir \"%s\" - %d (%s)",
375 nto_procfs_path
, errno
, safe_strerror (errno
));
379 cleanups
= make_cleanup (do_closedir_cleanup
, dp
);
381 /* Start scan at first pid. */
387 struct cleanup
*inner_cleanup
;
389 /* Get the right pid and procfs path for the pid. */
395 do_cleanups (cleanups
);
398 snprintf (buf
, 511, "%s/%s/as", nto_procfs_path
, dirp
->d_name
);
399 pid
= atoi (dirp
->d_name
);
403 /* Open the procfs path. */
404 fd
= open (buf
, O_RDONLY
);
407 fprintf_unfiltered (gdb_stderr
, "failed to open %s - %d (%s)\n",
408 buf
, errno
, safe_strerror (errno
));
409 do_cleanups (cleanups
);
412 inner_cleanup
= make_cleanup_close (fd
);
414 pidinfo
= (procfs_info
*) buf
;
415 if (devctl (fd
, DCMD_PROC_INFO
, pidinfo
, sizeof (buf
), 0) != EOK
)
417 fprintf_unfiltered (gdb_stderr
,
418 "devctl DCMD_PROC_INFO failed - %d (%s)\n",
419 errno
, safe_strerror (errno
));
422 num_threads
= pidinfo
->num_threads
;
424 info
= (procfs_debuginfo
*) buf
;
425 if (devctl (fd
, DCMD_PROC_MAPDEBUG_BASE
, info
, sizeof (buf
), 0) != EOK
)
426 strcpy (name
, "unavailable");
428 strcpy (name
, info
->path
);
430 /* Collect state info on all the threads. */
431 status
= (procfs_status
*) buf
;
432 for (status
->tid
= 1; status
->tid
<= num_threads
; status
->tid
++)
434 if (devctl (fd
, DCMD_PROC_TIDSTATUS
, status
, sizeof (buf
), 0) != EOK
437 if (status
->tid
!= 0)
438 printf_filtered ("%s - %d/%d\n", name
, pid
, status
->tid
);
441 do_cleanups (inner_cleanup
);
443 while (dirp
!= NULL
);
445 do_cleanups (cleanups
);
450 procfs_meminfo (char *args
, int from_tty
)
452 procfs_mapinfo
*mapinfos
= NULL
;
453 static int num_mapinfos
= 0;
454 procfs_mapinfo
*mapinfo_p
, *mapinfo_p2
;
455 int flags
= ~0, err
, num
, i
, j
;
459 procfs_debuginfo info
;
460 char buff
[_POSIX_PATH_MAX
];
468 unsigned debug_vaddr
;
469 unsigned long long offset
;
474 unsigned long long ino
;
481 /* Get the number of map entrys. */
482 err
= devctl (ctl_fd
, DCMD_PROC_MAPINFO
, NULL
, 0, &num
);
485 printf ("failed devctl num mapinfos - %d (%s)\n", err
,
486 safe_strerror (err
));
490 mapinfos
= xmalloc (num
* sizeof (procfs_mapinfo
));
493 mapinfo_p
= mapinfos
;
495 /* Fill the map entrys. */
496 err
= devctl (ctl_fd
, DCMD_PROC_MAPINFO
, mapinfo_p
, num
497 * sizeof (procfs_mapinfo
), &num
);
500 printf ("failed devctl mapinfos - %d (%s)\n", err
, safe_strerror (err
));
505 num
= min (num
, num_mapinfos
);
507 /* Run through the list of mapinfos, and store the data and text info
508 so we can print it at the bottom of the loop. */
509 for (mapinfo_p
= mapinfos
, i
= 0; i
< num
; i
++, mapinfo_p
++)
511 if (!(mapinfo_p
->flags
& flags
))
514 if (mapinfo_p
->ino
== 0) /* Already visited. */
517 map
.info
.vaddr
= mapinfo_p
->vaddr
;
519 err
= devctl (ctl_fd
, DCMD_PROC_MAPDEBUG
, &map
, sizeof (map
), 0);
523 memset (&printme
, 0, sizeof printme
);
524 printme
.dev
= mapinfo_p
->dev
;
525 printme
.ino
= mapinfo_p
->ino
;
526 printme
.text
.addr
= mapinfo_p
->vaddr
;
527 printme
.text
.size
= mapinfo_p
->size
;
528 printme
.text
.flags
= mapinfo_p
->flags
;
529 printme
.text
.offset
= mapinfo_p
->offset
;
530 printme
.text
.debug_vaddr
= map
.info
.vaddr
;
531 strcpy (printme
.name
, map
.info
.path
);
533 /* Check for matching data. */
534 for (mapinfo_p2
= mapinfos
, j
= 0; j
< num
; j
++, mapinfo_p2
++)
536 if (mapinfo_p2
->vaddr
!= mapinfo_p
->vaddr
537 && mapinfo_p2
->ino
== mapinfo_p
->ino
538 && mapinfo_p2
->dev
== mapinfo_p
->dev
)
540 map
.info
.vaddr
= mapinfo_p2
->vaddr
;
542 devctl (ctl_fd
, DCMD_PROC_MAPDEBUG
, &map
, sizeof (map
), 0);
546 if (strcmp (map
.info
.path
, printme
.name
))
549 /* Lower debug_vaddr is always text, if nessessary, swap. */
550 if ((int) map
.info
.vaddr
< (int) printme
.text
.debug_vaddr
)
552 memcpy (&(printme
.data
), &(printme
.text
),
553 sizeof (printme
.data
));
554 printme
.text
.addr
= mapinfo_p2
->vaddr
;
555 printme
.text
.size
= mapinfo_p2
->size
;
556 printme
.text
.flags
= mapinfo_p2
->flags
;
557 printme
.text
.offset
= mapinfo_p2
->offset
;
558 printme
.text
.debug_vaddr
= map
.info
.vaddr
;
562 printme
.data
.addr
= mapinfo_p2
->vaddr
;
563 printme
.data
.size
= mapinfo_p2
->size
;
564 printme
.data
.flags
= mapinfo_p2
->flags
;
565 printme
.data
.offset
= mapinfo_p2
->offset
;
566 printme
.data
.debug_vaddr
= map
.info
.vaddr
;
573 printf_filtered ("%s\n", printme
.name
);
574 printf_filtered ("\ttext=%08x bytes @ 0x%08x\n", printme
.text
.size
,
576 printf_filtered ("\t\tflags=%08x\n", printme
.text
.flags
);
577 printf_filtered ("\t\tdebug=%08x\n", printme
.text
.debug_vaddr
);
578 printf_filtered ("\t\toffset=%s\n", phex (printme
.text
.offset
, 8));
579 if (printme
.data
.size
)
581 printf_filtered ("\tdata=%08x bytes @ 0x%08x\n", printme
.data
.size
,
583 printf_filtered ("\t\tflags=%08x\n", printme
.data
.flags
);
584 printf_filtered ("\t\tdebug=%08x\n", printme
.data
.debug_vaddr
);
585 printf_filtered ("\t\toffset=%s\n", phex (printme
.data
.offset
, 8));
587 printf_filtered ("\tdev=0x%x\n", printme
.dev
);
588 printf_filtered ("\tino=0x%x\n", (unsigned int) printme
.ino
);
594 /* Print status information about what we're accessing. */
596 procfs_files_info (struct target_ops
*ignore
)
598 struct inferior
*inf
= current_inferior ();
600 printf_unfiltered ("\tUsing the running image of %s %s via %s.\n",
601 inf
->attach_flag
? "attached" : "child",
602 target_pid_to_str (inferior_ptid
), nto_procfs_path
);
605 /* Mark our target-struct as eligible for stray "run" and "attach"
608 procfs_can_run (struct target_ops
*self
)
613 /* Attach to process PID, then initialize for debugging it. */
615 procfs_attach (struct target_ops
*ops
, char *args
, int from_tty
)
619 struct inferior
*inf
;
621 pid
= parse_pid_to_attach (args
);
623 if (pid
== getpid ())
624 error (_("Attaching GDB to itself is not a good idea..."));
628 exec_file
= (char *) get_exec_file (0);
631 printf_unfiltered ("Attaching to program `%s', %s\n", exec_file
,
632 target_pid_to_str (pid_to_ptid (pid
)));
634 printf_unfiltered ("Attaching to %s\n",
635 target_pid_to_str (pid_to_ptid (pid
)));
637 gdb_flush (gdb_stdout
);
639 inferior_ptid
= do_attach (pid_to_ptid (pid
));
640 inf
= current_inferior ();
641 inferior_appeared (inf
, pid
);
642 inf
->attach_flag
= 1;
646 procfs_find_new_threads (ops
);
650 procfs_post_attach (struct target_ops
*self
, pid_t pid
)
653 solib_create_inferior_hook (0);
657 do_attach (ptid_t ptid
)
659 procfs_status status
;
660 struct sigevent event
;
663 snprintf (path
, PATH_MAX
- 1, "%s/%d/as", nto_procfs_path
,
664 ptid_get_pid (ptid
));
665 ctl_fd
= open (path
, O_RDWR
);
667 error (_("Couldn't open proc file %s, error %d (%s)"), path
, errno
,
668 safe_strerror (errno
));
669 if (devctl (ctl_fd
, DCMD_PROC_STOP
, &status
, sizeof (status
), 0) != EOK
)
670 error (_("Couldn't stop process"));
672 /* Define a sigevent for process stopped notification. */
673 event
.sigev_notify
= SIGEV_SIGNAL_THREAD
;
674 event
.sigev_signo
= SIGUSR1
;
675 event
.sigev_code
= 0;
676 event
.sigev_value
.sival_ptr
= NULL
;
677 event
.sigev_priority
= -1;
678 devctl (ctl_fd
, DCMD_PROC_EVENT
, &event
, sizeof (event
), 0);
680 if (devctl (ctl_fd
, DCMD_PROC_STATUS
, &status
, sizeof (status
), 0) == EOK
681 && status
.flags
& _DEBUG_FLAG_STOPPED
)
682 SignalKill (nto_node (), ptid_get_pid (ptid
), 0, SIGCONT
, 0, 0);
683 nto_init_solib_absolute_prefix ();
684 return ptid_build (ptid_get_pid (ptid
), 0, status
.tid
);
687 /* Ask the user what to do when an interrupt is received. */
689 interrupt_query (void)
691 target_terminal_ours ();
693 if (query (_("Interrupted while waiting for the program.\n\
694 Give up (and stop debugging it)? ")))
696 target_mourn_inferior ();
700 target_terminal_inferior ();
703 /* The user typed ^C twice. */
705 nto_interrupt_twice (int signo
)
707 signal (signo
, ofunc
);
709 signal (signo
, nto_interrupt_twice
);
713 nto_interrupt (int signo
)
715 /* If this doesn't work, try more severe steps. */
716 signal (signo
, nto_interrupt_twice
);
718 target_stop (inferior_ptid
);
722 procfs_wait (struct target_ops
*ops
,
723 ptid_t ptid
, struct target_waitstatus
*ourstatus
, int options
)
727 procfs_status status
;
728 static int exit_signo
= 0; /* To track signals that cause termination. */
730 ourstatus
->kind
= TARGET_WAITKIND_SPURIOUS
;
732 if (ptid_equal (inferior_ptid
, null_ptid
))
734 ourstatus
->kind
= TARGET_WAITKIND_STOPPED
;
735 ourstatus
->value
.sig
= GDB_SIGNAL_0
;
741 sigaddset (&set
, SIGUSR1
);
743 devctl (ctl_fd
, DCMD_PROC_STATUS
, &status
, sizeof (status
), 0);
744 while (!(status
.flags
& _DEBUG_FLAG_ISTOP
))
746 ofunc
= (void (*)()) signal (SIGINT
, nto_interrupt
);
747 sigwaitinfo (&set
, &info
);
748 signal (SIGINT
, ofunc
);
749 devctl (ctl_fd
, DCMD_PROC_STATUS
, &status
, sizeof (status
), 0);
752 if (status
.flags
& _DEBUG_FLAG_SSTEP
)
754 ourstatus
->kind
= TARGET_WAITKIND_STOPPED
;
755 ourstatus
->value
.sig
= GDB_SIGNAL_TRAP
;
757 /* Was it a breakpoint? */
758 else if (status
.flags
& _DEBUG_FLAG_TRACE
)
760 ourstatus
->kind
= TARGET_WAITKIND_STOPPED
;
761 ourstatus
->value
.sig
= GDB_SIGNAL_TRAP
;
763 else if (status
.flags
& _DEBUG_FLAG_ISTOP
)
767 case _DEBUG_WHY_SIGNALLED
:
768 ourstatus
->kind
= TARGET_WAITKIND_STOPPED
;
769 ourstatus
->value
.sig
=
770 gdb_signal_from_host (status
.info
.si_signo
);
773 case _DEBUG_WHY_FAULTED
:
774 ourstatus
->kind
= TARGET_WAITKIND_STOPPED
;
775 if (status
.info
.si_signo
== SIGTRAP
)
777 ourstatus
->value
.sig
= 0;
782 ourstatus
->value
.sig
=
783 gdb_signal_from_host (status
.info
.si_signo
);
784 exit_signo
= ourstatus
->value
.sig
;
788 case _DEBUG_WHY_TERMINATED
:
792 waitpid (ptid_get_pid (inferior_ptid
), &waitval
, WNOHANG
);
795 /* Abnormal death. */
796 ourstatus
->kind
= TARGET_WAITKIND_SIGNALLED
;
797 ourstatus
->value
.sig
= exit_signo
;
802 ourstatus
->kind
= TARGET_WAITKIND_EXITED
;
803 ourstatus
->value
.integer
= WEXITSTATUS (waitval
);
809 case _DEBUG_WHY_REQUESTED
:
810 /* We are assuming a requested stop is due to a SIGINT. */
811 ourstatus
->kind
= TARGET_WAITKIND_STOPPED
;
812 ourstatus
->value
.sig
= GDB_SIGNAL_INT
;
818 return ptid_build (status
.pid
, 0, status
.tid
);
821 /* Read the current values of the inferior's registers, both the
822 general register set and floating point registers (if supported)
823 and update gdb's idea of their current values. */
825 procfs_fetch_registers (struct target_ops
*ops
,
826 struct regcache
*regcache
, int regno
)
832 procfs_altreg altreg
;
837 procfs_set_thread (inferior_ptid
);
838 if (devctl (ctl_fd
, DCMD_PROC_GETGREG
, ®
, sizeof (reg
), ®size
) == EOK
)
839 nto_supply_gregset (regcache
, (char *) ®
.greg
);
840 if (devctl (ctl_fd
, DCMD_PROC_GETFPREG
, ®
, sizeof (reg
), ®size
)
842 nto_supply_fpregset (regcache
, (char *) ®
.fpreg
);
843 if (devctl (ctl_fd
, DCMD_PROC_GETALTREG
, ®
, sizeof (reg
), ®size
)
845 nto_supply_altregset (regcache
, (char *) ®
.altreg
);
848 /* Helper for procfs_xfer_partial that handles memory transfers.
849 Arguments are like target_xfer_partial. */
851 static enum target_xfer_status
852 procfs_xfer_memory (gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
853 ULONGEST memaddr
, ULONGEST len
, ULONGEST
*xfered_len
)
857 if (lseek (ctl_fd
, (off_t
) memaddr
, SEEK_SET
) != (off_t
) memaddr
)
858 return TARGET_XFER_E_IO
;
860 if (writebuf
!= NULL
)
861 nbytes
= write (ctl_fd
, writebuf
, len
);
863 nbytes
= read (ctl_fd
, readbuf
, len
);
865 return TARGET_XFER_E_IO
;
866 *xfered_len
= nbytes
;
867 return TARGET_XFER_OK
;
870 /* Target to_xfer_partial implementation. */
872 static enum target_xfer_status
873 procfs_xfer_partial (struct target_ops
*ops
, enum target_object object
,
874 const char *annex
, gdb_byte
*readbuf
,
875 const gdb_byte
*writebuf
, ULONGEST offset
, ULONGEST len
,
876 ULONGEST
*xfered_len
)
880 case TARGET_OBJECT_MEMORY
:
881 return procfs_xfer_memory (readbuf
, writebuf
, offset
, len
, xfered_len
);
883 return ops
->beneath
->to_xfer_partial (ops
->beneath
, object
, annex
,
884 readbuf
, writebuf
, offset
, len
);
888 /* Take a program previously attached to and detaches it.
889 The program resumes execution and will no longer stop
890 on signals, etc. We'd better not have left any breakpoints
891 in the program or it'll die when it hits one. */
893 procfs_detach (struct target_ops
*ops
, const char *args
, int from_tty
)
900 char *exec_file
= get_exec_file (0);
903 printf_unfiltered ("Detaching from program: %s %s\n",
904 exec_file
, target_pid_to_str (inferior_ptid
));
905 gdb_flush (gdb_stdout
);
908 siggnal
= atoi (args
);
911 SignalKill (nto_node (), ptid_get_pid (inferior_ptid
), 0, siggnal
, 0, 0);
916 pid
= ptid_get_pid (inferior_ptid
);
917 inferior_ptid
= null_ptid
;
918 detach_inferior (pid
);
920 unpush_target (&procfs_ops
); /* Pop out of handling an inferior. */
924 procfs_breakpoint (CORE_ADDR addr
, int type
, int size
)
931 errno
= devctl (ctl_fd
, DCMD_PROC_BREAK
, &brk
, sizeof (brk
), 0);
938 procfs_insert_breakpoint (struct target_ops
*ops
, struct gdbarch
*gdbarch
,
939 struct bp_target_info
*bp_tgt
)
941 return procfs_breakpoint (bp_tgt
->placed_address
, _DEBUG_BREAK_EXEC
, 0);
945 procfs_remove_breakpoint (struct target_ops
*ops
, struct gdbarch
*gdbarch
,
946 struct bp_target_info
*bp_tgt
)
948 return procfs_breakpoint (bp_tgt
->placed_address
, _DEBUG_BREAK_EXEC
, -1);
952 procfs_insert_hw_breakpoint (struct target_ops
*self
, struct gdbarch
*gdbarch
,
953 struct bp_target_info
*bp_tgt
)
955 return procfs_breakpoint (bp_tgt
->placed_address
,
956 _DEBUG_BREAK_EXEC
| _DEBUG_BREAK_HW
, 0);
960 procfs_remove_hw_breakpoint (struct target_ops
*self
,
961 struct gdbarch
*gdbarch
,
962 struct bp_target_info
*bp_tgt
)
964 return procfs_breakpoint (bp_tgt
->placed_address
,
965 _DEBUG_BREAK_EXEC
| _DEBUG_BREAK_HW
, -1);
969 procfs_resume (struct target_ops
*ops
,
970 ptid_t ptid
, int step
, enum gdb_signal signo
)
973 procfs_status status
;
974 sigset_t
*run_fault
= (sigset_t
*) (void *) &run
.fault
;
976 if (ptid_equal (inferior_ptid
, null_ptid
))
979 procfs_set_thread (ptid_equal (ptid
, minus_one_ptid
) ? inferior_ptid
:
982 run
.flags
= _DEBUG_RUN_FAULT
| _DEBUG_RUN_TRACE
;
984 run
.flags
|= _DEBUG_RUN_STEP
;
986 sigemptyset (run_fault
);
987 sigaddset (run_fault
, FLTBPT
);
988 sigaddset (run_fault
, FLTTRACE
);
989 sigaddset (run_fault
, FLTILL
);
990 sigaddset (run_fault
, FLTPRIV
);
991 sigaddset (run_fault
, FLTBOUNDS
);
992 sigaddset (run_fault
, FLTIOVF
);
993 sigaddset (run_fault
, FLTIZDIV
);
994 sigaddset (run_fault
, FLTFPE
);
995 /* Peter V will be changing this at some point. */
996 sigaddset (run_fault
, FLTPAGE
);
998 run
.flags
|= _DEBUG_RUN_ARM
;
1000 signal_to_pass
= gdb_signal_to_host (signo
);
1004 devctl (ctl_fd
, DCMD_PROC_STATUS
, &status
, sizeof (status
), 0);
1005 signal_to_pass
= gdb_signal_to_host (signo
);
1006 if (status
.why
& (_DEBUG_WHY_SIGNALLED
| _DEBUG_WHY_FAULTED
))
1008 if (signal_to_pass
!= status
.info
.si_signo
)
1010 SignalKill (nto_node (), ptid_get_pid (inferior_ptid
), 0,
1011 signal_to_pass
, 0, 0);
1012 run
.flags
|= _DEBUG_RUN_CLRFLT
| _DEBUG_RUN_CLRSIG
;
1014 else /* Let it kill the program without telling us. */
1015 sigdelset (&run
.trace
, signal_to_pass
);
1019 run
.flags
|= _DEBUG_RUN_CLRSIG
| _DEBUG_RUN_CLRFLT
;
1021 errno
= devctl (ctl_fd
, DCMD_PROC_RUN
, &run
, sizeof (run
), 0);
1024 perror (_("run error!\n"));
1030 procfs_mourn_inferior (struct target_ops
*ops
)
1032 if (!ptid_equal (inferior_ptid
, null_ptid
))
1034 SignalKill (nto_node (), ptid_get_pid (inferior_ptid
), 0, SIGKILL
, 0, 0);
1037 inferior_ptid
= null_ptid
;
1038 init_thread_list ();
1039 unpush_target (&procfs_ops
);
1040 generic_mourn_inferior ();
1043 /* This function breaks up an argument string into an argument
1044 vector suitable for passing to execvp().
1045 E.g., on "run a b c d" this routine would get as input
1046 the string "a b c d", and as output it would fill in argv with
1047 the four arguments "a", "b", "c", "d". The only additional
1048 functionality is simple quoting. The gdb command:
1050 will fill in argv with the three args "a", "b c d", "e". */
1052 breakup_args (char *scratch
, char **argv
)
1054 char *pp
, *cp
= scratch
;
1059 /* Scan past leading separators. */
1061 while (*cp
== ' ' || *cp
== '\t' || *cp
== '\n')
1064 /* Break if at end of string. */
1072 quoting
= strchr (cp
, '"') ? 1 : 0;
1077 /* Scan for next arg separator. */
1080 cp
= strchr (pp
, '"');
1081 if ((cp
== NULL
) || (!quoting
))
1082 cp
= strchr (pp
, ' ');
1084 cp
= strchr (pp
, '\t');
1086 cp
= strchr (pp
, '\n');
1088 /* No separators => end of string => break. */
1095 /* Replace the separator with a terminator. */
1099 /* Execv requires a null-terminated arg vector. */
1104 procfs_create_inferior (struct target_ops
*ops
, char *exec_file
,
1105 char *allargs
, char **env
, int from_tty
)
1107 struct inheritance inherit
;
1111 const char *in
= "", *out
= "", *err
= "";
1114 const char *inferior_io_terminal
= get_inferior_io_terminal ();
1115 struct inferior
*inf
;
1117 argv
= xmalloc (((strlen (allargs
) + 1) / (unsigned) 2 + 2) *
1119 argv
[0] = get_exec_file (1);
1123 argv
[0] = exec_file
;
1128 args
= xstrdup (allargs
);
1129 breakup_args (args
, exec_file
? &argv
[1] : &argv
[0]);
1131 argv
= nto_parse_redirection (argv
, &in
, &out
, &err
);
1133 fds
[0] = STDIN_FILENO
;
1134 fds
[1] = STDOUT_FILENO
;
1135 fds
[2] = STDERR_FILENO
;
1137 /* If the user specified I/O via gdb's --tty= arg, use it, but only
1138 if the i/o is not also being specified via redirection. */
1139 if (inferior_io_terminal
)
1142 in
= inferior_io_terminal
;
1144 out
= inferior_io_terminal
;
1146 err
= inferior_io_terminal
;
1151 fd
= open (in
, O_RDONLY
);
1159 fd
= open (out
, O_WRONLY
);
1167 fd
= open (err
, O_WRONLY
);
1174 /* Clear any pending SIGUSR1's but keep the behavior the same. */
1175 signal (SIGUSR1
, signal (SIGUSR1
, SIG_IGN
));
1178 sigaddset (&set
, SIGUSR1
);
1179 sigprocmask (SIG_UNBLOCK
, &set
, NULL
);
1181 memset (&inherit
, 0, sizeof (inherit
));
1183 if (ND_NODE_CMP (nto_procfs_node
, ND_LOCAL_NODE
) != 0)
1185 inherit
.nd
= nto_node ();
1186 inherit
.flags
|= SPAWN_SETND
;
1187 inherit
.flags
&= ~SPAWN_EXEC
;
1189 inherit
.flags
|= SPAWN_SETGROUP
| SPAWN_HOLD
;
1190 inherit
.pgroup
= SPAWN_NEWPGROUP
;
1191 pid
= spawnp (argv
[0], 3, fds
, &inherit
, argv
,
1192 ND_NODE_CMP (nto_procfs_node
, ND_LOCAL_NODE
) == 0 ? env
: 0);
1195 sigprocmask (SIG_BLOCK
, &set
, NULL
);
1198 error (_("Error spawning %s: %d (%s)"), argv
[0], errno
,
1199 safe_strerror (errno
));
1201 if (fds
[0] != STDIN_FILENO
)
1203 if (fds
[1] != STDOUT_FILENO
)
1205 if (fds
[2] != STDERR_FILENO
)
1208 inferior_ptid
= do_attach (pid_to_ptid (pid
));
1209 procfs_find_new_threads (ops
);
1211 inf
= current_inferior ();
1212 inferior_appeared (inf
, pid
);
1213 inf
->attach_flag
= 0;
1215 flags
= _DEBUG_FLAG_KLC
; /* Kill-on-Last-Close flag. */
1216 errn
= devctl (ctl_fd
, DCMD_PROC_SET_FLAG
, &flags
, sizeof (flags
), 0);
1219 /* FIXME: expected warning? */
1220 /* warning( "Failed to set Kill-on-Last-Close flag: errno = %d(%s)\n",
1221 errn, strerror(errn) ); */
1224 target_terminal_init ();
1226 if (exec_bfd
!= NULL
1227 || (symfile_objfile
!= NULL
&& symfile_objfile
->obfd
!= NULL
))
1228 solib_create_inferior_hook (0);
1232 procfs_stop (struct target_ops
*self
, ptid_t ptid
)
1234 devctl (ctl_fd
, DCMD_PROC_STOP
, NULL
, 0, 0);
1238 procfs_kill_inferior (struct target_ops
*ops
)
1240 target_mourn_inferior ();
1243 /* Store register REGNO, or all registers if REGNO == -1, from the contents
1246 procfs_prepare_to_store (struct target_ops
*self
, struct regcache
*regcache
)
1250 /* Fill buf with regset and return devctl cmd to do the setting. Return
1251 -1 if we fail to get the regset. Store size of regset in regsize. */
1253 get_regset (int regset
, char *buf
, int bufsize
, int *regsize
)
1255 int dev_get
, dev_set
;
1258 case NTO_REG_GENERAL
:
1259 dev_get
= DCMD_PROC_GETGREG
;
1260 dev_set
= DCMD_PROC_SETGREG
;
1264 dev_get
= DCMD_PROC_GETFPREG
;
1265 dev_set
= DCMD_PROC_SETFPREG
;
1269 dev_get
= DCMD_PROC_GETALTREG
;
1270 dev_set
= DCMD_PROC_SETALTREG
;
1273 case NTO_REG_SYSTEM
:
1277 if (devctl (ctl_fd
, dev_get
, buf
, bufsize
, regsize
) != EOK
)
1284 procfs_store_registers (struct target_ops
*ops
,
1285 struct regcache
*regcache
, int regno
)
1291 procfs_altreg altreg
;
1295 int len
, regset
, regsize
, dev_set
, err
;
1298 if (ptid_equal (inferior_ptid
, null_ptid
))
1300 procfs_set_thread (inferior_ptid
);
1304 for (regset
= NTO_REG_GENERAL
; regset
< NTO_REG_END
; regset
++)
1306 dev_set
= get_regset (regset
, (char *) ®
,
1307 sizeof (reg
), ®size
);
1311 if (nto_regset_fill (regcache
, regset
, (char *) ®
) == -1)
1314 err
= devctl (ctl_fd
, dev_set
, ®
, regsize
, 0);
1316 fprintf_unfiltered (gdb_stderr
,
1317 "Warning unable to write regset %d: %s\n",
1318 regno
, safe_strerror (err
));
1323 regset
= nto_regset_id (regno
);
1327 dev_set
= get_regset (regset
, (char *) ®
, sizeof (reg
), ®size
);
1331 len
= nto_register_area (get_regcache_arch (regcache
),
1332 regno
, regset
, &off
);
1337 regcache_raw_collect (regcache
, regno
, (char *) ®
+ off
);
1339 err
= devctl (ctl_fd
, dev_set
, ®
, regsize
, 0);
1341 fprintf_unfiltered (gdb_stderr
,
1342 "Warning unable to write regset %d: %s\n", regno
,
1343 safe_strerror (err
));
1347 /* Set list of signals to be handled in the target. */
1350 procfs_pass_signals (struct target_ops
*self
,
1351 int numsigs
, unsigned char *pass_signals
)
1355 sigfillset (&run
.trace
);
1357 for (signo
= 1; signo
< NSIG
; signo
++)
1359 int target_signo
= gdb_signal_from_host (signo
);
1360 if (target_signo
< numsigs
&& pass_signals
[target_signo
])
1361 sigdelset (&run
.trace
, signo
);
1365 static struct tidinfo
*
1366 procfs_thread_info (pid_t pid
, short tid
)
1373 procfs_pid_to_str (struct target_ops
*ops
, ptid_t ptid
)
1375 static char buf
[1024];
1377 struct tidinfo
*tip
;
1379 pid
= ptid_get_pid (ptid
);
1380 tid
= ptid_get_tid (ptid
);
1382 n
= snprintf (buf
, 1023, "process %d", pid
);
1385 tip
= procfs_thread_info (pid
, tid
);
1387 snprintf (&buf
[n
], 1023, " (state = 0x%02x)", tip
->state
);
1394 init_procfs_ops (void)
1396 procfs_ops
.to_shortname
= "procfs";
1397 procfs_ops
.to_longname
= "QNX Neutrino procfs child process";
1399 "QNX Neutrino procfs child process (started by the \"run\" command).\n\
1400 target procfs <node>";
1401 procfs_ops
.to_open
= procfs_open
;
1402 procfs_ops
.to_attach
= procfs_attach
;
1403 procfs_ops
.to_post_attach
= procfs_post_attach
;
1404 procfs_ops
.to_detach
= procfs_detach
;
1405 procfs_ops
.to_resume
= procfs_resume
;
1406 procfs_ops
.to_wait
= procfs_wait
;
1407 procfs_ops
.to_fetch_registers
= procfs_fetch_registers
;
1408 procfs_ops
.to_store_registers
= procfs_store_registers
;
1409 procfs_ops
.to_prepare_to_store
= procfs_prepare_to_store
;
1410 procfs_ops
.to_xfer_partial
= procfs_xfer_partial
;
1411 procfs_ops
.to_files_info
= procfs_files_info
;
1412 procfs_ops
.to_insert_breakpoint
= procfs_insert_breakpoint
;
1413 procfs_ops
.to_remove_breakpoint
= procfs_remove_breakpoint
;
1414 procfs_ops
.to_can_use_hw_breakpoint
= procfs_can_use_hw_breakpoint
;
1415 procfs_ops
.to_insert_hw_breakpoint
= procfs_insert_hw_breakpoint
;
1416 procfs_ops
.to_remove_hw_breakpoint
= procfs_remove_hw_breakpoint
;
1417 procfs_ops
.to_insert_watchpoint
= procfs_insert_hw_watchpoint
;
1418 procfs_ops
.to_remove_watchpoint
= procfs_remove_hw_watchpoint
;
1419 procfs_ops
.to_stopped_by_watchpoint
= procfs_stopped_by_watchpoint
;
1420 procfs_ops
.to_terminal_init
= terminal_init_inferior
;
1421 procfs_ops
.to_terminal_inferior
= terminal_inferior
;
1422 procfs_ops
.to_terminal_ours_for_output
= terminal_ours_for_output
;
1423 procfs_ops
.to_terminal_ours
= terminal_ours
;
1424 procfs_ops
.to_terminal_info
= child_terminal_info
;
1425 procfs_ops
.to_kill
= procfs_kill_inferior
;
1426 procfs_ops
.to_create_inferior
= procfs_create_inferior
;
1427 procfs_ops
.to_mourn_inferior
= procfs_mourn_inferior
;
1428 procfs_ops
.to_can_run
= procfs_can_run
;
1429 procfs_ops
.to_pass_signals
= procfs_pass_signals
;
1430 procfs_ops
.to_thread_alive
= procfs_thread_alive
;
1431 procfs_ops
.to_find_new_threads
= procfs_find_new_threads
;
1432 procfs_ops
.to_pid_to_str
= procfs_pid_to_str
;
1433 procfs_ops
.to_stop
= procfs_stop
;
1434 procfs_ops
.to_stratum
= process_stratum
;
1435 procfs_ops
.to_has_all_memory
= default_child_has_all_memory
;
1436 procfs_ops
.to_has_memory
= default_child_has_memory
;
1437 procfs_ops
.to_has_stack
= default_child_has_stack
;
1438 procfs_ops
.to_has_registers
= default_child_has_registers
;
1439 procfs_ops
.to_has_execution
= default_child_has_execution
;
1440 procfs_ops
.to_magic
= OPS_MAGIC
;
1441 procfs_ops
.to_have_continuable_watchpoint
= 1;
1442 procfs_ops
.to_extra_thread_info
= nto_extra_thread_info
;
1445 #define OSTYPE_NTO 1
1448 _initialize_procfs (void)
1453 add_target (&procfs_ops
);
1455 /* We use SIGUSR1 to gain control after we block waiting for a process.
1456 We use sigwaitevent to wait. */
1458 sigaddset (&set
, SIGUSR1
);
1459 sigprocmask (SIG_BLOCK
, &set
, NULL
);
1461 /* Initially, make sure all signals are reported. */
1462 sigfillset (&run
.trace
);
1464 /* Stuff some information. */
1465 nto_cpuinfo_flags
= SYSPAGE_ENTRY (cpuinfo
)->flags
;
1466 nto_cpuinfo_valid
= 1;
1468 add_info ("pidlist", procfs_pidlist
, _("pidlist"));
1469 add_info ("meminfo", procfs_meminfo
, _("memory information"));
1471 nto_is_nto_target
= procfs_is_nto_target
;
1476 procfs_hw_watchpoint (int addr
, int len
, int type
)
1483 brk
.type
= _DEBUG_BREAK_RD
;
1485 case 2: /* Read/Write. */
1486 brk
.type
= _DEBUG_BREAK_RW
;
1488 default: /* Modify. */
1489 /* FIXME: brk.type = _DEBUG_BREAK_RWM gives EINVAL for some reason. */
1490 brk
.type
= _DEBUG_BREAK_RW
;
1492 brk
.type
|= _DEBUG_BREAK_HW
; /* Always ask for HW. */
1496 errno
= devctl (ctl_fd
, DCMD_PROC_BREAK
, &brk
, sizeof (brk
), 0);
1499 perror (_("Failed to set hardware watchpoint"));
1506 procfs_can_use_hw_breakpoint (struct target_ops
*self
,
1507 int type
, int cnt
, int othertype
)
1513 procfs_remove_hw_watchpoint (struct target_ops
*self
,
1514 CORE_ADDR addr
, int len
, int type
,
1515 struct expression
*cond
)
1517 return procfs_hw_watchpoint (addr
, -1, type
);
1521 procfs_insert_hw_watchpoint (struct target_ops
*self
,
1522 CORE_ADDR addr
, int len
, int type
,
1523 struct expression
*cond
)
1525 return procfs_hw_watchpoint (addr
, len
, type
);
1529 procfs_stopped_by_watchpoint (struct target_ops
*ops
)