public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
* [PATCH master+7.12 v2 1/3] Introduce cleanup to restore current_uiout
  2016-09-14 17:46 [PATCH master+7.12 v2 0/3] Emit user selection change notifications on all UIs Simon Marchi
@ 2016-09-14 17:46 ` Simon Marchi
  2016-09-14 17:56   ` Pedro Alves
  2016-09-14 18:11   ` Tom Tromey
  2016-09-14 17:46 ` [PATCH master+7.12 v2 3/3] Add test for user context selection sync Simon Marchi
  2016-09-14 17:46 ` [PATCH master+7.12 v2 2/3] Emit inferior, thread and frame selection events to all UIs Simon Marchi
  2 siblings, 2 replies; 21+ messages in thread
From: Simon Marchi @ 2016-09-14 17:46 UTC (permalink / raw)
  To: gdb-patches; +Cc: Simon Marchi

Make a globally available cleanup from a pre-existing one in infrun.c.
This is used in the following patch.

gdb/ChangeLog:

	* infrun.c (restore_current_uiout_cleanup): Move to ui-out.c.
	(print_stop_event): Use make_cleanup_restore_current_uiout.
	* ui-out.c (restore_current_uiout_cleanup): Move from infrun.c.
	(make_cleanup_restore_current_uiout): New function definition.
	* ui-out.h (make_cleanup_restore_current_uiout): New function
	definition.
---
 gdb/infrun.c | 12 +-----------
 gdb/ui-out.c | 18 ++++++++++++++++++
 gdb/ui-out.h |  4 ++++
 3 files changed, 23 insertions(+), 11 deletions(-)

diff --git a/gdb/infrun.c b/gdb/infrun.c
index 70d7a09..ec37ca1 100644
--- a/gdb/infrun.c
+++ b/gdb/infrun.c
@@ -8096,16 +8096,6 @@ print_stop_location (struct target_waitstatus *ws)
     print_stack_frame (get_selected_frame (NULL), 0, source_flag, 1);
 }
 
-/* Cleanup that restores a previous current uiout.  */
-
-static void
-restore_current_uiout_cleanup (void *arg)
-{
-  struct ui_out *saved_uiout = (struct ui_out *) arg;
-
-  current_uiout = saved_uiout;
-}
-
 /* See infrun.h.  */
 
 void
@@ -8118,7 +8108,7 @@ print_stop_event (struct ui_out *uiout)
 
   get_last_target_status (&last_ptid, &last);
 
-  old_chain = make_cleanup (restore_current_uiout_cleanup, current_uiout);
+  old_chain = make_cleanup_restore_current_uiout ();
   current_uiout = uiout;
 
   print_stop_location (&last);
diff --git a/gdb/ui-out.c b/gdb/ui-out.c
index 3972a56..ec44ab6 100644
--- a/gdb/ui-out.c
+++ b/gdb/ui-out.c
@@ -953,6 +953,24 @@ ui_out_destroy (struct ui_out *uiout)
   xfree (uiout);
 }
 
+/* Cleanup that restores a previous current uiout.  */
+
+static void
+restore_current_uiout_cleanup (void *arg)
+{
+  struct ui_out *saved_uiout = (struct ui_out *) arg;
+
+  current_uiout = saved_uiout;
+}
+
+/* See ui-out.h.  */
+
+struct cleanup *
+make_cleanup_restore_current_uiout (void)
+{
+  return make_cleanup (restore_current_uiout_cleanup, current_uiout);
+}
+
 /* Standard gdb initialization hook.  */
 
 void
diff --git a/gdb/ui-out.h b/gdb/ui-out.h
index 9e1e74d..6a4d78a 100644
--- a/gdb/ui-out.h
+++ b/gdb/ui-out.h
@@ -247,4 +247,8 @@ extern void ui_out_destroy (struct ui_out *uiout);
 
 extern int ui_out_redirect (struct ui_out *uiout, struct ui_file *outstream);
 
+/* Make a cleanup that restores the previous current uiout.  */
+
+extern struct cleanup *make_cleanup_restore_current_uiout (void);
+
 #endif /* UI_OUT_H */
-- 
2.9.3

^ permalink raw reply	[flat|nested] 21+ messages in thread

* [PATCH master+7.12 v2 0/3] Emit user selection change notifications on all UIs
@ 2016-09-14 17:46 Simon Marchi
  2016-09-14 17:46 ` [PATCH master+7.12 v2 1/3] Introduce cleanup to restore current_uiout Simon Marchi
                   ` (2 more replies)
  0 siblings, 3 replies; 21+ messages in thread
From: Simon Marchi @ 2016-09-14 17:46 UTC (permalink / raw)
  To: gdb-patches; +Cc: Simon Marchi

Hi all,

This is version 2 of

  https://sourceware.org/ml/gdb-patches/2016-08/msg00311.html

addressing Pedro's comments, for the most part.

The test, now in its separate patch (3/3) has been reworked.  It now checks
properly for absence of output (to check that no event were emitted).  It also
passes with make check-read1 as well.

Simon

Antoine Tremblay (2):
  Emit inferior, thread and frame selection events to all UIs
  Add test for user context selection sync

Simon Marchi (1):
  Introduce cleanup to restore current_uiout

 gdb/NEWS                                           |    4 +
 gdb/cli/cli-decode.c                               |   32 +-
 gdb/cli/cli-decode.h                               |    6 +
 gdb/cli/cli-interp.c                               |   38 +
 gdb/command.h                                      |   16 +
 gdb/defs.h                                         |   16 +
 gdb/doc/gdb.texinfo                                |   33 +-
 gdb/doc/observer.texi                              |    4 +
 gdb/frame.h                                        |    8 +
 gdb/gdbthread.h                                    |    4 +
 gdb/inferior.c                                     |   40 +-
 gdb/inferior.h                                     |    3 +
 gdb/infrun.c                                       |   12 +-
 gdb/mi/mi-cmds.c                                   |    6 +-
 gdb/mi/mi-interp.c                                 |   61 ++
 gdb/mi/mi-main.c                                   |   55 +-
 gdb/mi/mi-main.h                                   |    2 +
 gdb/stack.c                                        |   42 +-
 gdb/testsuite/gdb.mi/mi-pthreads.exp               |    4 +-
 gdb/testsuite/gdb.mi/user-selected-context-sync.c  |   64 ++
 .../gdb.mi/user-selected-context-sync.exp          | 1146 ++++++++++++++++++++
 gdb/thread.c                                       |   86 +-
 gdb/tui/tui-interp.c                               |   33 +
 gdb/ui-out.c                                       |   18 +
 gdb/ui-out.h                                       |    4 +
 25 files changed, 1638 insertions(+), 99 deletions(-)
 create mode 100644 gdb/testsuite/gdb.mi/user-selected-context-sync.c
 create mode 100644 gdb/testsuite/gdb.mi/user-selected-context-sync.exp

-- 
2.9.3

^ permalink raw reply	[flat|nested] 21+ messages in thread

* [PATCH master+7.12 v2 3/3] Add test for user context selection sync
  2016-09-14 17:46 [PATCH master+7.12 v2 0/3] Emit user selection change notifications on all UIs Simon Marchi
  2016-09-14 17:46 ` [PATCH master+7.12 v2 1/3] Introduce cleanup to restore current_uiout Simon Marchi
