This commit is contained in:
parent
5d2ee141d1
commit
5014603a42
@ -22,12 +22,17 @@ RUN apk add --no-cache \
|
|||||||
libunwind \
|
libunwind \
|
||||||
libunwind-dev \
|
libunwind-dev \
|
||||||
libunwind-static \
|
libunwind-static \
|
||||||
libunwind-dbg \
|
|
||||||
zlib-dev \
|
zlib-dev \
|
||||||
zlib-static \
|
zlib-static \
|
||||||
|
xz \
|
||||||
xz-dev \
|
xz-dev \
|
||||||
xz-static \
|
xz-static \
|
||||||
xz-libs
|
xz-libs \
|
||||||
|
libdwarf \
|
||||||
|
libdwarf-dev \
|
||||||
|
elfutils \
|
||||||
|
elfutils-dev \
|
||||||
|
elfutils-libelf
|
||||||
|
|
||||||
#RUN apk add --no-cache --update --repository=https://dl-cdn.alpinelinux.org/alpine/v3.16/main/ libexecinfo-dev
|
#RUN apk add --no-cache --update --repository=https://dl-cdn.alpinelinux.org/alpine/v3.16/main/ libexecinfo-dev
|
||||||
|
|
||||||
|
@ -84,14 +84,13 @@ FetchContent_MakeAvailable(nlohmann_json)
|
|||||||
find_package(PkgConfig REQUIRED)
|
find_package(PkgConfig REQUIRED)
|
||||||
pkg_check_modules(LIBUNWIND REQUIRED IMPORTED_TARGET libunwind)
|
pkg_check_modules(LIBUNWIND REQUIRED IMPORTED_TARGET libunwind)
|
||||||
|
|
||||||
# Additional required libraries for static linking
|
# Required libraries for basic stack tracing
|
||||||
set(EXTRA_LIBS
|
set(EXTRA_LIBS
|
||||||
${LIBUNWIND_LIBRARIES}
|
${LIBUNWIND_LIBRARIES}
|
||||||
-lunwind
|
-lunwind
|
||||||
-lunwind-x86_64
|
-lunwind-x86_64
|
||||||
-llzma
|
-llzma # Required by libunwind for debug info
|
||||||
-lz
|
-ldl # For dladdr
|
||||||
-ldl
|
|
||||||
)
|
)
|
||||||
|
|
||||||
# Link libraries
|
# Link libraries
|
||||||
|
@ -6,6 +6,19 @@
|
|||||||
#include <cxxabi.h>
|
#include <cxxabi.h>
|
||||||
#include <libunwind.h>
|
#include <libunwind.h>
|
||||||
#include <dlfcn.h>
|
#include <dlfcn.h>
|
||||||
|
#include <cstring>
|
||||||
|
#include <memory>
|
||||||
|
|
||||||
|
// 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;
|
||||||
|
};
|
||||||
|
|
||||||
void print_stacktrace() {
|
void print_stacktrace() {
|
||||||
unw_cursor_t cursor;
|
unw_cursor_t cursor;
|
||||||
@ -14,6 +27,7 @@ void print_stacktrace() {
|
|||||||
char sym[256];
|
char sym[256];
|
||||||
char *name = sym;
|
char *name = sym;
|
||||||
int status;
|
int status;
|
||||||
|
int frame_num = 0;
|
||||||
|
|
||||||
// Initialize cursor to current frame
|
// Initialize cursor to current frame
|
||||||
unw_getcontext(&context);
|
unw_getcontext(&context);
|
||||||
@ -28,21 +42,30 @@ void print_stacktrace() {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::cerr << "#" << frame_num++ << " ";
|
||||||
|
|
||||||
// Get symbol name
|
// Get symbol name
|
||||||
if (unw_get_proc_name(&cursor, sym, sizeof(sym), &offset) == 0) {
|
if (unw_get_proc_name(&cursor, sym, sizeof(sym), &offset) == 0) {
|
||||||
// Demangle the C++ name
|
// Demangle the C++ name
|
||||||
int status;
|
int status = 0;
|
||||||
size_t length = sizeof(sym);
|
size_t length = sizeof(sym);
|
||||||
name = abi::__cxa_demangle(sym, nullptr, &length, &status);
|
name = abi::__cxa_demangle(sym, nullptr, &length, &status);
|
||||||
if (name == nullptr) {
|
|
||||||
name = sym;
|
Dl_info info;
|
||||||
|
if (dladdr(reinterpret_cast<void*>(pc), &info) && info.dli_fname) {
|
||||||
|
std::cerr << "in " << info.dli_fname << ": ";
|
||||||
}
|
}
|
||||||
std::cerr << " 0x" << std::hex << pc << ": " << name << " + 0x" << offset << "\n";
|
|
||||||
if (name != sym) {
|
if (name) {
|
||||||
|
std::cerr << name << " + 0x" << std::hex << offset;
|
||||||
free(name);
|
free(name);
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
std::cerr << " 0x" << std::hex << pc << ": [unknown]\n";
|
std::cerr << sym << " + 0x" << std::hex << offset;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::cerr << " [0x" << std::hex << pc << "]\n";
|
||||||
|
} else {
|
||||||
|
std::cerr << "[unknown] [0x" << std::hex << pc << "]\n";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user