blob: 5c8c4900c88d4c1a8df47035c410411c663b4d86 [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 Dionnedd059fa2022-11-14 10:33:03 -100038Iterator bounds checking
39------------------------
40The library provides iterators that ensure they are within the bounds of their container when dereferenced.
41Arithmetic can be performed on these iterators to create out-of-bounds iterators, but they cannot be dereferenced
42when out-of-bounds. The following classes currently provide iterators that have bounds checking:
43
44- ``std::string``
45- ``std::vector<T>`` (``T != bool``)
46- ``std::span``
47
48.. TODO: Add support for iterator bounds checking in ``std::string_view`` and ``std::array``
49
50Iterator ownership checking
51---------------------------
52The library provides iterator ownership checking, which allows catching cases where e.g.
53an iterator from container ``X`` is used as a position to insert into container ``Y``.
54The following classes support iterator ownership checking:
Louis Dionne0a4b2b82021-06-08 16:32:53 -040055
56- ``std::string``
57- ``std::vector<T>`` (``T != bool``)
58- ``std::list``
59- ``std::unordered_map``
60- ``std::unordered_multimap``
61- ``std::unordered_set``
62- ``std::unordered_multiset``
63
Louis Dionne510450b2022-04-01 16:38:30 -040064Randomizing unspecified behavior
65--------------------------------
66The library supports the randomization of unspecified behavior. For example, randomizing
67the relative order of equal elements in ``std::sort`` or randomizing both parts of the
68partition after calling ``std::nth_element``. This effort helps migrating to potential
69future faster versions of these algorithms that might not have the exact same behavior.
70In particular, it makes it easier to deflake tests that depend on unspecified behavior.
71A seed can be used to make such failures reproducible: use ``_LIBCPP_DEBUG_RANDOMIZE_UNSPECIFIED_STABILITY_SEED=seed``.