2022-10-06 11:35:46 +00:00
|
|
|
/*
|
2024-10-04 11:19:50 +00:00
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <andreas@ladybird.org>
|
2022-10-06 11:35:46 +00:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <AK/Assertions.h>
|
2024-06-24 23:19:10 +00:00
|
|
|
#include <AK/Backtrace.h>
|
2022-10-06 11:35:46 +00:00
|
|
|
#include <AK/Format.h>
|
2023-08-30 14:54:33 +00:00
|
|
|
#include <AK/Platform.h>
|
2023-09-14 19:38:06 +00:00
|
|
|
#include <AK/StringBuilder.h>
|
2023-08-30 14:54:33 +00:00
|
|
|
#include <AK/StringView.h>
|
|
|
|
|
2023-09-14 19:38:06 +00:00
|
|
|
#if defined(AK_OS_ANDROID) && (__ANDROID_API__ >= 33)
|
|
|
|
# include <android/log.h>
|
|
|
|
# define EXECINFO_BACKTRACE
|
|
|
|
# define PRINT_ERROR(s) __android_log_write(ANDROID_LOG_WARN, "AK", (s))
|
|
|
|
#else
|
|
|
|
# define PRINT_ERROR(s) (void)fputs((s), stderr)
|
|
|
|
#endif
|
|
|
|
|
2024-06-24 23:19:10 +00:00
|
|
|
#if defined(AK_HAS_BACKTRACE_HEADER)
|
2023-08-30 14:54:33 +00:00
|
|
|
# include <cxxabi.h>
|
|
|
|
#endif
|
2022-10-06 11:35:46 +00:00
|
|
|
|
2023-09-14 19:38:06 +00:00
|
|
|
#if defined(AK_OS_SERENITY)
|
|
|
|
# define ERRORLN dbgln
|
|
|
|
#else
|
|
|
|
# define ERRORLN warnln
|
|
|
|
#endif
|
|
|
|
|
2024-06-24 23:19:10 +00:00
|
|
|
#if defined(AK_HAS_BACKTRACE_HEADER)
|
2023-08-30 14:54:33 +00:00
|
|
|
namespace {
|
|
|
|
ALWAYS_INLINE void dump_backtrace()
|
|
|
|
{
|
|
|
|
// Grab symbols and dso name for up to 256 frames
|
|
|
|
void* trace[256] = {};
|
2023-10-27 13:21:49 +00:00
|
|
|
int const num_frames = backtrace(trace, array_size(trace));
|
2023-08-30 14:54:33 +00:00
|
|
|
char** syms = backtrace_symbols(trace, num_frames);
|
|
|
|
|
|
|
|
for (auto i = 0; i < num_frames; ++i) {
|
|
|
|
// If there is a C++ symbol name in the line of the backtrace, demangle it
|
|
|
|
StringView sym(syms[i], strlen(syms[i]));
|
2023-09-14 19:38:06 +00:00
|
|
|
StringBuilder error_builder;
|
2023-08-30 14:54:33 +00:00
|
|
|
if (auto idx = sym.find("_Z"sv); idx.has_value()) {
|
|
|
|
// Play C games with the original string so we can print before and after the mangled symbol with a C API
|
|
|
|
// We don't want to call dbgln() here on substring StringView because we might VERIFY() within AK::Format
|
|
|
|
syms[i][idx.value() - 1] = '\0';
|
2023-09-14 19:38:06 +00:00
|
|
|
error_builder.append(syms[i], strlen(syms[i]));
|
|
|
|
error_builder.append(' ');
|
2023-08-30 14:54:33 +00:00
|
|
|
|
2023-09-02 19:07:45 +00:00
|
|
|
auto sym_substring = sym.substring_view(idx.value());
|
|
|
|
auto end_of_sym = sym_substring.find_any_of("+ "sv).value_or(sym_substring.length() - 1);
|
|
|
|
syms[i][idx.value() + end_of_sym] = '\0';
|
2023-08-30 14:54:33 +00:00
|
|
|
|
|
|
|
size_t buf_size = 128u;
|
|
|
|
char* buf = static_cast<char*>(malloc(buf_size));
|
|
|
|
auto* raw_str = &syms[i][idx.value()];
|
|
|
|
buf = abi::__cxa_demangle(raw_str, buf, &buf_size, nullptr);
|
|
|
|
|
2023-09-14 19:38:06 +00:00
|
|
|
auto* buf_to_print = buf ? buf : raw_str;
|
|
|
|
error_builder.append(buf_to_print, strlen(buf_to_print));
|
2023-08-30 14:54:33 +00:00
|
|
|
free(buf);
|
|
|
|
|
2023-09-14 19:38:06 +00:00
|
|
|
error_builder.append(' ');
|
|
|
|
auto* end_of_line = &syms[i][idx.value() + end_of_sym + 1];
|
|
|
|
error_builder.append(end_of_line, strlen(end_of_line));
|
2023-08-30 14:54:33 +00:00
|
|
|
} else {
|
2023-09-14 19:38:06 +00:00
|
|
|
error_builder.append(sym);
|
2023-08-30 14:54:33 +00:00
|
|
|
}
|
2024-06-17 22:12:53 +00:00
|
|
|
# if !defined(AK_OS_ANDROID)
|
2023-09-14 19:38:06 +00:00
|
|
|
error_builder.append('\n');
|
2024-06-17 22:12:53 +00:00
|
|
|
# endif
|
2023-09-14 19:38:06 +00:00
|
|
|
error_builder.append('\0');
|
|
|
|
PRINT_ERROR(error_builder.string_view().characters_without_null_termination());
|
2023-08-30 14:54:33 +00:00
|
|
|
}
|
|
|
|
free(syms);
|
|
|
|
}
|
|
|
|
}
|
2024-06-17 22:12:53 +00:00
|
|
|
#endif
|
2023-08-30 14:54:33 +00:00
|
|
|
|
2022-10-06 11:35:46 +00:00
|
|
|
extern "C" {
|
|
|
|
|
|
|
|
void ak_verification_failed(char const* message)
|
|
|
|
{
|
2024-06-17 22:12:53 +00:00
|
|
|
#if defined(AK_OS_SERENITY) || defined(AK_OS_ANDROID)
|
2023-10-20 18:49:38 +00:00
|
|
|
bool colorize_output = true;
|
2024-06-17 22:12:53 +00:00
|
|
|
#elif defined(AK_OS_WINDOWS)
|
2023-10-29 05:19:24 +00:00
|
|
|
bool colorize_output = false;
|
2024-06-17 22:12:53 +00:00
|
|
|
#else
|
2023-10-20 18:49:38 +00:00
|
|
|
bool colorize_output = isatty(STDERR_FILENO) == 1;
|
2024-06-17 22:12:53 +00:00
|
|
|
#endif
|
2023-10-20 18:49:38 +00:00
|
|
|
|
|
|
|
if (colorize_output)
|
|
|
|
ERRORLN("\033[31;1mVERIFICATION FAILED\033[0m: {}", message);
|
|
|
|
else
|
|
|
|
ERRORLN("VERIFICATION FAILED: {}", message);
|
|
|
|
|
2024-07-09 12:21:34 +00:00
|
|
|
#if defined(AK_HAS_BACKTRACE_HEADER)
|
|
|
|
dump_backtrace();
|
|
|
|
#endif
|
|
|
|
__builtin_trap();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ak_assertion_failed(char const* message)
|
|
|
|
{
|
|
|
|
#if defined(AK_OS_SERENITY) || defined(AK_OS_ANDROID)
|
|
|
|
bool colorize_output = true;
|
|
|
|
#elif defined(AK_OS_WINDOWS)
|
|
|
|
bool colorize_output = false;
|
|
|
|
#else
|
|
|
|
bool colorize_output = isatty(STDERR_FILENO) == 1;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (colorize_output)
|
|
|
|
ERRORLN("\033[31;1mASSERTION FAILED\033[0m: {}", message);
|
|
|
|
else
|
|
|
|
ERRORLN("ASSERTION FAILED: {}", message);
|
|
|
|
|
2024-06-24 23:19:10 +00:00
|
|
|
#if defined(AK_HAS_BACKTRACE_HEADER)
|
2023-08-30 14:54:33 +00:00
|
|
|
dump_backtrace();
|
2024-06-17 22:12:53 +00:00
|
|
|
#endif
|
2022-10-06 11:35:46 +00:00
|
|
|
__builtin_trap();
|
|
|
|
}
|
|
|
|
}
|