From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id 2NVDItNP52VLtjkAWB0awg (envelope-from ) for ; Tue, 05 Mar 2024 12:01:07 -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=gCpQ3KA4; dkim-atps=neutral Received: by simark.ca (Postfix, from userid 112) id 87C5C1E0D2; Tue, 5 Mar 2024 12:01:07 -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 743D81E092 for ; Tue, 5 Mar 2024 12:01:05 -0500 (EST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 27FD93858018 for ; Tue, 5 Mar 2024 17:01:05 +0000 (GMT) Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id 610F33858C54 for ; Tue, 5 Mar 2024 17:00:34 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 610F33858C54 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 610F33858C54 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1709658037; cv=none; b=YI98GcRRIA9CDK4aD9uiZVqusMO2cNQ39XrjoKxtZolgiLM0pwP21VmUgRi+c4pIEDACoeAkUfRcWDvtkWqW2OtOJIKmuf5+Ss+6fpsbdFpPUamN6v2mDKZ9SRrrMuhHS6Ac7NRAxjwCoT4e9tqJ5s1rrUUKmrS74B8BJI8P6N0= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1709658037; c=relaxed/simple; bh=jrQjt0KZMclBRfviJh6vdBIqgIZ9DsDinBIGGiJRjV8=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=Ax/cSgC9kebn54WxS6TZ4HDKMJAsPWyJBMN59pokSm3CT8TBIVbtxD/y97IhhG9kGno0lAGSnMthAlmAAXoxBLgOFK76GlmjuLI+RWpEmtKvntYoQMpZsCryagm35nxu4bcxqkXBmBwXJjdZEihKqjIVjwuqv6rK8IN7y0Na3to= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1709658034; 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=gCpQ3KA4GBEUXv3kxDqf+Xr+Y+m65B1vQtN4WkmKV5W9VDoWjnNxesCSIb1TtU+vQSweVR Vvem/5h2tZ/shWIDG/uqXbd4NAH4N7ELTSTRirTBsji9JiG0YgXY/ic+EecH9GG+/5ARHg NybYJqMlEJHA+SLan/pqtd4t9M/taA0= Received: from mail-ej1-f72.google.com (mail-ej1-f72.google.com [209.85.218.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-267-cI-DxcjCMOCJyWq6J9753Q-1; Tue, 05 Mar 2024 12:00:32 -0500 X-MC-Unique: cI-DxcjCMOCJyWq6J9753Q-1 Received: by mail-ej1-f72.google.com with SMTP id a640c23a62f3a-a45acc7f191so48095566b.0 for ; Tue, 05 Mar 2024 09:00:32 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709658030; x=1710262830; 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=a/sKYXTqhcFQI9xNMhQRHHUeiXydp4eljt5Q/Z/caL9RDfo4AYN4cEy28A0tvtx3QX mcmLgdVOvQo/ezWV0o37mMNiM+XN5kIvQesU30Y8RHHdYvHwsdFEpOiuCkxeiYGVaNdV pz1yVm1Fhg1ePp4CNttnDBxnVehu7njqvizwBAXY+kcEPeg+pBjyUa7R11SHWoHhjizw LIoy5ac8dunGrR6BIASOCKZzCxbRK4qdsRTVOwqrf3qkS+WnNaaJnqG3jGv78iqVH5LZ y3sq2pO1L2VaAeX4OSyS1eJ5XbbpMC0QqGWClh9iPatGWg/tJA64X2dVuzQ1SACV5OpP 1jlA== X-Gm-Message-State: AOJu0YxsR9wk9tuvoMdl7uDuXtGqPYy2+XUmlTQLWUntTC3yoLi6R7+h ll+4VmSdHjXQdO3nXryd9ssRY36UzQ+RRuB2cNXJCOqQiDh5asWm0EX/iuv9d6kixfMYPM1gE+y aa8lL2ReWIWjylEe3AsuNyuk8RWIPUIOLK8z5WHXQG0q1pbfb4jfNhjXj5k7/UA+bNUJaGLS4ql TvJFPHPE1zKnbBhFIZVaahYgCuop2IdnTTQ8JKoRh35LI= X-Received: by 2002:a17:906:4342:b0:a45:607c:7bca with SMTP id z2-20020a170906434200b00a45607c7bcamr3675379ejm.31.1709658030536; Tue, 05 Mar 2024 09:00:30 -0800 (PST) X-Google-Smtp-Source: AGHT+IECAohEIZqQngr8e5GU6VLbOLYkl4Zj9H366tZGXu4oMiMl6+k0vHFRBxB4UVKP1bZxKdJKcg== X-Received: by 2002:a17:906:4342:b0:a45:607c:7bca with SMTP id z2-20020a170906434200b00a45607c7bcamr3675367ejm.31.1709658030027; Tue, 05 Mar 2024 09:00:30 -0800 (PST) Received: from localhost (185.223.159.143.dyn.plus.net. [143.159.223.185]) by smtp.gmail.com with ESMTPSA id rp25-20020a170906d97900b00a3d665c6778sm6318746ejb.12.2024.03.05.09.00.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 05 Mar 2024 09:00:29 -0800 (PST) From: Andrew Burgess To: gdb-patches@sourceware.org Cc: Andrew Burgess Subject: [PATCHv2 2/7] gdb/x86: move reading of cs and ds state into gdb/nat directory Date: Tue, 5 Mar 2024 17:00:17 +0000 Message-Id: <56268fed55672ec3c13401c42d128a27504e99ed.1709657954.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=-12.3 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, 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