gpt4 book ai didi

c++ - Raspberry Pi 优化的 backtrace()

转载 作者:行者123 更新时间:2023-12-02 21:43:16 25 4
gpt4 key购买 nike

是否有任何 Raspberry Pi 优化/特定的 backtrace() 实现?我正在使用标准 backtrace() 代码,但期待 my_backtrace 函数的更详细输出。

void my_backtrace(){
void *stack[10];
int n = backtrace(stack, 10);
std::cout << "Last frames==" << n << std::endl;
backtrace_symbols_fd(stack, n, STDOUT_FILENO);
}

最佳答案

//--There is list of options:
//a) backtrace in combination with abi::__cxa_demangle
//b) GDB
//c) [libunwind][1], [http://www.nongnu.org/libunwind/docs.html]
//d) libbfd-dev
//e) [backward-cpp][1], [https://github.com/bombela/backward-cpp]
//f) [libbacktrace][1], [https://github.com/ErwanLegrand/libbacktrace]
//-----------------------------------------------
#include <execinfo.h>
#include <iostream>
#include <sys/types.h>
#include <unistd.h>
#include <sstream>
#include <sys/wait.h>
//-----------------------------------------------
//Based on GDB
//-----------------------------------------------
void print_trace_gdb() {
char pid_buf[30];
sprintf(pid_buf, "%d", getpid());
char name_buf[512];
name_buf[readlink("/proc/self/exe", name_buf, 511)]=0;

int child_pid = fork();
if (!child_pid) {
dup2(2,1); // redirect output to stderr
fprintf(stdout,"stack trace for %s pid=%s\n",name_buf, pid_buf);
execlp("gdb", "gdb", "--batch", "-n", "-ex", "thread", "-ex", "bt", name_buf, pid_buf, NULL);
//if gdb failed to start
abort();
} else {
waitpid(child_pid,NULL,0);
}
}

//-----------------------------------------------
void rec_function(int ii)
{
if ( ii == 0 ) {std::cout << "int value==" << ii << "\n";}
else {rec_function(--ii);}
print_trace_gdb();
}

//-----------------------------------------------
int main()
{
int jj=1;
std::cout << "\n---begin test-----\n";
std::cout << "int value==" << jj << "\n";
rec_function(jj);
std::cout << "---end test-----\n";
}

Output from this code is

---begin test-----
int value==1
int value==0
stack trace for /opt/cpp/linux_backtrace_gdb pid=4181
0x00007f878b5ca4ca in waitpid () from /lib/x86_64-linux-gnu/libc.so.6
[Current thread is 1 (process 4181)]
#0 0x00007f878b5ca4ca in waitpid () from /lib/x86_64-linux-gnu/libc.so.6
#1 0x0000000000400e69 in print_trace_gdb () at linux_backtrace_gdb.cpp:25
#2 0x0000000000400ed2 in rec_function (ii=0) at linux_backtrace_gdb.cpp:34
#3 0x0000000000400ecd in rec_function (ii=0) at linux_backtrace_gdb.cpp:33
#4 0x0000000000400f29 in main () at linux_backtrace_gdb.cpp:43
stack trace for /opt/cpp/linux_backtrace_gdb pid=4181
0x00007f878b5ca4ca in waitpid () from /lib/x86_64-linux-gnu/libc.so.6
[Current thread is 1 (process 4181)]
#0 0x00007f878b5ca4ca in waitpid () from /lib/x86_64-linux-gnu/libc.so.6
#1 0x0000000000400e69 in print_trace_gdb () at linux_backtrace_gdb.cpp:25
#2 0x0000000000400ed2 in rec_function (ii=0) at linux_backtrace_gdb.cpp:34
#3 0x0000000000400f29 in main () at linux_backtrace_gdb.cpp:43
---end test-----

关于c++ - Raspberry Pi 优化的 backtrace(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41698803/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com