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.

159 lines
3.7 KiB

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