diff options
author | Simon Marchi <simon.marchi@ericsson.com> | 2016-12-08 13:06:14 -0500 |
---|---|---|
committer | Simon Marchi <simon.marchi@ericsson.com> | 2017-02-23 17:25:30 -0500 |
commit | 44c04ee9bf959b40819de6327500557fbb5d8c4a (patch) | |
tree | f2fae0b73585f6b67cfbb0f7c29d65f9d70ebc77 /gdb/mi/mi-main.c | |
parent | a567769b813b2538bebc97d689fc0739f172028e (diff) | |
download | binutils-users/simark/user-selection-rfc.zip binutils-users/simark/user-selection-rfc.tar.gz binutils-users/simark/user-selection-rfc.tar.bz2 |
Decouple user selection from internal selectionusers/simark/user-selection-rfc
I am sending this as an RFC because it's far from complete and
definitive, but I'd like to gather some comments and opinions before
going further in this direction.
The goal of this patch is to decouple the notion of the user-selected
inferior/thread/frame from GDB's internally selected
inferior/thread/frame.
Currently, for example, the inferior_ptid variable has two jobs:
- it's the user-selected thread: it's changed by the "thread" command.
Other commands (continue, backtrace, etc) apply to this thread.
- it's the internally-selected thread: it defines the thread GDB is
currently "working" on. For example, implementations of
to_xfer_partial will refer to it to know from which thread to
read/write memory.
Because of this dual usage, if we want to do some operations on a thread
other than the currently selected one, we have to save the current
inferior/thread/frame and restore them when we're done. Failing to do
so would result in an unexpected selection switch for the user.
To improve this, Pedro suggested in [1] to decouple the two concepts. This
is essentially what this patch is trying to do.
A new "user_selection" object is introduced, which contains the selected
inferior/thread/frame from the point of view of the user. Before every
command, we "apply" this selection to the core of GDB to make sure the
internal selection matches the user selection.
There is a single user selection for the whole GDB (named "global
user-selection"), but as was mentioned in the linked thread, it opens
the door to having different selections for different UIs. This means
that each UI would have its own user-selection object, which would be
applied to the core prior to executing commands from this UI.
The global user-selection object only gets modified when we really
intend to change it. It can be because of the thread / -thread-select /
up / down / frame / inferior commands, a breakpoint hit in all-stop, an
inferior exit, etc.
The problem that initially prompted this effort is that the "--thread"
flag of MI commands changes the user-selected thread under the user's
feet. My initial attempt to fix it was to restore the selection after
the MI command execution. However, some cases are hard to get right.
For example:
(thread 1 is currently selected)
-interpreter-exec --thread 2 console "thread 3"
Restoring the selected thread to thread 1 after the MI command execution
wrongfully cancels the switch to thread 3. So it's hard to determine
when we should or shouldn't restore. With the current patch, it works
naturally: the --thread flag doesn't touch the user-selected thread,
only the internal one. The "thread 3" command updates the user
selection.
Another difficulty is to send the right notifications to MI when the
user selection changes. That means to not miss any, but not send too
many either. Getting it somewhat right lead to ugly hacks (see the
command_notifies_uscc_observer function) and even then it's not perfect
(see the kfails in user-selected-context-sync.exp test). With the
proposed method, it's easy to know when the user-selection changes and
send notifications.
With this patch, there are probably a few usage of
make_cleanup_restore_current_thread that are not needed anymore, if they
are only used to restore the user selection. I kept removing them for a
later time though.
In the current state, there are a few minor regressions in the testsuite
(especially some follow-fork stuff I'm not sure how to handle), but the
vast majority of the previously passing tests still pass.
Comments are welcome!
Thanks,
Simon
[1] https://sourceware.org/ml/gdb-patches/2016-08/msg00031.html
Diffstat (limited to 'gdb/mi/mi-main.c')
-rw-r--r-- | gdb/mi/mi-main.c | 89 |
1 files changed, 8 insertions, 81 deletions
diff --git a/gdb/mi/mi-main.c b/gdb/mi/mi-main.c index cf4e45a..63458d9 100644 --- a/gdb/mi/mi-main.c +++ b/gdb/mi/mi-main.c @@ -54,6 +54,7 @@ #include "extension.h" #include "gdbcmd.h" #include "observer.h" +#include "user-selection.h" #include <ctype.h> #include "run-time-clock.h" @@ -560,31 +561,15 @@ mi_cmd_target_flash_erase (char *command, char **argv, int argc) void 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); + thread_select (argv[0], false); - /* 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); - } + user_selection *us = global_user_selection (); - print_selected_thread_frame (current_uiout, + print_selected_thread_frame (current_uiout, us, 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 @@ -2025,6 +2010,8 @@ captured_mi_execute_command (struct ui_out *uiout, struct mi_parse *context) { char *argv[2]; + apply_global_user_selection (); + /* A CLI command was read from the input stream. */ /* This "feature" will be removed as soon as we have a complete set of mi commands. */ @@ -2085,34 +2072,6 @@ mi_print_exception (const char *token, struct gdb_exception exception) 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 mi_execute_command (const char *cmd, int from_tty) { @@ -2139,7 +2098,6 @@ 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; @@ -2178,39 +2136,6 @@ mi_execute_command (const char *cmd, int from_tty) bpstat_do_actions (); - if (/* The notifications are only output when the top-level - interpreter (specified on the command line) is MI. */ - interp_ui_out (top_level_interpreter ())->is_mi_like_p () - /* Don't try report anything if there are no threads -- - the program is dead. */ - && thread_count () != 0 - /* If the command already reports the thread change, no need to do it - again. */ - && !command_notifies_uscc_observer (command)) - { - struct mi_interp *mi = (struct mi_interp *) top_level_interpreter (); - int report_change = 0; - - if (command->thread == -1) - { - report_change = (!ptid_equal (previous_ptid, null_ptid) - && !ptid_equal (inferior_ptid, previous_ptid) - && !ptid_equal (inferior_ptid, null_ptid)); - } - else if (!ptid_equal (inferior_ptid, null_ptid)) - { - struct thread_info *ti = inferior_thread (); - - report_change = (ti->global_num != command->thread); - } - - if (report_change) - { - observer_notify_user_selected_context_changed - (USER_SELECTED_THREAD | USER_SELECTED_FRAME); - } - } - mi_parse_free (command); do_cleanups (cleanup); @@ -2224,6 +2149,8 @@ mi_cmd_execute (struct mi_parse *parse) cleanup = prepare_execute_command (); + apply_global_user_selection (); + if (parse->all && parse->thread_group != -1) error (_("Cannot specify --thread-group together with --all")); |