Add a uc_backtrace_buf() functio
This commit is contained in:
@@ -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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user