On this page
Tracing and Debugging
Coverage: ftrace (function/function_graph) → tracepoints → kprobes → perf events → eBPF tracing (bpftrace/bcc) → kdump/crash → KGDB → printk and dynamic debugging Kernel versions: 2.6 ~ 6.x
ftrace: Built-in Kernel Tracer
Function Tracer
# List available tracers
# function: trace all function calls (very high overhead, for debugging only)
Function Graph: With Timing
# Output:
# 0) | tcp_v4_rcv() {
# 0) 0.234 us | tcp_checksum_complete();
# 0) 0.567 us | tcp_v4_do_rcv() {
# 0) 1.234 us | tcp_rcv_established();
# 0) 2.345 us | }
# 0) 3.456 us | }
Dynamic ftrace: Enable/Disable at Runtime
# Dynamic function probes (via debugfs):
Tracepoints: Stable Probe Points
# All available tracepoints
# sched/ syscalls/ net/ block/ irq/ timer/ ...
# Trace all exec calls:
# Trace scheduler:
# Trace block IO:
Perf Events: Hardware Performance Counters
# perf stat: Count hardware events during program execution
# perf record: Sampling (basis for flame graphs)
# perf top: Real-time hotspots
# Hardware events:
# cycles, instructions, cache-references, cache-misses,
# branch-instructions, branch-misses, bus-cycles,
# stalled-cycles-frontend, stalled-cycles-backend
eBPF Tracing: bpftrace
# bpftrace: High-level tracing language in DTrace style
# One-line tracing:
# Histograms:
# Aggregation:
bcc Toolset
# bcc: Pre-compiled toolset using Python + BPF (/usr/share/bcc/tools/)
printk and Dynamic Debugging
printk Log Levels
// include/linux/kern_levels.h
// Control console log level at runtime:
echo 4 > /proc/sys/kernel/printk // Show only ERROR and above
Dynamic Debug (dyndbg)
# Enable debug output for specific modules at runtime (no recompilation needed)
# View currently enabled dynamic debug entries
|
kdump: Crash Dump
# 1. Load capture kernel (at boot or runtime)
# 2. Trigger panic → capture kernel starts → vmcore generated
# 3. Analyze:
# crash commands:
KGDB: Kernel Debugger
Remote kernel debugging via serial port (similar to gdb server):
→ Set breakpoints, step execution, read registers, read memory
→ However, only usable in development/debugging environments (requires special configuration)
Enable: kgdboc=ttyS0,115200 kgdbwait (boot parameter)
Connect: gdb vmlinux → target remote /dev/ttyS0
Practice: Steps for Diagnosing Performance Issues
1. Use perf top to identify CPU hotspots
2. Use bpftrace/bcc to examine latency distributions (biolatency, runqlat, tcpretrans)
3. Use ftrace function_graph to trace call timing in critical paths
4. Use tracepoints to validate hypotheses (e.g., is sched_switch frequency too high?)
5. Write custom tools with eBPF (if necessary)
References
- Source Code:
kernel/trace/,kernel/kexec_core.c,kernel/printk/ - Books: "BPF Performance Tools" (Brendan Gregg), "Systems Performance" (same author)
- Tools: perf, bpftrace, bcc, crash, trace-cmd (ftrace frontend)
Keywords: ftrace, function_graph, tracepoint, kprobe, perf, bpftrace, bcc, kdump, crash, dynamic debug, KGDB