Emit inferior, thread and frame selection events to all UIs

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 addresses 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.

The function command_notifies_uscc_observer was added to extract
the rather complicated logical expression from the if statement.  It is
also now clearer what that logic does: if the command used by the user
already notifies the user_selected_context_changed observer, there is
not need to notify it again.  It therefore protects again emitting the
event twice.

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 send it if command already sends the notification.
	(command_notifies_uscc_observer): New function.
	(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.
This commit is contained in:
Antoine Tremblay 2016-10-03 16:52:44 -04:00 committed by Simon Marchi
parent 0ee0f92162
commit 4034d0ff52
23 changed files with 505 additions and 90 deletions

View file

@ -1,3 +1,61 @@
2016-10-03 Antoine Tremblay <antoine.tremblay@ericsson.com>
2016-10-03 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 send it if command already sends the notification.
(command_notifies_uscc_observer): New function.
(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.
2016-09-29 Jan Kratochvil <jan.kratochvil@redhat.com> 2016-09-29 Jan Kratochvil <jan.kratochvil@redhat.com>
PR gdb/20609 - attach of JIT-debug-enabled inf 7.11.1 regression PR gdb/20609 - attach of JIT-debug-enabled inf 7.11.1 regression

View file

@ -152,6 +152,10 @@ signal-event EVENTID
=record-started,thread-group="i1",method="btrace",format="bts" =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 * New targets
Andes NDS32 nds32*-*-elf Andes NDS32 nds32*-*-elf

View file

@ -253,6 +253,7 @@ add_cmd (const char *name, enum command_class theclass, cmd_cfunc_ftype *fun,
c->user_commands = NULL; c->user_commands = NULL;
c->cmd_pointer = NULL; c->cmd_pointer = NULL;
c->alias_chain = NULL; c->alias_chain = NULL;
c->suppress_notification = NULL;
return c; 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); return add_alias_cmd (name, oldname, theclass, abbrev_flag, &cmdlist);
} }
/* 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 /* Recursively walk the commandlist structures, and print out the
documentation of commands that match our regex in either their documentation of commands that match our regex in either their
name, or their documentation. name, or their documentation.
@ -1885,7 +1901,19 @@ void
cmd_func (struct cmd_list_element *cmd, char *args, int from_tty) cmd_func (struct cmd_list_element *cmd, char *args, int from_tty)
{ {
if (cmd_func_p (cmd)) if (cmd_func_p (cmd))
(*cmd->func) (cmd, args, from_tty); {
struct cleanup *cleanups = make_cleanup (null_cleanup, NULL);
if (cmd->suppress_notification != NULL)
{
make_cleanup_restore_integer (cmd->suppress_notification);
*cmd->suppress_notification = 1;
}
(*cmd->func) (cmd, args, from_tty);
do_cleanups (cleanups);
}
else else
error (_("Invalid command")); error (_("Invalid command"));
} }

View file

@ -218,6 +218,12 @@ struct cmd_list_element
/* Link pointer for aliases on an alias list. */ /* Link pointer for aliases on an alias list. */
struct cmd_list_element *alias_chain; 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, extern void help_cmd_list (struct cmd_list_element *, enum command_class,

View file

@ -37,6 +37,12 @@ struct cli_interp
struct ui_out *cli_uiout; 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, /* Returns the INTERP's data cast as cli_interp if INTERP is a CLI,
and returns NULL otherwise. */ and returns NULL otherwise. */
@ -229,6 +235,36 @@ cli_on_command_error (void)
display_gdb_prompt (NULL); 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. */ /* pre_command_loop implementation. */
void void
@ -393,4 +429,6 @@ _initialize_cli_interp (void)
observer_attach_no_history (cli_on_no_history); observer_attach_no_history (cli_on_no_history);
observer_attach_sync_execution_done (cli_on_sync_execution_done); observer_attach_sync_execution_done (cli_on_sync_execution_done);
observer_attach_command_error (cli_on_command_error); observer_attach_command_error (cli_on_command_error);
observer_attach_user_selected_context_changed
(cli_on_user_selected_context_changed);
} }

View file

