ReactOS  0.4.12-dev-918-g6c6e7b8
TestDebug.c
Go to the documentation of this file.
1 #define NATIVE 0
2 
3 #if NATIVE
4 #define _X86_
5 #include "ntndk.h"
6 #else
7 #include "stdio.h"
8 #include "windows.h"
9 #endif
10 
11 VOID
13 {
14 #if NATIVE
19 #else
23 #endif
25  BOOLEAN Alive = TRUE;
26 
27 #if NATIVE
28  printf("*** Native (DbgUi) Debugging Test Application\n");
29  printf("Press any key to connect to Dbgk...");
30  getchar();
31 
33  printf(" Connection Established. Status: %lx\n", Status);
34  printf("Debug Object Handle: %lx\n", NtCurrentTeb()->DbgSsReserved[1]);
35  printf("Press any key to debug services.exe...");
36 #else
37  printf("*** Win32 (Debug) Debugging Test Application\n");
38  printf("Press any key to debug services.exe...");
39 #endif
40  getchar();
41 
42 #if NATIVE
49  &ClientId);
51 #else
53 #endif
54 
55 #if NATIVE
56  printf(" Debugger Attached. Status: %lx\n", Status);
57 #else
58  printf(" Debugger Attached. Error: %lx\n", Error);
59 #endif
60  printf("Press any key to get first debug event... ");
61  getchar();
62 
63  while (Alive)
64  {
65 #if NATIVE
67  printf(" Event Received. Status: %lx\n", Status);
68  printf("New State: %lx. Application Client ID: %lx/%lx\n",
69  State.NewState,
70  State.AppClientId.UniqueProcess, State.AppClientId.UniqueThread);
71 #else
73  printf(" Event Received. Error: %lx\n", Error);
74  printf("New State: %lx. Application Client ID: %lx/%lx\n",
75  DebugEvent.dwDebugEventCode,
76  DebugEvent.dwProcessId, DebugEvent.dwThreadId);
77 #endif
78 
79 #if NATIVE
80  switch (State.NewState)
81 #else
82  switch (DebugEvent.dwDebugEventCode)
83 #endif
84  {
85 #if NATIVE
87  printf("Process Handle: %lx. Thread Handle: %lx\n",
88  State.StateInfo.CreateProcessInfo.HandleToProcess,
89  State.StateInfo.CreateProcessInfo.HandleToThread);
90  printf("Process image handle: %lx\n",
91  State.StateInfo.CreateProcessInfo.NewProcess.FileHandle);
92  printf("Process image base: %lx\n",
93  State.StateInfo.CreateProcessInfo.NewProcess.BaseOfImage);
94 #else
96  printf("Process Handle: %lx. Thread Handle: %lx\n",
97  DebugEvent.u.CreateProcessInfo.hProcess,
98  DebugEvent.u.CreateProcessInfo.hThread);
99  printf("Process image handle: %lx\n",
100  DebugEvent.u.CreateProcessInfo.hFile);
101  printf("Process image base: %lx\n",
102  DebugEvent.u.CreateProcessInfo.lpBaseOfImage);
103  hProcess = DebugEvent.u.CreateProcessInfo.hProcess;
104 #endif
105  break;
106 
107 #if NATIVE
109  printf("New thread: %lx\n", State.StateInfo.CreateThread.HandleToThread);
110  printf("Thread Start Address: %p\n", State.StateInfo.CreateThread.NewThread.StartAddress);
111 #else
113  printf("New thread: %lx\n", DebugEvent.u.CreateThread.hThread);
114  printf("Thread Start Address: %p\n",
115  DebugEvent.u.CreateThread.lpStartAddress);
116 #endif
117  break;
118 
119 #if NATIVE
121  printf("New DLL: %lx\n", State.StateInfo.LoadDll.FileHandle);
122  printf("DLL LoadAddress: %p\n", State.StateInfo.LoadDll.BaseOfDll);
123 #else
125  printf("New DLL: %lx\n", DebugEvent.u.LoadDll.hFile);
126  printf("DLL LoadAddress: %p\n", DebugEvent.u.LoadDll.lpBaseOfDll);
128  DebugEvent.u.LoadDll.lpImageName,
129  &DebugEvent.u.LoadDll.lpImageName,
130  sizeof(DebugEvent.u.LoadDll.lpImageName),
131  &BytesRead);
132  if (DebugEvent.u.LoadDll.lpImageName)
133  {
135  DebugEvent.u.LoadDll.lpImageName,
136  ImageName,
137  sizeof(ImageName),
138  &BytesRead);
139  printf("DLL Name: %S\n", ImageName);
140  }
141 #endif
142  break;
143 
144 #if NATIVE
146  printf("Initial breakpoint hit at: %p!\n",
147  State.StateInfo.Exception.ExceptionRecord.ExceptionAddress);
148 #else
149 
150 #endif
151  break;
152 
153 #if NATIVE
155  printf("Thread exited: %lx\n", State.StateInfo.ExitThread.ExitStatus);
156 #else
157 
158 #endif
159  break;
160 
161 #if NATIVE
163  printf("Process exited: %lx\n", State.StateInfo.ExitProcess.ExitStatus);
164  Alive = FALSE;
165 #else
166 
167 #endif
168  break;
169  }
170 
171  printf("Press any key to continue debuggee...");
172  getchar();
173 
174 #if NATIVE
175  ClientId.UniqueProcess = State.AppClientId.UniqueProcess;
176  ClientId.UniqueThread = State.AppClientId.UniqueThread;
178  printf(" Debuggee Resumed. Status: %lx\n", Status);
179 #else
180  Error = ContinueDebugEvent(DebugEvent.dwProcessId,
181  DebugEvent.dwThreadId,
182  DBG_CONTINUE);
183  printf(" Debuggee Resumed. Error: %lx\n", Error);
184 #endif
185 
186  printf("Press any key to get next debug event... ");
187  getchar();
188  };
189  printf("*** End of test\n");
190  getchar();
191 }
IN PUNICODE_STRING IN POBJECT_ATTRIBUTES ObjectAttributes
Definition: conport.c:35
NTSTATUS NTAPI DbgUiConnectToDbg(VOID)
Definition: dbgui.c:25
#define TRUE
Definition: types.h:120
#define PROCESS_ALL_ACCESS
Definition: nt_native.h:1324
LONG NTSTATUS
Definition: precomp.h:26
BOOL WINAPI ContinueDebugEvent(IN DWORD dwProcessId, IN DWORD dwThreadId, IN DWORD dwContinueStatus)
Definition: debugger.c:448
#define DebugEvent(tess)
Definition: sweep.c:59
_In_ BOOL _In_ HANDLE hProcess
Definition: mapping.h:70
#define ReadProcessMemory(a, b, c, d, e)
Definition: compat.h:415
#define LOAD_DLL_DEBUG_EVENT
Definition: winbase.h:107
HANDLE UniqueProcess
Definition: compat.h:474
NTSTATUS NTAPI NtOpenProcess(OUT PHANDLE ProcessHandle, IN ACCESS_MASK DesiredAccess, IN POBJECT_ATTRIBUTES ObjectAttributes, IN PCLIENT_ID ClientId)
Definition: process.c:1440
#define DBG_CONTINUE
Definition: ntstatus.h:47
unsigned char BOOLEAN
smooth NULL
Definition: ftsmooth.c:416
NTSTATUS NTAPI DbgUiContinue(IN PCLIENT_ID ClientId, IN NTSTATUS ContinueStatus)
Definition: dbgui.c:47
NTSTATUS NTAPI DbgUiDebugActiveProcess(IN HANDLE Process)
Definition: dbgui.c:355
_Out_ PCLIENT_ID ClientId
Definition: kefuncs.h:1176
__wchar_t WCHAR
Definition: xmlstorage.h:180
#define CREATE_THREAD_DEBUG_EVENT
Definition: winbase.h:103
#define UlongToHandle(ul)
Definition: basetsd.h:97
#define MAX_PATH
Definition: compat.h:26
unsigned long DWORD
Definition: ntddk_ex.h:95
BOOL Error
Definition: chkdsk.c:66
NTSTATUS NTAPI DbgUiWaitStateChange(OUT PDBGUI_WAIT_STATE_CHANGE WaitStateChange, IN PLARGE_INTEGER TimeOut OPTIONAL)
Definition: dbgui.c:274
HANDLE UniqueThread
Definition: compat.h:475
BOOL WINAPI WaitForDebugEvent(IN LPDEBUG_EVENT lpDebugEvent, IN DWORD dwMilliseconds)
Definition: debugger.c:625
static const char * ImageName
Definition: image.c:34
Status
Definition: gdiplustypes.h:24
_Check_return_ _CRTIMP int __cdecl getchar(void)
Definition: file.c:3627
enum State_ State
Definition: pofuncs.h:54
FORCEINLINE struct _TEB * NtCurrentTeb(VOID)
Definition: psfuncs.h:420
VOID Main(VOID)
Definition: TestDebug.c:12
#define InitializeObjectAttributes(p, n, a, r, s)
Definition: reg.c:106
#define CREATE_PROCESS_DEBUG_EVENT
Definition: winbase.h:104
BOOL WINAPI DebugActiveProcess(IN DWORD dwProcessId)
Definition: debugger.c:480
_Must_inspect_result_ _In_ PFILE_OBJECT _In_opt_ PLARGE_INTEGER _In_ ULONG _In_ FLT_IO_OPERATION_FLAGS _Out_opt_ PULONG BytesRead
Definition: fltkernel.h:1255
#define printf
Definition: config.h:203