{"id":134,"date":"2018-02-05T15:48:16","date_gmt":"2018-02-05T23:48:16","guid":{"rendered":"https:\/\/sysprogs.com\/tutorials\/?p=134"},"modified":"2022-09-10T16:48:49","modified_gmt":"2022-09-10T23:48:49","slug":"debugging-raspberry-pi-kernel-module-with-visualkernel","status":"publish","type":"post","link":"https:\/\/sysprogs.com\/VisualKernel\/tutorials\/raspberry\/basicmodule\/","title":{"rendered":"Debugging Raspberry PI kernel module with VisualKernel"},"content":{"rendered":"<p>This tutorial shows how to debug a basic Linux kernel module on Raspberry PI over JTAG using Visual Studio. Before you begin, install\u00a0<a href=\"http:\/\/visualkernel.com\/download\/\">VisualKernel<\/a>\u00a03.0 or later. Also ensure you have\u00a0an extra x64-based Linux machine available (e.g. a stock Ubuntu VM) that\u00a0will be used to build the Raspberry Pi kernel.<\/p>\n<p><strong>Update: Starting from VisualKernel 4.0, you can use pre-built Raspberry Pi kernel images and avoid building the kernel yourself. See <a href=\"https:\/\/sysprogs.com\/VisualKernel\/tutorials\/raspberry\/prebuilt\/\">this tutorial<\/a> for more details.<\/strong><\/p>\n<ol>\n<li>Start Visual Studio. Select File-&gt;New-&gt;Project-&gt;VisualKernel-&gt;Linux Kernel Module Wizard:<a href=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/01-newprj.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-154 size-full\" src=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/01-newprj.png\" alt=\"\" width=\"941\" height=\"653\" srcset=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/01-newprj.png 941w, https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/01-newprj-300x208.png 300w, https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/01-newprj-768x533.png 768w, https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/01-newprj-392x272.png 392w, https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/01-newprj-130x90.png 130w\" sizes=\"(max-width: 941px) 100vw, 941px\" \/><\/a><\/li>\n<li>On the first page select Create a new kernel module -&gt; Hello, World:<img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-155\" src=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/02-hello.png\" alt=\"\" width=\"822\" height=\"642\" srcset=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/02-hello.png 822w, https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/02-hello-300x234.png 300w, https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/02-hello-768x600.png 768w\" sizes=\"(max-width: 822px) 100vw, 822px\" \/><\/li>\n<li>On the Target selection page specify the following settings:\n<ul>\n<li><strong>Targeted Linux computer<\/strong>\u00a0&#8211; your Raspberry PI<\/li>\n<li><strong>Additional build computer<\/strong>\u00a0&#8211; the\u00a0x64 Linux machine that will run the build process<\/li>\n<li><strong>Kernel build environment<\/strong> &#8211; Download and Setup Automatically<br \/>\n<img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-156\" src=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/03-raspi.png\" alt=\"\" width=\"822\" height=\"642\" srcset=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/03-raspi.png 822w, https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/03-raspi-300x234.png 300w, https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/03-raspi-768x600.png 768w\" sizes=\"(max-width: 822px) 100vw, 822px\" \/><\/li>\n<\/ul>\n<\/li>\n<li>Press &#8220;Next&#8221;. VisualKernel will ask for a directory on the build machine where the kernel symbols will be stored. Proceed with the default directory or select another one:<img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-158\" src=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/04-dir.png\" alt=\"\" width=\"486\" height=\"262\" srcset=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/04-dir.png 486w, https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/04-dir-300x162.png 300w\" sizes=\"(max-width: 486px) 100vw, 486px\" \/><\/li>\n<li>VisualKernel will automatically download the Raspberry Pi cross-toolchain and the latest kernel sources. Then it will configure and build the kernel using the cross-toolchain: <img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-160\" src=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/06-progress.png\" alt=\"\" width=\"736\" height=\"553\" srcset=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/06-progress.png 736w, https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/06-progress-300x225.png 300w\" sizes=\"(max-width: 736px) 100vw, 736px\" \/><\/li>\n<li>Once the build is complete, VisualKernel will suggest automatically installing the kernel on Raspberry Pi. Ensure you have a backup of your SD card contents\u00a0and press &#8220;Yes&#8221;:<img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-161\" src=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/07-inst.png\" alt=\"\" width=\"822\" height=\"643\" srcset=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/07-inst.png 822w, https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/07-inst-300x235.png 300w, https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/07-inst-768x601.png 768w\" sizes=\"(max-width: 822px) 100vw, 822px\" \/><\/li>\n<li>VisualKernel will automatically install both the kernel and the\u00a0related modules:<img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-162\" src=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/08-progress.png\" alt=\"\" width=\"736\" height=\"553\" srcset=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/08-progress.png 736w, https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/08-progress-300x225.png 300w\" sizes=\"(max-width: 736px) 100vw, 736px\" \/><\/li>\n<li>Once the installation is complete, restart your Raspberry Pi and press &#8220;OK&#8221; to automatically verify\u00a0that the new kernel has started:<img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-163\" src=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/09-done.png\" alt=\"\" width=\"837\" height=\"164\" srcset=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/09-done.png 837w, https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/09-done-300x59.png 300w, https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/09-done-768x150.png 768w\" sizes=\"(max-width: 837px) 100vw, 837px\" \/><\/li>\n<li>VisualKernel will proceed to the next page of the wizard.\u00a0As you are using an extra Linux machine for building the sources,\u00a0VisualKernel can\u00a0access\u00a0the source code directly via SSH and won&#8217;t need to\u00a0keep 2 separate copies of the sources\u00a0or synchronize include directories. Select &#8220;Access the sources over SSH&#8221; to enable this mode. If you want to keep a copy of the sources on the Windows machine, select &#8220;Store source files on Windows computer&#8221; instead:<br \/>\n<img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-164\" src=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/10-ssh.png\" alt=\"\" width=\"822\" height=\"642\" srcset=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/10-ssh.png 822w, https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/10-ssh-300x234.png 300w, https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/10-ssh-768x600.png 768w\" sizes=\"(max-width: 822px) 100vw, 822px\" \/><\/li>\n<li>On the Debug Settings page select &#8220;Debug an embedded board via JTAG&#8221;. Then plug in your JTAG debugger, connect it to Raspberry Pi as described in <a href=\"http:\/\/sysprogs.com\/VisualKernel\/tutorials\/raspberry\/jtagsetup\/\">this tutorial<\/a> and pick\u00a0your Raspberry Pi version in the &#8220;Debugged device&#8221; field. Then press &#8220;Test&#8221; to test the connection:<img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-165\" src=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/11-debug.png\" alt=\"\" width=\"863\" height=\"640\" srcset=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/11-debug.png 863w, https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/11-debug-300x222.png 300w, https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/11-debug-768x570.png 768w\" sizes=\"(max-width: 863px) 100vw, 863px\" \/><\/li>\n<li>VisualKernel will automatically\u00a0configure Raspberry Pi to use the JTAG pins described in the <a href=\"https:\/\/sysprogs.com\/VisualKernel\/tutorials\/raspberry\/jtagsetup\/\">JTAG tutorial<\/a> and test\u00a0that it can be debugged:<img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-167\" src=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/13-tested.png\" alt=\"\" width=\"1049\" height=\"579\" srcset=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/13-tested.png 1049w, https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/13-tested-300x166.png 300w, https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/13-tested-768x424.png 768w, https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/13-tested-1024x565.png 1024w\" sizes=\"(max-width: 1049px) 100vw, 1049px\" \/><\/li>\n<li>Once the test succeeds, press &#8220;Finish&#8221; to create the project. Note how the full path of the source file starts with <strong>vkrnl-ssh:\/\/<\/strong> indicating a file on a remote Linux machine that is directly accessed via SSH:<img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-166\" src=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/14-ssh.png\" alt=\"\" width=\"1041\" height=\"643\" srcset=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/14-ssh.png 1041w, https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/14-ssh-300x185.png 300w, https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/14-ssh-768x474.png 768w, https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/14-ssh-1024x632.png 1024w\" sizes=\"(max-width: 1041px) 100vw, 1041px\" \/><\/li>\n<li>Press Ctrl-Shift-B to build the project, set a breakpoint in the init function and start debugging by pressing F5. VisualKernel will load your module and the breakpoint will trigger:<img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-169\" src=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/15-stopped.png\" alt=\"\" width=\"1041\" height=\"643\" srcset=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/15-stopped.png 1041w, https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/15-stopped-300x185.png 300w, https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/15-stopped-768x474.png 768w, https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/15-stopped-1024x632.png 1024w\" sizes=\"(max-width: 1041px) 100vw, 1041px\" \/><\/li>\n<li>Use the Call Stack to navigate to the relevant parts of the Linux kernel:<img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-170\" src=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/16-stack.png\" alt=\"\" width=\"1041\" height=\"643\" srcset=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/16-stack.png 1041w, https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/16-stack-300x185.png 300w, https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/16-stack-768x474.png 768w, https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/16-stack-1024x632.png 1024w\" sizes=\"(max-width: 1041px) 100vw, 1041px\" \/><\/li>\n<li>Normally VisualKernel will hook the printk() function to display any\u00a0kernel-mode output in Visual Studio.\u00a0If\u00a0your JTAG frequency is relatively low, this could\u00a0noticeably slow down debugging. You can disable this option via VisualKernel Project Properties -&gt; Debug Settings -&gt; Display kernel output by hooking the\u00a0printk() function:<img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-174\" src=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/17-printk.png\" alt=\"\" width=\"1073\" height=\"872\" srcset=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/17-printk.png 1073w, https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/17-printk-300x244.png 300w, https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/17-printk-768x624.png 768w, https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/17-printk-1024x832.png 1024w\" sizes=\"(max-width: 1073px) 100vw, 1073px\" \/><\/li>\n<li>If you are using the Advanced Kernel Project Subsystem, you can easily\u00a0add the sources for the entire Raspberry Pi kernel to your solution. Simply locate it under the &#8220;Referenced Kernel&#8221; node in Solution Explorer and double-click on it to import it into the solution:<br \/>\n<img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-175\" src=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/18-modref.png\" alt=\"\" width=\"1041\" height=\"643\" srcset=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/18-modref.png 1041w, https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/18-modref-300x185.png 300w, https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/18-modref-768x474.png 768w, https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/18-modref-1024x632.png 1024w\" sizes=\"(max-width: 1041px) 100vw, 1041px\" \/><\/li>\n<li>VisualKernel will\u00a0create a special kernel release project and add it to the solution:<img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-173\" src=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/20-ref.png\" alt=\"\" width=\"1041\" height=\"711\" srcset=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/20-ref.png 1041w, https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/20-ref-300x205.png 300w, https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/20-ref-768x525.png 768w, https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/20-ref-1024x699.png 1024w, https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/20-ref-130x90.png 130w\" sizes=\"(max-width: 1041px) 100vw, 1041px\" \/><\/li>\n<li>Try it out by\u00a0selecting the call to printf() and clicking &#8220;Go to definition&#8221;. VisualKernel will locate the source for the printk() function in the kernel package and will open it in Visual Studio:<img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-172\" src=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/21-printf.png\" alt=\"\" width=\"1041\" height=\"711\" srcset=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/21-printf.png 1041w, https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/21-printf-300x205.png 300w, https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/21-printf-768x525.png 768w, https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/21-printf-1024x699.png 1024w, https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/21-printf-130x90.png 130w\" sizes=\"(max-width: 1041px) 100vw, 1041px\" \/><\/li>\n<li>You can also use the Code Map functionality to explore the relations between the functions. E.g. you can see how\u00a0the <strong>printk()<\/strong> function uses the\u00a0global <strong>printk_func<\/strong> variable that is set by <strong>printk_nmi_exit()<\/strong> to point to\u00a0<strong>vprintk_default()<\/strong>.<br \/>\n<img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-171\" src=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/22-codemap.png\" alt=\"\" width=\"997\" height=\"652\" srcset=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/22-codemap.png 997w, https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/22-codemap-300x196.png 300w, https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2018\/02\/22-codemap-768x502.png 768w\" sizes=\"(max-width: 997px) 100vw, 997px\" \/><\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>This tutorial shows how to debug a basic Linux kernel module on Raspberry PI over JTAG using Visual Studio. Before<\/p>\n","protected":false},"author":1,"featured_media":182,"comment_status":"closed","ping_status":"closed","sticky":true,"template":"","format":"standard","meta":{"footnotes":""},"categories":[35],"tags":[20,37],"_links":{"self":[{"href":"https:\/\/sysprogs.com\/tutorials\/wp-json\/wp\/v2\/posts\/134"}],"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=134"}],"version-history":[{"count":4,"href":"https:\/\/sysprogs.com\/tutorials\/wp-json\/wp\/v2\/posts\/134\/revisions"}],"predecessor-version":[{"id":566,"href":"https:\/\/sysprogs.com\/tutorials\/wp-json\/wp\/v2\/posts\/134\/revisions\/566"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/sysprogs.com\/tutorials\/wp-json\/wp\/v2\/media\/182"}],"wp:attachment":[{"href":"https:\/\/sysprogs.com\/tutorials\/wp-json\/wp\/v2\/media?parent=134"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sysprogs.com\/tutorials\/wp-json\/wp\/v2\/categories?post=134"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sysprogs.com\/tutorials\/wp-json\/wp\/v2\/tags?post=134"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}