binutils——如何找到内存中的内存错误

问题:我工程项目中,经常会出现内存操作错误这样的BUG。而这种BUG往往是很难发现的,因为编译器发现不了这样的错误,并且发生错误的地方和导致错误的地方往往不在同一个地方。这样的BUG很难用人力去发现。
那么我们可以用什么工具帮助我们找到BUG的地方呢?

addr2line

  • 将指定地址转换为对应的文件名和行号
  • 常用语分析和定位内存访问错误的问题
*示例代码:利用addr2line查找内存错误*
//func.c
#include <stdio.h>

int* g_pointer;

void func()
{
*g_pointer =(int)"D.T.Software";

return;
}
//main.c
#include <stdio.h>

int g_global =0;
int g_test =1;

externint* g_pointer;
externvoid func();

int main(int argc,char*argv[])
{
    printf("&g_global = %p\n",&g_global);
    printf("&g_test = %p\n",&g_test);
    printf("&g_pointer = %p\n",&g_pointer);
    printf("g_pointer = %p\n", g_pointer);
    printf("&func = %p\n",&func);
    printf("&main = %p\n",&main);

    func();

return0;
}

输出结果:
delphi@delphi-vm:~/workspace$ gcc -g func.c main.c -o test.out
delphi@delphi-vm:~/workspace$ ls
func.c main.c test.out
delphi@delphi-vm:~/workspace\$ ./test.out
&g_global = 0x804a020
&g_test = 0x804a014
&g_pointer = 0x804a024
g_pointer = (nil)
&func = 0x80483c4
&main = 0x80483d8
段错误

明显的,从打印信息可以判断出:发生段错误的是func()中的某一行。
而我们通过阅读代码可以知道:BUG的根源是:int* g_pointer。
这种问题在小程序可以轻易找到,但是在大型项目中,这种问题却很难发现,并且一般在项目中,没有打印信息帮助我们,这时候我们就需要借助工具来帮助我们找到BUG了。

Addr2line示例:定位0地址访问

1.开启core dump选项

ulimit –c unlimited
我们在用这个命令的时候主要是为了产生core文件,就是程序运行发行段错误时的文件。unlimited代表不限制core文件的大小。

2.运行程序,并生成崩溃的core文件

执行导致程序崩溃的测试用例(可执行文件)。

3.读取core文件,获取IP寄存器的值

dmseg core
core文件无法直接打开,只能通过dmseg命令将其打开。

4.使用addr2line定位代码行

addr2line (core得到导致段错误地址) –f –e test.out
-f代表打印文件名和行号,-e用于指定可执行文件名称。
更多命令选项,可以使用man addr2line命令进行查看。

调试过程:

delphi@delphi-vm:~/workspace$ ulimit -c unlimited
delphi@delphi-vm:~/workspace$ ./test.out 
&g_global = 0x804a020
&g_test = 0x804a014
&g_pointer = 0x804a024
g_pointer = (nil)
&func = 0x80483c4
&main = 0x80483d8
段错误 (核心已转储)
delphi@delphi-vm:~/workspace$ ls
core  func.c  main.c  test.out
delphi@delphi-vm:~/workspace$ dmseg core
….(此处省略N行)
[201464.145237] eth0: no IPv6 routers present
[201699.968041] test.out[1780]: segfault at 0 ip 08048469 sp bf983808 error 6 in test.out[8048000+1000]
[202039.963372] test.out[1816]: segfault at 0 ip 08048469 sp bfcbbe98 error 6 in test.out[8048000+1000]
[207305.105360] test.out[2312]: segfault at 0 ip 08048469 sp bfd3fdb8 error 6 in test.out[8048000+1000]
[207358.788489] test.out[2376]: segfault at 0 ip 080483d1 sp bf803e18 error 6 in test.out[8048000+1000]
[207391.433831] test.out[2379]: segfault at 0 ip 080483d1 sp bfd6fc08 error 6 in test.out[8048000+1000]
[208563.525809] test.out[2558]: segfault at 0 ip 080483d1 sp bfa05a38 error 6 in test.out[8048000+1000]
delphi@delphi-vm:~/workspace$ addr2line 08048469 -f -e test.out
main
/home/delphi/workspace/main.c:20
delphi@delphi-vm:~/workspace$ addr2line 080483d1 -f -e test.out
func
/home/delphi/workspace/func.c:7

注意:
要获得core文件。编译时一定要带上-g选项:gcc -g func.c main.c -o test.out。这是为了让编译器在可执行文件中放置调试信息,否则add2line命令将不能发挥作用。
具体调试信息中包含了哪些内容。可以用objdump命令查看。

问题:我们调试程序需要调试信息,但是我们最终发布出去的版本却不需要这些调试信息,那么我们如何剔除可执行文件中的调试信息呢?

strip

  • 剔除程序文件中的调试信息,减少目标程序的大小
  • 一般在程序发布前都需要将调试信息剔除
  • 过多的调试信息可能影响程序的执行效率

strip test.out

几乎所有的调试工具都依赖于目标文件中的调试信息,这些调试信息能都帮助我们快速定位问题。而这些调试信息给可执行文件代码冗余,一般我们在程序发布前会将这些调试信息剔除掉。

操作过程:

root@Shaw-vm:/home/delphi/workspace# ls -l test.out 
-rwxr-xr-x 1 root root 8433 2017-05-25 08:48 test.out
root@Shaw-vm:/home/delphi/workspace# strip test.out 
root@Shaw-vm:/home/delphi/workspace# ls -l test.out 
-rwxr-xr-x 1 root root 5508 2017-05-25 09:03 test.out
root@Shaw-vm:/home/delphi/workspace# addr2line 0x08080483d1 -f -e test.out 
??
??:0

猜你喜欢

转载自blog.csdn.net/small_prince_/article/details/80670320