]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/testsuite/gdb.base/cached-source-file.exp
Update copyright year range in header of all files managed by GDB
[thirdparty/binutils-gdb.git] / gdb / testsuite / gdb.base / cached-source-file.exp
1 # Copyright (C) 2020-2024 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 for PR tui/25126.
17 #
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.
21 #
22 # This bug is reproducible even without using the TUI.
23
24 standard_testfile
25
26 # Only run on native boards.
27 require !use_gdb_stub
28 if { [target_info gdb_protocol] == "extended-remote" } {
29 return -1
30 }
31
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
37 set srcfile $newsrc
38
39 if { [prepare_for_testing "failed to prepare" $testfile $srcfile] } {
40 return -1
41 }
42
43 # Get the line number for the line with the "break-here" marker.
44 set bp_line [gdb_get_line_number "break-here" $srcfile]
45
46 gdb_assert { [runto "$srcfile:$bp_line"] } \
47 "run to break-here marker"
48
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"
54
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]
60
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 */"
65 }
66 puts $bkpsrcfd $line
67 }
68
69 close $bkpsrcfd
70 close $srcfd
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.
77 sleep 1
78
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}] != "" } {
82 return -1
83 }
84
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
87 # different contents.
88 set q \
89 [multi_line \
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]
93 set re \
94 [multi_line \
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
99
100 # Again, perform the listing and check that the line indeed has
101 # changed for GDB.
102 gdb_test "list" "${bp_line}\[ \t\]+printf \\(\"foo\\\\n\"\\); /\\\* new-marker \\\*/.*" \
103 "verify that the source code is properly reloaded"
104
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]
110
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 */"
115 } else {
116 puts $bkpsrcfd $line
117 }
118 }
119
120 close $bkpsrcfd
121 close $srcfd
122 file rename -force -- $bkpsrc $srcfile
123
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.
128 sleep 1
129
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
132 # used.
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"
135
136 gdb_test "maint flush source-cache" "Source cache flushed\\."
137
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"