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