-
编译时加入调试信息 ((PC 机上))
编译参数为
-g
loongarch64-linux-gnu-gcc -g 1.c
-
开启core 文件 (开发板上)
ulimit -c unlimited
查看ulimit 的所有参数设置
# ulimit -a core file size (blocks, -c) unlimited data seg size (kbytes, -d) unlimited scheduling priority (-e) 0 file size (blocks, -f) unlimited pending signals (-i) 3145 max locked memory (kbytes, -l) 65536 max memory size (kbytes, -m) unlimited open files (-n) 1024 pipe size (512 bytes, -p) 8 POSIX message queues (bytes, -q) 819200 real-time priority (-r) 0 stack size (kbytes, -s) unlimited cpu time (seconds, -t) unlimited max user processes (-u) 100000 virtual memory (kbytes, -v) unlimited file locks (-x) unlimited
-
查看core 文件的名字 (开发板上)
# cat /proc/sys/kernel/core_pattern
core
或者
# sysctl kernel.core_pattern
kernel.core_pattern = core
- 示例 (开发板上)
# ./a.out
[ 374.566878] do_page_fault(): sending SIGSEGV to a.out for invalid write access to 0000000000000000
[ 374.576031] era = 0000000120000748 in a.out[120000000+4000]
[ 374.581699] ra = 0000000120000788 in a.out[120000000+4000]
Segmentation fault (core dumped)
执行程序之后在当前路径生成了名为core
的 coredump 文件。
# ls -sh
total 508K
16K a.out 488K core
-
调试(PC 机上)
将板卡上运行生产的
core
文件复制到PC 机上,然后使用交叉工具链中的gdb 进行调试。调试步骤:
- 启动 loongarch64-linux-gnu-gdb
- 加载 二进制文件
- 加载 core 文件
- 查看堆栈
$ loongarch64-linux-gnu-gdb GNU gdb (LoongArch GNU toolchain rc1.2 (20230615)) 8.1.50.20190122-git Copyright (C) 2018 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 "--host=x86_64-linux-gnu --target=loongarch64-linux-gnu". Type "show configuration" for configuration details. For bug reporting instructions, please see: <http://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". (gdb) file a.out ## 加载二进制文件 Reading symbols from a.out...done. (gdb) core-file ./core ## 加载core 文件 warning: exec file is newer than core file. [New LWP 473] warning: Section `.reg2/473' in core file too small. warning: Could not load shared library symbols for /lib/loongarch64-linux-gnu/libc.so.6. Do you need "set solib-search-path" or "set sysroot"? Core was generated by `./a.out'. Program terminated with signal SIGSEGV, Segmentation fault. warning: Section `.reg2/473' in core file too small. #0 0x0000000120000748 in change_val (p=0x0) at 1.c:7 ## 从这里可知是给空指针赋值后导致的 7 *p = 10; (gdb) bt ## 查看堆栈 #0 0x0000000120000748 in change_val (p=0x0) at 1.c:7 #1 0x0000000120000788 in main (argc=1, argv=0x7ffbcbc0e8) at 1.c:13 (gdb)
示例源代码如下
#include <stdio.h> #include <stdlib.h> #include <string.h> void change_val(char *p) { *p = 10; } int main(int argc, char *argv[]) { char *p = NULL; change_val(p); printf("p:%d\n", *p); return 0; }