This is the mail archive of the gdb-patches@sourceware.org mailing list for the GDB project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[RFA] gdb/win32-nat.c: do not call CloseHandle on process and thread handles


  Following Christopher and Pedro's comments,
I added a new variable called fake_create_used
to record if the current_process_handle is
coming from OpenProcess called in fake_create_process,
in which case we should close the handle, or
coming from the CREATE_PROCESS_DEBUG_EVENT,
which is closed by the system.

Pierre

ChangeLog entry:

2007-11-23  Pierre Muller  <muller@ics.u-strasbg.fr>

	*win32-nat.c (fake_create_used): New static variable.
	(win32_init_thread_list): Remove call to CloseHandle for thread.
	(win32_delete_thread): Ditto.
	(fake_create_process): Set fake_create_used if OpenProcess call
	is successful.
	(get_win32_debug_event): Do not close process handle.
	(do_initial_win32_stuff): Set fake_create_used to zero.
	(win32_mourn_inferior): Call CloseHandle for current_process_handle
	if fake_create_process is set.
	(win32_kill_inferior): Do not close process and main_thread handles.


Index: gdb/win32-nat.c
===================================================================
RCS file: /cvs/src/src/gdb/win32-nat.c,v
retrieving revision 1.139
diff -u -p -r1.139 win32-nat.c
--- gdb/win32-nat.c     16 Nov 2007 04:53:46 -0000      1.139
+++ gdb/win32-nat.c     23 Nov 2007 13:30:39 -0000
@@ -140,6 +140,7 @@ static DWORD main_thread_id;                /* Thread
 static int exception_count = 0;
 static int event_count = 0;
 static int saw_create;
+static int fake_create_used = 0;

 /* User options. */
 static int new_console = 0;
@@ -316,7 +317,6 @@ win32_init_thread_list (void)
     {
       thread_info *here = th->next;
       th->next = here->next;
-      (void) CloseHandle (here->h);
       xfree (here);
     }
   thread_head.next = NULL;
@@ -341,7 +341,6 @@ win32_delete_thread (DWORD id)
     {
       thread_info *here = th->next;
       th->next = here->next;
-      CloseHandle (here->h);
       xfree (here);
     }
 }
@@ -1160,6 +1159,17 @@ fake_create_process (void)
 {
   current_process_handle = OpenProcess (PROCESS_ALL_ACCESS, FALSE,
                                        current_event.dwProcessId);
+  if (current_process_handle != NULL)
+    {
+      fake_create_used = 1;
+    }
+  else
+    {
+      printf_unfiltered ("OpenProcess call failed, GetLastError = %lud\n",
+       GetLastError ());
+      /*  We can not debug anything in that case.  */
+      return 0;
+    }
   main_thread_id = current_event.dwThreadId;
   current_thread = win32_add_thread (main_thread_id,
                                     current_event.u.CreateThread.hThread);
@@ -1289,7 +1299,8 @@ get_win32_debug_event (int pid, struct t
                 thread event.  Caused when attached process does not have
                 a main thread. */
              retval = ourstatus->value.related_pid = fake_create_process
();
-             saw_create++;
+             if (retval)
+               saw_create++;
            }
          break;
        }
@@ -1323,7 +1334,6 @@ get_win32_debug_event (int pid, struct t
       CloseHandle (current_event.u.CreateProcessInfo.hFile);
       if (++saw_create != 1)
        {
-         CloseHandle (current_event.u.CreateProcessInfo.hProcess);
          break;
        }

@@ -1346,7 +1356,6 @@ get_win32_debug_event (int pid, struct t
        break;
       ourstatus->kind = TARGET_WAITKIND_EXITED;
       ourstatus->value.integer = current_event.u.ExitProcess.dwExitCode;
-      CloseHandle (current_process_handle);
       retval = main_thread_id;
       break;

@@ -1478,6 +1487,7 @@ do_initial_win32_stuff (DWORD pid)
   last_sig = TARGET_SIGNAL_0;
   event_count = 0;
   exception_count = 0;
+  fake_create_used = 0;
   debug_registers_changed = 0;
   debug_registers_used = 0;
   for (i = 0; i < sizeof (dr) / sizeof (dr[0]); i++)
@@ -1876,9 +1886,6 @@ win32_create_inferior (char *exec_file,
     error (_("Error creating process %s, (error %d)."),
           exec_file, (unsigned) GetLastError ());

-  CloseHandle (pi.hThread);
-  CloseHandle (pi.hProcess);
-
   if (useshell && shell[0] != '\0')
     saw_create = -1;
   else
@@ -1894,6 +1901,11 @@ win32_mourn_inferior (void)
 {
   (void) win32_continue (DBG_CONTINUE, -1);
   i386_cleanup_dregs();
+  if (fake_create_used)
+    {
+      CHECK (CloseHandle (current_process_handle));
+      fake_create_used = 0;
+    }
   unpush_target (&win32_ops);
   generic_mourn_inferior ();
 }
@@ -1950,11 +1962,6 @@ win32_kill_inferior (void)
        break;
     }

-  CHECK (CloseHandle (current_process_handle));
-
-  /* this may fail in an attached process so don't check. */
-  if (current_thread && current_thread->h)
-    (void) CloseHandle (current_thread->h);
   target_mourn_inferior ();    /* or just win32_mourn_inferior? */
 }



Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]