Bug 17472 - with annotations, input while executing in the foreground crashes readline/gdb
Summary: with annotations, input while executing in the foreground crashes readline/gdb
Status: RESOLVED FIXED
Alias: None
Product: gdb
Classification: Unclassified
Component: gdb (show other bugs)
Version: HEAD
: P2 normal
Target Milestone: 7.8.1
Assignee: Pedro Alves
URL:
Keywords:
: 17456 (view as bug list)
Depends on:
Blocks:
 
Reported: 2014-10-09 15:43 UTC by Pedro Alves
Modified: 2016-08-09 20:55 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Pedro Alves 2014-10-09 15:43:48 UTC
(gdb) set annotate 2

▒▒pre-prompt
(gdb)
▒▒prompt
c

▒▒post-prompt
Continuing.

▒▒starting

▒▒frames-invalid

p 1<ret>
readline: readline_callback_read_char() called with no handler!
Aborted (core dumped)
$
Comment 1 Pedro Alves 2014-10-09 15:44:09 UTC
Mine.
Comment 2 Jan Kratochvil 2014-10-10 08:00:11 UTC
*** Bug 17456 has been marked as a duplicate of this bug. ***
Comment 3 Sourceware Commits 2014-10-17 12:47:46 UTC
This is an automated email from the git hooks/post-receive script. It was
generated because a ref change was pushed to the repository containing
the project "gdb and binutils".

The branch, master has been updated
       via  6c4486e63f7583ed85a0c72841f6ccceebbf858e (commit)
       via  0ff33695eeedf3c2e8cdec8690ae4a10a66b3389 (commit)
       via  6fdebc3d1c54d2202f7637e7345fe6d25c7ed93b (commit)
       via  5842f62aad5c20dbb3511208efbc40a8d638b6d3 (commit)
      from  3f7308212cefe5cd958b29e0b9e92d92c4dbe941 (commit)

Those revisions listed above that are new to this repository have
not appeared on any other notification email; so we list those
revisions in full, below.

- Log -----------------------------------------------------------------
https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=6c4486e63f7583ed85a0c72841f6ccceebbf858e

commit 6c4486e63f7583ed85a0c72841f6ccceebbf858e
Author: Pedro Alves <palves@redhat.com>
Date:   Fri Oct 17 13:31:26 2014 +0100

    PR gdb/17471: Repeating a background command makes it foreground
    
    When we repeat a command, by just pressing <ret>, the input from the
    previous command is reused for the new command invocation.
    
    When an execution command strips the "&" out of its incoming argument
    string, to detect background execution, we poke a '\0' directly to the
    incoming argument string.
    
    Combine both, and a repeat of a background command loses the "&".
    
    This is actually only visible if args other than "&" are specified
    (e.g., "c 1&" or "next 2&" or "c -a&"), as in the special case of "&"
    alone (e.g. "c&") doesn't actually clobber the incoming string.
    
    Fix this by making strip_bg_char return a new string instead of poking
    a hole in the input string.
    
    New test included.
    
    Tested on x86_64 Fedora 20, native and gdbserver.
    
    gdb/
    2014-10-17  Pedro Alves  <palves@redhat.com>
    
    	PR gdb/17471
    	* infcmd.c (strip_bg_char): Change prototype and rewrite.  Now
    	returns a copy of the input.
    	(run_command_1, continue_command, step_1, jump_command)
    	(signal_command, until_command, advance_command, finish_command)
    	(attach_command): Adjust and install a cleanup to free the
    	stripped args.
    
    gdb/testsuite/
    2014-10-17  Pedro Alves  <palves@redhat.com>
    
    	PR gdb/17471
    	* gdb.base/bg-execution-repeat.c: New file.
    	* gdb.base/bg-execution-repeat.exp: New file.

https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=0ff33695eeedf3c2e8cdec8690ae4a10a66b3389

