我在分析核心文件时我的真正 gdb 脚本我尝试取消引用指针并得到“源命令文件中的错误:无法访问地址处的内存”,然后我的 gdb 脚本停止。我想要的只是继续执行我的 gdb 脚本而不停止。可能吗?
这是一个测试程序和一个测试 gdb 脚本,它演示了我的问题。在这种情况下,指针具有 NULL 值,但在实际情况下,指针将不具有 null 无效值。
这是测试C程序:
#include <stdio.h>
struct my_struct {
int v1;
int v2;
};
int main()
{
my_struct *p;
printf("%d %d\n", p->v1, p->v2);
return 0;
}
这是一个测试 gdb 脚本:
>cat analyze.gdb
p p->v1
q
这是问题的演示(我想从 gdb 获取此错误消息,然后执行处理quit
命令):
>gdb -silent a.out ./core.22384 -x ./analyze.gdb
Reading symbols from /a.out...done.
[New Thread 22384]
Core was generated by `./a.out'.
Program terminated with signal 11, Segmentation fault.
#0 0x0000000000400598 in main () at main.cpp:11
11 printf("%d %d\n", p->v1, p->v2);
./analyze.gdb:1: Error in sourced command file:
Cannot access memory at address 0x0
Missing separate debuginfos, use: debuginfo-install glibc-2.12-1.80.el6.x86_64
更新
感谢汤姆。这是处理此问题的 gdb 脚本:
>cat ./analyze.v2.gdb
python
def my_ignore_errors(arg):
try:
gdb.execute("print \"" + "Executing command: " + arg + "\"")
gdb.execute (arg)
except:
gdb.execute("print \"" + "ERROR: " + arg + "\"")
pass
my_ignore_errors("p p")
my_ignore_errors("p p->v1")
gdb.execute("quit")
这是它的工作原理:
>gdb -silent ./a.out -x ./analyze.v2.gdb -c ./core.15045
Reading symbols from /import/home/a.out...done.
[New Thread 15045]
Core was generated by `./a.out'.
Program terminated with signal 11, Segmentation fault.
#0 0x0000000000400598 in main () at main.cpp:11
11 printf("%d %d\n", p->v1, p->v2);
$1 = "Executing command: p p"
$2 = (my_struct *) 0x0
$3 = "Executing command: p p->v1"
$4 = "ERROR: p p->v1"
$5 = "Executing command: quit"