{"id":645,"date":"2021-11-01T07:52:00","date_gmt":"2021-11-01T07:52:00","guid":{"rendered":"https:\/\/262235.xyz\/?p=645"},"modified":"2021-11-01T07:52:00","modified_gmt":"2021-11-01T07:52:00","slug":"645","status":"publish","type":"post","link":"https:\/\/lyvba.com\/index.php\/2021\/11\/01\/645\/","title":{"rendered":"C\u8bed\u8a00\u7f16\u7a0b-GDB\u7a0b\u5e8f\u8c03\u8bd5\u5de5\u5177\u547d\u4ee4\u50bb\u74dc\u901f\u6210"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/lyvba.com\/wp-content\/uploads\/2021\/11\/2762974415.webp\" alt=\"gdb.webp\" title=\"gdb.webp\"><br \/>GNU\u8c03\u8bd5\u5668\uff08\u82f1\u8bed\uff1aGNU Debugger\uff0c\u7f29\u5199\uff1aGDB\uff09\uff0c\u662fGNU\u8f6f\u4ef6\u7cfb\u7edf\u4e2d\u7684\u6807\u51c6\u8c03\u8bd5\u5668\uff0c\u6b64\u5916GDB\u4e5f\u662f\u4e2a\u5177\u6709\u79fb\u643a\u6027\u7684\u8c03\u8bd5\u5668\uff0c\u7ecf\u8fc7\u79fb\u643a\u9700\u6c42\u7684\u8c03\u4fee\u4e0e\u91cd\u65b0\u7f16\u8bd1\uff0c\u5982\u4eca\u8bb8\u591a\u7684\u7c7bUNIX\u64cd\u4f5c\u7cfb\u7edf\u4e0a\u90fd\u53ef\u4ee5\u4f7f\u7528GDB\uff0c\u800c\u73b0\u6709GDB\u6240\u80fd\u652f\u6301\u8c03\u8bd5\u7684\u7f16\u7a0b\u8bed\u8a00\u6709C\u3001C++\u3001Pascal\u4ee5\u53caFORTRAN\u3002<\/p>\n<h2>1. \u7f16\u5199\u4e00\u4e2aC\u8bed\u8a00\u4ee3\u7801 <code>a.c<\/code><\/h2>\n<pre><code>int main()\n{\n    int i = 5;\n    int b = 4;\n    int ret = i * b;\n    return ret ;\n}<\/code><\/pre>\n<h2>2. \u4ee5\u8c03\u8bd5\u6a21\u5f0f\u7f16\u8bd1C\u8bed\u8a00\u6e90\u4ee3\u7801\uff0c\u8f93\u51fa <code>a.out<\/code><\/h2>\n<pre><code>gcc -g a.c<\/code><\/pre>\n<h2>3. \u7f16\u8bd1\u5b8c\u6210\u540e\uff0c\u542f\u52a8GDB\u8c03\u8bd5\u5de5\u5177\uff0c\u8c03\u8bd5 <code>a.out<\/code><\/h2>\n<pre><code>gdb a.out<\/code><\/pre>\n<p><img decoding=\"async\" src=\"https:\/\/lyvba.com\/wp-content\/uploads\/2021\/11\/2835935667.png\" alt=\"gdb.png\" title=\"gdb.png\"><\/p>\n<h2>4. \u5982\u56fe\uff0cGDB\u8c03\u8bd5\u57fa\u672c\u547d\u4ee4 <code>l b r n p i f q<\/code><\/h2>\n<ul>\n<li>\u7528l\u67e5\u770b\u6e90\u7801\uff0cb\u8bbe\u7f6e\u65ad\u70b9\u4e3amain\u51fd\u6570\uff0c\u4e5f\u53ef\u4ee5\u6309\u884c\u53f7\u8bbe\u7f6e\u65ad\u70b9\u884cb x ;<\/li>\n<li>r\u8fd0\u884c\u7a0b\u5e8f\uff0cn\u9010\u884c\u8fd0\u884c\u7a0b\u5e8f\uff0cp\u663e\u793a\u53d8\u91cf\u503c;<\/li>\n<li>i\u547d\u4ee4\u662finfo\u7684\u7f29\u5199\uff0c <code>info locals<\/code> \u53ef\u4ee5\u540c\u65f6\u663e\u793a\u51fd\u6570\u4e2d\u591a\u4e2a\u53d8\u91cf\u7684\u503c\u3002<\/li>\n<li>f\u547d\u4ee4\u662fframe\u7684\u7f29\u5199\uff0c\u663e\u793a\u5f53\u524d\u6e90\u7801\u548c\u51fd\u6570\u4fe1\u606f\uff0cq\u547d\u4ee4\u662fquit\u7684\u7f29\u5199\u3002<\/li>\n<\/ul>\n<p><img decoding=\"async\" src=\"https:\/\/lyvba.com\/wp-content\/uploads\/2021\/11\/3714510525.png\" alt=\"info.png\" title=\"info.png\"><\/p>\n<h2>5. GDB\u4e2d\u8f93\u5165\u547d\u4ee4\u65f6\u53ef\u4ee5\u4f7f\u7528 <code>TAB<\/code> \u81ea\u52a8\u8865\u5168\uff0c\u5e2e\u4f60\u5217\u51fa\u76f8\u5173\u547d\u4ee4<\/h2>\n<ul>\n<li>\u5982\u56fe, \u8f93\u5165 i \u6309 TAB\u952e\uff0c\u5c31\u663e\u793a i \u5f00\u5934\u7684\u547d\u4ee4<\/li>\n<li>\u8f93\u5165 info \u540e\u6309\u7a7a\u683c\uff0c\u518d\u6309 TAB\u952e\uff0c\u5c31\u663e\u793a\u5f53\u524d\u547d\u4ee4\u7684 \u53c2\u6570\u547d\u4ee4\u53ef\u4f9b\u9009\u62e9<\/li>\n<\/ul>\n<h2>6. GDB\u5728\u5b66\u4e60ARM\u6c47\u7f16\u4e5f\u5f88\u597d\u7528\uff0c\u53ef\u4ee5\u663e\u793a\u6e90\u7801\uff0c\u6c47\u7f16\u7801\uff0c\u5bc4\u5b58\u5668\u503c<\/h2>\n<p><img decoding=\"async\" src=\"https:\/\/lyvba.com\/wp-content\/uploads\/2021\/11\/906146969.png\" alt=\"gdb-asm.png\" title=\"gdb-asm.png\"><\/p>\n<ul>\n<li><code>layout asm<\/code> \u53ef\u4ee5\u7528\u6765\u5206\u7a97\u53e3\u663e\u793a\u6c47\u7f16\u7801<\/li>\n<li>\n<p><code>disassemble<\/code> \u547d\u4ee4\u663e\u793a\u6c47\u7f16\u7801; <code>info registers<\/code> \u663e\u793a\u5bc4\u5b58\u5668\u503c<\/p>\n<pre><code>(gdb) layout\nList of layout subcommands:\n\nlayout asm -- Apply the \"asm\" layout.\nlayout next -- Apply the next TUI layout.\nlayout prev -- Apply the previous TUI layout.\nlayout regs -- Apply the TUI register layout.\nlayout split -- Apply the \"split\" layout.\nlayout src -- Apply the \"src\" layout.\n\n(gdb) disassemble\nDump of assembler code for function main:\n 0x2a0004e0 &lt;+0&gt;:    push    {r7}\n 0x2a0004e2 &lt;+2&gt;:    sub    sp, #20\n 0x2a0004e4 &lt;+4&gt;:    add    r7, sp, #0\n 0x2a0004e6 &lt;+6&gt;:    movs    r3, #5\n 0x2a0004e8 &lt;+8&gt;:    str    r3, [r7, #12]\n 0x2a0004ea &lt;+10&gt;:    movs    r3, #4\n 0x2a0004ec &lt;+12&gt;:    str    r3, [r7, #8]\n=&gt; 0x2a0004ee &lt;+14&gt;:    ldr    r3, [r7, #12]\n 0x2a0004f0 &lt;+16&gt;:    ldr    r2, [r7, #8]\n 0x2a0004f2 &lt;+18&gt;:    mul.w    r3, r2, r3\n 0x2a0004f6 &lt;+22&gt;:    str    r3, [r7, #4]\n 0x2a0004f8 &lt;+24&gt;:    ldr    r3, [r7, #4]\n 0x2a0004fa &lt;+26&gt;:    mov    r0, r3\n 0x2a0004fc &lt;+28&gt;:    adds    r7, #20\n 0x2a0004fe &lt;+30&gt;:    mov    sp, r7\n 0x2a000500 &lt;+32&gt;:    ldr.w    r7, [sp], #4\n 0x2a000504 &lt;+36&gt;:    bx    lr\nEnd of assembler dump.\n\n(gdb) info registers\nr0             0x1                 1\nr1             0xbefff784          3204446084\nr2             0xbefff78c          3204446092\nr3             0x4                 4\nr4             0xbefff638          3204445752\nr5             0x0                 0\nr6             0x2a0003d1          704644049\nr7             0xbefff610          3204445712\nr8             0x0                 0\nr9             0x0                 0\nr10            0x2a011000          704712704\nr11            0x0                 0\nr12            0xbefff6a0          3204445856\nsp             0xbefff610          0xbefff610\nlr             0xb6ef0a21          -1225848287\npc             0x2a0004ee          0x2a0004ee &lt;main+14&gt;\ncpsr           0xe0030             917552\nfpscr          0x0                 0<\/code><\/pre>\n<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>GNU\u8c03\u8bd5\u5668\uff08\u82f1\u8bed\uff1aGNU Debugger\uff0c\u7f29\u5199\uff1aGDB\uff09\uff0c\u662fGNU\u8f6f\u4ef6\u7cfb\u7edf\u4e2d\u7684\u6807\u51c6\u8c03\u8bd5\u5668\uff0c\u6b64\u5916G [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8,12],"tags":[58,31],"class_list":["post-645","post","type-post","status-publish","format-standard","hentry","category-linux","category-learn","tag-arm","tag-cpp"],"_links":{"self":[{"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/posts\/645","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/comments?post=645"}],"version-history":[{"count":0,"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/posts\/645\/revisions"}],"wp:attachment":[{"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/media?parent=645"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/categories?post=645"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/tags?post=645"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}