@ 2016-09-14 17:46 ` Simon Marchi
  2016-09-14 19:31   ` Pedro Alves
  2016-09-14 17:46 ` [PATCH master+7.12 v2 2/3] Emit inferior, thread and frame selection events to all UIs Simon Marchi
  2 siblings, 1 reply; 21+ messages in thread
From: Simon Marchi @ 2016-09-14 17:46 UTC (permalink / raw)
  To: gdb-patches; +Cc: Antoine Tremblay

From: Antoine Tremblay <antoine.tremblay@ericsson.com>

This patch adds a test to verify that events are sent properly to all
UIs when the user selection context (inferior, thread, frame) changes.

The goal of the C test file is to provide two threads that are
interrupted with the same predictable backtrace (so that we can test
frame switching).  This is achieved by having them loop on a single
line, such that when the main thread hits a breakpoint, they are both
stopped that line.  It would not be practical to have the threads sleep,
since their backtraces would not be predictable (the functions that
implement sleep may vary between systems).

There is a 1 second sleep in the main thread to make sure the threads
have time to spawn and reach the loop.  If you can find a way that is
sleep-free and race-free to achieve the same result, it would be really
nice, as most of the time taken by the test is spent sleeping.

gdb/testsuite/ChangeLog:

YYYY-MM-DD  Antoine Tremblay  <antoine.tremblay@ericsson.com>
YYYY-MM-DD  Simon Marchi  <simon.marchi@ericsson.com>

	PR gdb/20487
	* gdb.mi/user-selected-context-sync.exp: New file.
	* gdb.mi/user-selected-context-sync.c: New file.
---
 gdb/testsuite/gdb.mi/user-selected-context-sync.c  |   64 ++
 .../gdb.mi/user-selected-context-sync.exp          | 1146 ++++++++++++++++++++
 2 files changed, 1210 insertions(+)
 create mode 100644 gdb/testsuite/gdb.mi/user-selected-context-sync.c
 create mode 100644 gdb/testsuite/gdb.mi/user-selected-context-sync.exp

diff --git a/gdb/testsuite/gdb.mi/user-selected-context-sync.c b/gdb/testsuite/gdb.mi/user-selected-context-sync.c
new file mode 100644
index 0000000..6967f57
--- /dev/null
+++ b/gdb/testsuite/gdb.mi/user-selected-context-sync.c
@@ -0,0 +1,64 @@
+/* This testcase is part of GDB, the GNU debugger.
+
+   Copyright 2016 Free Software Foundation, Inc.
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+*/
+
+#include <pthread.h>
+#include <unistd.h>
+
+#define NUM_THREADS 2
+
+static int volatile quit = 0;
+
+static void
+child_sub_function (void)
+{
+  while (!quit); /* thread loop line */
+}
+
+static void *
+child_function (void *args)
+{
+  child_sub_function (); /* thread caller line */
+
+  return NULL;
+}
+
+int
+main (void)
+{
+  int i = 0;
+  pthread_t threads[NUM_THREADS];
+
+  for (i = 0; i < NUM_THREADS; i++)
+    pthread_create (&threads[i], NULL, child_function, NULL);
+
+  /* Leave enough time for the threads to reach their infinite loop. */
+  sleep (1);
+  
+  i = 0; /* main break line */
+
+  sleep (2);
+  
+  /* Allow the test to exit cleanly.  */
+  quit = 1;
+
+  for (i = 0; i < NUM_THREADS; i++)
+    pthread_join (threads[i], NULL);
+
+  return 0;
+}
diff --git a/gdb/testsuite/gdb.mi/user-selected-context-sync.exp b/gdb/testsuite/gdb.mi/user-selected-context-sync.exp
new file mode 100644
index 0000000..c043564
--- /dev/null
+++ b/gdb/testsuite/gdb.mi/user-selected-context-sync.exp
@@ -0,0 +1,1146 @@
+# Copyright 2016 Free Software Foundation, Inc.
+
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+# This test checks that thread, select-frame, frame or inferior selection
+# events are properly sent to all uis.
+#
+# This test considers the case where console and mi are two different uis
+# and mi is created with the new-ui command.
+#
+# It also considers the case where the console commands are sent directly in
+# the mi channel as described in PR 20487.
+#
+# It does so by starting 2 inferiors with 3 threads each.
+# - Thread 1 of each inferior is the main thread, starting the others.
+# - Thread 2 of each inferior is stopped at /* thread loop line */.
+# - Thread 3 of each inferior is either stopped at /* thread loop line */, if we
+#   are using all-stop, or running, if we are using non-stop.
+
+load_lib mi-support.exp
+
+standard_testfile
+
+# Multiple inferiors are needed, therefore only native gdb and extended
+# gdbserver modes are supported.
+if [use_gdb_stub] {
+    untested ${testfile}.exp
+    return
+}
+
+set compile_options "debug pthreads"
+if {[build_executable $testfile.exp $testfile ${srcfile} ${compile_options}] == -1} {
+    untested "failed to compile $testfile"
+    return -1
+}
+
+set main_bp_line [gdb_get_line_number "main break line"]
+set thread_loop_line [gdb_get_line_number "thread loop line"]
+set thread_caller_line [gdb_get_line_number "thread caller line"]
+
+# Call PROCNAME with the given arguments, inside a with_test_prefix $procname
+# block.
+
+proc with_test_prefix_procname { procname args } {
+    with_test_prefix $procname {
+	# Note: this syntax requires TCL 8.5, if we need to support 8.4,
+	# we'll need to find an alternative.
+	$procname {*}$args
+    }
+}
+
+# Return whether we expect thread THREAD to be running in mode MODE.
+#
+# MODE can be either "all-stop" or "non-stop".
+# THREAD can be either a CLI thread id (e.g. 2.3) or an MI thread id (e.g. 6).
+
+proc thread_is_running { mode thread } {
+    if { $mode != "non-stop" } {
+	return 0
+    }
+
+    return [expr {
+	$thread == 1.3
+	|| $thread == 2.3
+	|| $thread == 3
+	|| $thread == 6
+    }]
+}
+
+# Make a regular expression to match the various inferior/thread/frame selection
+# events for CLI.
+#
+# MODE can be either "all-stop" or "non-stop", indicating which one is currently
+#   in use.
+# INF is the inferior number we are expecting GDB to switch to, or -1 if we are
+#   not expecting GDB to announce an inferior switch.
+# THREAD is the thread number we are expecting GDB to switch to, or -1 if we are
+#   not expecting GDB to announce a thread switch.
+# FRAME is the frame number we are expecting GDB to switch to, or -1 if we are
+#   not expecting GDB to announce a frame switch.  See the FRAME_RE variable for
+#   details.
+
+proc make_cli_re { mode inf thread frame } {
+    global srcfile
+    global thread_caller_line thread_loop_line
+
+    set cli_re ""
+
+    set inf_re "\\\[Switching to inferior $inf.*\\\]"
+    set all_stop_thread_re "\\\[Switching to thread [string_to_regexp $thread].*\\\]"
+
+    set frame_re(0) "#0.*child_sub_function.*$srcfile:$thread_loop_line\r\n.*thread loop line \\\*/"
+    set frame_re(1) "#1.*child_function \\\(args=0x0\\\) at .*$srcfile:$thread_caller_line\r\n$thread_caller_line.*/\\\* thread caller line \\\*/"
+
+    # Special frame for main thread.
+    set frame_re(2) "#0.*"
+
+    if { $inf != -1 } {
+	append cli_re $inf_re
+    }
+
+    if { $thread != -1 } {
+	if { $inf != -1 } {
+	    append cli_re "\r\n"
+	}
+	set thread_re $all_stop_thread_re
+
+	if [thread_is_running $mode $thread] {
+	    set thread_re "$thread_re\\\(running\\\)"
+	}
+
+	append cli_re $thread_re
+    }
+
+    if { $frame != -1 } {
+	if { $thread != -1 } {
+	    append cli_re "\r\n"
+	}
+	append cli_re $frame_re($frame)
+    }
+
+    return $cli_re
+}
+
+# Make a regular expression to match the various inferior/thread/frame selection
+# events for MI.
+#
+# MODE can be either "all-stop" or "non-stop", indicating which one is currently
+#   in use.
+# THREAD is the thread number we are expecting GDB to switch to, or -1 if we are
+#   not expecting GDB to announce a thread switch.
+# If EVENT is 1, build a regex for an "=thread-selected" async event.
+#   Otherwise, build a regex for a response to a command.
+# FRAME is the frame number we are expecting GDB to switch to, or -1 if we are
+#   not expecting GDB to announce a frame switch.  See the FRAME_RE variable for
+#   details.
+
+proc make_mi_re { mode thread frame type } {
+    global srcfile hex
+    global thread_loop_line thread_caller_line
+
+    set mi_re ""
+
+    set thread_event_re "=thread-selected,id=\"$thread\""
+    set thread_answer_re ".*\\^done,new-thread-id=\"$thread\""
+
+    set frame_re(0) ",frame=\{level=\"0\",addr=\"$hex\",func=\"child_sub_function\",args=\\\[\\\],file=\".*$srcfile\",.*line=\"$thread_loop_line\"\}"
+    set frame_re(1) ",frame=\{level=\"1\",addr=\"$hex\",func=\"child_function\",args=\\\[\{name=\"args\",value=\"0x0\"\}\\\],file=\".*$srcfile\",.*line=\"$thread_caller_line\"\}"
+
+    # Special frame for main thread.
+    set frame_re(2) ",frame=\{level=\"0\",addr=\"$hex\",func=\".*\",args=\\\[\\\],file=\".*\",fullname=\".*\",line=\".*\"\}"
+
+    if { $thread != -1 } {
+	if { $type == "event" } {
+	    append mi_re $thread_event_re
+	} elseif { $type == "response" } {
+	    append mi_re $thread_answer_re
+	} else {
+	    error "Invalid value for EVENT."
+	}
+    }
+
+    if { $frame != -1 } {
+	append mi_re $frame_re($frame)
+    }
+
+    if { $type == "event" } {
+	append mi_re "\r\n"
+    }
+
+    return $mi_re
+}
+
+# Make a regular expression to match the various inferior/thread/frame selection
+# events when issuing CLI commands inside MI.
+#
+# COMMAND is the CLI command that was sent to GDB, which will be output in the
+#   console output stream.
+# CLI_IN_MI_MODE indicates which method of cli-in-mi command is used.  It can be
+#   either "direct" of "interpreter-exec".
+# MODE can be either "all-stop" or "non-stop", indicating which one is currently
+#   in use.
+# If EVENT is 1, expect a =thread-select MI event.
+# INF is the inferior number we are expecting GDB to switch to, or -1 if we are
+#   not expecting GDB to announce an inferior switch.
+# CLI_THREAD is the thread number as seen in the CLI (inferior-qualified) we are
+#   expecting GDB to switch to, or -1 if we are not expecting GDB to announce a
+#   thread switch.
+# MI_THREAD is the thread number as seen in the MI (global number) we are
+#   expecting GDB to switch to, or -1 if we are not expecting GDB to announce a
+#   thread switch.
+# FRAME is the frame number we are expecting GDB to switch to, or -1 if we are
+#   not expecting GDB to announce a frame switch.  See the FRAME_RE variable for
+#   details.
+
+proc make_cli_in_mi_re { command cli_in_mi_mode mode event inf cli_thread
+		         mi_thread frame  } {
+    global srcfile thread_loop_line thread_caller_line
+
+    set command_re [string_to_regexp $command]
+    set cli_in_mi_re "$command_re\r\n"
+
+    if { $cli_in_mi_mode == "direct" } {
+	append cli_in_mi_re "&\"$command_re\\\\n\"\r\n"
+    }
+
+    set frame_re(0) "~\"#0.*child_sub_function.*$srcfile:$thread_loop_line\\\\n.*thread loop line \\\*/\\\\n\"\r\n"
+    set frame_re(1) "~\"#1.*child_function \\\(args=0x0\\\) at .*$srcfile:$thread_caller_line\\\\n\"\r\n~\"$thread_caller_line.*thread caller line \\\*/\\\\n\"\r\n"
+
+    # Special frame for main thread.
+    set frame_re(2) "~\"#0.*main.*\\\\n\"\r\n"
+
+    if { $inf != -1 } {
+	append cli_in_mi_re "~\""
+	append cli_in_mi_re [make_cli_re $mode $inf -1 -1]
+	append cli_in_mi_re "\\\\n\"\r\n"
+    }
+
+    if { $cli_thread != "-1" } {
+	append cli_in_mi_re "~\""
+	append cli_in_mi_re [make_cli_re $mode -1 $cli_thread -1]
+	append cli_in_mi_re "\\\\n\"\r\n"
+    }
+
+    if { $frame != -1 } {
+	append cli_in_mi_re $frame_re($frame)
+    }
+
+    if { $event == 1 } {
+	append cli_in_mi_re [make_mi_re $mode $mi_thread $frame event]
+    }
+
+    append cli_in_mi_re "\\^done"
+
+    return $cli_in_mi_re
+}
+
+# Continue inferior INF until the breakpoint indicating the threads are started.
+
+proc test_continue_to_start { mode inf } {
+    global gdb_prompt gdb_spawn_id gdb_main_spawn_id
+
+    if { $gdb_spawn_id != $gdb_main_spawn_id } {
+	error "This should not happen."
+    }
+
+    with_test_prefix "inferior $inf" {
+	with_spawn_id $gdb_main_spawn_id {
+	    gdb_continue_to_breakpoint "main breakpoint"
+
+	    if { $mode == "non-stop" } {
+		gdb_test "thread $inf.2" ".*" "switch to thread $inf.2"
+
+		send_gdb "interrupt\n"
+		gdb_expect {
+		    -re "Thread.*2.*stopped" {
+			pass "interrupt thread $inf.2"
+		    }
+		}
+	    }
+	}
+    }
+}
+
+# Prepare the test environment.
+#
+# MODE can be either "all-stop" or "non-stop".
+
+proc test_setup { mode } {
+    global srcfile srcdir subdir testfile
+    global gdb_main_spawn_id mi_spawn_id
+    global decimal binfile main_bp_line
+    global GDBFLAGS
+
+    mi_gdb_exit
+
+    save_vars { GDBFLAGS } {
+	if { $mode == "non-stop" } {
+	    set GDBFLAGS [concat $GDBFLAGS " -ex \"set non-stop 1\""]
+	}
+
+	if { [mi_gdb_start "separate-mi-tty"] != 0 } {
+	    return
+	}
+    }
+
+    mi_delete_breakpoints
+    mi_gdb_reinitialize_dir $srcdir/$subdir
+    mi_gdb_load $binfile
+
+    if { [mi_runto main] < 0 } {
+	fail "Can't run to main"
+	return
+    }
+
+    with_spawn_id $gdb_main_spawn_id {
+	gdb_test "break $srcfile:$main_bp_line" \
+	    "Breakpoint $decimal .*$srcfile, line $main_bp_line\\\." \
+	    "set breakpoint"
+
+	test_continue_to_start $mode 1
+
+	# Add a second inferior.
+	gdb_test "add-inferior" "Added inferior 2" "Add inferior 2"
+	gdb_test "inferior 2" ".*" "switch to inferior 2"
+	gdb_load ${binfile}
+	gdb_test "start" "Temporary breakpoint.*Starting program.*"
+	test_continue_to_start $mode 2
+	gdb_test "inferior 1" ".*" "switch back to inferior 1"
+    }
+}
+
+# Reset the selection to frame #0 of thread THREAD.
+
+proc reset_selection { thread } {
+    global gdb_main_spawn_id
+
+    with_spawn_id $gdb_main_spawn_id {
+	gdb_test "thread $thread" ".*" "reset selection to thread $thread"
+	gdb_test "frame 0" ".*" "reset selection to frame 0"
+    }
+}
+
+# Flush Expect's internal buffers for both CLI and MI.
+#
+# The idea here is to send a command, and to consume all the characters that we
+# expect that command to output, including the following prompt.  Using gdb_test
+# and mi_gdb_test should do that.
+
+proc flush_buffers { } {
+    global gdb_main_spawn_id mi_spawn_id
+
+    with_spawn_id $gdb_main_spawn_id {
+	gdb_test "print 444" "= 444" "flush cli"
+    }
+
+    with_spawn_id $mi_spawn_id {
+	mi_gdb_test "555-data-evaluate-expression 666" ".*done,value=\"666\"" "flush mi"
+    }
+}
+
+# Run a command on the current spawn id, to confirm that no output is pending
+# in Expect's internal buffer.  This is used to ensure that nothing was output
+# on the spawn id since the call to gdb_test/mi_gdb_test/flush_buffers.
+#
+# The key here is that the regexes use start-of-buffer anchors (^), ensuring
+# that they match the entire buffer, confirming that there was nothing in it
+# before.
+
+proc ensure_no_output { test } {
+    global gdb_spawn_id gdb_main_spawn_id mi_spawn_id
+    global decimal
+
+    if { $gdb_spawn_id == $gdb_main_spawn_id } {
+	# CLI
+	gdb_test "print 666" \
+		 "^print 666\r\n\\\$$decimal = 666" \
+		 "$test, ensure no output cli"
+    } elseif { $gdb_spawn_id == $mi_spawn_id } {
+	# MI
+	mi_gdb_test "777-data-evaluate-expression 888" \
+		    "^777-data-evaluate-expression 888\r\n777\\^done,value=\"888\"" \
+		    "$test, ensure no output mi"
+    } else {
+	error "Unexpected gdb_spawn_id value."
+    }
+}
+
+# Match a regular expression, or ensure that there was no output.
+#
+# If RE is non-empty, try to match the content of the program output (using the
+# current spawn_id) and pass/fail TEST accordingly.
+# If RE is empty, ensure that the program did not output anything.
+
+proc match_re_or_ensure_not_output { re test } {
+    if { $re != "" } {
+	gdb_expect {
+	    -re "$re" {
+		pass $test
+	    }
+
+	    default {
+		fail $test
+	    }
+	}
+    } else {
+	ensure_no_output $test
+    }
+}
+
+# Test selecting an inferior from CLI.
+
+proc test_cli_inferior { mode } {
+    global gdb_main_spawn_id mi_spawn_id
+
+    reset_selection "1.1"
+
+    set mi_re [make_mi_re $mode 4 2 event]
+    set cli_re [make_cli_re $mode 2 2.1 2]
+
+    flush_buffers
+
+    # Do the 'inferior' command.
+    with_spawn_id $gdb_main_spawn_id {
+	gdb_test "inferior 2" $cli_re "cli select inferior"
+    }
+
+    with_spawn_id $mi_spawn_id {
+	match_re_or_ensure_not_output $mi_re "event on mi"
+    }
+
+    # Do the 'inferior' command on the currently selected inferior.  For now,
+    # GDB naively re-outputs everything.
+    with_spawn_id $gdb_main_spawn_id {
+	gdb_test "inferior 2" $cli_re "cli select inferior again"
+    }
+
+    with_spawn_id $mi_spawn_id {
+	match_re_or_ensure_not_output $mi_re "event on mi again"
+    }
+}
+
+# Test thread selection from CLI.
+
+proc test_cli_thread { mode } {
+    global gdb_main_spawn_id mi_spawn_id
+
+    reset_selection "1.1"
+    flush_buffers
+
+    with_test_prefix "thread 1.2" {
+	# Do the 'thread' command to select a stopped thread.
+
+	set mi_re [make_mi_re $mode 2 0 event]
+	set cli_re [make_cli_re $mode -1 1.2 0]
+
+	with_spawn_id $gdb_main_spawn_id {
+	    gdb_test "thread 1.2" $cli_re "select thread"
+	}
+
+	with_spawn_id $mi_spawn_id {
+	    match_re_or_ensure_not_output $mi_re "select thread, event on mi "
+	}
+
+	# Do the 'thread' command to select the same thread.  We shouldn't receive
+	# an event on MI, since we won't actually switch thread.
+
+	set mi_re ""
+
+	with_spawn_id $gdb_main_spawn_id {
+	    gdb_test "thread 1.2" $cli_re "select thread again"
+	}
+
+	with_spawn_id $mi_spawn_id {
+	    match_re_or_ensure_not_output $mi_re "select thread, event on mi again"
+	}
+
+	# Try the 'thread' command without arguments.
+
+	set cli_re "\\\[Current thread is 1\\.2.*\\\]"
+	set mi_re ""
+
+	with_spawn_id $gdb_main_spawn_id {
+	    gdb_test "thread" $cli_re "thread without args"
+	}
+
+	with_spawn_id $mi_spawn_id {
+	    match_re_or_ensure_not_output $mi_re "thread without args, event on mi"
+	}
+    }
+
+    with_test_prefix "thread 1.3" {
+	# Do the 'thread' command to select the third thread, stopped on all-stop,
+	# running on non-stop.
+
+	if { $mode == "all-stop" } {
+	    set cli_re [make_cli_re $mode -1 1.3 0]
+	    set mi_re [make_mi_re $mode 3 0 event]
+	} else {
+	    set cli_re [make_cli_re $mode -1 1.3 -1]
+	    set mi_re [make_mi_re $mode 3 -1 event]
+	}
+
+	with_spawn_id $gdb_main_spawn_id {
+	    gdb_test "thread 1.3" $cli_re "select thread"
+	}
+
+	with_spawn_id $mi_spawn_id {
+	    match_re_or_ensure_not_output $mi_re "select thread, event on mi"
+	}
+
+	# Do the 'thread' command to select the third thread again.  Again, we
+	# shouldn't receive an event on MI.
+
+	set mi_re ""
+
+	with_spawn_id $gdb_main_spawn_id {
+	    gdb_test "thread 1.3" $cli_re "select thread again"
+	}
+
+	with_spawn_id $mi_spawn_id {
+	    match_re_or_ensure_not_output $mi_re "select thread again, event on mi"
+	}
+
+	# Try the 'thread' command without arguments.
+
+	set cli_re "\\\[Current thread is 1\\.3.*\\\]"
+	set mi_re ""
+
+	with_spawn_id $gdb_main_spawn_id {
+	    gdb_test "thread" $cli_re "thread without args"
+	}
+
+	with_spawn_id $mi_spawn_id {
+	    match_re_or_ensure_not_output $mi_re "thread without args, event on mi"
+	}
+    }
+
+    # Idea for the future: selecting a thread in a different inferior.  For now,
+    # GDB doesn't show an inferior switch, but if it did, it would be a nice
+    # place to test it.
+}
+
+# Test frame selection from CLI.
+
+proc test_cli_frame { mode } {
+    global gdb_main_spawn_id mi_spawn_id
+
+    with_test_prefix "thread 1.2" {
+	reset_selection "1.2"
+	flush_buffers
+
+	# Do the 'frame' command to select frame 1.
+
+	set mi_re [make_mi_re $mode 2 1 event]
+	set cli_re [make_cli_re $mode -1 -1 1]
+
+	with_spawn_id $gdb_main_spawn_id {
+	    gdb_test "frame 1" $cli_re "select frame 1"
+	}
+
+	with_spawn_id $mi_spawn_id {
+	    match_re_or_ensure_not_output $mi_re "select frame 1, event on mi"
+	}
+
+	# Do the 'frame' command to select the same frame.  This time we don't
+	# expect an event on MI, since we won't actually change frame.
+
+	set mi_re ""
+
+	with_spawn_id $gdb_main_spawn_id {
+	    gdb_test "frame 1" $cli_re "select frame 1 again"
+	}
+
+	with_spawn_id $mi_spawn_id {
+	    match_re_or_ensure_not_output $mi_re "select frame 1 again, event on mi"
+	}
+
+	# Do the 'frame' command without arguments.  We shouldn't see anything on MI.
+
+	with_spawn_id $gdb_main_spawn_id {
+	    gdb_test "frame" $cli_re "frame without args"
+	}
+
+	with_spawn_id $mi_spawn_id {
+	    match_re_or_ensure_not_output $mi_re "frame without args, event on mi"
+	}
+    }
+
+    with_test_prefix "thread 1.3" {
+	# Now, try the 'frame' command on thread 3, which is running if we are in
+	# non-stop mode.
+	reset_selection "1.3"
+	flush_buffers
+
+	if {$mode == "all-stop"} {
+	    set mi_re [make_mi_re $mode 3 1 event]
+	    set cli_re [make_cli_re $mode -1 -1 1]
+	} elseif {$mode == "non-stop"} {
+	    set mi_re ""
+	    set cli_re "Selected thread is running\\."
+	}
+
+	with_spawn_id $gdb_main_spawn_id {
+	    gdb_test "frame 1" $cli_re "select frame 1"
+	}
+
+	with_spawn_id $mi_spawn_id {
+	    match_re_or_ensure_not_output $mi_re "select frame 1, event on mi"
+	}
+
+	# Do the 'frame' command without arguments.
+
+	if { $mode == "non-stop" } {
+	    set cli_re "No stack\\."
+	}
+	set mi_re ""
+
+	with_spawn_id $gdb_main_spawn_id {
+	    gdb_test "frame" $cli_re "frame without args"
+	}
+
+	with_spawn_id $mi_spawn_id {
+	    match_re_or_ensure_not_output $mi_re "frame without args, event on mi"
+	}
+    }
+}
+
+# Test frame selection from CLI with the select-frame command.
+
+proc test_cli_select_frame { mode } {
+    global gdb_main_spawn_id mi_spawn_id expect_out
+
+    with_test_prefix "thread 1.2" {
+	reset_selection "1.2"
+	flush_buffers
+
+	# Do the 'select-frame' command to select frame 1.
+
+	set mi_re [make_mi_re $mode 2 1 event]
+
+	with_spawn_id $gdb_main_spawn_id {
+	    gdb_test_no_output "select-frame 1" "select frame 1"
+	}
+
+	with_spawn_id $mi_spawn_id {
+	    match_re_or_ensure_not_output $mi_re "select frame 1, event on mi"
+	}
+
+	# Do the 'select-frame' command to select the same frame.  This time we expect to
+	# event on MI, since we won't actually change frame.
+
+	set mi_re ""
+
+	with_spawn_id $gdb_main_spawn_id {
+	    gdb_test_no_output "select-frame 1" "select frame 1 again"
+	}
+
+	with_spawn_id $mi_spawn_id {
+	    match_re_or_ensure_not_output $mi_re "select frame 1 again, event on mi"
+	}
+    }
+
+    with_test_prefix "thread 1.3" {
+	# Now, try the 'select-frame' command on thread 3, which is running if we are in
+	# non-stop mode.
+	reset_selection "1.3"
+	flush_buffers
+
+	if {$mode == "all-stop"} {
+	    set mi_re [make_mi_re $mode 3 1 event]
+	} elseif {$mode == "non-stop"} {
+	    set mi-re ""
+	    set cli_re "Selected thread is running\\."
+	}
+
+	with_spawn_id $gdb_main_spawn_id {
+	    if { $mode == "all-stop" } {
+		gdb_test_no_output "select-frame 1" "select frame 1"
+	    } else {
+		gdb_test "select-frame 1" $cli_re "select frame 1"
+	    }
+	}
+
+	with_spawn_id $mi_spawn_id {
+	    match_re_or_ensure_not_output $mi_re "select frame 1, event on mi"
+	}
+    }
+}
+
+# Test doing an up and then down command from cli.
+
+proc test_cli_up_down { mode } {
+    global gdb_main_spawn_id mi_spawn_id
+
+    reset_selection "1.2"
+    flush_buffers
+
+    # Try doing an 'up'.
+
+    set mi_re [make_mi_re $mode 2 1 event]
+    set cli_re [make_cli_re $mode -1 -1 1]
+
+    with_spawn_id $gdb_main_spawn_id {
+	gdb_test "up" $cli_re "frame up"
+    }
+
+    with_spawn_id $mi_spawn_id {
+	match_re_or_ensure_not_output $mi_re "frame up, event on mi"
+    }
+
+    # Try doing a 'down'.
+
+    set mi_re [make_mi_re $mode 2 0 event]
+    set cli_re [make_cli_re $mode -1 -1 0]
+
+    with_spawn_id $gdb_main_spawn_id {
+	gdb_test "down" $cli_re "frame down"
+    }
+
+    with_spawn_id $mi_spawn_id {
+	match_re_or_ensure_not_output $mi_re "frame down, event on mi"
+    }
+}
+
+# Test selecting a thread from MI.
+
+proc test_mi_thread_select { mode } {
+    global gdb_main_spawn_id mi_spawn_id
+
+    reset_selection "1.1"
+    flush_buffers
+
+    with_test_prefix "thread 1.2" {
+	# Do the '-thread-select' command to select a stopped thread.
+
+	set mi_re [make_mi_re $mode 2 0 response]
+	set cli_re [make_cli_re $mode -1 1.2 0]
+
+	with_spawn_id $mi_spawn_id {
+	    mi_gdb_test "-thread-select 2" $mi_re "-thread-select"
+	}
+
+	with_spawn_id $gdb_main_spawn_id {
+	    match_re_or_ensure_not_output "$cli_re\r\n" "-thread-select, event on cli"
+	}
+
+	# Do the '-thread-select' command to select the same thread.  We
+	# shouldn't receive an event on CLI, since we won't actually switch
+	# thread.
+
+	set cli_re ""
+
+	with_spawn_id $mi_spawn_id {
+	    mi_gdb_test "-thread-select 2" $mi_re "-thread-select again"
+	}
+
+	with_spawn_id $gdb_main_spawn_id {
+	    match_re_or_ensure_not_output $cli_re "-thread-select again, event on cli"
+	}
+    }
+
+    with_test_prefix "thread 1.3" {
+	# Do the '-thread-select' command to select the third thread, stopped on all-stop,
+	# running on non-stop.
+
+	if { $mode == "all-stop" } {
+	    set mi_re [make_mi_re $mode 3 0 response]
+	    set cli_re [make_cli_re $mode -1 1.3 0]
+	} else {
+	    set mi_re [make_mi_re $mode 3 -1 response]
+	    set cli_re [make_cli_re $mode -1 1.3 -1]
+	}
+
+	with_spawn_id $mi_spawn_id {
+	    mi_gdb_test "-thread-select 3" $mi_re "-thread-select"
+	}
+
+	with_spawn_id $gdb_main_spawn_id {
+	    match_re_or_ensure_not_output "$cli_re\r\n" "-thread-select, event on cli"
+	}
+
+	# Do the 'thread' command to select the third thread again.  Again, we
+	# shouldn't receive an event on MI.
+
+	set cli_re ""
+
+	with_spawn_id $mi_spawn_id {
+	    mi_gdb_test "-thread-select 3" $mi_re "-thread-select again"
+	}
+
+	with_spawn_id $gdb_main_spawn_id {
+	    match_re_or_ensure_not_output $cli_re "-thread-select again, event on cli"
+	}
+    }
+
+    with_test_prefix "thread 1.2 with --thread" {
+	# Test selecting a thread from MI with a --thread option.  This test
+	# verifies that even if the thread GDB would switch to is the same has
+	# the thread specified with --thread, an event is still sent to CLI.
+	# In this case this is thread 1.2
+
+	set mi_re [make_mi_re $mode 2 0 response]
+	set cli_re [make_cli_re $mode -1 1.2 0]
+
+	with_spawn_id $mi_spawn_id {
+	    mi_gdb_test "-thread-select --thread 2 2" $mi_re "-thread-select"
+	}
+
+	with_spawn_id $gdb_main_spawn_id {
+	    # TODO: it doesn't work as of now.
+	    # match_re_or_ensure_not_output "$cli_re\r\n" "-thread-select, event on cli"
+	}
+    }
+
+    # Idea for the future: selecting a thread in a different inferior.  For now,
+    # GDB doesn't show an inferior switch, but if it did, it would be a nice
+    # place to test it.
+}
+
+proc test_mi_stack_select_frame { mode } {
+    global gdb_main_spawn_id mi_spawn_id
+
+    with_test_prefix "thread 1.2" {
+	reset_selection "1.2"
+	flush_buffers
+
+	# Do the '-stack-select-frame' command to select frame 1.
+
+	set mi_re "\\^done"
+	set cli_re [make_cli_re $mode -1 -1 1]
+
+	with_spawn_id $mi_spawn_id {
+	    mi_gdb_test "-stack-select-frame 1" $mi_re "-stack-select-frame"
+	}
+
+	with_spawn_id $gdb_main_spawn_id {
+	    match_re_or_ensure_not_output "$cli_re\r\n" "-stack-select-frame, event on mi"
+	}
+
+	# Do the '-stack-select-frame' command to select the same frame.  This time we don't
+	# expect an event on CLI, since we won't actually change frame.
+
+	set cli_re ""
+
+	with_spawn_id $mi_spawn_id {
+	    mi_gdb_test "-stack-select-frame 1" $mi_re "-stack-select-frame again"
+	}
+
+	with_spawn_id $gdb_main_spawn_id {
+	    match_re_or_ensure_not_output $cli_re "-stack-select-frame again, event on mi"
+	}
+    }
+
+    with_test_prefix "thread 1.3" {
+	# Now, try the '-stack-select-frame' command on thread 3, which is
+	# running if we are in non-stop mode.
+	reset_selection "1.3"
+	flush_buffers
+
+	if {$mode == "all-stop"} {
+	    set mi_re "\\^done"
+	    set cli_re [make_cli_re $mode -1 -1 1]
+	    append cli_re "\r\n"
+	} elseif {$mode == "non-stop"} {
+	    set cli_re ""
+	    set mi_re ".*\\^error,msg=\"Selected thread is running\\.\""
+	}
+
+	with_spawn_id $mi_spawn_id {
+	    mi_gdb_test "-stack-select-frame 1" $mi_re "-stack-select-frame"
+	}
+
+	with_spawn_id $gdb_main_spawn_id {
+	    match_re_or_ensure_not_output $cli_re "-stack-select-frame, event on mi"
+	}
+    }
+}
+
+proc make_cli_in_mi_command { cli_in_mi_mode command } {
+    if { $cli_in_mi_mode == "direct" } {
+	return $command
+    } elseif { $cli_in_mi_mode == "interpreter-exec" } {
+	return "-interpreter-exec console \"$command\""
+    } else {
+	error "Invalid value for CLI_IN_MI_MODE."
+    }
+}
+
+# Test selecting the inferior using a cli command in the mi channel.
+
+proc test_cli_in_mi_inferior { mode cli_in_mi_mode } {
+    global gdb_main_spawn_id mi_spawn_id
+
+    reset_selection "1.1"
+    flush_buffers
+
+    set command [make_cli_in_mi_command $cli_in_mi_mode "inferior 2"]
+
+    set mi_re [make_cli_in_mi_re $command $cli_in_mi_mode $mode 1 2 2.1 4 2]
+    set cli_re [make_cli_re $mode 2 "2.1" 2]
+
+    with_spawn_id $mi_spawn_id {
+	mi_gdb_test $command $mi_re "select inferior"
+    }
+
+    with_spawn_id $gdb_main_spawn_id {
+	match_re_or_ensure_not_output "$cli_re\r\n" "select inferior, event on cli"
+    }
+
+    # Do the 'inferior' command on the currently selected inferior.  For now,
+    # GDB naively re-outputs everything.
+    with_spawn_id $mi_spawn_id {
+	mi_gdb_test $command $mi_re "select inferior again"
+    }
+
+    with_spawn_id $gdb_main_spawn_id {
+	match_re_or_ensure_not_output $cli_re "select inferior again, event on cli"
+    }
+}
+
+# Test selecting the thread using a CLI command in the MI channel.
+
+proc test_cli_in_mi_thread { mode cli_in_mi_mode } {
+    global gdb_main_spawn_id mi_spawn_id
+
+    reset_selection "1.1"
+    flush_buffers
+
+    with_test_prefix "thread 1.2" {
+	# Do the 'thread' command to select a stopped thread.
+
+	set command [make_cli_in_mi_command $cli_in_mi_mode "thread 1.2"]
+	set mi_re [make_cli_in_mi_re $command $cli_in_mi_mode $mode 1 -1 1.2 2 0]
+	set cli_re [make_cli_re $mode -1 1.2 0]
+
+	with_spawn_id $mi_spawn_id {
+	    mi_gdb_test $command $mi_re "select thread"
+	}
+
+	with_spawn_id $gdb_main_spawn_id {
+	    match_re_or_ensure_not_output "$cli_re\r\n" "select thread, event on cli"
+	}
+
+	# Do the 'thread' command to select the same thread.  We shouldn't
+	# receive an event on CLI, since we won't actually switch thread.
+
+	set mi_re [make_cli_in_mi_re $command $cli_in_mi_mode $mode 0 -1 1.2 2 0]
+	set cli_re ""
+
+	with_spawn_id $mi_spawn_id {
+	    mi_gdb_test $command $mi_re "select thread again"
+	}
+
+	with_spawn_id $gdb_main_spawn_id {
+	    match_re_or_ensure_not_output $cli_re "select thread again, event on cli"
+	}
+
+	# Try the 'thread' command without arguments.
+
+	set command [make_cli_in_mi_command $cli_in_mi_mode "thread"]
+
+	set mi_re "${command}.*~\"\\\[Current thread is 1\\.2.*\\\]\\\\n\".*\\^done"
+	set cli_re ""
+
+	with_spawn_id $mi_spawn_id {
+	    mi_gdb_test $command $mi_re "thread without args"
+	}
+
+	with_spawn_id $gdb_main_spawn_id {
+	    match_re_or_ensure_not_output $cli_re "thread without args, event on cli"
+	}
+    }
+
+    with_test_prefix "thread 1.3" {
+	# Do the 'thread' command to select the third thread, stopped on
+	# all-stop, running on non-stop.
+
+	set command [make_cli_in_mi_command $cli_in_mi_mode "thread 1.3"]
+	if { $mode == "all-stop" } {
+	    set mi_re [make_cli_in_mi_re $command $cli_in_mi_mode $mode 1 -1 1.3 3 0]
+	    set cli_re [make_cli_re $mode -1 "1.3" 0]
+	} else {
+	    set mi_re [make_cli_in_mi_re $command $cli_in_mi_mode $mode 1 -1 1.3 3 -1]
+	    set cli_re [make_cli_re $mode -1 "1.3" -1]
+	}
+
+	with_spawn_id $mi_spawn_id {
+	    mi_gdb_test $command $mi_re "select thread"
+	}
+
+	with_spawn_id $gdb_main_spawn_id {
+	    match_re_or_ensure_not_output "$cli_re\r\n" "select thread, event on cli"
+	}
+
+	# Do the 'thread' command to select the third thread again.  Again, we
+	# shouldn't receive an event on MI.
+
+	if { $mode == "all-stop" } {
+	    set mi_re [make_cli_in_mi_re $command $cli_in_mi_mode $mode 0 -1 1.3 3 0]
+	} else {
+	    set mi_re [make_cli_in_mi_re $command $cli_in_mi_mode $mode 0 -1 1.3 3 -1]
+	}
+	set cli_re ""
+
+	with_spawn_id $mi_spawn_id {
+	    mi_gdb_test $command $mi_re "select thread again"
+	}
+
+	with_spawn_id $gdb_main_spawn_id {
+	    match_re_or_ensure_not_output $cli_re "select thread again, event on cli"
+	}
+
+	# Try the 'thread' command without arguments.
+
+	set command [make_cli_in_mi_command $cli_in_mi_mode "thread"]
+
+	set mi_re "${command}.*~\"\\\[Current thread is 1\\.3.*\\\]\\\\n\".*\\^done"
+	set cli_re ""
+
+	with_spawn_id $mi_spawn_id {
+	    mi_gdb_test $command $mi_re "thread without args"
+	}
+
+	with_spawn_id $gdb_main_spawn_id {
+	    match_re_or_ensure_not_output $cli_re "thread without args, event on cli"
+	}
+    }
+
+    # Idea for the future: selecting a thread in a different inferior.  For now,
+    # GDB doesn't show an inferior switch, but if it did, it would be a nice
+    # place to test it.
+}
+
+# Test selecting the frame using a CLI command in the MI channel.
+
+proc test_cli_in_mi_frame { mode cli_in_mi_mode } {
+    global gdb_main_spawn_id mi_spawn_id
+
+    with_test_prefix "thread 1.2" {
+	reset_selection "1.2"
+	flush_buffers
+
+	# Do the 'frame' command to select frame 1.
+
+	set command [make_cli_in_mi_command $cli_in_mi_mode "frame 1"]
+	set cli_re [make_cli_re $mode -1 -1 1]
+	set mi_re [make_cli_in_mi_re $command $cli_in_mi_mode $mode 1 -1 -1 2 1]
+
+	with_spawn_id $mi_spawn_id {
+	    mi_gdb_test $command $mi_re "select frame 1"
+	}
+
+	with_spawn_id $gdb_main_spawn_id {
+	    match_re_or_ensure_not_output "$cli_re\r\n" "select frame 1, event on cli"
+	}
+
+	# Do the 'frame' command to select the same frame.  This time we don't
+	# expect an event on MI, since we won't actually change frame.
+
+	set mi_re [make_cli_in_mi_re $command $cli_in_mi_mode $mode 0 -1 -1 2 1]
+	set cli_re ""
+
+	with_spawn_id $mi_spawn_id {
+	    mi_gdb_test $command $mi_re "select frame 1 again"
+	}
+
+	with_spawn_id $gdb_main_spawn_id {
+	    match_re_or_ensure_not_output $cli_re "select frame 1 again, event on cli"
+	}
+
+	# Do the 'frame' command without arguments.  We shouldn't see anything on MI.
+
+	set command [make_cli_in_mi_command $cli_in_mi_mode "frame"]
+	set mi_re [make_cli_in_mi_re $command $cli_in_mi_mode $mode 0 -1 -1 2 1]
+
+	with_spawn_id $mi_spawn_id {
+	    mi_gdb_test $command $mi_re "frame without args"
+	}
+
+	with_spawn_id $gdb_main_spawn_id {
+	    match_re_or_ensure_not_output $cli_re "frame without args, event on cli"
+	}
+    }
+
+    with_test_prefix "thread 1.3" {
+	# Now, try the 'frame' command on thread 3, which is running if we are in
+	# non-stop mode.
+	reset_selection "1.3"
+	flush_buffers
+
+	set command [make_cli_in_mi_command $cli_in_mi_mode "frame 1"]
+	if {$mode == "all-stop"} {
+	    set cli_re [make_cli_re $mode -1 -1 1]
+	    append cli_re "\r\n"
+	    set mi_re [make_cli_in_mi_re $command $cli_in_mi_mode $mode 1 -1 -1 3 1]
+	} elseif {$mode == "non-stop"} {
+	    set cli_re ""
+	    set mi_re ".*msg=\"Selected thread is running\\.\".*"
+	}
+
+	with_spawn_id $mi_spawn_id {
+	    mi_gdb_test $command $mi_re "select frame 1"
+	}
+
+	with_spawn_id $gdb_main_spawn_id {
+	    match_re_or_ensure_not_output $cli_re "select frame 1, event on cli"
+	}
+
+	# Do the 'frame' command without arguments.
+
+	set command [make_cli_in_mi_command $cli_in_mi_mode "frame"]
+	if { $mode == "all-stop" } {
+	    set mi_re [make_cli_in_mi_re $command $cli_in_mi_mode $mode 0 -1 -1 -1 1]
+	} else {
+	    set mi_re ".*\\^error,msg=\"No stack\\.\""
+	}
+	set cli_re ""
+
+	with_spawn_id $mi_spawn_id {
+	    mi_gdb_test $command $mi_re "frame without args"
+	}
+
+	with_spawn_id $gdb_main_spawn_id {
+	    match_re_or_ensure_not_output $cli_re "frame without args, event on cli"
+	}
+    }
+}
+
+foreach_with_prefix mode { "all-stop" "non-stop" } {
+    with_test_prefix_procname test_setup $mode
+
+    # Test selecting inferior, thread and frame from CLI
+
+    with_test_prefix_procname test_cli_inferior $mode
+    with_test_prefix_procname test_cli_thread $mode
+    with_test_prefix_procname test_cli_frame $mode
+    with_test_prefix_procname test_cli_select_frame $mode
+    with_test_prefix_procname test_cli_up_down $mode
+
+    # Test selecting thread and frame from MI
+
+    with_test_prefix_procname test_mi_thread_select $mode
+    with_test_prefix_procname test_mi_stack_select_frame $mode
+
+    # Test some CLI commands sent through MI, both with a "direct" command,
+    # such as "thread 1", and with -interpreter-exec, such as
+    # '-interpreter-exec console "thread 1"'.
+
+    foreach_with_prefix exec_mode {"direct" "interpreter-exec"} {
+	with_test_prefix_procname test_cli_in_mi_inferior $mode $exec_mode
+	with_test_prefix_procname test_cli_in_mi_thread $mode $exec_mode
+	with_test_prefix_procname test_cli_in_mi_frame $mode $exec_mode
+    }
+}
-- 
2.9.3

^ permalink raw reply	[flat|nested] 21+ messages in thread

* [PATCH master+7.12 v2 2/3] Emit inferior, thread and frame selection events to all UIs
  2016-09-14 17:46 [PATCH master+7.12 v2 0/3] Emit user selection change notifications on all UIs Simon Marchi
  2016-09-14 17:46 ` [PATCH master+7.12 v2 1/3] Introduce cleanup to restore current_uiout Simon Marchi
  2016-09-14 17:46 ` [PATCH master+7.12 v2 3/3] Add test for user context selection sync Simon Marchi
@ 2016-09-14 17:46 ` Simon Marchi
  2016-09-14 18:30   ` Pedro Alves
  2 siblings, 1 reply; 21+ messages in thread
From: Simon Marchi @ 2016-09-14 17:46 UTC (permalink / raw)
  To: gdb-patches; +Cc: Antoine Tremblay

From: Antoine Tremblay <antoine.tremblay@ericsson.com>

With this patch, when an inferior, thread or frame is explicitly
selected by the user, notifications will appear on all CLI and MI UIs.
When a GDB console is integrated in a front-end, this allows the
front-end to follow a selection made by the user ont he CLI, and it
informs the user about selection changes made behind the scenes by the
front-end.

This patch fixes PR gdb/20487.

In order to communicate frame changes to the front-end, this patch adds
a new field to the =thread-selected event for the selected frame.  The
idea is that since inferior/thread/frame can be seen as a composition,
it makes sense to send them together in the same event.  The vision
would be to eventually send the inferior information as well, if we find
that it's needed, although the "=thread-selected" event would be
ill-named for that job.

Front-ends need to handle this new field if they want to follow the
frame selection changes that originate from the console.  The format of
the frame attribute is the same as what is found in the *stopped events.

Here's a detailed example for each command and the events they generate:

thread
------

1. CLI command:

     thread 1.3

   MI event:

     =thread-selected,id="3",frame={...}

2. MI command:

     -thread-select 3

   CLI event:

     [Switching to thread 1.3 ...]

3. MI command (CLI-in-MI):

     thread 1.3

   MI event/reply:

     &"thread 1.3\n"
     ~"#0  child_sub_function () ...
     =thread-selected,id="3",frame={level="0",...}
     ^done

frame
-----

1. CLI command:

     frame 1

   MI event:

     =thread-selected,id="3",frame={level="1",...}

2. MI command:

     -stack-select-frame 1

   CLI event:

     #1  0x00000000004007f0 in child_function...

3. MI command (CLI-in-MI):

     frame 1

   MI event/reply:

     &"frame 1\n"
     ~"#1  0x00000000004007f9 in ..."
     =thread-selected,id="3",frame={level="1"...}
     ^done

inferior
--------

Inferior selection events only go from the console to MI, since there's
no way to select the inferior in pure MI.

1. CLI command:

     inferior 2

   MI event:

     =thread-selected,id="3"

Note that if the user selects an inferior that is not started or exited,
the MI doesn't receive a notification.  Since there is no threads to
select, the =thread-selected event does not apply...

2. MI command (CLI-in-MI):

     inferior 2

   MI event/reply:

     &"inferior 2\n"
     ~"[Switching to inferior 2 ...]"
     =thread-selected,id="4",frame={level="0"...}
     ^done

Internal implementation detail: this patch makes it possible to suppress
notifications caused by a CLI command, like what is done in mi-interp.c.
This means that it's now possible to use the
add_com_suppress_notification function to register a command with some
event suppressed.  It is used to implement the select-frame command in
this patch.

No regressions, tested on ubuntu 14.04 x86 with target boards unix and
native-extended-gdbserver.

gdb/ChangeLog:

YYYY-MM-DD  Antoine Tremblay  <antoine.tremblay@ericsson.com>
YYYY-MM-DD  Simon Marchi  <simon.marchi@ericsson.com>

	PR gdb/20487
	* NEWS: Mention new frame field of =thread-selected event.
	* cli/cli-decode.c (add_cmd): Initialize c->suppress_notification.
	(add_com_suppress_notification): New function definition.
	(cmd_func): Set and restore the suppress_notification flag.
	* cli/cli-deicode.h (struct cmd_list_element)
	<suppress_notification>: New field.
	* cli/cli-interp.c (cli_suppress_notification): New global variable.
	(cli_on_user_selected_context_changed): New function.
	(_initialize_cli_interp): Attach to user_selected_context_changed
	observer.
	* command.h (struct cli_suppress_notification): New structure.
	(cli_suppress_notification): New global variable declaration.
	(add_com_suppress_notification): New function declaration.
	* defs.h (enum user_selected_what_flag): New enum.
	(user_selected_what): New enum flag type.
	* frame.h (print_stack_frame_to_uiout): New function declaration.
	* gdbthread.h (print_selected_thread_frame): New function declaration.
	* inferior.c (print_selected_inferior): New function definition.
	(inferior_command): Remove printing of inferior/thread/frame switch
	notifications, notify user_selected_context_changed observer.
	* inferior.h (print_selected_inferior): New function declaration.
	* mi/mi-cmds.c (struct mi_cmd): Add user_selected_context
	suppression to stack-select-frame and thread-select commands.
	* mi/mi-interp.c (struct mi_suppress_notification)
	<user_selected_context>: Initialize.
	(mi_user_selected_context_changed): New function definition.
	(_initialize_mi_interp): Attach to user_selected_context_changed.
	* mi/mi-main.c (mi_cmd_thread_select): Print thread selection reply.
	(mi_execute_command): Handle notification suppression.  Notify
	user_selected_context_changed observer on thread change instead of printing
	event directly.  Don't do it for thread and inferior CLI commands.
	(mi_cmd_execute): Don,t handle notification suppression.
	* mi/mi-main.h (struct mi_suppress_notification)
	<user_selected_context>: New field.
	* stack.c (print_stack_frame_to_uiout): New function definition.
	(select_frame_command): Notify user_selected_context_changed
	observer.
	(frame_command): Call print_selected_thread_frame if there's no frame
	change or notify user_selected_context_changed observer if there is.
	(up_command): Notify user_selected_context_changed observer.
	(down_command): Likewise.
	(_initialize_stack): Suppress user_selected_context notification for
	command select-frame.
	* thread.c (thread_command): Notify
	user_selected_context_changed if the thread has changed, print
	thread info directly if it hasn't.
	(do_captured_thread_select): Do not print thread switch event.
	(print_selected_thread_frame): New function definition.
	* tui/tui-interp.c (tui_on_user_selected_context_changed):
	New function definition.
	(_initialize_tui_interp): Attach to user_selected_context_changed
	observer.

gdb/doc/ChangeLog:

	PR gdb/20487
	* gdb.texinfo (Context management): Update mention of frame
	change notifications.
	(gdb/mi Async Records): Document frame field in
	=thread-select event.
	* observer.texi (GDB Observers): New user_selected_context_changed
	observer.

gdb/testsuite/ChangeLog:

	PR gdb/20487
	* gdb.mi/mi-pthreads.exp (check_mi_thread_command_set): Adapt
	=thread-select-event check.
---
 gdb/NEWS                             |  4 ++
 gdb/cli/cli-decode.c                 | 32 +++++++++++++-
 gdb/cli/cli-decode.h                 |  6 +++
 gdb/cli/cli-interp.c                 | 38 ++++++++++++++++
 gdb/command.h                        | 16 +++++++
 gdb/defs.h                           | 16 +++++++
 gdb/doc/gdb.texinfo                  | 33 ++++++++------
 gdb/doc/observer.texi                |  4 ++
 gdb/frame.h                          |  8 ++++
 gdb/gdbthread.h                      |  4 ++
 gdb/inferior.c                       | 40 ++++++++++-------
 gdb/inferior.h                       |  3 ++
 gdb/mi/mi-cmds.c                     |  6 ++-
 gdb/mi/mi-interp.c                   | 61 +++++++++++++++++++++++++
 gdb/mi/mi-main.c                     | 55 ++++++++++++++---------
 gdb/mi/mi-main.h                     |  2 +
 gdb/stack.c                          | 42 +++++++++++++++---
 gdb/testsuite/gdb.mi/mi-pthreads.exp |  4 +-
 gdb/thread.c                         | 86 +++++++++++++++++++++++++++---------
 gdb/tui/tui-interp.c                 | 33 ++++++++++++++
 20 files changed, 405 insertions(+), 88 deletions(-)

diff --git a/gdb/NEWS b/gdb/NEWS
index 99e1013..b004627 100644
--- a/gdb/NEWS
+++ b/gdb/NEWS
@@ -148,6 +148,10 @@ signal-event EVENTID
 
     =record-started,thread-group="i1",method="btrace",format="bts"
 
+* MI async record =thread-selected now includes the frame field.  For example:
+
+     =thread-selected,id="3",frame={level="0",addr="0x00000000004007c0"}
+
 * New targets
 
 Andes NDS32			nds32*-*-elf
diff --git a/gdb/cli/cli-decode.c b/gdb/cli/cli-decode.c
index 0d2b137..1e4a438 100644
--- a/gdb/cli/cli-decode.c
+++ b/gdb/cli/cli-decode.c
@@ -253,6 +253,7 @@ add_cmd (const char *name, enum command_class theclass, cmd_cfunc_ftype *fun,
   c->user_commands = NULL;
   c->cmd_pointer = NULL;
   c->alias_chain = NULL;
+  c->suppress_notification = NULL;
 
   return c;
 }
@@ -883,7 +884,22 @@ add_com_alias (const char *name, const char *oldname, enum command_class theclas
 {
   return add_alias_cmd (name, oldname, theclass, abbrev_flag, &cmdlist);
 }
-\f
+
+/* Add an element with a suppress notification to the list of commands.  */
+
+struct cmd_list_element *
+add_com_suppress_notification (const char *name, enum command_class theclass,
+			       cmd_cfunc_ftype *fun, const char *doc,
+			       int *suppress_notification)
+{
+  struct cmd_list_element *element;
+
+  element = add_cmd (name, theclass, fun, doc, &cmdlist);
+  element->suppress_notification = suppress_notification;
+
+  return element;
+}
+
 /* Recursively walk the commandlist structures, and print out the
    documentation of commands that match our regex in either their
    name, or their documentation.
@@ -1885,7 +1901,19 @@ void
 cmd_func (struct cmd_list_element *cmd, char *args, int from_tty)
 {
   if (cmd_func_p (cmd))
-    (*cmd->func) (cmd, args, from_tty);
+    {
+      struct cleanup *cleanups = make_cleanup (null_cleanup, NULL);
+
+      if (cmd->suppress_notification != NULL)
+	{
+	  cleanups = make_cleanup_restore_integer (cmd->suppress_notification);
+	  *cmd->suppress_notification = 1;
+	}
+
+      (*cmd->func) (cmd, args, from_tty);
+
+      do_cleanups (cleanups);
+    }
   else
     error (_("Invalid command"));
 }
diff --git a/gdb/cli/cli-decode.h b/gdb/cli/cli-decode.h
index 4ea8063..4ef2e1b 100644
--- a/gdb/cli/cli-decode.h
+++ b/gdb/cli/cli-decode.h
@@ -218,6 +218,12 @@ struct cmd_list_element
 
     /* Link pointer for aliases on an alias list.  */
     struct cmd_list_element *alias_chain;
+
+    /* If non-null, the pointer to a field in 'struct
+       cli_suppress_notification', which will be set to true in cmd_func
+       when this command is being executed.  It will be set back to false
+       when the command has been executed.  */
+    int *suppress_notification;
   };
 
 extern void help_cmd_list (struct cmd_list_element *, enum command_class,
diff --git a/gdb/cli/cli-interp.c b/gdb/cli/cli-interp.c
index 5d67ba4..cc556a4 100644
--- a/gdb/cli/cli-interp.c
+++ b/gdb/cli/cli-interp.c
@@ -37,6 +37,12 @@ struct cli_interp
   struct ui_out *cli_uiout;
 };
 
+/* Suppress notification struct.  */
+struct cli_suppress_notification cli_suppress_notification =
+  {
+    0   /* user_selected_context_changed */
+  };
+
 /* Returns the INTERP's data cast as cli_interp if INTERP is a CLI,
    and returns NULL otherwise.  */
 
@@ -229,6 +235,36 @@ cli_on_command_error (void)
   display_gdb_prompt (NULL);
 }
 
+/* Observer for the user_selected_context_changed notification.  */
+
+static void
+cli_on_user_selected_context_changed (user_selected_what selection)
+{
+  struct switch_thru_all_uis state;
+  struct thread_info *tp;
+
+  /* This event is suppressed.  */
+  if (cli_suppress_notification.user_selected_context)
+    return;
+
+  tp = find_thread_ptid (inferior_ptid);
+
+  SWITCH_THRU_ALL_UIS (state)
+    {
+      struct cli_interp *cli = as_cli_interp (top_level_interpreter ());
+
+      if (cli == NULL)
+	continue;
+
+      if (selection & USER_SELECTED_INFERIOR)
+	print_selected_inferior (cli->cli_uiout);
+
+      if (tp != NULL
+	  && ((selection & (USER_SELECTED_THREAD | USER_SELECTED_FRAME))))
+	print_selected_thread_frame (cli->cli_uiout, selection);
+    }
+}
+
 /* pre_command_loop implementation.  */
 
 void
@@ -393,4 +429,6 @@ _initialize_cli_interp (void)
   observer_attach_no_history (cli_on_no_history);
   observer_attach_sync_execution_done (cli_on_sync_execution_done);
   observer_attach_command_error (cli_on_command_error);
+  observer_attach_user_selected_context_changed
+    (cli_on_user_selected_context_changed);
 }
diff --git a/gdb/command.h b/gdb/command.h
index ab62601..965d91f 100644
--- a/gdb/command.h
+++ b/gdb/command.h
@@ -115,6 +115,17 @@ struct cmd_list_element;
 
 typedef void cmd_cfunc_ftype (char *args, int from_tty);
 
+/* This structure specifies notifications to be suppressed by a cli
+   command interpreter.  */
+
+struct cli_suppress_notification
+{
+  /* Inferior, thread, frame selected notification suppressed?  */
+  int user_selected_context;
+};
+
+extern struct cli_suppress_notification cli_suppress_notification;
+
 /* Forward-declarations of the entry-points of cli/cli-decode.c.  */
 
 /* API to the manipulation of command lists.  */
@@ -218,6 +229,11 @@ extern struct cmd_list_element *add_com (const char *, enum command_class,
 extern struct cmd_list_element *add_com_alias (const char *, const char *,
 					       enum command_class, int);
 
+extern struct cmd_list_element *add_com_suppress_notification
+		       (const char *name, enum command_class theclass,
+			cmd_cfunc_ftype *fun, const char *doc,
+			int *supress_notification);
+
 extern struct cmd_list_element *add_info (const char *,
 					  cmd_cfunc_ftype *fun,
 					  const char *);
diff --git a/gdb/defs.h b/gdb/defs.h
index fee5f41..9b44f44 100644
--- a/gdb/defs.h
+++ b/gdb/defs.h
@@ -53,6 +53,7 @@
 #include "ui-file.h"
 
 #include "host-defs.h"
+#include "common/enum-flags.h"
 
 /* Scope types enumerator.  List the types of scopes the compiler will
    accept.  */
@@ -750,6 +751,21 @@ enum block_enum
   FIRST_LOCAL_BLOCK = 2
 };
 
+/* User selection used in observer.h and multiple print functions.  */
+
+enum user_selected_what_flag
+  {
+    /* Inferior selected.  */
+    USER_SELECTED_INFERIOR = 1 << 1,
+
+    /* Thread selected.  */
+    USER_SELECTED_THREAD = 1 << 2,
+
+    /* Frame selected.  */
+    USER_SELECTED_FRAME = 1 << 3
+  };
+DEF_ENUM_FLAGS_TYPE (enum user_selected_what_flag, user_selected_what);
+
 #include "utils.h"
 
 #endif /* #ifndef DEFS_H */
diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo
index d1a5e7c..7139204 100644
--- a/gdb/doc/gdb.texinfo
+++ b/gdb/doc/gdb.texinfo
@@ -25807,13 +25807,13 @@ identifier for thread and frame to operate on.
 Usually, each top-level window in a frontend allows the user to select
 a thread and a frame, and remembers the user selection for further
 operations.  However, in some cases @value{GDBN} may suggest that the
-current thread be changed.  For example, when stopping on a breakpoint
-it is reasonable to switch to the thread where breakpoint is hit.  For
-another example, if the user issues the CLI @samp{thread} command via
-the frontend, it is desirable to change the frontend's selected thread to the
-one specified by user.  @value{GDBN} communicates the suggestion to
-change current thread using the @samp{=thread-selected} notification.
-No such notification is available for the selected frame at the moment.
+current thread or frame be changed.  For example, when stopping on a
+breakpoint it is reasonable to switch to the thread where breakpoint is
+hit.  For another example, if the user issues the CLI @samp{thread} or
+@samp{frame} commands via the frontend, it is desirable to change the
+frontend's selection to the one specified by user.  @value{GDBN}
+communicates the suggestion to change current thread and frame using the
+@samp{=thread-selected} notification.
 
 Note that historically, MI shares the selected thread with CLI, so 
 frontends used the @code{-thread-select} to execute commands in the
@@ -26459,13 +26459,18 @@ A thread either was created, or has exited.  The @var{id} field
 contains the global @value{GDBN} identifier of the thread.  The @var{gid}
 field identifies the thread group this thread belongs to.
 
-@item =thread-selected,id="@var{id}"
-Informs that the selected thread was changed as result of the last
-command.  This notification is not emitted as result of @code{-thread-select}
-command but is emitted whenever an MI command that is not documented
-to change the selected thread actually changes it.  In particular,
-invoking, directly or indirectly (via user-defined command), the CLI
-@code{thread} command, will generate this notification.
+@item =thread-selected,id="@var{id}"[,frame="@var{frame}"]
+Informs that the selected thread or frame were changed.  This notification
+is not emitted as result of the @code{-thread-select} or
+@code{-stack-select-frame} commands, but is emitted whenever an MI command
+that is not documented to change the selected thread and frame actually
+changes them.  In particular, invoking, directly or indirectly
+(via user-defined command), the CLI @code{thread} or @code{frame} commands,
+will generate this notification.  Changing the thread of frame from another
+user interface (see @ref{Interpreters}) will also generate this notification.
+
+The @var{frame} field is only present if the newly selected thread is
+stopped.  See @ref{GDB/MI Frame Information} for the format of its value.
 
 We suggest that in response to this notification, front ends
 highlight the selected thread and cause subsequent commands to apply to
diff --git a/gdb/doc/observer.texi b/gdb/doc/observer.texi
index fc7aac4..6ef27b7 100644
--- a/gdb/doc/observer.texi
+++ b/gdb/doc/observer.texi
@@ -307,3 +307,7 @@ This observer is used for internal testing.  Do not use.
 See testsuite/gdb.gdb/observer.exp.
 @end deftypefun
 
+@deftypefun void user_selected_context_changed (user_selected_what @var{selection})
+The user-selected inferior, thread and/or frame has changed.  The user_select_what
+flag specifies if the inferior, thread and/or frame has changed.
+@end deftypefun
diff --git a/gdb/frame.h b/gdb/frame.h
index 5f21bb8..de13e7d 100644
--- a/gdb/frame.h
+++ b/gdb/frame.h
@@ -704,6 +704,14 @@ extern CORE_ADDR get_pc_function_start (CORE_ADDR);
 
 extern struct frame_info *find_relative_frame (struct frame_info *, int *);
 
+/* Wrapper over print_stack_frame modifying current_uiout with UIOUT for
+   the function call.  */
+
+extern void print_stack_frame_to_uiout (struct ui_out *uiout,
+					struct frame_info *, int print_level,
+					enum print_what print_what,
+					int set_current_sal);
+
 extern void print_stack_frame (struct frame_info *, int print_level,
 			       enum print_what print_what,
 			       int set_current_sal);
diff --git a/gdb/gdbthread.h b/gdb/gdbthread.h
index af2dc86..8f37fbb 100644
--- a/gdb/gdbthread.h
+++ b/gdb/gdbthread.h
@@ -630,6 +630,10 @@ extern void validate_registers_access (void);
    true iff we ever detected multiple threads.  */
 extern int show_thread_that_caused_stop (void);
 
+/* Print the message for a thread or/and frame selected.  */
+extern void print_selected_thread_frame (struct ui_out *uiout,
+					 user_selected_what selection);
+
 extern struct thread_info *thread_list;
 
 #endif /* GDBTHREAD_H */
diff --git a/gdb/inferior.c b/gdb/inferior.c
index 47d91c7..277b988 100644
--- a/gdb/inferior.c
+++ b/gdb/inferior.c
@@ -548,6 +548,24 @@ inferior_pid_to_str (int pid)
     return _("<null>");
 }
 
+/* See inferior.h.  */
+
+void
+print_selected_inferior (struct ui_out *uiout)
+{
+  char buf[PATH_MAX + 256];
+  struct inferior *inf = current_inferior ();
+
+  xsnprintf (buf, sizeof (buf),
+	     _("[Switching to inferior %d [%s] (%s)]\n"),
+	     inf->num,
+	     inferior_pid_to_str (inf->pid),
+	     (inf->pspace->pspace_exec_filename != NULL
+	      ? inf->pspace->pspace_exec_filename
+	      : _("<noexec>")));
+  ui_out_text (uiout, buf);
+}
+
 /* Prints the list of inferiors and their details on UIOUT.  This is a
    version of 'info_inferior_command' suitable for use from MI.
 
@@ -726,13 +744,6 @@ inferior_command (char *args, int from_tty)
   if (inf == NULL)
     error (_("Inferior ID %d not known."), num);
 
-  printf_filtered (_("[Switching to inferior %d [%s] (%s)]\n"),
-		   inf->num,
-		   inferior_pid_to_str (inf->pid),
-		   (inf->pspace->pspace_exec_filename != NULL
-		    ? inf->pspace->pspace_exec_filename
-		    : _("<noexec>")));
-
   if (inf->pid != 0)
     {
       if (inf->pid != ptid_get_pid (inferior_ptid))
@@ -746,9 +757,10 @@ inferior_command (char *args, int from_tty)
 	  switch_to_thread (tp->ptid);
 	}
 
-      printf_filtered (_("[Switching to thread %s (%s)] "),
-		       print_thread_id (inferior_thread ()),
-		       target_pid_to_str (inferior_ptid));
+      observer_notify_user_selected_context_changed
+	(USER_SELECTED_INFERIOR
+	 | USER_SELECTED_THREAD
+	 | USER_SELECTED_FRAME);
     }
   else
     {
@@ -758,14 +770,8 @@ inferior_command (char *args, int from_tty)
       set_current_inferior (inf);
       switch_to_thread (null_ptid);
       set_current_program_space (inf->pspace);
-    }
 
-  if (inf->pid != 0 && is_running (inferior_ptid))
-    ui_out_text (current_uiout, "(running)\n");
-  else if (inf->pid != 0)
-    {
-      ui_out_text (current_uiout, "\n");
-      print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC, 1);
+      observer_notify_user_selected_context_changed (USER_SELECTED_INFERIOR);
     }
 }
 
diff --git a/gdb/inferior.h b/gdb/inferior.h
index 571d26a..54c6f65 100644
--- a/gdb/inferior.h
+++ b/gdb/inferior.h
@@ -523,4 +523,7 @@ extern int number_of_inferiors (void);
 
 extern struct inferior *add_inferior_with_spaces (void);
 
+/* Print the current selected inferior.  */
+extern void print_selected_inferior (struct ui_out *uiout);
+
 #endif /* !defined (INFERIOR_H) */
diff --git a/gdb/mi/mi-cmds.c b/gdb/mi/mi-cmds.c
index 4779832..85c19c1 100644
--- a/gdb/mi/mi-cmds.c
+++ b/gdb/mi/mi-cmds.c
@@ -137,7 +137,8 @@ static struct mi_cmd mi_cmds[] =
   DEF_MI_CMD_MI ("stack-list-frames", mi_cmd_stack_list_frames),
   DEF_MI_CMD_MI ("stack-list-locals", mi_cmd_stack_list_locals),
   DEF_MI_CMD_MI ("stack-list-variables", mi_cmd_stack_list_variables),
-  DEF_MI_CMD_MI ("stack-select-frame", mi_cmd_stack_select_frame),
+  DEF_MI_CMD_MI_1 ("stack-select-frame", mi_cmd_stack_select_frame,
+		   &mi_suppress_notification.user_selected_context),
   DEF_MI_CMD_MI ("symbol-list-lines", mi_cmd_symbol_list_lines),
   DEF_MI_CMD_CLI ("target-attach", "attach", 1),
   DEF_MI_CMD_MI ("target-detach", mi_cmd_target_detach),
@@ -149,7 +150,8 @@ static struct mi_cmd mi_cmds[] =
   DEF_MI_CMD_CLI ("target-select", "target", 1),
   DEF_MI_CMD_MI ("thread-info", mi_cmd_thread_info),
   DEF_MI_CMD_MI ("thread-list-ids", mi_cmd_thread_list_ids),
-  DEF_MI_CMD_MI ("thread-select", mi_cmd_thread_select),
+  DEF_MI_CMD_MI_1 ("thread-select", mi_cmd_thread_select,
+		   &mi_suppress_notification.user_selected_context),
   DEF_MI_CMD_MI ("trace-define-variable", mi_cmd_trace_define_variable),
   DEF_MI_CMD_MI_1 ("trace-find", mi_cmd_trace_find,
 		   &mi_suppress_notification.traceframe),
diff --git a/gdb/mi/mi-interp.c b/gdb/mi/mi-interp.c
index e3c7dbd..d7db499 100644
--- a/gdb/mi/mi-interp.c
+++ b/gdb/mi/mi-interp.c
@@ -769,6 +769,7 @@ struct mi_suppress_notification mi_suppress_notification =
     0,
     0,
     0,
+    0,
   };
 
 /* Emit notification on changing a traceframe.  */
@@ -1334,6 +1335,64 @@ mi_memory_changed (struct inferior *inferior, CORE_ADDR memaddr,
     }
 }
 
+/* Emit an event when the selection context (inferior, thread, frame)
+   changed.  */
+
+static void
+mi_user_selected_context_changed (user_selected_what selection)
+{
+  struct switch_thru_all_uis state;
+  struct thread_info *tp;
+
+  /* Don't send an event if we're responding to an MI command.  */
+  if (mi_suppress_notification.user_selected_context)
+    return;
+
+  tp = find_thread_ptid (inferior_ptid);
+
+  SWITCH_THRU_ALL_UIS (state)
+    {
+      struct mi_interp *mi = as_mi_interp (top_level_interpreter ());
+      struct ui_out *mi_uiout;
+      struct cleanup *old_chain;
+
+      if (mi == NULL)
+	continue;
+
+      mi_uiout = interp_ui_out (top_level_interpreter ());
+
+      ui_out_redirect (mi_uiout, mi->event_channel);
+
+      old_chain = make_cleanup_ui_out_redirect_pop (mi_uiout);
+
+      make_cleanup_restore_target_terminal ();
+      target_terminal_ours_for_output ();
+
+      if (selection & USER_SELECTED_INFERIOR)
+	print_selected_inferior (mi->cli_uiout);
+
+      if (tp != NULL
+	  && (selection & (USER_SELECTED_THREAD | USER_SELECTED_FRAME)))
+	{
+	  print_selected_thread_frame (mi->cli_uiout, selection);
+
+	  fprintf_unfiltered (mi->event_channel,
+			      "thread-selected,id=\"%d\"",
+			      tp->global_num);
+
+	  if (tp->state != THREAD_RUNNING)
+	    {
+	      if (has_stack_frames ())
+		print_stack_frame_to_uiout (mi_uiout, get_selected_frame (NULL),
+					    1, SRC_AND_LOC, 1);
+	    }
+	}
+
+      gdb_flush (mi->event_channel);
+      do_cleanups (old_chain);
+    }
+}
+
 static int
 report_initial_inferior (struct inferior *inf, void *closure)
 {
@@ -1466,4 +1525,6 @@ _initialize_mi_interp (void)
   observer_attach_command_param_changed (mi_command_param_changed);
   observer_attach_memory_changed (mi_memory_changed);
   observer_attach_sync_execution_done (mi_on_sync_execution_done);
+  observer_attach_user_selected_context_changed
+    (mi_user_selected_context_changed);
 }
diff --git a/gdb/mi/mi-main.c b/gdb/mi/mi-main.c
index 1913157..dcff36b 100644
--- a/gdb/mi/mi-main.c
+++ b/gdb/mi/mi-main.c
@@ -53,6 +53,7 @@
 #include "linespec.h"
 #include "extension.h"
 #include "gdbcmd.h"
+#include "observer.h"
 
 #include <ctype.h>
 #include "gdb_sys_time.h"
@@ -564,17 +565,29 @@ mi_cmd_thread_select (char *command, char **argv, int argc)
 {
   enum gdb_rc rc;
   char *mi_error_message;
+  ptid_t previous_ptid = inferior_ptid;
 
   if (argc != 1)
     error (_("-thread-select: USAGE: threadnum."));
 
   rc = gdb_thread_select (current_uiout, argv[0], &mi_error_message);
 
+  /* If thread switch did not succeed don't notify or print.  */
   if (rc == GDB_RC_FAIL)
     {
       make_cleanup (xfree, mi_error_message);
       error ("%s", mi_error_message);
     }
+
+  print_selected_thread_frame (current_uiout,
+			       USER_SELECTED_THREAD | USER_SELECTED_FRAME);
+
+  /* Notify if the thread has effectively changed.  */
+  if (!ptid_equal (inferior_ptid, previous_ptid))
+    {
+      observer_notify_user_selected_context_changed (USER_SELECTED_THREAD
+						     | USER_SELECTED_FRAME);
+    }
 }
 
 void
@@ -2124,9 +2137,16 @@ mi_execute_command (const char *cmd, int from_tty)
   if (command != NULL)
     {
       ptid_t previous_ptid = inferior_ptid;
+      struct cleanup *cleanup = make_cleanup (null_cleanup, NULL);
 
       command->token = token;
 
+      if (command->cmd != NULL && command->cmd->suppress_notification != NULL)
+        {
+          make_cleanup_restore_integer (command->cmd->suppress_notification);
+          *command->cmd->suppress_notification = 1;
+        }
+
       if (do_timings)
 	{
 	  command->cmd_start = XNEW (struct mi_timestamp);
@@ -2161,10 +2181,15 @@ mi_execute_command (const char *cmd, int from_tty)
 	  /* Don't try report anything if there are no threads --
 	     the program is dead.  */
 	  && thread_count () != 0
-	  /* -thread-select explicitly changes thread. If frontend uses that
-	     internally, we don't want to emit =thread-selected, since
-	     =thread-selected is supposed to indicate user's intentions.  */
-	  && strcmp (command->command, "thread-select") != 0)
+	  /* For CLI commands "thread" and "inferior", the event is already sent
+	     by the command, so don't send it again.  */
+	  && ((command->op == CLI_COMMAND
+	       && strncmp (command->command, "thread", 6) != 0
+	       && strncmp (command->command, "inferior", 8) != 0)
+	      || (command->op == MI_COMMAND && command->argc > 1
+		  && strcmp (command->command, "interpreter-exec") == 0
+		  && strncmp (command->argv[1], "thread", 6) != 0
+		  && strncmp (command->argv[1], "inferior", 8) != 0)))
 	{
 	  struct mi_interp *mi
 	    = (struct mi_interp *) top_level_interpreter_data ();
@@ -2185,22 +2210,14 @@ mi_execute_command (const char *cmd, int from_tty)
 
 	  if (report_change)
 	    {
-	      struct thread_info *ti = inferior_thread ();
-	      struct cleanup *old_chain;
-
-	      old_chain = make_cleanup_restore_target_terminal ();
-	      target_terminal_ours_for_output ();
-
-	      fprintf_unfiltered (mi->event_channel,
-				  "thread-selected,id=\"%d\"",
-				  ti->global_num);
-	      gdb_flush (mi->event_channel);
-
-	      do_cleanups (old_chain);
+		observer_notify_user_selected_context_changed
+		  (USER_SELECTED_THREAD | USER_SELECTED_FRAME);
 	    }
 	}
 
       mi_parse_free (command);
+
+      do_cleanups (cleanup);
     }
 }
 
@@ -2277,12 +2294,6 @@ mi_cmd_execute (struct mi_parse *parse)
 
   current_context = parse;
 
-  if (parse->cmd->suppress_notification != NULL)
-    {
-      make_cleanup_restore_integer (parse->cmd->suppress_notification);
-      *parse->cmd->suppress_notification = 1;
-    }
-
   if (parse->cmd->argv_func != NULL)
     {
       parse->cmd->argv_func (parse->command, parse->argv, parse->argc);
diff --git a/gdb/mi/mi-main.h b/gdb/mi/mi-main.h
index 18000cf..bf4d1b6 100644
--- a/gdb/mi/mi-main.h
+++ b/gdb/mi/mi-main.h
@@ -49,6 +49,8 @@ struct mi_suppress_notification
   int traceframe;
   /* Memory changed notification suppressed?  */
   int memory;
+  /* User selected context changed notification suppressed?  */
+  int user_selected_context;
 };
 extern struct mi_suppress_notification mi_suppress_notification;
 
diff --git a/gdb/stack.c b/gdb/stack.c
index 417e887..b719fcd 100644
--- a/gdb/stack.c
+++ b/gdb/stack.c
@@ -51,6 +51,7 @@
 #include "safe-ctype.h"
 #include "symfile.h"
 #include "extension.h"
+#include "observer.h"
 
 /* The possible choices of "set print frame-arguments", and the value
    of this setting.  */
@@ -141,6 +142,24 @@ frame_show_address (struct frame_info *frame,
   return get_frame_pc (frame) != sal.pc;
 }
 
+/* See frame.h.  */
+
+void
+print_stack_frame_to_uiout (struct ui_out *uiout, struct frame_info *frame,
+			    int print_level, enum print_what print_what,
+			    int set_current_sal)
+{
+  struct cleanup *old_chain;
+
+  old_chain = make_cleanup_restore_current_uiout ();
+
+  current_uiout = uiout;
+
+  print_stack_frame (frame, print_level, print_what, set_current_sal);
+
+  do_cleanups (old_chain);
+}
+
 /* Show or print a stack frame FRAME briefly.  The output is formatted
    according to PRINT_LEVEL and PRINT_WHAT printing the frame's
    relative level, function name, argument list, and file name and
@@ -2302,7 +2321,11 @@ find_relative_frame (struct frame_info *frame, int *level_offset_ptr)
 void
 select_frame_command (char *level_exp, int from_tty)
 {
+  struct frame_info *prev_frame = get_selected_frame_if_set ();
+
   select_frame (parse_frame_specification (level_exp, NULL));
+  if (get_selected_frame_if_set () != prev_frame)
+    observer_notify_user_selected_context_changed (USER_SELECTED_FRAME);
 }
 
 /* The "frame" command.  With no argument, print the selected frame
@@ -2312,8 +2335,13 @@ select_frame_command (char *level_exp, int from_tty)
 static void
 frame_command (char *level_exp, int from_tty)
 {
-  select_frame_command (level_exp, from_tty);
-  print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC, 1);
+  struct frame_info *prev_frame = get_selected_frame_if_set ();
+
+  select_frame (parse_frame_specification (level_exp, NULL));
+  if (get_selected_frame_if_set () != prev_frame)
+    observer_notify_user_selected_context_changed (USER_SELECTED_FRAME);
+  else
+    print_selected_thread_frame (current_uiout, USER_SELECTED_FRAME);
 }
 
 /* Select the frame up one or COUNT_EXP stack levels from the
@@ -2344,7 +2372,7 @@ static void
 up_command (char *count_exp, int from_tty)
 {
   up_silently_base (count_exp);
-  print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC, 1);
+  observer_notify_user_selected_context_changed (USER_SELECTED_FRAME);
 }
 
 /* Select the frame down one or COUNT_EXP stack levels from the previously
@@ -2383,9 +2411,8 @@ static void
 down_command (char *count_exp, int from_tty)
 {
   down_silently_base (count_exp);
-  print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC, 1);
+  observer_notify_user_selected_context_changed (USER_SELECTED_FRAME);
 }
-\f
 
 void
 return_command (char *retval_exp, int from_tty)
@@ -2616,10 +2643,11 @@ a command file or a user-defined command."));
 
   add_com_alias ("f", "frame", class_stack, 1);
 
-  add_com ("select-frame", class_stack, select_frame_command, _("\
+  add_com_suppress_notification ("select-frame", class_stack, select_frame_command, _("\
 Select a stack frame without printing anything.\n\
 An argument specifies the frame to select.\n\
-It can be a stack frame number or the address of the frame.\n"));
+It can be a stack frame number or the address of the frame.\n"),
+		 &cli_suppress_notification.user_selected_context);
 
   add_com ("backtrace", class_stack, backtrace_command, _("\
 Print backtrace of all stack frames, or innermost COUNT frames.\n\
diff --git a/gdb/testsuite/gdb.mi/mi-pthreads.exp b/gdb/testsuite/gdb.mi/mi-pthreads.exp
index 88a600a..511f0ca 100644
--- a/gdb/testsuite/gdb.mi/mi-pthreads.exp
+++ b/gdb/testsuite/gdb.mi/mi-pthreads.exp
@@ -53,8 +53,8 @@ proc check_mi_thread_command_set {} {
 
   foreach thread $thread_list {
       mi_gdb_test "-interpreter-exec console \"thread $thread\"" \
-          ".*\\^done\r\n=thread-selected,id=\"$thread\"" \
-          "check =thread-selected: thread $thread"
+	  ".*=thread-selected,id=\"$thread\".*\r\n\\^done" \
+	  "check =thread-selected: thread $thread"
   }
 }
 
diff --git a/gdb/thread.c b/gdb/thread.c
index a66a2b5..13449a8 100644
--- a/gdb/thread.c
+++ b/gdb/thread.c
@@ -1923,7 +1923,7 @@ thread_apply_command (char *tidlist, int from_tty)
 void
 thread_command (char *tidstr, int from_tty)
 {
-  if (!tidstr)
+  if (tidstr == NULL)
     {
       if (ptid_equal (inferior_ptid, null_ptid))
 	error (_("No thread selected"));
@@ -1943,10 +1943,31 @@ thread_command (char *tidstr, int from_tty)
 	}
       else
 	error (_("No stack."));
-      return;
     }
+  else
+    {
+      ptid_t previous_ptid = inferior_ptid;
+      enum gdb_rc result;
+
+      result = gdb_thread_select (current_uiout, tidstr, NULL);
+
+      /* If thread switch did not succeed don't notify or print.  */
+      if (result == GDB_RC_FAIL)
+	return;
 
-  gdb_thread_select (current_uiout, tidstr, NULL);
+      /* Print if the thread has not changed, otherwise an event will be sent.  */
+      if (ptid_equal (inferior_ptid, previous_ptid))
+	{
+	  print_selected_thread_frame (current_uiout,
+				       USER_SELECTED_THREAD
+				       | USER_SELECTED_FRAME);
+	}
+      else
+	{
+	  observer_notify_user_selected_context_changed (USER_SELECTED_THREAD
+							 | USER_SELECTED_FRAME);
+	}
+    }
 }
 
 /* Implementation of `thread name'.  */
