addr2line如何根据地址找到行号?

针对于linux平台 下,c程序使用场景举例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
root@ubuntu:~# cat ./mem_test.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main(int argc, char *argv[])
{
char *p1;
char *p2;
char *p3;
char *p4;

sleep(20);//让程序sleep 20s是因为我们程序先起来之后,等待SystemTap启动设置探测点
p1 = malloc(500);
p2 = malloc(200);
p3 = malloc(300);
p4 = malloc(300);//泄漏
free(p1);
free(p2);
free(p3);
free(p2);//重复释放
printf("p1: %p, p2: %p, p3: %p, p4: %p\n", p1, p2, p3, p4);
return 0;
}

编译运行

1
2
gcc -g mem_test.c -o main
./main&

运行结果:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
root@ubuntu:~# *** Error in `./main': double free or corruption (!prev): 0x0000000000b71210 ***

======= Backtrace: =========

/lib/x86_64-linux-gnu/libc.so.6(+0x777e5)[0x7f1cebfee7e5]

/lib/x86_64-linux-gnu/libc.so.6(+0x8037a)[0x7f1cebff737a]

/lib/x86_64-linux-gnu/libc.so.6(cfree+0x4c)[0x7f1cebffb53c]

./main[0x400677]

/lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xf0)[0x7f1cebf97830]

./main[0x400529]

======= Memory map: ========

00400000-00401000 r-xp 00000000 fc:00 526710 /root/main

00600000-00601000 r--p 00000000 fc:00 526710 /root/main

00601000-00602000 rw-p 00001000 fc:00 526710 /root/main

00b71000-00b92000 rw-p 00000000 00:00 0 [heap]

7f1ce4000000-7f1ce4021000 rw-p 00000000 00:00 0

7f1ce4021000-7f1ce8000000 ---p 00000000 00:00 0

7f1cebd61000-7f1cebd77000 r-xp 00000000 fc:00 786953 /lib/x86_64-linux-gnu/libgcc_s.so.1
7f1cebd77000-7f1cebf76000 ---p 00016000 fc:00 786953 /lib/x86_64-linux-gnu/libgcc_s.so.1
7f1cebf76000-7f1cebf77000 rw-p 00015000 fc:00 786953 /lib/x86_64-linux-gnu/libgcc_s.so.1
7f1cebf77000-7f1cec137000 r-xp 00000000 fc:00 791185 /lib/x86_64-linux-gnu/libc-2.23.so
7f1cec137000-7f1cec337000 ---p 001c0000 fc:00 791185 /lib/x86_64-linux-gnu/libc-2.23.so
7f1cec337000-7f1cec33b000 r--p 001c0000 fc:00 791185 /lib/x86_64-linux-gnu/libc-2.23.so
7f1cec33b000-7f1cec33d000 rw-p 001c4000 fc:00 791185 /lib/x86_64-linux-gnu/libc-2.23.so
7f1cec33d000-7f1cec341000 rw-p 00000000 00:00 0
7f1cec341000-7f1cec367000 r-xp 00000000 fc:00 791163 /lib/x86_64-linux-gnu/ld-2.23.so
7f1cec557000-7f1cec55a000 rw-p 00000000 00:00 0
7f1cec563000-7f1cec566000 rw-p 00000000 00:00 0
7f1cec566000-7f1cec567000 r--p 00025000 fc:00 791163 /lib/x86_64-linux-gnu/ld-2.23.so
7f1cec567000-7f1cec568000 rw-p 00026000 fc:00 791163 /lib/x86_64-linux-gnu/ld-2.23.so
7f1cec568000-7f1cec569000 rw-p 00000000 00:00 0
7fff0e000000-7fff0e021000 rw-p 00000000 00:00 0 [stack]
7fff0e09b000-7fff0e09d000 r--p 00000000 00:00 0 [vvar]
7fff0e09d000-7fff0e09f000 r-xp 00000000 00:00 0 [vdso]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0 [vsyscall]

[1]+ Aborted (core dumped) ./main

0x400677有问题,那么它对应那一行?

如何查找0x400677对应哪一行

1
2
3
root@ubuntu:~# addr2line -e main 0x400677

/root/mem_test.c:21

分析

如上我们发现21行是重复释放导致