2 * Copyright (c) 2002-2005 The Regents of The University of Michigan
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.
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.
28 * Authors: Nathan Binkert
32 * Copyright (c) 1990, 1993
33 * The Regents of the University of California. All rights reserved.
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.
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.
44 * Redistribution and use in source and binary forms, with or without
45 * modification, are permitted provided that the following conditions
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.
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
72 * @(#)kgdb_stub.c 8.4 (Berkeley) 1/12/94
76 * Copyright (c) 2001 The NetBSD Foundation, Inc.
77 * All rights reserved.
79 * This code is derived from software contributed to The NetBSD Foundation
82 * Redistribution and use in source and binary forms, with or without
83 * modification, are permitted provided that the following conditions
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.
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.
112 * $NetBSD: kgdb_stub.c,v 1.8 2001/07/07 22:58:00 wdk Exp $
116 * "Stub" to allow remote cpu to debug over a serial line using gdb.
119 #include <sys/signal.h>
124 #include "config/full_system.hh"
127 #include "arch/vtophys.hh"
130 #include "base/intmath.hh"
131 #include "base/remote_gdb.hh"
132 #include "base/socket.hh"
133 #include "base/trace.hh"
134 #include "cpu/thread_context.hh"
135 #include "cpu/static_inst.hh"
136 //#include "mem/physical.hh"
137 #include "mem/port.hh"
138 #include "mem/translating_port.hh"
139 #include "sim/system.hh"
142 using namespace TheISA
;
145 vector
<BaseRemoteGDB
*> debuggers
;
150 static int current_debugger
= -1;
151 if (current_debugger
>= 0 && current_debugger
< debuggers
.size()) {
152 BaseRemoteGDB
*gdb
= debuggers
[current_debugger
];
153 if (!gdb
->isattached())
154 gdb
->listener
->accept();
155 if (gdb
->isattached())
161 ///////////////////////////////////////////////////////////
166 GDBListener::Event::Event(GDBListener
*l
, int fd
, int e
)
167 : PollEvent(fd
, e
), listener(l
)
171 GDBListener::Event::process(int revent
)
176 GDBListener::GDBListener(BaseRemoteGDB
*g
, int p
)
177 : event(NULL
), gdb(g
), port(p
)
179 assert(!gdb
->listener
);
180 gdb
->listener
= this;
183 GDBListener::~GDBListener()
192 return gdb
->name() + ".listener";
196 GDBListener::listen()
198 while (!listener
.listen(port
, true)) {
199 DPRINTF(GDBMisc
, "Can't bind port %d\n", port
);
203 event
= new Event(this, listener
.getfd(), POLLIN
);
204 pollQueue
.schedule(event
);
207 gdb
->number
= debuggers
.size();
208 debuggers
.push_back(gdb
);
212 ccprintf(cerr
, "%d: %s: listening for remote gdb #%d on port %d\n",
213 curTick
, name(), gdb
->number
, port
);
215 ccprintf(cerr
, "%d: %s: listening for remote gdb on port %d\n",
216 curTick
, name(), port
);
221 GDBListener::accept()
223 if (!listener
.islistening())
224 panic("GDBListener::accept(): cannot accept if we're not listening!");
226 int sfd
= listener
.accept(true);
229 if (gdb
->isattached())
236 BaseRemoteGDB::Event::Event(BaseRemoteGDB
*g
, int fd
, int e
)
237 : PollEvent(fd
, e
), gdb(g
)
241 BaseRemoteGDB::Event::process(int revent
)
245 else if (revent
& POLLNVAL
)
249 BaseRemoteGDB::BaseRemoteGDB(System
*_system
, ThreadContext
*c
, size_t cacheSize
)
250 : event(NULL
), listener(NULL
), number(-1), fd(-1),
251 active(false), attached(false),
252 system(_system
), pmem(_system
->physmem
), context(c
),
255 memset(gdbregs
.regs
, 0, gdbregs
.bytes());
258 BaseRemoteGDB::~BaseRemoteGDB()
265 BaseRemoteGDB::name()
267 return system
->name() + ".remote_gdb";
271 BaseRemoteGDB::isattached()
275 BaseRemoteGDB::attach(int f
)
279 event
= new Event(this, fd
, POLLIN
);
280 pollQueue
.schedule(event
);
283 DPRINTFN("remote gdb attached\n");
287 BaseRemoteGDB::detach()
293 pollQueue
.remove(event
);
294 DPRINTFN("remote gdb detached\n");
298 BaseRemoteGDB::gdb_command(char cmd
)
301 case GDBSignal
: return "KGDB_SIGNAL";
302 case GDBSetBaud
: return "KGDB_SET_BAUD";
303 case GDBSetBreak
: return "KGDB_SET_BREAK";
304 case GDBCont
: return "KGDB_CONT";
305 case GDBAsyncCont
: return "KGDB_ASYNC_CONT";
306 case GDBDebug
: return "KGDB_DEBUG";
307 case GDBDetach
: return "KGDB_DETACH";
308 case GDBRegR
: return "KGDB_REG_R";
309 case GDBRegW
: return "KGDB_REG_W";
310 case GDBSetThread
: return "KGDB_SET_THREAD";
311 case GDBCycleStep
: return "KGDB_CYCLE_STEP";
312 case GDBSigCycleStep
: return "KGDB_SIG_CYCLE_STEP";
313 case GDBKill
: return "KGDB_KILL";
314 case GDBMemW
: return "KGDB_MEM_W";
315 case GDBMemR
: return "KGDB_MEM_R";
316 case GDBSetReg
: return "KGDB_SET_REG";
317 case GDBReadReg
: return "KGDB_READ_REG";
318 case GDBQueryVar
: return "KGDB_QUERY_VAR";
319 case GDBSetVar
: return "KGDB_SET_VAR";
320 case GDBReset
: return "KGDB_RESET";
321 case GDBStep
: return "KGDB_STEP";
322 case GDBAsyncStep
: return "KGDB_ASYNC_STEP";
323 case GDBThreadAlive
: return "KGDB_THREAD_ALIVE";
324 case GDBTargetExit
: return "KGDB_TARGET_EXIT";
325 case GDBBinaryDload
: return "KGDB_BINARY_DLOAD";
326 case GDBClrHwBkpt
: return "KGDB_CLR_HW_BKPT";
327 case GDBSetHwBkpt
: return "KGDB_SET_HW_BKPT";
328 case GDBStart
: return "KGDB_START";
329 case GDBEnd
: return "KGDB_END";
330 case GDBGoodP
: return "KGDB_GOODP";
331 case GDBBadP
: return "KGDB_BADP";
332 default: return "KGDB_UNKNOWN";
336 /////////////////////////
341 BaseRemoteGDB::getbyte()
349 BaseRemoteGDB::putbyte(uint8_t b
)
354 // Send a packet to gdb
356 BaseRemoteGDB::send(const char *bp
)
361 DPRINTF(GDBSend
, "send: %s\n", bp
);
365 //Start sending a packet
367 //Send the contents, and also keep a check sum.
368 for (csum
= 0; (c
= *p
); p
++) {
372 //Send the ending character.
375 putbyte(i2digit(csum
>> 4));
376 putbyte(i2digit(csum
));
377 //Try transmitting over and over again until the other end doesn't send an
379 } while ((c
= getbyte() & 0x7f) == GDBBadP
);
382 // Receive a packet from gdb
384 BaseRemoteGDB::recv(char *bp
, int maxlen
)
393 //Find the beginning of a packet
394 while ((c
= getbyte()) != GDBStart
)
397 //Read until you find the end of the data in the packet, and keep
398 //track of the check sum.
399 while ((c
= getbyte()) != GDBEnd
&& len
< maxlen
) {
406 //Mask the check sum, and terminate the command string.
410 //If the command was too long, report an error.
416 //Bring in the checksum. If the check sum matches, csum will be 0.
417 csum
-= digit2i(getbyte()) * 16;
418 csum
-= digit2i(getbyte());
420 //If the check sum was correct
422 //Report that the packet was received correctly
429 memcpy(bp
, bp
+3, len
);
433 //Otherwise, report that there was a mistake.
437 DPRINTF(GDBRecv
, "recv: %s: %s\n", gdb_command(*bp
), bp
);
442 // Read bytes from kernel address space for debugger.
444 BaseRemoteGDB::read(Addr vaddr
, size_t size
, char *data
)
446 static Addr lastaddr
= 0;
447 static size_t lastsize
= 0;
450 DPRINTF(GDBRead
, "read: reading memory location zero!\n");
451 vaddr
= lastaddr
+ lastsize
;
454 DPRINTF(GDBRead
, "read: addr=%#x, size=%d", vaddr
, size
);
457 VirtualPort
*port
= context
->getVirtPort(context
);
459 TranslatingPort
*port
= context
->getMemPort();
461 port
->readBlob(vaddr
, (uint8_t*)data
, size
);
463 context
->delVirtPort(port
);
467 if (DTRACE(GDBRead
)) {
468 if (DTRACE(GDBExtra
)) {
470 mem2hex(buf
, data
, size
);
471 DPRINTFNR(": %s\n", buf
);
480 // Write bytes to kernel address space for debugger.
482 BaseRemoteGDB::write(Addr vaddr
, size_t size
, const char *data
)
484 static Addr lastaddr
= 0;
485 static size_t lastsize
= 0;
488 DPRINTF(GDBWrite
, "write: writing memory location zero!\n");
489 vaddr
= lastaddr
+ lastsize
;
492 if (DTRACE(GDBWrite
)) {
493 DPRINTFN("write: addr=%#x, size=%d", vaddr
, size
);
494 if (DTRACE(GDBExtra
)) {
496 mem2hex(buf
, data
, size
);
497 DPRINTFNR(": %s\n", buf
);
502 VirtualPort
*port
= context
->getVirtPort(context
);
504 TranslatingPort
*port
= context
->getMemPort();
506 port
->writeBlob(vaddr
, (uint8_t*)data
, size
);
508 context
->delVirtPort(port
);
516 PCEventQueue
*BaseRemoteGDB::getPcEventQueue()
518 return &system
->pcEventQueue
;
521 BaseRemoteGDB::HardBreakpoint::HardBreakpoint(BaseRemoteGDB
*_gdb
, Addr pc
)
522 : PCEvent(_gdb
->getPcEventQueue(), "HardBreakpoint Event", pc
),
523 gdb(_gdb
), refcount(0)
525 DPRINTF(GDBMisc
, "creating hardware breakpoint at %#x\n", evpc
);
529 BaseRemoteGDB::HardBreakpoint::process(ThreadContext
*tc
)
531 DPRINTF(GDBMisc
, "handling hardware breakpoint at %#x\n", pc());
533 if (tc
== gdb
->context
)
538 BaseRemoteGDB::insertSoftBreak(Addr addr
, size_t len
)
540 if (len
!= sizeof(TheISA::MachInst
))
541 panic("invalid length\n");
543 return insertHardBreak(addr
, len
);
547 BaseRemoteGDB::removeSoftBreak(Addr addr
, size_t len
)
549 if (len
!= sizeof(MachInst
))
550 panic("invalid length\n");
552 return removeHardBreak(addr
, len
);
556 BaseRemoteGDB::insertHardBreak(Addr addr
, size_t len
)
558 if (len
!= sizeof(MachInst
))
559 panic("invalid length\n");
561 DPRINTF(GDBMisc
, "inserting hardware breakpoint at %#x\n", addr
);
563 HardBreakpoint
*&bkpt
= hardBreakMap
[addr
];
565 bkpt
= new HardBreakpoint(this, addr
);
573 BaseRemoteGDB::removeHardBreak(Addr addr
, size_t len
)
575 if (len
!= sizeof(MachInst
))
576 panic("invalid length\n");
578 DPRINTF(GDBMisc
, "removing hardware breakpoint at %#x\n", addr
);
580 break_iter_t i
= hardBreakMap
.find(addr
);
581 if (i
== hardBreakMap
.end())
584 HardBreakpoint
*hbp
= (*i
).second
;
585 if (--hbp
->refcount
== 0) {
587 hardBreakMap
.erase(i
);
594 BaseRemoteGDB::setTempBreakpoint(Addr bkpt
)
596 DPRINTF(GDBMisc
, "setTempBreakpoint: addr=%#x\n", bkpt
);
597 insertHardBreak(bkpt
, sizeof(TheISA::MachInst
));
601 BaseRemoteGDB::clearTempBreakpoint(Addr
&bkpt
)
603 DPRINTF(GDBMisc
, "setTempBreakpoint: addr=%#x\n", bkpt
);
604 removeHardBreak(bkpt
, sizeof(TheISA::MachInst
));
609 BaseRemoteGDB::break_type(char c
)
612 case '0': return "software breakpoint";
613 case '1': return "hardware breakpoint";
614 case '2': return "write watchpoint";
615 case '3': return "read watchpoint";
616 case '4': return "access watchpoint";
617 default: return "unknown breakpoint/watchpoint";
621 // This function does all command processing for interfacing to a
622 // remote gdb. Note that the error codes are ignored by gdb at
623 // present, but might eventually become meaningful. (XXX) It might
624 // makes sense to use POSIX errno values, because that is what the
625 // gdb/remote.c functions want to return.
627 BaseRemoteGDB::trap(int type
)
631 char data
[GDBPacketBufLen
+ 1];
635 char command
, subcmd
;
642 bufferSize
= gdbregs
.bytes() * 2 + 256;
643 buffer
= (char*)malloc(bufferSize
);
645 DPRINTF(GDBMisc
, "trap: PC=%#x NPC=%#x\n",
646 context
->readPC(), context
->readNextPC());
651 * The first entry to this function is normally through
652 * a breakpoint trap in kgdb_connect(), in which case we
653 * must advance past the breakpoint because gdb will not.
655 * On the first entry here, we expect that gdb is not yet
656 * listening to us, so just enter the interaction loop.
657 * After the debugger is "active" (connected) it will be
658 * waiting for a "signaled" message from us.
663 // Tell remote host that an exception has occurred.
664 snprintf((char *)buffer
, bufferSize
, "S%02x", type
);
667 // Stick frame regs into our reg cache.
671 datalen
= recv(data
, sizeof(data
));
672 data
[sizeof(data
) - 1] = 0; // Sentinel
679 // if this command came from a running gdb, answer it --
680 // the other guy has no way of knowing if we're in or out
681 // of this loop when he issues a "remote-signal".
682 snprintf((char *)buffer
, bufferSize
,
688 if (2 * gdbregs
.bytes() > bufferSize
)
689 panic("buffer too small");
691 mem2hex(buffer
, gdbregs
.regs
, gdbregs
.bytes());
696 p
= hex2mem(gdbregs
.regs
, p
, gdbregs
.bytes());
697 if (p
== NULL
|| *p
!= '\0')
712 if (val
< 0 && val
>= KGDB_NUMREGS
) {
717 gdbregs
.regs
[val
] = hex2i(&p
);
735 if (len
> bufferSize
) {
739 if (!acc(val
, len
)) {
744 if (read(val
, (size_t)len
, (char *)buffer
)) {
745 // variable length array would be nice, but C++ doesn't
746 // officially support those...
747 char *temp
= new char[2*len
+1];
748 mem2hex(temp
, buffer
, len
);
767 if (len
> datalen
- (p
- data
)) {
771 p
= hex2mem(buffer
, p
, bufferSize
);
776 if (!acc(val
, len
)) {
780 if (write(val
, (size_t)len
, (char *)buffer
))
807 context
->setNextPC(val
+ sizeof(MachInst
));
813 if (p
- data
< datalen
) {
816 context
->setNextPC(val
+ sizeof(MachInst
));
826 context
->setNextPC(val
+ sizeof(MachInst
));
832 if (p
- data
< datalen
) {
835 context
->setNextPC(val
+ sizeof(MachInst
));
842 if (*p
++ != ',') send("E0D");
844 if (*p
++ != ',') send("E0D");
847 DPRINTF(GDBMisc
, "clear %s, addr=%#x, len=%d\n",
848 break_type(subcmd
), val
, len
);
853 case '0': // software breakpoint
854 ret
= removeSoftBreak(val
, len
);
857 case '1': // hardware breakpoint
858 ret
= removeHardBreak(val
, len
);
861 case '2': // write watchpoint
862 case '3': // read watchpoint
863 case '4': // access watchpoint
869 send(ret
? "OK" : "E0C");
874 if (*p
++ != ',') send("E0D");
876 if (*p
++ != ',') send("E0D");
879 DPRINTF(GDBMisc
, "set %s, addr=%#x, len=%d\n",
880 break_type(subcmd
), val
, len
);
885 case '0': // software breakpoint
886 ret
= insertSoftBreak(val
, len
);
889 case '1': // hardware breakpoint
890 ret
= insertHardBreak(val
, len
);
893 case '2': // write watchpoint
894 case '3': // read watchpoint
895 case '4': // access watchpoint
901 send(ret
? "OK" : "E0C");
905 var
= string(p
, datalen
- 1);
916 case GDBSigCycleStep
:
923 // Unsupported command
924 DPRINTF(GDBMisc
, "Unsupported command: %s\n",
925 gdb_command(command
));
926 DDUMP(GDBMisc
, (uint8_t *)data
, datalen
);
932 DPRINTF(GDBMisc
, "Unknown command: %c(%#x)\n",
946 // Convert a hex digit into an integer.
947 // This returns -1 if the argument passed is no valid hex digit.
949 BaseRemoteGDB::digit2i(char c
)
951 if (c
>= '0' && c
<= '9')
953 else if (c
>= 'a' && c
<= 'f')
954 return (c
- 'a' + 10);
955 else if (c
>= 'A' && c
<= 'F')
957 return (c
- 'A' + 10);
962 // Convert the low 4 bits of an integer into an hex digit.
964 BaseRemoteGDB::i2digit(int n
)
966 return ("0123456789abcdef"[n
& 0x0f]);
969 // Convert a byte array into an hex string.
971 BaseRemoteGDB::mem2hex(void *vdst
, const void *vsrc
, int len
)
973 char *dst
= (char *)vdst
;
974 const char *src
= (const char *)vsrc
;
977 *dst
++ = i2digit(*src
>> 4);
978 *dst
++ = i2digit(*src
++);
983 // Convert an hex string into a byte array.
984 // This returns a pointer to the character following the last valid
985 // hex digit. If the string ends in the middle of a byte, NULL is
988 BaseRemoteGDB::hex2mem(void *vdst
, const char *src
, int maxlen
)
990 char *dst
= (char *)vdst
;
993 while (*src
&& maxlen
--) {
994 msb
= digit2i(*src
++);
997 lsb
= digit2i(*src
++);
1000 *dst
++ = (msb
<< 4) | lsb
;
1005 // Convert an hex string into an integer.
1006 // This returns a pointer to the character following the last valid
1009 BaseRemoteGDB::hex2i(const char **srcp
)
1011 const char *src
= *srcp
;
1015 while ((nibble
= digit2i(*src
)) >= 0) {