Summary: | Thread sync broken between mi and cli with new-ui command | ||
---|---|---|---|
Product: | gdb | Reporter: | Antoine Tremblay <antoine.tremblay> |
Component: | gdb | Assignee: | Antoine Tremblay <antoine.tremblay> |
Status: | RESOLVED FIXED | ||
Severity: | normal | CC: | marc.khouzam, pedro, simark |
Priority: | P2 | ||
Version: | 7.12 | ||
Target Milestone: | 7.12 | ||
Host: | Target: | ||
Build: | Last reconfirmed: | ||
Attachments: | thread-select-event patch |
Description
Antoine Tremblay
2016-08-18 14:09:42 UTC
I would argue this is a bug with the 'new-ui' functionality since the event is missing. I'd recommend targeting 7.12 for this. Fine with me. I think the fix will be to make mi_execute_command's code that emits the event loop over UIs. Should be quite safe. Pedro: Ooops I made a mistake in my description I meant if you do the same with mi created with new-ui like so : (gdb) new mi /dev/pts/0 New UI allocated (gdb) break 33 Breakpoint 1 at 0x4007b4: file ./thread-selected-sync.c, line 33. (gdb) r Starting program: /home/eantotr/src/test-thread-selected/test [Thread debugging using libthread_db enabled] Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1". [New Thread 0x7ffff77f6700 (LWP 24810)] [New Thread 0x7ffff6ff5700 (LWP 24811)] [Switching to Thread 0x7ffff6ff5700 (LWP 24811)] Thread 3 "test" hit Breakpoint 1, child_sub_function () at ./thread-selected-sync.c:33 33 while (!quit) /* set break here */ (gdb) thread 2 [Switching to thread 2 (Thread 0x7ffff77f6700 (LWP 24810))] #0 child_sub_function () at ./thread-selected-sync.c:33 33 while (!quit) /* set break here */ (gdb) Then you don't get an MI event. My solution at the moment is to do fire an event in gdb_thread_select... Even if I get into some little trouble with safe_execute_command. Note it's the same thing with a gdb -i=mi and new tui /dev/x command and then thread x in the tui console So my description was OK.. sorry for the confusion. But your suggestion would be a good fix if we were trying to fix that with gdb -i=mi if we where to input a cli command in that channel like my 1st example: (gdb) thread 1 &"thread 1\n" ~"[Switching to thread 1 (Thread 0x7ffff7fc7740 (LWP 15517))]\n" ~"#0 0x00007ffff7bc565b in pthread_join () from /lib/x86_64-linux-gnu/libpthread.so.0\n" ^done =thread-selected,id="1" And we would get nothing on the cli created with new-ui... We don't but that's not the issue. Unless I misunderstood your fix ? This sounds an awful lot like what Simon was handling around MI's --thread option. I mean, it's not just the "thread" command that changes current thread. We have at least "inferior", and "run&", "attach&", "core", and probably others. BTW, I notice that going from "no thread selected" -> "thread selected" doesn't emit an MI event, even when using CLI commands in MI directly (instead of using new-ui): inferior 2 &"inferior 2\n" ~"[Switching to inferior 2 [<null>] (<noexec>)]\n" ^done (gdb) thread &"thread\n" &"No thread selected\n" ^error,msg="No thread selected" (gdb) thread 1 &"thread 1\n" ~"[Switching to thread 1 (Thread 0x7ffff7fbd780 (LWP 13495))]\n" ~"#0 main (argc=1, argv=0x7fffffffd918) at /home/pedro/gdb/mygit/src/gdb/gdb.c:28\n" ~"28\t memset (&args, 0, sizeof args);\n" ^done (gdb) (In reply to Antoine Tremblay from comment #4) > (gdb) > thread 1 > &"thread 1\n" > ~"[Switching to thread 1 (Thread 0x7ffff7fc7740 (LWP 15517))]\n" > ~"#0 0x00007ffff7bc565b in pthread_join () from > /lib/x86_64-linux-gnu/libpthread.so.0\n" > ^done > =thread-selected,id="1" > > And we would get nothing on the cli created with new-ui... Right, and nothing on other MI UIs (you can have more than one, though obviously Eclipse doesn't care about that.). > > We don't but that's not the issue. > > Unless I misunderstood your fix ? No, I had misunderstood what you were reporting. Created attachment 9453 [details]
thread-select-event patch
The patch above is my work in progress not that it does not cover all the cases where the thread changes, but covers the cases were the user explicitly changes it. Simon's work should bring it a step further later. I see. From a quick skim (reading patches in bugzilla sucks), I think the observer notification calls would be much clearer if you replace the two boolean arguments with an enum or enum flags (common/enum-flags.h). + if (ptid_equal (inferior_ptid, null_ptid)) + return; + else + tp = inferior_thread (); + + /* The thread is mandatory. */ + if (tp == NULL) + return; this is redundant, because inferior_thread never returns NULL. I'd drop the else: if (ptid_equal (inferior_ptid, null_ptid)) return; tp = inferior_thread (); + current_ui->prompt_state = PROMPT_NEEDED; + display_gdb_prompt (NULL); + } Doesn't look like these work correctly when the UI was in sync mode? E.g.,: - Do "continue" in the console. Since that was a sync command, the console doesn't prompt until the target stops. - Select another thread in the MI UI. - We display the prompt in the console? (and input is disabled..) + if (ptid_equal (inferior_ptid, null_ptid)) + return; + else... indeed that was an ugly bit, fixed. + current_ui->prompt_state = PROMPT_NEEDED; + display_gdb_prompt (NULL); + } right, I wonder why I had this now... I removed it, thx. I also have some problems remaining with interpreter-exec , fixing those now... (In reply to Pedro Alves from comment #5) > This sounds an awful lot like what Simon was handling around MI's --thread > option. > > I mean, it's not just the "thread" command that changes current thread. We > have at least "inferior", and "run&", "attach&", "core", and probably others. Right, when we implement your suggestion (decouple user selection from gdb's internal selection), emitting the =thread-selected notification should be easy, as it will be clear when the user selection changes. There will be one or two functions responsible for modifying the user selection, so the event should be sent from there. The master branch has been updated by Simon Marchi <simark@sourceware.org>: https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=4034d0ff52b0f346efedd2d335ccbc672466da45 commit 4034d0ff52b0f346efedd2d335ccbc672466da45 Author: Antoine Tremblay <antoine.tremblay@ericsson.com> Date: Mon Oct 3 16:52:44 2016 -0400 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. The master branch has been updated by Simon Marchi <simark@sourceware.org>: https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=9c36d9544f0987cbd840a19552af3343252d28a0 commit 9c36d9544f0987cbd840a19552af3343252d28a0 Author: Antoine Tremblay <antoine.tremblay@ericsson.com> Date: Mon Oct 3 16:55:35 2016 -0400 Add test for user context selection sync 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 stopped with the same predictable backtrace (so that we can test frame switching). The barrier helps us know when the child threads are started. Then, scheduler-locking is used to bring each thread one by one to the position we expect them to be during the test. 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. The gdb-7.12-branch branch has been updated by Simon Marchi <simark@sourceware.org>: https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=1555a1e7846048adeee7b9734ab3ec200d776858 commit 1555a1e7846048adeee7b9734ab3ec200d776858 Author: Antoine Tremblay <antoine.tremblay@ericsson.com> Date: Mon Oct 3 16:52:44 2016 -0400 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. The gdb-7.12-branch branch has been updated by Simon Marchi <simark@sourceware.org>: https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=ec55637a35d74522d4a1fbb65ed4137803ed9d64 commit ec55637a35d74522d4a1fbb65ed4137803ed9d64 Author: Antoine Tremblay <antoine.tremblay@ericsson.com> Date: Mon Oct 3 16:55:35 2016 -0400 Add test for user context selection sync 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 stopped with the same predictable backtrace (so that we can test frame switching). The barrier helps us know when the child threads are started. Then, scheduler-locking is used to bring each thread one by one to the position we expect them to be during the test. 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. With those fixed now pushed, is there anything in this bug that needs to be addressed? I think we can close it. Thanks! |