Blame SOURCES/gdb-rhbz2015131-restore-inferior-terminal-1of2.patch

57a32e
From FEDORA_PATCHES Mon Sep 17 00:00:00 2001
57a32e
From: Simon Marchi <simon.marchi@polymtl.ca>
57a32e
Date: Wed, 22 Aug 2018 11:09:45 -0400
57a32e
Subject: gdb-rhbz2015131-restore-inferior-terminal-1of2.patch
57a32e
57a32e
;; Fix restoring of inferior terminal settings
57a32e
;; (Simon Marchi, RHBZ 2015131)
57a32e
57a32e
I noticed that the child_terminal_save_inferior function was not used
57a32e
since the commit f6ac5f3d63e0 ("Convert struct target_ops to C++").  I
57a32e
was able to make a little test program to illustrate the problem (see
57a32e
test case).
57a32e
57a32e
I think we're just missing the override of the terminal_save_inferior
57a32e
method in inf_child_target (along with the other terminal-related
57a32e
methods).
57a32e
57a32e
Instead of creating a new test, I thought that gdb.base/term.exp was a
57a32e
good candidate for testing that gdb restores properly the inferior's
57a32e
terminal settings.
57a32e
57a32e
gdb/ChangeLog:
57a32e
57a32e
	* inf-child.h (inf_child_target) <terminal_save_inferior>: New.
57a32e
	* inf-child.c (inf_child_target::terminal_save_inferior): New.
57a32e
57a32e
gdb/testsuite/ChangeLog:
57a32e
57a32e
	* gdb.base/term.exp: Compare terminal settings with values from
57a32e
	the inferior.
57a32e
	* gdb.base/term.c: Get and set terminal settings.
57a32e
57a32e
diff --git a/gdb/inf-child.c b/gdb/inf-child.c
57a32e
--- a/gdb/inf-child.c
57a32e
+++ b/gdb/inf-child.c
57a32e
@@ -113,6 +113,12 @@ inf_child_target::terminal_inferior ()
57a32e
   child_terminal_inferior (this);
57a32e
 }
57a32e
 
57a32e
+void
57a32e
+inf_child_target::terminal_save_inferior ()
57a32e
+{
57a32e
+  child_terminal_save_inferior (this);
57a32e
+}
57a32e
+
57a32e
 void
57a32e
 inf_child_target::terminal_ours_for_output ()
