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.

218 lines
5.0 KiB

  1. /* Copyright (c) 1995, Microsoft Corporation, all rights reserved
  2. **
  3. ** debug.c
  4. ** Debug, trace, and assert library
  5. **
  6. ** 08/25/95 Steve Cobb
  7. */
  8. #include <windows.h> // Win32 root
  9. #include <debug.h> // Our public header
  10. #include <rtutils.h>
  11. #if (DBG || FREETRACE)
  12. /*----------------------------------------------------------------------------
  13. ** Globals
  14. **----------------------------------------------------------------------------
  15. */
  16. /* The debug trace ID of this module as returned by TraceRegisterExA.
  17. */
  18. DWORD g_dwTraceId = INVALID_TRACEID;
  19. DWORD g_dwInitRefCount = 0;
  20. HINSTANCE g_hTraceLibrary = NULL;
  21. /* RtUtil DLL tracing entrypoints loaded by DebugInit. It is safe to assume
  22. ** these addresses are loaded if g_dwTraceId is not -1.
  23. */
  24. TRACEREGISTEREXA g_pTraceRegisterExA = NULL;
  25. TRACEDEREGISTERA g_pTraceDeregisterA = NULL;
  26. TRACEDEREGISTEREXA g_pTraceDeregisterExA = NULL;
  27. TRACEPRINTFA g_pTracePrintfA = NULL;
  28. TRACEPRINTFEXA g_pTracePrintfExA = NULL;
  29. TRACEDUMPEXA g_pTraceDumpExA = NULL;
  30. /*----------------------------------------------------------------------------
  31. ** Routines
  32. **----------------------------------------------------------------------------
  33. */
  34. DWORD
  35. DebugFreeTraceLibrary()
  36. {
  37. if (g_dwInitRefCount == 0)
  38. {
  39. return NO_ERROR;
  40. }
  41. InterlockedDecrement(&g_dwInitRefCount);
  42. if (g_hTraceLibrary)
  43. {
  44. FreeLibrary(g_hTraceLibrary);
  45. }
  46. return NO_ERROR;
  47. }
  48. DWORD
  49. DebugLoadTraceLibary()
  50. {
  51. // Increment the ref count.
  52. //
  53. InterlockedIncrement(&g_dwInitRefCount);
  54. if ((g_hTraceLibrary = LoadLibrary( L"RTUTILS.DLL" ))
  55. && (g_pTraceRegisterExA = (TRACEREGISTEREXA )GetProcAddress(
  56. g_hTraceLibrary, "TraceRegisterExA" ))
  57. && (g_pTraceDeregisterA = (TRACEDEREGISTERA )GetProcAddress(
  58. g_hTraceLibrary, "TraceDeregisterA" ))
  59. && (g_pTraceDeregisterExA = (TRACEDEREGISTEREXA )GetProcAddress(
  60. g_hTraceLibrary, "TraceDeregisterExA" ))
  61. && (g_pTracePrintfA = (TRACEPRINTFA )GetProcAddress(
  62. g_hTraceLibrary, "TracePrintfA" ))
  63. && (g_pTracePrintfExA = (TRACEPRINTFEXA )GetProcAddress(
  64. g_hTraceLibrary, "TracePrintfExA" ))
  65. && (g_pTraceDumpExA = (TRACEDUMPEXA )GetProcAddress(
  66. g_hTraceLibrary, "TraceDumpExA" )))
  67. {
  68. return NO_ERROR;
  69. }
  70. // The trace library failed to load. Clean up the
  71. // globals as appropriate
  72. //
  73. DebugFreeTraceLibrary();
  74. return GetLastError();
  75. }
  76. DWORD
  77. DebugInitEx(
  78. IN CHAR* pszModule,
  79. OUT LPDWORD lpdwId)
  80. {
  81. DWORD dwErr = NO_ERROR;
  82. // Return whether the debugging module has already been initialized
  83. //
  84. if (*lpdwId != INVALID_TRACEID)
  85. {
  86. return NO_ERROR;
  87. }
  88. /* Load and register with the trace DLL.
  89. */
  90. dwErr = DebugLoadTraceLibary();
  91. if (dwErr != NO_ERROR)
  92. {
  93. return dwErr;
  94. }
  95. if (NULL != g_hTraceLibrary)
  96. {
  97. *lpdwId = g_pTraceRegisterExA( pszModule, 0 );
  98. if (*lpdwId == INVALID_TRACEID)
  99. {
  100. return GetLastError();
  101. }
  102. }
  103. return dwErr;
  104. }
  105. VOID
  106. DebugTermEx(
  107. OUT LPDWORD lpdwTraceId )
  108. /* Terminate debug support.
  109. */
  110. {
  111. /* De-register with the trace DLL.
  112. */
  113. if ((*lpdwTraceId != INVALID_TRACEID) && (NULL != g_pTraceDeregisterExA))
  114. {
  115. g_pTraceDeregisterExA( *lpdwTraceId, 4 );
  116. *lpdwTraceId = INVALID_TRACEID;
  117. }
  118. DebugFreeTraceLibrary();
  119. }
  120. VOID
  121. DebugInit(
  122. IN CHAR* pszModule )
  123. /* Initialize debug trace and assertion support.
  124. */
  125. {
  126. DebugInitEx(pszModule, &g_dwTraceId);
  127. }
  128. VOID
  129. DebugTerm(
  130. void )
  131. {
  132. DebugTermEx(&g_dwTraceId);
  133. }
  134. VOID
  135. Assert(
  136. IN const CHAR* pszExpression,
  137. IN const CHAR* pszFile,
  138. IN UINT unLine )
  139. /* Assertion handler called from ASSERT macro with the expression that
  140. ** failed and the filename and line number where the problem occurred.
  141. */
  142. {
  143. CHAR szBuf[ 512 ];
  144. wsprintfA(
  145. szBuf,
  146. "The assertion \"%s\" at line %d of file %s is false.",
  147. pszExpression, unLine, pszFile );
  148. MessageBoxA(
  149. NULL, szBuf, "Assertion Failure", MB_ICONEXCLAMATION + MB_OK );
  150. }
  151. VOID
  152. TracePrintfW1(
  153. CHAR* pszFormat,
  154. TCHAR* psz1 )
  155. /* Like TracePrintf but provides W->A argument conversion on the single
  156. ** string argument. This is better than mixing TracePrinfA and
  157. ** TracePrintfW calls which causes viewing problems when the trace is sent
  158. ** to a file.
  159. */
  160. {
  161. #ifdef UNICODE
  162. CHAR szBuf[ 512 ];
  163. DWORD cb;
  164. if (WideCharToMultiByte(
  165. CP_UTF8, 0, psz1, -1, szBuf, 512, NULL, NULL ) <= 0)
  166. {
  167. TRACE("TraceW1 failed");
  168. return;
  169. }
  170. TRACE1( pszFormat, szBuf );
  171. #else
  172. TRACE1( pszFormat, psz1 );
  173. #endif
  174. }
  175. #endif // (DBG || FREETRACE)