DebugMode.rst 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. ==========
  2. Debug Mode
  3. ==========
  4. .. contents::
  5. :local
  6. .. _using-debug-mode:
  7. Using Debug Mode
  8. ================
  9. Libc++ provides a debug mode that enables assertions meant to detect incorrect
  10. usage of the standard library. By default these assertions are disabled but
  11. they can be enabled using the ``_LIBCPP_DEBUG`` macro.
  12. **_LIBCPP_DEBUG** Macro
  13. -----------------------
  14. **_LIBCPP_DEBUG**:
  15. This macro is used to enable assertions and iterator debugging checks within
  16. libc++. By default it is undefined.
  17. **Values**: ``0``, ``1``
  18. Defining ``_LIBCPP_DEBUG`` to ``0`` or greater enables most of libc++'s
  19. assertions. Defining ``_LIBCPP_DEBUG`` to ``1`` enables "iterator debugging"
  20. which provides additional assertions about the validity of iterators used by
  21. the program.
  22. Note that this option has no effect on libc++'s ABI
  23. **_LIBCPP_DEBUG_USE_EXCEPTIONS**:
  24. When this macro is defined ``_LIBCPP_ASSERT`` failures throw
  25. ``__libcpp_debug_exception`` instead of aborting. Additionally this macro
  26. disables exception specifications on functions containing ``_LIBCPP_ASSERT``
  27. checks. This allows assertion failures to correctly throw through these
  28. functions.
  29. Handling Assertion Failures
  30. ---------------------------
  31. When a debug assertion fails the assertion handler is called via the
  32. ``std::__libcpp_debug_function`` function pointer. It is possible to override
  33. this function pointer using a different handler function. Libc++ provides two
  34. different assertion handlers, the default handler
  35. ``std::__libcpp_abort_debug_handler`` which aborts the program, and
  36. ``std::__libcpp_throw_debug_handler`` which throws an instance of
  37. ``std::__libcpp_debug_exception``. Libc++ can be changed to use the throwing
  38. assertion handler as follows:
  39. .. code-block:: cpp
  40. #define _LIBCPP_DEBUG 1
  41. #include <string>
  42. int main() {
  43. std::__libcpp_debug_function = std::__libcpp_throw_debug_function;
  44. try {
  45. std::string::iterator bad_it;
  46. std::string str("hello world");
  47. str.insert(bad_it, '!'); // causes debug assertion
  48. } catch (std::__libcpp_debug_exception const&) {
  49. return EXIT_SUCCESS;
  50. }
  51. return EXIT_FAILURE;
  52. }
  53. Debug Mode Checks
  54. =================
  55. Libc++'s debug mode offers two levels of checking. The first enables various
  56. precondition checks throughout libc++. The second additionally enables
  57. "iterator debugging" which checks the validity of iterators used by the program.
  58. Basic Checks
  59. ============
  60. These checks are enabled when ``_LIBCPP_DEBUG`` is defined to either 0 or 1.
  61. The following checks are enabled by ``_LIBCPP_DEBUG``:
  62. * FIXME: Update this list
  63. Iterator Debugging Checks
  64. =========================
  65. These checks are enabled when ``_LIBCPP_DEBUG`` is defined to 1.
  66. The following containers and STL classes support iterator debugging:
  67. * ``std::string``
  68. * ``std::vector<T>`` (``T != bool``)
  69. * ``std::list``
  70. * ``std::unordered_map``
  71. * ``std::unordered_multimap``
  72. * ``std::unordered_set``
  73. * ``std::unordered_multiset``
  74. The remaining containers do not currently support iterator debugging.
  75. Patches welcome.