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.

3391 lines
99 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1997.
  5. //
  6. // File: T R A C E . C P P
  7. //
  8. // Contents: The actual tracing code (loading from ini, calling the
  9. // trace routines, etc.
  10. //
  11. // Notes:
  12. //
  13. // Author: jeffspr 9 Apr 1997
  14. //
  15. //----------------------------------------------------------------------------
  16. #include <pch.h>
  17. #pragma hdrstop
  18. #ifdef ENABLETRACE
  19. #include <crtdbg.h>
  20. #include "ncdebug.h"
  21. #include "ncmisc.h"
  22. #include <ntrtl.h>
  23. #include <avrf.h>
  24. DWORD g_dwTlsTracing = 0;
  25. LPCRITICAL_SECTION g_csTracing = NULL;
  26. #define MAX_TRACE_LEN 4096
  27. EXTERN_C
  28. IMAGE_DOS_HEADER __ImageBase;
  29. //---[ CTracing class ]-------------------------------------------------------
  30. //
  31. // Don't give this class a constructor or destructor. We declare a global
  32. // (static to this module) instance of this class and, by definition, static
  33. // variables are automatically initialized to zero.
  34. //
  35. class CTracing
  36. {
  37. public:
  38. CTracing();
  39. ~CTracing();
  40. // Initialize/Deinitialize this class
  41. //
  42. private:
  43. HRESULT HrInit();
  44. HRESULT HrUnInit();
  45. public:
  46. VOID Trace( TraceTagId ttid,
  47. PCSTR pszaTrace );
  48. private:
  49. BOOL m_fInitialized; // Has the object been initialized
  50. BOOL m_fAttemptedLogFileOpen; // Already attempted to open log
  51. BOOL m_fDisableLogFile; // Disable use of file logging?
  52. UINT m_uiAllocOnWhichToBreak; // For _CrtSetBreakAlloc
  53. HANDLE m_hLogFile; // Handle for debug output file
  54. CHAR m_szLogFilePath[MAX_PATH+1]; // File for debug output
  55. BOOL m_fDebugFlagsLoaded; // Have these been loaded yet.
  56. VOID CorruptionCheck(); // Validate the tracetag structure
  57. HRESULT HrLoadOptionsFromIniFile();
  58. HRESULT HrLoadSectionsFromIniFile();
  59. HRESULT HrLoadDebugFlagsFromIniFile();
  60. HRESULT HrWriteDebugFlagsToIniFile();
  61. HRESULT HrOpenLogFile();
  62. HRESULT HrProcessTagSection(TraceTagElement * ptte);
  63. HRESULT HrGetPrivateProfileString( PCSTR lpAppName,
  64. PCSTR lpKeyName,
  65. PCSTR lpDefault,
  66. PSTR lpReturnedString,
  67. DWORD nSize,
  68. PCSTR lpFileName,
  69. DWORD * pcchReturn );
  70. HRESULT FIniFileInit(); // Returns S_OK if the file exist
  71. };
  72. //---[ Static Variables ]-----------------------------------------------------
  73. #pragma warning(disable:4073) // warning about the following init_seg statement
  74. #pragma init_seg(lib)
  75. static CTracing g_Tracing; // Our global tracing object
  76. //---[ Constants ]------------------------------------------------------------
  77. static const WCHAR c_szDebugIniFileName[] = L"netcfg.ini"; // .INI file
  78. CHAR c_szDebugIniFileNameA[MAX_PATH]; // .INI file
  79. static const CHAR c_szTraceLogFileNameA[] = "nctrace.log"; // .LOG file
  80. // constants for the INI file labels
  81. static const CHAR c_szaOptions[] = "Options";
  82. static const CHAR c_szaLogFilePath[] = "LogFilePath";
  83. static const CHAR c_szaDisableLogFile[] = "DisableLogFile";
  84. const INT c_iDefaultDisableLogFile = 0;
  85. static CHAR c_szLowMemory[] = "<low on memory>";
  86. //+---------------------------------------------------------------------------
  87. //
  88. // Function: HrInitTracing
  89. //
  90. // Purpose: Initialize the Tracing object and other random data.
  91. //
  92. // Arguments:
  93. // bDisableFaultInjection [in] Disable App Verifier Fault injection for tracing
  94. //
  95. // Returns: S_OK or valid Win32 HRESULT
  96. //
  97. // Author: jeffspr 9 Apr 1997
  98. //
  99. // Notes:
  100. //
  101. HRESULT HrInitTracing(BOOL bDisableFaultInjection)
  102. {
  103. HMODULE hVrfLib;
  104. VERIFIER_ENABLE_FAULT_INJECTION_EXCLUSION_RANGE_FUNCTION pfnVrfEnable;
  105. ULONG Index;
  106. if (bDisableFaultInjection)
  107. {
  108. if (NtCurrentPeb()->NtGlobalFlag & FLG_APPLICATION_VERIFIER)
  109. {
  110. hVrfLib = LoadLibrary (L"verifier.dll");
  111. if (hVrfLib)
  112. {
  113. // If appverifier is not enabled on this process, the LoadLibrary will fail.
  114. pfnVrfEnable = (VERIFIER_ENABLE_FAULT_INJECTION_EXCLUSION_RANGE_FUNCTION)GetProcAddress(hVrfLib, "VerifierEnableFaultInjectionExclusionRange");
  115. if (pfnVrfEnable)
  116. {
  117. Index = pfnVrfEnable(HrInitTracing, HrUnInitTracing);
  118. WCHAR szTmp[MAX_PATH * 2];
  119. DWORD dwProcessId = GetCurrentProcessId();
  120. WCHAR szModuleName[MAX_PATH];
  121. ZeroMemory(szModuleName, MAX_PATH);
  122. if (!GetModuleFileName(reinterpret_cast<HMODULE>(&__ImageBase), szModuleName, MAX_PATH))
  123. {
  124. if (!GetModuleFileName(reinterpret_cast<HMODULE>(::GetModuleHandle(NULL)), szModuleName, MAX_PATH))
  125. {
  126. wsprintfW(szModuleName, L"<unknown>");
  127. }
  128. }
  129. if (0 == Index)
  130. {
  131. wsprintfW(szTmp, L"NETCFG: %s (%x) Tracing initialized. Problem disabling AVRF fault injection.\n", szModuleName, dwProcessId);
  132. }
  133. else
  134. {
  135. wsprintfW(szTmp, L"NETCFG: %s (%x) Tracing initialized. Disabled AVRF fault injection for tracing. Index = %x\n", szModuleName, dwProcessId, Index);
  136. }
  137. OutputDebugString(szTmp);
  138. }
  139. }
  140. }
  141. }
  142. return S_OK;
  143. }
  144. extern HANDLE g_hHeap;
  145. //+---------------------------------------------------------------------------
  146. //
  147. // Function: HrMallocNoFI
  148. //
  149. // Purpose: HRESULT returning version of malloc. This version is immune
  150. // against AVRF fault injection due to the
  151. // VerifierEnableFaultInjectionExclusionRange call in
  152. // HrInitTracing above.
  153. //
  154. // Arguments:
  155. // cb [in] Count of bytes to allocate.
  156. // ppv [out] Address of returned allocation.
  157. //
  158. // Returns: S_OK or E_OUTOFMEMORY;
  159. //
  160. // Author: deonb 6/24/02
  161. //
  162. // Notes: Free the returned buffer with MemFree.
  163. //
  164. HRESULT HrMallocNoFI(size_t cb, PVOID* ppv) throw()
  165. {
  166. Assert(ppv);
  167. *ppv = NULL;
  168. if (!g_hHeap)
  169. {
  170. g_hHeap = GetProcessHeap();
  171. if (!g_hHeap)
  172. {
  173. return E_UNEXPECTED;
  174. }
  175. }
  176. *ppv = HeapAlloc (g_hHeap, 0, cb);
  177. if (!*ppv)
  178. {
  179. return E_OUTOFMEMORY;
  180. }
  181. return S_OK;
  182. }
  183. class CNoFaultInject {} NO_FI;
  184. class CThrowOnFail {} THROW_ON_FAIL;
  185. NOTHROW VOID* __cdecl operator new (
  186. size_t cb,
  187. CNoFaultInject &NoFi
  188. ) throw()
  189. {
  190. LPVOID lpv = NULL;
  191. HRESULT hr = HrMallocNoFI (cb, &lpv);
  192. if (SUCCEEDED(hr))
  193. {
  194. return lpv;
  195. }
  196. else
  197. {
  198. return NULL;
  199. }
  200. }
  201. NOTHROW VOID __cdecl operator delete (
  202. void* pv,
  203. CNoFaultInject &NoFi
  204. ) throw()
  205. {
  206. LPVOID lpv = NULL;
  207. MemFree(pv);
  208. }
  209. VOID*
  210. __cdecl
  211. operator new (
  212. size_t cb,
  213. CNoFaultInject &NoFault,
  214. CThrowOnFail &
  215. )
  216. {
  217. LPVOID lpv = NULL;
  218. HRESULT hr = HrMallocNoFI (cb, &lpv);
  219. if (SUCCEEDED(hr))
  220. {
  221. return lpv;
  222. }
  223. else
  224. {
  225. throw std::bad_alloc();
  226. }
  227. }
  228. namespace NOFAULT_ALLOC
  229. {
  230. template<class T> inline
  231. T _FARQ *_NoFaultAllocate(_PDFT nCount, T _FARQ *)
  232. {
  233. if (nCount < 0)
  234. {
  235. nCount = 0;
  236. }
  237. // Call our throwing form of operator new. This will throw a bad_alloc on failure.
  238. return ((T _FARQ *)operator new((_SIZT)nCount * sizeof (T), NO_FI, THROW_ON_FAIL));
  239. }
  240. // TEMPLATE FUNCTION _Construct
  241. // See comments for _Allocate
  242. template<class T1, class T2> inline
  243. void _NoFaultConstruct(T1 _FARQ *p, const T2& v)
  244. {
  245. // Placement new only. No memory allocation, hence no need to throw.
  246. new ((void _FARQ *)p) T1(v);
  247. }
  248. // TEMPLATE FUNCTION _Destroy
  249. // See comments for _Allocate
  250. template<class T>
  251. inline void _NoFaultDestroy(T _FARQ *p)
  252. {
  253. (p)->~T(); // call the destructor
  254. }
  255. // FUNCTION _Destroy
  256. // See comments for _Allocate
  257. inline void _NoFaultDestroy(char _FARQ *p)
  258. {
  259. (void *)p;
  260. }
  261. // FUNCTION _Destroy
  262. // See comments for _Allocate
  263. inline void _NoFaultDestroy(wchar_t _FARQ *p)
  264. {
  265. (void *)p;
  266. }
  267. template<class T>
  268. class nofault_allocator
  269. {
  270. public:
  271. typedef _SIZT size_type;
  272. typedef _PDFT difference_type;
  273. typedef T _FARQ *pointer;
  274. typedef const T _FARQ *const_pointer;
  275. typedef T _FARQ& reference;
  276. typedef const T _FARQ& const_reference;
  277. typedef T value_type;
  278. pointer address(reference x) const
  279. {
  280. return (&x);
  281. }
  282. const_pointer address(const_reference x) const
  283. {
  284. return (&x);
  285. }
  286. pointer allocate(size_type nCount, const void *)
  287. {
  288. return (_NoFaultAllocate((difference_type)nCount, (pointer)0));
  289. }
  290. char _FARQ *_Charalloc(size_type nCount)
  291. {
  292. return (_NoFaultAllocate((difference_type)nCount, (char _FARQ *)0));
  293. }
  294. void deallocate(void _FARQ *p, size_type)
  295. {
  296. operator delete(p);
  297. }
  298. void construct(pointer p, const T& v)
  299. {
  300. _NoFaultConstruct(p, v);
  301. }
  302. void destroy(pointer p)
  303. {
  304. _NoFaultDestroy(p);
  305. }
  306. _SIZT max_size() const
  307. {
  308. _SIZT nCount = (_SIZT)(-1) / sizeof (T);
  309. return (0 < nCount ? nCount : 1);
  310. }
  311. };
  312. template<class T, class U>
  313. bool operator ==(const nofault_allocator<T>&, const nofault_allocator<U>&)
  314. {
  315. return (true);
  316. }
  317. }
  318. //+---------------------------------------------------------------------------
  319. //
  320. // Function: HrUnInitTracing
  321. //
  322. // Purpose: Uninitialize the tracing object.
  323. //
  324. // Arguments:
  325. // (none)
  326. //
  327. // Returns: S_OK or a valid Win32 HRESULT
  328. //
  329. // Author: jeffspr 14 Apr 1997
  330. //
  331. // Notes:
  332. //
  333. HRESULT HrUnInitTracing()
  334. {
  335. return S_OK;
  336. }
  337. const DWORD TI_HRESULT = 0x00000001;
  338. const DWORD TI_WIN32 = 0x00000002;
  339. const DWORD TI_IGNORABLE = 0x00000004;
  340. //+---------------------------------------------------------------------------
  341. //
  342. // Function: TraceInternal
  343. //
  344. // Purpose: The one and only place that a string to be traced is formed
  345. // and traced.
  346. //
  347. // Arguments:
  348. //
  349. // Returns: nothing.
  350. //
  351. // Author: shaunco 13 Mar 1998
  352. //
  353. // Notes: Restructured from lots of other code that was added to this
  354. // module over the past year.
  355. //
  356. VOID
  357. TraceInternal (
  358. TRACETAGID ttid,
  359. PCSTR pszaFile,
  360. INT nLine,
  361. DWORD dwFlags,
  362. PCSTR pszaCallerText,
  363. DWORD dwErrorCode,
  364. BOOL bTraceStackOnError)
  365. {
  366. // If this tracetag is turned off, don't do anything.
  367. //
  368. if (!g_TraceTags[ttid].fOutputDebugString &&
  369. !g_TraceTags[ttid].fOutputToFile)
  370. {
  371. return;
  372. }
  373. BOOL fError = dwFlags & (TI_HRESULT | TI_WIN32);
  374. BOOL fIgnorable = dwFlags & TI_IGNORABLE;
  375. HRESULT hr = (dwFlags & TI_HRESULT) ? dwErrorCode : S_OK;
  376. DWORD dwWin32Error = (dwFlags & TI_WIN32) ? dwErrorCode : ERROR_SUCCESS;
  377. // Ignore if told and we're not set to trace ignored errors or warnings.
  378. //
  379. if (fError && fIgnorable &&
  380. !FIsDebugFlagSet (dfidShowIgnoredErrors) &&
  381. !FIsDebugFlagSet (dfidExtremeTracing))
  382. {
  383. return;
  384. }
  385. // Don't do anything if we're tracing for an error and we don't have one,
  386. // unless the "Extreme Tracing" flag is on, in which case we trace
  387. // everything in the world (for debugger use only, really)
  388. // This is the path taken by TraceError ("...", S_OK) or
  389. // TraceLastWin32Error when there is no last Win32 error.
  390. //
  391. if (fError && !dwErrorCode && !FIsDebugFlagSet(dfidExtremeTracing))
  392. {
  393. return;
  394. }
  395. CHAR *pszaBuf = new(NO_FI) CHAR[MAX_TRACE_LEN * 2];
  396. PSTR pcha = pszaBuf;
  397. // Form the prefix, process id and thread id.
  398. //
  399. static const CHAR c_szaFmtPrefix [] = "NETCFG";
  400. lstrcpyA (pcha, c_szaFmtPrefix);
  401. pcha += lstrlenA (c_szaFmtPrefix);
  402. // Add process and thread ids if the debug flags indicate to do so.
  403. //
  404. if (FIsDebugFlagSet (dfidShowProcessAndThreadIds))
  405. {
  406. static const CHAR c_szaFmtPidAndTid [] = " %03x.%03x";
  407. pcha += _snprintf (pcha, MAX_TRACE_LEN, c_szaFmtPidAndTid,
  408. GetCurrentProcessId (),
  409. GetCurrentThreadId ());
  410. }
  411. // Add a time stamp if the debug flags indicate to do so.
  412. //
  413. if (FIsDebugFlagSet (dfidTracingTimeStamps))
  414. {
  415. static const CHAR c_szaFmtTime [] = " [%02dh%02d:%02d.%03d]";
  416. SYSTEMTIME stLocal;
  417. GetLocalTime (&stLocal);
  418. pcha += _snprintf (pcha, MAX_TRACE_LEN, c_szaFmtTime,
  419. stLocal.wHour,
  420. stLocal.wMinute,
  421. stLocal.wSecond,
  422. stLocal.wMilliseconds);
  423. }
  424. // Add a severity indicator if this trace is for an error or warning.
  425. //
  426. if (fError || (ttidError == ttid))
  427. {
  428. static const CHAR c_szaSevIgnored [] = " Ignored:";
  429. static const CHAR c_szaSevError [] = " *ERROR*:";
  430. static const CHAR c_szaSevWarning [] = " Warning:";
  431. PCSTR pszaSev = NULL;
  432. if (fError && SUCCEEDED(hr) && !dwWin32Error && !fIgnorable)
  433. {
  434. pszaSev = c_szaSevWarning;
  435. }
  436. else
  437. {
  438. if (fIgnorable && FIsDebugFlagSet (dfidShowIgnoredErrors))
  439. {
  440. pszaSev = c_szaSevIgnored;
  441. }
  442. else
  443. {
  444. pszaSev = c_szaSevError;
  445. }
  446. }
  447. Assert (pszaSev);
  448. lstrcatA (pcha, pszaSev);
  449. pcha += lstrlenA (pszaSev);
  450. }
  451. // Add the tracetag short name. Don't do this for ttidError if
  452. // we already have the severity indicator from above.
  453. //
  454. if (ttid && (ttid < g_nTraceTagCount) && (ttid != ttidError))
  455. {
  456. if (FIsDebugFlagSet(dfidTraceMultiLevel))
  457. {
  458. static const CHAR c_szaFmtTraceTag [] = " (%-16s)";
  459. pcha += _snprintf (pcha, MAX_TRACE_LEN, c_szaFmtTraceTag,
  460. g_TraceTags[ttid].szShortName);
  461. }
  462. else
  463. {
  464. static const CHAR c_szaFmtTraceTag [] = " (%s)";
  465. pcha += _snprintf (pcha, MAX_TRACE_LEN, c_szaFmtTraceTag,
  466. g_TraceTags[ttid].szShortName);
  467. }
  468. *pcha = ' ';
  469. pcha++;
  470. if (FIsDebugFlagSet(dfidTraceMultiLevel))
  471. {
  472. // Add the indentation text.
  473. DWORD dwNumSpaces = CTracingIndent::getspaces();
  474. Assert(dwNumSpaces >= 2);
  475. pcha += _snprintf(pcha, MAX_TRACE_LEN, "%1x", dwNumSpaces - 2);
  476. memset(pcha, '-', dwNumSpaces-1 );
  477. pcha += dwNumSpaces-1;
  478. }
  479. }
  480. else
  481. {
  482. *pcha = ' ';
  483. pcha++;
  484. }
  485. // Add the caller's text.
  486. //
  487. if (pszaCallerText)
  488. {
  489. static const CHAR c_szaFmtCallerText [] = "%s";
  490. pcha += _snprintf (pcha, MAX_TRACE_LEN, c_szaFmtCallerText,
  491. pszaCallerText);
  492. Assert (pcha > pszaBuf);
  493. if ('\n' == *(pcha-1))
  494. {
  495. pcha--;
  496. *pcha = 0;
  497. }
  498. }
  499. // Add descriptive error text if this is an error and we can get some.
  500. //
  501. if (FAILED(hr) || dwWin32Error)
  502. {
  503. BOOL fFacilityWin32 = (FACILITY_WIN32 == HRESULT_FACILITY(hr));
  504. // dwError will be the error code we pass to FormatMessage. It may
  505. // come from hr or dwWin32Error. Give preference to hr.
  506. //
  507. DWORD dwError = 0;
  508. if (fFacilityWin32)
  509. {
  510. dwError = HRESULT_CODE(hr);
  511. }
  512. else if (FAILED(hr))
  513. {
  514. dwError = hr;
  515. }
  516. else
  517. {
  518. dwError = dwWin32Error;
  519. }
  520. Assert (dwError);
  521. if (!FIsDebugFlagSet (dfidNoErrorText))
  522. {
  523. PSTR pszaErrorText = NULL;
  524. FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER |
  525. FORMAT_MESSAGE_FROM_SYSTEM,
  526. NULL, dwError,
  527. MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL),
  528. (PSTR)&pszaErrorText, 0, NULL);
  529. if (pszaErrorText)
  530. {
  531. // Strip off newline characters.
  532. //
  533. PSTR pchText = pszaErrorText;
  534. while (*pchText && (*pchText != '\r') && (*pchText != '\n'))
  535. {
  536. pchText++;
  537. }
  538. *pchText = 0;
  539. // Add the error text.
  540. //
  541. static const CHAR c_szaFmtErrorText [] = " [%s]";
  542. pcha += _snprintf (pcha, MAX_TRACE_LEN, c_szaFmtErrorText,
  543. pszaErrorText);
  544. LocalFree (pszaErrorText);
  545. }
  546. }
  547. // Add the Win32 error code.
  548. //
  549. if (fFacilityWin32 || dwWin32Error)
  550. {
  551. static const CHAR c_szaFmtWin32Error [] = " Win32=%d,0x%08X";
  552. pcha += _snprintf (pcha, MAX_TRACE_LEN, c_szaFmtWin32Error,
  553. dwError, dwError);
  554. }
  555. }
  556. // Add the HRESULT.
  557. //
  558. if (S_OK != hr)
  559. {
  560. static const CHAR c_szaFmtHresult [] = " hr=0x%08X";
  561. pcha += _snprintf (pcha, MAX_TRACE_LEN, c_szaFmtHresult,
  562. hr);
  563. }
  564. // Add the file and line.
  565. //
  566. if (pszaFile)
  567. {
  568. static const CHAR c_szaFmtFileAndLine [] = " File:%s,%d";
  569. pcha += _snprintf (pcha, MAX_TRACE_LEN, c_szaFmtFileAndLine,
  570. pszaFile, nLine);
  571. }
  572. // Add the newline.
  573. //
  574. lstrcatA (pcha, "\n");
  575. g_Tracing.Trace (ttid, pszaBuf);
  576. // Now that the message is on the debugger, break if we have an error
  577. // and the debug flag to break on error is set.
  578. //
  579. if ((FAILED(hr) || dwWin32Error || (ttidError == ttid)) &&
  580. !fIgnorable && FIsDebugFlagSet(dfidBreakOnError))
  581. {
  582. DebugBreak();
  583. }
  584. if ( (bTraceStackOnError) && FIsDebugFlagSet(dfidTraceCallStackOnError) && (ttid == ttidError) )
  585. {
  586. CTracingIndent::TraceStackFn(ttidError);
  587. }
  588. delete[] pszaBuf;
  589. }
  590. //+---------------------------------------------------------------------------
  591. //
  592. // Function: TraceInternal
  593. //
  594. // Purpose: The one and only place that a string to be traced is formed
  595. // and traced.
  596. //
  597. // Arguments:
  598. //
  599. // Returns: nothing.
  600. //
  601. // Author: shaunco 13 Mar 1998
  602. //
  603. // Notes: Restructured from lots of other code that was added to this
  604. // module over the past year.
  605. //
  606. VOID
  607. TraceInternal (
  608. TRACETAGID ttid,
  609. PCSTR pszaFile,
  610. INT nLine,
  611. PCSTR pszaFunc,
  612. DWORD dwFlags,
  613. PCSTR pszaCallerText,
  614. DWORD dwErrorCode,
  615. BOOL bTraceStackOnError)
  616. {
  617. // If this tracetag is turned off, don't do anything.
  618. //
  619. if (!g_TraceTags[ttid].fOutputDebugString &&
  620. !g_TraceTags[ttid].fOutputToFile)
  621. {
  622. return;
  623. }
  624. BOOL fError = dwFlags & (TI_HRESULT | TI_WIN32);
  625. BOOL fIgnorable = dwFlags & TI_IGNORABLE;
  626. HRESULT hr = (dwFlags & TI_HRESULT) ? dwErrorCode : S_OK;
  627. DWORD dwWin32Error = (dwFlags & TI_WIN32) ? dwErrorCode : ERROR_SUCCESS;
  628. // Ignore if told and we're not set to trace ignored errors or warnings.
  629. //
  630. if (fError && fIgnorable &&
  631. !FIsDebugFlagSet (dfidShowIgnoredErrors) &&
  632. !FIsDebugFlagSet (dfidExtremeTracing))
  633. {
  634. return;
  635. }
  636. // Don't do anything if we're tracing for an error and we don't have one,
  637. // unless the "Extreme Tracing" flag is on, in which case we trace
  638. // everything in the world (for debugger use only, really)
  639. // This is the path taken by TraceError ("...", S_OK) or
  640. // TraceLastWin32Error when there is no last Win32 error.
  641. //
  642. if (fError && !dwErrorCode && !FIsDebugFlagSet(dfidExtremeTracing))
  643. {
  644. return;
  645. }
  646. CHAR *pszaBuf = new(NO_FI) CHAR[MAX_TRACE_LEN * 2];
  647. PSTR pcha = pszaBuf;
  648. // Form the prefix, process id and thread id.
  649. //
  650. static const CHAR c_szaFmtPrefix [] = "NETCFG";
  651. lstrcpyA (pcha, c_szaFmtPrefix);
  652. pcha += lstrlenA (c_szaFmtPrefix);
  653. // Add process and thread ids if the debug flags indicate to do so.
  654. //
  655. if (FIsDebugFlagSet (dfidShowProcessAndThreadIds))
  656. {
  657. static const CHAR c_szaFmtPidAndTid [] = " %03d.%03d";
  658. pcha += _snprintf (pcha, MAX_TRACE_LEN, c_szaFmtPidAndTid,
  659. GetCurrentProcessId (),
  660. GetCurrentThreadId ());
  661. }
  662. // Add a time stamp if the debug flags indicate to do so.
  663. //
  664. if (FIsDebugFlagSet (dfidTracingTimeStamps))
  665. {
  666. static const CHAR c_szaFmtTime [] = " [%02d:%02d:%02d.%03d]";
  667. SYSTEMTIME stLocal;
  668. GetLocalTime (&stLocal);
  669. pcha += _snprintf (pcha, MAX_TRACE_LEN, c_szaFmtTime,
  670. stLocal.wHour,
  671. stLocal.wMinute,
  672. stLocal.wSecond,
  673. stLocal.wMilliseconds);
  674. }
  675. // Add a severity indicator if this trace is for an error or warning.
  676. //
  677. if (fError || (ttidError == ttid))
  678. {
  679. static const CHAR c_szaSevIgnored [] = " Ignored:";
  680. static const CHAR c_szaSevError [] = " *ERROR*:";
  681. static const CHAR c_szaSevWarning [] = " Warning:";
  682. PCSTR pszaSev = NULL;
  683. if (fError && SUCCEEDED(hr) && !dwWin32Error && !fIgnorable)
  684. {
  685. pszaSev = c_szaSevWarning;
  686. }
  687. else
  688. {
  689. if (fIgnorable && FIsDebugFlagSet (dfidShowIgnoredErrors))
  690. {
  691. pszaSev = c_szaSevIgnored;
  692. }
  693. else
  694. {
  695. pszaSev = c_szaSevError;
  696. }
  697. }
  698. Assert (pszaSev);
  699. lstrcatA (pcha, pszaSev);
  700. pcha += lstrlenA (pszaSev);
  701. }
  702. // Add the tracetag short name. Don't do this for ttidError if
  703. // we already have the severity indicator from above.
  704. //
  705. if (ttid && (ttid < g_nTraceTagCount) && (ttid != ttidError))
  706. {
  707. static const CHAR c_szaFmtTraceTag [] = " (%s)";
  708. pcha += _snprintf (pcha, MAX_TRACE_LEN, c_szaFmtTraceTag,
  709. g_TraceTags[ttid].szShortName);
  710. }
  711. // Add the caller's text.
  712. //
  713. if (pszaCallerText)
  714. {
  715. static const CHAR c_szaFmtCallerText [] = " %s";
  716. pcha += _snprintf (pcha, MAX_TRACE_LEN, c_szaFmtCallerText,
  717. pszaCallerText);
  718. Assert (pcha > pszaBuf);
  719. if ('\n' == *(pcha-1))
  720. {
  721. pcha--;
  722. *pcha = 0;
  723. }
  724. }
  725. // Add descriptive error text if this is an error and we can get some.
  726. //
  727. if (FAILED(hr) || dwWin32Error)
  728. {
  729. BOOL fFacilityWin32 = (FACILITY_WIN32 == HRESULT_FACILITY(hr));
  730. // dwError will be the error code we pass to FormatMessage. It may
  731. // come from hr or dwWin32Error. Give preference to hr.
  732. //
  733. DWORD dwError = 0;
  734. if (fFacilityWin32)
  735. {
  736. dwError = HRESULT_CODE(hr);
  737. }
  738. else if (FAILED(hr))
  739. {
  740. dwError = hr;
  741. }
  742. else
  743. {
  744. dwError = dwWin32Error;
  745. }
  746. Assert (dwError);
  747. if (!FIsDebugFlagSet (dfidNoErrorText))
  748. {
  749. PSTR pszaErrorText = NULL;
  750. FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER |
  751. FORMAT_MESSAGE_FROM_SYSTEM,
  752. NULL, dwError,
  753. MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL),
  754. (PSTR)&pszaErrorText, 0, NULL);
  755. if (pszaErrorText)
  756. {
  757. // Strip off newline characters.
  758. //
  759. PSTR pchText = pszaErrorText;
  760. while (*pchText && (*pchText != '\r') && (*pchText != '\n'))
  761. {
  762. pchText++;
  763. }
  764. *pchText = 0;
  765. // Add the error text.
  766. //
  767. static const CHAR c_szaFmtErrorText [] = " [%s]";
  768. pcha += _snprintf (pcha, MAX_TRACE_LEN, c_szaFmtErrorText,
  769. pszaErrorText);
  770. LocalFree (pszaErrorText);
  771. }
  772. }
  773. // Add the Win32 error code.
  774. //
  775. if (fFacilityWin32 || dwWin32Error)
  776. {
  777. static const CHAR c_szaFmtWin32Error [] = " Win32=%d,0x%08X";
  778. pcha += _snprintf (pcha, MAX_TRACE_LEN, c_szaFmtWin32Error,
  779. dwError, dwError);
  780. }
  781. }
  782. // Add the HRESULT.
  783. //
  784. if (S_OK != hr)
  785. {
  786. static const CHAR c_szaFmtHresult [] = " hr=0x%08X";
  787. pcha += _snprintf (pcha, MAX_TRACE_LEN, c_szaFmtHresult,
  788. hr);
  789. }
  790. // Add the file and line.
  791. //
  792. if (pszaFile)
  793. {
  794. static const CHAR c_szaFmtFileAndLine [] = " File:%s,%d";
  795. pcha += _snprintf (pcha, MAX_TRACE_LEN, c_szaFmtFileAndLine,
  796. pszaFile, nLine);
  797. }
  798. // Add the function name
  799. //
  800. if (pszaFunc)
  801. {
  802. static const CHAR c_szaFmtFunc[] = ":";
  803. pcha += _snprintf (pcha, MAX_TRACE_LEN, c_szaFmtFunc, pszaFunc);
  804. }
  805. // Add the newline.
  806. //
  807. lstrcatA (pcha, "\n");
  808. g_Tracing.Trace (ttid, pszaBuf);
  809. // Now that the message is on the debugger, break if we have an error
  810. // and the debug flag to break on error is set.
  811. //
  812. if ((FAILED(hr) || dwWin32Error || (ttidError == ttid)) &&
  813. !fIgnorable && FIsDebugFlagSet(dfidBreakOnError))
  814. {
  815. DebugBreak();
  816. }
  817. if ( (bTraceStackOnError) && FIsDebugFlagSet(dfidTraceCallStackOnError) && (ttid == ttidError) )
  818. {
  819. CTracingIndent::TraceStackFn(ttidError);
  820. }
  821. delete[] pszaBuf;
  822. }
  823. //+---------------------------------------------------------------------------
  824. //
  825. // Function: TraceErrorFn
  826. //
  827. // Purpose: Output debug trace of an HRESULT, allowing an additional
  828. // caller-defined error string.
  829. //
  830. // Arguments:
  831. // sz [] Caller-defined additional error text
  832. // hr [] The error HRESULT.
  833. //
  834. // Returns:
  835. //
  836. // Author: jeffspr 14 Apr 1997
  837. //
  838. // Notes:
  839. //
  840. VOID
  841. WINAPI
  842. TraceErrorFn (
  843. PCSTR pszaFile,
  844. INT nLine,
  845. PCSTR psza,
  846. HRESULT hr)
  847. {
  848. DWORD dwFlags = TI_HRESULT;
  849. if (S_FALSE == hr)
  850. {
  851. dwFlags |= TI_IGNORABLE;
  852. }
  853. TraceInternal (ttidError, pszaFile, nLine, dwFlags, psza, hr, TRUE);
  854. }
  855. //+---------------------------------------------------------------------------
  856. //
  857. // Function: TraceErrorOptionalFn
  858. //
  859. // Purpose: Implements TraceErrorOptional macro
  860. //
  861. // Arguments:
  862. // pszaFile [in] __FILE__ value
  863. // nLine [in] __LINE__ value
  864. // psza [in] String to trace.
  865. // hr [in] HRESULT value to trace.
  866. // fOpt [in] TRUE if error should be treated as optional, FALSE if
  867. // ERROR is not optional and should be reported thru
  868. // TraceError().
  869. //
  870. // Returns: Nothing.
  871. //
  872. // Author: danielwe 12 May 1997
  873. //
  874. // Notes:
  875. //
  876. VOID
  877. WINAPI
  878. TraceErrorOptionalFn (
  879. PCSTR pszaFile,
  880. INT nLine,
  881. PCSTR psza,
  882. HRESULT hr,
  883. BOOL fIgnorable)
  884. {
  885. DWORD dwFlags = TI_HRESULT;
  886. if (fIgnorable)
  887. {
  888. dwFlags |= TI_IGNORABLE;
  889. }
  890. TraceInternal (ttidError, pszaFile, nLine, dwFlags, psza, hr, TRUE);
  891. }
  892. //+---------------------------------------------------------------------------
  893. //
  894. // Function: TraceErrorSkipFn
  895. //
  896. // Purpose: Implements TraceErrorOptional macro
  897. //
  898. // Arguments:
  899. // pszaFile [in] __FILE__ value
  900. // nLine [in] __LINE__ value
  901. // psza [in] String to trace.
  902. // hr [in] HRESULT value to trace.
  903. // c [in] count of pass-through Hresults. if hr is any of these
  904. // the error is treated as optional.
  905. // ... [in] list of hresults.
  906. //
  907. // Returns: Nothing.
  908. //
  909. // Author: sumitc 08 Jan 1998
  910. //
  911. // Notes:
  912. //
  913. VOID WINAPI
  914. TraceErrorSkipFn (
  915. PCSTR pszaFile,
  916. INT nLine,
  917. PCSTR psza,
  918. HRESULT hr,
  919. UINT c, ...)
  920. {
  921. va_list valMarker;
  922. BOOL fIgnorable = FALSE;
  923. va_start(valMarker, c);
  924. for (UINT i = 0; i < c; ++i)
  925. {
  926. fIgnorable = (va_arg(valMarker, HRESULT) == hr);
  927. if (fIgnorable)
  928. {
  929. break;
  930. }
  931. }
  932. va_end(valMarker);
  933. DWORD dwFlags = TI_HRESULT;
  934. if (fIgnorable)
  935. {
  936. dwFlags |= TI_IGNORABLE;
  937. }
  938. TraceInternal (ttidError, pszaFile, nLine, dwFlags, psza, hr, TRUE);
  939. }
  940. //+---------------------------------------------------------------------------
  941. //
  942. // Function: TraceLastWin32ErrorFn
  943. //
  944. // Purpose: Trace the last Win32 error, which we get with GetLastError().
  945. // Not a whole lot to it.
  946. //
  947. // Arguments:
  948. // sz [] Additional error text.
  949. //
  950. // Returns:
  951. //
  952. // Author: jeffspr 14 Apr 1997
  953. //
  954. // Notes:
  955. //
  956. VOID
  957. WINAPIV
  958. TraceLastWin32ErrorFn (
  959. PCSTR pszaFile,
  960. INT nLine,
  961. PCSTR psza)
  962. {
  963. TraceInternal (ttidError, pszaFile, nLine, TI_WIN32, psza, GetLastError(), TRUE);
  964. }
  965. //+---------------------------------------------------------------------------
  966. //
  967. // Function: TraceHrFn
  968. //
  969. // Purpose: Generic replacement for the TraceErrorOptional, TraceError,
  970. // and a couple other random functions.
  971. //
  972. // Arguments:
  973. // ttid [] TraceTag to use for the debug output
  974. // pszaFile [] Source file to log
  975. // nLine [] Line number to log
  976. // hr [] Error to log
  977. // fIgnorable [] Ignore this error? (The optional bit)
  978. // pszaFmt [] Format of the vargs
  979. //
  980. // Returns:
  981. //
  982. // Author: jeffspr 10 Oct 1997
  983. //
  984. // Notes:
  985. //
  986. VOID
  987. WINAPIV
  988. TraceHrFn (
  989. TRACETAGID ttid,
  990. PCSTR pszaFile,
  991. INT nLine,
  992. HRESULT hr,
  993. BOOL fIgnorable,
  994. PCSTR pszaFmt,
  995. ...)
  996. {
  997. // If this tracetag is turned off, don't do anything.
  998. //
  999. if (!g_TraceTags[ttid].fOutputDebugString &&
  1000. !g_TraceTags[ttid].fOutputToFile)
  1001. {
  1002. return;
  1003. }
  1004. CHAR *pszaBuf = new(NO_FI) CHAR[MAX_TRACE_LEN];
  1005. // Build the string from the varg list
  1006. //
  1007. va_list valMarker;
  1008. va_start (valMarker, pszaFmt);
  1009. vsprintf (pszaBuf, pszaFmt, valMarker);
  1010. va_end (valMarker);
  1011. DWORD dwFlags = TI_HRESULT;
  1012. if (fIgnorable)
  1013. {
  1014. dwFlags |= TI_IGNORABLE;
  1015. }
  1016. TraceInternal (ttid, pszaFile, nLine, dwFlags, pszaBuf, hr, TRUE);
  1017. delete[] pszaBuf;
  1018. }
  1019. //+---------------------------------------------------------------------------
  1020. //
  1021. // Function: TraceHrFn
  1022. //
  1023. // Purpose: Generic replacement for the TraceErrorOptional, TraceError,
  1024. // and a couple other random functions.
  1025. //
  1026. // Arguments:
  1027. // ttid [] TraceTag to use for the debug output
  1028. // pszaFile [] Source file to log
  1029. // nLine [] Line number to log
  1030. // hr [] Error to log
  1031. // fIgnorable [] Ignore this error? (The optional bit)
  1032. // pszaFmt [] Format of the vargs
  1033. //
  1034. // Returns:
  1035. //
  1036. // Author: jeffspr 10 Oct 1997
  1037. //
  1038. // Notes:
  1039. //
  1040. VOID
  1041. WINAPIV
  1042. TraceHrFn (
  1043. TRACETAGID ttid,
  1044. PCSTR pszaFile,
  1045. INT nLine,
  1046. PCSTR pszaFunc,
  1047. HRESULT hr,
  1048. BOOL fIgnorable,
  1049. PCSTR pszaFmt,
  1050. ...)
  1051. {
  1052. // If this tracetag is turned off, don't do anything.
  1053. //
  1054. if (!g_TraceTags[ttid].fOutputDebugString &&
  1055. !g_TraceTags[ttid].fOutputToFile)
  1056. {
  1057. return;
  1058. }
  1059. CHAR *pszaBuf = new(NO_FI) CHAR[MAX_TRACE_LEN];
  1060. // Build the string from the varg list
  1061. //
  1062. va_list valMarker;
  1063. va_start (valMarker, pszaFmt);
  1064. _vsnprintf (pszaBuf, MAX_TRACE_LEN, pszaFmt, valMarker);
  1065. va_end (valMarker);
  1066. DWORD dwFlags = TI_HRESULT;
  1067. if (fIgnorable)
  1068. {
  1069. dwFlags |= TI_IGNORABLE;
  1070. }
  1071. TraceInternal (ttid, pszaFile, nLine, pszaFunc, dwFlags, pszaBuf, hr, TRUE);
  1072. delete[] pszaBuf;
  1073. }
  1074. //+---------------------------------------------------------------------------
  1075. //
  1076. // Function: TraceTagFn
  1077. //
  1078. // Purpose: Output a debug trace to one or more trace targets (ODS,
  1079. // File, COM port, etc.). This function determines the targets
  1080. // and performs the actual trace.
  1081. //
  1082. // Arguments:
  1083. // ttid [] TraceTag to use for the debug output
  1084. // pszaFmt [] Format of the vargs.
  1085. //
  1086. // Returns:
  1087. //
  1088. // Author: jeffspr 14 Apr 1997
  1089. //
  1090. // Notes:
  1091. //
  1092. VOID
  1093. WINAPIV
  1094. TraceTagFn (
  1095. TRACETAGID ttid,
  1096. PCSTR pszaFmt,
  1097. ...)
  1098. {
  1099. // If this tracetag is turned off, don't do anything.
  1100. //
  1101. if (!g_TraceTags[ttid].fOutputDebugString &&
  1102. !g_TraceTags[ttid].fOutputToFile)
  1103. {
  1104. return;
  1105. }
  1106. CHAR *pszaBuf = new(NO_FI) CHAR[MAX_TRACE_LEN];
  1107. // Build the string from the varg list
  1108. //
  1109. va_list valMarker;
  1110. va_start (valMarker, pszaFmt);
  1111. _vsnprintf (pszaBuf, MAX_TRACE_LEN, pszaFmt, valMarker);
  1112. va_end (valMarker);
  1113. TraceInternal (ttid, NULL, 0, 0, pszaBuf, 0, TRUE);
  1114. delete[] pszaBuf;
  1115. }
  1116. //+---------------------------------------------------------------------------
  1117. //
  1118. // Member: CTracing::CTracing
  1119. //
  1120. // Purpose: Constructor for CTracing. Initialize all vars.
  1121. //
  1122. // Arguments:
  1123. // (none)
  1124. //
  1125. // Returns:
  1126. //
  1127. // Author: jeffspr 23 Jan 1999
  1128. //
  1129. // Notes:
  1130. //
  1131. CTracing::CTracing()
  1132. {
  1133. m_fInitialized = FALSE; // Has the object been initialized
  1134. m_fAttemptedLogFileOpen = FALSE; // Already attempted to open log
  1135. m_fDisableLogFile = FALSE; // Disable use of file logging?
  1136. m_uiAllocOnWhichToBreak = 0; // For _CrtSetBreakAlloc
  1137. m_hLogFile = NULL; // Handle for debug output file
  1138. m_szLogFilePath[0] = '\0'; // File for debug output
  1139. m_fDebugFlagsLoaded = FALSE; // Have these been loaded yet.
  1140. g_dwTlsTracing = NULL;
  1141. HrInit();
  1142. }
  1143. CTracing::~CTracing()
  1144. {
  1145. HrUnInit();
  1146. }
  1147. //+---------------------------------------------------------------------------
  1148. //
  1149. // Member: CTracing::HrInit
  1150. //
  1151. // Purpose: Initialize the CTracing object.
  1152. //
  1153. // Arguments:
  1154. // (none)
  1155. //
  1156. // Returns: S_OK or valid Win32 HRESULT
  1157. //
  1158. // Author: jeffspr 9 Apr 1997
  1159. //
  1160. // Notes: This should get called from some standard exe initialization
  1161. // point. And make sure to call HrDeinit when you're done, eh?
  1162. //
  1163. HRESULT CTracing::HrInit()
  1164. {
  1165. HRESULT hr = S_OK;
  1166. AssertSz(!m_fInitialized,
  1167. "CTracing::HrInit -- Let's not go overboard. Already initialized");
  1168. g_csTracing = new(NO_FI) CRITICAL_SECTION;
  1169. Assert(g_csTracing);
  1170. InitializeCriticalSection(g_csTracing);
  1171. g_dwTlsTracing = TlsAlloc();
  1172. AssertSz(g_dwTlsTracing, "g_dwTlsTracing could not aquire a TLS slot");
  1173. hr = FIniFileInit();
  1174. if (FAILED(hr))
  1175. {
  1176. goto Exit;
  1177. }
  1178. // Temporarily set this so the called functions don't believe that we're
  1179. // uninitialized. At Exit, if we fail, we'll set it back so no-one tries
  1180. // to call these functions when uninitialized.
  1181. //
  1182. m_fInitialized = TRUE;
  1183. // Check for corruptions in the tracing structure. This can't fail, but
  1184. // it will send up asserts all over the place if something is amiss.
  1185. //
  1186. CorruptionCheck();
  1187. // Load the "options" section from the ini file
  1188. //
  1189. hr = HrLoadOptionsFromIniFile();
  1190. if (FAILED(hr))
  1191. {
  1192. goto Exit;
  1193. }
  1194. // Load the DebugFlags section from the ini file.
  1195. //
  1196. hr = HrLoadDebugFlagsFromIniFile();
  1197. if (FAILED(hr))
  1198. {
  1199. goto Exit;
  1200. }
  1201. // Load the tracetag sections from the ini file.
  1202. // Make sure this is called after HrLoadDebugFlagsFromIniFile(),
  1203. // as those options will affect the tracetag sections (we also
  1204. // assert on this)
  1205. //
  1206. hr = HrLoadSectionsFromIniFile();
  1207. if (FAILED(hr))
  1208. {
  1209. goto Exit;
  1210. }
  1211. // If certain tracetags are on, we want others to be off because some
  1212. // encompase the functionality of others.
  1213. //
  1214. if (g_TraceTags[ttidBeDiag].fOutputDebugString)
  1215. {
  1216. g_TraceTags[ttidNetCfgPnp].fOutputDebugString = FALSE;
  1217. }
  1218. #ifdef ENABLELEAKDETECTION
  1219. if (FIsDebugFlagSet(dfidTrackObjectLeaks))
  1220. {
  1221. g_pObjectLeakTrack = new(NO_FI) CObjectLeakTrack;
  1222. Assert(g_pObjectLeakTrack);
  1223. }
  1224. #endif
  1225. Exit:
  1226. if (FAILED(hr))
  1227. {
  1228. m_fInitialized = FALSE;
  1229. }
  1230. return hr;
  1231. }
  1232. //+---------------------------------------------------------------------------
  1233. //
  1234. // Member: CTracing::HrUnInit
  1235. //
  1236. // Purpose: Uninitialize the Tracing object
  1237. //
  1238. // Arguments:
  1239. // (none)
  1240. //
  1241. // Returns: S_OK or valid Win32 HRESULT
  1242. //
  1243. // Author: jeffspr 12 Apr 1997
  1244. //
  1245. // Notes:
  1246. //
  1247. HRESULT CTracing::HrUnInit()
  1248. {
  1249. HRESULT hr = S_OK;
  1250. #ifdef ENABLELEAKDETECTION
  1251. if (FIsDebugFlagSet(dfidTrackObjectLeaks))
  1252. {
  1253. BOOL fAsserted = g_pObjectLeakTrack->AssertIfObjectsStillAllocated(NULL);
  1254. if (fAsserted)
  1255. {
  1256. // AssertSz(FALSE, "Spew is completed - press RETRY to look at spew and map ReturnAddr values to symbols");
  1257. }
  1258. delete g_pObjectLeakTrack;
  1259. }
  1260. #endif
  1261. if (g_dwTlsTracing)
  1262. {
  1263. TlsFree(g_dwTlsTracing);
  1264. g_dwTlsTracing = 0;
  1265. }
  1266. if (g_csTracing)
  1267. {
  1268. {
  1269. __try
  1270. {
  1271. EnterCriticalSection(g_csTracing);
  1272. }
  1273. __finally
  1274. {
  1275. LeaveCriticalSection(g_csTracing);
  1276. }
  1277. }
  1278. DeleteCriticalSection(g_csTracing);
  1279. delete g_csTracing;
  1280. g_csTracing = NULL;
  1281. }
  1282. // Don't assert on m_fInitialized here, because we allow this to
  1283. // be called even if initialization failed.
  1284. //
  1285. if (m_fInitialized)
  1286. {
  1287. hr = HrWriteDebugFlagsToIniFile();
  1288. if (FAILED(hr))
  1289. {
  1290. // continue on, but I want to know why this is failing.
  1291. AssertSz(FALSE, "Whoa, why can't we write the debug flags?");
  1292. }
  1293. // Close the log file, if there's one open
  1294. //
  1295. if (m_hLogFile)
  1296. {
  1297. CloseHandle(m_hLogFile);
  1298. m_hLogFile = NULL;
  1299. }
  1300. // Mark us as being uninitialized.
  1301. //
  1302. m_fInitialized = FALSE;
  1303. }
  1304. return hr;
  1305. }
  1306. //+---------------------------------------------------------------------------
  1307. //
  1308. // Member: CTracing::HrGetPrivateProfileString
  1309. //
  1310. // Purpose:
  1311. //
  1312. // Arguments:
  1313. // lpAppName [] points to section name
  1314. // lpKeyName [] points to key name
  1315. // lpDefault [] points to default string
  1316. // lpReturnedString [] points to destination buffer
  1317. // nSize [] size of destination buffer
  1318. // lpFileName [] points to initialization filename
  1319. // pcchReturn return buffer for the old Win32 API return code
  1320. //
  1321. //
  1322. // Returns: S_OK or valid Win32 HRESULT
  1323. //
  1324. // Author: jeffspr 12 Apr 1997
  1325. //
  1326. // Notes:
  1327. //
  1328. HRESULT CTracing::HrGetPrivateProfileString( PCSTR lpAppName,
  1329. PCSTR lpKeyName,
  1330. PCSTR lpDefault,
  1331. PSTR lpReturnedString,
  1332. DWORD nSize,
  1333. PCSTR lpFileName,
  1334. DWORD* pcchReturn
  1335. )
  1336. {
  1337. HRESULT hr = S_OK;
  1338. Assert(m_fInitialized);
  1339. // Assert on the known conditions required for this API call
  1340. //
  1341. Assert(lpDefault);
  1342. Assert(lpFileName);
  1343. // Call the Win32 API
  1344. //
  1345. DWORD dwGPPSResult = GetPrivateProfileStringA(
  1346. lpAppName,
  1347. lpKeyName,
  1348. lpDefault,
  1349. lpReturnedString,
  1350. nSize,
  1351. lpFileName);
  1352. // Check to see if we've gotten a string-size error
  1353. if (lpAppName && lpKeyName)
  1354. {
  1355. // If we get back (nSize - 1), then our string buffer wasn't
  1356. // large enough
  1357. //
  1358. if (dwGPPSResult == (nSize - 1))
  1359. {
  1360. hr = HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER);
  1361. goto Exit;
  1362. }
  1363. }
  1364. else
  1365. {
  1366. // Since either of the app name or key name are NULL, then
  1367. // we're supposed to be receiving a doubly-NULL terminated
  1368. // list of strings. If we're at (nSize - 2), that means
  1369. // our buffer was too small.
  1370. //
  1371. if (dwGPPSResult == (nSize - 2))
  1372. {
  1373. hr = HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER);
  1374. goto Exit;
  1375. }
  1376. }
  1377. Exit:
  1378. *pcchReturn = dwGPPSResult;
  1379. return hr;
  1380. }
  1381. //+---------------------------------------------------------------------------
  1382. //
  1383. // Member: CTracing::HrLoadOptionsFromIniFile
  1384. //
  1385. // Purpose: Load the options section from the ini file, and set our
  1386. // state accordingly
  1387. //
  1388. // Arguments:
  1389. // (none)
  1390. //
  1391. // Returns: S_OK or valid Win32 HRESULT
  1392. //
  1393. // Author: jeffspr 10 Apr 1997
  1394. //
  1395. // Notes:
  1396. //
  1397. HRESULT CTracing::HrLoadOptionsFromIniFile()
  1398. {
  1399. HRESULT hr = S_OK;
  1400. DWORD cchReturnBufferSize = 0;
  1401. WCHAR szLogFilePath[MAX_PATH+1] = { 0 };
  1402. DWORD dwTempPathLength = 0;
  1403. // Get the explicit log file path, if any. If it doesn't exist, then
  1404. // use the default path, which is the temp file path plus the default
  1405. // trace file name
  1406. //
  1407. // Get the location of the "temporary files" path
  1408. dwTempPathLength = GetTempPath(MAX_PATH, szLogFilePath);
  1409. if ((dwTempPathLength == 0) ||
  1410. (dwTempPathLength > MAX_PATH))
  1411. {
  1412. TraceLastWin32Error("GetTempPath failure");
  1413. hr = HrFromLastWin32Error();
  1414. goto Exit;
  1415. }
  1416. // Tack the log file name onto the end.
  1417. //
  1418. _snprintf(m_szLogFilePath, MAX_TRACE_LEN, "%s%s", szLogFilePath, c_szTraceLogFileNameA);
  1419. // This will overwrite the log file path if one exists in the INI file
  1420. //
  1421. hr = HrGetPrivateProfileString(
  1422. c_szaOptions, // "Options"
  1423. c_szaLogFilePath, // "LogFilePath
  1424. m_szLogFilePath, // Default string, already filled
  1425. m_szLogFilePath, // Return string (same string)
  1426. MAX_PATH+1,
  1427. c_szDebugIniFileNameA,
  1428. &cchReturnBufferSize);
  1429. if (FAILED(hr))
  1430. {
  1431. // This should not cause problems with recursive failure, since
  1432. // Traces will work regardless of the state of trace initialization.
  1433. //
  1434. TraceError(
  1435. "GetPrivateProfileString failed on Options::LogFilePath", hr);
  1436. goto Exit;
  1437. }
  1438. // Get the "disable log file option". No return code here.
  1439. m_fDisableLogFile = GetPrivateProfileIntA(
  1440. c_szaOptions, // "Options"
  1441. c_szaDisableLogFile, // "DisableLogFile"
  1442. c_iDefaultDisableLogFile,
  1443. c_szDebugIniFileNameA);
  1444. if (FAILED(hr))
  1445. {
  1446. TraceError(
  1447. "GetPrivateProfileInt failed on Options::DisableLogFile", hr);
  1448. goto Exit;
  1449. }
  1450. Exit:
  1451. return hr;
  1452. }
  1453. //+---------------------------------------------------------------------------
  1454. //
  1455. // Member: CTracing::HrLoadSectionsFromIniFile
  1456. //
  1457. // Purpose: Load the individual tracetag sections from the ini file, and
  1458. // set our array elements accordingly, defaulting if necessary.
  1459. //
  1460. // Arguments:
  1461. // (none)
  1462. //
  1463. // Returns: S_OK or valid Win32 HRESULT
  1464. //
  1465. // Author: jeffspr 10 Apr 1997
  1466. //
  1467. // Notes:
  1468. //
  1469. HRESULT CTracing::HrLoadSectionsFromIniFile()
  1470. {
  1471. HRESULT hr = S_OK;
  1472. // Make sure that we've loaded the debug flags first, as they can
  1473. // affect each tracetag section
  1474. //
  1475. Assert(m_fDebugFlagsLoaded);
  1476. // Loop through the array and load the data.
  1477. //
  1478. for (INT nLoop = 0; nLoop < g_nTraceTagCount; nLoop++ )
  1479. {
  1480. // Process the individual lines from the section
  1481. hr = HrProcessTagSection(&(g_TraceTags[nLoop]));
  1482. if (FAILED(hr))
  1483. {
  1484. break;
  1485. }
  1486. }
  1487. return hr;
  1488. }
  1489. //+---------------------------------------------------------------------------
  1490. //
  1491. // Member: CTracing::HrLoadDebugFlagsFromIniFile
  1492. //
  1493. // Purpose: Load the individual debugflag values from the ini file, and
  1494. // set our array elements accordingly, defaulting if necessary.
  1495. //
  1496. // Arguments:
  1497. // (none)
  1498. //
  1499. // Returns: S_OK or valid Win32 HRESULT
  1500. //
  1501. // Author: jeffspr 10 Apr 1997
  1502. //
  1503. // Notes:
  1504. //
  1505. HRESULT CTracing::HrLoadDebugFlagsFromIniFile()
  1506. {
  1507. HRESULT hr = S_OK;
  1508. INT nLoop;
  1509. // Loop through the array and load the data.
  1510. //
  1511. for (nLoop = 0; nLoop < g_nDebugFlagCount; nLoop++)
  1512. {
  1513. switch(nLoop)
  1514. {
  1515. case dfidBreakOnAlloc:
  1516. // Get the "break on alloc" alloc count.
  1517. //
  1518. m_uiAllocOnWhichToBreak = GetPrivateProfileIntA(
  1519. "DebugFlags",
  1520. g_DebugFlags[nLoop].szShortName,
  1521. FALSE,
  1522. c_szDebugIniFileNameA);
  1523. g_DebugFlags[nLoop].dwValue = (m_uiAllocOnWhichToBreak > 0);
  1524. // If there was a value set, set the break..
  1525. //
  1526. if (m_uiAllocOnWhichToBreak != 0)
  1527. _CrtSetBreakAlloc(m_uiAllocOnWhichToBreak);
  1528. break;
  1529. default:
  1530. // Get the enabled file param
  1531. //
  1532. g_DebugFlags[nLoop].dwValue = GetPrivateProfileIntA(
  1533. "DebugFlags",
  1534. g_DebugFlags[nLoop].szShortName,
  1535. FALSE,
  1536. c_szDebugIniFileNameA);
  1537. break;
  1538. }
  1539. }
  1540. if (SUCCEEDED(hr))
  1541. {
  1542. m_fDebugFlagsLoaded = TRUE;
  1543. }
  1544. return hr;
  1545. }
  1546. HRESULT CTracing::FIniFileInit()
  1547. {
  1548. HRESULT hr = E_FAIL;
  1549. WCHAR szWindowsPath[MAX_PATH+1] = L"";
  1550. WCHAR szPath[MAX_PATH+1] = L"";
  1551. UINT uiCharsReturned = 0;
  1552. HANDLE hFile = INVALID_HANDLE_VALUE;
  1553. uiCharsReturned = GetWindowsDirectory(szWindowsPath, MAX_PATH);
  1554. if ((uiCharsReturned == 0) || (uiCharsReturned > MAX_PATH))
  1555. {
  1556. AssertSz(FALSE, "GetWindowsDirectory failed in CTracing::FIniFileInit");
  1557. hr = E_UNEXPECTED;
  1558. goto Exit;
  1559. }
  1560. wcscpy (szPath, szWindowsPath);
  1561. wcscat (szPath, L"\\");
  1562. wcscat (szPath, c_szDebugIniFileName);
  1563. hFile = CreateFile(
  1564. szPath,
  1565. GENERIC_READ,
  1566. FILE_SHARE_READ | FILE_SHARE_WRITE,
  1567. NULL,
  1568. OPEN_EXISTING,
  1569. FILE_ATTRIBUTE_NORMAL,
  1570. NULL);
  1571. if (hFile == INVALID_HANDLE_VALUE)
  1572. {
  1573. DWORD dwLastError = GetLastError();
  1574. if (dwLastError != ERROR_FILE_NOT_FOUND)
  1575. {
  1576. AssertSz(FALSE, "FIniFileInit failed for some reason other than FILE_NOT_FOUND");
  1577. hr = HRESULT_FROM_WIN32(dwLastError);
  1578. goto Exit;
  1579. }
  1580. }
  1581. else
  1582. {
  1583. hr = S_OK;
  1584. wcstombs(c_szDebugIniFileNameA, szPath, MAX_PATH);
  1585. goto Exit;
  1586. }
  1587. _wsplitpath(szWindowsPath, szPath, NULL, NULL, NULL);
  1588. wcscat (szPath, L"\\");
  1589. wcscat (szPath, c_szDebugIniFileName);
  1590. hFile = CreateFile(
  1591. szPath,
  1592. GENERIC_READ,
  1593. FILE_SHARE_READ | FILE_SHARE_WRITE,
  1594. NULL,
  1595. OPEN_EXISTING,
  1596. FILE_ATTRIBUTE_NORMAL,
  1597. NULL);
  1598. if (hFile == INVALID_HANDLE_VALUE)
  1599. {
  1600. DWORD dwLastError = GetLastError();
  1601. hr = HRESULT_FROM_WIN32(dwLastError);
  1602. if (dwLastError != ERROR_FILE_NOT_FOUND)
  1603. {
  1604. AssertSz(FALSE, "FIniFileInit failed for some reason other than FILE_NOT_FOUND");
  1605. }
  1606. }
  1607. else
  1608. {
  1609. wcstombs(c_szDebugIniFileNameA, szPath, MAX_PATH);
  1610. hr = S_OK;
  1611. }
  1612. Exit:
  1613. if (hFile)
  1614. {
  1615. CloseHandle(hFile);
  1616. hFile = NULL;
  1617. }
  1618. return hr;
  1619. }
  1620. HRESULT CTracing::HrWriteDebugFlagsToIniFile()
  1621. {
  1622. HRESULT hr = S_OK;
  1623. // First, check to see if the file exists. If it doesn't, then we don't want
  1624. // to write the entries.
  1625. //
  1626. if (FIniFileInit())
  1627. {
  1628. // Loop through the array and write the data.
  1629. //
  1630. for (INT nLoop = 0; nLoop < g_nDebugFlagCount; nLoop++)
  1631. {
  1632. CHAR szInt[16]; // Sure, it's arbitrary, but it's also OK.
  1633. switch(nLoop)
  1634. {
  1635. // BreakOnAlloc is special case -- no associated flag entry
  1636. //
  1637. case dfidBreakOnAlloc:
  1638. _snprintf(szInt, MAX_TRACE_LEN, "%d", m_uiAllocOnWhichToBreak);
  1639. break;
  1640. // These store a DWORD in its standard form
  1641. //
  1642. case dfidBreakOnHr:
  1643. case dfidBreakOnHrIteration:
  1644. case dfidBreakOnIteration:
  1645. _snprintf( szInt, MAX_TRACE_LEN, "%d", g_DebugFlags[nLoop].dwValue);
  1646. break;
  1647. // default are treated as boolean, and stored that way
  1648. //
  1649. default:
  1650. // !! means it will always be 1 or 0.
  1651. _snprintf( szInt, MAX_TRACE_LEN, "%d", (!!g_DebugFlags[nLoop].dwValue));
  1652. break;
  1653. }
  1654. // Write the param to the ini file
  1655. WritePrivateProfileStringA(
  1656. "DebugFlags",
  1657. g_DebugFlags[nLoop].szShortName,
  1658. szInt,
  1659. c_szDebugIniFileNameA);
  1660. }
  1661. }
  1662. // For now, this is always S_OK, since there's nothing above that can
  1663. // fail.
  1664. //
  1665. return hr;
  1666. }
  1667. //+---------------------------------------------------------------------------
  1668. //
  1669. // Member: CTracing::HrProcessTagSection
  1670. //
  1671. // Purpose: Grab the parameters from the ini file. If they're not
  1672. // available, then use the settings in default. Note - this
  1673. // will always work because ttidDefault will always be the first
  1674. // element. If a [default] section wasn't present, then it will
  1675. // be using the settings that were in the struct initialization,
  1676. // which is also fine.
  1677. //
  1678. // Arguments:
  1679. // ptte [] TraceTag element to load
  1680. //
  1681. // Returns:
  1682. //
  1683. // Author: jeffspr 15 Apr 1997
  1684. //
  1685. // Notes:
  1686. //
  1687. HRESULT CTracing::HrProcessTagSection( TraceTagElement * ptte )
  1688. {
  1689. HRESULT hr = S_OK;
  1690. AssertSz(m_fInitialized,
  1691. "CTracing::HrProcessTagSection. Class not initialized");
  1692. AssertSz(ptte, "CTracing::HrProcessTagSection -- invalid ptte");
  1693. // Get the output to file param
  1694. //
  1695. ptte->fOutputToFile = GetPrivateProfileIntA(
  1696. ptte->szShortName,
  1697. "OutputToFile",
  1698. ptte->fVerboseOnly ?
  1699. FALSE : g_TraceTags[ttidDefault].fOutputToFile,
  1700. c_szDebugIniFileNameA);
  1701. // Get the OutputDebugString param. Require that the error tag
  1702. // always has at least output debug string on.
  1703. //
  1704. if (ptte->ttid == ttidError)
  1705. {
  1706. ptte->fOutputDebugString = TRUE;
  1707. }
  1708. else
  1709. {
  1710. // Load the OutputToDebug
  1711. ptte->fOutputDebugString = GetPrivateProfileIntA(
  1712. ptte->szShortName,
  1713. "OutputToDebug",
  1714. ptte->fVerboseOnly ?
  1715. FALSE : g_TraceTags[ttidDefault].fOutputDebugString,
  1716. c_szDebugIniFileNameA);
  1717. }
  1718. return hr;
  1719. }
  1720. //+---------------------------------------------------------------------------
  1721. //
  1722. // Member: CTracing::CorruptionCheck
  1723. //
  1724. // Purpose: Validate the tracetag array. Check to see that the
  1725. // shortnames are valid, that the descriptions are valid,
  1726. // and that the tracetag elements are not out of order.
  1727. // Also verify that the correct number of tracetag elements
  1728. // exist.
  1729. //
  1730. // Arguments:
  1731. // (none)
  1732. //
  1733. // Returns:
  1734. //
  1735. // Author: jeffspr 15 Apr 1997
  1736. //
  1737. // Notes:
  1738. // (shaunco) 16 Jul 1997: This is #if'defd out until JVert
  1739. // gives us a fix for the alpha compiler. It blows up compiling this
  1740. // function in retail.
  1741. //
  1742. // (jeffspr) Tough noogies for JVert - I need this code. Hopefully
  1743. // this has been fixed by now.
  1744. //
  1745. VOID CTracing::CorruptionCheck()
  1746. {
  1747. INT nLoop = 0;
  1748. // Validate the tracetag structure
  1749. //
  1750. for (nLoop = 0; nLoop < g_nTraceTagCount; nLoop++)
  1751. {
  1752. // Verify that we're not out of order or missing ttids
  1753. //
  1754. AssertSz(g_TraceTags[nLoop].ttid == nLoop,
  1755. "Invalid ttid in the tracetag structure. Out of order. " \
  1756. "CTracing::CorruptionCheck");
  1757. AssertSz(g_TraceTags[nLoop].ttid < g_nTraceTagCount,
  1758. "Invalid ttid (out of range) in CTracing::CorruptionCheck");
  1759. // Validate the shortname (verify not NULL or empty strings)
  1760. //
  1761. AssertSz(g_TraceTags[nLoop].szShortName,
  1762. "Invalid tracetag short name (NULL) in CTracing::CorruptionCheck");
  1763. AssertSz(g_TraceTags[nLoop].szShortName[0] != 0,
  1764. "Invalid tracetagshort name (empty) in CTracing::CorruptionCheck");
  1765. // Validate the descriptions (verify not NULL or empty strings)
  1766. //
  1767. AssertSz(g_TraceTags[nLoop].szDescription,
  1768. "Invalid tracetagdescription in CTracing::CorruptionCheck");
  1769. AssertSz(g_TraceTags[nLoop].szDescription[0] != 0,
  1770. "Invalid tracetagdescription (empty) in CTracing::CorruptionCheck");
  1771. }
  1772. // Validate the debug flags structure
  1773. //
  1774. for (nLoop = 0; nLoop < g_nDebugFlagCount; nLoop++)
  1775. {
  1776. // Verify that we're not out of order or missing dfids
  1777. //
  1778. AssertSz(g_DebugFlags[nLoop].dfid == nLoop,
  1779. "Invalid dfid in the debugflag structure. Out of order. " \
  1780. "CTracing::CorruptionCheck");
  1781. AssertSz(g_DebugFlags[nLoop].dfid < g_nDebugFlagCount,
  1782. "Invalid dfid (out of range) in CTracing::CorruptionCheck");
  1783. // Validate the shortname (verify not NULL or empty strings)
  1784. //
  1785. AssertSz(g_DebugFlags[nLoop].szShortName,
  1786. "Invalid debug flag short name (NULL) in CTracing::CorruptionCheck");
  1787. AssertSz(g_DebugFlags[nLoop].szShortName[0] != 0,
  1788. "Invalid debug flag short name (empty) in CTracing::CorruptionCheck");
  1789. // Validate the descriptions (verify not NULL or empty strings)
  1790. //
  1791. AssertSz(g_DebugFlags[nLoop].szDescription,
  1792. "Invalid debug flag description in CTracing::CorruptionCheck");
  1793. AssertSz(g_DebugFlags[nLoop].szDescription[0] != 0,
  1794. "Invalid debug flag description (empty) in CTracing::CorruptionCheck");
  1795. }
  1796. return;
  1797. }
  1798. //+---------------------------------------------------------------------------
  1799. //
  1800. // Member: CTracing::Trace
  1801. //
  1802. // Purpose: The actual trace call that takes care of doing the output
  1803. // to each trace target (file, OutputDebugString, etc.)
  1804. //
  1805. // Arguments:
  1806. // ttid [] The tracetag to use for output
  1807. // pszaTrace [] The trace string itself.
  1808. //
  1809. // Returns:
  1810. //
  1811. // Author: jeffspr 12 Apr 1997
  1812. //
  1813. // Notes:
  1814. //
  1815. VOID CTracing::Trace( TraceTagId ttid,
  1816. PCSTR pszaTrace )
  1817. {
  1818. // HrInit should have called a corruption checker for the entire trace
  1819. // block, but we'll check again just to make sure.
  1820. //
  1821. AssertSz(g_nTraceTagCount > ttid, "ttid out of range in CTracing::Trace");
  1822. AssertSz(g_TraceTags[ttid].ttid == ttid,
  1823. "TraceTag structure is corrupt in CTracing::Trace");
  1824. // If they want debug string output
  1825. //
  1826. if (g_TraceTags[ttid].fOutputDebugString)
  1827. {
  1828. // Then output the string
  1829. //
  1830. OutputDebugStringA(pszaTrace);
  1831. }
  1832. // If they want file output
  1833. if (g_TraceTags[ttid].fOutputToFile)
  1834. {
  1835. if (!m_hLogFile)
  1836. {
  1837. // Assuming that we haven't already tried to open the file
  1838. // and failed, open it.
  1839. if (!m_fAttemptedLogFileOpen)
  1840. {
  1841. HRESULT hr = HrOpenLogFile();
  1842. if (FAILED(hr))
  1843. {
  1844. AssertSz(FALSE, "Failed to open log file for tracing. No, "
  1845. "this isn't a coding error, but hey, figured that "
  1846. "you'd want to know...");
  1847. }
  1848. }
  1849. }
  1850. // If we were already open, or the open has now succeeded, do the
  1851. // trace
  1852. //
  1853. if (m_hLogFile)
  1854. {
  1855. Assert(pszaTrace);
  1856. // Since pszTrace is guaranteed to be a single-byte trace, we
  1857. // don't need to do the WCHAR multiply on the length, just
  1858. // a char multiply.
  1859. //
  1860. DWORD dwBytesToWrite = lstrlenA(pszaTrace) * sizeof(CHAR);
  1861. DWORD dwBytesWritten = 0;
  1862. BOOL fWriteResult = FALSE;
  1863. fWriteResult = WriteFile(
  1864. m_hLogFile, // handle to file to write to
  1865. pszaTrace, // pointer to data to write to file
  1866. dwBytesToWrite, // size of trace
  1867. &dwBytesWritten, // Bytes actually written.
  1868. NULL ); // No overlapped
  1869. if (!fWriteResult || (dwBytesToWrite != dwBytesWritten))
  1870. {
  1871. AssertSz(FALSE, "CTracing failure: Can't write to log file."
  1872. " Can't trace or we'll be recursing on this failure.");
  1873. }
  1874. }
  1875. }
  1876. }
  1877. HRESULT CTracing::HrOpenLogFile()
  1878. {
  1879. HRESULT hr = S_OK;
  1880. AssertSz(m_fInitialized,
  1881. "CTracing not initialized in HrOpenLogFile()");
  1882. AssertSz(!m_hLogFile,
  1883. "File already open before call to HrOpenLogFile()");
  1884. // Mark us as having attempted to open the file, so we don't call this
  1885. // function everytime we log, if we can't open it.
  1886. //
  1887. m_fAttemptedLogFileOpen = TRUE;
  1888. // $$TODO (jeffspr) - Allow flags in the Options section of the ini
  1889. // file specify the create flags and attributes, which would allow
  1890. // us to control the overwriting of log files and/or the write-through
  1891. // properties.
  1892. //
  1893. // Actually open the file, creating if necessary.
  1894. //
  1895. m_hLogFile = CreateFileA(
  1896. m_szLogFilePath, // Pointer to name of file
  1897. GENERIC_WRITE, // access (read-write) mode
  1898. FILE_SHARE_READ, // share mode (allow read access)
  1899. NULL, // pointer to security attributes
  1900. CREATE_ALWAYS, // how to create
  1901. FILE_ATTRIBUTE_NORMAL | FILE_FLAG_WRITE_THROUGH,
  1902. NULL);
  1903. if (INVALID_HANDLE_VALUE == m_hLogFile)
  1904. {
  1905. m_hLogFile = NULL;
  1906. hr = HrFromLastWin32Error();
  1907. goto Exit;
  1908. }
  1909. Exit:
  1910. return hr;
  1911. }
  1912. using namespace std;
  1913. typedef deque<CTracingFuncCall, NOFAULT_ALLOC::nofault_allocator<CTracingFuncCall> > TRACING_FUNCTIONSTACK;
  1914. CTracingFuncCall::CTracingFuncCall(const CTracingFuncCall& TracingFuncCall)
  1915. {
  1916. Assert(g_csTracing);
  1917. m_szFunctionName = new(NO_FI) CHAR[strlen(TracingFuncCall.m_szFunctionName)+1];
  1918. if (m_szFunctionName)
  1919. {
  1920. strcpy(m_szFunctionName, TracingFuncCall.m_szFunctionName);
  1921. }
  1922. else
  1923. {
  1924. m_szFunctionName = c_szLowMemory;
  1925. }
  1926. m_szFunctionDName = new(NO_FI) CHAR[strlen(TracingFuncCall.m_szFunctionDName)+1];
  1927. if (m_szFunctionDName)
  1928. {
  1929. strcpy(m_szFunctionDName, TracingFuncCall.m_szFunctionDName);
  1930. }
  1931. else
  1932. {
  1933. m_szFunctionDName = c_szLowMemory;
  1934. }
  1935. m_szFile = new(NO_FI) CHAR[strlen(TracingFuncCall.m_szFile)+1];
  1936. if (m_szFile)
  1937. {
  1938. strcpy(m_szFile, TracingFuncCall.m_szFile);
  1939. }
  1940. else
  1941. {
  1942. m_szFile = c_szLowMemory;
  1943. }
  1944. m_dwLine = TracingFuncCall.m_dwLine;
  1945. m_dwFramePointer = TracingFuncCall.m_dwFramePointer;
  1946. m_dwThreadId = TracingFuncCall.m_dwThreadId;
  1947. m_ReturnAddress = TracingFuncCall.m_ReturnAddress;
  1948. #if defined(_X86_) || defined(_AMD64_)
  1949. m_arguments[0] = TracingFuncCall.m_arguments[0];
  1950. m_arguments[1] = TracingFuncCall.m_arguments[1];
  1951. m_arguments[2] = TracingFuncCall.m_arguments[2];
  1952. #elif defined (_IA64_)
  1953. m_arguments[0] = TracingFuncCall.m_arguments[0];
  1954. m_arguments[1] = TracingFuncCall.m_arguments[1];
  1955. m_arguments[2] = TracingFuncCall.m_arguments[2];
  1956. #else
  1957. // add other processors here
  1958. #endif
  1959. }
  1960. #if defined (_X86_) || defined (_AMD64_)
  1961. CTracingFuncCall::CTracingFuncCall(LPCSTR szFunctionName, LPCSTR szFunctionDName, LPCSTR szFile, const DWORD dwLine, DWORD_PTR dwpReturnAddress, const DWORD_PTR dwFramePointer)
  1962. #elif defined (_IA64_)
  1963. CTracingFuncCall::CTracingFuncCall(LPCSTR szFunctionName, LPCSTR szFunctionDName, LPCSTR szFile, const DWORD dwLine, DWORD_PTR dwpReturnAddress, const __int64 Args1, const __int64 Args2, const __int64 Args3)
  1964. #else
  1965. CTracingFuncCall::CTracingFuncCall(LPCSTR szFunctionName, LPCSTR szFunctionDName, LPCSTR szFile, const DWORD dwLine)
  1966. #endif
  1967. {
  1968. Assert(g_csTracing);
  1969. m_szFunctionName = new(NO_FI) CHAR[strlen(szFunctionName)+1];
  1970. if (m_szFunctionName)
  1971. {
  1972. strcpy(m_szFunctionName, szFunctionName);
  1973. }
  1974. else
  1975. {
  1976. m_szFunctionName = c_szLowMemory;
  1977. }
  1978. m_szFunctionDName = new(NO_FI) CHAR[strlen(szFunctionDName)+1];
  1979. if (m_szFunctionDName)
  1980. {
  1981. strcpy(m_szFunctionDName, szFunctionDName);
  1982. }
  1983. else
  1984. {
  1985. m_szFunctionDName = c_szLowMemory;
  1986. }
  1987. m_szFile = new(NO_FI) CHAR[strlen(szFile)+1];
  1988. if (m_szFile)
  1989. {
  1990. strcpy(m_szFile, szFile);
  1991. }
  1992. else
  1993. {
  1994. m_szFile = c_szLowMemory;
  1995. }
  1996. m_dwLine = dwLine;
  1997. m_ReturnAddress = dwpReturnAddress;
  1998. #if defined (_X86_) || defined (_AMD64_)
  1999. m_dwFramePointer = dwFramePointer;
  2000. if (dwFramePointer)
  2001. {
  2002. PDWORD_PTR pdwEbp = reinterpret_cast<PDWORD_PTR>(dwFramePointer);
  2003. pdwEbp++; // advance pass BaseEBP
  2004. pdwEbp++; // advance pass ReturnIP
  2005. m_arguments[0] = *pdwEbp; pdwEbp++;
  2006. m_arguments[1] = *pdwEbp; pdwEbp++;
  2007. m_arguments[2] = *pdwEbp;
  2008. }
  2009. else
  2010. {
  2011. m_arguments[0] = 0;
  2012. m_arguments[1] = 0;
  2013. m_arguments[2] = 0;
  2014. }
  2015. #elif defined (_IA64_)
  2016. m_dwFramePointer = 0;
  2017. m_arguments[0] = Args1;
  2018. m_arguments[1] = Args2;
  2019. m_arguments[2] = Args3;
  2020. #else
  2021. m_dwFramePointer = 0;
  2022. #endif
  2023. m_dwThreadId = GetCurrentThreadId();
  2024. }
  2025. CTracingFuncCall::~CTracingFuncCall()
  2026. {
  2027. Assert(g_csTracing);
  2028. if (c_szLowMemory != m_szFile)
  2029. {
  2030. delete[] m_szFile;
  2031. }
  2032. if (c_szLowMemory != m_szFunctionDName)
  2033. {
  2034. delete[] m_szFunctionDName;
  2035. }
  2036. if (c_szLowMemory != m_szFunctionName)
  2037. {
  2038. delete[] m_szFunctionName;
  2039. }
  2040. }
  2041. CTracingThreadInfo::CTracingThreadInfo()
  2042. {
  2043. Assert(g_csTracing);
  2044. m_dwLevel = 1;
  2045. m_dwThreadId = GetCurrentThreadId();
  2046. m_pfnStack = new(NO_FI) TRACING_FUNCTIONSTACK;
  2047. }
  2048. CTracingThreadInfo::~CTracingThreadInfo()
  2049. {
  2050. Assert(g_csTracing);
  2051. TRACING_FUNCTIONSTACK *pfnStack = reinterpret_cast<TRACING_FUNCTIONSTACK *>(m_pfnStack);
  2052. delete pfnStack;
  2053. }
  2054. CTracingThreadInfo* CTracingIndent::GetThreadInfo()
  2055. {
  2056. CTracingThreadInfo *pThreadInfo = NULL;
  2057. AssertSz(g_dwTlsTracing, "Tracing not initialized... Did RawDllMain run?");
  2058. AssertSz(g_csTracing, "Tracing not initialized... Did RawDllMain run?");
  2059. pThreadInfo = reinterpret_cast<CTracingThreadInfo *>(TlsGetValue(g_dwTlsTracing));
  2060. if (!pThreadInfo)
  2061. {
  2062. pThreadInfo = new(NO_FI) CTracingThreadInfo;
  2063. TlsSetValue(g_dwTlsTracing, pThreadInfo);
  2064. Assert(pThreadInfo == reinterpret_cast<CTracingThreadInfo *>(TlsGetValue(g_dwTlsTracing)));
  2065. }
  2066. Assert(pThreadInfo);
  2067. return pThreadInfo;
  2068. }
  2069. void CTracingIndent::FreeThreadInfo()
  2070. {
  2071. CTracingThreadInfo *pThreadInfo = NULL;
  2072. pThreadInfo = reinterpret_cast<CTracingThreadInfo *>(TlsGetValue(g_dwTlsTracing));
  2073. if (pThreadInfo)
  2074. {
  2075. TraceStackFn(ttidError);
  2076. AssertSz(pThreadInfo->m_dwLevel == 1, "Thread is being terminated that didn't complete (ignore will spew remaining stack if you attach a debugger)");
  2077. TraceStackFn(ttidError);
  2078. AssertSz(pThreadInfo->m_dwLevel == 1, "Thread is being terminated that didn't complete");
  2079. delete pThreadInfo;
  2080. TlsSetValue(g_dwTlsTracing, reinterpret_cast<LPVOID>(-1));
  2081. }
  2082. }
  2083. CTracingIndent::CTracingIndent()
  2084. {
  2085. bFirstTrace = TRUE;
  2086. m_szFunctionDName = NULL;
  2087. m_dwFramePointer = NULL;
  2088. }
  2089. #if defined (_X86_) || defined (_AMD64_)
  2090. void CTracingIndent::AddTrace(LPCSTR szFunctionName, LPCSTR szFunctionDName, LPCSTR szFile, const DWORD dwLine, LPCVOID pReturnAddress, const DWORD_PTR dwFramePointer)
  2091. #elif defined (_IA64_)
  2092. void CTracingIndent::AddTrace(LPCSTR szFunctionName, LPCSTR szFunctionDName, LPCSTR szFile, const DWORD dwLine, LPCVOID pReturnAddress, const __int64 Args1, const __int64 Args2, const __int64 Args3)
  2093. #else
  2094. void CTracingIndent::AddTrace(LPCSTR szFunctionName, LPCSTR szFunctionDName, LPCSTR szFile, const DWORD dwLine)
  2095. #endif
  2096. {
  2097. Assert(szFunctionName);
  2098. Assert(szFunctionDName);
  2099. Assert(szFile);
  2100. if (!bFirstTrace)
  2101. {
  2102. #if defined (_X86_) || defined (_AMD64_)
  2103. RemoveTrace(szFunctionDName, dwFramePointer);
  2104. #elif defined (_IA64_)
  2105. RemoveTrace(szFunctionDName, 0);
  2106. #else
  2107. RemoveTrace(szFunctionDName, 0);
  2108. #endif
  2109. }
  2110. else
  2111. {
  2112. bFirstTrace = FALSE;
  2113. }
  2114. volatile CTracingThreadInfo *pThreadInfo = GetThreadInfo();
  2115. TRACING_FUNCTIONSTACK &fnStack = *reinterpret_cast<TRACING_FUNCTIONSTACK *>(pThreadInfo->m_pfnStack);
  2116. Assert(g_csTracing);
  2117. try
  2118. {
  2119. EnterCriticalSection(g_csTracing);
  2120. #if defined (_X86_) || defined (_AMD64_)
  2121. CTracingFuncCall fnCall(szFunctionName, szFunctionDName, szFile, dwLine, reinterpret_cast<DWORD_PTR>(pReturnAddress), dwFramePointer);
  2122. #elif defined (_IA64_)
  2123. CTracingFuncCall fnCall(szFunctionName, szFunctionDName, szFile, dwLine, reinterpret_cast<DWORD_PTR>(pReturnAddress), Args1, Args2, Args3);
  2124. #else
  2125. CTracingFuncCall fnCall(szFunctionName, szFunctionDName, szFile, dwLine);
  2126. #endif
  2127. if (fnStack.size() == 0)
  2128. {
  2129. pThreadInfo->m_dwLevel++;
  2130. }
  2131. else
  2132. {
  2133. const CTracingFuncCall& fnTopOfStack = fnStack.front();
  2134. if ( (fnCall.m_dwFramePointer != fnTopOfStack.m_dwFramePointer) ||
  2135. strcmp(fnCall.m_szFunctionDName, fnTopOfStack.m_szFunctionDName))
  2136. {
  2137. pThreadInfo->m_dwLevel++;
  2138. }
  2139. }
  2140. m_szFunctionDName = new(NO_FI) CHAR[strlen(fnCall.m_szFunctionDName)+1];
  2141. if (m_szFunctionDName)
  2142. {
  2143. strcpy(m_szFunctionDName, fnCall.m_szFunctionDName);
  2144. }
  2145. else
  2146. {
  2147. m_szFunctionDName = c_szLowMemory;
  2148. }
  2149. m_dwFramePointer = fnCall.m_dwFramePointer;
  2150. fnStack.push_front(fnCall);
  2151. }
  2152. catch (bad_alloc)
  2153. {
  2154. }
  2155. LeaveCriticalSection(g_csTracing);
  2156. }
  2157. CTracingIndent::~CTracingIndent()
  2158. {
  2159. AssertSz(g_csTracing, "Tracing not initialized");
  2160. RemoveTrace(m_szFunctionDName, m_dwFramePointer);
  2161. }
  2162. void CTracingIndent::RemoveTrace(LPCSTR szFunctionDName, const DWORD_PTR dwFramePointer)
  2163. {
  2164. __try
  2165. {
  2166. EnterCriticalSection(g_csTracing);
  2167. volatile CTracingThreadInfo *pThreadInfo = GetThreadInfo();
  2168. TRACING_FUNCTIONSTACK &fnStack = *reinterpret_cast<TRACING_FUNCTIONSTACK *>(pThreadInfo->m_pfnStack);
  2169. Assert(szFunctionDName);
  2170. Assert(m_szFunctionDName);
  2171. Assert(g_csTracing);
  2172. if (
  2173. (fnStack.size() == 0)
  2174. ||
  2175. (
  2176. (
  2177. strcmp(m_szFunctionDName, szFunctionDName)
  2178. ||
  2179. strcmp(m_szFunctionDName, fnStack.front().m_szFunctionDName)
  2180. )
  2181. &&
  2182. (
  2183. (c_szLowMemory != m_szFunctionDName)
  2184. &&
  2185. (c_szLowMemory != fnStack.front().m_szFunctionDName)
  2186. &&
  2187. (c_szLowMemory != szFunctionDName)
  2188. )
  2189. )
  2190. ||
  2191. (m_dwFramePointer != fnStack.front().m_dwFramePointer)
  2192. ||
  2193. (dwFramePointer != m_dwFramePointer)
  2194. )
  2195. {
  2196. // Make sure to leave the critical section during the assert, so that it does not cause a deadlock.
  2197. LeaveCriticalSection(g_csTracing);
  2198. // This will trace the stack:
  2199. if (IsDebuggerPresent())
  2200. {
  2201. TraceTagFn(ttidError, "Tracing self-inconsistent - either a stack over/underwrite occurred or an exception was thrown in faulting stack:");
  2202. TraceStackFn(ttidError);
  2203. }
  2204. else
  2205. {
  2206. AssertSz(FALSE, "Tracing self-inconsistent - either a stack over/underwrite occurred or an exception was thrown.\r\nPlease attach a debugger and hit Ignore on this assert to spew info to the debugger (it will assert again).");
  2207. TraceTagFn(ttidError, "Tracing self-inconsistent - either a stack over/underwrite occurred or an exception was thrown in faulting stack:");
  2208. TraceStackFn(ttidError);
  2209. }
  2210. TraceTagFn(ttidError, "1) For complete stack info, .frame down to CTracingIndent__RemoveTrace, dv and find dwFramePointer (2nd parameter to CTracingIndent__RemoveTrace)");
  2211. TraceTagFn(ttidError, " Then do a kb=(value of dwFramePointer)");
  2212. TraceTagFn(ttidError, "2) For even more complete stack info, .frame down to CTracingIndent__RemoveTrace, dv and then dt -r on fnStack");
  2213. TraceTagFn(ttidError, " Then find the _Next, where m_szFunctionName == 'CTracingIndent::RemoveTrace'");
  2214. TraceTagFn(ttidError, " If it exists, find the value of m_dwFramePointer under _Next");
  2215. TraceTagFn(ttidError, " Then do a kb=(value of m_dwFramePointer)");
  2216. DebugBreak();
  2217. // Try to recover.
  2218. if (fnStack.size() > 0)
  2219. {
  2220. fnStack.pop_front();
  2221. }
  2222. EnterCriticalSection(g_csTracing);
  2223. }
  2224. else
  2225. {
  2226. DWORD_PTR dwOldFramePointer = fnStack.front().m_dwFramePointer;
  2227. fnStack.pop_front();
  2228. if ( (fnStack.size() == 0) ||
  2229. (dwOldFramePointer != fnStack.front().m_dwFramePointer) ||
  2230. strcmp(m_szFunctionDName, fnStack.front().m_szFunctionDName) )
  2231. {
  2232. pThreadInfo->m_dwLevel--;
  2233. Assert(pThreadInfo->m_dwLevel);
  2234. }
  2235. }
  2236. if (c_szLowMemory != m_szFunctionDName)
  2237. {
  2238. delete [] m_szFunctionDName;
  2239. }
  2240. }
  2241. __finally
  2242. {
  2243. LeaveCriticalSection(g_csTracing);
  2244. }
  2245. }
  2246. DWORD CTracingIndent::getspaces()
  2247. {
  2248. volatile CTracingThreadInfo *pThreadInfo = GetThreadInfo();
  2249. return pThreadInfo->m_dwLevel;
  2250. }
  2251. void CTracingIndent::TraceStackFn(TRACETAGID TraceTagId)
  2252. {
  2253. if (!g_TraceTags[TraceTagId].fOutputDebugString &&
  2254. !g_TraceTags[TraceTagId].fOutputToFile)
  2255. {
  2256. return;
  2257. }
  2258. volatile CTracingThreadInfo *pThreadInfo = GetThreadInfo();
  2259. TRACING_FUNCTIONSTACK &fnStack = *reinterpret_cast<TRACING_FUNCTIONSTACK *>(pThreadInfo->m_pfnStack);
  2260. Assert(g_csTracing);
  2261. __try
  2262. {
  2263. EnterCriticalSection(g_csTracing);
  2264. if (fnStack.size() == 0)
  2265. {
  2266. return;
  2267. }
  2268. #if defined (_X86_) || defined (_AMD64_)
  2269. TraceInternal(TraceTagId, NULL, 0, 0, "ChildEBP RetAddr Args to Child (reconstructed - ChildEBP is invalid now)", 0, FALSE);
  2270. #elif defined (_IA64_)
  2271. TraceInternal(TraceTagId, NULL, 0, 0, "RetAddr Args to Child (reconstructed)", 0, FALSE);
  2272. #else
  2273. TraceInternal(TraceTagId, NULL, 0, 0, "Function stack", 0, FALSE);
  2274. #endif
  2275. for (TRACING_FUNCTIONSTACK::const_iterator i = fnStack.begin(); i != fnStack.end(); i++)
  2276. {
  2277. CHAR szBuffer[MAX_TRACE_LEN];
  2278. #if defined (_X86_) || defined (_AMD64_)
  2279. _snprintf(szBuffer, MAX_TRACE_LEN, "%08x %08x %08x %08x %08x %s [%s @ %d]", i->m_dwFramePointer, i->m_ReturnAddress, i->m_arguments[0], i->m_arguments[1], i->m_arguments[2], i->m_szFunctionName, i->m_szFile, i->m_dwLine);
  2280. #elif defined (_IA64_)
  2281. _snprintf(szBuffer, MAX_TRACE_LEN, "%016I64x %016I64x 0x%016I64x 0x%016I64x %s [%s @ %d]", i->m_ReturnAddress, i->m_arguments[0], i->m_arguments[1], i->m_arguments[2], i->m_szFunctionName, i->m_szFile, i->m_dwLine);
  2282. #else
  2283. _snprintf(szBuffer, MAX_TRACE_LEN, "%s", i->m_szFunctionName);
  2284. #endif
  2285. TraceInternal (TraceTagId, NULL, 0, 0, szBuffer, 0, FALSE);
  2286. }
  2287. }
  2288. __finally
  2289. {
  2290. LeaveCriticalSection(g_csTracing);
  2291. }
  2292. }
  2293. void CTracingIndent::TraceStackFn(IN OUT LPSTR szString, IN OUT LPDWORD pdwSize)
  2294. {
  2295. volatile CTracingThreadInfo *pThreadInfo = GetThreadInfo();
  2296. TRACING_FUNCTIONSTACK &fnStack = *reinterpret_cast<TRACING_FUNCTIONSTACK *>(pThreadInfo->m_pfnStack);
  2297. Assert(g_csTracing);
  2298. __try
  2299. {
  2300. EnterCriticalSection(g_csTracing);
  2301. ZeroMemory(szString, *pdwSize);
  2302. if (fnStack.size() == 0)
  2303. {
  2304. return;
  2305. }
  2306. Assert(*pdwSize > MAX_TRACE_LEN);
  2307. LPSTR pszString = szString;
  2308. #if defined (_X86_) || defined (_AMD64_)
  2309. pszString += _snprintf(pszString, MAX_TRACE_LEN, " ChildEBP RetAddr Args to Child (reconstructed - ChildEBP is invalid now)\r\n");
  2310. #elif defined (_IA64_)
  2311. pszString += _snprintf(pszString, MAX_TRACE_LEN, " RetAddr Args to Child (reconstructed)\r\n");
  2312. #else
  2313. pszString += _snprintf(pszString, MAX_TRACE_LEN, " Function stack\r\n");
  2314. #endif
  2315. DWORD dwSizeIn = *pdwSize;
  2316. for (TRACING_FUNCTIONSTACK::const_iterator i = fnStack.begin(); i != fnStack.end(); i++)
  2317. {
  2318. CHAR szBuffer[1024];
  2319. #if defined (_X86_) || defined (_AMD64_)
  2320. _snprintf(szBuffer, MAX_TRACE_LEN, " %08x %08x %08x %08x %08x %s [%s @ %d]", i->m_dwFramePointer, i->m_ReturnAddress, i->m_arguments[0], i->m_arguments[1], i->m_arguments[2], i->m_szFunctionName, i->m_szFile, i->m_dwLine);
  2321. #elif defined (_IA64_)
  2322. _snprintf(szBuffer, MAX_TRACE_LEN, " %016I64x %016I64x 0x%016I64x 0x%016I64x %s [%s @ %d]", i->m_ReturnAddress, i->m_arguments[0], i->m_arguments[1], i->m_arguments[2], i->m_szFunctionName, i->m_szFile, i->m_dwLine);
  2323. #else
  2324. _snprintf(szBuffer, MAX_TRACE_LEN, " %s", i->m_szFunctionName);
  2325. #endif
  2326. pszString += _snprintf(pszString, MAX_TRACE_LEN, "%s\r\n", szBuffer);
  2327. if (pszString > (szString + (*pdwSize - celems(szBuffer))) ) // Can't use strlen since I need to know the length of the
  2328. // next element - not this one. Hence just take the maximum size.
  2329. {
  2330. pszString += _snprintf(pszString, MAX_TRACE_LEN, "...", szBuffer);
  2331. *pdwSize = dwSizeIn * 2; // Tell the caller to allocate more memory and call us back if they want more info.
  2332. break;
  2333. }
  2334. }
  2335. if (*pdwSize < dwSizeIn)
  2336. {
  2337. *pdwSize = (DWORD)(pszString - szString);
  2338. }
  2339. }
  2340. __finally
  2341. {
  2342. LeaveCriticalSection(g_csTracing);
  2343. }
  2344. }
  2345. VOID
  2346. WINAPIV
  2347. TraceFileFuncFn (TRACETAGID ttid)
  2348. {
  2349. if (FIsDebugFlagSet (dfidTraceFileFunc))
  2350. {
  2351. CHAR szBuffer[MAX_TRACE_LEN];
  2352. volatile CTracingThreadInfo *pThreadInfo = CTracingIndent::GetThreadInfo();
  2353. TRACING_FUNCTIONSTACK &fnStack = *reinterpret_cast<TRACING_FUNCTIONSTACK *>(pThreadInfo->m_pfnStack);
  2354. Assert(g_csTracing);
  2355. __try
  2356. {
  2357. EnterCriticalSection(g_csTracing);
  2358. const CTracingFuncCall& fnCall = fnStack.front();
  2359. if (fnStack.size() != 0)
  2360. {
  2361. if (FIsDebugFlagSet (dfidTraceSource))
  2362. {
  2363. #if defined (_X86_) || defined (_AMD64_)
  2364. _snprintf(szBuffer, MAX_TRACE_LEN, "%s [0x%08x 0x%08x 0x%08x] %s:%d", fnCall.m_szFunctionName, fnCall.m_arguments[0], fnCall.m_arguments[1], fnCall.m_arguments[2], fnCall.m_szFile, fnCall.m_dwLine);
  2365. #elif defined (_IA64_)
  2366. _snprintf(szBuffer, MAX_TRACE_LEN, "%s [0x%016I64x 0x%016I64x 0x%016I64x] %s:%d", fnCall.m_szFunctionName, fnCall.m_arguments[0], fnCall.m_arguments[1], fnCall.m_arguments[2], fnCall.m_szFile, fnCall.m_dwLine);
  2367. #else
  2368. _snprintf(szBuffer, MAX_TRACE_LEN, "%s %s:%d", fnCall.m_szFunctionName, fnCall.m_szFile, fnCall.m_dwLine);
  2369. #endif
  2370. }
  2371. else
  2372. {
  2373. #if defined (_X86_) || defined (_AMD64_)
  2374. _snprintf(szBuffer, MAX_TRACE_LEN, "%s [0x%08x 0x%08x 0x%08x]", fnCall.m_szFunctionName, fnCall.m_arguments[0], fnCall.m_arguments[1], fnCall.m_arguments[2]);
  2375. #elif defined (_IA64_)
  2376. _snprintf(szBuffer, MAX_TRACE_LEN, "%s [0x%016I64x 0x%016I64x 0x%016I64x]", fnCall.m_szFunctionName, fnCall.m_arguments[0], fnCall.m_arguments[1], fnCall.m_arguments[2]);
  2377. #else
  2378. _snprintf(szBuffer, MAX_TRACE_LEN, "%s", fnCall.m_szFunctionName);
  2379. #endif
  2380. }
  2381. TraceTagFn(ttid, szBuffer);
  2382. }
  2383. else
  2384. {
  2385. AssertSz(FALSE, "Trace failure");
  2386. }
  2387. }
  2388. __finally
  2389. {
  2390. LeaveCriticalSection(g_csTracing);
  2391. }
  2392. }
  2393. }
  2394. LPCSTR DBG_EMNAMES[] =
  2395. {
  2396. "INVALID_EVENTMGR",
  2397. "EVENTMGR_CONMAN",
  2398. "EVENTMGR_EAPOLMAN"
  2399. };
  2400. LPCSTR DBG_CMENAMES[] =
  2401. {
  2402. "INVALID_TYPE",
  2403. "CONNECTION_ADDED",
  2404. "CONNECTION_BANDWIDTH_CHANGE",
  2405. "CONNECTION_DELETED",
  2406. "CONNECTION_MODIFIED",
  2407. "CONNECTION_RENAMED",
  2408. "CONNECTION_STATUS_CHANGE",
  2409. "REFRESH_ALL",
  2410. "CONNECTION_ADDRESS_CHANGE"
  2411. };
  2412. LPCSTR DBG_NCMNAMES[] =
  2413. {
  2414. "NCM_NONE",
  2415. "NCM_DIRECT",
  2416. "NCM_ISDN",
  2417. "NCM_LAN",
  2418. "NCM_PHONE",
  2419. "NCM_TUNNEL",
  2420. "NCM_PPPOE",
  2421. "NCM_BRIDGE",
  2422. "NCM_SHAREDACCESSHOST_LAN",
  2423. "NCM_SHAREDACCESSHOST_RAS"
  2424. };
  2425. LPCSTR DBG_NCSMNAMES[] =
  2426. {
  2427. "NCSM_NONE",
  2428. "NCSM_LAN",
  2429. "NCSM_WIRELESS",
  2430. "NCSM_ATM",
  2431. "NCSM_ELAN",
  2432. "NCSM_1394",
  2433. "NCSM_DIRECT",
  2434. "NCSM_IRDA",
  2435. "NCSM_CM",
  2436. };
  2437. LPCSTR DBG_NCSNAMES[] =
  2438. {
  2439. "NCS_DISCONNECTED",
  2440. "NCS_CONNECTING",
  2441. "NCS_CONNECTED",
  2442. "NCS_DISCONNECTING",
  2443. "NCS_HARDWARE_NOT_PRESENT",
  2444. "NCS_HARDWARE_DISABLED",
  2445. "NCS_HARDWARE_MALFUNCTION",
  2446. "NCS_MEDIA_DISCONNECTED",
  2447. "NCS_AUTHENTICATING",
  2448. "NCS_AUTHENTICATION_SUCCEEDED",
  2449. "NCS_AUTHENTICATION_FAILED",
  2450. "NCS_INVALID_ADDRESS",
  2451. "NCS_CREDENTIALS_REQUIRED"
  2452. };
  2453. // Shorten these to fit more in.
  2454. LPCSTR DBG_NCCSFLAGS[] =
  2455. {
  2456. "_NONE",
  2457. "_ALL_USERS",
  2458. "_ALLOW_DUPLICATION",
  2459. "_ALLOW_REMOVAL",
  2460. "_ALLOW_RENAME",
  2461. "_SHOW_ICON",
  2462. "_INCOMING_ONLY",
  2463. "_OUTGOING_ONLY",
  2464. "_BRANDED",
  2465. "_SHARED",
  2466. "_BRIDGED",
  2467. "_FIREWALLED",
  2468. "_DEFAULT"
  2469. };
  2470. LPCSTR DbgEvents(DWORD Event)
  2471. {
  2472. if (Event < celems(DBG_CMENAMES))
  2473. {
  2474. return DBG_CMENAMES[Event];
  2475. }
  2476. else
  2477. {
  2478. return "UNKNOWN Event: Update DBG_CMENAMES table.";
  2479. }
  2480. }
  2481. LPCSTR DbgEventManager(DWORD EventManager)
  2482. {
  2483. if (EventManager < celems(DBG_EMNAMES))
  2484. {
  2485. return DBG_EMNAMES[EventManager];
  2486. }
  2487. else
  2488. {
  2489. return "UNKNOWN Event: Update DBG_EMNAMES table.";
  2490. }
  2491. }
  2492. LPCSTR DbgNcm(DWORD ncm)
  2493. {
  2494. if (ncm < celems(DBG_NCMNAMES))
  2495. {
  2496. return DBG_NCMNAMES[ncm];
  2497. }
  2498. else
  2499. {
  2500. return "UNKNOWN NCM: Update DBG_NCMNAMES table.";
  2501. }
  2502. }
  2503. LPCSTR DbgNcsm(DWORD ncsm)
  2504. {
  2505. if (ncsm < celems(DBG_NCSMNAMES))
  2506. {
  2507. return DBG_NCSMNAMES[ncsm];
  2508. }
  2509. else
  2510. {
  2511. return "UNKNOWN NCM: Update DBG_NCSMNAMES table.";
  2512. }
  2513. }
  2514. LPCSTR DbgNcs(DWORD ncs)
  2515. {
  2516. if (ncs < celems(DBG_NCSNAMES))
  2517. {
  2518. return DBG_NCSNAMES[ncs];
  2519. }
  2520. else
  2521. {
  2522. return "UNKNOWN NCS: Update DBG_NCSNAMES table.";
  2523. }
  2524. }
  2525. LPCSTR DbgNccf(DWORD nccf)
  2526. {
  2527. static CHAR szName[MAX_PATH];
  2528. if (nccf >= (1 << celems(DBG_NCCSFLAGS)) )
  2529. {
  2530. return "UNKNOWN NCCF: Update DBG_NCCSFLAGS table.";
  2531. }
  2532. if (0 == nccf)
  2533. {
  2534. strcpy(szName, DBG_NCCSFLAGS[0]);
  2535. }
  2536. else
  2537. {
  2538. szName[0] = '\0';
  2539. LPSTR szTemp = szName;
  2540. BOOL bFirst = TRUE;
  2541. for (DWORD x = 0; x < celems(DBG_NCCSFLAGS); x++)
  2542. {
  2543. if (nccf & (1 << x))
  2544. {
  2545. if (!bFirst)
  2546. {
  2547. szTemp += _snprintf(szTemp, MAX_TRACE_LEN, "+");
  2548. }
  2549. else
  2550. {
  2551. szTemp += _snprintf(szTemp, MAX_TRACE_LEN, "NCCF:");
  2552. }
  2553. bFirst = FALSE;
  2554. szTemp += _snprintf(szTemp, MAX_TRACE_LEN, "%s", DBG_NCCSFLAGS[x+1]);
  2555. }
  2556. }
  2557. }
  2558. return szName;
  2559. }
  2560. #ifdef ENABLELEAKDETECTION
  2561. CObjectLeakTrack *g_pObjectLeakTrack = NULL;
  2562. // First LPSTR is classname
  2563. // Second LPSTR is stack trace
  2564. class CObjectAllocationInfo
  2565. {
  2566. private:
  2567. void InitializeFromClass(const CObjectAllocationInfo & ObjectAllocationInfo);
  2568. public:
  2569. CObjectAllocationInfo & operator =(const CObjectAllocationInfo & ObjectAllocationInfo);
  2570. CObjectAllocationInfo();
  2571. CObjectAllocationInfo(LPCSTR szClassName, LPCSTR szStackInfo);
  2572. CObjectAllocationInfo(const CObjectAllocationInfo & ObjectAllocationInfo);
  2573. ~CObjectAllocationInfo();
  2574. LPSTR m_szClassName;
  2575. LPSTR m_szStackInfo;
  2576. LPVOID m_ppvStackTrace[32];
  2577. };
  2578. CObjectAllocationInfo::CObjectAllocationInfo()
  2579. {
  2580. Assert(FIsDebugFlagSet(dfidTrackObjectLeaks));
  2581. m_szClassName = NULL;
  2582. m_szStackInfo = NULL;
  2583. ZeroMemory(m_ppvStackTrace, sizeof(m_ppvStackTrace));
  2584. }
  2585. CObjectAllocationInfo & CObjectAllocationInfo::operator =(const CObjectAllocationInfo & ObjectAllocationInfo)
  2586. {
  2587. Assert(FIsDebugFlagSet(dfidTrackObjectLeaks));
  2588. if (m_szClassName)
  2589. {
  2590. delete[] m_szClassName;
  2591. m_szClassName = NULL;
  2592. }
  2593. if (m_szStackInfo)
  2594. {
  2595. delete[] m_szStackInfo;
  2596. m_szStackInfo = NULL;
  2597. }
  2598. ZeroMemory(m_ppvStackTrace, sizeof(m_ppvStackTrace));
  2599. InitializeFromClass(ObjectAllocationInfo);
  2600. return *this;
  2601. }
  2602. CObjectAllocationInfo::CObjectAllocationInfo(LPCSTR szClassName, LPCSTR szStackInfo)
  2603. {
  2604. Assert(FIsDebugFlagSet(dfidTrackObjectLeaks));
  2605. if (szClassName)
  2606. {
  2607. DWORD dwLen = strlen(szClassName) + 1;
  2608. m_szClassName = new(NO_FI) CHAR[dwLen];
  2609. if (szClassName)
  2610. {
  2611. strncpy(m_szClassName, szClassName, dwLen);
  2612. }
  2613. }
  2614. if (szStackInfo)
  2615. {
  2616. DWORD dwLen = strlen(szStackInfo) + 1;
  2617. m_szStackInfo = new(NO_FI) CHAR[dwLen];
  2618. if (m_szStackInfo)
  2619. {
  2620. strncpy(m_szStackInfo, szStackInfo, dwLen);
  2621. }
  2622. }
  2623. ZeroMemory(m_ppvStackTrace, sizeof(m_ppvStackTrace));
  2624. }
  2625. void CObjectAllocationInfo::InitializeFromClass(const CObjectAllocationInfo & ObjectAllocationInfo)
  2626. {
  2627. Assert(FIsDebugFlagSet(dfidTrackObjectLeaks));
  2628. m_szClassName = NULL;
  2629. m_szStackInfo = NULL;
  2630. if (ObjectAllocationInfo.m_szClassName)
  2631. {
  2632. DWORD dwLen = strlen(ObjectAllocationInfo.m_szClassName) + 1;
  2633. m_szClassName = new(NO_FI) CHAR[dwLen];
  2634. if (m_szClassName)
  2635. {
  2636. strncpy(m_szClassName, ObjectAllocationInfo.m_szClassName, dwLen);
  2637. }
  2638. }
  2639. if (ObjectAllocationInfo.m_szStackInfo)
  2640. {
  2641. DWORD dwLen = strlen(ObjectAllocationInfo.m_szStackInfo) + 1;
  2642. m_szStackInfo = new(NO_FI) CHAR[dwLen];
  2643. if (m_szStackInfo)
  2644. {
  2645. strncpy(m_szStackInfo, ObjectAllocationInfo.m_szStackInfo, dwLen);
  2646. }
  2647. }
  2648. memcpy(m_ppvStackTrace, ObjectAllocationInfo.m_ppvStackTrace, sizeof(m_ppvStackTrace));
  2649. }
  2650. CObjectAllocationInfo::CObjectAllocationInfo(const CObjectAllocationInfo & ObjectAllocationInfo)
  2651. {
  2652. Assert(FIsDebugFlagSet(dfidTrackObjectLeaks));
  2653. InitializeFromClass(ObjectAllocationInfo);
  2654. }
  2655. CObjectAllocationInfo::~CObjectAllocationInfo()
  2656. {
  2657. Assert(FIsDebugFlagSet(dfidTrackObjectLeaks));
  2658. if (m_szStackInfo)
  2659. {
  2660. delete[] m_szStackInfo;
  2661. m_szStackInfo = NULL;
  2662. }
  2663. if (m_szClassName)
  2664. {
  2665. delete[] m_szClassName;
  2666. m_szClassName = NULL;
  2667. }
  2668. }
  2669. typedef map<LPCVOID, CObjectAllocationInfo, NOFAULT_ALLOC::nofault_allocator<CTracingFuncCall> > MAPOBJLEAK;
  2670. //+---------------------------------------------------------------------------
  2671. //
  2672. // Member: CObjectLeakTrack::CObjectLeakTrack
  2673. //
  2674. // Purpose: Constructor
  2675. //
  2676. // Arguments:
  2677. //
  2678. // Returns:
  2679. //
  2680. // Author: deonb 7 July 2001
  2681. //
  2682. // Notes: We are allocating our g_mapObjLeak here
  2683. //
  2684. CObjectLeakTrack::CObjectLeakTrack()
  2685. {
  2686. Assert(FIsDebugFlagSet(dfidTrackObjectLeaks));
  2687. g_mapObjLeak = new(NO_FI) MAPOBJLEAK;
  2688. }
  2689. //+---------------------------------------------------------------------------
  2690. //
  2691. // Member: CObjectLeakTrack::CObjectLeakTrack
  2692. //
  2693. // Purpose: Destructor
  2694. //
  2695. // Arguments:
  2696. //
  2697. // Returns: none
  2698. //
  2699. // Author: deonb 7 July 2001
  2700. //
  2701. // Notes: We are deleting our g_mapObjLeak here. We have to typecast it
  2702. // first since the data types exported by trace.h to the world is
  2703. // LPVOID, to minimize dependencies in order to include tracing.
  2704. //
  2705. CObjectLeakTrack::~CObjectLeakTrack()
  2706. {
  2707. Assert(FIsDebugFlagSet(dfidTrackObjectLeaks));
  2708. delete reinterpret_cast<MAPOBJLEAK *>(g_mapObjLeak);
  2709. }
  2710. //+---------------------------------------------------------------------------
  2711. //
  2712. // Member: CObjectLeakTrack::Insert
  2713. //
  2714. // Purpose: Insert an object instance in the list
  2715. //
  2716. // Arguments: [in] pThis - This pointer of the object instance. This must
  2717. // be the same as the this pointer of the ::Remove
  2718. // [in] szdbgClassName - The classname of the object
  2719. // [in own] pszConstructionStack - The stacktrace of the object constructor.
  2720. // (or any other information that is useful to describe
  2721. // the origin of the object).
  2722. // This must be allocated with the global new operator
  2723. // since we will take ownership and free this
  2724. // Returns: none
  2725. //
  2726. // Author: deonb 7 July 2001
  2727. //
  2728. // Notes:
  2729. //
  2730. void CObjectLeakTrack::Insert(IN LPCVOID pThis, IN LPCSTR szdbgClassName, IN TAKEOWNERSHIP LPSTR pszConstructionStack)
  2731. {
  2732. Assert(FIsDebugFlagSet(dfidTrackObjectLeaks));
  2733. if (g_mapObjLeak)
  2734. {
  2735. MAPOBJLEAK &rmapObjLeak = *reinterpret_cast<MAPOBJLEAK *>(g_mapObjLeak);
  2736. CObjectAllocationInfo ObjectAllocationInfo(szdbgClassName, pszConstructionStack);
  2737. delete[] pszConstructionStack;
  2738. RtlWalkFrameChain(ObjectAllocationInfo.m_ppvStackTrace, celems(ObjectAllocationInfo.m_ppvStackTrace), 0);
  2739. rmapObjLeak[pThis] = ObjectAllocationInfo;
  2740. }
  2741. }
  2742. //+---------------------------------------------------------------------------
  2743. //
  2744. // Member: CObjectLeakTrack::Remove
  2745. //
  2746. // Purpose: Remove an object instance from the list
  2747. //
  2748. // Arguments: [in] pThis - This pointer of the object instance. This must
  2749. // be the same as the this pointer of the ::Insert
  2750. //
  2751. // Returns:
  2752. //
  2753. // Author: deonb 7 July 2001
  2754. //
  2755. // Notes:
  2756. //
  2757. void CObjectLeakTrack::Remove(IN LPCVOID pThis)
  2758. {
  2759. Assert(FIsDebugFlagSet(dfidTrackObjectLeaks));
  2760. if (g_mapObjLeak)
  2761. {
  2762. MAPOBJLEAK &rmapObjLeak = *reinterpret_cast<MAPOBJLEAK *>(g_mapObjLeak);
  2763. MAPOBJLEAK::iterator iter = rmapObjLeak.find(pThis);
  2764. if (iter != rmapObjLeak.end())
  2765. {
  2766. rmapObjLeak.erase(iter);
  2767. }
  2768. }
  2769. }
  2770. void RemoveKnownleakFn(LPCVOID pThis)
  2771. {
  2772. if (FIsDebugFlagSet(dfidTrackObjectLeaks) && g_pObjectLeakTrack)
  2773. {
  2774. __try
  2775. {
  2776. EnterCriticalSection(g_csTracing);
  2777. TraceTag(ttidAllocations, "An object at '0x%08x' was marked as a known leak", pThis);
  2778. g_pObjectLeakTrack->Remove(pThis);
  2779. }
  2780. __finally
  2781. {
  2782. LeaveCriticalSection(g_csTracing);
  2783. }
  2784. }
  2785. }
  2786. //+---------------------------------------------------------------------------
  2787. //
  2788. // Member: CObjectLeakTrack::AssertIfObjectsStillAllocated
  2789. //
  2790. // Purpose: Assert if the the list of allocated objects in the process is not NULL.
  2791. // and call DumpAllocatedObjects to dump out this list.
  2792. //
  2793. // Arguments: [in] szClassName. The classname of the objects to assert that there are nothing of.
  2794. // This classname can be obtained by calling typeid(CLASS).name()
  2795. // on your class. (E.g. typeid(CConnectionManager).name() )
  2796. //
  2797. // Can also be NULL to ensure that there are NO objects allocated
  2798. // Returns: none
  2799. //
  2800. // Author: deonb 7 July 2001
  2801. //
  2802. // Notes: Don't call this from outside tracing - rather call AssertNoAllocatedInstances
  2803. // which is safe to call in CHK and FRE.
  2804. BOOL CObjectLeakTrack::AssertIfObjectsStillAllocated(IN LPCSTR szClassName)
  2805. {
  2806. if (!FIsDebugFlagSet(dfidTrackObjectLeaks))
  2807. {
  2808. return FALSE;
  2809. }
  2810. if (g_mapObjLeak)
  2811. {
  2812. __try
  2813. {
  2814. EnterCriticalSection(g_csTracing);
  2815. MAPOBJLEAK &rmapObjLeak = *reinterpret_cast<MAPOBJLEAK *>(g_mapObjLeak);
  2816. BOOL fFoundObjectOfType = FALSE;
  2817. for (MAPOBJLEAK::const_iterator iter = rmapObjLeak.begin(); iter != rmapObjLeak.end(); iter++)
  2818. {
  2819. if (szClassName)
  2820. {
  2821. if (0 == strcmp(iter->second.m_szClassName, szClassName) )
  2822. {
  2823. fFoundObjectOfType = TRUE;
  2824. break;
  2825. }
  2826. }
  2827. else
  2828. {
  2829. fFoundObjectOfType = TRUE;
  2830. break;
  2831. }
  2832. }
  2833. WCHAR szModuleName[MAX_PATH];
  2834. ZeroMemory(szModuleName, MAX_PATH);
  2835. if (!GetModuleFileName(reinterpret_cast<HMODULE>(&__ImageBase), szModuleName, MAX_PATH))
  2836. {
  2837. if (GetModuleFileName(reinterpret_cast<HMODULE>(::GetModuleHandle(NULL)), szModuleName, MAX_PATH))
  2838. {
  2839. WCHAR szTemp[MAX_PATH];
  2840. wcscpy(szTemp, szModuleName);
  2841. wsprintf(szModuleName, L"at address 0x%08x inside %s", __ImageBase, szTemp);
  2842. }
  2843. else
  2844. {
  2845. wsprintf(szModuleName, L"at address 0x%08x", __ImageBase);
  2846. }
  2847. }
  2848. if (fFoundObjectOfType)
  2849. {
  2850. if (!IsDebuggerPresent())
  2851. {
  2852. CHAR szDescription[MAX_PATH];
  2853. sprintf(szDescription,
  2854. "An object leak has been detected in %S. Please attach a user or kernel mode debugger to process id %d (0x%04x) and hit IGNORE to dump the offending stacks.\r\nE.g. ntsd -Gg -p %d",
  2855. szModuleName,
  2856. GetCurrentProces
  2857. GetCurrentProcessId(),
  2858. GetCurrentProcessId(),
  2859. GetCurrentProcessId());
  2860. AssertSz(FALSE, szDescription);
  2861. }
  2862. DumpAllocatedObjects(ttidError, szClassName);
  2863. AssertSz(FALSE, "An object leak has been detected. This leak has been spewed to the debugger - press RETRY to look at spew and map ReturnAddr values to symbols.");
  2864. return TRUE;
  2865. }
  2866. else
  2867. {
  2868. CHAR szSpewText[MAX_PATH];
  2869. TraceTag(ttidError, "No leaks were detected inside module %S", szModuleName);
  2870. // sprintf(szSpewText, "No leaks were detected inside module %S", szModuleName);
  2871. // AssertSz(NULL, szSpewText);
  2872. }
  2873. }
  2874. __finally
  2875. {
  2876. LeaveCriticalSection(g_csTracing);
  2877. }
  2878. }
  2879. return FALSE;
  2880. }
  2881. //+---------------------------------------------------------------------------
  2882. //
  2883. // Member: CObjectLeakTrack::DumpAllocatedObjects
  2884. //
  2885. // Purpose: Dump the list of the objects and their construction stacks
  2886. // for the objects that were allocated but not deleted yet. Dumps
  2887. // to the debugger.
  2888. //
  2889. // Arguments: [in] TraceTagId. The TraceTag to trace it to.
  2890. // [in] szClassName. The classname of the objects to dump out.
  2891. // This classname can be obtained by calling typeid(CLASS).name()
  2892. // on your class. (E.g. typeid(CConnectionManager).name() )
  2893. //
  2894. // Can also be NULL to dump out objects of ALL types.
  2895. //
  2896. // Returns: none
  2897. //
  2898. // Author: deonb 7 July 2001
  2899. //
  2900. // Notes: Don't call this from outside - rather call TraceAllocatedObjects
  2901. // which is safe to call in CHK and FRE.
  2902. //
  2903. void CObjectLeakTrack::DumpAllocatedObjects(IN TRACETAGID TraceTagId, IN LPCSTR szClassName)
  2904. {
  2905. if (!FIsDebugFlagSet(dfidTrackObjectLeaks))
  2906. {
  2907. return;
  2908. }
  2909. if (g_mapObjLeak)
  2910. {
  2911. __try
  2912. {
  2913. EnterCriticalSection(g_csTracing);
  2914. MAPOBJLEAK &rmapObjLeak = *reinterpret_cast<MAPOBJLEAK *>(g_mapObjLeak);
  2915. for (MAPOBJLEAK::const_iterator iter = rmapObjLeak.begin(); iter != rmapObjLeak.end(); iter++)
  2916. {
  2917. BOOL fMustSpew = TRUE;
  2918. if (szClassName)
  2919. {
  2920. if (0 != strcmp(iter->second.m_szClassName, szClassName) )
  2921. {
  2922. fMustSpew = FALSE;
  2923. }
  2924. }
  2925. if (fMustSpew)
  2926. {
  2927. TraceTag(TraceTagId, "The object of type '%s' allocated at 0x%08x has not been freed:",
  2928. iter->second.m_szClassName, iter->first);
  2929. BOOL bHasDetailedStack = FALSE;
  2930. #if defined (_X86_)
  2931. if (iter->second.m_ppvStackTrace[0] != NULL)
  2932. {
  2933. bHasDetailedStack = TRUE;
  2934. }
  2935. #endif
  2936. if (*iter->second.m_szStackInfo)
  2937. {
  2938. TraceTag (TraceTagId, "Callstack below:\r\n%s", iter->second.m_szStackInfo);
  2939. }
  2940. else
  2941. {
  2942. if (!bHasDetailedStack)
  2943. {
  2944. TraceTag(TraceTagId, " <call stack information not available. See comments inside trace.h on how to increase your call stack information>.\r\n");
  2945. }
  2946. }
  2947. if (bHasDetailedStack)
  2948. {
  2949. DWORD dwCount = 0;
  2950. while ( (dwCount < celems(iter->second.m_ppvStackTrace)) &&
  2951. (iter->second.m_ppvStackTrace[dwCount]) )
  2952. {
  2953. dwCount++;
  2954. }
  2955. TraceTag (TraceTagId, "Detailed callstack information is available through the debugger. Execute the folowing command:");
  2956. TraceTag (TraceTagId, "dds 0x%08x L 0x%02x\r\n", iter->second.m_ppvStackTrace, dwCount);
  2957. }
  2958. }
  2959. }
  2960. }
  2961. __finally
  2962. {
  2963. LeaveCriticalSection(g_csTracing);
  2964. }
  2965. }
  2966. }
  2967. #endif // ENABLELEAKDETECTION
  2968. #endif // ENABLETRACE