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