问题描述
最近我开始开发linux设备驱动程序,
Recently I start to develop the linux device driver,
当我想使用内核代码进行调试,并在内核文件中添加一些printk
调试消息时,我遇到了一个问题.
I face a problem, when I want to debug with kernel code, and add some printk
debug message in the kernel file.
例如,最近我在include/linux/debug_locks.h
中的__debug_locks_off()
中添加了printk()
和dump_stack()
.
for example, recently I add some printk()
and dump_stack()
in the __debug_locks_off()
which resides in include/linux/debug_locks.h
.
然后我执行以下步骤,这非常耗时.
Then I do the following steps, which is very time consuming.
make clean
make bzImage
make modules
make modules_install
mkinitrfmfs -o /boot/initrd.img 3.12.6[my kernel version]
cp arch/x86/boot/bzImage /boot
update-grub
然后重新启动并选择我的新内核版本.
Then reboot and choose my new kernel version.
我不知道有没有多余的步骤?任何指导或帮助将不胜感激.
I don't know if there are some redundant steps?Any guideline or help will be appreciate.
推荐答案
这是我有关如何构建和运行自定义内核的说明.
Here is my notes on how to build and run the custom kernel.
Linus Torvalds的树是[1].
Linus Torvalds' tree is [1].
在[2]上将其标记为主线".
It's marked as "mainline" on [2].
要克隆它,请使用[1]中的信息:
To clone it use information from [1]:
$ git clone git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
现在转到linux/
目录并在master分支上签出(我们需要使用最新的变化作为发展的起点):
Now go to linux/
dir and checkout on master branch (we need to use most recentchanges as starting point for development):
$ cd linux
$ git checkout master
在实际开发之前,请不要忘记更新您的分支机构:
Before actual development don't forget to update your branch:
$ git pull --rebase
建筑物
我计算机上的内核版本:
Building
Kernel version on my machine:
$ uname -r
3.16.0-4-amd64
要从我的计算机上运行的系统获取配置,请执行以下操作:
To obtain config from the system running on my machine:
$ cp /boot/config-`uname -r` ./.config
要更新我的配置(使用默认答案),我使用了下一个命令:
To update my config (with default answers) I used next command:
$ make olddefconfig
要禁用(不构建)当前系统中未加载的模块,请执行以下操作:
To disable (to not build) modules which are not loaded in my current system:
$ make localmodconfig
要用默认答案回答所有问题,我只需单击直到完成(实际上只有两次).
To answer all questions with default answer, I just clicked until finish(just two times actually).
接下来我做了:
$ make menuconfig
并选择下一个配置选项:
and chose next config options:
CONFIG_LOCALVERSION_AUTO=y
CONFIG_LOCALVERSION="-joe"
设置ccache和构建环境:
Setting up ccache and build environment:
$ ccache -C
$ ccache -M 4G
$ export CC="ccache gcc"
构建内核(使用ccache
):
$ reset
$ make -j4
$ make -j4 modules
内置内核映像为:
arch/x86_64/boot/bzImage
安装
为您的内核安装模块:
Installing
Installing modules for your kernel:
$ sudo make modules_install
安装新内核:
$ sudo make install
已安装的模块位于/lib/modules/*-joe/kernel/
.
已安装的内核文件位于/boot/*joe*
:
Installed kernel files reside at /boot/*joe*
:
- config-*joe*
- initrd.img-*joe*
- System.map-*joe*
- vmlinuz-*joe*
update-grub
作为make install
脚本的一部分运行,因此无需运行它手动.
update-grub
was run as part of make install
script, so no need to run itmanually.
注意:modules_install
必须先运行 install
,因为使用模块填充initramfs映像需要install
规则.检查/boot/initrd.img-*joe*
文件的大小:必须大于等于15 MiB(如果较小,则可能是模块不在其中).
NOTE: modules_install
must be run before install
, because install
rule is needed for populating initramfs image with modules.Check size of /boot/initrd.img-*joe*
file: it must be >= 15 MiB(if it's smaller, chances are modules are not in there).
通常,您的自定义内核的版本应大于发行版内核的版本,因此默认情况下应运行自定义内核.如果否,请继续阅读.
Usually your custom kernel should have version bigger than your distro kernel,so custom kernel should be run by default. If no, read further.
重新启动,转到GRUB,选择下一个条目:
Reboot, go to GRUB, select next entries:
-> Advanced options for Debian GNU/Linux
-> Debian GNU/Linux, with Linux 4.0.0-rc7-joe-00061-g3259b12
默认使您的发行版内核加载
由于视频可能无法在您的自定义内核中运行(视频驱动程序必须为对此进行重建),您可能希望GRUB在默认情况下加载make distro内核.
Make your distro kernel load by default
Since video may not work in your custom kernel (video drivers must berebuild for this), you may want make distro kernel be loaded by default by GRUB.
为此,只需编辑/etc/default/grub
文件:
$ sudo vim /etc/default/grub
并更改行
GRUB_DEFAULT=0
到
GRUB_DEFAULT="1>3"
其中"1>3"
表示: -转到GRUB的第二行,输入 -并使用第四行启动.
where "1>3"
means: - go to 2nd line in GRUB, enter - and boot using 4th line.
此运行后:
$ sudo update-grub
注意:不要编辑/boot/grub/grub.cfg
文件,因为它是自动生成的,并且会 在每个update-grub
命令之后替换.
NOTE: do not edit /boot/grub/grub.cfg
file as it's auto-generated and will be replace after each update-grub
command.
如果您不再需要自定义内核,则可能要删除它.要删除已安装的内核,请执行下一步.
If you don't need your custom kernel anymore, you may want to remove it.To remove installed kernel, do next.
-
删除所有安装到/boot的文件:
Remove all files installed to /boot:
$ sudo rm -f *joe*
删除所有已安装的模块:
Remove all modules installed:
$ sudo rm -rf /lib/modules/*joe*
更新GRUB:
Update GRUB:
$ sudo update-grub
清理内置内核
如果您不需要进行增量构建,而想要进行干净构建(例如,您已结帐到另一个版本),则可能需要清理已构建的版本首先下载文件:
Cleaning up built kernel
If you don't need to do incremental build and want to do clean build instead(e.g. you made checkout to another version), you may want to clean your builtfiles first:
$ make -j4 distclean
链接
[1] https://git.kernel .org/cgit/linux/kernel/git/torvalds/linux.git/
[3] http://kernelnewbies.org/FAQ/KernelCompilation
这篇关于开发/调试linux内核的最有效,最优雅的方法是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!