How to compile Linux kernel with -O0 flag

Hao Lee haolee.swjtu at
Sun Aug 14 07:11:01 EDT 2016

> Just out of curiosity - is there a technical reason why -O0 couldn't
> be used in Linux kernel? I don't know, spinlocks would not work in this
> case because it's how GCC was written or something. Or just nobody
> compiles and tests kernel like this so it most likely would not work?
>> But why do you want to use a debugger on the kernel?  That's not a
>> normal task unless you are bringing up a new hardware platform.
> It's just something I always do when I learn new things. Trying to
> figure out how to debug something in this new environment. No real task
> so far.

Hi, I also like figuring out what's happening in the OS underlying. So
I have some ideas about reducing optimization. Although you couldn't
turn off optimization completely, you can use both -O2 and other
options to reducing optimization as far as possible.
You can use -O2 -Q -v to find out which options are enabled when using
-O2. Then you can try -O2 -fno-defer-pop -fno-thread-jumps etc. to
disable some options. I once used this approach to debug kernel-2.4 in
bochs simulator. Unfortunately, this approach could not counteract the
effects of -O2 completely, but it's worth a try.

Good Luck,
Hao Lee

More information about the Kernelnewbies mailing list