home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / vc98 / include / wdbgexts.h < prev    next >
C/C++ Source or Header  |  1998-04-25  |  16KB  |  626 lines

  1. /*++
  2.  
  3. Copyright 1992 - 1998 Microsoft Corporation
  4.  
  5. Module Name:
  6.  
  7.     wdbgexts.h
  8.  
  9. Abstract:
  10.  
  11.     This file contains the necessary prototypes and data types for a user
  12.     to write a debugger extension DLL.  This header file is also included
  13.     by the NT debuggers (WINDBG & KD).
  14.  
  15.     This header file must be included after "windows.h" and "imagehlp.h".
  16.  
  17.     Please see the NT DDK documentation for specific information about
  18.     how to write your own debugger extension DLL.
  19.  
  20. Environment:
  21.  
  22.     Win32 only.
  23.  
  24. Revision History:
  25.  
  26. --*/
  27.  
  28. #ifndef _WDBGEXTS_
  29. #define _WDBGEXTS_
  30.  
  31. #ifdef __cplusplus
  32. extern "C" {
  33. #endif
  34.  
  35. #if !defined(WDBGAPI)
  36. #define WDBGAPI __stdcall
  37. #endif
  38.  
  39. #ifndef _WINDEF_
  40. typedef CONST void far *LPCVOID;
  41. #endif
  42.  
  43.  
  44. typedef
  45. VOID
  46. (WDBGAPI*PWINDBG_OUTPUT_ROUTINE)(
  47.     PCSTR lpFormat,
  48.     ...
  49.     );
  50.  
  51. typedef
  52. ULONG
  53. (WDBGAPI*PWINDBG_GET_EXPRESSION)(
  54.     PCSTR lpExpression
  55.     );
  56.  
  57. typedef
  58. VOID
  59. (WDBGAPI*PWINDBG_GET_SYMBOL)(
  60.     PVOID   offset,
  61.     PUCHAR  pchBuffer,
  62.     PULONG  pDisplacement
  63.     );
  64.  
  65. typedef
  66. ULONG
  67. (WDBGAPI*PWINDBG_DISASM)(
  68.     PULONG lpOffset,
  69.     PCSTR  lpBuffer,
  70.     ULONG  fShowEffectiveAddress
  71.     );
  72.  
  73. typedef
  74. ULONG
  75. (WDBGAPI*PWINDBG_CHECK_CONTROL_C)(
  76.     VOID
  77.     );
  78.  
  79. typedef
  80. ULONG
  81. (WDBGAPI*PWINDBG_READ_PROCESS_MEMORY_ROUTINE)(
  82.     ULONG  offset,
  83.     PVOID  lpBuffer,
  84.     ULONG  cb,
  85.     PULONG lpcbBytesRead
  86.     );
  87.  
  88. typedef
  89. ULONG
  90. (WDBGAPI*PWINDBG_WRITE_PROCESS_MEMORY_ROUTINE)(
  91.     ULONG   offset,
  92.     LPCVOID lpBuffer,
  93.     ULONG   cb,
  94.     PULONG  lpcbBytesWritten
  95.     );
  96.  
  97. typedef
  98. ULONG
  99. (WDBGAPI*PWINDBG_GET_THREAD_CONTEXT_ROUTINE)(
  100.     ULONG       Processor,
  101.     PCONTEXT    lpContext,
  102.     ULONG       cbSizeOfContext
  103.     );
  104.  
  105. typedef
  106. ULONG
  107. (WDBGAPI*PWINDBG_SET_THREAD_CONTEXT_ROUTINE)(
  108.     ULONG       Processor,
  109.     PCONTEXT    lpContext,
  110.     ULONG       cbSizeOfContext
  111.     );
  112.  
  113. typedef
  114. ULONG
  115. (WDBGAPI*PWINDBG_IOCTL_ROUTINE)(
  116.     USHORT   IoctlType,
  117.     PVOID    lpvData,
  118.     ULONG    cbSize
  119.     );
  120.  
  121. typedef
  122. ULONG
  123. (WDBGAPI*PWINDBG_OLDKD_READ_PHYSICAL_MEMORY)(
  124.     LARGE_INTEGER    address,
  125.     PVOID            buffer,
  126.     ULONG            count,
  127.     PULONG           bytesread
  128.     );
  129.  
  130. typedef
  131. ULONG
  132. (WDBGAPI*PWINDBG_OLDKD_WRITE_PHYSICAL_MEMORY)(
  133.     LARGE_INTEGER    address,
  134.     PVOID            buffer,
  135.     ULONG            length,
  136.     PULONG           byteswritten
  137.     );
  138.  
  139.  
  140. typedef struct _tagEXTSTACKTRACE {
  141.     ULONG       FramePointer;
  142.     ULONG       ProgramCounter;
  143.     ULONG       ReturnAddress;
  144.     ULONG       Args[4];
  145. } EXTSTACKTRACE, *PEXTSTACKTRACE;
  146.  
  147.  
  148. typedef
  149. ULONG
  150. (*PWINDBG_STACKTRACE_ROUTINE)(
  151.     ULONG             FramePointer,
  152.     ULONG             StackPointer,
  153.     ULONG             ProgramCounter,
  154.     PEXTSTACKTRACE    StackFrames,
  155.     ULONG             Frames
  156.     );
  157.  
  158. typedef struct _WINDBG_EXTENSION_APIS {
  159.     ULONG                                  nSize;
  160.     PWINDBG_OUTPUT_ROUTINE                 lpOutputRoutine;
  161.     PWINDBG_GET_EXPRESSION                 lpGetExpressionRoutine;
  162.     PWINDBG_GET_SYMBOL                     lpGetSymbolRoutine;
  163.     PWINDBG_DISASM                         lpDisasmRoutine;
  164.     PWINDBG_CHECK_CONTROL_C                lpCheckControlCRoutine;
  165.     PWINDBG_READ_PROCESS_MEMORY_ROUTINE    lpReadProcessMemoryRoutine;
  166.     PWINDBG_WRITE_PROCESS_MEMORY_ROUTINE   lpWriteProcessMemoryRoutine;
  167.     PWINDBG_GET_THREAD_CONTEXT_ROUTINE     lpGetThreadContextRoutine;
  168.     PWINDBG_SET_THREAD_CONTEXT_ROUTINE     lpSetThreadContextRoutine;
  169.     PWINDBG_IOCTL_ROUTINE                  lpIoctlRoutine;
  170.     PWINDBG_STACKTRACE_ROUTINE             lpStackTraceRoutine;
  171. } WINDBG_EXTENSION_APIS, *PWINDBG_EXTENSION_APIS;
  172.  
  173. typedef struct _WINDBG_OLD_EXTENSION_APIS {
  174.     ULONG                                  nSize;
  175.     PWINDBG_OUTPUT_ROUTINE                 lpOutputRoutine;
  176.     PWINDBG_GET_EXPRESSION                 lpGetExpressionRoutine;
  177.     PWINDBG_GET_SYMBOL                     lpGetSymbolRoutine;
  178.     PWINDBG_DISASM                         lpDisasmRoutine;
  179.     PWINDBG_CHECK_CONTROL_C                lpCheckControlCRoutine;
  180. } WINDBG_OLD_EXTENSION_APIS, *PWINDBG_OLD_EXTENSION_APIS;
  181.  
  182. typedef struct _WINDBG_OLDKD_EXTENSION_APIS {
  183.     ULONG                                  nSize;
  184.     PWINDBG_OUTPUT_ROUTINE                 lpOutputRoutine;
  185.     PWINDBG_GET_EXPRESSION                 lpGetExpressionRoutine;
  186.     PWINDBG_GET_SYMBOL                     lpGetSymbolRoutine;
  187.     PWINDBG_DISASM                         lpDisasmRoutine;
  188.     PWINDBG_CHECK_CONTROL_C                lpCheckControlCRoutine;
  189.     PWINDBG_READ_PROCESS_MEMORY_ROUTINE    lpReadVirtualMemRoutine;
  190.     PWINDBG_WRITE_PROCESS_MEMORY_ROUTINE   lpWriteVirtualMemRoutine;
  191.     PWINDBG_OLDKD_READ_PHYSICAL_MEMORY     lpReadPhysicalMemRoutine;
  192.     PWINDBG_OLDKD_WRITE_PHYSICAL_MEMORY    lpWritePhysicalMemRoutine;
  193. } WINDBG_OLDKD_EXTENSION_APIS, *PWINDBG_OLDKD_EXTENSION_APIS;
  194.  
  195. typedef
  196. VOID
  197. (WDBGAPI*PWINDBG_OLD_EXTENSION_ROUTINE)(
  198.     HANDLE                  hCurrentProcess,
  199.     HANDLE                  hCurrentThread,
  200.     ULONG                   dwCurrentPc,
  201.     PWINDBG_EXTENSION_APIS  lpExtensionApis,
  202.     PCSTR                   lpArgumentString
  203.     );
  204.  
  205. typedef
  206. VOID
  207. (WDBGAPI*PWINDBG_EXTENSION_ROUTINE)(
  208.     HANDLE                  hCurrentProcess,
  209.     HANDLE                  hCurrentThread,
  210.     ULONG                   dwCurrentPc,
  211.     ULONG                   dwProcessor,
  212.     PCSTR                   lpArgumentString
  213.     );
  214.  
  215. typedef
  216. VOID
  217. (WDBGAPI*PWINDBG_OLDKD_EXTENSION_ROUTINE)(
  218.     ULONG                        dwCurrentPc,
  219.     PWINDBG_OLDKD_EXTENSION_APIS lpExtensionApis,
  220.     PCSTR                        lpArgumentString
  221.     );
  222.  
  223. typedef
  224. VOID
  225. (WDBGAPI*PWINDBG_EXTENSION_DLL_INIT)(
  226.     PWINDBG_EXTENSION_APIS lpExtensionApis,
  227.     USHORT                 MajorVersion,
  228.     USHORT                 MinorVersion
  229.     );
  230.  
  231. typedef
  232. ULONG
  233. (WDBGAPI*PWINDBG_CHECK_VERSION)(
  234.     VOID
  235.     );
  236.  
  237. #define EXT_API_VERSION_NUMBER 5
  238.  
  239. typedef struct EXT_API_VERSION {
  240.     USHORT  MajorVersion;
  241.     USHORT  MinorVersion;
  242.     USHORT  Revision;
  243.     USHORT  Reserved;
  244. } EXT_API_VERSION, *LPEXT_API_VERSION;
  245.  
  246. typedef
  247. LPEXT_API_VERSION
  248. (WDBGAPI*PWINDBG_EXTENSION_API_VERSION)(
  249.     VOID
  250.     );
  251.  
  252. #define IG_KD_CONTEXT               1
  253. #define IG_READ_CONTROL_SPACE       2
  254. #define IG_WRITE_CONTROL_SPACE      3
  255. #define IG_READ_IO_SPACE            4
  256. #define IG_WRITE_IO_SPACE           5
  257. #define IG_READ_PHYSICAL            6
  258. #define IG_WRITE_PHYSICAL           7
  259. #define IG_READ_IO_SPACE_EX         8
  260. #define IG_WRITE_IO_SPACE_EX        9
  261. #define IG_KSTACK_HELP             10   // obsolete
  262. #define IG_SET_THREAD              11
  263. #define IG_READ_MSR                12
  264. #define IG_WRITE_MSR               13
  265. #define IG_GET_DEBUGGER_DATA       14
  266. #define IG_GET_KERNEL_VERSION      15
  267. #define IG_RELOAD_SYMBOLS          16
  268. #define IG_GET_SET_SYMPATH         17
  269. #define IG_GET_EXCEPTION_RECORD    18
  270.  
  271. typedef struct _tagPROCESSORINFO {
  272.     USHORT      Processor;                // current processor
  273.     USHORT      NumberProcessors;         // total number of processors
  274. } PROCESSORINFO, *PPROCESSORINFO;
  275.  
  276. typedef struct _tagREADCONTROLSPACE {
  277.     USHORT      Processor;
  278.     ULONG       Address;
  279.     ULONG       BufLen;
  280.     UCHAR       Buf[1];
  281. } READCONTROLSPACE, *PREADCONTROLSPACE;
  282.  
  283. typedef struct _tagIOSPACE {
  284.     ULONG       Address;
  285.     ULONG       Length;                   // 1, 2, or 4 bytes
  286.     ULONG       Data;
  287. } IOSPACE, *PIOSPACE;
  288.  
  289. typedef struct _tagIOSPACE_EX {
  290.     ULONG       Address;
  291.     ULONG       Length;                   // 1, 2, or 4 bytes
  292.     ULONG       Data;
  293.     ULONG       InterfaceType;
  294.     ULONG       BusNumber;
  295.     ULONG       AddressSpace;
  296. } IOSPACE_EX, *PIOSPACE_EX;
  297.  
  298. typedef struct _tagPHYSICAL {
  299.     LARGE_INTEGER          Address;
  300.     ULONG                  BufLen;
  301.     UCHAR                  Buf[1];
  302. } PHYSICAL, *PPHYSICAL;
  303.  
  304. typedef struct _tagREAD_WRITE_MSR {
  305.     ULONG       Msr;
  306.     LONGLONG    Value;
  307. } READ_WRITE_MSR, *PREAD_WRITE_MSR;
  308.  
  309. typedef struct _tagGET_SET_SYMPATH {
  310.     PCSTR       Args;       // args to !reload command
  311.     PSTR        Result;     // returns new path
  312.     int         Length;     // Length of result buffer
  313. } GET_SET_SYMPATH, *PGET_SET_SYMPATH;
  314.  
  315. #ifdef __cplusplus
  316. #define CPPMOD extern "C"
  317. #else
  318. #define CPPMOD
  319. #endif
  320.  
  321.  
  322. #define DECLARE_API(s)                             \
  323.     CPPMOD VOID                                    \
  324.     s(                                             \
  325.         HANDLE                 hCurrentProcess,    \
  326.         HANDLE                 hCurrentThread,     \
  327.         ULONG                  dwCurrentPc,        \
  328.         ULONG                  dwProcessor,        \
  329.         PCSTR                  args                \
  330.      )
  331.  
  332. #ifndef NOEXTAPI
  333.  
  334. #define dprintf          (ExtensionApis.lpOutputRoutine)
  335. #define GetExpression    (ExtensionApis.lpGetExpressionRoutine)
  336. #define GetSymbol        (ExtensionApis.lpGetSymbolRoutine)
  337. #define Disassm          (ExtensionApis.lpDisasmRoutine)
  338. #define CheckControlC    (ExtensionApis.lpCheckControlCRoutine)
  339. #define ReadMemory       (ExtensionApis.lpReadProcessMemoryRoutine)
  340. #define WriteMemory      (ExtensionApis.lpWriteProcessMemoryRoutine)
  341. #define GetContext       (ExtensionApis.lpGetThreadContextRoutine)
  342. #define SetContext       (ExtensionApis.lpSetThreadContextRoutine)
  343. #define Ioctl            (ExtensionApis.lpIoctlRoutine)
  344. #define StackTrace       (ExtensionApis.lpStackTraceRoutine)
  345.  
  346.  
  347. #define GetKdContext(ppi) \
  348.     Ioctl( IG_KD_CONTEXT, (PVOID)ppi, sizeof(*ppi) )
  349.  
  350. //
  351. // BOOL
  352. // GetDebuggerData(
  353. //     ULONG Tag,
  354. //     PVOID Buf,
  355. //     ULONG Size
  356. //     )
  357. //
  358. /*++
  359.  
  360. Routine Description:
  361.  
  362.  
  363.  
  364. Arguments:
  365.  
  366.  
  367.  
  368. Return Value:
  369.  
  370.  
  371.  
  372. --*/
  373.  
  374. #define GetDebuggerData(TAG, BUF, SIZE)                             \
  375.       ( (((PDBGKD_DEBUG_DATA_HEADER)(BUF))->OwnerTag = (TAG)),      \
  376.         (((PDBGKD_DEBUG_DATA_HEADER)(BUF))->Size = (SIZE)),         \
  377.         Ioctl( IG_GET_DEBUGGER_DATA, (PVOID)(BUF), (SIZE) ) )
  378.  
  379. extern WINDBG_EXTENSION_APIS ExtensionApis;
  380.  
  381. __inline VOID
  382. ReadControlSpace(
  383.     USHORT  processor,
  384.     ULONG   address,
  385.     PVOID   buf,
  386.     ULONG   size
  387.     )
  388. {
  389.     PREADCONTROLSPACE prc;
  390.     prc = (PREADCONTROLSPACE)LocalAlloc(LPTR, sizeof(*prc) + size );
  391.     ZeroMemory( prc->Buf, size );
  392.     prc->Processor = processor;
  393.     prc->Address = (ULONG)address;
  394.     prc->BufLen = size;
  395.     Ioctl( IG_READ_CONTROL_SPACE, (PVOID)prc, sizeof(*prc) + size );
  396.     CopyMemory( buf, prc->Buf, size );
  397.     LocalFree( prc );
  398. }
  399.  
  400. __inline VOID
  401. ReadIoSpace(
  402.     ULONG   address,
  403.     PULONG  data,
  404.     PULONG  size
  405.     )
  406. {
  407.     IOSPACE is;
  408.     is.Address = (ULONG)address;
  409.     is.Length = *size;
  410.     Ioctl( IG_READ_IO_SPACE, (PVOID)&is, sizeof(is) );
  411.     memcpy(data, &is.Data, is.Length);
  412.     *size = is.Length;
  413. }
  414.  
  415. __inline VOID
  416. WriteIoSpace(
  417.     ULONG   address,
  418.     ULONG   data,
  419.     PULONG  size
  420.     )
  421. {
  422.     IOSPACE is;
  423.     is.Address = (ULONG)address;
  424.     is.Length = *size;
  425.     is.Data = data;
  426.     Ioctl( IG_WRITE_IO_SPACE, (PVOID)&is, sizeof(is) );
  427.     *size = is.Length;
  428. }
  429.  
  430. __inline VOID
  431. ReadIoSpaceEx(
  432.     ULONG   address,
  433.     PULONG  data,
  434.     PULONG  size,
  435.     ULONG   interfacetype,
  436.     ULONG   busnumber,
  437.     ULONG   addressspace
  438.     )
  439. {
  440.     IOSPACE_EX is;
  441.     is.Address = (ULONG)address;
  442.     is.Length = *size;
  443.     is.Data = 0;
  444.     is.InterfaceType = interfacetype;
  445.     is.BusNumber = busnumber;
  446.     is.AddressSpace = addressspace;
  447.     Ioctl( IG_READ_IO_SPACE_EX, (PVOID)&is, sizeof(is) );
  448.     *data = is.Data;
  449.     *size = is.Length;
  450. }
  451.  
  452. __inline VOID
  453. WriteIoSpaceEx(
  454.     ULONG   address,
  455.     ULONG   data,
  456.     PULONG  size,
  457.     ULONG   interfacetype,
  458.     ULONG   busnumber,
  459.     ULONG   addressspace
  460.     )
  461. {
  462.     IOSPACE_EX is;
  463.     is.Address = (ULONG)address;
  464.     is.Length = *size;
  465.     is.Data = data;
  466.     is.InterfaceType = interfacetype;
  467.     is.BusNumber = busnumber;
  468.     is.AddressSpace = addressspace;
  469.     Ioctl( IG_WRITE_IO_SPACE_EX, (PVOID)&is, sizeof(is) );
  470.     *size = is.Length;
  471. }
  472.  
  473. __inline VOID
  474. ReadPhysical(
  475.     LARGE_INTEGER       address,
  476.     PVOID               buf,
  477.     ULONG               size,
  478.     PULONG              sizer
  479.     )
  480. {
  481.     PPHYSICAL phy;
  482.     phy = (PPHYSICAL)LocalAlloc(LPTR,  sizeof(*phy) + size );
  483.     ZeroMemory( phy->Buf, size );
  484.     phy->Address = address;
  485.     phy->BufLen = size;
  486.     Ioctl( IG_READ_PHYSICAL, (PVOID)phy, sizeof(*phy) + size );
  487.     *sizer = phy->BufLen;
  488.     CopyMemory( buf, phy->Buf, *sizer );
  489.     LocalFree( phy );
  490. }
  491.  
  492. __inline VOID
  493. WritePhysical(
  494.     LARGE_INTEGER       address,
  495.     PVOID               buf,
  496.     ULONG               size,
  497.     PULONG              sizew
  498.     )
  499. {
  500.     PPHYSICAL phy;
  501.     phy = (PPHYSICAL)LocalAlloc(LPTR, sizeof(*phy) + size );
  502.     ZeroMemory( phy->Buf, size );
  503.     phy->Address = address;
  504.     phy->BufLen = size;
  505.     CopyMemory( phy->Buf, buf, size );
  506.     Ioctl( IG_WRITE_PHYSICAL, (PVOID)phy, sizeof(*phy) + size );
  507.     *sizew = phy->BufLen;
  508.     LocalFree( phy );
  509. }
  510.  
  511. __inline VOID
  512. SetThreadForOperation(
  513.     PULONG Thread
  514.     )
  515. {
  516.     Ioctl(IG_SET_THREAD, (PVOID)Thread, sizeof(ULONG));
  517. }
  518.  
  519. __inline VOID
  520. ReadMsr(
  521.     ULONG       MsrReg,
  522.     ULONGLONG   *MsrValue
  523.     )
  524. {
  525.     PREAD_WRITE_MSR msr;
  526.     LARGE_INTEGER li;
  527.  
  528.     msr = (PREAD_WRITE_MSR)LocalAlloc(LPTR,  sizeof(*msr));
  529.     msr->Msr = MsrReg;
  530.     Ioctl( IG_READ_MSR, (PVOID)msr, sizeof(*msr) );
  531.  
  532.     *MsrValue = msr->Value;
  533.     LocalFree( msr );
  534. }
  535.  
  536. __inline VOID
  537. WriteMsr(
  538.     ULONG       MsrReg,
  539.     ULONGLONG   MsrValue
  540.     )
  541. {
  542.     PREAD_WRITE_MSR msr;
  543.  
  544.     msr = (PREAD_WRITE_MSR)LocalAlloc(LPTR,  sizeof(*msr));
  545.     msr->Msr = MsrReg;
  546.     msr->Value = MsrValue;
  547.     Ioctl( IG_WRITE_MSR, (PVOID)msr, sizeof(*msr) );
  548.     LocalFree( msr );
  549. }
  550.  
  551. __inline VOID
  552. ReloadSymbols(
  553.     IN PSTR Arg OPTIONAL
  554.     )
  555. /*++
  556.  
  557. Routine Description:
  558.  
  559.     Calls the debugger to reload symbols.
  560.  
  561. Arguments:
  562.  
  563.     Args - Supplies the tail of a !reload command string.
  564.  
  565.         !reload [flags] [module[=address]]
  566.         flags:   /n  do not load from usermode list
  567.                  /u  unload symbols, no reload
  568.                  /v  verbose
  569.  
  570.         A value of NULL is equivalent to an empty string
  571.  
  572. Return Value:
  573.  
  574.     None
  575.  
  576. --*/
  577. {
  578.     Ioctl(IG_RELOAD_SYMBOLS, (PVOID)Arg, Arg?(strlen(Arg)+1):0);
  579. }
  580.  
  581. __inline VOID
  582. GetSetSympath(
  583.     IN PSTR Arg,
  584.     OUT PSTR Result OPTIONAL,
  585.     IN int Length
  586.     )
  587. /*++
  588.  
  589. Routine Description:
  590.  
  591.     Calls the debugger to set or retrieve symbol search path.
  592.  
  593. Arguments:
  594.  
  595.     Arg - Supplies new search path.  If Arg is NULL or string is empty,
  596.             the search path is not changed and the current setting is
  597.             returned in Result.  When the symbol search path is changed,
  598.             a call to ReloadSymbols is made implicitly.
  599.  
  600.     Result - OPTIONAL Returns the symbol search path setting.
  601.  
  602.     Length - Supplies the size of the buffer supplied by Result.
  603.  
  604. Return Value:
  605.  
  606.     None
  607.  
  608. --*/
  609. {
  610.     GET_SET_SYMPATH gss;
  611.     gss.Args = Arg;
  612.     gss.Result = Result;
  613.     gss.Length = Length;
  614.     Ioctl(IG_GET_SET_SYMPATH, (PVOID)&gss, sizeof(gss));
  615. }
  616.  
  617.  
  618. #endif
  619.  
  620.  
  621. #ifdef __cplusplus
  622. }
  623. #endif
  624.  
  625. #endif // _WDBGEXTS_
  626.