blob: faf7b8e1b81cd5aa4a288b51930c072100ab2782 [file] [log] [blame]
Eric Fiselierfb825432016-12-28 04:58:52 +00001==========
2Debug Mode
3==========
4
5.. contents::
Eric Fiselier53923992017-02-05 01:16:25 +00006 :local:
Eric Fiselierfb825432016-12-28 04:58:52 +00007
8.. _using-debug-mode:
9
Louis Dionne0a4b2b82021-06-08 16:32:53 -040010Using the debug mode
11====================
Eric Fiselierfb825432016-12-28 04:58:52 +000012
Louis Dionne0a4b2b82021-06-08 16:32:53 -040013Libc++ provides a debug mode that enables special debugging checks meant to detect
14incorrect usage of the standard library. These checks are disabled by default, but
Eric Fiselierfb825432016-12-28 04:58:52 +000015they can be enabled using the ``_LIBCPP_DEBUG`` macro.
16
Louis Dionne0a4b2b82021-06-08 16:32:53 -040017Note that using the debug mode discussed in this document requires that the library
18has been compiled with support for the debug mode (see ``LIBCXX_ENABLE_DEBUG_MODE_SUPPORT``).
Eric Fiselierfb825432016-12-28 04:58:52 +000019
Louis Dionne0a4b2b82021-06-08 16:32:53 -040020Also note that while the debug mode has no effect on libc++'s ABI, it does have broad ODR
21implications. Users should compile their whole program at the same debugging level.
Eric Fiselierfb825432016-12-28 04:58:52 +000022
Louis Dionne0a4b2b82021-06-08 16:32:53 -040023The various levels of checking provided by the debug mode follow.
Eric Fiselierfb825432016-12-28 04:58:52 +000024
Louis Dionne0a4b2b82021-06-08 16:32:53 -040025No debugging checks (``_LIBCPP_DEBUG`` not defined)
26---------------------------------------------------
27When ``_LIBCPP_DEBUG`` is not defined, there are no debugging checks performed by
28the library. This is the default.
Eric Fiselierfb825432016-12-28 04:58:52 +000029
Louis Dionne0a4b2b82021-06-08 16:32:53 -040030Basic checks (``_LIBCPP_DEBUG == 0``)
31-------------------------------------
32When ``_LIBCPP_DEBUG`` is defined to ``0`` (to be understood as level ``0``), some
33debugging checks are enabled. The non-exhaustive list of things is:
34
35- Many algorithms, such as ``binary_search``, ``merge``, ``next_permutation``, and ``sort``,
36 wrap the user-provided comparator to assert that `!comp(y, x)` whenever
37 `comp(x, y)`. This can cause the user-provided comparator to be evaluated
38 up to twice as many times as it would be without ``_LIBCPP_DEBUG``, and
39 causes the library to violate some of the Standard's complexity clauses.
40
41- FIXME: Update this list
42
43Iterator debugging checks (``_LIBCPP_DEBUG == 1``)
44--------------------------------------------------
45Defining ``_LIBCPP_DEBUG`` to ``1`` enables "iterator debugging", which provides
46additional assertions about the validity of iterators used by the program.
47
48The following containers and classes support iterator debugging:
49
50- ``std::string``
51- ``std::vector<T>`` (``T != bool``)
52- ``std::list``
53- ``std::unordered_map``
54- ``std::unordered_multimap``
55- ``std::unordered_set``
56- ``std::unordered_multiset``
57
58The remaining containers do not currently support iterator debugging.
59Patches welcome.
Eric Fiselierfb825432016-12-28 04:58:52 +000060
Danila Kutenina9cbea12021-11-16 15:48:59 -050061Randomizing Unspecified Behavior (``_LIBCPP_DEBUG == 1``)
62---------------------------------------------------------
63This also enables the randomization of unspecified behavior, for
64example, for equal elements in ``std::sort`` or randomizing both parts of
65the partition after ``std::nth_element`` call. This effort helps you to migrate
66to potential future faster versions of these algorithms and deflake your tests
67which depend on such behavior. To fix the seed, use
68``_LIBCPP_DEBUG_RANDOMIZE_UNSPECIFIED_STABILITY_SEED=seed`` definition.
69
Eric Fiselierfb825432016-12-28 04:58:52 +000070Handling Assertion Failures
Louis Dionne0a4b2b82021-06-08 16:32:53 -040071===========================
Eric Fiselierfb825432016-12-28 04:58:52 +000072When a debug assertion fails the assertion handler is called via the
73``std::__libcpp_debug_function`` function pointer. It is possible to override
Eric Fiselierba56b322019-06-08 04:59:41 +000074this function pointer using a different handler function. Libc++ provides a
75the default handler, ``std::__libcpp_abort_debug_handler``, which aborts the
76program. The handler may not return. Libc++ can be changed to use a custom
77assertion handler as follows.
Eric Fiselierfb825432016-12-28 04:58:52 +000078
79.. code-block:: cpp
80
81 #define _LIBCPP_DEBUG 1
82 #include <string>
Eric Fiselierba56b322019-06-08 04:59:41 +000083 void my_handler(std::__libcpp_debug_info const&);
JF Bastienb7b53ca2019-02-04 20:31:13 +000084 int main(int, char**) {
Eric Fiselierba56b322019-06-08 04:59:41 +000085 std::__libcpp_debug_function = &my_handler;
86
87 std::string::iterator bad_it;
88 std::string str("hello world");
89 str.insert(bad_it, '!'); // causes debug assertion
90 // control flow doesn't return
Eric Fiselierfb825432016-12-28 04:58:52 +000091 }