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.

199 lines
4.8 KiB

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