1// -*- C++ -*-
2//===----------------------------------------------------------------------===//
3//
4// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5// See https://llvm.org/LICENSE.txt for license information.
6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7//
8//===----------------------------------------------------------------------===//
9
10#ifndef _LIBCPP_DEBUGGING
11#define _LIBCPP_DEBUGGING
12
13/*
14// all freestanding
15namespace std {
16 // [debugging.utility], utility
17 void breakpoint() noexcept;
18 void breakpoint_if_debugging() noexcept;
19 bool is_debugger_present() noexcept;
20}
21*/
22
23#if __cplusplus < 201103L && defined(_LIBCPP_USE_FROZEN_CXX03_HEADERS)
24# include <__cxx03/__config>
25#else
26
27# include <__config>
28# include <version>
29
30# if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
31# pragma GCC system_header
32# endif
33
34# if _LIBCPP_STD_VER >= 26
35
36_LIBCPP_BEGIN_NAMESPACE_STD
37
38_LIBCPP_BEGIN_EXPLICIT_ABI_ANNOTATIONS
39
40[[nodiscard]] _LIBCPP_AVAILABILITY_DEBUGGING _LIBCPP_OVERRIDABLE_FUNC_VIS bool is_debugger_present() noexcept;
41
42_LIBCPP_END_EXPLICIT_ABI_ANNOTATIONS
43
44// Functions are marked nodebug and always_inline to force the debugger land in the calling frame upon hitting a
45// breakpoint, rather than in the standard library.
46
47_LIBCPP_NODEBUG _LIBCPP_AVAILABILITY_DEBUGGING _LIBCPP_ALWAYS_INLINE inline void breakpoint() noexcept {
48# if __has_builtin(__builtin_debugtrap)
49 __builtin_debugtrap();
50# else
51 __builtin_trap();
52# endif
53}
54
55_LIBCPP_NODEBUG _LIBCPP_AVAILABILITY_DEBUGGING _LIBCPP_ALWAYS_INLINE inline void breakpoint_if_debugging() noexcept {
56 if (is_debugger_present())
57 breakpoint();
58}
59
60_LIBCPP_END_NAMESPACE_STD
61
62# endif // _LIBCPP_STD_VER >= 26
63
64#endif // __cplusplus < 201103L && defined(_LIBCPP_USE_FROZEN_CXX03_HEADERS)
65
66#endif // _LIBCPP_DEBUGGING
67