@ -115,6 +115,17 @@ struct cmd_list_element;
typedef void cmd_cfunc_ftype (char *args, int from_tty); 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. */ /* Forward-declarations of the entry-points of cli/cli-decode.c. */
/* API to the manipulation of command lists. */ /* 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 *, extern struct cmd_list_element *add_com_alias (const char *, const char *,
enum command_class, int); 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 *, extern struct cmd_list_element *add_info (const char *,
cmd_cfunc_ftype *fun, cmd_cfunc_ftype *fun,
const char *); const char *);

View file

@ -53,6 +53,7 @@
#include "ui-file.h" #include "ui-file.h"
#include "host-defs.h" #include "host-defs.h"
#include "common/enum-flags.h"
/* Scope types enumerator. List the types of scopes the compiler will /* Scope types enumerator. List the types of scopes the compiler will
accept. */ accept. */
@ -743,6 +744,21 @@ enum block_enum
FIRST_LOCAL_BLOCK = 2 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" #include "utils.h"
#endif /* #ifndef DEFS_H */ #endif /* #ifndef DEFS_H */

View file

@ -1,3 +1,14 @@
2016-10-03 Antoine Tremblay <antoine.tremblay@ericsson.com>
2016-10-03 Simon Marchi <simon.marchi@ericsson.com>
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.
2016-09-28 Tom Tromey <tom@tromey.com> 2016-09-28 Tom Tromey <tom@tromey.com>
* gdb.texinfo (Packets) <z0>: Use "software breakpoint" rather * gdb.texinfo (Packets) <z0>: Use "software breakpoint" rather

View file

@ -25832,13 +25832,13 @@ identifier for thread and frame to operate on.
Usually, each top-level window in a frontend allows the user to select 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 a thread and a frame, and remembers the user selection for further
operations. However, in some cases @value{GDBN} may suggest that the operations. However, in some cases @value{GDBN} may suggest that the
current thread be changed. For example, when stopping on a breakpoint current thread or frame be changed. For example, when stopping on a
it is reasonable to switch to the thread where breakpoint is hit. For breakpoint it is reasonable to switch to the thread where breakpoint is
another example, if the user issues the CLI @samp{thread} command via hit. For another example, if the user issues the CLI @samp{thread} or
the frontend, it is desirable to change the frontend's selected thread to the @samp{frame} commands via the frontend, it is desirable to change the
one specified by user. @value{GDBN} communicates the suggestion to frontend's selection to the one specified by user. @value{GDBN}
change current thread using the @samp{=thread-selected} notification. communicates the suggestion to change current thread and frame using the
No such notification is available for the selected frame at the moment. @samp{=thread-selected} notification.
Note that historically, MI shares the selected thread with CLI, so Note that historically, MI shares the selected thread with CLI, so
frontends used the @code{-thread-select} to execute commands in the frontends used the @code{-thread-select} to execute commands in the
@ -26484,13 +26484,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} contains the global @value{GDBN} identifier of the thread. The @var{gid}
field identifies the thread group this thread belongs to. field identifies the thread group this thread belongs to.
@item =thread-selected,id="@var{id}" @item =thread-selected,id="@var{id}"[,frame="@var{frame}"]
Informs that the selected thread was changed as result of the last Informs that the selected thread or frame were changed. This notification
command. This notification is not emitted as result of @code{-thread-select} is not emitted as result of the @code{-thread-select} or
command but is emitted whenever an MI command that is not documented @code{-stack-select-frame} commands, but is emitted whenever an MI command
to change the selected thread actually changes it. In particular, that is not documented to change the selected thread and frame actually
invoking, directly or indirectly (via user-defined command), the CLI changes them. In particular, invoking, directly or indirectly
@code{thread} command, will generate this notification. (via user-defined command), the CLI @code{thread} or @code{frame} commands,
will generate this notification. Changing the thread or 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 We suggest that in response to this notification, front ends
highlight the selected thread and cause subsequent commands to apply to highlight the selected thread and cause subsequent commands to apply to

View file

@ -307,3 +307,7 @@ This observer is used for internal testing. Do not use.
See testsuite/gdb.gdb/observer.exp. See testsuite/gdb.gdb/observer.exp.
@end deftypefun @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

View file

@ -704,6 +704,14 @@ extern CORE_ADDR get_pc_function_start (CORE_ADDR);
extern struct frame_info *find_relative_frame (struct frame_info *, int *); 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, extern void print_stack_frame (struct frame_info *, int print_level,
enum print_what print_what, enum print_what print_what,
int set_current_sal); int set_current_sal);

View file

