Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

Difference between revisions of "HowTo use the CDT to navigate Linux kernel source"

(Bugfix: Like the kernel Makefile does, append '-iwithprefix include' to the compiler specs. This automates things and avoids adding certain GCC-provided headers manually.)
(26 intermediate revisions by 8 users not shown)
Line 3: Line 3:
 
Anyway, as you do these steps, I think you may understand how they assist the indexer to do a good job for the Linux kernel source.
 
Anyway, as you do these steps, I think you may understand how they assist the indexer to do a good job for the Linux kernel source.
  
''Disclaimer: these steps were developed for Eclipse 3.5.1 + CDT 6.0.0.''
+
''Disclaimer: these steps were last updated for Eclipse Oxygen 4.7.2 + CDT 9.4.0, and originally developed for Eclipse 3.5.1 + CDT 6.0.0''
  
 
#Download and install Eclipse plus the CDT.  
 
#Download and install Eclipse plus the CDT.  
#Configure and build your kernel. This can be done before or after downloading and installing Eclipse.  
+
#Configure and build your kernel to define CONFIG_* and generate autoconf.h. This can be done before or after downloading and installing Eclipse.
 +
#Ensure that you have the right kernel source (e.g. make sure you are on the right git branch).  If you check out another branch later, that's ok, but you will need to re-index the source, and that takes about 20 minutes.
 
#Start up Eclipse.  
 
#Start up Eclipse.  
 
#Click '''File'''->'''New'''->'''C Project'''  
 
#Click '''File'''->'''New'''->'''C Project'''  
Line 13: Line 14:
 
#In the '''Project type:''' pane, click the '''Makefile project''' and select '''Empty Project'''  
 
#In the '''Project type:''' pane, click the '''Makefile project''' and select '''Empty Project'''  
 
#On the right side, select '''Linux GCC'''  
 
#On the right side, select '''Linux GCC'''  
#Click '''Finish'''  
+
#Click '''Advanced settings...''' and a Properties dialog will pop up.
#Eclipse will start indexing the kernel source files, so double click on the little moving "activity" icon in the lower right part of the Eclipse window.  
+
# Note: At this point, latest Eclipse versions (e.g. Oxygen) will aggressively start indexing your project, which can make Eclipse painfully slow for the rest of the configuration steps below -- especially if the desire is to only index a small relevant part of the kernel. To mitigate that, temporarily disable indexing now by opening '''C/C++ General''' section, click on '''Indexer''', click on '''Enable project-specific settings''', then unmark the '''Enable indexer''' option.
#Click the square red stop button on the indexer.
+
#Open the '''C/C++ General''' selection on the left.
#Right click the top-level project in the '''Project Explorer''' pane on the left, and select '''Properties''' at the bottom.
+
#Click on '''Preprocessor Include Paths'''
#Click the '''Manage Configurations''' at the top right.  
+
#Select '''GNU C''' in the '''Languages''' list
#Select '''Linux GCC''' (if it isn't already) then click '''New...'''  
+
#Select '''CDT User Setting Entries''' in the '''Setting Entries''' list
#Give it a name like ''Linux config'' and a similar description, and click '''OK'''  
+
#Click on '''Add...'''. Choose '''Preprocessor Macros File''' from the top left dropdown, '''Project Path''' from the top right dropdown, and enter "{{Code|include/linux/kconfig.h}}" into the '''File''' text box. Note 1: For older kernels (e.g. 4.1.12, 4.2.7, or less), selecting "{{Code|include/generated/autoconf.h}}" works better. In newer kernels, selecting {{Code|kconfig.h}} is better as this file includes <generated/autoconf.h> and also makes sure that tests such as IS_ENABLED(option) are correctly expanded by the CDT scanner. Note 2: For kernels older than 2.6.33, the location of autoconf.h is {{Code|include/linux/autoconf.h}}
#Select your new configuration, and click '''Set Active''' and then '''OK'''  
+
#Also add any other macros files you are using.
#Select '''Resource''', and then in the '''Text file encoding''' section, select '''Other''' and '''ISO-8859-1''' in the box, then click '''Apply'''
+
#Click on '''Indexer'''
#Click on '''Indexer''' and then clear out the '''Files to index up-front''' box.  
+
#Checkmark the '''Enable project specific setttings''' box.
#Select '''Use active build configuration''' at the bottom, and click '''Apply'''  
+
#Uncheck '''Index source files not included in the build'''
#Click on '''Paths and Symbols'''  
+
#Click on '''Paths and Symbols''' on the left.
 
