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.

299 lines
5.4 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. sizeof( 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 )
  100. ? ModuleBuffer
  101. : L"Assertion Failure",
  102. MB_ABORTRETRYIGNORE
  103. | MB_ICONHAND
  104. | MB_SETFOREGROUND
  105. | MB_TASKMODAL
  106. );
  107. switch( Response ) {
  108. //
  109. // Terminate the process.
  110. //
  111. case IDABORT:
  112. {
  113. ExitProcess( (UINT) -1 );
  114. break;
  115. }
  116. //
  117. // Ignore the failed assertion.
  118. //
  119. case IDIGNORE:
  120. {
  121. break;
  122. }
  123. //
  124. // Break into a debugger.
  125. //
  126. case IDRETRY:
  127. {
  128. DebugBreak( );
  129. break;
  130. }
  131. //
  132. // Break into a debugger because of a catastrophic failure.
  133. //
  134. default:
  135. {
  136. DebugBreak( );
  137. break;
  138. }
  139. }
  140. }
  141. }
  142. VOID
  143. DebugPrintfW(
  144. IN LPCWSTR Format,
  145. IN ...
  146. )
  147. /*++
  148. Routine Description:
  149. Display a printf style string on the debugger.
  150. Arguments:
  151. Format - Supplies a FormatMessage style format string.
  152. ... - Supplies zero or more values based on the format
  153. descpritors supplied in Format.
  154. Return Value:
  155. None.
  156. --*/
  157. {
  158. LPCWSTR Buffer;
  159. va_list Args;
  160. DbgPointerAssert( Format );
  161. //
  162. // Retrieve the values and format the string.
  163. //
  164. va_start( Args, Format );
  165. Buffer = DebugFormatStringW( 0, Format, &Args );
  166. va_end( Args );
  167. //
  168. // Display the string on the debugger.
  169. //
  170. OutputDebugString( Buffer );
  171. }
  172. LPCWSTR
  173. DebugFormatStringW(
  174. IN DWORD Flags,
  175. IN LPCWSTR Format,
  176. IN va_list* Args
  177. )
  178. /*++
  179. Routine Description:
  180. Formats a string using the FormatMessage API.
  181. Arguments:
  182. Flags - Supplies flags which are used to control the FormatMessage API.
  183. Format - Supplies a printf style format string.
  184. Args - Supplies a list of arguments whose format is depndent on the
  185. flags valuse.
  186. Return Value:
  187. LPCWSTR - Returns a pointer to the formatted string.
  188. --*/
  189. {
  190. static
  191. WCHAR Buffer[ MAX_CHARS ];
  192. DWORD Count;
  193. DbgPointerAssert( Format );
  194. //
  195. // Format the string.
  196. //
  197. Count = FormatMessageW(
  198. Flags
  199. | FORMAT_MESSAGE_FROM_STRING
  200. & ~FORMAT_MESSAGE_FROM_HMODULE,
  201. ( LPVOID ) Format,
  202. 0,
  203. 0,
  204. Buffer,
  205. sizeof( Buffer ),
  206. Args
  207. );
  208. DbgAssert( Count != 0 );
  209. //
  210. // Return the formatted string.
  211. //
  212. return Buffer;
  213. }
  214. #endif // DBG