Add a uc_backtrace_buf() functio
This commit is contained in:
@@ -2,7 +2,23 @@
|
|||||||
#define UC_BACKTRACE_H_
|
#define UC_BACKTRACE_H_
|
||||||
|
|
||||||
#define UC_BACTRACE_STDERR uc_backtrace_fd(2)
|
#define UC_BACTRACE_STDERR uc_backtrace_fd(2)
|
||||||
|
#include <stddef.h>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Print a stacktrace to the given file descriptor.
|
||||||
|
*
|
||||||
|
* @param fd filedescriptor to write to.
|
||||||
|
*/
|
||||||
void uc_backtrace_fd(int fd);
|
void uc_backtrace_fd(int fd);
|
||||||
|
/**
|
||||||
|
* Print a stactrace to the given buffer
|
||||||
|
* (Note that unlike uc_backtrace_fd(), this function
|
||||||
|
* does malloc() call and may fail in a signal handler or
|
||||||
|
* if memory is corrupted.
|
||||||
|
*
|
||||||
|
* @param buf buffer to place the stacktrace in.
|
||||||
|
* @param len length of buffer
|
||||||
|
*/
|
||||||
|
void uc_backtrace_buf(char *buf, size_t len);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
#include <execinfo.h>
|
#include <execinfo.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <ucore/ucore_backtrace.h>
|
||||||
|
|
||||||
#define MAX_BACKTRACE_SYMBOLS 96
|
#define MAX_BACKTRACE_SYMBOLS 96
|
||||||
|
|
||||||
@@ -20,4 +22,30 @@ void uc_backtrace_fd(int fd)
|
|||||||
backtrace_symbols_fd(syms, ptrs, 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);
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
#include <stdio.h>
|
||||||
#include <ucore/ucore_backtrace.h>
|
#include <ucore/ucore_backtrace.h>
|
||||||
|
|
||||||
//note , compiling this without -rdynamic does not
|
//note , compiling this without -rdynamic does not
|
||||||
@@ -7,9 +8,22 @@ void func2(void)
|
|||||||
uc_backtrace_fd(2);
|
uc_backtrace_fd(2);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void func3(void)
|
||||||
|
{
|
||||||
|
uc_backtrace_fd(2);
|
||||||
|
}
|
||||||
|
|
||||||
|
void func4(void)
|
||||||
|
{
|
||||||
|
char trace[256];
|
||||||
|
uc_backtrace_buf(trace, sizeof trace);
|
||||||
|
printf("Stacktrace from %s:\n%s", __func__, trace);
|
||||||
|
}
|
||||||
|
|
||||||
void func1(void)
|
void func1(void)
|
||||||
{
|
{
|
||||||
func2();
|
func2();
|
||||||
|
func4();
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char *argv[])
|
int main(int argc, char *argv[])
|
||||||
|
|||||||
Reference in New Issue
Block a user