@@ -2058,32 +2079,53 @@ do_captured_thread_select (struct ui_out *uiout, void *tidstr_v)
 
   annotate_thread_changed ();
 
-  if (ui_out_is_mi_like_p (uiout))
-    ui_out_field_int (uiout, "new-thread-id", inferior_thread ()->global_num);
-  else
+  /* Since the current thread may have changed, see if there is any
+     exited thread we can now delete.  */
+  prune_threads ();
+
+  return GDB_RC_OK;
+}
+
+/* Print thread and frame switch command response.  */
+
+void
+print_selected_thread_frame (struct ui_out *uiout,
+			     user_selected_what selection)
+{
+  struct thread_info *tp = inferior_thread ();
+  struct inferior *inf = current_inferior ();
+
+  if (selection & USER_SELECTED_THREAD)
     {
-      ui_out_text (uiout, "[Switching to thread ");
-      ui_out_field_string (uiout, "new-thread-id", print_thread_id (tp));
-      ui_out_text (uiout, " (");
-      ui_out_text (uiout, target_pid_to_str (inferior_ptid));
-      ui_out_text (uiout, ")]");
+      if (ui_out_is_mi_like_p (uiout))
+	{
+	  ui_out_field_int (uiout, "new-thread-id",
+			    inferior_thread ()->global_num);
+	}
+      else
+	{
+	  ui_out_text (uiout, "[Switching to thread ");
+	  ui_out_field_string (uiout, "new-thread-id", print_thread_id (tp));
+	  ui_out_text (uiout, " (");
+	  ui_out_text (uiout, target_pid_to_str (inferior_ptid));
+	  ui_out_text (uiout, ")]");
+	}
     }
 
-  /* Note that we can't reach this with an exited thread, due to the
-     thread_alive check above.  */
   if (tp->state == THREAD_RUNNING)
-    ui_out_text (uiout, "(running)\n");
-  else
     {
-      ui_out_text (uiout, "\n");
-      print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC, 1);
+      if (selection & USER_SELECTED_THREAD)
+	ui_out_text (uiout, "(running)\n");
     }
