xref: /linux/kernel/debug/kdb/kdb_debugger.c (revision 7e66eaf14e19c032433be7c4df3c892fa2a5282f)
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 
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  */
20 get_char_func kdb_poll_funcs[] = {
21 	dbg_io_get_char,
22 	NULL,
23 	NULL,
24 	NULL,
25 	NULL,
26 	NULL,
27 };
28 EXPORT_SYMBOL_GPL(kdb_poll_funcs);
29 
30 int kdb_poll_idx = 1;
31 EXPORT_SYMBOL_GPL(kdb_poll_idx);
32 
33 static struct kgdb_state *kdb_ks;
34 
35 int 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 
44 	kdb_ks = ks;
45 	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);
92 	kdb_initial_cpu = atomic_read(&kgdb_active);
93 	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);
99 	KDB_STATE_SET(PAGER);
100 	/* 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 	}
111 	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;
126 	KDB_STATE_CLEAR(PAGER);
127 	kdbnearsym_cleanup();
128 	if (error == KDB_CMD_KGDB) {
129 		if (KDB_STATE(DOING_KGDB))
130 			KDB_STATE_CLEAR(DOING_KGDB);
131 		return DBG_PASS_EVENT;
132 	}
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");
138 	else
139 		gdbstub_state(ks, "c");
140 
141 	KDB_FLAG_CLEAR(CATASTROPHIC);
142 
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 }
159 
160 void kdb_gdb_state_pass(char *buf)
161 {
162 	gdbstub_state(kdb_ks, buf);
163 }
164