{"id":307,"date":"2019-03-19T15:51:33","date_gmt":"2019-03-19T22:51:33","guid":{"rendered":"https:\/\/sysprogs.com\/tutorials\/?p=307"},"modified":"2019-07-16T16:43:39","modified_gmt":"2019-07-16T23:43:39","slug":"preparing-petalinux-kernels-for-debugging-with-visualkernel","status":"publish","type":"post","link":"https:\/\/sysprogs.com\/VisualKernel\/tutorials\/xilinx\/petalinux\/","title":{"rendered":"Preparing Petalinux Kernels for Debugging with VisualKernel"},"content":{"rendered":"<p>This tutorial shows how to prepare a kernel built with the <a href=\"https:\/\/www.xilinx.com\/products\/design-tools\/embedded-software\/petalinux-sdk.html\">Petalinux tools<\/a> for importing it into VisualKernel and creating kernel modules for it. We will start with a basic Petalinux project, modify its settings to support debugging and will show the parameters necessary to import the kernel into VisualKernel.<\/p>\n<p>In order to debug the Petalinux-based kernel from VisualKernel, we will need to perform the following modifications:<\/p>\n<ol>\n<li>Enable debug symbol generation so that the debugger can step through the kernel source and unwind stack traces.<\/li>\n<li>Disable the memory protection attributes on the code sections of the kernel and modules, so that VisualKernel can set software breakpoints there.<\/li>\n<li>Modify the kernel build scripts to dump the environment variables set by Petalinux so that VisualKernel can automatically configure IntelliSense.<\/li>\n<li>Finally build the kernel with the new settings<\/li>\n<\/ol>\n<p>In the rest of this tutorial we will provide detailed steps on accomplishing the tasks above.<\/p>\n<ol>\n<li>Ensure you have a Linux-based machine (or a VM) where you can run the Petalinux tools. Download and install the <a href=\"https:\/\/www.xilinx.com\/support\/download\/index.html\/content\/xilinx\/en\/downloadNav\/embedded-design-tools.html\">tools<\/a>\u00a0from the Xilinx website and copy your Petalinux project to the same machine.<\/li>\n<li>Connect to the build machine via SSH and run the following commands:\n<pre class=\"\">cd &lt;Petalinux tools directory&gt;\r\n. settings.sh\r\ncd &lt;Petalinux project directory&gt;\r\npetalinux-config -c kernel<\/pre>\n<p>This will start the process of downloading and configuring a Linux kernel:<br \/>\n<img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-308\" src=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2019\/03\/01-configure.png\" alt=\"\" width=\"1001\" height=\"701\" srcset=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2019\/03\/01-configure.png 1001w, https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2019\/03\/01-configure-300x210.png 300w, https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2019\/03\/01-configure-768x538.png 768w, https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2019\/03\/01-configure-130x90.png 130w\" sizes=\"(max-width: 1001px) 100vw, 1001px\" \/><\/li>\n<li>Once the necessary files are downloaded, you will see the kernel configuration menu:<img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-309\" src=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2019\/03\/02-menu.png\" alt=\"\" width=\"1001\" height=\"701\" srcset=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2019\/03\/02-menu.png 1001w, https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2019\/03\/02-menu-300x210.png 300w, https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2019\/03\/02-menu-768x538.png 768w, https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2019\/03\/02-menu-130x90.png 130w\" sizes=\"(max-width: 1001px) 100vw, 1001px\" \/><br \/>\nNote that as of March 2019, the Petalinux menu background appears distorted when accessed from Windows SSH clients. This does not affect the functionality.<\/li>\n<li>Enable the\u00a0<strong>Kernel hacking -&gt; Compile-time checks and compiler options -&gt; Compile the kernel with debug info<\/strong>\u00a0setting in the menu, but <strong>do not close it yet<\/strong>:<img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-310\" src=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2019\/03\/03-dbginfo.png\" alt=\"\" width=\"1001\" height=\"701\" srcset=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2019\/03\/03-dbginfo.png 1001w, https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2019\/03\/03-dbginfo-300x210.png 300w, https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2019\/03\/03-dbginfo-768x538.png 768w, https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2019\/03\/03-dbginfo-130x90.png 130w\" sizes=\"(max-width: 1001px) 100vw, 1001px\" \/><br \/>\nOnce you close the menu, the Petalinux tools will delete the Linux kernel source files, so if we want to modify them to disable memory protection and support environment dumping, we need to do it while the menu is open, obtain a patch file and integrate it into the Petalinux project.<\/li>\n<li>In another tab go to your Petalinux project directory (not the Petalinux tools directory) and run the following command:\n<pre class=\"\">find . | grep \/printk.c<\/pre>\n<p>This will help locate the directory where the Linux kernel sources have been deployed. Go to the parent directory, locate the Makefile in it and open it in a text editor:<br \/>\n<img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-311\" src=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2019\/03\/04-makefile.png\" alt=\"\" width=\"1210\" height=\"701\" srcset=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2019\/03\/04-makefile.png 1210w, https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2019\/03\/04-makefile-300x174.png 300w, https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2019\/03\/04-makefile-768x445.png 768w, https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2019\/03\/04-makefile-1024x593.png 1024w\" sizes=\"(max-width: 1210px) 100vw, 1210px\" \/><\/li>\n<li>Locate the recipe for the <strong>vmlinux<\/strong> target and add the following line to it:\n<pre class=\"\">&lt;TAB&gt;set &gt; kernel-env.txt<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-312\" src=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2019\/03\/05-env.png\" alt=\"\" width=\"1210\" height=\"701\" srcset=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2019\/03\/05-env.png 1210w, https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2019\/03\/05-env-300x174.png 300w, https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2019\/03\/05-env-768x445.png 768w, https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2019\/03\/05-env-1024x593.png 1024w\" sizes=\"(max-width: 1210px) 100vw, 1210px\" \/><\/li>\n<li>Now open the arch\/arm\/KConfig file and comment the lines setting the <strong>ARCH_HAS_STRICT_KERNEL_RWX<\/strong> and <strong>ARCH_HAS_STRICT_MODULE_RWX<\/strong> parameters:<img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-313\" src=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2019\/03\/06-comment.png\" alt=\"\" width=\"1210\" height=\"701\" srcset=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2019\/03\/06-comment.png 1210w, https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2019\/03\/06-comment-300x174.png 300w, https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2019\/03\/06-comment-768x445.png 768w, https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2019\/03\/06-comment-1024x593.png 1024w\" sizes=\"(max-width: 1210px) 100vw, 1210px\" \/><br \/>\nThis will allow software breakpoints (not limited to hardware breakpoint registers) inside the kernel and the modules, however would decrease the overall kernel security. Hence we only recommend to keep this option during debugging.<\/li>\n<li>Now we need to configure Petalinux to apply our changes to the kernel each time it is built. Run the following commands from the kernel source directory:\n<pre class=\"\">git commit -a -m \"Prepared for debugging with VisualKernel\"\r\ngit format-patch -1\r\nmkdir -p &lt;Petalinux Project Directory&gt;\/project-spec\/meta-user\/recipes-kernel\/linux\/linux-xlnx\r\ncp 0001-Prepared-for-debugging-with-VisualKernel.patch &lt;Petalinux Project Directory&gt;\/project-spec\/meta-user\/recipes-kernel\/linux\/linux-xlnx<\/pre>\n<p>This will create a patch file that can be automatically applied to the Linux kernel.<\/li>\n<li>Finally, open the <strong>&lt;Project Directory&gt;\/project-spec\/meta-user\/recipes-kernel\/linux\/linux-xlnx_%.bbappend<\/strong> file in a text editor (create it if it&#8217;s missing) and ensure it mentions the patch file as shown below:<img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-314\" src=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2019\/03\/07-registerpatch.png\" alt=\"\" width=\"1210\" height=\"701\" srcset=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2019\/03\/07-registerpatch.png 1210w, https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2019\/03\/07-registerpatch-300x174.png 300w, https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2019\/03\/07-registerpatch-768x445.png 768w, https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2019\/03\/07-registerpatch-1024x593.png 1024w\" sizes=\"(max-width: 1210px) 100vw, 1210px\" \/><\/li>\n<li>Go back to the tab where you ran <strong>petalinux-config<\/strong>, save the changes and close the configuration menu. Once the command exits, re-open the\u00a0<strong>linux-xlnx_%.bbappend<\/strong> file and locate the last .cfg file listed in the SRC_URI statement. This file will contain the configuration changes we made through the menu. Ensure it contains the &#8220;CONFIG_DEBUG_INFO=y&#8221; line as otherwise the debug symbols will not be generated:<img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-317\" src=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2019\/03\/11-dbg.png\" alt=\"\" width=\"1210\" height=\"553\" srcset=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2019\/03\/11-dbg.png 1210w, https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2019\/03\/11-dbg-300x137.png 300w, https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2019\/03\/11-dbg-768x351.png 768w, https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2019\/03\/11-dbg-1024x468.png 1024w\" sizes=\"(max-width: 1210px) 100vw, 1210px\" \/><\/li>\n<li>Now you can start building the Petalinux project. Go to the project directory (ensure you use the shell where you ran <strong>.\u00a0settings.sh<\/strong>) and run the following command:\n<pre class=\"\">petalinux-build<\/pre>\n<\/li>\n<li>Wait for the build to complete. You can ignore the &#8220;missing lsb_release&#8221; and &#8220;failed to copy built images to tftp dir&#8221; errors:<img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-316\" src=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2019\/03\/09-built.png\" alt=\"\" width=\"1210\" height=\"701\" srcset=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2019\/03\/09-built.png 1210w, https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2019\/03\/09-built-300x174.png 300w, https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2019\/03\/09-built-768x445.png 768w, https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2019\/03\/09-built-1024x593.png 1024w\" sizes=\"(max-width: 1210px) 100vw, 1210px\" \/><\/li>\n<li>Now we will need to locate the <strong>kernel-env.txt<\/strong> file produced by our modified build scripts. Run the &#8220;find . | grep kernel-env.txt&#8221; command in the project directory and take a note of the output. Then run the following command from the project directory:\n<pre class=\"\">sed -e 's\/^\\(AS\\|AR\\|NM\\|CPP\\|PATH\\|LD\\|CCLD\\|STRIP\\|CXX\\|OBJCOPY\\|ARCH\\|\r\nRANLIB\\|CROSS_COMPILE\\|CC\\|READELF\\|OBJDUMP\\|TARGET_CFLAGS\\|PETALINUX_.*\r\n\\|KERNEL_.*\\)=\\(.*\\)\/export \\1=\\2\/;t;d' &lt; [full path to kernel-env.txt] &gt; kernel.env<\/pre>\n<p>This will convert the dump from the &#8216;set&#8217; command into a shell script that can be loaded using the &#8216;.&#8217; command.<\/li>\n<li>Locate the full path to the GDB executable provided by the Xilinx tools by running &#8220;which arm-linux-gnueabihf-gdb&#8221;:<img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-324\" src=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2019\/03\/gdb.png\" alt=\"\" width=\"1210\" height=\"379\" srcset=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2019\/03\/gdb.png 1210w, https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2019\/03\/gdb-300x94.png 300w, https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2019\/03\/gdb-768x241.png 768w, https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2019\/03\/gdb-1024x321.png 1024w\" sizes=\"(max-width: 1210px) 100vw, 1210px\" \/><\/li>\n<li>Finally, open the <strong>vmlinux<\/strong> file from the directory of the <strong>kernel-env.txt<\/strong> file with <strong>gdb<\/strong> and ensure it contains symbols by running the &#8220;info line printk&#8221; command:<img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-319\" src=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2019\/03\/12-gdb.png\" alt=\"\" width=\"1210\" height=\"616\" srcset=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2019\/03\/12-gdb.png 1210w, https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2019\/03\/12-gdb-300x153.png 300w, https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2019\/03\/12-gdb-768x391.png 768w, https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2019\/03\/12-gdb-1024x521.png 1024w\" sizes=\"(max-width: 1210px) 100vw, 1210px\" \/><\/li>\n<li>Ensure you can boot into the new kernel. Typically, copying the <strong>image.ub<\/strong> file (the exact name is different for different boards) to the SD card should be sufficient. You can check the version of the kernel running on the device by running the &#8220;uname -a&#8221; command on it (not on the build machine):<a href=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2019\/03\/2booted2.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-373\" src=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2019\/03\/2booted2.png\" alt=\"\" width=\"687\" height=\"475\" srcset=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2019\/03\/2booted2.png 687w, https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2019\/03\/2booted2-300x207.png 300w, https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2019\/03\/2booted2-392x272.png 392w, https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2019\/03\/2booted2-130x90.png 130w\" sizes=\"(max-width: 687px) 100vw, 687px\" \/><\/a><\/li>\n<li>Now we are ready to import the kernel into VisualKernel. Take a note of the following paths from your setup as you will need them to configure VisualKernel later:<br \/>\n<table>\n<tbody>\n<tr>\n<th>Path<\/th>\n<th>Sample value<\/th>\n<\/tr>\n<tr>\n<td>Petalinux project directory<\/td>\n<td>\/opt\/pkg\/petalinux\/project<\/td>\n<\/tr>\n<tr>\n<td>Kernel environment file<\/td>\n<td>\/opt\/pkg\/petalinux\/project\/kernel.env<\/td>\n<\/tr>\n<tr>\n<td>Kernel image with symbols<\/td>\n<td>\/opt\/pkg\/petalinux\/project\/build\/tmp\/work\/plnx_zynq7-xilinx-linux-gnueabi\/linux-xlnx\/4.14-xilinx-v2018.3+gitAUTOINC+eeab73d120-r0\/linux-plnx_zynq7-standard-build\/vmlinux<\/td>\n<\/tr>\n<tr>\n<td>Kernel source directory<\/td>\n<td>\/opt\/pkg\/petalinux\/project\/build\/tmp\/work\/plnx_zynq7-xilinx-linux-gnueabi\/linux-xlnx\/4.14-xilinx-v2018.3+gitAUTOINC+eeab73d120-r0\/linux-plnx_zynq7-standard-build\/source<\/td>\n<\/tr>\n<tr>\n<td>GDB executable<\/td>\n<td>\/opt\/pkg\/petalinux\/2018.3\/tools\/linux-i386\/gcc-arm-linux-gnueabi\/bin\/arm-linux-gnueabihf-gdb<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/li>\n<\/ol>\n<p>Follow <a href=\"https:\/\/sysprogs.com\/VisualKernel\/tutorials\/xilinx\/zynq\/module\/\">this tutorial<\/a> to create a basic kernel module for the Petalinux-based kernel using VisualKernel.<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This tutorial shows how to prepare a kernel built with the Petalinux tools for importing it into VisualKernel and creating<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[51],"tags":[50,49,48],"_links":{"self":[{"href":"https:\/\/sysprogs.com\/tutorials\/wp-json\/wp\/v2\/posts\/307"}],"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=307"}],"version-history":[{"count":7,"href":"https:\/\/sysprogs.com\/tutorials\/wp-json\/wp\/v2\/posts\/307\/revisions"}],"predecessor-version":[{"id":395,"href":"https:\/\/sysprogs.com\/tutorials\/wp-json\/wp\/v2\/posts\/307\/revisions\/395"}],"wp:attachment":[{"href":"https:\/\/sysprogs.com\/tutorials\/wp-json\/wp\/v2\/media?parent=307"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sysprogs.com\/tutorials\/wp-json\/wp\/v2\/categories?post=307"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sysprogs.com\/tutorials\/wp-json\/wp\/v2\/tags?post=307"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}