1# Copyright (C) 2020-2023 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
24standard_testfile
25
26# Only run on native boards.
27if { [use_gdb_stub] || [target_info gdb_protocol] == "extended-remote" } {
28    return -1
29}
30
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).
34set newsrc [standard_output_file $testfile].c
35file copy -force -- $srcdir/$subdir/$srcfile $newsrc
36set srcfile $newsrc
37
38if { [prepare_for_testing "failed to prepare" $testfile $srcfile] } {
39    return -1
40}
41
42# Get the line number for the line with the "break-here" marker.
43set bp_line [gdb_get_line_number "break-here" $srcfile]
44
45gdb_assert { [runto "$srcfile:$bp_line"] } \
46    "run to break-here marker"
47
48# Do a "list" and check that the printed line matches the line of the
49# original source file.
50gdb_test_no_output "set listsize 1"
51gdb_test "list" "$bp_line\[ \t\]+printf \\(\"hello\\\\n\"\\); /\\* break-here \\*/" \
52    "check the first version of the source file"
53
54# Modify the original source file, and add an extra line into it.
55# This only works locally because of the TCL commands.
56set bkpsrc [standard_output_file $testfile].c.bkp
57set bkpsrcfd [open $bkpsrc w]
58set srcfd [open $srcfile r]
59
60while { [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 */"
64    }
65    puts $bkpsrcfd $line
66}
67
68close $bkpsrcfd
69close $srcfd
70file 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.
76sleep 1
77
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".
80if { [gdb_compile "${srcfile}" "${binfile}" executable {debug}] != "" } {
81    return -1
82}
83
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
86# different contents.
87set q \
88    [multi_line \
89	 "The program being debugged has been started already\\." \
90	 "Start it from the beginning\\? \\(y or n\\) "]
91set binregex [string_to_regexp $binfile]
92set re \
93    [multi_line \
94	 "\\`$binregex\\' has changed; re-reading symbols\\.(" \
95	 "Expanding full symbols from $binfile\\.\\.\\.)?" \
96	 "Starting program: ${binregex}.*"]
97gdb_test "run" $re "rerun program" $q y
98
99# Again, perform the listing and check that the line indeed has
100# changed for GDB.
101gdb_test "list" "${bp_line}\[ \t\]+printf \\(\"foo\\\\n\"\\); /\\\* new-marker \\\*/.*" \
102    "verify that the source code is properly reloaded"
103
104# Modify the source file again.  As before, this only works locally
105# because of the TCL commands.
106set bkpsrc [standard_output_file $testfile].c.bkp
107set bkpsrcfd [open $bkpsrc w]
108set srcfd [open $srcfile r]
109
110while { [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 */"
114    } else {
115	puts $bkpsrcfd $line
116    }
117}
118
119close $bkpsrcfd
120close $srcfd
121file rename -force -- $bkpsrc $srcfile
122
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.
127sleep 1
128
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
131# used.
132gdb_test "list ${bp_line}" "${bp_line}\[ \t\]+printf \\(\"foo\\\\n\"\\); /\\\* new-marker \\\*/.*" \
133    "verify that the source code change is not seen yet"
134
135gdb_test "maint flush source-cache" "Source cache flushed\\."
136
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.
139gdb_test "list ${bp_line}" "${bp_line}\[ \t\]+printf \\(\"foo\\\\n\"\\); /\\\* new-marker updated \\\*/.*" \
140    "verify that the updated source code change is not seen"
141