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.

525 lines
13 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1992 - 1996.
  5. //
  6. // File: trace.cxx
  7. //
  8. // Contents: TraceInfo functions
  9. //
  10. // History: 14-Jul-95 t-stevan Created
  11. //
  12. //----------------------------------------------------------------------------
  13. #include <stdarg.h>
  14. #include <ole2int.h>
  15. #if DBG==1
  16. #ifdef FLAT
  17. #include <sem.hxx>
  18. #include <dllsem.hxx>
  19. #endif // FLAT
  20. #include "oleprint.hxx"
  21. #include "sym.hxx"
  22. // *** Global data ***
  23. DWORD g_dwInfoLevel = INF_OFF;
  24. extern CSym *g_pSym;
  25. extern char gPidString[];
  26. //+---------------------------------------------------------------------------
  27. //
  28. // Function: TraceInfoEnabled
  29. //
  30. // Synopsis: Checks our trace info level to see if output of
  31. // trace information is enabled
  32. //
  33. // Arguments: (none)
  34. //
  35. // Returns: > 0 if enabled, 0 if not
  36. //
  37. // History: 15-Jul-95 t-stevan Created
  38. //
  39. //----------------------------------------------------------------------------
  40. inline int TraceInfoEnabled()
  41. {
  42. return g_dwInfoLevel & INF_BASE;
  43. }
  44. //+---------------------------------------------------------------------------
  45. //
  46. // Function: TraceCmnEnabled
  47. //
  48. // Synopsis: Checks our trace info level to see if output of
  49. // cmn api information is enabled
  50. //
  51. // Arguments: (none)
  52. //
  53. // Returns: > 0 if enabled, 0 if not
  54. //
  55. // History: 15-Jul-95 t-stevan Created
  56. //
  57. //----------------------------------------------------------------------------
  58. inline int TraceCmnEnabled()
  59. {
  60. return g_dwInfoLevel & INF_CMN;
  61. }
  62. //+---------------------------------------------------------------------------
  63. //
  64. // Function: SymInfoEnabled
  65. //
  66. // Synopsis: Checks our trace info level to see if output of
  67. // symbol information is enabled
  68. //
  69. // Arguments: (none)
  70. //
  71. // Returns: > 0 if enabled, 0 if not
  72. //
  73. // History: 15-Jul-95 t-stevan Created
  74. //
  75. //----------------------------------------------------------------------------
  76. inline int SymInfoEnabled()
  77. {
  78. return g_dwInfoLevel & INF_SYM;
  79. }
  80. //+---------------------------------------------------------------------------
  81. //
  82. // Function: StructInfoEnabled
  83. //
  84. // Synopsis: Checks our trace info level to see if output of
  85. // expanded structures is enabled
  86. //
  87. // Arguments: (none)
  88. //
  89. // Returns: > 0 if enabled, 0 if not
  90. //
  91. // History: 15-Jul-95 t-stevan Created
  92. //
  93. //----------------------------------------------------------------------------
  94. inline int StructInfoEnabled()
  95. {
  96. return g_dwInfoLevel & INF_STRUCT;
  97. }
  98. //+---------------------------------------------------------------------------
  99. //
  100. // Function: TLSIncTraceNestingLevel
  101. //
  102. // Synopsis: Returns the current nesting level, then increments it
  103. //
  104. // Returns: nesting level for OLETRACE
  105. //
  106. // History: 13-Jul-95 t-stevan Created
  107. //
  108. //----------------------------------------------------------------------------
  109. inline LONG TLSIncTraceNestingLevel()
  110. {
  111. HRESULT hr;
  112. COleTls tls(hr);
  113. if (SUCCEEDED(hr))
  114. {
  115. return (tls->cTraceNestingLevel)++;
  116. }
  117. return 0;
  118. }
  119. //+---------------------------------------------------------------------------
  120. //
  121. // Function: TLSDecTraceNestingLevel
  122. //
  123. // Synopsis: Decrement and return nesting level
  124. //
  125. // Returns: nesting level for OLETRACE
  126. //
  127. // History: 13-Jul-95 t-stevan Created
  128. //
  129. //----------------------------------------------------------------------------
  130. inline LONG TLSDecTraceNestingLevel()
  131. {
  132. HRESULT hr;
  133. COleTls tls(hr);
  134. if (SUCCEEDED(hr))
  135. {
  136. return --(tls->cTraceNestingLevel);
  137. }
  138. return 0;
  139. }
  140. // *** Inline Functions
  141. //+---------------------------------------------------------------------------
  142. //
  143. // Function: IsAPIID
  144. //
  145. // Synopsis: Returns whether or not an 32-bit ID is a API ID
  146. //
  147. // Arguments: [dwID] - 32-bit ID
  148. //
  149. // Returns: TRUE if it is an API ID, FALSE otherwise
  150. //
  151. // History: 04-Aug-95 t-stevan Created
  152. //
  153. //----------------------------------------------------------------------------
  154. BOOL IsAPIID(DWORD dwID)
  155. {
  156. return !(dwID>>16);
  157. }
  158. //+---------------------------------------------------------------------------
  159. //
  160. // Function: GetNameFromAPIID
  161. //
  162. // Synopsis: Returns a pointer to a string containing the API name
  163. //
  164. // Arguments: [dwID] - API ID
  165. //
  166. // Returns: Pointer to a string
  167. //
  168. // History: 04-Aug-95 t-stevan Created
  169. //
  170. //----------------------------------------------------------------------------
  171. const char *GetNameFromAPIID(DWORD dwID)
  172. {
  173. return (g_ppNameTables[dwID>>16])[dwID&0xffff];
  174. }
  175. //+---------------------------------------------------------------------------
  176. //
  177. // Function: GetNameFromOBJID
  178. //
  179. // Synopsis: Returns a pointer to a string containing the object/method name
  180. //
  181. // Arguments: [dwID] - 32-bit ID
  182. //
  183. // Returns: Pointer to a string
  184. //
  185. // History: 04-Aug-95 t-stevan Created
  186. //
  187. //----------------------------------------------------------------------------
  188. const char *GetNameFromOBJID(DWORD dwID, IUnknown *pUnk, char *pBuf)
  189. {
  190. wsprintfA(pBuf, "%s(%x)->%s", g_pscInterfaceNames[dwID>>16], pUnk, (g_ppNameTables[dwID>>16])[dwID&0xffff]);
  191. return pBuf;
  192. }
  193. //+---------------------------------------------------------------------------
  194. //
  195. // Function: _oletracein
  196. //
  197. // Synopsis: Prints trace information for API/Method-entry
  198. //
  199. // Arguments: [dwID] - API/Method ID
  200. //
  201. // Returns: nothing
  202. //
  203. // History: 14-Jul-95 t-stevan Created
  204. //
  205. //----------------------------------------------------------------------------
  206. void _oletracein(DWORD dwID, ...)
  207. {
  208. const char *pscFormat;
  209. va_list args;
  210. int iNestingLevel;
  211. va_start(args, dwID);
  212. iNestingLevel = TLSIncTraceNestingLevel();
  213. if(TraceInfoEnabled())
  214. {
  215. if(IsAPIID(dwID))
  216. {
  217. // This is an API
  218. pscFormat = va_arg(args, const char*);
  219. oleprintf(iNestingLevel,
  220. GetNameFromAPIID(dwID), pscFormat, args);
  221. }
  222. else
  223. {
  224. IUnknown *pUnk;
  225. char szTemp[128];
  226. // This is an object/method call
  227. pUnk = va_arg(args, IUnknown *);
  228. pscFormat = va_arg(args, const char *);
  229. oleprintf(iNestingLevel,
  230. GetNameFromOBJID(dwID, pUnk, szTemp), pscFormat, args);
  231. }
  232. }
  233. va_end(args);
  234. }
  235. //+---------------------------------------------------------------------------
  236. //
  237. // Function: _oletracecmnin
  238. //
  239. // Synopsis: Prints trace information for API/Method-entry
  240. //
  241. // Arguments: [dwID] - API/Method ID
  242. //
  243. // Returns: nothing
  244. //
  245. // History: 14-Jul-95 t-stevan Created
  246. //
  247. //----------------------------------------------------------------------------
  248. void _oletracecmnin(DWORD dwID, ...)
  249. {
  250. const char *pscFormat;
  251. va_list args;
  252. int iNestingLevel;
  253. if (!TraceCmnEnabled())
  254. {
  255. return;
  256. }
  257. va_start(args, dwID);
  258. iNestingLevel = TLSIncTraceNestingLevel();
  259. if(TraceInfoEnabled())
  260. {
  261. if(IsAPIID(dwID))
  262. {
  263. // This is an API
  264. pscFormat = va_arg(args, const char*);
  265. oleprintf(iNestingLevel,
  266. GetNameFromAPIID(dwID), pscFormat, args);
  267. }
  268. else
  269. {
  270. IUnknown *pUnk;
  271. char szTemp[128];
  272. // This is an object/method call
  273. pUnk = va_arg(args, IUnknown *);
  274. pscFormat = va_arg(args, const char *);
  275. oleprintf(iNestingLevel,
  276. GetNameFromOBJID(dwID, pUnk, szTemp), pscFormat, args);
  277. }
  278. }
  279. va_end(args);
  280. }
  281. //+---------------------------------------------------------------------------
  282. //
  283. // Function: _oletraceout
  284. //
  285. // Synopsis: Prints trace information for API/Method-exit. assuming
  286. // return value is an HRESULT
  287. //
  288. // Arguments: [dwID] - API/Method ID
  289. // [hr] - return value
  290. //
  291. // Returns: nothing
  292. //
  293. // History: 14-Jul-95 t-stevan Created
  294. //
  295. //----------------------------------------------------------------------------
  296. void _oletraceout(DWORD dwID, HRESULT hr)
  297. {
  298. _oletraceoutex(dwID, RETURNFMT("%x"), hr);
  299. }
  300. //+---------------------------------------------------------------------------
  301. //
  302. // Function: _oletracecmnout
  303. //
  304. // Synopsis: Prints trace information for API/Method-exit. assuming
  305. // return value is an HRESULT
  306. //
  307. // Arguments: [dwID] - API/Method ID
  308. // [hr] - return value
  309. //
  310. // Returns: nothing
  311. //
  312. // History: 14-Jul-95 t-stevan Created
  313. //
  314. //----------------------------------------------------------------------------
  315. void _oletracecmnout(DWORD dwID, HRESULT hr)
  316. {
  317. _oletracecmnoutex(dwID, RETURNFMT("%x"), hr);
  318. }
  319. //+---------------------------------------------------------------------------
  320. //
  321. // Function: _oletraceoutex
  322. //
  323. // Synopsis: Prints trace information for API/Method-exit, using given
  324. // format string for return value
  325. //
  326. // Arguments: [dwID] - API/Method ID
  327. //
  328. // Returns: nothing
  329. //
  330. // History: 14-Jul-95 t-stevan Created
  331. //
  332. //----------------------------------------------------------------------------
  333. void _oletraceoutex(DWORD dwID, ...)
  334. {
  335. const char *pscFormat;
  336. va_list args;
  337. int iNestingLevel;
  338. va_start(args, dwID);
  339. iNestingLevel = TLSDecTraceNestingLevel();
  340. if(TraceInfoEnabled())
  341. {
  342. if(IsAPIID(dwID))
  343. {
  344. // This is an API
  345. pscFormat = va_arg(args, const char*);
  346. oleprintf(iNestingLevel,
  347. GetNameFromAPIID(dwID), pscFormat, args);
  348. }
  349. else
  350. {
  351. IUnknown *pUnk;
  352. char szTemp[128];
  353. // This is an object/method call
  354. pUnk = va_arg(args, IUnknown *);
  355. pscFormat = va_arg(args, const char *);
  356. oleprintf(iNestingLevel,
  357. GetNameFromOBJID(dwID, pUnk, szTemp), pscFormat, args);
  358. }
  359. }
  360. va_end(args);
  361. }
  362. //+---------------------------------------------------------------------------
  363. //
  364. // Function: _oletracecmnoutex
  365. //
  366. // Synopsis: Prints trace information for API/Method-exit, using given
  367. // format string for return value
  368. //
  369. // Arguments: [dwID] - API/Method ID
  370. //
  371. // Returns: nothing
  372. //
  373. // History: 14-Jul-95 t-stevan Created
  374. //
  375. //----------------------------------------------------------------------------
  376. void _oletracecmnoutex(DWORD dwID, ...)
  377. {
  378. const char *pscFormat;
  379. va_list args;
  380. int iNestingLevel;
  381. if (!TraceCmnEnabled())
  382. {
  383. return;
  384. }
  385. va_start(args, dwID);
  386. iNestingLevel = TLSDecTraceNestingLevel();
  387. if(TraceInfoEnabled())
  388. {
  389. if(IsAPIID(dwID))
  390. {
  391. // This is an API
  392. pscFormat = va_arg(args, const char*);
  393. oleprintf(iNestingLevel,
  394. GetNameFromAPIID(dwID), pscFormat, args);
  395. }
  396. else
  397. {
  398. IUnknown *pUnk;
  399. char szTemp[128];
  400. // This is an object/method call
  401. pUnk = va_arg(args, IUnknown *);
  402. pscFormat = va_arg(args, const char *);
  403. oleprintf(iNestingLevel,
  404. GetNameFromOBJID(dwID, pUnk, szTemp), pscFormat, args);
  405. }
  406. }
  407. va_end(args);
  408. }
  409. //+---------------------------------------------------------------------------
  410. //
  411. // Function: InitializeTraceInfo
  412. //
  413. // Synopsis: Initializes the trace information's global variables,
  414. //
  415. // Arguments: (none)
  416. //
  417. // Returns: nothing
  418. //
  419. // History: 11-Jul-95 t-stevan Created
  420. //
  421. //----------------------------------------------------------------------------
  422. void InitializeTraceInfo()
  423. {
  424. // get Pid string once
  425. _itoa(GetCurrentProcessId(), gPidString, 10);
  426. if(TraceInfoEnabled() && SymInfoEnabled())
  427. {
  428. // Initialize the symbol information
  429. // CAUTION: This is very expensive to turn on!
  430. g_pSym = new CSym();
  431. }
  432. }
  433. void SetTraceInfoLevel(DWORD dwLevel)
  434. {
  435. g_dwInfoLevel = dwLevel;
  436. if(TraceInfoEnabled() && (g_pSym == NULL) && SymInfoEnabled())
  437. {
  438. // Initialize the symbol information
  439. g_pSym = new CSym();
  440. }
  441. }
  442. //+---------------------------------------------------------------------------
  443. //
  444. // Function: CleanupTraceInfo
  445. //
  446. // Synopsis: Cleans up trace information's global variables
  447. //
  448. // Arguments: (none)
  449. //
  450. // Returns: nothing
  451. //
  452. // History: 11-Jul-95 t-stevan Created
  453. //
  454. //----------------------------------------------------------------------------
  455. void CleanupTraceInfo()
  456. {
  457. if(g_pSym != NULL)
  458. {
  459. delete g_pSym;
  460. }
  461. WriteToLogFile(NULL); // Stop writing to log file
  462. }
  463. #endif // DBG==1