commit 0ff33695eeedf3c2e8cdec8690ae4a10a66b3389
Author: Pedro Alves <palves@redhat.com>
Date:   Fri Oct 17 13:31:25 2014 +0100

    PR gdb/17300: Input after "c -a" crashes readline/GDB
    
    If all threads in the target were already running when the user does
    "c -a", nothing puts the inferior's terminal settings in effect and
    removes stdin from the event loop, which we must when running a
    foreground command.  The result is that user input afterwards crashes
    readline/gdb:
    
     (gdb) start
     Temporary breakpoint 1 at 0x4005d4: file continue-all-already-running.c, line 23.
     Starting program: continue-all-already-running
    
     Temporary breakpoint 1, main () at continue-all-already-running.c:23
     23        sleep (10);
     (gdb) c -a&
     Continuing.
     (gdb) c -a
     Continuing.
     p 1
     readline: readline_callback_read_char() called with no handler!
     Aborted (core dumped)
     $
    
    Backtrace:
    
     Program received signal SIGABRT, Aborted.
     0x0000003b36a35877 in __GI_raise (sig=sig@entry=6) at ../nptl/sysdeps/unix/sysv/linux/raise.c:56
     56        return INLINE_SYSCALL (tgkill, 3, pid, selftid, sig);
     (top-gdb) p 1
     $1 = 1
     (top-gdb) bt
     #0  0x0000003b36a35877 in __GI_raise (sig=sig@entry=6) at ../nptl/sysdeps/unix/sysv/linux/raise.c:56
     #1  0x0000003b36a36f68 in __GI_abort () at abort.c:89
     #2  0x0000000000784aa9 in rl_callback_read_char () at readline/callback.c:116
     #3  0x0000000000619181 in rl_callback_read_char_wrapper (client_data=0x0) at gdb/event-top.c:167
     #4  0x0000000000619557 in stdin_event_handler (error=0, client_data=0x0) at gdb/event-top.c:373
     #5  0x000000000061814a in handle_file_event (data=...) at gdb/event-loop.c:763
     #6  0x0000000000617631 in process_event () at gdb/event-loop.c:340
     #7  0x00000000006176f8 in gdb_do_one_event () at gdb/event-loop.c:404
     #8  0x0000000000617748 in start_event_loop () at gdb/event-loop.c:429
     #9  0x00000000006191b3 in cli_command_loop (data=0x0) at gdb/event-top.c:182
     #10 0x000000000060f538 in current_interp_command_loop () at gdb/interps.c:318
     #11 0x0000000000610701 in captured_command_loop (data=0x0) at gdb/main.c:323
     #12 0x000000000060c3f5 in catch_errors (func=0x6106e6 <captured_command_loop>, func_args=0x0, errstring=0x9002c1 "", mask=RETURN_MASK_ALL)
         at gdb/exceptions.c:237
     #13 0x0000000000611bff in captured_main (data=0x7fffffffd780) at gdb/main.c:1151
     #14 0x000000000060c3f5 in catch_errors (func=0x610afe <captured_main>, func_args=0x7fffffffd780, errstring=0x9002c1 "", mask=RETURN_MASK_ALL)
         at gdb/exceptions.c:237
     #15 0x0000000000611c28 in gdb_main (args=0x7fffffffd780) at gdb/main.c:1159
     #16 0x000000000045ef97 in main (argc=5, argv=0x7fffffffd888) at gdb/gdb.c:32
     (top-gdb)
    
    Tested on x86_64 Fedora 20, native and gdbserver.
    
    gdb/
    2014-10-17  Pedro Alves  <palves@redhat.com>
    
    	PR gdb/17300
    	* infcmd.c (continue_1): If continuing all threads in the
    	foreground, make sure the inferior's terminal settings are put in
    	effect.
    
    gdb/testsuite/
    2014-10-17  Pedro Alves  <palves@redhat.com>
    
    	PR gdb/17300
    	* gdb.base/continue-all-already-running.c: New file.
    	* gdb.base/continue-all-already-running.exp: New file.

https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=6fdebc3d1c54d2202f7637e7345fe6d25c7ed93b

