kdebug.h 13 KB

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