[ntoskrnl]
[reactos.git] / reactos / ntoskrnl / dbgk / dbgkutil.c
index 2145219..0d5d571 100644 (file)
@@ -59,7 +59,7 @@ DbgkpSuspendProcess(VOID)
     PAGED_CODE();
 
     /* Make sure this isn't a deleted process */
-    if (PsGetCurrentProcess()->ProcessDelete)
+    if (!PsGetCurrentProcess()->ProcessDelete)
     {
         /* Freeze all the threads */
         KeFreezeAllThreads();
@@ -84,9 +84,9 @@ DbgkpResumeProcess(VOID)
 
 VOID
 NTAPI
-DbgkCreateThread(PVOID StartAddress)
+DbgkCreateThread(IN PETHREAD Thread,
+                 IN PVOID StartAddress)
 {
-    PETHREAD Thread = PsGetCurrentThread();
     PEPROCESS Process = PsGetCurrentProcess();
     ULONG ProcessFlags;
     IMAGE_INFO ImageInfo;
@@ -104,10 +104,15 @@ DbgkCreateThread(PVOID StartAddress)
     PTEB Teb;
     PAGED_CODE();
 
-    /* Check if this process has already been notified */
-    ProcessFlags = InterlockedAnd((PLONG)&Process->Flags,
-                                  PSF_CREATE_REPORTED_BIT |
-                                  PSF_IMAGE_NOTIFY_DONE_BIT);
+    /* Sanity check */
+    ASSERT(Thread == PsGetCurrentThread());
+
+    /* Try ORing in the create reported and image notify flags */
+    ProcessFlags = PspSetProcessFlag(Process,
+                                     PSF_CREATE_REPORTED_BIT |
+                                     PSF_IMAGE_NOTIFY_DONE_BIT);
+
+    /* Check if we were the first to set them or if another thread raced us */
     if (!(ProcessFlags & PSF_IMAGE_NOTIFY_DONE_BIT) && (PsImageNotifyEnabled))
     {
         /* It hasn't.. set up the image info for the process */
@@ -176,8 +181,8 @@ DbgkCreateThread(PVOID StartAddress)
     if (!(ProcessFlags & PSF_CREATE_REPORTED_BIT))
     {
         /* Setup the information structure for the new thread */
-        CreateThread->SubSystemKey = 0;
-        CreateThread->StartAddress = NULL;
+        CreateProcess->InitialThread.SubSystemKey = 0;
+        CreateProcess->InitialThread.StartAddress = NULL;
 
         /* And for the new process */
         CreateProcess->SubSystemKey = 0;
@@ -192,10 +197,9 @@ DbgkCreateThread(PVOID StartAddress)
         if (NtHeader)
         {
             /* Fill out data from the header */
-            CreateThread->StartAddress = (PVOID)((ULONG_PTR)NtHeader->
-                                                 OptionalHeader.ImageBase +
-                                                 NtHeader->OptionalHeader.
-                                                 AddressOfEntryPoint);
+            CreateProcess->InitialThread.StartAddress =
+                (PVOID)((ULONG_PTR)NtHeader->OptionalHeader.ImageBase +
+                        NtHeader->OptionalHeader.AddressOfEntryPoint);
             CreateProcess->DebugInfoFileOffset = NtHeader->FileHeader.
                                                  PointerToSymbolTable;
             CreateProcess->DebugInfoSize = NtHeader->FileHeader.
@@ -205,7 +209,8 @@ DbgkCreateThread(PVOID StartAddress)
         /* Setup the API Message */
         ApiMessage.h.u1.Length = sizeof(DBGKM_MSG) << 16 |
                                  (8 + sizeof(DBGKM_CREATE_PROCESS));
-        ApiMessage.h.u2.ZeroInit = LPC_DEBUG_EVENT;
+        ApiMessage.h.u2.ZeroInit = 0;
+        ApiMessage.h.u2.s2.Type = LPC_DEBUG_EVENT;
         ApiMessage.ApiNumber = DbgKmCreateProcessApi;
 
         /* Send the message */
@@ -237,11 +242,11 @@ DbgkCreateThread(PVOID StartAddress)
             /* Copy the system library name and link to it */
             wcsncpy(Teb->StaticUnicodeBuffer,
                     L"ntdll.dll",
-                    sizeof(Teb->StaticUnicodeBuffer));
+                    sizeof(Teb->StaticUnicodeBuffer) / sizeof(WCHAR));
             Teb->Tib.ArbitraryUserPointer = Teb->StaticUnicodeBuffer;
 
             /* Return it in the debug event as well */
-            LoadDll->NamePointer = Teb->Tib.ArbitraryUserPointer;
+            LoadDll->NamePointer = &Teb->Tib.ArbitraryUserPointer;
         }
 
         /* Get a handle */
@@ -265,7 +270,8 @@ DbgkCreateThread(PVOID StartAddress)
             /* Setup the API Message */
             ApiMessage.h.u1.Length = sizeof(DBGKM_MSG) << 16 |
                                      (8 + sizeof(DBGKM_LOAD_DLL));
-            ApiMessage.h.u2.ZeroInit = LPC_DEBUG_EVENT;
+            ApiMessage.h.u2.ZeroInit = 0;
+            ApiMessage.h.u2.s2.Type = LPC_DEBUG_EVENT;
             ApiMessage.ApiNumber = DbgKmLoadDllApi;
 
             /* Send the message */
@@ -279,12 +285,13 @@ DbgkCreateThread(PVOID StartAddress)
     {
         /* Otherwise, do it just for the thread */
         CreateThread->SubSystemKey = 0;
-        CreateThread->StartAddress = NULL;
+        CreateThread->StartAddress = StartAddress;
 
         /* Setup the API Message */
         ApiMessage.h.u1.Length = sizeof(DBGKM_MSG) << 16 |
                                  (8 + sizeof(DBGKM_CREATE_THREAD));
-        ApiMessage.h.u2.ZeroInit = LPC_DEBUG_EVENT;
+        ApiMessage.h.u2.ZeroInit = 0;
+        ApiMessage.h.u2.s2.Type = LPC_DEBUG_EVENT;
         ApiMessage.ApiNumber = DbgKmCreateThreadApi;
 
         /* Send the message */
@@ -317,7 +324,8 @@ DbgkExitProcess(IN NTSTATUS ExitStatus)
     /* Setup the API Message */
     ApiMessage.h.u1.Length = sizeof(DBGKM_MSG) << 16 |
                              (8 + sizeof(DBGKM_EXIT_PROCESS));
-    ApiMessage.h.u2.ZeroInit = LPC_DEBUG_EVENT;
+    ApiMessage.h.u2.ZeroInit = 0;
+    ApiMessage.h.u2.s2.Type = LPC_DEBUG_EVENT;
     ApiMessage.ApiNumber = DbgKmExitProcessApi;
 
     /* Set the current exit time */
@@ -353,7 +361,8 @@ DbgkExitThread(IN NTSTATUS ExitStatus)
     /* Setup the API Message */
     ApiMessage.h.u1.Length = sizeof(DBGKM_MSG) << 16 |
                              (8 + sizeof(DBGKM_EXIT_THREAD));
-    ApiMessage.h.u2.ZeroInit = LPC_DEBUG_EVENT;
+    ApiMessage.h.u2.ZeroInit = 0;
+    ApiMessage.h.u2.s2.Type = LPC_DEBUG_EVENT;
     ApiMessage.ApiNumber = DbgKmExitThreadApi;
 
     /* Suspend the process */
@@ -368,7 +377,7 @@ DbgkExitThread(IN NTSTATUS ExitStatus)
 
 VOID
 NTAPI
-DbgkMapViewOfSection(IN HANDLE SectionHandle,
+DbgkMapViewOfSection(IN PVOID Section,
                      IN PVOID BaseAddress,
                      IN ULONG SectionOffset,
                      IN ULONG_PTR ViewSize)
@@ -379,19 +388,20 @@ DbgkMapViewOfSection(IN HANDLE SectionHandle,
     PETHREAD Thread = PsGetCurrentThread();
     PIMAGE_NT_HEADERS NtHeader;
     PAGED_CODE();
+    DBGKTRACE(DBGK_PROCESS_DEBUG,
+              "Section: %p. Base: %p\n", Section, BaseAddress);
 
-    /* Check if this thread is hidden, doesn't have a debug port, or died */
-    if ((Thread->HideFromDebugger) ||
-        !(Process->DebugPort) ||
-        (Thread->DeadThread) ||
-        (KeGetPreviousMode() == KernelMode))
+    /* Check if this thread is kernel, hidden or doesn't have a debug port */
+    if ((ExGetPreviousMode() == KernelMode) ||
+        (Thread->HideFromDebugger) ||
+        !(Process->DebugPort))
     {
         /* Don't notify the debugger */
         return;
     }
 
     /* Setup the parameters */
-    LoadDll->FileHandle = DbgkpSectionToFileHandle(SectionHandle);
+    LoadDll->FileHandle = DbgkpSectionToFileHandle(Section);
     LoadDll->BaseOfDll = BaseAddress;
     LoadDll->DebugInfoFileOffset = 0;
     LoadDll->DebugInfoSize = 0;
@@ -409,7 +419,8 @@ DbgkMapViewOfSection(IN HANDLE SectionHandle,
     /* Setup the API Message */
     ApiMessage.h.u1.Length = sizeof(DBGKM_MSG) << 16 |
                              (8 + sizeof(DBGKM_LOAD_DLL));
-    ApiMessage.h.u2.ZeroInit = LPC_DEBUG_EVENT;
+    ApiMessage.h.u2.ZeroInit = 0;
+    ApiMessage.h.u2.s2.Type = LPC_DEBUG_EVENT;
     ApiMessage.ApiNumber = DbgKmLoadDllApi;
 
     /* Send the message */
@@ -429,11 +440,10 @@ DbgkUnMapViewOfSection(IN PVOID BaseAddress)
     PETHREAD Thread = PsGetCurrentThread();
     PAGED_CODE();
 
-    /* Check if this thread is hidden, doesn't have a debug port, or died */
-    if ((Thread->HideFromDebugger) ||
-        !(Process->DebugPort) ||
-        (Thread->DeadThread) ||
-        (KeGetPreviousMode() == KernelMode))
+    /* Check if this thread is kernel, hidden or doesn't have a debug port */
+    if ((ExGetPreviousMode() == KernelMode) ||
+        (Thread->HideFromDebugger) ||
+        !(Process->DebugPort))
     {
         /* Don't notify the debugger */
         return;
@@ -445,11 +455,10 @@ DbgkUnMapViewOfSection(IN PVOID BaseAddress)
     /* Setup the API Message */
     ApiMessage.h.u1.Length = sizeof(DBGKM_MSG) << 16 |
                              (8 + sizeof(DBGKM_UNLOAD_DLL));
-    ApiMessage.h.u2.ZeroInit = LPC_DEBUG_EVENT;
+    ApiMessage.h.u2.ZeroInit = 0;
+    ApiMessage.h.u2.s2.Type = LPC_DEBUG_EVENT;
     ApiMessage.ApiNumber = DbgKmUnloadDllApi;
 
     /* Send the message */
     DbgkpSendApiMessage(&ApiMessage, TRUE);
 }
-
-/* EOF */