Skip to contentSkip to conversation

How to compile Linux kernel with -O0 flag

DISCUSSION Aleksander Alekseev 5 participants 9 messages Aug 13, 2016 – Aug 14, 2016

lore
Share & tools
lore
#0 lore raw

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 :)

Signature · 2 lines
Best regards,
Aleksander Alekseev
#1 lore raw
Quoted 7 lines · 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.

Quoted 5 lines

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

#2 lore raw
Quoted 2 lines

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?

Quoted 2 lines

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.

Signature · 2 lines
Best regards,
Aleksander Alekseev
#3 lore raw
Quoted 7 lines · Aleksander Alekseev
Quoted 2 lines

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 :)

Quoted 3 lines

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

#4 lore raw
In reply to Aleksander Alekseev
Quoted 9 lines · Aleksander Alekseev
Quoted 2 lines

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.

Quoted 16 lines
Quoted 3 lines

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.

Signature · 7 lines
Best regards,
Aleksander Alekseev

_______________________________________________
Kernelnewbies mailing list
Kernelnewbies at kernelnewbies.org
https://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies
#5 lore raw
In reply to Aleksander Alekseev
Quoted 11 lines

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?

Quoted 2 lines

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

#6 lore raw
Quoted 12 lines · Hao Lee
Quoted 11 lines

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?

Quoted 2 lines

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.

Quoted 9 lines

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

#7 lore raw
Quoted 10 lines
Quoted 9 lines

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.

Quoted 7 lines

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

#8 lore raw
In reply to Greg KH

Hi,

Le dimanche 14 ao?t 2016 ? 13:31 +0200, Greg KH a ?crit?:

Quoted 9 lines · Hao Lee
Quoted 8 lines
Quoted 8 lines

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?

[...]

Quoted 10 lines
Quoted 9 lines

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.

Quoted 4 lines

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