Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Eli Zaretskii via Gdb-patches <gdb-patches@sourceware.org>
To: Simon Marchi <simon.marchi@polymtl.ca>
Cc: tom@tromey.com, gdb-patches@sourceware.org
Subject: Re: Subtle problems with "info sharedlibrary" on MS-Windows
Date: Sun, 11 Apr 2021 21:43:03 +0300	[thread overview]
Message-ID: <83v98s3bns.fsf@gnu.org> (raw)
In-Reply-To: <a2117113-fd72-52eb-7786-670887a1c2b7@polymtl.ca> (message from Simon Marchi on Sun, 11 Apr 2021 08:27:55 -0400)

> Cc: tom@tromey.com, gdb-patches@sourceware.org
> From: Simon Marchi <simon.marchi@polymtl.ca>
> Date: Sun, 11 Apr 2021 08:27:55 -0400
> 
> > I'd actually prefer it the other way around, for 2 reasons: (a) the
> > patch for windows-nat.c is already in, so it would make sense to fix
> > gdbserver ASAP; (b) I built with the patch a recent snapshot of master
> > where these conditions are still present, and I would like to avoid
> > the need to build yet another snapshot just for this small change.
> 
> Ok, now that I re-read the patch, I realize that the _WIN32_WCE macro
> doesn't add much complexity, I thought it was worst at first.  So, the
> patch LGTM, with a proper commit message and ChangeLog entry of course.

Thanks, done.  Below is the actual change I pushed:

commit 114ee2a4aef573bf43ab71b9d0b85aaccfd8852e
Author:     Eli Zaretskii <eliz@gnu.org>
AuthorDate: Sun Apr 11 21:37:29 2021 +0300
Commit:     Eli Zaretskii <eliz@gnu.org>
CommitDate: Sun Apr 11 21:37:29 2021 +0300

    Improve support for loading DLLs at run time in gdbserver.
    
    This fixes win32-low.cc in the same way as a recent change in
    windows-nat.c did for GDB: if the lpImageName member of the load-DLL
    debug event doesn't allow us to find the file name of the DLL, then
    loop over all the DLLs mapped into the inferior to find the one loaded
    at the same base address as given by the lpBaseOfDll member of the
    debug event.
    
    gdbserver/ChangeLog:
    
    2021-04-11  Eli Zaretskii  <eliz@gnu.org>
    
            * win32-low.cc (win32_add_dll): New function, with body almost
            identical to what win32_add_all_dlls did.  Accepts one argument;
            if that is non-NULL, returns the file name of the DLL that is
            loaded at the base address equal to that argument, or NULL if not
            found.  If the argument is NULL, add all the DLLs loaded by the
            inferior to the list of solibs and return NULL.
            (win32_add_all_dlls): Now a thin wrapper around win32_add_dll.
            (windows_nat::handle_load_dll) [!_WIN32_WCE]: If get_image_name
            failed to glean the file name of the DLL, call win32_add_dll to
            try harder using the lpBaseOfDll member of the load-DLL event.

diff --git a/gdbserver/ChangeLog b/gdbserver/ChangeLog
index 58ed0f0..029a2e4 100644
--- a/gdbserver/ChangeLog
+++ b/gdbserver/ChangeLog
@@ -1,3 +1,16 @@
+2021-04-11  Eli Zaretskii  <eliz@gnu.org>
+
+	* win32-low.cc (win32_add_dll): New function, with body almost
+	identical to what win32_add_all_dlls did.  Accepts one argument;
+	if that is non-NULL, returns the file name of the DLL that is
+	loaded at the base address equal to that argument, or NULL if not
+	found.  If the argument is NULL, add all the DLLs loaded by the
+	inferior to the list of solibs and return NULL.
+	(win32_add_all_dlls): Now a thin wrapper around win32_add_dll.
+	(windows_nat::handle_load_dll) [!_WIN32_WCE]: If get_image_name
+	failed to glean the file name of the DLL, call win32_add_dll to
+	try harder using the lpBaseOfDll member of the load-DLL event.
+
 2021-03-30  Luis Machado  <luis.machado@linaro.org>
 
 	* server.cc (handle_general_set, handle_query): Update variable
diff --git a/gdbserver/win32-low.cc b/gdbserver/win32-low.cc
index 1f319a2..f6d35ca 100644
--- a/gdbserver/win32-low.cc
+++ b/gdbserver/win32-low.cc
@@ -1165,11 +1165,13 @@ load_psapi (void)
 
 #ifndef _WIN32_WCE
 
-/* Iterate over all DLLs currently mapped by our inferior, and
-   add them to our list of solibs.  */
+/* Iterate over all DLLs currently mapped by our inferior, looking for
+   a DLL loaded at LOAD_ADDR; if found, return its file name,
+   otherwise return NULL.  If LOAD_ADDR is NULL, add all mapped DLLs
+   to our list of solibs.  */
 
