From patchwork Tue Dec 6 19:50:28 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: 30480 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:f944:0:0:0:0:0 with SMTP id q4csp3019156wrr; Tue, 6 Dec 2022 11:52:09 -0800 (PST) X-Google-Smtp-Source: AA0mqf7B84ubK82+wpElVGmyPGh9rIPORciH5JMoV6PkafjyIOMH4AM5GpSPinOYTswgSH7oEE8c X-Received: by 2002:a17:906:2355:b0:7c0:e6da:e25a with SMTP id m21-20020a170906235500b007c0e6dae25amr11167668eja.22.1670356329634; Tue, 06 Dec 2022 11:52:09 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1670356329; cv=none; d=google.com; s=arc-20160816; b=kODNYP+9uUQFuXzz8BKoadmJX3r0IqmBaQS/h+ekTd2ReUlSNfD7oK7Bra9Zq9VJRY Fkwn17sVpxJ2S7HsXmI1+cLBpviU/eBZbJ8bzXV73TyCcpTfVQhRMHt0StpeOjPbFP5A BM0kM/m3uTCDbTQTrLurZI1epb2HET4itoLvccDIGxqmSEq6E6aFUW0ZuG/MnOCZgNvK cHtOY65Rjted58rBYpkvDzrft3E7S2R7eHzsurGqNSqy8dy8YjidS6mCzrf09rxSKKP8 ZgLyHmR0v5bYIuNjnq4ZbZ+ITpOmu4GHdutP+YPicdh5EAU7FdANb08c2mVP88qkY2up ADGA== 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=iaXDAdfhD7kVYSziRMvZEgDC1bCbldNtT/3FbZq/Lss=; b=iqWH/H6KccwbeYAUSPgZ0tHtNCV69LEKjoW+AvBlXr9n3kN2Q5heNWFf8H7taUHKFu aLKn/YmyRied5oJqpxoh5YHu/fQT/q36+VjDWZVuLHLHymYQ15yf7JCbTEGxHqMh8uuH qwijRzOcpBTcZqeK3f+iFpr70kBPijk4leZBpk3B5nIhsDmTKubI1Ag4LWNqQHu/htb5 r+w/q5G9RAmOnKf6nm5l7ItiDUMeq/9P3HTsd3SAIfpTGVH0URumVc3U+3cS/NjGnsqQ BAC8uXT1Mq2u7kTXuWA5qub/sBED0ad3Jr8qANlczfqLYnwe4IGFk1Y809uz5KOhsuIz o0hw== 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 hs39-20020a1709073ea700b0078e1246e7d6si3267085ejc.748.2022.12.06.11.52.09 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 06 Dec 2022 11:52:09 -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 8EDB63839D30 for ; Tue, 6 Dec 2022 19:51:08 +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 C71FB383B6F9 for ; Tue, 6 Dec 2022 19:50:38 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org C71FB383B6F9 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 4D79C700B61; Tue, 6 Dec 2022 20:50:37 +0100 (CET) From: =?utf-8?q?Bj=C3=B6rn_Sch=C3=A4pers?= To: gcc-patches@gcc.gnu.org, iant@golang.org Subject: [PATCH 4/4] libbacktrace: get debug information for loaded dlls Date: Tue, 6 Dec 2022 20:50:28 +0100 Message-Id: <20221206195028.37104-4-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=-12.9 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?1751495558667644333?= X-GMAIL-MSGID: =?utf-8?q?1751495558667644333?= From: Björn Schäpers Fixes https://github.com/ianlancetaylor/libbacktrace/issues/53, except that libraries loaded after the backtrace_initialize are not handled. But as far as I can see that's the same for elf. Tested on x86_64-linux and i686-w64-mingw32. -- >8 -- * pecoff.c (coff_add): New argument for the module handle of the file, to get the base address. * pecoff.c (backtrace_initialize): Iterate over loaded libraries and call coff_add. Signed-off-by: Björn Schäpers --- libbacktrace/pecoff.c | 76 ++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 72 insertions(+), 4 deletions(-) diff --git a/libbacktrace/pecoff.c b/libbacktrace/pecoff.c index 296f1357b5f..40395109e51 100644 --- a/libbacktrace/pecoff.c +++ b/libbacktrace/pecoff.c @@ -49,6 +49,7 @@ POSSIBILITY OF SUCH DAMAGE. */ #endif #include +#include #endif /* Coff file header. */ @@ -592,7 +593,8 @@ coff_syminfo (struct backtrace_state *state, uintptr_t addr, static int coff_add (struct backtrace_state *state, int descriptor, backtrace_error_callback error_callback, void *data, - fileline *fileline_fn, int *found_sym, int *found_dwarf) + fileline *fileline_fn, int *found_sym, int *found_dwarf, + uintptr_t module_handle ATTRIBUTE_UNUSED) { struct backtrace_view fhdr_view; off_t fhdr_off; @@ -623,7 +625,6 @@ coff_add (struct backtrace_state *state, int descriptor, int is_64; uintptr_t image_base; uintptr_t base_address = 0; - uintptr_t module_handle; struct dwarf_sections dwarf_sections; *found_sym = 0; @@ -871,7 +872,6 @@ coff_add (struct backtrace_state *state, int descriptor, } #ifdef HAVE_WINDOWS_H - module_handle = (uintptr_t) GetModuleHandleW (NULL); base_address = module_handle - image_base; #endif @@ -914,12 +914,80 @@ backtrace_initialize (struct backtrace_state *state, int found_sym; int found_dwarf; fileline coff_fileline_fn; + uintptr_t module_handle = 0; + +#ifdef HAVE_WINDOWS_H + DWORD i; + DWORD module_count; + DWORD bytes_needed_for_modules; + HMODULE *modules; + char module_name[MAX_PATH]; + int module_found_sym; + fileline module_fileline_fn; + + module_handle = (uintptr_t) GetModuleHandleW (NULL); +#endif ret = coff_add (state, descriptor, error_callback, data, - &coff_fileline_fn, &found_sym, &found_dwarf); + &coff_fileline_fn, &found_sym, &found_dwarf, module_handle); if (!ret) return 0; +#ifdef HAVE_WINDOWS_H + module_count = 1000; + alloc_modules: + modules = backtrace_alloc (state, module_count * sizeof(HMODULE), + error_callback, data); + if (modules == NULL) + goto skip_modules; + if (!EnumProcessModules (GetCurrentProcess (), modules, module_count, + &bytes_needed_for_modules)) + { + error_callback(data, "Could not enumerate process modules", + (int) GetLastError ()); + goto free_modules; + } + if (bytes_needed_for_modules > module_count * sizeof(HMODULE)) + { + backtrace_free (state, modules, module_count * sizeof(HMODULE), + error_callback, data); + // Add an extra of 2, if some module is loaded in another thread. + module_count = bytes_needed_for_modules / sizeof(HMODULE) + 2; + modules = NULL; + goto alloc_modules; + } + + for (i = 0; i < bytes_needed_for_modules / sizeof(HMODULE); ++i) + { + if (GetModuleFileNameA (modules[i], module_name, MAX_PATH - 1)) + { + if (strcmp (filename, module_name) == 0) + continue; + + module_handle = (uintptr_t) GetModuleHandleA (module_name); + if (module_handle == 0) + continue; + + descriptor = backtrace_open (module_name, error_callback, data, NULL); + if (descriptor < 0) + continue; + + coff_add (state, descriptor, error_callback, data, + &module_fileline_fn, &module_found_sym, &found_dwarf, + module_handle); + if (module_found_sym) + found_sym = 1; + } + } + + free_modules: + if (modules) + backtrace_free(state, modules, module_count * sizeof(HMODULE), + error_callback, data); + modules = NULL; + skip_modules: +#endif + if (!state->threaded) { if (found_sym)