home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / sdktools / image / winnt / pfmon / module.c < prev    next >
C/C++ Source or Header  |  1997-10-05  |  4KB  |  178 lines

  1. /*++
  2.  
  3. Copyright (c) 1995-1997  Microsoft Corporation
  4.  
  5. Module Name:
  6.  
  7.     module.c
  8.  
  9. Abstract:
  10.  
  11.     This module maintains the module (symbol) information for the pfmon application
  12.  
  13. Author:
  14.  
  15.     Mark Lucovsky (markl) 27-Jan-1995
  16.  
  17. Revision History:
  18.  
  19. --*/
  20.  
  21. #include "pfmonp.h"
  22.  
  23. BOOL
  24. AddModule(
  25.     LPDEBUG_EVENT DebugEvent
  26.     )
  27. {
  28.     PMODULE_INFO Module;
  29.     LPVOID BaseAddress;
  30.     HANDLE Handle;
  31.  
  32.     if ( DebugEvent->dwDebugEventCode == CREATE_PROCESS_DEBUG_EVENT) {
  33.         Handle = DebugEvent->u.CreateProcessInfo.hFile;
  34.         BaseAddress = DebugEvent->u.CreateProcessInfo.lpBaseOfImage;
  35.         }
  36.     else {
  37.         Handle = DebugEvent->u.LoadDll.hFile;
  38.         BaseAddress = DebugEvent->u.LoadDll.lpBaseOfDll;
  39.         }
  40.  
  41.     Module = FindModuleContainingAddress(BaseAddress);
  42.     if ( Module ) {
  43.         DeleteModule(Module);
  44.         }
  45.  
  46.     Module = LocalAlloc(LMEM_ZEROINIT, sizeof( *Module ) );
  47.  
  48.     if (Module == NULL) {
  49.         return FALSE;
  50.         }
  51.  
  52.     Module->Handle = Handle;
  53.     Module->BaseAddress = BaseAddress;
  54.  
  55.     if ( !Module->Handle ) {
  56.         LocalFree(Module);
  57.         return FALSE;
  58.         }
  59.  
  60.     Module->DebugInfo = MapDebugInformation(
  61.                             Module->Handle,
  62.                             NULL,
  63.                             SymbolSearchPath,
  64.                             (DWORD)Module->BaseAddress
  65.                             );
  66.  
  67.     if ( !Module->DebugInfo ) {
  68.         LocalFree(Module);
  69.         return FALSE;
  70.         }
  71.     Module->VirtualSize = Module->DebugInfo->SizeOfImage;
  72.  
  73.     SymLoadModule(hProcess,Handle,NULL,NULL,(DWORD)BaseAddress,0);
  74.  
  75.     InsertTailList( &ModuleListHead, &Module->Entry );
  76.  
  77.     return TRUE;
  78. }
  79.  
  80. BOOL
  81. DeleteModule(
  82.     PMODULE_INFO Module
  83.     )
  84. {
  85.     CHAR Line[256];
  86.  
  87.     if ( Module ) {
  88.         RemoveEntryList(&Module->Entry);
  89.  
  90.         sprintf(Line,"%16s Caused %6d faults had %6d Soft %6d Hard faulted VA's\n",
  91.             Module->DebugInfo->ImageFileName,
  92.             Module->NumberCausedFaults,
  93.             Module->NumberFaultedSoftVas,
  94.             Module->NumberFaultedHardVas
  95.             );
  96.         if ( !fLogOnly ) {
  97.             fprintf(stdout,"%s",Line);
  98.             }
  99.         if ( LogFile ) {
  100.             fprintf(LogFile,"%s",Line);
  101.             }
  102.  
  103.         UnmapDebugInformation(Module->DebugInfo);
  104.         LocalFree(Module);
  105.         }
  106.  
  107.     return TRUE;
  108. }
  109.  
  110.  
  111. PMODULE_INFO
  112. FindModuleContainingAddress(
  113.     LPVOID Address
  114.     )
  115. {
  116.     PLIST_ENTRY Next;
  117.     PMODULE_INFO Module;
  118.  
  119.     Next = ModuleListHead.Flink;
  120.     while ( Next != &ModuleListHead ) {
  121.         Module = CONTAINING_RECORD(Next,MODULE_INFO,Entry);
  122.         if ( Address >= Module->BaseAddress &&
  123.              Address < (LPVOID)((PUCHAR)(Module->BaseAddress)+Module->VirtualSize) ) {
  124.             return Module;
  125.             }
  126.         Next = Next->Flink;
  127.         }
  128.     return NULL;
  129. }
  130.  
  131. VOID
  132. SetSymbolSearchPath( )
  133. {
  134.     LPSTR lpSymPathEnv, lpAltSymPathEnv, lpSystemRootEnv;
  135.     ULONG cbSymPath;
  136.     DWORD dw;
  137.  
  138.     cbSymPath = 18;
  139.     if (lpSymPathEnv = getenv("_NT_SYMBOL_PATH")) {
  140.         cbSymPath += strlen(lpSymPathEnv) + 1;
  141.     }
  142.     if (lpAltSymPathEnv = getenv("_NT_ALT_SYMBOL_PATH")) {
  143.         cbSymPath += strlen(lpAltSymPathEnv) + 1;
  144.     }
  145.  
  146.     if (lpSystemRootEnv = getenv("SystemRoot")) {
  147.         cbSymPath += strlen(lpSystemRootEnv) + 1;
  148.     }
  149.  
  150.     SymbolSearchPath = LocalAlloc(LMEM_ZEROINIT,cbSymPath);
  151.  
  152.     if (lpAltSymPathEnv) {
  153.         dw = GetFileAttributes(lpAltSymPathEnv);
  154.         if ( dw != 0xffffffff && dw & FILE_ATTRIBUTE_DIRECTORY ) {
  155.             strcat(SymbolSearchPath,lpAltSymPathEnv);
  156.             strcat(SymbolSearchPath,";");
  157.             }
  158.     }
  159.     if (lpSymPathEnv) {
  160.         dw = GetFileAttributes(lpSymPathEnv);
  161.         if ( dw != 0xffffffff && dw & FILE_ATTRIBUTE_DIRECTORY ) {
  162.             strcat(SymbolSearchPath,lpSymPathEnv);
  163.             strcat(SymbolSearchPath,";");
  164.             }
  165.     }
  166.  
  167.     if (lpSystemRootEnv) {
  168.         dw = GetFileAttributes(lpSystemRootEnv);
  169.         if ( dw != 0xffffffff && dw & FILE_ATTRIBUTE_DIRECTORY ) {
  170.             strcat(SymbolSearchPath,lpSystemRootEnv);
  171.             strcat(SymbolSearchPath,";");
  172.             }
  173.     }
  174.  
  175.     strcat(SymbolSearchPath,".;");
  176.  
  177. }
  178.