3abca39ac915c30f12ebeabdfee2fef32a3f307e
[deliverable/binutils-gdb.git] / gdb / testsuite / gdb.python / py-events.exp
1 # Copyright (C) 2010-2020 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 if [use_gdb_stub] {
17 return 0
18 }
19
20 load_lib gdb-python.exp
21
22 set libfile "py-events-shlib"
23 set libsrc $srcdir/$subdir/$libfile.c
24 set lib_sl [standard_output_file $libfile.so]
25 set lib_opts debug
26
27 standard_testfile
28 set exec_opts [list debug shlib=$lib_sl]
29
30 if [get_compiler_info] {
31 return -1
32 }
33
34 if { [gdb_compile_shlib $libsrc $lib_sl $lib_opts] != ""
35 || [gdb_compile $srcdir/$subdir/$srcfile $binfile executable $exec_opts] != ""} {
36 untested "failed to compile"
37 return -1
38 }
39
40 # Start with a fresh gdb.
41 clean_restart ${testfile}
42
43 if { [skip_python_tests] } { continue }
44
45 set pyfile [gdb_remote_download host ${srcdir}/${subdir}/py-events.py]
46 gdb_test_no_output "source ${pyfile}" "load python file"
47
48 gdb_test "test-objfile-events" "Object file events registered."
49
50 gdb_breakpoint "main" {temporary}
51
52 gdb_test "run" ".*event type: new_objfile.*new objfile name.*" "new objfile notification"
53
54 gdb_test_no_output "set detach-on-fork off" ""
55
56 gdb_test "test-events" "Event testers registered."
57
58 gdb_breakpoint "first"
59 gdb_breakpoint "first"
60
61 # Test continue event and breakpoint stop event
62 gdb_test "continue" ".*event type: continue.*
63 .*event type: stop.*
64 .*stop reason: breakpoint.*
65 .*first breakpoint number: 2.*
66 .*breakpoint number: 2.*
67 .*breakpoint number: 3.*
68 all threads stopped.*"
69
70 # Test that when "step N" trips on a breakpoint, we get a stop event
71 # with breakpoint stop reason.
72 gdb_breakpoint "do_nothing"
73 gdb_test "step 3" ".*event type: continue.*
74 .*event type: stop.*
75 .*stop reason: breakpoint.*
76 .*first breakpoint number: 4.*
77 .*breakpoint number: 4.*
78 all threads stopped.*"
79
80 delete_breakpoints
81
82 # Test inferior call events
83
84 gdb_test_multiple "info threads" "get current thread" {
85 -re "\[^\n\r\]*process (\[0-9\]+)\[^\n\r\]*do_nothing.*$gdb_prompt $" {
86 set process_id $expect_out(1,string)
87 pass "get current thread"
88 }
89 }
90
91 gdb_test_multiple "print do_nothing" "get address of do_nothing" {
92 -re "\[^\n\r\]*(0x\[0-9a-f\]+) \<do_nothing\>.*$gdb_prompt $" {
93 set addr $expect_out(1,string)
94 pass "get address of do_nothing"
95 }
96 }
97
98 set expected [list "event type: pre-call"]
99 lappend expected "ptid: \\($process_id, $process_id, 0\\)" "address: $addr"
100 lappend expected "event type: post-call"
101 lappend expected "ptid: \\($process_id, $process_id, 0\\)" "address: $addr"
102 gdb_test_sequence "call do_nothing()" "" $expected
103
104 # Test register changed event
105 gdb_test_no_output {set $old_sp = $sp}
106 gdb_test_sequence {set $sp = 0} "" {
107 "event type: register-changed"
108 "frame: "
109 "num: "
110 }
111 gdb_test_sequence {set $sp = 1} "" {
112 "event type: register-changed"
113 "frame: "
114 "num: "
115 }
116 gdb_test_sequence {set $sp = $old_sp} "" {
117 "event type: register-changed"
118 "frame: "
119 "num: "
120 }
121
122 # Test that no register_changed event is generated on "non-user"
123 # modifications
124 set test "up"
125 gdb_test_multiple {up} $test {
126 -re "event type: register-changed.*\r\n$gdb_prompt $" {
127 fail $test
128 }
129 -re "#1.*in first.*\r\n.*do_nothing.*\r\n$gdb_prompt $" {
130 pass $test
131 }
132 }
133
134 set test "down"
135 gdb_test_multiple {down} $test {
136 -re "event type: register-changed.*\r\n$gdb_prompt $" {
137 fail $test
138 }
139 -re "#0.*do_nothing.* at .*\r\n.*void do_nothing.*\r\n$gdb_prompt $" {
140 pass $test
141 }
142 }
143
144 set test "step"
145 # Note: This will step the inferior out of do_nothing and back into first.
146 gdb_test_multiple {step} $test {
147 -re "event type: register-changed.*\r\n$gdb_prompt $" {
148 fail $test
149 }
150 -re "first.* at .*\r\n$gdb_prompt $" {
151 pass $test
152 }
153 }
154
155 # Test memory changed event
156 gdb_test_no_output {set $saved = *(int*) $sp}
157 gdb_test_sequence {set *(int*) $sp = 0} "" {
158 "event type: memory-changed"
159 "address: "
160 "length: "
161 }
162 gdb_test_sequence {set *(int*) $sp = $saved} "" {
163 "event type: memory-changed"
164 "address: "
165 "length: "
166 }
167
168 # Test that no memory_changed event is generated on breakpoint
169 # activity
170 set test "break second"
171 gdb_test_multiple "break second" $test {
172 -re "event type: memory-changed" {
173 fail $test
174 }
175 -re "Breakpoint (\[0-9\]+) at .*\r\n$gdb_prompt $" {
176 set second_breakpoint $expect_out(1,string)
177 pass $test
178 }
179
180 }
181
182 set test "continue to breakpoint $second_breakpoint"
183 set expected ".*event type: continue.*\r\n"
184 append expected ".*event type: stop.*\r\n"
185 append expected ".*stop reason: breakpoint.*\r\n"
186 append expected ".*all threads stopped.*\r\n$gdb_prompt $"
187
188 gdb_test_multiple "continue" $test {
189 -re "event type: memory-changed.*\r\n$gdb_prompt $" {
190 fail $test
191 }
192 -re $expected {
193 pass $test
194 }
195 }
196
197 gdb_test_no_output "delete $second_breakpoint"
198
199 #test exited event.
200 gdb_test "continue" ".*event type: continue.*
201 .*clear_objfiles\[\r\n\]*progspace: .*py-events.*
202 .*event type: exit.*
203 .*exit code: 12.*
204 .*exit inf: 1.*
205 dir ok: True.*" "Inferior 1 terminated."
206
207 gdb_test "inferior 2" ".*Switching to inferior 2.*"
208 gdb_test "continue" ".*event type: continue.*
209 .*event type: exit.*
210 .*exit code: 12.*
211 .*exit inf: 2.*
212 dir ok: True.*" "Inferior 2 terminated."
213
214
215 # Test before_prompt event.
216 gdb_py_test_multiple "define new user command" \
217 "define xxz" "End with a line saying just .end.." \
218 "set variable \$x = 72" "" \
219 "set variable \$y = 93" "" \
220 "end" ""
221
222 gdb_py_test_multiple "add before_prompt listener" \
223 "python" "" \
224 "count = 0" "" \
225 "def listener():" "" \
226 " global count" "" \
227 " count = count + 1" "" \
228 "gdb.events.before_prompt.connect(listener)" "" \
229 "end" ""
230
231 gdb_test_no_output "set variable \$x = 32" "do something"
232 # Result is due to one emitted before "set var" and one emitted before
233 # this command.
234 gdb_test "python print(count)" 2 "check for before_prompt event"
235
236 gdb_test_no_output "xxz" "run a canned sequence"
237 gdb_test "python print(count)" 4 "check for before_prompt event"
This page took 0.034229 seconds and 3 git commands to generate.