kdb_debugger.c 4.2 KB

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