Skip to main content

Getting confused when exploring Qemu source? gcc comes to rescue!

Quick summary first: use gcc -save-temps!

Ever dig into Qemu (qemu.org) source code? OK, I assume you ever did that at least once... may I ask, what's your first impression?

Here's mine: it's complex C code...and to make it more like a nightmare, it heavily uses c (gcc, to be precise) tricks almost everywhere. ifdef, "##", define....almost endless. IMHO, since Fabrice Bellard, its author, is somekind of C compiler wizard, he somehow pull out all of those tricks so easy from his mind. I know it should make the code kinda more readable, but for me, is not.

Take one for example: INDEX_op symbols. AFAIK, it has something to do with code generation, to be precise it's an index toward instruction op which will later be translated to target. Previously, I thought it was defined somewhere in header files, but turns out (after long hours of grep and cscope sessions) they were created by preprocessor (token concatenation, to be precise -- explanation here).

So, what is the recipe? I think I found it (thanks to this URL http://stackoverflow.com/questions/3812670/what-are-the-internal-processes-involved-for-a-c-compilation/3814007#3814007) , although not really ideal. During configuration session, use extra cflags like this:
./configure --extra-cflags="-save-temps"
Put additional parameters as needed. Then do "make". Now, if you do this in main Qemu source tree:
find -iname "*.[is]"
you'll find several files. Each of them are result of  preprocessing (.i) and assembling (.s). Yeap, "-save-temps" comes to rescue, folks! So there you go... open them one by one and hopefully you get better picture on how to code works.

regards,

Mulyadi Santosa

Comments

Bhaskar said…
use navigation features of a good IDE, like Netbeans. It even has Call Graph feature.
Unknown said…
it does not work now. I have gcc4.7. With --extra-cflags it is giving error,
Error: "cc" either does not exist of does not work
Mulyadi Santosa said…
@Junaid: are you sure you have gcc properly installed?

what is the output of :
gcc --version

@ Bhaskar: thanks for the info
Unknown said…
my gcc is 4.8.4
Unknown said…
@ santosa...my gcc version is 4.7

Popular posts from this blog

How to reduce CPU soft lock up in KVM guest

Ever saw something like below messages inside your KVM (Kernel Virtual Machine) guest's console? " BUG: soft lockup - CPU#0 stuck for 10s! [swapper:0] " I did and I find it a bit annoying. If you're inside graphical desktop like environment like KDE or GNOME, you  probably won't notice it directly. But you will likely suffer the same condition, the guest OS somehow become unresponsive for a few moment. In my case, it manifest into stalled CD/DVD access and "ruins" the console display. I had to press Enter few times before I could get back to normal shell prompt. Before I go further, FYI I use Fedora 9, kernel version 2.6.27.23-xx.x.xx.fc9.i686 on a Core Duo powered laptop. First, why the kernel shows such message? I use the default CentOS 5.3 kernel, so I check the related kernel config inside /boot directory and here is the related configuration item: CONFIG_DETECT_SOFTLOCKUP=y What does it do? Ingo Molnar, the writer of this lockup detection patch desc

gdb trick for printing array content

This is taken from Fedora planet, somekind of blog aggregator of the member of Fedora Community. This post discusses about the way we can print array values in gdb. Here is the link http://wagiaalla.com/2011/01/20/gdb-tricks-printing-arrays/, written by Sami Wagiaalla. ....And here is some excerpt from it:  int main(){    int *a;    int b[3] = {1,2,3};    a = b;    int *c[3] = {a, b, 0};    int **d = c;    return 0;  }    While debugging the above code if you do:  (gdb) print b  $4 = {1, 2, 3}    that works.   (gdb) print a  $5 = (int *) 0x7fffffffe0f0    that works too, but in order to print a as an array you must do:   (gdb) print (int []) *a  $7 = {1}    and when you specify the size it gets better:   (gdb) print (int [3]) *a  $8 = {1, 2, 3} regards, Mulyadi