home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / winbase / debug / wdbgexts / wdbgexts.c < prev    next >
C/C++ Source or Header  |  1997-10-05  |  12KB  |  295 lines

  1. /**********************************************************************\
  2. *                                                                      *
  3. * Copyright (c) 1992-1997  Microsoft Corporation                      *
  4. *                                                                      *
  5. * Module Name:                                                         *
  6. *                                                                      *
  7. *    wdbgexts.c                                                        *
  8. *                                                                      *
  9. * Abstract:                                                            *
  10. *                                                                      *
  11. * This file is a ported version of ntsdexts.c, supporting remote       *
  12. * operations in WinDbg command window.                       *
  13. *                                                                      *
  14. * By including wdbgexts.h, an extension has 4 more apis available,     *
  15. * beyond those available to ntsd extensions:                   *
  16. *                                                                      *
  17. *    PWINDBG_READ_PROCESS_MEMORY_ROUTINE lpReadProcessMemoryRoutine;   *
  18. *    PWINDBG_WRITE_PROCESS_MEMORY_ROUTINE lpWriteProcessMemoryRoutine; *
  19. *    PWINDBG_GET_THREAD_CONTEXT_ROUTINE lpGetThreadContextRoutine;     *
  20. *    PWINDBG_SET_THREAD_CONTEXT_ROUTINE lpSetThreadContextRoutine;     *
  21. *                                       *
  22. * The apis available to NTSD and WINDBG extensions are:            *
  23. *                                       *
  24. *    PNTSD_OUTPUT_ROUTINE lpOutputRoutine;                   *
  25. *    PNTSD_GET_EXPRESSION lpGetExpressionRoutine;               *
  26. *    PNTSD_GET_SYMBOL lpGetSymbolRoutine;                   *
  27. *    PNTSD_DISASM lpDisasmRoutine;                       *
  28. *    PNTSD_CHECK_CONTROL_C lpCheckControlCRoutine;               *
  29. *                                       *
  30. *                                                                      *
  31. \**********************************************************************/
  32.  
  33. #include <windows.h>
  34. #include <string.h>
  35. #include <windbgkd.h>
  36. #include <ntsdexts.h>
  37. #include <wdbgexts.h>
  38.  
  39. #define MAXLEN 80
  40.  
  41. CHAR  igrepLastPattern[256];
  42. DWORD igrepSearchStartAddress;
  43. DWORD igrepLastPc;
  44.  
  45.  
  46. /**********************************************************************\
  47. *                                                                      *
  48. * igrep( )                                                             *
  49. *                                                                      *
  50. * Description:                                                         *
  51. *                                                                      *
  52. *    This function is called as a WINDBG extension to grep the         *
  53. *    instruction stream for a particular pattern.                      *
  54. *                                                                      *
  55. * Arguments:                                                           *
  56. *                                                                      *
  57. *    hCurrentProcess - Supplies a handle to the current process (at    *
  58. *        the time the extension was called).                           *
  59. *                                                                      *
  60. *    hCurrentThread - Supplies a handle to the current thread (at      *
  61. *        the time the extension was called).                           *
  62. *                                                                      *
  63. *    CurrentPc - Supplies the current pc at the time the extension is  *
  64. *        called.                                                       *
  65. *                                                                      *
  66. *    lpExtensionApis - Supplies the address of the functions callable  *
  67. *        by this extension.                                            *
  68. *                                                                      *
  69. *    lpArgumentString   Supplies the pattern and expression for this   *
  70. *        command.                                                      *
  71. *                                                                      *
  72. * Return Value:                                                        *
  73. *                                                                      *
  74. *    None.                                                             *
  75. \**********************************************************************/
  76.  
  77. VOID igrep (
  78.     HANDLE hCurrentProcess,
  79.     HANDLE hCurrentThread,
  80.     DWORD dwCurrentPc,
  81.     PWINDBG_EXTENSION_APIS lpExtensionApis,
  82.     LPSTR lpArgumentString
  83.     )
  84.  
  85. {
  86.     DWORD dwNextGrepAddr;
  87.     DWORD dwCurrGrepAddr;
  88.     CHAR SourceLine[256];
  89.     BOOL NewPc;
  90.     DWORD d;
  91.     PNTSD_OUTPUT_ROUTINE lpOutputRoutine;
  92.     PNTSD_GET_EXPRESSION lpGetExpressionRoutine;
  93.     PNTSD_GET_SYMBOL lpGetSymbolRoutine;
  94.     PNTSD_DISASM lpDisasmRoutine;
  95.     PNTSD_CHECK_CONTROL_C lpCheckControlCRoutine;
  96.     LPSTR pc;
  97.     LPSTR Pattern;
  98.     LPSTR Expression;
  99.     CHAR Symbol[64];
  100.     DWORD Displacement;
  101.  
  102.     UNREFERENCED_PARAMETER( hCurrentProcess );
  103.     UNREFERENCED_PARAMETER( hCurrentThread );
  104.  
  105.     lpOutputRoutine = lpExtensionApis->lpOutputRoutine;
  106.     lpGetExpressionRoutine = lpExtensionApis->lpGetExpressionRoutine;
  107.     lpGetSymbolRoutine = lpExtensionApis->lpGetSymbolRoutine;
  108.     lpDisasmRoutine = (PNTSD_DISASM) lpExtensionApis->lpDisasmRoutine;
  109.     lpCheckControlCRoutine = lpExtensionApis->lpCheckControlCRoutine;
  110.  
  111.     if( igrepLastPc && igrepLastPc == dwCurrentPc ) {
  112.         NewPc = FALSE;
  113.     }
  114.     else {
  115.         igrepLastPc = dwCurrentPc;
  116.         NewPc = TRUE;
  117.     }
  118.  
  119.     //
  120.     // check for pattern.
  121.     //
  122.  
  123.     pc = lpArgumentString;
  124.     Pattern = NULL;
  125.     Expression = NULL;
  126.     if( *pc ) {
  127.         Pattern = pc;
  128.         while( *pc > ' ' ) {
  129.             pc++;
  130.     }
  131.  
  132.         //
  133.         // check for an expression
  134.         //
  135.  
  136.         if( *pc != '\0' ) {
  137.             *pc = '\0';
  138.             pc++;
  139.             if( *pc <= ' ' ) {
  140.                 while (*pc <= ' ') {
  141.                     pc++;
  142.                 }
  143.             }
  144.             if( *pc ) {
  145.                 Expression = pc;
  146.             }
  147.         }
  148.     }
  149.  
  150.     if( Pattern ) {
  151.         strcpy(igrepLastPattern,Pattern);
  152.  
  153.         if( Expression ) {
  154.             igrepSearchStartAddress = (lpGetExpressionRoutine)(Expression);
  155.             if( !igrepSearchStartAddress ) {
  156.                 igrepSearchStartAddress = igrepLastPc;
  157.                 return;
  158.             }
  159.         }
  160.         else {
  161.             igrepSearchStartAddress = igrepLastPc;
  162.         }
  163.     }
  164.  
  165.     dwNextGrepAddr = igrepSearchStartAddress;
  166.     dwCurrGrepAddr = dwNextGrepAddr;
  167.     d = (lpDisasmRoutine)(&dwNextGrepAddr,SourceLine,FALSE);
  168.     while( d ) {
  169.         if( strstr(SourceLine,igrepLastPattern) ) {
  170.             igrepSearchStartAddress = dwNextGrepAddr;
  171.         (lpGetSymbolRoutine)((LPVOID)dwCurrGrepAddr,(PUCHAR)Symbol,&Displacement);
  172.             (lpOutputRoutine)("%s",SourceLine);
  173.             return;
  174.         }
  175.         if( (lpCheckControlCRoutine)() ) {
  176.             return;
  177.         }
  178.         dwCurrGrepAddr = dwNextGrepAddr;
  179.         d = (lpDisasmRoutine)(&dwNextGrepAddr,SourceLine,FALSE);
  180.     }
  181. }
  182.  
  183.  
  184. /**********************************************************************\
  185. *                                                                      *
  186. * str( )                                   *
  187. *                                                                      *
  188. * Routine Description:                                                 *
  189. *                                                                      *
  190. *    This function is called as a WINDBG extension to format and dump  *
  191. *    a counted ansi string.                                            *
  192. *                                                                      *
  193. * Arguments:                                                           *
  194. *                                                                      *
  195. *    hCurrentProcess - Supplies a handle to the current process (at    *
  196. *        the time the extension was called).                           *
  197. *                                                                      *
  198. *    hCurrentThread - Supplies a handle to the current thread (at the  *
  199. *        time the extension was called).                               *
  200. *                                                                      *
  201. *    CurrentPc - Supplies the current pc at the time the extension is  *
  202. *        called.                                                       *
  203. *                                                                      *
  204. *    lpExtensionApis - Supplies the address of the functions callable  *
  205. *        by this extension.                                            *
  206. *                                                                      *
  207. *    lpArgumentString - Supplies the asciiz string that describes the  *
  208. *        ansi string to be dumped.                                     *
  209. *                                                                      *
  210. * Return Value:                                                        *
  211. *                                                                      *
  212. *    None.                                                             *
  213. *                                                                      *
  214. \**********************************************************************/
  215.  
  216. VOID str (
  217.     HANDLE hCurrentProcess,
  218.     HANDLE hCurrentThread,
  219.     DWORD dwCurrentPc,
  220.     PWINDBG_EXTENSION_APIS lpExtensionApis,
  221.     LPSTR lpArgumentString
  222.     )
  223.  
  224. {
  225.     CHAR String[MAXLEN];
  226.     size_t Length;
  227.     DWORD dwAddrString;
  228.     CHAR Symbol[64];
  229.     LPSTR StringData;
  230.     DWORD Displacement;
  231.     BOOL b;
  232.     PNTSD_OUTPUT_ROUTINE lpOutputRoutine;
  233.     PNTSD_GET_EXPRESSION lpGetExpressionRoutine;
  234.     PNTSD_GET_SYMBOL lpGetSymbolRoutine;
  235.  
  236.     UNREFERENCED_PARAMETER( hCurrentProcess );
  237.     UNREFERENCED_PARAMETER( hCurrentThread );
  238.     UNREFERENCED_PARAMETER( dwCurrentPc );
  239.  
  240.     lpOutputRoutine = lpExtensionApis->lpOutputRoutine;
  241.     lpGetExpressionRoutine = lpExtensionApis->lpGetExpressionRoutine;
  242.     lpGetSymbolRoutine = lpExtensionApis->lpGetSymbolRoutine;
  243.  
  244.     //
  245.     // Evaluate the argument string to get the address of
  246.     // the string to dump.
  247.     //
  248.  
  249.     dwAddrString = (lpGetExpressionRoutine)(lpArgumentString);
  250.     if (!dwAddrString) {
  251.     (lpOutputRoutine)( "Invalid Expression." );
  252.     return;
  253.     }
  254.  
  255.     //
  256.     // Get the symbolic name of the string
  257.     //
  258.  
  259.     (lpGetSymbolRoutine)((LPVOID)dwAddrString,(PUCHAR)Symbol,&Displacement);
  260.  
  261.     //
  262.     // Read current process memory and handle remote read as well
  263.     //
  264.  
  265.     b = (lpExtensionApis->lpReadProcessMemoryRoutine)(
  266.                                                      dwAddrString,
  267.                              String,
  268.                              MAXLEN,
  269.                                                      NULL
  270.                                                      );
  271.  
  272.     if (!b) {
  273.     (lpOutputRoutine)( "ReadProcessMemory failed." );
  274.     return;
  275.     }
  276.  
  277.     Length = strlen( String );
  278.  
  279.     StringData = (LPSTR)LocalAlloc(LMEM_ZEROINIT,Length+1);
  280.  
  281.     if (!StringData) {
  282.     (lpOutputRoutine)( "LocalAlloc failed. Error = %x", GetLastError());
  283.         return;
  284.     }
  285.  
  286.     (lpOutputRoutine)(
  287.     "String: %s ; %d bytes at %lx\n",
  288.     String,
  289.     Length,
  290.     dwAddrString
  291.         );
  292.  
  293.     LocalFree( StringData );
  294. }
  295.