您的位置:首页 > 编程语言 > PHP开发

《专业嵌入式软件开发》笔记-第5章addr2line

2012-02-18 20:38 218 查看
binutils工具集

5.1 
addr2line指令地址翻译器,编译时加上-g选项,addr2line才有作用

main.c
#include <stdio.h>

void foo(void)
{
printf("The address of foo() is %p.\n", foo);
}

int main(void)
{
foo();
return 0;
}


$gcc  -g  main.c -o test

$./test

the address of foo() is 0x401090.

$addr2line 0x401090 -f -e test

foo/home/ALTA/study/binutils/main.c:4

输出了程序地址所对应的函数名、文件名和行号

Usage: addr2line [option(s)] [addr(s)] 

Convert addresses into line number/file name pairs.

 If no addresses are specified on the command line, they will be read from stdin 

The options are: 

@<file>                Read options from <file> 

-a --addresses         Show addresses 

-b --target=<bfdname>  Set the binary file format

  -e --exe=<executable>  Set the input file name (default is a.out) 

-i --inlines           Unwind inlined functions 

-j --section=<name>    Read section-relative offsets instead of addresses 

-p --pretty-print      Make the output easier to read for humans 

-s --basenames         Strip directory names 

-f --functions         Show function names 

-C --demangle[=style]  Demangle function names 

-h --help              Display this information  -v --version           Display the program's version

例子中的地址是通过打印知晓的。实际中在程序崩溃时通过某种方式获得。另外,nm工具可以得到如下信息

----显示有删减---

00401090 t .text

00401090 T _foo004010ac T _main

004010e4 t .text004010e4 T ___chkstk

 

实际发现addr2line addr -f -e test,当addr∈[0x401090,0x4010ac)时,显示的结果都是foo,这说明foo函数的大小就是0x4010ac-0x401090

 

C++程序的不同,主要是c++对函数重载,所以addr2line 获得的函数名可能经过mangling(win中称decorating)

main.cpp
#include <iostream>
using namespace std;

int foo(void)
{
cout<<"the addr of foo() is "<<hex<<int(foo)<<endl;
}

int main(void)
{
foo();
return 0;
}
$g++ -g main.cpp -o main
$ ./main.exe
the addr of foo() is 401190
$ addr2line 0x401190 -f -e main.exe
_Z3foov
/home/ALTA/study/binutils/main.cpp:5

--demangle=gnu-v3选项可以获得我们所书写的函数名,但是cygwin下无效

$ addr2line 0x401190--demangle=gnu-v3-f -e main.exe

Z3foov

/home/ALTA/study/binutils/main.cpp:5

    





                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