0

I follow one of many tutorials found on Google results to build and debug the Linux kernel with gcc and kgdb/gdb. And I end up by discovering that is all waste of time. Since I can't compile the kernel without gcc optimization -O0 neither -Og. There's no config option for removing optimization. And last but not leat, Linus said years ago that is against debugging.

Saying that kgdb must exist for some reason. I was wondering if there is a way to get rid of variables/arguments "optimized out" and let the debugger step through the code sequentially and not jumping from everywhere to everywhere?

3

1 Answer 1

-1

The Linux kernel source tree has Makefiles (many of them actually) which contain all the flags you're looking for, including -O2 which you can remove or replace with -O0.

Here's how you can change the compilation flags for all of them (which might be risky according to Stephen Kitt):

cd linux
find . -name Makefile -print0 | xargs -0 sed -i 's/-O2/-O0/g'
or
find . -name Makefile -print0 | xargs -0 sed -i 's/-O2/-Og/g'

Or change them only for the root Makefile (which is safer):

cd linux
sed -i 's/-O2/-O0/g' Makefile
or 
xargs -0 sed -i 's/-O2/-Og/g' Makefile

man gcc says -Og is good for debugging purposes.

6
  • 1
    Do you know if the kernel will build and run with a global -O0? I know individual components can be built like that, and flags can be set using CFLAGS_file.o in individual Makefiles, but it used to be the case that at least parts of the kernel needed -O1 or more. I haven’t checked recently, so I don’t know what the situation is nowadays... Aug 27, 2020 at 13:34
  • You're right, I've discovered -O2 in various Makefiles for other components. Looks like sed must be run recursively. Aug 27, 2020 at 13:37
  • 2
    My point is that there is a decent chance those flags in sub-Makefiles are there for a reason, and blindly changing them is liable to break the build in unobvious ways, for example not inlining setjmps which need to be. Aug 27, 2020 at 13:45
  • 1
    Well, the OP will learn it the hard way. Aug 27, 2020 at 13:46
  • 1
    What a lovely attitude... Aug 27, 2020 at 13:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.