Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

171 lines
4.1 KiB

  1. // Implementation of debug support functions
  2. #include "precomp.hxx"
  3. #include <tchar.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <stdarg.h>
  7. #include <malloc.h>
  8. #define DLL_IMPLEMENTATION
  9. #define IMPLEMENTATION_EXPORT
  10. #include <irtldbg.h>
  11. IRTL_DLLEXP
  12. void __cdecl
  13. IrtlTrace(
  14. LPCTSTR ptszFormat,
  15. ...)
  16. {
  17. TCHAR tszBuff[2048];
  18. va_list args;
  19. va_start(args, ptszFormat);
  20. _vstprintf(tszBuff, ptszFormat, args);
  21. va_end(args);
  22. DBGPRINTF((DBG_CONTEXT, tszBuff));
  23. }
  24. #ifdef _DEBUG
  25. # if defined(USE_DEBUG_CRTS) && defined(_MSC_VER) && (_MSC_VER >= 1000)
  26. # ifdef RUNNING_AS_SERVICE
  27. // The default assertion mechanism set up by Visual C++ 4 will not
  28. // work with Active Server Pages because it's running inside a service
  29. // and there is no desktop to interact with.
  30. // Note: for this to work properly, #define _WIN32_WINNT 0x400 before
  31. // including <winuser.h> or MB_SERVICE_NOTIFICATION won't be #define'd.
  32. int __cdecl
  33. AspAssertHandler(
  34. int nReportType,
  35. char* pszErrorText,
  36. int* pnReturn)
  37. {
  38. const char szInfo[] = " (Press ABORT to terminate IIS,"
  39. " RETRY to debug this failure,"
  40. " or IGNORE to continue.)";
  41. char* pszMessageTitle = NULL;
  42. // These flags enable message boxes to show up on the user's console
  43. switch (nReportType)
  44. {
  45. case _CRT_WARN:
  46. // If using MFC's TRACE macro (AfxTrace), the report hook
  47. // (AspAssertHandler) will get called with _CRT_WARN. Ignore.
  48. pszMessageTitle = "Warning";
  49. *pnReturn = 0;
  50. return FALSE;
  51. case _CRT_ERROR:
  52. pszMessageTitle = "Fatal Error";
  53. break;
  54. case _CRT_ASSERT:
  55. pszMessageTitle = "Assertion Failed";
  56. break;
  57. }
  58. char* pszMessageText =
  59. static_cast<char*>(_alloca(strlen(pszErrorText) + strlen(szInfo) + 1));
  60. strcpy(pszMessageText, pszErrorText);
  61. strcat(pszMessageText, szInfo);
  62. const int n = MessageBoxA(NULL, pszMessageText, pszMessageTitle,
  63. (MB_SERVICE_NOTIFICATION | MB_TOPMOST
  64. | MB_ABORTRETRYIGNORE | MB_ICONEXCLAMATION));
  65. if (n == IDABORT)
  66. {
  67. exit(1);
  68. }
  69. else if (n == IDRETRY)
  70. {
  71. *pnReturn = 1; // tell _CrtDbgReport to start the debugger
  72. return TRUE; // tell _CrtDbgReport to run
  73. }
  74. *pnReturn = 0; // nothing for _CrtDbgReport to do
  75. return FALSE;
  76. }
  77. # endif // RUNNING_AS_SERVICE
  78. # endif // _MSC_VER >= 1000
  79. void
  80. IrtlDebugInit()
  81. {
  82. # if defined(USE_DEBUG_CRTS) && defined(_MSC_VER) && (_MSC_VER >= 1000)
  83. # ifdef RUNNING_AS_SERVICE
  84. // If we end up in _CrtDbgReport, don't put up a message box
  85. // _CrtSetReportMode(_CRT_WARN, _CRTDBG_MODE_DEBUG);
  86. _CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_DEBUG);
  87. _CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_DEBUG);
  88. // Use AspAssertHandler to put up a message box instead
  89. _CrtSetReportHook(AspAssertHandler);
  90. # endif // RUNNING_AS_SERVICE
  91. // Enable debug heap allocations & check for memory leaks at program exit
  92. // The memory leak check will not be performed if inetinfo.exe is
  93. // run directly under a debugger, only if it is run as a service.
  94. _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF
  95. | _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG));
  96. # endif // _MSC_VER >= 1000
  97. }
  98. void
  99. IrtlDebugTerm()
  100. {
  101. # if defined(USE_DEBUG_CRTS) && defined(_MSC_VER) && (_MSC_VER >= 1000)
  102. # ifdef RUNNING_AS_SERVICE
  103. // Turn off AspAssertHandler, so that we don't get numerous message boxes
  104. // if there are memory leaks on shutdown
  105. _CrtSetReportHook(NULL);
  106. # endif // RUNNING_AS_SERVICE
  107. # endif // _MSC_VER >= 1000
  108. }
  109. #endif //_DEBUG
  110. BOOL
  111. IsValidString(
  112. LPCTSTR ptsz,
  113. int nLength /* =-1 */)
  114. {
  115. if (ptsz == NULL)
  116. return FALSE;
  117. return !IsBadStringPtr(ptsz, nLength);
  118. }
  119. BOOL
  120. IsValidAddress(
  121. LPCVOID pv,
  122. UINT nBytes,
  123. BOOL fReadWrite /* =TRUE */)
  124. {
  125. return (pv != NULL
  126. && !IsBadReadPtr(pv, nBytes)
  127. && (!fReadWrite || !IsBadWritePtr((LPVOID) pv, nBytes)));
  128. }