[12/27] Use clean_restart in gdb.trace

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

Commit Message

Tom Tromey Jan. 25, 2023, 10:45 p.m. UTC
  Change gdb.trace to use clean_restart more consistently.
---
 gdb/testsuite/gdb.trace/actions.exp        | 20 +++--------------
 gdb/testsuite/gdb.trace/ax.exp             |  6 +-----
 gdb/testsuite/gdb.trace/change-loc.exp     |  4 +---
 gdb/testsuite/gdb.trace/deltrace.exp       | 10 +--------
 gdb/testsuite/gdb.trace/entry-values.exp   | 11 +---------
 gdb/testsuite/gdb.trace/infotrace.exp      |  9 +-------
 gdb/testsuite/gdb.trace/packetlen.exp      |  8 ++-----
 gdb/testsuite/gdb.trace/passc-dyn.exp      |  7 ++----
 gdb/testsuite/gdb.trace/passcount.exp      |  9 +-------
 gdb/testsuite/gdb.trace/pending.exp        |  4 +---
 gdb/testsuite/gdb.trace/read-memory.exp    |  5 +----
 gdb/testsuite/gdb.trace/report.exp         |  7 +-----
 gdb/testsuite/gdb.trace/save-trace.exp     |  9 +-------
 gdb/testsuite/gdb.trace/tfile.exp          |  5 +----
 gdb/testsuite/gdb.trace/tfind.exp          |  8 ++-----
 gdb/testsuite/gdb.trace/tracecmd.exp       |  9 +-------
 gdb/testsuite/gdb.trace/tsv.exp            |  6 ++----
 gdb/testsuite/gdb.trace/unavailable.exp    | 25 +++++-----------------
 gdb/testsuite/gdb.trace/while-dyn.exp      |  8 ++-----
 gdb/testsuite/gdb.trace/while-stepping.exp | 20 +++--------------
 20 files changed, 33 insertions(+), 157 deletions(-)
  

Comments

