{"id":1310,"date":"2018-06-01T19:52:05","date_gmt":"2018-06-01T11:52:05","guid":{"rendered":"https:\/\/damogame.cn:15443\/wordpress\/?p=1310"},"modified":"2018-06-01T19:52:05","modified_gmt":"2018-06-01T11:52:05","slug":"valgrind%e4%bd%bf%e7%94%a8%e8%af%b4%e6%98%8e","status":"publish","type":"post","link":"https:\/\/i007.cc\/wordpress\/archives\/1310","title":{"rendered":"Valgrind\u4f7f\u7528\u8bf4\u660e"},"content":{"rendered":"<p><strong>Valgrind<\/strong><strong>\u4f7f\u7528\u8bf4\u660e<\/strong><\/p>\n<p>Valgrind\u662f\u8fd0\u884c\u5728Linux\u4e0a\u4e00\u5957\u57fa\u4e8e\u4eff\u771f\u6280\u672f\u7684\u7a0b\u5e8f\u8c03\u8bd5\u548c\u5206\u6790\u5de5\u5177\uff0c\u662f\u516c\u8ba4\u7684\u6700\u63a5\u8fd1Purify\u7684\u4ea7\u54c1\uff0c\u5b83\u5305\u542b\u4e00\u4e2a\u5185\u6838\u2014\u2014\u4e00\u4e2a\u8f6f\u4ef6\u5408\u6210\u7684CPU\uff0c\u548c\u4e00\u7cfb\u5217\u7684\u5c0f\u5de5\u5177\uff0c\u6bcf\u4e2a\u5de5\u5177\u90fd\u53ef\u4ee5\u5b8c\u6210\u4e00\u9879\u4efb\u52a1\u2014\u2014\u8c03\u8bd5\uff0c\u5206\u6790\uff0c\u6216\u6d4b\u8bd5\u7b49\u3002Valgrind\u53ef\u4ee5\u68c0\u6d4b\u5185\u5b58\u6cc4\u6f0f\u548c\u5185\u5b58\u8d8a\u754c\uff0c\u8fd8\u53ef\u4ee5\u5206\u6790cache\u7684\u4f7f\u7528\u7b49\uff0c\u7075\u6d3b\u8f7b\u5de7\u800c\u53c8\u5f3a\u5927\u3002<\/p>\n<p><strong>\u4e00<\/strong><strong>\u00a0Valgrind<\/strong><strong>\u6982\u89c2<\/strong><\/p>\n<p>Valgrind\u7684\u6700\u65b0\u7248\u662f3.2.3\uff0c\u8be5\u7248\u672c\u5305\u542b\u4e0b\u5217\u5de5\u5177\uff1a<\/p>\n<p>1\u3001memcheck\uff1a\u68c0\u67e5\u7a0b\u5e8f\u4e2d\u7684\u5185\u5b58\u95ee\u9898\uff0c\u5982\u6cc4\u6f0f\u3001\u8d8a\u754c\u3001\u975e\u6cd5\u6307\u9488\u7b49\u3002<\/p>\n<p>2\u3001callgrind\uff1a\u68c0\u6d4b\u7a0b\u5e8f\u4ee3\u7801\u8986\u76d6\uff0c\u4ee5\u53ca\u5206\u6790\u7a0b\u5e8f\u6027\u80fd\u3002<\/p>\n<p>3\u3001cachegrind\uff1a\u5206\u6790CPU\u7684cache\u547d\u4e2d\u7387\u3001\u4e22\u5931\u7387\uff0c\u7528\u4e8e\u8fdb\u884c\u4ee3\u7801\u4f18\u5316\u3002<\/p>\n<p>4\u3001helgrind\uff1a\u7528\u4e8e\u68c0\u67e5\u591a\u7ebf\u7a0b\u7a0b\u5e8f\u7684\u7ade\u6001\u6761\u4ef6\u3002<\/p>\n<p>5\u3001massif\uff1a\u5806\u6808\u5206\u6790\u5668\uff0c\u6307\u793a\u7a0b\u5e8f\u4e2d\u4f7f\u7528\u4e86\u591a\u5c11\u5806\u5185\u5b58\u7b49\u4fe1\u606f\u3002<\/p>\n<p>6\u3001lackey\uff1a<\/p>\n<p>7\u3001nulgrind\uff1a<\/p>\n<p><strong>\u4e8c<\/strong><strong>\u00a0Valgrind<\/strong><strong>\u5de5\u5177\u8be6\u89e3<\/strong><\/p>\n<p><strong>1.Memcheck<\/strong><\/p>\n<p>\u6700\u5e38\u7528\u7684\u5de5\u5177\uff0c\u7528\u6765\u68c0\u6d4b\u7a0b\u5e8f\u4e2d\u51fa\u73b0\u7684\u5185\u5b58\u95ee\u9898\uff0c\u6240\u6709\u5bf9\u5185\u5b58\u7684\u8bfb\u5199\u90fd\u4f1a\u88ab\u68c0\u6d4b\u5230\uff0c\u4e00\u5207\u5bf9malloc\u3001free\u3001new\u3001delete\u7684\u8c03\u7528\u90fd\u4f1a\u88ab\u6355\u83b7\u3002\u6240\u4ee5\uff0c\u5b83\u80fd\u68c0\u6d4b\u4ee5\u4e0b\u95ee\u9898\uff1a<\/p>\n<p>1\u3001\u5bf9\u672a\u521d\u59cb\u5316\u5185\u5b58\u7684\u4f7f\u7528\uff1b<\/p>\n<p>2\u3001\u8bfb\/\u5199\u91ca\u653e\u540e\u7684\u5185\u5b58\u5757\uff1b<\/p>\n<p>3\u3001\u8bfb\/\u5199\u8d85\u51famalloc\u5206\u914d\u7684\u5185\u5b58\u5757\uff1b<\/p>\n<p>4\u3001\u8bfb\/\u5199\u4e0d\u9002\u5f53\u7684\u6808\u4e2d\u5185\u5b58\u5757\uff1b<\/p>\n<p>5\u3001\u5185\u5b58\u6cc4\u6f0f\uff0c\u6307\u5411\u4e00\u5757\u5185\u5b58\u7684\u6307\u9488\u6c38\u8fdc\u4e22\u5931\uff1b<\/p>\n<p>6\u3001\u4e0d\u6b63\u786e\u7684malloc\/free\u6216new\/delete\u5339\u914d\uff1b<\/p>\n<p>7\u3001memcpy()\u76f8\u5173\u51fd\u6570\u4e2d\u7684dst\u548csrc\u6307\u9488\u91cd\u53e0\u3002<\/p>\n<p>\u8fd9\u4e9b\u95ee\u9898\u5f80\u5f80\u662fC\/C++\u7a0b\u5e8f\u5458\u6700\u5934\u75bc\u7684\u95ee\u9898\uff0cMemcheck\u80fd\u5728\u8fd9\u91cc\u5e2e\u4e0a\u5927\u5fd9\u3002<\/p>\n<p><strong>2.Callgrind<\/strong><\/p>\n<p>\u548cgprof\u7c7b\u4f3c\u7684\u5206\u6790\u5de5\u5177\uff0c\u4f46\u5b83\u5bf9\u7a0b\u5e8f\u7684\u8fd0\u884c\u89c2\u5bdf\u66f4\u662f\u5165\u5fae\uff0c\u80fd\u7ed9\u6211\u4eec\u63d0\u4f9b\u66f4\u591a\u7684\u4fe1\u606f\u3002\u548cgprof\u4e0d\u540c\uff0c\u5b83\u4e0d\u9700\u8981\u5728\u7f16\u8bd1\u6e90\u4ee3\u7801\u65f6\u9644\u52a0\u7279\u6b8a\u9009\u9879\uff0c\u4f46\u52a0\u4e0a\u8c03\u8bd5\u9009\u9879\u662f\u63a8\u8350\u7684\u3002Callgrind\u6536\u96c6\u7a0b\u5e8f\u8fd0\u884c\u65f6\u7684\u4e00\u4e9b\u6570\u636e\uff0c\u5efa\u7acb\u51fd\u6570\u8c03\u7528\u5173\u7cfb\u56fe\uff0c\u8fd8\u53ef\u4ee5\u6709\u9009\u62e9\u5730\u8fdb\u884ccache\u6a21\u62df\u3002\u5728\u8fd0\u884c\u7ed3\u675f\u65f6\uff0c\u5b83\u4f1a\u628a\u5206\u6790\u6570\u636e\u5199\u5165\u4e00\u4e2a\u6587\u4ef6\u3002callgrind_annotate\u53ef\u4ee5\u628a\u8fd9\u4e2a\u6587\u4ef6\u7684\u5185\u5bb9\u8f6c\u5316\u6210\u53ef\u8bfb\u7684\u5f62\u5f0f\u3002<\/p>\n<p>\u8bf4\u660e\uff1a\u8fd9\u4e2a\u5de5\u5177\u6211\u4e5f\u6ca1\u6709\u7528\u4f1a\uff0c\u7f51\u4e0a\u57fa\u672c\u6ca1\u6709\u627e\u5230\u6709\u6307\u5bfc\u6027\u7684\u6587\u6863\uff0c\u6682\u65f6\u7559\u5728\u540e\u9762\u6162\u6162\u7814\u7a76\u5427\u3002<\/p>\n<p><strong>3.Cachegrind<\/strong><\/p>\n<p>Cache\u5206\u6790\u5668\uff0c\u5b83\u6a21\u62dfCPU\u4e2d\u7684\u4e00\u7ea7\u7f13\u5b58I1\uff0cDl\u548c\u4e8c\u7ea7\u7f13\u5b58\uff0c\u80fd\u591f\u7cbe\u786e\u5730\u6307\u51fa\u7a0b\u5e8f\u4e2dcache\u7684\u4e22\u5931\u548c\u547d\u4e2d\u3002\u5982\u679c\u9700\u8981\uff0c\u5b83\u8fd8\u80fd\u591f\u4e3a\u6211\u4eec\u63d0\u4f9bcache\u4e22\u5931\u6b21\u6570\uff0c\u5185\u5b58\u5f15\u7528\u6b21\u6570\uff0c\u4ee5\u53ca\u6bcf\u884c\u4ee3\u7801\uff0c\u6bcf\u4e2a\u51fd\u6570\uff0c\u6bcf\u4e2a\u6a21\u5757\uff0c\u6574\u4e2a\u7a0b\u5e8f\u4ea7\u751f\u7684\u6307\u4ee4\u6570\u3002\u8fd9\u5bf9\u4f18\u5316\u7a0b\u5e8f\u6709\u5f88\u5927\u7684\u5e2e\u52a9\u3002<\/p>\n<p>\u4f5c\u4e00\u4e0b\u5e7f\u544a\uff1avalgrind\u81ea\u8eab\u5229\u7528\u8be5\u5de5\u5177\u5728\u8fc7\u53bb\u51e0\u4e2a\u6708\u5185\u4f7f\u6027\u80fd\u63d0\u9ad8\u4e8625%-30%\u3002\u636e\u65e9\u5148\u62a5\u9053\uff0ckde\u7684\u5f00\u53d1team\u4e5f\u5bf9valgrind\u5728\u63d0\u9ad8kde\u6027\u80fd\u65b9\u9762\u7684\u5e2e\u52a9\u8868\u793a\u611f\u8c22\u3002<\/p>\n<p><strong>4.Helgrind<\/strong><\/p>\n<p>\u5b83\u4e3b\u8981\u7528\u6765\u68c0\u67e5\u591a\u7ebf\u7a0b\u7a0b\u5e8f\u4e2d\u51fa\u73b0\u7684\u7ade\u4e89\u95ee\u9898\u3002Helgrind\u5bfb\u627e\u5185\u5b58\u4e2d\u88ab\u591a\u4e2a\u7ebf\u7a0b\u8bbf\u95ee\uff0c\u800c\u53c8\u6ca1\u6709\u4e00\u8d2f\u52a0\u9501\u7684\u533a\u57df\uff0c\u8fd9\u4e9b\u533a\u57df\u5f80\u5f80\u662f\u7ebf\u7a0b\u4e4b\u95f4\u5931\u53bb\u540c\u6b65\u7684\u5730\u65b9\uff0c\u800c\u4e14\u4f1a\u5bfc\u81f4\u96be\u4ee5\u53d1\u6398\u7684\u9519\u8bef\u3002Helgrind\u5b9e\u73b0\u4e86\u540d\u4e3a\u201cEraser\u201d\u7684\u7ade\u4e89\u68c0\u6d4b\u7b97\u6cd5\uff0c\u5e76\u505a\u4e86\u8fdb\u4e00\u6b65\u6539\u8fdb\uff0c\u51cf\u5c11\u4e86\u62a5\u544a\u9519\u8bef\u7684\u6b21\u6570\u3002\u4e0d\u8fc7\uff0cHelgrind\u4ecd\u7136\u5904\u4e8e\u5b9e\u9a8c\u9636\u6bb5\u3002<\/p>\n<ol start=\"5\">\n<li><strong> Massif<\/strong><\/li>\n<\/ol>\n<p>\u5806\u6808\u5206\u6790\u5668\uff0c\u5b83\u80fd\u6d4b\u91cf\u7a0b\u5e8f\u5728\u5806\u6808\u4e2d\u4f7f\u7528\u4e86\u591a\u5c11\u5185\u5b58\uff0c\u544a\u8bc9\u6211\u4eec\u5806\u5757\uff0c\u5806\u7ba1\u7406\u5757\u548c\u6808\u7684\u5927\u5c0f\u3002Massif\u80fd\u5e2e\u52a9\u6211\u4eec\u51cf\u5c11\u5185\u5b58\u7684\u4f7f\u7528\uff0c\u5728\u5e26\u6709\u865a\u62df\u5185\u5b58\u7684\u73b0\u4ee3\u7cfb\u7edf\u4e2d\uff0c\u5b83\u8fd8\u80fd\u591f\u52a0\u901f\u6211\u4eec\u7a0b\u5e8f\u7684\u8fd0\u884c\uff0c\u51cf\u5c11\u7a0b\u5e8f\u505c\u7559\u5728\u4ea4\u6362\u533a\u4e2d\u7684\u51e0\u7387\u3002<\/p>\n<p>Massif\u5bf9\u5185\u5b58\u7684\u5206\u914d\u548c\u91ca\u653e\u505aprofile\u3002\u7a0b\u5e8f\u5f00\u53d1\u8005\u901a\u8fc7\u5b83\u53ef\u4ee5\u6df1\u5165\u4e86\u89e3\u7a0b\u5e8f\u7684\u5185\u5b58\u4f7f\u7528\u884c\u4e3a\uff0c\u4ece\u800c\u5bf9\u5185\u5b58\u4f7f\u7528\u8fdb\u884c\u4f18\u5316\u3002\u8fd9\u4e2a\u529f\u80fd\u5bf9C++\u5c24\u5176\u6709\u7528\uff0c\u56e0\u4e3aC++\u6709\u5f88\u591a\u9690\u85cf\u7684\u5185\u5b58\u5206\u914d\u548c\u91ca\u653e<\/p>\n<p>\u6b64\u5916\uff0clackey\u548cnulgrind\u4e5f\u4f1a\u63d0\u4f9b\u3002Lackey\u662f\u5c0f\u578b\u5de5\u5177\uff0c\u5f88\u5c11\u7528\u5230\uff1bNulgrind\u53ea\u662f\u4e3a\u5f00\u53d1\u8005\u5c55\u793a\u5982\u4f55\u521b\u5efa\u4e00\u4e2a\u5de5\u5177\u3002\u6211\u4eec\u5c31\u4e0d\u505a\u4ecb\u7ecd\u4e86\u3002<\/p>\n<p><strong>\u4e09<\/strong><strong>\u00a0<\/strong><strong>\u4f7f\u7528<\/strong><strong>Valgrind<\/strong><\/p>\n<p>Valgrind\u4f7f\u7528\u8d77\u6765\u975e\u5e38\u7b80\u5355\uff0c\u4f60\u751a\u81f3\u4e0d\u9700\u8981\u91cd\u65b0\u7f16\u8bd1\u4f60\u7684\u7a0b\u5e8f\u5c31\u53ef\u4ee5\u7528\u5b83\u3002\u5f53\u7136\u5982\u679c\u8981\u8fbe\u5230\u6700\u597d\u7684\u6548\u679c\uff0c\u83b7\u5f97\u6700\u51c6\u786e\u7684\u4fe1\u606f\uff0c\u8fd8\u662f\u9700\u8981\u6309\u8981\u6c42\u91cd\u65b0\u7f16\u8bd1\u4e00\u4e0b\u7684\u3002\u6bd4\u5982\u5728\u4f7f\u7528memcheck\u7684\u65f6\u5019\uff0c\u6700\u597d\u5173\u95ed\u4f18\u5316\u9009\u9879\u3002<\/p>\n<p>valgrind\u547d\u4ee4\u7684\u683c\u5f0f\u5982\u4e0b\uff1a<\/p>\n<p>valgrind\u00a0[valgrind-options] your-prog [your-prog options]<\/p>\n<p>\u4e00\u4e9b\u5e38\u7528\u7684\u9009\u9879\u5982\u4e0b\uff1a<\/p>\n<table>\n<thead>\n<tr>\n<td width=\"314\"><strong>\u9009\u9879<\/strong><\/td>\n<td width=\"312\"><strong>\u4f5c\u7528<\/strong><\/td>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td width=\"314\">-h &#8211;help<\/td>\n<td width=\"312\">\u663e\u793a\u5e2e\u52a9\u4fe1\u606f\u3002<\/td>\n<\/tr>\n<tr>\n<td width=\"314\">&#8211;version<\/td>\n<td width=\"312\">\u663e\u793avalgrind\u5185\u6838\u7684\u7248\u672c\uff0c\u6bcf\u4e2a\u5de5\u5177\u90fd\u6709\u5404\u81ea\u7684\u7248\u672c\u3002<\/td>\n<\/tr>\n<tr>\n<td width=\"314\">-q &#8211;quiet<\/td>\n<td width=\"312\">\u5b89\u9759\u5730\u8fd0\u884c\uff0c\u53ea\u6253\u5370\u9519\u8bef\u4fe1\u606f\u3002<\/td>\n<\/tr>\n<tr>\n<td width=\"314\">-v &#8211;verbose<\/td>\n<td width=\"312\">\u6253\u5370\u66f4\u8be6\u7ec6\u7684\u4fe1\u606f\u3002<\/td>\n<\/tr>\n<tr>\n<td width=\"314\">&#8211;tool=&lt;<em>toolname<\/em>&gt; [default: memcheck]<\/td>\n<td width=\"312\">\u6700\u5e38\u7528\u7684\u9009\u9879\u3002\u8fd0\u884cvalgrind\u4e2d\u540d\u4e3a<em>toolname<\/em>\u7684\u5de5\u5177\u3002\u5982\u679c\u7701\u7565\u5de5\u5177\u540d\uff0c\u9ed8\u8ba4\u8fd0\u884cmemcheck\u3002<\/td>\n<\/tr>\n<tr>\n<td width=\"314\">&#8211;db-attach=&lt;yes|no&gt; [default: no]<\/td>\n<td width=\"312\">\u7ed1\u5b9a\u5230\u8c03\u8bd5\u5668\u4e0a\uff0c\u4fbf\u4e8e\u8c03\u8bd5\u9519\u8bef\u3002<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>1\u3001\u68c0\u6d4b\u5185\u5b58\u6cc4\u6f0f<\/p>\n<p>\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<p>#include &lt;stdlib.h&gt;<\/p>\n<p>#include &lt;stdio.h&gt;<\/p>\n<p>int main(void)<\/p>\n<p>{<\/p>\n<p>char *ptr;<\/p>\n<p>ptr = (char *)malloc(10);<\/p>\n<p>return 0;<\/p>\n<p>}<\/p>\n<p>\u4fdd\u5b58\u4e3amemleak.c\u5e76\u7f16\u8bd1\uff0c\u7136\u540e\u7528valgrind\u68c0\u6d4b\u3002<\/p>\n<p>$ gcc -o memleak memleak.c<\/p>\n<p>\uff08valgrind\u548cpurify\u6700\u5927\u7684\u4e0d\u540c\u5728\u4e8e\uff1avalgrind\u53ea\u63a5\u7ba1\u7a0b\u5e8f\u6267\u884c\u7684\u8fc7\u7a0b\uff0c\u7f16\u8bd1\u65f6\u4e0d\u9700\u8981valgrind\u5e72\u9884\uff0c\u800cpurify\u4f1a\u5e72\u9884\u7a0b\u5e8f\u7f16\u8bd1\u8fc7\u7a0b\uff09<\/p>\n<p>$\u00a0valgrind\u00a0&#8211;tool=memcheck .\/memleak<\/p>\n<p>\u6211\u4eec\u5f97\u5230\u5982\u4e0b\u9519\u8bef\u4fe1\u606f\uff1a<\/p>\n<p>[konten@tencent test_valgrind]$ valgrind\u00a0.\/memleak<\/p>\n<p>==29646== Memcheck, a memory error detector.<\/p>\n<p>==29646== Copyright (C) 2002-2007, and GNU GPL&#8217;d, by Julian Seward et al.<\/p>\n<p>==29646== Using LibVEX rev 1732, a library for dynamic binary translation.<\/p>\n<p>==29646== Copyright (C) 2004-2007, and GNU GPL&#8217;d, by OpenWorks LLP.<\/p>\n<p>==29646== Using valgrind-3.2.3, a dynamic binary instrumentation framework.<\/p>\n<p>==29646== Copyright (C) 2000-2007, and GNU GPL&#8217;d, by Julian Seward et al.<\/p>\n<p>==29646== For more details, rerun with: -v<\/p>\n<p>==29646==<\/p>\n<p>==29646==<\/p>\n<p>==29646== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 15 from 1)<\/p>\n<p>==29646== malloc\/free: in use at exit: 10 bytes in 1 blocks.\u00a0\u00a0\u00a0\/\/\u6307\u793a\u5728\u7a0b\u5e8f\u9000\u51fa\u65f6\uff0c\u8fd8\u6709\u591a\u5c11\u5185\u5b58\u6ca1\u6709\u91ca\u653e\u3002<\/p>\n<p>==29646== malloc\/free: 1 allocs, 0 frees, 10 bytes allocated.\u00a0\/\/\u00a0\u6307\u793a\u8be5\u6267\u884c\u8fc7\u7a0bmalloc\u548cfree\u8c03\u7528\u7684\u6b21\u6570\u3002<\/p>\n<p>==29646== For counts of detected errors, rerun with: -v\u00a0\/\/\u00a0\u63d0\u793a\u5982\u679c\u8981\u66f4\u8be6\u7ec6\u7684\u4fe1\u606f\uff0c\u7528-v\u9009\u9879\u3002<\/p>\n<p>==29646== searching for pointers to 1 not-freed blocks.<\/p>\n<p>==29646== checked 56,164 bytes.<\/p>\n<p>==29646==<\/p>\n<p>==29646== LEAK SUMMARY:<\/p>\n<p>==29646==\u00a0\u00a0\u00a0\u00a0definitely lost: 10 bytes in 1 blocks.<\/p>\n<p>==29646==\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0possibly lost: 0 bytes in 0 blocks.<\/p>\n<p>==29646==\u00a0\u00a0\u00a0\u00a0still reachable: 0 bytes in 0 blocks.<\/p>\n<p>==29646==\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0suppressed: 0 bytes in 0 blocks.<\/p>\n<p>==29646== Rerun with &#8211;leak-check=full to see details of leaked memory.<\/p>\n<p>[konten@tencent test_valgrind]$<\/p>\n<p>\u4ee5\u4e0a\u7ed3\u679c\u4e2d\uff0c\u7ea2\u8272\u7684\u662f\u624b\u5de5\u6dfb\u52a0\u7684\u8bf4\u660e\u4fe1\u606f\uff0c\u5176\u4ed6\u662fvalgrind\u7684\u8f93\u51fa\u3002\u53ef\u4ee5\u770b\u5230\uff0c\u5982\u679c\u6211\u4eec\u4ec5\u4ec5\u7528\u9ed8\u8ba4\u65b9\u5f0f\u6267\u884c\uff0cvalgrind\u53ea\u62a5\u544a\u5185\u5b58\u6cc4\u6f0f\uff0c\u4f46\u6ca1\u6709\u663e\u793a\u5177\u4f53\u4ee3\u7801\u4e2d\u6cc4\u6f0f\u7684\u5730\u65b9\u3002<\/p>\n<p>\u56e0\u6b64\u6211\u4eec\u9700\u8981\u4f7f\u7528\u00a0\u201c&#8211;leak-check=full\u201d\u9009\u9879\u542f\u52a8\u00a0valgrind\uff0c\u6211\u4eec\u518d\u6267\u884c\u4e00\u6b21\uff1a<\/p>\n<p>[konten@tencent test_valgrind]$ valgrind &#8211;leak-check=full .\/memleak<\/p>\n<p>==29661== Memcheck, a memory error detector.<\/p>\n<p>==29661== Copyright (C) 2002-2007, and GNU GPL&#8217;d, by Julian Seward et al.<\/p>\n<p>==29661== Using LibVEX rev 1732, a library for dynamic binary translation.<\/p>\n<p>==29661== Copyright (C) 2004-2007, and GNU GPL&#8217;d, by OpenWorks LLP.<\/p>\n<p>==29661== Using valgrind-3.2.3, a dynamic binary instrumentation framework.<\/p>\n<p>==29661== Copyright (C) 2000-2007, and GNU GPL&#8217;d, by Julian Seward et al.<\/p>\n<p>==29661== For more details, rerun with: -v<\/p>\n<p>==29661==<\/p>\n<p>==29661==<\/p>\n<p>==29661== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 15 from 1)<\/p>\n<p>==29661== malloc\/free: in use at exit: 10 bytes in 1 blocks.<\/p>\n<p>==29661== malloc\/free: 1 allocs, 0 frees, 10 bytes allocated.<\/p>\n<p>==29661== For counts of detected errors, rerun with: -v<\/p>\n<p>==29661== searching for pointers to 1 not-freed blocks.<\/p>\n<p>==29661== checked 56,164 bytes.<\/p>\n<p>==29661==<\/p>\n<p>==29661== 10 bytes in 1 blocks are definitely lost in loss record 1 of 1<\/p>\n<p>==29661==\u00a0\u00a0\u00a0\u00a0at 0x401A846: malloc (vg_replace_malloc.c:149)<\/p>\n<p>==29661==\u00a0\u00a0\u00a0\u00a0by 0x804835D: main (memleak.c:6)<\/p>\n<p>==29661==<\/p>\n<p>==29661== LEAK SUMMARY:<\/p>\n<p>==29661==\u00a0\u00a0\u00a0\u00a0definitely lost: 10 bytes in 1 blocks.<\/p>\n<p>==29661==\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0possibly lost: 0 bytes in 0 blocks.<\/p>\n<p>==29661==\u00a0\u00a0\u00a0\u00a0still reachable: 0 bytes in 0 blocks.<\/p>\n<p>==29661==\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0suppressed: 0 bytes in 0 blocks.<\/p>\n<p>[konten@tencent test_valgrind]$<\/p>\n<p>\u548c\u4e0a\u6b21\u7684\u6267\u884c\u7ed3\u679c\u57fa\u672c\u76f8\u540c\uff0c\u53ea\u662f\u591a\u4e86\u4e0a\u9762\u84dd\u8272\u7684\u90e8\u5206\uff0c\u6307\u660e\u4e86\u4ee3\u7801\u4e2d\u51fa\u73b0\u6cc4\u6f0f\u7684\u5177\u4f53\u4f4d\u7f6e\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u7528valgrind\u68c0\u67e5\u5185\u5b58\u6cc4\u6f0f\u7684\u65b9\u6cd5\uff0c\u7528\u5230\u7684\u4f8b\u5b50\u6bd4\u8f83\u7b80\u5355\uff0c\u590d\u6742\u7684\u4ee3\u7801\u6700\u540e\u7ed3\u679c\u4e5f\u90fd\u4e00\u6837\u3002<\/p>\n<table>\n<tbody>\n<tr>\n<td>2\u3001\u5176\u4ed6\u5185\u5b58\u95ee\u9898<\/p>\n<p>\u6211\u4eec\u4e0b\u9762\u7684\u4f8b\u5b50\u4e2d\u5305\u62ec\u5e38\u89c1\u7684\u51e0\u7c7b\u5185\u5b58\u95ee\u9898\uff1a\u5806\u4e2d\u7684\u5185\u5b58\u8d8a\u754c\u3001\u8e29\u5185\u5b58\u3001\u6808\u4e2d\u7684\u5185\u5b58\u8d8a\u754c\u3001\u975e\u6cd5\u6307\u9488\u4f7f\u7528\u3001\u91cd\u590dfree\u3002<\/p>\n<p>#include &lt;stdlib.h&gt;<\/p>\n<p>#include &lt;stdio.h&gt;<\/p>\n<p>int main(void)<\/p>\n<p>{<\/p>\n<p>char *ptr = malloc(10);<\/p>\n<p>ptr[12] = &#8216;a&#8217;; \/\/\u00a0\u5185\u5b58\u8d8a\u754c<\/p>\n<p>memcpy(ptr +1, ptr, 5); \/\/\u00a0\u8e29\u5185\u5b58<\/p>\n<p>char a[10];<\/p>\n<p>a[12] = &#8216;i&#8217;;\u00a0\/\/\u00a0\u6570\u7ec4\u8d8a\u754c<\/p>\n<p>free(ptr);\u00a0\/\/\u00a0\u91cd\u590d\u91ca\u653e<\/p>\n<p>free(ptr);<\/p>\n<p>char *p1;<\/p>\n<p>*p1 = &#8216;1&#8217;;\u00a0\/\/\u00a0\u975e\u6cd5\u6307\u9488<\/p>\n<p>&nbsp;<\/p>\n<p>return 0;<\/p>\n<p>}<\/p>\n<p>\u7f16\u8bd1\uff1a\u00a0gcc -o invalidptr invalidptr.c -g<\/p>\n<p>\u6267\u884c\uff1avalgrind &#8211;leak-check=full .\/invalidptr<\/p>\n<p>\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n<p>[konten@tencent test_valgrind]$ valgrind &#8211;leak-check=full .\/invalidptr<\/p>\n<p>==29776== Memcheck, a memory error detector.<\/p>\n<p>==29776== Copyright (C) 2002-2007, and GNU GPL&#8217;d, by Julian Seward et al.<\/p>\n<p>==29776== Using LibVEX rev 1732, a library for dynamic binary translation.<\/p>\n<p>==29776== Copyright (C) 2004-2007, and GNU GPL&#8217;d, by OpenWorks LLP.<\/p>\n<p>==29776== Using valgrind-3.2.3, a dynamic binary instrumentation framework.<\/p>\n<p>==29776== Copyright (C) 2000-2007, and GNU GPL&#8217;d, by Julian Seward et al.<\/p>\n<p>==29776== For more details, rerun with: -v<\/p>\n<p>==29776==<\/p>\n<p>==29776== Invalid write of size 1\u00a0\/\/\u5806\u5185\u5b58\u8d8a\u754c\u88ab\u67e5\u51fa\u6765<\/p>\n<p>==29776==\u00a0\u00a0\u00a0\u00a0at 0x80483D2: main (invalidptr.c:7)<\/p>\n<p>==29776==\u00a0Address 0x4159034 is 2 bytes after a block of size 10 alloc&#8217;d<\/p>\n<p>==29776==\u00a0\u00a0\u00a0\u00a0at 0x401A846: malloc (vg_replace_malloc.c:149)<\/p>\n<p>==29776==\u00a0\u00a0\u00a0\u00a0by 0x80483C5: main (invalidptr.c:6)<\/p>\n<p>==29776==<\/p>\n<p>==29776== Source and destination overlap in memcpy(0x4159029, 0x4159028, 5)\u00a0\/\/\u8e29\u5185\u5b58<\/p>\n<p>==29776==\u00a0\u00a0\u00a0\u00a0at 0x401C96D: memcpy (mc_replace_strmem.c:116)<\/p>\n<p>==29776==\u00a0\u00a0\u00a0\u00a0by 0x80483E6: main (invalidptr.c:9)<\/p>\n<p>==29776==<\/p>\n<p>==29776== Invalid free() \/ delete \/ delete[]\u00a0\/\/\u91cd\u590d\u91ca\u653e<\/p>\n<p>==29776==\u00a0\u00a0\u00a0\u00a0at 0x401B3FB: free (vg_replace_malloc.c:233)<\/p>\n<p>==29776==\u00a0\u00a0\u00a0\u00a0by 0x8048406: main (invalidptr.c:16)<\/p>\n<p>==29776==\u00a0Address 0x4159028 is 0 bytes inside a block of size 10 free&#8217;d<\/p>\n<p>==29776==\u00a0\u00a0\u00a0\u00a0at 0x401B3FB: free (vg_replace_malloc.c:233)<\/p>\n<p>==29776==\u00a0\u00a0\u00a0\u00a0by 0x80483F8: main (invalidptr.c:15)<\/p>\n<p>==29776==<\/p>\n<p>==29776== Use of uninitialised value of size 4<\/p>\n<p>==29776==\u00a0\u00a0\u00a0\u00a0at 0x804840D: main (invalidptr.c:19)<\/p>\n<p>==29776==\u00a0\/\/\u975e\u6cd5\u6307\u9488\uff0c\u5bfc\u81f4coredump<\/p>\n<p>==29776== Process terminating with default action of signal 11 (SIGSEGV): dumping core<\/p>\n<p>==29776==\u00a0Bad permissions for mapped region at address 0x80482AD<\/p>\n<p>==29776==\u00a0\u00a0\u00a0\u00a0at 0x804840D: main (invalidptr.c:19)<\/p>\n<p>==29776==<\/p>\n<p>==29776== ERROR SUMMARY: 4 errors from 4 contexts (suppressed: 15 from 1)<\/p>\n<p>==29776== malloc\/free: in use at exit: 0 bytes in 0 blocks.<\/p>\n<p>==29776== malloc\/free: 1 allocs, 2 frees, 10 bytes allocated.<\/p>\n<p>==29776== For counts of detected errors, rerun with: -v<\/p>\n<p>==29776== All heap blocks were freed &#8212; no leaks are possible.<\/p>\n<p>Segmentation fault<\/p>\n<p>[konten@tencent test_valgrind]$<\/p>\n<p>\u4ece\u4e0a\u9762\u7684\u7ed3\u679c\u770b\u51fa\uff0c\u9664\u4e86\u6808\u5185\u5b58\u8d8a\u754c\u5916\uff0c\u5176\u4ed6\u5e38\u89c1\u7684\u5185\u5b58\u95ee\u9898\u90fd\u53ef\u4ee5\u7528valgrind\u7b80\u5355\u7684\u67e5\u51fa\u6765\u3002<\/p>\n<p>3\u3001\u663e\u793a\u4ee3\u7801\u8986\u76d6<\/p>\n<p>\u7528callgrind\u5de5\u5177\u80fd\u65b9\u4fbf\u7684\u663e\u793a\u7a0b\u5e8f\u6267\u884c\u7684\u4ee3\u7801\u8986\u76d6\u60c5\u51b5\u3002<\/p>\n<p>\u770b\u5982\u4e0b\u4f8b\u5b50\uff1a<\/p>\n<p>&nbsp;<\/p>\n<p>4\u3001\u663e\u793a\u7ebf\u7a0b\u7ade\u6001\u6761\u4ef6\u00a0&lt;\u8be5\u7248\u672c\u6682\u4e0d\u652f\u6301&gt;<\/p>\n<p>\u7528helgrind\u5de5\u5177\u53ef\u4ee5\u5728\u591a\u7ebf\u7a0b\u4ee3\u7801\u4e2d\u627e\u5230\u53ef\u80fd\u4ea7\u751f\u7ade\u6001\u6761\u4ef6\u7684\u5730\u65b9\u3002<\/p>\n<p><strong>\u56db<\/strong><strong>\u00a0memcheck\u00a0<\/strong><strong>\u5de5\u5177\u7684\u5e38\u7528\u9009\u578b<\/strong><\/p>\n<p>1\u3001leak-check<\/p>\n<p>&#8211;leak-check=&lt;no|summary|yes|full&gt; [default: summary]<\/p>\n<p>\u7528\u4e8e\u63a7\u5236\u5185\u5b58\u6cc4\u6f0f\u68c0\u6d4b\u529b\u5ea6\u3002<\/p>\n<p>no\uff0c\u4e0d\u68c0\u6d4b\u5185\u5b58\u6cc4\u6f0f\uff1b<\/p>\n<p>summary\uff0c\u4ec5\u62a5\u544a\u603b\u5171\u6cc4\u6f0f\u7684\u6570\u91cf\uff0c\u4e0d\u62a5\u544a\u5177\u4f53\u6cc4\u6f0f\u4f4d\u7f6e\uff1b<\/p>\n<p>yes\/full\uff0c\u62a5\u544a\u6cc4\u6f0f\u603b\u6570\u3001\u6cc4\u6f0f\u7684\u5177\u4f53\u4f4d\u7f6e\u3002<\/p>\n<p>2\u3001show-reachable<\/p>\n<p>&#8211;show-reachable=&lt;yes|no&gt; [default: no]<\/p>\n<p>\u7528\u4e8e\u63a7\u5236\u662f\u5426\u68c0\u6d4b\u63a7\u5236\u8303\u56f4\u4e4b\u5916\u7684\u6cc4\u6f0f\uff0c\u6bd4\u5982\u5168\u5c40\u6307\u9488\u3001static\u6307\u9488\u7b49\u3002<\/p>\n<p>#include &lt;stdlib.h&gt;<\/p>\n<p>#include &lt;stdio.h&gt;<\/p>\n<p>\/\/char *gptr = NULL;<\/p>\n<p>int main(void)<\/p>\n<p>{<\/p>\n<p>gptr = (char *)malloc(10);<\/p>\n<p>return 0;<\/p>\n<p>}<\/p>\n<p>\u5bf9\u5e94\u4ee5\u4e0a\u4ee3\u7801\uff0c\u82e5&#8211;show-reachable\u4e3ano\uff0c\u5219valgrind\u4e0d\u62a5\u544a\u5185\u5b58\u6cc4\u6f0f\uff0c\u5426\u5219\u4f1a\u62a5\u544a\u3002<\/p>\n<p>3\u3001undef-value-errors<\/p>\n<p>&#8211;undef-value-errors=&lt;yes|no&gt; [default: yes]<\/p>\n<p>\u7528\u4e8e\u63a7\u5236\u662f\u5426\u68c0\u6d4b\u4ee3\u7801\u4e2d\u4f7f\u7528\u672a\u521d\u59cb\u5316\u53d8\u91cf\u7684\u60c5\u51b5\u3002<\/p>\n<p>\u5bf9\u5e94\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n<p>int a;<\/p>\n<p>printf(&#8220;a = %d \\n&#8221;, a);<\/p>\n<p>\u82e5\u00a0&#8211;undef-value-errors\uff1dno\uff0c\u5219valgrind\u4e0d\u62a5\u544a\u9519\u8bef\uff0c\u5426\u5219\u62a5\u544a\u201cUse of uninitialised value &#8230;\u201d\u7684\u9519\u8bef\u3002<\/p>\n<p>4\u3001\u5176\u4ed6\u9009\u9879<\/p>\n<p>&#8211;log-file=filename\u00a0\u5c06\u7ed3\u679c\u8f93\u51fa\u5230\u6587\u4ef6\u3002<\/p>\n<p>&#8211;log-socket=192.168.0.1:12345\u00a0\u8f93\u51fa\u5230\u7f51\u7edc\u3002<\/p>\n<p>&#8211;trace-children=&lt;yes|no&gt; [default: no]<\/p>\n<p>&#8211;track-fds=&lt;yes|no&gt; [default: no]<\/p>\n<p>&#8211;log-fd=&lt;number&gt; [default: 2, stderr]<\/p>\n<p>&#8211;xml=&lt;yes|no&gt; [default: no]<\/p>\n<p>&#8211;num-callers=&lt;number&gt; [default: 12]<\/p>\n<p>&#8211;show-below-main=&lt;yes|no&gt; [default: no]<\/p>\n<p><strong>\u4e94<\/strong><strong>\u00a0Valgrind<\/strong><strong>\u7684\u7f16\u8bd1\u5b89\u88c5<\/strong><\/p>\n<p>1\u3001\u4e0b\u8f7d\u6e90\u4ee3\u7801\uff0c\u4e0b\u8f7d\u5730\u5740http:\/\/valgrind.org\/downloads\/current.html#current\u00a0\uff0c\u622a\u6b62\u76ee\u524d\u4e3a\u6b62\uff0c\u6700\u65b0\u7248\u672c\u662f3.2.3<\/p>\n<p>2\u3001\u7f16\u8bd1\uff0c\u5728\u6e90\u4ee3\u7801\u76ee\u5f55\u4e0b\u6267\u884c\uff1a<\/p>\n<p>.\/configure &#8211;prefix=[\u4f60\u81ea\u5df1\u7684\u5b89\u88c5\u76ee\u5f55]<\/p>\n<p>make;make install<\/p>\n<p>\u4fbf\u597d\u4e86\u3002<\/p>\n<p>3\u3001\u914d\u7f6e\u7f3a\u7701\u9009\u9879<\/p>\n<p>valgrind\u63d0\u4f9b3\u79cd\u65b9\u5f0f\u7528\u4e8e\u8bbe\u7f6e\u7f3a\u7701\u9009\u9879\uff1a<\/p>\n<p>a\u3001~\/.valgrindrc\u6587\u4ef6\uff1b<\/p>\n<p>b\u3001\u73af\u5883\u53d8\u91cf$VALGRIND_OPTS\uff1b<\/p>\n<p>c\u3001\u5f53\u524d\u76ee\u5f55\u4e0b\u7684.valgrindrc\u6587\u4ef6\uff1b<\/p>\n<p>\u4f18\u5148\u987a\u5e8f\u4e3a\u00a0a\u3001b\u3001c<\/p>\n<p>.valgrindrc\u7684\u683c\u5f0f\u4e3a\uff1a<\/p>\n<p>&#8211;ToolName:OptionName=OptionVal<\/p>\n<p>\u5982\uff1a<\/p>\n<p>&#8211;memcheck:leak-check=yes<\/p>\n<p>&#8211;memcheck:show-reachable=yes<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u5b8c<\/p>\n<p>\u5206\u7c7b:\u00a0<a href=\"http:\/\/www.cnblogs.com\/wangkangluo1\/category\/304181.html\">valgrind<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Valgrind\u4f7f\u7528\u8bf4\u660e Valgrin<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"colormag_page_container_layout":"default_layout","colormag_page_sidebar_layout":"default_layout","footnotes":""},"categories":[],"tags":[118],"class_list":["post-1310","post","type-post","status-publish","format-standard","hentry","tag-02-"],"_links":{"self":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/posts\/1310","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/comments?post=1310"}],"version-history":[{"count":0,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/posts\/1310\/revisions"}],"wp:attachment":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/media?parent=1310"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/categories?post=1310"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/tags?post=1310"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}