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.

230 lines
6.8 KiB

  1. #include "precomp.h"
  2. #pragma hdrstop
  3. #include "wiadebug.h"
  4. CSystemGlobalData *CGlobalDebugState::Lock(void)
  5. {
  6. CSystemGlobalData *pSystemGlobalData = NULL;
  7. if (IsValid())
  8. {
  9. DWORD dwWait = WaitForSingleObject(m_hSystemDataMutex,10000);
  10. if (WAIT_OBJECT_0 == dwWait)
  11. {
  12. pSystemGlobalData = m_pSystemGlobalData;
  13. }
  14. }
  15. return pSystemGlobalData;
  16. }
  17. void CGlobalDebugState::Release(void)
  18. {
  19. ReleaseMutex(m_hSystemDataMutex);
  20. }
  21. CGlobalDebugState::CGlobalDebugState(void)
  22. : m_hSystemDataMutex(NULL),
  23. m_hMemoryMappedFile(NULL),
  24. m_pSystemGlobalData(NULL)
  25. {
  26. //
  27. // Create the mutex that protects the system global data
  28. //
  29. m_hSystemDataMutex = CreateMutex( NULL, FALSE, WIADEBUG_MEMORYMAPPED_MUTEXNAME );
  30. if (m_hSystemDataMutex)
  31. {
  32. //
  33. // Grab the mutex that protects the system global data
  34. //
  35. DWORD dwWait = WaitForSingleObject( m_hSystemDataMutex, 10000 );
  36. if (WAIT_OBJECT_0 == dwWait)
  37. {
  38. //
  39. // First try to open the memory mapped file.
  40. //
  41. m_hMemoryMappedFile = OpenFileMapping( FILE_MAP_WRITE, FALSE, WIADEBUG_MEMORYMAPPED_FILENAME );
  42. if (m_hMemoryMappedFile)
  43. {
  44. m_pSystemGlobalData = reinterpret_cast<CSystemGlobalData*>(MapViewOfFile( m_hMemoryMappedFile, FILE_MAP_ALL_ACCESS, 0, 0, 0 ));
  45. }
  46. else
  47. {
  48. //
  49. // Create the memory mapped file
  50. //
  51. m_hMemoryMappedFile = CreateFileMapping( INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, sizeof(CSystemGlobalData), WIADEBUG_MEMORYMAPPED_FILENAME );
  52. if (m_hMemoryMappedFile)
  53. {
  54. m_pSystemGlobalData = reinterpret_cast<CSystemGlobalData*>(MapViewOfFile( m_hMemoryMappedFile, FILE_MAP_ALL_ACCESS, 0, 0, 0 ));
  55. if (m_pSystemGlobalData)
  56. {
  57. //
  58. // Start off with everything zeroed out.
  59. //
  60. ZeroMemory( m_pSystemGlobalData, sizeof( CSystemGlobalData ) );
  61. //
  62. // Get the initial global debug settings from the registry
  63. //
  64. m_pSystemGlobalData->nAllowDebugMessages = CSimpleReg( HKEY_CLASSES_ROOT, DEBUG_REGISTRY_PATH, false, KEY_READ ).Query( DEBUG_REGISTRY_ENABLE_DBG, 0 );
  65. //
  66. // No window is registered initially
  67. //
  68. m_pSystemGlobalData->hwndDebug = NULL;
  69. //
  70. // Initialize color table data
  71. //
  72. static const COLORREF crColors[NUMBER_OF_DEBUG_COLORS] =
  73. {
  74. RGB(0x00,0x00,0x00),
  75. RGB(0x00,0x00,0x7F),
  76. RGB(0x00,0x7F,0x00),
  77. RGB(0x00,0x7F,0x7F),
  78. RGB(0x7F,0x00,0x00),
  79. RGB(0x7F,0x00,0x7F),
  80. RGB(0x7F,0x00,0x7F),
  81. RGB(0x7F,0x7F,0x7F)
  82. };
  83. for (int i=0;i<NUMBER_OF_DEBUG_COLORS;i++)
  84. {
  85. m_pSystemGlobalData->crDebugColors[i] = crColors[i];
  86. }
  87. m_pSystemGlobalData->nCurrentColor = 0;
  88. }
  89. }
  90. }
  91. ReleaseMutex(m_hSystemDataMutex);
  92. }
  93. }
  94. //
  95. // If everything didn't work out, destroy the object completely
  96. //
  97. if (!IsValid())
  98. {
  99. Destroy();
  100. }
  101. }
  102. void CGlobalDebugState::Destroy(void)
  103. {
  104. if (m_pSystemGlobalData)
  105. {
  106. UnmapViewOfFile(m_pSystemGlobalData);
  107. m_pSystemGlobalData = NULL;
  108. }
  109. if (m_hMemoryMappedFile)
  110. {
  111. CloseHandle(m_hMemoryMappedFile);
  112. m_hMemoryMappedFile = NULL;
  113. }
  114. if (m_hSystemDataMutex)
  115. {
  116. CloseHandle(m_hSystemDataMutex);
  117. m_hSystemDataMutex = NULL;
  118. }
  119. }
  120. CGlobalDebugState::~CGlobalDebugState(void)
  121. {
  122. Destroy();
  123. }
  124. bool CGlobalDebugState::IsValid(void) const
  125. {
  126. return (m_hSystemDataMutex && m_hMemoryMappedFile && m_pSystemGlobalData);
  127. }
  128. DWORD CGlobalDebugState::AllowDebugMessages(void)
  129. {
  130. DWORD nResult = 0;
  131. CSystemGlobalData *pSystemGlobalData = Lock();
  132. if (pSystemGlobalData)
  133. {
  134. nResult = m_pSystemGlobalData->nAllowDebugMessages;
  135. Release();
  136. }
  137. return nResult;
  138. }
  139. DWORD CGlobalDebugState::AllowDebugMessages( DWORD nAllowDebugMessages )
  140. {
  141. DWORD nResult = 0;
  142. CSystemGlobalData *pSystemGlobalData = Lock();
  143. if (pSystemGlobalData)
  144. {
  145. nResult = m_pSystemGlobalData->nAllowDebugMessages;
  146. m_pSystemGlobalData->nAllowDebugMessages = nAllowDebugMessages;
  147. Release();
  148. }
  149. return nResult;
  150. }
  151. DWORD CGlobalDebugState::AllocateNextColorIndex(void)
  152. {
  153. DWORD nResult = 0;
  154. CSystemGlobalData *pSystemGlobalData = Lock();
  155. if (pSystemGlobalData)
  156. {
  157. nResult = m_pSystemGlobalData->nCurrentColor++;
  158. Release();
  159. }
  160. return nResult;
  161. }
  162. COLORREF CGlobalDebugState::GetColorFromIndex( DWORD nIndex )
  163. {
  164. COLORREF crResult = RGB(0,0,0);
  165. CSystemGlobalData *pSystemGlobalData = Lock();
  166. if (pSystemGlobalData)
  167. {
  168. crResult = m_pSystemGlobalData->crDebugColors[nIndex % NUMBER_OF_DEBUG_COLORS];
  169. Release();
  170. }
  171. return crResult;
  172. }
  173. bool CGlobalDebugState::DebugWindow( HWND hWnd )
  174. {
  175. bool bResult = false;
  176. CSystemGlobalData *pSystemGlobalData = Lock();
  177. if (pSystemGlobalData)
  178. {
  179. m_pSystemGlobalData->hwndDebug = static_cast<DWORD>(reinterpret_cast<UINT_PTR>(hWnd));
  180. bResult = true;
  181. Release();
  182. }
  183. return bResult;
  184. }
  185. HWND CGlobalDebugState::DebugWindow(void)
  186. {
  187. HWND hWndResult = NULL;
  188. CSystemGlobalData *pSystemGlobalData = Lock();
  189. if (pSystemGlobalData)
  190. {
  191. hWndResult = reinterpret_cast<HWND>(static_cast<UINT_PTR>(m_pSystemGlobalData->hwndDebug));
  192. Release();
  193. }
  194. return hWndResult;
  195. }
  196. bool CGlobalDebugState::SendDebugWindowMessage( UINT nMsg, WPARAM wParam, LPARAM lParam )
  197. {
  198. bool bResult = false;
  199. CSystemGlobalData *pSystemGlobalData = Lock();
  200. if (pSystemGlobalData)
  201. {
  202. HWND hWndDebug = reinterpret_cast<HWND>(static_cast<UINT_PTR>(m_pSystemGlobalData->hwndDebug));
  203. if (IsWindow(hWndDebug))
  204. {
  205. SendMessage( hWndDebug, nMsg, wParam, lParam );
  206. bResult = true;
  207. }
  208. Release();
  209. }
  210. return bResult;
  211. }