25. Linux下gdb调试

1.什么是core文件?
有问题的程序运行后,产生“段错误 (核心已转储)”时生成的具有堆栈信息和调试信息的文件。

编译时需要加 -g 选项使程序生成调试信息: gcc -g core_test.c -o core_test

2.怎样配置生成 core 文件
(1)core文件开关
    ①使用 ulimit -c 查看core开关,如果为0表示关闭,不会生成core文件;

    ②使用 ulimit -c [filesize] 设置core文件大小,当最小设置为4之后才会生成core文件;

    ③使用 ulimit -c unlimited 设置core文件大小为不限制,这是常用的做法;

    ④如果需要开机就执行,则需要将这句命令写到 /etc/profile 等文件。

(2)core文件命名和保存路径

1)/proc/sys/kernel/core_uses_pid可以控制core文件的文件名中是否添加pid作为扩展。文件内容为1,表示添加pid作为扩展名,生成的core文件格式为core.xxxx;为0则表示生成的core文件同一命名为core。
可通过以下命令修改此文件:
echo “1″ > /proc/sys/kernel/core_uses_pid

2)proc/sys/kernel/core_pattern可以控制core文件保存位置和文件名格式。
可通过以下命令修改此文件:
echo “/corefile/core-%e-%p-%t” > core_pattern,可以将core文件统一生成到/corefile目录下,产生的文件名为core-命令名-pid-时间戳

    ①core文件有默认的名称和路径,但为了方便,我们通常会自己命名和指定保存路径;

    ②可以通过 /proc/sys/kernel/core_pattern 设置 core 文件名和保存路径,方法如下:

echo "/corefile/core-%e-%p-%t" > /proc/sys/kernel/core_pattern

命名的参数列表: 

  %p - insert pid into filename 添加pid 
    %u - insert current uid into filename 添加当前uid 
    %g - insert current gid into filename 添加当前gid 
    %s - insert signal that caused the coredump into the filename 添加导致产生core的信号 
    %t - insert UNIX time that the coredump occurred into filename 添加core文件生成时的unix时间 
    %h - insert hostname where the coredump happened into filename 添加主机名 
    %e - insert coredumping executable name into filename 添加命令名。

3.调试core文件

1)gdb -core=core.xxxx
file ./a.out
bt
2)gdb -c core.xxxx
file ./a.out
bt

    (1)方法1: gdb [exec file] [core file] 然后执行bt看堆栈信息:

    (2)方法②:gdb -c [core file],然后 file [exec file],最后再使用 bt 查看错误位置:

————————————————
版权声明:本文为CSDN博主「wkd_007」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/wkd_007/article/details/79757289

猜你喜欢

转载自www.cnblogs.com/life-Meer/p/11746850.html