From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id SHP9NxzCK2Zj5TwAWB0awg (envelope-from ) for ; Fri, 26 Apr 2024 11:02:52 -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=EtVRd2MH; dkim-atps=neutral Received: by simark.ca (Postfix, from userid 112) id DF5D81E0CE; Fri, 26 Apr 2024 11:02:52 -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 CC5B31E0C0 for ; Fri, 26 Apr 2024 11:02:50 -0400 (EDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 86E84384AB6B for ; Fri, 26 Apr 2024 15:02:50 +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 368373858401 for ; Fri, 26 Apr 2024 15:02:12 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 368373858401 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 368373858401 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=1714143733; cv=none; b=gKdfEgV3QYTKDbokCBBS1PyIsVFJ8ozK8c/1JtWXGE2eDUu3QBb90PCVu0NakPpEKQ3/StE4Z1O+hoi4V5yAwxWurwzYIQW5JPRGXUkmRIxiyc33ZE/bpOxJL7LSSYsWuP96oY1d7Rv093wuRxMNLb7p0mIiBFpC3uf2l3iJMeE= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1714143733; c=relaxed/simple; bh=Zhn34iZOsz7xcfu/19nJ64JAzMn6e9bjMTByRm6Mh1w=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=SB8dhnKtPxMq0hjYhVqkZwKqiPifYzOfZQcfKO2Gj1JSd/IZ60pzkXOQQIZkK7R5hzwM5gktsd5I8YlSkKYwlT5EVNDRieb4qQZMLf8VQK/zvqToRrm5hQojo2hrq8Lv0ghd9d/4QpH/joyq0gBykEhCZ1rZxHXV0cLBcUROrBU= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1714143731; 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=EtVRd2MHIprkEKjtL3SMbvQSlaw0hZiDTokhDEUz1T6s2KSuYMRVnaLko0XQdbUV/7pU5e VDyyauJ+JBd2TmIGBvIew8oeG5SbscOWIqlcgWGBm7TgMmpafDYzSLwWMyiDGY3lsW9Rrh d8eJ/x+8hwicJ2gxtHcp5p/2HUdeZvI= Received: from mail-wm1-f70.google.com (mail-wm1-f70.google.com [209.85.128.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-624-Mp8xC8FiM5mEVcCXEpfb1w-1; Fri, 26 Apr 2024 11:02:10 -0400 X-MC-Unique: Mp8xC8FiM5mEVcCXEpfb1w-1 Received: by mail-wm1-f70.google.com with SMTP id 5b1f17b1804b1-4167351545cso15696615e9.1 for ; Fri, 26 Apr 2024 08:02:10 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1714143728; x=1714748528; 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=OZyxq1gAB7vG4Jr/nv8OddHcz40+4h7RK97MNhalQok6Jy4NmOKcjOH04l4e3w9YLE v64BsDUCVp3LUkpa+oodzbezoGMmC++sAZwzet+vXDg+I6NNpuBcSTfo5Ai66sB/mtc5 8y66ShGZdD7G3d+un6jnWsq9wZRyLkhSoiePr1PsGDv9sYDJ2I/CTX0yrBJXnFhlaCV+ mXpknatxK9h/3/GxvCwQygHasrh0tXsX+/+E9v/Rs67e2rTCH3rHCsW2mFoYwvoQqLR4 ayCsseSz1zpXXWgf79aTQCBkHX0dipLd9//9nZK02hM1eZr7BD0svvR/Jl0/0fZzYs73 qeVg== X-Gm-Message-State: AOJu0YxgRj1ET/zJPeHQaIEaDKfTpHoQxvVG5dJ2DPh45iDpDN5eGJow leeGq4hDjUxm/adJNeK6ah1JS3k3V2cvG4EkxEdYnTZWaAGK1p9Gx1m/5gegg5ZchlP/uUt9q63 J41awxdssWc3tiWDiuu2QBZ1qonPHzarT0rMqgm52UEDNB5XW3g7nZ6c14/xSNkAvuauQqs2bQI JTu++tgX5Yt0DcYkCIcdfXChlpTwbTDeiBUxJSRY64x5s= X-Received: by 2002:a05:600c:1c9e:b0:41a:2c91:6352 with SMTP id k30-20020a05600c1c9e00b0041a2c916352mr2451810wms.36.1714143728311; Fri, 26 Apr 2024 08:02:08 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHtMTrJSmeOEMqzQUJrWYimBJzIwGZaaxEgCqwqMv+lZzCtCx/jD1oxaBsmcIkP64kJVOExlw== X-Received: by 2002:a05:600c:1c9e:b0:41a:2c91:6352 with SMTP id k30-20020a05600c1c9e00b0041a2c916352mr2451771wms.36.1714143727678; Fri, 26 Apr 2024 08:02:07 -0700 (PDT) Received: from localhost (185.223.159.143.dyn.plus.net. [143.159.223.185]) by smtp.gmail.com with ESMTPSA id k41-20020a05600c1ca900b00417e8be070csm31445985wms.9.2024.04.26.08.02.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 26 Apr 2024 08:02:06 -0700 (PDT) From: Andrew Burgess To: gdb-patches@sourceware.org Cc: Andrew Burgess , felix.willgerodt@intel.com, John Baldwin Subject: [PATCHv5 03/11] gdb/x86: move reading of cs and ds state into gdb/nat directory Date: Fri, 26 Apr 2024 16:01:47 +0100 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.2 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