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