[2/2] Don't close process handle provided by WaitForDebugEvent

Message ID 20200524222448.11955-2-ssbssa@yahoo.de
State New
Headers show
Series
  • [1/2] Don't close thread handles provided by WaitForDebugEvent
Related show

Commit Message

Kevin Buettner via Gdb-patches May 24, 2020, 10:24 p.m.
Only the process handle returned by OpenProcess or CreateProcess needs to
be closed, the one provided by WaitForDebugEvent is closed automatically.

gdbserver/ChangeLog:

2020-05-25  Hannes Domani  <ssbssa@yahoo.de>

	* win32-low.cc (do_initial_child_stuff): Set open_process_used.
	(win32_clear_inferiors): Use open_process_used.
	(get_child_debug_event): Likewise.
---
 gdbserver/win32-low.cc | 19 +++++++++++++++----
 1 file changed, 15 insertions(+), 4 deletions(-)

-- 
2.26.2

Comments

Tom Tromey May 26, 2020, 8:30 p.m. | #1
>>>>> "Hannes" == Hannes Domani via Gdb-patches <gdb-patches@sourceware.org> writes:


Hannes> +/* True if current_process_handle needs to be closed.  */
Hannes> +bool open_process_used = false;

This should probably be "static".

The patch is ok with this changed.

Perhaps a bit more of this code can be merged between gdb and gdbserver?
I didn't look too deeply.

Tom
Kevin Buettner via Gdb-patches May 27, 2020, 5:47 p.m. | #2
Am Dienstag, 26. Mai 2020, 22:30:58 MESZ hat Tom Tromey <tom@tromey.com> Folgendes geschrieben:

> >>>>> "Hannes" == Hannes Domani via Gdb-patches <gdb-patches@sourceware.org> writes:

>

> Hannes> +/* True if current_process_handle needs to be closed.  */

> Hannes> +bool open_process_used = false;

>

> This should probably be "static".

>

> The patch is ok with this changed.


Pushed with this change, and also the thread one.


> Perhaps a bit more of this code can be merged between gdb and gdbserver?

> I didn't look too deeply.


I plan to look into this once I don't have so many open patches any more.


Thanks,
Hannes

Patch

diff --git a/gdbserver/win32-low.cc b/gdbserver/win32-low.cc
index 4eb63b7ca2..e440e28913 100644
--- a/gdbserver/win32-low.cc
+++ b/gdbserver/win32-low.cc
@@ -88,6 +88,9 @@  static int soft_interrupt_requested = 0;
    by suspending all the threads.  */
 static int faked_breakpoint = 0;
 
+/* True if current_process_handle needs to be closed.  */
+bool open_process_used = false;
+
 #ifdef __x86_64__
 bool wow64_process = false;
 #endif
@@ -383,6 +386,7 @@  do_initial_child_stuff (HANDLE proch, DWORD pid, int attached)
 
   soft_interrupt_requested = 0;
   faked_breakpoint = 0;
+  open_process_used = true;
 
   memset (&current_event, 0, sizeof (current_event));
 
@@ -859,8 +863,11 @@  windows_nat::handle_output_debug_string (struct target_waitstatus *ourstatus)
 static void
 win32_clear_inferiors (void)
 {
-  if (current_process_handle != NULL)
-    CloseHandle (current_process_handle);
+  if (open_process_used)
+    {
+      CloseHandle (current_process_handle);
+      open_process_used = false;
+    }
 
   for_each_thread (delete_thread_info);
   siginfo_er.ExceptionCode = 0;
@@ -1513,6 +1520,12 @@  get_child_debug_event (DWORD *continue_status,
 		(unsigned) current_event.dwThreadId));
       CloseHandle (current_event.u.CreateProcessInfo.hFile);
 
+      if (open_process_used)
+	{
+	  CloseHandle (current_process_handle);
+	  open_process_used = false;
+	}
+
       current_process_handle = current_event.u.CreateProcessInfo.hProcess;
       main_thread_id = current_event.dwThreadId;
 
@@ -1560,8 +1573,6 @@  get_child_debug_event (DWORD *continue_status,
 	  }
       }
       child_continue (DBG_CONTINUE, desired_stop_thread_id);
-      CloseHandle (current_process_handle);
-      current_process_handle = NULL;
       break;
 
     case LOAD_DLL_DEBUG_EVENT: