Creating call-graphs to explore the Linux kernel with CodeViz

This article uses CodeViz, which downloads gcc 4.6.2 to compile the kernel while extracting code information. Other posts explore other techniques.

Making codeviz and gcc

The makefile calls the script “compilers/install_gcc-4.6.2.sh” to compile the patched gcc. There might be a few changes to that script in order to get everything working. Thanks to Stephan Friedl’s post, which gave the final fix for the gcc compilation.

Get a VM to compile gcc

This technique for setting up the system involves making many changes to /usr/lib and /usr/include, which could break the system in the future. I installed Ubuntu 14.04 desktop amd64 with 16GB of hard-disk on VirtualBox (also tested QEMU, but the VM was slow).

GCC prerequisites

While the official prerequisites are detailed but elaborate, following the official wiki is much simpler (thanks SO authors), and Friedl’s solution makes it actually work

sudo apt-get build-dep gcc-4.6-base
cd /usr/include
sudo ln -s x86_64-linux-gnu/* .
cd /usr/lib
sudo ln -s x86_64-linux-gnu/crt* .
Make sure you can fetch the sources

It uses ncftpget to fetch gcc source code, so if you don’t have it installed, now might be a good time (or change the script to another utility such as wget)

sudo apt-get install ncftp
 Modify configuration and make file in the script

We modify the configuration and build lines in “install_gcc-4.6.2.sh”. Some reasons are: (1) multilib support, (2) recent versions of makeinfo apparently break gcc document compilation.

# Configure and compile
../gcc-4.6.2/configure --prefix=$INSTALL_PATH --enable-shared --enable-languages=c,c++ --disable-multilib || exit
make bootstrap MAKEINFO="true" -j12

This adds –disable-multilib to configure (which I’m not sure is necessary, but many troubleshooting discussions had), and for make, disables MAKEINFO and runs multiple threads to compile.

 Compiling the Linux kernel with codeviz

I like to use the linux-stable git repo:

git clone git://git.kernel.org/pub/scm/linux/kernel/git/stable/linux-stable.git

 

Now, to compile using the codeviz-recommended line:

make O=~/linux-build CC=/usr/local/gccgraph/bin/gcc bzImage

 

Posted in Tech Blog.

7 Comments

  1. Hi,

    I followed your instructions to build patched GCC for codeviz but I’m getting the following error:
    …..
    /usr/bin/ld: .libs/alloc.o: relocation R_X86_64_32 against `.rodata.str1.8′ can not be used when making a shared object; recompile with -fPIC
    .libs/alloc.o: error adding symbols: Bad value
    collect2: ld returned 1 exit status
    make[5]: *** [libgomp.la] Error 1
    make[5]: Leaving directory `/home/amir/Downloads/codeviz-1.0.12/compilers/gcc-graph/objdir/x86_64-unknown-linux-gnu/libgomp’
    make[4]: *** [all-recursive] Error 1
    make[4]: Leaving directory `/home/amir/Downloads/codeviz-1.0.12/compilers/gcc-graph/objdir/x86_64-unknown-linux-gnu/libgomp’
    make[3]: *** [all] Error 2
    make[3]: Leaving directory `/home/amir/Downloads/codeviz-1.0.12/compilers/gcc-graph/objdir/x86_64-unknown-linux-gnu/libgomp’
    make[2]: *** [all-stage1-target-libgomp] Error 2
    make[2]: Leaving directory `/home/amir/Downloads/codeviz-1.0.12/compilers/gcc-graph/objdir’
    make[1]: *** [stage1-bubble] Error 2
    make[1]: Leaving directory `/home/amir/Downloads/codeviz-1.0.12/compilers/gcc-graph/objdir’
    make: *** [bootstrap] Error 2
    …..

    Do you have any idea how to solve this?
    I’m running Ubuntu 14.04 – x86_64

    Thanks

    • Hi Amir

      I also built codeviz-1.0.12 and don’t remember encountering this problem. Googling shows it is possible to add –disable-libgomp to gcc’s configure, but I’m not sure if Linux will compile with the resulting gcc.

      Did you start with a fresh install (e.g., in a VM)? Gcc compilation was very sensitive to which packages were previously installed on the system; I found working in a fresh VM was necessary for compilation.

      Jonathan

      • Hi Jonathan,

        Thank you for the reply.
        I added “–disable-libgomp” to the bash script but got another error on somewhere else.

        The answer to your second question is “Yes”, after getting that error on my working Linux machine, I tried to build it on a VM(virtualbox). But same error.

        It’s trying to link the object file which is not compiled with “-fPIC” which means not a shared file.

        Did you successfully generate the callgraph for Linux kernel functions? I’m trying to do that for the Linux system calls.

        Thanks,
        Amir

      • Could you please compress and upload the compiled version of the GCC somewhere if it’s convenient for you.

        Thanks

  2. OK, I made it work by running “make” on the codeviz top-level directory instead of executing the install script by itself.

    Thanks again for the time,
    Amir

    • Glad it’s working for you. I remember getting output, but reading the code manually proved more effective than the call graphs. If you have more insights please feel free to post them here.

    • Hi Amir

      Could you please compress and upload the compiled version of the GCC somewhere if it’s convenient for you.

      Thanks a lot

Leave a Reply

Your email address will not be published. Required fields are marked *