How to locate the complete definition of a struct at the site lxr.free-electrons.com?

814 views Asked by At

The site lxr.free-electrons.com is very useful when I want to find the definition of a struct in linux kernel.

However, what's annoying is:

If a struct is declared in many other header files, then the found results will be a lot of incomplete declarations and one complete definition of the struct. It's hard to distinguish the definition and the declarations.

For instance, the search results of struct task_struct are:

task_struct

Defined as a struct type in:

drivers/mmc/card/queue.h, line 7 drivers/oprofile/cpu_buffer.h, line 21

drivers/net/wireless/cw1200/cw1200.h, line 34

include/uapi/linux/capability.h, line 18

include/asm-generic/syscall.h, line 22

......

......

That's very terrible to locate the complete definition of struct task_struct.

How to solve this problem?

3

There are 3 answers

0
manav m-n On

I find it much useful to download the linux source code in my system and browse using cscope command line tool. It together with ctags is very useful for code navigation.

1
eepp On

This seems to be a limitation of LXR, the engine behind lxr.free-electrons.com: it doesn't differentiate forward declarations (struct task_struct;) from full definitions.

My method of choice is always by looking at file names, since it's usually obvious where a given structure definition should be (e.g., in your case, it's related to scheduling, so sched.h in the file list should have it). However, I understand that one might be unfamiliar with the Linux kernel source code, and thus have no idea where to go.

The best way to find a structure definition in the Linux source tree is by searching for literally:

struct struct_name {

Since a strict coding style is followed by Linux contributors, the structure definitions should always take this form.

LXR has a freetext search to cope with its limitations. Unfortunately, it uses Google Search, which ignores most punctuations. GitHub seems to ignore them too, as well as Google Code.

My advice is to clone the mainline Linux Git repository:

git clone git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git

and then grep (or, better, ag) what you're looking for in the include directory:

grep -rin 'struct task_struct {' include

or, using ag:

ag 'struct task_struct {' include

You will see the files that define this structure (usually only one) in the results, and then you may go to the appropriate file on LXR and continue your research.

Here's a shell function to go to the appropriate LXR page using the first result:

lxr() {
    chromium "http://lxr.free-electrons.com/source/$(grep -rl "$1" include | head -1)"
}

or, using ag:

lxr() {
    chromium "http://lxr.free-electrons.com/source/$(ag -l "$1" include | head -1)"
}

Replace chromium with whatever your favorite browser command is. Use it like this:

lxr 'struct task_struct {'

Hope it helps.

0
alex On

Try this https://code-grep.com/view/project/54b083273b2082684a000008/linux-3.19-rc2?search=task_struct&type=definitions&page=0

it is actually showing up the line of the definition so we do not need to go back and forth to check.