public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: Doug Evans <dje@google.com>
To: Pedro Alves <palves@redhat.com>
Cc: gdb-patches <gdb-patches@sourceware.org>
Subject: Re: [PATCH] [7.8 regression] PR cli/17828: -batch -ex r breaks terminal
Date: Mon, 12 Jan 2015 18:39:00 -0000	[thread overview]
Message-ID: <CADPb22RqHOhRzqR260QbK+TCx45GKKgb7xs_6+v7nEJakvbC9w@mail.gmail.com> (raw)
In-Reply-To: <1421080814-10269-1-git-send-email-palves@redhat.com>

On Mon, Jan 12, 2015 at 8:40 AM, Pedro Alves <palves@redhat.com> wrote:
> Commit d3d4baed (PR python/17372 - Python hangs when displaying
> help()) had the side effect of causing 'gdb -batch' to leave the
> terminal in the wrong state if the program was run.  E.g,.
>
>  $ echo 'main(){*(int*)0=0;}' | gcc -x c -; ./gdb/gdb -batch -ex r ./a.out
>  Program received signal SIGSEGV, Segmentation fault.
>  0x00000000004004ff in main ()
>  $
>
> If you start typing the next command, seemingly nothing happens - GDB
> left the terminal with echo disabled.
>
> The issue is that that "r" ends up in fetch_inferior_event, which
> calls reinstall_readline_callback_handler_cleanup, which causes
> readline to prep the terminal (raw, echo disabled).  But "-batch"
> causes GDB to exit before the top level event loop is first started,
> and then nothing de-preps the terminal.
>
> The reinstall_readline_callback_handler_cleanup function's intro
> comment mentions:
>
>  "Need to do this as we go back to the event loop, ready to process
>  further input."
>
> but the implementation forgets the case of when the interpreter is
> sync, which indicates we won't return to the event loop yet, or as in
> the case of -batch, we have not started it yet.
>
> The fix is to not install the readline callback in that case.
>
> For the test, in this case, checking that command echo still works is
> sufficient.  Comparing stty output before/after running GDB is even
> better.  Because stty may not be available, the test tries both ways.
> In any case, since expect's spawn (what we use to start gdb) creates a
> new pseudo tty, another expect spawn or tcl exec after GDB exits would
> not see the wrong terminal settings.  So instead, the test spawns a
> shell and runs stty and GDB in it.
>
> Tested on x86_64 Fedora 20.
>
> gdb/
> 2015-01-12  Pedro Alves  <palves@redhat.com>
>
>         * infrun.c (reinstall_readline_callback_handler_cleanup): Don't
>         reinstall is the interpreter is sync.
>
> gdb/testsuite/
> 2015-01-12  Pedro Alves  <palves@redhat.com>
>
>         * testsuite/gdb.base/batch-preserve-term-settings.c: New file.
>         * testsuite/gdb.base/batch-preserve-term-settings.exp: New file.
> ---
>  gdb/infrun.c                                       |   2 +-
>  .../gdb.base/batch-preserve-term-settings.c        |  22 +++
>  .../gdb.base/batch-preserve-term-settings.exp      | 186 +++++++++++++++++++++
>  3 files changed, 209 insertions(+), 1 deletion(-)
>  create mode 100644 gdb/testsuite/gdb.base/batch-preserve-term-settings.c
>  create mode 100644 gdb/testsuite/gdb.base/batch-preserve-term-settings.exp
>
> diff --git a/gdb/infrun.c b/gdb/infrun.c
> index 105862a..4c9979f 100644
> --- a/gdb/infrun.c
> +++ b/gdb/infrun.c
> @@ -3178,7 +3178,7 @@ wait_for_inferior (void)
>  static void
>  reinstall_readline_callback_handler_cleanup (void *arg)
>  {
> -  if (async_command_editing_p && !sync_execution)
> +  if (interpreter_async && async_command_editing_p && !sync_execution)
>      gdb_rl_callback_handler_reinstall ();
>  }
>

IWBN if the subtleties going on here were explained in the code.

Maybe something like

  /* Don't call gdb_rl_callback_handler_reinstall if !interpreter_async:
     It will prep the terminal, readline-style (raw, noecho), which is not
     what we want if we're not using readline (e.g., --batch).  */

I'm sure that can be improved on, but the point is this stuff is
obviously subtle and readers should at least be given a bit of
assistance (I'm all for more full explanations residing in the commit log).

Another way to go, and while this is a separate thread, this seems
like a good time to (re)start it, would be to try to encapsulate this
a bit more.
I'm not saying to do this now, obviously; we want to get a fix in
for the release.  But long term, it seems like there should be a layer
between infrun and "readline", and it's this layer's job to DTRT instead
of infrun checking no less than three global state variables to decide to
install the "real" readline callback handler.  I say "real" because
gdb has another readline: gdb_readline2, another reason for more
encapsulation.

  reply	other threads:[~2015-01-12 18:39 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-01-12 16:40 Pedro Alves
2015-01-12 18:39 ` Doug Evans [this message]
2015-01-14 13:23   ` Pedro Alves
2015-01-13  9:48 ` Joel Brobecker

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=CADPb22RqHOhRzqR260QbK+TCx45GKKgb7xs_6+v7nEJakvbC9w@mail.gmail.com \
    --to=dje@google.com \
    --cc=gdb-patches@sourceware.org \
    --cc=palves@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).