Home | History | Annotate | Line # | Download | only in gdb.python
      1 # Copyright (C) 2023-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 require allow_python_tests
     17 
     18 load_lib gdb-python.exp
     19 
     20 standard_testfile
     21 
     22 set binfile1 ${binfile}-a
     23 set binfile2 ${binfile}-b
     24 
     25 if {[build_executable "failed to prepare first executable" \
     26 	 $binfile1 $srcfile]} {
     27     return -1
     28 }
     29 
     30 if {[build_executable "failed to prepare second executable" \
     31 	 $binfile2 $srcfile]} {
     32     return -1
     33 }
     34 
     35 set binfile1 [gdb_remote_download host $binfile1]
     36 set binfile2 [gdb_remote_download host $binfile2]
     37 
     38 # Setup a Python function to listen for the executable changed event.
     39 proc setup_exec_change_handler {} {
     40     gdb_py_test_silent_cmd \
     41 	[multi_line \
     42 	     "python" \
     43 	     "def reset_state():" \
     44 	     "   global exec_changed_state" \
     45 	     "   exec_changed_state = \[0, None, None\]" \
     46 	     "end" ] \
     47 	"build reset_state function" 0
     48 
     49     gdb_py_test_silent_cmd \
     50 	[multi_line \
     51 	     "python" \
     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" \
     57 	     "end" ] \
     58 	"build executable_changed function" 0
     59 
     60     gdb_test_no_output -nopass "python reset_state()"
     61     gdb_test_no_output "python gdb.events.executable_changed.connect(executable_changed)"
     62 }
     63 
     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'"
     72     }
     73     if { $filename_re eq "None" && $reload eq "None" } {
     74 	set count 0
     75     } else {
     76 	set count 1
     77     }
     78     gdb_test "python print(exec_changed_state)" \
     79 	"\\\[$count, $filename_re, $reload\\\]" \
     80 	$testname
     81     gdb_test_no_output -nopass "python reset_state()"
     82 }
     83 
     84 # Check that the executable_filename is set correctly after using the
     85 # 'file' command.
     86 with_test_prefix "using 'file' command" {
     87     clean_restart
     88 
     89     setup_exec_change_handler
     90 
     91     gdb_test "python print(gdb.current_progspace().executable_filename)" \
     92 	"None" \
     93 	"check executable_filename when no file is loaded"
     94 
     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"
    101 
    102     check_exec_change [string_to_regexp $binfile1] False \
    103 	"check executable_changed state after first executable was loaded"
    104 
    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"
    112 
    113     check_exec_change [string_to_regexp $binfile2] False \
    114 	"check executable_changed state after second executable was loaded"
    115 
    116     gdb_unload
    117     gdb_test "python print(gdb.current_progspace().executable_filename)" \
    118 	"None" \
    119 	"check executable_filename after unloading file"
    120 
    121     check_exec_change None False \
    122 	"check executable_changed state after unloading the executable"
    123 }
    124 
    125 # Check that the executable_filename is correctly set when we only set
    126 # the exec-file.
    127 with_test_prefix "using 'exec-file' command" {
    128     clean_restart
    129 
    130     setup_exec_change_handler
    131 
    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"
    137 
    138     check_exec_change [string_to_regexp $binfile1] False \
    139 	"check executable_changed state after first executable was loaded"
    140 
    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"
    146 
    147     check_exec_change [string_to_regexp $binfile2] False \
    148 	"check executable_changed state after second executable was loaded"
    149 
    150     gdb_test "exec-file" "No executable file now\\."
    151     gdb_test "python print(gdb.current_progspace().executable_filename)" \
    152 	"None" \
    153 	"check executable_filename after unloading file"
    154 
    155     check_exec_change None False \
    156 	"check executable_changed state after unloading the executable"
    157 }
    158 
    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" {
    162     clean_restart
    163 
    164     setup_exec_change_handler
    165 
    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)" \
    170 	"None" \
    171 	"check executable_filename after setting symbol-file"
    172 
    173     check_exec_change None None \
    174 	"check executable_changed state after setting symbol-file"
    175 }
    176 
    177 # Check the executable_changed event when the executable changes on disk.
    178 with_test_prefix "exec changes on disk" {
    179     clean_restart $binfile1
    180 
    181     setup_exec_change_handler
    182 
    183     runto_main
    184 
    185     gdb_test_no_output "shell sleep 1" \
    186 	"ensure executable is at least 1 second old"
    187 
    188     gdb_test "shell touch ${binfile1}" "" \
    189 	"update the executable on disk"
    190 
    191     runto_main
    192 
    193     check_exec_change [string_to_regexp $binfile1] True \
    194 	"check executable_changed state after exec changed on disk"
    195 }
    196