From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 32464 invoked by alias); 16 Dec 2014 20:52:48 -0000 Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org Received: (qmail 32452 invoked by uid 89); 16 Dec 2014 20:52:47 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-2.0 required=5.0 tests=AWL,BAYES_00,SPF_PASS autolearn=ham version=3.3.2 X-HELO: usevmg21.ericsson.net Received: from usevmg21.ericsson.net (HELO usevmg21.ericsson.net) (198.24.6.65) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES256-SHA encrypted) ESMTPS; Tue, 16 Dec 2014 20:52:45 +0000 Received: from EUSAAHC005.ericsson.se (Unknown_Domain [147.117.188.87]) by usevmg21.ericsson.net (Symantec Mail Security) with SMTP id 91.81.25146.8BE30945; Tue, 16 Dec 2014 15:16:24 +0100 (CET) Received: from [142.133.110.254] (147.117.188.8) by smtps-am.internal.ericsson.com (147.117.188.87) with Microsoft SMTP Server (TLS) id 14.3.195.1; Tue, 16 Dec 2014 15:52:42 -0500 Message-ID: <54909B99.5070806@ericsson.com> Date: Tue, 16 Dec 2014 20:52:00 -0000 From: Simon Marchi User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.3.0 MIME-Version: 1.0 To: Subject: Re: [PATCH 2/5] Linux: on attach, attach to lwps listed under /proc/$pid/task/ References: <1418748834-27545-1-git-send-email-palves@redhat.com> <1418748834-27545-3-git-send-email-palves@redhat.com> In-Reply-To: <1418748834-27545-3-git-send-email-palves@redhat.com> Content-Type: multipart/signed; protocol="application/pkcs7-signature"; micalg=sha1; boundary="------------ms040406090005000901050605" X-IsSubscribed: yes X-SW-Source: 2014-12/txt/msg00452.txt.bz2 --------------ms040406090005000901050605 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Content-length: 6381 On 2014-12-16 11:53 AM, Pedro Alves wrote: > ... instead of relying on libthread_db. >=20 > I wrote a test that attaches to a program that constantly spawns > short-lived threads, which exposed several issues. This is one of > them. >=20 > On Linux, we need to attach to all threads of a process (thread group) > individually. We currently rely on libthread_db to list the threads, > but that is problematic, because libthread_db relies on reading data > structures out of the inferior (which may well be corrupted). If > threads are being created or exiting just while we try to attach, we > may trip on inconsistencies in the inferior's thread list. To work > around that, when we see a seemingly corrupt list, we currently retry > a few times: >=20 > static void > thread_db_find_new_threads_2 (ptid_t ptid, int until_no_new) > { > ... > if (until_no_new) > { > /* Require 4 successive iterations which do not find any new threa= ds. > The 4 is a heuristic: there is an inherent race here, and I have > seen that 2 iterations in a row are not always sufficient to > "capture" all threads. */ > ... >=20 > That heuristic may well fail, and when it does, we end up with threads > in the program that aren't under GDB's control. That's obviously bad > and results in quite mistifying failures, like e.g., the process dying > for seeminly no reason when a thread that wasn't attached trips on a > breakpoint. >=20 > There's really no reason to rely on libthread_db for this nowadays > when we have /proc mounted. In that case, which is the usual case, we > can list the LWPs from /proc/PID/task/. In fact, GDBserver is already > doing this. The patch factors out that code that knows to walk the > task/ directory out of GDBserver, and makes GDB use it too. >=20 > Like GDBserver, the patch makes GDB attach to LWPs and _not_ wait for > them to stop immediately. Instead, we just tag the LWP as having an > expected stop. Because we can only set the ptrace options when the > thread stops, we need a new flag in the lwp structure to keep track of > whether we've already set the ptrace options, just like in GDBserver. > Note that nothing issues any ptrace command to the threads between the > PTRACE_ATTACH and the stop, so this is safe (unlike one scenario > described in gdbserver's linux-low.c). >=20 > When we attach to a program that has threads exiting while we attach, > it's easy to race with a thread just exiting as we try to attach to > it, like: >=20 > #1 - get current list of threads > #2 - attach to each listed thread > #3 - ooops, attach failed, thread is already gone >=20 > As this is pretty normal, we shouldn't be issuing a scary warning in > step #3. >=20 > When #3 happens, PTRACE_ATTACH usually fails with ESRCH, but sometimes > we'll see EPERM as well. That happens when the kernel still has the > kernel in its task list, but the thread is marked as dead. "still has the kernel" -> "still has the thread" > Unfortunately, EPERM is ambiguous and we'll get it also on other > scenarios where the thread isn't dead, and in those cases, it's useful > to get a warning. To distiguish the cases, when we get an EPERM > failure, we open /proc/PID/status, and check the thread's state -- if > the /proc file no longer exists, or the state is "Z (Zombie)" or "X > (Dead)", we ignore the EPERM error silently; otherwise, we'll warn. > Unfortunately, there seems to be a kernel race here. Sometimes I get > EPERM, and then the /proc state still indicates "R (Running)"... If > we wait a bit and retry, we do end up seeing X or Z state, or get an > ESRCH. I thought of making GDB retry the attach a few times, but even > with a 500ms wait and 4 retries, I still see the warning sometimes. I > haven't been able to identify the kernel path that causes this yet, > but in any case, it looks like a kernel bug to me. As this just > results failure to suppress a warning that we've been printing since > about forever anyway, I'm just making the test cope with it, and issue > an XFAIL. >=20 > gdb/gdbserver/ > 2014-12-16 Pedro Alves >=20 > * linux-low.c (linux_attach_fail_reason_string): Move to > nat/linux-ptrace.c, and rename. > (linux_attach_lwp): Update comment. > (attach_proc_task_lwp_callback): New function. > (linux_attach): Adjus to rename and use Adjus -> Adjust > linux_proc_attach_tgid_threads. > (linux_attach_fail_reason_string): Delete declaration. >=20 > gdb/ > 2014-12-16 Pedro Alves >=20 > * linux-nat.c (attach_proc_task_lwp_callback): New function. > (linux_nat_attach): Use linux_proc_attach_tgid_threads. > (wait_lwp, linux_nat_filter_event): If not set yet, set the lwp's > ptrace option flags. > * linux-nat.h (struct lwp_info) : New > field. > * nat/linux-procfs.c: Include . > (linux_proc_get_int): New parameter "warn". Handle it. > (linux_proc_get_tgid): Adjust. > (linux_proc_get_tracerpid): Rename to ... > (linux_proc_get_tracerpid_nowarn): ... this. > (linux_proc_pid_get_state): New function, factored out from > (linux_proc_pid_has_state): ... this. Add new parameter "warn" > and handle it. > (linux_proc_pid_is_gone): New function. > (linux_proc_pid_is_stopped): Adjust. > (linux_proc_pid_is_zombie_maybe_warn) > (linux_proc_pid_is_zombie_nowarn): New functions. > (linux_proc_pid_is_zombie): Use > linux_proc_pid_is_zombie_maybe_warn. > (linux_proc_attach_tgid_threads): New function. > * nat/linux-procfs.h (linux_proc_get_tgid): Update comment. > (linux_proc_get_tracerpid): Rename to ... > (linux_proc_get_tracerpid_nowarn): ... this, and update comment. > (linux_proc_pid_is_gone): New declaration. > (linux_proc_pid_is_zombie): Update comment. > (linux_proc_pid_is_zombie_nowarn): New declaration. > (linux_proc_attach_lwp_func): New typedef. > (linux_proc_attach_tgid_threads): New declaration. > * nat/linux-ptrace.c (linux_ptrace_attach_fail_reason): Adjust to > use nowarn functions. > (linux_ptrace_attach_fail_reason_string): Move here from > gdbserver/linux-low.c and rename. > (ptrace_supports_feature): If the current ptrace options are not > known yet, check them now, instead of asserting. > * nat/linux-ptrace.h (linux_ptrace_attach_fail_reason_string): > Declare. I think it makes sense, not that I know anything about it. Simon --------------ms040406090005000901050605 Content-Type: application/pkcs7-signature; name="smime.p7s" Content-Transfer-Encoding: base64 Content-Disposition: attachment; filename="smime.p7s" Content-Description: S/MIME Cryptographic Signature Content-length: 5531 MIAGCSqGSIb3DQEHAqCAMIACAQExCzAJBgUrDgMCGgUAMIAGCSqGSIb3DQEH AQAAoIIMqjCCBewwggPUoAMCAQICEQCcTF8c4EKa6GVNC9lZ0VtfMA0GCSqG SIb3DQEBBQUAMDoxETAPBgNVBAoMCEVyaWNzc29uMSUwIwYDVQQDDBxFcmlj c3NvbiBOTCBJbmRpdmlkdWFsIENBIHYyMB4XDTE0MTIxNjE5NTk0NloXDTE3 MTIxNjE5NTk0NVowZjERMA8GA1UECgwIRXJpY3Nzb24xFTATBgNVBAMMDFNp bW9uIE1hcmNoaTEoMCYGCSqGSIb3DQEJARYZc2ltb24ubWFyY2hpQGVyaWNz c29uLmNvbTEQMA4GA1UEBRMHZW1haXNpbjCCASIwDQYJKoZIhvcNAQEBBQAD ggEPADCCAQoCggEBAIUJ3Y1Kka6vwpVQ/RJWY5WcRchKpXoDaydFzm2o7BHE brgv8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