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.

130 lines
4.6 KiB

  1. // --------------------------------------------------------------------------------
  2. // Debug.c
  3. // Copyright (c)1993-1995 Microsoft Corporation, All Rights Reserved
  4. // --------------------------------------------------------------------------------
  5. #include <windows.h>
  6. #include <stdarg.h>
  7. #include <shlwapi.h>
  8. #include "msoedbg.h"
  9. ASSERTDATA
  10. #ifdef DEBUG
  11. #define E_PENDING _HRESULT_TYPEDEF_(0x8000000AL)
  12. #define FACILITY_INTERNET 12
  13. #define MIME_E_NOT_FOUND MAKE_HRESULT(SEVERITY_ERROR, FACILITY_INTERNET, 0xCE05)
  14. // --------------------------------------------------------------------------------
  15. // DebugStrf
  16. // --------------------------------------------------------------------------------
  17. __cdecl DebugStrf(LPTSTR lpszFormat, ...)
  18. {
  19. static TCHAR szDebugBuff[500];
  20. va_list arglist;
  21. va_start(arglist, lpszFormat);
  22. wvnsprintf(szDebugBuff, ARRAYSIZE(szDebugBuff), lpszFormat, arglist);
  23. va_end(arglist);
  24. OutputDebugString(szDebugBuff);
  25. }
  26. // --------------------------------------------------------------------------------
  27. // HrTrace
  28. // --------------------------------------------------------------------------------
  29. HRESULT HrTrace(HRESULT hr, LPSTR lpszFile, INT nLine)
  30. {
  31. if (FAILED(hr) && MIME_E_NOT_FOUND != hr && E_PENDING != hr && E_NOINTERFACE != hr)
  32. DebugTrace("%s(%d) - HRESULT - %0X\n", lpszFile, nLine, hr);
  33. return hr;
  34. }
  35. // --------------------------------------------------------------------------------
  36. // AssertSzFn
  37. // --------------------------------------------------------------------------------
  38. void AssertSzFn(LPSTR szMsg, LPSTR szFile, int nLine)
  39. {
  40. static const char rgch1[] = "File %s, line %d:";
  41. static const char rgch2[] = "Unknown file:";
  42. static const char szAssert[] = "Assert Failure";
  43. static const char szInstructions[] = "\n\nPress Abort to stop execution and break into a debugger.\nPress Retry to break into the debugger.\nPress Ignore to continue running the program.";
  44. char rgch[1024];
  45. char *lpsz;
  46. int ret, cch;
  47. HWND hwndActive;
  48. DWORD dwFlags = MB_ABORTRETRYIGNORE | MB_ICONHAND | MB_SYSTEMMODAL | MB_SETFOREGROUND;
  49. if (szFile)
  50. wnsprintf(rgch, ARRAYSIZE(rgch)-2, rgch1, szFile, nLine);
  51. else
  52. StrCpyN(rgch, rgch2, ARRAYSIZE(rgch)-2);
  53. StrCatBuff(rgch, szInstructions, ARRAYSIZE(rgch)-2);
  54. cch = lstrlen(rgch);
  55. Assert(lstrlen(szMsg)<(512-cch-3));
  56. lpsz = &rgch[cch];
  57. *lpsz++ = '\n';
  58. *lpsz++ = '\n';
  59. StrCpyN(lpsz, szMsg, ARRAYSIZE(rgch) - cch -2);
  60. // If the active window is NULL, and we are running on
  61. // WinNT, let's set the MB_SERVICE_NOTIFICATION flag. That
  62. // way, if we are running as a service, the message box will
  63. // pop-up on the desktop.
  64. //
  65. // NOTE: This might not work in the case where we are a
  66. // service, and the current thread has called CoInitializeEx
  67. // with COINIT_APARTMENTTHREADED - 'cause in that case,
  68. // GetActiveWindow might return non-NULL (apartment model
  69. // threads have message queues). But hey - life's not
  70. // perfect...
  71. hwndActive = GetActiveWindow();
  72. if (hwndActive == NULL)
  73. {
  74. OSVERSIONINFO osvi;
  75. osvi.dwOSVersionInfoSize = sizeof(osvi);
  76. if (GetVersionEx(&osvi) && (osvi.dwPlatformId == VER_PLATFORM_WIN32_NT))
  77. {
  78. // See the docs for MessageBox and the MB_SERVICE_NOTIFICATION flag
  79. // to see why we do this...
  80. if (osvi.dwMajorVersion < 4)
  81. {
  82. dwFlags |= MB_SERVICE_NOTIFICATION_NT3X;
  83. }
  84. else
  85. {
  86. dwFlags |= MB_SERVICE_NOTIFICATION;
  87. }
  88. }
  89. }
  90. ret = MessageBox(hwndActive, rgch, szAssert, dwFlags);
  91. if ((IDABORT == ret) || (IDRETRY== ret))
  92. DebugBreak();
  93. /* Force a hard exit w/ a GP-fault so that Dr. Watson generates a nice stack trace log. */
  94. if (ret == IDABORT)
  95. *(LPBYTE)0 = 1; // write to address 0 causes GP-fault
  96. }
  97. // --------------------------------------------------------------------------------
  98. // NFAssertSzFn
  99. // --------------------------------------------------------------------------------
  100. void NFAssertSzFn(LPSTR szMsg, LPSTR szFile, int nLine)
  101. {
  102. char rgch[512];
  103. #ifdef MAC
  104. static const char rgch1[] = "Non-fatal assert:\n\tFile %s, line %u:\n\t%s\n";
  105. #else // !MAC
  106. static const char rgch1[] = "Non-fatal assert:\r\n\tFile %s, line %u:\r\n\t%s\r\n";
  107. #endif // MAC
  108. wnsprintf(rgch, ARRAYSIZE(rgch), rgch1, szFile, nLine, szMsg ? szMsg : "");
  109. OutputDebugString(rgch);
  110. }
  111. #endif