{"id":94,"date":"2022-09-15T15:10:37","date_gmt":"2022-09-15T22:10:37","guid":{"rendered":"https:\/\/sysprogs.com\/tutorials\/?p=94"},"modified":"2022-09-17T10:03:46","modified_gmt":"2022-09-17T17:03:46","slug":"debugging-linux-kernel-with-visual-studio-virtualbox","status":"publish","type":"post","link":"https:\/\/sysprogs.com\/VisualKernel\/tutorials\/virtualbox\/","title":{"rendered":"Debugging Linux Kernel with Visual Studio + VirtualBox"},"content":{"rendered":"<p id=\"title\">This tutorial shows how to debug a Linux kernel module using Visual Studio and the internal VirtualBox debugger. This method of debugging is faster than using KGDB with a virtual COM port, however it requires installing a development build of VirtualBox.<\/p>\n<p>Before you begin, please install Visual Studio, <a href=\"https:\/\/sysprogs.com\/VisualKernel\/documentation\/vboxdev\/\">development build of VirtualBox<\/a> and the latest\u00a0<a href=\"http:\/\/visualkernel.com\/download\/\">VisualKernel<\/a>.<\/p>\n<p>Then follow the steps below:<\/p>\n<ol>\n<li>Launch Visual Studio. Begin creating a new project with VisualKernel kernel module wizard:<a href=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/01-newprj-1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-612\" src=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/01-newprj-1.png\" alt=\"\" width=\"1014\" height=\"675\" \/><\/a><\/li>\n<li>Enter the name and location for your project:<a href=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/02-name-1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-613\" src=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/02-name-1.png\" alt=\"\" width=\"1014\" height=\"675\" \/><\/a><\/li>\n<li>On the first wizard page select &#8220;Create a new kernel module&#8221; and continue by clicking &#8220;Next&#8221;:<a href=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/03-hello-1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-614\" src=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/03-hello-1.png\" alt=\"\" width=\"886\" height=\"693\" \/><\/a><\/li>\n<li>On the next page select &#8220;Setup a new target for running Linux Kernels&#8221;. If you already have a VM you would like to debug, select it from the list. You can also select &#8220;Download a ready-to-debug image&#8221; to automatically install a VM image along with the matching kernel symbols:<a href=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/04-new.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-615\" src=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/04-new.png\" alt=\"\" width=\"886\" height=\"693\" \/><\/a><\/li>\n<li>If you are using an existing VM, VisualKernel will ask for the SSH settings to connect to the VM. Normally, just using the host name or IP address should work:<a href=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/06a-connect.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-616\" src=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/06a-connect.png\" alt=\"\" width=\"586\" height=\"510\" \/><\/a><\/li>\n<li>If you cannot connect to the VM, double-check that it is using bridged networking linked with your primary network adapter, and that the &#8220;cable connected&#8221; checkbox is checked:<a href=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/05a-testsettings.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-617\" src=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/05a-testsettings.png\" alt=\"\" width=\"700\" height=\"524\" \/><\/a><\/li>\n<li>Press &#8220;Next&#8221;. VisualKernel will try building a basic kernel module on the target, and will install the missing dependencies if required:<a href=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/06-test.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-618\" src=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/06-test.png\" alt=\"\" width=\"470\" height=\"236\" \/><\/a><\/li>\n<li>Proceed with the default settings on the next wizard page:<a href=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/07-sources-1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-619\" src=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/07-sources-1.png\" alt=\"\" width=\"886\" height=\"693\" \/><\/a><\/li>\n<li>On the last wizard page select &#8220;Debug a Virtual Machine&#8221;, then select &#8220;VirtualBox&#8221; and specify the path to your .vbox file:<a href=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/08-debug-1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-620\" src=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/08-debug-1.png\" alt=\"\" width=\"886\" height=\"693\" \/><\/a><\/li>\n<li>Press &#8220;Finish&#8221; to generate your project. Now you can build it using the &#8220;Build All&#8221; command in Visual Studio:<a href=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/09-build-1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-621\" src=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/09-build-1.png\" alt=\"\" width=\"1385\" height=\"923\" \/><\/a><\/li>\n<li>Due to the way VirtualBox handles memory access, attaching the kernel debugger to a machine that is actively running user-space code will prevent it from setting kernel breakpoints. VisualKernel will attempt to work around it by resuming the VM and requesting another break. You can minimize the need for the workarounds by stopping any user-mode processes with heavy CPU usage until the user-mode CPU utilization drops below 10%: <a href=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/10-nocpu-1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-623\" src=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/10-nocpu-1.png\" alt=\"\" width=\"802\" height=\"673\" \/><\/a><\/li>\n<li>Set a breakpoint in your module&#8217;s init() function and press F5 to begin debugging. VisualKernel will automatically start debugging, load the kernel module and the breakpoint will trigger:<a href=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/11-breakpoint.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-624\" src=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/11-breakpoint.png\" alt=\"\" width=\"1385\" height=\"923\" \/><\/a>You can now use the normal debugging techniques to debug your kernel module.<\/li>\n<\/ol>\n<p>You can also use VisualKernel to debug VMWare and QEMU virtual machines. See <a href=\"https:\/\/sysprogs.com\/VisualKernel\/documentation\/vms\">this page<\/a> for more details.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This tutorial shows how to debug a Linux kernel module using Visual Studio and the internal VirtualBox debugger. This method<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[42],"tags":[25,26],"_links":{"self":[{"href":"https:\/\/sysprogs.com\/tutorials\/wp-json\/wp\/v2\/posts\/94"}],"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=94"}],"version-history":[{"count":4,"href":"https:\/\/sysprogs.com\/tutorials\/wp-json\/wp\/v2\/posts\/94\/revisions"}],"predecessor-version":[{"id":670,"href":"https:\/\/sysprogs.com\/tutorials\/wp-json\/wp\/v2\/posts\/94\/revisions\/670"}],"wp:attachment":[{"href":"https:\/\/sysprogs.com\/tutorials\/wp-json\/wp\/v2\/media?parent=94"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sysprogs.com\/tutorials\/wp-json\/wp\/v2\/categories?post=94"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sysprogs.com\/tutorials\/wp-json\/wp\/v2\/tags?post=94"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}