@ -630,6 +630,10 @@ extern void validate_registers_access (void);
true iff we ever detected multiple threads. */ true iff we ever detected multiple threads. */
extern int show_thread_that_caused_stop (void); 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; extern struct thread_info *thread_list;
#endif /* GDBTHREAD_H */ #endif /* GDBTHREAD_H */

View file

@ -548,6 +548,24 @@ inferior_pid_to_str (int pid)
return _("<null>"); 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 /* Prints the list of inferiors and their details on UIOUT. This is a
version of 'info_inferior_command' suitable for use from MI. version of 'info_inferior_command' suitable for use from MI.
@ -726,13 +744,6 @@ inferior_command (char *args, int from_tty)
if (inf == NULL) if (inf == NULL)
error (_("Inferior ID %d not known."), num); 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 != 0)
{ {
if (inf->pid != ptid_get_pid (inferior_ptid)) if (inf->pid != ptid_get_pid (inferior_ptid))
@ -746,9 +757,10 @@ inferior_command (char *args, int from_tty)
switch_to_thread (tp->ptid); switch_to_thread (tp->ptid);
} }
printf_filtered (_("[Switching to thread %s (%s)] "), observer_notify_user_selected_context_changed
print_thread_id (inferior_thread ()), (USER_SELECTED_INFERIOR
target_pid_to_str (inferior_ptid)); | USER_SELECTED_THREAD
| USER_SELECTED_FRAME);
} }
else else
{ {
@ -758,14 +770,8 @@ inferior_command (char *args, int from_tty)
set_current_inferior (inf); set_current_inferior (inf);
switch_to_thread (null_ptid); switch_to_thread (null_ptid);
set_current_program_space (inf->pspace); set_current_program_space (inf->pspace);
}
if (inf->pid != 0 && is_running (inferior_ptid)) observer_notify_user_selected_context_changed (USER_SELECTED_INFERIOR);
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);
} }
} }

View file

@ -523,4 +523,7 @@ extern int number_of_inferiors (void);
extern struct inferior *add_inferior_with_spaces (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) */ #endif /* !defined (INFERIOR_H) */

View file

@ -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-frames", mi_cmd_stack_list_frames),
DEF_MI_CMD_MI ("stack-list-locals", mi_cmd_stack_list_locals), 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-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_MI ("symbol-list-lines", mi_cmd_symbol_list_lines),
DEF_MI_CMD_CLI ("target-attach", "attach", 1), DEF_MI_CMD_CLI ("target-attach", "attach", 1),
DEF_MI_CMD_MI ("target-detach", mi_cmd_target_detach), 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_CLI ("target-select", "target", 1),
DEF_MI_CMD_MI ("thread-info", mi_cmd_thread_info), 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-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 ("trace-define-variable", mi_cmd_trace_define_variable),
DEF_MI_CMD_MI_1 ("trace-find", mi_cmd_trace_find, DEF_MI_CMD_MI_1 ("trace-find", mi_cmd_trace_find,
&mi_suppress_notification.traceframe), &mi_suppress_notification.traceframe),

View file

