From patchwork Sat Mar 23 16:35:20 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Burgess X-Patchwork-Id: 87573 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 74FB43858410 for ; Sat, 23 Mar 2024 16:37:28 +0000 (GMT) X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org 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 F1C203858C33 for ; Sat, 23 Mar 2024 16:35:39 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org F1C203858C33 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 F1C203858C33 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=1711211741; cv=none; b=WKWBNmedbo9iAHGWOMNSbSESyB6gE3eP+7XSWqFJvkZvE9HTy06/ETSv2tt71pWz+vNIqR1IxfncbzjBqFp1FCD1JMzdwM1EkKAg3akpNwFyf6gE3p+vbqkRxS6frra9ND+4fs6SQcqjwDBkZFImY8Vh3b9eJTEDDkgBMuSN+eU= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1711211741; c=relaxed/simple; bh=Ller8/aehxm4RpXqOfF53+zCGeQwfvrmtztk/3YVc7k=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=QxNV2g7o7gVvqmDQTbyUZGuWCj/jTc0FwADDe6px2+q5XS0dl47DtCuhVDei/qgVq4gO47Z0sZhk87srb6amsVmApm5j+igerDQTP2XVs/jQGxxXMivVllXb9Y+arvXljvjZ32EFlgasbqwrbEirfXYVAyOWPF95JS2gNDgBOWU= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1711211739; 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=XgTm4/kQ9kZ2CUD+KyJ3m1I5WeQCMdGM6y9DiUOPzFA=; b=bTurCBq9TjBU5QJvby53d/hVldObJKjrn7LKYwH6hjsvAl/djTemqE1HkdxSKTBfeOKB8y KejC2OUaNMKsiQIkzMPKfbJambGXQFsutuJ8r8Nku02v9TdipKOYIZuARDuqT1jw76dA8S 5fuMS1zJXTwv+Skximzrx92SIAWj/3w= Received: from mail-ej1-f71.google.com (mail-ej1-f71.google.com [209.85.218.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-164-YW3OzYtjP5WC0fH8IX8d_Q-1; Sat, 23 Mar 2024 12:35:37 -0400 X-MC-Unique: YW3OzYtjP5WC0fH8IX8d_Q-1 Received: by mail-ej1-f71.google.com with SMTP id a640c23a62f3a-a4715d8c761so172653666b.1 for ; Sat, 23 Mar 2024 09:35:37 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711211735; x=1711816535; 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=XgTm4/kQ9kZ2CUD+KyJ3m1I5WeQCMdGM6y9DiUOPzFA=; b=IUQiFEOD7s03PptJdnNflRuuOfk82wSPw0jvUPkR0Sk0tBD8sLKB4JOOljAMO3GBCR 5AS+Hzx+DGzIEQWbLwwhw9t0gDoORQAWfwtJt7AvPySkPrpIXaDg8koGB2ppmsUbZln+ PkoX4IpHycEN25k0MbsVAr34Aohh/tdzRdhTWAX18M/icEtIOBmXQ3yErnY+Ygfx+Ird kLA/fcFJznVfyjPu5xo+QmpM6/swnyW3H28Gse6oyT00gSnlmpeSSfUw+sFe/9J+87Qi wc7vLbkLkTtiKIj2kM+slbtazATsZ4hYsrHwvNnOQrPcUU65RnbW1vA2H1D8fo1h4C86 BOOQ== X-Gm-Message-State: AOJu0YwNkGnw6br263VHIYdYlSrWtXvWmscN7ArRN7KikOu0LXIYovHO wGaTGapLFOArn9KOa1LrQLLZv1pefP8Wf1zR17aL87fAyR9FWBz0F9WEoHuLQ2WFOscBMP89LdE iCd8tC1A+0ka0ByODETFduIOuSViYcVJltREgcDGrx3mXui9IkMQh5Q4MKR0dglUYOSETAlkSBl 3Zv+Z3ja7cCvQSfZQw+446wQ9bATK2qW/0TD2dGHwaC10= X-Received: by 2002:a05:6402:5215:b0:56b:ff5b:c49c with SMTP id s21-20020a056402521500b0056bff5bc49cmr620650edd.33.1711211734787; Sat, 23 Mar 2024 09:35:34 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHzCMuC0cSljpfaWbkstVrHVQhpse2+IhUPxlbpBrnM+fMOhlhOz/pWoAgFAlqsniT+HnHaVQ== X-Received: by 2002:a05:6402:5215:b0:56b:ff5b:c49c with SMTP id s21-20020a056402521500b0056bff5bc49cmr620628edd.33.1711211734207; Sat, 23 Mar 2024 09:35:34 -0700 (PDT) Received: from localhost (185.223.159.143.dyn.plus.net. [143.159.223.185]) by smtp.gmail.com with ESMTPSA id ef3-20020a05640228c300b0056bfb4a94e9sm602000edb.59.2024.03.23.09.35.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 23 Mar 2024 09:35:33 -0700 (PDT) From: Andrew Burgess To: gdb-patches@sourceware.org Cc: Andrew Burgess , hjl.tools@gmail.com, John Baldwin Subject: [PATCHv3 2/8] gdb/x86: move reading of cs and ds state into gdb/nat directory Date: Sat, 23 Mar 2024 16:35:20 +0000 Message-Id: <35cb0063d1d6ec15f1ecda637e69c2a7248dcb95.1711211528.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 X-Spam-Status: No, score=-12.1 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+patchwork=sourceware.org@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). 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 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) {