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.

167 lines
4.1 KiB

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