kdb_debugger.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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 (ks->err_code == KDB_REASON_SYSTEM_NMI && ks->signo == SIGTRAP)
  64. reason = KDB_REASON_SYSTEM_NMI;
  65. else if (in_nmi())
  66. reason = KDB_REASON_NMI;
  67. for (i = 0, bp = kdb_breakpoints; i < KDB_MAXBPT; i++, bp++) {
  68. if ((bp->bp_enabled) && (bp->bp_addr == addr)) {
  69. reason = KDB_REASON_BREAK;
  70. db_result = KDB_DB_BPT;
  71. if (addr != instruction_pointer(ks->linux_regs))
  72. kgdb_arch_set_pc(ks->linux_regs, addr);
  73. break;
  74. }
  75. }
  76. if (reason == KDB_REASON_BREAK || reason == KDB_REASON_SWITCH) {
  77. for (i = 0, bp = kdb_breakpoints; i < KDB_MAXBPT; i++, bp++) {
  78. if (bp->bp_free)
  79. continue;
  80. if (bp->bp_addr == addr) {
  81. bp->bp_delay = 1;
  82. bp->bp_delayed = 1;
  83. /*
  84. * SSBPT is set when the kernel debugger must single step a
  85. * task in order to re-establish an instruction breakpoint
  86. * which uses the instruction replacement mechanism. It is
  87. * cleared by any action that removes the need to single-step
  88. * the breakpoint.
  89. */
  90. reason = KDB_REASON_BREAK;
  91. db_result = KDB_DB_BPT;
  92. KDB_STATE_SET(SSBPT);
  93. break;
  94. }
  95. }
  96. }
  97. if (reason != KDB_REASON_BREAK && ks->ex_vector == 0 &&
  98. ks->signo == SIGTRAP) {
  99. reason = KDB_REASON_SSTEP;
  100. db_result = KDB_DB_BPT;
  101. }
  102. /* Set initial kdb state variables */
  103. KDB_STATE_CLEAR(KGDB_TRANS);
  104. kdb_common_init_state(ks);
  105. /* Remove any breakpoints as needed by kdb and clear single step */
  106. kdb_bp_remove();
  107. KDB_STATE_CLEAR(DOING_SS);
  108. KDB_STATE_SET(PAGER);
  109. if (ks->err_code == DIE_OOPS || reason == KDB_REASON_OOPS) {
  110. ks->pass_exception = 1;
  111. KDB_FLAG_SET(CATASTROPHIC);
  112. }
  113. /* set CATASTROPHIC if the system contains unresponsive processors */
  114. for_each_online_cpu(i)
  115. if (!kgdb_info[i].enter_kgdb)
  116. KDB_FLAG_SET(CATASTROPHIC);
  117. if (KDB_STATE(SSBPT) && reason == KDB_REASON_SSTEP) {
  118. KDB_STATE_CLEAR(SSBPT);
  119. KDB_STATE_CLEAR(DOING_SS);
  120. } else {
  121. /* Start kdb main loop */
  122. error = kdb_main_loop(KDB_REASON_ENTER, reason,
  123. ks->err_code, db_result, ks->linux_regs);
  124. }
  125. /*
  126. * Upon exit from the kdb main loop setup break points and restart
  127. * the system based on the requested continue state
  128. */
  129. kdb_common_deinit_state();
  130. KDB_STATE_CLEAR(PAGER);
  131. kdbnearsym_cleanup();
  132. if (error == KDB_CMD_KGDB) {
  133. if (KDB_STATE(DOING_KGDB))
  134. KDB_STATE_CLEAR(DOING_KGDB);
  135. return DBG_PASS_EVENT;
  136. }
  137. kdb_bp_install(ks->linux_regs);
  138. dbg_activate_sw_breakpoints();
  139. /* Set the exit state to a single step or a continue */
  140. if (KDB_STATE(DOING_SS))
  141. gdbstub_state(ks, "s");
  142. else
  143. gdbstub_state(ks, "c");
  144. KDB_FLAG_CLEAR(CATASTROPHIC);
  145. /* Invoke arch specific exception handling prior to system resume */
  146. kgdb_info[ks->cpu].ret_state = gdbstub_state(ks, "e");
  147. if (ks->pass_exception)
  148. kgdb_info[ks->cpu].ret_state = 1;
  149. if (error == KDB_CMD_CPU) {
  150. KDB_STATE_SET(REENTRY);
  151. /*
  152. * Force clear the single step bit because kdb emulates this
  153. * differently vs the gdbstub
  154. */
  155. kgdb_single_step = 0;
  156. dbg_deactivate_sw_breakpoints();
  157. return DBG_SWITCH_CPU_EVENT;
  158. }
  159. return kgdb_info[ks->cpu].ret_state;
  160. }
  161. void kdb_gdb_state_pass(char *buf)
  162. {
  163. gdbstub_state(kdb_ks, buf);
  164. }