Windows NT 4.0 source code leak
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.

343 lines
7.4 KiB

4 years ago
  1. //==========================================================================;
  2. //
  3. // THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
  4. // KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  5. // IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR
  6. // PURPOSE.
  7. //
  8. // Copyright (c) 1992 - 1994 Microsoft Corporation. All Rights Reserved.
  9. //
  10. //--------------------------------------------------------------------------;
  11. //
  12. // debug.c
  13. //
  14. // Description:
  15. // This file contains code to support easy-to-use debugging support.
  16. // All code compiles to nothing if DEBUG is not defined.
  17. //
  18. //
  19. //==========================================================================;
  20. #ifdef DEBUG
  21. #include <windows.h>
  22. #include <windowsx.h>
  23. #include <stdarg.h>
  24. #include "debug.h"
  25. //
  26. // since we don't UNICODE our debugging messages, use the ASCII entry
  27. // points regardless of how we are compiled.
  28. //
  29. #ifndef WIN32
  30. #define lstrcatA lstrcat
  31. #define lstrlenA lstrlen
  32. #define GetProfileIntA GetProfileInt
  33. #define OutputDebugStringA OutputDebugString
  34. #endif
  35. //
  36. //
  37. //
  38. BOOL __gfDbgEnabled = TRUE; // master enable
  39. UINT __guDbgLevel = 0; // current debug level
  40. //--------------------------------------------------------------------------;
  41. //
  42. // void DbgVPrintF
  43. //
  44. // Description:
  45. //
  46. //
  47. // Arguments:
  48. // LPSTR szFormat:
  49. //
  50. // va_list va:
  51. //
  52. // Return (void):
  53. // No value is returned.
  54. //
  55. //--------------------------------------------------------------------------;
  56. void FAR CDECL DbgVPrintF
  57. (
  58. LPSTR szFormat,
  59. va_list va
  60. )
  61. {
  62. char ach[DEBUG_MAX_LINE_LEN];
  63. BOOL fDebugBreak = FALSE;
  64. BOOL fPrefix = TRUE;
  65. BOOL fCRLF = TRUE;
  66. ach[0] = '\0';
  67. for (;;)
  68. {
  69. switch (*szFormat)
  70. {
  71. case '!':
  72. fDebugBreak = TRUE;
  73. szFormat++;
  74. continue;
  75. case '`':
  76. fPrefix = FALSE;
  77. szFormat++;
  78. continue;
  79. case '~':
  80. fCRLF = FALSE;
  81. szFormat++;
  82. continue;
  83. }
  84. break;
  85. }
  86. if (fDebugBreak)
  87. {
  88. ach[0] = '\007';
  89. ach[1] = '\0';
  90. }
  91. if (fPrefix)
  92. {
  93. lstrcatA(ach, DEBUG_MODULE_NAME ": ");
  94. }
  95. #ifdef WIN32
  96. wvsprintfA(ach + lstrlenA(ach), szFormat, va);
  97. #else
  98. wvsprintf(ach + lstrlenA(ach), szFormat, (LPSTR)va);
  99. #endif
  100. if (fCRLF)
  101. {
  102. lstrcatA(ach, "\r\n");
  103. }
  104. OutputDebugStringA(ach);
  105. if (fDebugBreak)
  106. {
  107. DebugBreak();
  108. }
  109. } // DbgVPrintF()
  110. //--------------------------------------------------------------------------;
  111. //
  112. // void dprintf
  113. //
  114. // Description:
  115. // dprintf() is called by the DPF() macro if DEBUG is defined at compile
  116. // time. It is recommended that you only use the DPF() macro to call
  117. // this function--so you don't have to put #ifdef DEBUG around all
  118. // of your code.
  119. //
  120. // Arguments:
  121. // UINT uDbgLevel:
  122. //
  123. // LPSTR szFormat:
  124. //
  125. // Return (void):
  126. // No value is returned.
  127. //
  128. //--------------------------------------------------------------------------;
  129. void FAR CDECL dprintf
  130. (
  131. UINT uDbgLevel,
  132. LPSTR szFormat,
  133. ...
  134. )
  135. {
  136. va_list va;
  137. if (!__gfDbgEnabled || (__guDbgLevel < uDbgLevel))
  138. return;
  139. va_start(va, szFormat);
  140. DbgVPrintF(szFormat, va);
  141. va_end(va);
  142. } // dprintf()
  143. //--------------------------------------------------------------------------;
  144. //
  145. // BOOL DbgEnable
  146. //
  147. // Description:
  148. //
  149. //
  150. // Arguments:
  151. // BOOL fEnable:
  152. //
  153. // Return (BOOL):
  154. // Returns the previous debugging state.
  155. //
  156. //--------------------------------------------------------------------------;
  157. BOOL WINAPI DbgEnable
  158. (
  159. BOOL fEnable
  160. )
  161. {
  162. BOOL fOldState;
  163. fOldState = __gfDbgEnabled;
  164. __gfDbgEnabled = fEnable;
  165. return (fOldState);
  166. } // DbgEnable()
  167. //--------------------------------------------------------------------------;
  168. //
  169. // UINT DbgSetLevel
  170. //
  171. // Description:
  172. //
  173. //
  174. // Arguments:
  175. // UINT uLevel:
  176. //
  177. // Return (UINT):
  178. // Returns the previous debugging level.
  179. //
  180. //--------------------------------------------------------------------------;
  181. UINT WINAPI DbgSetLevel
  182. (
  183. UINT uLevel
  184. )
  185. {
  186. UINT uOldLevel;
  187. uOldLevel = __guDbgLevel;
  188. __guDbgLevel = uLevel;
  189. return (uOldLevel);
  190. } // DbgSetLevel()
  191. //--------------------------------------------------------------------------;
  192. //
  193. // UINT DbgGetLevel
  194. //
  195. // Description:
  196. //
  197. //
  198. // Arguments:
  199. // None.
  200. //
  201. // Return (UINT):
  202. // Returns the current debugging level.
  203. //
  204. //--------------------------------------------------------------------------;
  205. UINT WINAPI DbgGetLevel
  206. (
  207. void
  208. )
  209. {
  210. return (__guDbgLevel);
  211. } // DbgGetLevel()
  212. //--------------------------------------------------------------------------;
  213. //
  214. // UINT DbgInitialize
  215. //
  216. // Description:
  217. //
  218. //
  219. // Arguments:
  220. // BOOL fEnable:
  221. //
  222. // Return (UINT):
  223. // Returns the debugging level that was set.
  224. //
  225. //--------------------------------------------------------------------------;
  226. UINT WINAPI DbgInitialize
  227. (
  228. BOOL fEnable
  229. )
  230. {
  231. UINT uLevel;
  232. uLevel = GetProfileIntA(DEBUG_SECTION, DEBUG_MODULE_NAME, (UINT)-1);
  233. if ((UINT)-1 == uLevel)
  234. {
  235. //
  236. // if the debug key is not present, then force debug output to
  237. // be disabled. this way running a debug version of a component
  238. // on a non-debugging machine will not generate output unless
  239. // the debug key exists.
  240. //
  241. uLevel = 0;
  242. fEnable = FALSE;
  243. }
  244. DbgSetLevel(uLevel);
  245. DbgEnable(fEnable);
  246. return (__guDbgLevel);
  247. } // DbgInitialize()
  248. //--------------------------------------------------------------------------;
  249. //
  250. // void _Assert
  251. //
  252. // Description:
  253. // This routine is called if the ASSERT macro (defined in debug.h)
  254. // tests and expression that evaluates to FALSE. This routine
  255. // displays an "assertion failed" message box allowing the user to
  256. // abort the program, enter the debugger (the "retry" button), or
  257. // ignore the assertion and continue executing. The message box
  258. // displays the file name and line number of the _Assert() call.
  259. //
  260. // Arguments:
  261. // char * szFile: Filename where assertion occurred.
  262. // int iLine: Line number of assertion.
  263. //
  264. //--------------------------------------------------------------------------;
  265. void WINAPI _Assert
  266. (
  267. char * szFile,
  268. int iLine
  269. )
  270. {
  271. static CHAR ach[300]; // debug output (avoid stack overflow)
  272. int id;
  273. wsprintfA(ach, "Assertion failed in file %s, line %d. [Press RETRY to debug.]", (LPSTR)szFile, iLine);
  274. id = MessageBoxA(NULL, ach, "Assertion Failed",
  275. MB_SYSTEMMODAL | MB_ICONHAND | MB_ABORTRETRYIGNORE );
  276. switch (id)
  277. {
  278. case IDABORT: // Kill the application.
  279. FatalAppExit(0, TEXT("Good Bye"));
  280. break;
  281. case IDRETRY: // Break into the debugger.
  282. DebugBreak();
  283. break;
  284. case IDIGNORE: // Ignore assertion, continue executing.
  285. break;
  286. }
  287. } // _Assert
  288. #endif // #ifdef DEBUG