testsuite: Fix some tests that write outside of the standard output directory
[deliverable/binutils-gdb.git] / gdb / testsuite / lib / trace-support.exp
CommitLineData
618f726f 1# Copyright (C) 1998-2016 Free Software Foundation, Inc.
c906108c
SS
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
e22f8b7c 5# the Free Software Foundation; either version 3 of the License, or
c906108c 6# (at your option) any later version.
e22f8b7c 7#
c906108c
SS
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.
e22f8b7c 12#
c906108c 13# You should have received a copy of the GNU General Public License
e22f8b7c 14# along with this program. If not, see <http://www.gnu.org/licenses/>.
c906108c 15
c906108c
SS
16
17#
18# Support procedures for trace testing
19#
20
21
6e7675a7
MK
22#
23# Program counter / stack pointer / frame pointer for supported targets.
24# Used in many tests, kept here to avoid duplication.
25#
26
27if [is_amd64_regs_target] {
28 set fpreg "rbp"
29 set spreg "rsp"
30 set pcreg "rip"
31} elseif [is_x86_like_target] {
32 set fpreg "ebp"
33 set spreg "esp"
34 set pcreg "eip"
35} elseif [is_aarch64_target] {
36 set fpreg "x29"
37 set spreg "sp"
38 set pcreg "pc"
39} else {
40 set fpreg "fp"
41 set spreg "sp"
42 set pcreg "pc"
43}
44
c906108c
SS
45#
46# Procedure: gdb_target_supports_trace
47# Returns true if GDB is connected to a target that supports tracing.
48# Allows tests to abort early if not running on a trace-aware target.
49#
50
51proc gdb_target_supports_trace { } {
52 global gdb_prompt
53
54 send_gdb "tstatus\n"
55 gdb_expect {
56 -re "\[Tt\]race can only be run on.*$gdb_prompt $" {
57 return 0
58 }
59 -re "\[Tt\]race can not be run on.*$gdb_prompt $" {
60 return 0
61 }
62 -re "\[Tt\]arget does not support.*$gdb_prompt $" {
63 return 0
64 }
65 -re ".*\[Ee\]rror.*$gdb_prompt $" {
66 return 0
67 }
68 -re ".*\[Ww\]arning.*$gdb_prompt $" {
69 return 0
70 }
71 -re ".*$gdb_prompt $" {
72 return 1
73 }
74 timeout {
75 return 0
76 }
77 }
78}
79
80
81#
82# Procedure: gdb_delete_tracepoints
83# Many of the tests depend on setting tracepoints at various places and
84# running until that tracepoint is reached. At times, we want to start
85# with a clean slate with respect to tracepoints, so this utility proc
86# lets us do this without duplicating this code everywhere.
87#
88
89proc gdb_delete_tracepoints {} {
90 global gdb_prompt
91
92 send_gdb "delete tracepoints\n"
93 gdb_expect 30 {
94 -re "Delete all tracepoints.*y or n.*$" {
4ec70201 95 send_gdb "y\n"
c906108c
SS
96 exp_continue
97 }
98 -re ".*$gdb_prompt $" { # This happens if there were no tracepoints }
99 timeout {
100 perror "Delete all tracepoints in delete_tracepoints (timeout)"
101 return
102 }
103 }
104 send_gdb "info tracepoints\n"
105 gdb_expect 30 {
106 -re "No tracepoints.*$gdb_prompt $" {}
107 -re "$gdb_prompt $" { perror "tracepoints not deleted" ; return }
108 timeout { perror "info tracepoints (timeout)" ; return }
109 }
110}
111
c906108c
SS
112# Define actions for a tracepoint.
113# Arguments:
c9a6ce02
PA
114# actions_command -- the command used to create the actions.
115# either "actions" or "commands".
c906108c 116# testname -- identifying string for pass/fail output
c9a6ce02 117# tracepoint -- to which tracepoint(s) do these actions apply? (optional)
c906108c
SS
118# args -- list of actions to be defined.
119# Returns:
120# zero -- success
121# non-zero -- failure
122
c9a6ce02 123proc gdb_trace_setactions_command { actions_command testname tracepoint args } {
4ec70201 124 global gdb_prompt
c906108c 125
4ec70201
PA
126 set state 0
127 set passfail "pass"
128 send_gdb "$actions_command $tracepoint\n"
129 set expected_result ""
c906108c
SS
130 gdb_expect 5 {
131 -re "No tracepoint number .*$gdb_prompt $" {
132 fail $testname
ae59b1da 133 return 1
c906108c
SS
134 }
135 -re "Enter actions for tracepoint $tracepoint.*>" {
136 if { [llength $args] > 0 } {
4ec70201
PA
137 set lastcommand "[lindex $args $state]"
138 send_gdb "[lindex $args $state]\n"
139 incr state
140 set expected_result [lindex $args $state]
141 incr state
c906108c 142 } else {
4ec70201 143 send_gdb "end\n"
c906108c 144 }
4ec70201 145 exp_continue
c906108c 146 }
a7bdde9e 147 -re "\(.*\)\[\r\n\]+\[ \t]*>$" {
c906108c 148 if { $expected_result != "" } {
4ec70201 149 regsub "^\[^\r\n\]+\[\r\n\]+" "$expect_out(1,string)" "" out
c906108c 150 if ![regexp $expected_result $out] {
4ec70201 151 set passfail "fail"
c906108c 152 }
4ec70201 153 set expected_result ""
c906108c
SS
154 }
155 if { $state < [llength $args] } {
4ec70201
PA
156 send_gdb "[lindex $args $state]\n"
157 incr state
158 set expected_result [lindex $args $state]
159 incr state
c906108c 160 } else {
4ec70201
PA
161 send_gdb "end\n"
162 set expected_result ""
c906108c 163 }
4ec70201 164 exp_continue
c906108c
SS
165 }
166 -re "\(.*\)$gdb_prompt $" {
167 if { $expected_result != "" } {
168 if ![regexp $expected_result $expect_out(1,string)] {
4ec70201 169 set passfail "fail"
c906108c 170 }
4ec70201 171 set expected_result ""
c906108c
SS
172 }
173 if { [llength $args] < $state } {
4ec70201 174 set passfail "fail"
c906108c
SS
175 }
176 }
177 default {
4ec70201 178 set passfail "fail"
c906108c
SS
179 }
180 }
181 if { $testname != "" } {
4ec70201 182 $passfail $testname
c906108c
SS
183 }
184 if { $passfail == "pass" } then {
ae59b1da 185 return 0
c906108c 186 } else {
ae59b1da 187 return 1
c906108c
SS
188 }
189}
190
c9a6ce02
PA
191# Define actions for a tracepoint, using the "actions" command. See
192# gdb_trace_setactions_command.
193#
194proc gdb_trace_setactions { testname tracepoint args } {
195 eval gdb_trace_setactions_command "actions" {$testname} {$tracepoint} $args
196}
197
198# Define actions for a tracepoint, using the "commands" command. See
199# gdb_trace_setactions_command.
200#
201proc gdb_trace_setcommands { testname tracepoint args } {
202 eval gdb_trace_setactions_command "commands" {$testname} {$tracepoint} $args
203}
204
c906108c
SS
205#
206# Procedure: gdb_tfind_test
207# Find a specified trace frame.
208# Arguments:
209# testname -- identifying string for pass/fail output
210# tfind_arg -- frame (line, PC, etc.) identifier
211# exp_res -- Expected result of frame test
212# args -- Test expression
213# Returns:
214# zero -- success
215# non-zero -- failure
216#
217
218proc gdb_tfind_test { testname tfind_arg exp_res args } {
4ec70201 219 global gdb_prompt
c906108c
SS
220
221 if { "$args" != "" } {
4ec70201
PA
222 set expr "$exp_res"
223 set exp_res "$args"
c906108c 224 } else {
4ec70201 225 set expr "(int) \$trace_frame"
c906108c 226 }
4ec70201 227 set passfail "fail"
c906108c
SS
228
229 gdb_test "tfind $tfind_arg" "" ""
4ec70201 230 send_gdb "printf \"x \%d x\\n\", $expr\n"
c906108c
SS
231 gdb_expect 10 {
232 -re "x (-*\[0-9\]+) x" {
233 if { $expect_out(1,string) == $exp_res } {
4ec70201 234 set passfail "pass"
c906108c 235 }
4ec70201 236 exp_continue
c906108c
SS
237 }
238 -re "$gdb_prompt $" { }
239 }
4ec70201 240 $passfail "$testname"
c906108c 241 if { $passfail == "pass" } then {
ae59b1da 242 return 0
c906108c 243 } else {
ae59b1da 244 return 1
c906108c
SS
245 }
246}
247
248#
249# Procedure: gdb_readexpr
250# Arguments:
251# gdb_expr -- the expression whose value is desired
252# Returns:
253# the value of gdb_expr, as evaluated by gdb.
254# [FIXME: returns -1 on error, which is sometimes a legit value]
255#
256
257proc gdb_readexpr { gdb_expr } {
4ec70201 258 global gdb_prompt
c906108c 259
4ec70201 260 set result -1
c906108c
SS
261 send_gdb "print $gdb_expr\n"
262 gdb_expect 5 {
263 -re "\[$\].*= (\[0-9\]+).*$gdb_prompt $" {
4ec70201 264 set result $expect_out(1,string)
c906108c
SS
265 }
266 -re "$gdb_prompt $" { }
267 default { }
268 }
ae59b1da 269 return $result
c906108c
SS
270}
271
272#
273# Procedure: gdb_gettpnum
274# Arguments:
275# tracepoint (optional): if supplied, set a tracepoint here.
276# Returns:
277# the tracepoint ID of the most recently set tracepoint.
278#
279
280proc gdb_gettpnum { tracepoint } {
4ec70201 281 global gdb_prompt
c906108c
SS
282
283 if { $tracepoint != "" } {
284 gdb_test "trace $tracepoint" "" ""
285 }
ae59b1da 286 return [gdb_readexpr "\$tpnum"]
c906108c
SS
287}
288
289
290#
291# Procedure: gdb_find_function_baseline
292# Arguments:
293# func_name -- name of source function
294# Returns:
295# Sourcefile line of function definition (open curly brace),
296# or -1 on failure. Caller must check return value.
297# Note:
298# Works only for open curly brace at beginning of source line!
299#
300
301proc gdb_find_function_baseline { func_name } {
4ec70201 302 global gdb_prompt
c906108c 303
4ec70201 304 set baseline -1
c906108c
SS
305
306 send_gdb "list $func_name\n"
307# gdb_expect {
308# -re "\[\r\n\]\[\{\].*$gdb_prompt $" {
309# set baseline 1
310# }
311# }
312}
313
314#
315# Procedure: gdb_find_function_baseline
316# Arguments:
317# filename: name of source file of desired function.
318# Returns:
319# Sourcefile line of function definition (open curly brace),
320# or -1 on failure. Caller must check return value.
321# Note:
322# Works only for open curly brace at beginning of source line!
323#
324
325proc gdb_find_recursion_test_baseline { filename } {
4ec70201 326 global gdb_prompt
c906108c 327
4ec70201 328 set baseline -1
c906108c
SS
329
330 gdb_test "list $filename:1" "" ""
331 send_gdb "search gdb_recursion_test line 0\n"
332 gdb_expect {
333 -re "(\[0-9\]+)\[\t \]+\{.*line 0.*$gdb_prompt $" {
4ec70201 334 set baseline $expect_out(1,string)
c906108c
SS
335 }
336 -re "$gdb_prompt $" { }
337 default { }
338 }
ae59b1da 339 return $baseline
c906108c 340}
c0d4d1c0
YQ
341
342# Return the location of the IPA library.
343
344proc get_in_proc_agent {} {
345 global objdir
346
347 if [target_info exists in_proc_agent] {
348 return [target_info in_proc_agent]
349 } else {
350 return $objdir/../gdbserver/libinproctrace.so
351 }
352}
b4429ea2
YQ
353
354# Execute BINFILE on target to generate tracefile. Return 1 if
355# tracefile is generated successfully, return 0 otherwise.
356
357proc generate_tracefile { binfile } {
358 set status [remote_exec target "$binfile"]
359
360 if { [lindex $status 0] != 0 } {
361 # Failed to execute $binfile, for example on bare metal targets.
362 # Alternatively, load the binary and run it. If target doesn't
363 # have fileio capabilities, tracefile can't be generated. Skip
364 # the test.
365 if [target_info exists gdb,nofileio] {
366 return 0
367 }
368
369 clean_restart $binfile
370
371 if ![runto_main] then {
372 return 0
373 }
374 gdb_continue_to_end "" continue 1
375 gdb_exit
376 }
377
378 return 1
379}
This page took 1.709365 seconds and 4 git commands to generate.