{"id":1702,"date":"2022-05-08T19:33:25","date_gmt":"2022-05-08T11:33:25","guid":{"rendered":"https:\/\/blog.dynox.cn\/?p=1702"},"modified":"2024-04-28T12:00:29","modified_gmt":"2024-04-28T04:00:29","slug":"%e9%80%9a%e8%bf%87qemu-kvm-gdb-stub%e8%bf%9b%e8%a1%8c%e5%86%85%e6%a0%b8%e8%b0%83%e8%af%95","status":"publish","type":"post","link":"https:\/\/blog.dynox.cn\/?p=1702","title":{"rendered":"\u901a\u8fc7QEMU\/KVM GDB Stub\u8fdb\u884c\u5185\u6838\u8c03\u8bd5"},"content":{"rendered":"<div class=\"gruber-markdown\"><h1>\u5185\u6838\u51c6\u5907<\/h1>\n<h2>\u5185\u6838\u914d\u7f6e<\/h2>\n<p>\u5efa\u8bae\u4ece\u6e90\u4ee3\u7801\u91cd\u65b0\u7f16\u8bd1\u5185\u6838\uff0c\u8fd9\u6837\u53ef\u4ee5\u66f4\u65b9\u4fbf\u5730\u8fdb\u884c\u6e90\u4ee3\u7801\u7ea7\u8c03\u8bd5\uff1b\u5f53\u7136\u81ea\u5df1\u4ece\u53d1\u884c\u7248\u5b98\u7f51\u4e0b\u8f7d\u76f8\u5e94\u5185\u6838\u7684\u8c03\u8bd5\u7b26\u53f7\u4ea6\u53ef\u4ee5\uff0c\u901a\u5e38\u60c5\u51b5\u4e0b\u53ea\u7528\u4e8e\u5e94\u6025\u5206\u6790\uff0c\u6bd4\u5982crash dump\u5206\u6790\u7b49\u3002<\/p>\n<p>\u81ea\u5df1\u7f16\u8bd1Linux\u5185\u6838\u7684\u8bdd\uff0c\u6700\u597d\u6253\u5f00\u4e24\u4e2a\u9009\u9879\uff1a<\/p>\n<pre><code class=\"prettyprint\" class=\"language-css\">CONFIG_DEBUG_INFO=y\nCONFIG_GDB_SCRIPTS=y<\/code><\/pre>\n<p>\u53ef\u901a\u8fc7make menuconfig\u6765\u8bbe\u7f6e\u5185\u6838\u914d\u7f6e\u9879\uff0c\u5206\u522b\u4ee55.12\u53ca6.1.0\u4e3a\u4f8b\uff1a\n5.12:\n<img decoding=\"async\" src=\"https:\/\/blog.dynox.cn\/wp-content\/uploads\/2022\/05\/GDB-Linux-5.12.jpg\" alt=\"Linux kernel 5.12 menuconfig\" \/>\n6.1.0:\n<img decoding=\"async\" src=\"https:\/\/blog.dynox.cn\/wp-content\/uploads\/2022\/05\/GDB-Linux-6.1.jpg\" alt=\"Linux kernel 6.1.0 menuconfig\" \/><\/p>\n<h2>\u7981\u6b62\u7f16\u8bd1\u4f18\u5316<\/h2>\n<p>\u65e0\u6cd5\u9488\u5bf9Linux\u5185\u6838\u7981\u6b62\u5168\u5c40\u4f18\u5316\uff0c\u4f46\u53ef\u4ee5\u9488\u5bf9\u7279\u5b9a\u7684\u6e90\u4ee3\u7801\u6587\u4ef6\u505a\u7981\u6b62\u7f16\u8bd1\u4f18\u5316\u7684\u5904\u7406\uff0c\u53ef\u901a\u8fc7\u4ee5\u4e0b\u65b9\u5f0f\u8fbe\u6210\uff1a<\/p>\n<p>\u5728\u9700\u8981\u88ab\u8c03\u8bd5\u7684\u6e90\u4ee3\u7801\u6587\u4ef6\u5934\u90e8\u589e\u52a0\u4e0b\u9762\u4e00\u884c\u4ee3\u7801\uff1a<\/p>\n<pre><code class=\"prettyprint\" class=\"language-c\">#pragma GCC optimize (\"O0\")<\/code><\/pre>\n<p>\u6216\u8005\u4fee\u6539\u5176\u5bf9\u5e94\u7684Makefile\uff0c\u4ee5\u6587\u4ef6\u540dcore.c\u4e3a\u4f8b:<\/p>\n<pre><code class=\"prettyprint\" class=\"language-c\">CFLAGS_REMOVE_core.o := -O2\nCFLAGS_core.o := -O0<\/code><\/pre>\n<h1>\u955c\u50cf\u51c6\u5907<\/h1>\n<p>\u5efa\u8bae\u901a\u8fc7libvirt\u6765\u7ba1\u7406\u672c\u5730KVM\u865a\u62df\u673a\uff0c\u8fd9\u6837\u4f7f\u7528\u8d77\u6765\u975e\u5e38\u65b9\u4fbf\uff0c\u5982\u679c\u6761\u4ef6\u4e0d\u5141\u8bb8\u7684\u8bdd\u4ea6\u53ef\u7528qemu\u547d\u4ee4\u884c\u65b9\u5f0f\u6765\u8fd0\u884c\u3002<\/p>\n<h2>\u6dfb\u52a0 gdb stub<\/h2>\n<p>\u4e0b\u9762\u7684\u793a\u4f8b\u662f\u4ee5\u547d\u4ee4\u884c\u65b9\u5f0f\u6765\u8fd0\u884c\u8c03\u8bd5\u76ee\u6807\uff1a<\/p>\n<pre><code class=\"prettyprint\" class=\"language-bash\">qemu-system-x86_64 -m 4096 -nographic -net user,hostfwd=tcp::44334-:22 -net nic -hda \\\n    debian-10.7.qcow2 -machine type=pc,accel=kvm -cpu host -smp 4 -gdb tcp:127.0.0.1:44333<\/code><\/pre>\n<p>\u201c-gdb tcp:127.0.0.1:44333\u201d \u5c31\u662fgdb stub\u7684\u547d\u4ee4\u884c\u53c2\u6570\uff0c\u5373gdb\u53ef\u8fde\u63a5localhost:44333\u6765\u8c03\u8bd5\u76ee\u6807\u7cfb\u7edf\u3002<\/p>\n<p>\u5982\u679c\u4f7f\u7528libvirt\u7684\u8bdd\u5219\u9700\u8981\u4fee\u6539QEMU\u914d\u7f6e\uff0c\u53ef\u901a\u8fc7\u547d\u4ee4virsh edit\u6765\u8fdb\u884c\u4fee\u6539\uff0c\u9700\u8981\u5728\u672b\u5c3e\u52a0\u4e0a\u4e2d\u95f44\u884c\uff1a<\/p>\n<pre><code class=\"prettyprint\" class=\"language-c\">   &lt;\/devices&gt;\n\n   &lt;qemu:commandline&gt;\n    &lt;qemu:arg value='-gdb'\/&gt;\n    &lt;qemu:arg value='tcp:localhost:44333'\/&gt;\n  &lt;\/qemu:commandline&gt;\n\n&lt;\/domain&gt;<\/code><\/pre>\n<p>\u53e6\u5916\u8fd8\u9700\u8981\u66f4\u6539\u865a\u62df\u673a\u914d\u7f6e\u6587\u4ef6\u7684domain type\uff0c\u5373\u5c06\u7b2c\u4e00\u884c\u4ece<\/p>\n<pre><code class=\"prettyprint\" class=\"language-c\">&lt;domain type='kvm'&gt;<\/code><\/pre>\n<p>\u6539\u4e3a\uff1a<\/p>\n<pre><code class=\"prettyprint\" class=\"language-c\">&lt;domain type='kvm' xmlns:qemu='http:\/\/libvirt.org\/schemas\/domain\/qemu\/1.0'&gt;<\/code><\/pre>\n<h2>\u4fee\u6539\u5185\u6838\u542f\u52a8\u9879<\/h2>\n<p>\u672c\u6b65\u9aa4\u4e3b\u8981\u4e3a\u4e86\u7981\u6b62\u5185\u6838\u5730\u5740\u968f\u673a\u5316\uff0c\u4e0d\u7136\u5185\u6838\u7b26\u53f7\u65e0\u6cd5\u5b9a\u4f4d\u5e76\u6b63\u786e\u7ed1\u5b9a\u3002\u53ef\u901a\u8fc7\u4fee\u6539grub\u9879\u5b8c\u6210\uff0c\u5728\u5b89\u88c5\u65b0\u5185\u6838\u8fc7\u7a0b\u4e2d\u4f1a\u81ea\u52a0\u589e\u52a0nokaslr\u53canopti\uff0c\u5f53\u7136\u5728\u7f16\u8bd1\u5185\u6838\u65f6\u4ea6\u53ef\u76f4\u63a5\u7981\u6b62\u76f8\u5e94\u529f\u80fd\u4ea6\u53ef\u3002<\/p>\n<pre><code class=\"prettyprint\" class=\"language-c\">root@T490:~# cat \/etc\/default\/grub | grep nokaslr\nGRUB_CMDLINE_LINUX_DEFAULT=&amp;quot;noquiet nopti nokaslr console=ttyS0&amp;quot;<\/code><\/pre>\n<p>\u6700\u76f4\u63a5\u7684\u65b9\u5f0f\u5c31\u662f\u76f4\u63a5\u4fee\u6539\/boot\/grub\/grub.cfg\u6587\u4ef6\uff0c\u6216\u8005\u5728\u7cfb\u7edf\u542f\u52a8\u65f6\u624b\u5de5\u7f16\u8bd1grub\u542f\u52a8\u9879\u4ea6\u53ef\uff1a<\/p>\n<pre><code class=\"prettyprint\" class=\"language-c\">root@T490:~# cat \/boot\/grub\/grub.cfg | grep nokaslr\n    linux   \/boot\/vmlinuz-5.10.14 root=UUID=c4072cd3-9ed9-4376-b85c-95d1306e817b ro  noquiet nopti nokaslr console=ttyS0,115200<\/code><\/pre>\n<h1>host\u7aef\u51c6\u5907<\/h1>\n<p>\u524d\u9762\u6211\u4eec\u5c31\u5b8c\u6210\u4e86guest\u7aef\u5373\u8c03\u8bd5\u76ee\u6807\u673a\u7684\u914d\u7f6e\uff0c\u7136\u540e\u5c31\u662f\u51c6\u5907host\u7aef\uff0c\u53ef\u4ee5\u7528gdb\uff0c\u4ea6\u53ef\u4ee5\u7528vscode\u7b49gui\u5de5\u5177\uff0c\u751a\u81f3\u53ef\u4ee5\u7528Windows Visual Studio\u914d\u5408VisualKernel\u5de5\u5177\u6765\u8c03\u8bd5Linux\u7cfb\u7edf\u3002<\/p>\n<h2>\u6e90\u4ee3\u7801\u53ca\u8c03\u8bd5\u7b26\u53f7vmlinux<\/h2>\n<p>\u5c06\u76f8\u5e94\u6e90\u4ee3\u7801\u53ca\u8c03\u8bd5\u7b26\u53f7vmlinux\u590d\u5236\u5230host\u7aef\u5373\u53ef\uff0c\u590d\u5236\u65f6\u6ce8\u610f\u8f6f\u94fe\u63a5\u95ee\u9898\uff0c\u6bd4\u5982\uff1a<\/p>\n<pre><code class=\"prettyprint\" class=\"language-c\">vmlinux-gdb.py -&gt; \/BUILD\/linux-5.12\/scripts\/gdb\/vmlinux-gdb.py<\/code><\/pre>\n<h2>\u8bbe\u7f6egdb\u7684auto-load\u8def\u5f84<\/h2>\n<p>\u5efa\u8bae\u76f4\u63a5\u4fee\u6539 ~\/.gdbinit\u6587\u4ef6\uff0c\u5f53\u7136\u624b\u52a8\u6267\u884c\u4ea6\u53ef\uff0c\u5f53\u5904\u7406\u591a\u4e2a\u8c03\u8bd5\u673a\u65f6\u6700\u597d\u901a\u8fc7.gdbinit\u6765\u4fdd\u5b58\u914d\u7f6e\uff1a<\/p>\n<pre><code class=\"prettyprint\" class=\"language-c\">matt@T490 \/W\/K\/debian-10.7&gt; cat ~\/.gdbinit \nadd-auto-load-safe-path \/usr\/share\/gdb\/python\/gdb\/\nadd-auto-load-safe-path \/usr\/share\/gdb\/python\/\nadd-auto-load-safe-path \/usr\/share\/gdb\/\nadd-auto-load-safe-path \/BUILD\/linux-5.12\nset auto-load python-scripts on\nsource \/BUILD\/pwndbg\/gdbinit.py<\/code><\/pre>\n<p>\u6211\u672c\u5730\u7684\u8c03\u8bd5\u73af\u5883\u91cc\u5b89\u88c5\u7684\u662fpwndbg\uff0c\u5df2\u914d\u7f6e\u597d\u4e86python\u7ec4\u4ef6<\/p>\n<h2>\u542f\u52a8gdb<\/h2>\n<pre><code class=\"prettyprint\" class=\"language-c\">cd \/BUILD\/linux-5.12; gdb .\/vmlinux<\/code><\/pre>\n<p>\u5728gdb\u4e2d\u6267\u884c\u4ee5\u4e0b\u6267\u884c\u5f00\u542f\u5185\u6838\u8c03\u8bd5\uff1a<\/p>\n<pre><code class=\"prettyprint\" class=\"language-c\">    target remote :44333<\/code><\/pre>\n<p>\u7136\u540egdb\u4f1a\u81ea\u52a8\u52a0\u8f7dpwndbg\u5e76\u4e2d\u65ad\u76ee\u6807\u673a\uff1a\n<img decoding=\"async\" src=\"https:\/\/blog.dynox.cn\/wp-content\/uploads\/2022\/05\/GDB-pwndbg1.png\" alt=\"Linux kernel 5.12 gdb\" \/><\/p>\n<p>\u5728\u5185\u6838gdb scripts\u52a0\u8f7d\u6210\u529f\u7684\u60c5\u51b5\u4e0b\u53ef\u4ee5\u6267\u884clx\u7cfb\u5217\u6269\u5c55\u547d\u4ee4\u4ee5\u83b7\u53d6\u5185\u6838\u4fe1\u606f\uff1a\n<img decoding=\"async\" src=\"https:\/\/blog.dynox.cn\/wp-content\/uploads\/2022\/05\/GDB-pwndbg3.png\" alt=\"Linux kernel 5.12 gdb scripts\" \/><\/p>\n<p>\u53ef\u901a\u8fc7apropos lx\u67e5\u8be2\u76f8\u5e94\u7684gdb scripts\u6269\u5c55\u547d\u4ee4\uff0c\u5982lx-lsmod,  \u53d8\u91cf\uff1a$lx_current()\uff1a<\/p>\n<pre><code class=\"prettyprint\" class=\"language-c\">gdb&gt; apropos lx\nfunction lx_clk_core_lookup -- Find struct clk_core by name\nfunction lx_current -- Return current task.\nfunction lx_device_find_by_bus_name -- Find struct device by bus and name (both strings)\nfunction lx_device_find_by_class_name -- Find struct device by class and name (both strings)\nfunction lx_module -- Find module by name and return the module variable.\nfunction lx_per_cpu -- Return per-cpu variable.\nfunction lx_rb_first -- Lookup and return a node from an RBTree\nfunction lx_rb_last -- Lookup and return a node from an RBTree.\nfunction lx_rb_next -- Lookup and return a node from an RBTree.\nfunction lx_rb_prev -- Lookup and return a node from an RBTree.\nfunction lx_task_by_pid -- Find Linux task by PID and return the task_struct variable.\nfunction lx_thread_info -- Calculate Linux thread_info from task variable.\nfunction lx_thread_info_by_pid -- Calculate Linux thread_info from task variable found by pid\nlx-clk-summary -- Print clk tree summary\nlx-cmdline --  Report the Linux Commandline used in the current kernel.\nlx-configdump -- Output kernel config to the filename specified as the command\nlx-cpus -- List CPU status arrays\nlx-device-list-bus -- Print devices on a bus (or all buses if not specified)\nlx-device-list-class -- Print devices in a class (or all classes if not specified)\nlx-device-list-tree -- Print a device and its children recursively\nlx-dmesg -- Print Linux kernel log buffer.\nlx-fdtdump -- Output Flattened Device Tree header and dump FDT blob to the filename\nlx-genpd-summary -- Print genpd summary\nlx-iomem -- Identify the IO memory resource locations defined by the kernel\nlx-ioports -- Identify the IO port resource locations defined by the kernel\nlx-list-check -- Verify a list consistency\nlx-lsmod -- List currently loaded modules.\nlx-mounts -- Report the VFS mounts of the current process namespace.\nlx-ps -- Dump Linux tasks.\nlx-symbols -- (Re-)load symbols of Linux kernel and currently loaded modules.\nlx-timerlist -- Print \/proc\/timer_list\nlx-version --  Report the Linux Version of the current kernel.<\/code><\/pre>\n<p><img decoding=\"async\" src=\"https:\/\/blog.dynox.cn\/wp-content\/uploads\/2022\/05\/qemu-img1.png\" alt=\"Linux kernel 6.0 gdb\" \/>\n<img decoding=\"async\" src=\"https:\/\/blog.dynox.cn\/wp-content\/uploads\/2022\/05\/qemu-img2.png\" alt=\"Linux kernel 6.0 gdb\" \/>\n<img decoding=\"async\" src=\"https:\/\/blog.dynox.cn\/wp-content\/uploads\/2022\/05\/qemu-img3.png\" alt=\"Linux kernel 6.0 gdb\" \/><\/p>\n<h1>gdb\u5e38\u7528\u547d\u4ee4\u53ca\u8c03\u8bd5\u6280\u5de7<\/h1>\n<h2>\u5e38\u7528\u547d\u4ee4<\/h2>\n<p>\u5e38\u7528gdb\u6307\u4ee4\uff1abt, disass, step, next, break\/delete, print, x\/[i\/g\/d]\u7b49\uff0c\u5177\u4f53\u53ef\u67e5\u8be2gdb\u547d\u4ee4\u624b\u518c<\/p>\n<h2>\u65ad\u70b9\u8bbe\u7f6e<\/h2>\n<p>\u4f7f\u7528break\u547d\u4ee4\u53ef\u8bbe\u7f6e\u65ad\u70b9\uff0c\u652f\u6301\u4ee5\u4e0b\u51e0\u79cd\u547d\u4ee4\u683c\u5f0f\uff1a<\/p>\n<ul>\n<li>break \u51fd\u6570\u540d: break dump_stack<\/li>\n<li>break source_file:line_number: break smith_hook.c:70<\/li>\n<li>break <em>\u903b\u8f91\u5730\u5740\uff1abreak <\/em>0xffffffff81003010\uff0c\u53ef\u4ee5\u5f3a\u5236break\u8df3\u8fc7\u7b26\u53f7\/\u51fd\u6570\u540d\u8bc6\u522b<\/li>\n<\/ul>\n<p>\u9488\u5bf9\u6a21\u5757\u7684\u8c03\u8bd5\uff0c\u53ef\u4ee5\u8003\u8651\u5728\u6a21\u5757\u4e2d\u589e\u52a0\u4e0d\u5e38\u7528\u5185\u6838\u51fd\u6570\u7684\u8c03\u7528\uff0c\u5982dump_stack\uff1b\u6a21\u5757\u52a0\u8f7d\u524d\u662f\u65e0\u6cd5\u8bbe\u7f6e\u65ad\u70b9\u7684\uff0c\u4f46\u5185\u6838\u51fd\u6570\u7684\u8c03\u7528\u662f\u53ef\u4ee5\u7684\uff0c\u8fd9\u6837\u5c31\u53ef\u4ee5\u5728\u6a21\u5757\u8c03\u7528\u6307\u5b9a\u5185\u6838\u51fd\u6570\u65f6\u89e6\u53d1\u65ad\u70b9\uff0c\u7136\u540e\u518d\u901a\u8fc7add-symbol-file \/path_to\/ko_file base_address\uff0cbase_address\u6216\u901a\u8fc7lx-lsmod\u67e5\u8be2\u5f97\u5230\uff0c\u5373\u6a21\u5757\u5728\u5185\u6838\u4e2d\u7684\u5185\u5b58\u4f4d\u7f6e\uff0c\u4e4b\u540e\u5c31\u53ef\u4ee5\u8bbf\u95ee\u6a21\u5757\u5185\u7684\u7b26\u53f7\u4e86\uff0c\u5982\u5168\u5c40\u53d8\u91cf\u6216\u5185\u90e8\u51fd\u6570<\/p>\n<h2>X86_64\u4f4d\u73af\u5883\u8c03\u8bd532\u4f4d\u865a\u62df\u673a<\/h2>\n<p>\u9700\u8981\u5728attach\u524d\u8bbe\u7f6e\u76ee\u6807\u53ca\u5f53\u524d\u7cfb\u7edf\u67b6\u6784\uff0c\u5982 set architecture i386:x86-64\u3002\u5efa\u8bae\u5b89\u88c5gdb-multiarch\uff0c\u4ee5\u514d\u7cfb\u7edf\u81ea\u5e26gdb\u4e0d\u652f\u6301\u591a\u79cd\u67b6\u6784\uff1a<\/p>\n<pre><code class=\"prettyprint\" class=\"language-c\">pwndbg&gt; target remote :44323\nRemote debugging using :44323\nwarning: Selected architecture i386 is not compatible with reported target architecture i386:x86-64\nwarning: Architecture rejected target-supplied description\nRemote 'g' packet reply is too long (expected 312 bytes, got 608 bytes): 102b91c10......0000\n\npwndbg&gt; set architecture i386:x86-64\nThe target architecture is set to \"i386:x86-64\".\npwndbg&gt; target remote :44323\nRemote debugging using :44323\n0x00000000c1912b23 in default_idle () at arch\/x86\/kernel<\/code><\/pre><\/div>","protected":false},"excerpt":{"rendered":"<p>\u5185\u6838\u51c6\u5907 \u5185\u6838\u914d\u7f6e \u5efa\u8bae\u4ece\u6e90\u4ee3\u7801\u91cd\u65b0\u7f16\u8bd1\u5185\u6838\uff0c\u8fd9\u6837\u53ef\u4ee5\u66f4\u65b9\u4fbf\u5730\u8fdb\u884c\u6e90\u4ee3\u7801\u7ea7\u8c03\u8bd5\uff1b\u5f53\u7136\u81ea\u5df1\u4ece\u53d1\u884c\u7248\u5b98\u7f51\u4e0b\u8f7d\u76f8\u5e94\u5185 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"ngg_post_thumbnail":0,"footnotes":""},"categories":[9],"tags":[642,645,646,644,618,643,394,647],"views":14577,"_links":{"self":[{"href":"https:\/\/blog.dynox.cn\/index.php?rest_route=\/wp\/v2\/posts\/1702"}],"collection":[{"href":"https:\/\/blog.dynox.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.dynox.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.dynox.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.dynox.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1702"}],"version-history":[{"count":6,"href":"https:\/\/blog.dynox.cn\/index.php?rest_route=\/wp\/v2\/posts\/1702\/revisions"}],"predecessor-version":[{"id":1775,"href":"https:\/\/blog.dynox.cn\/index.php?rest_route=\/wp\/v2\/posts\/1702\/revisions\/1775"}],"wp:attachment":[{"href":"https:\/\/blog.dynox.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1702"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.dynox.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1702"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.dynox.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1702"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}