是否可以在异常(SEGFAULT)错误后自动重新启动程序
Is it possible to automaticly restart program in GDB after exception (segfault) error?
如何使用GDB在C 中运行程序,以便在错误异常(Segfaults)的情况下重新启动程序(我希望GDB自动使用"运行"命令),并且在同时,将任何错误记录到文件(命令" where")。
是否可能?
让我向您展示一个示例,该示例以3次重新启动程序,以防它崩溃。我使用Python脚本来处理Sigsegv(https://sourceware.org/gdb/onlinedocs/gdb/events-in-python.html)。
首先,这是GDB会话的一个示例:
>gdb -q -x restart.py ./a.out
Reading symbols from /home/a.out...done.
process id: 1700
Program received signal SIGSEGV, Segmentation fault.
0x000000000040060e in c () at main2.cpp:9
9 *ptr = *ptr +1;
#0 0x000000000040060e in c () at main2.cpp:9
#1 0x000000000040062a in main () at main2.cpp:15
process id: 1704
Program received signal SIGSEGV, Segmentation fault.
0x000000000040060e in c () at main2.cpp:9
9 *ptr = *ptr +1;
#0 0x000000000040060e in c () at main2.cpp:9
#1 0x000000000040062a in main () at main2.cpp:15
process id: 1705
Program received signal SIGSEGV, Segmentation fault.
0x000000000040060e in c () at main2.cpp:9
9 *ptr = *ptr +1;
#0 0x000000000040060e in c () at main2.cpp:9
#1 0x000000000040062a in main () at main2.cpp:15
process id: 1706
Program received signal SIGSEGV, Segmentation fault.
0x000000000040060e in c () at main2.cpp:9
9 *ptr = *ptr +1;
#0 0x000000000040060e in c () at main2.cpp:9
#1 0x000000000040062a in main () at main2.cpp:15
(gdb)
对于每次崩溃,创建了一个带有名称crash.file.pid的文件。这是文件的一个示例:
>more crash.file.1860
#0 0x000000000040060e in c () at main2.cpp:9
#1 0x000000000040062a in main () at main2.cpp:15
这是一个C 程序:
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
int c()
{
printf("process id: %dn", getpid());
int *ptr =0;
*ptr = *ptr +1;
return *ptr;
}
int main()
{
c();
return 0;
}
这是一个python脚本:
>cat restart.py
#!gdb
import sys
import gdb
import os
number_restarts = 3
def on_stop(sig):
global number_restarts
if isinstance(sig, gdb.SignalEvent) and sig.stop_signal == "SIGSEGV":
crash_file = "crash.file." + str( gdb.selected_inferior().pid)
gdb.execute("set logging file " + crash_file)
gdb.execute("set logging on")
gdb.execute("where")
gdb.execute("set logging off")
if (number_restarts > 0):
number_restarts -= 1
gdb.execute("set confirm off")
gdb.execute("kill")
gdb.execute("run")
gdb.events.stop.connect (on_stop)
gdb.execute("set pagination off")
gdb.execute("run")
相关文章:
- 程序崩溃并显示"std::out_of_range"错误
- xmake总是报告:错误:无法获取cxx的程序,为什么
- VSCode-有一个红色下划线,但程序构建和运行正确,并且出现配音错误
- 分段错误当我试图运行程序时出错
- 显示错误输出的简单数组排序程序
- 示例外壳应用程序显示的 V8 "segmentation fault (core dumped)"错误
- C++应用程序 MySQL odbc 数据库连接错误:在引发"otl_tmpl_exception<>"实例后终止调用
- 为什么程序在 c++ 中迭代 emtpy 向量时会抛出运行时错误
- 在气泡排序程序中未声明错误功能
- 我在c ++矩阵计算器程序中找不到错误
- 叮当格式化程序多行格式配置错误
- 全局向量导致 C++ 程序结束时出现段错误
- 运行时间错误:程序跳过提示,以获取第二名和第三个名称
- Glibc 2.7 配置错误程序太旧
- 运行时错误:程序已停止响应
- OpenCV for Android:在 PATH 中找不到错误程序"sh"
- C++中的 scanf 错误 - 程序没有响应
- c++语法错误:程序以非零状态退出
- 运行时错误C++程序:检测到glibc,munmap_chunk(),指针无效
- libxml2错误程序c++