What should I have to fix a bug?

  1. Bug. Well known particular bug.
  2. Version of "buged" kernel.
  3. Bit of luck.

Note: Having a repeatable bug is more then 50% of success.

Function printk().

printk is a very useful function similar to printf(). This function works everywhere and at any time (apart from early stage of booting the kernel when video isn't initialized). It uses log levels to tell the console the importance of each message.

Full list of levels:

  1. KERN_EMERG <-- the most important

  8. KERN_DEBUG <-- the least important

The console will print messages only with a level higher than console_loglevel. By default printk uses DEFAULT_MESSAGE_LOGLEVEL == KERN_WARNING (but this may be changed in the future).

printk() uses a cyclic buffer to manage the messages. Next klogd reads the messages (using /proc/kmsg) from the buffer and gives them to syslogd which writes them to /var/log/messages. (You can configure syslogd by editing /etc/syslog.conf).

Error oops.

An oops is report of a bug in the kernel. When an oops occurs the kernel will print what the registers contain and a "back trace". An oops does not mean the system has crashed, as the system can sometimes recover from the error. If the system can not recover from the error then the kernel will panic and stop running. By default the back trace will contain the addresses of the functions that were called. If you compile your kernel with CONFIG_KALLSYMS=y the oops will be decoded and will display the function names. In the 2.4 kernel you can use ksymoops file_with_oops.txt to see the names of the functions.

Additional compiling options.

These options are very useful when debugging kernel:






Causing errors and printing extra informations.

  1. Sometimes you will want to see oops informations about some bug. Use BUG() BUG_ON():


or BUG_ON(bad_thing);

  1. Sometimes you will want to see oops informations and then stop system. Use panic(): if(terrible_error)
    • panic("var = %ld \n", var);
  2. Sometimes you will want to see stack. Use dump_stack():


Magic SysRq Key.

If you set CONFIG_MAGIC_SYSRQ=y or typed 'echo 1 > /proc/sys/kernel/sysrq', you can use SysRq Key (on PPC or i386) 'Alt+PrintScreen'.

  1. SysRq+b Restart computer

  2. SysRq+e Send SIGTERM to all tasks (with out init !!!)

  3. SysRq+h Help

  4. SysRq+i Send SIGKILL to all tasks (with out init !!!)

  5. SysRq+k Kill all tasks ran from this console

  6. SysRq+l Send SIGTKILL to all tasks (with init !!!)

  7. SysRq+m Dump core and show it on console

  8. SysRq+o Halt system and shutdown it

  9. SysRq+p Print CPU registers on console

  10. SysRq+r Change keyboard from RAW to XLATE

  11. SysRq+s Save dirty buffers on HDD

  12. SysRq+t Show current task info on console

  13. SysRq+u Unmount all filesystems, and remount read only

Note that every user can use SysRq keys, and it can work improperly on an unstable system.

How to use non-exist debugger?

Linus doesn't want a debugger in the kernel because it can lead to fixing symptoms instead of actually understanding the code and fixing the real problem.

However, there are some non-official debuggers (Some require patching the kernel).

Note: use Additional compiling options to make gdb more functional.

  1. gdb (it is not a patch it is GNU debugger)

+ simple to use

-you must configure connection

  1. kdb

+only one computer needed +all functions of gdb implemented +simple to use

When everything fail.

No one likes bugs. So when you spend hours/days on bug fixing, you may write a short and descriptive email containing your all of the information you have found, and send it to LKML. Good luck with Bug Hunting.

KernelNewbies: KernelHacking-HOWTO/Debugging_Kernel (last edited 2007-01-16 20:55:42 by aczr30)