Home | History | Annotate | Line # | Download | only in gdb.trace
      1 #   Copyright 2010-2025 Free Software Foundation, Inc.
      2 #
      3 # This program is free software; you can redistribute it and/or modify
      4 # it under the terms of the GNU General Public License as published by
      5 # the Free Software Foundation; either version 3 of the License, or
      6 # (at your option) any later version.
      7 #
      8 # This program is distributed in the hope that it will be useful,
      9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
     10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     11 # GNU General Public License for more details.
     12 #
     13 # You should have received a copy of the GNU General Public License
     14 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
     15 
     16 # Test of trace file support.
     17 
     18 # Note that unlike most of the tracing tests, this can be run on
     19 # targets lacking tracepoint support; the program tfile.c has the
     20 # ability to generate synthetic trace files directly, and the tfile
     21 # target is available to all GDB configs.
     22 
     23 load_lib "trace-support.exp"
     24 
     25 if {![is_remote host] && ![is_remote target]} {
     26     set tfile_basic [standard_output_file tfile-basic.tf]
     27     set tfile_error [standard_output_file tfile-error.tf]
     28     set tfile_dir [file dirname $tfile_basic]/
     29     set purely_local 1
     30 } else {
     31     set tfile_basic tfile-basic.tf
     32     set tfile_error tfile-error.tf
     33     set tfile_dir ""
     34     set purely_local 0
     35 }
     36 
     37 standard_testfile
     38 if { [gdb_compile "$srcdir/$subdir/$srcfile" $binfile \
     39 	  executable \
     40 	  [list debug nopie\
     41 	       "additional_flags=-DTFILE_DIR=\"$tfile_dir\""]] \
     42 	 != "" } {
     43     untested "failed to compile"
     44     return -1
     45 }
     46 
     47 # Make sure we are starting fresh.
     48 remote_file host delete $tfile_basic
     49 remote_file host delete $tfile_error
     50 remote_file target delete $tfile_basic
     51 remote_file target delete $tfile_error
     52 
     53 if { ![generate_tracefile $binfile] } {
     54     unsupported "unable to generate trace file"
     55     return -1
     56 }
     57 
     58 if {!$purely_local} {
     59     # Copy tracefile from target to host through build.
     60     remote_download host [remote_upload target tfile-basic.tf] tfile-basic.tf
     61     remote_download host [remote_upload target tfile-error.tf] tfile-error.tf
     62 }
     63 
     64 clean_restart $testfile
     65 
     66 # Program has presumably exited, now target a trace file it created.
     67 
     68 gdb_test "target tfile $tfile_basic" "Created tracepoint.*" \
     69     "target tfile [file tail $tfile_basic]"
     70 
     71 gdb_test "info trace" ".*tracepoint.*in write_basic_trace_file.*" \
     72     "info tracepoints on trace file"
     73 
     74 gdb_test "tfind 0" \
     75     "Found trace frame 0, tracepoint \[0-9\]+.
     76 \#0  write_basic_trace_file ().*" \
     77     "tfind 0 on trace file"
     78 
     79 # Note that there is no tracepoint collecting these globals, we
     80 # just happen to know they are covered by the trace frame.
     81 
     82 gdb_test "print testglob" " = 31415" "print testglob on trace file"
     83 
     84 gdb_test "print testglob2" " = 271828" "print testglob2 on trace file"
     85 
     86 # This global is not covered by the trace frame, but since it's const,
     87 # we should be able to read it from the executable.
     88 
     89 gdb_test "print constglob" " = 10000" "print constglob on trace file"
     90 
     91 # Similarly, disassembly should find the read-only pieces in the executable.
     92 gdb_test "disassemble main" \
     93     "Dump of assembler code for function main:.*   $hex <\\+0\\>:.*End of assembler dump\."
     94 
     95 # This global is also not covered by the trace frame, and since it's
     96 # non-const, we should _not_ read it from the executable, as that
     97 # would show the variable's initial value, not the current at time the
     98 # trace frame was created.
     99 
    100 gdb_test "print nonconstglob" \
    101     " = <unavailable>" "print nonconstglob on trace file"
    102 
    103 gdb_test "tfind" "Target failed to find requested trace frame." \
    104     "tfind does not find a second frame in trace file"
    105 
    106 gdb_test "tstatus" \
    107     "Using a trace file.*
    108 Trace stopped by a tstop command.*
    109 Collected 1 trace frame.*
    110 Trace buffer has 256 bytes of 4096 bytes free \\(93% full\\).*
    111 Looking at trace frame 0, tracepoint .*" \
    112     "tstatus on trace file"
    113 
    114 gdb_test "tfind end" "No longer looking at any trace frame" "leave tfind mode"
    115 
    116 gdb_test "backtrace" "No stack\." \
    117     "no stack if no traceframe selected"
    118 
    119 gdb_test "info registers" "The program has no registers now\." \
    120     "no registers if no traceframe selected"
    121 
    122 # Now start afresh, using only a trace file.
    123 
    124 clean_restart $testfile
    125 
    126 gdb_test "target tfile $tfile_error" "Created tracepoint.*" \
    127     "target tfile [file tail $tfile_error]"
    128 
    129 gdb_test "tstatus" \
    130     "Using a trace file.*
    131 Trace stopped by an error \\(made-up error, tracepoint 1\\).*
    132 Collected 0 trace frame.*
    133 Trace buffer has 256 bytes of 4096 bytes free \\(93% full\\).*
    134 Not looking at any trace frame.*" \
    135     "tstatus on error trace file"
    136 
    137 # Make sure we can reopen without error.
    138 gdb_test \
    139     "interpreter-exec mi \"-target-select tfile $tfile_basic\"" \
    140     "\\^connected.*" \
    141     "interpreter-exec mi \"-target-select tfile tfile-basic.tf\""
    142 
    143 gdb_test "interpreter-exec mi \"-trace-status\"" \
    144     "\\^done,supported=\"file\",trace-file=\".*$tfile_basic\",running=\"0\",stop-reason=\"request\",frames=\"${decimal}\",frames-created=\"${decimal}\",buffer-size=\"${decimal}\",buffer-free=\"${decimal}\",disconnected=\".*\",circular=\".*\",user-name=\"\",notes=\"\",start-time=\".*\",stop-time=\".*\"" \
    145     "-trace-status"
    146 
    147 # Test completion works well.
    148 
    149 if { [readline_is_used] } {
    150     gdb_test "target tfile [file rootname $tfile_basic]\t" \
    151 	"Assuming tracepoint.*" \
    152 	"complete-command 'target tfile'"
    153 }
    154