{"id":164,"date":"2011-03-10T03:44:37","date_gmt":"2011-03-10T11:44:37","guid":{"rendered":"http:\/\/zhanxw.com\/blog\/?p=164"},"modified":"2011-05-31T04:26:36","modified_gmt":"2011-05-31T08:26:36","slug":"gdb-%e4%bd%bf%e7%94%a8%e7%bb%8f%e9%aa%8c","status":"publish","type":"post","link":"https:\/\/zhanxw.com\/blog\/2011\/03\/gdb-%e4%bd%bf%e7%94%a8%e7%bb%8f%e9%aa%8c\/","title":{"rendered":"GDB \u4f7f\u7528\u7ecf\u9a8c"},"content":{"rendered":"<p>\u672c\u6587\u4ecb\u7ecd\u4e00\u4e9b\u5e38\u7528\u7684GDB\u6280\u5de7<\/p>\n<p>&nbsp;<\/p>\n<h2>\uff081\uff09\u4f7f\u7528.gdbinit<\/h2>\n<p>\u5728.gdbinit\u91cc\u53ef\u4ee5\u4f7f\u7528 define \u547d\u4ee4\uff0c\u6765\u7b80\u5316\u590d\u6742\u547d\u4ee4\u7684\u8f93\u5165\uff0c\u4f8b\u5982\u91cd\u5b9a\u5411stdout, stderr\uff1a<br \/>\ndef  redirect<br \/>\ncall (void)close(1)<br \/>\ncall (void)close(2)<br \/>\ncall (int)open($arg0, 2)<br \/>\ncall (int)open($arg0, 2)<br \/>\nend<\/p>\n<p>\u4e4b\u540e\u5c31\u53ef\u4ee5\u7528<\/p>\n<p>(gdb)redirect(&#8220;\/dev\/ttyp3&#8221;)<\/p>\n<p>\u6765\u628a\u8f93\u5165\u8f93\u51fa\u91cd\u5b9a\u5411\u5230tty3 \uff08\u8fd9\u4e2a\/dev\/tty3\u53ef\u4ee5\u7528 shell tty\u547d\u4ee4\u83b7\u5f97\uff09<br \/>\n\u8fd9\u4e2a\u4f8b\u5b50\u9009\u81eahttp:\/\/blogold.chinaunix.net\/u3\/111274\/showart_2162256.html<\/p>\n<p>\u6b64\u5916\uff0c\u8fd8\u53ef\u4ee5\u7ed9\u8fd9\u4e2a\u522b\u540d\u52a0\u4e0a\u5e2e\u52a9\u8bf4\u660e\u6027\u6587\u5b57\uff0c\u683c\u5f0f\u4e3a\uff1a<br \/>\ndocument user-defined-command<br \/>\n\u5e2e\u52a9\u8bf4\u660e\u6027\u6587\u5b57<br \/>\nend<\/p>\n<p>\u4f8b\u5982\uff1a<br \/>\ndocument redirect<br \/>\nredirect stdout and stderr to arg0<br \/>\nend<\/p>\n<p>.gdbinit\u4e2d\u8fd8\u53ef\u4ee5\u5b9a\u4e49hook\u51fd\u6570\uff08http:\/\/dev.firnow.com\/course\/6_system\/linux\/linuxjq\/20090307\/159416.html\uff09\uff0c<br \/>\n\u4f8b\u5982\u60f3\u5728print\u547d\u4ee4\u524d\u663e\u793a\u4e00\u6bb5\u201c&#8212;&#8212;&#8212;-\u201d\uff0c\uff1a<br \/>\ndefine hook-print<br \/>\necho &#8212;&#8212;&#8212;-\\n<br \/>\nend<\/p>\n<p>.gdbinit\u4f1a\u88abGDB\u9ed8\u8ba4\u8bfb\u53d6\uff0c\u5982\u679c\u6709\u7279\u5b9a\u7684\u914d\u7f6e\u6587\u4ef6\uff0c\u53ef\u4ee5\u7528source\u547d\u4ee4\uff08\u7c7b\u4f3cbash\u7684source\u547d\u4ee4\uff09<\/p>\n<h2>\uff082\uff09\u8865\u5145\u4e00\u4e9b\u6709\u7528\u7684\u547d\u4ee4<\/h2>\n<p>\u7528whatis\u547d\u4ee4\u68c0\u67e5\u53d8\u91cf\u7684\u7c7b\u578b\uff0c<br \/>\nptype:\u6bd4whatis\u7684\u529f\u80fd\u66f4\u5f3a\uff0c\u4ed6\u53ef\u4ee5\u63d0\u4f9b\u4e00\u4e2a\u7ed3\u6784\u7684\u5b9a\u4e49\uff0c<br \/>\ncommands \u547d\u4e2d\u65ad\u70b9\u65f6\uff0c\u5217\u51fa\u5c06\u8981\u6267\u884c\u7684\u547d\u4ee4<br \/>\n\u4f7f\u7528\u201crb\u201d\u547d\u4ee4\uff0c\u5982\u679c\u6267\u884c\u201crb\u201d\u65f6\u4e0d\u5e26\u53c2\u6570\uff0c\u5219\u8868\u793a\u5728\u6240\u6709\u51fd\u6570\u5904\u6253\u4e00\u4e2a\u65ad\u70b9\uff0c\u201crb\u201d\u540e\u9762\u53ef\u4ee5\u63a5\u4e00\u4e2a\u7b26\u5408\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u53c2\u6570\uff0c\u7528\u6765\u5bf9\u7b26\u5408\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u6240\u6709\u51fd\u6570\u6253\u65ad\u70b9<br \/>\ninfo break \u663e\u793a\u5f53\u524d\u65ad\u70b9\u6e05\u5355\uff0c\u5305\u62ec\u5230\u8fbe\u65ad\u70b9\u5904\u7684\u6b21\u6570\u7b49\u3002<br \/>\ninfo files \u663e\u793a\u88ab\u8c03\u8bd5\u6587\u4ef6\u7684\u8be6\u7ec6\u4fe1\u606f\u3002<br \/>\ninfo func \u663e\u793a\u6240\u6709\u7684\u51fd\u6570\u540d\u79f0\u3002<br \/>\ninfo local \u663e\u793a\u5f53\u51fd\u6570\u4e2d\u7684\u5c40\u90e8\u53d8\u91cf\u4fe1\u606f\u3002<br \/>\ninfo prog \u663e\u793a\u88ab\u8c03\u8bd5\u7a0b\u5e8f\u7684\u6267\u884c\u72b6\u6001\u3002<br \/>\ninfo var \u663e\u793a\u6240\u6709\u7684\u5168\u5c40\u548c\u9759\u6001\u53d8\u91cf\u540d\u79f0\u3002<br \/>\nrwatch \u5f53\u8868\u8fbe\u5f0f\uff08\u53d8\u91cf\uff09expr\u88ab\u8bfb\u65f6\uff0c\u505c\u4f4f\u7a0b\u5e8f\u3002<br \/>\nawatch \u5f53\u8868\u8fbe\u5f0f\uff08\u53d8\u91cf\uff09\u7684\u503c\u88ab\u8bfb\u6216\u88ab\u5199\u65f6\uff0c\u505c\u4f4f\u7a0b\u5e8f\u3002<br \/>\nsave breakpoints &#8212; Save current breakpoint definitions as a script<br \/>\nc [count]  \u8868\u793a\u8fde\u7eed(c)ontinue \u591a\u6b21<\/p>\n<p>from: http:\/\/www.cppblog.com\/true\/archive\/2009\/01\/11\/71749.html<\/p>\n<h2>\uff083\uff09print\u7684\u4e24\u4e2a\u6280\u5de7<\/h2>\n<p>(gdb)print $1 ($1\u4e3a\u5386\u53f2\u8bb0\u5f55\u53d8\u91cf,\u5728\u4ee5\u540e\u53ef\u4ee5\u76f4\u63a5\u5f15\u7528 $1 \u7684\u503c)<br \/>\nl \u4eba\u4e3a\u6570\u7ec4<br \/>\n\u4eba\u4e3a\u6570\u7ec4\u63d0\u4f9b\u4e86\u4e00\u79cd\u53bb\u663e\u793a\u5b58\u50a8\u5668\u5757\uff08\u6570\u7ec4\u8282\u6216\u52a8\u6001\u5206\u914d\u7684\u5b58\u50a8\u533a\uff09\u5185\u5bb9\u7684\u65b9\u6cd5\u3002\u65e9\u671f\u7684\u8c03\u8bd5\u7a0b\u5e8f\u6ca1\u6709\u5f88\u597d\u7684\u65b9\u6cd5\u5c06\u4efb\u610f\u7684\u6307\u9488\u6362\u6210\u4e00\u4e2a\u6570\u7ec4\u3002\u5c31\u50cf\u5bf9\u5f85\u53c2\u6570\u4e00\u6837\uff0c\u8ba9\u6211\u4eec\u67e5\u770b\u5185\u5b58\u4e2d\u5728\u53d8\u91cfh\u540e\u9762\u768410\u4e2a\u6574\u6570\uff0c\u4e00\u4e2a\u52a8\u6001\u6570\u7ec4\u7684\u8bed\u6cd5\u5982\u4e0b\u6240\u793a\uff1a<br \/>\nbase@length<br \/>\n\u56e0\u6b64\uff0c\u8981\u60f3\u663e\u793a\u5728h\u540e\u9762\u768410\u4e2a\u5143\u7d20\uff0c\u53ef\u4ee5\u4f7f\u7528h@10\uff1a<br \/>\n(gdb)print h@10<br \/>\n$13=(-1,345,23,-234,0,0,0,98,345,10)<\/p>\n<p>http:\/\/moonwater.blogbus.com\/logs\/2256694.html<\/p>\n<h2><\/h2>\n<h2>\uff084\uff09 GDB \u5176\u4ed6\u5e94\u7528\uff1a\u663e\u793aSTL\u91cc\u7684\u6570\u636e\u7ed3\u6784\uff1b\u753b\u56fe\u7b49\u7b49<\/h2>\n<p>All from GDB wiki<br \/>\n\u7f8e\u89c2\u7684\u663e\u793aSTL\u6570\u636e\u7ed3\u6784\uff0c\u6bd4\u5982std::string, std::vector&#8230;<br \/>\nhttp:\/\/sourceware.org\/gdb\/wiki\/STLSupport<\/p>\n<p>\u753bone-dimensinoal\u56fe\uff1a<br \/>\nhttp:\/\/sourceware.org\/gdb\/wiki\/PlottingFromGDB<\/p>\n<p>\u5176\u4ed6\u6709\u610f\u601d\u7684extension\uff1a<br \/>\n\u6bd4\u5982\u68c0\u67e5libc\u7684heap\uff1b\u7f8e\u89c2\u7684\u8c03\u8bd5wxWidgets\u4e2d\u7684\u53d8\u91cf\u7b49\u7b49\uff1a<br \/>\nhttp:\/\/sourceware.org\/gdb\/wiki\/HomePage<\/p>\n<h2>\uff085\uff09GDB\u73af\u5883\u53d8\u91cf<\/h2>\n<p>(\u9648\u7693\u4e13\u680f\u3000\u3010\u7a7a\u8c37\u5e7d\u5170\uff0c\u5fc3\u5982\u7693\u6708\u3011 http:\/\/blog.csdn.net\/haoel\/archive\/2003\/07\/02\/2879.aspx)<br \/>\n\u4f60\u53ef\u4ee5\u5728GDB\u7684\u8c03\u8bd5\u73af\u5883\u4e2d\u5b9a\u4e49\u81ea\u5df1\u7684\u53d8\u91cf\uff0c\u7528\u6765\u4fdd\u5b58\u4e00\u4e9b\u8c03\u8bd5\u7a0b\u5e8f\u4e2d\u7684\u8fd0\u884c\u6570\u636e\u3002\u8981\u5b9a<br \/>\n\u4e49\u4e00\u4e2aGDB\u7684\u53d8\u91cf\u5f88\u7b80\u5355\u53ea\u9700\u3002\u4f7f\u7528GDB\u7684set\u547d\u4ee4\u3002GDB\u7684\u73af\u5883\u53d8\u91cf\u548cUNIX\u4e00\u6837\uff0c<br \/>\n\u4e5f\u662f\u4ee5$\u8d77\u5934\u3002\u5982\uff1a<br \/>\nset $foo = *object_ptr<br \/>\n\u4f7f\u7528\u73af\u5883\u53d8\u91cf\u65f6\uff0cGDB\u4f1a\u5728\u4f60\u7b2c\u4e00\u6b21\u4f7f\u7528\u65f6\u521b\u5efa\u8fd9\u4e2a\u53d8\u91cf\uff0c\u800c\u5728\u4ee5\u540e\u7684\u4f7f\u7528\u4e2d\uff0c\u5219\u76f4\u63a5\u5bf9<br \/>\n\u5176\u8ce6\u503c\u3002\u73af\u5883\u53d8\u91cf\u6ca1\u6709\u7c7b\u578b\uff0c\u4f60\u53ef\u4ee5\u7ed9\u73af\u5883\u53d8\u91cf\u5b9a\u4e49\u4efb\u4e00\u7684\u7c7b\u578b\u3002\u5305\u62ec\u7ed3\u6784\u4f53\u548c\u6570\u7ec4\u3002<br \/>\nshow convenience<br \/>\n\u8be5\u547d\u4ee4\u67e5\u770b\u5f53\u524d\u6240\u8bbe\u7f6e\u7684\u6240\u6709\u7684\u73af\u5883\u53d8\u91cf\u3002<br \/>\n\u8fd9\u662f\u4e00\u4e2a\u6bd4\u8f83\u5f3a\u5927\u7684\u529f\u80fd\uff0c\u73af\u5883\u53d8\u91cf\u548c\u7a0b\u5e8f\u53d8\u91cf\u7684\u4ea4\u4e92\u4f7f\u7528\uff0c\u5c06\u4f7f\u5f97\u7a0b\u5e8f\u8c03\u8bd5\u66f4\u4e3a\u7075\u6d3b\u4fbf\u6377\u3002<br \/>\n\u4f8b\u5982\uff1a<br \/>\nset $i = 0<br \/>\nprint bar[$i++]-&gt;contents<br \/>\n\u4e8e\u662f\uff0c\u5f53\u4f60\u5c31\u4e0d\u5fc5\uff0cprint bar[0]-&gt;contents, print bar[1]-&gt;contents\u5730\u8f93\u5165\u547d\u4ee4\u4e86\u3002\u8f93\u5165\u8fd9\u6837\u7684<br \/>\n\u547d\u4ee4\u540e\uff0c\u53ea\u7528\u6572\u56de\u8f66\uff0c\u91cd\u590d\u6267\u884c\u4e0a\u4e00\u6761\u8bed\u53e5\uff0c\u73af\u5883\u53d8\u91cf\u4f1a\u81ea\u52a8\u7d2f\u52a0\uff0c\u4ece\u800c\u5b8c\u6210\u9010\u4e2a\u8f93\u51fa\u7684\u529f<br \/>\n\u80fd\u3002<\/p>\n<p>&nbsp;<\/p>\n<h2>\u5176\u4ed6\u6709\u76ca\u7684\u53c2\u8003<\/h2>\n<p>\u30101\u3011<a href=\"http:\/\/www.ibm.com\/developerworks\/aix\/library\/au-gdb.html\">GNU Project Debugger: More fun with GDB<br \/>\n<\/a> \u6b64\u6587\u4e2d\u4ecb\u7ecd\u4e86\u5404\u79cddefine\u7684command\uff0c\u53ef\u4ee5\u7528\u6765\u7b80\u5316\u547d\u4ee4\u7684\u8f93\u5165\u3002<br \/>\n\u30102\u3011 \u9648\u7693\u535a\u5ba2\uff1a<a href=\"http:\/\/blog.csdn.net\/haoel\/archive\/2003\/07\/02\/2879.aspx\">\u7528GDB\u8c03\u8bd5\u7a0b\u5e8f<\/a> \u5305\u62ec\u7684\u5f88\u8be6\u5c3d\u7684\u5bf9\u5404\u79cd\u547d\u4ee4\u7684\u4ecb\u7ecd<br \/>\n\u30103\u3011<a href=\"http:\/\/coolshell.cn\/articles\/3643.html\">GDB\u4e2d\u5e94\u8be5\u77e5\u9053\u7684\u51e0\u4e2a\u8c03\u8bd5\u65b9\u6cd5<\/a> \u5e38\u7528\u7684\u51e0\u4e2aGDB\u6280\u5de7\uff0c\u4f8b\u5982\u8c03\u8bd5macro\uff0c\u6761\u4ef6\u65ad\u70b9\uff0ccommand\u547d\u4ee4\u7b49\u7b49\u3002<\/p>\n<p>&nbsp;<\/p>\n<h2>BTW\uff1a<\/h2>\n<p>\u521a\u521a\u53d1\u73b0\u8fdb\u5165blog\u7684\u65f6\u5019\u53c8\u662f\u767d\u5c4f\u4e86\uff0c\u539f\u56e0\u8fd8\u662fMySQL\u6ca1\u6709\u81ea\u52a8\u542f\u52a8\u3002<br \/>\n\u4ece\u8fd9\u91cc\u627e\u89e3\u51b3\u65b9\u6cd5\uff0c\u5e0c\u671b\u7ba1\u7528\uff1a<br \/>\nhttp:\/\/ubuntuforums.org\/showthread.php?t=1668170<\/p>\n<p>sudo apt-get install sysv-rc-conf<br \/>\nsudo sysv-rc-conf<br \/>\n\u628amysql\u5728level 2,3,4,5\u4e2d\u9009\u4e2d\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u672c\u6587\u4ecb\u7ecd\u4e00\u4e9b\u5e38\u7528\u7684GDB\u6280\u5de7 &nbsp; \uff081\uff09\u4f7f\u7528.gdbinit \u5728.gdbinit\u91cc\u53ef\u4ee5\u4f7f\u7528 define \u547d\u4ee4\uff0c\u6765\u7b80\u5316\u590d\u6742\u547d\u4ee4\u7684\u8f93\u5165\uff0c\u4f8b\u5982\u91cd\u5b9a\u5411stdout, stderr\uff1a def redirect call (void)close(1) call (void)close(2) call (int)open($arg0, 2) call (int)open($arg0, 2) end \u4e4b\u540e\u5c31\u53ef\u4ee5\u7528 (gdb)redirect(&#8220;\/dev\/ttyp3&#8221;) \u6765\u628a\u8f93\u5165\u8f93\u51fa\u91cd\u5b9a\u5411\u5230tty3 \uff08\u8fd9\u4e2a\/dev\/tty3\u53ef\u4ee5\u7528 shell tty\u547d\u4ee4\u83b7\u5f97\uff09 \u8fd9\u4e2a\u4f8b\u5b50\u9009\u81eahttp:\/\/blogold.chinaunix.net\/u3\/111274\/showart_2162256.html \u6b64\u5916\uff0c\u8fd8\u53ef\u4ee5\u7ed9\u8fd9\u4e2a\u522b\u540d\u52a0\u4e0a\u5e2e\u52a9\u8bf4\u660e\u6027\u6587\u5b57\uff0c\u683c\u5f0f\u4e3a\uff1a document user-defined-command \u5e2e\u52a9\u8bf4\u660e\u6027\u6587\u5b57 end \u4f8b\u5982\uff1a document redirect redirect stdout and stderr to arg0 end .gdbinit\u4e2d\u8fd8\u53ef\u4ee5\u5b9a\u4e49hook\u51fd\u6570\uff08http:\/\/dev.firnow.com\/course\/6_system\/linux\/linuxjq\/20090307\/159416.html\uff09\uff0c \u4f8b\u5982\u60f3\u5728print\u547d\u4ee4\u524d\u663e\u793a\u4e00\u6bb5\u201c&#8212;&#8212;&#8212;-\u201d\uff0c\uff1a define hook-print echo &#8212;&#8212;&#8212;-\\n end .gdbinit\u4f1a\u88abGDB\u9ed8\u8ba4\u8bfb\u53d6\uff0c\u5982\u679c\u6709\u7279\u5b9a\u7684\u914d\u7f6e\u6587\u4ef6\uff0c\u53ef\u4ee5\u7528source\u547d\u4ee4\uff08\u7c7b\u4f3cbash\u7684source\u547d\u4ee4\uff09 \uff082\uff09\u8865\u5145\u4e00\u4e9b\u6709\u7528\u7684\u547d\u4ee4 \u7528whatis\u547d\u4ee4\u68c0\u67e5\u53d8\u91cf\u7684\u7c7b\u578b\uff0c ptype:\u6bd4whatis\u7684\u529f\u80fd\u66f4\u5f3a\uff0c\u4ed6\u53ef\u4ee5\u63d0\u4f9b\u4e00\u4e2a\u7ed3\u6784\u7684\u5b9a\u4e49\uff0c commands \u547d\u4e2d\u65ad\u70b9\u65f6\uff0c\u5217\u51fa\u5c06\u8981\u6267\u884c\u7684\u547d\u4ee4 [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[41,27],"class_list":["post-164","post","type-post","status-publish","format-standard","hentry","category-code","tag-gdb","tag-mysql"],"_links":{"self":[{"href":"https:\/\/zhanxw.com\/blog\/wp-json\/wp\/v2\/posts\/164","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zhanxw.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zhanxw.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zhanxw.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/zhanxw.com\/blog\/wp-json\/wp\/v2\/comments?post=164"}],"version-history":[{"count":0,"href":"https:\/\/zhanxw.com\/blog\/wp-json\/wp\/v2\/posts\/164\/revisions"}],"wp:attachment":[{"href":"https:\/\/zhanxw.com\/blog\/wp-json\/wp\/v2\/media?parent=164"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zhanxw.com\/blog\/wp-json\/wp\/v2\/categories?post=164"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zhanxw.com\/blog\/wp-json\/wp\/v2\/tags?post=164"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}