simonkagstrom (simonkagstrom) wrote,

Writing linux kernel code in Eclipse

As I've stated in an earlier post, I've started using Eclipse (Ganymede) for writing C/C++ code. I work with Linux-based network switches at Ericsson and have been writing some kernel drivers for our hardware. At home, I've also done a bit of kernel work for the Openmoko FreeRunner (some improvements of the accelerometer driver).

Naturally, I'd like to use Eclipse for this work. The main advantage Eclipse gives over Emacs when working on large codebases is the source code parsing and indexing. This enables lookup of symbols, showing call hierarchies and "code assisting" for accessing structure members. Eclipse also grays sections of code which is ifdeffed out[*]. Very convenient. The problem is just to get the thing configured to understand the Linux kernel in the first place.

A particular thing about Linux is the configuration, which after menuconfig (or xconfig or ...) ends up as a set of defines in include/linux/autoconf.h. When a file in the kernel tree is compiled, this file is automatically included. Without trickery, Eclipse misses this and will gray out a lot of code which is part of the build and symbol lookup etc will generally behave bad. Without this configured in a good way, eclipse is basically on par with emacs while eating orders of magnitude more memory. Rats and darn.

What I'd like is basically this (Eclipse knows about CONFIG_PM!):


So how should this be configured? I'm not sure, but this is how I've done so far:
  • In your favourite shell, run make menuconfig and make and save a configuration for Linux. This will create include/linux/autoconf.h.

  • Start eclipse with eclipse -vmargs -Xmx650M. This will save you from out of memory errors.

  • Create new project as File/New/C Project, select Makefile project and --Other toolchain--

  • Under Projects/Properties, C/C++ general/Paths and symbols, select the Includes tab and click the Add... button. Check the Add to all languages checkbox and click the Workspace... button and add the include/ path so that Eclipse can find the Linux include files.

  • In the same dialogue, select the Symbols tab and click the Add... button. Check the Add to all languages checkbox and add __KERNEL__.

  • Under Projects/Properties, C/C++ general/Indexer, select No indexer.

  • Press OK

  • (Optional but typically useful) remove unused architectures from the build by right clicking on the directory in the project area and selecting Exclude from build...

  • We now need to add the symbols from the autoconf.h file. I use a script to do this, Execute as path-to-autoconf.h path-to-eclipse-dir
    This will update the .cproject file with defines for all symbols in autoconf.h (make a copy of .cproject first!)

  • Refresh the project: right click on the project in the project list and select refresh

  • Under Projects/Properties, C/C++ general/Indexer, select Fast C/C++ indexer.

  • Now watch in horror as Eclipse eclipses (pun intended) all your memory and slows down your computer to remind you of the 8-bit days. This is the time to have dinner or even go out and have a few beers.

Once you are back, providing your computer hasn't overheated, eclipse will have indexed your Linux kernel source tree.

There are still some open problems I'm facing, kind lazywebbers are encouraged to present solutions to them:
  • Eclipse adds trailing whitespace. I got this pointed out by Andy Green when working on the accelerometer patches. Luckily, this was the openmoko list, so it was only kind flames. LKML would not have been as forgiving.

  • Related to the first bullet: How do I get eclipse to show trailing whitespace without showing every non-text character?

  • Also related to the first bullet: I know there is an Eclipse option to remove trailing whitespace. The problem with that is that Eclipse happily cleans up files when you opens them. Your patches then become full of non-relevant changes, and LKML would scream at you if you send them in.

  • There has to be some better way of including autoconf.h in the indexing (GCC uses the -include option for this - Eclipse should do that as well). Does someone know?

Warning: The script below is badly written. Rerun it each time you change the kernel configuration.

[*] Ifdeffed might maybe become one of the first english words originating froma non-natural language context. Together with ifzeroed. Sorry, we've ifzeroed the subway today, No service. (Subversion archive)
Comments for this post were disabled by the author