mcpField
D
dwarf-debug-format

dwarf-debug-format

github.com/mohitmishra786开发269484 次安装18 周前更新
代码仓库

关于 dwarf-debug-format

面向处理 ELF 二进制文件中 DWARF 调试信息的 Agent,涵盖段检查、分离式 DWARF、debuginfod,以及 LTO 和剥离操作的影响。

主要功能

  1. 01用 readelf 检查 DWARF 段
  2. 02用 dwarfdump 查看调试数据
  3. 03了解分离式 DWARF 和 .dwo 文件
  4. 04通过 debuginfod 获取远程符号
  5. 05评估 LTO 和剥离的影响

适用场景

  1. 01查找 ELF 二进制文件中的调试段
  2. 02排查 GDB 未找到调试符号
  3. 03配置远程符号获取
  4. 04了解 LTO 后的调试数据

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/mohitmishra786/low-level-dev-skills --skill dwarf-debug-format

安装后在 Claude Code 中输入/dwarf-debug-format确认已加载。

详细介绍

DWARF 调试格式

用途

指导 Agent 理解和使用 DWARF 调试信息:DWARF 的关键段、如何使用 dwarfdump 和 readelf 进行检查、拆分 DWARF(.dwo 文件)、用于远程符号服务器的 debuginfod,以及 LTO 和剥离操作对调试信息的影响。

触发条件

  • “ELF 二进制文件中有哪些 DWARF 段?”
  • “如何检查二进制文件中的 DWARF 调试信息?”
  • “拆分 DWARF 是如何工作的?”
  • “如何设置 debuginfod 以自动获取调试符号?”
  • “为什么 GDB 提示‘未找到调试符号’?”
  • “LTO 会如何影响 DWARF 调试信息?”

工作流

1. DWARF 段概览
# Show all sections in a binary (including DWARF)
readelf -S prog | grep "\.debug"

# Common DWARF sections:
# .debug_info      — types, variables, functions (DIEs — Debug Info Entries)
# .debug_abbrev    — abbreviation table for .debug_info encoding
# .debug_line      — line number table (source line → address mapping)
# .debug_str       — string table for identifiers
# .debug_loc       — location expressions (where variables live)
# .debug_ranges    — non-contiguous address ranges for CUs
# .debug_aranges   — fast lookup: address → compilation unit
# .debug_pubnames  — global variable and function names
# .debug_frame     — call frame information (for unwinding)
# .debug_types     — type unit entries (DWARF 4+)
# .debug_rnglists  — range lists (DWARF 5)
# .debug_loclists  — location lists (DWARF 5)
# .debug_addr      — address table (DWARF 5)
# .debug_line_str  — line number string table (DWARF 5)
2. 使用 dwarfdump 检查 DWARF
# Install dwarfdump
apt-get install dwarfdump    # Ubuntu (part of libdwarf)
brew install libdwarf        # macOS

# Dump all DWARF info
dwarfdump prog

# Dump specific sections
dwarfdump --debug-info prog        # types, variables, functions
dwarfdump --debug-line prog        # line number table
dwarfdump --debug-loc prog         # variable locations
dwarfdump --debug-frame prog       # call frame info

# readelf alternatives (less verbose)
readelf --debug-dump=info prog | head -100
readelf --debug-dump=lines prog | head -50

# llvm-dwarfdump (more readable output)
llvm-dwarfdump prog
llvm-dwarfdump --debug-info prog | grep "DW_AT_name"
llvm-dwarfdump --statistics prog   # summarize DWARF sizes
3. 读取 DWARF DIE 结构
# Sample dwarfdump output for a variable
DW_TAG_compile_unit
  DW_AT_producer  : "GNU C17 13.2.0"
  DW_AT_language  : DW_LANG_C11
  DW_AT_name      : "main.c"
  DW_AT_comp_dir  : "/home/user/project"

  DW_TAG_subprogram
    DW_AT_name    : "add"
    DW_AT_type    : <0x42>  → points to int type DIE
    DW_AT_low_pc  : 0x401130  ← function start address
    DW_AT_high_pc : 0x401150  ← function end address

    DW_TAG_formal_parameter
      DW_AT_name  : "a"
      DW_AT_type  : <0x42>
      DW_AT_location : DW_OP_reg5 (rdi)   ← lives in register rdi

    DW_TAG_formal_parameter
      DW_AT_name  : "b"
      DW_AT_location : DW_OP_reg4 (rsi)   ← lives in register rsi

