在Linux中编译内核代码

发布于 2024-10-18 12:00:09 字数 543 浏览 4 评论 0原文

好的,我正在阅读有关 Linux 内核开发的内容,其中有一些使用内核数据结构和内容的代码片段。假设我想尝试一下它们,例如有一个非常简单的片段:

#include <../../linux-2.6.37.1/include/linux/sched.h>
struct task_struct *task;
for_each_process(task) {
    printk("%s[%d]\n", task->comm, task->pid);
}

看起来很简单,是吗?那么现在我不可能建造这个东西了。我正在使用 NetBeans。 sched.h 是正确的文件,就好像可以按住 CTRL 键并单击它一样,就会转到正确的文件。

我是否需要以某种方式包含我的示例文件并从 Makefile 构建整个内核?我只是希望看到它能够构建并且可能会起作用。如果我需要构建整个内核,我将如何实际测试我的东西?

我一定做了一些非常愚蠢的事情,因为我对内核开发非常陌生。我有点失落了。

谢谢你们!

Okay, I'm reading about Linux kernel development and there are some code snippets using kernel's data structures and stuff. Let's say I'd like to experiment with them, e.g. there's a very simple snippet:

#include <../../linux-2.6.37.1/include/linux/sched.h>
struct task_struct *task;
for_each_process(task) {
    printk("%s[%d]\n", task->comm, task->pid);
}

Seems pretty simple, eh? Now then, I can't possibly build the thing. I am using NetBeans. The sched.h is the correct file as if one can CTRL+clicks on it, one is brought to the right file.

Do I need to include somehow my sample file and build the whole kernel from the Makefile? I just wished to see that it builds and possibly that it would work. If I need to build the whole kernel how would I actually test my stuff?

I must be making something really stupid as I am very new to kernel development. I am quite a bit lost.

Thanks guys!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

ㄖ落Θ余辉 2024-10-25 12:00:09

您不需要编译整个内核,但您至少必须创建一个内核模块,这更容易编译。您应该看一下教程,例如 this,甚至像这本书这样的完整书籍。

请记住,并非所有内核代码都可以移动到模块中 - 只有那些使用内核公共(导出)接口的代码才可以移动到模块中。内核核心部分(例如VM 或调度程序)固有的代码可能无法从内核的其余部分访问。

另请记住,不建议在您的开发计算机上尝试内核代码 -
轻微的错误很容易导致整个系统瘫痪。您应该考虑在单独的虚拟机中尝试内核代码,例如在 VirtualBox 中。

一个细节让事情变得更加困难:一般来说,您只能将模块插入到其构建的内核中。当且仅当内核相同时(即来自同一发行版的相同内核软件包版本),在主机系统上编译的模块才可以在测试虚拟机上使用。考虑到您需要升级主机发行版,我认为在测试系统上构建模块更简单。

由于您需要完整的 C 开发套件,因此您可能应该安装流行的 Linux 之一分布。它应该更稳定,您可以访问其用户社区。如果您想缩小其大小,您可以只安装基本系统,而不需要 X 服务器或图形应用程序。

BTW Netbeans 旨在开发用户空间应用程序。您可能可以将其改编为内核代码,但它永远不会像用户空间编程那样适合。事实上,没有哪个IDE是真正适合的。内核代码无法从用户空间运行(更不用说使用单独的虚拟机),这打破了 IDE 自动化的正常编辑 -> 编译 -> 运行 -> 调试工作流程循环。

大多数内核开发人员只是使用具有 C 语法突出显示功能的增强型编辑器,例如 VimEmacs。 Emacs 实际上是一个 IDE(以及更多),但是,正如我上面提到的,您无法轻松地使用基于 IDE 的工作流程进行内核代码开发。

You do not need to compile the whole kernel, but you have to at least create a kernel module, which is far easier to compile. You should have a look at a tutorial, such as this, or even a full blown book like this.

Keep in mind that not all kernel code can be moved to a module - just those that use the public (exported) interfaces of the kernel. Code that is intrinsic to the kernel core parts (e.g. the VM or the scheduler) is probably inaccessible from the rest of the kernel.

Also keep in mind that trying out kernel code on your development machine is not advised - a
slight mistake can easily bring the whole system down. You should look at trying out your kernel code in a separate virtual machine e.g. in VirtualBox.

A detail that makes thing harder: in general you can only insert a module in the kernel that it was built for. A module compiled on the host system can be used on the testing VM if and only if the kernel is identical, i.e. the same kernel package version from the same distribution. Considering that you will want to upgrade your host distribution, in my opinion it is just simpler to build the module on the testing system.

Since you need a full development suite for C, you should probably install one of the popular Linux distrbutions. It should be more stable and you can have access to its user community. If you want to keep its size down, you can just install the base system without an X server or graphical applications.

