Source code of Windows XP (NT5)
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.

305 lines
7.3 KiB

  1. //+----------------------------------------------------------------------------
  2. //
  3. // File: debug.cpp
  4. //
  5. // Module: Network Load Balancing
  6. //
  7. // Synopsis: Provide the functionality of ASSERT and TRACE
  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 "precomp.h"
  17. #include "debug.h"
  18. #if ( defined(DEBUG) || defined(_DEBUG) || defined (DBG))
  19. #ifndef MB_SERVICE_NOTIFICATION
  20. #define MB_SERVICE_NOTIFICATION 0
  21. #endif
  22. static long dwAssertCount = 0; // Avoid another assert while the messagebox is up
  23. //+----------------------------------------------------------------------------
  24. //
  25. // Function: TraceMessage
  26. //
  27. // Synopsis: Output debug string
  28. //
  29. // Arguments: const char *pszFmt ...- Printf style arguments list
  30. //
  31. //
  32. // Returns: Nothing
  33. //
  34. // History: fengsun Created Header 8/3/98
  35. //
  36. //+----------------------------------------------------------------------------
  37. extern "C" void TraceMessageW(const TCHAR *pszFmt, ...)
  38. {
  39. va_list valArgs;
  40. TCHAR szOutput[512];
  41. va_start(valArgs,pszFmt);
  42. wvsprintf(szOutput,pszFmt,valArgs);
  43. va_end(valArgs);
  44. lstrcat(szOutput,TEXT("\r\n"));
  45. OutputDebugString(szOutput);
  46. }
  47. //+----------------------------------------------------------------------------
  48. //
  49. // Function: AssertMessage
  50. //
  51. // Synopsis: Popup a message box for asserting failure. Has three options:
  52. // ignore/debug/abort.
  53. //
  54. // Arguments: const char *pszFile - File name
  55. // unsigned nLine - Line number
  56. // const char *pszMsg - Message in the dialog box
  57. //
  58. // Returns: Nothing
  59. //
  60. // History: fengsun Created Header 8/3/98
  61. //
  62. //+----------------------------------------------------------------------------
  63. extern "C" void AssertMessageW(const TCHAR *pszFile, unsigned nLine, const TCHAR *pszMsg)
  64. {
  65. TCHAR szOutput[1024];
  66. wsprintf(szOutput,TEXT("%s(%u) - %s\n"),pszFile,nLine,pszMsg);
  67. OutputDebugString(szOutput);
  68. wsprintf(szOutput,TEXT("%s(%u) - %s\n( Press Retry to debug )"),pszFile,nLine,pszMsg);
  69. int nCode = IDIGNORE;
  70. //
  71. // If there is no Assertion messagebox, popup one
  72. //
  73. if (dwAssertCount <2 )
  74. {
  75. dwAssertCount++;
  76. //
  77. // Title format: Assertion Failed - hello.dll
  78. //
  79. //
  80. // Find the base address of this module.
  81. //
  82. MEMORY_BASIC_INFORMATION mbi;
  83. mbi.AllocationBase = NULL; // current process by if VirtualQuery failed
  84. VirtualQuery(
  85. AssertMessageW, // any pointer with in the module
  86. &mbi,
  87. sizeof(mbi) );
  88. //
  89. // Get the module filename.
  90. //
  91. WCHAR szFileName[MAX_PATH + 1];
  92. szFileName[0] = L'\0'; // in case of failure
  93. GetModuleFileNameW(
  94. (HINSTANCE)mbi.AllocationBase,
  95. szFileName,
  96. MAX_PATH );
  97. //
  98. // Get the filename out of the full path
  99. //
  100. for (int i=lstrlen(szFileName);i != 0 && szFileName[i-1] != L'\\'; i--)
  101. ;
  102. WCHAR szTitle[48];
  103. lstrcpyW(szTitle, L"Assertion Failed - ");
  104. lstrcpynW(&szTitle[lstrlenW(szTitle)], szFileName+i,
  105. sizeof(szTitle)/sizeof(szTitle[0]) - lstrlenW(szTitle) -1); // there is no lstrcatn
  106. nCode = MessageBoxEx(NULL,szOutput,szTitle,
  107. MB_TOPMOST | MB_ICONHAND | MB_ABORTRETRYIGNORE | MB_SERVICE_NOTIFICATION,LANG_USER_DEFAULT);
  108. dwAssertCount--;
  109. }
  110. if (nCode == IDIGNORE)
  111. {
  112. return; // ignore
  113. }
  114. else if (nCode == IDRETRY)
  115. {
  116. #ifdef _X86_
  117. //
  118. // break into the debugger .
  119. // Step out of this fuction to get to your ASSERT() code
  120. //
  121. _asm { int 3 };
  122. #else
  123. DebugBreak();
  124. #endif
  125. return; // ignore and continue in debugger to diagnose problem
  126. }
  127. // else fall through and call Abort
  128. ExitProcess((DWORD)-1);
  129. }
  130. //+----------------------------------------------------------------------------
  131. //
  132. // Function: TraceMessage
  133. //
  134. // Synopsis: Output debug string
  135. //
  136. // Arguments: const char *pszFmt ...- Printf style arguments list
  137. //
  138. //
  139. // Returns: Nothing
  140. //
  141. // History: fengsun Created Header 8/3/98
  142. //
  143. //+----------------------------------------------------------------------------
  144. extern "C" void TraceMessageA(const CHAR *pszFmt, ...)
  145. {
  146. va_list valArgs;
  147. CHAR szOutput[512];
  148. va_start(valArgs,pszFmt);
  149. wvsprintfA(szOutput,pszFmt,valArgs);
  150. va_end(valArgs);
  151. lstrcatA(szOutput,("\r\n"));
  152. OutputDebugStringA(szOutput);
  153. }
  154. //+----------------------------------------------------------------------------
  155. //
  156. // Function: AssertMessageA
  157. //
  158. // Synopsis: Popup a message box for asserting failure. Has three options:
  159. // ignore/debug/abort.
  160. //
  161. // Arguments: const char *pszFile - File name
  162. // unsigned nLine - Line number
  163. // const char *pszMsg - Message in the dialog box
  164. //
  165. // Returns: Nothing
  166. //
  167. // History: fengsun Created Header 8/3/98
  168. //
  169. //+----------------------------------------------------------------------------
  170. extern "C" void AssertMessageA(const CHAR *pszFile, unsigned nLine, const CHAR *pszMsg)
  171. {
  172. CHAR szOutput[1024];
  173. wsprintfA(szOutput,("%s(%u) - %s\n"),pszFile,nLine,pszMsg);
  174. OutputDebugStringA(szOutput);
  175. wsprintfA(szOutput,("%s(%u) - %s\n( Press Retry to debug )"),pszFile,nLine,pszMsg);
  176. int nCode = IDIGNORE;
  177. //
  178. // If there is no Assertion messagebox, popup one
  179. //
  180. if (dwAssertCount <2 )
  181. {
  182. dwAssertCount++;
  183. //
  184. // Title format: Assertion Failed - hello.dll
  185. //
  186. //
  187. // Find the base address of this module.
  188. //
  189. MEMORY_BASIC_INFORMATION mbi;
  190. mbi.AllocationBase = NULL; // current process by if VirtualQuery failed
  191. VirtualQuery(
  192. AssertMessageW, // any pointer with in the module
  193. &mbi,
  194. sizeof(mbi) );
  195. //
  196. // Get the module filename.
  197. //
  198. CHAR szFileName[MAX_PATH + 1];
  199. szFileName[0] = '\0'; // in case of failure
  200. GetModuleFileNameA(
  201. (HINSTANCE)mbi.AllocationBase,
  202. szFileName,
  203. MAX_PATH );
  204. //
  205. // Get the filename out of the full path
  206. //
  207. for (int i=lstrlenA(szFileName);i != 0 && szFileName[i-1] != '\\'; i--)
  208. ;
  209. CHAR szTitle[48];
  210. lstrcpyA(szTitle, "Assertion Failed - ");
  211. lstrcpynA(&szTitle[lstrlenA(szTitle)], szFileName+i,
  212. sizeof(szTitle)/sizeof(szTitle[0]) - lstrlenA(szTitle) -1); // there is no lstrcatn
  213. nCode = MessageBoxExA(NULL,szOutput,szTitle,
  214. MB_TOPMOST | MB_ICONHAND | MB_ABORTRETRYIGNORE | MB_SERVICE_NOTIFICATION,LANG_USER_DEFAULT);
  215. dwAssertCount--;
  216. }
  217. dwAssertCount--;
  218. if (nCode == IDIGNORE)
  219. {
  220. return; // ignore
  221. }
  222. else if (nCode == IDRETRY)
  223. {
  224. #ifdef _X86_
  225. //
  226. // break into the debugger .
  227. // Step out of this fuction to get to your ASSERT() code
  228. //
  229. _asm { int 3 };
  230. #else
  231. DebugBreak();
  232. #endif
  233. return; // ignore and continue in debugger to diagnose problem
  234. }
  235. // else fall through and call Abort
  236. ExitProcess((DWORD)-1);
  237. }
  238. #endif //_DEBUG