Commit | Line | Data |
---|---|---|
6aba47ca | 1 | # Copyright 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, |
9b254dd1 | 2 | # 2003, 2004, 2005, 2007, 2008 Free Software Foundation, Inc. |
c906108c SS |
3 | |
4 | # This program is free software; you can redistribute it and/or modify | |
5 | # it under the terms of the GNU General Public License as published by | |
e22f8b7c | 6 | # the Free Software Foundation; either version 3 of the License, or |
c906108c | 7 | # (at your option) any later version. |
e22f8b7c | 8 | # |
c906108c SS |
9 | # This program is distributed in the hope that it will be useful, |
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | # GNU General Public License for more details. | |
e22f8b7c | 13 | # |
c906108c | 14 | # You should have received a copy of the GNU General Public License |
e22f8b7c | 15 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
c906108c | 16 | |
c906108c SS |
17 | # This file was written by Fred Fish. (fnf@cygnus.com) |
18 | ||
19 | # Generic gdb subroutines that should work for any target. If these | |
20 | # need to be modified for any target, it can be done with a variable | |
21 | # or by passing arguments. | |
22 | ||
97c3f1f3 JK |
23 | if {$tool == ""} { |
24 | # Tests would fail, logs on get_compiler_info() would be missing. | |
25 | send_error "`site.exp' not found, run `make site.exp'!\n" | |
26 | exit 2 | |
27 | } | |
28 | ||
c906108c SS |
29 | load_lib libgloss.exp |
30 | ||
31 | global GDB | |
c906108c SS |
32 | |
33 | if [info exists TOOL_EXECUTABLE] { | |
34 | set GDB $TOOL_EXECUTABLE; | |
35 | } | |
36 | if ![info exists GDB] { | |
37 | if ![is_remote host] { | |
38 | set GDB [findfile $base_dir/../../gdb/gdb "$base_dir/../../gdb/gdb" [transform gdb]] | |
39 | } else { | |
40 | set GDB [transform gdb]; | |
41 | } | |
42 | } | |
43 | verbose "using GDB = $GDB" 2 | |
44 | ||
45 | global GDBFLAGS | |
46 | if ![info exists GDBFLAGS] { | |
47 | set GDBFLAGS "-nx" | |
48 | } | |
49 | verbose "using GDBFLAGS = $GDBFLAGS" 2 | |
50 | ||
9e0b60a8 JM |
51 | # The variable gdb_prompt is a regexp which matches the gdb prompt. |
52 | # Set it if it is not already set. | |
c906108c | 53 | global gdb_prompt |
9e0b60a8 | 54 | if ![info exists gdb_prompt] then { |
c906108c SS |
55 | set gdb_prompt "\[(\]gdb\[)\]" |
56 | } | |
57 | ||
6006a3a1 BR |
58 | # The variable fullname_syntax_POSIX is a regexp which matches a POSIX |
59 | # absolute path ie. /foo/ | |
60 | set fullname_syntax_POSIX "/.*/" | |
61 | # The variable fullname_syntax_UNC is a regexp which matches a Windows | |
62 | # UNC path ie. \\D\foo\ | |
63 | set fullname_syntax_UNC {\\\\[^\\]+\\.+\\} | |
64 | # The variable fullname_syntax_DOS_CASE is a regexp which matches a | |
65 | # particular DOS case that GDB most likely will output | |
66 | # ie. \foo\, but don't match \\.*\ | |
67 | set fullname_syntax_DOS_CASE {\\[^\\].*\\} | |
68 | # The variable fullname_syntax_DOS is a regexp which matches a DOS path | |
69 | # ie. a:\foo\ && a:foo\ | |
70 | set fullname_syntax_DOS {[a-zA-Z]:.*\\} | |
71 | # The variable fullname_syntax is a regexp which matches what GDB considers | |
72 | # an absolute path. It is currently debatable if the Windows style paths | |
73 | # d:foo and \abc should be considered valid as an absolute path. | |
74 | # Also, the purpse of this regexp is not to recognize a well formed | |
75 | # absolute path, but to say with certainty that a path is absolute. | |
76 | set fullname_syntax "($fullname_syntax_POSIX|$fullname_syntax_UNC|$fullname_syntax_DOS_CASE|$fullname_syntax_DOS)" | |
77 | ||
93076499 ND |
78 | # Needed for some tests under Cygwin. |
79 | global EXEEXT | |
80 | global env | |
81 | ||
82 | if ![info exists env(EXEEXT)] { | |
83 | set EXEEXT "" | |
84 | } else { | |
85 | set EXEEXT $env(EXEEXT) | |
86 | } | |
87 | ||
bb2bed55 NR |
88 | set octal "\[0-7\]+" |
89 | ||
085dd6e6 JM |
90 | ### Only procedures should come after this point. |
91 | ||
c906108c SS |
92 | # |
93 | # gdb_version -- extract and print the version number of GDB | |
94 | # | |
95 | proc default_gdb_version {} { | |
96 | global GDB | |
97 | global GDBFLAGS | |
98 | global gdb_prompt | |
99 | set fileid [open "gdb_cmd" w]; | |
100 | puts $fileid "q"; | |
101 | close $fileid; | |
102 | set cmdfile [remote_download host "gdb_cmd"]; | |
103 | set output [remote_exec host "$GDB -nw --command $cmdfile"] | |
104 | remote_file build delete "gdb_cmd"; | |
105 | remote_file host delete "$cmdfile"; | |
106 | set tmp [lindex $output 1]; | |
107 | set version "" | |
108 | regexp " \[0-9\]\[^ \t\n\r\]+" "$tmp" version | |
109 | if ![is_remote host] { | |
110 | clone_output "[which $GDB] version $version $GDBFLAGS\n" | |
111 | } else { | |
112 | clone_output "$GDB on remote host version $version $GDBFLAGS\n" | |
113 | } | |
114 | } | |
115 | ||
116 | proc gdb_version { } { | |
117 | return [default_gdb_version]; | |
118 | } | |
119 | ||
120 | # | |
121 | # gdb_unload -- unload a file if one is loaded | |
122 | # | |
123 | ||
124 | proc gdb_unload {} { | |
125 | global verbose | |
126 | global GDB | |
127 | global gdb_prompt | |
128 | send_gdb "file\n" | |
129 | gdb_expect 60 { | |
130 | -re "No executable file now\[^\r\n\]*\[\r\n\]" { exp_continue } | |
131 | -re "No symbol file now\[^\r\n\]*\[\r\n\]" { exp_continue } | |
132 | -re "A program is being debugged already..*Kill it.*y or n. $"\ | |
133 | { send_gdb "y\n" | |
134 | verbose "\t\tKilling previous program being debugged" | |
135 | exp_continue | |
136 | } | |
137 | -re "Discard symbol table from .*y or n.*$" { | |
138 | send_gdb "y\n" | |
139 | exp_continue | |
140 | } | |
141 | -re "$gdb_prompt $" {} | |
142 | timeout { | |
143 | perror "couldn't unload file in $GDB (timed out)." | |
144 | return -1 | |
145 | } | |
146 | } | |
147 | } | |
148 | ||
149 | # Many of the tests depend on setting breakpoints at various places and | |
150 | # running until that breakpoint is reached. At times, we want to start | |
151 | # with a clean-slate with respect to breakpoints, so this utility proc | |
152 | # lets us do this without duplicating this code everywhere. | |
153 | # | |
154 | ||
155 | proc delete_breakpoints {} { | |
156 | global gdb_prompt | |
157 | ||
a0b3c4fd JM |
158 | # we need a larger timeout value here or this thing just confuses |
159 | # itself. May need a better implementation if possible. - guo | |
160 | # | |
c906108c | 161 | send_gdb "delete breakpoints\n" |
a0b3c4fd | 162 | gdb_expect 100 { |
c906108c SS |
163 | -re "Delete all breakpoints.*y or n.*$" { |
164 | send_gdb "y\n"; | |
165 | exp_continue | |
166 | } | |
167 | -re "$gdb_prompt $" { # This happens if there were no breakpoints | |
168 | } | |
169 | timeout { perror "Delete all breakpoints in delete_breakpoints (timeout)" ; return } | |
170 | } | |
171 | send_gdb "info breakpoints\n" | |
a0b3c4fd | 172 | gdb_expect 100 { |
c906108c SS |
173 | -re "No breakpoints or watchpoints..*$gdb_prompt $" {} |
174 | -re "$gdb_prompt $" { perror "breakpoints not deleted" ; return } | |
175 | -re "Delete all breakpoints.*or n.*$" { | |
176 | send_gdb "y\n"; | |
177 | exp_continue | |
178 | } | |
179 | timeout { perror "info breakpoints (timeout)" ; return } | |
180 | } | |
181 | } | |
182 | ||
183 | ||
184 | # | |
185 | # Generic run command. | |
186 | # | |
187 | # The second pattern below matches up to the first newline *only*. | |
188 | # Using ``.*$'' could swallow up output that we attempt to match | |
189 | # elsewhere. | |
190 | # | |
191 | proc gdb_run_cmd {args} { | |
192 | global gdb_prompt | |
193 | ||
194 | if [target_info exists gdb_init_command] { | |
195 | send_gdb "[target_info gdb_init_command]\n"; | |
196 | gdb_expect 30 { | |
197 | -re "$gdb_prompt $" { } | |
198 | default { | |
199 | perror "gdb_init_command for target failed"; | |
200 | return; | |
201 | } | |
202 | } | |
203 | } | |
204 | ||
205 | if [target_info exists use_gdb_stub] { | |
206 | if [target_info exists gdb,do_reload_on_run] { | |
b741e217 | 207 | if { [gdb_reload] != 0 } { |
917317f4 JM |
208 | return; |
209 | } | |
c906108c SS |
210 | send_gdb "continue\n"; |
211 | gdb_expect 60 { | |
212 | -re "Continu\[^\r\n\]*\[\r\n\]" {} | |
213 | default {} | |
214 | } | |
215 | return; | |
216 | } | |
217 | ||
218 | if [target_info exists gdb,start_symbol] { | |
219 | set start [target_info gdb,start_symbol]; | |
220 | } else { | |
221 | set start "start"; | |
222 | } | |
223 | send_gdb "jump *$start\n" | |
917317f4 JM |
224 | set start_attempt 1; |
225 | while { $start_attempt } { | |
226 | # Cap (re)start attempts at three to ensure that this loop | |
227 | # always eventually fails. Don't worry about trying to be | |
228 | # clever and not send a command when it has failed. | |
229 | if [expr $start_attempt > 3] { | |
230 | perror "Jump to start() failed (retry count exceeded)"; | |
c906108c SS |
231 | return; |
232 | } | |
917317f4 JM |
233 | set start_attempt [expr $start_attempt + 1]; |
234 | gdb_expect 30 { | |
235 | -re "Continuing at \[^\r\n\]*\[\r\n\]" { | |
236 | set start_attempt 0; | |
237 | } | |
238 | -re "No symbol \"_start\" in current.*$gdb_prompt $" { | |
239 | perror "Can't find start symbol to run in gdb_run"; | |
240 | return; | |
241 | } | |
242 | -re "No symbol \"start\" in current.*$gdb_prompt $" { | |
243 | send_gdb "jump *_start\n"; | |
244 | } | |
245 | -re "No symbol.*context.*$gdb_prompt $" { | |
246 | set start_attempt 0; | |
247 | } | |
248 | -re "Line.* Jump anyway.*y or n. $" { | |
249 | send_gdb "y\n" | |
250 | } | |
251 | -re "The program is not being run.*$gdb_prompt $" { | |
b741e217 | 252 | if { [gdb_reload] != 0 } { |
917317f4 JM |
253 | return; |
254 | } | |
255 | send_gdb "jump *$start\n"; | |
256 | } | |
257 | timeout { | |
258 | perror "Jump to start() failed (timeout)"; | |
259 | return | |
260 | } | |
c906108c | 261 | } |
c906108c SS |
262 | } |
263 | if [target_info exists gdb_stub] { | |
264 | gdb_expect 60 { | |
265 | -re "$gdb_prompt $" { | |
266 | send_gdb "continue\n" | |
267 | } | |
268 | } | |
269 | } | |
270 | return | |
271 | } | |
83f66e8f DJ |
272 | |
273 | if [target_info exists gdb,do_reload_on_run] { | |
b741e217 | 274 | if { [gdb_reload] != 0 } { |
83f66e8f DJ |
275 | return; |
276 | } | |
277 | } | |
c906108c SS |
278 | send_gdb "run $args\n" |
279 | # This doesn't work quite right yet. | |
5aa7ddc2 PM |
280 | # Use -notransfer here so that test cases (like chng-sym.exp) |
281 | # may test for additional start-up messages. | |
282 | gdb_expect 60 { | |
c906108c SS |
283 | -re "The program .* has been started already.*y or n. $" { |
284 | send_gdb "y\n" | |
285 | exp_continue | |
286 | } | |
bbb88ebf | 287 | -notransfer -re "Starting program: \[^\r\n\]*" {} |
c906108c SS |
288 | } |
289 | } | |
290 | ||
b741e217 DJ |
291 | # Generic start command. Return 0 if we could start the program, -1 |
292 | # if we could not. | |
293 | ||
294 | proc gdb_start_cmd {args} { | |
295 | global gdb_prompt | |
296 | ||
297 | if [target_info exists gdb_init_command] { | |
298 | send_gdb "[target_info gdb_init_command]\n"; | |
299 | gdb_expect 30 { | |
300 | -re "$gdb_prompt $" { } | |
301 | default { | |
302 | perror "gdb_init_command for target failed"; | |
303 | return; | |
304 | } | |
305 | } | |
306 | } | |
307 | ||
308 | if [target_info exists use_gdb_stub] { | |
309 | return -1 | |
310 | } | |
311 | ||
312 | send_gdb "start $args\n" | |
313 | gdb_expect 60 { | |
314 | -re "The program .* has been started already.*y or n. $" { | |
315 | send_gdb "y\n" | |
316 | exp_continue | |
317 | } | |
318 | # Use -notransfer here so that test cases (like chng-sym.exp) | |
319 | # may test for additional start-up messages. | |
320 | -notransfer -re "Starting program: \[^\r\n\]*" { | |
321 | return 0 | |
322 | } | |
323 | } | |
324 | return -1 | |
325 | } | |
326 | ||
78a1a894 | 327 | # Set a breakpoint at FUNCTION. If there is an additional argument it is |
e48883f7 | 328 | # a list of options; the supported options are allow-pending and temporary. |
78a1a894 DJ |
329 | |
330 | proc gdb_breakpoint { function args } { | |
c906108c SS |
331 | global gdb_prompt |
332 | global decimal | |
333 | ||
78a1a894 DJ |
334 | set pending_response n |
335 | if {[lsearch -exact [lindex $args 0] allow-pending] != -1} { | |
336 | set pending_response y | |
337 | } | |
338 | ||
e48883f7 | 339 | set break_command "break" |
18ac113b | 340 | set break_message "Breakpoint" |
e48883f7 DJ |
341 | if {[lsearch -exact [lindex $args 0] temporary] != -1} { |
342 | set break_command "tbreak" | |
18ac113b | 343 | set break_message "Temporary breakpoint" |
e48883f7 DJ |
344 | } |
345 | ||
346 | send_gdb "$break_command $function\n" | |
c906108c SS |
347 | # The first two regexps are what we get with -g, the third is without -g. |
348 | gdb_expect 30 { | |
18ac113b AR |
349 | -re "$break_message \[0-9\]* at .*: file .*, line $decimal.\r\n$gdb_prompt $" {} |
350 | -re "$break_message \[0-9\]*: file .*, line $decimal.\r\n$gdb_prompt $" {} | |
351 | -re "$break_message \[0-9\]* at .*$gdb_prompt $" {} | |
352 | -re "$break_message \[0-9\]* \\(.*\\) pending.*$gdb_prompt $" { | |
78a1a894 DJ |
353 | if {$pending_response == "n"} { |
354 | fail "setting breakpoint at $function" | |
355 | return 0 | |
356 | } | |
357 | } | |
9f27c604 | 358 | -re "Make breakpoint pending.*y or \\\[n\\\]. $" { |
78a1a894 | 359 | send_gdb "$pending_response\n" |
14b1a056 | 360 | exp_continue |
18fe2033 | 361 | } |
c906108c SS |
362 | -re "$gdb_prompt $" { fail "setting breakpoint at $function" ; return 0 } |
363 | timeout { fail "setting breakpoint at $function (timeout)" ; return 0 } | |
364 | } | |
365 | return 1; | |
366 | } | |
367 | ||
368 | # Set breakpoint at function and run gdb until it breaks there. | |
369 | # Since this is the only breakpoint that will be set, if it stops | |
370 | # at a breakpoint, we will assume it is the one we want. We can't | |
371 | # just compare to "function" because it might be a fully qualified, | |
78a1a894 DJ |
372 | # single quoted C++ function specifier. If there's an additional argument, |
373 | # pass it to gdb_breakpoint. | |
c906108c | 374 | |
78a1a894 | 375 | proc runto { function args } { |
c906108c SS |
376 | global gdb_prompt |
377 | global decimal | |
378 | ||
379 | delete_breakpoints | |
380 | ||
78a1a894 | 381 | if ![gdb_breakpoint $function [lindex $args 0]] { |
c906108c SS |
382 | return 0; |
383 | } | |
384 | ||
385 | gdb_run_cmd | |
386 | ||
387 | # the "at foo.c:36" output we get with -g. | |
388 | # the "in func" output we get without -g. | |
389 | gdb_expect 30 { | |
390 | -re "Break.* at .*:$decimal.*$gdb_prompt $" { | |
391 | return 1 | |
392 | } | |
393 | -re "Breakpoint \[0-9\]*, \[0-9xa-f\]* in .*$gdb_prompt $" { | |
394 | return 1 | |
395 | } | |
396 | -re "$gdb_prompt $" { | |
397 | fail "running to $function in runto" | |
398 | return 0 | |
399 | } | |
400 | timeout { | |
401 | fail "running to $function in runto (timeout)" | |
402 | return 0 | |
403 | } | |
404 | } | |
405 | return 1 | |
406 | } | |
407 | ||
408 | # | |
409 | # runto_main -- ask gdb to run until we hit a breakpoint at main. | |
410 | # The case where the target uses stubs has to be handled | |
411 | # specially--if it uses stubs, assuming we hit | |
412 | # breakpoint() and just step out of the function. | |
413 | # | |
414 | proc runto_main { } { | |
415 | global gdb_prompt | |
416 | global decimal | |
417 | ||
418 | if ![target_info exists gdb_stub] { | |
419 | return [runto main] | |
420 | } | |
421 | ||
422 | delete_breakpoints | |
423 | ||
424 | gdb_step_for_stub; | |
425 | ||
426 | return 1 | |
427 | } | |
428 | ||
7a292a7a | 429 | |
4ce44c66 JM |
430 | ### Continue, and expect to hit a breakpoint. |
431 | ### Report a pass or fail, depending on whether it seems to have | |
432 | ### worked. Use NAME as part of the test name; each call to | |
433 | ### continue_to_breakpoint should use a NAME which is unique within | |
434 | ### that test file. | |
435 | proc gdb_continue_to_breakpoint {name} { | |
436 | global gdb_prompt | |
437 | set full_name "continue to breakpoint: $name" | |
438 | ||
439 | send_gdb "continue\n" | |
440 | gdb_expect { | |
441 | -re "Breakpoint .* at .*\r\n$gdb_prompt $" { | |
442 | pass $full_name | |
443 | } | |
444 | -re ".*$gdb_prompt $" { | |
445 | fail $full_name | |
446 | } | |
447 | timeout { | |
448 | fail "$full_name (timeout)" | |
449 | } | |
450 | } | |
451 | } | |
452 | ||
453 | ||
039cf96d AC |
454 | # gdb_internal_error_resync: |
455 | # | |
456 | # Answer the questions GDB asks after it reports an internal error | |
457 | # until we get back to a GDB prompt. Decline to quit the debugging | |
458 | # session, and decline to create a core file. Return non-zero if the | |
459 | # resync succeeds. | |
460 | # | |
461 | # This procedure just answers whatever questions come up until it sees | |
462 | # a GDB prompt; it doesn't require you to have matched the input up to | |
463 | # any specific point. However, it only answers questions it sees in | |
464 | # the output itself, so if you've matched a question, you had better | |
465 | # answer it yourself before calling this. | |
466 | # | |
467 | # You can use this function thus: | |
468 | # | |
469 | # gdb_expect { | |
470 | # ... | |
471 | # -re ".*A problem internal to GDB has been detected" { | |
472 | # gdb_internal_error_resync | |
473 | # } | |
474 | # ... | |
475 | # } | |
476 | # | |
477 | proc gdb_internal_error_resync {} { | |
478 | global gdb_prompt | |
479 | ||
480 | set count 0 | |
481 | while {$count < 10} { | |
482 | gdb_expect { | |
483 | -re "Quit this debugging session\\? \\(y or n\\) $" { | |
484 | send_gdb "n\n" | |
485 | incr count | |
486 | } | |
487 | -re "Create a core file of GDB\\? \\(y or n\\) $" { | |
488 | send_gdb "n\n" | |
489 | incr count | |
490 | } | |
491 | -re "$gdb_prompt $" { | |
492 | # We're resynchronized. | |
493 | return 1 | |
494 | } | |
495 | timeout { | |
496 | perror "Could not resync from internal error (timeout)" | |
497 | return 0 | |
498 | } | |
499 | } | |
500 | } | |
2b211c59 AC |
501 | perror "Could not resync from internal error (resync count exceeded)" |
502 | return 0 | |
039cf96d AC |
503 | } |
504 | ||
4ce44c66 | 505 | |
2307bd6a | 506 | # gdb_test_multiple COMMAND MESSAGE EXPECT_ARGUMENTS |
8dbfb380 | 507 | # Send a command to gdb; test the result. |
c906108c SS |
508 | # |
509 | # COMMAND is the command to execute, send to GDB with send_gdb. If | |
510 | # this is the null string no command is sent. | |
2307bd6a DJ |
511 | # MESSAGE is a message to be printed with the built-in failure patterns |
512 | # if one of them matches. If MESSAGE is empty COMMAND will be used. | |
513 | # EXPECT_ARGUMENTS will be fed to expect in addition to the standard | |
514 | # patterns. Pattern elements will be evaluated in the caller's | |
515 | # context; action elements will be executed in the caller's context. | |
516 | # Unlike patterns for gdb_test, these patterns should generally include | |
517 | # the final newline and prompt. | |
c906108c SS |
518 | # |
519 | # Returns: | |
2307bd6a DJ |
520 | # 1 if the test failed, according to a built-in failure pattern |
521 | # 0 if only user-supplied patterns matched | |
c906108c SS |
522 | # -1 if there was an internal error. |
523 | # | |
d422fe19 AC |
524 | # You can use this function thus: |
525 | # | |
526 | # gdb_test_multiple "print foo" "test foo" { | |
527 | # -re "expected output 1" { | |
528 | # pass "print foo" | |
529 | # } | |
530 | # -re "expected output 2" { | |
531 | # fail "print foo" | |
532 | # } | |
533 | # } | |
534 | # | |
535 | # The standard patterns, such as "Program exited..." and "A problem | |
536 | # ...", all being implicitly appended to that list. | |
537 | # | |
2307bd6a | 538 | proc gdb_test_multiple { command message user_code } { |
c906108c SS |
539 | global verbose |
540 | global gdb_prompt | |
541 | global GDB | |
542 | upvar timeout timeout | |
c47cebdb | 543 | upvar expect_out expect_out |
c906108c | 544 | |
2307bd6a DJ |
545 | if { $message == "" } { |
546 | set message $command | |
c906108c | 547 | } |
c906108c | 548 | |
2307bd6a DJ |
549 | # TCL/EXPECT WART ALERT |
550 | # Expect does something very strange when it receives a single braced | |
551 | # argument. It splits it along word separators and performs substitutions. | |
552 | # This means that { "[ab]" } is evaluated as "[ab]", but { "\[ab\]" } is | |
553 | # evaluated as "\[ab\]". But that's not how TCL normally works; inside a | |
554 | # double-quoted list item, "\[ab\]" is just a long way of representing | |
555 | # "[ab]", because the backslashes will be removed by lindex. | |
556 | ||
557 | # Unfortunately, there appears to be no easy way to duplicate the splitting | |
558 | # that expect will do from within TCL. And many places make use of the | |
559 | # "\[0-9\]" construct, so we need to support that; and some places make use | |
560 | # of the "[func]" construct, so we need to support that too. In order to | |
561 | # get this right we have to substitute quoted list elements differently | |
562 | # from braced list elements. | |
563 | ||
564 | # We do this roughly the same way that Expect does it. We have to use two | |
565 | # lists, because if we leave unquoted newlines in the argument to uplevel | |
566 | # they'll be treated as command separators, and if we escape newlines | |
567 | # we mangle newlines inside of command blocks. This assumes that the | |
568 | # input doesn't contain a pattern which contains actual embedded newlines | |
569 | # at this point! | |
570 | ||
571 | regsub -all {\n} ${user_code} { } subst_code | |
572 | set subst_code [uplevel list $subst_code] | |
573 | ||
574 | set processed_code "" | |
575 | set patterns "" | |
576 | set expecting_action 0 | |
577 | foreach item $user_code subst_item $subst_code { | |
578 | if { $item == "-n" || $item == "-notransfer" || $item == "-nocase" } { | |
579 | lappend processed_code $item | |
580 | continue | |
581 | } | |
582 | if {$item == "-indices" || $item == "-re" || $item == "-ex"} { | |
583 | lappend processed_code $item | |
584 | continue | |
585 | } | |
586 | if { $expecting_action } { | |
587 | lappend processed_code "uplevel [list $item]" | |
588 | set expecting_action 0 | |
589 | # Cosmetic, no effect on the list. | |
590 | append processed_code "\n" | |
591 | continue | |
592 | } | |
593 | set expecting_action 1 | |
594 | lappend processed_code $subst_item | |
595 | if {$patterns != ""} { | |
596 | append patterns "; " | |
597 | } | |
598 | append patterns "\"$subst_item\"" | |
c906108c SS |
599 | } |
600 | ||
2307bd6a DJ |
601 | # Also purely cosmetic. |
602 | regsub -all {\r} $patterns {\\r} patterns | |
603 | regsub -all {\n} $patterns {\\n} patterns | |
604 | ||
c906108c SS |
605 | if $verbose>2 then { |
606 | send_user "Sending \"$command\" to gdb\n" | |
2307bd6a | 607 | send_user "Looking to match \"$patterns\"\n" |
c906108c SS |
608 | send_user "Message is \"$message\"\n" |
609 | } | |
610 | ||
611 | set result -1 | |
612 | set string "${command}\n"; | |
613 | if { $command != "" } { | |
614 | while { "$string" != "" } { | |
615 | set foo [string first "\n" "$string"]; | |
616 | set len [string length "$string"]; | |
617 | if { $foo < [expr $len - 1] } { | |
618 | set str [string range "$string" 0 $foo]; | |
619 | if { [send_gdb "$str"] != "" } { | |
620 | global suppress_flag; | |
621 | ||
622 | if { ! $suppress_flag } { | |
623 | perror "Couldn't send $command to GDB."; | |
624 | } | |
625 | fail "$message"; | |
626 | return $result; | |
627 | } | |
a0b3c4fd JM |
628 | # since we're checking if each line of the multi-line |
629 | # command are 'accepted' by GDB here, | |
630 | # we need to set -notransfer expect option so that | |
631 | # command output is not lost for pattern matching | |
632 | # - guo | |
5f279fa6 DJ |
633 | gdb_expect 2 { |
634 | -notransfer -re "\[\r\n\]" { verbose "partial: match" 3 } | |
635 | timeout { verbose "partial: timeout" 3 } | |
c906108c SS |
636 | } |
637 | set string [string range "$string" [expr $foo + 1] end]; | |
638 | } else { | |
639 | break; | |
640 | } | |
641 | } | |
642 | if { "$string" != "" } { | |
643 | if { [send_gdb "$string"] != "" } { | |
644 | global suppress_flag; | |
645 | ||
646 | if { ! $suppress_flag } { | |
647 | perror "Couldn't send $command to GDB."; | |
648 | } | |
649 | fail "$message"; | |
650 | return $result; | |
651 | } | |
652 | } | |
653 | } | |
654 | ||
9d2e1bab ND |
655 | if [target_info exists gdb,timeout] { |
656 | set tmt [target_info gdb,timeout]; | |
c906108c | 657 | } else { |
c906108c SS |
658 | if [info exists timeout] { |
659 | set tmt $timeout; | |
660 | } else { | |
9d2e1bab ND |
661 | global timeout; |
662 | if [info exists timeout] { | |
663 | set tmt $timeout; | |
664 | } else { | |
665 | set tmt 60; | |
666 | } | |
c906108c SS |
667 | } |
668 | } | |
2307bd6a DJ |
669 | |
670 | set code { | |
039cf96d AC |
671 | -re ".*A problem internal to GDB has been detected" { |
672 | fail "$message (GDB internal error)" | |
673 | gdb_internal_error_resync | |
674 | } | |
c906108c SS |
675 | -re "\\*\\*\\* DOSEXIT code.*" { |
676 | if { $message != "" } { | |
677 | fail "$message"; | |
678 | } | |
679 | gdb_suppress_entire_file "GDB died"; | |
2307bd6a | 680 | set result -1; |
c906108c | 681 | } |
b0f4b84b DJ |
682 | } |
683 | append code $processed_code | |
684 | append code { | |
9e0b60a8 | 685 | -re "Ending remote debugging.*$gdb_prompt $" { |
c906108c SS |
686 | if ![isnative] then { |
687 | warning "Can`t communicate to remote target." | |
688 | } | |
689 | gdb_exit | |
690 | gdb_start | |
691 | set result -1 | |
692 | } | |
9e0b60a8 | 693 | -re "Undefined\[a-z\]* command:.*$gdb_prompt $" { |
c906108c | 694 | perror "Undefined command \"$command\"." |
9e0b60a8 | 695 | fail "$message" |
c906108c SS |
696 | set result 1 |
697 | } | |
698 | -re "Ambiguous command.*$gdb_prompt $" { | |
699 | perror "\"$command\" is not a unique command name." | |
9e0b60a8 | 700 | fail "$message" |
c906108c SS |
701 | set result 1 |
702 | } | |
703 | -re "Program exited with code \[0-9\]+.*$gdb_prompt $" { | |
704 | if ![string match "" $message] then { | |
ed4c619a | 705 | set errmsg "$message (the program exited)" |
c906108c | 706 | } else { |
ed4c619a | 707 | set errmsg "$command (the program exited)" |
c906108c SS |
708 | } |
709 | fail "$errmsg" | |
2307bd6a | 710 | set result -1 |
cb9a9d3e MS |
711 | } |
712 | -re "EXIT code \[0-9\r\n\]+Program exited normally.*$gdb_prompt $" { | |
713 | if ![string match "" $message] then { | |
ed4c619a | 714 | set errmsg "$message (the program exited)" |
cb9a9d3e | 715 | } else { |
ed4c619a | 716 | set errmsg "$command (the program exited)" |
cb9a9d3e MS |
717 | } |
718 | fail "$errmsg" | |
2307bd6a | 719 | set result -1 |
c906108c SS |
720 | } |
721 | -re "The program is not being run.*$gdb_prompt $" { | |
722 | if ![string match "" $message] then { | |
ed4c619a | 723 | set errmsg "$message (the program is no longer running)" |
c906108c | 724 | } else { |
ed4c619a | 725 | set errmsg "$command (the program is no longer running)" |
c906108c SS |
726 | } |
727 | fail "$errmsg" | |
2307bd6a | 728 | set result -1 |
c906108c | 729 | } |
734b8fe8 | 730 | -re "\r\n$gdb_prompt $" { |
c906108c SS |
731 | if ![string match "" $message] then { |
732 | fail "$message" | |
733 | } | |
734 | set result 1 | |
735 | } | |
736 | "<return>" { | |
737 | send_gdb "\n" | |
738 | perror "Window too small." | |
9e0b60a8 | 739 | fail "$message" |
2307bd6a | 740 | set result -1 |
c906108c SS |
741 | } |
742 | -re "\\(y or n\\) " { | |
743 | send_gdb "n\n" | |
744 | perror "Got interactive prompt." | |
9e0b60a8 | 745 | fail "$message" |
2307bd6a | 746 | set result -1 |
c906108c SS |
747 | } |
748 | eof { | |
749 | perror "Process no longer exists" | |
750 | if { $message != "" } { | |
751 | fail "$message" | |
752 | } | |
753 | return -1 | |
754 | } | |
755 | full_buffer { | |
756 | perror "internal buffer is full." | |
9e0b60a8 | 757 | fail "$message" |
2307bd6a | 758 | set result -1 |
c906108c SS |
759 | } |
760 | timeout { | |
761 | if ![string match "" $message] then { | |
762 | fail "$message (timeout)" | |
763 | } | |
764 | set result 1 | |
765 | } | |
766 | } | |
2307bd6a DJ |
767 | |
768 | set result 0 | |
04f6ecf2 DJ |
769 | set code [catch {gdb_expect $tmt $code} string] |
770 | if {$code == 1} { | |
771 | global errorInfo errorCode; | |
772 | return -code error -errorinfo $errorInfo -errorcode $errorCode $string | |
773 | } elseif {$code == 2} { | |
774 | return -code return $string | |
775 | } elseif {$code == 3} { | |
776 | return | |
777 | } elseif {$code > 4} { | |
778 | return -code $code $string | |
779 | } | |
c906108c SS |
780 | return $result |
781 | } | |
2307bd6a DJ |
782 | |
783 | # gdb_test COMMAND PATTERN MESSAGE QUESTION RESPONSE | |
784 | # Send a command to gdb; test the result. | |
785 | # | |
786 | # COMMAND is the command to execute, send to GDB with send_gdb. If | |
787 | # this is the null string no command is sent. | |
788 | # PATTERN is the pattern to match for a PASS, and must NOT include | |
789 | # the \r\n sequence immediately before the gdb prompt. | |
790 | # MESSAGE is an optional message to be printed. If this is | |
791 | # omitted, then the pass/fail messages use the command string as the | |
792 | # message. (If this is the empty string, then sometimes we don't | |
793 | # call pass or fail at all; I don't understand this at all.) | |
794 | # QUESTION is a question GDB may ask in response to COMMAND, like | |
795 | # "are you sure?" | |
796 | # RESPONSE is the response to send if QUESTION appears. | |
797 | # | |
798 | # Returns: | |
799 | # 1 if the test failed, | |
800 | # 0 if the test passes, | |
801 | # -1 if there was an internal error. | |
802 | # | |
803 | proc gdb_test { args } { | |
804 | global verbose | |
805 | global gdb_prompt | |
806 | global GDB | |
807 | upvar timeout timeout | |
808 | ||
809 | if [llength $args]>2 then { | |
810 | set message [lindex $args 2] | |
811 | } else { | |
812 | set message [lindex $args 0] | |
813 | } | |
814 | set command [lindex $args 0] | |
815 | set pattern [lindex $args 1] | |
816 | ||
817 | if [llength $args]==5 { | |
818 | set question_string [lindex $args 3]; | |
819 | set response_string [lindex $args 4]; | |
820 | } else { | |
821 | set question_string "^FOOBAR$" | |
822 | } | |
823 | ||
824 | return [gdb_test_multiple $command $message { | |
825 | -re "\[\r\n\]*($pattern)\[\r\n\]+$gdb_prompt $" { | |
826 | if ![string match "" $message] then { | |
827 | pass "$message" | |
828 | } | |
829 | } | |
830 | -re "(${question_string})$" { | |
831 | send_gdb "$response_string\n"; | |
832 | exp_continue; | |
833 | } | |
834 | }] | |
835 | } | |
c906108c SS |
836 | \f |
837 | # Test that a command gives an error. For pass or fail, return | |
838 | # a 1 to indicate that more tests can proceed. However a timeout | |
839 | # is a serious error, generates a special fail message, and causes | |
840 | # a 0 to be returned to indicate that more tests are likely to fail | |
841 | # as well. | |
842 | ||
843 | proc test_print_reject { args } { | |
844 | global gdb_prompt | |
845 | global verbose | |
846 | ||
847 | if [llength $args]==2 then { | |
848 | set expectthis [lindex $args 1] | |
849 | } else { | |
850 | set expectthis "should never match this bogus string" | |
851 | } | |
852 | set sendthis [lindex $args 0] | |
853 | if $verbose>2 then { | |
854 | send_user "Sending \"$sendthis\" to gdb\n" | |
855 | send_user "Looking to match \"$expectthis\"\n" | |
856 | } | |
857 | send_gdb "$sendthis\n" | |
858 | #FIXME: Should add timeout as parameter. | |
859 | gdb_expect { | |
860 | -re "A .* in expression.*\\.*$gdb_prompt $" { | |
861 | pass "reject $sendthis" | |
862 | return 1 | |
863 | } | |
864 | -re "Invalid syntax in expression.*$gdb_prompt $" { | |
865 | pass "reject $sendthis" | |
866 | return 1 | |
867 | } | |
868 | -re "Junk after end of expression.*$gdb_prompt $" { | |
869 | pass "reject $sendthis" | |
870 | return 1 | |
871 | } | |
872 | -re "Invalid number.*$gdb_prompt $" { | |
873 | pass "reject $sendthis" | |
874 | return 1 | |
875 | } | |
876 | -re "Invalid character constant.*$gdb_prompt $" { | |
877 | pass "reject $sendthis" | |
878 | return 1 | |
879 | } | |
880 | -re "No symbol table is loaded.*$gdb_prompt $" { | |
881 | pass "reject $sendthis" | |
882 | return 1 | |
883 | } | |
884 | -re "No symbol .* in current context.*$gdb_prompt $" { | |
885 | pass "reject $sendthis" | |
886 | return 1 | |
887 | } | |
c4b7bc2b JB |
888 | -re "Unmatched single quote.*$gdb_prompt $" { |
889 | pass "reject $sendthis" | |
890 | return 1 | |
891 | } | |
892 | -re "A character constant must contain at least one character.*$gdb_prompt $" { | |
893 | pass "reject $sendthis" | |
894 | return 1 | |
895 | } | |
c906108c SS |
896 | -re "$expectthis.*$gdb_prompt $" { |
897 | pass "reject $sendthis" | |
898 | return 1 | |
899 | } | |
900 | -re ".*$gdb_prompt $" { | |
901 | fail "reject $sendthis" | |
902 | return 1 | |
903 | } | |
904 | default { | |
905 | fail "reject $sendthis (eof or timeout)" | |
906 | return 0 | |
907 | } | |
908 | } | |
909 | } | |
910 | \f | |
911 | # Given an input string, adds backslashes as needed to create a | |
912 | # regexp that will match the string. | |
913 | ||
914 | proc string_to_regexp {str} { | |
915 | set result $str | |
39fb8e9e | 916 | regsub -all {[]*+.|()^$\[\\]} $str {\\&} result |
c906108c SS |
917 | return $result |
918 | } | |
919 | ||
920 | # Same as gdb_test, but the second parameter is not a regexp, | |
921 | # but a string that must match exactly. | |
922 | ||
923 | proc gdb_test_exact { args } { | |
924 | upvar timeout timeout | |
925 | ||
926 | set command [lindex $args 0] | |
927 | ||
928 | # This applies a special meaning to a null string pattern. Without | |
929 | # this, "$pattern\r\n$gdb_prompt $" will match anything, including error | |
930 | # messages from commands that should have no output except a new | |
931 | # prompt. With this, only results of a null string will match a null | |
932 | # string pattern. | |
933 | ||
934 | set pattern [lindex $args 1] | |
935 | if [string match $pattern ""] { | |
936 | set pattern [string_to_regexp [lindex $args 0]] | |
937 | } else { | |
938 | set pattern [string_to_regexp [lindex $args 1]] | |
939 | } | |
940 | ||
941 | # It is most natural to write the pattern argument with only | |
942 | # embedded \n's, especially if you are trying to avoid Tcl quoting | |
943 | # problems. But gdb_expect really wants to see \r\n in patterns. So | |
944 | # transform the pattern here. First transform \r\n back to \n, in | |
945 | # case some users of gdb_test_exact already do the right thing. | |
946 | regsub -all "\r\n" $pattern "\n" pattern | |
947 | regsub -all "\n" $pattern "\r\n" pattern | |
948 | if [llength $args]==3 then { | |
949 | set message [lindex $args 2] | |
950 | } else { | |
951 | set message $command | |
952 | } | |
953 | ||
954 | return [gdb_test $command $pattern $message] | |
955 | } | |
956 | \f | |
957 | proc gdb_reinitialize_dir { subdir } { | |
958 | global gdb_prompt | |
959 | ||
960 | if [is_remote host] { | |
961 | return ""; | |
962 | } | |
963 | send_gdb "dir\n" | |
964 | gdb_expect 60 { | |
965 | -re "Reinitialize source path to empty.*y or n. " { | |
966 | send_gdb "y\n" | |
967 | gdb_expect 60 { | |
968 | -re "Source directories searched.*$gdb_prompt $" { | |
969 | send_gdb "dir $subdir\n" | |
970 | gdb_expect 60 { | |
971 | -re "Source directories searched.*$gdb_prompt $" { | |
972 | verbose "Dir set to $subdir" | |
973 | } | |
974 | -re "$gdb_prompt $" { | |
975 | perror "Dir \"$subdir\" failed." | |
976 | } | |
977 | } | |
978 | } | |
979 | -re "$gdb_prompt $" { | |
980 | perror "Dir \"$subdir\" failed." | |
981 | } | |
982 | } | |
983 | } | |
984 | -re "$gdb_prompt $" { | |
985 | perror "Dir \"$subdir\" failed." | |
986 | } | |
987 | } | |
988 | } | |
989 | ||
990 | # | |
991 | # gdb_exit -- exit the GDB, killing the target program if necessary | |
992 | # | |
993 | proc default_gdb_exit {} { | |
994 | global GDB | |
995 | global GDBFLAGS | |
996 | global verbose | |
997 | global gdb_spawn_id; | |
998 | ||
999 | gdb_stop_suppressing_tests; | |
1000 | ||
1001 | if ![info exists gdb_spawn_id] { | |
1002 | return; | |
1003 | } | |
1004 | ||
1005 | verbose "Quitting $GDB $GDBFLAGS" | |
1006 | ||
1007 | if { [is_remote host] && [board_info host exists fileid] } { | |
1008 | send_gdb "quit\n"; | |
1009 | gdb_expect 10 { | |
1010 | -re "y or n" { | |
1011 | send_gdb "y\n"; | |
1012 | exp_continue; | |
1013 | } | |
1014 | -re "DOSEXIT code" { } | |
1015 | default { } | |
1016 | } | |
1017 | } | |
1018 | ||
1019 | if ![is_remote host] { | |
1020 | remote_close host; | |
1021 | } | |
1022 | unset gdb_spawn_id | |
1023 | } | |
1024 | ||
3e3ffd2b | 1025 | # Load a file into the debugger. |
2db8e78e | 1026 | # The return value is 0 for success, -1 for failure. |
c906108c | 1027 | # |
2db8e78e MC |
1028 | # This procedure also set the global variable GDB_FILE_CMD_DEBUG_INFO |
1029 | # to one of these values: | |
3e3ffd2b | 1030 | # |
2db8e78e MC |
1031 | # debug file was loaded successfully and has debug information |
1032 | # nodebug file was loaded successfully and has no debug information | |
1033 | # fail file was not loaded | |
c906108c | 1034 | # |
2db8e78e MC |
1035 | # I tried returning this information as part of the return value, |
1036 | # but ran into a mess because of the many re-implementations of | |
1037 | # gdb_load in config/*.exp. | |
3e3ffd2b | 1038 | # |
2db8e78e MC |
1039 | # TODO: gdb.base/sepdebug.exp and gdb.stabs/weird.exp might be able to use |
1040 | # this if they can get more information set. | |
3e3ffd2b | 1041 | |
c906108c | 1042 | proc gdb_file_cmd { arg } { |
3e3ffd2b | 1043 | global gdb_prompt |
c906108c | 1044 | global verbose |
c906108c | 1045 | global GDB |
b741e217 DJ |
1046 | global last_loaded_file |
1047 | ||
1048 | set last_loaded_file $arg | |
c906108c | 1049 | |
2db8e78e MC |
1050 | # Set whether debug info was found. |
1051 | # Default to "fail". | |
1052 | global gdb_file_cmd_debug_info | |
1053 | set gdb_file_cmd_debug_info "fail" | |
1054 | ||
c906108c | 1055 | if [is_remote host] { |
3e3ffd2b | 1056 | set arg [remote_download host $arg] |
c906108c | 1057 | if { $arg == "" } { |
2db8e78e MC |
1058 | perror "download failed" |
1059 | return -1 | |
c906108c SS |
1060 | } |
1061 | } | |
1062 | ||
4c42eaff DJ |
1063 | # The file command used to kill the remote target. For the benefit |
1064 | # of the testsuite, preserve this behavior. | |
1065 | send_gdb "kill\n" | |
1066 | gdb_expect 120 { | |
1067 | -re "Kill the program being debugged. .y or n. $" { | |
1068 | send_gdb "y\n" | |
1069 | verbose "\t\tKilling previous program being debugged" | |
1070 | exp_continue | |
1071 | } | |
1072 | -re "$gdb_prompt $" { | |
1073 | # OK. | |
1074 | } | |
1075 | } | |
1076 | ||
c906108c SS |
1077 | send_gdb "file $arg\n" |
1078 | gdb_expect 120 { | |
3e3ffd2b MC |
1079 | -re "Reading symbols from.*no debugging symbols found.*done.*$gdb_prompt $" { |
1080 | verbose "\t\tLoaded $arg into the $GDB with no debugging symbols" | |
2db8e78e MC |
1081 | set gdb_file_cmd_debug_info "nodebug" |
1082 | return 0 | |
3e3ffd2b | 1083 | } |
c906108c SS |
1084 | -re "Reading symbols from.*done.*$gdb_prompt $" { |
1085 | verbose "\t\tLoaded $arg into the $GDB" | |
2db8e78e MC |
1086 | set gdb_file_cmd_debug_info "debug" |
1087 | return 0 | |
c906108c | 1088 | } |
c906108c SS |
1089 | -re "Load new symbol table from \".*\".*y or n. $" { |
1090 | send_gdb "y\n" | |
1091 | gdb_expect 120 { | |
1092 | -re "Reading symbols from.*done.*$gdb_prompt $" { | |
1093 | verbose "\t\tLoaded $arg with new symbol table into $GDB" | |
2db8e78e MC |
1094 | set gdb_file_cmd_debug_info "debug" |
1095 | return 0 | |
c906108c SS |
1096 | } |
1097 | timeout { | |
2db8e78e MC |
1098 | perror "(timeout) Couldn't load $arg, other program already loaded." |
1099 | return -1 | |
c906108c SS |
1100 | } |
1101 | } | |
1102 | } | |
1103 | -re "No such file or directory.*$gdb_prompt $" { | |
2db8e78e MC |
1104 | perror "($arg) No such file or directory" |
1105 | return -1 | |
c906108c SS |
1106 | } |
1107 | -re "$gdb_prompt $" { | |
2db8e78e MC |
1108 | perror "couldn't load $arg into $GDB." |
1109 | return -1 | |
c906108c SS |
1110 | } |
1111 | timeout { | |
2db8e78e MC |
1112 | perror "couldn't load $arg into $GDB (timed out)." |
1113 | return -1 | |
c906108c SS |
1114 | } |
1115 | eof { | |
1116 | # This is an attempt to detect a core dump, but seems not to | |
1117 | # work. Perhaps we need to match .* followed by eof, in which | |
1118 | # gdb_expect does not seem to have a way to do that. | |
2db8e78e MC |
1119 | perror "couldn't load $arg into $GDB (end of file)." |
1120 | return -1 | |
c906108c SS |
1121 | } |
1122 | } | |
1123 | } | |
1124 | ||
1125 | # | |
1126 | # start gdb -- start gdb running, default procedure | |
1127 | # | |
1128 | # When running over NFS, particularly if running many simultaneous | |
1129 | # tests on different hosts all using the same server, things can | |
1130 | # get really slow. Give gdb at least 3 minutes to start up. | |
1131 | # | |
1132 | proc default_gdb_start { } { | |
1133 | global verbose | |
1134 | global GDB | |
1135 | global GDBFLAGS | |
1136 | global gdb_prompt | |
1137 | global timeout | |
1138 | global gdb_spawn_id; | |
1139 | ||
1140 | gdb_stop_suppressing_tests; | |
1141 | ||
1142 | verbose "Spawning $GDB -nw $GDBFLAGS" | |
1143 | ||
1144 | if [info exists gdb_spawn_id] { | |
1145 | return 0; | |
1146 | } | |
1147 | ||
1148 | if ![is_remote host] { | |
1149 | if { [which $GDB] == 0 } then { | |
1150 | perror "$GDB does not exist." | |
1151 | exit 1 | |
1152 | } | |
1153 | } | |
1154 | set res [remote_spawn host "$GDB -nw $GDBFLAGS [host_info gdb_opts]"]; | |
1155 | if { $res < 0 || $res == "" } { | |
1156 | perror "Spawning $GDB failed." | |
1157 | return 1; | |
1158 | } | |
1159 | gdb_expect 360 { | |
1160 | -re "\[\r\n\]$gdb_prompt $" { | |
1161 | verbose "GDB initialized." | |
1162 | } | |
1163 | -re "$gdb_prompt $" { | |
1164 | perror "GDB never initialized." | |
1165 | return -1 | |
1166 | } | |
1167 | timeout { | |
1168 | perror "(timeout) GDB never initialized after 10 seconds." | |
1169 | remote_close host; | |
1170 | return -1 | |
1171 | } | |
1172 | } | |
1173 | set gdb_spawn_id -1; | |
1174 | # force the height to "unlimited", so no pagers get used | |
1175 | ||
1176 | send_gdb "set height 0\n" | |
1177 | gdb_expect 10 { | |
1178 | -re "$gdb_prompt $" { | |
1179 | verbose "Setting height to 0." 2 | |
1180 | } | |
1181 | timeout { | |
1182 | warning "Couldn't set the height to 0" | |
1183 | } | |
1184 | } | |
1185 | # force the width to "unlimited", so no wraparound occurs | |
1186 | send_gdb "set width 0\n" | |
1187 | gdb_expect 10 { | |
1188 | -re "$gdb_prompt $" { | |
1189 | verbose "Setting width to 0." 2 | |
1190 | } | |
1191 | timeout { | |
1192 | warning "Couldn't set the width to 0." | |
1193 | } | |
1194 | } | |
1195 | return 0; | |
1196 | } | |
1197 | ||
d4f3574e SS |
1198 | # Return a 1 for configurations for which we don't even want to try to |
1199 | # test C++. | |
1200 | ||
1201 | proc skip_cplus_tests {} { | |
d4f3574e SS |
1202 | if { [istarget "h8300-*-*"] } { |
1203 | return 1 | |
1204 | } | |
81d2cbae | 1205 | |
1146c7f1 SC |
1206 | # The C++ IO streams are too large for HC11/HC12 and are thus not |
1207 | # available. The gdb C++ tests use them and don't compile. | |
1208 | if { [istarget "m6811-*-*"] } { | |
1209 | return 1 | |
1210 | } | |
1211 | if { [istarget "m6812-*-*"] } { | |
1212 | return 1 | |
1213 | } | |
d4f3574e SS |
1214 | return 0 |
1215 | } | |
1216 | ||
89a237cb MC |
1217 | # Return a 1 if I don't even want to try to test FORTRAN. |
1218 | ||
1219 | proc skip_fortran_tests {} { | |
1220 | return 0 | |
1221 | } | |
1222 | ||
93f02886 DJ |
1223 | # Return a 1 if we should skip shared library tests. |
1224 | ||
1225 | proc skip_shlib_tests {} { | |
1226 | # Run the shared library tests on native systems. | |
1227 | if {[isnative]} { | |
1228 | return 0 | |
1229 | } | |
1230 | ||
1231 | # An abbreviated list of remote targets where we should be able to | |
1232 | # run shared library tests. | |
1233 | if {([istarget *-*-linux*] | |
1234 | || [istarget *-*-*bsd*] | |
1235 | || [istarget *-*-solaris2*] | |
1236 | || [istarget arm*-*-symbianelf*] | |
1237 | || [istarget *-*-mingw*] | |
1238 | || [istarget *-*-cygwin*] | |
1239 | || [istarget *-*-pe*])} { | |
1240 | return 0 | |
1241 | } | |
1242 | ||
1243 | return 1 | |
1244 | } | |
1245 | ||
3c95e6af PG |
1246 | # Run a test on the target to see if it supports vmx hardware. Return 0 if so, |
1247 | # 1 if it does not. Based on 'check_vmx_hw_available' from the GCC testsuite. | |
1248 | ||
1249 | proc skip_altivec_tests {} { | |
1250 | global skip_vmx_tests_saved | |
1251 | global srcdir subdir gdb_prompt | |
1252 | ||
1253 | # Use the cached value, if it exists. | |
1254 | set me "skip_altivec_tests" | |
1255 | if [info exists skip_vmx_tests_saved] { | |
1256 | verbose "$me: returning saved $skip_vmx_tests_saved" 2 | |
1257 | return $skip_vmx_tests_saved | |
1258 | } | |
1259 | ||
1260 | # Some simulators are known to not support VMX instructions. | |
1261 | if { [istarget powerpc-*-eabi] || [istarget powerpc*-*-eabispe] } { | |
1262 | verbose "$me: target known to not support VMX, returning 1" 2 | |
476308bf | 1263 | return [set skip_vmx_tests_saved 1] |
3c95e6af PG |
1264 | } |
1265 | ||
1266 | # Make sure we have a compiler that understands altivec. | |
fc91c6c2 | 1267 | set compile_flags {debug nowarnings} |
3c95e6af PG |
1268 | if [get_compiler_info not-used] { |
1269 | warning "Could not get compiler info" | |
1270 | return 1 | |
1271 | } | |
1272 | if [test_compiler_info gcc*] { | |
1273 | set compile_flags "$compile_flags additional_flags=-maltivec" | |
1274 | } elseif [test_compiler_info xlc*] { | |
1275 | set compile_flags "$compile_flags additional_flags=-qaltivec" | |
1276 | } else { | |
1277 | verbose "Could not compile with altivec support, returning 1" 2 | |
1278 | return 1 | |
1279 | } | |
1280 | ||
1281 | # Set up, compile, and execute a test program containing VMX instructions. | |
1282 | # Include the current process ID in the file names to prevent conflicts | |
1283 | # with invocations for multiple testsuites. | |
1284 | set src vmx[pid].c | |
1285 | set exe vmx[pid].x | |
1286 | ||
1287 | set f [open $src "w"] | |
1288 | puts $f "int main() {" | |
1289 | puts $f "#ifdef __MACH__" | |
1290 | puts $f " asm volatile (\"vor v0,v0,v0\");" | |
1291 | puts $f "#else" | |
1292 | puts $f " asm volatile (\"vor 0,0,0\");" | |
1293 | puts $f "#endif" | |
1294 | puts $f " return 0; }" | |
1295 | close $f | |
1296 | ||
1297 | verbose "$me: compiling testfile $src" 2 | |
1298 | set lines [gdb_compile $src $exe executable $compile_flags] | |
1299 | file delete $src | |
1300 | ||
1301 | if ![string match "" $lines] then { | |
1302 | verbose "$me: testfile compilation failed, returning 1" 2 | |
1303 | return [set skip_vmx_tests_saved 1] | |
1304 | } | |
1305 | ||
1306 | # No error message, compilation succeeded so now run it via gdb. | |
1307 | ||
1308 | gdb_exit | |
1309 | gdb_start | |
1310 | gdb_reinitialize_dir $srcdir/$subdir | |
1311 | gdb_load "$exe" | |
1312 | gdb_run_cmd | |
1313 | gdb_expect { | |
1314 | -re ".*Illegal instruction.*${gdb_prompt} $" { | |
1315 | verbose -log "\n$me altivec hardware not detected" | |
1316 | set skip_vmx_tests_saved 1 | |
1317 | } | |
1318 | -re ".*Program exited normally.*${gdb_prompt} $" { | |
1319 | verbose -log "\n$me: altivec hardware detected" | |
1320 | set skip_vmx_tests_saved 0 | |
1321 | } | |
1322 | default { | |
1323 | warning "\n$me: default case taken" | |
1324 | set skip_vmx_tests_saved 1 | |
1325 | } | |
1326 | } | |
1327 | gdb_exit | |
1328 | remote_file build delete $exe | |
1329 | ||
1330 | verbose "$me: returning $skip_vmx_tests_saved" 2 | |
1331 | return $skip_vmx_tests_saved | |
1332 | } | |
1333 | ||
7a292a7a SS |
1334 | # Skip all the tests in the file if you are not on an hppa running |
1335 | # hpux target. | |
1336 | ||
1337 | proc skip_hp_tests {} { | |
1338 | eval set skip_hp [ expr ![isnative] || ![istarget "hppa*-*-hpux*"] ] | |
c906108c SS |
1339 | verbose "Skip hp tests is $skip_hp" |
1340 | return $skip_hp | |
1341 | } | |
1342 | ||
94b8e876 MC |
1343 | set compiler_info "unknown" |
1344 | set gcc_compiled 0 | |
1345 | set hp_cc_compiler 0 | |
1346 | set hp_aCC_compiler 0 | |
94b8e876 MC |
1347 | |
1348 | # Figure out what compiler I am using. | |
1349 | # | |
1350 | # BINFILE is a "compiler information" output file. This implementation | |
1351 | # does not use BINFILE. | |
1352 | # | |
1353 | # ARGS can be empty or "C++". If empty, "C" is assumed. | |
1354 | # | |
1355 | # There are several ways to do this, with various problems. | |
1356 | # | |
1357 | # [ gdb_compile -E $ifile -o $binfile.ci ] | |
1358 | # source $binfile.ci | |
1359 | # | |
1360 | # Single Unix Spec v3 says that "-E -o ..." together are not | |
1361 | # specified. And in fact, the native compiler on hp-ux 11 (among | |
1362 | # others) does not work with "-E -o ...". Most targets used to do | |
1363 | # this, and it mostly worked, because it works with gcc. | |
1364 | # | |
1365 | # [ catch "exec $compiler -E $ifile > $binfile.ci" exec_output ] | |
1366 | # source $binfile.ci | |
1367 | # | |
1368 | # This avoids the problem with -E and -o together. This almost works | |
1369 | # if the build machine is the same as the host machine, which is | |
1370 | # usually true of the targets which are not gcc. But this code does | |
1371 | # not figure which compiler to call, and it always ends up using the C | |
1372 | # compiler. Not good for setting hp_aCC_compiler. Targets | |
1373 | # hppa*-*-hpux* and mips*-*-irix* used to do this. | |
1374 | # | |
1375 | # [ gdb_compile -E $ifile > $binfile.ci ] | |
1376 | # source $binfile.ci | |
1377 | # | |
1378 | # dejagnu target_compile says that it supports output redirection, | |
1379 | # but the code is completely different from the normal path and I | |
1380 | # don't want to sweep the mines from that path. So I didn't even try | |
1381 | # this. | |
1382 | # | |
1383 | # set cppout [ gdb_compile $ifile "" preprocess $args quiet ] | |
1384 | # eval $cppout | |
1385 | # | |
1386 | # I actually do this for all targets now. gdb_compile runs the right | |
1387 | # compiler, and TCL captures the output, and I eval the output. | |
1388 | # | |
1389 | # Unfortunately, expect logs the output of the command as it goes by, | |
1390 | # and dejagnu helpfully prints a second copy of it right afterwards. | |
1391 | # So I turn off expect logging for a moment. | |
1392 | # | |
1393 | # [ gdb_compile $ifile $ciexe_file executable $args ] | |
1394 | # [ remote_exec $ciexe_file ] | |
1395 | # [ source $ci_file.out ] | |
1396 | # | |
1397 | # I could give up on -E and just do this. | |
1398 | # I didn't get desperate enough to try this. | |
1399 | # | |
1400 | # -- chastain 2004-01-06 | |
853d6e5b | 1401 | |
c906108c | 1402 | proc get_compiler_info {binfile args} { |
94b8e876 | 1403 | # For compiler.c and compiler.cc |
c906108c | 1404 | global srcdir |
94b8e876 MC |
1405 | |
1406 | # I am going to play with the log to keep noise out. | |
1407 | global outdir | |
1408 | global tool | |
1409 | ||
1410 | # These come from compiler.c or compiler.cc | |
853d6e5b | 1411 | global compiler_info |
4f70a4c9 MC |
1412 | |
1413 | # Legacy global data symbols. | |
94b8e876 MC |
1414 | global gcc_compiled |
1415 | global hp_cc_compiler | |
1416 | global hp_aCC_compiler | |
c906108c | 1417 | |
94b8e876 MC |
1418 | # Choose which file to preprocess. |
1419 | set ifile "${srcdir}/lib/compiler.c" | |
1420 | if { [llength $args] > 0 && [lindex $args 0] == "c++" } { | |
1421 | set ifile "${srcdir}/lib/compiler.cc" | |
c906108c | 1422 | } |
085dd6e6 | 1423 | |
94b8e876 MC |
1424 | # Run $ifile through the right preprocessor. |
1425 | # Toggle gdb.log to keep the compiler output out of the log. | |
1426 | log_file | |
1427 | set cppout [ gdb_compile "${ifile}" "" preprocess [list "$args" quiet] ] | |
1428 | log_file -a "$outdir/$tool.log" | |
1429 | ||
4f70a4c9 MC |
1430 | # Eval the output. |
1431 | set unknown 0 | |
94b8e876 | 1432 | foreach cppline [ split "$cppout" "\n" ] { |
4f70a4c9 MC |
1433 | if { [ regexp "^#" "$cppline" ] } { |
1434 | # line marker | |
1435 | } elseif { [ regexp "^\[\n\r\t \]*$" "$cppline" ] } { | |
1436 | # blank line | |
1437 | } elseif { [ regexp "^\[\n\r\t \]*set\[\n\r\t \]" "$cppline" ] } { | |
1438 | # eval this line | |
1439 | verbose "get_compiler_info: $cppline" 2 | |
1440 | eval "$cppline" | |
1441 | } else { | |
1442 | # unknown line | |
1443 | verbose -log "get_compiler_info: $cppline" | |
1444 | set unknown 1 | |
94b8e876 | 1445 | } |
085dd6e6 | 1446 | } |
4f70a4c9 MC |
1447 | |
1448 | # Reset to unknown compiler if any diagnostics happened. | |
1449 | if { $unknown } { | |
1450 | set compiler_info "unknown" | |
4f70a4c9 MC |
1451 | } |
1452 | ||
1453 | # Set the legacy symbols. | |
1454 | set gcc_compiled 0 | |
1455 | set hp_cc_compiler 0 | |
1456 | set hp_aCC_compiler 0 | |
1457 | if { [regexp "^gcc-1-" "$compiler_info" ] } { set gcc_compiled 1 } | |
1458 | if { [regexp "^gcc-2-" "$compiler_info" ] } { set gcc_compiled 2 } | |
1459 | if { [regexp "^gcc-3-" "$compiler_info" ] } { set gcc_compiled 3 } | |
1460 | if { [regexp "^gcc-4-" "$compiler_info" ] } { set gcc_compiled 4 } | |
1461 | if { [regexp "^gcc-5-" "$compiler_info" ] } { set gcc_compiled 5 } | |
1462 | if { [regexp "^hpcc-" "$compiler_info" ] } { set hp_cc_compiler 1 } | |
1463 | if { [regexp "^hpacc-" "$compiler_info" ] } { set hp_aCC_compiler 1 } | |
1464 | ||
1465 | # Log what happened. | |
94b8e876 | 1466 | verbose -log "get_compiler_info: $compiler_info" |
085dd6e6 JM |
1467 | |
1468 | # Most compilers will evaluate comparisons and other boolean | |
1469 | # operations to 0 or 1. | |
1470 | uplevel \#0 { set true 1 } | |
1471 | uplevel \#0 { set false 0 } | |
1472 | ||
94b8e876 MC |
1473 | # Use of aCC results in boolean results being displayed as |
1474 | # "true" or "false" | |
1475 | if { $hp_aCC_compiler } { | |
1476 | uplevel \#0 { set true true } | |
1477 | uplevel \#0 { set false false } | |
085dd6e6 JM |
1478 | } |
1479 | ||
c906108c SS |
1480 | return 0; |
1481 | } | |
1482 | ||
9b593790 | 1483 | proc test_compiler_info { {compiler ""} } { |
853d6e5b | 1484 | global compiler_info |
6e87504d PG |
1485 | |
1486 | # if no arg, return the compiler_info string | |
1487 | ||
1488 | if [string match "" $compiler] { | |
1489 | if [info exists compiler_info] { | |
1490 | return $compiler_info | |
1491 | } else { | |
1492 | perror "No compiler info found." | |
1493 | } | |
1494 | } | |
1495 | ||
853d6e5b AC |
1496 | return [string match $compiler $compiler_info] |
1497 | } | |
1498 | ||
f1c47eb2 MS |
1499 | set gdb_wrapper_initialized 0 |
1500 | ||
1501 | proc gdb_wrapper_init { args } { | |
1502 | global gdb_wrapper_initialized; | |
1503 | global gdb_wrapper_file; | |
1504 | global gdb_wrapper_flags; | |
1505 | ||
1506 | if { $gdb_wrapper_initialized == 1 } { return; } | |
1507 | ||
1508 | if {[target_info exists needs_status_wrapper] && \ | |
277254ba | 1509 | [target_info needs_status_wrapper] != "0"} { |
f1c47eb2 MS |
1510 | set result [build_wrapper "testglue.o"]; |
1511 | if { $result != "" } { | |
1512 | set gdb_wrapper_file [lindex $result 0]; | |
1513 | set gdb_wrapper_flags [lindex $result 1]; | |
1514 | } else { | |
1515 | warning "Status wrapper failed to build." | |
1516 | } | |
1517 | } | |
1518 | set gdb_wrapper_initialized 1 | |
1519 | } | |
1520 | ||
c906108c SS |
1521 | proc gdb_compile {source dest type options} { |
1522 | global GDB_TESTCASE_OPTIONS; | |
f1c47eb2 MS |
1523 | global gdb_wrapper_file; |
1524 | global gdb_wrapper_flags; | |
1525 | global gdb_wrapper_initialized; | |
c906108c | 1526 | |
695e2681 MK |
1527 | set outdir [file dirname $dest] |
1528 | ||
1529 | # Add platform-specific options if a shared library was specified using | |
1530 | # "shlib=librarypath" in OPTIONS. | |
1531 | set new_options "" | |
1532 | set shlib_found 0 | |
1533 | foreach opt $options { | |
57bf0e56 DJ |
1534 | if [regexp {^shlib=(.*)} $opt dummy_var shlib_name] { |
1535 | if [test_compiler_info "xlc-*"] { | |
93f02886 DJ |
1536 | # IBM xlc compiler doesn't accept shared library named other |
1537 | # than .so: use "-Wl," to bypass this | |
1538 | lappend source "-Wl,$shlib_name" | |
1539 | } elseif { ([istarget "*-*-mingw*"] | |
1540 | || [istarget *-*-cygwin*] | |
1541 | || [istarget *-*-pe*])} { | |
1542 | lappend source "${shlib_name}.a" | |
57bf0e56 DJ |
1543 | } else { |
1544 | lappend source $shlib_name | |
1545 | } | |
1546 | if {$shlib_found == 0} { | |
1547 | set shlib_found 1 | |
1548 | if { ([test_compiler_info "gcc-*"] | |
1549 | && ([istarget "powerpc*-*-aix*"] | |
1550 | || [istarget "rs6000*-*-aix*"] )) } { | |
695e2681 | 1551 | lappend options "additional_flags=-L${outdir}" |
57bf0e56 | 1552 | } elseif { [istarget "mips-sgi-irix*"] } { |
695e2681 | 1553 | lappend options "additional_flags=-rpath ${outdir}" |
57bf0e56 DJ |
1554 | } |
1555 | } | |
b0f4b84b DJ |
1556 | } elseif { $opt == "shlib_load" } { |
1557 | if { ([istarget "*-*-mingw*"] | |
1558 | || [istarget *-*-cygwin*] | |
1559 | || [istarget *-*-pe*] | |
1560 | || [istarget arm*-*-symbianelf*] | |
1561 | || [istarget hppa*-*-hpux*])} { | |
1562 | # Do not need anything. | |
695e2681 MK |
1563 | } elseif { [istarget *-*-openbsd*] } { |
1564 | lappend new_options "additional_flags=-Wl,-rpath,${outdir}" | |
b0f4b84b DJ |
1565 | } else { |
1566 | lappend new_options "libs=-ldl" | |
1567 | lappend new_options "additional_flags=-Wl,-rpath,\\\$ORIGIN" | |
1568 | } | |
57bf0e56 DJ |
1569 | } else { |
1570 | lappend new_options $opt | |
1571 | } | |
695e2681 MK |
1572 | } |
1573 | set options $new_options | |
57bf0e56 | 1574 | |
c906108c SS |
1575 | if [target_info exists gdb_stub] { |
1576 | set options2 { "additional_flags=-Dusestubs" } | |
1577 | lappend options "libs=[target_info gdb_stub]"; | |
1578 | set options [concat $options2 $options] | |
1579 | } | |
1580 | if [target_info exists is_vxworks] { | |
1581 | set options2 { "additional_flags=-Dvxworks" } | |
1582 | lappend options "libs=[target_info gdb_stub]"; | |
1583 | set options [concat $options2 $options] | |
1584 | } | |
1585 | if [info exists GDB_TESTCASE_OPTIONS] { | |
1586 | lappend options "additional_flags=$GDB_TESTCASE_OPTIONS"; | |
1587 | } | |
1588 | verbose "options are $options" | |
1589 | verbose "source is $source $dest $type $options" | |
1590 | ||
f1c47eb2 MS |
1591 | if { $gdb_wrapper_initialized == 0 } { gdb_wrapper_init } |
1592 | ||
1593 | if {[target_info exists needs_status_wrapper] && \ | |
1594 | [target_info needs_status_wrapper] != "0" && \ | |
1595 | [info exists gdb_wrapper_file]} { | |
1596 | lappend options "libs=${gdb_wrapper_file}" | |
1597 | lappend options "ldflags=${gdb_wrapper_flags}" | |
1598 | } | |
1599 | ||
fc91c6c2 PB |
1600 | # Replace the "nowarnings" option with the appropriate additional_flags |
1601 | # to disable compiler warnings. | |
1602 | set nowarnings [lsearch -exact $options nowarnings] | |
1603 | if {$nowarnings != -1} { | |
1604 | if [target_info exists gdb,nowarnings_flag] { | |
1605 | set flag "additional_flags=[target_info gdb,nowarnings_flag]" | |
1606 | } else { | |
1607 | set flag "additional_flags=-w" | |
1608 | } | |
1609 | set options [lreplace $options $nowarnings $nowarnings $flag] | |
1610 | } | |
1611 | ||
c906108c | 1612 | set result [target_compile $source $dest $type $options]; |
93f02886 DJ |
1613 | |
1614 | # Prune uninteresting compiler (and linker) output. | |
1615 | regsub "Creating library file: \[^\r\n\]*\[\r\n\]+" $result "" result | |
1616 | ||
c906108c SS |
1617 | regsub "\[\r\n\]*$" "$result" "" result; |
1618 | regsub "^\[\r\n\]*" "$result" "" result; | |
93f02886 | 1619 | |
81d2cbae NS |
1620 | if { $result != "" && [lsearch $options quiet] == -1} { |
1621 | clone_output "gdb compile failed, $result" | |
c906108c SS |
1622 | } |
1623 | return $result; | |
1624 | } | |
1625 | ||
b6ff0e81 JB |
1626 | |
1627 | # This is just like gdb_compile, above, except that it tries compiling | |
1628 | # against several different thread libraries, to see which one this | |
1629 | # system has. | |
1630 | proc gdb_compile_pthreads {source dest type options} { | |
0ae67eb3 | 1631 | set built_binfile 0 |
b6ff0e81 JB |
1632 | set why_msg "unrecognized error" |
1633 | foreach lib {-lpthreads -lpthread -lthread} { | |
1634 | # This kind of wipes out whatever libs the caller may have | |
1635 | # set. Or maybe theirs will override ours. How infelicitous. | |
b5ab8ff3 | 1636 | set options_with_lib [concat $options [list libs=$lib quiet]] |
b6ff0e81 JB |
1637 | set ccout [gdb_compile $source $dest $type $options_with_lib] |
1638 | switch -regexp -- $ccout { | |
1639 | ".*no posix threads support.*" { | |
1640 | set why_msg "missing threads include file" | |
1641 | break | |
1642 | } | |
1643 | ".*cannot open -lpthread.*" { | |
1644 | set why_msg "missing runtime threads library" | |
1645 | } | |
1646 | ".*Can't find library for -lpthread.*" { | |
1647 | set why_msg "missing runtime threads library" | |
1648 | } | |
1649 | {^$} { | |
1650 | pass "successfully compiled posix threads test case" | |
1651 | set built_binfile 1 | |
1652 | break | |
1653 | } | |
1654 | } | |
1655 | } | |
0ae67eb3 | 1656 | if {!$built_binfile} { |
b6ff0e81 JB |
1657 | unsupported "Couldn't compile $source: ${why_msg}" |
1658 | return -1 | |
1659 | } | |
57bf0e56 DJ |
1660 | } |
1661 | ||
1662 | # Build a shared library from SOURCES. You must use get_compiler_info | |
1663 | # first. | |
1664 | ||
1665 | proc gdb_compile_shlib {sources dest options} { | |
1666 | set obj_options $options | |
1667 | ||
1668 | switch -glob [test_compiler_info] { | |
1669 | "xlc-*" { | |
1670 | lappend obj_options "additional_flags=-qpic" | |
1671 | } | |
1672 | "gcc-*" { | |
1673 | if { !([istarget "powerpc*-*-aix*"] | |
227c54da DJ |
1674 | || [istarget "rs6000*-*-aix*"] |
1675 | || [istarget "*-*-cygwin*"] | |
1676 | || [istarget "*-*-mingw*"] | |
1677 | || [istarget "*-*-pe*"]) } { | |
57bf0e56 DJ |
1678 | lappend obj_options "additional_flags=-fpic" |
1679 | } | |
1680 | } | |
1681 | default { | |
1682 | switch -glob [istarget] { | |
1683 | "hppa*-hp-hpux*" { | |
1684 | lappend obj_options "additional_flags=+z" | |
1685 | } | |
1686 | "mips-sgi-irix*" { | |
1687 | # Disable SGI compiler's implicit -Dsgi | |
1688 | lappend obj_options "additional_flags=-Usgi" | |
1689 | } | |
1690 | default { | |
1691 | # don't know what the compiler is... | |
1692 | } | |
1693 | } | |
1694 | } | |
1695 | } | |
1696 | ||
1697 | set outdir [file dirname $dest] | |
1698 | set objects "" | |
1699 | foreach source $sources { | |
1700 | set sourcebase [file tail $source] | |
1701 | if {[gdb_compile $source "${outdir}/${sourcebase}.o" object $obj_options] != ""} { | |
1702 | return -1 | |
1703 | } | |
1704 | lappend objects ${outdir}/${sourcebase}.o | |
1705 | } | |
1706 | ||
1707 | if [istarget "hppa*-*-hpux*"] { | |
1708 | remote_exec build "ld -b ${objects} -o ${dest}" | |
1709 | } else { | |
1710 | set link_options $options | |
1711 | if [test_compiler_info "xlc-*"] { | |
1712 | lappend link_options "additional_flags=-qmkshrobj" | |
1713 | } else { | |
1714 | lappend link_options "additional_flags=-shared" | |
93f02886 DJ |
1715 | |
1716 | if { ([istarget "*-*-mingw*"] | |
1717 | || [istarget *-*-cygwin*] | |
1718 | || [istarget *-*-pe*])} { | |
1719 | lappend link_options "additional_flags=-Wl,--out-implib,${dest}.a" | |
1720 | } | |
57bf0e56 DJ |
1721 | } |
1722 | if {[gdb_compile "${objects}" "${dest}" executable $link_options] != ""} { | |
1723 | return -1 | |
1724 | } | |
1725 | } | |
b6ff0e81 JB |
1726 | } |
1727 | ||
130cacce AF |
1728 | # This is just like gdb_compile_pthreads, above, except that we always add the |
1729 | # objc library for compiling Objective-C programs | |
1730 | proc gdb_compile_objc {source dest type options} { | |
1731 | set built_binfile 0 | |
1732 | set why_msg "unrecognized error" | |
1733 | foreach lib {-lobjc -lpthreads -lpthread -lthread solaris} { | |
1734 | # This kind of wipes out whatever libs the caller may have | |
1735 | # set. Or maybe theirs will override ours. How infelicitous. | |
1736 | if { $lib == "solaris" } { | |
1737 | set lib "-lpthread -lposix4" | |
1738 | } | |
1739 | if { $lib != "-lobjc" } { | |
1740 | set lib "-lobjc $lib" | |
1741 | } | |
1742 | set options_with_lib [concat $options [list libs=$lib quiet]] | |
1743 | set ccout [gdb_compile $source $dest $type $options_with_lib] | |
1744 | switch -regexp -- $ccout { | |
1745 | ".*no posix threads support.*" { | |
1746 | set why_msg "missing threads include file" | |
1747 | break | |
1748 | } | |
1749 | ".*cannot open -lpthread.*" { | |
1750 | set why_msg "missing runtime threads library" | |
1751 | } | |
1752 | ".*Can't find library for -lpthread.*" { | |
1753 | set why_msg "missing runtime threads library" | |
1754 | } | |
1755 | {^$} { | |
1756 | pass "successfully compiled objc with posix threads test case" | |
1757 | set built_binfile 1 | |
1758 | break | |
1759 | } | |
1760 | } | |
1761 | } | |
1762 | if {!$built_binfile} { | |
1763 | unsupported "Couldn't compile $source: ${why_msg}" | |
1764 | return -1 | |
1765 | } | |
1766 | } | |
1767 | ||
c906108c SS |
1768 | proc send_gdb { string } { |
1769 | global suppress_flag; | |
1770 | if { $suppress_flag } { | |
1771 | return "suppressed"; | |
1772 | } | |
1773 | return [remote_send host "$string"]; | |
1774 | } | |
1775 | ||
1776 | # | |
1777 | # | |
1778 | ||
1779 | proc gdb_expect { args } { | |
1780 | if { [llength $args] == 2 && [lindex $args 0] != "-re" } { | |
2f34202f | 1781 | set atimeout [lindex $args 0]; |
c906108c SS |
1782 | set expcode [list [lindex $args 1]]; |
1783 | } else { | |
c906108c | 1784 | set expcode $args; |
2f34202f MR |
1785 | } |
1786 | ||
1787 | upvar timeout timeout; | |
1788 | ||
1789 | if [target_info exists gdb,timeout] { | |
1790 | if [info exists timeout] { | |
1791 | if { $timeout < [target_info gdb,timeout] } { | |
c906108c | 1792 | set gtimeout [target_info gdb,timeout]; |
2f34202f MR |
1793 | } else { |
1794 | set gtimeout $timeout; | |
c906108c | 1795 | } |
2f34202f MR |
1796 | } else { |
1797 | set gtimeout [target_info gdb,timeout]; | |
c906108c | 1798 | } |
2f34202f | 1799 | } |
c906108c | 1800 | |
2f34202f MR |
1801 | if ![info exists gtimeout] { |
1802 | global timeout; | |
1803 | if [info exists timeout] { | |
1804 | set gtimeout $timeout; | |
1805 | } | |
1806 | } | |
1807 | ||
1808 | if [info exists atimeout] { | |
1809 | if { ![info exists gtimeout] || $gtimeout < $atimeout } { | |
1810 | set $gtimeout $atimeout; | |
1811 | } | |
1812 | } else { | |
c906108c | 1813 | if ![info exists gtimeout] { |
2f34202f MR |
1814 | # Eeeeew. |
1815 | set gtimeout 60; | |
c906108c SS |
1816 | } |
1817 | } | |
2f34202f | 1818 | |
c906108c SS |
1819 | global suppress_flag; |
1820 | global remote_suppress_flag; | |
1821 | if [info exists remote_suppress_flag] { | |
1822 | set old_val $remote_suppress_flag; | |
1823 | } | |
1824 | if [info exists suppress_flag] { | |
1825 | if { $suppress_flag } { | |
1826 | set remote_suppress_flag 1; | |
1827 | } | |
1828 | } | |
a0b3c4fd | 1829 | set code [catch \ |
5f279fa6 | 1830 | {uplevel remote_expect host $gtimeout $expcode} string]; |
c906108c SS |
1831 | if [info exists old_val] { |
1832 | set remote_suppress_flag $old_val; | |
1833 | } else { | |
1834 | if [info exists remote_suppress_flag] { | |
1835 | unset remote_suppress_flag; | |
1836 | } | |
1837 | } | |
1838 | ||
1839 | if {$code == 1} { | |
1840 | global errorInfo errorCode; | |
1841 | ||
1842 | return -code error -errorinfo $errorInfo -errorcode $errorCode $string | |
1843 | } elseif {$code == 2} { | |
1844 | return -code return $string | |
1845 | } elseif {$code == 3} { | |
1846 | return | |
1847 | } elseif {$code > 4} { | |
1848 | return -code $code $string | |
1849 | } | |
1850 | } | |
1851 | ||
c2d11a7d | 1852 | # gdb_expect_list MESSAGE SENTINEL LIST -- expect a sequence of outputs |
085dd6e6 JM |
1853 | # |
1854 | # Check for long sequence of output by parts. | |
11cf8741 | 1855 | # MESSAGE: is the test message to be printed with the test success/fail. |
085dd6e6 JM |
1856 | # SENTINEL: Is the terminal pattern indicating that output has finished. |
1857 | # LIST: is the sequence of outputs to match. | |
1858 | # If the sentinel is recognized early, it is considered an error. | |
1859 | # | |
11cf8741 JM |
1860 | # Returns: |
1861 | # 1 if the test failed, | |
1862 | # 0 if the test passes, | |
1863 | # -1 if there was an internal error. | |
1864 | # | |
c2d11a7d | 1865 | proc gdb_expect_list {test sentinel list} { |
085dd6e6 | 1866 | global gdb_prompt |
11cf8741 | 1867 | global suppress_flag |
085dd6e6 | 1868 | set index 0 |
43ff13b4 | 1869 | set ok 1 |
11cf8741 JM |
1870 | if { $suppress_flag } { |
1871 | set ok 0 | |
a20ce2c3 | 1872 | unresolved "${test}" |
11cf8741 | 1873 | } |
43ff13b4 | 1874 | while { ${index} < [llength ${list}] } { |
085dd6e6 JM |
1875 | set pattern [lindex ${list} ${index}] |
1876 | set index [expr ${index} + 1] | |
1877 | if { ${index} == [llength ${list}] } { | |
43ff13b4 JM |
1878 | if { ${ok} } { |
1879 | gdb_expect { | |
c2d11a7d | 1880 | -re "${pattern}${sentinel}" { |
a20ce2c3 | 1881 | # pass "${test}, pattern ${index} + sentinel" |
c2d11a7d JM |
1882 | } |
1883 | -re "${sentinel}" { | |
a20ce2c3 | 1884 | fail "${test} (pattern ${index} + sentinel)" |
c2d11a7d | 1885 | set ok 0 |
43ff13b4 | 1886 | } |
5c5455dc AC |
1887 | -re ".*A problem internal to GDB has been detected" { |
1888 | fail "${test} (GDB internal error)" | |
1889 | set ok 0 | |
1890 | gdb_internal_error_resync | |
1891 | } | |
43ff13b4 | 1892 | timeout { |
a20ce2c3 | 1893 | fail "${test} (pattern ${index} + sentinel) (timeout)" |
43ff13b4 JM |
1894 | set ok 0 |
1895 | } | |
085dd6e6 | 1896 | } |
43ff13b4 | 1897 | } else { |
a20ce2c3 | 1898 | # unresolved "${test}, pattern ${index} + sentinel" |
085dd6e6 JM |
1899 | } |
1900 | } else { | |
43ff13b4 JM |
1901 | if { ${ok} } { |
1902 | gdb_expect { | |
1903 | -re "${pattern}" { | |
a20ce2c3 | 1904 | # pass "${test}, pattern ${index}" |
43ff13b4 | 1905 | } |
c2d11a7d | 1906 | -re "${sentinel}" { |
a20ce2c3 | 1907 | fail "${test} (pattern ${index})" |
43ff13b4 JM |
1908 | set ok 0 |
1909 | } | |
5c5455dc AC |
1910 | -re ".*A problem internal to GDB has been detected" { |
1911 | fail "${test} (GDB internal error)" | |
1912 | set ok 0 | |
1913 | gdb_internal_error_resync | |
1914 | } | |
43ff13b4 | 1915 | timeout { |
a20ce2c3 | 1916 | fail "${test} (pattern ${index}) (timeout)" |
43ff13b4 JM |
1917 | set ok 0 |
1918 | } | |
085dd6e6 | 1919 | } |
43ff13b4 | 1920 | } else { |
a20ce2c3 | 1921 | # unresolved "${test}, pattern ${index}" |
085dd6e6 JM |
1922 | } |
1923 | } | |
1924 | } | |
11cf8741 | 1925 | if { ${ok} } { |
a20ce2c3 | 1926 | pass "${test}" |
11cf8741 JM |
1927 | return 0 |
1928 | } else { | |
1929 | return 1 | |
1930 | } | |
085dd6e6 JM |
1931 | } |
1932 | ||
1933 | # | |
1934 | # | |
c906108c SS |
1935 | proc gdb_suppress_entire_file { reason } { |
1936 | global suppress_flag; | |
1937 | ||
1938 | warning "$reason\n"; | |
1939 | set suppress_flag -1; | |
1940 | } | |
1941 | ||
1942 | # | |
1943 | # Set suppress_flag, which will cause all subsequent calls to send_gdb and | |
1944 | # gdb_expect to fail immediately (until the next call to | |
1945 | # gdb_stop_suppressing_tests). | |
1946 | # | |
1947 | proc gdb_suppress_tests { args } { | |
1948 | global suppress_flag; | |
1949 | ||
1950 | return; # fnf - disable pending review of results where | |
1951 | # testsuite ran better without this | |
1952 | incr suppress_flag; | |
1953 | ||
1954 | if { $suppress_flag == 1 } { | |
1955 | if { [llength $args] > 0 } { | |
1956 | warning "[lindex $args 0]\n"; | |
1957 | } else { | |
1958 | warning "Because of previous failure, all subsequent tests in this group will automatically fail.\n"; | |
1959 | } | |
1960 | } | |
1961 | } | |
1962 | ||
1963 | # | |
1964 | # Clear suppress_flag. | |
1965 | # | |
1966 | proc gdb_stop_suppressing_tests { } { | |
1967 | global suppress_flag; | |
1968 | ||
1969 | if [info exists suppress_flag] { | |
1970 | if { $suppress_flag > 0 } { | |
1971 | set suppress_flag 0; | |
1972 | clone_output "Tests restarted.\n"; | |
1973 | } | |
1974 | } else { | |
1975 | set suppress_flag 0; | |
1976 | } | |
1977 | } | |
1978 | ||
1979 | proc gdb_clear_suppressed { } { | |
1980 | global suppress_flag; | |
1981 | ||
1982 | set suppress_flag 0; | |
1983 | } | |
1984 | ||
1985 | proc gdb_start { } { | |
1986 | default_gdb_start | |
1987 | } | |
1988 | ||
1989 | proc gdb_exit { } { | |
1990 | catch default_gdb_exit | |
1991 | } | |
1992 | ||
e63b55d1 NS |
1993 | # |
1994 | # gdb_load_cmd -- load a file into the debugger. | |
1995 | # ARGS - additional args to load command. | |
1996 | # return a -1 if anything goes wrong. | |
1997 | # | |
1998 | proc gdb_load_cmd { args } { | |
1999 | global gdb_prompt | |
2000 | ||
2001 | if [target_info exists gdb_load_timeout] { | |
2002 | set loadtimeout [target_info gdb_load_timeout] | |
2003 | } else { | |
2004 | set loadtimeout 1600 | |
2005 | } | |
2006 | send_gdb "load $args\n" | |
e91528f0 | 2007 | verbose "Timeout is now $loadtimeout seconds" 2 |
e63b55d1 NS |
2008 | gdb_expect $loadtimeout { |
2009 | -re "Loading section\[^\r\]*\r\n" { | |
2010 | exp_continue | |
2011 | } | |
2012 | -re "Start address\[\r\]*\r\n" { | |
2013 | exp_continue | |
2014 | } | |
2015 | -re "Transfer rate\[\r\]*\r\n" { | |
2016 | exp_continue | |
2017 | } | |
2018 | -re "Memory access error\[^\r\]*\r\n" { | |
2019 | perror "Failed to load program" | |
2020 | return -1 | |
2021 | } | |
2022 | -re "$gdb_prompt $" { | |
2023 | return 0 | |
2024 | } | |
2025 | -re "(.*)\r\n$gdb_prompt " { | |
2026 | perror "Unexpected reponse from 'load' -- $expect_out(1,string)" | |
2027 | return -1 | |
2028 | } | |
2029 | timeout { | |
c4b347c7 | 2030 | perror "Timed out trying to load $args." |
e63b55d1 NS |
2031 | return -1 |
2032 | } | |
2033 | } | |
2034 | return -1 | |
2035 | } | |
2036 | ||
93f02886 DJ |
2037 | # gdb_download |
2038 | # | |
2039 | # Copy a file to the remote target and return its target filename. | |
2040 | # Schedule the file to be deleted at the end of this test. | |
2041 | ||
2042 | proc gdb_download { filename } { | |
2043 | global cleanfiles | |
2044 | ||
2045 | set destname [remote_download target $filename] | |
2046 | lappend cleanfiles $destname | |
2047 | return $destname | |
2048 | } | |
2049 | ||
2050 | # gdb_load_shlibs LIB... | |
2051 | # | |
2052 | # Copy the listed libraries to the target. | |
2053 | ||
2054 | proc gdb_load_shlibs { args } { | |
2055 | if {![is_remote target]} { | |
2056 | return | |
2057 | } | |
2058 | ||
2059 | foreach file $args { | |
2060 | gdb_download $file | |
2061 | } | |
2062 | ||
2063 | # Even if the target supplies full paths for shared libraries, | |
2064 | # they may not be paths for this system. | |
2065 | gdb_test "set solib-search-path [file dirname [lindex $args 0]]" "" "" | |
2066 | } | |
2067 | ||
c906108c SS |
2068 | # |
2069 | # gdb_load -- load a file into the debugger. | |
2db8e78e | 2070 | # Many files in config/*.exp override this procedure. |
c906108c SS |
2071 | # |
2072 | proc gdb_load { arg } { | |
2073 | return [gdb_file_cmd $arg] | |
2074 | } | |
2075 | ||
b741e217 DJ |
2076 | # gdb_reload -- load a file into the target. Called before "running", |
2077 | # either the first time or after already starting the program once, | |
2078 | # for remote targets. Most files that override gdb_load should now | |
2079 | # override this instead. | |
2080 | ||
2081 | proc gdb_reload { } { | |
2082 | # For the benefit of existing configurations, default to gdb_load. | |
2083 | # Specifying no file defaults to the executable currently being | |
2084 | # debugged. | |
2085 | return [gdb_load ""] | |
2086 | } | |
2087 | ||
c906108c SS |
2088 | proc gdb_continue { function } { |
2089 | global decimal | |
2090 | ||
2091 | return [gdb_test "continue" ".*Breakpoint $decimal, $function .*" "continue to $function"]; | |
2092 | } | |
2093 | ||
2094 | proc default_gdb_init { args } { | |
277254ba | 2095 | global gdb_wrapper_initialized |
93f02886 | 2096 | global cleanfiles |
277254ba | 2097 | |
93f02886 DJ |
2098 | set cleanfiles {} |
2099 | ||
c906108c SS |
2100 | gdb_clear_suppressed; |
2101 | ||
277254ba MS |
2102 | # Make sure that the wrapper is rebuilt |
2103 | # with the appropriate multilib option. | |
2104 | set gdb_wrapper_initialized 0 | |
2105 | ||
7b433602 JB |
2106 | # Unlike most tests, we have a small number of tests that generate |
2107 | # a very large amount of output. We therefore increase the expect | |
2108 | # buffer size to be able to contain the entire test output. | |
2109 | match_max -d 30000 | |
c906108c SS |
2110 | |
2111 | # We want to add the name of the TCL testcase to the PASS/FAIL messages. | |
2112 | if { [llength $args] > 0 } { | |
2113 | global pf_prefix | |
2114 | ||
2115 | set file [lindex $args 0]; | |
2116 | ||
2117 | set pf_prefix "[file tail [file dirname $file]]/[file tail $file]:"; | |
2118 | } | |
2119 | global gdb_prompt; | |
2120 | if [target_info exists gdb_prompt] { | |
2121 | set gdb_prompt [target_info gdb_prompt]; | |
2122 | } else { | |
2123 | set gdb_prompt "\\(gdb\\)" | |
2124 | } | |
2125 | } | |
2126 | ||
2127 | proc gdb_init { args } { | |
2128 | return [eval default_gdb_init $args]; | |
2129 | } | |
2130 | ||
2131 | proc gdb_finish { } { | |
93f02886 DJ |
2132 | global cleanfiles |
2133 | ||
2134 | # Exit first, so that the files are no longer in use. | |
2135 | gdb_exit | |
2136 | ||
2137 | if { [llength $cleanfiles] > 0 } { | |
2138 | eval remote_file target delete $cleanfiles | |
2139 | set cleanfiles {} | |
2140 | } | |
c906108c SS |
2141 | } |
2142 | ||
2143 | global debug_format | |
7a292a7a | 2144 | set debug_format "unknown" |
c906108c SS |
2145 | |
2146 | # Run the gdb command "info source" and extract the debugging format | |
2147 | # information from the output and save it in debug_format. | |
2148 | ||
2149 | proc get_debug_format { } { | |
2150 | global gdb_prompt | |
2151 | global verbose | |
2152 | global expect_out | |
2153 | global debug_format | |
2154 | ||
2155 | set debug_format "unknown" | |
2156 | send_gdb "info source\n" | |
2157 | gdb_expect 10 { | |
919d772c | 2158 | -re "Compiled with (.*) debugging format.\r\n.*$gdb_prompt $" { |
c906108c SS |
2159 | set debug_format $expect_out(1,string) |
2160 | verbose "debug format is $debug_format" | |
2161 | return 1; | |
2162 | } | |
2163 | -re "No current source file.\r\n$gdb_prompt $" { | |
2164 | perror "get_debug_format used when no current source file" | |
2165 | return 0; | |
2166 | } | |
2167 | -re "$gdb_prompt $" { | |
2168 | warning "couldn't check debug format (no valid response)." | |
2169 | return 1; | |
2170 | } | |
2171 | timeout { | |
2172 | warning "couldn't check debug format (timed out)." | |
2173 | return 1; | |
2174 | } | |
2175 | } | |
2176 | } | |
2177 | ||
838ae6c4 JB |
2178 | # Return true if FORMAT matches the debug format the current test was |
2179 | # compiled with. FORMAT is a shell-style globbing pattern; it can use | |
2180 | # `*', `[...]', and so on. | |
2181 | # | |
2182 | # This function depends on variables set by `get_debug_format', above. | |
2183 | ||
2184 | proc test_debug_format {format} { | |
2185 | global debug_format | |
2186 | ||
2187 | return [expr [string match $format $debug_format] != 0] | |
2188 | } | |
2189 | ||
c906108c SS |
2190 | # Like setup_xfail, but takes the name of a debug format (DWARF 1, |
2191 | # COFF, stabs, etc). If that format matches the format that the | |
2192 | # current test was compiled with, then the next test is expected to | |
2193 | # fail for any target. Returns 1 if the next test or set of tests is | |
2194 | # expected to fail, 0 otherwise (or if it is unknown). Must have | |
2195 | # previously called get_debug_format. | |
b55a4771 | 2196 | proc setup_xfail_format { format } { |
838ae6c4 | 2197 | set ret [test_debug_format $format]; |
b55a4771 | 2198 | |
838ae6c4 | 2199 | if {$ret} then { |
b55a4771 MS |
2200 | setup_xfail "*-*-*" |
2201 | } | |
2202 | return $ret; | |
2203 | } | |
c906108c SS |
2204 | |
2205 | proc gdb_step_for_stub { } { | |
2206 | global gdb_prompt; | |
2207 | ||
2208 | if ![target_info exists gdb,use_breakpoint_for_stub] { | |
2209 | if [target_info exists gdb_stub_step_command] { | |
2210 | set command [target_info gdb_stub_step_command]; | |
2211 | } else { | |
2212 | set command "step"; | |
2213 | } | |
2214 | send_gdb "${command}\n"; | |
2215 | set tries 0; | |
2216 | gdb_expect 60 { | |
2217 | -re "(main.* at |.*in .*start).*$gdb_prompt" { | |
2218 | return; | |
2219 | } | |
2220 | -re ".*$gdb_prompt" { | |
2221 | incr tries; | |
2222 | if { $tries == 5 } { | |
2223 | fail "stepping out of breakpoint function"; | |
2224 | return; | |
2225 | } | |
2226 | send_gdb "${command}\n"; | |
2227 | exp_continue; | |
2228 | } | |
2229 | default { | |
2230 | fail "stepping out of breakpoint function"; | |
2231 | return; | |
2232 | } | |
2233 | } | |
2234 | } | |
2235 | send_gdb "where\n"; | |
2236 | gdb_expect { | |
2237 | -re "main\[^\r\n\]*at \(\[^:]+\):\(\[0-9\]+\)" { | |
2238 | set file $expect_out(1,string); | |
2239 | set linenum [expr $expect_out(2,string) + 1]; | |
2240 | set breakplace "${file}:${linenum}"; | |
2241 | } | |
2242 | default {} | |
2243 | } | |
2244 | send_gdb "break ${breakplace}\n"; | |
2245 | gdb_expect 60 { | |
2246 | -re "Breakpoint (\[0-9\]+) at.*$gdb_prompt" { | |
2247 | set breakpoint $expect_out(1,string); | |
2248 | } | |
2249 | -re "Breakpoint (\[0-9\]+): file.*$gdb_prompt" { | |
2250 | set breakpoint $expect_out(1,string); | |
2251 | } | |
2252 | default {} | |
2253 | } | |
2254 | send_gdb "continue\n"; | |
2255 | gdb_expect 60 { | |
2256 | -re "Breakpoint ${breakpoint},.*$gdb_prompt" { | |
2257 | gdb_test "delete $breakpoint" ".*" ""; | |
2258 | return; | |
2259 | } | |
2260 | default {} | |
2261 | } | |
2262 | } | |
2263 | ||
c6fee705 MC |
2264 | # gdb_get_line_number TEXT [FILE] |
2265 | # | |
2266 | # Search the source file FILE, and return the line number of the | |
2267 | # first line containing TEXT. If no match is found, return -1. | |
2268 | # | |
2269 | # TEXT is a string literal, not a regular expression. | |
2270 | # | |
2271 | # The default value of FILE is "$srcdir/$subdir/$srcfile". If FILE is | |
2272 | # specified, and does not start with "/", then it is assumed to be in | |
2273 | # "$srcdir/$subdir". This is awkward, and can be fixed in the future, | |
2274 | # by changing the callers and the interface at the same time. | |
2275 | # In particular: gdb.base/break.exp, gdb.base/condbreak.exp, | |
2276 | # gdb.base/ena-dis-br.exp. | |
2277 | # | |
2278 | # Use this function to keep your test scripts independent of the | |
2279 | # exact line numbering of the source file. Don't write: | |
2280 | # | |
2281 | # send_gdb "break 20" | |
2282 | # | |
2283 | # This means that if anyone ever edits your test's source file, | |
2284 | # your test could break. Instead, put a comment like this on the | |
2285 | # source file line you want to break at: | |
2286 | # | |
2287 | # /* breakpoint spot: frotz.exp: test name */ | |
2288 | # | |
2289 | # and then write, in your test script (which we assume is named | |
2290 | # frotz.exp): | |
2291 | # | |
2292 | # send_gdb "break [gdb_get_line_number "frotz.exp: test name"]\n" | |
2293 | # | |
2294 | # (Yes, Tcl knows how to handle the nested quotes and brackets. | |
2295 | # Try this: | |
2296 | # $ tclsh | |
2297 | # % puts "foo [lindex "bar baz" 1]" | |
2298 | # foo baz | |
2299 | # % | |
2300 | # Tcl is quite clever, for a little stringy language.) | |
2301 | # | |
2302 | # === | |
2303 | # | |
2304 | # The previous implementation of this procedure used the gdb search command. | |
2305 | # This version is different: | |
2306 | # | |
2307 | # . It works with MI, and it also works when gdb is not running. | |
2308 | # | |
2309 | # . It operates on the build machine, not the host machine. | |
2310 | # | |
2311 | # . For now, this implementation fakes a current directory of | |
2312 | # $srcdir/$subdir to be compatible with the old implementation. | |
2313 | # This will go away eventually and some callers will need to | |
2314 | # be changed. | |
2315 | # | |
2316 | # . The TEXT argument is literal text and matches literally, | |
2317 | # not a regular expression as it was before. | |
2318 | # | |
2319 | # . State changes in gdb, such as changing the current file | |
2320 | # and setting $_, no longer happen. | |
2321 | # | |
2322 | # After a bit of time we can forget about the differences from the | |
2323 | # old implementation. | |
2324 | # | |
2325 | # --chastain 2004-08-05 | |
2326 | ||
2327 | proc gdb_get_line_number { text { file "" } } { | |
2328 | global srcdir | |
2329 | global subdir | |
2330 | global srcfile | |
c906108c | 2331 | |
c6fee705 MC |
2332 | if { "$file" == "" } then { |
2333 | set file "$srcfile" | |
2334 | } | |
2335 | if { ! [regexp "^/" "$file"] } then { | |
2336 | set file "$srcdir/$subdir/$file" | |
c906108c SS |
2337 | } |
2338 | ||
c6fee705 MC |
2339 | if { [ catch { set fd [open "$file"] } message ] } then { |
2340 | perror "$message" | |
2341 | return -1 | |
c906108c | 2342 | } |
c6fee705 MC |
2343 | |
2344 | set found -1 | |
2345 | for { set line 1 } { 1 } { incr line } { | |
2346 | if { [ catch { set nchar [gets "$fd" body] } message ] } then { | |
2347 | perror "$message" | |
2348 | return -1 | |
2349 | } | |
2350 | if { $nchar < 0 } then { | |
2351 | break | |
2352 | } | |
2353 | if { [string first "$text" "$body"] >= 0 } then { | |
2354 | set found $line | |
2355 | break | |
2356 | } | |
2357 | } | |
2358 | ||
2359 | if { [ catch { close "$fd" } message ] } then { | |
2360 | perror "$message" | |
2361 | return -1 | |
2362 | } | |
2363 | ||
2364 | return $found | |
c906108c SS |
2365 | } |
2366 | ||
7a292a7a SS |
2367 | # gdb_continue_to_end: |
2368 | # The case where the target uses stubs has to be handled specially. If a | |
2369 | # stub is used, we set a breakpoint at exit because we cannot rely on | |
2370 | # exit() behavior of a remote target. | |
2371 | # | |
2372 | # mssg is the error message that gets printed. | |
2373 | ||
2374 | proc gdb_continue_to_end {mssg} { | |
2375 | if [target_info exists use_gdb_stub] { | |
2376 | if {![gdb_breakpoint "exit"]} { | |
2377 | return 0 | |
2378 | } | |
2379 | gdb_test "continue" "Continuing..*Breakpoint .*exit.*" \ | |
2380 | "continue until exit at $mssg" | |
2381 | } else { | |
2382 | # Continue until we exit. Should not stop again. | |
2383 | # Don't bother to check the output of the program, that may be | |
2384 | # extremely tough for some remote systems. | |
2385 | gdb_test "continue"\ | |
1c56143a | 2386 | "Continuing.\[\r\n0-9\]+(... EXIT code 0\[\r\n\]+|Program exited normally\\.).*"\ |
7a292a7a SS |
2387 | "continue until exit at $mssg" |
2388 | } | |
2389 | } | |
2390 | ||
2391 | proc rerun_to_main {} { | |
2392 | global gdb_prompt | |
2393 | ||
2394 | if [target_info exists use_gdb_stub] { | |
2395 | gdb_run_cmd | |
2396 | gdb_expect { | |
2397 | -re ".*Breakpoint .*main .*$gdb_prompt $"\ | |
2398 | {pass "rerun to main" ; return 0} | |
2399 | -re "$gdb_prompt $"\ | |
2400 | {fail "rerun to main" ; return 0} | |
2401 | timeout {fail "(timeout) rerun to main" ; return 0} | |
2402 | } | |
2403 | } else { | |
2404 | send_gdb "run\n" | |
2405 | gdb_expect { | |
11350d2a CV |
2406 | -re "The program .* has been started already.*y or n. $" { |
2407 | send_gdb "y\n" | |
2408 | exp_continue | |
2409 | } | |
7a292a7a SS |
2410 | -re "Starting program.*$gdb_prompt $"\ |
2411 | {pass "rerun to main" ; return 0} | |
2412 | -re "$gdb_prompt $"\ | |
2413 | {fail "rerun to main" ; return 0} | |
2414 | timeout {fail "(timeout) rerun to main" ; return 0} | |
2415 | } | |
2416 | } | |
2417 | } | |
c906108c | 2418 | |
13a5e3b8 MS |
2419 | # Print a message and return true if a test should be skipped |
2420 | # due to lack of floating point suport. | |
2421 | ||
2422 | proc gdb_skip_float_test { msg } { | |
2423 | if [target_info exists gdb,skip_float_tests] { | |
2424 | verbose "Skipping test '$msg': no float tests."; | |
2425 | return 1; | |
2426 | } | |
2427 | return 0; | |
2428 | } | |
2429 | ||
2430 | # Print a message and return true if a test should be skipped | |
2431 | # due to lack of stdio support. | |
2432 | ||
2433 | proc gdb_skip_stdio_test { msg } { | |
2434 | if [target_info exists gdb,noinferiorio] { | |
2435 | verbose "Skipping test '$msg': no inferior i/o."; | |
2436 | return 1; | |
2437 | } | |
2438 | return 0; | |
2439 | } | |
2440 | ||
2441 | proc gdb_skip_bogus_test { msg } { | |
2442 | return 0; | |
2443 | } | |
2444 | ||
e515b470 DJ |
2445 | # Return true if a test should be skipped due to lack of XML support |
2446 | # in the host GDB. | |
2447 | ||
2448 | proc gdb_skip_xml_test { } { | |
2449 | global gdb_prompt | |
2450 | global srcdir | |
2451 | global xml_missing_cached | |
2452 | ||
2453 | if {[info exists xml_missing_cached]} { | |
2454 | return $xml_missing_cached | |
2455 | } | |
2456 | ||
2457 | gdb_start | |
2458 | set xml_missing_cached 0 | |
2459 | gdb_test_multiple "set tdesc filename ${srcdir}/gdb.xml/trivial.xml" "" { | |
2460 | -re ".*XML support was disabled at compile time.*$gdb_prompt $" { | |
2461 | set xml_missing_cached 1 | |
2462 | } | |
2463 | -re ".*$gdb_prompt $" { } | |
2464 | } | |
2465 | gdb_exit | |
2466 | return $xml_missing_cached | |
2467 | } | |
1f8a6abb EZ |
2468 | |
2469 | # Note: the procedure gdb_gnu_strip_debug will produce an executable called | |
2470 | # ${binfile}.dbglnk, which is just like the executable ($binfile) but without | |
2471 | # the debuginfo. Instead $binfile has a .gnu_debuglink section which contains | |
94277a38 | 2472 | # the name of a debuginfo only file. This file will be stored in the |
1f8a6abb EZ |
2473 | # gdb.base/.debug subdirectory. |
2474 | ||
2475 | # Functions for separate debug info testing | |
2476 | ||
2477 | # starting with an executable: | |
2478 | # foo --> original executable | |
2479 | ||
2480 | # at the end of the process we have: | |
2481 | # foo.stripped --> foo w/o debug info | |
2482 | # .debug/foo.debug --> foo's debug info | |
2483 | # foo --> like foo, but with a new .gnu_debuglink section pointing to foo.debug. | |
2484 | ||
2485 | # Return the name of the file in which we should stor EXEC's separated | |
2486 | # debug info. EXEC contains the full path. | |
2487 | proc separate_debug_filename { exec } { | |
2488 | ||
2489 | # In a .debug subdirectory off the same directory where the testcase | |
2490 | # executable is going to be. Something like: | |
2491 | # <your-path>/gdb/testsuite/gdb.base/.debug/blah.debug. | |
2492 | # This is the default location where gdb expects to findi | |
2493 | # the debug info file. | |
2494 | ||
2495 | set exec_dir [file dirname $exec] | |
2496 | set exec_file [file tail $exec] | |
2497 | set debug_dir [file join $exec_dir ".debug"] | |
2498 | set debug_file [file join $debug_dir "${exec_file}.debug"] | |
2499 | ||
2500 | return $debug_file | |
2501 | } | |
2502 | ||
4935890f JK |
2503 | # Return the build-id hex string (usually 160 bits as 40 hex characters) |
2504 | # converted to the form: .build-id/ab/cdef1234...89.debug | |
2505 | # Return "" if no build-id found. | |
2506 | proc build_id_debug_filename_get { exec } { | |
2507 | set tmp "${exec}-tmp" | |
8b3fc8d8 MK |
2508 | set objcopy_program [transform objcopy] |
2509 | ||
2510 | set result [catch "exec $objcopy_program -j .note.gnu.build-id -O binary $exec $tmp" output] | |
2511 | verbose "result is $result" | |
2512 | verbose "output is $output" | |
2513 | if {$result == 1} { | |
2514 | return "" | |
2515 | } | |
4935890f | 2516 | set fi [open $tmp] |
b7fca990 | 2517 | fconfigure $fi -translation binary |
4935890f JK |
2518 | # Skip the NOTE header. |
2519 | read $fi 16 | |
2520 | set data [read $fi] | |
2521 | close $fi | |
2522 | file delete $tmp | |
7020f05c | 2523 | if ![string compare $data ""] then { |
4935890f JK |
2524 | return "" |
2525 | } | |
2526 | # Convert it to hex. | |
2527 | binary scan $data H* data | |
2528 | set data [regsub {^..} $data {\0/}] | |
2529 | return ".build-id/${data}.debug"; | |
2530 | } | |
2531 | ||
94277a38 DJ |
2532 | # Create stripped files for DEST, replacing it. If ARGS is passed, it is a |
2533 | # list of optional flags. The only currently supported flag is no-main, | |
2534 | # which removes the symbol entry for main from the separate debug file. | |
1f8a6abb | 2535 | |
94277a38 DJ |
2536 | proc gdb_gnu_strip_debug { dest args } { |
2537 | ||
1f8a6abb | 2538 | set debug_file [separate_debug_filename $dest] |
b741e217 DJ |
2539 | set strip_to_file_program [transform strip] |
2540 | set objcopy_program [transform objcopy] | |
1f8a6abb EZ |
2541 | |
2542 | # Make sure the directory that will hold the separated debug | |
2543 | # info actually exists. | |
2544 | set debug_dir [file dirname $debug_file] | |
2545 | if {! [file isdirectory $debug_dir]} { | |
2546 | file mkdir $debug_dir | |
2547 | } | |
2548 | ||
2549 | set debug_link [file tail $debug_file] | |
2550 | set stripped_file "${dest}.stripped" | |
2551 | ||
2552 | # Get rid of the debug info, and store result in stripped_file | |
2553 | # something like gdb/testsuite/gdb.base/blah.stripped. | |
2554 | set result [catch "exec $strip_to_file_program --strip-debug ${dest} -o ${stripped_file}" output] | |
2555 | verbose "result is $result" | |
2556 | verbose "output is $output" | |
2557 | if {$result == 1} { | |
2558 | return 1 | |
2559 | } | |
2560 | ||
2561 | # Get rid of everything but the debug info, and store result in debug_file | |
2562 | # This will be in the .debug subdirectory, see above. | |
2563 | set result [catch "exec $strip_to_file_program --only-keep-debug ${dest} -o ${debug_file}" output] | |
2564 | verbose "result is $result" | |
2565 | verbose "output is $output" | |
2566 | if {$result == 1} { | |
2567 | return 1 | |
2568 | } | |
2569 | ||
94277a38 DJ |
2570 | # If no-main is passed, strip the symbol for main from the separate |
2571 | # file. This is to simulate the behavior of elfutils's eu-strip, which | |
2572 | # leaves the symtab in the original file only. There's no way to get | |
2573 | # objcopy or strip to remove the symbol table without also removing the | |
2574 | # debugging sections, so this is as close as we can get. | |
2575 | if { [llength $args] == 1 && [lindex $args 0] == "no-main" } { | |
2576 | set result [catch "exec $objcopy_program -N main ${debug_file} ${debug_file}-tmp" output] | |
2577 | verbose "result is $result" | |
2578 | verbose "output is $output" | |
2579 | if {$result == 1} { | |
2580 | return 1 | |
2581 | } | |
2582 | file delete "${debug_file}" | |
2583 | file rename "${debug_file}-tmp" "${debug_file}" | |
2584 | } | |
2585 | ||
1f8a6abb EZ |
2586 | # Link the two previous output files together, adding the .gnu_debuglink |
2587 | # section to the stripped_file, containing a pointer to the debug_file, | |
2588 | # save the new file in dest. | |
2589 | # This will be the regular executable filename, in the usual location. | |
2590 | set result [catch "exec $objcopy_program --add-gnu-debuglink=${debug_file} ${stripped_file} ${dest}" output] | |
2591 | verbose "result is $result" | |
2592 | verbose "output is $output" | |
2593 | if {$result == 1} { | |
2594 | return 1 | |
2595 | } | |
2596 | ||
2597 | return 0 | |
2598 | } | |
2599 | ||
d8295fe9 VP |
2600 | # Test the output of GDB_COMMAND matches the pattern obtained |
2601 | # by concatenating all elements of EXPECTED_LINES. This makes | |
2602 | # it possible to split otherwise very long string into pieces. | |
2603 | # If third argument is not empty, it's used as the name of the | |
2604 | # test to be printed on pass/fail. | |
2605 | proc help_test_raw { gdb_command expected_lines args } { | |
2606 | set message $gdb_command | |
2607 | if [llength $args]>0 then { | |
2608 | set message [lindex $args 0] | |
2609 | } | |
2610 | set expected_output [join $expected_lines ""] | |
2611 | gdb_test "${gdb_command}" "${expected_output}" $message | |
2612 | } | |
2613 | ||
2614 | # Test the output of "help COMMNAD_CLASS". EXPECTED_INITIAL_LINES | |
2615 | # are regular expressions that should match the beginning of output, | |
2616 | # before the list of commands in that class. The presence of | |
2617 | # command list and standard epilogue will be tested automatically. | |
2618 | proc test_class_help { command_class expected_initial_lines args } { | |
2619 | set l_stock_body { | |
2620 | "List of commands\:.*\[\r\n\]+" | |
2621 | "Type \"help\" followed by command name for full documentation\.\[\r\n\]+" | |
2622 | "Type \"apropos word\" to search for commands related to \"word\"\.[\r\n\]+" | |
2623 | "Command name abbreviations are allowed if unambiguous\." | |
2624 | } | |
2625 | set l_entire_body [concat $expected_initial_lines $l_stock_body] | |
2626 | ||
2627 | eval [list help_test_raw "help ${command_class}" $l_entire_body] $args | |
2628 | } | |
2629 | ||
2630 | # COMMAND_LIST should have either one element -- command to test, or | |
2631 | # two elements -- abbreviated command to test, and full command the first | |
2632 | # element is abbreviation of. | |
2633 | # The command must be a prefix command. EXPECTED_INITIAL_LINES | |
2634 | # are regular expressions that should match the beginning of output, | |
2635 | # before the list of subcommands. The presence of | |
2636 | # subcommand list and standard epilogue will be tested automatically. | |
2637 | proc test_prefix_command_help { command_list expected_initial_lines args } { | |
2638 | set command [lindex $command_list 0] | |
2639 | if {[llength $command_list]>1} { | |
2640 | set full_command [lindex $command_list 1] | |
2641 | } else { | |
2642 | set full_command $command | |
2643 | } | |
2644 | # Use 'list' and not just {} because we want variables to | |
2645 | # be expanded in this list. | |
2646 | set l_stock_body [list\ | |
2647 | "List of $full_command subcommands\:.*\[\r\n\]+"\ | |
2648 | "Type \"help $full_command\" followed by $full_command subcommand name for full documentation\.\[\r\n\]+"\ | |
2649 | "Type \"apropos word\" to search for commands related to \"word\"\.\[\r\n\]+"\ | |
2650 | "Command name abbreviations are allowed if unambiguous\."] | |
2651 | set l_entire_body [concat $expected_initial_lines $l_stock_body] | |
2652 | if {[llength $args]>0} { | |
2653 | help_test_raw "help ${command}" $l_entire_body [lindex $args 0] | |
2654 | } else { | |
2655 | help_test_raw "help ${command}" $l_entire_body | |
2656 | } | |
2657 | } | |
dbc52822 VP |
2658 | |
2659 | # Build executable named EXECUTABLE, from SOURCES. If SOURCES are not | |
2660 | # provided, uses $EXECUTABLE.c. The TESTNAME paramer is the name of test | |
2661 | # to pass to untested, if something is wrong. OPTIONS are passed | |
2662 | # to gdb_compile directly. | |
2663 | proc build_executable { testname executable {sources ""} {options {debug}} } { | |
2664 | ||
2665 | global objdir | |
2666 | global subdir | |
2667 | global srcdir | |
2668 | if {[llength $sources]==0} { | |
2669 | set sources ${executable}.c | |
2670 | } | |
2671 | ||
2672 | set binfile ${objdir}/${subdir}/${executable} | |
2673 | ||
2674 | set objects {} | |
2675 | for {set i 0} "\$i<[llength $sources]" {incr i} { | |
2676 | set s [lindex $sources $i] | |
2677 | if { [gdb_compile "${srcdir}/${subdir}/${s}" "${binfile}${i}.o" object $options] != "" } { | |
2678 | untested $testname | |
2679 | return -1 | |
2680 | } | |
2681 | lappend objects "${binfile}${i}.o" | |
2682 | } | |
2683 | ||
2684 | if { [gdb_compile $objects "${binfile}" executable $options] != "" } { | |
2685 | untested $testname | |
2686 | return -1 | |
2687 | } | |
2688 | ||
2689 | if [get_compiler_info ${binfile}] { | |
2690 | return -1 | |
2691 | } | |
2692 | return 0 | |
2693 | } | |
2694 | ||
2695 | # Starts fresh GDB binary and loads EXECUTABLE into GDB. EXECUTABLE is | |
2696 | # the name of binary in ${objdir}/${subdir}. | |
2697 | proc clean_restart { executable } { | |
2698 | global srcdir | |
2699 | global objdir | |
2700 | global subdir | |
2701 | set binfile ${objdir}/${subdir}/${executable} | |
2702 | ||
2703 | gdb_exit | |
2704 | gdb_start | |
2705 | gdb_reinitialize_dir $srcdir/$subdir | |
2706 | gdb_load ${binfile} | |
2707 | ||
2708 | if [target_info exists gdb_stub] { | |
2709 | gdb_step_for_stub; | |
2710 | } | |
2711 | } | |
2712 | ||
2713 | # Prepares for testing, by calling build_executable, and then clean_restart. | |
2714 | # Please refer to build_executable for parameter description. | |
2715 | proc prepare_for_testing { testname executable {sources ""} {options {debug}}} { | |
2716 | ||
2717 | if {[build_executable $testname $executable $sources] == -1} { | |
2718 | return -1 | |
2719 | } | |
2720 | clean_restart $executable | |
2721 | ||
2722 | return 0 | |
2723 | } |