Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linux kernel live debugging, how it's done and what tools are used?

What are the most common and why not uncommon methods and tools used to do live debugging on the Linux kernel? I know that Linus for eg. is against this kind of debugging for the Linux Kernel or it least was and thus nothing much has been done in that sense in those years, but honestly a lot of time has passed since 2000 and i am interested if that mentality has changed regarding the Linux project and what current methods are used to do live debugging on the Linux kernel at the moment(either local or remote)?

References to walkthroughs and tutorials on mentioned techniques and tools are welcome.

like image 684
Shinnok Avatar asked Feb 09 '11 10:02

Shinnok


People also ask

What is debugging tools in Linux?

Strace. Strace is used for debugging and troubleshooting the execution of an executable on Linux environment. It displays the system calls used by the process, and the signals received by the process. Strace monitors the system calls and signals of a specific program.

How does a kernel debugger work?

When a new one is found, the kernel enters a break-in loop where it waits for additional commands to be received from the host kernel debugger. While the system on the target machine is halted, the break-in loop checks for any new commands sent by the host kernel debugger.


2 Answers

Another option is to use ICE/JTAG controller, and GDB. This 'hardware' solution is especially used with embedded systems,

but for instance Qemu offers similar features:

  • start qemu with a gdb 'remote' stub which listens on 'localhost:1234' : qemu -s ...,

  • then with GDB you open the kernel file vmlinux compiled with debug information (you can take a look a this mailing list thread where they discuss the unoptimization of the kernel).

  • connect GDB and Qemu: target remote localhost:1234

  • see your live kernel:

    (gdb) where #0  cpu_v7_do_idle () at arch/arm/mm/proc-v7.S:77 #1  0xc0029728 in arch_idle () atarm/mach-realview/include/mach/system.h:36 #2  default_idle () at arm/kernel/process.c:166 #3  0xc00298a8 in cpu_idle () at arch/arm/kernel/process.c:199 #4  0xc00089c0 in start_kernel () at init/main.c:713 

unfortunately, user-space debugging is not possible so far with GDB (no task list information, no MMU reprogramming to see different process contexts, ...), but if you stay in kernel-space, that's quite convenient.

  • info threads will give you the list and states of the different CPUs

EDIT:

You can get more details about the procedure in this PDF:

Debugging Linux systems using GDB and QEMU.

like image 185
Kevin Avatar answered Sep 25 '22 15:09

Kevin


While debugging Linux kernel we can utilize several tools, for example, debuggers (KDB, KGDB), dumping while crashed (LKCD), tracing toolkit (LTT, LTTV, LTTng), custom kernel instruments (dprobes, kprobes). In the following section I tried to summarized most of them, hope these will help.

LKCD (Linux Kernel Crash Dump) tool allows the Linux system to write the contents of its memory when a crash occurs. These logs can be further analyzed for the root cause of the crash. Resources regarding LKCD

  • http://www-01.ibm.com/support/knowledgecenter/linuxonibm/liaax/lkcd.pdf
  • https://www.novell.com/coolsolutions/feature/15284.html
  • https://www.novell.com/support/kb/doc.php?id=3044267

Oops when kernel detects a problem, it prints an Oops message. Such a message is generated by printk statements in the fault handler (arch/*/kernel/traps.c). A dedicated ring buffer in the kernel being used by the printk statements. Oops contains information like, the CPU where the Oops occurred on, contents of CPU registers, number of Oops, description, stack back trace and others. Resources regarding kernel Oops

  • https://www.kernel.org/doc/Documentation/oops-tracing.txt
  • http://madwifi-project.org/wiki/DevDocs/KernelOops
  • https://wiki.ubuntu.com/DebuggingKernelOops

Dynamic Probes is one of the popular debugging tool for Linux which developed by IBM. This tool allows the placement of a “probe” at almost any place in the system, in both user and kernel space. The probe consists of some code (written in a specialized, stack-oriented language) that is executed when control hits the given point. Resources regarding Dynamic Probe listed below

  • http://www-01.ibm.com/support/knowledgecenter/linuxonibm/liaax/dprobesltt.pdf
  • http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.107.6212&rep=rep1&type=pdf

Linux Trace Toolkit is a kernel patch and a set of related utilities that allow the tracing of events in the kernel. The trace includes timing information and can create a reasonably complete picture of what happened over a given period of time. Resources of LTT, LTT Viewer and LTT Next Generation

  • http://elinux.org/Linux_Trace_Toolkit
  • http://www.linuxjournal.com/article/3829
  • http://multivax.blogspot.com/2010/11/introduction-to-linux-tracing-toolkit.html

MEMWATCH is an open source memory error detection tool. It works by defining MEMWATCH in gcc statement and by adding a header file to our code. Through this we can track memory leaks and memory corruptions. Resources regarding MEMWATCH

  • http://www.linuxjournal.com/article/6059

ftrace is a good tracing framework for Linux kernel. ftrace traces internal operations of the kernel. This tool included in the Linux kernel in 2.6.27. With its various tracer plugins, ftrace can be targeted at different static tracepoints, such as scheduling events, interrupts, memory-mapped I/O, CPU power state transitions, and operations related to file systems and virtualization. Also, dynamic tracking of kernel function calls is available, optionally restrictable to a subset of functions by using globs, and with the possibility to generate call graphs and provide stack usage. You can find a good tutorial of ftrace at https://events.linuxfoundation.org/slides/2010/linuxcon_japan/linuxcon_jp2010_rostedt.pdf

ltrace is a debugging utility in Linux, used to display the calls a user space application makes to shared libraries. This tool can be used to trace any dynamic library function call. It intercepts and records the dynamic library calls which are called by the executed process and the signals which are received by that process. It can also intercept and print the system calls executed by the program.

  • http://www.ellexus.com/getting-started-with-ltrace-how-does-it-do-that/?doing_wp_cron=1425295977.1327838897705078125000
  • http://developerblog.redhat.com/2014/07/10/ltrace-for-rhel-6-and-7/

KDB is the in-kernel debugger of the Linux kernel. KDB follows simplistic shell-style interface. We can use it to inspect memory, registers, process lists, dmesg, and even set breakpoints to stop in a certain location. Through KDB we can set breakpoints and execute some basic kernel run control (Although KDB is not source level debugger). Several handy resources regarding KDB

  • http://www.drdobbs.com/open-source/linux-kernel-debugging/184406318
  • http://elinux.org/KDB
  • http://dev.man-online.org/man1/kdb/
  • https://www.kernel.org/pub/linux/kernel/people/jwessel/kdb/usingKDB.html

KGDB is intended to be used as a source level debugger for the Linux kernel. It is used along with gdb to debug a Linux kernel. Two machines are required for using kgdb. One of these machines is a development machine and the other is the target machine. The kernel to be debugged runs on the target machine. The expectation is that gdb can be used to "break in" to the kernel to inspect memory, variables and look through call stack information similar to the way an application developer would use gdb to debug an application. It is possible to place breakpoints in kernel code and perform some limited execution stepping. Several handy resources regarding KGDB

  • http://landley.net/kdocs/Documentation/DocBook/xhtml-nochunks/kgdb.html
like image 37
Md Mahbubur Rahman Avatar answered Sep 21 '22 15:09

Md Mahbubur Rahman