{"id":695,"date":"2023-04-04T18:23:53","date_gmt":"2023-04-05T01:23:53","guid":{"rendered":"https:\/\/sysprogs.com\/tutorials\/?p=695"},"modified":"2023-04-04T18:23:53","modified_gmt":"2023-04-05T01:23:53","slug":"using-live-trace-to-record-linux-kernel-debugging-sessions","status":"publish","type":"post","link":"https:\/\/sysprogs.com\/VisualKernel\/tutorials\/tracing\/","title":{"rendered":"Using Live Trace to Record Linux Kernel Debugging Sessions"},"content":{"rendered":"<p>This tutorial shows how to use the Live Trace feature of VisualKernel 4.0 to easily record the kernel debugging sessions without having to stop the target. We will create a basic character device, record the calls to its <strong>read()<\/strong> handler, and will also show how to set tracepoints in arbitrary kernel functions. Before you begin, install VisualKernel 4.0 or later.<\/p>\n<ol>\n<li>Start Visual Studio and open the Linux Kernel Module wizard: <a href=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2023\/04\/01-prj.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-696\" src=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2023\/04\/01-prj.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\/2023\/04\/02-trace.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-697\" src=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2023\/04\/02-trace.png\" alt=\"\" width=\"1014\" height=\"675\" \/><\/a><\/li>\n<li>Select &#8220;Create a new kernel module -&gt; Character device&#8221;: <a href=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2023\/04\/03-chardev.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-698\" src=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2023\/04\/03-chardev.png\" alt=\"\" width=\"886\" height=\"693\" \/><\/a><\/li>\n<li>Choose the Linux target where you would like to run the module. VisualKernel supports various virtual machine types, physical machines, as well as embedded boards (e.g. Raspberry Pi): <a href=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2023\/04\/04-vm.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-699\" src=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2023\/04\/04-vm.png\" alt=\"\" width=\"886\" height=\"693\" \/><\/a><\/li>\n<li>Proceed with the default settings for storing the source files: <a href=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2023\/04\/05-src.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-700\" src=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2023\/04\/05-src.png\" alt=\"\" width=\"886\" height=\"693\" \/><\/a><\/li>\n<li>Select the debug settings matching your target: <a href=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2023\/04\/06-debugvm.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-701\" src=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2023\/04\/06-debugvm.png\" alt=\"\" width=\"886\" height=\"693\" \/><\/a><\/li>\n<li>Press &#8220;Finish&#8221; to generate the project. Try building it with Ctrl-Shift-B. Depending on your Linux kernel version, you may get errors in the WARN_ON() macro. They are caused by a Linux kernel bug that triggers when trying to build modules without optimization: <a href=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2023\/04\/07-warn.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-702\" src=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2023\/04\/07-warn.png\" alt=\"\" width=\"1287\" height=\"885\" \/><\/a><\/li>\n<li>You can work around it by using <strong>_copy_to_user()<\/strong> instead of <strong>copy_to_user()<\/strong> and commenting out the WARN_ON() call in the <strong>BasicCharacterDevice.c<\/strong> file: <a href=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2023\/04\/08-fix.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-703\" src=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2023\/04\/08-fix.png\" alt=\"\" width=\"1287\" height=\"885\" \/><\/a>Another option would be to open the Makefile in Solution Explorer and change the optimization level from <strong>-O0<\/strong> to <strong>-Og<\/strong> (although, it would slightly reduce debugging accuracy).<\/li>\n<li>Start debugging your module by pressing F5. Then, right-click on the project node in Solution Explorer and select &#8220;<strong>Open a new SSH Window<\/strong>&#8220;:<a href=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2023\/04\/09-debug.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-704\" src=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2023\/04\/09-debug.png\" alt=\"\" width=\"1287\" height=\"886\" \/><\/a><\/li>\n<li>Once the SSH window opens, run the &#8220;<strong>dmesg<\/strong>&#8221; command to see the help on using the sample module, and then run the command suggested by it (e.g. <strong>sudo cat \/dev\/KernelTraceDemo<\/strong>): <a href=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2023\/04\/10-trace.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-705\" src=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2023\/04\/10-trace.png\" alt=\"\" width=\"905\" height=\"523\" \/><\/a><\/li>\n<li>Switch back to the Visual Studio window. One way to debug the module would be to set breakpoints in various places and step through the code. However, it would stop the entire target system in the debugger, possibly causing timeouts. Also, setting breakpoints in frequently hit functions could be impractical. To address these limitations, VisualKernel allows setting tracepoints in arbitrary code locations, that run without stopping the target. Click on the &#8220;show traceable functions&#8221; label near the <strong>CharDevDemo_read()<\/strong> function: <a href=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2023\/04\/11-showfunc.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-706\" src=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2023\/04\/11-showfunc.png\" alt=\"\" width=\"1287\" height=\"885\" \/><\/a><\/li>\n<li>VisualKernel will load the list of traceable functions in the kernel and the currently loaded module. Click on the tracepoint icon again and select to trace the <strong>pFile<\/strong>, <strong>size<\/strong>, <strong>pOffset<\/strong> variables:<a href=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2023\/04\/12-tracep.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-707\" src=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2023\/04\/12-tracep.png\" alt=\"\" width=\"1287\" height=\"885\" \/><\/a><\/li>\n<li>Click OK to set the <strong>tracepoint<\/strong>. VisualKernel will immediately create it without having to stop the target, and will begin recording events. You can view the events by clicking on the tracepoint label, or via <strong>Debug-&gt;Windows-&gt;Tracepoints<\/strong>: <a href=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2023\/04\/13-events.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-708\" src=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2023\/04\/13-events.png\" alt=\"\" width=\"1287\" height=\"885\" \/><\/a><\/li>\n<li>Switch the Tracepoints view to &#8220;Both&#8221; (Tracepoints + Events) and select any of the recorded events. The values of the recorded variables will be shown in the <strong>Trace Data<\/strong> window: <a href=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2023\/04\/14-data.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-709\" src=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2023\/04\/14-data.png\" alt=\"\" width=\"1287\" height=\"885\" \/><\/a><\/li>\n<li>Note that we recorded all fields of the <strong>struct file<\/strong>, but did not descend enough to capture the actual file name. You can change it by switching the <strong>Trace Data<\/strong> view to <strong>Properties<\/strong>:<a href=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2023\/04\/15-dentry.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-710\" src=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2023\/04\/15-dentry.png\" alt=\"\" width=\"1287\" height=\"885\" \/><\/a><\/li>\n<li>Then, locate <strong>pFile-&gt;f_path-&gt;dentry-&gt;d_name<\/strong>, check it and set the type to <strong>NULL-terminated<\/strong> string:<a href=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2023\/04\/16-name.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-711\" src=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2023\/04\/16-name.png\" alt=\"\" width=\"1287\" height=\"885\" \/><\/a><\/li>\n<li>Now every time the <strong>CharDeevDemo_read()<\/strong> function is traced, VisualKernel will record the name of the file as well:<a href=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2023\/04\/17-gotname.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-712\" src=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2023\/04\/17-gotname.png\" alt=\"\" width=\"1287\" height=\"885\" \/><\/a><\/li>\n<li>You can set the tracepoints at arbitrary code lines as well. Click on the tracepoint bar to the left of the breakpoint bar on the &#8220;*pOffset += todo&#8221; line in CharDevDemo_read(). Select to trace <strong>pBuffer, size and pOffset<\/strong>. Then, switch pBuffer view to &#8220;<strong>Dynamic array<\/strong>&#8220;: <a href=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2023\/04\/18-arr.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-713\" src=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2023\/04\/18-arr.png\" alt=\"\" width=\"1287\" height=\"885\" \/><\/a><\/li>\n<li>VisualKernel will show the parameters for tracing pBuffer below. Drag the <strong>size<\/strong> item into the &#8220;<strong>number of elements<\/strong>&#8221; field below so that VisualKernel will record [<strong>size<\/strong>] elements from the byte array pointed by <strong>pBuffer<\/strong>:<a href=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2023\/04\/19-size.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-714\" src=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2023\/04\/19-size.png\" alt=\"\" width=\"1287\" height=\"885\" \/><\/a><\/li>\n<li>Press OK to set the tracepoint. See how the recorded data now contains pBuffer as well. Click on the <strong>details<\/strong> button near <strong>pBuffer<\/strong>: <a href=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2023\/04\/20-data.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-715\" src=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2023\/04\/20-data.png\" alt=\"\" width=\"1287\" height=\"885\" \/><\/a><\/li>\n<li>VisualKernel will open the Memory window, showing the captured contents of pBuffer:<a href=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2023\/04\/21-gotdata.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-716\" src=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2023\/04\/21-gotdata.png\" alt=\"\" width=\"1287\" height=\"885\" \/><\/a>Note that none of the steps shown so far actually stops the target. The trace events are generated in the background, streamed into VisualKernel via network, and stored in a trace file, where you can review them at your convenience, while the target is still running uninterrupted.<\/li>\n<li>Now we will show how to efficiently navigate through large amounts of trace events. Click the &#8220;<strong>Views<\/strong>&#8221; button in the Trace Data window and select &#8220;<strong>Create a new table view<\/strong>&#8220;:<a href=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2023\/04\/22-table.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-717\" src=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2023\/04\/22-table.png\" alt=\"\" width=\"1287\" height=\"885\" \/><\/a><\/li>\n<li>When the new view window opens, select one of the <strong>CharDevDemo_read<\/strong> events and drag the <strong>pOffset-&gt;[0]<\/strong> item into the Columns view:<a href=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2023\/04\/23-view.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-718\" src=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2023\/04\/23-view.png\" alt=\"\" width=\"1287\" height=\"885\" \/><\/a><\/li>\n<li>VisualKernel will immediately show all events generated by that tracepoint, next to the value of <strong>pOffset[0]<\/strong>. Rename the view name to <strong>File reads<\/strong>:<a href=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2023\/04\/24-reads.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-719\" src=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2023\/04\/24-reads.png\" alt=\"\" width=\"1287\" height=\"885\" \/><\/a><\/li>\n<li>You can similarly rename the [0] column to something more meaningful, e.g. <strong>Offset<\/strong>: <a href=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2023\/04\/25-offset.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-720\" src=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2023\/04\/25-offset.png\" alt=\"\" width=\"1287\" height=\"885\" \/><\/a>Note that each column can contain multiple fields from different tracepoints. E.g. a column called &#8220;file descriptor&#8221; can contain the <strong>fd<\/strong> argument both <strong>sys_read()<\/strong> and <strong>sys_write()<\/strong>. In this case, each event produced by either of the 2 tracepoints will produce a row in the table view.<\/li>\n<li>Similarly, drag the <strong>size<\/strong> field <strong>underneath<\/strong> the <strong>Offset<\/strong> row (dragging it straight into <strong>Offset<\/strong> row will add it under the <strong>Offset<\/strong> column): <a href=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2023\/04\/26-size.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-721\" src=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2023\/04\/26-size.png\" alt=\"\" width=\"1287\" height=\"885\" \/><\/a><\/li>\n<li>The table view allows sorting the events by any of the columns. You can also switch to the <strong>Graph<\/strong> view to plot the recorded data. Double-clicking on the table row, or graph point, will immediately go to the corresponding event, highlighting its location in the source code, and letting you examine the captured data via the <strong>Trace Data<\/strong> window: <a href=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2023\/04\/27-graph.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-722\" src=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2023\/04\/27-graph.png\" alt=\"\" width=\"1287\" height=\"885\" \/><\/a><\/li>\n<li>You can also add tracepoints anywhere in the kernel by function name. Click on the <strong>New Tracepoint<\/strong> button in the <strong>Tracepoints<\/strong> window:<a href=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2023\/04\/28-newtp.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-723\" src=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2023\/04\/28-newtp.png\" alt=\"\" width=\"1287\" height=\"885\" \/><\/a><\/li>\n<li>Enter &#8220;do_sys_open&#8221; in the filter to quickly locate the <strong>do_sys_open()<\/strong> function. The filter box supports substrings, and regex-like &#8220;^&#8221; and &#8220;$&#8221; expressions. Once you enable the tracepoint, select to trace all function arguments:<a href=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2023\/04\/29-open.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-724\" src=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2023\/04\/29-open.png\" alt=\"\" width=\"1287\" height=\"885\" \/><\/a><\/li>\n<li>You can also trace arbitrary global variables. Enter &#8220;^jiffies&#8221; in the data filter and check the &#8220;<strong>jiffies<\/strong>&#8221; item:<a href=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2023\/04\/30-sysopen.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-725\" src=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2023\/04\/30-sysopen.png\" alt=\"\" width=\"1287\" height=\"885\" \/><\/a><\/li>\n<li>VisualKernel will now record every call to <strong>do_sys_open()<\/strong>, including the inlined ones, and will record the relevant variables (e.g. <strong>flags<\/strong> could be optimized out for some inlining sites):<a href=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2023\/04\/31-stop.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-726\" src=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2023\/04\/31-stop.png\" alt=\"\" width=\"1287\" height=\"885\" \/><\/a><\/li>\n<li>Stop the debug session by pressing Shift-F5. VisualKernel will save a trace report to a file, allowing you to replay it later. You can view the list of reports for the current project via <strong>View-&gt;Profiling\/Tracing Reports<\/strong>. For your own convenience, you can rename the reports or add arbitrary descriptions to them: <a href=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2023\/04\/32-report.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-727\" src=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2023\/04\/32-report.png\" alt=\"\" width=\"1287\" height=\"885\" \/><\/a><\/li>\n<li>Press the &#8220;Replay trace&#8221; button. VisualKernel will show the tracepoints and events recorded in the file. You can navigate between the events using the buttons in the event highlight, or via the <strong>Tracepoints<\/strong> window in Visual Studio: <a href=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2023\/04\/33-open.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-728\" src=\"https:\/\/sysprogs.com\/tutorials\/wp-content\/uploads\/2023\/04\/33-open.png\" alt=\"\" width=\"1287\" height=\"885\" \/><\/a><\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>This tutorial shows how to use the Live Trace feature of VisualKernel 4.0 to easily record the kernel debugging sessions<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[59],"tags":[62],"_links":{"self":[{"href":"https:\/\/sysprogs.com\/tutorials\/wp-json\/wp\/v2\/posts\/695"}],"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=695"}],"version-history":[{"count":1,"href":"https:\/\/sysprogs.com\/tutorials\/wp-json\/wp\/v2\/posts\/695\/revisions"}],"predecessor-version":[{"id":729,"href":"https:\/\/sysprogs.com\/tutorials\/wp-json\/wp\/v2\/posts\/695\/revisions\/729"}],"wp:attachment":[{"href":"https:\/\/sysprogs.com\/tutorials\/wp-json\/wp\/v2\/media?parent=695"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sysprogs.com\/tutorials\/wp-json\/wp\/v2\/categories?post=695"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sysprogs.com\/tutorials\/wp-json\/wp\/v2\/tags?post=695"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}