how to read linux kernel source with pycparser

Michael Torrie torriem at gmail.com
Tue Oct 4 19:47:51 EDT 2016


On 10/04/2016 03:36 AM, meInvent bbird wrote:
> i expect to use pycparser to read linux kernel source
> and get a AST tree, 
> 
> but there are so many directory, 
> 
> how to read linux kernel source with pycparser?
> 
> how to customize pycparser to search what we want such as bug or fix 
> to make a linux patch for linux kernel source with python?

C projects with many .c files aren't meant to be compiled into one unit
(AST) usually.  The kernel is designed to be compiled into many
discrete, compiled object files which are then linked together after
compilation.  Each compilation unit would come from its own AST tree.
Furthermore, most C files can't be parsed by a compiler or parser at all
until the preprocessor has run over it first to handle the many
#define's, #if's, etc.  Fortunately you can run the preprocessor by
itself and output the bare C code.  On gcc I think if you pass -E it
will output the processed code.  Or use the cpp binary, which is
normally invoked by the compiler.

Another thing that will make this very difficult (and is related to the
preprocessor stuff) is that the Linux kernel's compilation can take many
different paths depending on how you configure the kernel. Some parts
may be skipped over entirely, other parts depend on which platform you
are configuring it for.

You could probably do this, and get lots of ASTs you can look at, but
you'll have to do some heavy-duty scripting and modification of
Makefiles to get it to happen in any sort of automatic way.  You make be
able to modify the Makefiles to have GCC itself dump the parse trees as
it creates them. I know GCC can do this. This is really the only
practical way I can see.



More information about the Python-list mailing list