2 * Created by: Jason Wessel <jason.wessel@windriver.com>
4 * Copyright (c) 2009 Wind River Systems, Inc. All Rights Reserved.
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.
11 #include <linux/kgdb.h>
12 #include <linux/kdb.h>
13 #include <linux/kdebug.h>
14 #include "kdb_private.h"
15 #include "../debug_core.h"
18 * KDB interface to KGDB internals
20 get_char_func kdb_poll_funcs[] = {
25 int kdb_stub(struct kgdb_state *ks)
29 unsigned long addr = kgdb_arch_pc(ks->ex_vector, ks->linux_regs);
30 kdb_reason_t reason = KDB_REASON_OOPS;
31 kdb_dbtrap_t db_result = KDB_DB_NOBPT;
34 if (KDB_STATE(REENTRY)) {
35 reason = KDB_REASON_SWITCH;
36 KDB_STATE_CLEAR(REENTRY);
37 addr = instruction_pointer(ks->linux_regs);
39 ks->pass_exception = 0;
40 if (atomic_read(&kgdb_setting_breakpoint))
41 reason = KDB_REASON_KEYBOARD;
43 for (i = 0, bp = kdb_breakpoints; i < KDB_MAXBPT; i++, bp++) {
44 if ((bp->bp_enabled) && (bp->bp_addr == addr)) {
45 reason = KDB_REASON_BREAK;
46 db_result = KDB_DB_BPT;
47 if (addr != instruction_pointer(ks->linux_regs))
48 kgdb_arch_set_pc(ks->linux_regs, addr);
52 if (reason == KDB_REASON_BREAK || reason == KDB_REASON_SWITCH) {
53 for (i = 0, bp = kdb_breakpoints; i < KDB_MAXBPT; i++, bp++) {
56 if (bp->bp_addr == addr) {
60 * SSBPT is set when the kernel debugger must single step a
61 * task in order to re-establish an instruction breakpoint
62 * which uses the instruction replacement mechanism. It is
63 * cleared by any action that removes the need to single-step
66 reason = KDB_REASON_BREAK;
67 db_result = KDB_DB_BPT;
74 if (reason != KDB_REASON_BREAK && ks->ex_vector == 0 &&
75 ks->signo == SIGTRAP) {
76 reason = KDB_REASON_SSTEP;
77 db_result = KDB_DB_BPT;
79 /* Set initial kdb state variables */
80 KDB_STATE_CLEAR(KGDB_TRANS);
81 kdb_initial_cpu = ks->cpu;
82 kdb_current_task = kgdb_info[ks->cpu].task;
83 kdb_current_regs = kgdb_info[ks->cpu].debuggerinfo;
84 /* Remove any breakpoints as needed by kdb and clear single step */
86 KDB_STATE_CLEAR(DOING_SS);
87 KDB_STATE_CLEAR(DOING_SSB);
88 /* zero out any offline cpu data */
89 for_each_present_cpu(i) {
91 kgdb_info[i].debuggerinfo = NULL;
92 kgdb_info[i].task = NULL;
95 if (ks->err_code == DIE_OOPS || reason == KDB_REASON_OOPS) {
96 ks->pass_exception = 1;
97 KDB_FLAG_SET(CATASTROPHIC);
99 kdb_initial_cpu = ks->cpu;
100 if (KDB_STATE(SSBPT) && reason == KDB_REASON_SSTEP) {
101 KDB_STATE_CLEAR(SSBPT);
102 KDB_STATE_CLEAR(DOING_SS);
104 /* Start kdb main loop */
105 error = kdb_main_loop(KDB_REASON_ENTER, reason,
106 ks->err_code, db_result, ks->linux_regs);
109 * Upon exit from the kdb main loop setup break points and restart
110 * the system based on the requested continue state
112 kdb_initial_cpu = -1;
113 kdb_current_task = NULL;
114 kdb_current_regs = NULL;
115 kdbnearsym_cleanup();
116 if (error == KDB_CMD_KGDB) {
117 if (KDB_STATE(DOING_KGDB) || KDB_STATE(DOING_KGDB2)) {
119 * This inteface glue which allows kdb to transition in into
120 * the gdb stub. In order to do this the '?' or '' gdb serial
121 * packet response is processed here. And then control is
122 * passed to the gdbstub.
124 if (KDB_STATE(DOING_KGDB))
125 gdbstub_state(ks, "?");
127 gdbstub_state(ks, "");
128 KDB_STATE_CLEAR(DOING_KGDB);
129 KDB_STATE_CLEAR(DOING_KGDB2);
131 return DBG_PASS_EVENT;
133 kdb_bp_install(ks->linux_regs);
134 dbg_activate_sw_breakpoints();
135 /* Set the exit state to a single step or a continue */
136 if (KDB_STATE(DOING_SS))
137 gdbstub_state(ks, "s");
139 gdbstub_state(ks, "c");
141 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);
150 * Force clear the single step bit because kdb emulates this
151 * differently vs the gdbstub
153 kgdb_single_step = 0;
154 dbg_deactivate_sw_breakpoints();
155 return DBG_SWITCH_CPU_EVENT;
157 return kgdb_info[ks->cpu].ret_state;