16 OBJECT_ATTRIBUTES ObjectAttributes
;
18 DBGUI_WAIT_STATE_CHANGE State
;
20 DWORD Error
, BytesRead
;
21 DEBUG_EVENT DebugEvent
;
22 WCHAR ImageName
[MAX_PATH
];
28 printf("*** Native (DbgUi) Debugging Test Application\n");
29 printf("Press any key to connect to Dbgk...");
32 Status
= DbgUiConnectToDbg();
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...");
37 printf("*** Win32 (Debug) Debugging Test Application\n");
38 printf("Press any key to debug services.exe...");
43 InitializeObjectAttributes(&ObjectAttributes
, NULL
, 0, 0, 0);
44 ClientId
.UniqueThread
= 0;
45 ClientId
.UniqueProcess
= UlongToHandle(168);
46 Status
= NtOpenProcess(&hProcess
,
50 Status
= DbgUiDebugActiveProcess(hProcess
);
52 Error
= DebugActiveProcess(168);
56 printf(" Debugger Attached. Status: %lx\n", Status
);
58 printf(" Debugger Attached. Error: %lx\n", Error
);
60 printf("Press any key to get first debug event... ");
66 Status
= DbgUiWaitStateChange(&State
, NULL
);
67 printf(" Event Received. Status: %lx\n", Status
);
68 printf("New State: %lx. Application Client ID: %lx/%lx\n",
70 State
.AppClientId
.UniqueProcess
, State
.AppClientId
.UniqueThread
);
72 Error
= WaitForDebugEvent(&DebugEvent
, -1);
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
);
80 switch (State
.NewState
)
82 switch (DebugEvent
.dwDebugEventCode
)
86 case DbgCreateProcessStateChange
:
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
);
95 case CREATE_PROCESS_DEBUG_EVENT
:
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
;
108 case DbgCreateThreadStateChange
:
109 printf("New thread: %lx\n", State
.StateInfo
.CreateThread
.HandleToThread
);
110 printf("Thread Start Address: %p\n", State
.StateInfo
.CreateThread
.NewThread
.StartAddress
);
112 case CREATE_THREAD_DEBUG_EVENT
:
113 printf("New thread: %lx\n", DebugEvent
.u
.CreateThread
.hThread
);
114 printf("Thread Start Address: %p\n",
115 DebugEvent
.u
.CreateThread
.lpStartAddress
);
120 case DbgLoadDllStateChange
:
121 printf("New DLL: %lx\n", State
.StateInfo
.LoadDll
.FileHandle
);
122 printf("DLL LoadAddress: %p\n", State
.StateInfo
.LoadDll
.BaseOfDll
);
124 case LOAD_DLL_DEBUG_EVENT
:
125 printf("New DLL: %lx\n", DebugEvent
.u
.LoadDll
.hFile
);
126 printf("DLL LoadAddress: %p\n", DebugEvent
.u
.LoadDll
.lpBaseOfDll
);
127 Error
= ReadProcessMemory(hProcess
,
128 DebugEvent
.u
.LoadDll
.lpImageName
,
129 &DebugEvent
.u
.LoadDll
.lpImageName
,
130 sizeof(DebugEvent
.u
.LoadDll
.lpImageName
),
132 if (DebugEvent
.u
.LoadDll
.lpImageName
)
134 Error
= ReadProcessMemory(hProcess
,
135 DebugEvent
.u
.LoadDll
.lpImageName
,
139 printf("DLL Name: %S\n", ImageName
);
145 case DbgBreakpointStateChange
:
146 printf("Initial breakpoint hit at: %p!\n",
147 State
.StateInfo
.Exception
.ExceptionRecord
.ExceptionAddress
);
154 case DbgExitThreadStateChange
:
155 printf("Thread exited: %lx\n", State
.StateInfo
.ExitThread
.ExitStatus
);
162 case DbgExitProcessStateChange
:
163 printf("Process exited: %lx\n", State
.StateInfo
.ExitProcess
.ExitStatus
);
171 printf("Press any key to continue debuggee...");
175 ClientId
.UniqueProcess
= State
.AppClientId
.UniqueProcess
;
176 ClientId
.UniqueThread
= State
.AppClientId
.UniqueThread
;
177 Status
= DbgUiContinue(&ClientId
, DBG_CONTINUE
);
178 printf(" Debuggee Resumed. Status: %lx\n", Status
);
180 Error
= ContinueDebugEvent(DebugEvent
.dwProcessId
,
181 DebugEvent
.dwThreadId
,
183 printf(" Debuggee Resumed. Error: %lx\n", Error
);
186 printf("Press any key to get next debug event... ");
189 printf("*** End of test\n");