- Update Dbgk test application to test Win32 as well (definable by a compile-time...
authorAlex Ionescu <aionescu@gmail.com>
Thu, 30 Nov 2006 19:02:22 +0000 (19:02 +0000)
committerAlex Ionescu <aionescu@gmail.com>
Thu, 30 Nov 2006 19:02:22 +0000 (19:02 +0000)
svn path=/trunk/; revision=24999

reactos/ntoskrnl/tests/TestDebug.c

index ad83f9f..8da141c 100644 (file)
@@ -1,16 +1,30 @@
+#define NATIVE 0\r
+\r
+#if NATIVE\r
 #define _X86_\r
 #include "ntndk.h"\r
+#else\r
+#include "stdio.h"\r
+#include "windows.h"\r
+#endif\r
 \r
 VOID\r
 Main(VOID)\r
 {\r
+#if NATIVE\r
     NTSTATUS Status;\r
-    HANDLE hProcess;\r
     OBJECT_ATTRIBUTES ObjectAttributes;\r
     CLIENT_ID ClientId;\r
     DBGUI_WAIT_STATE_CHANGE State;\r
+#else\r
+    DWORD Error, BytesRead;\r
+    DEBUG_EVENT DebugEvent;\r
+    WCHAR ImageName[MAX_PATH];\r
+#endif\r
+    HANDLE hProcess;\r
     BOOLEAN Alive = TRUE;\r
 \r
+#if NATIVE\r
     printf("*** Native (DbgUi) Debugging Test Application\n");\r
     printf("Press any key to connect to Dbgk...");\r
     getchar();\r
@@ -19,8 +33,13 @@ Main(VOID)
     printf(" Connection Established. Status: %lx\n", Status);\r
     printf("Debug Object Handle: %lx\n", NtCurrentTeb()->DbgSsReserved[1]);\r
     printf("Press any key to debug services.exe...");\r
+#else\r
+    printf("*** Win32 (Debug) Debugging Test Application\n");\r
+    printf("Press any key to debug services.exe...");\r
+#endif\r
     getchar();\r
 \r
+#if NATIVE\r
     InitializeObjectAttributes(&ObjectAttributes, NULL, 0, 0, 0);\r
     ClientId.UniqueThread = 0;\r
     ClientId.UniqueProcess = UlongToHandle(168);\r
@@ -29,20 +48,41 @@ Main(VOID)
                            &ObjectAttributes,\r
                            &ClientId);\r
     Status = DbgUiDebugActiveProcess(hProcess);\r
+#else\r
+    Error = DebugActiveProcess(2648);\r
+#endif\r
+\r
+#if NATIVE\r
     printf(" Debugger Attached. Status: %lx\n", Status);\r
+#else\r
+    printf(" Debugger Attached. Error: %lx\n", Error);\r
+#endif\r
     printf("Press any key to get first debug event... ");\r
     getchar();\r
 \r
     while (Alive)\r
     {\r
+#if NATIVE\r
         Status = DbgUiWaitStateChange(&State, NULL);\r
         printf(" Event Received. Status: %lx\n", Status);\r
         printf("New State: %lx. Application Client ID: %lx/%lx\n",\r
                State.NewState,\r
                State.AppClientId.UniqueProcess, State.AppClientId.UniqueThread);\r
+#else\r
+        Error = WaitForDebugEvent(&DebugEvent, -1);\r
+        printf(" Event Received. Error: %lx\n", Error);\r
+        printf("New State: %lx. Application Client ID: %lx/%lx\n",\r
+               DebugEvent.dwDebugEventCode,\r
+               DebugEvent.dwProcessId, DebugEvent.dwThreadId);\r
+#endif\r
 \r
+#if NATIVE\r
         switch (State.NewState)\r
+#else\r
+        switch (DebugEvent.dwDebugEventCode)\r
+#endif\r
         {\r
+#if NATIVE\r
             case DbgCreateProcessStateChange:\r
                 printf("Process Handle: %lx. Thread Handle: %lx\n",\r
                     State.StateInfo.CreateProcessInfo.HandleToProcess,\r
@@ -51,40 +91,98 @@ Main(VOID)
                     State.StateInfo.CreateProcessInfo.NewProcess.FileHandle);\r
                 printf("Process image base: %lx\n",\r
                     State.StateInfo.CreateProcessInfo.NewProcess.BaseOfImage);\r
+#else\r
+            case CREATE_PROCESS_DEBUG_EVENT:\r
+                printf("Process Handle: %lx. Thread Handle: %lx\n",\r
+                        DebugEvent.u.CreateProcessInfo.hProcess,\r
+                        DebugEvent.u.CreateProcessInfo.hThread);\r
+                printf("Process image handle: %lx\n",\r
+                        DebugEvent.u.CreateProcessInfo.hFile);\r
+                printf("Process image base: %lx\n",\r
+                        DebugEvent.u.CreateProcessInfo.lpBaseOfImage);\r
+                hProcess = DebugEvent.u.CreateProcessInfo.hProcess;\r
+#endif\r
                 break;\r
 \r
+#if NATIVE\r
             case DbgCreateThreadStateChange:\r
                 printf("New thread: %lx\n", State.StateInfo.CreateThread.HandleToThread);\r
                 printf("Thread Start Address: %p\n", State.StateInfo.CreateThread.NewThread.StartAddress);\r
+#else\r
+            case CREATE_THREAD_DEBUG_EVENT:\r
+                printf("New thread: %lx\n", DebugEvent.u.CreateThread.hThread);\r
+                printf("Thread Start Address: %p\n",\r
+                        DebugEvent.u.CreateThread.lpStartAddress);\r
+#endif\r
                 break;\r
 \r
+#if NATIVE\r
             case DbgLoadDllStateChange:\r
                 printf("New DLL: %lx\n", State.StateInfo.LoadDll.FileHandle);\r
                 printf("DLL LoadAddress: %p\n", State.StateInfo.LoadDll.BaseOfDll);\r
+#else\r
+            case LOAD_DLL_DEBUG_EVENT:\r
+                printf("New DLL: %lx\n", DebugEvent.u.LoadDll.hFile);\r
+                printf("DLL LoadAddress: %p\n", DebugEvent.u.LoadDll.lpBaseOfDll);\r
+                Error = ReadProcessMemory(hProcess,\r
+                                          DebugEvent.u.LoadDll.lpImageName,\r
+                                          &DebugEvent.u.LoadDll.lpImageName,\r
+                                          sizeof(DebugEvent.u.LoadDll.lpImageName),\r
+                                          &BytesRead);\r
+                if (DebugEvent.u.LoadDll.lpImageName)\r
+                {\r
+                    Error = ReadProcessMemory(hProcess,\r
+                                              DebugEvent.u.LoadDll.lpImageName,\r
+                                              ImageName,\r
+                                              sizeof(ImageName),\r
+                                              &BytesRead);\r
+                    printf("DLL Name: %S\n", ImageName);\r
+                }\r
+#endif\r
                 break;\r
 \r
+#if NATIVE\r
             case DbgBreakpointStateChange:\r
                 printf("Initial breakpoint hit at: %p!\n",\r
                        State.StateInfo.Exception.ExceptionRecord.ExceptionAddress);\r
+#else\r
+\r
+#endif\r
                 break;\r
 \r
+#if NATIVE\r
             case DbgExitThreadStateChange:\r
                 printf("Thread exited: %lx\n", State.StateInfo.ExitThread.ExitStatus);\r
+#else\r
+\r
+#endif\r
                 break;\r
 \r
+#if NATIVE\r
             case DbgExitProcessStateChange:\r
                 printf("Process exited: %lx\n", State.StateInfo.ExitProcess.ExitStatus);\r
                 Alive = FALSE;\r
+#else\r
+\r
+#endif\r
                 break;\r
         }\r
 \r
         printf("Press any key to continue debuggee...");\r
         getchar();\r
 \r
+#if NATIVE\r
         ClientId.UniqueProcess = State.AppClientId.UniqueProcess;\r
         ClientId.UniqueThread = State.AppClientId.UniqueThread;\r
         Status = DbgUiContinue(&ClientId, DBG_CONTINUE);\r
         printf(" Debuggee Resumed. Status: %lx\n", Status);\r
+#else\r
+        Error = ContinueDebugEvent(DebugEvent.dwProcessId,\r
+                                   DebugEvent.dwThreadId,\r
+                                   DBG_CONTINUE);\r
+        printf(" Debuggee Resumed. Error: %lx\n", Error);\r
+#endif\r
+\r
         printf("Press any key to get next debug event... ");\r
         getchar();\r
     };\r