{"id":1189,"date":"2018-05-01T10:24:53","date_gmt":"2018-05-01T02:24:53","guid":{"rendered":"https:\/\/damogame.cn:15443\/wordpress\/?p=1189"},"modified":"2018-05-01T10:24:53","modified_gmt":"2018-05-01T02:24:53","slug":"gdb-%e5%a6%82%e4%bd%95%e8%b0%83%e7%94%a8%e5%87%bd%e6%95%b0%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/i007.cc\/wordpress\/archives\/1189","title":{"rendered":"gdb \u5982\u4f55\u8c03\u7528\u51fd\u6570\uff1f"},"content":{"rendered":"<header class=\"Post-Header\">\n<h1 class=\"Post-Title\">gdb \u5982\u4f55\u8c03\u7528\u51fd\u6570\uff1f<\/h1>\n<div class=\"Post-Author\">\n<div class=\"AuthorInfo\">\n<div class=\"Popover\">\n<div id=\"Popover-19125-2542-toggle\" aria-haspopup=\"true\" aria-expanded=\"false\" aria-owns=\"Popover-19125-2542-content\"><a class=\"UserLink-link\" href=\"https:\/\/www.zhihu.com\/org\/linuxzhong-guo\" target=\"_blank\" rel=\"noopener\" data-za-detail-view-element_name=\"User\"><img loading=\"lazy\" decoding=\"async\" class=\"Avatar Avatar--round AuthorInfo-avatar\" src=\"https:\/\/pic3.zhimg.com\/v2-7fa1189a360e5fbf0224015877714629_xs.jpg\" srcset=\"https:\/\/pic3.zhimg.com\/v2-7fa1189a360e5fbf0224015877714629_l.jpg 2x\" alt=\"Linux\u4e2d\u56fd\" width=\"38\" height=\"38\" \/><\/a><\/div>\n<\/div>\n<div class=\"AuthorInfo-content\">\n<div class=\"AuthorInfo-head\">\n<div class=\"Popover\">\n<div id=\"Popover-19126-23695-toggle\" aria-haspopup=\"true\" aria-expanded=\"false\" aria-owns=\"Popover-19126-23695-content\"><a class=\"UserLink-link\" href=\"https:\/\/www.zhihu.com\/org\/linuxzhong-guo\" target=\"_blank\" rel=\"noopener\" data-za-detail-view-element_name=\"User\">Linux\u4e2d\u56fd<\/a><\/div>\n<\/div>\n<\/div>\n<div class=\"AuthorInfo-detail\">\n<div class=\"AuthorInfo-badge\">\n<div class=\"AuthorInfo-badgeText\">\u5df2\u8ba4\u8bc1\u7684\u5b98\u65b9\u5e10\u53f7<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p><button class=\"Button FollowButton Button--primary Button--blue\" type=\"button\">\u200b\u5173\u6ce8<\/button><\/div>\n<div><span class=\"Voters\"><button class=\"Button Button--plain\" type=\"button\">36 \u4eba\u8d5e\u4e86\u8be5\u6587\u7ae0<\/button><\/span><\/div>\n<\/header>\n<div>\n<div class=\"RichText Post-RichText\">\n<p>\uff08\u4e4b\u524d\u7684 gdb \u7cfb\u5217\u6587\u7ae0\uff1a<a class=\" wrap external\" href=\"https:\/\/link.zhihu.com\/?target=https%3A\/\/linux.cn\/article-9491-1.html\" target=\"_blank\" rel=\"nofollow noopener noreferrer\" data-za-detail-view-id=\"1043\">gdb \u5982\u4f55\u5de5\u4f5c\uff082016\uff09<\/a> \u548c<a class=\" wrap external\" href=\"https:\/\/link.zhihu.com\/?target=https%3A\/\/linux.cn\/article-9276-1.html\" target=\"_blank\" rel=\"nofollow noopener noreferrer\" data-za-detail-view-id=\"1043\">\u4e09\u6b65\u4e0a\u624b gdb\uff082014\uff09<\/a>\uff09<\/p>\n<p>\u5728\u8fd9\u5468\uff0c\u6211\u53d1\u73b0\u6211\u53ef\u4ee5\u4ece gdb \u4e0a\u8c03\u7528 C \u51fd\u6570\u3002\u8fd9\u770b\u8d77\u6765\u5f88\u9177\uff0c\u56e0\u4e3a\u5728\u8fc7\u53bb\u6211\u8ba4\u4e3a gdb \u6700\u591a\u53ea\u662f\u4e00\u4e2a\u53ea\u8bfb\u8c03\u8bd5\u5de5\u5177\u3002<\/p>\n<p>\u6211\u5bf9 gdb \u80fd\u591f\u8c03\u7528\u51fd\u6570\u611f\u5230\u5f88\u5403\u60ca\u3002\u6b63\u5982\u5f80\u5e38\u6240\u505a\u7684\u90a3\u6837\uff0c\u6211\u5728 <a class=\" wrap external\" href=\"https:\/\/link.zhihu.com\/?target=https%3A\/\/twitter.com\/b0rk\/status\/948060808243765248\" target=\"_blank\" rel=\"nofollow noopener noreferrer\" data-za-detail-view-id=\"1043\">Twitter<\/a> \u4e0a\u8be2\u95ee\u8fd9\u662f\u5982\u4f55\u5de5\u4f5c\u7684\u3002\u6211\u5f97\u5230\u4e86\u5927\u91cf\u7684\u6709\u7528\u7b54\u6848\u3002\u6211\u6700\u559c\u6b22\u7684\u7b54\u6848\u662f <a class=\" wrap external\" href=\"https:\/\/link.zhihu.com\/?target=https%3A\/\/github.com\/eklitzke\/ptrace-call-userspace\/blob\/master\/call_fprintf.c\" target=\"_blank\" rel=\"nofollow noopener noreferrer\" data-za-detail-view-id=\"1043\">Evan Klitzke \u7684\u793a\u4f8b C \u4ee3\u7801<\/a>\uff0c\u5b83\u5c55\u793a\u4e86 gdb \u5982\u4f55\u8c03\u7528\u51fd\u6570\u3002\u4ee3\u7801\u80fd\u591f\u8fd0\u884c\uff0c\u8fd9\u5f88\u4ee4\u4eba\u6fc0\u52a8\uff01<\/p>\n<p>\u6211\uff08\u901a\u8fc7\u4e00\u4e9b\u8ddf\u8e2a\u548c\u5b9e\u9a8c\uff09\u8ba4\u4e3a\u90a3\u4e2a\u793a\u4f8b C \u4ee3\u7801\u548c gdb \u5b9e\u9645\u4e0a\u5982\u4f55\u8c03\u7528\u51fd\u6570\u4e0d\u540c\u3002\u56e0\u6b64\uff0c\u5728\u8fd9\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u5c06\u4f1a\u9610\u8ff0 gdb \u662f\u5982\u4f55\u8c03\u7528\u51fd\u6570\u7684\uff0c\u4ee5\u53ca\u6211\u662f\u5982\u4f55\u77e5\u9053\u7684\u3002<\/p>\n<p>\u5173\u4e8e gdb \u5982\u4f55\u8c03\u7528\u51fd\u6570\uff0c\u8fd8\u6709\u8bb8\u591a\u6211\u4e0d\u77e5\u9053\u7684\u4e8b\u60c5\uff0c\u5e76\u4e14\uff0c\u5728\u8fd9\u513f\u6211\u5199\u7684\u5185\u5bb9\u6709\u53ef\u80fd\u662f\u9519\u8bef\u7684\u3002<\/p>\n<h2><b>\u4ece gdb \u4e2d\u8c03\u7528 C \u51fd\u6570\u610f\u5473\u7740\u4ec0\u4e48\uff1f<\/b><\/h2>\n<p>\u5728\u5f00\u59cb\u8bb2\u89e3\u8fd9\u662f\u5982\u4f55\u5de5\u4f5c\u4e4b\u524d\uff0c\u6211\u5148\u5feb\u901f\u7684\u8c08\u8bba\u4e00\u4e0b\u6211\u662f\u5982\u4f55\u53d1\u73b0\u8fd9\u4ef6\u4ee4\u4eba\u60ca\u8bb6\u7684\u4e8b\u60c5\u7684\u3002<\/p>\n<p>\u5047\u5982\uff0c\u4f60\u5df2\u7ecf\u5728\u8fd0\u884c\u4e00\u4e2a C \u7a0b\u5e8f\uff08\u76ee\u6807\u7a0b\u5e8f\uff09\u3002\u4f60\u53ef\u4ee5\u8fd0\u884c\u7a0b\u5e8f\u4e2d\u7684\u4e00\u4e2a\u51fd\u6570\uff0c\u53ea\u9700\u8981\u50cf\u4e0b\u9762\u8fd9\u6837\u505a\uff1a<\/p>\n<ul>\n<li>\u6682\u505c\u7a0b\u5e8f\uff08\u56e0\u4e3a\u5b83\u5df2\u7ecf\u5728\u8fd0\u884c\u4e2d\uff09<\/li>\n<li>\u627e\u5230\u4f60\u60f3\u8c03\u7528\u7684\u51fd\u6570\u7684\u5730\u5740\uff08\u4f7f\u7528\u7b26\u53f7\u8868\uff09<\/li>\n<li>\u4f7f\u7a0b\u5e8f\uff08\u76ee\u6807\u7a0b\u5e8f\uff09\u8df3\u8f6c\u5230\u90a3\u4e2a\u5730\u5740<\/li>\n<li>\u5f53\u51fd\u6570\u8fd4\u56de\u65f6\uff0c\u6062\u590d\u4e4b\u524d\u7684\u6307\u4ee4\u6307\u9488\u548c\u5bc4\u5b58\u5668<\/li>\n<\/ul>\n<p>\u901a\u8fc7\u7b26\u53f7\u8868\u6765\u627e\u5230\u60f3\u8981\u8c03\u7528\u7684\u51fd\u6570\u7684\u5730\u5740\u975e\u5e38\u5bb9\u6613\u3002\u4e0b\u9762\u662f\u4e00\u6bb5\u975e\u5e38\u7b80\u5355\u4f46\u80fd\u591f\u5de5\u4f5c\u7684\u4ee3\u7801\uff0c\u6211\u5728 Linux \u4e0a\u4f7f\u7528\u8fd9\u6bb5\u4ee3\u7801\u4f5c\u4e3a\u4f8b\u5b50\u6765\u8bb2\u89e3\u5982\u4f55\u627e\u5230\u5730\u5740\u3002\u8fd9\u6bb5\u4ee3\u7801\u4f7f\u7528 <a class=\" wrap external\" href=\"https:\/\/link.zhihu.com\/?target=https%3A\/\/cole14.github.io\/rust-elf\" target=\"_blank\" rel=\"nofollow noopener noreferrer\" data-za-detail-view-id=\"1043\">elf crate<\/a>\u3002\u5982\u679c\u6211\u60f3\u627e\u5230 PID \u4e3a 2345 \u7684\u8fdb\u7a0b\u4e2d\u7684 <code>foo<\/code> \u51fd\u6570\u7684\u5730\u5740\uff0c\u90a3\u4e48\u6211\u53ef\u4ee5\u8fd0\u884c <code>elf_symbol_value(\"\/proc\/2345\/exe\", \"foo\")<\/code>\u3002<\/p>\n<div class=\"highlight\">\n<pre><code class=\"language-text\">fn elf_symbol_value(file_name: &amp;str, symbol_name: &amp;str) -&gt; Result&lt;u64, Box&lt;std::error::Error&gt;&gt; {\r\n    \/\/ \u6253\u5f00 ELF \u6587\u4ef6 \r\n    let file = elf::File::open_path(file_name).ok().ok_or(\"parse error\")?;\r\n    \/\/ \u5728\u6240\u6709\u7684\u6bb5 &amp; \u7b26\u53f7\u4e2d\u5faa\u73af\uff0c\u76f4\u5230\u627e\u5230\u6b63\u786e\u7684\u90a3\u4e2a\r\n    let sections = &amp;file.sections;\r\n    for s in sections {\r\n        for sym in file.get_symbols(&amp;s).ok().ok_or(\"parse error\")? {\r\n            if sym.name == symbol_name {\r\n                return Ok(sym.value);\r\n            }\r\n        }\r\n    }\r\n    None.ok_or(\"No symbol found\")?\r\n}\r\n<\/code><\/pre>\n<\/div>\n<p>\u8fd9\u5e76\u4e0d\u80fd\u591f\u771f\u7684\u53d1\u6325\u4f5c\u7528\uff0c\u4f60\u8fd8\u9700\u8981\u627e\u5230\u6587\u4ef6\u7684\u5185\u5b58\u6620\u5c04\uff0c\u5e76\u5c06\u7b26\u53f7\u504f\u79fb\u91cf\u52a0\u5230\u6587\u4ef6\u6620\u5c04\u7684\u8d77\u59cb\u4f4d\u7f6e\u3002\u627e\u5230\u5185\u5b58\u6620\u5c04\u5e76\u4e0d\u56f0\u96be\uff0c\u5b83\u4f4d\u4e8e <code>\/proc\/PID\/maps<\/code> \u4e2d\u3002<\/p>\n<p>\u603b\u4e4b\uff0c\u627e\u5230\u60f3\u8981\u8c03\u7528\u7684\u51fd\u6570\u5730\u5740\u5bf9\u6211\u6765\u8bf4\u5f88\u76f4\u63a5\uff0c\u4f46\u662f\u5176\u4f59\u90e8\u5206\uff08\u6539\u53d8\u6307\u4ee4\u6307\u9488\uff0c\u6062\u590d\u5bc4\u5b58\u5668\u7b49\uff09\u770b\u8d77\u6765\u5c31\u4e0d\u8fd9\u4e48\u660e\u663e\u4e86\u3002<\/p>\n<h2><b>\u4f60\u4e0d\u80fd\u4ec5\u4ec5\u8fdb\u884c\u8df3\u8f6c<\/b><\/h2>\n<p>\u6211\u5df2\u7ecf\u8bf4\u8fc7\uff0c\u4f60\u4e0d\u80fd\u591f\u4ec5\u4ec5\u627e\u5230\u4f60\u60f3\u8981\u8fd0\u884c\u7684\u90a3\u4e2a\u51fd\u6570\u5730\u5740\uff0c\u7136\u540e\u8df3\u8f6c\u5230\u90a3\u513f\u3002\u6211\u5728 gdb \u4e2d\u5c1d\u8bd5\u8fc7\u90a3\u6837\u505a\uff08<code>jump foo<\/code>\uff09\uff0c\u7136\u540e\u7a0b\u5e8f\u51fa\u73b0\u4e86\u6bb5\u9519\u8bef\u3002\u6beb\u65e0\u610f\u4e49\u3002<\/p>\n<h2><b>\u5982\u4f55\u4ece gdb \u4e2d\u8c03\u7528 C \u51fd\u6570<\/b><\/h2>\n<p>\u9996\u5148\uff0c\u8fd9\u662f\u53ef\u80fd\u7684\u3002\u6211\u5199\u4e86\u4e00\u4e2a\u975e\u5e38\u7b80\u6d01\u7684 C \u7a0b\u5e8f\uff0c\u5b83\u6240\u505a\u7684\u4e8b\u53ea\u6709 <code>sleep<\/code> 1000 \u79d2\uff0c\u628a\u8fd9\u4e2a\u6587\u4ef6\u547d\u540d\u4e3a <code>test.c<\/code> \uff1a<\/p>\n<div class=\"highlight\">\n<pre><code class=\"language-text\">#include &lt;unistd.h&gt;\r\n\r\nint foo() {\r\n    return 3;\r\n}\r\nint main() {\r\n    sleep(1000);\r\n}\r\n<\/code><\/pre>\n<\/div>\n<p>\u63a5\u4e0b\u6765\uff0c\u7f16\u8bd1\u5e76\u8fd0\u884c\u5b83\uff1a<\/p>\n<div class=\"highlight\">\n<pre><code class=\"language-text\">$ gcc -o test  test.c\r\n$ .\/test\r\n<\/code><\/pre>\n<\/div>\n<p>\u6700\u540e\uff0c\u6211\u4eec\u4f7f\u7528 gdb \u6765\u8ddf\u8e2a <code>test<\/code> \u8fd9\u4e00\u7a0b\u5e8f\uff1a<\/p>\n<div class=\"highlight\">\n<pre><code class=\"language-text\">$ sudo gdb -p $(pgrep -f test)\r\n(gdb) p foo()\r\n$1 = 3\r\n(gdb) quit\r\n<\/code><\/pre>\n<\/div>\n<p>\u6211\u8fd0\u884c <code>p foo()<\/code> \u7136\u540e\u5b83\u8fd0\u884c\u4e86\u8fd9\u4e2a\u51fd\u6570\uff01\u8fd9\u975e\u5e38\u6709\u8da3\u3002<\/p>\n<h2><b>\u8fd9\u6709\u4ec0\u4e48\u7528\uff1f<\/b><\/h2>\n<p>\u4e0b\u9762\u662f\u4e00\u4e9b\u53ef\u80fd\u7684\u7528\u9014\uff1a<\/p>\n<ul>\n<li>\u5b83\u4f7f\u5f97\u4f60\u53ef\u4ee5\u628a gdb \u5f53\u6210\u4e00\u4e2a C \u5e94\u7b54\u5f0f\u7a0b\u5e8f\uff08REPL\uff09\uff0c\u8fd9\u5f88\u6709\u8da3\uff0c\u6211\u60f3\u5bf9\u5f00\u53d1\u4e5f\u4f1a\u6709\u7528<\/li>\n<li>\u5728 gdb \u4e2d\u8fdb\u884c\u8c03\u8bd5\u7684\u65f6\u5019\u5c55\u793a\/\u6d4f\u89c8\u590d\u6742\u6570\u636e\u7ed3\u6784\u7684\u529f\u80fd\u51fd\u6570\uff08\u611f\u8c22 <a class=\" wrap external\" href=\"https:\/\/link.zhihu.com\/?target=https%3A\/\/twitter.com\/invalidop\/status\/949161146526781440\" target=\"_blank\" rel=\"nofollow noopener noreferrer\" data-za-detail-view-id=\"1043\">@invalidop<\/a>\uff09<\/li>\n<li><a class=\" wrap external\" href=\"https:\/\/link.zhihu.com\/?target=https%3A\/\/github.com\/baloo\/setns\/blob\/master\/setns.c\" target=\"_blank\" rel=\"nofollow noopener noreferrer\" data-za-detail-view-id=\"1043\">\u5728\u8fdb\u7a0b\u8fd0\u884c\u65f6\u8bbe\u7f6e\u4e00\u4e2a\u4efb\u610f\u7684\u540d\u5b57\u7a7a\u95f4<\/a>\uff08\u6211\u7684\u540c\u4e8b <a class=\" wrap external\" href=\"https:\/\/link.zhihu.com\/?target=https%3A\/\/github.com\/nelhage\" target=\"_blank\" rel=\"nofollow noopener noreferrer\" data-za-detail-view-id=\"1043\">nelhage<\/a> \u5bf9\u6b64\u975e\u5e38\u60ca\u8bb6\uff09<\/li>\n<li>\u53ef\u80fd\u8fd8\u6709\u8bb8\u591a\u6211\u6240\u4e0d\u77e5\u9053\u7684\u7528\u9014<\/li>\n<\/ul>\n<h2><b>\u5b83\u662f\u5982\u4f55\u5de5\u4f5c\u7684<\/b><\/h2>\n<p>\u5f53\u6211\u5728 Twitter \u4e0a\u8be2\u95ee\u4ece gdb \u4e2d\u8c03\u7528\u51fd\u6570\u662f\u5982\u4f55\u5de5\u4f5c\u7684\u65f6\uff0c\u6211\u5f97\u5230\u4e86\u5927\u91cf\u6709\u7528\u7684\u56de\u7b54\u3002\u8bb8\u591a\u7b54\u6848\u662f\u201c\u4f60\u4ece\u7b26\u53f7\u8868\u4e2d\u5f97\u5230\u4e86\u51fd\u6570\u7684\u5730\u5740\u201d\uff0c\u4f46\u8fd9\u5e76\u4e0d\u662f\u5b8c\u6574\u7684\u7b54\u6848\u3002<\/p>\n<p>\u6709\u4e2a\u4eba\u544a\u8bc9\u4e86\u6211\u4e24\u7bc7\u5173\u4e8e gdb \u5982\u4f55\u5de5\u4f5c\u7684\u7cfb\u5217\u6587\u7ae0\uff1a<a class=\" wrap external\" href=\"https:\/\/link.zhihu.com\/?target=https%3A\/\/www.cl.cam.ac.uk\/%7Esrk31\/blog\/2016\/02\/25\/%23native-debugging-part-1\" target=\"_blank\" rel=\"nofollow noopener noreferrer\" data-za-detail-view-id=\"1043\">\u539f\u751f\u8c03\u8bd5\uff1a\u7b2c\u4e00\u90e8\u5206<\/a>\uff0c<a class=\" wrap external\" href=\"https:\/\/link.zhihu.com\/?target=https%3A\/\/www.cl.cam.ac.uk\/%7Esrk31\/blog\/2017\/01\/30\/%23native-debugging-part-2\" target=\"_blank\" rel=\"nofollow noopener noreferrer\" data-za-detail-view-id=\"1043\">\u539f\u751f\u8c03\u8bd5\uff1a\u7b2c\u4e8c\u90e8\u5206<\/a>\u3002\u7b2c\u4e00\u90e8\u5206\u8bb2\u8ff0\u4e86 gdb \u662f\u5982\u4f55\u8c03\u7528\u51fd\u6570\u7684\uff08\u6307\u51fa\u4e86 gdb \u5b9e\u9645\u4e0a\u5b8c\u6210\u8fd9\u4ef6\u4e8b\u5e76\u4e0d\u7b80\u5355\uff0c\u4f46\u662f\u6211\u5c06\u4f1a\u5c3d\u529b\uff09\u3002<\/p>\n<p>\u6b65\u9aa4\u5217\u4e3e\u5982\u4e0b\uff1a<\/p>\n<ol>\n<li>\u505c\u6b62\u8fdb\u7a0b<\/li>\n<li>\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u6808\u6846\uff08\u8fdc\u79bb\u771f\u5b9e\u6808\uff09<\/li>\n<li>\u4fdd\u5b58\u6240\u6709\u5bc4\u5b58\u5668<\/li>\n<li>\u8bbe\u7f6e\u4f60\u60f3\u8981\u8c03\u7528\u7684\u51fd\u6570\u7684\u5bc4\u5b58\u5668\u53c2\u6570<\/li>\n<li>\u8bbe\u7f6e\u6808\u6307\u9488\u6307\u5411\u65b0\u7684 <i>\u6808\u6846(stack frame)<\/i><\/li>\n<li>\u5728\u5185\u5b58\u4e2d\u67d0\u4e2a\u4f4d\u7f6e\u653e\u7f6e\u4e00\u6761\u9677\u9631\u6307\u4ee4<\/li>\n<li>\u4e3a\u9677\u9631\u6307\u4ee4\u8bbe\u7f6e\u8fd4\u56de\u5730\u5740<\/li>\n<li>\u8bbe\u7f6e\u6307\u4ee4\u5bc4\u5b58\u5668\u7684\u503c\u4e3a\u4f60\u60f3\u8981\u8c03\u7528\u7684\u51fd\u6570\u5730\u5740<\/li>\n<li>\u518d\u6b21\u8fd0\u884c\u8fdb\u7a0b\uff01<\/li>\n<\/ol>\n<p>\uff08LCTT \u8bd1\u6ce8\uff1a\u5982\u679c\u5c06\u8fd9\u4e2a\u8c03\u7528\u7684\u51fd\u6570\u770b\u6210\u4e00\u4e2a\u5355\u72ec\u7684\u7ebf\u7a0b\uff0cgdb \u5b9e\u9645\u4e0a\u6240\u505a\u7684\u4e8b\u60c5\u5c31\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u7ebf\u7a0b\u4e0a\u4e0b\u6587\u5207\u6362\uff09<\/p>\n<p>\u6211\u4e0d\u77e5\u9053 gdb \u662f\u5982\u4f55\u5b8c\u6210\u8fd9\u4e9b\u6240\u6709\u4e8b\u60c5\u7684\uff0c\u4f46\u662f\u4eca\u5929\u665a\u4e0a\uff0c\u6211\u5b66\u5230\u4e86\u8fd9\u4e9b\u6240\u6709\u4e8b\u60c5\u4e2d\u7684\u5176\u4e2d\u51e0\u4ef6\u3002<\/p>\n<h2><code>\u521b\u5efa\u4e00\u4e2a\u6808\u6846<\/code><\/h2>\n<p>\u5982\u679c\u4f60\u60f3\u8981\u8fd0\u884c\u4e00\u4e2a C \u51fd\u6570\uff0c\u90a3\u4e48\u4f60\u9700\u8981\u4e00\u4e2a\u6808\u6765\u5b58\u50a8\u53d8\u91cf\u3002\u4f60\u80af\u5b9a\u4e0d\u60f3\u7ee7\u7eed\u4f7f\u7528\u5f53\u524d\u7684\u6808\u3002\u51c6\u786e\u6765\u8bf4\uff0c\u5728 gdb \u8c03\u7528\u51fd\u6570\u4e4b\u524d\uff08\u901a\u8fc7\u8bbe\u7f6e\u51fd\u6570\u6307\u9488\u5e76\u8df3\u8f6c\uff09\uff0c\u5b83\u9700\u8981\u8bbe\u7f6e\u6808\u6307\u9488\u5230\u67d0\u4e2a\u5730\u65b9\u3002<\/p>\n<p>\u8fd9\u513f\u662f Twitter \u4e0a\u4e00\u4e9b\u5173\u4e8e\u5b83\u5982\u4f55\u5de5\u4f5c\u7684\u731c\u6d4b\uff1a<\/p>\n<blockquote><p>\u6211\u8ba4\u4e3a\u5b83\u5728\u5f53\u524d\u6808\u7684\u6808\u9876\u4e0a\u6784\u9020\u4e86\u4e00\u4e2a\u65b0\u7684\u6808\u6846\u6765\u8fdb\u884c\u8c03\u7528\uff01<\/p><\/blockquote>\n<p>\u4ee5\u53ca<\/p>\n<blockquote><p>\u4f60\u786e\u5b9a\u662f\u8fd9\u6837\u5417\uff1f\u5b83\u5e94\u8be5\u662f\u5206\u914d\u4e00\u4e2a\u4f2a\u6808\uff0c\u7136\u540e\u4e34\u65f6\u5c06 sp \uff08\u6808\u6307\u9488\u5bc4\u5b58\u5668\uff09\u7684\u503c\u6539\u4e3a\u90a3\u4e2a\u6808\u7684\u5730\u5740\u3002\u4f60\u53ef\u4ee5\u8bd5\u4e00\u8bd5\uff0c\u4f60\u53ef\u4ee5\u5728\u90a3\u513f\u8bbe\u7f6e\u4e00\u4e2a\u65ad\u70b9\uff0c\u7136\u540e\u770b\u4e00\u770b\u6808\u6307\u9488\u5bc4\u5b58\u5668\u7684\u503c\uff0c\u5b83\u662f\u5426\u548c\u5f53\u524d\u7a0b\u5e8f\u5bc4\u5b58\u5668\u7684\u503c\u76f8\u8fd1\uff1f<\/p><\/blockquote>\n<p>\u6211\u901a\u8fc7 gdb \u505a\u4e86\u4e00\u4e2a\u8bd5\u9a8c\uff1a<\/p>\n<div class=\"highlight\">\n<pre><code class=\"language-text\">(gdb) p $rsp\r\n$7 = (void *) 0x7ffea3d0bca8\r\n(gdb) break foo\r\nBreakpoint 1 at 0x40052a\r\n(gdb) p foo()\r\nBreakpoint 1, 0x000000000040052a in foo ()\r\n(gdb) p $rsp\r\n$8 = (void *) 0x7ffea3d0bc00\r\n<\/code><\/pre>\n<\/div>\n<p>\u8fd9\u770b\u8d77\u6765\u7b26\u5408\u201cgdb \u5728\u5f53\u524d\u6808\u7684\u6808\u9876\u6784\u9020\u4e86\u4e00\u4e2a\u65b0\u7684\u6808\u6846\u201d\u8fd9\u4e00\u7406\u8bba\u3002\u56e0\u4e3a\u6808\u6307\u9488\uff08<code>$rsp<\/code>\uff09\u4ece <code>0x7ffea3d0bca8<\/code> \u53d8\u6210\u4e86 <code>0x7ffea3d0bc00<\/code> \u2014\u2014 \u6808\u6307\u9488\u4ece\u9ad8\u5730\u5740\u5f80\u4f4e\u5730\u5740\u957f\u3002\u6240\u4ee5 <code>0x7ffea3d0bca8<\/code> \u5728 <code>0x7ffea3d0bc00<\/code> \u7684\u540e\u9762\u3002\u771f\u662f\u6709\u8da3\uff01<\/p>\n<p>\u6240\u4ee5\uff0c\u770b\u8d77\u6765 gdb \u53ea\u662f\u5728\u5f53\u524d\u6808\u6240\u5728\u4f4d\u7f6e\u521b\u5efa\u4e86\u4e00\u4e2a\u65b0\u7684\u6808\u6846\u3002\u8fd9\u4ee4\u6211\u5f88\u60ca\u8bb6\uff01<\/p>\n<h2><code>\u6539\u53d8\u6307\u4ee4\u6307\u9488<\/code><\/h2>\n<p>\u8ba9\u6211\u4eec\u6765\u770b\u4e00\u770b gdb \u662f\u5982\u4f55\u6539\u53d8\u6307\u4ee4\u6307\u9488\u7684\uff01<\/p>\n<div class=\"highlight\">\n<pre><code class=\"language-text\">(gdb) p $rip\r\n$1 = (void (*)()) 0x7fae7d29a2f0 &lt;__nanosleep_nocancel+7&gt;\r\n(gdb) b foo\r\nBreakpoint 1 at 0x40052a\r\n(gdb) p foo()\r\nBreakpoint 1, 0x000000000040052a in foo ()\r\n(gdb) p $rip\r\n$3 = (void (*)()) 0x40052a &lt;foo+4&gt;\r\n<\/code><\/pre>\n<\/div>\n<p>\u7684\u786e\u662f\uff01\u6307\u4ee4\u6307\u9488\u4ece <code>0x7fae7d29a2f0<\/code> \u53d8\u4e3a\u4e86 <code>0x40052a<\/code>\uff08<code>foo<\/code> \u51fd\u6570\u7684\u5730\u5740\uff09\u3002<\/p>\n<p>\u6211\u76ef\u7740\u8f93\u51fa\u770b\u4e86\u5f88\u4e45\uff0c\u4f46\u4ecd\u7136\u4e0d\u7406\u89e3\u5b83\u662f\u5982\u4f55\u6539\u53d8\u6307\u4ee4\u6307\u9488\u7684\uff0c\u4f46\u8fd9\u5e76\u4e0d\u5f71\u54cd\u4ec0\u4e48\u3002<\/p>\n<h2><code>\u5982\u4f55\u8bbe\u7f6e\u65ad\u70b9<\/code><\/h2>\n<p>\u4e0a\u9762\u6211\u5199\u5230 <code>break foo<\/code> \u3002\u6211\u8ddf\u8e2a gdb \u8fd0\u884c\u7a0b\u5e8f\u7684\u8fc7\u7a0b\uff0c\u4f46\u662f\u6ca1\u6709\u4efb\u4f55\u53d1\u73b0\u3002<\/p>\n<p>\u4e0b\u9762\u662f gdb \u7528\u6765\u8bbe\u7f6e\u65ad\u70b9\u7684\u4e00\u4e9b\u7cfb\u7edf\u8c03\u7528\u3002\u5b83\u4eec\u975e\u5e38\u7b80\u5355\u3002\u5b83\u628a\u4e00\u6761\u6307\u4ee4\u7528 <code>cc<\/code> \u4ee3\u66ff\u4e86\uff08\u8fd9\u544a\u8bc9\u6211\u4eec <code>int3<\/code> \u610f\u5473\u7740 <code>send SIGTRAP<\/code> <a class=\" wrap external\" href=\"https:\/\/link.zhihu.com\/?target=https%3A\/\/defuse.ca\/online-x86-assembler.htm\" target=\"_blank\" rel=\"nofollow noopener noreferrer\" data-za-detail-view-id=\"1043\">https:\/\/defuse.ca\/online-x86-assembler.html<\/a>\uff09\uff0c\u5e76\u4e14\u4e00\u65e6\u7a0b\u5e8f\u88ab\u6253\u65ad\u4e86\uff0c\u5b83\u5c31\u628a\u6307\u4ee4\u6062\u590d\u4e3a\u539f\u5148\u7684\u6837\u5b50\u3002<\/p>\n<p>\u6211\u5728\u51fd\u6570 <code>foo<\/code> \u90a3\u513f\u8bbe\u7f6e\u4e86\u4e00\u4e2a\u65ad\u70b9\uff0c\u5730\u5740\u4e3a <code>0x400528<\/code> \u3002<\/p>\n<p><code>PTRACE_POKEDATA<\/code> \u5c55\u793a\u4e86 gdb \u5982\u4f55\u6539\u53d8\u6b63\u5728\u8fd0\u884c\u7684\u7a0b\u5e8f\u3002<\/p>\n<div class=\"highlight\">\n<pre><code class=\"language-text\">\/\/ \u6539\u53d8 0x400528 \u5904\u7684\u6307\u4ee4\r\n25622 ptrace(PTRACE_PEEKTEXT, 25618, 0x400528, [0x5d00000003b8e589]) = 0\r\n25622 ptrace(PTRACE_POKEDATA, 25618, 0x400528, 0x5d00000003cce589) = 0\r\n\/\/ \u5f00\u59cb\u8fd0\u884c\u7a0b\u5e8f\r\n25622 ptrace(PTRACE_CONT, 25618, 0x1, SIG_0) = 0\r\n\/\/ \u5f53\u5230\u8fbe\u65ad\u70b9\u65f6\u83b7\u53d6\u4e00\u4e2a\u4fe1\u53f7\r\n25622 ptrace(PTRACE_GETSIGINFO, 25618, NULL, {si_signo=SIGTRAP, si_code=SI_KERNEL, si_value={int=-1447215360, ptr=0x7ffda9bd3f00}}) = 0\r\n\/\/ \u5c06 0x400528 \u5904\u7684\u6307\u4ee4\u66f4\u6539\u4e3a\u4e4b\u524d\u7684\u6837\u5b50\r\n25622 ptrace(PTRACE_PEEKTEXT, 25618, 0x400528, [0x5d00000003cce589]) = 0\r\n25622 ptrace(PTRACE_POKEDATA, 25618, 0x400528, 0x5d00000003b8e589) = 0\r\n<\/code><\/pre>\n<\/div>\n<h2><code>\u5728\u67d0\u5904\u653e\u7f6e\u4e00\u6761\u9677\u9631\u6307\u4ee4<\/code><\/h2>\n<p>\u5f53 gdb \u8fd0\u884c\u4e00\u4e2a\u51fd\u6570\u7684\u65f6\u5019\uff0c\u5b83\u4e5f\u4f1a\u5728\u67d0\u4e2a\u5730\u65b9\u653e\u7f6e\u4e00\u6761\u9677\u9631\u6307\u4ee4\u3002\u8fd9\u662f\u5176\u4e2d\u4e00\u6761\u3002\u5b83\u57fa\u672c\u4e0a\u662f\u7528 <code>cc<\/code>\u6765\u66ff\u6362\u4e00\u6761\u6307\u4ee4\uff08<code>int3<\/code>\uff09\u3002<\/p>\n<div class=\"highlight\">\n<pre><code class=\"language-text\">5908  ptrace(PTRACE_PEEKTEXT, 5810, 0x7f6fa7c0b260, [0x48f389fd89485355]) = 0\r\n5908  ptrace(PTRACE_PEEKTEXT, 5810, 0x7f6fa7c0b260, [0x48f389fd89485355]) = 0\r\n5908 ptrace(PTRACE_POKEDATA, 5810, 0x7f6fa7c0b260, 0x48f389fd894853cc) = 0\r\n<\/code><\/pre>\n<\/div>\n<p><code>0x7f6fa7c0b260<\/code> \u662f\u4ec0\u4e48\uff1f\u6211\u67e5\u770b\u4e86\u8fdb\u7a0b\u7684\u5185\u5b58\u6620\u5c04\uff0c\u53d1\u73b0\u5b83\u4f4d\u4e8e <code>\/lib\/x86_64-linux-gnu\/libc-2.23.so<\/code> \u4e2d\u7684\u67d0\u4e2a\u4f4d\u7f6e\u3002\u8fd9\u5f88\u5947\u602a\uff0c\u4e3a\u4ec0\u4e48 gdb \u5c06\u9677\u9631\u6307\u4ee4\u653e\u5728 libc \u4e2d\uff1f<\/p>\n<p>\u8ba9\u6211\u4eec\u770b\u4e00\u770b\u91cc\u9762\u7684\u51fd\u6570\u662f\u4ec0\u4e48\uff0c\u5b83\u662f <code>__libc_siglongjmp<\/code> \u3002\u5176\u4ed6 gdb \u653e\u7f6e\u9677\u9631\u6307\u4ee4\u7684\u5730\u65b9\u7684\u51fd\u6570\u662f <code>__longjmp<\/code> \u3001<code>___longjmp_chk<\/code> \u3001<code>dl_main<\/code> \u548c <code>_dl_close_worker<\/code> \u3002<\/p>\n<p>\u4e3a\u4ec0\u4e48\uff1f\u6211\u4e0d\u77e5\u9053\uff01\u4e5f\u8bb8\u51fa\u4e8e\u67d0\u79cd\u539f\u56e0\uff0c\u5f53\u51fd\u6570 <code>foo()<\/code> \u8fd4\u56de\u65f6\uff0c\u5b83\u8c03\u7528 <code>longjmp<\/code> \uff0c\u4ece\u800c gdb \u80fd\u591f\u8fdb\u884c\u8fd4\u56de\u63a7\u5236\u3002\u6211\u4e0d\u786e\u5b9a\u3002<\/p>\n<h2><b>gdb \u5982\u4f55\u8c03\u7528\u51fd\u6570\u662f\u5f88\u590d\u6742\u7684\uff01<\/b><\/h2>\n<p>\u6211\u5c06\u8981\u5728\u8fd9\u513f\u505c\u6b62\u4e86\uff08\u73b0\u5728\u5df2\u7ecf\u51cc\u6668 1 \u70b9\uff09\uff0c\u4f46\u662f\u6211\u77e5\u9053\u7684\u591a\u4e00\u4e9b\u4e86\uff01<\/p>\n<p>\u770b\u8d77\u6765\u201cgdb \u5982\u4f55\u8c03\u7528\u51fd\u6570\u201d\u8fd9\u4e00\u95ee\u9898\u7684\u7b54\u6848\u5e76\u4e0d\u7b80\u5355\u3002\u6211\u53d1\u73b0\u8fd9\u5f88\u6709\u8da3\u5e76\u4e14\u52aa\u529b\u627e\u51fa\u5176\u4e2d\u4e00\u4e9b\u7b54\u6848\uff0c\u5e0c\u671b\u4f60\u4e5f\u80fd\u591f\u627e\u5230\u3002<\/p>\n<p>\u6211\u4f9d\u65e7\u6709\u5f88\u591a\u672a\u56de\u7b54\u7684\u95ee\u9898\uff0c\u5173\u4e8e gdb \u662f\u5982\u4f55\u5b8c\u6210\u8fd9\u4e9b\u6240\u6709\u4e8b\u7684\uff0c\u4f46\u662f\u53ef\u4ee5\u4e86\u3002\u6211\u4e0d\u9700\u8981\u771f\u7684\u77e5\u9053\u5173\u4e8e gdb \u662f\u5982\u4f55\u5de5\u4f5c\u7684\u6240\u6709\u7ec6\u8282\uff0c\u4f46\u662f\u6211\u5f88\u5f00\u5fc3\uff0c\u6211\u6709\u4e86\u4e00\u4e9b\u8fdb\u4e00\u6b65\u7684\u7406\u89e3\u3002<\/p>\n<hr \/>\n<p>via: <a class=\" external\" href=\"https:\/\/link.zhihu.com\/?target=https%3A\/\/jvns.ca\/blog\/2018\/01\/04\/how-does-gdb-call-functions\/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\" data-za-detail-view-id=\"1043\"><span class=\"invisible\">https:\/\/<\/span><span class=\"visible\">jvns.ca\/blog\/2018\/01\/04<\/span><span class=\"invisible\">\/how-does-gdb-call-functions\/<\/span><\/a><\/p>\n<p>\u4f5c\u8005\uff1a<a class=\" wrap external\" href=\"https:\/\/link.zhihu.com\/?target=https%3A\/\/jvns.ca\/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\" data-za-detail-view-id=\"1043\">Julia Evans<\/a> \u8bd1\u8005\uff1a<a class=\" wrap external\" href=\"https:\/\/link.zhihu.com\/?target=https%3A\/\/github.com\/ucasFL\" target=\"_blank\" rel=\"nofollow noopener noreferrer\" data-za-detail-view-id=\"1043\">ucasFL<\/a> \u6821\u5bf9\uff1a<a class=\" wrap external\" href=\"https:\/\/link.zhihu.com\/?target=https%3A\/\/github.com\/wxy\" target=\"_blank\" rel=\"nofollow noopener noreferrer\" data-za-detail-view-id=\"1043\">wxy<\/a><\/p>\n<p>\u672c\u6587\u7531 <a class=\" wrap external\" href=\"https:\/\/link.zhihu.com\/?target=https%3A\/\/github.com\/LCTT\/TranslateProject\" target=\"_blank\" rel=\"nofollow noopener noreferrer\" data-za-detail-view-id=\"1043\">LCTT<\/a> \u539f\u521b\u7f16\u8bd1\uff0c<a class=\" wrap external\" href=\"https:\/\/link.zhihu.com\/?target=https%3A\/\/linux.cn\/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\" data-za-detail-view-id=\"1043\">Linux\u4e2d\u56fd<\/a> \u8363\u8a89\u63a8\u51fa<\/p>\n<\/div>\n<\/div>\n<div class=\"ContentItem-time\"><a href=\"http:\/\/zhuanlan.zhihu.com\/p\/36253807\" target=\"_blank\" rel=\"noopener\"><span data-tooltip=\"\u53d1\u5e03\u4e8e 2018-04-29 11:19\">\u7f16\u8f91\u4e8e 2018-04-29<\/span><\/a><\/div>\n","protected":false},"excerpt":{"rendered":"<p>gdb \u5982\u4f55\u8c03\u7528\u51fd\u6570\uff1f Linux\u4e2d\u56fd <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"colormag_page_container_layout":"default_layout","colormag_page_sidebar_layout":"default_layout","footnotes":""},"categories":[],"tags":[118],"class_list":["post-1189","post","type-post","status-publish","format-standard","hentry","tag-02-"],"_links":{"self":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/posts\/1189","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/comments?post=1189"}],"version-history":[{"count":0,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/posts\/1189\/revisions"}],"wp:attachment":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/media?parent=1189"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/categories?post=1189"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/tags?post=1189"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}