1 /* Copyright (C) 2020-2021 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/>. */
20 #include "netbsd-low.h"
21 #include "nat/netbsd-nat.h"
23 #include <sys/param.h>
24 #include <sys/types.h>
26 #include <sys/ptrace.h>
27 #include <sys/sysctl.h>
35 #include <type_traits>
37 #include "gdbsupport/eintr.h"
38 #include "gdbsupport/gdb_wait.h"
39 #include "gdbsupport/filestuff.h"
40 #include "gdbsupport/common-inferior.h"
41 #include "nat/fork-inferior.h"
44 int using_threads
= 1;
46 /* Callback used by fork_inferior to start tracing the inferior. */
51 /* Switch child to its own process group so that signals won't
52 directly affect GDBserver. */
53 if (setpgid (0, 0) < 0)
54 trace_start_error_with_name (("setpgid"));
56 if (ptrace (PT_TRACE_ME
, 0, nullptr, 0) < 0)
57 trace_start_error_with_name (("ptrace"));
59 /* If GDBserver is connected to gdb via stdio, redirect the inferior's
60 stdout to stderr so that inferior i/o doesn't corrupt the connection.
61 Also, redirect stdin to /dev/null. */
62 if (remote_connection_is_stdio ())
65 trace_start_error_with_name (("close"));
66 if (open ("/dev/null", O_RDONLY
) < 0)
67 trace_start_error_with_name (("open"));
69 trace_start_error_with_name (("dup2"));
70 if (write (2, "stdin/stdout redirected\n",
71 sizeof ("stdin/stdout redirected\n") - 1) < 0)
78 /* Implement the create_inferior method of the target_ops vector. */
81 netbsd_process_target::create_inferior (const char *program
,
82 const std::vector
<char *> &program_args
)
84 std::string str_program_args
= construct_inferior_arguments (program_args
);
86 pid_t pid
= fork_inferior (program
, str_program_args
.c_str (),
87 get_environ ()->envp (), netbsd_ptrace_fun
,
88 nullptr, nullptr, nullptr, nullptr);
92 post_fork_inferior (pid
, program
);
97 /* Implement the post_create_inferior target_ops method. */
100 netbsd_process_target::post_create_inferior ()
102 pid_t pid
= current_process ()->pid
;
103 netbsd_nat::enable_proc_events (pid
);
108 /* Implement the attach target_ops method. */
111 netbsd_process_target::attach (unsigned long pid
)
117 /* Returns true if GDB is interested in any child syscalls. */
120 gdb_catching_syscalls_p (pid_t pid
)
122 struct process_info
*proc
= find_process_pid (pid
);
123 return !proc
->syscalls_to_catch
.empty ();
126 /* Implement the resume target_ops method. */
129 netbsd_process_target::resume (struct thread_resume
*resume_info
, size_t n
)
131 ptid_t resume_ptid
= resume_info
[0].thread
;
132 const int signal
= resume_info
[0].sig
;
133 const bool step
= resume_info
[0].kind
== resume_step
;
135 if (resume_ptid
== minus_one_ptid
)
136 resume_ptid
= ptid_of (current_thread
);
138 const pid_t pid
= resume_ptid
.pid ();
139 const lwpid_t lwp
= resume_ptid
.lwp ();
140 regcache_invalidate_pid (pid
);
147 if (ptid
.lwp () == lwp
|| n
!= 1)
149 if (ptrace (PT_SETSTEP
, pid
, NULL
, ptid
.lwp ()) == -1)
150 perror_with_name (("ptrace"));
151 if (ptrace (PT_RESUME
, pid
, NULL
, ptid
.lwp ()) == -1)
152 perror_with_name (("ptrace"));
156 if (ptrace (PT_CLEARSTEP
, pid
, NULL
, ptid
.lwp ()) == -1)
157 perror_with_name (("ptrace"));
158 if (ptrace (PT_SUSPEND
, pid
, NULL
, ptid
.lwp ()) == -1)
159 perror_with_name (("ptrace"));
164 if (ptrace (PT_CLEARSTEP
, pid
, NULL
, ptid
.lwp ()) == -1)
165 perror_with_name (("ptrace"));
166 if (ptrace (PT_RESUME
, pid
, NULL
, ptid
.lwp ()) == -1)
167 perror_with_name (("ptrace"));
171 netbsd_nat::for_each_thread (pid
, fn
);
173 int request
= gdb_catching_syscalls_p (pid
) ? PT_CONTINUE
: PT_SYSCALL
;
176 ptrace (request
, pid
, (void *)1, signal
);
178 perror_with_name (("ptrace"));
181 /* Returns true if GDB is interested in the reported SYSNO syscall. */
184 netbsd_catch_this_syscall (int sysno
)
186 struct process_info
*proc
= current_process ();
188 if (proc
->syscalls_to_catch
.empty ())
191 if (proc
->syscalls_to_catch
[0] == ANY_SYSCALL
)
194 for (int iter
: proc
->syscalls_to_catch
)
201 /* Helper function for child_wait and the derivatives of child_wait.
202 HOSTSTATUS is the waitstatus from wait() or the equivalent; store our
203 translation of that in OURSTATUS. */
206 netbsd_store_waitstatus (struct target_waitstatus
*ourstatus
, int hoststatus
)
208 if (WIFEXITED (hoststatus
))
210 ourstatus
->kind
= TARGET_WAITKIND_EXITED
;
211 ourstatus
->value
.integer
= WEXITSTATUS (hoststatus
);
213 else if (!WIFSTOPPED (hoststatus
))
215 ourstatus
->kind
= TARGET_WAITKIND_SIGNALLED
;
216 ourstatus
->value
.sig
= gdb_signal_from_host (WTERMSIG (hoststatus
));
220 ourstatus
->kind
= TARGET_WAITKIND_STOPPED
;
221 ourstatus
->value
.sig
= gdb_signal_from_host (WSTOPSIG (hoststatus
));
225 /* Implement a safe wrapper around waitpid(). */
228 netbsd_waitpid (ptid_t ptid
, struct target_waitstatus
*ourstatus
,
229 target_wait_flags target_options
)
232 int options
= (target_options
& TARGET_WNOHANG
) ? WNOHANG
: 0;
235 = gdb::handle_eintr (-1, ::waitpid
, ptid
.pid (), &status
, options
);
238 perror_with_name (_("Child process unexpectedly missing"));
240 netbsd_store_waitstatus (ourstatus
, status
);
245 /* Implement the wait target_ops method.
247 Wait for the child specified by PTID to do something. Return the
248 process ID of the child, or MINUS_ONE_PTID in case of error; store
249 the status in *OURSTATUS. */
252 netbsd_wait (ptid_t ptid
, struct target_waitstatus
*ourstatus
,
253 target_wait_flags target_options
)
255 pid_t pid
= netbsd_waitpid (ptid
, ourstatus
, target_options
);
256 ptid_t wptid
= ptid_t (pid
);
260 gdb_assert (target_options
& TARGET_WNOHANG
);
261 ourstatus
->kind
= TARGET_WAITKIND_IGNORE
;
265 gdb_assert (pid
!= -1);
267 /* If the child stopped, keep investigating its status. */
268 if (ourstatus
->kind
!= TARGET_WAITKIND_STOPPED
)
271 /* Extract the event and thread that received a signal. */
272 ptrace_siginfo_t psi
;
273 if (ptrace (PT_GET_SIGINFO
, pid
, &psi
, sizeof (psi
)) == -1)
274 perror_with_name (("ptrace"));
276 /* Pick child's siginfo_t. */
277 siginfo_t
*si
= &psi
.psi_siginfo
;
279 lwpid_t lwp
= psi
.psi_lwpid
;
281 int signo
= si
->si_signo
;
282 const int code
= si
->si_code
;
284 /* Construct PTID with a specified thread that received the event.
285 If a signal was targeted to the whole process, lwp is 0. */
286 wptid
= ptid_t (pid
, lwp
, 0);
288 /* Bail out on non-debugger oriented signals. */
289 if (signo
!= SIGTRAP
)
292 /* Stop examining non-debugger oriented SIGTRAP codes. */
293 if (code
<= SI_USER
|| code
== SI_NOINFO
)
296 /* Process state for threading events. */
297 ptrace_state_t pst
= {};
298 if (code
== TRAP_LWP
)
299 if (ptrace (PT_GET_PROCESS_STATE
, pid
, &pst
, sizeof (pst
)) == -1)
300 perror_with_name (("ptrace"));
302 if (code
== TRAP_LWP
&& pst
.pe_report_event
== PTRACE_LWP_EXIT
)
304 /* If GDB attaches to a multi-threaded process, exiting
305 threads might be skipped during post_attach that
306 have not yet reported their PTRACE_LWP_EXIT event.
307 Ignore exited events for an unknown LWP. */
308 thread_info
*thr
= find_thread_ptid (wptid
);
310 ourstatus
->kind
= TARGET_WAITKIND_SPURIOUS
;
313 ourstatus
->kind
= TARGET_WAITKIND_THREAD_EXITED
;
314 /* NetBSD does not store an LWP exit status. */
315 ourstatus
->value
.integer
= 0;
322 if (find_thread_ptid (ptid_t (pid
)))
323 current_thread
= find_thread_ptid (wptid
);
325 if (code
== TRAP_LWP
&& pst
.pe_report_event
== PTRACE_LWP_CREATE
)
327 /* If GDB attaches to a multi-threaded process, newborn
328 threads might be added by nbsd_add_threads that have
329 not yet reported their PTRACE_LWP_CREATE event. Ignore
330 born events for an already-known LWP. */
331 if (find_thread_ptid (wptid
))
332 ourstatus
->kind
= TARGET_WAITKIND_SPURIOUS
;
335 add_thread (wptid
, NULL
);
336 ourstatus
->kind
= TARGET_WAITKIND_THREAD_CREATED
;
341 if (code
== TRAP_EXEC
)
343 ourstatus
->kind
= TARGET_WAITKIND_EXECD
;
344 ourstatus
->value
.execd_pathname
345 = xstrdup (netbsd_nat::pid_to_exec_file (pid
));
349 if (code
== TRAP_TRACE
)
352 if (code
== TRAP_SCE
|| code
== TRAP_SCX
)
354 int sysnum
= si
->si_sysnum
;
356 if (!netbsd_catch_this_syscall(sysnum
))
358 /* If the core isn't interested in this event, ignore it. */
359 ourstatus
->kind
= TARGET_WAITKIND_SPURIOUS
;
364 = ((code
== TRAP_SCE
) ? TARGET_WAITKIND_SYSCALL_ENTRY
:
365 TARGET_WAITKIND_SYSCALL_RETURN
);
366 ourstatus
->value
.syscall_number
= sysnum
;
370 if (code
== TRAP_BRKPT
)
372 #ifdef PTRACE_BREAKPOINT_ADJ
375 ptrace (PT_GETREGS
, pid
, &r
, psi
.psi_lwpid
);
376 pc
= PTRACE_REG_PC (&r
);
377 PTRACE_REG_SET_PC (&r
, pc
- PTRACE_BREAKPOINT_ADJ
);
378 ptrace (PT_SETREGS
, pid
, &r
, psi
.psi_lwpid
);
383 /* Unclassified SIGTRAP event. */
384 ourstatus
->kind
= TARGET_WAITKIND_SPURIOUS
;
388 /* Implement the wait target_ops method. */
391 netbsd_process_target::wait (ptid_t ptid
, struct target_waitstatus
*ourstatus
,
392 target_wait_flags target_options
)
396 ptid_t wptid
= netbsd_wait (ptid
, ourstatus
, target_options
);
398 /* Register thread in the gdbcore if a thread was not reported earlier.
399 This is required after ::create_inferior, when the gdbcore does not
400 know about the first internal thread.
401 This may also happen on attach, when an event is registered on a thread
402 that was not fully initialized during the attach stage. */
403 if (wptid
.lwp () != 0 && !find_thread_ptid (wptid
)
404 && ourstatus
->kind
!= TARGET_WAITKIND_THREAD_EXITED
)
405 add_thread (wptid
, nullptr);
407 switch (ourstatus
->kind
)
409 case TARGET_WAITKIND_EXITED
:
410 case TARGET_WAITKIND_STOPPED
:
411 case TARGET_WAITKIND_SIGNALLED
:
412 case TARGET_WAITKIND_FORKED
:
413 case TARGET_WAITKIND_VFORKED
:
414 case TARGET_WAITKIND_EXECD
:
415 case TARGET_WAITKIND_VFORK_DONE
:
416 case TARGET_WAITKIND_SYSCALL_ENTRY
:
417 case TARGET_WAITKIND_SYSCALL_RETURN
:
418 /* Pass the result to the generic code. */
420 case TARGET_WAITKIND_THREAD_CREATED
:
421 case TARGET_WAITKIND_THREAD_EXITED
:
422 /* The core needlessly stops on these events. */
424 case TARGET_WAITKIND_SPURIOUS
:
425 /* Spurious events are unhandled by the gdbserver core. */
426 if (ptrace (PT_CONTINUE
, current_process ()->pid
, (void *) 1, 0)
428 perror_with_name (("ptrace"));
431 error (("Unknown stopped status"));
436 /* Implement the kill target_ops method. */
439 netbsd_process_target::kill (process_info
*process
)
441 pid_t pid
= process
->pid
;
442 if (ptrace (PT_KILL
, pid
, nullptr, 0) == -1)
446 if (gdb::handle_eintr (-1, ::waitpid
, pid
, &status
, 0) == -1)
452 /* Implement the detach target_ops method. */
455 netbsd_process_target::detach (process_info
*process
)
457 pid_t pid
= process
->pid
;
459 ptrace (PT_DETACH
, pid
, (void *) 1, 0);
464 /* Implement the mourn target_ops method. */
467 netbsd_process_target::mourn (struct process_info
*proc
)
469 for_each_thread (proc
->pid
, remove_thread
);
471 remove_process (proc
);
474 /* Implement the join target_ops method. */
477 netbsd_process_target::join (int pid
)
479 /* The PT_DETACH is sufficient to detach from the process.
480 So no need to do anything extra. */
483 /* Implement the thread_alive target_ops method. */
486 netbsd_process_target::thread_alive (ptid_t ptid
)
488 return netbsd_nat::thread_alive (ptid
);
491 /* Implement the fetch_registers target_ops method. */
494 netbsd_process_target::fetch_registers (struct regcache
*regcache
, int regno
)
496 const netbsd_regset_info
*regset
= get_regs_info ();
497 ptid_t inferior_ptid
= ptid_of (current_thread
);
499 while (regset
->size
>= 0)
501 std::vector
<char> buf
;
502 buf
.resize (regset
->size
);
503 int res
= ptrace (regset
->get_request
, inferior_ptid
.pid (), buf
.data (),
504 inferior_ptid
.lwp ());
506 perror_with_name (("ptrace"));
507 regset
->store_function (regcache
, buf
.data ());
512 /* Implement the store_registers target_ops method. */
515 netbsd_process_target::store_registers (struct regcache
*regcache
, int regno
)
517 const netbsd_regset_info
*regset
= get_regs_info ();
518 ptid_t inferior_ptid
= ptid_of (current_thread
);
520 while (regset
->size
>= 0)
522 std::vector
<char> buf
;
523 buf
.resize (regset
->size
);
524 int res
= ptrace (regset
->get_request
, inferior_ptid
.pid (), buf
.data (),
525 inferior_ptid
.lwp ());
527 perror_with_name (("ptrace"));
529 /* Then overlay our cached registers on that. */
530 regset
->fill_function (regcache
, buf
.data ());
531 /* Only now do we write the register set. */
532 res
= ptrace (regset
->set_request
, inferior_ptid
.pid (), buf
. data (),
533 inferior_ptid
.lwp ());
535 perror_with_name (("ptrace"));
540 /* Implement the read_memory target_ops method. */
543 netbsd_process_target::read_memory (CORE_ADDR memaddr
, unsigned char *myaddr
,
546 pid_t pid
= current_process ()->pid
;
547 return netbsd_nat::read_memory (pid
, myaddr
, memaddr
, size
, nullptr);
550 /* Implement the write_memory target_ops method. */
553 netbsd_process_target::write_memory (CORE_ADDR memaddr
,
554 const unsigned char *myaddr
, int size
)
556 pid_t pid
= current_process ()->pid
;
557 return netbsd_nat::write_memory (pid
, myaddr
, memaddr
, size
, nullptr);
560 /* Implement the request_interrupt target_ops method. */
563 netbsd_process_target::request_interrupt ()
565 ptid_t inferior_ptid
= ptid_of (get_first_thread ());
567 ::kill (inferior_ptid
.pid (), SIGINT
);
570 /* Read the AUX Vector for the specified PID, wrapping the ptrace(2) call
571 with the PIOD_READ_AUXV operation and using the PT_IO standard input
572 and output arguments. */
575 netbsd_read_auxv(pid_t pid
, void *offs
, void *addr
, size_t len
)
577 struct ptrace_io_desc pio
;
579 pio
.piod_op
= PIOD_READ_AUXV
;
580 pio
.piod_offs
= offs
;
581 pio
.piod_addr
= addr
;
584 if (ptrace (PT_IO
, pid
, &pio
, 0) == -1)
585 perror_with_name (("ptrace"));
590 /* Copy LEN bytes from inferior's auxiliary vector starting at OFFSET
591 to debugger memory starting at MYADDR. */
594 netbsd_process_target::read_auxv (CORE_ADDR offset
,
595 unsigned char *myaddr
, unsigned int len
)
597 pid_t pid
= pid_of (current_thread
);
599 return netbsd_read_auxv (pid
, (void *) (intptr_t) offset
, myaddr
, len
);
603 netbsd_process_target::supports_z_point_type (char z_type
)
610 case Z_PACKET_WRITE_WP
:
611 case Z_PACKET_READ_WP
:
612 case Z_PACKET_ACCESS_WP
:
614 return false; /* Not supported. */
618 /* Insert {break/watch}point at address ADDR. SIZE is not used. */
621 netbsd_process_target::insert_point (enum raw_bkpt_type type
, CORE_ADDR addr
,
622 int size
, struct raw_breakpoint
*bp
)
626 case raw_bkpt_type_sw
:
627 return insert_memory_breakpoint (bp
);
628 case raw_bkpt_type_hw
:
629 case raw_bkpt_type_write_wp
:
630 case raw_bkpt_type_read_wp
:
631 case raw_bkpt_type_access_wp
:
633 return 1; /* Not supported. */
637 /* Remove {break/watch}point at address ADDR. SIZE is not used. */
640 netbsd_process_target::remove_point (enum raw_bkpt_type type
, CORE_ADDR addr
,
641 int size
, struct raw_breakpoint
*bp
)
645 case raw_bkpt_type_sw
:
646 return remove_memory_breakpoint (bp
);
647 case raw_bkpt_type_hw
:
648 case raw_bkpt_type_write_wp
:
649 case raw_bkpt_type_read_wp
:
650 case raw_bkpt_type_access_wp
:
652 return 1; /* Not supported. */
656 /* Implement the stopped_by_sw_breakpoint target_ops method. */
659 netbsd_process_target::stopped_by_sw_breakpoint ()
661 ptrace_siginfo_t psi
;
662 pid_t pid
= current_process ()->pid
;
664 if (ptrace (PT_GET_SIGINFO
, pid
, &psi
, sizeof (psi
)) == -1)
665 perror_with_name (("ptrace"));
667 return psi
.psi_siginfo
.si_signo
== SIGTRAP
&&
668 psi
.psi_siginfo
.si_code
== TRAP_BRKPT
;
671 /* Implement the supports_stopped_by_sw_breakpoint target_ops method. */
674 netbsd_process_target::supports_stopped_by_sw_breakpoint ()
679 /* Implement the supports_qxfer_siginfo target_ops method. */
682 netbsd_process_target::supports_qxfer_siginfo ()
687 /* Implement the qxfer_siginfo target_ops method. */
690 netbsd_process_target::qxfer_siginfo (const char *annex
, unsigned char *readbuf
,
691 unsigned const char *writebuf
,
692 CORE_ADDR offset
, int len
)
694 if (current_thread
== nullptr)
697 pid_t pid
= current_process ()->pid
;
699 return netbsd_nat::qxfer_siginfo(pid
, annex
, readbuf
, writebuf
, offset
, len
);
702 /* Implement the supports_non_stop target_ops method. */
705 netbsd_process_target::supports_non_stop ()
710 /* Implement the supports_multi_process target_ops method. */
713 netbsd_process_target::supports_multi_process ()
718 /* Check if fork events are supported. */
721 netbsd_process_target::supports_fork_events ()
726 /* Check if vfork events are supported. */
729 netbsd_process_target::supports_vfork_events ()
734 /* Check if exec events are supported. */
737 netbsd_process_target::supports_exec_events ()
742 /* Implement the supports_disable_randomization target_ops method. */
745 netbsd_process_target::supports_disable_randomization ()
750 /* Extract &phdr and num_phdr in the inferior. Return 0 on success. */
752 template <typename T
>
753 int get_phdr_phnum_from_proc_auxv (const pid_t pid
,
754 CORE_ADDR
*phdr_memaddr
, int *num_phdr
)
756 typedef typename
std::conditional
<sizeof(T
) == sizeof(int64_t),
757 Aux64Info
, Aux32Info
>::type auxv_type
;
758 const size_t auxv_size
= sizeof (auxv_type
);
759 const size_t auxv_buf_size
= 128 * sizeof (auxv_type
);
761 std::vector
<char> auxv_buf
;
762 auxv_buf
.resize (auxv_buf_size
);
764 netbsd_read_auxv (pid
, nullptr, auxv_buf
.data (), auxv_buf_size
);
769 for (char *buf
= auxv_buf
.data ();
770 buf
< (auxv_buf
.data () + auxv_buf_size
);
773 auxv_type
*const aux
= (auxv_type
*) buf
;
778 *phdr_memaddr
= aux
->a_v
;
781 *num_phdr
= aux
->a_v
;
785 if (*phdr_memaddr
!= 0 && *num_phdr
!= 0)
789 if (*phdr_memaddr
== 0 || *num_phdr
== 0)
791 warning ("Unexpected missing AT_PHDR and/or AT_PHNUM: "
792 "phdr_memaddr = %s, phdr_num = %d",
793 core_addr_to_string (*phdr_memaddr
), *num_phdr
);
800 /* Return &_DYNAMIC (via PT_DYNAMIC) in the inferior, or 0 if not present. */
802 template <typename T
>
804 get_dynamic (const pid_t pid
)
806 typedef typename
std::conditional
<sizeof(T
) == sizeof(int64_t),
807 Elf64_Phdr
, Elf32_Phdr
>::type phdr_type
;
808 const int phdr_size
= sizeof (phdr_type
);
810 CORE_ADDR phdr_memaddr
;
812 if (get_phdr_phnum_from_proc_auxv
<T
> (pid
, &phdr_memaddr
, &num_phdr
))
815 std::vector
<unsigned char> phdr_buf
;
816 phdr_buf
.resize (num_phdr
* phdr_size
);
818 if (netbsd_nat::read_memory (pid
, phdr_buf
.data (), phdr_memaddr
,
819 phdr_buf
.size (), nullptr))
822 /* Compute relocation: it is expected to be 0 for "regular" executables,
823 non-zero for PIE ones. */
824 CORE_ADDR relocation
= -1;
825 for (int i
= 0; relocation
== -1 && i
< num_phdr
; i
++)
827 phdr_type
*const p
= (phdr_type
*) (phdr_buf
.data () + i
* phdr_size
);
829 if (p
->p_type
== PT_PHDR
)
830 relocation
= phdr_memaddr
- p
->p_vaddr
;
833 if (relocation
== -1)
835 /* PT_PHDR is optional, but necessary for PIE in general. Fortunately
836 any real world executables, including PIE executables, have always
837 PT_PHDR present. PT_PHDR is not present in some shared libraries or
838 in fpc (Free Pascal 2.4) binaries but neither of those have a need for
839 or present DT_DEBUG anyway (fpc binaries are statically linked).
841 Therefore if there exists DT_DEBUG there is always also PT_PHDR.
843 GDB could find RELOCATION also from AT_ENTRY - e_entry. */
848 for (int i
= 0; i
< num_phdr
; i
++)
850 phdr_type
*const p
= (phdr_type
*) (phdr_buf
.data () + i
* phdr_size
);
852 if (p
->p_type
== PT_DYNAMIC
)
853 return p
->p_vaddr
+ relocation
;
859 /* Return &_r_debug in the inferior, or -1 if not present. Return value
860 can be 0 if the inferior does not yet have the library list initialized.
861 We look for DT_MIPS_RLD_MAP first. MIPS executables use this instead of
862 DT_DEBUG, although they sometimes contain an unused DT_DEBUG entry too. */
864 template <typename T
>
866 get_r_debug (const pid_t pid
)
868 typedef typename
std::conditional
<sizeof(T
) == sizeof(int64_t),
869 Elf64_Dyn
, Elf32_Dyn
>::type dyn_type
;
870 const int dyn_size
= sizeof (dyn_type
);
871 unsigned char buf
[sizeof (dyn_type
)]; /* The larger of the two. */
874 CORE_ADDR dynamic_memaddr
= get_dynamic
<T
> (pid
);
875 if (dynamic_memaddr
== 0)
878 while (netbsd_nat::read_memory (pid
, buf
, dynamic_memaddr
, dyn_size
, nullptr)
881 dyn_type
*const dyn
= (dyn_type
*) buf
;
882 #if defined DT_MIPS_RLD_MAP
886 unsigned char buf
[sizeof (T
)];
890 if (dyn
->d_tag
== DT_MIPS_RLD_MAP
)
892 if (netbsd_nat::read_memory (pid
, rld_map
.buf
, dyn
->d_un
.d_val
,
893 sizeof (rld_map
.buf
), nullptr) == 0)
898 #endif /* DT_MIPS_RLD_MAP */
900 if (dyn
->d_tag
== DT_DEBUG
&& map
== -1)
901 map
= dyn
->d_un
.d_val
;
903 if (dyn
->d_tag
== DT_NULL
)
906 dynamic_memaddr
+= dyn_size
;
912 /* Read one pointer from MEMADDR in the inferior. */
915 read_one_ptr (const pid_t pid
, CORE_ADDR memaddr
, CORE_ADDR
*ptr
, int ptr_size
)
917 /* Go through a union so this works on either big or little endian
918 hosts, when the inferior's pointer size is smaller than the size
919 of CORE_ADDR. It is assumed the inferior's endianness is the
920 same of the superior's. */
929 int ret
= netbsd_nat::read_memory (pid
, &addr
.uc
, memaddr
, ptr_size
, nullptr);
932 if (ptr_size
== sizeof (CORE_ADDR
))
933 *ptr
= addr
.core_addr
;
934 else if (ptr_size
== sizeof (unsigned int))
937 gdb_assert_not_reached ("unhandled pointer size");
942 /* Construct qXfer:libraries-svr4:read reply. */
944 template <typename T
>
946 netbsd_qxfer_libraries_svr4 (const pid_t pid
, const char *annex
,
947 unsigned char *readbuf
,
948 unsigned const char *writebuf
,
949 CORE_ADDR offset
, int len
)
951 struct link_map_offsets
953 /* Offset and size of r_debug.r_version. */
954 int r_version_offset
;
956 /* Offset and size of r_debug.r_map. */
959 /* Offset to l_addr field in struct link_map. */
962 /* Offset to l_name field in struct link_map. */
965 /* Offset to l_ld field in struct link_map. */
968 /* Offset to l_next field in struct link_map. */
971 /* Offset to l_prev field in struct link_map. */
975 static const struct link_map_offsets lmo_32bit_offsets
=
977 0, /* r_version offset. */
978 4, /* r_debug.r_map offset. */
979 0, /* l_addr offset in link_map. */
980 4, /* l_name offset in link_map. */
981 8, /* l_ld offset in link_map. */
982 12, /* l_next offset in link_map. */
983 16 /* l_prev offset in link_map. */
986 static const struct link_map_offsets lmo_64bit_offsets
=
988 0, /* r_version offset. */
989 8, /* r_debug.r_map offset. */
990 0, /* l_addr offset in link_map. */
991 8, /* l_name offset in link_map. */
992 16, /* l_ld offset in link_map. */
993 24, /* l_next offset in link_map. */
994 32 /* l_prev offset in link_map. */
997 CORE_ADDR lm_addr
= 0, lm_prev
= 0;
998 CORE_ADDR l_name
, l_addr
, l_ld
, l_next
, l_prev
;
1001 const struct link_map_offsets
*lmo
1002 = ((sizeof (T
) == sizeof (int64_t))
1003 ? &lmo_64bit_offsets
: &lmo_32bit_offsets
);
1004 int ptr_size
= sizeof (T
);
1006 while (annex
[0] != '\0')
1008 const char *sep
= strchr (annex
, '=');
1012 int name_len
= sep
- annex
;
1014 if (name_len
== 5 && startswith (annex
, "start"))
1016 else if (name_len
== 4 && startswith (annex
, "prev"))
1020 annex
= strchr (sep
, ';');
1021 if (annex
== nullptr)
1027 annex
= decode_address_to_semicolon (addrp
, sep
+ 1);
1032 CORE_ADDR r_debug
= get_r_debug
<T
> (pid
);
1034 /* We failed to find DT_DEBUG. Such situation will not change
1035 for this inferior - do not retry it. Report it to GDB as
1036 E01, see for the reasons at the GDB solib-svr4.c side. */
1037 if (r_debug
== (CORE_ADDR
) -1)
1042 CORE_ADDR map_offset
= r_debug
+ lmo
->r_map_offset
;
1043 if (read_one_ptr (pid
, map_offset
, &lm_addr
, ptr_size
) != 0)
1044 warning ("unable to read r_map from %s",
1045 core_addr_to_string (map_offset
));
1049 std::string document
= "<library-list-svr4 version=\"1.0\"";
1052 && read_one_ptr (pid
, lm_addr
+ lmo
->l_name_offset
,
1053 &l_name
, ptr_size
) == 0
1054 && read_one_ptr (pid
, lm_addr
+ lmo
->l_addr_offset
,
1055 &l_addr
, ptr_size
) == 0
1056 && read_one_ptr (pid
, lm_addr
+ lmo
->l_ld_offset
,
1057 &l_ld
, ptr_size
) == 0
1058 && read_one_ptr (pid
, lm_addr
+ lmo
->l_prev_offset
,
1059 &l_prev
, ptr_size
) == 0
1060 && read_one_ptr (pid
, lm_addr
+ lmo
->l_next_offset
,
1061 &l_next
, ptr_size
) == 0)
1063 if (lm_prev
!= l_prev
)
1065 warning ("Corrupted shared library list: 0x%lx != 0x%lx",
1066 (long) lm_prev
, (long) l_prev
);
1070 /* Ignore the first entry even if it has valid name as the first entry
1071 corresponds to the main executable. The first entry should not be
1072 skipped if the dynamic loader was loaded late by a static executable
1073 (see solib-svr4.c parameter ignore_first). But in such case the main
1074 executable does not have PT_DYNAMIC present and this function already
1075 exited above due to failed get_r_debug. */
1077 string_appendf (document
, " main-lm=\"0x%lx\"",
1078 (unsigned long) lm_addr
);
1081 unsigned char libname
[PATH_MAX
];
1083 /* Not checking for error because reading may stop before
1084 we've got PATH_MAX worth of characters. */
1086 netbsd_nat::read_memory (pid
, libname
, l_name
, sizeof (libname
) - 1,
1088 libname
[sizeof (libname
) - 1] = '\0';
1089 if (libname
[0] != '\0')
1093 /* Terminate `<library-list-svr4'. */
1098 string_appendf (document
, "<library name=\"");
1099 xml_escape_text_append (&document
, (char *) libname
);
1100 string_appendf (document
, "\" lm=\"0x%lx\" "
1101 "l_addr=\"0x%lx\" l_ld=\"0x%lx\"/>",
1102 (unsigned long) lm_addr
, (unsigned long) l_addr
,
1103 (unsigned long) l_ld
);
1113 /* Empty list; terminate `<library-list-svr4'. */
1117 document
+= "</library-list-svr4>";
1119 int document_len
= document
.length ();
1120 if (offset
< document_len
)
1121 document_len
-= offset
;
1124 if (len
> document_len
)
1127 memcpy (readbuf
, document
.data () + offset
, len
);
1132 /* Return true if FILE is a 64-bit ELF file,
1133 false if the file is not a 64-bit ELF file,
1134 and error if the file is not accessible or doesn't exist. */
1137 elf_64_file_p (const char *file
)
1139 int fd
= gdb::handle_eintr (-1, ::open
, file
, O_RDONLY
);
1141 perror_with_name (("open"));
1144 ssize_t ret
= gdb::handle_eintr (-1, ::read
, fd
, &header
, sizeof (header
));
1146 perror_with_name (("read"));
1147 gdb::handle_eintr (-1, ::close
, fd
);
1148 if (ret
!= sizeof (header
))
1149 error ("Cannot read ELF file header: %s", file
);
1151 if (header
.e_ident
[EI_MAG0
] != ELFMAG0
1152 || header
.e_ident
[EI_MAG1
] != ELFMAG1
1153 || header
.e_ident
[EI_MAG2
] != ELFMAG2
1154 || header
.e_ident
[EI_MAG3
] != ELFMAG3
)
1155 error ("Unrecognized ELF file header: %s", file
);
1157 return header
.e_ident
[EI_CLASS
] == ELFCLASS64
;
1160 /* Construct qXfer:libraries-svr4:read reply. */
1163 netbsd_process_target::qxfer_libraries_svr4 (const char *annex
,
1164 unsigned char *readbuf
,
1165 unsigned const char *writebuf
,
1166 CORE_ADDR offset
, int len
)
1168 if (writebuf
!= nullptr)
1170 if (readbuf
== nullptr)
1173 struct process_info
*proc
= current_process ();
1174 pid_t pid
= proc
->pid
;
1175 bool is_elf64
= elf_64_file_p (netbsd_nat::pid_to_exec_file (pid
));
1178 return netbsd_qxfer_libraries_svr4
<int64_t> (pid
, annex
, readbuf
,
1179 writebuf
, offset
, len
);
1181 return netbsd_qxfer_libraries_svr4
<int32_t> (pid
, annex
, readbuf
,
1182 writebuf
, offset
, len
);
1185 /* Implement the supports_qxfer_libraries_svr4 target_ops method. */
1188 netbsd_process_target::supports_qxfer_libraries_svr4 ()
1193 /* Return the name of a file that can be opened to get the symbols for
1194 the child process identified by PID. */
1197 netbsd_process_target::pid_to_exec_file (pid_t pid
)
1199 return netbsd_nat::pid_to_exec_file (pid
);
1202 /* Implementation of the target_ops method "supports_pid_to_exec_file". */
1205 netbsd_process_target::supports_pid_to_exec_file ()
1210 /* Implementation of the target_ops method "supports_hardware_single_step". */
1212 netbsd_process_target::supports_hardware_single_step ()
1217 /* Implementation of the target_ops method "sw_breakpoint_from_kind". */
1220 netbsd_process_target::sw_breakpoint_from_kind (int kind
, int *size
)
1222 static gdb_byte brkpt
[PTRACE_BREAKPOINT_SIZE
] = {*PTRACE_BREAKPOINT
};
1224 *size
= PTRACE_BREAKPOINT_SIZE
;
1229 /* Implement the thread_name target_ops method. */
1232 netbsd_process_target::thread_name (ptid_t ptid
)
1234 return netbsd_nat::thread_name (ptid
);
1237 /* Implement the supports_catch_syscall target_ops method. */
1240 netbsd_process_target::supports_catch_syscall ()
1245 /* Implement the supports_read_auxv target_ops method. */
1248 netbsd_process_target::supports_read_auxv ()
1256 set_target_ops (the_netbsd_target
);