GDB学习入门之gdb准备2

本文详细介绍了如何在Linux系统下使用GDB调试core文件,包括core文件的生成、环境配置、大小设置及存储路径定制。通过开启core文件生成机制,设置core文件大小为无限,并指定其存储位置,可以有效进行程序崩溃分析。同时,文章提供了core调试的实例,演示了从产生core文件到使用GDB进行调试的全过程。
摘要由CSDN通过智能技术生成

GDB文章目录

第一章 GDB 学习入门之GDB初识
第二章 GDB学习入门之gdb准备
第三章 GDB学习入门之gdb准备2


前言

文章主要是讲解gdb 的第三种调试方式,由于此方式涉及的信息比较多,在这里新开一章节进行内容的详述。介绍linux core 文件的生成原因,core文件的相关配置。core 文件调试等。


一、core简介

服务器程序运行一段时间后会出现突然崩溃,这不是我们希望看到的,系统程序崩溃这是比较大的事故。我们需要及时解决程序奔溃问题,需要定位程序崩溃的原因等。只要程序在崩溃的时候有 core 文件产生,就可以使用这个 core 文件来定位崩溃的原因。linux 系统默认是不开启程序崩溃产生core文件这一机制的

二、core环境

1.core机制是否开启

来定位崩溃的原因。当然, Linux 系统默认是不开启程序崩溃产生 core 文件这一机制的,我们可以使用:

1.1检查是否开启

检查命令: ulimit -c 
[root@localhost ~]# ulimit -a
core file size          (blocks, -c) 0
data seg size           (kbytes, -d) unlimited
scheduling priority             (-e) 0
file size               (blocks, -f) unlimited
pending signals                 (-i) 256507
max locked memory       (kbytes, -l) 64
max memory size         (kbytes, -m) unlimited
open files                      (-n) 1024
pipe size            (512 bytes, -p) 8
POSIX message queues     (bytes, -q) 819200
real-time priority              (-r) 0
stack size              (kbytes, -s) 8192
cpu time               (seconds, -t) unlimited
max user processes              (-u) 256507
virtual memory          (kbytes, -v) unlimited
file locks                      (-x) unlimited
[root@localhost ~]# 

根据输出信息分析 core file size (blocks, -c) 0 默认是0,表示关闭生成core文件。 core文件的大小可以通过ulimit 选 项名 设置值来修改。
设置值”来修改。例如,可以将 core 文件生成改成具体某个值(最大允许的字节数),这里我们使用 ulimit -c unlimited unlimited 是 -c 选项值)直接修改成 不限 制大小 。

2.设置core文件大小

设置命令 ulimit -c 选项值
默认设置最大不限制 ulimit -c unlimited

3.设置开机默认启动

ulimit -c unlimited 放入 /etc/profile中,然后执行 source /etc/profile即可立即生效。
步骤如下

将 ulimit -c unlimited 放入 /etc/profile
source /etc/profile
再次查看 ulimit -a

结果

[root@localhost ~]# ulimit -a
core file size          (blocks, -c) unlimited
data seg size           (kbytes, -d) unlimited
scheduling priority             (-e) 0
file size               (blocks, -f) unlimited
pending signals                 (-i) 256507
max locked memory       (kbytes, -l) 64
max memory size         (kbytes, -m) unlimited
open files                      (-n) 1024
pipe size            (512 bytes, -p) 8
POSIX message queues     (bytes, -q) 819200
real-time priority              (-r) 0
stack size              (kbytes, -s) 8192
cpu time               (seconds, -t) unlimited
max user processes              (-u) 256507
virtual memory          (kbytes, -v) unlimited
file locks                      (-x) unlimited
[root@localhost ~]# 


三、core 文件存储设置

系统默认corefile是生成在程序的执行目录下或者程序启动调用了 chdir之后的目录 ,我们可以通过设置生成 corefile的格式来控制它,让其生成在固定的目录下 对应的是自己的目录,不要直接写我的目录 )),并让每次 启动后自动 生效。

(1) 在 /etc/sysctl.conf写入 corefile文件生成的目录。

命令: kernel.core_pattern=/home/lqf/core_dump/core --%e --%p --%t
在这里插入图片描述
(2 )创建应对的生成目录
命令: mkdir /home/lqf/core_dump
(3) 然后执行 生效
命令: sudo sysctl p /etc/sysctl.conf

其中:/home/lqf/core_dump/ 对应自己要存放的目录, core-%e-%p-%t 文件格式
格式总结:

格式含义
%%:相当于 %
%p:相当于 <pid>
%u:相当于 <uid>
%g:相当于 <gid>
%s:相当于导致 dump的信号的数字
%t:相当于 dump的时间
%e:相当于执行文件的名称
%h:相当于 hostname

4)可以使用 cat去查看路径是否生效

命令: cat /proc/sys/kernel/core_pattern

生效则显示
在这里插入图片描述

四、core 调试例子

demo:

#include <stdio.h>

int main(int argc, char *argv[]) {
        printf("hello world! dump core for set value to NULL pointer\n");
        
        *(char *)0=0;


        return 0;
}

# 编译:
gcc -g -o core_dump core_dump.c

# 生成段错误
./core_dump

查看生成的core 文件

root@VM-24-3-ubuntu:~# ls
core-core_dump-2992783-1652713786  core_dump  core_dump.c

执行gdb 调试

root@VM-24-3-ubuntu:~# gdb ./core_dump   core-core_dump-2992783-1652713786 
GNU gdb (Ubuntu 9.2-0ubuntu1~20.04.1) 9.2
Copyright (C) 2020 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Type "show copying" and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
    <http://www.gnu.org/software/gdb/documentation/>.

For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from ./core_dump...
[New LWP 2992783]
Core was generated by `./core_dump'.
Program terminated with signal SIGSEGV, Segmentation fault.
#0  main (argc=1, argv=0x7ffcd8446ad8) at core_dump.c:6
6               *(char *)0=0;
(gdb) quit

总结

提示:这里对文章进行总结:
此篇主要是介绍gdb 如何调试core 文件,以及core文件环境的设置,和core机制的开启等。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

技术鱼

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值