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.

247 lines
4.8 KiB

  1. ///////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (c) Microsoft Corporation
  4. //
  5. // SYNOPSIS
  6. //
  7. // Defines the API into the IAS trace facility.
  8. //
  9. ///////////////////////////////////////////////////////////////////////////////
  10. #include <windows.h>
  11. #include <stdlib.h>
  12. #include <rtutils.h>
  13. //////////
  14. // Flags passed for all trace calls.
  15. //////////
  16. #define IAS_TRACE_FLAGS \
  17. (0x00010000 | TRACE_USE_MASK | TRACE_USE_MSEC | TRACE_USE_DATE)
  18. //////////
  19. // Trace ID for this module.
  20. //////////
  21. DWORD dwTraceID = INVALID_TRACEID;
  22. //////////
  23. // Initialization ref. count.
  24. //////////
  25. LONG lRefCount = 0;
  26. //////////
  27. // Non-zero if the registration code is locked.
  28. //////////
  29. LONG lLocked = 0;
  30. //////////
  31. // Macros to lock/unlock the registration code.
  32. //////////
  33. #define LOCK_TRACE() \
  34. while (InterlockedExchange(&lLocked, 1)) Sleep(5)
  35. #define UNLOCK_TRACE() \
  36. InterlockedExchange(&lLocked, 0)
  37. VOID
  38. WINAPI
  39. IASTraceInitialize( VOID )
  40. {
  41. LONG state;
  42. DWORD status;
  43. MEMORY_BASIC_INFORMATION mbi;
  44. WCHAR filename[MAX_PATH + 1], *basename, *suffix;
  45. LOCK_TRACE();
  46. if (++lRefCount == 1)
  47. {
  48. // Find the base address of this module.
  49. if (VirtualQuery(IASTraceInitialize, &mbi, sizeof(mbi)))
  50. {
  51. // Get the module filename.
  52. status = GetModuleFileNameW(
  53. (HINSTANCE)mbi.AllocationBase,
  54. filename,
  55. MAX_PATH
  56. );
  57. if (status != 0)
  58. {
  59. // Strip everything before the last backslash.
  60. basename = wcsrchr(filename, L'\\');
  61. if (basename == NULL)
  62. {
  63. basename = filename;
  64. }
  65. else
  66. {
  67. ++basename;
  68. }
  69. // Strip everything after the last dot.
  70. suffix = wcsrchr(basename, L'.');
  71. if (suffix)
  72. {
  73. *suffix = L'\0';
  74. }
  75. // Convert to uppercase.
  76. _wcsupr(basename);
  77. // Register the module.
  78. dwTraceID = TraceRegisterExW(basename, 0);
  79. }
  80. }
  81. }
  82. UNLOCK_TRACE();
  83. }
  84. VOID
  85. WINAPI
  86. IASTraceUninitialize( VOID )
  87. {
  88. LOCK_TRACE();
  89. if (--lRefCount == 0)
  90. {
  91. TraceDeregisterW(dwTraceID);
  92. dwTraceID = INVALID_TRACEID;
  93. }
  94. UNLOCK_TRACE();
  95. }
  96. //////////
  97. // Formats an error message from the system message table.
  98. //////////
  99. DWORD
  100. WINAPI
  101. IASFormatSysErr(
  102. DWORD dwError,
  103. PSTR lpBuffer,
  104. DWORD nSize
  105. )
  106. {
  107. DWORD nChar;
  108. // Attempt to format the message using the system message table.
  109. nChar = FormatMessageA(
  110. FORMAT_MESSAGE_FROM_SYSTEM,
  111. NULL,
  112. dwError,
  113. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
  114. lpBuffer,
  115. nSize,
  116. NULL
  117. );
  118. if (nChar > 0)
  119. {
  120. // Format succeeded, so strip any trailing newline and exit.
  121. if (lpBuffer[nChar - 1] == '\n')
  122. {
  123. --nChar;
  124. lpBuffer[nChar] = '\0';
  125. if (lpBuffer[nChar - 1] == '\r')
  126. {
  127. --nChar;
  128. lpBuffer[nChar] = '\0';
  129. }
  130. }
  131. goto exit;
  132. }
  133. // Only error condition we can handle is when the message is not found.
  134. if (GetLastError() != ERROR_MR_MID_NOT_FOUND)
  135. {
  136. goto exit;
  137. }
  138. // Do we have enough space for the fallback error message ?
  139. if (nSize < 25)
  140. {
  141. SetLastError(ERROR_INSUFFICIENT_BUFFER);
  142. goto exit;
  143. }
  144. // No entry in the message table, so just format the raw error code.
  145. nChar = wsprintfA(lpBuffer, "Unknown error 0x%0lX", dwError);
  146. exit:
  147. return nChar;
  148. }
  149. VOID
  150. WINAPIV
  151. IASTracePrintf(
  152. IN PCSTR szFormat,
  153. ...
  154. )
  155. {
  156. va_list marker;
  157. va_start(marker, szFormat);
  158. TraceVprintfExA(
  159. dwTraceID,
  160. IAS_TRACE_FLAGS,
  161. szFormat,
  162. marker
  163. );
  164. va_end(marker);
  165. }
  166. VOID
  167. WINAPI
  168. IASTraceString(
  169. IN PCSTR szString
  170. )
  171. {
  172. TracePutsExA(
  173. dwTraceID,
  174. IAS_TRACE_FLAGS,
  175. szString
  176. );
  177. }
  178. VOID
  179. WINAPI
  180. IASTraceBinary(
  181. IN CONST BYTE* lpbBytes,
  182. IN DWORD dwByteCount
  183. )
  184. {
  185. TraceDumpExA(
  186. dwTraceID,
  187. IAS_TRACE_FLAGS,
  188. (LPBYTE)lpbBytes,
  189. dwByteCount,
  190. 1,
  191. FALSE,
  192. NULL
  193. );
  194. }
  195. VOID
  196. WINAPI
  197. IASTraceFailure(
  198. IN PCSTR szFunction,
  199. IN DWORD dwError
  200. )
  201. {
  202. CHAR szMessage[256];
  203. DWORD nChar;
  204. nChar = IASFormatSysErr(
  205. dwError,
  206. szMessage,
  207. sizeof(szMessage)
  208. );
  209. szMessage[nChar] = '\0';
  210. IASTracePrintf("%s failed: %s", szFunction, szMessage);
  211. }