From: Pedro Alves <palves@redhat.com>
To: Simon Marchi <simon.marchi@ericsson.com>, gdb-patches@sourceware.org
Subject: Re: [PATCH 2/5] Linux: on attach, attach to lwps listed under /proc/$pid/task/
Date: Wed, 17 Dec 2014 13:35:00 -0000 [thread overview]
Message-ID: <549186A9.9080800@redhat.com> (raw)
In-Reply-To: <54909B99.5070806@ericsson.com>
On 12/16/2014 08:52 PM, Simon Marchi wrote:
> On 2014-12-16 11:53 AM, Pedro Alves wrote:
>> ... instead of relying on libthread_db.
>>
>> I wrote a test that attaches to a program that constantly spawns
>> short-lived threads, which exposed several issues. This is one of
>> them.
>>
>> 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:
>>
>> 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 threads.
>> 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. */
>> ...
>>
>> 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.
>>
>> 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.
>>
>> 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).
>>
>> 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:
>>
>> #1 - get current list of threads
>> #2 - attach to each listed thread
>> #3 - ooops, attach failed, thread is already gone
>>
>> As this is pretty normal, we shouldn't be issuing a scary warning in
>> step #3.
>>
>> 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"
Indeed. Fixed locally.
>> (linux_attach): Adjus to rename and use
>
> Adjus -> Adjust
>
Fixed.
>
> I think it makes sense, not that I know anything about it.
Thanks for the review.
Thanks,
Pedro Alves
next prev parent reply other threads:[~2014-12-17 13:35 UTC|newest]
Thread overview: 24+ messages / expand[flat|nested] mbox.gz Atom feed top
2014-12-16 16:54 [PATCH 0/5] GNU/Linux, fix attach races/problems Pedro Alves
2014-12-16 16:54 ` [PATCH 1/5] libthread_db: debug output should go to gdb_stdlog Pedro Alves
2014-12-17 8:02 ` Yao Qi
2014-12-17 13:45 ` Pedro Alves
2014-12-17 14:09 ` Yao Qi
2014-12-16 16:54 ` [PATCH 3/5] libthread_db: Skip attaching to terminated and joined threads Pedro Alves
2014-12-16 16:54 ` [PATCH 4/5] Linux: Skip thread_db thread event reporting if PTRACE_EVENT_CLONE is supported Pedro Alves
2014-12-16 21:24 ` Simon Marchi
2014-12-17 13:04 ` Pedro Alves
2014-12-16 16:54 ` [PATCH 2/5] Linux: on attach, attach to lwps listed under /proc/$pid/task/ Pedro Alves
2014-12-16 20:52 ` Simon Marchi
2014-12-17 13:35 ` Pedro Alves [this message]
2014-12-16 17:35 ` [PATCH 5/5] Test attaching to a program that constantly spawns short-lived threads Pedro Alves
2014-12-17 11:10 ` Yao Qi
2014-12-18 0:02 ` Pedro Alves
2015-01-05 19:02 ` Breazeal, Don
2015-01-07 16:17 ` [PATCH] skip "attach" tests when testing against stub-like targets (was: Re: [PATCH 5/5] Test attaching to a program that constantly spawns short-lived threads) Pedro Alves
2015-01-09 11:24 ` [PATCH] skip "attach" tests when testing against stub-like targets Pedro Alves
2015-01-12 4:43 ` [regression/native-gdbserver][buildbot] Python testscases get staled (was: Re: [PATCH] skip "attach" tests when testing against stub-like targets) Sergio Durigan Junior
2015-01-12 11:15 ` [regression/native-gdbserver][buildbot] Python testscases get staled Pedro Alves
2015-01-12 16:55 ` Sergio Durigan Junior
2015-01-12 17:01 ` Pedro Alves
2015-01-12 17:13 ` [PATCH] gdb.python/py-prompt.exp: restore GDBFLAGS Pedro Alves
2015-01-09 12:03 ` [PATCH 0/5] GNU/Linux, fix attach races/problems Pedro Alves
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=549186A9.9080800@redhat.com \
--to=palves@redhat.com \
--cc=gdb-patches@sourceware.org \
--cc=simon.marchi@ericsson.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).