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