DbgSymSrv.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  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. uint8 outGuid[16];
  117. int32 outAge;
  118. if (mPDBRequested.IndexOf('\\') != -1) // Do we have an absolute path at all? System dlls won't.
  119. {
  120. // Check actual path
  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. else
  140. {
  141. String checkPath = ::GetFileDir(mModulePath);
  142. checkPath += "\\";
  143. checkPath += mPDBRequested;
  144. if (CheckPDBData(checkPath, outGuid, outAge))
  145. {
  146. mFinalPDBPath = checkPath;
  147. return;
  148. }
  149. }
  150. mMayBeOld = true;
  151. }
  152. void DbgSymRequest::SearchCache()
  153. {
  154. if ((gDebugManager->mSymSrvOptions.mFlags & BfSymSrvFlag_Disable) != 0)
  155. {
  156. mFailed = true;
  157. return;
  158. }
  159. if (mOptions.mCacheDir.IsEmpty())
  160. {
  161. mFailed = true;
  162. return;
  163. }
  164. uint8 outGuid[16];
  165. int32 outAge;
  166. String cacheDir = mOptions.mCacheDir;
  167. cacheDir += GetPDBStoreDir();
  168. String cacheFilePath = cacheDir + "/" + GetFileName(mPDBRequested);
  169. if (CheckPDBData(cacheFilePath, outGuid, outAge))
  170. {
  171. mFinalPDBPath = cacheFilePath;
  172. return;
  173. }
  174. }
  175. void DbgSymRequest::SearchSymSrv()
  176. {
  177. if ((gDebugManager->mSymSrvOptions.mFlags & BfSymSrvFlag_Disable) != 0)
  178. {
  179. mFailed = true;
  180. return;
  181. }
  182. if (mOptions.mCacheDir.IsEmpty())
  183. {
  184. mFailed = true;
  185. return;
  186. }
  187. /*if (mPDBRequested.Contains("IDEHelper"))
  188. Sleep(3000);*/
  189. SetAndRestoreValue<bool> prevSearchingSymSrv(mSearchingSymSrv, true);
  190. NetResult* chainNetResult = NULL;
  191. uint8 outGuid[16];
  192. int32 outAge;
  193. String cacheDir = mOptions.mCacheDir;
  194. cacheDir += GetPDBStoreDir();
  195. //TODO: Remove!
  196. // for (int i = 0; i < 8; i++)
  197. // {
  198. // mDbgSymSrv->mDebugger->OutputMessage(StrFormat("Sleep %d\n", i));
  199. // if (mCancelling)
  200. // break;
  201. // Sleep(1000);
  202. // }
  203. String cacheFilePath = cacheDir + "/" + GetFileName(mPDBRequested);
  204. for (auto& symServAddr : mOptions.mSymbolServers)
  205. {
  206. if (mCancelling)
  207. break;
  208. int colonPos = symServAddr.IndexOf(':');
  209. if (colonPos > 1)
  210. {
  211. // HTTP
  212. bool done = false;
  213. //TODO: Check for 'index2.txt' for two-tiered structure
  214. String checkPath = symServAddr;
  215. checkPath += GetPDBStoreDir();
  216. checkPath += GetFileName(mPDBRequested);
  217. if (Get(checkPath, cacheFilePath, &chainNetResult))
  218. done = true;
  219. if ((!done) && (!mCancelling))
  220. {
  221. // Compressed version
  222. checkPath[checkPath.length() - 1] = '_';
  223. String compCacheFilePath = cacheFilePath;
  224. compCacheFilePath[compCacheFilePath.length() - 1] = '_';
  225. if (Get(checkPath, compCacheFilePath, &chainNetResult))
  226. {
  227. CabFile cabFile;
  228. cabFile.Load(compCacheFilePath);
  229. mDbgSymSrv->mDebugger->OutputRawMessage(StrFormat("symsrv Decompressing '%s'", compCacheFilePath.c_str()));
  230. if (!cabFile.DecompressAll(cacheDir))
  231. {
  232. Fail(cabFile.mError);
  233. return;
  234. }
  235. done = true;
  236. }
  237. }
  238. if ((!done) && (!mCancelling))
  239. {
  240. String checkPath = symServAddr;
  241. checkPath += GetPDBStoreDir();
  242. checkPath += "/file.dir";
  243. String fileDirFilePath = symServAddr;
  244. fileDirFilePath += GetPDBStoreDir();
  245. fileDirFilePath += "/file.dir";
  246. if (Get(checkPath, fileDirFilePath, &chainNetResult, true))
  247. {
  248. // What do I do with this?
  249. }
  250. }
  251. }
  252. else if (!mIsPreCache)
  253. {
  254. // Uncompressed version
  255. String checkPath = symServAddr;
  256. checkPath += GetPDBStoreDir();
  257. checkPath += GetFileName(mPDBRequested);
  258. if (CheckPDBData(checkPath, outGuid, outAge))
  259. {
  260. mFinalPDBPath = checkPath;
  261. return;
  262. }
  263. // Compressed version
  264. checkPath[checkPath.length() - 1] = '_';
  265. if (FileExists(checkPath))
  266. {
  267. CabFile cabFile;
  268. cabFile.Load(checkPath);
  269. mDbgSymSrv->mDebugger->OutputRawMessage(StrFormat("symsrv Decompressing '%s'", checkPath.c_str()));
  270. if (!cabFile.DecompressAll(cacheDir))
  271. {
  272. Fail(cabFile.mError);
  273. return;
  274. }
  275. }
  276. }
  277. if (mIsPreCache)
  278. continue;
  279. // Re-check cache
  280. if (CheckPDBData(cacheFilePath, outGuid, outAge))
  281. {
  282. mFinalPDBPath = cacheFilePath;
  283. return;
  284. }
  285. }
  286. mFailed = true;
  287. }
  288. bool DbgSymRequest::CheckPEFile(const StringImpl& filePath, uint32 fileTime, int size)
  289. {
  290. FileStream fs;
  291. if (!fs.Open(filePath, "rb"))
  292. return false;
  293. PEHeader hdr;
  294. fs.ReadT(hdr);
  295. PE_NTHeaders64 ntHdr64;
  296. fs.SetPos(hdr.e_lfanew);
  297. fs.Read(&ntHdr64, sizeof(PE_NTHeaders64));
  298. if (ntHdr64.mFileHeader.mMachine == PE_MACHINE_X64)
  299. {
  300. if ((ntHdr64.mFileHeader.mTimeDateStamp != fileTime) || (ntHdr64.mOptionalHeader.mSizeOfImage != size))
  301. return false;
  302. }
  303. else
  304. {
  305. PE_NTHeaders32 ntHdr32;
  306. fs.SetPos(hdr.e_lfanew);
  307. fs.Read(&ntHdr32, sizeof(PE_NTHeaders32));
  308. if ((ntHdr32.mFileHeader.mTimeDateStamp != fileTime) || (ntHdr32.mOptionalHeader.mSizeOfImage != size))
  309. return false;
  310. }
  311. return true;
  312. }
  313. String DbgSymRequest::SearchForImage(const String& filePath, uint32 fileTime, int size)
  314. {
  315. if ((gDebugManager->mSymSrvOptions.mFlags & BfSymSrvFlag_Disable) != 0)
  316. return "";
  317. if (FileExists(filePath))
  318. {
  319. if (CheckPEFile(filePath, fileTime, size))
  320. return filePath;
  321. }
  322. /*if (filePath.Contains("IDEHelper"))
  323. Sleep(3000);*/
  324. if (mOptions.mCacheDir.IsEmpty())
  325. {
  326. mFailed = true;
  327. return "";
  328. }
  329. NetResult* chainNetResult = NULL;
  330. String fileName = GetFileName(filePath);
  331. String imageStoreDir = "/";
  332. imageStoreDir += fileName;
  333. imageStoreDir += "/";
  334. imageStoreDir += StrFormat("%08X%x/", fileTime, size);
  335. SetAndRestoreValue<bool> prevSearchingSymSrv(mSearchingSymSrv, true);
  336. String cacheDir = mOptions.mCacheDir;
  337. cacheDir += imageStoreDir;
  338. /*for (int i = 0; i < 8; i++)
  339. {
  340. mDbgSymSrv->mDebugger->OutputMessage(StrFormat("Image Sleep %d\n", i));
  341. if (mCancelling)
  342. break;
  343. Sleep(1000);
  344. }*/
  345. String cacheFilePath = cacheDir + "/" + GetFileName(filePath);
  346. // Check cache
  347. if (CheckPEFile(cacheFilePath, fileTime, size))
  348. {
  349. return cacheFilePath;
  350. }
  351. for (auto& symServAddr : mOptions.mSymbolServers)
  352. {
  353. if (mCancelling)
  354. break;
  355. int colonPos = symServAddr.IndexOf(':');
  356. if (colonPos > 1)
  357. {
  358. // HTTP
  359. bool done = false;
  360. //TODO: Check for 'index2.txt' for two-tiered structure
  361. String checkPath = symServAddr;
  362. checkPath += imageStoreDir;
  363. checkPath += fileName;
  364. if (Get(checkPath, cacheFilePath, &chainNetResult))
  365. done = true;
  366. if ((!done) && (!mCancelling))
  367. {
  368. // Compressed version
  369. checkPath[checkPath.length() - 1] = '_';
  370. String compCacheFilePath = cacheFilePath;
  371. compCacheFilePath[compCacheFilePath.length() - 1] = '_';
  372. if (Get(checkPath, compCacheFilePath, &chainNetResult))
  373. {
  374. mDbgSymSrv->mDebugger->mDebugManager->mOutMessages.push_back(StrFormat("symsrv Decompressing '%s'", checkPath.c_str()));
  375. CabFile cabFile;
  376. cabFile.Load(compCacheFilePath);
  377. if (!cabFile.DecompressAll(cacheDir))
  378. {
  379. Fail(cabFile.mError);
  380. return "";
  381. }
  382. done = true;
  383. }
  384. }
  385. if ((!done) && (!mCancelling))
  386. {
  387. String checkPath = symServAddr;
  388. checkPath += imageStoreDir;
  389. checkPath += "/file.dir";
  390. String fileDirFilePath = symServAddr;
  391. fileDirFilePath += imageStoreDir;
  392. fileDirFilePath += "/file.dir";
  393. if (Get(checkPath, fileDirFilePath, &chainNetResult, true))
  394. {
  395. // What do I do with this?
  396. }
  397. }
  398. }
  399. else if (!mIsPreCache)
  400. {
  401. // Uncompressed version
  402. String checkPath = symServAddr;
  403. checkPath += imageStoreDir;
  404. checkPath += fileName;
  405. if (CheckPEFile(checkPath, fileTime, size))
  406. {
  407. return checkPath;
  408. }
  409. // Compressed version
  410. checkPath[checkPath.length() - 1] = '_';
  411. if (FileExists(checkPath))
  412. {
  413. CabFile cabFile;
  414. cabFile.Load(checkPath);
  415. if (!cabFile.DecompressAll(cacheDir))
  416. {
  417. Fail(cabFile.mError);
  418. return "";
  419. }
  420. }
  421. }
  422. if (mIsPreCache)
  423. continue;
  424. // Re-check cache
  425. if (CheckPEFile(cacheFilePath, fileTime, size))
  426. {
  427. return cacheFilePath;
  428. }
  429. }
  430. if (!mIsPreCache)
  431. {
  432. mDbgSymSrv->mDebugger->mHadImageFindError = true;
  433. 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",
  434. GetFileName(filePath).c_str(), fileTime, size));
  435. }
  436. mFailed = true;
  437. return "";
  438. }
  439. void DbgSymRequest::Cancel()
  440. {
  441. mCancelling = true;
  442. mDbgSymSrv->mDebugger->mDebugManager->mNetManager->CancelCurrent();
  443. }
  444. bool DbgSymRequest::IsDone()
  445. {
  446. AutoCrit autoCrit(mCritSect);
  447. if (!mFinalPDBPath.IsEmpty())
  448. return true;
  449. if (mFailed)
  450. return true;
  451. return false;
  452. }
  453. DbgSymSrv::DbgSymSrv(Debugger* debugger)
  454. {
  455. mDebugger = debugger;
  456. }
  457. void DbgSymSrv::PreCacheImage(const String& filePath, uint32 fileTime, int size)
  458. {
  459. auto request = CreateRequest();
  460. request->mIsPreCache = true;
  461. request->SearchForImage(filePath, fileTime, size);
  462. delete request;
  463. }
  464. DbgSymRequest* DbgSymSrv::CreateRequest(const StringImpl& modulePath, const StringImpl& pdbRequested, uint8 wantGuid[16], int32 wantAge)
  465. {
  466. AutoCrit autoCrit(mDebugger->mDebugManager->mCritSect);
  467. DbgSymRequest* symRequest = new DbgSymRequest();
  468. symRequest->mOptions = mDebugger->mDebugManager->mSymSrvOptions;
  469. symRequest->mDbgSymSrv = this;
  470. symRequest->mModulePath = modulePath;
  471. symRequest->mPDBRequested = pdbRequested;
  472. memcpy(symRequest->mWantGuid, wantGuid, 16);
  473. symRequest->mWantAge = wantAge;
  474. return symRequest;
  475. }
  476. DbgSymRequest* DbgSymSrv::CreateRequest()
  477. {
  478. AutoCrit autoCrit(mDebugger->mDebugManager->mCritSect);
  479. DbgSymRequest* symRequest = new DbgSymRequest();
  480. symRequest->mOptions = mDebugger->mDebugManager->mSymSrvOptions;
  481. symRequest->mDbgSymSrv = this;
  482. return symRequest;
  483. }
  484. void DbgSymSrv::ReleaseRequest(DbgSymRequest* dbgSymRequest)
  485. {
  486. delete dbgSymRequest;
  487. }
  488. void DbgSymSrv::Update()
  489. {
  490. }