LLDB mainline
LLDBAssert.cpp
Go to the documentation of this file.
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
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#include <mutex>
15
16#if LLVM_SUPPORT_XCODE_SIGNPOSTS
17#include <os/log.h>
18#endif
19
20#include <atomic>
21
22namespace lldb_private {
23
24/// The default callback prints to stderr.
25static void DefaultAssertCallback(llvm::StringRef message,
26 llvm::StringRef backtrace,
27 llvm::StringRef prompt) {
28 llvm::errs() << message << '\n';
29 llvm::errs() << backtrace; // Backtrace includes a newline.
30 llvm::errs() << prompt << '\n';
31}
32
33static std::atomic<LLDBAssertCallback> g_lldb_assert_callback =
35
36void _lldb_assert(bool expression, const char *expr_text, const char *func,
37 const char *file, unsigned int line,
38 std::once_flag &once_flag) {
39 if (LLVM_LIKELY(expression))
40 return;
41
42 std::call_once(once_flag, [&]() {
43#if LLVM_SUPPORT_XCODE_SIGNPOSTS
44 if (__builtin_available(macos 10.12, iOS 10, tvOS 10, watchOS 3, *)) {
45 os_log_fault(OS_LOG_DEFAULT,
46 "Assertion failed: (%s), function %s, file %s, line %u\n",
47 expr_text, func, file, line);
48 }
49#endif
50
51 std::string buffer;
52 llvm::raw_string_ostream backtrace(buffer);
53 llvm::sys::PrintStackTrace(backtrace);
54
55 (*g_lldb_assert_callback.load())(
56 llvm::formatv(
57 "Assertion failed: ({0}), function {1}, file {2}, line {3}",
58 expr_text, func, file, line)
59 .str(),
60 buffer,
61 "Please file a bug report against lldb and include the backtrace, the "
62 "version and as many details as possible.");
63 });
64}
65
67 g_lldb_assert_callback.exchange(callback);
68}
69
70} // namespace lldb_private
A class that represents a running process on the host machine.
static void DefaultAssertCallback(llvm::StringRef message, llvm::StringRef backtrace, llvm::StringRef prompt)
The default callback prints to stderr.
void SetLLDBAssertCallback(LLDBAssertCallback callback)
Replace the LLDB assert callback.
void(* LLDBAssertCallback)(llvm::StringRef message, llvm::StringRef backtrace, llvm::StringRef prompt)
The default LLDB assert callback, which prints to stderr.
Definition LLDBAssert.h:47
static std::atomic< LLDBAssertCallback > g_lldb_assert_callback
void _lldb_assert(bool expression, const char *expr_text, const char *func, const char *file, unsigned int line, std::once_flag &once_flag)
Don't use _lldb_assert directly.