Path: blob/main/contrib/llvm-project/lldb/source/Utility/LLDBAssert.cpp
39587 views
//===-- LLDBAssert.cpp ----------------------------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#include "lldb/Utility/LLDBAssert.h"9#include "llvm/Config/llvm-config.h"10#include "llvm/Support/FormatVariadic.h"11#include "llvm/Support/Signals.h"12#include "llvm/Support/raw_ostream.h"1314#if LLVM_SUPPORT_XCODE_SIGNPOSTS15#include <os/log.h>16#endif1718#include <atomic>1920namespace lldb_private {2122static void DefaultAssertCallback(llvm::StringRef message,23llvm::StringRef backtrace,24llvm::StringRef prompt) {25llvm::errs() << message << '\n';26llvm::errs() << backtrace; // Backtrace includes a newline.27llvm::errs() << prompt << '\n';28}2930static std::atomic<LLDBAssertCallback> g_lldb_assert_callback =31&DefaultAssertCallback;3233void lldb_assert(bool expression, const char *expr_text, const char *func,34const char *file, unsigned int line) {35if (LLVM_LIKELY(expression))36return;3738#if LLVM_SUPPORT_XCODE_SIGNPOSTS39if (__builtin_available(macos 10.12, iOS 10, tvOS 10, watchOS 3, *)) {40os_log_fault(OS_LOG_DEFAULT,41"Assertion failed: (%s), function %s, file %s, line %u\n",42expr_text, func, file, line);43}44#endif4546// Print a warning and encourage the user to file a bug report, similar to47// LLVM’s crash handler, and then return execution.48std::string buffer;49llvm::raw_string_ostream backtrace(buffer);50llvm::sys::PrintStackTrace(backtrace);5152(*g_lldb_assert_callback.load())(53llvm::formatv("Assertion failed: ({0}), function {1}, file {2}, line {3}",54expr_text, func, file, line)55.str(),56backtrace.str(),57"Please file a bug report against lldb reporting this failure log, and "58"as many details as possible");59}6061void SetLLDBAssertCallback(LLDBAssertCallback callback) {62g_lldb_assert_callback.exchange(callback);63}6465} // namespace lldb_private666768