{"id":516,"date":"2022-09-19T09:34:32","date_gmt":"2022-09-19T16:34:32","guid":{"rendered":"https:\/\/sysprogs.com\/tutorials\/?p=516"},"modified":"2022-09-19T09:36:52","modified_gmt":"2022-09-19T16:36:52","slug":"exploring-linux-kernel-with-pre-built-vm-images","status":"publish","type":"post","link":"https:\/\/sysprogs.com\/VisualKernel\/tutorials\/vms\/","title":{"rendered":"Using Pre-built VM Images to Develop Kernel Modules Efficiently"},"content":{"rendered":"<p>This tutorial shows how to quickly explore the Linux kernel internals using the prebuilt VM images installed by VisualKernel. We will show how to download the ready-to-use Debian image, automatically create a Qemu virtual machine based on it and use it to experiment with a basic character device driver.<\/p>\n<p>Before you begin, install VisualKernel 4.0 or later.<\/p>\n<ol>\n<li>Start Visual Studio and open the VisualKernel Linux Kernel Module Wizard:<a href=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2022\/09\/01-newmod.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-517 aligncenter\" src=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2022\/09\/01-newmod.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\/2022\/09\/02-name.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-518 aligncenter\" src=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2022\/09\/02-name.png\" alt=\"\" width=\"1014\" height=\"675\" \/><\/a><\/li>\n<li>Pick the project template you would like to use. In this tutorial we will cover the <strong>character device<\/strong> template showing how to create a basic device in the <strong>\/dev<\/strong> directory: <a href=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2022\/09\/03-device.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-519 aligncenter\" src=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2022\/09\/03-device.png\" alt=\"\" width=\"886\" height=\"693\" \/><\/a><\/li>\n<li>On the next page of the wizard select &#8220;<strong>Setup a new target for running Linux Kernels -&gt; Download a ready-to-debug VM Image<\/strong>&#8220;:<a href=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2022\/09\/04-newvm.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-520 aligncenter\" src=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2022\/09\/04-newvm.png\" alt=\"\" width=\"886\" height=\"693\" \/><\/a><\/li>\n<li>VisualKernel will show the list of VM images available for download. Pick &#8220;<strong>Debian 11<\/strong>&#8221; and choose a password for your VM (e.g. <strong>vmuser<\/strong>):<a href=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2022\/09\/05-debian.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-521 aligncenter\" src=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2022\/09\/05-debian.png\" alt=\"\" width=\"786\" height=\"593\" \/><\/a>For the best kernel debugging experience with the pre-built VMs we recommend <a href=\"https:\/\/sysprogs.com\/VisualKernel\/documentation\/qemu\/#acceleration\">enabling the Hyper-V platform<\/a> and letting VisualKernel automatically download and configure QEMU. However, you can also use VMWare or VirtualBox if your system supports kernel debugging with them. See <a href=\"https:\/\/sysprogs.com\/VisualKernel\/documentation\/vms\">this page<\/a> for a detailed overview of the supported virtualization software.<\/li>\n<li>Press &#8220;Download&#8221;. VisualKernel will automatically fetch the VM image and will create a virtual machine based on it. It will also fetch the pre-indexed kernel sources and symbols, so you will be able to use IntelliSense without waiting for it to index the kernel. Once the VM has been created, it will be started automatically: <a href=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2022\/09\/06-vm.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-522 aligncenter\" src=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2022\/09\/06-vm.png\" alt=\"\" width=\"642\" height=\"537\" \/><\/a><\/li>\n<li>Note that VisualKernel will remember the created VM under the &#8220;previously configured targets&#8221; list. So next time you want to create a kernel-mode project for this VM, you can simply pick it from the list on the <strong>Target Type<\/strong> page: <a href=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2022\/09\/07-list.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-523 aligncenter\" src=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2022\/09\/07-list.png\" alt=\"\" width=\"886\" height=\"693\" \/><\/a><\/li>\n<li>Proceed with the default source code access settings: <a href=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2022\/09\/08-sources.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-524 aligncenter\" src=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2022\/09\/08-sources.png\" alt=\"\" width=\"886\" height=\"693\" \/><\/a><\/li>\n<li>Finally, it is time to select the debugging settings. VisualKernel can automatically debug the QEMU VMs, so simply proceed with the default settings and press &#8220;Finish&#8221; to create the project:<a href=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2022\/09\/09-debug.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-525 aligncenter\" src=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2022\/09\/09-debug.png\" alt=\"\" width=\"886\" height=\"693\" \/><\/a><\/li>\n<li>Once the project is created, you can build it by pressing Ctrl-Shift-B. Open the <strong>BasicCharacterDevice.c<\/strong> file and use the Code Explorer to quickly navigate to the <strong>CharDevDemo_read()<\/strong> function:<a href=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2022\/09\/10-functions.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-526 aligncenter\" src=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2022\/09\/10-functions.png\" alt=\"\" width=\"1278\" height=\"812\" \/><\/a><\/li>\n<li>Add the following code to the read function:\n<pre class=\"\">static int ReadCount = 0;\r\nReadCount++;<\/pre>\n<p><a href=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2022\/09\/11-readcount.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-527 aligncenter\" src=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2022\/09\/11-readcount.png\" alt=\"\" width=\"1278\" height=\"812\" \/><\/a><\/li>\n<li>Press F5 to begin debugging. You can set breakpoints and step through the code as if you were debugging a regular application:<a href=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2022\/09\/debug.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-549 size-full\" src=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2022\/09\/debug.png\" alt=\"\" width=\"1278\" height=\"763\" \/><\/a><\/li>\n<li>Once the module is loaded, the <strong>VisualKernel Output<\/strong> window will display the output from the kernel module advising to open the <strong>\/dev\/PrebuiltImageDemo<\/strong> file. Right-click on the project in Solution Explorer and select &#8220;Open a new SSH Window&#8221; to connect to the target via SSH:<a href=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2022\/09\/12-debug.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-528 aligncenter\" src=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2022\/09\/12-debug.png\" alt=\"\" width=\"1278\" height=\"813\" \/><\/a><\/li>\n<li>Open the Live Watch window via <strong>Debug-&gt;Windows-&gt;Live Watch<\/strong> and locate the <strong>ReadCount<\/strong> variable:<a href=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2022\/09\/13-count.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-529 aligncenter\" src=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2022\/09\/13-count.png\" alt=\"\" width=\"1278\" height=\"812\" \/><\/a><\/li>\n<li>Try opening the <strong>\/dev\/PrebuiltImageDemo<\/strong> file from the terminal window. Note how the ReadCount value begins growing in real time and VisualKernel reports it without ever having to stop the target: <a href=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2022\/09\/14-plot.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-530 aligncenter\" src=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2022\/09\/14-plot.png\" alt=\"\" width=\"1279\" height=\"895\" \/><\/a> The live watch reporting is done via UDP by the <strong>LinuxKernelDebugHelper<\/strong> module that is built and automatically installed by VisualKernel.<\/li>\n<li>Now we will show how to quickly navigate the Linux kernel source code using VisualKernel&#8217;s Code Explorer. Open the <strong>PrebuiltImageDemo_main.c<\/strong> file, go to the <strong>AllocateBasicCharacterDevice()<\/strong> call and click the &#8220;Go to Code Explorer&#8221; button in the top right corner of the next editor:<a href=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2022\/09\/15-codeexp-1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-546 aligncenter\" src=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2022\/09\/15-codeexp-1.png\" alt=\"\" width=\"1278\" height=\"895\" \/><\/a><\/li>\n<li>Click the &#8220;details&#8221; button to view various details about the <strong>AllocateBasicCharacterDevice()<\/strong> function: <a href=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2022\/09\/16-details.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-532 aligncenter\" src=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2022\/09\/16-details.png\" alt=\"\" width=\"1278\" height=\"895\" \/><\/a><\/li>\n<li>We will now restrict the shown details to the incoming and outgoing calls, and will use it to quickly find other functions calling similar API. Shift+click on the &#8220;Outgoing calls&#8221; item under the &#8220;Show&#8221; menu, and then click on &#8220;Incoming calls&#8221;: <a href=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2022\/09\/17-relations.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-533 aligncenter\" src=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2022\/09\/17-relations.png\" alt=\"\" width=\"1278\" height=\"763\" \/><\/a><\/li>\n<li>Locate the outgoing call to <strong>cdev_add()<\/strong> and expand the Incoming calls node under it. It will instantly show other functions <strong>in the entire kernel codebase<\/strong>, that also call <strong>cdev_add()<\/strong>. Locate the <strong>add_port()<\/strong> function and double-click on it to go to definition:<a href=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2022\/09\/18-cdev.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-534 aligncenter\" src=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2022\/09\/18-cdev.png\" alt=\"\" width=\"1278\" height=\"763\" \/><\/a> You can use this mechanism to quickly find examples of various functions or structures in other modules, or in the kernel itself.<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>This tutorial shows how to quickly explore the Linux kernel internals using the prebuilt VM images installed by VisualKernel. We<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19],"tags":[56,26],"_links":{"self":[{"href":"https:\/\/sysprogs.com\/tutorials\/wp-json\/wp\/v2\/posts\/516"}],"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=516"}],"version-history":[{"count":6,"href":"https:\/\/sysprogs.com\/tutorials\/wp-json\/wp\/v2\/posts\/516\/revisions"}],"predecessor-version":[{"id":672,"href":"https:\/\/sysprogs.com\/tutorials\/wp-json\/wp\/v2\/posts\/516\/revisions\/672"}],"wp:attachment":[{"href":"https:\/\/sysprogs.com\/tutorials\/wp-json\/wp\/v2\/media?parent=516"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sysprogs.com\/tutorials\/wp-json\/wp\/v2\/categories?post=516"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sysprogs.com\/tutorials\/wp-json\/wp\/v2\/tags?post=516"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}