Files
binutils-gdb/gdb/testsuite/gdb.python/py-exec-file.exp
Andrew Burgess c7bdb38baf gdb: use reopen_exec_file from reread_symbols
This commit fixes an issue that was discovered while writing the tests
for the previous commit.

I noticed that, when GDB restarts an inferior, the executable_changed
event would trigger twice.  The first notification would originate
from:

  #0  exec_file_attach (filename=0x4046680 "/tmp/hello.x", from_tty=0) at ../../src/gdb/exec.c:513
  #1  0x00000000006f3adb in reopen_exec_file () at ../../src/gdb/corefile.c:122
  #2  0x0000000000e6a3f2 in generic_mourn_inferior () at ../../src/gdb/target.c:3682
  #3  0x0000000000995121 in inf_child_target::mourn_inferior (this=0x2fe95c0 <the_amd64_linux_nat_target>) at ../../src/gdb/inf-child.c:192
  #4  0x0000000000995cff in inf_ptrace_target::mourn_inferior (this=0x2fe95c0 <the_amd64_linux_nat_target>) at ../../src/gdb/inf-ptrace.c:125
  #5  0x0000000000a32472 in linux_nat_target::mourn_inferior (this=0x2fe95c0 <the_amd64_linux_nat_target>) at ../../src/gdb/linux-nat.c:3609
  #6  0x0000000000e68a40 in target_mourn_inferior (ptid=...) at ../../src/gdb/target.c:2761
  #7  0x0000000000a323ec in linux_nat_target::kill (this=0x2fe95c0 <the_amd64_linux_nat_target>) at ../../src/gdb/linux-nat.c:3593
  #8  0x0000000000e64d1c in target_kill () at ../../src/gdb/target.c:924
  #9  0x00000000009a19bc in kill_if_already_running (from_tty=1) at ../../src/gdb/infcmd.c:328
  #10 0x00000000009a1a6f in run_command_1 (args=0x0, from_tty=1, run_how=RUN_STOP_AT_MAIN) at ../../src/gdb/infcmd.c:381
  #11 0x00000000009a20a5 in start_command (args=0x0, from_tty=1) at ../../src/gdb/infcmd.c:527
  #12 0x000000000068dc5d in do_simple_func (args=0x0, from_tty=1, c=0x35c7200) at ../../src/gdb/cli/cli-decode.c:95

While the second originates from:

  #0  exec_file_attach (filename=0x3d7a1d0 "/tmp/hello.x", from_tty=0) at ../../src/gdb/exec.c:513
  #1  0x0000000000dfe525 in reread_symbols (from_tty=1) at ../../src/gdb/symfile.c:2517
  #2  0x00000000009a1a98 in run_command_1 (args=0x0, from_tty=1, run_how=RUN_STOP_AT_MAIN) at ../../src/gdb/infcmd.c:398
  #3  0x00000000009a20a5 in start_command (args=0x0, from_tty=1) at ../../src/gdb/infcmd.c:527
  #4  0x000000000068dc5d in do_simple_func (args=0x0, from_tty=1, c=0x35c7200) at ../../src/gdb/cli/cli-decode.c:95

In the first case the call to exec_file_attach first passes through
reopen_exec_file.  The reopen_exec_file performs a modification time
check on the executable file, and only calls exec_file_attach if the
executable has changed on disk since it was last loaded.

However, in the second case things work a little differently.  In this
case GDB is really trying to reread the debug symbol.  As such, we
iterate over the objfiles list, and for each of those we check the
modification time, if the file on disk has changed then we reload the
debug symbols from that file.

However, there is an additional check, if the objfile has the same
name as the executable then we will call exec_file_attach, but we do
so without checking the cached modification time that indicates when
the executable was last reloaded, as a result, we reload the
executable twice.

In this commit I propose that reread_symbols be changed to
unconditionally call reopen_exec_file before performing the objfile
iteration.  This will ensure that, if the executable has changed, then
the executable will be reloaded, however, if the executable has
already been recently reloaded, we will not reload it for a second
time.

After handling the executable, GDB can then iterate over the objfiles
list and reload them in the normal way.

With this done I now see the executable reloaded only once when GDB
restarts an inferior, which means I can remove the kfail that I added
to the gdb.python/py-exec-file.exp test in the previous commit.

Approved-By: Tom Tromey <tom@tromey.com>
2023-09-28 15:33:13 +01:00

196 lines
6.3 KiB
Plaintext

