Valgrind内存调试和代码解剖工具

Valgrind官网:http://valgrind.org/downloads/repository.html

参考文献:https://blog.csdn.net/kesalin/article/details/2593958

Valgrind工具包包含多个工具,如Memcheck,Cachegrind,Helgrind, Callgrind,Massif。下面分别介绍个工具的作用:

Memcheck 工具主要检查下面的程序错误:

        使用未初始化的内存 (Use of uninitialised memory)
        使用已经释放了的内存 (Reading/writing memory after it has been free’d)
        使用超过 malloc分配的内存空间(Reading/writing off the end of malloc’d blocks)
        对堆栈的非法访问 (Reading/writing inappropriate areas on the stack)
        申请的空间是否有释放 (Memory leaks – where pointers to malloc’d blocks are lost forever)
        malloc/free/new/delete申请和释放内存的匹配(Mismatched use of malloc/new/new [] vs free/delete/delete [])
        src和dst的重叠(Overlapping src and dst pointers in memcpy() and related functions)
Callgrind
        Callgrind收集程序运行时的一些数据,函数调用关系等信息,还可以有选择地进行cache 模拟。在运行结束时,它会把分析数据写入一个文件。callgrind_annotate可以把这个文件的内容转化成可读的形式。

Cachegrind
        它模拟 CPU中的一级缓存I1,D1和L2二级缓存,能够精确地指出程序中 cache的丢失和命中。如果需要,它还能够为我们提供cache丢失次数,内存引用次数,以及每行代码,每个函数,每个模块,整个程序产生的指令数。这对优化程序有很大的帮助。

Helgrind
        它主要用来检查多线程程序中出现的竞争问题。Helgrind 寻找内存中被多个线程访问,而又没有一贯加锁的区域,这些区域往往是线程之间失去同步的地方,而且会导致难以发掘的错误。Helgrind实现了名为” Eraser” 的竞争检测算法,并做了进一步改进,减少了报告错误的次数。

Massif
        堆栈分析器,它能测量程序在堆栈中使用了多少内存,告诉我们堆块,堆管理块和栈的大小。Massif能帮助我们减少内存的使用,在带有虚拟内存的现代系统中,它还能够加速我们程序的运行,减少程序停留在交换区中的几率。

安装及使用:

git clone http://repo.or.cz/valgrind.git
cd valgrind
./autogen.sh
./configure --prefix=/home/your_name/valgrind/
make
make install


gcc -Wall test.c -g -o test
valgrind --tool=memcheck --leak-check=full ./test


