一、 需求
在Linux内核开发过程中不可避免地地会面对如何调试内核的问题。比如printk() 是调试内核代码时最常用的一种技术,但是这种调试方法需要对问题产生的点有个大概的感知,这样才能有的放矢地在合适的代码处添加打印信息。
但是很多情况下,我们对问题产生的原因毫无头绪,也就不可能在茫茫的代码中添加printk,这时候我们就依赖于GDB构建Linux内核调试环境。本文以Linux 4.1.15为例,介绍调试环境的搭建过程。
二、 原理
参考内核文档:
linux/Documentation/gdb-kernel-debugging.txt
GDB调试过程中需要加载符号信息,即lx-symbols命令,该命令是实现在内核自带python脚本:/linux/scripts/gdb/vmlinux-gdb.py,这也是”build gdb 7.2+ (recommended: 7.4+) with python supportenabled”的原因。
三、 在虚拟机上搭建调试环境
1. 检查虚拟机上已安装的GDB和python
[admin@local /home/elvis]
$whichgdb
/usr/bin/gdb
[admin@local /home/elvis]
$/usr/bin/gdb-v
GNUgdb (GDB) Red Hat Enterprise Linux (7.0.1-37.el5)
Copyright(C) 2009 Free Software Foundation, Inc.
LicenseGPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
Thisis free software: you are free to change and redistribute it.
Thereis NO WARRANTY, to the extent permitted by law. Type "show copying"
and"show warranty" for details.
ThisGDB was configured as "x86_64-redhat-linux-gnu".
Forbug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
[admin@local /home/elvis]
$which python
/usr/bin/python
[admin@<