FreeBSD/Linux Kernel Cross Reference
sys/gdb/gdb_packet.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 AUTHOR ``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 AUTHOR 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/ctype.h>
33 #include <sys/kdb.h>
34
35 #include <machine/gdb_machdep.h>
36 #include <machine/kdb.h>
37
38 #include <gdb/gdb.h>
39 #include <gdb/gdb_int.h>
40
41 static char gdb_rxbuf[GDB_BUFSZ];
42 char *gdb_rxp = NULL;
43 size_t gdb_rxsz = 0;
44 static char gdb_txbuf[GDB_BUFSZ];
45 char *gdb_txp = NULL; /* Used in inline functions. */
46
47 #define C2N(c) (((c) < 'A') ? (c) - '' : \
48 10 + (((c) < 'a') ? (c) - 'A' : (c) - 'a'))
49 #define N2C(n) (((n) < 10) ? (n) + '' : (n) + 'a' - 10)
50
51 /*
52 * Get a single character
53 */
54
55 static int
56 gdb_getc(void)
57 {
58 int c;
59
60 do
61 c = gdb_cur->gdb_getc();
62 while (c == -1);
63 return (c);
64 }
65
66 /*
67 * Functions to receive and extract from a packet.
68 */
69
70 int
71 gdb_rx_begin(void)
72 {
73 int c, cksum;
74
75 gdb_rxp = NULL;
76 do {
77 /*
78 * Wait for the start character, ignore all others.
79 * XXX needs a timeout.
80 */
81 while ((c = gdb_getc()) != '$')
82 ;
83
84 /* Read until a # or end of buffer is found. */
85 cksum = 0;
86 gdb_rxsz = 0;
87 while (gdb_rxsz < sizeof(gdb_rxbuf) - 1) {
88 c = gdb_getc();
89 if (c == '#')
90 break;
91 gdb_rxbuf[gdb_rxsz++] = c;
92 cksum += c;
93 }
94 gdb_rxbuf[gdb_rxsz] = 0;
95 cksum &= 0xff;
96
97 /* Bail out on a buffer overflow. */
98 if (c != '#') {
99 gdb_cur->gdb_putc('-');
100 return (ENOSPC);
101 }
102
103 c = gdb_getc();
104 cksum -= (C2N(c) << 4) & 0xf0;
105 c = gdb_getc();
106 cksum -= C2N(c) & 0x0f;
107 gdb_cur->gdb_putc((cksum == 0) ? '+' : '-');
108 if (cksum != 0)
109 printf("GDB: packet `%s' has invalid checksum\n",
110 gdb_rxbuf);
111 } while (cksum != 0);
112
113 gdb_rxp = gdb_rxbuf;
114 return (0);
115 }
116
117 int
118 gdb_rx_equal(const char *str)
119 {
120 int len;
121
122 len = strlen(str);
123 if (len > gdb_rxsz || strncmp(str, gdb_rxp, len) != 0)
124 return (0);
125 gdb_rxp += len;
126 gdb_rxsz -= len;
127 return (1);
128 }
129
130 int
131 gdb_rx_mem(unsigned char *addr, size_t size)
132 {
133 unsigned char *p;
134 void *prev;
135 jmp_buf jb;
136 size_t cnt;
137 int ret;
138 unsigned char c;
139
140 if (size * 2 != gdb_rxsz)
141 return (-1);
142
143 prev = kdb_jmpbuf(jb);
144 ret = setjmp(jb);
145 if (ret == 0) {
146 p = addr;
147 cnt = size;
148 while (cnt-- > 0) {
149 c = (C2N(gdb_rxp[0]) << 4) & 0xf0;
150 c |= C2N(gdb_rxp[1]) & 0x0f;
151 *p++ = c;
152 gdb_rxsz -= 2;
153 gdb_rxp += 2;
154 }
155 kdb_cpu_sync_icache(addr, size);
156 }
157 (void)kdb_jmpbuf(prev);
158 return ((ret == 0) ? 1 : 0);
159 }
160
161 int
162 gdb_rx_varhex(uintmax_t *vp)
163 {
164 uintmax_t v;
165 int c, neg;
166
167 c = gdb_rx_char();
168 neg = (c == '-') ? 1 : 0;
169 if (neg == 1)
170 c = gdb_rx_char();
171 if (!isxdigit(c)) {
172 gdb_rxp -= ((c == -1) ? 0 : 1) + neg;
173 gdb_rxsz += ((c == -1) ? 0 : 1) + neg;
174 return (-1);
175 }
176 v = 0;
177 do {
178 v <<= 4;
179 v += C2N(c);
180 c = gdb_rx_char();
181 } while (isxdigit(c));
182 if (c != -1) {
183 gdb_rxp--;
184 gdb_rxsz++;
185 }
186 *vp = (neg) ? -v : v;
187 return (0);
188 }
189
190 /*
191 * Function to build and send a package.
192 */
193
194 void
195 gdb_tx_begin(char tp)
196 {
197
198 gdb_txp = gdb_txbuf;
199 if (tp != '\0')
200 gdb_tx_char(tp);
201 }
202
203 int
204 gdb_tx_end(void)
205 {
206 const char *p;
207 int runlen;
208 unsigned char c, cksum;
209
210 do {
211 gdb_cur->gdb_putc('$');
212
213 cksum = 0;
214 p = gdb_txbuf;
215 while (p < gdb_txp) {
216 /* Send a character and start run-length encoding. */
217 c = *p++;
218 gdb_cur->gdb_putc(c);
219 cksum += c;
220 runlen = 0;
221 /* Determine run-length and update checksum. */
222 while (p < gdb_txp && *p == c) {
223 runlen++;
224 p++;
225 }
226 /* Emit the run-length encoded string. */
227 while (runlen >= 97) {
228 gdb_cur->gdb_putc('*');
229 cksum += '*';
230 gdb_cur->gdb_putc(97+29);
231 cksum += 97+29;
232 runlen -= 97;
233 if (runlen > 0) {
234 gdb_cur->gdb_putc(c);
235 cksum += c;
236 runlen--;
237 }
238 }
239 if (runlen == 1) {
240 gdb_cur->gdb_putc(c);
241 cksum += c;
242 runlen--;
243 }
244 if (runlen == 0)
245 continue;
246 /* Don't emit '$', '#', '+' or '-'. */
247 if (runlen == 7) {
248 gdb_cur->gdb_putc(c);
249 cksum += c;
250 runlen--;
251 }
252 if (runlen == 6 || runlen == 14 || runlen == 16) {
253 gdb_cur->gdb_putc(c);
254 cksum += c;
255 runlen--;
256 }
257 gdb_cur->gdb_putc('*');
258 cksum += '*';
259 gdb_cur->gdb_putc(runlen+29);
260 cksum += runlen+29;
261 }
262
263 gdb_cur->gdb_putc('#');
264 c = cksum >> 4;
265 gdb_cur->gdb_putc(N2C(c));
266 c = cksum & 0x0f;
267 gdb_cur->gdb_putc(N2C(c));
268
269 c = gdb_getc();
270 } while (c != '+');
271
272 return (0);
273 }
274
275 int
276 gdb_tx_mem(const unsigned char *addr, size_t size)
277 {
278 void *prev;
279 jmp_buf jb;
280 int ret;
281
282 prev = kdb_jmpbuf(jb);
283 ret = setjmp(jb);
284 if (ret == 0) {
285 while (size-- > 0) {
286 *gdb_txp++ = N2C(*addr >> 4);
287 *gdb_txp++ = N2C(*addr & 0x0f);
288 addr++;
289 }
290 }
291 (void)kdb_jmpbuf(prev);
292 return ((ret == 0) ? 1 : 0);
293 }
294
295 void
296 gdb_tx_reg(int regnum)
297 {
298 unsigned char *regp;
299 size_t regsz;
300
301 regp = gdb_cpu_getreg(regnum, ®sz);
302 if (regp == NULL) {
303 /* Register unavailable. */
304 while (regsz--) {
305 gdb_tx_char('x');
306 gdb_tx_char('x');
307 }
308 } else
309 gdb_tx_mem(regp, regsz);
310 }
Cache object: 69bb960b26ca13ab3722c5bc514e3e97
|