commit 6fdebc3d1c54d2202f7637e7345fe6d25c7ed93b
Author: Pedro Alves <palves@redhat.com>
Date:   Fri Oct 17 13:31:25 2014 +0100

    PR gdb/17472: With annotations, input while executing in the foreground crashes readline/GDB
    
    Jan caught an intermittent GDB crash with the annota1.exp test:
    
     Starting program: .../gdb/testsuite/gdb.base/annota1 ^M
     [...]
     FAIL: gdb.base/annota1.exp: run until main breakpoint (timeout)
     [...]
     readline: readline_callback_read_char() called with no handler!^M
     ERROR: Process no longer exists
    
    All we need to is to continue the inferior in the foreground, and type
    a command while the inferior is running.  E.g.:
    
     (gdb) set annotate 2
    
     ▒▒pre-prompt
     (gdb)
     ▒▒prompt
     c
    
     ▒▒post-prompt
     Continuing.
    
     ▒▒starting
    
     ▒▒frames-invalid
    
     *inferior is running now*
    
     p 1<ret>
    
     readline: readline_callback_read_char() called with no handler!
     Aborted (core dumped)
     $
    
    
    When we run a foreground execution command we call
    target_terminal_inferior to stop GDB from processing input, and to put
    the inferior's terminal settings in effect.  Then we tell readline to
    hide the prompt with display_gdb_prompt, which clears readline's input
    callback too.  When the target stops, we call target_terminal_ours,
    which re-installs stdin in the event loop, and then we redisplay the
    prompt, reinstalling the readline callbacks.
    
    However, when annotations are in effect, the "frames-invalid"
    annotation code calls target_terminal_ours after 'resume' had already
    called target_terminal_inferior:
    
     (top-gdb) bt
     #0  0x000000000056b82f in annotate_frames_invalid () at gdb/annotate.c:219
     #1  0x000000000072e6cc in reinit_frame_cache () at gdb/frame.c:1705
     #2  0x0000000000594bb9 in registers_changed_ptid (ptid=...) at gdb/regcache.c:612
     #3  0x000000000064cca1 in target_resume (ptid=..., step=1, signal=GDB_SIGNAL_0) at gdb/target.c:2136
     #4  0x00000000005f57af in resume (step=1, sig=GDB_SIGNAL_0) at gdb/infrun.c:2263
     #5  0x00000000005f6051 in proceed (addr=18446744073709551615, siggnal=GDB_SIGNAL_DEFAULT, step=1) at gdb/infrun.c:2613
    
    And then once we hide the prompt and remove readline's input handler
    callback, we're in a bad state.  We end up with the target running
    supposedly in the foreground, but with stdin still installed on the
    event loop.  Any input then calls into readline, which aborts because
    no rl_linefunc callback handler is installed:
    
     Program received signal SIGABRT, Aborted.
     0x0000003b36a35877 in __GI_raise (sig=sig@entry=6) at ../nptl/sysdeps/unix/sysv/linux/raise.c:56
     56        return INLINE_SYSCALL (tgkill, 3, pid, selftid, sig);
    
     (top-gdb) bt
     #0  0x0000003b36a35877 in __GI_raise (sig=sig@entry=6) at ../nptl/sysdeps/unix/sysv/linux/raise.c:56
     #1  0x0000003b36a36f68 in __GI_abort () at abort.c:89
     During symbol reading, debug info gives source 9 included from file at zero line 0.
     During symbol reading, debug info gives command-line macro definition with non-zero line 19: _STDC_PREDEF_H 1.
     #2  0x0000000000784a25 in rl_callback_read_char () at src/readline/callback.c:116
     #3  0x0000000000619111 in rl_callback_read_char_wrapper (client_data=0x0) at src/gdb/event-top.c:167
     #4  0x00000000006194e7 in stdin_event_handler (error=0, client_data=0x0) at src/gdb/event-top.c:373
     #5  0x00000000006180da in handle_file_event (data=...) at src/gdb/event-loop.c:763
     #6  0x00000000006175c1 in process_event () at src/gdb/event-loop.c:340
     #7  0x0000000000617688 in gdb_do_one_event () at src/gdb/event-loop.c:404
     #8  0x00000000006176d8 in start_event_loop () at src/gdb/event-loop.c:429
     #9  0x0000000000619143 in cli_command_loop (data=0x0) at src/gdb/event-top.c:182
     #10 0x000000000060f4c8 in current_interp_command_loop () at src/gdb/interps.c:318
     #11 0x0000000000610691 in captured_command_loop (data=0x0) at src/gdb/main.c:323
     #12 0x000000000060c385 in catch_errors (func=0x610676 <captured_command_loop>, func_args=0x0, errstring=0x900241 "", mask=RETURN_MASK_ALL)
         at src/gdb/exceptions.c:237
     #13 0x0000000000611b8f in captured_main (data=0x7fffffffd7b0) at src/gdb/main.c:1151
     #14 0x000000000060c385 in catch_errors (func=0x610a8e <captured_main>, func_args=0x7fffffffd7b0, errstring=0x900241 "", mask=RETURN_MASK_ALL)
         at src/gdb/exceptions.c:237
     #15 0x0000000000611bb8 in gdb_main (args=0x7fffffffd7b0) at src/gdb/main.c:1159
     #16 0x000000000045ef57 in main (argc=3, argv=0x7fffffffd8b8) at src/gdb/gdb.c:32
    
    The fix is to make the annotation code call target_terminal_inferior
    again after printing, if the inferior's settings were in effect.
    
    While at it, when we're doing output only, instead of
    target_terminal_ours, we should call target_terminal_ours_for_output.
    The latter doesn't actually remove stdin from the event loop, and also
    leaves SIGINT forwarded to the target.
    
    New test included.
    
    Tested on x86_64 Fedora 20, native and gdbserver.
    
    gdb/
    2014-10-17  Pedro Alves  <palves@redhat.com>
    
    	PR gdb/17472
    	* annotate.c (annotate_breakpoints_invalid): Use
    	target_terminal_our_for_output instead of target_terminal_ours.
    	Give back the terminal to the target.
    	(annotate_frames_invalid): Likewise.
    
    gdb/testsuite/
    2014-10-17  Pedro Alves  <palves@redhat.com>
    
    	PR gdb/17472
    	* gdb.base/annota-input-while-running.c: New file.
    	* gdb.base/annota-input-while-running.exp: New file.

