Edit online

CoreDump 调试

CoreDump(核心转储)是一款调试复杂软件问题的核心工具。CoreDump 通过操作系统在进程异常终止时生成的内存快照文件,记录崩溃瞬间的寄存器状态、堆栈信息、内存布局等关键数据。

CoreDump 详细使用说明如下:
  1. 启用内核 CoreDump 功能:
    1. 在 SDK 根目录下执行下列命令打开 kernel 的配置界面:
      make kenel-menuconfig
    2. 勾选下列选项,启用内核 CoreDump 功能:
      Executable file formats  --->
          [*] Kernel support for ELF binaries
          [*] Write ELF core dumps with partial segments
          [*] Enable core dump support
  2. 配置 SDK 工程化适配功能:
    1. 在 SDK 根层目录执行下列命令,进入 menuconfig 配置界面:
      make menuconfig
    2. 在 menuconfig 配置界面,勾选下列选项使能应用 test_coredump,并保留应用调试信息:
      Build options  --->
           [*] build packages with debugging symbols
                   gcc debug level (debug level 2)  --->
           [*] strip target binaries
                   (test_coredump)    executables that should not be stripped
    3. 同时勾选下列选项,使能调试工具 GDB:
      Third-party packages  --->
       [*] binutils  --->
           [*]   binutils binaries
           [*] gdb  --->
                   [*]   gdbserver
                   [*]   full debugger
                   [*]   TUI support
  3. 在 SDK 根目录下执行下列命令,对指定源码包重新编译:
    make <pkg>-reconfigure
    例如,重新编译 test-coredump:
    make test-coredump-reconfigure
  4. 重新编译完成后,在 SDK 根目录下执行下列命令重新打包并烧录源码包:
    make
  5. 重新烧录启动后在控制台执行下列命令:
    nm usr/bin/test_coredump | grep 'T'

    如打印下列类似符号表,表示配置成功:

    0000000000012020 d _GLOBAL_OFFSET_TABLE_
    0000000000010390 t _PROCEDURE_LINKAGE_TABLE_
    0000000000012000 D __DATA_BEGIN__
    0000000000012028 D __SDATA_BEGIN__
    0000000000012000 D __TMC_END__
    0000000000010560 T __libc_csu_fini
    0000000000010500 T __libc_csu_init
    00000000000103e0 T _start
    0000000000010484 T add
    00000000000103d0 T main
    00000000000104a6 T sub
    00000000000104c8 T test_add_and_sub
  6. 执行下列命令解除系统对 CoreDump 文件大小的限制:
    ulimit -c unlimited
  7. 运行测试程序 test_coredump,当发生段错误时会自动在当前目录生成完整的 CoreDump 文件:
    Segmentation fault (core dumped)
  8. 执行下列命令,对指定应用程序进行调试:
    gdb /usr/bin/test_coredump core
    GDB 初始化信息显示:
    GNU gdb (GDB) 14.2
     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 "riscv64-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 usr/bin/test_coredump...
    
     warning: could not convert 'main' from the host encoding (ANSI_X3.4-1968) to UTF-32.
     This normally should not happen, please file a bug report.
    
     warning: exec file is newer than core file.
     [New LWP 181]
     Core was generated by `test_coredump'.
     Program terminated with signal SIGSEGV, Segmentation fault.
     #0  0x0000000000010488 in add ()
  9. 在断点处执行下列命令查看函数调用栈:
    bt
    0x0000000000010488 in add ()
    0x00000000000104d6 in test_add_and_sub ()
    0x00000000000103d8 in main ()

关于更多 GDB 的使用方法,参考 GDB 官方文档。