From patchwork Tue Dec 6 19:50:26 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Bj=C3=B6rn_Sch=C3=A4pers?= X-Patchwork-Id: 30483 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:f944:0:0:0:0:0 with SMTP id q4csp3019328wrr; Tue, 6 Dec 2022 11:52:41 -0800 (PST) X-Google-Smtp-Source: AA0mqf60WumsL7CUoKdIu8bF93l+ZCFwOeDSfOdTxgzq9mjd+tDlT9bY+2FuBdX4Pq8xcPrhNi+k X-Received: by 2002:a05:6402:54:b0:46a:c420:6da7 with SMTP id f20-20020a056402005400b0046ac4206da7mr46215142edu.202.1670356361246; Tue, 06 Dec 2022 11:52:41 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1670356361; cv=none; d=google.com; s=arc-20160816; b=D59lYqoqagukOr+/+PDT3UVcOq7q9KTOlgkBC6MdEhBqZuxeoBBzKoytfqdET2qWAs 1dNNYz38h5y6gPR4wCMTHprX5QFqzsjNSZDLQm0eB9P/Rr01K2D0AoIk+EdI4WcKgzQC x8H0q7m948pG46GXhBlkWm32WjFnNc4CSVlm+I9a5LvzQzY3h7NoEsaIBmqIBdmhLGDC EeZkdmpc5u6faWV/BQ//2XIl/SOCeGPvF3u7cVXLN4BF8pQsKT+FwFah0T51DzW8pqMf nMo3z6+CJ9isot7mLgNOPZNFbZvTnrxWTHxyj3IV6rsRmOw9r9AJMJTYwWBihL/aa9h7 tc9g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:to:from :dmarc-filter:delivered-to; bh=lZwTuffxMGl6NvjHVBEMH+tQMewZG5eSJGxTNh9z2vM=; b=xYBe7TDMo15pNDMxSeTVyn+QMSboMLQMhjBxOSZSDbzmg5SnRncbW2qdbQGCkRG/CV oMeIUJHGfTArpWTaKt3pb2tQHqaqHlEZQ7+yezn6j++OVi3ZIJOSAhNKs3dxOBs1xuUf G0BVhkmWPNP055oMrhwlAUweur+2ehddCdW0adOalQFA8XQtPLRj8MYtJH2fvBXuZCW6 PeIFGF9u7KTghS9Q3QUB10f6bd13tDSE6FGIbOYsc89idIgvsPDSfv4nHqjQCNdaOz/J ATfZYDfafLVRc2bZZ1EyqEzquHIBt9cPEIMXf4Z9ay9ZfIBGMhdL397LrUh4Ye73puXu pyGg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org" Received: from sourceware.org (server2.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id q20-20020a056402041400b0046b0205ab58si2369939edv.580.2022.12.06.11.52.41 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 06 Dec 2022 11:52:41 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org" Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 176703833A19 for ; Tue, 6 Dec 2022 19:51:16 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail.hazardy.de (mail.hazardy.de [78.94.181.132]) by sourceware.org (Postfix) with ESMTPS id 438C8383B6F4 for ; Tue, 6 Dec 2022 19:50:38 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 438C8383B6F4 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=hazardy.de Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=hazardy.de Received: from NB-372.intranet.mimot.com (unknown [78.94.181.132]) by mail.hazardy.de (Postfix) with ESMTPSA id 723A37002D6; Tue, 6 Dec 2022 20:50:36 +0100 (CET) From: =?utf-8?q?Bj=C3=B6rn_Sch=C3=A4pers?= To: gcc-patches@gcc.gnu.org, iant@golang.org Subject: [PATCH 2/4] libbacktrace: detect executable path on windows Date: Tue, 6 Dec 2022 20:50:26 +0100 Message-Id: <20221206195028.37104-2-gcc@hazardy.de> X-Mailer: git-send-email 2.38.1 In-Reply-To: <20221206195028.37104-1-gcc@hazardy.de> References: <20221206195028.37104-1-gcc@hazardy.de> MIME-Version: 1.0 X-Spam-Status: No, score=-13.5 required=5.0 tests=BAYES_00, GIT_PATCH_0, KAM_DMARC_STATUS, 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 X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1751495592319193716?= X-GMAIL-MSGID: =?utf-8?q?1751495592319193716?= From: Björn Schäpers This is actually needed so that libstdc++'s implementation to be able to work on windows. Tested on x86_64-linux and i686-w64-mingw32. -- >8 -- * configure.ac: Add a check for windows.h. * configure, config.h.in: Regenerate. * fileline.c: Add windows_get_executable_path. * fileline.c (fileline_initialiez): Add a pass using windows_get_executable_path. Signed-off-by: Björn Schäpers --- libbacktrace/configure.ac | 2 ++ libbacktrace/fileline.c | 43 ++++++++++++++++++++++++++++++++++++++- 4 files changed, 60 insertions(+), 1 deletion(-) diff --git a/libbacktrace/configure.ac b/libbacktrace/configure.ac index 1daaa2f62d2..b5feb29bcdc 100644 --- a/libbacktrace/configure.ac +++ b/libbacktrace/configure.ac @@ -377,6 +377,8 @@ if test "$have_loadquery" = "yes"; then AC_DEFINE(HAVE_LOADQUERY, 1, [Define if AIX loadquery is available.]) fi +AC_CHECK_HEADERS(windows.h) + # Check for the fcntl function. if test -n "${with_target_subdir}"; then case "${host}" in diff --git a/libbacktrace/fileline.c b/libbacktrace/fileline.c index a40cd498114..73c2c8e8bc9 100644 --- a/libbacktrace/fileline.c +++ b/libbacktrace/fileline.c @@ -47,6 +47,18 @@ POSSIBILITY OF SUCH DAMAGE. */ #include #endif +#ifdef HAVE_WINDOWS_H +#ifndef WIN32_MEAN_AND_LEAN +#define WIN32_MEAN_AND_LEAN +#endif + +#ifndef NOMINMAX +#define NOMINMAX +#endif + +#include +#endif + #include "backtrace.h" #include "internal.h" @@ -155,6 +167,28 @@ macho_get_executable_path (struct backtrace_state *state, #endif /* !defined (HAVE_MACH_O_DYLD_H) */ +#ifdef HAVE_WINDOWS_H + +static char * +windows_get_executable_path (char *buf, backtrace_error_callback error_callback, + void *data) +{ + if (GetModuleFileNameA (NULL, buf, MAX_PATH - 1) == 0) + { + error_callback (data, + "could not get the filename of the current executable", + (int) GetLastError ()); + return NULL; + } + return buf; +} + +#else /* !defined (HAVE_WINDOWS_H) */ + +#define windows_get_executable_path(buf, error_callback, data) NULL + +#endif /* !defined (HAVE_WINDOWS_H) */ + /* Initialize the fileline information from the executable. Returns 1 on success, 0 on failure. */ @@ -168,7 +202,11 @@ fileline_initialize (struct backtrace_state *state, int called_error_callback; int descriptor; const char *filename; +#ifdef HAVE_WINDOWS_H + char buf[MAX_PATH]; +#else char buf[64]; +#endif if (!state->threaded) failed = state->fileline_initialization_failed; @@ -192,7 +230,7 @@ fileline_initialize (struct backtrace_state *state, descriptor = -1; called_error_callback = 0; - for (pass = 0; pass < 8; ++pass) + for (pass = 0; pass < 9; ++pass) { int does_not_exist; @@ -224,6 +262,9 @@ fileline_initialize (struct backtrace_state *state, case 7: filename = macho_get_executable_path (state, error_callback, data); break; + case 8: + filename = windows_get_executable_path (buf, error_callback, data); + break; default: abort (); }