From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id BB7D2385781C; Sun, 23 May 2021 08:08:50 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org BB7D2385781C From: "cvs-commit at gcc dot gnu.org" To: gdb-prs@sourceware.org Subject: [Bug tdep/27822] [gdb/tdep, x86_64] Wrong thread picked to select process 64/32-bitness Date: Sun, 23 May 2021 08:08:50 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: changed X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gdb X-Bugzilla-Component: tdep X-Bugzilla-Version: HEAD X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: cvs-commit at gcc dot gnu.org X-Bugzilla-Status: NEW X-Bugzilla-Resolution: X-Bugzilla-Priority: P2 X-Bugzilla-Assigned-To: unassigned at sourceware dot org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: Message-ID: In-Reply-To: References: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Bugzilla-URL: http://sourceware.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 X-BeenThere: gdb-prs@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gdb-prs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 23 May 2021 08:08:50 -0000 https://sourceware.org/bugzilla/show_bug.cgi?id=3D27822 --- Comment #3 from cvs-commit at gcc dot gnu.org --- The master branch has been updated by Tom de Vries : https://sourceware.org/git/gitweb.cgi?p=3Dbinutils-gdb.git;h=3Dfbf3c4b97907= cb198168f58e7a22d497868e5926 commit fbf3c4b97907cb198168f58e7a22d497868e5926 Author: Tom de Vries Date: Sun May 23 10:08:45 2021 +0200 [gdb/tdep] Use pid to choose process 64/32-bitness In a linux kernel mailing list discussion, it was mentioned that "gdb h= as this odd thing where it takes the 64-bit vs 32-bit data for the whole process from one thread, and picks the worst possible thread to do it (ie explicitly not even the main thread, ...)" [1]. The picking of the thread is done here in x86_linux_nat_target::read_description: ... /* GNU/Linux LWP ID's are process ID's. */ tid =3D inferior_ptid.lwp (); if (tid =3D=3D 0) tid =3D inferior_ptid.pid (); /* Not a threaded program. */ ... To understand what this code does, let's investigate a scenario in which inferior_ptid.lwp () !=3D inferior_ptid.pid (). Say we start exec jit-attach-pie, identified with pid x. The main thre= ad starts another thread that sleeps, and then the main thread waits for t= he sleeping thread. So we have two threads, identified with LWP IDs x and x+1: ... PID LWP CMD x x ./jit-attach-pie x x+1 ./jit-attach-pie ... [ The thread with LWP x is known as the thread group leader. ] When attaching to this exec using the pid, gdb does a stop_all_threads which iterates over all the threads, first LWP x, and then LWP x+1. So the state we arrive with at x86_linux_nat_target::read_description i= s: ... (gdb) p inferior_ptid $1 =3D {m_pid =3D x, m_lwp =3D x+1, m_tid =3D 0} ... and consequently we probe 64/32-bitness from thread LWP x+1. [ Note that this is different from when gdb doesn't attach but instead launches the exec itself, in which case there's just one thread to begin with, and consequently the probed thread is LWP x. ] According to aforementioned remark, a better choice would have been the main thread, that is, LWP x. This patch implement that choice, by simply doing: ... tid =3D inferior_ptid.pid (); ... The fact that gdb makes a per-process permanent choice for 64/32-bitnes= s is a problem in itself: each thread can be in either 64 or 32 bit mode, and change forth and back. That is a problem that this patch doesn't fix. Now finally: why does this matter in the context of the linux kernel discussion? The discussion was related to a patch that exposed io_uring threads to user-space. This made it possible that one of those threads would be picked out to select 64/32-bitness. Given that such threads are atypical user-space threads in the sense that they don't return to user-space and don't have a userspace register state, reading their registers returns garbag= e, and so it could f.i. occur that in a 64-bit process with all normal user-sp= ace threads in 64-bit mode, the probing would return 32-bit. It may be that this is worked-around on the kernel side by providing userspace register state in those threads such that current gdb is happy.=20 Nevertheless, it seems prudent to fix this on the gdb size as well. Tested on x86_64-linux. [1] https://lore.kernel.org/io-uring/CAHk-=3Dwh0KoEZXPYMGkfkeVEerSCEF1AiCZSvz9T= Rrx=3DKj74D+Q@mail.gmail.com/ gdb/ChangeLog: 2021-05-23 Tom de Vries PR tdep/27822 * target.h (struct target_ops): Mention target_thread_architect= ure in read_description comment. * x86-linux-nat.c (x86_linux_nat_target::read_description): Use pid to determine if process is 64-bit or 32-bit. * aarch64-linux-nat.c (aarch64_linux_nat_target::read_descripti= on): Same. * ppc-linux-nat.c (ppc_linux_nat_target::read_description): Sam= e. * riscv-linux-nat.c (riscv_linux_nat_target::read_description): Same. * s390-linux-nat.c (s390_linux_nat_target::read_description): S= ame. * arm-linux-nat.c (arm_linux_nat_target::read_description): Sam= e. Likewise, use pid to determine if kernel supports reading VFP registers. --=20 You are receiving this mail because: You are on the CC list for the bug.=