blob: 9d0e78e7581bd4e58fc55a20a3e112b0aabf2b5e [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
Louis Dionne510450b2022-04-01 16:38:30 -040015they can be enabled by vendors when building the library by using ``LIBCXX_ENABLE_DEBUG_MODE``.
Eric Fiselierfb825432016-12-28 04:58:52 +000016
Louis Dionne510450b2022-04-01 16:38:30 -040017Since the debug mode has ABI implications, users should compile their whole program,
18including any dependent libraries, against a Standard library configured identically
19with respect to the debug mode. In other words, they should not mix code built against
20a Standard library with the debug mode enabled with code built against a Standard library
21where the debug mode is disabled.
Eric Fiselierfb825432016-12-28 04:58:52 +000022
Louis Dionne510450b2022-04-01 16:38:30 -040023Furthermore, users should not rely on a stable ABI being provided when the debug mode is
24enabled -- we reserve the right to change the ABI at any time. If you need a stable ABI
25and still want some level of hardening, you should look into enabling :ref:`assertions <assertions-mode>`
26instead.
Eric Fiselierfb825432016-12-28 04:58:52 +000027
Louis Dionne510450b2022-04-01 16:38:30 -040028The debug mode provides various checks to aid application debugging.
Eric Fiselierfb825432016-12-28 04:58:52 +000029
Louis Dionne510450b2022-04-01 16:38:30 -040030Comparator consistency checks
31-----------------------------
Louis Dionne06569362022-03-07 11:31:45 -050032Libc++ provides some checks for the consistency of comparators passed to algorithms. Specifically,
33many algorithms such as ``binary_search``, ``merge``, ``next_permutation``, and ``sort``, wrap the
34user-provided comparator to assert that `!comp(y, x)` whenever `comp(x, y)`. This can cause the
35user-provided comparator to be evaluated up to twice as many times as it would be without the
36debug mode, and causes the library to violate some of the Standard's complexity clauses.
Louis Dionne0a4b2b82021-06-08 16:32:53 -040037
Louis Dionne510450b2022-04-01 16:38:30 -040038Iterator debugging checks
39-------------------------
40The library contains various assertions to check the validity of iterators used
41by the program. The following containers and classes support iterator debugging:
Louis Dionne0a4b2b82021-06-08 16:32:53 -040042
43- ``std::string``
44- ``std::vector<T>`` (``T != bool``)
45- ``std::list``
46- ``std::unordered_map``
47- ``std::unordered_multimap``
48- ``std::unordered_set``
49- ``std::unordered_multiset``
50
51The remaining containers do not currently support iterator debugging.
52Patches welcome.
Eric Fiselierfb825432016-12-28 04:58:52 +000053
Louis Dionne510450b2022-04-01 16:38:30 -040054Randomizing unspecified behavior
55--------------------------------
56The library supports the randomization of unspecified behavior. For example, randomizing
57the relative order of equal elements in ``std::sort`` or randomizing both parts of the
58partition after calling ``std::nth_element``. This effort helps migrating to potential
59future faster versions of these algorithms that might not have the exact same behavior.
60In particular, it makes it easier to deflake tests that depend on unspecified behavior.
61A seed can be used to make such failures reproducible: use ``_LIBCPP_DEBUG_RANDOMIZE_UNSPECIFIED_STABILITY_SEED=seed``.