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 C1FA73986418 for ; Fri, 4 Sep 2020 00:30:05 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org C1FA73986418 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=1599179387; bh=ZYP3ITmO5w2j7qz62r4CdU1yGGYDq6L5CRvMo/Uw51Y=; h=X-UI-Sender-Class:From:To:Cc:Subject:Date:In-Reply-To:References; b=PzbxoQBKgBV1zF4twgh6DfW4QZeV6idK4xQw6Tl9rPYnajlcEJhksHqLMPDFm+X8q vSFjGpBGu3uGqgrBWC0RvBkhM6ie+A18pAXc2Cz8s6LLuBBzoF+GoRM8lxjqkGVC7O k3dZJd4ffaM2JxfiMONeG0Rfnq7s/yM0oQ/Pxqlw= 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 1MgvrL-1klgzm1KHt-00hR52; Fri, 04 Sep 2020 02:29:47 +0200 From: Kamil Rytarowski To: gdb-patches@sourceware.org Cc: tom@tromey.com, tankut.baris.aktemur@intel.com, Kamil Rytarowski Subject: [PATCH v2 05/10] Add gdb/nat common functions for listing threads Date: Fri, 4 Sep 2020 02:29:00 +0200 Message-Id: <20200904002905.13616-6-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:WRFG0spGxJt+Oz3GYRe1z+OGaMrgEoDb1I3LJ3H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h6yScZImAzcEOikiI0SXKZEISIBlFRH/oIAbZxPZFF843II 9x6+tAm6jAs5ZKsh0M8jsZBCaOs3bvgyHbQs5c5wLOGABp4VSLI2FozTR3chSayd5xv/LdAVZ BeNRTa6qld8YWEaGbfb5H3Edl36tPeFhOvfhAvpl7zpCFX0nfjTFtHLgnnqxaffRhJrXxHS9g V4yWX8PP8xf17Jq1bhirQN6GPKSDYdgCwlMY6IYiK2I3WVyFYHTA/Y/ZDY4fGT6lhxBGR5NFA YFlFPKKWU5SJM3vt41Q5z5A/xjt3glRroYAJEXBxXwDCb5wvoO0kiYi26fuHJ8UuejyObBMwn aQojaCYodEwGkupt3lf2RoZjyd3YjsM9g6uudEKAEQnXIi0f5OY1Wbgp/EziaikXtjofEzx+P ehMBFl2d9hgcAxZ0I 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, 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: Fri, 04 Sep 2020 00:30:08 -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::for_each_thread gdb/ChangeLog: * netbsd-nat.h: Include "gdbsupport/function-view.h". * (netbsd_nat::thread_alive, netbsd_nat::thread_name) (netbsd_nat::for_each_thread): Add. * netbsd-nat.c: Include "gdbsupport/common-defs.h" and "gdbsupport/common-debug.h". * (netbsd_nat::netbsd_thread_lister) (netbsd_nat::thread_alive, netbsd_nat::thread_name) (netbsd_nat::for_each_thread): Add. =2D-- gdb/ChangeLog | 11 ++++ gdb/nat/netbsd-nat.c | 123 +++++++++++++++++++++++++++++++++++++++++++ gdb/nat/netbsd-nat.h | 8 +++ 3 files changed, 142 insertions(+) diff --git a/gdb/ChangeLog b/gdb/ChangeLog index 335d6b7271f..b9b028ebe68 100644 =2D-- a/gdb/ChangeLog +++ b/gdb/ChangeLog @@ -1,3 +1,14 @@ +2020-09-04 Kamil Rytarowski + + * netbsd-nat.h: Include "gdbsupport/function-view.h". + * (netbsd_nat::thread_alive, netbsd_nat::thread_name) + (netbsd_nat::for_each_thread): Add. + * netbsd-nat.c: Include "gdbsupport/common-defs.h" and + "gdbsupport/common-debug.h". + * (netbsd_nat::netbsd_thread_lister) + (netbsd_nat::thread_alive, netbsd_nat::thread_name) + (netbsd_nat::for_each_thread): Add. + 2020-09-04 Kamil Rytarowski * netbsd-nat.h: Include . diff --git a/gdb/nat/netbsd-nat.c b/gdb/nat/netbsd-nat.c index 297188bb8b4..bd28e116b56 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 +for_each_thread (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..c3a58ae7f49 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 for_each_thread (pid_t pid, + gdb::function_view callback); } #endif =2D- 2.28.0