[08/27] Use mi_clean_restart more

Message ID 20230125224614.1006886-9-tom@tromey.com
State Committed
Headers
Series Many small testsuite cleanups |

Commit Message

Tom Tromey Jan. 25, 2023, 10:45 p.m. UTC
  This changes a number of MI tests to use mi_clean_restart rather than
separate calls.  This reduces the number of lines, which is nice, and
also provides a nicer model to copy for future tests.
---
 gdb/testsuite/gdb.ada/mi_ex_cond.exp          |  7 +------
 gdb/testsuite/gdb.ada/mi_task_arg.exp         |  7 +------
 gdb/testsuite/gdb.ada/mi_task_info.exp        |  7 +------
 .../gdb.dwarf2/dw2-opt-structptr.exp          | 11 ++---------
 .../gdb.mi/dw2-ref-missing-frame.exp          | 12 ++----------
 gdb/testsuite/gdb.mi/gdb680.exp               |  3 +--
 .../gdb.mi/list-thread-groups-no-inferior.exp |  3 +--
 gdb/testsuite/gdb.mi/mi-async-run.exp         |  4 +---
 gdb/testsuite/gdb.mi/mi-async.exp             | 11 +++--------
 gdb/testsuite/gdb.mi/mi-break-qualified.exp   |  8 +-------
 .../gdb.mi/mi-breakpoint-changed.exp          |  5 +----
 .../gdb.mi/mi-catch-cpp-exceptions.exp        |  6 +-----
 gdb/testsuite/gdb.mi/mi-cmd-error.exp         |  3 +--
 .../gdb.mi/mi-condbreak-call-thr-state.exp    | 12 +++---------
 gdb/testsuite/gdb.mi/mi-console.exp           | 11 ++++-------
 gdb/testsuite/gdb.mi/mi-dprintf-pending.exp   |  9 ++++-----
 gdb/testsuite/gdb.mi/mi-editing.exp           |  3 +--
 gdb/testsuite/gdb.mi/mi-eval.exp              | 12 +++---------
 gdb/testsuite/gdb.mi/mi-file-transfer.exp     |  6 +-----
 gdb/testsuite/gdb.mi/mi-file.exp              | 11 +++--------
 gdb/testsuite/gdb.mi/mi-fullname-deleted.exp  | 10 +++-------
 gdb/testsuite/gdb.mi/mi-hack-cli.exp          |  3 +--
 gdb/testsuite/gdb.mi/mi-i-cmd.exp             |  3 +--
 gdb/testsuite/gdb.mi/mi-info-os.exp           | 11 ++++-------
 .../gdb.mi/mi-inheritance-syntax-error.exp    | 10 ++++------
 gdb/testsuite/gdb.mi/mi-language.exp          |  3 +--
 gdb/testsuite/gdb.mi/mi-memory-changed.exp    |  4 +---
 gdb/testsuite/gdb.mi/mi-pending.exp           |  7 +++----
 gdb/testsuite/gdb.mi/mi-pthreads.exp          | 10 +++-------
 gdb/testsuite/gdb.mi/mi-reg-undefined.exp     | 12 +++---------
 gdb/testsuite/gdb.mi/mi-return.exp            | 13 +++----------
 gdb/testsuite/gdb.mi/mi-simplerun.exp         | 12 +++---------
 gdb/testsuite/gdb.mi/mi-solib.exp             | 11 +++--------
 gdb/testsuite/gdb.mi/mi-stack.exp             | 12 +++---------
 gdb/testsuite/gdb.mi/mi-start.exp             | 12 +++---------
 gdb/testsuite/gdb.mi/mi-threads-interrupt.exp |  6 +-----
 gdb/testsuite/gdb.mi/mi-undefined-cmd.exp     |  4 +---
 gdb/testsuite/gdb.mi/mi-until.exp             | 12 +++---------
 gdb/testsuite/gdb.mi/mi-var-block.exp         | 11 +++--------
 gdb/testsuite/gdb.mi/mi-var-child-f.exp       | 10 +++-------
 gdb/testsuite/gdb.mi/mi-var-child.exp         | 11 +++--------
 gdb/testsuite/gdb.mi/mi-var-cmd.exp           | 12 +++---------
 gdb/testsuite/gdb.mi/mi-var-cp.exp            |  9 +++------
 gdb/testsuite/gdb.mi/mi-var-display.exp       | 11 +++--------
 gdb/testsuite/gdb.mi/mi-var-invalidate.exp    | 11 +++--------
 ...i-var-list-children-invalid-grandchild.exp | 11 +++--------
 gdb/testsuite/gdb.mi/mi-var-rtti.exp          |  9 +++------
 gdb/testsuite/gdb.mi/mi-vla-c99.exp           | 11 +++--------
 gdb/testsuite/gdb.mi/mi-vla-fortran.exp       | 11 +++--------
 gdb/testsuite/gdb.mi/mi-watch.exp             |  2 --
 .../gdb.mi/mi2-amd64-entry-value.exp          | 14 ++++----------
 gdb/testsuite/gdb.mi/mi2-cli-display.exp      | 10 +++-------
 gdb/testsuite/gdb.mi/mi2-prompt.exp           |  3 +--
 gdb/testsuite/gdb.mi/mi2-var-child.exp        | 11 +++--------
 .../gdb.python/py-framefilter-mi.exp          | 11 +++--------
 gdb/testsuite/gdb.python/py-mi-events.exp     | 12 +++++-------
 gdb/testsuite/gdb.python/py-mi-objfile.exp    | 11 ++++-------
 gdb/testsuite/gdb.python/py-mi.exp            | 16 ++++------------
 .../gdb.trace/mi-tracepoint-changed.exp       | 19 +++----------------
 gdb/testsuite/gdb.trace/mi-tsv-changed.exp    | 18 ++++--------------
 60 files changed, 147 insertions(+), 403 deletions(-)
  

Patch

