7822e665a2bf1f750192bddba60c3ac0252974fa
[binutils-gdb.git] / gdb / testsuite / lib / mi-support.exp
1 # Copyright 1999, 2000, 2002, 2003, 2004, 2005, 2007, 2008, 2009
2 # Free Software Foundation, Inc.
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
6 # the Free Software Foundation; either version 3 of the License, or
7 # (at your option) any later version.
8 #
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.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
16
17 # This file was based on a file written by Fred Fish. (fnf@cygnus.com)
18
19 # Test setup routines that work with the MI interpreter.
20
21 # The variable mi_gdb_prompt is a regexp which matches the gdb mi prompt.
22 # Set it if it is not already set.
23 global mi_gdb_prompt
24 if ![info exists mi_gdb_prompt] then {
25 set mi_gdb_prompt "\[(\]gdb\[)\] \r\n"
26 }
27
28 global mi_inferior_spawn_id
29 global mi_inferior_tty_name
30
31 set MIFLAGS "-i=mi"
32
33 set thread_selected_re "=thread-selected,id=\"\[0-9+\]\"\r\n"
34
35 #
36 # mi_gdb_exit -- exit the GDB, killing the target program if necessary
37 #
38 proc mi_gdb_exit {} {
39 catch mi_uncatched_gdb_exit
40 }
41
42 proc mi_uncatched_gdb_exit {} {
43 global GDB
44 global INTERNAL_GDBFLAGS GDBFLAGS
45 global verbose
46 global gdb_spawn_id;
47 global gdb_prompt
48 global mi_gdb_prompt
49 global MIFLAGS
50
51 gdb_stop_suppressing_tests;
52
53 if { [info procs sid_exit] != "" } {
54 sid_exit
55 }
56
57 if ![info exists gdb_spawn_id] {
58 return;
59 }
60
61 verbose "Quitting $GDB $INTERNAL_GDBFLAGS $GDBFLAGS $MIFLAGS"
62
63 if { [is_remote host] && [board_info host exists fileid] } {
64 send_gdb "999-gdb-exit\n";
65 gdb_expect 10 {
66 -re "y or n" {
67 send_gdb "y\n";
68 exp_continue;
69 }
70 -re "Undefined command.*$gdb_prompt $" {
71 send_gdb "quit\n"
72 exp_continue;
73 }
74 -re "DOSEXIT code" { }
75 default { }
76 }
77 }
78
79 if ![is_remote host] {
80 remote_close host;
81 }
82 unset gdb_spawn_id
83 }
84
85 #
86 # default_mi_gdb_start [INFERIOR_PTY] -- start gdb running, default procedure
87 #
88 # INFERIOR_PTY should be set to separate-inferior-tty to have the inferior work
89 # with it's own PTY. If set to same-inferior-tty, the inferior shares GDB's PTY.
90 # The default value is same-inferior-tty.
91 #
92 # When running over NFS, particularly if running many simultaneous
93 # tests on different hosts all using the same server, things can
94 # get really slow. Give gdb at least 3 minutes to start up.
95 #
96 proc default_mi_gdb_start { args } {
97 global verbose
98 global GDB
99 global INTERNAL_GDBFLAGS GDBFLAGS
100 global gdb_prompt
101 global mi_gdb_prompt
102 global timeout
103 global gdb_spawn_id;
104 global MIFLAGS
105
106 gdb_stop_suppressing_tests;
107 set inferior_pty no-tty
108
109 if { [llength $args] == 1} {
110 set inferior_pty [lindex $args 0]
111 }
112
113 set separate_inferior_pty [string match $inferior_pty separate-inferior-tty]
114
115 # Start SID.
116 if { [info procs sid_start] != "" } {
117 verbose "Spawning SID"
118 sid_start
119 }
120
121 verbose "Spawning $GDB $INTERNAL_GDBFLAGS $GDBFLAGS $MIFLAGS"
122
123 if [info exists gdb_spawn_id] {
124 return 0;
125 }
126
127 if ![is_remote host] {
128 if { [which $GDB] == 0 } then {
129 perror "$GDB does not exist."
130 exit 1
131 }
132 }
133
134 # Create the new PTY for the inferior process.
135 if { $separate_inferior_pty } {
136 spawn -pty
137 global mi_inferior_spawn_id
138 global mi_inferior_tty_name
139 set mi_inferior_spawn_id $spawn_id
140 set mi_inferior_tty_name $spawn_out(slave,name)
141 }
142
143 set res [remote_spawn host "$GDB $INTERNAL_GDBFLAGS $GDBFLAGS $MIFLAGS [host_info gdb_opts]"];
144 if { $res < 0 || $res == "" } {
145 perror "Spawning $GDB failed."
146 return 1;
147 }
148 gdb_expect {
149 -re "~\"GNU.*\r\n~\".*$mi_gdb_prompt$" {
150 # We have a new format mi startup prompt. If we are
151 # running mi1, then this is an error as we should be
152 # using the old-style prompt.
153 if { $MIFLAGS == "-i=mi1" } {
154 perror "(mi startup) Got unexpected new mi prompt."
155 remote_close host;
156 return -1;
157 }
158 verbose "GDB initialized."
159 }
160 -re "\[^~\].*$mi_gdb_prompt$" {
161 # We have an old format mi startup prompt. If we are
162 # not running mi1, then this is an error as we should be
163 # using the new-style prompt.
164 if { $MIFLAGS != "-i=mi1" } {
165 perror "(mi startup) Got unexpected old mi prompt."
166 remote_close host;
167 return -1;
168 }
169 verbose "GDB initialized."
170 }
171 -re ".*unrecognized option.*for a complete list of options." {
172 untested "Skip mi tests (not compiled with mi support)."
173 remote_close host;
174 return -1;
175 }
176 -re ".*Interpreter `mi' unrecognized." {
177 untested "Skip mi tests (not compiled with mi support)."
178 remote_close host;
179 return -1;
180 }
181 timeout {
182 perror "(timeout) GDB never initialized after 10 seconds."
183 remote_close host;
184 return -1
185 }
186 }
187 set gdb_spawn_id -1;
188
189 # FIXME: mi output does not go through pagers, so these can be removed.
190 # force the height to "unlimited", so no pagers get used
191 send_gdb "100-gdb-set height 0\n"
192 gdb_expect 10 {
193 -re ".*100-gdb-set height 0\r\n100\\\^done\r\n$mi_gdb_prompt$" {
194 verbose "Setting height to 0." 2
195 }
196 timeout {
197 warning "Couldn't set the height to 0"
198 }
199 }
200 # force the width to "unlimited", so no wraparound occurs
201 send_gdb "101-gdb-set width 0\n"
202 gdb_expect 10 {
203 -re ".*101-gdb-set width 0\r\n101\\\^done\r\n$mi_gdb_prompt$" {
204 verbose "Setting width to 0." 2
205 }
206 timeout {
207 warning "Couldn't set the width to 0."
208 }
209 }
210 # If allowing the inferior to have its own PTY then assign the inferior
211 # its own terminal device here.
212 if { $separate_inferior_pty } {
213 send_gdb "102-inferior-tty-set $mi_inferior_tty_name\n"
214 gdb_expect 10 {
215 -re ".*102\\\^done\r\n$mi_gdb_prompt$" {
216 verbose "redirect inferior output to new terminal device."
217 }
218 timeout {
219 warning "Couldn't redirect inferior output." 2
220 }
221 }
222 }
223
224 detect_async
225
226 return 0;
227 }
228
229 #
230 # Overridable function. You can override this function in your
231 # baseboard file.
232 #
233 proc mi_gdb_start { args } {
234 return [default_mi_gdb_start $args]
235 }
236
237 # Many of the tests depend on setting breakpoints at various places and
238 # running until that breakpoint is reached. At times, we want to start
239 # with a clean-slate with respect to breakpoints, so this utility proc
240 # lets us do this without duplicating this code everywhere.
241 #
242
243 proc mi_delete_breakpoints {} {
244 global mi_gdb_prompt
245
246 # FIXME: The mi operation won't accept a prompt back and will use the 'all' arg
247 send_gdb "102-break-delete\n"
248 gdb_expect 30 {
249 -re "Delete all breakpoints.*y or n.*$" {
250 send_gdb "y\n";
251 exp_continue
252 }
253 -re "102-break-delete\r\n102\\\^done\r\n$mi_gdb_prompt$" {
254 # This happens if there were no breakpoints
255 }
256 timeout { perror "Delete all breakpoints in mi_delete_breakpoints (timeout)" ; return }
257 }
258
259 # The correct output is not "No breakpoints or watchpoints." but an
260 # empty BreakpointTable. Also, a query is not acceptable with mi.
261 send_gdb "103-break-list\n"
262 gdb_expect 30 {
263 -re "103-break-list\r\n103\\\^done,BreakpointTable=\{\}\r\n$mi_gdb_prompt$" {}
264 -re "103-break-list\r\n103\\\^done,BreakpointTable=\{nr_rows=\".\",nr_cols=\".\",hdr=\\\[\{width=\".*\",alignment=\".*\",col_name=\"number\",colhdr=\"Num\"\}.*colhdr=\"Type\".*colhdr=\"Disp\".*colhdr=\"Enb\".*colhdr=\"Address\".*colhdr=\"What\".*\\\],body=\\\[\\\]\}\r\n$mi_gdb_prompt$" {}
265 -re "103-break-list\r\n103\\\^doneNo breakpoints or watchpoints.\r\n\r\n$mi_gdb_prompt$" {warning "Unexpected console text received"}
266 -re "$mi_gdb_prompt$" { perror "Breakpoints not deleted" ; return }
267 -re "Delete all breakpoints.*or n.*$" {
268 warning "Unexpected prompt for breakpoints deletion";
269 send_gdb "y\n";
270 exp_continue
271 }
272 timeout { perror "-break-list (timeout)" ; return }
273 }
274 }
275
276 proc mi_gdb_reinitialize_dir { subdir } {
277 global mi_gdb_prompt
278 global MIFLAGS
279
280 global suppress_flag
281 if { $suppress_flag } {
282 return
283 }
284
285 if [is_remote host] {
286 return "";
287 }
288
289 if { $MIFLAGS == "-i=mi1" } {
290 send_gdb "104-environment-directory\n"
291 gdb_expect 60 {
292 -re ".*Reinitialize source path to empty.*y or n. " {
293 warning "Got confirmation prompt for dir reinitialization."
294 send_gdb "y\n"
295 gdb_expect 60 {
296 -re "$mi_gdb_prompt$" {}
297 timeout {error "Dir reinitialization failed (timeout)"}
298 }
299 }
300 -re "$mi_gdb_prompt$" {}
301 timeout {error "Dir reinitialization failed (timeout)"}
302 }
303 } else {
304 send_gdb "104-environment-directory -r\n"
305 gdb_expect 60 {
306 -re "104\\\^done,source-path=.*\r\n$mi_gdb_prompt$" {}
307 -re "$mi_gdb_prompt$" {}
308 timeout {error "Dir reinitialization failed (timeout)"}
309 }
310 }
311
312 send_gdb "105-environment-directory $subdir\n"
313 gdb_expect 60 {
314 -re "Source directories searched.*$mi_gdb_prompt$" {
315 verbose "Dir set to $subdir"
316 }
317 -re "105\\\^done.*\r\n$mi_gdb_prompt$" {
318 # FIXME: We return just the prompt for now.
319 verbose "Dir set to $subdir"
320 # perror "Dir \"$subdir\" failed."
321 }
322 }
323 }
324
325 # Send GDB the "target" command.
326 # FIXME: Some of these patterns are not appropriate for MI. Based on
327 # config/monitor.exp:gdb_target_command.
328 proc mi_gdb_target_cmd { targetname serialport } {
329 global mi_gdb_prompt
330
331 set serialport_re [string_to_regexp $serialport]
332 for {set i 1} {$i <= 3} {incr i} {
333 send_gdb "47-target-select $targetname $serialport\n"
334 gdb_expect 60 {
335 -re "47\\^connected.*$mi_gdb_prompt$" {
336 verbose "Set target to $targetname";
337 return 0;
338 }
339 -re "unknown host.*$mi_gdb_prompt" {
340 verbose "Couldn't look up $serialport"
341 }
342 -re "Couldn't establish connection to remote.*$mi_gdb_prompt$" {
343 verbose "Connection failed";
344 }
345 -re "Remote MIPS debugging.*$mi_gdb_prompt$" {
346 verbose "Set target to $targetname";
347 return 0;
348 }
349 -re "Remote debugging using .*$serialport_re.*$mi_gdb_prompt$" {
350 verbose "Set target to $targetname";
351 return 0;
352 }
353 -re "Remote target $targetname connected to.*$mi_gdb_prompt$" {
354 verbose "Set target to $targetname";
355 return 0;
356 }
357 -re "Connected to.*$mi_gdb_prompt$" {
358 verbose "Set target to $targetname";
359 return 0;
360 }
361 -re "Ending remote.*$mi_gdb_prompt$" { }
362 -re "Connection refused.*$mi_gdb_prompt$" {
363 verbose "Connection refused by remote target. Pausing, and trying again."
364 sleep 5
365 continue
366 }
367 -re "Timeout reading from remote system.*$mi_gdb_prompt$" {
368 verbose "Got timeout error from gdb.";
369 }
370 timeout {
371 send_gdb "\ 3";
372 break
373 }
374 }
375 }
376 return 1
377 }
378
379 #
380 # load a file into the debugger (file command only).
381 # return a -1 if anything goes wrong.
382 #
383 proc mi_gdb_file_cmd { arg } {
384 global verbose
385 global loadpath
386 global loadfile
387 global GDB
388 global mi_gdb_prompt
389 global last_loaded_file
390 upvar timeout timeout
391
392 set last_loaded_file $arg
393
394 if [is_remote host] {
395 set arg [remote_download host $arg];
396 if { $arg == "" } {
397 error "download failed"
398 return -1;
399 }
400 }
401
402 # FIXME: Several of these patterns are only acceptable for console
403 # output. Queries are an error for mi.
404 send_gdb "105-file-exec-and-symbols $arg\n"
405 gdb_expect 120 {
406 -re "Reading symbols from.*done.*$mi_gdb_prompt$" {
407 verbose "\t\tLoaded $arg into the $GDB"
408 return 0
409 }
410 -re "has no symbol-table.*$mi_gdb_prompt$" {
411 perror "$arg wasn't compiled with \"-g\""
412 return -1
413 }
414 -re "Load new symbol table from \".*\".*y or n. $" {
415 send_gdb "y\n"
416 gdb_expect 120 {
417 -re "Reading symbols from.*done.*$mi_gdb_prompt$" {
418 verbose "\t\tLoaded $arg with new symbol table into $GDB"
419 # All OK
420 }
421 timeout {
422 perror "(timeout) Couldn't load $arg, other program already loaded."
423 return -1
424 }
425 }
426 }
427 -re "No such file or directory.*$mi_gdb_prompt$" {
428 perror "($arg) No such file or directory\n"
429 return -1
430 }
431 -re "105-file-exec-and-symbols .*\r\n105\\\^done\r\n$mi_gdb_prompt$" {
432 # We (MI) are just giving the prompt back for now, instead of giving
433 # some acknowledgement.
434 return 0
435 }
436 timeout {
437 perror "couldn't load $arg into $GDB (timed out)."
438 return -1
439 }
440 eof {
441 # This is an attempt to detect a core dump, but seems not to
442 # work. Perhaps we need to match .* followed by eof, in which
443 # gdb_expect does not seem to have a way to do that.
444 perror "couldn't load $arg into $GDB (end of file)."
445 return -1
446 }
447 }
448 }
449
450 #
451 # connect to the target and download a file, if necessary.
452 # return a -1 if anything goes wrong.
453 #
454 proc mi_gdb_target_load { } {
455 global verbose
456 global loadpath
457 global loadfile
458 global GDB
459 global mi_gdb_prompt
460 upvar timeout timeout
461
462 if { [info procs gdbserver_gdb_load] != "" } {
463 mi_gdb_test "kill" ".*" ""
464 set res [gdbserver_gdb_load]
465 set protocol [lindex $res 0]
466 set gdbport [lindex $res 1]
467
468 if { [mi_gdb_target_cmd $protocol $gdbport] != 0 } {
469 return -1
470 }
471 } elseif { [info procs send_target_sid] != "" } {
472 # For SID, things get complex
473 send_gdb "kill\n"
474 gdb_expect 10 {
475 -re ".*$mi_gdb_prompt$"
476 }
477 send_target_sid
478 gdb_expect 60 {
479 -re "\\^done.*$mi_gdb_prompt$" {
480 }
481 timeout {
482 perror "Unable to connect to SID target"
483 return -1
484 }
485 }
486 send_gdb "48-target-download\n"
487 gdb_expect 10 {
488 -re "48\\^done.*$mi_gdb_prompt$" {
489 }
490 timeout {
491 perror "Unable to download to SID target"
492 return -1
493 }
494 }
495 } elseif { [target_info protocol] == "sim" } {
496 # For the simulator, just connect to it directly.
497 send_gdb "47-target-select sim\n"
498 gdb_expect 10 {
499 -re "47\\^connected.*$mi_gdb_prompt$" {
500 }
501 timeout {
502 perror "Unable to select sim target"
503 return -1
504 }
505 }
506 send_gdb "48-target-download\n"
507 gdb_expect 10 {
508 -re "48\\^done.*$mi_gdb_prompt$" {
509 }
510 timeout {
511 perror "Unable to download to sim target"
512 return -1
513 }
514 }
515 } elseif { [target_info gdb_protocol] == "remote" } {
516 # remote targets
517 if { [mi_gdb_target_cmd "remote" [target_info netport]] != 0 } {
518 perror "Unable to connect to remote target"
519 return -1
520 }
521 send_gdb "48-target-download\n"
522 gdb_expect 10 {
523 -re "48\\^done.*$mi_gdb_prompt$" {
524 }
525 timeout {
526 perror "Unable to download to remote target"
527 return -1
528 }
529 }
530 }
531 return 0
532 }
533
534 #
535 # load a file into the debugger.
536 # return a -1 if anything goes wrong.
537 #
538 proc mi_gdb_load { arg } {
539 if { $arg != "" } {
540 return [mi_gdb_file_cmd $arg]
541 }
542 return 0
543 }
544
545 # mi_gdb_test COMMAND PATTERN MESSAGE [IPATTERN] -- send a command to gdb;
546 # test the result.
547 #
548 # COMMAND is the command to execute, send to GDB with send_gdb. If
549 # this is the null string no command is sent.
550 # PATTERN is the pattern to match for a PASS, and must NOT include
551 # the \r\n sequence immediately before the gdb prompt.
552 # MESSAGE is the message to be printed. (If this is the empty string,
553 # then sometimes we don't call pass or fail at all; I don't
554 # understand this at all.)
555 # IPATTERN is the pattern to match for the inferior's output. This parameter
556 # is optional. If present, it will produce a PASS if the match is
557 # successful, and a FAIL if unsuccessful.
558 #
559 # Returns:
560 # 1 if the test failed,
561 # 0 if the test passes,
562 # -1 if there was an internal error.
563 #
564 proc mi_gdb_test { args } {
565 global verbose
566 global mi_gdb_prompt
567 global GDB expect_out
568 upvar timeout timeout
569
570 set command [lindex $args 0]
571 set pattern [lindex $args 1]
572 set message [lindex $args 2]
573
574 if [llength $args]==4 {
575 set ipattern [lindex $args 3]
576 }
577
578 if [llength $args]==5 {
579 set question_string [lindex $args 3];
580 set response_string [lindex $args 4];
581 } else {
582 set question_string "^FOOBAR$"
583 }
584
585 if $verbose>2 then {
586 send_user "Sending \"$command\" to gdb\n"
587 send_user "Looking to match \"$pattern\"\n"
588 send_user "Message is \"$message\"\n"
589 }
590
591 set result -1
592 set string "${command}\n";
593 set string_regex [string_to_regexp $command]
594
595 if { $command != "" } {
596 while { "$string" != "" } {
597 set foo [string first "\n" "$string"];
598 set len [string length "$string"];
599 if { $foo < [expr $len - 1] } {
600 set str [string range "$string" 0 $foo];
601 if { [send_gdb "$str"] != "" } {
602 global suppress_flag;
603
604 if { ! $suppress_flag } {
605 perror "Couldn't send $command to GDB.";
606 }
607 fail "$message";
608 return $result;
609 }
610 gdb_expect 2 {
611 -re "\[\r\n\]" { }
612 timeout { }
613 }
614 set string [string range "$string" [expr $foo + 1] end];
615 } else {
616 break;
617 }
618 }
619 if { "$string" != "" } {
620 if { [send_gdb "$string"] != "" } {
621 global suppress_flag;
622
623 if { ! $suppress_flag } {
624 perror "Couldn't send $command to GDB.";
625 }
626 fail "$message";
627 return $result;
628 }
629 }
630 }
631
632 if [info exists timeout] {
633 set tmt $timeout;
634 } else {
635 global timeout;
636 if [info exists timeout] {
637 set tmt $timeout;
638 } else {
639 set tmt 60;
640 }
641 }
642 verbose -log "Expecting: ^($string_regex\[\r\n\]+)?($pattern\[\r\n\]+$mi_gdb_prompt\[ \]*)"
643 gdb_expect $tmt {
644 -re "\\*\\*\\* DOSEXIT code.*" {
645 if { $message != "" } {
646 fail "$message";
647 }
648 gdb_suppress_entire_file "GDB died";
649 return -1;
650 }
651 -re "Ending remote debugging.*$mi_gdb_prompt\[ \]*$" {
652 if ![isnative] then {
653 warning "Can`t communicate to remote target."
654 }
655 gdb_exit
656 gdb_start
657 set result -1
658 }
659 -re "^($string_regex\[\r\n\]+)?($pattern\[\r\n\]+$mi_gdb_prompt\[ \]*)" {
660 # At this point, $expect_out(1,string) is the MI input command.
661 # and $expect_out(2,string) is the MI output command.
662 # If $expect_out(1,string) is "", then there was no MI input command here.
663
664 # NOTE, there is no trailing anchor because with GDB/MI,
665 # asynchronous responses can happen at any point, causing more
666 # data to be available. Normally an anchor is used to make
667 # sure the end of the output is matched, however, $mi_gdb_prompt
668 # is just as good of an anchor since mi_gdb_test is meant to
669 # match a single mi output command. If a second GDB/MI output
670 # response is sent, it will be in the buffer for the next
671 # time mi_gdb_test is called.
672 if ![string match "" $message] then {
673 pass "$message"
674 }
675 set result 0
676 }
677 -re "(${question_string})$" {
678 send_gdb "$response_string\n";
679 exp_continue;
680 }
681 -re "Undefined.* command:.*$mi_gdb_prompt\[ \]*$" {
682 perror "Undefined command \"$command\"."
683 fail "$message"
684 set result 1
685 }
686 -re "Ambiguous command.*$mi_gdb_prompt\[ \]*$" {
687 perror "\"$command\" is not a unique command name."
688 fail "$message"
689 set result 1
690 }
691 -re "Program exited with code \[0-9\]+.*$mi_gdb_prompt\[ \]*$" {
692 if ![string match "" $message] then {
693 set errmsg "$message (the program exited)"
694 } else {
695 set errmsg "$command (the program exited)"
696 }
697 fail "$errmsg"
698 return -1
699 }
700 -re "The program is not being run.*$mi_gdb_prompt\[ \]*$" {
701 if ![string match "" $message] then {
702 set errmsg "$message (the program is no longer running)"
703 } else {
704 set errmsg "$command (the program is no longer running)"
705 }
706 fail "$errmsg"
707 return -1
708 }
709 -re ".*$mi_gdb_prompt\[ \]*$" {
710 if ![string match "" $message] then {
711 fail "$message"
712 }
713 set result 1
714 }
715 "<return>" {
716 send_gdb "\n"
717 perror "Window too small."
718 fail "$message"
719 }
720 -re "\\(y or n\\) " {
721 send_gdb "n\n"
722 perror "Got interactive prompt."
723 fail "$message"
724 }
725 eof {
726 perror "Process no longer exists"
727 if { $message != "" } {
728 fail "$message"
729 }
730 return -1
731 }
732 full_buffer {
733 perror "internal buffer is full."
734 fail "$message"
735 }
736 timeout {
737 if ![string match "" $message] then {
738 fail "$message (timeout)"
739 }
740 set result 1
741 }
742 }
743
744 # If the GDB output matched, compare the inferior output.
745 if { $result == 0 } {
746 if [ info exists ipattern ] {
747 if { ![target_info exists gdb,noinferiorio] } {
748 global mi_inferior_spawn_id
749 expect {
750 -i $mi_inferior_spawn_id -re "$ipattern" {
751 pass "$message inferior output"
752 }
753 timeout {
754 fail "$message inferior output (timeout)"
755 set result 1
756 }
757 }
758 } else {
759 unsupported "$message inferior output"
760 }
761 }
762 }
763
764 return $result
765 }
766
767 #
768 # MI run command. (A modified version of gdb_run_cmd)
769 #
770
771 # In patterns, the newline sequence ``\r\n'' is matched explicitly as
772 # ``.*$'' could swallow up output that we attempt to match elsewhere.
773
774 proc mi_run_cmd {args} {
775 global suppress_flag
776 if { $suppress_flag } {
777 return -1
778 }
779 global mi_gdb_prompt
780 global thread_selected_re
781
782 if [target_info exists gdb_init_command] {
783 send_gdb "[target_info gdb_init_command]\n";
784 gdb_expect 30 {
785 -re "$mi_gdb_prompt$" { }
786 default {
787 perror "gdb_init_command for target failed";
788 return;
789 }
790 }
791 }
792
793 if { [mi_gdb_target_load] < 0 } {
794 return
795 }
796
797 if [target_info exists use_gdb_stub] {
798 if [target_info exists gdb,do_reload_on_run] {
799 send_gdb "220-exec-continue\n";
800 gdb_expect 60 {
801 -re "220\\^running\[\r\n\]+\\*running,thread-id=\"\[^\"\]+\"\r\n$mi_gdb_prompt" {}
802 default {}
803 }
804 return;
805 }
806
807 if [target_info exists gdb,start_symbol] {
808 set start [target_info gdb,start_symbol];
809 } else {
810 set start "start";
811 }
812
813 # HACK: Should either use 000-jump or fix the target code
814 # to better handle RUN.
815 send_gdb "jump *$start\n"
816 warning "Using CLI jump command, expect run-to-main FAIL"
817 return
818 }
819
820 send_gdb "220-exec-run $args\n"
821 gdb_expect {
822 -re "220\\^running\r\n(\\*running,thread-id=\"\[^\"\]+\"\r\n|=thread-created,id=\"1\",group-id=\"\[0-9\]+\"\r\n)*(${thread_selected_re})?${mi_gdb_prompt}" {
823 }
824 timeout {
825 perror "Unable to start target"
826 return
827 }
828 }
829 # NOTE: Shortly after this there will be a ``000*stopped,...(gdb)''
830 }
831
832 #
833 # Just like run-to-main but works with the MI interface
834 #
835
836 proc mi_run_to_main { } {
837 global suppress_flag
838 if { $suppress_flag } {
839 return -1
840 }
841
842 global srcdir
843 global subdir
844 global binfile
845 global srcfile
846
847 mi_delete_breakpoints
848 mi_gdb_reinitialize_dir $srcdir/$subdir
849 mi_gdb_load ${binfile}
850
851 mi_runto main
852 }
853
854
855 # Just like gdb's "runto" proc, it will run the target to a given
856 # function. The big difference here between mi_runto and mi_execute_to
857 # is that mi_execute_to must have the inferior running already. This
858 # proc will (like gdb's runto) (re)start the inferior, too.
859 #
860 # FUNC is the linespec of the place to stop (it inserts a breakpoint here).
861 # It returns:
862 # -1 if test suppressed, failed, timedout
863 # 0 if test passed
864
865 proc mi_runto_helper {func run_or_continue} {
866 global suppress_flag
867 if { $suppress_flag } {
868 return -1
869 }
870
871 global mi_gdb_prompt expect_out
872 global hex decimal fullname_syntax
873
874 set test "mi runto $func"
875 mi_gdb_test "200-break-insert -t $func" \
876 "200\\^done,bkpt=\{number=\"\[0-9\]+\",type=\"breakpoint\",disp=\"del\",enabled=\"y\",addr=\"$hex\",func=\"$func\(\\\(.*\\\)\)?\",file=\".*\",line=\"\[0-9\]*\",times=\"0\",original-location=\".*\"\}" \
877 "breakpoint at $func"
878
879 if {![regexp {number="[0-9]+"} $expect_out(buffer) str]
880 || ![scan $str {number="%d"} bkptno]} {
881 set bkptno {[0-9]+}
882 }
883
884 if {$run_or_continue == "run"} {
885 mi_run_cmd
886 } else {
887 mi_send_resuming_command "exec-continue" "$test"
888 }
889
890 mi_expect_stop "breakpoint-hit" $func ".*" ".*" "\[0-9\]+" { "" "disp=\"del\"" } $test
891 }
892
893 proc mi_runto {func} {
894 mi_runto_helper $func "run"
895 }
896
897 # Next to the next statement
898 # For return values, see mi_execute_to_helper
899
900 proc mi_next { test } {
901 return [mi_next_to {.*} {.*} {.*} {.*} $test]
902 }
903
904
905 # Step to the next statement
906 # For return values, see mi_execute_to_helper
907
908 proc mi_step { test } {
909 return [mi_step_to {.*} {.*} {.*} {.*} $test]
910 }
911
912 set async "unknown"
913
914 proc detect_async {} {
915 global async
916 global mi_gdb_prompt
917
918 send_gdb "show target-async\n"
919
920 gdb_expect {
921 -re ".*Controlling the inferior in asynchronous mode is on...*$mi_gdb_prompt$" {
922 set async 1
923 }
924 -re ".*$mi_gdb_prompt$" {
925 set async 0
926 }
927 timeout {
928 set async 0
929 }
930 }
931 return $async
932 }
933
934 # Wait for MI *stopped notification to appear.
935 # The REASON, FUNC, ARGS, FILE and LINE are regular expressions
936 # to match against whatever is output in *stopped. ARGS should
937 # not include [] the list of argument is enclosed in, and other
938 # regular expressions should not include quotes.
939 # If EXTRA is a list of one element, it's the regular expression
940 # for output expected right after *stopped, and before GDB prompt.
941 # If EXTRA is a list of two elements, the first element is for
942 # output right after *stopped, and the second element is output
943 # right after reason field. The regex after reason should not include
944 # the comma separating it from the following fields.
945 #
946 # When we fail to match output at all, -1 is returned. Otherwise,
947 # the line at which we stop is returned. This is useful when exact
948 # line is not possible to specify for some reason -- one can pass
949 # the .* or "\[0-9\]*" regexps for line, and then check the line
950 # programmatically.
951 #
952 # Do not pass .* for any argument if you are expecting more than one stop.
953 proc mi_expect_stop { reason func args file line extra test } {
954
955 global mi_gdb_prompt
956 global hex
957 global decimal
958 global fullname_syntax
959 global async
960 global thread_selected_re
961
962 set after_stopped ""
963 set after_reason ""
964 if { [llength $extra] == 2 } {
965 set after_stopped [lindex $extra 0]
966 set after_reason [lindex $extra 1]
967 set after_reason "${after_reason},"
968 } elseif { [llength $extra] == 1 } {
969 set after_stopped [lindex $extra 0]
970 }
971
972 if {$async} {
973 set prompt_re ""
974 } else {
975 set prompt_re "$mi_gdb_prompt$"
976 }
977
978 if { $reason == "really-no-reason" } {
979 gdb_expect {
980 -re "\\*stopped\r\n$prompt_re" {
981 pass "$test"
982 }
983 timeout {
984 fail "$test (unknown output after running)"
985 }
986 }
987 return
988 }
989
990 if { $reason == "exited-normally" } {
991
992 gdb_expect {
993 -re "\\*stopped,reason=\"exited-normally\"\r\n$prompt_re" {
994 pass "$test"
995 }
996 -re ".*$mi_gdb_prompt$" {fail "continue to end (2)"}
997 timeout {
998 fail "$test (unknown output after running)"
999 }
1000 }
1001 return
1002 }
1003
1004 set args "\\\[$args\\\]"
1005
1006 set bn ""
1007 if { $reason == "breakpoint-hit" } {
1008 set bn {bkptno="[0-9]+",}
1009 }
1010
1011 set r ""
1012 if { $reason != "" } {
1013 set r "reason=\"$reason\","
1014 }
1015
1016
1017 set a $after_reason
1018
1019 set any "\[^\n\]*"
1020
1021 verbose -log "mi_expect_stop: expecting: \\*stopped,${r}${a}${bn}thread-id=\"$decimal\",stopped-threads=$any,frame=\{addr=\"$hex\",func=\"$func\",args=$args,file=\"$any$file\",fullname=\"${fullname_syntax}$file\",line=\"$line\"\}$after_stopped\r\n($thread_selected_re)?$prompt_re"
1022 gdb_expect {
1023 -re "\\*stopped,${r}${a}${bn}thread-id=\"$decimal\",stopped-threads=$any,frame=\{addr=\"$hex\",func=\"$func\",args=$args,file=\"$any$file\",fullname=\"${fullname_syntax}$file\",line=\"($line)\"\}$after_stopped\r\n($thread_selected_re)?$prompt_re" {
1024 pass "$test"
1025 return $expect_out(2,string)
1026 }
1027 -re "\\*stopped,${r}${a}${bn}thread-id=\"$decimal\",stopped-threads=$any,frame=\{addr=\"$hex\",func=\"$any\",args=\[\\\[\{\]$any\[\\\]\}\],file=\"$any\",fullname=\"${fullname_syntax}$any\",line=\"\[0-9\]*\"\}$any\r\n$prompt_re" {
1028 verbose -log "got $expect_out(buffer)"
1029 fail "$test (stopped at wrong place)"
1030 return -1
1031 }
1032 -re ".*\r\n$mi_gdb_prompt$" {
1033 verbose -log "got $expect_out(buffer)"
1034 fail "$test (unknown output after running)"
1035 return -1
1036 }
1037 timeout {
1038 fail "$test (timeout)"
1039 return -1
1040 }
1041 }
1042 }
1043
1044 # Wait for MI *stopped notification related to an interrupt request to
1045 # appear.
1046 proc mi_expect_interrupt { test } {
1047 global mi_gdb_prompt
1048 global decimal
1049 global async
1050
1051 if {$async} {
1052 set prompt_re ""
1053 } else {
1054 set prompt_re "$mi_gdb_prompt$"
1055 }
1056
1057 set r "reason=\"signal-received\",signal-name=\"0\",signal-meaning=\"Signal 0\""
1058
1059 set any "\[^\n\]*"
1060
1061 # A signal can land anywhere, just ignore the location
1062 verbose -log "mi_expect_interrupt: expecting: \\*stopped,${r},thread-id=\"$decimal\",stopped-threads=$any\r\n$prompt_re"
1063 gdb_expect {
1064 -re "\\*stopped,${r},thread-id=\"$decimal\",stopped-threads=$any\r\n$prompt_re" {
1065 pass "$test"
1066 return 0;
1067 }
1068 -re ".*\r\n$mi_gdb_prompt$" {
1069 verbose -log "got $expect_out(buffer)"
1070 fail "$test (unknown output after running)"
1071 return -1
1072 }
1073 timeout {
1074 fail "$test (timeout)"
1075 return -1
1076 }
1077 }
1078 }
1079
1080 # cmd should not include the number or newline (i.e. "exec-step 3", not
1081 # "220-exec-step 3\n"
1082
1083 # Can not match -re ".*\r\n${mi_gdb_prompt}", because of false positives
1084 # after the first prompt is printed.
1085
1086 proc mi_execute_to { cmd reason func args file line extra test } {
1087 global suppress_flag
1088 if { $suppress_flag } {
1089 return -1
1090 }
1091
1092 mi_send_resuming_command "$cmd" "$test"
1093 set r [mi_expect_stop $reason $func $args $file $line $extra $test]
1094 return $r
1095 }
1096
1097 proc mi_next_to { func args file line test } {
1098 mi_execute_to "exec-next" "end-stepping-range" "$func" "$args" \
1099 "$file" "$line" "" "$test"
1100 }
1101
1102 proc mi_step_to { func args file line test } {
1103 mi_execute_to "exec-step" "end-stepping-range" "$func" "$args" \
1104 "$file" "$line" "" "$test"
1105 }
1106
1107 proc mi_finish_to { func args file line result ret test } {
1108 mi_execute_to "exec-finish" "function-finished" "$func" "$args" \
1109 "$file" "$line" \
1110 ",gdb-result-var=\"$result\",return-value=\"$ret\"" \
1111 "$test"
1112 }
1113
1114 proc mi_continue_to {func} {
1115 mi_runto_helper $func "continue"
1116 }
1117
1118 proc mi0_execute_to { cmd reason func args file line extra test } {
1119 mi_execute_to_helper "$cmd" "$reason" "$func" "\{$args\}" \
1120 "$file" "$line" "$extra" "$test"
1121 }
1122
1123 proc mi0_next_to { func args file line test } {
1124 mi0_execute_to "exec-next" "end-stepping-range" "$func" "$args" \
1125 "$file" "$line" "" "$test"
1126 }
1127
1128 proc mi0_step_to { func args file line test } {
1129 mi0_execute_to "exec-step" "end-stepping-range" "$func" "$args" \
1130 "$file" "$line" "" "$test"
1131 }
1132
1133 proc mi0_finish_to { func args file line result ret test } {
1134 mi0_execute_to "exec-finish" "function-finished" "$func" "$args" \
1135 "$file" "$line" \
1136 ",gdb-result-var=\"$result\",return-value=\"$ret\"" \
1137 "$test"
1138 }
1139
1140 proc mi0_continue_to { bkptno func args file line test } {
1141 mi0_execute_to "exec-continue" "breakpoint-hit\",bkptno=\"$bkptno" \
1142 "$func" "$args" "$file" "$line" "" "$test"
1143 }
1144
1145 # Creates a breakpoint and checks the reported fields are as expected
1146 proc mi_create_breakpoint { location number disp func file line address test } {
1147 verbose -log "Expecting: 222\\^done,bkpt=\{number=\"$number\",type=\"breakpoint\",disp=\"$disp\",enabled=\"y\",addr=\"$address\",func=\"$func\",file=\"$file\",fullname=\".*\",line=\"$line\",times=\"0\",original-location=\".*\"\}"
1148 mi_gdb_test "222-break-insert $location" \
1149 "222\\^done,bkpt=\{number=\"$number\",type=\"breakpoint\",disp=\"$disp\",enabled=\"y\",addr=\"$address\",func=\"$func\",file=\"$file\",fullname=\".*\",line=\"$line\",times=\"0\",original-location=\".*\"\}" \
1150 $test
1151 }
1152
1153 proc mi_list_breakpoints { expected test } {
1154 set fullname ".*"
1155
1156 set body ""
1157 set first 1
1158
1159 foreach item $children {
1160 if {$first == 0} {
1161 set body "$body,"
1162 }
1163 set number disp func file line address
1164 set number [lindex $item 0]
1165 set disp [lindex $item 1]
1166 set func [lindex $item 2]
1167 set line [lindex $item 3]
1168 set address [lindex $item 4]
1169 set body "$body,bkpt=\{number=\"$number\",type=\"breakpoint\",disp=\"$disp\",enabled=\"y\",addr=\"$address\",func=\"$func\",file=\"$file\",${fullname},line=\"$line\",times=\"0\",original-location=\".*\"\}"
1170 set first 0
1171 }
1172
1173 verbose -log "Expecint: 666\\\^done,BreakpointTable=\{nr_rows=\".\",nr_cols=\".\",hdr=\\\[\{width=\".*\",alignment=\".*\",col_name=\"number\",colhdr=\"Num\"\}.*colhdr=\"Type\".*colhdr=\"Disp\".*colhdr=\"Enb\".*colhdr=\"Address\".*colhdr=\"What\".*\\\],body=\\\[$body\\\]\}" \
1174 mi_gdb_test "666-break-list" \
1175 "666\\\^done,BreakpointTable=\{nr_rows=\".\",nr_cols=\".\",hdr=\\\[\{width=\".*\",alignment=\".*\",col_name=\"number\",colhdr=\"Num\"\}.*colhdr=\"Type\".*colhdr=\"Disp\".*colhdr=\"Enb\".*colhdr=\"Address\".*colhdr=\"What\".*\\\],body=\\\[$body\\\]\}" \
1176 $test
1177 }
1178
1179 # Creates varobj named NAME for EXPRESSION.
1180 # Name cannot be "-".
1181 proc mi_create_varobj { name expression testname } {
1182 mi_gdb_test "-var-create $name * $expression" \
1183 "\\^done,name=\"$name\",numchild=\"\[0-9\]+\",value=\".*\",type=.*" \
1184 $testname
1185 }
1186
1187 proc mi_create_floating_varobj { name expression testname } {
1188 mi_gdb_test "-var-create $name @ $expression" \
1189 "\\^done,name=\"$name\",numchild=\"\[0-9\]+\",value=\".*\",type=.*" \
1190 $testname
1191 }
1192
1193
1194 # Same as mi_create_varobj, but also checks the reported type
1195 # of the varobj.
1196 proc mi_create_varobj_checked { name expression type testname } {
1197 mi_gdb_test "-var-create $name * $expression" \
1198 "\\^done,name=\"$name\",numchild=\"\[0-9\]+\",value=\".*\",type=\"$type\".*" \
1199 $testname
1200 }
1201
1202 # Deletes the specified NAME.
1203 proc mi_delete_varobj { name testname } {
1204 mi_gdb_test "-var-delete $name" \
1205 "\\^done,ndeleted=.*" \
1206 $testname
1207 }
1208
1209 # Updates varobj named NAME and checks that all varobjs in EXPECTED
1210 # are reported as updated, and no other varobj is updated.
1211 # Assumes that no varobj is out of scope and that no varobj changes
1212 # types.
1213 proc mi_varobj_update { name expected testname } {
1214 set er "\\^done,changelist=\\\["
1215 set first 1
1216 foreach item $expected {
1217 set v "{name=\"$item\",in_scope=\"true\",type_changed=\"false\"}"
1218 if {$first == 1} {
1219 set er "$er$v"
1220 set first 0
1221 } else {
1222 set er "$er,$v"
1223 }
1224 }
1225 set er "$er\\\]"
1226
1227 verbose -log "Expecting: $er" 2
1228 mi_gdb_test "-var-update $name" $er $testname
1229 }
1230
1231 proc mi_varobj_update_with_type_change { name new_type new_children testname } {
1232 set v "{name=\"$name\",in_scope=\"true\",type_changed=\"true\",new_type=\"$new_type\",new_num_children=\"$new_children\"}"
1233 set er "\\^done,changelist=\\\[$v\\\]"
1234 verbose -log "Expecting: $er"
1235 mi_gdb_test "-var-update $name" $er $testname
1236 }
1237
1238 proc mi_check_varobj_value { name value testname } {
1239
1240 mi_gdb_test "-var-evaluate-expression $name" \
1241 "\\^done,value=\"$value\"" \
1242 $testname
1243 }
1244
1245 # Check the results of the:
1246 #
1247 # -var-list-children VARNAME
1248 #
1249 # command. The CHILDREN parement should be a list of lists.
1250 # Each inner list can have either 3 or 4 elements, describing
1251 # fields that gdb is expected to report for child variable object,
1252 # in the following order
1253 #
1254 # - Name
1255 # - Expression
1256 # - Number of children
1257 # - Type
1258 #
1259 # If inner list has 3 elements, the gdb is expected to output no
1260 # type for a child and no value.
1261 #
1262 # If the inner list has 4 elements, gdb output is expected to
1263 # have no value.
1264 #
1265 proc mi_list_varobj_children { varname children testname } {
1266
1267 set options ""
1268 if {[llength $varname] == 2} {
1269 set options [lindex $varname 1]
1270 set varname [lindex $varname 0]
1271 }
1272
1273 set numchildren [llength $children]
1274 set children_exp {}
1275 set whatever "\"\[^\"\]+\""
1276
1277 foreach item $children {
1278
1279 set name [lindex $item 0]
1280 set exp [lindex $item 1]
1281 set numchild [lindex $item 2]
1282 if {[llength $item] == 5} {
1283 set type [lindex $item 3]
1284 set value [lindex $item 4]
1285
1286 lappend children_exp\
1287 "child={name=\"$name\",exp=\"$exp\",numchild=\"$numchild\",value=\"$value\",type=\"$type\"\(,thread-id=\"\[0-9\]+\")?}"
1288 } elseif {[llength $item] == 4} {
1289 set type [lindex $item 3]
1290
1291 lappend children_exp\
1292 "child={name=\"$name\",exp=\"$exp\",numchild=\"$numchild\",type=\"$type\"\(,thread-id=\"\[0-9\]+\")?}"
1293 } else {
1294 lappend children_exp\
1295 "child={name=\"$name\",exp=\"$exp\",numchild=\"$numchild\"(,thread-id=\"\[0-9\]+\")?}"
1296 }
1297 }
1298 set children_exp_j [join $children_exp ","]
1299 if {$numchildren} {
1300 set expected "\\^done,numchild=\".*\",children=\\\[$children_exp_j.*\\\]"
1301 } {
1302 set expected "\\^done,numchild=\"0\""
1303 }
1304
1305 verbose -log "Expecting: $expected"
1306
1307 mi_gdb_test "-var-list-children $options $varname" $expected $testname
1308 }
1309
1310 # Verifies that variable object VARNAME has NUMBER children,
1311 # where each one is named $VARNAME.<index-of-child> and has type TYPE.
1312 proc mi_list_array_varobj_children { varname number type testname } {
1313 set t {}
1314 for {set i 0} {$i < $number} {incr i} {
1315 lappend t [list $varname.$i $i 0 $type]
1316 }
1317 mi_list_varobj_children $varname $t $testname
1318 }
1319
1320 # A list of two-element lists. First element of each list is
1321 # a Tcl statement, and the second element is the line
1322 # number of source C file where the statement originates.
1323 set mi_autotest_data ""
1324 # The name of the source file for autotesting.
1325 set mi_autotest_source ""
1326
1327 proc count_newlines { string } {
1328 return [regexp -all "\n" $string]
1329 }
1330
1331 # Prepares for running inline tests in FILENAME.
1332 # See comments for mi_run_inline_test for detailed
1333 # explanation of the idea and syntax.
1334 proc mi_prepare_inline_tests { filename } {
1335
1336 global srcdir
1337 global subdir
1338 global mi_autotest_source
1339 global mi_autotest_data
1340
1341 set mi_autotest_data {}
1342
1343 set mi_autotest_source $filename
1344
1345 if { ! [regexp "^/" "$filename"] } then {
1346 set filename "$srcdir/$subdir/$filename"
1347 }
1348
1349 set chan [open $filename]
1350 set content [read $chan]
1351 set line_number 1
1352 while {1} {
1353 set start [string first "/*:" $content]
1354 if {$start != -1} {
1355 set end [string first ":*/" $content]
1356 if {$end == -1} {
1357 error "Unterminated special comment in $filename"
1358 }
1359
1360 set prefix [string range $content 0 $start]
1361 set prefix_newlines [count_newlines $prefix]
1362
1363 set line_number [expr $line_number+$prefix_newlines]
1364 set comment_line $line_number
1365
1366 set comment [string range $content [expr $start+3] [expr $end-1]]
1367
1368 set comment_newlines [count_newlines $comment]
1369 set line_number [expr $line_number+$comment_newlines]
1370
1371 set comment [string trim $comment]
1372 set content [string range $content [expr $end+3] \
1373 [string length $content]]
1374 lappend mi_autotest_data [list $comment $comment_line]
1375 } else {
1376 break
1377 }
1378 }
1379 close $chan
1380 }
1381
1382 # Helper to mi_run_inline_test below.
1383 # Return the list of all (statement,line_number) lists
1384 # that comprise TESTCASE. The begin and end markers
1385 # are not included.
1386 proc mi_get_inline_test {testcase} {
1387
1388 global mi_gdb_prompt
1389 global mi_autotest_data
1390 global mi_autotest_source
1391
1392 set result {}
1393
1394 set seen_begin 0
1395 set seen_end 0
1396 foreach l $mi_autotest_data {
1397
1398 set comment [lindex $l 0]
1399
1400 if {$comment == "BEGIN: $testcase"} {
1401 set seen_begin 1
1402 } elseif {$comment == "END: $testcase"} {
1403 set seen_end 1
1404 break
1405 } elseif {$seen_begin==1} {
1406 lappend result $l
1407 }
1408 }
1409
1410 if {$seen_begin == 0} {
1411 error "Autotest $testcase not found"
1412 }
1413
1414 if {$seen_begin == 1 && $seen_end == 0} {
1415 error "Missing end marker for test $testcase"
1416 }
1417
1418 return $result
1419 }
1420
1421 # Sets temporary breakpoint at LOCATION.
1422 proc mi_tbreak {location} {
1423
1424 global mi_gdb_prompt
1425
1426 mi_gdb_test "-break-insert -t $location" \
1427 {\^done,bkpt=.*} \
1428 "run to $location (set breakpoint)"
1429 }
1430
1431 # Send COMMAND that must be a command that resumes
1432 # the inferiour (run/continue/next/etc) and consumes
1433 # the "^running" output from it.
1434 proc mi_send_resuming_command_raw {command test} {
1435
1436 global mi_gdb_prompt
1437 global thread_selected_re
1438
1439 send_gdb "$command\n"
1440 gdb_expect {
1441 -re "\\^running\r\n\\*running,thread-id=\"\[^\"\]+\"\r\n($thread_selected_re)?${mi_gdb_prompt}" {
1442 # Note that lack of 'pass' call here -- this works around limitation
1443 # in DejaGNU xfail mechanism. mi-until.exp has this:
1444 #
1445 # setup_kfail gdb/2104 "*-*-*"
1446 # mi_execute_to ...
1447 #
1448 # and mi_execute_to uses mi_send_resuming_command. If we use 'pass' here,
1449 # it will reset kfail, so when the actual test fails, it will be flagged
1450 # as real failure.
1451 return 0
1452 }
1453 -re ".*${mi_gdb_prompt}" {
1454 fail "$test (failed to resume)"
1455 return -1
1456 }
1457 -re "\\^error,msg=.*" {
1458 fail "$test (MI error)"
1459 return -1
1460 }
1461 timeout {
1462 fail "$test"
1463 return -1
1464 }
1465 }
1466 }
1467
1468 proc mi_send_resuming_command {command test} {
1469 mi_send_resuming_command_raw -$command $test
1470 }
1471
1472 # Helper to mi_run_inline_test below.
1473 # Sets a temporary breakpoint at LOCATION and runs
1474 # the program using COMMAND. When the program is stopped
1475 # returns the line at which it. Returns -1 if line cannot
1476 # be determined.
1477 # Does not check that the line is the same as requested.
1478 # The caller can check itself if required.
1479 proc mi_continue_to_line {location test} {
1480
1481 mi_tbreak $location
1482 mi_send_resuming_command "exec-continue" "run to $location (exec-continue)"
1483 return [mi_get_stop_line $test]
1484 }
1485
1486 # Wait until gdb prints the current line.
1487 proc mi_get_stop_line {test} {
1488
1489 global mi_gdb_prompt
1490 global async
1491
1492 if {$async} {
1493 set prompt_re ""
1494 } else {
1495 set prompt_re "$mi_gdb_prompt$"
1496 }
1497
1498 gdb_expect {
1499 -re ".*line=\"(\[0-9\]*)\".*\r\n$prompt_re" {
1500 return $expect_out(1,string)
1501 }
1502 -re ".*$mi_gdb_prompt" {
1503 fail "wait for stop ($test)"
1504 }
1505 timeout {
1506 fail "wait for stop ($test)"
1507 }
1508 }
1509 }
1510
1511 # Run a MI test embedded in comments in a C file.
1512 # The C file should contain special comments in the following
1513 # three forms:
1514 #
1515 # /*: BEGIN: testname :*/
1516 # /*: <Tcl statements> :*/
1517 # /*: END: testname :*/
1518 #
1519 # This procedure find the begin and end marker for the requested
1520 # test. Then, a temporary breakpoint is set at the begin
1521 # marker and the program is run (from start).
1522 #
1523 # After that, for each special comment between the begin and end
1524 # marker, the Tcl statements are executed. It is assumed that
1525 # for each comment, the immediately preceding line is executable
1526 # C statement. Then, gdb will be single-stepped until that
1527 # preceding C statement is executed, and after that the
1528 # Tcl statements in the comment will be executed.
1529 #
1530 # For example:
1531 #
1532 # /*: BEGIN: assignment-test :*/
1533 # v = 10;
1534 # /*: <Tcl code to check that 'v' is indeed 10 :*/
1535 # /*: END: assignment-test :*/
1536 #
1537 # The mi_prepare_inline_tests function should be called before
1538 # calling this function. A given C file can contain several
1539 # inline tests. The names of the tests must be unique within one
1540 # C file.
1541 #
1542 proc mi_run_inline_test { testcase } {
1543
1544 global mi_gdb_prompt
1545 global hex
1546 global decimal
1547 global fullname_syntax
1548 global mi_autotest_source
1549
1550 set commands [mi_get_inline_test $testcase]
1551
1552 set first 1
1553 set line_now 1
1554
1555 foreach c $commands {
1556 set statements [lindex $c 0]
1557 set line [lindex $c 1]
1558 set line [expr $line-1]
1559
1560 # We want gdb to be stopped at the expression immediately
1561 # before the comment. If this is the first comment, the
1562 # program is either not started yet or is in some random place,
1563 # so we run it. For further comments, we might be already
1564 # standing at the right line. If not continue till the
1565 # right line.
1566
1567 if {$first==1} {
1568 # Start the program afresh.
1569 mi_tbreak "$mi_autotest_source:$line"
1570 mi_run_cmd
1571 set line_now [mi_get_stop_line "$testcase: step to $line"]
1572 set first 0
1573 } elseif {$line_now!=$line} {
1574 set line_now [mi_continue_to_line "$mi_autotest_source:$line" "continue to $line"]
1575 }
1576
1577 if {$line_now!=$line} {
1578 fail "$testcase: go to line $line"
1579 }
1580
1581 # We're not at the statement right above the comment.
1582 # Execute that statement so that the comment can test
1583 # the state after the statement is executed.
1584
1585 # Single-step past the line.
1586 if { [mi_send_resuming_command "exec-next" "$testcase: step over $line"] != 0 } {
1587 return -1
1588 }
1589 set line_now [mi_get_stop_line "$testcase: step over $line"]
1590
1591 # We probably want to use 'uplevel' so that statements
1592 # have direct access to global variables that the
1593 # main 'exp' file has set up. But it's not yet clear,
1594 # will need more experience to be sure.
1595 eval $statements
1596 }
1597 }
1598
1599 proc get_mi_thread_list {name} {
1600 global expect_out
1601
1602 # MI will return a list of thread ids:
1603 #
1604 # -thread-list-ids
1605 # ^done,thread-ids=[thread-id="1",thread-id="2",...],number-of-threads="N"
1606 # (gdb)
1607 mi_gdb_test "-thread-list-ids" \
1608 {.*\^done,thread-ids={(thread-id="[0-9]+"(,)?)+},number-of-threads="[0-9]+"} \
1609 "-thread_list_ids ($name)"
1610
1611 set output {}
1612 if {[info exists expect_out(buffer)]} {
1613 set output $expect_out(buffer)
1614 }
1615
1616 set thread_list {}
1617 if {![regexp {thread-ids=\{(thread-id="[0-9]+"(,)?)*\}} $output threads]} {
1618 fail "finding threads in MI output ($name)"
1619 } else {
1620 pass "finding threads in MI output ($name)"
1621
1622 # Make list of console threads
1623 set start [expr {[string first \{ $threads] + 1}]
1624 set end [expr {[string first \} $threads] - 1}]
1625 set threads [string range $threads $start $end]
1626 foreach thread [split $threads ,] {
1627 if {[scan $thread {thread-id="%d"} num]} {
1628 lappend thread_list $num
1629 }
1630 }
1631 }
1632
1633 return $thread_list
1634 }
1635
1636 # Check that MI and the console know of the same threads.
1637 # Appends NAME to all test names.
1638 proc check_mi_and_console_threads {name} {
1639 global expect_out
1640
1641 mi_gdb_test "-thread-list-ids" \
1642 {.*\^done,thread-ids={(thread-id="[0-9]+"(,)*)+},number-of-threads="[0-9]+"} \
1643 "-thread-list-ids ($name)"
1644 set mi_output {}
1645 if {[info exists expect_out(buffer)]} {
1646 set mi_output $expect_out(buffer)
1647 }
1648
1649 # GDB will return a list of thread ids and some more info:
1650 #
1651 # (gdb)
1652 # -interpreter-exec console "info threads"
1653 # ~" 4 Thread 2051 (LWP 7734) 0x401166b1 in __libc_nanosleep () at __libc_nanosleep:-1"
1654 # ~" 3 Thread 1026 (LWP 7733) () at __libc_nanosleep:-1"
1655 # ~" 2 Thread 2049 (LWP 7732) 0x401411f8 in __poll (fds=0x804bb24, nfds=1, timeout=2000) at ../sysdeps/unix/sysv/linux/poll.c:63"
1656 # ~"* 1 Thread 1024 (LWP 7731) main (argc=1, argv=0xbfffdd94) at ../../../src/gdb/testsuite/gdb.mi/pthreads.c:160"
1657 # FIXME: kseitz/2002-09-05: Don't use the hack-cli method.
1658 mi_gdb_test "info threads" \
1659 {.*(~".*"[\r\n]*)+.*} \
1660 "info threads ($name)"
1661 set console_output {}
1662 if {[info exists expect_out(buffer)]} {
1663 set console_output $expect_out(buffer)
1664 }
1665
1666 # Make a list of all known threads to console (gdb's thread IDs)
1667 set console_thread_list {}
1668 foreach line [split $console_output \n] {
1669 if {[string index $line 0] == "~"} {
1670 # This is a line from the console; trim off "~", " ", "*", and "\""
1671 set line [string trim $line ~\ \"\*]
1672 if {[scan $line "%d" id] == 1} {
1673 lappend console_thread_list $id
1674 }
1675 }
1676 }
1677
1678 # Now find the result string from MI
1679 set mi_result ""
1680 foreach line [split $mi_output \n] {
1681 if {[string range $line 0 4] == "^done"} {
1682 set mi_result $line
1683 }
1684 }
1685 if {$mi_result == ""} {
1686 fail "finding MI result string ($name)"
1687 } else {
1688 pass "finding MI result string ($name)"
1689 }
1690
1691 # Finally, extract the thread ids and compare them to the console
1692 set num_mi_threads_str ""
1693 if {![regexp {number-of-threads="[0-9]+"} $mi_result num_mi_threads_str]} {
1694 fail "finding number of threads in MI output ($name)"
1695 } else {
1696 pass "finding number of threads in MI output ($name)"
1697
1698 # Extract the number of threads from the MI result
1699 if {![scan $num_mi_threads_str {number-of-threads="%d"} num_mi_threads]} {
1700 fail "got number of threads from MI ($name)"
1701 } else {
1702 pass "got number of threads from MI ($name)"
1703
1704 # Check if MI and console have same number of threads
1705 if {$num_mi_threads != [llength $console_thread_list]} {
1706 fail "console and MI have same number of threads ($name)"
1707 } else {
1708 pass "console and MI have same number of threads ($name)"
1709
1710 # Get MI thread list
1711 set mi_thread_list [get_mi_thread_list $name]
1712
1713 # Check if MI and console have the same threads
1714 set fails 0
1715 foreach ct [lsort $console_thread_list] mt [lsort $mi_thread_list] {
1716 if {$ct != $mt} {
1717 incr fails
1718 }
1719 }
1720 if {$fails > 0} {
1721 fail "MI and console have same threads ($name)"
1722
1723 # Send a list of failures to the log
1724 send_log "Console has thread ids: $console_thread_list\n"
1725 send_log "MI has thread ids: $mi_thread_list\n"
1726 } else {
1727 pass "MI and console have same threads ($name)"
1728 }
1729 }
1730 }
1731 }
1732 }
1733
1734 proc mi_load_shlibs { args } {
1735 if {![is_remote target]} {
1736 return
1737 }
1738
1739 foreach file $args {
1740 gdb_download $file
1741 }
1742
1743 # Even if the target supplies full paths for shared libraries,
1744 # they may not be paths for this system.
1745 mi_gdb_test "set solib-search-path [file dirname [lindex $args 0]]" "\^done" ""
1746 }
1747
1748 proc mi_reverse_list { list } {
1749 if { [llength $list] <= 1 } {
1750 return $list
1751 }
1752 set tail [lrange $list 1 [llength $list]]
1753 set rtail [mi_reverse_list $tail]
1754 lappend rtail [lindex $list 0]
1755 return $rtail
1756 }
1757
1758 proc mi_check_thread_states { xstates test } {
1759 global expect_out
1760 set states [mi_reverse_list $xstates]
1761 set pattern "\\^done,threads=\\\["
1762 foreach s $states {
1763 set pattern "${pattern}(.*)state=\"$s\""
1764 }
1765 set pattern "$pattern\\\}\\\].*"
1766
1767 verbose -log "expecting: $pattern"
1768 mi_gdb_test "-thread-info" $pattern $test
1769 }