Your Name eeb5d44b73
Some checks failed
dropshell-build / build (push) Failing after 51m54s
'Generic Commit'
2025-06-14 18:17:57 +12:00

183 lines
5.6 KiB
C++

#pragma once
#include <iostream>
#include <string_view>
#include <source_location>
#include <cxxabi.h>
// execinfo.h not available in Alpine Linux - using alternative approach
#include <dlfcn.h>
#include <cstring>
#include <memory>
#include <sstream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <unistd.h>
#include <vector>
// ANSI color codes
namespace colors {
constexpr const char* reset = "\033[0m";
constexpr const char* red = "\033[31m";
constexpr const char* green = "\033[32m";
constexpr const char* yellow = "\033[33m";
constexpr const char* blue = "\033[34m";
constexpr const char* magenta = "\033[35m";
constexpr const char* cyan = "\033[36m";
}
// Simple RAII wrapper for file descriptor
class FileDescriptor {
int fd_ = -1;
public:
explicit FileDescriptor(int fd) : fd_(fd) {}
~FileDescriptor() { if (fd_ != -1) close(fd_); }
operator int() const { return fd_; }
FileDescriptor(const FileDescriptor&) = delete;
FileDescriptor& operator=(const FileDescriptor&) = delete;
};
struct SourceInfo {
std::string function;
std::string file;
int line = 0;
};
std::vector<SourceInfo> get_source_info_batch(const char* executable, const std::vector<void*>& addresses) {
if (addresses.empty()) return {};
// Build command with all addresses
std::string cmd = std::string("addr2line -f -e ") + executable;
for (void* addr : addresses) {
char addr_buf[32];
snprintf(addr_buf, sizeof(addr_buf), " %p", addr);
cmd += addr_buf;
}
cmd += " 2>/dev/null";
FILE* pipe = popen(cmd.c_str(), "r");
if (!pipe) return std::vector<SourceInfo>(addresses.size());
std::vector<SourceInfo> results(addresses.size());
char buffer[1024] = {0};
size_t current_frame = 0;
// Read function names and file/line info for each address
while (current_frame < addresses.size() && fgets(buffer, sizeof(buffer), pipe)) {
// Remove trailing newline
size_t len = strlen(buffer);
if (len > 0 && buffer[len-1] == '\n') {
buffer[len-1] = '\0';
}
// Demangle function name
int status = 0;
char* demangled = abi::__cxa_demangle(buffer, nullptr, nullptr, &status);
std::string func_name;
if (demangled) {
func_name = demangled;
free(demangled);
} else {
func_name = buffer;
}
// Simplify function signature: replace argument list with (...)
size_t paren_pos = func_name.find('(');
if (paren_pos != std::string::npos) {
func_name = func_name.substr(0, paren_pos) + "(...)";
}
results[current_frame].function = func_name;
// Read file and line number
if (fgets(buffer, sizeof(buffer), pipe)) {
char* colon = strchr(buffer, ':');
if (colon) {
*colon = '\0';
results[current_frame].line = atoi(colon + 1);
// Extract just the filename
const char* slash = strrchr(buffer, '/');
results[current_frame].file = (slash ? slash + 1 : buffer);
}
}
current_frame++;
}
pclose(pipe);
return results;
}
void print_stacktrace() {
// Simple stacktrace implementation that doesn't segfault
std::cerr << colors::yellow << "Stack trace:" << colors::reset << "\n";
char exe_path[1024] = {0};
ssize_t count = readlink("/proc/self/exe", exe_path, sizeof(exe_path) - 1);
if (count == -1) {
strcpy(exe_path, "[unknown]");
} else {
exe_path[count] = '\0';
}
// Get just a couple of stack frames safely
std::vector<void*> addresses;
void* addr1 = __builtin_return_address(1); // assert_failed
void* addr2 = __builtin_return_address(2); // caller of ASSERT
// Adjust addresses to point to call site instead of return address
// Subtract a small offset to get the call instruction instead of the return address
if (addr1) {
addresses.push_back(static_cast<char*>(addr1) - 1);
}
if (addr2) {
addresses.push_back(static_cast<char*>(addr2) - 1);
}
if (addresses.empty()) {
std::cerr << " " << colors::red << "[no frames available]" << colors::reset << "\n";
return;
}
// Get source info
std::vector<SourceInfo> results = get_source_info_batch(exe_path, addresses);
for (size_t i = 0; i < results.size(); ++i) {
const auto& frame = results[i];
std::cerr << " ";
if (!frame.function.empty()) {
std::cerr << colors::green << frame.function << colors::reset;
} else {
std::cerr << colors::red << "[unknown function]" << colors::reset;
}
if (!frame.file.empty() && frame.line > 0) {
std::cerr << " at " << colors::blue << frame.file << colors::reset
<< ":" << colors::magenta << frame.line << colors::reset;
}
std::cerr << "\n";
}
}
void assert_failed(
bool condition,
std::string_view message,
std::source_location location
) {
if (!condition) {
std::cout << std::flush;
std::cerr << std::flush;
std::cerr << colors::red << "Assertion failed at " << location.file_name() << ":" << location.line() << ": "
<< location.function_name() << ": " << message << colors::reset << "\n";
print_stacktrace();
std::abort();
}
}
#define ASSERT(condition, message) assert_failed(condition, message, std::source_location::current())