@ -769,6 +769,7 @@ struct mi_suppress_notification mi_suppress_notification =
0, 0,
0, 0,
0, 0,
0,
}; };
/* Emit notification on changing a traceframe. */ /* 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 static int
report_initial_inferior (struct inferior *inf, void *closure) 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_command_param_changed (mi_command_param_changed);
observer_attach_memory_changed (mi_memory_changed); observer_attach_memory_changed (mi_memory_changed);
observer_attach_sync_execution_done (mi_on_sync_execution_done); observer_attach_sync_execution_done (mi_on_sync_execution_done);
observer_attach_user_selected_context_changed
(mi_user_selected_context_changed);
} }

View file

@ -53,6 +53,7 @@
#include "linespec.h" #include "linespec.h"
#include "extension.h" #include "extension.h"
#include "gdbcmd.h" #include "gdbcmd.h"
#include "observer.h"
#include <ctype.h> #include <ctype.h>
#include "gdb_sys_time.h" #include "gdb_sys_time.h"
@ -564,17 +565,29 @@ mi_cmd_thread_select (char *command, char **argv, int argc)
{ {
enum gdb_rc rc; enum gdb_rc rc;
char *mi_error_message; char *mi_error_message;
ptid_t previous_ptid = inferior_ptid;
if (argc != 1) if (argc != 1)
error (_("-thread-select: USAGE: threadnum.")); error (_("-thread-select: USAGE: threadnum."));
rc = gdb_thread_select (current_uiout, argv[0], &mi_error_message); 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) if (rc == GDB_RC_FAIL)
{ {
make_cleanup (xfree, mi_error_message); make_cleanup (xfree, mi_error_message);
error ("%s", 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 void
@ -2097,6 +2110,34 @@ mi_print_exception (const char *token, struct gdb_exception exception)
fputs_unfiltered ("\n", mi->raw_stdout); fputs_unfiltered ("\n", mi->raw_stdout);
} }
/* Determine whether the parsed command already notifies the
user_selected_context_changed observer. */
static int
command_notifies_uscc_observer (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
/* -thread-select already sends it. */
return strcmp (command->command, "thread-select") == 0;
}
}
void void
mi_execute_command (const char *cmd, int from_tty) mi_execute_command (const char *cmd, int from_tty)
{ {
@ -2124,9 +2165,16 @@ mi_execute_command (const char *cmd, int from_tty)
if (command != NULL) if (command != NULL)
{ {
ptid_t previous_ptid = inferior_ptid; ptid_t previous_ptid = inferior_ptid;
struct cleanup *cleanup = make_cleanup (null_cleanup, NULL);
command->token = token; 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) if (do_timings)
{ {
command->cmd_start = XNEW (struct mi_timestamp); command->cmd_start = XNEW (struct mi_timestamp);
@ -2161,10 +2209,9 @@ mi_execute_command (const char *cmd, int from_tty)
/* Don't try report anything if there are no threads -- /* Don't try report anything if there are no threads --
the program is dead. */ the program is dead. */
&& thread_count () != 0 && thread_count () != 0
/* -thread-select explicitly changes thread. If frontend uses that /* If the command already reports the thread change, no need to do it
internally, we don't want to emit =thread-selected, since again. */
=thread-selected is supposed to indicate user's intentions. */ && !command_notifies_uscc_observer (command))
&& strcmp (command->command, "thread-select") != 0)
{ {
struct mi_interp *mi struct mi_interp *mi
= (struct mi_interp *) top_level_interpreter_data (); = (struct mi_interp *) top_level_interpreter_data ();
@ -2185,22 +2232,14 @@ mi_execute_command (const char *cmd, int from_tty)
if (report_change) if (report_change)
{ {
struct thread_info *ti = inferior_thread (); observer_notify_user_selected_context_changed
struct cleanup *old_chain; (USER_SELECTED_THREAD | USER_SELECTED_FRAME);
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);
} }
} }
mi_parse_free (command); mi_parse_free (command);
do_cleanups (cleanup);
} }
} }
@ -2277,12 +2316,6 @@ mi_cmd_execute (struct mi_parse *parse)
current_context = 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) if (parse->cmd->argv_func != NULL)
{ {
parse->cmd->argv_func (parse->command, parse->argv, parse->argc); parse->cmd->argv_func (parse->command, parse->argv, parse->argc);

View file

@ -49,6 +49,8 @@ struct mi_suppress_notification
int traceframe; int traceframe;
/* Memory changed notification suppressed? */ /* Memory changed notification suppressed? */
int memory; int memory;
/* User selected context changed notification suppressed? */
int user_selected_context;
}; };
extern struct mi_suppress_notification mi_suppress_notification; extern struct mi_suppress_notification mi_suppress_notification;

View file