-static void
-win32_add_all_dlls (void)
+static char *
+win32_add_dll (LPVOID load_addr)
 {
   size_t i;
   HMODULE dh_buf[1];
@@ -1178,7 +1180,7 @@ win32_add_all_dlls (void)
   BOOL ok;
 
   if (!load_psapi ())
-    return;
+    return NULL;
 
   cbNeeded = 0;
 #ifdef __x86_64__
@@ -1196,11 +1198,11 @@ win32_add_all_dlls (void)
 				      &cbNeeded);
 
   if (!ok || !cbNeeded)
-    return;
+    return NULL;
 
   DllHandle = (HMODULE *) alloca (cbNeeded);
   if (!DllHandle)
-    return;
+    return NULL;
 
 #ifdef __x86_64__
   if (wow64_process)
@@ -1216,7 +1218,7 @@ win32_add_all_dlls (void)
 				      cbNeeded,
 				      &cbNeeded);
   if (!ok)
-    return;
+    return NULL;
 
   char system_dir[MAX_PATH];
   char syswow_dir[MAX_PATH];
@@ -1252,7 +1254,7 @@ win32_add_all_dlls (void)
   for (i = 1; i < ((size_t) cbNeeded / sizeof (HMODULE)); i++)
     {
       MODULEINFO mi;
-      char dll_name[MAX_PATH];
+      static char dll_name[MAX_PATH];
 
       if (!(*win32_GetModuleInformation) (current_process_handle,
 					  DllHandle[i],
@@ -1265,6 +1267,9 @@ win32_add_all_dlls (void)
 					 MAX_PATH) == 0)
 	continue;
 
+      if (load_addr != nullptr && mi.lpBaseOfDll != load_addr)
+	continue;
+
       const char *name = dll_name;
       /* Convert the DLL path of 32bit processes returned by
 	 GetModuleFileNameEx from the 64bit system directory to the
@@ -1279,10 +1284,27 @@ win32_add_all_dlls (void)
 	  name = syswow_dll_path.c_str();
 	}
 
-      win32_add_one_solib (name, (CORE_ADDR) (uintptr_t) mi.lpBaseOfDll);
+      if (load_addr != nullptr)
+	{
+	  if (name != dll_name)
+	    strcpy (dll_name, name);
+	  return dll_name;
+	}
+      else
+	win32_add_one_solib (name, (CORE_ADDR) (uintptr_t) mi.lpBaseOfDll);
     }
+  return NULL;
 }
-#endif
+
+/* Iterate over all DLLs currently mapped by our inferior, and
+   add them to our list of solibs.  */
+
+static void
+win32_add_all_dlls (void)
+{
+  win32_add_dll (NULL);
+}
+#endif	/* !_WIN32_WCE */
 
 typedef HANDLE (WINAPI *winapi_CreateToolhelp32Snapshot) (DWORD, DWORD);
 typedef BOOL (WINAPI *winapi_Module32First) (HANDLE, LPMODULEENTRY32);
@@ -1298,7 +1320,12 @@ windows_nat::handle_load_dll ()
 
   dll_name = get_image_name (current_process_handle,
 			     event->lpImageName, event->fUnicode);
-  if (!dll_name)
+#ifndef _WIN32_WCE
+  if (dll_name == nullptr
+      && event->lpBaseOfDll != nullptr)
+    dll_name = win32_add_dll (event->lpBaseOfDll);
+#endif
+  if (dll_name == nullptr)
     return;
 
   win32_add_one_solib (dll_name, (CORE_ADDR) (uintptr_t) event->lpBaseOfDll);

  reply	other threads:[~2021-04-11 18:43 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-03-10 12:36 Eli Zaretskii via Gdb-patches
2021-03-10 16:30 ` Hannes Domani via Gdb-patches
2021-03-10 16:51   ` Eli Zaretskii via Gdb-patches
2021-03-10 17:35     ` Hannes Domani via Gdb-patches
2021-04-05 17:51       ` Eli Zaretskii via Gdb-patches
2021-04-06 13:16         ` Eli Zaretskii via Gdb-patches
2021-04-07 21:18           ` Simon Marchi via Gdb-patches
2021-04-08  7:06             ` Eli Zaretskii via Gdb-patches
2021-04-08 13:57               ` Simon Marchi via Gdb-patches
2021-04-10  8:46                 ` Eli Zaretskii via Gdb-patches
2021-04-10 15:03           ` Tom Tromey
2021-04-10 18:07             ` Eli Zaretskii via Gdb-patches
2021-04-10 22:56               ` Simon Marchi via Gdb-patches
2021-04-10 23:11                 ` Simon Marchi via Gdb-patches
2021-04-11  7:10                 ` Eli Zaretskii via Gdb-patches
2021-04-11 12:27                   ` Simon Marchi via Gdb-patches
2021-04-11 18:43                     ` Eli Zaretskii via Gdb-patches [this message]
2021-04-12 19:03                       ` Tom Tromey

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=83v98s3bns.fsf@gnu.org \
    --to=gdb-patches@sourceware.org \
    --cc=eliz@gnu.org \
    --cc=simon.marchi@polymtl.ca \
    --cc=tom@tromey.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