{"id":71889,"date":"2023-12-21T22:35:45","date_gmt":"2023-12-21T14:35:45","guid":{"rendered":"http:\/\/lrxjmw.cn\/?p=71889"},"modified":"2023-12-21T22:35:45","modified_gmt":"2023-12-21T14:35:45","slug":"debugger-principle-information","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/debugger-principle-information.html","title":{"rendered":"\u8c03\u8bd5\u5668\u5de5\u4f5c\u539f\u7406\u4e4b\uff08\u4e09\uff09\uff1a\u7814\u7a76\u8c03\u8bd5\u4fe1\u606f"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u672c\u6587\u5c06\u89e3\u91ca\u8c03\u8bd5\u5668\u662f\u5982\u4f55\u5728\u673a\u5668\u7801\u4e2d\u67e5\u627e\u5b83\u5c06 C \u8bed\u8a00\u6e90\u4ee3\u7801\u8f6c\u6362\u6210\u673a\u5668\u8bed\u8a00\u4ee3\u7801\u65f6\u6240\u9700\u8981\u7684 C \u8bed\u8a00\u51fd\u6570\u3001\u53d8\u91cf\u3001\u4e0e\u6570\u636e\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\u8fd9\u662f\u8c03\u8bd5\u5668\u7684\u5de5\u4f5c\u539f\u7406\u7cfb\u5217\u6587\u7ae0\u7684\u7b2c\u4e09\u7bc7\u3002\u9605\u8bfb\u8fd9\u7bc7\u6587\u7ae0\u4e4b\u524d\u5e94\u5f53\u5148\u9605\u8bfb\u7b2c\u4e00\u7bc7<\/a>\u4e0e\u7b2c\u4e8c\u7bc7<\/a>\u3002<\/p>\n

\u8c03\u8bd5\u4fe1\u606f<\/strong><\/div>\n

\u73b0\u4ee3\u7f16\u8bd1\u5668\u80fd\u591f\u5c06\u6709\u7740\u5404\u79cd\u7f29\u8fdb\u6216\u5d4c\u5957\u7684\u7a0b\u5e8f\u6d41\u7a0b\u3001\u5404\u79cd\u6570\u636e\u7c7b\u578b\u7684\u53d8\u91cf\u7684\u9ad8\u7ea7\u8bed\u8a00\u4ee3\u7801\u8f6c\u6362\u4e3a\u4e00\u5927\u5806\u79f0\u4e4b\u4e3a\u673a\u5668\u7801\u7684 0\/1 \u6570\u636e\uff0c\u8fd9\u4e48\u505a\u7684\u552f\u4e00\u76ee\u7684\u662f\u5c3d\u53ef\u80fd\u5feb\u7684\u5728\u76ee\u6807 CPU \u4e0a\u8fd0\u884c\u7a0b\u5e8f\u3002\u901a\u5e38\u6765\u8bf4\u4e00\u884c C \u8bed\u8a00\u4ee3\u7801\u80fd\u591f\u8f6c\u6362\u4e3a\u82e5\u5e72\u6761\u673a\u5668\u7801\u3002\u53d8\u91cf\u88ab\u5206\u6563\u5728\u673a\u5668\u7801\u4e2d\u7684\u5404\u4e2a\u90e8\u5206\uff0c\u6709\u7684\u5728\u5806\u6808\u4e2d\uff0c\u6709\u7684\u5728\u5bc4\u5b58\u5668\u4e2d\uff0c\u6216\u8005\u76f4\u63a5\u88ab\u4f18\u5316\u6389\u4e86\u3002\u6570\u636e\u7ed3\u6784\u4e0e\u5bf9\u8c61\u5728\u673a\u5668\u7801\u4e2d\u751a\u81f3\u4e0d\u201c\u5b58\u5728\u201d\uff0c\u5b83\u4eec\u53ea\u662f\u7528\u4e8e\u5c06\u6570\u636e\u6309\u4e00\u5b9a\u7684\u7ed3\u6784\u7f16\u7801\u5b58\u50a8\u8fdb\u7f13\u5b58\u3002<\/p>\n

\u90a3\u4e48\u8c03\u8bd5\u5668\u600e\u4e48\u77e5\u9053\uff0c\u5f53\u4f60\u9700\u8981\u5728\u67d0\u4e2a\u51fd\u6570\u5165\u53e3\u5904\u6682\u505c\u65f6\uff0c\u7a0b\u5e8f\u8981\u5728\u54ea\u505c\u4e0b\u6765\u5462\uff1f\u5b83\u600e\u4e48\u77e5\u9053\u5f53\u4f60\u67e5\u770b\u67d0\u4e2a\u53d8\u91cf\u503c\u65f6\uff0c\u5b83\u600e\u4e48\u627e\u5230\u8fd9\u4e2a\u503c\uff1f\u7b54\u6848\u662f\uff0c\u8c03\u8bd5\u4fe1\u606f\u3002<\/p>\n

\u7f16\u8bd1\u5668\u5728\u751f\u6210\u673a\u5668\u7801\u65f6\u540c\u65f6\u4f1a\u751f\u6210\u76f8\u5e94\u7684\u8c03\u8bd5\u4fe1\u606f\u3002\u8c03\u8bd5\u4fe1\u606f\u4ee3\u8868\u4e86\u53ef\u6267\u884c\u7a0b\u5e8f\u4e0e\u6e90\u4ee3\u7801\u4e4b\u95f4\u7684\u5173\u7cfb\uff0c\u5e76\u4ee5\u4e00\u79cd\u63d0\u524d\u5b9a\u4e49\u597d\u7684\u683c\u5f0f\uff0c\u540c\u673a\u5668\u7801\u5b58\u653e\u5728\u4e00\u8d77\u3002\u8fc7\u53bb\u7684\u6570\u5e74\u91cc\uff0c\u4eba\u4eec\u9488\u5bf9\u4e0d\u540c\u7684\u5e73\u53f0\u4e0e\u53ef\u6267\u884c\u6587\u4ef6\u53d1\u660e\u4e86\u5f88\u591a\u79cd\u7528\u4e8e\u5b58\u50a8\u8fd9\u4e9b\u4fe1\u606f\u7684\u683c\u5f0f\u3002\u4e0d\u8fc7\u6211\u4eec\u8fd9\u7bc7\u6587\u7ae0\u4e0d\u4f1a\u8bb2\u8fd9\u4e9b\u683c\u5f0f\u7684\u5386\u53f2\uff0c\u800c\u662f\u5c06\u9610\u8ff0\u8fd9\u4e9b\u8c03\u8bd5\u4fe1\u606f\u662f\u5982\u4f55\u5de5\u4f5c\u7684\uff0c\u6240\u4ee5\u6211\u4eec\u5c06\u4e13\u6ce8\u4e8e\u4e00\u4e9b\u4e8b\u60c5\uff0c\u6bd4\u5982 DWARF\u3002DWARF \u5982\u4eca\u5341\u5206\u5e7f\u6cdb\u7684\u7528\u4f5c Linux \u548c\u7c7b Unix \u5e73\u53f0\u4e0a\u7684\u53ef\u6267\u884c\u6587\u4ef6\u7684\u8c03\u8bd5\u683c\u5f0f\u3002<\/p>\n

ELF \u4e2d\u7684 DWARF<\/strong><\/div>\n

\"\"<\/a><\/p>\n

\u6839\u636e\u5b83\u7684\u7ef4\u57fa\u767e\u79d1<\/a> \u6240\u63cf\u8ff0\uff0c\u867d\u7136 DWARF \u662f\u540c ELF \u4e00\u540c\u8bbe\u8ba1\u7684\uff08DWARF \u662f\u7531 DWARF \u6807\u51c6\u59d4\u5458\u4f1a\u63a8\u51fa\u7684\u5f00\u653e\u6807\u51c6\u3002\u4e0a\u6587\u4e2d\u5c55\u793a\u7684\u56fe\u6807\u5c31\u6765\u81ea\u8fd9\u4e2a\u7f51\u7ad9\u3002\uff09\uff0c\u4f46 DWARF\u5728\u7406\u8bba\u4e0a\u6765\u8bf4\u4e5f\u53ef\u4ee5\u5d4c\u5165\u5230\u5176\u4ed6\u7684\u53ef\u6267\u884c\u6587\u4ef6\u683c\u5f0f\u4e2d\u3002<\/p>\n

DWARF \u662f\u4e00\u79cd\u590d\u6742\u7684\u683c\u5f0f\uff0c\u5b83\u5438\u6536\u4e86\u8fc7\u53bb\u8bb8\u591a\u5e74\u5404\u79cd\u4e0d\u540c\u7684\u67b6\u6784\u4e0e\u64cd\u4f5c\u7cfb\u7edf\u7684\u683c\u5f0f\u7684\u7ecf\u9a8c\u3002\u6b63\u662f\u56e0\u4e3a\u5b83\u89e3\u51b3\u4e86\u4e00\u4e2a\u5728\u4efb\u4f55\u5e73\u53f0\u4e0e ABI \uff08\u5e94\u7528\u4e8c\u8fdb\u5236\u63a5\u53e3\uff09\u4e0a\u4e3a\u4efb\u610f\u9ad8\u7ea7\u8bed\u8a00\u4ea7\u751f\u8c03\u8bd5\u4fe1\u606f\u8fd9\u6837\u68d8\u624b\u7684\u96be\u9898\uff0c\u5b83\u4e5f\u5fc5\u987b\u5f88\u590d\u6742\u3002\u60f3\u8981\u900f\u5f7b\u7684\u8bb2\u89e3 DWARF \u4ec5\u4ec5\u662f\u901a\u8fc7\u8fd9\u5355\u8584\u7684\u4e00\u7bc7\u6587\u7ae0\u662f\u8fdc\u8fdc\u4e0d\u591f\u7684\uff0c\u8bf4\u5b9e\u8bdd\u6211\u4e5f\u5e76\u6ca1\u6709\u5145\u5206\u5730\u4e86\u89e3 DWARF \u5230\u6bcf\u4e00\u4e2a\u5fae\u5c0f\u7684\u7ec6\u8282\uff0c\u6240\u4ee5\u6211\u4e5f\u4e0d\u80fd\u5341\u5206\u900f\u5f7b\u7684\u8bb2\u89e3 \uff08\u5982\u679c\u4f60\u611f\u5174\u8da3\u7684\u8bdd\uff0c\u6587\u672b\u6709\u4e00\u4e9b\u80fd\u591f\u5e2e\u52a9\u4f60\u7684\u8d44\u6e90\u3002\u5efa\u8bae\u4ece DWARF \u6559\u7a0b\u5f00\u59cb\u4e0a\u624b\uff09\u3002\u8fd9\u7bc7\u6587\u7ae0\u4e2d\u6211\u5c06\u4ee5\u6d45\u663e\u6613\u61c2\u7684\u65b9\u5f0f\u5c55\u793a DWARF\uff0c\u4ee5\u8bf4\u660e\u8c03\u8bd5\u4fe1\u606f\u662f\u5982\u4f55\u5b9e\u9645\u5de5\u4f5c\u7684\u3002<\/p>\n

ELF \u6587\u4ef6\u4e2d\u7684\u8c03\u8bd5\u90e8\u5206<\/strong><\/div>\n

\u9996\u5148\u8ba9\u6211\u4eec\u770b\u770bDWARF \u5904\u5728 ELF \u6587\u4ef6\u4e2d\u7684\u4ec0\u4e48\u4f4d\u7f6e\u3002ELF< \u5b9a\u4e49\u4e86\u6bcf\u4e00\u4e2a\u751f\u6210\u7684\u76ee\u6807\u6587\u4ef6\u4e2d\u7684\u6bcf\u4e00\u8282\u3002 \u8282\u5934\u8868section header table \u58f0\u660e\u5e76\u5b9a\u4e49\u4e86\u6bcf\u4e00\u8282\u53ca\u5176\u540d\u5b57\u3002\u4e0d\u540c\u7684\u5de5\u5177\u4ee5\u4e0d\u540c\u7684\u65b9\u5f0f\u5904\u7406\u4e0d\u540c\u7684\u8282\uff0c\u4f8b\u5982\u8fde\u63a5\u5668\u4f1a\u5bfb\u627e\u8fde\u63a5\u5668\u9700\u8981\u7684\u90e8\u5206\uff0c\u8c03\u8bd5\u5668\u4f1a\u67e5\u627e\u8c03\u8bd5\u5668\u9700\u8981\u7684\u90e8\u5206\u3002<\/p>\n

\u6211\u4eec\u672c\u6587\u7684\u5b9e\u9a8c\u4f1a\u4f7f\u7528\u4ece\u8fd9\u4e2a C \u8bed\u8a00\u6e90\u6587\u4ef6\u6784\u5efa\u7684\u53ef\u6267\u884c\u6587\u4ef6\uff0c\u7f16\u8bd1\u6210 tracedprog2\uff1a<\/p>\n

#include <stdio.h>\r\n\r\nvoid do_stuff(int my_arg)\u3001\r\n{\r\n    int my_local = my_arg + 2;\r\n    int i;\r\n\r\n    for (i = 0; i < my_local; ++i)\r\n        printf(\"i = %d\/n\", i);\r\n}\r\n\r\nint main()\r\n{\r\n    do_stuff(2);\r\n    return 0;\r\n}\r\n<\/pre>\n

\u4f7f\u7528 objdump -h \u547d\u4ee4\u68c0\u67e5 ELF \u53ef\u6267\u884c\u6587\u4ef6\u4e2d\u7684\u8282\u5934section header<\/rt><\/ruby>\uff0c\u6211\u4eec\u4f1a\u770b\u5230\u51e0\u4e2a\u4ee5 .debug_ \u5f00\u5934\u7684\u8282\uff0c\u8fd9\u4e9b\u5c31\u662f DWARF \u7684\u8c03\u8bd5\u90e8\u5206\u3002<\/p>\n

26 .debug_aranges 00000020  00000000  00000000  00001037\r\n                 CONTENTS, READONLY, DEBUGGING\r\n27 .debug_pubnames 00000028  00000000  00000000  00001057\r\n                 CONTENTS, READONLY, DEBUGGING\r\n28 .debug_info   000000cc  00000000  00000000  0000107f\r\n                 CONTENTS, READONLY, DEBUGGING\r\n29 .debug_abbrev 0000008a  00000000  00000000  0000114b\r\n                 CONTENTS, READONLY, DEBUGGING\r\n30 .debug_line   0000006b  00000000  00000000  000011d5\r\n                 CONTENTS, READONLY, DEBUGGING\r\n31 .debug_frame  00000044  00000000  00000000  00001240\r\n                 CONTENTS, READONLY, DEBUGGING\r\n32 .debug_str    000000ae  00000000  00000000  00001284\r\n                 CONTENTS, READONLY, DEBUGGING\r\n33 .debug_loc    00000058  00000000  00000000  00001332\r\n                 CONTENTS, READONLY, DEBUGGING\r\n<\/pre>\n

\u6bcf\u4e2a\u8282\u7684\u7b2c\u4e00\u4e2a\u6570\u5b57\u4ee3\u8868\u4e86\u8be5\u8282\u7684\u5927\u5c0f\uff0c\u6700\u540e\u4e00\u4e2a\u6570\u5b57\u4ee3\u8868\u4e86\u8fd9\u4e2a\u8282\u5f00\u59cb\u4f4d\u7f6e\u8ddd\u79bb ELF \u7684\u504f\u79fb\u91cf\u3002\u8c03\u8bd5\u5668\u5229\u7528\u8fd9\u4e9b\u4fe1\u606f\u4ece\u53ef\u6267\u884c\u6587\u4ef6\u4e2d\u8bfb\u53d6\u8282\u3002<\/p>\n

\u73b0\u5728\u8ba9\u6211\u4eec\u770b\u770b\u4e00\u4e9b\u5728 DWARF \u4e2d\u67e5\u627e\u6709\u7528\u7684\u8c03\u8bd5\u4fe1\u606f\u7684\u5b9e\u9645\u4f8b\u5b50\u3002<\/p>\n

\u67e5\u627e\u51fd\u6570<\/strong><\/div>\n

\u8c03\u8bd5\u5668\u7684\u6700\u57fa\u7840\u7684\u4efb\u52a1\u4e4b\u4e00\uff0c\u5c31\u662f\u5f53\u6211\u4eec\u5728\u67d0\u4e2a\u51fd\u6570\u5904\u8bbe\u7f6e\u65ad\u70b9\u65f6\uff0c\u8c03\u8bd5\u5668\u9700\u8981\u80fd\u591f\u5728\u5165\u53e3\u5904\u6682\u505c\u3002\u4e3a\u6b64\uff0c\u5fc5\u987b\u4e3a\u9ad8\u7ea7\u4ee3\u7801\u4e2d\u7684\u51fd\u6570\u540d\u79f0\u4e0e\u51fd\u6570\u5728\u673a\u5668\u7801\u4e2d\u6307\u4ee4\u5f00\u59cb\u7684\u5730\u5740\u8fd9\u4e24\u8005\u4e4b\u95f4\u5efa\u7acb\u8d77\u67d0\u79cd\u6620\u5c04\u5173\u7cfb\u3002<\/p>\n

\u4e3a\u4e86\u83b7\u53d6\u8fd9\u79cd\u6620\u5c04\u5173\u7cfb\uff0c\u6211\u4eec\u53ef\u4ee5\u67e5\u627e DWARF\u4e2d\u7684 .debug_info \u8282\u3002\u5728\u6211\u4eec\u6df1\u5165\u4e4b\u524d\uff0c\u9700\u8981\u4e00\u70b9\u57fa\u7840\u77e5\u8bc6\u3002DWARF \u4e2d\u6bcf\u4e00\u4e2a\u63cf\u8ff0\u7c7b\u578b\u88ab\u79f0\u4e4b\u4e3a\u8c03\u8bd5\u4fe1\u606f\u5165\u53e3\uff08DIE\uff09\u3002\u6bcf\u4e2a DIE \u90fd\u6709\u5173\u4e8e\u5b83\u7684\u7c7b\u578b\u3001\u5c5e\u6027\u4e4b\u7c7b\u7684\u6807\u7b7e\u3002DIE \u4e4b\u95f4\u901a\u8fc7\u5144\u5f1f\u8282\u70b9\u6216\u5b50\u8282\u70b9\u76f8\u4e92\u8fde\u63a5\uff0c\u5c5e\u6027\u7684\u503c\u4e5f\u53ef\u4ee5\u6307\u5411\u5176\u5b83\u7684 DIE\u3002<\/p>\n

\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n

objdump --dwarf=info tracedprog2\r\n<\/pre>\n

\u8f93\u51fa\u6587\u4ef6\u76f8\u5f53\u7684\u957f\uff0c\u4e3a\u4e86\u65b9\u4fbf\u4e3e\u4f8b\u6211\u4eec\u53ea\u5173\u6ce8\u8fd9\u4e9b\u884c\uff08\u4ece\u8fd9\u91cc\u5f00\u59cb\uff0c\u65e0\u7528\u7684\u5197\u957f\u4fe1\u606f\u6211\u4f1a\u4ee5 \uff08...\uff09\u4ee3\u66ff\uff0c\u65b9\u4fbf\u6392\u7248\uff09\uff1a<\/p>\n

<1><71>: Abbrev Number: 5 (DW_TAG_subprogram)\r\n    <72>   DW_AT_external    : 1\r\n    <73>   DW_AT_name        : (...): do_stuff\r\n    <77>   DW_AT_decl_file   : 1\r\n    <78>   DW_AT_decl_line   : 4\r\n    <79>   DW_AT_prototyped  : 1\r\n    <7a>   DW_AT_low_pc      : 0x8048604\r\n    <7e>   DW_AT_high_pc     : 0x804863e\r\n    <82>   DW_AT_frame_base  : 0x0      (location list)\r\n    <86>   DW_AT_sibling     : <0xb3>\r\n\r\n<1><b3>: Abbrev Number: 9 (DW_TAG_subprogram)\r\n    <b4>   DW_AT_external    : 1\r\n    <b5>   DW_AT_name        : (...): main\r\n    <b9>   DW_AT_decl_file   : 1\r\n    <ba>   DW_AT_decl_line   : 14\r\n    <bb>   DW_AT_type        : <0x4b>\r\n    <bf>   DW_AT_low_pc      : 0x804863e\r\n    <c3>   DW_AT_high_pc     : 0x804865a\r\n    <c7>   DW_AT_frame_base  : 0x2c     (location list)\r\n<\/pre>\n

\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\u6709\u4e24\u4e2a\u5e26\u6709 DW_TAG_subprogram \u6807\u7b7e\u7684\u5165\u53e3\uff0c\u5728 DWARF \u4e2d\u8fd9\u662f\u5bf9\u51fd\u6570\u7684\u6307\u4ee3\u3002\u6ce8\u610f\uff0c\u8fd9\u662f\u4e24\u4e2a\u8282\u7684\u5165\u53e3\uff0c\u5176\u4e2d\u4e00\u4e2a\u662f do_stuff \u51fd\u6570\u7684\u5165\u53e3\uff0c\u53e6\u4e00\u4e2a\u662f\u4e3b\uff08main\uff09\u51fd\u6570\u7684\u5165\u53e3\u3002\u8fd9\u4e9b\u4fe1\u606f\u4e2d\u6709\u5f88\u591a\u503c\u5f97\u5173\u6ce8\u7684\u5c5e\u6027\uff0c\u4f46\u5176\u4e2d\u6700\u503c\u5f97\u6ce8\u610f\u7684\u662f DW_AT_low_pc\u3002\u5b83\u4ee3\u8868\u4e86\u51fd\u6570\u5f00\u59cb\u5904\u7a0b\u5e8f\u6307\u9488\u7684\u503c\uff08\u5728 x86 \u5e73\u53f0\u4e0a\u662f EIP\uff09\u3002\u6b64\u5904 0x8048604 \u4ee3\u8868\u4e86 do_stuff \u51fd\u6570\u5f00\u59cb\u5904\u7684\u7a0b\u5e8f\u6307\u9488\u3002\u4e0b\u9762\u6211\u4eec\u5c06\u5229\u7528 objdump -d \u547d\u4ee4\u5bf9\u53ef\u6267\u884c\u6587\u4ef6\u8fdb\u884c\u53cd\u6c47\u7f16\u3002\u6765\u770b\u770b\u8fd9\u5757\u5730\u5740\u4e2d\u90fd\u6709\u4ec0\u4e48\uff1a<\/p>\n

08048604 <do_stuff>:\r\n 8048604:       55           push   ebp\r\n 8048605:       89 e5        mov    ebp,esp\r\n 8048607:       83 ec 28     sub    esp,0x28\r\n 804860a:       8b 45 08     mov    eax,DWORD PTR [ebp+0x8]\r\n 804860d:       83 c0 02     add    eax,0x2\r\n 8048610:       89 45 f4     mov    DWORD PTR [ebp-0xc],eax\r\n 8048613:       c7 45 (...)  mov    DWORD PTR [ebp-0x10],0x0\r\n 804861a:       eb 18        jmp    8048634 <do_stuff+0x30>\r\n 804861c:       b8 20 (...)  mov    eax,0x8048720\r\n 8048621:       8b 55 f0     mov    edx,DWORD PTR [ebp-0x10]\r\n 8048624:       89 54 24 04  mov    DWORD PTR [esp+0x4],edx\r\n 8048628:       89 04 24     mov    DWORD PTR [esp],eax\r\n 804862b:       e8 04 (...)  call   8048534 <printf@plt>\r\n 8048630:       83 45 f0 01  add    DWORD PTR [ebp-0x10],0x1\r\n 8048634:       8b 45 f0     mov    eax,DWORD PTR [ebp-0x10]\r\n 8048637:       3b 45 f4     cmp    eax,DWORD PTR [ebp-0xc]\r\n 804863a:       7c e0        jl     804861c <do_stuff+0x18>\r\n 804863c:       c9           leave\r\n 804863d:       c3           ret\r\n<\/pre>\n

\u663e\u7136\uff0c0x8048604 \u662f do_stuff \u7684\u5f00\u59cb\u5730\u5740\uff0c\u8fd9\u6837\u4e00\u6765\uff0c\u8c03\u8bd5\u5668\u5c31\u53ef\u4ee5\u5efa\u7acb\u51fd\u6570\u4e0e\u5176\u5728\u53ef\u6267\u884c\u6587\u4ef6\u4e2d\u7684\u4f4d\u7f6e\u95f4\u7684\u6620\u5c04\u5173\u7cfb\u3002<\/p>\n

\u67e5\u627e\u53d8\u91cf<\/strong><\/div>\n

\u5047\u8bbe\u6211\u4eec\u5f53\u524d\u5728 do_staff \u51fd\u6570\u4e2d\u67d0\u4e2a\u4f4d\u7f6e\u4e0a\u8bbe\u7f6e\u65ad\u70b9\u505c\u4e86\u4e0b\u6765\u3002\u6211\u4eec\u60f3\u901a\u8fc7\u8c03\u8bd5\u5668\u53d6\u5f97 my_local \u8fd9\u4e2a\u53d8\u91cf\u7684\u503c\u3002\u8c03\u8bd5\u5668\u600e\u4e48\u77e5\u9053\u5728\u54ea\u91cc\u53bb\u627e\u8fd9\u4e2a\u503c\u5462\uff1f\u5f88\u663e\u7136\u8fd9\u8981\u6bd4\u67e5\u627e\u51fd\u6570\u66f4\u4e3a\u56f0\u96be\u3002\u53d8\u91cf\u53ef\u80fd\u5b58\u50a8\u5728\u5168\u5c40\u5b58\u50a8\u533a\u3001\u5806\u6808\u3001\u751a\u81f3\u662f\u5bc4\u5b58\u5668\u4e2d\u3002\u6b64\u5916\uff0c\u540c\u540d\u53d8\u91cf\u5728\u4e0d\u540c\u7684\u4f5c\u7528\u57df\u4e2d\u53ef\u80fd\u6709\u7740\u4e0d\u540c\u7684\u503c\u3002\u8c03\u8bd5\u4fe1\u606f\u5fc5\u987b\u80fd\u591f\u53cd\u6620\u6240\u6709\u7684\u8fd9\u4e9b\u53d8\u5316\uff0c\u5f53\u7136\uff0cDWARF \u5c31\u80fd\u505a\u5230\u3002<\/p>\n

\u6211\u4e0d\u4f1a\u9010\u4e00\u53bb\u5c06\u6bcf\u4e00\u79cd\u53ef\u80fd\u7684\u72b6\u51b5\uff0c\u4f46\u6211\u4f1a\u4ee5\u8c03\u8bd5\u5668\u5728 do_stuff \u51fd\u6570\u4e2d\u67e5\u627e my_local \u53d8\u91cf\u7684\u8fc7\u7a0b\u6765\u4e3e\u4e2a\u4f8b\u5b50\u3002\u4e0b\u9762\u6211\u4eec\u518d\u770b\u4e00\u904d .debug_info \u4e2d do_stuff \u7684\u6bcf\u4e00\u4e2a\u5165\u53e3\uff0c\u8fd9\u6b21\u8fde\u5b83\u7684\u5b50\u5165\u53e3\u4e5f\u8981\u4e00\u8d77\u770b\u3002<\/p>\n

<1><71>: Abbrev Number: 5 (DW_TAG_subprogram)\r\n    <72>   DW_AT_external    : 1\r\n    <73>   DW_AT_name        : (...): do_stuff\r\n    <77>   DW_AT_decl_file   : 1\r\n    <78>   DW_AT_decl_line   : 4\r\n    <79>   DW_AT_prototyped  : 1\r\n    <7a>   DW_AT_low_pc      : 0x8048604\r\n    <7e>   DW_AT_high_pc     : 0x804863e\r\n    <82>   DW_AT_frame_base  : 0x0      (location list)\r\n    <86>   DW_AT_sibling     : <0xb3>\r\n <2><8a>: Abbrev Number: 6 (DW_TAG_formal_parameter)\r\n    <8b>   DW_AT_name        : (...): my_arg\r\n    <8f>   DW_AT_decl_file   : 1\r\n    <90>   DW_AT_decl_line   : 4\r\n    <91>   DW_AT_type        : <0x4b>\r\n    <95>   DW_AT_location    : (...)       (DW_OP_fbreg: 0)\r\n <2><98>: Abbrev Number: 7 (DW_TAG_variable)\r\n    <99>   DW_AT_name        : (...): my_local\r\n    <9d>   DW_AT_decl_file   : 1\r\n    <9e>   DW_AT_decl_line   : 6\r\n    <9f>   DW_AT_type        : <0x4b>\r\n    <a3>   DW_AT_location    : (...)      (DW_OP_fbreg: -20)\r\n<2><a6>: Abbrev Number: 8 (DW_TAG_variable)\r\n    <a7>   DW_AT_name        : i\r\n    <a9>   DW_AT_decl_file   : 1\r\n    <aa>   DW_AT_decl_line   : 7\r\n    <ab>   DW_AT_type        : <0x4b>\r\n    <af>   DW_AT_location    : (...)      (DW_OP_fbreg: -24)\r\n<\/pre>\n

\u770b\u5230\u6bcf\u4e2a\u5165\u53e3\u5904\u7b2c\u4e00\u5bf9\u5c16\u62ec\u53f7\u4e2d\u7684\u6570\u5b57\u4e86\u5417\uff1f\u8fd9\u4e9b\u662f\u5d4c\u5957\u7684\u7b49\u7ea7\uff0c\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u4ee5 <2> \u5f00\u5934\u7684\u5165\u53e3\u662f\u4ee5 <1> \u5f00\u5934\u7684\u5b50\u5165\u53e3\u3002\u56e0\u6b64\u6211\u4eec\u5f97\u77e5 my_local \u53d8\u91cf\uff08\u4ee5 DW_TAG_variable \u6807\u7b7e\u6807\u8bb0\uff09\u662f do_stuff \u51fd\u6570\u7684\u5c40\u90e8\u53d8\u91cf\u3002\u9664\u6b64\u4e4b\u5916\uff0c\u8c03\u8bd5\u5668\u4e5f\u9700\u8981\u77e5\u9053\u53d8\u91cf\u7684\u6570\u636e\u7c7b\u578b\uff0c\u8fd9\u6837\u624d\u80fd\u6b63\u786e\u7684\u4f7f\u7528\u4e0e\u663e\u793a\u53d8\u91cf\u3002\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d my_local \u7684\u53d8\u91cf\u7c7b\u578b\u6307\u5411\u53e6\u4e00\u4e2a DIE <0x4b>\u3002\u5982\u679c\u4f7f\u7528 objdump \u547d\u4ee4\u67e5\u770b\u8fd9\u4e2a DIE \u7684\u8bdd\uff0c\u6211\u4eec\u4f1a\u53d1\u73b0\u5b83\u662f\u4e00\u4e2a\u6709\u7b26\u53f7 4 \u5b57\u8282\u6574\u578b\u6570\u636e\u3002<\/p>\n

\u800c\u4e3a\u4e86\u5728\u5b9e\u9645\u8fd0\u884c\u7684\u7a0b\u5e8f\u5185\u5b58\u4e2d\u67e5\u627e\u53d8\u91cf\u7684\u503c\uff0c\u8c03\u8bd5\u5668\u9700\u8981\u4f7f\u7528\u5230 DW_AT_location \u5c5e\u6027\u3002\u5bf9\u4e8e my_local \u800c\u8a00\uff0c\u662f DW_OP_fbreg: -20\u3002\u8fd9\u4e2a\u4ee3\u7801\u6bb5\u7684\u610f\u601d\u662f\u8bf4 my_local \u5b58\u50a8\u5728\u8ddd\u79bb\u5b83\u6240\u5728\u51fd\u6570\u8d77\u59cb\u5730\u5740\u504f\u79fb\u91cf\u4e3a -20 \u7684\u5730\u65b9\u3002<\/p>\n

do_stuff \u51fd\u6570\u7684 DW_AT_frame_base \u5c5e\u6027\u503c\u4e3a 0x0 (location list)\u3002\u8fd9\u610f\u5473\u7740\u8fd9\u4e2a\u5c5e\u6027\u7684\u503c\u9700\u8981\u5728 location list \u4e2d\u67e5\u627e\u3002\u4e0b\u9762\u6211\u4eec\u6765\u4e00\u8d77\u770b\u770b\u3002<\/p>\n

$ objdump --dwarf=loc tracedprog2\r\n\r\ntracedprog2:     file format elf32-i386\r\n\r\nContents of the .debug_loc section:\r\n\r\n    Offset   Begin    End      Expression\r\n    00000000 08048604 08048605 (DW_OP_breg4: 4 )\r\n    00000000 08048605 08048607 (DW_OP_breg4: 8 )\r\n    00000000 08048607 0804863e (DW_OP_breg5: 8 )\r\n    00000000 <End of list>\r\n    0000002c 0804863e 0804863f (DW_OP_breg4: 4 )\r\n    0000002c 0804863f 08048641 (DW_OP_breg4: 8 )\r\n    0000002c 08048641 0804865a (DW_OP_breg5: 8 )\r\n    0000002c <End of list>\r\n<\/pre>\n

\u6211\u4eec\u9700\u8981\u5173\u6ce8\u7684\u662f\u7b2c\u4e00\u5217\uff08do_stuff \u51fd\u6570\u7684 DW_AT_frame_base \u5c5e\u6027\u5305\u542b location list \u4e2d 0x0 \u7684\u504f\u79fb\u91cf\u3002\u800c main \u51fd\u6570\u7684\u76f8\u540c\u5c5e\u6027\u5305\u542b 0x2c \u7684\u504f\u79fb\u91cf\uff0c\u8fd9\u4e2a\u504f\u79fb\u91cf\u662f\u7b2c\u4e8c\u5957\u5730\u5740\u5217\u8868\u7684\u504f\u79fb\u91cf\uff09\u3002\u5bf9\u4e8e\u8c03\u8bd5\u5668\u53ef\u80fd\u5b9a\u4f4d\u5230\u7684\u6bcf\u4e00\u4e2a\u5730\u5740\uff0c\u5b83\u90fd\u4f1a\u6307\u5b9a\u5f53\u524d\u6808\u5e27\u5230\u53d8\u91cf\u95f4\u7684\u504f\u79fb\u91cf\uff0c\u800c\u8fd9\u4e2a\u504f\u79fb\u5c31\u662f\u901a\u8fc7\u5bc4\u5b58\u5668\u6765\u8ba1\u7b97\u7684\u3002\u5bf9\u4e8e x86 \u5e73\u53f0\u800c\u8a00\uff0cbpreg4 \u6307\u5411 esp\uff0c\u800c bpreg5 \u6307\u5411 ebp\u3002<\/p>\n

\u8ba9\u6211\u4eec\u518d\u770b\u770b do_stuff \u51fd\u6570\u7684\u5934\u51e0\u6761\u6307\u4ee4\u3002<\/p>\n

08048604 <do_stuff>:\r\n 8048604:       55          push   ebp\r\n 8048605:       89 e5       mov    ebp,esp\r\n 8048607:       83 ec 28    sub    esp,0x28\r\n 804860a:       8b 45 08    mov    eax,DWORD PTR [ebp+0x8]\r\n 804860d:       83 c0 02    add    eax,0x2\r\n 8048610:       89 45 f4    mov    DWORD PTR [ebp-0xc],eax\r\n<\/pre>\n

\u53ea\u6709\u5f53\u7b2c\u4e8c\u6761\u6307\u4ee4\u6267\u884c\u540e\uff0cebp \u5bc4\u5b58\u5668\u624d\u771f\u6b63\u5b58\u50a8\u4e86\u6709\u7528\u7684\u503c\u3002\u5f53\u7136\uff0c\u524d\u4e24\u6761\u6307\u4ee4\u7684\u57fa\u5740\u662f\u7531\u4e0a\u9762\u6240\u5217\u51fa\u6765\u7684\u5730\u5740\u4fe1\u606f\u8868\u8ba1\u7b97\u51fa\u6765\u7684\u3002\u4e00\u4f46 ebp \u786e\u5b9a\u4e86\uff0c\u8ba1\u7b97\u504f\u79fb\u91cf\u5c31\u5341\u5206\u65b9\u4fbf\u4e86\uff0c\u56e0\u4e3a\u5c3d\u7ba1 esp \u5728\u64cd\u4f5c\u5806\u6808\u7684\u65f6\u5019\u9700\u8981\u79fb\u52a8\uff0c\u4f46 ebp \u4f5c\u4e3a\u6808\u5e95\u5e76\u4e0d\u9700\u8981\u79fb\u52a8\u3002<\/p>\n

\u7a76\u7adf\u6211\u4eec\u5e94\u8be5\u53bb\u54ea\u91cc\u627e my_local \u7684\u503c\u5462\uff1f\u5728 0x8048610 \u8fd9\u5757\u5730\u5740\u540e\uff0c my_local \u7684\u503c\u7ecf\u8fc7\u5728 eax \u4e2d\u7684\u8ba1\u7b97\u540e\u88ab\u5b58\u5728\u4e86\u5185\u5b58\u4e2d\uff0c\u4ece\u8fd9\u91cc\u5f00\u59cb\u6211\u4eec\u624d\u9700\u8981\u5173\u6ce8 my_local \u7684\u503c\u3002\u8c03\u8bd5\u5668\u4f1a\u5229\u7528 DW_OP_breg5: 8 \u8fd9\u4e2a\u6808\u5e27\u6765\u67e5\u627e\u3002\u6211\u4eec\u56de\u60f3\u4e0b\uff0cmy_local \u7684 DW_AT_location \u5c5e\u6027\u503c\u4e3a DW_OP_fbreg: -20\u3002\u6240\u4ee5\u5e94\u5f53\u4ece\u57fa\u5740\u4e2d -20 \uff0c\u540c\u65f6\u7531\u4e8e ebp \u5bc4\u5b58\u5668\u9700\u8981 +8\uff0c\u6240\u4ee5\u6700\u7ec8\u7ed3\u679c\u4e3a ebp - 12\u3002\u73b0\u5728\u518d\u6b21\u67e5\u770b\u53cd\u6c47\u7f16\u4ee3\u7801\uff0c\u6765\u770b\u770b\u6570\u636e\u4ece eax \u4e2d\u88ab\u79fb\u52a8\u5230\u54ea\u91cc\u4e86\u3002\u5f53\u7136\uff0c\u8fd9\u91cc my_local \u5e94\u5f53\u88ab\u5b58\u50a8\u5728\u4e86 ebp - 12 \u7684\u5730\u5740\u4e2d\u3002<\/p>\n

\u67e5\u770b\u884c\u53f7<\/strong><\/div>\n

\u5f53\u6211\u4eec\u8c08\u5230\u5728\u8c03\u8bd5\u4fe1\u606f\u5bfb\u627e\u51fd\u6570\u7684\u65f6\u5019\uff0c\u6211\u4eec\u5229\u7528\u4e86\u4e9b\u6280\u5de7\u3002\u5f53\u8c03\u8bd5 C \u8bed\u8a00\u6e90\u4ee3\u7801\u5e76\u5728\u67d0\u4e2a\u51fd\u6570\u51fa\u653e\u7f6e\u65ad\u70b9\u7684\u65f6\u5019\uff0c\u6211\u4eec\u5e76\u4e0d\u5173\u6ce8\u7b2c\u4e00\u6761\u201c\u673a\u5668\u7801\u201d\u6307\u4ee4\uff08\u51fd\u6570\u7684\u8c03\u7528\u51c6\u5907\u5de5\u4f5c\u5df2\u7ecf\u5b8c\u6210\u800c\u5c40\u90e8\u53d8\u91cf\u8fd8\u6ca1\u6709\u521d\u59cb\u5316\uff09\u3002\u6211\u4eec\u771f\u6b63\u5173\u6ce8\u7684\u662f\u51fd\u6570\u7684\u7b2c\u4e00\u884c\u201cC \u4ee3\u7801\u201d\u3002<\/p>\n

\u8fd9\u5c31\u662f DWARF \u5b8c\u5168\u8986\u76d6\u6620\u5c04 C \u6e90\u4ee3\u7801\u4e2d\u7684\u884c\u4e0e\u53ef\u6267\u884c\u6587\u4ef6\u4e2d\u673a\u5668\u7801\u5730\u5740\u7684\u539f\u56e0\u3002\u4e0b\u9762\u662f .debug_line \u8282\u4e2d\u6240\u5305\u542b\u7684\u5185\u5bb9\uff0c\u6211\u4eec\u5c06\u5176\u8f6c\u6362\u4e3a\u53ef\u8bfb\u7684\u683c\u5f0f\u5c55\u793a\u5982\u4e0b\u3002<\/p>\n

$ objdump --dwarf=decodedline tracedprog2\r\n\r\ntracedprog2:     file format elf32-i386\r\n\r\nDecoded dump of debug contents of section .debug_line:\r\n\r\nCU: \/home\/eliben\/eli\/eliben-code\/debugger\/tracedprog2.c:\r\nFile name           Line number    Starting address\r\ntracedprog2.c                5           0x8048604\r\ntracedprog2.c                6           0x804860a\r\ntracedprog2.c                9           0x8048613\r\ntracedprog2.c               10           0x804861c\r\ntracedprog2.c                9           0x8048630\r\ntracedprog2.c               11           0x804863c\r\ntracedprog2.c               15           0x804863e\r\ntracedprog2.c               16           0x8048647\r\ntracedprog2.c               17           0x8048653\r\ntracedprog2.c               18           0x8048658\r\n<\/pre>\n

\u5f88\u5bb9\u6613\u5c31\u53ef\u4ee5\u770b\u51fa\u5176\u4e2d C \u6e90\u4ee3\u7801\u4e0e\u53cd\u6c47\u7f16\u4ee3\u7801\u4e4b\u95f4\u7684\u5bf9\u5e94\u5173\u7cfb\u3002\u7b2c 5 \u884c\u6307\u5411do_stuff \u51fd\u6570\u7684\u5165\u53e3\uff0c0x8040604\u3002\u7b2c 6 \u884c\uff0c\u6307\u5411 0x804860a \uff0c\u6b63\u662f\u8c03\u8bd5\u5668\u5728\u8c03\u8bd5 do_stuff \u51fd\u6570\u65f6\u9700\u8981\u505c\u4e0b\u6765\u7684\u5730\u65b9\u3002\u8fd9\u91cc\u5df2\u7ecf\u5b8c\u6210\u4e86\u51fd\u6570\u8c03\u7528\u7684\u51c6\u5907\u5de5\u4f5c\u3002\u4e0a\u9762\u7684\u8fd9\u4e9b\u4fe1\u606f\u5f62\u6210\u4e86\u884c\u53f7\u4e0e\u5730\u5740\u95f4\u7684\u53cc\u5411\u6620\u5c04\u5173\u7cfb\u3002<\/p>\n