#Select the '''Includes''' tab and then select '''GNU C'''  
 
#Select the '''Includes''' tab and then select '''GNU C'''  
 
#Click '''Add...'''  
 
#Click '''Add...'''  
#Click '''Workspace...''' then select your kernel's {{Code|include}} directory
+
#Click '''Workspace...''' then select your kernel's {{Code|include}}, and {{Code|include/uapi}} directories
#Do another Add, Workspace and add {{Code|arch/}}''architecture''{{Code|/include}}, e.g., {{Code|arch/powerpc/include}}  
+
#Do another Add, Workspace and add both {{Code|arch/}}''architecture''{{Code|/include}}, and {{Code|arch/}}''architecture''{{Code|/include/uapi}} directories. e.g., {{Code|arch/powerpc/include}} and {{Code|arch/powerpc/include/uapi}} (The UAPI directories are due to the kernel's user/kernel header split covered [http://lwn.net/Articles/507794/ here] in-detail)
 
#Click the '''# Symbols''' tab  
 
#Click the '''# Symbols''' tab  
 
#Click '''Add...'''  
 
#Click '''Add...'''  
Line 34: Line 35:
 
#Set the value to {{Code|1}} and click '''OK'''  
 
#Set the value to {{Code|1}} and click '''OK'''  
 
#Click the '''Source Location''' tab  
 
#Click the '''Source Location''' tab  
#Click the twisty for your project.  
+
#Click the plus sign next to your project name.
 
#Select the '''Filter''' item and click '''Edit Filter...'''  
 
#Select the '''Filter''' item and click '''Edit Filter...'''  
 
#Click '''Add Multiple...''' and then select all of the {{Code|arch/*}} directories in your kernel source that will not be used (i.e. all the ones that are not for the architecture you are using)  
 
#Click '''Add Multiple...''' and then select all of the {{Code|arch/*}} directories in your kernel source that will not be used (i.e. all the ones that are not for the architecture you are using)  
 
#Click '''OK''' and '''OK''' again to dismiss that dialog.  
 
#Click '''OK''' and '''OK''' again to dismiss that dialog.  
#Click '''OK''' on the Properties dialog.  
+
#Under '''C/C++ General''', select '''Preprocessor Include Paths, Macros etc.'''
#Right click on the project again and select '''Index''' then select '''Rebuild'''  
+
#Click the '''Providers''' tab and select '''CDT GCC Built-in Compiler Settings'''
#It will take about 20 minutes or so to complete.
+
#Uncheck '''Use global provider shared between projects
 +
#Append {{Code|-nostdinc}} to the curretly-existing '''Command to get compiler specs'''. The kernel is a ''free-standing'' environment by ISO C99 definition. That is, it does not want to be polluted, and obviously cannot work with, the "host" header files and libraries.
 +
#Also append {{Code|-iwithprefix include}} to the '''Command to get compiler specs''' above. Rationale is, {{Code|-nostdinc}} above asked gcc to ''not'' search the standard system directories for header files. But the Linux Kernel depends on GCC-provided "freestanding environment" headers like ''stdarg.h'', ''stdbool.h'', etc., and which are typically hosted by GCC under ''/lib/gcc/<arch>/<version>/include''. Thus the append.
 +
#Check '''Allocate console in the Console View''' so you can see that this is working
 +
#Click '''OK''' on the Properties dialog.
 +
#Note: If you temporarily disabled indexing as earlier recommended. This is the right time to re-enable it. Under '''C/C++ General''', click on '''Indexer''', and mark the '''Enable indexer''' option.
 +
