1 # Copyright (C) 2020-2023 Free Software Foundation, Inc.
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.
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.
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/>.
16 # Test for PR tui/25126.
18 # The bug is about a regression that makes GDB not reload its source
19 # code cache when the inferior's symbols are reloaded, which leads to
20 # wrong backtraces/listings.
22 # This bug is reproducible even without using the TUI.
26 # Only run on native boards.
27 if { [use_gdb_stub] || [target_info gdb_protocol] == "extended-remote" } {
31 # Because we need to modify the source file later, it's better if we
32 # just copy it to our output directory (instead of messing with the
33 # user's source directory).
34 set newsrc [standard_output_file $testfile].c
35 file copy -force -- $srcdir/$subdir/$srcfile $newsrc
38 if { [prepare_for_testing "failed to prepare" $testfile $srcfile] } {
42 # Get the line number for the line with the "break-here" marker.
43 set bp_line [gdb_get_line_number "break-here" $srcfile]
45 gdb_assert { [runto "$srcfile:$bp_line"] } \
46 "run to break-here marker"
48 # Do a "list" and check that the printed line matches the line of the
49 # original source file.
50 gdb_test_no_output "set listsize 1"
51 gdb_test "list" "$bp_line\[ \t\]+printf \\(\"hello\\\\n\"\\); /\\* break-here \\*/" \
52 "check the first version of the source file"
54 # Modify the original source file, and add an extra line into it.
55 # This only works locally because of the TCL commands.
56 set bkpsrc [standard_output_file $testfile].c.bkp
57 set bkpsrcfd [open $bkpsrc w]
58 set srcfd [open $srcfile r]
60 while { [gets $srcfd line] != -1 } {
61 if { [string first "break-here" $line] != -1 } {
62 # Put a "printf" line before the "break-here" line.
63 puts $bkpsrcfd " printf (\"foo\\n\"); /* new-marker */"
70 file rename -force -- $bkpsrc $srcfile
71 # We have to wait 1 second because of the way GDB checks whether the
72 # binary has changed or not. GDB uses stat(2) and currently checks
73 # 'st_mtime', whose precision is measured in seconds. Since the copy,
74 # rename, and rebuild can take less than 1 second, GDB might mistakenly
75 # assume that the binary is unchanged.
78 # Recompile the modified source. We use "gdb_compile" here instead of
79 # "prepare_for_testing" because we don't want to call "clean_restart".
80 if { [gdb_compile "${srcfile}" "${binfile}" executable {debug}] != "" } {
84 # Rerun the program. This should not only force GDB to reload the
85 # source cache, but also to break at BP_LINE again, which now has
89 "The program being debugged has been started already\\." \
90 "Start it from the beginning\\? \\(y or n\\) "]
91 set binregex [string_to_regexp $binfile]
94 "\\`$binregex\\' has changed; re-reading symbols\\.(" \
95 "Expanding full symbols from $binfile\\.\\.\\.)?" \
96 "Starting program: ${binregex}.*"]
97 gdb_test "run" $re "rerun program" $q y
99 # Again, perform the listing and check that the line indeed has
101 gdb_test "list" "${bp_line}\[ \t\]+printf \\(\"foo\\\\n\"\\); /\\\* new-marker \\\*/.*" \
102 "verify that the source code is properly reloaded"
104 # Modify the source file again. As before, this only works locally
105 # because of the TCL commands.
106 set bkpsrc [standard_output_file $testfile].c.bkp
107 set bkpsrcfd [open $bkpsrc w]
108 set srcfd [open $srcfile r]
110 while { [gets $srcfd line] != -1 } {
111 if { [string first "new-marker" $line] != -1 } {
112 # Modify the printf line that we added previously.
113 puts $bkpsrcfd " printf (\"foo\\n\"); /* new-marker updated */"
121 file rename -force -- $bkpsrc $srcfile
123 # As before, delay so that at least one second has passed. GDB still
124 # will not spot that the source file has changed, as GDB doesn't do a
125 # time check unless the binary has also changed, this delay just
126 # allows us to confirm this behaviour.
129 # List the printf line again, we should not see the file changes yet
130 # as the binary is unchanged, so the cached contents will still be
132 gdb_test "list ${bp_line}" "${bp_line}\[ \t\]+printf \\(\"foo\\\\n\"\\); /\\\* new-marker \\\*/.*" \
133 "verify that the source code change is not seen yet"
135 gdb_test "maint flush source-cache" "Source cache flushed\\."
137 # List the printf line again. After the cache flush GDB will re-read
138 # the source file and we should now see the changes.
139 gdb_test "list ${bp_line}" "${bp_line}\[ \t\]+printf \\(\"foo\\\\n\"\\); /\\\* new-marker updated \\\*/.*" \
140 "verify that the updated source code change is not seen"