0Pricing
Production Debugging & Incident Response Playbook · 课时

使用核心转储进行事后调试

学习分析核心转储和崩溃报告,以便在无法实时访问系统时调试过去发生的问题

使用核心转储进行事后调试 是 CoddyKit 上的免费 Production Debugging & Incident Response Playbook 课时。 这是第 2 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Production Debugging & Incident Response Playbook 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Production Debugging & Incident Response Playbook 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

Debugging After the Fact

Welcome! In this lesson, we'll explore post-mortem debugging. This powerful technique lets you investigate software failures after they've occurred, without needing to reproduce the issue live.

It's incredibly useful when you can't attach a debugger directly to a crashing application, especially in production environments.

What's a Core Dump?

The cornerstone of post-mortem debugging is the core dump. Think of it as a snapshot of a program's entire memory space and CPU state at the exact moment it crashed.

  • It's a file generated by the operating system.
  • It contains critical information about the program's execution.
  • It helps you understand why a crash happened.

When Do Core Dumps Happen?

Core dumps are typically generated when a program encounters a severe, unhandled error that causes it to terminate unexpectedly. Common scenarios include:

  • Segmentation Faults (Segfaults): Accessing invalid memory.
  • Unhandled Exceptions: Language-specific errors not caught by the program.
  • Assertion Failures: When a program's internal assumptions are violated.
  • Program Crashes: Any abrupt, abnormal termination.

Enabling Core Dumps (Linux)

On Linux systems, core dump generation might be disabled by default or limited in size. You can enable it:

  • Temporarily: Use ulimit -c unlimited in your shell session.
  • System-wide: Modify /etc/sysctl.conf (e.g., kernel.core_pattern to specify output path and filename format).

Without proper configuration, your system might not save core dumps when crashes occur.

Core Dump Contents

A core dump is packed with forensic data. It typically includes:

  • Memory Image: A copy of the program's entire virtual memory.
  • CPU Register Values: The state of the CPU registers at the crash time.
  • Stack Trace: The sequence of function calls leading up to the crash.
  • Process Information: Process ID, signal that caused the crash, executable path.
  • Loaded Libraries: Information about shared libraries linked to the program.

Key Analysis Tools

To make sense of a core dump, you need specialized tools. Some popular ones include:

  • GDB (GNU Debugger): Widely used for C/C++ programs on Linux/Unix.
  • WinDbg: Microsoft's powerful debugger for Windows applications.
  • jstack/jmap: For Java applications, these tools can extract thread dumps and memory maps that act as a form of 'core dump'.
  • Delve: A debugger for Go programs.

We'll focus on GDB as a common example.

Crash Program Demo

Let's look at a simple C program that will intentionally cause a segmentation fault. This will generate a core dump file if your system is configured to do so.

Try compiling and running this code:

#include <stdio.h>
#include <stdlib.h>

int main() {
  int *ptr = NULL; // Declare a null pointer
  printf("Attempting to dereference a null pointer...\n");
  *ptr = 10;       // This line will cause a segmentation fault
  printf("This line will not be reached.\n");
  return 0;
}

Basic GDB Usage: Backtrace

After the program crashes and creates a core dump (e.g., core or core.PID), you can load it into GDB. Assuming your executable is a.out:

gdb ./a.out core
bt

The bt (backtrace) command is essential. It shows the call stack leading to the crash, helping you pinpoint the exact function and line number where the error occurred.

Inspecting Variables with GDB

Once you have the backtrace, you can navigate the stack frames (e.g., using frame N where N is the frame number). Then, you can inspect variable values at that point in time:

  • print variable_name: Shows the value of a specific variable.
  • info locals: Lists all local variables in the current stack frame and their values.

This helps you understand the state of the program's data when it crashed.

Core Dump Quiz

Let's check your understanding of core dumps.

Recap: Post-mortem Power

Great work! You've learned about the power of post-mortem debugging using core dumps.

  • Core dumps are memory snapshots of crashed programs.
  • They contain vital info like stack traces and variable states.
  • Tools like GDB help analyze them without live access.

This technique is indispensable for debugging hard-to-reproduce or production-only issues, enabling you to fix problems even after the event.

常见问题解答

「使用核心转储进行事后调试」课时是免费的吗?

是的 — 「使用核心转储进行事后调试」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Production Debugging & Incident Response Playbook 课程的其余内容,请升级到 CoddyKit PRO。 Production Debugging & Incident Response Playbook 课程共包含 4 节课。

「使用核心转储进行事后调试」这节课中我会学到什么?

学习分析核心转储和崩溃报告,以便在无法实时访问系统时调试过去发生的问题 你通过在浏览器中直接运行的动手代码来练习 Production Debugging & Incident Response Playbook,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Production Debugging & Incident Response Playbook 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Production Debugging & Incident Response Playbook 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 2 节课,共 4 节。

「使用核心转储进行事后调试」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Production Debugging & Incident Response Playbook 课中编写并运行代码吗?

能。每节 Production Debugging & Incident Response Playbook 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 远程调试在线应用
  2. 使用核心转储进行事后调试
  3. 内存与 CPU 性能分析技术
  4. 用分布式追踪定位延迟热点
← 返回 Production Debugging & Incident Response Playbook