DbgSymSrv.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  1. #include "DbgSymSrv.h"
  2. #include "DbgSymSrv.h"
  3. #include "BeefySysLib/MemStream.h"
  4. #include "BeefySysLib/util/BeefPerf.h"
  5. #include "BeefySysLib/util/CabUtil.h"
  6. #include "NetManager.h"
  7. #include "Compiler/BfUtil.h"
  8. #include "DebugManager.h"
  9. #include <shlobj.h>
  10. #include "COFF.h"
  11. #include "BeefySysLib/util/AllocDebug.h"
  12. // #define BF_DBG_64
  13. // #include "DebugCommon.h"
  14. // #include "COFF.h"
  15. // #undef BF_DBG_64
  16. USING_NS_BF_DBG;
  17. DbgSymRequest::DbgSymRequest()
  18. {
  19. mFailed = false;
  20. mSearchingSymSrv = false;
  21. mCancelling = false;
  22. mInProcess = false;
  23. mLastUpdateTick = 0;
  24. mDbgSymSrv = NULL;
  25. mWantAge = 0;
  26. mMayBeOld = false;
  27. mIsPreCache = false;
  28. }
  29. DbgSymRequest::~DbgSymRequest()
  30. {
  31. }
  32. String DbgSymRequest::GetGuidString()
  33. {
  34. String str;
  35. // Seems like weird ordering, but the guid is really supposed to be (uint32, uint16, uint16, uint8[8])
  36. str += StrFormat("%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
  37. mWantGuid[3], mWantGuid[2], mWantGuid[1], mWantGuid[0],
  38. mWantGuid[5], mWantGuid[4],
  39. mWantGuid[7], mWantGuid[6],
  40. mWantGuid[8], mWantGuid[9], mWantGuid[10], mWantGuid[11], mWantGuid[12], mWantGuid[13], mWantGuid[14], mWantGuid[15]);
  41. return str;
  42. }
  43. String DbgSymRequest::GetPDBStoreDir()
  44. {
  45. String checkPath = "/";
  46. checkPath += GetFileName(mPDBRequested);
  47. checkPath += "/";
  48. checkPath += GetGuidString();
  49. checkPath += StrFormat("%x/", mWantAge);
  50. return checkPath;
  51. }
  52. void DbgSymRequest::Fail(const StringImpl& error)
  53. {
  54. mFailed = true;
  55. if (mError.IsEmpty())
  56. mError = error;
  57. }
  58. bool DbgSymRequest::CheckPDBData(const StringImpl& pdbPath, uint8 outGuid[16], int32& outAge)
  59. {
  60. BP_ZONE("DbgSymRequest::CheckPDBData");
  61. if (!FileExists(pdbPath))
  62. return false;
  63. mDbgSymSrv->mDebugger->OutputRawMessage(StrFormat("symsrv Checking '%s'", pdbPath.c_str()));
  64. COFF coff(NULL);
  65. coff.mParseKind = COFF::ParseKind_Header;
  66. bool result = coff.TryLoadPDB(pdbPath, mWantGuid, mWantAge);
  67. memcpy(outGuid, coff.mPDBGuid, 16);
  68. outAge = coff.mDebugAge;
  69. if (!result)
  70. {
  71. if (outAge != -1)
  72. {
  73. mDbgSymSrv->mDebugger->OutputMessage(StrFormat("ERROR: %s cannot be used due to age mismatch\n", pdbPath.c_str()));
  74. }
  75. else
  76. {
  77. bool hasGuid = false;
  78. for (int i = 0; i < 16; i++)
  79. if (outGuid[i] != 0)
  80. hasGuid = true;
  81. if (hasGuid)
  82. {
  83. mDbgSymSrv->mDebugger->OutputMessage(StrFormat("ERROR: %s cannot be used due to GUID mismatch\n", pdbPath.c_str()));
  84. }
  85. else
  86. {
  87. mDbgSymSrv->mDebugger->OutputMessage(StrFormat("ERROR: %s failed to load\n", pdbPath.c_str()));
  88. }
  89. }
  90. }
  91. return result;
  92. }
  93. bool DbgSymRequest::Get(const StringImpl& url, const StringImpl& destPath, NetResult** chainNetResult, bool ignoreSuccess)
  94. {
  95. if (mIsPreCache)
  96. {
  97. auto netResult = mDbgSymSrv->mDebugger->mDebugManager->mNetManager->QueueGet(url, destPath, true);
  98. if (chainNetResult != NULL)
  99. {
  100. if ((*chainNetResult != NULL) && (netResult != NULL))
  101. mDbgSymSrv->mDebugger->mDebugManager->mNetManager->SetCancelOnSuccess(*chainNetResult, netResult);
  102. if (!ignoreSuccess)
  103. *chainNetResult = netResult;
  104. }
  105. return false;
  106. }
  107. return mDbgSymSrv->mDebugger->mDebugManager->mNetManager->Get(url, destPath);
  108. }
  109. void DbgSymRequest::SearchLocal()
  110. {
  111. if ((gDebugManager->mSymSrvOptions.mFlags & BfSymSrvFlag_Disable) != 0)
  112. {
  113. mFailed = true;
  114. return;
  115. }
  116. if (mPDBRequested.IndexOf('\\') != -1) // Do we have an absolute path at all? System dlls won't.
  117. {
  118. // Check actual path
  119. uint8 outGuid[16];
  120. int32 outAge;
  121. if (CheckPDBData(mPDBRequested, outGuid, outAge))
  122. {
  123. mFinalPDBPath = mPDBRequested;
  124. return;
  125. }
  126. // Check in same dir as module
  127. String checkPath = ::GetFileDir(mModulePath);
  128. checkPath += "\\";
  129. checkPath += GetFileName(mPDBRequested);
  130. if (!FileNameEquals(checkPath, mPDBRequested))
  131. {
  132. if (CheckPDBData(checkPath, outGuid, outAge))
  133. {
  134. mFinalPDBPath = checkPath;
  135. return;
  136. }
  137. }
  138. }
  139. mMayBeOld = true;
  140. }
  141. void DbgSymRequest::SearchCache()
  142. {
  143. if ((gDebugManager->mSymSrvOptions.mFlags & BfSymSrvFlag_Disable) != 0)
  144. {
  145. mFailed = true;
  146. return;
  147. }
  148. if (mOptions.mCacheDir.IsEmpty())
  149. {
  150. mFailed = true;
  151. return;
  152. }
  153. uint8 outGuid[16];
  154. int32 outAge;
  155. String cacheDir = mOptions.mCacheDir;
  156. cacheDir += GetPDBStoreDir();
  157. String cacheFilePath = cacheDir + "/" + GetFileName(mPDBRequested);
  158. if (CheckPDBData(cacheFilePath, outGuid, outAge))
  159. {
  160. mFinalPDBPath = cacheFilePath;
  161. return;
  162. }
  163. }
  164. void DbgSymRequest::SearchSymSrv()
  165. {
  166. if ((gDebugManager->mSymSrvOptions.mFlags & BfSymSrvFlag_Disable) != 0)
  167. {
  168. mFailed = true;
  169. return;
  170. }
  171. if (mOptions.mCacheDir.IsEmpty())
  172. {
  173. mFailed = true;
  174. return;
  175. }
  176. /*if (mPDBRequested.Contains("IDEHelper"))
  177. Sleep(3000);*/
  178. SetAndRestoreValue<bool> prevSearchingSymSrv(mSearchingSymSrv, true);
  179. NetResult* chainNetResult = NULL;
  180. uint8 outGuid[16];
  181. int32 outAge;
  182. String cacheDir = mOptions.mCacheDir;
  183. cacheDir += GetPDBStoreDir();
  184. //TODO: Remove!
  185. // for (int i = 0; i < 8; i++)
  186. // {
  187. // mDbgSymSrv->mDebugger->OutputMessage(StrFormat("Sleep %d\n", i));
  188. // if (mCancelling)
  189. // break;
  190. // Sleep(1000);
  191. // }
  192. String cacheFilePath = cacheDir + "/" + GetFileName(mPDBRequested);
  193. for (auto& symServAddr : mOptions.mSymbolServers)
  194. {
  195. if (mCancelling)
  196. break;
  197. int colonPos = symServAddr.IndexOf(':');
  198. if (colonPos > 1)
  199. {
  200. // HTTP
  201. bool done = false;
  202. //TODO: Check for 'index2.txt' for two-tiered structure
  203. String checkPath = symServAddr;
  204. checkPath += GetPDBStoreDir();
  205. checkPath += GetFileName(mPDBRequested);
  206. if (Get(checkPath, cacheFilePath, &chainNetResult))
  207. done = true;
  208. if ((!done) && (!mCancelling))
  209. {
  210. // Compressed version
  211. checkPath[checkPath.length() - 1] = '_';
  212. String compCacheFilePath = cacheFilePath;
  213. compCacheFilePath[compCacheFilePath.length() - 1] = '_';
  214. if (Get(checkPath, compCacheFilePath, &chainNetResult))
  215. {
  216. CabFile cabFile;
  217. cabFile.Load(compCacheFilePath);
  218. mDbgSymSrv->mDebugger->OutputRawMessage(StrFormat("symsrv Decompressing '%s'", compCacheFilePath.c_str()));
  219. if (!cabFile.DecompressAll(cacheDir))
  220. {
  221. Fail(cabFile.mError);
  222. return;
  223. }
  224. done = true;
  225. }
  226. }
  227. if ((!done) && (!mCancelling))
  228. {
  229. String checkPath = symServAddr;
  230. checkPath += GetPDBStoreDir();
  231. checkPath += "/file.dir";
  232. String fileDirFilePath = symServAddr;
  233. fileDirFilePath += GetPDBStoreDir();
  234. fileDirFilePath += "/file.dir";
  235. if (Get(checkPath, fileDirFilePath, &chainNetResult, true))
  236. {
  237. // What do I do with this?
  238. }
  239. }
  240. }
  241. else if (!mIsPreCache)
  242. {
  243. // Uncompressed version
  244. String checkPath = symServAddr;
  245. checkPath += GetPDBStoreDir();
  246. checkPath += GetFileName(mPDBRequested);
  247. if (CheckPDBData(checkPath, outGuid, outAge))
  248. {
  249. mFinalPDBPath = checkPath;
  250. return;
  251. }
  252. // Compressed version
  253. checkPath[checkPath.length() - 1] = '_';
  254. if (FileExists(checkPath))
  255. {
  256. CabFile cabFile;
  257. cabFile.Load(checkPath);
  258. mDbgSymSrv->mDebugger->OutputRawMessage(StrFormat("symsrv Decompressing '%s'", checkPath.c_str()));
  259. if (!cabFile.DecompressAll(cacheDir))
  260. {
  261. Fail(cabFile.mError);
  262. return;
  263. }
  264. }
  265. }
  266. if (mIsPreCache)
  267. continue;
  268. // Re-check cache
  269. if (CheckPDBData(cacheFilePath, outGuid, outAge))
  270. {
  271. mFinalPDBPath = cacheFilePath;
  272. return;
  273. }
  274. }
  275. mFailed = true;
  276. }
  277. bool DbgSymRequest::CheckPEFile(const StringImpl& filePath, uint32 fileTime, int size)
  278. {
  279. FileStream fs;
  280. if (!fs.Open(filePath, "rb"))
  281. return false;
  282. PEHeader hdr;
  283. fs.ReadT(hdr);
  284. PE_NTHeaders64 ntHdr64;
  285. fs.SetPos(hdr.e_lfanew);
  286. fs.Read(&ntHdr64, sizeof(PE_NTHeaders64));
  287. if (ntHdr64.mFileHeader.mMachine == PE_MACHINE_X64)
  288. {
  289. if ((ntHdr64.mFileHeader.mTimeDateStamp != fileTime) || (ntHdr64.mOptionalHeader.mSizeOfImage != size))
  290. return false;
  291. }
  292. else
  293. {
  294. PE_NTHeaders32 ntHdr32;
  295. fs.SetPos(hdr.e_lfanew);
  296. fs.Read(&ntHdr32, sizeof(PE_NTHeaders32));
  297. if ((ntHdr32.mFileHeader.mTimeDateStamp != fileTime) || (ntHdr32.mOptionalHeader.mSizeOfImage != size))
  298. return false;
  299. }
  300. return true;
  301. }
  302. String DbgSymRequest::SearchForImage(const String& filePath, uint32 fileTime, int size)
  303. {
  304. if ((gDebugManager->mSymSrvOptions.mFlags & BfSymSrvFlag_Disable) != 0)
  305. return "";
  306. if (FileExists(filePath))
  307. {
  308. if (CheckPEFile(filePath, fileTime, size))
  309. return filePath;
  310. }
  311. /*if (filePath.Contains("IDEHelper"))
  312. Sleep(3000);*/
  313. if (mOptions.mCacheDir.IsEmpty())
  314. {
  315. mFailed = true;
  316. return "";
  317. }
  318. NetResult* chainNetResult = NULL;
  319. String fileName = GetFileName(filePath);
  320. String imageStoreDir = "/";
  321. imageStoreDir += fileName;
  322. imageStoreDir += "/";
  323. imageStoreDir += StrFormat("%08X%x/", fileTime, size);
  324. SetAndRestoreValue<bool> prevSearchingSymSrv(mSearchingSymSrv, true);
  325. String cacheDir = mOptions.mCacheDir;
  326. cacheDir += imageStoreDir;
  327. /*for (int i = 0; i < 8; i++)
  328. {
  329. mDbgSymSrv->mDebugger->OutputMessage(StrFormat("Image Sleep %d\n", i));
  330. if (mCancelling)
  331. break;
  332. Sleep(1000);
  333. }*/
  334. String cacheFilePath = cacheDir + "/" + GetFileName(filePath);
  335. // Check cache
  336. if (CheckPEFile(cacheFilePath, fileTime, size))
  337. {
  338. return cacheFilePath;
  339. }
  340. for (auto& symServAddr : mOptions.mSymbolServers)
  341. {
  342. if (mCancelling)
  343. break;
  344. int colonPos = symServAddr.IndexOf(':');
  345. if (colonPos > 1)
  346. {
  347. // HTTP
  348. bool done = false;
  349. //TODO: Check for 'index2.txt' for two-tiered structure
  350. String checkPath = symServAddr;
  351. checkPath += imageStoreDir;
  352. checkPath += fileName;
  353. if (Get(checkPath, cacheFilePath, &chainNetResult))
  354. done = true;
  355. if ((!done) && (!mCancelling))
  356. {
  357. // Compressed version
  358. checkPath[checkPath.length() - 1] = '_';
  359. String compCacheFilePath = cacheFilePath;
  360. compCacheFilePath[compCacheFilePath.length() - 1] = '_';
  361. if (Get(checkPath, compCacheFilePath, &chainNetResult))
  362. {
  363. mDbgSymSrv->mDebugger->mDebugManager->mOutMessages.push_back(StrFormat("symsrv Decompressing '%s'", checkPath.c_str()));
  364. CabFile cabFile;
  365. cabFile.Load(compCacheFilePath);
  366. if (!cabFile.DecompressAll(cacheDir))
  367. {
  368. Fail(cabFile.mError);
  369. return "";
  370. }
  371. done = true;
  372. }
  373. }
  374. if ((!done) && (!mCancelling))
  375. {
  376. String checkPath = symServAddr;
  377. checkPath += imageStoreDir;
  378. checkPath += "/file.dir";
  379. String fileDirFilePath = symServAddr;
  380. fileDirFilePath += imageStoreDir;
  381. fileDirFilePath += "/file.dir";
  382. if (Get(checkPath, fileDirFilePath, &chainNetResult, true))
  383. {
  384. // What do I do with this?
  385. }
  386. }
  387. }
  388. else if (!mIsPreCache)
  389. {
  390. // Uncompressed version
  391. String checkPath = symServAddr;
  392. checkPath += imageStoreDir;
  393. checkPath += fileName;
  394. if (CheckPEFile(checkPath, fileTime, size))
  395. {
  396. return checkPath;
  397. }
  398. // Compressed version
  399. checkPath[checkPath.length() - 1] = '_';
  400. if (FileExists(checkPath))
  401. {
  402. CabFile cabFile;
  403. cabFile.Load(checkPath);
  404. if (!cabFile.DecompressAll(cacheDir))
  405. {
  406. Fail(cabFile.mError);
  407. return "";
  408. }
  409. }
  410. }
  411. if (mIsPreCache)
  412. continue;
  413. // Re-check cache
  414. if (CheckPEFile(cacheFilePath, fileTime, size))
  415. {
  416. return cacheFilePath;
  417. }
  418. }
  419. if (!mIsPreCache)
  420. {
  421. mDbgSymSrv->mDebugger->mHadImageFindError = true;
  422. mDbgSymSrv->mDebugger->OutputMessage(StrFormat("ERROR: Unable to locate image '%s' (%08X%x). If this file is located on a symbol server, configure the symbol server location in File\\Preferences\\Settings under Debugger\\Symbol File Locations.\n",
  423. GetFileName(filePath).c_str(), fileTime, size));
  424. }
  425. mFailed = true;
  426. return "";
  427. }
  428. void DbgSymRequest::Cancel()
  429. {
  430. mCancelling = true;
  431. mDbgSymSrv->mDebugger->mDebugManager->mNetManager->CancelCurrent();
  432. }
  433. bool DbgSymRequest::IsDone()
  434. {
  435. AutoCrit autoCrit(mCritSect);
  436. if (!mFinalPDBPath.IsEmpty())
  437. return true;
  438. if (mFailed)
  439. return true;
  440. return false;
  441. }
  442. DbgSymSrv::DbgSymSrv(Debugger* debugger)
  443. {
  444. mDebugger = debugger;
  445. }
  446. void DbgSymSrv::PreCacheImage(const String& filePath, uint32 fileTime, int size)
  447. {
  448. auto request = CreateRequest();
  449. request->mIsPreCache = true;
  450. request->SearchForImage(filePath, fileTime, size);
  451. delete request;
  452. }
  453. DbgSymRequest* DbgSymSrv::CreateRequest(const StringImpl& modulePath, const StringImpl& pdbRequested, uint8 wantGuid[16], int32 wantAge)
  454. {
  455. AutoCrit autoCrit(mDebugger->mDebugManager->mCritSect);
  456. DbgSymRequest* symRequest = new DbgSymRequest();
  457. symRequest->mOptions = mDebugger->mDebugManager->mSymSrvOptions;
  458. symRequest->mDbgSymSrv = this;
  459. symRequest->mModulePath = modulePath;
  460. symRequest->mPDBRequested = pdbRequested;
  461. memcpy(symRequest->mWantGuid, wantGuid, 16);
  462. symRequest->mWantAge = wantAge;
  463. return symRequest;
  464. }
  465. DbgSymRequest* DbgSymSrv::CreateRequest()
  466. {
  467. AutoCrit autoCrit(mDebugger->mDebugManager->mCritSect);
  468. DbgSymRequest* symRequest = new DbgSymRequest();
  469. symRequest->mOptions = mDebugger->mDebugManager->mSymSrvOptions;
  470. symRequest->mDbgSymSrv = this;
  471. return symRequest;
  472. }
  473. void DbgSymSrv::ReleaseRequest(DbgSymRequest* dbgSymRequest)
  474. {
  475. delete dbgSymRequest;
  476. }
  477. void DbgSymSrv::Update()
  478. {
  479. }