1 /* 2 * Kernel Debug Core 3 * 4 * Maintainer: Jason Wessel <jason.wessel@windriver.com> 5 * 6 * Copyright (C) 2000-2001 VERITAS Software Corporation. 7 * Copyright (C) 2002-2004 Timesys Corporation 8 * Copyright (C) 2003-2004 Amit S. Kale <amitkale@linsyssoft.com> 9 * Copyright (C) 2004 Pavel Machek <pavel@ucw.cz> 10 * Copyright (C) 2004-2006 Tom Rini <trini@kernel.crashing.org> 11 * Copyright (C) 2004-2006 LinSysSoft Technologies Pvt. Ltd. 12 * Copyright (C) 2005-2009 Wind River Systems, Inc. 13 * Copyright (C) 2007 MontaVista Software, Inc. 14 * Copyright (C) 2008 Red Hat, Inc., Ingo Molnar <mingo@redhat.com> 15 * 16 * Contributors at various stages not listed above: 17 * Jason Wessel ( jason.wessel@windriver.com ) 18 * George Anzinger <george@mvista.com> 19 * Anurekh Saxena (anurekh.saxena@timesys.com) 20 * Lake Stevens Instrument Division (Glenn Engel) 21 * Jim Kingdon, Cygnus Support. 22 * 23 * Original KGDB stub: David Grothe <dave@gcom.com>, 24 * Tigran Aivazian <tigran@sco.com> 25 * 26 * This file is licensed under the terms of the GNU General Public License 27 * version 2. This program is licensed "as is" without any warranty of any 28 * kind, whether express or implied. 29 */ 30 31 #include <linux/kernel.h> 32 #include <linux/kgdb.h> 33 #include <linux/kdb.h> 34 #include <linux/reboot.h> 35 #include <linux/uaccess.h> 36 #include <asm/cacheflush.h> 37 #include <asm/unaligned.h> 38 #include "debug_core.h" 39 40 #define KGDB_MAX_THREAD_QUERY 17 41 42 /* Our I/O buffers. */ 43 static char remcom_in_buffer[BUFMAX]; 44 static char remcom_out_buffer[BUFMAX]; 45 46 /* Storage for the registers, in GDB format. */ 47 static unsigned long gdb_regs[(NUMREGBYTES + 48 sizeof(unsigned long) - 1) / 49 sizeof(unsigned long)]; 50 51 /* 52 * GDB remote protocol parser: 53 */ 54 55 #ifdef CONFIG_KGDB_KDB 56 static int gdbstub_read_wait(void) 57 { 58 int ret = -1; 59 int i; 60 61 /* poll any additional I/O interfaces that are defined */ 62 while (ret < 0) 63 for (i = 0; kdb_poll_funcs[i] != NULL; i++) { 64 ret = kdb_poll_funcs[i](); 65 if (ret > 0) 66 break; 67 } 68 return ret; 69 } 70 #else 71 static int gdbstub_read_wait(void) 72 { 73 int ret = dbg_io_ops->read_char(); 74 while (ret == NO_POLL_CHAR) 75 ret = dbg_io_ops->read_char(); 76 return ret; 77 } 78 #endif 79 /* scan for the sequence $<data>#<checksum> */ 80 static void get_packet(char *buffer) 81 { 82 unsigned char checksum; 83 unsigned char xmitcsum; 84 int count; 85 char ch; 86 87 do { 88 /* 89 * Spin and wait around for the start character, ignore all 90 * other characters: 91 */ 92 while ((ch = (gdbstub_read_wait())) != '$') 93 /* nothing */; 94 95 kgdb_connected = 1; 96 checksum = 0; 97 xmitcsum = -1; 98 99 count = 0; 100 101 /* 102 * now, read until a # or end of buffer is found: 103 */ 104 while (count < (BUFMAX - 1)) { 105 ch = gdbstub_read_wait(); 106 if (ch == '#') 107 break; 108 checksum = checksum + ch; 109 buffer[count] = ch; 110 count = count + 1; 111 } 112 buffer[count] = 0; 113 114 if (ch == '#') { 115 xmitcsum = hex_to_bin(gdbstub_read_wait()) << 4; 116 xmitcsum += hex_to_bin(gdbstub_read_wait()); 117 118 if (checksum != xmitcsum) 119 /* failed checksum */ 120 dbg_io_ops->write_char('-'); 121 else 122 /* successful transfer */ 123 dbg_io_ops->write_char('+'); 124 if (dbg_io_ops->flush) 125 dbg_io_ops->flush(); 126 } 127 } while (checksum != xmitcsum); 128 } 129 130 /* 131 * Send the packet in buffer. 132 * Check for gdb connection if asked for. 133 */ 134 static void put_packet(char *buffer) 135 { 136 unsigned char checksum; 137 int count; 138 char ch; 139 140 /* 141 * $<packet info>#<checksum>. 142 */ 143 while (1) { 144 dbg_io_ops->write_char('$'); 145 checksum = 0; 146 count = 0; 147 148 while ((ch = buffer[count])) { 149 dbg_io_ops->write_char(ch); 150 checksum += ch; 151 count++; 152 } 153 154 dbg_io_ops->write_char('#'); 155 dbg_io_ops->write_char(hex_asc_hi(checksum)); 156 dbg_io_ops->write_char(hex_asc_lo(checksum)); 157 if (dbg_io_ops->flush) 158 dbg_io_ops->flush(); 159 160 /* Now see what we get in reply. */ 161 ch = gdbstub_read_wait(); 162 163 if (ch == 3) 164 ch = gdbstub_read_wait(); 165 166 /* If we get an ACK, we are done. */ 167 if (ch == '+') 168 return; 169 170 /* 171 * If we get the start of another packet, this means 172 * that GDB is attempting to reconnect. We will NAK 173 * the packet being sent, and stop trying to send this 174 * packet. 175 */ 176 if (ch == '$') { 177 dbg_io_ops->write_char('-'); 178 if (dbg_io_ops->flush) 179 dbg_io_ops->flush(); 180 return; 181 } 182 } 183 } 184 185 static char gdbmsgbuf[BUFMAX + 1]; 186 187 void gdbstub_msg_write(const char *s, int len) 188 { 189 char *bufptr; 190 int wcount; 191 int i; 192 193 if (len == 0) 194 len = strlen(s); 195 196 /* 'O'utput */ 197 gdbmsgbuf[0] = 'O'; 198 199 /* Fill and send buffers... */ 200 while (len > 0) { 201 bufptr = gdbmsgbuf + 1; 202 203 /* Calculate how many this time */ 204 if ((len << 1) > (BUFMAX - 2)) 205 wcount = (BUFMAX - 2) >> 1; 206 else 207 wcount = len; 208 209 /* Pack in hex chars */ 210 for (i = 0; i < wcount; i++) 211 bufptr = pack_hex_byte(bufptr, s[i]); 212 *bufptr = '\0'; 213 214 /* Move up */ 215 s += wcount; 216 len -= wcount; 217 218 /* Write packet */ 219 put_packet(gdbmsgbuf); 220 } 221 } 222 223 /* 224 * Convert the memory pointed to by mem into hex, placing result in 225 * buf. Return a pointer to the last char put in buf (null). May 226 * return an error. 227 */ 228 char *kgdb_mem2hex(char *mem, char *buf, int count) 229 { 230 char *tmp; 231 int err; 232 233 /* 234 * We use the upper half of buf as an intermediate buffer for the 235 * raw memory copy. Hex conversion will work against this one. 236 */ 237 tmp = buf + count; 238 239 err = probe_kernel_read(tmp, mem, count); 240 if (err) 241 return NULL; 242 while (count > 0) { 243 buf = pack_hex_byte(buf, *tmp); 244 tmp++; 245 count--; 246 } 247 *buf = 0; 248 249 return buf; 250 } 251 252 /* 253 * Convert the hex array pointed to by buf into binary to be placed in 254 * mem. Return a pointer to the character AFTER the last byte 255 * written. May return an error. 256 */ 257 int kgdb_hex2mem(char *buf, char *mem, int count) 258 { 259 char *tmp_raw; 260 char *tmp_hex; 261 262 /* 263 * We use the upper half of buf as an intermediate buffer for the 264 * raw memory that is converted from hex. 265 */ 266 tmp_raw = buf + count * 2; 267 268 tmp_hex = tmp_raw - 1; 269 while (tmp_hex >= buf) { 270 tmp_raw--; 271 *tmp_raw = hex_to_bin(*tmp_hex--); 272 *tmp_raw |= hex_to_bin(*tmp_hex--) << 4; 273 } 274 275 return probe_kernel_write(mem, tmp_raw, count); 276 } 277 278 /* 279 * While we find nice hex chars, build a long_val. 280 * Return number of chars processed. 281 */ 282 int kgdb_hex2long(char **ptr, unsigned long *long_val) 283 { 284 int hex_val; 285 int num = 0; 286 int negate = 0; 287 288 *long_val = 0; 289 290 if (**ptr == '-') { 291 negate = 1; 292 (*ptr)++; 293 } 294 while (**ptr) { 295 hex_val = hex_to_bin(**ptr); 296 if (hex_val < 0) 297 break; 298 299 *long_val = (*long_val << 4) | hex_val; 300 num++; 301 (*ptr)++; 302 } 303 304 if (negate) 305 *long_val = -*long_val; 306 307 return num; 308 } 309 310 /* 311 * Copy the binary array pointed to by buf into mem. Fix $, #, and 312 * 0x7d escaped with 0x7d. Return -EFAULT on failure or 0 on success. 313 * The input buf is overwitten with the result to write to mem. 314 */ 315 static int kgdb_ebin2mem(char *buf, char *mem, int count) 316 { 317 int size = 0; 318 char *c = buf; 319 320 while (count-- > 0) { 321 c[size] = *buf++; 322 if (c[size] == 0x7d) 323 c[size] = *buf++ ^ 0x20; 324 size++; 325 } 326 327 return probe_kernel_write(mem, c, size); 328 } 329 330 #if DBG_MAX_REG_NUM > 0 331 void pt_regs_to_gdb_regs(unsigned long *gdb_regs, struct pt_regs *regs) 332 { 333 int i; 334 int idx = 0; 335 char *ptr = (char *)gdb_regs; 336 337 for (i = 0; i < DBG_MAX_REG_NUM; i++) { 338 dbg_get_reg(i, ptr + idx, regs); 339 idx += dbg_reg_def[i].size; 340 } 341 } 342 343 void gdb_regs_to_pt_regs(unsigned long *gdb_regs, struct pt_regs *regs) 344 { 345 int i; 346 int idx = 0; 347 char *ptr = (char *)gdb_regs; 348 349 for (i = 0; i < DBG_MAX_REG_NUM; i++) { 350 dbg_set_reg(i, ptr + idx, regs); 351 idx += dbg_reg_def[i].size; 352 } 353 } 354 #endif /* DBG_MAX_REG_NUM > 0 */ 355 356 /* Write memory due to an 'M' or 'X' packet. */ 357 static int write_mem_msg(int binary) 358 { 359 char *ptr = &remcom_in_buffer[1]; 360 unsigned long addr; 361 unsigned long length; 362 int err; 363 364 if (kgdb_hex2long(&ptr, &addr) > 0 && *(ptr++) == ',' && 365 kgdb_hex2long(&ptr, &length) > 0 && *(ptr++) == ':') { 366 if (binary) 367 err = kgdb_ebin2mem(ptr, (char *)addr, length); 368 else 369 err = kgdb_hex2mem(ptr, (char *)addr, length); 370 if (err) 371 return err; 372 if (CACHE_FLUSH_IS_SAFE) 373 flush_icache_range(addr, addr + length); 374 return 0; 375 } 376 377 return -EINVAL; 378 } 379 380 static void error_packet(char *pkt, int error) 381 { 382 error = -error; 383 pkt[0] = 'E'; 384 pkt[1] = hex_asc[(error / 10)]; 385 pkt[2] = hex_asc[(error % 10)]; 386 pkt[3] = '\0'; 387 } 388 389 /* 390 * Thread ID accessors. We represent a flat TID space to GDB, where 391 * the per CPU idle threads (which under Linux all have PID 0) are 392 * remapped to negative TIDs. 393 */ 394 395 #define BUF_THREAD_ID_SIZE 8 396 397 static char *pack_threadid(char *pkt, unsigned char *id) 398 { 399 unsigned char *limit; 400 int lzero = 1; 401 402 limit = id + (BUF_THREAD_ID_SIZE / 2); 403 while (id < limit) { 404 if (!lzero || *id != 0) { 405 pkt = pack_hex_byte(pkt, *id); 406 lzero = 0; 407 } 408 id++; 409 } 410 411 if (lzero) 412 pkt = pack_hex_byte(pkt, 0); 413 414 return pkt; 415 } 416 417 static void int_to_threadref(unsigned char *id, int value) 418 { 419 put_unaligned_be32(value, id); 420 } 421 422 static struct task_struct *getthread(struct pt_regs *regs, int tid) 423 { 424 /* 425 * Non-positive TIDs are remapped to the cpu shadow information 426 */ 427 if (tid == 0 || tid == -1) 428 tid = -atomic_read(&kgdb_active) - 2; 429 if (tid < -1 && tid > -NR_CPUS - 2) { 430 if (kgdb_info[-tid - 2].task) 431 return kgdb_info[-tid - 2].task; 432 else 433 return idle_task(-tid - 2); 434 } 435 if (tid <= 0) { 436 printk(KERN_ERR "KGDB: Internal thread select error\n"); 437 dump_stack(); 438 return NULL; 439 } 440 441 /* 442 * find_task_by_pid_ns() does not take the tasklist lock anymore 443 * but is nicely RCU locked - hence is a pretty resilient 444 * thing to use: 445 */ 446 return find_task_by_pid_ns(tid, &init_pid_ns); 447 } 448 449 450 /* 451 * Remap normal tasks to their real PID, 452 * CPU shadow threads are mapped to -CPU - 2 453 */ 454 static inline int shadow_pid(int realpid) 455 { 456 if (realpid) 457 return realpid; 458 459 return -raw_smp_processor_id() - 2; 460 } 461 462 /* 463 * All the functions that start with gdb_cmd are the various 464 * operations to implement the handlers for the gdbserial protocol 465 * where KGDB is communicating with an external debugger 466 */ 467 468 /* Handle the '?' status packets */ 469 static void gdb_cmd_status(struct kgdb_state *ks) 470 { 471 /* 472 * We know that this packet is only sent 473 * during initial connect. So to be safe, 474 * we clear out our breakpoints now in case 475 * GDB is reconnecting. 476 */ 477 dbg_remove_all_break(); 478 479 remcom_out_buffer[0] = 'S'; 480 pack_hex_byte(&remcom_out_buffer[1], ks->signo); 481 } 482 483 static void gdb_get_regs_helper(struct kgdb_state *ks) 484 { 485 struct task_struct *thread; 486 void *local_debuggerinfo; 487 int i; 488 489 thread = kgdb_usethread; 490 if (!thread) { 491 thread = kgdb_info[ks->cpu].task; 492 local_debuggerinfo = kgdb_info[ks->cpu].debuggerinfo; 493 } else { 494 local_debuggerinfo = NULL; 495 for_each_online_cpu(i) { 496 /* 497 * Try to find the task on some other 498 * or possibly this node if we do not 499 * find the matching task then we try 500 * to approximate the results. 501 */ 502 if (thread == kgdb_info[i].task) 503 local_debuggerinfo = kgdb_info[i].debuggerinfo; 504 } 505 } 506 507 /* 508 * All threads that don't have debuggerinfo should be 509 * in schedule() sleeping, since all other CPUs 510 * are in kgdb_wait, and thus have debuggerinfo. 511 */ 512 if (local_debuggerinfo) { 513 pt_regs_to_gdb_regs(gdb_regs, local_debuggerinfo); 514 } else { 515 /* 516 * Pull stuff saved during switch_to; nothing 517 * else is accessible (or even particularly 518 * relevant). 519 * 520 * This should be enough for a stack trace. 521 */ 522 sleeping_thread_to_gdb_regs(gdb_regs, thread); 523 } 524 } 525 526 /* Handle the 'g' get registers request */ 527 static void gdb_cmd_getregs(struct kgdb_state *ks) 528 { 529 gdb_get_regs_helper(ks); 530 kgdb_mem2hex((char *)gdb_regs, remcom_out_buffer, NUMREGBYTES); 531 } 532 533 /* Handle the 'G' set registers request */ 534 static void gdb_cmd_setregs(struct kgdb_state *ks) 535 { 536 kgdb_hex2mem(&remcom_in_buffer[1], (char *)gdb_regs, NUMREGBYTES); 537 538 if (kgdb_usethread && kgdb_usethread != current) { 539 error_packet(remcom_out_buffer, -EINVAL); 540 } else { 541 gdb_regs_to_pt_regs(gdb_regs, ks->linux_regs); 542 strcpy(remcom_out_buffer, "OK"); 543 } 544 } 545 546 /* Handle the 'm' memory read bytes */ 547 static void gdb_cmd_memread(struct kgdb_state *ks) 548 { 549 char *ptr = &remcom_in_buffer[1]; 550 unsigned long length; 551 unsigned long addr; 552 char *err; 553 554 if (kgdb_hex2long(&ptr, &addr) > 0 && *ptr++ == ',' && 555 kgdb_hex2long(&ptr, &length) > 0) { 556 err = kgdb_mem2hex((char *)addr, remcom_out_buffer, length); 557 if (!err) 558 error_packet(remcom_out_buffer, -EINVAL); 559 } else { 560 error_packet(remcom_out_buffer, -EINVAL); 561 } 562 } 563 564 /* Handle the 'M' memory write bytes */ 565 static void gdb_cmd_memwrite(struct kgdb_state *ks) 566 { 567 int err = write_mem_msg(0); 568 569 if (err) 570 error_packet(remcom_out_buffer, err); 571 else 572 strcpy(remcom_out_buffer, "OK"); 573 } 574 575 #if DBG_MAX_REG_NUM > 0 576 static char *gdb_hex_reg_helper(int regnum, char *out) 577 { 578 int i; 579 int offset = 0; 580 581 for (i = 0; i < regnum; i++) 582 offset += dbg_reg_def[i].size; 583 return kgdb_mem2hex((char *)gdb_regs + offset, out, 584 dbg_reg_def[i].size); 585 } 586 587 /* Handle the 'p' individual regster get */ 588 static void gdb_cmd_reg_get(struct kgdb_state *ks) 589 { 590 unsigned long regnum; 591 char *ptr = &remcom_in_buffer[1]; 592 593 kgdb_hex2long(&ptr, ®num); 594 if (regnum >= DBG_MAX_REG_NUM) { 595 error_packet(remcom_out_buffer, -EINVAL); 596 return; 597 } 598 gdb_get_regs_helper(ks); 599 gdb_hex_reg_helper(regnum, remcom_out_buffer); 600 } 601 602 /* Handle the 'P' individual regster set */ 603 static void gdb_cmd_reg_set(struct kgdb_state *ks) 604 { 605 unsigned long regnum; 606 char *ptr = &remcom_in_buffer[1]; 607 int i = 0; 608 609 kgdb_hex2long(&ptr, ®num); 610 if (*ptr++ != '=' || 611 !(!kgdb_usethread || kgdb_usethread == current) || 612 !dbg_get_reg(regnum, gdb_regs, ks->linux_regs)) { 613 error_packet(remcom_out_buffer, -EINVAL); 614 return; 615 } 616 memset(gdb_regs, 0, sizeof(gdb_regs)); 617 while (i < sizeof(gdb_regs) * 2) 618 if (hex_to_bin(ptr[i]) >= 0) 619 i++; 620 else 621 break; 622 i = i / 2; 623 kgdb_hex2mem(ptr, (char *)gdb_regs, i); 624 dbg_set_reg(regnum, gdb_regs, ks->linux_regs); 625 strcpy(remcom_out_buffer, "OK"); 626 } 627 #endif /* DBG_MAX_REG_NUM > 0 */ 628 629 /* Handle the 'X' memory binary write bytes */ 630 static void gdb_cmd_binwrite(struct kgdb_state *ks) 631 { 632 int err = write_mem_msg(1); 633 634 if (err) 635 error_packet(remcom_out_buffer, err); 636 else 637 strcpy(remcom_out_buffer, "OK"); 638 } 639 640 /* Handle the 'D' or 'k', detach or kill packets */ 641 static void gdb_cmd_detachkill(struct kgdb_state *ks) 642 { 643 int error; 644 645 /* The detach case */ 646 if (remcom_in_buffer[0] == 'D') { 647 error = dbg_remove_all_break(); 648 if (error < 0) { 649 error_packet(remcom_out_buffer, error); 650 } else { 651 strcpy(remcom_out_buffer, "OK"); 652 kgdb_connected = 0; 653 } 654 put_packet(remcom_out_buffer); 655 } else { 656 /* 657 * Assume the kill case, with no exit code checking, 658 * trying to force detach the debugger: 659 */ 660 dbg_remove_all_break(); 661 kgdb_connected = 0; 662 } 663 } 664 665 /* Handle the 'R' reboot packets */ 666 static int gdb_cmd_reboot(struct kgdb_state *ks) 667 { 668 /* For now, only honor R0 */ 669 if (strcmp(remcom_in_buffer, "R0") == 0) { 670 printk(KERN_CRIT "Executing emergency reboot\n"); 671 strcpy(remcom_out_buffer, "OK"); 672 put_packet(remcom_out_buffer); 673 674 /* 675 * Execution should not return from 676 * machine_emergency_restart() 677 */ 678 machine_emergency_restart(); 679 kgdb_connected = 0; 680 681 return 1; 682 } 683 return 0; 684 } 685 686 /* Handle the 'q' query packets */ 687 static void gdb_cmd_query(struct kgdb_state *ks) 688 { 689 struct task_struct *g; 690 struct task_struct *p; 691 unsigned char thref[BUF_THREAD_ID_SIZE]; 692 char *ptr; 693 int i; 694 int cpu; 695 int finished = 0; 696 697 switch (remcom_in_buffer[1]) { 698 case 's': 699 case 'f': 700 if (memcmp(remcom_in_buffer + 2, "ThreadInfo", 10)) 701 break; 702 703 i = 0; 704 remcom_out_buffer[0] = 'm'; 705 ptr = remcom_out_buffer + 1; 706 if (remcom_in_buffer[1] == 'f') { 707 /* Each cpu is a shadow thread */ 708 for_each_online_cpu(cpu) { 709 ks->thr_query = 0; 710 int_to_threadref(thref, -cpu - 2); 711 ptr = pack_threadid(ptr, thref); 712 *(ptr++) = ','; 713 i++; 714 } 715 } 716 717 do_each_thread(g, p) { 718 if (i >= ks->thr_query && !finished) { 719 int_to_threadref(thref, p->pid); 720 ptr = pack_threadid(ptr, thref); 721 *(ptr++) = ','; 722 ks->thr_query++; 723 if (ks->thr_query % KGDB_MAX_THREAD_QUERY == 0) 724 finished = 1; 725 } 726 i++; 727 } while_each_thread(g, p); 728 729 *(--ptr) = '\0'; 730 break; 731 732 case 'C': 733 /* Current thread id */ 734 strcpy(remcom_out_buffer, "QC"); 735 ks->threadid = shadow_pid(current->pid); 736 int_to_threadref(thref, ks->threadid); 737 pack_threadid(remcom_out_buffer + 2, thref); 738 break; 739 case 'T': 740 if (memcmp(remcom_in_buffer + 1, "ThreadExtraInfo,", 16)) 741 break; 742 743 ks->threadid = 0; 744 ptr = remcom_in_buffer + 17; 745 kgdb_hex2long(&ptr, &ks->threadid); 746 if (!getthread(ks->linux_regs, ks->threadid)) { 747 error_packet(remcom_out_buffer, -EINVAL); 748 break; 749 } 750 if ((int)ks->threadid > 0) { 751 kgdb_mem2hex(getthread(ks->linux_regs, 752 ks->threadid)->comm, 753 remcom_out_buffer, 16); 754 } else { 755 static char tmpstr[23 + BUF_THREAD_ID_SIZE]; 756 757 sprintf(tmpstr, "shadowCPU%d", 758 (int)(-ks->threadid - 2)); 759 kgdb_mem2hex(tmpstr, remcom_out_buffer, strlen(tmpstr)); 760 } 761 break; 762 #ifdef CONFIG_KGDB_KDB 763 case 'R': 764 if (strncmp(remcom_in_buffer, "qRcmd,", 6) == 0) { 765 int len = strlen(remcom_in_buffer + 6); 766 767 if ((len % 2) != 0) { 768 strcpy(remcom_out_buffer, "E01"); 769 break; 770 } 771 kgdb_hex2mem(remcom_in_buffer + 6, 772 remcom_out_buffer, len); 773 len = len / 2; 774 remcom_out_buffer[len++] = 0; 775 776 kdb_parse(remcom_out_buffer); 777 strcpy(remcom_out_buffer, "OK"); 778 } 779 break; 780 #endif 781 } 782 } 783 784 /* Handle the 'H' task query packets */ 785 static void gdb_cmd_task(struct kgdb_state *ks) 786 { 787 struct task_struct *thread; 788 char *ptr; 789 790 switch (remcom_in_buffer[1]) { 791 case 'g': 792 ptr = &remcom_in_buffer[2]; 793 kgdb_hex2long(&ptr, &ks->threadid); 794 thread = getthread(ks->linux_regs, ks->threadid); 795 if (!thread && ks->threadid > 0) { 796 error_packet(remcom_out_buffer, -EINVAL); 797 break; 798 } 799 kgdb_usethread = thread; 800 ks->kgdb_usethreadid = ks->threadid; 801 strcpy(remcom_out_buffer, "OK"); 802 break; 803 case 'c': 804 ptr = &remcom_in_buffer[2]; 805 kgdb_hex2long(&ptr, &ks->threadid); 806 if (!ks->threadid) { 807 kgdb_contthread = NULL; 808 } else { 809 thread = getthread(ks->linux_regs, ks->threadid); 810 if (!thread && ks->threadid > 0) { 811 error_packet(remcom_out_buffer, -EINVAL); 812 break; 813 } 814 kgdb_contthread = thread; 815 } 816 strcpy(remcom_out_buffer, "OK"); 817 break; 818 } 819 } 820 821 /* Handle the 'T' thread query packets */ 822 static void gdb_cmd_thread(struct kgdb_state *ks) 823 { 824 char *ptr = &remcom_in_buffer[1]; 825 struct task_struct *thread; 826 827 kgdb_hex2long(&ptr, &ks->threadid); 828 thread = getthread(ks->linux_regs, ks->threadid); 829 if (thread) 830 strcpy(remcom_out_buffer, "OK"); 831 else 832 error_packet(remcom_out_buffer, -EINVAL); 833 } 834 835 /* Handle the 'z' or 'Z' breakpoint remove or set packets */ 836 static void gdb_cmd_break(struct kgdb_state *ks) 837 { 838 /* 839 * Since GDB-5.3, it's been drafted that '0' is a software 840 * breakpoint, '1' is a hardware breakpoint, so let's do that. 841 */ 842 char *bpt_type = &remcom_in_buffer[1]; 843 char *ptr = &remcom_in_buffer[2]; 844 unsigned long addr; 845 unsigned long length; 846 int error = 0; 847 848 if (arch_kgdb_ops.set_hw_breakpoint && *bpt_type >= '1') { 849 /* Unsupported */ 850 if (*bpt_type > '4') 851 return; 852 } else { 853 if (*bpt_type != '0' && *bpt_type != '1') 854 /* Unsupported. */ 855 return; 856 } 857 858 /* 859 * Test if this is a hardware breakpoint, and 860 * if we support it: 861 */ 862 if (*bpt_type == '1' && !(arch_kgdb_ops.flags & KGDB_HW_BREAKPOINT)) 863 /* Unsupported. */ 864 return; 865 866 if (*(ptr++) != ',') { 867 error_packet(remcom_out_buffer, -EINVAL); 868 return; 869 } 870 if (!kgdb_hex2long(&ptr, &addr)) { 871 error_packet(remcom_out_buffer, -EINVAL); 872 return; 873 } 874 if (*(ptr++) != ',' || 875 !kgdb_hex2long(&ptr, &length)) { 876 error_packet(remcom_out_buffer, -EINVAL); 877 return; 878 } 879 880 if (remcom_in_buffer[0] == 'Z' && *bpt_type == '0') 881 error = dbg_set_sw_break(addr); 882 else if (remcom_in_buffer[0] == 'z' && *bpt_type == '0') 883 error = dbg_remove_sw_break(addr); 884 else if (remcom_in_buffer[0] == 'Z') 885 error = arch_kgdb_ops.set_hw_breakpoint(addr, 886 (int)length, *bpt_type - '0'); 887 else if (remcom_in_buffer[0] == 'z') 888 error = arch_kgdb_ops.remove_hw_breakpoint(addr, 889 (int) length, *bpt_type - '0'); 890 891 if (error == 0) 892 strcpy(remcom_out_buffer, "OK"); 893 else 894 error_packet(remcom_out_buffer, error); 895 } 896 897 /* Handle the 'C' signal / exception passing packets */ 898 static int gdb_cmd_exception_pass(struct kgdb_state *ks) 899 { 900 /* C09 == pass exception 901 * C15 == detach kgdb, pass exception 902 */ 903 if (remcom_in_buffer[1] == '0' && remcom_in_buffer[2] == '9') { 904 905 ks->pass_exception = 1; 906 remcom_in_buffer[0] = 'c'; 907 908 } else if (remcom_in_buffer[1] == '1' && remcom_in_buffer[2] == '5') { 909 910 ks->pass_exception = 1; 911 remcom_in_buffer[0] = 'D'; 912 dbg_remove_all_break(); 913 kgdb_connected = 0; 914 return 1; 915 916 } else { 917 gdbstub_msg_write("KGDB only knows signal 9 (pass)" 918 " and 15 (pass and disconnect)\n" 919 "Executing a continue without signal passing\n", 0); 920 remcom_in_buffer[0] = 'c'; 921 } 922 923 /* Indicate fall through */ 924 return -1; 925 } 926 927 /* 928 * This function performs all gdbserial command procesing 929 */ 930 int gdb_serial_stub(struct kgdb_state *ks) 931 { 932 int error = 0; 933 int tmp; 934 935 /* Initialize comm buffer and globals. */ 936 memset(remcom_out_buffer, 0, sizeof(remcom_out_buffer)); 937 kgdb_usethread = kgdb_info[ks->cpu].task; 938 ks->kgdb_usethreadid = shadow_pid(kgdb_info[ks->cpu].task->pid); 939 ks->pass_exception = 0; 940 941 if (kgdb_connected) { 942 unsigned char thref[BUF_THREAD_ID_SIZE]; 943 char *ptr; 944 945 /* Reply to host that an exception has occurred */ 946 ptr = remcom_out_buffer; 947 *ptr++ = 'T'; 948 ptr = pack_hex_byte(ptr, ks->signo); 949 ptr += strlen(strcpy(ptr, "thread:")); 950 int_to_threadref(thref, shadow_pid(current->pid)); 951 ptr = pack_threadid(ptr, thref); 952 *ptr++ = ';'; 953 put_packet(remcom_out_buffer); 954 } 955 956 while (1) { 957 error = 0; 958 959 /* Clear the out buffer. */ 960 memset(remcom_out_buffer, 0, sizeof(remcom_out_buffer)); 961 962 get_packet(remcom_in_buffer); 963 964 switch (remcom_in_buffer[0]) { 965 case '?': /* gdbserial status */ 966 gdb_cmd_status(ks); 967 break; 968 case 'g': /* return the value of the CPU registers */ 969 gdb_cmd_getregs(ks); 970 break; 971 case 'G': /* set the value of the CPU registers - return OK */ 972 gdb_cmd_setregs(ks); 973 break; 974 case 'm': /* mAA..AA,LLLL Read LLLL bytes at address AA..AA */ 975 gdb_cmd_memread(ks); 976 break; 977 case 'M': /* MAA..AA,LLLL: Write LLLL bytes at address AA..AA */ 978 gdb_cmd_memwrite(ks); 979 break; 980 #if DBG_MAX_REG_NUM > 0 981 case 'p': /* pXX Return gdb register XX (in hex) */ 982 gdb_cmd_reg_get(ks); 983 break; 984 case 'P': /* PXX=aaaa Set gdb register XX to aaaa (in hex) */ 985 gdb_cmd_reg_set(ks); 986 break; 987 #endif /* DBG_MAX_REG_NUM > 0 */ 988 case 'X': /* XAA..AA,LLLL: Write LLLL bytes at address AA..AA */ 989 gdb_cmd_binwrite(ks); 990 break; 991 /* kill or detach. KGDB should treat this like a 992 * continue. 993 */ 994 case 'D': /* Debugger detach */ 995 case 'k': /* Debugger detach via kill */ 996 gdb_cmd_detachkill(ks); 997 goto default_handle; 998 case 'R': /* Reboot */ 999 if (gdb_cmd_reboot(ks)) 1000 goto default_handle; 1001 break; 1002 case 'q': /* query command */ 1003 gdb_cmd_query(ks); 1004 break; 1005 case 'H': /* task related */ 1006 gdb_cmd_task(ks); 1007 break; 1008 case 'T': /* Query thread status */ 1009 gdb_cmd_thread(ks); 1010 break; 1011 case 'z': /* Break point remove */ 1012 case 'Z': /* Break point set */ 1013 gdb_cmd_break(ks); 1014 break; 1015 #ifdef CONFIG_KGDB_KDB 1016 case '3': /* Escape into back into kdb */ 1017 if (remcom_in_buffer[1] == '\0') { 1018 gdb_cmd_detachkill(ks); 1019 return DBG_PASS_EVENT; 1020 } 1021 #endif 1022 case 'C': /* Exception passing */ 1023 tmp = gdb_cmd_exception_pass(ks); 1024 if (tmp > 0) 1025 goto default_handle; 1026 if (tmp == 0) 1027 break; 1028 /* Fall through on tmp < 0 */ 1029 case 'c': /* Continue packet */ 1030 case 's': /* Single step packet */ 1031 if (kgdb_contthread && kgdb_contthread != current) { 1032 /* Can't switch threads in kgdb */ 1033 error_packet(remcom_out_buffer, -EINVAL); 1034 break; 1035 } 1036 dbg_activate_sw_breakpoints(); 1037 /* Fall through to default processing */ 1038 default: 1039 default_handle: 1040 error = kgdb_arch_handle_exception(ks->ex_vector, 1041 ks->signo, 1042 ks->err_code, 1043 remcom_in_buffer, 1044 remcom_out_buffer, 1045 ks->linux_regs); 1046 /* 1047 * Leave cmd processing on error, detach, 1048 * kill, continue, or single step. 1049 */ 1050 if (error >= 0 || remcom_in_buffer[0] == 'D' || 1051 remcom_in_buffer[0] == 'k') { 1052 error = 0; 1053 goto kgdb_exit; 1054 } 1055 1056 } 1057 1058 /* reply to the request */ 1059 put_packet(remcom_out_buffer); 1060 } 1061 1062 kgdb_exit: 1063 if (ks->pass_exception) 1064 error = 1; 1065 return error; 1066 } 1067 1068 int gdbstub_state(struct kgdb_state *ks, char *cmd) 1069 { 1070 int error; 1071 1072 switch (cmd[0]) { 1073 case 'e': 1074 error = kgdb_arch_handle_exception(ks->ex_vector, 1075 ks->signo, 1076 ks->err_code, 1077 remcom_in_buffer, 1078 remcom_out_buffer, 1079 ks->linux_regs); 1080 return error; 1081 case 's': 1082 case 'c': 1083 strcpy(remcom_in_buffer, cmd); 1084 return 0; 1085 case '?': 1086 gdb_cmd_status(ks); 1087 break; 1088 case '\0': 1089 strcpy(remcom_out_buffer, ""); 1090 break; 1091 } 1092 dbg_io_ops->write_char('+'); 1093 put_packet(remcom_out_buffer); 1094 return 0; 1095 } 1096