From: Pedro Alves <palves@redhat.com>
To: Cleber Rosa <crosa@redhat.com>, gdb-patches@sourceware.org
Cc: areis@redhat.com, Sergio Durigan Junior <sergiodj@redhat.com>
Subject: Re: [PATCH 0/4] GDBServer: introduce a dedicated stderr stream
Date: Wed, 01 Apr 2015 11:17:00 -0000 [thread overview]
Message-ID: <551BD3AF.2010609@redhat.com> (raw)
In-Reply-To: <551199CF.2000400@redhat.com>
On 03/24/2015 05:07 PM, Cleber Rosa wrote:
> On 03/21/2015 12:05 PM, Pedro Alves wrote:
>> On 03/21/2015 02:34 AM, Cleber Rosa wrote:
>>> This patch series add command line options and monitor commands that
>>> will redirect all of the gdbserver's own output (always sent to stderr)
>>> to a separate file. This feature makes it possible to distinguish between
>>> the inferior process stderr and gdbserver's own stderr.
>> A specific FILE* is a fragile approach; libraries that gdbserver loads
>> may well print to stdout/stderr or write to file descriptors 1 or
>> 2 directly, for example. If we're doing this, redirection is best done
>> at the lower OS file descriptor layer, not at C-runtime stdio (stdout/stderr)
>> layer, with e.g., dup/dup2.
>
> I do agree with the fragility of the method chosen. The truth is that
> all other approaches I considered turned out to be, IMHO, excessively
> complex and cumbersome for what was trying to be achieved.
>
>>
>> And, gdbserver itself may print to stdout/stderr _before_ the redirection
>> command-line option is processed. Thus it's safer/better to just start gdbserver
>> with its input/output redirected already. Of course, then because new
>> inferiors inherit the input/output from gdbserver, we'd need a way to
>> start the inferior with input/output redirected somewhere instead.
>
> You're absolutely right that loaded libraries can write to the file
> descriptor this patch is trying to "protect", and so can instrumented
> commands during a debug session and possibly many others, other than
> gdbserver itself. Even new code that slips into gdbserver itself may
> end up breaking this "contract".
>
Why not do it with dup/dup2 instead then? Something around this:
int inferior_fds[3] = { -1, -1, -1 };
set_server_output ()
{
for (i = 0; i < 3; i++)
inferior_fds[i] = dup (i);
for (i = 0; i < 3; i++)
dup2 (i, redirect_fd);
}
and then when the inferior is created, in linux_create_inferior,
around where we close most of the fork child's fds, make the child
re-redirect its output to the original file descriptors:
for (i = 0; i < 3; i++)
if (inferior_fds[i] != -1)
dup2 (i, inferior_fds[i]);
Note this linux_create_inferior change wouldn't be that
different from an option that would make gdbserver redirect
the inferior's stdin/stdout/stderr instead of its own without
shell involvement, like an hypothetical
"set inferior-stdin|stdout|stderr" feature to complement
"set inferior-tty".
> I actually tried that approach many months ago[1]. I was actually
> expecting the feature parity between gdb and gdbserver, and it kind of
> let me down. But, even with an exclusive TTY for the inferior, one big
> gap would remain: no clean way to differentiate between an application
> STDOUT and STDERR simply by reading from its TTY.
>
> My dream feature set would be gdb supporting redirection *including*
> STDERR (doesn't seem to be the case right now[2]),
If you're referring to the "run > outfile" example, GDB just creates
the inferior using the shell, like "sh -c program > outfile" behind
the scenes. So "2 > stderr.txt" should work fine too.
Making gdbserver start the inferior with a shell so these things
work with gdbserver too (with target extended-remote and "run") is
exactly the gdb/gdbserver feature parity point that Sergio is
working on.
Thanks,
Pedro Alves
prev parent reply other threads:[~2015-04-01 11:17 UTC|newest]
Thread overview: 19+ messages / expand[flat|nested] mbox.gz Atom feed top
2015-03-21 2:35 Cleber Rosa
2015-03-21 2:35 ` [PATCH 2/4] GDBServer: give more complete usage information Cleber Rosa
2015-03-21 17:05 ` Pedro Alves
2015-03-24 14:15 ` Cleber Rosa
2015-03-31 14:44 ` Cleber Rosa
2015-04-01 10:10 ` Pedro Alves
2015-03-21 2:35 ` [PATCH 3/4] GDBServer: introduce --server-stderr command line option Cleber Rosa
2015-03-21 8:26 ` Eli Zaretskii
2015-03-23 18:51 ` Cleber Rosa
2015-03-23 19:12 ` Eli Zaretskii
2015-03-23 20:35 ` Cleber Rosa
2015-03-23 20:43 ` Eli Zaretskii
2015-03-21 2:35 ` [PATCH 1/4] GDBServer: introduce a stderr stream dedicated to the server Cleber Rosa
2015-03-21 2:35 ` [PATCH 4/4] GDBServer: add 'monitor set server-stderr' command Cleber Rosa
2015-03-21 8:29 ` Eli Zaretskii
2015-03-23 20:09 ` Cleber Rosa
2015-03-21 15:05 ` [PATCH 0/4] GDBServer: introduce a dedicated stderr stream Pedro Alves
2015-03-24 17:07 ` Cleber Rosa
2015-04-01 11:17 ` Pedro Alves [this message]
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=551BD3AF.2010609@redhat.com \
--to=palves@redhat.com \
--cc=areis@redhat.com \
--cc=crosa@redhat.com \
--cc=gdb-patches@sourceware.org \
--cc=sergiodj@redhat.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).