Eric Fiselier | fb82543 | 2016-12-28 04:58:52 +0000 | [diff] [blame] | 1 | ========== |
| 2 | Debug Mode |
| 3 | ========== |
| 4 | |
| 5 | .. contents:: |
Eric Fiselier | 5392399 | 2017-02-05 01:16:25 +0000 | [diff] [blame] | 6 | :local: |
Eric Fiselier | fb82543 | 2016-12-28 04:58:52 +0000 | [diff] [blame] | 7 | |
| 8 | .. _using-debug-mode: |
| 9 | |
Louis Dionne | 0a4b2b8 | 2021-06-08 16:32:53 -0400 | [diff] [blame] | 10 | Using the debug mode |
| 11 | ==================== |
Eric Fiselier | fb82543 | 2016-12-28 04:58:52 +0000 | [diff] [blame] | 12 | |
Louis Dionne | 0a4b2b8 | 2021-06-08 16:32:53 -0400 | [diff] [blame] | 13 | Libc++ provides a debug mode that enables special debugging checks meant to detect |
| 14 | incorrect usage of the standard library. These checks are disabled by default, but |
Eric Fiselier | fb82543 | 2016-12-28 04:58:52 +0000 | [diff] [blame] | 15 | they can be enabled using the ``_LIBCPP_DEBUG`` macro. |
| 16 | |
Louis Dionne | 0a4b2b8 | 2021-06-08 16:32:53 -0400 | [diff] [blame] | 17 | Note that using the debug mode discussed in this document requires that the library |
| 18 | has been compiled with support for the debug mode (see ``LIBCXX_ENABLE_DEBUG_MODE_SUPPORT``). |
Eric Fiselier | fb82543 | 2016-12-28 04:58:52 +0000 | [diff] [blame] | 19 | |
Louis Dionne | 0a4b2b8 | 2021-06-08 16:32:53 -0400 | [diff] [blame] | 20 | Also note that while the debug mode has no effect on libc++'s ABI, it does have broad ODR |
| 21 | implications. Users should compile their whole program at the same debugging level. |
Eric Fiselier | fb82543 | 2016-12-28 04:58:52 +0000 | [diff] [blame] | 22 | |
Louis Dionne | 0a4b2b8 | 2021-06-08 16:32:53 -0400 | [diff] [blame] | 23 | The various levels of checking provided by the debug mode follow. |
Eric Fiselier | fb82543 | 2016-12-28 04:58:52 +0000 | [diff] [blame] | 24 | |
Louis Dionne | 0a4b2b8 | 2021-06-08 16:32:53 -0400 | [diff] [blame] | 25 | No debugging checks (``_LIBCPP_DEBUG`` not defined) |
| 26 | --------------------------------------------------- |
| 27 | When ``_LIBCPP_DEBUG`` is not defined, there are no debugging checks performed by |
| 28 | the library. This is the default. |
Eric Fiselier | fb82543 | 2016-12-28 04:58:52 +0000 | [diff] [blame] | 29 | |
Louis Dionne | 0656936 | 2022-03-07 11:31:45 -0500 | [diff] [blame^] | 30 | Comparator consistency checks (``_LIBCPP_DEBUG == 1``) |
| 31 | ------------------------------------------------------ |
| 32 | Libc++ provides some checks for the consistency of comparators passed to algorithms. Specifically, |
| 33 | many algorithms such as ``binary_search``, ``merge``, ``next_permutation``, and ``sort``, wrap the |
| 34 | user-provided comparator to assert that `!comp(y, x)` whenever `comp(x, y)`. This can cause the |
| 35 | user-provided comparator to be evaluated up to twice as many times as it would be without the |
| 36 | debug mode, and causes the library to violate some of the Standard's complexity clauses. |
Louis Dionne | 0a4b2b8 | 2021-06-08 16:32:53 -0400 | [diff] [blame] | 37 | |
| 38 | Iterator debugging checks (``_LIBCPP_DEBUG == 1``) |
| 39 | -------------------------------------------------- |
| 40 | Defining ``_LIBCPP_DEBUG`` to ``1`` enables "iterator debugging", which provides |
| 41 | additional assertions about the validity of iterators used by the program. |
| 42 | |
| 43 | The following containers and classes support iterator debugging: |
| 44 | |
| 45 | - ``std::string`` |
| 46 | - ``std::vector<T>`` (``T != bool``) |
| 47 | - ``std::list`` |
| 48 | - ``std::unordered_map`` |
| 49 | - ``std::unordered_multimap`` |
| 50 | - ``std::unordered_set`` |
| 51 | - ``std::unordered_multiset`` |
| 52 | |
| 53 | The remaining containers do not currently support iterator debugging. |
| 54 | Patches welcome. |
Eric Fiselier | fb82543 | 2016-12-28 04:58:52 +0000 | [diff] [blame] | 55 | |
Danila Kutenin | a9cbea1 | 2021-11-16 15:48:59 -0500 | [diff] [blame] | 56 | Randomizing Unspecified Behavior (``_LIBCPP_DEBUG == 1``) |
| 57 | --------------------------------------------------------- |
| 58 | This also enables the randomization of unspecified behavior, for |
| 59 | example, for equal elements in ``std::sort`` or randomizing both parts of |
| 60 | the partition after ``std::nth_element`` call. This effort helps you to migrate |
| 61 | to potential future faster versions of these algorithms and deflake your tests |
| 62 | which depend on such behavior. To fix the seed, use |
| 63 | ``_LIBCPP_DEBUG_RANDOMIZE_UNSPECIFIED_STABILITY_SEED=seed`` definition. |
| 64 | |
Eric Fiselier | fb82543 | 2016-12-28 04:58:52 +0000 | [diff] [blame] | 65 | Handling Assertion Failures |
Louis Dionne | 0a4b2b8 | 2021-06-08 16:32:53 -0400 | [diff] [blame] | 66 | =========================== |
Eric Fiselier | fb82543 | 2016-12-28 04:58:52 +0000 | [diff] [blame] | 67 | When a debug assertion fails the assertion handler is called via the |
| 68 | ``std::__libcpp_debug_function`` function pointer. It is possible to override |
Eric Fiselier | ba56b32 | 2019-06-08 04:59:41 +0000 | [diff] [blame] | 69 | this function pointer using a different handler function. Libc++ provides a |
| 70 | the default handler, ``std::__libcpp_abort_debug_handler``, which aborts the |
| 71 | program. The handler may not return. Libc++ can be changed to use a custom |
| 72 | assertion handler as follows. |
Eric Fiselier | fb82543 | 2016-12-28 04:58:52 +0000 | [diff] [blame] | 73 | |
| 74 | .. code-block:: cpp |
| 75 | |
| 76 | #define _LIBCPP_DEBUG 1 |
| 77 | #include <string> |
Eric Fiselier | ba56b32 | 2019-06-08 04:59:41 +0000 | [diff] [blame] | 78 | void my_handler(std::__libcpp_debug_info const&); |
JF Bastien | b7b53ca | 2019-02-04 20:31:13 +0000 | [diff] [blame] | 79 | int main(int, char**) { |
Eric Fiselier | ba56b32 | 2019-06-08 04:59:41 +0000 | [diff] [blame] | 80 | std::__libcpp_debug_function = &my_handler; |
| 81 | |
| 82 | std::string::iterator bad_it; |
| 83 | std::string str("hello world"); |
| 84 | str.insert(bad_it, '!'); // causes debug assertion |
| 85 | // control flow doesn't return |
Eric Fiselier | fb82543 | 2016-12-28 04:58:52 +0000 | [diff] [blame] | 86 | } |