diff options
author | Samuel Holland <samuel@sholland.org> | 2018-01-14 21:22:23 -0600 |
---|---|---|
committer | Samuel Holland <samuel@sholland.org> | 2018-01-14 21:26:44 -0600 |
commit | 51fcab754dabef67a26210a6e1cd752290a10749 (patch) | |
tree | c2b4a6ae9dc6eafd2da9b1f90507ece8e03e0beb /libgcompat | |
parent | 9e0bbd07ef5affa403d476b0bcdabfc5a5684cf9 (diff) | |
download | gcompat-51fcab754dabef67a26210a6e1cd752290a10749.tar.gz gcompat-51fcab754dabef67a26210a6e1cd752290a10749.tar.bz2 gcompat-51fcab754dabef67a26210a6e1cd752290a10749.tar.xz gcompat-51fcab754dabef67a26210a6e1cd752290a10749.zip |
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 <samuel@sholland.org>
Diffstat (limited to 'libgcompat')
-rw-r--r-- | libgcompat/backtrace.c | 51 | ||||
-rw-r--r-- | libgcompat/execinfo.c | 97 |
2 files changed, 97 insertions, 51 deletions
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; + } + } +} |