Backend/C

valgrind

petitCoding 2011. 5. 26. 11:47



Valgrind : Linux-x86 용 실행 파일의 디버깅과 프로파일링을 위한 오픈 소스 툴. Valgrind는 Memcheck이나 Addrcheck 툴을 사용하여 실행중인 프로그램에서 메모리 누출(leak)/오염(corruption)을 찾아낼 수 있다. 그 외의 Cachegrind, Helgrind 툴을 사용하여 캐쉬 프로파일링을 하거나, 멀티 쓰레드에서의 데이타 경쟁을 발견을 할 수 있다. 64Bit Linux는 지원하지 않는다.


Valgrind 에는 다음과 같이 다양한 툴들이 있다.

 

1. Memcheck

- 초기화되지 않은 메모리의 사용

- free된 메모리에 읽기/쓰기를 시도하는 경우

- malloc된 메모리 블럭 외에 읽기/쓰기를 시도하는 경우

- stack의 부적절한 지역에 읽기/쓰기가 시도되는 경우

- 메모리 누출 - malloc되고 free되지 않은 메모리

- 초기화되지 않거나 주소를 알 수 없는 메모리가 시스템 호출로 넘겨지는 경우

- memcpy()와 관련된 함수에서 src와 dst 포인터가 겹치는 경우

- 몇 가지의 POSIX pthreads API의 잘못된 사용

 

2. Addrcheck : Memcheck의 가벼운 버전


3. Cachegrind : 프로그램의 캐시 프로파일링을 한다.

 

4. Helgrind : 멀티스레드 프로그램에서 데이터의 경쟁 상태를 발견한다.


간단한 사용법은 다음과 같다.

 

<프로그램 소스> 

 

      1 #include <stdio.h>
      2 #include <stdlib.h>
      3
      4 typedef struct p{
      5     int num;
      6     char *ch;
      7     struct p *next;
      8 }p;
      9
     10 int main()
     11 {
     12     struct p    *pp;
     13     struct p    *qq;
     14
     15     pp = (p*)malloc(sizeof(p));
     16     printf("%s\n", pp->ch);
     17
     18
     19     pp->next = qq;
     20
     21     printf("%d\n", pp->next->num);

                  //컴파일시 탐지되지 않으나, 실행시 세그멘테이션 오류가 남
     22 }

 

<디버깅>

1. memcheck 결과

 

[whitelka]# valgrind --tool=memcheck ./main //valgrind --tool=<사용할 툴> 실행파일
==3816== Memcheck, a memory error detector for x86-linux.
==3816== Copyright (C) 2002-2004, and GNU GPL'd, by Julian Seward et al.
==3816== Using valgrind-2.2.0, a program supervision framework for x86-linux.
==3816== Copyright (C) 2000-2004, and GNU GPL'd, by Julian Seward et al.
==3816== For more details, rerun with: -v
==3816==
==3816== Conditional jump or move depends on uninitialised value(s)
==3816==    at 0x9F837F: _IO_vfprintf_internal (in /lib/tls/libc-2.3.3.so)
==3816==    by 0x9FF68F: _IO_printf (in /lib/tls/libc-2.3.3.so)
==3816==    by 0x804851F: main (main.c:16)
(null)
==3816==
==3816== Use of uninitialised value of size 4
==3816==    at 0x8048535: main (main.c:21)
==3816==
==3816== Invalid read of size 4
==3816==    at 0x8048535: main (main.c:21)
==3816==  Address 0x0 is not stack'd, malloc'd or (recently) free'd
==3816==
==3816== Process terminating with default action of signal 11 (SIGSEGV): dumping core
==3816==  Access not within mapped region at address 0x0
==3816==    at 0x8048535: main (main.c:21)
==3816==
==3816== ERROR SUMMARY: 3 errors from 3 contexts (suppressed: 12 from 1)
==3816== malloc/free: in use at exit: 1218 bytes in 2 blocks.
==3816== malloc/free: 2 allocs, 0 frees, 1218 bytes allocated.
==3816== For a detailed leak analysis,  rerun with: --leak-check=yes
==3816== For counts of detected errors, rerun with: -v
세그멘테이션 오류

 

2. addrcheck 결과

 

[whitelka]# valgrind --tool=addrcheck ./main
==4126== Addrcheck, a fine-grained address checker for x86-linux.
==4126== Copyright (C) 2002-2004, and GNU GPL'd, by Julian Seward et al.
==4126== Using valgrind-2.2.0, a program supervision framework for x86-linux.
==4126== Copyright (C) 2000-2004, and GNU GPL'd, by Julian Seward et al.
==4126== For more details, rerun with: -v
==4126==
(null)
==4126== Invalid read of size 4
==4126==    at 0x8048535: main (main.c:21)
==4126==  Address 0x0 is not stack'd, malloc'd or (recently) free'd
==4126==
==4126== Process terminating with default action of signal 11 (SIGSEGV): dumping core
==4126==  Access not within mapped region at address 0x0
==4126==    at 0x8048535: main (main.c:21)
==4126==
==4126== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)
==4126== malloc/free: in use at exit: 1218 bytes in 2 blocks.
==4126== malloc/free: 2 allocs, 0 frees, 1218 bytes allocated.
==4126== For a detailed leak analysis,  rerun with: --leak-check=yes
==4126== For counts of detected errors, rerun with: -v

 

3. cachegrind  결과

 

[whitelka]# valgrind --tool=cachegrind ./main
==4207== Cachegrind, an I1/D1/L2 cache profiler for x86-linux.
==4207== Copyright (C) 2002-2004, and GNU GPL'd, by Nicholas Nethercote et al.
==4207== Using valgrind-2.2.0, a program supervision framework for x86-linux.
==4207== Copyright (C) 2000-2004, and GNU GPL'd, by Julian Seward et al.
--4207-- warning: Pentium with 12 K micro-op instruction trace cache
--4207--          Simulating a 16 KB cache with 32 B lines
==4207== For more details, rerun with: -v
==4207==
(null)
==4207==
==4207== Process terminating with default action of signal 11 (SIGSEGV): dumping core
==4207==  Access not within mapped region at address 0x0
==4207==    at 0x8048535: main (main.c:21)
==4207==
==4207== I   refs:      143,499
==4207== I1  misses:      1,198
==4207== L2i misses:        684
==4207== I1  miss rate:    0.83%
==4207== L2i miss rate:    0.47%
==4207==
==4207== D   refs:       65,758  (49,008 rd + 16,750 wr)
==4207== D1  misses:      2,477  ( 2,157 rd +    320 wr)
==4207== L2d misses:      1,297  ( 1,060 rd +    237 wr)
==4207== D1  miss rate:     3.7% (   4.4%   +    1.9%  )
==4207== L2d miss rate:     1.9% (   2.1%   +    1.4%  )
==4207==
==4207== L2 refs:         3,675  ( 3,355 rd +    320 wr)
==4207== L2 misses:       1,981  ( 1,744 rd +    237 wr)
==4207== L2 miss rate:      0.9% (   0.9%   +    1.4%  )


 

참조 : http://bibreen.cafe24.com/moniwiki/wiki.php/Valgrind

반응형

'Backend > C' 카테고리의 다른 글

glib의 간단한 사용법  (0) 2012.04.12
Memmove  (0) 2012.04.12
gprof  (0) 2011.05.26
GDB  (0) 2011.05.26
memset()을 이용하여 메모리를 채워보자  (0) 2011.05.20