GDB学习入门之gdb准备2

简介: 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机制的开启等。

相关实践学习
阿里云图数据库GDB入门与应用
图数据库(Graph Database,简称GDB)是一种支持Property Graph图模型、用于处理高度连接数据查询与存储的实时、可靠的在线数据库服务。它支持Apache TinkerPop Gremlin查询语言,可以帮您快速构建基于高度连接的数据集的应用程序。GDB非常适合社交网络、欺诈检测、推荐引擎、实时图谱、网络/IT运营这类高度互连数据集的场景。 GDB由阿里云自主研发,具备如下优势: 标准图查询语言:支持属性图,高度兼容Gremlin图查询语言。 高度优化的自研引擎:高度优化的自研图计算层和存储层,云盘多副本保障数据超高可靠,支持ACID事务。 服务高可用:支持高可用实例,节点故障迅速转移,保障业务连续性。 易运维:提供备份恢复、自动升级、监控告警、故障切换等丰富的运维功能,大幅降低运维成本。 产品主页:https://www.aliyun.com/product/gdb
目录
相关文章
|
1月前
|
NoSQL Shell Linux
GDB学习入门之gdb准备
GDB学习入门之gdb准备
30 0
|
1月前
|
NoSQL 小程序 C语言
GDB调试学习(四):段错误
GDB调试学习(四):段错误
61 0
|
1月前
|
NoSQL
GDB调试学习(三):观察点
GDB调试学习(三):观察点
48 0
|
1月前
|
NoSQL
GDB调试学习(二):断点
GDB调试学习(二):断点
47 0
|
1月前
|
NoSQL IDE Linux
Linux的学习之路:8、Linux调试器-gdb使用
Linux的学习之路:8、Linux调试器-gdb使用
33 0
|
1月前
|
NoSQL Shell C语言
GDB调试学习(一):单步执行和跟踪函数调用
GDB调试学习(一):单步执行和跟踪函数调用
88 1
|
1月前
|
NoSQL 编译器 Linux
GDB 学习入门之GDB初识
GDB 学习入门之GDB初识
44 0
|
1月前
|
NoSQL Linux Android开发
OPENJTAG调试学习(三):使用 gdb 命令行进行调试
OPENJTAG调试学习(三):使用 gdb 命令行进行调试
65 0
|
1月前
|
NoSQL 搜索推荐 openCL
【C/C++ 调试 GDB指南 】gdb调试基本操作
【C/C++ 调试 GDB指南 】gdb调试基本操作
152 2
|
5天前
|
NoSQL Linux C语言
Linux gdb调试的时候没有对应的c调试信息库怎么办?
Linux gdb调试的时候没有对应的c调试信息库怎么办?
13 1