dumpinit.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. // This is a part of the Microsoft Foundation Classes C++ library.
  2. // Copyright (C) 1992-1998 Microsoft Corporation
  3. // All rights reserved.
  4. //
  5. // This source code is only intended as a supplement to the
  6. // Microsoft Foundation Classes Reference and related
  7. // electronic documentation provided with the library.
  8. // See these sources for detailed information regarding the
  9. // Microsoft Foundation Classes product.
  10. #include "stdafx.h"
  11. #ifdef _DEBUG // entire file
  12. #ifdef AFX_AUX_SEG
  13. #pragma code_seg(AFX_AUX_SEG)
  14. #endif
  15. // You must have AFX.INI (from \MSVC20\MFC\SRC) in your Windows
  16. // directory if you desire diagnostic output.
  17. // See Technical note TN007 for a description of
  18. // afxTraceFlags and afxTraceEnabled.
  19. AFX_DATADEF CDumpContext afxDump;
  20. AFX_DATADEF BOOL afxTraceEnabled;
  21. AFX_DATADEF UINT afxTraceFlags;
  22. BOOL _afxDiagnosticInit = AfxDiagnosticInit();
  23. /////////////////////////////////////////////////////////////////////////////
  24. // _AFX_DEBUG_STATE implementation
  25. static const TCHAR szIniFile[] = _T("AFX.INI");
  26. static const TCHAR szDiagSection[] = _T("Diagnostics");
  27. static const TCHAR szTraceEnabled[] = _T("TraceEnabled");
  28. static const TCHAR szTraceFlags[] = _T("TraceFlags");
  29. #ifndef _AFX_NO_DEBUG_CRT
  30. static _CRT_DUMP_CLIENT pfnOldCrtDumpClient = NULL;
  31. static _CRT_REPORT_HOOK pfnOldCrtReportHook = NULL;
  32. void __cdecl _AfxCrtDumpClient(void * pvData, unsigned int nBytes)
  33. {
  34. char sz[256];
  35. CObject* pObject = (CObject*)pvData;
  36. #ifndef _AFX_PORTABLE
  37. // use SEH (structured exception handling) to catch even GPFs
  38. // that result from partially valid objects.
  39. __try
  40. #endif
  41. {
  42. // with vtable, verify that object and vtable are valid
  43. if (!AfxIsValidAddress(*(void**)pObject, sizeof(void*), FALSE) ||
  44. !AfxIsValidAddress(pObject, pObject->GetRuntimeClass()->m_nObjectSize, FALSE))
  45. {
  46. // short form for invalid objects
  47. wsprintfA(sz, "an invalid object at $%08lX, %u bytes long\n",
  48. pvData, nBytes);
  49. afxDump << sz;
  50. }
  51. else if (afxDump.GetDepth() > 0)
  52. {
  53. // long form
  54. pObject->Dump(afxDump);
  55. afxDump << "\n";
  56. }
  57. else
  58. {
  59. // short form
  60. wsprintfA(sz, "a %hs object at $%08lX, %u bytes long\n",
  61. pObject->GetRuntimeClass()->m_lpszClassName, pvData, nBytes);
  62. afxDump << sz;
  63. }
  64. }
  65. #ifndef _AFX_PORTABLE
  66. __except(EXCEPTION_EXECUTE_HANDLER)
  67. {
  68. // short form for trashed objects
  69. wsprintfA(sz, "faulted while dumping object at $%08lX, %u bytes long\n",
  70. pvData, nBytes);
  71. afxDump << sz;
  72. }
  73. #endif
  74. if (pfnOldCrtDumpClient != NULL)
  75. (*pfnOldCrtDumpClient)(pvData, nBytes);
  76. return;
  77. }
  78. int __cdecl _AfxCrtReportHook(int nRptType, char *szMsg, int* pResult)
  79. {
  80. // call the old report hook if there was one
  81. if (pfnOldCrtReportHook != NULL &&
  82. (*pfnOldCrtReportHook)(nRptType, szMsg, pResult))
  83. {
  84. return TRUE;
  85. }
  86. // no hook on asserts or when m_pFile is NULL
  87. if (nRptType == _CRT_ASSERT || afxDump.m_pFile == NULL)
  88. return FALSE;
  89. // non-NULL m_pFile, so go through afxDump for the message
  90. *pResult = FALSE;
  91. afxDump << szMsg;
  92. return TRUE;
  93. }
  94. #endif // _AFX_NO_DEBUG_CRT
  95. _AFX_DEBUG_STATE::_AFX_DEBUG_STATE()
  96. {
  97. afxTraceEnabled = ::GetPrivateProfileInt(szDiagSection, szTraceEnabled,
  98. TRUE, szIniFile);
  99. afxTraceFlags = ::GetPrivateProfileInt(szDiagSection, szTraceFlags,
  100. 0, szIniFile);
  101. #ifndef _AFX_NO_DEBUG_CRT
  102. ASSERT(pfnOldCrtDumpClient == NULL);
  103. pfnOldCrtDumpClient = _CrtSetDumpClient(_AfxCrtDumpClient);
  104. ASSERT(pfnOldCrtReportHook == NULL);
  105. pfnOldCrtReportHook = _CrtSetReportHook(_AfxCrtReportHook);
  106. _CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_WNDW);
  107. #endif // _AFX_NO_DEBUG_CRT
  108. }
  109. _AFX_DEBUG_STATE::~_AFX_DEBUG_STATE()
  110. {
  111. #ifndef _AFX_NO_DEBUG_CRT
  112. _CrtDumpMemoryLeaks();
  113. int nOldState = _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG);
  114. _CrtSetDbgFlag(nOldState & ~_CRTDBG_LEAK_CHECK_DF);
  115. _CrtSetReportHook(pfnOldCrtReportHook);
  116. _CrtSetDumpClient(pfnOldCrtDumpClient);
  117. #endif // _AFX_NO_DEBUG_CRT
  118. }
  119. #pragma warning(disable: 4074)
  120. #pragma init_seg(lib)
  121. PROCESS_LOCAL(_AFX_DEBUG_STATE, afxDebugState)
  122. BOOL AFXAPI AfxDiagnosticInit(void)
  123. {
  124. // just get the debug state to cause initialization
  125. _AFX_DEBUG_STATE* pState = afxDebugState.GetData();
  126. ASSERT(pState != NULL);
  127. return TRUE;
  128. }
  129. #endif //_DEBUG
  130. /////////////////////////////////////////////////////////////////////////////