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 E8660385B834 for ; Mon, 30 Mar 2020 21:19:08 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org E8660385B834 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=1585603116; bh=l4l/KL1J9Hh2i7NUL6sCpoSTHCEM3STmKpeQEusy2K4=; h=X-UI-Sender-Class:Subject:To:Cc:References:From:Date:In-Reply-To; b=fcNvlMGjDiAiRFrAk6mYI+DbFdT4fwNU6Q+PzaNpyYygRM3NtJcQO12PRQmzX1kah YfqIkmYGHSLiHUh+b8GvfTNOGATK2UsV+b33/wMA5TixQEvqozEM0MBvWg5LlihvRn r4IhDEfhahJiJwGMqBhMdGwTRor6zWiTZx8nhWfc= X-UI-Sender-Class: 01bb95c1-4bf8-414a-932a-4f6e2808ef9c Received: from [192.168.0.241] ([89.79.191.25]) by mail.gmx.com (mrgmx005 [212.227.17.184]) with ESMTPSA (Nemesis) id 1MLzBj-1japnm48Y5-00HxiX; Mon, 30 Mar 2020 23:18:36 +0200 Subject: Re: [PATCH v2] Implement basic threading support in the NetBSD target To: gdb-patches@sourceware.org References: <20200326025314.2392-1-n54@gmx.com> <20200326031658.3659-1-n54@gmx.com> From: Kamil Rytarowski Message-ID: <2c845809-674b-1f48-5922-580e73567386@gmx.com> Date: Mon, 30 Mar 2020 23:17:27 +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: <20200326031658.3659-1-n54@gmx.com> Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: quoted-printable X-Provags-ID: V03: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UskcOWBmCsS9z8GYN Aam0enk+Pn28fVAHzhNIKMcOmImkNl2BGtyGRASpINDxOiElB7mQ9UVwrdKLRT5pA+baiVNlz o4xwyrDgrqZ+JTYih7XDUCL+PFjAj11W9w0fVz0oGPG34yeVGa4C7m/MJyI7NznmRuIBDMoVF r8F2LhR3XlrvtkKqRgQYA0g1nhYLxILbHDUjfsfFrsqZLpc0oLi/dYJln9yLe3iYpQnBsLvp2 xXvSG7MtYosc4rnM4eQmPasN69hI7HRyqcSKgkrTFYwzbXhKRUhxHRnkgc/vW5JbPCk7Jz70e zwYmaQ5dAohEOAEfocGixN2mXmwrYWoYJ0UqbgMhhWPOq3K3M2pan0RWV2zKZ//J5qdH++li+ RgRPw2WvE766OvxDbm++SaAV7kg4rRMYGNBPC5KGu0l82Le6bXr+BG4ayY8dpkXBAr9rA= X-Spam-Status: No, score=-29.0 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, FREEMAIL_ENVFROM_END_DIGIT, FREEMAIL_FROM, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, RCVD_IN_BARRACUDACENTRAL, RCVD_IN_DNSWL_LOW, SPF_HELO_NONE, SPF_PASS, TXREP, URIBL_CSS, URIBL_CSS_A 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: Mon, 30 Mar 2020 21:19:11 -0000 Ping? On 26.03.2020 04:16, Kamil Rytarowski wrote: > Use sysctl(3) as the portable interface to prompt NetBSD threads on > all supported NetBSD versions. In future newer versions could switch > to PT_LWPSTATUS ptrace(2) API that will be supported on NetBSD 10.0 > and newer. > > Implement as part of nbsd_nat_target: > - thread_name() - read descriptive thread name > - thread_alive() - check whether a thread is alive > - post_attach() - updates the list of threads after attach > - update_thread_list() - updates the list of threads > - pid_to_str() - translates ptid to a descriptive string > > There are two local static functions: > - nbsd_thread_lister() - generic LWP lister for a specified pid > - nbsd_add_threads() - utility to update the list of threads > > Now, GDB on NetBSD can attach to a multithreaded process, spawn > a multithreaded process, list threads, print their LWP+PID numbers > and descriptive thread names. > > gdb/ChangeLog: > > * nbsd-nat.h (struct thread_info): Add forward declaration. > (nbsd_nat_target::thread_alive): Add. > (nbsd_nat_target::thread_name): Likewise. > (nbsd_nat_target::update_thread_list): Likewise. > (update_thread_list::post_attach): Likewise. > (post_attach::pid_to_str): Likewise. > * nbsd-nat.c: Include "gdbthread.h" and "inferior.h". > (nbsd_thread_lister): Add. > (nbsd_nat_target::thread_alive): Likewise. > (nbsd_nat_target::thread_name): Likewise. > (nbsd_add_threads): Likewise. > (update_thread_list::post_attach): Likewise. > (nbsd_nat_target::update_thread_list): Likewise. > (post_attach::pid_to_str): Likewise. > --- > gdb/ChangeLog | 17 +++++ > gdb/nbsd-nat.c | 166 +++++++++++++++++++++++++++++++++++++++++++++++++ > gdb/nbsd-nat.h | 8 +++ > 3 files changed, 191 insertions(+) > > diff --git a/gdb/ChangeLog b/gdb/ChangeLog > index 1ee58a541cb..f31a7cf236c 100644 > --- a/gdb/ChangeLog > +++ b/gdb/ChangeLog > @@ -1,3 +1,20 @@ > +2020-03-25 Kamil Rytarowski > + > + * nbsd-nat.h (struct thread_info): Add forward declaration. > + (nbsd_nat_target::thread_alive): Add. > + (nbsd_nat_target::thread_name): Likewise. > + (nbsd_nat_target::update_thread_list): Likewise. > + (update_thread_list::post_attach): Likewise. > + (post_attach::pid_to_str): Likewise. > + * nbsd-nat.c: Include "gdbthread.h" and "inferior.h". > + (nbsd_thread_lister): Add. > + (nbsd_nat_target::thread_alive): Likewise. > + (nbsd_nat_target::thread_name): Likewise. > + (nbsd_add_threads): Likewise. > + (update_thread_list::post_attach): Likewise. > + (nbsd_nat_target::update_thread_list): Likewise. > + (post_attach::pid_to_str): Likewise. > + > 2020-03-25 Tom Tromey > > * compile/compile-object-load.c (get_out_value_type): Mention > diff --git a/gdb/nbsd-nat.c b/gdb/nbsd-nat.c > index 326bbe3aec3..945699ccfbe 100644 > --- a/gdb/nbsd-nat.c > +++ b/gdb/nbsd-nat.c > @@ -20,6 +20,8 @@ > #include "defs.h" > > #include "nbsd-nat.h" > +#include "gdbthread.h" > +#include "inferior.h" > > #include > #include > @@ -39,3 +41,167 @@ nbsd_nat_target::pid_to_exec_file (int pid) > return NULL; > return buf; > } > + > +/* Generic thread (LWP) lister within a specified process. The callbac= k > + parameters is a C++ function that is called for each detected thread= . */ > + > +static void > +nbsd_thread_lister (const pid_t pid, > + std::function 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 ||= size =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 (kl =3D=3D NULL) > + perror_with_name (("calloc")); > + > + 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.get ()[i]; > + > + /* Return true if the specified thread is alive. */ > + auto lwp_alive =3D > + [&l]() > + { > + switch (l->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 ()) > + continue; > + > + if (callback (l)) > + break; > + } > +} > + > +/* 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 (); > + bool found =3D false; > + > + auto fn =3D > + [&lwp, &found] (const struct kinfo_lwp *kl) > + { > + if (kl->l_lid =3D=3D lwp) > + { > + found =3D true; > + return true; > + } > + return false; > + }; > + > + nbsd_thread_lister (pid, fn); > + > + return found; > +} > + > +/* Return the name assigned to a thread by an application. Returns > + the string in a static buffer. */ > + > +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; > + }; > + > + nbsd_thread_lister (pid, fn); > + > + return buf; > +} > + > +/* Implement the "post_attach" target_ops method. */ > + > +static void > +nbsd_add_threads (nbsd_nat_target *target, pid_t pid) > +{ > + auto fn =3D > + [&target, &pid] (const struct kinfo_lwp *kl) > + { > + ptid_t ptid =3D ptid_t (pid, kl->l_lid, 0); > + if (!in_thread_list (target, ptid)) > + { > + if (inferior_ptid.lwp () =3D=3D 0) > + thread_change_ptid (target, inferior_ptid, ptid); > + else > + add_thread (target, ptid); > + } > + return false; > + }; > + > + nbsd_thread_lister (pid, fn); > +} > + > +/* Implement the "post_attach" target_ops method. */ > + > +void > +nbsd_nat_target::post_attach (int pid) > +{ > + nbsd_add_threads (this, pid); > +} > + > +/* Implement the "update_thread_list" target_ops method. */ > + > +void > +nbsd_nat_target::update_thread_list () > +{ > + prune_threads (); > + > + nbsd_add_threads (this, inferior_ptid.pid ()); > +} > + > +/* Convert PTID to a string. */ > + > +std::string > +nbsd_nat_target::pid_to_str (ptid_t ptid) > +{ > + int lwp =3D ptid.lwp (); > + > + if (lwp !=3D 0) > + { > + pid_t pid =3D ptid.pid (); > + > + return string_printf ("LWP %d of process %d", lwp, pid); > + } > + > + return normal_pid_to_str (ptid); > +} > diff --git a/gdb/nbsd-nat.h b/gdb/nbsd-nat.h > index a752fbe572d..3606048cd07 100644 > --- a/gdb/nbsd-nat.h > +++ b/gdb/nbsd-nat.h > @@ -22,11 +22,19 @@ > > #include "inf-ptrace.h" > > +struct thread_info; > + > /* A prototype NetBSD target. */ > > struct nbsd_nat_target : public inf_ptrace_target > { > char *pid_to_exec_file (int pid) override; > + > + bool thread_alive (ptid_t ptid) override; > + const char *thread_name (struct thread_info *thr) override; > + void post_attach (int pid) override; > + void update_thread_list () override; > + std::string pid_to_str (ptid_t ptid) override; > }; > > #endif /* nbsd-nat.h */ > -- > 2.25.0 >