1 # Copyright
(C
) 2010-2024 Free Software Foundation
, Inc.
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.
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.
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
/>.
16 # This file is part of the GDB testsuite. It tests the mechanism
17 # exposing
values to Python.
19 load_lib gdb
-python.exp
21 require allow_python_tests
25 if { [prepare_for_testing
"failed to prepare" ${testfile} ${srcfile}] } {
34 gdb_breakpoint
[gdb_get_line_number
"Block break here."]
35 gdb_continue_to_breakpoint
"Block break here."
37 # Test initial innermost block.
38 gdb_py_test_silent_cmd
"python frame = gdb.selected_frame()" "Get Frame" 0
39 gdb_py_test_silent_cmd
"python block = frame.block()" \
40 "Get block, initial innermost block" 0
41 gdb_test
"python print (block)" "<gdb.Block <anonymous> \{i, f, b\}>" \
42 "check block not None"
43 gdb_test
"python print (block.function)" "None" "first anonymous block"
44 gdb_test
"python print (block.start)" "${decimal}" "check start not None"
45 gdb_test
"python print (block.end)" "${decimal}" "check end not None"
46 gdb_test
"python print (block\['f'\].name == 'f')" "True" "check variable access"
47 gdb_test
"python print (block\['nonexistent'\])" ".*KeyError.*: 'nonexistent'.*" \
48 "check nonexistent variable"
49 gdb_test
"python print (block\[42\])" ".*TypeError.*: Expected a string.*" \
50 "check non-string key"
52 # Test global
/static blocks
53 gdb_py_test_silent_cmd
"python frame = gdb.selected_frame()" "Get Frame" 0
54 gdb_py_test_silent_cmd
"python block = frame.block()" \
55 "Get block, frame.block" 0
56 gdb_test
"python print (block.is_global)" "False" "not a global block"
57 gdb_test
"python print (block.is_static)" "False" "not a static block"
58 gdb_py_test_silent_cmd
"python gblock = block.global_block" \
59 "Get block, global_block" 1
60 gdb_py_test_silent_cmd
"python sblock = block.static_block" \
61 "Get block, static_block" 1
62 gdb_test
"python print (gblock.is_global)" "True" "is the global block"
63 gdb_test
"python print (sblock.is_static)" "True" "is the static block"
65 #
Move up superblock
(s
) until we reach function block_func.
66 gdb_test_no_output
"python block = block.superblock" "get superblock"
67 gdb_test
"python print (block.function)" "None" "second anonymous block"
68 gdb_test_no_output
"python block = block.superblock" "get superblock 2"
69 gdb_test
"python print (block.function)" "block_func" \
70 "Print superblock 2 function"
72 #
Switch frames
, then test block
for no_locals_func.
73 gdb_test
"continue" ".*" "continue to no_locals_func breakpoint"
74 gdb_test
"up" ".*" "up to no_locals_func"
75 gdb_py_test_silent_cmd
"python frame = gdb.selected_frame()" "Get Frame 2" 0
76 gdb_py_test_silent_cmd
"python block = frame.block()" "Get Frame 2's block" 0
77 gdb_test
"python print (repr (block))" "<gdb.Block no_locals_func \{\}>" \
78 "Check block in no_locals_func"
79 gdb_test
"python print (block.function)" "no_locals_func" \
80 "no_locals_func block"
82 #
Switch frames
, then test block
for few_locals_func.
83 gdb_test
"continue" ".*" "continue to few_locals_func breakpoint"
84 gdb_test
"up" ".*" "up to few_locals_func"
85 gdb_py_test_silent_cmd
"python frame = gdb.selected_frame()" "Get Frame 2" 0
86 gdb_py_test_silent_cmd
"python block = frame.block()" "Get Frame 2's block" 0
87 gdb_test
"python print (repr (block))" \
88 "<gdb.Block few_locals_func \{i, j, k, x, y\}>" \
89 "Check block in few_locals_func"
90 gdb_test
"python print (block.function)" "few_locals_func" \
91 "few_locals_func block"
93 #
Switch frames
, then test block
for many_locals_func.
94 gdb_test
"continue" ".*" "continue to many_locals_func breakpoint"
95 gdb_test
"up" ".*" "up to many_locals_func"
96 gdb_py_test_silent_cmd
"python frame = gdb.selected_frame()" "Get Frame 2" 0
97 gdb_py_test_silent_cmd
"python block = frame.block()" "Get Frame 2's block" 0
98 gdb_test
"python print (repr (block))" \
99 "<gdb.Block many_locals_func \{i, j, k, x, y, \\.\\.\\. \\(1 more symbol\\)\}>" \
100 "Check block in many_locals_func"
101 gdb_test
"python print (block.function)" "many_locals_func" \
102 "many_locals_func block"
104 #
Switch frames
, then test
for main block.
106 gdb_py_test_silent_cmd
"python frame = gdb.selected_frame()" "Get Frame 2" 0
107 gdb_py_test_silent_cmd
"python block = frame.block()" "Get Frame 2's block" 0
108 gdb_test
"python print (repr (block))" "<gdb.Block main \{.*\}>" \
109 "Check Frame 2's block not None"
110 gdb_test
"python print (block.function)" "main" "main block"
112 # Test Block is_valid. This must always be the last test in this
113 # testcase as it unloads the object file.
115 gdb_py_test_silent_cmd
"python frame = gdb.selected_frame()" "Get Frame" 0
116 gdb_py_test_silent_cmd
"python block = frame.block()" "Get Frame block" 0
117 gdb_py_test_silent_cmd
"python block_iter = iter (block)" "Get Frame block" 0
118 gdb_test
"python print (block.is_valid())" "True" \
119 "Check block validity"
120 gdb_test
"python print (block_iter.is_valid())" "True" \
121 "Check block_iter validity"
122 gdb_test_no_output
"python x = hash(block)" "block is hashable"
123 gdb_test
"python print (type (x))" "<class 'int'>" "block hash is integer"
125 gdb_test
"python print (block.is_valid())" "False" \
126 "Check block validity after unload"
127 gdb_test
"python print (block_iter.is_valid())" "False" \
128 "Check block_iter validity after unload"
129 gdb_test
"python print (hash (block) == x)" "True" \
130 "block hash did not change"