1# Copyright 2003-2013 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 was written by Elena Zannoni (ezannoni@cygnus.com)
17
18
19# are we on a target board? If so, don't run these tests.
20# note: this is necessary because we cannot use runto_main (which would
21# work for remote targets too) because of the different prompt we get
22# when using annotation level 2.
23#
24if [is_remote target] then {
25    return 0
26}
27
28
29#
30# test running programs
31#
32
33set testfile "annota3"
34set srcfile ${testfile}.c
35set binfile ${objdir}/${subdir}/${testfile}$EXEEXT
36
37if  { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {debug nowarnings}] != "" } {
38    untested annota3.exp
39    return -1
40}
41
42
43gdb_exit
44gdb_start
45gdb_reinitialize_dir $srcdir/$subdir
46gdb_load ${binfile}
47
48# The commands we test here produce many lines of output; disable "press
49# <return> to continue" prompts.
50gdb_test_no_output "set height 0"
51
52#
53# break at main
54#
55
56set main_line [gdb_get_line_number "break main"]
57
58gdb_test "break main" \
59    "Breakpoint.*at.* file .*$srcfile, line $main_line\\." \
60    "breakpoint main"
61
62
63# NOTE: this prompt is OK only when the annotation level is > 1
64
65# NOTE: When this prompt is in use the gdb_test procedure cannot be
66# used because it assumes that the last char after the gdb_prompt is a
67# white space. This is not true with this annotated prompt. So we must
68# use send_gdb and gdb_expect or gdb_expect_list.
69
70set old_gdb_prompt $gdb_prompt
71set gdb_prompt "\r\n\032\032pre-prompt\r\n$gdb_prompt \r\n\032\032prompt\r\n"
72
73
74
75#
76# set the annotation level to 3
77#
78# of course, this will test:
79# annotate-pre-prompt
80# annotate-prompt
81# annotate-post-prompt (in the next block)
82#
83send_gdb "set annotate 3\n"
84gdb_expect_list "annotation set at level 3" "\r\n$gdb_prompt$" {
85    "set annotate 3"
86}
87
88#
89# if construct:
90#
91gdb_test_multiple "if 1" "start if construct" {
92    -re "^if 1\r\n\r\n\032\032post-prompt\r\n\r\n\032\032pre-commands\r\n >\r\n\032\032commands\r\n$" {
93	pass "start if construct"
94    }
95}
96
97gdb_test_multiple "end" "end if construct" {
98    -re "^end\r\n\r\n\032\032post-commands\r\n$gdb_prompt$" {
99 	pass "end if construct"
100    }
101}
102
103#
104# info break:
105#
106send_gdb "info break\n"
107gdb_expect_list "breakpoint info" "$gdb_prompt$" [concat {
108    "\r\n\032\032post-prompt\r\n"
109    "Num     Type           Disp Enb Address    +What\r\n" } [list \
110    "1       breakpoint     keep y   0x\[0-9a-zA-Z\]+ +in main at .*annota3.c:$main_line\r\n"]]
111
112
113#
114# run to a break point will test:
115#
116#exp_internal 1
117send_gdb "run\n"
118gdb_expect_list "run until main breakpoint" "$gdb_prompt$" [concat {
119    "\r\n\032\032post-prompt\r\n"
120    "Starting program: .*annota3(|\.exe) \r\n"
121    "\r\n\032\032starting\r\n"
122    "\r\n\032\032breakpoint 1\r\n"
123    "\r\n"
124    "Breakpoint 1, "
125    "\r\n\032\032frame-begin 0 0x\[0-9a-z\]+\r\n" } [list \
126    "main \\(\\) at .*annota3.c:$main_line\r\n"] [list \
127    "\r\n\032\032source.*annota3.c:$main_line:.*:beg:0x\[0-9a-z\]+\r\n"] {
128    "\r\n\032\032stopped\r\n"
129}]
130#exp_internal 0
131#exit 0
132
133#
134# Let's do a next, to get to a point where the array is initialized
135# We don't care about the annotated output for this operation, it is the same as
136# the one produced by run above
137#
138send_gdb "next\n"
139gdb_expect_list "go after array init line" "$gdb_prompt$" {
140    "\r\n\032\032post-prompt\r\n"
141    "\r\n\032\032starting\r\n"
142    "\r\n\032\032source .*annota3.c:\[0-9\]+:\[0-9\]+:beg:0x\[0-9a-z\]+\r\n"
143    "\r\n\032\032stopped\r\n"
144}
145
146
147#
148# printing the array:
149#
150send_gdb "print my_array\n"
151gdb_expect_list "print array" "$gdb_prompt$" {
152    "\r\n\032\032post-prompt\r\n"
153    ".*= .1, 2, 3.\r\n"
154}
155
156
157#
158# this should generate an error message, so to test:
159# annotate-error-begin
160# FIXME: annotate-error not tested
161#
162
163#exp_internal 1
164send_gdb "print non_existent_value\n"
165gdb_expect_list "print non_existent_value" "$gdb_prompt$" {
166    "\r\n\032\032post-prompt\r\n"
167    "\r\n\032\032error-begin\r\n"
168    "No symbol \"non_existent_value\" in current context.\r\n"
169    "\r\n\032\032error\r\n"
170}
171
172
173#
174# break at signal handler
175#
176send_gdb "break handle_USR1\n"
177gdb_expect_list "breakpoint handle_USR1" "$gdb_prompt$" {
178    "\r\n\032\032post-prompt\r\n"
179    "Breakpoint.*at 0x\[0-9a-z\]+: file.*annota3.c, line.*\r\n"
180}
181
182#
183# break at printf. When we are stopped at printf, we can test
184#
185send_gdb "break printf\n"
186gdb_expect_list "breakpoint printf" "$gdb_prompt$" {
187    "\r\n\032\032post-prompt\r\n"
188    "Breakpoint.*at 0x\[0-9a-z\]+.*"
189}
190
191#
192# get to printf
193#
194send_gdb "continue\n"
195gdb_expect_list "continue to printf" "$gdb_prompt$" {
196    "\r\n\032\032post-prompt\r\n"
197    "Continuing.\r\n"
198    "\r\n\032\032starting\r\n"
199    "\r\n\032\032breakpoint 3\r\n"
200    "\r\n"
201    "Breakpoint 3, \[^\r\n\]*\r\n"
202    "\r\n\032\032stopped\r\n"
203}
204
205send_gdb "backtrace\n"
206gdb_expect_list "backtrace from shlibrary" "$gdb_prompt$" {
207    "\r\n\032\032post-prompt\r\n"
208    "#0 .* .*printf(@\[^ ]*)? \[^\r\n\]*\r\n"
209    "#1 .* main \[^\r\n\]*\r\n"
210}
211
212
213#
214# test printing a frame with some arguments:
215#
216
217if [target_info exists gdb,nosignals] {
218    unsupported "send SIGUSR1"
219    unsupported "backtrace @ signal handler"
220} else {
221    send_gdb "signal SIGUSR1\n"
222    gdb_expect_list "send SIGUSR1" "$gdb_prompt$" {
223	"\r\n\032\032post-prompt\r\n"
224	"Continuing with signal SIGUSR1.\r\n"
225	"\r\n\032\032starting\r\n"
226	"\r\n\032\032breakpoint 2\r\n"
227	"\r\n"
228	"Breakpoint 2, "
229	"\r\n\032\032frame-begin 0 0x\[0-9a-z\]+\r\n"
230	"handle_USR1 \\(sig=\[0-9\]+\\) at .*annota3.c:\[0-9\]+\r\n"
231	"\r\n\032\032source .*annota3.c:\[0-9\]+:\[0-9\]+:beg:0x\[0-9a-z\]+\r\n"
232	"\r\n\032\032stopped\r\n"
233    }
234
235    #
236    # test:
237    #
238    send_gdb "backtrace\n"
239    gdb_expect_list "backtrace @ signal handler" "$gdb_prompt$" {
240	"#0 +handle_USR1 \[^\r\n\]+\r\n"
241	"#1 +.signal handler called.\r\n"
242	"#2 .* .*printf(@\[^ \]*)? \[^\r\n\]+\r\n"
243	"#3 .* main \[^\r\n\]+\r\n"
244    }
245}
246
247#
248# delete all the breakpoints
249#
250send_gdb "delete 1\n"
251gdb_expect_list "delete bp 1" "$gdb_prompt$" {
252    "\r\n\032\032post-prompt\r\n"
253}
254
255send_gdb "delete 2\n"
256gdb_expect_list "delete bp 2" "$gdb_prompt$" {
257    "\r\n\032\032post-prompt\r\n"
258}
259
260send_gdb "delete 3\n"
261gdb_expect_list "delete bp 3" "$gdb_prompt$" {
262    "\r\n\032\032post-prompt\r\n"
263}
264
265#
266# break at main, after value is initialized. This is in preparation
267# to test the annotate output for the display command.
268#
269send_gdb "break main\n"
270gdb_expect_list "break at main" "$gdb_prompt$" [concat {
271    "\r\n\032\032post-prompt\r\n" } [list \
272    "Breakpoint 4 at 0x\[0-9a-z\]+: file .*annota3.c, line $main_line.\r\n"]]
273
274#
275# display the value
276#
277send_gdb "display value\n"
278gdb_expect_list "set up display" "$gdb_prompt$" {
279    "\r\n\032\032post-prompt\r\n"
280    "1: value = 7\r\n"
281}
282
283
284# should ask query. Test annotate-query.
285# we don't care about anything else here, only the query.
286
287send_gdb "run\n"
288gdb_expect {
289  -re "pre-query.*already.*\\(y or n\\).*query\r\n" {
290         send_gdb "y\n"
291         gdb_expect {
292	     -re ".*post-query.*$gdb_prompt$" \
293		     { pass "re-run" }
294	     -re ".*$gdb_prompt$"  { fail "re-run" }
295	     timeout { fail "re-run (timeout)" }
296	 }
297     }
298  -re ".*$gdb_prompt$"  { fail "re-run" }
299  timeout { fail "re-run (timeout)" }
300}
301
302#
303# Test that breakpoints-invalid is issued once and only once for
304# breakpoint ignore count changes, after annotation stopped.
305# NOTE: breakpoints-invalid annotations have been removed from
306# level 3 but keep these tests for continuity and comparison
307# with annota1.exp.
308
309set value_inc_line [gdb_get_line_number "increment value"]
310
311send_gdb "break $value_inc_line\n"
312gdb_expect_list  "break at value++" "$gdb_prompt$" [concat {
313    "\r\n\032\032post-prompt\r\n" } [list \
314    "Breakpoint 5 at 0x\[0-9a-z\]+: file .*annota3.c, line $value_inc_line.\r\n"]]
315
316send_gdb "ignore 5 4\n"
317gdb_expect_list "ignore 5 4" "$gdb_prompt$" {
318    "\r\n\032\032post-prompt\r\n"
319    "Will ignore next 4 crossings of breakpoint 5"
320    "\r\n"
321}
322
323send_gdb "continue\n"
324gdb_expect_list "annotate ignore count change" "$gdb_prompt$" [concat {
325    "\r\n\032\032post-prompt\r\n"
326    "\r\n\032\032breakpoint 5\r\n"
327    "\r\n"
328    "Breakpoint 5, "
329    "\r\n\032\032frame-begin 0 0x\[0-9a-z\]+\r\n" } [list \
330    "main \\(\\) at .*annota3.c:$value_inc_line\r\n"] [list \
331    "\r\n\032\032source .*annota3.c:$value_inc_line:\[0-9\]+:beg:0x\[0-9a-z\]+\r\n"] {
332    "1: value = 11\r\n"
333    "\r\n\032\032stopped\r\n"
334}]
335
336# check that ignore command is working, or the above can provide
337# misleading assurance ...
338
339send_gdb "next\n"
340gdb_expect_list "next to exit loop" "$gdb_prompt$"  {
341    "\r\n\032\032post-prompt\r\n"
342    "\r\n\032\032starting\r\n"
343    "\r\n\032\032source.*annota3.c:\[0-9\]+:\[0-9\]+:beg:0x\[0-9a-z\]+\r\n"
344    "1: value = 12\r\n"
345    "\r\n\032\032stopped\r\n"
346}
347
348set after_loop_line [gdb_get_line_number "after loop"]
349
350send_gdb "next\n"
351gdb_expect_list "breakpoint ignore count" "$gdb_prompt$" [concat {
352    "\r\n\032\032post-prompt\r\n"
353    "\r\n\032\032starting\r\n" } [list \
354    "\r\n\032\032source.*annota3.c:$after_loop_line:\[0-9\]+:beg:0x\[0-9a-z\]+\r\n"] {
355    "1: value = 12\r\n"
356    "\r\n\032\032stopped\r\n"
357}]
358
359# Get the inferior's PID for later.
360
361set test "get inferior pid"
362set pid -1
363gdb_test_multiple "info inferior 1" "$test" {
364    -re "process (\[0-9\]*).*$gdb_prompt$" {
365	set pid $expect_out(1,string)
366	pass "$test"
367    }
368}
369
370#
371# Send a signal that is not handled
372#
373# SIGTRAP signals are dropped before they get to the inferior process
374# on hpux11.  In theory, this behaivor can be controlled by setting
375# TTEO_NORM_SIGTRAP in the inferior, but doing so did not cause
376# the signal to be properly delivered.
377#
378# It has been verified that other signals will be delivered.  However,
379# rather than twiddle the test, I choose to leave it as-is as it
380# exposes an interesting failure on hpux11.
381
382if [target_info exists gdb,nosignals] {
383    unsupported "signal sent"
384} else {
385    setup_xfail hppa*-*-hpux11*
386    send_gdb "signal SIGTRAP\n"
387    gdb_expect_list "signal sent" "$gdb_prompt$" {
388	"\r\n\032\032post-prompt\r\n"
389	"Continuing with signal SIGTRAP.\r\n"
390	"\r\n\032\032starting\r\n"
391	"\r\n\032\032signalled\r\n"
392	"\r\nProgram terminated with signal SIGTRAP, Trace.breakpoint trap.\r\n"
393	"The program no longer exists.\r\n"
394	"\r\n\032\032stopped\r\n"
395    }
396}
397
398
399# Check for production of a core file and remove it!
400
401set test "cleanup core file"
402if { [remote_file host exists core] } {
403    remote_file host delete core
404    pass "$test (removed)"
405} elseif { $pid != -1 && [remote_file host exists core.$pid] } {
406    remote_file host delete core.$pid
407    pass "$test (removed)"
408} else {
409    pass "$test (not dumped)"
410}
411
412# restore the original prompt for the rest of the testsuite
413
414set gdb_prompt $old_gdb_prompt
415