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.

248 lines
7.5 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. // We need to create this file mapping with access to everyone.
  50. // Since it is debug-only code, this doesn't represent a security risk.
  51. //
  52. SECURITY_INFORMATION SecurityInformation = DACL_SECURITY_INFORMATION;
  53. SECURITY_DESCRIPTOR SecurityDescriptor = {0};
  54. SECURITY_ATTRIBUTES SecurityAttributes = {0};
  55. SecurityAttributes.nLength = sizeof(SecurityAttributes);
  56. SecurityAttributes.lpSecurityDescriptor = &SecurityDescriptor;
  57. SecurityAttributes.bInheritHandle = FALSE;
  58. //
  59. // Give access to everybody
  60. //
  61. InitializeSecurityDescriptor(&SecurityDescriptor, SECURITY_DESCRIPTOR_REVISION);
  62. SetSecurityDescriptorDacl(&SecurityDescriptor, TRUE, (PACL)NULL, FALSE);
  63. //
  64. // Create the memory mapped file
  65. //
  66. m_hMemoryMappedFile = CreateFileMapping( INVALID_HANDLE_VALUE, &SecurityAttributes, PAGE_READWRITE, 0, sizeof(CSystemGlobalData), WIADEBUG_MEMORYMAPPED_FILENAME );
  67. if (m_hMemoryMappedFile)
  68. {
  69. m_pSystemGlobalData = reinterpret_cast<CSystemGlobalData*>(MapViewOfFile( m_hMemoryMappedFile, FILE_MAP_ALL_ACCESS, 0, 0, 0 ));
  70. if (m_pSystemGlobalData)
  71. {
  72. //
  73. // Start off with everything zeroed out.
  74. //
  75. ZeroMemory( m_pSystemGlobalData, sizeof( CSystemGlobalData ) );
  76. //
  77. // Get the initial global debug settings from the registry
  78. //
  79. m_pSystemGlobalData->nAllowDebugMessages = CSimpleReg( HKEY_CLASSES_ROOT, DEBUG_REGISTRY_PATH, false, KEY_READ ).Query( DEBUG_REGISTRY_ENABLE_DBG, 0 );
  80. //
  81. // No window is registered initially
  82. //
  83. m_pSystemGlobalData->hwndDebug = NULL;
  84. //
  85. // Initialize color table data
  86. //
  87. static const COLORREF crColors[NUMBER_OF_DEBUG_COLORS] =
  88. {
  89. RGB(0x00,0x00,0x00),
  90. RGB(0x00,0x00,0x7F),
  91. RGB(0x00,0x7F,0x00),
  92. RGB(0x00,0x7F,0x7F),
  93. RGB(0x7F,0x00,0x00),
  94. RGB(0x7F,0x00,0x7F),
  95. RGB(0x7F,0x00,0x7F),
  96. RGB(0x7F,0x7F,0x7F)
  97. };
  98. for (int i=0;i<NUMBER_OF_DEBUG_COLORS;i++)
  99. {
  100. m_pSystemGlobalData->crDebugColors[i] = crColors[i];
  101. }
  102. m_pSystemGlobalData->nCurrentColor = 0;
  103. }
  104. }
  105. }
  106. ReleaseMutex(m_hSystemDataMutex);
  107. }
  108. }
  109. //
  110. // If everything didn't work out, destroy the object completely
  111. //
  112. if (!IsValid())
  113. {
  114. Destroy();
  115. }
  116. }
  117. void CGlobalDebugState::Destroy(void)
  118. {
  119. if (m_pSystemGlobalData)
  120. {
  121. UnmapViewOfFile(m_pSystemGlobalData);
  122. m_pSystemGlobalData = NULL;
  123. }
  124. if (m_hMemoryMappedFile)
  125. {
  126. CloseHandle(m_hMemoryMappedFile);
  127. m_hMemoryMappedFile = NULL;
  128. }
  129. if (m_hSystemDataMutex)
  130. {
  131. CloseHandle(m_hSystemDataMutex);
  132. m_hSystemDataMutex = NULL;
  133. }
  134. }
  135. CGlobalDebugState::~CGlobalDebugState(void)
  136. {
  137. Destroy();
  138. }
  139. bool CGlobalDebugState::IsValid(void) const
  140. {
  141. return (m_hSystemDataMutex && m_hMemoryMappedFile && m_pSystemGlobalData);
  142. }
  143. DWORD CGlobalDebugState::AllowDebugMessages(void)
  144. {
  145. DWORD nResult = 0;
  146. CSystemGlobalData *pSystemGlobalData = Lock();
  147. if (pSystemGlobalData)
  148. {
  149. nResult = m_pSystemGlobalData->nAllowDebugMessages;
  150. Release();
  151. }
  152. return nResult;
  153. }
  154. DWORD CGlobalDebugState::AllowDebugMessages( DWORD nAllowDebugMessages )
  155. {
  156. DWORD nResult = 0;
  157. CSystemGlobalData *pSystemGlobalData = Lock();
  158. if (pSystemGlobalData)
  159. {
  160. nResult = m_pSystemGlobalData->nAllowDebugMessages;
  161. m_pSystemGlobalData->nAllowDebugMessages = nAllowDebugMessages;
  162. Release();
  163. }
  164. return nResult;
  165. }
  166. DWORD CGlobalDebugState::AllocateNextColorIndex(void)
  167. {
  168. DWORD nResult = 0;
  169. CSystemGlobalData *pSystemGlobalData = Lock();
  170. if (pSystemGlobalData)
  171. {
  172. nResult = m_pSystemGlobalData->nCurrentColor++;
  173. Release();
  174. }
  175. return nResult;
  176. }
  177. COLORREF CGlobalDebugState::GetColorFromIndex( DWORD nIndex )
  178. {
  179. COLORREF crResult = RGB(0,0,0);
  180. CSystemGlobalData *pSystemGlobalData = Lock();
  181. if (pSystemGlobalData)
  182. {
  183. crResult = m_pSystemGlobalData->crDebugColors[nIndex % NUMBER_OF_DEBUG_COLORS];
  184. Release();
  185. }
  186. return crResult;
  187. }
  188. bool CGlobalDebugState::DebugWindow( HWND hWnd )
  189. {
  190. bool bResult = false;
  191. CSystemGlobalData *pSystemGlobalData = Lock();
  192. if (pSystemGlobalData)
  193. {
  194. m_pSystemGlobalData->hwndDebug = static_cast<DWORD>(reinterpret_cast<UINT_PTR>(hWnd));
  195. bResult = true;
  196. Release();
  197. }
  198. return bResult;
  199. }
  200. HWND CGlobalDebugState::DebugWindow(void)
  201. {
  202. HWND hWndResult = NULL;
  203. CSystemGlobalData *pSystemGlobalData = Lock();
  204. if (pSystemGlobalData)
  205. {
  206. hWndResult = reinterpret_cast<HWND>(static_cast<UINT_PTR>(m_pSystemGlobalData->hwndDebug));
  207. Release();
  208. }
  209. return hWndResult;
  210. }
  211. bool CGlobalDebugState::SendDebugWindowMessage( UINT nMsg, WPARAM wParam, LPARAM lParam )
  212. {
  213. bool bResult = false;
  214. CSystemGlobalData *pSystemGlobalData = Lock();
  215. if (pSystemGlobalData)
  216. {
  217. HWND hWndDebug = reinterpret_cast<HWND>(static_cast<UINT_PTR>(m_pSystemGlobalData->hwndDebug));
  218. if (IsWindow(hWndDebug))
  219. {
  220. SendMessage( hWndDebug, nMsg, wParam, lParam );
  221. bResult = true;
  222. }
  223. Release();
  224. }
  225. return bResult;
  226. }