FreeBSD/Linux Kernel Cross Reference
sys/gdb/gdb_main.c
1 /*-
2 * Copyright (c) 2004 Marcel Moolenaar
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/kdb.h>
33 #include <sys/kernel.h>
34 #include <sys/pcpu.h>
35 #include <sys/proc.h>
36 #include <sys/reboot.h>
37
38 #include <machine/gdb_machdep.h>
39 #include <machine/kdb.h>
40
41 #include <gdb/gdb.h>
42 #include <gdb/gdb_int.h>
43
44 static dbbe_init_f gdb_init;
45 static dbbe_trap_f gdb_trap;
46
47 KDB_BACKEND(gdb, gdb_init, NULL, NULL, gdb_trap);
48
49 static struct gdb_dbgport null_gdb_dbgport;
50 DATA_SET(gdb_dbgport_set, null_gdb_dbgport);
51 SET_DECLARE(gdb_dbgport_set, struct gdb_dbgport);
52
53 struct gdb_dbgport *gdb_cur = NULL;
54 int gdb_listening = 0;
55
56 static int
57 gdb_init(void)
58 {
59 struct gdb_dbgport *dp, **iter;
60 int cur_pri, pri;
61
62 gdb_cur = NULL;
63 cur_pri = -1;
64 SET_FOREACH(iter, gdb_dbgport_set) {
65 dp = *iter;
66 pri = (dp->gdb_probe != NULL) ? dp->gdb_probe() : -1;
67 dp->gdb_active = (pri >= 0) ? 0 : -1;
68 if (pri > cur_pri) {
69 cur_pri = pri;
70 gdb_cur = dp;
71 }
72 }
73 if (gdb_cur != NULL) {
74 printf("GDB: debug ports:");
75 SET_FOREACH(iter, gdb_dbgport_set) {
76 dp = *iter;
77 if (dp->gdb_active == 0)
78 printf(" %s", dp->gdb_name);
79 }
80 printf("\n");
81 } else
82 printf("GDB: no debug ports present\n");
83 if (gdb_cur != NULL) {
84 gdb_cur->gdb_init();
85 printf("GDB: current port: %s\n", gdb_cur->gdb_name);
86 }
87 if (gdb_cur != NULL) {
88 cur_pri = (boothowto & RB_GDB) ? 2 : 0;
89 gdb_consinit();
90 } else
91 cur_pri = -1;
92 return (cur_pri);
93 }
94
95 static int
96 gdb_trap(int type, int code)
97 {
98 jmp_buf jb;
99 struct thread *thr_iter;
100 void *prev_jb;
101
102 prev_jb = kdb_jmpbuf(jb);
103 if (setjmp(jb) != 0) {
104 printf("%s bailing, hopefully back to ddb!\n", __func__);
105 gdb_listening = 0;
106 (void)kdb_jmpbuf(prev_jb);
107 return (1);
108 }
109
110 gdb_listening = 0;
111 /*
112 * Send a T packet. We currently do not support watchpoints (the
113 * awatch, rwatch or watch elements).
114 */
115 gdb_tx_begin('T');
116 gdb_tx_hex(gdb_cpu_signal(type, code), 2);
117 gdb_tx_varhex(GDB_REG_PC);
118 gdb_tx_char(':');
119 gdb_tx_reg(GDB_REG_PC);
120 gdb_tx_char(';');
121 gdb_tx_str("thread:");
122 gdb_tx_varhex((long)kdb_thread->td_tid);
123 gdb_tx_char(';');
124 gdb_tx_end(); /* XXX check error condition. */
125
126 thr_iter = NULL;
127 while (gdb_rx_begin() == 0) {
128 /* printf("GDB: got '%s'\n", gdb_rxp); */
129 switch (gdb_rx_char()) {
130 case '?': /* Last signal. */
131 gdb_tx_begin('S');
132 gdb_tx_hex(gdb_cpu_signal(type, code), 2);
133 gdb_tx_end();
134 break;
135 case 'c': { /* Continue. */
136 uintmax_t addr;
137 register_t pc;
138 if (!gdb_rx_varhex(&addr)) {
139 pc = addr;
140 gdb_cpu_setreg(GDB_REG_PC, &pc);
141 }
142 kdb_cpu_clear_singlestep();
143 gdb_listening = 1;
144 return (1);
145 }
146 case 'C': { /* Continue with signal. */
147 uintmax_t addr, sig;
148 register_t pc;
149 if (!gdb_rx_varhex(&sig) && gdb_rx_char() == ';' &&
150 !gdb_rx_varhex(&addr)) {
151 pc = addr;
152 gdb_cpu_setreg(GDB_REG_PC, &pc);
153 }
154 kdb_cpu_clear_singlestep();
155 gdb_listening = 1;
156 return (1);
157 }
158 case 'D': { /* Detach */
159 gdb_tx_ok();
160 kdb_cpu_clear_singlestep();
161 return (1);
162 }
163 case 'g': { /* Read registers. */
164 size_t r;
165 gdb_tx_begin(0);
166 for (r = 0; r < GDB_NREGS; r++)
167 gdb_tx_reg(r);
168 gdb_tx_end();
169 break;
170 }
171 case 'G': /* Write registers. */
172 gdb_tx_err(0);
173 break;
174 case 'H': { /* Set thread. */
175 intmax_t tid;
176 struct thread *thr;
177 gdb_rx_char();
178 if (gdb_rx_varhex(&tid)) {
179 gdb_tx_err(EINVAL);
180 break;
181 }
182 if (tid > 0) {
183 thr = kdb_thr_lookup(tid);
184 if (thr == NULL) {
185 gdb_tx_err(ENOENT);
186 break;
187 }
188 kdb_thr_select(thr);
189 }
190 gdb_tx_ok();
191 break;
192 }
193 case 'k': /* Kill request. */
194 kdb_cpu_clear_singlestep();
195 gdb_listening = 1;
196 return (1);
197 case 'm': { /* Read memory. */
198 uintmax_t addr, size;
199 if (gdb_rx_varhex(&addr) || gdb_rx_char() != ',' ||
200 gdb_rx_varhex(&size)) {
201 gdb_tx_err(EINVAL);
202 break;
203 }
204 gdb_tx_begin(0);
205 if (gdb_tx_mem((char *)(uintptr_t)addr, size))
206 gdb_tx_end();
207 else
208 gdb_tx_err(EIO);
209 break;
210 }
211 case 'M': { /* Write memory. */
212 uintmax_t addr, size;
213 if (gdb_rx_varhex(&addr) || gdb_rx_char() != ',' ||
214 gdb_rx_varhex(&size) || gdb_rx_char() != ':') {
215 gdb_tx_err(EINVAL);
216 break;
217 }
218 if (gdb_rx_mem((char *)(uintptr_t)addr, size) == 0)
219 gdb_tx_err(EIO);
220 else
221 gdb_tx_ok();
222 break;
223 }
224 case 'P': { /* Write register. */
225 char *val;
226 uintmax_t reg;
227 val = gdb_rxp;
228 if (gdb_rx_varhex(®) || gdb_rx_char() != '=' ||
229 !gdb_rx_mem(val, gdb_cpu_regsz(reg))) {
230 gdb_tx_err(EINVAL);
231 break;
232 }
233 gdb_cpu_setreg(reg, val);
234 gdb_tx_ok();
235 break;
236 }
237 case 'q': /* General query. */
238 if (gdb_rx_equal("fThreadInfo")) {
239 thr_iter = kdb_thr_first();
240 gdb_tx_begin('m');
241 gdb_tx_hex((long)thr_iter->td_tid, 8);
242 gdb_tx_end();
243 } else if (gdb_rx_equal("sThreadInfo")) {
244 if (thr_iter == NULL) {
245 gdb_tx_err(ENXIO);
246 break;
247 }
248 thr_iter = kdb_thr_next(thr_iter);
249 if (thr_iter != NULL) {
250 gdb_tx_begin('m');
251 gdb_tx_hex((long)thr_iter->td_tid, 8);
252 gdb_tx_end();
253 } else {
254 gdb_tx_begin('l');
255 gdb_tx_end();
256 }
257 } else if (!gdb_cpu_query())
258 gdb_tx_empty();
259 break;
260 case 's': { /* Step. */
261 uintmax_t addr;
262 register_t pc;
263 if (!gdb_rx_varhex(&addr)) {
264 pc = addr;
265 gdb_cpu_setreg(GDB_REG_PC, &pc);
266 }
267 kdb_cpu_set_singlestep();
268 gdb_listening = 1;
269 return (1);
270 }
271 case 'S': { /* Step with signal. */
272 uintmax_t addr, sig;
273 register_t pc;
274 if (!gdb_rx_varhex(&sig) && gdb_rx_char() == ';' &&
275 !gdb_rx_varhex(&addr)) {
276 pc = addr;
277 gdb_cpu_setreg(GDB_REG_PC, &pc);
278 }
279 kdb_cpu_set_singlestep();
280 gdb_listening = 1;
281 return (1);
282 }
283 case 'T': { /* Thread alive. */
284 intmax_t tid;
285 if (gdb_rx_varhex(&tid)) {
286 gdb_tx_err(EINVAL);
287 break;
288 }
289 if (kdb_thr_lookup(tid) != NULL)
290 gdb_tx_ok();
291 else
292 gdb_tx_err(ENOENT);
293 break;
294 }
295 case -1:
296 /* Empty command. Treat as unknown command. */
297 /* FALLTHROUGH */
298 default:
299 /* Unknown command. Send empty response. */
300 gdb_tx_empty();
301 break;
302 }
303 }
304 (void)kdb_jmpbuf(prev_jb);
305 return (0);
306 }
Cache object: 52c59e208ed01e846b5e9b89011b9048
|