欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

linux基础36——调试coredump的两种方法

程序员文章站 2022-05-10 17:19:26
...

一段程序示例:

#include <stdio.h>

int main()
{
	int *p = NULL;

	*p = 0;

	printf("core dump\n");

	return 0;
}

编译生成可执行程序,执行可执行程序段错误。

[email protected] coredump]# cat test1.cpp 
#include <stdio.h>

int main()
{
	int *p = NULL;

	*p = 0;

	printf("core dump\n");

	return 0;
}
[[email protected] coredump]# g++ -g test1.cpp 
[[email protected] coredump]# ls
a.out  test1.cpp
[[email protected] coredump]# ./a.out 
段错误

1. dmesg+addr2line调试

dmesg ,一种程序,用于检测和控制内核缓冲。程序用来帮助用户,了解系统的启动信息,可以获得出错堆栈地址。

addr2line ,可以将指令的地址和可执行映像转换成文件名,函数名或源代码的工具。这种功能将跟踪地址转换成更有意义的内容来说很有用。

在调用 addr2line 工具时,要使用 -e 选项来指定可执行映像,使用 -f 选项可以告诉工具输出函数名。

[[email protected] coredump]# dmesg | grep a.out 
[    4.593497] iTCO_wdt: initialized. heartbeat=30 sec (nowayout=0)
[  273.301104] a.out[1841]: segfault at 0 ip 0000000000400536 sp 00007ffc056508d0 error 6 in a.out[400000+1000]
[  455.796400] a.out[1860]: segfault at 0 ip 0000000000400536 sp 00007fff62fdf580 error 6 in a.out[400000+1000]
[[email protected] coredump]# addr2line -e a.out 0000000000400536
/home/coredump/test1.cpp:7

先通过dmesg找到对应出错的地址,再用 addr2line -e 将地址解析到对应的代码行。

2. strace+addr2line调试

strace 是一个集诊断、调试、统计与一体的工具,我们可以使用strace,对应用的系统调用和信号传递的跟踪结果,来对应用进行分析,以达到解决问题,或者是了解应用工作过程的目的。

strace 的简单的用法就是,执行一个指定的命令,在指定的命令结束之后,它也就退出了。
在命令执行的过程中,strace 会记录和解析命令进程的所有系统调用,以及这个进程所接收到的,所有的信号值。

  • -c ,统计每一系统调用的所执行的时间,次数和出错的次数等
  • -p ,指定进程pid
  • -i  ,输出系统调用的入口指针