https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=5842f62aad5c20dbb3511208efbc40a8d638b6d3

commit 5842f62aad5c20dbb3511208efbc40a8d638b6d3
Author: Pedro Alves <palves@redhat.com>
Date:   Fri Oct 17 13:31:25 2014 +0100

    Make common code handle target_terminal_* idempotency
    
    I found a place that should be giving back the terminal to the target,
    but only if the target was already owning it.  So I need to add a
    getter for who owns the terminal.
    
    The trouble is that several places/target have their own globals to
    track this state:
    
     - inflow.c:terminal_is_ours
     - remote.c:remote_async_terminal_ours_p
     - linux-nat.c:async_terminal_is_ours
     - go32-nat.c:terminal_is_ours
    
    While one might think of adding a new target_ops method to query this,
    conceptually, this state isn't really part of a particular target_ops.
    Considering multi-target, the core shouldn't have to ask all targets
    to know whether it's GDB that owns the terminal.  There's only one GDB
    (or rather, only one top level interpreter).
    
    So what this comment does is add a new global that is tracked by the
    core instead.  A subsequent pass may later remove the other globals.
    
    Tested on x86_64 Fedora 20, native and gdbserver.
    
    gdb/
    2014-10-17  Pedro Alves  <palves@redhat.com>
    
    	* target.c (enum terminal_state): New enum.
    	(terminal_state): New global.
    	(target_terminal_init): New function.
    	(target_terminal_inferior): Skip if inferior already owns the
    	terminal.
    	(target_terminal_ours, target_terminal_ours_for_output): New
    	functions.
    	* target.h (target_terminal_init): Convert to function prototype.
    	(target_terminal_ours_for_output): Convert to function prototype
    	and tweak comment.
    	(target_terminal_ours): Convert to function prototype and tweak
    	comment.
    	* windows-nat.c (do_initial_windows_stuff): Call
    	target_terminal_init instead of child_terminal_init_with_pgrp.

-----------------------------------------------------------------------

Summary of changes:
 gdb/ChangeLog                                      |   42 +++++
 gdb/annotate.c                                     |   22 +++-
 gdb/infcmd.c                                       |  160 +++++++++++++-------
 gdb/target.c                                       |   64 ++++++++
 gdb/target.h                                       |   25 ++--
 gdb/testsuite/ChangeLog                            |   18 +++
 .../gdb.base/annota-input-while-running.c          |   25 +++
 .../gdb.base/annota-input-while-running.exp        |  130 ++++++++++++++++
 gdb/testsuite/gdb.base/bg-execution-repeat.c       |   33 ++++
 gdb/testsuite/gdb.base/bg-execution-repeat.exp     |   86 +++++++++++
 .../gdb.base/continue-all-already-running.c        |   25 +++
 .../gdb.base/continue-all-already-running.exp      |   79 ++++++++++
 gdb/windows-nat.c                                  |    2 +-
 13 files changed, 642 insertions(+), 69 deletions(-)
 create mode 100644 gdb/testsuite/gdb.base/annota-input-while-running.c
 create mode 100644 gdb/testsuite/gdb.base/annota-input-while-running.exp
 create mode 100644 gdb/testsuite/gdb.base/bg-execution-repeat.c
 create mode 100644 gdb/testsuite/gdb.base/bg-execution-repeat.exp
 create mode 100644 gdb/testsuite/gdb.base/continue-all-already-running.c
 create mode 100644 gdb/testsuite/gdb.base/continue-all-already-running.exp
