diff options
-rw-r--r-- | Makefile | 2 | ||||
-rw-r--r-- | libgcompat/backtrace.c | 51 | ||||
-rw-r--r-- | libgcompat/execinfo.c | 97 |
3 files changed, 98 insertions, 52 deletions
@@ -1,9 +1,9 @@ LIBGCOMPAT_INCLUDE = \ libgcompat/alias.h LIBGCOMPAT_SRC = \ - libgcompat/backtrace.c \ libgcompat/dlmopen.c \ libgcompat/dlvsym.c \ + libgcompat/execinfo.c \ libgcompat/gnulib.c \ libgcompat/malloc.c \ libgcompat/math.c \ diff --git a/libgcompat/backtrace.c b/libgcompat/backtrace.c deleted file mode 100644 index c8ded8b..0000000 --- a/libgcompat/backtrace.c +++ /dev/null @@ -1,51 +0,0 @@ -#include <dlfcn.h> -#include <stddef.h> -#include <stdlib.h> - -#define _frame_level(addr_buf, curr, frame, size) \ - do { \ - if (__builtin_frame_address(frame) != NULL \ - && (curr = __builtin_return_address(frame)) > 0x1000 \ - && frame <= size) { \ - addr_buf[frame] = curr; \ - } else { \ - return size; \ - } \ - } while (0) - -int backtrace(void **addr_buf, int size) -{ - void *curr; - _frame_level(addr_buf, curr, 0, size); - _frame_level(addr_buf, curr, 1, size); - _frame_level(addr_buf, curr, 2, size); - _frame_level(addr_buf, curr, 3, size); - _frame_level(addr_buf, curr, 4, size); - _frame_level(addr_buf, curr, 5, size); - _frame_level(addr_buf, curr, 6, size); - _frame_level(addr_buf, curr, 7, size); - _frame_level(addr_buf, curr, 8, size); - _frame_level(addr_buf, curr, 9, size); - return 9; -} - -char **backtrace_symbols(void *const *addr_buf, int size) -{ - char **result = calloc(sizeof(char *), size); - if (result == NULL) { - return result; - } - - for (int next = 0; next < size; next++) { - Dl_info info; - int err = dladdr(addr_buf[next], &info); - - if (err != 0) { - result[next] = "??:0"; - } else { - result[next] = info.dli_sname; - } - } - - return result; -} diff --git a/libgcompat/execinfo.c b/libgcompat/execinfo.c new file mode 100644 index 0000000..59441aa --- /dev/null +++ b/libgcompat/execinfo.c @@ -0,0 +1,97 @@ +#include <dlfcn.h> /* dladdr */ +#include <stddef.h> /* NULL */ +#include <stdint.h> /* uintptr_t */ +#include <stdlib.h> /* calloc */ +#include <string.h> /* strlen */ +#include <unistd.h> /* write */ + +#define get_frame_level(array, size, n) \ + do { \ + if (n >= size || __builtin_frame_address(n) == NULL) { \ + return n; \ + } \ + void *address = __builtin_return_address(n); \ + array[n] = __builtin_extract_return_addr(address); \ + if ((uintptr_t) array[n] < 0x1000) { \ + return n; \ + } \ + } while (0) + +/** + * Obtain a backtrace for the calling program. + * + * LSB 5.0: LSB-Core-generic/baselib-backtrace-1.html + */ +int backtrace(void **array, int size) +{ + get_frame_level(array, size, 0); + get_frame_level(array, size, 1); + get_frame_level(array, size, 2); + get_frame_level(array, size, 3); + get_frame_level(array, size, 4); + get_frame_level(array, size, 5); + get_frame_level(array, size, 6); + get_frame_level(array, size, 7); + get_frame_level(array, size, 8); + get_frame_level(array, size, 9); + return 10; +} + +/** + * Translate addresses into symbol information. + * + * LSB 5.0: LSB-Core-generic/baselib-backtrace-1.html + */ +const char **backtrace_symbols(void *const *array, int size) +{ + const char **result = calloc(size, sizeof(char *)); + + if (result == NULL) { + return NULL; + } + for (int i = 0; i < size; ++i) { + Dl_info info; + + if (dladdr(array[i], &info) && info.dli_sname != NULL) { + result[i] = info.dli_sname; + } else { + result[i] = "??:0"; + } + } + + return result; +} + +/** + * Write symbol information to a file without allocating memory. + * + * LSB 5.0: LSB-Core-generic/baselib-backtrace-1.html + */ +void backtrace_symbols_fd(void *const *array, int size, int fd) +{ + for (int i = 0; i < size; ++i) { + Dl_info info; + const char *line; + int len; + + if (dladdr(array[i], &info) && info.dli_sname != NULL) { + line = info.dli_sname; + len = strlen(line); + } else { + line = "??:0"; + len = sizeof("??:0") - 1; + } + while (len > 0) { + int written = write(fd, line, len); + + if (written < 1) { + return; + } + line += written; + len -= written; + } + if (write(fd, "\n", 1) != 1) { + return; + } + } +} |