kdb_debugger.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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_stub(struct kgdb_state *ks)
  33. {
  34. int error = 0;
  35. kdb_bp_t *bp;
  36. unsigned long addr = kgdb_arch_pc(ks->ex_vector, ks->linux_regs);
  37. kdb_reason_t reason = KDB_REASON_OOPS;
  38. kdb_dbtrap_t db_result = KDB_DB_NOBPT;
  39. int i;
  40. kdb_ks = ks;
  41. if (KDB_STATE(REENTRY)) {
  42. reason = KDB_REASON_SWITCH;
  43. KDB_STATE_CLEAR(REENTRY);
  44. addr = instruction_pointer(ks->linux_regs);
  45. }
  46. ks->pass_exception = 0;
  47. if (atomic_read(&kgdb_setting_breakpoint))
  48. reason = KDB_REASON_KEYBOARD;
  49. if (in_nmi())
  50. reason = KDB_REASON_NMI;
  51. for (i = 0, bp = kdb_breakpoints; i < KDB_MAXBPT; i++, bp++) {
  52. if ((bp->bp_enabled) && (bp->bp_addr == addr)) {
  53. reason = KDB_REASON_BREAK;
  54. db_result = KDB_DB_BPT;
  55. if (addr != instruction_pointer(ks->linux_regs))
  56. kgdb_arch_set_pc(ks->linux_regs, addr);
  57. break;
  58. }
  59. }
  60. if (reason == KDB_REASON_BREAK || reason == KDB_REASON_SWITCH) {
  61. for (i = 0, bp = kdb_breakpoints; i < KDB_MAXBPT; i++, bp++) {
  62. if (bp->bp_free)
  63. continue;
  64. if (bp->bp_addr == addr) {
  65. bp->bp_delay = 1;
  66. bp->bp_delayed = 1;
  67. /*
  68. * SSBPT is set when the kernel debugger must single step a
  69. * task in order to re-establish an instruction breakpoint
  70. * which uses the instruction replacement mechanism. It is
  71. * cleared by any action that removes the need to single-step
  72. * the breakpoint.
  73. */
  74. reason = KDB_REASON_BREAK;
  75. db_result = KDB_DB_BPT;
  76. KDB_STATE_SET(SSBPT);
  77. break;
  78. }
  79. }
  80. }
  81. if (reason != KDB_REASON_BREAK && ks->ex_vector == 0 &&
  82. ks->signo == SIGTRAP) {
  83. reason = KDB_REASON_SSTEP;
  84. db_result = KDB_DB_BPT;
  85. }
  86. /* Set initial kdb state variables */
  87. KDB_STATE_CLEAR(KGDB_TRANS);
  88. kdb_initial_cpu = atomic_read(&kgdb_active);
  89. kdb_current_task = kgdb_info[ks->cpu].task;
  90. kdb_current_regs = kgdb_info[ks->cpu].debuggerinfo;
  91. /* Remove any breakpoints as needed by kdb and clear single step */
  92. kdb_bp_remove();
  93. KDB_STATE_CLEAR(DOING_SS);
  94. KDB_STATE_CLEAR(DOING_SSB);
  95. KDB_STATE_SET(PAGER);
  96. /* zero out any offline cpu data */
  97. for_each_present_cpu(i) {
  98. if (!cpu_online(i)) {
  99. kgdb_info[i].debuggerinfo = NULL;
  100. kgdb_info[i].task = NULL;
  101. }
  102. }
  103. if (ks->err_code == DIE_OOPS || reason == KDB_REASON_OOPS) {
  104. ks->pass_exception = 1;
  105. KDB_FLAG_SET(CATASTROPHIC);
  106. }
  107. if (KDB_STATE(SSBPT) && reason == KDB_REASON_SSTEP) {
  108. KDB_STATE_CLEAR(SSBPT);
  109. KDB_STATE_CLEAR(DOING_SS);
  110. } else {
  111. /* Start kdb main loop */
  112. error = kdb_main_loop(KDB_REASON_ENTER, reason,
  113. ks->err_code, db_result, ks->linux_regs);
  114. }
  115. /*
  116. * Upon exit from the kdb main loop setup break points and restart
  117. * the system based on the requested continue state
  118. */
  119. kdb_initial_cpu = -1;
  120. kdb_current_task = NULL;
  121. kdb_current_regs = NULL;
  122. KDB_STATE_CLEAR(PAGER);
  123. kdbnearsym_cleanup();
  124. if (error == KDB_CMD_KGDB) {
  125. if (KDB_STATE(DOING_KGDB))
  126. KDB_STATE_CLEAR(DOING_KGDB);
  127. return DBG_PASS_EVENT;
  128. }
  129. kdb_bp_install(ks->linux_regs);
  130. dbg_activate_sw_breakpoints();
  131. /* Set the exit state to a single step or a continue */
  132. if (KDB_STATE(DOING_SS))
  133. gdbstub_state(ks, "s");
  134. else
  135. gdbstub_state(ks, "c");
  136. KDB_FLAG_CLEAR(CATASTROPHIC);
  137. /* Invoke arch specific exception handling prior to system resume */
  138. kgdb_info[ks->cpu].ret_state = gdbstub_state(ks, "e");
  139. if (ks->pass_exception)
  140. kgdb_info[ks->cpu].ret_state = 1;
  141. if (error == KDB_CMD_CPU) {
  142. KDB_STATE_SET(REENTRY);
  143. /*
  144. * Force clear the single step bit because kdb emulates this
  145. * differently vs the gdbstub
  146. */
  147. kgdb_single_step = 0;
  148. dbg_deactivate_sw_breakpoints();
  149. return DBG_SWITCH_CPU_EVENT;
  150. }
  151. return kgdb_info[ks->cpu].ret_state;
  152. }
  153. void kdb_gdb_state_pass(char *buf)
  154. {
  155. gdbstub_state(kdb_ks, buf);
  156. }