+  else if (selection & USER_SELECTED_FRAME)
+    {
+      if (selection & USER_SELECTED_THREAD)
+	ui_out_text (uiout, "\n");
 
-  /* Since the current thread may have changed, see if there is any
-     exited thread we can now delete.  */
-  prune_threads ();
-
-  return GDB_RC_OK;
+      if (has_stack_frames ())
+	print_stack_frame_to_uiout (uiout, get_selected_frame (NULL),
+				    1, SRC_AND_LOC, 1);
+    }
 }
 
 enum gdb_rc
diff --git a/gdb/tui/tui-interp.c b/gdb/tui/tui-interp.c
index 3856382..e06d679 100644
--- a/gdb/tui/tui-interp.c
+++ b/gdb/tui/tui-interp.c
@@ -206,6 +206,37 @@ tui_on_command_error (void)
   display_gdb_prompt (NULL);
 }
 
+/* Observer for the user_selected_context_changed notification.  */
+
+static void
+tui_on_user_selected_context_changed (user_selected_what selection)
+{
+  struct switch_thru_all_uis state;
+  struct thread_info *tp;
+
+  /* This event is suppressed.  */
+  if (cli_suppress_notification.user_selected_context)
+    return;
+
+  tp = find_thread_ptid (inferior_ptid);
+
+  SWITCH_THRU_ALL_UIS (state)
+    {
+      struct interp *tui = as_tui_interp (top_level_interpreter ());
+
+      if (tui == NULL)
+	continue;
+
+      if (selection & USER_SELECTED_INFERIOR)
+	print_selected_inferior (tui_ui_out (tui));
+
+      if (tp != NULL
+	  && ((selection & (USER_SELECTED_THREAD | USER_SELECTED_FRAME))))
+	print_selected_thread_frame (tui_ui_out (tui), selection);
+
+    }
+}
+
 /* These implement the TUI interpreter.  */
 
 static void *
