1 # Copyright (C) 2020-2024 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.
28 if { [target_info gdb_protocol] == "extended-remote" } {
32 # Because we need to modify the source file later, it's better if we
33 # just copy it to our output directory (instead of messing with the
34 # user's source directory).
35 set newsrc [standard_output_file $testfile].c
36 file copy -force -- $srcdir/$subdir/$srcfile $newsrc
39 if { [prepare_for_testing "failed to prepare" $testfile $srcfile] } {
43 # Get the line number for the line with the "break-here" marker.
44 set bp_line [gdb_get_line_number "break-here" $srcfile]
46 gdb_assert { [runto "$srcfile:$bp_line"] } \
47 "run to break-here marker"
49 # Do a "list" and check that the printed line matches the line of the
50 # original source file.
51 gdb_test_no_output "set listsize 1"
52 gdb_test "list" "$bp_line\[ \t\]+printf \\(\"hello\\\\n\"\\); /\\* break-here \\*/" \
53 "check the first version of the source file"
55 # Modify the original source file, and add an extra line into it.
56 # This only works locally because of the TCL commands.
57 set bkpsrc [standard_output_file $testfile].c.bkp
58 set bkpsrcfd [open $bkpsrc w]
59 set srcfd [open $srcfile r]
61 while { [gets $srcfd line] != -1 } {
62 if { [string first "break-here" $line] != -1 } {
63 # Put a "printf" line before the "break-here" line.
64 puts $bkpsrcfd " printf (\"foo\\n\"); /* new-marker */"
71 file rename -force -- $bkpsrc $srcfile
72 # We have to wait 1 second because of the way GDB checks whether the
73 # binary has changed or not. GDB uses stat(2) and currently checks
74 # 'st_mtime', whose precision is measured in seconds. Since the copy,
75 # rename, and rebuild can take less than 1 second, GDB might mistakenly
76 # assume that the binary is unchanged.
79 # Recompile the modified source. We use "gdb_compile" here instead of
80 # "prepare_for_testing" because we don't want to call "clean_restart".
81 if { [gdb_compile "${srcfile}" "${binfile}" executable {debug}] != "" } {
85 # Rerun the program. This should not only force GDB to reload the
86 # source cache, but also to break at BP_LINE again, which now has
90 "The program being debugged has been started already\\." \
91 "Start it from the beginning\\? \\(y or n\\) "]
92 set binregex [string_to_regexp $binfile]
95 "\\`$binregex\\' has changed; re-reading symbols\\.(" \
96 "Expanding full symbols from $binfile\\.\\.\\.)?" \
97 "Starting program: ${binregex}.*"]
98 gdb_test "run" $re "rerun program" $q y
100 # Again, perform the listing and check that the line indeed has
102 gdb_test "list" "${bp_line}\[ \t\]+printf \\(\"foo\\\\n\"\\); /\\\* new-marker \\\*/.*" \
103 "verify that the source code is properly reloaded"
105 # Modify the source file again. As before, this only works locally
106 # because of the TCL commands.
107 set bkpsrc [standard_output_file $testfile].c.bkp
108 set bkpsrcfd [open $bkpsrc w]
109 set srcfd [open $srcfile r]
111 while { [gets $srcfd line] != -1 } {
112 if { [string first "new-marker" $line] != -1 } {
113 # Modify the printf line that we added previously.
114 puts $bkpsrcfd " printf (\"foo\\n\"); /* new-marker updated */"
122 file rename -force -- $bkpsrc $srcfile
124 # As before, delay so that at least one second has passed. GDB still
125 # will not spot that the source file has changed, as GDB doesn't do a
126 # time check unless the binary has also changed, this delay just
127 # allows us to confirm this behaviour.
130 # List the printf line again, we should not see the file changes yet
131 # as the binary is unchanged, so the cached contents will still be
133 gdb_test "list ${bp_line}" "${bp_line}\[ \t\]+printf \\(\"foo\\\\n\"\\); /\\\* new-marker \\\*/.*" \
134 "verify that the source code change is not seen yet"
136 gdb_test "maint flush source-cache" "Source cache flushed\\."
138 # List the printf line again. After the cache flush GDB will re-read
139 # the source file and we should now see the changes.
140 gdb_test "list ${bp_line}" "${bp_line}\[ \t\]+printf \\(\"foo\\\\n\"\\); /\\\* new-marker updated \\\*/.*" \
141 "verify that the updated source code change is not seen"