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.

373 lines
7.6 KiB

  1. /*++
  2. Copyright (c) 1996-2000 Microsoft Corporation
  3. Module Name:
  4. miscellaneous.c
  5. Abstract:
  6. Quick and not-so-dirty user-mode dh for heap.
  7. Author(s):
  8. Silviu Calinoiu (SilviuC) 06-Feb-00
  9. Revision History:
  10. SilviuC 06-Feb-00 Initial version
  11. --*/
  12. #include <stdlib.h>
  13. #include <stdio.h>
  14. #include <stdarg.h>
  15. #include <windows.h>
  16. #include "miscellaneous.h"
  17. GLOBALS Globals;
  18. PVOID
  19. Xalloc (
  20. PCHAR File,
  21. ULONG Line,
  22. SIZE_T Size
  23. )
  24. {
  25. PSIZE_T Result;
  26. Result = (PSIZE_T) malloc (Size + sizeof (SIZE_T));
  27. if (Result == NULL) {
  28. //
  29. // We will never return from this call.
  30. //
  31. Comment( "malloc(%p) failed %s:%d",Size-sizeof(SIZE_T),File,Line);
  32. return NULL;
  33. }
  34. Globals.CurrentHeapUsage += Size;
  35. if (Globals.CurrentHeapUsage > Globals.MaximumHeapUsage) {
  36. Globals.MaximumHeapUsage = Globals.CurrentHeapUsage;
  37. }
  38. ZeroMemory (Result, Size + sizeof(SIZE_T));
  39. *Result = Size;
  40. return (PVOID)(Result + 1);
  41. }
  42. VOID
  43. Xfree (
  44. PVOID Object
  45. )
  46. {
  47. PSIZE_T Block;
  48. if (Object) {
  49. Block = (PSIZE_T)Object;
  50. Block -= 1;
  51. Globals.CurrentHeapUsage -= *Block;
  52. free (Block);
  53. }
  54. }
  55. PVOID
  56. Xrealloc (
  57. PCHAR File,
  58. ULONG Line,
  59. PVOID Object,
  60. SIZE_T Size
  61. )
  62. {
  63. PVOID Block;
  64. SIZE_T OldSize;
  65. Block = Xalloc (File, Line, Size);
  66. if (Block == NULL) {
  67. return NULL;
  68. }
  69. OldSize = *((PSIZE_T)Object - 1);
  70. CopyMemory (Block, Object, (Size > OldSize) ? OldSize : Size);
  71. Xfree (Object);
  72. return Block;;
  73. }
  74. VOID
  75. ReportStatistics (
  76. )
  77. {
  78. Comment ("UMDH version: %s", Globals.Version);
  79. Comment ("Peak heap usage: %p bytes", Globals.MaximumHeapUsage);
  80. }
  81. VOID
  82. Info (
  83. PCHAR Format,
  84. ...
  85. )
  86. {
  87. va_list Params;
  88. va_start (Params, Format);
  89. vfprintf (Globals.OutFile, Format, Params);
  90. fprintf (Globals.OutFile, "\n");
  91. fflush( Globals.OutFile );
  92. }
  93. VOID
  94. Comment (
  95. PCHAR Format,
  96. ...
  97. )
  98. {
  99. va_list Params;
  100. va_start (Params, Format);
  101. fprintf (Globals.OutFile, "// ");
  102. vfprintf (Globals.OutFile, Format, Params);
  103. fprintf (Globals.OutFile, "\n");
  104. fflush( Globals.OutFile );
  105. }
  106. VOID
  107. Warning (
  108. PCHAR File,
  109. ULONG Line,
  110. PCHAR Format,
  111. ...
  112. )
  113. {
  114. va_list Params;
  115. va_start (Params, Format);
  116. if (File) {
  117. fprintf (Globals.ErrorFile, "Warning: %s: %u: ", File, Line);
  118. }
  119. else {
  120. fprintf (Globals.ErrorFile, "Warning: ");
  121. }
  122. vfprintf (Globals.ErrorFile, Format, Params);
  123. fprintf (Globals.ErrorFile, "\n");
  124. fflush( Globals.ErrorFile );
  125. }
  126. VOID
  127. Error (
  128. PCHAR File,
  129. ULONG Line,
  130. PCHAR Format,
  131. ...
  132. )
  133. {
  134. va_list Params;
  135. va_start (Params, Format);
  136. if (File) {
  137. fprintf (Globals.ErrorFile, "Error: %s: %u: ", File, Line);
  138. }
  139. else {
  140. fprintf (Globals.ErrorFile, "Error: ");
  141. }
  142. vfprintf (Globals.ErrorFile, Format, Params);
  143. fprintf (Globals.ErrorFile, "\n");
  144. fflush( Globals.ErrorFile );
  145. }
  146. VOID
  147. Debug (
  148. PCHAR File,
  149. ULONG Line,
  150. PCHAR Format,
  151. ...
  152. )
  153. {
  154. va_list Params;
  155. va_start (Params, Format);
  156. if (Globals.Verbose) {
  157. if (File) {
  158. fprintf (Globals.ErrorFile, "Debug: %s: %u: ", File, Line);
  159. }
  160. else {
  161. fprintf (Globals.ErrorFile, "Debug: ");
  162. }
  163. vfprintf (Globals.ErrorFile, Format, Params);
  164. fprintf (Globals.ErrorFile, "\n");
  165. fflush( Globals.ErrorFile );
  166. }
  167. }
  168. BOOL
  169. UmdhReadAtVa(
  170. IN PCHAR File,
  171. IN ULONG Line,
  172. IN HANDLE Process,
  173. IN PVOID Address,
  174. IN PVOID Data,
  175. IN SIZE_T Size
  176. )
  177. /*++
  178. Routine Description:
  179. UmdhReadAtVa
  180. Arguments:
  181. Address - address in the target process at which we begin reading;
  182. Data - pointer to the buffer (in our process) to be written to the
  183. with data read from the target process;
  184. Size - number of bytes to be read.
  185. Return Value:
  186. Returns TRUE if the write was successful, FALSE otherwise.
  187. --*/
  188. {
  189. BOOL Result;
  190. SIZE_T BytesRead = 0;
  191. Result = ReadProcessMemory(Process,
  192. Address,
  193. Data,
  194. Size,
  195. &BytesRead);
  196. if (Result == FALSE) {
  197. Error (File, Line,
  198. "ReadProcessMemory (%p for %d) failed with winerror %u (bytes read: %d)",
  199. Address,
  200. Size,
  201. GetLastError(),
  202. BytesRead);
  203. //
  204. // Try to give more information about why we failed.
  205. //
  206. {
  207. MEMORY_BASIC_INFORMATION MemoryInfo;
  208. SIZE_T Bytes;
  209. Bytes = VirtualQueryEx (Process,
  210. Address,
  211. &MemoryInfo,
  212. sizeof MemoryInfo);
  213. if (Bytes != sizeof MemoryInfo) {
  214. Error (NULL, 0, "VirtualQueryEx (%p) failed with error %u",
  215. Address, GetLastError());
  216. }
  217. Error (NULL, 0, " BaseAddress %p", MemoryInfo.BaseAddress);
  218. Error (NULL, 0, " AllocationBase %p", MemoryInfo.AllocationBase);
  219. Error (NULL, 0, " RegionSize %p", MemoryInfo.RegionSize);
  220. Error (NULL, 0, " State %08X", MemoryInfo.State);
  221. Error (NULL, 0, " Protect %08X", MemoryInfo.Protect);
  222. Error (NULL, 0, " Type %08X", MemoryInfo.Type);
  223. if (MemoryInfo.State == MEM_RESERVE) {
  224. Error (NULL, 0, " Uncommitted memory area");
  225. }
  226. }
  227. return FALSE;
  228. }
  229. else {
  230. if( Globals.InfoLevel > 0 ) {
  231. Comment( "ReadProcessMemory( %p for % d)",Address,BytesRead);
  232. }
  233. return TRUE;
  234. }
  235. }
  236. BOOL
  237. SetSymbolsPath (
  238. )
  239. /*++
  240. Routine Description:
  241. SetSymbolsPath tries to set automatically the symbol path if
  242. _NT_SYMBOL_PATH environment variable is not already defined.
  243. Arguments:
  244. None.
  245. Return Value:
  246. Returns TRUE if the symbols path seems to be ok, that is
  247. _NT_SYMBOL_PATH was defined or we managed to define it to
  248. a meaningful value.
  249. --*/
  250. {
  251. TCHAR Buffer [MAX_PATH];
  252. DWORD Length;
  253. BOOL Result;
  254. Length = GetEnvironmentVariable (TEXT("_NT_SYMBOL_PATH"),
  255. Buffer,
  256. MAX_PATH);
  257. if (Length == 0) {
  258. Warning (NULL, 0,
  259. "_NT_SYMBOL_PATH variable is not defined. Will be set to %%windir%%\\symbols.");
  260. Length = GetEnvironmentVariable (TEXT("windir"),
  261. Buffer,
  262. MAX_PATH);
  263. if (Length == 0) {
  264. Error (NULL, 0,
  265. "Cannot get value of WINDIR environment variable.");
  266. return FALSE;
  267. }
  268. strcat (Buffer, TEXT("\\symbols"));
  269. Result = SetEnvironmentVariable (TEXT("_NT_SYMBOL_PATH"),
  270. Buffer);
  271. if (Result == FALSE) {
  272. Error (NULL, 0,
  273. "Failed to set _NT_SYMBOL_PATH to `%s'", Buffer);
  274. return FALSE;
  275. }
  276. Comment ("_NT_SYMBOL_PATH set by default to %s", Buffer);
  277. }
  278. return TRUE;
  279. }