stacktrace.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. #include <linux/kernel.h>
  2. typedef unsigned int instr;
  3. #define MAJOR_OP 0xfc000000
  4. #define LDA_OP 0x20000000
  5. #define STQ_OP 0xb4000000
  6. #define BR_OP 0xc0000000
  7. #define STK_ALLOC_1 0x23de8000 /* lda $30,-X($30) */
  8. #define STK_ALLOC_1M 0xffff8000
  9. #define STK_ALLOC_2 0x43c0153e /* subq $30,X,$30 */
  10. #define STK_ALLOC_2M 0xffe01fff
  11. #define MEM_REG 0x03e00000
  12. #define MEM_BASE 0x001f0000
  13. #define MEM_OFF 0x0000ffff
  14. #define MEM_OFF_SIGN 0x00008000
  15. #define BASE_SP 0x001e0000
  16. #define STK_ALLOC_MATCH(INSTR) \
  17. (((INSTR) & STK_ALLOC_1M) == STK_ALLOC_1 \
  18. || ((INSTR) & STK_ALLOC_2M) == STK_ALLOC_2)
  19. #define STK_PUSH_MATCH(INSTR) \
  20. (((INSTR) & (MAJOR_OP | MEM_BASE | MEM_OFF_SIGN)) == (STQ_OP | BASE_SP))
  21. #define MEM_OP_OFFSET(INSTR) \
  22. (((long)((INSTR) & MEM_OFF) << 48) >> 48)
  23. #define MEM_OP_REG(INSTR) \
  24. (((INSTR) & MEM_REG) >> 22)
  25. /* Branches, jumps, PAL calls, and illegal opcodes end a basic block. */
  26. #define BB_END(INSTR) \
  27. (((instr)(INSTR) >= BR_OP) | ((instr)(INSTR) < LDA_OP) | \
  28. ((((instr)(INSTR) ^ 0x60000000) < 0x20000000) & \
  29. (((instr)(INSTR) & 0x0c000000) != 0)))
  30. #define IS_KERNEL_TEXT(PC) ((unsigned long)(PC) > START_ADDR)
  31. static char reg_name[][4] = {
  32. "v0 ", "t0 ", "t1 ", "t2 ", "t3 ", "t4 ", "t5 ", "t6 ", "t7 ",
  33. "s0 ", "s1 ", "s2 ", "s3 ", "s4 ", "s5 ", "s6 ", "a0 ", "a1 ",
  34. "a2 ", "a3 ", "a4 ", "a5 ", "t8 ", "t9 ", "t10", "t11", "ra ",
  35. "pv ", "at ", "gp ", "sp ", "0"
  36. };
  37. static instr *
  38. display_stored_regs(instr * pro_pc, unsigned char * sp)
  39. {
  40. instr * ret_pc = 0;
  41. int reg;
  42. unsigned long value;
  43. printk("Prologue [<%p>], Frame %p:\n", pro_pc, sp);
  44. while (!BB_END(*pro_pc))
  45. if (STK_PUSH_MATCH(*pro_pc)) {
  46. reg = (*pro_pc & MEM_REG) >> 21;
  47. value = *(unsigned long *)(sp + (*pro_pc & MEM_OFF));
  48. if (reg == 26)
  49. ret_pc = (instr *)value;
  50. printk("\t\t%s / 0x%016lx\n", reg_name[reg], value);
  51. }
  52. return ret_pc;
  53. }
  54. static instr *
  55. seek_prologue(instr * pc)
  56. {
  57. while (!STK_ALLOC_MATCH(*pc))
  58. --pc;
  59. while (!BB_END(*(pc - 1)))
  60. --pc;
  61. return pc;
  62. }
  63. static long
  64. stack_increment(instr * prologue_pc)
  65. {
  66. while (!STK_ALLOC_MATCH(*prologue_pc))
  67. ++prologue_pc;
  68. /* Count the bytes allocated. */
  69. if ((*prologue_pc & STK_ALLOC_1M) == STK_ALLOC_1M)
  70. return -(((long)(*prologue_pc) << 48) >> 48);
  71. else
  72. return (*prologue_pc >> 13) & 0xff;
  73. }
  74. void
  75. stacktrace(void)
  76. {
  77. instr * ret_pc;
  78. instr * prologue = (instr *)stacktrace;
  79. register unsigned char * sp __asm__ ("$30");
  80. printk("\tstack trace:\n");
  81. do {
  82. ret_pc = display_stored_regs(prologue, sp);
  83. sp += stack_increment(prologue);
  84. prologue = seek_prologue(ret_pc);
  85. } while (IS_KERNEL_TEXT(ret_pc));
  86. }