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.

383 lines
13 KiB

  1. /*
  2. * ++ Copyright (c) 2001 Microsoft Corporation Module Name: tracer.h Abstract:
  3. * Retail tracing for WinHttp DLL
  4. * Author: S. Ganesh
  5. * Environment: Win32 user-mode
  6. */
  7. #if defined(__cplusplus)
  8. extern "C"
  9. {
  10. #endif
  11. #ifndef ENABLE_DEBUG
  12. typedef enum { None, Bool, Int, Dword, String, Handle, Pointer } TRACE_FUNCTION_RETURN_TYPE;
  13. #else
  14. typedef DEBUG_FUNCTION_RETURN_TYPE TRACE_FUNCTION_RETURN_TYPE;
  15. #endif
  16. /* Externs: */
  17. class HANDLE_OBJECT;
  18. struct TraceValues
  19. {
  20. long _dwPrefix;
  21. LPCSTR _lpszFnName;
  22. TRACE_FUNCTION_RETURN_TYPE _ReturnType;
  23. TraceValues()
  24. :
  25. _dwPrefix(-1),
  26. _lpszFnName(NULL),
  27. _ReturnType(None)
  28. {
  29. }
  30. TraceValues (long dwPrefix, LPCSTR lpszFnName, TRACE_FUNCTION_RETURN_TYPE ReturnType)
  31. :
  32. _dwPrefix(dwPrefix),
  33. _lpszFnName(lpszFnName),
  34. _ReturnType(ReturnType)
  35. {
  36. }
  37. };
  38. class CTracer
  39. {
  40. /*
  41. -----------------------------------------------------------------------------------------------------------------------
  42. Statics:
  43. -----------------------------------------------------------------------------------------------------------------------
  44. */
  45. private:
  46. /* Global-level CTracer instance, instantiated in GlobalDataInitialize(): */
  47. static CTracer *s_pTracer;
  48. static BOOL s_bTraceInitialized;
  49. static BOOL s_bTraceEnabled;
  50. static BOOL s_bApiTraceEnabled;
  51. static DWORD s_dwMaxFileSize;
  52. public:
  53. static CCritSec s_CritSectionTraceInit;
  54. /*
  55. -----------------------------------------------------------------------------------------------------------------------
  56. Instance:
  57. -----------------------------------------------------------------------------------------------------------------------
  58. */
  59. private:
  60. DWORD _traceControlFlags;
  61. HANDLE _traceFileHandle;
  62. DWORD _dwWritten;
  63. CRITICAL_SECTION _CriticalSection;
  64. static DWORD ReadTraceSettingsDwordKey(IN LPCSTR ParameterName, OUT LPDWORD ParameterValue);
  65. static DWORD ReadTraceSettingsStringKey
  66. (
  67. IN LPCSTR ParameterName,
  68. OUT LPSTR ParameterValue,
  69. IN OUT LPDWORD ParameterLength
  70. );
  71. /*
  72. -----------------------------------------------------------------------------------------------------------------------
  73. -----------------------------------------------------------------------------------------------------------------------
  74. */
  75. public:
  76. static BOOL GlobalTraceInit( BOOL fOverrideRegistryToEnableTracing = FALSE);
  77. static BOOL unlockedGlobalTraceInit( BOOL fOverrideRegistryToEnableTracing = FALSE);
  78. static void GlobalTraceTerm(void);
  79. static
  80. void EnableTracing(void)
  81. {
  82. s_bTraceEnabled = TRUE;
  83. }
  84. static
  85. void DisableTracing(void)
  86. {
  87. s_bTraceEnabled = FALSE;
  88. }
  89. static
  90. BOOL IsTracingEnabled(void)
  91. {
  92. return s_bTraceEnabled;
  93. }
  94. static
  95. void EnableApiTracing(void)
  96. {
  97. s_bApiTraceEnabled = TRUE;
  98. }
  99. static
  100. void DisableApiTracing(void)
  101. {
  102. s_bApiTraceEnabled = FALSE;
  103. }
  104. static
  105. BOOL IsApiTracingEnabled(void)
  106. {
  107. return s_bApiTraceEnabled;
  108. }
  109. static
  110. CTracer *GlobalTracer(void)
  111. {
  112. return s_pTracer;
  113. }
  114. CTracer(DWORD traceControlFlags);
  115. ~CTracer(void);
  116. /*
  117. -----------------------------------------------------------------------------------------------------------------------
  118. -----------------------------------------------------------------------------------------------------------------------
  119. */
  120. private:
  121. BOOL OpenTraceFile(IN LPSTR Filename);
  122. VOID CloseTraceFile(VOID);
  123. /*
  124. -----------------------------------------------------------------------------------------------------------------------
  125. -----------------------------------------------------------------------------------------------------------------------
  126. */
  127. public:
  128. TraceValues TraceFnEnter
  129. (
  130. IN DWORD Category,
  131. IN TRACE_FUNCTION_RETURN_TYPE ReturnType,
  132. IN LPCSTR Function,
  133. IN LPCSTR ParameterList OPTIONAL,
  134. IN...
  135. );
  136. TraceValues TraceFnEnter2
  137. (
  138. IN DWORD Category,
  139. IN TRACE_FUNCTION_RETURN_TYPE ReturnType,
  140. IN LPCSTR Function,
  141. IN HINTERNET hInternet,
  142. IN LPCSTR ParameterList OPTIONAL,
  143. IN...
  144. );
  145. VOID TraceFnLeave(IN DWORD_PTR Variable, IN TraceValues *pTraceValues);
  146. VOID TracePrintError(IN DWORD Error, IN TraceValues *pTraceValues);
  147. VOID TracePrint(IN DWORD dwPrefix, IN LPSTR Format, ...);
  148. VOID TracePrint2(IN LPSTR Format, ...);
  149. VOID TracePrintString(IN LPSTR String, IN long dwPrefix = -1);
  150. VOID TraceOut(IN LPSTR Buffer);
  151. VOID TraceDump(IN LPSTR Text, IN LPBYTE Address, IN DWORD Size, IN long dwPrefix = -1);
  152. DWORD TraceDumpFormat(IN LPBYTE Address, IN DWORD Size, IN DWORD ElementSize, OUT LPSTR Buffer);
  153. DWORD TraceDumpText
  154. (
  155. IN LPBYTE Address,
  156. IN DWORD Size,
  157. OUT LPSTR Buffer,
  158. IN DWORD BufferSize,
  159. IN long dwPrefix = -1
  160. );
  161. /*
  162. -----------------------------------------------------------------------------------------------------------------------
  163. -----------------------------------------------------------------------------------------------------------------------
  164. */
  165. private:
  166. static LPSTR TraceSetPrefix(IN LPSTR Buffer, IN long dwPrefix = -1);
  167. /*
  168. -----------------------------------------------------------------------------------------------------------------------
  169. -----------------------------------------------------------------------------------------------------------------------
  170. */
  171. private:
  172. class CCounter
  173. {
  174. /*
  175. -----------------------------------------------------------------------------------------------------------------------
  176. -----------------------------------------------------------------------------------------------------------------------
  177. */
  178. private:
  179. LONG _dwRef;
  180. /*
  181. -----------------------------------------------------------------------------------------------------------------------
  182. -----------------------------------------------------------------------------------------------------------------------
  183. */
  184. public:
  185. CCounter()
  186. :
  187. _dwRef(0)
  188. {
  189. }
  190. DWORD AddOne(void) { return(DWORD)::InterlockedIncrement(&_dwRef); }
  191. };
  192. /*
  193. -----------------------------------------------------------------------------------------------------------------------
  194. -----------------------------------------------------------------------------------------------------------------------
  195. */
  196. public:
  197. DWORD GetOneUniqueId(void) { return uniqueIdGenerator.AddOne(); }
  198. /*
  199. ===================================================================================================================
  200. Helper, since there's no strnstr in std lib:
  201. ===================================================================================================================
  202. */
  203. static LPSTR strnstr(LPSTR haystack, int Len, LPCSTR needle)
  204. {
  205. /*~~~~~~~~~~~~~~~~~~~~~~*/
  206. int found = 0;
  207. int need = strlen(needle);
  208. int i, start;
  209. /*~~~~~~~~~~~~~~~~~~~~~~*/
  210. for(start = i = 0; i < Len; i++)
  211. {
  212. if(haystack[i] == needle[found])
  213. {
  214. if(++found == need)
  215. {
  216. i -= need - 1; /* beginning of string */
  217. return haystack + i;
  218. }
  219. }
  220. else
  221. {
  222. found = 0;
  223. }
  224. }
  225. return NULL;
  226. }
  227. /*
  228. -----------------------------------------------------------------------------------------------------------------------
  229. -----------------------------------------------------------------------------------------------------------------------
  230. */
  231. private:
  232. CCounter uniqueIdGenerator;
  233. };
  234. #define GLOBAL_TRACER() \
  235. if(CTracer::IsTracingEnabled() && CTracer::GlobalTracer()) \
  236. CTracer::GlobalTracer()->
  237. /*
  238. =======================================================================================================================
  239. TRACE_START - initialize debugging support
  240. =======================================================================================================================
  241. */
  242. #define TRACE_START() CTracer::GlobalTraceInit()
  243. /*
  244. =======================================================================================================================
  245. TRACE_FINISH - terminate debugging support
  246. =======================================================================================================================
  247. */
  248. #define TRACE_FINISH() CTracer::GlobalTraceTerm()
  249. /*
  250. =======================================================================================================================
  251. TRACE_ENTER_API - Call when entering an API function
  252. =======================================================================================================================
  253. */
  254. #define TRACE_ENTER_API(ParameterList) \
  255. TraceValues a_traceValues; \
  256. if(CTracer::IsTracingEnabled() && CTracer::GlobalTracer()) \
  257. { \
  258. /*
  259. * TraceFnEnter has a dual-purpose: one, to return a TraceValues structure after
  260. * splitting ParameterList,
  261. * and two, to print the FnEnter messages
  262. */ \
  263. a_traceValues = CTracer::GlobalTracer()->TraceFnEnter ParameterList; \
  264. }
  265. /*
  266. =======================================================================================================================
  267. See related DEBUG_ENTER2_API in inetdbg.h
  268. =======================================================================================================================
  269. */
  270. #define TRACE_ENTER2_API(ParameterList) \
  271. TraceValues a_traceValues; \
  272. if(CTracer::IsTracingEnabled() && CTracer::GlobalTracer()) \
  273. { \
  274. /*
  275. * TraceFnEnter2 has a dual-purpose: one, to return a TraceValues structure after
  276. * splitting ParameterList,
  277. * and two, to print the FnEnter messages
  278. */ \
  279. a_traceValues = CTracer::GlobalTracer()->TraceFnEnter2 ParameterList; \
  280. }
  281. #define TRACE_LEAVE_API(Variable) \
  282. if \
  283. ( \
  284. CTracer::IsTracingEnabled() \
  285. && CTracer::GlobalTracer() \
  286. && CTracer::IsApiTracingEnabled() \
  287. ) \
  288. { \
  289. CTracer::GlobalTracer()->TraceFnLeave(Variable, &a_traceValues); \
  290. }
  291. /*
  292. =======================================================================================================================
  293. TRACE_ERROR - displays an error and its symbolic name
  294. =======================================================================================================================
  295. */
  296. #define TRACE_ERROR(Category, Error) \
  297. if \
  298. ( \
  299. CTracer::IsTracingEnabled() \
  300. && CTracer::GlobalTracer() \
  301. && a_traceValues._lpszFnName \
  302. ) \
  303. { \
  304. CTracer::GlobalTracer()->TracePrintError(Error, &a_traceValues); \
  305. }
  306. #define TRACE_PRINT_API(Category, ErrorLevel, Args) GLOBAL_TRACER() TracePrint2 Args
  307. #define TRACE_DUMP_API(Category, Text, Address, Length, dwPrefix) \
  308. GLOBAL_TRACER() TraceDump \
  309. ( \
  310. Text, \
  311. (LPBYTE) Address, \
  312. Length, \
  313. dwPrefix \
  314. )
  315. #define TRACE_GET_REQUEST_ID() \
  316. CTracer::IsTracingEnabled() \
  317. && CTracer::GlobalTracer() ? CTracer::GlobalTracer()->GetOneUniqueId() : -1
  318. #define TRACE_SET_ID_FROM_HANDLE(hInternet) \
  319. if \
  320. ( \
  321. hInternet \
  322. && CTracer::IsTracingEnabled() \
  323. && CTracer::GlobalTracer() \
  324. && a_traceValues._lpszFnName \
  325. ) \
  326. { \
  327. HINTERNET a_hRequestMapped = NULL; \
  328. if(MapHandleToAddress(hInternet, (LPVOID *) &a_hRequestMapped, FALSE) == ERROR_SUCCESS) \
  329. { \
  330. BOOL a_isLocal; \
  331. BOOL a_isAsync; \
  332. if(RIsHandleLocal(a_hRequestMapped, &a_isLocal, &a_isAsync, TypeHttpRequestHandle) == ERROR_SUCCESS) \
  333. { \
  334. a_traceValues._dwPrefix = ((HTTP_REQUEST_HANDLE_OBJECT *) (a_hRequestMapped))->GetRequestId(); \
  335. } \
  336. DereferenceObject(a_hRequestMapped); \
  337. } \
  338. }
  339. #if defined(__cplusplus)
  340. }
  341. #endif