This commit is contained in:
186
src/assert.hpp
Normal file
186
src/assert.hpp
Normal file
@@ -0,0 +1,186 @@
|
||||
#pragma once
|
||||
|
||||
#include <iostream>
|
||||
#include <string_view>
|
||||
#include <source_location>
|
||||
#include <cxxabi.h>
|
||||
#include <libunwind.h>
|
||||
#include <dlfcn.h>
|
||||
#include <cstring>
|
||||
#include <memory>
|
||||
#include <sstream>
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include <unistd.h>
|
||||
|
||||
// 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);
|
||||
if (demangled) {
|
||||
results[current_frame].function = demangled;
|
||||
free(demangled);
|
||||
} else {
|
||||
results[current_frame].function = buffer;
|
||||
}
|
||||
|
||||
// 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() {
|
||||
unw_cursor_t cursor;
|
||||
unw_context_t context;
|
||||
unw_word_t pc;
|
||||
char exe_path[1024] = {0};
|
||||
bool found_main = false;
|
||||
std::vector<SourceInfo> frames;
|
||||
|
||||
// Get the path to the current executable
|
||||
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';
|
||||
}
|
||||
|
||||
// Initialize cursor to current frame
|
||||
unw_getcontext(&context);
|
||||
unw_init_local(&cursor, &context);
|
||||
|
||||
// First pass: collect frames until we find main
|
||||
std::vector<void*> addresses;
|
||||
while (unw_step(&cursor) > 0) {
|
||||
if (unw_get_reg(&cursor, UNW_REG_IP, &pc) != 0 || pc == 0) {
|
||||
break;
|
||||
}
|
||||
addresses.push_back(reinterpret_cast<void*>(pc));
|
||||
}
|
||||
|
||||
// Get source info for all addresses in batch
|
||||
std::vector<SourceInfo> results = get_source_info_batch(exe_path, addresses);
|
||||
|
||||
// Process results and check for main
|
||||
for (auto& info : results) {
|
||||
// Stop collecting after main()
|
||||
if (!info.function.empty() &&
|
||||
(info.function.find("main") != std::string::npos ||
|
||||
info.function.find("__libc_start_main") != std::string::npos)) {
|
||||
found_main = true;
|
||||
frames.push_back(info);
|
||||
break;
|
||||
}
|
||||
frames.push_back(info);
|
||||
}
|
||||
|
||||
// Print the collected frames in reverse order (most recent last)
|
||||
std::cerr << colors::yellow << "Stack trace (most recent call last):" << colors::reset << "\n";
|
||||
|
||||
for (size_t i = frames.size(); i-- > 0; ) {
|
||||
const auto& frame = frames[i];
|
||||
std::cerr << " "; // Indent each line
|
||||
|
||||
if (frame.function.empty()) {
|
||||
std::cerr << colors::red << "[unknown function]" << colors::reset;
|
||||
} else {
|
||||
std::cerr << colors::green << frame.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";
|
||||
}
|
||||
|
||||
if (!found_main) {
|
||||
std::cerr << " " << colors::yellow << "[truncated - main() not found]" << colors::reset << "\n";
|
||||
}
|
||||
}
|
||||
|
||||
void assert_failed(
|
||||
bool condition,
|
||||
std::string_view message,
|
||||
std::source_location location = std::source_location::current()
|
||||
) {
|
||||
if (!condition) {
|
||||
std::cerr << "Assertion failed at " << location.file_name() << ":" << location.line() << ": "
|
||||
<< location.function_name() << ": " << message << "\n";
|
||||
print_stacktrace();
|
||||
std::abort();
|
||||
}
|
||||
}
|
||||
|
||||
#define ASSERT(condition, message) assert_failed(condition, message, std::source_location::current())
|
Reference in New Issue
Block a user