kdb_debugger.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. /* zero out any offline cpu data */
  110. for_each_present_cpu(i) {
  111. if (!cpu_online(i)) {
  112. kgdb_info[i].debuggerinfo = NULL;
  113. kgdb_info[i].task = NULL;
  114. }
  115. }
  116. if (ks->err_code == DIE_OOPS || reason == KDB_REASON_OOPS) {
  117. ks->pass_exception = 1;
  118. KDB_FLAG_SET(CATASTROPHIC);
  119. }
  120. if (KDB_STATE(SSBPT) && reason == KDB_REASON_SSTEP) {
  121. KDB_STATE_CLEAR(SSBPT);
  122. KDB_STATE_CLEAR(DOING_SS);
  123. } else {
  124. /* Start kdb main loop */
  125. error = kdb_main_loop(KDB_REASON_ENTER, reason,
  126. ks->err_code, db_result, ks->linux_regs);
  127. }
  128. /*
  129. * Upon exit from the kdb main loop setup break points and restart
  130. * the system based on the requested continue state
  131. */
  132. kdb_common_deinit_state();
  133. KDB_STATE_CLEAR(PAGER);
  134. kdbnearsym_cleanup();
  135. if (error == KDB_CMD_KGDB) {
  136. if (KDB_STATE(DOING_KGDB))
  137. KDB_STATE_CLEAR(DOING_KGDB);
  138. return DBG_PASS_EVENT;
  139. }
  140. kdb_bp_install(ks->linux_regs);
  141. dbg_activate_sw_breakpoints();
  142. /* Set the exit state to a single step or a continue */
  143. if (KDB_STATE(DOING_SS))
  144. gdbstub_state(ks, "s");
  145. else
  146. gdbstub_state(ks, "c");
  147. KDB_FLAG_CLEAR(CATASTROPHIC);
  148. /* Invoke arch specific exception handling prior to system resume */
  149. kgdb_info[ks->cpu].ret_state = gdbstub_state(ks, "e");
  150. if (ks->pass_exception)
  151. kgdb_info[ks->cpu].ret_state = 1;
  152. if (error == KDB_CMD_CPU) {
  153. KDB_STATE_SET(REENTRY);
  154. /*
  155. * Force clear the single step bit because kdb emulates this
  156. * differently vs the gdbstub
  157. */
  158. kgdb_single_step = 0;
  159. dbg_deactivate_sw_breakpoints();
  160. return DBG_SWITCH_CPU_EVENT;
  161. }
  162. return kgdb_info[ks->cpu].ret_state;
  163. }
  164. void kdb_gdb_state_pass(char *buf)
  165. {
  166. gdbstub_state(kdb_ks, buf);
  167. }