1 //===-- LLDBAssert.cpp ----------------------------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "lldb/Utility/LLDBAssert.h"
10 #include "llvm/Config/llvm-config.h"
11 #include "llvm/Support/FormatVariadic.h"
12 #include "llvm/Support/Signals.h"
13 #include "llvm/Support/raw_ostream.h"
15 #if LLVM_SUPPORT_XCODE_SIGNPOSTS
21 namespace lldb_private
{
23 static void DefaultAssertCallback(llvm::StringRef message
,
24 llvm::StringRef backtrace
,
25 llvm::StringRef prompt
) {
26 llvm::errs() << message
<< '\n';
27 llvm::errs() << backtrace
; // Backtrace includes a newline.
28 llvm::errs() << prompt
<< '\n';
31 static std::atomic
<LLDBAssertCallback
> g_lldb_assert_callback
=
32 &DefaultAssertCallback
;
34 void lldb_assert(bool expression
, const char *expr_text
, const char *func
,
35 const char *file
, unsigned int line
) {
36 if (LLVM_LIKELY(expression
))
39 #if LLVM_SUPPORT_XCODE_SIGNPOSTS
40 if (__builtin_available(macos
10.12, iOS
10, tvOS
10, watchOS
3, *)) {
41 os_log_fault(OS_LOG_DEFAULT
,
42 "Assertion failed: (%s), function %s, file %s, line %u\n",
43 expr_text
, func
, file
, line
);
47 // Print a warning and encourage the user to file a bug report, similar to
48 // LLVM’s crash handler, and then return execution.
50 llvm::raw_string_ostream
backtrace(buffer
);
51 llvm::sys::PrintStackTrace(backtrace
);
53 (*g_lldb_assert_callback
.load())(
54 llvm::formatv("Assertion failed: ({0}), function {1}, file {2}, line {3}",
55 expr_text
, func
, file
, line
)
58 "Please file a bug report against lldb reporting this failure log, and "
59 "as many details as possible");
62 void SetLLDBAssertCallback(LLDBAssertCallback callback
) {
63 g_lldb_assert_callback
.exchange(callback
);
66 } // namespace lldb_private