1//===-- LLDBAssert.cpp ----------------------------------------------------===//
2//
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
6//
7//===----------------------------------------------------------------------===//
8
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"
14
15#if LLVM_SUPPORT_XCODE_SIGNPOSTS
16#include <os/log.h>
17#endif
18
19#include <atomic>
20
21namespace lldb_private {
22
23static 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';
29}
30
31static std::atomic<LLDBAssertCallback> g_lldb_assert_callback =
32 &DefaultAssertCallback;
33
34void lldb_assert(bool expression, const char *expr_text, const char *func,
35 const char *file, unsigned int line) {
36 if (LLVM_LIKELY(expression))
37 return;
38
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);
44 }
45#endif
46
47 // Print a warning and encourage the user to file a bug report, similar to
48 // LLVM’s crash handler, and then return execution.
49 std::string buffer;
50 llvm::raw_string_ostream backtrace(buffer);
51 llvm::sys::PrintStackTrace(OS&: backtrace);
52
53 (*g_lldb_assert_callback.load())(
54 llvm::formatv(Fmt: "Assertion failed: ({0}), function {1}, file {2}, line {3}",
55 Vals&: expr_text, Vals&: func, Vals&: file, Vals&: line)
56 .str(),
57 backtrace.str(),
58 "Please file a bug report against lldb reporting this failure log, and "
59 "as many details as possible");
60}
61
62void SetLLDBAssertCallback(LLDBAssertCallback callback) {
63 g_lldb_assert_callback.exchange(p: callback);
64}
65
66} // namespace lldb_private
67

source code of lldb/source/Utility/LLDBAssert.cpp