Use save_vars when saving and restoring GDBFLAGS

Message ID 20240206173132.1272893-1-tom@tromey.com
State New
Headers
Series Use save_vars when saving and restoring GDBFLAGS |

Checks

Context Check Description
linaro-tcwg-bot/tcwg_gdb_build--master-aarch64 success Testing passed
linaro-tcwg-bot/tcwg_gdb_build--master-arm success Testing passed
linaro-tcwg-bot/tcwg_gdb_check--master-arm success Testing passed
linaro-tcwg-bot/tcwg_gdb_check--master-aarch64 success Testing passed

Commit Message

Tom Tromey Feb. 6, 2024, 5:31 p.m. UTC
  I noticed that mi-async.exp manually restored GDBFLAGS.  I then looked
and found a number of other places doing the same.  This patch changes
the test suite to use save_vars for this instead.

Regression tested on x86-64 Fedora 38.
---
 .../gdb.base/batch-preserve-term-settings.exp    |  4 ----
 .../gdb.base/bp-cmds-execution-x-script.exp      | 14 ++++----------
 .../gdb.base/break-main-file-remove-fail.exp     |  9 ++++-----
 .../gdb.base/break-on-linker-gcd-function.exp    |  8 ++++----
 gdb/testsuite/gdb.base/break-unload-file.exp     |  9 ++++-----
 gdb/testsuite/gdb.base/code_elim.exp             |  9 ++++-----
 .../gdb.base/paginate-execution-startup.exp      |  8 ++------
 gdb/testsuite/gdb.dwarf2/count.exp               |  9 ++++-----
 gdb/testsuite/gdb.dwarf2/dw2-icycle.exp          |  8 ++++----
 gdb/testsuite/gdb.dwarf2/dw2-var-zero-addr.exp   |  8 ++++----
 gdb/testsuite/gdb.dwarf2/dw4-sig-type-unused.exp |  8 ++++----
 gdb/testsuite/gdb.dwarf2/implptrconst.exp        |  8 ++++----
 gdb/testsuite/gdb.dwarf2/implptrpiece.exp        |  8 ++++----
 gdb/testsuite/gdb.dwarf2/nostaticblock.exp       |  8 ++++----
 gdb/testsuite/gdb.dwarf2/symtab-producer.exp     | 15 ++++++---------
 gdb/testsuite/gdb.mi/mi-async.exp                | 13 +++++--------
 gdb/testsuite/gdb.python/py-prompt.exp           |  6 +-----
 gdb/testsuite/gdb.threads/fork-plus-threads.exp  | 15 ++++++---------
 .../forking-threads-plus-breakpoint.exp          |  8 ++++----
 .../process-dies-while-handling-bp.exp           |  8 ++++----
 gdb/testsuite/lib/gdb.exp                        | 16 ++++++----------
 21 files changed, 82 insertions(+), 117 deletions(-)
  

Patch

diff --git a/gdb/testsuite/gdb.base/batch-preserve-term-settings.exp b/gdb/testsuite/gdb.base/batch-preserve-term-settings.exp
index fbaba634e20..00c53e1925a 100644
--- a/gdb/testsuite/gdb.base/batch-preserve-term-settings.exp
+++ b/gdb/testsuite/gdb.base/batch-preserve-term-settings.exp
@@ -229,8 +229,6 @@  proc test_terminal_settings_preserved_after_cli_exit { cmds } {
 	return
     }
 
-    set saved_gdbflags $GDBFLAGS
-
     set stty_supported [run_stty "stty before" stty_before]
 
     set test "start gdb"
@@ -297,8 +295,6 @@  proc test_terminal_settings_preserved_after_sigterm { } {
 	return
     }
 
-    set saved_gdbflags $GDBFLAGS
-
     set stty_supported [run_stty "stty before" stty_before]
 
     set test "start gdb"
