您尚未登录。

楼主 #1 2021-02-23 11:36:02

qianfan
会员
注册时间: 2019-11-29
已发帖子: 38
积分: 106.5

How to compile Linux kernel with -O0 flag

参考链接:

https://lists.kernelnewbies.org/pipermail/kernelnewbies/2016-August/016686.html

搬运工:

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







On Sat, Aug 13, 2016 at 09:15:16PM +0300, Aleksander Alekseev wrote:
> 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







> 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?

> 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









On Sat, Aug 13, 2016 at 09:56:05PM +0300, Aleksander Alekseev wrote:
> > 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 :)

> 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






On Sat, Aug 13, 2016 at 09:56:05PM +0300, Aleksander Alekseev wrote:
> > 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.

>
> > 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
> Kernelnewbies at kernelnewbies.org
> https://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies








> 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







On Sun, Aug 14, 2016 at 07:11:01PM +0800, Hao Lee wrote:
> > 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.

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






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






Hi,

Le dimanche 14 août 2016 à 13:31 +0200, Greg KH a écrit :
> On Sun, Aug 14, 2016 at 07:11:01PM +0800, Hao Lee wrote:
> >
> > >
> > > 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?
> > >
[...]
> > 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

最近编辑记录 qianfan (2021-02-23 11:36:28)

离线

页脚

工信部备案:粤ICP备20025096号 Powered by FluxBB

感谢为中文互联网持续输出优质内容的各位老铁们。 QQ: 516333132, 微信(wechat): whycan_cn (哇酷网/挖坑网/填坑网) service@whycan.cn