71 lines · cpp
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-exception6//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#include <mutex>15 16#if LLVM_SUPPORT_XCODE_SIGNPOSTS17#include <os/log.h>18#endif19 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 =34 &DefaultAssertCallback;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_SIGNPOSTS44 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#endif50 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 66void SetLLDBAssertCallback(LLDBAssertCallback callback) {67 g_lldb_assert_callback.exchange(callback);68}69 70} // namespace lldb_private71