Bug 15811 - Using interpreter-exec from a sourced file crashes gdb.
Summary: Using interpreter-exec from a sourced file crashes gdb.
Status: RESOLVED FIXED
Alias: None
Product: gdb
Classification: Unclassified
Component: mi (show other bugs)
Version: HEAD
: P2 normal
Target Milestone: 13.1
Assignee: Tom Tromey
URL:
Keywords:
: 21388 (view as bug list)
Depends on:
Blocks:
 
Reported: 2013-08-01 15:40 UTC by Andrew Burgess
Modified: 2022-08-31 17:15 UTC (History)
2 users (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 Andrew Burgess 2013-08-01 15:40:01 UTC
If I do the following...

## START ##
> cat cmd.gdb
interpreter-exec mi "-gdb-show verbose"
> gdb -q -x cmd.gdb
^done,value="off"
(gdb) 
(gdb) Invalid or non-`poll'able fd 7
error detected on stdin
## END ##

I took a look at this and the issue is something like this:

 - during start up gdb initialises the tui interpreter, at this point the input stream is stdin, and gdb registers a file event handler against stdin.
 - the '-x' triggers a call to read_command_file, as part of this process gdb opens the script file and makes this newly opened file the "current" input stream.
 - When processing the intpereter-exec in the script file the MI interpreter in started up, this cases a file event handler against the script file descriptor (I'm not sure this is the correct thing to do...)
 - When the interpreter-exec has finished the MI interpreter is suspended, de-registering the file event handler previously registered.
 - The TUI interpreter is "re-activated", this triggers the registering of a file event handler against the open script file.
 - When the script is finished the script file is closed, but the script file descriptor remains in the list of file descriptors being monitored for events by gdb.
 - Finally, gdb enters the standard interactive command loop and polls for events on all monitored file descriptors, including the closed script file descriptor, this triggers the error.


It's also possible to trigger this bug using "source <script-file>" at an interactive gdb prompt, or when using the -ex 'source <script-file>' from the command line.

Here's a patch to add a reproducer to the gdb test suite:

diff --git a/gdb/testsuite/gdb.base/interpreter-exec.gdb b/gdb/testsuite/gdb.base/interpreter-exec.gdb
new file mode 100644
index 0000000..6afff23
--- /dev/null
+++ b/gdb/testsuite/gdb.base/interpreter-exec.gdb
@@ -0,0 +1,20 @@
+# This testcase is part of GDB, the GNU debugger.
+
+# Copyright 2013 Free Software Foundation, Inc.
+
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+# Test GDB's "source" command for scripts containing interpreter-exec.
+
+interpreter-exec mi "-gdb-show verbose"
diff --git a/gdb/testsuite/gdb.base/source.exp b/gdb/testsuite/gdb.base/source.exp
index 61fd221..dfe0539 100644
--- a/gdb/testsuite/gdb.base/source.exp
+++ b/gdb/testsuite/gdb.base/source.exp
@@ -57,4 +57,10 @@ gdb_test "source -v -s ./source-test.gdb" \
     "echo test source options.*" \
     "source -v -s"
 
+# There was a case where sourcing a script containing "interpreter-exec"
+# commands would corrupt the interpreter mechanism and crash gdb.
+gdb_test "source ${srcdir}/${subdir}/interpreter-exec.gdb" \
+    "\\^done,value=\"off\"" \
+    "source interpreter-exec"
+
 gdb_exit
Comment 1 Tom Tromey 2022-08-12 19:41:12 UTC
*** Bug 21388 has been marked as a duplicate of this bug. ***
Comment 2 Tom Tromey 2022-08-12 19:50:28 UTC
I have a patch.
Comment 3 Sourceware Commits 2022-08-31 17:15:12 UTC
The master branch has been updated by Tom Tromey <tromey@sourceware.org>:

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

commit 1e28eebbbc34ebec69ea4913f7e8d19352e35630
Author: Tom Tromey <tromey@adacore.com>
Date:   Fri Aug 12 13:50:35 2022 -0600

    Fix "source" with interpreter-exec
    
    PR mi/15811 points out that "source"ing a file that uses
    interpreter-exec will put gdb in a weird state, where the CLI stops
    working.  The bug is that tui_interp::suspend does not unregister the
    event file descriptor.
    
    The test case is from Andrew Burgess.
    
    Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=15811
Comment 4 Tom Tromey 2022-08-31 17:15:31 UTC
Fixed.