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.

246 lines
4.8 KiB

  1. /*++
  2. Copyright (c) 1992 Microsoft Corporation
  3. Module Name:
  4. Debug.c
  5. Abstract:
  6. This module contains debugging support.
  7. Author:
  8. David J. Gilman (davegi) 30-Jul-1992
  9. Environment:
  10. User Mode
  11. --*/
  12. //
  13. // Global flag bits.
  14. //
  15. struct
  16. DEBUG_FLAGS {
  17. int DebuggerAttached:1;
  18. } WintoolsGlobalFlags;
  19. #if DBG
  20. #include <stdarg.h>
  21. #include <stdio.h>
  22. #include "wintools.h"
  23. //
  24. // Internal function prototypes.
  25. //
  26. LPCWSTR
  27. DebugFormatStringW(
  28. IN DWORD Flags,
  29. IN LPCWSTR Format,
  30. IN va_list* Args
  31. );
  32. VOID
  33. DebugAssertW(
  34. IN LPCWSTR Expression,
  35. IN LPCSTR File,
  36. IN DWORD LineNumber
  37. )
  38. /*++
  39. Routine Description:
  40. Display an assertion failure message box which gives the user a choice
  41. as to whether the process should be aborted, the assertion ignored or
  42. a break exception generated.
  43. Arguments:
  44. Expression - Supplies a string representation of the failed assertion.
  45. File - Supplies a pointer to the file name where the assertion
  46. failed.
  47. LineNumber - Supplies the line number in the file where the assertion
  48. failed.
  49. Return Value:
  50. None.
  51. --*/
  52. {
  53. LPCWSTR Buffer;
  54. DWORD_PTR Args[ ] = {
  55. ( DWORD_PTR ) Expression,
  56. ( DWORD_PTR ) GetLastError( ),
  57. ( DWORD_PTR ) File,
  58. ( DWORD_PTR ) LineNumber
  59. };
  60. DbgPointerAssert( Expression );
  61. DbgPointerAssert( File );
  62. //
  63. // Format the assertion string that describes the failure.
  64. //
  65. Buffer = DebugFormatStringW(
  66. FORMAT_MESSAGE_ARGUMENT_ARRAY,
  67. L"Assertion Failed : %1!s! (%2!d!)\nin file %3!hs! at line %4!d!\n",
  68. ( va_list* ) Args
  69. );
  70. //
  71. // If the debugger is attached flag is set, display the string on the
  72. // debugger and break. If not generate a pop-up and leave the choice
  73. // to the user.
  74. //
  75. if( WintoolsGlobalFlags.DebuggerAttached ) {
  76. OutputDebugString( Buffer );
  77. DebugBreak( );
  78. } else {
  79. int Response;
  80. WCHAR ModuleBuffer[ MAX_PATH ];
  81. DWORD Length;
  82. //
  83. // Get the asserting module's file name.
  84. //
  85. Length = GetModuleFileName(
  86. NULL,
  87. ModuleBuffer,
  88. ARRAYSIZE(ModuleBuffer)
  89. );
  90. //
  91. // Display the assertin message and gives the user the choice of:
  92. // Abort: - kills the process.
  93. // Retry: - generates a breakpoint exception.
  94. // Ignore: - continues the process.
  95. //
  96. Response = MessageBox(
  97. NULL,
  98. Buffer,
  99. ( Length != 0 ) ? ModuleBuffer : L"Assertion Failure",
  100. MB_ABORTRETRYIGNORE | MB_ICONHAND | MB_SETFOREGROUND | MB_TASKMODAL
  101. );
  102. switch( Response ) {
  103. //
  104. // Terminate the process.
  105. //
  106. case IDABORT:
  107. {
  108. ExitProcess( (UINT) -1 );
  109. break;
  110. }
  111. //
  112. // Ignore the failed assertion.
  113. //
  114. case IDIGNORE:
  115. {
  116. break;
  117. }
  118. //
  119. // Break into a debugger.
  120. //
  121. case IDRETRY:
  122. {
  123. DebugBreak( );
  124. break;
  125. }
  126. //
  127. // Break into a debugger because of a catastrophic failure.
  128. //
  129. default:
  130. {
  131. DebugBreak( );
  132. break;
  133. }
  134. }
  135. }
  136. }
  137. LPCWSTR
  138. DebugFormatStringW(
  139. IN DWORD Flags,
  140. IN LPCWSTR Format,
  141. IN va_list* Args
  142. )
  143. /*++
  144. Routine Description:
  145. Formats a string using the FormatMessage API.
  146. Arguments:
  147. Flags - Supplies flags which are used to control the FormatMessage API.
  148. Format - Supplies a printf style format string.
  149. Args - Supplies a list of arguments whose format is depndent on the
  150. flags valuse.
  151. Return Value:
  152. LPCWSTR - Returns a pointer to the formatted string.
  153. --*/
  154. {
  155. static
  156. WCHAR Buffer[ MAX_CHARS ];
  157. DWORD Count;
  158. DbgPointerAssert( Format );
  159. //
  160. // Format the string.
  161. //
  162. Count = FormatMessageW(
  163. Flags | FORMAT_MESSAGE_FROM_STRING & ~FORMAT_MESSAGE_FROM_HMODULE,
  164. ( LPVOID ) Format,
  165. 0,
  166. 0,
  167. Buffer,
  168. ARRAYSIZE( Buffer ),
  169. Args
  170. );
  171. DbgAssert( Count != 0 );
  172. //
  173. // Return the formatted string.
  174. //
  175. return Buffer;
  176. }
  177. #endif // DBG