add2line 이용하기

addr2line 은 디버그 정보를 이용해서 파일명과 행 번호를 얻는다.  이때문에 프로그램은 미리 디버그 정보를 포함하도록 컴파일해야 한다.
gcc에서는  -g 옵션을 사용한다.

  1 #include <stdio.h>
  2 
  3 void func(void)
  4 {
  5     printf("func call\n");
  6 }
  7 
  8 int main(void)
  9 {
 10     printf(" func addr %p\n", func);
 11 }
 12 
 13 

컴파일은 아래와 같이 한다.
root@boggle70-desktop:tmp# gcc -g addr2line.c 

실행을 한다.
root@boggle70-desktop:tmp# ./a.out 
 func addr 0x8048414

먼저 헬프를 보자
root@boggle70-desktop:tmp# addr2line -help
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

addr2line: supported targets: elf32-i386 a.out-i386-linux pei-i386 elf32-little elf32-big srec symbolsrec verilog tekhex binary ihex trad-core

가장 간단한 예제를 살펴보자
root@boggle70-desktop:tmp# addr2line -e a.out 0x8048414
/root/tmp/addr2line.c:4
input file 과 주소를 주니 실행라인이 정확히 출력된다.
함수의 이름도 출력할수 있다.
root@boggle70-desktop:tmp# addr2line -f -e a.out 0x8048414
func
/root/tmp/addr2line.c:4

addr2line 역시 BFD 라이브러리를 이용해서 디버그 정보를 구한다.
addr2line 은 주소로부터 파일명과 행 번호를 얻을 수 있다.



반응형

+ Recent posts