kdb_debugger.c 4.5 KB

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