Windows XP Windows 7 Windows 2003 Windows Vista Windows 綜合教學 Linux 系統教學
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟體教學
您现在的位置: Windows教程網 >> Windows教程 >  >> Linux 系統教學 >> Linux 教學

Linux Core Dump 核心轉儲完整指南

編輯:Linux 教學

什麼是 Core 文件?

程式崩潰時,作業系統可將進程的記憶體映像(含調試資訊)寫入磁碟,形成 core 文件。它是事後除錯的關鍵證據,配合 GDB 可還原崩潰現場。

啟用與控制 Core 生成

1. ulimit 命令(進程級)

ulimit -c 0          # 禁止生成(預設)
ulimit -c unlimited  # 無大小限制
ulimit -c 1024       # 限制 1024 KB
ulimit -a            # 查看所有限制

永久生效請修改 /etc/profile 或用戶 ~/.bash_profile。

2. 系統級配置(/proc/sys/kernel/)

文件作用典型值
core_uses_pid文件名是否包含 PID1 (包含)
core_pattern路徑與命名格式/corefile/core-%e-%p-%t

core_pattern 格式化參數

%p - PID          %u - UID          %g - GID
%s - 觸發信號     %t - UNIX 時間戳  %h - 主機名
%e - 程式名

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

使用 GDB 分析 Core 文件

# 基本用法
gdb -c core

# 指定程式(推薦)
gdb -c core.12345 MyServer

# 進入 GDB 後常用指令
bt          # 查看堆疊回溯
info reg    # 查看寄存器
frame N     # 切換堆疊幀

快速測試 Core 生成

kill -s SIGSEGV $$   # 向當前 Shell 發送段錯誤信號

常見問題排查:為何沒生成 Core?

  1. ulimit -c 為 0 → 修改為 unlimited 或具體大小。
  2. 目錄無寫入權限 → 檢查 core_pattern 路徑權限。
  3. 程式以 setuid/setgid 執行 → 安全機制禁止 core dump。
  4. /proc/sys/kernel/core_pattern 指向管道程式(如 systemd-coredump) → core 文件被管道程式攔截,需查詢對應工具(如 coredumpctl)。

總結

合理配置 ulimit 與 core_pattern,善用 GDB 分析,能大幅縮短線上故障定位時間。

Copyright © Windows教程網 All Rights Reserved