From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1726) id 2CC7F3857374; Fri, 22 Apr 2022 17:51:44 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 2CC7F3857374 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable From: Andrew Burgess To: gdb-cvs@sourceware.org Subject: [binutils-gdb] gdb: handle bracketed-paste-mode and EOF correctly X-Act-Checkin: binutils-gdb X-Git-Author: Andrew Burgess X-Git-Refname: refs/heads/master X-Git-Oldrev: 4fb7bc4b147fd30b781ea2dad533956d0362295a X-Git-Newrev: 91395d97d905c31ac38513e4aaedecb3b25e818f Message-Id: <20220422175144.2CC7F3857374@sourceware.org> Date: Fri, 22 Apr 2022 17:51:44 +0000 (GMT) X-BeenThere: gdb-cvs@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gdb-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 22 Apr 2022 17:51:44 -0000 https://sourceware.org/git/gitweb.cgi?p=3Dbinutils-gdb.git;h=3D91395d97d905= c31ac38513e4aaedecb3b25e818f commit 91395d97d905c31ac38513e4aaedecb3b25e818f Author: Andrew Burgess Date: Tue Feb 15 17:28:03 2022 +0000 gdb: handle bracketed-paste-mode and EOF correctly =20 This commit replaces an earlier commit that worked around the issues reported in bug PR gdb/28833. =20 The previous commit just implemented a work around in order to avoid the worst results of the bug, but was not a complete solution. The full solution was considered too risky to merge close to branching GDB 12. This improved fix has been applied after GDB 12 branched. See this thread for more details: =20 https://sourceware.org/pipermail/gdb-patches/2022-March/186391.html =20 This commit replaces this earlier commit: =20 commit 74a159a420d4b466cc81061c16d444568e36740c Date: Fri Mar 11 14:44:03 2022 +0000 =20 gdb: work around prompt corruption caused by bracketed-paste-mode =20 Please read that commit for a full description of the bug, and why is occurs. =20 In this commit I extend GDB to use readline's rl_deprep_term_function hook to call a new function gdb_rl_deprep_term_function. From this new function we can now print the 'quit' message, this replaces the old printing of 'quit' in command_line_handler. Of course, we only print 'quit' in gdb_rl_deprep_term_function when we are handling EOF, but thanks to the previous commit (to readline) we now know when this is. =20 There are two aspects of this commit that are worth further discussion, the first is in the new gdb_rl_deprep_term_function function. In here I have used a scoped_restore_tmpl to disable the readline global variable rl_eof_found. =20 The reason for this is that, in rl_deprep_terminal, readline will print an extra '\n' character before printing the escape sequence to leave bracketed paste mode. You might then think that in the gdb_rl_deprep_term_function function, we could simply print "quit" and rely on rl_deprep_terminal to print the trailing '\n'. However, rl_deprep_terminal only prints the '\n' when bracketed paste mode is on. If the user has turned this feature off, no '\n' is printed. This means that in gdb_rl_deprep_term_function we need to print "quit" when bracketed paste mode is on, and "quit\n" when bracketed paste mode is off. =20 We could absolutely do that, no problem, but given we know how rl_deprep_terminal is implemented, it's easier (I think) to just temporarily clear rl_eof_found, this prevents the '\n' being printed from rl_deprep_terminal, and so in gdb_rl_deprep_term_function, we can now always print "quit\n" and this works for all cases. =20 The second issue that should be discussed is backwards compatibility with older versions of readline. GDB can be built against the system readline, which might be older than the version contained within GDB's tree. If this is the case then the system readline might not contain the fixes needed to support correctly printing the 'quit' string. =20 To handle this situation I have retained the existing code in command_line_handler for printing 'quit', however, this code is only used now if the version of readline we are using doesn't not include the required fixes. And so, if a user is using an older version of readline, and they have bracketed paste mode on, then they will see the 'quit' sting printed on the line below the prompt, like this: =20 (gdb) quit =20 I think this is the best we can do when someone builds GDB against an older version of readline. =20 Using a newer version of readline, or the patched version of readline that is in-tree, will now give a result like this in all cases: =20 (gdb) quit =20 Which is what we want. =20 Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=3D28833 Diff: --- gdb/event-top.c | 68 +++++++++++++++++++++++++++++++++++++++++++++++------= ---- gdb/event-top.h | 6 +++++ gdb/top.c | 1 + 3 files changed, 63 insertions(+), 12 deletions(-) diff --git a/gdb/event-top.c b/gdb/event-top.c index 6261020b4ac..35664312f42 100644 --- a/gdb/event-top.c +++ b/gdb/event-top.c @@ -742,6 +742,25 @@ handle_line_of_input (struct buffer *cmd_line_buffer, return cmd; } =20 +/* See event-top.h. */ + +void +gdb_rl_deprep_term_function (void) +{ +#ifdef RL_STATE_EOF + gdb::optional> restore_eof_found; + + if (RL_ISSTATE (RL_STATE_EOF)) + { + printf_unfiltered ("quit\n"); + restore_eof_found.emplace (&rl_eof_found, 0); + } + +#endif /* RL_STATE_EOF */ + + rl_deprep_terminal (); +} + /* Handle a complete line of input. This is called by the callback mechanism within the readline library. Deal with incomplete commands as well, by saving the partial input in a global @@ -764,26 +783,51 @@ command_line_handler (gdb::unique_xmalloc_ptr &= &rl) This happens at the end of a testsuite run, after Expect has hung up but GDB is still alive. In such a case, we just quit gdb killing the inferior program too. This also happens if the - user sends EOF, which is usually bound to ctrl+d. + user sends EOF, which is usually bound to ctrl+d. */ + +#ifndef RL_STATE_EOF + /* When readline is using bracketed paste mode, then, when eof is + received, readline will emit the control sequence to leave + bracketed paste mode. + + This control sequence ends with \r, which means that the "quit" we + are about to print will overwrite the prompt on this line. + + The solution to this problem is to actually print the "quit" + message from gdb_rl_deprep_term_function (see above), however, we + can only do that if we can know, in that function, when eof was + received. + + Unfortunately, with older versions of readline, it is not possible + in the gdb_rl_deprep_term_function to know if eof was received or + not, and, as GDB can be built against the system readline, which + could be older than the readline in GDB's repository, then we + can't be sure that we can work around this prompt corruption in + the gdb_rl_deprep_term_function function. =20 - What we want to do in this case is print "quit" after the GDB - prompt, as if the user had just typed "quit" and pressed return. + If we get here, RL_STATE_EOF is not defined. This indicates that + we are using an older readline, and couldn't print the quit + message in gdb_rl_deprep_term_function. So, what we do here is + check to see if bracketed paste mode is on or not. If it's on + then we print a \n and then the quit, this means the user will + see: =20 - This used to work just fine, but unfortunately, doesn't play well - with readline's bracketed paste mode. By the time we get here, - readline has already sent the control sequence to leave bracketed - paste mode, and this sequence ends with a '\r' character. As a - result, if bracketed paste mode is on, and we print quit here, - then this will overwrite the prompt. + (gdb) + quit =20 - To work around this issue, when bracketed paste mode is enabled, - we first print '\n' to move to the next line, and then print the - quit. This isn't ideal, but avoids corrupting the prompt. */ + Rather than the usual: + + (gdb) quit + + Which we will get with a newer readline, but this really is the + best we can do with older versions of readline. */ const char *value =3D rl_variable_value ("enable-bracketed-paste"); if (value !=3D nullptr && strcmp (value, "on") =3D=3D 0 && ((rl_readline_version >> 8) & 0xff) > 0x07) printf_unfiltered ("\n"); printf_unfiltered ("quit\n"); +#endif + execute_command ("quit", 1); } else if (cmd =3D=3D NULL) diff --git a/gdb/event-top.h b/gdb/event-top.h index 078482b79aa..722e636a110 100644 --- a/gdb/event-top.h +++ b/gdb/event-top.h @@ -70,6 +70,12 @@ extern void gdb_rl_callback_handler_install (const char = *prompt); currently installed. */ extern void gdb_rl_callback_handler_reinstall (void); =20 +/* Called by readline after a complete line has been gathered from the + user, but before the line is dispatched to back to GDB. This function + is a wrapper around readline's builtin rl_deprep_terminal function, and + handles the case where readline received EOF. */ +extern void gdb_rl_deprep_term_function (void); + typedef void (*segv_handler_t) (int); =20 /* On construction, replaces the current thread's SIGSEGV handler with diff --git a/gdb/top.c b/gdb/top.c index 1cfffbeee7e..73cb389a141 100644 --- a/gdb/top.c +++ b/gdb/top.c @@ -2225,6 +2225,7 @@ init_main (void) rl_completion_display_matches_hook =3D cli_display_match_list; rl_readline_name =3D "gdb"; rl_terminal_name =3D getenv ("TERM"); + rl_deprep_term_function =3D gdb_rl_deprep_term_function; =20 /* The name for this defun comes from Bash, where it originated. 15 is Control-o, the same binding this function has in Bash. */