Linux – Debugging a kernel shut down

debugginglinuxstrace

I'm on Linux 4.2.0, the Ubuntu Wily HWE kernel for Ubuntu 14.04 (which is what I'm running).

There's a nasty bug on MacBook 11,4 and 11,5 models where the laptops won't actually ever shut off, they seem to reach the Power down kernel message and then just hang without ever shutting off. This bug is likely preventing suspend and resume as well. Interestingly enough, it doesn't manifest when reboot is called, only when trying to just halt the machine.

The details aren't so relevant, but what is relevant is how I could debug this. Is there a way to strace or debug the kernel while it's shutting down and observe the output somehow? I'm fairly certain that the power off logic is making a syscall that is just hanging forever and not returning for some strange reason. If I can figure out which syscall isn't working, I could then continue onward to figure out why it isn't working and what specifically about the hardware is causing the shutdown to fail.

Is there a standard practice for debugging a kernel shut down? Do I need special hardware? I'm fine writing patches for the kernel, but I don't even know where to begin looking for this issue unless I can find the syscall that isn't working.

Best Answer

A rough outline on what you may need to do:

  1. Download source and compile the kernel to enable debugging (config -> kernel hacking)
  2. Install kgdb patches
  3. Connect to target via a serial port from another machine. The target could be a different machine or could be an emulator like qemu or bochs.
  4. From the source code, identify the routine for shutdown
  5. Set a break point from gdb and step through until you see the hang.

You can find more info on these steps by searching for kernel debugging using kgdb.

This guide can help you set up your debugging environment.