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