]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/testsuite/gdb.python/py-events.exp
Automatic Copyright Year update after running gdb/copyright.py
[thirdparty/binutils-gdb.git] / gdb / testsuite / gdb.python / py-events.exp
CommitLineData
4a94e368 1# Copyright (C) 2010-2022 Free Software Foundation, Inc.
c17a9e46
HZ
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
079670b9 16if [use_gdb_stub] {
3a3dad98
JK
17 return 0
18}
19
c17a9e46
HZ
20load_lib gdb-python.exp
21
20c168b5
KP
22set libfile "py-events-shlib"
23set libsrc $srcdir/$subdir/$libfile.c
b4a58790 24set lib_sl [standard_output_file $libfile.so]
20c168b5
KP
25set lib_opts debug
26
b4a58790 27standard_testfile
20c168b5 28set exec_opts [list debug shlib=$lib_sl]
c17a9e46 29
4c93b1db 30if [get_compiler_info] {
c17a9e46
HZ
31 return -1
32}
33
20c168b5
KP
34if { [gdb_compile_shlib $libsrc $lib_sl $lib_opts] != ""
35 || [gdb_compile $srcdir/$subdir/$srcfile $binfile executable $exec_opts] != ""} {
84c93cd5 36 untested "failed to compile"
20c168b5
KP
37 return -1
38}
39
40# Start with a fresh gdb.
41clean_restart ${testfile}
42
c17a9e46
HZ
43if { [skip_python_tests] } { continue }
44
74c2c1f4 45set pyfile [gdb_remote_download host ${srcdir}/${subdir}/py-events.py]
72ca0410 46gdb_test_no_output "source ${pyfile}" "load python file"
c17a9e46 47
314bb8c3 48gdb_test "test-objfile-events" "Object file events registered."
20c168b5
KP
49
50gdb_breakpoint "main" {temporary}
51
cdc7edd7 52gdb_test "run" ".*event type: new_objfile.*new objfile name.*" "new objfile notification"
c17a9e46 53
cb6be26b
KP
54gdb_test_no_output "set detach-on-fork off" ""
55
314bb8c3 56gdb_test "test-events" "Event testers registered."
c17a9e46 57
6839b47f 58gdb_breakpoint "first"
c17a9e46
HZ
59gdb_breakpoint "first"
60
61# Test continue event and breakpoint stop event
62gdb_test "continue" ".*event type: continue.*
63.*event type: stop.*
64.*stop reason: breakpoint.*
6839b47f 65.*first breakpoint number: 2.*
c17a9e46 66.*breakpoint number: 2.*
6839b47f 67.*breakpoint number: 3.*
5068630a 68all threads stopped.*"
c17a9e46 69
2ca0b532
PA
70# Test that when "step N" trips on a breakpoint, we get a stop event
71# with breakpoint stop reason.
72gdb_breakpoint "do_nothing"
73gdb_test "step 3" ".*event type: continue.*
74.*event type: stop.*
75.*stop reason: breakpoint.*
76.*first breakpoint number: 4.*
77.*breakpoint number: 4.*
5068630a 78all threads stopped.*"
2ca0b532
PA
79
80delete_breakpoints
81
162078c8
NB
82# Test inferior call events
83
84gdb_test_multiple "info threads" "get current thread" {
378f6478 85 -re -wrap "process ($decimal)\[^\n\r\]*do_nothing.*" {
162078c8 86 set process_id $expect_out(1,string)
378f6478
TV
87 pass $gdb_test_name
88 }
89 -re -wrap "Thread $hex \\(LWP ($decimal)\\)\[^\n\r\]*do_nothing.*" {
90 set process_id $expect_out(1,string)
91 pass $gdb_test_name
162078c8
NB
92 }
93}
94
95gdb_test_multiple "print do_nothing" "get address of do_nothing" {
96 -re "\[^\n\r\]*(0x\[0-9a-f\]+) \<do_nothing\>.*$gdb_prompt $" {
97 set addr $expect_out(1,string)
98 pass "get address of do_nothing"
99 }
100}
101
102set expected [list "event type: pre-call"]
103lappend expected "ptid: \\($process_id, $process_id, 0\\)" "address: $addr"
104lappend expected "event type: post-call"
105lappend expected "ptid: \\($process_id, $process_id, 0\\)" "address: $addr"
106gdb_test_sequence "call do_nothing()" "" $expected
107
108# Test register changed event
109gdb_test_no_output {set $old_sp = $sp}
110gdb_test_sequence {set $sp = 0} "" {
111 "event type: register-changed"
112 "frame: "
113 "num: "
114}
115gdb_test_sequence {set $sp = 1} "" {
116 "event type: register-changed"
117 "frame: "
118 "num: "
119}
120gdb_test_sequence {set $sp = $old_sp} "" {
121 "event type: register-changed"
122 "frame: "
123 "num: "
124}
125
126# Test that no register_changed event is generated on "non-user"
127# modifications
128set test "up"
129gdb_test_multiple {up} $test {
130 -re "event type: register-changed.*\r\n$gdb_prompt $" {
131 fail $test
132 }
133 -re "#1.*in first.*\r\n.*do_nothing.*\r\n$gdb_prompt $" {
134 pass $test
135 }
136}
137
138set test "down"
139gdb_test_multiple {down} $test {
140 -re "event type: register-changed.*\r\n$gdb_prompt $" {
141 fail $test
142 }
143 -re "#0.*do_nothing.* at .*\r\n.*void do_nothing.*\r\n$gdb_prompt $" {
144 pass $test
145 }
146}
147
148set test "step"
149# Note: This will step the inferior out of do_nothing and back into first.
150gdb_test_multiple {step} $test {
151 -re "event type: register-changed.*\r\n$gdb_prompt $" {
152 fail $test
153 }
154 -re "first.* at .*\r\n$gdb_prompt $" {
155 pass $test
156 }
157}
158
159# Test memory changed event
160gdb_test_no_output {set $saved = *(int*) $sp}
161gdb_test_sequence {set *(int*) $sp = 0} "" {
162 "event type: memory-changed"
163 "address: "
164 "length: "
165}
166gdb_test_sequence {set *(int*) $sp = $saved} "" {
167 "event type: memory-changed"
168 "address: "
169 "length: "
170}
171
172# Test that no memory_changed event is generated on breakpoint
173# activity
174set test "break second"
175gdb_test_multiple "break second" $test {
176 -re "event type: memory-changed" {
177 fail $test
178 }
179 -re "Breakpoint (\[0-9\]+) at .*\r\n$gdb_prompt $" {
180 set second_breakpoint $expect_out(1,string)
181 pass $test
182 }
183
184}
185
186set test "continue to breakpoint $second_breakpoint"
187set expected ".*event type: continue.*\r\n"
188append expected ".*event type: stop.*\r\n"
189append expected ".*stop reason: breakpoint.*\r\n"
190append expected ".*all threads stopped.*\r\n$gdb_prompt $"
191
192gdb_test_multiple "continue" $test {
193 -re "event type: memory-changed.*\r\n$gdb_prompt $" {
194 fail $test
195 }
196 -re $expected {
197 pass $test
198 }
199}
200
201gdb_test_no_output "delete $second_breakpoint"
202
c17a9e46 203#test exited event.
df5bc734
MH
204proc get_process_id {test} {
205 global gdb_prompt
206 gdb_test_multiple "info proc" $test {
207 -re "process (\\d+).*$gdb_prompt $" {
208 set process_id $expect_out(1,string)
209 pass $gdb_test_name
210 }
211 }
212 return ${process_id}
213}
214
215set process_id [get_process_id "get inferior 1 process id"]
c17a9e46 216gdb_test "continue" ".*event type: continue.*
4ffbba72 217.*clear_objfiles\[\r\n\]*progspace: .*py-events.*
c17a9e46 218.*event type: exit.*
cb6be26b 219.*exit code: 12.*
2e8265fd 220.*exit inf: 1.*
df5bc734 221.*exit pid: $process_id.*
2e8265fd 222dir ok: True.*" "Inferior 1 terminated."
cb6be26b
KP
223
224gdb_test "inferior 2" ".*Switching to inferior 2.*"
df5bc734 225set process_id [get_process_id "get inferior 2 process id"]
cb6be26b
KP
226gdb_test "continue" ".*event type: continue.*
227.*event type: exit.*
228.*exit code: 12.*
2e8265fd 229.*exit inf: 2.*
df5bc734 230.*exit pid: $process_id.*
2e8265fd 231dir ok: True.*" "Inferior 2 terminated."
3f77c769
TT
232
233
234# Test before_prompt event.
2a17c803 235gdb_test_multiline "define new user command" \
3f77c769
TT
236 "define xxz" "End with a line saying just .end.." \
237 "set variable \$x = 72" "" \
238 "set variable \$y = 93" "" \
239 "end" ""
240
2a17c803 241gdb_test_multiline "add before_prompt listener" \
3f77c769
TT
242 "python" "" \
243 "count = 0" "" \
244 "def listener():" "" \
245 " global count" "" \
246 " count = count + 1" "" \
247 "gdb.events.before_prompt.connect(listener)" "" \
248 "end" ""
249
250gdb_test_no_output "set variable \$x = 32" "do something"
251# Result is due to one emitted before "set var" and one emitted before
252# this command.
253gdb_test "python print(count)" 2 "check for before_prompt event"
254
255gdb_test_no_output "xxz" "run a canned sequence"
7f99d636 256gdb_test "python print(count)" 4 "check for before_prompt event again"
df5bc734
MH
257
258# Test evaluating expressions from within an inferior exit event handler. This
259# used to cause a crash when expression were evaluated as C++.
260gdb_test_no_output "set language c++"
261
262gdb_test_multiline "add exited listener" \
263 "python" "" \
264 "def increment_foo(_):" "" \
265 " gdb.execute('set \$_foo=\$_foo+1')" "" \
266 "gdb.events.exited.connect(increment_foo)" "" \
267 "end" ""
268gdb_test "set \$_foo=0" "" "initialize foo variable"
269gdb_test "print \$_foo" "= 0" "check foo initialized"
270
271with_test_prefix "inferior run exit" {
272 gdb_run_cmd
273 gdb_test "" "exited with code.*" "run to exit"
274 gdb_test "print \$_foo" "= 1" "check foo after run"
275}
276
277with_test_prefix "inferior continue exit" {
278 gdb_start_cmd
279 gdb_test "" "Temporary breakpoint .* main .*" "stop on a frame"
280 gdb_test "continue" "exited with code.*" "continue to exit"
281 gdb_test "print \$_foo" "= 2" "check foo after start continue"
282}
1cb56ad3
AB
283
284# Check that when GDB exits, we see gdb.ExitedEvent objects with no
b1f0f284
AB
285# 'exit_code' attribute, and that a gdb.GdbExitingEvent is emitted.
286with_test_prefix "gdb exiting: normal" {
287 gdb_test "test-exiting-event normal" "GDB exiting event registered\\."
288
289 set saw_exiting_event 0
1cb56ad3
AB
290 set saw_inferior_exit 0
291 gdb_test_multiple "quit" "" {
292 -re "Quit anyway\\? \\(y or n\\) $" {
293 send_gdb "y\n"
294 exp_continue
295 }
b1f0f284
AB
296 -re "event type: gdb-exiting\r\nexit code: $decimal" {
297 incr saw_exiting_event
298 exp_continue
299 }
1cb56ad3
AB
300 -re "event type: exit\r\nexit code: not-present\r\nexit inf: $decimal\r\nexit pid: $decimal\r\ndir ok: False\r\n" {
301 incr saw_inferior_exit
302 exp_continue
303 }
304 eof {
b1f0f284 305 gdb_assert { $saw_exiting_event == 1 }
1cb56ad3
AB
306 gdb_assert { $saw_inferior_exit == 2 }
307 pass $gdb_test_name
308 }
309 }
310}
b1f0f284
AB
311
312# Check that if the GdbExitingEvent raises an exception then this
313# doesn't impact GDB's exit process.
314with_test_prefix "gdb exiting: error" {
315 clean_restart ${testfile}
316
317 if ![runto_main] then {
318 fail "cannot run to main."
319 return 0
320 }
321
322 gdb_test_no_output "source ${pyfile}" "load python file"
323 gdb_test "test-exiting-event error" "GDB exiting event registered\\."
324 gdb_test "test-events" "Event testers registered\\."
325
326 set saw_exiting_error 0
327 set saw_inferior_exit 0
328 gdb_test_multiple "quit" "" {
329 -re "Quit anyway\\? \\(y or n\\) $" {
330 send_gdb "y\n"
331 exp_continue
332 }
333 -re "event type: gdb-exiting\r\nexit code: $decimal" {
334 fail "$gdb_test_name XXXX"
335 exp_continue
336 }
337 -re "Python Exception <class 'gdb.GdbError'>: error from gdb_exiting_handler\r\n" {
338 incr saw_exiting_error
339 exp_continue
340 }
341 -re "event type: exit\r\nexit code: not-present\r\nexit inf: $decimal\r\nexit pid: $decimal\r\ndir ok: False\r\n" {
342 incr saw_inferior_exit
343 exp_continue
344 }
345 eof {
346 gdb_assert { $saw_inferior_exit == 1 }
347 gdb_assert { $saw_exiting_error == 1 }
348 pass $gdb_test_name
349 }
350 }
351}