From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mout.gmx.net (mout.gmx.net [212.227.17.22]) by sourceware.org (Postfix) with ESMTPS id 4EC893959E5D for ; Wed, 2 Sep 2020 18:00:11 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 4EC893959E5D 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=1599069610; bh=bbPN9/JWJ7P2S4Xctw602RpnZ9Tk6f1eyrM6rqQhL3E=; h=X-UI-Sender-Class:From:To:Cc:Subject:Date:In-Reply-To:References; b=Z86EpLufLZiYWwIM66M0TZgNaG03GznU+TiYF/rEhjymwwbrZqY0If5GIpYVuyz6a h7FS8AjjejeOnwEWDSq+sMCnoi3H1nWISOp6pW2n6kBoZjE7nYztcMfhtoqC6np4FH N6HuTjqtBOfA1IH1oY52iZ/O0bfjm1BY39Em+jBE= X-UI-Sender-Class: 01bb95c1-4bf8-414a-932a-4f6e2808ef9c Received: from localhost.localdomain ([89.79.191.25]) by mail.gmx.com (mrgmx104 [212.227.17.174]) with ESMTPSA (Nemesis) id 1MfpSb-1koUQy3mSf-00gILa; Wed, 02 Sep 2020 20:00:10 +0200 From: Kamil Rytarowski To: gdb-patches@sourceware.org Subject: [PATCH 05/10] Add gdb/nat common functions for listing threads Date: Wed, 2 Sep 2020 19:59:21 +0200 Message-Id: <20200902175926.13182-6-n54@gmx.com> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200902175926.13182-1-n54@gmx.com> References: <20200902175926.13182-1-n54@gmx.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Provags-ID: V03:K1:eVlNyngi65oPzImmhD/O+uyHSeEeDdcKZjcTkrkW08rL5B8Dt8G 2nuJISwOZQ9k18pnZMUzHZwt7AFLGoNVGvwPIDYIrJ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ilm9prGb3GVfu+Za9mJbFKY2nV/LIWlmVF2VBuF4gTr/FpELJj zKitb5BV4Cprp+3ME6st1HP/UUAliX8kHPtRDIKD1y57Rv2sVuM+auyl0pWTnJcLQSjY3i94/ mCjPP+z5EUulQ8olnEnYiKNm7CWDfc4O57U0iJA+Y+ekFcYER8TUol/TM3fkgd4n5AEDWj0vP +nY1EEiBDGjz1qHN19B/f2laPDXV/M204rja8Is3YVJzu//6kZLg9bOiG2N7+EPjqPS+26lXm vFD0r/Ka0CBwpg00tkX5RHBq3bJMB/lxWuJXqRq7gDwdF6h+BQXKpEBj3C6ZFOjbAVXc5xmcv OVqgImFoshmGklL7p X-Spam-Status: No, score=-17.9 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, FREEMAIL_ENVFROM_END_DIGIT, FREEMAIL_FROM, GIT_PATCH_0, KAM_SHORT, 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: Wed, 02 Sep 2020 18:00:13 -0000 Add netbsd_nat::netbsd_thread_lister a generic thread lister, used internally in netbsd-nat.c, copied from gdb/nbsd-nat.c. Add public extern functions for listing threads: * netbsd_nat::thread_alive * netbsd_nat::thread_name * netbsd_nat::list_threads gdb/ChangeLog: * netbsd-nat.h (netbsd_nat::thread_alive, netbsd_nat::thread_name) (netbsd_nat::list_threads): Add. * netbsd-nat.c (netbsd_nat::netbsd_thread_lister) (netbsd_nat::thread_alive, netbsd_nat::thread_name) (netbsd_nat::list_threads): Add. =2D-- gdb/ChangeLog | 8 +++ gdb/nat/netbsd-nat.c | 123 +++++++++++++++++++++++++++++++++++++++++++ gdb/nat/netbsd-nat.h | 8 +++ 3 files changed, 139 insertions(+) diff --git a/gdb/ChangeLog b/gdb/ChangeLog index 2adfd3d79d1..8b9ca1e9b32 100644 =2D-- a/gdb/ChangeLog +++ b/gdb/ChangeLog @@ -1,3 +1,11 @@ +2020-08-13 Kamil Rytarowski + + * netbsd-nat.h (netbsd_nat::thread_alive, netbsd_nat::thread_name) + (netbsd_nat::list_threads): Add. + * netbsd-nat.c (netbsd_nat::netbsd_thread_lister) + (netbsd_nat::thread_alive, netbsd_nat::thread_name) + (netbsd_nat::list_threads): Add. + 2020-08-13 Kamil Rytarowski * netbsd-nat.h (netbsd_nat::pid_to_exec_file): Add. diff --git a/gdb/nat/netbsd-nat.c b/gdb/nat/netbsd-nat.c index 297188bb8b4..5f005c38bf2 100644 =2D-- a/gdb/nat/netbsd-nat.c +++ b/gdb/nat/netbsd-nat.c @@ -17,11 +17,17 @@ You should have received a copy of the GNU General Public License along with this program. If not, see . = */ +#include "gdbsupport/common-defs.h" #include "nat/netbsd-nat.h" +#include "gdbsupport/common-debug.h" #include #include +#include + +#include "gdbsupport/function-view.h" + namespace netbsd_nat { @@ -39,4 +45,121 @@ pid_to_exec_file (pid_t pid) return buf; } +/* Generic thread (LWP) lister within a specified process. The callback + parameters is a C++ function that is called for each detected thread. = */ + +static bool +netbsd_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 +thread_alive (ptid_t ptid) +{ + pid_t pid =3D ptid.pid (); + lwpid_t lwp =3D ptid.lwp (); + + auto fn + =3D [&lwp] (const struct kinfo_lwp *kl) + { + return kl->l_lid =3D=3D lwp; + }; + + return netbsd_thread_lister (pid, fn); +} + +/* Return the name assigned to a thread by an application. Returns + the string in a static buffer. */ + +const char * +thread_name (ptid_t ptid) +{ + pid_t pid =3D ptid.pid (); + lwpid_t 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 (netbsd_thread_lister (pid, fn)) + return buf; + else + return NULL; +} + +/* A generic thread lister within a specific PID. The CALLBACK parameter + is a C++ function that is called for each detected thread. */ + +void +list_threads (pid_t pid, gdb::function_view callback) +{ + auto fn + =3D [&callback, &pid] (const struct kinfo_lwp *kl) + { + ptid_t ptid =3D ptid_t (pid, kl->l_lid, 0); + callback (ptid); + return false; + }; + + netbsd_thread_lister (pid, fn); +} + } diff --git a/gdb/nat/netbsd-nat.h b/gdb/nat/netbsd-nat.h index a5f664d95f4..b6885e1a9a6 100644 =2D-- a/gdb/nat/netbsd-nat.h +++ b/gdb/nat/netbsd-nat.h @@ -20,6 +20,8 @@ #ifndef NAT_NETBSD_NAT_H #define NAT_NETBSD_NAT_H +#include "gdbsupport/function-view.h" + #include namespace netbsd_nat @@ -27,6 +29,12 @@ namespace netbsd_nat extern char *pid_to_exec_file (pid_t pid); +extern bool thread_alive (ptid_t ptid); + +extern const char *thread_name (ptid_t ptid); + +extern void list_threads (pid_t pid, + gdb::function_view callback); } #endif =2D- 2.28.0