ia64/xen-unstable

view xen/common/gdbstub.c @ 9776:72f9c751d3ea

Replace &foo[0] with foo where the latter seems cleaner
(which is usually, and particularly when its an argument
to one of the bitops functions).

Signed-off-by: Keir Fraser <keir@xensource.com>
author kaf24@firebug.cl.cam.ac.uk
date Wed Apr 19 18:32:20 2006 +0100 (2006-04-19)
parents 6aa5179f2416
children 19b126974c1f
line source
1 /*
2 * Copyright (C) 2005 Jimi Xenidis <jimix@watson.ibm.com>, IBM Corporation
3 * Copyright (C) 2006 Isaku Yamahata <yamahata at valinux co jp>
4 * VA Linux Systems Japan. K.K.
5 *
6 * gdbstub arch neutral part
7 * Based on x86 cdb (xen/arch/x86/cdb.c) and ppc gdbstub(xen/common/gdbstub.c)
8 * But extensively modified.
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 */
25 /*
26 * gdbstub: implements the architecture independant parts of the
27 * gdb remote protocol.
28 */
30 /* We try to avoid assuming much about what the rest of the system is
31 doing. In particular, dynamic memory allocation is out of the
32 question. */
34 /* Resuming after we've stopped used to work, but more through luck
35 than any actual intention. It doesn't at the moment. */
37 #include <xen/lib.h>
38 #include <xen/spinlock.h>
39 #include <xen/serial.h>
40 #include <xen/irq.h>
41 #include <asm/debugger.h>
42 #include <xen/init.h>
43 #include <xen/smp.h>
44 #include <xen/console.h>
46 /* Printk isn't particularly safe just after we've trapped to the
47 debugger. so avoid it. */
48 #define dbg_printk(...)
49 /*#define dbg_printk(...) printk(__VA_ARGS__)*/
51 #define GDB_RETRY_MAX 10
53 static char opt_gdb[30] = "none";
54 string_param("gdb", opt_gdb);
56 /* value <-> char (de)serialzers */
57 char
58 hex2char(unsigned long x)
59 {
60 const char array[] = "0123456789abcdef";
62 return array[x & 15];
63 }
65 int
66 char2hex(unsigned char c)
67 {
68 if ( (c >= '0') && (c <= '9') )
69 return c - '0';
70 else if ( (c >= 'a') && (c <= 'f') )
71 return c - 'a' + 10;
72 else if ( (c >= 'A') && (c <= 'F') )
73 return c - 'A' + 10;
74 else
75 BUG();
76 return -1;
77 }
79 char
80 str2hex(const char *str)
81 {
82 return (char2hex(str[0]) << 4) | char2hex(str[1]);
83 }
85 unsigned long
86 str2ulong(const char *str, unsigned long bytes)
87 {
88 unsigned long x = 0;
89 unsigned long i = 0;
91 while ( *str && (i < (bytes * 2)) )
92 {
93 x <<= 4;
94 x += char2hex(*str);
95 ++str;
96 ++i;
97 }
99 return x;
100 }
102 /* gdb io wrappers */
103 static signed long
104 gdb_io_write(const char *buf, unsigned long len, struct gdb_context *ctx)
105 {
106 int i;
107 for ( i = 0; i < len; i++ )
108 serial_putc(ctx->serhnd, buf[i]);
109 return i;
110 }
112 static int
113 gdb_io_write_char(u8 data, struct gdb_context *ctx)
114 {
115 return gdb_io_write((char*)&data, 1, ctx);
116 }
118 static unsigned char
119 gdb_io_read(struct gdb_context *ctx)
120 {
121 return serial_getc(ctx->serhnd);
122 }
124 /* Receive a command. Returns -1 on csum error, 0 otherwise. */
125 /* Does not acknowledge. */
126 static int
127 attempt_receive_packet(struct gdb_context *ctx)
128 {
129 u8 csum;
130 u8 received_csum;
131 u8 ch;
133 /* Skip over everything up to the first '$' */
134 while ( (ch = gdb_io_read(ctx)) != '$' )
135 continue;
137 csum = 0;
138 for ( ctx->in_bytes = 0;
139 ctx->in_bytes < sizeof(ctx->in_buf);
140 ctx->in_bytes++ )
141 {
142 ch = gdb_io_read(ctx);
143 if ( ch == '#' )
144 break;
145 ctx->in_buf[ctx->in_bytes] = ch;
146 csum += ch;
147 }
149 if ( ctx->in_bytes == sizeof(ctx->in_buf) )
150 {
151 dbg_printk("WARNING: GDB sent a stupidly big packet.\n");
152 return -1;
153 }
155 ctx->in_buf[ctx->in_bytes] = '\0';
156 received_csum = char2hex(gdb_io_read(ctx)) * 16 +
157 char2hex(gdb_io_read(ctx));
159 return (received_csum == csum) ? 0 : -1;
160 }
162 /* Receive a command, discarding up to ten packets with csum
163 * errors. Acknowledges all received packets. */
164 static int
165 receive_command(struct gdb_context *ctx)
166 {
167 int r, count = 0;
169 count = 0;
170 do {
171 r = attempt_receive_packet(ctx);
172 gdb_io_write_char((r < 0) ? '-' : '+', ctx);
173 count++;
174 } while ( (r < 0) && (count < GDB_RETRY_MAX) );
176 return r;
177 }
179 /* routines to send reply packets */
181 static void
182 gdb_start_packet(struct gdb_context *ctx)
183 {
184 ctx->out_buf[0] = '$';
185 ctx->out_offset = 1;
186 ctx->out_csum = 0;
187 }
189 static void
190 gdb_write_to_packet_char(u8 data, struct gdb_context *ctx)
191 {
192 ctx->out_csum += data;
193 ctx->out_buf[ctx->out_offset] = data;
194 ctx->out_offset++;
195 }
197 void
198 gdb_write_to_packet(const char *buf, int count, struct gdb_context *ctx)
199 {
200 int x;
201 for ( x = 0; x < count; x++ )
202 gdb_write_to_packet_char(buf[x], ctx);
203 }
205 void
206 gdb_write_to_packet_str(const char *buf, struct gdb_context *ctx)
207 {
208 gdb_write_to_packet(buf, strlen(buf), ctx);
209 }
211 void
212 gdb_write_to_packet_hex(unsigned long x, int int_size, struct gdb_context *ctx)
213 {
214 char buf[sizeof(unsigned long) * 2 + 1];
215 int i = sizeof(unsigned long) * 2;
216 int width = int_size * 2;
218 buf[sizeof(unsigned long) * 2] = 0;
220 switch ( int_size )
221 {
222 case sizeof(u8):
223 case sizeof(u16):
224 case sizeof(u32):
225 case sizeof(u64):
226 break;
227 default:
228 dbg_printk("WARNING: %s x: 0x%lx int_size: %d\n",
229 __func__, x, int_size);
230 break;
231 }
233 do {
234 buf[--i] = hex2char(x & 15);
235 x >>= 4;
236 } while ( x );
238 while ( (i + width) > (sizeof(unsigned long) * 2) )
239 buf[--i] = '0';
241 gdb_write_to_packet(&buf[i], width, ctx);
242 }
244 static int
245 gdb_check_ack(struct gdb_context *ctx)
246 {
247 u8 c = gdb_io_read(ctx);
249 switch ( c )
250 {
251 case '+':
252 return 1;
253 case '-':
254 return 0;
255 default:
256 printk("Bad ack: %c\n", c);
257 return 0;
258 }
259 }
261 /* Return 0 if the reply was successfully received, !0 otherwise. */
262 void
263 gdb_send_packet(struct gdb_context *ctx)
264 {
265 char buf[3];
266 int count;
268 sprintf(buf, "%.02x\n", ctx->out_csum);
270 gdb_write_to_packet_char('#', ctx);
271 gdb_write_to_packet(buf, 2, ctx);
273 count = 0;
274 do {
275 gdb_io_write(ctx->out_buf, ctx->out_offset, ctx);
276 } while ( !gdb_check_ack(ctx) && (count++ < GDB_RETRY_MAX) );
278 if ( count == GDB_RETRY_MAX )
279 dbg_printk("WARNING: %s reached max retry %d\n",
280 __func__, GDB_RETRY_MAX);
281 }
283 void
284 gdb_send_reply(const char *buf, struct gdb_context *ctx)
285 {
286 gdb_start_packet(ctx);
287 gdb_write_to_packet_str(buf, ctx);
288 gdb_send_packet(ctx);
289 }
291 /* arch neutral command handlers */
293 static void
294 gdb_cmd_signum(struct gdb_context *ctx)
295 {
296 gdb_write_to_packet_char('S', ctx);
297 gdb_write_to_packet_hex(ctx->signum, sizeof(ctx->signum), ctx);
298 gdb_send_packet(ctx);
299 }
301 static void
302 gdb_cmd_read_mem(unsigned long addr, unsigned long length,
303 struct gdb_context *ctx)
304 {
305 int x, r;
306 unsigned char val;
308 dbg_printk("Memory read starting at %lx, length %lx.\n", addr,
309 length);
311 for ( x = 0; x < length; x++ )
312 {
313 r = gdb_arch_copy_from_user(&val, (void *)(addr + x), 1);
314 if ( r != 0 )
315 {
316 dbg_printk("Error reading from %lx.\n", addr + x);
317 break;
318 }
319 gdb_write_to_packet_hex(val, sizeof(val), ctx);
320 }
322 if ( x == 0 )
323 gdb_write_to_packet_str("E05", ctx);
325 dbg_printk("Read done.\n");
327 gdb_send_packet(ctx);
328 }
330 static void
331 gdb_cmd_write_mem(unsigned long addr, unsigned long length,
332 const char *buf, struct gdb_context *ctx)
333 {
334 int x, r;
335 unsigned char val;
337 dbg_printk("Memory write starting at %lx, length %lx.\n", addr, length);
339 for ( x = 0; x < length; x++, addr++, buf += 2 )
340 {
341 val = str2ulong(buf, sizeof(val));
342 r = gdb_arch_copy_to_user((void*)addr, (void*)&val, 1);
343 if ( r != 0 )
344 {
345 dbg_printk("Error writing to %lx.\n", addr);
346 break;
347 }
348 }
350 if (x == length)
351 gdb_write_to_packet_str("OK", ctx);
352 else
353 gdb_write_to_packet_str("E11", ctx);
355 dbg_printk("Write done.\n");
357 gdb_send_packet(ctx);
358 }
360 /* command dispatcher */
361 static int
362 process_command(struct cpu_user_regs *regs, struct gdb_context *ctx)
363 {
364 char *ptr;
365 unsigned long addr, length;
366 int resume = 0;
368 /* XXX check ctx->in_bytes >= 2 or similar. */
370 gdb_start_packet(ctx);
371 switch ( ctx->in_buf[0] )
372 {
373 case '?': /* query signal number */
374 gdb_cmd_signum(ctx);
375 break;
376 case 'H': /* thread operations */
377 gdb_send_reply("OK", ctx);
378 break;
379 case 'g': /* Read registers */
380 gdb_arch_read_reg_array(regs, ctx);
381 break;
382 case 'G': /* Write registers */
383 gdb_arch_write_reg_array(regs, ctx->in_buf + 1, ctx);
384 break;
385 case 'm': /* Read memory */
386 addr = simple_strtoul(ctx->in_buf + 1, &ptr, 16);
387 if ( (ptr == (ctx->in_buf + 1)) || (ptr[0] != ',') )
388 {
389 gdb_send_reply("E03", ctx);
390 return 0;
391 }
392 length = simple_strtoul(ptr + 1, &ptr, 16);
393 if ( ptr[0] != 0 )
394 {
395 gdb_send_reply("E04", ctx);
396 return 0;
397 }
398 gdb_cmd_read_mem(addr, length, ctx);
399 break;
400 case 'M': /* Write memory */
401 addr = simple_strtoul(ctx->in_buf + 1, &ptr, 16);
402 if ( (ptr == (ctx->in_buf + 1)) || (ptr[0] != ',') )
403 {
404 gdb_send_reply("E03", ctx);
405 return 0;
406 }
407 length = simple_strtoul(ptr + 1, &ptr, 16);
408 if ( ptr[0] != ':')
409 {
410 gdb_send_reply("E04", ctx);
411 return 0;
412 }
413 gdb_cmd_write_mem(addr, length, ptr + 1, ctx);
414 break;
415 case 'p': /* read register */
416 addr = simple_strtoul(ctx->in_buf + 1, &ptr, 16);
417 if ( ptr == (ctx->in_buf + 1) )
418 {
419 gdb_send_reply("E03", ctx);
420 return 0;
421 }
422 if ( ptr[0] != 0 )
423 {
424 gdb_send_reply("E04", ctx);
425 return 0;
426 }
427 gdb_arch_read_reg(addr, regs, ctx);
428 break;
429 case 'D':
430 ctx->currently_attached = 0;
431 gdb_send_reply("OK", ctx);
432 /* fall through */
433 case 'k':
434 ctx->connected = 0;
435 /* fall through */
436 case 's': /* Single step */
437 case 'c': /* Resume at current address */
438 {
439 unsigned long addr = ~((unsigned long)0);
440 unsigned long type = GDB_CONTINUE;
441 if ( ctx->in_buf[0] == 's' )
442 type = GDB_STEP;
443 if ( ((ctx->in_buf[0] == 's') || (ctx->in_buf[0] == 'c')) &&
444 ctx->in_buf[1] )
445 addr = str2ulong(&ctx->in_buf[1], sizeof(unsigned long));
446 if ( ctx->in_buf[0] != 'D' )
447 ctx->currently_attached = 1;
448 resume = 1;
449 gdb_arch_resume(regs, addr, type, ctx);
450 break;
451 }
453 default:
454 gdb_send_reply("", ctx);
455 break;
456 }
457 return resume;
458 }
460 static struct gdb_context
461 __gdb_ctx = {
462 .serhnd = -1,
463 .currently_attached = 0,
464 .running = ATOMIC_INIT(1),
465 .connected = 0,
466 .signum = 1,
467 .in_bytes = 0,
468 .out_offset = 0,
469 .out_csum = 0,
470 };
471 static struct gdb_context *gdb_ctx = &__gdb_ctx;
473 /* trap handler: main entry point */
474 int
475 __trap_to_gdb(struct cpu_user_regs *regs, unsigned long cookie)
476 {
477 int resume = 0;
478 int r;
479 unsigned long flags;
481 if ( gdb_ctx->serhnd < 0 )
482 {
483 dbg_printk("Debugger not ready yet.\n");
484 return 0;
485 }
487 /* We rely on our caller to ensure we're only on one processor
488 * at a time... We should probably panic here, but given that
489 * we're a debugger we should probably be a little tolerant of
490 * things going wrong. */
491 /* We don't want to use a spin lock here, because we're doing
492 two distinct things:
494 1 -- we don't want to run on more than one processor at a time,
495 and
496 2 -- we want to do something sensible if we re-enter ourselves.
498 Spin locks are good for 1, but useless for 2. */
499 if ( !atomic_dec_and_test(&gdb_ctx->running) )
500 {
501 printk("WARNING WARNING WARNING: Avoiding recursive gdb.\n");
502 atomic_inc(&gdb_ctx->running);
503 return 0;
504 }
506 if ( !gdb_ctx->connected )
507 {
508 printk("GDB connection activated.\n");
509 gdb_arch_print_state(regs);
510 gdb_ctx->connected = 1;
511 }
513 smp_send_stop();
515 /* Try to make things a little more stable by disabling
516 interrupts while we're here. */
517 local_irq_save(flags);
519 watchdog_disable();
520 console_start_sync();
522 /* Shouldn't really do this, but otherwise we stop for no
523 obvious reason, which is Bad */
524 printk("Waiting for GDB to attach...\n");
526 gdb_arch_enter(regs);
527 gdb_ctx->signum = gdb_arch_signal_num(regs, cookie);
528 /* If gdb is already attached, tell it we've stopped again. */
529 if ( gdb_ctx->currently_attached )
530 {
531 gdb_start_packet(gdb_ctx);
532 gdb_cmd_signum(gdb_ctx);
533 }
535 while ( resume == 0 )
536 {
537 r = receive_command(gdb_ctx);
538 if ( r < 0 )
539 {
540 dbg_printk("GDB disappeared, trying to resume Xen...\n");
541 resume = 1;
542 }
543 else
544 {
545 resume = process_command(regs, gdb_ctx);
546 }
547 }
549 gdb_arch_exit(regs);
550 console_end_sync();
551 watchdog_enable();
552 atomic_inc(&gdb_ctx->running);
554 local_irq_restore(flags);
556 return 0;
557 }
559 void
560 initialise_gdb(void)
561 {
562 gdb_ctx->serhnd = serial_parse_handle(opt_gdb);
563 if ( gdb_ctx->serhnd != -1 )
564 printk("GDB stub initialised.\n");
565 serial_start_sync(gdb_ctx->serhnd);
566 }
568 /*
569 * Local variables:
570 * mode: C
571 * c-set-style: "BSD"
572 * c-basic-offset: 4
573 * tab-width: 4
574 * End:
575 */