Bug 20684 - MI commands with "--thread X" cause a switch of the user-selected thread
Summary: MI commands with "--thread X" cause a switch of the user-selected thread
Status: RESOLVED FIXED
Alias: None
Product: gdb
Classification: Unclassified
Component: mi (show other bugs)
Version: HEAD
: P2 normal
Target Milestone: 12.1
Assignee: Not yet assigned to anyone
URL:
Keywords:
: 19087 (view as bug list)
Depends on:
Blocks:
 
Reported: 2016-10-11 16:13 UTC by Simon Marchi
Modified: 2024-01-13 23:26 UTC (History)
3 users (show)

See Also:
Host:
Target:
Build:
Last reconfirmed:


Attachments
More thorough patch to fix the gdb-mi problem as well (493 bytes, patch)
2022-03-27 09:49 UTC, Eli Zaretskii
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Simon Marchi 2016-10-11 16:13:44 UTC
Issuing an MI command with a --thread or --thread-group flag changes the user-selected thread.  That is a problem if the user uses a CLI in a front-end which does MI requests in the background, as the currently selected thread can change at any moment without warning.

For example:

Thread 1 is selected:

(gdb) 
info threads
&"info threads\n"
~"  Id   Target Id         Frame \n"
~"* 1    Thread 0x7ffff7fc5740 (LWP 12348) \"user-selected-c\" main () at /home/emaisin/src/binutils-gdb/gdb/testsuite/gdb.mi/user-selected-context-sync.c:56\n"
~"  2    Thread 0x7ffff77f6700 (LWP 12387) \"user-selected-c\" 0x00007ffff7bc923c in pthread_barrier_wait () from /lib/x86_64-linux-gnu/libpthread.so.0\n"
^done

Do a command with --thread 2:

(gdb) 
-gdb-show --thread 2 non-stop
^done,value="off"

Thread 2 is now selected:

(gdb) 
info threads
&"info threads\n"
~"  Id   Target Id         Frame \n"
~"  1    Thread 0x7ffff7fc5740 (LWP 12348) \"user-selected-c\" main () at /home/emaisin/src/binutils-gdb/gdb/testsuite/gdb.mi/user-selected-context-sync.c:56\n"
~"* 2    Thread 0x7ffff77f6700 (LWP 12387) \"user-selected-c\" 0x00007ffff7bc923c in pthread_barrier_wait () from /lib/x86_64-linux-gnu/libpthread.so.0\n"
^done


The current plan is to fix this by decoupling the user-selected thread from the inferior_ptid variable, and to only use inferior_ptid as the internal current thread.
Comment 1 Sourceware Commits 2022-03-08 16:57:15 UTC
The master branch has been updated by Andrew Burgess <aburgess@sourceware.org>:

https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=a9c82bc13cf8dd5d9076e746f744ee711eb55507

