Counter Strike : Global Offensive Source Code
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.

852 lines
31 KiB

  1. //===== Copyright (c) Valve Corporation, All rights reserved. ========//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //
  7. //====================================================================//
  8. #ifndef DBG_H
  9. #define DBG_H
  10. #if !defined(__SPU__)
  11. #ifdef _WIN32
  12. #pragma once
  13. #endif
  14. #include "tier0/platform.h"
  15. #include "tier0/basetypes.h"
  16. #include "dbgflag.h"
  17. #include "logging.h"
  18. #include <math.h>
  19. #include <stdio.h>
  20. #include <stdarg.h>
  21. //-----------------------------------------------------------------------------
  22. // dll export stuff
  23. //-----------------------------------------------------------------------------
  24. #ifdef TIER0_DLL_EXPORT
  25. #define DBG_INTERFACE DLL_EXPORT
  26. #define DBG_OVERLOAD DLL_GLOBAL_EXPORT
  27. #define DBG_CLASS DLL_CLASS_EXPORT
  28. #else
  29. #define DBG_INTERFACE DLL_IMPORT
  30. #define DBG_OVERLOAD DLL_GLOBAL_IMPORT
  31. #define DBG_CLASS DLL_CLASS_IMPORT
  32. #endif
  33. class Color;
  34. //-----------------------------------------------------------------------------
  35. // Usage model for the Dbg library
  36. //
  37. // 1. Assertions.
  38. //
  39. // Assertions are used to detect and warn about invalid states
  40. //
  41. // To use an assertion, use
  42. //
  43. // Assert( (f == 5) );
  44. // AssertMsg( (f == 5), ("F needs to be %d here!\n", 5) );
  45. // AssertFunc( (f == 5), BadFunc() );
  46. // AssertEquals( f, 5 );
  47. // AssertFloatEquals( f, 5.0f, 1e-3 );
  48. //
  49. // The first will simply report that an assertion failed on a particular
  50. // code file and line. The second version will display a print-f formatted message
  51. // along with the file and line, the third will display a generic message and
  52. // will also cause the function BadFunc to be executed, and the last two
  53. // will report an error if f is not equal to 5 (the last one asserts within
  54. // a particular tolerance).
  55. //
  56. // 2. Code activation
  57. //
  58. // To cause code to be run only in debug builds, use DBG_CODE:
  59. // An example is below.
  60. //
  61. // DBG_CODE(
  62. // {
  63. // int x = 5;
  64. // ++x;
  65. // }
  66. // );
  67. //
  68. // Code can be activated based on the dynamic spew groups also. Use
  69. //
  70. // DBG_DCODE( "group", level,
  71. // { int x = 5; ++x; }
  72. // );
  73. //
  74. // 3. Breaking into the debugger.
  75. //
  76. // To cause an unconditional break into the debugger in debug builds only, use DBG_BREAK
  77. //
  78. // DBG_BREAK();
  79. //
  80. // You can force a break in any build (release or debug) using
  81. //
  82. // DebuggerBreak();
  83. //-----------------------------------------------------------------------------
  84. PLATFORM_INTERFACE void _ExitOnFatalAssert( const tchar* pFile, int line );
  85. #if defined( DBGFLAG_STRINGS_STRIP )
  86. #define DbgFlagMacro_ExitOnFatalAssert( pFile, line ) _ExitOnFatalAssert( "", 0 )
  87. #else
  88. #define DbgFlagMacro_ExitOnFatalAssert( pFile, line ) _ExitOnFatalAssert( pFile, line )
  89. #endif
  90. PLATFORM_INTERFACE bool ShouldUseNewAssertDialog();
  91. PLATFORM_INTERFACE bool SetupWin32ConsoleIO();
  92. // Returns true if they want to break in the debugger.
  93. PLATFORM_INTERFACE bool DoNewAssertDialog( const tchar *pFile, int line, const tchar *pExpression );
  94. #if defined( DBGFLAG_STRINGS_STRIP )
  95. #define DbgFlagMacro_DoNewAssertDialog( pFile, line, pExpression ) DoNewAssertDialog( "", 0, "" )
  96. #else
  97. #define DbgFlagMacro_DoNewAssertDialog( pFile, line, pExpression ) DoNewAssertDialog( pFile, line, pExpression )
  98. #endif
  99. // Allows the assert dialogs to be turned off from code
  100. PLATFORM_INTERFACE bool AreAllAssertsDisabled();
  101. PLATFORM_INTERFACE void SetAllAssertsDisabled( bool bAssertsEnabled );
  102. PLATFORM_INTERFACE bool IsAssertDialogDisabled();
  103. PLATFORM_INTERFACE void SetAssertDialogDisabled( bool bAssertDialogDisabled );
  104. // Provides a callback that is called on asserts regardless of spew levels
  105. typedef void (*AssertFailedNotifyFunc_t)( const char *pchFile, int nLine, const char *pchMessage );
  106. PLATFORM_INTERFACE void SetAssertFailedNotifyFunc( AssertFailedNotifyFunc_t func );
  107. PLATFORM_INTERFACE void CallAssertFailedNotifyFunc( const char *pchFile, int nLine, const char *pchMessage );
  108. #if defined( LINUX )
  109. PLATFORM_INTERFACE void SetAssertDialogParent( struct SDL_Window *window );
  110. PLATFORM_INTERFACE struct SDL_Window * GetAssertDialogParent();
  111. #endif
  112. /* Used to define macros, never use these directly. */
  113. #ifdef _PREFAST_
  114. // When doing /analyze builds define _AssertMsg to be __analysis_assume. This tells
  115. // the compiler to assume that the condition is true, which helps to suppress many
  116. // warnings. This define is done in debug and release builds.
  117. // The unfortunate !! is necessary because otherwise /analyze is incapable of evaluating
  118. // all of the logical expressions that the regular compiler can handle.
  119. // Include _msg in the macro so that format errors in it are detected.
  120. #define _AssertMsg( _exp, _msg, _executeExp, _bFatal ) do { __analysis_assume( !!(_exp) ); _msg; } while (0)
  121. #define _AssertMsgOnce( _exp, _msg, _bFatal ) do { __analysis_assume( !!(_exp) ); _msg; } while (0)
  122. // Force asserts on for /analyze so that we get a __analysis_assume of all of the constraints.
  123. #define DBGFLAG_ASSERT
  124. #define DBGFLAG_ASSERTFATAL
  125. #define DBGFLAG_ASSERTDEBUG
  126. // Define the Q_ASSERT macro to override the QT assert macro so that its asserts
  127. // suppress warnings instead of causing them.
  128. #define Q_ASSERT( _exp ) _AssertMsg( _exp, _T("Assertion Failed: ") _T(#_exp), ((void)0), false )
  129. #else
  130. #define _AssertMsg( _exp, _msg, _executeExp, _bFatal ) \
  131. do { \
  132. if (!(_exp)) \
  133. { \
  134. LoggingResponse_t ret = Log_Assert( "%s (%d) : %s\n", __TFILE__, __LINE__, static_cast<const char*>( _msg ) ); \
  135. CallAssertFailedNotifyFunc( __TFILE__, __LINE__, _msg ); \
  136. _executeExp; \
  137. if ( ret == LR_DEBUGGER ) \
  138. { \
  139. if ( ShouldUseNewAssertDialog() ) \
  140. { \
  141. if ( DbgFlagMacro_DoNewAssertDialog( __TFILE__, __LINE__, _msg ) ) \
  142. DebuggerBreak(); \
  143. } \
  144. if ( _bFatal ) \
  145. DbgFlagMacro_ExitOnFatalAssert( __TFILE__, __LINE__ ); \
  146. } \
  147. } \
  148. } while (0)
  149. #define _AssertMsgOnce( _exp, _msg, _bFatal ) \
  150. do { \
  151. static bool fAsserted; \
  152. if (!fAsserted ) \
  153. { \
  154. _AssertMsg( _exp, _msg, (fAsserted = true), _bFatal ); \
  155. } \
  156. } while (0)
  157. #endif
  158. /* Spew macros... */
  159. // AssertFatal macros
  160. // AssertFatal is used to detect an unrecoverable error condition.
  161. // If enabled, it may display an assert dialog (if DBGFLAG_ASSERTDLG is turned on or running under the debugger),
  162. // and always terminates the application
  163. #ifdef DBGFLAG_ASSERTFATAL
  164. #define AssertFatal( _exp ) _AssertMsg( _exp, _T("Assertion Failed: ") _T(#_exp), ((void)0), true )
  165. #define AssertFatalOnce( _exp ) _AssertMsgOnce( _exp, _T("Assertion Failed: ") _T(#_exp), true )
  166. #define AssertFatalMsg( _exp, _msg ) _AssertMsg( _exp, _msg, ((void)0), true )
  167. #define AssertFatalMsgOnce( _exp, _msg ) _AssertMsgOnce( _exp, _msg, true )
  168. #define AssertFatalFunc( _exp, _f ) _AssertMsg( _exp, _T("Assertion Failed: " _T(#_exp), _f, true )
  169. #define AssertFatalEquals( _exp, _expectedValue ) AssertFatalMsg2( (_exp) == (_expectedValue), _T("Expected %d but got %d!"), (_expectedValue), (_exp) )
  170. #define AssertFatalFloatEquals( _exp, _expectedValue, _tol ) AssertFatalMsg2( fabs((_exp) - (_expectedValue)) <= (_tol), _T("Expected %f but got %f!"), (_expectedValue), (_exp) )
  171. #define VerifyFatal( _exp ) AssertFatal( _exp )
  172. #define VerifyEqualsFatal( _exp, _expectedValue ) AssertFatalEquals( _exp, _expectedValue )
  173. #define DbgVerifyFatal( _exp ) AssertFatal( _exp )
  174. #define AssertFatalMsg1( _exp, _msg, a1 ) AssertFatalMsg( _exp, (const tchar *)(CDbgFmtMsg( _msg, a1 )))
  175. #define AssertFatalMsg2( _exp, _msg, a1, a2 ) AssertFatalMsg( _exp, (const tchar *)(CDbgFmtMsg( _msg, a1, a2 )))
  176. #define AssertFatalMsg3( _exp, _msg, a1, a2, a3 ) AssertFatalMsg( _exp, (const tchar *)(CDbgFmtMsg( _msg, a1, a2, a3 )))
  177. #define AssertFatalMsg4( _exp, _msg, a1, a2, a3, a4 ) AssertFatalMsg( _exp, (const tchar *)(CDbgFmtMsg( _msg, a1, a2, a3, a4 )))
  178. #define AssertFatalMsg5( _exp, _msg, a1, a2, a3, a4, a5 ) AssertFatalMsg( _exp, (const tchar *)(CDbgFmtMsg( _msg, a1, a2, a3, a4, a5 )))
  179. #define AssertFatalMsg6( _exp, _msg, a1, a2, a3, a4, a5, a6 ) AssertFatalMsg( _exp, (const tchar *)(CDbgFmtMsg( _msg, a1, a2, a3, a4, a5, a6 )))
  180. #define AssertFatalMsg6( _exp, _msg, a1, a2, a3, a4, a5, a6 ) AssertFatalMsg( _exp, (const tchar *)(CDbgFmtMsg( _msg, a1, a2, a3, a4, a5, a6 )))
  181. #define AssertFatalMsg7( _exp, _msg, a1, a2, a3, a4, a5, a6, a7 ) AssertFatalMsg( _exp, (const tchar *)(CDbgFmtMsg( _msg, a1, a2, a3, a4, a5, a6, a7 )))
  182. #define AssertFatalMsg8( _exp, _msg, a1, a2, a3, a4, a5, a6, a7, a8 ) AssertFatalMsg( _exp, (const tchar *)(CDbgFmtMsg( _msg, a1, a2, a3, a4, a5, a6, a7, a8 )))
  183. #define AssertFatalMsg9( _exp, _msg, a1, a2, a3, a4, a5, a6, a7, a8, a9 ) AssertFatalMsg( _exp, (const tchar *)(CDbgFmtMsg( _msg, a1, a2, a3, a4, a5, a6, a7, a8, a9 )))
  184. #else // DBGFLAG_ASSERTFATAL
  185. #define AssertFatal( _exp ) ((void)0)
  186. #define AssertFatalOnce( _exp ) ((void)0)
  187. #define AssertFatalMsg( _exp, _msg ) ((void)0)
  188. #define AssertFatalMsgOnce( _exp, _msg ) ((void)0)
  189. #define AssertFatalFunc( _exp, _f ) ((void)0)
  190. #define AssertFatalEquals( _exp, _expectedValue ) ((void)0)
  191. #define AssertFatalFloatEquals( _exp, _expectedValue, _tol ) ((void)0)
  192. #define VerifyFatal( _exp ) (_exp)
  193. #define VerifyEqualsFatal( _exp, _expectedValue ) (_exp)
  194. #define DbgVerifyFatal( _exp ) (_exp)
  195. #define AssertFatalMsg1( _exp, _msg, a1 ) ((void)0)
  196. #define AssertFatalMsg2( _exp, _msg, a1, a2 ) ((void)0)
  197. #define AssertFatalMsg3( _exp, _msg, a1, a2, a3 ) ((void)0)
  198. #define AssertFatalMsg4( _exp, _msg, a1, a2, a3, a4 ) ((void)0)
  199. #define AssertFatalMsg5( _exp, _msg, a1, a2, a3, a4, a5 ) ((void)0)
  200. #define AssertFatalMsg6( _exp, _msg, a1, a2, a3, a4, a5, a6 ) ((void)0)
  201. #define AssertFatalMsg6( _exp, _msg, a1, a2, a3, a4, a5, a6 ) ((void)0)
  202. #define AssertFatalMsg7( _exp, _msg, a1, a2, a3, a4, a5, a6, a7 ) ((void)0)
  203. #define AssertFatalMsg8( _exp, _msg, a1, a2, a3, a4, a5, a6, a7, a8 ) ((void)0)
  204. #define AssertFatalMsg9( _exp, _msg, a1, a2, a3, a4, a5, a6, a7, a8, a9 ) ((void)0)
  205. #endif // DBGFLAG_ASSERTFATAL
  206. // lightweight assert macros: in theory, can be run in release without slowing it down
  207. #if defined(_CERT) || defined(_RETAIL)
  208. #define AssertAligned(PTR)
  209. #define AssertAlignedWidth(PTR, width)
  210. #define AssertAlignedConsole(PTR)
  211. #else
  212. # if defined( _X360 )
  213. # define AssertAlignedWidth( PTR, width ) __twnei( intp(PTR) & ( width - 1 ), 0 ) // trap if not equal to immediate value (from width mask); unsigned comparison
  214. # define AssertAligned( PTR ) AssertAlignedWidth( PTR, 16 ) // Call above with 16 width defined
  215. # define AssertAlignedConsole( PTR ) AssertAlignedWidth( PTR, 4 ) // Call above with 4 width defined (xbox only for now)
  216. # elif defined( DBGFLAG_ASSERT )
  217. # define AssertAlignedWidth( adr, width ) Assert( ( ( ( intp ) ( adr ) ) & ( width - 1 ) ) == 0 )
  218. # define AssertAligned( adr ) AssertAlignedWidth( adr, 16 )
  219. # define AssertAlignedConsole(adr) // XBox only for now.
  220. # else
  221. # define AssertAlignedWidth(PTR, width)
  222. # define AssertAligned(PTR)
  223. # define AssertAlignedConsole(PTR)
  224. # endif
  225. #endif
  226. // Assert macros
  227. // Assert is used to detect an important but survivable error.
  228. // It's only turned on when DBGFLAG_ASSERT is true.
  229. #ifdef DBGFLAG_ASSERT
  230. #define Assert( _exp ) _AssertMsg( _exp, _T("Assertion Failed: ") _T(#_exp), ((void)0), false )
  231. #define AssertMsg_( _exp, _msg ) _AssertMsg( _exp, _msg, ((void)0), false )
  232. #define AssertOnce( _exp ) _AssertMsgOnce( _exp, _T("Assertion Failed: ") _T(#_exp), false )
  233. #define AssertMsgOnce( _exp, _msg ) _AssertMsgOnce( _exp, _msg, false )
  234. #define AssertFunc( _exp, _f ) _AssertMsg( _exp, _T("Assertion Failed: ") _T(#_exp), _f, false )
  235. #define AssertEquals( _exp, _expectedValue ) AssertMsg2( (_exp) == (_expectedValue), _T("Expected %d but got %d!"), (_expectedValue), (_exp) )
  236. #define AssertFloatEquals( _exp, _expectedValue, _tol ) AssertMsg2( fabs((_exp) - (_expectedValue)) <= (_tol), _T("Expected %f but got %f!"), (_expectedValue), (_exp) )
  237. #define Verify( _exp ) ( _exp )
  238. #define VerifyEquals( _exp, _expectedValue ) AssertEquals( _exp, _expectedValue )
  239. #ifndef DbgVerify
  240. #define DbgVerify( _exp ) Assert( _exp )
  241. #endif
  242. #ifdef _DEBUG
  243. #define DbgAssert( _exp ) Assert( _exp )
  244. #else
  245. #define DbgAssert( _exp ) ((void)0)
  246. #endif
  247. #ifdef _DEBUG
  248. #define DbgAssert( _exp ) Assert( _exp )
  249. #else
  250. #define DbgAssert( _exp ) ((void)0)
  251. #endif
  252. #define AssertMsg( _exp, _msg ) AssertMsg_( _exp, _T( _msg ) )
  253. #define AssertMsg1( _exp, _msg, a1 ) AssertMsg_( _exp, (const tchar *)(CDbgFmtMsg( _T( _msg ), a1 )) )
  254. #define AssertMsg2( _exp, _msg, a1, a2 ) AssertMsg_( _exp, (const tchar *)(CDbgFmtMsg( _T( _msg ), a1, a2 )) )
  255. #define AssertMsg3( _exp, _msg, a1, a2, a3 ) AssertMsg_( _exp, (const tchar *)(CDbgFmtMsg( _T( _msg ), a1, a2, a3 )) )
  256. #define AssertMsg4( _exp, _msg, a1, a2, a3, a4 ) AssertMsg_( _exp, (const tchar *)(CDbgFmtMsg( _T( _msg ), a1, a2, a3, a4 )) )
  257. #define AssertMsg5( _exp, _msg, a1, a2, a3, a4, a5 ) AssertMsg_( _exp, (const tchar *)(CDbgFmtMsg( _T( _msg ), a1, a2, a3, a4, a5 )) )
  258. #define AssertMsg6( _exp, _msg, a1, a2, a3, a4, a5, a6 ) AssertMsg_( _exp, (const tchar *)(CDbgFmtMsg( _T( _msg ), a1, a2, a3, a4, a5, a6 )) )
  259. #define AssertMsg7( _exp, _msg, a1, a2, a3, a4, a5, a6, a7 ) AssertMsg_( _exp, (const tchar *)(CDbgFmtMsg( _T( _msg ), a1, a2, a3, a4, a5, a6, a7 )) )
  260. #define AssertMsg8( _exp, _msg, a1, a2, a3, a4, a5, a6, a7, a8 ) AssertMsg_( _exp, (const tchar *)(CDbgFmtMsg( _T( _msg ), a1, a2, a3, a4, a5, a6, a7, a8 )) )
  261. #define AssertMsg9( _exp, _msg, a1, a2, a3, a4, a5, a6, a7, a8, a9 ) AssertMsg_( _exp, (const tchar *)(CDbgFmtMsg( _T( _msg ), a1, a2, a3, a4, a5, a6, a7, a8, a9 )) )
  262. #else // DBGFLAG_ASSERT
  263. #define Assert( _exp ) ((void)0)
  264. #define AssertOnce( _exp ) ((void)0)
  265. #define AssertMsg( _exp, _msg ) ((void)0)
  266. #define AssertMsgOnce( _exp, _msg ) ((void)0)
  267. #define AssertFunc( _exp, _f ) ((void)0)
  268. #define AssertEquals( _exp, _expectedValue ) ((void)0)
  269. #define AssertFloatEquals( _exp, _expectedValue, _tol ) ((void)0)
  270. #define Verify( _exp ) (_exp)
  271. #define VerifyEquals( _exp, _expectedValue ) (_exp)
  272. #ifndef DbgVerify
  273. #define DbgVerify( _exp ) (_exp)
  274. #endif
  275. #define DbgAssert( _exp ) ((void)0)
  276. #define AssertMsg1( _exp, _msg, a1 ) ((void)0)
  277. #define AssertMsg2( _exp, _msg, a1, a2 ) ((void)0)
  278. #define AssertMsg3( _exp, _msg, a1, a2, a3 ) ((void)0)
  279. #define AssertMsg4( _exp, _msg, a1, a2, a3, a4 ) ((void)0)
  280. #define AssertMsg5( _exp, _msg, a1, a2, a3, a4, a5 ) ((void)0)
  281. #define AssertMsg6( _exp, _msg, a1, a2, a3, a4, a5, a6 ) ((void)0)
  282. #define AssertMsg6( _exp, _msg, a1, a2, a3, a4, a5, a6 ) ((void)0)
  283. #define AssertMsg7( _exp, _msg, a1, a2, a3, a4, a5, a6, a7 ) ((void)0)
  284. #define AssertMsg8( _exp, _msg, a1, a2, a3, a4, a5, a6, a7, a8 ) ((void)0)
  285. #define AssertMsg9( _exp, _msg, a1, a2, a3, a4, a5, a6, a7, a8, a9 ) ((void)0)
  286. #endif // DBGFLAG_ASSERT
  287. // Source2 compatibility macro
  288. #define AssertDbg( X ) DbgAssert( X )
  289. // Use AssertAnalyze when the main purpose is to work around /analyze bugs by
  290. // telling the compiler that a condition is impossible. If DBGFLAG_ASSERT is set
  291. // then these will still be Asserts (just in case). The use of a different macro
  292. // is in order to indicate their purpose.
  293. #define AssertAnalyze( _exp ) Assert( _exp )
  294. #define STRINGIFY_INTERNAL(x) #x
  295. #define STRINGIFY(x) STRINGIFY_INTERNAL(x)
  296. // The Always version of the assert macros are defined even when DBGFLAG_ASSERT is not,
  297. // so they will be available even in release.
  298. #define AssertAlways( _exp ) _AssertMsg( _exp, _T("Assertion Failed: ") _T(#_exp), ((void)0), false )
  299. #define AssertMsgAlways( _exp, _msg ) _AssertMsg( _exp, _msg, ((void)0), false )
  300. #define FILE_LINE_FUNCTION_STRING __FILE__ "(" STRINGIFY(__LINE__) "):" __FUNCTION__ ":"
  301. #define FILE_LINE_STRING __FILE__ "(" STRINGIFY(__LINE__) "):"
  302. #define FUNCTION_LINE_STRING __FUNCTION__ "(" STRINGIFY(__LINE__) "): "
  303. // Handy define for inserting clickable messages into the build output.
  304. // Use like this:
  305. // #pragma MESSAGE("Some message")
  306. #define MESSAGE(msg) message(__FILE__ "(" FUNCTION_LINE_TOSTRING(__LINE__) "): " msg)
  307. //////////////////////////////////////////////////////////////////////////
  308. // Legacy Logging System
  309. //////////////////////////////////////////////////////////////////////////
  310. // Channels which map the legacy logging system to the new system.
  311. // Channel for all default Msg/Warning/Error commands.
  312. PLATFORM_INTERFACE LoggingChannelID_t LOG_GENERAL;
  313. // Channel for all asserts.
  314. DECLARE_LOGGING_CHANNEL( LOG_ASSERT );
  315. // Channel for all ConMsg and ConColorMsg commands.
  316. DECLARE_LOGGING_CHANNEL( LOG_CONSOLE );
  317. // Channel for all DevMsg and DevWarning commands with level < 2.
  318. DECLARE_LOGGING_CHANNEL( LOG_DEVELOPER );
  319. // Channel for ConDMsg commands.
  320. DECLARE_LOGGING_CHANNEL( LOG_DEVELOPER_CONSOLE );
  321. // Channel for all DevMsg and DevWarning commands with level >= 2.
  322. DECLARE_LOGGING_CHANNEL( LOG_DEVELOPER_VERBOSE );
  323. // Legacy logging functions
  324. // These functions do not return.
  325. PLATFORM_INTERFACE void Error( PRINTF_FORMAT_STRING const tchar *pMsg, ... ) FMTFUNCTION( 1, 2 );
  326. PLATFORM_INTERFACE void Error_SpewCallStack( int iMaxCallStackLength, PRINTF_FORMAT_STRING const tchar *pMsg, ... ) FMTFUNCTION( 2, 3 );
  327. #define Plat_FatalError( ... ) do { Log_Error( LOG_GENERAL, ##__VA_ARGS__ ); Plat_ExitProcess( EXIT_FAILURE ); } while( 0 )
  328. #if defined( DBGFLAG_STRINGS_STRIP )
  329. #define Msg( ... ) ((void)0)
  330. #define Warning( ... ) ((void)0)
  331. #define Warning_SpewCallStack( ... ) ((void)0)
  332. #define DevMsg( ... ) ((void)0)
  333. #define DevWarning( ... ) ((void)0)
  334. #define ConColorMsg( ... ) ((void)0)
  335. #define ConMsg( ... ) ((void)0)
  336. #define ConDMsg( ... ) ((void)0)
  337. #define COM_TimestampedLog( ... ) ((void)0)
  338. #else // DBGFLAG_STRINGS_STRIP
  339. PLATFORM_INTERFACE void Msg( PRINTF_FORMAT_STRING const tchar* pMsg, ... ) FMTFUNCTION( 1, 2 );
  340. PLATFORM_INTERFACE void Warning( PRINTF_FORMAT_STRING const tchar *pMsg, ... ) FMTFUNCTION( 1, 2 );
  341. PLATFORM_INTERFACE void Warning_SpewCallStack( int iMaxCallStackLength, PRINTF_FORMAT_STRING const tchar *pMsg, ... ) FMTFUNCTION( 2, 3 );
  342. #ifdef _PS3
  343. PLATFORM_OVERLOAD void DevMsg( int level, PRINTF_FORMAT_STRING const tchar* pMsg, ... ) FMTFUNCTION( 2, 3 );
  344. PLATFORM_OVERLOAD void DevWarning( int level, PRINTF_FORMAT_STRING const tchar *pMsg, ... ) FMTFUNCTION( 2, 3 );
  345. PLATFORM_INTERFACE void DevMsg( PRINTF_FORMAT_STRING const tchar* pMsg, ... ) FMTFUNCTION( 1, 2 );
  346. PLATFORM_INTERFACE void DevWarning( PRINTF_FORMAT_STRING const tchar *pMsg, ... ) FMTFUNCTION( 1, 2 );
  347. PLATFORM_INTERFACE void ConColorMsg( const Color& clr, PRINTF_FORMAT_STRING const tchar* pMsg, ... ) FMTFUNCTION( 2, 3 );
  348. PLATFORM_INTERFACE void ConMsg( PRINTF_FORMAT_STRING const tchar* pMsg, ... ) FMTFUNCTION( 1, 2 );
  349. #else // !_PS3
  350. PLATFORM_INTERFACE void DevMsg( int level, PRINTF_FORMAT_STRING const tchar* pMsg, ... ) FMTFUNCTION( 2, 3 );
  351. PLATFORM_INTERFACE void DevWarning( int level, PRINTF_FORMAT_STRING const tchar *pMsg, ... ) FMTFUNCTION( 2, 3 );
  352. PLATFORM_OVERLOAD void DevMsg( PRINTF_FORMAT_STRING const tchar* pMsg, ... ) FMTFUNCTION( 1, 2 );
  353. PLATFORM_OVERLOAD void DevWarning( PRINTF_FORMAT_STRING const tchar *pMsg, ... ) FMTFUNCTION( 1, 2 );
  354. PLATFORM_OVERLOAD void ConColorMsg( const Color& clr, PRINTF_FORMAT_STRING const tchar* pMsg, ... ) FMTFUNCTION( 2, 3 );
  355. PLATFORM_OVERLOAD void ConMsg( PRINTF_FORMAT_STRING const tchar* pMsg, ... ) FMTFUNCTION( 1, 2 );
  356. #endif // _PS3
  357. PLATFORM_INTERFACE void ConDMsg( PRINTF_FORMAT_STRING const tchar* pMsg, ... ) FMTFUNCTION( 1, 2 );
  358. PLATFORM_INTERFACE void COM_TimestampedLog( PRINTF_FORMAT_STRING char const *fmt, ... ) FMTFUNCTION( 1, 2 );
  359. #endif // DBGFLAG_STRINGS_STRIP
  360. // You can use this macro like a runtime assert macro.
  361. // If the condition fails, then Error is called with the message. This macro is called
  362. // like AssertMsg, where msg must be enclosed in parenthesis:
  363. //
  364. // ErrorIfNot( bCondition, ("a b c %d %d %d", 1, 2, 3) );
  365. #define ErrorIfNot( condition, msg ) \
  366. if ( (condition) ) \
  367. ; \
  368. else \
  369. { \
  370. Error msg; \
  371. }
  372. #ifdef _DEBUG
  373. #define DebugMsg(...) DevMsg(__VA_ARGS__)
  374. #else
  375. #define DebugMsg(...)
  376. #endif
  377. // @TODO: these callstack spew functions are currently disabled in the new logging system. Need to add support for these if desired.
  378. PLATFORM_INTERFACE void _Warning_AlwaysSpewCallStack_Enable( bool bEnable );
  379. PLATFORM_INTERFACE void _Warning_AlwaysSpewCallStack_Length( int iMaxCallStackLength );
  380. PLATFORM_INTERFACE void _Error_AlwaysSpewCallStack_Enable( bool bEnable );
  381. PLATFORM_INTERFACE void _Error_AlwaysSpewCallStack_Length( int iMaxCallStackLength );
  382. /* Code macros, debugger interface */
  383. #ifdef _DEBUG
  384. #define DBG_CODE( _code ) if (0) ; else { _code }
  385. #define DBG_CODE_NOSCOPE( _code ) _code
  386. #define DBG_DCODE( _g, _l, _code ) if (IsSpewActive( _g, _l )) { _code } else {}
  387. #define DBG_BREAK() DebuggerBreak() /* defined in platform.h */
  388. #else /* not _DEBUG */
  389. #define DBG_CODE( _code ) ((void)0)
  390. #define DBG_CODE_NOSCOPE( _code )
  391. #define DBG_DCODE( _g, _l, _code ) ((void)0)
  392. #define DBG_BREAK() ((void)0)
  393. #endif /* _DEBUG */
  394. #ifdef _DEBUG
  395. template<typename DEST_POINTER_TYPE, typename SOURCE_POINTER_TYPE>
  396. inline DEST_POINTER_TYPE assert_cast(SOURCE_POINTER_TYPE* pSource)
  397. {
  398. Assert( static_cast<DEST_POINTER_TYPE>(pSource) == dynamic_cast<DEST_POINTER_TYPE>(pSource) );
  399. return static_cast<DEST_POINTER_TYPE>(pSource);
  400. }
  401. #else
  402. #define assert_cast static_cast
  403. #endif
  404. //-----------------------------------------------------------------------------
  405. // Templates to assist in validating pointers:
  406. // Have to use these stubs so we don't have to include windows.h here.
  407. PLATFORM_INTERFACE void _AssertValidReadPtr( void* ptr, int count = 1 );
  408. PLATFORM_INTERFACE void _AssertValidWritePtr( void* ptr, int count = 1 );
  409. PLATFORM_INTERFACE void _AssertValidReadWritePtr( void* ptr, int count = 1 );
  410. PLATFORM_INTERFACE void _AssertValidStringPtr( const tchar* ptr, int maxchar );
  411. #ifdef DBGFLAG_ASSERT
  412. inline void AssertValidStringPtr( const tchar* ptr, int maxchar = 0xFFFFFF ) { _AssertValidStringPtr( ptr, maxchar ); }
  413. template<class T> inline void AssertValidReadPtr( T* ptr, int count = 1 ) { _AssertValidReadPtr( (void*)ptr, count ); }
  414. template<class T> inline void AssertValidWritePtr( T* ptr, int count = 1 ) { _AssertValidWritePtr( (void*)ptr, count ); }
  415. template<class T> inline void AssertValidReadWritePtr( T* ptr, int count = 1 ) { _AssertValidReadWritePtr( (void*)ptr, count ); }
  416. #define AssertValidThis() AssertValidReadWritePtr(this,sizeof(*this))
  417. #else
  418. inline void AssertValidStringPtr( const tchar* ptr, int maxchar = 0xFFFFFF ) { }
  419. template<class T> inline void AssertValidReadPtr( T* ptr, int count = 1 ) { }
  420. template<class T> inline void AssertValidWritePtr( T* ptr, int count = 1 ) { }
  421. template<class T> inline void AssertValidReadWritePtr( T* ptr, int count = 1 ) { }
  422. #define AssertValidThis()
  423. #endif
  424. //-----------------------------------------------------------------------------
  425. // Macro to protect functions that are not reentrant
  426. #ifdef _DEBUG
  427. class CReentryGuard
  428. {
  429. public:
  430. CReentryGuard(int *pSemaphore)
  431. : m_pSemaphore(pSemaphore)
  432. {
  433. ++(*m_pSemaphore);
  434. }
  435. ~CReentryGuard()
  436. {
  437. --(*m_pSemaphore);
  438. }
  439. private:
  440. int *m_pSemaphore;
  441. };
  442. #define ASSERT_NO_REENTRY() \
  443. static int fSemaphore##__LINE__; \
  444. Assert( !fSemaphore##__LINE__ ); \
  445. CReentryGuard ReentryGuard##__LINE__( &fSemaphore##__LINE__ )
  446. #else
  447. #define ASSERT_NO_REENTRY()
  448. #endif
  449. // Tier0 uses these for string functions since this abstraction is normally done in tier1.
  450. #ifdef POSIX
  451. #define Tier0Internal_sntprintf snprintf
  452. #define Tier0Internal_vsntprintf vsnprintf
  453. #define Tier0Internal_vsnprintf vsnprintf
  454. #else
  455. #define Tier0Internal_sntprintf _sntprintf
  456. #define Tier0Internal_vsntprintf _vsntprintf
  457. #define Tier0Internal_vsnprintf _vsnprintf
  458. #endif
  459. //-----------------------------------------------------------------------------
  460. //
  461. // Purpose: Inline string formatter
  462. //
  463. #include "tier0/valve_off.h"
  464. class CDbgFmtMsg
  465. {
  466. public:
  467. CDbgFmtMsg(PRINTF_FORMAT_STRING const tchar *pszFormat, ...)
  468. {
  469. va_list arg_ptr;
  470. va_start(arg_ptr, pszFormat);
  471. Tier0Internal_vsntprintf(m_szBuf, sizeof(m_szBuf)-1, pszFormat, arg_ptr);
  472. va_end(arg_ptr);
  473. m_szBuf[sizeof(m_szBuf)-1] = 0;
  474. }
  475. operator const tchar *() const
  476. {
  477. return m_szBuf;
  478. }
  479. private:
  480. tchar m_szBuf[256];
  481. };
  482. #include "tier0/valve_on.h"
  483. //-----------------------------------------------------------------------------
  484. //
  485. // Purpose: Embed debug info in each file.
  486. //
  487. #if defined( _WIN32 ) && !defined( _X360 )
  488. #ifdef _DEBUG
  489. #pragma comment(compiler)
  490. #endif
  491. #endif
  492. //-----------------------------------------------------------------------------
  493. //
  494. // Purpose: Wrap around a variable to create a simple place to put a breakpoint
  495. //
  496. #ifdef _DEBUG
  497. template< class Type >
  498. class CDataWatcher
  499. {
  500. public:
  501. const Type& operator=( const Type &val )
  502. {
  503. return Set( val );
  504. }
  505. const Type& operator=( const CDataWatcher<Type> &val )
  506. {
  507. return Set( val.m_Value );
  508. }
  509. const Type& Set( const Type &val )
  510. {
  511. // Put your breakpoint here
  512. m_Value = val;
  513. return m_Value;
  514. }
  515. Type& GetForModify()
  516. {
  517. return m_Value;
  518. }
  519. const Type& operator+=( const Type &val )
  520. {
  521. return Set( m_Value + val );
  522. }
  523. const Type& operator-=( const Type &val )
  524. {
  525. return Set( m_Value - val );
  526. }
  527. const Type& operator/=( const Type &val )
  528. {
  529. return Set( m_Value / val );
  530. }
  531. const Type& operator*=( const Type &val )
  532. {
  533. return Set( m_Value * val );
  534. }
  535. const Type& operator^=( const Type &val )
  536. {
  537. return Set( m_Value ^ val );
  538. }
  539. const Type& operator|=( const Type &val )
  540. {
  541. return Set( m_Value | val );
  542. }
  543. const Type& operator++()
  544. {
  545. return (*this += 1);
  546. }
  547. Type operator--()
  548. {
  549. return (*this -= 1);
  550. }
  551. Type operator++( int ) // postfix version..
  552. {
  553. Type val = m_Value;
  554. (*this += 1);
  555. return val;
  556. }
  557. Type operator--( int ) // postfix version..
  558. {
  559. Type val = m_Value;
  560. (*this -= 1);
  561. return val;
  562. }
  563. // For some reason the compiler only generates type conversion warnings for this operator when used like
  564. // CNetworkVarBase<unsigned tchar> = 0x1
  565. // (it warns about converting from an int to an unsigned char).
  566. template< class C >
  567. const Type& operator&=( C val )
  568. {
  569. return Set( m_Value & val );
  570. }
  571. operator const Type&() const
  572. {
  573. return m_Value;
  574. }
  575. const Type& Get() const
  576. {
  577. return m_Value;
  578. }
  579. const Type* operator->() const
  580. {
  581. return &m_Value;
  582. }
  583. Type m_Value;
  584. };
  585. #else
  586. template< class Type >
  587. class CDataWatcher
  588. {
  589. private:
  590. CDataWatcher(); // refuse to compile in non-debug builds
  591. };
  592. #endif
  593. // Code for programmatically setting/unsetting hardware breakpoints (there's probably a 360 and
  594. #ifdef IS_WINDOWS_PC
  595. typedef void * HardwareBreakpointHandle_t;
  596. enum EHardwareBreakpointType
  597. {
  598. BREAKPOINT_EXECUTE = 0,
  599. BREAKPOINT_WRITE,
  600. BREAKPOINT_READWRITE,
  601. };
  602. enum EHardwareBreakpointSize
  603. {
  604. BREAKPOINT_SIZE_1 = 1,
  605. BREAKPOINT_SIZE_2 = 2,
  606. BREAKPOINT_SIZE_4 = 4,
  607. BREAKPOINT_SIZE_8 = 8,
  608. };
  609. PLATFORM_INTERFACE HardwareBreakpointHandle_t SetHardwareBreakpoint( EHardwareBreakpointType eType, EHardwareBreakpointSize eSize, const void *pvLocation );
  610. PLATFORM_INTERFACE bool ClearHardwareBreakpoint( HardwareBreakpointHandle_t handle );
  611. class CHardwareBreakPointScopeGuard
  612. {
  613. public:
  614. CHardwareBreakPointScopeGuard( const void *pvLocation, size_t nLocationSize, EHardwareBreakpointType eType = BREAKPOINT_WRITE )
  615. {
  616. EHardwareBreakpointSize eSize = BREAKPOINT_SIZE_4;
  617. switch ( nLocationSize )
  618. {
  619. case 1:
  620. eSize = BREAKPOINT_SIZE_1;
  621. break;
  622. case 2:
  623. eSize = BREAKPOINT_SIZE_2;
  624. break;
  625. case 4:
  626. eSize = BREAKPOINT_SIZE_4;
  627. break;
  628. case 8:
  629. eSize = BREAKPOINT_SIZE_8;
  630. break;
  631. default:
  632. Warning( _T( "SetHardwareBreakpoint can only work with 1, 2, 4 or 8 byte data fields." ) );
  633. break;
  634. }
  635. m_hBreakPoint = SetHardwareBreakpoint( eType, eSize, pvLocation );
  636. m_bActive = m_hBreakPoint != (HardwareBreakpointHandle_t)0;
  637. }
  638. ~CHardwareBreakPointScopeGuard()
  639. {
  640. Release();
  641. }
  642. void Release()
  643. {
  644. if ( !m_bActive )
  645. return;
  646. ClearHardwareBreakpoint( m_hBreakPoint );
  647. }
  648. private:
  649. bool m_bActive;
  650. HardwareBreakpointHandle_t m_hBreakPoint;
  651. };
  652. #endif // IS_WINDOWS_PC
  653. //-----------------------------------------------------------------------------
  654. #else //#if !defined(__SPU__)
  655. // void these for now
  656. #define Assert( _exp ) ((void)0)
  657. #define AssertOnce( _exp ) ((void)0)
  658. #define AssertMsg( _exp, _msg ) ((void)0)
  659. #define AssertMsgOnce( _exp, _msg ) ((void)0)
  660. #define AssertFunc( _exp, _f ) ((void)0)
  661. #define AssertEquals( _exp, _expectedValue ) ((void)0)
  662. #define AssertFloatEquals( _exp, _expectedValue, _tol ) ((void)0)
  663. #define Verify( _exp ) (_exp)
  664. #define VerifyEquals( _exp, _expectedValue ) (_exp)
  665. #define AssertMsg1( _exp, _msg, a1 ) ((void)0)
  666. #define AssertMsg2( _exp, _msg, a1, a2 ) ((void)0)
  667. #define AssertMsg3( _exp, _msg, a1, a2, a3 ) ((void)0)
  668. #define AssertMsg4( _exp, _msg, a1, a2, a3, a4 ) ((void)0)
  669. #define AssertMsg5( _exp, _msg, a1, a2, a3, a4, a5 ) ((void)0)
  670. #define AssertMsg6( _exp, _msg, a1, a2, a3, a4, a5, a6 ) ((void)0)
  671. #define AssertMsg6( _exp, _msg, a1, a2, a3, a4, a5, a6 ) ((void)0)
  672. #define AssertMsg7( _exp, _msg, a1, a2, a3, a4, a5, a6, a7 ) ((void)0)
  673. #define AssertMsg8( _exp, _msg, a1, a2, a3, a4, a5, a6, a7, a8 ) ((void)0)
  674. #define AssertMsg9( _exp, _msg, a1, a2, a3, a4, a5, a6, a7, a8, a9 ) ((void)0)
  675. #define COMPILE_TIME_ASSERT( pred )
  676. #define ASSERT_INVARIANT( pred )
  677. #define AssertFatal( _exp ) ((void)0)
  678. #define AssertFatalOnce( _exp ) ((void)0)
  679. #define AssertFatalMsg( _exp, _msg ) ((void)0)
  680. #define AssertFatalMsgOnce( _exp, _msg ) ((void)0)
  681. #define AssertFatalFunc( _exp, _f ) ((void)0)
  682. #define AssertFatalEquals( _exp, _expectedValue ) ((void)0)
  683. #define AssertFatalFloatEquals( _exp, _expectedValue, _tol ) ((void)0)
  684. #define VerifyFatal( _exp ) (_exp)
  685. #define VerifyEqualsFatal( _exp, _expectedValue ) (_exp)
  686. #define AssertFatalMsg1( _exp, _msg, a1 ) ((void)0)
  687. #define AssertFatalMsg2( _exp, _msg, a1, a2 ) ((void)0)
  688. #define AssertFatalMsg3( _exp, _msg, a1, a2, a3 ) ((void)0)
  689. #define AssertFatalMsg4( _exp, _msg, a1, a2, a3, a4 ) ((void)0)
  690. #define AssertFatalMsg5( _exp, _msg, a1, a2, a3, a4, a5 ) ((void)0)
  691. #define AssertFatalMsg6( _exp, _msg, a1, a2, a3, a4, a5, a6 ) ((void)0)
  692. #define AssertFatalMsg6( _exp, _msg, a1, a2, a3, a4, a5, a6 ) ((void)0)
  693. #define AssertFatalMsg7( _exp, _msg, a1, a2, a3, a4, a5, a6, a7 ) ((void)0)
  694. #define AssertFatalMsg8( _exp, _msg, a1, a2, a3, a4, a5, a6, a7, a8 ) ((void)0)
  695. #define AssertFatalMsg9( _exp, _msg, a1, a2, a3, a4, a5, a6, a7, a8, a9 ) ((void)0)
  696. #define AssertAligned(PTR)
  697. #endif
  698. #endif /* DBG_H */