[ntoskrnl]
[reactos.git] / reactos / ntoskrnl / dbgk / dbgkutil.c
index 9a6b91d..0d5d571 100644 (file)
@@ -19,7 +19,7 @@ NTAPI
 DbgkpSectionToFileHandle(IN PVOID Section)
 {
     NTSTATUS Status;
-    UNICODE_STRING FileName;
+    POBJECT_NAME_INFORMATION FileName;
     OBJECT_ATTRIBUTES ObjectAttributes;
     IO_STATUS_BLOCK IoStatusBlock;
     HANDLE Handle;
@@ -31,7 +31,7 @@ DbgkpSectionToFileHandle(IN PVOID Section)
 
     /* Initialize object attributes */
     InitializeObjectAttributes(&ObjectAttributes,
-                               &FileName,
+                               &FileName->Name,
                                OBJ_CASE_INSENSITIVE |
                                OBJ_FORCE_ACCESS_CHECK |
                                OBJ_KERNEL_HANDLE,
@@ -47,7 +47,7 @@ DbgkpSectionToFileHandle(IN PVOID Section)
                         FILE_SYNCHRONOUS_IO_NONALERT);
 
     /* Free the name and return the handle if we succeeded */
-    ExFreePool(FileName.Buffer);
+    ExFreePool(FileName);
     if (!NT_SUCCESS(Status)) return NULL;
     return Handle;
 }
@@ -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,14 +84,15 @@ 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;
     PIMAGE_NT_HEADERS NtHeader;
-    UNICODE_STRING ModuleName;
+    POBJECT_NAME_INFORMATION ModuleName;
+    UNICODE_STRING NtDllName;
     NTSTATUS Status;
     PVOID DebugPort;
     DBGKM_MSG ApiMessage;
@@ -103,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 */
@@ -130,10 +136,10 @@ DbgkCreateThread(PVOID StartAddress)
         if (NT_SUCCESS(Status))
         {
             /* Call the notify routines and free the name */
-            PspRunLoadImageNotifyRoutines(&ModuleName,
+            PspRunLoadImageNotifyRoutines(&ModuleName->Name,
                                           Process->UniqueProcessId,
                                           &ImageInfo);
-            ExFreePool(ModuleName.Buffer);
+            ExFreePool(ModuleName);
         }
         else
         {
@@ -160,9 +166,9 @@ DbgkCreateThread(PVOID StartAddress)
         }
 
         /* Call the notify routines */
-        RtlInitUnicodeString(&ModuleName,
+        RtlInitUnicodeString(&NtDllName,
                              L"\\SystemRoot\\System32\\ntdll.dll");
-        PspRunLoadImageNotifyRoutines(&ModuleName,
+        PspRunLoadImageNotifyRoutines(&NtDllName,
                                       Process->UniqueProcessId,
                                       &ImageInfo);
     }
@@ -175,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;
@@ -191,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.
@@ -204,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 */
@@ -236,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 */
@@ -264,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 */
@@ -278,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 */
@@ -316,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 */
@@ -352,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 */
@@ -367,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)
@@ -378,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;
@@ -408,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 */
@@ -428,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;
@@ -444,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 */