From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id sGMTBg7wD2YqYiQAWB0awg (envelope-from ) for ; Fri, 05 Apr 2024 08:35:26 -0400 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=GDNZuTYr; dkim-atps=neutral Received: by simark.ca (Postfix, from userid 112) id 0D4E31E0C0; Fri, 5 Apr 2024 08:35:26 -0400 (EDT) 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 EA13C1E030 for ; Fri, 5 Apr 2024 08:35:23 -0400 (EDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id AF0C83846077 for ; Fri, 5 Apr 2024 12:35:23 +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 C29E838460A2 for ; Fri, 5 Apr 2024 12:33:33 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org C29E838460A2 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 C29E838460A2 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=1712320419; cv=none; b=HaCMyDh9KJek8dPBnWesRRpXQ2bI2iX3+XMnCgB379UmtQSwqswgMloQkM/Vn9DvGZ/MTBj4useZwm8oEIQrufCbEt28iJ3SMzQUs58GgLc7iS6ld5ddmSJV2/UlgdFBUoUx8mTxsGycArp1YMXWBqW8oQ3pZ9wT75nB8BWIyKQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1712320419; c=relaxed/simple; bh=Zhn34iZOsz7xcfu/19nJ64JAzMn6e9bjMTByRm6Mh1w=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=hfRPLkrvcH606d/VXvbAqdXGPbiOKwwjGCcf3G9ORkD+s+xScR0SGEg1TY05O3aqicJuiMoaHSWTuMqctJjAQn1dHJ7mo4kRcLgnqc59nJcPUL4gUhS37qg/MYfxzp8UT9dLMqcPZJEBEtp3zW0ZNZEDhSkrB1au5SPmJiMySn4= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1712320413; 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=Blz9vexsOyLY8vjMSHQSA6p5ruUMa4DPzfneCO6yA1Q=; b=GDNZuTYr3MhwzWbVu8/jIpOmxN/fflYmwJJghUA7BgwrOkV+vl5AKSmxc7wBxF+7BLci6w 83+6yYk2/r5QOPCxjJObYsusidH8l0RXOYZ/4ggsociCWctvQVvbJSRk5CkcmCqsmcM/mN MyRTYux00fXOUoOlZm9W3J9fGBdKGUw= Received: from mail-ed1-f71.google.com (mail-ed1-f71.google.com [209.85.208.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-627-kty3eaEjN9Cwzkg9tB-nJQ-1; Fri, 05 Apr 2024 08:33:32 -0400 X-MC-Unique: kty3eaEjN9Cwzkg9tB-nJQ-1 Received: by mail-ed1-f71.google.com with SMTP id 4fb4d7f45d1cf-56c3dc8d331so1486129a12.2 for ; Fri, 05 Apr 2024 05:33:32 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712320410; x=1712925210; 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=Blz9vexsOyLY8vjMSHQSA6p5ruUMa4DPzfneCO6yA1Q=; b=Suhbg91E11+oSTQ6nCEcMOjb+CpM0wNWVUVmrmjlI5MsB7REiJwFMUKHdZWUEarb1Y /PqP/+Jg5M+kBmmSq+LhPQA5MMyF86EV1TlnTKCn+01nNew344EFnSObpz4f7+VcMVtn LZ/Qi/JVyw2s1bo7R7YJXLDeAPY94jEquUXSlC8OSr3SuvKUQbexfa2cY2WMFnzR85iL QfrSbQo7BDM+bH8BH9ILnJMIHqOd4Zq6mxfuKvdXupOH6goBRWne9lOAeIefz6QNCp5a h/MrXosExqeaJtwrY9DY48kb/oTSyIFrhvGoMJqQVsPapUrxFZKDGi5thtxybZPQo1ib JVpg== X-Gm-Message-State: AOJu0YzHXQi+zRhaj3ydwtm9X0HpUzgnea22g326qnsHoaPCqLDUemG4 Lrz+UQJeyCeX4tMDQNjlB4xAjq45zUcJ/dB/suTv1Z4feG4WpOGCEqXxtkG3KhylKGEl7mC0WT3 NskdkLQu+FG63tQmFBPWTV6W3mKKmfqJaBzDz7O+FQydGhvMIE1mYGmk8TXH1grxYlcEYVIFYtp zs2cByfQYSpKxllUJagPVIMIwQcev0P3+iJtxhvlkW4v0= X-Received: by 2002:a17:907:9611:b0:a4e:2189:d06b with SMTP id gb17-20020a170907961100b00a4e2189d06bmr1173912ejc.60.1712320409665; Fri, 05 Apr 2024 05:33:29 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHXl3LaVIhLUrlWKhInCHwg7sYDfkDBoJEVF49s+Ys98a0CG7w1mcSCOviUVEW/1x6boI0Eqg== X-Received: by 2002:a17:907:9611:b0:a4e:2189:d06b with SMTP id gb17-20020a170907961100b00a4e2189d06bmr1173886ejc.60.1712320409097; Fri, 05 Apr 2024 05:33:29 -0700 (PDT) Received: from localhost (185.223.159.143.dyn.plus.net. [143.159.223.185]) by smtp.gmail.com with ESMTPSA id ks1-20020a170906f84100b00a5188484b53sm788940ejb.219.2024.04.05.05.33.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 05 Apr 2024 05:33:28 -0700 (PDT) From: Andrew Burgess To: gdb-patches@sourceware.org Cc: Andrew Burgess , John Baldwin Subject: [PATCHv4 03/10] gdb/x86: move reading of cs and ds state into gdb/nat directory Date: Fri, 5 Apr 2024 13:33:13 +0100 Message-Id: <7745bd8c51f75b0b876f86a152ed866e90f2c301.1712245988.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=-11.9 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_H4, RCVD_IN_MSPIKE_WL, 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 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 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 --- 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 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 a3d8ffb60f1..f91db492d05 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,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