Pedro Alves Jan. 26, 2023, 5:34 p.m. UTC | #1
On 2023-01-25 10:45 p.m., Tom Tromey wrote:
> --- a/gdb/testsuite/gdb.trace/unavailable.exp
> +++ b/gdb/testsuite/gdb.trace/unavailable.exp
> @@ -202,10 +202,7 @@ proc gdb_collect_args_test {} {
>  
>  	foreach target_name ${trace_file_targets} {
>  	    # Restart GDB and read the trace data in ${TARGET_NAME} target.
> -	    gdb_exit
> -	    gdb_start
> -	    gdb_reinitialize_dir $srcdir/$subdir
> -	    gdb_file_cmd $binfile
> +	    clean_restart $binfile

Note this one is not 100% equivalent, because this was using gdb_file_cmd instead of gdb_load for
a reason --  I think because afterwards we're explicitly connecting to a target, while gdb_load may connect
to the target.  I recall patches from Daniel switching from gdb_load to gdb_file_cmd in tests that
don't really need execution for that reason years ago.  ISTR a number of them, but I'm not finding all
that much.  I did find this one, though:

  https://sourceware.org/pipermail/gdb-patches/2010-March/072735.html

So IIRC, what Daniel described back then is I think the reason several tests in
for example gdb.base/ use gdb_file_cmd.

So I grepped for gdb_file_cmd in gdb.base/ and randomly picked gdb.base/list.exp, used
git blame and that led me to this patch:

  https://sourceware.org/pipermail/gdb-patches/2012-June/093697.html

which again is exactly about avoiding gdb_load.

So maybe for the cases we are using gdb_file_cmd today, we should use:

	    clean_restart
	    gdb_file_cmd $binfile

instead of:

	    clean_restart $binfile

?

Pedro Alves

>  	    gdb_test "target ${target_name} ${tracefile}.args.${target_name}" ".*" \
>  		"change to ${target_name} target"
  
Tom Tromey Jan. 26, 2023, 6:34 p.m. UTC | #2
>>>>> "Pedro" == Pedro Alves <pedro@palves.net> writes:

>> -	    gdb_exit
>> -	    gdb_start
>> -	    gdb_reinitialize_dir $srcdir/$subdir
>> -	    gdb_file_cmd $binfile
>> +	    clean_restart $binfile

Pedro> Note this one is not 100% equivalent, because this was using
Pedro> gdb_file_cmd instead of gdb_load for a reason

Ugh, I saw that clean_restart calls gdb_load, but gdb_load is really
simple -- it just calls gdb_file_cmd!

But... now I see the board files redefine gdb_load.


It would be nice to get rid of the redefinitions and monkey-patching
that is done in the test suite.  IMO this leads to a lot of hidden
gotchas.

Maybe one idea would be to have a list (or namespace or something) of
override-able procs, and require each board to provide them all... and
then have a default "native" board.  This way at least the default
implementations wouldn't be living in gdb.exp to confuse me.

I'll dig through and fix up these bits.

Tom
  
Tom Tromey Jan. 27, 2023, 12:36 a.m. UTC | #3
Tom> I'll dig through and fix up these bits.

I went through all the patches and restored the use of gdb_file_cmd in
all the spots where I'd changed it.  I've re-tested and now I'm going to
check it in.

Tom
  

Patch

diff --git a/gdb/testsuite/gdb.trace/actions.exp b/gdb/testsuite/gdb.trace/actions.exp
index cf7efbbd4fd..270dd3f5aa4 100644
--- a/gdb/testsuite/gdb.trace/actions.exp
+++ b/gdb/testsuite/gdb.trace/actions.exp
@@ -17,10 +17,6 @@ 
 
 load_lib "trace-support.exp"
 
-
-gdb_exit
-gdb_start
-
 standard_testfile
 require gdb_trace_common_supports_arch
 if { [gdb_compile "$srcdir/$subdir/$srcfile" $binfile \
@@ -28,12 +24,8 @@  if { [gdb_compile "$srcdir/$subdir/$srcfile" $binfile \
     untested "failed to compile"
     return -1
 }
-gdb_reinitialize_dir $srcdir/$subdir
-
-# If testing on a remote host, download the source file.
-# remote_download host $srcdir/$subdir/$srcfile
 
-gdb_file_cmd $binfile
+clean_restart $binfile
 
 # define relative source line numbers:
 # all subsequent line numbers are relative to this first one (baseline)
@@ -325,10 +317,7 @@  gdb_test "tsave -ctf ${tracefile}.ctf" \
     "save ctf trace file"
 
 # Restart GDB and read the trace data in tfile target.
-gdb_exit
-gdb_start
-gdb_reinitialize_dir $srcdir/$subdir
-gdb_file_cmd $binfile
+clean_restart $binfile
 gdb_test "target tfile ${tracefile}.tf" ".*" \
     "change to tfile target"
 check_tracepoint "tfile"
@@ -345,10 +334,7 @@  gdb_test_multiple "target ctf" "" {
 }
 
 if { $gdb_can_read_ctf_data } {
-    gdb_exit
-    gdb_start
-    gdb_reinitialize_dir $srcdir/$subdir
-    gdb_file_cmd $binfile
+    clean_restart $binfile
     gdb_test "target ctf ${tracefile}.ctf" ".*" \
 	"change to ctf target"
     check_tracepoint "ctf"
diff --git a/gdb/testsuite/gdb.trace/ax.exp b/gdb/testsuite/gdb.trace/ax.exp
index 9dee743c8dd..9cc61d09182 100644
--- a/gdb/testsuite/gdb.trace/ax.exp
+++ b/gdb/testsuite/gdb.trace/ax.exp
@@ -20,9 +20,6 @@ 
 
 load_lib "trace-support.exp"
 
-
-gdb_exit
-gdb_start
 standard_testfile actions.c
 
 require gdb_trace_common_supports_arch
@@ -33,9 +30,8 @@  if { [gdb_compile "$srcdir/$subdir/$srcfile" $binfile \
     return -1
 }
 
-gdb_load $binfile
+clean_restart $binfile
 runto_main
-gdb_reinitialize_dir $srcdir/$subdir
 
 gdb_test "maint agent 12" ".*const8 12.*pop.*end.*"
 
diff --git a/gdb/testsuite/gdb.trace/change-loc.exp b/gdb/testsuite/gdb.trace/change-loc.exp
index 4c4f54d89be..b77e283c0b4 100644
--- a/gdb/testsuite/gdb.trace/change-loc.exp
+++ b/gdb/testsuite/gdb.trace/change-loc.exp
@@ -175,9 +175,7 @@  proc tracepoint_change_loc_2 { trace_type } {
 	global binfile
 	global gdb_prompt
 
-	gdb_exit
-	gdb_start
-	gdb_reinitialize_dir $srcdir/$subdir
+	clean_restart
 
 	gdb_test_multiple "${trace_type} set_tracepoint" "set pending tracepoint" {
 	    -re ".*Make \(|fast \)tracepoint pending.*y or \\\[n\\\]. $" {
diff --git a/gdb/testsuite/gdb.trace/deltrace.exp b/gdb/testsuite/gdb.trace/deltrace.exp
index cfcb81e8c91..c3107853808 100644
--- a/gdb/testsuite/gdb.trace/deltrace.exp
+++ b/gdb/testsuite/gdb.trace/deltrace.exp
@@ -17,10 +17,6 @@ 
 
 load_lib "trace-support.exp"
 
-
-gdb_exit
-gdb_start
-
 standard_testfile actions.c
 
 require gdb_trace_common_supports_arch
@@ -30,12 +26,8 @@  if { [gdb_compile "$srcdir/$subdir/$srcfile" $binfile \
     untested "failed to compile"
     return -1
 }
-gdb_reinitialize_dir $srcdir/$subdir
-
-# If testing on a remote host, download the source file.
-# remote_download host $srcdir/$subdir/$srcfile
 
-gdb_file_cmd $binfile
+clean_restart $binfile
 
 # define relative source line numbers:
 # all subsequent line numbers are relative to this first one (baseline)
diff --git a/gdb/testsuite/gdb.trace/entry-values.exp b/gdb/testsuite/gdb.trace/entry-values.exp
index d7e5a89f281..9fbad50e3bd 100644
--- a/gdb/testsuite/gdb.trace/entry-values.exp
+++ b/gdb/testsuite/gdb.trace/entry-values.exp
@@ -29,14 +29,7 @@  if  {[gdb_compile [list ${binfile}1.o] \
     return -1
 }
 
-# Start GDB and load executable file, compute the offset of the
-# instruction in bar returned from foo.  It is needed in the Dwarf
-# Assembler.
-
-gdb_exit
-gdb_start
-gdb_reinitialize_dir $srcdir/$subdir
-gdb_load ${binfile}1
+clean_restart ${binfile}1
 
 set returned_from_foo ""
 
@@ -83,8 +76,6 @@  if { [string equal $returned_from_foo ""] } {
     return -1
 }
 
-gdb_exit
-
 # Make some DWARF for the test.
 set asm_file [standard_output_file $srcfile2]
 Dwarf::assemble $asm_file {
diff --git a/gdb/testsuite/gdb.trace/infotrace.exp b/gdb/testsuite/gdb.trace/infotrace.exp
index 1252b30c0f5..41779206679 100644
--- a/gdb/testsuite/gdb.trace/infotrace.exp
+++ b/gdb/testsuite/gdb.trace/infotrace.exp
@@ -18,9 +18,6 @@ 
 load_lib "trace-support.exp"
 
 
-gdb_exit
-gdb_start
-
 standard_testfile actions.c
 
 require gdb_trace_common_supports_arch
@@ -30,12 +27,8 @@  if { [gdb_compile "$srcdir/$subdir/$srcfile" $binfile \
     untested "failed to compile"
     return -1
 }
-gdb_reinitialize_dir $srcdir/$subdir
-
-# If testing on a remote host, download the source file.
-# remote_download host $srcdir/$subdir/$srcfile
 
-gdb_file_cmd $binfile
+clean_restart $binfile
 
 #
 # test "info tracepoints" command
diff --git a/gdb/testsuite/gdb.trace/packetlen.exp b/gdb/testsuite/gdb.trace/packetlen.exp
index 31686bcb3bb..df4badc35bd 100644
--- a/gdb/testsuite/gdb.trace/packetlen.exp
+++ b/gdb/testsuite/gdb.trace/packetlen.exp
@@ -17,10 +17,6 @@ 
 
 load_lib "trace-support.exp"
 
-
-gdb_exit
-gdb_start
-
 standard_testfile actions.c
 require gdb_trace_common_supports_arch
 if { [gdb_compile "$srcdir/$subdir/$srcfile" $binfile \
@@ -28,11 +24,11 @@  if { [gdb_compile "$srcdir/$subdir/$srcfile" $binfile \
     untested "failed to compile"
     return -1
 }
-gdb_load $binfile
+
+clean_restart $binfile
 gdb_test "tstop"       ".*" ""
 gdb_test "tfind none"  ".*" ""
 runto_main
-gdb_reinitialize_dir $srcdir/$subdir
 
 if {![gdb_target_supports_trace]} {
     unsupported "current target does not support trace"
diff --git a/gdb/testsuite/gdb.trace/passc-dyn.exp b/gdb/testsuite/gdb.trace/passc-dyn.exp
index c005c79053b..26a2cac4dd9 100644
--- a/gdb/testsuite/gdb.trace/passc-dyn.exp
+++ b/gdb/testsuite/gdb.trace/passc-dyn.exp
@@ -17,9 +17,6 @@ 
 
 load_lib "trace-support.exp"
 
-
-gdb_exit
-gdb_start
 standard_testfile actions.c
 require gdb_trace_common_supports_arch
 if { [gdb_compile "$srcdir/$subdir/$srcfile" $binfile \
@@ -27,9 +24,9 @@  if { [gdb_compile "$srcdir/$subdir/$srcfile" $binfile \
     untested "failed to compile"
     return -1
 }
-gdb_load $binfile
+
+clean_restart $binfile
 runto_main
-gdb_reinitialize_dir $srcdir/$subdir
 
 if {![gdb_target_supports_trace]} {
     unsupported "current target does not support trace"
diff --git a/gdb/testsuite/gdb.trace/passcount.exp b/gdb/testsuite/gdb.trace/passcount.exp
index 3f711496083..d26d99946ed 100644
--- a/gdb/testsuite/gdb.trace/passcount.exp
+++ b/gdb/testsuite/gdb.trace/passcount.exp
@@ -17,9 +17,6 @@ 
 
 load_lib "trace-support.exp"
 
-
-gdb_exit
-gdb_start
 standard_testfile actions.c
 require gdb_trace_common_supports_arch
 if { [gdb_compile "$srcdir/$subdir/$srcfile" $binfile \
@@ -27,12 +24,8 @@  if { [gdb_compile "$srcdir/$subdir/$srcfile" $binfile \
     untested "failed to compile"
     return -1
 }
-gdb_reinitialize_dir $srcdir/$subdir
-
-# If testing on a remote host, download the source file.
-# remote_download host $srcdir/$subdir/$srcfile
 
-gdb_file_cmd $binfile
+clean_restart $binfile
 
 # define relative source line numbers:
 # all subsequent line numbers are relative to this first one (baseline)
diff --git a/gdb/testsuite/gdb.trace/pending.exp b/gdb/testsuite/gdb.trace/pending.exp
index 76d28652820..da355bcdaee 100644
--- a/gdb/testsuite/gdb.trace/pending.exp
+++ b/gdb/testsuite/gdb.trace/pending.exp
@@ -66,9 +66,7 @@  proc pending_tracepoint_resolved { trace_type } {
 	global lib_sl1
 
 	# Start with a fresh gdb.
-	gdb_exit
-	gdb_start
-	gdb_reinitialize_dir $srcdir/$subdir
+	clean_restart
 
 	gdb_test_multiple "$trace_type set_point1" "set pending tracepoint" {
 	    -re ".*Make \(fast |\)tracepoint pending.*y or \\\[n\\\]. $" {
diff --git a/gdb/testsuite/gdb.trace/read-memory.exp b/gdb/testsuite/gdb.trace/read-memory.exp
index 9d82939ca74..f24f72884ae 100644
--- a/gdb/testsuite/gdb.trace/read-memory.exp
+++ b/gdb/testsuite/gdb.trace/read-memory.exp
@@ -127,10 +127,7 @@  proc teset_from_exec { target } {
     global tracefile
 
     # Restart GDB and read the trace data in ${target} target.
-    gdb_exit
-    gdb_start
-    gdb_reinitialize_dir $srcdir/$subdir
-    gdb_file_cmd $binfile
+    clean_restart $binfile
 
     gdb_test "target ${target} ${tracefile}.${target}" ".*" \
 	"change to ${target} target"
diff --git a/gdb/testsuite/gdb.trace/report.exp b/gdb/testsuite/gdb.trace/report.exp
index 33bccaa1a6a..14db2511d40 100644
--- a/gdb/testsuite/gdb.trace/report.exp
+++ b/gdb/testsuite/gdb.trace/report.exp
@@ -17,10 +17,6 @@ 
 
 load_lib "trace-support.exp"
 
-
-gdb_exit
-gdb_start
-
 standard_testfile actions.c
 require gdb_trace_common_supports_arch
 if { [gdb_compile "$srcdir/$subdir/$srcfile" $binfile \
@@ -28,9 +24,8 @@  if { [gdb_compile "$srcdir/$subdir/$srcfile" $binfile \
     untested "failed to compile"
     return -1
 }
-gdb_load $binfile
+clean_restart $binfile
 runto_main
-gdb_reinitialize_dir $srcdir/$subdir
 
 if {![gdb_target_supports_trace]} {
     unsupported "current target does not support trace"
diff --git a/gdb/testsuite/gdb.trace/save-trace.exp b/gdb/testsuite/gdb.trace/save-trace.exp
index 71a094a25dc..ab0dce26d13 100644
--- a/gdb/testsuite/gdb.trace/save-trace.exp
+++ b/gdb/testsuite/gdb.trace/save-trace.exp
@@ -18,9 +18,6 @@ 
 load_lib "trace-support.exp"
 
 
-gdb_exit
-gdb_start
-
 standard_testfile actions.c
 require gdb_trace_common_supports_arch
 if { [gdb_compile "$srcdir/$subdir/$srcfile" $binfile \
@@ -28,12 +25,8 @@  if { [gdb_compile "$srcdir/$subdir/$srcfile" $binfile \
     untested "failed to compile"
     return -1
 }
-gdb_reinitialize_dir $srcdir/$subdir
-
-# If testing on a remote host, download the source file.
-# remote_download host $srcdir/$subdir/$srcfile
 
-gdb_file_cmd $binfile
+clean_restart $binfile
 
 # define relative source line numbers:
 # all subsequent line numbers are relative to this first one (baseline)
diff --git a/gdb/testsuite/gdb.trace/tfile.exp b/gdb/testsuite/gdb.trace/tfile.exp
index 68e951b1c83..c62f7e3e9e4 100644
--- a/gdb/testsuite/gdb.trace/tfile.exp
+++ b/gdb/testsuite/gdb.trace/tfile.exp
@@ -121,10 +121,7 @@  gdb_test "info registers" "The program has no registers now\." \
 
 # Now start afresh, using only a trace file.
 
-gdb_exit
-gdb_start
-
-gdb_load $binfile
+clean_restart $binfile
 
 gdb_test "target tfile $tfile_error" "Created tracepoint.*" \
     "target tfile [file tail $tfile_error]"
diff --git a/gdb/testsuite/gdb.trace/tfind.exp b/gdb/testsuite/gdb.trace/tfind.exp
index 4579e85ed86..2dbc42bb631 100644
--- a/gdb/testsuite/gdb.trace/tfind.exp
+++ b/gdb/testsuite/gdb.trace/tfind.exp
@@ -17,10 +17,6 @@ 
 
 load_lib "trace-support.exp"
 
-
-gdb_exit
-gdb_start
-
 standard_testfile actions.c
 
 require gdb_trace_common_supports_arch
@@ -30,7 +26,8 @@  if { [gdb_compile "$srcdir/$subdir/$srcfile" "$binfile" \
     untested "failed to compile"
     return -1
 }
-gdb_load $binfile
+
+clean_restart $binfile
 
 # 6.2 test help tstart
 gdb_test "help tstart" \
@@ -60,7 +57,6 @@  gdb_test "help tfind trace" "Select a trace frame by tracepoint number.*" \
 	"8.38: help tfind tracepoint"
 
 runto_main
-gdb_reinitialize_dir $srcdir/$subdir
 
 if {![gdb_target_supports_trace]} {
     unsupported "current target does not support trace"
diff --git a/gdb/testsuite/gdb.trace/tracecmd.exp b/gdb/testsuite/gdb.trace/tracecmd.exp
index 7ce6f59e2c9..0cf1ce0e0c0 100644
--- a/gdb/testsuite/gdb.trace/tracecmd.exp
+++ b/gdb/testsuite/gdb.trace/tracecmd.exp
@@ -17,9 +17,6 @@ 
 
 load_lib "trace-support.exp"
 
-
-gdb_exit
-gdb_start
 standard_testfile actions.c
 require gdb_trace_common_supports_arch
 if { [gdb_compile "$srcdir/$subdir/$srcfile" $binfile \
@@ -27,12 +24,8 @@  if { [gdb_compile "$srcdir/$subdir/$srcfile" $binfile \
     untested "failed to compile"
     return -1
 }
-gdb_reinitialize_dir $srcdir/$subdir
-
-# If testing on a remote host, download the source file.
-# remote_download host $srcdir/$subdir/$srcfile
 
-gdb_file_cmd $binfile
+clean_restart $binfile
 
 # define relative source line numbers:
 # all subsequent line numbers are relative to this first one (baseline)
diff --git a/gdb/testsuite/gdb.trace/tsv.exp b/gdb/testsuite/gdb.trace/tsv.exp
index ea7ad55143a..2319444fab1 100644
--- a/gdb/testsuite/gdb.trace/tsv.exp
+++ b/gdb/testsuite/gdb.trace/tsv.exp
@@ -15,9 +15,6 @@ 
 
 load_lib "trace-support.exp"
 
-
-gdb_exit
-gdb_start
 standard_testfile actions.c
 require gdb_trace_common_supports_arch
 if { [gdb_compile "$srcdir/$subdir/$srcfile" $binfile \
@@ -25,7 +22,8 @@  if { [gdb_compile "$srcdir/$subdir/$srcfile" $binfile \
     untested "failed to compile"
     return -1
 }
-gdb_load $binfile
+
+clean_restart $binfile
 
 # PR gdb/21352: Command tsave does not support -r argument
 gdb_test "tsave -r" "Argument required \\\(file in which to save trace data\\\)\." \
diff --git a/gdb/testsuite/gdb.trace/unavailable.exp b/gdb/testsuite/gdb.trace/unavailable.exp
index 65fc56c3383..ef091cbb0f4 100644
--- a/gdb/testsuite/gdb.trace/unavailable.exp
+++ b/gdb/testsuite/gdb.trace/unavailable.exp
@@ -202,10 +202,7 @@  proc gdb_collect_args_test {} {
 
 	foreach target_name ${trace_file_targets} {
 	    # Restart GDB and read the trace data in ${TARGET_NAME} target.
-	    gdb_exit
-	    gdb_start
-	    gdb_reinitialize_dir $srcdir/$subdir
-	    gdb_file_cmd $binfile
+	    clean_restart $binfile
 	    gdb_test "target ${target_name} ${tracefile}.args.${target_name}" ".*" \
 		"change to ${target_name} target"
 
@@ -286,10 +283,7 @@  proc gdb_collect_locals_test { func msg } {
 
 	foreach target_name ${trace_file_targets} {
 	    # Restart GDB and read the trace data in ${TARGET_NAME} target.
-	    gdb_exit
-	    gdb_start
-	    gdb_reinitialize_dir $srcdir/$subdir
-	    gdb_file_cmd $binfile
+	    clean_restart $binfile
 	    gdb_test "target ${target_name} ${tracefile}.locals.${target_name}" ".*" \
 		"change to ${target_name} target"
 
@@ -369,10 +363,7 @@  proc gdb_unavailable_registers_test { } {
 
 	foreach target_name ${trace_file_targets} {
 	    # Restart GDB and read the trace data in ${TARGET_NAME} target.
-	    gdb_exit
-	    gdb_start
-	    gdb_reinitialize_dir $srcdir/$subdir
-	    gdb_file_cmd $binfile
+	    clean_restart $binfile
 	    gdb_test "target ${target_name} ${tracefile}.registers.${target_name}" ".*" \
 		"change to ${target_name} target"
 
@@ -433,10 +424,7 @@  proc gdb_unavailable_floats { } {
 
 	foreach target_name ${trace_file_targets} {
 	    # Restart GDB and read the trace data in ${TARGET_NAME} target.
-	    gdb_exit
-	    gdb_start
-	    gdb_reinitialize_dir $srcdir/$subdir
-	    gdb_file_cmd $binfile
+	    clean_restart $binfile
 	    gdb_test "target ${target_name} ${tracefile}.floats.${target_name}" ".*" \
 		"change to ${target_name} target"
 
@@ -701,10 +689,7 @@  proc gdb_collect_globals_test { } {
 
 	foreach target_name ${trace_file_targets} {
 	    # Restart GDB and read the trace data in ${TARGET_NAME} target.
-	    gdb_exit
-	    gdb_start
-	    gdb_reinitialize_dir $srcdir/$subdir
-	    gdb_file_cmd $binfile
+	    clean_restart $binfile
 	    gdb_test "target ${target_name} ${tracefile}.globals.${target_name}" ".*" \
 		"change to ${target_name} target"
 
diff --git a/gdb/testsuite/gdb.trace/while-dyn.exp b/gdb/testsuite/gdb.trace/while-dyn.exp
index 960a95926b7..797020e7674 100644
--- a/gdb/testsuite/gdb.trace/while-dyn.exp
+++ b/gdb/testsuite/gdb.trace/while-dyn.exp
@@ -17,10 +17,6 @@ 
 
 load_lib "trace-support.exp"
 
-
-gdb_exit
-gdb_start
-
 standard_testfile actions.c
 set executable $testfile
 require gdb_trace_common_supports_arch
@@ -29,9 +25,9 @@  if { [gdb_compile "$srcdir/$subdir/$srcfile" $binfile \
     untested "failed to compile"
     return -1
 }
-gdb_load $binfile
+
+clean_restart $binfile
 runto_main
-gdb_reinitialize_dir $srcdir/$subdir
 
 if {![gdb_target_supports_trace]} {
     unsupported "current target does not support trace"
diff --git a/gdb/testsuite/gdb.trace/while-stepping.exp b/gdb/testsuite/gdb.trace/while-stepping.exp
index 64c0c9ed84f..ba4ad4434d5 100644
--- a/gdb/testsuite/gdb.trace/while-stepping.exp
+++ b/gdb/testsuite/gdb.trace/while-stepping.exp
@@ -17,10 +17,6 @@ 
 
 load_lib "trace-support.exp"
 
-
-gdb_exit
-gdb_start
-
 standard_testfile actions.c
 require gdb_trace_common_supports_arch
 if { [gdb_compile "${srcdir}/${subdir}/${srcfile}" $binfile \
@@ -28,12 +24,8 @@  if { [gdb_compile "${srcdir}/${subdir}/${srcfile}" $binfile \
     untested "failed to compile"
     return -1
 }
-gdb_reinitialize_dir $srcdir/$subdir
-
-# If testing on a remote host, download the source file.
-# remote_download host $srcdir/$subdir/$srcfile
 
-gdb_file_cmd $binfile
+clean_restart $binfile
 
 #
 # test while-stepping command
@@ -151,10 +143,7 @@  gdb_test "tsave -ctf ${tracefile}.ctf" \
     "save ctf trace file"
 
 # Restart GDB and read the trace data in tfile target.
-gdb_exit
-gdb_start
-gdb_reinitialize_dir $srcdir/$subdir
-gdb_file_cmd $binfile
+clean_restart $binfile
 gdb_test "target tfile ${tracefile}.tf" ".*" \
     "change to tfile target"
 check_tracepoint "tfile"
@@ -171,10 +160,7 @@  gdb_test_multiple "target ctf" "" {
 }
 
 if { $gdb_can_read_ctf_data } {
-    gdb_exit
-    gdb_start
-    gdb_reinitialize_dir $srcdir/$subdir
-    gdb_file_cmd $binfile
+    clean_restart $binfile
     gdb_test "target ctf ${tracefile}.ctf" ".*" \
 	"change to ctf target"
     check_tracepoint "ctf"