ada-lang.c::advance_wild_match improve doc and parameter+temporaries types
[binutils-gdb.git] / gdbserver / netbsd-low.cc
blob30028d3a384a50bec07869b5f45479330fd0eb32
1 /* Copyright (C) 2020 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/>. */
18 #include "server.h"
19 #include "target.h"
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>
29 #include <limits.h>
30 #include <unistd.h>
31 #include <signal.h>
33 #include <elf.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"
42 #include "hostio.h"
44 int using_threads = 1;
46 const struct target_desc *netbsd_tdesc;
48 /* Call add_process with the given parameters, and initialize
49 the process' private data. */
51 static void
52 netbsd_add_process (int pid, int attached)
54 struct process_info *proc = add_process (pid, attached);
55 proc->tdesc = netbsd_tdesc;
56 proc->priv = nullptr;
59 /* Callback used by fork_inferior to start tracing the inferior. */
61 static void
62 netbsd_ptrace_fun ()
64 /* Switch child to its own process group so that signals won't
65 directly affect GDBserver. */
66 if (setpgid (0, 0) < 0)
67 trace_start_error_with_name (("setpgid"));
69 if (ptrace (PT_TRACE_ME, 0, nullptr, 0) < 0)
70 trace_start_error_with_name (("ptrace"));
72 /* If GDBserver is connected to gdb via stdio, redirect the inferior's
73 stdout to stderr so that inferior i/o doesn't corrupt the connection.
74 Also, redirect stdin to /dev/null. */
75 if (remote_connection_is_stdio ())
77 if (close (0) < 0)
78 trace_start_error_with_name (("close"));
79 if (open ("/dev/null", O_RDONLY) < 0)
80 trace_start_error_with_name (("open"));
81 if (dup2 (2, 1) < 0)
82 trace_start_error_with_name (("dup2"));
83 if (write (2, "stdin/stdout redirected\n",
84 sizeof ("stdin/stdout redirected\n") - 1) < 0)
86 /* Errors ignored. */
91 /* Implement the create_inferior method of the target_ops vector. */
93 int
94 netbsd_process_target::create_inferior (const char *program,
95 const std::vector<char *> &program_args)
97 std::string str_program_args = construct_inferior_arguments (program_args);
99 pid_t pid = fork_inferior (program, str_program_args.c_str (),
100 get_environ ()->envp (), netbsd_ptrace_fun,
101 nullptr, nullptr, nullptr, nullptr);
103 netbsd_add_process (pid, 0);
105 post_fork_inferior (pid, program);
107 return pid;
110 /* Implement the post_create_inferior target_ops method. */
112 void
113 netbsd_process_target::post_create_inferior ()
115 pid_t pid = current_process ()->pid;
116 netbsd_nat::enable_proc_events (pid);
118 low_arch_setup ();
121 /* Implement the attach target_ops method. */
124 netbsd_process_target::attach (unsigned long pid)
126 /* Unimplemented. */
127 return -1;
130 /* Returns true if GDB is interested in any child syscalls. */
132 static bool
133 gdb_catching_syscalls_p (pid_t pid)
135 struct process_info *proc = find_process_pid (pid);
136 return !proc->syscalls_to_catch.empty ();
139 /* Implement the resume target_ops method. */
141 void
142 netbsd_process_target::resume (struct thread_resume *resume_info, size_t n)
144 ptid_t resume_ptid = resume_info[0].thread;
145 const int signal = resume_info[0].sig;
146 const bool step = resume_info[0].kind == resume_step;
148 if (resume_ptid == minus_one_ptid)
149 resume_ptid = ptid_of (current_thread);
151 const pid_t pid = resume_ptid.pid ();
152 const lwpid_t lwp = resume_ptid.lwp ();
153 regcache_invalidate_pid (pid);
155 auto fn
156 = [&] (ptid_t ptid)
158 if (step)
160 if (ptid.lwp () == lwp || n != 1)
162 if (ptrace (PT_SETSTEP, pid, NULL, ptid.lwp ()) == -1)
163 perror_with_name (("ptrace"));
164 if (ptrace (PT_RESUME, pid, NULL, ptid.lwp ()) == -1)
165 perror_with_name (("ptrace"));
167 else
169 if (ptrace (PT_CLEARSTEP, pid, NULL, ptid.lwp ()) == -1)
170 perror_with_name (("ptrace"));
171 if (ptrace (PT_SUSPEND, pid, NULL, ptid.lwp ()) == -1)
172 perror_with_name (("ptrace"));
175 else
177 if (ptrace (PT_CLEARSTEP, pid, NULL, ptid.lwp ()) == -1)
178 perror_with_name (("ptrace"));
179 if (ptrace (PT_RESUME, pid, NULL, ptid.lwp ()) == -1)
180 perror_with_name (("ptrace"));
184 netbsd_nat::for_each_thread (pid, fn);
186 int request = gdb_catching_syscalls_p (pid) ? PT_CONTINUE : PT_SYSCALL;
188 errno = 0;
189 ptrace (request, pid, (void *)1, signal);
190 if (errno)
191 perror_with_name (("ptrace"));
194 /* Returns true if GDB is interested in the reported SYSNO syscall. */
196 static bool
197 netbsd_catch_this_syscall (int sysno)
199 struct process_info *proc = current_process ();
201 if (proc->syscalls_to_catch.empty ())
202 return false;
204 if (proc->syscalls_to_catch[0] == ANY_SYSCALL)
205 return true;
207 for (int iter : proc->syscalls_to_catch)
208 if (iter == sysno)
209 return true;
211 return false;
214 /* Helper function for child_wait and the derivatives of child_wait.
215 HOSTSTATUS is the waitstatus from wait() or the equivalent; store our
216 translation of that in OURSTATUS. */
218 static void
219 netbsd_store_waitstatus (struct target_waitstatus *ourstatus, int hoststatus)
221 if (WIFEXITED (hoststatus))
223 ourstatus->kind = TARGET_WAITKIND_EXITED;
224 ourstatus->value.integer = WEXITSTATUS (hoststatus);
226 else if (!WIFSTOPPED (hoststatus))
228 ourstatus->kind = TARGET_WAITKIND_SIGNALLED;
229 ourstatus->value.sig = gdb_signal_from_host (WTERMSIG (hoststatus));
231 else
233 ourstatus->kind = TARGET_WAITKIND_STOPPED;
234 ourstatus->value.sig = gdb_signal_from_host (WSTOPSIG (hoststatus));
238 /* Implement a safe wrapper around waitpid(). */
240 static pid_t
241 netbsd_waitpid (ptid_t ptid, struct target_waitstatus *ourstatus,
242 target_wait_flags target_options)
244 int status;
245 int options = (target_options & TARGET_WNOHANG) ? WNOHANG : 0;
247 pid_t pid
248 = gdb::handle_eintr<int> (-1, ::waitpid, ptid.pid (), &status, options);
250 if (pid == -1)
251 perror_with_name (_("Child process unexpectedly missing"));
253 netbsd_store_waitstatus (ourstatus, status);
254 return pid;
258 /* Implement the wait target_ops method.
260 Wait for the child specified by PTID to do something. Return the
261 process ID of the child, or MINUS_ONE_PTID in case of error; store
262 the status in *OURSTATUS. */
264 static ptid_t
265 netbsd_wait (ptid_t ptid, struct target_waitstatus *ourstatus,
266 target_wait_flags target_options)
268 pid_t pid = netbsd_waitpid (ptid, ourstatus, target_options);
269 ptid_t wptid = ptid_t (pid);
271 if (pid == 0)
273 gdb_assert (target_options & TARGET_WNOHANG);
274 ourstatus->kind = TARGET_WAITKIND_IGNORE;
275 return null_ptid;
278 gdb_assert (pid != -1);
280 /* If the child stopped, keep investigating its status. */
281 if (ourstatus->kind != TARGET_WAITKIND_STOPPED)
282 return wptid;
284 /* Extract the event and thread that received a signal. */
285 ptrace_siginfo_t psi;
286 if (ptrace (PT_GET_SIGINFO, pid, &psi, sizeof (psi)) == -1)
287 perror_with_name (("ptrace"));
289 /* Pick child's siginfo_t. */
290 siginfo_t *si = &psi.psi_siginfo;
292 lwpid_t lwp = psi.psi_lwpid;
294 int signo = si->si_signo;
295 const int code = si->si_code;
297 /* Construct PTID with a specified thread that received the event.
298 If a signal was targeted to the whole process, lwp is 0. */
299 wptid = ptid_t (pid, lwp, 0);
301 /* Bail out on non-debugger oriented signals. */
302 if (signo != SIGTRAP)
303 return wptid;
305 /* Stop examining non-debugger oriented SIGTRAP codes. */
306 if (code <= SI_USER || code == SI_NOINFO)
307 return wptid;
309 /* Process state for threading events. */
310 ptrace_state_t pst = {};
311 if (code == TRAP_LWP)
312 if (ptrace (PT_GET_PROCESS_STATE, pid, &pst, sizeof (pst)) == -1)
313 perror_with_name (("ptrace"));
315 if (code == TRAP_LWP && pst.pe_report_event == PTRACE_LWP_EXIT)
317 /* If GDB attaches to a multi-threaded process, exiting
318 threads might be skipped during post_attach that
319 have not yet reported their PTRACE_LWP_EXIT event.
320 Ignore exited events for an unknown LWP. */
321 thread_info *thr = find_thread_ptid (wptid);
322 if (thr == nullptr)
323 ourstatus->kind = TARGET_WAITKIND_SPURIOUS;
324 else
326 ourstatus->kind = TARGET_WAITKIND_THREAD_EXITED;
327 /* NetBSD does not store an LWP exit status. */
328 ourstatus->value.integer = 0;
330 remove_thread (thr);
332 return wptid;
335 if (find_thread_ptid (ptid_t (pid)))
336 current_thread = find_thread_ptid (wptid);
338 if (code == TRAP_LWP && pst.pe_report_event == PTRACE_LWP_CREATE)
340 /* If GDB attaches to a multi-threaded process, newborn
341 threads might be added by nbsd_add_threads that have
342 not yet reported their PTRACE_LWP_CREATE event. Ignore
343 born events for an already-known LWP. */
344 if (find_thread_ptid (wptid))
345 ourstatus->kind = TARGET_WAITKIND_SPURIOUS;
346 else
348 add_thread (wptid, NULL);
349 ourstatus->kind = TARGET_WAITKIND_THREAD_CREATED;
351 return wptid;
354 if (code == TRAP_EXEC)
356 ourstatus->kind = TARGET_WAITKIND_EXECD;
357 ourstatus->value.execd_pathname
358 = xstrdup (netbsd_nat::pid_to_exec_file (pid));
359 return wptid;
362 if (code == TRAP_TRACE)
363 return wptid;
365 if (code == TRAP_SCE || code == TRAP_SCX)
367 int sysnum = si->si_sysnum;
369 if (!netbsd_catch_this_syscall(sysnum))
371 /* If the core isn't interested in this event, ignore it. */
372 ourstatus->kind = TARGET_WAITKIND_SPURIOUS;
373 return wptid;
376 ourstatus->kind
377 = ((code == TRAP_SCE) ? TARGET_WAITKIND_SYSCALL_ENTRY :
378 TARGET_WAITKIND_SYSCALL_RETURN);
379 ourstatus->value.syscall_number = sysnum;
380 return wptid;
383 if (code == TRAP_BRKPT)
385 #ifdef PTRACE_BREAKPOINT_ADJ
386 CORE_ADDR pc;
387 struct reg r;
388 ptrace (PT_GETREGS, pid, &r, psi.psi_lwpid);
389 pc = PTRACE_REG_PC (&r);
390 PTRACE_REG_SET_PC (&r, pc - PTRACE_BREAKPOINT_ADJ);
391 ptrace (PT_SETREGS, pid, &r, psi.psi_lwpid);
392 #endif
393 return wptid;
396 /* Unclassified SIGTRAP event. */
397 ourstatus->kind = TARGET_WAITKIND_SPURIOUS;
398 return wptid;
401 /* Implement the wait target_ops method. */
403 ptid_t
404 netbsd_process_target::wait (ptid_t ptid, struct target_waitstatus *ourstatus,
405 target_wait_flags target_options)
407 while (true)
409 ptid_t wptid = netbsd_wait (ptid, ourstatus, target_options);
411 /* Register thread in the gdbcore if a thread was not reported earlier.
412 This is required after ::create_inferior, when the gdbcore does not
413 know about the first internal thread.
414 This may also happen on attach, when an event is registered on a thread
415 that was not fully initialized during the attach stage. */
416 if (wptid.lwp () != 0 && !find_thread_ptid (wptid)
417 && ourstatus->kind != TARGET_WAITKIND_THREAD_EXITED)
418 add_thread (wptid, nullptr);
420 switch (ourstatus->kind)
422 case TARGET_WAITKIND_EXITED:
423 case TARGET_WAITKIND_STOPPED:
424 case TARGET_WAITKIND_SIGNALLED:
425 case TARGET_WAITKIND_FORKED:
426 case TARGET_WAITKIND_VFORKED:
427 case TARGET_WAITKIND_EXECD:
428 case TARGET_WAITKIND_VFORK_DONE:
429 case TARGET_WAITKIND_SYSCALL_ENTRY:
430 case TARGET_WAITKIND_SYSCALL_RETURN:
431 /* Pass the result to the generic code. */
432 return wptid;
433 case TARGET_WAITKIND_THREAD_CREATED:
434 case TARGET_WAITKIND_THREAD_EXITED:
435 /* The core needlessly stops on these events. */
436 /* FALLTHROUGH */
437 case TARGET_WAITKIND_SPURIOUS:
438 /* Spurious events are unhandled by the gdbserver core. */
439 if (ptrace (PT_CONTINUE, current_process ()->pid, (void *) 1, 0)
440 == -1)
441 perror_with_name (("ptrace"));
442 break;
443 default:
444 error (("Unknown stopped status"));
449 /* Implement the kill target_ops method. */
452 netbsd_process_target::kill (process_info *process)
454 pid_t pid = process->pid;
455 if (ptrace (PT_KILL, pid, nullptr, 0) == -1)
456 return -1;
458 int status;
459 if (gdb::handle_eintr<int> (-1, ::waitpid, pid, &status, 0) == -1)
460 return -1;
461 mourn (process);
462 return 0;
465 /* Implement the detach target_ops method. */
468 netbsd_process_target::detach (process_info *process)
470 pid_t pid = process->pid;
472 ptrace (PT_DETACH, pid, (void *) 1, 0);
473 mourn (process);
474 return 0;
477 /* Implement the mourn target_ops method. */
479 void
480 netbsd_process_target::mourn (struct process_info *proc)
482 for_each_thread (proc->pid, remove_thread);
484 remove_process (proc);
487 /* Implement the join target_ops method. */
489 void
490 netbsd_process_target::join (int pid)
492 /* The PT_DETACH is sufficient to detach from the process.
493 So no need to do anything extra. */
496 /* Implement the thread_alive target_ops method. */
498 bool
499 netbsd_process_target::thread_alive (ptid_t ptid)
501 return netbsd_nat::thread_alive (ptid);
504 /* Implement the fetch_registers target_ops method. */
506 void
507 netbsd_process_target::fetch_registers (struct regcache *regcache, int regno)
509 const netbsd_regset_info *regset = get_regs_info ();
510 ptid_t inferior_ptid = ptid_of (current_thread);
512 while (regset->size >= 0)
514 std::vector<char> buf;
515 buf.resize (regset->size);
516 int res = ptrace (regset->get_request, inferior_ptid.pid (), buf.data (),
517 inferior_ptid.lwp ());
518 if (res == -1)
519 perror_with_name (("ptrace"));
520 regset->store_function (regcache, buf.data ());
521 regset++;
525 /* Implement the store_registers target_ops method. */
527 void
528 netbsd_process_target::store_registers (struct regcache *regcache, int regno)
530 const netbsd_regset_info *regset = get_regs_info ();
531 ptid_t inferior_ptid = ptid_of (current_thread);
533 while (regset->size >= 0)
535 std::vector<char> buf;
536 buf.resize (regset->size);
537 int res = ptrace (regset->get_request, inferior_ptid.pid (), buf.data (),
538 inferior_ptid.lwp ());
539 if (res == -1)
540 perror_with_name (("ptrace"));
542 /* Then overlay our cached registers on that. */
543 regset->fill_function (regcache, buf.data ());
544 /* Only now do we write the register set. */
545 res = ptrace (regset->set_request, inferior_ptid.pid (), buf. data (),
546 inferior_ptid.lwp ());
547 if (res == -1)
548 perror_with_name (("ptrace"));
549 regset++;
553 /* Implement the read_memory target_ops method. */
556 netbsd_process_target::read_memory (CORE_ADDR memaddr, unsigned char *myaddr,
557 int size)
559 pid_t pid = current_process ()->pid;
560 return netbsd_nat::read_memory (pid, myaddr, memaddr, size, nullptr);
563 /* Implement the write_memory target_ops method. */
566 netbsd_process_target::write_memory (CORE_ADDR memaddr,
567 const unsigned char *myaddr, int size)
569 pid_t pid = current_process ()->pid;
570 return netbsd_nat::write_memory (pid, myaddr, memaddr, size, nullptr);
573 /* Implement the request_interrupt target_ops method. */
575 void
576 netbsd_process_target::request_interrupt ()
578 ptid_t inferior_ptid = ptid_of (get_first_thread ());
580 ::kill (inferior_ptid.pid (), SIGINT);
583 /* Read the AUX Vector for the specified PID, wrapping the ptrace(2) call
584 with the PIOD_READ_AUXV operation and using the PT_IO standard input
585 and output arguments. */
587 static size_t
588 netbsd_read_auxv(pid_t pid, void *offs, void *addr, size_t len)
590 struct ptrace_io_desc pio;
592 pio.piod_op = PIOD_READ_AUXV;
593 pio.piod_offs = offs;
594 pio.piod_addr = addr;
595 pio.piod_len = len;
597 if (ptrace (PT_IO, pid, &pio, 0) == -1)
598 perror_with_name (("ptrace"));
600 return pio.piod_len;
603 /* Copy LEN bytes from inferior's auxiliary vector starting at OFFSET
604 to debugger memory starting at MYADDR. */
607 netbsd_process_target::read_auxv (CORE_ADDR offset,
608 unsigned char *myaddr, unsigned int len)
610 pid_t pid = pid_of (current_thread);
612 return netbsd_read_auxv (pid, (void *) (intptr_t) offset, myaddr, len);
615 bool
616 netbsd_process_target::supports_z_point_type (char z_type)
618 switch (z_type)
620 case Z_PACKET_SW_BP:
621 return true;
622 case Z_PACKET_HW_BP:
623 case Z_PACKET_WRITE_WP:
624 case Z_PACKET_READ_WP:
625 case Z_PACKET_ACCESS_WP:
626 default:
627 return false; /* Not supported. */
631 /* Insert {break/watch}point at address ADDR. SIZE is not used. */
634 netbsd_process_target::insert_point (enum raw_bkpt_type type, CORE_ADDR addr,
635 int size, struct raw_breakpoint *bp)
637 switch (type)
639 case raw_bkpt_type_sw:
640 return insert_memory_breakpoint (bp);
641 case raw_bkpt_type_hw:
642 case raw_bkpt_type_write_wp:
643 case raw_bkpt_type_read_wp:
644 case raw_bkpt_type_access_wp:
645 default:
646 return 1; /* Not supported. */
650 /* Remove {break/watch}point at address ADDR. SIZE is not used. */
653 netbsd_process_target::remove_point (enum raw_bkpt_type type, CORE_ADDR addr,
654 int size, struct raw_breakpoint *bp)
656 switch (type)
658 case raw_bkpt_type_sw:
659 return remove_memory_breakpoint (bp);
660 case raw_bkpt_type_hw:
661 case raw_bkpt_type_write_wp:
662 case raw_bkpt_type_read_wp:
663 case raw_bkpt_type_access_wp:
664 default:
665 return 1; /* Not supported. */
669 /* Implement the stopped_by_sw_breakpoint target_ops method. */
671 bool
672 netbsd_process_target::stopped_by_sw_breakpoint ()
674 ptrace_siginfo_t psi;
675 pid_t pid = current_process ()->pid;
677 if (ptrace (PT_GET_SIGINFO, pid, &psi, sizeof (psi)) == -1)
678 perror_with_name (("ptrace"));
680 return psi.psi_siginfo.si_signo == SIGTRAP &&
681 psi.psi_siginfo.si_code == TRAP_BRKPT;
684 /* Implement the supports_stopped_by_sw_breakpoint target_ops method. */
686 bool
687 netbsd_process_target::supports_stopped_by_sw_breakpoint ()
689 return true;
692 /* Implement the supports_qxfer_siginfo target_ops method. */
694 bool
695 netbsd_process_target::supports_qxfer_siginfo ()
697 return true;
700 /* Implement the qxfer_siginfo target_ops method. */
703 netbsd_process_target::qxfer_siginfo (const char *annex, unsigned char *readbuf,
704 unsigned const char *writebuf,
705 CORE_ADDR offset, int len)
707 if (current_thread == nullptr)
708 return -1;
710 pid_t pid = current_process ()->pid;
712 return netbsd_nat::qxfer_siginfo(pid, annex, readbuf, writebuf, offset, len);
715 /* Implement the supports_non_stop target_ops method. */
717 bool
718 netbsd_process_target::supports_non_stop ()
720 return false;
723 /* Implement the supports_multi_process target_ops method. */
725 bool
726 netbsd_process_target::supports_multi_process ()
728 return true;
731 /* Check if fork events are supported. */
733 bool
734 netbsd_process_target::supports_fork_events ()
736 return false;
739 /* Check if vfork events are supported. */
741 bool
742 netbsd_process_target::supports_vfork_events ()
744 return false;
747 /* Check if exec events are supported. */
749 bool
750 netbsd_process_target::supports_exec_events ()
752 return true;
755 /* Implement the supports_disable_randomization target_ops method. */
757 bool
758 netbsd_process_target::supports_disable_randomization ()
760 return false;
763 /* Extract &phdr and num_phdr in the inferior. Return 0 on success. */
765 template <typename T>
766 int get_phdr_phnum_from_proc_auxv (const pid_t pid,
767 CORE_ADDR *phdr_memaddr, int *num_phdr)
769 typedef typename std::conditional<sizeof(T) == sizeof(int64_t),
770 Aux64Info, Aux32Info>::type auxv_type;
771 const size_t auxv_size = sizeof (auxv_type);
772 const size_t auxv_buf_size = 128 * sizeof (auxv_type);
774 std::vector<char> auxv_buf;
775 auxv_buf.resize (auxv_buf_size);
777 netbsd_read_auxv (pid, nullptr, auxv_buf.data (), auxv_buf_size);
779 *phdr_memaddr = 0;
780 *num_phdr = 0;
782 for (char *buf = auxv_buf.data ();
783 buf < (auxv_buf.data () + auxv_buf_size);
784 buf += auxv_size)
786 auxv_type *const aux = (auxv_type *) buf;
788 switch (aux->a_type)
790 case AT_PHDR:
791 *phdr_memaddr = aux->a_v;
792 break;
793 case AT_PHNUM:
794 *num_phdr = aux->a_v;
795 break;
798 if (*phdr_memaddr != 0 && *num_phdr != 0)
799 break;
802 if (*phdr_memaddr == 0 || *num_phdr == 0)
804 warning ("Unexpected missing AT_PHDR and/or AT_PHNUM: "
805 "phdr_memaddr = %s, phdr_num = %d",
806 core_addr_to_string (*phdr_memaddr), *num_phdr);
807 return 2;
810 return 0;
813 /* Return &_DYNAMIC (via PT_DYNAMIC) in the inferior, or 0 if not present. */
815 template <typename T>
816 static CORE_ADDR
817 get_dynamic (const pid_t pid)
819 typedef typename std::conditional<sizeof(T) == sizeof(int64_t),
820 Elf64_Phdr, Elf32_Phdr>::type phdr_type;
821 const int phdr_size = sizeof (phdr_type);
823 CORE_ADDR phdr_memaddr;
824 int num_phdr;
825 if (get_phdr_phnum_from_proc_auxv<T> (pid, &phdr_memaddr, &num_phdr))
826 return 0;
828 std::vector<unsigned char> phdr_buf;
829 phdr_buf.resize (num_phdr * phdr_size);
831 if (netbsd_nat::read_memory (pid, phdr_buf.data (), phdr_memaddr,
832 phdr_buf.size (), nullptr))
833 return 0;
835 /* Compute relocation: it is expected to be 0 for "regular" executables,
836 non-zero for PIE ones. */
837 CORE_ADDR relocation = -1;
838 for (int i = 0; relocation == -1 && i < num_phdr; i++)
840 phdr_type *const p = (phdr_type *) (phdr_buf.data () + i * phdr_size);
842 if (p->p_type == PT_PHDR)
843 relocation = phdr_memaddr - p->p_vaddr;
846 if (relocation == -1)
848 /* PT_PHDR is optional, but necessary for PIE in general. Fortunately
849 any real world executables, including PIE executables, have always
850 PT_PHDR present. PT_PHDR is not present in some shared libraries or
851 in fpc (Free Pascal 2.4) binaries but neither of those have a need for
852 or present DT_DEBUG anyway (fpc binaries are statically linked).
854 Therefore if there exists DT_DEBUG there is always also PT_PHDR.
856 GDB could find RELOCATION also from AT_ENTRY - e_entry. */
858 return 0;
861 for (int i = 0; i < num_phdr; i++)
863 phdr_type *const p = (phdr_type *) (phdr_buf.data () + i * phdr_size);
865 if (p->p_type == PT_DYNAMIC)
866 return p->p_vaddr + relocation;
869 return 0;
872 /* Return &_r_debug in the inferior, or -1 if not present. Return value
873 can be 0 if the inferior does not yet have the library list initialized.
874 We look for DT_MIPS_RLD_MAP first. MIPS executables use this instead of
875 DT_DEBUG, although they sometimes contain an unused DT_DEBUG entry too. */
877 template <typename T>
878 static CORE_ADDR
879 get_r_debug (const pid_t pid)
881 typedef typename std::conditional<sizeof(T) == sizeof(int64_t),
882 Elf64_Dyn, Elf32_Dyn>::type dyn_type;
883 const int dyn_size = sizeof (dyn_type);
884 unsigned char buf[sizeof (dyn_type)]; /* The larger of the two. */
885 CORE_ADDR map = -1;
887 CORE_ADDR dynamic_memaddr = get_dynamic<T> (pid);
888 if (dynamic_memaddr == 0)
889 return map;
891 while (netbsd_nat::read_memory (pid, buf, dynamic_memaddr, dyn_size, nullptr)
892 == 0)
894 dyn_type *const dyn = (dyn_type *) buf;
895 #if defined DT_MIPS_RLD_MAP
896 union
898 T map;
899 unsigned char buf[sizeof (T)];
901 rld_map;
903 if (dyn->d_tag == DT_MIPS_RLD_MAP)
905 if (netbsd_nat::read_memory (pid, rld_map.buf, dyn->d_un.d_val,
906 sizeof (rld_map.buf), nullptr) == 0)
907 return rld_map.map;
908 else
909 break;
911 #endif /* DT_MIPS_RLD_MAP */
913 if (dyn->d_tag == DT_DEBUG && map == -1)
914 map = dyn->d_un.d_val;
916 if (dyn->d_tag == DT_NULL)
917 break;
919 dynamic_memaddr += dyn_size;
922 return map;
925 /* Read one pointer from MEMADDR in the inferior. */
927 static int
928 read_one_ptr (const pid_t pid, CORE_ADDR memaddr, CORE_ADDR *ptr, int ptr_size)
930 /* Go through a union so this works on either big or little endian
931 hosts, when the inferior's pointer size is smaller than the size
932 of CORE_ADDR. It is assumed the inferior's endianness is the
933 same of the superior's. */
935 union
937 CORE_ADDR core_addr;
938 unsigned int ui;
939 unsigned char uc;
940 } addr;
942 int ret = netbsd_nat::read_memory (pid, &addr.uc, memaddr, ptr_size, nullptr);
943 if (ret == 0)
945 if (ptr_size == sizeof (CORE_ADDR))
946 *ptr = addr.core_addr;
947 else if (ptr_size == sizeof (unsigned int))
948 *ptr = addr.ui;
949 else
950 gdb_assert_not_reached ("unhandled pointer size");
952 return ret;
955 /* Construct qXfer:libraries-svr4:read reply. */
957 template <typename T>
959 netbsd_qxfer_libraries_svr4 (const pid_t pid, const char *annex,
960 unsigned char *readbuf,
961 unsigned const char *writebuf,
962 CORE_ADDR offset, int len)
964 struct link_map_offsets
966 /* Offset and size of r_debug.r_version. */
967 int r_version_offset;
969 /* Offset and size of r_debug.r_map. */
970 int r_map_offset;
972 /* Offset to l_addr field in struct link_map. */
973 int l_addr_offset;
975 /* Offset to l_name field in struct link_map. */
976 int l_name_offset;
978 /* Offset to l_ld field in struct link_map. */
979 int l_ld_offset;
981 /* Offset to l_next field in struct link_map. */
982 int l_next_offset;
984 /* Offset to l_prev field in struct link_map. */
985 int l_prev_offset;
988 static const struct link_map_offsets lmo_32bit_offsets =
990 0, /* r_version offset. */
991 4, /* r_debug.r_map offset. */
992 0, /* l_addr offset in link_map. */
993 4, /* l_name offset in link_map. */
994 8, /* l_ld offset in link_map. */
995 12, /* l_next offset in link_map. */
996 16 /* l_prev offset in link_map. */
999 static const struct link_map_offsets lmo_64bit_offsets =
1001 0, /* r_version offset. */
1002 8, /* r_debug.r_map offset. */
1003 0, /* l_addr offset in link_map. */
1004 8, /* l_name offset in link_map. */
1005 16, /* l_ld offset in link_map. */
1006 24, /* l_next offset in link_map. */
1007 32 /* l_prev offset in link_map. */
1010 CORE_ADDR lm_addr = 0, lm_prev = 0;
1011 CORE_ADDR l_name, l_addr, l_ld, l_next, l_prev;
1012 int header_done = 0;
1014 const struct link_map_offsets *lmo
1015 = ((sizeof (T) == sizeof (int64_t))
1016 ? &lmo_64bit_offsets : &lmo_32bit_offsets);
1017 int ptr_size = sizeof (T);
1019 while (annex[0] != '\0')
1021 const char *sep = strchr (annex, '=');
1022 if (sep == nullptr)
1023 break;
1025 int name_len = sep - annex;
1026 CORE_ADDR *addrp;
1027 if (name_len == 5 && startswith (annex, "start"))
1028 addrp = &lm_addr;
1029 else if (name_len == 4 && startswith (annex, "prev"))
1030 addrp = &lm_prev;
1031 else
1033 annex = strchr (sep, ';');
1034 if (annex == nullptr)
1035 break;
1036 annex++;
1037 continue;
1040 annex = decode_address_to_semicolon (addrp, sep + 1);
1043 if (lm_addr == 0)
1045 CORE_ADDR r_debug = get_r_debug<T> (pid);
1047 /* We failed to find DT_DEBUG. Such situation will not change
1048 for this inferior - do not retry it. Report it to GDB as
1049 E01, see for the reasons at the GDB solib-svr4.c side. */
1050 if (r_debug == (CORE_ADDR) -1)
1051 return -1;
1053 if (r_debug != 0)
1055 CORE_ADDR map_offset = r_debug + lmo->r_map_offset;
1056 if (read_one_ptr (pid, map_offset, &lm_addr, ptr_size) != 0)
1057 warning ("unable to read r_map from %s",
1058 core_addr_to_string (map_offset));
1062 std::string document = "<library-list-svr4 version=\"1.0\"";
1064 while (lm_addr
1065 && read_one_ptr (pid, lm_addr + lmo->l_name_offset,
1066 &l_name, ptr_size) == 0
1067 && read_one_ptr (pid, lm_addr + lmo->l_addr_offset,
1068 &l_addr, ptr_size) == 0
1069 && read_one_ptr (pid, lm_addr + lmo->l_ld_offset,
1070 &l_ld, ptr_size) == 0
1071 && read_one_ptr (pid, lm_addr + lmo->l_prev_offset,
1072 &l_prev, ptr_size) == 0
1073 && read_one_ptr (pid, lm_addr + lmo->l_next_offset,
1074 &l_next, ptr_size) == 0)
1076 if (lm_prev != l_prev)
1078 warning ("Corrupted shared library list: 0x%lx != 0x%lx",
1079 (long) lm_prev, (long) l_prev);
1080 break;
1083 /* Ignore the first entry even if it has valid name as the first entry
1084 corresponds to the main executable. The first entry should not be
1085 skipped if the dynamic loader was loaded late by a static executable
1086 (see solib-svr4.c parameter ignore_first). But in such case the main
1087 executable does not have PT_DYNAMIC present and this function already
1088 exited above due to failed get_r_debug. */
1089 if (lm_prev == 0)
1090 string_appendf (document, " main-lm=\"0x%lx\"",
1091 (unsigned long) lm_addr);
1092 else
1094 unsigned char libname[PATH_MAX];
1096 /* Not checking for error because reading may stop before
1097 we've got PATH_MAX worth of characters. */
1098 libname[0] = '\0';
1099 netbsd_nat::read_memory (pid, libname, l_name, sizeof (libname) - 1,
1100 nullptr);
1101 libname[sizeof (libname) - 1] = '\0';
1102 if (libname[0] != '\0')
1104 if (!header_done)
1106 /* Terminate `<library-list-svr4'. */
1107 document += '>';
1108 header_done = 1;
1111 string_appendf (document, "<library name=\"");
1112 xml_escape_text_append (&document, (char *) libname);
1113 string_appendf (document, "\" lm=\"0x%lx\" "
1114 "l_addr=\"0x%lx\" l_ld=\"0x%lx\"/>",
1115 (unsigned long) lm_addr, (unsigned long) l_addr,
1116 (unsigned long) l_ld);
1120 lm_prev = lm_addr;
1121 lm_addr = l_next;
1124 if (!header_done)
1126 /* Empty list; terminate `<library-list-svr4'. */
1127 document += "/>";
1129 else
1130 document += "</library-list-svr4>";
1132 int document_len = document.length ();
1133 if (offset < document_len)
1134 document_len -= offset;
1135 else
1136 document_len = 0;
1137 if (len > document_len)
1138 len = document_len;
1140 memcpy (readbuf, document.data () + offset, len);
1142 return len;
1145 /* Return true if FILE is a 64-bit ELF file,
1146 false if the file is not a 64-bit ELF file,
1147 and error if the file is not accessible or doesn't exist. */
1149 static bool
1150 elf_64_file_p (const char *file)
1152 int fd = gdb::handle_eintr<int> (-1, ::open, file, O_RDONLY);
1153 if (fd < 0)
1154 perror_with_name (("open"));
1156 Elf64_Ehdr header;
1157 ssize_t ret = gdb::handle_eintr<ssize_t> (-1, ::read, fd, &header, sizeof (header));
1158 if (ret == -1)
1159 perror_with_name (("read"));
1160 gdb::handle_eintr<int> (-1, ::close, fd);
1161 if (ret != sizeof (header))
1162 error ("Cannot read ELF file header: %s", file);
1164 if (header.e_ident[EI_MAG0] != ELFMAG0
1165 || header.e_ident[EI_MAG1] != ELFMAG1
1166 || header.e_ident[EI_MAG2] != ELFMAG2
1167 || header.e_ident[EI_MAG3] != ELFMAG3)
1168 error ("Unrecognized ELF file header: %s", file);
1170 return header.e_ident[EI_CLASS] == ELFCLASS64;
1173 /* Construct qXfer:libraries-svr4:read reply. */
1176 netbsd_process_target::qxfer_libraries_svr4 (const char *annex,
1177 unsigned char *readbuf,
1178 unsigned const char *writebuf,
1179 CORE_ADDR offset, int len)
1181 if (writebuf != nullptr)
1182 return -2;
1183 if (readbuf == nullptr)
1184 return -1;
1186 struct process_info *proc = current_process ();
1187 pid_t pid = proc->pid;
1188 bool is_elf64 = elf_64_file_p (netbsd_nat::pid_to_exec_file (pid));
1190 if (is_elf64)
1191 return netbsd_qxfer_libraries_svr4<int64_t> (pid, annex, readbuf,
1192 writebuf, offset, len);
1193 else
1194 return netbsd_qxfer_libraries_svr4<int32_t> (pid, annex, readbuf,
1195 writebuf, offset, len);
1198 /* Implement the supports_qxfer_libraries_svr4 target_ops method. */
1200 bool
1201 netbsd_process_target::supports_qxfer_libraries_svr4 ()
1203 return true;
1206 /* Return the name of a file that can be opened to get the symbols for
1207 the child process identified by PID. */
1209 char *
1210 netbsd_process_target::pid_to_exec_file (pid_t pid)
1212 return const_cast<char *> (netbsd_nat::pid_to_exec_file (pid));
1215 /* Implementation of the target_ops method "supports_pid_to_exec_file". */
1217 bool
1218 netbsd_process_target::supports_pid_to_exec_file ()
1220 return true;
1223 /* Implementation of the target_ops method "supports_hardware_single_step". */
1224 bool
1225 netbsd_process_target::supports_hardware_single_step ()
1227 return true;
1230 /* Implementation of the target_ops method "sw_breakpoint_from_kind". */
1232 const gdb_byte *
1233 netbsd_process_target::sw_breakpoint_from_kind (int kind, int *size)
1235 static gdb_byte brkpt[PTRACE_BREAKPOINT_SIZE] = {*PTRACE_BREAKPOINT};
1237 *size = PTRACE_BREAKPOINT_SIZE;
1239 return brkpt;
1242 /* Implement the thread_name target_ops method. */
1244 const char *
1245 netbsd_process_target::thread_name (ptid_t ptid)
1247 return netbsd_nat::thread_name (ptid);
1250 /* Implement the supports_catch_syscall target_ops method. */
1252 bool
1253 netbsd_process_target::supports_catch_syscall ()
1255 return true;
1258 /* Implement the supports_read_auxv target_ops method. */
1260 bool
1261 netbsd_process_target::supports_read_auxv ()
1263 return true;
1266 void
1267 initialize_low ()
1269 set_target_ops (the_netbsd_target);