home *** CD-ROM | disk | FTP | other *** search
/ Windows Graphics Programming / Feng_Yuan_Win32_GDI_DirectX.iso / Samples / Chapt_03 / Fosterer / Host.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-11  |  6.0 KB  |  278 lines

  1. //-----------------------------------------------------------------------------------//
  2. //              Windows Graphics Programming: Win32 GDI and DirectDraw               //
  3. //                             ISBN  0-13-086985-6                                   //
  4. //                                                                                   //
  5. //  Written            by  Yuan, Feng                             www.fengyuan.com   //
  6. //  Copyright (c) 2000 by  Hewlett-Packard Company                www.hp.com         //
  7. //  Published          by  Prentice Hall PTR, Prentice-Hall, Inc. www.phptr.com      //
  8. //                                                                                   //
  9. //  FileName   : host.cpp                                                             //
  10. //  Description: KHost class                                                         //
  11. //  Version    : 1.00.000, May 31, 2000                                              //
  12. //-----------------------------------------------------------------------------------//
  13.  
  14. #define STRICT
  15. #define WIN32_LEAN_AND_MEAN
  16.  
  17. #include <windows.h>
  18. #include <assert.h>
  19. #include <tchar.h>
  20. #include <stdio.h>
  21. #include <stdarg.h>
  22. #include <winioctl.h>
  23. #include <imagehlp.h> 
  24. #include <wdbgexts.h>
  25.  
  26. #include "ImageModule.h"
  27. #include "Host.h"
  28.  
  29.  
  30. ULONG KPeriscopeClient::Read(void * dst, const void * src, unsigned len)
  31. {
  32.     unsigned      cmd[2] = { (unsigned) src, len };
  33.     unsigned long dwRead = 0;
  34.    
  35.     IoControl(IOCTL_PERISCOPE, cmd, sizeof(cmd), dst, len, &dwRead);
  36.    
  37.     return dwRead;
  38. }
  39.  
  40.  
  41. void KHost::WndOutput(HWND hWnd, const char * format, va_list argptr)
  42. {
  43.     char buffer[1024];
  44.     
  45.     vsprintf(buffer, format, argptr);
  46.  
  47.     if ( hWnd )
  48.     {
  49.         SendMessage(hWnd, EM_SETSEL, 0xFFFFFF, 0xFFFFFF);
  50.         SendMessage(hWnd, EM_REPLACESEL, 0, (LPARAM) buffer);
  51.     }
  52.     else
  53.         OutputDebugString(buffer);
  54. }
  55.  
  56.  
  57. void KHost::Log(const char * format, ...)
  58. {
  59.     va_list ap;
  60.  
  61.     va_start(ap, format);
  62.     WndOutput(hwndLog, format, ap);
  63.     va_end(ap);
  64. }
  65.  
  66.  
  67. void KHost::ExtOutput(const char * format, ...)
  68. {
  69.     va_list ap;
  70.     
  71.     va_start(ap, format);
  72.     WndOutput(hwndOutput, format, ap);
  73.     va_end(ap);
  74. }
  75.  
  76.  
  77. unsigned KHost::ExtGetExpression(const char * expr)
  78. {
  79.     if ( (expr==NULL) || strlen(expr)==0 )
  80.     {
  81.         assert(false);
  82.         return 0;
  83.     }
  84.  
  85.     if ( (expr[0]>='0') && (expr[0]<='9') ) // hex number
  86.     {
  87.         DWORD number;
  88.  
  89.         sscanf(expr, "%x", & number);
  90.         return number;
  91.     }
  92.  
  93.     if ( pWin32k )
  94.     {
  95.         const IMAGEHLP_SYMBOL * pis;
  96.         
  97.         if ( expr[0]=='&' )        // skip the first &
  98.             pis = pWin32k->ImageGetSymbol(expr+1);
  99.         else
  100.             pis = pWin32k->ImageGetSymbol(expr);
  101.  
  102.         if ( pis )
  103.         {
  104.             DWORD addr = pis->Address;  // ??? //
  105.         
  106.             Log("GetExpression(%s)=%08lx\n", expr, addr);
  107.             
  108.             return addr;
  109.         }    
  110.     }
  111.  
  112.     ExtOutput("Unknown GetExpression(""%s"")\n", expr); 
  113.     throw "Unknown Expression";
  114.  
  115.     return 0;
  116. }
  117.  
  118.  
  119. bool KHost::ExtReadProcessMemory(const void * address, 
  120.      unsigned * buffer, unsigned  count, 
  121.      unsigned long * bytesread)
  122. {
  123.     if ( pScope )
  124.     {
  125.         ULONG dwRead = 0;
  126.  
  127.         if ( (unsigned) address >= 0x80000000 )
  128.         {
  129.     
  130. /*            if ( (count==264) || (count==1552) )
  131.                 for (int i=4; i<count/4; i++)
  132.                     buffer[i] = i*4;
  133.  
  134.             else
  135. */    
  136.             dwRead = pScope->Read(buffer, address, count);
  137.  
  138. //            if ( count==264 )
  139. //                for (int i=count/4-1; i>(count/4-10); i--)
  140. //                    buffer[i] = i * 4;
  141.         }
  142.         else
  143.     
  144. /*            if ( count==264 )
  145.                 for (int i=0; i<count/4; i++)
  146.                     buffer[i] = 0xff000000+ i*4;
  147.             else
  148. */    
  149.             ReadProcessMemory(hProcess, address, buffer, count, & dwRead);
  150.  
  151.         if ( bytesread )
  152.             * bytesread = dwRead;
  153.         
  154.         if ( (unsigned) address >= 0x80000000 )
  155.             Log("ReadKRam(%08x, %d)=", address, count);
  156.         else
  157.             Log("ReadURam(%x, %08x, %d)=", hProcess, address, count);
  158.             
  159.         int len = min(4, count/4);
  160.  
  161.         for (int i=0; i<len; i++)
  162.             Log("%08x ", buffer[i]);
  163.             
  164.         Log("\n");
  165.  
  166.         return dwRead == count;
  167.     }
  168.     else
  169.     {
  170.         assert(false);
  171.         return false;
  172.     }
  173. }
  174.  
  175. KHost theHost;
  176.  
  177.  
  178. void WDBGAPI ExtOutputRoutine(PCSTR format, ...)
  179. {
  180.     va_list ap;
  181.     va_start(ap, format);
  182.  
  183.     theHost.WndOutput(theHost.hwndOutput, format, ap);
  184.     
  185.     va_end(ap);
  186. }
  187.  
  188.  
  189. ULONG WDBGAPI ExtGetExpression(PCSTR expr)
  190. {
  191.     return theHost.ExtGetExpression(expr);
  192. }
  193.  
  194.  
  195. void WDBGAPI ExtGetSymbol(PVOID offset, PUCHAR  pchBuffer, PULONG  pDisplacement)
  196. {
  197.     throw "GetSymbol not implemented";
  198. }
  199.  
  200.  
  201. ULONG WDBGAPI ExtDisAsm(PULONG lpOffset, PCSTR  lpBuffer, ULONG  fShowEffectiveAddress)
  202. {
  203.     throw "Disam not implemented";
  204.     return FALSE;
  205. }
  206.  
  207.  
  208. ULONG WDBGAPI ExtCheckControl_C(VOID)
  209. {
  210.     return FALSE;
  211. }
  212.  
  213.  
  214. ULONG WDBGAPI ExtReadProcessMemory(ULONG address, 
  215.     PVOID buffer, ULONG  count, PULONG bytesread)
  216. {
  217.     return theHost.ExtReadProcessMemory(
  218.         (const void *)address, (unsigned *) buffer, count, bytesread);
  219. }
  220.  
  221.  
  222. ULONG WDBGAPI ExtWriteProcessMemory(ULONG offset, 
  223.     LPCVOID lpBuffer, ULONG cb, PULONG  lpcbBytesWritten)
  224. {
  225.     throw "WriteProcessMemory not implemented";
  226.     
  227.     return 0;
  228. }
  229.  
  230.  
  231. ULONG WDBGAPI ExtGetThreadContext(ULONG Processor, PCONTEXT lpContext, ULONG cbSizeOfContext)
  232. {
  233.     throw "GetThreadContext not implemented";
  234.     return 0;
  235. }
  236.  
  237.  
  238. ULONG WDBGAPI ExtSetThreadContext(ULONG Processor,
  239.     PCONTEXT lpContext, ULONG cbSizeOfContext)
  240. {
  241.     throw "SetThreadContext not implemented";
  242.     return 0;
  243. }
  244.  
  245.  
  246. ULONG WDBGAPI ExtIOCTL(USHORT IoctlType, PVOID lpvData, ULONG cbSize)
  247. {
  248.     throw "IOCTL not implemented";
  249.     return 0;
  250. }
  251.  
  252.  
  253. ULONG ExtStackTrace(ULONG FramePointer,
  254.     ULONG StackPointer, ULONG ProgramCounter,
  255.     PEXTSTACKTRACE StackFrames, ULONG Frames)
  256. {
  257.     throw "ExtStackTrace not implemented";
  258.     return 0;
  259. }
  260.  
  261.  
  262. WINDBG_EXTENSION_APIS ExtensionAPI = 
  263. {
  264.     sizeof(WINDBG_EXTENSION_APIS),
  265.  
  266.     ExtOutputRoutine,
  267.     ExtGetExpression,
  268.     ExtGetSymbol,
  269.     ExtDisAsm,
  270.     ExtCheckControl_C,
  271.     ExtReadProcessMemory,
  272.     ExtWriteProcessMemory,
  273.     ExtGetThreadContext,
  274.     ExtSetThreadContext,
  275.     ExtIOCTL,
  276.     ExtStackTrace
  277. };
  278.