{"id":85819,"date":"2023-12-26T13:23:39","date_gmt":"2023-12-26T05:23:39","guid":{"rendered":"http:\/\/lrxjmw.cn\/?p=85819"},"modified":"2023-12-26T13:23:39","modified_gmt":"2023-12-26T05:23:39","slug":"linux-debugger-stack","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/linux-debugger-stack.html","title":{"rendered":"Linux \u8c03\u8bd5\u5668\u4e4b\u5806\u6808\u5c55\u5f00\uff01"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u6709\u65f6\u4f60\u9700\u8981\u77e5\u9053\u7684\u6700\u91cd\u8981\u7684\u4fe1\u606f\u662f\u4ec0\u4e48\uff0c\u4f60\u5f53\u524d\u7684\u7a0b\u5e8f\u72b6\u6001\u662f\u5982\u4f55\u5230\u8fbe\u90a3\u91cc\u7684\u3002\u6709\u4e00\u4e2a backtrace \u547d\u4ee4\uff0c\u5b83\u7ed9\u4f60\u63d0\u4f9b\u4e86\u7a0b\u5e8f\u5f53\u524d\u7684\u51fd\u6570\u8c03\u7528\u94fe\u3002\u8fd9\u7bc7\u6587\u7ae0\u5c06\u5411\u4f60\u5c55\u793a\u5982\u4f55\u5728 x86_64 \u4e0a\u5b9e\u73b0\u5806\u6808\u5c55\u5f00\u4ee5\u751f\u6210\u8fd9\u6837\u7684\u56de\u6eaf\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n
\u7cfb\u5217\u7d22\u5f15<\/strong><\/div>\n

\u8fd9\u4e9b\u94fe\u63a5\u5c06\u4f1a\u968f\u7740\u5176\u4ed6\u5e16\u5b50\u7684\u53d1\u5e03\u800c\u4e0a\u7ebf\u3002<\/p>\n

    \n
  1. \u51c6\u5907\u73af\u5883<\/li>\n
  2. \u65ad\u70b9<\/li>\n
  3. \u5bc4\u5b58\u5668\u548c\u5185\u5b58<\/li>\n
  4. ELF \u548c DWARF<\/li>\n
  5. \u6e90\u7801\u548c\u4fe1\u53f7<\/li>\n
  6. \u6e90\u7801\u7ea7\u9010\u6b65\u6267\u884c<\/li>\n
  7. \u6e90\u7801\u7ea7\u65ad\u70b9<\/li>\n
  8. \u5806\u6808\u5c55\u5f00<\/li>\n
  9. \u8bfb\u53d6\u53d8\u91cf<\/li>\n
  10. \u4e4b\u540e\u6b65\u9aa4<\/li>\n<\/ol>\n

    \u7528\u4e0b\u9762\u7684\u7a0b\u5e8f\u4f5c\u4e3a\u4f8b\u5b50\uff1a<\/p>\n

    void a() {\r\n\/\/stopped here\r\n}\r\nvoid b() {\r\na();\r\n}\r\nvoid c() {\r\na();\r\n}\r\nint main() {\r\nb();\r\nc();\r\n}<\/pre>\n

    \u5982\u679c\u8c03\u8bd5\u5668\u505c\u5728 \/\/stopped here' \u8fd9\u884c\uff0c\u90a3\u4e48\u6709\u4e24\u79cd\u65b9\u6cd5\u53ef\u4ee5\u8fbe\u5230\uff1amain->b->a\u6216main->c->a`\u3002\u5982\u679c\u6211\u4eec\u7528 LLDB \u8bbe\u7f6e\u4e00\u4e2a\u65ad\u70b9\uff0c\u7ee7\u7eed\u6267\u884c\u5e76\u8bf7\u6c42\u4e00\u4e2a\u56de\u6eaf\uff0c\u90a3\u4e48\u6211\u4eec\u5c06\u5f97\u5230\u4ee5\u4e0b\u5185\u5bb9\uff1a<\/p>\n

    * frame #0: 0x00000000004004da a.out`a() + 4 at bt.cpp:3\r\nframe #1: 0x00000000004004e6 a.out`b() + 9 at bt.cpp:6\r\nframe #2: 0x00000000004004fe a.out`main + 9 at bt.cpp:14\r\nframe #3: 0x00007ffff7a2e830 libc.so.6`__libc_start_main + 240 at libc-start.c:291\r\nframe #4: 0x0000000000400409 a.out`_start + 41<\/pre>\n

    \u8fd9\u8bf4\u660e\u6211\u4eec\u76ee\u524d\u5728\u51fd\u6570 a \u4e2d\uff0ca \u4ece\u51fd\u6570 b \u4e2d\u8df3\u8f6c\uff0cb \u4ece main \u4e2d\u8df3\u8f6c\u7b49\u7b49\u3002\u6700\u540e\u4e24\u4e2a\u5e27\u662f\u7f16\u8bd1\u5668\u5982\u4f55\u5f15\u5bfc main \u51fd\u6570\u7684\u3002<\/p>\n

    \u73b0\u5728\u7684\u95ee\u9898\u662f\u6211\u4eec\u5982\u4f55\u5728 x86_64 \u4e0a\u5b9e\u73b0\u3002\u6700\u7a33\u5065\u7684\u65b9\u6cd5\u662f\u89e3\u6790 ELF \u6587\u4ef6\u7684 .eh_frame \u90e8\u5206\uff0c\u5e76\u89e3\u51b3\u5982\u4f55\u4ece\u90a3\u91cc\u5c55\u5f00\u5806\u6808\uff0c\u4f46\u8fd9\u4f1a\u5f88\u75db\u82e6\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528 libunwind \u6216\u7c7b\u4f3c\u7684\u6765\u505a\uff0c\u4f46\u8fd9\u5f88\u65e0\u804a\u3002\u76f8\u53cd\uff0c\u6211\u4eec\u5047\u8bbe\u7f16\u8bd1\u5668\u4ee5\u67d0\u79cd\u65b9\u5f0f\u8bbe\u7f6e\u4e86\u5806\u6808\uff0c\u6211\u4eec\u5c06\u624b\u52a8\u904d\u5386\u5b83\u3002\u4e3a\u4e86\u505a\u5230\u8fd9\u4e00\u70b9\uff0c\u6211\u4eec\u9996\u5148\u9700\u8981\u4e86\u89e3\u5806\u6808\u7684\u5e03\u5c40\u3002<\/p>\n

    High\r\n| ... |\r\n+---------+\r\n+24| Arg 1 |\r\n+---------+\r\n+16| Arg 2 |\r\n+---------+\r\n+ 8| Return |\r\n+---------+\r\nEBP+--> |Saved EBP|\r\n+---------+\r\n- 8| Var 1 |\r\n+---------+\r\nESP+--> | Var 2 |\r\n+---------+\r\n| ... |\r\nLow<\/pre>\n

    \u5982\u4f60\u6240\u89c1\uff0c\u6700\u540e\u4e00\u4e2a\u5806\u6808\u5e27\u7684\u5e27\u6307\u9488\u5b58\u50a8\u5728\u5f53\u524d\u5806\u6808\u5e27\u7684\u5f00\u59cb\u5904\uff0c\u521b\u5efa\u4e00\u4e2a\u94fe\u63a5\u7684\u6307\u9488\u5217\u8868\u3002\u5806\u6808\u4f9d\u636e\u8fd9\u4e2a\u94fe\u8868\u89e3\u5f00\u3002\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u67e5\u627e DWARF \u4fe1\u606f\u4e2d\u7684\u8fd4\u56de\u5730\u5740\u6765\u627e\u51fa\u5217\u8868\u4e2d\u4e0b\u4e00\u5e27\u7684\u51fd\u6570\u3002\u4e00\u4e9b\u7f16\u8bd1\u5668\u5c06\u5ffd\u7565\u8ddf\u8e2a EBP \u7684\u5e27\u57fa\u5740\uff0c\u56e0\u4e3a\u8fd9\u53ef\u4ee5\u8868\u793a\u4e3a ESP \u7684\u504f\u79fb\u91cf\uff0c\u5e76\u53ef\u4ee5\u91ca\u653e\u4e00\u4e2a\u989d\u5916\u7684\u5bc4\u5b58\u5668\u3002\u5373\u4f7f\u542f\u7528\u4e86\u4f18\u5316\uff0c\u4f20\u9012 -fno-omit-frame-pointer \u5230 GCC \u6216 Clang \u4f1a\u5f3a\u5236\u5b83\u9075\u5faa\u6211\u4eec\u4f9d\u8d56\u7684\u7ea6\u5b9a\u3002<\/p>\n

    \u6211\u4eec\u5c06\u5728 print_backtrace \u51fd\u6570\u4e2d\u5b8c\u6210\u6240\u6709\u7684\u5de5\u4f5c\uff1a<\/p>\n

    void debugger::print_backtrace() {<\/pre>\n

    \u9996\u5148\u8981\u51b3\u5b9a\u7684\u662f\u4f7f\u7528\u4ec0\u4e48\u683c\u5f0f\u6253\u5370\u51fa\u5e27\u4fe1\u606f\u3002\u6211\u7528\u4e86\u4e00\u4e2a lambda \u6765\u63a8\u51fa\u8fd9\u4e2a\u65b9\u6cd5\uff1a<\/p>\n

    auto output_frame = [frame_number = 0] (auto&& func) mutable {\r\nstd::cout << \"frame #\" << frame_number++ << \": 0x\" << dwarf::at_low_pc(func)\r\n<< ' ' << dwarf::at_name(func) << std::endl;\r\n};<\/pre>\n

    \u6253\u5370\u8f93\u51fa\u7684\u7b2c\u4e00\u5e27\u662f\u5f53\u524d\u6b63\u5728\u6267\u884c\u7684\u5e27\u3002\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u67e5\u627e DWARF \u4e2d\u7684\u5f53\u524d\u7a0b\u5e8f\u8ba1\u6570\u5668\u6765\u83b7\u53d6\u6b64\u5e27\u7684\u4fe1\u606f\uff1a<\/p>\n

    auto current_func = get_function_from_pc(get_pc());\r\noutput_frame(current_func);<\/pre>\n

    \u63a5\u4e0b\u6765\u6211\u4eec\u9700\u8981\u83b7\u53d6\u5f53\u524d\u51fd\u6570\u7684\u5e27\u6307\u9488\u548c\u8fd4\u56de\u5730\u5740\u3002\u5e27\u6307\u9488\u5b58\u50a8\u5728 rbp \u5bc4\u5b58\u5668\u4e2d\uff0c\u8fd4\u56de\u5730\u5740\u662f\u4ece\u5e27\u6307\u9488\u5806\u6808\u8d77\u7684 8 \u5b57\u8282\u3002<\/p>\n

    auto frame_pointer = get_register_value(m_pid, reg::rbp);\r\nauto return_address = read_memory(frame_pointer+8);<\/pre>\n

    \u73b0\u5728\u6211\u4eec\u62e5\u6709\u4e86\u5c55\u5f00\u5806\u6808\u6240\u9700\u7684\u6240\u6709\u4fe1\u606f\u3002\u6211\u53ea\u9700\u8981\u7ee7\u7eed\u5c55\u5f00\uff0c\u76f4\u5230\u8c03\u8bd5\u5668\u547d\u4e2d main\uff0c\u4f46\u662f\u5f53\u5e27\u6307\u9488\u4e3a 0x0 \u65f6\uff0c\u4f60\u4e5f\u53ef\u4ee5\u9009\u62e9\u505c\u6b62\uff0c\u8fd9\u4e9b\u662f\u4f60\u5728\u8c03\u7528 main \u51fd\u6570\u4e4b\u524d\u8c03\u7528\u7684\u51fd\u6570\u3002\u6211\u4eec\u5c06\u4ece\u6bcf\u5e27\u6293\u53d6\u5e27\u6307\u9488\u548c\u8fd4\u56de\u5730\u5740\uff0c\u5e76\u6253\u5370\u51fa\u4fe1\u606f\u3002<\/p>\n

    while (dwarf::at_name(current_func) != \"main\") {\r\ncurrent_func = get_function_from_pc(return_address);\r\noutput_frame(current_func);\r\nframe_pointer = read_memory(frame_pointer);\r\nreturn_address = read_memory(frame_pointer+8);\r\n}\r\n}<\/pre>\n

    \u5c31\u662f\u8fd9\u6837\uff01\u4ee5\u4e0b\u662f\u6574\u4e2a\u51fd\u6570\uff1a<\/p>\n

    void debugger::print_backtrace() {\r\nauto output_frame = [frame_number = 0] (auto&& func) mutable {\r\nstd::cout << \"frame #\" << frame_number++ << \": 0x\" << dwarf::at_low_pc(func)\r\n<< ' ' << dwarf::at_name(func) << std::endl;\r\n};\r\nauto current_func = get_function_from_pc(get_pc());\r\noutput_frame(current_func);\r\nauto frame_pointer = get_register_value(m_pid, reg::rbp);\r\nauto return_address = read_memory(frame_pointer+8);\r\nwhile (dwarf::at_name(current_func) != \"main\") {\r\ncurrent_func = get_function_from_pc(return_address);\r\noutput_frame(current_func);\r\nframe_pointer = read_memory(frame_pointer);\r\nreturn_address = read_memory(frame_pointer+8);\r\n}\r\n}\r\n<\/pre>\n
    \u6dfb\u52a0\u547d\u4ee4<\/strong><\/div>\n

    \u5f53\u7136\uff0c\u6211\u4eec\u5fc5\u987b\u5411\u7528\u6237\u516c\u5f00\u8fd9\u4e2a\u547d\u4ee4\u3002<\/p>\n

    else if(is_prefix(command, \"backtrace\")) {\r\nprint_backtrace();\r\n}<\/pre>\n
    \u6d4b\u8bd5<\/strong><\/div>\n

    \u6d4b\u8bd5\u6b64\u529f\u80fd\u7684\u4e00\u4e2a\u65b9\u6cd5\u662f\u901a\u8fc7\u7f16\u5199\u4e00\u4e2a\u6d4b\u8bd5\u7a0b\u5e8f\u4e0e\u4e00\u5806\u4e92\u76f8\u8c03\u7528\u7684\u5c0f\u51fd\u6570\u3002\u8bbe\u7f6e\u51e0\u4e2a\u65ad\u70b9\uff0c\u8df3\u5230\u4ee3\u7801\u9644\u8fd1\uff0c\u5e76\u786e\u4fdd\u4f60\u7684\u56de\u6eaf\u662f\u51c6\u786e\u7684\u3002<\/p>\n

    \u6211\u4eec\u5df2\u7ecf\u4ece\u4e00\u4e2a\u53ea\u80fd\u4ea7\u751f\u5e76\u9644\u52a0\u5230\u5176\u4ed6\u7a0b\u5e8f\u7684\u7a0b\u5e8f\u8d70\u4e86\u5f88\u957f\u7684\u8def\u3002\u672c\u7cfb\u5217\u7684\u5012\u6570\u7b2c\u4e8c\u7bc7\u6587\u7ae0\u5c06\u901a\u8fc7\u652f\u6301\u8bfb\u5199\u53d8\u91cf\u6765\u5b8c\u6210\u8c03\u8bd5\u5668\u7684\u5b9e\u73b0\u3002\u5728\u6b64\u4e4b\u524d\uff0c\u4f60\u53ef\u4ee5\u5728\u8fd9\u91cc\u627e\u5230\u8fd9\u4e2a\u5e16\u5b50\u7684\u4ee3\u7801\u3002<\/p>\n

    via: https:\/\/blog.tartanllama.xyz\/c++\/2017\/06\/24\/writing-a-linux-debugger-unwinding\/<\/p>\n

    \u4f5c\u8005\uff1aSimon Brand \u8bd1\u8005\uff1ageekpi \u6821\u5bf9\uff1awxy<\/p>\n

    \u672c\u6587\u7531 LCTT \u539f\u521b\u7f16\u8bd1\uff0cLinux\u4e2d\u56fd \u8363\u8a89\u63a8\u51fa<\/p>\n

    \n

    \u539f\u6587\u6765\u81ea\uff1ahttps:\/\/linux.cn\/article-8930-1.html<\/a><\/p>\n

    \u672c\u6587\u5730\u5740\uff1ahttp:\/\/lrxjmw.cn\/linux-debugger-stack.html<\/a>\u7f16\u8f91\uff1a\u9ad8\u519b\uff0c\u5ba1\u6838\u5458\uff1a\u9004\u589e\u5b9d<\/span><\/p>\n<\/blockquote>\n","protected":false},"excerpt":{"rendered":"

    \u5bfc\u8bfb \u6709\u65f6\u4f60\u9700\u8981\u77e5\u9053\u7684\u6700\u91cd\u8981\u7684\u4fe1\u606f\u662f\u4ec0\u4e48\uff0c\u4f60\u5f53\u524d\u7684\u7a0b\u5e8f\u72b6\u6001\u662f\u5982\u4f55\u5230\u8fbe\u90a3\u91cc\u7684\u3002\u6709\u4e00\u4e2a backtrace \u547d\u4ee4\uff0c […]<\/p>\n","protected":false},"author":63,"featured_media":85821,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-85819","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/85819","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/63"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=85819"}],"version-history":[{"count":3,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/85819\/revisions"}],"predecessor-version":[{"id":85917,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/85819\/revisions\/85917"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/85821"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=85819"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=85819"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=85819"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}