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.

142 lines
3.8 KiB

  1. //+----------------------------------------------------------------------------
  2. //
  3. // File: debug.cpp
  4. //
  5. // Module: Network Load Balancing
  6. //
  7. // Synopsis: Provide the functionality of ASSERT
  8. //
  9. // Copyright (C) Microsoft Corporation. All rights reserved.
  10. //
  11. // Author: fengsun Created 8/3/98
  12. //
  13. //+----------------------------------------------------------------------------
  14. #include "pch.h"
  15. #pragma hdrstop
  16. #include "debug.h"
  17. #include <strsafe.h>
  18. #include "utils.h"
  19. #if ( defined(DEBUG) || defined(_DEBUG) || defined (DBG))
  20. #ifndef MB_SERVICE_NOTIFICATION
  21. #define MB_SERVICE_NOTIFICATION 0
  22. #endif
  23. static long dwAssertCount = 0; // Avoid another assert while the messagebox is up
  24. //+----------------------------------------------------------------------------
  25. //
  26. // Function: AssertMessage
  27. //
  28. // Synopsis: Popup a message box for asserting failure. Has three options:
  29. // ignore/debug/abort.
  30. //
  31. // Arguments: const char *pszFile - File name
  32. // unsigned nLine - Line number
  33. // const char *pszMsg - Message in the dialog box
  34. //
  35. // Returns: Nothing
  36. //
  37. // History: fengsun Created Header 8/3/98
  38. //
  39. //+----------------------------------------------------------------------------
  40. extern "C" void AssertMessageW(const TCHAR *pszFile, unsigned nLine, const TCHAR *pszMsg)
  41. {
  42. TCHAR szOutput[1024];
  43. //
  44. // Ignore return value of StringCchPrintf since it will truncate the buffer and
  45. // guarantees to null-terminate it for us.
  46. //
  47. (VOID) StringCchPrintf(szOutput, ASIZECCH(szOutput), TEXT("%s(%u) - %s\n"), pszFile, nLine, pszMsg);
  48. OutputDebugString(szOutput);
  49. (VOID) StringCchPrintf(szOutput, ASIZECCH(szOutput), TEXT("%s(%u) - %s\n( Press Retry to debug )"), pszFile, nLine, pszMsg);
  50. int nCode = IDIGNORE;
  51. //
  52. // If there is no Assertion messagebox, popup one
  53. //
  54. if (dwAssertCount <2 )
  55. {
  56. dwAssertCount++;
  57. //
  58. // Title format: Assertion Failed - hello.dll
  59. //
  60. //
  61. // Find the base address of this module.
  62. //
  63. MEMORY_BASIC_INFORMATION mbi;
  64. mbi.AllocationBase = NULL; // current process by if VirtualQuery failed
  65. VirtualQuery(
  66. AssertMessageW, // any pointer with in the module
  67. &mbi,
  68. sizeof(mbi) );
  69. //
  70. // Get the module filename.
  71. //
  72. WCHAR szFileName[MAX_PATH + 1];
  73. szFileName[0] = L'\0'; // in case of failure
  74. if (GetModuleFileNameW(
  75. (HINSTANCE)mbi.AllocationBase,
  76. szFileName,
  77. MAX_PATH ) == 0)
  78. {
  79. szFileName[0] = L'\0';
  80. }
  81. //
  82. // Get the filename out of the full path
  83. //
  84. for (int i=lstrlen(szFileName);i != 0 && szFileName[i-1] != L'\\'; i--)
  85. ;
  86. WCHAR szTitle[48];
  87. if (StringCchCopy(szTitle, ASIZECCH(szTitle), L"Assertion Failed - ") == S_OK)
  88. {
  89. (VOID) StringCchCat(szTitle, ASIZECCH(szTitle), szFileName+i);
  90. }
  91. nCode = MessageBoxEx(NULL,szOutput,szTitle,
  92. MB_TOPMOST | MB_ICONHAND | MB_ABORTRETRYIGNORE | MB_SERVICE_NOTIFICATION,LANG_USER_DEFAULT);
  93. dwAssertCount--;
  94. }
  95. if (nCode == IDIGNORE)
  96. {
  97. return; // ignore
  98. }
  99. else if (nCode == IDRETRY)
  100. {
  101. #ifdef _X86_
  102. //
  103. // break into the debugger .
  104. // Step out of this fuction to get to your ASSERT() code
  105. //
  106. _asm { int 3 };
  107. #else
  108. DebugBreak();
  109. #endif
  110. return; // ignore and continue in debugger to diagnose problem
  111. }
  112. // else fall through and call Abort
  113. ExitProcess((DWORD)-1);
  114. }
  115. #endif //_DEBUG