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.

244 lines
5.0 KiB

  1. #include <windows.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include "view.h"
  5. #include "dump.h"
  6. #include "memory.h"
  7. static CRITICAL_SECTION tCritSec;
  8. static PBYTE pDumpBuffer;
  9. static DWORD dwDumpBufferSize;
  10. static HANDLE hDump = INVALID_HANDLE_VALUE;
  11. BOOL
  12. InitializeDumpData(VOID)
  13. {
  14. DWORD dwCounter;
  15. DWORD dwResult;
  16. InitializeCriticalSection(&tCritSec);
  17. //
  18. // Allocate memory for the logging
  19. //
  20. pDumpBuffer = AllocMem(DUMP_BUFFER_SIZE);
  21. if (0 == pDumpBuffer) {
  22. return FALSE;
  23. }
  24. dwDumpBufferSize = 0;
  25. //
  26. // Get our file ready for dumping
  27. //
  28. hDump = CreateFileA(DUMP_LOG_NAME,
  29. GENERIC_READ | GENERIC_WRITE,
  30. 0,
  31. 0,
  32. CREATE_ALWAYS,
  33. FILE_ATTRIBUTE_NORMAL,
  34. 0);
  35. if (INVALID_HANDLE_VALUE == hDump) {
  36. return FALSE;
  37. }
  38. return TRUE;
  39. }
  40. BOOL
  41. AddToDump(PBYTE pBuffer,
  42. DWORD dwLength,
  43. BOOL bFlushImmediate)
  44. {
  45. BOOL bResult;
  46. EnterCriticalSection(&tCritSec);
  47. //
  48. // See if our write would cause overflow
  49. //
  50. if (TRUE == bFlushImmediate ||
  51. (dwDumpBufferSize + dwLength) >= DUMP_BUFFER_SIZE) {
  52. //
  53. // If we're doing an immediate flush, do the memory copy and buffer update
  54. //
  55. if (TRUE == bFlushImmediate) {
  56. MoveMemory((PVOID)(pDumpBuffer + dwDumpBufferSize), pBuffer, dwLength);
  57. dwDumpBufferSize += dwLength;
  58. }
  59. //
  60. // Do the flush
  61. //
  62. bResult = FlushBuffer();
  63. if (FALSE == bResult) {
  64. return FALSE;
  65. }
  66. dwDumpBufferSize = 0;
  67. }
  68. if (FALSE == bFlushImmediate) {
  69. MoveMemory((PVOID)(pDumpBuffer + dwDumpBufferSize), pBuffer, dwLength);
  70. dwDumpBufferSize += dwLength;
  71. }
  72. LeaveCriticalSection(&tCritSec);
  73. return TRUE;
  74. }
  75. VOID
  76. FlushForTermination(VOID)
  77. {
  78. DWORD dwCounter;
  79. EnterCriticalSection(&tCritSec);
  80. //
  81. // Flush the buffer
  82. //
  83. FlushBuffer();
  84. //
  85. // Flush the buffer
  86. //
  87. FlushFileBuffers(hDump);
  88. //
  89. // Close the file dump handle
  90. //
  91. if (INVALID_HANDLE_VALUE != hDump) {
  92. CloseHandle(hDump);
  93. hDump = INVALID_HANDLE_VALUE;
  94. }
  95. LeaveCriticalSection(&tCritSec);
  96. }
  97. BOOL
  98. FlushBuffer(VOID)
  99. {
  100. BOOL bResult;
  101. DWORD dwBytesWritten;
  102. bResult = WriteFile(hDump,
  103. pDumpBuffer,
  104. dwDumpBufferSize,
  105. &dwBytesWritten,
  106. 0);
  107. if (FALSE == bResult) {
  108. return FALSE;
  109. }
  110. /*
  111. bResult = FlushFileBuffers(hDump);
  112. if (FALSE == bResult) {
  113. return FALSE;
  114. }
  115. */
  116. return TRUE;
  117. }
  118. BOOL
  119. WriteThreadStart(DWORD dwThreadId,
  120. DWORD dwStartAddress)
  121. {
  122. THREADSTART threadStart;
  123. BOOL bResult = FALSE;
  124. threadStart.dwType = ThreadStartId;
  125. threadStart.dwThreadId = dwThreadId;
  126. threadStart.dwStartAddress = dwStartAddress;
  127. bResult = AddToDump((PVOID)&threadStart,
  128. sizeof(THREADSTART),
  129. FALSE);
  130. return bResult;
  131. }
  132. BOOL
  133. WriteExeFlow(DWORD dwThreadId,
  134. DWORD dwAddress,
  135. DWORD dwCallLevel)
  136. {
  137. EXEFLOW exeFlow;
  138. BOOL bResult = FALSE;
  139. exeFlow.dwType = ExeFlowId;
  140. exeFlow.dwThreadId = dwThreadId;
  141. exeFlow.dwAddress = dwAddress;
  142. exeFlow.dwCallLevel = dwCallLevel;
  143. bResult = AddToDump((PVOID)&exeFlow,
  144. sizeof(EXEFLOW),
  145. FALSE);
  146. return bResult;
  147. }
  148. BOOL
  149. WriteDllInfo(CHAR *szDLL,
  150. DWORD dwBaseAddress,
  151. DWORD dwLength)
  152. {
  153. DLLBASEINFO dllBaseInfo;
  154. BOOL bResult = FALSE;
  155. CHAR szFile[_MAX_FNAME];
  156. //
  157. // Trim off any directory information
  158. //
  159. _splitpath(szDLL, 0, 0, szFile, 0);
  160. strcpy(dllBaseInfo.szDLLName, szFile);
  161. dllBaseInfo.dwType = DllBaseInfoId;
  162. dllBaseInfo.dwBase = dwBaseAddress;
  163. dllBaseInfo.dwLength = dwLength;
  164. bResult = AddToDump((PVOID)&dllBaseInfo,
  165. sizeof(DLLBASEINFO),
  166. FALSE);
  167. return bResult;
  168. }
  169. BOOL
  170. WriteMapInfo(DWORD dwAddress,
  171. DWORD dwMaxMapLength)
  172. {
  173. MAPINFO mapInfo;
  174. BOOL bResult = FALSE;
  175. mapInfo.dwType = MapInfoId;
  176. mapInfo.dwAddress = dwAddress;
  177. mapInfo.dwMaxMapLength = dwMaxMapLength;
  178. bResult = AddToDump((PVOID)&mapInfo,
  179. sizeof(MAPINFO),
  180. FALSE);
  181. return bResult;
  182. }
  183. BOOL
  184. WriteError(CHAR *szMessage)
  185. {
  186. ERRORINFO errorInfo;
  187. BOOL bResult = FALSE;
  188. errorInfo.dwType = ErrorInfoId;
  189. strcpy(errorInfo.szMessage, szMessage);
  190. bResult = AddToDump((PVOID)&errorInfo,
  191. sizeof(ERRORINFO),
  192. TRUE);
  193. return bResult;
  194. }