dumpstack.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /* Provide basic stack dumping functions
  2. *
  3. * Copyright 2004-2009 Analog Devices Inc.
  4. *
  5. * Licensed under the GPL-2 or later
  6. */
  7. #include <linux/kernel.h>
  8. #include <linux/thread_info.h>
  9. #include <linux/mm.h>
  10. #include <linux/uaccess.h>
  11. #include <linux/module.h>
  12. #include <asm/trace.h>
  13. /*
  14. * Checks to see if the address pointed to is either a
  15. * 16-bit CALL instruction, or a 32-bit CALL instruction
  16. */
  17. static bool is_bfin_call(unsigned short *addr)
  18. {
  19. unsigned short opcode = 0, *ins_addr;
  20. ins_addr = (unsigned short *)addr;
  21. if (!get_instruction(&opcode, ins_addr))
  22. return false;
  23. if ((opcode >= 0x0060 && opcode <= 0x0067) ||
  24. (opcode >= 0x0070 && opcode <= 0x0077))
  25. return true;
  26. ins_addr--;
  27. if (!get_instruction(&opcode, ins_addr))
  28. return false;
  29. if (opcode >= 0xE300 && opcode <= 0xE3FF)
  30. return true;
  31. return false;
  32. }
  33. void show_stack(struct task_struct *task, unsigned long *stack)
  34. {
  35. #ifdef CONFIG_PRINTK
  36. unsigned int *addr, *endstack, *fp = 0, *frame;
  37. unsigned short *ins_addr;
  38. char buf[150];
  39. unsigned int i, j, ret_addr, frame_no = 0;
  40. /*
  41. * If we have been passed a specific stack, use that one otherwise
  42. * if we have been passed a task structure, use that, otherwise
  43. * use the stack of where the variable "stack" exists
  44. */
  45. if (stack == NULL) {
  46. if (task) {
  47. /* We know this is a kernel stack, so this is the start/end */
  48. stack = (unsigned long *)task->thread.ksp;
  49. endstack = (unsigned int *)(((unsigned int)(stack) & ~(THREAD_SIZE - 1)) + THREAD_SIZE);
  50. } else {
  51. /* print out the existing stack info */
  52. stack = (unsigned long *)&stack;
  53. endstack = (unsigned int *)PAGE_ALIGN((unsigned int)stack);
  54. }
  55. } else
  56. endstack = (unsigned int *)PAGE_ALIGN((unsigned int)stack);
  57. printk(KERN_NOTICE "Stack info:\n");
  58. decode_address(buf, (unsigned int)stack);
  59. printk(KERN_NOTICE " SP: [0x%p] %s\n", stack, buf);
  60. if (!access_ok(VERIFY_READ, stack, (unsigned int)endstack - (unsigned int)stack)) {
  61. printk(KERN_NOTICE "Invalid stack pointer\n");
  62. return;
  63. }
  64. /* First thing is to look for a frame pointer */
  65. for (addr = (unsigned int *)((unsigned int)stack & ~0xF); addr < endstack; addr++) {
  66. if (*addr & 0x1)
  67. continue;
  68. ins_addr = (unsigned short *)*addr;
  69. ins_addr--;
  70. if (is_bfin_call(ins_addr))
  71. fp = addr - 1;
  72. if (fp) {
  73. /* Let's check to see if it is a frame pointer */
  74. while (fp >= (addr - 1) && fp < endstack
  75. && fp && ((unsigned int) fp & 0x3) == 0)
  76. fp = (unsigned int *)*fp;
  77. if (fp == 0 || fp == endstack) {
  78. fp = addr - 1;
  79. break;
  80. }
  81. fp = 0;
  82. }
  83. }
  84. if (fp) {
  85. frame = fp;
  86. printk(KERN_NOTICE " FP: (0x%p)\n", fp);
  87. } else
  88. frame = 0;
  89. /*
  90. * Now that we think we know where things are, we
  91. * walk the stack again, this time printing things out
  92. * incase there is no frame pointer, we still look for
  93. * valid return addresses
  94. */
  95. /* First time print out data, next time, print out symbols */
  96. for (j = 0; j <= 1; j++) {
  97. if (j)
  98. printk(KERN_NOTICE "Return addresses in stack:\n");
  99. else
  100. printk(KERN_NOTICE " Memory from 0x%08lx to %p", ((long unsigned int)stack & ~0xF), endstack);
  101. fp = frame;
  102. frame_no = 0;
  103. for (addr = (unsigned int *)((unsigned int)stack & ~0xF), i = 0;
  104. addr < endstack; addr++, i++) {
  105. ret_addr = 0;
  106. if (!j && i % 8 == 0)
  107. printk(KERN_NOTICE "%p:", addr);
  108. /* if it is an odd address, or zero, just skip it */
  109. if (*addr & 0x1 || !*addr)
  110. goto print;
  111. ins_addr = (unsigned short *)*addr;
  112. /* Go back one instruction, and see if it is a CALL */
  113. ins_addr--;
  114. ret_addr = is_bfin_call(ins_addr);
  115. print:
  116. if (!j && stack == (unsigned long *)addr)
  117. printk("[%08x]", *addr);
  118. else if (ret_addr)
  119. if (j) {
  120. decode_address(buf, (unsigned int)*addr);
  121. if (frame == addr) {
  122. printk(KERN_NOTICE " frame %2i : %s\n", frame_no, buf);
  123. continue;
  124. }
  125. printk(KERN_NOTICE " address : %s\n", buf);
  126. } else
  127. printk("<%08x>", *addr);
  128. else if (fp == addr) {
  129. if (j)
  130. frame = addr+1;
  131. else
  132. printk("(%08x)", *addr);
  133. fp = (unsigned int *)*addr;
  134. frame_no++;
  135. } else if (!j)
  136. printk(" %08x ", *addr);
  137. }
  138. if (!j)
  139. printk("\n");
  140. }
  141. #endif
  142. }
  143. EXPORT_SYMBOL(show_stack);
  144. void dump_stack(void)
  145. {
  146. unsigned long stack;
  147. #ifdef CONFIG_DEBUG_BFIN_HWTRACE_ON
  148. int tflags;
  149. #endif
  150. trace_buffer_save(tflags);
  151. dump_bfin_trace_buffer();
  152. show_stack(current, &stack);
  153. trace_buffer_restore(tflags);
  154. }
  155. EXPORT_SYMBOL(dump_stack);