Comment 4 Sourceware Commits 2014-10-17 13:00:32 UTC
This is an automated email from the git hooks/post-receive script. It was
generated because a ref change was pushed to the repository containing
the project "gdb and binutils".

The branch, gdb-7.8-branch has been updated
       via  36c57a3ceeb2260913f7d2d349e994dd844cbcae (commit)
       via  e37951dc10da6940ef354f062fc43ee03687c571 (commit)
       via  04f0515702a6e5711d71203fcc0ea488161ba086 (commit)
       via  92e08c0d191908b7315603558226c7ca0bfa86a5 (commit)
      from  e7366593996e36a8ac452fc4577725fe9c3556ca (commit)

Those revisions listed above that are new to this repository have
not appeared on any other notification email; so we list those
revisions in full, below.

- Log -----------------------------------------------------------------
https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=36c57a3ceeb2260913f7d2d349e994dd844cbcae

commit 36c57a3ceeb2260913f7d2d349e994dd844cbcae
Author: Pedro Alves <palves@redhat.com>
Date:   Fri Oct 17 13:31:26 2014 +0100

    PR gdb/17471: Repeating a background command makes it foreground
    
    When we repeat a command, by just pressing <ret>, the input from the
    previous command is reused for the new command invocation.
    
    When an execution command strips the "&" out of its incoming argument
    string, to detect background execution, we poke a '\0' directly to the
    incoming argument string.
    
    Combine both, and a repeat of a background command loses the "&".
    
    This is actually only visible if args other than "&" are specified
    (e.g., "c 1&" or "next 2&" or "c -a&"), as in the special case of "&"
    alone (e.g. "c&") doesn't actually clobber the incoming string.
    
    Fix this by making strip_bg_char return a new string instead of poking
    a hole in the input string.
    
    New test included.
    
    Tested on x86_64 Fedora 20, native and gdbserver.
    
    gdb/
    2014-10-17  Pedro Alves  <palves@redhat.com>
    
    	PR gdb/17471
    	* infcmd.c (strip_bg_char): Change prototype and rewrite.  Now
    	returns a copy of the input.
    	(run_command_1, continue_command, step_1, jump_command)
    	(signal_command, until_command, advance_command, finish_command)
    	(attach_command): Adjust and install a cleanup to free the
    	stripped args.
    
    gdb/testsuite/
    2014-10-17  Pedro Alves  <palves@redhat.com>
    
    	PR gdb/17471
    	* gdb.base/bg-execution-repeat.c: New file.
    	* gdb.base/bg-execution-repeat.exp: New file.

https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=e37951dc10da6940ef354f062fc43ee03687c571

