Skip to content

Commit

Permalink
modify post092
Browse files Browse the repository at this point in the history
  • Loading branch information
hilyso committed Nov 20, 2024
1 parent 0f9cca2 commit f69bd4d
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 82 deletions.
143 changes: 61 additions & 82 deletions source/_posts/092.coredump.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,83 +11,42 @@ tags:
> core dump又叫核心转储, 当程序运行过程中发生异常, 程序异常退出时, 由操作系统把程序当前的内存状况存储在一个core文件中, 叫core dump. (linux中如果内存越界会收到SIGSEGV信号,然后就会core dump)

## 一、 配置基于 systemd 的 core dump

## 一、 传统 core dump 配置
> 该模式下的核心转储文件默认保存在 ``` /var/lib/systemd/coredump ```, **默认保存三天**
> 该核心转储功能由`journal`配合产生, 因此控制转储文件的保存目录以及时间较难, 如需自定义, 推荐使用传统方式配置核心转储。
### 1.1 开启 core dump

编辑 ```/etc/security/limits.conf``` 文件,添加或修改以下行:

``` shell
* soft core unlimited
* hard core unlimited
```
该配置允许用户生成无限大小的core文件


### 1.2 配置命名模板以及自定义core文件存储目录

创建目录并修改权限如下:
``` mkdir /var/coredumps ```
``` chmod 1777 /var/coredumps ```

编辑 ```/etc/sysctl.conf``` 添加或修改以下行:

``` shell
kernel.core_pattern = /var/coredumps/core-%e-%s-%u-%g-%p-%t
```


- %e: 可执行文件名
- %s: 导致 core dump 的信号号
- %u: 进程的实际用户 ID
- %g: 进程的实际组 ID
- %p: 进程 ID
- %t: core dump 的时间(UNIX 时间戳)

应用并生效
``` sysctl -p ```


## 二、 配置基于 systemd 的 core dump


编辑 ``` /etc/systemd/system.conf``` 文件,找到并修改(或添加)以下行:
编辑文件 ``` /etc/systemd/system.conf``` ,添加/修改以下行:

``` shell
DumpCore=yes
DefaultLimitCORE=infinity
```

编辑 ``` /etc/systemd/coredump.conf ``` 文件, 保持默认配置即可
编辑文件 ``` /etc/systemd/coredump.conf ``` , 添加/修改一下行:

``` shell
[Coredump]
#Storage=external
#Compress=yes
#ProcessSizeMax=2G
#ExternalSizeMax=2G
#JournalSizeMax=767M
#MaxUse=
#KeepFree=
Storage=external # 将核心转储存储在文件系统中
Compress=yes # 是否压缩转储文件
ProcessSizeMax=2G # 大小限制 (0 表示不限制)
ExternalSizeMax=2G
JournalSizeMax=767M
MaxUse=5G
KeepFree=1G
```

重载 systemd 配置

``` systemctl daemon-reexec ```

编辑 ```/etc/sysctl.conf``` 添加或修改以下行:
编辑文件 ```/etc/sysctl.conf``` 添加/修改以下行:

``` shell
kernel.core_pattern=|/usr/lib/systemd/systemd-coredump %P %u %g %s %t %c %h
```

应用并生效
``` sysctl -p ```
应用上述配置并生效: `systemctl daemon-reexec && sysctl -p `



## 、 core dump 实例分析
## 、 core dump 实例分析


使用一个简单的 C 程序验证 core dump
Expand All @@ -108,45 +67,65 @@ int main() {
![图片](/images/092.coredump.md.01.png)


### 2.2 基于systemd的 coredump 处理

### 3.1 传统方式 coredump 处理

使用 gdb 工具调试 core dump 文件
- 列出所有核心转储
`coredumpctl list`
![图片](/images/092.coredump.md.03.png)
- `PID`: 崩溃进程的 PID。
- `SIG`: 导致崩溃的信号编号(如 11 表示 SIGSEGV)
- `COREFILE`: 表示核心转储是否存在(present 或 missing)

``` gdb <path_to_executable> <path_to_core_dump_file> ```

![图片](/images/092.coredump.md.02.png)

### 3.2 基于systemd的 coredump 处理

该模式下的核心转储文件默认保存在 ``` var/lib/systemd/coredump ```, 默认保存三天
- 查看特定核心转储的详细信息
`coredumpctl info <PID>`
![图片](/images/092.coredump.md.04.png)

![图片](/images/092.coredump.md.03.png)
- 导出核心转储文件
`coredumpctl dump <PID>`
![图片](/images/092.coredump.md.05.png)

使用 coredumpctl 查看和管理核心转储
- 分析核心转储
`coredumpctl gdb <PID>`
![图片](/images/092.coredump.md.06.png)

- 列出核心转储

``` coredump list ```
``` * ``` 表示已存储的核心
![图片](/images/092.coredump.md.04.png)

## 三、 传统方式配置 coredump

- 查看特定进程的核心转储
> 与基于 coredumpctl 方式不同, 该方式下, 转储文件会直接生成
``` coredumpctl list <process_name> ```

### 3.1 开启coredump

- 查看核心转储的详细信息
- 启用核心转储
`ulimit -c unlimited`

``` coredumpctl info <PID> ```
- 编辑 `/etc/security/limits.conf` 文件,添加或修改以下行:
``` bash
* soft core unlimited
* hard core unlimited
```
该配置允许用户生成无限大小的core文件

![图片](/images/092.coredump.md.05.png)
### 3.2 自定义转储文件的目录及大小

- 分析核心转储文件
- 编辑文件 `/etc/sysctl.conf` 添加/修改以下行:
``` bash
kernel.core_pattern = /var/coredumps/core-%e-%s-%u-%g-%p-%t
```
- %e: 可执行文件名
- %s: 导致 core dump 的信号号
- %u: 进程的实际用户 ID
- %g: 进程的实际组 ID
- %p: 进程 ID
- %t: core dump 的时间(UNIX 时间戳)

``` coredumpctl gdb <PID> ```
- 创建转储目录
`mkdir /var/coredumps`
`chmod 777 /var/coredumps`

- 删除核心转储文件
- 应用并生效
`sysctl -p`

``` coredumpctl remove <PID> ```
- 测试
可以看见, 该方式下, 转储文件直接生成
![图片](/images/092.coredump.md.07.png)
Binary file modified source/images/092.coredump.md.03.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified source/images/092.coredump.md.04.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified source/images/092.coredump.md.05.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added source/images/092.coredump.md.06.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added source/images/092.coredump.md.07.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit f69bd4d

Please sign in to comment.