标签(DW_TAG_*):compile_unit、subprogram、variable、formal_parameter、typedef、structure_type、member、array_type、pointer_type、base_type

属性(DW_AT_*):name、type、location、low_pc、high_pc、byte_size、encoding、file、line

4. 拆分 DWARF(.dwo 文件)

拆分 DWARF 会将调试信息从已链接的二进制文件中分离出来,存放在 .dwo 辅助文件中,从而减小链接器的输入大小:

# Compile with split DWARF
gcc -g -gsplit-dwarf -O2 -c main.c -o main.o
# Creates: main.o (object without full DWARF) + main.dwo (debug info)

# Link (linker only processes small .o files, not large DWARF)
gcc main.o -o prog
# prog references main.dwo but doesn't embed it

# GDB finds .dwo files via DW_AT_GNU_dwo_name attribute in prog
gdb prog    # works if main.dwo is in the same directory

# Inspect the split reference
readelf -S prog | grep "\.gnu_debuglink\|dwo"
dwarfdump prog | grep "dwo_name"    # shows path to .dwo files

# Package .dwo files into a single .dwp for distribution
dwp -o prog.dwp prog        # GNU dwp tool
llvm-dwp -o prog.dwp prog   # LLVM version
# With .dwp next to prog, GDB finds all debug info
5. debuginfod——远程调试符号服务器

debuginfod 通过 HTTP 提供调试符号、源代码和可执行文件:

# Client: configure to use a debuginfod server
export DEBUGINFOD_URLS="https://debuginfod.elfutils.org/"

# GDB uses it automatically when symbols are missing
gdb /usr/bin/git
# GDB will fetch debug symbols from debuginfod if not locally installed

# Explicit fetch
debuginfod-find debuginfo /usr/bin/git     # fetch debug info
debuginfod-find source /usr/bin/git        # fetch source

# Distribution servers
# Fedora:  https://debuginfod.fedoraproject.org/
# Ubuntu:  https://debuginfod.ubuntu.com/
# Debian:  https://debuginfod.debian.net/
# elfutils: https://debuginfod.elfutils.org/

# Configure in GDB
(gdb) set debuginfod enabled on
(gdb) set debuginfod verbose 1

# Set up your own debuginfod server
debuginfod -d /var/cache/debuginfod -p 8002 /path/to/binaries/
export DEBUGINFOD_URLS="http://localhost:8002"
6. LTO 和 DWARF

LTO 会显著影响 DWARF 调试信息:

# Full LTO: DWARF is generated after link-time optimization
# Variables and functions may be merged, inlined, or eliminated
gcc -flto -O2 -g main.c util.c -o prog
# DWARF in prog reflects post-LTO state (may lose some info)

# Thin LTO (better for debug info preservation)
clang -flto=thin -O2 -g main.c util.c -o prog

# For best debug info with LTO: use -Og or separate debug build
gcc -Og -g main.c util.c -o prog_debug     # no LTO, full debug
gcc -O2 -flto main.c util.c -o prog_fast   # LTO, limited debug

# Rust: LTO discard debug info by default in non-release profiles
[profile.dev]
lto = "off"    # preserves debug info
7. 剥离和独立调试文件
# Strip binary (remove DWARF for distribution)
strip --strip-debug prog -o prog.stripped

# Keep separate debug file
objcopy --only-keep-debug prog prog.debug
strip --strip-debug prog
# Link stripped binary to its debug file
objcopy --add-gnu-debuglink=prog.debug prog

# GDB finds debug file automatically
gdb prog    # loads prog.debug via .gnu_debuglink

# Or use eu-strip (elfutils) — creates both in one step
eu-strip -f prog.debug prog

# Verify debug link
readelf -n prog | grep "Debug\|debuglink"

# Check DWARF size contribution
llvm-dwarfdump --statistics prog | grep "file size"
size --format=SysV prog | sort -k2rn | head -15

相关技能

  • 使用 skills/debuggers/debug-optimized-builds 调试使用拆分 DWARF 构建的程序
  • 使用 skills/debuggers/core-dumps 在分析核心转储时使用 debuginfod
  • 使用 skills/binaries/elf-inspection 检查 ELF 段
  • 使用 skills/build-systems/build-acceleration 利用拆分 DWARF 加快链接

评论

支持 Markdown