123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104 |
- #include <execinfo.h>
- #include <assert.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <dlfcn.h>
- #include <link.h> /* required for __ELF_NATIVE_CLASS */
- #if __ELF_NATIVE_CLASS == 32
- # define WORD_WIDTH 8
- #else
- # define WORD_WIDTH 16
- #endif
- char ** backtrace_symbols (void *const *array, int size)
- {
- Dl_info info[size];
- int status[size];
- int cnt;
- size_t total = 0;
- char **result;
-
- for (cnt = 0; cnt < size; ++cnt) {
- status[cnt] = dladdr (array[cnt], &info[cnt]);
- if (status[cnt] && info[cnt].dli_fname &&
- info[cnt].dli_fname[0] != '\0')
-
- total += (strlen (info[cnt].dli_fname ?: "") +
- (info[cnt].dli_sname ?
- strlen (info[cnt].dli_sname) + 3 + WORD_WIDTH + 3 : 1)
- + WORD_WIDTH + 5);
- else
- total += 5 + WORD_WIDTH;
- }
-
- result = (char **) malloc (size * sizeof (char *) + total);
- if (result != NULL) {
- char *last = (char *) (result + size);
- for (cnt = 0; cnt < size; ++cnt) {
- result[cnt] = last;
- if (status[cnt] && info[cnt].dli_fname
- && info[cnt].dli_fname[0] != '\0') {
- char buf[20];
- if (array[cnt] >= (void *) info[cnt].dli_saddr)
- sprintf (buf, "+%#lx",
- (unsigned long)(array[cnt] - info[cnt].dli_saddr));
- else
- sprintf (buf, "-%#lx",
- (unsigned long)(info[cnt].dli_saddr - array[cnt]));
- last += 1 + sprintf (last, "%s%s%s%s%s[%p]",
- info[cnt].dli_fname ?: "",
- info[cnt].dli_sname ? "(" : "",
- info[cnt].dli_sname ?: "",
- info[cnt].dli_sname ? buf : "",
- info[cnt].dli_sname ? ") " : " ",
- array[cnt]);
- } else
- last += 1 + sprintf (last, "[%p]", array[cnt]);
- }
- assert (last <= (char *) result + size * sizeof (char *) + total);
- }
- return result;
- }
|