commit a9c82bc13cf8dd5d9076e746f744ee711eb55507
Author: Jan Vrany <jan.vrany@labware.com>
Date:   Wed Mar 2 13:23:30 2022 +0000

    gdb/mi: preserve user selected thread and frame when invoking MI commands
    
    Fix for PR gdb/20684.  When invoking MI commands with --thread and/or
    --frame, the user selected thread and frame was not preserved:
    
      (gdb)
      info thread
      &"info thread\n"
      ~"  Id   Target Id                                           Frame \n"
      ~"* 1    Thread 0x7ffff7c30740 (LWP 19302) \"user-selected-c\" main () at /home/uuu/gdb/gdb/testsuite/gdb.mi/user-selected-context-sync.c:60\n"
      ~"  2    Thread 0x7ffff7c2f700 (LWP 19306) \"user-selected-c\" child_sub_function () at /home/uuu/gdb/gdb/testsuite/gdb.mi/user-selected-context-sync.c:30\n"
      ~"  3    Thread 0x7ffff742e700 (LWP 19307) \"user-selected-c\" child_sub_function () at /home/uuu/gdb/gdb/testsuite/gdb.mi/user-selected-context-sync.c:30\n"
      ^done
      (gdb)
      info frame
      &"info frame\n"
      ~"Stack level 0, frame at 0x7fffffffdf90:\n"
      ~" rip = 0x555555555207 in main (/home/uuu/gdb/gdb/testsuite/gdb.mi/user-selected-context-sync.c:60); saved rip = 0x7ffff7c5709b\n"
      ~" source language c.\n"
      ~" Arglist at 0x7fffffffdf80, args: \n"
      ~" Locals at 0x7fffffffdf80, Previous frame's sp is 0x7fffffffdf90\n"
      ~" Saved registers:\n "
      ~" rbp at 0x7fffffffdf80, rip at 0x7fffffffdf88\n"
      ^done
      (gdb)
      -stack-info-depth --thread 3
      ^done,depth="4"
      (gdb)
      info thread
      &"info thread\n"
      ~"  Id   Target Id                                           Frame \n"
      ~"  1    Thread 0x7ffff7c30740 (LWP 19302) \"user-selected-c\" main () at /home/uuu/gdb/gdb/testsuite/gdb.mi/user-selected-context-sync.c:60\n"
      ~"  2    Thread 0x7ffff7c2f700 (LWP 19306) \"user-selected-c\" child_sub_function () at /home/uuu/gdb/gdb/testsuite/gdb.mi/user-selected-context-sync.c:30\n"
      ~"* 3    Thread 0x7ffff742e700 (LWP 19307) \"user-selected-c\" child_sub_function () at /home/uuu/gdb/gdb/testsuite/gdb.mi/user-selected-context-sync.c:30\n"
      ^done
      (gdb)
      info frame
      &"info frame\n"
      ~"Stack level 0, frame at 0x7ffff742dee0:\n"
      ~" rip = 0x555555555169 in child_sub_function (/home/uuu/gdb/gdb/testsuite/gdb.mi/user-selected-context-sync.c:30); saved rip = 0x555555555188\n"
      ~" called by frame at 0x7ffff742df00\n"
      ~" source language c.\n"
      ~" Arglist at 0x7ffff742ded0, args: \n"
      ~" Locals at 0x7ffff742ded0, Previous frame's sp is 0x7ffff742dee0\n"
      ~" Saved registers:\n "
      ~" rbp at 0x7ffff742ded0, rip at 0x7ffff742ded8\n"
      ^done
      (gdb)
    
    This caused problems for frontends that provide access to CLI because UI
    may silently change the context for CLI commands (as demonstrated above).
    
    This commit fixes the problem by restoring thread and frame in
    mi_cmd_execute (). With this change, there are only two GDB/MI commands
    that can change user selected context: -thread-select and -stack-select-frame.
    This allows us to remove all and rather complicated logic of notifying
    about user selected context change from mi_execute_command (), leaving it
    to these two commands themselves to notify.
    
    Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=20684
Comment 2 Eli Zaretskii 2022-03-27 09:49:47 UTC
Created attachment 14038 [details]
More thorough patch to fix the gdb-mi problem as well
Comment 3 Hannes Domani 2024-01-13 23:22:03 UTC
*** Bug 19087 has been marked as a duplicate of this bug. ***
Comment 4 Hannes Domani 2024-01-13 23:26:51 UTC
(In reply to Sourceware Commits from comment #1)
> The master branch has been updated by Andrew Burgess
> <aburgess@sourceware.org>:
> 
> https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;
> h=a9c82bc13cf8dd5d9076e746f744ee711eb55507
> 
> commit a9c82bc13cf8dd5d9076e746f744ee711eb55507
> Author: Jan Vrany <jan.vrany@labware.com>
> Date:   Wed Mar 2 13:23:30 2022 +0000
> 
>     gdb/mi: preserve user selected thread and frame when invoking MI commands
>     
>     Fix for PR gdb/20684.  When invoking MI commands with --thread and/or
>     --frame, the user selected thread and frame was not preserved:

Fixed.