@ -51,6 +51,7 @@
#include "safe-ctype.h" #include "safe-ctype.h"
#include "symfile.h" #include "symfile.h"
#include "extension.h" #include "extension.h"
#include "observer.h"
/* The possible choices of "set print frame-arguments", and the value /* The possible choices of "set print frame-arguments", and the value
of this setting. */ of this setting. */
@ -141,6 +142,24 @@ frame_show_address (struct frame_info *frame,
return get_frame_pc (frame) != sal.pc; 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 /* Show or print a stack frame FRAME briefly. The output is formatted
according to PRINT_LEVEL and PRINT_WHAT printing the frame's according to PRINT_LEVEL and PRINT_WHAT printing the frame's
relative level, function name, argument list, and file name and 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 void
select_frame_command (char *level_exp, int from_tty) 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)); 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 /* 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 static void
frame_command (char *level_exp, int from_tty) frame_command (char *level_exp, int from_tty)
{ {
select_frame_command (level_exp, from_tty); struct frame_info *prev_frame = get_selected_frame_if_set ();
print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC, 1);
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 /* 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_command (char *count_exp, int from_tty)
{ {
up_silently_base (count_exp); 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 /* 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_command (char *count_exp, int from_tty)
{ {
down_silently_base (count_exp); 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);
} }
void void
return_command (char *retval_exp, int from_tty) 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_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\ Select a stack frame without printing anything.\n\
An argument specifies the frame to select.\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, _("\ add_com ("backtrace", class_stack, backtrace_command, _("\
Print backtrace of all stack frames, or innermost COUNT frames.\n\ Print backtrace of all stack frames, or innermost COUNT frames.\n\

View file

@ -1,3 +1,10 @@
2016-10-03 Antoine Tremblay <antoine.tremblay@ericsson.com>
2016-10-03 Simon Marchi <simon.marchi@ericsson.com>
PR gdb/20487
* gdb.mi/mi-pthreads.exp (check_mi_thread_command_set): Adapt
=thread-select-event check.
2016-09-29 Peter Bergner <bergner@vnet.ibm.com> 2016-09-29 Peter Bergner <bergner@vnet.ibm.com>
* gdb.arch/powerpc-power.exp <cmprb>: Update tests to account for * gdb.arch/powerpc-power.exp <cmprb>: Update tests to account for

View file

@ -53,8 +53,8 @@ proc check_mi_thread_command_set {} {
foreach thread $thread_list { foreach thread $thread_list {
mi_gdb_test "-interpreter-exec console \"thread $thread\"" \ mi_gdb_test "-interpreter-exec console \"thread $thread\"" \
".*\\^done\r\n=thread-selected,id=\"$thread\"" \ ".*=thread-selected,id=\"$thread\".*\r\n\\^done" \
"check =thread-selected: thread $thread" "check =thread-selected: thread $thread"
} }
} }

View file

@ -1923,7 +1923,7 @@ thread_apply_command (char *tidlist, int from_tty)
void void
thread_command (char *tidstr, int from_tty) thread_command (char *tidstr, int from_tty)
{ {
if (!tidstr) if (tidstr == NULL)
{ {
if (ptid_equal (inferior_ptid, null_ptid)) if (ptid_equal (inferior_ptid, null_ptid))
error (_("No thread selected")); error (_("No thread selected"));
@ -1943,10 +1943,31 @@ thread_command (char *tidstr, int from_tty)
} }
else else
error (_("No stack.")); error (_("No stack."));
return;
} }
else
{
ptid_t previous_ptid = inferior_ptid;
enum gdb_rc result;
gdb_thread_select (current_uiout, tidstr, NULL); 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;
/* 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'. */ /* Implementation of `thread name'. */
@ -2058,27 +2079,6 @@ do_captured_thread_select (struct ui_out *uiout, void *tidstr_v)
annotate_thread_changed (); annotate_thread_changed ();
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);
}
/* Since the current thread may have changed, see if there is any /* Since the current thread may have changed, see if there is any
exited thread we can now delete. */ exited thread we can now delete. */
prune_threads (); prune_threads ();
@ -2086,6 +2086,48 @@ do_captured_thread_select (struct ui_out *uiout, void *tidstr_v)
return GDB_RC_OK; 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)
{
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, ")]");
}
}
if (tp->state == THREAD_RUNNING)
{
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");
if (has_stack_frames ())
print_stack_frame_to_uiout (uiout, get_selected_frame (NULL),
1, SRC_AND_LOC, 1);
}
}
enum gdb_rc enum gdb_rc
gdb_thread_select (struct ui_out *uiout, char *tidstr, char **error_message) gdb_thread_select (struct ui_out *uiout, char *tidstr, char **error_message)
{ {

View file

@ -206,6 +206,37 @@ tui_on_command_error (void)
display_gdb_prompt (NULL); 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. */ /* These implement the TUI interpreter. */
static void * static void *
@ -323,4 +354,6 @@ _initialize_tui_interp (void)
observer_attach_no_history (tui_on_no_history); observer_attach_no_history (tui_on_no_history);
observer_attach_sync_execution_done (tui_on_sync_execution_done); observer_attach_sync_execution_done (tui_on_sync_execution_done);
observer_attach_command_error (tui_on_command_error); observer_attach_command_error (tui_on_command_error);
observer_attach_user_selected_context_changed
(tui_on_user_selected_context_changed);
} }