remote_gdb.cc revision 8852:c744483edfcf
1/* 2 * Copyright (c) 2002-2005 The Regents of The University of Michigan 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are 7 * met: redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer; 9 * redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution; 12 * neither the name of the copyright holders nor the names of its 13 * contributors may be used to endorse or promote products derived from 14 * this software without specific prior written permission. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 22 * 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 26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 * 28 * Authors: Nathan Binkert 29 */ 30 31/* 32 * Copyright (c) 1990, 1993 The Regents of the University of California 33 * All rights reserved 34 * 35 * This software was developed by the Computer Systems Engineering group 36 * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and 37 * contributed to Berkeley. 38 * 39 * All advertising materials mentioning features or use of this software 40 * must display the following acknowledgement: 41 * This product includes software developed by the University of 42 * California, Lawrence Berkeley Laboratories. 43 * 44 * Redistribution and use in source and binary forms, with or without 45 * modification, are permitted provided that the following conditions 46 * are met: 47 * 1. Redistributions of source code must retain the above copyright 48 * notice, this list of conditions and the following disclaimer. 49 * 2. Redistributions in binary form must reproduce the above copyright 50 * notice, this list of conditions and the following disclaimer in the 51 * documentation and/or other materials provided with the distribution. 52 * 3. All advertising materials mentioning features or use of this software 53 * must display the following acknowledgement: 54 * This product includes software developed by the University of 55 * California, Berkeley and its contributors. 56 * 4. Neither the name of the University nor the names of its contributors 57 * may be used to endorse or promote products derived from this software 58 * without specific prior written permission. 59 * 60 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 61 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 62 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 63 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 64 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 65 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 66 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 67 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 68 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 69 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 70 * SUCH DAMAGE. 71 * 72 * @(#)kgdb_stub.c 8.4 (Berkeley) 1/12/94 73 */ 74 75/*- 76 * Copyright (c) 2001 The NetBSD Foundation, Inc. 77 * All rights reserved. 78 * 79 * This code is derived from software contributed to The NetBSD Foundation 80 * by Jason R. Thorpe. 81 * 82 * Redistribution and use in source and binary forms, with or without 83 * modification, are permitted provided that the following conditions 84 * are met: 85 * 1. Redistributions of source code must retain the above copyright 86 * notice, this list of conditions and the following disclaimer. 87 * 2. Redistributions in binary form must reproduce the above copyright 88 * notice, this list of conditions and the following disclaimer in the 89 * documentation and/or other materials provided with the distribution. 90 * 3. All advertising materials mentioning features or use of this software 91 * must display the following acknowledgement: 92 * This product includes software developed by the NetBSD 93 * Foundation, Inc. and its contributors. 94 * 4. Neither the name of The NetBSD Foundation nor the names of its 95 * contributors may be used to endorse or promote products derived 96 * from this software without specific prior written permission. 97 * 98 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 99 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 100 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 101 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 102 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 103 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 104 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 105 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 106 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 107 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 108 * POSSIBILITY OF SUCH DAMAGE. 109 */ 110 111/* 112 * $NetBSD: kgdb_stub.c,v 1.8 2001/07/07 22:58:00 wdk Exp $ 113 * 114 * Taken from NetBSD 115 * 116 * "Stub" to allow remote cpu to debug over a serial line using gdb. 117 */ 118 119#include <sys/signal.h> 120#include <unistd.h> 121 122#include <cstdio> 123#include <string> 124 125#include "arch/vtophys.hh" 126#include "base/intmath.hh" 127#include "base/remote_gdb.hh" 128#include "base/socket.hh" 129#include "base/trace.hh" 130#include "config/the_isa.hh" 131#include "cpu/static_inst.hh" 132#include "cpu/thread_context.hh" 133#include "debug/GDBAll.hh" 134#include "mem/port.hh" 135#include "mem/fs_translating_port_proxy.hh" 136#include "mem/se_translating_port_proxy.hh" 137#include "sim/full_system.hh" 138#include "sim/system.hh" 139 140using namespace std; 141using namespace Debug; 142using namespace TheISA; 143 144#ifndef NDEBUG 145vector<BaseRemoteGDB *> debuggers; 146 147void 148debugger() 149{ 150 static int current_debugger = -1; 151 if (current_debugger >= 0 && current_debugger < (int)debuggers.size()) { 152 BaseRemoteGDB *gdb = debuggers[current_debugger]; 153 if (!gdb->isattached()) 154 gdb->listener->accept(); 155 if (gdb->isattached()) 156 gdb->trap(SIGILL); 157 } 158} 159#endif 160 161/////////////////////////////////////////////////////////// 162// 163// 164// 165 166GDBListener::Event::Event(GDBListener *l, int fd, int e) 167 : PollEvent(fd, e), listener(l) 168{} 169 170void 171GDBListener::Event::process(int revent) 172{ 173 listener->accept(); 174} 175 176GDBListener::GDBListener(BaseRemoteGDB *g, int p) 177 : event(NULL), gdb(g), port(p) 178{ 179 assert(!gdb->listener); 180 gdb->listener = this; 181} 182 183GDBListener::~GDBListener() 184{ 185 if (event) 186 delete event; 187} 188 189string 190GDBListener::name() 191{ 192 return gdb->name() + ".listener"; 193} 194 195void 196GDBListener::listen() 197{ 198 if (ListenSocket::allDisabled()) { 199 warn_once("Sockets disabled, not accepting gdb connections"); 200 return; 201 } 202 203 while (!listener.listen(port, true)) { 204 DPRINTF(GDBMisc, "Can't bind port %d\n", port); 205 port++; 206 } 207 208 event = new Event(this, listener.getfd(), POLLIN); 209 pollQueue.schedule(event); 210 211#ifndef NDEBUG 212 gdb->number = debuggers.size(); 213 debuggers.push_back(gdb); 214#endif 215 216#ifndef NDEBUG 217 ccprintf(cerr, "%d: %s: listening for remote gdb #%d on port %d\n", 218 curTick(), name(), gdb->number, port); 219#else 220 ccprintf(cerr, "%d: %s: listening for remote gdb on port %d\n", 221 curTick(), name(), port); 222#endif 223} 224 225void 226GDBListener::accept() 227{ 228 if (!listener.islistening()) 229 panic("GDBListener::accept(): cannot accept if we're not listening!"); 230 231 int sfd = listener.accept(true); 232 233 if (sfd != -1) { 234 if (gdb->isattached()) 235 close(sfd); 236 else 237 gdb->attach(sfd); 238 } 239} 240 241BaseRemoteGDB::Event::Event(BaseRemoteGDB *g, int fd, int e) 242 : PollEvent(fd, e), gdb(g) 243{} 244 245void 246BaseRemoteGDB::Event::process(int revent) 247{ 248 if (revent & POLLIN) 249 gdb->trap(SIGILL); 250 else if (revent & POLLNVAL) 251 gdb->detach(); 252} 253 254BaseRemoteGDB::BaseRemoteGDB(System *_system, ThreadContext *c, size_t cacheSize) 255 : event(NULL), listener(NULL), number(-1), fd(-1), 256 active(false), attached(false), 257 system(_system), pmem(_system->physmem), context(c), 258 gdbregs(cacheSize) 259{ 260 memset(gdbregs.regs, 0, gdbregs.bytes()); 261} 262 263BaseRemoteGDB::~BaseRemoteGDB() 264{ 265 if (event) 266 delete event; 267} 268 269string 270BaseRemoteGDB::name() 271{ 272 return system->name() + ".remote_gdb"; 273} 274 275bool 276BaseRemoteGDB::isattached() 277{ return attached; } 278 279void 280BaseRemoteGDB::attach(int f) 281{ 282 fd = f; 283 284 event = new Event(this, fd, POLLIN); 285 pollQueue.schedule(event); 286 287 attached = true; 288 DPRINTFN("remote gdb attached\n"); 289} 290 291void 292BaseRemoteGDB::detach() 293{ 294 attached = false; 295 close(fd); 296 fd = -1; 297 298 pollQueue.remove(event); 299 DPRINTFN("remote gdb detached\n"); 300} 301 302const char * 303BaseRemoteGDB::gdb_command(char cmd) 304{ 305 switch (cmd) { 306 case GDBSignal: return "KGDB_SIGNAL"; 307 case GDBSetBaud: return "KGDB_SET_BAUD"; 308 case GDBSetBreak: return "KGDB_SET_BREAK"; 309 case GDBCont: return "KGDB_CONT"; 310 case GDBAsyncCont: return "KGDB_ASYNC_CONT"; 311 case GDBDebug: return "KGDB_DEBUG"; 312 case GDBDetach: return "KGDB_DETACH"; 313 case GDBRegR: return "KGDB_REG_R"; 314 case GDBRegW: return "KGDB_REG_W"; 315 case GDBSetThread: return "KGDB_SET_THREAD"; 316 case GDBCycleStep: return "KGDB_CYCLE_STEP"; 317 case GDBSigCycleStep: return "KGDB_SIG_CYCLE_STEP"; 318 case GDBKill: return "KGDB_KILL"; 319 case GDBMemW: return "KGDB_MEM_W"; 320 case GDBMemR: return "KGDB_MEM_R"; 321 case GDBSetReg: return "KGDB_SET_REG"; 322 case GDBReadReg: return "KGDB_READ_REG"; 323 case GDBQueryVar: return "KGDB_QUERY_VAR"; 324 case GDBSetVar: return "KGDB_SET_VAR"; 325 case GDBReset: return "KGDB_RESET"; 326 case GDBStep: return "KGDB_STEP"; 327 case GDBAsyncStep: return "KGDB_ASYNC_STEP"; 328 case GDBThreadAlive: return "KGDB_THREAD_ALIVE"; 329 case GDBTargetExit: return "KGDB_TARGET_EXIT"; 330 case GDBBinaryDload: return "KGDB_BINARY_DLOAD"; 331 case GDBClrHwBkpt: return "KGDB_CLR_HW_BKPT"; 332 case GDBSetHwBkpt: return "KGDB_SET_HW_BKPT"; 333 case GDBStart: return "KGDB_START"; 334 case GDBEnd: return "KGDB_END"; 335 case GDBGoodP: return "KGDB_GOODP"; 336 case GDBBadP: return "KGDB_BADP"; 337 default: return "KGDB_UNKNOWN"; 338 } 339} 340 341///////////////////////// 342// 343// 344 345uint8_t 346BaseRemoteGDB::getbyte() 347{ 348 uint8_t b; 349 if (::read(fd, &b, 1) != 1) 350 warn("could not read byte from debugger"); 351 return b; 352} 353 354void 355BaseRemoteGDB::putbyte(uint8_t b) 356{ 357 if (::write(fd, &b, 1) != 1) 358 warn("could not write byte to debugger"); 359} 360 361// Send a packet to gdb 362void 363BaseRemoteGDB::send(const char *bp) 364{ 365 const char *p; 366 uint8_t csum, c; 367 368 DPRINTF(GDBSend, "send: %s\n", bp); 369 370 do { 371 p = bp; 372 //Start sending a packet 373 putbyte(GDBStart); 374 //Send the contents, and also keep a check sum. 375 for (csum = 0; (c = *p); p++) { 376 putbyte(c); 377 csum += c; 378 } 379 //Send the ending character. 380 putbyte(GDBEnd); 381 //Sent the checksum. 382 putbyte(i2digit(csum >> 4)); 383 putbyte(i2digit(csum)); 384 //Try transmitting over and over again until the other end doesn't send an 385 //error back. 386 } while ((c = getbyte() & 0x7f) == GDBBadP); 387} 388 389// Receive a packet from gdb 390int 391BaseRemoteGDB::recv(char *bp, int maxlen) 392{ 393 char *p; 394 int c, csum; 395 int len; 396 397 do { 398 p = bp; 399 csum = len = 0; 400 //Find the beginning of a packet 401 while ((c = getbyte()) != GDBStart) 402 ; 403 404 //Read until you find the end of the data in the packet, and keep 405 //track of the check sum. 406 while ((c = getbyte()) != GDBEnd && len < maxlen) { 407 c &= 0x7f; 408 csum += c; 409 *p++ = c; 410 len++; 411 } 412 413 //Mask the check sum, and terminate the command string. 414 csum &= 0xff; 415 *p = '\0'; 416 417 //If the command was too long, report an error. 418 if (len >= maxlen) { 419 putbyte(GDBBadP); 420 continue; 421 } 422 423 //Bring in the checksum. If the check sum matches, csum will be 0. 424 csum -= digit2i(getbyte()) * 16; 425 csum -= digit2i(getbyte()); 426 427 //If the check sum was correct 428 if (csum == 0) { 429 //Report that the packet was received correctly 430 putbyte(GDBGoodP); 431 // Sequence present? 432 if (bp[2] == ':') { 433 putbyte(bp[0]); 434 putbyte(bp[1]); 435 len -= 3; 436 memcpy(bp, bp+3, len); 437 } 438 break; 439 } 440 //Otherwise, report that there was a mistake. 441 putbyte(GDBBadP); 442 } while (1); 443 444 DPRINTF(GDBRecv, "recv: %s: %s\n", gdb_command(*bp), bp); 445 446 return (len); 447} 448 449// Read bytes from kernel address space for debugger. 450bool 451BaseRemoteGDB::read(Addr vaddr, size_t size, char *data) 452{ 453 static Addr lastaddr = 0; 454 static size_t lastsize = 0; 455 456 if (vaddr < 10) { 457 DPRINTF(GDBRead, "read: reading memory location zero!\n"); 458 vaddr = lastaddr + lastsize; 459 } 460 461 DPRINTF(GDBRead, "read: addr=%#x, size=%d", vaddr, size); 462 463 if (FullSystem) { 464 FSTranslatingPortProxy &proxy = context->getVirtProxy(); 465 proxy.readBlob(vaddr, (uint8_t*)data, size); 466 } else { 467 SETranslatingPortProxy &proxy = context->getMemProxy(); 468 proxy.readBlob(vaddr, (uint8_t*)data, size); 469 } 470 471#if TRACING_ON 472 if (DTRACE(GDBRead)) { 473 if (DTRACE(GDBExtra)) { 474 char buf[1024]; 475 mem2hex(buf, data, size); 476 DPRINTFNR(": %s\n", buf); 477 } else 478 DPRINTFNR("\n"); 479 } 480#endif 481 482 return true; 483} 484 485// Write bytes to kernel address space for debugger. 486bool 487BaseRemoteGDB::write(Addr vaddr, size_t size, const char *data) 488{ 489 static Addr lastaddr = 0; 490 static size_t lastsize = 0; 491 492 if (vaddr < 10) { 493 DPRINTF(GDBWrite, "write: writing memory location zero!\n"); 494 vaddr = lastaddr + lastsize; 495 } 496 497 if (DTRACE(GDBWrite)) { 498 DPRINTFN("write: addr=%#x, size=%d", vaddr, size); 499 if (DTRACE(GDBExtra)) { 500 char buf[1024]; 501 mem2hex(buf, data, size); 502 DPRINTFNR(": %s\n", buf); 503 } else 504 DPRINTFNR("\n"); 505 } 506 if (FullSystem) { 507 FSTranslatingPortProxy &proxy = context->getVirtProxy(); 508 proxy.writeBlob(vaddr, (uint8_t*)data, size); 509 } else { 510 SETranslatingPortProxy &proxy = context->getMemProxy(); 511 proxy.writeBlob(vaddr, (uint8_t*)data, size); 512 } 513 514 return true; 515} 516 517PCEventQueue *BaseRemoteGDB::getPcEventQueue() 518{ 519 return &system->pcEventQueue; 520} 521 522BaseRemoteGDB::HardBreakpoint::HardBreakpoint(BaseRemoteGDB *_gdb, Addr pc) 523 : PCEvent(_gdb->getPcEventQueue(), "HardBreakpoint Event", pc), 524 gdb(_gdb), refcount(0) 525{ 526 DPRINTF(GDBMisc, "creating hardware breakpoint at %#x\n", evpc); 527} 528 529void 530BaseRemoteGDB::HardBreakpoint::process(ThreadContext *tc) 531{ 532 DPRINTF(GDBMisc, "handling hardware breakpoint at %#x\n", pc()); 533 534 if (tc == gdb->context) 535 gdb->trap(SIGTRAP); 536} 537 538bool 539BaseRemoteGDB::insertSoftBreak(Addr addr, size_t len) 540{ 541 if (len != sizeof(TheISA::MachInst)) 542 panic("invalid length\n"); 543 544 return insertHardBreak(addr, len); 545} 546 547bool 548BaseRemoteGDB::removeSoftBreak(Addr addr, size_t len) 549{ 550 if (len != sizeof(MachInst)) 551 panic("invalid length\n"); 552 553 return removeHardBreak(addr, len); 554} 555 556bool 557BaseRemoteGDB::insertHardBreak(Addr addr, size_t len) 558{ 559 if (len != sizeof(MachInst)) 560 panic("invalid length\n"); 561 562 DPRINTF(GDBMisc, "inserting hardware breakpoint at %#x\n", addr); 563 564 HardBreakpoint *&bkpt = hardBreakMap[addr]; 565 if (bkpt == 0) 566 bkpt = new HardBreakpoint(this, addr); 567 568 bkpt->refcount++; 569 570 return true; 571} 572 573bool 574BaseRemoteGDB::removeHardBreak(Addr addr, size_t len) 575{ 576 if (len != sizeof(MachInst)) 577 panic("invalid length\n"); 578 579 DPRINTF(GDBMisc, "removing hardware breakpoint at %#x\n", addr); 580 581 break_iter_t i = hardBreakMap.find(addr); 582 if (i == hardBreakMap.end()) 583 return false; 584 585 HardBreakpoint *hbp = (*i).second; 586 if (--hbp->refcount == 0) { 587 delete hbp; 588 hardBreakMap.erase(i); 589 } 590 591 return true; 592} 593 594void 595BaseRemoteGDB::setTempBreakpoint(Addr bkpt) 596{ 597 DPRINTF(GDBMisc, "setTempBreakpoint: addr=%#x\n", bkpt); 598 insertHardBreak(bkpt, sizeof(TheISA::MachInst)); 599} 600 601void 602BaseRemoteGDB::clearTempBreakpoint(Addr &bkpt) 603{ 604 DPRINTF(GDBMisc, "setTempBreakpoint: addr=%#x\n", bkpt); 605 removeHardBreak(bkpt, sizeof(TheISA::MachInst)); 606 bkpt = 0; 607} 608 609const char * 610BaseRemoteGDB::break_type(char c) 611{ 612 switch(c) { 613 case '0': return "software breakpoint"; 614 case '1': return "hardware breakpoint"; 615 case '2': return "write watchpoint"; 616 case '3': return "read watchpoint"; 617 case '4': return "access watchpoint"; 618 default: return "unknown breakpoint/watchpoint"; 619 } 620} 621 622// This function does all command processing for interfacing to a 623// remote gdb. Note that the error codes are ignored by gdb at 624// present, but might eventually become meaningful. (XXX) It might 625// makes sense to use POSIX errno values, because that is what the 626// gdb/remote.c functions want to return. 627bool 628BaseRemoteGDB::trap(int type) 629{ 630 uint64_t val; 631 size_t datalen, len; 632 char data[GDBPacketBufLen + 1]; 633 char *buffer; 634 size_t bufferSize; 635 const char *p; 636 char command, subcmd; 637 string var; 638 bool ret; 639 640 if (!attached) 641 return false; 642 643 bufferSize = gdbregs.bytes() * 2 + 256; 644 buffer = (char*)malloc(bufferSize); 645 646 DPRINTF(GDBMisc, "trap: PC=%s\n", context->pcState()); 647 648 clearSingleStep(); 649 650 /* 651 * The first entry to this function is normally through 652 * a breakpoint trap in kgdb_connect(), in which case we 653 * must advance past the breakpoint because gdb will not. 654 * 655 * On the first entry here, we expect that gdb is not yet 656 * listening to us, so just enter the interaction loop. 657 * After the debugger is "active" (connected) it will be 658 * waiting for a "signaled" message from us. 659 */ 660 if (!active) 661 active = true; 662 else 663 // Tell remote host that an exception has occurred. 664 snprintf((char *)buffer, bufferSize, "S%02x", type); 665 send(buffer); 666 667 // Stick frame regs into our reg cache. 668 getregs(); 669 670 for (;;) { 671 datalen = recv(data, sizeof(data)); 672 data[sizeof(data) - 1] = 0; // Sentinel 673 command = data[0]; 674 subcmd = 0; 675 p = data + 1; 676 switch (command) { 677 678 case GDBSignal: 679 // if this command came from a running gdb, answer it -- 680 // the other guy has no way of knowing if we're in or out 681 // of this loop when he issues a "remote-signal". 682 snprintf((char *)buffer, bufferSize, 683 "S%02x", type); 684 send(buffer); 685 continue; 686 687 case GDBRegR: 688 if (2 * gdbregs.bytes() > bufferSize) 689 panic("buffer too small"); 690 691 mem2hex(buffer, gdbregs.regs, gdbregs.bytes()); 692 send(buffer); 693 continue; 694 695 case GDBRegW: 696 p = hex2mem(gdbregs.regs, p, gdbregs.bytes()); 697 if (p == NULL || *p != '\0') 698 send("E01"); 699 else { 700 setregs(); 701 send("OK"); 702 } 703 continue; 704 705#if 0 706 case GDBSetReg: 707 val = hex2i(&p); 708 if (*p++ != '=') { 709 send("E01"); 710 continue; 711 } 712 if (val < 0 && val >= KGDB_NUMREGS) { 713 send("E01"); 714 continue; 715 } 716 717 gdbregs.regs[val] = hex2i(&p); 718 setregs(); 719 send("OK"); 720 721 continue; 722#endif 723 724 case GDBMemR: 725 val = hex2i(&p); 726 if (*p++ != ',') { 727 send("E02"); 728 continue; 729 } 730 len = hex2i(&p); 731 if (*p != '\0') { 732 send("E03"); 733 continue; 734 } 735 if (len > bufferSize) { 736 send("E04"); 737 continue; 738 } 739 if (!acc(val, len)) { 740 send("E05"); 741 continue; 742 } 743 744 if (read(val, (size_t)len, (char *)buffer)) { 745 // variable length array would be nice, but C++ doesn't 746 // officially support those... 747 char *temp = new char[2*len+1]; 748 mem2hex(temp, buffer, len); 749 send(temp); 750 delete [] temp; 751 } else { 752 send("E05"); 753 } 754 continue; 755 756 case GDBMemW: 757 val = hex2i(&p); 758 if (*p++ != ',') { 759 send("E06"); 760 continue; 761 } 762 len = hex2i(&p); 763 if (*p++ != ':') { 764 send("E07"); 765 continue; 766 } 767 if (len > datalen - (p - data)) { 768 send("E08"); 769 continue; 770 } 771 p = hex2mem(buffer, p, bufferSize); 772 if (p == NULL) { 773 send("E09"); 774 continue; 775 } 776 if (!acc(val, len)) { 777 send("E0A"); 778 continue; 779 } 780 if (write(val, (size_t)len, (char *)buffer)) 781 send("OK"); 782 else 783 send("E0B"); 784 continue; 785 786 case GDBSetThread: 787 subcmd = *p++; 788 val = hex2i(&p); 789 if (val == 0) 790 send("OK"); 791 else 792 send("E01"); 793 continue; 794 795 case GDBDetach: 796 case GDBKill: 797 active = false; 798 clearSingleStep(); 799 detach(); 800 goto out; 801 802 case GDBAsyncCont: 803 subcmd = hex2i(&p); 804 if (*p++ == ';') { 805 val = hex2i(&p); 806 context->pcState(val); 807 } 808 clearSingleStep(); 809 goto out; 810 811 case GDBCont: 812 if (p - data < (ptrdiff_t)datalen) { 813 val = hex2i(&p); 814 context->pcState(val); 815 } 816 clearSingleStep(); 817 goto out; 818 819 case GDBAsyncStep: 820 subcmd = hex2i(&p); 821 if (*p++ == ';') { 822 val = hex2i(&p); 823 context->pcState(val); 824 } 825 setSingleStep(); 826 goto out; 827 828 case GDBStep: 829 if (p - data < (ptrdiff_t)datalen) { 830 val = hex2i(&p); 831 context->pcState(val); 832 } 833 setSingleStep(); 834 goto out; 835 836 case GDBClrHwBkpt: 837 subcmd = *p++; 838 if (*p++ != ',') send("E0D"); 839 val = hex2i(&p); 840 if (*p++ != ',') send("E0D"); 841 len = hex2i(&p); 842 843 DPRINTF(GDBMisc, "clear %s, addr=%#x, len=%d\n", 844 break_type(subcmd), val, len); 845 846 ret = false; 847 848 switch (subcmd) { 849 case '0': // software breakpoint 850 ret = removeSoftBreak(val, len); 851 break; 852 853 case '1': // hardware breakpoint 854 ret = removeHardBreak(val, len); 855 break; 856 857 case '2': // write watchpoint 858 case '3': // read watchpoint 859 case '4': // access watchpoint 860 default: // unknown 861 send(""); 862 break; 863 } 864 865 send(ret ? "OK" : "E0C"); 866 continue; 867 868 case GDBSetHwBkpt: 869 subcmd = *p++; 870 if (*p++ != ',') send("E0D"); 871 val = hex2i(&p); 872 if (*p++ != ',') send("E0D"); 873 len = hex2i(&p); 874 875 DPRINTF(GDBMisc, "set %s, addr=%#x, len=%d\n", 876 break_type(subcmd), val, len); 877 878 ret = false; 879 880 switch (subcmd) { 881 case '0': // software breakpoint 882 ret = insertSoftBreak(val, len); 883 break; 884 885 case '1': // hardware breakpoint 886 ret = insertHardBreak(val, len); 887 break; 888 889 case '2': // write watchpoint 890 case '3': // read watchpoint 891 case '4': // access watchpoint 892 default: // unknown 893 send(""); 894 break; 895 } 896 897 send(ret ? "OK" : "E0C"); 898 continue; 899 900 case GDBQueryVar: 901 var = string(p, datalen - 1); 902 if (var == "C") 903 send("QC0"); 904 else 905 send(""); 906 continue; 907 908 case GDBSetBaud: 909 case GDBSetBreak: 910 case GDBDebug: 911 case GDBCycleStep: 912 case GDBSigCycleStep: 913 case GDBReadReg: 914 case GDBSetVar: 915 case GDBReset: 916 case GDBThreadAlive: 917 case GDBTargetExit: 918 case GDBBinaryDload: 919 // Unsupported command 920 DPRINTF(GDBMisc, "Unsupported command: %s\n", 921 gdb_command(command)); 922 DDUMP(GDBMisc, (uint8_t *)data, datalen); 923 send(""); 924 continue; 925 926 default: 927 // Unknown command. 928 DPRINTF(GDBMisc, "Unknown command: %c(%#x)\n", 929 command, command); 930 send(""); 931 continue; 932 933 934 } 935 } 936 937 out: 938 free(buffer); 939 return true; 940} 941 942// Convert a hex digit into an integer. 943// This returns -1 if the argument passed is no valid hex digit. 944int 945BaseRemoteGDB::digit2i(char c) 946{ 947 if (c >= '0' && c <= '9') 948 return (c - '0'); 949 else if (c >= 'a' && c <= 'f') 950 return (c - 'a' + 10); 951 else if (c >= 'A' && c <= 'F') 952 953 return (c - 'A' + 10); 954 else 955 return (-1); 956} 957 958// Convert the low 4 bits of an integer into an hex digit. 959char 960BaseRemoteGDB::i2digit(int n) 961{ 962 return ("0123456789abcdef"[n & 0x0f]); 963} 964 965// Convert a byte array into an hex string. 966void 967BaseRemoteGDB::mem2hex(void *vdst, const void *vsrc, int len) 968{ 969 char *dst = (char *)vdst; 970 const char *src = (const char *)vsrc; 971 972 while (len--) { 973 *dst++ = i2digit(*src >> 4); 974 *dst++ = i2digit(*src++); 975 } 976 *dst = '\0'; 977} 978 979// Convert an hex string into a byte array. 980// This returns a pointer to the character following the last valid 981// hex digit. If the string ends in the middle of a byte, NULL is 982// returned. 983const char * 984BaseRemoteGDB::hex2mem(void *vdst, const char *src, int maxlen) 985{ 986 char *dst = (char *)vdst; 987 int msb, lsb; 988 989 while (*src && maxlen--) { 990 msb = digit2i(*src++); 991 if (msb < 0) 992 return (src - 1); 993 lsb = digit2i(*src++); 994 if (lsb < 0) 995 return (NULL); 996 *dst++ = (msb << 4) | lsb; 997 } 998 return (src); 999} 1000 1001// Convert an hex string into an integer. 1002// This returns a pointer to the character following the last valid 1003// hex digit. 1004Addr 1005BaseRemoteGDB::hex2i(const char **srcp) 1006{ 1007 const char *src = *srcp; 1008 Addr r = 0; 1009 int nibble; 1010 1011 while ((nibble = digit2i(*src)) >= 0) { 1012 r *= 16; 1013 r += nibble; 1014 src++; 1015 } 1016 *srcp = src; 1017 return (r); 1018} 1019 1020