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.9 KiB

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