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-2020 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 "gdbsupport/filestuff.h"
46 #include "gdbsupport/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 (gdb::array_view
<const unsigned char>) override
;
114 bool thread_alive (ptid_t ptid
) override
;
116 void update_thread_list () override
;
118 std::string
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
;
219 nto_procfs_node
= netmgr_strtond (nodestr
, &endstr
);
220 if (nto_procfs_node
== -1)
222 if (errno
== ENOTSUP
)
223 printf_filtered ("QNX Net Manager not found.\n");
224 printf_filtered ("Invalid QNX node %s: error %d (%s).\n", nodestr
,
225 errno
, safe_strerror (errno
));
228 nto_procfs_node
= ND_LOCAL_NODE
;
232 if (*(endstr
- 1) == '/')
238 snprintf (nto_procfs_path
, PATH_MAX
- 1, "%s%s",
239 (nodestr
!= NULL
) ? nodestr
: "", "/proc");
241 scoped_fd
fd (open (nto_procfs_path
, O_RDONLY
));
244 printf_filtered ("Error opening %s : %d (%s)\n", nto_procfs_path
, errno
,
245 safe_strerror (errno
));
246 error (_("Invalid procfs arg"));
249 sysinfo
= (void *) buffer
;
250 if (devctl (fd
.get (), DCMD_PROC_SYSINFO
, sysinfo
, sizeof buffer
, 0) != EOK
)
252 printf_filtered ("Error getting size: %d (%s)\n", errno
,
253 safe_strerror (errno
));
254 error (_("Devctl failed."));
258 total_size
= sysinfo
->total_size
;
259 sysinfo
= alloca (total_size
);
262 printf_filtered ("Memory error: %d (%s)\n", errno
,
263 safe_strerror (errno
));
264 error (_("alloca failed."));
268 if (devctl (fd
.get (), DCMD_PROC_SYSINFO
, sysinfo
, total_size
, 0)
271 printf_filtered ("Error getting sysinfo: %d (%s)\n", errno
,
272 safe_strerror (errno
));
273 error (_("Devctl failed."));
278 nto_map_arch_to_cputype (gdbarch_bfd_arch_info
279 (target_gdbarch ())->arch_name
))
280 error (_("Invalid target CPU."));
285 inf_child_target::open (arg
, from_tty
);
286 printf_filtered ("Debugging using %s\n", nto_procfs_path
);
290 procfs_set_thread (ptid_t ptid
)
295 devctl (ctl_fd
, DCMD_PROC_CURTHREAD
, &tid
, sizeof (tid
), 0);
298 /* Return true if the thread TH is still alive. */
301 nto_procfs_target::thread_alive (ptid_t ptid
)
305 procfs_status status
;
311 if (kill (pid
, 0) == -1)
315 if ((err
= devctl (ctl_fd
, DCMD_PROC_TIDSTATUS
,
316 &status
, sizeof (status
), 0)) != EOK
)
319 /* Thread is alive or dead but not yet joined,
320 or dead and there is an alive (or dead unjoined) thread with
323 If the tid is not the same as requested, requested tid is dead. */
324 return (status
.tid
== tid
) && (status
.state
!= STATE_DEAD
);
328 update_thread_private_data_name (struct thread_info
*new_thread
,
331 nto_thread_info
*pti
= get_nto_thread_info (new_thread
);
333 gdb_assert (newname
!= NULL
);
334 gdb_assert (new_thread
!= NULL
);
338 pti
= new nto_thread_info
;
339 new_thread
->priv
.reset (pti
);
346 update_thread_private_data (struct thread_info
*new_thread
,
347 pthread_t tid
, int state
, int flags
)
350 struct _thread_name
*tn
;
351 procfs_threadctl tctl
;
353 #if _NTO_VERSION > 630
354 gdb_assert (new_thread
!= NULL
);
356 if (devctl (ctl_fd
, DCMD_PROC_INFO
, &pidinfo
,
357 sizeof(pidinfo
), 0) != EOK
)
360 memset (&tctl
, 0, sizeof (tctl
));
361 tctl
.cmd
= _NTO_TCTL_NAME
;
362 tn
= (struct _thread_name
*) (&tctl
.data
);
364 /* Fetch name for the given thread. */
366 tn
->name_buf_len
= sizeof (tctl
.data
) - sizeof (*tn
);
367 tn
->new_name_len
= -1; /* Getting, not setting. */
368 if (devctl (ctl_fd
, DCMD_PROC_THREADCTL
, &tctl
, sizeof (tctl
), NULL
) != EOK
)
369 tn
->name_buf
[0] = '\0';
371 tn
->name_buf
[_NTO_THREAD_NAME_MAX
] = '\0';
373 update_thread_private_data_name (new_thread
, tn
->name_buf
);
375 nto_thread_info
*pti
= get_nto_thread_info (new_thread
);
379 #endif /* _NTO_VERSION */
383 nto_procfs_target::update_thread_list ()
385 procfs_status status
;
389 struct thread_info
*new_thread
;
396 pid
= inferior_ptid
.pid ();
400 for (tid
= 1;; ++tid
)
402 if (status
.tid
== tid
403 && (devctl (ctl_fd
, DCMD_PROC_TIDSTATUS
, &status
, sizeof (status
), 0)
406 if (status
.tid
!= tid
)
407 /* The reason why this would not be equal is that devctl might have
408 returned different tid, meaning the requested tid no longer exists
409 (e.g. thread exited). */
411 ptid
= ptid_t (pid
, 0, tid
);
412 new_thread
= find_thread_ptid (this, ptid
);
414 new_thread
= add_thread (ptid
);
415 update_thread_private_data (new_thread
, tid
, status
.state
, 0);
422 procfs_pidlist (const char *args
, int from_tty
)
424 struct dirent
*dirp
= NULL
;
426 procfs_info
*pidinfo
= NULL
;
427 procfs_debuginfo
*info
= NULL
;
428 procfs_status
*status
= NULL
;
429 pid_t num_threads
= 0;
432 char procfs_dir
[PATH_MAX
];
434 snprintf (procfs_dir
, sizeof (procfs_dir
), "%s%s",
435 (nodestr
!= NULL
) ? nodestr
: "", "/proc");
437 gdb_dir_up
dp (opendir (procfs_dir
));
440 fprintf_unfiltered (gdb_stderr
, "failed to opendir \"%s\" - %d (%s)",
441 procfs_dir
, errno
, safe_strerror (errno
));
445 /* Start scan at first pid. */
446 rewinddir (dp
.get ());
450 /* Get the right pid and procfs path for the pid. */
453 dirp
= readdir (dp
.get ());
456 snprintf (buf
, sizeof (buf
), "%s%s/%s/as",
457 (nodestr
!= NULL
) ? nodestr
: "",
458 "/proc", dirp
->d_name
);
459 pid
= atoi (dirp
->d_name
);
463 /* Open the procfs path. */
464 scoped_fd
fd (open (buf
, O_RDONLY
));
467 fprintf_unfiltered (gdb_stderr
, "failed to open %s - %d (%s)\n",
468 buf
, errno
, safe_strerror (errno
));
472 pidinfo
= (procfs_info
*) buf
;
473 if (devctl (fd
.get (), DCMD_PROC_INFO
, pidinfo
, sizeof (buf
), 0) != EOK
)
475 fprintf_unfiltered (gdb_stderr
,
476 "devctl DCMD_PROC_INFO failed - %d (%s)\n",
477 errno
, safe_strerror (errno
));
480 num_threads
= pidinfo
->num_threads
;
482 info
= (procfs_debuginfo
*) buf
;
483 if (devctl (fd
.get (), DCMD_PROC_MAPDEBUG_BASE
, info
, sizeof (buf
), 0)
485 strcpy (name
, "unavailable");
487 strcpy (name
, info
->path
);
489 /* Collect state info on all the threads. */
490 status
= (procfs_status
*) buf
;
491 for (status
->tid
= 1; status
->tid
<= num_threads
; status
->tid
++)
494 = devctl (fd
.get (), DCMD_PROC_TIDSTATUS
, status
, sizeof (buf
), 0);
495 printf_filtered ("%s - %d", name
, pid
);
496 if (err
== EOK
&& status
->tid
!= 0)
497 printf_filtered ("/%d\n", status
->tid
);
500 printf_filtered ("\n");
505 while (dirp
!= NULL
);
509 procfs_meminfo (const char *args
, int from_tty
)
511 procfs_mapinfo
*mapinfos
= NULL
;
512 static int num_mapinfos
= 0;
513 procfs_mapinfo
*mapinfo_p
, *mapinfo_p2
;
514 int flags
= ~0, err
, num
, i
, j
;
518 procfs_debuginfo info
;
519 char buff
[_POSIX_PATH_MAX
];
527 unsigned debug_vaddr
;
528 unsigned long long offset
;
533 unsigned long long ino
;
540 /* Get the number of map entrys. */
541 err
= devctl (ctl_fd
, DCMD_PROC_MAPINFO
, NULL
, 0, &num
);
544 printf ("failed devctl num mapinfos - %d (%s)\n", err
,
545 safe_strerror (err
));
549 mapinfos
= XNEWVEC (procfs_mapinfo
, num
);
552 mapinfo_p
= mapinfos
;
554 /* Fill the map entrys. */
555 err
= devctl (ctl_fd
, DCMD_PROC_MAPINFO
, mapinfo_p
, num
556 * sizeof (procfs_mapinfo
), &num
);
559 printf ("failed devctl mapinfos - %d (%s)\n", err
, safe_strerror (err
));
564 num
= std::min (num
, num_mapinfos
);
566 /* Run through the list of mapinfos, and store the data and text info
567 so we can print it at the bottom of the loop. */
568 for (mapinfo_p
= mapinfos
, i
= 0; i
< num
; i
++, mapinfo_p
++)
570 if (!(mapinfo_p
->flags
& flags
))
573 if (mapinfo_p
->ino
== 0) /* Already visited. */
576 map
.info
.vaddr
= mapinfo_p
->vaddr
;
578 err
= devctl (ctl_fd
, DCMD_PROC_MAPDEBUG
, &map
, sizeof (map
), 0);
582 memset (&printme
, 0, sizeof printme
);
583 printme
.dev
= mapinfo_p
->dev
;
584 printme
.ino
= mapinfo_p
->ino
;
585 printme
.text
.addr
= mapinfo_p
->vaddr
;
586 printme
.text
.size
= mapinfo_p
->size
;
587 printme
.text
.flags
= mapinfo_p
->flags
;
588 printme
.text
.offset
= mapinfo_p
->offset
;
589 printme
.text
.debug_vaddr
= map
.info
.vaddr
;
590 strcpy (printme
.name
, map
.info
.path
);
592 /* Check for matching data. */
593 for (mapinfo_p2
= mapinfos
, j
= 0; j
< num
; j
++, mapinfo_p2
++)
595 if (mapinfo_p2
->vaddr
!= mapinfo_p
->vaddr
596 && mapinfo_p2
->ino
== mapinfo_p
->ino
597 && mapinfo_p2
->dev
== mapinfo_p
->dev
)
599 map
.info
.vaddr
= mapinfo_p2
->vaddr
;
601 devctl (ctl_fd
, DCMD_PROC_MAPDEBUG
, &map
, sizeof (map
), 0);
605 if (strcmp (map
.info
.path
, printme
.name
))
608 /* Lower debug_vaddr is always text, if necessary, swap. */
609 if ((int) map
.info
.vaddr
< (int) printme
.text
.debug_vaddr
)
611 memcpy (&(printme
.data
), &(printme
.text
),
612 sizeof (printme
.data
));
613 printme
.text
.addr
= mapinfo_p2
->vaddr
;
614 printme
.text
.size
= mapinfo_p2
->size
;
615 printme
.text
.flags
= mapinfo_p2
->flags
;
616 printme
.text
.offset
= mapinfo_p2
->offset
;
617 printme
.text
.debug_vaddr
= map
.info
.vaddr
;
621 printme
.data
.addr
= mapinfo_p2
->vaddr
;
622 printme
.data
.size
= mapinfo_p2
->size
;
623 printme
.data
.flags
= mapinfo_p2
->flags
;
624 printme
.data
.offset
= mapinfo_p2
->offset
;
625 printme
.data
.debug_vaddr
= map
.info
.vaddr
;
632 printf_filtered ("%s\n", printme
.name
);
633 printf_filtered ("\ttext=%08x bytes @ 0x%08x\n", printme
.text
.size
,
635 printf_filtered ("\t\tflags=%08x\n", printme
.text
.flags
);
636 printf_filtered ("\t\tdebug=%08x\n", printme
.text
.debug_vaddr
);
637 printf_filtered ("\t\toffset=%s\n", phex (printme
.text
.offset
, 8));
638 if (printme
.data
.size
)
640 printf_filtered ("\tdata=%08x bytes @ 0x%08x\n", printme
.data
.size
,
642 printf_filtered ("\t\tflags=%08x\n", printme
.data
.flags
);
643 printf_filtered ("\t\tdebug=%08x\n", printme
.data
.debug_vaddr
);
644 printf_filtered ("\t\toffset=%s\n", phex (printme
.data
.offset
, 8));
646 printf_filtered ("\tdev=0x%x\n", printme
.dev
);
647 printf_filtered ("\tino=0x%x\n", (unsigned int) printme
.ino
);
653 /* Print status information about what we're accessing. */
655 nto_procfs_target::files_info ()
657 struct inferior
*inf
= current_inferior ();
659 printf_unfiltered ("\tUsing the running image of %s %s via %s.\n",
660 inf
->attach_flag
? "attached" : "child",
661 target_pid_to_str (inferior_ptid
).c_str (),
662 (nodestr
!= NULL
) ? nodestr
: "local node");
665 /* Target to_pid_to_exec_file implementation. */
668 nto_procfs_target::pid_to_exec_file (const int pid
)
671 static char proc_path
[PATH_MAX
];
674 /* Read exe file name. */
675 snprintf (proc_path
, sizeof (proc_path
), "%s/proc/%d/exefile",
676 (nodestr
!= NULL
) ? nodestr
: "", pid
);
677 proc_fd
= open (proc_path
, O_RDONLY
);
681 rd
= read (proc_fd
, proc_path
, sizeof (proc_path
) - 1);
688 proc_path
[rd
] = '\0';
692 /* Attach to process PID, then initialize for debugging it. */
694 nto_procfs_target::attach (const char *args
, int from_tty
)
697 struct inferior
*inf
;
699 pid
= parse_pid_to_attach (args
);
701 if (pid
== getpid ())
702 error (_("Attaching GDB to itself is not a good idea..."));
706 const char *exec_file
= get_exec_file (0);
709 printf_unfiltered ("Attaching to program `%s', %s\n", exec_file
,
710 target_pid_to_str (ptid_t (pid
)).c_str ());
712 printf_unfiltered ("Attaching to %s\n",
713 target_pid_to_str (ptid_t (pid
)).c_str ());
715 inferior_ptid
= do_attach (ptid_t (pid
));
716 inf
= current_inferior ();
717 inferior_appeared (inf
, pid
);
718 inf
->attach_flag
= 1;
720 if (!target_is_pushed (ops
))
723 procfs_update_thread_list (ops
);
727 nto_procfs_target::post_attach (pid_t pid
)
730 solib_create_inferior_hook (0);
734 do_attach (ptid_t ptid
)
736 procfs_status status
;
737 struct sigevent event
;
740 snprintf (path
, PATH_MAX
- 1, "%s%s/%d/as",
741 (nodestr
!= NULL
) ? nodestr
: "", "/proc", ptid
.pid ());
742 ctl_fd
= open (path
, O_RDWR
);
744 error (_("Couldn't open proc file %s, error %d (%s)"), path
, errno
,
745 safe_strerror (errno
));
746 if (devctl (ctl_fd
, DCMD_PROC_STOP
, &status
, sizeof (status
), 0) != EOK
)
747 error (_("Couldn't stop process"));
749 /* Define a sigevent for process stopped notification. */
750 event
.sigev_notify
= SIGEV_SIGNAL_THREAD
;
751 event
.sigev_signo
= SIGUSR1
;
752 event
.sigev_code
= 0;
753 event
.sigev_value
.sival_ptr
= NULL
;
754 event
.sigev_priority
= -1;
755 devctl (ctl_fd
, DCMD_PROC_EVENT
, &event
, sizeof (event
), 0);
757 if (devctl (ctl_fd
, DCMD_PROC_STATUS
, &status
, sizeof (status
), 0) == EOK
758 && status
.flags
& _DEBUG_FLAG_STOPPED
)
759 SignalKill (nto_node (), ptid
.pid (), 0, SIGCONT
, 0, 0);
760 nto_init_solib_absolute_prefix ();
761 return ptid_t (ptid
.pid (), 0, status
.tid
);
764 /* Ask the user what to do when an interrupt is received. */
766 interrupt_query (void)
768 if (query (_("Interrupted while waiting for the program.\n\
769 Give up (and stop debugging it)? ")))
771 target_mourn_inferior (inferior_ptid
);
776 /* The user typed ^C twice. */
778 nto_handle_sigint_twice (int signo
)
780 signal (signo
, ofunc
);
782 signal (signo
, nto_handle_sigint_twice
);
786 nto_handle_sigint (int signo
)
788 /* If this doesn't work, try more severe steps. */
789 signal (signo
, nto_handle_sigint_twice
);
795 nto_procfs_target::wait (ptid_t ptid
, struct target_waitstatus
*ourstatus
,
800 procfs_status status
;
801 static int exit_signo
= 0; /* To track signals that cause termination. */
803 ourstatus
->kind
= TARGET_WAITKIND_SPURIOUS
;
805 if (inferior_ptid
== null_ptid
)
807 ourstatus
->kind
= TARGET_WAITKIND_STOPPED
;
808 ourstatus
->value
.sig
= GDB_SIGNAL_0
;
814 sigaddset (&set
, SIGUSR1
);
816 devctl (ctl_fd
, DCMD_PROC_STATUS
, &status
, sizeof (status
), 0);
817 while (!(status
.flags
& _DEBUG_FLAG_ISTOP
))
819 ofunc
= signal (SIGINT
, nto_handle_sigint
);
820 sigwaitinfo (&set
, &info
);
821 signal (SIGINT
, ofunc
);
822 devctl (ctl_fd
, DCMD_PROC_STATUS
, &status
, sizeof (status
), 0);
825 nto_inferior_data (NULL
)->stopped_flags
= status
.flags
;
826 nto_inferior_data (NULL
)->stopped_pc
= status
.ip
;
828 if (status
.flags
& _DEBUG_FLAG_SSTEP
)
830 ourstatus
->kind
= TARGET_WAITKIND_STOPPED
;
831 ourstatus
->value
.sig
= GDB_SIGNAL_TRAP
;
833 /* Was it a breakpoint? */
834 else if (status
.flags
& _DEBUG_FLAG_TRACE
)
836 ourstatus
->kind
= TARGET_WAITKIND_STOPPED
;
837 ourstatus
->value
.sig
= GDB_SIGNAL_TRAP
;
839 else if (status
.flags
& _DEBUG_FLAG_ISTOP
)
843 case _DEBUG_WHY_SIGNALLED
:
844 ourstatus
->kind
= TARGET_WAITKIND_STOPPED
;
845 ourstatus
->value
.sig
=
846 gdb_signal_from_host (status
.info
.si_signo
);
849 case _DEBUG_WHY_FAULTED
:
850 ourstatus
->kind
= TARGET_WAITKIND_STOPPED
;
851 if (status
.info
.si_signo
== SIGTRAP
)
853 ourstatus
->value
.sig
= 0;
858 ourstatus
->value
.sig
=
859 gdb_signal_from_host (status
.info
.si_signo
);
860 exit_signo
= ourstatus
->value
.sig
;
864 case _DEBUG_WHY_TERMINATED
:
868 waitpid (inferior_ptid
.pid (), &waitval
, WNOHANG
);
871 /* Abnormal death. */
872 ourstatus
->kind
= TARGET_WAITKIND_SIGNALLED
;
873 ourstatus
->value
.sig
= exit_signo
;
878 ourstatus
->kind
= TARGET_WAITKIND_EXITED
;
879 ourstatus
->value
.integer
= WEXITSTATUS (waitval
);
885 case _DEBUG_WHY_REQUESTED
:
886 /* We are assuming a requested stop is due to a SIGINT. */
887 ourstatus
->kind
= TARGET_WAITKIND_STOPPED
;
888 ourstatus
->value
.sig
= GDB_SIGNAL_INT
;
894 return ptid_t (status
.pid
, 0, status
.tid
);
897 /* Read the current values of the inferior's registers, both the
898 general register set and floating point registers (if supported)
899 and update gdb's idea of their current values. */
901 nto_procfs_target::fetch_registers (struct regcache
*regcache
, int regno
)
907 procfs_altreg altreg
;
912 procfs_set_thread (regcache
->ptid ());
913 if (devctl (ctl_fd
, DCMD_PROC_GETGREG
, ®
, sizeof (reg
), ®size
) == EOK
)
914 nto_supply_gregset (regcache
, (char *) ®
.greg
);
915 if (devctl (ctl_fd
, DCMD_PROC_GETFPREG
, ®
, sizeof (reg
), ®size
)
917 nto_supply_fpregset (regcache
, (char *) ®
.fpreg
);
918 if (devctl (ctl_fd
, DCMD_PROC_GETALTREG
, ®
, sizeof (reg
), ®size
)
920 nto_supply_altregset (regcache
, (char *) ®
.altreg
);
923 /* Helper for procfs_xfer_partial that handles memory transfers.
924 Arguments are like target_xfer_partial. */
926 static enum target_xfer_status
927 procfs_xfer_memory (gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
928 ULONGEST memaddr
, ULONGEST len
, ULONGEST
*xfered_len
)
932 if (lseek (ctl_fd
, (off_t
) memaddr
, SEEK_SET
) != (off_t
) memaddr
)
933 return TARGET_XFER_E_IO
;
935 if (writebuf
!= NULL
)
936 nbytes
= write (ctl_fd
, writebuf
, len
);
938 nbytes
= read (ctl_fd
, readbuf
, len
);
940 return TARGET_XFER_E_IO
;
941 *xfered_len
= nbytes
;
942 return TARGET_XFER_OK
;
945 /* Target to_xfer_partial implementation. */
947 enum target_xfer_status
948 nto_procfs_target::xfer_partial (enum target_object object
,
949 const char *annex
, gdb_byte
*readbuf
,
950 const gdb_byte
*writebuf
, ULONGEST offset
,
951 ULONGEST len
, ULONGEST
*xfered_len
)
955 case TARGET_OBJECT_MEMORY
:
956 return procfs_xfer_memory (readbuf
, writebuf
, offset
, len
, xfered_len
);
957 case TARGET_OBJECT_AUXV
:
961 CORE_ADDR initial_stack
;
962 debug_process_t procinfo
;
963 /* For 32-bit architecture, size of auxv_t is 8 bytes. */
964 const unsigned int sizeof_auxv_t
= sizeof (auxv_t
);
965 const unsigned int sizeof_tempbuf
= 20 * sizeof_auxv_t
;
967 gdb_byte
*const tempbuf
= alloca (sizeof_tempbuf
);
970 return TARGET_XFER_E_IO
;
972 err
= devctl (ctl_fd
, DCMD_PROC_INFO
, &procinfo
,
975 return TARGET_XFER_E_IO
;
977 initial_stack
= procinfo
.initial_stack
;
979 /* procfs is always 'self-hosted', no byte-order manipulation. */
980 tempread
= nto_read_auxv_from_initial_stack (initial_stack
, tempbuf
,
983 tempread
= std::min (tempread
, len
) - offset
;
984 memcpy (readbuf
, tempbuf
+ offset
, tempread
);
985 *xfered_len
= tempread
;
986 return tempread
? TARGET_XFER_OK
: TARGET_XFER_EOF
;
990 return this->beneath ()->xfer_partial (object
, annex
,
991 readbuf
, writebuf
, offset
, len
,
996 /* Take a program previously attached to and detaches it.
997 The program resumes execution and will no longer stop
998 on signals, etc. We'd better not have left any breakpoints
999 in the program or it'll die when it hits one. */
1001 nto_procfs_target::detach (inferior
*inf
, int from_tty
)
1005 target_announce_detach ();
1008 SignalKill (nto_node (), inferior_ptid
.pid (), 0, 0, 0, 0);
1013 pid
= inferior_ptid
.pid ();
1014 inferior_ptid
= null_ptid
;
1015 detach_inferior (pid
);
1016 init_thread_list ();
1017 inf_child_maybe_unpush_target (ops
);
1021 procfs_breakpoint (CORE_ADDR addr
, int type
, int size
)
1028 errno
= devctl (ctl_fd
, DCMD_PROC_BREAK
, &brk
, sizeof (brk
), 0);
1035 nto_procfs_target::insert_breakpoint (struct gdbarch
*gdbarch
,
1036 struct bp_target_info
*bp_tgt
)
1038 bp_tgt
->placed_address
= bp_tgt
->reqstd_address
;
1039 return procfs_breakpoint (bp_tgt
->placed_address
, _DEBUG_BREAK_EXEC
, 0);
1043 nto_procfs_target::remove_breakpoint (struct gdbarch
*gdbarch
,
1044 struct bp_target_info
*bp_tgt
,
1045 enum remove_bp_reason reason
)
1047 return procfs_breakpoint (bp_tgt
->placed_address
, _DEBUG_BREAK_EXEC
, -1);
1051 nto_procfs_target::insert_hw_breakpoint (struct gdbarch
*gdbarch
,
1052 struct bp_target_info
*bp_tgt
)
1054 bp_tgt
->placed_address
= bp_tgt
->reqstd_address
;
1055 return procfs_breakpoint (bp_tgt
->placed_address
,
1056 _DEBUG_BREAK_EXEC
| _DEBUG_BREAK_HW
, 0);
1060 nto_procfs_target::remove_hw_breakpoint (struct gdbarch
*gdbarch
,
1061 struct bp_target_info
*bp_tgt
)
1063 return procfs_breakpoint (bp_tgt
->placed_address
,
1064 _DEBUG_BREAK_EXEC
| _DEBUG_BREAK_HW
, -1);
1068 nto_procfs_target::resume (ptid_t ptid
, int step
, enum gdb_signal signo
)
1071 procfs_status status
;
1072 sigset_t
*run_fault
= (sigset_t
*) (void *) &run
.fault
;
1074 if (inferior_ptid
== null_ptid
)
1077 procfs_set_thread (ptid
== minus_one_ptid
? inferior_ptid
:
1080 run
.flags
= _DEBUG_RUN_FAULT
| _DEBUG_RUN_TRACE
;
1082 run
.flags
|= _DEBUG_RUN_STEP
;
1084 sigemptyset (run_fault
);
1085 sigaddset (run_fault
, FLTBPT
);
1086 sigaddset (run_fault
, FLTTRACE
);
1087 sigaddset (run_fault
, FLTILL
);
1088 sigaddset (run_fault
, FLTPRIV
);
1089 sigaddset (run_fault
, FLTBOUNDS
);
1090 sigaddset (run_fault
, FLTIOVF
);
1091 sigaddset (run_fault
, FLTIZDIV
);
1092 sigaddset (run_fault
, FLTFPE
);
1093 /* Peter V will be changing this at some point. */
1094 sigaddset (run_fault
, FLTPAGE
);
1096 run
.flags
|= _DEBUG_RUN_ARM
;
1098 signal_to_pass
= gdb_signal_to_host (signo
);
1102 devctl (ctl_fd
, DCMD_PROC_STATUS
, &status
, sizeof (status
), 0);
1103 signal_to_pass
= gdb_signal_to_host (signo
);
1104 if (status
.why
& (_DEBUG_WHY_SIGNALLED
| _DEBUG_WHY_FAULTED
))
1106 if (signal_to_pass
!= status
.info
.si_signo
)
1108 SignalKill (nto_node (), inferior_ptid
.pid (), 0,
1109 signal_to_pass
, 0, 0);
1110 run
.flags
|= _DEBUG_RUN_CLRFLT
| _DEBUG_RUN_CLRSIG
;
1112 else /* Let it kill the program without telling us. */
1113 sigdelset (&run
.trace
, signal_to_pass
);
1117 run
.flags
|= _DEBUG_RUN_CLRSIG
| _DEBUG_RUN_CLRFLT
;
1119 errno
= devctl (ctl_fd
, DCMD_PROC_RUN
, &run
, sizeof (run
), 0);
1122 perror (_("run error!\n"));
1128 nto_procfs_target::mourn_inferior ()
1130 if (inferior_ptid
!= null_ptid
)
1132 SignalKill (nto_node (), inferior_ptid
.pid (), 0, SIGKILL
, 0, 0);
1135 inferior_ptid
= null_ptid
;
1136 init_thread_list ();
1137 inf_child_mourn_inferior (ops
);
1140 /* This function breaks up an argument string into an argument
1141 vector suitable for passing to execvp().
1142 E.g., on "run a b c d" this routine would get as input
1143 the string "a b c d", and as output it would fill in argv with
1144 the four arguments "a", "b", "c", "d". The only additional
1145 functionality is simple quoting. The gdb command:
1147 will fill in argv with the three args "a", "b c d", "e". */
1149 breakup_args (char *scratch
, char **argv
)
1151 char *pp
, *cp
= scratch
;
1156 /* Scan past leading separators. */
1158 while (*cp
== ' ' || *cp
== '\t' || *cp
== '\n')
1161 /* Break if at end of string. */
1169 quoting
= strchr (cp
, '"') ? 1 : 0;
1174 /* Scan for next arg separator. */
1177 cp
= strchr (pp
, '"');
1178 if ((cp
== NULL
) || (!quoting
))
1179 cp
= strchr (pp
, ' ');
1181 cp
= strchr (pp
, '\t');
1183 cp
= strchr (pp
, '\n');
1185 /* No separators => end of string => break. */
1192 /* Replace the separator with a terminator. */
1196 /* Execv requires a null-terminated arg vector. */
1201 nto_procfs_target::create_inferior (const char *exec_file
,
1202 const std::string
&allargs
,
1203 char **env
, int from_tty
)
1205 struct inheritance inherit
;
1209 const char *in
= "", *out
= "", *err
= "";
1212 const char *inferior_io_terminal
= get_inferior_io_terminal ();
1213 struct inferior
*inf
;
1215 argv
= xmalloc ((allargs
.size () / (unsigned) 2 + 2) *
1217 argv
[0] = const_cast<char *> (get_exec_file (1));
1221 argv
[0] = exec_file
;
1226 args
= xstrdup (allargs
.c_str ());
1227 breakup_args (args
, (exec_file
!= NULL
) ? &argv
[1] : &argv
[0]);
1229 argv
= nto_parse_redirection (argv
, &in
, &out
, &err
);
1231 fds
[0] = STDIN_FILENO
;
1232 fds
[1] = STDOUT_FILENO
;
1233 fds
[2] = STDERR_FILENO
;
1235 /* If the user specified I/O via gdb's --tty= arg, use it, but only
1236 if the i/o is not also being specified via redirection. */
1237 if (inferior_io_terminal
)
1240 in
= inferior_io_terminal
;
1242 out
= inferior_io_terminal
;
1244 err
= inferior_io_terminal
;
1249 fd
= open (in
, O_RDONLY
);
1257 fd
= open (out
, O_WRONLY
);
1265 fd
= open (err
, O_WRONLY
);
1272 /* Clear any pending SIGUSR1's but keep the behavior the same. */
1273 signal (SIGUSR1
, signal (SIGUSR1
, SIG_IGN
));
1276 sigaddset (&set
, SIGUSR1
);
1277 sigprocmask (SIG_UNBLOCK
, &set
, NULL
);
1279 memset (&inherit
, 0, sizeof (inherit
));
1281 if (ND_NODE_CMP (nto_procfs_node
, ND_LOCAL_NODE
) != 0)
1283 inherit
.nd
= nto_node ();
1284 inherit
.flags
|= SPAWN_SETND
;
1285 inherit
.flags
&= ~SPAWN_EXEC
;
1287 inherit
.flags
|= SPAWN_SETGROUP
| SPAWN_HOLD
;
1288 inherit
.pgroup
= SPAWN_NEWPGROUP
;
1289 pid
= spawnp (argv
[0], 3, fds
, &inherit
, argv
,
1290 ND_NODE_CMP (nto_procfs_node
, ND_LOCAL_NODE
) == 0 ? env
: 0);
1293 sigprocmask (SIG_BLOCK
, &set
, NULL
);
1296 error (_("Error spawning %s: %d (%s)"), argv
[0], errno
,
1297 safe_strerror (errno
));
1299 if (fds
[0] != STDIN_FILENO
)
1301 if (fds
[1] != STDOUT_FILENO
)
1303 if (fds
[2] != STDERR_FILENO
)
1306 inferior_ptid
= do_attach (ptid_t (pid
));
1307 procfs_update_thread_list (ops
);
1309 inf
= current_inferior ();
1310 inferior_appeared (inf
, pid
);
1311 inf
->attach_flag
= 0;
1313 flags
= _DEBUG_FLAG_KLC
; /* Kill-on-Last-Close flag. */
1314 errn
= devctl (ctl_fd
, DCMD_PROC_SET_FLAG
, &flags
, sizeof (flags
), 0);
1317 /* FIXME: expected warning? */
1318 /* warning( "Failed to set Kill-on-Last-Close flag: errno = %d(%s)\n",
1319 errn, safe_strerror(errn) ); */
1321 if (!target_is_pushed (ops
))
1323 target_terminal::init ();
1325 if (exec_bfd
!= NULL
1326 || (symfile_objfile
!= NULL
&& symfile_objfile
->obfd
!= NULL
))
1327 solib_create_inferior_hook (0);
1331 nto_procfs_target::interrupt ()
1333 devctl (ctl_fd
, DCMD_PROC_STOP
, NULL
, 0, 0);
1337 nto_procfs_target::kill ()
1339 target_mourn_inferior (inferior_ptid
);
1342 /* Fill buf with regset and return devctl cmd to do the setting. Return
1343 -1 if we fail to get the regset. Store size of regset in regsize. */
1345 get_regset (int regset
, char *buf
, int bufsize
, int *regsize
)
1347 int dev_get
, dev_set
;
1350 case NTO_REG_GENERAL
:
1351 dev_get
= DCMD_PROC_GETGREG
;
1352 dev_set
= DCMD_PROC_SETGREG
;
1356 dev_get
= DCMD_PROC_GETFPREG
;
1357 dev_set
= DCMD_PROC_SETFPREG
;
1361 dev_get
= DCMD_PROC_GETALTREG
;
1362 dev_set
= DCMD_PROC_SETALTREG
;
1365 case NTO_REG_SYSTEM
:
1369 if (devctl (ctl_fd
, dev_get
, buf
, bufsize
, regsize
) != EOK
)
1376 nto_procfs_target::store_registers (struct regcache
*regcache
, int regno
)
1382 procfs_altreg altreg
;
1386 int len
, regset
, regsize
, dev_set
, err
;
1388 ptid_t ptid
= regcache
->ptid ();
1390 if (ptid
== null_ptid
)
1392 procfs_set_thread (ptid
);
1396 for (regset
= NTO_REG_GENERAL
; regset
< NTO_REG_END
; regset
++)
1398 dev_set
= get_regset (regset
, (char *) ®
,
1399 sizeof (reg
), ®size
);
1403 if (nto_regset_fill (regcache
, regset
, (char *) ®
) == -1)
1406 err
= devctl (ctl_fd
, dev_set
, ®
, regsize
, 0);
1408 fprintf_unfiltered (gdb_stderr
,
1409 "Warning unable to write regset %d: %s\n",
1410 regno
, safe_strerror (err
));
1415 regset
= nto_regset_id (regno
);
1419 dev_set
= get_regset (regset
, (char *) ®
, sizeof (reg
), ®size
);
1423 len
= nto_register_area (regcache
->arch (),
1424 regno
, regset
, &off
);
1429 regcache
->raw_collect (regno
, (char *) ®
+ off
);
1431 err
= devctl (ctl_fd
, dev_set
, ®
, regsize
, 0);
1433 fprintf_unfiltered (gdb_stderr
,
1434 "Warning unable to write regset %d: %s\n", regno
,
1435 safe_strerror (err
));
1439 /* Set list of signals to be handled in the target. */
1442 nto_procfs_target::pass_signals
1443 (gdb::array_view
<const unsigned char> pass_signals
)
1447 sigfillset (&run
.trace
);
1449 for (signo
= 1; signo
< NSIG
; signo
++)
1451 int target_signo
= gdb_signal_from_host (signo
);
1452 if (target_signo
< pass_signals
.size () && pass_signals
[target_signo
])
1453 sigdelset (&run
.trace
, signo
);
1458 nto_procfs_target::pid_to_str (ptid_t ptid
)
1461 struct tidinfo
*tip
;
1467 tip
= procfs_thread_info (pid
, tid
);
1469 snprintf (&buf
[n
], 1023, " (state = 0x%02x)", tip
->state
);
1472 return string_printf ("process %d", pid
);
1475 /* to_can_run implementation for "target procfs". Note this really
1476 means "can this target be the default run target", which there can
1477 be only one, and we make it be "target native" like other ports.
1478 "target procfs <node>" wouldn't make sense as default run target, as
1482 nto_procfs_target::can_run ()
1487 /* "target procfs". */
1488 static nto_procfs_target_procfs nto_procfs_ops
;
1490 /* "target native". */
1491 static nto_procfs_target_native nto_native_ops
;
1493 /* Create the "native" and "procfs" targets. */
1496 init_procfs_targets (void)
1498 /* Register "target native". This is the default run target. */
1499 add_target (nto_native_target_info
, inf_child_open_target
);
1500 set_native_target (&nto_native_ops
);
1502 /* Register "target procfs <node>". */
1503 add_target (nto_procfs_target_info
, inf_child_open_target
);
1506 #define OSTYPE_NTO 1
1508 void _initialize_procfs ();
1510 _initialize_procfs ()
1514 init_procfs_targets ();
1516 /* We use SIGUSR1 to gain control after we block waiting for a process.
1517 We use sigwaitevent to wait. */
1519 sigaddset (&set
, SIGUSR1
);
1520 sigprocmask (SIG_BLOCK
, &set
, NULL
);
1522 /* Initially, make sure all signals are reported. */
1523 sigfillset (&run
.trace
);
1525 /* Stuff some information. */
1526 nto_cpuinfo_flags
= SYSPAGE_ENTRY (cpuinfo
)->flags
;
1527 nto_cpuinfo_valid
= 1;
1529 add_info ("pidlist", procfs_pidlist
, _("pidlist"));
1530 add_info ("meminfo", procfs_meminfo
, _("memory information"));
1532 nto_is_nto_target
= procfs_is_nto_target
;
1537 procfs_hw_watchpoint (int addr
, int len
, enum target_hw_bp_type type
)
1544 brk
.type
= _DEBUG_BREAK_RD
;
1547 brk
.type
= _DEBUG_BREAK_RW
;
1549 default: /* Modify. */
1550 /* FIXME: brk.type = _DEBUG_BREAK_RWM gives EINVAL for some reason. */
1551 brk
.type
= _DEBUG_BREAK_RW
;
1553 brk
.type
|= _DEBUG_BREAK_HW
; /* Always ask for HW. */
1557 errno
= devctl (ctl_fd
, DCMD_PROC_BREAK
, &brk
, sizeof (brk
), 0);
1560 perror (_("Failed to set hardware watchpoint"));
1567 nto_procfs_target::can_use_hw_breakpoint (enum bptype type
,
1568 int cnt
, int othertype
)
1574 nto_procfs_target::remove_hw_watchpoint (CORE_ADDR addr
, int len
,
1575 enum target_hw_bp_type type
,
1576 struct expression
*cond
)
1578 return procfs_hw_watchpoint (addr
, -1, type
);
1582 nto_procfs_target::insert_hw_watchpoint (CORE_ADDR addr
, int len
,
1583 enum target_hw_bp_type type
,
1584 struct expression
*cond
)
1586 return procfs_hw_watchpoint (addr
, len
, type
);
1590 nto_procfs_target::stopped_by_watchpoint ()
1592 /* NOTE: nto_stopped_by_watchpoint will be called ONLY while we are
1593 stopped due to a SIGTRAP. This assumes gdb works in 'all-stop' mode;
1594 future gdb versions will likely run in 'non-stop' mode in which case
1595 we will have to store/examine statuses per thread in question.
1596 Until then, this will work fine. */
1598 struct inferior
*inf
= current_inferior ();
1599 struct nto_inferior_data
*inf_data
;
1601 gdb_assert (inf
!= NULL
);
1603 inf_data
= nto_inferior_data (inf
);
1605 return inf_data
->stopped_flags
1606 & (_DEBUG_FLAG_TRACE_RD
1607 | _DEBUG_FLAG_TRACE_WR
1608 | _DEBUG_FLAG_TRACE_MODIFY
);