[[email protected] coredump]# strace -i ./a.out 
[00007f031a295cc7] execve("./a.out", ["./a.out"], 0x7ffe7ac63618 /* 25 vars */) = 0
[00007f1e86f67aac] brk(NULL)            = 0x1455000
[00007f1e86f687da] mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f1e8716f000
[00007f1e86f686d7] access("/etc/ld.so.preload", R_OK) = -1 ENOENT (没有那个文件或目录)
[00007f1e86f68677] open("/etc/ld.so.cache", O_RDONLY|O_CLOEXEC) = 3
[00007f1e86f68604] fstat(3, {st_mode=S_IFREG|0644, st_size=36474, ...}) = 0
[00007f1e86f687da] mmap(NULL, 36474, PROT_READ, MAP_PRIVATE, 3, 0) = 0x7f1e87166000
[00007f1e86f68787] close(3)             = 0
[00007f1e86f68677] open("/lib/libstdc++.so.6", O_RDONLY|O_CLOEXEC) = 3
[00007f1e86f68697] read(3, "\177ELF\2\1\1\3\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\[email protected]\217\t\0\0\0\0\0"..., 832) = 832
[00007f1e86f68604] fstat(3, {st_mode=S_IFREG|0755, st_size=16646312, ...}) = 0
[00007f1e86f687da] mmap(NULL, 3983616, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7f1e86b82000
[00007f1e86f68877] mprotect(0x7f1e86d3f000, 2093056, PROT_NONE) = 0
[00007f1e86f687da] mmap(0x7f1e86f3e000, 57344, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x1bc000) = 0x7f1e86f3e000
[00007f1e86f687da] mmap(0x7f1e86f4c000, 10496, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x7f1e86f4c000
[00007f1e86f68787] close(3)             = 0
[00007f1e86f68677] open("/lib64/libm.so.6", O_RDONLY|O_CLOEXEC) = 3
[00007f1e86f68697] read(3, "\177ELF\2\1\1\3\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0PS\0\0\0\0\0\0"..., 832) = 832
[00007f1e86f68604] fstat(3, {st_mode=S_IFREG|0755, st_size=1136944, ...}) = 0
[00007f1e86f687da] mmap(NULL, 3150136, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7f1e86880000
[00007f1e86f68877] mprotect(0x7f1e86981000, 2093056, PROT_NONE) = 0
[00007f1e86f687da] mmap(0x7f1e86b80000, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x100000) = 0x7f1e86b80000
[00007f1e86f68787] close(3)             = 0
[00007f1e86f68677] open("/lib64/libgcc_s.so.1", O_RDONLY|O_CLOEXEC) = 3
[00007f1e86f68697] read(3, "\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0\320*\0\0\0\0\0\0"..., 832) = 832
[00007f1e86f68604] fstat(3, {st_mode=S_IFREG|0755, st_size=88720, ...}) = 0
[00007f1e86f687da] mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f1e87165000
[00007f1e86f687da] mmap(NULL, 2184192, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7f1e8666a000
[00007f1e86f68877] mprotect(0x7f1e8667f000, 2093056, PROT_NONE) = 0
[00007f1e86f687da] mmap(0x7f1e8687e000, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x14000) = 0x7f1e8687e000
[00007f1e86f68787] close(3)             = 0
[00007f1e86f68677] open("/lib64/libc.so.6", O_RDONLY|O_CLOEXEC) = 3
[00007f1e86f68697] read(3, "\177ELF\2\1\1\3\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0`&\2\0\0\0\0\0"..., 832) = 832
[00007f1e86f68604] fstat(3, {st_mode=S_IFREG|0755, st_size=2156344, ...}) = 0
[00007f1e86f687da] mmap(NULL, 3985920, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7f1e8629c000
[00007f1e86f68877] mprotect(0x7f1e86460000, 2093056, PROT_NONE) = 0
[00007f1e86f687da] mmap(0x7f1e8665f000, 24576, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x1c3000) = 0x7f1e8665f000
[00007f1e86f687da] mmap(0x7f1e86665000, 16896, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x7f1e86665000
[00007f1e86f68787] close(3)             = 0
[00007f1e86f687da] mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f1e87164000
[00007f1e86f687da] mmap(NULL, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f1e87162000
[00007f1e86f4fefd] arch_prctl(ARCH_SET_FS, 0x7f1e87162740) = 0
[00007f1e86f68877] mprotect(0x7f1e8665f000, 16384, PROT_READ) = 0
[00007f1e86f68877] mprotect(0x7f1e8687e000, 4096, PROT_READ) = 0
[00007f1e86f68877] mprotect(0x7f1e86b80000, 4096, PROT_READ) = 0
[00007f1e86f687da] mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f1e87161000
[00007f1e86f68877] mprotect(0x7f1e86f3e000, 45056, PROT_READ) = 0
[00007f1e86f68877] mprotect(0x600000, 4096, PROT_READ) = 0
[00007f1e86f68877] mprotect(0x7f1e87170000, 4096, PROT_READ) = 0
[00007f1e86f68857] munmap(0x7f1e87166000, 36474) = 0
[00007f1e8639127c] brk(NULL)            = 0x1455000
[00007f1e8639127c] brk(0x1487000)       = 0x1487000
[0000000000400536] --- SIGSEGV {si_signo=SIGSEGV, si_code=SEGV_MAPERR, si_addr=NULL} ---
[????????????????] +++ killed by SIGSEGV +++
段错误
[[email protected] coredump]# addr2line -e a.out 0000000000400536
/home/coredump/test1.cpp:7
[[email protected] coredump]#