什么是开发/调试 linux 内核最高效、最优雅的方式 [英] What is the most efficient and elegant way develop/debug linux kernel

查看:12
本文介绍了什么是开发/调试 linux 内核最高效、最优雅的方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近开始开发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 的 __debug_locks_off() 中添加了一些 printk()dump_stack()/linux/debug_locks.h.

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 recent changes 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

要使用默认答案回答所有问题,我只需单击 Enter 直到完成(实际上只有两次).

To answer all questions with default answer, I just clicked Enter 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 it manually.

注意:modules_install 必须在 before install 运行,因为 install 规则需要使用模块填充 initramfs 映像.检查 /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 your distro kernel load by default

Since video may not work in your custom kernel (video drivers must be rebuild for this), you may want make distro kernel be loaded by default by GRUB.

为此只需编辑 /etc/default/grub 文件:

For this just edit /etc/default/grub file:

$ sudo vim /etc/default/grub

换行

GRUB_DEFAULT=0

GRUB_DEFAULT="1>3"

其中 "1>3" 表示:- 转到 GRUB 中的第二行,输入- 并使用第 4 行启动.

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.

  1. 删除所有安装到/boot 的文件:

  1. 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 built files first:

    $ make -j4 distclean
    

    链接

    [1] https://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/

    [2] https://kernel.org/

    [3] http://kernelnewbies.org/FAQ/KernelCompilation

    这篇关于什么是开发/调试 linux 内核最高效、最优雅的方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