{"id":15607,"date":"2008-12-18T23:34:57","date_gmt":"2008-12-18T15:34:57","guid":{"rendered":"https:\/\/www.fuhaijun.com\/?p=15607"},"modified":"2008-12-18T23:34:57","modified_gmt":"2008-12-18T15:34:57","slug":"linux%e4%b8%8bc%e5%bc%80%e5%8f%91%e5%b7%a5%e5%85%b7%e4%bb%8b%e7%bb%8d","status":"publish","type":"post","link":"https:\/\/www.fuhaijun.com\/?p=15607","title":{"rendered":"Linux\u4e0bC\u5f00\u53d1\u5de5\u5177\u4ecb\u7ecd"},"content":{"rendered":"<div id=\"Published By Juziyue-[4]1_661990F0234A4063AB0F25DF46D7C53E_5139C68D1F8542EF9A693BA10776008B\">\n<p>Linux\u7684\u53d1\u884c\u7248\u4e2d\u5305\u542b\u4e86\u5f88\u591a\u8f6f\u4ef6\u5f00\u53d1\u5de5\u5177. \u5b83\u4eec\u4e2d\u7684\u5f88\u591a\u662f\u7528\u4e8e C \u548c C++\u5e94\u7528\u7a0b\u5e8f\u5f00\u53d1\u7684. \u672c\u6587\u4ecb\u7ecd\u4e86\u5728 Linux \u4e0b\u80fd\u7528\u4e8e C \u5e94\u7528\u7a0b\u5e8f\u5f00\u53d1\u548c\u8c03\u8bd5\u7684\u5de5\u5177. \u672c\u6587\u7684\u4e3b\u65e8\u662f\u4ecb\u7ecd\u5982\u4f55\u5728 Linux \u4e0b\u4f7f\u7528 C \u7f16\u8bd1\u5668\u548c\u5176\u4ed6 C \u7f16\u7a0b\u5de5\u5177, \u800c\u975e C \u8bed\u8a00\u7f16\u7a0b\u7684\u6559\u7a0b.&nbsp;<\/p>\n<p><\/p>\n<p><\/p>\n<p>GNU C \u7f16\u8bd1\u5668&nbsp;<\/p>\n<p>GNU C \u7f16\u8bd1\u5668(GCC)\u662f\u4e00\u4e2a\u5168\u529f\u80fd\u7684 ANSI C \u517c\u5bb9\u7f16\u8bd1\u5668. \u5982\u679c\u4f60\u719f\u6089\u5176\u4ed6\u64cd\u4f5c\u7cfb\u7edf\u6216\u786c\u4ef6\u5e73\u53f0\u4e0a\u7684\u4e00\u79cd C \u7f16\u8bd1\u5668, \u4f60\u5c06\u80fd\u5f88\u5feb\u5730\u638c\u63e1 GCC. \u672c\u8282\u5c06\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528 GCC \u548c\u4e00\u4e9b GCC \u7f16\u8bd1\u5668\u6700\u5e38\u7528\u7684\u9009\u9879.&nbsp;<\/p>\n<p><\/p>\n<p>\u4f7f\u7528 GCC&nbsp;<\/p>\n<p>\u901a\u5e38\u540e\u8ddf\u4e00\u4e9b\u9009\u9879\u548c\u6587\u4ef6\u540d\u6765\u4f7f\u7528 GCC \u7f16\u8bd1\u5668. gcc \u547d\u4ee4\u7684\u57fa\u672c\u7528\u6cd5\u5982\u4e0b:&nbsp;<\/p>\n<p><\/p>\n<p>gcc [options] [filenames]&nbsp;<\/p>\n<p>\u547d\u4ee4\u884c\u9009\u9879\u6307\u5b9a\u7684\u64cd\u4f5c\u5c06\u5728\u547d\u4ee4\u884c\u4e0a\u6bcf\u4e2a\u7ed9\u51fa\u7684\u6587\u4ef6\u4e0a\u6267\u884c. \u4e0b\u4e00\u5c0f\u8282\u5c06\u53d9\u8ff0\u4e00\u4e9b\u4f60\u4f1a\u6700\u5e38\u7528\u5230\u7684\u9009\u9879.&nbsp;<\/p>\n<p><\/p>\n<p><\/p>\n<p>GCC \u9009\u9879&nbsp;<\/p>\n<p>GCC \u6709\u8d85\u8fc7100\u4e2a\u7684\u7f16\u8bd1\u9009\u9879\u53ef\u7528. \u8fd9\u4e9b\u9009\u9879\u4e2d\u7684\u8bb8\u591a\u4f60\u53ef\u80fd\u6c38\u8fdc\u90fd\u4e0d\u4f1a\u7528\u5230, \u4f46\u4e00\u4e9b\u4e3b\u8981\u7684\u9009\u9879\u5c06\u4f1a\u9891\u7e41\u7528\u5230. \u5f88\u591a\u7684 GCC \u9009\u9879\u5305\u62ec\u4e00\u4e2a\u4ee5\u4e0a\u7684\u5b57\u7b26. \u56e0\u6b64\u4f60\u5fc5\u987b\u4e3a\u6bcf\u4e2a\u9009\u9879\u6307\u5b9a\u5404\u81ea\u7684\u8fde\u5b57\u7b26, \u5e76\u4e14\u5c31\u8c61\u5927\u591a\u6570 Linux \u547d\u4ee4\u4e00\u6837\u4f60\u4e0d\u80fd\u5728\u4e00\u4e2a\u5355\u72ec\u7684\u8fde\u5b57\u7b26\u540e\u8ddf\u4e00\u7ec4\u9009\u9879. \u4f8b\u5982, \u4e0b\u9762\u7684\u4e24\u4e2a\u547d\u4ee4\u662f\u4e0d\u540c\u7684:&nbsp;<\/p>\n<p><\/p>\n<p>gcc -p -g test.c&nbsp;<\/p>\n<p><\/p>\n<p>gcc -pg test.c&nbsp;<\/p>\n<p>\u7b2c\u4e00\u6761\u547d\u4ee4\u544a\u8bc9 GCC \u7f16\u8bd1 test.c \u65f6\u4e3a prof \u547d\u4ee4\u5efa\u7acb\u5256\u6790(profile)\u4fe1\u606f\u5e76\u4e14\u628a\u8c03\u8bd5\u4fe1\u606f\u52a0\u5165\u5230\u53ef\u6267\u884c\u7684\u6587\u4ef6\u91cc. \u7b2c\u4e8c\u6761\u547d\u4ee4\u53ea\u544a\u8bc9 GCC \u4e3a gprof \u547d\u4ee4\u5efa\u7acb\u5256\u6790\u4fe1\u606f.&nbsp;<\/p>\n<p><\/p>\n<p>\u5f53\u4f60\u4e0d\u7528\u4efb\u4f55\u9009\u9879\u7f16\u8bd1\u4e00\u4e2a\u7a0b\u5e8f\u65f6, GCC \u5c06\u4f1a\u5efa\u7acb(\u5047\u5b9a\u7f16\u8bd1\u6210\u529f)\u4e00\u4e2a\u540d\u4e3a a.out \u7684\u53ef\u6267\u884c\u6587\u4ef6. \u4f8b\u5982, \u4e0b\u9762\u7684\u547d\u4ee4\u5c06\u5728\u5f53\u524d\u76ee\u5f55\u4e0b\u4ea7\u751f\u4e00\u4e2a\u53eb a.out \u7684\u6587\u4ef6:&nbsp;<\/p>\n<p><\/p>\n<p>gcc test.c&nbsp;<\/p>\n<p>\u4f60\u80fd\u7528 -o \u7f16\u8bd1\u9009\u9879\u6765\u4e3a\u5c06\u4ea7\u751f\u7684\u53ef\u6267\u884c\u6587\u4ef6\u6307\u5b9a\u4e00\u4e2a\u6587\u4ef6\u540d\u6765\u4ee3\u66ff a.out. \u4f8b\u5982, \u5c06\u4e00\u4e2a\u53eb count.c \u7684 C \u7a0b\u5e8f\u7f16\u8bd1\u4e3a\u540d\u53eb count \u7684\u53ef\u6267\u884c\u6587\u4ef6, \u4f60\u5c06\u8f93\u5165\u4e0b\u9762\u7684\u547d\u4ee4:&nbsp;<\/p>\n<p><\/p>\n<p>gcc -o count count.c&nbsp;<\/p>\n<p><\/p>\n<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;&nbsp;<\/p>\n<p><\/p>\n<p>\u6ce8\u610f: \u5f53\u4f60\u4f7f\u7528 -o \u9009\u9879\u65f6, -o \u540e\u9762\u5fc5\u987b\u8ddf\u4e00\u4e2a\u6587\u4ef6\u540d.&nbsp;<\/p>\n<p><\/p>\n<p><\/p>\n<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;&nbsp;<\/p>\n<p><\/p>\n<p>GCC \u540c\u6837\u6709\u6307\u5b9a\u7f16\u8bd1\u5668\u5904\u7406\u591a\u5c11\u7684\u7f16\u8bd1\u9009\u9879. -c \u9009\u9879\u544a\u8bc9 GCC \u4ec5\u628a\u6e90\u4ee3\u7801\u7f16\u8bd1\u4e3a\u76ee\u6807\u4ee3\u7801\u800c\u8df3\u8fc7\u6c47\u7f16\u548c\u8fde\u63a5\u7684\u6b65\u9aa4. \u8fd9\u4e2a\u9009\u9879\u4f7f\u7528\u7684\u975e\u5e38\u9891\u7e41\u56e0\u4e3a\u5b83\u4f7f\u5f97\u7f16\u8bd1\u591a\u4e2a C \u7a0b\u5e8f\u65f6\u901f\u5ea6\u66f4\u5feb\u5e76\u4e14\u66f4\u6613\u4e8e\u7ba1\u7406. \u7f3a\u7701\u65f6 GCC \u5efa\u7acb\u7684\u76ee\u6807\u4ee3\u7801\u6587\u4ef6\u6709\u4e00\u4e2a .o \u7684\u6269\u5c55\u540d.&nbsp;<\/p>\n<p><\/p>\n<p>-S \u7f16\u8bd1\u9009\u9879\u544a\u8bc9 GCC \u5728\u4e3a C \u4ee3\u7801\u4ea7\u751f\u4e86\u6c47\u7f16\u8bed\u8a00\u6587\u4ef6\u540e\u505c\u6b62\u7f16\u8bd1. GCC \u4ea7\u751f\u7684\u6c47\u7f16\u8bed\u8a00\u6587\u4ef6\u7684\u7f3a\u7701\u6269\u5c55\u540d\u662f .s . -E \u9009\u9879\u6307\u793a\u7f16\u8bd1\u5668\u4ec5\u5bf9\u8f93\u5165\u6587\u4ef6\u8fdb\u884c\u9884\u5904\u7406. \u5f53\u8fd9\u4e2a\u9009\u9879\u88ab\u4f7f\u7528\u65f6, \u9884\u5904\u7406\u5668\u7684\u8f93\u51fa\u88ab\u9001\u5230\u6807\u51c6\u8f93\u51fa\u800c\u4e0d\u662f\u50a8\u5b58\u5728\u6587\u4ef6\u91cc.&nbsp;<\/p>\n<p><\/p>\n<p>\u4f18 \u5316 \u9009 \u9879&nbsp;<\/p>\n<p>\u5f53 \u4f60\u7528 GCC \u7f16\u8bd1 C \u4ee3\u7801\u65f6, \u5b83\u4f1a\u8bd5\u7740\u7528\u6700\u5c11\u7684\u65f6\u95f4\u5b8c\u6210\u7f16\u8bd1\u5e76\u4e14\u4f7f\u7f16\u8bd1\u540e\u7684\u4ee3\u7801\u6613\u4e8e\u8c03\u8bd5. \u6613\u4e8e\u8c03\u8bd5\u610f\u5473\u7740\u7f16\u8bd1\u540e\u7684\u4ee3\u7801\u4e0e\u6e90\u4ee3\u7801\u6709\u540c\u6837\u7684\u6267\u884c\u6b21\u5e8f, \u7f16\u8bd1\u540e\u7684\u4ee3\u7801\u6ca1\u6709\u7ecf\u8fc7\u4f18\u5316. \u6709\u5f88\u591a\u9009\u9879\u53ef\u7528\u4e8e\u544a\u8bc9 GCC \u5728\u8017\u8d39\u66f4\u591a\u7f16\u8bd1\u65f6\u95f4\u548c\u727a\u7272\u6613\u8c03\u8bd5\u6027\u7684\u57fa\u7840\u4e0a\u4ea7\u751f\u66f4\u5c0f\u66f4\u5feb\u7684\u53ef\u6267\u884c\u6587\u4ef6. \u8fd9\u4e9b\u9009\u9879\u4e2d\u6700\u5178\u578b\u7684\u662f-O \u548c -O2 \u9009\u9879.&nbsp;<\/p>\n<p><\/p>\n<p>-O \u9009\u9879\u544a\u8bc9 GCC \u5bf9\u6e90\u4ee3\u7801\u8fdb\u884c\u57fa\u672c\u4f18\u5316. \u8fd9\u4e9b\u4f18\u5316\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\u90fd\u4f1a\u4f7f\u7a0b\u5e8f\u6267\u884c\u7684\u66f4\u5feb. -O2 \u9009\u9879\u544a\u8bc9 GCC \u4ea7\u751f\u5c3d\u53ef\u80fd\u5c0f\u548c\u5c3d\u53ef\u80fd\u5feb\u7684\u4ee3\u7801. -O2 \u9009\u9879\u5c06\u4f7f\u7f16\u8bd1\u7684\u901f\u5ea6\u6bd4\u4f7f\u7528 -O \u65f6\u6162. \u4f46\u901a\u5e38\u4ea7\u751f\u7684\u4ee3\u7801\u6267\u884c\u901f\u5ea6\u4f1a\u66f4\u5feb.&nbsp;<\/p>\n<p><\/p>\n<p>\u9664 \u4e86 -O \u548c -O2 \u4f18\u5316\u9009\u9879\u5916, \u8fd8\u6709\u4e00\u4e9b\u4f4e\u7ea7\u9009\u9879\u7528\u4e8e\u4ea7\u751f\u66f4\u5feb\u7684\u4ee3\u7801. \u8fd9\u4e9b\u9009\u9879\u975e\u5e38\u7684\u7279\u6b8a, \u800c\u4e14\u6700\u597d\u53ea\u6709\u5f53\u4f60\u5b8c\u5168\u7406\u89e3\u8fd9\u4e9b\u9009\u9879\u5c06\u4f1a\u5bf9\u7f16\u8bd1\u540e\u7684\u4ee3\u7801\u4ea7\u751f\u4ec0\u4e48\u6837\u7684\u6548\u679c\u65f6\u518d\u53bb\u4f7f\u7528. \u8fd9\u4e9b\u9009\u9879\u7684\u8be6\u7ec6\u63cf\u8ff0, \u8bf7\u53c2\u8003 GCC \u7684\u6307\u5357\u9875, \u5728\u547d\u4ee4\u884c\u4e0a\u952e\u5165 man gcc .&nbsp;<\/p>\n<p><\/p>\n<p>\u8c03\u8bd5\u548c\u5256\u6790\u9009\u9879&nbsp;<\/p>\n<p>GCC \u652f\u6301\u6570\u79cd\u8c03\u8bd5\u548c\u5256\u6790\u9009\u9879. \u5728\u8fd9\u4e9b\u9009\u9879\u91cc\u4f60\u4f1a\u6700\u5e38\u7528\u5230\u7684\u662f -g \u548c -pg \u9009\u9879.&nbsp;<\/p>\n<p>-g \u9009\u9879\u544a\u8bc9 GCC \u4ea7\u751f\u80fd\u88ab GNU \u8c03\u8bd5\u5668\u4f7f\u7528\u7684\u8c03\u8bd5\u4fe1\u606f\u4ee5\u4fbf\u8c03\u8bd5\u4f60\u7684\u7a0b\u5e8f. GCC \u63d0\u4f9b\u4e86\u4e00\u4e2a\u5f88\u591a\u5176\u4ed6 C \u7f16\u8bd1\u5668\u91cc\u6ca1\u6709\u7684\u7279\u6027, \u5728 GCC \u91cc\u4f60\u80fd\u4f7f -g \u548c -O (\u4ea7\u751f\u4f18\u5316\u4ee3\u7801)\u8054\u7528. \u8fd9\u4e00\u70b9\u975e\u5e38\u6709\u7528\u56e0\u4e3a\u4f60\u80fd\u5728\u4e0e\u6700\u7ec8\u4ea7\u54c1\u5c3d\u53ef\u80fd\u76f8\u8fd1\u7684\u60c5\u51b5\u4e0b\u8c03\u8bd5\u4f60\u7684\u4ee3\u7801. \u5728\u4f60\u540c\u65f6\u4f7f\u7528\u8fd9\u4e24\u4e2a\u9009\u9879\u65f6\u4f60\u5fc5\u987b\u6e05\u695a\u4f60\u6240\u5199\u7684\u67d0\u4e9b\u4ee3\u7801\u5df2\u7ecf\u5728\u4f18\u5316\u65f6\u88ab GCC \u4f5c\u4e86\u6539\u52a8. \u5173\u4e8e\u8c03\u8bd5 C \u7a0b\u5e8f\u7684\u66f4\u591a\u4fe1\u606f\u8bf7\u770b\u4e0b\u4e00\u8282&#8221;\u7528 gdb \u8c03\u8bd5 C \u7a0b\u5e8f&#8221; .&nbsp;<\/p>\n<p>-pg \u9009\u9879\u544a\u8bc9 GCC \u5728\u4f60\u7684\u7a0b\u5e8f\u91cc\u52a0\u5165\u989d\u5916\u7684\u4ee3\u7801, \u6267\u884c\u65f6, \u4ea7\u751f gprof \u7528\u7684\u5256\u6790\u4fe1\u606f\u4ee5\u663e\u793a\u4f60\u7684\u7a0b\u5e8f\u7684\u8017\u65f6\u60c5\u51b5. \u5173\u4e8e gprof \u7684\u66f4\u591a\u4fe1\u606f\u8bf7\u53c2\u8003 &#8220;gprof&#8221; \u4e00\u8282.&nbsp;<\/p>\n<p><\/p>\n<p><\/p>\n<p>\u7528 gdb \u8c03\u8bd5 GCC \u7a0b\u5e8f&nbsp;<\/p>\n<p>Linux \u5305\u542b\u4e86\u4e00\u4e2a\u53eb gdb \u7684 GNU \u8c03\u8bd5\u7a0b\u5e8f. gdb \u662f\u4e00\u4e2a\u7528\u6765\u8c03\u8bd5 C \u548c C++ \u7a0b\u5e8f\u7684\u5f3a\u529b\u8c03\u8bd5\u5668. \u5b83\u4f7f\u4f60\u80fd\u5728\u7a0b\u5e8f\u8fd0\u884c\u65f6\u89c2\u5bdf\u7a0b\u5e8f\u7684\u5185\u90e8\u7ed3\u6784\u548c\u5185\u5b58\u7684\u4f7f\u7528\u60c5\u51b5. \u4ee5\u4e0b\u662f gdb \u6240\u63d0\u4f9b\u7684\u4e00\u4e9b\u529f\u80fd:&nbsp;<\/p>\n<p><\/p>\n<p>\u5b83\u4f7f\u4f60\u80fd\u76d1\u89c6\u4f60\u7a0b\u5e8f\u4e2d\u53d8\u91cf\u7684\u503c.&nbsp;<\/p>\n<p>\u5b83\u4f7f\u4f60\u80fd\u8bbe\u7f6e\u65ad\u70b9\u4ee5\u4f7f\u7a0b\u5e8f\u5728\u6307\u5b9a\u7684\u4ee3\u7801\u884c\u4e0a\u505c\u6b62\u6267\u884c.&nbsp;<\/p>\n<p>\u5b83\u4f7f\u4f60\u80fd\u4e00\u884c\u884c\u7684\u6267\u884c\u4f60\u7684\u4ee3\u7801.&nbsp;<\/p>\n<p><\/p>\n<p><\/p>\n<p>\u5728\u547d\u4ee4\u884c\u4e0a\u952e\u5165 gdb \u5e76\u6309\u56de\u8f66\u952e\u5c31\u53ef\u4ee5\u8fd0\u884c gdb \u4e86, \u5982\u679c\u4e00\u5207\u6b63\u5e38\u7684\u8bdd, gdb \u5c06\u88ab\u542f\u52a8\u5e76\u4e14\u4f60\u5c06\u5728\u5c4f\u5e55\u4e0a\u770b\u5230\u7c7b\u4f3c\u7684\u5185\u5bb9:&nbsp;<\/p>\n<p><\/p>\n<p>GNU gdb 5.0&nbsp;<\/p>\n<p>Copyright 2000 Free Software Foundation, Inc.&nbsp;<\/p>\n<p>GDB is free software, covered by the GNU General Public License, and you are&nbsp;<\/p>\n<p>welcome to change it and\/or distribute copies of it under certain conditions.&nbsp;<\/p>\n<p>Type &#8220;show copying&#8221; to see the conditions.&nbsp;<\/p>\n<p>There is absolutely no warranty for GDB. Type &#8220;show warranty&#8221; for details.&nbsp;<\/p>\n<p>This GDB was configured as &#8220;i386-redhat-linux&#8221;.&nbsp;<\/p>\n<p>(gdb)&nbsp;<\/p>\n<p>\u5f53\u4f60\u542f\u52a8 gdb \u540e, \u4f60\u80fd\u5728\u547d\u4ee4\u884c\u4e0a\u6307\u5b9a\u5f88\u591a\u7684\u9009\u9879. \u4f60\u4e5f\u53ef\u4ee5\u4ee5\u4e0b\u9762\u7684\u65b9\u5f0f\u6765\u8fd0\u884c gdb :&nbsp;<\/p>\n<p><\/p>\n<p>gdb&nbsp;<\/p>\n<p>\u5f53 \u4f60\u7528\u8fd9\u79cd\u65b9\u5f0f\u8fd0\u884c gdb , \u4f60\u80fd\u76f4\u63a5\u6307\u5b9a\u60f3\u8981\u8c03\u8bd5\u7684\u7a0b\u5e8f. \u8fd9\u5c06\u544a\u8bc9gdb \u88c5\u5165\u540d\u4e3a fname \u7684\u53ef\u6267\u884c\u6587\u4ef6. \u4f60\u4e5f\u53ef\u4ee5\u7528 gdb \u53bb\u68c0\u67e5\u4e00\u4e2a\u56e0\u7a0b\u5e8f\u5f02\u5e38\u7ec8\u6b62\u800c\u4ea7\u751f\u7684 core \u6587\u4ef6, \u6216\u8005\u4e0e\u4e00\u4e2a\u6b63\u5728\u8fd0\u884c\u7684\u7a0b\u5e8f\u76f8\u8fde. \u4f60\u53ef\u4ee5\u53c2\u8003 gdb \u6307\u5357\u9875\u6216\u5728\u547d\u4ee4\u884c\u4e0a\u952e\u5165 gdb -h \u5f97\u5230\u4e00\u4e2a\u6709\u5173\u8fd9\u4e9b\u9009\u9879\u7684\u8bf4\u660e\u7684\u7b80\u5355\u5217\u8868.&nbsp;<\/p>\n<p><\/p>\n<p><\/p>\n<p>\u4e3a\u8c03\u8bd5\u7f16\u8bd1\u4ee3\u7801(Compiling Code for Debugging)&nbsp;<\/p>\n<p>\u4e3a\u4e86\u4f7f gdb \u6b63\u5e38\u5de5\u4f5c, \u4f60\u5fc5\u987b\u4f7f\u4f60\u7684\u7a0b\u5e8f\u5728\u7f16\u8bd1\u65f6\u5305\u542b\u8c03\u8bd5\u4fe1\u606f. \u8c03\u8bd5\u4fe1\u606f\u5305\u542b\u4f60\u7a0b\u5e8f\u91cc\u7684\u6bcf\u4e2a\u53d8\u91cf\u7684\u7c7b\u578b\u548c\u5728\u53ef\u6267\u884c\u6587\u4ef6\u91cc\u7684\u5730\u5740\u6620\u5c04\u4ee5\u53ca\u6e90\u4ee3\u7801\u7684\u884c\u53f7. gdb \u5229\u7528\u8fd9\u4e9b\u4fe1\u606f\u4f7f\u6e90\u4ee3\u7801\u548c\u673a\u5668\u7801\u76f8\u5173\u8054.&nbsp;<\/p>\n<p><\/p>\n<p>\u5728\u7f16\u8bd1\u65f6\u7528 -g \u9009\u9879\u6253\u5f00\u8c03\u8bd5\u9009\u9879.&nbsp;<\/p>\n<p><\/p>\n<p><\/p>\n<p>gdb \u57fa\u672c\u547d\u4ee4&nbsp;<\/p>\n<p>gdb \u652f\u6301\u5f88\u591a\u7684\u547d\u4ee4\u4f7f\u4f60\u80fd\u5b9e\u73b0\u4e0d\u540c\u7684\u529f\u80fd. \u8fd9\u4e9b\u547d\u4ee4\u4ece\u7b80\u5355\u7684\u6587\u4ef6\u88c5\u5165\u5230\u5141\u8bb8\u4f60\u68c0\u67e5\u6240\u8c03\u7528\u7684\u5806\u6808\u5185\u5bb9\u7684\u590d\u6742\u547d\u4ee4, \u886827.1\u5217\u51fa\u4e86\u4f60\u5728\u7528 gdb \u8c03\u8bd5\u65f6\u4f1a\u7528\u5230\u7684\u4e00\u4e9b\u547d\u4ee4. \u60f3\u4e86\u89e3 gdb \u7684\u8be6\u7ec6\u4f7f\u7528\u8bf7\u53c2\u8003 gdb \u7684\u6307\u5357\u9875.&nbsp;<\/p>\n<p><\/p>\n<p>\u57fa\u672c gdb \u547d\u4ee4.&nbsp;<\/p>\n<p><\/p>\n<p>\u547d \u4ee4 \u63cf \u8ff0&nbsp;<\/p>\n<p>file \u88c5\u5165\u60f3\u8981\u8c03\u8bd5\u7684\u53ef\u6267\u884c\u6587\u4ef6.&nbsp;<\/p>\n<p>kill \u7ec8\u6b62\u6b63\u5728\u8c03\u8bd5\u7684\u7a0b\u5e8f.&nbsp;<\/p>\n<p>list \u5217\u51fa\u4ea7\u751f\u6267\u884c\u6587\u4ef6\u7684\u6e90\u4ee3\u7801\u7684\u4e00\u90e8\u5206.&nbsp;<\/p>\n<p>next \u6267\u884c\u4e00\u884c\u6e90\u4ee3\u7801\u4f46\u4e0d\u8fdb\u5165\u51fd\u6570\u5185\u90e8.&nbsp;<\/p>\n<p>step \u6267\u884c\u4e00\u884c\u6e90\u4ee3\u7801\u800c\u4e14\u8fdb\u5165\u51fd\u6570\u5185\u90e8.&nbsp;<\/p>\n<p>run \u6267\u884c\u5f53\u524d\u88ab\u8c03\u8bd5\u7684\u7a0b\u5e8f&nbsp;<\/p>\n<p>quit \u7ec8\u6b62 gdb&nbsp;<\/p>\n<p>watch \u4f7f\u4f60\u80fd\u76d1\u89c6\u4e00\u4e2a\u53d8\u91cf\u7684\u503c\u800c\u4e0d\u7ba1\u5b83\u4f55\u65f6\u88ab\u6539\u53d8.&nbsp;<\/p>\n<p>print \u663e\u793a\u8868\u8fbe\u5f0f\u7684\u503c&nbsp;<\/p>\n<p>break \u5728\u4ee3\u7801\u91cc\u8bbe\u7f6e\u65ad\u70b9, \u8fd9\u5c06\u4f7f\u7a0b\u5e8f\u6267\u884c\u5230\u8fd9\u91cc\u65f6\u88ab\u6302\u8d77.&nbsp;<\/p>\n<p>make \u4f7f\u4f60\u80fd\u4e0d\u9000\u51fa gdb \u5c31\u53ef\u4ee5\u91cd\u65b0\u4ea7\u751f\u53ef\u6267\u884c\u6587\u4ef6.&nbsp;<\/p>\n<p>shell \u4f7f\u4f60\u80fd\u4e0d\u79bb\u5f00 gdb \u5c31\u6267\u884c UNIX shell \u547d\u4ee4.&nbsp;<\/p>\n<p><\/p>\n<p><\/p>\n<p><\/p>\n<p><\/p>\n<p>gdb \u652f\u6301\u5f88\u591a\u4e0e UNIX shell \u7a0b\u5e8f\u4e00\u6837\u7684\u547d\u4ee4\u7f16\u8f91\u7279\u5f81. \u4f60\u80fd\u8c61\u5728 bash \u6216 tcsh\u91cc\u90a3\u6837\u6309 Tab \u952e\u8ba9 gdb \u5e2e\u4f60\u8865\u9f50\u4e00\u4e2a\u552f\u4e00\u7684\u547d\u4ee4, \u5982\u679c\u4e0d\u552f\u4e00\u7684\u8bdd gdb \u4f1a\u5217\u51fa\u6240\u6709\u5339\u914d\u7684\u547d\u4ee4. \u4f60\u4e5f\u80fd\u7528\u5149\u6807\u952e\u4e0a\u4e0b\u7ffb\u52a8\u5386\u53f2\u547d\u4ee4.&nbsp;<\/p>\n<p><\/p>\n<p>gdb \u5e94\u7528\u4e3e\u4f8b&nbsp;<\/p>\n<p>\u672c\u8282\u7528\u4e00\u4e2a\u5b9e\u4f8b\u6559\u4f60\u4e00\u6b65\u6b65\u7684\u7528 gdb \u8c03\u8bd5\u7a0b\u5e8f. \u88ab\u8c03\u8bd5\u7684\u7a0b\u5e8f\u76f8\u5f53\u7684\u7b80\u5355, \u4f46\u5b83\u5c55\u793a\u4e86 gdb \u7684\u5178\u578b\u5e94\u7528.&nbsp;<\/p>\n<p><\/p>\n<p>\u4e0b\u9762\u5217\u51fa\u4e86\u5c06\u88ab\u8c03\u8bd5\u7684\u7a0b\u5e8f. \u8fd9\u4e2a\u7a0b\u5e8f\u88ab\u79f0\u4e3a hello , \u5b83\u663e\u793a\u4e00\u4e2a\u7b80\u5355\u7684\u95ee\u5019, \u518d\u7528\u53cd\u5e8f\u5c06\u5b83\u5217\u51fa.&nbsp;<\/p>\n<p><\/p>\n<p>#include&nbsp;<\/p>\n<p><\/p>\n<p>static void my_print (char *);&nbsp;<\/p>\n<p>static void my_print2 (char *);&nbsp;<\/p>\n<p><\/p>\n<p>main ()&nbsp;<\/p>\n<p>{&nbsp;<\/p>\n<p>char my_string[] = &#8220;hello world!&#8221;;&nbsp;<\/p>\n<p>my_print (my_string);&nbsp;<\/p>\n<p>my_print2 (my_string);&nbsp;<\/p>\n<p>}&nbsp;<\/p>\n<p><\/p>\n<p>void my_print (char *string)&nbsp;<\/p>\n<p>{&nbsp;<\/p>\n<p>printf (&#8220;The string is %s &#8220;, string);&nbsp;<\/p>\n<p>}&nbsp;<\/p>\n<p><\/p>\n<p>void my_print2 (char *string)&nbsp;<\/p>\n<p>{&nbsp;<\/p>\n<p>char *string2;&nbsp;<\/p>\n<p>int size, i;&nbsp;<\/p>\n<p><\/p>\n<p>size = strlen (string);&nbsp;<\/p>\n<p>string2 = (char *) malloc (size + 1);&nbsp;<\/p>\n<p>for (i = 0; i &lt; size; i++)&nbsp;<\/p>\n<p>string2[size &#8211; i] = string[i];&nbsp;<\/p>\n<p>string2[size+1] = ;&nbsp;<\/p>\n<p><\/p>\n<p>printf (&#8220;The string printed backward is %s &#8220;, string2);&nbsp;<\/p>\n<p>}&nbsp;<\/p>\n<p>\u7528\u4e0b\u9762\u7684\u547d\u4ee4\u7f16\u8bd1\u5b83:&nbsp;<\/p>\n<p><\/p>\n<p><\/p>\n<p>gcc -g -o hello hello.c&nbsp;<\/p>\n<p>\u8fd9\u4e2a\u7a0b\u5e8f\u6267\u884c\u65f6\u663e\u793a\u5982\u4e0b\u7ed3\u679c:&nbsp;<\/p>\n<p>.\/hello&nbsp;<\/p>\n<p>The string is hello world!&nbsp;<\/p>\n<p><\/p>\n<p>The string printed backward is&nbsp;<\/p>\n<p>\u8f93\u51fa\u7684\u7b2c\u4e00\u884c\u662f\u6b63\u786e\u7684, \u4f46\u7b2c\u4e8c\u884c\u6253\u5370\u51fa\u7684\u4e1c\u897f\u5e76\u4e0d\u662f\u6211\u4eec\u6240\u671f\u671b\u7684. \u6211\u4eec\u6240\u8bbe\u60f3\u7684\u8f93\u51fa\u5e94\u8be5\u662f:&nbsp;<\/p>\n<p><\/p>\n<p>The string printed backward is !dlrow olleh&nbsp;<\/p>\n<p>\u7531\u4e8e\u67d0\u4e9b\u539f\u56e0, my_print2 \u51fd\u6570\u6ca1\u6709\u6b63\u5e38\u5de5\u4f5c. \u8ba9\u6211\u4eec\u7528 gdb \u770b\u770b\u95ee\u9898\u7a76\u7adf\u51fa\u5728\u54ea\u513f, \u5148\u952e\u5165\u5982\u4e0b\u547d\u4ee4:&nbsp;<\/p>\n<p><\/p>\n<p><\/p>\n<p>gdb hello&nbsp;<\/p>\n<p><\/p>\n<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;&nbsp;<\/p>\n<p><\/p>\n<p>\u6ce8\u610f: \u8bb0\u5f97\u5728\u7f16\u8bd1 hello \u7a0b\u5e8f\u65f6\u628a\u8c03\u8bd5\u9009\u9879\u6253\u5f00.&nbsp;<\/p>\n<p><\/p>\n<p><\/p>\n<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;&nbsp;<\/p>\n<p><\/p>\n<p>\u5982\u679c\u4f60\u5728\u8f93\u5165\u547d\u4ee4\u65f6\u5fd8\u4e86\u628a\u8981\u8c03\u8bd5\u7684\u7a0b\u5e8f\u4f5c\u4e3a\u53c2\u6570\u4f20\u7ed9 gdb , \u4f60\u53ef\u4ee5\u5728 gdb \u63d0\u793a\u7b26\u4e0b\u7528 file \u547d\u4ee4\u6765\u8f7d\u5165\u5b83:&nbsp;<\/p>\n<p><\/p>\n<p><\/p>\n<p>(gdb) file hello&nbsp;<\/p>\n<p>\u8fd9\u4e2a\u547d\u4ee4\u5c06\u8f7d\u5165 hello \u53ef\u6267\u884c\u6587\u4ef6\u5c31\u8c61\u4f60\u5728 gdb \u547d\u4ee4\u884c\u91cc\u88c5\u5165\u5b83\u4e00\u6837.&nbsp;<\/p>\n<p><\/p>\n<p>\u8fd9\u65f6\u4f60\u80fd\u7528 gdb \u7684 run \u547d\u4ee4\u6765\u8fd0\u884c hello \u4e86. \u5f53\u5b83\u5728 gdb \u91cc\u88ab\u8fd0\u884c\u540e\u7ed3\u679c\u5927\u7ea6\u4f1a\u8c61\u8fd9\u6837:&nbsp;<\/p>\n<p><\/p>\n<p>(gdb) run&nbsp;<\/p>\n<p><\/p>\n<p>Starting program: \/root\/hello&nbsp;<\/p>\n<p><\/p>\n<p>The string is hello world!&nbsp;<\/p>\n<p><\/p>\n<p>The string printed backward is&nbsp;<\/p>\n<p><\/p>\n<p>Program exited with code 040&nbsp;<\/p>\n<p>\u8fd9\u4e2a\u8f93\u51fa\u548c\u5728 gdb \u5916\u9762\u8fd0\u884c\u7684\u7ed3\u679c\u4e00\u6837. \u95ee\u9898\u662f, \u4e3a\u4ec0\u4e48\u53cd\u5e8f\u6253\u5370\u6ca1\u6709\u5de5\u4f5c? \u4e3a\u4e86\u627e\u51fa\u75c7\u7ed3\u6240\u5728, \u6211\u4eec\u53ef\u4ee5\u5728 my_print2 \u51fd\u6570\u7684 for \u8bed\u53e5\u540e\u8bbe\u4e00\u4e2a\u65ad\u70b9, \u5177\u4f53\u7684\u505a\u6cd5\u662f\u5728 gdb \u63d0\u793a\u7b26\u4e0b\u952e\u5165 list \u547d\u4ee4\u4e09\u6b21, \u5217\u51fa\u6e90\u4ee3\u7801:&nbsp;<\/p>\n<p><\/p>\n<p>(gdb) list&nbsp;<\/p>\n<p><\/p>\n<p>(gdb) list&nbsp;<\/p>\n<p><\/p>\n<p>(gdb) list&nbsp;<\/p>\n<p><\/p>\n<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;&nbsp;<\/p>\n<p><\/p>\n<p>\u6280\u5de7: \u5728 gdb \u63d0\u793a\u7b26\u4e0b\u6309\u56de\u8f66\u5065\u5c06\u91cd\u590d\u4e0a\u4e00\u4e2a\u547d\u4ee4.&nbsp;<\/p>\n<p><\/p>\n<p><\/p>\n<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;&nbsp;<\/p>\n<p><\/p>\n<p>\u7b2c\u4e00\u6b21\u952e\u5165 list \u547d\u4ee4\u7684\u8f93\u51fa\u5982\u4e0b:&nbsp;<\/p>\n<p><\/p>\n<p><\/p>\n<p>1 #include&nbsp;<\/p>\n<p>2&nbsp;<\/p>\n<p>3 static void my_print (char *);&nbsp;<\/p>\n<p>4 static void my_print2 (char *);&nbsp;<\/p>\n<p>5&nbsp;<\/p>\n<p>6 main ()&nbsp;<\/p>\n<p>7 {&nbsp;<\/p>\n<p>8 char my_string[] = &#8220;hello world!&#8221;;&nbsp;<\/p>\n<p>9 my_print (my_string);&nbsp;<\/p>\n<p>10 my_print2 (my_string);&nbsp;<\/p>\n<p><\/p>\n<p><\/p>\n<p>\u5982\u679c\u6309\u4e0b\u56de\u8f66, gdb \u5c06\u518d\u6267\u884c\u4e00\u6b21 list \u547d\u4ee4, \u7ed9\u51fa\u4e0b\u5217\u8f93\u51fa:&nbsp;<\/p>\n<p><\/p>\n<p><\/p>\n<p>11 }&nbsp;<\/p>\n<p>12&nbsp;<\/p>\n<p>13 void my_print (char *string)&nbsp;<\/p>\n<p>14 {&nbsp;<\/p>\n<p>15 printf (&#8220;The string is %s &#8220;, string);&nbsp;<\/p>\n<p>16 }&nbsp;<\/p>\n<p>17&nbsp;<\/p>\n<p>18 void my_print2 (char *string)&nbsp;<\/p>\n<p>19 {&nbsp;<\/p>\n<p>20 char *string2;&nbsp;<\/p>\n<p>\u518d\u6309\u4e00\u6b21\u56de\u8f66\u5c06\u5217\u51fa hello \u7a0b\u5e8f\u7684\u5269\u4f59\u90e8\u5206:&nbsp;<\/p>\n<p><\/p>\n<p>21 int size, i;&nbsp;<\/p>\n<p>22&nbsp;<\/p>\n<p>23 size = strlen (string);&nbsp;<\/p>\n<p>24 string2 = (char *) malloc (size + 1);&nbsp;<\/p>\n<p>25 for (i = 0; i &lt; size; i++)&nbsp;<\/p>\n<p>26 string2[size &#8211; i] = string[i];&nbsp;<\/p>\n<p>27 string2[size+1] = ;&nbsp;<\/p>\n<p>28&nbsp;<\/p>\n<p>29 printf (&#8220;The string printed backward is %s &#8220;, string2);&nbsp;<\/p>\n<p>30 }&nbsp;<\/p>\n<p>\u6839\u636e\u5217\u51fa\u7684\u6e90\u7a0b\u5e8f, \u4f60\u80fd\u770b\u5230\u8981\u8bbe\u65ad\u70b9\u7684\u5730\u65b9\u5728\u7b2c26\u884c, \u5728 gdb \u547d\u4ee4\u884c\u63d0\u793a\u7b26\u4e0b\u952e\u5165\u5982\u4e0b\u547d\u4ee4\u8bbe\u7f6e\u65ad\u70b9:&nbsp;<\/p>\n<p><\/p>\n<p>(gdb) break 26&nbsp;<\/p>\n<p>gdb \u5c06\u4f5c\u51fa\u5982\u4e0b\u7684\u54cd\u5e94:&nbsp;<\/p>\n<p><\/p>\n<p>Breakpoint 1 at 0x804857c: file hello.c, line 26.&nbsp;<\/p>\n<p><\/p>\n<p>(gdb)&nbsp;<\/p>\n<p><\/p>\n<p>\u73b0\u5728\u518d\u952e\u5165 run \u547d\u4ee4, \u5c06\u4ea7\u751f\u5982\u4e0b\u7684\u8f93\u51fa:&nbsp;<\/p>\n<p><\/p>\n<p><\/p>\n<p>Starting program: \/root\/hello&nbsp;<\/p>\n<p><\/p>\n<p>The string is hello world!&nbsp;<\/p>\n<p><\/p>\n<p>Breakpoint 1, my_print2 (string=0xbffffab0 &#8220;hello world!&#8221;) at hello.c:26&nbsp;<\/p>\n<p>26 string2[size &#8211; i] = string[i];&nbsp;<\/p>\n<p>\u4f60\u80fd\u901a\u8fc7\u8bbe\u7f6e\u4e00\u4e2a\u89c2\u5bdf string2[size &#8211; i] \u53d8\u91cf\u7684\u503c\u7684\u89c2\u5bdf\u70b9\u6765\u770b\u51fa\u9519\u8bef\u662f\u600e\u6837\u4ea7\u751f\u7684, \u505a\u6cd5\u662f\u952e\u5165:&nbsp;<\/p>\n<p><\/p>\n<p>(gdb) watch string2[size &#8211; i]&nbsp;<\/p>\n<p>gdb \u5c06\u4f5c\u51fa\u5982\u4e0b\u56de\u5e94:&nbsp;<\/p>\n<p><\/p>\n<p>Hardware watchpoint 2: string2[size &#8211; i]&nbsp;<\/p>\n<p>\u73b0\u5728\u53ef\u4ee5\u7528 next \u547d\u4ee4\u6765\u4e00\u6b65\u6b65\u7684\u6267\u884c for \u5faa\u73af\u4e86:&nbsp;<\/p>\n<p><\/p>\n<p><\/p>\n<p>(gdb) next&nbsp;<\/p>\n<p>\u7ecf\u8fc7\u7b2c\u4e00\u6b21\u5faa\u73af\u540e, gdb \u544a\u8bc9\u6211\u4eec string2[size &#8211; i] \u7684\u503c\u662f `h`. gdb \u7528\u5982\u4e0b\u7684\u663e\u793a\u6765\u544a\u8bc9\u4f60\u8fd9\u4e2a\u4fe1\u606f:&nbsp;<\/p>\n<p><\/p>\n<p><\/p>\n<p>Hardware watchpoint 2: string2[size &#8211; i]&nbsp;<\/p>\n<p><\/p>\n<p>Old value = 0 0&nbsp;<\/p>\n<p>New value = 104 h&nbsp;<\/p>\n<p>my_print2 (string=0xbffffab0 &#8220;hello world!&#8221;) at hello.c:25&nbsp;<\/p>\n<p>25 for (i = 0; i &lt; size; i++)&nbsp;<\/p>\n<p>\u8fd9\u4e2a\u503c\u6b63\u662f\u671f\u671b\u7684. \u540e\u6765\u7684\u6570\u6b21\u5faa\u73af\u7684\u7ed3\u679c\u90fd\u662f\u6b63\u786e\u7684. \u5f53 i=11 \u65f6, \u8868\u8fbe\u5f0f string2[size &#8211; i] \u7684\u503c\u7b49\u4e8e `!`, size &#8211; i \u7684\u503c\u7b49\u4e8e 1, \u6700\u540e\u4e00\u4e2a\u5b57\u7b26\u5df2\u7ecf\u62f7\u5230\u65b0\u4e32\u91cc\u4e86.&nbsp;<\/p>\n<p><\/p>\n<p>\u5982 \u679c\u4f60\u518d\u628a\u5faa\u73af\u6267\u884c\u4e0b\u53bb, \u4f60\u4f1a\u770b\u5230\u5df2\u7ecf\u6ca1\u6709\u503c\u5206\u914d\u7ed9 string2[0] \u4e86, \u800c\u5b83\u662f\u65b0\u4e32\u7684\u7b2c\u4e00\u4e2a\u5b57\u7b26, \u56e0\u4e3a malloc \u51fd\u6570\u5728\u5206\u914d\u5185\u5b58\u65f6\u628a\u5b83\u4eec\u521d\u59cb\u5316\u4e3a\u7a7a(null)\u5b57\u7b26. \u6240\u4ee5 string2 \u7684\u7b2c\u4e00\u4e2a\u5b57\u7b26\u662f\u7a7a\u5b57\u7b26. \u8fd9\u89e3\u91ca\u4e86\u4e3a\u4ec0\u4e48\u5728\u6253\u5370 string2 \u65f6\u6ca1\u6709\u4efb\u4f55\u8f93\u51fa\u4e86.&nbsp;<\/p>\n<p><\/p>\n<p>\u73b0\u5728\u627e\u51fa\u4e86\u95ee\u9898\u51fa\u5728\u54ea\u91cc, \u4fee\u6b63\u8fd9\u4e2a\u9519\u8bef\u662f\u5f88\u5bb9\u6613\u7684. \u4f60\u5f97\u628a\u4ee3\u7801\u91cc\u5199\u5165 string2 \u7684\u7b2c\u4e00\u4e2a\u5b57\u7b26\u7684\u7684\u504f\u79fb\u91cf\u6539\u4e3a size &#8211; 1 \u800c\u4e0d\u662f size. \u8fd9\u662f\u56e0\u4e3a string2 \u7684\u5927\u5c0f\u4e3a 12, \u4f46\u8d77\u59cb\u504f\u79fb\u91cf\u662f 0, \u4e32\u5185\u7684\u5b57\u7b26\u4ece\u504f\u79fb\u91cf 0 \u5230 \u504f\u79fb\u91cf 10, \u504f\u79fb\u91cf 11 \u4e3a\u7a7a\u5b57\u7b26\u4fdd\u7559.&nbsp;<\/p>\n<p><\/p>\n<p>\u6539\u6b63\u65b9\u6cd5\u975e\u5e38\u7b80\u5355. \u8fd9\u662f\u8fd9\u79cd\u89e3\u51b3\u529e\u6cd5\u7684\u4ee3\u7801:&nbsp;<\/p>\n<p><\/p>\n<p>#include&nbsp;<\/p>\n<p><\/p>\n<p>static void my_print (char *);&nbsp;<\/p>\n<p>static void my_print2 (char *);&nbsp;<\/p>\n<p><\/p>\n<p>main ()&nbsp;<\/p>\n<p>{&nbsp;<\/p>\n<p>char my_string[] = &#8220;hello world!&#8221;;&nbsp;<\/p>\n<p>my_print (my_string);&nbsp;<\/p>\n<p>my_print2 (my_string);&nbsp;<\/p>\n<p>}&nbsp;<\/p>\n<p><\/p>\n<p>void my_print (char *string)&nbsp;<\/p>\n<p>{&nbsp;<\/p>\n<p>printf (&#8220;The string is %s &#8220;, string);&nbsp;<\/p>\n<p>}&nbsp;<\/p>\n<p><\/p>\n<p>void my_print2 (char *string)&nbsp;<\/p>\n<p>{&nbsp;<\/p>\n<p>char *string2;&nbsp;<\/p>\n<p>int size, i;&nbsp;<\/p>\n<p><\/p>\n<p>size = strlen (string);&nbsp;<\/p>\n<p>string2 = (char *) malloc (size + 1);&nbsp;<\/p>\n<p>for (i = 0; i &lt; size; i++)&nbsp;<\/p>\n<p>string2[size -1 &#8211; i] = string[i];&nbsp;<\/p>\n<p>string2[size] = ;&nbsp;<\/p>\n<p><\/p>\n<p>printf (&#8220;The string printed backward is %s &#8220;, string2);&nbsp;<\/p>\n<p>}&nbsp;<\/p>\n<p>\u5982 \u679c\u7a0b\u5e8f\u4ea7\u751f\u4e86core\u6587\u4ef6\uff0c\u53ef\u4ee5\u7528gdb hello core\u547d\u4ee4\u6765\u67e5\u770b\u7a0b\u5e8f\u5728\u4f55\u5904\u51fa\u9519\u3002\u5982\u5728\u51fd\u6570my_print2()\u4e2d\uff0c\u5982\u679c\u5fd8\u8bb0\u4e86\u7ed9string2\u5206\u914d\u5185\u5b58 string2 = (char *) malloc (size + 1);\uff0c\u5f88\u53ef\u80fd\u5c31\u4f1acore dump.&nbsp;<\/p>\n<p><\/p>\n<p><\/p>\n<p>\u53e6\u5916\u7684 C \u7f16\u7a0b\u5de5\u5177&nbsp;<\/p>\n<p><\/p>\n<p>xxgdb&nbsp;<\/p>\n<p>xxgdb \u662f gdb \u7684\u4e00\u4e2a\u57fa\u4e8e X Window \u7cfb\u7edf\u7684\u56fe\u5f62\u754c\u9762. xxgdb \u5305\u62ec\u4e86\u547d\u4ee4\u884c\u7248\u7684 gdb \u4e0a\u7684\u6240\u6709\u7279\u6027. xxgdb \u4f7f\u4f60\u80fd\u901a\u8fc7\u6309\u6309\u94ae\u6765\u6267\u884c\u5e38\u7528\u7684\u547d\u4ee4. \u8bbe\u7f6e\u4e86\u65ad\u70b9\u7684\u5730\u65b9\u4e5f\u7528\u56fe\u5f62\u6765\u663e\u793a.&nbsp;<\/p>\n<p><\/p>\n<p>\u4f60\u80fd\u5728\u4e00\u4e2a Xterm \u7a97\u53e3\u91cc\u952e\u5165\u4e0b\u9762\u7684\u547d\u4ee4\u6765\u8fd0\u884c\u5b83:&nbsp;<\/p>\n<p><\/p>\n<p>xxgdb&nbsp;<\/p>\n<p>\u4f60\u80fd\u7528 gdb \u91cc\u4efb\u4f55\u6709\u6548\u7684\u547d\u4ee4\u884c\u9009\u9879\u6765\u521d\u59cb\u5316 xxgdb . \u6b64\u5916 xxgdb \u4e5f\u6709\u4e00\u4e9b\u7279\u6709\u7684\u547d\u4ee4\u884c\u9009\u9879, \u8868 27.2 \u5217\u51fa\u4e86\u8fd9\u4e9b\u9009\u9879.&nbsp;<\/p>\n<p><\/p>\n<p>\u8868 27.2. xxgdb \u547d\u4ee4\u884c\u9009\u9879.&nbsp;<\/p>\n<p><\/p>\n<p>\u9009 \u9879 \u63cf \u8ff0&nbsp;<\/p>\n<p>db_name \u6307\u5b9a\u6240\u7528\u8c03\u8bd5\u5668\u7684\u540d\u5b57, \u7f3a\u7701\u662f gdb.&nbsp;<\/p>\n<p>db_prompt \u6307\u5b9a\u8c03\u8bd5\u5668\u63d0\u793a\u7b26, \u7f3a\u7701\u4e3a gdb.&nbsp;<\/p>\n<p>gdbinit \u6307\u5b9a\u521d\u59cb\u5316 gdb \u7684\u547d\u4ee4\u6587\u4ef6\u7684\u6587\u4ef6\u540d, \u7f3a\u7701\u4e3a .gdbinit.&nbsp;<\/p>\n<p><\/p>\n<p>nx \u544a\u8bc9 xxgdb \u4e0d\u6267\u884c .gdbinit \u6587\u4ef6.&nbsp;<\/p>\n<p>bigicon \u4f7f\u7528\u5927\u56fe\u6807.&nbsp;<\/p>\n<p><\/p>\n<p><\/p>\n<p>calls&nbsp;<\/p>\n<p>\u4f60\u53ef\u4ee5\u5728 sunsite.unc.edu FTP \u7ad9\u70b9\u7528\u4e0b\u9762\u7684\u8def\u5f84:&nbsp;<\/p>\n<p>\/pub\/Linux\/devel\/lang\/c\/calls.tar.Z&nbsp;<\/p>\n<p>\u6765 \u53d6\u5f97 calls , \u4e00\u4e9b\u65e7\u7248\u672c\u7684 Linux CD-ROM \u53d1\u884c\u7248\u91cc\u4e5f\u9644\u5e26\u6709. \u56e0\u4e3a\u5b83\u662f\u4e00\u4e2a\u6709\u7528\u7684\u5de5\u5177, \u6211\u4eec\u5728\u8fd9\u91cc\u4e5f\u4ecb\u7ecd\u4e00\u4e0b. \u5982\u679c\u4f60\u89c9\u5f97\u6709\u7528\u7684\u8bdd, \u4ece BBS, FTP, \u6216\u53e6\u4e00\u5f20CD-ROM \u4e0a\u5f04\u4e00\u4e2a\u62f7\u8d1d. calls \u8c03\u7528 GCC \u7684\u9884\u5904\u7406\u5668\u6765\u5904\u7406\u7ed9\u51fa\u7684\u6e90\u7a0b\u5e8f\u6587\u4ef6, \u7136\u540e\u8f93\u51fa\u8fd9\u4e9b\u6587\u4ef6\u7684\u91cc\u7684\u51fd\u6570\u8c03\u7528\u6811\u56fe.&nbsp;<\/p>\n<p><\/p>\n<p><\/p>\n<p>\u6ce8\u610f: \u5728\u4f60\u7684\u7cfb\u7edf\u4e0a\u5b89\u88c5 calls , \u4ee5\u8d85\u7ea7\u7528\u6237\u8eab\u4efd\u767b\u5f55\u540e\u6267\u884c\u4e0b\u9762\u7684\u6b65\u9aa4: 1. \u89e3\u538b\u548c untar \u6587\u4ef6. 2. cd \u8fdb\u5165 calls untar \u540e\u5efa\u7acb\u7684\u5b50\u76ee\u5f55. 3. \u628a\u540d\u53eb calls \u7684\u6587\u4ef6\u79fb\u52a8\u5230 \/usr\/bin \u76ee\u5f55. 4. \u628a\u540d\u53eb calls.1 \u7684\u6587\u4ef6\u79fb\u52a8\u5230\u76ee\u5f55 \/usr\/man\/man1 . 5. \u5220\u9664 \/tmp\/calls \u76ee\u5f55. \u8fd9\u4e9b\u6b65\u9aa4\u5c06\u628a calls \u7a0b\u5e8f\u548c\u5b83\u7684\u6307\u5357\u9875\u5b89\u88c5\u8f7d\u4f60\u7684\u7cfb\u7edf\u4e0a.&nbsp;<\/p>\n<p><\/p>\n<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;&nbsp;<\/p>\n<p><\/p>\n<p>\u5f53 calls \u6253\u5370\u51fa\u8c03\u7528\u8ddf\u8e2a\u7ed3\u679c\u65f6, \u5b83\u5728\u51fd\u6570\u540e\u9762\u7528\u4e2d\u62ec\u53f7\u7ed9\u51fa\u4e86\u51fd\u6570\u6240\u5728\u6587\u4ef6\u7684\u6587\u4ef6\u540d:&nbsp;<\/p>\n<p><\/p>\n<p>main [hello.c]&nbsp;<\/p>\n<p>\u5982\u679c\u51fd\u6570\u5e76\u4e0d\u662f\u5411 calls \u7ed9\u51fa\u7684\u6587\u4ef6\u91cc\u7684, calls \u4e0d\u77e5\u9053\u6240\u8c03\u7528\u7684\u51fd\u6570\u6765\u81ea\u54ea\u91cc, \u5219\u53ea\u663e\u793a\u51fd\u6570\u7684\u540d\u5b57:&nbsp;<\/p>\n<p><\/p>\n<p>printf&nbsp;<\/p>\n<p>calls \u4e0d\u5bf9\u9012\u5f52\u548c\u9759\u6001\u51fd\u6570\u8f93\u51fa. \u9012\u5f52\u51fd\u6570\u663e\u793a\u6210\u4e0b\u9762\u7684\u6837\u5b50:&nbsp;<\/p>\n<p><\/p>\n<p>fact &lt;&lt;&lt; recursive in factorial.c &gt;&gt;&gt;&nbsp;<\/p>\n<p>\u9759\u6001\u51fd\u6570\u8c61\u8fd9\u6837\u663e\u793a:&nbsp;<\/p>\n<p><\/p>\n<p>total [static in calculate.c]&nbsp;<\/p>\n<p>\u4f5c\u4e3a\u4e00\u4e2a\u4f8b\u5b50, \u5047\u8bbe\u7528 calls \u5904\u7406\u4e0b\u9762\u7684\u7a0b\u5e8f:&nbsp;<\/p>\n<p><\/p>\n<p>#include&nbsp;<\/p>\n<p><\/p>\n<p>static void my_print (char *);&nbsp;<\/p>\n<p>static void my_print2 (char *);&nbsp;<\/p>\n<p><\/p>\n<p>main ()&nbsp;<\/p>\n<p>{&nbsp;<\/p>\n<p>char my_string[] = &#8220;hello world!&#8221;;&nbsp;<\/p>\n<p>my_print (my_string);&nbsp;<\/p>\n<p>my_print2 (my_string);&nbsp;<\/p>\n<p>my_print (my_string);&nbsp;<\/p>\n<p>}&nbsp;<\/p>\n<p><\/p>\n<p>void count_sum()&nbsp;<\/p>\n<p>{&nbsp;<\/p>\n<p>int i,sum=0;&nbsp;<\/p>\n<p>for(i=0; i&lt;1000000; i++)&nbsp;<\/p>\n<p>sum += i;&nbsp;<\/p>\n<p>}&nbsp;<\/p>\n<p><\/p>\n<p>void my_print (char *string)&nbsp;<\/p>\n<p>{&nbsp;<\/p>\n<p>count_sum();&nbsp;<\/p>\n<p>printf (&#8220;The string is %s &#8220;, string);&nbsp;<\/p>\n<p>}&nbsp;<\/p>\n<p><\/p>\n<p>void my_print2 (char *string)&nbsp;<\/p>\n<p>{&nbsp;<\/p>\n<p>char *string2;&nbsp;<\/p>\n<p>int size, i,sum =0;&nbsp;<\/p>\n<p><\/p>\n<p>count_sum();&nbsp;<\/p>\n<p>size = strlen (string);&nbsp;<\/p>\n<p>string2 = (char *) malloc (size + 1);&nbsp;<\/p>\n<p>for (i = 0; i &lt; size; i++) string2[size -1 &#8211; i] = string[i];&nbsp;<\/p>\n<p>string2[size] = ;&nbsp;<\/p>\n<p>for(i=0; i&lt;5000000; i++)&nbsp;<\/p>\n<p>sum += i;&nbsp;<\/p>\n<p><\/p>\n<p>printf (&#8220;The string printed backward is %s &#8220;, string2);&nbsp;<\/p>\n<p>}&nbsp;<\/p>\n<p>\u5c06\u4ea7\u751f\u5982\u4e0b\u7684\u8f93\u51fa:&nbsp;<\/p>\n<p><\/p>\n<p>1 __underflow [hello.c]&nbsp;<\/p>\n<p>2 main&nbsp;<\/p>\n<p>3 my_print [hello.c]&nbsp;<\/p>\n<p>4 count_sum [hello.c]&nbsp;<\/p>\n<p>5 printf&nbsp;<\/p>\n<p>6 my_print2 [hello.c]&nbsp;<\/p>\n<p>7 count_sum&nbsp;<\/p>\n<p>8 strlen&nbsp;<\/p>\n<p>9 malloc&nbsp;<\/p>\n<p>10 printf&nbsp;<\/p>\n<p>calls \u6709\u5f88\u591a\u547d\u4ee4\u884c\u9009\u9879\u6765\u8bbe\u7f6e\u4e0d\u540c\u7684\u8f93\u51fa\u683c\u5f0f, \u6709\u5173\u8fd9\u4e9b\u9009\u9879\u7684\u66f4\u591a\u4fe1\u606f\u8bf7\u53c2\u8003 calls \u7684\u6307\u5357\u9875. \u65b9\u6cd5\u662f\u5728\u547d\u4ee4\u884c\u4e0a\u952e\u5165 calls -h .&nbsp;<\/p>\n<p><\/p>\n<p><\/p>\n<p>calltree&nbsp;<\/p>\n<p>calltree\u4e0ecalls\u7c7b\u4f3c\uff0c\u521d\u4e86\u8f93\u51fa\u51fd\u6570\u8c03\u7528\u6811\u56fe\u5916\uff0c\u8fd8\u6709\u5176\u5b83\u8be6\u7ec6\u7684\u4fe1\u606f\u3002&nbsp;<\/p>\n<p>\u53ef\u4ee5\u4ecesunsite.unc.edu FTP \u7ad9\u70b9\u7528\u4e0b\u9762\u7684\u8def\u5f84:\/pub\/Linux\/devel\/lang\/c\/calltree.tar.gz\u5f97\u5230calltree.&nbsp;<\/p>\n<p><\/p>\n<p>cproto&nbsp;<\/p>\n<p>cproto \u8bfb\u5165 C \u6e90\u7a0b\u5e8f\u6587\u4ef6\u5e76\u81ea\u52a8\u4e3a\u6bcf\u4e2a\u51fd\u6570\u4ea7\u751f\u539f\u578b\u7533\u660e. \u7528 cproto \u53ef\u4ee5\u5728\u5199\u7a0b\u5e8f\u65f6\u4e3a\u4f60\u8282\u7701\u5927\u91cf\u7528\u6765\u5b9a\u4e49\u51fd\u6570\u539f\u578b\u7684\u65f6\u95f4.&nbsp;<\/p>\n<p>\u5982\u679c\u4f60\u8ba9 cproto \u5904\u7406\u4e0b\u9762\u7684\u4ee3\u7801(cproto hello.c):&nbsp;<\/p>\n<p><\/p>\n<p>#include&nbsp;<\/p>\n<p><\/p>\n<p>static void my_print (char *);&nbsp;<\/p>\n<p>static void my_print2 (char *);&nbsp;<\/p>\n<p><\/p>\n<p>main ()&nbsp;<\/p>\n<p>{&nbsp;<\/p>\n<p>char my_string[] = &#8220;hello world!&#8221;;&nbsp;<\/p>\n<p>my_print (my_string);&nbsp;<\/p>\n<p>my_print2 (my_string);&nbsp;<\/p>\n<p>}&nbsp;<\/p>\n<p><\/p>\n<p>void my_print (char *string)&nbsp;<\/p>\n<p>{&nbsp;<\/p>\n<p>printf (&#8220;The string is %s &#8220;, string);&nbsp;<\/p>\n<p>}&nbsp;<\/p>\n<p><\/p>\n<p>void my_print2 (char *string)&nbsp;<\/p>\n<p>{&nbsp;<\/p>\n<p>char *string2;&nbsp;<\/p>\n<p>int size, i;&nbsp;<\/p>\n<p><\/p>\n<p>size = strlen (string);&nbsp;<\/p>\n<p>string2 = (char *) malloc (size + 1);&nbsp;<\/p>\n<p>for (i = 0; i &lt; size; i++)&nbsp;<\/p>\n<p>string2[size -1 &#8211; i] = string[i];&nbsp;<\/p>\n<p>string2[size] = ;&nbsp;<\/p>\n<p><\/p>\n<p>printf (&#8220;The string printed backward is %s &#8220;, string2);&nbsp;<\/p>\n<p>}&nbsp;<\/p>\n<p>\u4f60\u5c06\u5f97\u5230\u4e0b\u9762\u7684\u8f93\u51fa:&nbsp;<\/p>\n<p><\/p>\n<p>\/* hello.c *\/&nbsp;<\/p>\n<p><\/p>\n<p>int main(void);&nbsp;<\/p>\n<p><\/p>\n<p>int my_print(char *string);&nbsp;<\/p>\n<p><\/p>\n<p>int my_print2(char *string);&nbsp;<\/p>\n<p>\u8fd9\u4e2a\u8f93\u51fa\u53ef\u4ee5\u91cd\u5b9a\u5411\u5230\u4e00\u4e2a\u5b9a\u4e49\u51fd\u6570\u539f\u578b\u7684\u5305\u542b\u6587\u4ef6\u91cc.&nbsp;<\/p>\n<p><\/p>\n<p>indent&nbsp;<\/p>\n<p>indent \u5b9e\u7528\u7a0b\u5e8f\u662f Linux \u91cc\u5305\u542b\u7684\u53e6\u4e00\u4e2a\u7f16\u7a0b\u5b9e\u7528\u5de5\u5177. \u8fd9\u4e2a\u5de5\u5177\u7b80\u5355\u7684\u8bf4\u5c31\u4e3a\u4f60\u7684\u4ee3\u7801\u4ea7\u751f\u7f8e\u89c2\u7684\u7f29\u8fdb\u7684\u683c\u5f0f. indent \u4e5f\u6709\u5f88\u591a\u9009\u9879\u6765\u6307\u5b9a\u5982\u4f55\u683c\u5f0f\u5316\u4f60\u7684\u6e90\u4ee3\u7801.\u8fd9\u4e9b\u9009\u9879\u7684\u66f4\u591a\u4fe1\u606f\u8bf7\u770bindent \u7684\u6307\u5357\u9875, \u5728\u547d\u4ee4\u884c\u4e0a\u952e\u5165 indent -h .&nbsp;<\/p>\n<p><\/p>\n<p><\/p>\n<p>\u4e0b\u9762\u7684\u4f8b\u5b50\u662f indent \u7684\u7f3a\u7701\u8f93\u51fa:&nbsp;<\/p>\n<p><\/p>\n<p>\u8fd0\u884c indent \u4ee5\u524d\u7684 C \u4ee3\u7801:&nbsp;<\/p>\n<p><\/p>\n<p>#include&nbsp;<\/p>\n<p><\/p>\n<p>static void my_print (char *);&nbsp;<\/p>\n<p>static void my_print2 (char *);&nbsp;<\/p>\n<p><\/p>\n<p>main ()&nbsp;<\/p>\n<p>{&nbsp;<\/p>\n<p>char my_string[] = &#8220;hello world!&#8221;;&nbsp;<\/p>\n<p>my_print (my_string);&nbsp;<\/p>\n<p>my_print2 (my_string);&nbsp;<\/p>\n<p>}&nbsp;<\/p>\n<p><\/p>\n<p>void my_print (char *string)&nbsp;<\/p>\n<p>{&nbsp;<\/p>\n<p>printf (&#8220;The string is %s &#8220;, string);&nbsp;<\/p>\n<p>}&nbsp;<\/p>\n<p><\/p>\n<p>void my_print2 (char *string)&nbsp;<\/p>\n<p>{&nbsp;<\/p>\n<p>char *string2; int size, i;&nbsp;<\/p>\n<p><\/p>\n<p>size = strlen (string);&nbsp;<\/p>\n<p>string2 = (char *) malloc (size + 1);&nbsp;<\/p>\n<p>for (i = 0; i &lt; size; i++) string2[size -1 &#8211; i] = string[i];&nbsp;<\/p>\n<p>string2[size] = ;&nbsp;<\/p>\n<p><\/p>\n<p>printf (&#8220;The string printed backward is %s &#8220;, string2);&nbsp;<\/p>\n<p>}&nbsp;<\/p>\n<p>\u8fd0\u884c indent \u540e\u7684 C \u4ee3\u7801:&nbsp;<\/p>\n<p><\/p>\n<p>#include&nbsp;<\/p>\n<p>static void my_print (char *);&nbsp;<\/p>\n<p>static void my_print2 (char *);&nbsp;<\/p>\n<p>main ()&nbsp;<\/p>\n<p>{&nbsp;<\/p>\n<p>char my_string[] = &#8220;hello world!&#8221;;&nbsp;<\/p>\n<p>my_print (my_string);&nbsp;<\/p>\n<p>my_print2 (my_string);&nbsp;<\/p>\n<p>}&nbsp;<\/p>\n<p>void&nbsp;<\/p>\n<p>my_print (char *string)&nbsp;<\/p>\n<p>{&nbsp;<\/p>\n<p>printf (&#8220;The string is %s &#8220;, string);&nbsp;<\/p>\n<p>}&nbsp;<\/p>\n<p>void&nbsp;<\/p>\n<p>my_print2 (char *string)&nbsp;<\/p>\n<p>{&nbsp;<\/p>\n<p>char *string2;&nbsp;<\/p>\n<p>int size, i;&nbsp;<\/p>\n<p>size = strlen (string);&nbsp;<\/p>\n<p>string2 = (char *) malloc (size + 1);&nbsp;<\/p>\n<p>for (i = 0; i &lt; size; i++)&nbsp;<\/p>\n<p>string2[size &#8211; 1 &#8211; i] = string[i];&nbsp;<\/p>\n<p>string2[size] = ;&nbsp;<\/p>\n<p>printf (&#8220;The string printed backward is %s &#8220;, string2);&nbsp;<\/p>\n<p>}&nbsp;<\/p>\n<p>indent \u5e76\u4e0d\u6539\u53d8\u4ee3\u7801\u7684\u5b9e\u8d28\u5185\u5bb9, \u800c\u53ea\u662f\u6539\u53d8\u4ee3\u7801\u7684\u5916\u89c2. \u4f7f\u5b83\u53d8\u5f97\u66f4\u53ef\u8bfb, \u8fd9\u6c38\u8fdc\u662f\u4e00\u4ef6\u597d\u4e8b.&nbsp;<\/p>\n<p><\/p>\n<p>gprof&nbsp;<\/p>\n<p>gprof \u662f\u5b89\u88c5\u5728\u4f60\u7684 Linux \u7cfb\u7edf\u7684 \/usr\/bin \u76ee\u5f55\u4e0b\u7684\u4e00\u4e2a\u7a0b\u5e8f. \u5b83\u4f7f\u4f60\u80fd\u5256\u6790\u4f60\u7684\u7a0b\u5e8f\u4ece\u800c\u77e5\u9053\u7a0b\u5e8f\u7684\u54ea\u4e00\u4e2a\u90e8\u5206\u5728\u6267\u884c\u65f6\u6700\u8d39\u65f6\u95f4.&nbsp;<\/p>\n<p><\/p>\n<p>gprof \u5c06\u544a\u8bc9\u4f60\u7a0b\u5e8f\u91cc\u6bcf\u4e2a\u51fd\u6570\u88ab\u8c03\u7528\u7684\u6b21\u6570\u548c\u6bcf\u4e2a\u51fd\u6570\u6267\u884c\u65f6\u6240\u5360\u65f6\u95f4\u7684\u767e\u5206\u6bd4. \u4f60\u5982\u679c\u60f3\u63d0\u9ad8\u4f60\u7684\u7a0b\u5e8f\u6027\u80fd\u7684\u8bdd\u8fd9\u4e9b\u4fe1\u606f\u975e\u5e38\u6709\u7528.&nbsp;<\/p>\n<p><\/p>\n<p>\u4e3a\u4e86\u5728\u4f60\u7684\u7a0b\u5e8f\u4e0a\u4f7f\u7528 gprof, \u4f60\u5fc5\u987b\u5728\u7f16\u8bd1\u7a0b\u5e8f\u65f6\u52a0\u4e0a -pg \u9009\u9879. \u8fd9\u5c06\u4f7f\u7a0b\u5e8f\u5728\u6bcf\u6b21\u6267\u884c\u65f6\u4ea7\u751f\u4e00\u4e2a\u53eb gmon.out \u7684\u6587\u4ef6. gprof \u7528\u8fd9\u4e2a\u6587\u4ef6\u4ea7\u751f\u5256\u6790\u4fe1\u606f.&nbsp;<\/p>\n<p><\/p>\n<p>\u5728\u4f60\u8fd0\u884c\u4e86\u4f60\u7684\u7a0b\u5e8f\u5e76\u4ea7\u751f\u4e86 gmon.out \u6587\u4ef6\u540e\u4f60\u80fd\u7528\u4e0b\u9762\u7684\u547d\u4ee4\u83b7\u5f97\u5256\u6790\u4fe1\u606f:&nbsp;<\/p>\n<p><\/p>\n<p>gprof&nbsp;<\/p>\n<p>\u53c2\u6570 program_name \u662f\u4ea7\u751f gmon.out \u6587\u4ef6\u7684\u7a0b\u5e8f\u7684\u540d\u5b57.&nbsp;<\/p>\n<p><\/p>\n<p>\u4e3a\u4e86\u8bf4\u660e\u95ee\u9898\uff0c\u5728\u7a0b\u5e8f\u4e2d\u589e\u52a0\u4e86\u51fd\u6570count_sum()\u4ee5\u6d88\u8017CPU\u65f6\u95f4\uff0c\u7a0b\u5e8f\u5982\u4e0b&nbsp;<\/p>\n<p>#include&nbsp;<\/p>\n<p><\/p>\n<p>static void my_print (char *);&nbsp;<\/p>\n<p>static void my_print2 (char *);&nbsp;<\/p>\n<p><\/p>\n<p>main ()&nbsp;<\/p>\n<p>{&nbsp;<\/p>\n<p>char my_string[] = &#8220;hello world!&#8221;;&nbsp;<\/p>\n<p>my_print (my_string);&nbsp;<\/p>\n<p>my_print2 (my_string);&nbsp;<\/p>\n<p>my_print (my_string);&nbsp;<\/p>\n<p>}&nbsp;<\/p>\n<p><\/p>\n<p>void count_sum()&nbsp;<\/p>\n<p>{&nbsp;<\/p>\n<p>int i,sum=0;&nbsp;<\/p>\n<p>for(i=0; i&lt;1000000; i++)&nbsp;<\/p>\n<p>sum += i;&nbsp;<\/p>\n<p>}&nbsp;<\/p>\n<p><\/p>\n<p>void my_print (char *string)&nbsp;<\/p>\n<p>{&nbsp;<\/p>\n<p>count_sum();&nbsp;<\/p>\n<p>printf (&#8220;The string is %s &#8220;, string);&nbsp;<\/p>\n<p>}&nbsp;<\/p>\n<p><\/p>\n<p>void my_print2 (char *string)&nbsp;<\/p>\n<p>{&nbsp;<\/p>\n<p>char *string2;&nbsp;<\/p>\n<p>int size, i,sum =0;&nbsp;<\/p>\n<p><\/p>\n<p>count_sum();&nbsp;<\/p>\n<p>size = strlen (string);&nbsp;<\/p>\n<p>string2 = (char *) malloc (size + 1);&nbsp;<\/p>\n<p>for (i = 0; i &lt; size; i++) string2[size -1 &#8211; i] = string[i];&nbsp;<\/p>\n<p>string2[size] = ;&nbsp;<\/p>\n<p>for(i=0; i&lt;5000000; i++)&nbsp;<\/p>\n<p>sum += i;&nbsp;<\/p>\n<p><\/p>\n<p>printf (&#8220;The string printed backward is %s &#8220;, string2);&nbsp;<\/p>\n<p>}&nbsp;<\/p>\n<p>$ gcc -pg -o hello hello.c&nbsp;<\/p>\n<p>$ .\/hello&nbsp;<\/p>\n<p>$ gprof hello \u00a6 more&nbsp;<\/p>\n<p>\u5c06\u4ea7\u751f\u4ee5\u4e0b\u7684\u8f93\u51fa&nbsp;<\/p>\n<p>Flat profile:&nbsp;<\/p>\n<p><\/p>\n<p>Each sample counts as 0.01 seconds.&nbsp;<\/p>\n<p>% cumulative self self total&nbsp;<\/p>\n<p>time seconds seconds calls us\/call us\/call name&nbsp;<\/p>\n<p>69.23 0.09 0.09 1 90000.00 103333.33 my_print2&nbsp;<\/p>\n<p>30.77 0.13 0.04 3 13333.33 13333.33 count_sum&nbsp;<\/p>\n<p>0.00 0.13 0.00 2 0.00 13333.33 my_print&nbsp;<\/p>\n<p><\/p>\n<p>% \u6267\u884c\u6b64\u51fd\u6570\u6240\u5360\u7528\u7684\u65f6\u95f4\u5360\u7a0b\u5e8f\u603b&nbsp;<\/p>\n<p>time \u6267\u884c\u65f6\u95f4\u7684\u767e\u5206\u6bd4&nbsp;<\/p>\n<p><\/p>\n<p>cumulative \u7d2f\u8ba1\u79d2\u6570 \u6267\u884c\u6b64\u51fd\u6570\u82b1\u8d39\u7684\u65f6\u95f4&nbsp;<\/p>\n<p>seconds \uff08\u5305\u62ec\u6b64\u51fd\u6570\u8c03\u7528\u5176\u5b83\u51fd\u6570\u82b1\u8d39\u7684\u65f6\u95f4\uff09&nbsp;<\/p>\n<p><\/p>\n<p>self \u6267\u884c\u6b64\u51fd\u6570\u82b1\u8d39\u7684\u65f6\u95f4&nbsp;<\/p>\n<p>seconds \uff08\u8c03\u7528\u5176\u5b83\u51fd\u6570\u82b1\u8d39\u7684\u65f6\u95f4\u4e0d\u8ba1\u7b97\u5728\u5185\uff09&nbsp;<\/p>\n<p><\/p>\n<p>calls \u8c03\u7528\u6b21\u6570&nbsp;<\/p>\n<p><\/p>\n<p>self \u6bcf\u6b64\u6267\u884c\u6b64\u51fd\u6570\u82b1\u8d39\u7684\u5fae\u79d2\u65f6\u95f4&nbsp;<\/p>\n<p>us\/call&nbsp;<\/p>\n<p><\/p>\n<p>total \u6bcf\u6b64\u6267\u884c\u6b64\u51fd\u6570\u52a0\u4e0a\u5b83\u8c03\u7528\u5176\u5b83\u51fd\u6570&nbsp;<\/p>\n<p>us\/call \u82b1\u8d39\u7684\u5fae\u79d2\u65f6\u95f4&nbsp;<\/p>\n<p><\/p>\n<p>name \u51fd\u6570\u540d&nbsp;<\/p>\n<p><\/p>\n<p>\u7531\u4ee5\u4e0a\u6570\u636e\u53ef\u4ee5\u770b\u51fa\uff0c\u6267\u884cmy_print()\u51fd\u6570\u672c\u8eab\u6ca1\u82b1\u8d39\u4ec0\u4e48\u65f6\u95f4\uff0c\u4f46\u662f\u5b83\u53c8\u8c03\u7528\u4e86count_sum()\u51fd\u6570\uff0c\u6240\u4ee5\u7d2f\u8ba1\u79d2\u6570\u4e3a0.13.&nbsp;<\/p>\n<p><\/p>\n<p>\u6280\u5de7: gprof \u4ea7\u751f\u7684\u5256\u6790\u6570\u636e\u5f88\u5927, \u5982\u679c\u4f60\u60f3\u68c0\u67e5\u8fd9\u4e9b\u6570\u636e\u7684\u8bdd\u6700\u597d\u628a\u8f93\u51fa\u91cd\u5b9a\u5411\u5230\u4e00\u4e2a\u6587\u4ef6\u91cc.&nbsp;<\/p>\n<p><\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Linux\u7684\u53d1\u884c\u7248\u4e2d\u5305\u542b\u4e86\u5f88\u591a\u8f6f\u4ef6\u5f00\u53d1\u5de5\u5177. \u5b83\u4eec\u4e2d\u7684\u5f88\u591a\u662f\u7528\u4e8e C \u548c C++\u5e94\u7528\u7a0b\u5e8f\u5f00\u53d1\u7684. \u672c\u6587\u4ecb\u7ecd\u4e86\u5728 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[15],"tags":[],"class_list":["post-15607","post","type-post","status-publish","format-standard","hentry","category-linux"],"_links":{"self":[{"href":"https:\/\/www.fuhaijun.com\/index.php?rest_route=\/wp\/v2\/posts\/15607"}],"collection":[{"href":"https:\/\/www.fuhaijun.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.fuhaijun.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.fuhaijun.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.fuhaijun.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=15607"}],"version-history":[{"count":0,"href":"https:\/\/www.fuhaijun.com\/index.php?rest_route=\/wp\/v2\/posts\/15607\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.fuhaijun.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=15607"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.fuhaijun.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=15607"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.fuhaijun.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=15607"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}