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

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