Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2827 lines
75 KiB

  1. //----------------------------------------------------------------------------
  2. //
  3. // System methods for targets.
  4. //
  5. // Copyright (C) Microsoft Corporation, 2001-2002.
  6. //
  7. //----------------------------------------------------------------------------
  8. #include "ntsdp.hpp"
  9. #include <common.ver>
  10. ULONG g_UserIdFragmented[LAYER_COUNT];
  11. ULONG g_HighestUserId[LAYER_COUNT];
  12. TargetInfo* g_Target;
  13. ProcessInfo* g_Process;
  14. ThreadInfo* g_Thread;
  15. MachineInfo* g_Machine;
  16. char* g_SuiteMaskNames[] =
  17. {
  18. "SmallBusiness",
  19. "Enterprise",
  20. "BackOffice",
  21. "CommunicationServer",
  22. "TerminalServer",
  23. "SmallBusinessRestricted",
  24. "EmbeddedNT",
  25. "DataCenter",
  26. "SingleUserTS",
  27. "Personal",
  28. "Blade",
  29. "EmbeddedRestricted",
  30. };
  31. PCSTR g_NtSverNames[] =
  32. {
  33. "Windows NT 4", "Windows 2000 RC3", "Windows 2000", "Windows XP",
  34. "Windows Server 2003", "Longhorn",
  35. };
  36. PCSTR g_W9xSverNames[] =
  37. {
  38. "Windows 95", "Windows 98", "Windows 98 SE", "Windows ME",
  39. };
  40. PCSTR g_XBoxSverNames[] =
  41. {
  42. "XBox",
  43. };
  44. PCSTR g_BigSverNames[] =
  45. {
  46. "BIG KD Emulation",
  47. };
  48. PCSTR g_ExdiSverNames[] =
  49. {
  50. "eXDI Device",
  51. };
  52. PCSTR g_NtBdSverNames[] =
  53. {
  54. "Windows Boot Debugger",
  55. };
  56. PCSTR g_EfiSverNames[] =
  57. {
  58. "EFI KD Emulation",
  59. };
  60. PCSTR g_WceSverNames[] =
  61. {
  62. "Windows CE",
  63. };
  64. //----------------------------------------------------------------------------
  65. //
  66. // TargetInfo system methods.
  67. //
  68. //----------------------------------------------------------------------------
  69. void
  70. TargetInfo::ResetSystemInfo(void)
  71. {
  72. m_NumProcesses = 0;
  73. m_ProcessHead = NULL;
  74. m_CurrentProcess = NULL;
  75. m_AllProcessFlags = 0;
  76. m_TotalNumberThreads = 0;
  77. m_MaxThreadsInProcess = 0;
  78. m_SystemId = 0;
  79. m_Exited = FALSE;
  80. m_DeferContinueEvent = FALSE;
  81. m_BreakInTimeout = FALSE;
  82. m_ProcessesAdded = FALSE;
  83. ZeroMemory(&m_TypeInfo, sizeof(m_TypeInfo));
  84. m_SystemVersion = 0;
  85. m_ActualSystemVersion = 0;
  86. m_BuildNumber = 0;
  87. m_CheckedBuild = 0;
  88. m_PlatformId = 0;
  89. m_ServicePackString[0] = 0;
  90. m_ServicePackNumber = 0;
  91. m_BuildLabName[0] = 0;
  92. m_ProductType = INVALID_PRODUCT_TYPE;
  93. m_SuiteMask = 0;
  94. m_NumProcessors = 0;
  95. m_MachineType = IMAGE_FILE_MACHINE_UNKNOWN;
  96. ZeroMemory(&m_Machines, sizeof(m_Machines));
  97. m_Machine = NULL;
  98. ZeroMemory(&m_FirstProcessorId, sizeof(m_FirstProcessorId));
  99. m_MachinesInitialized = FALSE;
  100. m_EffMachineType = IMAGE_FILE_MACHINE_UNKNOWN;
  101. m_EffMachineIndex = MACHIDX_COUNT;
  102. m_EffMachine = NULL;
  103. m_RegContextThread = NULL;
  104. m_RegContextProcessor = -1;
  105. m_SystemRangeStart = 0;
  106. m_SystemCallVirtualAddress = 0;
  107. ZeroMemory(&m_KdDebuggerData, sizeof(m_KdDebuggerData));
  108. ZeroMemory(&m_KdVersion, sizeof(m_KdVersion));
  109. m_KdDebuggerDataOffset = 0;
  110. m_KdApi64 = FALSE;
  111. // The physical cache is suspended by default.
  112. m_PhysicalCache.ChangeSuspend(FALSE);
  113. InvalidateMemoryCaches(FALSE);
  114. m_ExtensionSearchPath = NULL;
  115. ResetImplicitData();
  116. }
  117. void
  118. TargetInfo::DeleteSystemInfo(void)
  119. {
  120. ULONG i;
  121. UnloadTargetExtensionDlls(this);
  122. while (m_ProcessHead)
  123. {
  124. delete m_ProcessHead;
  125. }
  126. for (i = 0; i < MACHIDX_COUNT; i++)
  127. {
  128. delete m_Machines[i];
  129. m_Machines[i] = NULL;
  130. }
  131. free(m_ExtensionSearchPath);
  132. m_ExtensionSearchPath = NULL;
  133. }
  134. HRESULT
  135. TargetInfo::ReadKdDataBlock(ProcessInfo* Process)
  136. {
  137. HRESULT Status;
  138. ULONG Size;
  139. KDDEBUGGER_DATA32 LocalData32;
  140. KDDEBUGGER_DATA64 LocalData64;
  141. if (IS_KERNEL_TRIAGE_DUMP(this) &&
  142. !((KernelTriageDumpTargetInfo*)this)->m_HasDebuggerData)
  143. {
  144. // This dump may have a data block offset in the header
  145. // but the actual data block content is not present so
  146. // don't attempt to read it. This is a normal occurrence
  147. // in older dumps so there's no error message.
  148. return S_FALSE;
  149. }
  150. if (!m_KdDebuggerDataOffset)
  151. {
  152. // NT4 doesn't have a data block so don't display a warning.
  153. if (m_SystemVersion > NT_SVER_NT4)
  154. {
  155. ErrOut("KdDebuggerDataBlock not available!\n");
  156. }
  157. return HRESULT_FROM_WIN32(ERROR_READ_FAULT);
  158. }
  159. //
  160. // Get the size of the KDDEBUGGER_DATA block.
  161. //
  162. if (m_KdApi64)
  163. {
  164. DBGKD_DEBUG_DATA_HEADER64 Header;
  165. Status = ReadAllVirtual(Process,
  166. m_KdDebuggerDataOffset,
  167. &Header, sizeof(Header));
  168. Size = Header.Size;
  169. }
  170. else
  171. {
  172. DBGKD_DEBUG_DATA_HEADER32 Header;
  173. Status = ReadAllVirtual(Process,
  174. m_KdDebuggerDataOffset,
  175. &Header, sizeof(Header));
  176. Size = Header.Size;
  177. }
  178. if (Status != S_OK || !Size)
  179. {
  180. ErrOut("*************************************"
  181. "*************************************\n");
  182. if (IS_DUMP_TARGET(this))
  183. {
  184. ErrOut("THIS DUMP FILE IS PARTIALLY CORRUPT.\n"
  185. "KdDebuggerDataBlock is not present or unreadable.\n");
  186. }
  187. else
  188. {
  189. ErrOut("Unable to read debugger data block header\n");
  190. }
  191. ErrOut("*************************************"
  192. "*************************************\n");
  193. return Status != S_OK ? Status : E_FAIL;
  194. }
  195. // Only read as much of the data block as we can hold in the debugger.
  196. if (Size > sizeof(KDDEBUGGER_DATA64))
  197. {
  198. Size = sizeof(KDDEBUGGER_DATA64);
  199. }
  200. //
  201. // Now read the data
  202. //
  203. if (m_KdApi64)
  204. {
  205. if ((Status = ReadAllVirtual(Process,
  206. m_KdDebuggerDataOffset,
  207. &LocalData64, Size)) != S_OK)
  208. {
  209. ErrOut("KdDebuggerDataBlock could not be read\n");
  210. return Status;
  211. }
  212. if (m_Machine->m_Ptr64)
  213. {
  214. memcpy(&m_KdDebuggerData, &LocalData64, Size);
  215. }
  216. else
  217. {
  218. //
  219. // Sign extended for X86
  220. //
  221. //
  222. // Extend the header so it doesn't get whacked
  223. //
  224. ListEntry32To64((PLIST_ENTRY32)(&LocalData64.Header.List),
  225. &(m_KdDebuggerData.Header.List));
  226. m_KdDebuggerData.Header.OwnerTag =
  227. LocalData64.Header.OwnerTag;
  228. m_KdDebuggerData.Header.Size =
  229. LocalData64.Header.Size;
  230. //
  231. // Sign extend all the 32 bits values to 64 bit
  232. //
  233. #define UIP(f) if (FIELD_OFFSET(KDDEBUGGER_DATA64, f) < Size) \
  234. { \
  235. m_KdDebuggerData.f = \
  236. (ULONG64)(LONG64)(LONG)(LocalData64.f); \
  237. }
  238. #define CPBIT(f) m_KdDebuggerData.f = LocalData64.f;
  239. #define CP(f) if (FIELD_OFFSET(KDDEBUGGER_DATA64, f) < Size) \
  240. { \
  241. m_KdDebuggerData.f = LocalData64.f; \
  242. }
  243. UIP(KernBase);
  244. UIP(BreakpointWithStatus);
  245. UIP(SavedContext);
  246. CP(ThCallbackStack);
  247. CP(NextCallback);
  248. CP(FramePointer);
  249. CPBIT(PaeEnabled);
  250. UIP(KiCallUserMode);
  251. UIP(KeUserCallbackDispatcher);
  252. UIP(PsLoadedModuleList);
  253. UIP(PsActiveProcessHead);
  254. UIP(PspCidTable);
  255. UIP(ExpSystemResourcesList);
  256. UIP(ExpPagedPoolDescriptor);
  257. UIP(ExpNumberOfPagedPools);
  258. UIP(KeTimeIncrement);
  259. UIP(KeBugCheckCallbackListHead);
  260. UIP(KiBugcheckData);
  261. UIP(IopErrorLogListHead);
  262. UIP(ObpRootDirectoryObject);
  263. UIP(ObpTypeObjectType);
  264. UIP(MmSystemCacheStart);
  265. UIP(MmSystemCacheEnd);
  266. UIP(MmSystemCacheWs);
  267. UIP(MmPfnDatabase);
  268. UIP(MmSystemPtesStart);
  269. UIP(MmSystemPtesEnd);
  270. UIP(MmSubsectionBase);
  271. UIP(MmNumberOfPagingFiles);
  272. UIP(MmLowestPhysicalPage);
  273. UIP(MmHighestPhysicalPage);
  274. UIP(MmNumberOfPhysicalPages);
  275. UIP(MmMaximumNonPagedPoolInBytes);
  276. UIP(MmNonPagedSystemStart);
  277. UIP(MmNonPagedPoolStart);
  278. UIP(MmNonPagedPoolEnd);
  279. UIP(MmPagedPoolStart);
  280. UIP(MmPagedPoolEnd);
  281. UIP(MmPagedPoolInformation);
  282. CP(MmPageSize);
  283. UIP(MmSizeOfPagedPoolInBytes);
  284. UIP(MmTotalCommitLimit);
  285. UIP(MmTotalCommittedPages);
  286. UIP(MmSharedCommit);
  287. UIP(MmDriverCommit);
  288. UIP(MmProcessCommit);
  289. UIP(MmPagedPoolCommit);
  290. UIP(MmExtendedCommit);
  291. UIP(MmZeroedPageListHead);
  292. UIP(MmFreePageListHead);
  293. UIP(MmStandbyPageListHead);
  294. UIP(MmModifiedPageListHead);
  295. UIP(MmModifiedNoWritePageListHead);
  296. UIP(MmAvailablePages);
  297. UIP(MmResidentAvailablePages);
  298. UIP(PoolTrackTable);
  299. UIP(NonPagedPoolDescriptor);
  300. UIP(MmHighestUserAddress);
  301. UIP(MmSystemRangeStart);
  302. UIP(MmUserProbeAddress);
  303. UIP(KdPrintCircularBuffer);
  304. UIP(KdPrintCircularBufferEnd);
  305. UIP(KdPrintWritePointer);
  306. UIP(KdPrintRolloverCount);
  307. UIP(MmLoadedUserImageList);
  308. // NT 5.1 additions
  309. UIP(NtBuildLab);
  310. UIP(KiNormalSystemCall);
  311. // NT 5.0 QFE additions
  312. UIP(KiProcessorBlock);
  313. UIP(MmUnloadedDrivers);
  314. UIP(MmLastUnloadedDriver);
  315. UIP(MmTriageActionTaken);
  316. UIP(MmSpecialPoolTag);
  317. UIP(KernelVerifier);
  318. UIP(MmVerifierData);
  319. UIP(MmAllocatedNonPagedPool);
  320. UIP(MmPeakCommitment);
  321. UIP(MmTotalCommitLimitMaximum);
  322. UIP(CmNtCSDVersion);
  323. // NT 5.1 additions
  324. UIP(MmPhysicalMemoryBlock);
  325. UIP(MmSessionBase);
  326. UIP(MmSessionSize);
  327. UIP(MmSystemParentTablePage);
  328. // Server additions
  329. UIP(MmVirtualTranslationBase);
  330. CP(OffsetKThreadNextProcessor);
  331. CP(OffsetKThreadTeb);
  332. CP(OffsetKThreadKernelStack);
  333. CP(OffsetKThreadInitialStack);
  334. CP(OffsetKThreadApcProcess);
  335. CP(OffsetKThreadState);
  336. CP(OffsetKThreadBStore);
  337. CP(OffsetKThreadBStoreLimit);
  338. CP(SizeEProcess);
  339. CP(OffsetEprocessPeb);
  340. CP(OffsetEprocessParentCID);
  341. CP(OffsetEprocessDirectoryTableBase);
  342. CP(SizePrcb);
  343. CP(OffsetPrcbDpcRoutine);
  344. CP(OffsetPrcbCurrentThread);
  345. CP(OffsetPrcbMhz);
  346. CP(OffsetPrcbCpuType);
  347. CP(OffsetPrcbVendorString);
  348. CP(OffsetPrcbProcStateContext);
  349. CP(OffsetPrcbNumber);
  350. CP(SizeEThread);
  351. UIP(KdPrintCircularBufferPtr);
  352. UIP(KdPrintBufferSize);
  353. UIP(KeLoaderBlock);
  354. CP(SizePcr);
  355. CP(OffsetPcrSelfPcr);
  356. CP(OffsetPcrCurrentPrcb);
  357. CP(OffsetPcrContainedPrcb);
  358. CP(OffsetPcrInitialBStore);
  359. CP(OffsetPcrBStoreLimit);
  360. CP(OffsetPcrInitialStack);
  361. CP(OffsetPcrStackLimit);
  362. CP(OffsetPrcbPcrPage);
  363. CP(OffsetPrcbProcStateSpecialReg);
  364. CP(GdtR0Code);
  365. CP(GdtR0Data);
  366. CP(GdtR0Pcr);
  367. CP(GdtR3Code);
  368. CP(GdtR3Data);
  369. CP(GdtR3Teb);
  370. CP(GdtLdt);
  371. CP(GdtTss);
  372. CP(Gdt64R3CmCode);
  373. CP(Gdt64R3CmTeb);
  374. UIP(IopNumTriageDumpDataBlocks);
  375. UIP(IopTriageDumpDataBlocks);
  376. }
  377. }
  378. else
  379. {
  380. if (Size != sizeof(LocalData32))
  381. {
  382. ErrOut("Someone changed the definition of KDDEBUGGER_DATA32 - "
  383. "please fix\n");
  384. return E_FAIL;
  385. }
  386. if ((Status = ReadAllVirtual(Process,
  387. m_KdDebuggerDataOffset,
  388. &LocalData32,
  389. sizeof(LocalData32))) != S_OK)
  390. {
  391. ErrOut("KdDebuggerDataBlock could not be read\n");
  392. return Status;
  393. }
  394. //
  395. // Convert all the 32 bits fields to 64 bit
  396. //
  397. #undef UIP
  398. #undef CP
  399. #define UIP(f) m_KdDebuggerData.f = EXTEND64(LocalData32.f)
  400. #define CP(f) m_KdDebuggerData.f = (LocalData32.f)
  401. //
  402. // Extend the header so it doesn't get whacked
  403. //
  404. ListEntry32To64((PLIST_ENTRY32)(&LocalData32.Header.List),
  405. &(m_KdDebuggerData.Header.List));
  406. m_KdDebuggerData.Header.OwnerTag =
  407. LocalData32.Header.OwnerTag;
  408. m_KdDebuggerData.Header.Size =
  409. LocalData32.Header.Size;
  410. UIP(KernBase);
  411. UIP(BreakpointWithStatus);
  412. UIP(SavedContext);
  413. CP(ThCallbackStack);
  414. CP(NextCallback);
  415. CP(FramePointer);
  416. CP(PaeEnabled);
  417. UIP(KiCallUserMode);
  418. UIP(KeUserCallbackDispatcher);
  419. UIP(PsLoadedModuleList);
  420. UIP(PsActiveProcessHead);
  421. UIP(PspCidTable);
  422. UIP(ExpSystemResourcesList);
  423. UIP(ExpPagedPoolDescriptor);
  424. UIP(ExpNumberOfPagedPools);
  425. UIP(KeTimeIncrement);
  426. UIP(KeBugCheckCallbackListHead);
  427. UIP(KiBugcheckData);
  428. UIP(IopErrorLogListHead);
  429. UIP(ObpRootDirectoryObject);
  430. UIP(ObpTypeObjectType);
  431. UIP(MmSystemCacheStart);
  432. UIP(MmSystemCacheEnd);
  433. UIP(MmSystemCacheWs);
  434. UIP(MmPfnDatabase);
  435. UIP(MmSystemPtesStart);
  436. UIP(MmSystemPtesEnd);
  437. UIP(MmSubsectionBase);
  438. UIP(MmNumberOfPagingFiles);
  439. UIP(MmLowestPhysicalPage);
  440. UIP(MmHighestPhysicalPage);
  441. UIP(MmNumberOfPhysicalPages);
  442. UIP(MmMaximumNonPagedPoolInBytes);
  443. UIP(MmNonPagedSystemStart);
  444. UIP(MmNonPagedPoolStart);
  445. UIP(MmNonPagedPoolEnd);
  446. UIP(MmPagedPoolStart);
  447. UIP(MmPagedPoolEnd);
  448. UIP(MmPagedPoolInformation);
  449. CP(MmPageSize);
  450. UIP(MmSizeOfPagedPoolInBytes);
  451. UIP(MmTotalCommitLimit);
  452. UIP(MmTotalCommittedPages);
  453. UIP(MmSharedCommit);
  454. UIP(MmDriverCommit);
  455. UIP(MmProcessCommit);
  456. UIP(MmPagedPoolCommit);
  457. UIP(MmExtendedCommit);
  458. UIP(MmZeroedPageListHead);
  459. UIP(MmFreePageListHead);
  460. UIP(MmStandbyPageListHead);
  461. UIP(MmModifiedPageListHead);
  462. UIP(MmModifiedNoWritePageListHead);
  463. UIP(MmAvailablePages);
  464. UIP(MmResidentAvailablePages);
  465. UIP(PoolTrackTable);
  466. UIP(NonPagedPoolDescriptor);
  467. UIP(MmHighestUserAddress);
  468. UIP(MmSystemRangeStart);
  469. UIP(MmUserProbeAddress);
  470. UIP(KdPrintCircularBuffer);
  471. UIP(KdPrintCircularBufferEnd);
  472. UIP(KdPrintWritePointer);
  473. UIP(KdPrintRolloverCount);
  474. UIP(MmLoadedUserImageList);
  475. //
  476. // DO NOT ADD ANY FIELDS HERE
  477. // The 32 bit structure should not be changed
  478. //
  479. }
  480. //
  481. // Sanity check the data.
  482. //
  483. if (m_KdDebuggerData.Header.OwnerTag != KDBG_TAG)
  484. {
  485. dprintf("\nKdDebuggerData.Header.OwnerTag is wrong!!!\n");
  486. }
  487. // Update any fields that weren't set from defaults
  488. // based on the system version information.
  489. m_Machine->GetDefaultKdData(&m_KdDebuggerData);
  490. KdOut("ReadKdDataBlock %08lx\n", Status);
  491. KdOut("KernBase %s\n",
  492. FormatAddr64(m_KdDebuggerData.KernBase));
  493. KdOut("BreakpointWithStatus %s\n",
  494. FormatAddr64(m_KdDebuggerData.BreakpointWithStatus));
  495. KdOut("SavedContext %s\n",
  496. FormatAddr64(m_KdDebuggerData.SavedContext));
  497. KdOut("ThCallbackStack %08lx\n",
  498. m_KdDebuggerData.ThCallbackStack);
  499. KdOut("NextCallback %08lx\n",
  500. m_KdDebuggerData.NextCallback);
  501. KdOut("FramePointer %08lx\n",
  502. m_KdDebuggerData.FramePointer);
  503. KdOut("PaeEnabled %08lx\n",
  504. m_KdDebuggerData.PaeEnabled);
  505. KdOut("KiCallUserMode %s\n",
  506. FormatAddr64(m_KdDebuggerData.KiCallUserMode));
  507. KdOut("KeUserCallbackDispatcher %s\n",
  508. FormatAddr64(m_KdDebuggerData.KeUserCallbackDispatcher));
  509. KdOut("PsLoadedModuleList %s\n",
  510. FormatAddr64(m_KdDebuggerData.PsLoadedModuleList));
  511. KdOut("PsActiveProcessHead %s\n",
  512. FormatAddr64(m_KdDebuggerData.PsActiveProcessHead));
  513. KdOut("MmPageSize %s\n",
  514. FormatAddr64(m_KdDebuggerData.MmPageSize));
  515. KdOut("MmLoadedUserImageList %s\n",
  516. FormatAddr64(m_KdDebuggerData.MmLoadedUserImageList));
  517. KdOut("MmSystemRangeStart %s\n",
  518. FormatAddr64(m_KdDebuggerData.MmSystemRangeStart));
  519. KdOut("KiProcessorBlock %s\n",
  520. FormatAddr64(m_KdDebuggerData.KiProcessorBlock));
  521. return S_OK;
  522. }
  523. HRESULT
  524. TargetInfo::QueryKernelInfo(ThreadInfo* Thread, BOOL LoadImage)
  525. {
  526. ULONG Result;
  527. BOOL ReadDataBlock = FALSE;
  528. if (!Thread)
  529. {
  530. return E_INVALIDARG;
  531. }
  532. if (IS_USER_TARGET(this))
  533. {
  534. return E_UNEXPECTED;
  535. }
  536. ProcessInfo* Process = Thread->m_Process;
  537. // If we know where the data block is go ahead
  538. // and read it in. If the read fails it may
  539. // be due to a bogus data block address.
  540. // In that case just fall into the symbol loading
  541. // as we may be able to get the proper data
  542. // block offset from symbols and read it later.
  543. if (m_KdDebuggerDataOffset)
  544. {
  545. if (ReadKdDataBlock(Process) == S_OK)
  546. {
  547. ReadDataBlock = TRUE;
  548. }
  549. }
  550. //
  551. // Load kernel symbols.
  552. //
  553. if (LoadImage)
  554. {
  555. // Remove any previous kernel image if we know where
  556. // the kernel image is supposed to be.
  557. if (m_KdDebuggerData.KernBase)
  558. {
  559. Process->DeleteImageByBase(m_KdDebuggerData.KernBase);
  560. }
  561. //
  562. // Reload the kernel. Reload may be calling QueryKernelInfo
  563. // so this may be a recursive call to Reload. It's restricted
  564. // to just reloading the kernel, though, so we cannot recurse again.
  565. //
  566. PCSTR ArgsRet;
  567. if (Reload(Thread, KERNEL_MODULE_NAME, &ArgsRet) == E_INVALIDARG)
  568. {
  569. // The most likely cause of this is missing paths.
  570. // We don't necessarily need a path to load
  571. // the kernel, so try again and ignore path problems.
  572. Reload(Thread, "-P "KERNEL_MODULE_NAME, &ArgsRet);
  573. }
  574. }
  575. //
  576. // If we haven't already loaded the data block we can now try
  577. // and find the data block using symbols.
  578. //
  579. if (!ReadDataBlock)
  580. {
  581. if (!GetOffsetFromSym(Process,
  582. "nt!KdDebuggerDataBlock",
  583. &m_KdDebuggerDataOffset, NULL))
  584. {
  585. m_KdDebuggerDataOffset = 0;
  586. }
  587. if (ReadKdDataBlock(Process) == S_OK)
  588. {
  589. ReadDataBlock = TRUE;
  590. }
  591. }
  592. // The KD version and KdDebuggerData blocks should agree.
  593. if (ReadDataBlock &&
  594. (m_KdVersion.KernBase != m_KdDebuggerData.KernBase ||
  595. m_KdVersion.PsLoadedModuleList !=
  596. m_KdDebuggerData.PsLoadedModuleList))
  597. {
  598. ErrOut("Debugger can not determine kernel base address\n");
  599. }
  600. if (m_MachineType == IMAGE_FILE_MACHINE_IA64)
  601. {
  602. //
  603. // Try to determine the kernel base virtual mapping address
  604. // for IA64. This should be done as early as possible
  605. // to enable later virtual translations to work.
  606. //
  607. if (!IS_KERNEL_TRIAGE_DUMP(this))
  608. {
  609. if (!m_KdDebuggerData.MmSystemParentTablePage)
  610. {
  611. GetOffsetFromSym(Process, "nt!MmSystemParentTablePage",
  612. &m_KdDebuggerData.MmSystemParentTablePage,
  613. NULL);
  614. }
  615. if (m_KdDebuggerData.MmSystemParentTablePage)
  616. {
  617. ULONG64 SysPtp;
  618. if (ReadAllVirtual(Process,
  619. m_KdDebuggerData.MmSystemParentTablePage,
  620. &SysPtp, sizeof(SysPtp)) == S_OK)
  621. {
  622. ((Ia64MachineInfo*)m_Machines[MACHIDX_IA64])->
  623. SetKernelPageDirectory(SysPtp << IA64_VALID_PFN_SHIFT);
  624. }
  625. }
  626. }
  627. //
  628. // Get the system call address from the debugger data block
  629. // Added around build 2204.
  630. // Default to symbols otherwise.
  631. //
  632. m_SystemCallVirtualAddress = 0;
  633. if (m_KdDebuggerData.KiNormalSystemCall)
  634. {
  635. if (ReadPointer(Process, m_Machine,
  636. m_KdDebuggerData.KiNormalSystemCall,
  637. &m_SystemCallVirtualAddress) != S_OK)
  638. {
  639. m_SystemCallVirtualAddress = 0;
  640. }
  641. }
  642. if (!m_SystemCallVirtualAddress)
  643. {
  644. GetOffsetFromSym(Process, "nt!KiNormalSystemCall",
  645. &m_SystemCallVirtualAddress,
  646. NULL);
  647. }
  648. if (!m_SystemCallVirtualAddress)
  649. {
  650. GetOffsetFromSym(Process, "nt!.KiNormalSystemCall",
  651. &m_SystemCallVirtualAddress,
  652. NULL);
  653. }
  654. if (!m_SystemCallVirtualAddress)
  655. {
  656. WarnOut("Could not get KiNormalSystemCall address\n");
  657. }
  658. }
  659. //
  660. // Now that we have symbols and a data block look
  661. // for any missing data block entries and try to
  662. // resolve them from symbols.
  663. //
  664. if (!IS_KERNEL_TRIAGE_DUMP(this))
  665. {
  666. if (!m_KdDebuggerData.CmNtCSDVersion)
  667. {
  668. GetOffsetFromSym(Process, "nt!CmNtCSDVersion",
  669. &m_KdDebuggerData.CmNtCSDVersion,
  670. NULL);
  671. }
  672. // Do an initial check for the CSD version.
  673. // This may need to be updated later if this
  674. // is early in boot and the CSD version hasn't
  675. // been read from the registry yet.
  676. if (m_KdDebuggerData.CmNtCSDVersion)
  677. {
  678. ULONG CmNtCSDVersion;
  679. if (ReadAllVirtual(Process,
  680. m_KdDebuggerData.CmNtCSDVersion,
  681. &CmNtCSDVersion,
  682. sizeof(CmNtCSDVersion)) == S_OK)
  683. {
  684. SetNtCsdVersion(m_BuildNumber, CmNtCSDVersion);
  685. }
  686. }
  687. if (m_KdDebuggerData.MmUnloadedDrivers == 0)
  688. {
  689. GetOffsetFromSym(Process, "nt!MmUnloadedDrivers",
  690. &m_KdDebuggerData.MmUnloadedDrivers,
  691. NULL);
  692. }
  693. if (m_KdDebuggerData.MmLastUnloadedDriver == 0)
  694. {
  695. GetOffsetFromSym(Process, "nt!MmLastUnloadedDriver",
  696. &m_KdDebuggerData.MmLastUnloadedDriver,
  697. NULL);
  698. }
  699. if (m_KdDebuggerData.KiProcessorBlock == 0)
  700. {
  701. GetOffsetFromSym(Process, "nt!KiProcessorBlock",
  702. &m_KdDebuggerData.KiProcessorBlock,
  703. NULL);
  704. }
  705. if (m_KdDebuggerData.MmPhysicalMemoryBlock == 0)
  706. {
  707. GetOffsetFromSym(Process, "nt!MmPhysicalMemoryBlock",
  708. &m_KdDebuggerData.MmPhysicalMemoryBlock,
  709. NULL);
  710. }
  711. if (m_KdDebuggerData.KeLoaderBlock == 0)
  712. {
  713. GetOffsetFromSym(Process, "nt!KeLoaderBlock",
  714. &m_KdDebuggerData.KeLoaderBlock,
  715. NULL);
  716. }
  717. if (m_KdDebuggerData.IopNumTriageDumpDataBlocks == 0)
  718. {
  719. GetOffsetFromSym(Process, "nt!IopNumTriageDumpDataBlocks",
  720. &m_KdDebuggerData.IopNumTriageDumpDataBlocks,
  721. NULL);
  722. }
  723. if (m_KdDebuggerData.IopTriageDumpDataBlocks == 0)
  724. {
  725. GetOffsetFromSym(Process, "nt!IopTriageDumpDataBlocks",
  726. &m_KdDebuggerData.IopTriageDumpDataBlocks,
  727. NULL);
  728. }
  729. }
  730. //
  731. // Try to get the start of system memory.
  732. // This may be zero because we are looking at an NT 4 system, so try
  733. // looking it up using symbols.
  734. //
  735. if (!m_KdDebuggerData.MmSystemRangeStart)
  736. {
  737. GetOffsetFromSym(Process, "nt!MmSystemRangeStart",
  738. &m_KdDebuggerData.MmSystemRangeStart,
  739. NULL);
  740. }
  741. if (m_KdDebuggerData.MmSystemRangeStart)
  742. {
  743. if (ReadPointer(Process, m_Machine,
  744. m_KdDebuggerData.MmSystemRangeStart,
  745. &m_SystemRangeStart) != S_OK)
  746. {
  747. m_SystemRangeStart = 0;
  748. }
  749. }
  750. //
  751. // If we did not have symbols, at least pick a default value.
  752. //
  753. if (!m_SystemRangeStart)
  754. {
  755. switch(m_MachineType)
  756. {
  757. case IMAGE_FILE_MACHINE_IA64:
  758. m_SystemRangeStart = 0xE000000000000000;
  759. break;
  760. default:
  761. m_SystemRangeStart = 0xFFFFFFFF80000000;
  762. break;
  763. }
  764. }
  765. if (m_KdDebuggerData.KernBase < m_SystemRangeStart)
  766. {
  767. ErrOut("KdDebuggerData.KernBase < SystemRangeStart\n");
  768. }
  769. //
  770. // Read build lab information if possible.
  771. //
  772. m_BuildLabName[0] = 0;
  773. Result = 0;
  774. if (m_KdDebuggerData.NtBuildLab)
  775. {
  776. ULONG PreLen;
  777. strcpy(m_BuildLabName, "Built by: ");
  778. PreLen = strlen(m_BuildLabName);
  779. if (ReadVirtual(Process, m_KdDebuggerData.NtBuildLab,
  780. m_BuildLabName + PreLen,
  781. sizeof(m_BuildLabName) - PreLen - 1,
  782. &Result) == S_OK &&
  783. Result >= 2)
  784. {
  785. Result += PreLen;
  786. }
  787. }
  788. DBG_ASSERT(Result < sizeof(m_BuildLabName));
  789. m_BuildLabName[Result] = 0;
  790. if (GetProductInfo(&m_ProductType, &m_SuiteMask) != S_OK)
  791. {
  792. m_ProductType = INVALID_PRODUCT_TYPE;
  793. m_SuiteMask = 0;
  794. }
  795. return S_OK;
  796. }
  797. void
  798. TargetInfo::SetNtCsdVersion(ULONG Build, ULONG CsdVersion)
  799. {
  800. m_ServicePackNumber = CsdVersion;
  801. if (CsdVersion == 0)
  802. {
  803. m_ServicePackString[0] = 0;
  804. return;
  805. }
  806. PSTR Str = m_ServicePackString;
  807. *Str = 0;
  808. if (CsdVersion & 0xFFFF)
  809. {
  810. sprintf(Str, "Service Pack %u", (CsdVersion & 0xFF00) >> 8);
  811. Str += strlen(Str);
  812. if (CsdVersion & 0xFF)
  813. {
  814. *Str++ = 'A' + (char)(CsdVersion & 0xFF) - 1;
  815. *Str = 0;
  816. }
  817. }
  818. if (CsdVersion & 0xFFFF0000)
  819. {
  820. // Prior to 2600 the upper word has two fields for
  821. // the release. For XPSPs it's just a release number.
  822. if (Build >= 2600)
  823. {
  824. sprintf(Str, ".%u", CsdVersion >> 16);
  825. }
  826. else
  827. {
  828. if (CsdVersion & 0xFFFF)
  829. {
  830. strcpy(Str, ", ");
  831. Str += strlen(Str);
  832. }
  833. sprintf(Str, "RC %u", (CsdVersion >> 24) & 0xFF);
  834. Str += strlen(Str);
  835. if (CsdVersion & 0x00FF0000)
  836. {
  837. sprintf(Str, ".%u", (CsdVersion >> 16) & 0xFF);
  838. Str += strlen(Str);
  839. }
  840. }
  841. }
  842. }
  843. void
  844. TargetInfo::SetKernel32BuildString(ProcessInfo* Process)
  845. {
  846. m_BuildLabName[0] = 0;
  847. ImageInfo* Image = Process->
  848. FindImageByName("kernel32", 8, INAME_MODULE, FALSE);
  849. if (!Image)
  850. {
  851. return;
  852. }
  853. //
  854. // Try and look up the build lab information from kernel32.
  855. //
  856. char Item[64];
  857. ULONG PreLen;
  858. sprintf(Item, "\\StringFileInfo\\%04x%04x\\FileVersion",
  859. VER_VERSION_TRANSLATION);
  860. strcpy(m_BuildLabName, "kernel32.dll version: ");
  861. PreLen = strlen(m_BuildLabName);
  862. if (FAILED(GetImageVersionInformation
  863. (Process, Image->m_ImagePath, Image->m_BaseOfImage,
  864. Item, m_BuildLabName + PreLen,
  865. sizeof(m_BuildLabName) - PreLen, NULL)))
  866. {
  867. m_BuildLabName[0] = 0;
  868. }
  869. }
  870. HRESULT
  871. TargetInfo::CreateVirtualProcess(ULONG Threads)
  872. {
  873. HRESULT Status;
  874. ProcessInfo* Process;
  875. ULONG Id;
  876. // Create the virtual process. Add the system ID
  877. // to the fake process ID base to keep each system's
  878. // fake processes separate from each other.
  879. Id = VIRTUAL_PROCESS_ID_BASE + m_UserId;
  880. Process = new ProcessInfo(this, Id,
  881. VIRTUAL_PROCESS_HANDLE(Id),
  882. (ULONG64)VIRTUAL_PROCESS_HANDLE(Id),
  883. 0, DEBUG_PROCESS_ONLY_THIS_PROCESS);
  884. if (!Process)
  885. {
  886. return E_OUTOFMEMORY;
  887. }
  888. if ((Status = Process->CreateVirtualThreads(0, Threads)) != S_OK)
  889. {
  890. delete Process;
  891. }
  892. return Status;
  893. }
  894. ProcessInfo*
  895. TargetInfo::FindProcessByUserId(ULONG Id)
  896. {
  897. ProcessInfo* Process;
  898. ForTargetProcesses(this)
  899. {
  900. if (Process->m_UserId == Id)
  901. {
  902. return Process;
  903. }
  904. }
  905. return NULL;
  906. }
  907. ProcessInfo*
  908. TargetInfo::FindProcessBySystemId(ULONG Id)
  909. {
  910. ProcessInfo* Process;
  911. ForTargetProcesses(this)
  912. {
  913. if (Process->m_SystemId == Id)
  914. {
  915. return Process;
  916. }
  917. }
  918. return NULL;
  919. }
  920. ProcessInfo*
  921. TargetInfo::FindProcessByHandle(ULONG64 Handle)
  922. {
  923. ProcessInfo* Process;
  924. ForTargetProcesses(this)
  925. {
  926. if (Process->m_SysHandle == Handle)
  927. {
  928. return Process;
  929. }
  930. }
  931. return NULL;
  932. }
  933. void
  934. TargetInfo::InsertProcess(ProcessInfo* Process)
  935. {
  936. ProcessInfo* Cur;
  937. ProcessInfo* Prev;
  938. Prev = NULL;
  939. for (Cur = m_ProcessHead; Cur; Cur = Cur->m_Next)
  940. {
  941. if (Cur->m_UserId > Process->m_UserId)
  942. {
  943. break;
  944. }
  945. Prev = Cur;
  946. }
  947. Process->m_Next = Cur;
  948. if (!Prev)
  949. {
  950. m_ProcessHead = Process;
  951. }
  952. else
  953. {
  954. Prev->m_Next = Process;
  955. }
  956. m_NumProcesses++;
  957. Process->m_Target = this;
  958. if (!m_CurrentProcess)
  959. {
  960. m_CurrentProcess = Process;
  961. }
  962. m_TotalNumberThreads += Process->m_NumThreads;
  963. if (Process->m_NumThreads > m_MaxThreadsInProcess)
  964. {
  965. m_MaxThreadsInProcess = Process->m_NumThreads;
  966. }
  967. m_AllProcessFlags |= Process->m_Flags;
  968. }
  969. void
  970. TargetInfo::RemoveProcess(ProcessInfo* Process)
  971. {
  972. ProcessInfo* Cur;
  973. ProcessInfo* Prev;
  974. Prev = NULL;
  975. for (Cur = m_ProcessHead; Cur; Cur = Cur->m_Next)
  976. {
  977. if (Cur == Process)
  978. {
  979. break;
  980. }
  981. Prev = Cur;
  982. }
  983. if (!Cur)
  984. {
  985. return;
  986. }
  987. if (!Prev)
  988. {
  989. m_ProcessHead = Process->m_Next;
  990. }
  991. else
  992. {
  993. Prev->m_Next = Process->m_Next;
  994. }
  995. m_NumProcesses--;
  996. Process->m_Target = NULL;
  997. if (m_CurrentProcess == Process)
  998. {
  999. m_CurrentProcess = m_ProcessHead;
  1000. }
  1001. ResetAllProcessInfo();
  1002. }
  1003. void
  1004. TargetInfo::ResetAllProcessInfo(void)
  1005. {
  1006. ProcessInfo* Process;
  1007. m_TotalNumberThreads = 0;
  1008. m_AllProcessFlags = 0;
  1009. m_MaxThreadsInProcess = 0;
  1010. ForTargetProcesses(this)
  1011. {
  1012. m_TotalNumberThreads += Process->m_NumThreads;
  1013. m_AllProcessFlags |= Process->m_Flags;
  1014. if (Process->m_NumThreads > m_MaxThreadsInProcess)
  1015. {
  1016. m_MaxThreadsInProcess = Process->m_NumThreads;
  1017. }
  1018. }
  1019. }
  1020. void
  1021. TargetInfo::AddThreadToAllProcessInfo(ProcessInfo* Process,
  1022. ThreadInfo* Thread)
  1023. {
  1024. m_TotalNumberThreads++;
  1025. if (Process->m_NumThreads > m_MaxThreadsInProcess)
  1026. {
  1027. m_MaxThreadsInProcess = Process->m_NumThreads;
  1028. }
  1029. }
  1030. BOOL
  1031. TargetInfo::DeleteExitedInfos(void)
  1032. {
  1033. ProcessInfo* Process;
  1034. ProcessInfo* ProcessNext;
  1035. BOOL DeletedSomething = FALSE;
  1036. for (Process = m_ProcessHead; Process; Process = ProcessNext)
  1037. {
  1038. ProcessNext = Process->m_Next;
  1039. if (Process->m_Exited)
  1040. {
  1041. delete Process;
  1042. DeletedSomething = TRUE;
  1043. }
  1044. else
  1045. {
  1046. if (Process->DeleteExitedInfos())
  1047. {
  1048. DeletedSomething = TRUE;
  1049. }
  1050. }
  1051. }
  1052. return DeletedSomething;
  1053. }
  1054. void
  1055. TargetInfo::InvalidateMemoryCaches(BOOL VirtOnly)
  1056. {
  1057. ProcessInfo* Process;
  1058. ForTargetProcesses(this)
  1059. {
  1060. Process->m_VirtualCache.Empty();
  1061. }
  1062. if (!VirtOnly)
  1063. {
  1064. m_PhysicalCache.Empty();
  1065. }
  1066. }
  1067. void
  1068. TargetInfo::SetSystemVersionAndBuild(ULONG Build, ULONG PlatformId)
  1069. {
  1070. switch(PlatformId)
  1071. {
  1072. case VER_PLATFORM_WIN32_NT:
  1073. m_ActualSystemVersion = NtBuildToSystemVersion(Build);
  1074. m_SystemVersion = m_ActualSystemVersion;
  1075. break;
  1076. case VER_PLATFORM_WIN32_WINDOWS:
  1077. // Win9x puts the major and minor versions in the high word
  1078. // of the build number so mask them off.
  1079. Build &= 0xffff;
  1080. m_ActualSystemVersion = Win9xBuildToSystemVersion(Build);
  1081. // Win98SE was the first Win9x version to support
  1082. // the extended registers thread context flag.
  1083. if (m_ActualSystemVersion >= W9X_SVER_W98SE)
  1084. {
  1085. m_SystemVersion = NT_SVER_W2K;
  1086. }
  1087. else
  1088. {
  1089. m_SystemVersion = NT_SVER_NT4;
  1090. }
  1091. break;
  1092. case VER_PLATFORM_WIN32_CE:
  1093. m_ActualSystemVersion = WinCeBuildToSystemVersion(Build);
  1094. m_SystemVersion = NT_SVER_NT4;
  1095. break;
  1096. }
  1097. m_BuildNumber = Build;
  1098. }
  1099. HRESULT
  1100. TargetInfo::InitializeForProcessor(void)
  1101. {
  1102. HRESULT Status;
  1103. ULONG i;
  1104. //
  1105. // Get the base processor ID for determing what
  1106. // kind of features a processor supports. The
  1107. // assumption is that the processors in a machine
  1108. // will be similar enough that retrieving this
  1109. // for one processor is sufficient.
  1110. // If this fails we continue on without a processor ID.
  1111. //
  1112. if (!IS_DUMP_TARGET(this))
  1113. {
  1114. GetProcessorId(0, &m_FirstProcessorId);
  1115. }
  1116. // Initialize with whatever processor ID information we have.
  1117. for (i = 0; i < MACHIDX_COUNT; i++)
  1118. {
  1119. if ((Status = m_Machines[i]->InitializeForProcessor()) != S_OK)
  1120. {
  1121. return Status;
  1122. }
  1123. }
  1124. return Status;
  1125. }
  1126. HRESULT
  1127. TargetInfo::InitializeMachines(ULONG MachineType)
  1128. {
  1129. HRESULT Status;
  1130. ULONG i;
  1131. if (MachineTypeIndex(MachineType) == MACHIDX_COUNT)
  1132. {
  1133. return E_INVALIDARG;
  1134. }
  1135. if (m_KdApi64 != (m_SystemVersion > NT_SVER_NT4))
  1136. {
  1137. WarnOut("Debug API version does not match system version\n");
  1138. }
  1139. if (IsImageMachineType64(MachineType) && !m_KdApi64)
  1140. {
  1141. WarnOut("64-bit machine not using 64-bit API\n");
  1142. }
  1143. //
  1144. // First create the initial machine instances and perform
  1145. // basic initialization.
  1146. //
  1147. for (i = 0; i < MACHIDX_COUNT; i++)
  1148. {
  1149. m_Machines[i] = NewMachineInfo(i, MachineType, this);
  1150. if (m_Machines[i] == NULL)
  1151. {
  1152. return E_OUTOFMEMORY;
  1153. }
  1154. if ((Status = m_Machines[i]->Initialize()) != S_OK)
  1155. {
  1156. return Status;
  1157. }
  1158. }
  1159. m_MachineType = MachineType;
  1160. m_Machine = MachineTypeInfo(this, MachineType);
  1161. m_Machine->GetSystemTypeInfo(&m_TypeInfo);
  1162. if (IS_KERNEL_TARGET(this))
  1163. {
  1164. m_Machine->GetDefaultKdData(&m_KdDebuggerData);
  1165. }
  1166. SetEffMachine(m_MachineType, FALSE);
  1167. // X86 prefers registers to be displayed at the prompt unless
  1168. // we're on a kernel connection where it would force a context
  1169. // load all the time.
  1170. if (MachineType == IMAGE_FILE_MACHINE_I386 &&
  1171. (IS_DUMP_TARGET(this) || IS_USER_TARGET(this)))
  1172. {
  1173. g_OciOutputRegs = TRUE;
  1174. }
  1175. m_MachinesInitialized = TRUE;
  1176. return S_OK;
  1177. }
  1178. void
  1179. TargetInfo::SetEffMachine(ULONG Machine, BOOL Notify)
  1180. {
  1181. BOOL Changed = m_EffMachineType != Machine;
  1182. if (Changed &&
  1183. m_EffMachineType != IMAGE_FILE_MACHINE_UNKNOWN &&
  1184. m_EffMachineType != m_MachineType)
  1185. {
  1186. // If the previous machine was not the target machine
  1187. // it may be an emulated machine that uses the
  1188. // target machine's context. In that case we need to
  1189. // make sure that any dirty registers it has get flushed
  1190. // so that if the new effective machine is the target
  1191. // machine it'll show changes due to changes through
  1192. // the emulated machine.
  1193. if (m_EffMachine->SetContext() != S_OK)
  1194. {
  1195. // Error already displayed.
  1196. return;
  1197. }
  1198. }
  1199. m_EffMachineType = Machine;
  1200. m_EffMachineIndex = MachineTypeIndex(Machine);
  1201. DBG_ASSERT(m_EffMachineIndex <= MACHIDX_COUNT);
  1202. m_EffMachine = m_Machines[m_EffMachineIndex];
  1203. if (g_Target == this)
  1204. {
  1205. g_Machine = m_EffMachine;
  1206. if (Changed && Notify)
  1207. {
  1208. NotifyChangeEngineState(DEBUG_CES_EFFECTIVE_PROCESSOR,
  1209. m_EffMachineType, TRUE);
  1210. }
  1211. }
  1212. }
  1213. void
  1214. TargetInfo::ChangeRegContext(ThreadInfo* Thread)
  1215. {
  1216. if (Thread && Thread->m_Process->m_Target != this)
  1217. {
  1218. ErrOut("ChangeRegContext to invalid thread\n");
  1219. return;
  1220. }
  1221. if (Thread != m_RegContextThread)
  1222. {
  1223. ULONG i;
  1224. // Flush any old thread context.
  1225. // We need to be careful when flushing context to
  1226. // NT4 boxes at the initial module load because the
  1227. // system is in a very fragile state and writing
  1228. // back the context generally causes a bugcheck 50.
  1229. if (m_RegContextThread != NULL &&
  1230. m_RegContextThread->m_Handle != NULL &&
  1231. (IS_USER_TARGET(this) ||
  1232. m_ActualSystemVersion != NT_SVER_NT4 ||
  1233. g_LastEventType != DEBUG_EVENT_LOAD_MODULE))
  1234. {
  1235. HRESULT Hr;
  1236. // If we're flushing register context we need to make
  1237. // sure that all machines involved are flushed so
  1238. // that context information actually gets sent to
  1239. // the target.
  1240. // First flush the secondary machines to accumulate
  1241. // context in the primary machine.
  1242. Hr = S_OK;
  1243. for (i = 0; i < MACHIDX_COUNT; i++)
  1244. {
  1245. if (m_Machines[i] != m_Machine)
  1246. {
  1247. Hr = m_Machines[i]->SetContext();
  1248. if (Hr != S_OK)
  1249. {
  1250. break;
  1251. }
  1252. }
  1253. }
  1254. // Now flush the primary machine.
  1255. if (Hr == S_OK)
  1256. {
  1257. Hr = m_Machine->SetContext();
  1258. }
  1259. if (Hr != S_OK)
  1260. {
  1261. ErrOut("MachineInfo::SetContext failed - Thread: %N "
  1262. "Handle: %I64x Id: %x - Error == 0x%X\n",
  1263. m_RegContextThread,
  1264. m_RegContextThread->m_Handle,
  1265. m_RegContextThread->m_SystemId,
  1266. Hr);
  1267. }
  1268. }
  1269. m_RegContextThread = Thread;
  1270. if (m_RegContextThread != NULL)
  1271. {
  1272. m_RegContextProcessor =
  1273. VIRTUAL_THREAD_INDEX(m_RegContextThread->m_Handle);
  1274. // We've now selected a new source of processor data so
  1275. // all machines, both emulated and direct, must be invalidated.
  1276. for (i = 0; i < MACHIDX_COUNT; i++)
  1277. {
  1278. m_Machines[i]->InvalidateContext();
  1279. }
  1280. }
  1281. else
  1282. {
  1283. m_RegContextProcessor = -1;
  1284. }
  1285. g_LastSelector = -1;
  1286. }
  1287. }
  1288. void
  1289. TargetInfo::FlushRegContext(void)
  1290. {
  1291. ThreadInfo* CurThread = m_RegContextThread;
  1292. ChangeRegContext(NULL);
  1293. ChangeRegContext(CurThread);
  1294. }
  1295. BOOL
  1296. TargetInfo::AnySystemProcesses(BOOL LocalOnly)
  1297. {
  1298. ULONG Flags = m_AllProcessFlags;
  1299. // There isn't any way to really know that a particular
  1300. // system is local or remote so just always assume
  1301. // local to be conservative.
  1302. if (IS_LIVE_USER_TARGET(this))
  1303. {
  1304. Flags |= ((LiveUserTargetInfo*)this)->m_AllPendingFlags;
  1305. }
  1306. return (Flags & ENG_PROC_SYSTEM) != 0;
  1307. }
  1308. void
  1309. TargetInfo::OutputProcessesAndThreads(PSTR Title)
  1310. {
  1311. ProcessInfo* Process;
  1312. ThreadInfo* Thread;
  1313. ImageInfo* Image;
  1314. // Kernel mode only has a virtual process and threads right
  1315. // now so it isn't particularly interesting.
  1316. if (IS_KERNEL_TARGET(this))
  1317. {
  1318. return;
  1319. }
  1320. VerbOut("OUTPUT_PROCESS: %s\n", Title);
  1321. Process = m_ProcessHead;
  1322. while (Process)
  1323. {
  1324. VerbOut("id: %x Handle: %I64x index: %d\n",
  1325. Process->m_SystemId,
  1326. Process->m_SysHandle,
  1327. Process->m_UserId);
  1328. Thread = Process->m_ThreadHead;
  1329. while (Thread)
  1330. {
  1331. VerbOut(" id: %x hThread: %I64x index: %d addr: %s\n",
  1332. Thread->m_SystemId,
  1333. Thread->m_Handle,
  1334. Thread->m_UserId,
  1335. FormatAddr64(Thread->m_StartOffset));
  1336. Thread = Thread->m_Next;
  1337. }
  1338. Image = Process->m_ImageHead;
  1339. while (Image)
  1340. {
  1341. VerbOut(" hFile: %I64x base: %s\n",
  1342. (ULONG64)((ULONG_PTR)Image->m_File),
  1343. FormatAddr64(Image->m_BaseOfImage));
  1344. Image = Image->m_Next;
  1345. }
  1346. Process = Process->m_Next;
  1347. }
  1348. }
  1349. void
  1350. TargetInfo::OutputProcessInfo(ProcessInfo* Match)
  1351. {
  1352. ProcessInfo* Process;
  1353. Process = m_ProcessHead;
  1354. while (Process)
  1355. {
  1356. if (Match == NULL || Match == Process)
  1357. {
  1358. char CurMark;
  1359. PSTR DebugKind;
  1360. if (Process == g_Process)
  1361. {
  1362. CurMark = '.';
  1363. }
  1364. else if (Process == g_EventProcess)
  1365. {
  1366. CurMark = '#';
  1367. }
  1368. else
  1369. {
  1370. CurMark = ' ';
  1371. }
  1372. DebugKind = "child";
  1373. if (Process->m_Exited)
  1374. {
  1375. DebugKind = "exited";
  1376. }
  1377. else if (Process->m_Flags & ENG_PROC_ATTACHED)
  1378. {
  1379. DebugKind = (Process->m_Flags & ENG_PROC_SYSTEM) ?
  1380. "system" : "attach";
  1381. }
  1382. else if (Process->m_Flags & ENG_PROC_CREATED)
  1383. {
  1384. DebugKind = "create";
  1385. }
  1386. else if (Process->m_Flags & ENG_PROC_EXAMINED)
  1387. {
  1388. DebugKind = "examine";
  1389. }
  1390. dprintf("%c%3ld\tid: %lx\t%s\tname: %s\n",
  1391. CurMark,
  1392. Process->m_UserId,
  1393. Process->m_SystemId,
  1394. DebugKind,
  1395. Process->GetExecutableImageName());
  1396. }
  1397. Process = Process->m_Next;
  1398. }
  1399. }
  1400. void
  1401. TargetInfo::OutputVersion(void)
  1402. {
  1403. BOOL MpMachine;
  1404. if (IS_USER_TARGET(this))
  1405. {
  1406. dprintf("%s ", SystemVersionName(m_ActualSystemVersion));
  1407. }
  1408. else
  1409. {
  1410. dprintf("%s Kernel ", SystemVersionName(m_ActualSystemVersion));
  1411. }
  1412. dprintf("Version %u", m_BuildNumber);
  1413. //
  1414. // Service packs do not necessarily revise the kernel so
  1415. // the CSD version is actually read from the registry.
  1416. // This means there's a time during boot when the CSD
  1417. // version isn't set. If the debugger connects then it
  1418. // won't think this is a service pack build. In order
  1419. // to get around this poll for updates.
  1420. //
  1421. if (IS_CONN_KERNEL_TARGET(this) &&
  1422. !m_ServicePackString[0] &&
  1423. m_KdDebuggerData.CmNtCSDVersion)
  1424. {
  1425. ULONG CmNtCSDVersion;
  1426. if (ReadAllVirtual(m_ProcessHead,
  1427. m_KdDebuggerData.CmNtCSDVersion,
  1428. &CmNtCSDVersion,
  1429. sizeof(CmNtCSDVersion)) == S_OK)
  1430. {
  1431. SetNtCsdVersion(m_BuildNumber, CmNtCSDVersion);
  1432. }
  1433. }
  1434. // Win9x seems to set the CSD string to a space which isn't
  1435. // very interesting so ignore it.
  1436. if (m_ServicePackString[0] &&
  1437. strcmp(m_ServicePackString, " ") != 0)
  1438. {
  1439. dprintf(" (%s)", m_ServicePackString);
  1440. }
  1441. MpMachine = IS_LIVE_KERNEL_TARGET(this) ?
  1442. ((m_KdVersion.Flags & DBGKD_VERS_FLAG_MP) != 0) :
  1443. (m_NumProcessors > 1);
  1444. dprintf(" %s ", MpMachine ? "MP" : "UP");
  1445. if (MpMachine)
  1446. {
  1447. dprintf("(%d procs) ", m_NumProcessors);
  1448. }
  1449. dprintf("%s %s\n",
  1450. m_CheckedBuild == 0xC ? "Checked" : "Free",
  1451. m_Machine != NULL ?
  1452. m_Machine->m_FullName : "");
  1453. if (m_ProductType != INVALID_PRODUCT_TYPE)
  1454. {
  1455. dprintf("Product: ");
  1456. switch(m_ProductType)
  1457. {
  1458. case NtProductWinNt:
  1459. dprintf("WinNt");
  1460. break;
  1461. case NtProductLanManNt:
  1462. dprintf("LanManNt");
  1463. break;
  1464. case NtProductServer:
  1465. dprintf("Server");
  1466. break;
  1467. default:
  1468. dprintf("<%x>", m_ProductType);
  1469. break;
  1470. }
  1471. if (m_SuiteMask)
  1472. {
  1473. ULONG i;
  1474. dprintf(", suite:");
  1475. for (i = 0; i < MaxSuiteType; i++)
  1476. {
  1477. if (m_SuiteMask & (1 << i))
  1478. {
  1479. if (i < DIMA(g_SuiteMaskNames))
  1480. {
  1481. dprintf(" %s", g_SuiteMaskNames[i]);
  1482. }
  1483. else
  1484. {
  1485. dprintf(" <%x>", (1 << i));
  1486. }
  1487. }
  1488. }
  1489. }
  1490. dprintf("\n");
  1491. }
  1492. if (m_BuildLabName[0])
  1493. {
  1494. dprintf("%s\n", m_BuildLabName);
  1495. }
  1496. if (IS_KERNEL_TARGET(this))
  1497. {
  1498. dprintf("Kernel base = 0x%s PsLoadedModuleList = 0x%s\n",
  1499. FormatAddr64(m_KdDebuggerData.KernBase),
  1500. FormatAddr64(m_KdDebuggerData.PsLoadedModuleList));
  1501. }
  1502. OutputTime();
  1503. }
  1504. void
  1505. TargetInfo::OutputTime(void)
  1506. {
  1507. ULONG64 TimeDateN = GetCurrentTimeDateN();
  1508. if (TimeDateN)
  1509. {
  1510. dprintf("Debug session time: %s\n",
  1511. TimeToStr(FileTimeToTimeDateStamp(TimeDateN)));
  1512. }
  1513. ULONG64 UpTimeN = GetCurrentSystemUpTimeN();
  1514. if (UpTimeN)
  1515. {
  1516. dprintf("System Uptime: %s\n", DurationToStr(UpTimeN));
  1517. }
  1518. else
  1519. {
  1520. dprintf("System Uptime: not available\n");
  1521. }
  1522. if (IS_USER_TARGET(this))
  1523. {
  1524. ULONG64 UpTimeProcessN;
  1525. // In the startup time output we often don't have
  1526. // a process, but some targets don't require one so
  1527. // let the target decide.
  1528. UpTimeProcessN = GetProcessUpTimeN(g_Process);
  1529. if (UpTimeProcessN)
  1530. {
  1531. dprintf("Process Uptime: %s\n", DurationToStr(UpTimeProcessN));
  1532. }
  1533. else
  1534. {
  1535. dprintf("Process Uptime: not available\n");
  1536. }
  1537. }
  1538. }
  1539. void
  1540. TargetInfo::AddSpecificExtensions(void)
  1541. {
  1542. // Only notify once for all the adds in this function;
  1543. g_EngNotify++;
  1544. //
  1545. // Now that we have determined the type of architecture,
  1546. // we can load the right debugger extensions
  1547. //
  1548. if (m_ActualSystemVersion > BIG_SVER_START &&
  1549. m_ActualSystemVersion < BIG_SVER_END)
  1550. {
  1551. goto Refresh;
  1552. }
  1553. if (m_ActualSystemVersion > XBOX_SVER_START &&
  1554. m_ActualSystemVersion < XBOX_SVER_END)
  1555. {
  1556. AddExtensionDll("kdextx86", FALSE, this, NULL);
  1557. goto Refresh;
  1558. }
  1559. if (m_ActualSystemVersion > NTBD_SVER_START &&
  1560. m_ActualSystemVersion < NTBD_SVER_END)
  1561. {
  1562. goto Refresh;
  1563. }
  1564. if (IS_KERNEL_TARGET(this))
  1565. {
  1566. //
  1567. // Assume kernel mode is NT at this point.
  1568. //
  1569. if (m_MachineType == IMAGE_FILE_MACHINE_IA64)
  1570. {
  1571. //
  1572. // We rely on force loading of extensions at the end of this
  1573. // routine in order to get the entry point the debugger needs.
  1574. //
  1575. AddExtensionDll("wow64exts", FALSE, this, NULL);
  1576. }
  1577. if (m_MachineType == IMAGE_FILE_MACHINE_I386 &&
  1578. m_SystemVersion > NT_SVER_START &&
  1579. m_SystemVersion <= NT_SVER_W2K)
  1580. {
  1581. AddExtensionDll("kdextx86", FALSE, this, NULL);
  1582. }
  1583. else
  1584. {
  1585. //
  1586. // For all new architectures and new X86 builds, load
  1587. // kdexts
  1588. //
  1589. AddExtensionDll("kdexts", FALSE, this, NULL);
  1590. }
  1591. //
  1592. // Extensions that work on all versions of the OS for kernel mode
  1593. // Many of these are messages about legacy extensions.
  1594. AddExtensionDll("kext", FALSE, this, NULL);
  1595. }
  1596. else
  1597. {
  1598. //
  1599. // User mode only extensions.
  1600. //
  1601. if (m_ActualSystemVersion > NT_SVER_START &&
  1602. m_ActualSystemVersion < NT_SVER_END)
  1603. {
  1604. AddExtensionDll("ntsdexts", FALSE, this, NULL);
  1605. }
  1606. AddExtensionDll("uext", FALSE, this, NULL);
  1607. }
  1608. if (m_ActualSystemVersion > NT_SVER_W2K &&
  1609. m_ActualSystemVersion < NT_SVER_END)
  1610. {
  1611. AddExtensionDll("exts", FALSE, this, NULL);
  1612. }
  1613. // Load ext.dll for all versions
  1614. AddExtensionDll("ext", FALSE, NULL, NULL);
  1615. Refresh:
  1616. // Always load the Dbghelp extensions last so they are first on the list
  1617. AddExtensionDll("dbghelp", FALSE, NULL, NULL);
  1618. EXTDLL *Ext;
  1619. for (Ext = g_ExtDlls; Ext != NULL; Ext = Ext->Next)
  1620. {
  1621. LoadExtensionDll(this, Ext);
  1622. }
  1623. g_EngNotify--;
  1624. NotifyChangeEngineState(DEBUG_CES_EXTENSIONS, 0, TRUE);
  1625. }
  1626. void
  1627. TargetInfo::PrepareForExecution(void)
  1628. {
  1629. ProcessInfo* Process;
  1630. ChangeRegContext(NULL);
  1631. ForTargetProcesses(this)
  1632. {
  1633. Process->PrepareForExecution();
  1634. }
  1635. ResetImplicitData();
  1636. FlushSelectorCache();
  1637. m_PhysicalCache.Empty();
  1638. for (ULONG i = 0; i < MACHIDX_COUNT; i++)
  1639. {
  1640. m_Machines[i]->FlushPerExecutionCaches();
  1641. }
  1642. }
  1643. //----------------------------------------------------------------------------
  1644. //
  1645. // LiveKernelTargetInfo.
  1646. //
  1647. //----------------------------------------------------------------------------
  1648. void
  1649. LiveKernelTargetInfo::ResetSystemInfo(void)
  1650. {
  1651. m_KdMaxPacketType = 0;
  1652. m_KdMaxStateChange = 0;
  1653. m_KdMaxManipulate = 0;
  1654. TargetInfo::ResetSystemInfo();
  1655. }
  1656. HRESULT
  1657. LiveKernelTargetInfo::InitFromKdVersion(void)
  1658. {
  1659. HRESULT Status;
  1660. BOOL Ptr64;
  1661. if ((Status = GetTargetKdVersion(&m_KdVersion)) != S_OK)
  1662. {
  1663. ErrOut("Debugger can't get KD version information, %s\n",
  1664. FormatStatusCode(Status));
  1665. ZeroMemory(&m_KdVersion, sizeof(m_KdVersion));
  1666. return Status;
  1667. }
  1668. if (DBGKD_MAJOR_TYPE(m_KdVersion.MajorVersion) >= DBGKD_MAJOR_COUNT)
  1669. {
  1670. ErrOut("KD version has unknown kernel type\n");
  1671. ZeroMemory(&m_KdVersion, sizeof(m_KdVersion));
  1672. return E_INVALIDARG;
  1673. }
  1674. if (MachineTypeIndex(m_KdVersion.MachineType) == MACHIDX_COUNT)
  1675. {
  1676. ErrOut("KD version has unknown processor architecture\n");
  1677. ZeroMemory(&m_KdVersion, sizeof(m_KdVersion));
  1678. return E_INVALIDARG;
  1679. }
  1680. Ptr64 =
  1681. ((m_KdVersion.Flags & DBGKD_VERS_FLAG_PTR64) == DBGKD_VERS_FLAG_PTR64);
  1682. // Reloads cause the version to be retrieved but
  1683. // we don't want to completely reinitialize machines
  1684. // in that case as some settings can be lost. Only
  1685. // reinitialize if there's a need to do so.
  1686. BOOL MustInitializeMachines =
  1687. m_MachineType != m_KdVersion.MachineType ||
  1688. m_Machine == NULL;
  1689. m_MachineType = m_KdVersion.MachineType;
  1690. m_BuildNumber = m_KdVersion.MinorVersion;
  1691. m_CheckedBuild = m_KdVersion.MajorVersion & 0xFF;
  1692. //
  1693. // Determine the OS running.
  1694. //
  1695. switch(DBGKD_MAJOR_TYPE(m_KdVersion.MajorVersion))
  1696. {
  1697. case DBGKD_MAJOR_NT:
  1698. case DBGKD_MAJOR_TNT:
  1699. m_PlatformId = VER_PLATFORM_WIN32_NT;
  1700. m_ActualSystemVersion = NtBuildToSystemVersion(m_BuildNumber);
  1701. m_SystemVersion = m_ActualSystemVersion;
  1702. break;
  1703. case DBGKD_MAJOR_XBOX:
  1704. m_PlatformId = VER_PLATFORM_WIN32_NT;
  1705. m_ActualSystemVersion = XBOX_SVER_1;
  1706. m_SystemVersion = NT_SVER_W2K;
  1707. break;
  1708. case DBGKD_MAJOR_BIG:
  1709. m_PlatformId = VER_PLATFORM_WIN32_NT;
  1710. m_ActualSystemVersion = BIG_SVER_1;
  1711. m_SystemVersion = NT_SVER_W2K;
  1712. break;
  1713. case DBGKD_MAJOR_EXDI:
  1714. m_PlatformId = VER_PLATFORM_WIN32_NT;
  1715. m_ActualSystemVersion = EXDI_SVER_1;
  1716. m_SystemVersion = NT_SVER_W2K;
  1717. break;
  1718. case DBGKD_MAJOR_NTBD:
  1719. // Special mode for the NT boot debugger where
  1720. // the full system hasn't started yet.
  1721. m_PlatformId = VER_PLATFORM_WIN32_NT;
  1722. m_ActualSystemVersion = NTBD_SVER_XP;
  1723. m_SystemVersion = NtBuildToSystemVersion(m_BuildNumber);
  1724. break;
  1725. case DBGKD_MAJOR_EFI:
  1726. m_PlatformId = VER_PLATFORM_WIN32_NT;
  1727. m_ActualSystemVersion = EFI_SVER_1;
  1728. m_SystemVersion = NT_SVER_XP;
  1729. break;
  1730. }
  1731. //
  1732. // Pre-XP kernels didn't set these values so default them appropriately.
  1733. //
  1734. m_KdMaxPacketType = m_KdVersion.MaxPacketType;
  1735. if (m_SystemVersion < NT_SVER_XP ||
  1736. m_KdMaxPacketType == 0 ||
  1737. m_KdMaxPacketType > PACKET_TYPE_MAX)
  1738. {
  1739. m_KdMaxPacketType = PACKET_TYPE_KD_CONTROL_REQUEST + 1;
  1740. }
  1741. m_KdMaxStateChange = m_KdVersion.MaxStateChange + DbgKdMinimumStateChange;
  1742. if (m_SystemVersion < NT_SVER_XP ||
  1743. m_KdMaxStateChange == DbgKdMinimumStateChange ||
  1744. m_KdMaxStateChange > DbgKdMaximumStateChange)
  1745. {
  1746. m_KdMaxStateChange = DbgKdLoadSymbolsStateChange + 1;
  1747. }
  1748. m_KdMaxManipulate = m_KdVersion.MaxManipulate + DbgKdMinimumManipulate;
  1749. if (m_SystemVersion < NT_SVER_XP ||
  1750. m_KdMaxManipulate == DbgKdMinimumManipulate ||
  1751. m_KdMaxManipulate > DbgKdMaximumManipulate)
  1752. {
  1753. m_KdMaxManipulate = DbgKdCheckLowMemoryApi + 1;
  1754. }
  1755. if (MustInitializeMachines)
  1756. {
  1757. InitializeMachines(m_MachineType);
  1758. }
  1759. m_KdDebuggerData.PsLoadedModuleList = m_KdVersion.PsLoadedModuleList;
  1760. m_KdDebuggerData.KernBase = m_KdVersion.KernBase;
  1761. if (!m_KdVersion.DebuggerDataList)
  1762. {
  1763. // The debugger data list is always NULL early in boot
  1764. // so don't warn repeatedly. Also, NT4 didn't have
  1765. // a loader block so don't warn at all.
  1766. if (m_SystemVersion > NT_SVER_NT4 &&
  1767. (g_EngErr & ENG_ERR_DEBUGGER_DATA) == 0)
  1768. {
  1769. ErrOut("Debugger data list address is NULL\n");
  1770. }
  1771. }
  1772. // Using NULL for the process disables connected KD's
  1773. // PTE translation. The non-paged list data does not require
  1774. // it and a virtual translation causes all kinds of side
  1775. // effects that are undesirable at this point in initialization.
  1776. else if (ReadPointer(NULL, m_Machine,
  1777. m_KdVersion.DebuggerDataList,
  1778. &m_KdDebuggerDataOffset) != S_OK)
  1779. {
  1780. ErrOut("Unable to read head of debugger data list\n");
  1781. m_KdDebuggerDataOffset = 0;
  1782. }
  1783. KdOut("Target MajorVersion %08lx\n",
  1784. m_KdVersion.MajorVersion);
  1785. KdOut("Target MinorVersion %08lx\n",
  1786. m_KdVersion.MinorVersion);
  1787. KdOut("Target ProtocolVersion %08lx\n",
  1788. m_KdVersion.ProtocolVersion);
  1789. KdOut("Target Flags %08lx\n",
  1790. m_KdVersion.Flags);
  1791. KdOut("Target MachineType %08lx\n",
  1792. m_KdVersion.MachineType);
  1793. KdOut("Target MaxPacketType %x\n",
  1794. m_KdVersion.MaxPacketType);
  1795. KdOut("Target MaxStateChange %x\n",
  1796. m_KdVersion.MaxStateChange);
  1797. KdOut("Target MaxManipulate %x\n",
  1798. m_KdVersion.MaxManipulate);
  1799. KdOut("Target KernBase %s\n",
  1800. FormatAddr64(m_KdVersion.KernBase));
  1801. KdOut("Target PsLoadedModuleList %s\n",
  1802. FormatAddr64(m_KdVersion.PsLoadedModuleList));
  1803. KdOut("Target DebuggerDataList %s\n",
  1804. FormatAddr64(m_KdVersion.DebuggerDataList));
  1805. dprintf("Connected to %s %d %s target, ptr64 %s\n",
  1806. SystemVersionName(m_ActualSystemVersion),
  1807. m_BuildNumber,
  1808. m_Machine->m_FullName,
  1809. m_Machine->m_Ptr64 ? "TRUE" : "FALSE");
  1810. return S_OK;
  1811. }
  1812. //----------------------------------------------------------------------------
  1813. //
  1814. // ConnLiveKernelTargetInfo.
  1815. //
  1816. //----------------------------------------------------------------------------
  1817. void
  1818. ConnLiveKernelTargetInfo::ResetSystemInfo(void)
  1819. {
  1820. m_SwitchProcessor = 0;
  1821. LiveKernelTargetInfo::ResetSystemInfo();
  1822. }
  1823. //----------------------------------------------------------------------------
  1824. //
  1825. // LiveUserTargetInfo.
  1826. //
  1827. //----------------------------------------------------------------------------
  1828. void
  1829. LiveUserTargetInfo::DeleteSystemInfo(void)
  1830. {
  1831. DiscardPendingProcesses();
  1832. TargetInfo::DeleteSystemInfo();
  1833. }
  1834. HRESULT
  1835. LiveUserTargetInfo::SetServices(PUSER_DEBUG_SERVICES Services, BOOL Remote)
  1836. {
  1837. HRESULT Status;
  1838. if ((Status = Services->Initialize(&m_ServiceFlags)) != S_OK)
  1839. {
  1840. return Status;
  1841. }
  1842. m_Services = Services;
  1843. if (Remote)
  1844. {
  1845. char MachName[MAX_COMPUTERNAME_LENGTH + 1];
  1846. char TransId[DBGRPC_MAX_IDENTITY];
  1847. m_Local = FALSE;
  1848. if (FAILED(Services->
  1849. GetConnectionInfo(MachName, sizeof(MachName),
  1850. NULL, 0,
  1851. TransId, sizeof(TransId))))
  1852. {
  1853. strcpy(m_ProcessServer, "<Remote>");
  1854. }
  1855. else
  1856. {
  1857. PrintString(m_ProcessServer, DIMA(m_ProcessServer),
  1858. "%s (%s)", MachName, TransId);
  1859. }
  1860. }
  1861. return S_OK;
  1862. }
  1863. HRESULT
  1864. LiveUserTargetInfo::InitFromServices(void)
  1865. {
  1866. HRESULT Status;
  1867. ULONG Machine;
  1868. if ((Status = m_Services->
  1869. GetTargetInfo(&Machine,
  1870. &m_NumProcessors,
  1871. &m_PlatformId,
  1872. &m_BuildNumber,
  1873. &m_CheckedBuild,
  1874. m_ServicePackString,
  1875. sizeof(m_ServicePackString),
  1876. m_BuildLabName,
  1877. sizeof(m_BuildLabName),
  1878. &m_ProductType,
  1879. &m_SuiteMask)) != S_OK)
  1880. {
  1881. ErrOut("Unable to retrieve target machine information\n");
  1882. return Status;
  1883. }
  1884. SetSystemVersionAndBuild(m_BuildNumber, m_PlatformId);
  1885. m_KdApi64 = m_SystemVersion > NT_SVER_NT4;
  1886. // User mode can retrieve processor information at any time
  1887. // so we can immediately call InitializeForProcessor.
  1888. if ((Status = InitializeMachines(Machine)) != S_OK ||
  1889. (Status = InitializeForProcessor()) != S_OK)
  1890. {
  1891. ErrOut("Unable to initialize target machine information\n");
  1892. }
  1893. return Status;
  1894. }
  1895. //----------------------------------------------------------------------------
  1896. //
  1897. // Generic layer support.
  1898. //
  1899. //----------------------------------------------------------------------------
  1900. void
  1901. SetLayersFromTarget(TargetInfo* Target)
  1902. {
  1903. g_Target = Target;
  1904. g_Process = NULL;
  1905. g_Thread = NULL;
  1906. g_Machine = NULL;
  1907. if (g_Target)
  1908. {
  1909. g_Machine = g_Target->m_EffMachine;
  1910. g_Process = g_Target->m_CurrentProcess;
  1911. if (g_Process)
  1912. {
  1913. g_Thread = g_Process->m_CurrentThread;
  1914. }
  1915. }
  1916. }
  1917. void
  1918. SetLayersFromProcess(ProcessInfo* Process)
  1919. {
  1920. g_Process = Process;
  1921. g_Target = NULL;
  1922. g_Thread = NULL;
  1923. g_Machine = NULL;
  1924. if (g_Process)
  1925. {
  1926. g_Target = g_Process->m_Target;
  1927. g_Machine = g_Target->m_EffMachine;
  1928. g_Thread = g_Process->m_CurrentThread;
  1929. }
  1930. }
  1931. void
  1932. SetLayersFromThread(ThreadInfo* Thread)
  1933. {
  1934. g_Thread = Thread;
  1935. g_Target = NULL;
  1936. g_Process = NULL;
  1937. g_Machine = NULL;
  1938. if (g_Thread)
  1939. {
  1940. g_Process = g_Thread->m_Process;
  1941. g_Target = g_Process->m_Target;
  1942. g_Machine = g_Target->m_EffMachine;
  1943. }
  1944. }
  1945. void
  1946. SetToAnyLayers(BOOL SetPrompt)
  1947. {
  1948. if (!g_Target)
  1949. {
  1950. SetLayersFromTarget(g_TargetHead);
  1951. }
  1952. else if (!g_Process)
  1953. {
  1954. SetLayersFromProcess(g_Target->m_ProcessHead);
  1955. }
  1956. else if (!g_Thread)
  1957. {
  1958. SetLayersFromThread(g_Process->m_ThreadHead);
  1959. }
  1960. if (SetPrompt && g_Thread)
  1961. {
  1962. SetPromptThread(g_Thread, SPT_DEFAULT_OCI_FLAGS);
  1963. }
  1964. }
  1965. ULONG
  1966. FindNextUserId(LAYER Layer)
  1967. {
  1968. //
  1969. // It is very common for many layers to be created
  1970. // without any being deleted. Optimize this case
  1971. // with a simple incremental ID.
  1972. //
  1973. if (g_UserIdFragmented[Layer] == 0)
  1974. {
  1975. ULONG Id = g_HighestUserId[Layer]++;
  1976. return Id;
  1977. }
  1978. ULONG UserId = 0;
  1979. TargetInfo* Target;
  1980. ProcessInfo* Process;
  1981. ThreadInfo* Thread;
  1982. //
  1983. // Find the lowest unused ID across all layers.
  1984. // Every layer is given a unique ID to make identification
  1985. // simple and unambiguous.
  1986. //
  1987. for (;;)
  1988. {
  1989. BOOL Match = FALSE;
  1990. ForTargets()
  1991. {
  1992. if (Layer == LAYER_TARGET)
  1993. {
  1994. if (Target->m_UserId == UserId)
  1995. {
  1996. Match = TRUE;
  1997. break;
  1998. }
  1999. }
  2000. else
  2001. {
  2002. ForTargetProcesses(Target)
  2003. {
  2004. if (Layer == LAYER_PROCESS)
  2005. {
  2006. if (Process->m_UserId == UserId)
  2007. {
  2008. Match = TRUE;
  2009. break;
  2010. }
  2011. }
  2012. else
  2013. {
  2014. ForProcessThreads(Process)
  2015. {
  2016. if (Thread->m_UserId == UserId)
  2017. {
  2018. Match = TRUE;
  2019. break;
  2020. }
  2021. }
  2022. if (Match)
  2023. {
  2024. break;
  2025. }
  2026. }
  2027. }
  2028. if (Match)
  2029. {
  2030. break;
  2031. }
  2032. }
  2033. }
  2034. if (Match)
  2035. {
  2036. // There was a match so try the next ID.
  2037. UserId++;
  2038. }
  2039. else
  2040. {
  2041. // No match, use the ID.
  2042. break;
  2043. }
  2044. }
  2045. return UserId;
  2046. }
  2047. //----------------------------------------------------------------------------
  2048. //
  2049. // Functions.
  2050. //
  2051. //----------------------------------------------------------------------------
  2052. TargetInfo*
  2053. FindTargetByUserId(ULONG Id)
  2054. {
  2055. TargetInfo* Target;
  2056. ForAllLayersToTarget()
  2057. {
  2058. if (Target->m_UserId == Id)
  2059. {
  2060. return Target;
  2061. }
  2062. }
  2063. return NULL;
  2064. }
  2065. TargetInfo*
  2066. FindTargetBySystemId(ULONG SysId)
  2067. {
  2068. TargetInfo* Target;
  2069. ForAllLayersToTarget()
  2070. {
  2071. if (Target->m_SystemId == SysId)
  2072. {
  2073. return Target;
  2074. }
  2075. }
  2076. return NULL;
  2077. }
  2078. TargetInfo*
  2079. FindTargetByServer(ULONG64 Server)
  2080. {
  2081. TargetInfo* Target;
  2082. ForAllLayersToTarget()
  2083. {
  2084. if (IS_LIVE_USER_TARGET(Target))
  2085. {
  2086. LiveUserTargetInfo* UserTarget = (LiveUserTargetInfo*)Target;
  2087. if ((UserTarget->m_Local && Server == 0) ||
  2088. (!UserTarget->m_Local &&
  2089. (ULONG64)UserTarget->m_Services == Server))
  2090. {
  2091. return Target;
  2092. }
  2093. }
  2094. }
  2095. return NULL;
  2096. }
  2097. void
  2098. SuspendAllThreads(void)
  2099. {
  2100. TargetInfo* Target;
  2101. ForAllLayersToTarget()
  2102. {
  2103. Target->SuspendThreads();
  2104. }
  2105. }
  2106. BOOL
  2107. ResumeAllThreads(void)
  2108. {
  2109. TargetInfo* Target;
  2110. BOOL Error = FALSE;
  2111. ForAllLayersToTarget()
  2112. {
  2113. if (!Target->ResumeThreads())
  2114. {
  2115. Error = TRUE;
  2116. }
  2117. }
  2118. if (Error)
  2119. {
  2120. ErrOut("No active threads to run in event process %d\n",
  2121. g_EventProcess->m_UserId);
  2122. return FALSE;
  2123. }
  2124. return TRUE;
  2125. }
  2126. BOOL
  2127. DeleteAllExitedInfos(void)
  2128. {
  2129. TargetInfo* Target;
  2130. TargetInfo* TargetNext;
  2131. BOOL DeletedSomething = FALSE;
  2132. for (Target = g_TargetHead; Target; Target = TargetNext)
  2133. {
  2134. TargetNext = Target->m_Next;
  2135. if (Target->m_Exited)
  2136. {
  2137. delete Target;
  2138. DeletedSomething = TRUE;
  2139. }
  2140. else
  2141. {
  2142. if (Target->DeleteExitedInfos())
  2143. {
  2144. DeletedSomething = TRUE;
  2145. Target->OutputProcessesAndThreads("*** exit cleanup ***");
  2146. }
  2147. }
  2148. }
  2149. return DeletedSomething;
  2150. }
  2151. BOOL
  2152. AnyActiveProcesses(BOOL FinalOnly)
  2153. {
  2154. TargetInfo* Target;
  2155. ForAllLayersToTarget()
  2156. {
  2157. if (Target->m_ProcessHead ||
  2158. (!FinalOnly &&
  2159. IS_LIVE_USER_TARGET(Target) &&
  2160. ((LiveUserTargetInfo*)Target)->m_ProcessPending))
  2161. {
  2162. return TRUE;
  2163. }
  2164. }
  2165. return FALSE;
  2166. }
  2167. BOOL
  2168. AnySystemProcesses(BOOL LocalOnly)
  2169. {
  2170. TargetInfo* Target;
  2171. ForAllLayersToTarget()
  2172. {
  2173. if (Target->AnySystemProcesses(LocalOnly))
  2174. {
  2175. return TRUE;
  2176. }
  2177. }
  2178. return FALSE;
  2179. }
  2180. ULONG
  2181. AllProcessFlags(void)
  2182. {
  2183. TargetInfo* Target;
  2184. ULONG Flags;
  2185. Flags = 0;
  2186. ForAllLayersToTarget()
  2187. {
  2188. Flags |= Target->m_AllProcessFlags;
  2189. }
  2190. return Flags;
  2191. }
  2192. BOOL
  2193. AnyLiveUserTargets(void)
  2194. {
  2195. TargetInfo* Target;
  2196. ForAllLayersToTarget()
  2197. {
  2198. if (IS_LIVE_USER_TARGET(Target))
  2199. {
  2200. return TRUE;
  2201. }
  2202. }
  2203. return FALSE;
  2204. }
  2205. void
  2206. InvalidateAllMemoryCaches(void)
  2207. {
  2208. TargetInfo* Target;
  2209. ForAllLayersToTarget()
  2210. {
  2211. Target->InvalidateMemoryCaches(FALSE);
  2212. }
  2213. }
  2214. HANDLE
  2215. GloballyUniqueProcessHandle(TargetInfo* Target, ULONG64 FullHandle)
  2216. {
  2217. HANDLE Unique;
  2218. ProcessInfo* Process;
  2219. //
  2220. // With multiple systems it's possible for processes
  2221. // from different systems to have the same process handle.
  2222. // dbghelp's module list uses only the basic process handle
  2223. // as an identifier so we have to avoid collisions between
  2224. // process handles among all systems. This is a very difficult
  2225. // problem in general so we just go for a simple solution
  2226. // that should work most of the time:
  2227. // We primarily care about NT and NT process handles are currently
  2228. // always relatively small integers. Put the system ID in
  2229. // the topmost 8 bits to give each system its own ID space.
  2230. // This can break at any time, but at the moment it doesn't
  2231. // seem like the underlying assumptions will be invalidated
  2232. // for a long time.
  2233. //
  2234. Unique = (HANDLE)(ULONG_PTR)
  2235. ((Target->m_UserId << 24) | ((ULONG)FullHandle & 0xffffff));
  2236. // Check for uniqueness to make sure.
  2237. ForAllLayersToProcess()
  2238. {
  2239. if (Process->m_SymHandle == Unique)
  2240. {
  2241. ErrOut("ERROR: Ambiguous symbol process handle: %I64x\n",
  2242. (ULONG64)(ULONG_PTR)Unique);
  2243. return Unique;
  2244. }
  2245. }
  2246. return Unique;
  2247. }
  2248. ULONG
  2249. NtBuildToSystemVersion(ULONG Build)
  2250. {
  2251. if (Build > 3499)
  2252. {
  2253. return NT_SVER_NET_SERVER;
  2254. }
  2255. else if (Build > 2195)
  2256. {
  2257. return NT_SVER_XP;
  2258. }
  2259. else if (Build > 2183)
  2260. {
  2261. return NT_SVER_W2K;
  2262. }
  2263. else if (Build > 1381)
  2264. {
  2265. return NT_SVER_W2K_RC3;
  2266. }
  2267. else
  2268. {
  2269. return NT_SVER_NT4;
  2270. }
  2271. }
  2272. // Taken from http://kbinternal/kb/articles/q158/2/38.htm
  2273. //
  2274. // Release Version File dates
  2275. // ---------------------------------------------------------------------
  2276. // Windows 95 retail, OEM 4.00.950 7/11/95
  2277. // Windows 95 retail SP1 4.00.950A 7/11/95
  2278. // OEM Service Release 1 4.00.950A 7/11/95
  2279. // OEM Service Release 2 4.00.1111* (4.00.950B) 8/24/96
  2280. // OEM Service Release 2.1 4.03.1212-1214* (4.00.950B) 8/24/96-8/27/97
  2281. // OEM Service Release 2.5 4.03.1214* (4.00.950C) 8/24/96-11/18/97
  2282. // Windows 98 retail, OEM 4.10.1998 5/11/98
  2283. // Windows 98 Second Edition 4.10.2222A 4/23/99
  2284. ULONG
  2285. Win9xBuildToSystemVersion(ULONG Build)
  2286. {
  2287. if (Build > 2222)
  2288. {
  2289. return W9X_SVER_WME;
  2290. }
  2291. else if (Build > 1998)
  2292. {
  2293. return W9X_SVER_W98SE;
  2294. }
  2295. else if (Build > 950)
  2296. {
  2297. return W9X_SVER_W98;
  2298. }
  2299. else
  2300. {
  2301. return W9X_SVER_W95;
  2302. }
  2303. }
  2304. ULONG
  2305. WinCeBuildToSystemVersion(ULONG Build)
  2306. {
  2307. return WCE_SVER_CE;
  2308. }
  2309. PCSTR
  2310. SystemVersionName(ULONG Sver)
  2311. {
  2312. if (Sver > NT_SVER_START && Sver < NT_SVER_END)
  2313. {
  2314. return g_NtSverNames[Sver - NT_SVER_START - 1];
  2315. }
  2316. else if (Sver > W9X_SVER_START && Sver < W9X_SVER_END)
  2317. {
  2318. return g_W9xSverNames[Sver - W9X_SVER_START - 1];
  2319. }
  2320. else if (Sver > XBOX_SVER_START && Sver < XBOX_SVER_END)
  2321. {
  2322. return g_XBoxSverNames[Sver - XBOX_SVER_START - 1];
  2323. }
  2324. else if (Sver > BIG_SVER_START && Sver < BIG_SVER_END)
  2325. {
  2326. return g_BigSverNames[Sver - BIG_SVER_START - 1];
  2327. }
  2328. else if (Sver > EXDI_SVER_START && Sver < EXDI_SVER_END)
  2329. {
  2330. return g_ExdiSverNames[Sver - EXDI_SVER_START - 1];
  2331. }
  2332. else if (Sver > NTBD_SVER_START && Sver < NTBD_SVER_END)
  2333. {
  2334. return g_NtBdSverNames[Sver - NTBD_SVER_START - 1];
  2335. }
  2336. else if (Sver > EFI_SVER_START && Sver < EFI_SVER_END)
  2337. {
  2338. return g_EfiSverNames[Sver - EFI_SVER_START - 1];
  2339. }
  2340. else if (Sver > WCE_SVER_START && Sver < WCE_SVER_END)
  2341. {
  2342. return g_WceSverNames[Sver - WCE_SVER_START - 1];
  2343. }
  2344. return "Unknown System";
  2345. }
  2346. void
  2347. ParseSystemCommands(void)
  2348. {
  2349. TargetInfo* Target;
  2350. char Ch;
  2351. ULONG UserId;
  2352. if (!g_Target)
  2353. {
  2354. error(BADSYSTEM);
  2355. }
  2356. Ch = PeekChar();
  2357. Target = g_Target;
  2358. g_CurCmd++;
  2359. if (Ch == 0 || Ch == ';')
  2360. {
  2361. Target = NULL;
  2362. g_CurCmd--;
  2363. }
  2364. else if (Ch == '.')
  2365. {
  2366. // Use the current target.
  2367. }
  2368. else if (Ch == '#')
  2369. {
  2370. Target = g_EventTarget;
  2371. }
  2372. else if (Ch == '*')
  2373. {
  2374. Target = NULL;
  2375. }
  2376. else if (Ch == '[')
  2377. {
  2378. g_CurCmd--;
  2379. UserId = (ULONG)GetTermExpression("System ID missing from");
  2380. Target = FindTargetByUserId(UserId);
  2381. if (Target == NULL)
  2382. {
  2383. error(BADSYSTEM);
  2384. }
  2385. }
  2386. else if (Ch >= '0' && Ch <= '9')
  2387. {
  2388. UserId = 0;
  2389. do
  2390. {
  2391. UserId = UserId * 10 + Ch - '0';
  2392. Ch = *g_CurCmd++;
  2393. } while (Ch >= '0' && Ch <= '9');
  2394. g_CurCmd--;
  2395. Target = FindTargetByUserId(UserId);
  2396. if (Target == NULL)
  2397. {
  2398. error(BADSYSTEM);
  2399. }
  2400. }
  2401. else
  2402. {
  2403. g_CurCmd--;
  2404. }
  2405. Ch = PeekChar();
  2406. if (Ch == '\0' || Ch == ';')
  2407. {
  2408. TargetInfo* Cur;
  2409. for (Cur = g_TargetHead; Cur; Cur = Cur->m_Next)
  2410. {
  2411. if (Target && Cur != Target)
  2412. {
  2413. continue;
  2414. }
  2415. if (Cur == g_Target)
  2416. {
  2417. Ch = '.';
  2418. }
  2419. else if (Cur == g_EventTarget)
  2420. {
  2421. Ch = '#';
  2422. }
  2423. else
  2424. {
  2425. Ch = ' ';
  2426. }
  2427. dprintf("%c%3d ", Ch, Cur->m_UserId);
  2428. char Buf[2 * MAX_PATH];
  2429. Cur->GetDescription(Buf, sizeof(Buf), NULL);
  2430. dprintf("%s\n", Buf);
  2431. }
  2432. }
  2433. else
  2434. {
  2435. g_CurCmd++;
  2436. if (tolower(Ch) == 's')
  2437. {
  2438. if (Target == NULL)
  2439. {
  2440. error(BADSYSTEM);
  2441. }
  2442. if (Target == g_Target)
  2443. {
  2444. return;
  2445. }
  2446. if (Target->m_CurrentProcess == NULL)
  2447. {
  2448. Target->m_CurrentProcess = Target->m_ProcessHead;
  2449. if (Target->m_CurrentProcess == NULL)
  2450. {
  2451. error(BADSYSTEM);
  2452. }
  2453. }
  2454. if (Target->m_CurrentProcess->m_CurrentThread == NULL)
  2455. {
  2456. Target->m_CurrentProcess->m_CurrentThread =
  2457. Target->m_CurrentProcess->m_ThreadHead;
  2458. if (Target->m_CurrentProcess->m_CurrentThread == NULL)
  2459. {
  2460. error(BADSYSTEM);
  2461. }
  2462. }
  2463. Target->SwitchToTarget(g_Target);
  2464. }
  2465. else
  2466. {
  2467. g_CurCmd--;
  2468. }
  2469. }
  2470. }