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 # This file is part of the GDB testsuite. It tests the program space 17 # related events in the Python API. 18 19 load_lib gdb-python.exp 20 21 require allow_python_tests 22 23 standard_testfile 24 25 if {[prepare_for_testing "preparing" $testfile $srcfile] == -1} { 26 return -1 27 } 28 29 set pyfile [gdb_remote_download host ${srcdir}/${subdir}/py-progspace-events.py] 30 gdb_test_no_output "source ${pyfile}" "load python file" 31 32 if {![runto_main]} { 33 return 34 } 35 36 gdb_breakpoint breakpt 37 38 gdb_continue_to_breakpoint "run to breakpt function" 39 40 gdb_test_no_output "set detach-on-fork off" 41 42 # Continue until the parent process forks and a new child is added. 43 # Done this way so we can count the new progspace events; we expect to 44 # see exactly one. 45 set new_progspace_event_count 0 46 gdb_test_multiple "continue" "continue until child process appears" { 47 -re "^NewProgspaceEvent: <gdb.Progspace object at $hex>\r\n" { 48 # This is a correctly formed event line. 49 incr new_progspace_event_count 50 exp_continue 51 } 52 53 -re "^NewProgspaceEvent:\[^\r\n\]+\r\n" { 54 # This is an incorrectly formed event line. 55 fail $gdb_test_name 56 } 57 58 -re "^$gdb_prompt $" { 59 pass $gdb_test_name 60 } 61 62 -re "^\[^\r\n\]*\r\n" { 63 exp_continue 64 } 65 } 66 67 gdb_assert { $new_progspace_event_count == 1 } \ 68 "only a single new progspace event seen" 69 70 # Switch to inferior 2 and continue until we hit breakpt. 71 gdb_test "inferior 2" "\\\[Switching to inferior 2 .*" 72 gdb_continue_to_breakpoint "run to breakpt in inferior 2" 73 74 # Let inferior 2 exit. The new program space is not removed at this 75 # point. 76 gdb_test "continue" \ 77 [multi_line \ 78 "^Continuing\\." \ 79 "\\\[Inferior $decimal \[^\r\n\]+ exited normally\\\]"] \ 80 "continue until inferior 2 exits" 81 82 gdb_test "inferior 1" "\\\[Switching to inferior 1 .*" 83 84 # Step the inferior. During this process GDB will prune the now 85 # defunct inferior, which deletes its program space, which should 86 # trigger the FreeProgspaceEvent. 87 # 88 # However, there is a slight problem. When the target is remote, and 89 # GDB is accessing files using remote fileio, then GDB will attempt to 90 # prune the inferior at a point in time when the remote target is 91 # waiting for a stop reply. Pruning an inferior causes GDB to close 92 # files associated with that inferior. 93 # 94 # In non-async mode we can't send fileio packets while waiting for a 95 # stop reply, so the attempts to close files fails, and this shows up 96 # as an error. 97 # 98 # As this error has nothing to do with the feature being tested here, 99 # we just accept the error message, the important part is the 100 # 'FreeProgspaceEvent' string, so long as that appears (just once) 101 # then the test is a success. 102 set warning_msg \ 103 [multi_line \ 104 "warning: cannot close \"\[^\r\n\]+\": Cannot execute this command while the target is running\\." \ 105 "Use the \"interrupt\" command to stop the target" \ 106 "and then try again\\."] 107 108 gdb_test "step" \ 109 [multi_line \ 110 "^FreeProgspaceEvent.*: <gdb.Progspace object at $hex>(?:\r\n$warning_msg)*" \ 111 "do_parent_stuff \\(\\) at \[^\r\n\]+" \ 112 "$decimal\\s+\[^\r\n\]+"] 113 114 # Let this inferior run to completion. 115 gdb_continue_to_end 116 117 # Check the program space events trigger when a new inferior is 118 # manually added and removed. 119 gdb_test "add-inferior" \ 120 [multi_line \ 121 "^NewProgspaceEvent: <gdb.Progspace object at $hex>" \ 122 "\\\[New inferior 3\\\]" \ 123 "Added inferior 3\[^\r\n\]*"] 124 gdb_test "remove-inferior 3" \ 125 "^FreeProgspaceEvent: <gdb.Progspace object at $hex>" 126