Device Drivers Part 10

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

8/13/13

Write For Us Submit Tips

Device Drivers, Part 10: Kernel-Space Debuggers in Linux - LINUX For You
Subscribe to Print Edition Search

HOME

REVIEWS

HOW-TOS

CODING

INTERVIEWS

FEATURES

OVERVIEW

BLOGS

SERIES

IT ADMIN

Device Drivers, Part 10: Kernel-Space Debuggers in Linux


By Anil Kumar Pugalia on September 1, 2011 in Coding, Developers 0 Comments

Search for:

Search

This article, which is part of the series on Linux device drivers, talks about kernel-space debugging in Linux.
Shweta, back from hospital, was relaxing in the library, reading various books. Ever since she learned of the i o c t lway of debugging, she was impatient to find out more about debugging in kernel-space. She was curious about how and where to run the kernel-space debugger, if there was any. This was in contrast with application/user-space debugging, where we have the OS running underneath, and a shell or a GUI over it to run the debugger (like g d b , and the data display debugger, d d d ). Then she came across this interesting kernel-space debugging mechanism using k g d b , provided as part of the kernel itself, since kernel 2.6.26.

Get Connected RSS Feed Twitter

The debugger challenge in kernel-space


As we need some interface to be up to run a debugger to debug anything, a kernel debugger could be visualised in two possible ways: Put the debugger into the kernel itself, accessible via the usual console. For example, in the case of k d b , which was not official until kernel 2.6.35, one had to download source code (two sets of patches one architecture-dependent, one architecture-independent) from this FTP address and then patch these into the kernel source. However, since kernel 2.6.35, the majority of it is in the officially released kernel source. In either case, k d bsupport needs to be enabled in kernel source, with the kernel compiled, installed and booted with. The boot screen itself would give the k d bdebugging interface. Put a minimal debugging server into the kernel; a client would connect to it from a remote host or local user-space over some interface (say serial or Ethernet). This is k g d b , the kernels g d b server, to be used with g d bas its client. Since kernel 2.6.26, its serial interface is part of the official kernel release. However, if youre interested in a network interface, you still need to patch with one of the releases from the kgdb project page. In either case, you need to enable k g d bsupport in the kernel, recompile, install and boot the new kernel. Please note that in both the above cases, the complete kernel source for the kernel to be debugged is needed, unlike for building modules, where just headers are sufficient. Here is how to play around with k g d bover the serial interface.

LINUX For You on

Follow

Setting up the Linux kernel with kgdb


Here are the prerequisites: Either the kernel source package for the running kernel should be installed on your system, or a corresponding kernel source release should have been downloaded from kernel.org. First of all, the kernel to be debugged needs to have k g d benabled and built into it. To achieve that, the kernel source has to be configured with C O N F I G _ K G D B = y . Additionally, for k g d bover serial, C O N F I G _ K G D B _ S E R I A L _ C O N S O L E = yneeds to be configured. And C O N F I G _ D E B U G _ I N F Ois preferred for symbolic data to be built into the kernel, to make debugging with g d bmore

+2,489

www.linuxforu.com/2011/09/kernel-space-debuggers-in-linux/

1/4

8/13/13

Device Drivers, Part 10: Kernel-Space Debuggers in Linux - LINUX For You
Find us on Facebook

meaningful. C O N F I G _ F R A M E _ P O I N T E R = yenables frame pointers in the kernel, allowing g d bto construct more accurate stack back-traces. All these options are available under Kernel hacking in the menu obtained in the kernel source directory (preferably as root, or using s u d o ), by issuing the following command:
$m a k em r p r o p e r $m a k eo l d c o n f i g $m a k em e n u c o n f i g #T oc l e a nu pp r o p e r l y #C o n f i g u r et h ek e r n e ls a m ea st h ec u r r e n tr u n n i n go n e #S t a r tt h en c u r s e sb a s e dm e n uf o rf u r t h e rc o n f i g u r a t i o n

Open Source For You


Like 252,521 people like Open Source For You.

F acebook social plugin

Popular

Comments

Tag cloud

May 6, 2013 5 Comments Priyanka Sarkar

PHP Development: A Smart Career Move


June 20, 2013 3 Comments Priyanka Sarkar

What it Takes to be an Open Source Expert


June 20, 2013 2 Comments sophie-samuel

New and amazing features of Linux


May 6, 2013 1 Comments Deepti Sharma

A Simple guide to building your own Linux Kernel


Figure 1: Configuring kernel options for kgdb
May 6, 2013 1 Comments Prashant Phatak

Cyber Attacks Explained: Cryptographic Attacks

See the highlighted selections in Figure 1, for how and where these options would be: KGDB: kernel debugging with remote gdb > C O N F I G _ K G D B KGDB: use kgdb over the serial console > C O N F I G _ K G D B _ S E R I A L _ C O N S O L E Compile the kernel with debug info > C O N F I G _ D E B U G _ I N F O Compile the kernel with frame pointers > C O N F I G _ F R A M E _ P O I N T E R Once configuration is saved, build the kernel (run m a k e ), and then a m a k ei n s t a l lto install it, along with adding an entry for the installed kernel in the GRUB configuration file. Depending on the distribution, the GRUB configuration file may be / b o o t / g r u b / m e n u . l s t ,/ e t c / g r u b . c f g , or something similar. Once installed, the k g d b r e l a t e dkernel boot parameters need to be added to this new entry, as shown in the highlighted text in Figure 2.