#Click '''Finish''' on the C Project dialog.
 +
#The Project will index automatically.
 +
#On a platter drive indexing will take upwards of 20 minutes to complete, on a SSD indexing will take about 5 minutes to complete.
 +
 
 +
Notes:
 +
 
 +
# Adding {{Code|include}} and {{Code|arch/}}''architecture''{{Code|/include}} only gets you a couple of the common include paths.  To fully index all of the kernel, you would have to add dozens of paths, unfortunately.  For this reason, I advise against using PTP's remote indexing capability for the linux kernel, because what happens is that it will report thousands of errors in locating header files, and the process of reporting those errors over a possibly long-latency link, will cause the indexing to take many hours.
 +
# If you change any of your CONFIG_* settings, in order for Eclipse to recognize those changes, you may need to do a "build" from within Eclipse.  Note, this does ''not'' mean to re-build the index; this means to build the kernel, by having Eclipse invoke make (this is normally bound to the Ctrl-B key in Eclipse).  Eclipse should automatically detect changes to include/generated/autoconf.h, reread the compilation #defines it uses, and reindex.
 +
# The background color of "Quick Context View" will be dark if the Ambiance theme in Ubuntu is selected.
 +
#For some people, Eclipse may fail to index the kernel with a out of memory error.  The fix seems to be to start eclipse with the arguments: eclipse  -vmargs -Xmx650M 
  
 
Corey Ashford [mailto:cjashfor@us.ibm.com cjashfor@us.ibm.com]
 
Corey Ashford [mailto:cjashfor@us.ibm.com cjashfor@us.ibm.com]
 +
 +
Updated by Adam Duskett [mailto:Aduskett@gmail.com Aduskett@gmail.com]
 +
 +
Continuous kernel-related updates (from ''v2.6.33'' up to ''v4.15'') by Ahmed S. Darwish [mailto:darwish.07@gmail.com darwish.07@gmail.com]

Revision as of 01:45, 27 February 2018

Here are some steps that I've found to get the CDT to work well with the Linux kernel source. If you exclude some of these steps, it may still work to a large degree, but some things may not work exactly right; for example it may find the wrong include file for a C file.

Anyway, as you do these steps, I think you may understand how they assist the indexer to do a good job for the Linux kernel source.

