Skip to content

Commit 322885e

Browse files
committed
options/glibc: implement backtrace_symbols_fd
1 parent a63a104 commit 322885e

File tree

1 file changed

+24
-3
lines changed

1 file changed

+24
-3
lines changed

options/glibc/generic/execinfo.cpp

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1+
#include <dlfcn.h>
12
#include <execinfo.h>
3+
#include <inttypes.h>
4+
#include <stdio.h>
5+
#include <unistd.h>
26
#include <unwind.h>
37

48
#include <bits/ensure.h>
@@ -49,7 +53,24 @@ char **backtrace_symbols(void *const *, int) {
4953
__builtin_unreachable();
5054
}
5155

52-
void backtrace_symbols_fd(void *const *, int, int) {
53-
__ensure(!"Not implemented");
54-
__builtin_unreachable();
56+
void backtrace_symbols_fd(void *const *buffer, int size, int fd) {
57+
if (size <= 0 || fd < 0) {
58+
return;
59+
}
60+
61+
for (int frame_num = 0; frame_num < size; frame_num++) {
62+
Dl_info info;
63+
if (dladdr(buffer[frame_num], &info) != 0) {
64+
if (info.dli_fname != nullptr)
65+
write(fd, info.dli_fname, strlen(info.dli_fname));
66+
67+
if (info.dli_sname != nullptr)
68+
dprintf(fd, "(%s+0x%" PRIxPTR ") ", info.dli_sname,
69+
reinterpret_cast<uintptr_t>(buffer[frame_num]) - reinterpret_cast<uintptr_t>(info.dli_saddr));
70+
else
71+
dprintf(fd, "(+%p) ", info.dli_saddr);
72+
}
73+
74+
dprintf(fd, "[%p]\n", buffer[frame_num]);
75+
}
5576
}

0 commit comments

Comments
 (0)