@@ -323,4 +354,6 @@ _initialize_tui_interp (void)
   observer_attach_no_history (tui_on_no_history);
   observer_attach_sync_execution_done (tui_on_sync_execution_done);
   observer_attach_command_error (tui_on_command_error);
+  observer_attach_user_selected_context_changed
+    (tui_on_user_selected_context_changed);
 }
-- 
2.9.3

^ permalink raw reply	[flat|nested] 21+ messages in thread

* Re: [PATCH master+7.12 v2 1/3] Introduce cleanup to restore current_uiout
  2016-09-14 17:46 ` [PATCH master+7.12 v2 1/3] Introduce cleanup to restore current_uiout Simon Marchi
@ 2016-09-14 17:56   ` Pedro Alves
  2016-09-15  3:24     ` Simon Marchi
  2016-09-14 18:11   ` Tom Tromey
  1 sibling, 1 reply; 21+ messages in thread
From: Pedro Alves @ 2016-09-14 17:56 UTC (permalink / raw)
  To: Simon Marchi, gdb-patches

On 09/14/2016 06:45 PM, Simon Marchi wrote:
> Make a globally available cleanup from a pre-existing one in infrun.c.
> This is used in the following patch.
> 
> gdb/ChangeLog:
> 
> 	* infrun.c (restore_current_uiout_cleanup): Move to ui-out.c.
> 	(print_stop_event): Use make_cleanup_restore_current_uiout.
> 	* ui-out.c (restore_current_uiout_cleanup): Move from infrun.c.
> 	(make_cleanup_restore_current_uiout): New function definition.
> 	* ui-out.h (make_cleanup_restore_current_uiout): New function
> 	definition.

OK.

Thanks,
Pedro Alves

^ permalink raw reply	[flat|nested] 21+ messages in thread

* Re: [PATCH master+7.12 v2 1/3] Introduce cleanup to restore current_uiout
  2016-09-14 17:46 ` [PATCH master+7.12 v2 1/3] Introduce cleanup to restore current_uiout Simon Marchi
  2016-09-14 17:56   ` Pedro Alves
@ 2016-09-14 18:11   ` Tom Tromey
  2016-09-14 18:18     ` Simon Marchi
  1 sibling, 1 reply; 21+ messages in thread
From: Tom Tromey @ 2016-09-14 18:11 UTC (permalink / raw)
  To: Simon Marchi; +Cc: gdb-patches

