From patchwork Tue Nov 26 17:11:32 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Simon Marchi (Code Review)" X-Patchwork-Id: 36221 Received: (qmail 94838 invoked by alias); 26 Nov 2019 17:12:01 -0000 Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org Delivered-To: mailing list gdb-patches@sourceware.org Received: (qmail 94579 invoked by uid 89); 26 Nov 2019 17:11:59 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-26.9 required=5.0 tests=BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3 autolearn=ham version=3.3.1 spammy= X-HELO: mx1.osci.io Received: from polly.osci.io (HELO mx1.osci.io) (8.43.85.229) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Tue, 26 Nov 2019 17:11:56 +0000 Received: by mx1.osci.io (Postfix, from userid 994) id 3DBF020C06; Tue, 26 Nov 2019 12:11:55 -0500 (EST) Received: from gnutoolchain-gerrit.osci.io (gnutoolchain-gerrit.osci.io [IPv6:2620:52:3:1:5054:ff:fe06:16ca]) by mx1.osci.io (Postfix) with ESMTP id 57BEE20D23 for ; Tue, 26 Nov 2019 12:11:32 -0500 (EST) Received: from localhost (localhost [127.0.0.1]) by gnutoolchain-gerrit.osci.io (Postfix) with ESMTP id 3A0B120AF6 for ; Tue, 26 Nov 2019 12:11:32 -0500 (EST) X-Gerrit-PatchSet: 1 Date: Tue, 26 Nov 2019 12:11:32 -0500 From: "Tom Tromey (Code Review)" To: gdb-patches@sourceware.org Message-ID: Auto-Submitted: auto-generated X-Gerrit-MessageType: newchange Subject: [review] Share get_image_name between gdb and gdbserver X-Gerrit-Change-Id: Ic132f29d071f281db7435027a1c78a8911bed142 X-Gerrit-Change-Number: 709 X-Gerrit-ChangeURL: X-Gerrit-Commit: eef1afa9c3648d1c4d73158f4ec31314d1b603ef References: Reply-To: tromey@sourceware.org, gdb-patches@sourceware.org MIME-Version: 1.0 Content-Disposition: inline User-Agent: Gerrit/3.0.3-79-g83ff7f88f1 Change URL: https://gnutoolchain-gerrit.osci.io/r/c/binutils-gdb/+/709 ...................................................................... Share get_image_name between gdb and gdbserver This moves get_image_name to nat/windows-nat.c so that it can be shared between gdb and gdbserver. gdb/ChangeLog 2019-11-26 Tom Tromey * windows-nat.c (get_image_name): Move to nat/windows-nat.c. (handle_load_dll): Update. * nat/windows-nat.c (get_image_name): Move from windows-nat.c. gdb/gdbserver/ChangeLog 2019-11-26 Tom Tromey * win32-low.c (get_image_name): Remove. (handle_load_dll): Update. Change-Id: Ic132f29d071f281db7435027a1c78a8911bed142 --- M gdb/ChangeLog M gdb/gdbserver/ChangeLog M gdb/gdbserver/win32-low.c M gdb/nat/windows-nat.c M gdb/nat/windows-nat.h M gdb/windows-nat.c 6 files changed, 77 insertions(+), 101 deletions(-) diff --git a/gdb/ChangeLog b/gdb/ChangeLog index 0a6e4b8..911c6a1 100644 --- a/gdb/ChangeLog +++ b/gdb/ChangeLog @@ -1,5 +1,11 @@ 2019-11-26 Tom Tromey + * windows-nat.c (get_image_name): Move to nat/windows-nat.c. + (handle_load_dll): Update. + * nat/windows-nat.c (get_image_name): Move from windows-nat.c. + +2019-11-26 Tom Tromey + * windows-nat.c (enum thread_disposition_type): Move to nat/windows-nat.h. (windows_nat::thread_rec): Rename from thread_rec. No longer diff --git a/gdb/gdbserver/ChangeLog b/gdb/gdbserver/ChangeLog index 4e703b8..fff6640 100644 --- a/gdb/gdbserver/ChangeLog +++ b/gdb/gdbserver/ChangeLog @@ -1,5 +1,10 @@ 2019-11-26 Tom Tromey + * win32-low.c (get_image_name): Remove. + (handle_load_dll): Update. + +2019-11-26 Tom Tromey + * win32-low.c (windows_nat::thread_rec): Rename from thread_rec. No longer static. Change parameters. (child_add_thread, child_fetch_inferior_registers) diff --git a/gdb/gdbserver/win32-low.c b/gdb/gdbserver/win32-low.c index c4e6cd2..a458b6e 100644 --- a/gdb/gdbserver/win32-low.c +++ b/gdb/gdbserver/win32-low.c @@ -1018,55 +1018,6 @@ loaded_dll (buf2, load_addr); } -static char * -get_image_name (HANDLE h, void *address, int unicode) -{ - static char buf[(2 * MAX_PATH) + 1]; - DWORD size = unicode ? sizeof (WCHAR) : sizeof (char); - char *address_ptr; - int len = 0; - char b[2]; - SIZE_T done; - - /* Attempt to read the name of the dll that was detected. - This is documented to work only when actively debugging - a program. It will not work for attached processes. */ - if (address == NULL) - return NULL; - -#ifdef _WIN32_WCE - /* Windows CE reports the address of the image name, - instead of an address of a pointer into the image name. */ - address_ptr = address; -#else - /* See if we could read the address of a string, and that the - address isn't null. */ - if (!ReadProcessMemory (h, address, &address_ptr, - sizeof (address_ptr), &done) - || done != sizeof (address_ptr) - || !address_ptr) - return NULL; -#endif - - /* Find the length of the string */ - while (ReadProcessMemory (h, address_ptr + len++ * size, &b, size, &done) - && (b[0] != 0 || b[size - 1] != 0) && done == size) - continue; - - if (!unicode) - ReadProcessMemory (h, address_ptr, buf, len, &done); - else - { - WCHAR *unicode_address = XALLOCAVEC (WCHAR, len); - ReadProcessMemory (h, address_ptr, unicode_address, len * sizeof (WCHAR), - &done); - - WideCharToMultiByte (CP_ACP, 0, unicode_address, len, buf, len, 0, 0); - } - - return buf; -} - typedef BOOL (WINAPI *winapi_EnumProcessModules) (HANDLE, HMODULE *, DWORD, LPDWORD); typedef BOOL (WINAPI *winapi_GetModuleInformation) (HANDLE, HMODULE, @@ -1175,7 +1126,7 @@ handle_load_dll (void) { LOAD_DLL_DEBUG_INFO *event = ¤t_event.u.LoadDll; - char *dll_name; + const char *dll_name; dll_name = get_image_name (current_process_handle, event->lpImageName, event->fUnicode); diff --git a/gdb/nat/windows-nat.c b/gdb/nat/windows-nat.c index 375ceb7..e0ebe3a 100644 --- a/gdb/nat/windows-nat.c +++ b/gdb/nat/windows-nat.c @@ -69,4 +69,61 @@ suspended = 0; } +const char * +get_image_name (HANDLE h, void *address, int unicode) +{ +#ifdef __CYGWIN__ + static char buf[MAX_PATH]; +#else + static char buf[(2 * MAX_PATH) + 1]; +#endif + DWORD size = unicode ? sizeof (WCHAR) : sizeof (char); + char *address_ptr; + int len = 0; + char b[2]; + SIZE_T done; + + /* Attempt to read the name of the dll that was detected. + This is documented to work only when actively debugging + a program. It will not work for attached processes. */ + if (address == NULL) + return NULL; + +#ifdef _WIN32_WCE + /* Windows CE reports the address of the image name, + instead of an address of a pointer into the image name. */ + address_ptr = address; +#else + /* See if we could read the address of a string, and that the + address isn't null. */ + if (!ReadProcessMemory (h, address, &address_ptr, + sizeof (address_ptr), &done) + || done != sizeof (address_ptr) + || !address_ptr) + return NULL; +#endif + + /* Find the length of the string. */ + while (ReadProcessMemory (h, address_ptr + len++ * size, &b, size, &done) + && (b[0] != 0 || b[size - 1] != 0) && done == size) + continue; + + if (!unicode) + ReadProcessMemory (h, address_ptr, buf, len, &done); + else + { + WCHAR *unicode_address = (WCHAR *) alloca (len * sizeof (WCHAR)); + ReadProcessMemory (h, address_ptr, unicode_address, len * sizeof (WCHAR), + &done); +#ifdef __CYGWIN__ + wcstombs (buf, unicode_address, MAX_PATH); +#else + WideCharToMultiByte (CP_ACP, 0, unicode_address, len, buf, sizeof buf, + 0, 0); +#endif + } + + return buf; +} + } diff --git a/gdb/nat/windows-nat.h b/gdb/nat/windows-nat.h index e6eebee..e38b69e 100644 --- a/gdb/nat/windows-nat.h +++ b/gdb/nat/windows-nat.h @@ -105,6 +105,13 @@ extern windows_thread_info *thread_rec (ptid_t ptid, thread_disposition_type disposition); +/* Return the name of the DLL referenced by H at ADDRESS. UNICODE + determines what sort of string is read from the inferior. Returns + the name of the DLL, or NULL on error. If a name is returned, it + is stored in a static buffer which is valid until the next call to + get_image_name. */ +extern const char *get_image_name (HANDLE h, void *address, int unicode); + } #endif diff --git a/gdb/windows-nat.c b/gdb/windows-nat.c index 935d682..1752010 100644 --- a/gdb/windows-nat.c +++ b/gdb/windows-nat.c @@ -822,56 +822,6 @@ return so; } -static char * -get_image_name (HANDLE h, void *address, int unicode) -{ -#ifdef __CYGWIN__ - static char buf[__PMAX]; -#else - static char buf[(2 * __PMAX) + 1]; -#endif - DWORD size = unicode ? sizeof (WCHAR) : sizeof (char); - char *address_ptr; - int len = 0; - char b[2]; - SIZE_T done; - - /* Attempt to read the name of the dll that was detected. - This is documented to work only when actively debugging - a program. It will not work for attached processes. */ - if (address == NULL) - return NULL; - - /* See if we could read the address of a string, and that the - address isn't null. */ - if (!ReadProcessMemory (h, address, &address_ptr, - sizeof (address_ptr), &done) - || done != sizeof (address_ptr) || !address_ptr) - return NULL; - - /* Find the length of the string. */ - while (ReadProcessMemory (h, address_ptr + len++ * size, &b, size, &done) - && (b[0] != 0 || b[size - 1] != 0) && done == size) - continue; - - if (!unicode) - ReadProcessMemory (h, address_ptr, buf, len, &done); - else - { - WCHAR *unicode_address = (WCHAR *) alloca (len * sizeof (WCHAR)); - ReadProcessMemory (h, address_ptr, unicode_address, len * sizeof (WCHAR), - &done); -#ifdef __CYGWIN__ - wcstombs (buf, unicode_address, __PMAX); -#else - WideCharToMultiByte (CP_ACP, 0, unicode_address, len, buf, sizeof buf, - 0, 0); -#endif - } - - return buf; -} - /* Handle a DLL load event, and return 1. This function assumes that this event did not occur during inferior @@ -883,7 +833,7 @@ handle_load_dll () { LOAD_DLL_DEBUG_INFO *event = ¤t_event.u.LoadDll; - char *dll_name; + const char *dll_name; /* Try getting the DLL name via the lpImageName field of the event. Note that Microsoft documents this fields as strictly optional,