flex@flex-desktop:~/Downloads/redis-4.0.0-volatile/src$ valgrind --tool=memcheck --leak-check=full ./redis-server 
==30687== Memcheck, a memory error detector
==30687== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==30687== Using Valgrind-3.14.0.GIT and LibVEX; rerun with -h for copyright info
==30687== Command: ./redis-server
==30687== 
30687:C 04 Aug 00:50:25.451 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
30687:C 04 Aug 00:50:25.484 # Redis version=4.0.0, bits=64, commit=2c59022c, modified=0, pid=30687, just started
30687:C 04 Aug 00:50:25.485 # Warning: no config file specified, using the default config. In order to specify a config file use ./redis-server /path/to/redis.conf
30687:M 04 Aug 00:50:25.515 # You requested maxclients of 10000 requiring at least 10032 max file descriptors.
30687:M 04 Aug 00:50:25.519 # Server can't set maximum open files to 10032 because of OS error: Operation not permitted.
30687:M 04 Aug 00:50:25.519 # Current maximum open files is 1024. maxclients has been reduced to 992 to compensate for low ulimit. If you need higher maxclients increase 'ulimit -n'.
                _._                                                  
           _.-``__ ''-._                                             
      _.-``    `.  `_.  ''-._           Redis 4.0.0 (2c59022c/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._                                   
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 30687
  `-._    `-._  `-./  _.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |           http://redis.io        
  `-._    `-._`-.__.-'_.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |                                  
  `-._    `-._`-.__.-'_.-'    _.-'                                   
      `-._    `-.__.-'    _.-'                                       
          `-._        _.-'                                           
              `-.__.-'                                               

30687:M 04 Aug 00:50:25.673 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
30687:M 04 Aug 00:50:25.674 # Server initialized
30687:M 04 Aug 00:50:25.675 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
30687:M 04 Aug 00:50:25.675 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
30687:M 04 Aug 00:50:25.677 * Ready to accept connections
^C30687:signal-handler (1533315241) Received SIGINT scheduling shutdown...
30687:M 04 Aug 00:54:01.756 # User requested shutdown...
30687:M 04 Aug 00:54:01.758 * Saving the final RDB snapshot before exiting.
==30687== Conditional jump or move depends on uninitialised value(s)
==30687==    at 0x433617: lzf_compress (lzf_c.c:153)
==30687==    by 0x4497FE: rdbSaveLzfStringObject.part.1 (rdb.c:319)
==30687==    by 0x449ACE: rdbSaveLzfStringObject (rdb.c:390)
==30687==    by 0x449ACE: rdbSaveRawString (rdb.c:391)
==30687==    by 0x44B1B6: rdbSaveAuxField (rdb.c:831)
==30687==    by 0x44B341: rdbSaveInfoAuxFields (rdb.c:869)
==30687==    by 0x44B498: rdbSaveRio (rdb.c:895)
==30687==    by 0x44BAB6: rdbSave (rdb.c:1011)
==30687==    by 0x42C735: prepareForShutdown (server.c:2569)
==30687==    by 0x42C888: serverCron (server.c:1001)
==30687==    by 0x425B8B: processTimeEvents (ae.c:323)
==30687==    by 0x425B8B: aeProcessEvents (ae.c:432)
==30687==    by 0x425D1A: aeMain (ae.c:464)
==30687==    by 0x422815: main (server.c:3920)
==30687== 
30687:M 04 Aug 00:54:01.791 * DB saved on disk
30687:M 04 Aug 00:54:01.793 # Redis is now ready to exit, bye bye...
==30687== 
==30687== HEAP SUMMARY:
==30687==     in use at exit: 40,375 bytes in 485 blocks
==30687==   total heap usage: 799 allocs, 314 frees, 169,260 bytes allocated
==30687== 
==30687== 912 bytes in 3 blocks are possibly lost in loss record 189 of 200
==30687==    at 0x4C2FF6C: calloc (vg_replace_malloc.c:752)
==30687==    by 0x40138A4: allocate_dtv (dl-tls.c:322)
==30687==    by 0x40138A4: _dl_allocate_tls (dl-tls.c:539)
==30687==    by 0x555B26E: allocate_stack (allocatestack.c:588)
==30687==    by 0x555B26E: pthread_create@@GLIBC_2.2.5 (pthread_create.c:539)
==30687==    by 0x47E549: bioInit (bio.c:123)
==30687==    by 0x42F5DE: initServer (server.c:1967)
==30687==    by 0x42284B: main (server.c:3883)
==30687== 
==30687== LEAK SUMMARY:
==30687==    definitely lost: 0 bytes in 0 blocks
==30687==    indirectly lost: 0 bytes in 0 blocks
==30687==      possibly lost: 912 bytes in 3 blocks
==30687==    still reachable: 39,463 bytes in 482 blocks
==30687==         suppressed: 0 bytes in 0 blocks
==30687== Reachable blocks (those to which a pointer was found) are not shown.
==30687== To see them, rerun with: --leak-check=full --show-leak-kinds=all
==30687== 
==30687== For counts of detected and suppressed errors, rerun with: -v
==30687== Use --track-origins=yes to see where uninitialised values come from
==30687== ERROR SUMMARY: 36 errors from 2 contexts (suppressed: 0 from 0)



flex@flex-desktop:~/Downloads/redis-4.0.0-volatile/src$ valgrind --tool=memcheck --leak-check=full ./redis-cli
==30705== Memcheck, a memory error detector
==30705== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==30705== Using Valgrind-3.14.0.GIT and LibVEX; rerun with -h for copyright info
==30705== Command: ./redis-cli
==30705== 
127.0.0.1:6379> 
127.0.0.1:6379> 
127.0.0.1:6379> 
127.0.0.1:6379> 
127.0.0.1:6379> 
127.0.0.1:6379> SET runoobkey redis
OK
127.0.0.1:6379> GET runoobkey
"redis"
127.0.0.1:6379> DEL runoobkey
(integer) 1
127.0.0.1:6379> 
==30705== 
==30705== HEAP SUMMARY:
==30705==     in use at exit: 9,217 bytes in 685 blocks
==30705==   total heap usage: 4,813 allocs, 4,128 frees, 166,502 bytes allocated
==30705== 
==30705== 3 bytes in 1 blocks are possibly lost in loss record 1 of 16
==30705==    at 0x4C2FF6C: calloc (vg_replace_malloc.c:752)
==30705==    by 0x4117F4: sdsnewlen (sds.c:91)
==30705==    by 0x405B6B: main (redis-cli.c:2651)
==30705== 
==30705== 4 bytes in 1 blocks are possibly lost in loss record 2 of 16
==30705==    at 0x4C2FF6C: calloc (vg_replace_malloc.c:752)
==30705==    by 0x4117F4: sdsnewlen (sds.c:91)
==30705==    by 0x411054: redisBufferWrite (hiredis.c:850)
==30705==    by 0x411332: redisGetReply (hiredis.c:882)
==30705==    by 0x40B63D: cliReadReply (redis-cli.c:760)
==30705==    by 0x40C2DF: cliSendCommand (redis-cli.c:920)
==30705==    by 0x40C884: issueCommandRepeat (redis-cli.c:1189)
==30705==    by 0x40CC78: repl (redis-cli.c:1349)
==30705==    by 0x406BED: main (redis-cli.c:2723)
==30705== 
==30705== 11 bytes in 1 blocks are possibly lost in loss record 4 of 16
==30705==    at 0x4C2FF6C: calloc (vg_replace_malloc.c:752)
==30705==    by 0x4117F4: sdsnewlen (sds.c:91)
==30705==    by 0x4059C2: main (redis-cli.c:2607)
==30705== 
==30705== 36 bytes in 1 blocks are possibly lost in loss record 5 of 16
==30705==    at 0x4C3016F: realloc (vg_replace_malloc.c:826)
==30705==    by 0x411C2C: sdsMakeRoomFor (sds.c:221)
==30705==    by 0x4125B2: sdscatlen (sds.c:379)
==30705==    by 0x4147BB: redisReaderFeed (read.c:464)
==30705==    by 0x410F2C: redisBufferRead (hiredis.c:814)
==30705==    by 0x411361: redisGetReply (hiredis.c:888)
==30705==    by 0x40B63D: cliReadReply (redis-cli.c:760)
==30705==    by 0x40C2DF: cliSendCommand (redis-cli.c:920)
==30705==    by 0x40C884: issueCommandRepeat (redis-cli.c:1189)
==30705==    by 0x40CC78: repl (redis-cli.c:1349)
==30705==    by 0x406BED: main (redis-cli.c:2723)
==30705== 
==30705== 60 bytes in 1 blocks are definitely lost in loss record 6 of 16
==30705==    at 0x4C3016F: realloc (vg_replace_malloc.c:826)
==30705==    by 0x411C2C: sdsMakeRoomFor (sds.c:221)
==30705==    by 0x4125B2: sdscatlen (sds.c:379)
==30705==    by 0x412B06: sdscatvprintf (sds.c:533)
==30705==    by 0x412C13: sdscatprintf (sds.c:558)
==30705==    by 0x40CFF8: repl (redis-cli.c:1295)
==30705==    by 0x406BED: main (redis-cli.c:2723)
==30705== 
==30705== 108 bytes in 5 blocks are possibly lost in loss record 7 of 16
==30705==    at 0x4C3016F: realloc (vg_replace_malloc.c:826)
==30705==    by 0x411C2C: sdsMakeRoomFor (sds.c:221)
==30705==    by 0x4125B2: sdscatlen (sds.c:379)
==30705==    by 0x40D0FC: cliIntegrateHelp (redis-cli.c:311)
==30705==    by 0x40D0FC: repl (redis-cli.c:1285)
==30705==    by 0x406BED: main (redis-cli.c:2723)
==30705== 
==30705== 167 bytes in 17 blocks are possibly lost in loss record 8 of 16
==30705==    at 0x4C2FF6C: calloc (vg_replace_malloc.c:752)
==30705==    by 0x4117F4: sdsnewlen (sds.c:91)
==30705==    by 0x40D075: cliIntegrateHelp (redis-cli.c:298)
==30705==    by 0x40D075: repl (redis-cli.c:1285)
==30705==    by 0x406BED: main (redis-cli.c:2723)
==30705== 
==30705== 280 bytes in 14 blocks are possibly lost in loss record 10 of 16
==30705==    at 0x4C3016F: realloc (vg_replace_malloc.c:826)
==30705==    by 0x411C2C: sdsMakeRoomFor (sds.c:221)
==30705==    by 0x4125B2: sdscatlen (sds.c:379)
==30705==    by 0x412B06: sdscatvprintf (sds.c:533)
==30705==    by 0x412C13: sdscatprintf (sds.c:558)
==30705==    by 0x40C93F: cliInitHelp (redis-cli.c:247)
==30705==    by 0x40C93F: repl (redis-cli.c:1284)
==30705==    by 0x406BED: main (redis-cli.c:2723)
==30705== 
==30705== 608 bytes in 12 blocks are possibly lost in loss record 13 of 16
==30705==    at 0x4C3016F: realloc (vg_replace_malloc.c:826)
==30705==    by 0x411C2C: sdsMakeRoomFor (sds.c:221)
==30705==    by 0x4125B2: sdscatlen (sds.c:379)
==30705==    by 0x40D15C: cliIntegrateHelp (redis-cli.c:313)
==30705==    by 0x40D15C: repl (redis-cli.c:1285)
==30705==    by 0x406BED: main (redis-cli.c:2723)
==30705== 
==30705== 1,996 bytes in 197 blocks are possibly lost in loss record 15 of 16
==30705==    at 0x4C2FF6C: calloc (vg_replace_malloc.c:752)
==30705==    by 0x4117F4: sdsnewlen (sds.c:91)
==30705==    by 0x40C9D1: cliInitHelp (redis-cli.c:256)
==30705==    by 0x40C9D1: repl (redis-cli.c:1284)
==30705==    by 0x406BED: main (redis-cli.c:2723)
==30705== 
==30705== 3,286 bytes in 235 blocks are possibly lost in loss record 16 of 16
==30705==    at 0x4C3016F: realloc (vg_replace_malloc.c:826)
==30705==    by 0x411C2C: sdsMakeRoomFor (sds.c:221)
==30705==    by 0x4125B2: sdscatlen (sds.c:379)
==30705==    by 0x41422F: sdssplitargs (sds.c:1014)
==30705==    by 0x40C9C3: cliInitHelp (redis-cli.c:255)
==30705==    by 0x40C9C3: repl (redis-cli.c:1284)
==30705==    by 0x406BED: main (redis-cli.c:2723)
==30705== 
==30705== LEAK SUMMARY:
==30705==    definitely lost: 60 bytes in 1 blocks
==30705==    indirectly lost: 0 bytes in 0 blocks
==30705==      possibly lost: 6,499 bytes in 484 blocks
==30705==    still reachable: 2,658 bytes in 200 blocks
==30705==         suppressed: 0 bytes in 0 blocks
==30705== Reachable blocks (those to which a pointer was found) are not shown.
==30705== To see them, rerun with: --leak-check=full --show-leak-kinds=all
==30705== 
==30705== For counts of detected and suppressed errors, rerun with: -v
==30705== ERROR SUMMARY: 11 errors from 11 contexts (suppressed: 0 from 0)

猜你喜欢

转载自blog.csdn.net/qccz123456/article/details/81450906