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.

447 lines
7.4 KiB

  1. /*++
  2. Copyright (c) 2000 Microsoft Corporation
  3. Module Name:
  4. Trace Manager
  5. Abstract:
  6. This does all the interfacing with the tracing code.
  7. Author:
  8. Marc Reyhner 8/28/2000
  9. --*/
  10. #include "stdafx.h"
  11. #include "ZippyWindow.h"
  12. #include "TraceManager.h"
  13. #include "eZippy.h"
  14. #include "resource.h"
  15. // Instantions of all the static class members.
  16. HANDLE CTraceManager::gm_hDBWinSharedDataHandle = NULL;
  17. LPVOID CTraceManager::gm_hDBWinSharedData = NULL;
  18. HANDLE CTraceManager::gm_hDBWinDataReady = NULL;
  19. HANDLE CTraceManager::gm_hDBWinDataAck = NULL;
  20. // Our various defines for dbwin.
  21. #define DBWIN_BUFFER_READY _T("DBWIN_BUFFER_READY")
  22. #define DBWIN_DATA_READY _T("DBWIN_DATA_READY")
  23. #define DBWIN_BUFFER_NAME _T("DBWIN_BUFFER")
  24. #define DBWIN_BUFFER_SIZE 4096
  25. CTraceManager::CTraceManager(
  26. )
  27. /*++
  28. Routine Description:
  29. The constructor simply initializes the class variables.
  30. Arguments:
  31. None
  32. Return value:
  33. None
  34. --*/
  35. {
  36. m_hThread = NULL;
  37. m_bThreadStop = FALSE;
  38. }
  39. CTraceManager::~CTraceManager(
  40. )
  41. /*++
  42. Routine Description:
  43. The destructor does nothing now. Don't call this before the
  44. listen thread exits or bad things may happen.
  45. Arguments:
  46. None
  47. Return value:
  48. None
  49. --*/
  50. {
  51. }
  52. DWORD
  53. CTraceManager::StartListenThread(
  54. IN CZippyWindow *rZippyWindow
  55. )
  56. /*++
  57. Routine Description:
  58. This starts a new thread listening for trace output.
  59. Arguments:
  60. rZippyWindow - The main zippy window which will have data sent
  61. to it.
  62. Return value:
  63. 0 - Success
  64. Non zero - a win32 error code
  65. --*/
  66. {
  67. DWORD dwResult;
  68. DWORD threadId;
  69. dwResult = 0;
  70. m_rZippyWindow = rZippyWindow;
  71. m_hThread = CreateThread(NULL,0,_ThreadProc,this,0,&threadId);
  72. if (!m_hThread) {
  73. dwResult = GetLastError();
  74. }
  75. return dwResult;
  76. }
  77. DWORD
  78. CTraceManager::_InitTraceManager(
  79. )
  80. /*++
  81. Routine Description:
  82. This initializes all the mutexes and shared memory
  83. for dbwin. It also call TRC_Initialize
  84. Arguments:
  85. None
  86. Return value:
  87. 0 - Success
  88. Non zero - a win32 error code
  89. --*/
  90. {
  91. DWORD dwResult;
  92. BOOL bResult;
  93. dwResult = 0;
  94. TRC_Initialize(TRUE);
  95. gm_hDBWinDataAck = CreateEvent(NULL,FALSE,FALSE,DBWIN_BUFFER_READY);
  96. if (!gm_hDBWinDataAck) {
  97. dwResult = GetLastError();
  98. goto CLEANUP_AND_EXIT;
  99. }
  100. if (ERROR_ALREADY_EXISTS == GetLastError()) {
  101. TCHAR dlgTitle[MAX_STR_LEN];
  102. TCHAR dlgMessage[MAX_STR_LEN];
  103. LoadStringSimple(IDS_ZIPPYWINDOWTITLE,dlgTitle);
  104. LoadStringSimple(IDS_ZIPPYALREADYEXISTS,dlgMessage);
  105. MessageBox(NULL,dlgMessage,dlgTitle,MB_OK|MB_ICONERROR);
  106. ExitProcess(1);
  107. }
  108. gm_hDBWinDataReady = CreateEvent(NULL,FALSE,FALSE,DBWIN_DATA_READY);
  109. if (!gm_hDBWinDataReady) {
  110. dwResult = GetLastError();
  111. goto CLEANUP_AND_EXIT;
  112. }
  113. gm_hDBWinSharedDataHandle = CreateFileMapping(INVALID_HANDLE_VALUE,NULL,PAGE_READWRITE,
  114. 0,DBWIN_BUFFER_SIZE,DBWIN_BUFFER_NAME);
  115. if (!gm_hDBWinSharedDataHandle) {
  116. dwResult = GetLastError();
  117. goto CLEANUP_AND_EXIT;
  118. }
  119. gm_hDBWinSharedData = MapViewOfFile(gm_hDBWinSharedDataHandle,
  120. FILE_MAP_READ,0,0,0);
  121. if (!gm_hDBWinSharedData) {
  122. dwResult = GetLastError();
  123. goto CLEANUP_AND_EXIT;
  124. }
  125. CLEANUP_AND_EXIT:
  126. if (dwResult) {
  127. if (gm_hDBWinSharedData) {
  128. UnmapViewOfFile(gm_hDBWinSharedData);
  129. gm_hDBWinSharedData = NULL;
  130. }
  131. if (gm_hDBWinSharedDataHandle) {
  132. CloseHandle(gm_hDBWinSharedDataHandle);
  133. gm_hDBWinSharedDataHandle = NULL;
  134. }
  135. if (gm_hDBWinDataReady) {
  136. CloseHandle(gm_hDBWinDataReady);
  137. gm_hDBWinDataReady = NULL;
  138. }
  139. if (gm_hDBWinDataAck) {
  140. CloseHandle(gm_hDBWinDataAck);
  141. gm_hDBWinDataAck = NULL;
  142. }
  143. }
  144. return dwResult;
  145. }
  146. VOID
  147. CTraceManager::_CleanupTraceManager(
  148. )
  149. /*++
  150. Routine Description:
  151. Cleans up all the dbwin stuff.
  152. Arguments:
  153. None
  154. Return value:
  155. None
  156. --*/
  157. {
  158. if (gm_hDBWinSharedData) {
  159. UnmapViewOfFile(gm_hDBWinSharedData);
  160. gm_hDBWinSharedData = NULL;
  161. }
  162. if (gm_hDBWinSharedDataHandle) {
  163. CloseHandle(gm_hDBWinSharedDataHandle);
  164. gm_hDBWinSharedDataHandle = NULL;
  165. }
  166. if (gm_hDBWinDataReady) {
  167. CloseHandle(gm_hDBWinDataReady);
  168. gm_hDBWinDataReady = NULL;
  169. }
  170. if (gm_hDBWinDataAck) {
  171. CloseHandle(gm_hDBWinDataAck);
  172. gm_hDBWinDataAck = NULL;
  173. }
  174. }
  175. VOID
  176. CTraceManager::OnNewData(
  177. )
  178. /*++
  179. Routine Description:
  180. This is called whenever new data shows up for the trace. The data
  181. is then forwarded to the zippy window
  182. Arguments:
  183. None
  184. Return value:
  185. None
  186. --*/
  187. {
  188. LPTSTR debugStr;
  189. LPSTR asciiDebugStr;
  190. DWORD processID;
  191. UINT debugStrLen;
  192. #ifdef UNICODE
  193. INT result;
  194. TCHAR debugWStr[DBWIN_BUFFER_SIZE];
  195. #endif
  196. debugStr = NULL;
  197. processID = *(LPDWORD)gm_hDBWinSharedData;
  198. asciiDebugStr = (LPSTR)((PBYTE)(gm_hDBWinSharedData) + sizeof(DWORD));
  199. debugStrLen = strlen(asciiDebugStr);
  200. #ifdef UNICODE
  201. debugStr = debugWStr;
  202. result = MultiByteToWideChar(CP_ACP,0,asciiDebugStr,debugStrLen+1,
  203. debugStr,DBWIN_BUFFER_SIZE);
  204. if (!result) {
  205. // error
  206. goto CLEANUP_AND_EXIT;
  207. }
  208. #else
  209. debugStr = asciiDebugStr;
  210. #endif
  211. m_rZippyWindow->AppendTextToWindow(processID,debugStr,debugStrLen);
  212. CLEANUP_AND_EXIT:
  213. return;
  214. }
  215. DWORD WINAPI
  216. CTraceManager::_ThreadProc(
  217. IN LPVOID lpParameter
  218. )
  219. /*++
  220. Routine Description:
  221. Simply calls the non static version of the thread procedure.
  222. Arguments:
  223. lpParameter - Thread start information
  224. Return value:
  225. See ThreadProc for return values
  226. --*/
  227. {
  228. return ((CTraceManager*)lpParameter)->ThreadProc();
  229. }
  230. DWORD
  231. CTraceManager::ThreadProc(
  232. )
  233. /*++
  234. Routine Description:
  235. This loops catching debug data and then forwarding it to the zippy window.
  236. Arguments:
  237. None
  238. Return value:
  239. 0 - Success
  240. Non zero - Win32 error code
  241. --*/
  242. {
  243. DWORD dwResult;
  244. dwResult = 0;
  245. SetEvent(gm_hDBWinDataAck);
  246. while (!m_bThreadStop) {
  247. dwResult = WaitForSingleObject(gm_hDBWinDataReady,INFINITE);
  248. if (dwResult != WAIT_OBJECT_0) {
  249. break;
  250. }
  251. OnNewData();
  252. SetEvent(gm_hDBWinDataAck);
  253. }
  254. return dwResult;
  255. }
  256. BOOL
  257. CTraceManager::GetCurrentConfig(
  258. IN PTRC_CONFIG lpConfig
  259. )
  260. /*++
  261. Routine Description:
  262. Returns the current trace configuration
  263. Arguments:
  264. lpConfig - Pointer to a TRC_CONFIG struct which will receive the configuation.
  265. Return value:
  266. TRUE - config was successfully retrieved.
  267. FALSE - There was an error getting the config.
  268. --*/
  269. {
  270. return TRC_GetConfig(lpConfig,sizeof(TRC_CONFIG));
  271. }
  272. BOOL
  273. CTraceManager::SetCurrentConfig(
  274. IN PTRC_CONFIG lpNewConfig
  275. )
  276. /*++
  277. Routine Description:
  278. Sets the trace configuration
  279. Arguments:
  280. lpConfig - Pointer to the new configuration
  281. Return value:
  282. TRUE - config was successfully set.
  283. FALSE - There was an error setting the config.
  284. --*/
  285. {
  286. return TRC_SetConfig(lpNewConfig,sizeof(TRC_CONFIG));
  287. }
  288. VOID
  289. CTraceManager::TRC_ResetTraceFiles(
  290. )
  291. /*++
  292. Routine Description:
  293. Just a straight wrapper to the global TRC_ResetTraceFiles function,
  294. Arguments:
  295. None
  296. Return value:
  297. None
  298. --*/
  299. {
  300. // The :: is necessary to get the C version of the func.
  301. ::TRC_ResetTraceFiles();
  302. }