Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

505 lines
13 KiB

  1. /*++
  2. Copyright (c) 1989 Microsoft Corporation
  3. Module Name:
  4. dlluistb.c
  5. Abstract:
  6. Debug Subsystem DbgUi API Stubs
  7. Author:
  8. Mark Lucovsky (markl) 23-Jan-1990
  9. Revision History:
  10. Neill Clift 27-Apr-2000 - Rehashed to call new kernel APIs for process debugging
  11. --*/
  12. #include "dbgdllp.h"
  13. #include "windows.h"
  14. #define DbgUiDebugObjectHandle (NtCurrentTeb()->DbgSsReserved[1])
  15. NTSTATUS
  16. DbgUiConnectToDbg (
  17. VOID
  18. )
  19. /*++
  20. Routine Description:
  21. This routine makes a connection between the caller and the DbgUi
  22. port in the Dbg subsystem. In addition to returning a handle to a
  23. port object, a handle to a state change semaphore is returned. This
  24. semaphore is used in DbgUiWaitStateChange APIs.
  25. Arguments:
  26. None.
  27. Return Value:
  28. NTSTATUS.
  29. --*/
  30. {
  31. NTSTATUS st;
  32. OBJECT_ATTRIBUTES oa;
  33. //
  34. // if app is already connected, don't reconnect
  35. //
  36. st = STATUS_SUCCESS;
  37. if ( !DbgUiDebugObjectHandle ) {
  38. InitializeObjectAttributes (&oa, NULL, 0, NULL, NULL);
  39. st = NtCreateDebugObject (&DbgUiDebugObjectHandle,
  40. DEBUG_ALL_ACCESS,
  41. &oa,
  42. DEBUG_KILL_ON_CLOSE);
  43. }
  44. return st;
  45. }
  46. HANDLE
  47. DbgUiGetThreadDebugObject (
  48. )
  49. /*++
  50. Routine Description:
  51. This function returns the current threads debug port handle if it has one.
  52. Arguments:
  53. None
  54. Return Value:
  55. HANDLE - Debug port handle;
  56. --*/
  57. {
  58. return DbgUiDebugObjectHandle;
  59. }
  60. VOID
  61. DbgUiSetThreadDebugObject (
  62. IN HANDLE DebugObject
  63. )
  64. /*++
  65. Routine Description:
  66. This function sets the current thread's debug port handle.
  67. Any previous value is simply overwritten; there is no
  68. automatic close of a previous handle.
  69. Arguments:
  70. DebugObject - Debug object handle to set.
  71. Return Value:
  72. None.
  73. --*/
  74. {
  75. DbgUiDebugObjectHandle = DebugObject;
  76. }
  77. NTSTATUS
  78. DbgUiWaitStateChange (
  79. OUT PDBGUI_WAIT_STATE_CHANGE StateChange,
  80. IN PLARGE_INTEGER Timeout OPTIONAL
  81. )
  82. /*++
  83. Routine Description:
  84. This function causes the calling user interface to wait for a
  85. state change to occur in one of it's application threads. The
  86. wait is ALERTABLE.
  87. Arguments:
  88. StateChange - Supplies the address of state change record that
  89. will contain the state change information.
  90. Return Value:
  91. NTSTATUS.
  92. --*/
  93. {
  94. NTSTATUS st;
  95. //
  96. // Wait for a StateChange to occur
  97. //
  98. st = NtWaitForDebugEvent (DbgUiDebugObjectHandle,
  99. TRUE,
  100. Timeout,
  101. StateChange);
  102. return st;
  103. }
  104. NTSTATUS
  105. DbgUiContinue (
  106. IN PCLIENT_ID AppClientId,
  107. IN NTSTATUS ContinueStatus
  108. )
  109. /*++
  110. Routine Description:
  111. This function continues an application thread whose state change was
  112. previously reported through DbgUiWaitStateChange.
  113. Arguments:
  114. AppClientId - Supplies the address of the ClientId of the
  115. application thread being continued. This must be an application
  116. thread that previously notified the caller through
  117. DbgUiWaitStateChange but has not yet been continued.
  118. ContinueStatus - Supplies the continuation status to the thread
  119. being continued. valid values for this are:
  120. DBG_EXCEPTION_HANDLED
  121. DBG_EXCEPTION_NOT_HANDLED
  122. DBG_TERMINATE_THREAD
  123. DBG_TERMINATE_PROCESS
  124. DBG_CONTINUE
  125. Return Value:
  126. STATUS_SUCCESS - Successful call to DbgUiContinue
  127. STATUS_INVALID_CID - An invalid ClientId was specified for the
  128. AppClientId, or the specified Application was not waiting
  129. for a continue.
  130. STATUS_INVALID_PARAMETER - An invalid continue status was specified.
  131. --*/
  132. {
  133. NTSTATUS st;
  134. st = NtDebugContinue (DbgUiDebugObjectHandle,
  135. AppClientId,
  136. ContinueStatus);
  137. return st;
  138. }
  139. NTSTATUS
  140. DbgUiStopDebugging (
  141. IN HANDLE Process
  142. )
  143. /*++
  144. Routine Description:
  145. This function stops debugging the specified process
  146. Arguments:
  147. Process - Process handle of process being debugged
  148. Return Value:
  149. NTSTATUS - Status of call
  150. --*/
  151. {
  152. NTSTATUS st;
  153. st = NtRemoveProcessDebug (Process,
  154. DbgUiDebugObjectHandle);
  155. return st;
  156. }
  157. VOID
  158. DbgUiRemoteBreakin (
  159. IN PVOID Context
  160. )
  161. /*++
  162. Routine Description:
  163. This function starts debugging the target process
  164. Arguments:
  165. Context - Thread context
  166. Return Value:
  167. None
  168. --*/
  169. {
  170. UNREFERENCED_PARAMETER (Context);
  171. //
  172. // We need to cover the case here where the caller detaches the debugger
  173. // (or the debugger fails and the port is removed by
  174. // the kernel). In this case by the time we execute the debugger may be
  175. // gone. Test first that the debugger is present and if it
  176. // is call the breakpoint routine in a try/except block so if it goes
  177. // away now we unwind and just exit this thread.
  178. //
  179. if ((NtCurrentPeb()->BeingDebugged) ||
  180. (USER_SHARED_DATA->KdDebuggerEnabled & 0x00000002)) {
  181. try {
  182. DbgBreakPoint();
  183. } except (EXCEPTION_EXECUTE_HANDLER) {
  184. }
  185. }
  186. RtlExitUserThread (STATUS_SUCCESS);
  187. }
  188. NTSTATUS
  189. DbgUiIssueRemoteBreakin (
  190. IN HANDLE Process
  191. )
  192. /*++
  193. Routine Description:
  194. This function creates a remote thread int he target process to break in
  195. Arguments:
  196. Process - Process to debug
  197. Return Value:
  198. NTSTATUS - Status of call
  199. --*/
  200. {
  201. NTSTATUS Status, Status1;
  202. HANDLE Thread;
  203. CLIENT_ID ClientId;
  204. Status = RtlCreateUserThread (Process,
  205. NULL,
  206. FALSE,
  207. 0,
  208. 0,
  209. 0x4000,
  210. (PUSER_THREAD_START_ROUTINE) DbgUiRemoteBreakin,
  211. NULL,
  212. &Thread,
  213. &ClientId);
  214. if (NT_SUCCESS (Status)) {
  215. Status1 = NtClose (Thread);
  216. ASSERT (NT_SUCCESS (Status1));
  217. }
  218. return Status;
  219. }
  220. NTSTATUS
  221. DbgUiDebugActiveProcess (
  222. IN HANDLE Process
  223. )
  224. /*++
  225. Routine Description:
  226. This function starts debugging the target process
  227. Arguments:
  228. dwProcessId - Process ID of process being debugged
  229. Return Value:
  230. NTSTATUS - Status of call
  231. --*/
  232. {
  233. NTSTATUS Status, Status1;
  234. Status = NtDebugActiveProcess (Process,
  235. DbgUiDebugObjectHandle);
  236. if (NT_SUCCESS (Status)) {
  237. Status = DbgUiIssueRemoteBreakin (Process);
  238. if (!NT_SUCCESS (Status)) {
  239. Status1 = DbgUiStopDebugging (Process);
  240. }
  241. }
  242. return Status;
  243. }
  244. NTSTATUS
  245. DbgUiConvertStateChangeStructure (
  246. IN PDBGUI_WAIT_STATE_CHANGE StateChange,
  247. OUT LPDEBUG_EVENT DebugEvent)
  248. /*++
  249. Routine Description:
  250. This function converts the internal state change record to the win32 structure.
  251. Arguments:
  252. StateChange - Native debugger event structure
  253. DebugEvent - Win32 structure
  254. Return Value:
  255. NTSTATUS - Status of call
  256. --*/
  257. {
  258. NTSTATUS Status;
  259. THREAD_BASIC_INFORMATION ThreadBasicInfo;
  260. DebugEvent->dwProcessId = HandleToUlong (StateChange->AppClientId.UniqueProcess);
  261. DebugEvent->dwThreadId = HandleToUlong (StateChange->AppClientId.UniqueThread);
  262. switch (StateChange->NewState) {
  263. case DbgCreateThreadStateChange :
  264. DebugEvent->dwDebugEventCode = CREATE_THREAD_DEBUG_EVENT;
  265. DebugEvent->u.CreateThread.hThread =
  266. StateChange->StateInfo.CreateThread.HandleToThread;
  267. DebugEvent->u.CreateThread.lpStartAddress =
  268. (LPTHREAD_START_ROUTINE)(ULONG_PTR)StateChange->StateInfo.CreateThread.NewThread.StartAddress;
  269. Status = NtQueryInformationThread (StateChange->StateInfo.CreateThread.HandleToThread,
  270. ThreadBasicInformation,
  271. &ThreadBasicInfo,
  272. sizeof (ThreadBasicInfo),
  273. NULL);
  274. if (!NT_SUCCESS (Status)) {
  275. DebugEvent->u.CreateThread.lpThreadLocalBase = NULL;
  276. } else {
  277. DebugEvent->u.CreateThread.lpThreadLocalBase = ThreadBasicInfo.TebBaseAddress;
  278. }
  279. break;
  280. case DbgCreateProcessStateChange :
  281. DebugEvent->dwDebugEventCode = CREATE_PROCESS_DEBUG_EVENT;
  282. DebugEvent->u.CreateProcessInfo.hProcess =
  283. StateChange->StateInfo.CreateProcessInfo.HandleToProcess;
  284. DebugEvent->u.CreateProcessInfo.hThread =
  285. StateChange->StateInfo.CreateProcessInfo.HandleToThread;
  286. DebugEvent->u.CreateProcessInfo.hFile =
  287. StateChange->StateInfo.CreateProcessInfo.NewProcess.FileHandle;
  288. DebugEvent->u.CreateProcessInfo.lpBaseOfImage =
  289. StateChange->StateInfo.CreateProcessInfo.NewProcess.BaseOfImage;
  290. DebugEvent->u.CreateProcessInfo.dwDebugInfoFileOffset =
  291. StateChange->StateInfo.CreateProcessInfo.NewProcess.DebugInfoFileOffset;
  292. DebugEvent->u.CreateProcessInfo.nDebugInfoSize =
  293. StateChange->StateInfo.CreateProcessInfo.NewProcess.DebugInfoSize;
  294. DebugEvent->u.CreateProcessInfo.lpStartAddress =
  295. (LPTHREAD_START_ROUTINE)(ULONG_PTR)StateChange->StateInfo.CreateProcessInfo.NewProcess.InitialThread.StartAddress;
  296. Status = NtQueryInformationThread (StateChange->StateInfo.CreateProcessInfo.HandleToThread,
  297. ThreadBasicInformation,
  298. &ThreadBasicInfo,
  299. sizeof (ThreadBasicInfo),
  300. NULL);
  301. if (!NT_SUCCESS (Status)) {
  302. DebugEvent->u.CreateProcessInfo.lpThreadLocalBase = NULL;
  303. } else {
  304. DebugEvent->u.CreateProcessInfo.lpThreadLocalBase = ThreadBasicInfo.TebBaseAddress;
  305. }
  306. DebugEvent->u.CreateProcessInfo.lpImageName = NULL;
  307. DebugEvent->u.CreateProcessInfo.fUnicode = 1;
  308. break;
  309. case DbgExitThreadStateChange :
  310. DebugEvent->dwDebugEventCode = EXIT_THREAD_DEBUG_EVENT;
  311. DebugEvent->u.ExitThread.dwExitCode = (DWORD)StateChange->StateInfo.ExitThread.ExitStatus;
  312. break;
  313. case DbgExitProcessStateChange :
  314. DebugEvent->dwDebugEventCode = EXIT_PROCESS_DEBUG_EVENT;
  315. DebugEvent->u.ExitProcess.dwExitCode = (DWORD)StateChange->StateInfo.ExitProcess.ExitStatus;
  316. break;
  317. case DbgExceptionStateChange :
  318. case DbgBreakpointStateChange :
  319. case DbgSingleStepStateChange :
  320. if (StateChange->StateInfo.Exception.ExceptionRecord.ExceptionCode == DBG_PRINTEXCEPTION_C) {
  321. DebugEvent->dwDebugEventCode = OUTPUT_DEBUG_STRING_EVENT;
  322. DebugEvent->u.DebugString.lpDebugStringData =
  323. (PVOID)StateChange->StateInfo.Exception.ExceptionRecord.ExceptionInformation[1];
  324. DebugEvent->u.DebugString.nDebugStringLength =
  325. (WORD)StateChange->StateInfo.Exception.ExceptionRecord.ExceptionInformation[0];
  326. DebugEvent->u.DebugString.fUnicode = (WORD)0;
  327. } else if (StateChange->StateInfo.Exception.ExceptionRecord.ExceptionCode == DBG_RIPEXCEPTION) {
  328. DebugEvent->dwDebugEventCode = RIP_EVENT;
  329. DebugEvent->u.RipInfo.dwType =
  330. (DWORD)StateChange->StateInfo.Exception.ExceptionRecord.ExceptionInformation[1];
  331. DebugEvent->u.RipInfo.dwError =
  332. (DWORD)StateChange->StateInfo.Exception.ExceptionRecord.ExceptionInformation[0];
  333. } else {
  334. DebugEvent->dwDebugEventCode = EXCEPTION_DEBUG_EVENT;
  335. DebugEvent->u.Exception.ExceptionRecord =
  336. StateChange->StateInfo.Exception.ExceptionRecord;
  337. DebugEvent->u.Exception.dwFirstChance =
  338. StateChange->StateInfo.Exception.FirstChance;
  339. }
  340. break;
  341. case DbgLoadDllStateChange :
  342. DebugEvent->dwDebugEventCode = LOAD_DLL_DEBUG_EVENT;
  343. DebugEvent->u.LoadDll.lpBaseOfDll =
  344. StateChange->StateInfo.LoadDll.BaseOfDll;
  345. DebugEvent->u.LoadDll.hFile =
  346. StateChange->StateInfo.LoadDll.FileHandle;
  347. DebugEvent->u.LoadDll.dwDebugInfoFileOffset =
  348. StateChange->StateInfo.LoadDll.DebugInfoFileOffset;
  349. DebugEvent->u.LoadDll.nDebugInfoSize =
  350. StateChange->StateInfo.LoadDll.DebugInfoSize;
  351. //
  352. // pick up the image name
  353. //
  354. DebugEvent->u.LoadDll.lpImageName = StateChange->StateInfo.LoadDll.NamePointer;
  355. DebugEvent->u.LoadDll.fUnicode = 1;
  356. break;
  357. case DbgUnloadDllStateChange :
  358. DebugEvent->dwDebugEventCode = UNLOAD_DLL_DEBUG_EVENT;
  359. DebugEvent->u.UnloadDll.lpBaseOfDll =
  360. StateChange->StateInfo.UnloadDll.BaseAddress;
  361. break;
  362. default:
  363. return STATUS_UNSUCCESSFUL;
  364. }
  365. return STATUS_SUCCESS;
  366. }