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.

170 lines
2.9 KiB

  1. /*++
  2. Copyright (c) 1994 Microsoft Corporation
  3. All rights reserved.
  4. Module Name:
  5. debug.h
  6. Abstract:
  7. PrintUI core debugging macros/tools.
  8. Author:
  9. Lazar Ivanov (LazarI) Jul-05-2000
  10. Revision History:
  11. --*/
  12. #include <stdarg.h>
  13. #include <windef.h>
  14. #include <winbase.h>
  15. #include <shlwapi.h>
  16. #include "debug.h"
  17. #if DBG
  18. // globals - print & break only for errors
  19. // DWORD MODULE_DEBUG = MODULE_DEBUG_INIT(DBG_ERROR|DBG_INFO, DBG_ERROR);
  20. DWORD MODULE_DEBUG = MODULE_DEBUG_INIT(DBG_ERROR, DBG_ERROR);
  21. // private globals
  22. static CRITICAL_SECTION g_csDebug;
  23. static BOOL g_csDebugInitialized = FALSE;
  24. //////////////////////////////////////
  25. // single thread checking routines
  26. //
  27. VOID
  28. _DbgSingleThread(
  29. const DWORD *pdwThreadId
  30. )
  31. {
  32. SPLASSERT(g_csDebugInitialized);
  33. EnterCriticalSection(&g_csDebug);
  34. if( 0 == *pdwThreadId )
  35. *((DWORD*)(pdwThreadId)) = (DWORD)GetCurrentThreadId();
  36. SPLASSERT(*pdwThreadId == (DWORD)GetCurrentThreadId());
  37. LeaveCriticalSection(&g_csDebug);
  38. }
  39. VOID
  40. _DbgSingleThreadReset(
  41. const DWORD *pdwThreadId
  42. )
  43. {
  44. SPLASSERT(g_csDebugInitialized);
  45. EnterCriticalSection(&g_csDebug);
  46. *((DWORD*)(pdwThreadId)) = 0;
  47. LeaveCriticalSection(&g_csDebug);
  48. }
  49. VOID
  50. _DbgSingleThreadNot(
  51. const DWORD *pdwThreadId
  52. )
  53. {
  54. SPLASSERT(g_csDebugInitialized);
  55. EnterCriticalSection(&g_csDebug);
  56. SPLASSERT(*pdwThreadId != (DWORD)GetCurrentThreadId());
  57. LeaveCriticalSection(&g_csDebug);
  58. }
  59. //////////////////////////////
  60. // generic error logging API
  61. //
  62. VOID
  63. _DbgMsg(
  64. LPCSTR pszMsgFormat,
  65. ...
  66. )
  67. {
  68. va_list vargs;
  69. CHAR szBuffer[1024]; // 1K buffer should be enough
  70. SPLASSERT(g_csDebugInitialized);
  71. EnterCriticalSection(&g_csDebug);
  72. va_start(vargs, pszMsgFormat);
  73. wvnsprintfA(szBuffer, sizeof(szBuffer)/sizeof(szBuffer[0]), pszMsgFormat, vargs);
  74. va_end(vargs);
  75. OutputDebugStringA(szBuffer);
  76. LeaveCriticalSection(&g_csDebug);
  77. }
  78. VOID
  79. _DbgWarnInvalid(
  80. PVOID pvObject,
  81. UINT uDbg,
  82. UINT uLine,
  83. LPCSTR pszFileA,
  84. LPCSTR pszModuleA
  85. )
  86. {
  87. DBGMSG(DBG_WARN, ("Invalid Object LastError = %d\nLine %d, %hs\n", GetLastError(), uLine, pszFileA));
  88. }
  89. HRESULT
  90. _DbgInit(
  91. VOID
  92. )
  93. {
  94. HRESULT hr = S_OK;
  95. __try
  96. {
  97. InitializeCriticalSection(&g_csDebug);
  98. g_csDebugInitialized = TRUE;
  99. }
  100. __except(EXCEPTION_EXECUTE_HANDLER)
  101. {
  102. hr = E_OUTOFMEMORY;
  103. }
  104. return hr;
  105. }
  106. HRESULT
  107. _DbgDone(
  108. VOID
  109. )
  110. {
  111. if( g_csDebugInitialized )
  112. {
  113. DeleteCriticalSection(&g_csDebug);
  114. }
  115. return S_OK;
  116. }
  117. VOID
  118. _DbgBreak(
  119. VOID
  120. )
  121. {
  122. // since we don't want to break in kd, we should
  123. // break only if a user mode debugger is present.
  124. if( IsDebuggerPresent() )
  125. {
  126. DebugBreak();
  127. }
  128. else
  129. {
  130. // take the process down
  131. int *p = NULL;
  132. *p = 42;
  133. }
  134. }
  135. #endif // DBG