Discussion:
How to compile Linux kernel with -O0 flag
Aleksander Alekseev
2016-08-13 18:15:16 UTC
Permalink
Hello

I'm currently exploring different ways of debugging Linux kernel. And I
noticed that for many variables gdb displays <optimized out>. Also from
experience of debugging ring3 code I know that sometimes debugger may
act crazy unless -O0 was used.

Apparently there is no corresponding option in `make menuconfig`. So
how do you solve this problem? Just `CFLAGS=-O0 make ...` or somehow
else? I wonder what is the "right" way of doing it because you know,
it's a kernel, it can accidentally break something unless you are
careful :)
--
Best regards,
Aleksander Alekseev
Greg KH
2016-08-13 18:35:34 UTC
Permalink
Post by Aleksander Alekseev
Hello
I'm currently exploring different ways of debugging Linux kernel. And I
noticed that for many variables gdb displays <optimized out>. Also from
experience of debugging ring3 code I know that sometimes debugger may
act crazy unless -O0 was used.
Apparently there is no corresponding option in `make menuconfig`. So
how do you solve this problem? Just `CFLAGS=-O0 make ...` or somehow
else? I wonder what is the "right" way of doing it because you know,
it's a kernel, it can accidentally break something unless you are
careful :)
The kernel will not run with -O0, sorry, just live with the build
optimization levels that is currently used and you should be fine.

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.

best of luck,

greg k-h
Aleksander Alekseev
2016-08-13 18:56:05 UTC
Permalink
Post by Greg KH
The kernel will not run with -O0, sorry, just live with the build
optimization levels that is currently used and you should be fine.
Oh, I see. Fortunately I'm not afraid of assembler :) Thanks.

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?
Post by Greg KH
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.
--
Best regards,
Aleksander Alekseev
Greg KH
2016-08-13 19:12:03 UTC
Permalink
Post by Aleksander Alekseev
Post by Greg KH
The kernel will not run with -O0, sorry, just live with the build
optimization levels that is currently used and you should be fine.
Oh, I see. Fortunately I'm not afraid of assembler :) Thanks.
Just out of curiosity - is there a technical reason why -O0 couldn't
be used in Linux kernel?
Yes, it doesn't work :)
Post by Aleksander Alekseev
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?
Try it, it will not work :)

good luck!

greg k-h
Mike Krinkin
2016-08-13 19:15:47 UTC
Permalink
Post by Aleksander Alekseev
Post by Greg KH
The kernel will not run with -O0, sorry, just live with the build
optimization levels that is currently used and you should be fine.
Oh, I see. Fortunately I'm not afraid of assembler :) Thanks.
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?
Just fixed Makefile and tried to build it with -O0, it doesn't even
compile, i got errors like this:

./include/linux/compiler-gcc.h:243:38: error: impossible constraint in ‘asm’
#define asm_volatile_goto(x...) do { asm goto(x); asm (""); } while (0)
^
./arch/x86/include/asm/cpufeature.h:146:3: note: in expansion of macro ‘asm_volatile_goto’
asm_volatile_goto("1: jmp 6f\n"

Probably gcc cannot figure out that an macro argument can be evaluated at
compile time with optimizations disabled.
Post by Aleksander Alekseev
Post by Greg KH
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.
--
Best regards,
Aleksander Alekseev
_______________________________________________
Kernelnewbies mailing list
https://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies
Hao Lee
2016-08-14 11:11:01 UTC
Permalink
Post by Aleksander Alekseev
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?
Post by Greg KH
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
Greg KH
2016-08-14 11:31:26 UTC
Permalink
Post by Hao Lee
Post by Aleksander Alekseev
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?
Post by Greg KH
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.
No, please don't do that. If you do, you will end up with a completly
unsuported and unknown system and no one will be able to help you out
with any sort of problem solving.

Just use the default build options, and all should be fine. We know
they work. Anything else is a total unknown, and not something that
anyone trying to learn about the kernel should be messing with.

thanks,

greg k-h
Hao Lee
2016-08-14 12:05:33 UTC
Permalink
Post by Greg KH
Post by Hao Lee
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.
No, please don't do that. If you do, you will end up with a completly
unsuported and unknown system and no one will be able to help you out
with any sort of problem solving.
Just use the default build options, and all should be fine. We know
they work. Anything else is a total unknown, and not something that
anyone trying to learn about the kernel should be messing with.
Thank you!!!
I didn't know this before. Thanks for your correction!

Thanks,
Hao Lee
Yann Droneaud
2016-08-14 14:20:31 UTC
Permalink
Hi,
Post by Hao Lee
Post by Aleksander Alekseev
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?
[...]
Post by Hao Lee
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.
No, please don't do that.  If you do, you will end up with a
completly unsuported and unknown system and no one will be able to
help you out with any sort of problem solving.
And what about -Og which is a dedicated optimization level since GCC
4.8

https://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html#index-Og-723
https://gcc.gnu.org/gcc-4.8/changes.html

Regards.

-- 
Yann Droneaud
OPTEYA

Loading...