Home | History | Annotate | Line # | Download | only in gdb.python
      1 # Copyright (C) 2022-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 # Check that the gdb.connect_removed event triggers when we expect it
     17 # too.
     18 #
     19 # Checking this event has wider implications that simply some corner
     20 # of the Python API working or not.  The connection_removed event
     21 # triggers when the reference count of a process_stratum_target
     22 # reaches zero.  If these events stop triggering when expected then
     23 # GDB might be getting the reference counting on target_ops objects
     24 # wrong.
     25 
     26 load_lib gdb-python.exp
     27 
     28 require allow_python_tests
     29 
     30 standard_testfile py-connection.c
     31 
     32 if { [prepare_for_testing "failed to prepare" ${testfile} ${srcfile}] } {
     33     return -1
     34 }
     35 
     36 if ![runto_main] then {
     37     return 0
     38 }
     39 
     40 # Register a callback that will trigger when a connection is removed
     41 # (deleted) within GDB.
     42 gdb_test_multiline "Add connection_removed event" \
     43     "python" "" \
     44     "def connection_removed_handler(event):" "" \
     45     "   num = event.connection.num" "" \
     46     "   type = event.connection.type" "" \
     47     "   print('Connection %d (%s) removed' % (num, type))" "" \
     48     "gdb.events.connection_removed.connect (connection_removed_handler)" "" \
     49     "end" ""
     50 
     51 if { [target_info exists gdb_protocol] } {
     52     if { [target_info gdb_protocol] == "extended-remote" } {
     53 	set connection_type "extended-remote"
     54     } else {
     55 	set connection_type "remote"
     56     }
     57 } else {
     58     set connection_type "native"
     59 }
     60 
     61 # Add an inferior that shares a connection with inferior 1.
     62 gdb_test "add-inferior" "Added inferior 2 on connection 1 \[^\r\n\]+"
     63 
     64 # Add an inferior with no connection.
     65 gdb_test "add-inferior -no-connection" "Added inferior 3"
     66 
     67 # Kill the first inferior.  If we are using the plain 'remote' protocol then
     68 # it as this point that the remote target connection is removed.  For the
     69 # 'extended-remote' and 'native' targets the connection is removed later.
     70 if { $connection_type == "remote" } {
     71     gdb_test "with confirm off -- kill" \
     72 	"Connection 1 \\(remote\\) removed\r\n.*" "kill inferior"
     73 } else {
     74     gdb_test "with confirm off -- kill" "" "kill inferior"
     75 }
     76 
     77 # Switch to inferior 3 (the one with no connection).
     78 gdb_test "inferior 3"
     79 
     80 # Remove inferior 1, not expecting anything interesting at this point.
     81 gdb_test_no_output "remove-inferiors 1"
     82 
     83 # Now removed inferior 2.  For the 'remote' target the connection has
     84 # already been removed (see above), but for 'extended-remote' and 'native'
     85 # targets, it is at this point that the connection is removed.
     86 if { $connection_type == "remote" } {
     87     gdb_test_no_output "remove-inferiors 2"
     88 } else {
     89     gdb_test "remove-inferiors 2" \
     90 	"Connection 1 \\(${connection_type}\\) removed"
     91 }
     92