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.

451 lines
9.9 KiB

  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 yanked from several places to provide debug
  16. // support that works in win 16 and win 32.
  17. //
  18. //
  19. //==========================================================================;
  20. #include <win32.h> // to translate DBG -> DEBUG for NT builds
  21. #ifdef DEBUG
  22. #include <windows.h>
  23. #include <windowsx.h>
  24. #include <stdarg.h>
  25. #define _INC_DEBUG_CODE
  26. #include "debug.h"
  27. //
  28. // since we don't UNICODE our debugging messages, use the ASCII entry
  29. // points regardless of how we are compiled.
  30. //
  31. #ifdef _WIN32
  32. #include <wchar.h>
  33. #else
  34. #define lstrcpyA lstrcpy
  35. #define lstrcatA lstrcat
  36. #define lstrlenA lstrlen
  37. #define GetProfileIntA GetProfileInt
  38. #define OutputDebugStringA OutputDebugString
  39. #define wsprintfA wsprintf
  40. #define MessageBoxA MessageBox
  41. #endif
  42. //
  43. //
  44. //
  45. #define cDbgSpecs (sizeof(aszDbgSpecs) / sizeof(aszDbgSpecs[0]))
  46. BOOL __gfDbgEnabled[cDbgSpecs]; // master enable
  47. UINT __guDbgLevel[cDbgSpecs]; // current debug level
  48. //--------------------------------------------------------------------------;
  49. //
  50. // void DbgVPrintF
  51. //
  52. // Description:
  53. //
  54. //
  55. // Arguments:
  56. // LPSTR szFormat:
  57. //
  58. // va_list va:
  59. //
  60. // Return (void):
  61. // No value is returned.
  62. //
  63. //--------------------------------------------------------------------------;
  64. void FAR CDECL DbgVPrintF
  65. (
  66. LPSTR szFormat,
  67. va_list va
  68. )
  69. {
  70. char ach[DEBUG_MAX_LINE_LEN];
  71. BOOL fDebugBreak = FALSE;
  72. BOOL fPrefix = TRUE;
  73. BOOL fCRLF = TRUE;
  74. ach[0] = '\0';
  75. for (;;)
  76. {
  77. switch (*szFormat)
  78. {
  79. case '!':
  80. fDebugBreak = TRUE;
  81. szFormat++;
  82. continue;
  83. case '`':
  84. fPrefix = FALSE;
  85. szFormat++;
  86. continue;
  87. case '~':
  88. fCRLF = FALSE;
  89. szFormat++;
  90. continue;
  91. }
  92. break;
  93. }
  94. if (fDebugBreak)
  95. {
  96. ach[0] = '\007';
  97. ach[1] = '\0';
  98. }
  99. if (fPrefix)
  100. {
  101. lstrcatA(ach, DEBUG_MODULE_NAME ": ");
  102. }
  103. #ifdef _WIN32
  104. wvsprintfA(ach + lstrlenA(ach), szFormat, va);
  105. #else
  106. wvsprintf(ach + lstrlenA(ach), szFormat, (LPSTR)va);
  107. #endif
  108. if (fCRLF)
  109. {
  110. lstrcatA(ach, "\r\n");
  111. }
  112. OutputDebugStringA(ach);
  113. if (fDebugBreak)
  114. {
  115. DebugBreak();
  116. }
  117. } // DbgVPrintF()
  118. //--------------------------------------------------------------------------;
  119. //
  120. // void dprintfS
  121. //
  122. // Description:
  123. // dprintfS() is called by the DPFS() macro if DEBUG is defined at compile
  124. // time. It is recommended that you only use the DPFS() macro to call
  125. // this function--so you don't have to put #ifdef DEBUG around all
  126. // of your code.
  127. //
  128. // Arguments:
  129. // UINT uDbgSpec:
  130. //
  131. // UINT uDbgLevel:
  132. //
  133. // LPSTR szFormat:
  134. //
  135. // Return (void):
  136. // No value is returned.
  137. //
  138. //--------------------------------------------------------------------------;
  139. void FAR CDECL dprintfS
  140. (
  141. UINT uDbgSpec,
  142. UINT uDbgLevel,
  143. LPSTR szFormat,
  144. ...
  145. )
  146. {
  147. va_list va;
  148. if (!__gfDbgEnabled[uDbgSpec] || (__guDbgLevel[uDbgSpec] < uDbgLevel))
  149. return;
  150. va_start(va, szFormat);
  151. DbgVPrintF(szFormat, va);
  152. va_end(va);
  153. } // dprintf()
  154. //--------------------------------------------------------------------------;
  155. //
  156. // void dprintf
  157. //
  158. // Description:
  159. // dprintf() is called by the DPF() macro if DEBUG is defined at compile
  160. // time. It is recommended that you only use the DPF() macro to call
  161. // this function--so you don't have to put #ifdef DEBUG around all
  162. // of your code.
  163. //
  164. // Arguments:
  165. // UINT uDbgLevel:
  166. //
  167. // LPSTR szFormat:
  168. //
  169. // Return (void):
  170. // No value is returned.
  171. //
  172. //--------------------------------------------------------------------------;
  173. void FAR CDECL dprintf
  174. (
  175. UINT uDbgLevel,
  176. LPSTR szFormat,
  177. ...
  178. )
  179. {
  180. va_list va;
  181. if (!__gfDbgEnabled[dbgNone] || (__guDbgLevel[dbgNone] < uDbgLevel))
  182. return;
  183. va_start(va, szFormat);
  184. DbgVPrintF(szFormat, va);
  185. va_end(va);
  186. } // dprintf()
  187. //--------------------------------------------------------------------------;
  188. //
  189. // BOOL DbgEnable
  190. //
  191. // Description:
  192. //
  193. //
  194. // Arguments:
  195. // BOOL fEnable:
  196. //
  197. // Return (BOOL):
  198. // Returns the previous debugging state.
  199. //
  200. //--------------------------------------------------------------------------;
  201. BOOL WINAPI DbgEnable
  202. (
  203. UINT uDbgSpec,
  204. BOOL fEnable
  205. )
  206. {
  207. BOOL fOldState;
  208. fOldState = __gfDbgEnabled[uDbgSpec];
  209. __gfDbgEnabled[uDbgSpec] = fEnable;
  210. return (fOldState);
  211. } // DbgEnable()
  212. //--------------------------------------------------------------------------;
  213. //
  214. // UINT DbgSetLevel
  215. //
  216. // Description:
  217. //
  218. //
  219. // Arguments:
  220. // UINT uLevel:
  221. //
  222. // Return (UINT):
  223. // Returns the previous debugging level.
  224. //
  225. //--------------------------------------------------------------------------;
  226. UINT WINAPI DbgSetLevel
  227. (
  228. UINT uDbgSpec,
  229. UINT uLevel
  230. )
  231. {
  232. UINT uOldLevel;
  233. uOldLevel = __guDbgLevel[uDbgSpec];
  234. __guDbgLevel[uDbgSpec] = uLevel;
  235. return (uOldLevel);
  236. } // DbgSetLevel()
  237. //--------------------------------------------------------------------------;
  238. //
  239. // UINT DbgGetLevel
  240. //
  241. // Description:
  242. //
  243. //
  244. // Arguments:
  245. // None.
  246. //
  247. // Return (UINT):
  248. // Returns the current debugging level.
  249. //
  250. //--------------------------------------------------------------------------;
  251. UINT WINAPI DbgGetLevel
  252. (
  253. UINT uDbgSpec
  254. )
  255. {
  256. return (__guDbgLevel[uDbgSpec]);
  257. } // DbgGetLevel()
  258. //--------------------------------------------------------------------------;
  259. //
  260. // UINT DbgInitializeSpec
  261. //
  262. // Description:
  263. //
  264. //
  265. // Arguments:
  266. // BOOL fEnable:
  267. //
  268. // Return (UINT):
  269. // Returns the debugging level that was set.
  270. //
  271. //--------------------------------------------------------------------------;
  272. UINT WINAPI DbgInitializeSpec
  273. (
  274. UINT uDbgSpec,
  275. BOOL fEnable
  276. )
  277. {
  278. UINT uLevel;
  279. char szKey[DEBUG_MAX_LINE_LEN];
  280. lstrcpyA(szKey, DEBUG_MODULE_NAME);
  281. lstrcatA(szKey, aszDbgSpecs[uDbgSpec]);
  282. uLevel = GetProfileIntA(DEBUG_SECTION, szKey, (UINT)-1);
  283. if ((UINT)-1 == uLevel)
  284. {
  285. //
  286. // if the debug key is not present, then force debug output to
  287. // be disabled. this way running a debug version of a component
  288. // on a non-debugging machine will not generate output unless
  289. // the debug key exists.
  290. //
  291. uLevel = 0;
  292. fEnable = FALSE;
  293. }
  294. DbgSetLevel(uDbgSpec, uLevel);
  295. DbgEnable(uDbgSpec, fEnable);
  296. return (__guDbgLevel[uDbgSpec]);
  297. } // DbgInitialize()
  298. //--------------------------------------------------------------------------;
  299. //
  300. // VOID DbgInitialize
  301. //
  302. // Description:
  303. //
  304. //
  305. // Arguments:
  306. // BOOL fEnable:
  307. //
  308. // Return (UINT):
  309. // Returns the debugging level that was set.
  310. //
  311. //--------------------------------------------------------------------------;
  312. VOID WINAPI DbgInitialize
  313. (
  314. BOOL fEnable
  315. )
  316. {
  317. UINT i;
  318. for (i=0; i<sizeof(__guDbgLevel)/sizeof(__guDbgLevel[0]); i++)
  319. {
  320. DbgInitializeSpec(i, fEnable);
  321. }
  322. return;
  323. } // DbgInitialize()
  324. //--------------------------------------------------------------------------;
  325. //
  326. // void _Assert
  327. //
  328. // Description:
  329. // This routine is called if the ASSERT macro (defined in debug.h)
  330. // tests an expression that evaluates to FALSE. This routine
  331. // displays an "assertion failed" message box allowing the user to
  332. // abort the program, enter the debugger (the "retry" button), or
  333. // ignore the assertion and continue executing. The message box
  334. // displays the file name and line number of the _Assert() call.
  335. //
  336. // Arguments:
  337. // char * szFile: Filename where assertion occurred.
  338. // int iLine: Line number of assertion.
  339. //
  340. //--------------------------------------------------------------------------;
  341. #ifndef _WIN32
  342. #pragma warning(disable:4704)
  343. #endif
  344. void WINAPI _Assert
  345. (
  346. char * szFile,
  347. int iLine
  348. )
  349. {
  350. static char ach[300]; // debug output (avoid stack overflow)
  351. int id;
  352. #ifndef _WIN32
  353. int iExitCode;
  354. #endif
  355. wsprintfA(ach, "Assertion failed in file %s, line %d. [Press RETRY to debug.]", (LPSTR)szFile, iLine);
  356. id = MessageBoxA(NULL, ach, "Assertion Failed",
  357. MB_SYSTEMMODAL | MB_ICONHAND | MB_ABORTRETRYIGNORE );
  358. switch (id)
  359. {
  360. case IDABORT: // Kill the application.
  361. #ifndef _WIN32
  362. iExitCode = 0;
  363. _asm
  364. {
  365. mov ah, 4Ch
  366. mov al, BYTE PTR iExitCode
  367. int 21h
  368. }
  369. #else
  370. FatalAppExit(0, TEXT("Good Bye"));
  371. #endif // WIN16
  372. break;
  373. case IDRETRY: // Break into the debugger.
  374. DebugBreak();
  375. break;
  376. case IDIGNORE: // Ignore assertion, continue executing.
  377. break;
  378. }
  379. } // _Assert
  380. #ifndef _WIN32
  381. #pragma warning(default:4704)
  382. #endif
  383. #endif // #ifdef DEBUG