Figure 2: GRUB configuration for kgdb

k g d b o cis for g d bconnecting over the console, and the basic format is k g d b o c = < s e r i a l _ d e v i c e > ,< b a u d r a t e >where: < s e r i a l _ d e v i c e >is the serial device file (port) on the system running the kernel to be

debugged
< b a u d r a t e >is the baud rate of this serial port k g d b w a i ttells the kernel to delay booting till a g d bclient connects to it; this parameter should

be given only after k g d b o c . With this, were ready to begin. Make a copy of the v m l i n u xkernel image for use on the g d b

www.linuxforu.com/2011/09/kernel-space-debuggers-in-linux/

2/4

8/13/13
g d bto connect over the serial port.

Device Drivers, Part 10: Kernel-Space Debuggers in Linux - LINUX For You

client system. Reboot, and at the GRUB menu, choose the new kernel, and then it will wait for

All the above snapshots are with kernel version 2.6.33.14. The same should work for any 2.6.3x release of the kernel source. Also, the snapshots for k g d bare captured over the serial device file / d e v / t t y S 0 , i.e., the first serial port.

Setting up gdb on another system


Following are the prerequisites: Serial ports of the system to be debugged, and the other system to run g d b , should be connected using a null modem (i.e., a cross-over serial) cable. The vmlinux kernel image built, with k g d benabled, needs to be copied from the system to be debugged, into the working directory on the system where g d bis going to be run. To get g d bto connect to the waiting kernel, launch g d bfrom the shell and run these commands:
( g d b )f i l ev m l i n u x ( g d b )s e tr e m o t ei n t e r r u p t s e q u e n c eC t r l C ( g d b )s e tr e m o t e b a u d1 1 5 2 0 0 ( g d b )t a r g e tr e m o t e/ d e v / t t y S 0 ( g d b )c o n t i n u e

In the above commands, v m l i n u xis the kernel image copied from the system to be debugged.

Debugging using gdb with kgdb


After this, it is all like debugging an application from g d b . One may stop execution using C t r l + C , add break points using b [reak], stop execution using s [tep] or n [ext] the usual g d bway. There are enough GDB tutorials available online, if you need them. In fact, if you are not comfortable with text-based GDB, use any of the standard GUI tools over g d b , like d d d , Eclipse, etc.

Summing up
By now, Shweta was excited about wanting to try out k g d b . Since she needed two systems to try it out, she went to the Linux device drivers lab. There, she set up the systems and ran g d bas described above.

Related Posts:
KGDB with VirtualBox: Debug a Live Kernel Kernel Tracing with ftrace, Part 1 Kernel Development & Debugging Using the Eclipse IDE Install Linux from USB on System without BIOS Support for USB Boot Device Drivers, Part 3: Kernel C Extras in a Linux Driver
Tags: boot screen, configuration file, debug, Debugging, Ethernet, GDB, GUI, kdb, kernel, kernel debugger, kernel source, kernel space, kgdb project, LFY September 2011, Linux, linux device drivers, Linux Device Drivers Series, Linux kernel, network interface, patches, serial interface, serial port, shell, source code

Article written by:


Anil Kumar Pugalia
The author is a freelance trainer in Linux internals, Linux device drivers, embedded Linux and related topics. Prior to this, he had worked at Intel and Nvidia. He has been exploring Linux since 1994. A gold medallist from the Indian Institute of Science, Linux and knowledge-sharing are two of his many passions. Connect with him: Website - Twitter - Facebook - Google+

Previous Post

Next Post

Securing Apache, Part 11: Logs, et al.

The Comprehensive LAMP Guide -- Part 3 (PHP)

www.linuxforu.com/2011/09/kernel-space-debuggers-in-linux/

3/4

8/13/13
Reviews How-Tos Coding

Device Drivers, Part 10: Kernel-Space Debuggers in Linux - LINUX For You
Interviews Features Overview Blogs

Search
Popular tags
Linux , ubuntu, Java, MySQL, Google, python, Fedora, Android, PHP, C, html, w eb applications , India, Microsoft, unix , Window s , Red Hat, Oracle, Security , Apache, xml, LFY April 2012, FOSS, GNOME, http, JavaScript, LFY June 2011, open source, RAM, operating systems

For You & Me Developers Sysadmins Open Gurus CXOs Columns

All published articles are released under Creative Commons Attribution-NonCommercial 3.0 Unported License, unless otherw ise noted. LINUX For You is pow ered by WordPress, w hich gladly sits on top of a CentOS-based LEMP stack.

www.linuxforu.com/2011/09/kernel-space-debuggers-in-linux/

4/4

You might also like