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