<< 클래식 에코 출시 | Home | 겨울 앞에 선 엄니 >>

메모리 프로파일링 도구 ccmalloc 사용법

ccmalloc는 memory leaks을 찾아주는 프로파일러중에 하나입니다. 소스를 건드리지 않고 릭을 찾아주는 오픈소스라서 많이 사용하는 프로파일러중에 하나죠.
아래는 사용법에 대한 정보를 공유합니다. 메모리릭 걱정없는 세상을 꿈꾸면서. ㅋㅋ


1. 설치
[mimul]/home/k2/src> wget http://fossies.org/linux/misc/ccmalloc-0.4.0.tar.gz
[mimul]/home/k2/src> tar xvfz cmalloc-0.4.0.tar.gz
[mimul]/home/k2/src> cd ccmalloc-0.4.0
[mimul]/home/k2/src/ccmalloc-0.4.0> ./configure --prefix=/home/k2/Apps
- 라이브러리 디렉토리 카피
[mimul]/home/k2/src/ccmalloc-0.4.0> cp lib/libccmalloc.a /home/k2/Apps/lib
[mimul]/home/k2/src/ccmalloc-0.4.0> cp obj/ccmalloc-g++.o /home/k2/Apps/lib
[mimul]/home/k2/src/ccmalloc-0.4.0> cp obj/ccmalloc-gcc.o /home/k2/Apps/lib
2. 릭 테스트 샘플
#include <stdio.h>
void Leak(char *inStr)
{
  char *str = (char *) malloc(strlen(inStr));
  memcpy(str, inStr, strlen(inStr));
}

char *AvoidLeak(char *inStr)
{
  char *str = (char *) malloc(strlen(inStr));
  memcpy(str, inStr, strlen(inStr));
  return str;
}

int main()
{
  char *str;

  Leak("This leaks 19 bytes");
  str = AvoidLeak("This is not a 26 byte leak");
  free(str);
  str = AvoidLeak("12 byte leak");
  exit(0);
}
3. 테스트
- 로깅 출력 설정
#ccmalloc-0.4.0/ccmalloc.cfg 파일을 실행 프로그램이 있는 디렉토리에 .ccmalloc 파일로 복사
[mimul]/home/k2/src/ccmalloc-0.4.0> cp ccmalloc.cfg /home/k2/src/ccmalloc-0.4.0/myApps/.ccmalloc
%log FILE를 수정해서 log ccmalloc.log.

프로그램 실행이 끝난 후, 로컬 디렉토리에 ccmalloc.log 파일 속에 결과가 쌓인다.

- 컴파일 과정
[mimul]/home/k2/src/ccmalloc-0.4.0/myApps> vi Makefile
CC = ccmalloc gcc
CXX = ccmalloc g++

#메모리 릭 라이브러리 추가함
LIBS = -L/home/k2/Apps/lib -lccmalloc -ldl -lstdc++

[mimul]/home/k2/src/ccmalloc-0.4.0/myApps> make test1
ccmalloc gcc -o test1 test1.o -L/home/k2/Apps/lib -lccmalloc -ldl -lstdc++
[ccmalloc] version   .. 0.4.0
[ccmalloc] prefix    .. /home/k2/Apps
[ccmalloc] compilers .. g++ gcc
[ccmalloc] wrapper   .. /home/k2/Apps/lib/ccmalloc-gcc.o
gcc -o test1 test1.o -L/home/k2/Apps/lib -lccmalloc -ldl -lstdc++
  /home/k2/Apps/lib/ccmalloc-gcc.o -L/home/k2/Apps/lib -lccmalloc -ldl
[mimul]/home/k2/src/ccmalloc-0.4.0/myApps> test1
4. 리포팅 로그(ccmalloc report)
[mimul]/home/k2/src/ccmalloc-0.4.0/myApps> cat ccmalloc.log 
.--------------------------------------------------------------------------.
|================ ccmalloc-0.4.0 (C) 1997-2003 Armin Biere ================|
+--------------------------------------------------------------------------+
| executable       = /home/k2/src/ccmalloc-0.4.0/myApps/test1              |
| startup file     = .ccmalloc                                             |
| log file         = ccmalloc.log                                          |
| start time       = Thu Nov 10 20:27:25 2011                              |
| operating system = Linux 2.4.21-47.ELsmp i686 on mimul                   |
+--------------------------------------------------------------------------+
| only-count        = 0            keep-deallocated-data = 0               |
| check-interval    = 0            check-free-space      = 0               |
| check-start       = 0            file-info             = 1               |
| chain-length      = 0            additional-line       = 1               |
| check-underwrites = 0            print-addresses       = 0               |
| check-overwrites  = 0            print-on-one-line     = 0               |
| sort-by-wasted    = 1            sort-by-size          = 1               |
| # only-log-chain  = 0            continue              = 0               |
| # dont-log-chain  = 0            statistics            = 0               |
| debug             = 0            library-chains        = 0               |
| load-dynlibs      = 0            align-8-byte          = 0               |
| only-wasting-alloc= 1                                                    |
`--------------------------------------------------------------------------'

.---------------.
|ccmalloc report|
=======================================================
| total # of|   allocated | deallocated |     garbage |
+-----------+-------------+-------------+-------------+
|      bytes|          57 |          26 |          31 |
+-----------+-------------+-------------+-------------+
|allocations|           3 |           1 |           2 |
+-----------------------------------------------------+
| number of checks: 1                                 |
| number of counts: 4                                 |
| retrieving function names for addresses ... done.   |
| reading file info from gdb ... done.                |
| sorting by number of not reclaimed bytes ... done.  |
| number of call chains: 2                            |
| number of ignored call chains: 0                    |
| number of reported call chains: 2                   |
| number of internal call chains: 2                   |
| number of library call chains: 0                    |
=======================================================
|
* 61.3% = 19 Bytes of garbage allocated in 1 allocation
|       |
|       |       0x???????? in 
|       |
|       |       0x08049043 in 
| | at test1.c:20 | | | | 0x08048f97 in | | at test1.c:5 | | | `-----> 0x0804936f in | at src/wrapper.c:318 | * 38.7% = 12 Bytes of garbage allocated in 1 allocation | | | | 0x???????? in | | | | 0x08049078 in
| | at test1.c:23 | | | | 0x08048fe9 in | | at test1.c:11 | | | `-----> 0x0804936f in | at src/wrapper.c:318 | `------------------------------------------------------
- malloc()를 사용하여 않으면 통계가 나오지 않는다.


5. Troubleshooting
/home/k2/Apps/lib/ccmalloc-gcc.o(.eh_frame+0x11): In function `__static_initialization_and_destruction_0':
src/ccmalloc.cc:19: undefined reference to `__gxx_personality_v0'
collect2: ld returned 1 exit status

해결책은 -lstdc++ 추가해 준다.

6. 그외 memory leak detection tool
mtrace, memwatch, dmalloc, dmalloc, NJAMD, YAMD, patrol, Insure++, Valgrind, Purify, Bounds Checker, Coverity, Glow Code 등
Tags : , ,



Add a comment Send a TrackBack