From: Josh Stone <jistone@redhat.com>
To: Pedro Alves <palves@redhat.com>, gdb-patches@sourceware.org
Cc: philippe.waroquiers@skynet.be, sergiodj@redhat.com, eliz@gnu.org,
xdje42@gmail.com
Subject: Re: [PATCH v2 1/2] gdbserver: Set Linux ptrace options ASAP
Date: Tue, 01 Dec 2015 20:17:00 -0000 [thread overview]
Message-ID: <565E0060.3060104@redhat.com> (raw)
In-Reply-To: <565C9A73.9040707@redhat.com>
On 11/30/2015 10:50 AM, Josh Stone wrote:
> On 11/26/2015 02:34 AM, Pedro Alves wrote:
>> On 11/26/2015 02:53 AM, Josh Stone wrote:
>>> The ptrace options should be set as soon as we know a thread is stopped,
>>> so no events can be missed. There's an arch-setup early return that was
>>> effectively delaying this update before, and I found for instance that
>>> the first syscall event wouldn't be properly reported with TRACESYSGOOD.
>>> It's now more similar to the way that gdb/linux-nat.c handles it.
>>
>> Hmm, I'm confused on how this resulted in the first syscall being misssed.
>> That early return happens when we're not executing the real inferior
>> yet -- the process is still running the "gdbserver --wrapper WRAPPER"
>> binary.
>
> My memory of this is admittedly hazy by now. IIRC the first syscall
> wasn't *completely* missed, just reported without TRACESYSGOOD in
> effect, so it looked like a plain SIGTRAP.
>
> I will try to dig in and characterize the problem I had better,
> especially with your explanation of exec startup at hand. Thanks!
OK, I think I've got it, and it's a real regression from 7.10 even for
other events like fork. I'm not using --wrapper, so I'm not sure of the
interaction there, but even gdbserver's simple fork+exec can show the
problem. Basically, on the very first stop we don't set flags yet, so
the first resume from there continues without the right flags.
The sequence I was running into with syscalls goes like this:
- start_inferior calls create_inferior to fork, then calls mywait
- the forked process calls ptrace(PTRACE_TRACEME), then execs
- linux_low_filter_event sees a raw SIGTRAP for the child after exec
- (we haven't set PTRACE_O_TRACEEXEC yet, so SIGTRAP is expected)
- arch setup is needed, so it hits the early return (new since 7.10)
... thus child->must_set_ptrace_flags is not dealt with
- start_inferior calls target_arch_setup
- GDB sends QCatchSyscalls:1
- linux_resume_one_lwp_throw calls ptrace(PTRACE_SYSCALL)
- but we still haven't set any flags, especially PTRACE_O_TRACESYSGOOD
- linux_low_filter_event sees a raw SIGTRAP for the first syscall entry
- now we finally deal with child->must_set_ptrace_flags
- linux_resume_one_lwp_throw calls ptrace(PTRACE_SYSCALL)
- linux_low_filter_event sees SYSCALL_SIGTRAP for the return
- entry/return logic is confused now, thinks this is an entry
- (but if there's any other event, entry/return will get back in sync)
But this problem isn't particular to my syscall patches. Consider this
simple forking program and use 'catch fork':
#include <unistd.h>
int main() { fork(); return 0; }
Compiled normally, with dynamically-linked libc et al, you get:
- SIGTRAP after exec, ignores child->must_set_ptrace_flags.
- SIGTRAP for a swbreak, I guess some gdb setup, then it sets the
necessary flags, especially PTRACE_O_TRACEFORK.
- SIGTRAP for PTRACE_EVENT_FORK, hooray!
But compiled statically:
- SIGTRAP after exec, ignores child->must_set_ptrace_flags.
- CLD_EXITED, flags were never set!
- if I add a breakpoint on main, flags will be set when that's reached,
and then we do get the PTRACE_EVENT_FORK after all.
So, we need some point to get the right flags set before the program
starts running for real. If you don't like the way I moved the flags
before that arch-setup early return, then when should we do it?
- Perhaps before the ptrace call in linux_resume_one_lwp_throw? Then if
any state changes while the thread is still stopped, triggering new
must_set_ptrace_flags, we'll deal with it before resuming. But I don't
know if this would interact well with your wrapper concerns.
- Perhaps at the end of linux_arch_setup? AIUI this will be after
everything you're worried about wrappers.
>> It's pedantically good, though not crucial, to set PTRACE_O_TRACEEXEC early for
>> that scenario, to get a real PTRACE_EVENT_EXEC event instead of a bare SIGTRAP
>> when the exec wrapper (or in the future, the shell, when we start inferiors
>> with the shell, like gdb does, for arg expansion and globbing) actually execs.
>>
>> If the shell/wrapper forks, enabling fork events while still executing the
>> wrapper/shell breaks startup -- server.c:start_inferior. The gdb
>> version (fork-child.c:startup_inferior) does handle TARGET_WAITKIND_FORKED,
>> but AFAICS forgets detaching/resuming the child...
>>
>> We _must_ not catch syscall events while running the exec wrapper (or
>> the shell), otherwise server.c:start_inferior would get confused for seeing
>> unexpected syscall stops. If the backend treats syscall catchpoints, it's OK,
>> since gdb won't insert catchpoints in the process until after vRun returns,
>> indicating the process is stopped at the entry point. IIRC, gdb actually
>> does NOT handle catchpoint locations per-inferior today, but as long as
>> the backend side thinks of catchpoints per-inferior, we can fix the GDB side.
>>
>> So all in all, I'm not sure this actually buys us anything other than need
>> to fix the wrapper/shell-forks case.
>>
>>>
>>> gdb/gdbserver/ChangeLog:
>>>
>>> 2015-11-25 Josh Stone <jistone@redhat.com>
>>>
>>> * linux-low.c (linux_low_filter_event): Set ptrace options as soon as
>>> each thread is stopped, even before arch-specific setup.
>>
>> Thanks,
>> Pedro Alves
>>
>
next prev parent reply other threads:[~2015-12-01 20:17 UTC|newest]
Thread overview: 49+ messages / expand[flat|nested] mbox.gz Atom feed top
2015-10-30 11:02 [PATCH] Implement 'catch syscall' for gdbserver Josh Stone
2015-10-30 13:26 ` Eli Zaretskii
2015-11-01 22:15 ` Doug Evans
2015-11-02 18:24 ` Josh Stone
2015-11-21 10:29 ` Philippe Waroquiers
2015-11-23 4:20 ` Doug Evans
2015-11-23 4:20 ` Doug Evans
2015-11-25 2:37 ` Josh Stone
2015-11-26 2:53 ` [PATCH v2 1/2] gdbserver: Set Linux ptrace options ASAP Josh Stone
2015-11-26 2:54 ` [PATCH v2 2/2] Implement 'catch syscall' for gdbserver Josh Stone
2015-11-26 10:34 ` [PATCH v2 1/2] gdbserver: Set Linux ptrace options ASAP Pedro Alves
2015-11-30 18:50 ` Josh Stone
2015-12-01 20:17 ` Josh Stone [this message]
2015-12-02 14:01 ` Pedro Alves
2015-12-04 2:26 ` [PATCH v3 1/2] gdbserver: set ptrace flags after creating inferiors Josh Stone
2015-12-04 2:27 ` [PATCH v3 2/2] Implement 'catch syscall' for gdbserver Josh Stone
2015-12-04 8:45 ` Eli Zaretskii
2015-12-05 2:14 ` Josh Stone
2015-12-05 8:02 ` Eli Zaretskii
2015-12-07 16:50 ` Josh Stone
2015-12-07 17:15 ` Eli Zaretskii
2015-12-04 13:18 ` Pedro Alves
2015-12-05 2:16 ` Josh Stone
2015-12-08 13:31 ` Pedro Alves
2015-12-08 19:02 ` Josh Stone
2015-12-08 13:37 ` Pedro Alves
2015-12-11 21:19 ` Josh Stone
2015-12-16 15:42 ` Pedro Alves
2016-01-09 3:09 ` [PATCH v4] " Josh Stone
2016-01-09 7:37 ` Eli Zaretskii
2016-01-11 17:44 ` Philippe Waroquiers
2016-01-12 12:05 ` Pedro Alves
2016-01-12 19:10 ` Josh Stone
2016-01-12 19:22 ` Pedro Alves
2016-01-12 20:01 ` Josh Stone
2016-03-29 14:27 ` Yao Qi
2016-03-29 18:12 ` Josh Stone
2016-03-29 23:49 ` Josh Stone
2016-03-30 12:23 ` Yao Qi
2016-03-31 1:10 ` Josh Stone
2016-04-01 13:05 ` Yao Qi
2016-04-01 16:38 ` Josh Stone
2016-05-29 16:47 ` [doc] NEWS: QCatchSyscalls: simplify Jan Kratochvil
2016-05-29 17:29 ` Eli Zaretskii
2016-05-29 17:50 ` Jan Kratochvil
2016-05-29 18:19 ` Eli Zaretskii
2016-05-29 18:47 ` [commit] " Jan Kratochvil
2015-12-04 12:16 ` [PATCH v3 1/2] gdbserver: set ptrace flags after creating inferiors Pedro Alves
2015-12-05 2:14 ` Josh Stone
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=565E0060.3060104@redhat.com \
--to=jistone@redhat.com \
--cc=eliz@gnu.org \
--cc=gdb-patches@sourceware.org \
--cc=palves@redhat.com \
--cc=philippe.waroquiers@skynet.be \
--cc=sergiodj@redhat.com \
--cc=xdje42@gmail.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).