R – How does GDB find the symbol of a function when backtracing in MIPS Linux

gdbkernellinuxmips

I'm trying to implement a function to backtrace a crashed user space process in kernel. Since, I'm working in Kernel, I don't have the luxury of any libraries and provided backtrace function doesn't support MIPS architecture. I'm just wondering if I can emulate what GDB does.
The version of the kernel is 2.6.21.

Best Solution

I think you can reference the oprofile's implementation.

Oprofile use frame-pointer to get the back trace infomation, so it requires the user space applications and its related libraries all built with frame-pointer enabled.("-fno-omit-frame-pointer" option).

Another way is that if the user applications contains debug infomation, you may need check the user application's DWARF infomation, the DWARF call frame infomation proides the debugger with enough infomation about how a function called, so that it can locate each of the arguments to the function, locate the current call frame, and locate the call frame for the calling infomation.

If you want to simple crawl the back trace infomation without "frame-pointer" supported or any debug infomation, then you have to check the user application's mips instruction, loop through child's context(SP,IP,RP) things to get the parent context(SP,IP,RP) by the mips ABI specification, this is a little complicated and time consuming since you have to disassemble many instruction in the memory, but it works not bad. For example, for many routie, there is an "add sp, sp, -32" like instruction at the begging, and you will know that the parent'sp is current sp plus 32.

The second and third way you have to implement it yourself since you work in kernel.