Disclaimer: these steps were last updated for Eclipse Oxygen 4.7.2 + CDT 9.4.0, and originally developed for Eclipse 3.5.1 + CDT 6.0.0

  1. Download and install Eclipse plus the CDT.
  2. Configure and build your kernel to define CONFIG_* and generate autoconf.h. This can be done before or after downloading and installing Eclipse.
  3. Ensure that you have the right kernel source (e.g. make sure you are on the right git branch). If you check out another branch later, that's ok, but you will need to re-index the source, and that takes about 20 minutes.
  4. Start up Eclipse.
  5. Click File->New->C Project
  6. Fill in a project name like my_kernel
  7. Uncheck the Use default location box and type in the root directory of your kernel into the Location box.
  8. In the Project type: pane, click the Makefile project and select Empty Project
  9. On the right side, select Linux GCC
  10. Click Advanced settings... and a Properties dialog will pop up.
  11. Note: At this point, latest Eclipse versions (e.g. Oxygen) will aggressively start indexing your project, which can make Eclipse painfully slow for the rest of the configuration steps below -- especially if the desire is to only index a small relevant part of the kernel. To mitigate that, temporarily disable indexing now by opening C/C++ General section, click on Indexer, click on Enable project-specific settings, then unmark the Enable indexer option.
  12. Open the C/C++ General selection on the left.
  13. Click on Preprocessor Include Paths
  14. Select GNU C in the Languages list
  15. Select CDT User Setting Entries in the Setting Entries list
  16. Click on Add.... Choose Preprocessor Macros File from the top left dropdown, Project Path from the top right dropdown, and enter "include/linux/kconfig.h" into the File text box. Note 1: For older kernels (e.g. 4.1.12, 4.2.7, or less), selecting "include/generated/autoconf.h" works better. In newer kernels, selecting kconfig.h is better as this file includes <generated/autoconf.h> and also makes sure that tests such as IS_ENABLED(option) are correctly expanded by the CDT scanner. Note 2: For kernels older than 2.6.33, the location of autoconf.h is include/linux/autoconf.h
  17. Also add any other macros files you are using.
  18. Click on Indexer
  19. Checkmark the Enable project specific setttings box.
  20. Uncheck Index source files not included in the build
  21. Click on Paths and Symbols on the left.
  22. Select the Includes tab and then select GNU C
  23. Click Add...
  24. Click Workspace... then select your kernel's include, and include/uapi directories
  25. Do another Add, Workspace and add both arch/architecture/include, and arch/architecture/include/uapi directories. e.g., arch/powerpc/include and arch/powerpc/include/uapi (The UAPI directories are due to the kernel's user/kernel header split covered here in-detail)
  26. Click the # Symbols tab
  27. Click Add...
  28. Set the name to __KERNEL__
  29. Set the value to 1 and click OK
  30. Click the Source Location tab
  31. Click the plus sign next to your project name.
  32. Select the Filter item and click Edit Filter...
  33. Click Add Multiple... and then select all of the arch/* directories in your kernel source that will not be used (i.e. all the ones that are not for the architecture you are using)
  34. Click OK and OK again to dismiss that dialog.
  35. Under C/C++ General, select Preprocessor Include Paths, Macros etc.
  36. Click the Providers tab and select CDT GCC Built-in Compiler Settings
  37. Uncheck Use global provider shared between projects
  38. Append -nostdinc to the curretly-existing Command to get compiler specs. The kernel is a free-standing environment by ISO C99 definition. That is, it does not want to be polluted, and obviously cannot work with, the "host" header files and libraries.
  39. Also append -iwithprefix include to the Command to get compiler specs above. Rationale is, -nostdinc above asked gcc to not search the standard system directories for header files. But the Linux Kernel depends on GCC-provided "freestanding environment" headers like stdarg.h, stdbool.h, etc., and which are typically hosted by GCC under /lib/gcc/<arch>/<version>/include. Thus the append.
  40. Check Allocate console in the Console View so you can see that this is working
  41. Click OK on the Properties dialog.
  42. Note: If you temporarily disabled indexing as earlier recommended. This is the right time to re-enable it. Under C/C++ General, click on Indexer, and mark the Enable indexer option.
  43. Click Finish on the C Project dialog.
  44. The Project will index automatically.
  45. On a platter drive indexing will take upwards of 20 minutes to complete, on a SSD indexing will take about 5 minutes to complete.

Notes:

  1. Adding include and arch/architecture/include only gets you a couple of the common include paths. To fully index all of the kernel, you would have to add dozens of paths, unfortunately. For this reason, I advise against using PTP's remote indexing capability for the linux kernel, because what happens is that it will report thousands of errors in locating header files, and the process of reporting those errors over a possibly long-latency link, will cause the indexing to take many hours.
  2. If you change any of your CONFIG_* settings, in order for Eclipse to recognize those changes, you may need to do a "build" from within Eclipse. Note, this does not mean to re-build the index; this means to build the kernel, by having Eclipse invoke make (this is normally bound to the Ctrl-B key in Eclipse). Eclipse should automatically detect changes to include/generated/autoconf.h, reread the compilation #defines it uses, and reindex.
  3. The background color of "Quick Context View" will be dark if the Ambiance theme in Ubuntu is selected.
  4. For some people, Eclipse may fail to index the kernel with a out of memory error. The fix seems to be to start eclipse with the arguments: eclipse -vmargs -Xmx650M

Corey Ashford cjashfor@us.ibm.com

Updated by Adam Duskett Aduskett@gmail.com

Continuous kernel-related updates (from v2.6.33 up to v4.15) by Ahmed S. Darwish darwish.07@gmail.com

Back to the top