From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mout.gmx.net (mout.gmx.net [212.227.15.18]) by sourceware.org (Postfix) with ESMTPS id 0BDD33986419 for ; Fri, 4 Sep 2020 00:30:05 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 0BDD33986419 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=gmx.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=n54@gmx.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gmx.net; s=badeba3b8450; t=1599179388; bh=7/p2Tsh0zna0+be0nmimeGR+yycAuRGb9z4rVpeDhlU=; h=X-UI-Sender-Class:From:To:Cc:Subject:Date:In-Reply-To:References; b=L3knVimyDQ+akNVbP+hHo3CLxJwweMXHnm0xyYU8N/CxxDJbRankkVf4lGEarami6 Tqkn2T0aH36LUwbr6a4XLi4VG7WEyXR3Hludq5Jtsoi1uYrAMjmj2j2nOmDc9t6TRR /5PvorCLZkdzKEBCOEoaYATkzB0pzTtkiBWtN+Ds= X-UI-Sender-Class: 01bb95c1-4bf8-414a-932a-4f6e2808ef9c Received: from localhost.localdomain ([89.79.191.25]) by mail.gmx.com (mrgmx005 [212.227.17.184]) with ESMTPSA (Nemesis) id 1M7K3i-1kCuCu2qce-007pLZ; Fri, 04 Sep 2020 02:29:48 +0200 From: Kamil Rytarowski To: gdb-patches@sourceware.org Cc: tom@tromey.com, tankut.baris.aktemur@intel.com, Kamil Rytarowski Subject: [PATCH v2 09/10] Switch local native code to gdb/nat shared functions Date: Fri, 4 Sep 2020 02:29:04 +0200 Message-Id: <20200904002905.13616-10-n54@gmx.com> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200904002905.13616-1-n54@gmx.com> References: <20200904002905.13616-1-n54@gmx.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Provags-ID: V03:K1:fSw8Misj96EM8DYdar7uG9Vj93GzQMbqJ0tNrieF4PeMdChs1nb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EPiAEbKCK5BB+SOOmDKqxPJvOhpjxXA gc+wt9f4PtHc4BbbKt2sFRzZA66AG5SnS1Il98JHYsa/u2NbkgQzncQRY8P9KaKUNqTKTqx/o nkXopztIycJTYsHFhLcz9AfkIhsyAZbZIkH+XyxiIjFbRMdV9C+6cfrYV1I/TlOZ6PaNUsX1D 8SnTtvV7oYEXAxKduk6tfrqxp3sWWkdt/dCkQkXZ9rWzla8mcZxVd74IF/J87oJukCo1am1kP yUBaX1bqFQG0Z2Xe/ErzqxLUJ4xOGIzKS7oE0P8WwCF5uzky53jgNdVfxuZLeToLyFE+3+pOS IuJ6X7h8vKWo2FVQh5gA2PjQFgDQjbn8n1fNn7z9jtFaG36FKoTtEtMzuezjVeroYEu/tfD2q 8nEYV7dpDmu6DuknS X-Spam-Status: No, score=-17.1 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, FREEMAIL_ENVFROM_END_DIGIT, FREEMAIL_FROM, GIT_PATCH_0, RCVD_IN_BARRACUDACENTRAL, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H2, 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:30:08 -0000 No functional change as the same functionality inlined in nbsd-nat.c is offered in gdb/nat/netbsd-nat.c. gdb/ChangeLog: * nbsd-nat.c: Include "nat/netbsd-nat.h". * (nbsd_nat_target::pid_to_exec_file) (nbsd_nat_target::thread_alive, nbsd_nat_target::thread_name) (nbsd_nat_target::post_startup_inferior) (nbsd_nat_target::post_attach, nbsd_nat_target::xfer_partial) (nbsd_add_threads): Switch local code to common gdb/nat functions. * (nbsd_pid_to_cmdline): Call sysctl from the global namespace. * (nbsd_thread_lister): Remove. =2D-- gdb/ChangeLog | 11 ++++ gdb/nbsd-nat.c | 147 +++++-------------------------------------------- 2 files changed, 24 insertions(+), 134 deletions(-) diff --git a/gdb/ChangeLog b/gdb/ChangeLog index 1013f6a0b3c..2717101ebdd 100644 =2D-- a/gdb/ChangeLog +++ b/gdb/ChangeLog @@ -1,3 +1,14 @@ +2020-09-04 Kamil Rytarowski + + * nbsd-nat.c: Include "nat/netbsd-nat.h". + * (nbsd_nat_target::pid_to_exec_file) + (nbsd_nat_target::thread_alive, nbsd_nat_target::thread_name) + (nbsd_nat_target::post_startup_inferior) + (nbsd_nat_target::post_attach, nbsd_nat_target::xfer_partial) + (nbsd_add_threads): Switch local code to common gdb/nat functions. + * (nbsd_pid_to_cmdline): Call sysctl from the global namespace. + * (nbsd_thread_lister): Remove. + 2020-09-04 Kamil Rytarowski * fork-inferior.c (startup_inferior): Avoid double free. diff --git a/gdb/nbsd-nat.c b/gdb/nbsd-nat.c index 52c4d185695..8326fdc6552 100644 =2D-- a/gdb/nbsd-nat.c +++ b/gdb/nbsd-nat.c @@ -20,6 +20,7 @@ #include "defs.h" #include "nbsd-nat.h" +#include "nat/netbsd-nat.h" #include "gdbthread.h" #include "nbsd-tdep.h" #include "inferior.h" @@ -36,13 +37,7 @@ char * nbsd_nat_target::pid_to_exec_file (int pid) { - static char buf[PATH_MAX]; - size_t buflen; - int mib[4] =3D {CTL_KERN, KERN_PROC_ARGS, pid, KERN_PROC_PATHNAME}; - buflen =3D sizeof (buf); - if (sysctl (mib, ARRAY_SIZE (mib), buf, &buflen, NULL, 0)) - return NULL; - return buf; + return netbsd_nat::pid_to_exec_file (pid); } /* Return the current directory for the process identified by PID. */ @@ -80,12 +75,12 @@ nbsd_pid_to_cmdline (int pid) int mib[4] =3D {CTL_KERN, KERN_PROC_ARGS, pid, KERN_PROC_ARGV}; size_t size =3D 0; - if (sysctl (mib, ARRAY_SIZE (mib), NULL, &size, NULL, 0) =3D=3D -1 || s= ize =3D=3D 0) + if (::sysctl (mib, ARRAY_SIZE (mib), NULL, &size, NULL, 0) =3D=3D -1 ||= size =3D=3D 0) return nullptr; gdb::unique_xmalloc_ptr args (XNEWVAR (char, size)); - if (sysctl (mib, ARRAY_SIZE (mib), args.get (), &size, NULL, 0) =3D=3D = -1 + if (::sysctl (mib, ARRAY_SIZE (mib), args.get (), &size, NULL, 0) =3D= =3D -1 || size =3D=3D 0) return nullptr; @@ -99,76 +94,12 @@ nbsd_pid_to_cmdline (int pid) return args; } -/* Generic thread (LWP) lister within a specified process. The callback - parameters is a C++ function that is called for each detected thread. = */ - -static bool -nbsd_thread_lister (const pid_t pid, - gdb::function_view - callback) -{ - int mib[5] =3D {CTL_KERN, KERN_LWP, pid, sizeof (struct kinfo_lwp), 0}; - size_t size; - - if (sysctl (mib, ARRAY_SIZE (mib), NULL, &size, NULL, 0) =3D=3D -1 || s= ize =3D=3D 0) - perror_with_name (("sysctl")); - - mib[4] =3D size / sizeof (size_t); - - gdb::unique_xmalloc_ptr kl - ((struct kinfo_lwp *) xcalloc (size, 1)); - - if (sysctl (mib, ARRAY_SIZE (mib), kl.get (), &size, NULL, 0) =3D=3D -1 - || size =3D=3D 0) - perror_with_name (("sysctl")); - - for (size_t i =3D 0; i < size / sizeof (struct kinfo_lwp); i++) - { - struct kinfo_lwp *l =3D &kl[i]; - - /* Return true if the specified thread is alive. */ - auto lwp_alive - =3D [] (struct kinfo_lwp *lwp) - { - switch (lwp->l_stat) - { - case LSSLEEP: - case LSRUN: - case LSONPROC: - case LSSTOP: - case LSSUSPENDED: - return true; - default: - return false; - } - }; - - /* Ignore embryonic or demised threads. */ - if (!lwp_alive (l)) - continue; - - if (callback (l)) - return true; - } - - return false; -} - /* Return true if PTID is still active in the inferior. */ bool nbsd_nat_target::thread_alive (ptid_t ptid) { - pid_t pid =3D ptid.pid (); - int lwp =3D ptid.lwp (); - - auto fn - =3D [&lwp] (const struct kinfo_lwp *kl) - { - return kl->l_lid =3D=3D lwp; - }; - - return nbsd_thread_lister (pid, fn); + return netbsd_nat::thread_alive (ptid); } /* Return the name assigned to a thread by an application. Returns @@ -178,26 +109,7 @@ const char * nbsd_nat_target::thread_name (struct thread_info *thr) { ptid_t ptid =3D thr->ptid; - pid_t pid =3D ptid.pid (); - int lwp =3D ptid.lwp (); - - static char buf[KI_LNAMELEN] =3D {}; - - auto fn - =3D [&lwp] (const struct kinfo_lwp *kl) - { - if (kl->l_lid =3D=3D lwp) - { - xsnprintf (buf, sizeof buf, "%s", kl->l_name); - return true; - } - return false; - }; - - if (nbsd_thread_lister (pid, fn)) - return buf; - else - return NULL; + return netbsd_nat::thread_name (ptid); } /* Implement the "post_attach" target_ops method. */ @@ -206,9 +118,8 @@ static void nbsd_add_threads (nbsd_nat_target *target, pid_t pid) { auto fn - =3D [&target, &pid] (const struct kinfo_lwp *kl) + =3D [&target] (ptid_t ptid) { - ptid_t ptid =3D ptid_t (pid, kl->l_lid, 0); if (!in_thread_list (target, ptid)) { if (inferior_ptid.lwp () =3D=3D 0) @@ -216,27 +127,9 @@ nbsd_add_threads (nbsd_nat_target *target, pid_t pid) else add_thread (target, ptid); } - return false; }; - nbsd_thread_lister (pid, fn); -} - -/* Enable additional event reporting on new processes. */ - -static void -nbsd_enable_proc_events (pid_t pid) -{ - int events; - - if (ptrace (PT_GET_EVENT_MASK, pid, &events, sizeof (events)) =3D=3D -1= ) - perror_with_name (("ptrace")); - - events |=3D PTRACE_LWP_CREATE; - events |=3D PTRACE_LWP_EXIT; - - if (ptrace (PT_SET_EVENT_MASK, pid, &events, sizeof (events)) =3D=3D -1= ) - perror_with_name (("ptrace")); + netbsd_nat::for_each_thread (pid, fn); } /* Implement the "post_startup_inferior" target_ops method. */ @@ -244,7 +137,7 @@ nbsd_enable_proc_events (pid_t pid) void nbsd_nat_target::post_startup_inferior (ptid_t ptid) { - nbsd_enable_proc_events (ptid.pid ()); + netbsd_nat::enable_proc_events (ptid.pid ()); } /* Implement the "post_attach" target_ops method. */ @@ -252,7 +145,7 @@ nbsd_nat_target::post_startup_inferior (ptid_t ptid) void nbsd_nat_target::post_attach (int pid) { - nbsd_enable_proc_events (pid); + netbsd_nat::enable_proc_events (pid); nbsd_add_threads (this, pid); } @@ -861,26 +754,12 @@ nbsd_nat_target::xfer_partial (enum target_object ob= ject, { case TARGET_OBJECT_SIGNAL_INFO: { - ptrace_siginfo_t psi; + len =3D netbsd_nat::qxfer_siginfo(pid, annex, readbuf, writebuf, offset, + len); - if (offset > sizeof (siginfo_t)) + if (len =3D=3D -1) return TARGET_XFER_E_IO; - if (ptrace (PT_GET_SIGINFO, pid, &psi, sizeof (psi)) =3D=3D -1) - return TARGET_XFER_E_IO; - - if (offset + len > sizeof (siginfo_t)) - len =3D sizeof (siginfo_t) - offset; - - if (readbuf !=3D NULL) - memcpy (readbuf, ((gdb_byte *) &psi.psi_siginfo) + offset, len); - else - { - memcpy (((gdb_byte *) &psi.psi_siginfo) + offset, writebuf, len); - - if (ptrace (PT_SET_SIGINFO, pid, &psi, sizeof (psi)) =3D=3D -1) - return TARGET_XFER_E_IO; - } *xfered_len =3D len; return TARGET_XFER_OK; } =2D- 2.28.0