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.

323 lines
6.1 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 ARRAY_COUNT(_array) (sizeof (_array) / sizeof ((_array)[0]))
  14. #define MAX_DYNAMIC_FUNCTION_TABLE 256
  15. #define WIN32_LAST_STATUS() \
  16. (GetLastError() ? HRESULT_FROM_WIN32(GetLastError()) : E_FAIL)
  17. ULONG FORCEINLINE
  18. FileTimeToTimeDate(LPFILETIME FileTime)
  19. {
  20. ULARGE_INTEGER LargeTime;
  21. LargeTime.LowPart = FileTime->dwLowDateTime;
  22. LargeTime.HighPart = FileTime->dwHighDateTime;
  23. // Convert to seconds and from base year 1601 to base year 1970.
  24. return (ULONG)(LargeTime.QuadPart / 10000000 - 11644473600);
  25. }
  26. ULONG FORCEINLINE
  27. FileTimeToSeconds(LPFILETIME FileTime)
  28. {
  29. ULARGE_INTEGER LargeTime;
  30. LargeTime.LowPart = FileTime->dwLowDateTime;
  31. LargeTime.HighPart = FileTime->dwHighDateTime;
  32. // Convert to seconds.
  33. return (ULONG)(LargeTime.QuadPart / 10000000);
  34. }
  35. ULONG64 FORCEINLINE
  36. GenGetPointer(IN PMINIDUMP_STATE Dump,
  37. IN PVOID Data)
  38. {
  39. if (Dump->PtrSize == 8) {
  40. return *(PULONG64)Data;
  41. } else {
  42. return *(PLONG)Data;
  43. }
  44. }
  45. void FORCEINLINE
  46. GenSetPointer(IN PMINIDUMP_STATE Dump,
  47. IN PVOID Data,
  48. IN ULONG64 Val)
  49. {
  50. if (Dump->PtrSize == 8) {
  51. *(PULONG64)Data = Val;
  52. } else {
  53. *(PULONG)Data = (ULONG)Val;
  54. }
  55. }
  56. LPVOID
  57. AllocMemory(
  58. IN PMINIDUMP_STATE Dump,
  59. IN ULONG Size
  60. );
  61. VOID
  62. FreeMemory(
  63. IN PMINIDUMP_STATE Dump,
  64. IN LPVOID Memory
  65. );
  66. void
  67. GenAccumulateStatus(
  68. IN PMINIDUMP_STATE Dump,
  69. IN ULONG Status
  70. );
  71. struct _INTERNAL_THREAD;
  72. struct _INTERNAL_PROCESS;
  73. struct _INTERNAL_MODULE;
  74. struct _INTERNAL_FUNCTION_TABLE;
  75. BOOL
  76. GenExecuteIncludeThreadCallback(
  77. IN PMINIDUMP_STATE Dump,
  78. IN ULONG ThreadId,
  79. OUT PULONG WriteFlags
  80. );
  81. BOOL
  82. GenExecuteIncludeModuleCallback(
  83. IN PMINIDUMP_STATE Dump,
  84. IN ULONG64 BaseOfImage,
  85. OUT PULONG WriteFlags
  86. );
  87. HRESULT
  88. GenGetDataContributors(
  89. IN PMINIDUMP_STATE Dump,
  90. IN OUT PINTERNAL_PROCESS Process,
  91. IN PINTERNAL_MODULE Module
  92. );
  93. HRESULT
  94. GenGetThreadInstructionWindow(
  95. IN PMINIDUMP_STATE Dump,
  96. IN struct _INTERNAL_PROCESS* Process,
  97. IN struct _INTERNAL_THREAD* Thread
  98. );
  99. HRESULT
  100. GenGetProcessInfo(
  101. IN PMINIDUMP_STATE Dump,
  102. OUT struct _INTERNAL_PROCESS ** ProcessRet
  103. );
  104. VOID
  105. GenFreeProcessObject(
  106. IN PMINIDUMP_STATE Dump,
  107. IN struct _INTERNAL_PROCESS * Process
  108. );
  109. HRESULT
  110. GenAddMemoryBlock(
  111. IN PMINIDUMP_STATE Dump,
  112. IN PINTERNAL_PROCESS Process,
  113. IN MEMBLOCK_TYPE Type,
  114. IN ULONG64 Start,
  115. IN ULONG Size
  116. );
  117. void
  118. GenRemoveMemoryRange(
  119. IN PMINIDUMP_STATE Dump,
  120. IN PINTERNAL_PROCESS Process,
  121. IN ULONG64 Start,
  122. IN ULONG Size
  123. );
  124. HRESULT
  125. GenAddPebMemory(
  126. IN PMINIDUMP_STATE Dump,
  127. IN PINTERNAL_PROCESS Process
  128. );
  129. HRESULT
  130. GenAddTebMemory(
  131. IN PMINIDUMP_STATE Dump,
  132. IN PINTERNAL_PROCESS Process,
  133. IN PINTERNAL_THREAD Thread
  134. );
  135. HRESULT
  136. GenWriteHandleData(
  137. IN PMINIDUMP_STATE Dump,
  138. IN PMINIDUMP_STREAM_INFO StreamInfo
  139. );
  140. ULONG GenProcArchToImageMachine(ULONG ProcArch);
  141. //
  142. // Routines reimplemented for portability.
  143. //
  144. PIMAGE_NT_HEADERS
  145. GenImageNtHeader(
  146. IN PVOID Base,
  147. OUT OPTIONAL PIMAGE_NT_HEADERS64 Generic
  148. );
  149. PVOID
  150. GenImageDirectoryEntryToData(
  151. IN PVOID Base,
  152. IN BOOLEAN MappedAsImage,
  153. IN USHORT DirectoryEntry,
  154. OUT PULONG Size
  155. );
  156. LPWSTR
  157. GenStrCopyNW(
  158. OUT LPWSTR lpString1,
  159. IN LPCWSTR lpString2,
  160. IN int iMaxLength
  161. );
  162. size_t
  163. GenStrLengthW(
  164. const wchar_t * wcs
  165. );
  166. int
  167. GenStrCompareW(
  168. IN LPCWSTR String1,
  169. IN LPCWSTR String2
  170. );
  171. void
  172. GenExRecord32ToMd(PEXCEPTION_RECORD32 Rec32,
  173. PMINIDUMP_EXCEPTION RecMd);
  174. inline void
  175. GenExRecord64ToMd(PEXCEPTION_RECORD64 Rec64,
  176. PMINIDUMP_EXCEPTION RecMd)
  177. {
  178. // Structures are the same.
  179. memcpy(RecMd, Rec64, sizeof(*RecMd));
  180. }
  181. //
  182. // Stolen from NTRTL to avoid having to include it here.
  183. //
  184. #ifndef InitializeListHead
  185. #define InitializeListHead(ListHead) (\
  186. (ListHead)->Flink = (ListHead)->Blink = (ListHead))
  187. #endif
  188. //
  189. // VOID
  190. // InsertTailList(
  191. // PLIST_ENTRY ListHead,
  192. // PLIST_ENTRY Entry
  193. // );
  194. //
  195. #ifndef InsertTailList
  196. #define InsertTailList(ListHead,Entry) {\
  197. PLIST_ENTRY _EX_Blink;\
  198. PLIST_ENTRY _EX_ListHead;\
  199. _EX_ListHead = (ListHead);\
  200. _EX_Blink = _EX_ListHead->Blink;\
  201. (Entry)->Flink = _EX_ListHead;\
  202. (Entry)->Blink = _EX_Blink;\
  203. _EX_Blink->Flink = (Entry);\
  204. _EX_ListHead->Blink = (Entry);\
  205. }
  206. #endif
  207. //
  208. // VOID
  209. // InsertListAfter(
  210. // PLIST_ENTRY ListEntry,
  211. // PLIST_ENTRY InsertEntry
  212. // );
  213. //
  214. #ifndef InsertListAfter
  215. #define InsertListAfter(ListEntry,InsertEntry) {\
  216. (InsertEntry)->Flink = (ListEntry)->Flink;\
  217. (InsertEntry)->Blink = (ListEntry);\
  218. (ListEntry)->Flink->Blink = (InsertEntry);\
  219. (ListEntry)->Flink = (InsertEntry);\
  220. }
  221. #endif
  222. //
  223. // VOID
  224. // RemoveEntryList(
  225. // PLIST_ENTRY Entry
  226. // );
  227. //
  228. #ifndef RemoveEntryList
  229. #define RemoveEntryList(Entry) {\
  230. PLIST_ENTRY _EX_Blink;\
  231. PLIST_ENTRY _EX_Flink;\
  232. _EX_Flink = (Entry)->Flink;\
  233. _EX_Blink = (Entry)->Blink;\
  234. _EX_Blink->Flink = _EX_Flink;\
  235. _EX_Flink->Blink = _EX_Blink;\
  236. }
  237. #endif
  238. //
  239. // BOOLEAN
  240. // IsListEmpty(
  241. // PLIST_ENTRY ListHead
  242. // );
  243. //
  244. #ifndef IsListEmpty
  245. #define IsListEmpty(ListHead) \
  246. ((ListHead)->Flink == (ListHead))
  247. #endif
  248. //
  249. // Undefine ASSERT so we do not get RtlAssert().
  250. //
  251. #ifdef ASSERT
  252. #undef ASSERT
  253. #endif
  254. #if DBG
  255. #define ASSERT(_x)\
  256. if (!(_x)){\
  257. OutputDebugString ("ASSERT Failed");\
  258. DebugBreak ();\
  259. }
  260. #else
  261. #define ASSERT(_x)
  262. #endif