commit e37951dc10da6940ef354f062fc43ee03687c571
Author: Pedro Alves <palves@redhat.com>
Date:   Fri Oct 17 13:31:25 2014 +0100

    PR gdb/17300: Input after "c -a" crashes readline/GDB
    
    If all threads in the target were already running when the user does
    "c -a", nothing puts the inferior's terminal settings in effect and
    removes stdin from the event loop, which we must when running a
    foreground command.  The result is that user input afterwards crashes
    readline/gdb:
    
     (gdb) start
     Temporary breakpoint 1 at 0x4005d4: file continue-all-already-running.c, line 23.
     Starting program: continue-all-already-running
    
     Temporary breakpoint 1, main () at continue-all-already-running.c:23
     23        sleep (10);
     (gdb) c -a&
     Continuing.
     (gdb) c -a
     Continuing.
     p 1
     readline: readline_callback_read_char() called with no handler!
     Aborted (core dumped)
     $
    
    Backtrace:
    
     Program received signal SIGABRT, Aborted.
     0x0000003b36a35877 in __GI_raise (sig=sig@entry=6) at ../nptl/sysdeps/unix/sysv/linux/raise.c:56
     56        return INLINE_SYSCALL (tgkill, 3, pid, selftid, sig);
     (top-gdb) p 1
     $1 = 1
     (top-gdb) bt
     #0  0x0000003b36a35877 in __GI_raise (sig=sig@entry=6) at ../nptl/sysdeps/unix/sysv/linux/raise.c:56
     #1  0x0000003b36a36f68 in __GI_abort () at abort.c:89
     #2  0x0000000000784aa9 in rl_callback_read_char () at readline/callback.c:116
     #3  0x0000000000619181 in rl_callback_read_char_wrapper (client_data=0x0) at gdb/event-top.c:167
     #4  0x0000000000619557 in stdin_event_handler (error=0, client_data=0x0) at gdb/event-top.c:373
     #5  0x000000000061814a in handle_file_event (data=...) at gdb/event-loop.c:763
     #6  0x0000000000617631 in process_event () at gdb/event-loop.c:340
     #7  0x00000000006176f8 in gdb_do_one_event () at gdb/event-loop.c:404
     #8  0x0000000000617748 in start_event_loop () at gdb/event-loop.c:429
     #9  0x00000000006191b3 in cli_command_loop (data=0x0) at gdb/event-top.c:182
     #10 0x000000000060f538 in current_interp_command_loop () at gdb/interps.c:318
     #11 0x0000000000610701 in captured_command_loop (data=0x0) at gdb/main.c:323
     #12 0x000000000060c3f5 in catch_errors (func=0x6106e6 <captured_command_loop>, func_args=0x0, errstring=0x9002c1 "", mask=RETURN_MASK_ALL)
         at gdb/exceptions.c:237
     #13 0x0000000000611bff in captured_main (data=0x7fffffffd780) at gdb/main.c:1151
     #14 0x000000000060c3f5 in catch_errors (func=0x610afe <captured_main>, func_args=0x7fffffffd780, errstring=0x9002c1 "", mask=RETURN_MASK_ALL)
         at gdb/exceptions.c:237
     #15 0x0000000000611c28 in gdb_main (args=0x7fffffffd780) at gdb/main.c:1159
     #16 0x000000000045ef97 in main (argc=5, argv=0x7fffffffd888) at gdb/gdb.c:32
     (top-gdb)
    
    Tested on x86_64 Fedora 20, native and gdbserver.
    
    gdb/
    2014-10-17  Pedro Alves  <palves@redhat.com>
    
    	PR gdb/17300
    	* infcmd.c (continue_1): If continuing all threads in the
    	foreground, make sure the inferior's terminal settings are put in
    	effect.
    
    gdb/testsuite/
    2014-10-17  Pedro Alves  <palves@redhat.com>
    
    	PR gdb/17300
    	* gdb.base/continue-all-already-running.c: New file.
    	* gdb.base/continue-all-already-running.exp: New file.

https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=04f0515702a6e5711d71203fcc0ea488161ba086

