1dda28197Spatrick //===-- LLDBAssert.cpp ----------------------------------------------------===//
2061da546Spatrick //
3061da546Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4061da546Spatrick // See https://llvm.org/LICENSE.txt for license information.
5061da546Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6061da546Spatrick //
7061da546Spatrick //===----------------------------------------------------------------------===//
8061da546Spatrick 
9061da546Spatrick #include "lldb/Utility/LLDBAssert.h"
10*be691f3bSpatrick #include "llvm/Config/llvm-config.h"
11061da546Spatrick #include "llvm/Support/Format.h"
12061da546Spatrick #include "llvm/Support/Signals.h"
13061da546Spatrick #include "llvm/Support/raw_ostream.h"
14061da546Spatrick 
15*be691f3bSpatrick #if LLVM_SUPPORT_XCODE_SIGNPOSTS
16*be691f3bSpatrick #include <os/log.h>
17*be691f3bSpatrick #endif
18*be691f3bSpatrick 
19061da546Spatrick using namespace llvm;
20061da546Spatrick using namespace lldb_private;
21061da546Spatrick 
lldb_assert(bool expression,const char * expr_text,const char * func,const char * file,unsigned int line)22061da546Spatrick void lldb_private::lldb_assert(bool expression, const char *expr_text,
23061da546Spatrick                                const char *func, const char *file,
24061da546Spatrick                                unsigned int line) {
25061da546Spatrick   if (LLVM_LIKELY(expression))
26061da546Spatrick     return;
27061da546Spatrick 
28061da546Spatrick   // If asserts are enabled abort here.
29061da546Spatrick   assert(false && "lldb_assert failed");
30061da546Spatrick 
31*be691f3bSpatrick #if LLVM_SUPPORT_XCODE_SIGNPOSTS
32*be691f3bSpatrick   if (__builtin_available(macos 10.12, iOS 10, tvOS 10, watchOS 3, *)) {
33*be691f3bSpatrick     os_log_fault(OS_LOG_DEFAULT,
34*be691f3bSpatrick                  "Assertion failed: (%s), function %s, file %s, line %u\n",
35*be691f3bSpatrick                  expr_text, func, file, line);
36*be691f3bSpatrick   }
37*be691f3bSpatrick #endif
38*be691f3bSpatrick 
39061da546Spatrick   // In a release configuration it will print a warning and encourage the user
40061da546Spatrick   // to file a bug report, similar to LLVM’s crash handler, and then return
41061da546Spatrick   // execution.
42061da546Spatrick   errs() << format("Assertion failed: (%s), function %s, file %s, line %u\n",
43061da546Spatrick                    expr_text, func, file, line);
44061da546Spatrick   errs() << "backtrace leading to the failure:\n";
45061da546Spatrick   llvm::sys::PrintStackTrace(errs());
46061da546Spatrick   errs() << "please file a bug report against lldb reporting this failure "
47061da546Spatrick             "log, and as many details as possible\n";
48061da546Spatrick }
49