{"id":116,"date":"2014-12-19T01:26:51","date_gmt":"2014-12-18T17:26:51","guid":{"rendered":"http:\/\/www.zhuyanbin.com\/?p=116"},"modified":"2014-12-19T01:26:51","modified_gmt":"2014-12-18T17:26:51","slug":"linux%e4%b8%8bvalgrind%e7%9a%84%e4%bd%bf%e7%94%a8%e6%a6%82%e8%bf%b0","status":"publish","type":"post","link":"https:\/\/www.yanbin888.com\/?p=116","title":{"rendered":"linux\u4e0bvalgrind\u7684\u4f7f\u7528\u6982\u8ff0"},"content":{"rendered":"<p>Valgrind\u7b80\u4ecb\uff1a<\/p>\n<p>Valgrind\u662f\u52a8\u6001\u5206\u6790\u5de5\u5177\u7684\u6846\u67b6\u3002\u6709\u5f88\u591aValgrind\u5de5\u5177\u53ef\u4ee5\u81ea\u52a8\u7684\u68c0\u6d4b\u8bb8\u591a\u5185\u5b58\u7ba1\u7406\u548c\u591a\u8fdb\u7a0b\/\u7ebf\u7a0b\u7684bugs\uff0c\u5728\u7ec6\u8282\u4e0a\u5256\u6790\u4f60\u7684\u7a0b\u5e8f\u3002\u4f60\u4e5f\u53ef\u4ee5\u5229\u7528Valgrind\u6846\u67b6\u6765\u5b9e\u73b0\u81ea\u5df1\u7684\u5de5\u5177\u3002<\/p>\n<p>Valgrind\u901a\u5e38\u5305\u62ec6\u4e2a\u5de5\u5177\uff1a\u4e00\u4e2a\u5185\u5b58\u9519\u8bef\u4fa6\u6d4b\u5de5\u5177\uff0c\u4e24\u4e2a\u7ebf\u7a0b\u9519\u8bef\u4fa6\u6d4b\u5de5\u5177\uff0ccache\u548c\u5206\u652f\u9884\u6d4b\u7684\u5206\u6790\u5de5\u5177,\u5806\u7684\u5206\u6790\u5de5\u5177\u3002<\/p>\n<p>Valgrind\u7684\u4f7f\u7528\u4e0eCPU OS\u4ee5\u53ca\u7f16\u8bd1\u5668\u548cC\u5e93\u90fd\u6709\u5173\u7cfb\u3002\u76ee\u524d\u652f\u6301\u4e0b\u9762\u7684\u5e73\u53f0\uff1a<\/p>\n<p>&#8211; x86\/Linux<\/p>\n<p>&#8211; AMD64\/Linux<\/p>\n<p>&#8211; PPC32\/Linux<\/p>\n<p>&#8211; PPC64\/Linux<\/p>\n<p>&#8211; ARM\/Linux<\/p>\n<p>&#8211; x86\/MacOSX<\/p>\n<p>&#8211; AMD64\/MacOSX<\/p>\n<p>&nbsp;<\/p>\n<p>Valgrind\u662fGNU v2\u4e0b\u7684\u5f00\u6e90\u8f6f\u4ef6\uff0c\u4f60\u53ef\u4ee5\u4ecehttp:\/\/valgrind.org\u4e0b\u8f7d\u6700\u65b0\u7684\u6e90\u4ee3\u7801\u3002<\/p>\n<p>&nbsp;<\/p>\n<p>Valgrind\u7684\u5b89\u88c5\uff1a<\/p>\n<p>1.\u4ecehttp:\/\/valgrind.org\u4e0b\u8f7d\u6700\u65b0\u7684valgrind-3.7.0.tar.bz2d\uff0c\u7528tar -xfvalgrind-3.7.0.tar.bz2\u89e3\u538b\u5b89\u88c5\u5305\u3002<\/p>\n<p>2.\u6267\u884c.\/configure\uff0c\u68c0\u67e5\u5b89\u88c5\u8981\u6c42\u7684\u914d\u7f6e\u3002<\/p>\n<p>3.\u6267\u884cmake\u3002<\/p>\n<p>4.\u6267\u884cmake install\uff0c\u6700\u597d\u662f\u7528root\u6743\u9650\u3002<\/p>\n<p>5.\u8bd5\u7740valgrind ls -l\u6765\u68c0\u6d4b\u662f\u5426\u6b63\u5e38\u5de5\u4f5c\u3002<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>Valgrind\u7684\u6982\u8ff0\uff1a<\/p>\n<p>Valgrind\u65f6\u5efa\u7acb\u52a8\u6001\u5206\u6790\u5de5\u5177\u7684\u6846\u67b6\u3002\u5b83\u6709\u4e00\u7cfb\u5217\u7528\u4e8e\u8c03\u8bd5\u5206\u6790\u7684\u5de5\u5177\u3002Valgrind\u7684\u67b6\u6784\u662f\u7ec4\u4ef6\u5316\u7684\uff0c\u6240\u4ee5\u53ef\u4ee5\u65b9\u4fbf\u7684\u6dfb\u52a0\u65b0\u7684\u5de5\u5177\u800c\u4e0d\u5f71\u54cd\u5f53\u524d\u7684\u7ed3\u6784\u3002<\/p>\n<p>&nbsp;<\/p>\n<p>\u4e0b\u9762\u7684\u5de5\u5177\u662f\u5b89\u88c5\u65f6\u7684\u6807\u51c6\u914d\u7f6e\uff1a<\/p>\n<p>Memcheck\uff1a\u7528\u4e8e\u68c0\u6d4b\u5185\u5b58\u9519\u8bef\u3002\u5b83\u5e2e\u52a9c\u548cc++\u7684\u7a0b\u5e8f\u66f4\u6b63\u786e\u3002<\/p>\n<p>Cachegrind\uff1a\u7528\u4e8e\u5206\u6790cache\u548c\u5206\u652f\u9884\u6d4b\u3002\u5b83\u5e2e\u52a9\u7a0b\u5e8f\u6267\u884c\u5f97\u66f4\u5feb\u3002<\/p>\n<p>Callgrind\uff1a\u7528\u4e8e\u51fd\u6570\u8c03\u7528\u7684\u5206\u6790\u3002<\/p>\n<p>Helgrind\uff1a\u7528\u4e8e\u5206\u6790\u591a\u7ebf\u7a0b\u3002<\/p>\n<p>DRD\uff1a\u4e5f\u7528\u4e8e\u5206\u6790\u591a\u7ebf\u7a0b\u3002\u4e0eHelgrind\u7c7b\u4f3c\uff0c\u4f46\u662f\u7528\u4e0d\u540c\u7684\u5206\u6790\u6280\u672f\uff0c\u6240\u4ee5\u53ef\u4ee5\u68c0\u6d4b\u4e0d\u540c\u7684\u95ee\u9898\u3002<\/p>\n<p>Massif\uff1a\u7528\u4e8e\u5206\u6790\u5806\u3002\u5b83\u5e2e\u52a9\u7a0b\u5e8f\u7cbe\u7b80\u5185\u5b58\u7684\u4f7f\u7528\u3002<\/p>\n<p>SGcheck\uff1a\u68c0\u6d4b\u6808\u548c\u5168\u5c40\u6570\u7ec4\u6ea2\u51fa\u7684\u5b9e\u9a8c\u6027\u5de5\u5177\uff0c\u5b83\u548cMemcheck\u4e92\u8865\u4f7f\u7528\u3002<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>Valgrind\u7684\u4f7f\u7528\uff1a<\/p>\n<p>1.\u51c6\u5907\u597d\u7a0b\u5e8f\uff1a<\/p>\n<p>\u7f16\u8bd1\u7a0b\u5e8f\u65f6\u7528-g\uff0c\u8fd9\u6837\u7f16\u8bd1\u540e\u7684\u6587\u4ef6\u5305\u542b\u8c03\u8bd5\u4fe1\u606f\uff0c\u90a3Memcheck\u7684\u5206\u6790\u4fe1\u606f\u4e2d\u5c31\u5305\u542b\u6b63\u786e\u7684\u884c\u53f7\u3002\u6700\u597d\u4f7f\u7528-O0\u7684\u4f18\u5316\u7b49\u7ea7\uff0c\u4f7f\u7528-O2\u53ca\u4ee5\u4e0a\u7684\u4f18\u5316\u7b49\u7ea7\u4f7f\u7528\u65f6\u53ef\u80fd\u4f1a\u6709\u95ee\u9898\u3002<\/p>\n<p>2.\u5728Memcheck\u4e0b\u8fd0\u884c\u7a0b\u5e8f\uff1a<\/p>\n<p>\u5982\u679c\u4f60\u7684\u7a0b\u5e8f\u6267\u884c\u5982\u4e0b\uff1a<\/p>\n<p>myprog arg1 arg2<\/p>\n<p>\u90a3\u4e48\u4f7f\u7528\u5982\u4e0b\uff1a<\/p>\n<p>valgrind &#8211;leak-check=yes myprog arg1 arg2<\/p>\n<p>Memcheck\u662f\u9ed8\u8ba4\u7684\u5de5\u5177\u3002&#8211;leak-check\u6253\u5f00\u5185\u5b58\u6cc4\u6f0f\u68c0\u6d4b\u7684\u7ec6\u8282\u3002<\/p>\n<p>\u5728\u4e0a\u9762\u7684\u547d\u4ee4\u4e2d\u8fd0\u884c\u7a0b\u5e8f\u4f1a\u4f7f\u5f97\u7a0b\u5e8f\u8fd0\u884c\u5f88\u6162\uff0c\u800c\u4e14\u5360\u7528\u5927\u91cf\u7684\u5185\u5b58\u3002Memcheck\u4f1a\u663e\u793a\u5185\u5b58\u9519\u8bef\u548c\u68c0\u6d4b\u5230\u7684\u5185\u5b58\u6cc4\u6f0f\u3002<\/p>\n<p>3.\u5982\u4f55\u67e5\u770bMemcheck\u7684\u8f93\u51fa\uff1a<\/p>\n<p>\u8fd9\u91cc\u6709\u4e00\u4e2a\u5b9e\u4f8bc\u4ee3\u7801\uff08a.c\uff09\uff0c\u6709\u4e00\u4e2a\u5185\u5b58\u9519\u8bef\u548c\u4e00\u4e2a\u5185\u5b58\u6cc4\u6f0f\u3002<\/p>\n<p>#include &lt;stdlib.h&gt;<\/p>\n<p>void f(void)<\/p>\n<p>{<\/p>\n<p>int*x = (int *)malloc(10 * sizeof(int));<\/p>\n<p>x[10]= 0;<\/p>\n<p>\/\/problem 1: heap block overrun<\/p>\n<p>}\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/problem 2: memory leak &#8212; x not freed<\/p>\n<p>&nbsp;<\/p>\n<p>int main(void)<\/p>\n<p>{<\/p>\n<p>f();<\/p>\n<p>return0;<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>\u8fd0\u884c\u5982\u4e0b\uff1a<\/p>\n<p>huerjia@huerjia:~\/NFS\/valg\/test$ valgrind&#8211;leak-check=yes .\/a<\/p>\n<p>==24780== Memcheck, a memory error detector<\/p>\n<p>==24780== Copyright (C) 2002-2011, and GNUGPL&#8217;d, by Julian Seward et al.<\/p>\n<p>==24780== Using Valgrind-3.7.0 and LibVEX;rerun with -h for copyright info<\/p>\n<p>==24780== Command: .\/a<\/p>\n<p>==24780==<\/p>\n<p>==24780== Invalid write of size 4<\/p>\n<p>==24780==\u00a0\u00a0\u00a0at 0x80484DF: f() (a.c:5)<\/p>\n<p>==24780==\u00a0\u00a0\u00a0by 0x80484F1: main (a.c:11)<\/p>\n<p>==24780==\u00a0Address 0x42d3050 is 0 bytes after a block of size 40 alloc&#8217;d<\/p>\n<p>==24780==\u00a0\u00a0\u00a0at 0x4026444: malloc (vg_replace_malloc.c:263)<\/p>\n<p>==24780==\u00a0\u00a0\u00a0by 0x80484D5: f() (a.c:4)<\/p>\n<p>==24780==\u00a0\u00a0\u00a0by 0x80484F1: main (a.c:11)<\/p>\n<p>==24780==<\/p>\n<p>==24780==<\/p>\n<p>==24780== HEAP SUMMARY:<\/p>\n<p>==24780==\u00a0\u00a0\u00a0\u00a0 in use at exit: 40 bytes in 1 blocks<\/p>\n<p>==24780==\u00a0\u00a0total heap usage: 1 allocs, 0 frees, 40 bytes allocated<\/p>\n<p>==24780==<\/p>\n<p>==24780== 40 bytes in 1 blocks aredefinitely lost in loss record 1 of 1<\/p>\n<p>==24780==\u00a0\u00a0\u00a0at 0x4026444: malloc (vg_replace_malloc.c:263)<\/p>\n<p>==24780==\u00a0\u00a0\u00a0by 0x80484D5: f() (a.c:4)<\/p>\n<p>==24780==\u00a0\u00a0\u00a0by 0x80484F1: main (a.c:11)<\/p>\n<p>==24780==<\/p>\n<p>==24780== LEAK SUMMARY:<\/p>\n<p>==24780==\u00a0\u00a0\u00a0definitely lost: 40 bytes in 1 blocks<\/p>\n<p>==24780==\u00a0\u00a0\u00a0indirectly lost: 0 bytes in 0 blocks<\/p>\n<p>==24780==\u00a0\u00a0\u00a0\u00a0\u00a0 possibly lost: 0 bytes in 0 blocks<\/p>\n<p>==24780==\u00a0\u00a0\u00a0still reachable: 0 bytes in 0 blocks<\/p>\n<p>==24780==\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 suppressed: 0 bytes in 0 blocks<\/p>\n<p>==24780==<\/p>\n<p>==24780== For counts of detected andsuppressed errors, rerun with: -v<\/p>\n<p>==24780== ERROR SUMMARY: 2 errors from 2contexts (suppressed: 17 from 6)<\/p>\n<p>&nbsp;<\/p>\n<p>\u5982\u4f55\u6765\u9605\u8bfb\u8fd9\u4e2a\u8f93\u51fa\u7ed3\u679c\uff1a<\/p>\n<p>==24780== Memcheck, a memory error detector<\/p>\n<p>==24780== Copyright (C) 2002-2011, and GNUGPL&#8217;d, by Julian Seward et al.<\/p>\n<p>==24780== Using Valgrind-3.7.0 and LibVEX;rerun with -h for copyright info<\/p>\n<p>==24780== Command: .\/a<\/p>\n<p>\u8fd9\u4e00\u90e8\u5206\u662f\u663e\u793a\u4f7f\u7528\u7684\u5de5\u5177\u4ee5\u53ca\u7248\u672c\u4fe1\u606f\u3002\u5176\u4e2d24780\u662fProcess ID\u3002<\/p>\n<p>&nbsp;<\/p>\n<p>==24780== Invalid write of size 4<\/p>\n<p>==24780==\u00a0\u00a0\u00a0at 0x80484DF: f() (a.c:5)<\/p>\n<p>==24780==\u00a0\u00a0\u00a0by 0x80484F1: main (a.c:11)<\/p>\n<p>==24780==\u00a0Address 0x42d3050 is 0 bytes after a block of size 40 alloc&#8217;d<\/p>\n<p>==24780==\u00a0\u00a0\u00a0at 0x4026444: malloc (vg_replace_malloc.c:263)<\/p>\n<p>==24780==\u00a0\u00a0\u00a0by 0x80484D5: f() (a.c:4)<\/p>\n<p>==24780==\u00a0\u00a0\u00a0by 0x80484F1: main (a.c:11)<\/p>\n<p>\u8fd9\u90e8\u5206\u6307\u51fa\u4e86\u9519\u8bef\uff1aInvalid write\u3002\u540e\u9762\u7684\u51e0\u884c\u663e\u793a\u4e86\u51fd\u6570\u5806\u6808\u3002<\/p>\n<p>&nbsp;<\/p>\n<p>==24780== HEAP SUMMARY:<\/p>\n<p>==24780==\u00a0\u00a0\u00a0\u00a0 in use at exit: 40 bytes in 1 blocks<\/p>\n<p>==24780==\u00a0\u00a0total heap usage: 1 allocs, 0 frees, 40 bytes allocated<\/p>\n<p>==24780==<\/p>\n<p>==24780== 40 bytes in 1 blocks aredefinitely lost in loss record 1 of 1<\/p>\n<p>==24780==\u00a0\u00a0\u00a0at 0x4026444: malloc (vg_replace_malloc.c:263)<\/p>\n<p>==24780==\u00a0\u00a0\u00a0by 0x80484D5: f() (a.c:4)<\/p>\n<p>==24780==\u00a0\u00a0\u00a0by 0x80484F1: main (a.c:11)<\/p>\n<p>==24780==<\/p>\n<p>==24780== LEAK SUMMARY:<\/p>\n<p>==24780==\u00a0\u00a0\u00a0definitely lost: 40 bytes in 1 blocks<\/p>\n<p>==24780==\u00a0\u00a0\u00a0indirectly lost: 0 bytes in 0 blocks<\/p>\n<p>==24780==\u00a0\u00a0\u00a0\u00a0\u00a0 possibly lost: 0 bytes in 0 blocks<\/p>\n<p>==24780==\u00a0\u00a0\u00a0still reachable: 0 bytes in 0 blocks<\/p>\n<p>==24780==\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 suppressed: 0 bytes in 0 blocks<\/p>\n<p>\u8fd9\u90e8\u5206\u662f\u5bf9\u5806\u548c\u6cc4\u6f0f\u7684\u603b\u7ed3\uff0c\u53ef\u4ee5\u770b\u51fa\u5185\u5b58\u6cc4\u6f0f\u7684\u9519\u8bef\u3002<\/p>\n<p>&nbsp;<\/p>\n<p>==24780== For counts of detected andsuppressed errors, rerun with: -v<\/p>\n<p>==24780== ERROR SUMMARY: 2 errors from 2contexts (suppressed: 17 from 6)<\/p>\n<p>\u8fd9\u90e8\u5206\u662f\u5806\u6240\u6709\u68c0\u6d4b\u5230\u7684\u9519\u8bef\u7684\u603b\u7ed3\u3002\u4ee3\u7801\u4e2d\u7684\u4e24\u4e2a\u9519\u8bef\u90fd\u68c0\u6d4b\u5230\u4e86\u3002<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>Helgrind:\u7ebf\u7a0b\u9519\u8bef\u68c0\u6d4b\u5de5\u5177<\/p>\n<p>\u82e5\u4f7f\u7528\u8fd9\u4e2a\u5de5\u5177\uff0c\u5728Valgrind\u7684\u547d\u4ee4\u4e2d\u6dfb\u52a0&#8211;tool=helgrind\u3002<\/p>\n<p>Helgrind\u7528\u4e8ec\uff0cc++\u4e0b\u4f7f\u7528POSIXpthreads\u7684\u7a0b\u5e8f\u7684\u7ebf\u7a0b\u540c\u6b65\u9519\u8bef\u3002<\/p>\n<p>Helgrind\u53ef\u4ee5\u68c0\u6d4b\u4e0b\u9762\u4e09\u7c7b\u9519\u8bef\uff1a<\/p>\n<p>1.POSIX pthreads API\u7684\u9519\u8bef\u4f7f\u7528<\/p>\n<p>2.\u7531\u52a0\u9501\u548c\u89e3\u9501\u987a\u5e8f\u5f15\u8d77\u7684\u6f5c\u5728\u7684\u6b7b\u9501<\/p>\n<p>3.\u6570\u636e\u7ade\u6001&#8211;\u5728\u6ca1\u6709\u9501\u6216\u8005\u540c\u6b65\u673a\u5236\u4e0b\u8bbf\u95ee\u5185\u5b58<\/p>\n<p>&nbsp;<\/p>\n<p>\u4ee5\u6570\u636e\u7ade\u6001\u4e3a\u4f8b\u6765\u8bf4\u660eHelgrind\u7684\u7528\u6cd5\uff1a<\/p>\n<p>\u5728\u4e0d\u4f7f\u7528\u5408\u9002\u7684\u9501\u6216\u8005\u5176\u4ed6\u540c\u6b65\u673a\u5236\u6765\u4fdd\u8bc1\u5355\u7ebf\u7a0b\u8bbf\u95ee\u65f6\uff0c\u4e24\u4e2a\u6216\u8005\u591a\u4e2a\u7ebf\u7a0b\u8bbf\u95ee\u540c\u4e00\u5757\u5185\u5b58\u5c31\u53ef\u80fd\u5f15\u53d1\u6570\u636e\u7ade\u6001\u3002<\/p>\n<p>\u4e00\u4e2a\u7b80\u5355\u7684\u6570\u636e\u7ade\u6001\u7684\u4f8b\u5b50\uff1a<\/p>\n<p>#include &lt;pthread.h&gt;<\/p>\n<p>&nbsp;<\/p>\n<p>int var = 0;<\/p>\n<p>&nbsp;<\/p>\n<p>void* child_fn ( void* arg ) {<\/p>\n<p>var++;\/* Unprotected relative to parent *\/ \/* this is line 6 *\/<\/p>\n<p>returnNULL;<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>int main ( void ) {<\/p>\n<p>pthread_tchild;<\/p>\n<p>pthread_create(&amp;child,NULL, child_fn, NULL);<\/p>\n<p>var++;\/* Unprotected relative to child *\/ \/* this is line 13 *\/<\/p>\n<p>pthread_join(child,NULL);<\/p>\n<p>return0;<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>\u8fd0\u884c\u5982\u4e0b\uff1a<\/p>\n<p>huerjia@huerjia:~\/NFS\/valg\/test$ valgrind&#8211;tool=helgrind .\/b<\/p>\n<p>==25449== Helgrind, a thread error detector<\/p>\n<p>==25449== Copyright (C) 2007-2011, and GNUGPL&#8217;d, by OpenWorks LLP et al.<\/p>\n<p>==25449== Using Valgrind-3.7.0 and LibVEX;rerun with -h for copyright info<\/p>\n<p>==25449== Command: .\/b<\/p>\n<p>==25449==<\/p>\n<p>==25449==&#8212;Thread-Announcement&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<\/p>\n<p>==25449==<\/p>\n<p>==25449== Thread #1 is the program&#8217;s rootthread<\/p>\n<p>==25449==<\/p>\n<p>==25449== &#8212;Thread-Announcement&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<\/p>\n<p>==25449==<\/p>\n<p>==25449== Thread #2 was created<\/p>\n<p>==25449==\u00a0\u00a0\u00a0at 0x4123A38: clone (in \/lib\/tls\/i686\/cmov\/libc-2.11.1.so)<\/p>\n<p>==25449==\u00a0\u00a0\u00a0by 0x40430EA: pthread_create@@GLIBC_2.1 (in \/lib\/tls\/i686\/cmov\/libpthread-2.11.1.so)<\/p>\n<p>==25449==\u00a0\u00a0\u00a0by 0x402A9AD: pthread_create_WRK (hg_intercepts.c:255)<\/p>\n<p>==25449==\u00a0\u00a0\u00a0by 0x402AA85: pthread_create@* (hg_intercepts.c:286)<\/p>\n<p>==25449==\u00a0\u00a0\u00a0by 0x80484E1: main (b.c:11)<\/p>\n<p>==25449==<\/p>\n<p>==25449==&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-<\/p>\n<p>==25449==<\/p>\n<p>==25449== Possible data race during read ofsize 4 at 0x804A020 by thread #1<\/p>\n<p>==25449== Locks held: none<\/p>\n<p>==25449==\u00a0\u00a0\u00a0at 0x80484E2: main (b.c:12)<\/p>\n<p>==25449==<\/p>\n<p>==25449== This conflicts with a previouswrite of size 4 by thread #2<\/p>\n<p>==25449== Locks held: none<\/p>\n<p>==25449==\u00a0\u00a0\u00a0at 0x80484A7: child_fn (b.c:6)<\/p>\n<p>==25449==\u00a0\u00a0\u00a0by 0x402AB04: mythread_wrapper (hg_intercepts.c:219)<\/p>\n<p>==25449==\u00a0\u00a0\u00a0by 0x404296D: start_thread (in \/lib\/tls\/i686\/cmov\/libpthread-2.11.1.so)<\/p>\n<p>==25449==\u00a0\u00a0\u00a0by 0x4123A4D: clone (in \/lib\/tls\/i686\/cmov\/libc-2.11.1.so)<\/p>\n<p>==25449==<\/p>\n<p>==25449==&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-<\/p>\n<p>==25449==<\/p>\n<p>==25449== Possible data race during writeof size 4 at 0x804A020 by thread #1<\/p>\n<p>==25449== Locks held: none<\/p>\n<p>==25449==\u00a0\u00a0\u00a0at 0x80484E2: main (b.c:12)<\/p>\n<p>==25449==<\/p>\n<p>==25449== This conflicts with a previouswrite of size 4 by thread #2<\/p>\n<p>==25449== Locks held: none<\/p>\n<p>==25449==\u00a0\u00a0\u00a0at 0x80484A7: child_fn (b.c:6)<\/p>\n<p>==25449==\u00a0\u00a0\u00a0by 0x402AB04: mythread_wrapper (hg_intercepts.c:219)<\/p>\n<p>==25449==\u00a0\u00a0\u00a0by 0x404296D: start_thread (in \/lib\/tls\/i686\/cmov\/libpthread-2.11.1.so)<\/p>\n<p>==25449==\u00a0\u00a0\u00a0by 0x4123A4D: clone (in \/lib\/tls\/i686\/cmov\/libc-2.11.1.so)<\/p>\n<p>==25449==<\/p>\n<p>==25449==<\/p>\n<p>==25449== For counts of detected andsuppressed errors, rerun with: -v<\/p>\n<p>==25449== Use &#8211;history-level=approx or=none to gain increased speed, at<\/p>\n<p>==25449== the cost of reduced accuracy ofconflicting-access information<\/p>\n<p>==25449== ERROR SUMMARY: 2 errors from 2contexts (suppressed: 0 from 0)<\/p>\n<p>&nbsp;<\/p>\n<p>\u9519\u8bef\u4fe1\u606f\u4ece\u201cPossible data race during write of size 4 at 0x804A020 by thread #1<\/p>\n<p>\u201d\u5f00\u59cb\uff0c\u8fd9\u6761\u4fe1\u606f\u4f60\u53ef\u4ee5\u770b\u5230\u7ade\u6001\u8bbf\u95ee\u7684\u5730\u5740\u548c\u5927\u5c0f\uff0c\u8fd8\u6709\u8c03\u7528\u7684\u5806\u6808\u4fe1\u606f\u3002<\/p>\n<p>\u7b2c\u4e8c\u6761\u8c03\u7528\u5806\u6808\u4ece\u201cThis conflicts with a previous write of size 4 by thread #2<\/p>\n<p>\u201d\u5f00\u59cb\uff0c\u8fd9\u8868\u660e\u8fd9\u91cc\u4e0e\u7b2c\u4e00\u4e2a\u8c03\u7528\u5806\u6808\u6709\u7ade\u6001\u3002<\/p>\n<p>&nbsp;<\/p>\n<p>\u4e00\u65e6\u4f60\u627e\u5230\u4e24\u4e2a\u8c03\u7528\u5806\u6808\uff0c\u5982\u4f55\u627e\u5230\u7ade\u6001\u7684\u6839\u6e90\uff1a<\/p>\n<p>\u9996\u5148\u901a\u8fc7\u6bcf\u4e2a\u8c03\u7528\u5806\u6808\u68c0\u67e5\u4ee3\u7801\uff0c\u5b83\u4eec\u90fd\u4f1a\u663e\u793a\u5bf9\u540c\u4e00\u4e2a\u4f4d\u7f6e\u6216\u8005\u53d8\u91cf\u7684\u8bbf\u95ee\u3002<\/p>\n<p>\u73b0\u5728\u8003\u8651\u5982\u4f55\u6539\u6b63\u6765\u4f7f\u5f97\u591a\u7ebf\u7a0b\u8bbf\u95ee\u5b89\u5168\uff1a<\/p>\n<p>1.\u4f7f\u7528\u9501\u6216\u8005\u5176\u4ed6\u7684\u540c\u6b65\u673a\u5236\uff0c\u4fdd\u8bc1\u540c\u4e00\u65f6\u95f4\u53ea\u6709\u72ec\u7acb\u7684\u8bbf\u95ee\u3002<\/p>\n<p>2.\u4f7f\u7528\u6761\u4ef6\u53d8\u91cf\u7b49\u65b9\u6cd5\uff0c\u786e\u5b9a\u591a\u6b21\u8bbf\u95ee\u7684\u6b21\u5e8f\u3002<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>\u672c\u6587\u4ecb\u7ecd\u4e86valgrind\u7684\u4f53\u7cfb\u7ed3\u6784\uff0c\u5e76\u91cd\u70b9\u4ecb\u7ecd\u4e86\u5176\u5e94\u7528\u6700\u5e7f\u6cdb\u7684\u5de5\u5177\uff1amemcheck\u548chelgrind\u3002\u9610\u8ff0\u4e86memcheck\u548chelgrind\u7684\u57fa\u672c\u4f7f\u7528\u65b9\u6cd5\u3002\u5728\u9879\u76ee\u4e2d\u5c3d\u65e9\u7684\u53d1\u73b0\u5185\u5b58\u95ee\u9898\u548c\u591a\u8fdb\u7a0b\u540c\u6b65\u95ee\u9898\uff0c\u80fd\u591f\u6781\u5927\u5730\u63d0\u9ad8\u5f00\u53d1\u6548\u7387\uff0cvalgrind\u5c31\u662f\u80fd\u591f\u5e2e\u52a9\u4f60\u5b9e\u73b0\u8fd9\u4e00\u76ee\u6807\u7684\u51fa\u8272\u5de5\u5177\u3002<\/p>\n<p>&nbsp;<\/p>\n<p>link:\u00a0<a title=\"http:\/\/blog.csdn.net\/dndxhej\/article\/details\/7855520\" href=\"http:\/\/blog.csdn.net\/dndxhej\/article\/details\/7855520\" target=\"_blank\">http:\/\/blog.csdn.net\/dndxhej\/article\/details\/7855520<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Valgrind\u7b80\u4ecb\uff1a Valgrind\u662f\u52a8\u6001\u5206\u6790\u5de5\u5177\u7684\u6846\u67b6\u3002\u6709\u5f88\u591aValgrind\u5de5\u5177\u53ef\u4ee5\u81ea\u52a8\u7684\u68c0\u6d4b\u8bb8\u591a\u5185\u5b58 <span class=\"ellipsis\">&hellip;<\/span> <span class=\"more-link-wrap\"><a href=\"https:\/\/www.yanbin888.com\/?p=116\" class=\"more-link\"><span>Read More &rarr;<\/span><\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,2,1],"tags":[23,6],"class_list":["post-116","post","type-post","status-publish","format-standard","hentry","category-c","category-linux","category-mac","tag-linux","tag-mac-os-x"],"_links":{"self":[{"href":"https:\/\/www.yanbin888.com\/index.php?rest_route=\/wp\/v2\/posts\/116","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.yanbin888.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.yanbin888.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.yanbin888.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.yanbin888.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=116"}],"version-history":[{"count":1,"href":"https:\/\/www.yanbin888.com\/index.php?rest_route=\/wp\/v2\/posts\/116\/revisions"}],"predecessor-version":[{"id":117,"href":"https:\/\/www.yanbin888.com\/index.php?rest_route=\/wp\/v2\/posts\/116\/revisions\/117"}],"wp:attachment":[{"href":"https:\/\/www.yanbin888.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=116"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.yanbin888.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=116"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.yanbin888.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=116"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}