gdb_main.c revision 133446
1131899Smarcel/*
2131899Smarcel * Copyright (c) 2004 Marcel Moolenaar
3131899Smarcel * All rights reserved.
4131899Smarcel *
5131899Smarcel * Redistribution and use in source and binary forms, with or without
6131899Smarcel * modification, are permitted provided that the following conditions
7131899Smarcel * are met:
8131899Smarcel *
9131899Smarcel * 1. Redistributions of source code must retain the above copyright
10131899Smarcel *    notice, this list of conditions and the following disclaimer.
11131899Smarcel * 2. Redistributions in binary form must reproduce the above copyright
12131899Smarcel *    notice, this list of conditions and the following disclaimer in the
13131899Smarcel *    documentation and/or other materials provided with the distribution.
14131899Smarcel *
15131899Smarcel * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
16131899Smarcel * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17131899Smarcel * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18131899Smarcel * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
19131899Smarcel * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20131899Smarcel * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21131899Smarcel * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22131899Smarcel * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23131899Smarcel * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24131899Smarcel * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25131899Smarcel */
26131899Smarcel
27131899Smarcel#include <sys/cdefs.h>
28131899Smarcel__FBSDID("$FreeBSD: head/sys/gdb/gdb_main.c 133446 2004-08-10 19:32:33Z marcel $");
29131899Smarcel
30131899Smarcel#include <sys/param.h>
31131899Smarcel#include <sys/systm.h>
32131899Smarcel#include <sys/kdb.h>
33131899Smarcel#include <sys/kernel.h>
34131899Smarcel#include <sys/pcpu.h>
35131899Smarcel#include <sys/proc.h>
36131899Smarcel#include <sys/reboot.h>
37131899Smarcel
38131899Smarcel#include <machine/gdb_machdep.h>
39131899Smarcel#include <machine/kdb.h>
40131899Smarcel
41131899Smarcel#include <gdb/gdb.h>
42131899Smarcel#include <gdb/gdb_int.h>
43131899Smarcel
44131899Smarcelstatic dbbe_init_f gdb_init;
45131899Smarcelstatic dbbe_trap_f gdb_trap;
46131899Smarcel
47131899SmarcelKDB_BACKEND(gdb, gdb_init, NULL, gdb_trap);
48131899Smarcel
49131899SmarcelGDB_DBGPORT(null, NULL, NULL, NULL, NULL, NULL, NULL);
50131899SmarcelSET_DECLARE(gdb_dbgport_set, struct gdb_dbgport);
51131899Smarcel
52131899Smarcelstruct gdb_dbgport *gdb_cur = NULL;
53131899Smarcel
54131899Smarcelstatic int
55131899Smarcelgdb_init(void)
56131899Smarcel{
57131899Smarcel	struct gdb_dbgport *dp, **iter;
58131899Smarcel	int cur_pri, pri;
59131899Smarcel
60131899Smarcel	gdb_cur = NULL;
61131899Smarcel	cur_pri = -1;
62131899Smarcel	SET_FOREACH(iter, gdb_dbgport_set) {
63131899Smarcel		dp = *iter;
64131899Smarcel		pri = (dp->gdb_probe != NULL) ? dp->gdb_probe() : -1;
65131899Smarcel		dp->gdb_active = (pri >= 0) ? 0 : -1;
66131899Smarcel		if (pri > cur_pri) {
67131899Smarcel			cur_pri = pri;
68131899Smarcel			gdb_cur = dp;
69131899Smarcel		}
70131899Smarcel	}
71131899Smarcel	if (gdb_cur != NULL) {
72131899Smarcel		printf("GDB: debug ports:");
73131899Smarcel		SET_FOREACH(iter, gdb_dbgport_set) {
74131899Smarcel			dp = *iter;
75131899Smarcel			if (dp->gdb_active == 0)
76131899Smarcel				printf(" %s", dp->gdb_name);
77131899Smarcel		}
78131899Smarcel		printf("\n");
79131899Smarcel	} else
80131899Smarcel		printf("GDB: no debug ports present\n");
81131899Smarcel	if (gdb_cur != NULL) {
82131899Smarcel		gdb_cur->gdb_init();
83131899Smarcel		printf("GDB: current port: %s\n", gdb_cur->gdb_name);
84131899Smarcel	}
85131899Smarcel	if (gdb_cur != NULL)
86131899Smarcel		cur_pri = (boothowto & RB_GDB) ? 2 : 0;
87131899Smarcel	else
88131899Smarcel		cur_pri = -1;
89131899Smarcel	return (cur_pri);
90131899Smarcel}
91131899Smarcel
92131899Smarcelstatic int
93131899Smarcelgdb_trap(int type, int code)
94131899Smarcel{
95131899Smarcel	struct thread *thr_iter;
96131899Smarcel
97131899Smarcel	/*
98131899Smarcel	 * Send a T packet. We currently do not support watchpoints (the
99131899Smarcel	 * awatch, rwatch or watch elements).
100131899Smarcel	 */
101131899Smarcel	gdb_tx_begin('T');
102131899Smarcel	gdb_tx_hex(gdb_cpu_signal(type, code), 2);
103131899Smarcel	gdb_tx_varhex(GDB_REG_PC);
104131899Smarcel	gdb_tx_char(':');
105131899Smarcel	gdb_tx_reg(GDB_REG_PC);
106131899Smarcel	gdb_tx_char(';');
107131899Smarcel	gdb_tx_str("thread:");
108131899Smarcel	gdb_tx_varhex((long)kdb_thread->td_tid);
109131899Smarcel	gdb_tx_char(';');
110131899Smarcel	gdb_tx_end();			/* XXX check error condition. */
111131899Smarcel
112131899Smarcel	thr_iter = NULL;
113131899Smarcel	while (gdb_rx_begin() == 0) {
114133446Smarcel		/* printf("GDB: got '%s'\n", gdb_rxp); */
115131899Smarcel		switch (gdb_rx_char()) {
116131899Smarcel		case '?':	/* Last signal. */
117131899Smarcel			gdb_tx_begin('S');
118131899Smarcel			gdb_tx_hex(gdb_cpu_signal(type, code), 2);
119131899Smarcel			gdb_tx_end();
120131899Smarcel			break;
121131899Smarcel		case 'c': {	/* Continue. */
122131899Smarcel			uintmax_t addr;
123131899Smarcel			if (!gdb_rx_varhex(&addr))
124131899Smarcel				gdb_cpu_setreg(GDB_REG_PC, addr);
125131899Smarcel			kdb_cpu_clear_singlestep();
126131899Smarcel			return (1);
127131899Smarcel		}
128131899Smarcel		case 'C': {	/* Continue with signal. */
129131899Smarcel			uintmax_t addr, sig;
130131899Smarcel			if (!gdb_rx_varhex(&sig) && gdb_rx_char() == ';' &&
131131899Smarcel			    !gdb_rx_varhex(&addr))
132131899Smarcel				gdb_cpu_setreg(GDB_REG_PC, addr);
133131899Smarcel			kdb_cpu_clear_singlestep();
134131899Smarcel			return (1);
135131899Smarcel		}
136131899Smarcel		case 'g': {	/* Read registers. */
137131899Smarcel			size_t r;
138131899Smarcel			gdb_tx_begin(0);
139131899Smarcel			for (r = 0; r < GDB_NREGS; r++)
140131899Smarcel				gdb_tx_reg(r);
141131899Smarcel			gdb_tx_end();
142131899Smarcel			break;
143131899Smarcel		}
144131899Smarcel		case 'G':	/* Write registers. */
145131899Smarcel			gdb_tx_err(0);
146131899Smarcel			break;
147131899Smarcel		case 'H': {	/* Set thread. */
148131899Smarcel			intmax_t tid;
149131899Smarcel			struct thread *thr;
150131899Smarcel			gdb_rx_char();
151131899Smarcel			gdb_rx_varhex(&tid);
152131899Smarcel			if (tid > 0) {
153131899Smarcel				thr = kdb_thr_lookup(tid);
154131899Smarcel				if (thr == NULL) {
155131899Smarcel					gdb_tx_err(ENOENT);
156131899Smarcel					break;
157131899Smarcel				}
158131899Smarcel				kdb_thr_select(thr);
159131899Smarcel			}
160131899Smarcel			gdb_tx_ok();
161131899Smarcel			break;
162131899Smarcel		}
163131899Smarcel		case 'k':	/* Kill request. */
164131899Smarcel			kdb_cpu_clear_singlestep();
165131899Smarcel			return (1);
166131899Smarcel		case 'm': {	/* Read memory. */
167131899Smarcel			uintmax_t addr, size;
168131899Smarcel			if (gdb_rx_varhex(&addr) || gdb_rx_char() != ',' ||
169131899Smarcel			    gdb_rx_varhex(&size)) {
170131899Smarcel				gdb_tx_err(EINVAL);
171131899Smarcel				break;
172131899Smarcel			}
173131899Smarcel			gdb_tx_begin(0);
174131899Smarcel			if (gdb_tx_mem((char *)(uintptr_t)addr, size))
175131899Smarcel				gdb_tx_end();
176131899Smarcel			else
177131899Smarcel				gdb_tx_err(EIO);
178131899Smarcel			break;
179131899Smarcel		}
180131899Smarcel		case 'M': {	/* Write memory. */
181131899Smarcel			uintmax_t addr, size;
182131899Smarcel			if (gdb_rx_varhex(&addr) || gdb_rx_char() != ',' ||
183131899Smarcel			    gdb_rx_varhex(&size) || gdb_rx_char() != ':') {
184131899Smarcel				gdb_tx_err(EINVAL);
185131899Smarcel				break;
186131899Smarcel			}
187131899Smarcel			if (gdb_rx_mem((char *)(uintptr_t)addr, size) == 0)
188131899Smarcel				gdb_tx_err(EIO);
189131899Smarcel			else
190131899Smarcel				gdb_tx_ok();
191131899Smarcel			break;
192131899Smarcel		}
193131899Smarcel		case 'P': {	/* Write register. */
194131899Smarcel			uintmax_t reg, val;
195131899Smarcel			if (gdb_rx_varhex(&reg) || gdb_rx_char() != '=' ||
196131899Smarcel			    gdb_rx_varhex(&val)) {
197131899Smarcel				gdb_tx_err(EINVAL);
198131899Smarcel				break;
199131899Smarcel			}
200131899Smarcel			gdb_cpu_setreg(reg, val);
201131899Smarcel			gdb_tx_ok();
202131899Smarcel			break;
203131899Smarcel		}
204131899Smarcel		case 'q':	/* General query. */
205131899Smarcel			if (gdb_rx_equal("fThreadInfo")) {
206131899Smarcel				thr_iter = kdb_thr_first();
207131899Smarcel				gdb_tx_begin('m');
208131899Smarcel				gdb_tx_hex((long)thr_iter->td_tid, 8);
209131899Smarcel				gdb_tx_end();
210131899Smarcel			} else if (gdb_rx_equal("sThreadInfo")) {
211131899Smarcel				if (thr_iter == NULL) {
212131899Smarcel					gdb_tx_err(ENXIO);
213131899Smarcel					break;
214131899Smarcel				}
215131899Smarcel				thr_iter = kdb_thr_next(thr_iter);
216131899Smarcel				if (thr_iter != NULL) {
217131899Smarcel					gdb_tx_begin('m');
218131899Smarcel					gdb_tx_hex((long)thr_iter->td_tid, 8);
219131899Smarcel					gdb_tx_end();
220131899Smarcel				} else {
221131899Smarcel					gdb_tx_begin('l');
222131899Smarcel					gdb_tx_end();
223131899Smarcel				}
224131899Smarcel			} else if (!gdb_cpu_query())
225131899Smarcel				gdb_tx_empty();
226131899Smarcel			break;
227131899Smarcel		case 's': {	/* Step. */
228131899Smarcel			uintmax_t addr;
229131899Smarcel			if (!gdb_rx_varhex(&addr))
230131899Smarcel				gdb_cpu_setreg(GDB_REG_PC, addr);
231131899Smarcel			kdb_cpu_set_singlestep();
232131899Smarcel			return (1);
233131899Smarcel		}
234131899Smarcel		case 'S': {	/* Step with signal. */
235131899Smarcel			uintmax_t addr, sig;
236131899Smarcel			if (!gdb_rx_varhex(&sig) && gdb_rx_char() == ';' &&
237131899Smarcel			    !gdb_rx_varhex(&addr))
238131899Smarcel				gdb_cpu_setreg(GDB_REG_PC, addr);
239131899Smarcel			kdb_cpu_set_singlestep();
240131899Smarcel			return (1);
241131899Smarcel		}
242131899Smarcel		case 'T': {	/* Thread alive. */
243131899Smarcel			intmax_t tid;
244131899Smarcel			gdb_rx_varhex(&tid);
245131899Smarcel			if (kdb_thr_lookup(tid) != NULL)
246131899Smarcel				gdb_tx_ok();
247131899Smarcel			else
248131899Smarcel				gdb_tx_err(ENOENT);
249131899Smarcel			break;
250131899Smarcel		}
251131899Smarcel		case -1:
252131899Smarcel			/* Empty command. Treat as unknown command. */
253131899Smarcel			/* FALLTHROUGH */
254131899Smarcel		default:
255131899Smarcel			/* Unknown command. Send empty response. */
256131899Smarcel			gdb_tx_empty();
257131899Smarcel			break;
258131899Smarcel		}
259131899Smarcel	}
260131899Smarcel	return (0);
261131899Smarcel}
262