Changeset 366

Show
Ignore:
Timestamp:
08/12/08 09:30:48 (9 years ago)
Author:
rsteenson
Message:

Fix process manager not returning the correct process name when events occur quickly and the process map has not been updated yet

Location:
capture-hpc/trunk/capture-client
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • capture-hpc/trunk/capture-client/ProcessManager.cpp

    r365 r366  
    44ProcessManager::ProcessManager(void) 
    55{ 
    6         syncEvent = CreateEvent(NULL,FALSE,TRUE,NULL); 
     6        hProcessCreated = CreateEvent(NULL,FALSE,TRUE,NULL); 
    77        cacheHits = 0; 
    88        cacheMisses = 0; 
     
    1818        DebugPrint(L"\tCache Failures: %i\n", cacheFailures); 
    1919        instanceCreated = false; 
    20  
    21         if(WaitForSingleObject(syncEvent,INFINITE)==0) { 
    22                 processMap.clear(); 
    23                 SetEvent(syncEvent); 
    24         } else { 
    25                 DebugPrint(L"Capture-ProcessManager: ~ProcessManager signaled incorrectly"); 
    26         } 
    27          
    28         CloseHandle(syncEvent); 
    29  
     20        processMap.clear(); 
     21        CloseHandle(hProcessCreated); 
    3022} 
    3123 
     
    5951ProcessManager::insertProcess(DWORD processId, const std::wstring& processPath) 
    6052{ 
    61         DebugPrint(L"ProcessManager::insertProcess start\n"); 
    62         if(WaitForSingleObject(syncEvent,INFINITE)==0) { 
    63                 DebugPrint(L"ProcessManager::insertProcess signaled\n"); 
    64                 stdext::hash_map<DWORD, std::wstring>::iterator it; 
    65                 it = processMap.find(processId); 
    66                 if(it !=  processMap.end()) { 
    67                         processMap.erase(it); 
    68                 } 
    69                 std::wstring processCompletePath = convertFileObjectPathToDOSPath(processPath); 
    70                 DebugPrint(L"Capture-ProcessManager: Insert process %i -> %ls\n", processId, processCompletePath.c_str());  
    71                 processMap.insert(std::pair<DWORD, std::wstring>(processId, processCompletePath)); 
    72                 SetEvent(syncEvent); 
    73         } else { 
    74                 DebugPrint(L"Capture-ProcessManager: insertProcess signaled incorrectly"); 
    75         } 
    76         DebugPrint(L"ProcessManager::insertProcess end\n"); 
     53        stdext::hash_map<DWORD, std::wstring>::iterator it; 
     54        it = processMap.find(processId); 
     55        if(it !=  processMap.end()) { 
     56                processMap.erase(it); 
     57        } 
     58        std::wstring processCompletePath = convertFileObjectPathToDOSPath(processPath); 
     59        DebugPrint(L"Capture-ProcessManager: Insert process %i -> %ls\n", processId, processCompletePath.c_str());  
     60        processMap.insert(std::pair<DWORD, std::wstring>(processId, processCompletePath)); 
     61        SetEvent(hProcessCreated); 
    7762} 
    7863 
     
    117102{ 
    118103        std::wstring processPath = L"UNKNOWN"; 
    119  
    120          
    121104        if(processId == 4) 
    122105        { 
     
    125108        } 
    126109 
    127          
    128         if(WaitForSingleObject(syncEvent,INFINITE)==0) { 
    129                 stdext::hash_map<DWORD, std::wstring>::iterator it; 
     110        int retryCount = 10; 
     111        stdext::hash_map<DWORD, std::wstring>::iterator it; 
     112 
     113        // Get the process name from the map 
     114        // If its not there wait for the process created event to 
     115        // be signalled and try again up to a certain amount of time 
     116        // If this fails the old way of using windows API to determine 
     117        // the name of the process is used. 
     118        do 
     119        { 
    130120                it = processMap.find(processId); 
    131                 if((it !=  processMap.end()) && (it->second != L"<unknown>")) 
     121                if(it ==  processMap.end()) 
     122                { 
     123                        // Wait till a process has been entered 
     124                        WaitForSingleObject(hProcessCreated, 100); 
     125                } 
     126                else 
    132127                { 
    133128                        cacheHits++; 
    134                         processPath = it->second; 
    135                         DebugPrint(L"Capture-ProcessManager: Cache hit %i -> %ls\n", processId, processPath.c_str());  
    136                         SetEvent(syncEvent); 
    137                         return processPath; 
    138                 } 
    139                 SetEvent(syncEvent); 
    140         } else { 
    141                 DebugPrint(L"Capture-ProcessManager: getProcessPath signaled incorrectly"); 
    142         } 
     129                        return it->second; 
     130                } 
     131 
     132        } while(--retryCount != 0); 
    143133 
    144134        // NOTE testing showed that this case should never or hardly ever 
     
    151141 
    152142        HANDLE hProc = OpenProcess(PROCESS_QUERY_INFORMATION | 
    153                                                 PROCESS_VM_READ, FALSE, processId); 
    154         int time = 0; 
    155         while(!hProc && time<500) { //half a sec 
    156                 //failure to get path - maybe because process is not fully created yet 
    157                 //inserting delay and try again 
    158                 Sleep(10); 
    159                 time = time + 10; 
    160                 DebugPrint(L"Capture-ProcessManager: Cache failure1 delay %i\n",time); 
    161                 hProc = OpenProcess(PROCESS_QUERY_INFORMATION | 
    162                                                 PROCESS_VM_READ, FALSE, processId); 
    163         } 
     143                        PROCESS_VM_READ, FALSE, processId); 
    164144        if(!hProc) 
    165145        { 
    166146                cacheFailures++; 
    167                 processPath = L"UNKNOWN"; 
    168                 DebugPrint(L"Capture-ProcessManager: Cache failure1 %i -> %ls\n", processId, processPath.c_str());  
     147                DebugPrint(L"Capture-ProcessManager: Cache failure - process id = %i\n", processId);  
     148                _ltow_s(processId, szTemp, 256, 10); 
     149                processPath = szTemp; 
    169150                return processPath; 
    170151        } 
    171  
    172         time = 0; 
     152        // Cannot use GetModuleFileName because the module list won't be 
     153        // updated in time ... only happens on rare occasions. 
    173154        DWORD ret = GetProcessImageFileName(hProc, szTemp,1024); 
    174         while(ret<=0 && time<500) { 
    175                 Sleep(10); 
    176                 time = time + 10; 
    177                 DebugPrint(L"Capture-ProcessManager: Cache failure2 delay %i\n",time); 
    178                 ret = GetProcessImageFileName(hProc, szTemp,1024); 
    179         } 
    180  
    181155        CloseHandle(hProc); 
    182156        if(ret > 0) 
    183157        { 
    184158                processPath = szTemp; 
    185                 DebugPrint(L"Capture-ProcessManager: Cache miss %i -> %ls\n", processId, processPath.c_str());  
     159                DebugPrint(L"Capture-ProcessManager: Cache miss %i -> %ls\n", processId, szTemp);  
    186160                insertProcess(processId, processPath); 
    187161        } else { 
    188                 //failure to get path 
    189162                cacheFailures++; 
    190                 processPath = L"UNKNOWN"; 
    191                 DebugPrint(L"Capture-ProcessManager: Cache failure2 %i -> %ls\n", processId, processPath.c_str());  
    192         } 
    193         DebugPrint(L"ProcessManager::getProcessPath end4\n"); 
    194         return processPath;              
     163                DebugPrint(L"Capture-ProcessManager: Cache failure - process id = %i\n", processId);  
     164                _ltow_s(processId, szTemp, 256, 10); 
     165                processPath = szTemp; 
     166        } 
     167         
     168        return processPath; 
    195169} 
    196170 
  • capture-hpc/trunk/capture-client/ProcessManager.h

    r365 r366  
    6565        static bool instanceCreated; 
    6666    static ProcessManager *processManager; 
    67          
     67        HANDLE hProcessCreated; 
    6868        ProcessManager(void); 
    6969        int cacheHits;