2018-11-16 14:47:21 +01:00
|
|
|
#include "stack.h"
|
2018-11-19 13:56:19 +01:00
|
|
|
#include "types.h"
|
2018-11-16 14:47:21 +01:00
|
|
|
#include "vga.h"
|
|
|
|
|
2018-11-19 13:56:19 +01:00
|
|
|
extern vaddr_t _stack_bottom;
|
|
|
|
extern vaddr_t _stack_top;
|
|
|
|
void printStackTrace(unsigned int maxFrames)
|
|
|
|
{
|
2018-11-16 14:47:21 +01:00
|
|
|
#ifdef DEBUG
|
|
|
|
// Now on Stack:
|
2018-11-19 13:56:19 +01:00
|
|
|
// ( potential second function argument )
|
2018-11-16 14:47:21 +01:00
|
|
|
// first function argument (maxFrames)
|
|
|
|
// return address from caller
|
|
|
|
// EBP (Extended Base Pointer) of calling function
|
2019-01-09 16:20:06 +01:00
|
|
|
//
|
|
|
|
// retrace function from address could done by optaining function address with gdb or
|
|
|
|
// objdump -S kernel
|
2018-11-19 13:56:19 +01:00
|
|
|
unsigned int *ebp = __builtin_frame_address(0);
|
|
|
|
for (unsigned int frame = 0; frame < maxFrames; frame++) {
|
|
|
|
unsigned int eip = ebp[1];
|
|
|
|
if (eip == 0) {
|
2018-11-16 14:47:21 +01:00
|
|
|
// No caller on stack
|
|
|
|
break;
|
|
|
|
}
|
2018-11-19 13:56:19 +01:00
|
|
|
unsigned int *arguments = ebp + 2;
|
|
|
|
printf("[%d] 0x%x (", frame, eip);
|
|
|
|
int nbArg = 0;
|
|
|
|
do {
|
|
|
|
if ((_stack_bottom <= (vaddr_t)arguments) &&
|
|
|
|
((vaddr_t)arguments <= _stack_top)) {
|
|
|
|
printf(" 0x%x", *arguments);
|
|
|
|
arguments += 1;
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
nbArg++;
|
|
|
|
if (nbArg >= 4) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} while (1);
|
|
|
|
printf(")\n");
|
2018-11-16 14:47:21 +01:00
|
|
|
ebp = (unsigned int *)(ebp[0]);
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
printf("Must be compiled with -fno-omit-frame-pointer for full stack\n");
|
2018-11-19 13:56:19 +01:00
|
|
|
unsigned int *ebp = &maxFrames - 2;
|
|
|
|
unsigned int eip = ebp[1];
|
|
|
|
printf("[0] 0x%x\n", eip);
|
2018-11-16 14:47:21 +01:00
|
|
|
#endif
|
|
|
|
}
|