dbgext.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919
  1. /*++
  2. Copyright (c) 2012 Minoca Corp. All Rights Reserved
  3. Module Name:
  4. dbgext.h
  5. Abstract:
  6. This header defines the interface between the debug client and debugger
  7. extensions.
  8. Author:
  9. Evan Green 10-Sep-2012
  10. --*/
  11. //
  12. // ------------------------------------------------------------------- Includes
  13. //
  14. #include <stdarg.h>
  15. #include <minoca/debug/dbgproto.h>
  16. //
  17. // ---------------------------------------------------------------- Definitions
  18. //
  19. #define EXTENSION_API_VERSION 1
  20. #define MAX_EXTENSION_COMMAND 32
  21. //
  22. // ------------------------------------------------------ Data Type Definitions
  23. //
  24. typedef struct _DEBUGGER_CONTEXT DEBUGGER_CONTEXT, *PDEBUGGER_CONTEXT;
  25. typedef struct _TYPE_SYMBOL TYPE_SYMBOL, *PTYPE_SYMBOL;
  26. typedef
  27. INT
  28. (*PEXTENSION_PROTOTYPE) (
  29. PDEBUGGER_CONTEXT Context,
  30. PSTR Command,
  31. ULONG ArgumentCount,
  32. PSTR *ArgumentValues
  33. );
  34. /*++
  35. Routine Description:
  36. This routine defines a debugger extension prototype. This what gets called
  37. when the user invokes the extension.
  38. Arguments:
  39. Context - Supplies a pointer to the debugger applicaton context, which is
  40. an argument to most of the API functions.
  41. Command - Supplies the subcommand entered, if applicable, or NULL if no
  42. subcommand was registered.
  43. ArgumentCount - Supplies the number of arguments in the ArgumentValues
  44. array.
  45. ArgumentValues - Supplies the values of each argument. This memory will be
  46. reused when the function returns, so extensions must not touch this
  47. memory after returning from this call. The first argument is always the
  48. complete name itself (ie "!myext.help").
  49. Return Value:
  50. 0 if the debugger extension command was successful.
  51. Returns an error code if a failure occurred along the way.
  52. --*/
  53. /*++
  54. Structure Description:
  55. This structure stores information about the current debugging target.
  56. Members:
  57. MachineType - Supplies the architecture of the machine being debugged.
  58. See MACHINE_TYPE_* definitions.
  59. --*/
  60. typedef struct _DEBUG_TARGET_INFORMATION {
  61. ULONG MachineType;
  62. } DEBUG_TARGET_INFORMATION, *PDEBUG_TARGET_INFORMATION;
  63. /*++
  64. Structure Description:
  65. This structure defines a frame in a call stack.
  66. Members:
  67. FramePointer - Stores a pointer to the base of the stack frame. On x86
  68. architectures, this would be the EBP register.
  69. ReturnAddress - Stores the return address of the current stack frame.
  70. --*/
  71. typedef struct _STACK_FRAME {
  72. ULONGLONG FramePointer;
  73. ULONGLONG ReturnAddress;
  74. } STACK_FRAME, *PSTACK_FRAME;
  75. //
  76. // -------------------------------------------------------------------- Globals
  77. //
  78. //
  79. // -------------------------------------------------------- Function Prototypes
  80. //
  81. //
  82. // Functions to be implemented by the debug extension.
  83. //
  84. INT
  85. ExtensionMain (
  86. PDEBUGGER_CONTEXT Context,
  87. ULONG ExtensionApiVersion,
  88. PVOID Token
  89. );
  90. /*++
  91. Routine Description:
  92. This routine defines the extension's main routine. This routine will get
  93. called when the extension is loaded. It is responsible for registering the
  94. debugger extensions it supports.
  95. Arguments:
  96. Context - Supplies the application instance context. This must be passed
  97. into the registration routines.
  98. ExtensionApiVersion - Supplies the revision of the debugger extension API.
  99. Token - Supplies a token that uniquely idenfies the extension. This is used
  100. when registering extensions.
  101. Return Value:
  102. 0 on success.
  103. Returns an error code on failure.
  104. --*/
  105. //
  106. // Functions callable by the extension.
  107. //
  108. INT
  109. DbgRegisterExtension (
  110. PDEBUGGER_CONTEXT Context,
  111. PVOID Token,
  112. PSTR ExtensionName,
  113. PSTR OneLineDescription,
  114. PEXTENSION_PROTOTYPE Routine
  115. );
  116. /*++
  117. Routine Description:
  118. This routine registers a debugger extension with the client.
  119. Arguments:
  120. Context - Supplies a pointer to the application context.
  121. Token - Supplies the unique token provided to the extension library upon
  122. initialization.
  123. ExtensionName - Supplies the name of the extension to register. This name
  124. must not already be registered by the current extension or any other.
  125. OneLineDescription - Supplies a quick description of the extension, no
  126. longer than 60 characters. This parameter is not optional.
  127. Routine - Supplies the routine to call when the given extension is
  128. invoked.
  129. Return Value:
  130. 0 on success.
  131. Returns an error code on failure.
  132. --*/
  133. INT
  134. DbgOut (
  135. const char *Format,
  136. ...
  137. );
  138. /*++
  139. Routine Description:
  140. This routine prints a formatted string to the debugger console.
  141. Arguments:
  142. Format - Supplies the printf format string.
  143. ... - Supplies a variable number of arguments, as required by the printf
  144. format string argument.
  145. Return Value:
  146. Returns the number of bytes successfully converted, not including the null
  147. terminator.
  148. Returns a negative number if an error was encountered.
  149. --*/
  150. INT
  151. DbgOutVaList (
  152. PDEBUGGER_CONTEXT Context,
  153. const char *Format,
  154. va_list Arguments
  155. );
  156. /*++
  157. Routine Description:
  158. This routine prints a formatted string to the given debugger console.
  159. Arguments:
  160. Context - Supplies a pointer to the debugger context to output to.
  161. Format - Supplies the printf format string.
  162. Arguments - Supplies the argument list to the format string. The va_end
  163. macro is not invoked on this list.
  164. Return Value:
  165. Returns the number of bytes successfully converted. A null terminator is
  166. not written.
  167. Returns a negative number if an error was encountered.
  168. --*/
  169. INT
  170. DbgEvaluate (
  171. PDEBUGGER_CONTEXT Context,
  172. PSTR String,
  173. PULONGLONG Result
  174. );
  175. /*++
  176. Routine Description:
  177. This routine evaluates a mathematical expression. The following operators
  178. are supported: +, -, *, /, (, ). No spaces are permitted. Module symbols
  179. are permitted and will be translated into their corresponding address.
  180. Arguments:
  181. Context - Supplies a pointer to the debugger application context.
  182. String - Supplies the string to evaluate.
  183. Result - Supplies a pointer to the 64-bit unsigned integer where the result
  184. will be stored.
  185. Return Value:
  186. 0 if the expression was successfully evaluated.
  187. Returns an error code on failure.
  188. --*/
  189. INT
  190. DbgPrintAddressSymbol (
  191. PDEBUGGER_CONTEXT Context,
  192. ULONGLONG Address
  193. );
  194. /*++
  195. Routine Description:
  196. This routine prints a descriptive version of the given address, including
  197. the module and function name if possible.
  198. Arguments:
  199. Context - Supplies a pointer to the application context.
  200. Address - Supplies the virtual address of the target to print information
  201. about.
  202. Return Value:
  203. 0 if information was successfully printed.
  204. Returns an error code on failure.
  205. --*/
  206. INT
  207. DbgReadMemory (
  208. PDEBUGGER_CONTEXT Context,
  209. BOOL VirtualMemory,
  210. ULONGLONG Address,
  211. ULONG BytesToRead,
  212. PVOID Buffer,
  213. PULONG BytesRead
  214. );
  215. /*++
  216. Routine Description:
  217. This routine retrieves the debuggee's memory.
  218. Arguments:
  219. Context - Supplies a pointer to the application context.
  220. VirtualMemory - Supplies a flag indicating whether the read should be
  221. virtual or physical.
  222. Address - Supplies the address to read from the target's memory.
  223. BytesToRead - Supplies the number of bytes to be read.
  224. Buffer - Supplies a pointer to the buffer where the memory contents will be
  225. returned.
  226. BytesRead - Supplies a pointer that receive the number of bytes that were
  227. actually read from the target.
  228. Return Value:
  229. 0 on success.
  230. Returns an error code on failure.
  231. --*/
  232. INT
  233. DbgWriteMemory (
  234. PDEBUGGER_CONTEXT Context,
  235. BOOL VirtualMemory,
  236. ULONGLONG Address,
  237. ULONG BytesToWrite,
  238. PVOID Buffer,
  239. PULONG BytesWritten
  240. );
  241. /*++
  242. Routine Description:
  243. This routine writes to the debuggee's memory.
  244. Arguments:
  245. Context - Supplies a pointer to the application context.
  246. VirtualMemory - Supplies a flag indicating whether the read should be
  247. virtual or physical.
  248. Address - Supplies the address to write to the target's memory.
  249. BytesToWrite - Supplies the number of bytes to be written.
  250. Buffer - Supplies a pointer to the buffer containing the values to write.
  251. BytesWritten - Supplies a pointer that receives the number of bytes that
  252. were actually written to the target.
  253. Return Value:
  254. 0 if the write was successful.
  255. Returns an error code on failure.
  256. --*/
  257. INT
  258. DbgReboot (
  259. PDEBUGGER_CONTEXT Context,
  260. ULONG RebootType
  261. );
  262. /*++
  263. Routine Description:
  264. This routine attempts to reboot the target machine.
  265. Arguments:
  266. Context - Supplies a pointer to the application context.
  267. RebootType - Supplies the type of reboot to perform. See the
  268. DEBUG_REBOOT_TYPE enumeration.
  269. Return Value:
  270. 0 if the write was successful.
  271. Returns an error code on failure.
  272. --*/
  273. INT
  274. DbgGetCallStack (
  275. PDEBUGGER_CONTEXT Context,
  276. PREGISTERS_UNION Registers,
  277. PSTACK_FRAME Frames,
  278. PULONG FrameCount
  279. );
  280. /*++
  281. Routine Description:
  282. This routine attempts to unwind the call stack starting at the given
  283. machine state.
  284. Arguments:
  285. Context - Supplies a pointer to the application context.
  286. Registers - Supplies an optional pointer to the registers on input. On
  287. output, these registers will be updated with the unwound value. If this
  288. is NULL, then the current break notification registers will be used.
  289. Frames - Supplies a pointer where the array of stack frames will be
  290. returned.
  291. FrameCount - Supplies the number of frames allocated in the frames
  292. argument, representing the maximum number of frames to get. On output,
  293. returns the number of valid frames in the array.
  294. Return Value:
  295. 0 on success.
  296. Returns an error code on failure.
  297. --*/
  298. INT
  299. DbgStackUnwind (
  300. PDEBUGGER_CONTEXT Context,
  301. PREGISTERS_UNION Registers,
  302. PBOOL Unwind,
  303. PSTACK_FRAME Frame
  304. );
  305. /*++
  306. Routine Description:
  307. This routine attempts to unwind the stack by one frame.
  308. Arguments:
  309. Context - Supplies a pointer to the application context.
  310. Registers - Supplies a pointer to the registers on input. On output, these
  311. registers will be updated with the unwound value.
  312. Unwind - Supplies a pointer that on input should initially be set to TRUE,
  313. indicating to use the symbol unwinder if possible. If unwinding is not
  314. possible, this will be set to FALSE, and should remain FALSE for the
  315. remainder of the stack frames unwound.
  316. Frame - Supplies a pointer where the basic frame information for this
  317. frame will be returned.
  318. Return Value:
  319. 0 on success.
  320. EOF if there are no more stack frames.
  321. Returns an error code on failure.
  322. --*/
  323. INT
  324. DbgPrintCallStack (
  325. PDEBUGGER_CONTEXT Context,
  326. PREGISTERS_UNION Registers,
  327. BOOL PrintFrameNumbers
  328. );
  329. /*++
  330. Routine Description:
  331. This routine prints a call stack starting with the given registers.
  332. Arguments:
  333. Context - Supplies a pointer to the application context.
  334. Registers - Supplies an optional pointer to the registers to use when
  335. unwinding.
  336. PrintFrameNumbers - Supplies a boolean indicating whether or not frame
  337. numbers should be printed to the left of every frame.
  338. Return Value:
  339. 0 on success.
  340. Returns an error code on failure.
  341. --*/
  342. INT
  343. DbgGetTargetInformation (
  344. PDEBUGGER_CONTEXT Context,
  345. PDEBUG_TARGET_INFORMATION TargetInformation,
  346. ULONG TargetInformationSize
  347. );
  348. /*++
  349. Routine Description:
  350. This routine returns information about the machine being debugged.
  351. Arguments:
  352. Context - Supplies a pointer to the application context.
  353. TargetInformation - Supplies a pointer where the target information will
  354. be returned.
  355. TargetInformationSize - Supplies the size of the target information buffer.
  356. This must be the size of a debug target information structure.
  357. Return Value:
  358. 0 on success.
  359. Returns an error code on failure.
  360. --*/
  361. ULONG
  362. DbgGetTargetPointerSize (
  363. PDEBUGGER_CONTEXT Context
  364. );
  365. /*++
  366. Routine Description:
  367. This routine returns the size of a pointer on the target machine, in bytes.
  368. Arguments:
  369. Context - Supplies a pointer to the application context.
  370. Return Value:
  371. The size of a pointer on the target system, in bytes.
  372. --*/
  373. VOID
  374. DbgGetStackRegisters (
  375. PDEBUGGER_CONTEXT Context,
  376. PREGISTERS_UNION Registers,
  377. PULONGLONG StackPointer,
  378. PULONGLONG FramePointer
  379. );
  380. /*++
  381. Routine Description:
  382. This routine returns the stack and/or frame pointer registers from a
  383. given registers union.
  384. Arguments:
  385. Context - Supplies a pointer to the application context.
  386. Registers - Supplies a pointer to the filled out registers union.
  387. StackPointer - Supplies an optional pointer where the stack register value
  388. will be returned.
  389. FramePointer - Supplies an optional pointer where teh stack frame base
  390. register value will be returned.
  391. Return Value:
  392. None.
  393. --*/
  394. ULONGLONG
  395. DbgGetPc (
  396. PDEBUGGER_CONTEXT Context,
  397. PREGISTERS_UNION Registers
  398. );
  399. /*++
  400. Routine Description:
  401. This routine returns the value of the program counter (instruction pointer)
  402. register in the given registers union.
  403. Arguments:
  404. Context - Supplies a pointer to the application context.
  405. Registers - Supplies an optional pointer to the filled out registers union.
  406. If NULL, then the registers from the current frame will be used.
  407. Return Value:
  408. Returns the instruction pointer member from the given registers.
  409. --*/
  410. VOID
  411. DbgSetPc (
  412. PDEBUGGER_CONTEXT Context,
  413. PREGISTERS_UNION Registers,
  414. ULONGLONG Value
  415. );
  416. /*++
  417. Routine Description:
  418. This routine sets the value of the program counter (instruction pointer)
  419. register in the given registers union.
  420. Arguments:
  421. Context - Supplies a pointer to the application context.
  422. Registers - Supplies an optional pointer to the filled out registers union.
  423. If NULL, then the registers from the current frame will be used.
  424. Value - Supplies the new value to set.
  425. Return Value:
  426. None.
  427. --*/
  428. INT
  429. DbgGetMemberOffset (
  430. PTYPE_SYMBOL StructureType,
  431. PSTR FieldName,
  432. PULONG FieldOffset,
  433. PULONG FieldSize
  434. );
  435. /*++
  436. Routine Description:
  437. This routine returns the given field's offset (in bits) within the
  438. given structure.
  439. Arguments:
  440. StructureType - Supplies a pointer to a symbol structure type.
  441. FieldName - Supplies a string containing the name of the field whose offset
  442. will be returned.
  443. FieldOffset - Supplies a pointer that will receive the bit offset of the
  444. given field name within the given structure.
  445. FieldSize - Supplies a pointer that will receive the size of the field in
  446. bits.
  447. Return Value:
  448. 0 on success.
  449. ENOENT if no such field name exists.
  450. Other error codes on other errors.
  451. --*/
  452. INT
  453. DbgGetTypeByName (
  454. PDEBUGGER_CONTEXT Context,
  455. PSTR TypeName,
  456. PTYPE_SYMBOL *Type
  457. );
  458. /*++
  459. Routine Description:
  460. This routine finds a type symbol object by its type name.
  461. Arguments:
  462. Context - Supplies a pointer to the application context.
  463. TypeName - Supplies a pointer to the string containing the name of the
  464. type to find. This can be prefixed with an module name if needed.
  465. Type - Supplies a pointer where a pointer to the type will be returned.
  466. Return Value:
  467. 0 on success.
  468. ENOENT if no type with the given name was found.
  469. Returns an error number on failure.
  470. --*/
  471. INT
  472. DbgReadIntegerMember (
  473. PDEBUGGER_CONTEXT Context,
  474. PTYPE_SYMBOL Type,
  475. PSTR MemberName,
  476. ULONGLONG Address,
  477. PVOID Data,
  478. ULONG DataSize,
  479. PULONGLONG Value
  480. );
  481. /*++
  482. Routine Description:
  483. This routine reads an integer sized member out of an already read-in
  484. structure.
  485. Arguments:
  486. Context - Supplies a pointer to the application context.
  487. Type - Supplies a pointer to the type of the data.
  488. MemberName - Supplies a pointer to the member name.
  489. Address - Supplies the address where the data was obtained.
  490. Data - Supplies a pointer to the data contents.
  491. DataSize - Supplies the size of the data buffer in bytes.
  492. Value - Supplies a pointer where the value will be returned on success.
  493. Return Value:
  494. 0 on success.
  495. Returns an error number on failure.
  496. --*/
  497. INT
  498. DbgReadTypeByName (
  499. PDEBUGGER_CONTEXT Context,
  500. ULONGLONG Address,
  501. PSTR TypeName,
  502. PTYPE_SYMBOL *FinalType,
  503. PVOID *Data,
  504. PULONG DataSize
  505. );
  506. /*++
  507. Routine Description:
  508. This routine reads in data from the target for a specified type, which is
  509. given as a string.
  510. Arguments:
  511. Context - Supplies a pointer to the application context.
  512. Address - Supplies a target address pointer where the data resides.
  513. TypeName - Supplies a pointer to a string containing the type name to get.
  514. This should start with a type name, and can use dot '.' notation to
  515. specify field members, and array[] notation to specify dereferences.
  516. FinalType - Supplies a pointer where the final type symbol will be returned
  517. on success.
  518. Data - Supplies a pointer where the data will be returned on success. The
  519. caller is responsible for freeing this data when finished.
  520. DataSize - Supplies a pointer where the size of the data in bytes will be
  521. returned.
  522. Return Value:
  523. 0 on success.
  524. Returns an error number on failure.
  525. --*/
  526. INT
  527. DbgReadType (
  528. PDEBUGGER_CONTEXT Context,
  529. ULONGLONG Address,
  530. PTYPE_SYMBOL Type,
  531. PVOID *Data,
  532. PULONG DataSize
  533. );
  534. /*++
  535. Routine Description:
  536. This routine reads in data from the target for a specified type.
  537. Arguments:
  538. Context - Supplies a pointer to the application context.
  539. Address - Supplies a target address pointer where the data resides.
  540. Type - Supplies a pointer to the type symbol to get.
  541. Data - Supplies a pointer where the data will be returned on success. The
  542. caller is responsible for freeing this data when finished.
  543. DataSize - Supplies a pointer where the size of the data in bytes will be
  544. returned.
  545. Return Value:
  546. 0 on success.
  547. Returns an error number on failure.
  548. --*/
  549. INT
  550. DbgPrintTypeMember (
  551. PDEBUGGER_CONTEXT Context,
  552. ULONGLONG Address,
  553. PVOID Data,
  554. ULONG DataSize,
  555. PTYPE_SYMBOL Type,
  556. PSTR MemberName,
  557. ULONG SpaceLevel,
  558. ULONG RecursionCount
  559. );
  560. /*++
  561. Routine Description:
  562. This routine prints a member of a structure or union whose contents have
  563. already been read in.
  564. Arguments:
  565. Context - Supplies a pointer to the application context.
  566. Address - Supplies the address where this data came from.
  567. Data - Supplies a pointer to the data contents.
  568. DataSize - Supplies the size of the data contents buffer in bytes.
  569. Type - Supplies a pointer to the structure type.
  570. MemberName - Supplies the name of the member to print.
  571. SpaceLevel - Supplies the number of spaces worth of indentation to print
  572. for subsequent lines.
  573. RecursionCount - Supplies the number of substructures to recurse into.
  574. Return Value:
  575. 0 on success.
  576. Returns an error number on failure.
  577. --*/