1 /* Copyright (C) 2020-2024 Free Software Foundation, Inc.
3 This file is part of GDB.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19 #include "netbsd-low.h"
20 #include "nat/netbsd-nat.h"
22 #include <sys/param.h>
23 #include <sys/types.h>
25 #include <sys/ptrace.h>
26 #include <sys/sysctl.h>
34 #include <type_traits>
36 #include "gdbsupport/eintr.h"
37 #include "gdbsupport/gdb_wait.h"
38 #include "gdbsupport/filestuff.h"
39 #include "gdbsupport/common-inferior.h"
40 #include "nat/fork-inferior.h"
43 int using_threads
= 1;
45 /* Callback used by fork_inferior to start tracing the inferior. */
50 /* Switch child to its own process group so that signals won't
51 directly affect GDBserver. */
52 if (setpgid (0, 0) < 0)
53 trace_start_error_with_name (("setpgid"));
55 if (ptrace (PT_TRACE_ME
, 0, nullptr, 0) < 0)
56 trace_start_error_with_name (("ptrace"));
58 /* If GDBserver is connected to gdb via stdio, redirect the inferior's
59 stdout to stderr so that inferior i/o doesn't corrupt the connection.
60 Also, redirect stdin to /dev/null. */
61 if (remote_connection_is_stdio ())
64 trace_start_error_with_name (("close"));
65 if (open ("/dev/null", O_RDONLY
) < 0)
66 trace_start_error_with_name (("open"));
68 trace_start_error_with_name (("dup2"));
69 if (write (2, "stdin/stdout redirected\n",
70 sizeof ("stdin/stdout redirected\n") - 1) < 0)
77 /* Implement the create_inferior method of the target_ops vector. */
80 netbsd_process_target::create_inferior (const char *program
,
81 const std::string
&program_args
)
83 pid_t pid
= fork_inferior (program
, program_args
.c_str (),
84 get_environ ()->envp (), netbsd_ptrace_fun
,
85 nullptr, nullptr, nullptr, nullptr);
89 post_fork_inferior (pid
, program
);
94 /* Implement the post_create_inferior target_ops method. */
97 netbsd_process_target::post_create_inferior ()
99 pid_t pid
= current_process ()->pid
;
100 netbsd_nat::enable_proc_events (pid
);
105 /* Implement the attach target_ops method. */
108 netbsd_process_target::attach (unsigned long pid
)
114 /* Returns true if GDB is interested in any child syscalls. */
117 gdb_catching_syscalls_p (pid_t pid
)
119 struct process_info
*proc
= find_process_pid (pid
);
120 return !proc
->syscalls_to_catch
.empty ();
123 /* Implement the resume target_ops method. */
126 netbsd_process_target::resume (struct thread_resume
*resume_info
, size_t n
)
128 ptid_t resume_ptid
= resume_info
[0].thread
;
129 const int signal
= resume_info
[0].sig
;
130 const bool step
= resume_info
[0].kind
== resume_step
;
132 if (resume_ptid
== minus_one_ptid
)
133 resume_ptid
= current_thread
->id
;
135 const pid_t pid
= resume_ptid
.pid ();
136 const lwpid_t lwp
= resume_ptid
.lwp ();
137 regcache_invalidate_pid (pid
);
144 if (ptid
.lwp () == lwp
|| n
!= 1)
146 if (ptrace (PT_SETSTEP
, pid
, NULL
, ptid
.lwp ()) == -1)
147 perror_with_name (("ptrace"));
148 if (ptrace (PT_RESUME
, pid
, NULL
, ptid
.lwp ()) == -1)
149 perror_with_name (("ptrace"));
153 if (ptrace (PT_CLEARSTEP
, pid
, NULL
, ptid
.lwp ()) == -1)
154 perror_with_name (("ptrace"));
155 if (ptrace (PT_SUSPEND
, pid
, NULL
, ptid
.lwp ()) == -1)
156 perror_with_name (("ptrace"));
161 if (ptrace (PT_CLEARSTEP
, pid
, NULL
, ptid
.lwp ()) == -1)
162 perror_with_name (("ptrace"));
163 if (ptrace (PT_RESUME
, pid
, NULL
, ptid
.lwp ()) == -1)
164 perror_with_name (("ptrace"));
168 netbsd_nat::for_each_thread (pid
, fn
);
170 int request
= gdb_catching_syscalls_p (pid
) ? PT_CONTINUE
: PT_SYSCALL
;
173 ptrace (request
, pid
, (void *)1, signal
);
175 perror_with_name (("ptrace"));
178 /* Returns true if GDB is interested in the reported SYSNO syscall. */
181 netbsd_catch_this_syscall (int sysno
)
183 struct process_info
*proc
= current_process ();
185 if (proc
->syscalls_to_catch
.empty ())
188 if (proc
->syscalls_to_catch
[0] == ANY_SYSCALL
)
191 for (int iter
: proc
->syscalls_to_catch
)
198 /* Helper function for child_wait and the derivatives of child_wait.
199 HOSTSTATUS is the waitstatus from wait() or the equivalent; store our
200 translation of that in OURSTATUS. */
203 netbsd_store_waitstatus (struct target_waitstatus
*ourstatus
, int hoststatus
)
205 if (WIFEXITED (hoststatus
))
206 ourstatus
->set_exited (WEXITSTATUS (hoststatus
));
207 else if (!WIFSTOPPED (hoststatus
))
208 ourstatus
->set_signalled (gdb_signal_from_host (WTERMSIG (hoststatus
)));
210 ourstatus
->set_stopped (gdb_signal_from_host (WSTOPSIG (hoststatus
)));
213 /* Implement a safe wrapper around waitpid(). */
216 netbsd_waitpid (ptid_t ptid
, struct target_waitstatus
*ourstatus
,
217 target_wait_flags target_options
)
220 int options
= (target_options
& TARGET_WNOHANG
) ? WNOHANG
: 0;
223 = gdb::waitpid (ptid
.pid (), &status
, options
);
226 perror_with_name (_("Child process unexpectedly missing"));
228 netbsd_store_waitstatus (ourstatus
, status
);
233 /* Implement the wait target_ops method.
235 Wait for the child specified by PTID to do something. Return the
236 process ID of the child, or MINUS_ONE_PTID in case of error; store
237 the status in *OURSTATUS. */
240 netbsd_wait (ptid_t ptid
, struct target_waitstatus
*ourstatus
,
241 target_wait_flags target_options
)
243 pid_t pid
= netbsd_waitpid (ptid
, ourstatus
, target_options
);
244 ptid_t wptid
= ptid_t (pid
);
248 gdb_assert (target_options
& TARGET_WNOHANG
);
249 ourstatus
->set_ignore ();
253 gdb_assert (pid
!= -1);
255 /* If the child stopped, keep investigating its status. */
256 if (ourstatus
->kind () != TARGET_WAITKIND_STOPPED
)
259 /* Extract the event and thread that received a signal. */
260 ptrace_siginfo_t psi
;
261 if (ptrace (PT_GET_SIGINFO
, pid
, &psi
, sizeof (psi
)) == -1)
262 perror_with_name (("ptrace"));
264 /* Pick child's siginfo_t. */
265 siginfo_t
*si
= &psi
.psi_siginfo
;
267 lwpid_t lwp
= psi
.psi_lwpid
;
269 int signo
= si
->si_signo
;
270 const int code
= si
->si_code
;
272 /* Construct PTID with a specified thread that received the event.
273 If a signal was targeted to the whole process, lwp is 0. */
274 wptid
= ptid_t (pid
, lwp
, 0);
276 /* Bail out on non-debugger oriented signals. */
277 if (signo
!= SIGTRAP
)
280 /* Stop examining non-debugger oriented SIGTRAP codes. */
281 if (code
<= SI_USER
|| code
== SI_NOINFO
)
284 /* Process state for threading events. */
285 ptrace_state_t pst
= {};
286 if (code
== TRAP_LWP
)
287 if (ptrace (PT_GET_PROCESS_STATE
, pid
, &pst
, sizeof (pst
)) == -1)
288 perror_with_name (("ptrace"));
290 if (code
== TRAP_LWP
&& pst
.pe_report_event
== PTRACE_LWP_EXIT
)
292 /* If GDB attaches to a multi-threaded process, exiting
293 threads might be skipped during post_attach that
294 have not yet reported their PTRACE_LWP_EXIT event.
295 Ignore exited events for an unknown LWP. */
296 thread_info
*thr
= find_thread_ptid (wptid
);
298 ourstatus
->set_spurious ();
301 /* NetBSD does not store an LWP exit status. */
302 ourstatus
->set_thread_exited (0);
304 thr
->process ()->remove_thread (thr
);
309 if (find_thread_ptid (ptid_t (pid
)))
310 switch_to_thread (find_thread_ptid (wptid
));
312 if (code
== TRAP_LWP
&& pst
.pe_report_event
== PTRACE_LWP_CREATE
)
314 /* If GDB attaches to a multi-threaded process, newborn
315 threads might be added by nbsd_add_threads that have
316 not yet reported their PTRACE_LWP_CREATE event. Ignore
317 born events for an already-known LWP. */
318 if (find_thread_ptid (wptid
))
319 ourstatus
->set_spurious ();
322 find_process_pid (wptid
.pid ())->add_thread (wptid
, nullptr);
323 ourstatus
->set_thread_created ();
328 if (code
== TRAP_EXEC
)
331 (make_unique_xstrdup (netbsd_nat::pid_to_exec_file (pid
)));
335 if (code
== TRAP_TRACE
)
338 if (code
== TRAP_SCE
|| code
== TRAP_SCX
)
340 int sysnum
= si
->si_sysnum
;
342 if (!netbsd_catch_this_syscall(sysnum
))
344 /* If the core isn't interested in this event, ignore it. */
345 ourstatus
->set_spurious ();
349 if (code
== TRAP_SCE
)
350 ourstatus
->set_syscall_entry (sysnum
);
352 ourstatus
->set_syscall_return (sysnum
);
357 if (code
== TRAP_BRKPT
)
359 #ifdef PTRACE_BREAKPOINT_ADJ
362 ptrace (PT_GETREGS
, pid
, &r
, psi
.psi_lwpid
);
363 pc
= PTRACE_REG_PC (&r
);
364 PTRACE_REG_SET_PC (&r
, pc
- PTRACE_BREAKPOINT_ADJ
);
365 ptrace (PT_SETREGS
, pid
, &r
, psi
.psi_lwpid
);
370 /* Unclassified SIGTRAP event. */
371 ourstatus
->set_spurious ();
375 /* Implement the wait target_ops method. */
378 netbsd_process_target::wait (ptid_t ptid
, struct target_waitstatus
*ourstatus
,
379 target_wait_flags target_options
)
383 ptid_t wptid
= netbsd_wait (ptid
, ourstatus
, target_options
);
385 /* Register thread in the gdbcore if a thread was not reported earlier.
386 This is required after ::create_inferior, when the gdbcore does not
387 know about the first internal thread.
388 This may also happen on attach, when an event is registered on a thread
389 that was not fully initialized during the attach stage. */
390 if (wptid
.lwp () != 0 && !find_thread_ptid (wptid
)
391 && ourstatus
->kind () != TARGET_WAITKIND_THREAD_EXITED
)
392 find_process_pid (wptid
.pid ())->add_thread (wptid
, nullptr);
394 switch (ourstatus
->kind ())
396 case TARGET_WAITKIND_EXITED
:
397 case TARGET_WAITKIND_STOPPED
:
398 case TARGET_WAITKIND_SIGNALLED
:
399 case TARGET_WAITKIND_FORKED
:
400 case TARGET_WAITKIND_VFORKED
:
401 case TARGET_WAITKIND_EXECD
:
402 case TARGET_WAITKIND_VFORK_DONE
:
403 case TARGET_WAITKIND_SYSCALL_ENTRY
:
404 case TARGET_WAITKIND_SYSCALL_RETURN
:
405 /* Pass the result to the generic code. */
407 case TARGET_WAITKIND_THREAD_CREATED
:
408 case TARGET_WAITKIND_THREAD_EXITED
:
409 /* The core needlessly stops on these events. */
411 case TARGET_WAITKIND_SPURIOUS
:
412 /* Spurious events are unhandled by the gdbserver core. */
413 if (ptrace (PT_CONTINUE
, current_process ()->pid
, (void *) 1, 0)
415 perror_with_name (("ptrace"));
418 error (("Unknown stopped status"));
423 /* Implement the kill target_ops method. */
426 netbsd_process_target::kill (process_info
*process
)
428 pid_t pid
= process
->pid
;
429 if (ptrace (PT_KILL
, pid
, nullptr, 0) == -1)
433 if (gdb::waitpid (pid
, &status
, 0) == -1)
439 /* Implement the detach target_ops method. */
442 netbsd_process_target::detach (process_info
*process
)
444 pid_t pid
= process
->pid
;
446 ptrace (PT_DETACH
, pid
, (void *) 1, 0);
451 /* Implement the mourn target_ops method. */
454 netbsd_process_target::mourn (struct process_info
*proc
)
456 proc
->for_each_thread (remove_thread
);
458 remove_process (proc
);
461 /* Implement the join target_ops method. */
464 netbsd_process_target::join (int pid
)
466 /* The PT_DETACH is sufficient to detach from the process.
467 So no need to do anything extra. */
470 /* Implement the thread_alive target_ops method. */
473 netbsd_process_target::thread_alive (ptid_t ptid
)
475 return netbsd_nat::thread_alive (ptid
);
478 /* Implement the fetch_registers target_ops method. */
481 netbsd_process_target::fetch_registers (struct regcache
*regcache
, int regno
)
483 const netbsd_regset_info
*regset
= get_regs_info ();
484 ptid_t inferior_ptid
= current_thread
->id
;
486 while (regset
->size
>= 0)
488 std::vector
<char> buf
;
489 buf
.resize (regset
->size
);
490 int res
= ptrace (regset
->get_request
, inferior_ptid
.pid (), buf
.data (),
491 inferior_ptid
.lwp ());
493 perror_with_name (("ptrace"));
494 regset
->store_function (regcache
, buf
.data ());
499 /* Implement the store_registers target_ops method. */
502 netbsd_process_target::store_registers (struct regcache
*regcache
, int regno
)
504 const netbsd_regset_info
*regset
= get_regs_info ();
505 ptid_t inferior_ptid
= current_thread
->id
;
507 while (regset
->size
>= 0)
509 std::vector
<char> buf
;
510 buf
.resize (regset
->size
);
511 int res
= ptrace (regset
->get_request
, inferior_ptid
.pid (), buf
.data (),
512 inferior_ptid
.lwp ());
514 perror_with_name (("ptrace"));
516 /* Then overlay our cached registers on that. */
517 regset
->fill_function (regcache
, buf
.data ());
518 /* Only now do we write the register set. */
519 res
= ptrace (regset
->set_request
, inferior_ptid
.pid (), buf
. data (),
520 inferior_ptid
.lwp ());
522 perror_with_name (("ptrace"));
527 /* Implement the read_memory target_ops method. */
530 netbsd_process_target::read_memory (CORE_ADDR memaddr
, unsigned char *myaddr
,
533 pid_t pid
= current_process ()->pid
;
534 return netbsd_nat::read_memory (pid
, myaddr
, memaddr
, size
, nullptr);
537 /* Implement the write_memory target_ops method. */
540 netbsd_process_target::write_memory (CORE_ADDR memaddr
,
541 const unsigned char *myaddr
, int size
)
543 pid_t pid
= current_process ()->pid
;
544 return netbsd_nat::write_memory (pid
, myaddr
, memaddr
, size
, nullptr);
547 /* Implement the request_interrupt target_ops method. */
550 netbsd_process_target::request_interrupt ()
552 ptid_t inferior_ptid
= get_first_thread ()->id
;
554 ::kill (inferior_ptid
.pid (), SIGINT
);
557 /* Read the AUX Vector for the specified PID, wrapping the ptrace(2) call
558 with the PIOD_READ_AUXV operation and using the PT_IO standard input
559 and output arguments. */
562 netbsd_read_auxv(pid_t pid
, void *offs
, void *addr
, size_t len
)
564 struct ptrace_io_desc pio
;
566 pio
.piod_op
= PIOD_READ_AUXV
;
567 pio
.piod_offs
= offs
;
568 pio
.piod_addr
= addr
;
571 if (ptrace (PT_IO
, pid
, &pio
, 0) == -1)
572 perror_with_name (("ptrace"));
577 /* Copy LEN bytes from inferior's auxiliary vector starting at OFFSET
578 to debugger memory starting at MYADDR. */
581 netbsd_process_target::read_auxv (int pid
, CORE_ADDR offset
,
582 unsigned char *myaddr
, unsigned int len
)
584 return netbsd_read_auxv (pid
, (void *) (intptr_t) offset
, myaddr
, len
);
588 netbsd_process_target::supports_z_point_type (char z_type
)
595 case Z_PACKET_WRITE_WP
:
596 case Z_PACKET_READ_WP
:
597 case Z_PACKET_ACCESS_WP
:
599 return false; /* Not supported. */
603 /* Insert {break/watch}point at address ADDR. SIZE is not used. */
606 netbsd_process_target::insert_point (enum raw_bkpt_type type
, CORE_ADDR addr
,
607 int size
, struct raw_breakpoint
*bp
)
611 case raw_bkpt_type_sw
:
612 return insert_memory_breakpoint (bp
);
613 case raw_bkpt_type_hw
:
614 case raw_bkpt_type_write_wp
:
615 case raw_bkpt_type_read_wp
:
616 case raw_bkpt_type_access_wp
:
618 return 1; /* Not supported. */
622 /* Remove {break/watch}point at address ADDR. SIZE is not used. */
625 netbsd_process_target::remove_point (enum raw_bkpt_type type
, CORE_ADDR addr
,
626 int size
, struct raw_breakpoint
*bp
)
630 case raw_bkpt_type_sw
:
631 return remove_memory_breakpoint (bp
);
632 case raw_bkpt_type_hw
:
633 case raw_bkpt_type_write_wp
:
634 case raw_bkpt_type_read_wp
:
635 case raw_bkpt_type_access_wp
:
637 return 1; /* Not supported. */
641 /* Implement the stopped_by_sw_breakpoint target_ops method. */
644 netbsd_process_target::stopped_by_sw_breakpoint ()
646 ptrace_siginfo_t psi
;
647 pid_t pid
= current_process ()->pid
;
649 if (ptrace (PT_GET_SIGINFO
, pid
, &psi
, sizeof (psi
)) == -1)
650 perror_with_name (("ptrace"));
652 return psi
.psi_siginfo
.si_signo
== SIGTRAP
&&
653 psi
.psi_siginfo
.si_code
== TRAP_BRKPT
;
656 /* Implement the supports_stopped_by_sw_breakpoint target_ops method. */
659 netbsd_process_target::supports_stopped_by_sw_breakpoint ()
664 /* Implement the supports_qxfer_siginfo target_ops method. */
667 netbsd_process_target::supports_qxfer_siginfo ()
672 /* Implement the qxfer_siginfo target_ops method. */
675 netbsd_process_target::qxfer_siginfo (const char *annex
, unsigned char *readbuf
,
676 unsigned const char *writebuf
,
677 CORE_ADDR offset
, int len
)
679 if (current_thread
== nullptr)
682 pid_t pid
= current_process ()->pid
;
684 return netbsd_nat::qxfer_siginfo(pid
, annex
, readbuf
, writebuf
, offset
, len
);
687 /* Implement the supports_non_stop target_ops method. */
690 netbsd_process_target::supports_non_stop ()
695 /* Implement the supports_multi_process target_ops method. */
698 netbsd_process_target::supports_multi_process ()
703 /* Check if fork events are supported. */
706 netbsd_process_target::supports_fork_events ()
711 /* Check if vfork events are supported. */
714 netbsd_process_target::supports_vfork_events ()
719 /* Check if exec events are supported. */
722 netbsd_process_target::supports_exec_events ()
727 /* Implement the supports_disable_randomization target_ops method. */
730 netbsd_process_target::supports_disable_randomization ()
735 /* Extract &phdr and num_phdr in the inferior. Return 0 on success. */
737 template <typename T
>
738 int get_phdr_phnum_from_proc_auxv (const pid_t pid
,
739 CORE_ADDR
*phdr_memaddr
, int *num_phdr
)
741 typedef typename
std::conditional
<sizeof(T
) == sizeof(int64_t),
742 Aux64Info
, Aux32Info
>::type auxv_type
;
743 const size_t auxv_size
= sizeof (auxv_type
);
744 const size_t auxv_buf_size
= 128 * sizeof (auxv_type
);
746 std::vector
<char> auxv_buf
;
747 auxv_buf
.resize (auxv_buf_size
);
749 netbsd_read_auxv (pid
, nullptr, auxv_buf
.data (), auxv_buf_size
);
754 for (char *buf
= auxv_buf
.data ();
755 buf
< (auxv_buf
.data () + auxv_buf_size
);
758 auxv_type
*const aux
= (auxv_type
*) buf
;
763 *phdr_memaddr
= aux
->a_v
;
766 *num_phdr
= aux
->a_v
;
770 if (*phdr_memaddr
!= 0 && *num_phdr
!= 0)
774 if (*phdr_memaddr
== 0 || *num_phdr
== 0)
776 warning ("Unexpected missing AT_PHDR and/or AT_PHNUM: "
777 "phdr_memaddr = %s, phdr_num = %d",
778 core_addr_to_string (*phdr_memaddr
), *num_phdr
);
785 /* Return &_DYNAMIC (via PT_DYNAMIC) in the inferior, or 0 if not present. */
787 template <typename T
>
789 get_dynamic (const pid_t pid
)
791 typedef typename
std::conditional
<sizeof(T
) == sizeof(int64_t),
792 Elf64_Phdr
, Elf32_Phdr
>::type phdr_type
;
793 const int phdr_size
= sizeof (phdr_type
);
795 CORE_ADDR phdr_memaddr
;
797 if (get_phdr_phnum_from_proc_auxv
<T
> (pid
, &phdr_memaddr
, &num_phdr
))
800 std::vector
<unsigned char> phdr_buf
;
801 phdr_buf
.resize (num_phdr
* phdr_size
);
803 if (netbsd_nat::read_memory (pid
, phdr_buf
.data (), phdr_memaddr
,
804 phdr_buf
.size (), nullptr))
807 /* Compute relocation: it is expected to be 0 for "regular" executables,
808 non-zero for PIE ones. */
809 CORE_ADDR relocation
= -1;
810 for (int i
= 0; relocation
== -1 && i
< num_phdr
; i
++)
812 phdr_type
*const p
= (phdr_type
*) (phdr_buf
.data () + i
* phdr_size
);
814 if (p
->p_type
== PT_PHDR
)
815 relocation
= phdr_memaddr
- p
->p_vaddr
;
818 if (relocation
== -1)
820 /* PT_PHDR is optional, but necessary for PIE in general. Fortunately
821 any real world executables, including PIE executables, have always
822 PT_PHDR present. PT_PHDR is not present in some shared libraries or
823 in fpc (Free Pascal 2.4) binaries but neither of those have a need for
824 or present DT_DEBUG anyway (fpc binaries are statically linked).
826 Therefore if there exists DT_DEBUG there is always also PT_PHDR.
828 GDB could find RELOCATION also from AT_ENTRY - e_entry. */
833 for (int i
= 0; i
< num_phdr
; i
++)
835 phdr_type
*const p
= (phdr_type
*) (phdr_buf
.data () + i
* phdr_size
);
837 if (p
->p_type
== PT_DYNAMIC
)
838 return p
->p_vaddr
+ relocation
;
844 /* Return &_r_debug in the inferior, or -1 if not present. Return value
845 can be 0 if the inferior does not yet have the library list initialized.
846 We look for DT_MIPS_RLD_MAP first. MIPS executables use this instead of
847 DT_DEBUG, although they sometimes contain an unused DT_DEBUG entry too. */
849 template <typename T
>
851 get_r_debug (const pid_t pid
)
853 typedef typename
std::conditional
<sizeof(T
) == sizeof(int64_t),
854 Elf64_Dyn
, Elf32_Dyn
>::type dyn_type
;
855 const int dyn_size
= sizeof (dyn_type
);
856 unsigned char buf
[sizeof (dyn_type
)]; /* The larger of the two. */
859 CORE_ADDR dynamic_memaddr
= get_dynamic
<T
> (pid
);
860 if (dynamic_memaddr
== 0)
863 while (netbsd_nat::read_memory (pid
, buf
, dynamic_memaddr
, dyn_size
, nullptr)
866 dyn_type
*const dyn
= (dyn_type
*) buf
;
867 #if defined DT_MIPS_RLD_MAP
871 unsigned char buf
[sizeof (T
)];
875 if (dyn
->d_tag
== DT_MIPS_RLD_MAP
)
877 if (netbsd_nat::read_memory (pid
, rld_map
.buf
, dyn
->d_un
.d_val
,
878 sizeof (rld_map
.buf
), nullptr) == 0)
883 #endif /* DT_MIPS_RLD_MAP */
885 if (dyn
->d_tag
== DT_DEBUG
&& map
== -1)
886 map
= dyn
->d_un
.d_val
;
888 if (dyn
->d_tag
== DT_NULL
)
891 dynamic_memaddr
+= dyn_size
;
897 /* Read one pointer from MEMADDR in the inferior. */
900 read_one_ptr (const pid_t pid
, CORE_ADDR memaddr
, CORE_ADDR
*ptr
, int ptr_size
)
902 /* Go through a union so this works on either big or little endian
903 hosts, when the inferior's pointer size is smaller than the size
904 of CORE_ADDR. It is assumed the inferior's endianness is the
905 same of the superior's. */
914 int ret
= netbsd_nat::read_memory (pid
, &addr
.uc
, memaddr
, ptr_size
, nullptr);
917 if (ptr_size
== sizeof (CORE_ADDR
))
918 *ptr
= addr
.core_addr
;
919 else if (ptr_size
== sizeof (unsigned int))
922 gdb_assert_not_reached ("unhandled pointer size");
927 /* Construct qXfer:libraries-svr4:read reply. */
929 template <typename T
>
931 netbsd_qxfer_libraries_svr4 (const pid_t pid
, const char *annex
,
932 unsigned char *readbuf
,
933 unsigned const char *writebuf
,
934 CORE_ADDR offset
, int len
)
936 struct link_map_offsets
938 /* Offset and size of r_debug.r_version. */
939 int r_version_offset
;
941 /* Offset and size of r_debug.r_map. */
944 /* Offset to l_addr field in struct link_map. */
947 /* Offset to l_name field in struct link_map. */
950 /* Offset to l_ld field in struct link_map. */
953 /* Offset to l_next field in struct link_map. */
956 /* Offset to l_prev field in struct link_map. */
960 static const struct link_map_offsets lmo_32bit_offsets
=
962 0, /* r_version offset. */
963 4, /* r_debug.r_map offset. */
964 0, /* l_addr offset in link_map. */
965 4, /* l_name offset in link_map. */
966 8, /* l_ld offset in link_map. */
967 12, /* l_next offset in link_map. */
968 16 /* l_prev offset in link_map. */
971 static const struct link_map_offsets lmo_64bit_offsets
=
973 0, /* r_version offset. */
974 8, /* r_debug.r_map offset. */
975 0, /* l_addr offset in link_map. */
976 8, /* l_name offset in link_map. */
977 16, /* l_ld offset in link_map. */
978 24, /* l_next offset in link_map. */
979 32 /* l_prev offset in link_map. */
982 CORE_ADDR lm_addr
= 0, lm_prev
= 0;
983 CORE_ADDR l_name
, l_addr
, l_ld
, l_next
, l_prev
;
986 const struct link_map_offsets
*lmo
987 = ((sizeof (T
) == sizeof (int64_t))
988 ? &lmo_64bit_offsets
: &lmo_32bit_offsets
);
989 int ptr_size
= sizeof (T
);
991 while (annex
[0] != '\0')
993 const char *sep
= strchr (annex
, '=');
997 int name_len
= sep
- annex
;
999 if (name_len
== 5 && startswith (annex
, "start"))
1001 else if (name_len
== 4 && startswith (annex
, "prev"))
1005 annex
= strchr (sep
, ';');
1006 if (annex
== nullptr)
1012 annex
= decode_address_to_semicolon (addrp
, sep
+ 1);
1017 CORE_ADDR r_debug
= get_r_debug
<T
> (pid
);
1019 /* We failed to find DT_DEBUG. Such situation will not change
1020 for this inferior - do not retry it. Report it to GDB as
1021 E01, see for the reasons at the GDB solib-svr4.c side. */
1022 if (r_debug
== (CORE_ADDR
) -1)
1027 CORE_ADDR map_offset
= r_debug
+ lmo
->r_map_offset
;
1028 if (read_one_ptr (pid
, map_offset
, &lm_addr
, ptr_size
) != 0)
1029 warning ("unable to read r_map from %s",
1030 core_addr_to_string (map_offset
));
1034 std::string document
= "<library-list-svr4 version=\"1.0\"";
1037 && read_one_ptr (pid
, lm_addr
+ lmo
->l_name_offset
,
1038 &l_name
, ptr_size
) == 0
1039 && read_one_ptr (pid
, lm_addr
+ lmo
->l_addr_offset
,
1040 &l_addr
, ptr_size
) == 0
1041 && read_one_ptr (pid
, lm_addr
+ lmo
->l_ld_offset
,
1042 &l_ld
, ptr_size
) == 0
1043 && read_one_ptr (pid
, lm_addr
+ lmo
->l_prev_offset
,
1044 &l_prev
, ptr_size
) == 0
1045 && read_one_ptr (pid
, lm_addr
+ lmo
->l_next_offset
,
1046 &l_next
, ptr_size
) == 0)
1048 if (lm_prev
!= l_prev
)
1050 warning ("Corrupted shared library list: 0x%lx != 0x%lx",
1051 (long) lm_prev
, (long) l_prev
);
1055 /* Ignore the first entry even if it has valid name as the first entry
1056 corresponds to the main executable. The first entry should not be
1057 skipped if the dynamic loader was loaded late by a static executable
1058 (see solib-svr4.c parameter ignore_first). But in such case the main
1059 executable does not have PT_DYNAMIC present and this function already
1060 exited above due to failed get_r_debug. */
1062 string_appendf (document
, " main-lm=\"0x%lx\"",
1063 (unsigned long) lm_addr
);
1066 unsigned char libname
[PATH_MAX
];
1068 /* Not checking for error because reading may stop before
1069 we've got PATH_MAX worth of characters. */
1071 netbsd_nat::read_memory (pid
, libname
, l_name
, sizeof (libname
) - 1,
1073 libname
[sizeof (libname
) - 1] = '\0';
1074 if (libname
[0] != '\0')
1078 /* Terminate `<library-list-svr4'. */
1083 string_appendf (document
, "<library name=\"");
1084 xml_escape_text_append (document
, (char *) libname
);
1085 string_appendf (document
, "\" lm=\"0x%lx\" "
1086 "l_addr=\"0x%lx\" l_ld=\"0x%lx\"/>",
1087 (unsigned long) lm_addr
, (unsigned long) l_addr
,
1088 (unsigned long) l_ld
);
1098 /* Empty list; terminate `<library-list-svr4'. */
1102 document
+= "</library-list-svr4>";
1104 int document_len
= document
.length ();
1105 if (offset
< document_len
)
1106 document_len
-= offset
;
1109 if (len
> document_len
)
1112 memcpy (readbuf
, document
.data () + offset
, len
);
1117 /* Return true if FILE is a 64-bit ELF file,
1118 false if the file is not a 64-bit ELF file,
1119 and error if the file is not accessible or doesn't exist. */
1122 elf_64_file_p (const char *file
)
1124 int fd
= gdb::open (file
, O_RDONLY
);
1126 perror_with_name (("open"));
1129 ssize_t ret
= gdb::read (fd
, &header
, sizeof (header
));
1131 perror_with_name (("read"));
1133 if (ret
!= sizeof (header
))
1134 error ("Cannot read ELF file header: %s", file
);
1136 if (header
.e_ident
[EI_MAG0
] != ELFMAG0
1137 || header
.e_ident
[EI_MAG1
] != ELFMAG1
1138 || header
.e_ident
[EI_MAG2
] != ELFMAG2
1139 || header
.e_ident
[EI_MAG3
] != ELFMAG3
)
1140 error ("Unrecognized ELF file header: %s", file
);
1142 return header
.e_ident
[EI_CLASS
] == ELFCLASS64
;
1145 /* Construct qXfer:libraries-svr4:read reply. */
1148 netbsd_process_target::qxfer_libraries_svr4 (const char *annex
,
1149 unsigned char *readbuf
,
1150 unsigned const char *writebuf
,
1151 CORE_ADDR offset
, int len
)
1153 if (writebuf
!= nullptr)
1155 if (readbuf
== nullptr)
1158 struct process_info
*proc
= current_process ();
1159 pid_t pid
= proc
->pid
;
1160 bool is_elf64
= elf_64_file_p (netbsd_nat::pid_to_exec_file (pid
));
1163 return netbsd_qxfer_libraries_svr4
<int64_t> (pid
, annex
, readbuf
,
1164 writebuf
, offset
, len
);
1166 return netbsd_qxfer_libraries_svr4
<int32_t> (pid
, annex
, readbuf
,
1167 writebuf
, offset
, len
);
1170 /* Implement the supports_qxfer_libraries_svr4 target_ops method. */
1173 netbsd_process_target::supports_qxfer_libraries_svr4 ()
1178 /* Return the name of a file that can be opened to get the symbols for
1179 the child process identified by PID. */
1182 netbsd_process_target::pid_to_exec_file (pid_t pid
)
1184 return netbsd_nat::pid_to_exec_file (pid
);
1187 /* Implementation of the target_ops method "supports_pid_to_exec_file". */
1190 netbsd_process_target::supports_pid_to_exec_file ()
1195 /* Implementation of the target_ops method "supports_hardware_single_step". */
1197 netbsd_process_target::supports_hardware_single_step ()
1202 /* Implementation of the target_ops method "sw_breakpoint_from_kind". */
1205 netbsd_process_target::sw_breakpoint_from_kind (int kind
, int *size
)
1207 static gdb_byte brkpt
[PTRACE_BREAKPOINT_SIZE
] = {*PTRACE_BREAKPOINT
};
1209 *size
= PTRACE_BREAKPOINT_SIZE
;
1214 /* Implement the thread_name target_ops method. */
1217 netbsd_process_target::thread_name (ptid_t ptid
)
1219 return netbsd_nat::thread_name (ptid
);
1222 /* Implement the supports_catch_syscall target_ops method. */
1225 netbsd_process_target::supports_catch_syscall ()
1230 /* Implement the supports_read_auxv target_ops method. */
1233 netbsd_process_target::supports_read_auxv ()
1241 set_target_ops (the_netbsd_target
);