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, gdb_trap);
48
49 GDB_DBGPORT(null, NULL, NULL, NULL, NULL, NULL, NULL);
50 SET_DECLARE(gdb_dbgport_set, struct gdb_dbgport);
51
52 struct gdb_dbgport *gdb_cur = NULL;
53
54 static int
55 gdb_init(void)
56 {
57 struct gdb_dbgport *dp, **iter;
58 int cur_pri, pri;
59
60 gdb_cur = NULL;
61 cur_pri = -1;
62 SET_FOREACH(iter, gdb_dbgport_set) {
63 dp = *iter;
64 pri = (dp->gdb_probe != NULL) ? dp->gdb_probe() : -1;
65 dp->gdb_active = (pri >= 0) ? 0 : -1;
66 if (pri > cur_pri) {
67 cur_pri = pri;
68 gdb_cur = dp;
69 }
70 }
71 if (gdb_cur != NULL) {
72 printf("GDB: debug ports:");
73 SET_FOREACH(iter, gdb_dbgport_set) {
74 dp = *iter;
75 if (dp->gdb_active == 0)
76 printf(" %s", dp->gdb_name);
77 }
78 printf("\n");
79 } else
80 printf("GDB: no debug ports present\n");
81 if (gdb_cur != NULL) {
82 gdb_cur->gdb_init();
83 printf("GDB: current port: %s\n", gdb_cur->gdb_name);
84 }
85 if (gdb_cur != NULL)
86 cur_pri = (boothowto & RB_GDB) ? 2 : 0;
87 else
88 cur_pri = -1;
89 return (cur_pri);
90 }
91
92 static int
93 gdb_trap(int type, int code)
94 {
95 struct thread *thr_iter;
96
97 /*
98 * Send a T packet. We currently do not support watchpoints (the
99 * awatch, rwatch or watch elements).
100 */
101 gdb_tx_begin('T');
102 gdb_tx_hex(gdb_cpu_signal(type, code), 2);
103 gdb_tx_varhex(GDB_REG_PC);
104 gdb_tx_char(':');
105 gdb_tx_reg(GDB_REG_PC);
106 gdb_tx_char(';');
107 gdb_tx_str("thread:");
108 gdb_tx_varhex((long)kdb_thread->td_tid);
109 gdb_tx_char(';');
110 gdb_tx_end(); /* XXX check error condition. */
111
112 thr_iter = NULL;
113 while (gdb_rx_begin() == 0) {
114 /* printf("GDB: got '%s'\n", gdb_rxp); */
115 switch (gdb_rx_char()) {
116 case '?': /* Last signal. */
117 gdb_tx_begin('S');
118 gdb_tx_hex(gdb_cpu_signal(type, code), 2);
119 gdb_tx_end();
120 break;
121 case 'c': { /* Continue. */
122 uintmax_t addr;
123 register_t pc;
124 if (!gdb_rx_varhex(&addr)) {
125 pc = addr;
126 gdb_cpu_setreg(GDB_REG_PC, &pc);
127 }
128 kdb_cpu_clear_singlestep();
129 return (1);
130 }
131 case 'C': { /* Continue with signal. */
132 uintmax_t addr, sig;
133 register_t pc;
134 if (!gdb_rx_varhex(&sig) && gdb_rx_char() == ';' &&
135 !gdb_rx_varhex(&addr)) {
136 pc = addr;
137 gdb_cpu_setreg(GDB_REG_PC, &pc);
138 }
139 kdb_cpu_clear_singlestep();
140 return (1);
141 }
142 case 'D': { /* Detach */
143 gdb_tx_ok();
144 kdb_cpu_clear_singlestep();
145 return (1);
146 }
147 case 'g': { /* Read registers. */
148 size_t r;
149 gdb_tx_begin(0);
150 for (r = 0; r < GDB_NREGS; r++)
151 gdb_tx_reg(r);
152 gdb_tx_end();
153 break;
154 }
155 case 'G': /* Write registers. */
156 gdb_tx_err(0);
157 break;
158 case 'H': { /* Set thread. */
159 intmax_t tid;
160 struct thread *thr;
161 gdb_rx_char();
162 if (gdb_rx_varhex(&tid)) {
163 gdb_tx_err(EINVAL);
164 break;
165 }
166 if (tid > 0) {
167 thr = kdb_thr_lookup(tid);
168 if (thr == NULL) {
169 gdb_tx_err(ENOENT);
170 break;
171 }
172 kdb_thr_select(thr);
173 }
174 gdb_tx_ok();
175 break;
176 }
177 case 'k': /* Kill request. */
178 kdb_cpu_clear_singlestep();
179 return (1);
180 case 'm': { /* Read memory. */
181 uintmax_t addr, size;
182 if (gdb_rx_varhex(&addr) || gdb_rx_char() != ',' ||
183 gdb_rx_varhex(&size)) {
184 gdb_tx_err(EINVAL);
185 break;
186 }
187 gdb_tx_begin(0);
188 if (gdb_tx_mem((char *)(uintptr_t)addr, size))
189 gdb_tx_end();
190 else
191 gdb_tx_err(EIO);
192 break;
193 }
194 case 'M': { /* Write memory. */
195 uintmax_t addr, size;
196 if (gdb_rx_varhex(&addr) || gdb_rx_char() != ',' ||
197 gdb_rx_varhex(&size) || gdb_rx_char() != ':') {
198 gdb_tx_err(EINVAL);
199 break;
200 }
201 if (gdb_rx_mem((char *)(uintptr_t)addr, size) == 0)
202 gdb_tx_err(EIO);
203 else
204 gdb_tx_ok();
205 break;
206 }
207 case 'P': { /* Write register. */
208 char *val;
209 uintmax_t reg;
210 val = gdb_rxp;
211 if (gdb_rx_varhex(®) || gdb_rx_char() != '=' ||
212 !gdb_rx_mem(val, gdb_cpu_regsz(reg))) {
213 gdb_tx_err(EINVAL);
214 break;
215 }
216 gdb_cpu_setreg(reg, val);
217 gdb_tx_ok();
218 break;
219 }
220 case 'q': /* General query. */
221 if (gdb_rx_equal("fThreadInfo")) {
222 thr_iter = kdb_thr_first();
223 gdb_tx_begin('m');
224 gdb_tx_hex((long)thr_iter->td_tid, 8);
225 gdb_tx_end();
226 } else if (gdb_rx_equal("sThreadInfo")) {
227 if (thr_iter == NULL) {
228 gdb_tx_err(ENXIO);
229 break;
230 }
231 thr_iter = kdb_thr_next(thr_iter);
232 if (thr_iter != NULL) {
233 gdb_tx_begin('m');
234 gdb_tx_hex((long)thr_iter->td_tid, 8);
235 gdb_tx_end();
236 } else {
237 gdb_tx_begin('l');
238 gdb_tx_end();
239 }
240 } else if (!gdb_cpu_query())
241 gdb_tx_empty();
242 break;
243 case 's': { /* Step. */
244 uintmax_t addr;
245 register_t pc;
246 if (!gdb_rx_varhex(&addr)) {
247 pc = addr;
248 gdb_cpu_setreg(GDB_REG_PC, &pc);
249 }
250 kdb_cpu_set_singlestep();
251 return (1);
252 }
253 case 'S': { /* Step with signal. */
254 uintmax_t addr, sig;
255 register_t pc;
256 if (!gdb_rx_varhex(&sig) && gdb_rx_char() == ';' &&
257 !gdb_rx_varhex(&addr)) {
258 pc = addr;
259 gdb_cpu_setreg(GDB_REG_PC, &pc);
260 }
261 kdb_cpu_set_singlestep();
262 return (1);
263 }
264 case 'T': { /* Thread alive. */
265 intmax_t tid;
266 if (gdb_rx_varhex(&tid)) {
267 gdb_tx_err(EINVAL);
268 break;
269 }
270 if (kdb_thr_lookup(tid) != NULL)
271 gdb_tx_ok();
272 else
273 gdb_tx_err(ENOENT);
274 break;
275 }
276 case -1:
277 /* Empty command. Treat as unknown command. */
278 /* FALLTHROUGH */
279 default:
280 /* Unknown command. Send empty response. */
281 gdb_tx_empty();
282 break;
283 }
284 }
285 return (0);
286 }
Cache object: 0b50100aa3f3b4502c0ee3ee8cf2edec
|