# Copyright (C) 2023 Free Software Foundation, Inc.
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
require allow_python_tests
load_lib gdb-python.exp
standard_testfile
set binfile1 ${binfile}-a
set binfile2 ${binfile}-b
if {[build_executable "failed to prepare first executable" \
$binfile1 $srcfile]} {
return -1
}
if {[build_executable "failed to prepare second executable" \
$binfile2 $srcfile]} {
return -1
}
set binfile1 [gdb_remote_download host $binfile1]
set binfile2 [gdb_remote_download host $binfile2]
# Setup a Python function to listen for the executable changed event.
proc setup_exec_change_handler {} {
gdb_py_test_silent_cmd \
[multi_line \
"python" \
"def reset_state():" \
" global exec_changed_state" \
" exec_changed_state = \[0, None, None\]" \
"end" ] \
"build reset_state function" 0
gdb_py_test_silent_cmd \
[multi_line \
"python" \
"def executable_changed(event):" \
" global exec_changed_state" \
" exec_changed_state\[0\] += 1" \
" exec_changed_state\[1\] = event.progspace.executable_filename" \
" exec_changed_state\[2\] = event.reload" \
"end" ] \
"build executable_changed function" 0
gdb_test_no_output -nopass "python reset_state()"
gdb_test_no_output "python gdb.events.executable_changed.connect(executable_changed)"
}
# Check the global Python state that is updated when the
# executable_changed event occurs, and then reset the global state.
# FILENAME is a string, the name of the new executable file. RELOAD
# is a string, which should be 'True' or 'False', and represents if
# the executable file was reloaded, or changed.
proc check_exec_change { filename_re reload testname } {
if { $filename_re ne "None" } {
set filename_re "'$filename_re'"
}
if { $filename_re eq "None" && $reload eq "None" } {
set count 0
} else {
set count 1
}
gdb_test "python print(exec_changed_state)" \
"\\\[$count, $filename_re, $reload\\\]" \
$testname
gdb_test_no_output -nopass "python reset_state()"
}
# Check that the executable_filename is set correctly after using the
# 'file' command.
with_test_prefix "using 'file' command" {
clean_restart
setup_exec_change_handler
gdb_test "python print(gdb.current_progspace().executable_filename)" \
"None" \
"check executable_filename when no file is loaded"
gdb_test "file $binfile1" \
"Reading symbols from [string_to_regexp $binfile1]\\.\\.\\..*" \
"load first executable"
gdb_test "python print(gdb.current_progspace().executable_filename)" \
"[string_to_regexp $binfile1]" \
"check executable_filename when first executable is loaded"
check_exec_change [string_to_regexp $binfile1] False \
"check executable_changed state after first executable was loaded"
gdb_test "file $binfile2" \
"Reading symbols from [string_to_regexp $binfile2]\\.\\.\\..*" \
"load second executable" \
"Load new symbol table from .*\? .y or n. " "y"
gdb_test "python print(gdb.current_progspace().executable_filename)" \
"[string_to_regexp $binfile2]" \
"check executable_filename when second executable is loaded"
check_exec_change [string_to_regexp $binfile2] False \
"check executable_changed state after second executable was loaded"
gdb_unload
gdb_test "python print(gdb.current_progspace().executable_filename)" \
"None" \
"check executable_filename after unloading file"
check_exec_change None False \
"check executable_changed state after unloading the executable"
}
# Check that the executable_filename is correctly set when we only set
# the exec-file.
with_test_prefix "using 'exec-file' command" {
clean_restart
setup_exec_change_handler
gdb_test_no_output "exec-file $binfile1" \
"load first executable"
gdb_test "python print(gdb.current_progspace().executable_filename)" \
"[string_to_regexp $binfile1]" \
"check executable_filename when first executable is loaded"
check_exec_change [string_to_regexp $binfile1] False \
"check executable_changed state after first executable was loaded"
gdb_test_no_output "exec-file $binfile2" \
"load second executable"
gdb_test "python print(gdb.current_progspace().executable_filename)" \
"[string_to_regexp $binfile2]" \
"check executable_filename when second executable is loaded"
check_exec_change [string_to_regexp $binfile2] False \
"check executable_changed state after second executable was loaded"
gdb_test "exec-file" "No executable file now\\."
gdb_test "python print(gdb.current_progspace().executable_filename)" \
"None" \
"check executable_filename after unloading file"
check_exec_change None False \
"check executable_changed state after unloading the executable"
}
# Check that setting the symbol-file doesn't cause the
# executable_filename to be set.
with_test_prefix "using 'symbol-file' command" {
clean_restart
setup_exec_change_handler
gdb_test "symbol-file $binfile1" \
"Reading symbols from [string_to_regexp $binfile1]\\.\\.\\..*" \
"load first executable"
gdb_test "python print(gdb.current_progspace().executable_filename)" \
"None" \
"check executable_filename after setting symbol-file"
check_exec_change None None \
"check executable_changed state after setting symbol-file"
}
# Check the executable_changed event when the executable changes on disk.
with_test_prefix "exec changes on disk" {
clean_restart $binfile1
setup_exec_change_handler
runto_main
gdb_test_no_output "shell sleep 1" \
"ensure executable is at least 1 second old"
gdb_test "shell touch ${binfile1}" "" \
"update the executable on disk"
runto_main
check_exec_change [string_to_regexp $binfile1] True \
"check executable_changed state after exec changed on disk"
}