DebugMode.rst 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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; but it does have broad
  23. ODR implications. Users should compile their whole program at the same
  24. debugging level.
  25. Handling Assertion Failures
  26. ---------------------------
  27. When a debug assertion fails the assertion handler is called via the
  28. ``std::__libcpp_debug_function`` function pointer. It is possible to override
  29. this function pointer using a different handler function. Libc++ provides a
  30. the default handler, ``std::__libcpp_abort_debug_handler``, which aborts the
  31. program. The handler may not return. Libc++ can be changed to use a custom
  32. assertion handler as follows.
  33. .. code-block:: cpp
  34. #define _LIBCPP_DEBUG 1
  35. #include <string>
  36. void my_handler(std::__libcpp_debug_info const&);
  37. int main(int, char**) {
  38. std::__libcpp_debug_function = &my_handler;
  39. std::string::iterator bad_it;
  40. std::string str("hello world");
  41. str.insert(bad_it, '!'); // causes debug assertion
  42. // control flow doesn't return
  43. }
  44. Debug Mode Checks
  45. =================
  46. Libc++'s debug mode offers two levels of checking. The first enables various
  47. precondition checks throughout libc++. The second additionally enables
  48. "iterator debugging" which checks the validity of iterators used by the program.
  49. Basic Checks
  50. ============
  51. These checks are enabled when ``_LIBCPP_DEBUG`` is defined to either 0 or 1.
  52. The following checks are enabled by ``_LIBCPP_DEBUG``:
  53. * FIXME: Update this list
  54. Iterator Debugging Checks
  55. =========================
  56. These checks are enabled when ``_LIBCPP_DEBUG`` is defined to 1.
  57. The following containers and STL classes support iterator debugging:
  58. * ``std::string``
  59. * ``std::vector<T>`` (``T != bool``)
  60. * ``std::list``
  61. * ``std::unordered_map``
  62. * ``std::unordered_multimap``
  63. * ``std::unordered_set``
  64. * ``std::unordered_multiset``
  65. The remaining containers do not currently support iterator debugging.
  66. Patches welcome.