【转】Writing linux kernel code in Eclipse
2012-08-25 14:38
405 查看
【转】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, autoconf-to-eclipse.py. Execute as
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.
autoconf-to-eclipse.py: https://mondragon.tek.bth.se/svn/main/world/ska/autoconf-to-eclipse (Subversion archive)
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, autoconf-to-eclipse.py. Execute as
autoconf-to-eclipse.py 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.
autoconf-to-eclipse.py: https://mondragon.tek.bth.se/svn/main/world/ska/autoconf-to-eclipse (Subversion archive)
相关文章推荐
- CPU frequency and voltage scaling code in the Linux(TM) kernel
- CPU frequency and voltage scaling code in the Linux(TM) kernel
- CPU frequency and voltage scaling code in the Linux(TM) kernel
- How to Install Nvidia Kernel Module Cuda and Pyrit in Kali Linux
- Problems occurred when invoking code from plug-in: "org.eclipse.jface".
- 線上 Android/Linux Kernel Source Code瀏覽 - Android/Linux Source Code Cross Reference
- (OK) using persistant storage in linux kernel - /sys/fs/pstore/console-ramoops
- Ctrl+Shift+F to format code in Eclipse
- BUG_ON & WARN_ON in Linux Kernel
- [转]Init Call Mechanism in the Linux Kernel(很犀利)
- Init Call Mechanism in the Linux Kernel
- Run Nutch In Eclipse on Linux and Windows nutch version 0.9
- Kconfig in Linux kernel
- view class source code with JAD plugin in Eclipse
- Writing, Building, and Running Chaincode in a Development Environment
- Solve Eclipse black pop-up code assist box in Ubuntu 10.4
- Linux Deepin配置JDK,Eclipse,AndroidStudio
- test video frame rate & bitrate code in linux
- Writing a Linux Keylogger in C
- PHP学习笔记 - 在Eclipse中使用XDebug调试代码 | Using XDebug debug code in eclipse