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