From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-wm1-f47.google.com (mail-wm1-f47.google.com [209.85.128.47]) by sourceware.org (Postfix) with ESMTPS id 3981A384641E for ; Tue, 7 May 2024 23:44:35 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 3981A384641E Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=palves.net Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 3981A384641E Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=209.85.128.47 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1715125478; cv=none; b=VGAi4ctvi4F5WwTM4s+ckfxuR+y0wbeGJfCsfEtJBawhOQkumXUK3jlzcHI/GnWQ0VDzunH5ayBAWovzwLiG/XjBJBPE6EpbpBcf0XfD33gNMTmXXWjzN5+Aq1M1eVdmgDm97zbrGMW4swFwz7DOqMs20tCKgjK9SonhpJ33b74= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1715125478; c=relaxed/simple; bh=pL91O/tEkoKdRvMZqsyFNBj/taUJF8T5huLAZ4Bo1aI=; h=From:To:Subject:Date:Message-ID:MIME-Version; b=e5xJLmbBARp55SaEm8K3HOFz/BsRfPyHICpGRm8VfLcuY/d5REJXB4o3E6FIfpnr9a5El3iv+2I5Cy7Dj8C5vpkwIebpHzrHDHnPl66+rnvJKVpuNVIA3eUPIdWh1pik+DotqM7aYyWFWeRNFvMGJTWmQoj5tg2G/3fvUfgQeeI= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-wm1-f47.google.com with SMTP id 5b1f17b1804b1-41b782405bbso26398365e9.1 for ; Tue, 07 May 2024 16:44:35 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715125474; x=1715730274; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=DlsgXGuTxYhmE5HVIEMp3wUPtBwrbLSS+s0eFPpfN9o=; b=gKZRdaZjEiunlhH3u5KGthLuUKcmzA+8LsGpWZhwTtVFsOhTCVx7QydzD1tOaMMYoX 72sWa8mKaLnk67KvcV1G2LD+XzWrvhjgKQ/G2Z4miGe0xePLIrSrqXGl94o+Vy2BDZkU bm9MbSnxKLLK4z+oGeiTdu9C0VDrhGhuUEf1gX1zTx8VPLzCcqHJ4xWUUY9uPeTH+6LL 0hGH0xtHwLxaoGRK6+2qwxfcl0f5WyHeejIkSg4+LuSDAMjmjxY2EtGd/ii+i2YqhAYu ewhbNEIOWvS+CeIedWKnIgY5vNoElI9ega8mIir0M19c8sZAxV7xP1fxnjaBvhh2PVBu DZJw== X-Gm-Message-State: AOJu0YxjuyvgvbPWJyp+6HsRBrcn5KyT1rX1YEkW8MLlpS2G9R2H5vGm cMLTXfP1sXQD8rCjFrDifo11b9UagIliKeEQAAYS3gVj47f+00ERW1XBWeyk X-Google-Smtp-Source: AGHT+IHu+SNZYHq5YzZADFAIYAUPJsgdXZ50CiaT7E2ozDF9yiwiKbSAwVjfP8IgLA4hd+ZTxY/qeQ== X-Received: by 2002:a05:600c:1c82:b0:418:17e9:c23f with SMTP id 5b1f17b1804b1-41f719d6099mr9679375e9.31.1715125473888; Tue, 07 May 2024 16:44:33 -0700 (PDT) Received: from localhost ([2001:8a0:f908:4900:2dd1:1a0d:2b75:dc42]) by smtp.gmail.com with UTF8SMTPSA id 5b1f17b1804b1-41f87b264absm1984645e9.10.2024.05.07.16.44.33 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 07 May 2024 16:44:33 -0700 (PDT) From: Pedro Alves To: gdb-patches@sourceware.org Subject: [PATCH 31/34] Windows gdb+gdbserver: Check whether DBG_REPLY_LATER is available Date: Wed, 8 May 2024 00:42:30 +0100 Message-ID: <20240507234233.371123-32-pedro@palves.net> X-Mailer: git-send-email 2.43.2 In-Reply-To: <20240507234233.371123-1-pedro@palves.net> References: <20240507234233.371123-1-pedro@palves.net> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-10.8 required=5.0 tests=BAYES_00,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM,GIT_PATCH_0,HEADER_FROM_DIFFERENT_DOMAINS,KAM_DMARC_STATUS,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS,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: Per , DBG_REPLY_LATER is "Supported in Windows 10, version 1507 or above, ..." On Linux, we check which ptrace options are supported by the running kernel by forking gdb and then the parent gdb debugging the child gdb with PTRACE_ME, and then trying to set the ptrace options. Doing something like that on Windows would be more complicated, because #1 - we can't just fork, we have to start some executable, and the only executable we know we can start, probably, is gdb itself. And that's a large program, so take time to be started. And then we'd have to implement a WaitForDebugEvent loop to start up the process, and then finally try ContinueDebugEvent(DBG_REPLY_LATER). It seems a lot simpler to just check the Windows version. Unlike on Linux, we don't have to worry about kernel feature backports. Windows has a number of functions you can use to check the OS version, like GetVersion/GetVersionEx, or the Version Helper functions like IsWindows10OrGreater, VerifyVersionInfo, etc., however, as explained by https://learn.microsoft.com/en-us/windows/win32/api/sysinfoapi/nf-sysinfoapi-getversionexa ... and other pages, "Applications not manifested for Windows 8.1 or Windows 10 will return the Windows 8 OS version value (6.2)." "Manifested" here means that the application is linked with an xml manifest as detailed at: https://learn.microsoft.com/en-us/windows/win32/sysinfo/targeting-your-application-at-windows-8-1 I have actually tried doing that with windres, but I wasn't able to make it work. However, I found out that we can find the Windows major/minor/build in the KUSER_SHARED_DATA structure, which defines the layout of a data area that the kernel places at a pre-set address for sharing with user-mode software: https://www.geoffchappell.com/studies/windows/km/ntoskrnl/structs/kuser_shared_data/index.htm The Windows major/minor/build version retrieved using that method bypasses the manifest stuff, it actually gets you the real OS version numbers. That is what this patch is using. Change-Id: Ia27b981aeecaeef430ec90cebc5b3abdce00449d --- gdb/nat/windows-nat.c | 60 +++++++++++++++++++++++++++++++++++++++++++ gdb/nat/windows-nat.h | 4 +++ 2 files changed, 64 insertions(+) diff --git a/gdb/nat/windows-nat.c b/gdb/nat/windows-nat.c index 4fd717e6521..99bdadd80e6 100644 --- a/gdb/nat/windows-nat.c +++ b/gdb/nat/windows-nat.c @@ -939,6 +939,66 @@ disable_randomization_available () && DeleteProcThreadAttributeList != nullptr); } +/* We can find the Windows major/minor/build in the KUSER_SHARED_DATA + structure, which defines the layout of a data area that the kernel + places at a pre-set address for sharing with user-mode software. + + https://www.geoffchappell.com/studies/windows/km/ntoskrnl/structs/kuser_shared_data/index.htm + + GetVersionEx or the Version Helper functions (IsWindows10OrGreater, + VerifyVersionInfo, etc.) don't work for Windows 10 and above, + because unless the binary includes a XML manifest declaring support + for such Windows versions, the functions always return "Windows + 8.2". KUSER_SHARED_DATA gives you the actual running OS info even + without the manifest. */ +#define KUSER_SHARED_DATA 0x7ffe0000 + +/* The Windows major version number. */ + +static inline ULONG +windows_major_version () +{ + return *(ULONG *) (KUSER_SHARED_DATA + 0x026c); +} + +/* The Windows minor version number. */ + +static inline ULONG +windows_minor_version () +{ + return *(ULONG *) (KUSER_SHARED_DATA + 0x0270); +} + +/* The Windows build number. Windows build version info can be found + here: + + https://www.gaijin.at/en/infos/windows-version-numbers + */ + +static inline ULONG +windows_build_number () +{ + return *(ULONG *) (KUSER_SHARED_DATA + 0x0260); +} + +/* See windows-nat.h. */ + +bool +dbg_reply_later_available () +{ +#if 0 + debug_printf ("Windows Major Version: %d\n", windows_major_version ()); + debug_printf ("Windows Minor Version: %d\n", windows_minor_version ()); + debug_printf ("Windows Build Number: %d\n", windows_build_number ()); +#endif + + /* Supported since Windows 10, Version 1507, which is reported as + build number 10240. */ + return (windows_major_version () > 10 + || (windows_major_version () == 10 + && windows_build_number () >= 10240)); +} + /* See windows-nat.h. */ bool diff --git a/gdb/nat/windows-nat.h b/gdb/nat/windows-nat.h index 2efb54e1ce7..256355f6ad0 100644 --- a/gdb/nat/windows-nat.h +++ b/gdb/nat/windows-nat.h @@ -415,6 +415,10 @@ extern DeleteProcThreadAttributeList_ftype *DeleteProcThreadAttributeList; extern bool disable_randomization_available (); +/* Return true if it's possible to use DBG_REPLY_LATER with + ContinueDebugEvent on this host. */ +extern bool dbg_reply_later_available (); + /* Load any functions which may not be available in ancient versions of Windows. */ -- 2.43.2