I would like to use it to debug kernel drivers but I would try to avoid to add logging to all functions. OSReportWithBacktrace seems to work but I need symbols.
I'm not aware of a way to print symbolicated stack traces directly from a kext. You can get symbolicated panic logs by adding keepsyms=1
to the boot-args
nvram variable. I suspect the data structures for this have private linkage so you probably can't replicate the symbolicated panic code in your own kext. (It's in osfmk/i386/AT386/model_dep.c
of the xnu source though if you want to try.)
Your other option is to send the output from OSReportWithBacktrace
through the atos
command-line tool. For kext symbols, you'll need to find the kext's load address from kextstat
and pass that to the -l
command line argument.
Finally, you can of course use lldb kernel debugging to get a stack trace. If you need to set a breakpoint during early kext load, before you get a chance to do it from the lldb command line, you can insert __asm__("int $3")
(IIRC) at the point in the code where you want to break into the debugger.