public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
From: "Björn Schäpers" <gcc@hazardy.de>
To: Ian Lance Taylor <iant@google.com>
Cc: gcc-patches@gcc.gnu.org, gcc@gcc.gnu.org
Subject: Re: [PATCH 4/4] libbacktrace: get debug information for loaded dlls
Date: Wed, 3 Jan 2024 00:12:58 +0100	[thread overview]
Message-ID: <0c08e584-499f-473f-8699-a41c6a967536@hazardy.de> (raw)
In-Reply-To: <CAKOQZ8xgFYXwvm-=Q6L3r56-cn0Zi27H9P9LxNDrhArLDWPo3A@mail.gmail.com>

[-- Attachment #1: Type: text/plain, Size: 830 bytes --]

Am 30.11.2023 um 20:53 schrieb Ian Lance Taylor:
> On Fri, Jan 20, 2023 at 2:55 AM Björn Schäpers <gcc@hazardy.de> wrote:
>>
>> From: Björn Schäpers <bjoern@hazardy.de>
>>
>> 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.
> 
> Thanks, but I don't want a patch that loops using goto statements.
> Please rewrite to avoid that.  It may be simpler to call a function.
> 
> Also starting with a module count of 1000 seems like a lot.  Do
> typical Windows programs load that many modules?
> 
> Ian
> 
> 

Rewritten using a function.

If that is commited, could you attribute that commit to me (--author="Björn 
Schäpers <bjoern@hazardy.de>")?

Thanks and kind regards,
Björn.

[-- Attachment #2: 0003-libbacktrace-get-debug-information-for-loaded-dlls.patch --]
[-- Type: text/plain, Size: 5230 bytes --]

From bd552716ee7937cad9d54d4966532d6ea6dbc1bc Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Bj=C3=B6rn=20Sch=C3=A4pers?= <bjoern@hazardy.de>
Date: Sun, 30 Apr 2023 23:54:32 +0200
Subject: [PATCH] libbacktrace: get debug information for loaded dlls
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

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 <bjoern@hazardy.de>
---
 libbacktrace/pecoff.c | 104 ++++++++++++++++++++++++++++++++++++++----
 1 file changed, 96 insertions(+), 8 deletions(-)

diff --git a/libbacktrace/pecoff.c b/libbacktrace/pecoff.c
index f976a963bf3..3eb9c4a4853 100644
--- a/libbacktrace/pecoff.c
+++ b/libbacktrace/pecoff.c
@@ -49,6 +49,7 @@ POSSIBILITY OF SUCH DAMAGE.  */
 #endif
 
 #include <windows.h>
+#include <psapi.h>
 #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;
@@ -870,12 +872,7 @@ coff_add (struct backtrace_state *state, int descriptor,
     }
 
 #ifdef HAVE_WINDOWS_H
-  {
-    uintptr_t module_handle;
-
-    module_handle = (uintptr_t) GetModuleHandle (NULL);
-    base_address = module_handle - image_base;
-  }
+  base_address = module_handle - image_base;
 #endif
 
   if (!backtrace_dwarf_add (state, base_address, &dwarf_sections,
@@ -903,6 +900,53 @@ coff_add (struct backtrace_state *state, int descriptor,
   return 0;
 }
 
+#ifdef HAVE_WINDOWS_H
+static void
+free_modules (struct backtrace_state *state,
+	      backtrace_error_callback error_callback, void *data,
+	      HMODULE **modules, DWORD bytes_allocated)
+{
+  backtrace_free (state, *modules, bytes_allocated, error_callback, data);
+  *modules = NULL;
+}
+
+static void
+get_all_modules (struct backtrace_state *state,
+		 backtrace_error_callback error_callback, void *data, 
+		 HMODULE **modules, DWORD *module_count, DWORD *bytes_allocated)
+{
+  DWORD bytes_needed = 0;
+
+  for (;;)
+    {
+      *bytes_allocated = *module_count * sizeof(HMODULE);
+      *modules = backtrace_alloc (state, *bytes_allocated, error_callback, data);
+
+      if (*modules == NULL)
+	return;
+
+      if (!EnumProcessModules (GetCurrentProcess (), *modules, *module_count,
+			       &bytes_needed))
+	{
+	  error_callback(data, "Could not enumerate process modules",
+			 (int) GetLastError ());
+	  free_modules (state, error_callback, data, modules, *bytes_allocated);
+	  return;
+	}
+
+      *module_count = bytes_needed / sizeof(HMODULE);
+      if (bytes_needed <= *bytes_allocated)
+	{
+	  return;
+	}
+
+      free_modules (state, error_callback, data, modules, *bytes_allocated);
+      // Add an extra of 2, of some module is loaded in another thread.
+      *module_count += 2;
+    }
+}
+#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.  */
@@ -917,12 +961,56 @@ 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 = 100;
+  DWORD bytes_allocated_for_modules = 0;
+  HMODULE *modules = NULL;
+  char module_name[MAX_PATH];
+  int module_found_sym;
+  fileline module_fileline_fn;
+
+  module_handle = (uintptr_t) GetModuleHandle (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
+  get_all_modules (state, error_callback, data, &modules, 
+		   &module_count, &bytes_allocated_for_modules);
+
+  for (i = 0; i < module_count; ++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;
+	}
+    }
+
+  if (modules)
+    free_modules (state, error_callback, data, &modules, bytes_allocated_for_modules);
+#endif
+
   if (!state->threaded)
     {
       if (found_sym)
-- 
2.42.1


  parent reply	other threads:[~2024-01-02 23:13 UTC|newest]

Thread overview: 53+ 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 [this message]
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
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-05-03 22:27                       ` Ian Lance Taylor
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=0c08e584-499f-473f-8699-a41c6a967536@hazardy.de \
    --to=gcc@hazardy.de \
    --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).