1 1.1 joerg ========== 2 1.1 joerg Debug Mode 3 1.1 joerg ========== 4 1.1 joerg 5 1.1 joerg .. contents:: 6 1.1 joerg :local: 7 1.1 joerg 8 1.1 joerg .. _using-debug-mode: 9 1.1 joerg 10 1.1 joerg Using Debug Mode 11 1.1 joerg ================ 12 1.1 joerg 13 1.1 joerg Libc++ provides a debug mode that enables assertions meant to detect incorrect 14 1.1 joerg usage of the standard library. By default these assertions are disabled but 15 1.1 joerg they can be enabled using the ``_LIBCPP_DEBUG`` macro. 16 1.1 joerg 17 1.1 joerg **_LIBCPP_DEBUG** Macro 18 1.1 joerg ----------------------- 19 1.1 joerg 20 1.1 joerg **_LIBCPP_DEBUG**: 21 1.1 joerg This macro is used to enable assertions and iterator debugging checks within 22 1.1 joerg libc++. By default it is undefined. 23 1.1 joerg 24 1.1 joerg **Values**: ``0``, ``1`` 25 1.1 joerg 26 1.1 joerg Defining ``_LIBCPP_DEBUG`` to ``0`` or greater enables most of libc++'s 27 1.1 joerg assertions. Defining ``_LIBCPP_DEBUG`` to ``1`` enables "iterator debugging" 28 1.1 joerg which provides additional assertions about the validity of iterators used by 29 1.1 joerg the program. 30 1.1 joerg 31 1.1 joerg Note that this option has no effect on libc++'s ABI; but it does have broad 32 1.1 joerg ODR implications. Users should compile their whole program at the same 33 1.1 joerg debugging level. 34 1.1 joerg 35 1.1 joerg Handling Assertion Failures 36 1.1 joerg --------------------------- 37 1.1 joerg 38 1.1 joerg When a debug assertion fails the assertion handler is called via the 39 1.1 joerg ``std::__libcpp_debug_function`` function pointer. It is possible to override 40 1.1 joerg this function pointer using a different handler function. Libc++ provides a 41 1.1 joerg the default handler, ``std::__libcpp_abort_debug_handler``, which aborts the 42 1.1 joerg program. The handler may not return. Libc++ can be changed to use a custom 43 1.1 joerg assertion handler as follows. 44 1.1 joerg 45 1.1 joerg .. code-block:: cpp 46 1.1 joerg 47 1.1 joerg #define _LIBCPP_DEBUG 1 48 1.1 joerg #include <string> 49 1.1 joerg void my_handler(std::__libcpp_debug_info const&); 50 1.1 joerg int main(int, char**) { 51 1.1 joerg std::__libcpp_debug_function = &my_handler; 52 1.1 joerg 53 1.1 joerg std::string::iterator bad_it; 54 1.1 joerg std::string str("hello world"); 55 1.1 joerg str.insert(bad_it, '!'); // causes debug assertion 56 1.1 joerg // control flow doesn't return 57 1.1 joerg } 58 1.1 joerg 59 1.1 joerg Debug Mode Checks 60 1.1 joerg ================= 61 1.1 joerg 62 1.1 joerg Libc++'s debug mode offers two levels of checking. The first enables various 63 1.1 joerg precondition checks throughout libc++. The second additionally enables 64 1.1 joerg "iterator debugging" which checks the validity of iterators used by the program. 65 1.1 joerg 66 1.1 joerg Basic Checks 67 1.1 joerg ============ 68 1.1 joerg 69 1.1 joerg These checks are enabled when ``_LIBCPP_DEBUG`` is defined to either 0 or 1. 70 1.1 joerg 71 1.1 joerg The following checks are enabled by ``_LIBCPP_DEBUG``: 72 1.1 joerg 73 1.1 joerg * Many algorithms, such as ``binary_search``, ``merge``, ``next_permutation``, and ``sort``, 74 1.1 joerg wrap the user-provided comparator to assert that `!comp(y, x)` whenever 75 1.1 joerg `comp(x, y)`. This can cause the user-provided comparator to be evaluated 76 1.1 joerg up to twice as many times as it would be without ``_LIBCPP_DEBUG``, and 77 1.1 joerg causes the library to violate some of the Standard's complexity clauses. 78 1.1 joerg 79 1.1 joerg * FIXME: Update this list 80 1.1 joerg 81 1.1 joerg Iterator Debugging Checks 82 1.1 joerg ========================= 83 1.1 joerg 84 1.1 joerg These checks are enabled when ``_LIBCPP_DEBUG`` is defined to 1. 85 1.1 joerg 86 1.1 joerg The following containers and STL classes support iterator debugging: 87 1.1 joerg 88 1.1 joerg * ``std::string`` 89 1.1 joerg * ``std::vector<T>`` (``T != bool``) 90 1.1 joerg * ``std::list`` 91 1.1 joerg * ``std::unordered_map`` 92 1.1 joerg * ``std::unordered_multimap`` 93 1.1 joerg * ``std::unordered_set`` 94 1.1 joerg * ``std::unordered_multiset`` 95 1.1 joerg 96 1.1 joerg The remaining containers do not currently support iterator debugging. 97 1.1 joerg Patches welcome. 98