commit 04f0515702a6e5711d71203fcc0ea488161ba086
Author: Pedro Alves <palves@redhat.com>
Date:   Fri Oct 17 13:31:25 2014 +0100

    PR gdb/17472: With annotations, input while executing in the foreground crashes readline/GDB
    
    Jan caught an intermittent GDB crash with the annota1.exp test:
    
     Starting program: .../gdb/testsuite/gdb.base/annota1 ^M
     [...]
     FAIL: gdb.base/annota1.exp: run until main breakpoint (timeout)
     [...]
     readline: readline_callback_read_char() called with no handler!^M
     ERROR: Process no longer exists
    
    All we need to is to continue the inferior in the foreground, and type
    a command while the inferior is running.  E.g.:
    
     (gdb) set annotate 2
    
     ▒▒pre-prompt
     (gdb)
     ▒▒prompt
     c
    
     ▒▒post-prompt
     Continuing.
    
     ▒▒starting
    
     ▒▒frames-invalid
    
     *inferior is running now*
    
     p 1<ret>
    
     readline: readline_callback_read_char() called with no handler!
     Aborted (core dumped)
     $
    
    
    When we run a foreground execution command we call
    target_terminal_inferior to stop GDB from processing input, and to put
    the inferior's terminal settings in effect.  Then we tell readline to
    hide the prompt with display_gdb_prompt, which clears readline's input
    callback too.  When the target stops, we call target_terminal_ours,
    which re-installs stdin in the event loop, and then we redisplay the
    prompt, reinstalling the readline callbacks.
    
    However, when annotations are in effect, the "frames-invalid"
    annotation code calls target_terminal_ours after 'resume' had already
    called target_terminal_inferior:
    
     (top-gdb) bt
     #0  0x000000000056b82f in annotate_frames_invalid () at gdb/annotate.c:219
     #1  0x000000000072e6cc in reinit_frame_cache () at gdb/frame.c:1705
     #2  0x0000000000594bb9 in registers_changed_ptid (ptid=...) at gdb/regcache.c:612
     #3  0x000000000064cca1 in target_resume (ptid=..., step=1, signal=GDB_SIGNAL_0) at gdb/target.c:2136
     #4  0x00000000005f57af in resume (step=1, sig=GDB_SIGNAL_0) at gdb/infrun.c:2263
     #5  0x00000000005f6051 in proceed (addr=18446744073709551615, siggnal=GDB_SIGNAL_DEFAULT, step=1) at gdb/infrun.c:2613
    
    And then once we hide the prompt and remove readline's input handler
    callback, we're in a bad state.  We end up with the target running
    supposedly in the foreground, but with stdin still installed on the
    event loop.  Any input then calls into readline, which aborts because
    no rl_linefunc callback handler is installed:
    
     Program received signal SIGABRT, Aborted.
     0x0000003b36a35877 in __GI_raise (sig=sig@entry=6) at ../nptl/sysdeps/unix/sysv/linux/raise.c:56
     56        return INLINE_SYSCALL (tgkill, 3, pid, selftid, sig);
    
     (top-gdb) bt
     #0  0x0000003b36a35877 in __GI_raise (sig=sig@entry=6) at ../nptl/sysdeps/unix/sysv/linux/raise.c:56
     #1  0x0000003b36a36f68 in __GI_abort () at abort.c:89
     During symbol reading, debug info gives source 9 included from file at zero line 0.
     During symbol reading, debug info gives command-line macro definition with non-zero line 19: _STDC_PREDEF_H 1.
     #2  0x0000000000784a25 in rl_callback_read_char () at src/readline/callback.c:116
     #3  0x0000000000619111 in rl_callback_read_char_wrapper (client_data=0x0) at src/gdb/event-top.c:167
     #4  0x00000000006194e7 in stdin_event_handler (error=0, client_data=0x0) at src/gdb/event-top.c:373
     #5  0x00000000006180da in handle_file_event (data=...) at src/gdb/event-loop.c:763
     #6  0x00000000006175c1 in process_event () at src/gdb/event-loop.c:340
     #7  0x0000000000617688 in gdb_do_one_event () at src/gdb/event-loop.c:404
     #8  0x00000000006176d8 in start_event_loop () at src/gdb/event-loop.c:429
     #9  0x0000000000619143 in cli_command_loop (data=0x0) at src/gdb/event-top.c:182
     #10 0x000000000060f4c8 in current_interp_command_loop () at src/gdb/interps.c:318
     #11 0x0000000000610691 in captured_command_loop (data=0x0) at src/gdb/main.c:323
     #12 0x000000000060c385 in catch_errors (func=0x610676 <captured_command_loop>, func_args=0x0, errstring=0x900241 "", mask=RETURN_MASK_ALL)
         at src/gdb/exceptions.c:237
     #13 0x0000000000611b8f in captured_main (data=0x7fffffffd7b0) at src/gdb/main.c:1151
     #14 0x000000000060c385 in catch_errors (func=0x610a8e <captured_main>, func_args=0x7fffffffd7b0, errstring=0x900241 "", mask=RETURN_MASK_ALL)
         at src/gdb/exceptions.c:237
     #15 0x0000000000611bb8 in gdb_main (args=0x7fffffffd7b0) at src/gdb/main.c:1159
     #16 0x000000000045ef57 in main (argc=3, argv=0x7fffffffd8b8) at src/gdb/gdb.c:32
    
    The fix is to make the annotation code call target_terminal_inferior
    again after printing, if the inferior's settings were in effect.
    
    While at it, when we're doing output only, instead of
    target_terminal_ours, we should call target_terminal_ours_for_output.
    The latter doesn't actually remove stdin from the event loop, and also
    leaves SIGINT forwarded to the target.
    
    New test included.
    
    Tested on x86_64 Fedora 20, native and gdbserver.
    
    gdb/
    2014-10-17  Pedro Alves  <palves@redhat.com>
    
    	PR gdb/17472
    	* annotate.c (annotate_breakpoints_invalid): Use
    	target_terminal_our_for_output instead of target_terminal_ours.
    	Give back the terminal to the target.
    	(annotate_frames_invalid): Likewise.
    
    gdb/testsuite/
    2014-10-17  Pedro Alves  <palves@redhat.com>
    
    	PR gdb/17472
    	* gdb.base/annota-input-while-running.c: New file.
    	* gdb.base/annota-input-while-running.exp: New file.

