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

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