>>>>> "Simon" == Simon Marchi <simon.marchi@ericsson.com> writes:

Simon> -  old_chain = make_cleanup (restore_current_uiout_cleanup, current_uiout);
Simon> +  old_chain = make_cleanup_restore_current_uiout ();

There's also already make_cleanup_restore_ui_out.
You could just use "make_cleanup_restore_ui_out (&current_uiout)" instead.

Tom

^ permalink raw reply	[flat|nested] 21+ messages in thread

* Re: [PATCH master+7.12 v2 1/3] Introduce cleanup to restore current_uiout
  2016-09-14 18:11   ` Tom Tromey
@ 2016-09-14 18:18     ` Simon Marchi
  2016-09-14 18:32       ` Pedro Alves
  0 siblings, 1 reply; 21+ messages in thread
From: Simon Marchi @ 2016-09-14 18:18 UTC (permalink / raw)
  To: Tom Tromey; +Cc: gdb-patches

On 16-09-14 02:10 PM, Tom Tromey wrote:
>>>>>> "Simon" == Simon Marchi <simon.marchi@ericsson.com> writes:
> 
> Simon> -  old_chain = make_cleanup (restore_current_uiout_cleanup, current_uiout);
> Simon> +  old_chain = make_cleanup_restore_current_uiout ();
> 
> There's also already make_cleanup_restore_ui_out.
> You could just use "make_cleanup_restore_ui_out (&current_uiout)" instead.
> 
> Tom

Ah good point, I'll probably do that.

Thanks!

^ permalink raw reply	[flat|nested] 21+ messages in thread

* Re: [PATCH master+7.12 v2 2/3] Emit inferior, thread and frame selection events to all UIs
  2016-09-14 17:46 ` [PATCH master+7.12 v2 2/3] Emit inferior, thread and frame selection events to all UIs Simon Marchi
@ 2016-09-14 18:30   ` Pedro Alves
  2016-09-15 16:21     ` Simon Marchi
  0 siblings, 1 reply; 21+ messages in thread
From: Pedro Alves @ 2016-09-14 18:30 UTC (permalink / raw)
  To: Simon Marchi, gdb-patches; +Cc: Antoine Tremblay

Hi Simon,

On 09/14/2016 06:45 PM, Simon Marchi wrote:
> From: Antoine Tremblay <antoine.tremblay@ericsson.com>
> 
> With this patch, when an inferior, thread or frame is explicitly
> selected by the user, notifications will appear on all CLI and MI UIs.
> When a GDB console is integrated in a front-end, this allows the
> front-end to follow a selection made by the user ont he CLI, and it
> informs the user about selection changes made behind the scenes by the
> front-end.
> 
> This patch fixes PR gdb/20487.
> 
> In order to communicate frame changes to the front-end, this patch adds
> a new field to the =thread-selected event for the selected frame.  The
> idea is that since inferior/thread/frame can be seen as a composition,
> it makes sense to send them together in the same event.  The vision
> would be to eventually send the inferior information as well, if we find
> that it's needed, although the "=thread-selected" event would be
> ill-named for that job.

Given per-inferior thread numbers, we could also say that we switch to
thread 0 of inferior INF (i.e., "INF.0").  Then it wouldn't sound that
strange, maybe.  The problem is that MI talks in terms of the global
thread id, not the per-inferior id.

Anyway, since is for machine consumption, odd naming should not
be a big deal, IMO.

> frame
> -----
> 
> 1. CLI command:
> 
>      frame 1
> 
>    MI event:
> 
>      =thread-selected,id="3",frame={level="1",...}
> 
> 2. MI command:
> 
>      -stack-select-frame 1
> 
>    CLI event:
> 
>      #1  0x00000000004007f0 in child_function...
> 

I think it's likely that experience will show that will want to tweak
what we print in the CLI in the future, along with whether
we print at all, but that's fine for now.  Making all user-selection
change handling be consistent makes sense.

> 3. MI command (CLI-in-MI):
> 
>      frame 1
> 
>    MI event/reply:
> 
>      &"frame 1\n"
>      ~"#1  0x00000000004007f9 in ..."
>      =thread-selected,id="3",frame={level="1"...}
>      ^done
> 
> inferior
> --------
> 
> Inferior selection events only go from the console to MI, since there's
> no way to select the inferior in pure MI.
> 
> 1. CLI command:
> 
>      inferior 2
> 
>    MI event:
> 
>      =thread-selected,id="3"
> 
> Note that if the user selects an inferior that is not started or exited,
> the MI doesn't receive a notification.  Since there is no threads to
> select, the =thread-selected event does not apply...

We could solve that by adding the thread group id (inferior id) to
the notification, I think:

 =thread-selected,id="3",thread-group="i1",frame="..."

 =thread-selected,id="0",thread-group="i2",frame="..."

...

If you select an inferior that is not running yet, thread 0 is what
you effectively get:

(gdb) p $_inferior
$1 = 1
(gdb) p $_thread
$2 = 1
(gdb) p $_gthread
$3 = 1
(gdb) add-inferior 
Added inferior 2
(gdb) inferior 2
[Switching to inferior 2 [<null>] (<noexec>)]
(gdb) p $_inferior
$4 = 2
(gdb) p $_thread
$5 = 0
(gdb) p $_gthread
$6 = 0
(gdb) 

> @@ -2124,9 +2137,16 @@ mi_execute_command (const char *cmd, int from_tty)
>    if (command != NULL)
>      {
>        ptid_t previous_ptid = inferior_ptid;
> +      struct cleanup *cleanup = make_cleanup (null_cleanup, NULL);
>  
>        command->token = token;
>  
> +      if (command->cmd != NULL && command->cmd->suppress_notification != NULL)
> +        {
> +          make_cleanup_restore_integer (command->cmd->suppress_notification);
> +          *command->cmd->suppress_notification = 1;
> +        }
> +
>        if (do_timings)
>  	{
>  	  command->cmd_start = XNEW (struct mi_timestamp);
> @@ -2161,10 +2181,15 @@ mi_execute_command (const char *cmd, int from_tty)
>  	  /* Don't try report anything if there are no threads --
>  	     the program is dead.  */
>  	  && thread_count () != 0
> -	  /* -thread-select explicitly changes thread. If frontend uses that
> -	     internally, we don't want to emit =thread-selected, since
> -	     =thread-selected is supposed to indicate user's intentions.  */

I'm still uneasy about this.  Do we now emit a =thread-selected
event when the frontend uses -thread-select?  Is that a deliberate change?

> -	  && strcmp (command->command, "thread-select") != 0)
> +	  /* For CLI commands "thread" and "inferior", the event is already sent
> +	     by the command, so don't send it again.  */
> +	  && ((command->op == CLI_COMMAND
> +	       && strncmp (command->command, "thread", 6) != 0
> +	       && strncmp (command->command, "inferior", 8) != 0)
> +	      || (command->op == MI_COMMAND && command->argc > 1
> +		  && strcmp (command->command, "interpreter-exec") == 0
> +		  && strncmp (command->argv[1], "thread", 6) != 0
> +		  && strncmp (command->argv[1], "inferior", 8) != 0)))


These "strncmp" calls return 0 when the command is "threadfoo"
or "inferiorfoo"  I think we need to check the next character
too, somehow?

I think it doesn't make a difference for any of the current "thread"
subcommands ("thread apply", etc.), so probably not a big deal.
(though it'd be nice to clean it up sooner than later to avoid
this getting forgotten and breaking in the future.)

But, I suspect that we end up suppressing this case:

(gdb) define thread-foo
>thread $arg0
>end
(gdb) thread-foo 2

Contrived, but certainly not hard to imagine user-commands doing
something useful along with changing the selected thread.

What happens in this case?

Thanks,
Pedro Alves

^ permalink raw reply	[flat|nested] 21+ messages in thread

* Re: [PATCH master+7.12 v2 1/3] Introduce cleanup to restore current_uiout
  2016-09-14 18:18     ` Simon Marchi
@ 2016-09-14 18:32       ` Pedro Alves
  2016-09-14 19:12         ` Tom Tromey
  0 siblings, 1 reply; 21+ messages in thread
From: Pedro Alves @ 2016-09-14 18:32 UTC (permalink / raw)
  To: Simon Marchi, Tom Tromey; +Cc: gdb-patches

On 09/14/2016 07:18 PM, Simon Marchi wrote:
> On 16-09-14 02:10 PM, Tom Tromey wrote:
>>>>>>> "Simon" == Simon Marchi <simon.marchi@ericsson.com> writes:
>>
>> Simon> -  old_chain = make_cleanup (restore_current_uiout_cleanup, current_uiout);
>> Simon> +  old_chain = make_cleanup_restore_current_uiout ();
>>
>> There's also already make_cleanup_restore_ui_out.
>> You could just use "make_cleanup_restore_ui_out (&current_uiout)" instead.
>>
>> Tom
> 
> Ah good point, I'll probably do that.

FWIW, I like the newer spelling better since we'll always be
restoring current_uiout, but it's not a big deal at all.
(similarly to https://sourceware.org/ml/gdb-patches/2016-09/msg00060.html)

Thanks,
Pedro Alves

^ permalink raw reply	[flat|nested] 21+ messages in thread

* Re: [PATCH master+7.12 v2 1/3] Introduce cleanup to restore current_uiout
  2016-09-14 18:32       ` Pedro Alves
@ 2016-09-14 19:12         ` Tom Tromey
  2016-09-15  3:17           ` Simon Marchi
  0 siblings, 1 reply; 21+ messages in thread
From: Tom Tromey @ 2016-09-14 19:12 UTC (permalink / raw)
  To: Pedro Alves; +Cc: Simon Marchi, Tom Tromey, gdb-patches

>>>>> "Pedro" == Pedro Alves <palves@redhat.com> writes:

Pedro> FWIW, I like the newer spelling better since we'll always be
Pedro> restoring current_uiout, but it's not a big deal at all.

The only existing call to make_cleanup_restore_uiout uses current_uiout,
so another option would be to just replace that.

Tom

^ permalink raw reply	[flat|nested] 21+ messages in thread

* Re: [PATCH master+7.12 v2 3/3] Add test for user context selection sync
  2016-09-14 17:46 ` [PATCH master+7.12 v2 3/3] Add test for user context selection sync Simon Marchi
@ 2016-09-14 19:31   ` Pedro Alves
  2016-09-16  2:02     ` Simon Marchi
  0 siblings, 1 reply; 21+ messages in thread
From: Pedro Alves @ 2016-09-14 19:31 UTC (permalink / raw)
  To: Simon Marchi, gdb-patches; +Cc: Antoine Tremblay

Hi Simon,

I didn't try to understand everything in detail, but overall
it looks very nice now.  Thank you very much.  A few comments below.

On 09/14/2016 06:45 PM, Simon Marchi wrote:
> From: Antoine Tremblay <antoine.tremblay@ericsson.com>
> 
> This patch adds a test to verify that events are sent properly to all
> UIs when the user selection context (inferior, thread, frame) changes.
> 
> The goal of the C test file is to provide two threads that are
> interrupted with the same predictable backtrace (so that we can test
> frame switching).  This is achieved by having them loop on a single
> line, such that when the main thread hits a breakpoint, they are both
> stopped that line.  It would not be practical to have the threads sleep,
> since their backtraces would not be predictable (the functions that
> implement sleep may vary between systems).
> 
> There is a 1 second sleep in the main thread to make sure the threads
> have time to spawn and reach the loop.  If you can find a way that is
> sleep-free and race-free to achieve the same result, it would be really
> nice, as most of the time taken by the test is spent sleeping.

Did you try using barriers and breakpoints?  Several tests use that
to make sure threads are past a point.

> +int
> +main (void)
> +{
> +  int i = 0;
> +  pthread_t threads[NUM_THREADS];
> +
> +  for (i = 0; i < NUM_THREADS; i++)
> +    pthread_create (&threads[i], NULL, child_function, NULL);
> +
> +  /* Leave enough time for the threads to reach their infinite loop. */
> +  sleep (1);
> +  
> +  i = 0; /* main break line */
> +
> +  sleep (2);
> +  
> +  /* Allow the test to exit cleanly.  */
> +  quit = 1;
> +
> +  for (i = 0; i < NUM_THREADS; i++)
> +    pthread_join (threads[i], NULL);


Hmm, looks like this version of the test still runs forever.


e for more details.
> +#
> +# You should have received a copy of the GNU General Public License
> +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
> +
> +# This test checks that thread, select-frame, frame or inferior selection
> +# events are properly sent to all uis.

This comment is talking about "select-frame", which I take is
the CLI command's name, not a selection event.  It feels like the comment
could be tweaked to be a bit clearer.  Something like:

# This test checks that the "thread", "select-frame", "frame" and "inferior"
# commands send the appropriate user-selection-change events to all UIs.

Maybe say something about MI commands too, if the test exercises them.

> +#
> +# This test considers the case where console and mi are two different uis
> +# and mi is created with the new-ui command.
> +#
> +# It also considers the case where the console commands are sent directly in
> +# the mi channel as described in PR 20487.

Could you do a quick skim over the test and uppercase "mi" and "ui",
as in "MI", and "UIs".  IMO, that makes the comments easier to grok.


> +# Continue inferior INF until the breakpoint indicating the threads are started.
> +
> +proc test_continue_to_start { mode inf } {
> +    global gdb_prompt gdb_spawn_id gdb_main_spawn_id
> +
> +    if { $gdb_spawn_id != $gdb_main_spawn_id } {
> +	error "This should not happen."
> +    }
> +
> +    with_test_prefix "inferior $inf" {
> +	with_spawn_id $gdb_main_spawn_id {
> +	    gdb_continue_to_breakpoint "main breakpoint"
> +
> +	    if { $mode == "non-stop" } {
> +		gdb_test "thread $inf.2" ".*" "switch to thread $inf.2"
> +
> +		send_gdb "interrupt\n"
> +		gdb_expect {

gdb_test_multiple ?

> +		    -re "Thread.*2.*stopped" {
> +			pass "interrupt thread $inf.2"
> +		    }
> +		}
> +	    }
> +	}
> +    }
> +}


> +
> +# Match a regular expression, or ensure that there was no output.
> +#
> +# If RE is non-empty, try to match the content of the program output (using the
> +# current spawn_id) and pass/fail TEST accordingly.
> +# If RE is empty, ensure that the program did not output anything.
> +
> +proc match_re_or_ensure_not_output { re test } {
> +    if { $re != "" } {
> +	gdb_expect {

gdb_test_multiple?  Or maybe this is used by MI too?

> +	    -re "$re" {
> +		pass $test
> +	    }
> +
> +	    default {
> +		fail $test
> +	    }
> +	}
> +    } else {
> +	ensure_no_output $test
> +    }
> +}


> +    with_test_prefix "thread 1.2 with --thread" {
> +	# Test selecting a thread from MI with a --thread option.  This test
> +	# verifies that even if the thread GDB would switch to is the same has
> +	# the thread specified with --thread, an event is still sent to CLI.
> +	# In this case this is thread 1.2
> +
> +	set mi_re [make_mi_re $mode 2 0 response]
> +	set cli_re [make_cli_re $mode -1 1.2 0]
> +
> +	with_spawn_id $mi_spawn_id {
> +	    mi_gdb_test "-thread-select --thread 2 2" $mi_re "-thread-select"
> +	}
> +
> +	with_spawn_id $gdb_main_spawn_id {
> +	    # TODO: it doesn't work as of now.
> +	    # match_re_or_ensure_not_output "$cli_re\r\n" "-thread-select, event on cli"
> +	}

Is there a plan here?

-- 
Pedro Alves

^ permalink raw reply	[flat|nested] 21+ messages in thread

* Re: [PATCH master+7.12 v2 1/3] Introduce cleanup to restore current_uiout
  2016-09-14 19:12         ` Tom Tromey
@ 2016-09-15  3:17           ` Simon Marchi
  0 siblings, 0 replies; 21+ messages in thread
From: Simon Marchi @ 2016-09-15  3:17 UTC (permalink / raw)
  To: Tom Tromey; +Cc: Pedro Alves, Simon Marchi, gdb-patches

On 2016-09-14 15:12, Tom Tromey wrote:
>>>>>> "Pedro" == Pedro Alves <palves@redhat.com> writes:
> 
> Pedro> FWIW, I like the newer spelling better since we'll always be
> Pedro> restoring current_uiout, but it's not a big deal at all.
> 
> The only existing call to make_cleanup_restore_uiout uses 
> current_uiout,
> so another option would be to just replace that.
> 
> Tom

Ah then it makes sense to use make_cleanup_restore_current_uiout.  Why 
have a parametrized version when we don't need it?  I'll post an updated 
patch.

^ permalink raw reply	[flat|nested] 21+ messages in thread

* Re: [PATCH master+7.12 v2 1/3] Introduce cleanup to restore current_uiout
  2016-09-14 17:56   ` Pedro Alves
@ 2016-09-15  3:24     ` Simon Marchi
  2016-09-16 18:18       ` Pedro Alves
  0 siblings, 1 reply; 21+ messages in thread
From: Simon Marchi @ 2016-09-15  3:24 UTC (permalink / raw)
  To: Pedro Alves, Simon Marchi, gdb-patches

On 14/09/16 01:56 PM, Pedro Alves wrote:
> On 09/14/2016 06:45 PM, Simon Marchi wrote:
>> Make a globally available cleanup from a pre-existing one in infrun.c.
>> This is used in the following patch.
>>
>> gdb/ChangeLog:
>>
>> 	* infrun.c (restore_current_uiout_cleanup): Move to ui-out.c.
>> 	(print_stop_event): Use make_cleanup_restore_current_uiout.
>> 	* ui-out.c (restore_current_uiout_cleanup): Move from infrun.c.
>> 	(make_cleanup_restore_current_uiout): New function definition.
>> 	* ui-out.h (make_cleanup_restore_current_uiout): New function
>> 	definition.
> 
> OK.
> 
> Thanks,
> Pedro Alves

A little addition, I replace the usage of make_cleanup_restore_ui_out in python.c
by this new make_cleanup_restore_current_uiout, and remove
make_cleanupt_restore_ui_out.


From 21169da5b93b01a1c83174f79e6a377c7049974a Mon Sep 17 00:00:00 2001
From: Simon Marchi <simon.marchi@ericsson.com>
Date: Tue, 13 Sep 2016 16:44:05 -0400
Subject: [PATCH] Introduce cleanup to restore current_uiout

Make a globally available cleanup from a pre-existing one in infrun.c.
This is used in the following patch.

gdb/ChangeLog:

	* infrun.c (restore_current_uiout_cleanup): Move to ui-out.c.
	(print_stop_event): Use make_cleanup_restore_current_uiout.
	* python/python.c (execute_gdb_command): Likewise.
	* ui-out.c (restore_current_uiout_cleanup): Move from infrun.c.
	(make_cleanup_restore_current_uiout): New function definition.
	* ui-out.h (make_cleanup_restore_current_uiout): New function
	declaration.
	* utils.c (do_restore_ui_out): Remove.
	(make_cleanup_restore_ui_out): Remove.
	* utils.h (make_cleanup_restore_ui_out): Remove.
---
 gdb/infrun.c        | 12 +-----------
 gdb/python/python.c |  3 ++-
 gdb/ui-out.c        | 18 ++++++++++++++++++
 gdb/ui-out.h        |  4 ++++
 gdb/utils.c         | 23 -----------------------
 gdb/utils.h         |  3 ---
 6 files changed, 25 insertions(+), 38 deletions(-)

