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.

348 lines
11 KiB

  1. //----------------------------------------------------------------------------
  2. //
  3. // Process abstraction.
  4. //
  5. // Copyright (C) Microsoft Corporation, 2001-2002.
  6. //
  7. //----------------------------------------------------------------------------
  8. #ifndef __PROCESS_HPP__
  9. #define __PROCESS_HPP__
  10. // In the kernel debugger there is a virtual process representing
  11. // kernel space. It has an artificial handle and threads
  12. // representing each processor in the machine.
  13. // A similar scheme is used in user dump files where real
  14. // handles don't exist.
  15. // With multiple systems there may be multiple kernel spaces,
  16. // so the system user ID is added to the base fake ID to
  17. // create a unique virtual process ID.
  18. #define VIRTUAL_PROCESS_ID_BASE 0xf0f0f0f0
  19. #define VIRTUAL_PROCESS_HANDLE(Id) ((HANDLE)(ULONG_PTR)(Id))
  20. enum COR_DLL
  21. {
  22. // These enumerants are ordered to
  23. // allow mscorwks/svr to override
  24. // mscoree as the "known" COR image
  25. // by value comparison.
  26. COR_DLL_INVALID,
  27. COR_DLL_EE,
  28. COR_DLL_WKS,
  29. COR_DLL_SVR,
  30. };
  31. //----------------------------------------------------------------------------
  32. //
  33. // Thread and process information is much different bewteen
  34. // user and kernel debugging. The structures exist and are
  35. // as common as possible to enable common code.
  36. //
  37. // In user debugging process and thread info track the system
  38. // processes and threads being debugged.
  39. //
  40. // In kernel debugging there is only one process that represents
  41. // kernel space. There is one thread per processor, each
  42. // representing that processor's thread state.
  43. //
  44. //----------------------------------------------------------------------------
  45. #define ENG_PROC_ATTACHED 0x00000001
  46. #define ENG_PROC_CREATED 0x00000002
  47. #define ENG_PROC_EXAMINED 0x00000004
  48. #define ENG_PROC_ATTACH_EXISTING 0x00000008
  49. // Currently the only system process specially marked is CSR.
  50. #define ENG_PROC_SYSTEM 0x00000010
  51. #define ENG_PROC_NO_SUSPEND_RESUME 0x00000020
  52. #define ENG_PROC_NO_INITIAL_BREAK 0x00000040
  53. #define ENG_PROC_RESUME_AT_ATTACH 0x00000080
  54. #define ENG_PROC_ANY_ATTACH (ENG_PROC_ATTACHED | ENG_PROC_EXAMINED)
  55. #define ENG_PROC_ANY_EXAMINE (ENG_PROC_EXAMINED | ENG_PROC_ATTACH_EXISTING)
  56. // Handle must be closed when deleted.
  57. // This flag applies to both processes and threads.
  58. #define ENG_PROC_THREAD_CLOSE_HANDLE 0x80000000
  59. // The debugger set the trace flag when deferring
  60. // breakpoint work on the last event for this thread.
  61. #define ENG_THREAD_DEFER_BP_TRACE 0x00000001
  62. // Processes which were created or attached but that
  63. // have not yet generated events yet.
  64. typedef struct _PENDING_PROCESS
  65. {
  66. ULONG64 Handle;
  67. // Initial thread information is only valid for creations.
  68. ULONG64 InitialThreadHandle;
  69. ULONG Id;
  70. ULONG InitialThreadId;
  71. ULONG Flags;
  72. ULONG Options;
  73. struct _PENDING_PROCESS* Next;
  74. } PENDING_PROCESS;
  75. struct IMPLICIT_THREAD_SAVE
  76. {
  77. ULONG64 Value;
  78. BOOL Default;
  79. ThreadInfo* Thread;
  80. };
  81. struct OUT_OF_PROC_FUNC_TABLE_DLL
  82. {
  83. OUT_OF_PROC_FUNC_TABLE_DLL* Next;
  84. ULONG64 Handle;
  85. // Name data follows.
  86. };
  87. //----------------------------------------------------------------------------
  88. //
  89. // ProcCorDataAccessServices.
  90. //
  91. //----------------------------------------------------------------------------
  92. class ProcCorDataAccessServices : public ICorDataAccessServices
  93. {
  94. public:
  95. ProcCorDataAccessServices(void);
  96. // IUnknown.
  97. STDMETHOD(QueryInterface)(
  98. THIS_
  99. IN REFIID InterfaceId,
  100. OUT PVOID* Interface
  101. );
  102. STDMETHOD_(ULONG, AddRef)(
  103. THIS
  104. );
  105. STDMETHOD_(ULONG, Release)(
  106. THIS
  107. );
  108. // ICorDataAccessServices.
  109. virtual HRESULT STDMETHODCALLTYPE GetMachineType(
  110. /* [out] */ ULONG32 *machine);
  111. virtual HRESULT STDMETHODCALLTYPE GetPointerSize(
  112. /* [out] */ ULONG32 *size);
  113. virtual HRESULT STDMETHODCALLTYPE GetImageBase(
  114. /* [string][in] */ LPCWSTR name,
  115. /* [out] */ CORDATA_ADDRESS *base);
  116. virtual HRESULT STDMETHODCALLTYPE ReadVirtual(
  117. /* [in] */ CORDATA_ADDRESS address,
  118. /* [length_is][size_is][out] */ PBYTE buffer,
  119. /* [in] */ ULONG32 request,
  120. /* [optional][out] */ ULONG32 *done);
  121. virtual HRESULT STDMETHODCALLTYPE WriteVirtual(
  122. /* [in] */ CORDATA_ADDRESS address,
  123. /* [size_is][in] */ PBYTE buffer,
  124. /* [in] */ ULONG32 request,
  125. /* [optional][out] */ ULONG32 *done);
  126. virtual HRESULT STDMETHODCALLTYPE GetTlsValue(
  127. /* [in] */ ULONG32 index,
  128. /* [out] */ CORDATA_ADDRESS* value);
  129. virtual HRESULT STDMETHODCALLTYPE SetTlsValue(
  130. /* [in] */ ULONG32 index,
  131. /* [in] */ CORDATA_ADDRESS value);
  132. virtual HRESULT STDMETHODCALLTYPE GetCurrentThreadId(
  133. /* [out] */ ULONG32* threadId);
  134. virtual HRESULT STDMETHODCALLTYPE GetThreadContext(
  135. /* [in] */ ULONG32 threadId,
  136. /* [in] */ ULONG32 contextFlags,
  137. /* [in] */ ULONG32 contextSize,
  138. /* [out, size_is(contextSize)] */ PBYTE context);
  139. virtual HRESULT STDMETHODCALLTYPE SetThreadContext(
  140. /* [in] */ ULONG32 threadId,
  141. /* [in] */ ULONG32 contextSize,
  142. /* [in, size_is(contextSize)] */ PBYTE context);
  143. ProcessInfo* m_Process;
  144. };
  145. //----------------------------------------------------------------------------
  146. //
  147. // ProcessInfo.
  148. //
  149. //----------------------------------------------------------------------------
  150. class ProcessInfo
  151. {
  152. public:
  153. ProcessInfo(TargetInfo* Target,
  154. ULONG SystemId,
  155. HANDLE SymHandle,
  156. ULONG64 SysHandle,
  157. ULONG Flags,
  158. ULONG Options);
  159. ~ProcessInfo(void);
  160. TargetInfo* m_Target;
  161. ProcessInfo* m_Next;
  162. ULONG m_NumImages;
  163. ULONG m_NumUnloadedModules;
  164. ImageInfo* m_ImageHead;
  165. ImageInfo* m_ExecutableImage;
  166. ImageInfo* m_SynthesizedImage;
  167. ULONG m_NumThreads;
  168. ThreadInfo* m_ThreadHead;
  169. ThreadInfo* m_CurrentThread;
  170. ULONG m_UserId;
  171. ULONG m_SystemId;
  172. ULONG m_Exited:1;
  173. ULONG m_ModulesLoaded:1;
  174. ULONG m_InitialBreakDone:1;
  175. ULONG m_InitialBreak:1;
  176. ULONG m_InitialBreakWx86:1;
  177. ULONG64 m_DataOffset;
  178. // For kernel mode and dumps the process handle is
  179. // a virtual handle for the kernel/dump process.
  180. // dbghelp still uses HANDLE as the process handle
  181. // type even though we may want to pass in 64-bit
  182. // process handles when remote debugging. Keep
  183. // a cut-down version of the handle for normal
  184. // use but also keep the full handle around in
  185. // case it's needed.
  186. // There is also another purpose for the two handles:
  187. // as dbghelp has no notion of systems we need to
  188. // keep all handles from all systems unique. The
  189. // dbghelp handle may be further modified in order
  190. // to preserve uniqueness.
  191. HANDLE m_SymHandle;
  192. ULONG64 m_SysHandle;
  193. ULONG m_Flags;
  194. ULONG m_Options;
  195. ULONG m_NumBreakpoints;
  196. class Breakpoint* m_Breakpoints;
  197. class Breakpoint* m_BreakpointsTail;
  198. ULONG64 m_DynFuncTableList;
  199. OUT_OF_PROC_FUNC_TABLE_DLL* m_OopFuncTableDlls;
  200. ULONG64 m_RtlUnloadList;
  201. ULONG64 m_ImplicitThreadData;
  202. BOOL m_ImplicitThreadDataIsDefault;
  203. ThreadInfo* m_ImplicitThreadDataThread;
  204. ImageInfo* m_CorImage;
  205. COR_DLL m_CorImageType;
  206. HMODULE m_CorDebugDll;
  207. ICorDataAccess* m_CorAccess;
  208. ProcCorDataAccessServices m_CorServices;
  209. VirtualMemoryCache m_VirtualCache;
  210. ThreadInfo* FindThreadByUserId(ULONG Id);
  211. ThreadInfo* FindThreadBySystemId(ULONG Id);
  212. ThreadInfo* FindThreadByHandle(ULONG64 Handle);
  213. void InsertThread(ThreadInfo* Thread);
  214. void RemoveThread(ThreadInfo* Thread);
  215. HRESULT CreateVirtualThreads(ULONG StartId, ULONG Threads);
  216. ImageInfo* FindImageByIndex(ULONG Index);
  217. ImageInfo* FindImageByOffset(ULONG64 Offset, BOOL AllowSynth);
  218. ImageInfo* FindImageByName(PCSTR Name, ULONG NameChars,
  219. INAME Which, BOOL AllowSynth);
  220. BOOL GetOffsetFromMod(PCSTR String, PULONG64 Offset);
  221. void InsertImage(ImageInfo* Image);
  222. void RemoveImage(ImageInfo* Image);
  223. BOOL DeleteImageByName(PCSTR Name, INAME Which);
  224. BOOL DeleteImageByBase(ULONG64 Base);
  225. void DeleteImagesBelowOffset(ULONG64 Offset);
  226. void DeleteImages(void);
  227. HRESULT AddImage(PMODULE_INFO_ENTRY ModEntry,
  228. BOOL ForceSymbolLoad,
  229. ImageInfo** ImageAdded);
  230. void SynthesizeSymbols(void);
  231. void VerifyKernel32Version(void);
  232. BOOL DeleteExitedInfos(void);
  233. void PrepareForExecution(void);
  234. void OutputThreadInfo(ThreadInfo* Match, BOOL Verbose);
  235. HRESULT AddOopFuncTableDll(PWSTR Dll, ULONG64 Handle);
  236. void ClearOopFuncTableDlls(void);
  237. OUT_OF_PROC_FUNC_TABLE_DLL* FindOopFuncTableDll(PWSTR Dll);
  238. HRESULT LoadCorDebugDll(void);
  239. HRESULT IsCorCode(ULONG64 Native);
  240. HRESULT ConvertNativeToIlOffset(ULONG64 Native,
  241. PULONG64 ImageBase,
  242. PULONG32 MethodToken,
  243. PULONG32 MethodOffs);
  244. HRESULT GetCorSymbol(ULONG64 Native, PSTR Buffer, ULONG BufferChars,
  245. PULONG64 Displacement);
  246. ICorDataStackWalk* StartCorStack(ULONG32 CorThreadId);
  247. void FlushCorState(void)
  248. {
  249. if (m_CorDebugDll)
  250. {
  251. m_CorAccess->Flush();
  252. }
  253. }
  254. HRESULT Terminate(void);
  255. HRESULT Detach(void);
  256. HRESULT Abandon(void);
  257. void ResetImplicitData(void)
  258. {
  259. m_ImplicitThreadData = 0;
  260. m_ImplicitThreadDataIsDefault = TRUE;
  261. m_ImplicitThreadDataThread = NULL;
  262. }
  263. void SaveImplicitThread(IMPLICIT_THREAD_SAVE* Save)
  264. {
  265. Save->Value = m_ImplicitThreadData;
  266. Save->Default = m_ImplicitThreadDataIsDefault;
  267. Save->Thread = m_ImplicitThreadDataThread;
  268. }
  269. void RestoreImplicitThread(IMPLICIT_THREAD_SAVE* Save)
  270. {
  271. m_ImplicitThreadData = Save->Value;
  272. m_ImplicitThreadDataIsDefault = Save->Default;
  273. m_ImplicitThreadDataThread = Save->Thread;
  274. }
  275. HRESULT GetImplicitThreadData(ThreadInfo* Thread, PULONG64 Offset);
  276. HRESULT GetImplicitThreadDataTeb(ThreadInfo* Thread, PULONG64 Offset);
  277. HRESULT SetImplicitThreadData(ThreadInfo* Thread,
  278. ULONG64 Offset, BOOL Verbose);
  279. HRESULT ReadImplicitThreadInfoPointer(ThreadInfo* Thread,
  280. ULONG Offset, PULONG64 Ptr);
  281. void MarkExited(void)
  282. {
  283. m_Exited = TRUE;
  284. g_EngDefer |= ENG_DEFER_DELETE_EXITED;
  285. }
  286. PSTR GetExecutableImageName(void)
  287. {
  288. return m_ExecutableImage ?
  289. m_ExecutableImage->m_ImagePath :
  290. (m_ImageHead != NULL ?
  291. m_ImageHead->m_ImagePath : "?NoImage?");
  292. }
  293. };
  294. ProcessInfo* FindAnyProcessByUserId(ULONG Id);
  295. void ParseProcessCmds(void);
  296. HRESULT TerminateProcesses(void);
  297. HRESULT DetachProcesses(void);
  298. enum
  299. {
  300. SEP_TERMINATE,
  301. SEP_DETACH,
  302. SEP_ABANDON,
  303. };
  304. HRESULT SeparateCurrentProcess(ULONG Mode, PSTR Description);
  305. #endif // #ifndef __PROCESS_HPP__