Commit | Line | Data |
---|---|---|
b811d2c2 | 1 | # Copyright 1992-2020 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 | # This file was written by Fred Fish. (fnf@cygnus.com) |
17 | ||
18 | # Generic gdb subroutines that should work for any target. If these | |
19 | # need to be modified for any target, it can be done with a variable | |
20 | # or by passing arguments. | |
21 | ||
97c3f1f3 JK |
22 | if {$tool == ""} { |
23 | # Tests would fail, logs on get_compiler_info() would be missing. | |
24 | send_error "`site.exp' not found, run `make site.exp'!\n" | |
25 | exit 2 | |
26 | } | |
27 | ||
c906108c | 28 | load_lib libgloss.exp |
17e1c970 | 29 | load_lib cache.exp |
a25eb028 | 30 | load_lib gdb-utils.exp |
e309aa65 | 31 | load_lib memory.exp |
c906108c SS |
32 | |
33 | global GDB | |
c906108c | 34 | |
f71c18e7 PA |
35 | # The spawn ID used for I/O interaction with the inferior. For native |
36 | # targets, or remote targets that can do I/O through GDB | |
37 | # (semi-hosting) this will be the same as the host/GDB's spawn ID. | |
38 | # Otherwise, the board may set this to some other spawn ID. E.g., | |
39 | # when debugging with GDBserver, this is set to GDBserver's spawn ID, | |
40 | # so input/output is done on gdbserver's tty. | |
41 | global inferior_spawn_id | |
42 | ||
c906108c | 43 | if [info exists TOOL_EXECUTABLE] { |
4ec70201 | 44 | set GDB $TOOL_EXECUTABLE |
c906108c SS |
45 | } |
46 | if ![info exists GDB] { | |
47 | if ![is_remote host] { | |
48 | set GDB [findfile $base_dir/../../gdb/gdb "$base_dir/../../gdb/gdb" [transform gdb]] | |
49 | } else { | |
4ec70201 | 50 | set GDB [transform gdb] |
c906108c SS |
51 | } |
52 | } | |
53 | verbose "using GDB = $GDB" 2 | |
54 | ||
6b8ce727 DE |
55 | # GDBFLAGS is available for the user to set on the command line. |
56 | # E.g. make check RUNTESTFLAGS=GDBFLAGS=mumble | |
57 | # Testcases may use it to add additional flags, but they must: | |
58 | # - append new flags, not overwrite | |
59 | # - restore the original value when done | |
c906108c SS |
60 | global GDBFLAGS |
61 | if ![info exists GDBFLAGS] { | |
6b8ce727 | 62 | set GDBFLAGS "" |
c906108c SS |
63 | } |
64 | verbose "using GDBFLAGS = $GDBFLAGS" 2 | |
65 | ||
2f4e0a80 DE |
66 | # Make the build data directory available to tests. |
67 | set BUILD_DATA_DIRECTORY "[pwd]/../data-directory" | |
68 | ||
6b8ce727 | 69 | # INTERNAL_GDBFLAGS contains flags that the testsuite requires. |
1be00882 DE |
70 | global INTERNAL_GDBFLAGS |
71 | if ![info exists INTERNAL_GDBFLAGS] { | |
2f4e0a80 | 72 | set INTERNAL_GDBFLAGS "-nw -nx -data-directory $BUILD_DATA_DIRECTORY" |
1be00882 | 73 | } |
6b8ce727 | 74 | |
9e0b60a8 | 75 | # The variable gdb_prompt is a regexp which matches the gdb prompt. |
3714cea7 DE |
76 | # Set it if it is not already set. This is also set by default_gdb_init |
77 | # but it's not clear what removing one of them will break. | |
78 | # See with_gdb_prompt for more details on prompt handling. | |
c906108c | 79 | global gdb_prompt |
9e0b60a8 | 80 | if ![info exists gdb_prompt] then { |
3714cea7 | 81 | set gdb_prompt "\\(gdb\\)" |
c906108c SS |
82 | } |
83 | ||
94696ad3 | 84 | # A regexp that matches the pagination prompt. |
eb6af809 TT |
85 | set pagination_prompt \ |
86 | "--Type <RET> for more, q to quit, c to continue without paging--" | |
94696ad3 | 87 | |
6006a3a1 BR |
88 | # The variable fullname_syntax_POSIX is a regexp which matches a POSIX |
89 | # absolute path ie. /foo/ | |
d0b76dc6 | 90 | set fullname_syntax_POSIX {/[^\n]*/} |
6006a3a1 BR |
91 | # The variable fullname_syntax_UNC is a regexp which matches a Windows |
92 | # UNC path ie. \\D\foo\ | |
d0b76dc6 | 93 | set fullname_syntax_UNC {\\\\[^\\]+\\[^\n]+\\} |
6006a3a1 BR |
94 | # The variable fullname_syntax_DOS_CASE is a regexp which matches a |
95 | # particular DOS case that GDB most likely will output | |
96 | # ie. \foo\, but don't match \\.*\ | |
d0b76dc6 | 97 | set fullname_syntax_DOS_CASE {\\[^\\][^\n]*\\} |
6006a3a1 BR |
98 | # The variable fullname_syntax_DOS is a regexp which matches a DOS path |
99 | # ie. a:\foo\ && a:foo\ | |
d0b76dc6 | 100 | set fullname_syntax_DOS {[a-zA-Z]:[^\n]*\\} |
6006a3a1 BR |
101 | # The variable fullname_syntax is a regexp which matches what GDB considers |
102 | # an absolute path. It is currently debatable if the Windows style paths | |
103 | # d:foo and \abc should be considered valid as an absolute path. | |
104 | # Also, the purpse of this regexp is not to recognize a well formed | |
105 | # absolute path, but to say with certainty that a path is absolute. | |
106 | set fullname_syntax "($fullname_syntax_POSIX|$fullname_syntax_UNC|$fullname_syntax_DOS_CASE|$fullname_syntax_DOS)" | |
107 | ||
93076499 ND |
108 | # Needed for some tests under Cygwin. |
109 | global EXEEXT | |
110 | global env | |
111 | ||
112 | if ![info exists env(EXEEXT)] { | |
113 | set EXEEXT "" | |
114 | } else { | |
115 | set EXEEXT $env(EXEEXT) | |
116 | } | |
117 | ||
bb2bed55 NR |
118 | set octal "\[0-7\]+" |
119 | ||
f90ac7c2 | 120 | set inferior_exited_re "(?:\\\[Inferior \[0-9\]+ \\(\[^\n\r\]*\\) exited)" |
fda326dd | 121 | |
fad0c9fb PA |
122 | # A regular expression that matches a value history number. |
123 | # E.g., $1, $2, etc. | |
124 | set valnum_re "\\\$$decimal" | |
125 | ||
085dd6e6 JM |
126 | ### Only procedures should come after this point. |
127 | ||
c906108c SS |
128 | # |
129 | # gdb_version -- extract and print the version number of GDB | |
130 | # | |
131 | proc default_gdb_version {} { | |
132 | global GDB | |
6b8ce727 | 133 | global INTERNAL_GDBFLAGS GDBFLAGS |
c906108c | 134 | global gdb_prompt |
5e92f71a TT |
135 | global inotify_pid |
136 | ||
137 | if {[info exists inotify_pid]} { | |
138 | eval exec kill $inotify_pid | |
139 | } | |
140 | ||
fa335448 | 141 | set output [remote_exec host "$GDB $INTERNAL_GDBFLAGS --version"] |
4ec70201 | 142 | set tmp [lindex $output 1] |
c906108c SS |
143 | set version "" |
144 | regexp " \[0-9\]\[^ \t\n\r\]+" "$tmp" version | |
145 | if ![is_remote host] { | |
6b8ce727 | 146 | clone_output "[which $GDB] version $version $INTERNAL_GDBFLAGS $GDBFLAGS\n" |
c906108c | 147 | } else { |
6b8ce727 | 148 | clone_output "$GDB on remote host version $version $INTERNAL_GDBFLAGS $GDBFLAGS\n" |
c906108c SS |
149 | } |
150 | } | |
151 | ||
152 | proc gdb_version { } { | |
ae59b1da | 153 | return [default_gdb_version] |
c906108c SS |
154 | } |
155 | ||
156 | # | |
157 | # gdb_unload -- unload a file if one is loaded | |
608e2dbb | 158 | # Return 0 on success, -1 on error. |
c906108c SS |
159 | # |
160 | ||
161 | proc gdb_unload {} { | |
c906108c SS |
162 | global GDB |
163 | global gdb_prompt | |
164 | send_gdb "file\n" | |
165 | gdb_expect 60 { | |
166 | -re "No executable file now\[^\r\n\]*\[\r\n\]" { exp_continue } | |
167 | -re "No symbol file now\[^\r\n\]*\[\r\n\]" { exp_continue } | |
959e7469 | 168 | -re "A program is being debugged already.*Are you sure you want to change the file.*y or n. $" { |
f9e2e39d | 169 | send_gdb "y\n" answer |
c906108c SS |
170 | exp_continue |
171 | } | |
172 | -re "Discard symbol table from .*y or n.*$" { | |
f9e2e39d | 173 | send_gdb "y\n" answer |
c906108c SS |
174 | exp_continue |
175 | } | |
176 | -re "$gdb_prompt $" {} | |
177 | timeout { | |
975531db | 178 | perror "couldn't unload file in $GDB (timeout)." |
c906108c SS |
179 | return -1 |
180 | } | |
181 | } | |
608e2dbb | 182 | return 0 |
c906108c SS |
183 | } |
184 | ||
185 | # Many of the tests depend on setting breakpoints at various places and | |
186 | # running until that breakpoint is reached. At times, we want to start | |
187 | # with a clean-slate with respect to breakpoints, so this utility proc | |
188 | # lets us do this without duplicating this code everywhere. | |
189 | # | |
190 | ||
191 | proc delete_breakpoints {} { | |
192 | global gdb_prompt | |
193 | ||
a0b3c4fd JM |
194 | # we need a larger timeout value here or this thing just confuses |
195 | # itself. May need a better implementation if possible. - guo | |
196 | # | |
d8b901ed PA |
197 | set timeout 100 |
198 | ||
199 | set msg "delete all breakpoints in delete_breakpoints" | |
200 | set deleted 0 | |
201 | gdb_test_multiple "delete breakpoints" "$msg" { | |
202 | -re "Delete all breakpoints.*y or n.*$" { | |
f9e2e39d | 203 | send_gdb "y\n" answer |
c906108c SS |
204 | exp_continue |
205 | } | |
d8b901ed PA |
206 | -re "$gdb_prompt $" { |
207 | set deleted 1 | |
208 | } | |
c906108c | 209 | } |
d8b901ed PA |
210 | |
211 | if {$deleted} { | |
212 | # Confirm with "info breakpoints". | |
213 | set deleted 0 | |
214 | set msg "info breakpoints" | |
215 | gdb_test_multiple $msg $msg { | |
216 | -re "No breakpoints or watchpoints..*$gdb_prompt $" { | |
217 | set deleted 1 | |
218 | } | |
219 | -re "$gdb_prompt $" { | |
220 | } | |
c906108c | 221 | } |
d8b901ed PA |
222 | } |
223 | ||
224 | if {!$deleted} { | |
225 | perror "breakpoints not deleted" | |
c906108c SS |
226 | } |
227 | } | |
228 | ||
300b6685 PA |
229 | # Returns true iff the target supports using the "run" command. |
230 | ||
231 | proc target_can_use_run_cmd {} { | |
232 | if [target_info exists use_gdb_stub] { | |
233 | # In this case, when we connect, the inferior is already | |
234 | # running. | |
235 | return 0 | |
236 | } | |
237 | ||
238 | # Assume yes. | |
239 | return 1 | |
240 | } | |
241 | ||
c906108c SS |
242 | # Generic run command. |
243 | # | |
244 | # The second pattern below matches up to the first newline *only*. | |
245 | # Using ``.*$'' could swallow up output that we attempt to match | |
246 | # elsewhere. | |
247 | # | |
1d41d75c DE |
248 | # N.B. This function does not wait for gdb to return to the prompt, |
249 | # that is the caller's responsibility. | |
250 | ||
c906108c | 251 | proc gdb_run_cmd {args} { |
e11ac3a3 | 252 | global gdb_prompt use_gdb_stub |
c906108c | 253 | |
a25eb028 MR |
254 | foreach command [gdb_init_commands] { |
255 | send_gdb "$command\n" | |
c906108c SS |
256 | gdb_expect 30 { |
257 | -re "$gdb_prompt $" { } | |
258 | default { | |
4ec70201 PA |
259 | perror "gdb_init_command for target failed" |
260 | return | |
c906108c SS |
261 | } |
262 | } | |
263 | } | |
264 | ||
e11ac3a3 | 265 | if $use_gdb_stub { |
c906108c | 266 | if [target_info exists gdb,do_reload_on_run] { |
b741e217 | 267 | if { [gdb_reload] != 0 } { |
4ec70201 | 268 | return |
917317f4 | 269 | } |
4ec70201 | 270 | send_gdb "continue\n" |
c906108c SS |
271 | gdb_expect 60 { |
272 | -re "Continu\[^\r\n\]*\[\r\n\]" {} | |
273 | default {} | |
274 | } | |
4ec70201 | 275 | return |
c906108c SS |
276 | } |
277 | ||
278 | if [target_info exists gdb,start_symbol] { | |
4ec70201 | 279 | set start [target_info gdb,start_symbol] |
c906108c | 280 | } else { |
4ec70201 | 281 | set start "start" |
c906108c SS |
282 | } |
283 | send_gdb "jump *$start\n" | |
4ec70201 | 284 | set start_attempt 1 |
917317f4 JM |
285 | while { $start_attempt } { |
286 | # Cap (re)start attempts at three to ensure that this loop | |
287 | # always eventually fails. Don't worry about trying to be | |
288 | # clever and not send a command when it has failed. | |
289 | if [expr $start_attempt > 3] { | |
4ec70201 PA |
290 | perror "Jump to start() failed (retry count exceeded)" |
291 | return | |
c906108c | 292 | } |
4ec70201 | 293 | set start_attempt [expr $start_attempt + 1] |
917317f4 JM |
294 | gdb_expect 30 { |
295 | -re "Continuing at \[^\r\n\]*\[\r\n\]" { | |
4ec70201 | 296 | set start_attempt 0 |
917317f4 JM |
297 | } |
298 | -re "No symbol \"_start\" in current.*$gdb_prompt $" { | |
4ec70201 PA |
299 | perror "Can't find start symbol to run in gdb_run" |
300 | return | |
917317f4 JM |
301 | } |
302 | -re "No symbol \"start\" in current.*$gdb_prompt $" { | |
4ec70201 | 303 | send_gdb "jump *_start\n" |
917317f4 JM |
304 | } |
305 | -re "No symbol.*context.*$gdb_prompt $" { | |
4ec70201 | 306 | set start_attempt 0 |
917317f4 JM |
307 | } |
308 | -re "Line.* Jump anyway.*y or n. $" { | |
f9e2e39d | 309 | send_gdb "y\n" answer |
917317f4 JM |
310 | } |
311 | -re "The program is not being run.*$gdb_prompt $" { | |
b741e217 | 312 | if { [gdb_reload] != 0 } { |
4ec70201 | 313 | return |
917317f4 | 314 | } |
4ec70201 | 315 | send_gdb "jump *$start\n" |
917317f4 JM |
316 | } |
317 | timeout { | |
4ec70201 | 318 | perror "Jump to start() failed (timeout)" |
917317f4 JM |
319 | return |
320 | } | |
c906108c | 321 | } |
c906108c | 322 | } |
c906108c SS |
323 | return |
324 | } | |
83f66e8f DJ |
325 | |
326 | if [target_info exists gdb,do_reload_on_run] { | |
b741e217 | 327 | if { [gdb_reload] != 0 } { |
4ec70201 | 328 | return |
83f66e8f DJ |
329 | } |
330 | } | |
c906108c SS |
331 | send_gdb "run $args\n" |
332 | # This doesn't work quite right yet. | |
5aa7ddc2 PM |
333 | # Use -notransfer here so that test cases (like chng-sym.exp) |
334 | # may test for additional start-up messages. | |
335 | gdb_expect 60 { | |
c906108c | 336 | -re "The program .* has been started already.*y or n. $" { |
f9e2e39d | 337 | send_gdb "y\n" answer |
c906108c SS |
338 | exp_continue |
339 | } | |
bbb88ebf | 340 | -notransfer -re "Starting program: \[^\r\n\]*" {} |
8e46892c JK |
341 | -notransfer -re "$gdb_prompt $" { |
342 | # There is no more input expected. | |
343 | } | |
c906108c SS |
344 | } |
345 | } | |
346 | ||
b741e217 DJ |
347 | # Generic start command. Return 0 if we could start the program, -1 |
348 | # if we could not. | |
1d41d75c DE |
349 | # |
350 | # N.B. This function does not wait for gdb to return to the prompt, | |
351 | # that is the caller's responsibility. | |
b741e217 DJ |
352 | |
353 | proc gdb_start_cmd {args} { | |
e11ac3a3 | 354 | global gdb_prompt use_gdb_stub |
b741e217 | 355 | |
a25eb028 MR |
356 | foreach command [gdb_init_commands] { |
357 | send_gdb "$command\n" | |
b741e217 DJ |
358 | gdb_expect 30 { |
359 | -re "$gdb_prompt $" { } | |
360 | default { | |
4ec70201 | 361 | perror "gdb_init_command for target failed" |
ae59b1da | 362 | return -1 |
b741e217 DJ |
363 | } |
364 | } | |
365 | } | |
366 | ||
e11ac3a3 | 367 | if $use_gdb_stub { |
b741e217 DJ |
368 | return -1 |
369 | } | |
370 | ||
371 | send_gdb "start $args\n" | |
2de75e71 JB |
372 | # Use -notransfer here so that test cases (like chng-sym.exp) |
373 | # may test for additional start-up messages. | |
b741e217 DJ |
374 | gdb_expect 60 { |
375 | -re "The program .* has been started already.*y or n. $" { | |
f9e2e39d | 376 | send_gdb "y\n" answer |
b741e217 DJ |
377 | exp_continue |
378 | } | |
b741e217 DJ |
379 | -notransfer -re "Starting program: \[^\r\n\]*" { |
380 | return 0 | |
381 | } | |
382 | } | |
383 | return -1 | |
384 | } | |
385 | ||
4e5a4f58 JB |
386 | # Generic starti command. Return 0 if we could start the program, -1 |
387 | # if we could not. | |
388 | # | |
389 | # N.B. This function does not wait for gdb to return to the prompt, | |
390 | # that is the caller's responsibility. | |
391 | ||
392 | proc gdb_starti_cmd {args} { | |
393 | global gdb_prompt use_gdb_stub | |
394 | ||
395 | foreach command [gdb_init_commands] { | |
396 | send_gdb "$command\n" | |
397 | gdb_expect 30 { | |
398 | -re "$gdb_prompt $" { } | |
399 | default { | |
400 | perror "gdb_init_command for target failed" | |
401 | return -1 | |
402 | } | |
403 | } | |
404 | } | |
405 | ||
406 | if $use_gdb_stub { | |
407 | return -1 | |
408 | } | |
409 | ||
410 | send_gdb "starti $args\n" | |
411 | gdb_expect 60 { | |
412 | -re "The program .* has been started already.*y or n. $" { | |
f9e2e39d | 413 | send_gdb "y\n" answer |
4e5a4f58 JB |
414 | exp_continue |
415 | } | |
416 | -re "Starting program: \[^\r\n\]*" { | |
417 | return 0 | |
418 | } | |
419 | } | |
420 | return -1 | |
421 | } | |
422 | ||
78a1a894 | 423 | # Set a breakpoint at FUNCTION. If there is an additional argument it is |
55cd6f92 | 424 | # a list of options; the supported options are allow-pending, temporary, |
a20714ff | 425 | # message, no-message, passfail and qualified. |
5b7d0050 DE |
426 | # The result is 1 for success, 0 for failure. |
427 | # | |
428 | # Note: The handling of message vs no-message is messed up, but it's based | |
429 | # on historical usage. By default this function does not print passes, | |
430 | # only fails. | |
431 | # no-message: turns off printing of fails (and passes, but they're already off) | |
432 | # message: turns on printing of passes (and fails, but they're already on) | |
78a1a894 DJ |
433 | |
434 | proc gdb_breakpoint { function args } { | |
c906108c SS |
435 | global gdb_prompt |
436 | global decimal | |
437 | ||
78a1a894 | 438 | set pending_response n |
5b7d0050 | 439 | if {[lsearch -exact $args allow-pending] != -1} { |
78a1a894 DJ |
440 | set pending_response y |
441 | } | |
442 | ||
e48883f7 | 443 | set break_command "break" |
18ac113b | 444 | set break_message "Breakpoint" |
5b7d0050 | 445 | if {[lsearch -exact $args temporary] != -1} { |
e48883f7 | 446 | set break_command "tbreak" |
18ac113b | 447 | set break_message "Temporary breakpoint" |
e48883f7 DJ |
448 | } |
449 | ||
a20714ff PA |
450 | if {[lsearch -exact $args qualified] != -1} { |
451 | append break_command " -qualified" | |
452 | } | |
453 | ||
5b7d0050 DE |
454 | set print_pass 0 |
455 | set print_fail 1 | |
456 | set no_message_loc [lsearch -exact $args no-message] | |
457 | set message_loc [lsearch -exact $args message] | |
458 | # The last one to appear in args wins. | |
459 | if { $no_message_loc > $message_loc } { | |
460 | set print_fail 0 | |
461 | } elseif { $message_loc > $no_message_loc } { | |
462 | set print_pass 1 | |
55cd6f92 DJ |
463 | } |
464 | ||
5b7d0050 DE |
465 | set test_name "setting breakpoint at $function" |
466 | ||
e48883f7 | 467 | send_gdb "$break_command $function\n" |
c906108c SS |
468 | # The first two regexps are what we get with -g, the third is without -g. |
469 | gdb_expect 30 { | |
18ac113b AR |
470 | -re "$break_message \[0-9\]* at .*: file .*, line $decimal.\r\n$gdb_prompt $" {} |
471 | -re "$break_message \[0-9\]*: file .*, line $decimal.\r\n$gdb_prompt $" {} | |
472 | -re "$break_message \[0-9\]* at .*$gdb_prompt $" {} | |
473 | -re "$break_message \[0-9\]* \\(.*\\) pending.*$gdb_prompt $" { | |
78a1a894 | 474 | if {$pending_response == "n"} { |
5b7d0050 DE |
475 | if { $print_fail } { |
476 | fail $test_name | |
55cd6f92 | 477 | } |
78a1a894 DJ |
478 | return 0 |
479 | } | |
480 | } | |
9f27c604 | 481 | -re "Make breakpoint pending.*y or \\\[n\\\]. $" { |
78a1a894 | 482 | send_gdb "$pending_response\n" |
14b1a056 | 483 | exp_continue |
18fe2033 | 484 | } |
28781456 | 485 | -re "A problem internal to GDB has been detected" { |
5b7d0050 DE |
486 | if { $print_fail } { |
487 | fail "$test_name (GDB internal error)" | |
488 | } | |
28781456 JK |
489 | gdb_internal_error_resync |
490 | return 0 | |
491 | } | |
55cd6f92 | 492 | -re "$gdb_prompt $" { |
5b7d0050 DE |
493 | if { $print_fail } { |
494 | fail $test_name | |
495 | } | |
496 | return 0 | |
497 | } | |
498 | eof { | |
499 | if { $print_fail } { | |
500 | fail "$test_name (eof)" | |
55cd6f92 DJ |
501 | } |
502 | return 0 | |
503 | } | |
504 | timeout { | |
5b7d0050 DE |
505 | if { $print_fail } { |
506 | fail "$test_name (timeout)" | |
55cd6f92 DJ |
507 | } |
508 | return 0 | |
509 | } | |
c906108c | 510 | } |
5b7d0050 DE |
511 | if { $print_pass } { |
512 | pass $test_name | |
513 | } | |
ae59b1da | 514 | return 1 |
c906108c SS |
515 | } |
516 | ||
517 | # Set breakpoint at function and run gdb until it breaks there. | |
518 | # Since this is the only breakpoint that will be set, if it stops | |
519 | # at a breakpoint, we will assume it is the one we want. We can't | |
520 | # just compare to "function" because it might be a fully qualified, | |
5b7d0050 DE |
521 | # single quoted C++ function specifier. |
522 | # | |
523 | # If there are additional arguments, pass them to gdb_breakpoint. | |
524 | # We recognize no-message/message ourselves. | |
525 | # The default is no-message. | |
526 | # no-message is messed up here, like gdb_breakpoint: to preserve | |
527 | # historical usage fails are always printed by default. | |
528 | # no-message: turns off printing of fails (and passes, but they're already off) | |
529 | # message: turns on printing of passes (and fails, but they're already on) | |
c906108c | 530 | |
78a1a894 | 531 | proc runto { function args } { |
c906108c SS |
532 | global gdb_prompt |
533 | global decimal | |
534 | ||
535 | delete_breakpoints | |
536 | ||
5b7d0050 DE |
537 | # Default to "no-message". |
538 | set args "no-message $args" | |
539 | ||
540 | set print_pass 0 | |
541 | set print_fail 1 | |
542 | set no_message_loc [lsearch -exact $args no-message] | |
543 | set message_loc [lsearch -exact $args message] | |
544 | # The last one to appear in args wins. | |
545 | if { $no_message_loc > $message_loc } { | |
546 | set print_fail 0 | |
547 | } elseif { $message_loc > $no_message_loc } { | |
548 | set print_pass 1 | |
549 | } | |
550 | ||
551 | set test_name "running to $function in runto" | |
552 | ||
553 | # We need to use eval here to pass our varargs args to gdb_breakpoint | |
554 | # which is also a varargs function. | |
2c47921e DE |
555 | # But we also have to be careful because $function may have multiple |
556 | # elements, and we don't want Tcl to move the remaining elements after | |
557 | # the first to $args. That is why $function is wrapped in {}. | |
558 | if ![eval gdb_breakpoint {$function} $args] { | |
ae59b1da | 559 | return 0 |
c906108c SS |
560 | } |
561 | ||
562 | gdb_run_cmd | |
563 | ||
564 | # the "at foo.c:36" output we get with -g. | |
565 | # the "in func" output we get without -g. | |
566 | gdb_expect 30 { | |
567 | -re "Break.* at .*:$decimal.*$gdb_prompt $" { | |
5b7d0050 DE |
568 | if { $print_pass } { |
569 | pass $test_name | |
570 | } | |
c906108c SS |
571 | return 1 |
572 | } | |
573 | -re "Breakpoint \[0-9\]*, \[0-9xa-f\]* in .*$gdb_prompt $" { | |
5b7d0050 DE |
574 | if { $print_pass } { |
575 | pass $test_name | |
576 | } | |
c906108c SS |
577 | return 1 |
578 | } | |
8e46892c | 579 | -re "The target does not support running in non-stop mode.\r\n$gdb_prompt $" { |
5b7d0050 | 580 | if { $print_fail } { |
bc6c7af4 | 581 | unsupported "non-stop mode not supported" |
5b7d0050 | 582 | } |
8e46892c JK |
583 | return 0 |
584 | } | |
569b05a5 | 585 | -re ".*A problem internal to GDB has been detected" { |
5b7d0050 DE |
586 | if { $print_fail } { |
587 | fail "$test_name (GDB internal error)" | |
588 | } | |
569b05a5 JK |
589 | gdb_internal_error_resync |
590 | return 0 | |
591 | } | |
c906108c | 592 | -re "$gdb_prompt $" { |
5b7d0050 DE |
593 | if { $print_fail } { |
594 | fail $test_name | |
595 | } | |
c906108c SS |
596 | return 0 |
597 | } | |
72c63395 | 598 | eof { |
5b7d0050 DE |
599 | if { $print_fail } { |
600 | fail "$test_name (eof)" | |
601 | } | |
72c63395 JK |
602 | return 0 |
603 | } | |
c906108c | 604 | timeout { |
5b7d0050 DE |
605 | if { $print_fail } { |
606 | fail "$test_name (timeout)" | |
607 | } | |
c906108c SS |
608 | return 0 |
609 | } | |
610 | } | |
5b7d0050 DE |
611 | if { $print_pass } { |
612 | pass $test_name | |
613 | } | |
c906108c SS |
614 | return 1 |
615 | } | |
616 | ||
1d41d75c | 617 | # Ask gdb to run until we hit a breakpoint at main. |
c906108c | 618 | # |
1d41d75c DE |
619 | # N.B. This function deletes all existing breakpoints. |
620 | # If you don't want that, use gdb_start_cmd. | |
621 | ||
c906108c | 622 | proc runto_main { } { |
5b7d0050 | 623 | return [runto main no-message] |
c906108c SS |
624 | } |
625 | ||
4ce44c66 JM |
626 | ### Continue, and expect to hit a breakpoint. |
627 | ### Report a pass or fail, depending on whether it seems to have | |
628 | ### worked. Use NAME as part of the test name; each call to | |
629 | ### continue_to_breakpoint should use a NAME which is unique within | |
630 | ### that test file. | |
74960c60 | 631 | proc gdb_continue_to_breakpoint {name {location_pattern .*}} { |
4ce44c66 JM |
632 | global gdb_prompt |
633 | set full_name "continue to breakpoint: $name" | |
634 | ||
d6e76313 | 635 | set kfail_pattern "Process record does not support instruction 0xfae64 at.*" |
06d97543 | 636 | gdb_test_multiple "continue" $full_name { |
a1624241 | 637 | -re "(?:Breakpoint|Temporary breakpoint) .* (at|in) $location_pattern\r\n$gdb_prompt $" { |
4ce44c66 JM |
638 | pass $full_name |
639 | } | |
d6e76313 TV |
640 | -re "\[\r\n\]*(?:$kfail_pattern)\[\r\n\]+$gdb_prompt $" { |
641 | kfail "gdb/25038" $full_name | |
642 | } | |
4ce44c66 JM |
643 | } |
644 | } | |
645 | ||
646 | ||
039cf96d AC |
647 | # gdb_internal_error_resync: |
648 | # | |
649 | # Answer the questions GDB asks after it reports an internal error | |
650 | # until we get back to a GDB prompt. Decline to quit the debugging | |
651 | # session, and decline to create a core file. Return non-zero if the | |
652 | # resync succeeds. | |
653 | # | |
654 | # This procedure just answers whatever questions come up until it sees | |
655 | # a GDB prompt; it doesn't require you to have matched the input up to | |
656 | # any specific point. However, it only answers questions it sees in | |
657 | # the output itself, so if you've matched a question, you had better | |
658 | # answer it yourself before calling this. | |
659 | # | |
660 | # You can use this function thus: | |
661 | # | |
662 | # gdb_expect { | |
663 | # ... | |
664 | # -re ".*A problem internal to GDB has been detected" { | |
665 | # gdb_internal_error_resync | |
666 | # } | |
667 | # ... | |
668 | # } | |
669 | # | |
670 | proc gdb_internal_error_resync {} { | |
671 | global gdb_prompt | |
672 | ||
5b7d0050 DE |
673 | verbose -log "Resyncing due to internal error." |
674 | ||
039cf96d AC |
675 | set count 0 |
676 | while {$count < 10} { | |
677 | gdb_expect { | |
678 | -re "Quit this debugging session\\? \\(y or n\\) $" { | |
f9e2e39d | 679 | send_gdb "n\n" answer |
039cf96d AC |
680 | incr count |
681 | } | |
682 | -re "Create a core file of GDB\\? \\(y or n\\) $" { | |
f9e2e39d | 683 | send_gdb "n\n" answer |
039cf96d AC |
684 | incr count |
685 | } | |
686 | -re "$gdb_prompt $" { | |
687 | # We're resynchronized. | |
688 | return 1 | |
689 | } | |
690 | timeout { | |
691 | perror "Could not resync from internal error (timeout)" | |
692 | return 0 | |
693 | } | |
694 | } | |
695 | } | |
2b211c59 AC |
696 | perror "Could not resync from internal error (resync count exceeded)" |
697 | return 0 | |
039cf96d AC |
698 | } |
699 | ||
4ce44c66 | 700 | |
590003dc TV |
701 | # gdb_test_multiple COMMAND MESSAGE [ -promp PROMPT_REGEXP] [ -lbl ] |
702 | # EXPECT_ARGUMENTS | |
8dbfb380 | 703 | # Send a command to gdb; test the result. |
c906108c SS |
704 | # |
705 | # COMMAND is the command to execute, send to GDB with send_gdb. If | |
706 | # this is the null string no command is sent. | |
2307bd6a DJ |
707 | # MESSAGE is a message to be printed with the built-in failure patterns |
708 | # if one of them matches. If MESSAGE is empty COMMAND will be used. | |
590003dc TV |
709 | # -prompt PROMPT_REGEXP specifies a regexp matching the expected prompt |
710 | # after the command output. If empty, defaults to "$gdb_prompt $". | |
711 | # -lbl specifies that line-by-line matching will be used. | |
2307bd6a DJ |
712 | # EXPECT_ARGUMENTS will be fed to expect in addition to the standard |
713 | # patterns. Pattern elements will be evaluated in the caller's | |
714 | # context; action elements will be executed in the caller's context. | |
715 | # Unlike patterns for gdb_test, these patterns should generally include | |
716 | # the final newline and prompt. | |
c906108c SS |
717 | # |
718 | # Returns: | |
2307bd6a DJ |
719 | # 1 if the test failed, according to a built-in failure pattern |
720 | # 0 if only user-supplied patterns matched | |
c906108c SS |
721 | # -1 if there was an internal error. |
722 | # | |
d422fe19 AC |
723 | # You can use this function thus: |
724 | # | |
725 | # gdb_test_multiple "print foo" "test foo" { | |
726 | # -re "expected output 1" { | |
3d63690a | 727 | # pass "test foo" |
d422fe19 AC |
728 | # } |
729 | # -re "expected output 2" { | |
3d63690a AB |
730 | # fail "test foo" |
731 | # } | |
732 | # } | |
733 | # | |
734 | # Within action elements you can also make use of the variable | |
735 | # gdb_test_name. This variable is setup automatically by | |
736 | # gdb_test_multiple, and contains the value of MESSAGE. You can then | |
737 | # write this, which is equivalent to the above: | |
738 | # | |
739 | # gdb_test_multiple "print foo" "test foo" { | |
740 | # -re "expected output 1" { | |
741 | # pass $gdb_test_name | |
742 | # } | |
743 | # -re "expected output 2" { | |
744 | # fail $gdb_test_name | |
d422fe19 AC |
745 | # } |
746 | # } | |
747 | # | |
f71c18e7 PA |
748 | # Like with "expect", you can also specify the spawn id to match with |
749 | # -i "$id". Interesting spawn ids are $inferior_spawn_id and | |
750 | # $gdb_spawn_id. The former matches inferior I/O, while the latter | |
751 | # matches GDB I/O. E.g.: | |
752 | # | |
753 | # send_inferior "hello\n" | |
754 | # gdb_test_multiple "continue" "test echo" { | |
755 | # -i "$inferior_spawn_id" -re "^hello\r\nhello\r\n$" { | |
756 | # pass "got echo" | |
757 | # } | |
758 | # -i "$gdb_spawn_id" -re "Breakpoint.*$gdb_prompt $" { | |
759 | # fail "hit breakpoint" | |
760 | # } | |
761 | # } | |
762 | # | |
fda326dd | 763 | # The standard patterns, such as "Inferior exited..." and "A problem |
f71c18e7 PA |
764 | # ...", all being implicitly appended to that list. These are always |
765 | # expected from $gdb_spawn_id. IOW, callers do not need to worry | |
766 | # about resetting "-i" back to $gdb_spawn_id explicitly. | |
d422fe19 | 767 | # |
4ccdfbec TV |
768 | # In EXPECT_ARGUMENTS we can use a -wrap pattern flag, that wraps the regexp |
769 | # pattern as gdb_test wraps its message argument. | |
770 | # This allows us to rewrite: | |
771 | # gdb_test <command> <pattern> <message> | |
772 | # into: | |
773 | # gdb_test_multiple <command> <message> { | |
774 | # -re -wrap <pattern> { | |
775 | # pass $gdb_test_name | |
776 | # } | |
777 | # } | |
778 | # | |
60b6ede8 TV |
779 | # In EXPECT_ARGUMENTS, a pattern flag -early can be used. It makes sure the |
780 | # pattern is inserted before any implicit pattern added by gdb_test_multiple. | |
781 | # Using this pattern flag, we can f.i. setup a kfail for an assertion failure | |
782 | # <assert> during gdb_continue_to_breakpoint by the rewrite: | |
783 | # gdb_continue_to_breakpoint <msg> <pattern> | |
784 | # into: | |
785 | # set breakpoint_pattern "(?:Breakpoint|Temporary breakpoint) .* (at|in)" | |
786 | # gdb_test_multiple "continue" "continue to breakpoint: <msg>" { | |
787 | # -early -re "internal-error: <assert>" { | |
788 | # setup_kfail gdb/nnnnn "*-*-*" | |
789 | # exp_continue | |
790 | # } | |
791 | # -re "$breakpoint_pattern <pattern>\r\n$gdb_prompt $" { | |
792 | # pass $gdb_test_name | |
793 | # } | |
794 | # } | |
795 | # | |
590003dc | 796 | proc gdb_test_multiple { command message args } { |
e11ac3a3 | 797 | global verbose use_gdb_stub |
c3f814a1 | 798 | global gdb_prompt pagination_prompt |
c906108c | 799 | global GDB |
f71c18e7 | 800 | global gdb_spawn_id |
fda326dd | 801 | global inferior_exited_re |
c906108c | 802 | upvar timeout timeout |
c47cebdb | 803 | upvar expect_out expect_out |
749ef8f8 | 804 | global any_spawn_id |
c906108c | 805 | |
590003dc TV |
806 | set line_by_line 0 |
807 | set prompt_regexp "" | |
808 | for {set i 0} {$i < [llength $args]} {incr i} { | |
809 | set arg [lindex $args $i] | |
810 | if { $arg == "-prompt" } { | |
811 | incr i | |
812 | set prompt_regexp [lindex $args $i] | |
813 | } elseif { $arg == "-lbl" } { | |
814 | set line_by_line 1 | |
815 | } else { | |
816 | set user_code $arg | |
817 | break | |
818 | } | |
819 | } | |
820 | if { [expr $i + 1] < [llength $args] } { | |
821 | error "Too many arguments to gdb_test_multiple" | |
822 | } elseif { ![info exists user_code] } { | |
823 | error "Too few arguments to gdb_test_multiple" | |
824 | } | |
825 | ||
d17725d7 TV |
826 | if { "$prompt_regexp" == "" } { |
827 | set prompt_regexp "$gdb_prompt $" | |
828 | } | |
829 | ||
2307bd6a DJ |
830 | if { $message == "" } { |
831 | set message $command | |
c906108c | 832 | } |
c906108c | 833 | |
824cc8dd JK |
834 | if [string match "*\[\r\n\]" $command] { |
835 | error "Invalid trailing newline in \"$message\" test" | |
836 | } | |
837 | ||
8344e389 JK |
838 | if [string match "*\[\r\n\]*" $message] { |
839 | error "Invalid newline in \"$message\" test" | |
840 | } | |
841 | ||
e11ac3a3 | 842 | if {$use_gdb_stub |
9bfee719 | 843 | && [regexp -nocase {^\s*(r|run|star|start|at|att|atta|attac|attach)\M} \ |
e11ac3a3 JK |
844 | $command]} { |
845 | error "gdbserver does not support $command without extended-remote" | |
846 | } | |
847 | ||
2307bd6a DJ |
848 | # TCL/EXPECT WART ALERT |
849 | # Expect does something very strange when it receives a single braced | |
850 | # argument. It splits it along word separators and performs substitutions. | |
851 | # This means that { "[ab]" } is evaluated as "[ab]", but { "\[ab\]" } is | |
852 | # evaluated as "\[ab\]". But that's not how TCL normally works; inside a | |
853 | # double-quoted list item, "\[ab\]" is just a long way of representing | |
854 | # "[ab]", because the backslashes will be removed by lindex. | |
855 | ||
856 | # Unfortunately, there appears to be no easy way to duplicate the splitting | |
857 | # that expect will do from within TCL. And many places make use of the | |
858 | # "\[0-9\]" construct, so we need to support that; and some places make use | |
859 | # of the "[func]" construct, so we need to support that too. In order to | |
860 | # get this right we have to substitute quoted list elements differently | |
861 | # from braced list elements. | |
862 | ||
863 | # We do this roughly the same way that Expect does it. We have to use two | |
864 | # lists, because if we leave unquoted newlines in the argument to uplevel | |
865 | # they'll be treated as command separators, and if we escape newlines | |
866 | # we mangle newlines inside of command blocks. This assumes that the | |
867 | # input doesn't contain a pattern which contains actual embedded newlines | |
868 | # at this point! | |
869 | ||
870 | regsub -all {\n} ${user_code} { } subst_code | |
871 | set subst_code [uplevel list $subst_code] | |
872 | ||
873 | set processed_code "" | |
60b6ede8 TV |
874 | set early_processed_code "" |
875 | # The variable current_list holds the name of the currently processed | |
876 | # list, either processed_code or early_processed_code. | |
877 | set current_list "processed_code" | |
2307bd6a DJ |
878 | set patterns "" |
879 | set expecting_action 0 | |
21e24d21 | 880 | set expecting_arg 0 |
4ccdfbec | 881 | set wrap_pattern 0 |
2307bd6a DJ |
882 | foreach item $user_code subst_item $subst_code { |
883 | if { $item == "-n" || $item == "-notransfer" || $item == "-nocase" } { | |
60b6ede8 | 884 | lappend $current_list $item |
2307bd6a DJ |
885 | continue |
886 | } | |
21e24d21 | 887 | if { $item == "-indices" || $item == "-re" || $item == "-ex" } { |
60b6ede8 TV |
888 | lappend $current_list $item |
889 | continue | |
890 | } | |
891 | if { $item == "-early" } { | |
892 | set current_list "early_processed_code" | |
21e24d21 PA |
893 | continue |
894 | } | |
f71c18e7 | 895 | if { $item == "-timeout" || $item == "-i" } { |
21e24d21 | 896 | set expecting_arg 1 |
60b6ede8 | 897 | lappend $current_list $item |
21e24d21 PA |
898 | continue |
899 | } | |
4ccdfbec TV |
900 | if { $item == "-wrap" } { |
901 | set wrap_pattern 1 | |
902 | continue | |
903 | } | |
21e24d21 PA |
904 | if { $expecting_arg } { |
905 | set expecting_arg 0 | |
60b6ede8 | 906 | lappend $current_list $subst_item |
2307bd6a DJ |
907 | continue |
908 | } | |
909 | if { $expecting_action } { | |
60b6ede8 | 910 | lappend $current_list "uplevel [list $item]" |
2307bd6a DJ |
911 | set expecting_action 0 |
912 | # Cosmetic, no effect on the list. | |
60b6ede8 TV |
913 | append $current_list "\n" |
914 | # End the effect of -early, it only applies to one action. | |
915 | set current_list "processed_code" | |
2307bd6a DJ |
916 | continue |
917 | } | |
918 | set expecting_action 1 | |
4ccdfbec TV |
919 | if { $wrap_pattern } { |
920 | # Wrap subst_item as is done for the gdb_test PATTERN argument. | |
60b6ede8 | 921 | lappend $current_list \ |
4ccdfbec TV |
922 | "\[\r\n\]*(?:$subst_item)\[\r\n\]+$gdb_prompt $" |
923 | set wrap_pattern 0 | |
924 | } else { | |
60b6ede8 | 925 | lappend $current_list $subst_item |
4ccdfbec | 926 | } |
2307bd6a DJ |
927 | if {$patterns != ""} { |
928 | append patterns "; " | |
929 | } | |
930 | append patterns "\"$subst_item\"" | |
c906108c SS |
931 | } |
932 | ||
2307bd6a DJ |
933 | # Also purely cosmetic. |
934 | regsub -all {\r} $patterns {\\r} patterns | |
935 | regsub -all {\n} $patterns {\\n} patterns | |
936 | ||
c906108c SS |
937 | if $verbose>2 then { |
938 | send_user "Sending \"$command\" to gdb\n" | |
2307bd6a | 939 | send_user "Looking to match \"$patterns\"\n" |
c906108c SS |
940 | send_user "Message is \"$message\"\n" |
941 | } | |
942 | ||
943 | set result -1 | |
4ec70201 | 944 | set string "${command}\n" |
c906108c | 945 | if { $command != "" } { |
543a9323 | 946 | set multi_line_re "\[\r\n\] *>" |
c906108c | 947 | while { "$string" != "" } { |
4ec70201 PA |
948 | set foo [string first "\n" "$string"] |
949 | set len [string length "$string"] | |
c906108c | 950 | if { $foo < [expr $len - 1] } { |
4ec70201 | 951 | set str [string range "$string" 0 $foo] |
c906108c | 952 | if { [send_gdb "$str"] != "" } { |
4ec70201 | 953 | global suppress_flag |
c906108c SS |
954 | |
955 | if { ! $suppress_flag } { | |
4ec70201 | 956 | perror "Couldn't send $command to GDB." |
c906108c | 957 | } |
4ec70201 | 958 | fail "$message" |
ae59b1da | 959 | return $result |
c906108c | 960 | } |
a0b3c4fd JM |
961 | # since we're checking if each line of the multi-line |
962 | # command are 'accepted' by GDB here, | |
963 | # we need to set -notransfer expect option so that | |
964 | # command output is not lost for pattern matching | |
965 | # - guo | |
5f279fa6 | 966 | gdb_expect 2 { |
543a9323 | 967 | -notransfer -re "$multi_line_re$" { verbose "partial: match" 3 } |
5f279fa6 | 968 | timeout { verbose "partial: timeout" 3 } |
c906108c | 969 | } |
4ec70201 | 970 | set string [string range "$string" [expr $foo + 1] end] |
543a9323 | 971 | set multi_line_re "$multi_line_re.*\[\r\n\] *>" |
c906108c | 972 | } else { |
4ec70201 | 973 | break |
c906108c SS |
974 | } |
975 | } | |
976 | if { "$string" != "" } { | |
977 | if { [send_gdb "$string"] != "" } { | |
4ec70201 | 978 | global suppress_flag |
c906108c SS |
979 | |
980 | if { ! $suppress_flag } { | |
4ec70201 | 981 | perror "Couldn't send $command to GDB." |
c906108c | 982 | } |
4ec70201 | 983 | fail "$message" |
ae59b1da | 984 | return $result |
c906108c SS |
985 | } |
986 | } | |
987 | } | |
988 | ||
60b6ede8 TV |
989 | set code $early_processed_code |
990 | append code { | |
9bfee719 MR |
991 | -re ".*A problem internal to GDB has been detected" { |
992 | fail "$message (GDB internal error)" | |
993 | gdb_internal_error_resync | |
28054d69 | 994 | set result -1 |
9bfee719 MR |
995 | } |
996 | -re "\\*\\*\\* DOSEXIT code.*" { | |
997 | if { $message != "" } { | |
4ec70201 | 998 | fail "$message" |
9bfee719 | 999 | } |
4ec70201 PA |
1000 | gdb_suppress_entire_file "GDB died" |
1001 | set result -1 | |
9bfee719 | 1002 | } |
b0f4b84b DJ |
1003 | } |
1004 | append code $processed_code | |
9a93502f PA |
1005 | |
1006 | # Reset the spawn id, in case the processed code used -i. | |
b0f4b84b | 1007 | append code { |
f71c18e7 | 1008 | -i "$gdb_spawn_id" |
9a93502f | 1009 | } |
f71c18e7 | 1010 | |
9a93502f | 1011 | append code { |
d17725d7 | 1012 | -re "Ending remote debugging.*$prompt_regexp" { |
c906108c SS |
1013 | if ![isnative] then { |
1014 | warning "Can`t communicate to remote target." | |
1015 | } | |
1016 | gdb_exit | |
1017 | gdb_start | |
1018 | set result -1 | |
1019 | } | |
d17725d7 | 1020 | -re "Undefined\[a-z\]* command:.*$prompt_regexp" { |
c906108c | 1021 | perror "Undefined command \"$command\"." |
9bfee719 | 1022 | fail "$message" |
c906108c SS |
1023 | set result 1 |
1024 | } | |
d17725d7 | 1025 | -re "Ambiguous command.*$prompt_regexp" { |
c906108c | 1026 | perror "\"$command\" is not a unique command name." |
9bfee719 | 1027 | fail "$message" |
c906108c SS |
1028 | set result 1 |
1029 | } | |
d17725d7 | 1030 | -re "$inferior_exited_re with code \[0-9\]+.*$prompt_regexp" { |
c906108c | 1031 | if ![string match "" $message] then { |
ed4c619a | 1032 | set errmsg "$message (the program exited)" |
c906108c | 1033 | } else { |
ed4c619a | 1034 | set errmsg "$command (the program exited)" |
c906108c SS |
1035 | } |
1036 | fail "$errmsg" | |
2307bd6a | 1037 | set result -1 |
cb9a9d3e | 1038 | } |
d17725d7 | 1039 | -re "$inferior_exited_re normally.*$prompt_regexp" { |
cb9a9d3e | 1040 | if ![string match "" $message] then { |
ed4c619a | 1041 | set errmsg "$message (the program exited)" |
cb9a9d3e | 1042 | } else { |
ed4c619a | 1043 | set errmsg "$command (the program exited)" |
cb9a9d3e MS |
1044 | } |
1045 | fail "$errmsg" | |
2307bd6a | 1046 | set result -1 |
c906108c | 1047 | } |
d17725d7 | 1048 | -re "The program is not being run.*$prompt_regexp" { |
c906108c | 1049 | if ![string match "" $message] then { |
ed4c619a | 1050 | set errmsg "$message (the program is no longer running)" |
c906108c | 1051 | } else { |
ed4c619a | 1052 | set errmsg "$command (the program is no longer running)" |
c906108c SS |
1053 | } |
1054 | fail "$errmsg" | |
2307bd6a | 1055 | set result -1 |
c906108c | 1056 | } |
d17725d7 | 1057 | -re "\r\n$prompt_regexp" { |
c906108c SS |
1058 | if ![string match "" $message] then { |
1059 | fail "$message" | |
1060 | } | |
1061 | set result 1 | |
1062 | } | |
c3f814a1 | 1063 | -re "$pagination_prompt" { |
c906108c SS |
1064 | send_gdb "\n" |
1065 | perror "Window too small." | |
9bfee719 | 1066 | fail "$message" |
2307bd6a | 1067 | set result -1 |
c906108c | 1068 | } |
b598bfda | 1069 | -re "\\((y or n|y or \\\[n\\\]|\\\[y\\\] or n)\\) " { |
f9e2e39d | 1070 | send_gdb "n\n" answer |
d17725d7 | 1071 | gdb_expect -re "$prompt_regexp" |
b598bfda DJ |
1072 | fail "$message (got interactive prompt)" |
1073 | set result -1 | |
1074 | } | |
1075 | -re "\\\[0\\\] cancel\r\n\\\[1\\\] all.*\r\n> $" { | |
1076 | send_gdb "0\n" | |
d17725d7 | 1077 | gdb_expect -re "$prompt_regexp" |
b598bfda | 1078 | fail "$message (got breakpoint menu)" |
2307bd6a | 1079 | set result -1 |
c906108c | 1080 | } |
749ef8f8 | 1081 | |
fe1a5cad TV |
1082 | -i $gdb_spawn_id |
1083 | eof { | |
1084 | perror "GDB process no longer exists" | |
1085 | set wait_status [wait -i $gdb_spawn_id] | |
1086 | verbose -log "GDB process exited with wait status $wait_status" | |
1087 | if { $message != "" } { | |
1088 | fail "$message" | |
1089 | } | |
1090 | return -1 | |
1091 | } | |
9a93502f | 1092 | } |
fe1a5cad | 1093 | |
590003dc TV |
1094 | if {$line_by_line} { |
1095 | append code { | |
1096 | -re "\r\n\[^\r\n\]*(?=\r\n)" { | |
1097 | exp_continue | |
1098 | } | |
1099 | } | |
1100 | } | |
1101 | ||
9a93502f PA |
1102 | # Now patterns that apply to any spawn id specified. |
1103 | append code { | |
749ef8f8 | 1104 | -i $any_spawn_id |
9bfee719 MR |
1105 | eof { |
1106 | perror "Process no longer exists" | |
1107 | if { $message != "" } { | |
1108 | fail "$message" | |
1109 | } | |
1110 | return -1 | |
c906108c | 1111 | } |
9bfee719 | 1112 | full_buffer { |
c906108c | 1113 | perror "internal buffer is full." |
9bfee719 | 1114 | fail "$message" |
2307bd6a | 1115 | set result -1 |
c906108c SS |
1116 | } |
1117 | timeout { | |
1118 | if ![string match "" $message] then { | |
1119 | fail "$message (timeout)" | |
1120 | } | |
1121 | set result 1 | |
1122 | } | |
1123 | } | |
2307bd6a | 1124 | |
9a93502f PA |
1125 | # remote_expect calls the eof section if there is an error on the |
1126 | # expect call. We already have eof sections above, and we don't | |
1127 | # want them to get called in that situation. Since the last eof | |
1128 | # section becomes the error section, here we define another eof | |
1129 | # section, but with an empty spawn_id list, so that it won't ever | |
1130 | # match. | |
1131 | append code { | |
1132 | -i "" eof { | |
1133 | # This comment is here because the eof section must not be | |
1134 | # the empty string, otherwise remote_expect won't realize | |
1135 | # it exists. | |
1136 | } | |
1137 | } | |
1138 | ||
3d63690a AB |
1139 | # Create gdb_test_name in the parent scope. If this variable |
1140 | # already exists, which it might if we have nested calls to | |
1141 | # gdb_test_multiple, then preserve the old value, otherwise, | |
1142 | # create a new variable in the parent scope. | |
1143 | upvar gdb_test_name gdb_test_name | |
1144 | if { [info exists gdb_test_name] } { | |
1145 | set gdb_test_name_old "$gdb_test_name" | |
1146 | } | |
1147 | set gdb_test_name "$message" | |
1148 | ||
2307bd6a | 1149 | set result 0 |
4a40f85a | 1150 | set code [catch {gdb_expect $code} string] |
3d63690a AB |
1151 | |
1152 | # Clean up the gdb_test_name variable. If we had a | |
1153 | # previous value then restore it, otherwise, delete the variable | |
1154 | # from the parent scope. | |
1155 | if { [info exists gdb_test_name_old] } { | |
1156 | set gdb_test_name "$gdb_test_name_old" | |
1157 | } else { | |
1158 | unset gdb_test_name | |
1159 | } | |
1160 | ||
04f6ecf2 | 1161 | if {$code == 1} { |
4ec70201 | 1162 | global errorInfo errorCode |
04f6ecf2 | 1163 | return -code error -errorinfo $errorInfo -errorcode $errorCode $string |
d6d7a51a | 1164 | } elseif {$code > 1} { |
04f6ecf2 DJ |
1165 | return -code $code $string |
1166 | } | |
c906108c SS |
1167 | return $result |
1168 | } | |
2307bd6a DJ |
1169 | |
1170 | # gdb_test COMMAND PATTERN MESSAGE QUESTION RESPONSE | |
1171 | # Send a command to gdb; test the result. | |
1172 | # | |
1173 | # COMMAND is the command to execute, send to GDB with send_gdb. If | |
1174 | # this is the null string no command is sent. | |
1175 | # PATTERN is the pattern to match for a PASS, and must NOT include | |
79fad5b8 SL |
1176 | # the \r\n sequence immediately before the gdb prompt. This argument |
1177 | # may be omitted to just match the prompt, ignoring whatever output | |
1178 | # precedes it. | |
2307bd6a DJ |
1179 | # MESSAGE is an optional message to be printed. If this is |
1180 | # omitted, then the pass/fail messages use the command string as the | |
1181 | # message. (If this is the empty string, then sometimes we don't | |
1182 | # call pass or fail at all; I don't understand this at all.) | |
1183 | # QUESTION is a question GDB may ask in response to COMMAND, like | |
1184 | # "are you sure?" | |
1185 | # RESPONSE is the response to send if QUESTION appears. | |
1186 | # | |
1187 | # Returns: | |
1188 | # 1 if the test failed, | |
1189 | # 0 if the test passes, | |
1190 | # -1 if there was an internal error. | |
1191 | # | |
1192 | proc gdb_test { args } { | |
2307bd6a | 1193 | global gdb_prompt |
2307bd6a DJ |
1194 | upvar timeout timeout |
1195 | ||
1196 | if [llength $args]>2 then { | |
1197 | set message [lindex $args 2] | |
1198 | } else { | |
1199 | set message [lindex $args 0] | |
1200 | } | |
1201 | set command [lindex $args 0] | |
1202 | set pattern [lindex $args 1] | |
1203 | ||
e452e88f TV |
1204 | set user_code {} |
1205 | lappend user_code { | |
75312ae3 | 1206 | -re "\[\r\n\]*(?:$pattern)\[\r\n\]+$gdb_prompt $" { |
2307bd6a DJ |
1207 | if ![string match "" $message] then { |
1208 | pass "$message" | |
1209 | } | |
1210 | } | |
e452e88f TV |
1211 | } |
1212 | ||
1213 | if { [llength $args] == 5 } { | |
1214 | set question_string [lindex $args 3] | |
1215 | set response_string [lindex $args 4] | |
1216 | lappend user_code { | |
1217 | -re "(${question_string})$" { | |
1218 | send_gdb "$response_string\n" | |
1219 | exp_continue | |
1220 | } | |
2307bd6a | 1221 | } |
e452e88f TV |
1222 | } |
1223 | ||
1224 | set user_code [join $user_code] | |
1225 | return [gdb_test_multiple $command $message $user_code] | |
2307bd6a | 1226 | } |
a7b75dfd | 1227 | |
a80cf5d8 TV |
1228 | # Return 1 if version MAJOR.MINOR is at least AT_LEAST_MAJOR.AT_LEAST_MINOR. |
1229 | proc version_at_least { major minor at_least_major at_least_minor} { | |
1230 | if { $major > $at_least_major } { | |
2a3ad588 | 1231 | return 1 |
a80cf5d8 TV |
1232 | } elseif { $major == $at_least_major \ |
1233 | && $minor >= $at_least_minor } { | |
2a3ad588 TV |
1234 | return 1 |
1235 | } else { | |
1236 | return 0 | |
1237 | } | |
1238 | } | |
1239 | ||
a80cf5d8 TV |
1240 | # Return 1 if tcl version used is at least MAJOR.MINOR |
1241 | proc tcl_version_at_least { major minor } { | |
1242 | global tcl_version | |
1243 | regexp {^([0-9]+)\.([0-9]+)$} $tcl_version \ | |
1244 | dummy tcl_version_major tcl_version_minor | |
1245 | return [version_at_least $tcl_version_major $tcl_version_minor \ | |
1246 | $major $minor] | |
1247 | } | |
1248 | ||
2a3ad588 TV |
1249 | if { [tcl_version_at_least 8 5] == 0 } { |
1250 | # lrepeat was added in tcl 8.5. Only add if missing. | |
1251 | proc lrepeat { n element } { | |
1252 | if { [string is integer -strict $n] == 0 } { | |
1253 | error "expected integer but got \"$n\"" | |
1254 | } | |
1255 | if { $n < 0 } { | |
1256 | error "bad count \"$n\": must be integer >= 0" | |
1257 | } | |
1258 | set res [list] | |
1259 | for {set i 0} {$i < $n} {incr i} { | |
1260 | lappend res $element | |
1261 | } | |
1262 | return $res | |
1263 | } | |
1264 | } | |
1265 | ||
a7b75dfd JB |
1266 | # gdb_test_no_output COMMAND MESSAGE |
1267 | # Send a command to GDB and verify that this command generated no output. | |
1268 | # | |
1269 | # See gdb_test_multiple for a description of the COMMAND and MESSAGE | |
1270 | # parameters. If MESSAGE is ommitted, then COMMAND will be used as | |
c22decce JB |
1271 | # the message. (If MESSAGE is the empty string, then sometimes we do not |
1272 | # call pass or fail at all; I don't understand this at all.) | |
a7b75dfd JB |
1273 | |
1274 | proc gdb_test_no_output { args } { | |
1275 | global gdb_prompt | |
1276 | set command [lindex $args 0] | |
1277 | if [llength $args]>1 then { | |
1278 | set message [lindex $args 1] | |
1279 | } else { | |
1280 | set message $command | |
1281 | } | |
1282 | ||
1283 | set command_regex [string_to_regexp $command] | |
1284 | gdb_test_multiple $command $message { | |
1285 | -re "^$command_regex\r\n$gdb_prompt $" { | |
c22decce JB |
1286 | if ![string match "" $message] then { |
1287 | pass "$message" | |
1288 | } | |
a7b75dfd JB |
1289 | } |
1290 | } | |
1291 | } | |
1292 | ||
6b0ecdc2 DE |
1293 | # Send a command and then wait for a sequence of outputs. |
1294 | # This is useful when the sequence is long and contains ".*", a single | |
1295 | # regexp to match the entire output can get a timeout much easier. | |
1296 | # | |
968a13f8 PA |
1297 | # COMMAND is the command to execute, send to GDB with send_gdb. If |
1298 | # this is the null string no command is sent. | |
6b0ecdc2 DE |
1299 | # TEST_NAME is passed to pass/fail. COMMAND is used if TEST_NAME is "". |
1300 | # EXPECTED_OUTPUT_LIST is a list of regexps of expected output, which are | |
1301 | # processed in order, and all must be present in the output. | |
1302 | # | |
1303 | # It is unnecessary to specify ".*" at the beginning or end of any regexp, | |
1304 | # there is an implicit ".*" between each element of EXPECTED_OUTPUT_LIST. | |
1305 | # There is also an implicit ".*" between the last regexp and the gdb prompt. | |
1306 | # | |
1307 | # Like gdb_test and gdb_test_multiple, the output is expected to end with the | |
1308 | # gdb prompt, which must not be specified in EXPECTED_OUTPUT_LIST. | |
5fa290c1 DE |
1309 | # |
1310 | # Returns: | |
1311 | # 1 if the test failed, | |
1312 | # 0 if the test passes, | |
1313 | # -1 if there was an internal error. | |
6b0ecdc2 DE |
1314 | |
1315 | proc gdb_test_sequence { command test_name expected_output_list } { | |
1316 | global gdb_prompt | |
1317 | if { $test_name == "" } { | |
1318 | set test_name $command | |
1319 | } | |
1320 | lappend expected_output_list ""; # implicit ".*" before gdb prompt | |
968a13f8 PA |
1321 | if { $command != "" } { |
1322 | send_gdb "$command\n" | |
1323 | } | |
5fa290c1 | 1324 | return [gdb_expect_list $test_name "$gdb_prompt $" $expected_output_list] |
6b0ecdc2 DE |
1325 | } |
1326 | ||
c906108c SS |
1327 | \f |
1328 | # Test that a command gives an error. For pass or fail, return | |
1329 | # a 1 to indicate that more tests can proceed. However a timeout | |
1330 | # is a serious error, generates a special fail message, and causes | |
1331 | # a 0 to be returned to indicate that more tests are likely to fail | |
1332 | # as well. | |
1333 | ||
1334 | proc test_print_reject { args } { | |
1335 | global gdb_prompt | |
1336 | global verbose | |
1337 | ||
1338 | if [llength $args]==2 then { | |
1339 | set expectthis [lindex $args 1] | |
1340 | } else { | |
1341 | set expectthis "should never match this bogus string" | |
1342 | } | |
1343 | set sendthis [lindex $args 0] | |
1344 | if $verbose>2 then { | |
1345 | send_user "Sending \"$sendthis\" to gdb\n" | |
1346 | send_user "Looking to match \"$expectthis\"\n" | |
1347 | } | |
1348 | send_gdb "$sendthis\n" | |
1349 | #FIXME: Should add timeout as parameter. | |
1350 | gdb_expect { | |
1351 | -re "A .* in expression.*\\.*$gdb_prompt $" { | |
1352 | pass "reject $sendthis" | |
1353 | return 1 | |
1354 | } | |
1355 | -re "Invalid syntax in expression.*$gdb_prompt $" { | |
1356 | pass "reject $sendthis" | |
1357 | return 1 | |
1358 | } | |
1359 | -re "Junk after end of expression.*$gdb_prompt $" { | |
1360 | pass "reject $sendthis" | |
1361 | return 1 | |
1362 | } | |
1363 | -re "Invalid number.*$gdb_prompt $" { | |
1364 | pass "reject $sendthis" | |
1365 | return 1 | |
1366 | } | |
1367 | -re "Invalid character constant.*$gdb_prompt $" { | |
1368 | pass "reject $sendthis" | |
1369 | return 1 | |
1370 | } | |
1371 | -re "No symbol table is loaded.*$gdb_prompt $" { | |
1372 | pass "reject $sendthis" | |
1373 | return 1 | |
1374 | } | |
1375 | -re "No symbol .* in current context.*$gdb_prompt $" { | |
1376 | pass "reject $sendthis" | |
1377 | return 1 | |
1378 | } | |
c4b7bc2b JB |
1379 | -re "Unmatched single quote.*$gdb_prompt $" { |
1380 | pass "reject $sendthis" | |
1381 | return 1 | |
1382 | } | |
1383 | -re "A character constant must contain at least one character.*$gdb_prompt $" { | |
1384 | pass "reject $sendthis" | |
1385 | return 1 | |
1386 | } | |
c906108c SS |
1387 | -re "$expectthis.*$gdb_prompt $" { |
1388 | pass "reject $sendthis" | |
1389 | return 1 | |
1390 | } | |
1391 | -re ".*$gdb_prompt $" { | |
1392 | fail "reject $sendthis" | |
1393 | return 1 | |
1394 | } | |
1395 | default { | |
1396 | fail "reject $sendthis (eof or timeout)" | |
1397 | return 0 | |
1398 | } | |
1399 | } | |
1400 | } | |
1401 | \f | |
c906108c SS |
1402 | |
1403 | # Same as gdb_test, but the second parameter is not a regexp, | |
1404 | # but a string that must match exactly. | |
1405 | ||
1406 | proc gdb_test_exact { args } { | |
1407 | upvar timeout timeout | |
1408 | ||
1409 | set command [lindex $args 0] | |
1410 | ||
1411 | # This applies a special meaning to a null string pattern. Without | |
1412 | # this, "$pattern\r\n$gdb_prompt $" will match anything, including error | |
1413 | # messages from commands that should have no output except a new | |
1414 | # prompt. With this, only results of a null string will match a null | |
1415 | # string pattern. | |
1416 | ||
1417 | set pattern [lindex $args 1] | |
1418 | if [string match $pattern ""] { | |
1419 | set pattern [string_to_regexp [lindex $args 0]] | |
1420 | } else { | |
1421 | set pattern [string_to_regexp [lindex $args 1]] | |
1422 | } | |
1423 | ||
1424 | # It is most natural to write the pattern argument with only | |
1425 | # embedded \n's, especially if you are trying to avoid Tcl quoting | |
1426 | # problems. But gdb_expect really wants to see \r\n in patterns. So | |
1427 | # transform the pattern here. First transform \r\n back to \n, in | |
1428 | # case some users of gdb_test_exact already do the right thing. | |
1429 | regsub -all "\r\n" $pattern "\n" pattern | |
1430 | regsub -all "\n" $pattern "\r\n" pattern | |
1431 | if [llength $args]==3 then { | |
1432 | set message [lindex $args 2] | |
d1e36019 | 1433 | return [gdb_test $command $pattern $message] |
c906108c SS |
1434 | } |
1435 | ||
d1e36019 | 1436 | return [gdb_test $command $pattern] |
c906108c | 1437 | } |
2dfb8c17 DE |
1438 | |
1439 | # Wrapper around gdb_test_multiple that looks for a list of expected | |
1440 | # output elements, but which can appear in any order. | |
1441 | # CMD is the gdb command. | |
1442 | # NAME is the name of the test. | |
1443 | # ELM_FIND_REGEXP specifies how to partition the output into elements to | |
1444 | # compare. | |
1445 | # ELM_EXTRACT_REGEXP specifies the part of ELM_FIND_REGEXP to compare. | |
1446 | # RESULT_MATCH_LIST is a list of exact matches for each expected element. | |
1447 | # All elements of RESULT_MATCH_LIST must appear for the test to pass. | |
1448 | # | |
1449 | # A typical use of ELM_FIND_REGEXP/ELM_EXTRACT_REGEXP is to extract one line | |
1450 | # of text per element and then strip trailing \r\n's. | |
1451 | # Example: | |
1452 | # gdb_test_list_exact "foo" "bar" \ | |
eec52c44 PM |
1453 | # "\[^\r\n\]+\[\r\n\]+" \ |
1454 | # "\[^\r\n\]+" \ | |
2dfb8c17 DE |
1455 | # { \ |
1456 | # {expected result 1} \ | |
1457 | # {expected result 2} \ | |
1458 | # } | |
1459 | ||
1460 | proc gdb_test_list_exact { cmd name elm_find_regexp elm_extract_regexp result_match_list } { | |
1461 | global gdb_prompt | |
1462 | ||
1463 | set matches [lsort $result_match_list] | |
1464 | set seen {} | |
1465 | gdb_test_multiple $cmd $name { | |
1466 | "$cmd\[\r\n\]" { exp_continue } | |
1467 | -re $elm_find_regexp { | |
1468 | set str $expect_out(0,string) | |
1469 | verbose -log "seen: $str" 3 | |
1470 | regexp -- $elm_extract_regexp $str elm_seen | |
1471 | verbose -log "extracted: $elm_seen" 3 | |
1472 | lappend seen $elm_seen | |
1473 | exp_continue | |
1474 | } | |
1475 | -re "$gdb_prompt $" { | |
1476 | set failed "" | |
1477 | foreach got [lsort $seen] have $matches { | |
1478 | if {![string equal $got $have]} { | |
1479 | set failed $have | |
1480 | break | |
1481 | } | |
1482 | } | |
1483 | if {[string length $failed] != 0} { | |
1484 | fail "$name ($failed not found)" | |
1485 | } else { | |
1486 | pass $name | |
1487 | } | |
1488 | } | |
1489 | } | |
1490 | } | |
188a61b4 PA |
1491 | |
1492 | # gdb_test_stdio COMMAND INFERIOR_PATTERN GDB_PATTERN MESSAGE | |
1493 | # Send a command to gdb; expect inferior and gdb output. | |
1494 | # | |
1495 | # See gdb_test_multiple for a description of the COMMAND and MESSAGE | |
1496 | # parameters. | |
1497 | # | |
1498 | # INFERIOR_PATTERN is the pattern to match against inferior output. | |
1499 | # | |
1500 | # GDB_PATTERN is the pattern to match against gdb output, and must NOT | |
1501 | # include the \r\n sequence immediately before the gdb prompt, nor the | |
1502 | # prompt. The default is empty. | |
1503 | # | |
1504 | # Both inferior and gdb patterns must match for a PASS. | |
1505 | # | |
1506 | # If MESSAGE is ommitted, then COMMAND will be used as the message. | |
1507 | # | |
1508 | # Returns: | |
1509 | # 1 if the test failed, | |
1510 | # 0 if the test passes, | |
1511 | # -1 if there was an internal error. | |
1512 | # | |
1513 | ||
1514 | proc gdb_test_stdio {command inferior_pattern {gdb_pattern ""} {message ""}} { | |
1515 | global inferior_spawn_id gdb_spawn_id | |
1516 | global gdb_prompt | |
1517 | ||
1518 | if {$message == ""} { | |
1519 | set message $command | |
1520 | } | |
1521 | ||
1522 | set inferior_matched 0 | |
1523 | set gdb_matched 0 | |
1524 | ||
1525 | # Use an indirect spawn id list, and remove the inferior spawn id | |
1526 | # from the expected output as soon as it matches, in case | |
1527 | # $inferior_pattern happens to be a prefix of the resulting full | |
1528 | # gdb pattern below (e.g., "\r\n"). | |
1529 | global gdb_test_stdio_spawn_id_list | |
1530 | set gdb_test_stdio_spawn_id_list "$inferior_spawn_id" | |
1531 | ||
1532 | # Note that if $inferior_spawn_id and $gdb_spawn_id are different, | |
1533 | # then we may see gdb's output arriving before the inferior's | |
1534 | # output. | |
1535 | set res [gdb_test_multiple $command $message { | |
1536 | -i gdb_test_stdio_spawn_id_list -re "$inferior_pattern" { | |
1537 | set inferior_matched 1 | |
1538 | if {!$gdb_matched} { | |
1539 | set gdb_test_stdio_spawn_id_list "" | |
1540 | exp_continue | |
1541 | } | |
1542 | } | |
1543 | -i $gdb_spawn_id -re "$gdb_pattern\r\n$gdb_prompt $" { | |
1544 | set gdb_matched 1 | |
1545 | if {!$inferior_matched} { | |
1546 | exp_continue | |
1547 | } | |
1548 | } | |
1549 | }] | |
1550 | if {$res == 0} { | |
1551 | pass $message | |
1552 | } else { | |
1553 | verbose -log "inferior_matched=$inferior_matched, gdb_matched=$gdb_matched" | |
1554 | } | |
1555 | return $res | |
1556 | } | |
1557 | ||
2e62ab40 AB |
1558 | # get_print_expr_at_depths EXP OUTPUTS |
1559 | # | |
1560 | # Used for testing 'set print max-depth'. Prints the expression EXP | |
1561 | # with 'set print max-depth' set to various depths. OUTPUTS is a list | |
1562 | # of `n` different patterns to match at each of the depths from 0 to | |
1563 | # (`n` - 1). | |
1564 | # | |
1565 | # This proc does one final check with the max-depth set to 'unlimited' | |
1566 | # which is tested against the last pattern in the OUTPUTS list. The | |
1567 | # OUTPUTS list is therefore required to match every depth from 0 to a | |
1568 | # depth where the whole of EXP is printed with no ellipsis. | |
1569 | # | |
1570 | # This proc leaves the 'set print max-depth' set to 'unlimited'. | |
1571 | proc gdb_print_expr_at_depths {exp outputs} { | |
1572 | for { set depth 0 } { $depth <= [llength $outputs] } { incr depth } { | |
1573 | if { $depth == [llength $outputs] } { | |
1574 | set expected_result [lindex $outputs [expr [llength $outputs] - 1]] | |
1575 | set depth_string "unlimited" | |
1576 | } else { | |
1577 | set expected_result [lindex $outputs $depth] | |
1578 | set depth_string $depth | |
1579 | } | |
1580 | ||
1581 | with_test_prefix "exp='$exp': depth=${depth_string}" { | |
1582 | gdb_test_no_output "set print max-depth ${depth_string}" | |
1583 | gdb_test "p $exp" "$expected_result" | |
1584 | } | |
1585 | } | |
1586 | } | |
1587 | ||
c906108c | 1588 | \f |
bd293940 PA |
1589 | |
1590 | # Issue a PASS and return true if evaluating CONDITION in the caller's | |
1591 | # frame returns true, and issue a FAIL and return false otherwise. | |
1592 | # MESSAGE is the pass/fail message to be printed. If MESSAGE is | |
1593 | # omitted or is empty, then the pass/fail messages use the condition | |
1594 | # string as the message. | |
1595 | ||
1596 | proc gdb_assert { condition {message ""} } { | |
1597 | if { $message == ""} { | |
1598 | set message $condition | |
1599 | } | |
1600 | ||
1601 | set res [uplevel 1 expr $condition] | |
1602 | if {!$res} { | |
1603 | fail $message | |
1604 | } else { | |
1605 | pass $message | |
1606 | } | |
1607 | return $res | |
1608 | } | |
1609 | ||
c906108c SS |
1610 | proc gdb_reinitialize_dir { subdir } { |
1611 | global gdb_prompt | |
1612 | ||
1613 | if [is_remote host] { | |
ae59b1da | 1614 | return "" |
c906108c SS |
1615 | } |
1616 | send_gdb "dir\n" | |
1617 | gdb_expect 60 { | |
1618 | -re "Reinitialize source path to empty.*y or n. " { | |
f9e2e39d | 1619 | send_gdb "y\n" answer |
c906108c SS |
1620 | gdb_expect 60 { |
1621 | -re "Source directories searched.*$gdb_prompt $" { | |
1622 | send_gdb "dir $subdir\n" | |
1623 | gdb_expect 60 { | |
1624 | -re "Source directories searched.*$gdb_prompt $" { | |
1625 | verbose "Dir set to $subdir" | |
1626 | } | |
1627 | -re "$gdb_prompt $" { | |
1628 | perror "Dir \"$subdir\" failed." | |
1629 | } | |
1630 | } | |
1631 | } | |
1632 | -re "$gdb_prompt $" { | |
1633 | perror "Dir \"$subdir\" failed." | |
1634 | } | |
1635 | } | |
1636 | } | |
1637 | -re "$gdb_prompt $" { | |
1638 | perror "Dir \"$subdir\" failed." | |
1639 | } | |
1640 | } | |
1641 | } | |
1642 | ||
1643 | # | |
1644 | # gdb_exit -- exit the GDB, killing the target program if necessary | |
1645 | # | |
1646 | proc default_gdb_exit {} { | |
1647 | global GDB | |
6b8ce727 | 1648 | global INTERNAL_GDBFLAGS GDBFLAGS |
51f77c37 | 1649 | global gdb_spawn_id inferior_spawn_id |
5e92f71a | 1650 | global inotify_log_file |
c906108c | 1651 | |
4ec70201 | 1652 | gdb_stop_suppressing_tests |
c906108c SS |
1653 | |
1654 | if ![info exists gdb_spawn_id] { | |
4ec70201 | 1655 | return |
c906108c SS |
1656 | } |
1657 | ||
6b8ce727 | 1658 | verbose "Quitting $GDB $INTERNAL_GDBFLAGS $GDBFLAGS" |
c906108c | 1659 | |
5e92f71a TT |
1660 | if {[info exists inotify_log_file] && [file exists $inotify_log_file]} { |
1661 | set fd [open $inotify_log_file] | |
1662 | set data [read -nonewline $fd] | |
1663 | close $fd | |
1664 | ||
1665 | if {[string compare $data ""] != 0} { | |
1666 | warning "parallel-unsafe file creations noticed" | |
1667 | ||
1668 | # Clear the log. | |
1669 | set fd [open $inotify_log_file w] | |
1670 | close $fd | |
1671 | } | |
1672 | } | |
1673 | ||
c906108c | 1674 | if { [is_remote host] && [board_info host exists fileid] } { |
4ec70201 | 1675 | send_gdb "quit\n" |
c906108c SS |
1676 | gdb_expect 10 { |
1677 | -re "y or n" { | |
f9e2e39d | 1678 | send_gdb "y\n" answer |
4ec70201 | 1679 | exp_continue |
c906108c SS |
1680 | } |
1681 | -re "DOSEXIT code" { } | |
1682 | default { } | |
1683 | } | |
1684 | } | |
1685 | ||
1686 | if ![is_remote host] { | |
4ec70201 | 1687 | remote_close host |
c906108c SS |
1688 | } |
1689 | unset gdb_spawn_id | |
51f77c37 | 1690 | unset inferior_spawn_id |
c906108c SS |
1691 | } |
1692 | ||
3e3ffd2b | 1693 | # Load a file into the debugger. |
2db8e78e | 1694 | # The return value is 0 for success, -1 for failure. |
c906108c | 1695 | # |
2db8e78e MC |
1696 | # This procedure also set the global variable GDB_FILE_CMD_DEBUG_INFO |
1697 | # to one of these values: | |
3e3ffd2b | 1698 | # |
2db8e78e MC |
1699 | # debug file was loaded successfully and has debug information |
1700 | # nodebug file was loaded successfully and has no debug information | |
608e2dbb TT |
1701 | # lzma file was loaded, .gnu_debugdata found, but no LZMA support |
1702 | # compiled in | |
2db8e78e | 1703 | # fail file was not loaded |
c906108c | 1704 | # |
2db8e78e MC |
1705 | # I tried returning this information as part of the return value, |
1706 | # but ran into a mess because of the many re-implementations of | |
1707 | # gdb_load in config/*.exp. | |
3e3ffd2b | 1708 | # |
2db8e78e MC |
1709 | # TODO: gdb.base/sepdebug.exp and gdb.stabs/weird.exp might be able to use |
1710 | # this if they can get more information set. | |
3e3ffd2b | 1711 | |
c906108c | 1712 | proc gdb_file_cmd { arg } { |
3e3ffd2b | 1713 | global gdb_prompt |
c906108c | 1714 | global GDB |
b741e217 DJ |
1715 | global last_loaded_file |
1716 | ||
975531db | 1717 | # Save this for the benefit of gdbserver-support.exp. |
b741e217 | 1718 | set last_loaded_file $arg |
c906108c | 1719 | |
2db8e78e MC |
1720 | # Set whether debug info was found. |
1721 | # Default to "fail". | |
1722 | global gdb_file_cmd_debug_info | |
1723 | set gdb_file_cmd_debug_info "fail" | |
1724 | ||
c906108c | 1725 | if [is_remote host] { |
3e3ffd2b | 1726 | set arg [remote_download host $arg] |
c906108c | 1727 | if { $arg == "" } { |
2db8e78e MC |
1728 | perror "download failed" |
1729 | return -1 | |
c906108c SS |
1730 | } |
1731 | } | |
1732 | ||
4c42eaff | 1733 | # The file command used to kill the remote target. For the benefit |
f9e2e39d AH |
1734 | # of the testsuite, preserve this behavior. Mark as optional so it doesn't |
1735 | # get written to the stdin log. | |
1736 | send_gdb "kill\n" optional | |
4c42eaff DJ |
1737 | gdb_expect 120 { |
1738 | -re "Kill the program being debugged. .y or n. $" { | |
f9e2e39d | 1739 | send_gdb "y\n" answer |
4c42eaff DJ |
1740 | verbose "\t\tKilling previous program being debugged" |
1741 | exp_continue | |
1742 | } | |
1743 | -re "$gdb_prompt $" { | |
1744 | # OK. | |
1745 | } | |
1746 | } | |
1747 | ||
c906108c SS |
1748 | send_gdb "file $arg\n" |
1749 | gdb_expect 120 { | |
3453e7e4 | 1750 | -re "Reading symbols from.*LZMA support was disabled.*$gdb_prompt $" { |
608e2dbb TT |
1751 | verbose "\t\tLoaded $arg into $GDB; .gnu_debugdata found but no LZMA available" |
1752 | set gdb_file_cmd_debug_info "lzma" | |
1753 | return 0 | |
1754 | } | |
3453e7e4 | 1755 | -re "Reading symbols from.*no debugging symbols found.*$gdb_prompt $" { |
975531db | 1756 | verbose "\t\tLoaded $arg into $GDB with no debugging symbols" |
2db8e78e MC |
1757 | set gdb_file_cmd_debug_info "nodebug" |
1758 | return 0 | |
3e3ffd2b | 1759 | } |
3453e7e4 | 1760 | -re "Reading symbols from.*$gdb_prompt $" { |
975531db | 1761 | verbose "\t\tLoaded $arg into $GDB" |
2db8e78e MC |
1762 | set gdb_file_cmd_debug_info "debug" |
1763 | return 0 | |
c906108c | 1764 | } |
c906108c | 1765 | -re "Load new symbol table from \".*\".*y or n. $" { |
f9e2e39d | 1766 | send_gdb "y\n" answer |
c906108c | 1767 | gdb_expect 120 { |
3453e7e4 | 1768 | -re "Reading symbols from.*$gdb_prompt $" { |
c906108c | 1769 | verbose "\t\tLoaded $arg with new symbol table into $GDB" |
2db8e78e MC |
1770 | set gdb_file_cmd_debug_info "debug" |
1771 | return 0 | |
c906108c SS |
1772 | } |
1773 | timeout { | |
975531db | 1774 | perror "Couldn't load $arg, other program already loaded (timeout)." |
2db8e78e | 1775 | return -1 |
c906108c | 1776 | } |
975531db DE |
1777 | eof { |
1778 | perror "Couldn't load $arg, other program already loaded (eof)." | |
1779 | return -1 | |
1780 | } | |
c906108c SS |
1781 | } |
1782 | } | |
1783 | -re "No such file or directory.*$gdb_prompt $" { | |
2db8e78e MC |
1784 | perror "($arg) No such file or directory" |
1785 | return -1 | |
c906108c | 1786 | } |
04e7407c | 1787 | -re "A problem internal to GDB has been detected" { |
5b7d0050 | 1788 | fail "($arg) (GDB internal error)" |
04e7407c JK |
1789 | gdb_internal_error_resync |
1790 | return -1 | |
1791 | } | |
c906108c | 1792 | -re "$gdb_prompt $" { |
975531db | 1793 | perror "Couldn't load $arg into $GDB." |
2db8e78e | 1794 | return -1 |
c906108c SS |
1795 | } |
1796 | timeout { | |
975531db | 1797 | perror "Couldn't load $arg into $GDB (timeout)." |
2db8e78e | 1798 | return -1 |
c906108c SS |
1799 | } |
1800 | eof { | |
1801 | # This is an attempt to detect a core dump, but seems not to | |
1802 | # work. Perhaps we need to match .* followed by eof, in which | |
1803 | # gdb_expect does not seem to have a way to do that. | |
975531db | 1804 | perror "Couldn't load $arg into $GDB (eof)." |
2db8e78e | 1805 | return -1 |
c906108c SS |
1806 | } |
1807 | } | |
1808 | } | |
1809 | ||
94696ad3 PA |
1810 | # Default gdb_spawn procedure. |
1811 | ||
1812 | proc default_gdb_spawn { } { | |
1813 | global use_gdb_stub | |
c906108c | 1814 | global GDB |
6b8ce727 | 1815 | global INTERNAL_GDBFLAGS GDBFLAGS |
4ec70201 | 1816 | global gdb_spawn_id |
c906108c | 1817 | |
4ec70201 | 1818 | gdb_stop_suppressing_tests |
c906108c | 1819 | |
e11ac3a3 JK |
1820 | # Set the default value, it may be overriden later by specific testfile. |
1821 | # | |
1822 | # Use `set_board_info use_gdb_stub' for the board file to flag the inferior | |
1823 | # is already started after connecting and run/attach are not supported. | |
1824 | # This is used for the "remote" protocol. After GDB starts you should | |
1825 | # check global $use_gdb_stub instead of the board as the testfile may force | |
1826 | # a specific different target protocol itself. | |
1827 | set use_gdb_stub [target_info exists use_gdb_stub] | |
1828 | ||
6b8ce727 | 1829 | verbose "Spawning $GDB $INTERNAL_GDBFLAGS $GDBFLAGS" |
408e9b8b | 1830 | gdb_write_cmd_file "$GDB $INTERNAL_GDBFLAGS $GDBFLAGS" |
c906108c SS |
1831 | |
1832 | if [info exists gdb_spawn_id] { | |
ae59b1da | 1833 | return 0 |
c906108c SS |
1834 | } |
1835 | ||
1836 | if ![is_remote host] { | |
1837 | if { [which $GDB] == 0 } then { | |
1838 | perror "$GDB does not exist." | |
1839 | exit 1 | |
1840 | } | |
1841 | } | |
4ec70201 | 1842 | set res [remote_spawn host "$GDB $INTERNAL_GDBFLAGS $GDBFLAGS [host_info gdb_opts]"] |
c906108c SS |
1843 | if { $res < 0 || $res == "" } { |
1844 | perror "Spawning $GDB failed." | |
ae59b1da | 1845 | return 1 |
c906108c | 1846 | } |
717cf30c AG |
1847 | |
1848 | set gdb_spawn_id $res | |
94696ad3 PA |
1849 | return 0 |
1850 | } | |
1851 | ||
1852 | # Default gdb_start procedure. | |
1853 | ||
1854 | proc default_gdb_start { } { | |
bd447abb | 1855 | global gdb_prompt |
94696ad3 | 1856 | global gdb_spawn_id |
f71c18e7 | 1857 | global inferior_spawn_id |
94696ad3 PA |
1858 | |
1859 | if [info exists gdb_spawn_id] { | |
1860 | return 0 | |
1861 | } | |
1862 | ||
f9e2e39d AH |
1863 | # Keep track of the number of times GDB has been launched. |
1864 | global gdb_instances | |
1865 | incr gdb_instances | |
1866 | ||
1867 | gdb_stdin_log_init | |
1868 | ||
94696ad3 PA |
1869 | set res [gdb_spawn] |
1870 | if { $res != 0} { | |
1871 | return $res | |
1872 | } | |
1873 | ||
f71c18e7 PA |
1874 | # Default to assuming inferior I/O is done on GDB's terminal. |
1875 | if {![info exists inferior_spawn_id]} { | |
1876 | set inferior_spawn_id $gdb_spawn_id | |
1877 | } | |
1878 | ||
94696ad3 PA |
1879 | # When running over NFS, particularly if running many simultaneous |
1880 | # tests on different hosts all using the same server, things can | |
1881 | # get really slow. Give gdb at least 3 minutes to start up. | |
bd447abb SM |
1882 | gdb_expect 360 { |
1883 | -re "\[\r\n\]$gdb_prompt $" { | |
1884 | verbose "GDB initialized." | |
1885 | } | |
1886 | -re "$gdb_prompt $" { | |
1887 | perror "GDB never initialized." | |
1888 | unset gdb_spawn_id | |
1889 | return -1 | |
1890 | } | |
1891 | timeout { | |
1892 | perror "(timeout) GDB never initialized after 10 seconds." | |
1893 | remote_close host | |
1894 | unset gdb_spawn_id | |
1895 | return -1 | |
c906108c SS |
1896 | } |
1897 | } | |
94696ad3 | 1898 | |
c906108c SS |
1899 | # force the height to "unlimited", so no pagers get used |
1900 | ||
1901 | send_gdb "set height 0\n" | |
1902 | gdb_expect 10 { | |
1903 | -re "$gdb_prompt $" { | |
1904 | verbose "Setting height to 0." 2 | |
1905 | } | |
1906 | timeout { | |
1907 | warning "Couldn't set the height to 0" | |
1908 | } | |
1909 | } | |
1910 | # force the width to "unlimited", so no wraparound occurs | |
1911 | send_gdb "set width 0\n" | |
1912 | gdb_expect 10 { | |
1913 | -re "$gdb_prompt $" { | |
1914 | verbose "Setting width to 0." 2 | |
1915 | } | |
1916 | timeout { | |
1917 | warning "Couldn't set the width to 0." | |
1918 | } | |
1919 | } | |
29b52314 AH |
1920 | |
1921 | gdb_debug_init | |
ae59b1da | 1922 | return 0 |
c906108c SS |
1923 | } |
1924 | ||
717cf30c AG |
1925 | # Utility procedure to give user control of the gdb prompt in a script. It is |
1926 | # meant to be used for debugging test cases, and should not be left in the | |
1927 | # test cases code. | |
1928 | ||
1929 | proc gdb_interact { } { | |
1930 | global gdb_spawn_id | |
1931 | set spawn_id $gdb_spawn_id | |
1932 | ||
1933 | send_user "+------------------------------------------+\n" | |
1934 | send_user "| Script interrupted, you can now interact |\n" | |
1935 | send_user "| with by gdb. Type >>> to continue. |\n" | |
1936 | send_user "+------------------------------------------+\n" | |
1937 | ||
1938 | interact { | |
1939 | ">>>" return | |
1940 | } | |
1941 | } | |
1942 | ||
ec3c07fc NS |
1943 | # Examine the output of compilation to determine whether compilation |
1944 | # failed or not. If it failed determine whether it is due to missing | |
1945 | # compiler or due to compiler error. Report pass, fail or unsupported | |
1946 | # as appropriate | |
1947 | ||
1948 | proc gdb_compile_test {src output} { | |
1949 | if { $output == "" } { | |
1950 | pass "compilation [file tail $src]" | |
1951 | } elseif { [regexp {^[a-zA-Z_0-9]+: Can't find [^ ]+\.$} $output] } { | |
1952 | unsupported "compilation [file tail $src]" | |
1953 | } elseif { [regexp {.*: command not found[\r|\n]*$} $output] } { | |
1954 | unsupported "compilation [file tail $src]" | |
6bb85cd1 DE |
1955 | } elseif { [regexp {.*: [^\r\n]*compiler not installed[^\r\n]*[\r|\n]*$} $output] } { |
1956 | unsupported "compilation [file tail $src]" | |
ec3c07fc NS |
1957 | } else { |
1958 | verbose -log "compilation failed: $output" 2 | |
1959 | fail "compilation [file tail $src]" | |
1960 | } | |
1961 | } | |
1962 | ||
d4f3574e SS |
1963 | # Return a 1 for configurations for which we don't even want to try to |
1964 | # test C++. | |
1965 | ||
1966 | proc skip_cplus_tests {} { | |
d4f3574e SS |
1967 | if { [istarget "h8300-*-*"] } { |
1968 | return 1 | |
1969 | } | |
81d2cbae | 1970 | |
1146c7f1 SC |
1971 | # The C++ IO streams are too large for HC11/HC12 and are thus not |
1972 | # available. The gdb C++ tests use them and don't compile. | |
1973 | if { [istarget "m6811-*-*"] } { | |
1974 | return 1 | |
1975 | } | |
1976 | if { [istarget "m6812-*-*"] } { | |
1977 | return 1 | |
1978 | } | |
d4f3574e SS |
1979 | return 0 |
1980 | } | |
1981 | ||
759f0f0b PA |
1982 | # Return a 1 for configurations for which don't have both C++ and the STL. |
1983 | ||
1984 | proc skip_stl_tests {} { | |
1985 | # Symbian supports the C++ language, but the STL is missing | |
1986 | # (both headers and libraries). | |
1987 | if { [istarget "arm*-*-symbianelf*"] } { | |
1988 | return 1 | |
1989 | } | |
1990 | ||
1991 | return [skip_cplus_tests] | |
1992 | } | |
1993 | ||
89a237cb MC |
1994 | # Return a 1 if I don't even want to try to test FORTRAN. |
1995 | ||
1996 | proc skip_fortran_tests {} { | |
1997 | return 0 | |
1998 | } | |
1999 | ||
ec3c07fc NS |
2000 | # Return a 1 if I don't even want to try to test ada. |
2001 | ||
2002 | proc skip_ada_tests {} { | |
2003 | return 0 | |
2004 | } | |
2005 | ||
a766d390 DE |
2006 | # Return a 1 if I don't even want to try to test GO. |
2007 | ||
2008 | proc skip_go_tests {} { | |
2009 | return 0 | |
2010 | } | |
2011 | ||
7f420862 IB |
2012 | # Return a 1 if I don't even want to try to test D. |
2013 | ||
2014 | proc skip_d_tests {} { | |
2015 | return 0 | |
2016 | } | |
2017 | ||
67218854 TT |
2018 | # Return 1 to skip Rust tests, 0 to try them. |
2019 | proc skip_rust_tests {} { | |
2020 | return [expr {![isnative]}] | |
2021 | } | |
2022 | ||
f6bbabf0 | 2023 | # Return a 1 for configurations that do not support Python scripting. |
4d6cceb4 | 2024 | # PROMPT_REGEXP is the expected prompt. |
f6bbabf0 | 2025 | |
4d6cceb4 | 2026 | proc skip_python_tests_prompt { prompt_regexp } { |
9325cb04 | 2027 | global gdb_py_is_py3k |
9325cb04 | 2028 | |
590003dc TV |
2029 | gdb_test_multiple "python print ('test')" "verify python support" \ |
2030 | -prompt "$prompt_regexp" { | |
2031 | -re "not supported.*$prompt_regexp" { | |
2032 | unsupported "Python support is disabled." | |
2033 | return 1 | |
2034 | } | |
2035 | -re "$prompt_regexp" {} | |
f6bbabf0 | 2036 | } |
f6bbabf0 | 2037 | |
590003dc TV |
2038 | gdb_test_multiple "python print (sys.version_info\[0\])" "check if python 3" \ |
2039 | -prompt "$prompt_regexp" { | |
2040 | -re "3.*$prompt_regexp" { | |
2041 | set gdb_py_is_py3k 1 | |
2042 | } | |
2043 | -re ".*$prompt_regexp" { | |
2044 | set gdb_py_is_py3k 0 | |
2045 | } | |
2046 | } | |
9325cb04 | 2047 | |
f6bbabf0 PM |
2048 | return 0 |
2049 | } | |
2050 | ||
4d6cceb4 DE |
2051 | # Return a 1 for configurations that do not support Python scripting. |
2052 | # Note: This also sets various globals that specify which version of Python | |
2053 | # is in use. See skip_python_tests_prompt. | |
2054 | ||
2055 | proc skip_python_tests {} { | |
2056 | global gdb_prompt | |
2057 | return [skip_python_tests_prompt "$gdb_prompt $"] | |
2058 | } | |
2059 | ||
93f02886 DJ |
2060 | # Return a 1 if we should skip shared library tests. |
2061 | ||
2062 | proc skip_shlib_tests {} { | |
2063 | # Run the shared library tests on native systems. | |
2064 | if {[isnative]} { | |
2065 | return 0 | |
2066 | } | |
2067 | ||
2068 | # An abbreviated list of remote targets where we should be able to | |
2069 | # run shared library tests. | |
2070 | if {([istarget *-*-linux*] | |
2071 | || [istarget *-*-*bsd*] | |
2072 | || [istarget *-*-solaris2*] | |
2073 | || [istarget arm*-*-symbianelf*] | |
2074 | || [istarget *-*-mingw*] | |
2075 | || [istarget *-*-cygwin*] | |
2076 | || [istarget *-*-pe*])} { | |
2077 | return 0 | |
2078 | } | |
2079 | ||
2080 | return 1 | |
2081 | } | |
2082 | ||
ebe3b578 AB |
2083 | # Return 1 if we should skip tui related tests. |
2084 | ||
2085 | proc skip_tui_tests {} { | |
2086 | global gdb_prompt | |
2087 | ||
2088 | gdb_test_multiple "help layout" "verify tui support" { | |
2089 | -re "Undefined command: \"layout\".*$gdb_prompt $" { | |
2090 | return 1 | |
2091 | } | |
2092 | -re "$gdb_prompt $" { | |
2093 | } | |
2094 | } | |
2095 | ||
2096 | return 0 | |
2097 | } | |
2098 | ||
6a5870ce PA |
2099 | # Test files shall make sure all the test result lines in gdb.sum are |
2100 | # unique in a test run, so that comparing the gdb.sum files of two | |
2101 | # test runs gives correct results. Test files that exercise | |
2102 | # variations of the same tests more than once, shall prefix the | |
2103 | # different test invocations with different identifying strings in | |
2104 | # order to make them unique. | |
2105 | # | |
2106 | # About test prefixes: | |
2107 | # | |
2108 | # $pf_prefix is the string that dejagnu prints after the result (FAIL, | |
2109 | # PASS, etc.), and before the test message/name in gdb.sum. E.g., the | |
2110 | # underlined substring in | |
2111 | # | |
2112 | # PASS: gdb.base/mytest.exp: some test | |
2113 | # ^^^^^^^^^^^^^^^^^^^^ | |
2114 | # | |
2115 | # is $pf_prefix. | |
2116 | # | |
2117 | # The easiest way to adjust the test prefix is to append a test | |
2118 | # variation prefix to the $pf_prefix, using the with_test_prefix | |
2119 | # procedure. E.g., | |
2120 | # | |
2121 | # proc do_tests {} { | |
2122 | # gdb_test ... ... "test foo" | |
2123 | # gdb_test ... ... "test bar" | |
2124 | # | |
0f4d39d5 | 2125 | # with_test_prefix "subvariation a" { |
6a5870ce PA |
2126 | # gdb_test ... ... "test x" |
2127 | # } | |
2128 | # | |
0f4d39d5 | 2129 | # with_test_prefix "subvariation b" { |
6a5870ce PA |
2130 | # gdb_test ... ... "test x" |
2131 | # } | |
2132 | # } | |
2133 | # | |
0f4d39d5 | 2134 | # with_test_prefix "variation1" { |
6a5870ce PA |
2135 | # ...do setup for variation 1... |
2136 | # do_tests | |
2137 | # } | |
2138 | # | |
0f4d39d5 | 2139 | # with_test_prefix "variation2" { |
6a5870ce PA |
2140 | # ...do setup for variation 2... |
2141 | # do_tests | |
2142 | # } | |
2143 | # | |
2144 | # Results in: | |
2145 | # | |
2146 | # PASS: gdb.base/mytest.exp: variation1: test foo | |
2147 | # PASS: gdb.base/mytest.exp: variation1: test bar | |
2148 | # PASS: gdb.base/mytest.exp: variation1: subvariation a: test x | |
2149 | # PASS: gdb.base/mytest.exp: variation1: subvariation b: test x | |
2150 | # PASS: gdb.base/mytest.exp: variation2: test foo | |
2151 | # PASS: gdb.base/mytest.exp: variation2: test bar | |
2152 | # PASS: gdb.base/mytest.exp: variation2: subvariation a: test x | |
2153 | # PASS: gdb.base/mytest.exp: variation2: subvariation b: test x | |
2154 | # | |
2155 | # If for some reason more flexibility is necessary, one can also | |
2156 | # manipulate the pf_prefix global directly, treating it as a string. | |
2157 | # E.g., | |
2158 | # | |
2159 | # global pf_prefix | |
2160 | # set saved_pf_prefix | |
0f4d39d5 | 2161 | # append pf_prefix "${foo}: bar" |
6a5870ce PA |
2162 | # ... actual tests ... |
2163 | # set pf_prefix $saved_pf_prefix | |
2164 | # | |
2165 | ||
2166 | # Run BODY in the context of the caller, with the current test prefix | |
0f4d39d5 PA |
2167 | # (pf_prefix) appended with one space, then PREFIX, and then a colon. |
2168 | # Returns the result of BODY. | |
6a5870ce PA |
2169 | # |
2170 | proc with_test_prefix { prefix body } { | |
2171 | global pf_prefix | |
2172 | ||
2173 | set saved $pf_prefix | |
0f4d39d5 | 2174 | append pf_prefix " " $prefix ":" |
6a5870ce PA |
2175 | set code [catch {uplevel 1 $body} result] |
2176 | set pf_prefix $saved | |
2177 | ||
2178 | if {$code == 1} { | |
2179 | global errorInfo errorCode | |
2180 | return -code $code -errorinfo $errorInfo -errorcode $errorCode $result | |
2181 | } else { | |
2182 | return -code $code $result | |
2183 | } | |
2184 | } | |
2185 | ||
f1da4b11 PA |
2186 | # Wrapper for foreach that calls with_test_prefix on each iteration, |
2187 | # including the iterator's name and current value in the prefix. | |
2188 | ||
2189 | proc foreach_with_prefix {var list body} { | |
2190 | upvar 1 $var myvar | |
2191 | foreach myvar $list { | |
2192 | with_test_prefix "$var=$myvar" { | |
a26c8de0 PA |
2193 | set code [catch {uplevel 1 $body} result] |
2194 | } | |
2195 | ||
2196 | if {$code == 1} { | |
2197 | global errorInfo errorCode | |
2198 | return -code $code -errorinfo $errorInfo -errorcode $errorCode $result | |
213fd9fa PA |
2199 | } elseif {$code == 3} { |
2200 | break | |
2201 | } elseif {$code == 2} { | |
a26c8de0 | 2202 | return -code $code $result |
f1da4b11 PA |
2203 | } |
2204 | } | |
2205 | } | |
2206 | ||
64f367a2 PA |
2207 | # Like TCL's native proc, but defines a procedure that wraps its body |
2208 | # within 'with_test_prefix "$proc_name" { ... }'. | |
2209 | proc proc_with_prefix {name arguments body} { | |
2210 | # Define the advertised proc. | |
2211 | proc $name $arguments [list with_test_prefix $name $body] | |
2212 | } | |
2213 | ||
2214 | ||
abe8e607 PP |
2215 | # Run BODY in the context of the caller. After BODY is run, the variables |
2216 | # listed in VARS will be reset to the values they had before BODY was run. | |
2217 | # | |
2218 | # This is useful for providing a scope in which it is safe to temporarily | |
2219 | # modify global variables, e.g. | |
2220 | # | |
2221 | # global INTERNAL_GDBFLAGS | |
2222 | # global env | |
2223 | # | |
2224 | # set foo GDBHISTSIZE | |
2225 | # | |
2226 | # save_vars { INTERNAL_GDBFLAGS env($foo) env(HOME) } { | |
2227 | # append INTERNAL_GDBFLAGS " -nx" | |
2228 | # unset -nocomplain env(GDBHISTSIZE) | |
2229 | # gdb_start | |
2230 | # gdb_test ... | |
2231 | # } | |
2232 | # | |
2233 | # Here, although INTERNAL_GDBFLAGS, env(GDBHISTSIZE) and env(HOME) may be | |
2234 | # modified inside BODY, this proc guarantees that the modifications will be | |
2235 | # undone after BODY finishes executing. | |
2236 | ||
2237 | proc save_vars { vars body } { | |
2238 | array set saved_scalars { } | |
2239 | array set saved_arrays { } | |
2240 | set unset_vars { } | |
2241 | ||
2242 | foreach var $vars { | |
2243 | # First evaluate VAR in the context of the caller in case the variable | |
2244 | # name may be a not-yet-interpolated string like env($foo) | |
2245 | set var [uplevel 1 list $var] | |
2246 | ||
2247 | if [uplevel 1 [list info exists $var]] { | |
2248 | if [uplevel 1 [list array exists $var]] { | |
2249 | set saved_arrays($var) [uplevel 1 [list array get $var]] | |
2250 | } else { | |
2251 | set saved_scalars($var) [uplevel 1 [list set $var]] | |
2252 | } | |
2253 | } else { | |
2254 | lappend unset_vars $var | |
2255 | } | |
2256 | } | |
2257 | ||
2258 | set code [catch {uplevel 1 $body} result] | |
2259 | ||
2260 | foreach {var value} [array get saved_scalars] { | |
2261 | uplevel 1 [list set $var $value] | |
2262 | } | |
2263 | ||
2264 | foreach {var value} [array get saved_arrays] { | |
2265 | uplevel 1 [list unset $var] | |
2266 | uplevel 1 [list array set $var $value] | |
2267 | } | |
2268 | ||
2269 | foreach var $unset_vars { | |
2270 | uplevel 1 [list unset -nocomplain $var] | |
2271 | } | |
2272 | ||
2273 | if {$code == 1} { | |
2274 | global errorInfo errorCode | |
2275 | return -code $code -errorinfo $errorInfo -errorcode $errorCode $result | |
2276 | } else { | |
2277 | return -code $code $result | |
2278 | } | |
2279 | } | |
2280 | ||
25e3c82c SDJ |
2281 | # Run tests in BODY with the current working directory (CWD) set to |
2282 | # DIR. When BODY is finished, restore the original CWD. Return the | |
2283 | # result of BODY. | |
2284 | # | |
2285 | # This procedure doesn't check if DIR is a valid directory, so you | |
2286 | # have to make sure of that. | |
2287 | ||
2288 | proc with_cwd { dir body } { | |
2289 | set saved_dir [pwd] | |
2290 | verbose -log "Switching to directory $dir (saved CWD: $saved_dir)." | |
2291 | cd $dir | |
2292 | ||
2293 | set code [catch {uplevel 1 $body} result] | |
2294 | ||
2295 | verbose -log "Switching back to $saved_dir." | |
2296 | cd $saved_dir | |
2297 | ||
2298 | if {$code == 1} { | |
2299 | global errorInfo errorCode | |
2300 | return -code $code -errorinfo $errorInfo -errorcode $errorCode $result | |
2301 | } else { | |
2302 | return -code $code $result | |
2303 | } | |
2304 | } | |
abe8e607 | 2305 | |
8b5e6dc2 YQ |
2306 | # Run tests in BODY with GDB prompt and variable $gdb_prompt set to |
2307 | # PROMPT. When BODY is finished, restore GDB prompt and variable | |
2308 | # $gdb_prompt. | |
2309 | # Returns the result of BODY. | |
3714cea7 DE |
2310 | # |
2311 | # Notes: | |
2312 | # | |
2313 | # 1) If you want to use, for example, "(foo)" as the prompt you must pass it | |
2314 | # as "(foo)", and not the regexp form "\(foo\)" (expressed as "\\(foo\\)" in | |
2315 | # TCL). PROMPT is internally converted to a suitable regexp for matching. | |
2316 | # We do the conversion from "(foo)" to "\(foo\)" here for a few reasons: | |
2317 | # a) It's more intuitive for callers to pass the plain text form. | |
2318 | # b) We need two forms of the prompt: | |
2319 | # - a regexp to use in output matching, | |
2320 | # - a value to pass to the "set prompt" command. | |
2321 | # c) It's easier to convert the plain text form to its regexp form. | |
2322 | # | |
2323 | # 2) Don't add a trailing space, we do that here. | |
8b5e6dc2 YQ |
2324 | |
2325 | proc with_gdb_prompt { prompt body } { | |
2326 | global gdb_prompt | |
2327 | ||
3714cea7 DE |
2328 | # Convert "(foo)" to "\(foo\)". |
2329 | # We don't use string_to_regexp because while it works today it's not | |
2330 | # clear it will work tomorrow: the value we need must work as both a | |
2331 | # regexp *and* as the argument to the "set prompt" command, at least until | |
2332 | # we start recording both forms separately instead of just $gdb_prompt. | |
2333 | # The testsuite is pretty-much hardwired to interpret $gdb_prompt as the | |
2334 | # regexp form. | |
2335 | regsub -all {[]*+.|()^$\[\\]} $prompt {\\&} prompt | |
2336 | ||
8b5e6dc2 YQ |
2337 | set saved $gdb_prompt |
2338 | ||
3714cea7 | 2339 | verbose -log "Setting gdb prompt to \"$prompt \"." |
8b5e6dc2 YQ |
2340 | set gdb_prompt $prompt |
2341 | gdb_test_no_output "set prompt $prompt " "" | |
2342 | ||
2343 | set code [catch {uplevel 1 $body} result] | |
2344 | ||
3714cea7 | 2345 | verbose -log "Restoring gdb prompt to \"$saved \"." |
8b5e6dc2 YQ |
2346 | set gdb_prompt $saved |
2347 | gdb_test_no_output "set prompt $saved " "" | |
2348 | ||
2349 | if {$code == 1} { | |
2350 | global errorInfo errorCode | |
2351 | return -code $code -errorinfo $errorInfo -errorcode $errorCode $result | |
2352 | } else { | |
2353 | return -code $code $result | |
2354 | } | |
2355 | } | |
2356 | ||
389b98f7 YQ |
2357 | # Run tests in BODY with target-charset setting to TARGET_CHARSET. When |
2358 | # BODY is finished, restore target-charset. | |
2359 | ||
2360 | proc with_target_charset { target_charset body } { | |
2361 | global gdb_prompt | |
2362 | ||
2363 | set saved "" | |
2364 | gdb_test_multiple "show target-charset" "" { | |
2365 | -re "The target character set is \".*; currently (.*)\"\..*$gdb_prompt " { | |
2366 | set saved $expect_out(1,string) | |
2367 | } | |
2368 | -re "The target character set is \"(.*)\".*$gdb_prompt " { | |
2369 | set saved $expect_out(1,string) | |
2370 | } | |
2371 | -re ".*$gdb_prompt " { | |
2372 | fail "get target-charset" | |
2373 | } | |
2374 | } | |
2375 | ||
2376 | gdb_test_no_output "set target-charset $target_charset" "" | |
2377 | ||
2378 | set code [catch {uplevel 1 $body} result] | |
2379 | ||
2380 | gdb_test_no_output "set target-charset $saved" "" | |
2381 | ||
2382 | if {$code == 1} { | |
2383 | global errorInfo errorCode | |
2384 | return -code $code -errorinfo $errorInfo -errorcode $errorCode $result | |
2385 | } else { | |
2386 | return -code $code $result | |
2387 | } | |
2388 | } | |
2389 | ||
ac69f786 PA |
2390 | # Switch the default spawn id to SPAWN_ID, so that gdb_test, |
2391 | # mi_gdb_test etc. default to using it. | |
2392 | ||
2393 | proc switch_gdb_spawn_id {spawn_id} { | |
2394 | global gdb_spawn_id | |
2395 | global board board_info | |
2396 | ||
2397 | set gdb_spawn_id $spawn_id | |
2398 | set board [host_info name] | |
2399 | set board_info($board,fileid) $spawn_id | |
2400 | } | |
2401 | ||
4295e285 PA |
2402 | # Clear the default spawn id. |
2403 | ||
2404 | proc clear_gdb_spawn_id {} { | |
2405 | global gdb_spawn_id | |
2406 | global board board_info | |
2407 | ||
2408 | unset -nocomplain gdb_spawn_id | |
2409 | set board [host_info name] | |
2410 | unset -nocomplain board_info($board,fileid) | |
2411 | } | |
2412 | ||
ac69f786 PA |
2413 | # Run BODY with SPAWN_ID as current spawn id. |
2414 | ||
2415 | proc with_spawn_id { spawn_id body } { | |
2416 | global gdb_spawn_id | |
2417 | ||
4295e285 PA |
2418 | if [info exists gdb_spawn_id] { |
2419 | set saved_spawn_id $gdb_spawn_id | |
2420 | } | |
2421 | ||
ac69f786 PA |
2422 | switch_gdb_spawn_id $spawn_id |
2423 | ||
2424 | set code [catch {uplevel 1 $body} result] | |
2425 | ||
4295e285 PA |
2426 | if [info exists saved_spawn_id] { |
2427 | switch_gdb_spawn_id $saved_spawn_id | |
2428 | } else { | |
2429 | clear_gdb_spawn_id | |
2430 | } | |
ac69f786 PA |
2431 | |
2432 | if {$code == 1} { | |
2433 | global errorInfo errorCode | |
2434 | return -code $code -errorinfo $errorInfo -errorcode $errorCode $result | |
2435 | } else { | |
2436 | return -code $code $result | |
2437 | } | |
2438 | } | |
2439 | ||
45fd756c YQ |
2440 | # Select the largest timeout from all the timeouts: |
2441 | # - the local "timeout" variable of the scope two levels above, | |
2442 | # - the global "timeout" variable, | |
2443 | # - the board variable "gdb,timeout". | |
2444 | ||
2445 | proc get_largest_timeout {} { | |
2446 | upvar #0 timeout gtimeout | |
2447 | upvar 2 timeout timeout | |
2448 | ||
2449 | set tmt 0 | |
2450 | if [info exists timeout] { | |
2451 | set tmt $timeout | |
2452 | } | |
2453 | if { [info exists gtimeout] && $gtimeout > $tmt } { | |
2454 | set tmt $gtimeout | |
2455 | } | |
2456 | if { [target_info exists gdb,timeout] | |
2457 | && [target_info gdb,timeout] > $tmt } { | |
2458 | set tmt [target_info gdb,timeout] | |
2459 | } | |
2460 | if { $tmt == 0 } { | |
2461 | # Eeeeew. | |
2462 | set tmt 60 | |
2463 | } | |
2464 | ||
2465 | return $tmt | |
2466 | } | |
2467 | ||
2468 | # Run tests in BODY with timeout increased by factor of FACTOR. When | |
2469 | # BODY is finished, restore timeout. | |
2470 | ||
2471 | proc with_timeout_factor { factor body } { | |
2472 | global timeout | |
2473 | ||
2474 | set savedtimeout $timeout | |
2475 | ||
2476 | set timeout [expr [get_largest_timeout] * $factor] | |
2477 | set code [catch {uplevel 1 $body} result] | |
2478 | ||
2479 | set timeout $savedtimeout | |
2480 | if {$code == 1} { | |
2481 | global errorInfo errorCode | |
2482 | return -code $code -errorinfo $errorInfo -errorcode $errorCode $result | |
2483 | } else { | |
2484 | return -code $code $result | |
2485 | } | |
2486 | } | |
2487 | ||
d86bd7cb TV |
2488 | # Run BODY with timeout factor FACTOR if check-read1 is used. |
2489 | ||
2490 | proc with_read1_timeout_factor { factor body } { | |
2491 | if { [info exists ::env(READ1)] == 1 && $::env(READ1) == 1 } { | |
2492 | # Use timeout factor | |
2493 | } else { | |
2494 | # Reset timeout factor | |
2495 | set factor 1 | |
2496 | } | |
2497 | return [uplevel [list with_timeout_factor $factor $body]] | |
2498 | } | |
2499 | ||
e43ec454 YQ |
2500 | # Return 1 if _Complex types are supported, otherwise, return 0. |
2501 | ||
17e1c970 | 2502 | gdb_caching_proc support_complex_tests { |
fdebf1a4 YQ |
2503 | |
2504 | if { [gdb_skip_float_test] } { | |
2505 | # If floating point is not supported, _Complex is not | |
2506 | # supported. | |
2507 | return 0 | |
2508 | } | |
2509 | ||
c221b2f7 | 2510 | # Compile a test program containing _Complex types. |
e43ec454 | 2511 | |
c221b2f7 | 2512 | return [gdb_can_simple_compile complex { |
11ec5965 YQ |
2513 | int main() { |
2514 | _Complex float cf; | |
2515 | _Complex double cd; | |
2516 | _Complex long double cld; | |
2517 | return 0; | |
2518 | } | |
c221b2f7 | 2519 | } executable] |
e43ec454 YQ |
2520 | } |
2521 | ||
d7445728 TV |
2522 | # Return 1 if compiling go is supported. |
2523 | gdb_caching_proc support_go_compile { | |
2524 | ||
2525 | return [gdb_can_simple_compile go-hello { | |
2526 | package main | |
2527 | import "fmt" | |
2528 | func main() { | |
2529 | fmt.Println("hello world") | |
2530 | } | |
2531 | } executable go] | |
2532 | } | |
2533 | ||
4d7be007 YQ |
2534 | # Return 1 if GDB can get a type for siginfo from the target, otherwise |
2535 | # return 0. | |
2536 | ||
2537 | proc supports_get_siginfo_type {} { | |
5cd867b4 | 2538 | if { [istarget "*-*-linux*"] } { |
4d7be007 YQ |
2539 | return 1 |
2540 | } else { | |
2541 | return 0 | |
2542 | } | |
2543 | } | |
2544 | ||
1ed415e2 | 2545 | # Return 1 if the target supports hardware single stepping. |
ab254057 | 2546 | |
1ed415e2 | 2547 | proc can_hardware_single_step {} { |
ab254057 | 2548 | |
b0221781 | 2549 | if { [istarget "arm*-*-*"] || [istarget "mips*-*-*"] |
b5bee914 YQ |
2550 | || [istarget "tic6x-*-*"] || [istarget "sparc*-*-linux*"] |
2551 | || [istarget "nios2-*-*"] } { | |
ab254057 YQ |
2552 | return 0 |
2553 | } | |
2554 | ||
2555 | return 1 | |
2556 | } | |
2557 | ||
1ed415e2 PA |
2558 | # Return 1 if target hardware or OS supports single stepping to signal |
2559 | # handler, otherwise, return 0. | |
2560 | ||
2561 | proc can_single_step_to_signal_handler {} { | |
2562 | # Targets don't have hardware single step. On these targets, when | |
2563 | # a signal is delivered during software single step, gdb is unable | |
2564 | # to determine the next instruction addresses, because start of signal | |
2565 | # handler is one of them. | |
2566 | return [can_hardware_single_step] | |
2567 | } | |
2568 | ||
d3895d7d YQ |
2569 | # Return 1 if target supports process record, otherwise return 0. |
2570 | ||
2571 | proc supports_process_record {} { | |
2572 | ||
2573 | if [target_info exists gdb,use_precord] { | |
2574 | return [target_info gdb,use_precord] | |
2575 | } | |
2576 | ||
596662fa | 2577 | if { [istarget "arm*-*-linux*"] || [istarget "x86_64-*-linux*"] |
b4cdae6f | 2578 | || [istarget "i\[34567\]86-*-linux*"] |
a81bfbd0 | 2579 | || [istarget "aarch64*-*-linux*"] |
566c56c9 MK |
2580 | || [istarget "powerpc*-*-linux*"] |
2581 | || [istarget "s390*-*-linux*"] } { | |
d3895d7d YQ |
2582 | return 1 |
2583 | } | |
2584 | ||
2585 | return 0 | |
2586 | } | |
2587 | ||
2588 | # Return 1 if target supports reverse debugging, otherwise return 0. | |
2589 | ||
2590 | proc supports_reverse {} { | |
2591 | ||
2592 | if [target_info exists gdb,can_reverse] { | |
2593 | return [target_info gdb,can_reverse] | |
2594 | } | |
2595 | ||
596662fa | 2596 | if { [istarget "arm*-*-linux*"] || [istarget "x86_64-*-linux*"] |
b4cdae6f | 2597 | || [istarget "i\[34567\]86-*-linux*"] |
a81bfbd0 | 2598 | || [istarget "aarch64*-*-linux*"] |
566c56c9 MK |
2599 | || [istarget "powerpc*-*-linux*"] |
2600 | || [istarget "s390*-*-linux*"] } { | |
d3895d7d YQ |
2601 | return 1 |
2602 | } | |
2603 | ||
2604 | return 0 | |
2605 | } | |
2606 | ||
0d4d0e77 YQ |
2607 | # Return 1 if readline library is used. |
2608 | ||
2609 | proc readline_is_used { } { | |
2610 | global gdb_prompt | |
2611 | ||
2612 | gdb_test_multiple "show editing" "" { | |
2613 | -re ".*Editing of command lines as they are typed is on\..*$gdb_prompt $" { | |
2614 | return 1 | |
2615 | } | |
2616 | -re ".*$gdb_prompt $" { | |
2617 | return 0 | |
2618 | } | |
2619 | } | |
2620 | } | |
2621 | ||
e9f0e62e NB |
2622 | # Return 1 if target is ELF. |
2623 | gdb_caching_proc is_elf_target { | |
2624 | set me "is_elf_target" | |
2625 | ||
bf326452 AH |
2626 | set src { int foo () {return 0;} } |
2627 | if {![gdb_simple_compile elf_target $src]} { | |
2628 | return 0 | |
e9f0e62e NB |
2629 | } |
2630 | ||
2631 | set fp_obj [open $obj "r"] | |
2632 | fconfigure $fp_obj -translation binary | |
2633 | set data [read $fp_obj] | |
2634 | close $fp_obj | |
2635 | ||
2636 | file delete $obj | |
2637 | ||
2638 | set ELFMAG "\u007FELF" | |
2639 | ||
2640 | if {[string compare -length 4 $data $ELFMAG] != 0} { | |
2641 | verbose "$me: returning 0" 2 | |
2642 | return 0 | |
2643 | } | |
2644 | ||
2645 | verbose "$me: returning 1" 2 | |
2646 | return 1 | |
2647 | } | |
2648 | ||
20c6f1e1 YQ |
2649 | # Return 1 if the memory at address zero is readable. |
2650 | ||
2651 | gdb_caching_proc is_address_zero_readable { | |
2652 | global gdb_prompt | |
2653 | ||
2654 | set ret 0 | |
2655 | gdb_test_multiple "x 0" "" { | |
2656 | -re "Cannot access memory at address 0x0.*$gdb_prompt $" { | |
2657 | set ret 0 | |
2658 | } | |
2659 | -re ".*$gdb_prompt $" { | |
2660 | set ret 1 | |
2661 | } | |
2662 | } | |
2663 | ||
2664 | return $ret | |
2665 | } | |
2666 | ||
6dbb6798 YQ |
2667 | # Produce source file NAME and write SOURCES into it. |
2668 | ||
2669 | proc gdb_produce_source { name sources } { | |
2670 | set index 0 | |
2671 | set f [open $name "w"] | |
2672 | ||
2673 | puts $f $sources | |
2674 | close $f | |
2675 | } | |
2676 | ||
add265ae L |
2677 | # Return 1 if target is ILP32. |
2678 | # This cannot be decided simply from looking at the target string, | |
2679 | # as it might depend on externally passed compiler options like -m64. | |
17e1c970 | 2680 | gdb_caching_proc is_ilp32_target { |
c221b2f7 | 2681 | return [gdb_can_simple_compile is_ilp32_target { |
11ec5965 YQ |
2682 | int dummy[sizeof (int) == 4 |
2683 | && sizeof (void *) == 4 | |
2684 | && sizeof (long) == 4 ? 1 : -1]; | |
c221b2f7 | 2685 | }] |
add265ae L |
2686 | } |
2687 | ||
2688 | # Return 1 if target is LP64. | |
2689 | # This cannot be decided simply from looking at the target string, | |
2690 | # as it might depend on externally passed compiler options like -m64. | |
17e1c970 | 2691 | gdb_caching_proc is_lp64_target { |
c221b2f7 | 2692 | return [gdb_can_simple_compile is_lp64_target { |
11ec5965 YQ |
2693 | int dummy[sizeof (int) == 4 |
2694 | && sizeof (void *) == 8 | |
2695 | && sizeof (long) == 8 ? 1 : -1]; | |
c221b2f7 | 2696 | }] |
add265ae L |
2697 | } |
2698 | ||
e630b974 TT |
2699 | # Return 1 if target has 64 bit addresses. |
2700 | # This cannot be decided simply from looking at the target string, | |
2701 | # as it might depend on externally passed compiler options like -m64. | |
2702 | gdb_caching_proc is_64_target { | |
c221b2f7 | 2703 | return [gdb_can_simple_compile is_64_target { |
11ec5965 YQ |
2704 | int function(void) { return 3; } |
2705 | int dummy[sizeof (&function) == 8 ? 1 : -1]; | |
c221b2f7 | 2706 | }] |
e630b974 TT |
2707 | } |
2708 | ||
7f062217 JK |
2709 | # Return 1 if target has x86_64 registers - either amd64 or x32. |
2710 | # x32 target identifies as x86_64-*-linux*, therefore it cannot be determined | |
2711 | # just from the target string. | |
17e1c970 | 2712 | gdb_caching_proc is_amd64_regs_target { |
68fb0ec0 | 2713 | if {![istarget "x86_64-*-*"] && ![istarget "i?86-*"]} { |
7f062217 JK |
2714 | return 0 |
2715 | } | |
2716 | ||
224d30d3 MM |
2717 | return [gdb_can_simple_compile is_amd64_regs_target { |
2718 | int main (void) { | |
2719 | asm ("incq %rax"); | |
2720 | asm ("incq %r15"); | |
7f062217 | 2721 | |
224d30d3 MM |
2722 | return 0; |
2723 | } | |
2724 | }] | |
7f062217 JK |
2725 | } |
2726 | ||
6edba76f TT |
2727 | # Return 1 if this target is an x86 or x86-64 with -m32. |
2728 | proc is_x86_like_target {} { | |
68fb0ec0 | 2729 | if {![istarget "x86_64-*-*"] && ![istarget i?86-*]} { |
6edba76f TT |
2730 | return 0 |
2731 | } | |
7f062217 | 2732 | return [expr [is_ilp32_target] && ![is_amd64_regs_target]] |
6edba76f TT |
2733 | } |
2734 | ||
9fcf688e YQ |
2735 | # Return 1 if this target is an arm or aarch32 on aarch64. |
2736 | ||
2737 | gdb_caching_proc is_aarch32_target { | |
2738 | if { [istarget "arm*-*-*"] } { | |
2739 | return 1 | |
2740 | } | |
2741 | ||
2742 | if { ![istarget "aarch64*-*-*"] } { | |
2743 | return 0 | |
2744 | } | |
2745 | ||
9fcf688e YQ |
2746 | set list {} |
2747 | foreach reg \ | |
2748 | {r0 r1 r2 r3} { | |
2749 | lappend list "\tmov $reg, $reg" | |
2750 | } | |
9fcf688e | 2751 | |
c221b2f7 | 2752 | return [gdb_can_simple_compile aarch32 [join $list \n]] |
9fcf688e YQ |
2753 | } |
2754 | ||
4931af25 YQ |
2755 | # Return 1 if this target is an aarch64, either lp64 or ilp32. |
2756 | ||
2757 | proc is_aarch64_target {} { | |
2758 | if { ![istarget "aarch64*-*-*"] } { | |
2759 | return 0 | |
2760 | } | |
2761 | ||
2762 | return [expr ![is_aarch32_target]] | |
2763 | } | |
2764 | ||
be777e08 YQ |
2765 | # Return 1 if displaced stepping is supported on target, otherwise, return 0. |
2766 | proc support_displaced_stepping {} { | |
2767 | ||
2768 | if { [istarget "x86_64-*-linux*"] || [istarget "i\[34567\]86-*-linux*"] | |
2769 | || [istarget "arm*-*-linux*"] || [istarget "powerpc-*-linux*"] | |
34240514 YQ |
2770 | || [istarget "powerpc64-*-linux*"] || [istarget "s390*-*-*"] |
2771 | || [istarget "aarch64*-*-linux*"] } { | |
be777e08 YQ |
2772 | return 1 |
2773 | } | |
2774 | ||
2775 | return 0 | |
2776 | } | |
2777 | ||
3c95e6af PG |
2778 | # Run a test on the target to see if it supports vmx hardware. Return 0 if so, |
2779 | # 1 if it does not. Based on 'check_vmx_hw_available' from the GCC testsuite. | |
2780 | ||
17e1c970 | 2781 | gdb_caching_proc skip_altivec_tests { |
fda326dd | 2782 | global srcdir subdir gdb_prompt inferior_exited_re |
3c95e6af | 2783 | |
3c95e6af | 2784 | set me "skip_altivec_tests" |
3c95e6af PG |
2785 | |
2786 | # Some simulators are known to not support VMX instructions. | |
2787 | if { [istarget powerpc-*-eabi] || [istarget powerpc*-*-eabispe] } { | |
2788 | verbose "$me: target known to not support VMX, returning 1" 2 | |
17e1c970 | 2789 | return 1 |
3c95e6af PG |
2790 | } |
2791 | ||
2792 | # Make sure we have a compiler that understands altivec. | |
4c93b1db | 2793 | if [get_compiler_info] { |
3c95e6af PG |
2794 | warning "Could not get compiler info" |
2795 | return 1 | |
2796 | } | |
2797 | if [test_compiler_info gcc*] { | |
bf326452 | 2798 | set compile_flags "additional_flags=-maltivec" |
3c95e6af | 2799 | } elseif [test_compiler_info xlc*] { |
bf326452 | 2800 | set compile_flags "additional_flags=-qaltivec" |
3c95e6af PG |
2801 | } else { |
2802 | verbose "Could not compile with altivec support, returning 1" 2 | |
2803 | return 1 | |
2804 | } | |
2805 | ||
bf326452 AH |
2806 | # Compile a test program containing VMX instructions. |
2807 | set src { | |
11ec5965 YQ |
2808 | int main() { |
2809 | #ifdef __MACH__ | |
2810 | asm volatile ("vor v0,v0,v0"); | |
2811 | #else | |
2812 | asm volatile ("vor 0,0,0"); | |
2813 | #endif | |
2814 | return 0; | |
2815 | } | |
2816 | } | |
bf326452 | 2817 | if {![gdb_simple_compile $me $src executable $compile_flags]} { |
17e1c970 | 2818 | return 1 |
3c95e6af PG |
2819 | } |
2820 | ||
bf326452 | 2821 | # Compilation succeeded so now run it via gdb. |
3c95e6af PG |
2822 | |
2823 | gdb_exit | |
2824 | gdb_start | |
2825 | gdb_reinitialize_dir $srcdir/$subdir | |
bf326452 | 2826 | gdb_load "$obj" |
3c95e6af PG |
2827 | gdb_run_cmd |
2828 | gdb_expect { | |
2829 | -re ".*Illegal instruction.*${gdb_prompt} $" { | |
2830 | verbose -log "\n$me altivec hardware not detected" | |
17e1c970 | 2831 | set skip_vmx_tests 1 |
3c95e6af | 2832 | } |
fda326dd | 2833 | -re ".*$inferior_exited_re normally.*${gdb_prompt} $" { |
3c95e6af | 2834 | verbose -log "\n$me: altivec hardware detected" |
17e1c970 | 2835 | set skip_vmx_tests 0 |
3c95e6af PG |
2836 | } |
2837 | default { | |
2838 | warning "\n$me: default case taken" | |
17e1c970 | 2839 | set skip_vmx_tests 1 |
3c95e6af PG |
2840 | } |
2841 | } | |
2842 | gdb_exit | |
bf326452 | 2843 | remote_file build delete $obj |
3c95e6af | 2844 | |
17e1c970 TT |
2845 | verbose "$me: returning $skip_vmx_tests" 2 |
2846 | return $skip_vmx_tests | |
3c95e6af PG |
2847 | } |
2848 | ||
604c2f83 LM |
2849 | # Run a test on the target to see if it supports vmx hardware. Return 0 if so, |
2850 | # 1 if it does not. Based on 'check_vmx_hw_available' from the GCC testsuite. | |
2851 | ||
17e1c970 | 2852 | gdb_caching_proc skip_vsx_tests { |
fda326dd | 2853 | global srcdir subdir gdb_prompt inferior_exited_re |
604c2f83 | 2854 | |
604c2f83 | 2855 | set me "skip_vsx_tests" |
604c2f83 LM |
2856 | |
2857 | # Some simulators are known to not support Altivec instructions, so | |
2858 | # they won't support VSX instructions as well. | |
2859 | if { [istarget powerpc-*-eabi] || [istarget powerpc*-*-eabispe] } { | |
2860 | verbose "$me: target known to not support VSX, returning 1" 2 | |
17e1c970 | 2861 | return 1 |
604c2f83 LM |
2862 | } |
2863 | ||
2864 | # Make sure we have a compiler that understands altivec. | |
4c93b1db | 2865 | if [get_compiler_info] { |
604c2f83 LM |
2866 | warning "Could not get compiler info" |
2867 | return 1 | |
2868 | } | |
2869 | if [test_compiler_info gcc*] { | |
bf326452 | 2870 | set compile_flags "additional_flags=-mvsx" |
604c2f83 | 2871 | } elseif [test_compiler_info xlc*] { |
bf326452 | 2872 | set compile_flags "additional_flags=-qasm=gcc" |
604c2f83 LM |
2873 | } else { |
2874 | verbose "Could not compile with vsx support, returning 1" 2 | |
2875 | return 1 | |
2876 | } | |
2877 | ||
bf326452 AH |
2878 | # Compile a test program containing VSX instructions. |
2879 | set src { | |
11ec5965 YQ |
2880 | int main() { |
2881 | double a[2] = { 1.0, 2.0 }; | |
2882 | #ifdef __MACH__ | |
2883 | asm volatile ("lxvd2x v0,v0,%[addr]" : : [addr] "r" (a)); | |
2884 | #else | |
2885 | asm volatile ("lxvd2x 0,0,%[addr]" : : [addr] "r" (a)); | |
2886 | #endif | |
2887 | return 0; | |
2888 | } | |
2889 | } | |
bf326452 | 2890 | if {![gdb_simple_compile $me $src executable $compile_flags]} { |
17e1c970 | 2891 | return 1 |
604c2f83 LM |
2892 | } |
2893 | ||
2894 | # No error message, compilation succeeded so now run it via gdb. | |
2895 | ||
2896 | gdb_exit | |
2897 | gdb_start | |
2898 | gdb_reinitialize_dir $srcdir/$subdir | |
bf326452 | 2899 | gdb_load "$obj" |
604c2f83 LM |
2900 | gdb_run_cmd |
2901 | gdb_expect { | |
2902 | -re ".*Illegal instruction.*${gdb_prompt} $" { | |
2903 | verbose -log "\n$me VSX hardware not detected" | |
17e1c970 | 2904 | set skip_vsx_tests 1 |
604c2f83 | 2905 | } |
fda326dd | 2906 | -re ".*$inferior_exited_re normally.*${gdb_prompt} $" { |
604c2f83 | 2907 | verbose -log "\n$me: VSX hardware detected" |
17e1c970 | 2908 | set skip_vsx_tests 0 |
604c2f83 LM |
2909 | } |
2910 | default { | |
2911 | warning "\n$me: default case taken" | |
17e1c970 | 2912 | set skip_vsx_tests 1 |
604c2f83 LM |
2913 | } |
2914 | } | |
2915 | gdb_exit | |
bf326452 | 2916 | remote_file build delete $obj |
604c2f83 | 2917 | |
17e1c970 TT |
2918 | verbose "$me: returning $skip_vsx_tests" 2 |
2919 | return $skip_vsx_tests | |
604c2f83 LM |
2920 | } |
2921 | ||
da8c46d2 MM |
2922 | # Run a test on the target to see if it supports TSX hardware. Return 0 if so, |
2923 | # 1 if it does not. Based on 'check_vmx_hw_available' from the GCC testsuite. | |
2924 | ||
2925 | gdb_caching_proc skip_tsx_tests { | |
2926 | global srcdir subdir gdb_prompt inferior_exited_re | |
2927 | ||
2928 | set me "skip_tsx_tests" | |
2929 | ||
bf326452 AH |
2930 | # Compile a test program. |
2931 | set src { | |
2932 | int main() { | |
2933 | asm volatile ("xbegin .L0"); | |
2934 | asm volatile ("xend"); | |
2935 | asm volatile (".L0: nop"); | |
2936 | return 0; | |
2937 | } | |
da8c46d2 | 2938 | } |
bf326452 | 2939 | if {![gdb_simple_compile $me $src executable]} { |
da8c46d2 MM |
2940 | return 1 |
2941 | } | |
2942 | ||
2943 | # No error message, compilation succeeded so now run it via gdb. | |
2944 | ||
2945 | gdb_exit | |
2946 | gdb_start | |
2947 | gdb_reinitialize_dir $srcdir/$subdir | |
bf326452 | 2948 | gdb_load "$obj" |
da8c46d2 MM |
2949 | gdb_run_cmd |
2950 | gdb_expect { | |
2951 | -re ".*Illegal instruction.*${gdb_prompt} $" { | |
2952 | verbose -log "$me: TSX hardware not detected." | |
2953 | set skip_tsx_tests 1 | |
2954 | } | |
2955 | -re ".*$inferior_exited_re normally.*${gdb_prompt} $" { | |
2956 | verbose -log "$me: TSX hardware detected." | |
2957 | set skip_tsx_tests 0 | |
2958 | } | |
2959 | default { | |
2960 | warning "\n$me: default case taken." | |
2961 | set skip_tsx_tests 1 | |
2962 | } | |
2963 | } | |
2964 | gdb_exit | |
bf326452 | 2965 | remote_file build delete $obj |
da8c46d2 MM |
2966 | |
2967 | verbose "$me: returning $skip_tsx_tests" 2 | |
2968 | return $skip_tsx_tests | |
2969 | } | |
2970 | ||
2f1d9bdd MM |
2971 | # Run a test on the target to see if it supports btrace hardware. Return 0 if so, |
2972 | # 1 if it does not. Based on 'check_vmx_hw_available' from the GCC testsuite. | |
2973 | ||
f3a76454 | 2974 | gdb_caching_proc skip_btrace_tests { |
2f1d9bdd MM |
2975 | global srcdir subdir gdb_prompt inferior_exited_re |
2976 | ||
2f1d9bdd | 2977 | set me "skip_btrace_tests" |
2f1d9bdd MM |
2978 | if { ![istarget "i?86-*-*"] && ![istarget "x86_64-*-*"] } { |
2979 | verbose "$me: target does not support btrace, returning 1" 2 | |
f3a76454 | 2980 | return 1 |
2f1d9bdd MM |
2981 | } |
2982 | ||
bf326452 AH |
2983 | # Compile a test program. |
2984 | set src { int main() { return 0; } } | |
2985 | if {![gdb_simple_compile $me $src executable]} { | |
dcdec678 | 2986 | return 1 |
2f1d9bdd MM |
2987 | } |
2988 | ||
2989 | # No error message, compilation succeeded so now run it via gdb. | |
2990 | ||
f3a76454 TT |
2991 | gdb_exit |
2992 | gdb_start | |
2993 | gdb_reinitialize_dir $srcdir/$subdir | |
bf326452 | 2994 | gdb_load $obj |
2f1d9bdd | 2995 | if ![runto_main] { |
f3a76454 | 2996 | return 1 |
2f1d9bdd MM |
2997 | } |
2998 | # In case of an unexpected output, we return 2 as a fail value. | |
f3a76454 | 2999 | set skip_btrace_tests 2 |
2f1d9bdd MM |
3000 | gdb_test_multiple "record btrace" "check btrace support" { |
3001 | -re "You can't do that when your target is.*\r\n$gdb_prompt $" { | |
f3a76454 | 3002 | set skip_btrace_tests 1 |
2f1d9bdd MM |
3003 | } |
3004 | -re "Target does not support branch tracing.*\r\n$gdb_prompt $" { | |
f3a76454 | 3005 | set skip_btrace_tests 1 |
2f1d9bdd MM |
3006 | } |
3007 | -re "Could not enable branch tracing.*\r\n$gdb_prompt $" { | |
f3a76454 | 3008 | set skip_btrace_tests 1 |
2f1d9bdd MM |
3009 | } |
3010 | -re "^record btrace\r\n$gdb_prompt $" { | |
f3a76454 | 3011 | set skip_btrace_tests 0 |
2f1d9bdd MM |
3012 | } |
3013 | } | |
3014 | gdb_exit | |
bf326452 | 3015 | remote_file build delete $obj |
2f1d9bdd | 3016 | |
f3a76454 TT |
3017 | verbose "$me: returning $skip_btrace_tests" 2 |
3018 | return $skip_btrace_tests | |
2f1d9bdd MM |
3019 | } |
3020 | ||
da8c46d2 MM |
3021 | # Run a test on the target to see if it supports btrace pt hardware. |
3022 | # Return 0 if so, 1 if it does not. Based on 'check_vmx_hw_available' | |
3023 | # from the GCC testsuite. | |
3024 | ||
3025 | gdb_caching_proc skip_btrace_pt_tests { | |
3026 | global srcdir subdir gdb_prompt inferior_exited_re | |
3027 | ||
3028 | set me "skip_btrace_tests" | |
3029 | if { ![istarget "i?86-*-*"] && ![istarget "x86_64-*-*"] } { | |
3030 | verbose "$me: target does not support btrace, returning 1" 2 | |
3031 | return 1 | |
3032 | } | |
3033 | ||
bf326452 AH |
3034 | # Compile a test program. |
3035 | set src { int main() { return 0; } } | |
3036 | if {![gdb_simple_compile $me $src executable]} { | |
dcdec678 | 3037 | return 1 |
da8c46d2 MM |
3038 | } |
3039 | ||
3040 | # No error message, compilation succeeded so now run it via gdb. | |
3041 | ||
3042 | gdb_exit | |
3043 | gdb_start | |
3044 | gdb_reinitialize_dir $srcdir/$subdir | |
bf326452 | 3045 | gdb_load $obj |
da8c46d2 | 3046 | if ![runto_main] { |
da8c46d2 MM |
3047 | return 1 |
3048 | } | |
da8c46d2 MM |
3049 | # In case of an unexpected output, we return 2 as a fail value. |
3050 | set skip_btrace_tests 2 | |
c4e12631 | 3051 | gdb_test_multiple "record btrace pt" "check btrace pt support" { |
da8c46d2 MM |
3052 | -re "You can't do that when your target is.*\r\n$gdb_prompt $" { |
3053 | set skip_btrace_tests 1 | |
3054 | } | |
3055 | -re "Target does not support branch tracing.*\r\n$gdb_prompt $" { | |
3056 | set skip_btrace_tests 1 | |
3057 | } | |
3058 | -re "Could not enable branch tracing.*\r\n$gdb_prompt $" { | |
3059 | set skip_btrace_tests 1 | |
3060 | } | |
c4e12631 | 3061 | -re "support was disabled at compile time.*\r\n$gdb_prompt $" { |
46a3515b MM |
3062 | set skip_btrace_tests 1 |
3063 | } | |
da8c46d2 MM |
3064 | -re "^record btrace pt\r\n$gdb_prompt $" { |
3065 | set skip_btrace_tests 0 | |
3066 | } | |
3067 | } | |
3068 | gdb_exit | |
bf326452 | 3069 | remote_file build delete $obj |
da8c46d2 MM |
3070 | |
3071 | verbose "$me: returning $skip_btrace_tests" 2 | |
3072 | return $skip_btrace_tests | |
3073 | } | |
3074 | ||
6bb8890e AH |
3075 | # Run a test on the target to see if it supports Aarch64 SVE hardware. |
3076 | # Return 0 if so, 1 if it does not. Note this causes a restart of GDB. | |
3077 | ||
3078 | gdb_caching_proc skip_aarch64_sve_tests { | |
3079 | global srcdir subdir gdb_prompt inferior_exited_re | |
3080 | ||
3081 | set me "skip_aarch64_sve_tests" | |
3082 | ||
3083 | if { ![is_aarch64_target]} { | |
3084 | return 1 | |
3085 | } | |
3086 | ||
3087 | set compile_flags "{additional_flags=-march=armv8-a+sve}" | |
3088 | ||
3089 | # Compile a test program containing SVE instructions. | |
3090 | set src { | |
3091 | int main() { | |
3092 | asm volatile ("ptrue p0.b"); | |
3093 | return 0; | |
3094 | } | |
3095 | } | |
3096 | if {![gdb_simple_compile $me $src executable $compile_flags]} { | |
3097 | return 1 | |
3098 | } | |
3099 | ||
3100 | # Compilation succeeded so now run it via gdb. | |
3101 | clean_restart $obj | |
3102 | gdb_run_cmd | |
3103 | gdb_expect { | |
3104 | -re ".*Illegal instruction.*${gdb_prompt} $" { | |
3105 | verbose -log "\n$me sve hardware not detected" | |
3106 | set skip_sve_tests 1 | |
3107 | } | |
3108 | -re ".*$inferior_exited_re normally.*${gdb_prompt} $" { | |
3109 | verbose -log "\n$me: sve hardware detected" | |
3110 | set skip_sve_tests 0 | |
3111 | } | |
3112 | default { | |
3113 | warning "\n$me: default case taken" | |
3114 | set skip_sve_tests 1 | |
3115 | } | |
3116 | } | |
3117 | gdb_exit | |
3118 | remote_file build delete $obj | |
3119 | ||
3120 | verbose "$me: returning $skip_sve_tests" 2 | |
3121 | return $skip_sve_tests | |
3122 | } | |
3123 | ||
3124 | ||
007e1530 TT |
3125 | # A helper that compiles a test case to see if __int128 is supported. |
3126 | proc gdb_int128_helper {lang} { | |
c221b2f7 | 3127 | return [gdb_can_simple_compile "i128-for-$lang" { |
007e1530 TT |
3128 | __int128 x; |
3129 | int main() { return 0; } | |
c221b2f7 | 3130 | } executable $lang] |
007e1530 TT |
3131 | } |
3132 | ||
3133 | # Return true if the C compiler understands the __int128 type. | |
3134 | gdb_caching_proc has_int128_c { | |
3135 | return [gdb_int128_helper c] | |
3136 | } | |
3137 | ||
3138 | # Return true if the C++ compiler understands the __int128 type. | |
3139 | gdb_caching_proc has_int128_cxx { | |
3140 | return [gdb_int128_helper c++] | |
3141 | } | |
3142 | ||
ca98345e SL |
3143 | # Return true if the IFUNC feature is unsupported. |
3144 | gdb_caching_proc skip_ifunc_tests { | |
3145 | if [gdb_can_simple_compile ifunc { | |
3146 | extern void f_ (); | |
3147 | typedef void F (void); | |
3148 | F* g (void) { return &f_; } | |
3149 | void f () __attribute__ ((ifunc ("g"))); | |
3150 | } object] { | |
3151 | return 0 | |
3152 | } else { | |
3153 | return 1 | |
3154 | } | |
3155 | } | |
3156 | ||
edb3359d DJ |
3157 | # Return whether we should skip tests for showing inlined functions in |
3158 | # backtraces. Requires get_compiler_info and get_debug_format. | |
3159 | ||
3160 | proc skip_inline_frame_tests {} { | |
3161 | # GDB only recognizes inlining information in DWARF 2 (DWARF 3). | |
3162 | if { ! [test_debug_format "DWARF 2"] } { | |
3163 | return 1 | |
3164 | } | |
3165 | ||
3166 | # GCC before 4.1 does not emit DW_AT_call_file / DW_AT_call_line. | |
3167 | if { ([test_compiler_info "gcc-2-*"] | |
3168 | || [test_compiler_info "gcc-3-*"] | |
3169 | || [test_compiler_info "gcc-4-0-*"]) } { | |
3170 | return 1 | |
3171 | } | |
3172 | ||
3173 | return 0 | |
3174 | } | |
3175 | ||
3176 | # Return whether we should skip tests for showing variables from | |
3177 | # inlined functions. Requires get_compiler_info and get_debug_format. | |
3178 | ||
3179 | proc skip_inline_var_tests {} { | |
3180 | # GDB only recognizes inlining information in DWARF 2 (DWARF 3). | |
3181 | if { ! [test_debug_format "DWARF 2"] } { | |
3182 | return 1 | |
3183 | } | |
3184 | ||
3185 | return 0 | |
3186 | } | |
3187 | ||
b800ec70 UW |
3188 | # Return a 1 if we should skip tests that require hardware breakpoints |
3189 | ||
3190 | proc skip_hw_breakpoint_tests {} { | |
3191 | # Skip tests if requested by the board (note that no_hardware_watchpoints | |
3192 | # disables both watchpoints and breakpoints) | |
3193 | if { [target_info exists gdb,no_hardware_watchpoints]} { | |
3194 | return 1 | |
3195 | } | |
3196 | ||
3197 | # These targets support hardware breakpoints natively | |
3198 | if { [istarget "i?86-*-*"] | |
3199 | || [istarget "x86_64-*-*"] | |
e3039479 | 3200 | || [istarget "ia64-*-*"] |
52042a00 | 3201 | || [istarget "arm*-*-*"] |
8193adea AA |
3202 | || [istarget "aarch64*-*-*"] |
3203 | || [istarget "s390*-*-*"] } { | |
b800ec70 UW |
3204 | return 0 |
3205 | } | |
3206 | ||
3207 | return 1 | |
3208 | } | |
3209 | ||
3210 | # Return a 1 if we should skip tests that require hardware watchpoints | |
3211 | ||
3212 | proc skip_hw_watchpoint_tests {} { | |
3213 | # Skip tests if requested by the board | |
3214 | if { [target_info exists gdb,no_hardware_watchpoints]} { | |
3215 | return 1 | |
3216 | } | |
3217 | ||
3218 | # These targets support hardware watchpoints natively | |
3219 | if { [istarget "i?86-*-*"] | |
3220 | || [istarget "x86_64-*-*"] | |
3221 | || [istarget "ia64-*-*"] | |
e3039479 | 3222 | || [istarget "arm*-*-*"] |
52042a00 | 3223 | || [istarget "aarch64*-*-*"] |
b800ec70 UW |
3224 | || [istarget "powerpc*-*-linux*"] |
3225 | || [istarget "s390*-*-*"] } { | |
3226 | return 0 | |
3227 | } | |
3228 | ||
3229 | return 1 | |
3230 | } | |
3231 | ||
3232 | # Return a 1 if we should skip tests that require *multiple* hardware | |
3233 | # watchpoints to be active at the same time | |
3234 | ||
3235 | proc skip_hw_watchpoint_multi_tests {} { | |
3236 | if { [skip_hw_watchpoint_tests] } { | |
3237 | return 1 | |
3238 | } | |
3239 | ||
3240 | # These targets support just a single hardware watchpoint | |
e3039479 UW |
3241 | if { [istarget "arm*-*-*"] |
3242 | || [istarget "powerpc*-*-linux*"] } { | |
b800ec70 UW |
3243 | return 1 |
3244 | } | |
3245 | ||
3246 | return 0 | |
3247 | } | |
3248 | ||
3249 | # Return a 1 if we should skip tests that require read/access watchpoints | |
3250 | ||
3251 | proc skip_hw_watchpoint_access_tests {} { | |
3252 | if { [skip_hw_watchpoint_tests] } { | |
3253 | return 1 | |
3254 | } | |
3255 | ||
3256 | # These targets support just write watchpoints | |
3257 | if { [istarget "s390*-*-*"] } { | |
3258 | return 1 | |
3259 | } | |
3260 | ||
3261 | return 0 | |
3262 | } | |
3263 | ||
b4893d48 TT |
3264 | # Return 1 if we should skip tests that require the runtime unwinder |
3265 | # hook. This must be invoked while gdb is running, after shared | |
3266 | # libraries have been loaded. This is needed because otherwise a | |
3267 | # shared libgcc won't be visible. | |
3268 | ||
3269 | proc skip_unwinder_tests {} { | |
3270 | global gdb_prompt | |
3271 | ||
4442ada7 | 3272 | set ok 0 |
b4893d48 TT |
3273 | gdb_test_multiple "print _Unwind_DebugHook" "check for unwinder hook" { |
3274 | -re "= .*no debug info.*_Unwind_DebugHook.*\r\n$gdb_prompt $" { | |
b4893d48 TT |
3275 | } |
3276 | -re "= .*_Unwind_DebugHook.*\r\n$gdb_prompt $" { | |
4442ada7 | 3277 | set ok 1 |
b4893d48 TT |
3278 | } |
3279 | -re "No symbol .* in current context.\r\n$gdb_prompt $" { | |
b4893d48 TT |
3280 | } |
3281 | } | |
3282 | if {!$ok} { | |
3283 | gdb_test_multiple "info probe" "check for stap probe in unwinder" { | |
3284 | -re ".*libgcc.*unwind.*\r\n$gdb_prompt $" { | |
b4893d48 TT |
3285 | set ok 1 |
3286 | } | |
3287 | -re "\r\n$gdb_prompt $" { | |
3288 | } | |
3289 | } | |
3290 | } | |
3291 | return $ok | |
3292 | } | |
3293 | ||
b694989f | 3294 | # Return 1 if we should skip tests that require the libstdc++ stap |
72f1fe8a | 3295 | # probes. This must be invoked while gdb is running, after shared |
297989a1 | 3296 | # libraries have been loaded. PROMPT_REGEXP is the expected prompt. |
72f1fe8a | 3297 | |
297989a1 | 3298 | proc skip_libstdcxx_probe_tests_prompt { prompt_regexp } { |
b694989f | 3299 | set supported 0 |
590003dc TV |
3300 | gdb_test_multiple "info probe" "check for stap probe in libstdc++" \ |
3301 | -prompt "$prompt_regexp" { | |
3302 | -re ".*libstdcxx.*catch.*\r\n$prompt_regexp" { | |
3303 | set supported 1 | |
3304 | } | |
3305 | -re "\r\n$prompt_regexp" { | |
3306 | } | |
72f1fe8a | 3307 | } |
b694989f TV |
3308 | set skip [expr !$supported] |
3309 | return $skip | |
72f1fe8a TT |
3310 | } |
3311 | ||
297989a1 TV |
3312 | # As skip_libstdcxx_probe_tests_prompt, with gdb_prompt. |
3313 | ||
3314 | proc skip_libstdcxx_probe_tests {} { | |
3315 | global gdb_prompt | |
3316 | return [skip_libstdcxx_probe_tests_prompt "$gdb_prompt $"] | |
3317 | } | |
3318 | ||
bb2ec1b3 TT |
3319 | # Return 1 if we should skip tests of the "compile" feature. |
3320 | # This must be invoked after the inferior has been started. | |
3321 | ||
3322 | proc skip_compile_feature_tests {} { | |
3323 | global gdb_prompt | |
3324 | ||
3325 | set result 0 | |
3326 | gdb_test_multiple "compile code -- ;" "check for working compile command" { | |
3327 | "Could not load libcc1.*\r\n$gdb_prompt $" { | |
3328 | set result 1 | |
3329 | } | |
1bc1068a JK |
3330 | -re "Command not supported on this host\\..*\r\n$gdb_prompt $" { |
3331 | set result 1 | |
3332 | } | |
bb2ec1b3 TT |
3333 | -re "\r\n$gdb_prompt $" { |
3334 | } | |
3335 | } | |
3336 | return $result | |
3337 | } | |
3338 | ||
3275ef47 SM |
3339 | # Helper for gdb_is_target_* procs. TARGET_NAME is the name of the target |
3340 | # we're looking for (used to build the test name). TARGET_STACK_REGEXP | |
3341 | # is a regexp that will match the output of "maint print target-stack" if | |
3083294d SM |
3342 | # the target in question is currently pushed. PROMPT_REGEXP is a regexp |
3343 | # matching the expected prompt after the command output. | |
076855f9 | 3344 | |
3083294d | 3345 | proc gdb_is_target_1 { target_name target_stack_regexp prompt_regexp } { |
3275ef47 | 3346 | set test "probe for target ${target_name}" |
590003dc TV |
3347 | gdb_test_multiple "maint print target-stack" $test \ |
3348 | -prompt "$prompt_regexp" { | |
3349 | -re "${target_stack_regexp}${prompt_regexp}" { | |
3350 | pass $test | |
3351 | return 1 | |
3352 | } | |
3353 | -re "$prompt_regexp" { | |
3354 | pass $test | |
3355 | } | |
076855f9 | 3356 | } |
076855f9 PA |
3357 | return 0 |
3358 | } | |
3359 | ||
3083294d SM |
3360 | # Helper for gdb_is_target_remote where the expected prompt is variable. |
3361 | ||
3362 | proc gdb_is_target_remote_prompt { prompt_regexp } { | |
3363 | return [gdb_is_target_1 "remote" ".*emote serial target in gdb-specific protocol.*" $prompt_regexp] | |
3364 | } | |
3365 | ||
f015c27b PA |
3366 | # Check whether we're testing with the remote or extended-remote |
3367 | # targets. | |
3368 | ||
3275ef47 | 3369 | proc gdb_is_target_remote { } { |
3083294d SM |
3370 | global gdb_prompt |
3371 | ||
3372 | return [gdb_is_target_remote_prompt "$gdb_prompt $"] | |
3275ef47 SM |
3373 | } |
3374 | ||
3375 | # Check whether we're testing with the native target. | |
f015c27b | 3376 | |
3275ef47 | 3377 | proc gdb_is_target_native { } { |
3083294d SM |
3378 | global gdb_prompt |
3379 | ||
3380 | return [gdb_is_target_1 "native" ".*native \\(Native process\\).*" "$gdb_prompt $"] | |
f015c27b PA |
3381 | } |
3382 | ||
8929ad8b SM |
3383 | # Return the effective value of use_gdb_stub. |
3384 | # | |
3385 | # If the use_gdb_stub global has been set (it is set when the gdb process is | |
3386 | # spawned), return that. Otherwise, return the value of the use_gdb_stub | |
3387 | # property from the board file. | |
3388 | # | |
3389 | # This is the preferred way of checking use_gdb_stub, since it allows to check | |
3390 | # the value before the gdb has been spawned and it will return the correct value | |
3391 | # even when it was overriden by the test. | |
3392 | ||
3393 | proc use_gdb_stub {} { | |
3394 | global use_gdb_stub | |
3395 | ||
3396 | if [info exists use_gdb_stub] { | |
3397 | return $use_gdb_stub | |
3398 | } | |
3399 | ||
3400 | return [target_info exists use_gdb_stub] | |
3401 | } | |
3402 | ||
0a46d518 SM |
3403 | # Return 1 if the current remote target is an instance of our GDBserver, 0 |
3404 | # otherwise. Return -1 if there was an error and we can't tell. | |
3405 | ||
3406 | gdb_caching_proc target_is_gdbserver { | |
3407 | global gdb_prompt | |
3408 | ||
3409 | set is_gdbserver -1 | |
bc6c7af4 | 3410 | set test "probing for GDBserver" |
0a46d518 SM |
3411 | |
3412 | gdb_test_multiple "monitor help" $test { | |
3413 | -re "The following monitor commands are supported.*Quit GDBserver.*$gdb_prompt $" { | |
3414 | set is_gdbserver 1 | |
3415 | } | |
3416 | -re "$gdb_prompt $" { | |
3417 | set is_gdbserver 0 | |
3418 | } | |
3419 | } | |
3420 | ||
3421 | if { $is_gdbserver == -1 } { | |
3422 | verbose -log "Unable to tell whether we are using GDBserver or not." | |
3423 | } | |
3424 | ||
3425 | return $is_gdbserver | |
3426 | } | |
3427 | ||
a97b16b8 DE |
3428 | # N.B. compiler_info is intended to be local to this file. |
3429 | # Call test_compiler_info with no arguments to fetch its value. | |
3430 | # Yes, this is counterintuitive when there's get_compiler_info, | |
3431 | # but that's the current API. | |
3432 | if [info exists compiler_info] { | |
3433 | unset compiler_info | |
3434 | } | |
3435 | ||
94b8e876 | 3436 | set gcc_compiled 0 |
94b8e876 MC |
3437 | |
3438 | # Figure out what compiler I am using. | |
a97b16b8 | 3439 | # The result is cached so only the first invocation runs the compiler. |
94b8e876 | 3440 | # |
4c93b1db | 3441 | # ARG can be empty or "C++". If empty, "C" is assumed. |
94b8e876 MC |
3442 | # |
3443 | # There are several ways to do this, with various problems. | |
3444 | # | |
3445 | # [ gdb_compile -E $ifile -o $binfile.ci ] | |
3446 | # source $binfile.ci | |
3447 | # | |
3448 | # Single Unix Spec v3 says that "-E -o ..." together are not | |
3449 | # specified. And in fact, the native compiler on hp-ux 11 (among | |
3450 | # others) does not work with "-E -o ...". Most targets used to do | |
3451 | # this, and it mostly worked, because it works with gcc. | |
3452 | # | |
3453 | # [ catch "exec $compiler -E $ifile > $binfile.ci" exec_output ] | |
3454 | # source $binfile.ci | |
3455 | # | |
3456 | # This avoids the problem with -E and -o together. This almost works | |
3457 | # if the build machine is the same as the host machine, which is | |
3458 | # usually true of the targets which are not gcc. But this code does | |
3459 | # not figure which compiler to call, and it always ends up using the C | |
3831839c PA |
3460 | # compiler. Not good for setting hp_aCC_compiler. Target |
3461 | # hppa*-*-hpux* used to do this. | |
94b8e876 MC |
3462 | # |
3463 | # [ gdb_compile -E $ifile > $binfile.ci ] | |
3464 | # source $binfile.ci | |
3465 | # | |
3466 | # dejagnu target_compile says that it supports output redirection, | |
3467 | # but the code is completely different from the normal path and I | |
3468 | # don't want to sweep the mines from that path. So I didn't even try | |
3469 | # this. | |
3470 | # | |
3471 | # set cppout [ gdb_compile $ifile "" preprocess $args quiet ] | |
3472 | # eval $cppout | |
3473 | # | |
3474 | # I actually do this for all targets now. gdb_compile runs the right | |
3475 | # compiler, and TCL captures the output, and I eval the output. | |
3476 | # | |
3477 | # Unfortunately, expect logs the output of the command as it goes by, | |
3478 | # and dejagnu helpfully prints a second copy of it right afterwards. | |
3479 | # So I turn off expect logging for a moment. | |
3480 | # | |
3481 | # [ gdb_compile $ifile $ciexe_file executable $args ] | |
3482 | # [ remote_exec $ciexe_file ] | |
3483 | # [ source $ci_file.out ] | |
3484 | # | |
3485 | # I could give up on -E and just do this. | |
3486 | # I didn't get desperate enough to try this. | |
3487 | # | |
3488 | # -- chastain 2004-01-06 | |
853d6e5b | 3489 | |
4c93b1db | 3490 | proc get_compiler_info {{arg ""}} { |
94b8e876 | 3491 | # For compiler.c and compiler.cc |
c906108c | 3492 | global srcdir |
94b8e876 MC |
3493 | |
3494 | # I am going to play with the log to keep noise out. | |
3495 | global outdir | |
3496 | global tool | |
3497 | ||
3498 | # These come from compiler.c or compiler.cc | |
853d6e5b | 3499 | global compiler_info |
4f70a4c9 MC |
3500 | |
3501 | # Legacy global data symbols. | |
94b8e876 | 3502 | global gcc_compiled |
c906108c | 3503 | |
a97b16b8 DE |
3504 | if [info exists compiler_info] { |
3505 | # Already computed. | |
3506 | return 0 | |
3507 | } | |
3508 | ||
94b8e876 MC |
3509 | # Choose which file to preprocess. |
3510 | set ifile "${srcdir}/lib/compiler.c" | |
4c93b1db | 3511 | if { $arg == "c++" } { |
94b8e876 | 3512 | set ifile "${srcdir}/lib/compiler.cc" |
c906108c | 3513 | } |
085dd6e6 | 3514 | |
94b8e876 MC |
3515 | # Run $ifile through the right preprocessor. |
3516 | # Toggle gdb.log to keep the compiler output out of the log. | |
95d7853e | 3517 | set saved_log [log_file -info] |
94b8e876 | 3518 | log_file |
e7f86de9 JM |
3519 | if [is_remote host] { |
3520 | # We have to use -E and -o together, despite the comments | |
3521 | # above, because of how DejaGnu handles remote host testing. | |
3522 | set ppout "$outdir/compiler.i" | |
fc65c7db | 3523 | gdb_compile "${ifile}" "$ppout" preprocess [list "$arg" quiet getting_compiler_info] |
e7f86de9 JM |
3524 | set file [open $ppout r] |
3525 | set cppout [read $file] | |
3526 | close $file | |
3527 | } else { | |
fc65c7db | 3528 | set cppout [ gdb_compile "${ifile}" "" preprocess [list "$arg" quiet getting_compiler_info] ] |
e7f86de9 | 3529 | } |
95d7853e | 3530 | eval log_file $saved_log |
94b8e876 | 3531 | |
4f70a4c9 MC |
3532 | # Eval the output. |
3533 | set unknown 0 | |
94b8e876 | 3534 | foreach cppline [ split "$cppout" "\n" ] { |
4f70a4c9 MC |
3535 | if { [ regexp "^#" "$cppline" ] } { |
3536 | # line marker | |
3537 | } elseif { [ regexp "^\[\n\r\t \]*$" "$cppline" ] } { | |
3538 | # blank line | |
3539 | } elseif { [ regexp "^\[\n\r\t \]*set\[\n\r\t \]" "$cppline" ] } { | |
3540 | # eval this line | |
3541 | verbose "get_compiler_info: $cppline" 2 | |
3542 | eval "$cppline" | |
3543 | } else { | |
3544 | # unknown line | |
3545 | verbose -log "get_compiler_info: $cppline" | |
3546 | set unknown 1 | |
94b8e876 | 3547 | } |
085dd6e6 | 3548 | } |
4f70a4c9 | 3549 | |
a97b16b8 DE |
3550 | # Set to unknown if for some reason compiler_info didn't get defined. |
3551 | if ![info exists compiler_info] { | |
3552 | verbose -log "get_compiler_info: compiler_info not provided" | |
3553 | set compiler_info "unknown" | |
3554 | } | |
3555 | # Also set to unknown compiler if any diagnostics happened. | |
4f70a4c9 | 3556 | if { $unknown } { |
a97b16b8 | 3557 | verbose -log "get_compiler_info: got unexpected diagnostics" |
4f70a4c9 | 3558 | set compiler_info "unknown" |
4f70a4c9 MC |
3559 | } |
3560 | ||
3561 | # Set the legacy symbols. | |
f90fd8c2 JK |
3562 | set gcc_compiled 0 |
3563 | regexp "^gcc-(\[0-9\]+)-" "$compiler_info" matchall gcc_compiled | |
4f70a4c9 MC |
3564 | |
3565 | # Log what happened. | |
94b8e876 | 3566 | verbose -log "get_compiler_info: $compiler_info" |
085dd6e6 JM |
3567 | |
3568 | # Most compilers will evaluate comparisons and other boolean | |
3569 | # operations to 0 or 1. | |
3570 | uplevel \#0 { set true 1 } | |
3571 | uplevel \#0 { set false 0 } | |
3572 | ||
ae59b1da | 3573 | return 0 |
c906108c SS |
3574 | } |
3575 | ||
a97b16b8 DE |
3576 | # Return the compiler_info string if no arg is provided. |
3577 | # Otherwise the argument is a glob-style expression to match against | |
3578 | # compiler_info. | |
3579 | ||
9b593790 | 3580 | proc test_compiler_info { {compiler ""} } { |
853d6e5b | 3581 | global compiler_info |
a97b16b8 | 3582 | get_compiler_info |
6e87504d | 3583 | |
a97b16b8 DE |
3584 | # If no arg, return the compiler_info string. |
3585 | if [string match "" $compiler] { | |
3586 | return $compiler_info | |
3587 | } | |
6e87504d | 3588 | |
853d6e5b AC |
3589 | return [string match $compiler $compiler_info] |
3590 | } | |
3591 | ||
f6838f81 DJ |
3592 | proc current_target_name { } { |
3593 | global target_info | |
3594 | if [info exists target_info(target,name)] { | |
3595 | set answer $target_info(target,name) | |
3596 | } else { | |
3597 | set answer "" | |
3598 | } | |
3599 | return $answer | |
3600 | } | |
3601 | ||
f1c47eb2 | 3602 | set gdb_wrapper_initialized 0 |
f6838f81 | 3603 | set gdb_wrapper_target "" |
f1c47eb2 MS |
3604 | |
3605 | proc gdb_wrapper_init { args } { | |
4ec70201 PA |
3606 | global gdb_wrapper_initialized |
3607 | global gdb_wrapper_file | |
3608 | global gdb_wrapper_flags | |
f6838f81 | 3609 | global gdb_wrapper_target |
f1c47eb2 | 3610 | |
24ac169a SV |
3611 | # If the wrapper is initialized but the wrapper file cannot be |
3612 | # found anymore, the wrapper file must be built again. | |
3613 | if { $gdb_wrapper_initialized == 1 && \ | |
3614 | [info exists gdb_wrapper_file] && \ | |
3615 | ![file exists $gdb_wrapper_file] } { | |
3616 | verbose "reinitializing the wrapper" | |
3617 | set gdb_wrapper_initialized 0 | |
3618 | } | |
3619 | ||
f1c47eb2 MS |
3620 | if { $gdb_wrapper_initialized == 1 } { return; } |
3621 | ||
3622 | if {[target_info exists needs_status_wrapper] && \ | |
277254ba | 3623 | [target_info needs_status_wrapper] != "0"} { |
24ac169a | 3624 | set result [build_wrapper [standard_output_file "testglue.o"]] |
f1c47eb2 | 3625 | if { $result != "" } { |
4ec70201 PA |
3626 | set gdb_wrapper_file [lindex $result 0] |
3627 | set gdb_wrapper_flags [lindex $result 1] | |
f1c47eb2 MS |
3628 | } else { |
3629 | warning "Status wrapper failed to build." | |
3630 | } | |
3631 | } | |
3632 | set gdb_wrapper_initialized 1 | |
f6838f81 | 3633 | set gdb_wrapper_target [current_target_name] |
f1c47eb2 MS |
3634 | } |
3635 | ||
bf0ec4c2 AA |
3636 | # Determine options that we always want to pass to the compiler. |
3637 | gdb_caching_proc universal_compile_options { | |
3638 | set me "universal_compile_options" | |
3639 | set options {} | |
3640 | ||
3641 | set src [standard_temp_file ccopts[pid].c] | |
3642 | set obj [standard_temp_file ccopts[pid].o] | |
3643 | ||
3644 | gdb_produce_source $src { | |
3645 | int foo(void) { return 0; } | |
3646 | } | |
3647 | ||
3648 | # Try an option for disabling colored diagnostics. Some compilers | |
3649 | # yield colored diagnostics by default (when run from a tty) unless | |
3650 | # such an option is specified. | |
3651 | set opt "additional_flags=-fdiagnostics-color=never" | |
3652 | set lines [target_compile $src $obj object [list "quiet" $opt]] | |
3653 | if [string match "" $lines] then { | |
3654 | # Seems to have worked; use the option. | |
3655 | lappend options $opt | |
3656 | } | |
3657 | file delete $src | |
3658 | file delete $obj | |
3659 | ||
3660 | verbose "$me: returning $options" 2 | |
3661 | return $options | |
3662 | } | |
3663 | ||
c221b2f7 AH |
3664 | # Compile the code in $code to a file based on $name, using the flags |
3665 | # $compile_flag as well as debug, nowarning and quiet. | |
3666 | # Return 1 if code can be compiled | |
bf326452 | 3667 | # Leave the file name of the resulting object in the upvar object. |
c221b2f7 | 3668 | |
bf326452 AH |
3669 | proc gdb_simple_compile {name code {type object} {compile_flags {}} {object obj}} { |
3670 | upvar $object obj | |
c221b2f7 AH |
3671 | |
3672 | switch -regexp -- $type { | |
3673 | "executable" { | |
3674 | set postfix "x" | |
3675 | } | |
3676 | "object" { | |
3677 | set postfix "o" | |
3678 | } | |
3679 | "preprocess" { | |
3680 | set postfix "i" | |
3681 | } | |
3682 | "assembly" { | |
3683 | set postfix "s" | |
3684 | } | |
3685 | } | |
d7445728 TV |
3686 | set ext "c" |
3687 | foreach flag $compile_flags { | |
3688 | if { "$flag" == "go" } { | |
3689 | set ext "go" | |
3690 | break | |
3691 | } | |
3692 | } | |
3693 | set src [standard_temp_file $name-[pid].$ext] | |
c221b2f7 AH |
3694 | set obj [standard_temp_file $name-[pid].$postfix] |
3695 | set compile_flags [concat $compile_flags {debug nowarnings quiet}] | |
3696 | ||
3697 | gdb_produce_source $src $code | |
3698 | ||
3699 | verbose "$name: compiling testfile $src" 2 | |
3700 | set lines [gdb_compile $src $obj $type $compile_flags] | |
3701 | ||
3702 | file delete $src | |
c221b2f7 AH |
3703 | |
3704 | if ![string match "" $lines] then { | |
3705 | verbose "$name: compilation failed, returning 0" 2 | |
3706 | return 0 | |
3707 | } | |
3708 | return 1 | |
3709 | } | |
3710 | ||
bf326452 AH |
3711 | # Compile the code in $code to a file based on $name, using the flags |
3712 | # $compile_flag as well as debug, nowarning and quiet. | |
3713 | # Return 1 if code can be compiled | |
3714 | # Delete all created files and objects. | |
3715 | ||
3716 | proc gdb_can_simple_compile {name code {type object} {compile_flags ""}} { | |
3717 | set ret [gdb_simple_compile $name $code $type $compile_flags temp_obj] | |
3718 | file delete $temp_obj | |
3719 | return $ret | |
3720 | } | |
3721 | ||
f747e0ce PA |
3722 | # Some targets need to always link a special object in. Save its path here. |
3723 | global gdb_saved_set_unbuffered_mode_obj | |
3724 | set gdb_saved_set_unbuffered_mode_obj "" | |
3725 | ||
aff9c0f8 SM |
3726 | # Compile source files specified by SOURCE into a binary of type TYPE at path |
3727 | # DEST. gdb_compile is implemented using DejaGnu's target_compile, so the type | |
3728 | # parameter and most options are passed directly to it. | |
3729 | # | |
3730 | # The type can be one of the following: | |
3731 | # | |
3732 | # - object: Compile into an object file. | |
3733 | # - executable: Compile and link into an executable. | |
3734 | # - preprocess: Preprocess the source files. | |
3735 | # - assembly: Generate assembly listing. | |
3736 | # | |
3737 | # The following options are understood and processed by gdb_compile: | |
3738 | # | |
3739 | # - shlib=so_path: Add SO_PATH to the sources, and enable some target-specific | |
3740 | # quirks to be able to use shared libraries. | |
3741 | # - shlib_load: Link with appropriate libraries to allow the test to | |
3742 | # dynamically load libraries at runtime. For example, on Linux, this adds | |
3743 | # -ldl so that the test can use dlopen. | |
3744 | # - nowarnings: Inhibit all compiler warnings. | |
968aa7ae | 3745 | # - pie: Force creation of PIE executables. |
6e8b1ab2 | 3746 | # - nopie: Prevent creation of PIE executables. |
aff9c0f8 SM |
3747 | # |
3748 | # And here are some of the not too obscure options understood by DejaGnu that | |
3749 | # influence the compilation: | |
3750 | # | |
3751 | # - additional_flags=flag: Add FLAG to the compiler flags. | |
3752 | # - libs=library: Add LIBRARY to the libraries passed to the linker. The | |
3753 | # argument can be a file, in which case it's added to the sources, or a | |
3754 | # linker flag. | |
3755 | # - ldflags=flag: Add FLAG to the linker flags. | |
3756 | # - incdir=path: Add PATH to the searched include directories. | |
3757 | # - libdir=path: Add PATH to the linker searched directories. | |
3758 | # - ada, c++, f77: Compile the file as Ada, C++ or Fortran. | |
3759 | # - debug: Build with debug information. | |
3760 | # - optimize: Build with optimization. | |
3761 | ||
c906108c | 3762 | proc gdb_compile {source dest type options} { |
4ec70201 PA |
3763 | global GDB_TESTCASE_OPTIONS |
3764 | global gdb_wrapper_file | |
3765 | global gdb_wrapper_flags | |
3766 | global gdb_wrapper_initialized | |
f747e0ce PA |
3767 | global srcdir |
3768 | global objdir | |
3769 | global gdb_saved_set_unbuffered_mode_obj | |
c906108c | 3770 | |
695e2681 MK |
3771 | set outdir [file dirname $dest] |
3772 | ||
3773 | # Add platform-specific options if a shared library was specified using | |
3774 | # "shlib=librarypath" in OPTIONS. | |
dcc06925 | 3775 | set new_options {} |
5eb5f850 TT |
3776 | if {[lsearch -exact $options rust] != -1} { |
3777 | # -fdiagnostics-color is not a rustcc option. | |
3778 | } else { | |
3779 | set new_options [universal_compile_options] | |
3780 | } | |
8d70a9f0 AB |
3781 | |
3782 | # Place (and look for) Fortran `.mod` files in the output | |
3783 | # directory for this specific test. | |
3784 | if {[lsearch -exact $options f77] != -1 \ | |
3785 | || [lsearch -exact $options f90] != -1 } { | |
3786 | # Fortran compile. | |
3787 | set mod_path [standard_output_file ""] | |
3788 | lappend new_options "additional_flags=-J${mod_path}" | |
3789 | } | |
3790 | ||
695e2681 | 3791 | set shlib_found 0 |
bdf7534a | 3792 | set shlib_load 0 |
fc65c7db | 3793 | set getting_compiler_info 0 |
695e2681 | 3794 | foreach opt $options { |
6181e9c2 SM |
3795 | if {[regexp {^shlib=(.*)} $opt dummy_var shlib_name] |
3796 | && $type == "executable"} { | |
57bf0e56 | 3797 | if [test_compiler_info "xlc-*"] { |
93f02886 DJ |
3798 | # IBM xlc compiler doesn't accept shared library named other |
3799 | # than .so: use "-Wl," to bypass this | |
3800 | lappend source "-Wl,$shlib_name" | |
3801 | } elseif { ([istarget "*-*-mingw*"] | |
3802 | || [istarget *-*-cygwin*] | |
3803 | || [istarget *-*-pe*])} { | |
3804 | lappend source "${shlib_name}.a" | |
57bf0e56 DJ |
3805 | } else { |
3806 | lappend source $shlib_name | |
3807 | } | |
0413d738 | 3808 | if { $shlib_found == 0 } { |
57bf0e56 | 3809 | set shlib_found 1 |
0413d738 PA |
3810 | if { ([istarget "*-*-mingw*"] |
3811 | || [istarget *-*-cygwin*]) } { | |
bb61102d | 3812 | lappend new_options "additional_flags=-Wl,--enable-auto-import" |
0413d738 | 3813 | } |
6ebea266 DE |
3814 | if { [test_compiler_info "gcc-*"] || [test_compiler_info "clang-*"] } { |
3815 | # Undo debian's change in the default. | |
3816 | # Put it at the front to not override any user-provided | |
3817 | # value, and to make sure it appears in front of all the | |
3818 | # shlibs! | |
3819 | lappend new_options "early_flags=-Wl,--no-as-needed" | |
3820 | } | |
57bf0e56 | 3821 | } |
6181e9c2 | 3822 | } elseif { $opt == "shlib_load" && $type == "executable" } { |
bdf7534a | 3823 | set shlib_load 1 |
fc65c7db AH |
3824 | } elseif { $opt == "getting_compiler_info" } { |
3825 | # If this is set, calling test_compiler_info will cause recursion. | |
3826 | set getting_compiler_info 1 | |
57bf0e56 DJ |
3827 | } else { |
3828 | lappend new_options $opt | |
3829 | } | |
695e2681 | 3830 | } |
bdf7534a | 3831 | |
fc65c7db AH |
3832 | # Ensure stack protector is disabled for GCC, as this causes problems with |
3833 | # DWARF line numbering. | |
3834 | # See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=88432 | |
3835 | # This option defaults to on for Debian/Ubuntu. | |
3836 | if { $getting_compiler_info == 0 | |
3837 | && [test_compiler_info {gcc-*-*}] | |
3838 | && !([test_compiler_info {gcc-[0-3]-*}] | |
1670072e TT |
3839 | || [test_compiler_info {gcc-4-0-*}]) |
3840 | && [lsearch -exact $options rust] == -1} { | |
fc65c7db AH |
3841 | # Put it at the front to not override any user-provided value. |
3842 | lappend new_options "early_flags=-fno-stack-protector" | |
3843 | } | |
3844 | ||
6e774b13 SM |
3845 | # Because we link with libraries using their basename, we may need |
3846 | # (depending on the platform) to set a special rpath value, to allow | |
3847 | # the executable to find the libraries it depends on. | |
3848 | if { $shlib_load || $shlib_found } { | |
bdf7534a NF |
3849 | if { ([istarget "*-*-mingw*"] |
3850 | || [istarget *-*-cygwin*] | |
3ca22649 | 3851 | || [istarget *-*-pe*]) } { |
bdf7534a | 3852 | # Do not need anything. |
b2a6bdeb | 3853 | } elseif { [istarget *-*-freebsd*] || [istarget *-*-openbsd*] } { |
d8b34041 | 3854 | lappend new_options "ldflags=-Wl,-rpath,${outdir}" |
759f0f0b PA |
3855 | } elseif { [istarget arm*-*-symbianelf*] } { |
3856 | if { $shlib_load } { | |
3857 | lappend new_options "libs=-ldl" | |
3858 | } | |
bdf7534a NF |
3859 | } else { |
3860 | if { $shlib_load } { | |
3861 | lappend new_options "libs=-ldl" | |
3862 | } | |
d8b34041 | 3863 | lappend new_options "ldflags=-Wl,-rpath,\\\$ORIGIN" |
bdf7534a NF |
3864 | } |
3865 | } | |
695e2681 | 3866 | set options $new_options |
57bf0e56 | 3867 | |
c906108c | 3868 | if [info exists GDB_TESTCASE_OPTIONS] { |
4ec70201 | 3869 | lappend options "additional_flags=$GDB_TESTCASE_OPTIONS" |
c906108c SS |
3870 | } |
3871 | verbose "options are $options" | |
3872 | verbose "source is $source $dest $type $options" | |
3873 | ||
24ac169a | 3874 | gdb_wrapper_init |
f1c47eb2 MS |
3875 | |
3876 | if {[target_info exists needs_status_wrapper] && \ | |
3877 | [target_info needs_status_wrapper] != "0" && \ | |
3878 | [info exists gdb_wrapper_file]} { | |
3879 | lappend options "libs=${gdb_wrapper_file}" | |
3880 | lappend options "ldflags=${gdb_wrapper_flags}" | |
3881 | } | |
3882 | ||
fc91c6c2 PB |
3883 | # Replace the "nowarnings" option with the appropriate additional_flags |
3884 | # to disable compiler warnings. | |
3885 | set nowarnings [lsearch -exact $options nowarnings] | |
3886 | if {$nowarnings != -1} { | |
3887 | if [target_info exists gdb,nowarnings_flag] { | |
3888 | set flag "additional_flags=[target_info gdb,nowarnings_flag]" | |
3889 | } else { | |
3890 | set flag "additional_flags=-w" | |
3891 | } | |
3892 | set options [lreplace $options $nowarnings $nowarnings $flag] | |
3893 | } | |
3894 | ||
968aa7ae AH |
3895 | # Replace the "pie" option with the appropriate compiler and linker flags |
3896 | # to enable PIE executables. | |
3897 | set pie [lsearch -exact $options pie] | |
3898 | if {$pie != -1} { | |
3899 | if [target_info exists gdb,pie_flag] { | |
3900 | set flag "additional_flags=[target_info gdb,pie_flag]" | |
3901 | } else { | |
3902 | # For safety, use fPIE rather than fpie. On AArch64, m68k, PowerPC | |
3903 | # and SPARC, fpie can cause compile errors due to the GOT exceeding | |
3904 | # a maximum size. On other architectures the two flags are | |
3905 | # identical (see the GCC manual). Note Debian9 and Ubuntu16.10 | |
3906 | # onwards default GCC to using fPIE. If you do require fpie, then | |
3907 | # it can be set using the pie_flag. | |
3908 | set flag "additional_flags=-fPIE" | |
3909 | } | |
3910 | set options [lreplace $options $pie $pie $flag] | |
3911 | ||
3912 | if [target_info exists gdb,pie_ldflag] { | |
3913 | set flag "ldflags=[target_info gdb,pie_ldflag]" | |
3914 | } else { | |
3915 | set flag "ldflags=-pie" | |
3916 | } | |
3917 | lappend options "$flag" | |
3918 | } | |
3919 | ||
3920 | # Replace the "nopie" option with the appropriate linker flag to disable | |
3921 | # PIE executables. There are no compiler flags for this option. | |
6e8b1ab2 JV |
3922 | set nopie [lsearch -exact $options nopie] |
3923 | if {$nopie != -1} { | |
3924 | if [target_info exists gdb,nopie_flag] { | |
3925 | set flag "ldflags=[target_info gdb,nopie_flag]" | |
3926 | } else { | |
3927 | set flag "ldflags=-no-pie" | |
3928 | } | |
3929 | set options [lreplace $options $nopie $nopie $flag] | |
3930 | } | |
3931 | ||
f747e0ce PA |
3932 | if { $type == "executable" } { |
3933 | if { ([istarget "*-*-mingw*"] | |
56643c5e | 3934 | || [istarget "*-*-*djgpp"] |
f747e0ce PA |
3935 | || [istarget "*-*-cygwin*"])} { |
3936 | # Force output to unbuffered mode, by linking in an object file | |
3937 | # with a global contructor that calls setvbuf. | |
3938 | # | |
40c94099 | 3939 | # Compile the special object separately for two reasons: |
f747e0ce PA |
3940 | # 1) Insulate it from $options. |
3941 | # 2) Avoid compiling it for every gdb_compile invocation, | |
3942 | # which is time consuming, especially if we're remote | |
3943 | # host testing. | |
3944 | # | |
3945 | if { $gdb_saved_set_unbuffered_mode_obj == "" } { | |
3946 | verbose "compiling gdb_saved_set_unbuffered_obj" | |
3947 | set unbuf_src ${srcdir}/lib/set_unbuffered_mode.c | |
3948 | set unbuf_obj ${objdir}/set_unbuffered_mode.o | |
3949 | ||
3950 | set result [gdb_compile "${unbuf_src}" "${unbuf_obj}" object {nowarnings}] | |
3951 | if { $result != "" } { | |
3952 | return $result | |
3953 | } | |
f6dc277e YQ |
3954 | if {[is_remote host]} { |
3955 | set gdb_saved_set_unbuffered_mode_obj set_unbuffered_mode_saved.o | |
3956 | } else { | |
3957 | set gdb_saved_set_unbuffered_mode_obj ${objdir}/set_unbuffered_mode_saved.o | |
3958 | } | |
f747e0ce PA |
3959 | # Link a copy of the output object, because the |
3960 | # original may be automatically deleted. | |
f6dc277e | 3961 | remote_download host $unbuf_obj $gdb_saved_set_unbuffered_mode_obj |
f747e0ce PA |
3962 | } else { |
3963 | verbose "gdb_saved_set_unbuffered_obj already compiled" | |
3964 | } | |
3965 | ||
3966 | # Rely on the internal knowledge that the global ctors are ran in | |
3967 | # reverse link order. In that case, we can use ldflags to | |
3968 | # avoid copying the object file to the host multiple | |
3969 | # times. | |
ace5c364 PM |
3970 | # This object can only be added if standard libraries are |
3971 | # used. Thus, we need to disable it if -nostdlib option is used | |
3972 | if {[lsearch -regexp $options "-nostdlib"] < 0 } { | |
3973 | lappend options "ldflags=$gdb_saved_set_unbuffered_mode_obj" | |
3974 | } | |
f747e0ce PA |
3975 | } |
3976 | } | |
3977 | ||
4ec70201 | 3978 | set result [target_compile $source $dest $type $options] |
93f02886 DJ |
3979 | |
3980 | # Prune uninteresting compiler (and linker) output. | |
3981 | regsub "Creating library file: \[^\r\n\]*\[\r\n\]+" $result "" result | |
3982 | ||
4ec70201 PA |
3983 | regsub "\[\r\n\]*$" "$result" "" result |
3984 | regsub "^\[\r\n\]*" "$result" "" result | |
ec3c07fc | 3985 | |
a80cf5d8 TV |
3986 | if { $type == "executable" && $result == "" \ |
3987 | && ($nopie != -1 || $pie != -1) } { | |
3988 | set is_pie [exec_is_pie "$dest"] | |
3989 | if { $nopie != -1 && $is_pie == 1 } { | |
b13057d9 | 3990 | set result "nopie failed to prevent PIE executable" |
a80cf5d8 TV |
3991 | } elseif { $pie != -1 && $is_pie == 0 } { |
3992 | set result "pie failed to generate PIE executable" | |
b13057d9 TV |
3993 | } |
3994 | } | |
3995 | ||
ec3c07fc NS |
3996 | if {[lsearch $options quiet] < 0} { |
3997 | # We shall update this on a per language basis, to avoid | |
3998 | # changing the entire testsuite in one go. | |
3999 | if {[lsearch $options f77] >= 0} { | |
4000 | gdb_compile_test $source $result | |
4001 | } elseif { $result != "" } { | |
4002 | clone_output "gdb compile failed, $result" | |
4003 | } | |
c906108c | 4004 | } |
ae59b1da | 4005 | return $result |
c906108c SS |
4006 | } |
4007 | ||
b6ff0e81 JB |
4008 | |
4009 | # This is just like gdb_compile, above, except that it tries compiling | |
4010 | # against several different thread libraries, to see which one this | |
4011 | # system has. | |
4012 | proc gdb_compile_pthreads {source dest type options} { | |
26b911fb KB |
4013 | if {$type != "executable"} { |
4014 | return [gdb_compile $source $dest $type $options] | |
4015 | } | |
0ae67eb3 | 4016 | set built_binfile 0 |
b6ff0e81 | 4017 | set why_msg "unrecognized error" |
24486cb7 | 4018 | foreach lib {-lpthreads -lpthread -lthread ""} { |
b6ff0e81 JB |
4019 | # This kind of wipes out whatever libs the caller may have |
4020 | # set. Or maybe theirs will override ours. How infelicitous. | |
b5ab8ff3 | 4021 | set options_with_lib [concat $options [list libs=$lib quiet]] |
b6ff0e81 JB |
4022 | set ccout [gdb_compile $source $dest $type $options_with_lib] |
4023 | switch -regexp -- $ccout { | |
4024 | ".*no posix threads support.*" { | |
4025 | set why_msg "missing threads include file" | |
4026 | break | |
4027 | } | |
4028 | ".*cannot open -lpthread.*" { | |
4029 | set why_msg "missing runtime threads library" | |
4030 | } | |
4031 | ".*Can't find library for -lpthread.*" { | |
4032 | set why_msg "missing runtime threads library" | |
4033 | } | |
4034 | {^$} { | |
4035 | pass "successfully compiled posix threads test case" | |
4036 | set built_binfile 1 | |
4037 | break | |
4038 | } | |
4039 | } | |
4040 | } | |
0ae67eb3 | 4041 | if {!$built_binfile} { |
bc6c7af4 | 4042 | unsupported "couldn't compile [file tail $source]: ${why_msg}" |
b6ff0e81 JB |
4043 | return -1 |
4044 | } | |
57bf0e56 DJ |
4045 | } |
4046 | ||
409d8f48 | 4047 | # Build a shared library from SOURCES. |
57bf0e56 DJ |
4048 | |
4049 | proc gdb_compile_shlib {sources dest options} { | |
4050 | set obj_options $options | |
4051 | ||
409d8f48 AB |
4052 | set info_options "" |
4053 | if { [lsearch -exact $options "c++"] >= 0 } { | |
4054 | set info_options "c++" | |
4055 | } | |
4056 | if [get_compiler_info ${info_options}] { | |
4057 | return -1 | |
4058 | } | |
4059 | ||
57bf0e56 DJ |
4060 | switch -glob [test_compiler_info] { |
4061 | "xlc-*" { | |
4062 | lappend obj_options "additional_flags=-qpic" | |
4063 | } | |
ee92b0dd DE |
4064 | "clang-*" { |
4065 | if { !([istarget "*-*-cygwin*"] | |
4066 | || [istarget "*-*-mingw*"]) } { | |
4067 | lappend obj_options "additional_flags=-fpic" | |
4068 | } | |
4069 | } | |
57bf0e56 DJ |
4070 | "gcc-*" { |
4071 | if { !([istarget "powerpc*-*-aix*"] | |
227c54da DJ |
4072 | || [istarget "rs6000*-*-aix*"] |
4073 | || [istarget "*-*-cygwin*"] | |
4074 | || [istarget "*-*-mingw*"] | |
4075 | || [istarget "*-*-pe*"]) } { | |
57bf0e56 DJ |
4076 | lappend obj_options "additional_flags=-fpic" |
4077 | } | |
4078 | } | |
9b9b09e9 BH |
4079 | "icc-*" { |
4080 | lappend obj_options "additional_flags=-fpic" | |
4081 | } | |
57bf0e56 | 4082 | default { |
3ca22649 | 4083 | # don't know what the compiler is... |
57bf0e56 DJ |
4084 | } |
4085 | } | |
4086 | ||
4087 | set outdir [file dirname $dest] | |
4088 | set objects "" | |
4089 | foreach source $sources { | |
2ff0a947 TT |
4090 | set sourcebase [file tail $source] |
4091 | if {[file extension $source] == ".o"} { | |
4092 | # Already a .o file. | |
4093 | lappend objects $source | |
4094 | } elseif {[gdb_compile $source "${outdir}/${sourcebase}.o" object \ | |
4095 | $obj_options] != ""} { | |
4096 | return -1 | |
4097 | } else { | |
4098 | lappend objects ${outdir}/${sourcebase}.o | |
4099 | } | |
57bf0e56 DJ |
4100 | } |
4101 | ||
3ca22649 SM |
4102 | set link_options $options |
4103 | if [test_compiler_info "xlc-*"] { | |
4104 | lappend link_options "additional_flags=-qmkshrobj" | |
57bf0e56 | 4105 | } else { |
3ca22649 SM |
4106 | lappend link_options "additional_flags=-shared" |
4107 | ||
4108 | if { ([istarget "*-*-mingw*"] | |
4109 | || [istarget *-*-cygwin*] | |
4110 | || [istarget *-*-pe*]) } { | |
4111 | if { [is_remote host] } { | |
4112 | set name [file tail ${dest}] | |
4113 | } else { | |
4114 | set name ${dest} | |
4115 | } | |
4116 | lappend link_options "additional_flags=-Wl,--out-implib,${name}.a" | |
6e774b13 SM |
4117 | } else { |
4118 | # Set the soname of the library. This causes the linker on ELF | |
4119 | # systems to create the DT_NEEDED entry in the executable referring | |
4120 | # to the soname of the library, and not its absolute path. This | |
4121 | # (using the absolute path) would be problem when testing on a | |
4122 | # remote target. | |
4123 | # | |
4124 | # In conjunction with setting the soname, we add the special | |
4125 | # rpath=$ORIGIN value when building the executable, so that it's | |
4126 | # able to find the library in its own directory. | |
3ca22649 SM |
4127 | set destbase [file tail $dest] |
4128 | lappend link_options "additional_flags=-Wl,-soname,$destbase" | |
4129 | } | |
4130 | } | |
4131 | if {[gdb_compile "${objects}" "${dest}" executable $link_options] != ""} { | |
4132 | return -1 | |
57bf0e56 | 4133 | } |
3ca22649 SM |
4134 | if { [is_remote host] |
4135 | && ([istarget "*-*-mingw*"] | |
4136 | || [istarget *-*-cygwin*] | |
4137 | || [istarget *-*-pe*]) } { | |
4138 | set dest_tail_name [file tail ${dest}] | |
4139 | remote_upload host $dest_tail_name.a ${dest}.a | |
4140 | remote_file host delete $dest_tail_name.a | |
4141 | } | |
4142 | ||
4143 | return "" | |
b6ff0e81 JB |
4144 | } |
4145 | ||
756d88a7 UW |
4146 | # This is just like gdb_compile_shlib, above, except that it tries compiling |
4147 | # against several different thread libraries, to see which one this | |
4148 | # system has. | |
4149 | proc gdb_compile_shlib_pthreads {sources dest options} { | |
4150 | set built_binfile 0 | |
4151 | set why_msg "unrecognized error" | |
4152 | foreach lib {-lpthreads -lpthread -lthread ""} { | |
4153 | # This kind of wipes out whatever libs the caller may have | |
4154 | # set. Or maybe theirs will override ours. How infelicitous. | |
4155 | set options_with_lib [concat $options [list libs=$lib quiet]] | |
4156 | set ccout [gdb_compile_shlib $sources $dest $options_with_lib] | |
4157 | switch -regexp -- $ccout { | |
4158 | ".*no posix threads support.*" { | |
4159 | set why_msg "missing threads include file" | |
4160 | break | |
4161 | } | |
4162 | ".*cannot open -lpthread.*" { | |
4163 | set why_msg "missing runtime threads library" | |
4164 | } | |
4165 | ".*Can't find library for -lpthread.*" { | |
4166 | set why_msg "missing runtime threads library" | |
4167 | } | |
4168 | {^$} { | |
4169 | pass "successfully compiled posix threads test case" | |
4170 | set built_binfile 1 | |
4171 | break | |
4172 | } | |
4173 | } | |
4174 | } | |
4175 | if {!$built_binfile} { | |
bc6c7af4 | 4176 | unsupported "couldn't compile $sources: ${why_msg}" |
756d88a7 UW |
4177 | return -1 |
4178 | } | |
4179 | } | |
4180 | ||
130cacce AF |
4181 | # This is just like gdb_compile_pthreads, above, except that we always add the |
4182 | # objc library for compiling Objective-C programs | |
4183 | proc gdb_compile_objc {source dest type options} { | |
4184 | set built_binfile 0 | |
4185 | set why_msg "unrecognized error" | |
4186 | foreach lib {-lobjc -lpthreads -lpthread -lthread solaris} { | |
4187 | # This kind of wipes out whatever libs the caller may have | |
4188 | # set. Or maybe theirs will override ours. How infelicitous. | |
4189 | if { $lib == "solaris" } { | |
4190 | set lib "-lpthread -lposix4" | |
4191 | } | |
4192 | if { $lib != "-lobjc" } { | |
4193 | set lib "-lobjc $lib" | |
4194 | } | |
4195 | set options_with_lib [concat $options [list libs=$lib quiet]] | |
4196 | set ccout [gdb_compile $source $dest $type $options_with_lib] | |
4197 | switch -regexp -- $ccout { | |
4198 | ".*no posix threads support.*" { | |
4199 | set why_msg "missing threads include file" | |
4200 | break | |
4201 | } | |
4202 | ".*cannot open -lpthread.*" { | |
4203 | set why_msg "missing runtime threads library" | |
4204 | } | |
4205 | ".*Can't find library for -lpthread.*" { | |
4206 | set why_msg "missing runtime threads library" | |
4207 | } | |
4208 | {^$} { | |
4209 | pass "successfully compiled objc with posix threads test case" | |
4210 | set built_binfile 1 | |
4211 | break | |
4212 | } | |
4213 | } | |
4214 | } | |
4215 | if {!$built_binfile} { | |
bc6c7af4 | 4216 | unsupported "couldn't compile [file tail $source]: ${why_msg}" |
130cacce AF |
4217 | return -1 |
4218 | } | |
4219 | } | |
4220 | ||
26b911fb KB |
4221 | # Build an OpenMP program from SOURCE. See prefatory comment for |
4222 | # gdb_compile, above, for discussion of the parameters to this proc. | |
4223 | ||
4224 | proc gdb_compile_openmp {source dest type options} { | |
4225 | lappend options "additional_flags=-fopenmp" | |
4226 | return [gdb_compile $source $dest $type $options] | |
4227 | } | |
4228 | ||
f9e2e39d AH |
4229 | # Send a command to GDB. |
4230 | # For options for TYPE see gdb_stdin_log_write | |
4231 | ||
4232 | proc send_gdb { string {type standard}} { | |
4ec70201 | 4233 | global suppress_flag |
c906108c | 4234 | if { $suppress_flag } { |
ae59b1da | 4235 | return "suppressed" |
c906108c | 4236 | } |
f9e2e39d | 4237 | gdb_stdin_log_write $string $type |
ae59b1da | 4238 | return [remote_send host "$string"] |
c906108c SS |
4239 | } |
4240 | ||
f71c18e7 PA |
4241 | # Send STRING to the inferior's terminal. |
4242 | ||
4243 | proc send_inferior { string } { | |
4244 | global inferior_spawn_id | |
4245 | ||
4246 | if {[catch "send -i $inferior_spawn_id -- \$string" errorInfo]} { | |
4247 | return "$errorInfo" | |
4248 | } else { | |
4249 | return "" | |
4250 | } | |
4251 | } | |
4252 | ||
c906108c SS |
4253 | # |
4254 | # | |
4255 | ||
4256 | proc gdb_expect { args } { | |
4257 | if { [llength $args] == 2 && [lindex $args 0] != "-re" } { | |
4ec70201 PA |
4258 | set atimeout [lindex $args 0] |
4259 | set expcode [list [lindex $args 1]] | |
c906108c | 4260 | } else { |
4ec70201 | 4261 | set expcode $args |
2f34202f MR |
4262 | } |
4263 | ||
4a40f85a MR |
4264 | # A timeout argument takes precedence, otherwise of all the timeouts |
4265 | # select the largest. | |
4a40f85a MR |
4266 | if [info exists atimeout] { |
4267 | set tmt $atimeout | |
4268 | } else { | |
45fd756c | 4269 | set tmt [get_largest_timeout] |
c906108c | 4270 | } |
2f34202f | 4271 | |
4ec70201 PA |
4272 | global suppress_flag |
4273 | global remote_suppress_flag | |
c906108c | 4274 | if [info exists remote_suppress_flag] { |
4ec70201 | 4275 | set old_val $remote_suppress_flag |
c906108c SS |
4276 | } |
4277 | if [info exists suppress_flag] { | |
4278 | if { $suppress_flag } { | |
4ec70201 | 4279 | set remote_suppress_flag 1 |
c906108c SS |
4280 | } |
4281 | } | |
a0b3c4fd | 4282 | set code [catch \ |
4a40f85a | 4283 | {uplevel remote_expect host $tmt $expcode} string] |
c906108c | 4284 | if [info exists old_val] { |
4ec70201 | 4285 | set remote_suppress_flag $old_val |
c906108c SS |
4286 | } else { |
4287 | if [info exists remote_suppress_flag] { | |
4ec70201 | 4288 | unset remote_suppress_flag |
c906108c SS |
4289 | } |
4290 | } | |
4291 | ||
4292 | if {$code == 1} { | |
4ec70201 | 4293 | global errorInfo errorCode |
c906108c SS |
4294 | |
4295 | return -code error -errorinfo $errorInfo -errorcode $errorCode $string | |
d6d7a51a | 4296 | } else { |
c906108c SS |
4297 | return -code $code $string |
4298 | } | |
4299 | } | |
4300 | ||
5fa290c1 | 4301 | # gdb_expect_list TEST SENTINEL LIST -- expect a sequence of outputs |
085dd6e6 JM |
4302 | # |
4303 | # Check for long sequence of output by parts. | |
5fa290c1 | 4304 | # TEST: is the test message to be printed with the test success/fail. |
085dd6e6 JM |
4305 | # SENTINEL: Is the terminal pattern indicating that output has finished. |
4306 | # LIST: is the sequence of outputs to match. | |
4307 | # If the sentinel is recognized early, it is considered an error. | |
4308 | # | |
11cf8741 JM |
4309 | # Returns: |
4310 | # 1 if the test failed, | |
4311 | # 0 if the test passes, | |
4312 | # -1 if there was an internal error. | |
5fa290c1 | 4313 | |
c2d11a7d | 4314 | proc gdb_expect_list {test sentinel list} { |
085dd6e6 | 4315 | global gdb_prompt |
11cf8741 | 4316 | global suppress_flag |
085dd6e6 | 4317 | set index 0 |
43ff13b4 | 4318 | set ok 1 |
11cf8741 JM |
4319 | if { $suppress_flag } { |
4320 | set ok 0 | |
a20ce2c3 | 4321 | unresolved "${test}" |
11cf8741 | 4322 | } |
43ff13b4 | 4323 | while { ${index} < [llength ${list}] } { |
085dd6e6 JM |
4324 | set pattern [lindex ${list} ${index}] |
4325 | set index [expr ${index} + 1] | |
6b0ecdc2 | 4326 | verbose -log "gdb_expect_list pattern: /$pattern/" 2 |
085dd6e6 | 4327 | if { ${index} == [llength ${list}] } { |
43ff13b4 JM |
4328 | if { ${ok} } { |
4329 | gdb_expect { | |
c2d11a7d | 4330 | -re "${pattern}${sentinel}" { |
a20ce2c3 | 4331 | # pass "${test}, pattern ${index} + sentinel" |
c2d11a7d JM |
4332 | } |
4333 | -re "${sentinel}" { | |
a20ce2c3 | 4334 | fail "${test} (pattern ${index} + sentinel)" |
c2d11a7d | 4335 | set ok 0 |
43ff13b4 | 4336 | } |
5c5455dc AC |
4337 | -re ".*A problem internal to GDB has been detected" { |
4338 | fail "${test} (GDB internal error)" | |
4339 | set ok 0 | |
4340 | gdb_internal_error_resync | |
4341 | } | |
43ff13b4 | 4342 | timeout { |
a20ce2c3 | 4343 | fail "${test} (pattern ${index} + sentinel) (timeout)" |
43ff13b4 JM |
4344 | set ok 0 |
4345 | } | |
085dd6e6 | 4346 | } |
43ff13b4 | 4347 | } else { |
a20ce2c3 | 4348 | # unresolved "${test}, pattern ${index} + sentinel" |
085dd6e6 JM |
4349 | } |
4350 | } else { | |
43ff13b4 JM |
4351 | if { ${ok} } { |
4352 | gdb_expect { | |
4353 | -re "${pattern}" { | |
a20ce2c3 | 4354 | # pass "${test}, pattern ${index}" |
43ff13b4 | 4355 | } |
c2d11a7d | 4356 | -re "${sentinel}" { |
a20ce2c3 | 4357 | fail "${test} (pattern ${index})" |
43ff13b4 JM |
4358 | set ok 0 |
4359 | } | |
5c5455dc AC |
4360 | -re ".*A problem internal to GDB has been detected" { |
4361 | fail "${test} (GDB internal error)" | |
4362 | set ok 0 | |
4363 | gdb_internal_error_resync | |
4364 | } | |
43ff13b4 | 4365 | timeout { |
a20ce2c3 | 4366 | fail "${test} (pattern ${index}) (timeout)" |
43ff13b4 JM |
4367 | set ok 0 |
4368 | } | |
085dd6e6 | 4369 | } |
43ff13b4 | 4370 | } else { |
a20ce2c3 | 4371 | # unresolved "${test}, pattern ${index}" |
085dd6e6 JM |
4372 | } |
4373 | } | |
4374 | } | |
11cf8741 | 4375 | if { ${ok} } { |
a20ce2c3 | 4376 | pass "${test}" |
11cf8741 JM |
4377 | return 0 |
4378 | } else { | |
4379 | return 1 | |
4380 | } | |
085dd6e6 JM |
4381 | } |
4382 | ||
4383 | # | |
4384 | # | |
c906108c | 4385 | proc gdb_suppress_entire_file { reason } { |
4ec70201 | 4386 | global suppress_flag |
c906108c | 4387 | |
4ec70201 PA |
4388 | warning "$reason\n" |
4389 | set suppress_flag -1 | |
c906108c SS |
4390 | } |
4391 | ||
4392 | # | |
4393 | # Set suppress_flag, which will cause all subsequent calls to send_gdb and | |
4394 | # gdb_expect to fail immediately (until the next call to | |
4395 | # gdb_stop_suppressing_tests). | |
4396 | # | |
4397 | proc gdb_suppress_tests { args } { | |
4ec70201 | 4398 | global suppress_flag |
c906108c SS |
4399 | |
4400 | return; # fnf - disable pending review of results where | |
4401 | # testsuite ran better without this | |
4ec70201 | 4402 | incr suppress_flag |
c906108c SS |
4403 | |
4404 | if { $suppress_flag == 1 } { | |
4405 | if { [llength $args] > 0 } { | |
4ec70201 | 4406 | warning "[lindex $args 0]\n" |
c906108c | 4407 | } else { |
4ec70201 | 4408 | warning "Because of previous failure, all subsequent tests in this group will automatically fail.\n" |
c906108c SS |
4409 | } |
4410 | } | |
4411 | } | |
4412 | ||
4413 | # | |
4414 | # Clear suppress_flag. | |
4415 | # | |
4416 | proc gdb_stop_suppressing_tests { } { | |
4ec70201 | 4417 | global suppress_flag |
c906108c SS |
4418 | |
4419 | if [info exists suppress_flag] { | |
4420 | if { $suppress_flag > 0 } { | |
4ec70201 PA |
4421 | set suppress_flag 0 |
4422 | clone_output "Tests restarted.\n" | |
c906108c SS |
4423 | } |
4424 | } else { | |
4ec70201 | 4425 | set suppress_flag 0 |
c906108c SS |
4426 | } |
4427 | } | |
4428 | ||
4429 | proc gdb_clear_suppressed { } { | |
4ec70201 | 4430 | global suppress_flag |
c906108c | 4431 | |
4ec70201 | 4432 | set suppress_flag 0 |
c906108c SS |
4433 | } |
4434 | ||
94696ad3 PA |
4435 | # Spawn the gdb process. |
4436 | # | |
4437 | # This doesn't expect any output or do any other initialization, | |
4438 | # leaving those to the caller. | |
4439 | # | |
4440 | # Overridable function -- you can override this function in your | |
4441 | # baseboard file. | |
4442 | ||
4443 | proc gdb_spawn { } { | |
4444 | default_gdb_spawn | |
4445 | } | |
4446 | ||
98880d46 PA |
4447 | # Spawn GDB with CMDLINE_FLAGS appended to the GDBFLAGS global. |
4448 | ||
4449 | proc gdb_spawn_with_cmdline_opts { cmdline_flags } { | |
4450 | global GDBFLAGS | |
4451 | ||
4452 | set saved_gdbflags $GDBFLAGS | |
4453 | ||
0bbeccb1 PA |
4454 | if {$GDBFLAGS != ""} { |
4455 | append GDBFLAGS " " | |
4456 | } | |
98880d46 PA |
4457 | append GDBFLAGS $cmdline_flags |
4458 | ||
4459 | set res [gdb_spawn] | |
4460 | ||
4461 | set GDBFLAGS $saved_gdbflags | |
4462 | ||
4463 | return $res | |
4464 | } | |
4465 | ||
94696ad3 PA |
4466 | # Start gdb running, wait for prompt, and disable the pagers. |
4467 | ||
4468 | # Overridable function -- you can override this function in your | |
4469 | # baseboard file. | |
4470 | ||
c906108c SS |
4471 | proc gdb_start { } { |
4472 | default_gdb_start | |
4473 | } | |
4474 | ||
4475 | proc gdb_exit { } { | |
4476 | catch default_gdb_exit | |
4477 | } | |
4478 | ||
60b3033e PA |
4479 | # Return true if we can spawn a program on the target and attach to |
4480 | # it. | |
4481 | ||
4482 | proc can_spawn_for_attach { } { | |
2c8c5d37 PA |
4483 | # We use exp_pid to get the inferior's pid, assuming that gives |
4484 | # back the pid of the program. On remote boards, that would give | |
4485 | # us instead the PID of e.g., the ssh client, etc. | |
60b3033e PA |
4486 | if [is_remote target] then { |
4487 | return 0 | |
4488 | } | |
4489 | ||
4490 | # The "attach" command doesn't make sense when the target is | |
4491 | # stub-like, where GDB finds the program already started on | |
4492 | # initial connection. | |
4493 | if {[target_info exists use_gdb_stub]} { | |
4494 | return 0 | |
4495 | } | |
4496 | ||
4497 | # Assume yes. | |
4498 | return 1 | |
4499 | } | |
4500 | ||
2c8c5d37 PA |
4501 | # Kill a progress previously started with spawn_wait_for_attach, and |
4502 | # reap its wait status. PROC_SPAWN_ID is the spawn id associated with | |
4503 | # the process. | |
4504 | ||
4505 | proc kill_wait_spawned_process { proc_spawn_id } { | |
4506 | set pid [exp_pid -i $proc_spawn_id] | |
4507 | ||
4508 | verbose -log "killing ${pid}" | |
4509 | remote_exec build "kill -9 ${pid}" | |
4510 | ||
4511 | verbose -log "closing ${proc_spawn_id}" | |
4512 | catch "close -i $proc_spawn_id" | |
4513 | verbose -log "waiting for ${proc_spawn_id}" | |
4514 | ||
4515 | # If somehow GDB ends up still attached to the process here, a | |
4516 | # blocking wait hangs until gdb is killed (or until gdb / the | |
4517 | # ptracer reaps the exit status too, but that won't happen because | |
4518 | # something went wrong.) Passing -nowait makes expect tell Tcl to | |
4519 | # wait for the PID in the background. That's fine because we | |
4520 | # don't care about the exit status. */ | |
4521 | wait -nowait -i $proc_spawn_id | |
4522 | } | |
4523 | ||
4524 | # Returns the process id corresponding to the given spawn id. | |
4525 | ||
4526 | proc spawn_id_get_pid { spawn_id } { | |
4527 | set testpid [exp_pid -i $spawn_id] | |
4528 | ||
4529 | if { [istarget "*-*-cygwin*"] } { | |
4530 | # testpid is the Cygwin PID, GDB uses the Windows PID, which | |
4531 | # might be different due to the way fork/exec works. | |
4532 | set testpid [ exec ps -e | gawk "{ if (\$1 == $testpid) print \$4; }" ] | |
4533 | } | |
4534 | ||
4535 | return $testpid | |
4536 | } | |
4537 | ||
4c92ff2c | 4538 | # Start a set of programs running and then wait for a bit, to be sure |
2c8c5d37 PA |
4539 | # that they can be attached to. Return a list of processes spawn IDs, |
4540 | # one element for each process spawned. It's a test error to call | |
4541 | # this when [can_spawn_for_attach] is false. | |
4c92ff2c PA |
4542 | |
4543 | proc spawn_wait_for_attach { executable_list } { | |
2c8c5d37 | 4544 | set spawn_id_list {} |
4c92ff2c | 4545 | |
60b3033e PA |
4546 | if ![can_spawn_for_attach] { |
4547 | # The caller should have checked can_spawn_for_attach itself | |
4548 | # before getting here. | |
4549 | error "can't spawn for attach with this target/board" | |
4550 | } | |
4551 | ||
4c92ff2c | 4552 | foreach {executable} $executable_list { |
2c8c5d37 PA |
4553 | # Note we use Expect's spawn, not Tcl's exec, because with |
4554 | # spawn we control when to wait for/reap the process. That | |
4555 | # allows killing the process by PID without being subject to | |
4556 | # pid-reuse races. | |
4557 | lappend spawn_id_list [remote_spawn target $executable] | |
4c92ff2c PA |
4558 | } |
4559 | ||
4560 | sleep 2 | |
4561 | ||
2c8c5d37 | 4562 | return $spawn_id_list |
4c92ff2c PA |
4563 | } |
4564 | ||
e63b55d1 NS |
4565 | # |
4566 | # gdb_load_cmd -- load a file into the debugger. | |
4567 | # ARGS - additional args to load command. | |
4568 | # return a -1 if anything goes wrong. | |
4569 | # | |
4570 | proc gdb_load_cmd { args } { | |
4571 | global gdb_prompt | |
4572 | ||
4573 | if [target_info exists gdb_load_timeout] { | |
4574 | set loadtimeout [target_info gdb_load_timeout] | |
4575 | } else { | |
4576 | set loadtimeout 1600 | |
4577 | } | |
4578 | send_gdb "load $args\n" | |
e91528f0 | 4579 | verbose "Timeout is now $loadtimeout seconds" 2 |
e63b55d1 NS |
4580 | gdb_expect $loadtimeout { |
4581 | -re "Loading section\[^\r\]*\r\n" { | |
4582 | exp_continue | |
4583 | } | |
4584 | -re "Start address\[\r\]*\r\n" { | |
4585 | exp_continue | |
4586 | } | |
4587 | -re "Transfer rate\[\r\]*\r\n" { | |
4588 | exp_continue | |
4589 | } | |
4590 | -re "Memory access error\[^\r\]*\r\n" { | |
4591 | perror "Failed to load program" | |
4592 | return -1 | |
4593 | } | |
4594 | -re "$gdb_prompt $" { | |
4595 | return 0 | |
4596 | } | |
4597 | -re "(.*)\r\n$gdb_prompt " { | |
4598 | perror "Unexpected reponse from 'load' -- $expect_out(1,string)" | |
4599 | return -1 | |
4600 | } | |
4601 | timeout { | |
c4b347c7 | 4602 | perror "Timed out trying to load $args." |
e63b55d1 NS |
4603 | return -1 |
4604 | } | |
4605 | } | |
4606 | return -1 | |
4607 | } | |
4608 | ||
2d338fa9 TT |
4609 | # Invoke "gcore". CORE is the name of the core file to write. TEST |
4610 | # is the name of the test case. This will return 1 if the core file | |
4611 | # was created, 0 otherwise. If this fails to make a core file because | |
4612 | # this configuration of gdb does not support making core files, it | |
4613 | # will call "unsupported", not "fail". However, if this fails to make | |
4614 | # a core file for some other reason, then it will call "fail". | |
4615 | ||
4616 | proc gdb_gcore_cmd {core test} { | |
4617 | global gdb_prompt | |
4618 | ||
4619 | set result 0 | |
4620 | gdb_test_multiple "gcore $core" $test { | |
4621 | -re "Saved corefile .*\[\r\n\]+$gdb_prompt $" { | |
4622 | pass $test | |
4623 | set result 1 | |
4624 | } | |
bbe769cc | 4625 | -re "(?:Can't create a corefile|Target does not support core file generation\\.)\[\r\n\]+$gdb_prompt $" { |
2d338fa9 TT |
4626 | unsupported $test |
4627 | } | |
4628 | } | |
4629 | ||
4630 | return $result | |
4631 | } | |
4632 | ||
fac51dd9 DE |
4633 | # Load core file CORE. TEST is the name of the test case. |
4634 | # This will record a pass/fail for loading the core file. | |
4635 | # Returns: | |
4636 | # 1 - core file is successfully loaded | |
4637 | # 0 - core file loaded but has a non fatal error | |
4638 | # -1 - core file failed to load | |
4639 | ||
4640 | proc gdb_core_cmd { core test } { | |
4641 | global gdb_prompt | |
4642 | ||
4f424bb1 | 4643 | gdb_test_multiple "core $core" "$test" { |
fac51dd9 DE |
4644 | -re "\\\[Thread debugging using \[^ \r\n\]* enabled\\\]\r\n" { |
4645 | exp_continue | |
4646 | } | |
4647 | -re " is not a core dump:.*\r\n$gdb_prompt $" { | |
4f424bb1 | 4648 | fail "$test (bad file format)" |
fac51dd9 DE |
4649 | return -1 |
4650 | } | |
4651 | -re ": No such file or directory.*\r\n$gdb_prompt $" { | |
4f424bb1 | 4652 | fail "$test (file not found)" |
fac51dd9 DE |
4653 | return -1 |
4654 | } | |
4655 | -re "Couldn't find .* registers in core file.*\r\n$gdb_prompt $" { | |
4f424bb1 | 4656 | fail "$test (incomplete note section)" |
fac51dd9 DE |
4657 | return 0 |
4658 | } | |
4659 | -re "Core was generated by .*\r\n$gdb_prompt $" { | |
4f424bb1 | 4660 | pass "$test" |
fac51dd9 DE |
4661 | return 1 |
4662 | } | |
4663 | -re ".*$gdb_prompt $" { | |
4f424bb1 | 4664 | fail "$test" |
fac51dd9 DE |
4665 | return -1 |
4666 | } | |
4667 | timeout { | |
4f424bb1 | 4668 | fail "$test (timeout)" |
fac51dd9 DE |
4669 | return -1 |
4670 | } | |
4671 | } | |
4672 | fail "unsupported output from 'core' command" | |
4673 | return -1 | |
4674 | } | |
4675 | ||
759f0f0b PA |
4676 | # Return the filename to download to the target and load on the target |
4677 | # for this shared library. Normally just LIBNAME, unless shared libraries | |
4678 | # for this target have separate link and load images. | |
4679 | ||
4680 | proc shlib_target_file { libname } { | |
4681 | return $libname | |
4682 | } | |
4683 | ||
4684 | # Return the filename GDB will load symbols from when debugging this | |
4685 | # shared library. Normally just LIBNAME, unless shared libraries for | |
4686 | # this target have separate link and load images. | |
4687 | ||
4688 | proc shlib_symbol_file { libname } { | |
4689 | return $libname | |
4690 | } | |
4691 | ||
56744f0a JJ |
4692 | # Return the filename to download to the target and load for this |
4693 | # executable. Normally just BINFILE unless it is renamed to something | |
4694 | # else for this target. | |
4695 | ||
4696 | proc exec_target_file { binfile } { | |
4697 | return $binfile | |
4698 | } | |
4699 | ||
4700 | # Return the filename GDB will load symbols from when debugging this | |
4701 | # executable. Normally just BINFILE unless executables for this target | |
4702 | # have separate files for symbols. | |
4703 | ||
4704 | proc exec_symbol_file { binfile } { | |
4705 | return $binfile | |
4706 | } | |
4707 | ||
4708 | # Rename the executable file. Normally this is just BINFILE1 being renamed | |
4709 | # to BINFILE2, but some targets require multiple binary files. | |
4710 | proc gdb_rename_execfile { binfile1 binfile2 } { | |
faf067f1 JK |
4711 | file rename -force [exec_target_file ${binfile1}] \ |
4712 | [exec_target_file ${binfile2}] | |
56744f0a | 4713 | if { [exec_target_file ${binfile1}] != [exec_symbol_file ${binfile1}] } { |
faf067f1 JK |
4714 | file rename -force [exec_symbol_file ${binfile1}] \ |
4715 | [exec_symbol_file ${binfile2}] | |
56744f0a JJ |
4716 | } |
4717 | } | |
4718 | ||
4719 | # "Touch" the executable file to update the date. Normally this is just | |
4720 | # BINFILE, but some targets require multiple files. | |
4721 | proc gdb_touch_execfile { binfile } { | |
faf067f1 JK |
4722 | set time [clock seconds] |
4723 | file mtime [exec_target_file ${binfile}] $time | |
56744f0a | 4724 | if { [exec_target_file ${binfile}] != [exec_symbol_file ${binfile}] } { |
faf067f1 | 4725 | file mtime [exec_symbol_file ${binfile}] $time |
56744f0a JJ |
4726 | } |
4727 | } | |
4728 | ||
7817ea46 SM |
4729 | # Like remote_download but provides a gdb-specific behavior. |
4730 | # | |
4731 | # If the destination board is remote, the local file FROMFILE is transferred as | |
4732 | # usual with remote_download to TOFILE on the remote board. The destination | |
4733 | # filename is added to the CLEANFILES global, so it can be cleaned up at the | |
4734 | # end of the test. | |
4735 | # | |
4736 | # If the destination board is local, the destination path TOFILE is passed | |
4737 | # through standard_output_file, and FROMFILE is copied there. | |
4738 | # | |
4739 | # In both cases, if TOFILE is omitted, it defaults to the [file tail] of | |
4740 | # FROMFILE. | |
44ee8174 TT |
4741 | |
4742 | proc gdb_remote_download {dest fromfile {tofile {}}} { | |
7817ea46 SM |
4743 | # If TOFILE is not given, default to the same filename as FROMFILE. |
4744 | if {[string length $tofile] == 0} { | |
4745 | set tofile [file tail $fromfile] | |
44ee8174 | 4746 | } |
ce4ea2bb | 4747 | |
7817ea46 SM |
4748 | if {[is_remote $dest]} { |
4749 | # When the DEST is remote, we simply send the file to DEST. | |
4750 | global cleanfiles | |
44ee8174 | 4751 | |
7817ea46 SM |
4752 | set destname [remote_download $dest $fromfile $tofile] |
4753 | lappend cleanfiles $destname | |
93f02886 | 4754 | |
7817ea46 SM |
4755 | return $destname |
4756 | } else { | |
8392fa22 SM |
4757 | # When the DEST is local, we copy the file to the test directory (where |
4758 | # the executable is). | |
4759 | # | |
4760 | # Note that we pass TOFILE through standard_output_file, regardless of | |
4761 | # whether it is absolute or relative, because we don't want the tests | |
4762 | # to be able to write outside their standard output directory. | |
4763 | ||
7817ea46 | 4764 | set tofile [standard_output_file $tofile] |
93f02886 | 4765 | |
7817ea46 SM |
4766 | file copy -force $fromfile $tofile |
4767 | ||
4768 | return $tofile | |
4769 | } | |
93f02886 DJ |
4770 | } |
4771 | ||
d9019901 | 4772 | # gdb_load_shlib LIB... |
93f02886 | 4773 | # |
fca4cfd9 | 4774 | # Copy the listed library to the target. |
93f02886 | 4775 | |
d9019901 | 4776 | proc gdb_load_shlib { file } { |
c708f4d2 AB |
4777 | global gdb_spawn_id |
4778 | ||
4779 | if ![info exists gdb_spawn_id] { | |
4780 | perror "gdb_load_shlib: GDB is not running" | |
4781 | } | |
4782 | ||
fca4cfd9 | 4783 | set dest [gdb_remote_download target [shlib_target_file $file]] |
93f02886 | 4784 | |
6e774b13 SM |
4785 | if {[is_remote target]} { |
4786 | # If the target is remote, we need to tell gdb where to find the | |
4787 | # libraries. | |
4788 | # | |
4789 | # We could set this even when not testing remotely, but a user | |
4790 | # generally won't set it unless necessary. In order to make the tests | |
4791 | # more like the real-life scenarios, we don't set it for local testing. | |
fca4cfd9 | 4792 | gdb_test "set solib-search-path [file dirname $file]" "" "" |
6e774b13 | 4793 | } |
fca4cfd9 SM |
4794 | |
4795 | return $dest | |
93f02886 DJ |
4796 | } |
4797 | ||
c906108c | 4798 | # |
5b80f00d PA |
4799 | # gdb_load -- load a file into the debugger. Specifying no file |
4800 | # defaults to the executable currently being debugged. | |
7e60a48e | 4801 | # The return value is 0 for success, -1 for failure. |
2db8e78e | 4802 | # Many files in config/*.exp override this procedure. |
c906108c SS |
4803 | # |
4804 | proc gdb_load { arg } { | |
5b80f00d PA |
4805 | if { $arg != "" } { |
4806 | return [gdb_file_cmd $arg] | |
4807 | } | |
7e60a48e | 4808 | return 0 |
c906108c SS |
4809 | } |
4810 | ||
b741e217 DJ |
4811 | # gdb_reload -- load a file into the target. Called before "running", |
4812 | # either the first time or after already starting the program once, | |
4813 | # for remote targets. Most files that override gdb_load should now | |
4814 | # override this instead. | |
4815 | ||
4816 | proc gdb_reload { } { | |
4817 | # For the benefit of existing configurations, default to gdb_load. | |
4818 | # Specifying no file defaults to the executable currently being | |
4819 | # debugged. | |
4820 | return [gdb_load ""] | |
4821 | } | |
4822 | ||
c906108c SS |
4823 | proc gdb_continue { function } { |
4824 | global decimal | |
4825 | ||
ae59b1da | 4826 | return [gdb_test "continue" ".*Breakpoint $decimal, $function .*" "continue to $function"] |
c906108c SS |
4827 | } |
4828 | ||
73c9764f | 4829 | proc default_gdb_init { test_file_name } { |
277254ba | 4830 | global gdb_wrapper_initialized |
f6838f81 | 4831 | global gdb_wrapper_target |
0a6d0306 | 4832 | global gdb_test_file_name |
93f02886 | 4833 | global cleanfiles |
73c9764f | 4834 | global pf_prefix |
277254ba | 4835 | |
93f02886 DJ |
4836 | set cleanfiles {} |
4837 | ||
4ec70201 | 4838 | gdb_clear_suppressed |
c906108c | 4839 | |
73c9764f | 4840 | set gdb_test_file_name [file rootname [file tail $test_file_name]] |
0a6d0306 | 4841 | |
277254ba MS |
4842 | # Make sure that the wrapper is rebuilt |
4843 | # with the appropriate multilib option. | |
f6838f81 DJ |
4844 | if { $gdb_wrapper_target != [current_target_name] } { |
4845 | set gdb_wrapper_initialized 0 | |
4846 | } | |
277254ba | 4847 | |
7b433602 JB |
4848 | # Unlike most tests, we have a small number of tests that generate |
4849 | # a very large amount of output. We therefore increase the expect | |
ff604a67 MR |
4850 | # buffer size to be able to contain the entire test output. This |
4851 | # is especially needed by gdb.base/info-macros.exp. | |
4852 | match_max -d 65536 | |
8d417781 PM |
4853 | # Also set this value for the currently running GDB. |
4854 | match_max [match_max -d] | |
c906108c SS |
4855 | |
4856 | # We want to add the name of the TCL testcase to the PASS/FAIL messages. | |
73c9764f | 4857 | set pf_prefix "[file tail [file dirname $test_file_name]]/[file tail $test_file_name]:" |
c906108c | 4858 | |
4ec70201 | 4859 | global gdb_prompt |
c906108c | 4860 | if [target_info exists gdb_prompt] { |
4ec70201 | 4861 | set gdb_prompt [target_info gdb_prompt] |
c906108c SS |
4862 | } else { |
4863 | set gdb_prompt "\\(gdb\\)" | |
4864 | } | |
e11ac3a3 JK |
4865 | global use_gdb_stub |
4866 | if [info exists use_gdb_stub] { | |
4867 | unset use_gdb_stub | |
4868 | } | |
c906108c SS |
4869 | } |
4870 | ||
3d338901 DE |
4871 | # Return a path using GDB_PARALLEL. |
4872 | # ARGS is a list of path elements to append to "$objdir/$GDB_PARALLEL". | |
4873 | # GDB_PARALLEL must be defined, the caller must check. | |
4874 | # | |
4875 | # The default value for GDB_PARALLEL is, canonically, ".". | |
4876 | # The catch is that tests don't expect an additional "./" in file paths so | |
4877 | # omit any directory for the default case. | |
4878 | # GDB_PARALLEL is written as "yes" for the default case in Makefile.in to mark | |
4879 | # its special handling. | |
4880 | ||
4881 | proc make_gdb_parallel_path { args } { | |
4882 | global GDB_PARALLEL objdir | |
4883 | set joiner [list "file" "join" $objdir] | |
2151ccc5 | 4884 | if { [info exists GDB_PARALLEL] && $GDB_PARALLEL != "yes" } { |
3d338901 DE |
4885 | lappend joiner $GDB_PARALLEL |
4886 | } | |
4887 | set joiner [concat $joiner $args] | |
4888 | return [eval $joiner] | |
4889 | } | |
4890 | ||
0a6d0306 | 4891 | # Turn BASENAME into a full file name in the standard output |
8a3e1f8d TT |
4892 | # directory. It is ok if BASENAME is the empty string; in this case |
4893 | # the directory is returned. | |
0a6d0306 TT |
4894 | |
4895 | proc standard_output_file {basename} { | |
2151ccc5 | 4896 | global objdir subdir gdb_test_file_name |
0a6d0306 | 4897 | |
2151ccc5 SM |
4898 | set dir [make_gdb_parallel_path outputs $subdir $gdb_test_file_name] |
4899 | file mkdir $dir | |
97dd8e07 CB |
4900 | # If running on MinGW, replace /c/foo with c:/foo |
4901 | if { [ishost *-*-mingw*] } { | |
4902 | set dir [regsub {^/([a-z])/} $dir {\1:/}] | |
4903 | } | |
2151ccc5 | 4904 | return [file join $dir $basename] |
0a6d0306 TT |
4905 | } |
4906 | ||
f9e2e39d AH |
4907 | # Turn BASENAME into a full file name in the standard output directory. If |
4908 | # GDB has been launched more than once then append the count, starting with | |
4909 | # a ".1" postfix. | |
4910 | ||
4911 | proc standard_output_file_with_gdb_instance {basename} { | |
4912 | global gdb_instances | |
4913 | set count [expr $gdb_instances - 1 ] | |
4914 | ||
4915 | if {$count == 0} { | |
4916 | return [standard_output_file $basename] | |
4917 | } | |
4918 | return [standard_output_file ${basename}.${count}] | |
4919 | } | |
4920 | ||
4e234898 TT |
4921 | # Return the name of a file in our standard temporary directory. |
4922 | ||
4923 | proc standard_temp_file {basename} { | |
c4ef31bf SM |
4924 | # Since a particular runtest invocation is only executing a single test |
4925 | # file at any given time, we can use the runtest pid to build the | |
4926 | # path of the temp directory. | |
4927 | set dir [make_gdb_parallel_path temp [pid]] | |
4928 | file mkdir $dir | |
4929 | return [file join $dir $basename] | |
4e234898 TT |
4930 | } |
4931 | ||
436b5e99 TV |
4932 | # Rename file A to file B, if B does not already exists. Otherwise, leave B |
4933 | # as is and delete A. Return 1 if rename happened. | |
4934 | ||
4935 | proc tentative_rename { a b } { | |
4936 | global errorInfo errorCode | |
4937 | set code [catch {file rename -- $a $b} result] | |
4938 | if { $code == 1 && [lindex $errorCode 0] == "POSIX" \ | |
4939 | && [lindex $errorCode 1] == "EEXIST" } { | |
4940 | file delete $a | |
4941 | return 0 | |
4942 | } | |
4943 | if {$code == 1} { | |
4944 | return -code error -errorinfo $errorInfo -errorcode $errorCode $result | |
4945 | } elseif {$code > 1} { | |
4946 | return -code $code $result | |
4947 | } | |
4948 | return 1 | |
4949 | } | |
4950 | ||
4951 | # Create a file with name FILENAME and contents TXT in the cache directory. | |
4952 | # If EXECUTABLE, mark the new file for execution. | |
4953 | ||
4954 | proc cached_file { filename txt {executable 0}} { | |
4955 | set filename [make_gdb_parallel_path cache $filename] | |
4956 | ||
4957 | if { [file exists $filename] } { | |
4958 | return $filename | |
4959 | } | |
4960 | ||
4961 | set tmp_filename $filename.[pid] | |
4962 | set fd [open $tmp_filename w] | |
4963 | puts $fd $txt | |
4964 | close $fd | |
4965 | ||
4966 | if { $executable } { | |
4967 | exec chmod +x $tmp_filename | |
4968 | } | |
4969 | tentative_rename $tmp_filename $filename | |
4970 | ||
4971 | return $filename | |
4972 | } | |
4973 | ||
0a6d0306 TT |
4974 | # Set 'testfile', 'srcfile', and 'binfile'. |
4975 | # | |
4976 | # ARGS is a list of source file specifications. | |
4977 | # Without any arguments, the .exp file's base name is used to | |
4978 | # compute the source file name. The ".c" extension is added in this case. | |
4979 | # If ARGS is not empty, each entry is a source file specification. | |
4980 | # If the specification starts with a ".", it is treated as a suffix | |
4981 | # to append to the .exp file's base name. | |
4982 | # If the specification is the empty string, it is treated as if it | |
4983 | # were ".c". | |
4984 | # Otherwise it is a file name. | |
4985 | # The first file in the list is used to set the 'srcfile' global. | |
4986 | # Each subsequent name is used to set 'srcfile2', 'srcfile3', etc. | |
4987 | # | |
4988 | # Most tests should call this without arguments. | |
4989 | # | |
4990 | # If a completely different binary file name is needed, then it | |
4991 | # should be handled in the .exp file with a suitable comment. | |
4992 | ||
4993 | proc standard_testfile {args} { | |
4994 | global gdb_test_file_name | |
93c0ef37 | 4995 | global subdir |
686f09d0 | 4996 | global gdb_test_file_last_vars |
0a6d0306 TT |
4997 | |
4998 | # Outputs. | |
4999 | global testfile binfile | |
5000 | ||
5001 | set testfile $gdb_test_file_name | |
5002 | set binfile [standard_output_file ${testfile}] | |
5003 | ||
5004 | if {[llength $args] == 0} { | |
5005 | set args .c | |
5006 | } | |
5007 | ||
686f09d0 TT |
5008 | # Unset our previous output variables. |
5009 | # This can help catch hidden bugs. | |
5010 | if {[info exists gdb_test_file_last_vars]} { | |
5011 | foreach varname $gdb_test_file_last_vars { | |
5012 | global $varname | |
5013 | catch {unset $varname} | |
5014 | } | |
5015 | } | |
5016 | # 'executable' is often set by tests. | |
5017 | set gdb_test_file_last_vars {executable} | |
5018 | ||
0a6d0306 TT |
5019 | set suffix "" |
5020 | foreach arg $args { | |
5021 | set varname srcfile$suffix | |
5022 | global $varname | |
5023 | ||
5024 | # Handle an extension. | |
5025 | if {$arg == ""} { | |
5026 | set arg $testfile.c | |
5027 | } elseif {[string range $arg 0 0] == "."} { | |
5028 | set arg $testfile$arg | |
5029 | } | |
5030 | ||
5031 | set $varname $arg | |
686f09d0 | 5032 | lappend gdb_test_file_last_vars $varname |
0a6d0306 TT |
5033 | |
5034 | if {$suffix == ""} { | |
5035 | set suffix 2 | |
5036 | } else { | |
5037 | incr suffix | |
5038 | } | |
5039 | } | |
5040 | } | |
5041 | ||
7b356089 JB |
5042 | # The default timeout used when testing GDB commands. We want to use |
5043 | # the same timeout as the default dejagnu timeout, unless the user has | |
5044 | # already provided a specific value (probably through a site.exp file). | |
5045 | global gdb_test_timeout | |
5046 | if ![info exists gdb_test_timeout] { | |
5047 | set gdb_test_timeout $timeout | |
5048 | } | |
5049 | ||
47050449 JB |
5050 | # A list of global variables that GDB testcases should not use. |
5051 | # We try to prevent their use by monitoring write accesses and raising | |
5052 | # an error when that happens. | |
5053 | set banned_variables { bug_id prms_id } | |
5054 | ||
abcc4978 PA |
5055 | # A list of procedures that GDB testcases should not use. |
5056 | # We try to prevent their use by monitoring invocations and raising | |
5057 | # an error when that happens. | |
5058 | set banned_procedures { strace } | |
5059 | ||
41b2c92d PM |
5060 | # gdb_init is called by runtest at start, but also by several |
5061 | # tests directly; gdb_finish is only called from within runtest after | |
5062 | # each test source execution. | |
5063 | # Placing several traces by repetitive calls to gdb_init leads | |
5064 | # to problems, as only one trace is removed in gdb_finish. | |
5065 | # To overcome this possible problem, we add a variable that records | |
abcc4978 PA |
5066 | # if the banned variables and procedures are already traced. |
5067 | set banned_traced 0 | |
41b2c92d | 5068 | |
73c9764f | 5069 | proc gdb_init { test_file_name } { |
7b356089 JB |
5070 | # Reset the timeout value to the default. This way, any testcase |
5071 | # that changes the timeout value without resetting it cannot affect | |
5072 | # the timeout used in subsequent testcases. | |
5073 | global gdb_test_timeout | |
5074 | global timeout | |
5075 | set timeout $gdb_test_timeout | |
5076 | ||
8b696e31 YQ |
5077 | if { [regexp ".*gdb\.reverse\/.*" $test_file_name] |
5078 | && [target_info exists gdb_reverse_timeout] } { | |
5079 | set timeout [target_info gdb_reverse_timeout] | |
5080 | } | |
5081 | ||
5e92f71a TT |
5082 | # If GDB_INOTIFY is given, check for writes to '.'. This is a |
5083 | # debugging tool to help confirm that the test suite is | |
5084 | # parallel-safe. You need "inotifywait" from the | |
5085 | # inotify-tools package to use this. | |
5086 | global GDB_INOTIFY inotify_pid | |
5087 | if {[info exists GDB_INOTIFY] && ![info exists inotify_pid]} { | |
5088 | global outdir tool inotify_log_file | |
5089 | ||
5090 | set exclusions {outputs temp gdb[.](log|sum) cache} | |
5091 | set exclusion_re ([join $exclusions |]) | |
5092 | ||
5093 | set inotify_log_file [standard_temp_file inotify.out] | |
5094 | set inotify_pid [exec inotifywait -r -m -e move,create,delete . \ | |
5095 | --exclude $exclusion_re \ | |
5096 | |& tee -a $outdir/$tool.log $inotify_log_file &] | |
5097 | ||
5098 | # Wait for the watches; hopefully this is long enough. | |
5099 | sleep 2 | |
5100 | ||
5101 | # Clear the log so that we don't emit a warning the first time | |
5102 | # we check it. | |
5103 | set fd [open $inotify_log_file w] | |
5104 | close $fd | |
5105 | } | |
5106 | ||
abcc4978 PA |
5107 | # Block writes to all banned variables, and invocation of all |
5108 | # banned procedures... | |
47050449 | 5109 | global banned_variables |
abcc4978 PA |
5110 | global banned_procedures |
5111 | global banned_traced | |
5112 | if (!$banned_traced) { | |
41b2c92d PM |
5113 | foreach banned_var $banned_variables { |
5114 | global "$banned_var" | |
5115 | trace add variable "$banned_var" write error | |
5116 | } | |
abcc4978 PA |
5117 | foreach banned_proc $banned_procedures { |
5118 | global "$banned_proc" | |
5119 | trace add execution "$banned_proc" enter error | |
5120 | } | |
5121 | set banned_traced 1 | |
47050449 JB |
5122 | } |
5123 | ||
e7ab5e63 AB |
5124 | # We set LC_ALL, LC_CTYPE, and LANG to C so that we get the same |
5125 | # messages as expected. | |
c6f2ac43 | 5126 | setenv LC_ALL C |
e7ab5e63 | 5127 | setenv LC_CTYPE C |
c6f2ac43 PA |
5128 | setenv LANG C |
5129 | ||
e7ab5e63 AB |
5130 | # Don't let a .inputrc file or an existing setting of INPUTRC mess up |
5131 | # the test results. Even if /dev/null doesn't exist on the particular | |
5132 | # platform, the readline library will use the default setting just by | |
5133 | # failing to open the file. OTOH, opening /dev/null successfully will | |
5134 | # also result in the default settings being used since nothing will be | |
5135 | # read from this file. | |
5136 | setenv INPUTRC "/dev/null" | |
5137 | ||
9162a27c TT |
5138 | # This disables style output, which would interfere with many |
5139 | # tests. | |
5140 | setenv TERM "dumb" | |
e7ab5e63 | 5141 | |
63e163f2 AB |
5142 | # Ensure that GDBHISTFILE and GDBHISTSIZE are removed from the |
5143 | # environment, we don't want these modifications to the history | |
5144 | # settings. | |
5145 | unset -nocomplain ::env(GDBHISTFILE) | |
5146 | unset -nocomplain ::env(GDBHISTSIZE) | |
5147 | ||
bd447abb SM |
5148 | # Initialize GDB's pty with a fixed size, to make sure we avoid pagination |
5149 | # during startup. See "man expect" for details about stty_init. | |
5150 | global stty_init | |
5151 | set stty_init "rows 25 cols 80" | |
5152 | ||
e7ab5e63 | 5153 | # Some tests (for example gdb.base/maint.exp) shell out from gdb to use |
e4b8388f | 5154 | # grep. Clear GREP_OPTIONS to make the behavior predictable, |
e7ab5e63 AB |
5155 | # especially having color output turned on can cause tests to fail. |
5156 | setenv GREP_OPTIONS "" | |
5157 | ||
03f2bd59 JK |
5158 | # Clear $gdbserver_reconnect_p. |
5159 | global gdbserver_reconnect_p | |
5160 | set gdbserver_reconnect_p 1 | |
5161 | unset gdbserver_reconnect_p | |
5162 | ||
b2770d5e TV |
5163 | # Clear $last_loaded_file |
5164 | global last_loaded_file | |
5165 | unset -nocomplain last_loaded_file | |
5166 | ||
f9e2e39d AH |
5167 | # Reset GDB number of instances |
5168 | global gdb_instances | |
5169 | set gdb_instances 0 | |
5170 | ||
73c9764f | 5171 | return [default_gdb_init $test_file_name] |
c906108c SS |
5172 | } |
5173 | ||
5174 | proc gdb_finish { } { | |
a35cfb40 MR |
5175 | global gdbserver_reconnect_p |
5176 | global gdb_prompt | |
93f02886 DJ |
5177 | global cleanfiles |
5178 | ||
5179 | # Exit first, so that the files are no longer in use. | |
5180 | gdb_exit | |
5181 | ||
5182 | if { [llength $cleanfiles] > 0 } { | |
5183 | eval remote_file target delete $cleanfiles | |
5184 | set cleanfiles {} | |
5185 | } | |
47050449 JB |
5186 | |
5187 | # Unblock write access to the banned variables. Dejagnu typically | |
5188 | # resets some of them between testcases. | |
5189 | global banned_variables | |
abcc4978 PA |
5190 | global banned_procedures |
5191 | global banned_traced | |
5192 | if ($banned_traced) { | |
41b2c92d PM |
5193 | foreach banned_var $banned_variables { |
5194 | global "$banned_var" | |
5195 | trace remove variable "$banned_var" write error | |
5196 | } | |
abcc4978 PA |
5197 | foreach banned_proc $banned_procedures { |
5198 | global "$banned_proc" | |
5199 | trace remove execution "$banned_proc" enter error | |
5200 | } | |
5201 | set banned_traced 0 | |
47050449 | 5202 | } |
c906108c SS |
5203 | } |
5204 | ||
5205 | global debug_format | |
7a292a7a | 5206 | set debug_format "unknown" |
c906108c SS |
5207 | |
5208 | # Run the gdb command "info source" and extract the debugging format | |
5209 | # information from the output and save it in debug_format. | |
5210 | ||
5211 | proc get_debug_format { } { | |
5212 | global gdb_prompt | |
c906108c SS |
5213 | global expect_out |
5214 | global debug_format | |
5215 | ||
5216 | set debug_format "unknown" | |
5217 | send_gdb "info source\n" | |
5218 | gdb_expect 10 { | |
919d772c | 5219 | -re "Compiled with (.*) debugging format.\r\n.*$gdb_prompt $" { |
c906108c SS |
5220 | set debug_format $expect_out(1,string) |
5221 | verbose "debug format is $debug_format" | |
ae59b1da | 5222 | return 1 |
c906108c SS |
5223 | } |
5224 | -re "No current source file.\r\n$gdb_prompt $" { | |
5225 | perror "get_debug_format used when no current source file" | |
ae59b1da | 5226 | return 0 |
c906108c SS |
5227 | } |
5228 | -re "$gdb_prompt $" { | |
5229 | warning "couldn't check debug format (no valid response)." | |
ae59b1da | 5230 | return 1 |
c906108c SS |
5231 | } |
5232 | timeout { | |
975531db | 5233 | warning "couldn't check debug format (timeout)." |
ae59b1da | 5234 | return 1 |
c906108c SS |
5235 | } |
5236 | } | |
5237 | } | |
5238 | ||
838ae6c4 JB |
5239 | # Return true if FORMAT matches the debug format the current test was |
5240 | # compiled with. FORMAT is a shell-style globbing pattern; it can use | |
5241 | # `*', `[...]', and so on. | |
5242 | # | |
5243 | # This function depends on variables set by `get_debug_format', above. | |
5244 | ||
5245 | proc test_debug_format {format} { | |
5246 | global debug_format | |
5247 | ||
5248 | return [expr [string match $format $debug_format] != 0] | |
5249 | } | |
5250 | ||
c906108c SS |
5251 | # Like setup_xfail, but takes the name of a debug format (DWARF 1, |
5252 | # COFF, stabs, etc). If that format matches the format that the | |
5253 | # current test was compiled with, then the next test is expected to | |
5254 | # fail for any target. Returns 1 if the next test or set of tests is | |
5255 | # expected to fail, 0 otherwise (or if it is unknown). Must have | |
5256 | # previously called get_debug_format. | |
b55a4771 | 5257 | proc setup_xfail_format { format } { |
4ec70201 | 5258 | set ret [test_debug_format $format] |
b55a4771 | 5259 | |
838ae6c4 | 5260 | if {$ret} then { |
b55a4771 MS |
5261 | setup_xfail "*-*-*" |
5262 | } | |
ae59b1da | 5263 | return $ret |
b55a4771 | 5264 | } |
c906108c | 5265 | |
c6fee705 MC |
5266 | # gdb_get_line_number TEXT [FILE] |
5267 | # | |
5268 | # Search the source file FILE, and return the line number of the | |
0d7941a9 | 5269 | # first line containing TEXT. If no match is found, an error is thrown. |
c6fee705 MC |
5270 | # |
5271 | # TEXT is a string literal, not a regular expression. | |
5272 | # | |
5273 | # The default value of FILE is "$srcdir/$subdir/$srcfile". If FILE is | |
5274 | # specified, and does not start with "/", then it is assumed to be in | |
5275 | # "$srcdir/$subdir". This is awkward, and can be fixed in the future, | |
5276 | # by changing the callers and the interface at the same time. | |
5277 | # In particular: gdb.base/break.exp, gdb.base/condbreak.exp, | |
5278 | # gdb.base/ena-dis-br.exp. | |
5279 | # | |
5280 | # Use this function to keep your test scripts independent of the | |
5281 | # exact line numbering of the source file. Don't write: | |
5282 | # | |
5283 | # send_gdb "break 20" | |
5284 | # | |
5285 | # This means that if anyone ever edits your test's source file, | |
5286 | # your test could break. Instead, put a comment like this on the | |
5287 | # source file line you want to break at: | |
5288 | # | |
5289 | # /* breakpoint spot: frotz.exp: test name */ | |
5290 | # | |
5291 | # and then write, in your test script (which we assume is named | |
5292 | # frotz.exp): | |
5293 | # | |
5294 | # send_gdb "break [gdb_get_line_number "frotz.exp: test name"]\n" | |
5295 | # | |
5296 | # (Yes, Tcl knows how to handle the nested quotes and brackets. | |
5297 | # Try this: | |
5298 | # $ tclsh | |
5299 | # % puts "foo [lindex "bar baz" 1]" | |
5300 | # foo baz | |
5301 | # % | |
5302 | # Tcl is quite clever, for a little stringy language.) | |
5303 | # | |
5304 | # === | |
5305 | # | |
5306 | # The previous implementation of this procedure used the gdb search command. | |
5307 | # This version is different: | |
5308 | # | |
5309 | # . It works with MI, and it also works when gdb is not running. | |
5310 | # | |
5311 | # . It operates on the build machine, not the host machine. | |
5312 | # | |
5313 | # . For now, this implementation fakes a current directory of | |
5314 | # $srcdir/$subdir to be compatible with the old implementation. | |
5315 | # This will go away eventually and some callers will need to | |
5316 | # be changed. | |
5317 | # | |
5318 | # . The TEXT argument is literal text and matches literally, | |
5319 | # not a regular expression as it was before. | |
5320 | # | |
5321 | # . State changes in gdb, such as changing the current file | |
5322 | # and setting $_, no longer happen. | |
5323 | # | |
5324 | # After a bit of time we can forget about the differences from the | |
5325 | # old implementation. | |
5326 | # | |
5327 | # --chastain 2004-08-05 | |
5328 | ||
5329 | proc gdb_get_line_number { text { file "" } } { | |
5330 | global srcdir | |
5331 | global subdir | |
5332 | global srcfile | |
c906108c | 5333 | |
c6fee705 MC |
5334 | if { "$file" == "" } then { |
5335 | set file "$srcfile" | |
5336 | } | |
5337 | if { ! [regexp "^/" "$file"] } then { | |
5338 | set file "$srcdir/$subdir/$file" | |
c906108c SS |
5339 | } |
5340 | ||
c6fee705 | 5341 | if { [ catch { set fd [open "$file"] } message ] } then { |
0d7941a9 | 5342 | error "$message" |
c906108c | 5343 | } |
c6fee705 MC |
5344 | |
5345 | set found -1 | |
5346 | for { set line 1 } { 1 } { incr line } { | |
5347 | if { [ catch { set nchar [gets "$fd" body] } message ] } then { | |
0d7941a9 | 5348 | error "$message" |
c6fee705 MC |
5349 | } |
5350 | if { $nchar < 0 } then { | |
5351 | break | |
5352 | } | |
5353 | if { [string first "$text" "$body"] >= 0 } then { | |
5354 | set found $line | |
5355 | break | |
5356 | } | |
5357 | } | |
5358 | ||
5359 | if { [ catch { close "$fd" } message ] } then { | |
0d7941a9 KS |
5360 | error "$message" |
5361 | } | |
5362 | ||
5363 | if {$found == -1} { | |
5364 | error "undefined tag \"$text\"" | |
c6fee705 MC |
5365 | } |
5366 | ||
5367 | return $found | |
c906108c SS |
5368 | } |
5369 | ||
b477a5e6 PA |
5370 | # Continue the program until it ends. |
5371 | # | |
fda326dd TT |
5372 | # MSSG is the error message that gets printed. If not given, a |
5373 | # default is used. | |
5374 | # COMMAND is the command to invoke. If not given, "continue" is | |
5375 | # used. | |
eceb0c5f TT |
5376 | # ALLOW_EXTRA is a flag indicating whether the test should expect |
5377 | # extra output between the "Continuing." line and the program | |
5378 | # exiting. By default it is zero; if nonzero, any extra output | |
5379 | # is accepted. | |
fda326dd | 5380 | |
eceb0c5f | 5381 | proc gdb_continue_to_end {{mssg ""} {command continue} {allow_extra 0}} { |
e11ac3a3 | 5382 | global inferior_exited_re use_gdb_stub |
7a292a7a | 5383 | |
fda326dd TT |
5384 | if {$mssg == ""} { |
5385 | set text "continue until exit" | |
5386 | } else { | |
5387 | set text "continue until exit at $mssg" | |
5388 | } | |
eceb0c5f TT |
5389 | if {$allow_extra} { |
5390 | set extra ".*" | |
5391 | } else { | |
5392 | set extra "" | |
5393 | } | |
b477a5e6 PA |
5394 | |
5395 | # By default, we don't rely on exit() behavior of remote stubs -- | |
5396 | # it's common for exit() to be implemented as a simple infinite | |
5397 | # loop, or a forced crash/reset. For native targets, by default, we | |
5398 | # assume process exit is reported as such. If a non-reliable target | |
5399 | # is used, we set a breakpoint at exit, and continue to that. | |
5400 | if { [target_info exists exit_is_reliable] } { | |
5401 | set exit_is_reliable [target_info exit_is_reliable] | |
5402 | } else { | |
5403 | set exit_is_reliable [expr ! $use_gdb_stub] | |
5404 | } | |
5405 | ||
5406 | if { ! $exit_is_reliable } { | |
7a292a7a SS |
5407 | if {![gdb_breakpoint "exit"]} { |
5408 | return 0 | |
5409 | } | |
eceb0c5f | 5410 | gdb_test $command "Continuing..*Breakpoint .*exit.*" \ |
fda326dd | 5411 | $text |
7a292a7a SS |
5412 | } else { |
5413 | # Continue until we exit. Should not stop again. | |
5414 | # Don't bother to check the output of the program, that may be | |
5415 | # extremely tough for some remote systems. | |
eceb0c5f TT |
5416 | gdb_test $command \ |
5417 | "Continuing.\[\r\n0-9\]+${extra}(... EXIT code 0\[\r\n\]+|$inferior_exited_re normally).*"\ | |
fda326dd | 5418 | $text |
7a292a7a SS |
5419 | } |
5420 | } | |
5421 | ||
5422 | proc rerun_to_main {} { | |
e11ac3a3 | 5423 | global gdb_prompt use_gdb_stub |
7a292a7a | 5424 | |
e11ac3a3 | 5425 | if $use_gdb_stub { |
7a292a7a SS |
5426 | gdb_run_cmd |
5427 | gdb_expect { | |
5428 | -re ".*Breakpoint .*main .*$gdb_prompt $"\ | |
5429 | {pass "rerun to main" ; return 0} | |
5430 | -re "$gdb_prompt $"\ | |
5431 | {fail "rerun to main" ; return 0} | |
5432 | timeout {fail "(timeout) rerun to main" ; return 0} | |
5433 | } | |
5434 | } else { | |
5435 | send_gdb "run\n" | |
5436 | gdb_expect { | |
11350d2a | 5437 | -re "The program .* has been started already.*y or n. $" { |
f9e2e39d | 5438 | send_gdb "y\n" answer |
11350d2a CV |
5439 | exp_continue |
5440 | } | |
7a292a7a SS |
5441 | -re "Starting program.*$gdb_prompt $"\ |
5442 | {pass "rerun to main" ; return 0} | |
5443 | -re "$gdb_prompt $"\ | |
5444 | {fail "rerun to main" ; return 0} | |
5445 | timeout {fail "(timeout) rerun to main" ; return 0} | |
5446 | } | |
5447 | } | |
5448 | } | |
c906108c | 5449 | |
5a56d6a6 TV |
5450 | # Return true if EXECUTABLE contains a .gdb_index or .debug_names index section. |
5451 | ||
5452 | proc exec_has_index_section { executable } { | |
5453 | set readelf_program [gdb_find_readelf] | |
5454 | set res [catch {exec $readelf_program -S $executable \ | |
5455 | | grep -E "\.gdb_index|\.debug_names" }] | |
5456 | if { $res == 0 } { | |
5457 | return 1 | |
5458 | } | |
5459 | return 0 | |
5460 | } | |
5461 | ||
a80cf5d8 TV |
5462 | # Return list with major and minor version of readelf, or an empty list. |
5463 | gdb_caching_proc readelf_version { | |
5464 | set readelf_program [gdb_find_readelf] | |
5465 | set res [catch {exec $readelf_program --version} output] | |
5466 | if { $res != 0 } { | |
5467 | return [list] | |
5468 | } | |
5469 | set lines [split $output \n] | |
5470 | set line [lindex $lines 0] | |
5471 | set res [regexp {[ \t]+([0-9]+)[.]([0-9]+)[^ \t]*$} \ | |
5472 | $line dummy major minor] | |
5473 | if { $res != 1 } { | |
5474 | return [list] | |
5475 | } | |
5476 | return [list $major $minor] | |
5477 | } | |
5478 | ||
5479 | # Return 1 if readelf prints the PIE flag, 0 if is doesn't, and -1 if unknown. | |
5480 | proc readelf_prints_pie { } { | |
5481 | set version [readelf_version] | |
5482 | if { [llength $version] == 0 } { | |
5483 | return -1 | |
5484 | } | |
5485 | set major [lindex $version 0] | |
5486 | set minor [lindex $version 1] | |
5487 | # It would be better to construct a PIE executable and test if the PIE | |
5488 | # flag is printed by readelf, but we cannot reliably construct a PIE | |
5489 | # executable if the multilib_flags dictate otherwise | |
5490 | # (--target_board=unix/-no-pie/-fno-PIE). | |
5491 | return [version_at_least $major $minor 2 26] | |
5492 | } | |
5493 | ||
5494 | # Return 1 if EXECUTABLE is a Position Independent Executable, 0 if it is not, | |
5495 | # and -1 if unknown. | |
b13057d9 TV |
5496 | |
5497 | proc exec_is_pie { executable } { | |
a80cf5d8 TV |
5498 | set res [readelf_prints_pie] |
5499 | if { $res != 1 } { | |
5500 | return -1 | |
5501 | } | |
b13057d9 | 5502 | set readelf_program [gdb_find_readelf] |
a80cf5d8 TV |
5503 | set res [catch {exec $readelf_program -d $executable} output] |
5504 | if { $res != 0 } { | |
5505 | return -1 | |
5506 | } | |
5507 | set res [regexp -line {\(FLAGS_1\).*Flags:.* PIE($| )} $output] | |
5508 | if { $res == 1 } { | |
b13057d9 TV |
5509 | return 1 |
5510 | } | |
5511 | return 0 | |
5512 | } | |
5513 | ||
27aba047 YQ |
5514 | # Return true if a test should be skipped due to lack of floating |
5515 | # point support or GDB can't fetch the contents from floating point | |
5516 | # registers. | |
13a5e3b8 | 5517 | |
27aba047 | 5518 | gdb_caching_proc gdb_skip_float_test { |
13a5e3b8 | 5519 | if [target_info exists gdb,skip_float_tests] { |
ae59b1da | 5520 | return 1 |
13a5e3b8 | 5521 | } |
27aba047 YQ |
5522 | |
5523 | # There is an ARM kernel ptrace bug that hardware VFP registers | |
5524 | # are not updated after GDB ptrace set VFP registers. The bug | |
5525 | # was introduced by kernel commit 8130b9d7b9d858aa04ce67805e8951e3cb6e9b2f | |
5526 | # in 2012 and is fixed in e2dfb4b880146bfd4b6aa8e138c0205407cebbaf | |
5527 | # in May 2016. In other words, kernels older than 4.6.3, 4.4.14, | |
5528 | # 4.1.27, 3.18.36, and 3.14.73 have this bug. | |
5529 | # This kernel bug is detected by check how does GDB change the | |
5530 | # program result by changing one VFP register. | |
5531 | if { [istarget "arm*-*-linux*"] } { | |
5532 | ||
5533 | set compile_flags {debug nowarnings } | |
5534 | ||
5535 | # Set up, compile, and execute a test program having VFP | |
5536 | # operations. | |
5537 | set src [standard_temp_file arm_vfp[pid].c] | |
5538 | set exe [standard_temp_file arm_vfp[pid].x] | |
5539 | ||
5540 | gdb_produce_source $src { | |
5541 | int main() { | |
5542 | double d = 4.0; | |
5543 | int ret; | |
5544 | ||
5545 | asm ("vldr d0, [%0]" : : "r" (&d)); | |
5546 | asm ("vldr d1, [%0]" : : "r" (&d)); | |
5547 | asm (".global break_here\n" | |
5548 | "break_here:"); | |
5549 | asm ("vcmp.f64 d0, d1\n" | |
5550 | "vmrs APSR_nzcv, fpscr\n" | |
5551 | "bne L_value_different\n" | |
5552 | "movs %0, #0\n" | |
5553 | "b L_end\n" | |
5554 | "L_value_different:\n" | |
5555 | "movs %0, #1\n" | |
5556 | "L_end:\n" : "=r" (ret) :); | |
5557 | ||
5558 | /* Return $d0 != $d1. */ | |
5559 | return ret; | |
5560 | } | |
5561 | } | |
5562 | ||
5563 | verbose "compiling testfile $src" 2 | |
5564 | set lines [gdb_compile $src $exe executable $compile_flags] | |
5565 | file delete $src | |
5566 | ||
5567 | if ![string match "" $lines] then { | |
5568 | verbose "testfile compilation failed, returning 1" 2 | |
5569 | return 0 | |
5570 | } | |
5571 | ||
5572 | # No error message, compilation succeeded so now run it via gdb. | |
5573 | # Run the test up to 5 times to detect whether ptrace can | |
5574 | # correctly update VFP registers or not. | |
5575 | set skip_vfp_test 0 | |
5576 | for {set i 0} {$i < 5} {incr i} { | |
5577 | global gdb_prompt srcdir subdir | |
5578 | ||
5579 | gdb_exit | |
5580 | gdb_start | |
5581 | gdb_reinitialize_dir $srcdir/$subdir | |
5582 | gdb_load "$exe" | |
5583 | ||
5584 | runto_main | |
5585 | gdb_test "break *break_here" | |
5586 | gdb_continue_to_breakpoint "break_here" | |
5587 | ||
5588 | # Modify $d0 to a different value, so the exit code should | |
5589 | # be 1. | |
5590 | gdb_test "set \$d0 = 5.0" | |
5591 | ||
5592 | set test "continue to exit" | |
5593 | gdb_test_multiple "continue" "$test" { | |
5594 | -re "exited with code 01.*$gdb_prompt $" { | |
5595 | } | |
5596 | -re "exited normally.*$gdb_prompt $" { | |
5597 | # However, the exit code is 0. That means something | |
5598 | # wrong in setting VFP registers. | |
5599 | set skip_vfp_test 1 | |
5600 | break | |
5601 | } | |
5602 | } | |
5603 | } | |
5604 | ||
5605 | gdb_exit | |
5606 | remote_file build delete $exe | |
5607 | ||
5608 | return $skip_vfp_test | |
5609 | } | |
ae59b1da | 5610 | return 0 |
13a5e3b8 MS |
5611 | } |
5612 | ||
5613 | # Print a message and return true if a test should be skipped | |
5614 | # due to lack of stdio support. | |
5615 | ||
5616 | proc gdb_skip_stdio_test { msg } { | |
5617 | if [target_info exists gdb,noinferiorio] { | |
4ec70201 | 5618 | verbose "Skipping test '$msg': no inferior i/o." |
ae59b1da | 5619 | return 1 |
13a5e3b8 | 5620 | } |
ae59b1da | 5621 | return 0 |
13a5e3b8 MS |
5622 | } |
5623 | ||
5624 | proc gdb_skip_bogus_test { msg } { | |
ae59b1da | 5625 | return 0 |
13a5e3b8 MS |
5626 | } |
5627 | ||
e515b470 DJ |
5628 | # Return true if a test should be skipped due to lack of XML support |
5629 | # in the host GDB. | |
d0ef5df8 | 5630 | # NOTE: This must be called while gdb is *not* running. |
e515b470 | 5631 | |
17e1c970 | 5632 | gdb_caching_proc gdb_skip_xml_test { |
787f0025 | 5633 | global gdb_spawn_id |
e515b470 DJ |
5634 | global gdb_prompt |
5635 | global srcdir | |
e515b470 | 5636 | |
787f0025 MM |
5637 | if { [info exists gdb_spawn_id] } { |
5638 | error "GDB must not be running in gdb_skip_xml_tests." | |
5639 | } | |
5640 | ||
b22089ab YQ |
5641 | set xml_file [gdb_remote_download host "${srcdir}/gdb.xml/trivial.xml"] |
5642 | ||
e515b470 | 5643 | gdb_start |
17e1c970 | 5644 | set xml_missing 0 |
b22089ab | 5645 | gdb_test_multiple "set tdesc filename $xml_file" "" { |
e515b470 | 5646 | -re ".*XML support was disabled at compile time.*$gdb_prompt $" { |
17e1c970 | 5647 | set xml_missing 1 |
e515b470 DJ |
5648 | } |
5649 | -re ".*$gdb_prompt $" { } | |
5650 | } | |
5651 | gdb_exit | |
17e1c970 | 5652 | return $xml_missing |
e515b470 | 5653 | } |
1f8a6abb | 5654 | |
673dc4a0 YQ |
5655 | # Return true if argv[0] is available. |
5656 | ||
5657 | gdb_caching_proc gdb_has_argv0 { | |
5658 | set result 0 | |
5659 | ||
bf326452 AH |
5660 | # Compile and execute a test program to check whether argv[0] is available. |
5661 | gdb_simple_compile has_argv0 { | |
673dc4a0 YQ |
5662 | int main (int argc, char **argv) { |
5663 | return 0; | |
5664 | } | |
bf326452 | 5665 | } executable |
673dc4a0 | 5666 | |
673dc4a0 YQ |
5667 | |
5668 | # Helper proc. | |
5669 | proc gdb_has_argv0_1 { exe } { | |
5670 | global srcdir subdir | |
5671 | global gdb_prompt hex | |
5672 | ||
5673 | gdb_exit | |
5674 | gdb_start | |
5675 | gdb_reinitialize_dir $srcdir/$subdir | |
5676 | gdb_load "$exe" | |
5677 | ||
5678 | # Set breakpoint on main. | |
5679 | gdb_test_multiple "break main" "break main" { | |
5680 | -re "Breakpoint.*${gdb_prompt} $" { | |
5681 | } | |
5682 | -re "${gdb_prompt} $" { | |
5683 | return 0 | |
5684 | } | |
5685 | } | |
5686 | ||
5687 | # Run to main. | |
5688 | gdb_run_cmd | |
5689 | gdb_test_multiple "" "run to main" { | |
5690 | -re "Breakpoint.*${gdb_prompt} $" { | |
5691 | } | |
5692 | -re "${gdb_prompt} $" { | |
5693 | return 0 | |
5694 | } | |
5695 | } | |
5696 | ||
c0ecb95f JK |
5697 | set old_elements "200" |
5698 | set test "show print elements" | |
5699 | gdb_test_multiple $test $test { | |
5700 | -re "Limit on string chars or array elements to print is (\[^\r\n\]+)\\.\r\n$gdb_prompt $" { | |
5701 | set old_elements $expect_out(1,string) | |
5702 | } | |
5703 | } | |
5704 | set old_repeats "200" | |
5705 | set test "show print repeats" | |
5706 | gdb_test_multiple $test $test { | |
5707 | -re "Threshold for repeated print elements is (\[^\r\n\]+)\\.\r\n$gdb_prompt $" { | |
5708 | set old_repeats $expect_out(1,string) | |
5709 | } | |
5710 | } | |
5711 | gdb_test_no_output "set print elements unlimited" "" | |
5712 | gdb_test_no_output "set print repeats unlimited" "" | |
5713 | ||
5714 | set retval 0 | |
673dc4a0 YQ |
5715 | # Check whether argc is 1. |
5716 | gdb_test_multiple "p argc" "p argc" { | |
5717 | -re " = 1\r\n${gdb_prompt} $" { | |
5718 | ||
5719 | gdb_test_multiple "p argv\[0\]" "p argv\[0\]" { | |
5720 | -re " = $hex \".*[file tail $exe]\"\r\n${gdb_prompt} $" { | |
c0ecb95f | 5721 | set retval 1 |
673dc4a0 YQ |
5722 | } |
5723 | -re "${gdb_prompt} $" { | |
673dc4a0 YQ |
5724 | } |
5725 | } | |
5726 | } | |
5727 | -re "${gdb_prompt} $" { | |
673dc4a0 YQ |
5728 | } |
5729 | } | |
c0ecb95f JK |
5730 | |
5731 | gdb_test_no_output "set print elements $old_elements" "" | |
5732 | gdb_test_no_output "set print repeats $old_repeats" "" | |
5733 | ||
5734 | return $retval | |
673dc4a0 YQ |
5735 | } |
5736 | ||
bf326452 | 5737 | set result [gdb_has_argv0_1 $obj] |
673dc4a0 YQ |
5738 | |
5739 | gdb_exit | |
bf326452 | 5740 | file delete $obj |
673dc4a0 YQ |
5741 | |
5742 | if { !$result | |
5743 | && ([istarget *-*-linux*] | |
5744 | || [istarget *-*-freebsd*] || [istarget *-*-kfreebsd*] | |
5745 | || [istarget *-*-netbsd*] || [istarget *-*-knetbsd*] | |
5746 | || [istarget *-*-openbsd*] | |
5747 | || [istarget *-*-darwin*] | |
5748 | || [istarget *-*-solaris*] | |
5749 | || [istarget *-*-aix*] | |
5750 | || [istarget *-*-gnu*] | |
5751 | || [istarget *-*-cygwin*] || [istarget *-*-mingw32*] | |
5752 | || [istarget *-*-*djgpp*] || [istarget *-*-go32*] | |
5753 | || [istarget *-wince-pe] || [istarget *-*-mingw32ce*] | |
5754 | || [istarget *-*-symbianelf*] | |
5755 | || [istarget *-*-osf*] | |
673dc4a0 YQ |
5756 | || [istarget *-*-dicos*] |
5757 | || [istarget *-*-nto*] | |
5758 | || [istarget *-*-*vms*] | |
5759 | || [istarget *-*-lynx*178]) } { | |
5760 | fail "argv\[0\] should be available on this target" | |
5761 | } | |
5762 | ||
5763 | return $result | |
5764 | } | |
5765 | ||
1f8a6abb EZ |
5766 | # Note: the procedure gdb_gnu_strip_debug will produce an executable called |
5767 | # ${binfile}.dbglnk, which is just like the executable ($binfile) but without | |
5768 | # the debuginfo. Instead $binfile has a .gnu_debuglink section which contains | |
8e1d0c49 JK |
5769 | # the name of a debuginfo only file. This file will be stored in the same |
5770 | # subdirectory. | |
1f8a6abb EZ |
5771 | |
5772 | # Functions for separate debug info testing | |
5773 | ||
5774 | # starting with an executable: | |
5775 | # foo --> original executable | |
5776 | ||
5777 | # at the end of the process we have: | |
5778 | # foo.stripped --> foo w/o debug info | |
8e1d0c49 | 5779 | # foo.debug --> foo's debug info |
1f8a6abb EZ |
5780 | # foo --> like foo, but with a new .gnu_debuglink section pointing to foo.debug. |
5781 | ||
7c50a931 DE |
5782 | # Fetch the build id from the file. |
5783 | # Returns "" if there is none. | |
5784 | ||
5785 | proc get_build_id { filename } { | |
c74f7d1c JT |
5786 | if { ([istarget "*-*-mingw*"] |
5787 | || [istarget *-*-cygwin*]) } { | |
5788 | set objdump_program [gdb_find_objdump] | |
5789 | set result [catch {set data [exec $objdump_program -p $filename | grep signature | cut "-d " -f4]} output] | |
5790 | verbose "result is $result" | |
5791 | verbose "output is $output" | |
5792 | if {$result == 1} { | |
5793 | return "" | |
5794 | } | |
5795 | return $data | |
92046791 | 5796 | } else { |
c74f7d1c JT |
5797 | set tmp [standard_output_file "${filename}-tmp"] |
5798 | set objcopy_program [gdb_find_objcopy] | |
5799 | set result [catch "exec $objcopy_program -j .note.gnu.build-id -O binary $filename $tmp" output] | |
5800 | verbose "result is $result" | |
5801 | verbose "output is $output" | |
5802 | if {$result == 1} { | |
5803 | return "" | |
5804 | } | |
5805 | set fi [open $tmp] | |
5806 | fconfigure $fi -translation binary | |
5807 | # Skip the NOTE header. | |
5808 | read $fi 16 | |
5809 | set data [read $fi] | |
5810 | close $fi | |
5811 | file delete $tmp | |
5812 | if ![string compare $data ""] then { | |
5813 | return "" | |
5814 | } | |
5815 | # Convert it to hex. | |
5816 | binary scan $data H* data | |
5817 | return $data | |
4935890f | 5818 | } |
7c50a931 DE |
5819 | } |
5820 | ||
5821 | # Return the build-id hex string (usually 160 bits as 40 hex characters) | |
5822 | # converted to the form: .build-id/ab/cdef1234...89.debug | |
5823 | # Return "" if no build-id found. | |
5824 | proc build_id_debug_filename_get { filename } { | |
5825 | set data [get_build_id $filename] | |
5826 | if { $data == "" } { | |
5827 | return "" | |
5828 | } | |
061b5285 | 5829 | regsub {^..} $data {\0/} data |
ae59b1da | 5830 | return ".build-id/${data}.debug" |
4935890f JK |
5831 | } |
5832 | ||
94277a38 DJ |
5833 | # Create stripped files for DEST, replacing it. If ARGS is passed, it is a |
5834 | # list of optional flags. The only currently supported flag is no-main, | |
5835 | # which removes the symbol entry for main from the separate debug file. | |
c0201579 JK |
5836 | # |
5837 | # Function returns zero on success. Function will return non-zero failure code | |
5838 | # on some targets not supporting separate debug info (such as i386-msdos). | |
1f8a6abb | 5839 | |
94277a38 DJ |
5840 | proc gdb_gnu_strip_debug { dest args } { |
5841 | ||
8e1d0c49 JK |
5842 | # Use the first separate debug info file location searched by GDB so the |
5843 | # run cannot be broken by some stale file searched with higher precedence. | |
5844 | set debug_file "${dest}.debug" | |
5845 | ||
b741e217 | 5846 | set strip_to_file_program [transform strip] |
4fa7d390 | 5847 | set objcopy_program [gdb_find_objcopy] |
1f8a6abb | 5848 | |
1f8a6abb EZ |
5849 | set debug_link [file tail $debug_file] |
5850 | set stripped_file "${dest}.stripped" | |
5851 | ||
5852 | # Get rid of the debug info, and store result in stripped_file | |
5853 | # something like gdb/testsuite/gdb.base/blah.stripped. | |
5854 | set result [catch "exec $strip_to_file_program --strip-debug ${dest} -o ${stripped_file}" output] | |
5855 | verbose "result is $result" | |
5856 | verbose "output is $output" | |
5857 | if {$result == 1} { | |
5858 | return 1 | |
5859 | } | |
5860 | ||
d521f563 JK |
5861 | # Workaround PR binutils/10802: |
5862 | # Preserve the 'x' bit also for PIEs (Position Independent Executables). | |
5863 | set perm [file attributes ${dest} -permissions] | |
5864 | file attributes ${stripped_file} -permissions $perm | |
5865 | ||
1f8a6abb EZ |
5866 | # Get rid of everything but the debug info, and store result in debug_file |
5867 | # This will be in the .debug subdirectory, see above. | |
5868 | set result [catch "exec $strip_to_file_program --only-keep-debug ${dest} -o ${debug_file}" output] | |
5869 | verbose "result is $result" | |
5870 | verbose "output is $output" | |
5871 | if {$result == 1} { | |
5872 | return 1 | |
5873 | } | |
5874 | ||
94277a38 DJ |
5875 | # If no-main is passed, strip the symbol for main from the separate |
5876 | # file. This is to simulate the behavior of elfutils's eu-strip, which | |
5877 | # leaves the symtab in the original file only. There's no way to get | |
5878 | # objcopy or strip to remove the symbol table without also removing the | |
5879 | # debugging sections, so this is as close as we can get. | |
5880 | if { [llength $args] == 1 && [lindex $args 0] == "no-main" } { | |
5881 | set result [catch "exec $objcopy_program -N main ${debug_file} ${debug_file}-tmp" output] | |
5882 | verbose "result is $result" | |
5883 | verbose "output is $output" | |
5884 | if {$result == 1} { | |
5885 | return 1 | |
5886 | } | |
5887 | file delete "${debug_file}" | |
5888 | file rename "${debug_file}-tmp" "${debug_file}" | |
5889 | } | |
5890 | ||
1f8a6abb EZ |
5891 | # Link the two previous output files together, adding the .gnu_debuglink |
5892 | # section to the stripped_file, containing a pointer to the debug_file, | |
5893 | # save the new file in dest. | |
5894 | # This will be the regular executable filename, in the usual location. | |
5895 | set result [catch "exec $objcopy_program --add-gnu-debuglink=${debug_file} ${stripped_file} ${dest}" output] | |
5896 | verbose "result is $result" | |
5897 | verbose "output is $output" | |
5898 | if {$result == 1} { | |
5899 | return 1 | |
5900 | } | |
5901 | ||
d521f563 JK |
5902 | # Workaround PR binutils/10802: |
5903 | # Preserve the 'x' bit also for PIEs (Position Independent Executables). | |
5904 | set perm [file attributes ${stripped_file} -permissions] | |
5905 | file attributes ${dest} -permissions $perm | |
5906 | ||
5907 | return 0 | |
1f8a6abb EZ |
5908 | } |
5909 | ||
d8295fe9 VP |
5910 | # Test the output of GDB_COMMAND matches the pattern obtained |
5911 | # by concatenating all elements of EXPECTED_LINES. This makes | |
5912 | # it possible to split otherwise very long string into pieces. | |
206584bd | 5913 | # If third argument TESTNAME is not empty, it's used as the name of the |
d8295fe9 | 5914 | # test to be printed on pass/fail. |
206584bd | 5915 | proc help_test_raw { gdb_command expected_lines {testname {}} } { |
d8295fe9 | 5916 | set expected_output [join $expected_lines ""] |
d1e36019 TV |
5917 | if {$testname != {}} { |
5918 | gdb_test "${gdb_command}" "${expected_output}" $testname | |
5919 | return | |
5920 | } | |
5921 | ||
5922 | gdb_test "${gdb_command}" "${expected_output}" | |
d8295fe9 VP |
5923 | } |
5924 | ||
206584bd PW |
5925 | # A regexp that matches the end of help CLASS|PREFIX_COMMAND |
5926 | set help_list_trailer { | |
5927 | "Type \"apropos word\" to search for commands related to \"word\"\.[\r\n]+" | |
5928 | "Type \"apropos -v word\" for full documentation of commands related to \"word\"\.[\r\n]+" | |
5929 | "Command name abbreviations are allowed if unambiguous\." | |
5930 | } | |
5931 | ||
5932 | # Test the output of "help COMMAND_CLASS". EXPECTED_INITIAL_LINES | |
d8295fe9 | 5933 | # are regular expressions that should match the beginning of output, |
206584bd PW |
5934 | # before the list of commands in that class. |
5935 | # LIST_OF_COMMANDS are regular expressions that should match the | |
5936 | # list of commands in that class. If empty, the command list will be | |
5937 | # matched automatically. The presence of standard epilogue will be tested | |
5938 | # automatically. | |
5939 | # If last argument TESTNAME is not empty, it's used as the name of the | |
5940 | # test to be printed on pass/fail. | |
06f810bd MG |
5941 | # Notice that the '[' and ']' characters don't need to be escaped for strings |
5942 | # wrapped in {} braces. | |
206584bd PW |
5943 | proc test_class_help { command_class expected_initial_lines {list_of_commands {}} {testname {}} } { |
5944 | global help_list_trailer | |
5945 | if {[llength $list_of_commands]>0} { | |
5946 | set l_list_of_commands {"List of commands:[\r\n]+[\r\n]+"} | |
5947 | set l_list_of_commands [concat $l_list_of_commands $list_of_commands] | |
5948 | set l_list_of_commands [concat $l_list_of_commands {"[\r\n]+[\r\n]+"}] | |
5949 | } else { | |
5950 | set l_list_of_commands {"List of commands\:.*[\r\n]+"} | |
5951 | } | |
d8295fe9 | 5952 | set l_stock_body { |
06f810bd | 5953 | "Type \"help\" followed by command name for full documentation\.[\r\n]+" |
d8295fe9 | 5954 | } |
206584bd PW |
5955 | set l_entire_body [concat $expected_initial_lines $l_list_of_commands \ |
5956 | $l_stock_body $help_list_trailer] | |
d8295fe9 | 5957 | |
206584bd | 5958 | help_test_raw "help ${command_class}" $l_entire_body $testname |
d8295fe9 VP |
5959 | } |
5960 | ||
206584bd PW |
5961 | # Like test_class_help but specialised to test "help user-defined". |
5962 | proc test_user_defined_class_help { {list_of_commands {}} {testname {}} } { | |
5963 | test_class_help "user-defined" { | |
5964 | "User-defined commands\.[\r\n]+" | |
5965 | "The commands in this class are those defined by the user\.[\r\n]+" | |
5966 | "Use the \"define\" command to define a command\.[\r\n]+" | |
5967 | } $list_of_commands $testname | |
5968 | } | |
5969 | ||
5970 | ||
d8295fe9 VP |
5971 | # COMMAND_LIST should have either one element -- command to test, or |
5972 | # two elements -- abbreviated command to test, and full command the first | |
5973 | # element is abbreviation of. | |
5974 | # The command must be a prefix command. EXPECTED_INITIAL_LINES | |
5975 | # are regular expressions that should match the beginning of output, | |
5976 | # before the list of subcommands. The presence of | |
5977 | # subcommand list and standard epilogue will be tested automatically. | |
5978 | proc test_prefix_command_help { command_list expected_initial_lines args } { | |
206584bd | 5979 | global help_list_trailer |
d8295fe9 VP |
5980 | set command [lindex $command_list 0] |
5981 | if {[llength $command_list]>1} { | |
5982 | set full_command [lindex $command_list 1] | |
5983 | } else { | |
5984 | set full_command $command | |
5985 | } | |
5986 | # Use 'list' and not just {} because we want variables to | |
5987 | # be expanded in this list. | |
5988 | set l_stock_body [list\ | |
5989 | "List of $full_command subcommands\:.*\[\r\n\]+"\ | |
206584bd PW |
5990 | "Type \"help $full_command\" followed by $full_command subcommand name for full documentation\.\[\r\n\]+"] |
5991 | set l_entire_body [concat $expected_initial_lines $l_stock_body $help_list_trailer] | |
d8295fe9 VP |
5992 | if {[llength $args]>0} { |
5993 | help_test_raw "help ${command}" $l_entire_body [lindex $args 0] | |
5994 | } else { | |
5995 | help_test_raw "help ${command}" $l_entire_body | |
5996 | } | |
5997 | } | |
dbc52822 | 5998 | |
85b4440a TT |
5999 | # Build executable named EXECUTABLE from specifications that allow |
6000 | # different options to be passed to different sub-compilations. | |
6001 | # TESTNAME is the name of the test; this is passed to 'untested' if | |
6002 | # something fails. | |
a0d3f2f5 SCR |
6003 | # OPTIONS is passed to the final link, using gdb_compile. If OPTIONS |
6004 | # contains the option "pthreads", then gdb_compile_pthreads is used. | |
85b4440a TT |
6005 | # ARGS is a flat list of source specifications, of the form: |
6006 | # { SOURCE1 OPTIONS1 [ SOURCE2 OPTIONS2 ]... } | |
6007 | # Each SOURCE is compiled to an object file using its OPTIONS, | |
6008 | # using gdb_compile. | |
6009 | # Returns 0 on success, -1 on failure. | |
6010 | proc build_executable_from_specs {testname executable options args} { | |
dbc52822 VP |
6011 | global subdir |
6012 | global srcdir | |
dbc52822 | 6013 | |
0a6d0306 | 6014 | set binfile [standard_output_file $executable] |
dbc52822 | 6015 | |
fd961404 DE |
6016 | set info_options "" |
6017 | if { [lsearch -exact $options "c++"] >= 0 } { | |
6018 | set info_options "c++" | |
6019 | } | |
4c93b1db | 6020 | if [get_compiler_info ${info_options}] { |
dbc52822 VP |
6021 | return -1 |
6022 | } | |
a29a3fb7 | 6023 | |
a29a3fb7 | 6024 | set func gdb_compile |
26b911fb | 6025 | set func_index [lsearch -regexp $options {^(pthreads|shlib|shlib_pthreads|openmp)$}] |
a29a3fb7 GB |
6026 | if {$func_index != -1} { |
6027 | set func "${func}_[lindex $options $func_index]" | |
6028 | } | |
6029 | ||
6030 | # gdb_compile_shlib and gdb_compile_shlib_pthreads do not use the 3rd | |
6031 | # parameter. They also requires $sources while gdb_compile and | |
6032 | # gdb_compile_pthreads require $objects. Moreover they ignore any options. | |
6033 | if [string match gdb_compile_shlib* $func] { | |
6034 | set sources_path {} | |
6035 | foreach {s local_options} $args { | |
0e5c4555 AA |
6036 | if { [regexp "^/" "$s"] } then { |
6037 | lappend sources_path "$s" | |
6038 | } else { | |
6039 | lappend sources_path "$srcdir/$subdir/$s" | |
6040 | } | |
a29a3fb7 GB |
6041 | } |
6042 | set ret [$func $sources_path "${binfile}" $options] | |
67218854 TT |
6043 | } elseif {[lsearch -exact $options rust] != -1} { |
6044 | set sources_path {} | |
6045 | foreach {s local_options} $args { | |
6046 | if { [regexp "^/" "$s"] } then { | |
6047 | lappend sources_path "$s" | |
6048 | } else { | |
6049 | lappend sources_path "$srcdir/$subdir/$s" | |
6050 | } | |
6051 | } | |
6052 | set ret [gdb_compile_rust $sources_path "${binfile}" $options] | |
a29a3fb7 GB |
6053 | } else { |
6054 | set objects {} | |
6055 | set i 0 | |
6056 | foreach {s local_options} $args { | |
0e5c4555 AA |
6057 | if { ! [regexp "^/" "$s"] } then { |
6058 | set s "$srcdir/$subdir/$s" | |
6059 | } | |
26b911fb | 6060 | if { [$func "${s}" "${binfile}${i}.o" object $local_options] != "" } { |
a29a3fb7 GB |
6061 | untested $testname |
6062 | return -1 | |
6063 | } | |
6064 | lappend objects "${binfile}${i}.o" | |
6065 | incr i | |
6066 | } | |
6067 | set ret [$func $objects "${binfile}" executable $options] | |
6068 | } | |
6069 | if { $ret != "" } { | |
6070 | untested $testname | |
6071 | return -1 | |
6072 | } | |
6073 | ||
dbc52822 VP |
6074 | return 0 |
6075 | } | |
6076 | ||
85b4440a TT |
6077 | # Build executable named EXECUTABLE, from SOURCES. If SOURCES are not |
6078 | # provided, uses $EXECUTABLE.c. The TESTNAME paramer is the name of test | |
6079 | # to pass to untested, if something is wrong. OPTIONS are passed | |
6080 | # to gdb_compile directly. | |
6081 | proc build_executable { testname executable {sources ""} {options {debug}} } { | |
6082 | if {[llength $sources]==0} { | |
6083 | set sources ${executable}.c | |
6084 | } | |
6085 | ||
6086 | set arglist [list $testname $executable $options] | |
6087 | foreach source $sources { | |
6088 | lappend arglist $source $options | |
6089 | } | |
6090 | ||
6091 | return [eval build_executable_from_specs $arglist] | |
6092 | } | |
6093 | ||
7b606f95 DE |
6094 | # Starts fresh GDB binary and loads an optional executable into GDB. |
6095 | # Usage: clean_restart [executable] | |
6096 | # EXECUTABLE is the basename of the binary. | |
6097 | ||
6098 | proc clean_restart { args } { | |
dbc52822 | 6099 | global srcdir |
dbc52822 | 6100 | global subdir |
7b606f95 DE |
6101 | |
6102 | if { [llength $args] > 1 } { | |
6103 | error "bad number of args: [llength $args]" | |
6104 | } | |
dbc52822 VP |
6105 | |
6106 | gdb_exit | |
6107 | gdb_start | |
6108 | gdb_reinitialize_dir $srcdir/$subdir | |
7b606f95 DE |
6109 | |
6110 | if { [llength $args] >= 1 } { | |
6111 | set executable [lindex $args 0] | |
6112 | set binfile [standard_output_file ${executable}] | |
6113 | gdb_load ${binfile} | |
6114 | } | |
dbc52822 VP |
6115 | } |
6116 | ||
85b4440a TT |
6117 | # Prepares for testing by calling build_executable_full, then |
6118 | # clean_restart. | |
6119 | # TESTNAME is the name of the test. | |
6120 | # Each element in ARGS is a list of the form | |
6121 | # { EXECUTABLE OPTIONS SOURCE_SPEC... } | |
6122 | # These are passed to build_executable_from_specs, which see. | |
6123 | # The last EXECUTABLE is passed to clean_restart. | |
6124 | # Returns 0 on success, non-zero on failure. | |
6125 | proc prepare_for_testing_full {testname args} { | |
6126 | foreach spec $args { | |
6127 | if {[eval build_executable_from_specs [list $testname] $spec] == -1} { | |
6128 | return -1 | |
6129 | } | |
6130 | set executable [lindex $spec 0] | |
6131 | } | |
6132 | clean_restart $executable | |
6133 | return 0 | |
6134 | } | |
6135 | ||
dbc52822 VP |
6136 | # Prepares for testing, by calling build_executable, and then clean_restart. |
6137 | # Please refer to build_executable for parameter description. | |
6138 | proc prepare_for_testing { testname executable {sources ""} {options {debug}}} { | |
6139 | ||
734a5c36 | 6140 | if {[build_executable $testname $executable $sources $options] == -1} { |
dbc52822 VP |
6141 | return -1 |
6142 | } | |
6143 | clean_restart $executable | |
6144 | ||
6145 | return 0 | |
6146 | } | |
7065b901 | 6147 | |
0efcde63 AK |
6148 | # Retrieve the value of EXP in the inferior, represented in format |
6149 | # specified in FMT (using "printFMT"). DEFAULT is used as fallback if | |
6150 | # print fails. TEST is the test message to use. It can be omitted, | |
6151 | # in which case a test message is built from EXP. | |
6152 | ||
6153 | proc get_valueof { fmt exp default {test ""} } { | |
7065b901 TT |
6154 | global gdb_prompt |
6155 | ||
0efcde63 AK |
6156 | if {$test == "" } { |
6157 | set test "get valueof \"${exp}\"" | |
6158 | } | |
6159 | ||
7065b901 TT |
6160 | set val ${default} |
6161 | gdb_test_multiple "print${fmt} ${exp}" "$test" { | |
c2c2dd9f | 6162 | -re "\\$\[0-9\]* = (\[^\r\n\]*)\[\r\n\]*$gdb_prompt $" { |
417e16e2 | 6163 | set val $expect_out(1,string) |
1443936e | 6164 | pass "$test" |
417e16e2 PM |
6165 | } |
6166 | timeout { | |
6167 | fail "$test (timeout)" | |
6168 | } | |
6169 | } | |
6170 | return ${val} | |
6171 | } | |
6172 | ||
0efcde63 AK |
6173 | # Retrieve the value of EXP in the inferior, as a signed decimal value |
6174 | # (using "print /d"). DEFAULT is used as fallback if print fails. | |
6175 | # TEST is the test message to use. It can be omitted, in which case | |
6176 | # a test message is built from EXP. | |
6177 | ||
6178 | proc get_integer_valueof { exp default {test ""} } { | |
417e16e2 PM |
6179 | global gdb_prompt |
6180 | ||
0efcde63 AK |
6181 | if {$test == ""} { |
6182 | set test "get integer valueof \"${exp}\"" | |
6183 | } | |
6184 | ||
417e16e2 PM |
6185 | set val ${default} |
6186 | gdb_test_multiple "print /d ${exp}" "$test" { | |
7065b901 TT |
6187 | -re "\\$\[0-9\]* = (\[-\]*\[0-9\]*).*$gdb_prompt $" { |
6188 | set val $expect_out(1,string) | |
2f20e312 | 6189 | pass "$test" |
7065b901 TT |
6190 | } |
6191 | timeout { | |
417e16e2 | 6192 | fail "$test (timeout)" |
7065b901 TT |
6193 | } |
6194 | } | |
6195 | return ${val} | |
6196 | } | |
6197 | ||
20aa2c60 PA |
6198 | # Retrieve the value of EXP in the inferior, as an hexadecimal value |
6199 | # (using "print /x"). DEFAULT is used as fallback if print fails. | |
0efcde63 | 6200 | # TEST is the test message to use. It can be omitted, in which case |
20aa2c60 PA |
6201 | # a test message is built from EXP. |
6202 | ||
6203 | proc get_hexadecimal_valueof { exp default {test ""} } { | |
faafb047 | 6204 | global gdb_prompt |
20aa2c60 PA |
6205 | |
6206 | if {$test == ""} { | |
6207 | set test "get hexadecimal valueof \"${exp}\"" | |
6208 | } | |
6209 | ||
6210 | set val ${default} | |
6211 | gdb_test_multiple "print /x ${exp}" $test { | |
faafb047 PM |
6212 | -re "\\$\[0-9\]* = (0x\[0-9a-zA-Z\]+).*$gdb_prompt $" { |
6213 | set val $expect_out(1,string) | |
6214 | pass "$test" | |
6215 | } | |
faafb047 PM |
6216 | } |
6217 | return ${val} | |
6218 | } | |
417e16e2 | 6219 | |
0efcde63 AK |
6220 | # Retrieve the size of TYPE in the inferior, as a decimal value. DEFAULT |
6221 | # is used as fallback if print fails. TEST is the test message to use. | |
6222 | # It can be omitted, in which case a test message is 'sizeof (TYPE)'. | |
6223 | ||
6224 | proc get_sizeof { type default {test ""} } { | |
6225 | return [get_integer_valueof "sizeof (${type})" $default $test] | |
7065b901 TT |
6226 | } |
6227 | ||
ed3ef339 DE |
6228 | proc get_target_charset { } { |
6229 | global gdb_prompt | |
6230 | ||
6231 | gdb_test_multiple "show target-charset" "" { | |
6232 | -re "The target character set is \"auto; currently (\[^\"\]*)\".*$gdb_prompt $" { | |
6233 | return $expect_out(1,string) | |
6234 | } | |
6235 | -re "The target character set is \"(\[^\"\]*)\".*$gdb_prompt $" { | |
6236 | return $expect_out(1,string) | |
6237 | } | |
6238 | } | |
6239 | ||
6240 | # Pick a reasonable default. | |
6241 | warning "Unable to read target-charset." | |
6242 | return "UTF-8" | |
6243 | } | |
6244 | ||
5ad9dba7 YQ |
6245 | # Get the address of VAR. |
6246 | ||
6247 | proc get_var_address { var } { | |
6248 | global gdb_prompt hex | |
6249 | ||
6250 | # Match output like: | |
6251 | # $1 = (int *) 0x0 | |
6252 | # $5 = (int (*)()) 0 | |
6253 | # $6 = (int (*)()) 0x24 <function_bar> | |
6254 | ||
6255 | gdb_test_multiple "print &${var}" "get address of ${var}" { | |
6256 | -re "\\\$\[0-9\]+ = \\(.*\\) (0|$hex)( <${var}>)?\[\r\n\]+${gdb_prompt} $" | |
6257 | { | |
6258 | pass "get address of ${var}" | |
6259 | if { $expect_out(1,string) == "0" } { | |
6260 | return "0x0" | |
6261 | } else { | |
6262 | return $expect_out(1,string) | |
6263 | } | |
6264 | } | |
6265 | } | |
6266 | return "" | |
6267 | } | |
6268 | ||
45f25d6c AB |
6269 | # Return the frame number for the currently selected frame |
6270 | proc get_current_frame_number {{test_name ""}} { | |
6271 | global gdb_prompt | |
6272 | ||
6273 | if { $test_name == "" } { | |
6274 | set test_name "get current frame number" | |
6275 | } | |
6276 | set frame_num -1 | |
6277 | gdb_test_multiple "frame" $test_name { | |
6278 | -re "#(\[0-9\]+) .*$gdb_prompt $" { | |
6279 | set frame_num $expect_out(1,string) | |
6280 | } | |
6281 | } | |
6282 | return $frame_num | |
6283 | } | |
6284 | ||
db863c42 MF |
6285 | # Get the current value for remotetimeout and return it. |
6286 | proc get_remotetimeout { } { | |
6287 | global gdb_prompt | |
6288 | global decimal | |
6289 | ||
6290 | gdb_test_multiple "show remotetimeout" "" { | |
6291 | -re "Timeout limit to wait for target to respond is ($decimal).*$gdb_prompt $" { | |
ae59b1da | 6292 | return $expect_out(1,string) |
db863c42 MF |
6293 | } |
6294 | } | |
6295 | ||
6296 | # Pick the default that gdb uses | |
6297 | warning "Unable to read remotetimeout" | |
6298 | return 300 | |
6299 | } | |
6300 | ||
6301 | # Set the remotetimeout to the specified timeout. Nothing is returned. | |
6302 | proc set_remotetimeout { timeout } { | |
6303 | global gdb_prompt | |
6304 | ||
6305 | gdb_test_multiple "set remotetimeout $timeout" "" { | |
6306 | -re "$gdb_prompt $" { | |
6307 | verbose "Set remotetimeout to $timeout\n" | |
6308 | } | |
6309 | } | |
6310 | } | |
6311 | ||
805acca0 AA |
6312 | # Get the target's current endianness and return it. |
6313 | proc get_endianness { } { | |
6314 | global gdb_prompt | |
6315 | ||
6316 | gdb_test_multiple "show endian" "determine endianness" { | |
6317 | -re ".* (little|big) endian.*\r\n$gdb_prompt $" { | |
6318 | # Pass silently. | |
6319 | return $expect_out(1,string) | |
6320 | } | |
6321 | } | |
6322 | return "little" | |
6323 | } | |
6324 | ||
1e537771 TT |
6325 | # ROOT and FULL are file names. Returns the relative path from ROOT |
6326 | # to FULL. Note that FULL must be in a subdirectory of ROOT. | |
6327 | # For example, given ROOT = /usr/bin and FULL = /usr/bin/ls, this | |
6328 | # will return "ls". | |
6329 | ||
6330 | proc relative_filename {root full} { | |
6331 | set root_split [file split $root] | |
6332 | set full_split [file split $full] | |
6333 | ||
6334 | set len [llength $root_split] | |
6335 | ||
6336 | if {[eval file join $root_split] | |
6337 | != [eval file join [lrange $full_split 0 [expr {$len - 1}]]]} { | |
6338 | error "$full not a subdir of $root" | |
6339 | } | |
6340 | ||
6341 | return [eval file join [lrange $full_split $len end]] | |
6342 | } | |
6343 | ||
5e92f71a TT |
6344 | # If GDB_PARALLEL exists, then set up the parallel-mode directories. |
6345 | if {[info exists GDB_PARALLEL]} { | |
6346 | if {[is_remote host]} { | |
6347 | unset GDB_PARALLEL | |
6348 | } else { | |
3d338901 DE |
6349 | file mkdir \ |
6350 | [make_gdb_parallel_path outputs] \ | |
6351 | [make_gdb_parallel_path temp] \ | |
6352 | [make_gdb_parallel_path cache] | |
5e92f71a TT |
6353 | } |
6354 | } | |
6355 | ||
bbfba9ed | 6356 | proc core_find {binfile {deletefiles {}} {arg ""}} { |
37aeb5df JK |
6357 | global objdir subdir |
6358 | ||
6359 | set destcore "$binfile.core" | |
6360 | file delete $destcore | |
6361 | ||
6362 | # Create a core file named "$destcore" rather than just "core", to | |
6363 | # avoid problems with sys admin types that like to regularly prune all | |
6364 | # files named "core" from the system. | |
6365 | # | |
6366 | # Arbitrarily try setting the core size limit to "unlimited" since | |
6367 | # this does not hurt on systems where the command does not work and | |
6368 | # allows us to generate a core on systems where it does. | |
6369 | # | |
6370 | # Some systems append "core" to the name of the program; others append | |
6371 | # the name of the program to "core"; still others (like Linux, as of | |
6372 | # May 2003) create cores named "core.PID". In the latter case, we | |
6373 | # could have many core files lying around, and it may be difficult to | |
6374 | # tell which one is ours, so let's run the program in a subdirectory. | |
6375 | set found 0 | |
93c0ef37 | 6376 | set coredir [standard_output_file coredir.[getpid]] |
37aeb5df | 6377 | file mkdir $coredir |
bbfba9ed | 6378 | catch "system \"(cd ${coredir}; ulimit -c unlimited; ${binfile} ${arg}; true) >/dev/null 2>&1\"" |
37aeb5df JK |
6379 | # remote_exec host "${binfile}" |
6380 | foreach i "${coredir}/core ${coredir}/core.coremaker.c ${binfile}.core" { | |
6381 | if [remote_file build exists $i] { | |
6382 | remote_exec build "mv $i $destcore" | |
6383 | set found 1 | |
6384 | } | |
6385 | } | |
6386 | # Check for "core.PID". | |
6387 | if { $found == 0 } { | |
6388 | set names [glob -nocomplain -directory $coredir core.*] | |
6389 | if {[llength $names] == 1} { | |
6390 | set corefile [file join $coredir [lindex $names 0]] | |
6391 | remote_exec build "mv $corefile $destcore" | |
6392 | set found 1 | |
6393 | } | |
6394 | } | |
6395 | if { $found == 0 } { | |
6396 | # The braindamaged HPUX shell quits after the ulimit -c above | |
6397 | # without executing ${binfile}. So we try again without the | |
6398 | # ulimit here if we didn't find a core file above. | |
6399 | # Oh, I should mention that any "braindamaged" non-Unix system has | |
6400 | # the same problem. I like the cd bit too, it's really neat'n stuff. | |
6401 | catch "system \"(cd ${objdir}/${subdir}; ${binfile}; true) >/dev/null 2>&1\"" | |
6402 | foreach i "${objdir}/${subdir}/core ${objdir}/${subdir}/core.coremaker.c ${binfile}.core" { | |
6403 | if [remote_file build exists $i] { | |
6404 | remote_exec build "mv $i $destcore" | |
6405 | set found 1 | |
6406 | } | |
6407 | } | |
6408 | } | |
6409 | ||
6410 | # Try to clean up after ourselves. | |
6411 | foreach deletefile $deletefiles { | |
6412 | remote_file build delete [file join $coredir $deletefile] | |
6413 | } | |
6414 | remote_exec build "rmdir $coredir" | |
6415 | ||
6416 | if { $found == 0 } { | |
6417 | warning "can't generate a core file - core tests suppressed - check ulimit -c" | |
6418 | return "" | |
6419 | } | |
6420 | return $destcore | |
6421 | } | |
ee5683ab | 6422 | |
2223449a KB |
6423 | # gdb_target_symbol_prefix compiles a test program and then examines |
6424 | # the output from objdump to determine the prefix (such as underscore) | |
6425 | # for linker symbol prefixes. | |
6426 | ||
6427 | gdb_caching_proc gdb_target_symbol_prefix { | |
bf326452 AH |
6428 | # Compile a simple test program... |
6429 | set src { int main() { return 0; } } | |
6430 | if {![gdb_simple_compile target_symbol_prefix $src executable]} { | |
6431 | return 0 | |
2223449a KB |
6432 | } |
6433 | ||
2223449a KB |
6434 | set prefix "" |
6435 | ||
bf326452 AH |
6436 | set objdump_program [gdb_find_objdump] |
6437 | set result [catch "exec $objdump_program --syms $obj" output] | |
2223449a | 6438 | |
bf326452 AH |
6439 | if { $result == 0 \ |
6440 | && ![regexp -lineanchor \ | |
6441 | { ([^ a-zA-Z0-9]*)main$} $output dummy prefix] } { | |
6442 | verbose "gdb_target_symbol_prefix: Could not find main in objdump output; returning null prefix" 2 | |
2223449a KB |
6443 | } |
6444 | ||
bf326452 | 6445 | file delete $obj |
2223449a KB |
6446 | |
6447 | return $prefix | |
6448 | } | |
6449 | ||
5bd18990 AB |
6450 | # Return 1 if target supports scheduler locking, otherwise return 0. |
6451 | ||
6452 | gdb_caching_proc target_supports_scheduler_locking { | |
6453 | global gdb_prompt | |
6454 | ||
6455 | set me "gdb_target_supports_scheduler_locking" | |
6456 | ||
bf326452 AH |
6457 | set src { int main() { return 0; } } |
6458 | if {![gdb_simple_compile $me $src executable]} { | |
5bd18990 AB |
6459 | return 0 |
6460 | } | |
6461 | ||
bf326452 | 6462 | clean_restart $obj |
58bbcd02 TV |
6463 | if ![runto_main] { |
6464 | return 0 | |
6465 | } | |
5bd18990 AB |
6466 | |
6467 | set supports_schedule_locking -1 | |
6468 | set current_schedule_locking_mode "" | |
6469 | ||
6470 | set test "reading current scheduler-locking mode" | |
6471 | gdb_test_multiple "show scheduler-locking" $test { | |
6472 | -re "Mode for locking scheduler during execution is \"(\[\^\"\]*)\".*$gdb_prompt" { | |
6473 | set current_schedule_locking_mode $expect_out(1,string) | |
6474 | } | |
6475 | -re "$gdb_prompt $" { | |
6476 | set supports_schedule_locking 0 | |
6477 | } | |
6478 | timeout { | |
6479 | set supports_schedule_locking 0 | |
6480 | } | |
6481 | } | |
6482 | ||
6483 | if { $supports_schedule_locking == -1 } { | |
6484 | set test "checking for scheduler-locking support" | |
6485 | gdb_test_multiple "set scheduler-locking $current_schedule_locking_mode" $test { | |
6486 | -re "Target '\[^'\]+' cannot support this command\..*$gdb_prompt $" { | |
6487 | set supports_schedule_locking 0 | |
6488 | } | |
6489 | -re "$gdb_prompt $" { | |
6490 | set supports_schedule_locking 1 | |
6491 | } | |
6492 | timeout { | |
6493 | set supports_schedule_locking 0 | |
6494 | } | |
6495 | } | |
6496 | } | |
6497 | ||
6498 | if { $supports_schedule_locking == -1 } { | |
6499 | set supports_schedule_locking 0 | |
6500 | } | |
6501 | ||
6502 | gdb_exit | |
bf326452 | 6503 | remote_file build delete $obj |
5bd18990 AB |
6504 | verbose "$me: returning $supports_schedule_locking" 2 |
6505 | return $supports_schedule_locking | |
6506 | } | |
6507 | ||
bb47f919 KB |
6508 | # Return 1 if compiler supports use of nested functions. Otherwise, |
6509 | # return 0. | |
6510 | ||
6511 | gdb_caching_proc support_nested_function_tests { | |
6512 | # Compile a test program containing a nested function | |
6513 | return [gdb_can_simple_compile nested_func { | |
6514 | int main () { | |
6515 | int foo () { | |
6516 | return 0; | |
6517 | } | |
6518 | return foo (); | |
6519 | } | |
6520 | } executable] | |
6521 | } | |
6522 | ||
2223449a KB |
6523 | # gdb_target_symbol returns the provided symbol with the correct prefix |
6524 | # prepended. (See gdb_target_symbol_prefix, above.) | |
6525 | ||
6526 | proc gdb_target_symbol { symbol } { | |
6527 | set prefix [gdb_target_symbol_prefix] | |
6528 | return "${prefix}${symbol}" | |
6529 | } | |
6530 | ||
f01dcfd9 KB |
6531 | # gdb_target_symbol_prefix_flags_asm returns a string that can be |
6532 | # added to gdb_compile options to define the C-preprocessor macro | |
6533 | # SYMBOL_PREFIX with a value that can be prepended to symbols | |
6534 | # for targets which require a prefix, such as underscore. | |
6535 | # | |
6536 | # This version (_asm) defines the prefix without double quotes | |
6537 | # surrounding the prefix. It is used to define the macro | |
6538 | # SYMBOL_PREFIX for assembly language files. Another version, below, | |
6539 | # is used for symbols in inline assembler in C/C++ files. | |
6540 | # | |
6541 | # The lack of quotes in this version (_asm) makes it possible to | |
6542 | # define supporting macros in the .S file. (The version which | |
6543 | # uses quotes for the prefix won't work for such files since it's | |
6544 | # impossible to define a quote-stripping macro in C.) | |
6545 | # | |
6546 | # It's possible to use this version (_asm) for C/C++ source files too, | |
6547 | # but a string is usually required in such files; providing a version | |
6548 | # (no _asm) which encloses the prefix with double quotes makes it | |
6549 | # somewhat easier to define the supporting macros in the test case. | |
6550 | ||
6551 | proc gdb_target_symbol_prefix_flags_asm {} { | |
6552 | set prefix [gdb_target_symbol_prefix] | |
6553 | if {$prefix ne ""} { | |
6554 | return "additional_flags=-DSYMBOL_PREFIX=$prefix" | |
6555 | } else { | |
6556 | return ""; | |
6557 | } | |
6558 | } | |
6559 | ||
6560 | # gdb_target_symbol_prefix_flags returns the same string as | |
6561 | # gdb_target_symbol_prefix_flags_asm, above, but with the prefix | |
6562 | # enclosed in double quotes if there is a prefix. | |
6563 | # | |
6564 | # See the comment for gdb_target_symbol_prefix_flags_asm for an | |
6565 | # extended discussion. | |
ee5683ab PM |
6566 | |
6567 | proc gdb_target_symbol_prefix_flags {} { | |
f01dcfd9 KB |
6568 | set prefix [gdb_target_symbol_prefix] |
6569 | if {$prefix ne ""} { | |
6570 | return "additional_flags=-DSYMBOL_PREFIX=\"$prefix\"" | |
ee5683ab | 6571 | } else { |
f01dcfd9 | 6572 | return ""; |
ee5683ab PM |
6573 | } |
6574 | } | |
6575 | ||
6e45f158 DE |
6576 | # A wrapper for 'remote_exec host' that passes or fails a test. |
6577 | # Returns 0 if all went well, nonzero on failure. | |
6578 | # TEST is the name of the test, other arguments are as for remote_exec. | |
6579 | ||
6580 | proc run_on_host { test program args } { | |
6581 | verbose -log "run_on_host: $program $args" | |
6582 | # remote_exec doesn't work properly if the output is set but the | |
6583 | # input is the empty string -- so replace an empty input with | |
6584 | # /dev/null. | |
6585 | if {[llength $args] > 1 && [lindex $args 1] == ""} { | |
6586 | set args [lreplace $args 1 1 "/dev/null"] | |
6587 | } | |
6588 | set result [eval remote_exec host [list $program] $args] | |
6589 | verbose "result is $result" | |
6590 | set status [lindex $result 0] | |
6591 | set output [lindex $result 1] | |
6592 | if {$status == 0} { | |
6593 | pass $test | |
6594 | return 0 | |
6595 | } else { | |
50cc37c8 | 6596 | verbose -log "run_on_host failed: $output" |
6e45f158 DE |
6597 | fail $test |
6598 | return -1 | |
6599 | } | |
6600 | } | |
6601 | ||
a587b477 DE |
6602 | # Return non-zero if "board_info debug_flags" mentions Fission. |
6603 | # http://gcc.gnu.org/wiki/DebugFission | |
6604 | # Fission doesn't support everything yet. | |
6605 | # This supports working around bug 15954. | |
6606 | ||
6607 | proc using_fission { } { | |
6608 | set debug_flags [board_info [target_info name] debug_flags] | |
6609 | return [regexp -- "-gsplit-dwarf" $debug_flags] | |
6610 | } | |
6611 | ||
4b48d439 KS |
6612 | # Search the caller's ARGS list and set variables according to the list of |
6613 | # valid options described by ARGSET. | |
6614 | # | |
6615 | # The first member of each one- or two-element list in ARGSET defines the | |
6616 | # name of a variable that will be added to the caller's scope. | |
6617 | # | |
6618 | # If only one element is given to describe an option, it the value is | |
6619 | # 0 if the option is not present in (the caller's) ARGS or 1 if | |
6620 | # it is. | |
6621 | # | |
6622 | # If two elements are given, the second element is the default value of | |
6623 | # the variable. This is then overwritten if the option exists in ARGS. | |
6624 | # | |
6625 | # Any parse_args elements in (the caller's) ARGS will be removed, leaving | |
6626 | # any optional components. | |
6627 | ||
6628 | # Example: | |
6629 | # proc myproc {foo args} { | |
6630 | # parse_args {{bar} {baz "abc"} {qux}} | |
6631 | # # ... | |
6632 | # } | |
6633 | # myproc ABC -bar -baz DEF peanut butter | |
6634 | # will define the following variables in myproc: | |
6635 | # foo (=ABC), bar (=1), baz (=DEF), and qux (=0) | |
6636 | # args will be the list {peanut butter} | |
6637 | ||
6638 | proc parse_args { argset } { | |
6639 | upvar args args | |
6640 | ||
6641 | foreach argument $argset { | |
6642 | if {[llength $argument] == 1} { | |
6643 | # No default specified, so we assume that we should set | |
6644 | # the value to 1 if the arg is present and 0 if it's not. | |
6645 | # It is assumed that no value is given with the argument. | |
6646 | set result [lsearch -exact $args "-$argument"] | |
6647 | if {$result != -1} then { | |
6648 | uplevel 1 [list set $argument 1] | |
6649 | set args [lreplace $args $result $result] | |
6650 | } else { | |
6651 | uplevel 1 [list set $argument 0] | |
6652 | } | |
6653 | } elseif {[llength $argument] == 2} { | |
6654 | # There are two items in the argument. The second is a | |
6655 | # default value to use if the item is not present. | |
6656 | # Otherwise, the variable is set to whatever is provided | |
6657 | # after the item in the args. | |
6658 | set arg [lindex $argument 0] | |
6659 | set result [lsearch -exact $args "-[lindex $arg 0]"] | |
6660 | if {$result != -1} then { | |
6661 | uplevel 1 [list set $arg [lindex $args [expr $result+1]]] | |
6662 | set args [lreplace $args $result [expr $result+1]] | |
6663 | } else { | |
6664 | uplevel 1 [list set $arg [lindex $argument 1]] | |
6665 | } | |
6666 | } else { | |
6667 | error "Badly formatted argument \"$argument\" in argument set" | |
6668 | } | |
6669 | } | |
6670 | ||
6671 | # The remaining args should be checked to see that they match the | |
6672 | # number of items expected to be passed into the procedure... | |
6673 | } | |
6674 | ||
87f0e720 KS |
6675 | # Capture the output of COMMAND in a string ignoring PREFIX (a regexp); |
6676 | # return that string. | |
6677 | ||
e9089e05 MM |
6678 | proc capture_command_output { command prefix } { |
6679 | global gdb_prompt | |
6680 | global expect_out | |
6681 | ||
6682 | set output_string "" | |
6683 | gdb_test_multiple "$command" "capture_command_output for $command" { | |
87f0e720 | 6684 | -re "[string_to_regexp ${command}]\[\r\n\]+${prefix}(.*)\[\r\n\]+$gdb_prompt $" { |
e9089e05 MM |
6685 | set output_string $expect_out(1,string) |
6686 | } | |
6687 | } | |
6688 | return $output_string | |
6689 | } | |
6690 | ||
3c724c8c PMR |
6691 | # A convenience function that joins all the arguments together, with a |
6692 | # regexp that matches exactly one end of line in between each argument. | |
6693 | # This function is ideal to write the expected output of a GDB command | |
6694 | # that generates more than a couple of lines, as this allows us to write | |
6695 | # each line as a separate string, which is easier to read by a human | |
6696 | # being. | |
6697 | ||
6698 | proc multi_line { args } { | |
6699 | return [join $args "\r\n"] | |
6700 | } | |
6701 | ||
fad0c9fb PA |
6702 | # Similar to the above, but while multi_line is meant to be used to |
6703 | # match GDB output, this one is meant to be used to build strings to | |
6704 | # send as GDB input. | |
6705 | ||
6706 | proc multi_line_input { args } { | |
6707 | return [join $args "\n"] | |
6708 | } | |
6709 | ||
896c0c1e SM |
6710 | # Return the version of the DejaGnu framework. |
6711 | # | |
6712 | # The return value is a list containing the major, minor and patch version | |
6713 | # numbers. If the version does not contain a minor or patch number, they will | |
6714 | # be set to 0. For example: | |
6715 | # | |
6716 | # 1.6 -> {1 6 0} | |
6717 | # 1.6.1 -> {1 6 1} | |
6718 | # 2 -> {2 0 0} | |
6719 | ||
6720 | proc dejagnu_version { } { | |
6721 | # The frame_version variable is defined by DejaGnu, in runtest.exp. | |
6722 | global frame_version | |
6723 | ||
6724 | verbose -log "DejaGnu version: $frame_version" | |
6725 | verbose -log "Expect version: [exp_version]" | |
6726 | verbose -log "Tcl version: [info tclversion]" | |
6727 | ||
6728 | set dg_ver [split $frame_version .] | |
6729 | ||
6730 | while { [llength $dg_ver] < 3 } { | |
6731 | lappend dg_ver 0 | |
6732 | } | |
6733 | ||
6734 | return $dg_ver | |
6735 | } | |
fad0c9fb | 6736 | |
3a3fd0fd PA |
6737 | # Define user-defined command COMMAND using the COMMAND_LIST as the |
6738 | # command's definition. The terminating "end" is added automatically. | |
6739 | ||
6740 | proc gdb_define_cmd {command command_list} { | |
6741 | global gdb_prompt | |
6742 | ||
6743 | set input [multi_line_input {*}$command_list "end"] | |
6744 | set test "define $command" | |
6745 | ||
6746 | gdb_test_multiple "define $command" $test { | |
6747 | -re "End with" { | |
6748 | gdb_test_multiple $input $test { | |
6749 | -re "\r\n$gdb_prompt " { | |
6750 | } | |
6751 | } | |
6752 | } | |
6753 | } | |
6754 | } | |
6755 | ||
c3734e09 AH |
6756 | # Override the 'cd' builtin with a version that ensures that the |
6757 | # log file keeps pointing at the same file. We need this because | |
6758 | # unfortunately the path to the log file is recorded using an | |
6759 | # relative path name, and, we sometimes need to close/reopen the log | |
6760 | # after changing the current directory. See get_compiler_info. | |
6761 | ||
6762 | rename cd builtin_cd | |
6763 | ||
6764 | proc cd { dir } { | |
6765 | ||
6766 | # Get the existing log file flags. | |
6767 | set log_file_info [log_file -info] | |
6768 | ||
6769 | # Split the flags into args and file name. | |
6770 | set log_file_flags "" | |
6771 | set log_file_file "" | |
6772 | foreach arg [ split "$log_file_info" " "] { | |
6773 | if [string match "-*" $arg] { | |
6774 | lappend log_file_flags $arg | |
6775 | } else { | |
6776 | lappend log_file_file $arg | |
6777 | } | |
6778 | } | |
6779 | ||
6780 | # If there was an existing file, ensure it is an absolute path, and then | |
6781 | # reset logging. | |
6782 | if { $log_file_file != "" } { | |
6783 | set log_file_file [file normalize $log_file_file] | |
6784 | log_file | |
6785 | log_file $log_file_flags "$log_file_file" | |
6786 | } | |
6787 | ||
6788 | # Call the builtin version of cd. | |
6789 | builtin_cd $dir | |
6790 | } | |
6791 | ||
d7df6549 AB |
6792 | # Return a list of all languages supported by GDB, suitable for use in |
6793 | # 'set language NAME'. This doesn't include either the 'local' or | |
6794 | # 'auto' keywords. | |
6795 | proc gdb_supported_languages {} { | |
6796 | return [list c objective-c c++ d go fortran modula-2 asm pascal \ | |
6797 | opencl rust minimal ada] | |
6798 | } | |
6799 | ||
29b52314 AH |
6800 | # Check if debugging is enabled for gdb. |
6801 | ||
6802 | proc gdb_debug_enabled { } { | |
6803 | global gdbdebug | |
6804 | ||
6805 | # If not already read, get the debug setting from environment or board setting. | |
6806 | if {![info exists gdbdebug]} { | |
6807 | global env | |
6808 | if [info exists env(GDB_DEBUG)] { | |
6809 | set gdbdebug $env(GDB_DEBUG) | |
6810 | } elseif [target_info exists gdb,debug] { | |
6811 | set gdbdebug [target_info gdb,debug] | |
6812 | } else { | |
6813 | return 0 | |
6814 | } | |
6815 | } | |
6816 | ||
6817 | # Ensure it not empty. | |
6818 | return [expr { $gdbdebug != "" }] | |
6819 | } | |
6820 | ||
6821 | # Turn on debugging if enabled, or reset if already on. | |
6822 | ||
6823 | proc gdb_debug_init { } { | |
6824 | ||
6825 | global gdb_prompt | |
6826 | ||
6827 | if ![gdb_debug_enabled] { | |
6828 | return; | |
6829 | } | |
6830 | ||
6831 | # First ensure logging is off. | |
6832 | send_gdb "set logging off\n" | |
6833 | ||
6834 | set debugfile [standard_output_file gdb.debug] | |
6835 | send_gdb "set logging file $debugfile\n" | |
6836 | ||
6837 | send_gdb "set logging debugredirect\n" | |
6838 | ||
6839 | global gdbdebug | |
6840 | foreach entry [split $gdbdebug ,] { | |
6841 | send_gdb "set debug $entry 1\n" | |
6842 | } | |
6843 | ||
6844 | # Now that everything is set, enable logging. | |
6845 | send_gdb "set logging on\n" | |
6846 | gdb_expect 10 { | |
6847 | -re "Copying output to $debugfile.*Redirecting debug output to $debugfile.*$gdb_prompt $" {} | |
6848 | timeout { warning "Couldn't set logging file" } | |
6849 | } | |
6850 | } | |
6851 | ||
dd06d4d6 AH |
6852 | # Check if debugging is enabled for gdbserver. |
6853 | ||
6854 | proc gdbserver_debug_enabled { } { | |
6855 | # Always disabled for GDB only setups. | |
6856 | return 0 | |
6857 | } | |
6858 | ||
f9e2e39d AH |
6859 | # Open the file for logging gdb input |
6860 | ||
6861 | proc gdb_stdin_log_init { } { | |
6862 | global in_file | |
6863 | ||
6864 | if {[info exists in_file]} { | |
6865 | # Close existing file. | |
6866 | catch "close $in_file" | |
6867 | } | |
6868 | ||
6869 | set logfile [standard_output_file_with_gdb_instance gdb.in] | |
6870 | set in_file [open $logfile w] | |
6871 | } | |
6872 | ||
6873 | # Write to the file for logging gdb input. | |
6874 | # TYPE can be one of the following: | |
6875 | # "standard" : Default. Standard message written to the log | |
6876 | # "answer" : Answer to a question (eg "Y"). Not written the log. | |
6877 | # "optional" : Optional message. Not written to the log. | |
6878 | ||
6879 | proc gdb_stdin_log_write { message {type standard} } { | |
6880 | ||
6881 | global in_file | |
6882 | if {![info exists in_file]} { | |
6883 | return | |
6884 | } | |
6885 | ||
6886 | # Check message types. | |
6887 | switch -regexp -- $type { | |
6888 | "answer" { | |
6889 | return | |
6890 | } | |
6891 | "optional" { | |
6892 | return | |
6893 | } | |
6894 | } | |
6895 | ||
6896 | #Write to the log | |
6897 | puts -nonewline $in_file "$message" | |
6898 | } | |
6899 | ||
408e9b8b AH |
6900 | # Write the command line used to invocate gdb to the cmd file. |
6901 | ||
6902 | proc gdb_write_cmd_file { cmdline } { | |
6903 | set logfile [standard_output_file_with_gdb_instance gdb.cmd] | |
6904 | set cmd_file [open $logfile w] | |
6905 | puts $cmd_file $cmdline | |
6906 | catch "close $cmd_file" | |
6907 | } | |
6908 | ||
30331a6c TV |
6909 | # Compare contents of FILE to string STR. Pass with MSG if equal, otherwise |
6910 | # fail with MSG. | |
6911 | ||
6912 | proc cmp_file_string { file str msg } { | |
6913 | if { ![file exists $file]} { | |
6914 | fail "$msg" | |
6915 | return | |
6916 | } | |
6917 | ||
6918 | set caught_error [catch { | |
6919 | set fp [open "$file" r] | |
6920 | set file_contents [read $fp] | |
6921 | close $fp | |
6922 | } error_message] | |
6923 | if { $caught_error } then { | |
6924 | error "$error_message" | |
6925 | fail "$msg" | |
6926 | return | |
6927 | } | |
6928 | ||
6929 | if { $file_contents == $str } { | |
6930 | pass "$msg" | |
6931 | } else { | |
6932 | fail "$msg" | |
6933 | } | |
6934 | } | |
6935 | ||
30d0a636 AB |
6936 | # Does the compiler support CTF debug output using '-gt' compiler |
6937 | # flag? If not then we should skip these tests. | |
6938 | ||
6939 | gdb_caching_proc skip_ctf_tests { | |
6940 | return ![gdb_can_simple_compile ctfdebug { | |
6941 | int main () { | |
6942 | return 0; | |
6943 | } | |
6944 | } executable "additional_flags=-gt"] | |
6945 | } | |
6946 | ||
42159ca5 TT |
6947 | # Always load compatibility stuff. |
6948 | load_lib future.exp |