diff --git a/gdb/testsuite/gdb.base/bp-cmds-execution-x-script.exp b/gdb/testsuite/gdb.base/bp-cmds-execution-x-script.exp
index 02719b1a7af..12c3bd9a221 100644
--- a/gdb/testsuite/gdb.base/bp-cmds-execution-x-script.exp
+++ b/gdb/testsuite/gdb.base/bp-cmds-execution-x-script.exp
@@ -25,23 +25,17 @@  if {[build_executable "failed to prepare" $testfile $srcfile debug]} {
 
 set x_file ${srcdir}/${subdir}/$testfile.gdb
 
-global GDBFLAGS
-set saved_gdbflags $GDBFLAGS
-
 # Run the test proper.
 
 proc test {} {
     global inferior_exited_re
     global binfile
     global gdb_prompt
-    global saved_gdbflags GDBFLAGS
+    global GDBFLAGS
     global x_file
 
     gdb_exit
 
-    set GDBFLAGS "$GDBFLAGS -x $x_file $binfile"
-
-    set GDBFLAGS $saved_gdbflags
     append GDBFLAGS " -ex \"set height unlimited\""
     append GDBFLAGS " -x \"$x_file\""
     append GDBFLAGS " --args \"$binfile\""
@@ -65,6 +59,6 @@  proc test {} {
     }
 }
 
-test
-
-set GDBFLAGS $saved_gdbflags
+save_vars {GDBFLAGS} {
+    test
+}
diff --git a/gdb/testsuite/gdb.base/break-main-file-remove-fail.exp b/gdb/testsuite/gdb.base/break-main-file-remove-fail.exp
index 8a8c508b340..c2e6ba33103 100644
--- a/gdb/testsuite/gdb.base/break-main-file-remove-fail.exp
+++ b/gdb/testsuite/gdb.base/break-main-file-remove-fail.exp
@@ -35,8 +35,6 @@  proc test_remove_bp { initial_load } {
 
 	gdb_exit
 
-	set saved_gdbflags $GDBFLAGS
-
 	# See "used to behave differently" further below.
 	if { $initial_load == "file" } {
 	    gdb_start
@@ -48,12 +46,13 @@  proc test_remove_bp { initial_load } {
 	    # implementations use as binary.
 	    set last_loaded_file $binfile
 
-	    set GDBFLAGS "$GDBFLAGS $binfile"
-	    gdb_start
+	    save_vars {GDBFLAGS} {
+		append GDBFLAGS " $binfile"
+		gdb_start
+	    }
 	}
 	gdb_reinitialize_dir $srcdir/$subdir
 	gdb_reload
-	set GDBFLAGS $saved_gdbflags
 
 	if ![runto start] {
 	    return
diff --git a/gdb/testsuite/gdb.base/break-on-linker-gcd-function.exp b/gdb/testsuite/gdb.base/break-on-linker-gcd-function.exp
index 613c9dc47e8..f68e2c0b891 100644
--- a/gdb/testsuite/gdb.base/break-on-linker-gcd-function.exp
+++ b/gdb/testsuite/gdb.base/break-on-linker-gcd-function.exp
@@ -48,10 +48,10 @@  proc set_breakpoint_on_gcd_function {} {
 
 set_breakpoint_on_gcd_function
 
-set saved_gdbflags $GDBFLAGS
-set GDBFLAGS "$GDBFLAGS --readnow"
-clean_restart ${testfile}
-set GDBFLAGS $saved_gdbflags
+save_vars {GDBFLAGS} {
+    append GDBFLAGS " --readnow"
+    clean_restart ${testfile}
+}
 
 with_test_prefix "readnow" {
     set_breakpoint_on_gcd_function
diff --git a/gdb/testsuite/gdb.base/break-unload-file.exp b/gdb/testsuite/gdb.base/break-unload-file.exp
index 2641feccec9..5b06e501eef 100644
--- a/gdb/testsuite/gdb.base/break-unload-file.exp
+++ b/gdb/testsuite/gdb.base/break-unload-file.exp
@@ -38,8 +38,6 @@  proc test_break { initial_load always_inserted break_command } {
     with_test_prefix "$prefix" {
 	gdb_exit
 
-	set saved_gdbflags $GDBFLAGS
-
 	# See "used to behave differently" further below.
 	if { $initial_load == "file" } {
 	    gdb_start
@@ -51,13 +49,14 @@  proc test_break { initial_load always_inserted break_command } {
 	    # implementations use as binary.
 	    set last_loaded_file $binfile
 
-	    set GDBFLAGS "$GDBFLAGS $binfile"
-	    gdb_start
+	    save_vars {GDBFLAGS} {
+		append GDBFLAGS " $binfile"
+		gdb_start
+	    }
 	}
 
 	gdb_reinitialize_dir $srcdir/$subdir
 	gdb_reload
-	set GDBFLAGS $saved_gdbflags
 
 	if {![runto_main]} {
 	    return
diff --git a/gdb/testsuite/gdb.base/code_elim.exp b/gdb/testsuite/gdb.base/code_elim.exp
index e66382fdbd9..9c4e0ff4022 100644
--- a/gdb/testsuite/gdb.base/code_elim.exp
+++ b/gdb/testsuite/gdb.base/code_elim.exp
@@ -93,11 +93,10 @@  with_test_prefix "single psymtabs" {
 # Same thing for symtabs
 
 gdb_exit
-global GDBFLAGS
-set saved_gdbflags $GDBFLAGS
-set GDBFLAGS "$GDBFLAGS --readnow $binfile1"
-gdb_start
-set GDBFLAGS $saved_gdbflags
+save_vars {GDBFLAGS} {
+    append GDBFLAGS " --readnow $binfile1"
+    gdb_start
+}
 
 with_test_prefix "single symtabs" {
     test_eliminated_var my_global_symbol
diff --git a/gdb/testsuite/gdb.base/paginate-execution-startup.exp b/gdb/testsuite/gdb.base/paginate-execution-startup.exp
index 3f0e74e5efa..66fb8965b4f 100644
--- a/gdb/testsuite/gdb.base/paginate-execution-startup.exp
+++ b/gdb/testsuite/gdb.base/paginate-execution-startup.exp
@@ -27,9 +27,6 @@  if [is_remote host] {
   set file_arg [remote_download host $file_arg]
 }
 
-global GDBFLAGS
-set saved_gdbflags $GDBFLAGS
-
 # Returns true if the board can 'gdb -ex "start"', false otherwise.
 
 proc probe_can_run_cmdline  {} {
@@ -172,7 +169,8 @@  proc test_fg_execution_pagination_cancel { how } {
     }
 }
 
-save_vars { INTERNAL_GDBFLAGS } {
+save_vars { GDBFLAGS INTERNAL_GDBFLAGS } {
+    set saved_gdbflags $GDBFLAGS
     set INTERNAL_GDBFLAGS [string map {"-q" ""} $INTERNAL_GDBFLAGS]
 
     if {[probe_can_run_cmdline] > 0} {
@@ -183,5 +181,3 @@  save_vars { INTERNAL_GDBFLAGS } {
 	test_fg_execution_pagination_cancel "quit"
     }
 }
-
-set GDBFLAGS $saved_gdbflags
diff --git a/gdb/testsuite/gdb.dwarf2/count.exp b/gdb/testsuite/gdb.dwarf2/count.exp
index 99c52d231ae..02aa9cd645f 100644
--- a/gdb/testsuite/gdb.dwarf2/count.exp
+++ b/gdb/testsuite/gdb.dwarf2/count.exp
@@ -138,11 +138,10 @@  if { [gdb_compile [list ${binfile}1.o ${binfile}2.o] \
     return -1
 }
 
-global GDBFLAGS
-set saved_gdbflags $GDBFLAGS
-set GDBFLAGS [concat $GDBFLAGS " -readnow"]
-clean_restart ${testfile}
-set GDBFLAGS $saved_gdbflags
+save_vars {GDBFLAGS} {
+    append GDBFLAGS " -readnow"
+    clean_restart ${testfile}
+}
 
 if ![runto_main] {
     return -1
diff --git a/gdb/testsuite/gdb.dwarf2/dw2-icycle.exp b/gdb/testsuite/gdb.dwarf2/dw2-icycle.exp
index 9c004562d69..b7a68b8f367 100644
--- a/gdb/testsuite/gdb.dwarf2/dw2-icycle.exp
+++ b/gdb/testsuite/gdb.dwarf2/dw2-icycle.exp
@@ -33,10 +33,10 @@  if { [build_executable "failed to prepare" ${testfile} \
 # start the debugger with -readnow.  This force expansion as soon as
 # the objfile is loaded.
 
-set saved_gdbflags $GDBFLAGS
-set GDBFLAGS "$GDBFLAGS -readnow"
-clean_restart ${testfile}
-set GDBFLAGS $saved_gdbflags
+save_vars {GDBFLAGS} {
+    append GDBFLAGS " -readnow"
+    clean_restart ${testfile}
+}
 
 # And just to be sure that the debugger did not crash after having
 # expanded our symbols, do a life-check.
diff --git a/gdb/testsuite/gdb.dwarf2/dw2-var-zero-addr.exp b/gdb/testsuite/gdb.dwarf2/dw2-var-zero-addr.exp
index 2a5aa3ee0c1..614edda9fcc 100644
--- a/gdb/testsuite/gdb.dwarf2/dw2-var-zero-addr.exp
+++ b/gdb/testsuite/gdb.dwarf2/dw2-var-zero-addr.exp
@@ -32,10 +32,10 @@  proc test { } {
 
 test
 
-set saved_gdbflags $GDBFLAGS
-set GDBFLAGS "$GDBFLAGS --readnow"
-clean_restart ${binfile}
-set GDBFLAGS $saved_gdbflags
+save_vars {GDBFLAGS} {
+    append GDBFLAGS " --readnow"
+    clean_restart ${binfile}
+}
 
 with_test_prefix "readnow" {
     test
diff --git a/gdb/testsuite/gdb.dwarf2/dw4-sig-type-unused.exp b/gdb/testsuite/gdb.dwarf2/dw4-sig-type-unused.exp
index 63c834b8846..a4c512a7cb3 100644
--- a/gdb/testsuite/gdb.dwarf2/dw4-sig-type-unused.exp
+++ b/gdb/testsuite/gdb.dwarf2/dw4-sig-type-unused.exp
@@ -24,9 +24,9 @@  if { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" object {}] != ""
     return -1
 }
 
-set saved_gdbflags $GDBFLAGS
-set GDBFLAGS "$GDBFLAGS --readnow"
-clean_restart $executable
-set GDBFLAGS $saved_gdbflags
+save_vars {GDBFLAGS} {
+    append GDBFLAGS " --readnow"
+    clean_restart $executable
+}
 
 gdb_test "p 1" " = 1" "alive"
diff --git a/gdb/testsuite/gdb.dwarf2/implptrconst.exp b/gdb/testsuite/gdb.dwarf2/implptrconst.exp
index 796b8b70307..0004b51dc25 100644
--- a/gdb/testsuite/gdb.dwarf2/implptrconst.exp
+++ b/gdb/testsuite/gdb.dwarf2/implptrconst.exp
@@ -90,10 +90,10 @@  if { [build_executable ${testfile}.exp ${testfile} \
 
 # We need --readnow because otherwise we never read in the CU we
 # created above.
-set saved_gdbflags $GDBFLAGS
-set GDBFLAGS "$GDBFLAGS -readnow"
-clean_restart ${testfile}
-set GDBFLAGS $saved_gdbflags
+save_vars {GDBFLAGS} {
+    append GDBFLAGS " -readnow"
+    clean_restart ${testfile}
+}
 
 if ![runto_main] {
     return -1
diff --git a/gdb/testsuite/gdb.dwarf2/implptrpiece.exp b/gdb/testsuite/gdb.dwarf2/implptrpiece.exp
index 83bdf991135..aa411edb5f1 100644
--- a/gdb/testsuite/gdb.dwarf2/implptrpiece.exp
+++ b/gdb/testsuite/gdb.dwarf2/implptrpiece.exp
@@ -108,10 +108,10 @@  if { [build_executable ${testfile}.exp ${testfile} \
 
 # We need --readnow because otherwise we never read in the CU we
 # created above.
-set saved_gdbflags $GDBFLAGS
-set GDBFLAGS "$GDBFLAGS -readnow"
-clean_restart ${testfile}
-set GDBFLAGS $saved_gdbflags
+save_vars {GDBFLAGS} {
+    append GDBFLAGS " -readnow"
+    clean_restart ${testfile}
+}
 
 if ![runto_main] {
     return -1
diff --git a/gdb/testsuite/gdb.dwarf2/nostaticblock.exp b/gdb/testsuite/gdb.dwarf2/nostaticblock.exp
index 46e2b4c4ca8..b026073d41f 100644
--- a/gdb/testsuite/gdb.dwarf2/nostaticblock.exp
+++ b/gdb/testsuite/gdb.dwarf2/nostaticblock.exp
@@ -38,9 +38,9 @@  if { [build_executable ${testfile}.exp ${testfile} \
     return -1
 }
 
-set saved_gdbflags $GDBFLAGS
-set GDBFLAGS "$GDBFLAGS --readnow"
-clean_restart $testfile
-set GDBFLAGS $saved_gdbflags
+save_vars {GDBFLAGS} {
+    append GDBFLAGS " --readnow"
+    clean_restart $testfile
+}
 
 gdb_test "p 1" " = 1" "alive"
diff --git a/gdb/testsuite/gdb.dwarf2/symtab-producer.exp b/gdb/testsuite/gdb.dwarf2/symtab-producer.exp
index 756b1d60bf2..7981e0117ab 100644
--- a/gdb/testsuite/gdb.dwarf2/symtab-producer.exp
+++ b/gdb/testsuite/gdb.dwarf2/symtab-producer.exp
@@ -73,17 +73,14 @@  Dwarf::assemble $asm_file {
 
 # We need --readnow because otherwise we never read in the CUs we
 # created above.
-set saved_gdbflags $GDBFLAGS
-set GDBFLAGS "$GDBFLAGS -readnow"
-
-if { [prepare_for_testing "failed to prepare" ${testfile} \
-	  [list $srcfile $asm_file] {nodebug}] } {
-    set GDBFLAGS $saved_gdbflags
-    return -1
+save_vars {GDBFLAGS} {
+    append GDBFLAGS " -readnow"
+    if { [prepare_for_testing "failed to prepare" ${testfile} \
+	      [list $srcfile $asm_file] {nodebug}] } {
+	return -1
+    }
 }
 
-set GDBFLAGS $saved_gdbflags
-
 gdb_py_test_silent_cmd "python with_producer = gdb.lookup_global_symbol(\"with_producer\")" \
     "get with_producer symbol" 0
 
diff --git a/gdb/testsuite/gdb.mi/mi-async.exp b/gdb/testsuite/gdb.mi/mi-async.exp
index fc138a6e9ea..fa527191d8b 100644
--- a/gdb/testsuite/gdb.mi/mi-async.exp
+++ b/gdb/testsuite/gdb.mi/mi-async.exp
@@ -29,10 +29,6 @@  if {![istarget *-linux*]} {
 # Check if start command is supported.
 require !use_gdb_stub
 
-# The plan is for async mode to become the default but toggle for now.
-set saved_gdbflags $GDBFLAGS
-set GDBFLAGS [concat $GDBFLAGS " -ex \"set mi-async on\""]
-
 load_lib mi-support.exp
 
 standard_testfile basics.c
@@ -42,8 +38,11 @@  if  { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {deb
      return -1
 }
 
-if {[mi_clean_restart $binfile]} {
-    return
+save_vars {GDBFLAGS} {
+    append GDBFLAGS " -ex \"set mi-async on\""
+    if {[mi_clean_restart $binfile]} {
+	return
+    }
 }
 
 # mi_gdb_test cannot be used for asynchronous commands because there are
@@ -75,5 +74,3 @@  proc linux_async_tests {} {
 linux_async_tests
 
 mi_gdb_exit
-
-set GDBFLAGS $saved_gdbflags
diff --git a/gdb/testsuite/gdb.python/py-prompt.exp b/gdb/testsuite/gdb.python/py-prompt.exp
index b37d2fbd3eb..7d3cafb9539 100644
--- a/gdb/testsuite/gdb.python/py-prompt.exp
+++ b/gdb/testsuite/gdb.python/py-prompt.exp
@@ -27,11 +27,9 @@  if {[build_executable $testfile.exp $testfile $srcfile debug] == -1} {
     return -1
 }
 
-save_vars { INTERNAL_GDBFLAGS } {
+save_vars { GDBFLAGS INTERNAL_GDBFLAGS } {
     set INTERNAL_GDBFLAGS [string map {"-q" ""} $INTERNAL_GDBFLAGS]
 
-    global GDBFLAGS
-    set saved_gdbflags $GDBFLAGS
     set GDBFLAGS [concat $GDBFLAGS " -ex \"set height 0\""]
     set GDBFLAGS [concat $GDBFLAGS " -ex \"set width 0\""]
     set GDBFLAGS [concat $GDBFLAGS " -ex \"python p = list()\""]
@@ -74,7 +72,6 @@  save_vars { INTERNAL_GDBFLAGS } {
     }
 
     if {![can_spawn_for_attach]} {
-	set GDBFLAGS $saved_gdbflags
 	return 0
     }
 
@@ -117,6 +114,5 @@  save_vars { INTERNAL_GDBFLAGS } {
 	gdb_exit
     }
 
-    set GDBFLAGS $saved_gdbflags
     kill_wait_spawned_process $test_spawn_id
 }
diff --git a/gdb/testsuite/gdb.threads/fork-plus-threads.exp b/gdb/testsuite/gdb.threads/fork-plus-threads.exp
index 431ad4a8b7f..96e309151be 100644
--- a/gdb/testsuite/gdb.threads/fork-plus-threads.exp
+++ b/gdb/testsuite/gdb.threads/fork-plus-threads.exp
@@ -34,17 +34,14 @@  proc do_test { detach-on-fork } {
     global srcfile testfile
     global gdb_prompt
 
-    set saved_gdbflags $GDBFLAGS
-    set GDBFLAGS [concat $GDBFLAGS " -ex \"set non-stop on\""]
-
-    if {[prepare_for_testing "failed to prepare" \
-	     $testfile $srcfile {debug pthreads}] == -1} {
-	set GDBFLAGS $saved_gdbflags
-	return -1
+    save_vars {GDBFLAGS} {
+	append GDBFLAGS " -ex \"set non-stop on\""
+	if {[prepare_for_testing "failed to prepare" \
+		 $testfile $srcfile {debug pthreads}] == -1} {
+	    return -1
+	}
     }
 
-    set GDBFLAGS $saved_gdbflags
-
     if {![runto_main]} {
 	return 0
     }
diff --git a/gdb/testsuite/gdb.threads/forking-threads-plus-breakpoint.exp b/gdb/testsuite/gdb.threads/forking-threads-plus-breakpoint.exp
index 7f010045e29..d81bfc68ba4 100644
--- a/gdb/testsuite/gdb.threads/forking-threads-plus-breakpoint.exp
+++ b/gdb/testsuite/gdb.threads/forking-threads-plus-breakpoint.exp
@@ -72,10 +72,10 @@  proc do_test { cond_bp_target detach_on_fork displaced } {
     global linenum
     global is_remote_target
 
-    set saved_gdbflags $GDBFLAGS
-    set GDBFLAGS [concat $GDBFLAGS " -ex \"set non-stop on\""]
-    clean_restart $binfile
-    set GDBFLAGS $saved_gdbflags
+    save_vars {GDBFLAGS} {
+	append GDBFLAGS " -ex \"set non-stop on\""
+	clean_restart $binfile
+    }
 
     if {![runto_main]} {
 	return 0
diff --git a/gdb/testsuite/gdb.threads/process-dies-while-handling-bp.exp b/gdb/testsuite/gdb.threads/process-dies-while-handling-bp.exp
index a4c50d1c1f1..ccb99b172e1 100644
--- a/gdb/testsuite/gdb.threads/process-dies-while-handling-bp.exp
+++ b/gdb/testsuite/gdb.threads/process-dies-while-handling-bp.exp
@@ -40,10 +40,10 @@  proc do_test { non_stop cond_bp_target } {
     global binfile
     global linenum
 
-    set saved_gdbflags $GDBFLAGS
-    set GDBFLAGS [concat $GDBFLAGS " -ex \"set non-stop $non_stop\""]
-    clean_restart $binfile
-    set GDBFLAGS $saved_gdbflags
+    save_vars {GDBFLAGS} {
+	append GDBFLAGS " -ex \"set non-stop $non_stop\""
+	clean_restart $binfile
+    }
 
     if {![runto_main]} {
 	return 0
diff --git a/gdb/testsuite/lib/gdb.exp b/gdb/testsuite/lib/gdb.exp
index 9a906f0f349..6b82331bd15 100644
--- a/gdb/testsuite/lib/gdb.exp
+++ b/gdb/testsuite/lib/gdb.exp
@@ -6050,18 +6050,14 @@  proc gdb_spawn { } {
 proc gdb_spawn_with_cmdline_opts { cmdline_flags } {
     global GDBFLAGS
 
-    set saved_gdbflags $GDBFLAGS
+    save_vars {GDBFLAGS} {
+	if {$GDBFLAGS != ""} {
+	    append GDBFLAGS " "
+	}
+	append GDBFLAGS $cmdline_flags
 
-    if {$GDBFLAGS != ""} {
-	append GDBFLAGS " "
+	return [gdb_spawn]
     }
-    append GDBFLAGS $cmdline_flags
-
-    set res [gdb_spawn]
-
-    set GDBFLAGS $saved_gdbflags
-
-    return $res
 }
 
 # Start gdb running, wait for prompt, and disable the pagers.