本文整理汇总了C++中Backtrace::constData方法的典型用法代码示例。如果您正苦于以下问题:C++ Backtrace::constData方法的具体用法?C++ Backtrace::constData怎么用?C++ Backtrace::constData使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Backtrace
的用法示例。
在下文中一共展示了Backtrace::constData方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: printBacktrace
static void printBacktrace(Backtrace stacktrace)
{
void *const *stack = (void *const *)stacktrace.constData();
int stack_size = stacktrace.size() / sizeof(void*);
char **stack_symbols = backtrace_symbols(stack, stack_size);
int filter[2];
pid_t child = -1;
if (pipe(filter) != -1)
child = fork();
if (child == 0) {
// child process
dup2(fileno(stderr), fileno(stdout));
dup2(filter[0], fileno(stdin));
close(filter[0]);
close(filter[1]);
execlp("c++filt", "c++filt", "-n", NULL);
// execlp failed
execl("/bin/cat", "/bin/cat", NULL);
_exit(127);
}
// parent process
close(filter[0]);
FILE *output;
if (child == -1) {
// failed forking
close(filter[1]);
output = stderr;
} else {
output = fdopen(filter[1], "w");
}
fprintf(stderr, "Backtrace of the first creation (most recent frame first):\n");
for (int i = 0; i < stack_size; ++i) {
if (strlen(stack_symbols[i]))
fprintf(output, "#%-2d %s\n", i, stack_symbols[i]);
else
fprintf(output, "#%-2d %p\n", i, stack[i]);
}
if (child != -1) {
fclose(output);
waitpid(child, 0, 0);
}
}