异常(段错误)错误后,是否可以在GDB中自动重启程序?

时间:2014-03-13 06:38:47

标签: c++ file logging gdb

如何使用GDB在C ++中运行程序,以便在出现错误异常(segfaults)时重新启动程序(我希望GDB自动使用“run”命令),同时将任何错误记录到文件中(命令“where”)。

有可能吗?

1 个答案:

答案 0 :(得分:4)

让我向您展示一个示例,在程序崩溃的情况下重启程序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: %d\n", 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")