1 /* Machine independent support for QNX Neutrino /proc (process file system)
2 for GDB. Written by Colin Burgess at QNX Software Systems Limited.
4 Copyright (C) 2003-2018 Free Software Foundation, Inc.
6 Contributed by QNX Software Systems Ltd.
8 This file is part of GDB.
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>. */
27 #include <sys/debug.h>
28 #include <sys/procfs.h>
29 #include <sys/neutrino.h>
30 #include <sys/syspage.h>
32 #include <sys/netmgr.h>
39 #include "gdbthread.h"
44 #include "inf-child.h"
45 #include "common/filestuff.h"
46 #include "common/scoped_fd.h"
49 #define _DEBUG_FLAG_TRACE (_DEBUG_FLAG_TRACE_EXEC|_DEBUG_FLAG_TRACE_RD|\
50 _DEBUG_FLAG_TRACE_WR|_DEBUG_FLAG_TRACE_MODIFY)
54 static sighandler_t ofunc
;
56 static procfs_run run
;
58 /* Create the "native" and "procfs" targets. */
60 struct nto_procfs_target
: public inf_child_target
62 void open (const char *arg
, int from_tty
) override
;
64 void attach (const char *, int) override
= 0;
66 void post_attach (int);
68 void detach (inferior
*, int) override
;
70 void resume (ptid_t
, int, enum gdb_signal
) override
;
72 ptid_t
wait (ptid_t
, struct target_waitstatus
*, int) override
;
74 void fetch_registers (struct regcache
*, int) override
;
75 void store_registers (struct regcache
*, int) override
;
77 enum target_xfer_status
xfer_partial (enum target_object object
,
80 const gdb_byte
*writebuf
,
81 ULONGEST offset
, ULONGEST len
,
82 ULONGEST
*xfered_len
) override
;
84 void files_info () override
;
86 int insert_breakpoint (struct gdbarch
*, struct bp_target_info
*) override
;
88 int remove_breakpoint (struct gdbarch
*, struct bp_target_info
*,
89 enum remove_bp_reason
) override
;
91 int can_use_hw_breakpoint (enum bptype
, int, int) override
;
93 int insert_hw_breakpoint (struct gdbarch
*, struct bp_target_info
*) override
;
95 int remove_hw_breakpoint (struct gdbarch
*, struct bp_target_info
*) override
;
97 int insert_watchpoint (CORE_ADDR
, int, enum target_hw_bp_type
,
98 struct expression
*) override
;
100 int remove_watchpoint (CORE_ADDR
, int, enum target_hw_bp_type
,
101 struct expression
*) override
;
103 bool stopped_by_watchpoint () override
;
105 void kill () override
;
107 void create_inferior (const char *, const std::string
&,
108 char **, int) override
;
110 void mourn_inferior () override
;
112 void pass_signals (int, unsigned char *) override
;
114 bool thread_alive (ptid_t ptid
) override
;
116 void update_thread_list () override
;
118 const char *pid_to_str (ptid_t
) override
;
120 void interrupt () override
;
122 bool have_continuable_watchpoint ()
125 const char *extra_thread_info (struct thread_info
*) override
;
127 char *pid_to_exec_file (int pid
) override
;
130 /* For "target native". */
131 struct nto_procfs_target_native final
: public nto_procfs_target
133 /* Leave shortname as "native". */
135 const char *longname () override
136 { return _("QNX Neutrino local process"); }
138 const char *doc () override
139 { return _("QNX Neutrino local process (started by the \"run\" command)."); }
142 /* For "target procfs <node>". */
143 struct nto_procfs_target_procfs final
: public nto_procfs_target
145 const char *shortname () override
148 const char *longname () override
149 { return _("QNX Neutrino local or remote process"); }
151 const char *doc () override
152 { return _("QNX Neutrino process. target procfs <node>"); }
155 static ptid_t
do_attach (ptid_t ptid
);
157 /* These two globals are only ever set in procfs_open_1, but are
158 referenced elsewhere. 'nto_procfs_node' is a flag used to say
159 whether we are local, or we should get the current node descriptor
160 for the remote QNX node. */
161 static char *nodestr
;
162 static unsigned nto_procfs_node
= ND_LOCAL_NODE
;
164 /* Return the current QNX Node, or error out. This is a simple
165 wrapper for the netmgr_strtond() function. The reason this
166 is required is because QNX node descriptors are transient so
167 we have to re-acquire them every time. */
173 if (ND_NODE_CMP (nto_procfs_node
, ND_LOCAL_NODE
) == 0
175 return ND_LOCAL_NODE
;
177 node
= netmgr_strtond (nodestr
, 0);
179 error (_("Lost the QNX node. Debug session probably over."));
184 static enum gdb_osabi
185 procfs_is_nto_target (bfd
*abfd
)
187 return GDB_OSABI_QNXNTO
;
190 /* This is called when we call 'target native' or 'target procfs
191 <arg>' from the (gdb) prompt. For QNX6 (nto), the only valid arg
192 will be a QNX node string, eg: "/net/some_node". If arg is not a
193 valid QNX node, we will default to local. */
195 nto_procfs_target::open (const char *arg
, int from_tty
)
200 procfs_sysinfo
*sysinfo
;
201 char nto_procfs_path
[PATH_MAX
];
203 /* Offer to kill previous inferiors before opening this target. */
204 target_preopen (from_tty
);
206 nto_is_nto_target
= procfs_is_nto_target
;
208 /* Set the default node used for spawning to this one,
209 and only override it if there is a valid arg. */
214 nto_procfs_node
= ND_LOCAL_NODE
;
215 nodestr
= (arg
!= NULL
) ? xstrdup (arg
) : NULL
;
221 nto_procfs_node
= netmgr_strtond (nodestr
, &endstr
);
222 if (nto_procfs_node
== -1)
224 if (errno
== ENOTSUP
)
225 printf_filtered ("QNX Net Manager not found.\n");
226 printf_filtered ("Invalid QNX node %s: error %d (%s).\n", nodestr
,
227 errno
, safe_strerror (errno
));
230 nto_procfs_node
= ND_LOCAL_NODE
;
234 if (*(endstr
- 1) == '/')
240 snprintf (nto_procfs_path
, PATH_MAX
- 1, "%s%s",
241 (nodestr
!= NULL
) ? nodestr
: "", "/proc");
243 scoped_fd
fd (open (nto_procfs_path
, O_RDONLY
));
246 printf_filtered ("Error opening %s : %d (%s)\n", nto_procfs_path
, errno
,
247 safe_strerror (errno
));
248 error (_("Invalid procfs arg"));
251 sysinfo
= (void *) buffer
;
252 if (devctl (fd
.get (), DCMD_PROC_SYSINFO
, sysinfo
, sizeof buffer
, 0) != EOK
)
254 printf_filtered ("Error getting size: %d (%s)\n", errno
,
255 safe_strerror (errno
));
256 error (_("Devctl failed."));
260 total_size
= sysinfo
->total_size
;
261 sysinfo
= alloca (total_size
);
264 printf_filtered ("Memory error: %d (%s)\n", errno
,
265 safe_strerror (errno
));
266 error (_("alloca failed."));
270 if (devctl (fd
.get (), DCMD_PROC_SYSINFO
, sysinfo
, total_size
, 0)
273 printf_filtered ("Error getting sysinfo: %d (%s)\n", errno
,
274 safe_strerror (errno
));
275 error (_("Devctl failed."));
280 nto_map_arch_to_cputype (gdbarch_bfd_arch_info
281 (target_gdbarch ())->arch_name
))
282 error (_("Invalid target CPU."));
287 inf_child_target::open (arg
, from_tty
);
288 printf_filtered ("Debugging using %s\n", nto_procfs_path
);
292 procfs_set_thread (ptid_t ptid
)
296 tid
= ptid_get_tid (ptid
);
297 devctl (ctl_fd
, DCMD_PROC_CURTHREAD
, &tid
, sizeof (tid
), 0);
300 /* Return true if the thread TH is still alive. */
303 nto_procfs_target::thread_alive (ptid_t ptid
)
307 procfs_status status
;
310 tid
= ptid_get_tid (ptid
);
311 pid
= ptid_get_pid (ptid
);
313 if (kill (pid
, 0) == -1)
317 if ((err
= devctl (ctl_fd
, DCMD_PROC_TIDSTATUS
,
318 &status
, sizeof (status
), 0)) != EOK
)
321 /* Thread is alive or dead but not yet joined,
322 or dead and there is an alive (or dead unjoined) thread with
325 If the tid is not the same as requested, requested tid is dead. */
326 return (status
.tid
== tid
) && (status
.state
!= STATE_DEAD
);
330 update_thread_private_data_name (struct thread_info
*new_thread
,
333 nto_thread_info
*pti
= get_nto_thread_info (new_thread
);
335 gdb_assert (newname
!= NULL
);
336 gdb_assert (new_thread
!= NULL
);
340 pti
= new nto_thread_info
;
341 new_thread
->priv
.reset (pti
);
348 update_thread_private_data (struct thread_info
*new_thread
,
349 pthread_t tid
, int state
, int flags
)
352 struct _thread_name
*tn
;
353 procfs_threadctl tctl
;
355 #if _NTO_VERSION > 630
356 gdb_assert (new_thread
!= NULL
);
358 if (devctl (ctl_fd
, DCMD_PROC_INFO
, &pidinfo
,
359 sizeof(pidinfo
), 0) != EOK
)
362 memset (&tctl
, 0, sizeof (tctl
));
363 tctl
.cmd
= _NTO_TCTL_NAME
;
364 tn
= (struct _thread_name
*) (&tctl
.data
);
366 /* Fetch name for the given thread. */
368 tn
->name_buf_len
= sizeof (tctl
.data
) - sizeof (*tn
);
369 tn
->new_name_len
= -1; /* Getting, not setting. */
370 if (devctl (ctl_fd
, DCMD_PROC_THREADCTL
, &tctl
, sizeof (tctl
), NULL
) != EOK
)
371 tn
->name_buf
[0] = '\0';
373 tn
->name_buf
[_NTO_THREAD_NAME_MAX
] = '\0';
375 update_thread_private_data_name (new_thread
, tn
->name_buf
);
377 nto_thread_info
*pti
= get_nto_thread_info (new_thread
);
381 #endif /* _NTO_VERSION */
385 nto_procfs_target::update_thread_list ()
387 procfs_status status
;
391 struct thread_info
*new_thread
;
398 pid
= ptid_get_pid (inferior_ptid
);
402 for (tid
= 1;; ++tid
)
404 if (status
.tid
== tid
405 && (devctl (ctl_fd
, DCMD_PROC_TIDSTATUS
, &status
, sizeof (status
), 0)
408 if (status
.tid
!= tid
)
409 /* The reason why this would not be equal is that devctl might have
410 returned different tid, meaning the requested tid no longer exists
411 (e.g. thread exited). */
413 ptid
= ptid_build (pid
, 0, tid
);
414 new_thread
= find_thread_ptid (ptid
);
416 new_thread
= add_thread (ptid
);
417 update_thread_private_data (new_thread
, tid
, status
.state
, 0);
424 do_closedir_cleanup (void *dir
)
430 procfs_pidlist (const char *args
, int from_tty
)
433 struct dirent
*dirp
= NULL
;
435 procfs_info
*pidinfo
= NULL
;
436 procfs_debuginfo
*info
= NULL
;
437 procfs_status
*status
= NULL
;
438 pid_t num_threads
= 0;
441 struct cleanup
*cleanups
;
442 char procfs_dir
[PATH_MAX
];
444 snprintf (procfs_dir
, sizeof (procfs_dir
), "%s%s",
445 (nodestr
!= NULL
) ? nodestr
: "", "/proc");
447 dp
= opendir (procfs_dir
);
450 fprintf_unfiltered (gdb_stderr
, "failed to opendir \"%s\" - %d (%s)",
451 procfs_dir
, errno
, safe_strerror (errno
));
455 cleanups
= make_cleanup (do_closedir_cleanup
, dp
);
457 /* Start scan at first pid. */
462 /* Get the right pid and procfs path for the pid. */
468 do_cleanups (cleanups
);
471 snprintf (buf
, sizeof (buf
), "%s%s/%s/as",
472 (nodestr
!= NULL
) ? nodestr
: "",
473 "/proc", dirp
->d_name
);
474 pid
= atoi (dirp
->d_name
);
478 /* Open the procfs path. */
479 scoped_fd
fd (open (buf
, O_RDONLY
));
482 fprintf_unfiltered (gdb_stderr
, "failed to open %s - %d (%s)\n",
483 buf
, errno
, safe_strerror (errno
));
487 pidinfo
= (procfs_info
*) buf
;
488 if (devctl (fd
.get (), DCMD_PROC_INFO
, pidinfo
, sizeof (buf
), 0) != EOK
)
490 fprintf_unfiltered (gdb_stderr
,
491 "devctl DCMD_PROC_INFO failed - %d (%s)\n",
492 errno
, safe_strerror (errno
));
495 num_threads
= pidinfo
->num_threads
;
497 info
= (procfs_debuginfo
*) buf
;
498 if (devctl (fd
.get (), DCMD_PROC_MAPDEBUG_BASE
, info
, sizeof (buf
), 0)
500 strcpy (name
, "unavailable");
502 strcpy (name
, info
->path
);
504 /* Collect state info on all the threads. */
505 status
= (procfs_status
*) buf
;
506 for (status
->tid
= 1; status
->tid
<= num_threads
; status
->tid
++)
509 = devctl (fd
.get (), DCMD_PROC_TIDSTATUS
, status
, sizeof (buf
), 0);
510 printf_filtered ("%s - %d", name
, pid
);
511 if (err
== EOK
&& status
->tid
!= 0)
512 printf_filtered ("/%d\n", status
->tid
);
515 printf_filtered ("\n");
520 while (dirp
!= NULL
);
522 do_cleanups (cleanups
);
527 procfs_meminfo (const char *args
, int from_tty
)
529 procfs_mapinfo
*mapinfos
= NULL
;
530 static int num_mapinfos
= 0;
531 procfs_mapinfo
*mapinfo_p
, *mapinfo_p2
;
532 int flags
= ~0, err
, num
, i
, j
;
536 procfs_debuginfo info
;
537 char buff
[_POSIX_PATH_MAX
];
545 unsigned debug_vaddr
;
546 unsigned long long offset
;
551 unsigned long long ino
;
558 /* Get the number of map entrys. */
559 err
= devctl (ctl_fd
, DCMD_PROC_MAPINFO
, NULL
, 0, &num
);
562 printf ("failed devctl num mapinfos - %d (%s)\n", err
,
563 safe_strerror (err
));
567 mapinfos
= XNEWVEC (procfs_mapinfo
, num
);
570 mapinfo_p
= mapinfos
;
572 /* Fill the map entrys. */
573 err
= devctl (ctl_fd
, DCMD_PROC_MAPINFO
, mapinfo_p
, num
574 * sizeof (procfs_mapinfo
), &num
);
577 printf ("failed devctl mapinfos - %d (%s)\n", err
, safe_strerror (err
));
582 num
= std::min (num
, num_mapinfos
);
584 /* Run through the list of mapinfos, and store the data and text info
585 so we can print it at the bottom of the loop. */
586 for (mapinfo_p
= mapinfos
, i
= 0; i
< num
; i
++, mapinfo_p
++)
588 if (!(mapinfo_p
->flags
& flags
))
591 if (mapinfo_p
->ino
== 0) /* Already visited. */
594 map
.info
.vaddr
= mapinfo_p
->vaddr
;
596 err
= devctl (ctl_fd
, DCMD_PROC_MAPDEBUG
, &map
, sizeof (map
), 0);
600 memset (&printme
, 0, sizeof printme
);
601 printme
.dev
= mapinfo_p
->dev
;
602 printme
.ino
= mapinfo_p
->ino
;
603 printme
.text
.addr
= mapinfo_p
->vaddr
;
604 printme
.text
.size
= mapinfo_p
->size
;
605 printme
.text
.flags
= mapinfo_p
->flags
;
606 printme
.text
.offset
= mapinfo_p
->offset
;
607 printme
.text
.debug_vaddr
= map
.info
.vaddr
;
608 strcpy (printme
.name
, map
.info
.path
);
610 /* Check for matching data. */
611 for (mapinfo_p2
= mapinfos
, j
= 0; j
< num
; j
++, mapinfo_p2
++)
613 if (mapinfo_p2
->vaddr
!= mapinfo_p
->vaddr
614 && mapinfo_p2
->ino
== mapinfo_p
->ino
615 && mapinfo_p2
->dev
== mapinfo_p
->dev
)
617 map
.info
.vaddr
= mapinfo_p2
->vaddr
;
619 devctl (ctl_fd
, DCMD_PROC_MAPDEBUG
, &map
, sizeof (map
), 0);
623 if (strcmp (map
.info
.path
, printme
.name
))
626 /* Lower debug_vaddr is always text, if nessessary, swap. */
627 if ((int) map
.info
.vaddr
< (int) printme
.text
.debug_vaddr
)
629 memcpy (&(printme
.data
), &(printme
.text
),
630 sizeof (printme
.data
));
631 printme
.text
.addr
= mapinfo_p2
->vaddr
;
632 printme
.text
.size
= mapinfo_p2
->size
;
633 printme
.text
.flags
= mapinfo_p2
->flags
;
634 printme
.text
.offset
= mapinfo_p2
->offset
;
635 printme
.text
.debug_vaddr
= map
.info
.vaddr
;
639 printme
.data
.addr
= mapinfo_p2
->vaddr
;
640 printme
.data
.size
= mapinfo_p2
->size
;
641 printme
.data
.flags
= mapinfo_p2
->flags
;
642 printme
.data
.offset
= mapinfo_p2
->offset
;
643 printme
.data
.debug_vaddr
= map
.info
.vaddr
;
650 printf_filtered ("%s\n", printme
.name
);
651 printf_filtered ("\ttext=%08x bytes @ 0x%08x\n", printme
.text
.size
,
653 printf_filtered ("\t\tflags=%08x\n", printme
.text
.flags
);
654 printf_filtered ("\t\tdebug=%08x\n", printme
.text
.debug_vaddr
);
655 printf_filtered ("\t\toffset=%s\n", phex (printme
.text
.offset
, 8));
656 if (printme
.data
.size
)
658 printf_filtered ("\tdata=%08x bytes @ 0x%08x\n", printme
.data
.size
,
660 printf_filtered ("\t\tflags=%08x\n", printme
.data
.flags
);
661 printf_filtered ("\t\tdebug=%08x\n", printme
.data
.debug_vaddr
);
662 printf_filtered ("\t\toffset=%s\n", phex (printme
.data
.offset
, 8));
664 printf_filtered ("\tdev=0x%x\n", printme
.dev
);
665 printf_filtered ("\tino=0x%x\n", (unsigned int) printme
.ino
);
671 /* Print status information about what we're accessing. */
673 nto_procfs_target::files_info ()
675 struct inferior
*inf
= current_inferior ();
677 printf_unfiltered ("\tUsing the running image of %s %s via %s.\n",
678 inf
->attach_flag
? "attached" : "child",
679 target_pid_to_str (inferior_ptid
),
680 (nodestr
!= NULL
) ? nodestr
: "local node");
683 /* Target to_pid_to_exec_file implementation. */
686 nto_procfs_target::pid_to_exec_file (const int pid
)
689 static char proc_path
[PATH_MAX
];
692 /* Read exe file name. */
693 snprintf (proc_path
, sizeof (proc_path
), "%s/proc/%d/exefile",
694 (nodestr
!= NULL
) ? nodestr
: "", pid
);
695 proc_fd
= open (proc_path
, O_RDONLY
);
699 rd
= read (proc_fd
, proc_path
, sizeof (proc_path
) - 1);
706 proc_path
[rd
] = '\0';
710 /* Attach to process PID, then initialize for debugging it. */
712 nto_procfs_target::attach (const char *args
, int from_tty
)
716 struct inferior
*inf
;
718 pid
= parse_pid_to_attach (args
);
720 if (pid
== getpid ())
721 error (_("Attaching GDB to itself is not a good idea..."));
725 exec_file
= (char *) get_exec_file (0);
728 printf_unfiltered ("Attaching to program `%s', %s\n", exec_file
,
729 target_pid_to_str (pid_to_ptid (pid
)));
731 printf_unfiltered ("Attaching to %s\n",
732 target_pid_to_str (pid_to_ptid (pid
)));
734 gdb_flush (gdb_stdout
);
736 inferior_ptid
= do_attach (pid_to_ptid (pid
));
737 inf
= current_inferior ();
738 inferior_appeared (inf
, pid
);
739 inf
->attach_flag
= 1;
741 if (!target_is_pushed (ops
))
744 procfs_update_thread_list (ops
);
748 nto_procfs_target::post_attach (pid_t pid
)
751 solib_create_inferior_hook (0);
755 do_attach (ptid_t ptid
)
757 procfs_status status
;
758 struct sigevent event
;
761 snprintf (path
, PATH_MAX
- 1, "%s%s/%d/as",
762 (nodestr
!= NULL
) ? nodestr
: "", "/proc", ptid_get_pid (ptid
));
763 ctl_fd
= open (path
, O_RDWR
);
765 error (_("Couldn't open proc file %s, error %d (%s)"), path
, errno
,
766 safe_strerror (errno
));
767 if (devctl (ctl_fd
, DCMD_PROC_STOP
, &status
, sizeof (status
), 0) != EOK
)
768 error (_("Couldn't stop process"));
770 /* Define a sigevent for process stopped notification. */
771 event
.sigev_notify
= SIGEV_SIGNAL_THREAD
;
772 event
.sigev_signo
= SIGUSR1
;
773 event
.sigev_code
= 0;
774 event
.sigev_value
.sival_ptr
= NULL
;
775 event
.sigev_priority
= -1;
776 devctl (ctl_fd
, DCMD_PROC_EVENT
, &event
, sizeof (event
), 0);
778 if (devctl (ctl_fd
, DCMD_PROC_STATUS
, &status
, sizeof (status
), 0) == EOK
779 && status
.flags
& _DEBUG_FLAG_STOPPED
)
780 SignalKill (nto_node (), ptid_get_pid (ptid
), 0, SIGCONT
, 0, 0);
781 nto_init_solib_absolute_prefix ();
782 return ptid_build (ptid_get_pid (ptid
), 0, status
.tid
);
785 /* Ask the user what to do when an interrupt is received. */
787 interrupt_query (void)
789 if (query (_("Interrupted while waiting for the program.\n\
790 Give up (and stop debugging it)? ")))
792 target_mourn_inferior (inferior_ptid
);
797 /* The user typed ^C twice. */
799 nto_handle_sigint_twice (int signo
)
801 signal (signo
, ofunc
);
803 signal (signo
, nto_handle_sigint_twice
);
807 nto_handle_sigint (int signo
)
809 /* If this doesn't work, try more severe steps. */
810 signal (signo
, nto_handle_sigint_twice
);
816 nto_procfs_target::wait (ptid_t ptid
, struct target_waitstatus
*ourstatus
,
821 procfs_status status
;
822 static int exit_signo
= 0; /* To track signals that cause termination. */
824 ourstatus
->kind
= TARGET_WAITKIND_SPURIOUS
;
826 if (ptid_equal (inferior_ptid
, null_ptid
))
828 ourstatus
->kind
= TARGET_WAITKIND_STOPPED
;
829 ourstatus
->value
.sig
= GDB_SIGNAL_0
;
835 sigaddset (&set
, SIGUSR1
);
837 devctl (ctl_fd
, DCMD_PROC_STATUS
, &status
, sizeof (status
), 0);
838 while (!(status
.flags
& _DEBUG_FLAG_ISTOP
))
840 ofunc
= signal (SIGINT
, nto_handle_sigint
);
841 sigwaitinfo (&set
, &info
);
842 signal (SIGINT
, ofunc
);
843 devctl (ctl_fd
, DCMD_PROC_STATUS
, &status
, sizeof (status
), 0);
846 nto_inferior_data (NULL
)->stopped_flags
= status
.flags
;
847 nto_inferior_data (NULL
)->stopped_pc
= status
.ip
;
849 if (status
.flags
& _DEBUG_FLAG_SSTEP
)
851 ourstatus
->kind
= TARGET_WAITKIND_STOPPED
;
852 ourstatus
->value
.sig
= GDB_SIGNAL_TRAP
;
854 /* Was it a breakpoint? */
855 else if (status
.flags
& _DEBUG_FLAG_TRACE
)
857 ourstatus
->kind
= TARGET_WAITKIND_STOPPED
;
858 ourstatus
->value
.sig
= GDB_SIGNAL_TRAP
;
860 else if (status
.flags
& _DEBUG_FLAG_ISTOP
)
864 case _DEBUG_WHY_SIGNALLED
:
865 ourstatus
->kind
= TARGET_WAITKIND_STOPPED
;
866 ourstatus
->value
.sig
=
867 gdb_signal_from_host (status
.info
.si_signo
);
870 case _DEBUG_WHY_FAULTED
:
871 ourstatus
->kind
= TARGET_WAITKIND_STOPPED
;
872 if (status
.info
.si_signo
== SIGTRAP
)
874 ourstatus
->value
.sig
= 0;
879 ourstatus
->value
.sig
=
880 gdb_signal_from_host (status
.info
.si_signo
);
881 exit_signo
= ourstatus
->value
.sig
;
885 case _DEBUG_WHY_TERMINATED
:
889 waitpid (ptid_get_pid (inferior_ptid
), &waitval
, WNOHANG
);
892 /* Abnormal death. */
893 ourstatus
->kind
= TARGET_WAITKIND_SIGNALLED
;
894 ourstatus
->value
.sig
= exit_signo
;
899 ourstatus
->kind
= TARGET_WAITKIND_EXITED
;
900 ourstatus
->value
.integer
= WEXITSTATUS (waitval
);
906 case _DEBUG_WHY_REQUESTED
:
907 /* We are assuming a requested stop is due to a SIGINT. */
908 ourstatus
->kind
= TARGET_WAITKIND_STOPPED
;
909 ourstatus
->value
.sig
= GDB_SIGNAL_INT
;
915 return ptid_build (status
.pid
, 0, status
.tid
);
918 /* Read the current values of the inferior's registers, both the
919 general register set and floating point registers (if supported)
920 and update gdb's idea of their current values. */
922 nto_procfs_target::fetch_registers (struct regcache
*regcache
, int regno
)
928 procfs_altreg altreg
;
933 procfs_set_thread (regcache_get_ptid (regcache
));
934 if (devctl (ctl_fd
, DCMD_PROC_GETGREG
, ®
, sizeof (reg
), ®size
) == EOK
)
935 nto_supply_gregset (regcache
, (char *) ®
.greg
);
936 if (devctl (ctl_fd
, DCMD_PROC_GETFPREG
, ®
, sizeof (reg
), ®size
)
938 nto_supply_fpregset (regcache
, (char *) ®
.fpreg
);
939 if (devctl (ctl_fd
, DCMD_PROC_GETALTREG
, ®
, sizeof (reg
), ®size
)
941 nto_supply_altregset (regcache
, (char *) ®
.altreg
);
944 /* Helper for procfs_xfer_partial that handles memory transfers.
945 Arguments are like target_xfer_partial. */
947 static enum target_xfer_status
948 procfs_xfer_memory (gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
949 ULONGEST memaddr
, ULONGEST len
, ULONGEST
*xfered_len
)
953 if (lseek (ctl_fd
, (off_t
) memaddr
, SEEK_SET
) != (off_t
) memaddr
)
954 return TARGET_XFER_E_IO
;
956 if (writebuf
!= NULL
)
957 nbytes
= write (ctl_fd
, writebuf
, len
);
959 nbytes
= read (ctl_fd
, readbuf
, len
);
961 return TARGET_XFER_E_IO
;
962 *xfered_len
= nbytes
;
963 return TARGET_XFER_OK
;
966 /* Target to_xfer_partial implementation. */
968 enum target_xfer_status
969 nto_procfs_target::xfer_partial (enum target_object object
,
970 const char *annex
, gdb_byte
*readbuf
,
971 const gdb_byte
*writebuf
, ULONGEST offset
,
972 ULONGEST len
, ULONGEST
*xfered_len
)
976 case TARGET_OBJECT_MEMORY
:
977 return procfs_xfer_memory (readbuf
, writebuf
, offset
, len
, xfered_len
);
978 case TARGET_OBJECT_AUXV
:
982 CORE_ADDR initial_stack
;
983 debug_process_t procinfo
;
984 /* For 32-bit architecture, size of auxv_t is 8 bytes. */
985 const unsigned int sizeof_auxv_t
= sizeof (auxv_t
);
986 const unsigned int sizeof_tempbuf
= 20 * sizeof_auxv_t
;
988 gdb_byte
*const tempbuf
= alloca (sizeof_tempbuf
);
991 return TARGET_XFER_E_IO
;
993 err
= devctl (ctl_fd
, DCMD_PROC_INFO
, &procinfo
,
996 return TARGET_XFER_E_IO
;
998 initial_stack
= procinfo
.initial_stack
;
1000 /* procfs is always 'self-hosted', no byte-order manipulation. */
1001 tempread
= nto_read_auxv_from_initial_stack (initial_stack
, tempbuf
,
1004 tempread
= std::min (tempread
, len
) - offset
;
1005 memcpy (readbuf
, tempbuf
+ offset
, tempread
);
1006 *xfered_len
= tempread
;
1007 return tempread
? TARGET_XFER_OK
: TARGET_XFER_EOF
;
1011 return this->beneath
->xfer_partial (object
, annex
,
1012 readbuf
, writebuf
, offset
, len
,
1017 /* Take a program previously attached to and detaches it.
1018 The program resumes execution and will no longer stop
1019 on signals, etc. We'd better not have left any breakpoints
1020 in the program or it'll die when it hits one. */
1022 nto_procfs_target::detach (inferior
*inf
, int from_tty
)
1026 target_announce_detach ();
1029 SignalKill (nto_node (), ptid_get_pid (inferior_ptid
), 0, 0, 0, 0);
1034 pid
= ptid_get_pid (inferior_ptid
);
1035 inferior_ptid
= null_ptid
;
1036 detach_inferior (pid
);
1037 init_thread_list ();
1038 inf_child_maybe_unpush_target (ops
);
1042 procfs_breakpoint (CORE_ADDR addr
, int type
, int size
)
1049 errno
= devctl (ctl_fd
, DCMD_PROC_BREAK
, &brk
, sizeof (brk
), 0);
1056 nto_procfs_target::insert_breakpoint (struct gdbarch
*gdbarch
,
1057 struct bp_target_info
*bp_tgt
)
1059 bp_tgt
->placed_address
= bp_tgt
->reqstd_address
;
1060 return procfs_breakpoint (bp_tgt
->placed_address
, _DEBUG_BREAK_EXEC
, 0);
1064 nto_procfs_target::remove_breakpoint (struct gdbarch
*gdbarch
,
1065 struct bp_target_info
*bp_tgt
,
1066 enum remove_bp_reason reason
)
1068 return procfs_breakpoint (bp_tgt
->placed_address
, _DEBUG_BREAK_EXEC
, -1);
1072 nto_procfs_target::insert_hw_breakpoint (struct gdbarch
*gdbarch
,
1073 struct bp_target_info
*bp_tgt
)
1075 bp_tgt
->placed_address
= bp_tgt
->reqstd_address
;
1076 return procfs_breakpoint (bp_tgt
->placed_address
,
1077 _DEBUG_BREAK_EXEC
| _DEBUG_BREAK_HW
, 0);
1081 nto_procfs_target::remove_hw_breakpoint (struct gdbarch
*gdbarch
,
1082 struct bp_target_info
*bp_tgt
)
1084 return procfs_breakpoint (bp_tgt
->placed_address
,
1085 _DEBUG_BREAK_EXEC
| _DEBUG_BREAK_HW
, -1);
1089 nto_procfs_target::resume (ptid_t ptid
, int step
, enum gdb_signal signo
)
1092 procfs_status status
;
1093 sigset_t
*run_fault
= (sigset_t
*) (void *) &run
.fault
;
1095 if (ptid_equal (inferior_ptid
, null_ptid
))
1098 procfs_set_thread (ptid_equal (ptid
, minus_one_ptid
) ? inferior_ptid
:
1101 run
.flags
= _DEBUG_RUN_FAULT
| _DEBUG_RUN_TRACE
;
1103 run
.flags
|= _DEBUG_RUN_STEP
;
1105 sigemptyset (run_fault
);
1106 sigaddset (run_fault
, FLTBPT
);
1107 sigaddset (run_fault
, FLTTRACE
);
1108 sigaddset (run_fault
, FLTILL
);
1109 sigaddset (run_fault
, FLTPRIV
);
1110 sigaddset (run_fault
, FLTBOUNDS
);
1111 sigaddset (run_fault
, FLTIOVF
);
1112 sigaddset (run_fault
, FLTIZDIV
);
1113 sigaddset (run_fault
, FLTFPE
);
1114 /* Peter V will be changing this at some point. */
1115 sigaddset (run_fault
, FLTPAGE
);
1117 run
.flags
|= _DEBUG_RUN_ARM
;
1119 signal_to_pass
= gdb_signal_to_host (signo
);
1123 devctl (ctl_fd
, DCMD_PROC_STATUS
, &status
, sizeof (status
), 0);
1124 signal_to_pass
= gdb_signal_to_host (signo
);
1125 if (status
.why
& (_DEBUG_WHY_SIGNALLED
| _DEBUG_WHY_FAULTED
))
1127 if (signal_to_pass
!= status
.info
.si_signo
)
1129 SignalKill (nto_node (), ptid_get_pid (inferior_ptid
), 0,
1130 signal_to_pass
, 0, 0);
1131 run
.flags
|= _DEBUG_RUN_CLRFLT
| _DEBUG_RUN_CLRSIG
;
1133 else /* Let it kill the program without telling us. */
1134 sigdelset (&run
.trace
, signal_to_pass
);
1138 run
.flags
|= _DEBUG_RUN_CLRSIG
| _DEBUG_RUN_CLRFLT
;
1140 errno
= devctl (ctl_fd
, DCMD_PROC_RUN
, &run
, sizeof (run
), 0);
1143 perror (_("run error!\n"));
1149 nto_procfs_target::mourn_inferior ()
1151 if (!ptid_equal (inferior_ptid
, null_ptid
))
1153 SignalKill (nto_node (), ptid_get_pid (inferior_ptid
), 0, SIGKILL
, 0, 0);
1156 inferior_ptid
= null_ptid
;
1157 init_thread_list ();
1158 inf_child_mourn_inferior (ops
);
1161 /* This function breaks up an argument string into an argument
1162 vector suitable for passing to execvp().
1163 E.g., on "run a b c d" this routine would get as input
1164 the string "a b c d", and as output it would fill in argv with
1165 the four arguments "a", "b", "c", "d". The only additional
1166 functionality is simple quoting. The gdb command:
1168 will fill in argv with the three args "a", "b c d", "e". */
1170 breakup_args (char *scratch
, char **argv
)
1172 char *pp
, *cp
= scratch
;
1177 /* Scan past leading separators. */
1179 while (*cp
== ' ' || *cp
== '\t' || *cp
== '\n')
1182 /* Break if at end of string. */
1190 quoting
= strchr (cp
, '"') ? 1 : 0;
1195 /* Scan for next arg separator. */
1198 cp
= strchr (pp
, '"');
1199 if ((cp
== NULL
) || (!quoting
))
1200 cp
= strchr (pp
, ' ');
1202 cp
= strchr (pp
, '\t');
1204 cp
= strchr (pp
, '\n');
1206 /* No separators => end of string => break. */
1213 /* Replace the separator with a terminator. */
1217 /* Execv requires a null-terminated arg vector. */
1222 nto_procfs_target::create_inferior (const char *exec_file
,
1223 const std::string
&allargs
,
1224 char **env
, int from_tty
)
1226 struct inheritance inherit
;
1230 const char *in
= "", *out
= "", *err
= "";
1233 const char *inferior_io_terminal
= get_inferior_io_terminal ();
1234 struct inferior
*inf
;
1236 argv
= xmalloc ((allargs
.size () / (unsigned) 2 + 2) *
1238 argv
[0] = get_exec_file (1);
1242 argv
[0] = exec_file
;
1247 args
= xstrdup (allargs
.c_str ());
1248 breakup_args (args
, (exec_file
!= NULL
) ? &argv
[1] : &argv
[0]);
1250 argv
= nto_parse_redirection (argv
, &in
, &out
, &err
);
1252 fds
[0] = STDIN_FILENO
;
1253 fds
[1] = STDOUT_FILENO
;
1254 fds
[2] = STDERR_FILENO
;
1256 /* If the user specified I/O via gdb's --tty= arg, use it, but only
1257 if the i/o is not also being specified via redirection. */
1258 if (inferior_io_terminal
)
1261 in
= inferior_io_terminal
;
1263 out
= inferior_io_terminal
;
1265 err
= inferior_io_terminal
;
1270 fd
= open (in
, O_RDONLY
);
1278 fd
= open (out
, O_WRONLY
);
1286 fd
= open (err
, O_WRONLY
);
1293 /* Clear any pending SIGUSR1's but keep the behavior the same. */
1294 signal (SIGUSR1
, signal (SIGUSR1
, SIG_IGN
));
1297 sigaddset (&set
, SIGUSR1
);
1298 sigprocmask (SIG_UNBLOCK
, &set
, NULL
);
1300 memset (&inherit
, 0, sizeof (inherit
));
1302 if (ND_NODE_CMP (nto_procfs_node
, ND_LOCAL_NODE
) != 0)
1304 inherit
.nd
= nto_node ();
1305 inherit
.flags
|= SPAWN_SETND
;
1306 inherit
.flags
&= ~SPAWN_EXEC
;
1308 inherit
.flags
|= SPAWN_SETGROUP
| SPAWN_HOLD
;
1309 inherit
.pgroup
= SPAWN_NEWPGROUP
;
1310 pid
= spawnp (argv
[0], 3, fds
, &inherit
, argv
,
1311 ND_NODE_CMP (nto_procfs_node
, ND_LOCAL_NODE
) == 0 ? env
: 0);
1314 sigprocmask (SIG_BLOCK
, &set
, NULL
);
1317 error (_("Error spawning %s: %d (%s)"), argv
[0], errno
,
1318 safe_strerror (errno
));
1320 if (fds
[0] != STDIN_FILENO
)
1322 if (fds
[1] != STDOUT_FILENO
)
1324 if (fds
[2] != STDERR_FILENO
)
1327 inferior_ptid
= do_attach (pid_to_ptid (pid
));
1328 procfs_update_thread_list (ops
);
1330 inf
= current_inferior ();
1331 inferior_appeared (inf
, pid
);
1332 inf
->attach_flag
= 0;
1334 flags
= _DEBUG_FLAG_KLC
; /* Kill-on-Last-Close flag. */
1335 errn
= devctl (ctl_fd
, DCMD_PROC_SET_FLAG
, &flags
, sizeof (flags
), 0);
1338 /* FIXME: expected warning? */
1339 /* warning( "Failed to set Kill-on-Last-Close flag: errno = %d(%s)\n",
1340 errn, strerror(errn) ); */
1342 if (!target_is_pushed (ops
))
1344 target_terminal::init ();
1346 if (exec_bfd
!= NULL
1347 || (symfile_objfile
!= NULL
&& symfile_objfile
->obfd
!= NULL
))
1348 solib_create_inferior_hook (0);
1352 nto_procfs_target::interrupt ()
1354 devctl (ctl_fd
, DCMD_PROC_STOP
, NULL
, 0, 0);
1358 nto_procfs_target::kill ()
1360 target_mourn_inferior (inferior_ptid
);
1363 /* Fill buf with regset and return devctl cmd to do the setting. Return
1364 -1 if we fail to get the regset. Store size of regset in regsize. */
1366 get_regset (int regset
, char *buf
, int bufsize
, int *regsize
)
1368 int dev_get
, dev_set
;
1371 case NTO_REG_GENERAL
:
1372 dev_get
= DCMD_PROC_GETGREG
;
1373 dev_set
= DCMD_PROC_SETGREG
;
1377 dev_get
= DCMD_PROC_GETFPREG
;
1378 dev_set
= DCMD_PROC_SETFPREG
;
1382 dev_get
= DCMD_PROC_GETALTREG
;
1383 dev_set
= DCMD_PROC_SETALTREG
;
1386 case NTO_REG_SYSTEM
:
1390 if (devctl (ctl_fd
, dev_get
, buf
, bufsize
, regsize
) != EOK
)
1397 nto_procfs_target::store_registers (struct regcache
*regcache
, int regno
)
1403 procfs_altreg altreg
;
1407 int len
, regset
, regsize
, dev_set
, err
;
1409 ptid_t ptid
= regcache_get_ptid (regcache
);
1411 if (ptid_equal (ptid
, null_ptid
))
1413 procfs_set_thread (ptid
);
1417 for (regset
= NTO_REG_GENERAL
; regset
< NTO_REG_END
; regset
++)
1419 dev_set
= get_regset (regset
, (char *) ®
,
1420 sizeof (reg
), ®size
);
1424 if (nto_regset_fill (regcache
, regset
, (char *) ®
) == -1)
1427 err
= devctl (ctl_fd
, dev_set
, ®
, regsize
, 0);
1429 fprintf_unfiltered (gdb_stderr
,
1430 "Warning unable to write regset %d: %s\n",
1431 regno
, safe_strerror (err
));
1436 regset
= nto_regset_id (regno
);
1440 dev_set
= get_regset (regset
, (char *) ®
, sizeof (reg
), ®size
);
1444 len
= nto_register_area (regcache
->arch (),
1445 regno
, regset
, &off
);
1450 regcache_raw_collect (regcache
, regno
, (char *) ®
+ off
);
1452 err
= devctl (ctl_fd
, dev_set
, ®
, regsize
, 0);
1454 fprintf_unfiltered (gdb_stderr
,
1455 "Warning unable to write regset %d: %s\n", regno
,
1456 safe_strerror (err
));
1460 /* Set list of signals to be handled in the target. */
1463 nto_procfs_target::pass_signals (int numsigs
, unsigned char *pass_signals
)
1467 sigfillset (&run
.trace
);
1469 for (signo
= 1; signo
< NSIG
; signo
++)
1471 int target_signo
= gdb_signal_from_host (signo
);
1472 if (target_signo
< numsigs
&& pass_signals
[target_signo
])
1473 sigdelset (&run
.trace
, signo
);
1478 nto_procfs_target::pid_to_str (ptid_t ptid
)
1480 static char buf
[1024];
1482 struct tidinfo
*tip
;
1484 pid
= ptid_get_pid (ptid
);
1485 tid
= ptid_get_tid (ptid
);
1487 n
= snprintf (buf
, 1023, "process %d", pid
);
1490 tip
= procfs_thread_info (pid
, tid
);
1492 snprintf (&buf
[n
], 1023, " (state = 0x%02x)", tip
->state
);
1498 /* to_can_run implementation for "target procfs". Note this really
1499 means "can this target be the default run target", which there can
1500 be only one, and we make it be "target native" like other ports.
1501 "target procfs <node>" wouldn't make sense as default run target, as
1505 nto_procfs_target::can_run ()
1510 /* "target procfs". */
1511 static nto_procfs_target_procfs nto_procfs_ops
;
1513 /* "target native". */
1514 static nto_procfs_target_native nto_native_ops
;
1516 /* Create the "native" and "procfs" targets. */
1519 init_procfs_targets (void)
1521 /* Register "target native". This is the default run target. */
1522 add_target (&nto_native_ops
);
1524 /* Register "target procfs <node>". */
1525 add_target (&nto_procfs_ops
);
1528 #define OSTYPE_NTO 1
1531 _initialize_procfs (void)
1535 init_procfs_targets ();
1537 /* We use SIGUSR1 to gain control after we block waiting for a process.
1538 We use sigwaitevent to wait. */
1540 sigaddset (&set
, SIGUSR1
);
1541 sigprocmask (SIG_BLOCK
, &set
, NULL
);
1543 /* Initially, make sure all signals are reported. */
1544 sigfillset (&run
.trace
);
1546 /* Stuff some information. */
1547 nto_cpuinfo_flags
= SYSPAGE_ENTRY (cpuinfo
)->flags
;
1548 nto_cpuinfo_valid
= 1;
1550 add_info ("pidlist", procfs_pidlist
, _("pidlist"));
1551 add_info ("meminfo", procfs_meminfo
, _("memory information"));
1553 nto_is_nto_target
= procfs_is_nto_target
;
1558 procfs_hw_watchpoint (int addr
, int len
, enum target_hw_bp_type type
)
1565 brk
.type
= _DEBUG_BREAK_RD
;
1568 brk
.type
= _DEBUG_BREAK_RW
;
1570 default: /* Modify. */
1571 /* FIXME: brk.type = _DEBUG_BREAK_RWM gives EINVAL for some reason. */
1572 brk
.type
= _DEBUG_BREAK_RW
;
1574 brk
.type
|= _DEBUG_BREAK_HW
; /* Always ask for HW. */
1578 errno
= devctl (ctl_fd
, DCMD_PROC_BREAK
, &brk
, sizeof (brk
), 0);
1581 perror (_("Failed to set hardware watchpoint"));
1588 nto_procfs_target::can_use_hw_breakpoint (enum bptype type
,
1589 int cnt
, int othertype
)
1595 nto_procfs_target::remove_hw_watchpoint (CORE_ADDR addr
, int len
,
1596 enum target_hw_bp_type type
,
1597 struct expression
*cond
)
1599 return procfs_hw_watchpoint (addr
, -1, type
);
1603 nto_procfs_target::insert_hw_watchpoint (CORE_ADDR addr
, int len
,
1604 enum target_hw_bp_type type
,
1605 struct expression
*cond
)
1607 return procfs_hw_watchpoint (addr
, len
, type
);
1611 nto_procfs_target::stopped_by_watchpoint ()
1613 /* NOTE: nto_stopped_by_watchpoint will be called ONLY while we are
1614 stopped due to a SIGTRAP. This assumes gdb works in 'all-stop' mode;
1615 future gdb versions will likely run in 'non-stop' mode in which case
1616 we will have to store/examine statuses per thread in question.
1617 Until then, this will work fine. */
1619 struct inferior
*inf
= current_inferior ();
1620 struct nto_inferior_data
*inf_data
;
1622 gdb_assert (inf
!= NULL
);
1624 inf_data
= nto_inferior_data (inf
);
1626 return inf_data
->stopped_flags
1627 & (_DEBUG_FLAG_TRACE_RD
1628 | _DEBUG_FLAG_TRACE_WR
1629 | _DEBUG_FLAG_TRACE_MODIFY
);