void LOCATOR::SYMSRV::Init() { AutoLock lock(&m_cs); if (m_fInit) { return; } m_fInit = true; PREFAST_SUPPRESS(6321, "It's ok"); HMODULE hmod = LoadLibraryExW(L"SYMSRV.DLL", NULL, 0); if ((UINT_PTR) hmod < 32) { return; } m_hmod = hmod; m_pfnsymbolserverw = PFNSYMBOLSERVERW(GetProcAddress(m_hmod, "SymbolServerW")); m_pfnsymbolserversetoptions = PFNSYMBOLSERVERSETOPTIONS(GetProcAddress(m_hmod, "SymbolServerSetOptions")); m_pfnsymbolserverstorefilew = PFNSYMBOLSERVERSTOREFILEW(GetProcAddress(m_hmod, "SymbolServerStoreFileW")); }
bool LOCATOR::FLocateDbgValidate(const wchar_t *wszDbgPath) { FILE *fd = PDB_wfsopen(wszDbgPath, L"rb", SH_DENYWR); if (fd == NULL) { NotifyOpenDBG(wszDbgPath, EC_DBG_NOT_FOUND, NULL); return false; } IMAGE_SEPARATE_DEBUG_HEADER DbgHeader; if (fread(&DbgHeader, sizeof(DbgHeader), 1, fd) != 1) { // The file isn't large enough to contain a DBG header NotifyOpenDBG(wszDbgPath, EC_FORMAT, NULL); fclose(fd); return false; } if (DbgHeader.Signature != IMAGE_SEPARATE_DEBUG_SIGNATURE) { // The file isn't a DBG file NotifyOpenDBG(wszDbgPath, EC_FORMAT, NULL); fclose(fd); return false; } // Skip over the section headers and exported names DWORD cb = DbgHeader.NumberOfSections * sizeof(IMAGE_SECTION_HEADER) + DbgHeader.ExportedNamesSize; // Try to find the CodeView directory. // It's OK if we don't find it. if (fseek(fd, (long) cb, SEEK_CUR) != 0) { NotifyOpenDBG(wszDbgPath, EC_FORMAT, NULL); fclose(fd); return false; } long fo = ftell(fd); size_t cdbgdir = DbgHeader.DebugDirectorySize / sizeof(IMAGE_DEBUG_DIRECTORY); while (cdbgdir-- != 0) { IMAGE_DEBUG_DIRECTORY dbgdir; if (fread(&dbgdir, sizeof(dbgdir), 1, fd) != 1) { NotifyOpenDBG(wszDbgPath, EC_FORMAT, NULL); fclose(fd); return false; } if (!m_fCvInExe && (dbgdir.Type == IMAGE_DEBUG_TYPE_CODEVIEW)) { m_cbCv = dbgdir.SizeOfData; m_rvaCv = 0; m_foCv = dbgdir.PointerToRawData; } } // We check the timestamp after the debug directories so that we know that the // file is structurally valid before report a timestamp mismatch. This is so that // we don't remember the timestamp mismatch for something not a valid DBG file. if (DbgHeader.TimeDateStamp != m_dwTimeStampExe) { if ((m_dwTimeStampDbg == 0) || (DbgHeader.TimeDateStamp != m_dwTimeStampDbg)) { // The timestamp doesn't match the executable NotifyOpenDBG(wszDbgPath, EC_INVALID_EXE_TIMESTAMP, NULL); fclose(fd); return false; } } m_wszDbgPath = _wcsdup(wszDbgPath); m_fdDbg = fd; NotifyOpenDBG(wszDbgPath, EC_OK, NULL); // We notify caller of debug directories after notifying // caller of DBG file otherwise the caller doesn't know // where to look during the NotifyDebugDir callback. if (fseek(fd, fo, SEEK_SET) == 0) { cdbgdir = DbgHeader.DebugDirectorySize / sizeof(IMAGE_DEBUG_DIRECTORY); while (cdbgdir-- != 0) { IMAGE_DEBUG_DIRECTORY dbgdir; if (fread(&dbgdir, sizeof(dbgdir), 1, fd) != 1) { break;; } PREFAST_SUPPRESS(6029, "NotifyDebugDir() won't write into dbgdir"); NotifyDebugDir(false, &dbgdir); } } return true; }
DWORD FindNextDevice ( DWORD i ) { DWORD dwRet; HANDLE hDsk; TCHAR szTemp[16]; DISK_INFO diskInfo; DWORD cb; RETAILMSG(1, (_T("Poll the whole root dir to find all disks, i = 0x%x.\r\n"),i)); // Validate input parameters if (i > 9) { // Input parameter is out of range return NULL; } RETAILMSG(1, (_T("Poll the whole root dir to find all disks, i = 0x%x.\r\n"),i)); // Find the first available DSKx: device do { // Create the device name to try to open _stprintf( szTemp, TEXT("DSK%u:"), i ); // 'i' is the number of the last found device. Start the search at 'i+1' i++; // Attempt to open the device hDsk = CreateFile( szTemp, GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, 0 ); if(INVALID_HANDLE_VALUE == hDsk) { continue; } // this control code will tell us if the DSK is R/W media or a CD if(!DeviceIoControl(hDsk, DISK_IOCTL_GETINFO, &diskInfo, sizeof(diskInfo), NULL, 0, &cb, NULL) PREFAST_SUPPRESS(6001,"This Warning can be skipped!") || 0 == diskInfo.di_total_sectors ) { CloseHandle(hDsk); hDsk = INVALID_HANDLE_VALUE; continue; } RETAILMSG(TRUE, (TEXT("Find valid disk handle: %s:\r\n"), szTemp)); } while (i <= 9 ); // Was the search successful? if ( hDsk == INVALID_HANDLE_VALUE ) { // Did not find a device dwRet = NULL; } else { // Found a device. Close this instance, return the number of the deivce CloseHandle( hDsk ); dwRet = i; } return dwRet; }