mirror of
https://sourceware.org/git/binutils-gdb.git
synced 2024-12-27 04:52:05 +08:00
edae3fd660
New in this version: - Add a PY_MAJOR_VERSION check in configure.ac / AC_TRY_LIBPYTHON. If the user passes --with-python=python2, this will cause a configure failure saying that GDB only supports Python 3. Support for Python 2 is a maintenance burden for any patches touching Python support. Among others, the differences between Python 2 and 3 string and integer types are subtle. It requires a lot of effort and thinking to get something that behaves correctly on both. And that's if the author and reviewer of the patch even remember to test with Python 2. See this thread for an example: https://sourceware.org/pipermail/gdb-patches/2021-December/184260.html So, remove Python 2 support. Update the documentation to state that GDB can be built against Python 3 (as opposed to Python 2 or 3). Update all the spots that use: - sys.version_info - IS_PY3K - PY_MAJOR_VERSION - gdb_py_is_py3k ... to only keep the Python 3 portions and drop the use of some now-removed compatibility macros. I did not update the configure script more than just removing the explicit references to Python 2. We could maybe do more there, like check the Python version and reject it if that version is not supported. Otherwise (with this patch), things will only fail at compile time, so it won't really be clear to the user that they are trying to use an unsupported Python version. But I'm a bit lost in the configure code that checks for Python, so I kept that for later. Change-Id: I75b0f79c148afbe3c07ac664cfa9cade052c0c62
346 lines
15 KiB
Plaintext
346 lines
15 KiB
Plaintext
# Copyright (C) 2009-2022 Free Software Foundation, Inc.
|
|
|
|
# This program is free software; you can redistribute it and/or modify
|
|
# it under the terms of the GNU General Public License as published by
|
|
# the Free Software Foundation; either version 3 of the License, or
|
|
# (at your option) any later version.
|
|
#
|
|
# This program is distributed in the hope that it will be useful,
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
# GNU General Public License for more details.
|
|
#
|
|
# You should have received a copy of the GNU General Public License
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
# This file is part of the GDB testsuite. It tests the mechanism
|
|
# exposing inferiors to Python.
|
|
|
|
load_lib gdb-python.exp
|
|
|
|
standard_testfile
|
|
|
|
if { [gdb_compile_pthreads ${srcdir}/${subdir}/${srcfile} ${binfile} executable {debug}] != "" } {
|
|
return -1
|
|
}
|
|
|
|
# Start with a fresh gdb.
|
|
clean_restart ${testfile}
|
|
|
|
# Skip all tests if Python scripting is not enabled.
|
|
if { [skip_python_tests] } { continue }
|
|
|
|
switch [get_endianness] {
|
|
little { set python_pack_char "<" }
|
|
big { set python_pack_char ">" }
|
|
}
|
|
|
|
# The following tests require execution.
|
|
|
|
if ![runto_main] then {
|
|
return 0
|
|
}
|
|
|
|
# Test basic gdb.Inferior attributes and methods.
|
|
|
|
gdb_py_test_silent_cmd "python inferiors = gdb.inferiors ()" "get inferiors list" 1
|
|
gdb_test "python print (inferiors)" \
|
|
"\\(<gdb.Inferior num=1, pid=$decimal>,\\)" "verify inferiors list"
|
|
gdb_py_test_silent_cmd "python i0 = inferiors\[0\]" "get first inferior" 0
|
|
|
|
gdb_test "python print ('result = %s' % (i0 == inferiors\[0\]))" " = True" "test equality comparison (true)"
|
|
gdb_test "python print ('result = %s' % i0.num)" " = \[0-9\]+" "test Inferior.num"
|
|
gdb_test "python print ('result = %s' % i0.connection_num)" " = \[0-9\]+" "test Inferior.connection_num"
|
|
gdb_test "python print ('result = %s' % (i0.connection_num == i0.connection.num))" " = True" \
|
|
"Inferior.connection_num equals Inferior.connection.num"
|
|
gdb_test "python print ('result = %s' % i0.pid)" " = \[0-9\]+" "test Inferior.pid"
|
|
gdb_test "python print ('result = %s' % i0.was_attached)" " = False" "test Inferior.was_attached"
|
|
gdb_test "python print (i0.threads ())" "\\(<gdb.InferiorThread object at 0x\[\[:xdigit:\]\]+>,\\)" "test Inferior.threads"
|
|
|
|
gdb_test "python print (i0.progspace)" "<gdb.Progspace object at $hex>"
|
|
gdb_test "python print (i0.progspace == gdb.progspaces()\[0\])" "True"
|
|
|
|
# Test the number of inferior threads.
|
|
|
|
gdb_breakpoint check_threads
|
|
gdb_continue_to_breakpoint "cont to check_threads" ".*pthread_barrier_wait.*"
|
|
gdb_test "python print (len (i0.threads ()))" "\r\n9" "test Inferior.threads 2"
|
|
|
|
# Proceed to the next test.
|
|
|
|
gdb_breakpoint [gdb_get_line_number "Break here."]
|
|
gdb_continue_to_breakpoint "cont to Break here." ".*Break here\..*"
|
|
|
|
# Test memory read and write operations.
|
|
|
|
gdb_py_test_silent_cmd "python addr = gdb.selected_frame ().read_var ('str')" \
|
|
"read str address" 0
|
|
gdb_py_test_silent_cmd "python str = gdb.inferiors()\[0\].read_memory (addr, 5); print(str)" \
|
|
"read str contents" 1
|
|
gdb_py_test_silent_cmd "python a = bytes('a', 'ascii')" "" 0
|
|
gdb_py_test_silent_cmd "python str\[1\] = a" "change str" 0
|
|
gdb_py_test_silent_cmd "python gdb.inferiors()\[0\].write_memory (addr, str)" \
|
|
"write str" 1
|
|
gdb_test "print (str)" " = \"hallo, testsuite\"" \
|
|
"ensure str was changed in the inferior"
|
|
|
|
# Test memory search.
|
|
|
|
set hex_number {0x[0-9a-fA-F][0-9a-fA-F]*}
|
|
set dec_number {[0-9]+}
|
|
set history_prefix {[$][0-9]* = }
|
|
set newline {[\r\n]+}
|
|
set pattern_not_found "${newline}.None"
|
|
set one_pattern_found "${newline}.${dec_number}"
|
|
|
|
# Test string pattern.
|
|
|
|
with_test_prefix "string" {
|
|
gdb_test "set *(int32_t*) &int8_search_buf\[10\] = 0x61616161"
|
|
gdb_test "py search_buf = gdb.selected_frame ().read_var ('int8_search_buf')"
|
|
gdb_test_no_output "py start_addr = search_buf.address"
|
|
gdb_test_no_output "py length = search_buf.type.sizeof"
|
|
|
|
gdb_test "py print (gdb.inferiors()\[0\].search_memory (start_addr, length, 'aaa'))" \
|
|
"${one_pattern_found}" "find string pattern"
|
|
|
|
# Test not finding pattern because search range too small, with
|
|
# potential find at the edge of the range.
|
|
gdb_test "py print (gdb.inferiors()\[0\].search_memory (start_addr, 10+3, 'aaaa'))" \
|
|
"${pattern_not_found}" "pattern not found at end of range"
|
|
|
|
# Increase the search range by 1 and we should find the pattern.
|
|
gdb_test "py print (gdb.inferiors()\[0\].search_memory (start_addr, 10+3+1, 'aaa'))" \
|
|
"${one_pattern_found}" "pattern found at end of range"
|
|
}
|
|
|
|
# Import struct to pack the following patterns.
|
|
gdb_test_no_output "py from struct import *"
|
|
|
|
# Test 16-bit pattern.
|
|
|
|
with_test_prefix "16-bit" {
|
|
gdb_test_no_output "set int16_search_buf\[10\] = 0x1234"
|
|
gdb_test_no_output "py search_buf = gdb.selected_frame ().read_var ('int16_search_buf')"
|
|
gdb_test_no_output "py start_addr = search_buf.address"
|
|
gdb_test_no_output "py length = search_buf.type.sizeof"
|
|
gdb_test_no_output "py pattern = pack('${python_pack_char}H',0x1234)"
|
|
gdb_test "py print (gdb.inferiors()\[0\].search_memory (start_addr, length, pattern))" \
|
|
"${one_pattern_found}" "find 16-bit pattern, with value pattern"
|
|
}
|
|
|
|
# Test 32-bit pattern.
|
|
|
|
with_test_prefix "32-bit" {
|
|
gdb_test_no_output "set int32_search_buf\[10\] = 0x12345678"
|
|
gdb_test_no_output "py search_buf = gdb.selected_frame ().read_var ('int32_search_buf')"
|
|
gdb_test_no_output "py start_addr = search_buf.address"
|
|
gdb_test_no_output "py length = search_buf.type.sizeof"
|
|
gdb_test_no_output "py pattern = pack('${python_pack_char}I',0x12345678)"
|
|
gdb_test "py print (gdb.inferiors()\[0\].search_memory (start_addr, length, pattern))" \
|
|
"${one_pattern_found}" "find 32-bit pattern, with python pattern"
|
|
}
|
|
|
|
# Test 64-bit pattern.
|
|
|
|
with_test_prefix "64-bit" {
|
|
gdb_test_no_output "set int64_search_buf\[10\] = 0xfedcba9876543210LL"
|
|
gdb_test_no_output "py search_buf = gdb.selected_frame ().read_var ('int64_search_buf')"
|
|
gdb_test_no_output "py start_addr = search_buf.address"
|
|
gdb_test_no_output "py length = search_buf.type.sizeof"
|
|
gdb_test_no_output "py pattern = pack('${python_pack_char}Q', 0xfedcba9876543210)"
|
|
gdb_test "py print (gdb.inferiors()\[0\].search_memory (start_addr, length, pattern))" \
|
|
"${one_pattern_found}" "find 64-bit pattern, with value pattern"
|
|
}
|
|
|
|
# Test mixed-sized patterns.
|
|
|
|
with_test_prefix "mixed-sized" {
|
|
gdb_test_no_output "set *(int8_t*) &search_buf\[10\] = 0x62"
|
|
gdb_test_no_output "set *(int16_t*) &search_buf\[11\] = 0x6363"
|
|
gdb_test_no_output "set *(int32_t*) &search_buf\[13\] = 0x64646464"
|
|
gdb_test_no_output "py search_buf = gdb.selected_frame ().read_var ('search_buf')"
|
|
gdb_test_no_output "py start_addr = search_buf\[0\].address"
|
|
gdb_test_no_output "py pattern1 = pack('B', 0x62)"
|
|
gdb_test_no_output "py pattern2 = pack('${python_pack_char}H', 0x6363)"
|
|
gdb_test_no_output "py pattern3 = pack('${python_pack_char}I', 0x64646464)"
|
|
|
|
gdb_test "py print (gdb.inferiors()\[0\].search_memory (start_addr, 100, pattern1))" \
|
|
"${one_pattern_found}" "find mixed-sized pattern 1"
|
|
gdb_test "py print (gdb.inferiors()\[0\].search_memory (start_addr, 100, pattern2))" \
|
|
"${one_pattern_found}" "find mixed-sized pattern 2"
|
|
gdb_test "py print (gdb.inferiors()\[0\].search_memory (start_addr, 100, pattern3))" \
|
|
"${one_pattern_found}" "find mixed-sized pattern 3"
|
|
}
|
|
|
|
# Test search spanning a large range, in the particular case of native
|
|
# targets, test the search spanning multiple chunks.
|
|
# Remote targets may implement the search differently.
|
|
|
|
set CHUNK_SIZE 16000
|
|
with_test_prefix "large range" {
|
|
gdb_test_no_output "set *(int32_t*) &search_buf\[0*${CHUNK_SIZE}+100\] = 0x12345678"
|
|
gdb_test_no_output "set *(int32_t*) &search_buf\[1*${CHUNK_SIZE}+100\] = 0x12345678"
|
|
gdb_test_no_output "py start_addr = gdb.selected_frame ().read_var ('search_buf')"
|
|
gdb_test_no_output "py end_addr = start_addr + gdb.selected_frame ().read_var ('search_buf_size')"
|
|
gdb_test_no_output "py pattern = pack('${python_pack_char}I', 0x12345678)"
|
|
|
|
gdb_test_no_output "py first = gdb.inferiors()\[0\].search_memory (start_addr,end_addr - start_addr, pattern)"
|
|
gdb_test "py print (first)" "${one_pattern_found}" "search spanning large range 1st result"
|
|
gdb_test_no_output "py start_addr = first + 1"
|
|
gdb_test_no_output "py second = gdb.inferiors()\[0\].search_memory (start_addr, end_addr - start_addr, pattern)"
|
|
gdb_test "py print (second)" "${one_pattern_found}" "search spanning large range 2nd result"
|
|
gdb_test_no_output "py start_addr = second + 1"
|
|
gdb_test_no_output "py third = gdb.inferiors()\[0\].search_memory (start_addr, end_addr - start_addr, pattern)"
|
|
gdb_test "py print (third)" "${pattern_not_found}" "search spanning large range 3rd result"
|
|
}
|
|
|
|
# For native targets, test a pattern straddling a chunk boundary.
|
|
|
|
if [isnative] {
|
|
with_test_prefix "straddling" {
|
|
gdb_test_no_output "set *(int32_t*) &search_buf\[${CHUNK_SIZE}-1\] = 0xfdb97531"
|
|
gdb_test_no_output "py pattern = pack('${python_pack_char}I', 0xfdb97531)"
|
|
gdb_test_no_output "py start_addr = gdb.selected_frame ().read_var ('search_buf')"
|
|
gdb_test "py print (gdb.inferiors()\[0\].search_memory (start_addr, end_addr - start_addr, pattern))" \
|
|
"${one_pattern_found}" "find pattern straddling chunk boundary"
|
|
}
|
|
}
|
|
|
|
# Test Inferior is_valid. This must always be the last test in
|
|
# this testcase as it kills the inferior.
|
|
|
|
with_test_prefix "is_valid" {
|
|
gdb_py_test_silent_cmd "python inf_list = gdb.inferiors()" "get initial list" 1
|
|
gdb_test "python print (len(inf_list))" "1" "get inferior list length 1"
|
|
gdb_test "python print (inf_list\[0\].is_valid())" "True" \
|
|
"check inferior validity 1"
|
|
|
|
# The "dummy" line below used to cause a gdb crash.
|
|
gdb_test_multiline "install new inferior event handler" \
|
|
"python" "" \
|
|
"my_inferior_count = 1" "" \
|
|
"def new_inf_handler(evt):" "" \
|
|
" global my_inferior_count" "" \
|
|
" if evt.inferior is not None:" "" \
|
|
" my_inferior_count = my_inferior_count + 1" "" \
|
|
" dummy = gdb.Value(True)" "" \
|
|
"gdb.events.new_inferior.connect(new_inf_handler)" "" \
|
|
"end" ""
|
|
gdb_test_multiline "install inferior deleted event handler" \
|
|
"python" "" \
|
|
"def del_inf_handler(evt):" "" \
|
|
" global my_inferior_count" "" \
|
|
" if evt.inferior is not None:" "" \
|
|
" my_inferior_count = my_inferior_count - 1" "" \
|
|
"gdb.events.inferior_deleted.connect(del_inf_handler)" "" \
|
|
"end" ""
|
|
|
|
gdb_test "add-inferior" "Added inferior 2.*" "add empty inferior 2"
|
|
gdb_py_test_silent_cmd "python inf_list = gdb.inferiors()" "get new list" 1
|
|
gdb_test "python print (len(inf_list))" "2" "get inferior list length 2"
|
|
gdb_test "python print (inf_list\[0\].is_valid())" "True" \
|
|
"check inferior validity 2"
|
|
|
|
gdb_test "python print (my_inferior_count)" "2" \
|
|
"test new-inferior event handler"
|
|
|
|
gdb_test "python print (inf_list\[1\].is_valid())" "True" \
|
|
"check inferior validity 3"
|
|
|
|
gdb_test_no_output "remove-inferiors 2" "remove-inferiors 3"
|
|
gdb_test "python print (inf_list\[0\].is_valid())" "True" \
|
|
"check inferior validity 4"
|
|
|
|
gdb_test "python print (inf_list\[1\].is_valid())" "False" \
|
|
"check inferior validity 5"
|
|
|
|
gdb_test "python print (my_inferior_count)" "1" \
|
|
"test inferior-deleted event handler"
|
|
|
|
# Test that other properties and methods handle the removed inferior
|
|
# correctly.
|
|
gdb_test "python print (inf_list\[1\].num)" \
|
|
"RuntimeError: Inferior no longer exists.*"
|
|
gdb_test "python print (inf_list\[1\].connection_num)" \
|
|
"RuntimeError: Inferior no longer exists.*"
|
|
gdb_test "python print (inf_list\[1\].connection)" \
|
|
"RuntimeError: Inferior no longer exists.*"
|
|
gdb_test "python print (inf_list\[1\].pid)" \
|
|
"RuntimeError: Inferior no longer exists.*"
|
|
gdb_test "python print (inf_list\[1\].was_attached)" \
|
|
"RuntimeError: Inferior no longer exists.*"
|
|
gdb_test "python print (inf_list\[1\].progspace)" \
|
|
"RuntimeError: Inferior no longer exists.*"
|
|
gdb_test "python print (inf_list\[1\].threads ())" \
|
|
"RuntimeError: Inferior no longer exists.*"
|
|
gdb_test "python print (inf_list\[1\].thread_from_thread_handle (1))" \
|
|
"RuntimeError: Inferior no longer exists.*"
|
|
}
|
|
|
|
# Test gdb.selected_inferior()
|
|
with_test_prefix "selected_inferior" {
|
|
gdb_test "inferior 1" ".*" "switch to first inferior"
|
|
gdb_test "py print (gdb.selected_inferior().num)" "1" "first inferior selected"
|
|
gdb_test "py print (gdb.selected_inferior().connection_num)" "1" \
|
|
"first inferior's connection number"
|
|
gdb_test "py print (gdb.selected_inferior().connection.num)" "1" \
|
|
"first inferior's connection number, though connection object"
|
|
# Figure out if inf 1 has a native target.
|
|
set inf_1_is_native [gdb_is_target_native]
|
|
|
|
gdb_test "add-inferior -no-connection" "Added inferior 3" "create new inferior"
|
|
gdb_test "inferior 3" ".*" "switch to third inferior"
|
|
gdb_test "py print (gdb.selected_inferior().num)" "3" "third inferior selected"
|
|
gdb_test "py print (gdb.selected_inferior().connection_num)" "None" \
|
|
"third inferior's None connection number"
|
|
gdb_test "py print (gdb.selected_inferior().connection)" "None" \
|
|
"third inferior's None connection"
|
|
gdb_test "target native" "Done. Use the \"run\" command to start a process." \
|
|
"target for the third inferior"
|
|
|
|
# If inf 1 has a native target, inf 3's target is shared with 1's.
|
|
# Otherwise, it must have created a new target with a new number.
|
|
if {$inf_1_is_native} {
|
|
set expected_connection_num 1
|
|
} else {
|
|
set expected_connection_num 2
|
|
}
|
|
gdb_test "py print (gdb.selected_inferior().connection_num)" \
|
|
"$expected_connection_num" \
|
|
"third inferior's native connection number"
|
|
gdb_test "py print (gdb.selected_inferior().connection.num)" \
|
|
"$expected_connection_num" \
|
|
"third inferior's native connection number, though connection object"
|
|
|
|
# Test printing of gdb.TargetConnection object.
|
|
gdb_test "py print (gdb.selected_inferior().connection)" \
|
|
"<gdb.TargetConnection num=${expected_connection_num}, what=\"\[^\"\]+\">" \
|
|
"print a connection object"
|
|
|
|
gdb_test "inferior 1" ".*" "switch back to first inferior"
|
|
gdb_test_no_output "remove-inferiors 3" "remove second inferior"
|
|
}
|
|
|
|
# Test repr()/str()
|
|
with_test_prefix "__repr__" {
|
|
gdb_test "add-inferior" "Added inferior 4 on connection .*" "add inferior 4"
|
|
gdb_py_test_silent_cmd "python infs = gdb.inferiors()" "get inferior list" 1
|
|
gdb_test "python print (infs\[0\])" "<gdb.Inferior num=1, pid=$decimal>"
|
|
gdb_test "python print (infs)" \
|
|
"\\\(<gdb.Inferior num=1, pid=$decimal>, <gdb.Inferior num=4, pid=$decimal>\\\)" \
|
|
"print all inferiors 1"
|
|
gdb_test_no_output "remove-inferiors 4"
|
|
gdb_test "python print (infs)" \
|
|
"\\\(<gdb.Inferior num=1, pid=$decimal>, <gdb.Inferior \\\(invalid\\\)>\\\)" \
|
|
"print all inferiors 2"
|
|
}
|
|
|
|
# Test architecture.
|
|
with_test_prefix "architecture" {
|
|
gdb_test "inferior 1" ".*" "switch to first inferior"
|
|
gdb_test "python print(gdb.selected_frame().architecture() is gdb.selected_inferior().architecture())" \
|
|
"True" \
|
|
"inferior architecture matches frame architecture"
|
|
}
|