From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id gKs4BTK5u2XcfA8AWB0awg (envelope-from ) for ; Thu, 01 Feb 2024 10:30:58 -0500 Authentication-Results: simark.ca; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=WiQqbUXj; dkim-atps=neutral Received: by simark.ca (Postfix, from userid 112) id 0F01E1E0C3; Thu, 1 Feb 2024 10:30:58 -0500 (EST) Received: from server2.sourceware.org (server2.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by simark.ca (Postfix) with ESMTPS id E81EC1E092 for ; Thu, 1 Feb 2024 10:30:55 -0500 (EST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id A8CF9385AE5D for ; Thu, 1 Feb 2024 15:30:54 +0000 (GMT) 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 A0D623857B88 for ; Thu, 1 Feb 2024 15:28:44 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org A0D623857B88 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 A0D623857B88 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=1706801330; cv=none; b=IIIjBfJXEMhqE+csQglEpKKQrtdfx/4sHJhBdlLIkxskEhtE5Ix/EL5hSNPtxxOc1YqMW5pttJ4ZjdQK1NnvRMl8nNEA+eI32kP8WWRwFslddK/NiKjRwfSvdeZWI1oSKR8P5nSiPJFXbB8niCquj0AZgV+ghO7pWKYMTSf4nq0= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1706801330; c=relaxed/simple; bh=jrQjt0KZMclBRfviJh6vdBIqgIZ9DsDinBIGGiJRjV8=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=FpnnQFNFfG/n9YEx8CzQdI6SHdPI47pAWiD70JuM9EQKWiQBBUEMsX1EkpIQ7VMRq5OCmNWizlWNq6tfAI40qkb6d9U4HdGukkjKSQpGJfykiBWufZvSn48qdMmyWHb42esmp24yVbQBWFLH4ET6QRxfe0rph/l9rr7qYntqk5Q= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1706801324; 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=n3E/5/Dp/ldR+3ZldUUOQH7Gh19Z9aeEnWLVM19ESq8=; b=WiQqbUXjVLbcNoUYxH1RpRLEPbw2PbaZIc5LN5Te4Goqm21ErRxQl8wXeKMnOwyReTwWJB +Oreo+vSV2pYMWZzHslbX85Rc+GoEJFKNzuBxggM8sRAnIAC8d2Mq0t3wc8EWiosEoE+IA yQ4iMUOBZ/+YxcryEWrrg5tbIG7ZJvI= Received: from mail-wm1-f71.google.com (mail-wm1-f71.google.com [209.85.128.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-76-InShjbNYMVO5mlhqbSqpUA-1; Thu, 01 Feb 2024 10:28:43 -0500 X-MC-Unique: InShjbNYMVO5mlhqbSqpUA-1 Received: by mail-wm1-f71.google.com with SMTP id 5b1f17b1804b1-40f01cf71fdso5518945e9.1 for ; Thu, 01 Feb 2024 07:28:42 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706801321; x=1707406121; 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=n3E/5/Dp/ldR+3ZldUUOQH7Gh19Z9aeEnWLVM19ESq8=; b=Tidi+9IFMa8AYq0vOIX/FKJzPG87Q1ErcJxYwxIoIW1SkqFmyI2uh6DGdzXvZn187D SiqOnfAjhG2qeR3LiTyS4b7SqR4CXr5byA96ob0zVJDD7f63d33lSqcEOMH3MYKXb9pJ 317ojB1B7RJqHYBnxG8Cc0Uxv3ftR2luuI3/WoUnPfEDZItqUmkJufWGosXEF2+mP0DU YR/Y8EWRdvQqXwjnvDGKqlspxy9Z6vmjHX8xVsFiXN9AMyf7awfgGaxRzIMtJyBPhoh7 icu/88nFsjxb7aSNQmE6RGSaUxwslczFLhTDPVImkt7H0hF/aMihzlDzWVhMk/vwMfWY t6jQ== X-Gm-Message-State: AOJu0YzCj4WHXDnnj7NeZaVWqiy3mj0E8GYXdwUFKyaIO6SG8aOXf6MR vEpni3EqjxDW4lUmJNhtef/WCwK9t4bewxnW8+iQjnFCuYVByT3JjSUgsTWJwl+EMpuFC0IJEcZ iNS76uDBtWG2YqaTE5FMlF6D8Ij6wYIX7BmiKRnQyjoddECvsCNH9w1SSd3t0KtvIZPpFlxgi0V lW091wgkJB48I4gmcp8JuA4jDKakKJs7r7j554eoqHjkU= X-Received: by 2002:adf:fa91:0:b0:33b:136b:557a with SMTP id h17-20020adffa91000000b0033b136b557amr1786512wrr.70.1706801320765; Thu, 01 Feb 2024 07:28:40 -0800 (PST) X-Google-Smtp-Source: AGHT+IES7a5JnEAwqPsmfpJpW7fJH7O4sNa4IFus3DoxCpBqabSoYIMNQcCJGy3uKeBnD590WE2iyg== X-Received: by 2002:adf:fa91:0:b0:33b:136b:557a with SMTP id h17-20020adffa91000000b0033b136b557amr1786499wrr.70.1706801320350; Thu, 01 Feb 2024 07:28:40 -0800 (PST) Received: from localhost (185.223.159.143.dyn.plus.net. [143.159.223.185]) by smtp.gmail.com with ESMTPSA id m41-20020a05600c3b2900b0040fb8e7b348sm2611002wms.40.2024.02.01.07.28.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 01 Feb 2024 07:28:38 -0800 (PST) From: Andrew Burgess To: gdb-patches@sourceware.org Cc: Andrew Burgess Subject: [PATCH 2/7] gdb/x86: move reading of cs and ds state into gdb/nat directory Date: Thu, 1 Feb 2024 15:28:29 +0000 Message-Id: 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=-12.7 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H5, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gdb-patches-bounces+public-inbox=simark.ca@sourceware.org This patch is part of a series that has the aim of making the code that, for x86, reads the target description for a native process shared 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. 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 could 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). --- gdb/nat/x86-linux.c | 47 +++++++++++++++++++++++++++++++++++++++++++++ gdb/nat/x86-linux.h | 28 +++++++++++++++++++++++++++ gdb/x86-linux-nat.c | 42 +++++----------------------------------- 3 files changed, 80 insertions(+), 37 deletions(-) diff --git a/gdb/nat/x86-linux.c b/gdb/nat/x86-linux.c index e61f4d749ba..4242a1baafb 100644 --- a/gdb/nat/x86-linux.c +++ b/gdb/nat/x86-linux.c @@ -20,6 +20,8 @@ #include "gdbsupport/common-defs.h" #include "x86-linux.h" #include "x86-linux-dregs.h" +#include "nat/gdb_ptrace.h" +#include /* Per-thread arch-specific data we want to keep. */ @@ -80,3 +82,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 b93ffca38db..b39d05c401f 100644 --- a/gdb/x86-linux-nat.c +++ b/gdb/x86-linux-nat.c @@ -91,18 +91,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 * @@ -122,31 +110,11 @@ 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 (); + #elif HAVE_PTRACE_GETFPXREGS if (have_ptrace_getfpxregs == -1) { -- 2.25.4