diff --git a/gdb/infrun.c b/gdb/infrun.c
index 70d7a09..ec37ca1 100644
--- a/gdb/infrun.c
+++ b/gdb/infrun.c
@@ -8096,16 +8096,6 @@ print_stop_location (struct target_waitstatus *ws)
     print_stack_frame (get_selected_frame (NULL), 0, source_flag, 1);
 }

-/* Cleanup that restores a previous current uiout.  */
-
-static void
-restore_current_uiout_cleanup (void *arg)
-{
-  struct ui_out *saved_uiout = (struct ui_out *) arg;
-
-  current_uiout = saved_uiout;
-}
-
 /* See infrun.h.  */

 void
@@ -8118,7 +8108,7 @@ print_stop_event (struct ui_out *uiout)

   get_last_target_status (&last_ptid, &last);

-  old_chain = make_cleanup (restore_current_uiout_cleanup, current_uiout);
+  old_chain = make_cleanup_restore_current_uiout ();
   current_uiout = uiout;

   print_stop_location (&last);
diff --git a/gdb/python/python.c b/gdb/python/python.c
index 621e201..b00b70b 100644
--- a/gdb/python/python.c
+++ b/gdb/python/python.c
@@ -654,7 +654,8 @@ execute_gdb_command (PyObject *self, PyObject *args, PyObject *kw)
       make_cleanup_restore_integer (&current_ui->async);
       current_ui->async = 0;

-      make_cleanup_restore_ui_out (&current_uiout);
+      make_cleanup_restore_current_uiout ();
+
       /* Use the console interpreter uiout to have the same print format
 	for console or MI.  */
       interp = interp_lookup (current_ui, "console");
diff --git a/gdb/ui-out.c b/gdb/ui-out.c
index 3972a56..ec44ab6 100644
--- a/gdb/ui-out.c
+++ b/gdb/ui-out.c
@@ -953,6 +953,24 @@ ui_out_destroy (struct ui_out *uiout)
   xfree (uiout);
 }

+/* Cleanup that restores a previous current uiout.  */
+
+static void
+restore_current_uiout_cleanup (void *arg)
+{
+  struct ui_out *saved_uiout = (struct ui_out *) arg;
+
+  current_uiout = saved_uiout;
+}
+
+/* See ui-out.h.  */
+
+struct cleanup *
+make_cleanup_restore_current_uiout (void)
+{
+  return make_cleanup (restore_current_uiout_cleanup, current_uiout);
+}
+
 /* Standard gdb initialization hook.  */

 void
diff --git a/gdb/ui-out.h b/gdb/ui-out.h
index 9e1e74d..6a4d78a 100644
--- a/gdb/ui-out.h
+++ b/gdb/ui-out.h
@@ -247,4 +247,8 @@ extern void ui_out_destroy (struct ui_out *uiout);

 extern int ui_out_redirect (struct ui_out *uiout, struct ui_file *outstream);

+/* Make a cleanup that restores the previous current uiout.  */
+
+extern struct cleanup *make_cleanup_restore_current_uiout (void);
+
 #endif /* UI_OUT_H */
diff --git a/gdb/utils.c b/gdb/utils.c
index 5188828..2afff80 100644
--- a/gdb/utils.c
+++ b/gdb/utils.c
@@ -338,29 +338,6 @@ struct restore_ui_out_closure
   struct ui_out *value;
 };