https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=92e08c0d191908b7315603558226c7ca0bfa86a5

commit 92e08c0d191908b7315603558226c7ca0bfa86a5
Author: Pedro Alves <palves@redhat.com>
Date:   Fri Oct 17 13:49:28 2014 +0100

    Make common code handle target_terminal_* idempotency
    
    I found a place that should be giving back the terminal to the target,
    but only if the target was already owning it.  So I need to add a
    getter for who owns the terminal.
    
    The trouble is that several places/target have their own globals to
    track this state:
    
     - inflow.c:terminal_is_ours
     - remote.c:remote_async_terminal_ours_p
     - linux-nat.c:async_terminal_is_ours
     - go32-nat.c:terminal_is_ours
    
    While one might think of adding a new target_ops method to query this,
    conceptually, this state isn't really part of a particular target_ops.
    Considering multi-target, the core shouldn't have to ask all targets
    to know whether it's GDB that owns the terminal.  There's only one GDB
    (or rather, only one top level interpreter).
    
    So what this comment does is add a new global that is tracked by the
    core instead.  A subsequent pass may later remove the other globals.
    
    Tested on x86_64 Fedora 20, native and gdbserver.
    
    gdb/
    2014-10-17  Pedro Alves  <palves@redhat.com>
    
    	* target.c (enum terminal_state): New enum.
    	(terminal_state): New global.
    	(target_terminal_init): New function.
    	(target_terminal_inferior): Skip if inferior already owns the
    	terminal.
    	(target_terminal_ours, target_terminal_ours_for_output): New
    	functions.
    	* target.h (target_terminal_init): Convert to function prototype.
    	(target_terminal_ours_for_output): Convert to function prototype
    	and tweak comment.
    	(target_terminal_ours): Convert to function prototype and tweak
    	comment.
    	* windows-nat.c (do_initial_windows_stuff): Call
    	target_terminal_init instead of child_terminal_init_with_pgrp.

-----------------------------------------------------------------------

Summary of changes:
 gdb/ChangeLog                                      |   42 +++++
 gdb/annotate.c                                     |   22 +++-
 gdb/infcmd.c                                       |  160 +++++++++++++-------
 gdb/target.c                                       |   64 ++++++++
 gdb/target.h                                       |   25 ++--
 gdb/testsuite/ChangeLog                            |   18 +++
 .../gdb.base/annota-input-while-running.c          |   25 +++
 .../gdb.base/annota-input-while-running.exp        |  130 ++++++++++++++++
 gdb/testsuite/gdb.base/bg-execution-repeat.c       |   33 ++++
 gdb/testsuite/gdb.base/bg-execution-repeat.exp     |   86 +++++++++++
 .../gdb.base/continue-all-already-running.c        |   25 +++
 .../gdb.base/continue-all-already-running.exp      |   79 ++++++++++
 gdb/windows-nat.c                                  |    2 +-
 13 files changed, 642 insertions(+), 69 deletions(-)
 create mode 100644 gdb/testsuite/gdb.base/annota-input-while-running.c
 create mode 100644 gdb/testsuite/gdb.base/annota-input-while-running.exp
 create mode 100644 gdb/testsuite/gdb.base/bg-execution-repeat.c
 create mode 100644 gdb/testsuite/gdb.base/bg-execution-repeat.exp
 create mode 100644 gdb/testsuite/gdb.base/continue-all-already-running.c
 create mode 100644 gdb/testsuite/gdb.base/continue-all-already-running.exp
Comment 5 Pedro Alves 2014-10-17 13:29:52 UTC
Fixed, mainline and 7.8.1.
Comment 6 Sourceware Commits 2014-10-29 19:48:16 UTC Comment hidden (spam)
Comment 7 Sourceware Commits 2014-12-25 00:46:11 UTC Comment hidden (spam)
Comment 8 Pedro Alves 2016-08-09 20:55:20 UTC
Fix was already fixed a long while ago.  Closing.