Home | History | Annotate | Line # | Download | only in gdb.python
      1  1.1  christos # Copyright (C) 2023-2024 Free Software Foundation, Inc.
      2  1.1  christos 
      3  1.1  christos # This program is free software; you can redistribute it and/or modify
      4  1.1  christos # it under the terms of the GNU General Public License as published by
      5  1.1  christos # the Free Software Foundation; either version 3 of the License, or
      6  1.1  christos # (at your option) any later version.
      7  1.1  christos #
      8  1.1  christos # This program is distributed in the hope that it will be useful,
      9  1.1  christos # but WITHOUT ANY WARRANTY; without even the implied warranty of
     10  1.1  christos # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     11  1.1  christos # GNU General Public License for more details.
     12  1.1  christos #
     13  1.1  christos # You should have received a copy of the GNU General Public License
     14  1.1  christos # along with this program.  If not, see <http://www.gnu.org/licenses/>.
     15  1.1  christos 
     16  1.1  christos require allow_python_tests
     17  1.1  christos 
     18  1.1  christos load_lib gdb-python.exp
     19  1.1  christos 
     20  1.1  christos standard_testfile
     21  1.1  christos 
     22  1.1  christos set binfile1 ${binfile}-a
     23  1.1  christos set binfile2 ${binfile}-b
     24  1.1  christos 
     25  1.1  christos if {[build_executable "failed to prepare first executable" \
     26  1.1  christos 	 $binfile1 $srcfile]} {
     27  1.1  christos     return -1
     28  1.1  christos }
     29  1.1  christos 
     30  1.1  christos if {[build_executable "failed to prepare second executable" \
     31  1.1  christos 	 $binfile2 $srcfile]} {
     32  1.1  christos     return -1
     33  1.1  christos }
     34  1.1  christos 
     35  1.1  christos set binfile1 [gdb_remote_download host $binfile1]
     36  1.1  christos set binfile2 [gdb_remote_download host $binfile2]
     37  1.1  christos 
     38  1.1  christos # Setup a Python function to listen for the executable changed event.
     39  1.1  christos proc setup_exec_change_handler {} {
     40  1.1  christos     gdb_py_test_silent_cmd \
     41  1.1  christos 	[multi_line \
     42  1.1  christos 	     "python" \
     43  1.1  christos 	     "def reset_state():" \
     44  1.1  christos 	     "   global exec_changed_state" \
     45  1.1  christos 	     "   exec_changed_state = \[0, None, None\]" \
     46  1.1  christos 	     "end" ] \
     47  1.1  christos 	"build reset_state function" 0
     48  1.1  christos 
     49  1.1  christos     gdb_py_test_silent_cmd \
     50  1.1  christos 	[multi_line \
     51  1.1  christos 	     "python" \
     52  1.1  christos 	     "def executable_changed(event):" \
     53  1.1  christos 	     "   global exec_changed_state" \
     54  1.1  christos 	     "   exec_changed_state\[0\] += 1" \
     55  1.1  christos 	     "   exec_changed_state\[1\] = event.progspace.executable_filename" \
     56  1.1  christos 	     "   exec_changed_state\[2\] = event.reload" \
     57  1.1  christos 	     "end" ] \
     58  1.1  christos 	"build executable_changed function" 0
     59  1.1  christos 
     60  1.1  christos     gdb_test_no_output -nopass "python reset_state()"
     61  1.1  christos     gdb_test_no_output "python gdb.events.executable_changed.connect(executable_changed)"
     62  1.1  christos }
     63  1.1  christos 
     64  1.1  christos # Check the global Python state that is updated when the
     65  1.1  christos # executable_changed event occurs, and then reset the global state.
     66  1.1  christos # FILENAME is a string, the name of the new executable file.  RELOAD
     67  1.1  christos # is a string, which should be 'True' or 'False', and represents if
     68  1.1  christos # the executable file was reloaded, or changed.
     69  1.1  christos proc check_exec_change { filename_re reload testname } {
     70  1.1  christos     if { $filename_re ne "None" } {
     71  1.1  christos 	set filename_re "'$filename_re'"
     72  1.1  christos     }
     73  1.1  christos     if { $filename_re eq "None" && $reload eq "None" } {
     74  1.1  christos 	set count 0
     75  1.1  christos     } else {
     76  1.1  christos 	set count 1
     77  1.1  christos     }
     78  1.1  christos     gdb_test "python print(exec_changed_state)" \
     79  1.1  christos 	"\\\[$count, $filename_re, $reload\\\]" \
     80  1.1  christos 	$testname
     81  1.1  christos     gdb_test_no_output -nopass "python reset_state()"
     82  1.1  christos }
     83  1.1  christos 
     84  1.1  christos # Check that the executable_filename is set correctly after using the
     85  1.1  christos # 'file' command.
     86  1.1  christos with_test_prefix "using 'file' command" {
     87  1.1  christos     clean_restart
     88  1.1  christos 
     89  1.1  christos     setup_exec_change_handler
     90  1.1  christos 
     91  1.1  christos     gdb_test "python print(gdb.current_progspace().executable_filename)" \
     92  1.1  christos 	"None" \
     93  1.1  christos 	"check executable_filename when no file is loaded"
     94  1.1  christos 
     95  1.1  christos     gdb_test "file $binfile1" \
     96  1.1  christos 	"Reading symbols from [string_to_regexp $binfile1]\\.\\.\\..*" \
     97  1.1  christos 	"load first executable"
     98  1.1  christos     gdb_test "python print(gdb.current_progspace().executable_filename)" \
     99  1.1  christos 	"[string_to_regexp $binfile1]" \
    100  1.1  christos 	"check executable_filename when first executable is loaded"
    101  1.1  christos 
    102  1.1  christos     check_exec_change [string_to_regexp $binfile1] False \
    103  1.1  christos 	"check executable_changed state after first executable was loaded"
    104  1.1  christos 
    105  1.1  christos     gdb_test "file $binfile2" \
    106  1.1  christos 	"Reading symbols from [string_to_regexp $binfile2]\\.\\.\\..*" \
    107  1.1  christos 	"load second executable" \
    108  1.1  christos 	"Load new symbol table from .*\? .y or n. " "y"
    109  1.1  christos     gdb_test "python print(gdb.current_progspace().executable_filename)" \
    110  1.1  christos 	"[string_to_regexp $binfile2]" \
    111  1.1  christos 	"check executable_filename when second executable is loaded"
    112  1.1  christos 
    113  1.1  christos     check_exec_change [string_to_regexp $binfile2] False \
    114  1.1  christos 	"check executable_changed state after second executable was loaded"
    115  1.1  christos 
    116  1.1  christos     gdb_unload
    117  1.1  christos     gdb_test "python print(gdb.current_progspace().executable_filename)" \
    118  1.1  christos 	"None" \
    119  1.1  christos 	"check executable_filename after unloading file"
    120  1.1  christos 
    121  1.1  christos     check_exec_change None False \
    122  1.1  christos 	"check executable_changed state after unloading the executable"
    123  1.1  christos }
    124  1.1  christos 
    125  1.1  christos # Check that the executable_filename is correctly set when we only set
    126  1.1  christos # the exec-file.
    127  1.1  christos with_test_prefix "using 'exec-file' command" {
    128  1.1  christos     clean_restart
    129  1.1  christos 
    130  1.1  christos     setup_exec_change_handler
    131  1.1  christos 
    132  1.1  christos     gdb_test_no_output "exec-file $binfile1" \
    133  1.1  christos 	"load first executable"
    134  1.1  christos     gdb_test "python print(gdb.current_progspace().executable_filename)" \
    135  1.1  christos 	"[string_to_regexp $binfile1]" \
    136  1.1  christos 	"check executable_filename when first executable is loaded"
    137  1.1  christos 
    138  1.1  christos     check_exec_change [string_to_regexp $binfile1] False \
    139  1.1  christos 	"check executable_changed state after first executable was loaded"
    140  1.1  christos 
    141  1.1  christos     gdb_test_no_output "exec-file $binfile2" \
    142  1.1  christos 	"load second executable"
    143  1.1  christos     gdb_test "python print(gdb.current_progspace().executable_filename)" \
    144  1.1  christos 	"[string_to_regexp $binfile2]" \
    145  1.1  christos 	"check executable_filename when second executable is loaded"
    146  1.1  christos 
    147  1.1  christos     check_exec_change [string_to_regexp $binfile2] False \
    148  1.1  christos 	"check executable_changed state after second executable was loaded"
    149  1.1  christos 
    150  1.1  christos     gdb_test "exec-file" "No executable file now\\."
    151  1.1  christos     gdb_test "python print(gdb.current_progspace().executable_filename)" \
    152  1.1  christos 	"None" \
    153  1.1  christos 	"check executable_filename after unloading file"
    154  1.1  christos 
    155  1.1  christos     check_exec_change None False \
    156  1.1  christos 	"check executable_changed state after unloading the executable"
    157  1.1  christos }
    158  1.1  christos 
    159  1.1  christos # Check that setting the symbol-file doesn't cause the
    160  1.1  christos # executable_filename to be set.
    161  1.1  christos with_test_prefix "using 'symbol-file' command" {
    162  1.1  christos     clean_restart
    163  1.1  christos 
    164  1.1  christos     setup_exec_change_handler
    165  1.1  christos 
    166  1.1  christos     gdb_test "symbol-file $binfile1" \
    167  1.1  christos 	"Reading symbols from [string_to_regexp $binfile1]\\.\\.\\..*" \
    168  1.1  christos 	"load first executable"
    169  1.1  christos     gdb_test "python print(gdb.current_progspace().executable_filename)" \
    170  1.1  christos 	"None" \
    171  1.1  christos 	"check executable_filename after setting symbol-file"
    172  1.1  christos 
    173  1.1  christos     check_exec_change None None \
    174  1.1  christos 	"check executable_changed state after setting symbol-file"
    175  1.1  christos }
    176  1.1  christos 
    177  1.1  christos # Check the executable_changed event when the executable changes on disk.
    178  1.1  christos with_test_prefix "exec changes on disk" {
    179  1.1  christos     clean_restart $binfile1
    180  1.1  christos 
    181  1.1  christos     setup_exec_change_handler
    182  1.1  christos 
    183  1.1  christos     runto_main
    184  1.1  christos 
    185  1.1  christos     gdb_test_no_output "shell sleep 1" \
    186  1.1  christos 	"ensure executable is at least 1 second old"
    187  1.1  christos 
    188  1.1  christos     gdb_test "shell touch ${binfile1}" "" \
    189  1.1  christos 	"update the executable on disk"
    190  1.1  christos 
    191  1.1  christos     runto_main
    192  1.1  christos 
    193  1.1  christos     check_exec_change [string_to_regexp $binfile1] True \
    194  1.1  christos 	"check executable_changed state after exec changed on disk"
    195  1.1  christos }
    196