{"id":136,"date":"2018-02-06T12:33:46","date_gmt":"2018-02-06T20:33:46","guid":{"rendered":"https:\/\/sysprogs.com\/tutorials\/?p=136"},"modified":"2018-02-06T12:40:10","modified_gmt":"2018-02-06T20:40:10","slug":"creating-a-basic-linux-kernel-module-with-visual-studio","status":"publish","type":"post","link":"https:\/\/sysprogs.com\/VisualKernel\/tutorials\/basicmodule\/","title":{"rendered":"Creating a basic Linux Kernel Module with Visual Studio"},"content":{"rendered":"<p>This tutorial shows how to create, build and debug a basic Linux kernel module with Visual Studio and VisualKernel. Before you begin, install VisualKernel 3.0 or later and ensure you have a Linux machine running a recent version of the Linux kernel.<\/p>\n<ol>\n<li>Start Visual Studio and open the VisualKernel Module project wizard:<img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-138\" src=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/01-module.png\" alt=\"\" width=\"819\" height=\"507\" srcset=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/01-module.png 819w, https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/01-module-300x186.png 300w, https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/01-module-768x475.png 768w\" sizes=\"(max-width: 819px) 100vw, 819px\" \/><\/li>\n<li>Proceed with the\u00a0 default &#8220;Create a new kernel module from template&#8221; option. For better performance and more accurate IntelliSense we recommend selecting the Advanced Kernel Project Subsystem:\u00a0<img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-139\" src=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/02-type.png\" alt=\"\" width=\"822\" height=\"642\" srcset=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/02-type.png 822w, https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/02-type-300x234.png 300w, https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/02-type-768x600.png 768w\" sizes=\"(max-width: 822px) 100vw, 822px\" \/><\/li>\n<li>Select your Linux machine in the &#8220;Targeted Linux Computer&#8221; field. If you have not\u00a0used that machine with VisualKernel before, click &#8220;Create a new SSH connection&#8221;:\u00a0<img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-150\" src=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/03-newconn-1.png\" alt=\"\" width=\"822\" height=\"642\" srcset=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/03-newconn-1.png 822w, https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/03-newconn-1-300x234.png 300w, https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/03-newconn-1-768x600.png 768w\" sizes=\"(max-width: 822px) 100vw, 822px\" \/><\/li>\n<li>If you are creating a new SSH connection, ensure you use the user\u00a0that can run commands as root via\u00a0sudo, as otherwise VisualKernel won&#8217;t be able to load your kernel module:\u00a0<img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-141\" src=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/04-settings.png\" alt=\"\" width=\"822\" height=\"642\" srcset=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/04-settings.png 822w, https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/04-settings-300x234.png 300w, https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/04-settings-768x600.png 768w\" sizes=\"(max-width: 822px) 100vw, 822px\" \/><\/li>\n<li>Ensure that the &#8220;Download and install kernel symbols&#8221; checkbox is checked. Kernel symbols are not strictly required when debugging kernel modules, however\u00a0installing them will greatly improve the debugging usability by\u00a0showing the source code of the kernel functions, stepping through them and\u00a0allowing to explore it via Code Map:<img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-142\" src=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/05-build.png\" alt=\"\" width=\"822\" height=\"642\" srcset=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/05-build.png 822w, https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/05-build-300x234.png 300w, https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/05-build-768x600.png 768w\" sizes=\"(max-width: 822px) 100vw, 822px\" \/><\/li>\n<li>Press &#8220;Next&#8221;. VisualKernel will automatically download and install the kernel symbols for most common Linux distros:<img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-143\" src=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/06-prepare.png\" alt=\"\" width=\"736\" height=\"553\" srcset=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/06-prepare.png 736w, https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/06-prepare-300x225.png 300w\" sizes=\"(max-width: 736px) 100vw, 736px\" \/><\/li>\n<li>On the last page of the wizard select your debug settings. If your module does not directly interact with the hardware, we recommend using VMWare\u00a0with the VMWare-specific debug connection. It utilizes a high-performance gdb stub\u00a0that runs\u00a0inside VMWare (and outside the Linux kernel) and is much more stable than debug methods provided by the kernel itself. For embedded ARM-based boards we recommend using JTAG as the debug method:\u00a0<img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-145\" src=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/08-debug.png\" alt=\"\" width=\"822\" height=\"642\" srcset=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/08-debug.png 822w, https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/08-debug-300x234.png 300w, https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/08-debug-768x600.png 768w\" sizes=\"(max-width: 822px) 100vw, 822px\" \/><\/li>\n<li>Press &#8220;Finish&#8221; to create the project. Build it via Ctrl-Shift-B:<img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-146\" src=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/09-build.png\" alt=\"\" width=\"1041\" height=\"711\" srcset=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/09-build.png 1041w, https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/09-build-300x205.png 300w, https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/09-build-768x525.png 768w, https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/09-build-1024x699.png 1024w, https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/09-build-130x90.png 130w\" sizes=\"(max-width: 1041px) 100vw, 1041px\" \/><\/li>\n<li>Set a breakpoint in the init function of your module and press F5 to start debugging the project. VisualKernel will automatically configure\u00a0your kernel for debugging and will start the debug session. Once the breakpoint is hit, you can debug your module as if it was a regular program:<img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-147\" src=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/10-loaded.png\" alt=\"\" width=\"1041\" height=\"711\" srcset=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/10-loaded.png 1041w, https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/10-loaded-300x205.png 300w, https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/10-loaded-768x525.png 768w, https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/10-loaded-1024x699.png 1024w, https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/10-loaded-130x90.png 130w\" sizes=\"(max-width: 1041px) 100vw, 1041px\" \/><\/li>\n<li>If you are using the Advanced Kernel Project Subsystem, you can conveniently explore the sources of the kernel itself by locating it in Solution Explorer and double-clicking on it:<img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-148\" src=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/11-ref.png\" alt=\"\" width=\"1041\" height=\"711\" srcset=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/11-ref.png 1041w, https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/11-ref-300x205.png 300w, https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/11-ref-768x525.png 768w, https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/11-ref-1024x699.png 1024w, https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/11-ref-130x90.png 130w\" sizes=\"(max-width: 1041px) 100vw, 1041px\" \/><\/li>\n<li>This will\u00a0create a special\u00a0kernel release project, store it in your kernel cache directory and add it to Solution Explorer. Once the kernel release project is present in the solution, you can use functions like Go to Definition, Peek Definition and Code Map\u00a0to explore the kernel sources:<img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-149\" src=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/12-peek.png\" alt=\"\" width=\"1041\" height=\"711\" srcset=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/12-peek.png 1041w, https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/12-peek-300x205.png 300w, https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/12-peek-768x525.png 768w, https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/12-peek-1024x699.png 1024w, https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/12-peek-130x90.png 130w\" sizes=\"(max-width: 1041px) 100vw, 1041px\" \/><\/li>\n<li>If you have\u00a0created the project without downloading the kernel symbols, you can always select them later via the Module Build Settings page of VisualKernel Project Properties:\u00a0<img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-151\" src=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/13-syms.png\" alt=\"\" width=\"1041\" height=\"711\" srcset=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/13-syms.png 1041w, https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/13-syms-300x205.png 300w, https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/13-syms-768x525.png 768w, https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/13-syms-1024x699.png 1024w, https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/13-syms-130x90.png 130w\" sizes=\"(max-width: 1041px) 100vw, 1041px\" \/><\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>This tutorial shows how to create, build and debug a basic Linux kernel module with Visual Studio and VisualKernel. Before<\/p>\n","protected":false},"author":1,"featured_media":153,"comment_status":"closed","ping_status":"closed","sticky":true,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19],"tags":[20],"_links":{"self":[{"href":"https:\/\/sysprogs.com\/tutorials\/wp-json\/wp\/v2\/posts\/136"}],"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=136"}],"version-history":[{"count":2,"href":"https:\/\/sysprogs.com\/tutorials\/wp-json\/wp\/v2\/posts\/136\/revisions"}],"predecessor-version":[{"id":152,"href":"https:\/\/sysprogs.com\/tutorials\/wp-json\/wp\/v2\/posts\/136\/revisions\/152"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/sysprogs.com\/tutorials\/wp-json\/wp\/v2\/media\/153"}],"wp:attachment":[{"href":"https:\/\/sysprogs.com\/tutorials\/wp-json\/wp\/v2\/media?parent=136"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sysprogs.com\/tutorials\/wp-json\/wp\/v2\/categories?post=136"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sysprogs.com\/tutorials\/wp-json\/wp\/v2\/tags?post=136"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}