blob: fe422d275782b6d6cfaca4cfca227832d352d357 [file] [log] [blame]
Jason Wessel5d5314d2010-05-20 21:04:20 -05001/*
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
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"
16
17/*
18 * KDB interface to KGDB internals
19 */
20get_char_func kdb_poll_funcs[] = {
21 dbg_io_get_char,
22 NULL,
Jason Wesself5316b42010-05-20 21:04:22 -050023 NULL,
24 NULL,
25 NULL,
26 NULL,
Jason Wessel5d5314d2010-05-20 21:04:20 -050027};
Jason Wesself5316b42010-05-20 21:04:22 -050028EXPORT_SYMBOL_GPL(kdb_poll_funcs);
29
30int kdb_poll_idx = 1;
31EXPORT_SYMBOL_GPL(kdb_poll_idx);
Jason Wessel5d5314d2010-05-20 21:04:20 -050032
Jason Wesself679c492011-05-23 13:17:41 -050033static struct kgdb_state *kdb_ks;
34
Jason Wessel5d5314d2010-05-20 21:04:20 -050035int kdb_stub(struct kgdb_state *ks)
36{
37 int error = 0;
38 kdb_bp_t *bp;
39 unsigned long addr = kgdb_arch_pc(ks->ex_vector, ks->linux_regs);
40 kdb_reason_t reason = KDB_REASON_OOPS;
41 kdb_dbtrap_t db_result = KDB_DB_NOBPT;
42 int i;
43
Jason Wesself679c492011-05-23 13:17:41 -050044 kdb_ks = ks;
Jason Wessel5d5314d2010-05-20 21:04:20 -050045 if (KDB_STATE(REENTRY)) {
46 reason = KDB_REASON_SWITCH;
47 KDB_STATE_CLEAR(REENTRY);
48 addr = instruction_pointer(ks->linux_regs);
49 }
50 ks->pass_exception = 0;
51 if (atomic_read(&kgdb_setting_breakpoint))
52 reason = KDB_REASON_KEYBOARD;
53
54 for (i = 0, bp = kdb_breakpoints; i < KDB_MAXBPT; i++, bp++) {
55 if ((bp->bp_enabled) && (bp->bp_addr == addr)) {
56 reason = KDB_REASON_BREAK;
57 db_result = KDB_DB_BPT;
58 if (addr != instruction_pointer(ks->linux_regs))
59 kgdb_arch_set_pc(ks->linux_regs, addr);
60 break;
61 }
62 }
63 if (reason == KDB_REASON_BREAK || reason == KDB_REASON_SWITCH) {
64 for (i = 0, bp = kdb_breakpoints; i < KDB_MAXBPT; i++, bp++) {
65 if (bp->bp_free)
66 continue;
67 if (bp->bp_addr == addr) {
68 bp->bp_delay = 1;
69 bp->bp_delayed = 1;
70 /*
71 * SSBPT is set when the kernel debugger must single step a
72 * task in order to re-establish an instruction breakpoint
73 * which uses the instruction replacement mechanism. It is
74 * cleared by any action that removes the need to single-step
75 * the breakpoint.
76 */
77 reason = KDB_REASON_BREAK;
78 db_result = KDB_DB_BPT;
79 KDB_STATE_SET(SSBPT);
80 break;
81 }
82 }
83 }
84
85 if (reason != KDB_REASON_BREAK && ks->ex_vector == 0 &&
86 ks->signo == SIGTRAP) {
87 reason = KDB_REASON_SSTEP;
88 db_result = KDB_DB_BPT;
89 }
90 /* Set initial kdb state variables */
91 KDB_STATE_CLEAR(KGDB_TRANS);
Jason Wessel495363d2010-05-21 08:46:00 -050092 kdb_initial_cpu = atomic_read(&kgdb_active);
Jason Wessel5d5314d2010-05-20 21:04:20 -050093 kdb_current_task = kgdb_info[ks->cpu].task;
94 kdb_current_regs = kgdb_info[ks->cpu].debuggerinfo;
95 /* Remove any breakpoints as needed by kdb and clear single step */
96 kdb_bp_remove();
97 KDB_STATE_CLEAR(DOING_SS);
98 KDB_STATE_CLEAR(DOING_SSB);
Jason Wesself5316b42010-05-20 21:04:22 -050099 KDB_STATE_SET(PAGER);
Jason Wessel5d5314d2010-05-20 21:04:20 -0500100 /* zero out any offline cpu data */
101 for_each_present_cpu(i) {
102 if (!cpu_online(i)) {
103 kgdb_info[i].debuggerinfo = NULL;
104 kgdb_info[i].task = NULL;
105 }
106 }
107 if (ks->err_code == DIE_OOPS || reason == KDB_REASON_OOPS) {
108 ks->pass_exception = 1;
109 KDB_FLAG_SET(CATASTROPHIC);
110 }
Jason Wessel5d5314d2010-05-20 21:04:20 -0500111 if (KDB_STATE(SSBPT) && reason == KDB_REASON_SSTEP) {
112 KDB_STATE_CLEAR(SSBPT);
113 KDB_STATE_CLEAR(DOING_SS);
114 } else {
115 /* Start kdb main loop */
116 error = kdb_main_loop(KDB_REASON_ENTER, reason,
117 ks->err_code, db_result, ks->linux_regs);
118 }
119 /*
120 * Upon exit from the kdb main loop setup break points and restart
121 * the system based on the requested continue state
122 */
123 kdb_initial_cpu = -1;
124 kdb_current_task = NULL;
125 kdb_current_regs = NULL;
Jason Wesself5316b42010-05-20 21:04:22 -0500126 KDB_STATE_CLEAR(PAGER);
Jason Wessel5d5314d2010-05-20 21:04:20 -0500127 kdbnearsym_cleanup();
128 if (error == KDB_CMD_KGDB) {
129 if (KDB_STATE(DOING_KGDB) || KDB_STATE(DOING_KGDB2)) {
Jason Wessel5d5314d2010-05-20 21:04:20 -0500130 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
143 KDB_FLAG_CLEAR(CATASTROPHIC);
144
145 /* Invoke arch specific exception handling prior to system resume */
146 kgdb_info[ks->cpu].ret_state = gdbstub_state(ks, "e");
147 if (ks->pass_exception)
148 kgdb_info[ks->cpu].ret_state = 1;
149 if (error == KDB_CMD_CPU) {
150 KDB_STATE_SET(REENTRY);
151 /*
152 * Force clear the single step bit because kdb emulates this
153 * differently vs the gdbstub
154 */
155 kgdb_single_step = 0;
156 dbg_deactivate_sw_breakpoints();
157 return DBG_SWITCH_CPU_EVENT;
158 }
159 return kgdb_info[ks->cpu].ret_state;
160}
161
Jason Wesself679c492011-05-23 13:17:41 -0500162void kdb_gdb_state_pass(char *buf)
163{
164 gdbstub_state(kdb_ks, buf);
165}