57a32e
 {
57a32e
diff --git a/gdb/inf-child.h b/gdb/inf-child.h
57a32e
--- a/gdb/inf-child.h
57a32e
+++ b/gdb/inf-child.h
57a32e
@@ -46,6 +46,7 @@ public:
57a32e
   bool supports_terminal_ours () override;
57a32e
   void terminal_init () override;
57a32e
   void terminal_inferior () override;
57a32e
+  void terminal_save_inferior () override;
57a32e
   void terminal_ours_for_output () override;
57a32e
   void terminal_ours () override;
57a32e
   void terminal_info (const char *, int) override;
57a32e
diff --git a/gdb/testsuite/gdb.base/term.c b/gdb/testsuite/gdb.base/term.c
57a32e
--- a/gdb/testsuite/gdb.base/term.c
57a32e
+++ b/gdb/testsuite/gdb.base/term.c
57a32e
@@ -15,7 +15,29 @@
57a32e
    You should have received a copy of the GNU General Public License
57a32e
    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
57a32e
 
57a32e
+#include <termios.h>
57a32e
+#include <unistd.h>
57a32e
+
57a32e
+static struct termios t;
57a32e
+
57a32e
+static void
57a32e
+break_here ()
57a32e
+{
57a32e
+}
57a32e
+
57a32e
 int main ()
57a32e
 {
57a32e
+  tcgetattr (0, &t);
57a32e
+  break_here ();
57a32e
+
57a32e
+  /* Disable ECHO.  */
57a32e
+  t.c_lflag &= ~ECHO;
57a32e
+  tcsetattr (0, TCSANOW, &t);
57a32e
+  tcgetattr (0, &t);
57a32e
+  break_here ();
57a32e
+
57a32e
+  tcgetattr (0, &t);
57a32e
+  break_here ();
57a32e
+
57a32e
   return 0;
57a32e
 }
57a32e
diff --git a/gdb/testsuite/gdb.base/term.exp b/gdb/testsuite/gdb.base/term.exp
57a32e
--- a/gdb/testsuite/gdb.base/term.exp
57a32e
+++ b/gdb/testsuite/gdb.base/term.exp
57a32e
@@ -13,6 +13,9 @@
57a32e
 # You should have received a copy of the GNU General Public License
57a32e
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
57a32e
 
57a32e
+# Test that GDB saves and restores terminal settings correctly.  Also check
57a32e
+# the output of the "info terminal" command.
57a32e
+
57a32e
 if { [prepare_for_testing "failed to prepare" term term.c] } {
57a32e
     return -1
57a32e
 }
57a32e
@@ -20,28 +23,84 @@ if { [prepare_for_testing "failed to prepare" term term.c] } {
57a32e
 # Once before running the program.
57a32e
 gdb_test "info terminal" \
57a32e
     "No saved terminal information.*" \
57a32e
-    "test info terminal"
57a32e
+    "test info terminal pre-execution"
57a32e
 
57a32e
-if ![runto_main] then {
57a32e
-    fail "can't run to main"
57a32e
+if ![runto break_here] then {
57a32e
+    fail "can't run to break_here"
57a32e
     return 0
57a32e
 }
57a32e
 
57a32e
-# Once while the program is running and stopped.
57a32e
+# Read the inferior's terminal settings, saved in the T global variable.
57a32e
+
57a32e
+proc read_term_settings_from_inferior {} {
57a32e
+    set termios(c_iflag) [get_hexadecimal_valueof "t.c_iflag" oops]
57a32e
+    set termios(c_oflag) [get_hexadecimal_valueof "t.c_oflag" oops]
57a32e
+    set termios(c_cflag) [get_hexadecimal_valueof "t.c_cflag" oops]
57a32e
+    set termios(c_lflag) [get_hexadecimal_valueof "t.c_lflag" oops]
57a32e
+
57a32e
+    return [array get termios]
57a32e
+}
57a32e
+
57a32e
+# Validate that gdb's notion of the inferior's terminal settings are consistent
57a32e
+# with the values read from the inferior.
57a32e
+
57a32e
+proc compare_gdb_and_inferior_settings { t } {
57a32e
+    global decimal
57a32e
+    array set termios $t
57a32e
+
57a32e
+    gdb_test "info terminal" \
57a32e
+    [multi_line "Inferior's terminal status .currently saved by GDB.:" \
57a32e
+                "File descriptor flags = .*" \
57a32e
+                "Process group = $decimal" \
57a32e
+                "c_iflag = ${termios(c_iflag)}, c_oflag = ${termios(c_oflag)}," \
57a32e
+                "c_cflag = ${termios(c_cflag)}, c_lflag = ${termios(c_lflag)}.*" ]
57a32e
+}
57a32e
 
57a32e
-# While only native targets save terminal status, we still test
57a32e
-# everywhere to make sure that the command doesn't misbehave.
57a32e
 if {[target_info gdb_protocol] == ""} {
57a32e
-    set term_re "Inferior's terminal status .currently saved by GDB.:.*"
57a32e
+    # Record the initial terminal settings.  Verify that GDB's version of the
57a32e
+    # inferior's terminal settings is right.
57a32e
+    with_test_prefix "initial" {
57a32e
+        array set termios1 [read_term_settings_from_inferior]
57a32e
+        compare_gdb_and_inferior_settings [array get termios1]
57a32e
+    }
57a32e
+
57a32e
+    # Continue until after the inferior removes ECHO from its terminal settings.
57a32e
+    gdb_continue_to_breakpoint "continue until after tcsetattr"
57a32e
+
57a32e
+    # After the inferior has changed its terminal settings, check that GDB's
57a32e
+    # saved version reflects the new settings correctly.
57a32e
+    with_test_prefix "post tcsetattr" {
57a32e
+        array set termios2 [read_term_settings_from_inferior]
57a32e
+        compare_gdb_and_inferior_settings [array get termios2]
57a32e
+
57a32e
+        # Make sure that the current settings are different than the initial
57a32e
+        # settings... otherwise this test is meaningless.
57a32e
+        gdb_assert {${termios1(c_lflag)} != ${termios2(c_lflag)}}
57a32e
+    }
57a32e
+
57a32e
+    # Continue again...
57a32e
+    gdb_continue_to_breakpoint "continue again"
57a32e
+
57a32e
+    # ... and verify again, to validate that when resuming, GDB restored the
57a32e
+    # inferior's terminal settings correctly.
57a32e
+    with_test_prefix "after last resume" {
57a32e
+        array set termios3 [read_term_settings_from_inferior]
57a32e
+        compare_gdb_and_inferior_settings [array get termios3]
57a32e
+        gdb_assert {${termios2(c_iflag)} == ${termios3(c_iflag)}}
57a32e
+        gdb_assert {${termios2(c_oflag)} == ${termios3(c_oflag)}}
57a32e
+        gdb_assert {${termios2(c_cflag)} == ${termios3(c_cflag)}}
57a32e
+        gdb_assert {${termios2(c_lflag)} == ${termios3(c_lflag)}}
57a32e
+    }
57a32e
 } else {
57a32e
-    set term_re "No saved terminal information\\."
57a32e
+    # While only native targets save terminal status, we still test
57a32e
+    # that the command doesn't misbehave.
57a32e
+    gdb_test "info terminal" "No saved terminal information\\." "info terminal at breakpoint"
57a32e
 }
57a32e
 
57a32e
-gdb_test "info terminal" $term_re "info terminal at breakpoint"
57a32e
-
57a32e
+delete_breakpoints
57a32e
 gdb_continue_to_end
57a32e
 
57a32e
 # One last time after the program having exited.
57a32e
 gdb_test "info terminal" \
57a32e
     "No saved terminal information.*" \
57a32e
-    "test info terminal #2"
57a32e
+    "test info terminal post-execution"