180814287SRaphael Isemann //===-- LLDBAssert.cpp ----------------------------------------------------===//
253ed89c6SEnrico Granata //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
653ed89c6SEnrico Granata //
753ed89c6SEnrico Granata //===----------------------------------------------------------------------===//
853ed89c6SEnrico Granata 
953ed89c6SEnrico Granata #include "lldb/Utility/LLDBAssert.h"
10*4d593efdSJonas Devlieghere #include "llvm/Config/llvm-config.h"
11a893d301SZachary Turner #include "llvm/Support/Format.h"
12a893d301SZachary Turner #include "llvm/Support/Signals.h"
13b9c1b51eSKate Stone #include "llvm/Support/raw_ostream.h"
14a893d301SZachary Turner 
15e089b5e9SJonas Devlieghere #if LLVM_SUPPORT_XCODE_SIGNPOSTS
16e089b5e9SJonas Devlieghere #include <os/log.h>
17e089b5e9SJonas Devlieghere #endif
18e089b5e9SJonas Devlieghere 
19a893d301SZachary Turner using namespace llvm;
2053ed89c6SEnrico Granata using namespace lldb_private;
2153ed89c6SEnrico Granata 
lldb_assert(bool expression,const char * expr_text,const char * func,const char * file,unsigned int line)22b9c1b51eSKate Stone void lldb_private::lldb_assert(bool expression, const char *expr_text,
23b9c1b51eSKate Stone                                const char *func, const char *file,
24b9c1b51eSKate Stone                                unsigned int line) {
254d46fde6SDavid Bolvansky   if (LLVM_LIKELY(expression))
264d46fde6SDavid Bolvansky     return;
274d46fde6SDavid Bolvansky 
28fd19ffc6SJonas Devlieghere   // If asserts are enabled abort here.
29fd19ffc6SJonas Devlieghere   assert(false && "lldb_assert failed");
30b0fc4d47SJonas Devlieghere 
31e089b5e9SJonas Devlieghere #if LLVM_SUPPORT_XCODE_SIGNPOSTS
32e089b5e9SJonas Devlieghere   if (__builtin_available(macos 10.12, iOS 10, tvOS 10, watchOS 3, *)) {
33e089b5e9SJonas Devlieghere     os_log_fault(OS_LOG_DEFAULT,
34e089b5e9SJonas Devlieghere                  "Assertion failed: (%s), function %s, file %s, line %u\n",
35e089b5e9SJonas Devlieghere                  expr_text, func, file, line);
36e089b5e9SJonas Devlieghere   }
37e089b5e9SJonas Devlieghere #endif
38e089b5e9SJonas Devlieghere 
39fd19ffc6SJonas Devlieghere   // In a release configuration it will print a warning and encourage the user
40b0fc4d47SJonas Devlieghere   // to file a bug report, similar to LLVM’s crash handler, and then return
41b0fc4d47SJonas Devlieghere   // execution.
42a893d301SZachary Turner   errs() << format("Assertion failed: (%s), function %s, file %s, line %u\n",
43a893d301SZachary Turner                    expr_text, func, file, line);
44a893d301SZachary Turner   errs() << "backtrace leading to the failure:\n";
45a893d301SZachary Turner   llvm::sys::PrintStackTrace(errs());
46b9c1b51eSKate Stone   errs() << "please file a bug report against lldb reporting this failure "
47b9c1b51eSKate Stone             "log, and as many details as possible\n";
4853ed89c6SEnrico Granata }
49