From: "Björn Schäpers" <gcc@hazardy.de>
To: Eli Zaretskii <eliz@gnu.org>
Cc: iant@google.com, gcc-patches@gcc.gnu.org, gcc@gcc.gnu.org
Subject: Re: [PATCH 6/4] libbacktrace: Add loaded dlls after initialize
Date: Fri, 15 Mar 2024 21:41:41 +0100 [thread overview]
Message-ID: <6e6822f2-d3e9-4b61-a177-8e846e3c6380@hazardy.de> (raw)
In-Reply-To: <83h6jlnz8t.fsf@gnu.org>
[-- Attachment #1: Type: text/plain, Size: 679 bytes --]
Am 10.01.2024 um 13:34 schrieb Eli Zaretskii:
>> Date: Tue, 9 Jan 2024 21:02:44 +0100
>> Cc: iant@google.com, gcc-patches@gcc.gnu.org, gcc@gcc.gnu.org
>> From: Björn Schäpers <gcc@hazardy.de>
>>
>> Am 07.01.2024 um 18:03 schrieb Eli Zaretskii:
>>> In that case, you an call either GetModuleHandeExA or
>>> GetModuleHandeExW, the difference is minor.
>>
>> Here an updated version without relying on TEXT or TCHAR, directly calling
>> GetModuleHandleExW.
>
> Thanks, this LGTM (but I couldn't test it, I just looked at the
> sour ce code).
Here an updated version. It is rebased on the combined approach of getting the
loaded DLLs and two minor changes to suppress warnings.
[-- Attachment #2: 0004-libbacktrace-Add-loaded-dlls-after-initialize.patch --]
[-- Type: text/plain, Size: 4284 bytes --]
From 55d0f312c0a9c4e2305d72fa2329b37937a02e44 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Bj=C3=B6rn=20Sch=C3=A4pers?= <bjoern@hazardy.de>
Date: Sat, 6 Jan 2024 22:53:54 +0100
Subject: [PATCH 2/2] libbacktrace: Add loaded dlls after initialize
libbacktrace/Changelog:
* pecoff.c [HAVE_WINDOWS_H]:
(dll_notification_data): Added
(dll_notification_context): Added
(dll_notification): Added
(backtrace_initialize): Use LdrRegisterDllNotification to load
debug information of later loaded
dlls.
---
libbacktrace/pecoff.c | 106 ++++++++++++++++++++++++++++++++++++++++++
1 file changed, 106 insertions(+)
diff --git a/libbacktrace/pecoff.c b/libbacktrace/pecoff.c
index faa0be0b700..455a5c2191d 100644
--- a/libbacktrace/pecoff.c
+++ b/libbacktrace/pecoff.c
@@ -61,6 +61,34 @@ POSSIBILITY OF SUCH DAMAGE. */
#undef Module32Next
#endif
#endif
+
+#if defined(_ARM_)
+#define NTAPI
+#else
+#define NTAPI __stdcall
+#endif
+
+/* This is a simplified (but binary compatible) version of what Microsoft
+ defines in their documentation. */
+struct dll_notifcation_data
+{
+ ULONG reserved;
+ /* The name as UNICODE_STRING struct. */
+ PVOID full_dll_name;
+ PVOID base_dll_name;
+ PVOID dll_base;
+ ULONG size_of_image;
+};
+
+#define LDR_DLL_NOTIFICATION_REASON_LOADED 1
+
+typedef LONG NTSTATUS;
+typedef VOID CALLBACK (*LDR_DLL_NOTIFICATION)(ULONG,
+ struct dll_notifcation_data*,
+ PVOID);
+typedef NTSTATUS NTAPI (*LDR_REGISTER_FUNCTION)(ULONG,
+ LDR_DLL_NOTIFICATION, PVOID,
+ PVOID*);
#endif
/* Coff file header. */
@@ -911,6 +939,53 @@ coff_add (struct backtrace_state *state, int descriptor,
return 0;
}
+#ifdef HAVE_WINDOWS_H
+struct dll_notification_context
+{
+ struct backtrace_state *state;
+ backtrace_error_callback error_callback;
+ void *data;
+};
+
+static VOID CALLBACK
+dll_notification (ULONG reason,
+ struct dll_notifcation_data *notification_data,
+ PVOID context)
+{
+ char module_name[MAX_PATH];
+ int descriptor;
+ struct dll_notification_context* dll_context =
+ (struct dll_notification_context*) context;
+ struct backtrace_state *state = dll_context->state;
+ void *data = dll_context->data;
+ backtrace_error_callback error_callback = dll_context->data;
+ fileline fileline;
+ int found_sym;
+ int found_dwarf;
+ HMODULE module_handle;
+
+ if (reason != LDR_DLL_NOTIFICATION_REASON_LOADED)
+ return;
+
+ if (!GetModuleHandleExW (GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS
+ | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT,
+ (wchar_t*) notification_data->dll_base,
+ &module_handle))
+ return;
+
+ if (!GetModuleFileNameA ((HMODULE) module_handle, module_name, MAX_PATH - 1))
+ return;
+
+ descriptor = backtrace_open (module_name, error_callback, data, NULL);
+
+ if (descriptor < 0)
+ return;
+
+ coff_add (state, descriptor, error_callback, data, &fileline, &found_sym,
+ &found_dwarf, (uintptr_t) module_handle);
+}
+#endif
+
/* Initialize the backtrace data we need from an ELF executable. At
the ELF level, all we need to do is find the debug info
sections. */
@@ -935,6 +1010,8 @@ backtrace_initialize (struct backtrace_state *state,
#endif
#ifdef HAVE_WINDOWS_H
+ HMODULE nt_dll_handle;
+
module_handle = (uintptr_t) GetModuleHandle (NULL);
#endif
@@ -981,6 +1058,35 @@ backtrace_initialize (struct backtrace_state *state,
}
#endif
+#ifdef HAVE_WINDOWS_H
+ nt_dll_handle = GetModuleHandleW (L"ntdll.dll");
+ if (nt_dll_handle)
+ {
+ LDR_REGISTER_FUNCTION register_func;
+ const char register_name[] = "LdrRegisterDllNotification";
+ register_func = (void*) GetProcAddress (nt_dll_handle,
+ register_name);
+
+ if (register_func)
+ {
+ PVOID cookie;
+ struct dll_notification_context *context
+ = backtrace_alloc (state,
+ sizeof(struct dll_notification_context),
+ error_callback, data);
+
+ if (context)
+ {
+ context->state = state;
+ context->data = data;
+ context->error_callback = error_callback;
+
+ register_func (0, &dll_notification, context, &cookie);
+ }
+ }
+ }
+#endif
+
if (!state->threaded)
{
if (found_sym)
--
2.43.0
next prev parent reply other threads:[~2024-03-15 20:41 UTC|newest]
Thread overview: 52+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-01-20 10:54 [PATCH 1/4] libbacktrace: change all pc related variables to uintptr_t Björn Schäpers
2023-01-20 10:54 ` [PATCH 2/4] libbacktrace: detect executable path on windows Björn Schäpers
2023-01-23 23:00 ` Ian Lance Taylor
2023-01-24 13:11 ` Eli Zaretskii
2023-01-24 14:35 ` Ian Lance Taylor
2023-01-24 16:52 ` Eli Zaretskii
2023-01-24 17:58 ` Ian Lance Taylor
2023-01-24 18:11 ` Eli Zaretskii
2023-01-24 18:32 ` Ian Lance Taylor
2023-02-05 9:20 ` Björn Schäpers
2023-02-06 0:22 ` Ian Lance Taylor
2023-11-20 19:56 ` Björn Schäpers
2023-11-29 22:05 ` Ian Lance Taylor
2023-01-24 21:00 ` Björn Schäpers
2023-01-20 10:54 ` [PATCH 3/4] libbacktrace: work with aslr " Björn Schäpers
2023-01-20 13:39 ` Eli Zaretskii
2023-01-20 16:46 ` Gabriel Ravier
2023-01-20 19:19 ` Eli Zaretskii
2023-01-20 20:39 ` Gabriel Ravier
2023-01-21 4:05 ` Eli Zaretskii
2023-01-21 9:18 ` LIU Hao
2023-01-21 9:25 ` Eli Zaretskii
2023-01-21 10:47 ` Gabriel Ravier
2023-01-21 11:42 ` Eli Zaretskii
2023-11-20 19:57 ` Björn Schäpers
2023-11-20 20:07 ` Eli Zaretskii
2023-11-21 19:35 ` Björn Schäpers
2023-11-22 1:13 ` LIU Hao
2023-11-30 19:25 ` Ian Lance Taylor
2023-01-20 10:54 ` [PATCH 4/4] libbacktrace: get debug information for loaded dlls Björn Schäpers
2023-11-30 19:53 ` Ian Lance Taylor
2023-11-30 20:16 ` Eli Zaretskii
2024-01-02 23:12 ` Björn Schäpers
2024-01-04 22:33 ` [PATCH 5/4] libbacktrace: improve getting " Björn Schäpers
2024-01-06 22:15 ` [PATCH 6/4] libbacktrace: Add loaded dlls after initialize Björn Schäpers
2024-01-07 6:50 ` Eli Zaretskii
[not found] ` <4cb3a2a5-c0b3-40c8-b460-f21d65a9aea2@hazardy.de>
2024-01-07 14:46 ` Eli Zaretskii
2024-01-07 16:07 ` Björn Schäpers
2024-01-07 17:03 ` Eli Zaretskii
2024-01-09 20:02 ` Björn Schäpers
2024-01-10 12:34 ` Eli Zaretskii
2024-03-15 20:41 ` Björn Schäpers [this message]
2024-01-23 22:37 ` [PATCH 5/4] libbacktrace: improve getting debug information for loaded dlls Ian Lance Taylor
2024-01-25 19:53 ` Björn Schäpers
2024-01-25 22:04 ` Ian Lance Taylor
2024-03-15 20:40 ` Björn Schäpers
2024-04-25 20:14 ` Björn Schäpers
2024-04-28 18:16 ` Ian Lance Taylor
2024-05-02 19:23 ` Björn Schäpers
2024-01-23 21:24 ` [PATCH 4/4] libbacktrace: get " Björn Schäpers
2023-01-20 22:25 ` [PATCH 1/4] libbacktrace: change all pc related variables to uintptr_t Ian Lance Taylor
2023-01-23 20:17 ` Björn Schäpers
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=6e6822f2-d3e9-4b61-a177-8e846e3c6380@hazardy.de \
--to=gcc@hazardy.de \
--cc=eliz@gnu.org \
--cc=gcc-patches@gcc.gnu.org \
--cc=gcc@gcc.gnu.org \
--cc=iant@google.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).