]> Git Repo - binutils.git/blob - gdb/testsuite/lib/mi-support.exp
Automatic date update in version.in
[binutils.git] / gdb / testsuite / lib / mi-support.exp
1 # Copyright 1999-2022 Free Software Foundation, Inc.
2
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 3 of the License, or
6 # (at your option) any later version.
7 #
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 # GNU General Public License for more details.
12 #
13 # You should have received a copy of the GNU General Public License
14 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
15
16 # This file was based on a file written by Fred Fish. ([email protected])
17
18 # Test setup routines that work with the MI interpreter.
19
20 load_lib gdb-utils.exp
21
22 # The variable mi_gdb_prompt is a regexp which matches the gdb mi prompt.
23 # Set it if it is not already set.
24 global mi_gdb_prompt
25 if {![info exists mi_gdb_prompt]} {
26     set mi_gdb_prompt "\[(\]gdb\[)\] \r\n"
27 }
28
29 global mi_inferior_tty_name
30
31 # Always points to GDB's main UI spawn ID, even when testing with MI
32 # running on a secondary UI.
33 global gdb_main_spawn_id
34
35 # Points to the spawn id of the MI channel.  When testing with MI
36 # running as the primary/main UI, this is the same as
37 # gdb_main_spawn_id, but will be different when testing with MI
38 # running on a secondary UI.
39 global mi_spawn_id
40
41 set MIFLAGS "-i=mi"
42
43 set thread_selected_re "=thread-selected,id=\"\[0-9\]+\"\r\n"
44 set gdbindex_warning_re "&\"warning: Skipping \[^\r\n\]+ \.gdb_index section in \[^\r\n\]+\"\r\n(?:&\"\\\\n\"\r\n)?"
45 set library_loaded_re "=library-loaded\[^\n\]+\"\r\n(?:$gdbindex_warning_re)?"
46 set breakpoint_re "=(?:breakpoint-created|breakpoint-deleted)\[^\n\]+\"\r\n"
47
48 #
49 # mi_gdb_exit -- exit the GDB, killing the target program if necessary
50 #
51 proc mi_gdb_exit {} {
52     catch mi_uncatched_gdb_exit
53 }
54
55 proc mi_uncatched_gdb_exit {} {
56     global GDB
57     global INTERNAL_GDBFLAGS GDBFLAGS
58     global gdb_spawn_id gdb_main_spawn_id
59     global mi_spawn_id inferior_spawn_id
60     global gdb_prompt
61     global mi_gdb_prompt
62     global MIFLAGS
63
64     if { [info procs sid_exit] != "" } {
65         sid_exit
66     }
67
68     if ![info exists gdb_spawn_id] {
69         return
70     }
71
72     verbose "Quitting $GDB $INTERNAL_GDBFLAGS $GDBFLAGS $MIFLAGS"
73
74     if { [is_remote host] && [board_info host exists fileid] } {
75         send_gdb "999-gdb-exit\n"
76         gdb_expect 10 {
77             -re "y or n" {
78                 send_gdb "y\n"
79                 exp_continue
80             }
81             -re "Undefined command.*$gdb_prompt $" {
82                 send_gdb "quit\n"
83                 exp_continue
84             }
85             -re "DOSEXIT code" { }
86             -re "\r\n999\\^exit\r\n" { }
87         }
88     }
89
90     # Switch back to the main spawn id, so that remote_close below
91     # closes it, and not a secondary channel.  Closing a secondary
92     # channel does not make GDB exit.
93     if {$gdb_spawn_id != $gdb_main_spawn_id} {
94         switch_gdb_spawn_id $gdb_main_spawn_id
95     }
96
97     # Close secondary MI channel, if there's one.
98     if {$mi_spawn_id != $gdb_main_spawn_id} {
99         close -i $mi_spawn_id
100     }
101
102     if ![is_remote host] {
103         remote_close host
104     }
105     unset gdb_spawn_id
106     unset gdb_main_spawn_id
107     unset mi_spawn_id
108     unset inferior_spawn_id
109 }
110
111 # Create the PTY for the inferior process and tell GDB about it.
112
113 proc mi_create_inferior_pty {} {
114     global mi_gdb_prompt
115     global inferior_spawn_id
116     global mi_inferior_tty_name
117
118     spawn -pty
119     set inferior_spawn_id $spawn_id
120     set tty_name $spawn_out(slave,name)
121     set mi_inferior_tty_name $tty_name
122
123     send_gdb "102-inferior-tty-set $tty_name\n"
124     gdb_expect 10 {
125         -re ".*102\\\^done\r\n$mi_gdb_prompt$" {
126             verbose "redirect inferior output to new terminal device."
127         }
128         timeout {
129             warning "Couldn't redirect inferior output." 2
130         }
131     }
132 }
133
134 #
135 # Like default_mi_gdb_start below, but the MI is created as a separate
136 # ui in a new tty.  The global MI_SPAWN_ID is updated to point at the
137 # new tty created for the MI interface.  The global GDB_MAIN_SPAWN_ID
138 # is updated to the current value of the global GDB_SPAWN_ID.
139 #
140 proc mi_gdb_start_separate_mi_tty { { flags {} } } {
141     global gdb_prompt mi_gdb_prompt
142     global timeout
143     global gdb_spawn_id gdb_main_spawn_id mi_spawn_id
144     global inferior_spawn_id
145
146     set separate_inferior_pty 0
147
148     foreach flag $flags {
149         if {$flag == "separate-inferior-tty"} {
150             set separate_inferior_pty 1
151         }
152     }
153
154     gdb_start
155
156     # Create the new PTY for the MI UI.
157     spawn -pty
158     set mi_spawn_id $spawn_id
159     set mi_tty_name $spawn_out(slave,name)
160     gdb_test_multiple "new-ui mi $mi_tty_name" "new-ui" {
161         -re "New UI allocated\r\n$gdb_prompt $" {
162         }
163     }
164
165     # Switch to the MI channel.
166     set gdb_main_spawn_id $gdb_spawn_id
167     switch_gdb_spawn_id $mi_spawn_id
168
169     # Consume pending output and MI prompt.
170     gdb_expect {
171         -re "$mi_gdb_prompt$" {
172         }
173         default {
174             perror "MI channel failed"
175             remote_close host
176             return -1
177         }
178     }
179
180     if {$separate_inferior_pty} {
181         mi_create_inferior_pty
182     }
183
184     mi_detect_async
185
186     return 0
187 }
188
189 #
190 # default_mi_gdb_start [FLAGS] -- start gdb running, default procedure
191 #
192 # FLAGS is a list of flags, each flag is a string.
193 #
194 # If "separate-inferior-tty" is specified, the inferior works with
195 # it's own PTY.
196 #
197 # If "separate-mi-tty" is specified, the gdb starts in CLI mode, with
198 # MI running on a secondary UI, on its own tty.
199 #
200 # When running over NFS, particularly if running many simultaneous
201 # tests on different hosts all using the same server, things can
202 # get really slow.  Give gdb at least 3 minutes to start up.
203 #
204 proc default_mi_gdb_start { { flags {} } } {
205     global use_gdb_stub
206     global GDB
207     global INTERNAL_GDBFLAGS GDBFLAGS
208     global gdb_prompt
209     global mi_gdb_prompt
210     global timeout
211     global gdb_spawn_id gdb_main_spawn_id inferior_spawn_id mi_spawn_id
212     global MIFLAGS
213     global FORCE_SEPARATE_MI_TTY
214
215     # Keep track of the number of times GDB has been launched.
216     global gdb_instances
217     incr gdb_instances
218
219     gdb_stdin_log_init
220
221     if {[info exists FORCE_SEPARATE_MI_TTY]} {
222         set separate_mi_pty $FORCE_SEPARATE_MI_TTY
223     } else {
224         set separate_mi_pty 0
225     }
226
227     set separate_inferior_pty 0
228
229     foreach flag $flags {
230         if {$flag == "separate-mi-tty"} {
231             set separate_mi_pty 1
232         } elseif {$flag == "separate-inferior-tty"} {
233             set separate_inferior_pty 1
234         }
235     }
236
237     if {$separate_mi_pty} {
238         return [mi_gdb_start_separate_mi_tty $flags]
239     }
240
241     set inferior_pty no-tty
242
243     # Set the default value, it may be overriden later by specific testfile.
244     set use_gdb_stub [target_info exists use_gdb_stub]
245
246     # Start SID.
247     if { [info procs sid_start] != "" } {
248         verbose "Spawning SID"
249         sid_start
250     }
251
252     if [info exists gdb_spawn_id] {
253         return 0
254     }
255
256     save_vars { GDBFLAGS } {
257         append GDBFLAGS " $MIFLAGS"
258
259         set res [gdb_spawn]
260         if { $res != 0} {
261             return $res
262         }
263     }
264
265     gdb_expect {
266         -re "~\"GNU.*\r\n~\".*$mi_gdb_prompt$" {
267             # We have a new format mi startup prompt.  If we are
268             # running mi1, then this is an error as we should be
269             # using the old-style prompt.
270             if { $MIFLAGS == "-i=mi1" } {
271                 perror "(mi startup) Got unexpected new mi prompt."
272                 remote_close host
273                 unset gdb_spawn_id
274                 return -1
275             }
276             verbose "GDB initialized."
277         }
278         -re "\[^~\].*$mi_gdb_prompt$" {
279             # We have an old format mi startup prompt.  If we are
280             # not running mi1, then this is an error as we should be
281             # using the new-style prompt.
282             if { $MIFLAGS != "-i=mi1" } {
283                 perror "(mi startup) Got unexpected old mi prompt."
284                 remote_close host
285                 unset gdb_spawn_id
286                 return -1
287             }
288             verbose "GDB initialized."
289         }
290         -re ".*unrecognized option.*for a complete list of options." {
291             untested "skip mi tests (not compiled with mi support)."
292             remote_close host
293             unset gdb_spawn_id
294             return -1
295         }
296         -re ".*Interpreter `mi' unrecognized." {
297             untested "skip mi tests (not compiled with mi support)."
298             remote_close host
299             unset gdb_spawn_id
300             return -1
301         }
302         timeout {
303             perror "(timeout) GDB never initialized after 10 seconds."
304             remote_close host
305             unset gdb_spawn_id
306             return -1
307         }
308     }
309     set gdb_main_spawn_id $gdb_spawn_id
310     set mi_spawn_id $gdb_spawn_id
311
312     # FIXME: mi output does not go through pagers, so these can be removed.
313     # force the height to "unlimited", so no pagers get used
314     send_gdb "100-gdb-set height 0\n"
315     gdb_expect 10 {
316         -re ".*100-gdb-set height 0\r\n100\\\^done\r\n$mi_gdb_prompt$" {
317             verbose "Setting height to 0." 2
318         }
319         timeout {
320             warning "Couldn't set the height to 0"
321         }
322     }
323     # force the width to "unlimited", so no wraparound occurs
324     send_gdb "101-gdb-set width 0\n"
325     gdb_expect 10 {
326         -re ".*101-gdb-set width 0\r\n101\\\^done\r\n$mi_gdb_prompt$" {
327             verbose "Setting width to 0." 2
328         }
329         timeout {
330             warning "Couldn't set the width to 0."
331         }
332     }
333
334     if { $separate_inferior_pty } {
335         mi_create_inferior_pty
336     }
337
338     if {![info exists inferior_spawn_id]} {
339         set inferior_spawn_id $gdb_spawn_id
340     }
341
342     mi_detect_async
343
344     return 0
345 }
346
347 #
348 # Overridable function. You can override this function in your
349 # baseboard file.
350 #
351 proc mi_gdb_start { args } {
352   return [eval default_mi_gdb_start $args]
353 }
354
355 # Many of the tests depend on setting breakpoints at various places and
356 # running until that breakpoint is reached.  At times, we want to start
357 # with a clean-slate with respect to breakpoints, so this utility proc 
358 # lets us do this without duplicating this code everywhere.
359 #
360
361 proc mi_delete_breakpoints {} {
362     global mi_gdb_prompt
363
364 # FIXME: The mi operation won't accept a prompt back and will use the 'all' arg
365     send_gdb "102-break-delete\n"
366     gdb_expect 30 {
367          -re "Delete all breakpoints.*y or n.*$" {
368             send_gdb "y\n"
369             exp_continue
370          }
371          -re "102-break-delete\r\n102\\\^done\r\n$mi_gdb_prompt$" {
372              # This happens if there were no breakpoints
373          }
374          timeout { perror "Delete all breakpoints in mi_delete_breakpoints (timeout)" ; return }
375     }
376
377 # The correct output is not "No breakpoints or watchpoints." but an
378 # empty BreakpointTable. Also, a query is not acceptable with mi.
379     send_gdb "103-break-list\n"
380     gdb_expect 30 {
381          -re "103-break-list\r\n103\\\^done,BreakpointTable=\{\}\r\n$mi_gdb_prompt$" {}
382          -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$" {}
383          -re "103-break-list\r\n103\\\^doneNo breakpoints or watchpoints.\r\n\r\n$mi_gdb_prompt$" {warning "Unexpected console text received"}
384          -re "$mi_gdb_prompt$" { perror "Breakpoints not deleted" ; return }
385          -re "Delete all breakpoints.*or n.*$" {
386             warning "Unexpected prompt for breakpoints deletion"
387             send_gdb "y\n"
388             exp_continue
389         }
390          timeout { perror "-break-list (timeout)" ; return }
391     }
392 }
393
394 proc mi_gdb_reinitialize_dir { subdir } {
395     global mi_gdb_prompt
396     global MIFLAGS
397
398     if [is_remote host] {
399         return ""
400     }
401
402     if { $MIFLAGS == "-i=mi1" } {
403       send_gdb "104-environment-directory\n"
404       gdb_expect 60 {
405         -re ".*Reinitialize source path to empty.*y or n. " {
406             warning "Got confirmation prompt for dir reinitialization."
407             send_gdb "y\n"
408             gdb_expect 60 {
409                 -re "$mi_gdb_prompt$" {}
410                 timeout {error "Dir reinitialization failed (timeout)"}
411             }
412         }
413         -re "$mi_gdb_prompt$" {}
414           timeout {error "Dir reinitialization failed (timeout)"}
415       }
416     } else {
417         send_gdb "104-environment-directory -r\n"
418         gdb_expect 60 {
419             -re "104\\\^done,source-path=.*\r\n$mi_gdb_prompt$" {}
420             -re "$mi_gdb_prompt$" {}
421             timeout {error "Dir reinitialization failed (timeout)"}
422       }
423     }
424
425     send_gdb "105-environment-directory $subdir\n"
426     gdb_expect 60 {
427         -re "Source directories searched.*$mi_gdb_prompt$" {
428             verbose "Dir set to $subdir"
429         }
430         -re "105\\\^done.*\r\n$mi_gdb_prompt$" {
431             # FIXME: We return just the prompt for now.
432             verbose "Dir set to $subdir"
433             # perror "Dir \"$subdir\" failed."
434         }
435     }
436 }
437
438 # Send GDB the "target" command.
439 # FIXME: Some of these patterns are not appropriate for MI.  Based on
440 # config/monitor.exp:gdb_target_command.
441 proc mi_gdb_target_cmd { targetname serialport } {
442     global mi_gdb_prompt
443
444     set serialport_re [string_to_regexp $serialport]
445     for {set i 1} {$i <= 3} {incr i} {
446         send_gdb "47-target-select $targetname $serialport\n"
447         gdb_expect 60 {
448             -re "47\\^connected.*$mi_gdb_prompt" {
449                 verbose "Set target to $targetname"
450                 return 0
451             }
452             -re "unknown host.*$mi_gdb_prompt" {
453                 verbose "Couldn't look up $serialport"
454             }
455             -re "Couldn't establish connection to remote.*$mi_gdb_prompt$" {
456                 verbose "Connection failed"
457             }
458             -re "Remote MIPS debugging.*$mi_gdb_prompt$" {
459                 verbose "Set target to $targetname"
460                 return 0
461             }
462             -re "Remote debugging using .*$serialport_re.*$mi_gdb_prompt$" {
463                 verbose "Set target to $targetname"
464                 return 0
465             }
466             -re "Remote target $targetname connected to.*$mi_gdb_prompt$" {
467                 verbose "Set target to $targetname"
468                 return 0
469             }
470             -re "Connected to.*$mi_gdb_prompt$" {
471                 verbose "Set target to $targetname"
472                 return 0
473             }
474             -re "Ending remote.*$mi_gdb_prompt$" { }
475             -re "Connection refused.*$mi_gdb_prompt$" {
476                 verbose "Connection refused by remote target.  Pausing, and trying again."
477                 sleep 5
478                 continue
479             }
480             -re "Non-stop mode requested, but remote does not support non-stop.*$mi_gdb_prompt" {
481                 unsupported "non-stop mode not supported"
482                 return 1
483             }
484             -re "Timeout reading from remote system.*$mi_gdb_prompt$" {
485                 verbose "Got timeout error from gdb."
486             }
487             timeout {
488                 send_gdb "\ 3"
489                 break
490             }
491         }
492     }
493     return 1
494 }
495
496 #
497 # load a file into the debugger (file command only).
498 # return a -1 if anything goes wrong.
499 #
500 proc mi_gdb_file_cmd { arg } {
501     global loadpath
502     global loadfile
503     global GDB
504     global mi_gdb_prompt
505     global last_loaded_file
506     upvar timeout timeout
507
508     # GCC for Windows target may create foo.exe given "-o foo".
509     if { ![file exists $arg] && [file exists "$arg.exe"] } {
510         set arg "$arg.exe"
511     }
512
513     set last_loaded_file $arg
514
515     if [is_remote host] {
516         set arg [remote_download host $arg]
517         if { $arg == "" } {
518             error "download failed"
519             return -1
520         }
521     }
522
523 # FIXME: Several of these patterns are only acceptable for console
524 # output.  Queries are an error for mi.
525     send_gdb "105-file-exec-and-symbols $arg\n"
526     gdb_expect 120 {
527         -re "Reading symbols from.*$mi_gdb_prompt$" {
528             verbose "\t\tLoaded $arg into the $GDB"
529             return 0
530         }
531         -re "has no symbol-table.*$mi_gdb_prompt$" {
532             perror "$arg wasn't compiled with \"-g\""
533             return -1
534         }
535         -re "Load new symbol table from \".*\".*y or n. $" {
536             send_gdb "y\n"
537             gdb_expect 120 {
538                 -re "Reading symbols from.*$mi_gdb_prompt$" {
539                     verbose "\t\tLoaded $arg with new symbol table into $GDB"
540                     # All OK
541                 }
542                 timeout {
543                     perror "(timeout) Couldn't load $arg, other program already loaded."
544                     return -1
545                 }
546             }
547         }
548         -re "No such file or directory.*$mi_gdb_prompt$" {
549             perror "($arg) No such file or directory\n"
550             return -1
551         }
552         -re "105-file-exec-and-symbols .*\r\n105\\\^done\r\n$mi_gdb_prompt$" {
553             # We (MI) are just giving the prompt back for now, instead of giving
554             # some acknowledgement.
555             return 0
556         }
557         timeout {
558             perror "couldn't load $arg into $GDB (timed out)."
559             return -1
560         }
561         eof {
562             # This is an attempt to detect a core dump, but seems not to
563             # work.  Perhaps we need to match .* followed by eof, in which
564             # gdb_expect does not seem to have a way to do that.
565             perror "couldn't load $arg into $GDB (end of file)."
566             return -1
567         }
568     }
569 }
570
571 #
572 # connect to the target and download a file, if necessary.
573 # return a -1 if anything goes wrong.
574 #
575 proc mi_gdb_target_load { } {
576     global loadpath
577     global loadfile
578     global GDB
579     global mi_gdb_prompt
580
581     if [target_info exists gdb_load_timeout] {
582         set loadtimeout [target_info gdb_load_timeout]
583     } else {
584         set loadtimeout 1600
585     }
586
587     if { [info procs gdbserver_gdb_load] != "" } {
588         mi_gdb_test "kill" ".*" ""
589         if { [catch gdbserver_gdb_load res] == 1 } {
590             perror $res
591             return -1
592         }
593         set protocol [lindex $res 0]
594         set gdbport [lindex $res 1]
595
596         if { [mi_gdb_target_cmd $protocol $gdbport] != 0 } {
597             return -1
598         }
599     } elseif { [info procs send_target_sid] != "" } {
600         # For SID, things get complex
601         send_gdb "kill\n"
602         gdb_expect 10 {
603             -re ".*$mi_gdb_prompt$"
604         }
605         send_target_sid
606         gdb_expect $loadtimeout {
607             -re "\\^done.*$mi_gdb_prompt$" {
608             }
609             timeout {
610                 perror "Unable to connect to SID target (timeout)"
611                 return -1
612             }
613         }
614         send_gdb "48-target-download\n"
615         gdb_expect $loadtimeout {
616             -re "48\\^done.*$mi_gdb_prompt$" {
617             }
618             timeout {
619                 perror "Unable to download to SID target (timeout)"
620                 return -1
621             }
622         }
623     } elseif { [target_info protocol] == "sim" } {
624         set target_sim_options "[board_info target gdb,target_sim_options]"
625         # For the simulator, just connect to it directly.
626         send_gdb "47-target-select sim $target_sim_options\n"
627         gdb_expect $loadtimeout {
628             -re "47\\^connected.*$mi_gdb_prompt$" {
629             }
630             timeout {
631                 perror "Unable to select sim target (timeout)"
632                 return -1
633             }
634         }
635         send_gdb "48-target-download\n"
636         gdb_expect $loadtimeout {
637             -re "48\\^done.*$mi_gdb_prompt$" {
638             }
639             timeout {
640                 perror "Unable to download to sim target (timeout)"
641                 return -1
642             }
643         }
644     } elseif { [target_info gdb_protocol] == "remote" } {
645         # remote targets
646         if { [mi_gdb_target_cmd "remote" [target_info netport]] != 0 } {
647             perror "Unable to connect to remote target"
648             return -1
649         }
650         send_gdb "48-target-download\n"
651         gdb_expect $loadtimeout {
652             -re "48\\^done.*$mi_gdb_prompt$" {
653             }
654             timeout {
655                 perror "Unable to download to remote target (timeout)"
656                 return -1
657             }
658         }
659     }
660     return 0
661 }
662
663 #
664 # load a file into the debugger.
665 # return a -1 if anything goes wrong.
666 #
667 proc mi_gdb_load { arg } {
668     if { $arg != "" } {
669         return [mi_gdb_file_cmd $arg]
670     }
671     return 0
672 }
673
674 # Return 1 if symbols were read in using -readnow.  Otherwise, return 0.
675 # Based on readnow from lib/gdb.exp.
676
677 proc mi_readnow { args } {
678     global mi_gdb_prompt
679
680     if { [llength $args] == 1 } {
681         set re [lindex $args 0]
682     } else {
683         set re ""
684     }
685
686     set readnow_p 0
687     set cmd "maint print objfiles $re"
688     send_gdb "$cmd\n"
689     gdb_expect {
690         -re ".gdb_index: faked for ..readnow.." {
691             # Record that we've seen the above pattern.
692             set readnow_p 1
693             exp_continue
694         }
695         -re "\\^done\r\n$mi_gdb_prompt$" {
696         }
697     }
698
699     return $readnow_p
700 }
701
702 # mi_gdb_test COMMAND [PATTERN [MESSAGE [IPATTERN]]] -- send a command to gdb;
703 #   test the result.
704 #
705 # COMMAND is the command to execute, send to GDB with send_gdb.  If
706 #   this is the null string no command is sent.
707 # PATTERN is the pattern to match for a PASS, and must NOT include
708 #   the \r\n sequence immediately before the gdb prompt.
709 #   If not specified, .* is used.
710 # MESSAGE is the message to be printed.  (If this is the empty string,
711 #   then sometimes we don't call pass or fail at all; I don't
712 #   understand this at all.)
713 #   If not specified, COMMAND is used.
714 # IPATTERN is the pattern to match for the inferior's output.  This parameter
715 #   is optional.  If present, it will produce a PASS if the match is
716 #   successful, and a FAIL if unsuccessful.
717 #
718 # Returns:
719 #    1 if the test failed,
720 #    0 if the test passes,
721 #   -1 if there was an internal error.
722 #
723 proc mi_gdb_test { args } {
724     global verbose
725     global mi_gdb_prompt
726     global GDB expect_out
727     global inferior_exited_re async
728     upvar timeout timeout
729
730     if {[llength $args] >= 1} {
731         set command [lindex $args 0]
732     } else {
733         error "Not enough arguments in mi_gdb_test"
734     }
735
736     if {[llength $args] >= 2} {
737         set pattern [lindex $args 1]
738     } else {
739         set pattern ".*"
740     }
741
742     if {[llength $args] >= 3} {
743         set message [lindex $args 2]
744     } else {
745         set message $command
746     }
747
748     if [llength $args]==4 {
749         set ipattern [lindex $args 3]
750     }
751
752     if [llength $args]==5 {
753         set question_string [lindex $args 3]
754         set response_string [lindex $args 4]
755     } else {
756         set question_string "^FOOBAR$"
757     }
758
759     if { [llength $args] >= 6 } {
760         error "Too many arguments in mi_gdb_test"
761     }
762
763     if {$verbose > 2} {
764         send_user "Sending \"$command\" to gdb\n"
765         send_user "Looking to match \"$pattern\"\n"
766         send_user "Message is \"$message\"\n"
767     }
768
769     set result -1
770     set string "${command}\n"
771     set string_regex [string_to_regexp $command]
772
773     if { $command != "" } {
774         while { "$string" != "" } {
775             set foo [string first "\n" "$string"]
776             set len [string length "$string"]
777             if { $foo < [expr $len - 1] } {
778                 set str [string range "$string" 0 $foo]
779                 if { [send_gdb "$str"] != "" } {
780                     perror "Couldn't send $command to GDB."
781                 }
782                 gdb_expect 2 {
783                     -re "\[\r\n\]" { }
784                     timeout { }
785                 }
786                 set string [string range "$string" [expr $foo + 1] end]
787             } else {
788                 break
789             }
790         }
791         if { "$string" != "" } {
792             if { [send_gdb "$string"] != "" } {
793                 perror "Couldn't send $command to GDB."
794             }
795         }
796     }
797
798     if [info exists timeout] {
799         set tmt $timeout
800     } else {
801         global timeout
802         if [info exists timeout] {
803             set tmt $timeout
804         } else {
805             set tmt 60
806         }
807     }
808     if {$async} {
809         # With $prompt_re "" there may come arbitrary asynchronous response
810         # from the previous command, before or after $string_regex.
811         set string_regex ".*"
812     }
813     verbose -log "Expecting: ^($string_regex\[\r\n\]+)?($pattern\[\r\n\]+$mi_gdb_prompt\[ \]*)"
814     gdb_expect $tmt {
815          -re "\\*\\*\\* DOSEXIT code.*" {
816              if { $message != "" } {
817                  fail "$message"
818              }
819              return -1
820          }
821          -re "Ending remote debugging.*$mi_gdb_prompt\[ \]*$" {
822             if {![isnative]} {
823                 warning "Can`t communicate to remote target."
824             }
825             gdb_exit
826             gdb_start
827             set result -1
828         }
829          -re "^($string_regex\[\r\n\]+)?($pattern\[\r\n\]+$mi_gdb_prompt\[ \]*)" {
830             # At this point, $expect_out(1,string) is the MI input command.
831             # and $expect_out(2,string) is the MI output command.
832             # If $expect_out(1,string) is "", then there was no MI input command here.
833
834             # NOTE, there is no trailing anchor because with GDB/MI, 
835             # asynchronous responses can happen at any point, causing more 
836             # data to be available.  Normally an anchor is used to make 
837             # sure the end of the output is matched, however, $mi_gdb_prompt 
838             # is just as good of an anchor since mi_gdb_test is meant to 
839             # match a single mi output command.  If a second GDB/MI output 
840             # response is sent, it will be in the buffer for the next 
841             # time mi_gdb_test is called.
842             if {![string match "" $message]} {
843                 pass "$message"
844             }
845             set result 0
846         }
847          -re "(${question_string})$" {
848             send_gdb "$response_string\n"
849             exp_continue
850         }
851          -re "Undefined.* command:.*$mi_gdb_prompt\[ \]*$" {
852             perror "Undefined command \"$command\"."
853              fail "$message"
854             set result 1
855         }
856          -re "Ambiguous command.*$mi_gdb_prompt\[ \]*$" {
857             perror "\"$command\" is not a unique command name."
858              fail "$message"
859             set result 1
860         }
861          -re "$inferior_exited_re with code \[0-9\]+.*$mi_gdb_prompt\[ \]*$" {
862             if {![string match "" $message]} {
863                 set errmsg "$message (the program exited)"
864             } else {
865                 set errmsg "$command (the program exited)"
866             }
867             fail "$errmsg"
868             return -1
869         }
870          -re "The program is not being run.*$mi_gdb_prompt\[ \]*$" {
871             if {![string match "" $message]} {
872                 set errmsg "$message (the program is no longer running)"
873             } else {
874                 set errmsg "$command (the program is no longer running)"
875             }
876             fail "$errmsg"
877             return -1
878         }
879          -re ".*$mi_gdb_prompt\[ \]*$" {
880             if {![string match "" $message]} {
881                 fail "$message (unexpected output)"
882             }
883             set result 1
884         }
885          "<return>" {
886             send_gdb "\n"
887             perror "Window too small."
888              fail "$message"
889         }
890          eof {
891              perror "Process no longer exists"
892              if { $message != "" } {
893                  fail "$message"
894              }
895              return -1
896         }
897          full_buffer {
898             perror "internal buffer is full."
899              fail "$message"
900         }
901         timeout {
902             if {![string match "" $message]} {
903                 fail "$message (timeout)"
904             }
905             set result 1
906         }
907     }
908
909     # If the GDB output matched, compare the inferior output.
910     if { $result == 0 } {
911         if [ info exists ipattern ] {
912             if { ![target_info exists gdb,noinferiorio] } {
913                 global gdb_spawn_id inferior_spawn_id
914
915                 set sid "$inferior_spawn_id $gdb_spawn_id"
916                 gdb_expect {
917                     -i "$sid" -re "$ipattern" {
918                         pass "$message inferior output"
919                     }
920                     timeout {
921                         fail "$message inferior output (timeout)"
922                         set result 1
923                     }
924                 }
925             } else {
926                 unsupported "$message inferior output"
927             }
928         }
929     }
930
931     return $result
932 }
933
934 # Collect output sent to the console output stream until UNTIL is
935 # seen.  UNTIL is a regular expression.  MESSAGE is the message to be
936 # printed in case of timeout.
937
938 proc mi_gdb_expect_cli_output {until message} {
939
940     set output ""
941     gdb_expect {
942         -re "~\"(\[^\r\n\]+)\"\r\n" {
943             append output $expect_out(1,string)
944             exp_continue
945         }
946         -notransfer -re "$until" {
947             # Done
948         }
949         timeout {
950             fail "$message (timeout)"
951             return ""
952         }
953     }
954
955     return $output
956 }
957
958 #
959 # MI run command.  (A modified version of gdb_run_cmd)
960 #
961
962 # In patterns, the newline sequence ``\r\n'' is matched explicitly as
963 # ``.*$'' could swallow up output that we attempt to match elsewhere.
964
965 # Send the command to run the test program.
966 #
967 # If USE_MI_COMMAND is true, the "-exec-run" command is used.
968 # Otherwise, the "run" (CLI) command is used.  If the global USE_GDB_STUB is
969 # true, -exec-continue and continue are used instead of their run counterparts.
970 #
971 # ARGS is passed as argument to the command used to run the test program.
972 # Beware that arguments to "-exec-run" do not have the same semantics as
973 # arguments to the "run" command, so USE_MI_COMMAND influences the meaning
974 # of ARGS.  If USE_MI_COMMAND is true, they are arguments to -exec-run.
975 # If USE_MI_COMMAND is false, they are effectively arguments passed
976 # to the test program.  If the global USE_GDB_STUB is true, ARGS is not used.
977 proc mi_run_cmd_full {use_mi_command args} {
978     global mi_gdb_prompt use_gdb_stub
979     global thread_selected_re
980     global library_loaded_re
981
982     if {$use_mi_command} {
983         set run_prefix "220-exec-"
984         set run_match "220"
985     } else {
986         set run_prefix ""
987         set run_match ""
988     }
989
990     foreach command [gdb_init_commands] {
991         send_gdb "$command\n"
992         gdb_expect 30 {
993             -re "$mi_gdb_prompt$" { }
994             default {
995                 unresolved "gdb_init_command for target failed"
996                 return -1
997             }
998         }
999     }
1000
1001     if { [mi_gdb_target_load] < 0 } {
1002         return -1
1003     }
1004
1005     if $use_gdb_stub {
1006         if [target_info exists gdb,do_reload_on_run] {
1007             send_gdb "${run_prefix}continue\n"
1008             gdb_expect 60 {
1009                 -re "${run_match}\\^running\[\r\n\]+\\*running,thread-id=\"\[^\"\]+\"\r\n$mi_gdb_prompt" {}
1010                 -re "${run_match}\\^error.*$mi_gdb_prompt" {return -1}
1011                 default {}
1012             }
1013             return 0
1014         }
1015
1016         if [target_info exists gdb,start_symbol] {
1017             set start [target_info gdb,start_symbol]
1018         } else {
1019             set start "start"
1020         }
1021
1022         # HACK: Should either use 000-jump or fix the target code
1023         # to better handle RUN.
1024         send_gdb  "jump *$start\n"
1025         warning "Using CLI jump command, expect run-to-main FAIL"
1026         gdb_expect {
1027             -re "&\"jump \\*${start}\\\\n\"\[\r\n\]+~\"Continuing at 0x\[0-9A-Fa-f\]+\.\\\\n\"\[\r\n\]+\\^running\[\r\n\]+\\*running,thread-id=\"\[^\"\]+\"\[\r\n\]+${mi_gdb_prompt}" {}
1028             timeout {
1029                 unresolved "unable to start target"
1030                 return -1
1031             }
1032         }
1033         return 0
1034     }
1035
1036     send_gdb "${run_prefix}run $args\n"
1037     gdb_expect {
1038         -re "${run_match}\\^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}" {
1039         }
1040         -re "\\^error,msg=\"The target does not support running in non-stop mode.\"" {
1041             unsupported "non-stop mode not supported"
1042             return -1
1043         }
1044         timeout {
1045             unresolved "unable to start target"
1046             return -1
1047         }
1048     }
1049     # NOTE: Shortly after this there will be a ``000*stopped,...(gdb)''
1050
1051     return 0
1052 }
1053
1054 # A wrapper for mi_run_cmd_full which uses -exec-run and
1055 # -exec-continue, as appropriate.  ARGS are passed verbatim to
1056 # mi_run_cmd_full.
1057 proc mi_run_cmd {args} {
1058     return [eval mi_run_cmd_full 1 $args]
1059 }
1060
1061 # A wrapper for mi_run_cmd_full which uses the CLI commands 'run' and
1062 # 'continue', as appropriate.  ARGS are passed verbatim to
1063 # mi_run_cmd_full.
1064 proc mi_run_with_cli {args} {
1065     return [eval mi_run_cmd_full 0 $args]
1066 }
1067
1068 # Starts fresh GDB binary and loads an optional executable into GDB.
1069 # Usage: mi_clean_restart [executable]
1070 # EXECUTABLE is the basename of the binary.
1071 # Return -1 if starting gdb or loading the executable failed.
1072
1073 proc mi_clean_restart { args } {
1074     global srcdir
1075     global subdir
1076     global errcnt
1077     global warncnt
1078
1079     if { [llength $args] > 1 } {
1080         error "bad number of args: [llength $args]"
1081     }
1082
1083     gdb_exit
1084
1085     # This is a clean restart, so reset error and warning count.
1086     set errcnt 0
1087     set warncnt 0
1088
1089     if {[mi_gdb_start]} {
1090         return -1
1091     }
1092
1093     mi_gdb_reinitialize_dir $srcdir/$subdir
1094
1095     if { [llength $args] >= 1 } {
1096         set executable [lindex $args 0]
1097         set binfile [standard_output_file ${executable}]
1098         return [mi_gdb_load ${binfile}]
1099     }
1100
1101     return 0
1102 }
1103
1104 # Just like gdb's "runto" proc, it will run the target to a given
1105 # function.  The big difference here between mi_runto and mi_execute_to
1106 # is that mi_execute_to must have the inferior running already.  This
1107 # proc will (like gdb's runto) (re)start the inferior, too.
1108 #
1109 # FUNC is the linespec of the place to stop (it inserts a breakpoint here).
1110 # It returns:
1111 #   -1  if failed, timedout
1112 #    0  if test passed
1113 #
1114 # Supported options:
1115 #
1116 #  -qualified -- pass --qualified to -break-insert
1117 #  -pending   -- pass -f to -break-insert to create a pending
1118 #                breakpoint.
1119
1120 proc mi_runto_helper {func run_or_continue args} {
1121   global mi_gdb_prompt expect_out
1122   global hex decimal fullname_syntax
1123
1124   parse_args {{qualified} {pending}}
1125
1126   set test "mi runto $func"
1127   if {$pending} {
1128       set bp [mi_make_breakpoint_pending -type breakpoint -disp del]
1129   } else {
1130       set bp [mi_make_breakpoint -type breakpoint -disp del \
1131                   -func $func\(\\\(.*\\\)\)?]
1132   }
1133   set extra_opts ""
1134   set extra_output ""
1135   if {$qualified} {
1136       lappend extra_opts "--qualified"
1137   }
1138   if {$pending} {
1139       lappend extra_opts "-f"
1140       # MI prints "Function FUNC not defined", "No line NNN in current
1141       # file.", etc. to the CLI stream.
1142       set extra_output "&\"\[^\r\n\]+\"\r\n"
1143   }
1144   mi_gdb_test "200-break-insert [join $extra_opts " "] -t $func" "${extra_output}200\\^done,$bp" \
1145       "breakpoint at $func"
1146
1147   if {$run_or_continue == "run"} {
1148       if { [mi_run_cmd] < 0 } {
1149           return -1
1150       }
1151   } else {
1152       mi_send_resuming_command "exec-continue" "$test"
1153   }
1154
1155   mi_expect_stop "breakpoint-hit" $func ".*" ".*" "\[0-9\]+" { "" "disp=\"del\"" } $test
1156 }
1157
1158 proc mi_runto {func args} {
1159     return [mi_runto_helper $func "run" {*}$args]
1160 }
1161
1162 # Just like runto_main but works with the MI interface.
1163
1164 proc mi_runto_main {} {
1165     return [mi_runto_helper "main" "run" -qualified]
1166 }
1167
1168 # Next to the next statement
1169 # For return values, see mi_execute_to_helper
1170
1171 proc mi_next { test } {
1172   return [mi_next_to {.*} {.*} {.*} {.*} $test]
1173 }
1174
1175
1176 # Step to the next statement
1177 # For return values, see mi_execute_to_helper
1178
1179 proc mi_step { test } {
1180   return [mi_step_to {.*} {.*} {.*} {.*} $test]
1181 }
1182
1183 set async "unknown"
1184
1185 proc mi_detect_async {} {
1186     global async
1187     global mi_gdb_prompt
1188
1189     send_gdb "show mi-async\n"
1190
1191     gdb_expect {
1192         -re "asynchronous mode is on...*$mi_gdb_prompt$" {
1193             set async 1
1194         }
1195         -re ".*$mi_gdb_prompt$" {
1196             set async 0
1197         }
1198         timeout {
1199             set async 0
1200         }
1201     }
1202     return $async
1203 }
1204
1205 # Wait for MI *stopped notification to appear.
1206 # The REASON, FUNC, ARGS, FILE and LINE are regular expressions
1207 # to match against whatever is output in *stopped.  FILE may also match
1208 # filename of a file without debug info.  ARGS should not include [] the
1209 # list of argument is enclosed in, and other regular expressions should
1210 # not include quotes.
1211 # EXTRA can be a list of one, two or three elements.
1212 # The first element is the regular expression
1213 # for output expected right after *stopped, and before GDB prompt.
1214 # The third element is the regular expression for the locno
1215 # right after bkptno field.  The locno regex should not include
1216 # the comma separating it from the following fields.
1217 #
1218 # When we fail to match output at all, -1 is returned.  If FILE does
1219 # match and the target system has no debug info for FILE return 0.
1220 # Otherwise, the line at which we stop is returned.  This is useful when
1221 # exact line is not possible to specify for some reason -- one can pass
1222 # the .* or "\[0-9\]*" regexps for line, and then check the line
1223 # programmatically.
1224 #
1225 # Do not pass .* for any argument if you are expecting more than one stop.
1226 proc mi_expect_stop { reason func args file line extra test } {
1227
1228     global mi_gdb_prompt
1229     global hex
1230     global decimal
1231     global fullname_syntax
1232     global async
1233     global thread_selected_re
1234     global breakpoint_re
1235
1236     set any "\[^\n\]*"
1237
1238     set after_stopped ""
1239     set after_reason ""
1240     set locno ""
1241     if { [llength $extra] == 3 } {
1242         set after_stopped [lindex $extra 0]
1243         set after_reason [lindex $extra 1]
1244         set after_reason "${after_reason},"
1245         set locno [lindex $extra 2]
1246         set locno "${locno},"
1247     } elseif { [llength $extra] == 2 } {
1248         set after_stopped [lindex $extra 0]
1249         set after_reason [lindex $extra 1]
1250         set after_reason "${after_reason},"
1251     } elseif { [llength $extra] == 1 } {
1252         set after_stopped [lindex $extra 0]
1253     }
1254
1255     if {$async} {
1256         set prompt_re ""
1257     } else {
1258         set prompt_re "$mi_gdb_prompt$"
1259     }
1260
1261     if { $reason == "really-no-reason" } {
1262         gdb_expect {
1263             -re "\\*stopped\r\n$prompt_re" {
1264                 pass "$test"
1265             }
1266             timeout {
1267                 fail "$test (timeout)"
1268             }
1269         }
1270         return
1271     }
1272
1273     if { $reason == "exited-normally" } {
1274
1275         gdb_expect {
1276             -re "\\*stopped,reason=\"exited-normally\"\r\n$prompt_re" {
1277                 pass "$test"
1278             }
1279             -re ".*$mi_gdb_prompt$" {fail "continue to end (2)"}
1280             timeout {
1281                 fail "$test (timeout)"
1282             }
1283         }
1284         return
1285     }
1286     if { $reason == "exited" } {
1287         gdb_expect {
1288             -re "\\*stopped,reason=\"exited\",exit-code=\"\[0-7\]+\"\r\n$prompt_re" {
1289                 pass "$test"
1290             }
1291             -re ".*$mi_gdb_prompt$" {
1292                 fail "$test (inferior not stopped)"
1293             }
1294             timeout {
1295                 fail "$test (timeout)"
1296             }
1297         }
1298         return
1299     }
1300
1301     if { $reason == "solib-event" } {
1302         set pattern "\\*stopped,reason=\"solib-event\",thread-id=\"$decimal\",stopped-threads=$any\r\n($thread_selected_re|$breakpoint_re)*$prompt_re"
1303         verbose -log "mi_expect_stop: expecting: $pattern"
1304         gdb_expect {
1305             -re "$pattern" {
1306                 pass "$test"
1307             }
1308             timeout {
1309                 fail "$test (timeout)"
1310             }
1311         }
1312         return
1313     }
1314
1315     set args "\\\[$args\\\]"
1316
1317     set bn ""
1318     set ebn ""
1319     if { $reason == "breakpoint-hit" } {
1320         set bn {bkptno="[0-9]+",}
1321         set bn "${bn}${locno}"
1322     } elseif { $reason == "solib-event" } {
1323         set bn ".*"
1324     } elseif { $reason == "exception-caught" } {
1325         set ebn {bkptno="[0-9]+",}
1326         set ebn "${ebn}${locno}"
1327         set bn ".*"
1328         set reason "breakpoint-hit"
1329     }
1330
1331     set r ""
1332     if { $reason != "" } {
1333         set r "reason=\"$reason\","
1334     }
1335
1336
1337     set a $after_reason
1338
1339     verbose -log "mi_expect_stop: expecting: \\*stopped,${ebn}${r}${a}${bn}frame=\{addr=\"$hex\",func=\"$func\",args=$args,(?:file=\"$any$file\",fullname=\"${fullname_syntax}$file\",line=\"$line\",arch=\"$any\"|from=\"$file\")\}$after_stopped,thread-id=\"$decimal\",stopped-threads=$any\r\n($thread_selected_re|$breakpoint_re)*$prompt_re"
1340
1341     gdb_expect {
1342         -re "\\*stopped,${ebn}${r}${a}${bn}frame=\{addr=\"$hex\",func=\"$func\",args=$args,(?:file=\"$any$file\",fullname=\"${fullname_syntax}$file\",line=\"($line)\",arch=\"$any\"|from=\"$file\")\}$after_stopped,thread-id=\"$decimal\",stopped-threads=$any\r\n($thread_selected_re|$breakpoint_re)*$prompt_re" {
1343             pass "$test"
1344             if {[array names expect_out "2,string"] != ""} {
1345                 return $expect_out(2,string)
1346             }
1347             # No debug info available but $file does match.
1348             return 0
1349         }
1350         -re "\\*stopped,${ebn}${r}${a}${bn}frame=\{addr=\"$hex\",func=\"$any\",args=\[\\\[\{\]$any\[\\\]\}\],file=\"$any\",fullname=\"${fullname_syntax}$any\",line=\"\[0-9\]*\",arch=\"$any\"\}$after_stopped,thread-id=\"$decimal\",stopped-threads=$any\r\n($thread_selected_re|$breakpoint_re)*$prompt_re" {
1351             verbose -log "got $expect_out(buffer)"
1352             fail "$test (stopped at wrong place)"
1353             return -1
1354         }
1355         -re ".*\r\n$mi_gdb_prompt$" {
1356             verbose -log "got $expect_out(buffer)"
1357             fail "$test (unknown output after running)"
1358             return -1
1359         }
1360         timeout {
1361             fail "$test (timeout)"
1362             return -1
1363         }
1364     }
1365 }
1366
1367 # Wait for MI *stopped notification related to an interrupt request to
1368 # appear.
1369 proc mi_expect_interrupt { test } {
1370     global mi_gdb_prompt
1371     global decimal
1372     global async
1373
1374     if {$async} {
1375         set prompt_re ""
1376     } else {
1377         set prompt_re "$mi_gdb_prompt"
1378     }
1379
1380     set r_nonstop "reason=\"signal-received\",signal-name=\"0\",signal-meaning=\"Signal 0\""
1381     set r_allstop "reason=\"signal-received\",signal-name=\"SIGINT\",signal-meaning=\"Interrupt\""
1382     set r "(${r_nonstop}|${r_allstop})"
1383     set any "\[^\n\]*"
1384
1385     # A signal can land anywhere, just ignore the location
1386     verbose -log "mi_expect_interrupt: expecting: \\*stopped,${r}$any\r\n$prompt_re"
1387     gdb_expect {
1388         -re "\\*stopped,${r}$any\r\n$prompt_re" {
1389             pass "$test"
1390             return 0
1391         }
1392         -re ".*\r\n$mi_gdb_prompt" {
1393             verbose -log "got $expect_out(buffer)"
1394             fail "$test (unknown output after running)"
1395             return -1
1396         }
1397         timeout {
1398             fail "$test (timeout)"
1399             return -1
1400         }
1401     }
1402 }
1403
1404 # cmd should not include the number or newline (i.e. "exec-step 3", not
1405 # "220-exec-step 3\n"
1406
1407 # Can not match -re ".*\r\n${mi_gdb_prompt}", because of false positives
1408 # after the first prompt is printed.
1409
1410 proc mi_execute_to { cmd reason func args file line extra test } {
1411     mi_send_resuming_command "$cmd" "$test"
1412     set r [mi_expect_stop $reason $func $args $file $line $extra $test]
1413     return $r
1414 }
1415
1416 proc mi_next_to { func args file line test } {
1417     mi_execute_to "exec-next" "end-stepping-range" "$func" "$args" \
1418         "$file" "$line" "" "$test"
1419 }
1420
1421 proc mi_step_to { func args file line test } {
1422     mi_execute_to "exec-step" "end-stepping-range" "$func" "$args" \
1423         "$file" "$line" "" "$test"
1424 }
1425
1426 proc mi_finish_to { func args file line result ret test } {
1427     mi_execute_to "exec-finish" "function-finished" "$func" "$args" \
1428         "$file" "$line" \
1429         ",gdb-result-var=\"$result\",return-value=\"$ret\"" \
1430         "$test"
1431 }
1432
1433 proc mi_continue_to {func} {
1434     mi_runto_helper $func "continue"
1435 }
1436
1437 proc mi0_execute_to { cmd reason func args file line extra test } {
1438     mi_execute_to_helper "$cmd" "$reason" "$func" "\{$args\}" \
1439         "$file" "$line" "$extra" "$test"
1440 }
1441
1442 proc mi0_next_to { func args file line test } {
1443     mi0_execute_to "exec-next" "end-stepping-range" "$func" "$args" \
1444         "$file" "$line" "" "$test"
1445 }
1446
1447 proc mi0_step_to { func args file line test } {
1448     mi0_execute_to "exec-step" "end-stepping-range" "$func" "$args" \
1449         "$file" "$line" "" "$test"
1450 }
1451
1452 proc mi0_finish_to { func args file line result ret test } {
1453     mi0_execute_to "exec-finish" "function-finished" "$func" "$args" \
1454         "$file" "$line" \
1455         ",gdb-result-var=\"$result\",return-value=\"$ret\"" \
1456         "$test"
1457 }
1458
1459 proc mi0_continue_to { bkptno func args file line test } {
1460     mi0_execute_to "exec-continue" "breakpoint-hit\",bkptno=\"$bkptno" \
1461         "$func" "$args" "$file" "$line" "" "$test"
1462 }
1463
1464 # Creates a breakpoint and checks the reported fields are as expected.
1465 # This procedure takes the same options as mi_make_breakpoint and
1466 # returns the breakpoint regexp from that procedure.
1467
1468 proc mi_create_breakpoint {location test args} {
1469     set bp [eval mi_make_breakpoint $args]
1470     mi_gdb_test "222-break-insert $location" "222\\^done,$bp" $test
1471     return $bp
1472 }
1473
1474 # Like mi_create_breakpoint, but creates a breakpoint with multiple
1475 # locations using mi_make_breakpoint_multi instead.
1476
1477 proc mi_create_breakpoint_multi {location test args} {
1478     set bp [eval mi_make_breakpoint_multi $args]
1479     mi_gdb_test "222-break-insert $location" "222\\^done,$bp" $test
1480     return $bp
1481 }
1482
1483 # Creates varobj named NAME for EXPRESSION.
1484 # Name cannot be "-".
1485 proc mi_create_varobj { name expression testname } {
1486     mi_gdb_test "-var-create $name * $expression" \
1487         "\\^done,name=\"$name\",numchild=\"\[0-9\]+\",value=\".*\",type=.*,has_more=\"0\"" \
1488         $testname
1489 }
1490
1491 proc mi_create_floating_varobj { name expression testname } {
1492     mi_gdb_test "-var-create $name @ $expression" \
1493         "\\^done,name=\"$name\",numchild=\"\(-1\|\[0-9\]+\)\",value=\".*\",type=.*" \
1494         $testname
1495 }
1496
1497
1498 # Same as mi_create_varobj, but also checks the reported type
1499 # of the varobj.
1500 proc mi_create_varobj_checked { name expression type testname } {
1501     mi_gdb_test "-var-create $name * $expression" \
1502         "\\^done,name=\"$name\",numchild=\"\[0-9\]+\",value=\".*\",type=\"$type\".*" \
1503         $testname
1504 }
1505
1506 # Same as mi_create_floating_varobj, but assumes the test is creating
1507 # a dynamic varobj that has children, so the value must be "{...}".
1508 # The "has_more" attribute is checked.
1509 proc mi_create_dynamic_varobj {name expression has_more testname} {
1510     mi_gdb_test "-var-create $name @ $expression" \
1511         "\\^done,name=\"$name\",numchild=\"0\",value=\"{\\.\\.\\.}\",type=.*,has_more=\"${has_more}\"" \
1512         $testname
1513 }
1514
1515 # Deletes the specified NAME.
1516 proc mi_delete_varobj { name testname } {
1517     mi_gdb_test "-var-delete $name" \
1518         "\\^done,ndeleted=.*" \
1519         $testname
1520 }
1521
1522 # Updates varobj named NAME and checks that all varobjs in EXPECTED
1523 # are reported as updated, and no other varobj is updated.
1524 # Assumes that no varobj is out of scope and that no varobj changes
1525 # types.
1526 proc mi_varobj_update { name expected testname } {
1527     set er "\\^done,changelist=\\\["
1528     set first 1
1529     foreach item $expected {
1530         set v "{name=\"$item\",in_scope=\"true\",type_changed=\"false\",has_more=\".\"}"
1531         if {$first == 1} {
1532             set er "$er$v"
1533             set first 0
1534         } else {
1535             set er "$er,$v"
1536         }
1537     }
1538     set er "$er\\\]"
1539
1540     verbose -log "Expecting: $er" 2
1541     mi_gdb_test "-var-update $name" $er $testname
1542 }
1543
1544 proc mi_varobj_update_with_child_type_change { name child_name new_type new_children testname } {
1545     set v "{name=\"$child_name\",in_scope=\"true\",type_changed=\"true\",new_type=\"$new_type\",new_num_children=\"$new_children\",has_more=\".\"}"
1546     set er "\\^done,changelist=\\\[$v\\\]"
1547     verbose -log "Expecting: $er"
1548     mi_gdb_test "-var-update $name" $er $testname
1549 }
1550
1551 proc mi_varobj_update_with_type_change { name new_type new_children testname } {
1552     mi_varobj_update_with_child_type_change $name $name $new_type $new_children $testname
1553 }
1554
1555 # A helper that turns a key/value list into a regular expression
1556 # matching some MI output.
1557 proc mi_varobj_update_kv_helper {list} {
1558     set first 1
1559     set rx ""
1560     foreach {key value} $list {
1561         if {!$first} {
1562             append rx ,
1563         }
1564         set first 0
1565         if {$key == "new_children"} {
1566             append rx "$key=\\\[$value\\\]"
1567         } else {
1568             append rx "$key=\"$value\""
1569         }
1570     }
1571     return $rx
1572 }
1573
1574 # A helper for mi_varobj_update_dynamic that computes a match
1575 # expression given a child list.
1576 proc mi_varobj_update_dynamic_helper {children} {
1577     set crx ""
1578
1579     set first 1
1580     foreach child $children {
1581         if {!$first} {
1582             append crx ,
1583         }
1584         set first 0
1585         append crx "{"
1586         append crx [mi_varobj_update_kv_helper $child]
1587         append crx "}"
1588     }
1589
1590     return $crx
1591 }
1592
1593 # Update a dynamic varobj named NAME.  CHILDREN is a list of children
1594 # that have been updated; NEW_CHILDREN is a list of children that were
1595 # added to the primary varobj.  Each child is a list of key/value
1596 # pairs that are expected.  SELF is a key/value list holding
1597 # information about the varobj itself.  TESTNAME is the name of the
1598 # test.
1599 proc mi_varobj_update_dynamic {name testname self children new_children} {
1600     if {[llength $new_children]} {
1601         set newrx [mi_varobj_update_dynamic_helper $new_children]
1602         lappend self new_children $newrx
1603     }
1604     set selfrx [mi_varobj_update_kv_helper $self]
1605     set crx [mi_varobj_update_dynamic_helper $children]
1606
1607     set er "\\^done,changelist=\\\[\{name=\"$name\",in_scope=\"true\""
1608     append er ",$selfrx\}"
1609     if {"$crx" != ""} {
1610         append er ",$crx"
1611     }
1612     append er "\\\]"
1613
1614     verbose -log "Expecting: $er"
1615     mi_gdb_test "-var-update $name" $er $testname
1616 }
1617
1618 proc mi_check_varobj_value { name value testname } {
1619
1620     mi_gdb_test "-var-evaluate-expression $name" \
1621         "\\^done,value=\"$value\"" \
1622         $testname
1623 }
1624
1625 # Helper proc which constructs a child regexp for
1626 # mi_list_varobj_children and mi_varobj_update_dynamic.
1627 proc mi_child_regexp {children add_child} {
1628     set children_exp {}
1629
1630     if {$add_child} {
1631         set pre "child="
1632     } else {
1633         set pre ""
1634     }
1635
1636     foreach item $children {
1637
1638         set name [lindex $item 0]
1639         set exp [lindex $item  1]
1640         set numchild [lindex $item 2]
1641         if {[llength $item] == 5} {
1642             set type [lindex $item 3]
1643             set value [lindex $item 4]
1644
1645             lappend children_exp\
1646                 "$pre{name=\"$name\",exp=\"$exp\",numchild=\"$numchild\",value=\"$value\",type=\"$type\"(,thread-id=\"\[0-9\]+\")?}"
1647         } elseif {[llength $item] == 4} {
1648             set type [lindex $item 3]
1649
1650             lappend children_exp\
1651                 "$pre{name=\"$name\",exp=\"$exp\",numchild=\"$numchild\",type=\"$type\"(,thread-id=\"\[0-9\]+\")?}"
1652         } else {
1653             lappend children_exp\
1654                 "$pre{name=\"$name\",exp=\"$exp\",numchild=\"$numchild\"(,thread-id=\"\[0-9\]+\")?}"
1655         }
1656     }
1657     return [join $children_exp ","]
1658 }
1659
1660 # Check the results of the:
1661 #
1662 #   -var-list-children VARNAME
1663 #
1664 # command.  The CHILDREN parement should be a list of lists.
1665 # Each inner list can have either 3 or 4 elements, describing
1666 # fields that gdb is expected to report for child variable object,
1667 # in the following order
1668 #
1669 #   - Name
1670 #   - Expression
1671 #   - Number of children
1672 #   - Type
1673 #
1674 # If inner list has 3 elements, the gdb is expected to output no
1675 # type for a child and no value.
1676 #
1677 # If the inner list has 4 elements, gdb output is expected to
1678 # have no value.
1679 #
1680 proc mi_list_varobj_children { varname children testname } {
1681     mi_list_varobj_children_range $varname "" "" [llength $children] $children \
1682       $testname
1683 }
1684
1685 # Like mi_list_varobj_children, but sets a subrange.  NUMCHILDREN is
1686 # the total number of children.
1687 proc mi_list_varobj_children_range {varname from to numchildren children testname} {
1688     set options ""
1689     if {[llength $varname] == 2} {
1690         set options [lindex $varname 1]
1691         set varname [lindex $varname 0]
1692     }
1693
1694     set children_exp_j [mi_child_regexp $children 1]
1695     if {$numchildren} {
1696         set expected "\\^done,numchild=\".*\",children=\\\[$children_exp_j.*\\\]"
1697     } {
1698         set expected "\\^done,numchild=\"0\""
1699     }
1700
1701     if {"$to" == ""} {
1702         append expected ",has_more=\"0\""
1703     } elseif {$to >= 0 && $numchildren > $to} {
1704         append expected ",has_more=\"1\""
1705     } else {
1706         append expected ",has_more=\"0\""
1707     }
1708
1709     verbose -log "Expecting: $expected"
1710
1711     mi_gdb_test "-var-list-children $options $varname $from $to" \
1712       $expected $testname
1713 }
1714
1715 # Verifies that variable object VARNAME has NUMBER children,
1716 # where each one is named $VARNAME.<index-of-child> and has type TYPE.
1717 proc mi_list_array_varobj_children { varname number type testname } {
1718     mi_list_array_varobj_children_with_index $varname $number 0 $type $testname
1719 }
1720
1721 # Same as mi_list_array_varobj_children, but allowing to pass a start index
1722 # for an array.
1723 proc mi_list_array_varobj_children_with_index { varname number start_index \
1724   type testname } {
1725     set t {}
1726     set index $start_index
1727     for {set i 0} {$i < $number} {incr i} {
1728         lappend t [list $varname.$index $index 0 $type]
1729         incr index
1730     }
1731     mi_list_varobj_children $varname $t $testname
1732 }
1733
1734 # A list of two-element lists.  First element of each list is
1735 # a Tcl statement, and the second element is the line
1736 # number of source C file where the statement originates.
1737 set mi_autotest_data ""
1738 # The name of the source file for autotesting.
1739 set mi_autotest_source ""
1740
1741 # Prepares for running inline tests in FILENAME.
1742 # See comments for mi_run_inline_test for detailed
1743 # explanation of the idea and syntax.
1744 proc mi_prepare_inline_tests { filename } {
1745
1746     global srcdir
1747     global subdir
1748     global mi_autotest_source
1749     global mi_autotest_data
1750
1751     set mi_autotest_data {}
1752
1753     set mi_autotest_source $filename
1754
1755     if {![regexp "^/" "$filename"]} {
1756         set filename "$srcdir/$subdir/$filename"
1757     }
1758
1759     set chan [open $filename]
1760     set content [read $chan]
1761     set line_number 1
1762     while {1} {
1763         set start [string first "/*:" $content]
1764         if {$start != -1} {
1765             set end [string first ":*/" $content]
1766             if {$end == -1} {
1767                 error "Unterminated special comment in $filename"
1768             }
1769
1770             set prefix [string range $content 0 $start]
1771             set prefix_newlines [count_newlines $prefix]
1772
1773             set line_number [expr $line_number+$prefix_newlines]
1774             set comment_line $line_number
1775
1776             set comment [string range $content [expr $start+3] [expr $end-1]]
1777
1778             set comment_newlines [count_newlines $comment]
1779             set line_number [expr $line_number+$comment_newlines]
1780
1781             set comment [string trim $comment]
1782             set content [string range $content [expr $end+3] \
1783                              [string length $content]]
1784             lappend mi_autotest_data [list $comment $comment_line]
1785         } else {
1786             break
1787         }
1788     }
1789     close $chan
1790 }
1791
1792 # Helper to mi_run_inline_test below.
1793 # Return the list of all (statement,line_number) lists
1794 # that comprise TESTCASE.  The begin and end markers
1795 # are not included.
1796 proc mi_get_inline_test {testcase} {
1797
1798     global mi_gdb_prompt
1799     global mi_autotest_data
1800     global mi_autotest_source
1801
1802     set result {}
1803
1804     set seen_begin 0
1805     set seen_end 0
1806     foreach l $mi_autotest_data {
1807
1808         set comment [lindex $l 0]
1809
1810         if {$comment == "BEGIN: $testcase"} {
1811             set seen_begin 1
1812         } elseif {$comment == "END: $testcase"} {
1813             set seen_end 1
1814             break
1815         } elseif {$seen_begin==1} {
1816             lappend result $l
1817         }
1818     }
1819
1820     if {$seen_begin == 0} {
1821         error "Autotest $testcase not found"
1822     }
1823
1824     if {$seen_begin == 1 && $seen_end == 0} {
1825         error "Missing end marker for test $testcase"
1826     }
1827
1828     return $result
1829 }
1830
1831 # Sets temporary breakpoint at LOCATION.
1832 proc mi_tbreak {location test} {
1833
1834     global mi_gdb_prompt
1835
1836     mi_gdb_test "-break-insert -t $location" \
1837         {\^done,bkpt=.*} \
1838         $test
1839 }
1840
1841 # Send COMMAND that must be a command that resumes
1842 # the inferior (run/continue/next/etc) and consumes
1843 # the "^running" output from it.
1844 proc mi_send_resuming_command_raw {command test} {
1845
1846     global mi_gdb_prompt
1847     global thread_selected_re
1848     global library_loaded_re
1849
1850     send_gdb "$command\n"
1851     gdb_expect {
1852         -re "\\^running\r\n\\*running,thread-id=\"\[^\"\]+\"\r\n($library_loaded_re)*($thread_selected_re)?${mi_gdb_prompt}" {
1853             # Note that lack of 'pass' call here -- this works around limitation
1854             # in DejaGNU xfail mechanism. mi-until.exp has this:
1855             #
1856             #     setup_kfail gdb/2104 "*-*-*"
1857             #     mi_execute_to ...
1858             #
1859             # and mi_execute_to uses mi_send_resuming_command.  If we use 'pass' here,
1860             # it will reset kfail, so when the actual test fails, it will be flagged
1861             # as real failure.
1862             return 0
1863         }
1864         -re "\\^error,msg=\"Displaced stepping is only supported in ARM mode\".*" {
1865             unsupported "$test (Thumb mode)"
1866             return -1
1867         }
1868         -re "\\^error,msg=.*" {
1869             fail "$test (MI error)"
1870             return -1
1871         }
1872         -re ".*${mi_gdb_prompt}" {
1873             fail "$test (failed to resume)"
1874             return -1
1875         }
1876         timeout {
1877             fail "$test"
1878             return -1
1879         }
1880     }
1881 }
1882
1883 proc mi_send_resuming_command {command test} {
1884     mi_send_resuming_command_raw -$command $test
1885 }
1886
1887 # Helper to mi_run_inline_test below.
1888 # Sets a temporary breakpoint at LOCATION and runs
1889 # the program using COMMAND.  When the program is stopped
1890 # returns the line at which it.  Returns -1 if line cannot
1891 # be determined.
1892 # Does not check that the line is the same as requested.
1893 # The caller can check itself if required.
1894 proc_with_prefix mi_continue_to_line {location test} {
1895     with_test_prefix $test {
1896         mi_tbreak $location "set temporary breakpoint"
1897         mi_send_resuming_command "exec-continue" "continue to breakpoint"
1898         return [mi_get_stop_line]
1899     }
1900 }
1901
1902 # Wait until gdb prints the current line.
1903 proc mi_get_stop_line {} {
1904
1905   global mi_gdb_prompt
1906   global async
1907
1908   if {$async} {
1909       set prompt_re ""
1910   } else {
1911       set prompt_re "$mi_gdb_prompt$"
1912   }
1913
1914   gdb_expect {
1915       -re ".*line=\"(\[0-9\]*)\".*\r\n$prompt_re" {
1916           return $expect_out(1,string)
1917       }
1918       -re ".*$mi_gdb_prompt" {
1919           fail "wait for stop (unexpected output)"
1920       }
1921       timeout {
1922           fail "wait for stop (timeout)"
1923       }
1924   }
1925 }
1926
1927 # Run a MI test embedded in comments in a C file.
1928 # The C file should contain special comments in the following
1929 # three forms:
1930 #
1931 #    /*: BEGIN: testname :*/
1932 #    /*:  <Tcl statements> :*/
1933 #    /*: END: testname :*/
1934 #
1935 # This procedure find the begin and end marker for the requested
1936 # test. Then, a temporary breakpoint is set at the begin
1937 # marker and the program is run (from start).
1938 #
1939 # After that, for each special comment between the begin and end
1940 # marker, the Tcl statements are executed.  It is assumed that
1941 # for each comment, the immediately preceding line is executable
1942 # C statement.  Then, gdb will be single-stepped until that
1943 # preceding C statement is executed, and after that the
1944 # Tcl statements in the comment will be executed.
1945 #
1946 # For example:
1947 #
1948 #     /*: BEGIN: assignment-test :*/
1949 #     v = 10;
1950 #     /*: <Tcl code to check that 'v' is indeed 10 :*/
1951 #     /*: END: assignment-test :*/
1952 #
1953 # The mi_prepare_inline_tests function should be called before
1954 # calling this function.  A given C file can contain several
1955 # inline tests.  The names of the tests must be unique within one
1956 # C file.
1957 #
1958 proc mi_run_inline_test { testcase } {
1959
1960     global mi_gdb_prompt
1961     global hex
1962     global decimal
1963     global fullname_syntax
1964     global mi_autotest_source
1965
1966     set commands [mi_get_inline_test $testcase]
1967
1968     set first 1
1969     set line_now 1
1970
1971     foreach c $commands {
1972         set statements [lindex $c 0]
1973         set line [lindex $c 1]
1974         set line [expr $line-1]
1975
1976         # We want gdb to be stopped at the expression immediately
1977         # before the comment.  If this is the first comment, the
1978         # program is either not started yet or is in some random place,
1979         # so we run it.  For further comments, we might be already
1980         # standing at the right line. If not continue till the
1981         # right line.
1982
1983         if {$first==1} {
1984             # Start the program afresh.
1985             mi_tbreak "$mi_autotest_source:$line" "set temporary breakpoint"
1986             if { [mi_run_cmd] < 0 } {
1987                 return -1
1988             }
1989             set line_now [mi_get_stop_line]
1990             set first 0
1991         } elseif {$line_now!=$line} {
1992             set line_now [mi_continue_to_line "$mi_autotest_source:$line" "continue to $line"]
1993         }
1994
1995         if {$line_now!=$line} {
1996             fail "$testcase: go to line $line"
1997         }
1998
1999         # We're not at the statement right above the comment.
2000         # Execute that statement so that the comment can test
2001         # the state after the statement is executed.
2002
2003         # Single-step past the line.
2004         if { [mi_send_resuming_command "exec-next" "$testcase: step over $line"] != 0 } {
2005             return -1
2006         }
2007         set line_now [mi_get_stop_line]
2008
2009         # We probably want to use 'uplevel' so that statements
2010         # have direct access to global variables that the
2011         # main 'exp' file has set up.  But it's not yet clear,
2012         # will need more experience to be sure.
2013         eval $statements
2014     }
2015
2016     return 0
2017 }
2018
2019 proc get_mi_thread_list {name} {
2020   global expect_out
2021
2022   # MI will return a list of thread ids:
2023   #
2024   # -thread-list-ids
2025   # ^done,thread-ids=[thread-id="1",thread-id="2",...],number-of-threads="N"
2026   # (gdb)
2027   mi_gdb_test "-thread-list-ids" \
2028     {.*\^done,thread-ids={(thread-id="[0-9]+"(,)?)+},current-thread-id="[0-9]+",number-of-threads="[0-9]+"} \
2029     "-thread_list_ids ($name)"
2030
2031   set output {}
2032   if {[info exists expect_out(buffer)]} {
2033     set output $expect_out(buffer)
2034   }
2035
2036   set thread_list {}
2037   if {![regexp {thread-ids=\{(thread-id="[0-9]+"(,)?)*\}} $output threads]} {
2038     fail "finding threads in MI output ($name)"
2039   } else {
2040     pass "finding threads in MI output ($name)"
2041
2042     # Make list of console threads
2043     set start [expr {[string first \{ $threads] + 1}]
2044     set end   [expr {[string first \} $threads] - 1}]
2045     set threads [string range $threads $start $end]
2046     foreach thread [split $threads ,] {
2047       if {[scan $thread {thread-id="%d"} num]} {
2048         lappend thread_list $num
2049       }
2050     }
2051   }
2052
2053   return $thread_list
2054 }
2055
2056 # Check that MI and the console know of the same threads.
2057 # Appends NAME to all test names.
2058 proc check_mi_and_console_threads {name} {
2059   global expect_out
2060
2061   mi_gdb_test "-thread-list-ids" \
2062     {.*\^done,thread-ids={(thread-id="[0-9]+"(,)*)+},current-thread-id="[0-9]+",number-of-threads="[0-9]+"} \
2063     "-thread-list-ids ($name)"
2064   set mi_output {}
2065   if {[info exists expect_out(buffer)]} {
2066     set mi_output $expect_out(buffer)
2067   }
2068
2069   # GDB will return a list of thread ids and some more info:
2070   #
2071   # (gdb) 
2072   # -interpreter-exec console "info threads"
2073   # ~"  4 Thread 2051 (LWP 7734)  0x401166b1 in __libc_nanosleep () at __libc_nanosleep:-1"
2074   # ~"  3 Thread 1026 (LWP 7733)   () at __libc_nanosleep:-1"
2075   # ~"  2 Thread 2049 (LWP 7732)  0x401411f8 in __poll (fds=0x804bb24, nfds=1, timeout=2000) at ../sysdeps/unix/sysv/linux/poll.c:63"
2076   # ~"* 1 Thread 1024 (LWP 7731)  main (argc=1, argv=0xbfffdd94) at ../../../src/gdb/testsuite/gdb.mi/pthreads.c:160"
2077   # FIXME: kseitz/2002-09-05: Don't use the hack-cli method.
2078   mi_gdb_test "info threads" \
2079     {.*(~".*"[\r\n]*)+.*} \
2080     "info threads ($name)"
2081   set console_output {}
2082   if {[info exists expect_out(buffer)]} {
2083     set console_output $expect_out(buffer)
2084   }
2085
2086   # Make a list of all known threads to console (gdb's thread IDs)
2087   set console_thread_list {}
2088   foreach line [split $console_output \n] {
2089     if {[string index $line 0] == "~"} {
2090       # This is a line from the console; trim off "~", " ", "*", and "\""
2091       set line [string trim $line ~\ \"\*]
2092       if {[scan $line "%d" id] == 1} {
2093         lappend console_thread_list $id
2094       }
2095     }
2096   }
2097
2098   # Now find the result string from MI
2099   set mi_result ""
2100   foreach line [split $mi_output \n] {
2101     if {[string range $line 0 4] == "^done"} {
2102       set mi_result $line
2103     }
2104   }
2105   if {$mi_result == ""} {
2106     fail "finding MI result string ($name)"
2107   } else {
2108     pass "finding MI result string ($name)"
2109   }
2110
2111   # Finally, extract the thread ids and compare them to the console
2112   set num_mi_threads_str ""
2113   if {![regexp {number-of-threads="[0-9]+"} $mi_result num_mi_threads_str]} {
2114     fail "finding number of threads in MI output ($name)"
2115   } else {
2116     pass "finding number of threads in MI output ($name)"
2117
2118     # Extract the number of threads from the MI result
2119     if {![scan $num_mi_threads_str {number-of-threads="%d"} num_mi_threads]} {
2120       fail "got number of threads from MI ($name)"
2121     } else {
2122       pass "got number of threads from MI ($name)"
2123
2124       # Check if MI and console have same number of threads
2125       if {$num_mi_threads != [llength $console_thread_list]} {
2126         fail "console and MI have same number of threads ($name)"
2127       } else {
2128         pass "console and MI have same number of threads ($name)"
2129
2130         # Get MI thread list
2131         set mi_thread_list [get_mi_thread_list $name]
2132
2133         # Check if MI and console have the same threads
2134         set fails 0
2135         foreach ct [lsort $console_thread_list] mt [lsort $mi_thread_list] {
2136           if {$ct != $mt} {
2137             incr fails
2138           }
2139         }
2140         if {$fails > 0} {
2141           fail "MI and console have same threads ($name)"
2142
2143           # Send a list of failures to the log
2144           send_log "Console has thread ids: $console_thread_list\n"
2145           send_log "MI has thread ids: $mi_thread_list\n"
2146         } else {
2147           pass "MI and console have same threads ($name)"
2148         }
2149       }
2150     }
2151   }
2152 }
2153
2154 # Download shared libraries to the target.
2155 proc mi_load_shlibs { args } {
2156     foreach file $args {
2157         gdb_remote_download target [shlib_target_file $file]
2158     }
2159
2160     if {[is_remote target]} {
2161         # If the target is remote, we need to tell gdb where to find the
2162         # libraries.
2163         #
2164         # We could set this even when not testing remotely, but a user
2165         # generally won't set it unless necessary.  In order to make the tests
2166         # more like the real-life scenarios, we don't set it for local testing.
2167         mi_gdb_test "set solib-search-path [file dirname [lindex $args 0]]" "\^done" ""
2168     }
2169 }
2170
2171 proc mi_check_thread_states { states test } {
2172     global expect_out
2173     set pattern ".*\\^done,threads=\\\["
2174     foreach s $states {
2175         set pattern "${pattern}(.*)state=\"$s\""
2176     }
2177     set pattern "${pattern}(,core=\"\[0-9\]*\")?\\\}\\\].*"
2178
2179     verbose -log "expecting: $pattern"
2180     mi_gdb_test "-thread-info" $pattern $test
2181 }
2182
2183 # Return a list of MI features supported by this gdb.
2184 proc mi_get_features {} {
2185     global expect_out mi_gdb_prompt
2186
2187     send_gdb "-list-features\n"
2188
2189     gdb_expect {
2190         -re "\\^done,features=\\\[(.*)\\\]\r\n$mi_gdb_prompt$" {
2191             regsub -all -- \" $expect_out(1,string) "" features
2192             return [split $features ,]
2193         }
2194         -re ".*\r\n$mi_gdb_prompt$" {
2195             verbose -log "got $expect_out(buffer)"
2196             return ""
2197         }
2198         timeout {
2199             verbose -log "timeout in mi_gdb_prompt"
2200             return ""
2201         }
2202     }
2203 }
2204
2205 # Variable Object Trees
2206 #
2207 # Yet another way to check varobjs. Pass mi_walk_varobj_tree a "list" of
2208 # variables (not unlike the actual source code definition), and it will
2209 # automagically test the children for you (by default).
2210 #
2211 # Example:
2212 #
2213 # source code:
2214 # struct bar {
2215 #   union {
2216 #     int integer;
2217 #     void *ptr;
2218 #   };
2219 #   const int *iPtr;
2220 # };
2221 #
2222 # class foo {
2223 # public:
2224 #   int a;
2225 #   struct {
2226 #     int b;
2227 #     struct bar *c;
2228 #   };
2229 # };
2230 #
2231 # foo *f = new foo (); <-- break here
2232 #
2233 # We want to check all the children of "f".
2234 #
2235 # Translate the above structures into the following tree:
2236 #
2237 # set tree {
2238 #   foo f {
2239 #     {} public {
2240 #       int a {}
2241 #       anonymous struct {
2242 #         {} public {
2243 #           int b {}
2244 #           {bar *} c {
2245 #             {} public {
2246 #               anonymous union {
2247 #                 {} public {
2248 #                   int integer {}
2249 #                   {void *} ptr {}
2250 #                 }
2251 #               }
2252 #               {const int *} iPtr {
2253 #                 {const int} {*iPtr} {}
2254 #               }
2255 #             }
2256 #           }
2257 #         }
2258 #       }
2259 #     }
2260 #   }
2261 # }
2262 #
2263 # mi_walk_varobj_tree c++ $tree
2264 #
2265 # If you'd prefer to walk the tree using your own callback,
2266 # simply pass the name of the callback to mi_walk_varobj_tree.
2267 #
2268 # This callback should take one argument, the name of the variable
2269 # to process.  This name is the name of a global array holding the
2270 # variable's properties (object name, type, etc).
2271 #
2272 # An example callback:
2273 #
2274 # proc my_callback {var} {
2275 #   upvar #0 $var varobj
2276 #
2277 #   puts "my_callback: called on varobj $varobj(obj_name)"
2278 # }
2279 #
2280 # The arrays created for each variable object contain the following
2281 # members:
2282 #
2283 # obj_name     - the object name for accessing this variable via MI
2284 # display_name - the display name for this variable (exp="display_name" in
2285 #                the output of -var-list-children)
2286 # type         - the type of this variable (type="type" in the output
2287 #                of -var-list-children, or the special tag "anonymous"
2288 # path_expr    - the "-var-info-path-expression" for this variable
2289 #                NOTE: This member cannot be used reliably with typedefs.
2290 #                Use with caution!
2291 #                See notes inside get_path_expr for more.
2292 # parent       - the variable name of the parent varobj
2293 # children     - a list of children variable names (which are the
2294 #                names Tcl arrays, not object names)
2295 #
2296 # For each variable object, an array containing the above fields will
2297 # be created under the root node (conveniently called, "root").  For example,
2298 # a variable object with handle "OBJ.public.0_anonymous.a" will have
2299 # a corresponding global Tcl variable named "root.OBJ.public.0_anonymous.a".
2300 #
2301 # Note that right now, this mechanism cannot be used for recursive data
2302 # structures like linked lists.
2303
2304 namespace eval ::varobj_tree {
2305   # An index which is appended to root varobjs to ensure uniqueness.
2306   variable _root_idx 0
2307
2308   # A procedure to help with debuggging varobj trees.
2309   # VARIABLE_NAME is the name of the variable to dump.
2310   # CMD, if present, is the name of the callback to output the contstructed
2311   #   strings. By default, it uses expect's "send_log" command.
2312   # TERM, if present, is a terminating character. By default it is the newline.
2313   #
2314   # To output to the terminal (not the expect log), use
2315   # mi_varobj_tree_dump_variable my_variable puts ""
2316
2317   proc mi_varobj_tree_dump_variable {variable_name {cmd send_log} {term "\n"}} {
2318     upvar #0 $variable_name varobj
2319
2320     eval "$cmd \"VAR = $variable_name$term\""
2321
2322     # Explicitly encode the array indices, since outputting them
2323     # in some logical order is better than what "array names" might
2324     # return.
2325     foreach idx {obj_name parent display_name type path_expr} {
2326       eval "$cmd \"\t$idx = $varobj($idx)$term\""
2327     }
2328
2329     # Output children
2330     set num [llength $varobj(children)]
2331     eval "$cmd \"\tnum_children = $num$term\""
2332     if {$num > 0} {
2333       eval "$cmd \"\tchildren = $varobj(children)$term\""
2334     }
2335   }
2336
2337   # The default callback used by mi_walk_varobj_tree.  This callback
2338   # simply checks all of VAR's children.  It specifically does not test
2339   # path expressions, since that is very problematic.
2340   #
2341   # This procedure may be used in custom callbacks.
2342   proc test_children_callback {variable_name} {
2343     upvar #0 $variable_name varobj
2344
2345     if {[llength $varobj(children)] > 0} {
2346       # Construct the list of children the way mi_list_varobj_children
2347       # expects to get it:
2348       # { {obj_name display_name num_children type} ... }
2349       set children_list {}
2350       foreach child $varobj(children) {
2351         upvar #0 $child c
2352         set clist [list [string_to_regexp $c(obj_name)] \
2353                        [string_to_regexp $c(display_name)] \
2354                        [llength $c(children)]]
2355         if {[string length $c(type)] > 0} {
2356           lappend clist [string_to_regexp $c(type)]
2357         }
2358         lappend children_list $clist
2359       }
2360
2361       mi_list_varobj_children $varobj(obj_name) $children_list \
2362           "VT: list children of $varobj(obj_name)"
2363     }
2364   }
2365
2366   # Set the properties of the varobj represented by
2367   # PARENT_VARIABLE - the name of the parent's variable
2368   # OBJNAME         - the MI object name of this variable
2369   # DISP_NAME       - the display name of this variable
2370   # TYPE            - the type of this variable
2371   # PATH            - the path expression for this variable
2372   # CHILDREN        - a list of the variable's children
2373   proc create_varobj {parent_variable objname disp_name \
2374                           type path children} {
2375     upvar #0 $parent_variable parent
2376
2377     set var_name "root.$objname"
2378     global $var_name
2379     array set $var_name [list obj_name $objname]
2380     array set $var_name [list display_name $disp_name]
2381     array set $var_name [list type $type]
2382     array set $var_name [list path_expr $path]
2383     array set $var_name [list parent "$parent_variable"]
2384     array set $var_name [list children \
2385                              [get_tree_children $var_name $children]]
2386     return $var_name
2387   }
2388
2389   # Should VARIABLE be used in path expressions?  The CPLUS_FAKE_CHILD
2390   # varobjs and anonymous structs/unions are not used for path expressions.
2391   proc is_path_expr_parent {variable} {
2392     upvar #0 $variable varobj
2393
2394     # If the varobj's type is "", it is a CPLUS_FAKE_CHILD.
2395     # If the tail of the varobj's object name is "%d_anonymous",
2396     # then it represents an anonymous struct or union.
2397     if {[string length $varobj(type)] == 0 \
2398             || [regexp {[0-9]+_anonymous$} $varobj(obj_name)]} {
2399       return false
2400     }
2401
2402     return true
2403   }
2404
2405   # Return the path expression for the variable named NAME in
2406   # parent varobj whose variable name is given by PARENT_VARIABLE.
2407   proc get_path_expr {parent_variable name type} {
2408     upvar #0 $parent_variable parent
2409     upvar #0 $parent_variable path_parent
2410
2411     # If TYPE is "", this is one of the CPLUS_FAKE_CHILD varobjs,
2412     # which has no path expression.  Likewsise for anonymous structs
2413     # and unions.
2414     if {[string length $type] == 0 \
2415             || [string compare $type "anonymous"] == 0} {
2416       return ""
2417     }
2418
2419     # Find the path parent variable.
2420     while {![is_path_expr_parent $parent_variable]} {
2421       set parent_variable $path_parent(parent)
2422       upvar #0 $parent_variable path_parent
2423     }
2424
2425     # This is where things get difficult.  We do not actually know
2426     # the real type for variables defined via typedefs, so we don't actually
2427     # know whether the parent is a structure/union or not.
2428     #
2429     # So we assume everything that isn't a simple type is a compound type.
2430     set stars ""
2431     regexp {\*+} $parent(type) stars
2432     set is_compound 1
2433     if {[string index $name 0] == "*"} {
2434       set is_compound 0
2435     }
2436
2437     if {[string index $parent(type) end] == "\]"} {
2438       # Parent is an array.
2439       return "($path_parent(path_expr))\[$name\]"
2440     } elseif {$is_compound} {
2441       # Parent is a structure or union or a pointer to one.
2442       if {[string length $stars]} {
2443         set join "->"
2444       } else {
2445         set join "."
2446       }
2447
2448       global root
2449
2450       # To make matters even more hideous, varobj.c has slightly different
2451       # path expressions for C and C++.
2452       set path_expr "($path_parent(path_expr))$join$name"
2453       if {[string compare -nocase $root(language) "c"] == 0} {
2454         return $path_expr
2455       } else {
2456         return "($path_expr)"
2457       }
2458     } else {
2459       # Parent is a pointer.
2460       return "*($path_parent(path_expr))"
2461     }
2462   }
2463
2464   # Process the CHILDREN (a list of varobj_tree elements) of the variable
2465   # given by PARENT_VARIABLE.  Returns a list of children variables.
2466   proc get_tree_children {parent_variable children} {
2467     upvar #0 $parent_variable parent
2468
2469     set field_idx 0
2470     set children_list {}
2471     foreach {type name children} $children {
2472       if {[string compare $parent_variable "root"] == 0} {
2473         # Root variable
2474         variable _root_idx
2475         incr _root_idx
2476         set objname "$name$_root_idx"
2477         set disp_name "$name"
2478         set path_expr "$name"
2479       } elseif {[string compare $type "anonymous"] == 0} {
2480         # Special case: anonymous types.  In this case, NAME will either be
2481         # "struct" or "union".
2482         set objname "$parent(obj_name).${field_idx}_anonymous"
2483         set disp_name "<anonymous $name>"
2484         set path_expr ""
2485         set type "$name {...}"
2486       } else {
2487         set objname "$parent(obj_name).$name"
2488         set disp_name $name
2489         set path_expr [get_path_expr $parent_variable $name $type]
2490       }
2491
2492       lappend children_list [create_varobj $parent_variable $objname \
2493                                  $disp_name $type $path_expr $children]
2494       incr field_idx
2495     }
2496
2497     return $children_list
2498   }
2499
2500   # The main procedure to call the given CALLBACK on the elements of the
2501   # given varobj TREE.  See detailed explanation above.
2502   proc walk_tree {language tree callback} {
2503     global root
2504     variable _root_idx
2505
2506     if {[llength $tree] < 3} {
2507       error "tree does not contain enough elements"
2508     }
2509
2510     set _root_idx 0
2511
2512     # Create root node and process the tree.
2513     array set root [list language $language]
2514     array set root [list obj_name "root"]
2515     array set root [list display_name "root"]
2516     array set root [list type "root"]
2517     array set root [list path_expr "root"]
2518     array set root [list parent "root"]
2519     array set root [list children [get_tree_children root $tree]]
2520
2521     # Walk the tree
2522     set all_nodes $root(children); # a stack of nodes
2523     while {[llength $all_nodes] > 0} {
2524       # "Pop" the name of the global variable containing this varobj's
2525       # information from the stack of nodes.
2526       set var_name [lindex $all_nodes 0]
2527       set all_nodes [lreplace $all_nodes 0 0]
2528
2529       # Bring the global named in VAR_NAME into scope as the local variable
2530       # VAROBJ.
2531       upvar #0 $var_name varobj
2532
2533       # Append any children of VAROBJ to the list of nodes to walk.
2534       if {[llength $varobj(children)] > 0} {
2535         set all_nodes [concat $all_nodes $varobj(children)]
2536       }
2537
2538       # If this is a root variable, create the variable object for it.
2539       if {[string compare $varobj(parent) "root"] == 0} {
2540         mi_create_varobj $varobj(obj_name) $varobj(display_name) \
2541             "VT: create root varobj for $varobj(display_name)"
2542       }
2543
2544       # Now call the callback for VAROBJ.
2545       uplevel #0 $callback $var_name
2546     }
2547   }
2548 }
2549
2550 # The default varobj tree callback, which simply tests -var-list-children.
2551 proc mi_varobj_tree_test_children_callback {variable} {
2552   ::varobj_tree::test_children_callback $variable
2553 }
2554
2555 # Walk the variable object tree given by TREE, calling the specified
2556 # CALLBACK.  By default this uses mi_varobj_tree_test_children_callback.
2557 proc mi_walk_varobj_tree {language tree \
2558                               {callback \
2559                                    mi_varobj_tree_test_children_callback}} {
2560   ::varobj_tree::walk_tree $language $tree $callback
2561 }
2562
2563 # Build a list of key-value pairs given by the list ATTR_LIST.  Flatten
2564 # this list using the optional JOINER, a comma by default.
2565 #
2566 # The list must contain an even number of elements, which are the key-value
2567 # pairs.  Each value will be surrounded by quotes, according to the grammar,
2568 # except if the value starts with \[ or \{, when the quotes will be omitted.
2569 #
2570 # Example: mi_build_kv_pairs {a b c d e f g \[.*\]}
2571 # returns a=\"b\",c=\"d\",e=\"f\",g=\[.*\]
2572 proc mi_build_kv_pairs {attr_list {joiner ,}} {
2573     set l {}
2574     foreach {var value} $attr_list {
2575         if {[string range $value 0 1] == "\\\["
2576             || [string range $value 0 1] == "\\\{"} {
2577             lappend l "$var=$value"
2578         } else {
2579             lappend l "$var=\"$value\""
2580         }
2581     }
2582     return "[join $l $joiner]"
2583 }
2584
2585 # Construct a breakpoint location regexp.  This may be used along with
2586 # mi_make_breakpoint_multi to test the output of -break-insert,
2587 # -dprintf-insert, or -break-info with breapoints with multiple
2588 # locations.
2589 #
2590 # All arguments for the breakpoint location may be specified using the
2591 # options number, enabled, addr, func, file, fullname, line and
2592 # thread-groups.
2593 #
2594 # Example: mi_make_breakpoint_loc -number 2.1 -file ".*/myfile.c" -line 3
2595 # will return the breakpoint location:
2596 # {number="2.1",enabled=".*",addr=".*",func=".*",
2597 #       file=".*/myfile.c",fullname=".*",line="3",thread-groups=\[.*\]}
2598
2599 proc mi_make_breakpoint_loc {args} {
2600     parse_args {{number .*} {enabled .*} {addr .*}
2601         {func .*} {file .*} {fullname .*} {line .*}
2602         {thread-groups \\\[.*\\\]}}
2603
2604     set attr_list {}
2605     foreach attr [list number enabled addr func file \
2606                       fullname line thread-groups] {
2607         lappend attr_list $attr [set $attr]
2608     }
2609
2610     return "{[mi_build_kv_pairs $attr_list]}"
2611 }
2612
2613 # Bits shared between mi_make_breakpoint and mi_make_breakpoint_multi.
2614
2615 proc mi_make_breakpoint_1 {attr_list cond evaluated-by times \
2616                            ignore script original-location} {
2617     set result "bkpt=\\\{[mi_build_kv_pairs $attr_list]"
2618
2619     # There are always exceptions.
2620
2621     # If COND is not preset, do not output it.
2622     if {[string length $cond] > 0} {
2623         append result ","
2624         append result [mi_build_kv_pairs [list "cond" $cond]]
2625
2626         # When running on a remote, GDB may output who is evaluating
2627         # breakpoint conditions.
2628         if {[string length ${evaluated-by}] > 0} {
2629             append result [mi_build_kv_pairs \
2630                                [list "evaluated-by" ${evaluated-by}]]
2631         } else {
2632             append result {(,evaluated-by=".*")?}
2633         }
2634     }
2635
2636     append result ","
2637     append result [mi_build_kv_pairs [list "times" $times]]
2638
2639     # If SCRIPT and IGNORE are not present, do not output them.
2640     if {$ignore != 0} {
2641         append result ","
2642         append result [mi_build_kv_pairs [list "ignore" $ignore]]
2643         append result ","
2644     }
2645     if {[string length $script] > 0} {
2646         append result ","
2647         append result [mi_build_kv_pairs [list "script" $script]]
2648         append result ","
2649     } else {
2650         # Allow anything up until the next "official"/required attribute.
2651         # This pattern skips over script/ignore if matches on those
2652         # were not specifically required by the caller.
2653         append result ".*"
2654     }
2655     append result [mi_build_kv_pairs \
2656                        [list "original-location" ${original-location}]]
2657
2658     return $result
2659 }
2660
2661
2662 # Construct a breakpoint regexp, for a breakpoint with multiple
2663 # locations.  This may be used to test the output of -break-insert,
2664 # -dprintf-insert, or -break-info with breakpoints with multiple
2665 # locations.
2666 #
2667 # All arguments for the breakpoint may be specified using the options
2668 # number, type, disp, enabled, func, cond, evaluated-by, times,
2669 # ignore, script and locations.
2670 #
2671 # Only if -script and -ignore are given will they appear in the output.
2672 # Otherwise, this procedure will skip them using ".*".
2673 #
2674 # Example: mi_make_breakpoint_multi -number 2 -locations "$loc"
2675 # will return the breakpoint:
2676 # bkpt={number="2",type=".*",disp=".*",enabled=".*",addr="<MULTIPLE>",
2677 #       times="0".*original-location=".*",locations=$loc}
2678 #
2679 # You can construct the list of locations with mi_make_breakpoint_loc.
2680
2681 proc mi_make_breakpoint_multi {args} {
2682     parse_args {{number .*} {type .*} {disp .*} {enabled .*}
2683         {times .*} {ignore 0}
2684         {script ""} {original-location .*} {cond ""} {evaluated-by ""}
2685         {locations .*}}
2686
2687     set attr_list {}
2688     foreach attr [list number type disp enabled] {
2689         lappend attr_list $attr [set $attr]
2690     }
2691
2692     lappend attr_list "addr" "<MULTIPLE>"
2693
2694     set result [mi_make_breakpoint_1 \
2695                     $attr_list $cond ${evaluated-by} $times \
2696                     $ignore $script ${original-location}]
2697
2698     append result ","
2699     append result [mi_build_kv_pairs [list "locations" $locations]]
2700
2701     append result "\\\}"
2702     return $result
2703 }
2704
2705 # Construct a breakpoint regexp, for a pending breakpoint.  This may
2706 # be used to test the output of -break-insert, -dprintf-insert, or
2707 # -break-info for pending breakpoints.
2708 #
2709 # Arguments for the breakpoint may be specified using the options
2710 # number, type, disp, enabled, pending.
2711 #
2712 # Example: mi_make_breakpoint_pending -number 2 -pending func
2713 # will return the breakpoint:
2714 # bkpt={number="2",type=".*",disp=".*",enabled=".*",addr="<PENDING>",
2715 #       pending="func", times="0".*original-location=".*"}
2716
2717 proc mi_make_breakpoint_pending {args} {
2718     parse_args {{number .*} {type .*} {disp .*} {enabled .*}
2719         {pending .*} {original-location .*}}
2720
2721     set attr_list {}
2722     foreach attr [list number type disp enabled] {
2723         lappend attr_list $attr [set $attr]
2724     }
2725
2726     lappend attr_list "addr" "<PENDING>"
2727
2728     foreach attr [list pending] {
2729         lappend attr_list $attr [set $attr]
2730     }
2731
2732     set ignore 0
2733     set times 0
2734     set script ""
2735     set cond ""
2736     set evaluated-by ""
2737
2738     set result [mi_make_breakpoint_1 \
2739                     $attr_list $cond ${evaluated-by} $times \
2740                     $ignore $script ${original-location}]
2741
2742     append result "\\\}"
2743     return $result
2744 }
2745
2746 # Construct a breakpoint regexp.  This may be used to test the output of
2747 # -break-insert, -dprintf-insert, or -break-info.
2748 #
2749 # All arguments for the breakpoint may be specified using the options
2750 # number, type, disp, enabled, addr, func, file, fullanme, line,
2751 # thread-groups, cond, evaluated-by, times, ignore, script,
2752 # and original-location.
2753 #
2754 # Only if -script and -ignore are given will they appear in the output.
2755 # Otherwise, this procedure will skip them using ".*".
2756 #
2757 # Example: mi_make_breakpoint -number 2 -file ".*/myfile.c" -line 3
2758 # will return the breakpoint:
2759 # bkpt={number="2",type=".*",disp=".*",enabled=".*",addr=".*",func=".*",
2760 #       file=".*/myfile.c",fullname=".*",line="3",thread-groups=\[.*\],
2761 #       times="0".*original-location=".*"}
2762
2763 proc mi_make_breakpoint {args} {
2764     parse_args {{number .*} {type .*} {disp .*} {enabled .*} {addr .*}
2765         {func .*} {file .*} {fullname .*} {line .*}
2766         {thread-groups \\\[.*\\\]} {times .*} {ignore 0}
2767         {script ""} {original-location .*} {cond ""} {evaluated-by ""}}
2768
2769     set attr_list {}
2770     foreach attr [list number type disp enabled addr func file \
2771                       fullname line thread-groups] {
2772         lappend attr_list $attr [set $attr]
2773     }
2774
2775     set result [mi_make_breakpoint_1 \
2776                     $attr_list $cond ${evaluated-by} $times \
2777                     $ignore $script ${original-location}]
2778
2779     append result "\\\}"
2780     return $result
2781 }
2782
2783 # Build a breakpoint table regexp given the list of breakpoints in `bp_list',
2784 # constructed by mi_make_breakpoint.
2785 #
2786 # Example:  Construct a breakpoint table where the only attributes we
2787 # test for are the existence of three breakpoints numbered 1, 2, and 3.
2788 #
2789 # set bps {}
2790 # lappend bps [mi_make_breakpoint -number 1]
2791 # lappend bps [mi_make_breakpoint -number 2]
2792 # lappned bps [mi_make_breakpoint -number 3]
2793 # mi_make_breakpoint_table $bps
2794 # will return (abbreviated for clarity):
2795 # BreakpointTable={nr_rows="3",nr_cols="6",hdr=[{width=".*",...} ...],
2796 #   body=[bkpt={number="1",...},bkpt={number="2",...},bkpt={number="3",...}]}
2797
2798 proc mi_make_breakpoint_table {bp_list} {
2799     # Build header -- assume a standard header for all breakpoint tables.
2800     set hl {}
2801     foreach {nm hdr} [list number Num type Type disp Disp enabled Enb \
2802                           addr Address what What] {
2803         # The elements here are the MI table headers, which have the
2804         # format:
2805         # {width="7",alignment="-1",col_name="number",colhdr="Num"}
2806         lappend hl "{[mi_build_kv_pairs [list width .* alignment .* \
2807                                        col_name $nm colhdr $hdr]]}"
2808     }
2809     set header "hdr=\\\[[join $hl ,]\\\]"
2810
2811     # The caller has implicitly supplied the number of columns and rows.
2812     set nc [llength $hl]
2813     set nr [llength $bp_list]
2814
2815     # Build body -- mi_make_breakpoint has done most of the work.
2816     set body "body=\\\[[join $bp_list ,]\\\]"
2817
2818     # Assemble the final regexp.
2819     return "BreakpointTable={nr_rows=\"$nr\",nr_cols=\"$nc\",$header,$body}"
2820 }
2821
2822 # Return a 1 for configurations that do not support Python scripting.
2823 # Note: This also sets various globals that specify which version of Python
2824 # is in use.  See skip_python_tests_prompt.
2825
2826 proc mi_skip_python_tests {} {
2827     global mi_gdb_prompt
2828     return [skip_python_tests_prompt "$mi_gdb_prompt$"]
2829 }
2830
2831 # As skip_libstdcxx_probe_tests_prompt, with mi_gdb_prompt.
2832
2833 proc mi_skip_libstdcxx_probe_tests {} {
2834     global mi_gdb_prompt
2835     return [skip_libstdcxx_probe_tests_prompt "$mi_gdb_prompt$"]
2836 }
2837
2838 # Check whether we're testing with the remote or extended-remote
2839 # targets.
2840
2841 proc mi_is_target_remote {} {
2842     global mi_gdb_prompt
2843
2844     return [gdb_is_target_remote_prompt "$mi_gdb_prompt"]
2845 }
2846
2847 # Retrieve the value of EXP in the inferior, represented in format
2848 # specified in FMT (using "printFMT").  DEFAULT is used as fallback if
2849 # print fails.  TEST is the test message to use.  It can be omitted,
2850 # in which case a test message is built from EXP.
2851 #
2852 # This is an MI version of gdb_valueof.
2853
2854 proc mi_get_valueof { fmt exp default {test ""} } {
2855     global mi_gdb_prompt
2856
2857     if {$test == "" } {
2858         set test "get valueof \"${exp}\""
2859     }
2860
2861     set val ${default}
2862     gdb_test_multiple "print${fmt} ${exp}" "$test" -prompt "$::mi_gdb_prompt$" {
2863         -re "~\"\\$\[0-9\]* = (\[^\r\n\]*)\\\\n\"\r\n\\^done\r\n$mi_gdb_prompt$" {
2864             set val $expect_out(1,string)
2865             pass "$test"
2866         }
2867         timeout {
2868             fail "$test (timeout)"
2869         }
2870     }
2871     return ${val}
2872 }
This page took 0.181257 seconds and 4 git commands to generate.