dumpstack.c 4.3 KB

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