프로그래밍 언어/C/C++

GCC옵션: finstrument-functions

소혼 2011. 5. 24. 21:43
반응형
추가 조사 : -finstrument-functions-exclude-file-list
 

instrument-functions 옵션은 함수의 시작과 끝에 다른 함수를 삽입해주는 옵션입니다.
용도는 다양할 것 같지만, 디버깅이 어려운 환경 - 예를 들어 임베디드, 멀티 프로세스 환경, 서버-클라이언트 구조 등에서 큰 도움이 될 것 같습니다.
그 외 많은 프로파일링 툴이 이것을 사용하여 나름의 기능을 구현하는 것 같습니다.

사용 방법은 무척 간단합니다.



__cyg_profile_func_enter 와 __cyg_profile_func_exit는 instrument 옵션이 켜졌을 때 gcc가 연결해주는 함수입니다.
이 함수들은 다른 함수들과 다르게  no_instrument_function이라는 attribute를 가지고 있습니다.
no_instrument_function는 이 함수들에는  __cyg_profile_func_enter와 __cyg_profile_func_exit를 삽입하지 말라는 뜻입니다.

간단한 예제를 짜 보았습니다.

 
이 파일을 gcc로 컴파일하고 실행하면 아래와 같습니다.
# ./a.out 
## this is main()
## this is main() -- 32 

instrument-functions 옵션을 주고 컴파일해보겠습니다.
# gcc -c main.c -finstrument-functions -g
# gcc -c test.c -finstrument-functions -g
# gcc -o b.out main.o test.o
# ./b.out 
> Enter 0x8048561
 ## this is main()
> Enter 0x804850e
> Enter 0x80484ca
> Enter 0x8048454
> Leave 0x8048454
> Leave 0x80484ca
> Enter 0x80484ca
> Enter 0x804848f
> Leave 0x804848f
> Leave 0x80484ca
> Enter 0x80484ca
> Enter 0x8048454
> Leave 0x8048454
> Leave 0x80484ca
> Enter 0x80484ca
> Enter 0x804848f
> Leave 0x804848f
> Leave 0x80484ca
> Enter 0x80484ca
> Enter 0x8048454
> Leave 0x8048454
> Leave 0x80484ca
> Enter 0x80484ca
> Enter 0x804848f
> Leave 0x804848f
> Leave 0x80484ca
> Enter 0x80484ca
> Enter 0x8048454
> Leave 0x8048454
> Leave 0x80484ca
> Enter 0x80484ca
> Enter 0x804848f
> Leave 0x804848f
> Leave 0x80484ca
> Enter 0x80484ca
> Enter 0x8048454
> Leave 0x8048454
> Leave 0x80484ca
> Enter 0x80484ca
> Enter 0x804848f
> Leave 0x804848f
> Leave 0x80484ca
> Leave 0x804850e
> Enter 0x8048454
> Leave 0x8048454
 ## this is main() -- 32
> Leave 0x8048561
 
함수의 시작과 끝에 그 함수의 위치정보가 출력됩니다.
이 정보를 이해할 수 있는 값으로 바꾸어 주는 툴이 있습니다. addr2line입니다.

# addr2line 0x804850e -e b.out -f
a
/workspace/study/c/ins/main.c:23 

이 툴을 이용해서 위 코드를 python으로 변환해보았습니다.

# python converter.py test log
[0x8048561]  > main at main.c:31
 ## this is main()


[0x804850e]    > a at main.c:23
[0x80484ca]      > b at main.c:15
[0x8048454]        > c at main.c:5
[0x80484ca]      > b at main.c:15
[0x804848f]        > d at main.c:10
[0x80484ca]      > b at main.c:15
[0x8048454]        > c at main.c:5
[0x80484ca]      > b at main.c:15
[0x804848f]        > d at main.c:10
[0x80484ca]      > b at main.c:15
[0x8048454]        > c at main.c:5
[0x80484ca]      > b at main.c:15
[0x804848f]        > d at main.c:10
[0x80484ca]      > b at main.c:15
[0x8048454]        > c at main.c:5
[0x80484ca]      > b at main.c:15
[0x804848f]        > d at main.c:10
[0x80484ca]      > b at main.c:15
[0x8048454]        > c at main.c:5
[0x80484ca]      > b at main.c:15
[0x804848f]        > d at main.c:10
[0x8048454]    > c at main.c:5
 ## this is main() -- 32

실제 프로젝트로 해봐야 피부에 와닿을 것 같습니다.
 
반응형

'프로그래밍 언어 > C/C++' 카테고리의 다른 글

POD vs non POD  (0) 2011.08.07
memwatch  (0) 2011.05.24
linker 관련 링크  (0) 2011.04.15
calloc vs malloc  (7) 2011.04.06
[C언어] a - b < 0 vs. a < b  (0) 2010.10.25