ia64/xen-unstable

view xen/common/gdbstub.c @ 8609:85d693e6f61a

Arch-specific per-vcpu info should be initialised to zero
when allocating a new vcpu structure, not copied from
CPU0's idle VCPU. Especially now that the idle VCPU itself
is dynamically allocated.

This should fix assertions people have been seeing in
getdomain_info_ctxt() relation to IOPL in eflags.

Signed-off-by: Keir Fraser <keir@xensource.com>
author kaf24@firebug.cl.cam.ac.uk
date Sat Jan 14 21:26:40 2006 +0100 (2006-01-14)
parents 1b839e1b1de1
children ec7802acc8c9
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 <asm/uaccess.h>
39 #include <xen/spinlock.h>
40 #include <xen/serial.h>
41 #include <xen/irq.h>
42 #include <asm/debugger.h>
43 #include <xen/init.h>
44 #include <xen/smp.h>
45 #include <xen/console.h>
47 /* Printk isn't particularly safe just after we've trapped to the
48 debugger. so avoid it. */
49 #define dbg_printk(...)
50 /*#define dbg_printk(...) printk(__VA_ARGS__)*/
52 #define GDB_RETRY_MAX 10
54 static char opt_gdb[30] = "none";
55 string_param("gdb", opt_gdb);
57 /* value <-> char (de)serialzers */
58 char
59 hex2char(unsigned long x)
60 {
61 const char array[] = "0123456789abcdef";
63 return array[x & 15];
64 }
66 int
67 char2hex(unsigned char c)
68 {
69 if ( (c >= '0') && (c <= '9') )
70 return c - '0';
71 else if ( (c >= 'a') && (c <= 'f') )
72 return c - 'a' + 10;
73 else if ( (c >= 'A') && (c <= 'F') )
74 return c - 'A' + 10;
75 else
76 BUG();
77 return -1;
78 }
80 char
81 str2hex(const char *str)
82 {
83 return (char2hex(str[0]) << 4) | char2hex(str[1]);
84 }
86 unsigned long
87 str2ulong(const char *str, unsigned long bytes)
88 {
89 unsigned long x = 0;
90 unsigned long i = 0;
92 while ( *str && (i < (bytes * 2)) )
93 {
94 x <<= 4;
95 x += char2hex(*str);
96 ++str;
97 ++i;
98 }
100 return x;
101 }
103 /* gdb io wrappers */
104 static signed long
105 gdb_io_write(const char *buf, unsigned long len, struct gdb_context *ctx)
106 {
107 int i;
108 for ( i = 0; i < len; i++ )
109 serial_putc(ctx->serhnd, buf[i]);
110 return i;
111 }
113 static int
114 gdb_io_write_char(u8 data, struct gdb_context *ctx)
115 {
116 return gdb_io_write((char*)&data, 1, ctx);
117 }
119 static unsigned char
120 gdb_io_read(struct gdb_context *ctx)
121 {
122 return serial_getc(ctx->serhnd);
123 }
125 /* Receive a command. Returns -1 on csum error, 0 otherwise. */
126 /* Does not acknowledge. */
127 static int
128 attempt_receive_packet(struct gdb_context *ctx)
129 {
130 u8 csum;
131 u8 received_csum;
132 u8 ch;
134 /* Skip over everything up to the first '$' */
135 while ( (ch = gdb_io_read(ctx)) != '$' )
136 continue;
138 csum = 0;
139 for ( ctx->in_bytes = 0;
140 ctx->in_bytes < sizeof(ctx->in_buf);
141 ctx->in_bytes++ )
142 {
143 ch = gdb_io_read(ctx);
144 if ( ch == '#' )
145 break;
146 ctx->in_buf[ctx->in_bytes] = ch;
147 csum += ch;
148 }
150 if ( ctx->in_bytes == sizeof(ctx->in_buf) )
151 {
152 dbg_printk("WARNING: GDB sent a stupidly big packet.\n");
153 return -1;
154 }
156 ctx->in_buf[ctx->in_bytes] = '\0';
157 received_csum = char2hex(gdb_io_read(ctx)) * 16 +
158 char2hex(gdb_io_read(ctx));
160 return (received_csum == csum) ? 0 : -1;
161 }
163 /* Receive a command, discarding up to ten packets with csum
164 * errors. Acknowledges all received packets. */
165 static int
166 receive_command(struct gdb_context *ctx)
167 {
168 int r, count = 0;
170 count = 0;
171 do {
172 r = attempt_receive_packet(ctx);
173 gdb_io_write_char((r < 0) ? '-' : '+', ctx);
174 count++;
175 } while ( (r < 0) && (count < GDB_RETRY_MAX) );
177 return r;
178 }
180 /* routines to send reply packets */
182 static void
183 gdb_start_packet(struct gdb_context *ctx)
184 {
185 ctx->out_buf[0] = '$';
186 ctx->out_offset = 1;
187 ctx->out_csum = 0;
188 }
190 static void
191 gdb_write_to_packet_char(u8 data, struct gdb_context *ctx)
192 {
193 ctx->out_csum += data;
194 ctx->out_buf[ctx->out_offset] = data;
195 ctx->out_offset++;
196 }
198 void
199 gdb_write_to_packet(const char *buf, int count, struct gdb_context *ctx)
200 {
201 int x;
202 for ( x = 0; x < count; x++ )
203 gdb_write_to_packet_char(buf[x], ctx);
204 }
206 void
207 gdb_write_to_packet_str(const char *buf, struct gdb_context *ctx)
208 {
209 gdb_write_to_packet(buf, strlen(buf), ctx);
210 }
212 void
213 gdb_write_to_packet_hex(unsigned long x, int int_size, struct gdb_context *ctx)
214 {
215 char buf[sizeof(unsigned long) * 2 + 1];
216 int i = sizeof(unsigned long) * 2;
217 int width = int_size * 2;
219 buf[sizeof(unsigned long) * 2] = 0;
221 switch ( int_size )
222 {
223 case sizeof(u8):
224 case sizeof(u16):
225 case sizeof(u32):
226 case sizeof(u64):
227 break;
228 default:
229 dbg_printk("WARNING: %s x: 0x%lx int_size: %d\n",
230 __func__, x, int_size);
231 break;
232 }
234 do {
235 buf[--i] = hex2char(x & 15);
236 x >>= 4;
237 } while ( x );
239 while ( (i + width) > (sizeof(unsigned long) * 2) )
240 buf[--i] = '0';
242 gdb_write_to_packet(&buf[i], width, ctx);
243 }
245 static int
246 gdb_check_ack(struct gdb_context *ctx)
247 {
248 u8 c = gdb_io_read(ctx);
250 switch ( c )
251 {
252 case '+':
253 return 1;
254 case '-':
255 return 0;
256 default:
257 printk("Bad ack: %c\n", c);
258 return 0;
259 }
260 }
262 /* Return 0 if the reply was successfully received, !0 otherwise. */
263 void
264 gdb_send_packet(struct gdb_context *ctx)
265 {
266 char buf[3];
267 int count;
269 sprintf(buf, "%.02x\n", ctx->out_csum);
271 gdb_write_to_packet_char('#', ctx);
272 gdb_write_to_packet(buf, 2, ctx);
274 count = 0;
275 do {
276 gdb_io_write(ctx->out_buf, ctx->out_offset, ctx);
277 } while ( !gdb_check_ack(ctx) && (count++ < GDB_RETRY_MAX) );
279 if ( count == GDB_RETRY_MAX )
280 dbg_printk("WARNING: %s reached max retry %d\n",
281 __func__, GDB_RETRY_MAX);
282 }
284 void
285 gdb_send_reply(const char *buf, struct gdb_context *ctx)
286 {
287 gdb_start_packet(ctx);
288 gdb_write_to_packet_str(buf, ctx);
289 gdb_send_packet(ctx);
290 }
292 /* arch neutral command handlers */
294 static void
295 gdb_cmd_signum(struct gdb_context *ctx)
296 {
297 gdb_write_to_packet_char('S', ctx);
298 gdb_write_to_packet_hex(ctx->signum, sizeof(ctx->signum), ctx);
299 gdb_send_packet(ctx);
300 }
302 static void
303 gdb_cmd_read_mem(unsigned long addr, unsigned long length,
304 struct gdb_context *ctx)
305 {
306 int x, r;
307 unsigned char val;
309 dbg_printk("Memory read starting at %lx, length %lx.\n", addr,
310 length);
312 for ( x = 0; x < length; x++ )
313 {
314 r = gdb_arch_copy_from_user(&val, (void *)(addr + x), 1);
315 if ( r != 0 )
316 {
317 dbg_printk("Error reading from %lx.\n", addr + x);
318 break;
319 }
320 gdb_write_to_packet_hex(val, sizeof(val), ctx);
321 }
323 if ( x == 0 )
324 gdb_write_to_packet_str("E05", ctx);
326 dbg_printk("Read done.\n");
328 gdb_send_packet(ctx);
329 }
331 static void
332 gdb_cmd_write_mem(unsigned long addr, unsigned long length,
333 const char *buf, struct gdb_context *ctx)
334 {
335 int x, r;
336 unsigned char val;
338 dbg_printk("Memory write starting at %lx, length %lx.\n", addr, length);
340 for ( x = 0; x < length; x++, addr++, buf += 2 )
341 {
342 val = str2ulong(buf, sizeof(val));
343 r = gdb_arch_copy_to_user((void*)addr, (void*)&val, 1);
344 if ( r != 0 )
345 {
346 dbg_printk("Error writing to %lx.\n", addr);
347 break;
348 }
349 }
351 gdb_write_to_packet_str((x != length) ? "OK" : "E11", ctx);
353 dbg_printk("Write done.\n");
355 gdb_send_packet(ctx);
356 }
358 /* command dispatcher */
359 static int
360 process_command(struct cpu_user_regs *regs, struct gdb_context *ctx)
361 {
362 char *ptr;
363 unsigned long addr, length;
364 int resume = 0;
366 /* XXX check ctx->in_bytes >= 2 or similar. */
368 gdb_start_packet(ctx);
369 switch ( ctx->in_buf[0] )
370 {
371 case '?': /* query signal number */
372 gdb_cmd_signum(ctx);
373 break;
374 case 'H': /* thread operations */
375 gdb_send_reply("OK", ctx);
376 break;
377 case 'g': /* Read registers */
378 gdb_arch_read_reg_array(regs, ctx);
379 ASSERT(!local_irq_is_enabled());
380 break;
381 case 'G': /* Write registers */
382 gdb_arch_write_reg_array(regs, ctx->in_buf + 1, ctx);
383 break;
384 case 'm': /* Read memory */
385 addr = simple_strtoul(ctx->in_buf + 1, &ptr, 16);
386 if ( (ptr == (ctx->in_buf + 1)) || (ptr[0] != ',') )
387 {
388 gdb_send_reply("E03", ctx);
389 return 0;
390 }
391 length = simple_strtoul(ptr + 1, &ptr, 16);
392 if ( ptr[0] != 0 )
393 {
394 gdb_send_reply("E04", ctx);
395 return 0;
396 }
397 gdb_cmd_read_mem(addr, length, ctx);
398 ASSERT(!local_irq_is_enabled());
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 gdb_cmd_write_mem(addr, length, ptr, ctx);
409 break;
410 case 'p': /* read register */
411 addr = simple_strtoul(ctx->in_buf + 1, &ptr, 16);
412 if ( ptr == (ctx->in_buf + 1) )
413 {
414 gdb_send_reply("E03", ctx);
415 return 0;
416 }
417 if ( ptr[0] != 0 )
418 {
419 gdb_send_reply("E04", ctx);
420 return 0;
421 }
422 gdb_arch_read_reg(addr, regs, ctx);
423 break;
424 case 'Z': /* We need to claim to support these or gdb
425 won't let you continue the process. */
426 case 'z':
427 gdb_send_reply("OK", ctx);
428 break;
430 case 'D':
431 ctx->currently_attached = 0;
432 gdb_send_reply("OK", ctx);
433 /* fall through */
434 case 'k':
435 ctx->connected = 0;
436 /* fall through */
437 case 's': /* Single step */
438 case 'c': /* Resume at current address */
439 {
440 unsigned long addr = ~((unsigned long)0);
441 unsigned long type = GDB_CONTINUE;
442 if ( ctx->in_buf[0] == 's' )
443 type = GDB_STEP;
444 if ( ((ctx->in_buf[0] == 's') || (ctx->in_buf[0] == 'c')) &&
445 ctx->in_buf[1] )
446 addr = str2ulong(&ctx->in_buf[1], sizeof(unsigned long));
447 if ( ctx->in_buf[0] != 'D' )
448 ctx->currently_attached = 1;
449 resume = 1;
450 gdb_arch_resume(regs, addr, type, ctx);
451 break;
452 }
454 default:
455 gdb_send_reply("", ctx);
456 break;
457 }
458 return resume;
459 }
461 static struct gdb_context
462 __gdb_ctx = {
463 .serhnd = -1,
464 .currently_attached = 0,
465 .running = ATOMIC_INIT(1),
466 .connected = 0,
467 .signum = 1,
468 .in_bytes = 0,
469 .out_offset = 0,
470 .out_csum = 0,
471 };
472 static struct gdb_context *gdb_ctx = &__gdb_ctx;
474 /* trap handler: main entry point */
475 int
476 __trap_to_gdb(struct cpu_user_regs *regs, unsigned long cookie)
477 {
478 int resume = 0;
479 int r;
480 unsigned flags;
482 if ( gdb_ctx->serhnd < 0 )
483 {
484 dbg_printk("Debugger not ready yet.\n");
485 return 0;
486 }
488 /* We rely on our caller to ensure we're only on one processor
489 * at a time... We should probably panic here, but given that
490 * we're a debugger we should probably be a little tolerant of
491 * things going wrong. */
492 /* We don't want to use a spin lock here, because we're doing
493 two distinct things:
495 1 -- we don't want to run on more than one processor at a time,
496 and
497 2 -- we want to do something sensible if we re-enter ourselves.
499 Spin locks are good for 1, but useless for 2. */
500 if ( !atomic_dec_and_test(&gdb_ctx->running) )
501 {
502 printk("WARNING WARNING WARNING: Avoiding recursive gdb.\n");
503 atomic_inc(&gdb_ctx->running);
504 return 0;
505 }
507 if ( !gdb_ctx->connected )
508 {
509 printk("GDB connection activated\n");
510 gdb_arch_print_state(regs);
511 gdb_ctx->connected = 1;
512 }
514 smp_send_stop();
516 /* Try to make things a little more stable by disabling
517 interrupts while we're here. */
518 local_irq_save(flags);
520 watchdog_disable();
521 console_start_sync();
523 /* Shouldn't really do this, but otherwise we stop for no
524 obvious reason, which is Bad */
525 printk("Waiting for GDB to attach to Gdb\n");
527 gdb_arch_enter(regs);
528 gdb_ctx->signum = gdb_arch_signal_num(regs, cookie);
529 /* If gdb is already attached, tell it we've stopped again. */
530 if ( gdb_ctx->currently_attached )
531 {
532 gdb_start_packet(gdb_ctx);
533 gdb_cmd_signum(gdb_ctx);
534 }
536 while ( resume == 0 )
537 {
538 ASSERT(!local_irq_is_enabled());
539 r = receive_command(gdb_ctx);
540 ASSERT(!local_irq_is_enabled());
541 if ( r < 0 )
542 {
543 dbg_printk("GDB disappeared, trying to resume Xen...\n");
544 resume = 1;
545 }
546 else
547 {
548 ASSERT(!local_irq_is_enabled());
549 resume = process_command(regs, gdb_ctx);
550 ASSERT(!local_irq_is_enabled());
551 }
552 }
554 gdb_arch_exit(regs);
555 console_end_sync();
556 watchdog_enable();
557 atomic_inc(&gdb_ctx->running);
559 local_irq_restore(flags);
561 return 0;
562 }
564 /*
565 * initialization
566 * XXX TODO
567 * This should be an explicit call from architecture code.
568 * initcall is far too late for some early debugging, and only the
569 * architecture code knows when this call can be made.
570 */
571 static int
572 initialize_gdb(void)
573 {
574 if ( !strcmp(opt_gdb, "none") )
575 return 0;
576 gdb_ctx->serhnd = serial_parse_handle(opt_gdb);
577 if ( gdb_ctx->serhnd == -1 )
578 panic("Can't parse %s as GDB serial info.\n", opt_gdb);
580 printk("Gdb initialised.\n");
581 return 0;
582 }
584 __initcall(initialize_gdb);
586 /*
587 * Local variables:
588 * mode: C
589 * c-set-style: "BSD"
590 * c-basic-offset: 4
591 * tab-width: 4
592 * End:
593 */