1# Copyright (C) 2009-2023 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 mechanism 17# exposing inferior threads to Python. 18 19load_lib gdb-python.exp 20 21standard_testfile 22 23if { [prepare_for_testing "failed to prepare" ${testfile} ${srcfile}] } { 24 return -1 25} 26 27# Skip all tests if Python scripting is not enabled. 28if { [skip_python_tests] } { continue } 29 30gdb_test_multiline "install new_thread event handler" \ 31 "python" "" \ 32 "seen_a_thread = False" "" \ 33 "def thread_handler(evt):" "" \ 34 " if (evt.inferior_thread is not None" "" \ 35 " and isinstance (evt.inferior_thread, gdb.InferiorThread)):" "" \ 36 " global seen_a_thread" "" \ 37 " seen_a_thread = True" "" \ 38 "gdb.events.new_thread.connect(thread_handler)" "" \ 39 "end" "" 40 41# The following tests require execution. 42 43if {![runto_main]} { 44 return 0 45} 46 47gdb_test "python print(seen_a_thread)" "True" 48 49# Test basic gdb.Inferior attributes and methods. 50 51# Make sure that InferiorThread.inferior returns a new reference (see PR 21213). 52 53gdb_py_test_silent_cmd "python gdb.selected_thread ().inferior" "test InferiorThread.inferior 1" 1 54gdb_py_test_silent_cmd "python gdb.selected_thread ().inferior" "test InferiorThread.inferior 2" 1 55gdb_test_no_output "python import gc; gc.collect()" "call Python garbage collection" 56gdb_py_test_silent_cmd "python gdb.selected_thread ().inferior" "test InferiorThread.inferior 3" 1 57gdb_py_test_silent_cmd "python gdb.selected_thread ().inferior" "test InferiorThread.inferior 4" 1 58 59 60gdb_py_test_silent_cmd "python t0 = gdb.selected_thread ()" "test gdb.selected_thread" 1 61gdb_test "python print (t0)" "\\<gdb.InferiorThread object at 0x\[\[:xdigit:\]\]+>" "verify InferiorThread object" 62gdb_test "python print ('result = %s' % t0.num)" " = 1" "test InferiorThread.num" 63gdb_test "python print ('result = %s' % t0.global_num)" " = 1" "test InferiorThread.global_num" 64gdb_test "python print ('result = %s' % str (t0.ptid))" " = \\(\[0-9\]+, \[0-9\]+, \[0-9\]+\\)" "test InferiorThread.ptid" 65 66gdb_py_test_silent_cmd "python i0 = t0.inferior" "test InferiorThread.inferior" 1 67gdb_test "python print ('result = %s' % i0.num)" " = 1" "test Inferior.num" 68 69gdb_py_test_silent_cmd "python name = gdb.selected_thread().name" \ 70 "get supplied name of current thread" 1 71gdb_py_test_silent_cmd "python gdb.selected_thread().name = 'hibob'" \ 72 "set name of current thread" 1 73gdb_test "python print (gdb.selected_thread().name)" "hibob" \ 74 "check name of current thread" 75gdb_py_test_silent_cmd "python gdb.selected_thread().name = None" \ 76 "reset name of current thread" 1 77gdb_test "python print (gdb.selected_thread().name == name)" "True" \ 78 "check name of current thread again" 79 80gdb_test_no_output "python details = gdb.selected_thread().details" \ 81 "record the thread details string" 82gdb_test "python print(details is None or isinstance(details, str))" "True" \ 83 "check that the details has an acceptable type" 84 85gdb_test "python print ('result = %s' % t0.is_stopped ())" " = True" "test InferiorThread.is_stopped" 86gdb_test "python print ('result = %s' % t0.is_running ())" " = False" "test InferiorThread.is_running" 87gdb_test "python print ('result = %s' % t0.is_exited ())" " = False" "test InferiorThread.is_exited" 88 89# Test InferiorThread is_valid. This must always be the last test in 90# this testcase as it kills the inferior. 91 92gdb_test "python print ('result = %s' % t0.is_valid ())" " = True" "test InferiorThread.is_valid" 93gdb_test_no_output "kill inferior 1" "kill inferior 1" 94gdb_test "python print ('result = %s' % t0.is_valid ())" " = False" \ 95 "test InferiorThread.is_valid after thread has been killed" 96