Include kernel header files
I want to find the offset of a member of a structure within the kernel header file sched.h. (/usr/include/linux)
(i.e. I want to found out how many bytes the element 'tasks' is from the start of the task_struct which is declared in sched.h) Is there a way I can do that? My plan is to use the offsetof() function in C, but since sched.h is a kernel header file, I just inserting Code:
#include <linux/sched.h> Is there a way that I can do this? (i am using Suse 10.1 with kernel 2.6.16.13-4 |
First, the easy part. offsetof is not a function, but rather a preprocessor macro. You can use it a few ways yourself:
Now for the not-so-easy part, including a linux header file from userspace is useful mostly for getting the values of certain preprocessor macros. You can’t try to access anything in between “#ifdef __KERNEL__” and its corresponding “#endif” unless you are in kernel space. Generally, a distro will “sanitize” the kernel headers (i.e., removing those sections you can’t viably use) and put them in /usr/include/linux. As you can see in an unsanitized version of sched.h, struct task_struct is armored by #ifdef __KERNEL__/#endif (for various reasons). So you can’t do operations on a task_struct unless you are in kernelspace. As for the errors from including sched.h, that is odd. You might not have good, sanitized kernel headers, or you might be inadvertently using headers from your actual kernel source. If this is still puzzling you, can you give us some output from you attempt to compile such a file. |
Hmmm... that could pose few problems...
Well, in short my code is the following: Code:
#include <stddef.h> My problem is as you said, including sched.h to work on the task_struct. The errors that I get, when I compile this with gcc 4.1.0 are the following: Code:
In file included from /usr/include/linux/timex.h:61, I am using this version of sched.h So, what you said about the kernel space, is there a way I can do that in my program? Do i have to #define something? or create some sort of makefile? Or perhaps you can point me somewhere that explains to me how to do this? I would also need to get this working to use the sizeof() on the struct page, as mentioned here i imagine... Thanks |
Quote:
Look for code in np. |
Here’s (rather simple) sample code that does what you want:
offset.c Code:
#include <linux/kernel.h> Code:
ifndef KERNELDIR Code:
$ make |
Ah, the kernel modules... Yes, I should have known... what better way than to use Kernel space? :)
Though.... those caused me another branch of problems with the makefiles. But i think its a small one actually.... When i create a makefile, I always get the same error... Code:
make -C /lib/modules/2.6.16.13-4-default/build M=/home/fallen/code/offset Because the sched.h i need is in /usr/include/linux rather than /usr/src/linux-2.6.16.13-4-obj/i386/default. I actually have this problem with any kernel module i try... including those in the LKMPG tutorial... I have never been well acquanted with makefiles, only what i know from tinkering with them here and there, and that wasn't that much... thanks loads, your help is proving invaluble, seriously. I have been stuck on this problem for ages :) |
My first question is this: do you have the buildtree for the kernel you are running? Perhaps you might elucidate the situation by posting the output of the following:
Code:
$ ls -ld /lib/modules/`uname -r`/{build,source} Quote:
|
Also, I’m not too familiar with SUSE, so if anyone else knows how it manages kernels and their source/buildtrees, feel free to chime in.
|
|
I think you hit on the problem
Output from: Code:
$ ls -ld /lib/modules/`uname -r`/build the build directory only contains Module.symbers and there is a source shortcut which has an invalid link :rolleyes: That does not really bode well, does it? If i'm not mistaken, source should link to /usr/src/'uname -r' 'uname -r' is actually 2.6.16.13-4-default, however /usr/src contains a direcory called 2.6.16.13-4-obj also: Code:
$ sudo insmod offset.ko |
It seems you don’t have the correct package(s) for building kernel modules. Here is a guide for doing kernel related stuff in SUSE (check specifically the section entitled “Building additional (external) modules”).
Quote:
Quote:
The insmod utility is generally part of the module-init-utils package. It is in the same boat as modprobe and depmod (I think). So find out if you have module-init-utils installed and if you do, find out what files are contributed by the package (and where they’re located). Or perhaps the problem is your sudo is somehow trying to preserve $PATH. Try as root or with the full path (or add /sbin to your PATH). |
All times are GMT -5. The time now is 03:42 AM. |