kdb_debugger.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. * Created by: Jason Wessel <jason.wessel@windriver.com>
  3. *
  4. * Copyright (c) 2009 Wind River Systems, Inc. All Rights Reserved.
  5. *
  6. * This file is licensed under the terms of the GNU General Public
  7. * License version 2. This program is licensed "as is" without any
  8. * warranty of any kind, whether express or implied.
  9. */
  10. #include <linux/kgdb.h>
  11. #include <linux/kdb.h>
  12. #include <linux/kdebug.h>
  13. #include <linux/export.h>
  14. #include <linux/hardirq.h>
  15. #include "kdb_private.h"
  16. #include "../debug_core.h"
  17. /*
  18. * KDB interface to KGDB internals
  19. */
  20. get_char_func kdb_poll_funcs[] = {
  21. dbg_io_get_char,
  22. NULL,
  23. NULL,
  24. NULL,
  25. NULL,
  26. NULL,
  27. };
  28. EXPORT_SYMBOL_GPL(kdb_poll_funcs);
  29. int kdb_poll_idx = 1;
  30. EXPORT_SYMBOL_GPL(kdb_poll_idx);
  31. static struct kgdb_state *kdb_ks;
  32. int kdb_common_init_state(struct kgdb_state *ks)
  33. {
  34. kdb_initial_cpu = atomic_read(&kgdb_active);
  35. kdb_current_task = kgdb_info[ks->cpu].task;
  36. kdb_current_regs = kgdb_info[ks->cpu].debuggerinfo;
  37. return 0;
  38. }
  39. int kdb_common_deinit_state(void)
  40. {
  41. kdb_initial_cpu = -1;
  42. kdb_current_task = NULL;
  43. kdb_current_regs = NULL;
  44. return 0;
  45. }
  46. int kdb_stub(struct kgdb_state *ks)
  47. {
  48. int error = 0;
  49. kdb_bp_t *bp;
  50. unsigned long addr = kgdb_arch_pc(ks->ex_vector, ks->linux_regs);
  51. kdb_reason_t reason = KDB_REASON_OOPS;
  52. kdb_dbtrap_t db_result = KDB_DB_NOBPT;
  53. int i;
  54. kdb_ks = ks;
  55. if (KDB_STATE(REENTRY)) {
  56. reason = KDB_REASON_SWITCH;
  57. KDB_STATE_CLEAR(REENTRY);
  58. addr = instruction_pointer(ks->linux_regs);
  59. }
  60. ks->pass_exception = 0;
  61. if (atomic_read(&kgdb_setting_breakpoint))
  62. reason = KDB_REASON_KEYBOARD;
  63. if (in_nmi())
  64. reason = KDB_REASON_NMI;
  65. for (i = 0, bp = kdb_breakpoints; i < KDB_MAXBPT; i++, bp++) {
  66. if ((bp->bp_enabled) && (bp->bp_addr == addr)) {
  67. reason = KDB_REASON_BREAK;
  68. db_result = KDB_DB_BPT;
  69. if (addr != instruction_pointer(ks->linux_regs))
  70. kgdb_arch_set_pc(ks->linux_regs, addr);
  71. break;
  72. }
  73. }
  74. if (reason == KDB_REASON_BREAK || reason == KDB_REASON_SWITCH) {
  75. for (i = 0, bp = kdb_breakpoints; i < KDB_MAXBPT; i++, bp++) {
  76. if (bp->bp_free)
  77. continue;
  78. if (bp->bp_addr == addr) {
  79. bp->bp_delay = 1;
  80. bp->bp_delayed = 1;
  81. /*
  82. * SSBPT is set when the kernel debugger must single step a
  83. * task in order to re-establish an instruction breakpoint
  84. * which uses the instruction replacement mechanism. It is
  85. * cleared by any action that removes the need to single-step
  86. * the breakpoint.
  87. */
  88. reason = KDB_REASON_BREAK;
  89. db_result = KDB_DB_BPT;
  90. KDB_STATE_SET(SSBPT);
  91. break;
  92. }
  93. }
  94. }
  95. if (reason != KDB_REASON_BREAK && ks->ex_vector == 0 &&
  96. ks->signo == SIGTRAP) {
  97. reason = KDB_REASON_SSTEP;
  98. db_result = KDB_DB_BPT;
  99. }
  100. /* Set initial kdb state variables */
  101. KDB_STATE_CLEAR(KGDB_TRANS);
  102. kdb_common_init_state(ks);
  103. /* Remove any breakpoints as needed by kdb and clear single step */
  104. kdb_bp_remove();
  105. KDB_STATE_CLEAR(DOING_SS);
  106. KDB_STATE_SET(PAGER);
  107. /* zero out any offline cpu data */
  108. for_each_present_cpu(i) {
  109. if (!cpu_online(i)) {
  110. kgdb_info[i].debuggerinfo = NULL;
  111. kgdb_info[i].task = NULL;
  112. }
  113. }
  114. if (ks->err_code == DIE_OOPS || reason == KDB_REASON_OOPS) {
  115. ks->pass_exception = 1;
  116. KDB_FLAG_SET(CATASTROPHIC);
  117. }
  118. if (KDB_STATE(SSBPT) && reason == KDB_REASON_SSTEP) {
  119. KDB_STATE_CLEAR(SSBPT);
  120. KDB_STATE_CLEAR(DOING_SS);
  121. } else {
  122. /* Start kdb main loop */
  123. error = kdb_main_loop(KDB_REASON_ENTER, reason,
  124. ks->err_code, db_result, ks->linux_regs);
  125. }
  126. /*
  127. * Upon exit from the kdb main loop setup break points and restart
  128. * the system based on the requested continue state
  129. */
  130. kdb_common_deinit_state();
  131. KDB_STATE_CLEAR(PAGER);
  132. kdbnearsym_cleanup();
  133. if (error == KDB_CMD_KGDB) {
  134. if (KDB_STATE(DOING_KGDB))
  135. KDB_STATE_CLEAR(DOING_KGDB);
  136. return DBG_PASS_EVENT;
  137. }
  138. kdb_bp_install(ks->linux_regs);
  139. dbg_activate_sw_breakpoints();
  140. /* Set the exit state to a single step or a continue */
  141. if (KDB_STATE(DOING_SS))
  142. gdbstub_state(ks, "s");
  143. else
  144. gdbstub_state(ks, "c");
  145. KDB_FLAG_CLEAR(CATASTROPHIC);
  146. /* Invoke arch specific exception handling prior to system resume */
  147. kgdb_info[ks->cpu].ret_state = gdbstub_state(ks, "e");
  148. if (ks->pass_exception)
  149. kgdb_info[ks->cpu].ret_state = 1;
  150. if (error == KDB_CMD_CPU) {
  151. KDB_STATE_SET(REENTRY);
  152. /*
  153. * Force clear the single step bit because kdb emulates this
  154. * differently vs the gdbstub
  155. */
  156. kgdb_single_step = 0;
  157. dbg_deactivate_sw_breakpoints();
  158. return DBG_SWITCH_CPU_EVENT;
  159. }
  160. return kgdb_info[ks->cpu].ret_state;
  161. }
  162. void kdb_gdb_state_pass(char *buf)
  163. {
  164. gdbstub_state(kdb_ks, buf);
  165. }