aboutsummaryrefslogtreecommitdiff
path: root/gdb/testsuite
diff options
context:
space:
mode:
authorAndrew Burgess <aburgess@redhat.com>2023-08-28 16:22:36 +0100
committerAndrew Burgess <aburgess@redhat.com>2023-09-28 15:33:13 +0100
commit42f297ad36a13a7774fede3a8119e6c56ef6d318 (patch)
tree9d5ef57d0ec2eb14ce05975b670e8fce7628c99e /gdb/testsuite
parent063453b199e1291a03ee81c3422c31d7cca60af6 (diff)
downloadgdb-42f297ad36a13a7774fede3a8119e6c56ef6d318.zip
gdb-42f297ad36a13a7774fede3a8119e6c56ef6d318.tar.gz
gdb-42f297ad36a13a7774fede3a8119e6c56ef6d318.tar.bz2
gdb/python: make the executable_changed event available from Python
This commit makes the executable_changed observable available through the Python API as an event. There's nothing particularly interesting going on here, it just follows the same pattern as many of the other Python events we support. The new event registry is called events.executable_changed, and this emits an ExecutableChangedEvent object which has two attributes, a gdb.Progspace called 'progspace', this is the program space in which the executable changed, and a Boolean called 'reload', which is True if the same executable changed on disk and has been reloaded, or is False when a new executable has been loaded. One interesting thing did come up during testing though, you'll notice the test contains a setup_kfail call. During testing I observed that the executable_changed event would trigger twice when GDB restarted an inferior. However, the ExecutableChangedEvent object is identical for both calls, so the wrong information is never sent out, we just see one too many events. I tracked this down to how the reload_symbols function (symfile.c) takes care to also reload the executable, however, I've split fixing this into a separate commit, so see the next commit for details. Reviewed-By: Eli Zaretskii <eliz@gnu.org> Approved-By: Tom Tromey <tom@tromey.com>
Diffstat (limited to 'gdb/testsuite')
-rw-r--r--gdb/testsuite/gdb.python/py-exec-file.exp100
1 files changed, 100 insertions, 0 deletions
diff --git a/gdb/testsuite/gdb.python/py-exec-file.exp b/gdb/testsuite/gdb.python/py-exec-file.exp
index 14e5088..5ad3cd7 100644
--- a/gdb/testsuite/gdb.python/py-exec-file.exp
+++ b/gdb/testsuite/gdb.python/py-exec-file.exp
@@ -35,11 +35,59 @@ if {[build_executable "failed to prepare second executable" \
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"
@@ -51,6 +99,9 @@ with_test_prefix "using 'file' command" {
"[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" \
@@ -59,42 +110,91 @@ with_test_prefix "using 'file' command" {
"[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
+
+ # There is currently an issue where the executable_changed event
+ # will trigger twice during an inferior restart. This should be
+ # fixed in the next commit, at which point this kfail can be
+ # removed.
+ setup_kfail "????" *-*-*
+ check_exec_change [string_to_regexp $binfile1] True \
+ "check executable_changed state after exec changed on disk"
}