kdebug.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690
  1. /*++
  2. Copyright (c) 2012 Minoca Corp. All Rights Reserved
  3. Module Name:
  4. kdebug.h
  5. Abstract:
  6. This header contains definitions for the kernel debugging subsystem.
  7. Author:
  8. Evan Green 3-Jul-2012
  9. --*/
  10. //
  11. // ------------------------------------------------------------------ Constants
  12. //
  13. //
  14. // ---------------------------------------------------------------- Definitions
  15. //
  16. //
  17. // Define the amount of time in milliseconds to wait for a connection before
  18. // moving on.
  19. //
  20. #define DEBUG_CONNECTION_TIMEOUT (2 * 1000000)
  21. #define DEBUG_DEFAULT_BAUD_RATE 115200
  22. //
  23. // Define the maximum device path size, which is the maximum hub depth that
  24. // a KD USB device can be plugged in behind.
  25. //
  26. #define DEBUG_USB_DEVICE_PATH_SIZE 8
  27. #define EXCEPTION_NMI 0x02
  28. #define EXCEPTION_BREAK 0x03
  29. #define EXCEPTION_SINGLE_STEP 0x04
  30. #define EXCEPTION_ACCESS_VIOLATION 0x05
  31. #define EXCEPTION_UNDEFINED_INSTRUCTION 0x06
  32. #define EXCEPTION_ASSERTION_FAILURE 0x07
  33. #define EXCEPTION_POLL_DEBUGGER 0x08
  34. #define EXCEPTION_MODULE_CHANGE 0x09
  35. #define EXCEPTION_PRINT 0x0A
  36. #define EXCEPTION_DIVIDE_BY_ZERO 0x0B
  37. #define EXCEPTION_DOUBLE_FAULT 0x0C
  38. #define EXCEPTION_PROFILER 0x0D
  39. #define EXCEPTION_UNHANDLED_INTERRUPT 0x0E
  40. #define EXCEPTION_USER_MODE 0x0F
  41. #define EXCEPTION_DEBUGGER_DISCONNECT 0x10
  42. #define EXCEPTION_DEBUGGER_CONNECT 0x11
  43. #define EXCEPTION_MATH_FAULT 0x12
  44. //
  45. // ------------------------------------------------------ Data Type Definitions
  46. //
  47. /*++
  48. Structure Description:
  49. This structure stores a list of loaded images.
  50. Members:
  51. ModuleCount - Stores the number of modules in the list.
  52. Signature - Stores the total of all timestamps and loaded addresses in the
  53. module list.
  54. ModulesHead - Stores pointers to the first and last modules in the list.
  55. --*/
  56. typedef struct _DEBUG_MODULE_LIST {
  57. ULONG ModuleCount;
  58. ULONGLONG Signature;
  59. LIST_ENTRY ModulesHead;
  60. } DEBUG_MODULE_LIST, *PDEBUG_MODULE_LIST;
  61. /*++
  62. Structure Description:
  63. This structure stores information about a loaded image.
  64. Members:
  65. ListEntry - Stores pointers to the previous and next loaded modules.
  66. StructureSize - Stores the size of the structure, including the complete
  67. binary name string, in bytes.
  68. LowestAddress - Stores the lowest valid virtual address in the image. This
  69. can be above, below, or equal to the base address.
  70. Size - Stores the size of the image, in bytes, starting from the lowest
  71. address.
  72. Timestamp - Stores the file modification date in seconds since 2001.
  73. EntryPoint - Stores a pointer to the entry point of the image.
  74. Image - Stores a pointer to more detailed image information.
  75. Process - Stores the process ID of the process that this module is specific
  76. to.
  77. BinaryName - Stores the name of the binary. The allocated structure
  78. continues for the length of the string.
  79. --*/
  80. typedef struct _DEBUG_MODULE {
  81. LIST_ENTRY ListEntry;
  82. ULONG StructureSize;
  83. PVOID LowestAddress;
  84. ULONGLONG Size;
  85. ULONGLONG Timestamp;
  86. PVOID EntryPoint;
  87. PVOID Image;
  88. ULONG Process;
  89. CHAR BinaryName[ANYSIZE_ARRAY];
  90. } DEBUG_MODULE, *PDEBUG_MODULE;
  91. /*++
  92. Structure Description:
  93. This structure stores the information required to hand off primary
  94. control of the debug device to the real USB drivers.
  95. Members:
  96. DevicePathSize - Stores the number of valid elements in the device path.
  97. DevicePath - Stores the device path to the debug device as an array of
  98. port numbers off the root port. Each element represents a port number
  99. of the parent hub to the device.
  100. DeviceAddress - Stores the device address of the debug device.
  101. HubAddress - Stores the hub address of the debug device. If the debug
  102. device is high speed, this will be set to zero.
  103. Configuration - Stores the configuration value of the configuration the
  104. device is in.
  105. VendorId - Stores the vendor ID of the debug USB device.
  106. ProductId - Stores the product ID of the debug USB device.
  107. HostData - Stores a pointer to the host controller specific data.
  108. HostDataSize - Stores the size of the host controller data.
  109. --*/
  110. typedef struct _DEBUG_USB_HANDOFF_DATA {
  111. ULONG DevicePathSize;
  112. UCHAR DevicePath[DEBUG_USB_DEVICE_PATH_SIZE];
  113. UCHAR DeviceAddress;
  114. UCHAR HubAddress;
  115. UCHAR Configuration;
  116. USHORT VendorId;
  117. USHORT ProductId;
  118. PVOID HostData;
  119. ULONG HostDataSize;
  120. } DEBUG_USB_HANDOFF_DATA, *PDEBUG_USB_HANDOFF_DATA;
  121. /*++
  122. Structure Description:
  123. This structure stores additional information about the UART debug device.
  124. Members:
  125. OemData - Stores a pointer to OEM specific data.
  126. OemDataSize - Stores the size of the OEM data in bytes.
  127. --*/
  128. typedef struct _DEBUG_UART_HANDOFF_DATA {
  129. PVOID OemData;
  130. UINTN OemDataSize;
  131. } DEBUG_UART_HANDOFF_DATA, *PDEBUG_UART_HANDOFF_DATA;
  132. /*++
  133. Structure Description:
  134. This structure stores the information required to describe the device
  135. currently in use by the kernel debugger.
  136. Members:
  137. PortType - Stores the port type of the debug device as defined by the
  138. debug port table 2 specification.
  139. PortSubType - Stores the port subtype of the debug device as defined by the
  140. debug port table 2 specification.
  141. Identifier - Stores the unique identifier of the device, often its physical
  142. base address.
  143. --*/
  144. typedef struct _DEBUG_HANDOFF_DATA {
  145. USHORT PortType;
  146. USHORT PortSubType;
  147. ULONGLONG Identifier;
  148. union {
  149. DEBUG_USB_HANDOFF_DATA Usb;
  150. DEBUG_UART_HANDOFF_DATA Uart;
  151. } U;
  152. } DEBUG_HANDOFF_DATA, *PDEBUG_HANDOFF_DATA;
  153. //
  154. // -------------------------------------------------------------------- Externs
  155. //
  156. extern DEBUG_MODULE_LIST KdLoadedModules;
  157. //
  158. // -------------------------------------------------------- Function Prototypes
  159. //
  160. KERNEL_API
  161. VOID
  162. KdConnect (
  163. VOID
  164. );
  165. /*++
  166. Routine Description:
  167. This routine connects to the kernel debugger.
  168. Arguments:
  169. None.
  170. Return Value:
  171. None.
  172. --*/
  173. KERNEL_API
  174. VOID
  175. KdDisconnect (
  176. VOID
  177. );
  178. /*++
  179. Routine Description:
  180. This routine disconnects from the kernel debugger.
  181. Arguments:
  182. None.
  183. Return Value:
  184. None.
  185. --*/
  186. KERNEL_API
  187. KSTATUS
  188. KdGetDeviceInformation (
  189. PDEBUG_HANDOFF_DATA *Information
  190. );
  191. /*++
  192. Routine Description:
  193. This routine returns information about the debug device in use. This
  194. includes information identifying the device, OEM-specific data, and
  195. transport-specific data that may be needed to coordinate shared control
  196. between runtime drivers and the kernel debug subsystem.
  197. Arguments:
  198. Information - Supplies a pointer where a pointer to the debug information
  199. will be returned on success. The caller must not modify or free this
  200. data.
  201. Return Value:
  202. STATUS_SUCCESS if information was successfully returned.
  203. STATUS_NO_ELIGIBLE_DEVICES if no debug devices were found or used.
  204. Other status codes on other errors.
  205. --*/
  206. KSTATUS
  207. KdInitialize (
  208. PDEBUG_DEVICE_DESCRIPTION DebugDevice,
  209. PDEBUG_MODULE CurrentModule
  210. );
  211. /*++
  212. Routine Description:
  213. This routine initializes the debugger subsystem and connects to the target
  214. if debugging is enabled.
  215. Arguments:
  216. DebugDevice - Supplies a pointer to the debug device to communicate on.
  217. CurrentModule - Supplies the details of the initial running program.
  218. Return Value:
  219. Kernel status code.
  220. --*/
  221. VOID
  222. KdBreak (
  223. VOID
  224. );
  225. /*++
  226. Routine Description:
  227. This routine breaks into the debugger if one is connected.
  228. Arguments:
  229. None.
  230. Return Value:
  231. Kernel status code.
  232. --*/
  233. VOID
  234. KdPrint (
  235. PSTR Format,
  236. ...
  237. );
  238. /*++
  239. Routine Description:
  240. This routine prints a string to the debugger. Currently the maximum length
  241. string is a little less than one debug packet.
  242. Arguments:
  243. Format - Supplies a pointer to the printf-like format string.
  244. ... - Supplies any needed arguments for the format string.
  245. Return Value:
  246. None.
  247. --*/
  248. VOID
  249. KdPrintWithArgumentList (
  250. PSTR Format,
  251. va_list ArgumentList
  252. );
  253. /*++
  254. Routine Description:
  255. This routine prints a string to the debugger. Currently the maximum length
  256. string is a little less than one debug packet.
  257. Arguments:
  258. Format - Supplies a pointer to the printf-like format string.
  259. ArgumentList - Supplies a pointer to the initialized list of arguments
  260. required for the format string.
  261. Return Value:
  262. None.
  263. --*/
  264. VOID
  265. KdReportModuleChange (
  266. PDEBUG_MODULE Module,
  267. BOOL Loading
  268. );
  269. /*++
  270. Routine Description:
  271. This routine informs the debugger of an image being loaded or unloaded.
  272. Arguments:
  273. Module - Supplies a pointer to the module being loaded or unloaded. The
  274. caller is responsible for managing this memory. The memory should
  275. not be freed until after reporting that the module has unloaded.
  276. Loading - Supplies a flag indicating whether the module is being loaded or
  277. unloaded.
  278. Return Value:
  279. None.
  280. --*/
  281. VOID
  282. KdPollForBreakRequest (
  283. VOID
  284. );
  285. /*++
  286. Routine Description:
  287. This routine polls the debugger connection to determine if the debugger
  288. has requested to break in.
  289. Arguments:
  290. None.
  291. Return Value:
  292. None.
  293. --*/
  294. BOOL
  295. KdIsDebuggerConnected (
  296. VOID
  297. );
  298. /*++
  299. Routine Description:
  300. This routine indicates whether or not a kernel debugger is currently
  301. connected to the system.
  302. Arguments:
  303. None.
  304. Return Value:
  305. TRUE if the debugger is enabled and connected.
  306. FALSE otherwise.
  307. --*/
  308. BOOL
  309. KdAreUserModeExceptionsEnabled (
  310. VOID
  311. );
  312. /*++
  313. Routine Description:
  314. This routine indicates whether or not noteworthy exceptions caused in
  315. applications should bubble up to kernel mode debugger breaks.
  316. Arguments:
  317. None.
  318. Return Value:
  319. TRUE if user mode exceptions should bubble up to kernel mode.
  320. FALSE otherwise.
  321. --*/
  322. ULONG
  323. KdSetConnectionTimeout (
  324. ULONG Timeout
  325. );
  326. /*++
  327. Routine Description:
  328. This routine sets the debugger connection timeout.
  329. Arguments:
  330. Timeout - Supplies the new timeout in microseconds. Supply MAX_ULONG to
  331. cause the debugger to not call the stall function and never time out
  332. the connection.
  333. Return Value:
  334. Returns the original timeout.
  335. --*/
  336. VOID
  337. KdSendProfilingData (
  338. VOID
  339. );
  340. /*++
  341. Routine Description:
  342. This routine polls the system profiler to determine if there is profiling
  343. data to be sent to the debugger.
  344. Arguments:
  345. None.
  346. Return Value:
  347. None.
  348. --*/
  349. VOID
  350. KdEnableNmiBroadcast (
  351. BOOL Enable
  352. );
  353. /*++
  354. Routine Description:
  355. This routine enables or disables the use of NMI broadcasts by the debugger.
  356. Arguments:
  357. Enable - Supplies TRUE if NMI broadcast is being enabled, or FALSE if NMI
  358. broadcast is being disabled.
  359. Return Value:
  360. None.
  361. --*/
  362. VOID
  363. KdDebugExceptionHandler (
  364. ULONG Exception,
  365. PVOID Parameter,
  366. PTRAP_FRAME TrapFrame
  367. );
  368. /*++
  369. Routine Description:
  370. This routine handles the debug break exception. It is usually called by an
  371. assembly routine responding to an exception.
  372. Arguments:
  373. Exception - Supplies the type of exception that this function is handling.
  374. See EXCEPTION_* definitions for valid values.
  375. Parameter - Supplies a pointer to a parameter supplying additional
  376. context in the case of a debug service request.
  377. TrapFrame - Supplies a pointer to the state of the machine immediately
  378. before the debug exception occurred. Also returns the possibly modified
  379. machine state.
  380. Return Value:
  381. None.
  382. --*/
  383. VOID
  384. KdNmiHandler (
  385. PTRAP_FRAME TrapFrame
  386. );
  387. /*++
  388. Routine Description:
  389. This routine handles NMI interrupts.
  390. Arguments:
  391. TrapFrame - Supplies a pointer to the state of the machine immediately
  392. before the NMI occurred. Also returns the possibly modified
  393. machine state.
  394. Return Value:
  395. None.
  396. --*/
  397. VOID
  398. KdNmiHandlerAsm (
  399. VOID
  400. );
  401. /*++
  402. Routine Description:
  403. This routine is called directly when an NMI occurs. Since it is a hardware
  404. task switch, no registers need to be saved.
  405. Arguments:
  406. ReturnEip - Supplies the address after the instruction that caused the trap.
  407. ReturnCodeSelector - Supplies the code selector the code that trapped was
  408. running under.
  409. ReturnEflags - Supplies the EFLAGS register immediately before the trap.
  410. Return Value:
  411. None.
  412. --*/
  413. VOID
  414. KdDebugServiceHandlerAsm (
  415. ULONG ReturnEip,
  416. ULONG ReturnCodeSelector,
  417. ULONG ReturnEflags
  418. );
  419. /*++
  420. Routine Description:
  421. This routine is entered via an IDT entry to request debug service. It sets
  422. up the parameters and calls KdDebugExceptionHandler, and then restores
  423. machine state to return from the exception. The arguments to this function
  424. are pushed by the hardware. Upon Entry:
  425. eax - Supplies the debug service request.
  426. ecx - Supplies the parameter to the request.
  427. Arguments:
  428. ReturnEip - Supplies the address after the instruction that caused the trap.
  429. ReturnCodeSelector - Supplies the code selector the code that trapped was
  430. running under.
  431. ReturnEflags - Supplies the EFLAGS register immediately before the trap.
  432. Return Value:
  433. None.
  434. --*/