From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: 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 D85E4387088C for ; Wed, 8 May 2024 16:46:56 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org D85E4387088C 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 D85E4387088C 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=1715186821; cv=none; b=jy5yI1PjWBRrH8jwvWyxK4w0tZ4Mp0ang6V+0aixuTvoNQ3ydUQ0mpXm4PPS1uFH4pqatjz6eehpWsm9E32aqb/L+lLSsMozKiwpPa1MJ2dAtclfCG0wBWpbvSoQkGhMPBJmCcZnM7TMJYdjl1sksGwjrEIGYRv46eCYDXg6YEo= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1715186821; c=relaxed/simple; bh=aekI+ZyY0Iih3Hruqd8K01lpSO1nJiy9lcezDkSRm3o=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=RMdp6mTXAkM2ISpY1+Fco4xq7Ifay4TJBgF2UyxZPvstMal8NY3Uj2JubopDZuP8nd0Nf405s9fHsWZQ8zqxJQ7wTyTGQgnWOoJ78v9wNhDH2BJOzwNdRM6uUbWdlWbHpJrodAaaETRt/jyPJplHGmAmxkBHvDcvf/2upjTHKk8= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1715186816; 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=4+R3nbdzCnPabaBzt5qri338sh3vC6nAjMFAbER7sZo=; b=da96BdFv5IBMjqPOP8nb7qzRl7fLxk50it8wNJ2MJuO4hZ/TrSAlxrMea2EtHh054l/Ijf VzsaChZFYOrf5kk1S/zoYR2cQlanlKpgB7Bzj6ysI6CK2LA9fD2YZD+2MGhYQ7iilfS75O nKVdSw0SdxJlBqnY//V43Q7SKzbBi6Y= Received: from mail-wr1-f71.google.com (mail-wr1-f71.google.com [209.85.221.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-501-ToIhlZKMMyGzSpioPjr6Gw-1; Wed, 08 May 2024 12:46:55 -0400 X-MC-Unique: ToIhlZKMMyGzSpioPjr6Gw-1 Received: by mail-wr1-f71.google.com with SMTP id ffacd0b85a97d-34dc66313b3so3012836f8f.2 for ; Wed, 08 May 2024 09:46:55 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715186814; x=1715791614; 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=4+R3nbdzCnPabaBzt5qri338sh3vC6nAjMFAbER7sZo=; b=bpgPQ6mZrGFFRpP8TPFkINFIR0AA53trUB3hYVYLnVuiVlWuHGKjBjQo7NbeZB1+SX TTV8F1TEB1t/OgNdC2HkGKKfx0PoFv2nZJStc/5FSAm8ngN76jpDn78NzUo41XBSaaL1 s/2HX/2O3AvOpp4kgD+f4YbP0sbmYjT7CyCa77mRH+2Tj1WLtsz/YMhurPjaZIhpIrGJ +L4UzN0nT4DhH0+svsp8+nA909nHBWBYZkCSu4xAgk+9ztJPlJizqSwmlEYqw29UMrjK rXCOmiStsU4RCDFdu8GOEOuc+t3DeMBf2maRsAiznZEcMZgHXCOv2Ubpvqortq1ajH/i 7nsA== X-Gm-Message-State: AOJu0YwtgPwXggHnz/yMgO5E4js56FJUBySDPPYnCjoY0zL9G3mwzDFg XGbxlGNYCYhY8H4PwY7L2Q/P2rI/+6Ood+JfNGiJhMMKGQg1CrOIUn5XZVwC8gXum7jbhy7sJF/ ZwbRetRTrJUlfPTl/E6jVWzg5matU66OORlTKMjGxMeLyZMkmN/Pt3r5xwpgorFNkndsjpHaJvW ll+gCDnGvyL86VJFiYSNcUaM3BAQ+eU449nWAm5oB7Xu4= X-Received: by 2002:a5d:488b:0:b0:343:751e:8ef7 with SMTP id ffacd0b85a97d-34fcaa048b9mr2606229f8f.51.1715186813956; Wed, 08 May 2024 09:46:53 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEeckAlFycVqxKDckePBzZXtaVJrkNqLrckVAmZ3J7fbqR+sOKHrWVpsV9I5JMtGZ28G8NX/w== X-Received: by 2002:a5d:488b:0:b0:343:751e:8ef7 with SMTP id ffacd0b85a97d-34fcaa048b9mr2606208f8f.51.1715186813422; Wed, 08 May 2024 09:46:53 -0700 (PDT) Received: from localhost ([31.111.84.240]) by smtp.gmail.com with ESMTPSA id k7-20020a5d6287000000b0034f3f756293sm7118672wru.6.2024.05.08.09.46.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 08 May 2024 09:46:52 -0700 (PDT) From: Andrew Burgess To: gdb-patches@sourceware.org Cc: Andrew Burgess , felix.willgerodt@intel.com, jhb@FreeBSD.org Subject: [PATCHv6 5/9] gdb/x86: move reading of cs and ds state into gdb/nat directory Date: Wed, 8 May 2024 17:46:41 +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.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 List-Id: 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 Reviewed-By: Felix Willgerodt --- gdb/nat/x86-linux.c | 47 +++++++++++++++++++++++++++++++++++++++++++++ gdb/nat/x86-linux.h | 28 +++++++++++++++++++++++++++ gdb/x86-linux-nat.c | 45 ++++++++----------------------------------- 3 files changed, 83 insertions(+), 37 deletions(-) diff --git a/gdb/nat/x86-linux.c b/gdb/nat/x86-linux.c index ed1d06bd623..145684367d7 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 /* See nat/x86-linux.h. */ tribool have_ptrace_getfpxregs = @@ -91,3 +93,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 cb5f44f4b49..a76c5092f52 100644 --- a/gdb/nat/x86-linux.h +++ b/gdb/nat/x86-linux.h @@ -64,4 +64,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 b42884ad57f..285a4880103 100644 --- a/gdb/x86-linux-nat.c +++ b/gdb/x86-linux-nat.c @@ -87,18 +87,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 * @@ -118,31 +106,14 @@ 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 (); + + if (sizeof (void *) == 4 && is_64bit && !is_x32) + error (_("Can't debug 64-bit process with 32-bit GDB")); + #elif HAVE_PTRACE_GETFPXREGS if (have_ptrace_getfpxregs == TRIBOOL_UNKNOWN) { -- 2.25.4