Valgrind
Valgrind는 리눅스 기반의 오픈소스 (GPL license) DBI 도구이다. 이는 클라이언트 프로그램 (valgrind 에 입력으로 들어가는)의 실행 코드를 실행 시간에 직접 가공하는 기회를 제공함을 뜻한다. valgrind 는 크게 코어(Core)와 도구(Tool) 로 구성되어 있는데 일반적으로 valgrind 라 함은 코어를 뜻한다.
참고로 단어는 변수를 뜻하는 value와 갈다를 뜻하는 grind의 합성인 듯 하다.
Category
Valgrind Suppression File
How to make a suppression file
Run valgrind as usual, but with the extra option --gen-suppressions=all
Instrumentation
Instrumentation 은 코드 세그먼트(영역)에서 코드에 필요한 내용을 추가하여 원하는 결과를 내도록 하는 작업을 통틀어 말한다. 이 instrumentation 이란 여러 단계에 걸쳐 일어날 수 있는데,
- 소스 코드
- 프리프로세서(Preprocessor code)
- 어셈블리 코드
- 기계 코드
- 이진 실행 파일
등의 과정에서 어느 단계에서든 가능하다. 일반적으로 정적 instrumentation을 의미하는 데 이는, 일단 위 단계의 코드를 분석하여 필요한 추가 코드를 삽입하는 것을 뜻한다. 그리고 instrumented code 를 실행 시켜 원하는 결과를 얻는다.
그러나 dynamic instrumentation 의 경우 runtime 에 instrumentation 을 수행하여 바로 실행토록 한다. Static instrumentation 의 경우 실행 중에 발생하는 상황을 알 수 없기 때문에 dynamic instrumentation 은 실행 중의 메모리나 캐시의 동작을 관찰하여 대응할 수 있는 장점이 있다.
Valgrind 는 대표적인 dynamic instrumentation framework 이다.
Quick Start Guide
아래와 같이 프로그램을 실행하면 된다.
또는,
Report
- Conditional jump or move depends on uninitialised value(s)
- stackoverflow - pinpointing “conditional jump or move depends on uninitialized value(s)” valgrind message
- 초기화되지 않은 값에 의존해서 조건 분기나 move를 함.
- Use of uninitialised value of size (n)
- 초기화 되지 않은 변수를 사용했다.
XML Output
HTML로 출력하고 싶다면 XHTML을 사용하면 된다. XSL과 XML파일은 Spike-test-gen.sourceforge.net_valgrind.zip 항목을 다운받으면 된다.
Troubleshooting
README_MISSING_SYSCALL_OR_IOCTL
==10900== Warning: noted but unhandled ioctl 0x30000001 with no size/direction hints
==10900== This could cause spurious value errors to appear.
==10900== See README_MISSING_SYSCALL_OR_IOCTL for guidance on writing a proper wrapper.
Memory still reachable _dl_init()
- Stackoverflow: Valgrind: Memory still reachable with trivial program using <iostream>
- Stackoverflow: c++ valgrind shows memory leak in hello world
valgrind를 사용할 때, STL을 사용할 경우 아래와 같은 에러가 발생할 수 있다.
Output:
==11500== Memcheck, a memory error detector
==11500== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==11500== Using Valgrind-3.10.1 and LibVEX; rerun with -h for copyright info
==11500== Command: ./a.out
==11500==
Hello, World!
==11500==
==11500== HEAP SUMMARY:
==11500== in use at exit: 72,704 bytes in 1 blocks
==11500== total heap usage: 1 allocs, 0 frees, 72,704 bytes allocated
==11500==
==11500== 72,704 bytes in 1 blocks are still reachable in loss record 1 of 1
==11500== at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==11500== by 0x4E9D62F: ??? (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.22)
==11500== by 0x4010109: call_init.part.0 (dl-init.c:78)
==11500== by 0x40101F2: call_init (dl-init.c:36)
==11500== by 0x40101F2: _dl_init (dl-init.c:126)
==11500== by 0x4001309: ??? (in /lib/x86_64-linux-gnu/ld-2.19.so)
==11500==
==11500== LEAK SUMMARY:
==11500== definitely lost: 0 bytes in 0 blocks
==11500== indirectly lost: 0 bytes in 0 blocks
==11500== possibly lost: 0 bytes in 0 blocks
==11500== still reachable: 72,704 bytes in 1 blocks
==11500== suppressed: 0 bytes in 0 blocks
==11500==
==11500== For counts of detected and suppressed errors, rerun with: -v
==11500== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
이 현상은 STL에서 사용하는 전역 메모리풀 때문에 발생되는 현상으로, 신경쓰지 않아도 된다.
See also
Favorite site
- valgrind web site
- Wikipedia (en) Valgrind에 대한 설명
- Linux Tools Project - Valgrind Support
- Linux Tools Project/Valgrind/User Guide
- Ubuntu에서 Valgrind로 Memory Leak 검사하기 (기본편)
- Ubuntu에서 Valgrind로 Memory Leak 검사하기 (활용편)
- Valgrind 디버깅방법
- memory leak 판단과 valgrind사용법
- Valgrind로 메모리릭 체크하는 방법
- valgrind를 이용한 디버깅 (Linux Debugging) (Use the KCachegrind)