This commit is contained in:
@ -4,7 +4,7 @@
|
||||
#include <string_view>
|
||||
#include <source_location>
|
||||
#include <cxxabi.h>
|
||||
#include <libunwind.h>
|
||||
// execinfo.h not available in Alpine Linux - using alternative approach
|
||||
#include <dlfcn.h>
|
||||
#include <cstring>
|
||||
#include <memory>
|
||||
@ -13,6 +13,7 @@
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include <unistd.h>
|
||||
#include <vector>
|
||||
|
||||
// ANSI color codes
|
||||
namespace colors {
|
||||
@ -72,12 +73,21 @@ std::vector<SourceInfo> get_source_info_batch(const char* executable, const std:
|
||||
// Demangle function name
|
||||
int status = 0;
|
||||
char* demangled = abi::__cxa_demangle(buffer, nullptr, nullptr, &status);
|
||||
std::string func_name;
|
||||
if (demangled) {
|
||||
results[current_frame].function = demangled;
|
||||
func_name = demangled;
|
||||
free(demangled);
|
||||
} else {
|
||||
results[current_frame].function = buffer;
|
||||
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)) {
|
||||
@ -100,14 +110,10 @@ std::vector<SourceInfo> get_source_info_batch(const char* executable, const std:
|
||||
}
|
||||
|
||||
void print_stacktrace() {
|
||||
unw_cursor_t cursor;
|
||||
unw_context_t context;
|
||||
unw_word_t pc;
|
||||
// Simple stacktrace implementation that doesn't segfault
|
||||
std::cerr << colors::yellow << "Stack trace:" << colors::reset << "\n";
|
||||
|
||||
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]");
|
||||
@ -115,46 +121,37 @@ void print_stacktrace() {
|
||||
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
|
||||
// Get just a couple of stack frames safely
|
||||
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));
|
||||
|
||||
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 for all addresses in batch
|
||||
// Get source info
|
||||
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
|
||||
for (size_t i = 0; i < results.size(); ++i) {
|
||||
const auto& frame = results[i];
|
||||
std::cerr << " ";
|
||||
|
||||
if (frame.function.empty()) {
|
||||
std::cerr << colors::red << "[unknown function]" << colors::reset;
|
||||
} else {
|
||||
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) {
|
||||
@ -164,20 +161,16 @@ void print_stacktrace() {
|
||||
|
||||
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()
|
||||
std::source_location location
|
||||
) {
|
||||
if (!condition) {
|
||||
std::cerr << "Assertion failed at " << location.file_name() << ":" << location.line() << ": "
|
||||
<< location.function_name() << ": " << message << "\n";
|
||||
std::cerr << colors::red << "Assertion failed at " << location.file_name() << ":" << location.line() << ": "
|
||||
<< location.function_name() << ": " << message << colors::reset << "\n";
|
||||
print_stacktrace();
|
||||
std::abort();
|
||||
}
|
||||
|
Reference in New Issue
Block a user