From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail.netbsd.org (mail.netbsd.org [199.233.217.200]) by sourceware.org (Postfix) with ESMTPS id 23FAE3986410 for ; Fri, 4 Sep 2020 00:16:02 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 23FAE3986410 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=netbsd.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=kamil@netbsd.org Received: from [IPv6:::1] (localhost [127.0.0.1]) by mail.netbsd.org (Postfix) with ESMTP id A123584D6F; Fri, 4 Sep 2020 00:15:59 +0000 (UTC) To: "Aktemur, Tankut Baris" , "gdb-patches@sourceware.org" References: <20200902175926.13182-1-n54@gmx.com> <20200902175926.13182-11-n54@gmx.com> From: Kamil Rytarowski Autocrypt: addr=kamil@netbsd.org; keydata= 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 Subject: Re: [PATCH 10/10] Add minimal and functional NetBSD/amd64 gdbserver Message-ID: <0faf3e36-bad7-545d-a939-99badc20e5f4@netbsd.org> Date: Fri, 4 Sep 2020 02:13:03 +0200 User-Agent: Mozilla/5.0 (X11; NetBSD amd64; rv:68.0) Gecko/20100101 Thunderbird/68.4.1 MIME-Version: 1.0 In-Reply-To: Content-Type: multipart/signed; micalg=pgp-sha256; protocol="application/pgp-signature"; boundary="jYtdwdlo4mWpXnnLd7hHMiolvhDRb3R5e" X-Spam-Status: No, score=-10.3 required=5.0 tests=BAYES_00, GIT_PATCH_0, KAM_DMARC_STATUS, KAM_SHORT, NICE_REPLY_A, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 04 Sep 2020 00:16:09 -0000 This is an OpenPGP/MIME signed message (RFC 4880 and 3156) --jYtdwdlo4mWpXnnLd7hHMiolvhDRb3R5e Content-Type: multipart/mixed; boundary="5jrBwy0cyQdMlXWc9IJxeCiSOzOTBshCT"; protected-headers="v1" From: Kamil Rytarowski To: "Aktemur, Tankut Baris" , "gdb-patches@sourceware.org" Message-ID: <0faf3e36-bad7-545d-a939-99badc20e5f4@netbsd.org> Subject: Re: [PATCH 10/10] Add minimal and functional NetBSD/amd64 gdbserver References: <20200902175926.13182-1-n54@gmx.com> <20200902175926.13182-11-n54@gmx.com> In-Reply-To: --5jrBwy0cyQdMlXWc9IJxeCiSOzOTBshCT Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: quoted-printable On 03.09.2020 19:42, Aktemur, Tankut Baris wrote: > On September 2, 2020 7:59 PM, Kamil Rytarowski wrote: >> Implement the following functionality: create_inferior, >> post_create_inferior, attach, kill, detach, mourn, join, thread_alive,= >> resume, wait, fetch_registers, store_registers, read_memory, write_mem= ory, >> request_interrupt, supports_read_auxv, read_auxv, >> supports_hardware_single_step, sw_breakpoint_from_kind, >> supports_z_point_type, insert_point, remove_point, >> stopped_by_sw_breakpoint, supports_qxfer_siginfo, qxfer_siginfo, >> supports_stopped_by_sw_breakpoint, supports_non_stop, >> supports_multi_process, supports_fork_events, supports_vfork_events, >> supports_exec_events, supports_disable_randomization, >> supports_qxfer_libraries_svr4, qxfer_libraries_svr4, >> supports_pid_to_exec_file, pid_to_exec_file, thread_name, >> supports_catch_syscall. >> >> The only CPU architecture supported: x86_64. >> >> Implement only support for hardware assisted single step and >> software breakpoint. >> >> Implement support only for regular X86 registers, thus no FPU. >> >> gdb/ChangeLog: >=20 > This should be gdbserver/ChangeLog. >=20 Fixed. >> >> * netbsd-low.cc: Add. >> * netbsd-low.h: Likewise. >> * netbsd-x86_64-low.cc: Likewise. >> * Makefile.in (SFILES): Register "netbsd-low.cc", "netbsd-low.h= ", >> "netbsd-x86_64-low.cc". >> * configure.srv: Add x86_64-*-netbsd*. >> --- >> gdbserver/ChangeLog | 9 + >> gdbserver/Makefile.in | 3 + >> gdbserver/configure.srv | 7 + >> gdbserver/netbsd-low.cc | 1352 +++++++++++++++++++++++++++++++= + >> gdbserver/netbsd-low.h | 157 ++++ >> gdbserver/netbsd-x86_64-low.cc | 250 ++++++ >> 6 files changed, 1778 insertions(+) >> create mode 100644 gdbserver/netbsd-low.cc >> create mode 100644 gdbserver/netbsd-low.h >> create mode 100644 gdbserver/netbsd-x86_64-low.cc >> >> diff --git a/gdbserver/ChangeLog b/gdbserver/ChangeLog >> index e437493b56d..c0732d1a28a 100644 >> --- a/gdbserver/ChangeLog >> +++ b/gdbserver/ChangeLog >> @@ -1,3 +1,12 @@ >> +2020-08-13 Kamil Rytarowski >> + >> + * netbsd-low.cc: Add. >> + * netbsd-low.h: Likewise. >> + * netbsd-x86_64-low.cc: Likewise. >> + * Makefile.in (SFILES): Register "netbsd-low.cc", "netbsd-low.h", >> + "netbsd-x86_64-low.cc". >> + * configure.srv: Add x86_64-*-netbsd*. >> + >> 2020-08-13 Simon Marchi >> >> * server.cc (captured_main): Accept multiple `--selftest=3D` >> diff --git a/gdbserver/Makefile.in b/gdbserver/Makefile.in >> index 9d7687be534..1d597b14523 100644 >> --- a/gdbserver/Makefile.in >> +++ b/gdbserver/Makefile.in >> @@ -193,6 +193,9 @@ SFILES =3D \ >> $(srcdir)/linux-x86-low.cc \ >> $(srcdir)/linux-xtensa-low.cc \ >> $(srcdir)/mem-break.cc \ >> + $(srcdir)/netbsd-low.cc \ >> + $(srcdir)/netbsd-low.h \ >> + $(srcdir)/netbsd-x86_64-low.cc \ >> $(srcdir)/proc-service.cc \ >> $(srcdir)/proc-service.list \ >> $(srcdir)/regcache.cc \ >> diff --git a/gdbserver/configure.srv b/gdbserver/configure.srv >> index 5e33bd9c54d..12f9d410b7c 100644 >> --- a/gdbserver/configure.srv >> +++ b/gdbserver/configure.srv >> @@ -349,6 +349,13 @@ case "${gdbserver_host}" in >> srv_tgtobj=3D"${srv_tgtobj} nat/windows-nat.o" >> srv_tgtobj=3D"${srv_tgtobj} arch/amd64.o arch/i386.o" >> ;; >> + x86_64-*-netbsd*) srv_regobj=3D"" >> + srv_tgtobj=3D"netbsd-low.o netbsd-x86_64-low.o fork-child.o" >> + srv_tgtobj=3D"${srv_tgtobj} nat/fork-inferior.o" >> + srv_tgtobj=3D"${srv_tgtobj} nat/netbsd-nat.o" >> + srv_tgtobj=3D"${srv_tgtobj} arch/amd64.o" >> + srv_netbsd=3Dyes >=20 > Is `srv_netbsd` used anywhere? >=20 Not used. I have removed it. Other targets use it in configure.ac and this is not needed for NetBDS. >> + ;; >> >> xtensa*-*-linux*) srv_regobj=3Dreg-xtensa.o >> srv_tgtobj=3D"$srv_linux_obj linux-xtensa-low.o" >> diff --git a/gdbserver/netbsd-low.cc b/gdbserver/netbsd-low.cc >> new file mode 100644 >> index 00000000000..db9531c66d3 >> --- /dev/null >> +++ b/gdbserver/netbsd-low.cc >> @@ -0,0 +1,1352 @@ >> +/* Copyright (C) 2020 Free Software Foundation, Inc. >> + >> + This file is part of GDB. >> + >> + This program is free software; you can redistribute it and/or modi= fy >> + it under the terms of the GNU General Public License as published = by >> + the Free Software Foundation; either version 3 of the License, or >> + (at your option) any later version. >> + >> + This program is distributed in the hope that it will be useful, >> + but WITHOUT ANY WARRANTY; without even the implied warranty of >> + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the >> + GNU General Public License for more details. >> + >> + You should have received a copy of the GNU General Public License >> + along with this program. If not, see . */ >> + >> +#include "server.h" >> +#include "target.h" >> +#include "netbsd-low.h" >> +#include "nat/netbsd-nat.h" >> + >> +#include >> +#include >> + >> +#include >> +#include >> + >> +#include >> +#include >> +#include >> + >> +#include >> + >> +#include >> + >> +#include "gdbsupport/eintr.h" >> +#include "gdbsupport/gdb_wait.h" >> +#include "gdbsupport/filestuff.h" >> +#include "gdbsupport/common-inferior.h" >> +#include "nat/fork-inferior.h" >> +#include "hostio.h" >> + >> +int using_threads =3D 1; >> + >> +const struct target_desc *netbsd_tdesc; >> + >> +/* Call add_process with the given parameters, and initializes >=20 > To be consistent with the imperative "Call", "initializes" could read > "initialize". >=20 Fixed. >> + the process' private data. */ >> + >> +static struct process_info * >> +netbsd_add_process (int pid, int attached) >=20 > There are two uses of this function and in both the return value is ign= ored. > The function could as well be void. >=20 Done. >> +{ >> + struct process_info *proc =3D add_process (pid, attached); >> + proc->tdesc =3D netbsd_tdesc; >> + proc->priv =3D nullptr; >> + >> + return proc; >> +} >> + >> +/* Callback used by fork_inferior to start tracing the inferior. */ >> + >> +static void >> +netbsd_ptrace_fun () >> +{ >> + /* Switch child to its own process group so that signals won't >> + directly affect GDBserver. */ >> + if (setpgid (0, 0) < 0) >> + trace_start_error_with_name (("setpgid")); >> + >> + if (ptrace (PT_TRACE_ME, 0, nullptr, 0) < 0) >> + trace_start_error_with_name (("ptrace")); >> + >> + /* If GDBserver is connected to gdb via stdio, redirect the inferio= r's >> + stdout to stderr so that inferior i/o doesn't corrupt the connec= tion. >> + Also, redirect stdin to /dev/null. */ >> + if (remote_connection_is_stdio ()) >> + { >> + if (close (0) < 0) >> + trace_start_error_with_name (("close")); >> + if (open ("/dev/null", O_RDONLY) < 0) >> + trace_start_error_with_name (("open")); >> + if (dup2 (2, 1) < 0) >> + trace_start_error_with_name (("dup2")); >> + if (write (2, "stdin/stdout redirected\n", >> + sizeof ("stdin/stdout redirected\n") - 1) < 0) >> + { >> + /* Errors ignored. */; >=20 > Any particular reason for the explicit ';'? >=20 Copied from Linux. I've removed the strat semicolon. gdbserver/linux-low.cc: /* Errors ignored. */; >> + } >> + } >> +} >> + >> +/* Register threads in the process specified by PID. */ >> + >> +static void >> +netbsd_add_threads (pid_t pid) >> +{ >> + auto fn >> + =3D [] (ptid_t ptid) >> + { >> + add_thread (ptid, nullptr); >> + }; >> + >> + netbsd_nat::list_threads (pid, fn); >=20 > IMHO, the name "list_threads" gives the impression that it only lists t= he threads > and is side-effect free. Maybe use something like "for_each_thread", a= s in gdbthread.h? >=20 I've renamed it to for_each_thread. >> +} >> + >> +/* Implement the create_inferior method of the target_ops vector. */= >> + >> +int >> +netbsd_process_target::create_inferior (const char *program, >> + const std::vector &program_args) >> +{ >> + std::string str_program_args =3D construct_inferior_arguments (prog= ram_args); >> + >> + pid_t pid =3D fork_inferior (program, str_program_args.c_str (), >> + get_environ ()->envp (), netbsd_ptrace_fun, >> + nullptr, nullptr, nullptr, nullptr); >> + >> + netbsd_add_process (pid, 0); >> + >> + post_fork_inferior (pid, program); >> + >> + return pid; >> +} >> + >> +/* Implement the post_create_inferior target_ops method. */ >> + >> +void >> +netbsd_process_target::post_create_inferior () >> +{ >> + pid_t pid =3D current_process ()->pid; >> + netbsd_nat::enable_proc_events (pid); >> +} >> + >> +/* Implement the attach target_ops method. */ >> + >> +int >> +netbsd_process_target::attach (unsigned long pid) >> +{ >> + >> + if (ptrace (PT_ATTACH, pid, nullptr, 0) !=3D 0) >> + error ("Cannot attach to process %lu: %s (%d)\n", pid, >> + safe_strerror (errno), errno); >> + >> + netbsd_add_process (pid, 1); >> + netbsd_nat::enable_proc_events (pid); >> + netbsd_add_threads (pid); >> + >> + return 0; >> +} >> + >> +/* Returns true if GDB is interested in any child syscalls. */ >> + >> +static bool >> +gdb_catching_syscalls_p (pid_t pid) >> +{ >> + struct process_info *proc =3D find_process_pid (pid); >> + return !proc->syscalls_to_catch.empty (); >> +} >> + >> +/* Implement the resume target_ops method. */ >> + >> +void >> +netbsd_process_target::resume (struct thread_resume *resume_info, siz= e_t n) >> +{ >> + ptid_t resume_ptid =3D resume_info[0].thread; >> + const int signal =3D resume_info[0].sig; >> + const bool step =3D resume_info[0].kind =3D=3D resume_step; >> + >> + if (resume_ptid =3D=3D minus_one_ptid) >> + resume_ptid =3D ptid_of (current_thread); >> + >> + const pid_t pid =3D resume_ptid.pid (); >> + const lwpid_t lwp =3D resume_ptid.lwp (); >> + regcache_invalidate_pid (pid); >> + >> + auto fn >> + =3D [&] (ptid_t ptid) >> + { >> + if (step) >> + { >> + if (ptid.lwp () =3D=3D lwp || n !=3D 1) >> + { >> + if (ptrace (PT_SETSTEP, pid, NULL, ptid.lwp ()) =3D=3D -1) >> + perror_with_name (("ptrace")); >> + if (ptrace (PT_RESUME, pid, NULL, ptid.lwp ()) =3D=3D -1) >> + perror_with_name (("ptrace")); >> + } >> + else >> + { >> + if (ptrace (PT_CLEARSTEP, pid, NULL, ptid.lwp ()) =3D=3D -1) >> + perror_with_name (("ptrace")); >> + if (ptrace (PT_SUSPEND, pid, NULL, ptid.lwp ()) =3D=3D -1) >> + perror_with_name (("ptrace")); >> + } >> + } >> + else >> + { >> + if (ptrace (PT_CLEARSTEP, pid, NULL, ptid.lwp ()) =3D=3D -1) >> + perror_with_name (("ptrace")); >> + if (ptrace (PT_RESUME, pid, NULL, ptid.lwp ()) =3D=3D -1) >> + perror_with_name (("ptrace")); >> + } >> + }; >> + >> + netbsd_nat::list_threads (pid, fn); >> + >> + int request =3D gdb_catching_syscalls_p (pid) ? PT_CONTINUE : PT_SY= SCALL; >> + >> + errno =3D 0; >> + ptrace (request, pid, (void *)1, signal); >> + if (errno) >> + perror_with_name (("ptrace")); >> +} >> + >> +/* Returns true if GDB is interested in the reported SYSNO syscall. = */ >> + >> +static bool >> +netbsd_catch_this_syscall (int sysno) >> +{ >> + struct process_info *proc =3D current_process (); >> + >> + if (proc->syscalls_to_catch.empty ()) >> + return false; >> + >> + if (proc->syscalls_to_catch[0] =3D=3D ANY_SYSCALL) >> + return true; >> + >> + for (int iter : proc->syscalls_to_catch) >> + if (iter =3D=3D sysno) >> + return true; >> + >> + return false; >> +} >> + >> +/* Helper function for child_wait and the derivatives of child_wait. >> + HOSTSTATUS is the waitstatus from wait() or the equivalent; store = our >> + translation of that in OURSTATUS. */ >=20 > A blank line here between the comment and the function header. >=20 Fixed. >> +static void >> +netbsd_store_waitstatus (struct target_waitstatus *ourstatus, int hos= tstatus) >> +{ >> + if (WIFEXITED (hoststatus)) >> + { >> + ourstatus->kind =3D TARGET_WAITKIND_EXITED; >> + ourstatus->value.integer =3D WEXITSTATUS (hoststatus); >> + } >> + else if (!WIFSTOPPED (hoststatus)) >> + { >> + ourstatus->kind =3D TARGET_WAITKIND_SIGNALLED; >> + ourstatus->value.sig =3D gdb_signal_from_host (WTERMSIG (hostst= atus)); >> + } >> + else >> + { >> + ourstatus->kind =3D TARGET_WAITKIND_STOPPED; >> + ourstatus->value.sig =3D gdb_signal_from_host (WSTOPSIG (hostst= atus)); >> + } >> +} >> + >> +/* Implement a safe wrapper around waitpid(). */ >> + >> +static pid_t >> +netbsd_waitpid (ptid_t ptid, struct target_waitstatus *ourstatus, int= options) >> +{ >> + int status; >> + >> + pid_t pid =3D gdb::handle_eintr (::waitpid, ptid.pid (), &status, o= ptions); >> + >> + if (pid =3D=3D -1) >> + perror_with_name (_("Child process unexpectedly missing")); >> + >> + netbsd_store_waitstatus (ourstatus, status); >> + return pid; >> +} >> + >> + >> +/* Implement the wait target_ops method. >> + >> + Wait for the child specified by PTID to do something. Return the >> + process ID of the child, or MINUS_ONE_PTID in case of error; store= >> + the status in *OURSTATUS. */ >> + >> +static ptid_t >> +netbsd_wait (ptid_t ptid, struct target_waitstatus *ourstatus, >> + int target_options) >> +{ >> + pid_t pid =3D netbsd_waitpid (ptid, ourstatus, target_options); >> + ptid_t wptid =3D ptid_t (pid); >> + >> + if (pid =3D=3D 0) >> + { >> + gdb_assert (target_options & TARGET_WNOHANG); >> + ourstatus->kind =3D TARGET_WAITKIND_IGNORE; >> + return null_ptid; >> + } >> + >> + gdb_assert (pid !=3D -1); >> + >> + /* If the child stopped, keep investigating its status. */ >> + if (ourstatus->kind !=3D TARGET_WAITKIND_STOPPED) >> + return wptid; >> + >> + /* Extract the event and thread that received a signal. */ >> + ptrace_siginfo_t psi; >> + if (ptrace (PT_GET_SIGINFO, pid, &psi, sizeof (psi)) =3D=3D -1) >> + perror_with_name (("ptrace")); >> + >> + /* Pick child's siginfo_t. */ >> + siginfo_t *si =3D &psi.psi_siginfo; >> + >> + lwpid_t lwp =3D psi.psi_lwpid; >> + >> + int signo =3D si->si_signo; >> + const int code =3D si->si_code; >> + >> + /* Construct PTID with a specified thread that received the event. >> + If a signal was targeted to the whole process, lwp is 0. */ >> + wptid =3D ptid_t (pid, lwp, 0); >> + >> + /* Bail out on non-debugger oriented signals.. */ >=20 > Double periods. >=20 Fixed. >> + if (signo !=3D SIGTRAP) >> + return wptid; >> + >> + /* Stop examining non-debugger oriented SIGTRAP codes. */ >> + if (code <=3D SI_USER || code =3D=3D SI_NOINFO) >> + return wptid; >> + >> + /* Process state for threading events */ >=20 > End with dot-space-space? >=20 Fixed. >> + ptrace_state_t pst =3D {}; >> + if (code =3D=3D TRAP_LWP) >> + { >> + if (ptrace (PT_GET_PROCESS_STATE, pid, &pst, sizeof (pst)) =3D=3D= -1) >> + perror_with_name (("ptrace")); >> + } >=20 > I think braces can be removed. >=20 Fixed. >> + >> + if (code =3D=3D TRAP_LWP && pst.pe_report_event =3D=3D PTRACE_LWP_E= XIT) >> + { >> + /* If GDB attaches to a multi-threaded process, exiting >> + threads might be skipped during post_attach that >> + have not yet reported their PTRACE_LWP_EXIT event. >> + Ignore exited events for an unknown LWP. */ >> + thread_info *thr =3D find_thread_ptid (wptid); >> + if (thr =3D=3D nullptr) >> + ourstatus->kind =3D TARGET_WAITKIND_SPURIOUS; >> + else >> + { >> + ourstatus->kind =3D TARGET_WAITKIND_THREAD_EXITED; >> + /* NetBSD does not store an LWP exit status. */ >> + ourstatus->value.integer =3D 0; >> + >> + remove_thread (thr); >> + } >> + return wptid; >> + } >> + >> + if (find_thread_ptid (ptid_t (pid))) >> + current_thread =3D find_thread_ptid (wptid); >> + >> + if (code =3D=3D TRAP_LWP && pst.pe_report_event =3D=3D PTRACE_LWP_C= REATE) >> + { >> + /* If GDB attaches to a multi-threaded process, newborn >> + threads might be added by nbsd_add_threads that have >> + not yet reported their PTRACE_LWP_CREATE event. Ignore >> + born events for an already-known LWP. */ >> + if (find_thread_ptid (wptid)) >> + ourstatus->kind =3D TARGET_WAITKIND_SPURIOUS; >> + else >> + { >> + add_thread (wptid, NULL); >> + ourstatus->kind =3D TARGET_WAITKIND_THREAD_CREATED; >> + } >> + return wptid; >> + } >> + >> + if (code =3D=3D TRAP_EXEC) >> + { >> + ourstatus->kind =3D TARGET_WAITKIND_EXECD; >> + ourstatus->value.execd_pathname >> + =3D xstrdup (netbsd_nat::pid_to_exec_file (pid)); >> + return wptid; >> + } >> + >> + if (code =3D=3D TRAP_TRACE) >> + { >> + return wptid; >> + } >=20 > Braces can be removed. >=20 Fixed. >> + >> + if (code =3D=3D TRAP_SCE || code =3D=3D TRAP_SCX) >> + { >> + int sysnum =3D si->si_sysnum; >> + >> + if (!netbsd_catch_this_syscall(sysnum)) >> + { >> + /* If the core isn't interested in this event, ignore it. */ >> + ourstatus->kind =3D TARGET_WAITKIND_SPURIOUS; >> + return wptid; >> + } >> + >> + ourstatus->kind =3D >=20 > The assignment operator should be on the next line. >=20 Fixed and added () for nicer alignment. >> + (code =3D=3D TRAP_SCE) ? TARGET_WAITKIND_SYSCALL_ENTRY : >> + TARGET_WAITKIND_SYSCALL_RETURN; >> + ourstatus->value.syscall_number =3D sysnum; >> + return wptid; >> + } >> + >> + if (code =3D=3D TRAP_BRKPT) >> + { >> +#ifdef PTRACE_BREAKPOINT_ADJ >> + CORE_ADDR pc; >> + struct reg r; >> + ptrace (PT_GETREGS, pid, &r, psi.psi_lwpid); >> + pc =3D PTRACE_REG_PC (&r); >> + PTRACE_REG_SET_PC (&r, pc - PTRACE_BREAKPOINT_ADJ); >> + ptrace (PT_SETREGS, pid, &r, psi.psi_lwpid); >> +#endif >> + return wptid; >> + } >> + >> + /* Unclassified SIGTRAP event. */ >> + ourstatus->kind =3D TARGET_WAITKIND_SPURIOUS; >> + return wptid; >> +} >> + >> +/* Implement the wait target_ops method. */ >> + >> +ptid_t >> +netbsd_process_target::wait (ptid_t ptid, struct target_waitstatus *o= urstatus, >> + int target_options) >> +{ >> + while (true) >> + { >> + ptid_t wptid =3D netbsd_wait (ptid, ourstatus, target_options);= >> + >> + /* Register thread in the gdbcore if a thread was not reported = earlier. >> + This is required after ::create_inferior, when the gdbcore does not= >> + know about the first internal thread. >> + This may also happen on attach, when an event is registered on a th= read >> + that was not fully initialized during the attach stage. */ >=20 > Dot-space-space. >=20 Fixed. >> + if (wptid.lwp () !=3D 0 && !find_thread_ptid (wptid) >> + && ourstatus->kind !=3D TARGET_WAITKIND_THREAD_EXITED) >> + add_thread (wptid, nullptr); >> + >> + switch (ourstatus->kind) >> + { >> + case TARGET_WAITKIND_EXITED: >> + case TARGET_WAITKIND_STOPPED: >> + case TARGET_WAITKIND_SIGNALLED: >> + case TARGET_WAITKIND_FORKED: >> + case TARGET_WAITKIND_VFORKED: >> + case TARGET_WAITKIND_EXECD: >> + case TARGET_WAITKIND_VFORK_DONE: >> + case TARGET_WAITKIND_SYSCALL_ENTRY: >> + case TARGET_WAITKIND_SYSCALL_RETURN: >> + /* Pass the result to the generic code. */ >> + return wptid; >> + case TARGET_WAITKIND_THREAD_CREATED: >> + case TARGET_WAITKIND_THREAD_EXITED: >> + /* The core needlessly stops on these events. */ >> + /* FALLTHROUGH */ >> + case TARGET_WAITKIND_SPURIOUS: >> + /* Spurious events are unhandled by the gdbserver core. */ >> + if (ptrace (PT_CONTINUE, current_process ()->pid, (void *) 1, 0) >> + =3D=3D -1) >> + perror_with_name (("ptrace")); >> + break; >> + default: >> + error (("Unknwon stopped status")); >=20 > Typo "Unknwon". >=20 Fixed. >> + } >> + } >> +} >> + >> +/* Implement the kill target_ops method. */ >> + >> +int >> +netbsd_process_target::kill (process_info *process) >> +{ >> + pid_t pid =3D process->pid; >> + ptrace (PT_KILL, pid, nullptr, 0); >> + >> + int status; >> + gdb::handle_eintr (::waitpid, pid, &status, 0); >=20 > The "kill" target op is supposed to return -1 on failure. > So, maybe not ignore the return value of handle_eintr here? >=20 Fixed. I've added an error check for ptrace(2) and waitpid(2). >> + mourn (process); >> + return 0; >> +} >> + >> +/* Implement the detach target_ops method. */ >> + >> +int >> +netbsd_process_target::detach (process_info *process) >> +{ >> + pid_t pid =3D process->pid; >> + >> + ptrace (PT_DETACH, pid, (void *) 1, 0); >> + mourn (process); >> + return 0; >> +} >> + >> +/* Implement the mourn target_ops method. */ >> + >> +void >> +netbsd_process_target::mourn (struct process_info *proc) >> +{ >> + for_each_thread (proc->pid, remove_thread); >> + >> + remove_process (proc); >> +} >> + >> +/* Implement the join target_ops method. */ >> + >> +void >> +netbsd_process_target::join (int pid) >> +{ >> + /* The PT_DETACH is sufficient to detach from the process. >> + So no need to do anything extra. */ >> +} >> + >> +/* Implement the thread_alive target_ops method. */ >> + >> +bool >> +netbsd_process_target::thread_alive (ptid_t ptid) >> +{ >> + return netbsd_nat::thread_alive (ptid); >> +} >> + >> +/* Implement the fetch_registers target_ops method. */ >> + >> +void >> +netbsd_process_target::fetch_registers (struct regcache *regcache, in= t regno) >> +{ >> + struct netbsd_regset_info *regset =3D netbsd_target_regsets; >> + ptid_t inferior_ptid =3D ptid_of (current_thread); >> + >> + while (regset->size >=3D 0) >> + { >> + char *buf =3D (char *) xmalloc (regset->size); >> + int res =3D ptrace (regset->get_request, inferior_ptid.pid (), = buf, >> + inferior_ptid.lwp ()); >> + if (res =3D=3D -1) >> + perror_with_name (("ptrace")); >> + regset->store_function (regcache, buf); >> + free (buf); >> + regset++; >> + } >> +} >> + >> +/* Implement the store_registers target_ops method. */ >> + >> +void >> +netbsd_process_target::store_registers (struct regcache *regcache, in= t regno) >> +{ >> + struct netbsd_regset_info *regset =3D netbsd_target_regsets; >> + ptid_t inferior_ptid =3D ptid_of (current_thread); >> + >> + while (regset->size >=3D 0) >> + { >> + char *buf =3D (char *)xmalloc (regset->size); >> + int res =3D ptrace (regset->get_request, inferior_ptid.pid (), = buf, >> + inferior_ptid.lwp ()); >> + if (res =3D=3D -1) >> + perror_with_name (("ptrace")); >> + if (res =3D=3D 0) >> + { >> + /* Then overlay our cached registers on that. */ >> + regset->fill_function (regcache, buf); >> + /* Only now do we write the register set. */ >> + res =3D ptrace (regset->set_request, inferior_ptid.pid (), buf, >> + inferior_ptid.lwp ()); >> + } >> + free (buf); >> + regset++; >> + } >> +} >> + >> +/* Implement the read_memory target_ops method. */ >> + >> +int >> +netbsd_process_target::read_memory (CORE_ADDR memaddr, unsigned char = *myaddr, >> + int size) >> +{ >> + struct ptrace_io_desc io; >> + io.piod_op =3D PIOD_READ_D; >> + io.piod_len =3D size; >> + >> + pid_t pid =3D current_process ()->pid; >> + >> + int bytes_read =3D 0; >> + >> + if (size =3D=3D 0) >> + { >> + /* Zero length write always succeeds. */ >> + return 0; >> + } >> + do >> + { >> + io.piod_offs =3D (void *)(memaddr + bytes_read); >> + io.piod_addr =3D myaddr + bytes_read; >> + >> + int rv =3D ptrace (PT_IO, pid, &io, 0); >> + if (rv =3D=3D -1) >> + return errno; >> + if (io.piod_len =3D=3D 0) >> + return 0; >> + >> + bytes_read +=3D io.piod_len; >> + io.piod_len =3D size - bytes_read; >> + } >> + while (bytes_read < size); >> + >> + return 0; >> +} >> + >> +/* Implement the write_memory target_ops method. */ >> + >> +int >> +netbsd_process_target::write_memory (CORE_ADDR memaddr, >> + const unsigned char *myaddr, int size) >> +{ >> + struct ptrace_io_desc io; >> + io.piod_op =3D PIOD_WRITE_D; >> + io.piod_len =3D size; >> + >> + pid_t pid =3D current_process ()->pid; >> + >> + int bytes_written =3D 0; >> + >> + if (size =3D=3D 0) >> + { >> + /* Zero length write always succeeds. */ >> + return 0; >> + } >> + >> + do >> + { >> + io.piod_addr =3D (void *)(myaddr + bytes_written); >> + io.piod_offs =3D (void *)(memaddr + bytes_written); >> + >> + int rv =3D ptrace (PT_IO, pid, &io, 0); >> + if (rv =3D=3D -1) >> + return errno; >> + if (io.piod_len =3D=3D 0) >> + return 0; >> + >> + bytes_written +=3D io.piod_len; >> + io.piod_len =3D size - bytes_written; >> + } >> + while (bytes_written < size); >> + >> + return 0; >> +} >> + >> +/* Implement the kill_request target_ops method. */ >=20 > "kill_request" -> "request_interrupt". >=20 Fixed. >> + >> +void >> +netbsd_process_target::request_interrupt () >> +{ >> + ptid_t inferior_ptid =3D ptid_of (get_first_thread ()); >> + >> + ::kill (inferior_ptid.pid(), SIGINT); >> +} >> + >=20 > I think a function comment is needed here. >=20 Fixed. >> +static size_t >> +netbsd_read_auxv(pid_t pid, void *offs, void *addr, size_t len) >> +{ >> + struct ptrace_io_desc pio; >> + >> + pio.piod_op =3D PIOD_READ_AUXV; >> + pio.piod_offs =3D offs; >> + pio.piod_addr =3D addr; >> + pio.piod_len =3D len; >> + >> + if (ptrace (PT_IO, pid, &pio, 0) =3D=3D -1) >> + perror_with_name (("ptrace")); >> + >> + return pio.piod_len; >> +} >> + >> +/* Copy LEN bytes from inferior's auxiliary vector starting at OFFSET= >> + to debugger memory starting at MYADDR. */ >> + >> +int >> +netbsd_process_target::read_auxv (CORE_ADDR offset, >> + unsigned char *myaddr, unsigned int len) >> +{ >> + pid_t pid =3D pid_of (current_thread); >> + >> + return netbsd_read_auxv (pid, (void *) (intptr_t) offset, myaddr, l= en); >> +} >> + >> +bool >> +netbsd_process_target::supports_z_point_type (char z_type) >> +{ >> + switch (z_type) >> + { >> + case Z_PACKET_SW_BP: >> + return true; >> + case Z_PACKET_HW_BP: >> + case Z_PACKET_WRITE_WP: >> + case Z_PACKET_READ_WP: >> + case Z_PACKET_ACCESS_WP: >> + default: >> + return false; /* Not supported. */ >> + } >> +} >> + >> +/* Insert {break/watch}point at address ADDR. SIZE is not used. */ >> + >> +int >> +netbsd_process_target::insert_point (enum raw_bkpt_type type, CORE_AD= DR addr, >> + int size, struct raw_breakpoint *bp) >> +{ >> + switch (type) >> + { >> + case raw_bkpt_type_sw: >> + return insert_memory_breakpoint (bp); >> + case raw_bkpt_type_hw: >> + case raw_bkpt_type_write_wp: >> + case raw_bkpt_type_read_wp: >> + case raw_bkpt_type_access_wp: >> + default: >> + return 1; /* Not supported. */ >> + } >> +} >> + >> +/* Remove {break/watch}point at address ADDR. SIZE is not used. */ >> + >> +int >> +netbsd_process_target::remove_point (enum raw_bkpt_type type, CORE_AD= DR addr, >> + int size, struct raw_breakpoint *bp) >> +{ >> + switch (type) >> + { >> + case raw_bkpt_type_sw: >> + return remove_memory_breakpoint (bp); >> + case raw_bkpt_type_hw: >> + case raw_bkpt_type_write_wp: >> + case raw_bkpt_type_read_wp: >> + case raw_bkpt_type_access_wp: >> + default: >> + return 1; /* Not supported. */ >> + } >> +} >> + >> +/* Implement the to_stopped_by_sw_breakpoint target_ops >> + method. */ >=20 > "to_stopped_by_sw_breakpoint" -> "stopped_by_sw_breakpoint". >=20 Fixed. >> + >> +bool >> +netbsd_process_target::stopped_by_sw_breakpoint () >> +{ >> + ptrace_siginfo_t psi; >> + pid_t pid =3D current_process ()->pid; >> + >> + if (ptrace (PT_GET_SIGINFO, pid, &psi, sizeof (psi)) =3D=3D -1) >> + perror_with_name (("ptrace")); >> + >> + return psi.psi_siginfo.si_signo =3D=3D SIGTRAP && >> + psi.psi_siginfo.si_code =3D=3D TRAP_BRKPT; >> +} >> + >> +/* Implement the to_supports_stopped_by_sw_breakpoint target_ops >> + method. */ >=20 > The "to_" prefix shall be removed here, too. >=20 Fixed. >> + >> +bool >> +netbsd_process_target::supports_stopped_by_sw_breakpoint () >> +{ >> + return true; >> +} >> + >> +/* Implement the supports_qxfer_siginfo target_ops method. */ >> + >> +bool >> +netbsd_process_target::supports_qxfer_siginfo () >> +{ >> + return true; >> +} >> + >> +/* Implement the qxfer_siginfo target_ops method. */ >> + >> +int >> +netbsd_process_target::qxfer_siginfo (const char *annex, unsigned cha= r *readbuf, >> + unsigned const char *writebuf, >> + CORE_ADDR offset, int len) >> +{ >> + if (current_thread =3D=3D nullptr) >> + return -1; >> + >> + pid_t pid =3D current_process ()->pid; >> + >> + return netbsd_nat::qxfer_siginfo(pid, annex, readbuf, writebuf, off= set, len); >> +} >> + >> +/* Implement the supports_non_stop target_ops method. */ >> + >> +bool >> +netbsd_process_target::supports_non_stop () >> +{ >> + return false; >> +} >> + >> +/* Implement the supports_multi_process target_ops method. */ >> + >> +bool >> +netbsd_process_target::supports_multi_process () >> +{ >> + return true; >> +} >> + >> +/* Check if fork events are supported. */ >> + >> +bool >> +netbsd_process_target::supports_fork_events () >> +{ >> + return false; >> +} >> + >> +/* Check if vfork events are supported. */ >> + >> +bool >> +netbsd_process_target::supports_vfork_events () >> +{ >> + return false; >> +} >> + >> +/* Check if exec events are supported. */ >> + >> +bool >> +netbsd_process_target::supports_exec_events () >> +{ >> + return true; >> +} >> + >> +/* Implement the supports_disable_randomization target_ops method. *= / >> + >> +bool >> +netbsd_process_target::supports_disable_randomization () >> +{ >> + return false; >> +} >=20 > Some of the target ops above do not change the base class behavior. Th= e override > can be omitted for code simplicity. I think it's also OK to retain the= m if you want > to make the definitions explicit. >=20 Most of the can be supported in future, except supports_disable_randomization and supports_non_stop. I will leave them around for the time being. >> + >> +/* Extract &phdr and num_phdr in the inferior. Return 0 on success. = */ >> + >> +template >> +int get_phdr_phnum_from_proc_auxv (const pid_t pid, >> + CORE_ADDR *phdr_memaddr, int *num_phdr) >> +{ >> + typedef typename std::conditional> + Aux64Info, Aux32Info>::type auxv_type; >> + const size_t auxv_size =3D sizeof (auxv_type); >> + const size_t auxv_buf_size =3D 128 * sizeof (auxv_type); >> + >> + char *auxv_buf =3D (char *) xmalloc (auxv_buf_size); >> + >> + netbsd_read_auxv (pid, nullptr, auxv_buf, auxv_buf_size); >> + >> + *phdr_memaddr =3D 0; >> + *num_phdr =3D 0; >> + >> + for (char *buf =3D auxv_buf; buf < (auxv_buf + auxv_buf_size); buf = +=3D auxv_size) >> + { >> + auxv_type *const aux =3D (auxv_type *) buf; >> + >> + switch (aux->a_type) >> + { >> + case AT_PHDR: >> + *phdr_memaddr =3D aux->a_v; >> + break; >> + case AT_PHNUM: >> + *num_phdr =3D aux->a_v; >> + break; >> + } >> + >> + if (*phdr_memaddr !=3D 0 && *num_phdr !=3D 0) >> + break; >> + } >> + >> + xfree (auxv_buf); >> + >> + if (*phdr_memaddr =3D=3D 0 || *num_phdr =3D=3D 0) >> + { >> + warning ("Unexpected missing AT_PHDR and/or AT_PHNUM: " >> + "phdr_memaddr =3D %ld, phdr_num =3D %d", >> + (long) *phdr_memaddr, *num_phdr); >=20 > It might be better to use `core_addr_to_string` or `core_addr_to_string= _nz` > to print phdr_memaddr. >=20 Fixed. I've picked core_addr_to_string (). >> + return 2; >> + } >> + >> + return 0; >> +} >> + >> +/* Return &_DYNAMIC (via PT_DYNAMIC) in the inferior, or 0 if not pre= sent. */ >> + >> +template >> +static CORE_ADDR >> +get_dynamic (netbsd_process_target *target, const pid_t pid) >> +{ >> + typedef typename std::conditional> + Elf64_Phdr, Elf32_Phdr>::type phdr_type; >> + const int phdr_size =3D sizeof (phdr_type); >> + >> + CORE_ADDR phdr_memaddr; >> + int num_phdr; >> + if (get_phdr_phnum_from_proc_auxv (pid, &phdr_memaddr, &num_phdr= )) >> + return 0; >> + >> + std::vector phdr_buf; >> + phdr_buf.resize (num_phdr * phdr_size); >> + >> + if ((*target).read_memory (phdr_memaddr, phdr_buf.data (), phdr_buf= =2Esize ())) >=20 > Why not simply `target->read_memory`? Fixed. > Also, it might be better to explicitly check !=3D 0. >=20 I've avoided it as the line is too long and would need to be wrapped. >> + return 0; >> + >> + /* Compute relocation: it is expected to be 0 for "regular" executa= bles, >> + non-zero for PIE ones. */ >> + CORE_ADDR relocation =3D -1; >> + for (int i =3D 0; relocation =3D=3D -1 && i < num_phdr; i++) >> + { >> + phdr_type *const p =3D (phdr_type *) (phdr_buf.data() + i * phd= r_size); >> + >> + if (p->p_type =3D=3D PT_PHDR) >> + relocation =3D phdr_memaddr - p->p_vaddr; >> + } >> + >> + if (relocation =3D=3D -1) >> + { >> + /* PT_PHDR is optional, but necessary for PIE in general. Fort= unately >> + any real world executables, including PIE executables, have always >> + PT_PHDR present. PT_PHDR is not present in some shared libraries o= r >> + in fpc (Free Pascal 2.4) binaries but neither of those have a need = for >> + or present DT_DEBUG anyway (fpc binaries are statically linked). >> + >> + Therefore if there exists DT_DEBUG there is always also PT_PHDR. >> + >> + GDB could find RELOCATION also from AT_ENTRY - e_entry. */ >> + >> + return 0; >> + } >> + >> + for (int i =3D 0; i < num_phdr; i++) >> + { >> + phdr_type *const p =3D (phdr_type *) (phdr_buf.data () + i * ph= dr_size); >> + >> + if (p->p_type =3D=3D PT_DYNAMIC) >> + return p->p_vaddr + relocation; >> + } >> + >> + return 0; >> +} >> + >> +/* Return &_r_debug in the inferior, or -1 if not present. Return va= lue >> + can be 0 if the inferior does not yet have the library list initia= lized. >> + We look for DT_MIPS_RLD_MAP first. MIPS executables use this inst= ead of >> + DT_DEBUG, although they sometimes contain an unused DT_DEBUG entry= too. */ >> + >> +template >> +static CORE_ADDR >> +get_r_debug (netbsd_process_target *target, const int pid) >> +{ >> + typedef typename std::conditional> + Elf64_Dyn, Elf32_Dyn>::type dyn_type; >> + const int dyn_size =3D sizeof (dyn_type); >> + unsigned char buf[sizeof (dyn_type)]; /* The larger of the two. *= / >> + CORE_ADDR map =3D -1; >> + >> + CORE_ADDR dynamic_memaddr =3D get_dynamic (target, pid); >> + if (dynamic_memaddr =3D=3D 0) >> + return map; >> + >> + while ((*target).read_memory (dynamic_memaddr, buf, dyn_size) =3D=3D= 0) >=20 > The `target->read_memory` comment here, too. >=20 Fixed. >> + { >> + dyn_type *const dyn =3D (dyn_type *) buf; >> +#if defined DT_MIPS_RLD_MAP >> + union >> + { >> + T map; >> + unsigned char buf[sizeof (T)]; >> + } >> + rld_map; >> + >> + if (dyn->d_tag =3D=3D DT_MIPS_RLD_MAP) >> + { >> + if (read_memory (dyn->d_un.d_val, >> + rld_map.buf, sizeof (rld_map.buf)) =3D=3D 0) >> + return rld_map.map; >> + else >> + break; >> + } >> +#endif /* DT_MIPS_RLD_MAP */ >> + >> + if (dyn->d_tag =3D=3D DT_DEBUG && map =3D=3D -1) >> + map =3D dyn->d_un.d_val; >> + >> + if (dyn->d_tag =3D=3D DT_NULL) >> + break; >> + >> + dynamic_memaddr +=3D dyn_size; >> + } >> + >> + return map; >> +} >> + >> +/* Read one pointer from MEMADDR in the inferior. */ >> + >> +static int >> +read_one_ptr (netbsd_process_target *target, CORE_ADDR memaddr, CORE_= ADDR *ptr, >> + int ptr_size) >> +{ >> + /* Go through a union so this works on either big or little endian >> + hosts, when the inferior's pointer size is smaller than the size= >> + of CORE_ADDR. It is assumed the inferior's endianness is the >> + same of the superior's. */ >> + >> + union >> + { >> + CORE_ADDR core_addr; >> + unsigned int ui; >> + unsigned char uc; >> + } addr; >> + >> + int ret =3D (*target).read_memory (memaddr, &addr.uc, ptr_size); >=20 > Here, too. >=20 Fixed. >> + if (ret =3D=3D 0) >> + { >> + if (ptr_size =3D=3D sizeof (CORE_ADDR)) >> + *ptr =3D addr.core_addr; >> + else if (ptr_size =3D=3D sizeof (unsigned int)) >> + *ptr =3D addr.ui; >> + else >> + gdb_assert_not_reached ("unhandled pointer size"); >> + } >> + return ret; >> +} >> + >> +/* Construct qXfer:libraries-svr4:read reply. */ >> + >> +template >> +int >> +netbsd_qxfer_libraries_svr4 (netbsd_process_target *target, >> + const pid_t pid, const char *annex, >> + unsigned char *readbuf, >> + unsigned const char *writebuf, >> + CORE_ADDR offset, int len) >> +{ >> + struct link_map_offsets >> + { >> + /* Offset and size of r_debug.r_version. */ >> + int r_version_offset; >> + >> + /* Offset and size of r_debug.r_map. */ >> + int r_map_offset; >> + >> + /* Offset to l_addr field in struct link_map. */ >> + int l_addr_offset; >> + >> + /* Offset to l_name field in struct link_map. */ >> + int l_name_offset; >> + >> + /* Offset to l_ld field in struct link_map. */ >> + int l_ld_offset; >> + >> + /* Offset to l_next field in struct link_map. */ >> + int l_next_offset; >> + >> + /* Offset to l_prev field in struct link_map. */ >> + int l_prev_offset; >> + }; >> + >> + static const struct link_map_offsets lmo_32bit_offsets =3D >> + { >> + 0, /* r_version offset. */ >> + 4, /* r_debug.r_map offset. */ >> + 0, /* l_addr offset in link_map. */ >> + 4, /* l_name offset in link_map. */ >> + 8, /* l_ld offset in link_map. */ >> + 12, /* l_next offset in link_map. */ >> + 16 /* l_prev offset in link_map. */ >> + }; >> + >> + static const struct link_map_offsets lmo_64bit_offsets =3D >> + { >> + 0, /* r_version offset. */ >> + 8, /* r_debug.r_map offset. */ >> + 0, /* l_addr offset in link_map. */ >> + 8, /* l_name offset in link_map. */ >> + 16, /* l_ld offset in link_map. */ >> + 24, /* l_next offset in link_map. */ >> + 32 /* l_prev offset in link_map. */ >> + }; >> + >> + CORE_ADDR lm_addr =3D 0, lm_prev =3D 0; >> + CORE_ADDR l_name, l_addr, l_ld, l_next, l_prev; >> + int header_done =3D 0; >> + >> + const struct link_map_offsets *lmo >> + =3D (sizeof (T) =3D=3D sizeof (int64_t)) >> + ? &lmo_64bit_offsets : &lmo_32bit_offsets; >=20 > An extra pair of parens can be used to better align '?'. Like this: >=20 > const struct link_map_offsets *lmo > =3D ((sizeof (T) =3D=3D sizeof (int64_t)) > ? &lmo_64bit_offsets : &lmo_32bit_offsets); >=20 Fixed. >> + int ptr_size =3D sizeof (T); >> + >> + while (annex[0] !=3D '\0') >> + { >> + const char *sep =3D strchr (annex, '=3D'); >> + if (sep =3D=3D nullptr) >> + break; >> + >> + int name_len =3D sep - annex; >> + CORE_ADDR *addrp; >> + if (name_len =3D=3D 5 && startswith (annex, "start")) >> + addrp =3D &lm_addr; >> + else if (name_len =3D=3D 4 && startswith (annex, "prev")) >> + addrp =3D &lm_prev; >> + else >> + { >> + annex =3D strchr (sep, ';'); >> + if (annex =3D=3D nullptr) >> + break; >> + annex++; >> + continue; >> + } >> + >> + annex =3D decode_address_to_semicolon (addrp, sep + 1); >> + } >> + >> + if (lm_addr =3D=3D 0) >> + { >> + CORE_ADDR r_debug =3D get_r_debug (target, pid); >> + >> + /* We failed to find DT_DEBUG. Such situation will not change >> + for this inferior - do not retry it. Report it to GDB as >> + E01, see for the reasons at the GDB solib-svr4.c side. */ >> + if (r_debug =3D=3D (CORE_ADDR) -1) >> + return -1; >> + >> + if (r_debug !=3D 0) >> + { >> + if (read_one_ptr (target, r_debug + lmo->r_map_offset, >> + &lm_addr, ptr_size) !=3D 0) >> + { >> + warning ("unable to read r_map from 0x%lx", >> + (long) r_debug + lmo->r_map_offset); >=20 > Again, `core_addr_to_string` could be used. >=20 Fixed and I've refactored this code into: + CORE_ADDR map_offset =3D r_debug + lmo->r_map_offset; + if (read_one_ptr (target, map_offset, &lm_addr, ptr_size) !=3D = 0) + warning ("unable to read r_map from %s", + core_addr_to_string (map_offset)); >> + } >> + } >=20 > Braces can be removed in both conditional statements above. And, if yo= u prefer, > the conditions can be merged via &&. >=20 Fixed. >> + } >> + >> + std::string document =3D "> + >> + while (lm_addr >> + && read_one_ptr (target, lm_addr + lmo->l_name_offset, >> + &l_name, ptr_size) =3D=3D 0 >> + && read_one_ptr (target, lm_addr + lmo->l_addr_offset, >> + &l_addr, ptr_size) =3D=3D 0 >> + && read_one_ptr (target, lm_addr + lmo->l_ld_offset, >> + &l_ld, ptr_size) =3D=3D 0 >> + && read_one_ptr (target, lm_addr + lmo->l_prev_offset, >> + &l_prev, ptr_size) =3D=3D 0 >> + && read_one_ptr (target, lm_addr + lmo->l_next_offset, >> + &l_next, ptr_size) =3D=3D 0) >> + { >> + if (lm_prev !=3D l_prev) >> + { >> + warning ("Corrupted shared library list: 0x%lx !=3D 0x%lx", >> + (long) lm_prev, (long) l_prev); >> + break; >> + } >> + >> + /* Ignore the first entry even if it has valid name as the firs= t entry >> + corresponds to the main executable. The first entry should not be >> + skipped if the dynamic loader was loaded late by a static executabl= e >> + (see solib-svr4.c parameter ignore_first). But in such case the ma= in >> + executable does not have PT_DYNAMIC present and this function alrea= dy >> + exited above due to failed get_r_debug. */ >> + if (lm_prev =3D=3D 0) >> + string_appendf (document, " main-lm=3D\"0x%lx\"", >> + (unsigned long) lm_addr); >> + else >> + { >> + unsigned char libname[PATH_MAX]; >> + >> + /* Not checking for error because reading may stop before >> + we've got PATH_MAX worth of characters. */ >> + libname[0] =3D '\0'; >> + (*target).read_memory (l_name, libname, sizeof (libname) - 1); >> + libname[sizeof (libname) - 1] =3D '\0'; >> + if (libname[0] !=3D '\0') >> + { >> + if (!header_done) >> + { >> + /* Terminate `> + document +=3D '>'; >> + header_done =3D 1; >> + } >> + >> + string_appendf (document, "> + xml_escape_text_append (&document, (char *) libname); >> + string_appendf (document, "\" lm=3D\"0x%lx\" " >> + "l_addr=3D\"0x%lx\" l_ld=3D\"0x%lx\"/>", >> + (unsigned long) lm_addr, (unsigned long) l_addr, >> + (unsigned long) l_ld); >> + } >> + } >> + >> + lm_prev =3D lm_addr; >> + lm_addr =3D l_next; >> + } >> + >> + if (!header_done) >> + { >> + /* Empty list; terminate `> + document +=3D "/>"; >> + } >> + else >> + document +=3D ""; >> + >> + int document_len =3D document.length (); >> + if (offset < document_len) >> + document_len -=3D offset; >> + else >> + document_len =3D 0; >> + if (len > document_len) >> + len =3D document_len; >> + >> + memcpy (readbuf, document.data () + offset, len); >> + >> + return len; >> +} >> + >> +/* Return true if FILE is a 64-bit ELF file, >> + false if the file is not a 64-bit ELF file, >> + and error if the file is not accessible or doesn't exist. */ >> + >> +static bool >> +elf_64_file_p (const char *file) >> +{ >> + int fd =3D gdb::handle_eintr (::open, file, O_RDONLY); >> + if (fd < 0) >> + perror_with_name (("open")); >> + >> + Elf64_Ehdr header; >> + ssize_t ret =3D gdb::handle_eintr (::read, fd, &header, sizeof (hea= der)); >> + if (ret =3D=3D -1) >> + perror_with_name (("read")); >> + gdb::handle_eintr (::close, fd); >> + if (ret !=3D sizeof (header)) >> + error ("Cannot read ELF file header: %s", file); >> + >> + if (header.e_ident[EI_MAG0] !=3D ELFMAG0 >> + || header.e_ident[EI_MAG1] !=3D ELFMAG1 >> + || header.e_ident[EI_MAG2] !=3D ELFMAG2 >> + || header.e_ident[EI_MAG3] !=3D ELFMAG3) >> + error ("Unrecognized ELF file header: %s", file); >> + >> + return header.e_ident[EI_CLASS] =3D=3D ELFCLASS64; >> +} >> + >> +/* Construct qXfer:libraries-svr4:read reply. */ >> + >> +int >> +netbsd_process_target::qxfer_libraries_svr4 (const char *annex, >> + unsigned char *readbuf, >> + unsigned const char *writebuf, >> + CORE_ADDR offset, int len) >> +{ >> + if (writebuf !=3D nullptr) >> + return -2; >> + if (readbuf =3D=3D nullptr) >> + return -1; >> + >> + struct process_info *proc =3D current_process (); >> + pid_t pid =3D proc->pid; >> + bool is_elf64 =3D elf_64_file_p (netbsd_nat::pid_to_exec_file (pid)= ); >> + >> + if (is_elf64) >> + return netbsd_qxfer_libraries_svr4 (this, pid, annex, re= adbuf, >> + writebuf, offset, len); >> + else >> + return netbsd_qxfer_libraries_svr4 (this, pid, annex, re= adbuf, >> + writebuf, offset, len); >> +} >> + >> +/* Implement the supports_qxfer_libraries_svr4 target_ops method. */= >> + >> +bool >> +netbsd_process_target::supports_qxfer_libraries_svr4 () >> +{ >> + return true; >> +} >> + >> +/* Return the name of a file that can be opened to get the symbols fo= r >> + the child process identified by PID. */ >> + >> +char * >> +netbsd_process_target::pid_to_exec_file (pid_t pid) >> +{ >> + return netbsd_nat::pid_to_exec_file (pid); >> +} >> + >> +/* Implementation of the target_ops method "supports_pid_to_exec_file= ". */ >> + >> +bool >> +netbsd_process_target::supports_pid_to_exec_file () >> +{ >> + return true; >> +} >> + >> +/* Implementation of the target_ops method "supports_hardware_single_= step". */ >> +bool >> +netbsd_process_target::supports_hardware_single_step () >> +{ >> + return true; >> +} >> + >> +/* Implementation of the target_ops method "sw_breakpoint_from_kind".= */ >> + >> +const gdb_byte * >> +netbsd_process_target::sw_breakpoint_from_kind (int kind, int *size) >> +{ >> + static gdb_byte brkpt[PTRACE_BREAKPOINT_SIZE]; >> + >> + *size =3D PTRACE_BREAKPOINT_SIZE; >> + >> + memcpy (brkpt, PTRACE_BREAKPOINT, PTRACE_BREAKPOINT_SIZE); >=20 > Is there a need to memcpy each time the function is called? >=20 I've optimized it to remove memcpy(3). >> + >> + return brkpt; >> +} >> + >> +/* Implement the thread_name target_ops method. */ >> + >> +const char * >> +netbsd_process_target::thread_name (ptid_t ptid) >> +{ >> + return netbsd_nat::thread_name (ptid); >> +} >> + >> +/* Implement the supports_catch_syscall target_ops method. */ >> + >> +bool >> +netbsd_process_target::supports_catch_syscall () >> +{ >> + return true; >> +} >> + >> +/* Implement the supports_read_auxv target_ops method. */ >> + >> +bool >> +netbsd_process_target::supports_read_auxv () >> +{ >> + return true; >> +} >> + >> +/* The NetBSD target ops object. */ >> + >> +static netbsd_process_target the_netbsd_target; >> + >> +void >> +initialize_low () >> +{ >> + set_target_ops (&the_netbsd_target); >> + the_low_target.arch_setup (); >> +} >> diff --git a/gdbserver/netbsd-low.h b/gdbserver/netbsd-low.h >> new file mode 100644 >> index 00000000000..2bd57c51726 >> --- /dev/null >> +++ b/gdbserver/netbsd-low.h >> @@ -0,0 +1,157 @@ >> +/* Copyright (C) 2020 Free Software Foundation, Inc. >> + >> + This file is part of GDB. >> + >> + This program is free software; you can redistribute it and/or modi= fy >> + it under the terms of the GNU General Public License as published = by >> + the Free Software Foundation; either version 3 of the License, or >> + (at your option) any later version. >> + >> + This program is distributed in the hope that it will be useful, >> + but WITHOUT ANY WARRANTY; without even the implied warranty of >> + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the >> + GNU General Public License for more details. >> + >> + You should have received a copy of the GNU General Public License >> + along with this program. If not, see . */ >> + >> +#ifndef GDBSERVER_NETBSD_LOW_H >> +#define GDBSERVER_NETBSD_LOW_H >> + >> +struct regcache; >> +struct target_desc; >> + >> +/* Some information relative to a given register set. */ >> + >> +struct netbsd_regset_info >> +{ >> + /* The ptrace request needed to get/set registers of this set. */ >> + int get_request, set_request; >> + /* The size of the register set. */ >> + int size; >> + /* Fill the buffer BUF from the contents of the given REGCACHE. */= >> + void (*fill_function) (struct regcache *regcache, char *buf); >> + /* Store the register value in BUF in the given REGCACHE. */ >> + void (*store_function) (struct regcache *regcache, const char *buf)= ; >=20 > Leaving one blank line between each field may improve readability. >=20 Fixed. >> +}; >> + >> +/* A list of regsets for the target being debugged, terminated by an = entry >> + where the size is negative. >> + >> + This list should be created by the target-specific code. */ >> + >> +extern struct netbsd_regset_info netbsd_target_regsets[]; >> + >> +/* The target-specific operations for NetBSD support. */ >> + >> +struct netbsd_target_ops >> +{ >> + /* Architecture-specific setup. */ >> + void (*arch_setup) (); >> + >> + /* Hook to support target specific qSupported. */ >> + void (*process_qsupported) (char **, int count); >=20 > Is the `process_qsupported` function called anywhere? >=20 Hmm... it was originally copied from Linux (before C++ification). It looks like unused now, at least I'm not triggering a call for it. Should I drop it? >> +}; >=20 > No strong opinion, but the low target could be a derived class of > netbsd_process_target, like the linux low targets. >=20 This is a simplified x86_64 support and for the time being I will leave it as it is. During addition of i386, I will switch it to more linux-like approach. >> + >> +/* Target ops definitions for a NetBSD target. */ >> + >> +class netbsd_process_target : public process_stratum_target >> +{ >> +public: >> + >> + int create_inferior (const char *program, >> + const std::vector &program_args) override; >> + >> + void post_create_inferior () override; >> + >> + int attach (unsigned long pid) override; >> + >> + int kill (process_info *proc) override; >> + >> + int detach (process_info *proc) override; >> + >> + void mourn (process_info *proc) override; >> + >> + void join (int pid) override; >> + >> + bool thread_alive (ptid_t pid) override; >> + >> + void resume (thread_resume *resume_info, size_t n) override; >> + >> + ptid_t wait (ptid_t ptid, target_waitstatus *status, >> + int options) override; >> + >> + void fetch_registers (regcache *regcache, int regno) override; >> + >> + void store_registers (regcache *regcache, int regno) override; >> + >> + int read_memory (CORE_ADDR memaddr, unsigned char *myaddr, >> + int len) override; >> + >> + int write_memory (CORE_ADDR memaddr, const unsigned char *myaddr, >> + int len) override; >> + >> + void request_interrupt () override; >> + >> + bool supports_read_auxv () override; >> + >> + int read_auxv (CORE_ADDR offset, unsigned char *myaddr, >> + unsigned int len) override; >> + >> + bool supports_hardware_single_step () override; >> + >> + const gdb_byte *sw_breakpoint_from_kind (int kind, int *size) overr= ide; >> + >> + bool supports_z_point_type (char z_type) override; >> + >> + int insert_point (enum raw_bkpt_type type, CORE_ADDR addr, >> + int size, struct raw_breakpoint *bp) override; >> + >> + int remove_point (enum raw_bkpt_type type, CORE_ADDR addr, >> + int size, struct raw_breakpoint *bp) override; >> + >> + bool stopped_by_sw_breakpoint () override; >> + >> + bool supports_qxfer_siginfo () override; >> + >> + int qxfer_siginfo (const char *annex, unsigned char *readbuf, >> + unsigned const char *writebuf, CORE_ADDR offset, >> + int len) override; >> + >> + bool supports_stopped_by_sw_breakpoint () override; >> + >> + bool supports_non_stop () override; >> + >> + bool supports_multi_process () override; >> + >> + bool supports_fork_events () override; >> + >> + bool supports_vfork_events () override; >> + >> + bool supports_exec_events () override; >> + >> + bool supports_disable_randomization () override; >> + >> + bool supports_qxfer_libraries_svr4 () override; >> + >> + int qxfer_libraries_svr4 (const char*, unsigned char*, const unsign= ed char*, >> + CORE_ADDR, int) override; >> + >> + bool supports_pid_to_exec_file () override; >> + >> + char *pid_to_exec_file (int pid) override; >> + >> + const char *thread_name (ptid_t thread) override; >> + >> + bool supports_catch_syscall () override; >> +}; >> + >> +/* The inferior's target description. This is a global because the >> + NetBSD ports support neither bi-arch nor multi-process. */ >=20 > The "nor multi-process" part of this comment is confusing. What does t= he multi-process > feature have to do with the_low_target? And netbsd_process_target was = defined to > support multi-process above in the .cc file. >=20 It was an old comment and I have updated it. >> + >> +extern struct netbsd_target_ops the_low_target; >> + >> +/* XXX: multilib */ >> +extern const struct target_desc *netbsd_tdesc; >> + >> +#endif /* GDBSERVER_NETBSD_LOW_H */ >> diff --git a/gdbserver/netbsd-x86_64-low.cc b/gdbserver/netbsd-x86_64-= low.cc >> new file mode 100644 >> index 00000000000..c8679d9f153 >> --- /dev/null >> +++ b/gdbserver/netbsd-x86_64-low.cc >> @@ -0,0 +1,250 @@ >> +/* Copyright (C) 2020 Free Software Foundation, Inc. >> + >> + This file is part of GDB. >> + >> + This program is free software; you can redistribute it and/or modi= fy >> + it under the terms of the GNU General Public License as published = by >> + the Free Software Foundation; either version 3 of the License, or >> + (at your option) any later version. >> + >> + This program is distributed in the hope that it will be useful, >> + but WITHOUT ANY WARRANTY; without even the implied warranty of >> + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the >> + GNU General Public License for more details. >> + >> + You should have received a copy of the GNU General Public License >> + along with this program. If not, see . */ >> + >> +#include >> +#include >> +#include >> + >> +#include "server.h" >> +#include "netbsd-low.h" >> +#include "gdbsupport/x86-xstate.h" >> +#include "arch/amd64.h" >> +#include "x86-tdesc.h" >> +#include "tdesc.h" >> + >> +static int use_xml; >> + >> +/* The index of various registers inside the regcache. */ >> + >> +enum netbsd_x86_64_gdb_regnum >> +{ >> + AMD64_RAX_REGNUM, /* %rax */ >> + AMD64_RBX_REGNUM, /* %rbx */ >> + AMD64_RCX_REGNUM, /* %rcx */ >> + AMD64_RDX_REGNUM, /* %rdx */ >> + AMD64_RSI_REGNUM, /* %rsi */ >> + AMD64_RDI_REGNUM, /* %rdi */ >> + AMD64_RBP_REGNUM, /* %rbp */ >> + AMD64_RSP_REGNUM, /* %rsp */ >> + AMD64_R8_REGNUM, /* %r8 */ >> + AMD64_R9_REGNUM, /* %r9 */ >> + AMD64_R10_REGNUM, /* %r10 */ >> + AMD64_R11_REGNUM, /* %r11 */ >> + AMD64_R12_REGNUM, /* %r12 */ >> + AMD64_R13_REGNUM, /* %r13 */ >> + AMD64_R14_REGNUM, /* %r14 */ >> + AMD64_R15_REGNUM, /* %r15 */ >> + AMD64_RIP_REGNUM, /* %rip */ >> + AMD64_EFLAGS_REGNUM, /* %eflags */ >> + AMD64_CS_REGNUM, /* %cs */ >> + AMD64_SS_REGNUM, /* %ss */ >> + AMD64_DS_REGNUM, /* %ds */ >> + AMD64_ES_REGNUM, /* %es */ >> + AMD64_FS_REGNUM, /* %fs */ >> + AMD64_GS_REGNUM, /* %gs */ >> + AMD64_ST0_REGNUM =3D 24, /* %st0 */ >> + AMD64_ST1_REGNUM, /* %st1 */ >> + AMD64_FCTRL_REGNUM =3D AMD64_ST0_REGNUM + 8, >> + AMD64_FSTAT_REGNUM =3D AMD64_ST0_REGNUM + 9, >> + AMD64_FTAG_REGNUM =3D AMD64_ST0_REGNUM + 10, >> + AMD64_XMM0_REGNUM =3D 40, /* %xmm0 */ >> + AMD64_XMM1_REGNUM, /* %xmm1 */ >> + AMD64_MXCSR_REGNUM =3D AMD64_XMM0_REGNUM + 16, >> + AMD64_YMM0H_REGNUM, /* %ymm0h */ >> + AMD64_YMM15H_REGNUM =3D AMD64_YMM0H_REGNUM + 15, >> + AMD64_BND0R_REGNUM =3D AMD64_YMM15H_REGNUM + 1, >> + AMD64_BND3R_REGNUM =3D AMD64_BND0R_REGNUM + 3, >> + AMD64_BNDCFGU_REGNUM, >> + AMD64_BNDSTATUS_REGNUM, >> + AMD64_XMM16_REGNUM, >> + AMD64_XMM31_REGNUM =3D AMD64_XMM16_REGNUM + 15, >> + AMD64_YMM16H_REGNUM, >> + AMD64_YMM31H_REGNUM =3D AMD64_YMM16H_REGNUM + 15, >> + AMD64_K0_REGNUM, >> + AMD64_K7_REGNUM =3D AMD64_K0_REGNUM + 7, >> + AMD64_ZMM0H_REGNUM, >> + AMD64_ZMM31H_REGNUM =3D AMD64_ZMM0H_REGNUM + 31, >> + AMD64_PKRU_REGNUM, >> + AMD64_FSBASE_REGNUM, >> + AMD64_GSBASE_REGNUM >> +}; >> + >> +/* The fill_function for the general-purpose register set. */ >> + >> +static void >> +netbsd_x86_64_fill_gregset (struct regcache *regcache, char *buf) >> +{ >> + struct reg *r =3D (struct reg *)buf; >=20 > Space after ')'. >=20 Fixed. >> + >> +#define netbsd_x86_64_collect_gp(regnum, fld) do { \ >> + collect_register (regcache, regnum, &r->regs[_REG_##fld]); \ >> + } while (0) >> + >> + netbsd_x86_64_collect_gp (AMD64_RAX_REGNUM, RAX); >> + netbsd_x86_64_collect_gp (AMD64_RBX_REGNUM, RBX); >> + netbsd_x86_64_collect_gp (AMD64_RCX_REGNUM, RCX); >> + netbsd_x86_64_collect_gp (AMD64_RDX_REGNUM, RDX); >> + netbsd_x86_64_collect_gp (AMD64_RSI_REGNUM, RSI); >> + netbsd_x86_64_collect_gp (AMD64_RDI_REGNUM, RDI); >> + netbsd_x86_64_collect_gp (AMD64_RBP_REGNUM, RBP); >> + netbsd_x86_64_collect_gp (AMD64_RSP_REGNUM, RSP); >> + netbsd_x86_64_collect_gp (AMD64_R8_REGNUM, R8); >> + netbsd_x86_64_collect_gp (AMD64_R9_REGNUM, R9); >> + netbsd_x86_64_collect_gp (AMD64_R10_REGNUM, R10); >> + netbsd_x86_64_collect_gp (AMD64_R11_REGNUM, R11); >> + netbsd_x86_64_collect_gp (AMD64_R12_REGNUM, R12); >> + netbsd_x86_64_collect_gp (AMD64_R13_REGNUM, R13); >> + netbsd_x86_64_collect_gp (AMD64_R14_REGNUM, R14); >> + netbsd_x86_64_collect_gp (AMD64_R15_REGNUM, R15); >> + netbsd_x86_64_collect_gp (AMD64_RIP_REGNUM, RIP); >> + netbsd_x86_64_collect_gp (AMD64_EFLAGS_REGNUM, RFLAGS); >> + netbsd_x86_64_collect_gp (AMD64_CS_REGNUM, CS); >> + netbsd_x86_64_collect_gp (AMD64_SS_REGNUM, SS); >> + netbsd_x86_64_collect_gp (AMD64_DS_REGNUM, DS); >> + netbsd_x86_64_collect_gp (AMD64_ES_REGNUM, ES); >> + netbsd_x86_64_collect_gp (AMD64_FS_REGNUM, FS); >> + netbsd_x86_64_collect_gp (AMD64_GS_REGNUM, GS); >> +} >> + >> +/* The store_function for the general-purpose register set. */ >> + >> +static void >> +netbsd_x86_64_store_gregset (struct regcache *regcache, const char *b= uf) >> +{ >> + struct reg *r =3D (struct reg *)buf; >=20 > Space after ')'. >=20 Fixed. >> + >> +#define netbsd_x86_64_supply_gp(regnum, fld) do { \ >> + supply_register (regcache, regnum, &r->regs[_REG_##fld]); \ >> + } while(0) >> + >> + netbsd_x86_64_supply_gp (AMD64_RAX_REGNUM, RAX); >> + netbsd_x86_64_supply_gp (AMD64_RBX_REGNUM, RBX); >> + netbsd_x86_64_supply_gp (AMD64_RCX_REGNUM, RCX); >> + netbsd_x86_64_supply_gp (AMD64_RDX_REGNUM, RDX); >> + netbsd_x86_64_supply_gp (AMD64_RSI_REGNUM, RSI); >> + netbsd_x86_64_supply_gp (AMD64_RDI_REGNUM, RDI); >> + netbsd_x86_64_supply_gp (AMD64_RBP_REGNUM, RBP); >> + netbsd_x86_64_supply_gp (AMD64_RSP_REGNUM, RSP); >> + netbsd_x86_64_supply_gp (AMD64_R8_REGNUM, R8); >> + netbsd_x86_64_supply_gp (AMD64_R9_REGNUM, R9); >> + netbsd_x86_64_supply_gp (AMD64_R10_REGNUM, R10); >> + netbsd_x86_64_supply_gp (AMD64_R11_REGNUM, R11); >> + netbsd_x86_64_supply_gp (AMD64_R12_REGNUM, R12); >> + netbsd_x86_64_supply_gp (AMD64_R13_REGNUM, R13); >> + netbsd_x86_64_supply_gp (AMD64_R14_REGNUM, R14); >> + netbsd_x86_64_supply_gp (AMD64_R15_REGNUM, R15); >> + netbsd_x86_64_supply_gp (AMD64_RIP_REGNUM, RIP); >> + netbsd_x86_64_supply_gp (AMD64_EFLAGS_REGNUM, RFLAGS); >> + netbsd_x86_64_supply_gp (AMD64_CS_REGNUM, CS); >> + netbsd_x86_64_supply_gp (AMD64_SS_REGNUM, SS); >> + netbsd_x86_64_supply_gp (AMD64_DS_REGNUM, DS); >> + netbsd_x86_64_supply_gp (AMD64_ES_REGNUM, ES); >> + netbsd_x86_64_supply_gp (AMD64_FS_REGNUM, FS); >> + netbsd_x86_64_supply_gp (AMD64_GS_REGNUM, GS); >> +} >> + >> +/* Implements the netbsd_target_ops.arch_setup routine. */ >> + >> +static void >> +netbsd_x86_64_arch_setup (void) >> +{ >> + struct target_desc *tdesc >> + =3D amd64_create_target_description (X86_XSTATE_SSE_MASK, false, = false, false); >> + >> + init_target_desc (tdesc, amd64_expedite_regs); >> + >> + netbsd_tdesc =3D tdesc; >> +} >> + >> +/* Update all the target description of all processes; a new GDB >> + connected, and it may or not support xml target descriptions. */ >> + >> +static void >> +x86_64_netbsd_update_xmltarget (void) >> +{ >> + struct thread_info *saved_thread =3D current_thread; >> + >> + /* Before changing the register cache's internal layout, flush the >> + contents of the current valid caches back to the threads, and >> + release the current regcache objects. */ >> + regcache_release (); >> + >> + for_each_process ([] (process_info *proc) { >> + int pid =3D proc->pid; >> + >> + /* Look up any thread of this process. */ >> + current_thread =3D find_any_thread_of_pid (pid); >> + >> + the_low_target.arch_setup (); >=20 > I find this confusing because the target object is supposed to be a sin= gleton. > Why is its arch_setup called for each process? >=20 This logic was copied from Linux, should I drop it? However.. after removal of process_qsupported this is no longer in use and I have removed the x86_64_netbsd_update_xmltarget function entirely. >> + }); >> + >> + current_thread =3D saved_thread; >> +} >> + >> +/* Process qSupported query, "xmlRegisters=3D". */ >> + >> +static void >> +netbsd_x86_64_process_qsupported (char **features, int count) >> +{ >> + /* Return if gdb doesn't support XML. If gdb sends "xmlRegisters=3D= " >> + with "i386" in qSupported query, it supports x86 XML target >> + descriptions. */ >> + use_xml =3D 0; >> + for (int i =3D 0; i < count; i++) >> + { >> + const char *feature =3D features[i]; >> + >> + if (startswith (feature, "xmlRegisters=3D")) >> + { >> + char *copy =3D xstrdup (feature + 13); >> + char *last; >> + char *p; >> + >> + for (p =3D strtok_r (copy, ",", &last); p !=3D NULL; >> + p =3D strtok_r (NULL, ",", &last)) >> + { >> + if (strcmp (p, "i386") =3D=3D 0) >> + { >> + use_xml =3D 1; >> + break; >> + } >> + } >> + >> + free (copy); >> + } >> + } >> + x86_64_netbsd_update_xmltarget (); >> +} >> + >> +/* Description of all the x86-netbsd register sets. */ >> + >> +struct netbsd_regset_info netbsd_target_regsets[] =3D >> +{ >> + /* General Purpose Registers. */ >> + {PT_GETREGS, PT_SETREGS, sizeof (struct reg), >> + netbsd_x86_64_fill_gregset, netbsd_x86_64_store_gregset}, >> + /* End of list marker. */ >> + {0, 0, -1, NULL, NULL } >> +}; >> + >> +/* The netbsd_target_ops vector for x86-netbsd. */ >> + >> +struct netbsd_target_ops the_low_target =3D >> +{ >> + netbsd_x86_64_arch_setup, >> + netbsd_x86_64_process_qsupported, >> +}; >> -- >> 2.28.0 >=20 > Regards > -Baris >=20 >=20 > Intel Deutschland GmbH > Registered Address: Am Campeon 10-12, 85579 Neubiberg, Germany > Tel: +49 89 99 8853-0, www.intel.de > Managing Directors: Christin Eisenschmid, Gary Kershaw > Chairperson of the Supervisory Board: Nicole Lau > Registered Office: Munich > Commercial Register: Amtsgericht Muenchen HRB 186928 >=20 --5jrBwy0cyQdMlXWc9IJxeCiSOzOTBshCT-- --jYtdwdlo4mWpXnnLd7hHMiolvhDRb3R5e Content-Type: application/pgp-signature; name="signature.asc" Content-Description: OpenPGP digital signature Content-Disposition: attachment; filename="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAEBCAAdFiEELaxVpweEzw+lMDwuS7MI6bAudmwFAl9Rho8ACgkQS7MI6bAu dmwWTBAAnDVgaZGkpwfVAP9MkGXFVlMhVJkBhnQKCjU/X/0sb/FDxeZIBBRvJw0e 1RqHPCQfaIG4++jF78Zw2b0KpBhEhY600n8rjC7O7/I2aIfHVwBuPRANVgFir5Zq +G/KL4diAV9B3W7DCq5mRG5P8OrzpjqCBiV3uEFcuGhCQHA/30wHEaFltSUDtXya wrb0FJ0pqXc1XDOFYxASsFuKlnhKzHJWSf07x2z8PcfaraVKYBJ1+dIqDVDoZI5g hzN1ef7gd0KoxvWXa1xK5WLdlkJFcalFH2MgU0FIhyyWembkpsbcb4OmaKBfV9im 6IIrgGMPQ4LFIL51ujOqPaMwtSJRpi7zW2TZcmiESkAqf9DagiDC5A4hEixEJqca byakQrXu+9cG5mUrWAArk7BovopHCKQMYvE8iJAG4GE7fGvQybHe46f6PfFVwbPy p3sPgduvDA0SbnEemuPrm3mxm/2NaC8xcaOadpuEi+MwIXmXqjj+DKPKswHV6AVr wxpS6EN1lX5LwfcTcXX+2FxhSZF81vhgrJIi4OBJi9hBOXO00G8Lma00Bh6jKUbb ZjjUTlkKzFi48kfUX5Ic4GWGq2BSWx8/cYLj7xo/odHb8Cig5UJ228EeLJxUEIzu fKgkZrBHx+Uaj9uu03ssTUcTC25xlKdzCACWHD1SFXpfskJlyyQ= =QDK/ -----END PGP SIGNATURE----- --jYtdwdlo4mWpXnnLd7hHMiolvhDRb3R5e--