Re: [PATCH] Built kernel without -O2 option

Petr Hluzín
2010-11-30T22:40:30Z

On 29 November 2010 22:07, Arnaud Lacombe <lacombar@gmail.com> wrote:
> Hi,
>
> On Sun, Nov 28, 2010 at 10:56 PM, Hui Zhu <teawater@gmail.com> wrote:
>> Hi,
>>
>> Now, there are a lot of ways to debug the Linux kernel with GDB, like
>> qemu, kgtp or kgdb and so on.
>> But the developer more like add a printk. It have a lot of reason, a big one is:
>> (gdb) p ret
>> $3 = <value optimized out>
>> And the code execution order is not right.
>>
>> This is becuase the Kernel is bult with gcc -O2.  Gcc will not
>> generate enough debug message with file with -O2.
>> So GDB cannot work very well with Linux kernel.
>>
>> So I make a patch that add a option in "Kernel hacking" called "Close
>> GCC optimization".  It will make kernel be built without -O2.
>>
> no, it does not, see below ..
>
>> I built and use it in i386 and x86_64.  I will try to make it OK in other arch.
>>
>> And I will put new patch in here and
>> http://code.google.com/p/kgtp/downloads/list
>>
>> Thanks,
>> Hui
>>
>> Signed-off-by: Hui Zhu <teawater@gmail.com>
>> ---
>> [...]
>> +ifdef CONFIG_CC_CLOSE_OPTIMIZATION
>> +CFLAGS_process_$(BITS).o       += -O2
>> +CFLAGS_entry_$(BITS).o         += -O2
>> +CFLAGS_traps.o                 += -O2
>> +CFLAGS_i387.o                  += -O2
>> +CFLAGS_xsave.o                 += -O2
>> +CFLAGS_hpet.o                  += -O2
>> +endif
>>
>> [...]
>>
>> --- a/lib/Kconfig.debug
>> +++ b/lib/Kconfig.debug
>> @@ -136,6 +136,14 @@ config DEBUG_SECTION_MISMATCH
>>          - Enable verbose reporting from modpost to help solving
>>            the section mismatches reported.
>>
>> +config CC_CLOSE_OPTIMIZATION
>> +       bool "Close GCC optimization"
>> +       default n
>> +       help
>> +         Enabling this option will let gcc build kernel without "-O2".
>> +
>> +         If unsure, say N.
>> +
> You are not consistent with yourself, you add an option saying "do not
> build the kernel with -O2" and yet, you add "-O2" flags for unknown
> reason all over the tree...

The patch removes -O2 compilation flag from all files (see the
fragment below) and adds the flag to the few files that actually need
the flag. Unfortunately the patch does not explain that and the noise
of adding the flag obscures the main thing.

>> +++ b/Makefile
>> @@ -540,8 +540,10 @@ all: vmlinux
>> ifdef CONFIG_CC_OPTIMIZE_FOR_SIZE
>> KBUILD_CFLAGS += -Os
>> else
>> +ifndef CONFIG_CC_CLOSE_OPTIMIZATION
>> KBUILD_CFLAGS += -O2
>> endif
>> +endif

Hui, The name of the config option is close to meaningless. I suggest:

- config CONFIG_CC_CLOSE_OPTIMIZATION
- bool "Close GCC optimization"
+ config CONFIG_CC_NO_OPTIMIZATION
+ bool "Compile with almost no optimization"
default n
help
- Enabling this option will let gcc build kernel without "-O2".
+ Enabling this option will let gcc build kernel with no
optimization (where possible).
+ This makes debugging friendlier.

--
Petr Hluzin