1 # Copyright 2016-2023 Free Software Foundation, Inc.
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.
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.
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/>.
16 # This test checks that the "thread", "select-frame", "frame" and "inferior"
17 # CLI commands, as well as the "-thread-select" and "-stack-select-frame" MI
18 # commands send the appropriate user-selection-change events to all UIs.
20 # This test considers the case where console and MI are two different UIs,
21 # and MI is created with the new-ui command.
23 # It also considers the case where the console commands are sent directly in
24 # the MI channel as described in PR 20487.
26 # It does so by starting 2 inferiors with 3 threads each.
27 # - Thread 1 of each inferior is the main thread, starting the others.
28 # - Thread 2 of each inferior is stopped at /* thread loop line */.
29 # - Thread 3 of each inferior is either stopped at /* thread loop line */, if we
30 # are using all-stop, or running, if we are using non-stop.
32 # Do not run if gdb debug is enabled as it doesn't work for separate-mi-tty.
33 require !gdb_debug_enabled
35 load_lib mi-support.exp
39 # Multiple inferiors are needed, therefore only native gdb and extended
40 # gdbserver modes are supported.
43 set compile_options "debug pthreads"
44 if {[build_executable $testfile.exp $testfile ${srcfile} ${compile_options}] == -1} {
45 untested "failed to compile"
49 set main_break_line [gdb_get_line_number "main break line"]
50 set thread_loop_line [gdb_get_line_number "thread loop line"]
51 set thread_caller_line [gdb_get_line_number "thread caller line"]
53 # Return whether we expect thread THREAD to be running in mode MODE.
55 # MODE can be either "all-stop" or "non-stop".
56 # THREAD can be either a CLI thread id (e.g. 2.3) or an MI thread id (e.g. 6).
58 proc thread_is_running { mode thread } {
59 if { $mode != "non-stop" } {
71 # Make a regular expression to match the various inferior/thread/frame selection
74 # MODE can be either "all-stop" or "non-stop", indicating which one is currently
76 # INF is the inferior number we are expecting GDB to switch to, or -1 if we are
77 # not expecting GDB to announce an inferior switch.
78 # THREAD is the thread number we are expecting GDB to switch to, or -1 if we are
79 # not expecting GDB to announce a thread switch.
80 # FRAME is the frame number we are expecting GDB to switch to, or -1 if we are
81 # not expecting GDB to announce a frame switch. See the FRAME_RE variable for
84 proc make_cli_re { mode inf thread frame } {
86 global thread_caller_line
87 global thread_loop_line
88 global main_break_line
95 set inf_re "\\\[Switching to inferior $inf${any}\\\]"
96 set all_stop_thread_re "\\\[Switching to thread [string_to_regexp $thread]${any}\\\]"
98 set frame_re(0) "#0${any}child_sub_function$any$srcfile:$thread_loop_line\r\n${any}thread loop line \\\*/"
99 set frame_re(1) "#1${any}child_function \\\(args=0x0\\\) at ${any}$srcfile:$thread_caller_line\r\n$thread_caller_line${any}/\\\* thread caller line \\\*/"
101 # Special frame for main thread.
102 set frame_re(2) "#0${any}\r\n${main_break_line}${any}"
105 append cli_re $inf_re
108 if { $thread != -1 } {
112 set thread_re $all_stop_thread_re
114 if [thread_is_running $mode $thread] {
115 set thread_re "$thread_re\\\(running\\\)"
118 append cli_re $thread_re
121 if { $frame != -1 } {
122 if { $thread != -1 } {
125 append cli_re $frame_re($frame)
131 # Make a regular expression to match the various inferior/thread/frame selection
134 # MODE can be either "all-stop" or "non-stop", indicating which one is currently
136 # THREAD is the thread number we are expecting GDB to switch to, or -1 if we are
137 # not expecting GDB to announce a thread switch.
138 # If EVENT is 1, build a regex for an "=thread-selected" async event.
139 # Otherwise, build a regex for a response to a command.
140 # FRAME is the frame number we are expecting GDB to switch to, or -1 if we are
141 # not expecting GDB to announce a frame switch. See the FRAME_RE variable for
144 proc make_mi_re { mode thread frame type } {
148 global thread_loop_line
149 global main_break_line
150 global thread_caller_line
156 set thread_event_re "=thread-selected,id=\"$thread\""
157 set thread_answer_re "\\^done,new-thread-id=\"$thread\""
159 set frame_re(0) ",frame=\{level=\"0\",addr=\"$hex\",func=\"child_sub_function\",args=\\\[\\\],file=\"${any}${srcfile}\",fullname=\"${any}${srcfile}\",line=\"$thread_loop_line\",arch=\"$any\"\}"
160 set frame_re(1) ",frame=\{level=\"1\",addr=\"$hex\",func=\"child_function\",args=\\\[\{name=\"args\",value=\"0x0\"\}\\\],file=\"${any}${srcfile}\",fullname=\"${any}${srcfile}\",line=\"$thread_caller_line\",arch=\"$any\"\}"
162 # Special frame for main thread.
163 set frame_re(2) ",frame=\{level=\"0\",addr=\"$hex\",func=\"main\",args=\\\[\\\],file=\"${any}${srcfile}\",fullname=\"${any}${srcfile}\",line=\"${main_break_line}\",arch=\"$any\"\}"
165 if { $thread != -1 } {
166 if { $type == "event" } {
167 append mi_re $thread_event_re
168 } elseif { $type == "response" } {
169 append mi_re $thread_answer_re
171 error "Invalid value for EVENT."
175 if { $frame != -1 } {
176 append mi_re $frame_re($frame)
179 if { $type == "event" } {
186 # Make a regular expression to match the various inferior/thread/frame selection
187 # events when issuing CLI commands inside MI.
189 # COMMAND is the CLI command that was sent to GDB, which will be output in the
190 # console output stream.
191 # CLI_IN_MI_MODE indicates which method of CLI-in-MI command is used. It can be
192 # either "direct" of "interpreter-exec".
193 # MODE can be either "all-stop" or "non-stop", indicating which one is currently
195 # If EVENT is 1, expect a =thread-select MI event.
196 # INF is the inferior number we are expecting GDB to switch to, or -1 if we are
197 # not expecting GDB to announce an inferior switch.
198 # CLI_THREAD is the thread number as seen in the CLI (inferior-qualified) we are
199 # expecting GDB to switch to, or -1 if we are not expecting GDB to announce a
201 # MI_THREAD is the thread number as seen in the MI (global number) we are
202 # expecting GDB to switch to, or -1 if we are not expecting GDB to announce a
204 # FRAME is the frame number we are expecting GDB to switch to, or -1 if we are
205 # not expecting GDB to announce a frame switch. See the FRAME_RE variable for
208 proc make_cli_in_mi_re { command cli_in_mi_mode mode event inf cli_thread
211 global thread_loop_line
212 global main_break_line
213 global thread_caller_line
217 set command_re [string_to_regexp $command]
218 set cli_in_mi_re "$command_re\r\n"
220 if { $cli_in_mi_mode == "direct" } {
221 append cli_in_mi_re "&\"$command_re\\\\n\"\r\n"
224 set frame_re(0) "~\"#0${any}child_sub_function${any}$srcfile:$thread_loop_line\\\\n\"\r\n~\"${thread_loop_line}${any}thread loop line \\\*/\\\\n\"\r\n"
225 set frame_re(1) "~\"#1${any}child_function \\\(args=0x0\\\) at ${any}$srcfile:$thread_caller_line\\\\n\"\r\n~\"$thread_caller_line${any}thread caller line \\\*/\\\\n\"\r\n"
227 # Special frame for main thread.
228 set frame_re(2) "~\"#0${any}main${any}\\\\n\"\r\n~\"${main_break_line}${any}\"\r\n"
231 append cli_in_mi_re "~\""
232 append cli_in_mi_re [make_cli_re $mode $inf -1 -1]
233 append cli_in_mi_re "\\\\n\"\r\n"
236 if { $cli_thread != "-1" } {
237 append cli_in_mi_re "~\""
238 append cli_in_mi_re [make_cli_re $mode -1 $cli_thread -1]
239 append cli_in_mi_re "\\\\n\"\r\n"
242 if { $frame != -1 } {
243 append cli_in_mi_re $frame_re($frame)
247 append cli_in_mi_re [make_mi_re $mode $mi_thread $frame event]
250 append cli_in_mi_re "\\^done"
255 # Return the current value of the "scheduler-locking" parameter.
257 proc show_scheduler_locking { } {
263 set test "show scheduler-locking"
264 gdb_test_multiple $test $test {
265 -re ".*Mode for locking scheduler during execution is \"(${any})\".\r\n$gdb_prompt " {
267 return $expect_out(1,string)
271 error "Couldn't get current scheduler-locking value."
274 # Prepare inferior INF so it is in the state we expect (see comment at the top).
276 proc test_continue_to_start { mode inf } {
279 global gdb_main_spawn_id
281 global main_break_line
282 global thread_loop_line
288 if { $gdb_spawn_id != $gdb_main_spawn_id } {
289 error "This should not happen."
292 with_test_prefix "inferior $inf" {
293 with_spawn_id $gdb_main_spawn_id {
294 # Continue to the point where we know for sure the threads are
296 gdb_test "tbreak $srcfile:$main_break_line" \
297 "Temporary breakpoint ${any}" \
298 "set breakpoint in main"
300 gdb_continue_to_breakpoint "main breakpoint"
302 # Consume MI event output.
303 with_spawn_id $mi_spawn_id {
305 mi_expect_stop "breakpoint-hit" "main" "" "$srcfile" \
306 "$decimal" {"" "disp=\"del\""} "stop at breakpoint in main"
308 mi_expect_stop "breakpoint-hit" "main" "" "$srcfile" \
309 "$decimal" {"" "disp=\"del\"" "locno=\"[0-9]+\""} "stop at breakpoint in main"
313 if { $mode == "all-stop" } {
314 set previous_schedlock_val [show_scheduler_locking]
316 # Set scheduler-locking on, so that we can control threads
318 gdb_test_no_output "set scheduler-locking on"
320 # Continue each child thread to the point we want them to be.
321 foreach thread { 2 3 } {
322 gdb_test "thread $inf.$thread" ".*" "select child thread $inf.$thread"
324 gdb_test "tbreak $srcfile:$thread_loop_line" \
325 "Temporary breakpoint ${any}" \
326 "set breakpoint for thread $inf.$thread"
328 gdb_continue_to_breakpoint "continue thread $inf.$thread to infinite loop breakpoint"
331 with_spawn_id $mi_spawn_id {
333 mi_expect_stop "breakpoint-hit" "child_sub_function" \
334 "" "$srcfile" "$decimal" {"" "disp=\"del\""} \
335 "thread $inf.$thread stops MI"
337 mi_expect_stop "breakpoint-hit" "child_sub_function" \
338 "" "$srcfile" "$decimal" {"" "disp=\"del\"" "locno=\"[0-9]+\""} \
339 "thread $inf.$thread stops MI"
344 # Restore scheduler-locking to its original value.
345 gdb_test_no_output "set scheduler-locking $previous_schedlock_val"
346 } else { # $mode == "non-stop"
347 # Put a thread-specific breakpoint for thread 2 of the current
348 # inferior. We don't put a breakpoint for thread 3, since we
349 # want to let it run.
350 set test "set thread-specific breakpoint, thread $inf.2"
351 gdb_test_multiple "tbreak $srcfile:$thread_loop_line thread $inf.2" $test {
352 -re "Temporary breakpoint ${any}\r\n$gdb_prompt " {
357 # Confirm the stop of thread $inf.2.
358 set test "thread $inf.2 stops CLI"
359 gdb_test_multiple "" $test {
360 -re "Thread $inf.2 ${any} hit Temporary breakpoint ${any}\r\n$thread_loop_line${any}\r\n" {
366 with_spawn_id $mi_spawn_id {
368 mi_expect_stop "breakpoint-hit" "child_sub_function" \
369 "" "$srcfile" "$decimal" {"" "disp=\"del\""} \
370 "thread $inf.2 stops MI"
372 mi_expect_stop "breakpoint-hit" "child_sub_function" \
373 "" "$srcfile" "$decimal" {"" "disp=\"del\"" "locno=\"[0-9]+\""} \
374 "thread $inf.2 stops MI"
382 # Prepare the test environment.
384 # MODE can be either "all-stop" or "non-stop".
386 proc_with_prefix test_setup { mode } {
390 global gdb_main_spawn_id
399 save_vars { GDBFLAGS } {
400 if { $mode == "non-stop" } {
401 set GDBFLAGS [concat $GDBFLAGS " -ex \"set non-stop 1\""]
404 if { [mi_clean_restart $binfile "separate-mi-tty"] != 0 } {
409 if { [mi_runto_main] < 0 } {
413 # When using mi_expect_stop, we don't expect a prompt after the *stopped
414 # event, since the blocking commands are done from the CLI. Setting async
415 # to 1 makes it not expect the prompt.
418 with_spawn_id $gdb_main_spawn_id {
419 # Add the second inferior now. While this is not mandatory, it allows
420 # us to assume that per-inferior thread numbering will be used,
421 # simplifying test_continue_to_start a bit (Thread 1.2 and not Thread 2).
422 gdb_test "add-inferior" "Added inferior 2 on connection .*" "add inferior 2"
424 # Prepare the first inferior for the test.
425 test_continue_to_start $mode 1
427 # Switch to and start the second inferior.
428 gdb_test "inferior 2" "\\\[Switching to inferior 2${any}\\\]" "switch to inferior 2"
431 # Doing "start" on the CLI generates a ton of MI output. At some point,
432 # if we don't consume/match it, the buffer between GDB's MI channel and
433 # Expect will get full, GDB will block on a write system call and we'll
434 # deadlock, waiting for CLI output that will never arrive. And then
435 # we're sad. So instead of using gdb_test and expect CLI output, send
436 # the start command first, then consume MI output, and finally consume
440 with_spawn_id $mi_spawn_id {
441 mi_expect_stop "breakpoint-hit" "main" "" "$srcfile" "$decimal" \
442 {"" "disp=\"del\"" "locno=\"[0-9]+\""} "main stop"
445 # Consume CLI output.
446 gdb_test "" "Temporary breakpoint.*Starting program.*"
448 # Prepare the second inferior for the test.
449 test_continue_to_start $mode 2
455 # Reset the selection to frame #0 of thread THREAD.
457 proc reset_selection { thread } {
458 global gdb_main_spawn_id
462 with_spawn_id $gdb_main_spawn_id {
463 gdb_test "thread $thread" \
464 "\\\[Switching to thread $thread ${any}\\\].*" \
465 "reset selection to thread $thread"
466 gdb_test "frame 0" ".*" "reset selection to frame 0"
470 # Flush Expect's internal buffers for both CLI and MI.
472 # The idea here is to send a command, and to consume all the characters that we
473 # expect that command to output, including the following prompt. Using gdb_test
474 # and mi_gdb_test should do that.
476 proc flush_buffers { } {
477 global gdb_main_spawn_id mi_spawn_id
479 with_spawn_id $gdb_main_spawn_id {
480 gdb_test "print 444" "= 444" "flush CLI"
483 with_spawn_id $mi_spawn_id {
484 mi_gdb_test "555-data-evaluate-expression 666" ".*done,value=\"666\"" "flush MI"
488 # Run a command on the current spawn id, to confirm that no output is pending
489 # in Expect's internal buffer. This is used to ensure that nothing was output
490 # on the spawn id since the call to gdb_test/mi_gdb_test/flush_buffers.
492 # The key here is that the regexes use start-of-buffer anchors (^), ensuring
493 # that they match the entire buffer, confirming that there was nothing in it
496 proc ensure_no_output { test } {
497 global gdb_spawn_id gdb_main_spawn_id mi_spawn_id
500 if { $gdb_spawn_id == $gdb_main_spawn_id } {
502 gdb_test "print 666" \
503 "^\\\$$decimal = 666" \
504 "$test, ensure no output CLI"
505 } elseif { $gdb_spawn_id == $mi_spawn_id } {
507 mi_gdb_test "777-data-evaluate-expression 888" \
508 "^777-data-evaluate-expression 888\r\n777\\^done,value=\"888\"" \
509 "$test, ensure no output MI"
511 error "Unexpected gdb_spawn_id value."
515 # Match a regular expression, or ensure that there was no output.
517 # If RE is non-empty, try to match the content of the program output (using the
518 # current spawn_id) and pass/fail TEST accordingly.
519 # If RE is empty, ensure that the program did not output anything.
521 proc match_re_or_ensure_no_output { re test } {
533 ensure_no_output $test
537 # Test selecting an inferior from CLI.
539 proc_with_prefix test_cli_inferior { mode } {
540 global gdb_main_spawn_id mi_spawn_id
542 reset_selection "1.1"
544 set mi_re [make_mi_re $mode 4 2 event]
545 set cli_re [make_cli_re $mode 2 2.1 2]
549 # Do the 'inferior' command.
550 with_spawn_id $gdb_main_spawn_id {
551 gdb_test "inferior 2" $cli_re "CLI select inferior"
554 with_spawn_id $mi_spawn_id {
555 match_re_or_ensure_no_output $mi_re "event on MI"
558 # Do the 'inferior' command on the currently selected inferior. For now,
559 # GDB naively re-outputs everything.
560 with_spawn_id $gdb_main_spawn_id {
561 gdb_test "inferior 2" $cli_re "CLI select inferior again"
564 with_spawn_id $mi_spawn_id {
565 match_re_or_ensure_no_output $mi_re "event on MI again"
569 # Test thread selection from CLI.
571 proc_with_prefix test_cli_thread { mode } {
572 global gdb_main_spawn_id
577 reset_selection "1.1"
580 with_test_prefix "thread 1.2" {
581 # Do the 'thread' command to select a stopped thread.
583 set mi_re [make_mi_re $mode 2 0 event]
584 set cli_re [make_cli_re $mode -1 1.2 0]
586 with_spawn_id $gdb_main_spawn_id {
587 gdb_test "thread 1.2" $cli_re "select thread"
590 with_spawn_id $mi_spawn_id {
591 match_re_or_ensure_no_output $mi_re "select thread, event on MI "
594 # Do the 'thread' command to select the same thread. We shouldn't receive
595 # an event on MI, since we won't actually switch thread.
599 with_spawn_id $gdb_main_spawn_id {
600 gdb_test "thread 1.2" $cli_re "select thread again"
603 with_spawn_id $mi_spawn_id {
604 match_re_or_ensure_no_output $mi_re "select thread, event on MI again"
607 # Try the 'thread' command without arguments.
609 set cli_re "\\\[Current thread is 1\\.2.*\\\]"
612 with_spawn_id $gdb_main_spawn_id {
613 gdb_test "thread" $cli_re "thread without args"
616 with_spawn_id $mi_spawn_id {
617 match_re_or_ensure_no_output $mi_re "thread without args, event on MI"
621 with_test_prefix "thread 1.3" {
622 # Do the 'thread' command to select the third thread, stopped on all-stop,
623 # running on non-stop.
625 if { $mode == "all-stop" } {
626 set cli_re [make_cli_re $mode -1 1.3 0]
627 set mi_re [make_mi_re $mode 3 0 event]
629 set cli_re [make_cli_re $mode -1 1.3 -1]
630 set mi_re [make_mi_re $mode 3 -1 event]
633 with_spawn_id $gdb_main_spawn_id {
634 gdb_test "thread 1.3" $cli_re "select thread"
637 with_spawn_id $mi_spawn_id {
638 match_re_or_ensure_no_output $mi_re "select thread, event on MI"
641 # Do the 'thread' command to select the third thread again. Again, we
642 # shouldn't receive an event on MI.
646 with_spawn_id $gdb_main_spawn_id {
647 gdb_test "thread 1.3" $cli_re "select thread again"
650 with_spawn_id $mi_spawn_id {
651 match_re_or_ensure_no_output $mi_re "select thread again, event on MI"
654 # Try the 'thread' command without arguments.
656 set cli_re "\\\[Current thread is 1\\.3 ${any}\\\]"
659 with_spawn_id $gdb_main_spawn_id {
660 gdb_test "thread" $cli_re "thread without args"
663 with_spawn_id $mi_spawn_id {
664 match_re_or_ensure_no_output $mi_re "thread without args, event on MI"
668 # Idea for the future: selecting a thread in a different inferior. For now,
669 # GDB doesn't show an inferior switch, but if it did, it would be a nice
673 # Test frame selection from CLI.
675 proc_with_prefix test_cli_frame { mode } {
676 global gdb_main_spawn_id mi_spawn_id
678 with_test_prefix "thread 1.2" {
679 reset_selection "1.2"
682 # Do the 'frame' command to select frame 1.
684 set mi_re [make_mi_re $mode 2 1 event]
685 set cli_re [make_cli_re $mode -1 -1 1]
687 with_spawn_id $gdb_main_spawn_id {
688 gdb_test "frame 1" $cli_re "select frame 1"
691 with_spawn_id $mi_spawn_id {
692 match_re_or_ensure_no_output $mi_re "select frame 1, event on MI"
695 # Do the 'frame' command to select the same frame. This time we don't
696 # expect an event on MI, since we won't actually change frame.
700 with_spawn_id $gdb_main_spawn_id {
701 gdb_test "frame 1" $cli_re "select frame 1 again"
704 with_spawn_id $mi_spawn_id {
705 match_re_or_ensure_no_output $mi_re "select frame 1 again, event on MI"
708 # Do the 'frame' command without arguments. We shouldn't see anything on MI.
710 with_spawn_id $gdb_main_spawn_id {
711 gdb_test "frame" $cli_re "frame without args"
714 with_spawn_id $mi_spawn_id {
715 match_re_or_ensure_no_output $mi_re "frame without args, event on MI"
719 with_test_prefix "thread 1.3" {
720 # Now, try the 'frame' command on thread 3, which is running if we are in
722 reset_selection "1.3"
725 if {$mode == "all-stop"} {
726 set mi_re [make_mi_re $mode 3 1 event]
727 set cli_re [make_cli_re $mode -1 -1 1]
728 } elseif {$mode == "non-stop"} {
730 set cli_re "Selected thread is running\\."
733 with_spawn_id $gdb_main_spawn_id {
734 gdb_test "frame 1" $cli_re "select frame 1"
737 with_spawn_id $mi_spawn_id {
738 match_re_or_ensure_no_output $mi_re "select frame 1, event on MI"
741 # Do the 'frame' command without arguments.
743 if { $mode == "non-stop" } {
744 set cli_re "No stack\\."
748 with_spawn_id $gdb_main_spawn_id {
749 gdb_test "frame" $cli_re "frame without args"
752 with_spawn_id $mi_spawn_id {
753 match_re_or_ensure_no_output $mi_re "frame without args, event on MI"
758 # Test frame selection from CLI with the select-frame command.
760 proc_with_prefix test_cli_select_frame { mode } {
761 global gdb_main_spawn_id mi_spawn_id expect_out
763 with_test_prefix "thread 1.2" {
764 reset_selection "1.2"
767 # Do the 'select-frame' command to select frame 1.
769 set mi_re [make_mi_re $mode 2 1 event]
771 with_spawn_id $gdb_main_spawn_id {
772 gdb_test_no_output "select-frame 1" "select frame 1"
775 with_spawn_id $mi_spawn_id {
776 match_re_or_ensure_no_output $mi_re "select frame 1, event on MI"
779 # Do the 'select-frame' command to select the same frame. This time we expect to
780 # event on MI, since we won't actually change frame.
784 with_spawn_id $gdb_main_spawn_id {
785 gdb_test_no_output "select-frame 1" "select frame 1 again"
788 with_spawn_id $mi_spawn_id {
789 match_re_or_ensure_no_output $mi_re "select frame 1 again, event on MI"
793 with_test_prefix "thread 1.3" {
794 # Now, try the 'select-frame' command on thread 3, which is running if we are in
796 reset_selection "1.3"
799 if {$mode == "all-stop"} {
800 set mi_re [make_mi_re $mode 3 1 event]
801 } elseif {$mode == "non-stop"} {
803 set cli_re "Selected thread is running\\."
806 with_spawn_id $gdb_main_spawn_id {
807 if { $mode == "all-stop" } {
808 gdb_test_no_output "select-frame 1" "select frame 1"
810 gdb_test "select-frame 1" $cli_re "select frame 1"
814 with_spawn_id $mi_spawn_id {
815 match_re_or_ensure_no_output $mi_re "select frame 1, event on MI"
820 # Test doing an up and then down command from CLI.
822 proc_with_prefix test_cli_up_down { mode } {
823 global gdb_main_spawn_id mi_spawn_id
825 reset_selection "1.2"
830 set mi_re [make_mi_re $mode 2 1 event]
831 set cli_re [make_cli_re $mode -1 -1 1]
833 with_spawn_id $gdb_main_spawn_id {
834 gdb_test "up" $cli_re "frame up"
837 with_spawn_id $mi_spawn_id {
838 match_re_or_ensure_no_output $mi_re "frame up, event on MI"
841 # Try doing a 'down'.
843 set mi_re [make_mi_re $mode 2 0 event]
844 set cli_re [make_cli_re $mode -1 -1 0]
846 with_spawn_id $gdb_main_spawn_id {
847 gdb_test "down" $cli_re "frame down"
850 with_spawn_id $mi_spawn_id {
851 match_re_or_ensure_no_output $mi_re "frame down, event on MI"
855 # Test selecting a thread from MI.
857 proc_with_prefix test_mi_thread_select { mode } {
858 global gdb_main_spawn_id mi_spawn_id
860 reset_selection "1.1"
863 with_test_prefix "thread 1.2" {
864 # Do the '-thread-select' command to select a stopped thread.
866 set mi_re [make_mi_re $mode 2 0 response]
867 set cli_re [make_cli_re $mode -1 1.2 0]
869 with_spawn_id $mi_spawn_id {
870 mi_gdb_test "-thread-select 2" $mi_re "-thread-select"
873 with_spawn_id $gdb_main_spawn_id {
874 match_re_or_ensure_no_output "$cli_re\r\n" "-thread-select, event on CLI"
877 # Do the '-thread-select' command to select the same thread. We
878 # shouldn't receive an event on CLI, since we won't actually switch
883 with_spawn_id $mi_spawn_id {
884 mi_gdb_test "-thread-select 2" $mi_re "-thread-select again"
887 with_spawn_id $gdb_main_spawn_id {
888 match_re_or_ensure_no_output $cli_re "-thread-select again, event on CLI"
892 with_test_prefix "thread 1.3" {
893 # Do the '-thread-select' command to select the third thread, stopped on all-stop,
894 # running on non-stop.
896 if { $mode == "all-stop" } {
897 set mi_re [make_mi_re $mode 3 0 response]
898 set cli_re [make_cli_re $mode -1 1.3 0]
900 set mi_re [make_mi_re $mode 3 -1 response]
901 set cli_re [make_cli_re $mode -1 1.3 -1]
904 with_spawn_id $mi_spawn_id {
905 mi_gdb_test "-thread-select 3" $mi_re "-thread-select"
908 with_spawn_id $gdb_main_spawn_id {
909 match_re_or_ensure_no_output "$cli_re\r\n" "-thread-select, event on CLI"
912 # Do the 'thread' command to select the third thread again. Again, we
913 # shouldn't receive an event on MI.
917 with_spawn_id $mi_spawn_id {
918 mi_gdb_test "-thread-select 3" $mi_re "-thread-select again"
921 with_spawn_id $gdb_main_spawn_id {
922 match_re_or_ensure_no_output $cli_re "-thread-select again, event on CLI"
926 with_test_prefix "thread 1.2 with --thread 2" {
927 # Test selecting a thread from MI with a --thread option. This test
928 # verifies that even if the thread GDB would switch to is the same as
929 # the thread specified with --thread, an event is still sent to CLI.
930 # In this case this is thread 1.2
932 set mi_re [make_mi_re $mode 2 0 response]
933 set cli_re [make_cli_re $mode -1 1.2 0]
935 with_spawn_id $mi_spawn_id {
936 mi_gdb_test "-thread-select --thread 2 2" $mi_re "-thread-select"
939 with_spawn_id $gdb_main_spawn_id {
940 match_re_or_ensure_no_output "$cli_re\r\n" "-thread-select, event on cli"
944 with_test_prefix "thread 1.2 with --thread 3" {
945 # Test selecting a thread from MI with a --thread option.
946 # This test verifies that when different thread numbers are
947 # passed to the --thread option and the underlying
948 # -thread-select command, the correct thread is selected.
949 # In this case this is thread 1.2
951 reset_selection "1.1"
953 set mi_re [make_mi_re $mode 2 0 response]
954 set cli_re [make_cli_re $mode -1 1.2 0]
956 with_spawn_id $mi_spawn_id {
957 mi_gdb_test "-thread-select --thread 3 2" $mi_re "-thread-select"
960 with_spawn_id $gdb_main_spawn_id {
961 match_re_or_ensure_no_output "$cli_re\r\n" "-thread-select, event on cli"
965 # Idea for the future: selecting a thread in a different inferior. For now,
966 # GDB doesn't show an inferior switch, but if it did, it would be a nice
970 proc_with_prefix test_mi_stack_select_frame { mode } {
971 global gdb_main_spawn_id mi_spawn_id
973 with_test_prefix "thread 1.2" {
974 reset_selection "1.2"
977 # Do the '-stack-select-frame' command to select frame 1.
980 set cli_re [make_cli_re $mode -1 -1 1]
982 with_spawn_id $mi_spawn_id {
983 mi_gdb_test "-stack-select-frame 1" $mi_re "-stack-select-frame"
986 with_spawn_id $gdb_main_spawn_id {
987 match_re_or_ensure_no_output "$cli_re\r\n" "-stack-select-frame, event on CLI"
990 # Do the '-stack-select-frame' command to select the same frame. This time we don't
991 # expect an event on CLI, since we won't actually change frame.
995 with_spawn_id $mi_spawn_id {
996 mi_gdb_test "-stack-select-frame 1" $mi_re "-stack-select-frame again"
999 with_spawn_id $gdb_main_spawn_id {
1000 match_re_or_ensure_no_output $cli_re "-stack-select-frame again, event on CLI"
1003 # Now use the '-stack-select-frame' command with the --frame
1004 # option, this verifies that even when the frame GDB would
1005 # swith to is the same as the frame specified with --frame, an
1006 # event is still sent to the CLI.
1008 set cli_re [make_cli_re $mode -1 -1 0]
1010 with_spawn_id $mi_spawn_id {
1011 mi_gdb_test "-stack-select-frame --thread 2 --frame 0 0" $mi_re
1014 with_spawn_id $gdb_main_spawn_id {
1015 match_re_or_ensure_no_output "$cli_re\r\n" "-stack-select-frame with --frame 0, event on CLI"
1018 # Now use the '-stack-select-frame' command with the --frame
1019 # option, this verifies that the correct event is sent to the
1020 # CLI when the frame specified with --frame is different to
1021 # the actual frame selected.
1023 set cli_re [make_cli_re $mode -1 -1 1]
1025 with_spawn_id $mi_spawn_id {
1026 mi_gdb_test "-stack-select-frame --thread 2 --frame 2 1" $mi_re
1029 with_spawn_id $gdb_main_spawn_id {
1030 match_re_or_ensure_no_output "$cli_re\r\n" "-stack-select-frame with --frame 2, event on CLI"
1034 with_test_prefix "thread 1.3" {
1035 # Now, try the '-stack-select-frame' command on thread 3, which is
1036 # running if we are in non-stop mode.
1037 reset_selection "1.3"
1040 if {$mode == "all-stop"} {
1042 set cli_re [make_cli_re $mode -1 -1 1]
1043 append cli_re "\r\n"
1044 } elseif {$mode == "non-stop"} {
1046 set mi_re "\\^error,msg=\"Selected thread is running\\.\""
1049 with_spawn_id $mi_spawn_id {
1050 mi_gdb_test "-stack-select-frame 1" $mi_re "-stack-select-frame"
1053 with_spawn_id $gdb_main_spawn_id {
1054 match_re_or_ensure_no_output $cli_re "-stack-select-frame, event on CLI"
1059 proc make_cli_in_mi_command { cli_in_mi_mode command } {
1060 if { $cli_in_mi_mode == "direct" } {
1062 } elseif { $cli_in_mi_mode == "interpreter-exec" } {
1063 return "-interpreter-exec console \"$command\""
1065 error "Invalid value for CLI_IN_MI_MODE."
1069 # Test selecting the inferior using a CLI command in the MI channel.
1071 proc_with_prefix test_cli_in_mi_inferior { mode cli_in_mi_mode } {
1072 global gdb_main_spawn_id mi_spawn_id
1074 reset_selection "1.1"
1077 set command [make_cli_in_mi_command $cli_in_mi_mode "inferior 2"]
1079 set mi_re [make_cli_in_mi_re $command $cli_in_mi_mode $mode 1 2 2.1 4 2]
1080 set cli_re [make_cli_re $mode 2 "2.1" 2]
1082 with_spawn_id $mi_spawn_id {
1083 mi_gdb_test $command $mi_re "select inferior"
1086 with_spawn_id $gdb_main_spawn_id {
1087 match_re_or_ensure_no_output "$cli_re\r\n" "select inferior, event on CLI"
1090 # Do the 'inferior' command on the currently selected inferior. For now,
1091 # GDB naively re-outputs everything.
1092 with_spawn_id $mi_spawn_id {
1093 mi_gdb_test $command $mi_re "select inferior again"
1096 with_spawn_id $gdb_main_spawn_id {
1097 match_re_or_ensure_no_output $cli_re "select inferior again, event on CLI"
1101 # Test selecting the thread using a CLI command in the MI channel.
1103 proc_with_prefix test_cli_in_mi_thread { mode cli_in_mi_mode } {
1104 global gdb_main_spawn_id mi_spawn_id
1106 reset_selection "1.1"
1109 with_test_prefix "thread 1.2" {
1110 # Do the 'thread' command to select a stopped thread.
1112 set command [make_cli_in_mi_command $cli_in_mi_mode "thread 1.2"]
1113 set mi_re [make_cli_in_mi_re $command $cli_in_mi_mode $mode 1 -1 1.2 2 0]
1114 set cli_re [make_cli_re $mode -1 1.2 0]
1116 with_spawn_id $mi_spawn_id {
1117 mi_gdb_test $command $mi_re "select thread"
1120 with_spawn_id $gdb_main_spawn_id {
1121 match_re_or_ensure_no_output "$cli_re\r\n" "select thread, event on CLI"
1124 # Do the 'thread' command to select the same thread. We shouldn't
1125 # receive an event on CLI, since we won't actually switch thread.
1127 set mi_re [make_cli_in_mi_re $command $cli_in_mi_mode $mode 0 -1 1.2 2 0]
1130 with_spawn_id $mi_spawn_id {
1131 mi_gdb_test $command $mi_re "select thread again"
1134 with_spawn_id $gdb_main_spawn_id {
1135 match_re_or_ensure_no_output $cli_re "select thread again, event on CLI"
1138 # Try the 'thread' command without arguments.
1140 set command [make_cli_in_mi_command $cli_in_mi_mode "thread"]
1142 set mi_re "${command}.*~\"\\\[Current thread is 1\\.2.*\\\]\\\\n\".*\\^done"
1145 with_spawn_id $mi_spawn_id {
1146 mi_gdb_test $command $mi_re "thread without args"
1149 with_spawn_id $gdb_main_spawn_id {
1150 match_re_or_ensure_no_output $cli_re "thread without args, event on CLI"
1154 with_test_prefix "thread 1.3" {
1155 # Do the 'thread' command to select the third thread, stopped on
1156 # all-stop, running on non-stop.
1158 set command [make_cli_in_mi_command $cli_in_mi_mode "thread 1.3"]
1159 if { $mode == "all-stop" } {
1160 set mi_re [make_cli_in_mi_re $command $cli_in_mi_mode $mode 1 -1 1.3 3 0]
1161 set cli_re [make_cli_re $mode -1 "1.3" 0]
1163 set mi_re [make_cli_in_mi_re $command $cli_in_mi_mode $mode 1 -1 1.3 3 -1]
1164 set cli_re [make_cli_re $mode -1 "1.3" -1]
1167 with_spawn_id $mi_spawn_id {
1168 mi_gdb_test $command $mi_re "select thread"
1171 with_spawn_id $gdb_main_spawn_id {
1172 match_re_or_ensure_no_output "$cli_re\r\n" "select thread, event on CLI"
1175 # Do the 'thread' command to select the third thread again. Again, we
1176 # shouldn't receive an event on MI.
1178 if { $mode == "all-stop" } {
1179 set mi_re [make_cli_in_mi_re $command $cli_in_mi_mode $mode 0 -1 1.3 3 0]
1181 set mi_re [make_cli_in_mi_re $command $cli_in_mi_mode $mode 0 -1 1.3 3 -1]
1185 with_spawn_id $mi_spawn_id {
1186 mi_gdb_test $command $mi_re "select thread again"
1189 with_spawn_id $gdb_main_spawn_id {
1190 match_re_or_ensure_no_output $cli_re "select thread again, event on CLI"
1193 # Try the 'thread' command without arguments.
1195 set command [make_cli_in_mi_command $cli_in_mi_mode "thread"]
1197 set mi_re "${command}.*~\"\\\[Current thread is 1\\.3.*\\\]\\\\n\".*\\^done"
1200 with_spawn_id $mi_spawn_id {
1201 mi_gdb_test $command $mi_re "thread without args"
1204 with_spawn_id $gdb_main_spawn_id {
1205 match_re_or_ensure_no_output $cli_re "thread without args, event on CLI"
1209 # Idea for the future: selecting a thread in a different inferior. For now,
1210 # GDB doesn't show an inferior switch, but if it did, it would be a nice
1214 # Test selecting the frame using a CLI command in the MI channel.
1216 proc_with_prefix test_cli_in_mi_frame { mode cli_in_mi_mode } {
1217 global gdb_main_spawn_id mi_spawn_id
1219 with_test_prefix "thread 1.2" {
1220 reset_selection "1.2"
1223 # Do the 'frame' command to select frame 1.
1225 set command [make_cli_in_mi_command $cli_in_mi_mode "frame 1"]
1226 set cli_re [make_cli_re $mode -1 -1 1]
1227 set mi_re [make_cli_in_mi_re $command $cli_in_mi_mode $mode 1 -1 -1 2 1]
1229 with_spawn_id $mi_spawn_id {
1230 mi_gdb_test $command $mi_re "select frame 1"
1233 with_spawn_id $gdb_main_spawn_id {
1234 match_re_or_ensure_no_output "$cli_re\r\n" "select frame 1, event on CLI"
1237 # Do the 'frame' command to select the same frame. This time we don't
1238 # expect an event on MI, since we won't actually change frame.
1240 set mi_re [make_cli_in_mi_re $command $cli_in_mi_mode $mode 0 -1 -1 2 1]
1243 with_spawn_id $mi_spawn_id {
1244 mi_gdb_test $command $mi_re "select frame 1 again"
1247 with_spawn_id $gdb_main_spawn_id {
1248 match_re_or_ensure_no_output $cli_re "select frame 1 again, event on CLI"
1251 # Do the 'frame' command without arguments. We shouldn't see anything on MI.
1253 set command [make_cli_in_mi_command $cli_in_mi_mode "frame"]
1254 set mi_re [make_cli_in_mi_re $command $cli_in_mi_mode $mode 0 -1 -1 2 1]
1256 with_spawn_id $mi_spawn_id {
1257 mi_gdb_test $command $mi_re "frame without args"
1260 with_spawn_id $gdb_main_spawn_id {
1261 match_re_or_ensure_no_output $cli_re "frame without args, event on CLI"
1265 with_test_prefix "thread 1.3" {
1266 # Now, try the 'frame' command on thread 3, which is running if we are in
1268 reset_selection "1.3"
1271 set command [make_cli_in_mi_command $cli_in_mi_mode "frame 1"]
1272 if {$mode == "all-stop"} {
1273 set cli_re [make_cli_re $mode -1 -1 1]
1274 append cli_re "\r\n"
1275 set mi_re [make_cli_in_mi_re $command $cli_in_mi_mode $mode 1 -1 -1 3 1]
1276 } elseif {$mode == "non-stop"} {
1278 set mi_re "\\^error,msg=\"Selected thread is running\\.\".*"
1281 with_spawn_id $mi_spawn_id {
1282 mi_gdb_test $command $mi_re "select frame 1"
1285 with_spawn_id $gdb_main_spawn_id {
1286 match_re_or_ensure_no_output $cli_re "select frame 1, event on CLI"
1289 # Do the 'frame' command without arguments.
1291 set command [make_cli_in_mi_command $cli_in_mi_mode "frame"]
1292 if { $mode == "all-stop" } {
1293 set mi_re [make_cli_in_mi_re $command $cli_in_mi_mode $mode 0 -1 -1 -1 1]
1295 set mi_re "\\^error,msg=\"No stack\\.\""
1299 with_spawn_id $mi_spawn_id {
1300 mi_gdb_test $command $mi_re "frame without args"
1303 with_spawn_id $gdb_main_spawn_id {
1304 match_re_or_ensure_no_output $cli_re "frame without args, event on CLI"
1309 foreach_with_prefix mode { "all-stop" "non-stop" } {
1310 set test "setup done"
1311 if { [test_setup $mode] == -1 } {
1317 # Test selecting inferior, thread and frame from CLI
1319 test_cli_inferior $mode
1320 test_cli_thread $mode
1321 test_cli_frame $mode
1322 test_cli_select_frame $mode
1323 test_cli_up_down $mode
1325 # Test selecting thread and frame from MI
1327 test_mi_thread_select $mode
1328 test_mi_stack_select_frame $mode
1330 # Test some CLI commands sent through MI, both with a "direct" command,
1331 # such as "thread 1", and with -interpreter-exec, such as
1332 # '-interpreter-exec console "thread 1"'.
1334 foreach_with_prefix exec_mode {"direct" "interpreter-exec"} {
1335 test_cli_in_mi_inferior $mode $exec_mode
1336 test_cli_in_mi_thread $mode $exec_mode
1337 test_cli_in_mi_frame $mode $exec_mode