ia64/xen-unstable

view xen/common/gdbstub.c @ 19835:edfdeb150f27

Fix buildsystem to detect udev > version 124

udev removed the udevinfo symlink from versions higher than 123 and
xen's build-system could not detect if udev is in place and has the
required version.

Signed-off-by: Marc-A. Dahlhaus <mad@wol.de>
author Keir Fraser <keir.fraser@citrix.com>
date Thu Jun 25 13:02:37 2009 +0100 (2009-06-25)
parents 7dfc0a20fa59
children
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>
45 #include <xen/errno.h>
46 #include <xen/delay.h>
47 #include <asm/byteorder.h>
49 /* Printk isn't particularly safe just after we've trapped to the
50 debugger. so avoid it. */
51 #define dbg_printk(...)
52 /*#define dbg_printk(...) printk(__VA_ARGS__)*/
54 #define GDB_RETRY_MAX 10
56 struct gdb_cpu_info
57 {
58 atomic_t paused;
59 atomic_t ack;
60 };
62 static struct gdb_cpu_info gdb_cpu[NR_CPUS];
63 static atomic_t gdb_smp_paused_count;
65 static void gdb_smp_pause(void);
66 static void gdb_smp_resume(void);
68 static char opt_gdb[30];
69 string_param("gdb", opt_gdb);
71 static void gdbstub_console_puts(const char *str);
73 /* value <-> char (de)serialzers */
74 char
75 hex2char(unsigned long x)
76 {
77 const char array[] = "0123456789abcdef";
79 return array[x & 15];
80 }
82 int
83 char2hex(unsigned char c)
84 {
85 if ( (c >= '0') && (c <= '9') )
86 return c - '0';
87 else if ( (c >= 'a') && (c <= 'f') )
88 return c - 'a' + 10;
89 else if ( (c >= 'A') && (c <= 'F') )
90 return c - 'A' + 10;
91 else
92 BUG();
93 return -1;
94 }
96 char
97 str2hex(const char *str)
98 {
99 return (char2hex(str[0]) << 4) | char2hex(str[1]);
100 }
102 unsigned long
103 str2ulong(const char *str, unsigned long bytes)
104 {
105 unsigned long x = 0;
106 unsigned long i = 0;
108 while ( *str && (i < (bytes * 2)) )
109 {
110 x <<= 4;
111 x += char2hex(*str);
112 ++str;
113 ++i;
114 }
116 return x;
117 }
119 /* gdb io wrappers */
120 static signed long
121 gdb_io_write(const char *buf, unsigned long len, struct gdb_context *ctx)
122 {
123 int i;
124 for ( i = 0; i < len; i++ )
125 serial_putc(ctx->serhnd, buf[i]);
126 return i;
127 }
129 static int
130 gdb_io_write_char(u8 data, struct gdb_context *ctx)
131 {
132 return gdb_io_write((char*)&data, 1, ctx);
133 }
135 static unsigned char
136 gdb_io_read(struct gdb_context *ctx)
137 {
138 return serial_getc(ctx->serhnd);
139 }
141 /* Receive a command. Returns -1 on csum error, 0 otherwise. */
142 /* Does not acknowledge. */
143 static int
144 attempt_receive_packet(struct gdb_context *ctx)
145 {
146 u8 csum;
147 u8 received_csum;
148 u8 ch;
150 /* Skip over everything up to the first '$' */
151 while ( (ch = gdb_io_read(ctx)) != '$' )
152 continue;
154 csum = 0;
155 for ( ctx->in_bytes = 0;
156 ctx->in_bytes < sizeof(ctx->in_buf);
157 ctx->in_bytes++ )
158 {
159 ch = gdb_io_read(ctx);
160 if ( ch == '#' )
161 break;
162 ctx->in_buf[ctx->in_bytes] = ch;
163 csum += ch;
164 }
166 if ( ctx->in_bytes == sizeof(ctx->in_buf) )
167 {
168 dbg_printk("WARNING: GDB sent a stupidly big packet.\n");
169 return -1;
170 }
172 ctx->in_buf[ctx->in_bytes] = '\0';
173 received_csum = char2hex(gdb_io_read(ctx)) * 16 +
174 char2hex(gdb_io_read(ctx));
176 return (received_csum == csum) ? 0 : -1;
177 }
179 /* Receive a command, discarding up to ten packets with csum
180 * errors. Acknowledges all received packets. */
181 static int
182 receive_command(struct gdb_context *ctx)
183 {
184 int r, count = 0;
186 count = 0;
187 do {
188 r = attempt_receive_packet(ctx);
189 gdb_io_write_char((r < 0) ? '-' : '+', ctx);
190 count++;
191 } while ( (r < 0) && (count < GDB_RETRY_MAX) );
193 return r;
194 }
196 /* routines to send reply packets */
198 static void
199 gdb_start_packet(struct gdb_context *ctx)
200 {
201 ctx->out_buf[0] = '$';
202 ctx->out_offset = 1;
203 ctx->out_csum = 0;
204 }
206 static void
207 gdb_write_to_packet_char(u8 data, struct gdb_context *ctx)
208 {
209 ctx->out_csum += data;
210 ctx->out_buf[ctx->out_offset] = data;
211 ctx->out_offset++;
212 }
214 void
215 gdb_write_to_packet(const char *buf, int count, struct gdb_context *ctx)
216 {
217 int x;
218 for ( x = 0; x < count; x++ )
219 gdb_write_to_packet_char(buf[x], ctx);
220 }
222 void
223 gdb_write_to_packet_str(const char *buf, struct gdb_context *ctx)
224 {
225 gdb_write_to_packet(buf, strlen(buf), ctx);
226 }
228 void
229 gdb_write_to_packet_hex(unsigned long x, int int_size, struct gdb_context *ctx)
230 {
231 char buf[sizeof(unsigned long) * 2 + 1];
232 int i, width = int_size * 2;
234 buf[sizeof(unsigned long) * 2] = 0;
236 switch ( int_size )
237 {
238 case sizeof(u8):
239 case sizeof(u16):
240 case sizeof(u32):
241 case sizeof(u64):
242 break;
243 default:
244 dbg_printk("WARNING: %s x: 0x%lx int_size: %d\n",
245 __func__, x, int_size);
246 break;
247 }
249 #ifdef __BIG_ENDIAN
250 i = sizeof(unsigned long) * 2
251 do {
252 buf[--i] = hex2char(x & 15);
253 x >>= 4;
254 } while ( x );
256 while ( (i + width) > (sizeof(unsigned long) * 2) )
257 buf[--i] = '0';
259 gdb_write_to_packet(&buf[i], width, ctx);
260 #elif defined(__LITTLE_ENDIAN)
261 i = 0;
262 while ( i < width )
263 {
264 buf[i++] = hex2char(x>>4);
265 buf[i++] = hex2char(x);
266 x >>= 8;
267 }
268 gdb_write_to_packet(buf, width, ctx);
269 #else
270 # error unknown endian
271 #endif
272 }
274 static int
275 gdb_check_ack(struct gdb_context *ctx)
276 {
277 u8 c = gdb_io_read(ctx);
279 switch ( c )
280 {
281 case '+':
282 return 1;
283 case '-':
284 return 0;
285 default:
286 printk("Bad ack: %c\n", c);
287 return 0;
288 }
289 }
291 /* Return 0 if the reply was successfully received, !0 otherwise. */
292 void
293 gdb_send_packet(struct gdb_context *ctx)
294 {
295 char buf[3];
296 int count;
298 snprintf(buf, sizeof(buf), "%.02x\n", ctx->out_csum);
300 gdb_write_to_packet_char('#', ctx);
301 gdb_write_to_packet(buf, 2, ctx);
303 count = 0;
304 do {
305 gdb_io_write(ctx->out_buf, ctx->out_offset, ctx);
306 } while ( !gdb_check_ack(ctx) && (count++ < GDB_RETRY_MAX) );
308 if ( count == GDB_RETRY_MAX )
309 dbg_printk("WARNING: %s reached max retry %d\n",
310 __func__, GDB_RETRY_MAX);
311 }
313 void
314 gdb_send_reply(const char *buf, struct gdb_context *ctx)
315 {
316 gdb_start_packet(ctx);
317 gdb_write_to_packet_str(buf, ctx);
318 gdb_send_packet(ctx);
319 }
321 /* arch neutral command handlers */
323 static void
324 gdb_cmd_signum(struct gdb_context *ctx)
325 {
326 gdb_write_to_packet_char('S', ctx);
327 gdb_write_to_packet_hex(ctx->signum, sizeof(ctx->signum), ctx);
328 gdb_send_packet(ctx);
329 }
331 static void
332 gdb_cmd_read_mem(unsigned long addr, unsigned long length,
333 struct gdb_context *ctx)
334 {
335 int x, r;
336 unsigned char val;
338 dbg_printk("Memory read starting at %lx, length %lx.\n", addr,
339 length);
341 for ( x = 0; x < length; x++ )
342 {
343 r = gdb_arch_copy_from_user(&val, (void *)(addr + x), 1);
344 if ( r != 0 )
345 {
346 dbg_printk("Error reading from %lx.\n", addr + x);
347 break;
348 }
349 gdb_write_to_packet_hex(val, sizeof(val), ctx);
350 }
352 if ( x == 0 )
353 gdb_write_to_packet_str("E05", ctx);
355 dbg_printk("Read done.\n");
357 gdb_send_packet(ctx);
358 }
360 static void
361 gdb_cmd_write_mem(unsigned long addr, unsigned long length,
362 const char *buf, struct gdb_context *ctx)
363 {
364 int x, r;
365 unsigned char val;
367 dbg_printk("Memory write starting at %lx, length %lx.\n", addr, length);
369 for ( x = 0; x < length; x++, addr++, buf += 2 )
370 {
371 val = str2ulong(buf, sizeof(val));
372 r = gdb_arch_copy_to_user((void*)addr, (void*)&val, 1);
373 if ( r != 0 )
374 {
375 dbg_printk("Error writing to %lx.\n", addr);
376 break;
377 }
378 }
380 if (x == length)
381 gdb_write_to_packet_str("OK", ctx);
382 else
383 gdb_write_to_packet_str("E11", ctx);
385 dbg_printk("Write done.\n");
387 gdb_send_packet(ctx);
388 }
390 static void
391 gdbstub_attach(struct gdb_context *ctx)
392 {
393 if ( ctx->currently_attached )
394 return;
395 ctx->currently_attached = 1;
396 ctx->console_steal_id = console_steal(ctx->serhnd, gdbstub_console_puts);
397 }
399 static void
400 gdbstub_detach(struct gdb_context *ctx)
401 {
402 if ( !ctx->currently_attached )
403 return;
404 ctx->currently_attached = 0;
405 console_giveback(ctx->console_steal_id);
406 }
408 /* command dispatcher */
409 static int
410 process_command(struct cpu_user_regs *regs, struct gdb_context *ctx)
411 {
412 const char *ptr;
413 unsigned long addr, length, val;
414 int resume = 0;
415 unsigned long type = GDB_CONTINUE;
417 /* XXX check ctx->in_bytes >= 2 or similar. */
419 gdb_start_packet(ctx);
420 switch ( ctx->in_buf[0] )
421 {
422 case '?': /* query signal number */
423 gdb_cmd_signum(ctx);
424 break;
425 case 'H': /* thread operations */
426 gdb_send_reply("OK", ctx);
427 break;
428 case 'g': /* Read registers */
429 gdb_arch_read_reg_array(regs, ctx);
430 break;
431 case 'G': /* Write registers */
432 gdb_arch_write_reg_array(regs, ctx->in_buf + 1, ctx);
433 break;
434 case 'm': /* Read memory */
435 addr = simple_strtoul(ctx->in_buf + 1, &ptr, 16);
436 if ( (ptr == (ctx->in_buf + 1)) || (ptr[0] != ',') )
437 {
438 gdb_send_reply("E03", ctx);
439 return 0;
440 }
441 length = simple_strtoul(ptr + 1, &ptr, 16);
442 if ( ptr[0] != 0 )
443 {
444 gdb_send_reply("E04", ctx);
445 return 0;
446 }
447 gdb_cmd_read_mem(addr, length, ctx);
448 break;
449 case 'M': /* Write memory */
450 addr = simple_strtoul(ctx->in_buf + 1, &ptr, 16);
451 if ( (ptr == (ctx->in_buf + 1)) || (ptr[0] != ',') )
452 {
453 gdb_send_reply("E03", ctx);
454 return 0;
455 }
456 length = simple_strtoul(ptr + 1, &ptr, 16);
457 if ( ptr[0] != ':')
458 {
459 gdb_send_reply("E04", ctx);
460 return 0;
461 }
462 gdb_cmd_write_mem(addr, length, ptr + 1, ctx);
463 break;
464 case 'p': /* read register */
465 addr = simple_strtoul(ctx->in_buf + 1, &ptr, 16);
466 if ( ptr == (ctx->in_buf + 1) )
467 {
468 gdb_send_reply("E03", ctx);
469 return 0;
470 }
471 if ( ptr[0] != 0 )
472 {
473 gdb_send_reply("E04", ctx);
474 return 0;
475 }
476 gdb_arch_read_reg(addr, regs, ctx);
477 break;
478 case 'P': /* write register */
479 addr = simple_strtoul(ctx->in_buf + 1, &ptr, 16);
480 if ( ptr == (ctx->in_buf + 1) )
481 {
482 gdb_send_reply("E03", ctx);
483 return 0;
484 }
485 if ( ptr[0] != '=' )
486 {
487 gdb_send_reply("E04", ctx);
488 return 0;
489 }
490 ptr++;
491 val = str2ulong(ptr, sizeof(unsigned long));
492 gdb_arch_write_reg(addr, val, regs, ctx);
493 break;
494 case 'D':
495 case 'k':
496 gdbstub_detach(ctx);
497 gdb_send_reply("OK", ctx);
498 ctx->connected = 0;
499 resume = 1;
500 break;
501 case 's': /* Single step */
502 type = GDB_STEP;
503 case 'c': /* Resume at current address */
504 addr = ~((unsigned long)0);
506 if ( ctx->in_buf[1] )
507 addr = str2ulong(&ctx->in_buf[1], sizeof(unsigned long));
508 gdbstub_attach(ctx);
509 resume = 1;
510 gdb_arch_resume(regs, addr, type, ctx);
511 break;
512 default:
513 gdb_send_reply("", ctx);
514 break;
515 }
516 return resume;
517 }
519 static struct gdb_context
520 __gdb_ctx = {
521 .serhnd = -1,
522 .running = ATOMIC_INIT(1),
523 .signum = 1
524 };
525 static struct gdb_context *gdb_ctx = &__gdb_ctx;
527 static void
528 gdbstub_console_puts(const char *str)
529 {
530 const char *p;
532 gdb_start_packet(gdb_ctx);
533 gdb_write_to_packet_char('O', gdb_ctx);
535 for ( p = str; *p != '\0'; p++ )
536 {
537 gdb_write_to_packet_char(hex2char((*p>>4) & 0x0f), gdb_ctx );
538 gdb_write_to_packet_char(hex2char((*p) & 0x0f), gdb_ctx );
539 }
541 gdb_send_packet(gdb_ctx);
542 }
544 /* trap handler: main entry point */
545 int
546 __trap_to_gdb(struct cpu_user_regs *regs, unsigned long cookie)
547 {
548 int rc = 0;
549 unsigned long flags;
551 if ( gdb_ctx->serhnd < 0 )
552 {
553 printk("Debugging connection not set up.\n");
554 return -EBUSY;
555 }
557 /* We rely on our caller to ensure we're only on one processor
558 * at a time... We should probably panic here, but given that
559 * we're a debugger we should probably be a little tolerant of
560 * things going wrong. */
561 /* We don't want to use a spin lock here, because we're doing
562 two distinct things:
564 1 -- we don't want to run on more than one processor at a time,
565 and
566 2 -- we want to do something sensible if we re-enter ourselves.
568 Spin locks are good for 1, but useless for 2. */
569 if ( !atomic_dec_and_test(&gdb_ctx->running) )
570 {
571 printk("WARNING WARNING WARNING: Avoiding recursive gdb.\n");
572 atomic_inc(&gdb_ctx->running);
573 return -EBUSY;
574 }
576 if ( !gdb_ctx->connected )
577 {
578 printk("GDB connection activated.\n");
579 gdb_arch_print_state(regs);
580 gdb_ctx->connected = 1;
581 }
583 gdb_smp_pause();
585 local_irq_save(flags);
587 watchdog_disable();
588 console_start_sync();
590 /* Shouldn't really do this, but otherwise we stop for no
591 obvious reason, which is Bad */
592 printk("Waiting for GDB to attach...\n");
594 gdb_arch_enter(regs);
595 gdb_ctx->signum = gdb_arch_signal_num(regs, cookie);
597 /* If gdb is already attached, tell it we've stopped again. */
598 if ( gdb_ctx->currently_attached )
599 {
600 gdb_start_packet(gdb_ctx);
601 gdb_cmd_signum(gdb_ctx);
602 }
604 do {
605 if ( receive_command(gdb_ctx) < 0 )
606 {
607 dbg_printk("Error in GDB session...\n");
608 rc = -EIO;
609 break;
610 }
611 } while ( process_command(regs, gdb_ctx) == 0 );
613 gdb_smp_resume();
615 gdb_arch_exit(regs);
616 console_end_sync();
617 watchdog_enable();
618 atomic_inc(&gdb_ctx->running);
620 local_irq_restore(flags);
622 return rc;
623 }
625 void __init
626 initialise_gdb(void)
627 {
628 if ( *opt_gdb == '\0' )
629 return;
631 gdb_ctx->serhnd = serial_parse_handle(opt_gdb);
632 if ( gdb_ctx->serhnd == -1 )
633 {
634 printk("Bad gdb= option '%s'\n", opt_gdb);
635 return;
636 }
638 serial_start_sync(gdb_ctx->serhnd);
640 printk("GDB stub initialised.\n");
641 }
643 static void gdb_pause_this_cpu(void *unused)
644 {
645 unsigned long flags;
647 local_irq_save(flags);
649 atomic_set(&gdb_cpu[smp_processor_id()].ack, 1);
650 atomic_inc(&gdb_smp_paused_count);
652 while ( atomic_read(&gdb_cpu[smp_processor_id()].paused) )
653 mdelay(1);
655 atomic_dec(&gdb_smp_paused_count);
656 atomic_set(&gdb_cpu[smp_processor_id()].ack, 0);
658 /* Restore interrupts */
659 local_irq_restore(flags);
660 }
662 static void gdb_smp_pause(void)
663 {
664 int timeout = 100;
665 int cpu;
667 for_each_online_cpu(cpu)
668 {
669 atomic_set(&gdb_cpu[cpu].ack, 0);
670 atomic_set(&gdb_cpu[cpu].paused, 1);
671 }
673 atomic_set(&gdb_smp_paused_count, 0);
675 smp_call_function(gdb_pause_this_cpu, NULL, /* dont wait! */0);
677 /* Wait 100ms for all other CPUs to enter pause loop */
678 while ( (atomic_read(&gdb_smp_paused_count) < (num_online_cpus() - 1))
679 && (timeout-- > 0) )
680 mdelay(1);
682 if ( atomic_read(&gdb_smp_paused_count) < (num_online_cpus() - 1) )
683 {
684 printk("GDB: Not all CPUs have paused, missing CPUs ");
685 for_each_online_cpu(cpu)
686 {
687 if ( (cpu != smp_processor_id()) &&
688 !atomic_read(&gdb_cpu[cpu].ack) )
689 printk("%d ", cpu);
690 }
691 printk("\n");
692 }
693 }
695 static void gdb_smp_resume(void)
696 {
697 int cpu;
698 int timeout = 100;
700 for_each_online_cpu(cpu)
701 atomic_set(&gdb_cpu[cpu].paused, 0);
703 /* Make sure all CPUs resume */
704 while ( (atomic_read(&gdb_smp_paused_count) > 0)
705 && (timeout-- > 0) )
706 mdelay(1);
708 if ( atomic_read(&gdb_smp_paused_count) > 0 )
709 {
710 printk("GDB: Not all CPUs have resumed execution, missing CPUs ");
711 for_each_online_cpu(cpu)
712 {
713 if ( (cpu != smp_processor_id()) &&
714 atomic_read(&gdb_cpu[cpu].ack) )
715 printk("%d ", cpu);
716 }
717 printk("\n");
718 }
719 }
721 /*
722 * Local variables:
723 * mode: C
724 * c-set-style: "BSD"
725 * c-basic-offset: 4
726 * tab-width: 4
727 * End:
728 */