Search code examples
debuggingkernel-modeusermode

Is There Ever an Advantage to User Mode Debug over Kernel Mode Debug?


From what I understand, on a high level, user mode debugging provides you with access to the private virtual address for a process. A debug session is limited to that process and it cannot overwrite or tamper w/ other process' virtual address space/data.

Kernel mode debug, I understand, provides access to other drivers and kernel processes that need full access to multiple resources, in addition to the original process address space.

From this, I get to thinking that kernel mode debugging seems more robust than user mode debugging. This raises the question for me: is there a time, when both options of debug mode are available, that it makes sense to choose user mode over a more robust kernel mode?

I'm still fairly new to the concept, so perhaps I am thinking of the two modes incorrectly. I'd appreciate any insight there, as well, to better understand anything I may be missing. I just seem to notice that a lot of people seem to try to avoid kernel debugging. I'm not entirely sure why, as it seems more robust.


Solution

  • The following is mainly from a Windows background, but I guess it should be fine for Linux too. The concepts are not so different.

    Some inline answers first

    From what I understand, on a high level, user mode debugging provides you with access to the private virtual address for a process.

    Correct.

    A debug session is limited to that process

    No. You can attach to several processes at the same time, e.g. with WinDbg's .tlist/.attach command.

    and it cannot overwrite or tamper w/ other process' virtual address space/data.

    No. You can modify the memory, e.g. with WinDbg's ed command.

    Kernel mode debug, I understand, provides access to other drivers and kernel processes that need full access to multiple resources,

    Correct.

    in addition to the original process address space.

    As far as I know, you have access to physical RAM only. Some of the virtual address space may be swapped, so not the full address space is available.

    From this, I get to thinking that kernel mode debugging seems more robust than user mode debugging.

    I think the opposite. If you write incorrect values somewhere in kernel mode, the PC crashes with a blue screen. If you do that in user mode, it's only the application that crashes.

    This raises the question for me: is there a time, when both options of debug mode are available, that it makes sense to choose user mode over a more robust kernel mode?

    If you debug an application only and no drivers are involved, I prefer user mode debugging.

    IMHO, kernel mode debugging is not more robust, it's more fragile - you can really break everything at the lowest level. User mode debugging provides the typical protection against crashes of the OS.

    I just seem to notice that a lot of people seem to try to avoid kernel debugging

    I observe the same. And usually it's not so difficult once they try it. In my debugging workshops, I explain processes and threads from kernel point of view and do it live in the kernel. And once people try kernel debugging, it's not such a mystery any more.

    I'm not entirely sure why, as it seems more robust.

    Well, you really can blow up everything in kernel mode.

    User mode debugging

    User mode debugging is the default that any IDE will do. The integration is usually good, in some IDEs it feels quite native.

    During user mode debugging, things are easy. If you access memory that is paged out to disk, the OS is still running and will simply page it in, so you can read and write it.

    You have access to everything that you know from application development. There are threads and you can suspend or resume them. The knowledge you have from application development will be sufficient to operate the debugger.

    You can set breakpoints and inspect variables (as long as you have correct symbols).

    Some kinds of debugging is only available in user mode. E.g. the SOS extension for WinDbg to debug .NET application only works in user mode.

    Kernel debugging

    Kernel debugging is quite complex. Typically, you can't simply do local kernel debugging - if you stop somewhere in the kernel, how do you control the debugger? The system will just freeze. So, for kernel debugging, you need 2 PCs (or virtual PCs).

    During kernel mode debugging, things are complex. While you are just inside an application, a millisecond later, some interrupt occurs and does something completely different. You don't only have threads, you also need to deal with call stacks that are outside your application, you'll see CPU register content, instruction pointers etc. That's all stuff a "normal" app developer does not want to care about.

    You don't only have access to everything that you implemented. You also have access to everything that Microsoft, Intel, NVidia and lots of other companies developed.

    You cannot simply access all memory, because some memory that is paged out to the swap file will first generate a page fault, then involve some disk driver to fetch the data, potentially page out some other data, etc.

    There is so much giong on in kernel mode and in order to not break it, you need to have really professional comprehension of all those topics.

    Conclusion

    Most developers just want to care about their source code. So if they are writing programs (aka. applications, scripts, tools, games), they just want user mode debugging. If "their code" is driver code, of course they want kernel debugging.

    And of course Security Specialists and Crackers want kernel mode debugging because they want privileges.