http://blog.csdn.net/penzchan/article/details/10240951
使用 valgrind + memcheck 運行程序, 查找錯誤的線程id.
./valgrind --leak-check=yes --show-reachable=yes ./test // 這裏test爲目標程序
- ==1499== Thread 2:
- ==1499== Invalid read of size 4
- ==1499== at 0x9D70: thread_exec (test.c:152)
- ==1499== Address 0x0 is not stack'd, malloc'd or (recently) free'd
- ==1499==
- ==1499==
- ==1499== Process terminating with default action of signal 11 (SIGSEGV)
- ==1499== Access not within mapped region at address 0x0
- ==1499== at 0x9D70: thread_exec (test.c:152)
- ==1499== If you believe this happened as a result of a stack
- ==1499== overflow in your program's main thread (unlikely but
- ==1499== possible), you can try to increase the size of the
- ==1499== main thread stack using the --main-stacksize= flag.
- ==1499== The main thread stack size used in this run was 8388608.
- ==1499==
- ==1499== HEAP SUMMARY:
- ==1499== in use at exit: 272 bytes in 2 blocks
- ==1499== total heap usage: 2 allocs, 0 frees, 272 bytes allocated
- ==1499==
- ==1499== Thread 1:
- ==1499== 272 bytes in 2 blocks are possibly lost in loss record 1 of 1
- ==1499== at 0x4832240: calloc (vg_replace_malloc.c:593)
- ==1499== by 0x4011203: _dl_allocate_tls (in /lib/ld-2.8.so)
- ==1499== by 0x4A4ECA7: pthread_create (in /lib/libpthread-2.8.so)
- ==1499==
- ==1499== LEAK SUMMARY:
- ==1499== definitely lost: 0 bytes in 0 blocks
- ==1499== indirectly lost: 0 bytes in 0 blocks
- ==1499== possibly lost: 272 bytes in 2 blocks
- ==1499== still reachable: 0 bytes in 0 blocks
==1499== Thread 2:
==1499== Invalid read of size 4
==1499== at 0x9D70: thread_exec (test.c:152)
==1499== Address 0x0 is not stack'd, malloc'd or (recently) free'd
==1499==
==1499==
==1499== Process terminating with default action of signal 11 (SIGSEGV)
==1499== Access not within mapped region at address 0x0
==1499== at 0x9D70: thread_exec (test.c:152)
==1499== If you believe this happened as a result of a stack
==1499== overflow in your program's main thread (unlikely but
==1499== possible), you can try to increase the size of the
==1499== main thread stack using the --main-stacksize= flag.
==1499== The main thread stack size used in this run was 8388608.
==1499==
==1499== HEAP SUMMARY:
==1499== in use at exit: 272 bytes in 2 blocks
==1499== total heap usage: 2 allocs, 0 frees, 272 bytes allocated
==1499==
==1499== Thread 1:
==1499== 272 bytes in 2 blocks are possibly lost in loss record 1 of 1
==1499== at 0x4832240: calloc (vg_replace_malloc.c:593)
==1499== by 0x4011203: _dl_allocate_tls (in /lib/ld-2.8.so)
==1499== by 0x4A4ECA7: pthread_create (in /lib/libpthread-2.8.so)
==1499==
==1499== LEAK SUMMARY:
==1499== definitely lost: 0 bytes in 0 blocks
==1499== indirectly lost: 0 bytes in 0 blocks
==1499== possibly lost: 272 bytes in 2 blocks
==1499== still reachable: 0 bytes in 0 blocks
這裏顯示 線程2出現非法訪問, 訪問了四個字節.
注:編譯源文件時需要添加 -g 選項. valgrind直接定位出錯誤的文件和函數位置.
對於一些引用某些動態庫的程序, valgrind可能無法定位出錯誤的文件和函數位置.
那麼我們可以通過一下步驟來獲取更多的信息.
1. 爲線程設定名字. 以做明確的區分.
詳見: 爲線程設置名字
2.使用valgrind定位出錯誤的線程id號.
3. 使用valgrind執行程序後, ps 查看程序的id值. 如:ps | grep test
4. 進入進程的proc目錄. /proc/xxxx/task
搜索每個線程的名字. grep "Name*" */status 如:
- 1499/status:Name: memcheck-arm-li
- 1500/status:Name: chk_state
- 1501/status:Name: chk_pakage
1499/status:Name: memcheck-arm-li
1500/status:Name: chk_state
1501/status:Name: chk_pakage
這裏可以看出,
該進程的線程1爲調試線程.
線程2爲線程 chk_state
線程3爲線程chk_package
根據步驟2定位出的線程id, 就可以找到出錯的具體線程.