]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/gdb/gdb_packet.c
MFV r330102: ntp 4.2.8p11
[FreeBSD/FreeBSD.git] / sys / gdb / gdb_packet.c
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2004 Marcel Moolenaar
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/ctype.h>
35 #include <sys/kdb.h>
36 #include <sys/libkern.h>
37 #include <sys/ttydefaults.h>
38
39 #include <machine/gdb_machdep.h>
40 #include <machine/kdb.h>
41
42 #include <gdb/gdb.h>
43 #include <gdb/gdb_int.h>
44
45 static char gdb_rxbuf[GDB_BUFSZ];
46 char *gdb_rxp = NULL;
47 size_t gdb_rxsz = 0;
48 static char gdb_txbuf[GDB_BUFSZ];
49 char *gdb_txp = NULL;                   /* Used in inline functions. */
50
51 #define C2N(c)  (((c) < 'A') ? (c) - '0' : \
52             10 + (((c) < 'a') ? (c) - 'A' : (c) - 'a'))
53 #define N2C(n)  (((n) < 10) ? (n) + '0' : (n) + 'a' - 10)
54
55 /*
56  * Get a single character
57  */
58
59 static int
60 gdb_getc(void)
61 {
62         int c;
63
64         do
65                 c = gdb_cur->gdb_getc();
66         while (c == -1);
67
68         if (c == CTRL('C')) {
69                 printf("Received ^C; trying to switch back to ddb.\n");
70
71                 if (kdb_dbbe_select("ddb") != 0)
72                         printf("The ddb backend could not be selected.\n");
73                 else {
74                         printf("using longjmp, hope it works!\n");
75                         kdb_reenter();
76                 }
77         }
78         return (c);
79 }
80
81 /*
82  * Functions to receive and extract from a packet.
83  */
84
85 int
86 gdb_rx_begin(void)
87 {
88         int c, cksum;
89
90         gdb_rxp = NULL;
91         do {
92                 /*
93                  * Wait for the start character, ignore all others.
94                  * XXX needs a timeout.
95                  */
96                 while ((c = gdb_getc()) != '$')
97                         ;
98
99                 /* Read until a # or end of buffer is found. */
100                 cksum = 0;
101                 gdb_rxsz = 0;
102                 while (gdb_rxsz < sizeof(gdb_rxbuf) - 1) {
103                         c = gdb_getc();
104                         if (c == '#')
105                                 break;
106                         gdb_rxbuf[gdb_rxsz++] = c;
107                         cksum += c;
108                 }
109                 gdb_rxbuf[gdb_rxsz] = 0;
110                 cksum &= 0xff;
111
112                 /* Bail out on a buffer overflow. */
113                 if (c != '#') {
114                         gdb_cur->gdb_putc('-');
115                         return (ENOSPC);
116                 }
117
118                 c = gdb_getc();
119                 cksum -= (C2N(c) << 4) & 0xf0;
120                 c = gdb_getc();
121                 cksum -= C2N(c) & 0x0f;
122                 gdb_cur->gdb_putc((cksum == 0) ? '+' : '-');
123                 if (cksum != 0)
124                         printf("GDB: packet `%s' has invalid checksum\n",
125                             gdb_rxbuf);
126         } while (cksum != 0);
127
128         gdb_rxp = gdb_rxbuf;
129         return (0);
130 }
131
132 int
133 gdb_rx_equal(const char *str)
134 {
135         int len;
136
137         len = strlen(str);
138         if (len > gdb_rxsz || strncmp(str, gdb_rxp, len) != 0)
139                 return (0);
140         gdb_rxp += len;
141         gdb_rxsz -= len;
142         return (1);
143 }
144
145 int
146 gdb_rx_mem(unsigned char *addr, size_t size)
147 {
148         unsigned char *p;
149         void *prev;
150         jmp_buf jb;
151         size_t cnt;
152         int ret;
153         unsigned char c;
154
155         if (size * 2 != gdb_rxsz)
156                 return (-1);
157
158         prev = kdb_jmpbuf(jb);
159         ret = setjmp(jb);
160         if (ret == 0) {
161                 p = addr;
162                 cnt = size;
163                 while (cnt-- > 0) {
164                         c = (C2N(gdb_rxp[0]) << 4) & 0xf0;
165                         c |= C2N(gdb_rxp[1]) & 0x0f;
166                         *p++ = c;
167                         gdb_rxsz -= 2;
168                         gdb_rxp += 2;
169                 }
170                 kdb_cpu_sync_icache(addr, size);
171         }
172         (void)kdb_jmpbuf(prev);
173         return ((ret == 0) ? 1 : 0);
174 }
175
176 int
177 gdb_rx_varhex(uintmax_t *vp)
178 {
179         uintmax_t v;
180         int c, neg;
181
182         c = gdb_rx_char();
183         neg = (c == '-') ? 1 : 0;
184         if (neg == 1)
185                 c = gdb_rx_char();
186         if (!isxdigit(c)) {
187                 gdb_rxp -= ((c == -1) ? 0 : 1) + neg;
188                 gdb_rxsz += ((c == -1) ? 0 : 1) + neg;
189                 return (-1);
190         }
191         v = 0;
192         do {
193                 v <<= 4;
194                 v += C2N(c);
195                 c = gdb_rx_char();
196         } while (isxdigit(c));
197         if (c != -1) {
198                 gdb_rxp--;
199                 gdb_rxsz++;
200         }
201         *vp = (neg) ? -v : v;
202         return (0);
203 }
204
205 /*
206  * Function to build and send a package.
207  */
208
209 void
210 gdb_tx_begin(char tp)
211 {
212
213         gdb_txp = gdb_txbuf;
214         if (tp != '\0')
215                 gdb_tx_char(tp);
216 }
217
218 int
219 gdb_tx_end(void)
220 {
221         const char *p;
222         int runlen;
223         unsigned char c, cksum;
224
225         do {
226                 gdb_cur->gdb_putc('$');
227
228                 cksum = 0;
229                 p = gdb_txbuf;
230                 while (p < gdb_txp) {
231                         /* Send a character and start run-length encoding. */
232                         c = *p++;
233                         gdb_cur->gdb_putc(c);
234                         cksum += c;
235                         runlen = 0;
236                         /* Determine run-length and update checksum. */
237                         while (p < gdb_txp && *p == c) {
238                                 runlen++;
239                                 p++;
240                         }
241                         /* Emit the run-length encoded string. */
242                         while (runlen >= 97) {
243                                 gdb_cur->gdb_putc('*');
244                                 cksum += '*';
245                                 gdb_cur->gdb_putc(97+29);
246                                 cksum += 97+29;
247                                 runlen -= 97;
248                                 if (runlen > 0) {
249                                         gdb_cur->gdb_putc(c);
250                                         cksum += c;
251                                         runlen--;
252                                 }
253                         }
254                         if (runlen == 1) {
255                                 gdb_cur->gdb_putc(c);
256                                 cksum += c;
257                                 runlen--;
258                         }
259                         if (runlen == 0)
260                                 continue;
261                         /* Don't emit '$', '#', '+' or '-'. */
262                         if (runlen == 7) {
263                                 gdb_cur->gdb_putc(c);
264                                 cksum += c;
265                                 runlen--;
266                         }
267                         if (runlen == 6 || runlen == 14 || runlen == 16) {
268                                 gdb_cur->gdb_putc(c);
269                                 cksum += c;
270                                 runlen--;
271                         }
272                         gdb_cur->gdb_putc('*');
273                         cksum += '*';
274                         gdb_cur->gdb_putc(runlen+29);
275                         cksum += runlen+29;
276                 }
277
278                 gdb_cur->gdb_putc('#');
279                 c = cksum >> 4;
280                 gdb_cur->gdb_putc(N2C(c));
281                 c = cksum & 0x0f;
282                 gdb_cur->gdb_putc(N2C(c));
283
284                 c = gdb_getc();
285         } while (c != '+');
286
287         return (0);
288 }
289
290 int
291 gdb_tx_mem(const unsigned char *addr, size_t size)
292 {
293         void *prev;
294         jmp_buf jb;
295         int ret;
296
297         prev = kdb_jmpbuf(jb);
298         ret = setjmp(jb);
299         if (ret == 0) {
300                 while (size-- > 0) {
301                         *gdb_txp++ = N2C(*addr >> 4);
302                         *gdb_txp++ = N2C(*addr & 0x0f);
303                         addr++;
304                 }
305         }
306         (void)kdb_jmpbuf(prev);
307         return ((ret == 0) ? 1 : 0);
308 }
309
310 void
311 gdb_tx_reg(int regnum)
312 {
313         unsigned char *regp;
314         size_t regsz;
315
316         regp = gdb_cpu_getreg(regnum, &regsz);
317         if (regp == NULL) {
318                 /* Register unavailable. */
319                 while (regsz--) {
320                         gdb_tx_char('x');
321                         gdb_tx_char('x');
322                 }
323         } else
324                 gdb_tx_mem(regp, regsz);
325 }
326
327 /* Read binary data up until the end of the packet or until we have datalen decoded bytes */
328 int
329 gdb_rx_bindata(unsigned char *data, size_t datalen, size_t *amt)
330 {
331         int c;
332
333         *amt = 0;
334
335         while (*amt < datalen) {
336                 c = gdb_rx_char();
337                 /* End of packet? */
338                 if (c == -1)
339                         break;
340                 /* Escaped character up next */
341                 if (c == '}') {
342                         /* Truncated packet? Bail out */
343                         if ((c = gdb_rx_char()) == -1)
344                                 return (1);
345                         c ^= 0x20;
346                 }
347                 *(data++) = c & 0xff;
348                 (*amt)++;
349         }
350
351         return (0);
352 }
353
354 int
355 gdb_search_mem(const unsigned char *addr, size_t size, const unsigned char *pat, size_t patlen, const unsigned char **found)
356 {
357         void *prev;
358         jmp_buf jb;
359         int ret;
360
361         prev = kdb_jmpbuf(jb);
362         ret = setjmp(jb);
363         if (ret == 0)
364                 *found = memmem(addr, size, pat, patlen);
365
366         (void)kdb_jmpbuf(prev);
367         return ((ret == 0) ? 1 : 0);
368 }