xref: /aosp_15_r20/external/libcxx/docs/DesignDocs/DebugMode.rst (revision 58b9f456b02922dfdb1fad8a988d5fd8765ecb80)
1*58b9f456SAndroid Build Coastguard Worker==========
2*58b9f456SAndroid Build Coastguard WorkerDebug Mode
3*58b9f456SAndroid Build Coastguard Worker==========
4*58b9f456SAndroid Build Coastguard Worker
5*58b9f456SAndroid Build Coastguard Worker.. contents::
6*58b9f456SAndroid Build Coastguard Worker   :local:
7*58b9f456SAndroid Build Coastguard Worker
8*58b9f456SAndroid Build Coastguard Worker.. _using-debug-mode:
9*58b9f456SAndroid Build Coastguard Worker
10*58b9f456SAndroid Build Coastguard WorkerUsing Debug Mode
11*58b9f456SAndroid Build Coastguard Worker================
12*58b9f456SAndroid Build Coastguard Worker
13*58b9f456SAndroid Build Coastguard WorkerLibc++ provides a debug mode that enables assertions meant to detect incorrect
14*58b9f456SAndroid Build Coastguard Workerusage of the standard library. By default these assertions are disabled but
15*58b9f456SAndroid Build Coastguard Workerthey can be enabled using the ``_LIBCPP_DEBUG`` macro.
16*58b9f456SAndroid Build Coastguard Worker
17*58b9f456SAndroid Build Coastguard Worker**_LIBCPP_DEBUG** Macro
18*58b9f456SAndroid Build Coastguard Worker-----------------------
19*58b9f456SAndroid Build Coastguard Worker
20*58b9f456SAndroid Build Coastguard Worker**_LIBCPP_DEBUG**:
21*58b9f456SAndroid Build Coastguard Worker  This macro is used to enable assertions and iterator debugging checks within
22*58b9f456SAndroid Build Coastguard Worker  libc++. By default it is undefined.
23*58b9f456SAndroid Build Coastguard Worker
24*58b9f456SAndroid Build Coastguard Worker  **Values**: ``0``, ``1``
25*58b9f456SAndroid Build Coastguard Worker
26*58b9f456SAndroid Build Coastguard Worker  Defining ``_LIBCPP_DEBUG`` to ``0`` or greater enables most of libc++'s
27*58b9f456SAndroid Build Coastguard Worker  assertions. Defining ``_LIBCPP_DEBUG`` to ``1`` enables "iterator debugging"
28*58b9f456SAndroid Build Coastguard Worker  which provides additional assertions about the validity of iterators used by
29*58b9f456SAndroid Build Coastguard Worker  the program.
30*58b9f456SAndroid Build Coastguard Worker
31*58b9f456SAndroid Build Coastguard Worker  Note that this option has no effect on libc++'s ABI
32*58b9f456SAndroid Build Coastguard Worker
33*58b9f456SAndroid Build Coastguard Worker**_LIBCPP_DEBUG_USE_EXCEPTIONS**:
34*58b9f456SAndroid Build Coastguard Worker  When this macro is defined ``_LIBCPP_ASSERT`` failures throw
35*58b9f456SAndroid Build Coastguard Worker  ``__libcpp_debug_exception`` instead of aborting. Additionally this macro
36*58b9f456SAndroid Build Coastguard Worker  disables exception specifications on functions containing ``_LIBCPP_ASSERT``
37*58b9f456SAndroid Build Coastguard Worker  checks. This allows assertion failures to correctly throw through these
38*58b9f456SAndroid Build Coastguard Worker  functions.
39*58b9f456SAndroid Build Coastguard Worker
40*58b9f456SAndroid Build Coastguard WorkerHandling Assertion Failures
41*58b9f456SAndroid Build Coastguard Worker---------------------------
42*58b9f456SAndroid Build Coastguard Worker
43*58b9f456SAndroid Build Coastguard WorkerWhen a debug assertion fails the assertion handler is called via the
44*58b9f456SAndroid Build Coastguard Worker``std::__libcpp_debug_function`` function pointer. It is possible to override
45*58b9f456SAndroid Build Coastguard Workerthis function pointer using a different handler function. Libc++ provides two
46*58b9f456SAndroid Build Coastguard Workerdifferent assertion handlers, the default handler
47*58b9f456SAndroid Build Coastguard Worker``std::__libcpp_abort_debug_handler`` which aborts the program, and
48*58b9f456SAndroid Build Coastguard Worker``std::__libcpp_throw_debug_handler`` which throws an instance of
49*58b9f456SAndroid Build Coastguard Worker``std::__libcpp_debug_exception``. Libc++ can be changed to use the throwing
50*58b9f456SAndroid Build Coastguard Workerassertion handler as follows:
51*58b9f456SAndroid Build Coastguard Worker
52*58b9f456SAndroid Build Coastguard Worker.. code-block:: cpp
53*58b9f456SAndroid Build Coastguard Worker
54*58b9f456SAndroid Build Coastguard Worker  #define _LIBCPP_DEBUG 1
55*58b9f456SAndroid Build Coastguard Worker  #include <string>
56*58b9f456SAndroid Build Coastguard Worker  int main() {
57*58b9f456SAndroid Build Coastguard Worker    std::__libcpp_debug_function = std::__libcpp_throw_debug_function;
58*58b9f456SAndroid Build Coastguard Worker    try {
59*58b9f456SAndroid Build Coastguard Worker      std::string::iterator bad_it;
60*58b9f456SAndroid Build Coastguard Worker      std::string str("hello world");
61*58b9f456SAndroid Build Coastguard Worker      str.insert(bad_it, '!'); // causes debug assertion
62*58b9f456SAndroid Build Coastguard Worker    } catch (std::__libcpp_debug_exception const&) {
63*58b9f456SAndroid Build Coastguard Worker      return EXIT_SUCCESS;
64*58b9f456SAndroid Build Coastguard Worker    }
65*58b9f456SAndroid Build Coastguard Worker    return EXIT_FAILURE;
66*58b9f456SAndroid Build Coastguard Worker  }
67*58b9f456SAndroid Build Coastguard Worker
68*58b9f456SAndroid Build Coastguard WorkerDebug Mode Checks
69*58b9f456SAndroid Build Coastguard Worker=================
70*58b9f456SAndroid Build Coastguard Worker
71*58b9f456SAndroid Build Coastguard WorkerLibc++'s debug mode offers two levels of checking. The first enables various
72*58b9f456SAndroid Build Coastguard Workerprecondition checks throughout libc++. The second additionally enables
73*58b9f456SAndroid Build Coastguard Worker"iterator debugging" which checks the validity of iterators used by the program.
74*58b9f456SAndroid Build Coastguard Worker
75*58b9f456SAndroid Build Coastguard WorkerBasic Checks
76*58b9f456SAndroid Build Coastguard Worker============
77*58b9f456SAndroid Build Coastguard Worker
78*58b9f456SAndroid Build Coastguard WorkerThese checks are enabled when ``_LIBCPP_DEBUG`` is defined to either 0 or 1.
79*58b9f456SAndroid Build Coastguard Worker
80*58b9f456SAndroid Build Coastguard WorkerThe following checks are enabled by ``_LIBCPP_DEBUG``:
81*58b9f456SAndroid Build Coastguard Worker
82*58b9f456SAndroid Build Coastguard Worker  * FIXME: Update this list
83*58b9f456SAndroid Build Coastguard Worker
84*58b9f456SAndroid Build Coastguard WorkerIterator Debugging Checks
85*58b9f456SAndroid Build Coastguard Worker=========================
86*58b9f456SAndroid Build Coastguard Worker
87*58b9f456SAndroid Build Coastguard WorkerThese checks are enabled when ``_LIBCPP_DEBUG`` is defined to 1.
88*58b9f456SAndroid Build Coastguard Worker
89*58b9f456SAndroid Build Coastguard WorkerThe following containers and STL classes support iterator debugging:
90*58b9f456SAndroid Build Coastguard Worker
91*58b9f456SAndroid Build Coastguard Worker  * ``std::string``
92*58b9f456SAndroid Build Coastguard Worker  * ``std::vector<T>`` (``T != bool``)
93*58b9f456SAndroid Build Coastguard Worker  * ``std::list``
94*58b9f456SAndroid Build Coastguard Worker  * ``std::unordered_map``
95*58b9f456SAndroid Build Coastguard Worker  * ``std::unordered_multimap``
96*58b9f456SAndroid Build Coastguard Worker  * ``std::unordered_set``
97*58b9f456SAndroid Build Coastguard Worker  * ``std::unordered_multiset``
98*58b9f456SAndroid Build Coastguard Worker
99*58b9f456SAndroid Build Coastguard WorkerThe remaining containers do not currently support iterator debugging.
100*58b9f456SAndroid Build Coastguard WorkerPatches welcome.
101