如何获取有关 C++ 中崩溃程序的一些信息以供将来调试
How can I get some information about crashed program in c++ for future debugging
我想知道在c++中程序崩溃时有什么方法可以捕获一些信息。此信息可用于开发人员未来的调试。例如,此信息提供了我的程序在哪行代码和哪个函数中崩溃或最好是为什么崩溃。
这个概念在 Windows 上称为 故障转储 或在 Linux 上称为 核心转储。它不是 C++ 的特性,而是操作系统的特性。它适用于编译为本机汇编程序指令的代码(C++ 通常是这种情况)。
会保存崩溃程序在崩溃时的状态,这样你就可以查看调用堆栈、内存、寄存器等。信息量可以配置。然后在调试器的帮助下完成分析,例如windbg 在 Windows。
对于行号,请注意与二进制文件一起构建符号。为了进行分析,您需要将故障转储与正确的符号放在一起以获得行号。
How can I get some information about crashed program in c++ for future debugging
这是特定于实现的。
(实际上,编译器特定和 operating system specific; I am taking a Linux 中心观点)
C++11 是一种编程语言,由英文规范 (n3337) 定义。它不是软件。
你通常使用一些 compiler to compile your C++ code. I recommend using a recent version of GCC or of Clang. If you use it, compile your C++ code with all warnings and debug info, that is with g++ -Wall -Wextra -g
or clang++ -Wall -Wextra -g
at least. Some compilers are capable of optimization while emitting debug information (e.g. you could run g++ -Wall -Wextra -g -Og
, as we do in RefPerSys). You could sometimes also generate C++ code with tools like ANTLR or GNU bison -or even some other C++ program- (and such an approach is called metaprogramming).
使用 DWARF debug information (perhaps also using a build automation tool like GNU make), you could (on Linux at least) use the GDB debugger. Since gdb(1) can deal with a core(5) dump (see signal(7)).
编译 C++ 代码后
在不影响上面所说的情况下,我认为您需要构建一个信号处理程序,它可以执行回溯到代码中被破坏的地方。类似的东西
void signalHandler(int signal)
{
void *array[500];
size_t size;
// get void*'s for all entries on the stack
size = backtrace(array, 500);
// print out all the frames to stderr
fprintf(stderr, "Error: signal %d:\n", signal);
backtrace_symbols_fd(array, size, STDERR_FILENO);
exit(1);
}
void registerSignalHandler()
{
signal(SIGSEGV, signalHandler);
}
int main(int argc, char* argv[])
{
registerSignalHandler();
// your code
return 1;
}
google::InstallFailureSignalHandler();
可以给我们提供信息。它还会自动打印调用堆栈。
这是非常容易使用。
欲了解更多信息,请参阅
http://rpg.ifi.uzh.ch/docs/glog.html
我想知道在c++中程序崩溃时有什么方法可以捕获一些信息。此信息可用于开发人员未来的调试。例如,此信息提供了我的程序在哪行代码和哪个函数中崩溃或最好是为什么崩溃。
这个概念在 Windows 上称为 故障转储 或在 Linux 上称为 核心转储。它不是 C++ 的特性,而是操作系统的特性。它适用于编译为本机汇编程序指令的代码(C++ 通常是这种情况)。
会保存崩溃程序在崩溃时的状态,这样你就可以查看调用堆栈、内存、寄存器等。信息量可以配置。然后在调试器的帮助下完成分析,例如windbg 在 Windows。
对于行号,请注意与二进制文件一起构建符号。为了进行分析,您需要将故障转储与正确的符号放在一起以获得行号。
How can I get some information about crashed program in c++ for future debugging
这是特定于实现的。
(实际上,编译器特定和 operating system specific; I am taking a Linux 中心观点)
C++11 是一种编程语言,由英文规范 (n3337) 定义。它不是软件。
你通常使用一些 compiler to compile your C++ code. I recommend using a recent version of GCC or of Clang. If you use it, compile your C++ code with all warnings and debug info, that is with g++ -Wall -Wextra -g
or clang++ -Wall -Wextra -g
at least. Some compilers are capable of optimization while emitting debug information (e.g. you could run g++ -Wall -Wextra -g -Og
, as we do in RefPerSys). You could sometimes also generate C++ code with tools like ANTLR or GNU bison -or even some other C++ program- (and such an approach is called metaprogramming).
使用 DWARF debug information (perhaps also using a build automation tool like GNU make), you could (on Linux at least) use the GDB debugger. Since gdb(1) can deal with a core(5) dump (see signal(7)).
编译 C++ 代码后在不影响上面所说的情况下,我认为您需要构建一个信号处理程序,它可以执行回溯到代码中被破坏的地方。类似的东西
void signalHandler(int signal)
{
void *array[500];
size_t size;
// get void*'s for all entries on the stack
size = backtrace(array, 500);
// print out all the frames to stderr
fprintf(stderr, "Error: signal %d:\n", signal);
backtrace_symbols_fd(array, size, STDERR_FILENO);
exit(1);
}
void registerSignalHandler()
{
signal(SIGSEGV, signalHandler);
}
int main(int argc, char* argv[])
{
registerSignalHandler();
// your code
return 1;
}
google::InstallFailureSignalHandler();
可以给我们提供信息。它还会自动打印调用堆栈。
这是非常容易使用。
欲了解更多信息,请参阅
http://rpg.ifi.uzh.ch/docs/glog.html