From 51fcab754dabef67a26210a6e1cd752290a10749 Mon Sep 17 00:00:00 2001 From: Samuel Holland Date: Sun, 14 Jan 2018 21:22:23 -0600 Subject: execinfo: Clean up, implement backtrace_symbols_fd Rename the file to match the header it implements functions from. Changes to existing code: * Fix the return value from backtrace (off by one). * Use __builtin_extract_return_addr as recommended in gcc documentation. * Document header usage. * Document where the functions are referenced in the LSB standard. Signed-off-by: Samuel Holland --- libgcompat/backtrace.c | 51 -------------------------- libgcompat/execinfo.c | 97 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 97 insertions(+), 51 deletions(-) delete mode 100644 libgcompat/backtrace.c create mode 100644 libgcompat/execinfo.c (limited to 'libgcompat') 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 -#include -#include - -#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 /* dladdr */ +#include /* NULL */ +#include /* uintptr_t */ +#include /* calloc */ +#include /* strlen */ +#include /* 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; + } + } +} -- cgit v1.2.3-70-g09d2