dumpstack.c 4.3 KB

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