diff --git a/gdb/testsuite/gdb.ada/mi_ex_cond.exp b/gdb/testsuite/gdb.ada/mi_ex_cond.exp
index 6fba4da9e80..f41b633a822 100644
--- a/gdb/testsuite/gdb.ada/mi_ex_cond.exp
+++ b/gdb/testsuite/gdb.ada/mi_ex_cond.exp
@@ -57,15 +57,10 @@  gdb_test_multiple "catch exception" $msg {
 load_lib mi-support.exp
 set MIFLAGS "-i=mi"
 
-gdb_exit
-if [mi_gdb_start] {
+if {[mi_clean_restart $binfile]} {
     return
 }
 
-mi_delete_breakpoints
-mi_gdb_reinitialize_dir $srcdir/$subdir
-mi_gdb_load ${binfile}
-
 # And finally, the meat of the testcase... Insert an Ada exception
 # catchpoint that uses both conditions and exception name.
 
diff --git a/gdb/testsuite/gdb.ada/mi_task_arg.exp b/gdb/testsuite/gdb.ada/mi_task_arg.exp
index 771e9345c40..1d84e432ad8 100644
--- a/gdb/testsuite/gdb.ada/mi_task_arg.exp
+++ b/gdb/testsuite/gdb.ada/mi_task_arg.exp
@@ -26,15 +26,10 @@  if {[gdb_compile_ada "${srcfile}" "${binfile}" executable [list debug additional
 load_lib mi-support.exp
 set MIFLAGS "-i=mi"
 
-gdb_exit
-if [mi_gdb_start] {
+if {[mi_clean_restart $binfile]} {
     return
 }
 
-mi_delete_breakpoints
-mi_gdb_reinitialize_dir $srcdir/$subdir
-mi_gdb_load ${binfile}
-
 # This test won't work properly if system debuginfo is installed.
 mi_gdb_test "-gdb-set debug-file-directory \"\"" ".*"
 
diff --git a/gdb/testsuite/gdb.ada/mi_task_info.exp b/gdb/testsuite/gdb.ada/mi_task_info.exp
index 7701a3ef6d6..6e23f018e4c 100644
--- a/gdb/testsuite/gdb.ada/mi_task_info.exp
+++ b/gdb/testsuite/gdb.ada/mi_task_info.exp
@@ -26,15 +26,10 @@  if {[gdb_compile_ada "${srcfile}" "${binfile}" executable [list debug additional
 load_lib mi-support.exp
 set MIFLAGS "-i=mi"
 
-gdb_exit
-if [mi_gdb_start] {
+if {[mi_clean_restart $binfile]} {
     return
 }
 
-mi_delete_breakpoints
-mi_gdb_reinitialize_dir $srcdir/$subdir
-mi_gdb_load ${binfile}
-
 ####################################
 # 1. Try catching all exceptions.  #
 ####################################
diff --git a/gdb/testsuite/gdb.dwarf2/dw2-opt-structptr.exp b/gdb/testsuite/gdb.dwarf2/dw2-opt-structptr.exp
index ae6c58f2434..0d54947d36c 100644
--- a/gdb/testsuite/gdb.dwarf2/dw2-opt-structptr.exp
+++ b/gdb/testsuite/gdb.dwarf2/dw2-opt-structptr.exp
@@ -168,20 +168,13 @@  proc do_mi_test {} {
     set MIFLAGS "-i=mi"
 
     global mi_gdb_prompt
-    global srcdir
-    global subdir
     global binfile
     
     with_test_prefix "mi" {
-	gdb_exit
-	if {[mi_gdb_start]} {
-	    return -1
+	if {[mi_clean_restart $binfile]} {
+	    return
 	}
 	
-	mi_delete_breakpoints
-	mi_gdb_reinitialize_dir $srcdir/$subdir
-	mi_gdb_load $binfile
-	
 	# This causes GDB to dereference a pointer-to-structure when doing
 	# -var-create.
 	mi_gdb_test "-gdb-set print object on" ".*" "set print object on"
diff --git a/gdb/testsuite/gdb.mi/dw2-ref-missing-frame.exp b/gdb/testsuite/gdb.mi/dw2-ref-missing-frame.exp
index 336290ee1e1..692e0f1eeaa 100644
--- a/gdb/testsuite/gdb.mi/dw2-ref-missing-frame.exp
+++ b/gdb/testsuite/gdb.mi/dw2-ref-missing-frame.exp
@@ -32,14 +32,10 @@  if { [gdb_compile "${srcdir}/${subdir}/${srcfile}" $objsfile object {}] != ""
     return -1
 }
 
-if [mi_gdb_start] {
+if {[mi_clean_restart $binfile]} {
     return
 }
 
-mi_delete_breakpoints
-mi_gdb_reinitialize_dir $srcdir/$subdir
-mi_gdb_load ${binfile}
-
 if [mi_runto func_nofb_marker] {
     # First try referencing DW_AT_frame_base which is not defined.
     mi_gdb_test "300-stack-list-locals --thread 1 --frame 1 --all-values" \
@@ -48,13 +44,9 @@  if [mi_runto func_nofb_marker] {
 }
 
 # GDB could have crashed.
-mi_gdb_exit
-if [mi_gdb_start] {
+if {[mi_clean_restart $binfile]} {
     return
 }
-mi_delete_breakpoints
-mi_gdb_reinitialize_dir $srcdir/$subdir
-mi_gdb_load ${binfile}
 
 # And now try referencing DW_AT_frame_base defined using a self-reference
 # (DW_OP_fbreg).
diff --git a/gdb/testsuite/gdb.mi/gdb680.exp b/gdb/testsuite/gdb.mi/gdb680.exp
index fb9d865f6f4..a5d321d9368 100644
--- a/gdb/testsuite/gdb.mi/gdb680.exp
+++ b/gdb/testsuite/gdb.mi/gdb680.exp
@@ -20,8 +20,7 @@ 
 load_lib mi-support.exp
 set MIFLAGS "-i=mi"
 
-gdb_exit
-if [mi_gdb_start] {
+if {[mi_clean_restart]} {
     return
 }
 
diff --git a/gdb/testsuite/gdb.mi/list-thread-groups-no-inferior.exp b/gdb/testsuite/gdb.mi/list-thread-groups-no-inferior.exp
index 2e05f1d8f92..feb2915f8c5 100644
--- a/gdb/testsuite/gdb.mi/list-thread-groups-no-inferior.exp
+++ b/gdb/testsuite/gdb.mi/list-thread-groups-no-inferior.exp
@@ -21,8 +21,7 @@ 
 load_lib mi-support.exp
 set MIFLAGS "-i=mi"
 
-gdb_exit
-if [mi_gdb_start] {
+if {[mi_clean_restart]} {
     return
 }
 
diff --git a/gdb/testsuite/gdb.mi/mi-async-run.exp b/gdb/testsuite/gdb.mi/mi-async-run.exp
index 0267b546ef7..cd668b161f8 100644
--- a/gdb/testsuite/gdb.mi/mi-async-run.exp
+++ b/gdb/testsuite/gdb.mi/mi-async-run.exp
@@ -35,12 +35,10 @@  proc test_async_run {} {
 
 	set GDBFLAGS [concat $GDBFLAGS " -ex \"set mi-async on\""]
 
-	gdb_exit
-	if [mi_gdb_start] {
+	if {[mi_clean_restart $binfile]} {
 	    return
 	}
 
-	mi_gdb_load ${binfile}
 	mi_run_cmd
 	mi_gdb_test "123-exec-interrupt --all" "123\\^done" "send interrupt command"
 	mi_expect_interrupt "expect interrupt"
diff --git a/gdb/testsuite/gdb.mi/mi-async.exp b/gdb/testsuite/gdb.mi/mi-async.exp
index f970fc772ca..4bc82d5f55d 100644
--- a/gdb/testsuite/gdb.mi/mi-async.exp
+++ b/gdb/testsuite/gdb.mi/mi-async.exp
@@ -35,11 +35,6 @@  set GDBFLAGS [concat $GDBFLAGS " -ex \"set mi-async on\""]
 
 load_lib mi-support.exp
 
-gdb_exit
-if [mi_gdb_start] {
-    return
-}
-
 standard_testfile basics.c
 
 if  { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {debug}] != "" } {
@@ -47,9 +42,9 @@  if  { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {deb
      return -1
 }
 
-mi_delete_breakpoints
-mi_gdb_reinitialize_dir $srcdir/$subdir
-mi_gdb_load ${binfile}
+if {[mi_clean_restart $binfile]} {
+    return
+}
 
 # mi_gdb_test cannot be used for asynchronous commands because there are
 # two prompts involved and this can lead to a race condition.
diff --git a/gdb/testsuite/gdb.mi/mi-break-qualified.exp b/gdb/testsuite/gdb.mi/mi-break-qualified.exp
index 64bb9e4104e..3f6acc566b1 100644
--- a/gdb/testsuite/gdb.mi/mi-break-qualified.exp
+++ b/gdb/testsuite/gdb.mi/mi-break-qualified.exp
@@ -93,14 +93,8 @@  proc test_break_qualified {} {
 	    "delete temp breakpoints"
 }
 
-mi_gdb_exit
-
-if [mi_gdb_start] {
+if {[mi_clean_restart $binfile]} {
     return
 }
 
-mi_delete_breakpoints
-mi_gdb_reinitialize_dir $srcdir/$subdir
-mi_gdb_load ${binfile}
-
 test_break_qualified
diff --git a/gdb/testsuite/gdb.mi/mi-breakpoint-changed.exp b/gdb/testsuite/gdb.mi/mi-breakpoint-changed.exp
index 5cc2fe840fb..c0d9add9646 100644
--- a/gdb/testsuite/gdb.mi/mi-breakpoint-changed.exp
+++ b/gdb/testsuite/gdb.mi/mi-breakpoint-changed.exp
@@ -165,12 +165,9 @@  proc test_pending_resolved { } {
 	global lib_sl1 lib_sl2
 	global mi_gdb_prompt
 
-	gdb_exit
-	if [mi_gdb_start] {
+	if {[mi_clean_restart $binfile]} {
 	    return
 	}
-	mi_gdb_reinitialize_dir $srcdir/$subdir
-	mi_gdb_load ${binfile}
 	mi_load_shlibs $lib_sl1 $lib_sl2
 
 	# Create a pending breakpoint on pendfunc1
diff --git a/gdb/testsuite/gdb.mi/mi-catch-cpp-exceptions.exp b/gdb/testsuite/gdb.mi/mi-catch-cpp-exceptions.exp
index ac2d8e18828..595ae929e9a 100644
--- a/gdb/testsuite/gdb.mi/mi-catch-cpp-exceptions.exp
+++ b/gdb/testsuite/gdb.mi/mi-catch-cpp-exceptions.exp
@@ -46,14 +46,10 @@  proc restart_for_test {} {
     global srcdir subdir binfile srcfile
     global main_lineno
 
-    if {[mi_gdb_start]} {
+    if {[mi_clean_restart $binfile]} {
 	return
     }
 
-    mi_delete_breakpoints
-    mi_gdb_reinitialize_dir $srcdir/$subdir
-    mi_gdb_load ${binfile}
-
     mi_runto_main
 
     mi_create_breakpoint \
diff --git a/gdb/testsuite/gdb.mi/mi-cmd-error.exp b/gdb/testsuite/gdb.mi/mi-cmd-error.exp
index 5a3b84a58a6..a43d82495c6 100644
--- a/gdb/testsuite/gdb.mi/mi-cmd-error.exp
+++ b/gdb/testsuite/gdb.mi/mi-cmd-error.exp
@@ -25,8 +25,7 @@ 
 load_lib mi-support.exp
 set MIFLAGS "-i=mi"
 
-gdb_exit
-if [mi_gdb_start] {
+if {[mi_clean_restart]} {
     return
 }
 
diff --git a/gdb/testsuite/gdb.mi/mi-condbreak-call-thr-state.exp b/gdb/testsuite/gdb.mi/mi-condbreak-call-thr-state.exp
index de0914dd0ed..8184cd82ece 100644
--- a/gdb/testsuite/gdb.mi/mi-condbreak-call-thr-state.exp
+++ b/gdb/testsuite/gdb.mi/mi-condbreak-call-thr-state.exp
@@ -40,11 +40,6 @@  proc test { variant } {
     global mi_gdb_prompt async
 
     with_test_prefix "$variant" {
-	gdb_exit
-	if [mi_gdb_start] {
-	    return
-	}
-
 	set options {debug}
 	if {$variant == "mt" } {
 	    lappend options "pthreads"
@@ -64,10 +59,9 @@  proc test { variant } {
 	    return -1
 	}
 
-	mi_delete_breakpoints
-	mi_gdb_reinitialize_dir $srcdir/$subdir
-	mi_gdb_reinitialize_dir $srcdir/$subdir
-	mi_gdb_load ${binfile}
+	if {[mi_clean_restart $binfile]} {
+	    return
+	}
 
 	mi_runto test
 
diff --git a/gdb/testsuite/gdb.mi/mi-console.exp b/gdb/testsuite/gdb.mi/mi-console.exp
index 738960739f7..c39e05cc3d6 100644
--- a/gdb/testsuite/gdb.mi/mi-console.exp
+++ b/gdb/testsuite/gdb.mi/mi-console.exp
@@ -46,11 +46,6 @@  proc semihosted_string { string } {
   return [join $semihosted_list ""]
 }
 
-gdb_exit
-if [mi_gdb_start separate-inferior-tty] {
-    return
-}
-
 standard_testfile
 
 if  { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {debug}] != "" } {
@@ -58,8 +53,10 @@  if  { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {deb
      return -1
 }
 
-mi_gdb_reinitialize_dir $srcdir/$subdir
-mi_gdb_load ${binfile}
+if {[mi_clean_restart $binfile]} {
+    return
+}
+
 mi_runto_main
 
 # The output we get from the target depends on how it is hosted.  If
diff --git a/gdb/testsuite/gdb.mi/mi-dprintf-pending.exp b/gdb/testsuite/gdb.mi/mi-dprintf-pending.exp
index 66a30426a8b..358d929f1bb 100644
--- a/gdb/testsuite/gdb.mi/mi-dprintf-pending.exp
+++ b/gdb/testsuite/gdb.mi/mi-dprintf-pending.exp
@@ -39,11 +39,10 @@  if {[gdb_compile $srcdir/$subdir/$srcfile $binfile executable $exec_opts] != ""}
     return -1
 }
 
-# Start with a fresh gdb.
-gdb_exit
-mi_gdb_start
-mi_gdb_reinitialize_dir $srcdir/$subdir
-mi_gdb_load ${binfile}
+if {[mi_clean_restart $binfile]} {
+    return
+}
+
 mi_load_shlibs $lib_sl1
 
 set bp_location1 [gdb_get_line_number "set breakpoint 1 here"]
diff --git a/gdb/testsuite/gdb.mi/mi-editing.exp b/gdb/testsuite/gdb.mi/mi-editing.exp
index 35ed2af8ad4..2fb65ff9a9d 100644
--- a/gdb/testsuite/gdb.mi/mi-editing.exp
+++ b/gdb/testsuite/gdb.mi/mi-editing.exp
@@ -23,8 +23,7 @@ 
 load_lib mi-support.exp
 set MIFLAGS "-i=mi"
 
-gdb_exit
-if {[mi_gdb_start]} {
+if {[mi_clean_restart]} {
     return
 }
 
diff --git a/gdb/testsuite/gdb.mi/mi-eval.exp b/gdb/testsuite/gdb.mi/mi-eval.exp
index 1611b15c8f2..74ff887ef9c 100644
--- a/gdb/testsuite/gdb.mi/mi-eval.exp
+++ b/gdb/testsuite/gdb.mi/mi-eval.exp
@@ -25,11 +25,6 @@ 
 load_lib mi-support.exp
 set MIFLAGS "-i=mi"
 
-gdb_exit
-if [mi_gdb_start] {
-    return
-}
-
 standard_testfile basics.c
 
 if  { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {debug}] != "" } {
@@ -37,10 +32,9 @@  if  { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {deb
      return -1
 }
 
-mi_delete_breakpoints
-mi_gdb_reinitialize_dir $srcdir/$subdir
-mi_gdb_reinitialize_dir $srcdir/$subdir
-mi_gdb_load ${binfile}
+if {[mi_clean_restart $binfile]} {
+    return
+}
 
 set line_callee4_head [gdb_get_line_number "callee4 ("]
 set line_callee4_body [expr $line_callee4_head + 2]
diff --git a/gdb/testsuite/gdb.mi/mi-file-transfer.exp b/gdb/testsuite/gdb.mi/mi-file-transfer.exp
index 726504c46d8..5aacab90bce 100644
--- a/gdb/testsuite/gdb.mi/mi-file-transfer.exp
+++ b/gdb/testsuite/gdb.mi/mi-file-transfer.exp
@@ -29,13 +29,9 @@  if  { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {deb
     return -1
 }
 
-gdb_exit
-if [mi_gdb_start] {
+if {[mi_clean_restart $binfile]} {
     return
 }
-mi_delete_breakpoints
-mi_gdb_reinitialize_dir $srcdir/$subdir
-mi_gdb_file_cmd ${binfile}
 
 proc mi_gdbserver_run { } {
     mi_gdb_test "kill" ".*" ""
diff --git a/gdb/testsuite/gdb.mi/mi-file.exp b/gdb/testsuite/gdb.mi/mi-file.exp
index 811744e556f..b14b15a9165 100644
--- a/gdb/testsuite/gdb.mi/mi-file.exp
+++ b/gdb/testsuite/gdb.mi/mi-file.exp
@@ -18,11 +18,6 @@ 
 load_lib mi-support.exp
 set MIFLAGS "-i=mi"
 
-gdb_exit
-if [mi_gdb_start] {
-    return
-}
-
 standard_testfile basics.c
 
 if  { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {debug}] != "" } {
@@ -30,9 +25,9 @@  if  { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {deb
      return -1
 }
 
-mi_delete_breakpoints
-mi_gdb_reinitialize_dir $srcdir/$subdir
-mi_gdb_file_cmd ${binfile}
+if {[mi_clean_restart $binfile]} {
+    return
+}
 
 proc test_file_list_exec_source_file {} {
     global srcfile
diff --git a/gdb/testsuite/gdb.mi/mi-fullname-deleted.exp b/gdb/testsuite/gdb.mi/mi-fullname-deleted.exp
index b41cc7fa510..f898e556449 100644
--- a/gdb/testsuite/gdb.mi/mi-fullname-deleted.exp
+++ b/gdb/testsuite/gdb.mi/mi-fullname-deleted.exp
@@ -16,11 +16,6 @@ 
 load_lib mi-support.exp
 set MIFLAGS "-i=mi"
 
-gdb_exit
-if [mi_gdb_start] {
-    return
-}
-
 standard_testfile
 set srcfileabs [standard_output_file $srcfile]
 
@@ -63,8 +58,9 @@  if { [gdb_compile "$srcfileabs" "${binfile}" executable {debug}] != "" } {
 
 file delete -- $srcfileabs
 
-mi_gdb_reinitialize_dir $srcdir/$subdir
-mi_gdb_load ${binfile}
+if {[mi_clean_restart $binfile]} {
+    return
+}
 
 mi_gdb_test "-interpreter-exec console \"set substitute-path ${initdir} ${initdir}subst\"" {\^done} "set substitute-path"
 
diff --git a/gdb/testsuite/gdb.mi/mi-hack-cli.exp b/gdb/testsuite/gdb.mi/mi-hack-cli.exp
index 3013d87462a..f39923ba60b 100644
--- a/gdb/testsuite/gdb.mi/mi-hack-cli.exp
+++ b/gdb/testsuite/gdb.mi/mi-hack-cli.exp
@@ -19,8 +19,7 @@ 
 load_lib mi-support.exp
 set MIFLAGS "-i=mi"
 
-gdb_exit
-if [mi_gdb_start] {
+if {[mi_clean_restart]} {
     return
 }
 
diff --git a/gdb/testsuite/gdb.mi/mi-i-cmd.exp b/gdb/testsuite/gdb.mi/mi-i-cmd.exp
index 80ef266dd3a..e64f9006f3c 100644
--- a/gdb/testsuite/gdb.mi/mi-i-cmd.exp
+++ b/gdb/testsuite/gdb.mi/mi-i-cmd.exp
@@ -16,8 +16,7 @@ 
 load_lib mi-support.exp
 set MIFLAGS "-i=mi"
 
-gdb_exit
-if [mi_gdb_start] {
+if {[mi_clean_restart]} {
     return
 }
 
diff --git a/gdb/testsuite/gdb.mi/mi-info-os.exp b/gdb/testsuite/gdb.mi/mi-info-os.exp
index 2f243a7aac0..d71ad4c8192 100644
--- a/gdb/testsuite/gdb.mi/mi-info-os.exp
+++ b/gdb/testsuite/gdb.mi/mi-info-os.exp
@@ -25,20 +25,17 @@  if {![istarget *-*-linux*]} {
 # Support for XML-output is needed to run this test.
 require allow_xml_test
 
-gdb_exit
-if [mi_gdb_start] {
-    return
-}
-
 standard_testfile basics.c
 
 if [build_executable "Failed to build $testfile" $testfile $srcfile \
 	debug] {
     return -1;
 }
-if {[mi_gdb_load $binfile] < 0} {
-    return -1
+
+if {[mi_clean_restart $binfile]} {
+    return
 }
+
 # When testing a cross configuration, we need to be sure to first
 # connect to the target.  If we didn't do that, GDB would try running
 # the command against the default run target.  The usual way to do
diff --git a/gdb/testsuite/gdb.mi/mi-inheritance-syntax-error.exp b/gdb/testsuite/gdb.mi/mi-inheritance-syntax-error.exp
index d3523b07d00..90bb18257af 100644
--- a/gdb/testsuite/gdb.mi/mi-inheritance-syntax-error.exp
+++ b/gdb/testsuite/gdb.mi/mi-inheritance-syntax-error.exp
@@ -18,11 +18,6 @@  require allow_cplus_tests
 load_lib mi-support.exp
 set MIFLAGS "-i=mi"
 
-gdb_exit
-if [mi_gdb_start] {
-    return
-}
-
 standard_testfile .cc
 
 if {[gdb_compile $srcdir/$subdir/$srcfile $binfile executable {debug c++}] != ""} {
@@ -30,10 +25,13 @@  if {[gdb_compile $srcdir/$subdir/$srcfile $binfile executable {debug c++}] != ""
   return -1
 }
 
+if {[mi_clean_restart $binfile]} {
+    return
+}
+
 # Test that children of derived classes are given the proper
 # path by -var-info-path-expression
 
-mi_gdb_load ${binfile}
 mi_runto C::testLocation
 
 mi_create_varobj "var1" "this" "create var for THIS"
diff --git a/gdb/testsuite/gdb.mi/mi-language.exp b/gdb/testsuite/gdb.mi/mi-language.exp
index 4e8dea3995c..41a6c22d6f8 100644
--- a/gdb/testsuite/gdb.mi/mi-language.exp
+++ b/gdb/testsuite/gdb.mi/mi-language.exp
@@ -16,8 +16,7 @@ 
 load_lib mi-support.exp
 set MIFLAGS "-i=mi"
 
-gdb_exit
-if [mi_gdb_start] {
+if {[mi_clean_restart]} {
     return
 }
 
diff --git a/gdb/testsuite/gdb.mi/mi-memory-changed.exp b/gdb/testsuite/gdb.mi/mi-memory-changed.exp
index 846415d6267..c56256802f6 100644
--- a/gdb/testsuite/gdb.mi/mi-memory-changed.exp
+++ b/gdb/testsuite/gdb.mi/mi-memory-changed.exp
@@ -22,11 +22,9 @@  if { [gdb_compile "$srcdir/$subdir/$srcfile" $binfile \
 
 load_lib mi-support.exp
 
-if [mi_gdb_start] {
+if {[mi_clean_restart $binfile]} {
     return
 }
-mi_gdb_reinitialize_dir $srcdir/$subdir
-mi_gdb_load ${binfile}
 
 mi_gdb_test "-break-insert -t ${srcfile}:[gdb_get_line_number "C = A + B"]" \
     "\\^done,bkpt=\{number=\"1\".*" \
diff --git a/gdb/testsuite/gdb.mi/mi-pending.exp b/gdb/testsuite/gdb.mi/mi-pending.exp
index 271908c9855..153efdf45ce 100644
--- a/gdb/testsuite/gdb.mi/mi-pending.exp
+++ b/gdb/testsuite/gdb.mi/mi-pending.exp
@@ -45,10 +45,9 @@  if { [gdb_compile_pthreads $srcdir/$subdir/$srcfile $binfile executable $exec_op
 }
 
 # Start with a fresh gdb.
-gdb_exit
-mi_gdb_start
-mi_gdb_reinitialize_dir $srcdir/$subdir
-mi_gdb_load ${binfile}
+if {[mi_clean_restart $binfile]} {
+    return
+}
 mi_load_shlibs $lib_sl1
 mi_load_shlibs $lib_sl2
 
diff --git a/gdb/testsuite/gdb.mi/mi-pthreads.exp b/gdb/testsuite/gdb.mi/mi-pthreads.exp
index fc410af99be..f8e701e7854 100644
--- a/gdb/testsuite/gdb.mi/mi-pthreads.exp
+++ b/gdb/testsuite/gdb.mi/mi-pthreads.exp
@@ -22,11 +22,6 @@ 
 load_lib mi-support.exp
 set MIFLAGS "-i=mi"
 
-gdb_exit
-if {[mi_gdb_start]} {
-    return
-}
-
 # This procedure tests the various thread commands in MI.
 proc check_mi_thread_command_set {} {
 
@@ -68,8 +63,9 @@  if {[gdb_compile_pthreads "$srcdir/$subdir/$srcfile" $binfile executable $option
     return -1
 }
 
-mi_gdb_reinitialize_dir $srcdir/$subdir
-mi_gdb_load $binfile
+if {[mi_clean_restart $binfile]} {
+    return
+}
 
 check_mi_thread_command_set
 
diff --git a/gdb/testsuite/gdb.mi/mi-reg-undefined.exp b/gdb/testsuite/gdb.mi/mi-reg-undefined.exp
index 94db94289e2..1b8386cb2f5 100644
--- a/gdb/testsuite/gdb.mi/mi-reg-undefined.exp
+++ b/gdb/testsuite/gdb.mi/mi-reg-undefined.exp
@@ -24,11 +24,6 @@  if {![istarget "x86_64-*-*"] || ![is_lp64_target]} {
     return 0
 }
 
-gdb_exit
-if [mi_gdb_start] {
-    return
-}
-
 standard_testfile .S
 
 if  { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {debug nopie}] != "" } {
@@ -36,10 +31,9 @@  if  { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {deb
      return -1
 }
 
-mi_delete_breakpoints
-mi_gdb_reinitialize_dir $srcdir/$subdir
-mi_gdb_reinitialize_dir $srcdir/$subdir
-mi_gdb_load ${binfile}
+if {[mi_clean_restart $binfile]} {
+    return
+}
 
 if ![mi_runto stop_frame] {
     perror "Failed to stop in stop_frame"
diff --git a/gdb/testsuite/gdb.mi/mi-return.exp b/gdb/testsuite/gdb.mi/mi-return.exp
index 6254711bdec..12185fb81f5 100644
--- a/gdb/testsuite/gdb.mi/mi-return.exp
+++ b/gdb/testsuite/gdb.mi/mi-return.exp
@@ -24,11 +24,6 @@ 
 load_lib mi-support.exp
 set MIFLAGS "-i=mi"
 
-gdb_exit
-if [mi_gdb_start] {
-    return
-}
-
 standard_testfile basics.c
 
 if  { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {debug}] != "" } {
@@ -36,11 +31,9 @@  if  { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {deb
      return -1
 }
 
-mi_delete_breakpoints
-mi_gdb_reinitialize_dir $srcdir/$subdir
-mi_gdb_reinitialize_dir $srcdir/$subdir
-mi_gdb_load ${binfile}
-
+if {[mi_clean_restart $binfile]} {
+    return
+}
 
 proc test_return_simple {} {
     global mi_gdb_prompt
diff --git a/gdb/testsuite/gdb.mi/mi-simplerun.exp b/gdb/testsuite/gdb.mi/mi-simplerun.exp
index 99b729d5df3..fec4f014cd3 100644
--- a/gdb/testsuite/gdb.mi/mi-simplerun.exp
+++ b/gdb/testsuite/gdb.mi/mi-simplerun.exp
@@ -27,11 +27,6 @@ 
 load_lib mi-support.exp
 set MIFLAGS "-i=mi"
 
-gdb_exit
-if [mi_gdb_start] {
-    return
-}
-
 standard_testfile basics.c
 
 if  { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {debug}] != "" } {
@@ -39,10 +34,9 @@  if  { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {deb
      return -1
 }
 
-mi_delete_breakpoints
-mi_gdb_reinitialize_dir $srcdir/$subdir
-mi_gdb_reinitialize_dir $srcdir/$subdir
-mi_gdb_load ${binfile}
+if {[mi_clean_restart $binfile]} {
+    return
+}
 
 proc test_breakpoints_creation_and_listing {} {
     global srcfile
diff --git a/gdb/testsuite/gdb.mi/mi-solib.exp b/gdb/testsuite/gdb.mi/mi-solib.exp
index 03637651d92..665759d4d53 100644
--- a/gdb/testsuite/gdb.mi/mi-solib.exp
+++ b/gdb/testsuite/gdb.mi/mi-solib.exp
@@ -18,11 +18,6 @@  set MIFLAGS "-i=mi2"
 
 require allow_shlib_tests
 
-gdb_exit
-if [mi_gdb_start] {
-    return
-}
-
 standard_testfile solib-main.c solib-lib.c
 
 set libname "solib-lib"
@@ -39,9 +34,9 @@  if { [gdb_compile_shlib ${srcfile_lib} ${binfile_lib} $lib_flags] != ""
   return -1
 }
 
-mi_delete_breakpoints
-mi_gdb_reinitialize_dir $srcdir/$subdir
-mi_gdb_load ${binfile}
+if {[mi_clean_restart $binfile]} {
+    return
+}
 
 mi_load_shlibs $binfile_lib
 
diff --git a/gdb/testsuite/gdb.mi/mi-stack.exp b/gdb/testsuite/gdb.mi/mi-stack.exp
index 3359a877a6a..633e090c13a 100644
--- a/gdb/testsuite/gdb.mi/mi-stack.exp
+++ b/gdb/testsuite/gdb.mi/mi-stack.exp
@@ -25,11 +25,6 @@ 
 load_lib mi-support.exp
 set MIFLAGS "-i=mi"
 
-gdb_exit
-if [mi_gdb_start] {
-    return
-}
-
 standard_testfile
 
 if  { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {debug}] != "" } {
@@ -37,10 +32,9 @@  if  { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {deb
      return -1
 }
 
-mi_delete_breakpoints
-mi_gdb_reinitialize_dir $srcdir/$subdir
-mi_gdb_reinitialize_dir $srcdir/$subdir
-mi_gdb_load ${binfile}
+if {[mi_clean_restart $binfile]} {
+    return
+}
 
 proc test_stack_frame_listing {} {
     global mi_gdb_prompt
diff --git a/gdb/testsuite/gdb.mi/mi-start.exp b/gdb/testsuite/gdb.mi/mi-start.exp
index b7e81a30486..89ce2dd25ea 100644
--- a/gdb/testsuite/gdb.mi/mi-start.exp
+++ b/gdb/testsuite/gdb.mi/mi-start.exp
@@ -21,11 +21,6 @@  set MIFLAGS "-i=mi"
 # then there is no point in running this testcase...
 require !use_gdb_stub
 
-gdb_exit
-if [mi_gdb_start] {
-    continue
-}
-
 standard_testfile mi-start.c
 
 if  { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {debug}] != "" } {
@@ -33,10 +28,9 @@  if  { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {deb
      return -1
 }
 
-mi_delete_breakpoints
-mi_gdb_reinitialize_dir $srcdir/$subdir
-mi_gdb_reinitialize_dir $srcdir/$subdir
-mi_gdb_load ${binfile}
+if {[mi_clean_restart $binfile]} {
+    return
+}
 
 # First, verify that the debugger correctly advertises support
 # for the --start option of the -exec-run command.
diff --git a/gdb/testsuite/gdb.mi/mi-threads-interrupt.exp b/gdb/testsuite/gdb.mi/mi-threads-interrupt.exp
index 2193f05f1e2..149940d6743 100644
--- a/gdb/testsuite/gdb.mi/mi-threads-interrupt.exp
+++ b/gdb/testsuite/gdb.mi/mi-threads-interrupt.exp
@@ -36,14 +36,10 @@  proc test_continue_interrupt { } {
   global binfile
   global async
 
-  gdb_exit
-  if {[mi_gdb_start]} {
+  if {[mi_clean_restart $binfile]} {
       return
   }
 
-  # Load the binary in gdb...
-  mi_gdb_load $binfile
-
   # ... and run it.
   #
   # Note this test relies on mi_runto deleting the breakpoint.
diff --git a/gdb/testsuite/gdb.mi/mi-undefined-cmd.exp b/gdb/testsuite/gdb.mi/mi-undefined-cmd.exp
index 998853cefb8..9b57c8237f8 100644
--- a/gdb/testsuite/gdb.mi/mi-undefined-cmd.exp
+++ b/gdb/testsuite/gdb.mi/mi-undefined-cmd.exp
@@ -16,12 +16,10 @@ 
 load_lib mi-support.exp
 set MIFLAGS "-i=mi"
 
-gdb_exit
-if [mi_gdb_start] {
+if {[mi_clean_restart]} {
     return
 }
 
-
 # First, verify that the debugger correctly advertises support
 # for the "undefined-command" error code...
 mi_gdb_test "-list-features" \
diff --git a/gdb/testsuite/gdb.mi/mi-until.exp b/gdb/testsuite/gdb.mi/mi-until.exp
index be87d2c5474..902fd8ae591 100644
--- a/gdb/testsuite/gdb.mi/mi-until.exp
+++ b/gdb/testsuite/gdb.mi/mi-until.exp
@@ -24,11 +24,6 @@ 
 load_lib mi-support.exp
 set MIFLAGS "-i=mi"
 
-gdb_exit
-if [mi_gdb_start] {
-    return
-}
-
 standard_testfile until.c
 
 if  { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {debug}] != "" } {
@@ -36,10 +31,9 @@  if  { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {deb
      return -1
 }
 
-mi_delete_breakpoints
-mi_gdb_reinitialize_dir $srcdir/$subdir
-mi_gdb_reinitialize_dir $srcdir/$subdir
-mi_gdb_load ${binfile}
+if {[mi_clean_restart $binfile]} {
+    return
+}
 
 proc test_running_to_foo {} {
     set line [gdb_get_line_number "in-loop"]
diff --git a/gdb/testsuite/gdb.mi/mi-var-block.exp b/gdb/testsuite/gdb.mi/mi-var-block.exp
index 3638dfd7ed5..e2e788a888c 100644
--- a/gdb/testsuite/gdb.mi/mi-var-block.exp
+++ b/gdb/testsuite/gdb.mi/mi-var-block.exp
@@ -22,11 +22,6 @@ 
 load_lib mi-support.exp
 set MIFLAGS "-i=mi"
 
-gdb_exit
-if [mi_gdb_start] {
-    return
-}
-
 standard_testfile var-cmd.c
 
 if  { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {debug}] != "" } {
@@ -34,9 +29,9 @@  if  { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {deb
     return -1
 }
 
-mi_delete_breakpoints
-mi_gdb_reinitialize_dir $srcdir/$subdir
-mi_gdb_load ${binfile}
+if {[mi_clean_restart $binfile]} {
+    return
+}
 
 mi_runto do_block_tests
 
diff --git a/gdb/testsuite/gdb.mi/mi-var-child-f.exp b/gdb/testsuite/gdb.mi/mi-var-child-f.exp
index 7723dbc574f..1b4e164e3ea 100644
--- a/gdb/testsuite/gdb.mi/mi-var-child-f.exp
+++ b/gdb/testsuite/gdb.mi/mi-var-child-f.exp
@@ -21,11 +21,6 @@  load_lib "fortran.exp"
 
 require allow_fortran_tests
 
-gdb_exit
-if [mi_gdb_start] {
-    return
-}
-
 standard_testfile array.f90
 
 if {[gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" \
@@ -33,8 +28,9 @@  if {[gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" \
     return -1
 }
 
-mi_gdb_reinitialize_dir $srcdir/$subdir
-mi_gdb_load ${binfile}
+if {[mi_clean_restart $binfile]} {
+    return
+}
 
 # Avoid shared lib symbols.
 mi_gdb_test "-gdb-set auto-solib-add off" "\\^done"
diff --git a/gdb/testsuite/gdb.mi/mi-var-child.exp b/gdb/testsuite/gdb.mi/mi-var-child.exp
index e64bde94959..3cfa0ce88ac 100644
--- a/gdb/testsuite/gdb.mi/mi-var-child.exp
+++ b/gdb/testsuite/gdb.mi/mi-var-child.exp
@@ -22,11 +22,6 @@ 
 load_lib mi-support.exp
 set MIFLAGS "-i=mi"
 
-gdb_exit
-if [mi_gdb_start] {
-    return
-}
-
 standard_testfile
 
 if  { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {debug}] != "" } {
@@ -34,9 +29,9 @@  if  { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {deb
     return -1
 }
 
-mi_delete_breakpoints
-mi_gdb_reinitialize_dir $srcdir/$subdir
-mi_gdb_load ${binfile}
+if {[mi_clean_restart $binfile]} {
+    return
+}
 
 mi_runto do_children_tests
 
diff --git a/gdb/testsuite/gdb.mi/mi-var-cmd.exp b/gdb/testsuite/gdb.mi/mi-var-cmd.exp
index 9bd1f7cd1a8..08465803734 100644
--- a/gdb/testsuite/gdb.mi/mi-var-cmd.exp
+++ b/gdb/testsuite/gdb.mi/mi-var-cmd.exp
@@ -22,11 +22,6 @@ 
 load_lib mi-support.exp
 set MIFLAGS "-i=mi"
 
-gdb_exit
-if [mi_gdb_start] {
-    return
-}
-
 standard_testfile var-cmd.c
 
 if  { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {debug}] != "" } {
@@ -34,10 +29,9 @@  if  { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {deb
     return -1
 }
 
-mi_delete_breakpoints
-mi_gdb_reinitialize_dir $srcdir/$subdir
-mi_gdb_load ${binfile}
-
+if {[mi_clean_restart $binfile]} {
+    return
+}
 
 #####                   #####
 #                           #
diff --git a/gdb/testsuite/gdb.mi/mi-var-cp.exp b/gdb/testsuite/gdb.mi/mi-var-cp.exp
index 3ce48db6c2b..edb4332e253 100644
--- a/gdb/testsuite/gdb.mi/mi-var-cp.exp
+++ b/gdb/testsuite/gdb.mi/mi-var-cp.exp
@@ -18,11 +18,6 @@  require allow_cplus_tests
 load_lib mi-support.exp
 set MIFLAGS "-i=mi"
 
-gdb_exit
-if [mi_gdb_start] {
-    return
-}
-
 standard_testfile .cc
 
 if {[gdb_compile $srcdir/$subdir/$srcfile $binfile executable {debug c++}] != ""} {
@@ -30,7 +25,9 @@  if {[gdb_compile $srcdir/$subdir/$srcfile $binfile executable {debug c++}] != ""
   return -1
 }
 
-mi_gdb_load ${binfile}
+if {[mi_clean_restart $binfile]} {
+    return
+}
 
 mi_prepare_inline_tests $srcfile
 
diff --git a/gdb/testsuite/gdb.mi/mi-var-display.exp b/gdb/testsuite/gdb.mi/mi-var-display.exp
index b889d197051..548c4fe2ca3 100644
--- a/gdb/testsuite/gdb.mi/mi-var-display.exp
+++ b/gdb/testsuite/gdb.mi/mi-var-display.exp
@@ -22,11 +22,6 @@ 
 load_lib mi-support.exp
 set MIFLAGS "-i=mi"
 
-gdb_exit
-if [mi_gdb_start] {
-    return
-}
-
 standard_testfile var-cmd.c
 
 if  { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {debug}] != "" } {
@@ -34,9 +29,9 @@  if  { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {deb
     return -1
 }
 
-mi_delete_breakpoints
-mi_gdb_reinitialize_dir $srcdir/$subdir
-mi_gdb_load ${binfile}
+if {[mi_clean_restart $binfile]} {
+    return
+}
 
 set line_dct_end [gdb_get_line_number "{int a = 0;}"]
 
diff --git a/gdb/testsuite/gdb.mi/mi-var-invalidate.exp b/gdb/testsuite/gdb.mi/mi-var-invalidate.exp
index dfaff33b9a2..20d9171ac18 100644
--- a/gdb/testsuite/gdb.mi/mi-var-invalidate.exp
+++ b/gdb/testsuite/gdb.mi/mi-var-invalidate.exp
@@ -23,11 +23,6 @@ 
 load_lib mi-support.exp
 set MIFLAGS "-i=mi"
 
-gdb_exit
-if [mi_gdb_start] {
-    return
-}
-
 standard_testfile var-cmd.c
 
 if  { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {debug}] != "" } {
@@ -49,9 +44,9 @@  if  { [gdb_compile "${srcdir}/${subdir}/${srcfile2}" "${binfile2}" executable {d
     return -1
 }
 
-mi_delete_breakpoints
-mi_gdb_reinitialize_dir $srcdir/$subdir
-mi_gdb_load ${binfile}
+if {[mi_clean_restart $binfile]} {
+    return
+}
 
 # Desc:  Create global variable.
 mi_create_varobj global_simple global_simple "create global variable"
diff --git a/gdb/testsuite/gdb.mi/mi-var-list-children-invalid-grandchild.exp b/gdb/testsuite/gdb.mi/mi-var-list-children-invalid-grandchild.exp
index ea02467c5e0..f9da4e46c85 100644
--- a/gdb/testsuite/gdb.mi/mi-var-list-children-invalid-grandchild.exp
+++ b/gdb/testsuite/gdb.mi/mi-var-list-children-invalid-grandchild.exp
@@ -19,11 +19,6 @@ 
 load_lib mi-support.exp
 set MIFLAGS "-i=mi"
 
-gdb_exit
-if [mi_gdb_start] {
-    return
-}
-
 standard_testfile
 
 if  { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {debug}] != "" } {
@@ -31,9 +26,9 @@  if  { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {deb
     return -1
 }
 
-mi_delete_breakpoints
-mi_gdb_reinitialize_dir $srcdir/$subdir
-mi_gdb_load ${binfile}
+if {[mi_clean_restart $binfile]} {
+    return
+}
 
 set line_invalid_pointer_value [gdb_get_line_number "p_outer set to invalid value"]
 set line_valid_pointer_value [gdb_get_line_number "p_outer set to valid value"]
diff --git a/gdb/testsuite/gdb.mi/mi-var-rtti.exp b/gdb/testsuite/gdb.mi/mi-var-rtti.exp
index fb8e4923f82..a41d58e0b52 100644
--- a/gdb/testsuite/gdb.mi/mi-var-rtti.exp
+++ b/gdb/testsuite/gdb.mi/mi-var-rtti.exp
@@ -18,11 +18,6 @@  require allow_cplus_tests
 load_lib mi-support.exp
 set MIFLAGS "-i=mi"
 
-gdb_exit
-if [mi_gdb_start] {
-    return
-}
-
 standard_testfile .cc
 set opts {debug c++}
 
@@ -30,7 +25,9 @@  if [build_executable $testfile.exp $testfile $srcfile $opts] {
     return -1
 }
 
-mi_gdb_load ${binfile}
+if {[mi_clean_restart $binfile]} {
+    return
+}
 
 mi_prepare_inline_tests $srcfile
 
diff --git a/gdb/testsuite/gdb.mi/mi-vla-c99.exp b/gdb/testsuite/gdb.mi/mi-vla-c99.exp
index ddfe6076213..3c28549e621 100644
--- a/gdb/testsuite/gdb.mi/mi-vla-c99.exp
+++ b/gdb/testsuite/gdb.mi/mi-vla-c99.exp
@@ -21,11 +21,6 @@ 
 load_lib mi-support.exp
 set MIFLAGS "-i=mi"
 
-gdb_exit
-if [mi_gdb_start] {
-    return
-}
-
 standard_testfile vla.c
 
 if { [gdb_compile "${srcdir}/${subdir}/${srcfile}" \
@@ -34,9 +29,9 @@  if { [gdb_compile "${srcdir}/${subdir}/${srcfile}" \
      return -1
 }
 
-mi_delete_breakpoints
-mi_gdb_reinitialize_dir $srcdir/$subdir
-mi_gdb_load ${binfile}
+if {[mi_clean_restart $binfile]} {
+    return
+}
 
 set bp_lineno [gdb_get_line_number "vla-filled"]
 
diff --git a/gdb/testsuite/gdb.mi/mi-vla-fortran.exp b/gdb/testsuite/gdb.mi/mi-vla-fortran.exp
index a240fb36d61..e4db423babd 100644
--- a/gdb/testsuite/gdb.mi/mi-vla-fortran.exp
+++ b/gdb/testsuite/gdb.mi/mi-vla-fortran.exp
@@ -22,11 +22,6 @@  load_lib mi-support.exp
 load_lib fortran.exp
 set MIFLAGS "-i=mi"
 
-gdb_exit
-if [mi_gdb_start] {
-    return
-}
-
 standard_testfile vla.f90
 
 if { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable \
@@ -39,9 +34,9 @@  if { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable \
 # the type names can be printed differently.
 set real [fortran_real4]
 
-mi_delete_breakpoints
-mi_gdb_reinitialize_dir $srcdir/$subdir
-mi_gdb_load ${binfile}
+if {[mi_clean_restart $binfile]} {
+    return
+}
 
 set bp_lineno [gdb_get_line_number "vla1-not-allocated"]
 mi_create_breakpoint "-t vla.f90:$bp_lineno" \
diff --git a/gdb/testsuite/gdb.mi/mi-watch.exp b/gdb/testsuite/gdb.mi/mi-watch.exp
index a6e6f89bc11..5303a41e86e 100644
--- a/gdb/testsuite/gdb.mi/mi-watch.exp
+++ b/gdb/testsuite/gdb.mi/mi-watch.exp
@@ -31,8 +31,6 @@  set allow_hw_watchpoint_tests_p [allow_hw_watchpoint_tests]
 load_lib mi-support.exp
 set MIFLAGS "-i=mi"
 
-gdb_exit
-
 standard_testfile basics.c
 
 if  { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {debug}] != "" } {
diff --git a/gdb/testsuite/gdb.mi/mi2-amd64-entry-value.exp b/gdb/testsuite/gdb.mi/mi2-amd64-entry-value.exp
index 109f4f18235..9154f5c620f 100644
--- a/gdb/testsuite/gdb.mi/mi2-amd64-entry-value.exp
+++ b/gdb/testsuite/gdb.mi/mi2-amd64-entry-value.exp
@@ -16,11 +16,6 @@ 
 load_lib mi-support.exp
 set MIFLAGS "-i=mi2"
 
-gdb_exit
-if [mi_gdb_start] {
-    return
-}
-
 standard_testfile .s
 set opts {}
 
@@ -32,14 +27,13 @@  if [info exists COMPILE] {
     require {istarget x86_64-*-*} is_lp64_target
 }
 
-set executable ${testfile}
-
-if [build_executable ${testfile}.exp ${executable} ${srcfile} $opts] {
+if [build_executable ${testfile}.exp ${binfile} ${srcfile} $opts] {
     return -1
 }
 
-mi_gdb_reinitialize_dir $srcdir/$subdir
-mi_gdb_load ${binfile}
+if {[mi_clean_restart $binfile]} {
+    return
+}
 
 foreach name {different breakhere_different breakhere_validity breakhere_invalid} {
     mi_create_breakpoint $name "break $name"
diff --git a/gdb/testsuite/gdb.mi/mi2-cli-display.exp b/gdb/testsuite/gdb.mi/mi2-cli-display.exp
index 817c3832c66..6a2b86d7e12 100644
--- a/gdb/testsuite/gdb.mi/mi2-cli-display.exp
+++ b/gdb/testsuite/gdb.mi/mi2-cli-display.exp
@@ -18,10 +18,6 @@ 
 load_lib mi-support.exp
 set MIFLAGS "-i=mi2"
 
-if {[mi_gdb_start]} {
-    return
-}
-
 standard_testfile
 
 if  { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {debug}] != "" } {
@@ -29,9 +25,9 @@  if  { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {deb
     return -1
 }
 
-mi_delete_breakpoints
-mi_gdb_reinitialize_dir $srcdir/$subdir
-mi_gdb_load ${binfile}
+if {[mi_clean_restart $binfile]} {
+    return
+}
 
 mi_runto do_tests
 
diff --git a/gdb/testsuite/gdb.mi/mi2-prompt.exp b/gdb/testsuite/gdb.mi/mi2-prompt.exp
index 22fda0e0970..f528c76b3d8 100644
--- a/gdb/testsuite/gdb.mi/mi2-prompt.exp
+++ b/gdb/testsuite/gdb.mi/mi2-prompt.exp
@@ -16,8 +16,7 @@ 
 load_lib mi-support.exp
 set MIFLAGS "-i=mi2"
 
-gdb_exit
-if [mi_gdb_start] {
+if {[mi_clean_restart]} {
     return
 }
 
diff --git a/gdb/testsuite/gdb.mi/mi2-var-child.exp b/gdb/testsuite/gdb.mi/mi2-var-child.exp
index 47cfeb860e3..5c40f83d1fa 100644
--- a/gdb/testsuite/gdb.mi/mi2-var-child.exp
+++ b/gdb/testsuite/gdb.mi/mi2-var-child.exp
@@ -22,11 +22,6 @@ 
 load_lib mi-support.exp
 set MIFLAGS "-i=mi2"
 
-gdb_exit
-if [mi_gdb_start] {
-    return
-}
-
 standard_testfile var-cmd.c
 
 if  { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {debug}] != "" } {
@@ -34,9 +29,9 @@  if  { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {deb
     return -1
 }
 
-mi_delete_breakpoints
-mi_gdb_reinitialize_dir $srcdir/$subdir
-mi_gdb_load ${binfile}
+if {[mi_clean_restart $binfile]} {
+    return
+}
 
 mi_runto do_children_tests
 
diff --git a/gdb/testsuite/gdb.python/py-framefilter-mi.exp b/gdb/testsuite/gdb.python/py-framefilter-mi.exp
index 723945dde83..509f21b3962 100644
--- a/gdb/testsuite/gdb.python/py-framefilter-mi.exp
+++ b/gdb/testsuite/gdb.python/py-framefilter-mi.exp
@@ -20,11 +20,6 @@  load_lib gdb-python.exp
 
 set MIFLAGS "-i=mi2"
 
-gdb_exit
-if [mi_gdb_start] {
-    return
-}
-
 standard_testfile py-framefilter-mi.c
 set pyfile py-framefilter.py
 
@@ -33,9 +28,9 @@  if  { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {deb
     return -1
 }
 
-mi_delete_breakpoints
-mi_gdb_reinitialize_dir $srcdir/$subdir
-mi_gdb_load ${binfile}
+if {[mi_clean_restart $binfile]} {
+    return
+}
 
 if {[lsearch -exact [mi_get_features] python] < 0} {
     unsupported "python support is disabled"
diff --git a/gdb/testsuite/gdb.python/py-mi-events.exp b/gdb/testsuite/gdb.python/py-mi-events.exp
index cc417d48bfe..8d546a9e757 100644
--- a/gdb/testsuite/gdb.python/py-mi-events.exp
+++ b/gdb/testsuite/gdb.python/py-mi-events.exp
@@ -20,12 +20,8 @@  set MIFLAGS "-i=mi2"
 
 require allow_python_tests
 
-gdb_exit
-if [mi_gdb_start] {
-    return
-}
-
 standard_testfile
+
 set pyfile ${testfile}-gdb.py
 
 if  { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {debug}] != "" } {
@@ -35,8 +31,10 @@  if  { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {deb
 
 set remote_python_file [gdb_remote_download host ${srcdir}/${subdir}/${pyfile}]
 
-mi_delete_breakpoints
-mi_gdb_reinitialize_dir $srcdir/$subdir
+if {[mi_clean_restart]} {
+    return
+}
+
 mi_gdb_test "set auto-load safe-path ${remote_python_file}" \
     {.*\^done} \
     "set safe-path"
diff --git a/gdb/testsuite/gdb.python/py-mi-objfile.exp b/gdb/testsuite/gdb.python/py-mi-objfile.exp
index bc02da252bc..d098bba547d 100644
--- a/gdb/testsuite/gdb.python/py-mi-objfile.exp
+++ b/gdb/testsuite/gdb.python/py-mi-objfile.exp
@@ -20,11 +20,6 @@  set MIFLAGS "-i=mi2"
 
 require allow_python_tests
 
-gdb_exit
-if [mi_gdb_start] {
-    return
-}
-
 standard_testfile
 set pyfile ${testfile}-gdb.py
 
@@ -38,8 +33,10 @@  if  { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {deb
 # gdb will find it.
 set remote_python_file [gdb_remote_download host ${srcdir}/${subdir}/${pyfile}]
 
-mi_delete_breakpoints
-mi_gdb_reinitialize_dir $srcdir/$subdir
+if {[mi_clean_restart $binfile]} {
+    return
+}
+
 mi_gdb_test "set auto-load safe-path ${remote_python_file}" \
     {.*\^done} \
     "set safe-path"
diff --git a/gdb/testsuite/gdb.python/py-mi.exp b/gdb/testsuite/gdb.python/py-mi.exp
index 705a9c41741..a7b7a5ffcd3 100644
--- a/gdb/testsuite/gdb.python/py-mi.exp
+++ b/gdb/testsuite/gdb.python/py-mi.exp
@@ -19,11 +19,6 @@ 
 load_lib mi-support.exp
 set MIFLAGS "-i=mi2"
 
-gdb_exit
-if [mi_gdb_start] {
-    return
-}
-
 standard_testfile py-prettyprint.c
 set pyfile py-prettyprint.py
 if  { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {debug additional_flags=-DMI}] != "" } {
@@ -31,9 +26,9 @@  if  { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {deb
     return -1
 }
 
-mi_delete_breakpoints
-mi_gdb_reinitialize_dir $srcdir/$subdir
-mi_gdb_load ${binfile}
+if {[mi_clean_restart $binfile]} {
+    return
+}
 
 if {[lsearch -exact [mi_get_features] python] < 0} {
     unsupported "python support is disabled"
@@ -347,12 +342,9 @@  if  { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}-cxx" \
     return -1
 }
 
-if [mi_gdb_start] {
+if {[mi_clean_restart ${binfile}-cxx]} {
     return
 }
-mi_delete_breakpoints
-mi_gdb_reinitialize_dir $srcdir/$subdir
-mi_gdb_load ${binfile}-cxx
 
 if {[lsearch -exact [mi_get_features] python] < 0} {
     unsupported "python support is disabled"
diff --git a/gdb/testsuite/gdb.trace/mi-tracepoint-changed.exp b/gdb/testsuite/gdb.trace/mi-tracepoint-changed.exp
index 6dc5e38d1b2..fab1463d268 100644
--- a/gdb/testsuite/gdb.trace/mi-tracepoint-changed.exp
+++ b/gdb/testsuite/gdb.trace/mi-tracepoint-changed.exp
@@ -89,19 +89,12 @@  proc test_reconnect { } {
 	    }
 	}
 
-	gdb_exit
+	global binfile
 
-	if [mi_gdb_start] {
+	if {[mi_clean_restart $binfile]} {
 	    return
 	}
 
-	global srcdir
-	global subdir
-	global binfile
-
-	mi_gdb_reinitialize_dir $srcdir/$subdir
-	mi_gdb_load ${binfile}
-
 	global gdbserver_protocol
 	global gdbserver_gdbport
 
@@ -162,19 +155,13 @@  proc test_pending_resolved { } {
     with_test_prefix "pending resolved" {
 	global decimal hex
 	global executable
-	global srcdir
-	global subdir
 	global binfile
 	global lib_sl1 lib_sl2
 	global mi_gdb_prompt
 
-	gdb_exit
-	if [mi_gdb_start] {
+	if {[mi_clean_restart $binfile]} {
 	    return
 	}
-
-	mi_gdb_reinitialize_dir $srcdir/$subdir
-	mi_gdb_load ${binfile}
 	mi_load_shlibs $lib_sl1 $lib_sl2
 
 	# Create a pending tracepoint on pendfunc2
diff --git a/gdb/testsuite/gdb.trace/mi-tsv-changed.exp b/gdb/testsuite/gdb.trace/mi-tsv-changed.exp
index ea7fcf31a30..792bd4f7d42 100644
--- a/gdb/testsuite/gdb.trace/mi-tsv-changed.exp
+++ b/gdb/testsuite/gdb.trace/mi-tsv-changed.exp
@@ -76,13 +76,11 @@  proc test_create_delete_modify_tsv { } {
 	    return -1
 	}
 	gdb_exit
-	if [mi_gdb_start] {
+
+	if {[mi_clean_restart $binfile]} {
 	    return
 	}
 
-	mi_gdb_reinitialize_dir $srcdir/$subdir
-	mi_gdb_load ${binfile}
-
 	mi_gdb_test "tvariable \$tvar3 = 3" \
 	    ".*=tsv-created,name=\"tvar3\",initial=\"3\".*\\^done" \
 	    "tvariable \$tvar3 modified"
@@ -177,19 +175,11 @@  proc test_upload_tsv { } {
 	    }
 	}
 
-	gdb_exit
-
-	if [mi_gdb_start] {
+	global binfile
+	if {[mi_clean_restart $binfile]} {
 	    return
 	}
 
-	global srcdir
-	global subdir
-	global binfile
-
-	mi_gdb_reinitialize_dir $srcdir/$subdir
-	mi_gdb_load ${binfile}
-
 	global gdbserver_protocol
 	global gdbserver_gdbport