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