From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id 4500D387087B for ; Sat, 11 May 2024 10:09:07 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 4500D387087B Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 4500D387087B Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1715422149; cv=none; b=iL3w2rXYmo5fHZTT3hZHS2eKvt6ozwwEF4Qbo/CdFtkQzy7TeAo47ZAim9Ezfk6Dix6xHt932HOVSQr8f4Jpe0EEzLi4qmTUKAWqwtDu/RXXN9O6dDH0e84nHGYW9aJiIWm0/EhGRgXcI9YRvuHWC7SMIAS18JmlzwIFQZTAFZM= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1715422149; c=relaxed/simple; bh=Eg0e/D8jjSQd4227yIz/AZKcBTAwWsvE6grpgUf10Jo=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=lk0/huQ9cdaxjFWBrlaTWj34FdliutrTlUOaT1Te0UIq3J/9CTMTfyBuIvIi6XTNTQOO6cRpNbPFhJLeZ8Z3BeHYn8bgpkqfPadMaW25AqjQZrp9u0rd2+0/ZNM6myLQ19zTkbX4TwibRcg2olqmORd6NeLUezpV8lGUhCL+dLU= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1715422146; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=mYy9GjBU46G0QTv/thLVyVIg4b0pasrhjC6daDx7Uxo=; b=WoKRWIVObhZp6g6FmQ6kbvEUuWIycD2ylMh39TUyLNrk1w20O2LSj/eNEgm8f2kNQgSf26 C7RCMA8LcSposMqB2gNMUWr5xtrMwe3n0gdM6nBZpM/8cbv63EhI0D2COr9jH7J9PI2bjA 7bKIL8hpEg3Bilcd+lMNsLTJnPzuF8k= Received: from mail-wr1-f72.google.com (mail-wr1-f72.google.com [209.85.221.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-320-gHsjmnCpMl-cEd_7s1zuFw-1; Sat, 11 May 2024 06:09:00 -0400 X-MC-Unique: gHsjmnCpMl-cEd_7s1zuFw-1 Received: by mail-wr1-f72.google.com with SMTP id ffacd0b85a97d-34deefe9142so1574289f8f.0 for ; Sat, 11 May 2024 03:09:00 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715422139; x=1716026939; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=mYy9GjBU46G0QTv/thLVyVIg4b0pasrhjC6daDx7Uxo=; b=EmBtCA8CFmZDPRdbDPDHklqcQ9kwIHsBeEn8vY1Nzo8v6IvM7SU01jzGjh6WnfbzYU M8qQM7ta4WoZXGab+V04FZM0UimtzDRRihNs+Eu8rEchN8NUeq/bDAn3//kKPzV2EQHc 3wa9ggbdprtwj6tEVl5M4Sl1hajHWxiILMswsjcWIwe1adhsGyZY1AAxqeeLrswxkYax J+DvWAtkXUn+kRFM9FYFW34llVZ6byVo4YS33kPY2aikwyRdrnDfQtwaEJSbyYz9ZFhV f8AifsYym+7alq8P+AKOD4ZI5ksoE0QyPybMfYUcg64Bgx0D7rFzjZZDpn/387cldbo8 US4A== X-Gm-Message-State: AOJu0YyGhG7G+wYYDopDmrtcU1SHg/PKMxwLnRBT//WBFGh4APSX14VX qicxNhUcjInsNPAostyoVfgCSelLkBRbnmeaK1AhmqWAoTd4VlrmhJAC1YTg8CthRphQuUcRUTS Ud7OXB6Yhal6tXCKEifJnhSg7uhNN5VsZso977wNEGVECgPnvSYYKZP+8JNAQUIqX91zn9wxzv5 sHtk23FER/jVyjRg0Thsnx3LhJ/EIChFnssDL8mvXZ9zs= X-Received: by 2002:a5d:40c1:0:b0:34d:707c:a281 with SMTP id ffacd0b85a97d-3504a62fb28mr4161453f8f.6.1715422138732; Sat, 11 May 2024 03:08:58 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHG4TWVB7WPc0DfK3IAHB0oKbvQ3AvUE6ywA4/V0Zn8VzfEcV8K3L+Rrqj96HdsMRFxcV9CHA== X-Received: by 2002:a5d:40c1:0:b0:34d:707c:a281 with SMTP id ffacd0b85a97d-3504a62fb28mr4161436f8f.6.1715422138242; Sat, 11 May 2024 03:08:58 -0700 (PDT) Received: from localhost (92.40.185.101.threembb.co.uk. [92.40.185.101]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-3502b8a77easm6584152f8f.53.2024.05.11.03.08.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 11 May 2024 03:08:57 -0700 (PDT) From: Andrew Burgess To: gdb-patches@sourceware.org Cc: Andrew Burgess , felix.willgerodt@intel.com, jhb@FreeBSD.org Subject: [PATCHv7 5/9] gdb/x86: move reading of cs and ds state into gdb/nat directory Date: Sat, 11 May 2024 11:08:35 +0100 Message-Id: <2dade5f6a5663b6b141a60ca5b7cb90da62338e4.1715421687.git.aburgess@redhat.com> X-Mailer: git-send-email 2.25.4 In-Reply-To: References: MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset="US-ASCII"; x-default=true X-Spam-Status: No, score=-8.6 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,RCVD_IN_ABUSEAT,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H4,RCVD_IN_MSPIKE_WL,RCVD_IN_SBL_CSS,SPF_HELO_NONE,SPF_NONE,TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: This patch is part of a series that has the aim sharing the x86 Linux target description creation code between GDB and gdbserver. Within GDB part of this process involves reading the cs and ds state from the 'struct user_regs_struct' using a ptrace call. This isn't done by gdbserver, which is part of the motivation for this whole series; the approach gdbserver takes is inferior to the approach GDB takes (gdbserver relies on reading the file being debugged, and extracting similar information from the file headers). This commit moves the reading of cs and ds, which is used to figure out if a thread is 32-bit or 64-bit (or in x32 mode), into the gdb/nat directory so that the code can be shared with gdbserver, but at this point I'm not actually using the code in gdbserver, that will come later. As such there should be no user visible changes after this commit, GDB continues to do things as it did before (reading cs/ds), while gdbserver continues to use its own approach (which doesn't require reading cs/ds). Approved-By: John Baldwin Reviewed-By: Felix Willgerodt --- gdb/nat/x86-linux.c | 47 +++++++++++++++++++++++++++++++++++++++++++++ gdb/nat/x86-linux.h | 28 +++++++++++++++++++++++++++ gdb/x86-linux-nat.c | 45 ++++++++----------------------------------- 3 files changed, 83 insertions(+), 37 deletions(-) diff --git a/gdb/nat/x86-linux.c b/gdb/nat/x86-linux.c index 7a21c8f2c26..ad3ed3c2289 100644 --- a/gdb/nat/x86-linux.c +++ b/gdb/nat/x86-linux.c @@ -19,6 +19,8 @@ #include "x86-linux.h" #include "x86-linux-dregs.h" +#include "nat/gdb_ptrace.h" +#include /* Per-thread arch-specific data we want to keep. */ @@ -79,3 +81,48 @@ x86_linux_prepare_to_resume (struct lwp_info *lwp) { x86_linux_update_debug_registers (lwp); } + +#ifdef __x86_64__ +/* Value of CS segment register: + 64bit process: 0x33 + 32bit process: 0x23 */ +#define AMD64_LINUX_USER64_CS 0x33 + +/* Value of DS segment register: + LP64 process: 0x0 + X32 process: 0x2b */ +#define AMD64_LINUX_X32_DS 0x2b +#endif + +/* See nat/x86-linux.h. */ + +x86_linux_arch_size +x86_linux_ptrace_get_arch_size (int tid) +{ +#ifdef __x86_64__ + unsigned long cs; + unsigned long ds; + + /* Get CS register. */ + errno = 0; + cs = ptrace (PTRACE_PEEKUSER, tid, + offsetof (struct user_regs_struct, cs), 0); + if (errno != 0) + perror_with_name (_("Couldn't get CS register")); + + bool is_64bit = cs == AMD64_LINUX_USER64_CS; + + /* Get DS register. */ + errno = 0; + ds = ptrace (PTRACE_PEEKUSER, tid, + offsetof (struct user_regs_struct, ds), 0); + if (errno != 0) + perror_with_name (_("Couldn't get DS register")); + + bool is_x32 = ds == AMD64_LINUX_X32_DS; + + return x86_linux_arch_size (is_64bit, is_x32); +#else + return x86_linux_arch_size (false, false); +#endif +} diff --git a/gdb/nat/x86-linux.h b/gdb/nat/x86-linux.h index 822882173f9..15153ea277e 100644 --- a/gdb/nat/x86-linux.h +++ b/gdb/nat/x86-linux.h @@ -47,4 +47,32 @@ extern void x86_linux_delete_thread (struct arch_lwp_info *arch_lwp); extern void x86_linux_prepare_to_resume (struct lwp_info *lwp); +/* Return value from x86_linux_ptrace_get_arch_size function. Indicates if + a thread is 32-bit, 64-bit, or x32. */ + +struct x86_linux_arch_size +{ + explicit x86_linux_arch_size (bool is_64bit, bool is_x32) + : m_is_64bit (is_64bit), + m_is_x32 (is_x32) + { + /* Nothing. */ + } + + bool is_64bit () const + { return m_is_64bit; } + + bool is_x32 () const + { return m_is_x32; } + +private: + bool m_is_64bit = false; + bool m_is_x32 = false; +}; + +/* Use ptrace calls to figure out if thread TID is 32-bit, 64-bit, or + 64-bit running in x32 mode. */ + +extern x86_linux_arch_size x86_linux_ptrace_get_arch_size (int tid); + #endif /* NAT_X86_LINUX_H */ diff --git a/gdb/x86-linux-nat.c b/gdb/x86-linux-nat.c index 762151ff378..ed76c1760bc 100644 --- a/gdb/x86-linux-nat.c +++ b/gdb/x86-linux-nat.c @@ -90,18 +90,6 @@ x86_linux_nat_target::post_startup_inferior (ptid_t ptid) linux_nat_target::post_startup_inferior (ptid); } -#ifdef __x86_64__ -/* Value of CS segment register: - 64bit process: 0x33 - 32bit process: 0x23 */ -#define AMD64_LINUX_USER64_CS 0x33 - -/* Value of DS segment register: - LP64 process: 0x0 - X32 process: 0x2b */ -#define AMD64_LINUX_X32_DS 0x2b -#endif - /* Get Linux/x86 target description from running target. */ const struct target_desc * @@ -121,31 +109,14 @@ x86_linux_nat_target::read_description () tid = inferior_ptid.pid (); #ifdef __x86_64__ - { - unsigned long cs; - unsigned long ds; - - /* Get CS register. */ - errno = 0; - cs = ptrace (PTRACE_PEEKUSER, tid, - offsetof (struct user_regs_struct, cs), 0); - if (errno != 0) - perror_with_name (_("Couldn't get CS register")); - - is_64bit = cs == AMD64_LINUX_USER64_CS; - - /* Get DS register. */ - errno = 0; - ds = ptrace (PTRACE_PEEKUSER, tid, - offsetof (struct user_regs_struct, ds), 0); - if (errno != 0) - perror_with_name (_("Couldn't get DS register")); - - is_x32 = ds == AMD64_LINUX_X32_DS; - - if (sizeof (void *) == 4 && is_64bit && !is_x32) - error (_("Can't debug 64-bit process with 32-bit GDB")); - } + + x86_linux_arch_size arch_size = x86_linux_ptrace_get_arch_size (tid); + is_64bit = arch_size.is_64bit (); + is_x32 = arch_size.is_x32 (); + + if (sizeof (void *) == 4 && is_64bit && !is_x32) + error (_("Can't debug 64-bit process with 32-bit GDB")); + #elif HAVE_PTRACE_GETFPXREGS if (have_ptrace_getfpxregs == TRIBOOL_UNKNOWN) { -- 2.25.4