kdb_debugger.c 4.2 KB

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