程序是通過systemd監管,當程序出現crash的時候,需要保存crash的日志,也就是coredump日志,按照一般做法設置coredump。而在安裝有systemd服務的系統中一般都有systemd-coredump服務。
systemd-coredump 是 systemd 子系統中的一個工具,用于 收集、處理和存儲 Linux 系統中的 core dump 文件。它是現代 Linux 系統中處理進程崩潰信息的推薦方式,替代了傳統的直接生成 core 文件。
systemd-coredump的主要功能
功能 | 說明 |
---|---|
📦 捕獲崩潰進程的核心轉儲 | 接管核心轉儲處理,替代傳統 core 文件 |
🧾 保存或丟棄 core 文件 | 可配置是否保存到磁盤、壓縮、限制大小 |
🗂 統一存儲 | 所有 core 文件保存到 /var/lib/systemd/coredump/ |
🔍 提供調試接口 | coredumpctl 命令用于列出、查看、調試 core 文件 |
🧱 安全性 | 默認以受限權限運行,不會泄露敏感信息 |
安裝systemd-coredump服務
檢查是否已安裝
$ which coredumpctl
/usr/bin/coredumpctl
這里會輸出coredumpctl所在的位置,而我最開始的時候,系統是沒有安裝coredumpctl。
安裝systemd-coredump服務
$ sudo apt-get install systemd-coredump
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following NEW packages will be installed:systemd-coredump
0 upgraded, 1 newly installed, 0 to remove and 261 not upgraded.
Need to get 93.0 kB of archives.
After this operation, 374 kB of additional disk space will be used.
Get:1 https://mirrors.aliyun.com/debian-security bookworm-security/main arm64 systemd-coredump arm64 252.38-1~deb12u1 [93.0 kB]
Fetched 93.0 kB in 1s (181 kB/s)
Selecting previously unselected package systemd-coredump.
(Reading database ... 156369 files and directories currently installed.)
Preparing to unpack .../systemd-coredump_252.38-1~deb12u1_arm64.deb ...
Unpacking systemd-coredump (252.38-1~deb12u1) ...
Setting up systemd-coredump (252.38-1~deb12u1) ...Configuration file '/etc/systemd/coredump.conf'==> File on system created by you or by a script.==> File also in package provided by package maintainer.What would you like to do about it ? Your options are:Y or I : install the package maintainer's versionN or O : keep your currently-installed versionD : show the differences between the versionsZ : start a shell to examine the situationThe default action is to keep your current version.
*** coredump.conf (Y/I/N/O/D/Z) [default=N] ? Y
Installing new version of config file /etc/systemd/coredump.conf ...
Creating group 'systemd-coredump' with GID 991.
Creating user 'systemd-coredump' (systemd Core Dumper) with UID 991 and GID 991.
Processing triggers for man-db (2.11.2-2) ...
coredump.conf --> 這里輸入Y
配置coredump.conf
[Coredump]
Storage=external
Compress=yes
ProcessSizeMax=2G
ExternalSizeMax=2G
JournalSizeMax=767M
MaxUse=2G
#KeepFree=
檢查core_pattern
在安裝systemd-coredump服務時,已經做了對應的配置
cat /proc/sys/kernel/core_pattern
|/lib/systemd/systemd-coredump %P %u %g %s %t 9223372036854775808 %h %d
reload systemd
sudo systemctl daemon-reexec
測試
測試程序
// crash.cpp
int main() {int* p = nullptr;*p = 1;
}
編譯&觸發coredump
$ g++ crash.cpp -o crash
ulimit -c unlimited
./crash
檢查systemd-coredump的輸出
$ coredumpctl list
TIME PID UID GID SIG COREFILE EXE SIZE
Wed 2025-06-11 01:49:07 BST 357407 1000 1000 SIGSEGV present /home/pi/workspace/core/crash 35.5K
具體coredump信息
$ coredumpctl info crashPID: 357407 (crash)UID: 1000 (pi)GID: 1000 (pi)Signal: 11 (SEGV)Timestamp: Wed 2025-06-11 01:49:06 BST (2min 22s ago)Command Line: ./crashExecutable: /home/pi/workspace/core/crashControl Group: /user.slice/user-1000.slice/session-67.scopeUnit: session-67.scopeSlice: user-1000.sliceSession: 67Owner UID: 1000 (pi)Boot ID: 9f37ddcf177845c5b33baf5ede4df169Machine ID: 3193cd42b55048b38d93ded435763681Hostname: raspberrypi-CM5Storage: /var/lib/systemd/coredump/core.crash.1000.9f37ddcf177845c5b33baf5ede4df169.357407.1749602946000000.zst (present)Size on Disk: 35.5KMessage: Process 357407 (crash) of user 1000 dumped core.Stack trace of thread 357407:#0 0x000055562d490724 n/a (/home/pi/workspace/core/crash + 0x724)#1 0x00007fffb2fc7740 __libc_start_call_main (libc.so.6 + 0x27740)#2 0x000055562d490714 n/a (/home/pi/workspace/core/crash + 0x714)ELF object binary architecture: AARCH64
coredump debug
$ coredumpctl gdb crashPID: 357407 (crash)UID: 1000 (pi)GID: 1000 (pi)Signal: 11 (SEGV)Timestamp: Wed 2025-06-11 01:49:06 BST (2min 5s ago)Command Line: ./crashExecutable: /home/pi/workspace/core/crashControl Group: /user.slice/user-1000.slice/session-67.scopeUnit: session-67.scopeSlice: user-1000.sliceSession: 67Owner UID: 1000 (pi)Boot ID: 9f37ddcf177845c5b33baf5ede4df169Machine ID: 3193cd42b55048b38d93ded435763681Hostname: raspberrypi-CM5Storage: /var/lib/systemd/coredump/core.crash.1000.9f37ddcf177845c5b33baf5ede4df169.357407.1749602946000000.zst (present)Size on Disk: 35.5KMessage: Process 357407 (crash) of user 1000 dumped core.Stack trace of thread 357407:#0 0x000055562d490724 n/a (/home/pi/workspace/core/crash + 0x724)#1 0x00007fffb2fc7740 __libc_start_call_main (libc.so.6 + 0x27740)#2 0x000055562d490714 n/a (/home/pi/workspace/core/crash + 0x714)ELF object binary architecture: AARCH64GNU gdb (Debian 13.1-3) 13.1
Copyright (C) 2023 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 "aarch64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<https://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 /home/pi/workspace/core/crash...
(No debugging symbols found in /home/pi/workspace/core/crash)
[New LWP 357407]
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/aarch64-linux-gnu/libthread_db.so.1".
Core was generated by `./crash'.
Program terminated with signal SIGSEGV, Segmentation fault.
#0 0x000055562d490724 in main ()
(gdb) bt
#0 0x000055562d490724 in main ()
(gdb) quit
自定義service配置
[Unit]
Description=application
After=custom.service
Requires=xxxx.service[Service]
LimitCORE=infinity
LimitSTACK=infinity
Type=simple
ExecStart=/custom/app/custom
Restart=on-failure
TimeoutStopSec=30s
WorkingDirectory=/custom[Install]
WantedBy=multi-user.target
設置項 | 說明 |
---|---|
LimitCORE=infinity | 允許 systemd 啟動的程序生成 core dump |
WorkingDirectory | core 文件會默認保存到這里(如果未被 systemd-coredump 截獲) |
Restart=on-failure | 保證崩潰時自動重啟進程(但不影響 core dump) |