1 # Copyright (C) 2023-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 require allow_python_tests
18 load_lib gdb-python.exp
22 set binfile1 ${binfile}-a
23 set binfile2 ${binfile}-b
25 if {[build_executable "failed to prepare first executable" \
26 $binfile1 $srcfile]} {
30 if {[build_executable "failed to prepare second executable" \
31 $binfile2 $srcfile]} {
35 set binfile1 [gdb_remote_download host $binfile1]
36 set binfile2 [gdb_remote_download host $binfile2]
38 # Setup a Python function to listen for the executable changed event.
39 proc setup_exec_change_handler {} {
40 gdb_py_test_silent_cmd \
43 "def reset_state():" \
44 " global exec_changed_state" \
45 " exec_changed_state = \[0, None, None\]" \
47 "build reset_state function" 0
49 gdb_py_test_silent_cmd \
52 "def executable_changed(event):" \
53 " global exec_changed_state" \
54 " exec_changed_state\[0\] += 1" \
55 " exec_changed_state\[1\] = event.progspace.executable_filename" \
56 " exec_changed_state\[2\] = event.reload" \
58 "build executable_changed function" 0
60 gdb_test_no_output -nopass "python reset_state()"
61 gdb_test_no_output "python gdb.events.executable_changed.connect(executable_changed)"
64 # Check the global Python state that is updated when the
65 # executable_changed event occurs, and then reset the global state.
66 # FILENAME is a string, the name of the new executable file. RELOAD
67 # is a string, which should be 'True' or 'False', and represents if
68 # the executable file was reloaded, or changed.
69 proc check_exec_change { filename_re reload testname } {
70 if { $filename_re ne "None" } {
71 set filename_re "'$filename_re'"
73 if { $filename_re eq "None" && $reload eq "None" } {
78 gdb_test "python print(exec_changed_state)" \
79 "\\\[$count, $filename_re, $reload\\\]" \
81 gdb_test_no_output -nopass "python reset_state()"
84 # Check that the executable_filename is set correctly after using the
86 with_test_prefix "using 'file' command" {
89 setup_exec_change_handler
91 gdb_test "python print(gdb.current_progspace().executable_filename)" \
93 "check executable_filename when no file is loaded"
95 gdb_test "file $binfile1" \
96 "Reading symbols from [string_to_regexp $binfile1]\\.\\.\\..*" \
97 "load first executable"
98 gdb_test "python print(gdb.current_progspace().executable_filename)" \
99 "[string_to_regexp $binfile1]" \
100 "check executable_filename when first executable is loaded"
102 check_exec_change [string_to_regexp $binfile1] False \
103 "check executable_changed state after first executable was loaded"
105 gdb_test "file $binfile2" \
106 "Reading symbols from [string_to_regexp $binfile2]\\.\\.\\..*" \
107 "load second executable" \
108 "Load new symbol table from .*\? .y or n. " "y"
109 gdb_test "python print(gdb.current_progspace().executable_filename)" \
110 "[string_to_regexp $binfile2]" \
111 "check executable_filename when second executable is loaded"
113 check_exec_change [string_to_regexp $binfile2] False \
114 "check executable_changed state after second executable was loaded"
117 gdb_test "python print(gdb.current_progspace().executable_filename)" \
119 "check executable_filename after unloading file"
121 check_exec_change None False \
122 "check executable_changed state after unloading the executable"
125 # Check that the executable_filename is correctly set when we only set
127 with_test_prefix "using 'exec-file' command" {
130 setup_exec_change_handler
132 gdb_test_no_output "exec-file $binfile1" \
133 "load first executable"
134 gdb_test "python print(gdb.current_progspace().executable_filename)" \
135 "[string_to_regexp $binfile1]" \
136 "check executable_filename when first executable is loaded"
138 check_exec_change [string_to_regexp $binfile1] False \
139 "check executable_changed state after first executable was loaded"
141 gdb_test_no_output "exec-file $binfile2" \
142 "load second executable"
143 gdb_test "python print(gdb.current_progspace().executable_filename)" \
144 "[string_to_regexp $binfile2]" \
145 "check executable_filename when second executable is loaded"
147 check_exec_change [string_to_regexp $binfile2] False \
148 "check executable_changed state after second executable was loaded"
150 gdb_test "exec-file" "No executable file now\\."
151 gdb_test "python print(gdb.current_progspace().executable_filename)" \
153 "check executable_filename after unloading file"
155 check_exec_change None False \
156 "check executable_changed state after unloading the executable"
159 # Check that setting the symbol-file doesn't cause the
160 # executable_filename to be set.
161 with_test_prefix "using 'symbol-file' command" {
164 setup_exec_change_handler
166 gdb_test "symbol-file $binfile1" \
167 "Reading symbols from [string_to_regexp $binfile1]\\.\\.\\..*" \
168 "load first executable"
169 gdb_test "python print(gdb.current_progspace().executable_filename)" \
171 "check executable_filename after setting symbol-file"
173 check_exec_change None None \
174 "check executable_changed state after setting symbol-file"
177 # Check the executable_changed event when the executable changes on disk.
178 with_test_prefix "exec changes on disk" {
179 clean_restart $binfile1
181 setup_exec_change_handler
185 gdb_test_no_output "shell sleep 1" \
186 "ensure executable is at least 1 second old"
188 gdb_test "shell touch ${binfile1}" "" \
189 "update the executable on disk"
193 check_exec_change [string_to_regexp $binfile1] True \
194 "check executable_changed state after exec changed on disk"