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