BTW Netbeans is designed to develop userspace applications. You can probably adapt it for kernel code, but it will never be as suited as it is for userspace programming. As a matter of fact, no IDE is really suitable. Kernel code cannot be run from userspace (let alone using a separate VM), which breaks down the normal edit->compile->run->debug workflow cycle that IDEs automate.

Most kernel developers just use a souped-up editor with syntax highlighting for C, such as Vim or Emacs. Emacs is actually an IDE (and so much more) but, as I mentioned above, you cannot easily use an IDE-based workflow for kernel code development.

孤君无依 2024-10-25 12:00:09

如果您不想构建整个内核,则可以构建可加载的内核模块 - 例如参见 http://www.linux-tutorial.info/modules.php?name=Howto&pagename=Module-HOWTO

You can build a loadable kernel module if you don't want to build the whole kernel - e.g. see http://www.linux-tutorial.info/modules.php?name=Howto&pagename=Module-HOWTO.

¢好甜 2024-10-25 12:00:09

您编写、编译和运行的所有代码都作为用户程序运行......好吧,用户程序在用户模式下运行。内核运行在内核模式。两种模式是分开的,不能直接看到对方。他们通过定义的接口进行通信。这些接口是 C 系统调用(与 C 库调用相对)。

为了能够访问 task_struct 结构,您的代码必须在内核模式下运行。最好的选择是编写一个内核模块,并将其加载到内核中。

All the code you write, compile and run as user programs run as ... well, user programs, in user mode. The kernel runs in kernel mode. Both modes are separated and cannot see each other directly. They communicate through defined interfaces. These interfaces are the C system calls (as opposed to the C library calls).

To be able to access the task_struct structures, your code has to be running in kernel mode. The best choice for this is to write a kernel module, and to load it in the kernel.

寒冷纷飞旳雪 2024-10-25 12:00:09

很少有内核代码可以以任何形式在内核之外运行。大多数内核代码与内核代码的其他部分非常“交织在一起”(用我多年前从同事那里学到的一个短语来描述过度耦合)。函数“知道”许多结构的结构定义,而不是它们正在处理的内容。典型的软件工程人员讨厌这样的代码:

    if (unlikely(inode_init_always(sb, inode))) {
            if (inode->i_sb->s_op->destroy_inode)
                    inode->i_sb->s_op->destroy_inode(inode);
            else
                    kmem_cache_free(inode_cachep, inode);
            return NULL;
    }

该例程必须知道如何通过三个结构函数指针的调用约定来销毁索引节点在链条的另一端。内核社区非常了解所有这些功能,并且很乐意在进行更改时修改整个内核结构中的成员名称,但是这种紧密耦合使得在用户空间中单独运行内核部分变得极其困难。 (相信我,有时我希望能够对在用户空间中运行的我的一小部分内核代码编写测试。)

如果您想尝试一下,建立一个虚拟系统并不难这些天使用 qemu+kvm 或 virtualbox 或 uml 运行来尝试对内核进行修改。在实时运行的系统上“玩”结构相当困难,但它比尝试在用户空间中编译部分内核要可行得多。

祝你好运。 :)

Very little kernel code can run outside the kernel in any form. Most kernel code is very 'intertwingled' (to use a phrase I learned from a coworker years ago to describe excessive coupling) with other portions of kernel code. Functions 'know' structure definitions for many many structures away from what they are working on. Typical software engineering people hate code like this:

    if (unlikely(inode_init_always(sb, inode))) {
            if (inode->i_sb->s_op->destroy_inode)
                    inode->i_sb->s_op->destroy_inode(inode);
            else
                    kmem_cache_free(inode_cachep, inode);
            return NULL;
    }

This routine has to know how to destroy inodes through three structures and the calling convention of a function pointer on the other end of the chain. The kernel community knows all these functions very well, and are quite happy to modify member names in structures all throughout the kernel when changes are made, but this sort of tight coupling makes running portions of the kernel in userspace on their own extremely difficult. (And believe me, sometimes I wish I could write tests on my small portions of kernel code that would run in userspace.)

If you want to play around, it's not too hard to get a virtual system up and running these days with qemu+kvm or virtualbox or uml to try making modifications to the kernel. It is pretty hard to just "play" with structures on a live running system, but it is much more feasible than trying to compile portions of the kernel in userspace.

Good luck. :)

迷爱 2024-10-25 12:00:09

您可能喜欢使用 systemtap 作为小段内核模块代码的包装器:

# stap -g -e 'probe begin { your_function() exit() }
%{
#include <linux/whatever.h>
%}
function your_function() %{
   ... insert safe c code here ...
%}'

它也可以自动交叉编译(如果您使用 stap --remote=VIRTMACHINE ...)。

You might enjoy using systemtap as a wrapper for small bits of kernel module code:

# stap -g -e 'probe begin { your_function() exit() }
%{
#include <linux/whatever.h>
%}
function your_function() %{
   ... insert safe c code here ...
%}'

It can automatically cross-compile too (if you use stap --remote=VIRTMACHINE ...).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文