Add a uc_backtrace_buf() functio

This commit is contained in:
Nils O. Selåsdal
2013-03-06 21:58:56 +01:00
parent e1bd10208e
commit ee8d9ae19d
3 changed files with 58 additions and 0 deletions
+28
View File
@@ -1,5 +1,7 @@
#include <execinfo.h>
#include <stdio.h>
#include <stdlib.h>
#include <ucore/ucore_backtrace.h>
#define MAX_BACKTRACE_SYMBOLS 96
@@ -20,4 +22,30 @@ void uc_backtrace_fd(int fd)
backtrace_symbols_fd(syms, ptrs, fd);
}
void uc_backtrace_buf(char *buf, size_t len)
{
void *buffer[MAX_BACKTRACE_SYMBOLS];
int ptrs;
char **lines;
size_t remaining = len;
int i;
ptrs = backtrace(buffer, MAX_BACKTRACE_SYMBOLS);
buf[0] = 0;
lines = backtrace_symbols(buffer, ptrs);
for (i = 1; i < ptrs; i++) {
int rc = snprintf(buf, remaining, "%s\n", lines[i]);
if (rc <= 0 || (size_t)rc >= remaining) {
break;
}
remaining -= rc;
buf += rc;
}
free(lines);
}