Source code of Windows XP (NT5)
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.

408 lines
7.5 KiB

  1. /*++
  2. Copyright (c) 1999-2002 Microsoft Corporation
  3. Module Name:
  4. gen.c
  5. Abstract:
  6. Generic routines that work on all operating systems.
  7. Author:
  8. Matthew D Hendel (math) 20-Oct-1999
  9. Revision History:
  10. --*/
  11. #pragma once
  12. #define KBYTE (1024)
  13. #define SIGN_EXTEND(_v) ((ULONG64) (LONG64) (_v))
  14. #define ARRAY_COUNT(_array) (sizeof (_array) / sizeof ((_array)[0]))
  15. // Ran out of memory during dump writing.
  16. #define MDSTATUS_OUT_OF_MEMORY 0x00000001
  17. // ReadProcessMemory failed during dump writing.
  18. #define MDSTATUS_UNABLE_TO_READ_MEMORY 0x00000002
  19. // OS routine failed during dump writing.
  20. #define MDSTATUS_CALL_FAILED 0x00000004
  21. // Unexpected internal failure during dump writing.
  22. #define MDSTATUS_INTERNAL_ERROR 0x00000008
  23. ULONG FORCEINLINE
  24. FileTimeToTimeDate(LPFILETIME FileTime)
  25. {
  26. ULARGE_INTEGER LargeTime;
  27. LargeTime.LowPart = FileTime->dwLowDateTime;
  28. LargeTime.HighPart = FileTime->dwHighDateTime;
  29. // Convert to seconds and from base year 1601 to base year 1970.
  30. return (ULONG)(LargeTime.QuadPart / 10000000 - 11644473600);
  31. }
  32. ULONG FORCEINLINE
  33. FileTimeToSeconds(LPFILETIME FileTime)
  34. {
  35. ULARGE_INTEGER LargeTime;
  36. LargeTime.LowPart = FileTime->dwLowDateTime;
  37. LargeTime.HighPart = FileTime->dwHighDateTime;
  38. // Convert to seconds.
  39. return (ULONG)(LargeTime.QuadPart / 10000000);
  40. }
  41. struct _INTERNAL_THREAD;
  42. struct _INTERNAL_PROCESS;
  43. struct _INTERNAL_MODULE;
  44. struct _INTERNAL_FUNCTION_TABLE;
  45. ULONG
  46. GenGetAccumulatedStatus(
  47. void
  48. );
  49. void
  50. GenAccumulateStatus(
  51. IN ULONG Status
  52. );
  53. void
  54. GenClearAccumulatedStatus(
  55. void
  56. );
  57. BOOL
  58. GenExecuteIncludeThreadCallback(
  59. IN HANDLE hProcess,
  60. IN DWORD ProcessId,
  61. IN ULONG DumpType,
  62. IN ULONG ThreadId,
  63. IN MINIDUMP_CALLBACK_ROUTINE CallbackRoutine,
  64. IN PVOID CallbackParam,
  65. OUT PULONG WriteFlags
  66. );
  67. BOOL
  68. GenExecuteIncludeModuleCallback(
  69. IN HANDLE hProcess,
  70. IN DWORD ProcessId,
  71. IN ULONG DumpType,
  72. IN ULONG64 BaseOfImage,
  73. IN MINIDUMP_CALLBACK_ROUTINE CallbackRoutine,
  74. IN PVOID CallbackParam,
  75. OUT PULONG WriteFlags
  76. );
  77. BOOL
  78. GenGetDataContributors(
  79. IN OUT PINTERNAL_PROCESS Process,
  80. IN PINTERNAL_MODULE Module
  81. );
  82. HANDLE
  83. GenOpenThread(
  84. DWORD dwDesiredAccess,
  85. BOOL bInheritHandle,
  86. DWORD dwThreadId
  87. );
  88. HRESULT
  89. GenAllocateThreadObject(
  90. IN struct _INTERNAL_PROCESS* Process,
  91. IN HANDLE ProcessHandle,
  92. IN ULONG ThreadId,
  93. IN ULONG DumpType,
  94. IN ULONG WriteFlags,
  95. OUT struct _INTERNAL_THREAD** Thread
  96. );
  97. BOOL
  98. GenGetThreadInstructionWindow(
  99. IN struct _INTERNAL_PROCESS* Process,
  100. IN struct _INTERNAL_THREAD* Thread
  101. );
  102. struct _INTERNAL_PROCESS*
  103. GenAllocateProcessObject(
  104. IN HANDLE hProcess,
  105. IN ULONG ProcessId
  106. );
  107. BOOL
  108. GenFreeProcessObject(
  109. IN struct _INTERNAL_PROCESS * Process
  110. );
  111. struct _INTERNAL_MODULE*
  112. GenAllocateModuleObject(
  113. IN PINTERNAL_PROCESS Process,
  114. IN PWSTR FullPath,
  115. IN ULONG_PTR BaseOfModule,
  116. IN ULONG DumpType,
  117. IN ULONG WriteFlags
  118. );
  119. VOID
  120. GenFreeModuleObject(
  121. struct _INTERNAL_MODULE * Module
  122. );
  123. struct _INTERNAL_UNLOADED_MODULE*
  124. GenAllocateUnloadedModuleObject(
  125. IN PWSTR Path,
  126. IN ULONG_PTR BaseOfModule,
  127. IN ULONG SizeOfModule,
  128. IN ULONG CheckSum,
  129. IN ULONG TimeDateStamp
  130. );
  131. VOID
  132. GenFreeUnloadedModuleObject(
  133. struct _INTERNAL_UNLOADED_MODULE * Module
  134. );
  135. struct _INTERNAL_FUNCTION_TABLE*
  136. GenAllocateFunctionTableObject(
  137. IN ULONG64 MinAddress,
  138. IN ULONG64 MaxAddress,
  139. IN ULONG64 BaseAddress,
  140. IN ULONG EntryCount,
  141. IN PDYNAMIC_FUNCTION_TABLE RawTable
  142. );
  143. VOID
  144. GenFreeFunctionTableObject(
  145. IN struct _INTERNAL_FUNCTION_TABLE* Table
  146. );
  147. BOOL
  148. GenIncludeUnwindInfoMemory(
  149. IN PINTERNAL_PROCESS Process,
  150. IN ULONG DumpType,
  151. IN struct _INTERNAL_FUNCTION_TABLE* Table
  152. );
  153. LPVOID
  154. GenGetTebAddress(
  155. IN HANDLE Thread,
  156. OUT PULONG SizeOfTeb
  157. );
  158. LPVOID
  159. GenGetPebAddress(
  160. IN HANDLE Process,
  161. OUT PULONG SizeOfPeb
  162. );
  163. HRESULT
  164. GenGetThreadInfo(
  165. IN HANDLE Process,
  166. IN HANDLE Thread,
  167. OUT PULONG64 Teb,
  168. OUT PULONG SizeOfTeb,
  169. OUT PULONG64 StackBase,
  170. OUT PULONG64 StackLimit,
  171. OUT PULONG64 StoreBase,
  172. OUT PULONG64 StoreLimit
  173. );
  174. PVA_RANGE
  175. GenAddMemoryBlock(
  176. IN PINTERNAL_PROCESS Process,
  177. IN MEMBLOCK_TYPE Type,
  178. IN ULONG64 Start,
  179. IN ULONG Size
  180. );
  181. void
  182. GenRemoveMemoryRange(
  183. IN PINTERNAL_PROCESS Process,
  184. IN ULONG64 Start,
  185. IN ULONG Size
  186. );
  187. LPVOID
  188. AllocMemory(
  189. SIZE_T Size
  190. );
  191. VOID
  192. FreeMemory(
  193. IN LPVOID Memory
  194. );
  195. enum {
  196. Unknown,
  197. Win9x,
  198. WinNt,
  199. WinCe
  200. };
  201. VOID
  202. WINAPI
  203. GenGetSystemType(
  204. OUT ULONG * Type, OPTIONAL
  205. OUT ULONG * Major, OPTIONAL
  206. OUT ULONG * Minor, OPTIONAL
  207. OUT ULONG * ServicePack, OPTIONAL
  208. OUT ULONG * BuildNumber OPTIONAL
  209. );
  210. BOOL
  211. GenGetProcessInfo(
  212. IN HANDLE hProcess,
  213. IN ULONG ProcessId,
  214. IN ULONG DumpType,
  215. IN MINIDUMP_CALLBACK_ROUTINE CallbackRoutine,
  216. IN PVOID CallbackParam,
  217. OUT struct _INTERNAL_PROCESS ** ProcessRet
  218. );
  219. BOOL
  220. GenWriteHandleData(
  221. IN HANDLE ProcessHandle,
  222. IN HANDLE hFile,
  223. IN struct _MINIDUMP_STREAM_INFO * StreamInfo
  224. );
  225. //
  226. // Stolen from NTRTL to avoid having to include it here.
  227. //
  228. #ifndef InitializeListHead
  229. #define InitializeListHead(ListHead) (\
  230. (ListHead)->Flink = (ListHead)->Blink = (ListHead))
  231. #endif
  232. //
  233. // VOID
  234. // InsertTailList(
  235. // PLIST_ENTRY ListHead,
  236. // PLIST_ENTRY Entry
  237. // );
  238. //
  239. #ifndef InsertTailList
  240. #define InsertTailList(ListHead,Entry) {\
  241. PLIST_ENTRY _EX_Blink;\
  242. PLIST_ENTRY _EX_ListHead;\
  243. _EX_ListHead = (ListHead);\
  244. _EX_Blink = _EX_ListHead->Blink;\
  245. (Entry)->Flink = _EX_ListHead;\
  246. (Entry)->Blink = _EX_Blink;\
  247. _EX_Blink->Flink = (Entry);\
  248. _EX_ListHead->Blink = (Entry);\
  249. }
  250. #endif
  251. //
  252. // VOID
  253. // InsertListAfter(
  254. // PLIST_ENTRY ListEntry,
  255. // PLIST_ENTRY InsertEntry
  256. // );
  257. //
  258. #ifndef InsertListAfter
  259. #define InsertListAfter(ListEntry,InsertEntry) {\
  260. (InsertEntry)->Flink = (ListEntry)->Flink;\
  261. (InsertEntry)->Blink = (ListEntry);\
  262. (ListEntry)->Flink->Blink = (InsertEntry);\
  263. (ListEntry)->Flink = (InsertEntry);\
  264. }
  265. #endif
  266. //
  267. // VOID
  268. // RemoveEntryList(
  269. // PLIST_ENTRY Entry
  270. // );
  271. //
  272. #ifndef RemoveEntryList
  273. #define RemoveEntryList(Entry) {\
  274. PLIST_ENTRY _EX_Blink;\
  275. PLIST_ENTRY _EX_Flink;\
  276. _EX_Flink = (Entry)->Flink;\
  277. _EX_Blink = (Entry)->Blink;\
  278. _EX_Blink->Flink = _EX_Flink;\
  279. _EX_Flink->Blink = _EX_Blink;\
  280. }
  281. #endif
  282. //
  283. // BOOLEAN
  284. // IsListEmpty(
  285. // PLIST_ENTRY ListHead
  286. // );
  287. //
  288. #ifndef IsListEmpty
  289. #define IsListEmpty(ListHead) \
  290. ((ListHead)->Flink == (ListHead))
  291. #endif
  292. struct _THREADENTRY32;
  293. BOOL
  294. ProcessThread32Next(
  295. HANDLE hSnapshot,
  296. DWORD dwProcessID,
  297. struct tagTHREADENTRY32 * ThreadInfo
  298. );
  299. BOOL
  300. ProcessThread32First(
  301. HANDLE hSnapshot,
  302. DWORD dwProcessID,
  303. struct tagTHREADENTRY32 * ThreadInfo
  304. );
  305. ULONG
  306. CheckSum (
  307. IN ULONG PartialSum,
  308. IN PVOID SourceVa,
  309. IN ULONG_PTR Length
  310. );
  311. BOOL
  312. ThGetProcessInfo(
  313. IN HANDLE hProcess,
  314. IN ULONG ProcessId,
  315. IN ULONG DumpType,
  316. IN MINIDUMP_CALLBACK_ROUTINE CallbackRoutine,
  317. IN PVOID CallbackParam,
  318. OUT struct _INTERNAL_PROCESS ** ProcessRet
  319. );
  320. //
  321. // Undefine ASSERT so we do not get RtlAssert().
  322. //
  323. #ifdef ASSERT
  324. #undef ASSERT
  325. #endif
  326. #if DBG
  327. #define ASSERT(_x)\
  328. if (!(_x)){\
  329. OutputDebugString ("ASSERT Failed");\
  330. DebugBreak ();\
  331. }
  332. #else
  333. #define ASSERT(_x)
  334. #endif