-static void
-do_restore_ui_out (void *p)
-{
-  struct restore_ui_out_closure *closure
-    = (struct restore_ui_out_closure *) p;
-
-  *(closure->variable) = closure->value;
-}
-
-/* Remember the current value of *VARIABLE and make it restored when
-   the cleanup is run.  */
-
-struct cleanup *
-make_cleanup_restore_ui_out (struct ui_out **variable)
-{
-  struct restore_ui_out_closure *c = XNEW (struct restore_ui_out_closure);
-
-  c->variable = variable;
-  c->value = *variable;
-
-  return make_cleanup_dtor (do_restore_ui_out, (void *) c, xfree);
-}
-
 struct restore_ui_file_closure
 {
   struct ui_file **variable;
diff --git a/gdb/utils.h b/gdb/utils.h
index 6080f5b..bf77d7d 100644
--- a/gdb/utils.h
+++ b/gdb/utils.h
@@ -93,9 +93,6 @@ extern struct cleanup *make_cleanup_restore_uinteger (unsigned int *variable);
 struct target_ops;
 extern struct cleanup *make_cleanup_unpush_target (struct target_ops *ops);

-
-extern struct cleanup *
-  make_cleanup_restore_ui_out (struct ui_out **variable);
 extern struct cleanup *
   make_cleanup_restore_ui_file (struct ui_file **variable);

-- 
2.9.3



^ permalink raw reply	[flat|nested] 21+ messages in thread

* Re: [PATCH master+7.12 v2 2/3] Emit inferior, thread and frame selection events to all UIs
  2016-09-14 18:30   ` Pedro Alves
@ 2016-09-15 16:21     ` Simon Marchi
  2016-09-16 18:26       ` Pedro Alves
  0 siblings, 1 reply; 21+ messages in thread
From: Simon Marchi @ 2016-09-15 16:21 UTC (permalink / raw)
  To: Pedro Alves; +Cc: gdb-patches, Antoine Tremblay

On 16-09-14 02:30 PM, Pedro Alves wrote:
> Hi Simon,
> 
> On 09/14/2016 06:45 PM, Simon Marchi wrote:
>> From: Antoine Tremblay <antoine.tremblay@ericsson.com>
>>
>> With this patch, when an inferior, thread or frame is explicitly
>> selected by the user, notifications will appear on all CLI and MI UIs.
>> When a GDB console is integrated in a front-end, this allows the
>> front-end to follow a selection made by the user ont he CLI, and it
>> informs the user about selection changes made behind the scenes by the
>> front-end.
>>
>> This patch fixes PR gdb/20487.
>>
>> In order to communicate frame changes to the front-end, this patch adds
>> a new field to the =thread-selected event for the selected frame.  The
>> idea is that since inferior/thread/frame can be seen as a composition,
>> it makes sense to send them together in the same event.  The vision
>> would be to eventually send the inferior information as well, if we find
>> that it's needed, although the "=thread-selected" event would be
>> ill-named for that job.
> 
> Given per-inferior thread numbers, we could also say that we switch to
> thread 0 of inferior INF (i.e., "INF.0").  Then it wouldn't sound that
> strange, maybe.  The problem is that MI talks in terms of the global
> thread id, not the per-inferior id.
>
> Anyway, since is for machine consumption, odd naming should not
> be a big deal, IMO.

What you said lower (adding thread-group to =thread-selected) makes sense I think.

>> frame
>> -----
>>
>> 1. CLI command:
>>
>>      frame 1
>>
>>    MI event:
>>
>>      =thread-selected,id="3",frame={level="1",...}
>>
>> 2. MI command:
>>
>>      -stack-select-frame 1
>>
>>    CLI event:
>>
>>      #1  0x00000000004007f0 in child_function...
>>
> 
> I think it's likely that experience will show that will want to tweak
> what we print in the CLI in the future, along with whether
> we print at all, but that's fine for now.  Making all user-selection
> change handling be consistent makes sense.

Yep.

>> 3. MI command (CLI-in-MI):
>>
>>      frame 1
>>
>>    MI event/reply:
>>
>>      &"frame 1\n"
>>      ~"#1  0x00000000004007f9 in ..."
>>      =thread-selected,id="3",frame={level="1"...}
>>      ^done
>>
>> inferior
>> --------
>>
>> Inferior selection events only go from the console to MI, since there's
>> no way to select the inferior in pure MI.
>>
>> 1. CLI command:
>>
>>      inferior 2
>>
>>    MI event:
>>
>>      =thread-selected,id="3"
>>
>> Note that if the user selects an inferior that is not started or exited,
>> the MI doesn't receive a notification.  Since there is no threads to
>> select, the =thread-selected event does not apply...
> 
> We could solve that by adding the thread group id (inferior id) to
> the notification, I think:
> 
>  =thread-selected,id="3",thread-group="i1",frame="..."
> 
>  =thread-selected,id="0",thread-group="i2",frame="..."
> 
> ...
> 
> If you select an inferior that is not running yet, thread 0 is what
> you effectively get:
> 
> (gdb) p $_inferior
> $1 = 1
> (gdb) p $_thread
> $2 = 1
> (gdb) p $_gthread
> $3 = 1
> (gdb) add-inferior 
> Added inferior 2
> (gdb) inferior 2
> [Switching to inferior 2 [<null>] (<noexec>)]
> (gdb) p $_inferior
> $4 = 2
> (gdb) p $_thread
> $5 = 0
> (gdb) p $_gthread
> $6 = 0
> (gdb) 

That sounds good.  I think we can plan this for 7.13 though, the goal now is
to get the basic use cases working, while not breaking any previous ones.

>> @@ -2124,9 +2137,16 @@ mi_execute_command (const char *cmd, int from_tty)
>>    if (command != NULL)
>>      {
>>        ptid_t previous_ptid = inferior_ptid;
>> +      struct cleanup *cleanup = make_cleanup (null_cleanup, NULL);
>>  
>>        command->token = token;
>>  
>> +      if (command->cmd != NULL && command->cmd->suppress_notification != NULL)
>> +        {
>> +          make_cleanup_restore_integer (command->cmd->suppress_notification);
>> +          *command->cmd->suppress_notification = 1;
>> +        }
>> +
>>        if (do_timings)
>>  	{
>>  	  command->cmd_start = XNEW (struct mi_timestamp);
>> @@ -2161,10 +2181,15 @@ mi_execute_command (const char *cmd, int from_tty)
>>  	  /* Don't try report anything if there are no threads --
>>  	     the program is dead.  */
>>  	  && thread_count () != 0
>> -	  /* -thread-select explicitly changes thread. If frontend uses that
>> -	     internally, we don't want to emit =thread-selected, since
>> -	     =thread-selected is supposed to indicate user's intentions.  */
> 
> I'm still uneasy about this.  Do we now emit a =thread-selected
> event when the frontend uses -thread-select?  Is that a deliberate change?

No, if the MI command is -thread-select, it won't match the condition.  The last part
of the condition only matches "-interpreter-exec".  Note that an event is sent in
mi_cmd_thread_select if the thread has changed, but because of the suppression, we
don't get an MI event.

In the first version of the patch, we didn't send an event from mi_cmd_thread_select.
Instead, it relied on this line that was in the condition:

  || (command->op == MI_COMMAND && command->argc <= 1)

When running -thread-select, the execution would enter the "if" and send the event from
here.  I couldn't really explain that line though (what's the point of checking argc?
there might be some other MI commands that have two arguments and that might change the
thread...).  I thought it was cleaner to remove this line and to send the event from
mi_cmd_thread_select directly, as is done in thread_command for CLI.

However, this code in v2 is wrong, I miss any MI command that might change the thread
(which is kind of the point of this code).  For example
"-data-evaluate-expression start_thread()", when you have a breakpoint on "here" in this
code:

  #include <pthread.h>

  void here() {
  }

  void *func (void *v) {
  	here();
  	for (;;)
  }

  pthread_t thread;
  void start_thread(void) {
  	pthread_create(&thread, NULL, func, NULL);
  	sleep (1);
  }

  int main() {
  	for (;;)
  		sleep(1);
  	return 0;
  }

See lower for suggestion on how to fix it (and hopefully make the code look a bit better).

>> -	  && strcmp (command->command, "thread-select") != 0)
>> +	  /* For CLI commands "thread" and "inferior", the event is already sent
>> +	     by the command, so don't send it again.  */
>> +	  && ((command->op == CLI_COMMAND
>> +	       && strncmp (command->command, "thread", 6) != 0
>> +	       && strncmp (command->command, "inferior", 8) != 0)
>> +	      || (command->op == MI_COMMAND && command->argc > 1
>> +		  && strcmp (command->command, "interpreter-exec") == 0
>> +		  && strncmp (command->argv[1], "thread", 6) != 0
>> +		  && strncmp (command->argv[1], "inferior", 8) != 0)))
> 
> These "strncmp" calls return 0 when the command is "threadfoo"
> or "inferiorfoo"  I think we need to check the next character
> too, somehow?

I think too, we can change the

  strncmp (command->command, "thread", 6)

with

  strncmp (command->command, "thread ", 7)

and so on.

> 
> I think it doesn't make a difference for any of the current "thread"
> subcommands ("thread apply", etc.), so probably not a big deal.
> (though it'd be nice to clean it up sooner than later to avoid
> this getting forgotten and breaking in the future.)

I'll tackle the splitting of the user and internal selected thread right after
7.12 is out, it should get cleaned up then.

> But, I suspect that we end up suppressing this case:
> 
> (gdb) define thread-foo
>> thread $arg0
>> end
> (gdb) thread-foo 2
> 
> Contrived, but certainly not hard to imagine user-commands doing
> something useful along with changing the selected thread.
> 
> What happens in this case?

I think that's actually the opposite.  The purpose of this whole ugly condition
is to avoid sending a second event, if we removed that whole part with strcmp's,
we would end up sending events twice.  It would not be nice, but it would not be
as bad as not sending any event.

So if you use the "thread" command in a user-defined command, the thread command
code will still send an event on its own.  If the command is named foo:

  define foo
    thread $arg0
  end

then this code won't realize that the thread was switched by a command that already
sends an event on its own, and will send a second event.  I think we can live with
that for now.  If the command was named "threadfoo", then it would happen to match
the string comparison, and we wouldn't send a second event (not we if updated the
strcmp's as mentioned earlier).

As you said, thread sub-commands are probably ok, as they don't change the user
selected thread.


That big condition is clearly unclear though :).  I suggest breaking it in its own
function, where it will be more readable (even though not technically better):


/* Determine whether the parsed command already notifies the
   user_selected_context_changed observer.  */

static int
command_sends_thread_selected_event (struct mi_parse *command)
{
  if (command->op == CLI_COMMAND)
    {
      /* CLI commands "thread" and "inferior" already send it.  */
      return (strncmp (command->command, "thread ", 7) == 0
	      || strncmp (command->command, "inferior ", 9) == 0);
    }
  else /* MI_COMMAND */
    {
      if (strcmp (command->command, "interpreter-exec") == 0
	  && command->argc > 1)

	/* "thread" and "inferior" again, but through -interpreter-exec.  */
	return (strncmp (command->argv[1], "thread ", 7) == 0
		|| strncmp (command->argv[1], "inferior ", 9) == 0);

      else
	/* MI command -thread-select already sends it.  */
	return strcmp (command->command, "thread-select") == 0;
    }

  /* We should not get here...  */
  return 0;
}


And then the condition becomes readable, and we understand the intent:


	  /* If the command already reports the thread change, no need to do it
	     again.  */
	  && !command_sends_thread_selected_event (command))
	  ...


To address you uneasiness with -thread-select, let's see how that that code interacts
with that command.  First, an event about the thread change is sent in
mi_cmd_thread_select.  Then, command_sends_thread_selected_event will return true,
making us skip sending the event again.

WDYT?

Simon

^ permalink raw reply	[flat|nested] 21+ messages in thread

* Re: [PATCH master+7.12 v2 3/3] Add test for user context selection sync
  2016-09-14 19:31   ` Pedro Alves
@ 2016-09-16  2:02     ` Simon Marchi
  2016-09-21 16:43       ` Pedro Alves
  0 siblings, 1 reply; 21+ messages in thread
From: Simon Marchi @ 2016-09-16  2:02 UTC (permalink / raw)
  To: Pedro Alves; +Cc: Simon Marchi, gdb-patches, Antoine Tremblay

On 2016-09-14 15:31, Pedro Alves wrote:
> Hi Simon,
> 
> I didn't try to understand everything in detail, but overall
> it looks very nice now.  Thank you very much.  A few comments below.

Oops, while trying to rerun the full testsuite, I noticed that it leaves 
"-ex \"set non-stop 1\"" in the GDBFLAGS... that's not good.  You can 
see it easily if you swap the order of the "all-stop" and "non-stop" 
variations.  Can you spot why the save_vars isn't working as intended?  
When I use a temporary variable instead, it works as intended...

> On 09/14/2016 06:45 PM, Simon Marchi wrote:
>> From: Antoine Tremblay <antoine.tremblay@ericsson.com>
>> 
>> This patch adds a test to verify that events are sent properly to all
>> UIs when the user selection context (inferior, thread, frame) changes.
>> 
>> The goal of the C test file is to provide two threads that are
>> interrupted with the same predictable backtrace (so that we can test
>> frame switching).  This is achieved by having them loop on a single
>> line, such that when the main thread hits a breakpoint, they are both
>> stopped that line.  It would not be practical to have the threads 
>> sleep,
>> since their backtraces would not be predictable (the functions that
>> implement sleep may vary between systems).
>> 
>> There is a 1 second sleep in the main thread to make sure the threads
>> have time to spawn and reach the loop.  If you can find a way that is
>> sleep-free and race-free to achieve the same result, it would be 
>> really
>> nice, as most of the time taken by the test is spent sleeping.
> 
> Did you try using barriers and breakpoints?  Several tests use that
> to make sure threads are past a point.

I tried, but the issue is that depending on the scheduling, the threads 
might still be in the pthread_barrier_wait function when you stop.

Consider this pseudo-code:

thread_function:
   barrier_wait
   infinite_loop # This is where I want the threads to be stopped.

main_thread ():
   initialize_barrier with n = 3
   spawn_thread (thread_function)
   spawn_thread (thread_function)
   barrier_wait
   breakpoint

Once the main thread hits the breakpoint, we have the assurance that the 
threads have started, but we don't know where they are.  They might not 
have exited the barrier_wait function, or they might be in the infinite 
loop.  Adding a sleep before the breakpoint in the main thread is the 
only way to be reasonnably sure (assuming 1 second is enough...) that 
both threads will have reached the infinite loop.

Actually, it might work by putting thread-specific breakpoints on the 
single-line infinite loop, then doing two "continue".  This way I think 
we would be guaranteed that the two threads stop exactly at that line.  
With a regular breakpoint it might not work, since a thread could hit 
the breakpoint twice while the other still hasn't reached it.

>> +int
>> +main (void)
>> +{
>> +  int i = 0;
>> +  pthread_t threads[NUM_THREADS];
>> +
>> +  for (i = 0; i < NUM_THREADS; i++)
>> +    pthread_create (&threads[i], NULL, child_function, NULL);
>> +
>> +  /* Leave enough time for the threads to reach their infinite loop. 
>> */
>> +  sleep (1);
>> +
>> +  i = 0; /* main break line */
>> +
>> +  sleep (2);
>> +
>> +  /* Allow the test to exit cleanly.  */
>> +  quit = 1;
>> +
>> +  for (i = 0; i < NUM_THREADS; i++)
>> +    pthread_join (threads[i], NULL);
> 
> 
> Hmm, looks like this version of the test still runs forever.

I don't think so, the main thread sets the quit flag which unblocks the 
threads.  If you run the executable you'll see it exits in ~2 seconds.

>> +#
>> +# You should have received a copy of the GNU General Public License
>> +# along with this program.  If not, see 
>> <http://www.gnu.org/licenses/>.
>> +
>> +# This test checks that thread, select-frame, frame or inferior 
>> selection
>> +# events are properly sent to all uis.
> 
> This comment is talking about "select-frame", which I take is
> the CLI command's name, not a selection event.  It feels like the 
> comment
> could be tweaked to be a bit clearer.  Something like:
> 
> # This test checks that the "thread", "select-frame", "frame" and 
> "inferior"
> # commands send the appropriate user-selection-change events to all 
> UIs.
> 
> Maybe say something about MI commands too, if the test exercises them.

Good idea.  It became this:

# This test checks that the "thread", "select-frame", "frame" and 
"inferior"
# CLI commands, as well as the "-thread-select" and 
"-stack-select-frame" MI
# commands send the appropriate user-selection-change events to all UIs.

>> +#
>> +# This test considers the case where console and mi are two different 
>> uis
>> +# and mi is created with the new-ui command.
>> +#
>> +# It also considers the case where the console commands are sent 
>> directly in
>> +# the mi channel as described in PR 20487.
> 
> Could you do a quick skim over the test and uppercase "mi" and "ui",
> as in "MI", and "UIs".  IMO, that makes the comments easier to grok.

I think that too, I changed them when I saw them, but I must've missed 
some.  I think I got them all now.  I changed those in the test messages 
too.

> 
>> +# Continue inferior INF until the breakpoint indicating the threads 
>> are started.
>> +
>> +proc test_continue_to_start { mode inf } {
>> +    global gdb_prompt gdb_spawn_id gdb_main_spawn_id
>> +
>> +    if { $gdb_spawn_id != $gdb_main_spawn_id } {
>> +	error "This should not happen."
>> +    }
>> +
>> +    with_test_prefix "inferior $inf" {
>> +	with_spawn_id $gdb_main_spawn_id {
>> +	    gdb_continue_to_breakpoint "main breakpoint"
>> +
>> +	    if { $mode == "non-stop" } {
>> +		gdb_test "thread $inf.2" ".*" "switch to thread $inf.2"
>> +
>> +		send_gdb "interrupt\n"
>> +		gdb_expect {
> 
> gdb_test_multiple ?

Like this?

     set test "interrupt thread $inf.2"

     send_gdb "interrupt\n"
     gdb_test_multiple "" $test {
         -re "Thread.*2.*stopped" {
             pass $test
         }
     }

>> +
>> +# Match a regular expression, or ensure that there was no output.
>> +#
>> +# If RE is non-empty, try to match the content of the program output 
>> (using the
>> +# current spawn_id) and pass/fail TEST accordingly.
>> +# If RE is empty, ensure that the program did not output anything.
>> +
>> +proc match_re_or_ensure_not_output { re test } {
>> +    if { $re != "" } {
>> +	gdb_expect {
> 
> gdb_test_multiple?  Or maybe this is used by MI too?

Indeed it it used by both.

>> +    with_test_prefix "thread 1.2 with --thread" {
>> +	# Test selecting a thread from MI with a --thread option.  This test
>> +	# verifies that even if the thread GDB would switch to is the same 
>> has
>> +	# the thread specified with --thread, an event is still sent to CLI.
>> +	# In this case this is thread 1.2
>> +
>> +	set mi_re [make_mi_re $mode 2 0 response]
>> +	set cli_re [make_cli_re $mode -1 1.2 0]
>> +
>> +	with_spawn_id $mi_spawn_id {
>> +	    mi_gdb_test "-thread-select --thread 2 2" $mi_re 
>> "-thread-select"
>> +	}
>> +
>> +	with_spawn_id $gdb_main_spawn_id {
>> +	    # TODO: it doesn't work as of now.
>> +	    # match_re_or_ensure_not_output "$cli_re\r\n" "-thread-select, 
>> event on cli"
>> +	}
> 
> Is there a plan here?

I think that will go in the same basket as the fact that any MI command 
with --thread currently changes the selected thread silently (without 
any =thread-selected event).  Currently, --thread changes the thread tot 
he desired one, then when the mi_cmd_thread_select tries to change the 
thread, it thinks that it was already the current thread, so that an 
event isn't necessary.  This should get fixed in the next iteration, 
when we split the concepts of user-selected-ptid and 
internally-selected-ptid.  Specifying --thread won't mess with the 
user-selected-ptid, but if you do "-thread-select --thread 2 2", then 
mi_cmd_thread_select will change the user-selected-ptid, generating an 
event.

It's not pretty to leave it like this in the test though.  Should I 
create a bug right now and kfail it?  Leave it commented out but put a 
better description?

Thanks,

Simon

^ permalink raw reply	[flat|nested] 21+ messages in thread

* Re: [PATCH master+7.12 v2 1/3] Introduce cleanup to restore current_uiout
  2016-09-15  3:24     ` Simon Marchi
@ 2016-09-16 18:18       ` Pedro Alves
       [not found]         ` <0c9914b2-f012-3b59-f127-04e70a7f867a@ericsson.com>
  0 siblings, 1 reply; 21+ messages in thread
From: Pedro Alves @ 2016-09-16 18:18 UTC (permalink / raw)
  To: Simon Marchi, Simon Marchi, gdb-patches

On 09/15/2016 04:23 AM, Simon Marchi wrote:
> On 14/09/16 01:56 PM, Pedro Alves wrote:
>> On 09/14/2016 06:45 PM, Simon Marchi wrote:
>>> Make a globally available cleanup from a pre-existing one in infrun.c.
>>> This is used in the following patch.
>>>
>>> gdb/ChangeLog:
>>>
>>> 	* infrun.c (restore_current_uiout_cleanup): Move to ui-out.c.
>>> 	(print_stop_event): Use make_cleanup_restore_current_uiout.
>>> 	* ui-out.c (restore_current_uiout_cleanup): Move from infrun.c.
>>> 	(make_cleanup_restore_current_uiout): New function definition.
>>> 	* ui-out.h (make_cleanup_restore_current_uiout): New function
>>> 	definition.
>>
>> OK.
>>
>> Thanks,
>> Pedro Alves
> 
> A little addition, I replace the usage of make_cleanup_restore_ui_out in python.c
> by this new make_cleanup_restore_current_uiout, and remove
> make_cleanupt_restore_ui_out.

OK.

Thanks,
Pedro Alves

^ permalink raw reply	[flat|nested] 21+ messages in thread

* Re: [PATCH master+7.12 v2 2/3] Emit inferior, thread and frame selection events to all UIs
  2016-09-15 16:21     ` Simon Marchi
@ 2016-09-16 18:26       ` Pedro Alves
  0 siblings, 0 replies; 21+ messages in thread
From: Pedro Alves @ 2016-09-16 18:26 UTC (permalink / raw)
  To: Simon Marchi; +Cc: gdb-patches, Antoine Tremblay

On 09/15/2016 05:20 PM, Simon Marchi wrote:
> On 16-09-14 02:30 PM, Pedro Alves wrote:
>> We could solve that by adding the thread group id (inferior id) to
>> the notification, I think:
>>
>>  =thread-selected,id="3",thread-group="i1",frame="..."
>>
>>  =thread-selected,id="0",thread-group="i2",frame="..."
>>
>> ...
>>

> That sounds good.  I think we can plan this for 7.13 though, the goal now is
> to get the basic use cases working, while not breaking any previous ones.

OK.

>> But, I suspect that we end up suppressing this case:
>>
>> (gdb) define thread-foo
>>> thread $arg0
>>> end
>> (gdb) thread-foo 2
>>
>> Contrived, but certainly not hard to imagine user-commands doing
>> something useful along with changing the selected thread.
>>
>> What happens in this case?
> 
> I think that's actually the opposite.  The purpose of this whole ugly condition
> is to avoid sending a second event, if we removed that whole part with strcmp's,
> we would end up sending events twice.  It would not be nice, but it would not be
> as bad as not sending any event.

Ah!

> That big condition is clearly unclear though :).  I suggest breaking it in its own
> function, where it will be more readable (even though not technically better):

Indeed, that'd be a great boon.

> 
> And then the condition becomes readable, and we understand the intent:
> 
> 
> 	  /* If the command already reports the thread change, no need to do it
> 	     again.  */
> 	  && !command_sends_thread_selected_event (command))
> 	  ...

Awesome!

> 
> 
> To address you uneasiness with -thread-select, let's see how that that code interacts
> with that command.  First, an event about the thread change is sent in
> mi_cmd_thread_select.  Then, command_sends_thread_selected_event will return true,
> making us skip sending the event again.
> 
> WDYT?

I like it.

Thanks,
Pedro Alves

^ permalink raw reply	[flat|nested] 21+ messages in thread

* Re: [PATCH master+7.12 v2 3/3] Add test for user context selection sync
  2016-09-16  2:02     ` Simon Marchi
@ 2016-09-21 16:43       ` Pedro Alves
  2016-09-21 21:38         ` Simon Marchi
  0 siblings, 1 reply; 21+ messages in thread
From: Pedro Alves @ 2016-09-21 16:43 UTC (permalink / raw)
  To: Simon Marchi; +Cc: Simon Marchi, gdb-patches, Antoine Tremblay

On 09/16/2016 03:01 AM, Simon Marchi wrote:
> On 2016-09-14 15:31, Pedro Alves wrote:
>> Hi Simon,
>>
>> I didn't try to understand everything in detail, but overall
>> it looks very nice now.  Thank you very much.  A few comments below.
> 
> Oops, while trying to rerun the full testsuite, I noticed that it leaves
> "-ex \"set non-stop 1\"" in the GDBFLAGS... that's not good.  You can
> see it easily if you swap the order of the "all-stop" and "non-stop"
> variations.  Can you spot why the save_vars isn't working as intended? 
> When I use a temporary variable instead, it works as intended...

Did you ever figure this out?

>> Did you try using barriers and breakpoints?  Several tests use that
>> to make sure threads are past a point.
> 
> I tried, but the issue is that depending on the scheduling, the threads
> might still be in the pthread_barrier_wait function when you stop.
> 
> Consider this pseudo-code:
> 
> thread_function:
>   barrier_wait
>   infinite_loop # This is where I want the threads to be stopped.
> 
> main_thread ():
>   initialize_barrier with n = 3
>   spawn_thread (thread_function)
>   spawn_thread (thread_function)
>   barrier_wait
>   breakpoint
> 
> Once the main thread hits the breakpoint, we have the assurance that the
> threads have started, but we don't know where they are.  They might not
> have exited the barrier_wait function, or they might be in the infinite
> loop.  Adding a sleep before the breakpoint in the main thread is the
> only way to be reasonnably sure (assuming 1 second is enough...) that
> both threads will have reached the infinite loop.
> 
> Actually, it might work by putting thread-specific breakpoints on the
> single-line infinite loop, then doing two "continue".  This way I think
> we would be guaranteed that the two threads stop exactly at that line. 
> With a regular breakpoint it might not work, since a thread could hit
> the breakpoint twice while the other still hasn't reached it.

What I've done in the past is something very much like your breakpoints
idea.  Though, instead of a thread-specific breakpoint, I used a normal
breakpoint + "set scheduler-locking on" to control which thread got to
the breakpoint.  See multiple-step-overs.exp, for example.
Using a thread-specific breakpoint maybe makes that a little simpler,
worth a try.

>> Hmm, looks like this version of the test still runs forever.
> 
> I don't think so, the main thread sets the quit flag which unblocks the
> threads.  If you run the executable you'll see it exits in ~2 seconds.

Ah, OK.

>> gdb_test_multiple ?
> 
> Like this?
> 
>     set test "interrupt thread $inf.2"
> 
>     send_gdb "interrupt\n"
>     gdb_test_multiple "" $test {
>         -re "Thread.*2.*stopped" {
>             pass $test
>         }
>     }

No need for the separate send_gdb call:

     set test "interrupt thread $inf.2"
     gdb_test_multiple "interrupt" $test {
         -re "Thread.*2.*stopped" {
             pass $test
         }
     }


>>> +    with_spawn_id $gdb_main_spawn_id {
>>> +        # TODO: it doesn't work as of now.
>>> +        # match_re_or_ensure_not_output "$cli_re\r\n"
>>> "-thread-select, event on cli"
>>> +    }
>>
>> Is there a plan here?
> 
> I think that will go in the same basket as the fact that any MI command
> with --thread currently changes the selected thread silently (without
> any =thread-selected event).  Currently, --thread changes the thread tot
> he desired one, then when the mi_cmd_thread_select tries to change the
> thread, it thinks that it was already the current thread, so that an
> event isn't necessary.  This should get fixed in the next iteration,
> when we split the concepts of user-selected-ptid and
> internally-selected-ptid.  Specifying --thread won't mess with the
> user-selected-ptid, but if you do "-thread-select --thread 2 2", then
> mi_cmd_thread_select will change the user-selected-ptid, generating an
> event.
> 
> It's not pretty to leave it like this in the test though.  Should I
> create a bug right now and kfail it?  Leave it commented out but put a
> better description?

There's no right or wrong answer, but since you've already written the 
bits, I'd be inclined to file bug and kfail.

Thanks,
Pedro Alves

^ permalink raw reply	[flat|nested] 21+ messages in thread

* Re: [PATCH master+7.12 v2 3/3] Add test for user context selection sync
  2016-09-21 16:43       ` Pedro Alves
@ 2016-09-21 21:38         ` Simon Marchi
  2016-09-22  1:56           ` Simon Marchi
  0 siblings, 1 reply; 21+ messages in thread
From: Simon Marchi @ 2016-09-21 21:38 UTC (permalink / raw)
  To: Pedro Alves, Simon Marchi; +Cc: gdb-patches, Antoine Tremblay

On 16-09-21 12:40 PM, Pedro Alves wrote:
>> Oops, while trying to rerun the full testsuite, I noticed that it leaves
>> "-ex \"set non-stop 1\"" in the GDBFLAGS... that's not good.  You can
>> see it easily if you swap the order of the "all-stop" and "non-stop"
>> variations.  Can you spot why the save_vars isn't working as intended? 
>> When I use a temporary variable instead, it works as intended...
> 
> Did you ever figure this out?

As you found and mentioned on IRC, it was a stray invisible character:

save_vars { <stray>GDBFLAGS } {
  ...
}

The result was that <stray>GDBFLAGS was saved, instead of GDBFLAGS...  I
would have searched for so long, so thanks a lot!

> What I've done in the past is something very much like your breakpoints
> idea.  Though, instead of a thread-specific breakpoint, I used a normal
> breakpoint + "set scheduler-locking on" to control which thread got to
> the breakpoint.  See multiple-step-overs.exp, for example.
> Using a thread-specific breakpoint maybe makes that a little simpler,
> worth a try.

I'll try and send an updated patch if it works.

>>> gdb_test_multiple ?
>>
>> Like this?
>>
>>     set test "interrupt thread $inf.2"
>>
>>     send_gdb "interrupt\n"
>>     gdb_test_multiple "" $test {
>>         -re "Thread.*2.*stopped" {
>>             pass $test
>>         }
>>     }
> 
> No need for the separate send_gdb call:
> 
>      set test "interrupt thread $inf.2"
>      gdb_test_multiple "interrupt" $test {
>          -re "Thread.*2.*stopped" {
>              pass $test
>          }
>      }

Yep, it works fine with that.

>> I think that will go in the same basket as the fact that any MI command
>> with --thread currently changes the selected thread silently (without
>> any =thread-selected event).  Currently, --thread changes the thread tot
>> he desired one, then when the mi_cmd_thread_select tries to change the
>> thread, it thinks that it was already the current thread, so that an
>> event isn't necessary.  This should get fixed in the next iteration,
>> when we split the concepts of user-selected-ptid and
>> internally-selected-ptid.  Specifying --thread won't mess with the
>> user-selected-ptid, but if you do "-thread-select --thread 2 2", then
>> mi_cmd_thread_select will change the user-selected-ptid, generating an
>> event.
>>
>> It's not pretty to leave it like this in the test though.  Should I
>> create a bug right now and kfail it?  Leave it commented out but put a
>> better description?
> 
> There's no right or wrong answer, but since you've already written the 
> bits, I'd be inclined to file bug and kfail.

Ok I'll do that.

Thanks,

Simon

^ permalink raw reply	[flat|nested] 21+ messages in thread

* Re: [PATCH master+7.12 v2 3/3] Add test for user context selection sync
  2016-09-21 21:38         ` Simon Marchi
@ 2016-09-22  1:56           ` Simon Marchi
  0 siblings, 0 replies; 21+ messages in thread
From: Simon Marchi @ 2016-09-22  1:56 UTC (permalink / raw)
  To: Pedro Alves, Simon Marchi; +Cc: gdb-patches, Antoine Tremblay

On 16-09-21 02:29 PM, Simon Marchi wrote:
>> What I've done in the past is something very much like your breakpoints
>> idea.  Though, instead of a thread-specific breakpoint, I used a normal
>> breakpoint + "set scheduler-locking on" to control which thread got to
>> the breakpoint.  See multiple-step-overs.exp, for example.
>> Using a thread-specific breakpoint maybe makes that a little simpler,
>> worth a try.
> 
> I'll try and send an updated patch if it works.

After messing with this for a while, I think I'll give up for now, unless you
think having that sleep in there is out of the question.  I have been trying to
do roughly this (all-stop mode):

 1. Start inferior 1, break when the two child threads are started.
 2. Setup two thread specific breakpoints (for the two child threads) at the target line.
 3. Do continue twice in a row.

For the first inferior, I get two stops, as expected, because of the two threads
hitting their breakpoint.  For the second inferior, the first "continue" stops, but
the second doesn't, it just hangs forever.  Inspecting the state of the threads at
this moment shows that the thread that did not hit his breakpoint is past it, so it
somehow missed it.  It sounds like a bug in GDB.  I'll try to make a reproducer, but
I don't think the present test should be blocked because of that.

Simon

^ permalink raw reply	[flat|nested] 21+ messages in thread

* Re: [PATCH master+7.12 v2 1/3] Introduce cleanup to restore current_uiout
       [not found]         ` <0c9914b2-f012-3b59-f127-04e70a7f867a@ericsson.com>
@ 2016-10-03 21:25           ` Simon Marchi
  0 siblings, 0 replies; 21+ messages in thread
From: Simon Marchi @ 2016-10-03 21:25 UTC (permalink / raw)
  To: Pedro Alves; +Cc: gdb-patches

On 16-09-16 03:51 PM, Simon Marchi wrote:
> On 16-09-16 02:18 PM, Pedro Alves wrote:
>> OK.
> 
> Thanks, pushed!

I had forgotten to push this to the 7.12 branch.  It was necessary for the
user-selection notification patches to build.  I just pushed it.

^ permalink raw reply	[flat|nested] 21+ messages in thread

end of thread, other threads:[~2016-10-03 21:25 UTC | newest]

Thread overview: 21+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-09-14 17:46 [PATCH master+7.12 v2 0/3] Emit user selection change notifications on all UIs Simon Marchi
2016-09-14 17:46 ` [PATCH master+7.12 v2 1/3] Introduce cleanup to restore current_uiout Simon Marchi
2016-09-14 17:56   ` Pedro Alves
2016-09-15  3:24     ` Simon Marchi
2016-09-16 18:18       ` Pedro Alves
     [not found]         ` <0c9914b2-f012-3b59-f127-04e70a7f867a@ericsson.com>
2016-10-03 21:25           ` Simon Marchi
2016-09-14 18:11   ` Tom Tromey
2016-09-14 18:18     ` Simon Marchi
2016-09-14 18:32       ` Pedro Alves
2016-09-14 19:12         ` Tom Tromey
2016-09-15  3:17           ` Simon Marchi
2016-09-14 17:46 ` [PATCH master+7.12 v2 3/3] Add test for user context selection sync Simon Marchi
2016-09-14 19:31   ` Pedro Alves
2016-09-16  2:02     ` Simon Marchi
2016-09-21 16:43       ` Pedro Alves
2016-09-21 21:38         ` Simon Marchi
2016-09-22  1:56           ` Simon Marchi
2016-09-14 17:46 ` [PATCH master+7.12 v2 2/3] Emit inferior, thread and frame selection events to all UIs Simon Marchi
2016-09-14 18:30   ` Pedro Alves
2016-09-15 16:21     ` Simon Marchi
2016-09-16 18:26       ` Pedro Alves

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).