home *** CD-ROM | disk | FTP | other *** search
/ Windows Graphics Programming / Feng_Yuan_Win32_GDI_DirectX.iso / Samples / Chapt_04 / Diver / functable.cpp < prev    next >
C/C++ Source or Header  |  2000-05-16  |  5KB  |  176 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   : functable.h                                                          //
  10. //  Description: KMyFuncTable class                                                  //
  11. //  Version    : 1.00.000, May 31, 2000                                              //
  12. //-----------------------------------------------------------------------------------//
  13.  
  14. #define NOCRYPT
  15.  
  16. #include <windows.h>
  17. #include <assert.h>
  18. #include <tchar.h>
  19.  
  20. #include "..\..\include\Decoder.h"
  21. #include "..\..\include\atom.h"
  22.  
  23. #include "Function.h"
  24. #include "Timer.h"
  25. #include "Dump.h"
  26. #include "FuncTable.h"
  27. #include "Diver.h"
  28. #include "Report.h"
  29. #include "Format.h"
  30.  
  31. //  16,        module
  32. //  32,        function
  33. //  10,        enter
  34. //  10,        leave
  35. //  10,        result
  36.  
  37. //  10,        this
  38. //  100        parameters
  39.  
  40. KMyFuncTable FuncTable;
  41.  
  42. KFuncTable *pFuncTable = & FuncTable;
  43.  
  44. extern KDump ApiLog;
  45.  
  46.  
  47. int RealTime(int clock)
  48. {
  49.     return MulDiv(clock, 2560, nTimer.GetCPUSpeed());
  50. }
  51.  
  52.  
  53. void KMyFuncTable::FuncEntryCallBack(KRoutineInfo *routine, EntryInfo *info)
  54. {
  55.     int        funcid = routine->funcid;
  56.     const char  *name = GetFuncName(funcid);
  57.     int i;
  58.  
  59.     if (m_logcall)
  60.     {
  61.         KDumpData & dd = routine->dumpdata;
  62.         
  63.     //    dd.cppthis[0] = 0;
  64.         dd.param[0]   = 0;
  65.     
  66.     //  if ( strchr(name, '.') ) // com method: class.interface.function
  67.     //  {
  68.     //        // for COM method, this pointer is pushed as the first parameter
  69.     //        MainDecoder(dd.cppthis, sizeof(dd.cppthis)/sizeof(TCHAR), atom_this, info->m_para[0]);
  70.     //
  71.     //      for (i = 1; i<GetParaNo(funcid); i++)
  72.     //          MainDecoder(dd.param, sizeof(dd.param)/sizeof(TCHAR), GetParaTypeAtom(funcid, i), info->m_para[i]);
  73.     //  }
  74.     //  else
  75.         {
  76.             for (i = 1; i<GetParaNo(funcid); i++)
  77.                 MainDecoder(dd.param, sizeof(dd.param)/sizeof(TCHAR), GetParaTypeAtom(funcid, i), info->m_para[i-1]);
  78.         }
  79.     }
  80.  
  81.     m_func[funcid].f_callno ++;
  82.  
  83.     if (m_dispcall)
  84.     {
  85.         if (m_func[funcid].f_class & spy_summary)
  86.         {
  87.             if (((m_func[funcid].f_callno) % 1024)==1)
  88.                 Send(M_ENTERSUMMARY, m_func[funcid].f_callno, 0, name);
  89.         }
  90.         else    
  91.             Send(M_ENTER, 0, 0, name);
  92.     }
  93. }
  94.  
  95.  
  96. void KMyFuncTable::FuncExitCallBack(KRoutineInfo *routine, ExitInfo *info, unsigned leavetime, int depth)
  97. {
  98.     int funcid = routine->funcid;
  99.  
  100.     if (m_func[funcid].f_class & spy_summary)
  101.     {
  102.         m_func[funcid].f_totaltime += leavetime - routine->entertime;
  103.  
  104.         if (m_dispcall)
  105.             if (((m_func[funcid].f_callno) % 1024)==1)
  106.             {
  107.                 Send(M_LEAVESUMMARY, m_func[funcid].f_callno, RealTime(m_func[funcid].f_totaltime), GetFuncName(funcid));
  108.                 m_func[funcid].f_totaltime = 0;
  109.             }
  110.     }
  111.     else
  112.     {
  113.         if (m_dispcall)
  114.             Send(M_LEAVE, 0, RealTime(leavetime-routine->entertime), GetFuncName(funcid));
  115.     }
  116.  
  117.     if (m_logcall)
  118.     {
  119.         KDumpData & dd = routine->dumpdata;
  120.  
  121.         // 1: depth
  122.         ApiLog.WriteField(depth, atom_int);
  123.  
  124.         // 2: entering time
  125.         ApiLog.WriteField(RealTime(routine->entertime), atom_int);
  126.  
  127.         // 3: leaving time
  128.         ApiLog.WriteField(RealTime(leavetime),          atom_int);
  129.  
  130.         // 4: this pointer
  131.     //    ApiLog.WriteField(dd.cppthis);
  132.  
  133.         // 5: result
  134.         {
  135.             ATOM typ = atom_void;
  136.  
  137.             if (GetParaNo(funcid)>=1) 
  138.                 typ = GetParaTypeAtom(funcid, 0);
  139.         
  140.             if ( typ != atom_void )
  141.                 ApiLog.WriteField(info->m_rslt, typ);
  142.             else
  143.                 ApiLog.WriteField(_T(""));
  144.         }
  145.  
  146.         // 6: caller address
  147.         ApiLog.WriteField(routine->rtnaddr, atom_returnaddr);
  148.  
  149.         const TCHAR * name = GetFuncName(funcid);
  150.     //    const TCHAR * pCOM = _tcschr(name, '.');
  151.  
  152.     //    // 7: function name
  153.     //  if ( pCOM ) // com method: class.interface.function
  154.     //        ApiLog.WriteField(name);                // class.interface.function
  155.     //  else
  156.         {
  157.             TCHAR fullname[64];
  158.  
  159.             _tcscpy(fullname, CallerName(m_func[funcid].f_module));        // caller module
  160.             _tcscat(fullname, "!");
  161.             _tcscat(fullname, name);
  162.  
  163.             ApiLog.WriteField(fullname);                                // function
  164.         }
  165.  
  166.         // 8: parameters
  167.         ApiLog.WriteField(dd.param);
  168.  
  169.         ApiLog.Newline();
  170.     }
  171.  
  172.     // new dll may be loaded, do win32 api intercept if needed
  173.     if (m_func[funcid].f_class & spy_loadlibrary)
  174.         InterceptWin32();
  175. }        
  176.