debug.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: wx/debug.h
  3. // Purpose: Misc debug functions and macros
  4. // Author: Vadim Zeitlin
  5. // Created: 29/01/98
  6. // Copyright: (c) 1998-2009 Vadim Zeitlin <vadim@wxwidgets.org>
  7. // Licence: wxWindows licence
  8. /////////////////////////////////////////////////////////////////////////////
  9. #ifndef _WX_DEBUG_H_
  10. #define _WX_DEBUG_H_
  11. #if !defined(__WXWINCE__)
  12. #include <assert.h>
  13. #endif // systems without assert.h
  14. #include <limits.h> // for CHAR_BIT used below
  15. #include "wx/chartype.h" // for __TFILE__ and wxChar
  16. #include "wx/cpp.h" // for __WXFUNCTION__
  17. #include "wx/dlimpexp.h" // for WXDLLIMPEXP_FWD_BASE
  18. class WXDLLIMPEXP_FWD_BASE wxString;
  19. class WXDLLIMPEXP_FWD_BASE wxCStrData;
  20. // ----------------------------------------------------------------------------
  21. // Defines controlling the debugging macros
  22. // ----------------------------------------------------------------------------
  23. /*
  24. wxWidgets can be built with several different levels of debug support
  25. specified by the value of wxDEBUG_LEVEL constant:
  26. 0: No assertion macros at all, this should only be used when optimizing
  27. for resource-constrained systems (typically embedded ones).
  28. 1: Default level, most of the assertions are enabled.
  29. 2: Maximal (at least for now): asserts which are "expensive"
  30. (performance-wise) or only make sense for finding errors in wxWidgets
  31. itself, as opposed to bugs in applications using it, are also enabled.
  32. */
  33. // unless wxDEBUG_LEVEL is predefined (by configure or via wx/setup.h under
  34. // Windows), use the default
  35. #if !defined(wxDEBUG_LEVEL)
  36. #define wxDEBUG_LEVEL 1
  37. #endif // !defined(wxDEBUG_LEVEL)
  38. /*
  39. __WXDEBUG__ is defined when wxDEBUG_LEVEL != 0. This is done mostly for
  40. compatibility but it also provides a simpler way to check if asserts and
  41. debug logging is enabled at all.
  42. */
  43. #if wxDEBUG_LEVEL > 0
  44. #ifndef __WXDEBUG__
  45. #define __WXDEBUG__
  46. #endif
  47. #else
  48. #undef __WXDEBUG__
  49. #endif
  50. // Finally there is also a very old WXDEBUG macro not used anywhere at all, it
  51. // is only defined for compatibility.
  52. #ifdef __WXDEBUG__
  53. #if !defined(WXDEBUG) || !WXDEBUG
  54. #undef WXDEBUG
  55. #define WXDEBUG 1
  56. #endif // !WXDEBUG
  57. #endif // __WXDEBUG__
  58. // ----------------------------------------------------------------------------
  59. // Handling assertion failures
  60. // ----------------------------------------------------------------------------
  61. /*
  62. Type for the function called in case of assert failure, see
  63. wxSetAssertHandler().
  64. */
  65. typedef void (*wxAssertHandler_t)(const wxString& file,
  66. int line,
  67. const wxString& func,
  68. const wxString& cond,
  69. const wxString& msg);
  70. #if wxDEBUG_LEVEL
  71. // the global assert handler function, if it is NULL asserts don't check their
  72. // conditions
  73. extern WXDLLIMPEXP_DATA_BASE(wxAssertHandler_t) wxTheAssertHandler;
  74. /*
  75. Sets the function to be called in case of assertion failure.
  76. The default assert handler forwards to wxApp::OnAssertFailure() whose
  77. default behaviour is, in turn, to show the standard assertion failure
  78. dialog if a wxApp object exists or shows the same dialog itself directly
  79. otherwise.
  80. While usually it is enough -- and more convenient -- to just override
  81. OnAssertFailure(), to handle all assertion failures, including those
  82. occurring even before wxApp object creation or after its destruction you
  83. need to provide your assertion handler function.
  84. This function also provides a simple way to disable all asserts: simply
  85. pass NULL pointer to it. Doing this will result in not even evaluating
  86. assert conditions at all, avoiding almost all run-time cost of asserts.
  87. Notice that this function is not MT-safe, so you should call it before
  88. starting any other threads.
  89. The return value of this function is the previous assertion handler. It can
  90. be called after any pre-processing by your handler and can also be restored
  91. later if you uninstall your handler.
  92. */
  93. inline wxAssertHandler_t wxSetAssertHandler(wxAssertHandler_t handler)
  94. {
  95. const wxAssertHandler_t old = wxTheAssertHandler;
  96. wxTheAssertHandler = handler;
  97. return old;
  98. }
  99. /*
  100. Reset the default assert handler.
  101. This may be used to enable asserts, which are disabled by default in this
  102. case, for programs built in release build (NDEBUG defined).
  103. */
  104. extern void WXDLLIMPEXP_BASE wxSetDefaultAssertHandler();
  105. #else // !wxDEBUG_LEVEL
  106. // provide empty stubs in case assertions are completely disabled
  107. //
  108. // NB: can't use WXUNUSED() here as we're included from wx/defs.h before it is
  109. // defined
  110. inline wxAssertHandler_t wxSetAssertHandler(wxAssertHandler_t /* handler */)
  111. {
  112. return NULL;
  113. }
  114. inline void wxSetDefaultAssertHandler() { }
  115. #endif // wxDEBUG_LEVEL/!wxDEBUG_LEVEL
  116. // simply a synonym for wxSetAssertHandler(NULL)
  117. inline void wxDisableAsserts() { wxSetAssertHandler(NULL); }
  118. /*
  119. A macro which disables asserts for applications compiled in release build.
  120. By default, wxIMPLEMENT_APP (or rather wxIMPLEMENT_WXWIN_MAIN) disable the
  121. asserts in the applications compiled in the release build by calling this.
  122. It does nothing if NDEBUG is not defined.
  123. */
  124. #ifdef NDEBUG
  125. #define wxDISABLE_ASSERTS_IN_RELEASE_BUILD() wxDisableAsserts()
  126. #else
  127. #define wxDISABLE_ASSERTS_IN_RELEASE_BUILD()
  128. #endif
  129. #if wxDEBUG_LEVEL
  130. /*
  131. wxOnAssert() is used by the debugging macros defined below. Different
  132. overloads are needed because these macros can be used with or without wxT().
  133. All of them are implemented in src/common/appcmn.cpp and unconditionally
  134. call wxTheAssertHandler so the caller must check that it is non-NULL
  135. (assert macros do it).
  136. */
  137. #if wxUSE_UNICODE
  138. // these overloads are the ones typically used by debugging macros: we have to
  139. // provide wxChar* msg version because it's common to use wxT() in the macros
  140. // and finally, we can't use const wx(char)* msg = NULL, because that would
  141. // be ambiguous
  142. //
  143. // also notice that these functions can't be inline as wxString is not defined
  144. // yet (and can't be as wxString code itself may use assertions)
  145. extern WXDLLIMPEXP_BASE void wxOnAssert(const char *file,
  146. int line,
  147. const char *func,
  148. const char *cond);
  149. extern WXDLLIMPEXP_BASE void wxOnAssert(const char *file,
  150. int line,
  151. const char *func,
  152. const char *cond,
  153. const char *msg);
  154. extern WXDLLIMPEXP_BASE void wxOnAssert(const char *file,
  155. int line,
  156. const char *func,
  157. const char *cond,
  158. const wxChar *msg) ;
  159. #endif /* wxUSE_UNICODE */
  160. // this version is for compatibility with wx 2.8 Unicode build only, we don't
  161. // use it ourselves any more except in ANSI-only build in which case it is all
  162. // we need
  163. extern WXDLLIMPEXP_BASE void wxOnAssert(const wxChar *file,
  164. int line,
  165. const char *func,
  166. const wxChar *cond,
  167. const wxChar *msg = NULL);
  168. // these overloads work when msg passed to debug macro is a string and we
  169. // also have to provide wxCStrData overload to resolve ambiguity which would
  170. // otherwise arise from wxASSERT( s.c_str() )
  171. extern WXDLLIMPEXP_BASE void wxOnAssert(const wxString& file,
  172. int line,
  173. const wxString& func,
  174. const wxString& cond,
  175. const wxString& msg);
  176. extern WXDLLIMPEXP_BASE void wxOnAssert(const wxString& file,
  177. int line,
  178. const wxString& func,
  179. const wxString& cond);
  180. extern WXDLLIMPEXP_BASE void wxOnAssert(const char *file,
  181. int line,
  182. const char *func,
  183. const char *cond,
  184. const wxCStrData& msg);
  185. extern WXDLLIMPEXP_BASE void wxOnAssert(const char *file,
  186. int line,
  187. const char *func,
  188. const char *cond,
  189. const wxString& msg);
  190. #endif // wxDEBUG_LEVEL
  191. // ----------------------------------------------------------------------------
  192. // Debugging macros
  193. // ----------------------------------------------------------------------------
  194. /*
  195. Assertion macros: check if the condition is true and call assert handler
  196. (which will by default notify the user about failure) if it isn't.
  197. wxASSERT and wxFAIL macros as well as wxTrap() function do nothing at all
  198. if wxDEBUG_LEVEL is 0 however they do check their conditions at default
  199. debug level 1, unlike the previous wxWidgets versions.
  200. wxASSERT_LEVEL_2 is meant to be used for "expensive" asserts which should
  201. normally be disabled because they have a big impact on performance and so
  202. this macro only does anything if wxDEBUG_LEVEL >= 2.
  203. */
  204. #if wxDEBUG_LEVEL
  205. // wxTrap() can be used to break into the debugger unconditionally
  206. // (assuming the program is running under debugger, of course).
  207. //
  208. // If possible, we prefer to define it as a macro rather than as a function
  209. // to open the debugger at the position where we trapped and not inside the
  210. // trap function itself which is not very useful.
  211. #if wxCHECK_VISUALC_VERSION(7)
  212. #define wxTrap() __debugbreak()
  213. #else
  214. extern WXDLLIMPEXP_BASE void wxTrap();
  215. #endif // Win VisualC
  216. // Global flag used to indicate that assert macros should call wxTrap(): it
  217. // is set by the default assert handler if the user answers yes to the
  218. // question of whether to trap.
  219. extern WXDLLIMPEXP_DATA_BASE(bool) wxTrapInAssert;
  220. // This macro checks if the condition is true and calls the assert handler
  221. // with the provided message if it isn't and finally traps if the special
  222. // flag indicating that it should do it was set by the handler.
  223. //
  224. // Notice that we don't use the handler return value for compatibility
  225. // reasons (if we changed its return type, we'd need to change wxApp::
  226. // OnAssertFailure() too which would break user code overriding it), hence
  227. // the need for the ugly global flag.
  228. #define wxASSERT_MSG(cond, msg) \
  229. wxSTATEMENT_MACRO_BEGIN \
  230. if ( wxTheAssertHandler && !(cond) && \
  231. (wxOnAssert(__FILE__, __LINE__, __WXFUNCTION__, \
  232. #cond, msg), wxTrapInAssert) ) \
  233. { \
  234. wxTrapInAssert = false; \
  235. wxTrap(); \
  236. } \
  237. wxSTATEMENT_MACRO_END
  238. // a version without any additional message, don't use unless condition
  239. // itself is fully self-explanatory
  240. #define wxASSERT(cond) wxASSERT_MSG(cond, (const char*)NULL)
  241. // wxFAIL is a special form of assert: it always triggers (and so is
  242. // usually used in normally unreachable code)
  243. #define wxFAIL_COND_MSG(cond, msg) \
  244. wxSTATEMENT_MACRO_BEGIN \
  245. if ( wxTheAssertHandler && \
  246. (wxOnAssert(__FILE__, __LINE__, __WXFUNCTION__, \
  247. cond, msg), wxTrapInAssert) ) \
  248. { \
  249. wxTrapInAssert = false; \
  250. wxTrap(); \
  251. } \
  252. wxSTATEMENT_MACRO_END
  253. #define wxFAIL_MSG(msg) wxFAIL_COND_MSG("Assert failure", msg)
  254. #define wxFAIL wxFAIL_MSG((const char*)NULL)
  255. #else // !wxDEBUG_LEVEL
  256. #define wxTrap()
  257. #define wxASSERT(cond)
  258. #define wxASSERT_MSG(cond, msg)
  259. #define wxFAIL
  260. #define wxFAIL_MSG(msg)
  261. #define wxFAIL_COND_MSG(cond, msg)
  262. #endif // wxDEBUG_LEVEL
  263. #if wxDEBUG_LEVEL >= 2
  264. #define wxASSERT_LEVEL_2_MSG(cond, msg) wxASSERT_MSG(cond, msg)
  265. #define wxASSERT_LEVEL_2(cond) wxASSERT(cond)
  266. #else // wxDEBUG_LEVEL < 2
  267. #define wxASSERT_LEVEL_2_MSG(cond, msg)
  268. #define wxASSERT_LEVEL_2(cond)
  269. #endif
  270. // This is simply a wrapper for the standard abort() which is not available
  271. // under all platforms.
  272. //
  273. // It isn't really debug-related but there doesn't seem to be any better place
  274. // for it, so declare it here and define it in appbase.cpp, together with
  275. // wxTrap().
  276. extern void WXDLLIMPEXP_BASE wxAbort();
  277. /*
  278. wxCHECK macros always check their conditions, setting debug level to 0 only
  279. makes them silent in case of failure, otherwise -- including at default
  280. debug level 1 -- they call the assert handler if the condition is false
  281. They are supposed to be used only in invalid situation: for example, an
  282. invalid parameter (e.g. a NULL pointer) is passed to a function. Instead of
  283. dereferencing it and causing core dump the function might use
  284. wxCHECK_RET( p != NULL, "pointer can't be NULL" )
  285. */
  286. // the generic macro: takes the condition to check, the statement to be executed
  287. // in case the condition is false and the message to pass to the assert handler
  288. #define wxCHECK2_MSG(cond, op, msg) \
  289. if ( cond ) \
  290. {} \
  291. else \
  292. { \
  293. wxFAIL_COND_MSG(#cond, msg); \
  294. op; \
  295. } \
  296. struct wxDummyCheckStruct /* just to force a semicolon */
  297. // check which returns with the specified return code if the condition fails
  298. #define wxCHECK_MSG(cond, rc, msg) wxCHECK2_MSG(cond, return rc, msg)
  299. // check that expression is true, "return" if not (also FAILs in debug mode)
  300. #define wxCHECK(cond, rc) wxCHECK_MSG(cond, rc, (const char*)NULL)
  301. // check that expression is true, perform op if not
  302. #define wxCHECK2(cond, op) wxCHECK2_MSG(cond, op, (const char*)NULL)
  303. // special form of wxCHECK2: as wxCHECK, but for use in void functions
  304. //
  305. // NB: there is only one form (with msg parameter) and it's intentional:
  306. // there is no other way to tell the caller what exactly went wrong
  307. // from the void function (of course, the function shouldn't be void
  308. // to begin with...)
  309. #define wxCHECK_RET(cond, msg) wxCHECK2_MSG(cond, return, msg)
  310. // ----------------------------------------------------------------------------
  311. // Compile time asserts
  312. //
  313. // Unlike the normal assert and related macros above which are checked during
  314. // the program run-time the macros below will result in a compilation error if
  315. // the condition they check is false. This is usually used to check the
  316. // expressions containing sizeof()s which cannot be tested with the
  317. // preprocessor. If you can use the #if's, do use them as you can give a more
  318. // detailed error message then.
  319. // ----------------------------------------------------------------------------
  320. /*
  321. How this works (you don't have to understand it to be able to use the
  322. macros): we rely on the fact that it is invalid to define a named bit field
  323. in a struct of width 0. All the rest are just the hacks to minimize the
  324. possibility of the compiler warnings when compiling this macro: in
  325. particular, this is why we define a struct and not an object (which would
  326. result in a warning about unused variable) and a named struct (otherwise we'd
  327. get a warning about an unnamed struct not used to define an object!).
  328. */
  329. #define wxMAKE_UNIQUE_ASSERT_NAME wxMAKE_UNIQUE_NAME(wxAssert_)
  330. /*
  331. The second argument of this macro must be a valid C++ identifier and not a
  332. string. I.e. you should use it like this:
  333. wxCOMPILE_TIME_ASSERT( sizeof(int) >= 2, YourIntsAreTooSmall );
  334. It may be used both within a function and in the global scope.
  335. */
  336. #if defined(__WATCOMC__)
  337. /* avoid "unused symbol" warning */
  338. #define wxCOMPILE_TIME_ASSERT(expr, msg) \
  339. class wxMAKE_UNIQUE_ASSERT_NAME { \
  340. unsigned int msg: expr; \
  341. wxMAKE_UNIQUE_ASSERT_NAME() { wxUnusedVar(msg); } \
  342. }
  343. #elif defined( __VMS )
  344. namespace wxdebug{
  345. // HP aCC cannot deal with missing names for template value parameters
  346. template <bool x> struct STATIC_ASSERTION_FAILURE;
  347. template <> struct STATIC_ASSERTION_FAILURE<true> { enum { value = 1 }; };
  348. // HP aCC cannot deal with missing names for template value parameters
  349. template<int x> struct static_assert_test{};
  350. }
  351. #define WX_JOIN( X, Y ) X##Y
  352. #define WX_STATIC_ASSERT_BOOL_CAST(x) (bool)(x)
  353. #define wxCOMPILE_TIME_ASSERT(expr, msg) \
  354. typedef ::wxdebug::static_assert_test<\
  355. sizeof(::wxdebug::STATIC_ASSERTION_FAILURE< WX_STATIC_ASSERT_BOOL_CAST( expr ) >)>\
  356. WX_JOIN(wx_static_assert_typedef_, __LINE__)
  357. #else
  358. #define wxCOMPILE_TIME_ASSERT(expr, msg) \
  359. struct wxMAKE_UNIQUE_ASSERT_NAME { unsigned int msg: expr; }
  360. #endif
  361. /*
  362. When using VC++ 6 with "Edit and Continue" on, the compiler completely
  363. mishandles __LINE__ and so wxCOMPILE_TIME_ASSERT() doesn't work, provide a
  364. way to make "unique" assert names by specifying a unique prefix explicitly
  365. */
  366. #define wxMAKE_UNIQUE_ASSERT_NAME2(text) wxCONCAT(wxAssert_, text)
  367. #define wxCOMPILE_TIME_ASSERT2(expr, msg, text) \
  368. struct wxMAKE_UNIQUE_ASSERT_NAME2(text) { unsigned int msg: expr; }
  369. // helpers for wxCOMPILE_TIME_ASSERT below, for private use only
  370. #define wxMAKE_BITSIZE_MSG(type, size) type ## SmallerThan ## size ## Bits
  371. // a special case of compile time assert: check that the size of the given type
  372. // is at least the given number of bits
  373. #define wxASSERT_MIN_BITSIZE(type, size) \
  374. wxCOMPILE_TIME_ASSERT(sizeof(type) * CHAR_BIT >= size, \
  375. wxMAKE_BITSIZE_MSG(type, size))
  376. // ----------------------------------------------------------------------------
  377. // other miscellaneous debugger-related functions
  378. // ----------------------------------------------------------------------------
  379. /*
  380. Return true if we're running under debugger.
  381. Currently only really works under Win32 and just returns false elsewhere.
  382. */
  383. #if defined(__WIN32__)
  384. extern bool WXDLLIMPEXP_BASE wxIsDebuggerRunning();
  385. #else // !Mac
  386. inline bool wxIsDebuggerRunning() { return false; }
  387. #endif // Mac/!Mac
  388. // An assert helper used to avoid warning when testing constant expressions,
  389. // i.e. wxASSERT( sizeof(int) == 4 ) can generate a compiler warning about
  390. // expression being always true, but not using
  391. // wxASSERT( wxAssertIsEqual(sizeof(int), 4) )
  392. //
  393. // NB: this is made obsolete by wxCOMPILE_TIME_ASSERT() and should no
  394. // longer be used.
  395. extern bool WXDLLIMPEXP_BASE wxAssertIsEqual(int x, int y);
  396. // Use of wxFalse instead of false suppresses compiler warnings about testing
  397. // constant expression
  398. extern WXDLLIMPEXP_DATA_BASE(const bool) wxFalse;
  399. #define wxAssertFailure wxFalse
  400. // This is similar to WXUNUSED() and useful for parameters which are only used
  401. // in assertions.
  402. #if wxDEBUG_LEVEL
  403. #define WXUNUSED_UNLESS_DEBUG(param) param
  404. #else
  405. #define WXUNUSED_UNLESS_DEBUG(param) WXUNUSED(param)
  406. #endif
  407. #endif // _WX_DEBUG_H_