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.

313 lines
8.3 KiB

  1. //+---------------------------------------------------------------------------
  2. // Copyright (C) 1991, Microsoft Corporation.
  3. //
  4. // File: assert.cxx
  5. //
  6. // Contents: Debugging output routines for idsmgr.dll
  7. //
  8. // Functions: Assert
  9. // PopUpError
  10. //
  11. // History: 23-Jul-91 KyleP Created.
  12. // 09-Oct-91 KevinRo Major changes and comments added
  13. // 18-Oct-91 vich moved debug print routines out
  14. // 10-Jun-92 BryanT Switched to w4crt.h instead of wchar.h
  15. // 30-Sep-93 KyleP DEVL obsolete
  16. //
  17. //----------------------------------------------------------------------------
  18. #pragma hdrstop
  19. //
  20. // This one file **always** uses debugging options
  21. //
  22. #if DBG == 1
  23. # include <debnot.h>
  24. # include <sem.hxx>
  25. # include <dllsem.hxx>
  26. extern "C"
  27. {
  28. # include <windows.h>
  29. # include <strsafe.h>
  30. }
  31. unsigned long Win4InfoLevel = DEF_INFOLEVEL;
  32. unsigned long Win4InfoMask = 0xffffffff;
  33. unsigned long Win4AssertLevel = ASSRT_MESSAGE | ASSRT_BREAK | ASSRT_POPUP;
  34. //+------------------------------------------------------------
  35. // Function: vdprintf
  36. //
  37. // Synopsis: Prints debug output using a pointer to the
  38. // variable information. Used primarily by the
  39. // xxDebugOut macros
  40. //
  41. // Arguements:
  42. // ulCompMask -- Component level mask used to determine
  43. // output ability
  44. // pszComp -- String const of component prefix.
  45. // ppszfmt -- Pointer to output format and data
  46. //
  47. //-------------------------------------------------------------
  48. //
  49. // This semaphore is *outside* vdprintf because the compiler isn't smart
  50. // enough to serialize access for construction if it's function-local and
  51. // protected by a guard variable.
  52. //
  53. // KyleP - 20 May, 1993
  54. //
  55. static CDLLStaticMutexSem mxs;
  56. STDAPI_(void) vdprintf(
  57. unsigned long ulCompMask,
  58. char const *pszComp,
  59. char const *ppszfmt,
  60. va_list pargs)
  61. {
  62. char buffer[256];
  63. if ((ulCompMask & DEB_FORCE) == DEB_FORCE ||
  64. ((ulCompMask | Win4InfoLevel) & Win4InfoMask))
  65. {
  66. mxs.Request();
  67. DWORD tid = GetCurrentThreadId();
  68. DWORD pid = GetCurrentProcessId();
  69. if ((Win4InfoLevel & (DEB_DBGOUT | DEB_STDOUT)) != DEB_STDOUT)
  70. {
  71. if (! (ulCompMask & DEB_NOCOMPNAME))
  72. {
  73. StringCbPrintfA(buffer, sizeof(buffer), "%d.%03d> ", pid, tid );
  74. OutputDebugStringA (buffer);
  75. StringCbPrintfA(buffer, sizeof(buffer), "%s: ", pszComp );
  76. OutputDebugStringA (buffer);
  77. }
  78. StringCbVPrintfA (buffer, sizeof(buffer), ppszfmt, pargs);
  79. OutputDebugStringA (buffer);
  80. }
  81. if (Win4InfoLevel & DEB_STDOUT)
  82. {
  83. if (! (ulCompMask & DEB_NOCOMPNAME))
  84. {
  85. StringCbPrintfA (buffer, sizeof(buffer), "%03d> ", tid );
  86. OutputDebugStringA (buffer);
  87. StringCbPrintfA (buffer, sizeof(buffer), "%s: ", pszComp);
  88. OutputDebugStringA (buffer);
  89. }
  90. StringCbVPrintfA(buffer, sizeof(buffer), ppszfmt, pargs);
  91. OutputDebugStringA (buffer);
  92. }
  93. mxs.Release();
  94. }
  95. }
  96. //+---------------------------------------------------------------------------
  97. //
  98. // Function: _asdprintf
  99. //
  100. // Synopsis: Calls vdprintf to output a formatted message.
  101. //
  102. // History: 18-Oct-91 vich Created
  103. //
  104. //----------------------------------------------------------------------------
  105. inline void __cdecl _asdprintf( char const *pszfmt, ...)
  106. {
  107. va_list va;
  108. va_start(va, pszfmt);
  109. vdprintf(DEB_FORCE, "Assert", pszfmt, va);
  110. va_end(va);
  111. }
  112. //+---------------------------------------------------------------------------
  113. //
  114. // Function: _Win4Assert, private
  115. //
  116. // Synopsis: Display assertion information
  117. //
  118. // Effects: Called when an assertion is hit.
  119. //
  120. // History: 12-Jul-91 AlexT Created.
  121. // 05-Sep-91 AlexT Catch Throws and Catches
  122. // 19-Oct-92 HoiV Added events for TOM
  123. //
  124. //----------------------------------------------------------------------------
  125. STDAPI_(void) Win4AssertEx(
  126. char const * szFile,
  127. int iLine,
  128. char const * szMessage)
  129. {
  130. if (Win4AssertLevel & ASSRT_MESSAGE)
  131. {
  132. DWORD tid = GetCurrentThreadId();
  133. _asdprintf("%s File: %s Line: %u, thread id %d\n",
  134. szMessage, szFile, iLine, tid);
  135. }
  136. if (Win4AssertLevel & ASSRT_POPUP)
  137. {
  138. int id = PopUpError(szMessage,iLine,szFile);
  139. if (id == IDCANCEL)
  140. {
  141. DebugBreak();
  142. }
  143. }
  144. else if (Win4AssertLevel & ASSRT_BREAK)
  145. {
  146. DebugBreak();
  147. }
  148. }
  149. //+------------------------------------------------------------
  150. // Function: SetWin4InfoLevel(unsigned long ulNewLevel)
  151. //
  152. // Synopsis: Sets the global info level for debugging output
  153. // Returns: Old info level
  154. //
  155. //-------------------------------------------------------------
  156. EXPORTIMP unsigned long APINOT
  157. SetWin4InfoLevel(
  158. unsigned long ulNewLevel)
  159. {
  160. unsigned long ul;
  161. ul = Win4InfoLevel;
  162. Win4InfoLevel = ulNewLevel;
  163. return(ul);
  164. }
  165. //+------------------------------------------------------------
  166. // Function: _SetWin4InfoMask(unsigned long ulNewMask)
  167. //
  168. // Synopsis: Sets the global info mask for debugging output
  169. // Returns: Old info mask
  170. //
  171. //-------------------------------------------------------------
  172. EXPORTIMP unsigned long APINOT
  173. SetWin4InfoMask(
  174. unsigned long ulNewMask)
  175. {
  176. unsigned long ul;
  177. ul = Win4InfoMask;
  178. Win4InfoMask = ulNewMask;
  179. return(ul);
  180. }
  181. //+------------------------------------------------------------
  182. // Function: _SetWin4AssertLevel(unsigned long ulNewLevel)
  183. //
  184. // Synopsis: Sets the global assert level for debugging output
  185. // Returns: Old assert level
  186. //
  187. //-------------------------------------------------------------
  188. EXPORTIMP unsigned long APINOT
  189. SetWin4AssertLevel(
  190. unsigned long ulNewLevel)
  191. {
  192. unsigned long ul;
  193. ul = Win4AssertLevel;
  194. Win4AssertLevel = ulNewLevel;
  195. return(ul);
  196. }
  197. //+------------------------------------------------------------
  198. // Function: PopUpError
  199. //
  200. // Synopsis: Displays a dialog box using provided text,
  201. // and presents the user with the option to
  202. // continue or cancel.
  203. //
  204. // Arguments:
  205. // szMsg -- The string to display in main body of dialog
  206. // iLine -- Line number of file in error
  207. // szFile -- Filename of file in error
  208. //
  209. // Returns:
  210. // IDCANCEL -- User selected the CANCEL button
  211. // IDOK -- User selected the OK button
  212. //-------------------------------------------------------------
  213. STDAPI_(int) PopUpError(
  214. char const *szMsg,
  215. int iLine,
  216. char const *szFile)
  217. {
  218. int id;
  219. char szAssertCaption[MAX_PATH];
  220. char szModuleName[256];
  221. DWORD tid = GetCurrentThreadId();
  222. DWORD pid = GetCurrentProcessId();
  223. char * pszModuleName;
  224. if (GetModuleFileNameA(NULL, szModuleName, sizeof(szModuleName)))
  225. {
  226. pszModuleName = strrchr(szModuleName, '\\');
  227. if (!pszModuleName)
  228. {
  229. pszModuleName = szModuleName;
  230. }
  231. else
  232. {
  233. pszModuleName++;
  234. }
  235. }
  236. else
  237. {
  238. pszModuleName = "Unknown";
  239. }
  240. StringCbPrintfA(szAssertCaption, sizeof(szAssertCaption),
  241. "Process: %s File: %s line %u, thread id %d.%d",
  242. pszModuleName, szFile, iLine, pid, tid);
  243. DWORD dwMessageFlags = MB_SETFOREGROUND | MB_TASKMODAL |
  244. MB_ICONEXCLAMATION | MB_OKCANCEL;
  245. id = MessageBoxA(NULL,(char *) szMsg, (LPSTR) szAssertCaption,
  246. dwMessageFlags);
  247. // If id == 0, then an error occurred. There are two possibilities
  248. // that can cause the error: Access Denied, which means that this
  249. // process does not have access to the default desktop, and everything
  250. // else (usually out of memory). Oh well.
  251. return id;
  252. }
  253. #else
  254. int assertDontUseThisName(void)
  255. {
  256. return 1;
  257. }
  258. #endif // DBG == 1