Remove path name from test case
[binutils-gdb.git] / gdb / testsuite / gdb.python / py-frame.exp
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 values to Python.
18
19 load_lib gdb-python.exp
20
21 require allow_python_tests
22
23 standard_testfile
24
25 if { [prepare_for_testing "failed to prepare" ${testfile} ${srcfile}] } {
26 return -1
27 }
28
29 # The following tests require execution.
30
31 if {![runto_main]} {
32 return 0
33 }
34
35 gdb_breakpoint [gdb_get_line_number "Block break here."]
36 gdb_continue_to_breakpoint "Block break here."
37 gdb_py_test_silent_cmd "python bf1 = gdb.selected_frame ()" "get frame" 0
38
39 # Test Frame.architecture() method.
40 gdb_py_test_silent_cmd "python show_arch_str = gdb.execute(\"show architecture\", to_string=True)" "show arch" 0
41 gdb_test "python print (bf1.architecture().name() in show_arch_str)" "True" "test Frame.architecture()"
42
43 # First test that read_var is unaffected by PR 11036 changes.
44 gdb_test "python print (bf1.read_var(\"i\"))" "\"stuff\"" "test i"
45 gdb_test "python print (bf1.read_var(\"f\"))" "\"foo\"" "test f"
46 gdb_test "python print (bf1.read_var(\"b\"))" "\"bar\"" "test b"
47
48 # Check we can use a single named argument with read_var.
49 gdb_test "python print (bf1.read_var(variable = \"b\"))" "\"bar\"" \
50 "test b using named arguments"
51
52 # Test the read_var function in another block other than the current
53 # block (in this case, the super block). Test thar read_var is reading
54 # the correct variables of i and f but they are the correct value and type.
55 gdb_py_test_silent_cmd "python sb = bf1.block().superblock" "get superblock" 0
56 gdb_test "python print (bf1.read_var(\"i\", sb))" "1.1.*" "test i = 1.1"
57 gdb_test "python print (bf1.read_var(\"i\", sb).type)" "double" "test double i"
58 gdb_test "python print (bf1.read_var(\"f\", sb))" "2.2.*" "test f = 2.2"
59 gdb_test "python print (bf1.read_var(\"f\", sb).type)" "double" "test double f"
60
61 # Now test read_var with a variable and block using named arguments.
62 gdb_test "python print (bf1.read_var(block = sb, variable = \"i\"))" "1.1.*" \
63 "test i = 1.1 usign named arguments"
64 gdb_test "python print (bf1.read_var(block = sb, variable = \"f\"))" "2.2.*" \
65 "test f = 2.2 using named arguments"
66
67 # And again test another outerblock, this time testing "i" is the
68 # correct value and type.
69 gdb_py_test_silent_cmd "python sb = sb.superblock" "get superblock" 0
70 gdb_test "python print (bf1.read_var(\"i\", sb))" "99" "test i = 99"
71 gdb_test "python print (bf1.read_var(\"i\", sb).type)" "int" "test int i"
72
73 # Test what happens when we provide a block of the wrong type.
74 gdb_test "python print (bf1.read_var(\"i\", \"some_block\"))" \
75 [multi_line \
76 "TypeError: argument 2 must be gdb\\.Block, not str" \
77 "Error while executing Python code\\."] \
78 "check invalid block type error"
79 gdb_test "python print (bf1.read_var(block = \"some_block\", variable = \"i\"))" \
80 [multi_line \
81 "TypeError: argument 2 must be gdb\\.Block, not str" \
82 "Error while executing Python code\\."] \
83 "check invalid block type error when named args are used"
84
85 # Test what happens when we provide a variable of the wrong type.
86 gdb_test "python print (bf1.read_var(None))" \
87 [multi_line \
88 "TypeError: argument 1 must be gdb\\.Symbol or str, not NoneType" \
89 "Error while executing Python code\\."] \
90 "check read_var error when variable is None"
91 gdb_test "python print (bf1.read_var(sb))" \
92 [multi_line \
93 "TypeError: argument 1 must be gdb\\.Symbol or str, not gdb\\.Block" \
94 "Error while executing Python code\\."] \
95 "check read_var error when variable is a gdb.Block"
96
97 gdb_breakpoint "f2"
98 gdb_continue_to_breakpoint "breakpoint at f2"
99 gdb_py_test_silent_cmd "python bframe = gdb.selected_frame()" \
100 "get bottommost frame" 0
101 gdb_test "up" ".*" ""
102
103 gdb_py_test_silent_cmd "python f1 = gdb.selected_frame ()" "get second frame" 0
104 gdb_py_test_silent_cmd "python f0 = f1.newer ()" "get first frame" 0
105 gdb_py_test_silent_cmd "python f2 = f1.older ()" "get last frame" 0
106
107 # Check the Frame.level method.
108 gdb_test "python print ('bframe.level = %d' % bframe.level ())" \
109 "bframe\\.level = 0"
110 gdb_test "python print ('f0.level = %d' % f0.level ())" \
111 "f0\\.level = 0"
112 gdb_test "python print ('f1.level = %d' % f1.level ())" \
113 "f1\\.level = 1"
114 gdb_test "python print ('f2.level = %d' % f2.level ())" \
115 "f2\\.level = 2"
116
117 gdb_test "python print (f1 == gdb.newest_frame())" False \
118 "selected frame -vs- newest frame"
119 gdb_test "python print (bframe == gdb.newest_frame())" True \
120 "newest frame -vs- newest frame"
121
122 gdb_test "python print ('result = %s' % (f0 == f1))" " = False" "test equality comparison (false)"
123 gdb_test "python print ('result = %s' % (f0 == f0))" " = True" "test equality comparison (true)"
124 gdb_test "python print ('result = %s' % (f0 != f1))" " = True" "test inequality comparison (true)"
125 gdb_test "python print ('result = %s' % (f0 != f0))" " = False" "test inequality comparison (false)"
126 gdb_test "python print ('result = %s' % f0.is_valid ())" " = True" "test Frame.is_valid"
127 gdb_test "python print ('result = %s' % f0.name ())" " = f2" "test Frame.name"
128 gdb_test "python print ('result = %s' % (f0.type () == gdb.NORMAL_FRAME))" " = True" "test Frame.type"
129 gdb_test "python print ('result = %s' % (f0.unwind_stop_reason () == gdb.FRAME_UNWIND_NO_REASON))" \
130 " = True" "test Frame.unwind_stop_reason"
131 gdb_test "python print ('result = %s' % gdb.frame_stop_reason_string (gdb.FRAME_UNWIND_INNER_ID))" " = previous frame inner to this frame \\(corrupt stack\\?\\)" "test gdb.frame_stop_reason_string"
132 gdb_test "python print ('result = %s' % f0.pc ())" " = \[0-9\]+" "test Frame.pc"
133 gdb_test "python print ('result = %s' % (f0.older () == f1))" " = True" "test Frame.older"
134 gdb_test "python print ('result = %s' % (f1.newer () == f0))" " = True" "test Frame.newer"
135 gdb_test "python print ('result = %s' % f0.read_var ('variable_which_surely_doesnt_exist'))" \
136 "ValueError: Variable 'variable_which_surely_doesnt_exist' not found.*Error while executing Python code." \
137 "test Frame.read_var - error"
138 gdb_test "python print ('result = %s' % f0.read_var ('a'))" " = 1" "test Frame.read_var - success"
139
140 gdb_test "python print ('result = %s' % (gdb.selected_frame () == f1))" " = True" "test gdb.selected_frame"
141
142 # Can read SP register.
143 gdb_test "python print ('result = %s' % (gdb.selected_frame ().read_register ('sp') == gdb.parse_and_eval ('\$sp')))" \
144 " = True" \
145 "test Frame.read_register(sp)"
146
147 # PC value obtained via read_register is as expected.
148 gdb_test "python print ('result = %s' % (f0.read_register('pc') == f0.pc()))" \
149 " = True" \
150 "test Frame.read_register(pc)"
151
152 # Repeat the previous test, but this time use named arguments for the
153 # read_register method call.
154 gdb_test "python print ('result = %s' % (f0.read_register(register = 'pc') == f0.pc()))" \
155 " = True" \
156 "test Frame.read_register() using named arguments"
157
158 # Test arch-specific register name.
159 set pc ""
160 if {[is_amd64_regs_target]} {
161 set pc "rip"
162 } elseif {[is_x86_like_target]} {
163 set pc "eip"
164 }
165 if { $pc != "" } {
166 gdb_test "python print ('result = %s' % (f0.read_register('pc') == f0.read_register('$pc')))" \
167 " = True" \
168 "test Frame.read_register($pc)"
169 }
170
171 # Test language.
172 gdb_test "python print(gdb.selected_frame().language())" "c"
173 gdb_test "set language ada"
174 gdb_test "python print(gdb.selected_frame().language())" "c" \
175 "frame language is not affected by global language"
176
177 # This previously caused a crash -- the implementation was missing the
178 # case where a register had an unexpected type.
179 gdb_test "python print(gdb.selected_frame().read_register(list()))" \
180 ".*Invalid type for register.*" \
181 "test Frame.read_register with list"