{"id":88,"date":"2018-02-05T15:08:54","date_gmt":"2018-02-05T23:08:54","guid":{"rendered":"https:\/\/sysprogs.com\/tutorials\/?p=88"},"modified":"2020-04-10T16:57:01","modified_gmt":"2020-04-10T23:57:01","slug":"debugging-a-linux-kernel-without-making-a-project","status":"publish","type":"post","link":"https:\/\/sysprogs.com\/VisualKernel\/tutorials\/quickdebug\/","title":{"rendered":"Debugging a Linux kernel without making a project"},"content":{"rendered":"<p>This tutorial shows how to use the new Kernel Quick Debug feature added to VisualKernel 1.1 to quickly debug a Linux kernel without creating a project or making a module. Before you begin:<\/p>\n<ul>\n<li>Install Visual Studio<\/li>\n<li>Install\u00a0<a href=\"http:\/\/visualkernel.com\/download\/\">VisualKernel<\/a><\/li>\n<li>Ensure that you have a copy of the kernel image with symbols and sources on the Windows machine<\/li>\n<\/ul>\n<ol>\n<li>Start Visual Studio. Select <strong>Debug-&gt;Quick Debug Linux Kernel<\/strong>:<a href=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/01-qd.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-440\" src=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/01-qd.png\" alt=\"\" width=\"1032\" height=\"648\" \/><\/a><\/li>\n<li>Select the Linux machine you want to debug:<a href=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/02-settings.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-442\" src=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/02-settings.png\" alt=\"\" width=\"786\" height=\"603\" \/><\/a><\/li>\n<li>Then, open the &#8220;Kernel Symbols for Debugging&#8221; selector and click &#8220;Download and install kernel symbols&#8221;:<a href=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/03-getsyms-1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-443\" src=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/03-getsyms-1.png\" alt=\"\" width=\"786\" height=\"603\" \/><\/a><\/li>\n<li>VisualKernel will need to scan your kernel to determine its parameters and build a list of in-tree modules. This information will be stored in the kernel cache folder. If you are using this for the first time, you will be aked to select a location of the Kernel Cache folder:<br \/>\n<a href=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/04-dir-1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-444\" src=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/04-dir-1.png\" alt=\"\" width=\"486\" height=\"262\" \/><\/a><br \/>\n<span class=\"warning\">Note that if your kernel files are already stored elsewhere on the Windows machine, you can ignore the warning about 2-5GB of disk space as they will be used from their current location and won&#8217;t be copied.<\/span><\/li>\n<li>In many cases the symbols can be imported fully automatically. If not, you can always import them manually as shown in <a href=\"https:\/\/sysprogs.com\/VisualKernel\/tutorials\/setup\/symbols\/\">this tutorial<\/a>. You can view and manage the installed symbol packages via <strong>Tools-&gt;VisualKernel-&gt;Manage Linux Kernel Symbols<\/strong>:<br \/>\n<a href=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/syms.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-445\" src=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/syms.png\" alt=\"\" width=\"786\" height=\"446\" \/><\/a><\/li>\n<li>Select a kernel connection mode (e.g. KGDBoE to debug via network or VMWare stub if you are using a virtual machine):<a href=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/05-debug.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-446\" src=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/05-debug.png\" alt=\"\" width=\"786\" height=\"581\" \/><\/a><br \/>\nYou can get a detailed overview of the debug methods supported by VisualKernel, along with their pros and cons on <a href=\"https:\/\/sysprogs.com\/VisualKernel\/documentation\/overview\">this page<\/a>.<\/li>\n<li>If you want to avoid entering all the settings again next time, check the &#8216;save the preset&#8217; checkbox and enter a name:<a href=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/06-name.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-447\" src=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/06-name.png\" alt=\"\" width=\"786\" height=\"581\" \/><\/a><\/li>\n<li>When you hit &#8220;Debug&#8221;, VisualKernel will build and install several helper modules that facilitate debugging and eventually stop at the kgdb breakpoint:<a href=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/07-stopped.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-448\" src=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/07-stopped.png\" alt=\"\" width=\"1032\" height=\"648\" \/><\/a><\/li>\n<li>Now you can use the normal debugging techniques. E.g. you can view the list of loaded module and load the symbols for them or set breakpoints on some kernel functions. Let&#8217;s set a breakpoint on <strong>do_sys_open()<\/strong>:<br \/>\n<a href=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/08-bkpt.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-449\" src=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/08-bkpt.png\" alt=\"\" width=\"1032\" height=\"648\" \/><\/a><\/li>\n<li>Press F5 to resume debugging. Eventually some process will try to open a file and the <strong>do_sys_open()<\/strong> breakpoint will trigger: <a href=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/09-stopped.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-450\" src=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/09-stopped.png\" alt=\"\" width=\"1032\" height=\"648\" \/><\/a>You can use the normal debugging techniques (stepping, setting breakpoints, evaluating expressions). Just note that normally the kernel is optimized, so some variables will be shown as &#8216;optimized out&#8217; and stepping will sometimes look awkward, as the optimizer often moves the code around and reuses similar chunks of it. You can also use the GDB session window to send commands to GDB directly:<a href=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/cmd.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-452\" src=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/cmd.png\" alt=\"\" width=\"1032\" height=\"648\" \/><\/a><\/li>\n<li>Press Shift-F5 when you&#8217;re done. If you have saved your Quick Debug preset before, you can start debugging again by simply selecting it in the &#8220;preset&#8221; list in the Quick Debug Linux Kernel window and pressing &#8220;Debug&#8221;:<a href=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/10-preset.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-451\" src=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/10-preset.png\" alt=\"\" width=\"786\" height=\"603\" \/><\/a><\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>This tutorial shows how to use the new Kernel Quick Debug feature added to VisualKernel 1.1 to quickly debug a<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[43],"tags":[23],"_links":{"self":[{"href":"https:\/\/sysprogs.com\/tutorials\/wp-json\/wp\/v2\/posts\/88"}],"collection":[{"href":"https:\/\/sysprogs.com\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sysprogs.com\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sysprogs.com\/tutorials\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sysprogs.com\/tutorials\/wp-json\/wp\/v2\/comments?post=88"}],"version-history":[{"count":3,"href":"https:\/\/sysprogs.com\/tutorials\/wp-json\/wp\/v2\/posts\/88\/revisions"}],"predecessor-version":[{"id":453,"href":"https:\/\/sysprogs.com\/tutorials\/wp-json\/wp\/v2\/posts\/88\/revisions\/453"}],"wp:attachment":[{"href":"https:\/\/sysprogs.com\/tutorials\/wp-json\/wp\/v2\/media?parent=88"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sysprogs.com\/tutorials\/wp-json\/wp\/v2\/categories?post=88"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sysprogs.com\/tutorials\/wp-json\/wp\/v2\/tags?post=88"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}