2 * Copyright 2015 LabWare
3 * Copyright 2014 Google, Inc.
4 * Copyright (c) 2002-2005 The Regents of The University of Michigan
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are
9 * met: redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer;
11 * redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution;
14 * neither the name of the copyright holders nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 * Authors: Nathan Binkert
35 * Copyright (c) 1990, 1993 The Regents of the University of California
38 * This software was developed by the Computer Systems Engineering group
39 * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
40 * contributed to Berkeley.
42 * All advertising materials mentioning features or use of this software
43 * must display the following acknowledgement:
44 * This product includes software developed by the University of
45 * California, Lawrence Berkeley Laboratories.
47 * Redistribution and use in source and binary forms, with or without
48 * modification, are permitted provided that the following conditions
50 * 1. Redistributions of source code must retain the above copyright
51 * notice, this list of conditions and the following disclaimer.
52 * 2. Redistributions in binary form must reproduce the above copyright
53 * notice, this list of conditions and the following disclaimer in the
54 * documentation and/or other materials provided with the distribution.
55 * 3. All advertising materials mentioning features or use of this software
56 * must display the following acknowledgement:
57 * This product includes software developed by the University of
58 * California, Berkeley and its contributors.
59 * 4. Neither the name of the University nor the names of its contributors
60 * may be used to endorse or promote products derived from this software
61 * without specific prior written permission.
63 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
64 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
65 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
66 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
67 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
68 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
69 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
70 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
71 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
72 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
75 * @(#)kgdb_stub.c 8.4 (Berkeley) 1/12/94
79 * Copyright (c) 2001 The NetBSD Foundation, Inc.
80 * All rights reserved.
82 * This code is derived from software contributed to The NetBSD Foundation
85 * Redistribution and use in source and binary forms, with or without
86 * modification, are permitted provided that the following conditions
88 * 1. Redistributions of source code must retain the above copyright
89 * notice, this list of conditions and the following disclaimer.
90 * 2. Redistributions in binary form must reproduce the above copyright
91 * notice, this list of conditions and the following disclaimer in the
92 * documentation and/or other materials provided with the distribution.
93 * 3. All advertising materials mentioning features or use of this software
94 * must display the following acknowledgement:
95 * This product includes software developed by the NetBSD
96 * Foundation, Inc. and its contributors.
97 * 4. Neither the name of The NetBSD Foundation nor the names of its
98 * contributors may be used to endorse or promote products derived
99 * from this software without specific prior written permission.
101 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
102 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
103 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
104 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
105 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
106 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
107 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
108 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
109 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
110 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
111 * POSSIBILITY OF SUCH DAMAGE.
115 * $NetBSD: kgdb_stub.c,v 1.8 2001/07/07 22:58:00 wdk Exp $
119 * "Stub" to allow remote cpu to debug over a serial line using gdb.
122 #include "base/remote_gdb.hh"
124 #include <sys/signal.h>
132 #include "arch/vtophys.hh"
133 #include "base/intmath.hh"
134 #include "base/socket.hh"
135 #include "base/trace.hh"
136 #include "config/the_isa.hh"
137 #include "cpu/base.hh"
138 #include "cpu/static_inst.hh"
139 #include "cpu/thread_context.hh"
140 #include "debug/GDBAll.hh"
141 #include "mem/fs_translating_port_proxy.hh"
142 #include "mem/port.hh"
143 #include "mem/se_translating_port_proxy.hh"
144 #include "sim/full_system.hh"
145 #include "sim/system.hh"
148 using namespace TheISA
;
150 static const char GDBStart
= '$';
151 static const char GDBEnd
= '#';
152 static const char GDBGoodP
= '+';
153 static const char GDBBadP
= '-';
155 static const int GDBPacketBufLen
= 1024;
157 vector
<BaseRemoteGDB
*> debuggers
;
159 class HardBreakpoint
: public PCEvent
168 HardBreakpoint(BaseRemoteGDB
*_gdb
, PCEventQueue
*q
, Addr pc
)
169 : PCEvent(q
, "HardBreakpoint Event", pc
),
170 gdb(_gdb
), refcount(0)
172 DPRINTF(GDBMisc
, "creating hardware breakpoint at %#x\n", evpc
);
175 const std::string
name() const override
{ return gdb
->name() + ".hwbkpt"; }
178 process(ThreadContext
*tc
) override
180 DPRINTF(GDBMisc
, "handling hardware breakpoint at %#x\n", pc());
189 // Exception to throw when the connection to the client is broken.
193 BadClient(const char *_warning
=NULL
) : warning(_warning
)
197 // Exception to throw when an error needs to be reported to the client.
201 CmdError(std::string _error
) : error(_error
)
205 // Exception to throw when something isn't supported.
206 class Unsupported
{};
208 // Convert a hex digit into an integer.
209 // This returns -1 if the argument passed is no valid hex digit.
213 if (c
>= '0' && c
<= '9')
215 else if (c
>= 'a' && c
<= 'f')
216 return (c
- 'a' + 10);
217 else if (c
>= 'A' && c
<= 'F')
218 return (c
- 'A' + 10);
223 // Convert the low 4 bits of an integer into an hex digit.
227 return ("0123456789abcdef"[n
& 0x0f]);
230 // Convert a byte array into an hex string.
232 mem2hex(char *vdst
, const char *vsrc
, int len
)
235 const char *src
= vsrc
;
238 *dst
++ = i2digit(*src
>> 4);
239 *dst
++ = i2digit(*src
++);
244 // Convert an hex string into a byte array.
245 // This returns a pointer to the character following the last valid
246 // hex digit. If the string ends in the middle of a byte, NULL is
249 hex2mem(char *vdst
, const char *src
, int maxlen
)
254 while (*src
&& maxlen
--) {
255 msb
= digit2i(*src
++);
258 lsb
= digit2i(*src
++);
261 *dst
++ = (msb
<< 4) | lsb
;
266 // Convert an hex string into an integer.
267 // This returns a pointer to the character following the last valid
270 hex2i(const char **srcp
)
272 const char *src
= *srcp
;
276 while ((nibble
= digit2i(*src
)) >= 0) {
285 enum GdbBreakpointType
{
298 case GdbSoftBp
: return "software breakpoint";
299 case GdbHardBp
: return "hardware breakpoint";
300 case GdbWriteWp
: return "write watchpoint";
301 case GdbReadWp
: return "read watchpoint";
302 case GdbAccWp
: return "access watchpoint";
303 default: return "unknown breakpoint/watchpoint";
308 std::map
<Addr
, HardBreakpoint
*> hardBreakMap
;
311 getComInstEventQueue(ThreadContext
*tc
)
313 return tc
->getCpuPtr()->comInstEventQueue
[tc
->threadId()];
318 BaseRemoteGDB::BaseRemoteGDB(System
*_system
, ThreadContext
*c
, int _port
) :
319 connectEvent(nullptr), dataEvent(nullptr), _port(_port
), fd(-1),
320 active(false), attached(false), sys(_system
), tc(c
),
321 trapEvent(this), singleStepEvent(*this)
323 debuggers
.push_back(this);
326 BaseRemoteGDB::~BaseRemoteGDB()
333 BaseRemoteGDB::name()
335 return sys
->name() + ".remote_gdb";
339 BaseRemoteGDB::listen()
341 if (ListenSocket::allDisabled()) {
342 warn_once("Sockets disabled, not accepting gdb connections");
346 while (!listener
.listen(_port
, true)) {
347 DPRINTF(GDBMisc
, "Can't bind port %d\n", _port
);
351 connectEvent
= new ConnectEvent(this, listener
.getfd(), POLLIN
);
352 pollQueue
.schedule(connectEvent
);
354 ccprintf(cerr
, "%d: %s: listening for remote gdb on port %d\n",
355 curTick(), name(), _port
);
359 BaseRemoteGDB::connect()
361 panic_if(!listener
.islistening(),
362 "Cannot accept GDB connections if we're not listening!");
364 int sfd
= listener
.accept(true);
375 BaseRemoteGDB::port() const
377 panic_if(!listener
.islistening(),
378 "Remote GDB port is unknown until listen() has been called.\n");
383 BaseRemoteGDB::attach(int f
)
387 dataEvent
= new DataEvent(this, fd
, POLLIN
);
388 pollQueue
.schedule(dataEvent
);
391 DPRINTFN("remote gdb attached\n");
395 BaseRemoteGDB::detach()
403 pollQueue
.remove(dataEvent
);
404 DPRINTFN("remote gdb detached\n");
407 // This function does all command processing for interfacing to a
408 // remote gdb. Note that the error codes are ignored by gdb at
409 // present, but might eventually become meaningful. (XXX) It might
410 // makes sense to use POSIX errno values, because that is what the
411 // gdb/remote.c functions want to return.
413 BaseRemoteGDB::trap(int type
)
419 DPRINTF(GDBMisc
, "trap: PC=%s\n", tc
->pcState());
424 * The first entry to this function is normally through
425 * a breakpoint trap in kgdb_connect(), in which case we
426 * must advance past the breakpoint because gdb will not.
428 * On the first entry here, we expect that gdb is not yet
429 * listening to us, so just enter the interaction loop.
430 * After the debugger is "active" (connected) it will be
431 * waiting for a "signaled" message from us.
436 // Tell remote host that an exception has occurred.
437 send(csprintf("S%02x", type
).c_str());
440 // Stick frame regs into our reg cache.
441 regCachePtr
= gdbRegs();
442 regCachePtr
->getRegs(tc
);
444 char data
[GDBPacketBufLen
+ 1];
445 GdbCommand::Context cmdCtx
;
447 cmdCtx
.data
= &data
[1];
451 size_t datalen
= recv(data
, sizeof(data
));
455 data
[datalen
] = 0; // Sentinel
456 cmdCtx
.cmd_byte
= data
[0];
457 cmdCtx
.len
= datalen
- 1;
459 auto cmdIt
= command_map
.find(cmdCtx
.cmd_byte
);
460 if (cmdIt
== command_map
.end()) {
461 DPRINTF(GDBMisc
, "Unknown command: %c(%#x)\n",
462 cmdCtx
.cmd_byte
, cmdCtx
.cmd_byte
);
465 cmdCtx
.cmd
= &(cmdIt
->second
);
467 if (!(this->*(cmdCtx
.cmd
->func
))(cmdCtx
))
470 } catch (BadClient
&e
) {
475 } catch (Unsupported
&e
) {
477 } catch (CmdError
&e
) {
478 send(e
.error
.c_str());
480 panic("Unrecognzied GDB exception.");
488 BaseRemoteGDB::incomingData(int revent
)
490 if (trapEvent
.scheduled()) {
491 warn("GDB trap event has already been scheduled!");
495 if (revent
& POLLIN
) {
496 trapEvent
.type(SIGILL
);
497 scheduleInstCommitEvent(&trapEvent
, 0);
498 } else if (revent
& POLLNVAL
) {
499 descheduleInstCommitEvent(&trapEvent
);
505 BaseRemoteGDB::getbyte()
508 if (::read(fd
, &b
, sizeof(b
)) == sizeof(b
))
511 throw BadClient("Couldn't read data from debugger.");
515 BaseRemoteGDB::putbyte(uint8_t b
)
517 if (::write(fd
, &b
, sizeof(b
)) == sizeof(b
))
520 throw BadClient("Couldn't write data to the debugger.");
523 // Receive a packet from gdb
525 BaseRemoteGDB::recv(char *bp
, int maxlen
)
535 // Find the beginning of a packet
536 while ((c
= getbyte()) != GDBStart
);
538 // Read until you find the end of the data in the packet, and keep
539 // track of the check sum.
540 while (len
< maxlen
) {
550 // Mask the check sum, and terminate the command string.
554 // If the command was too long, report an error.
560 // Bring in the checksum. If the check sum matches, csum will be 0.
561 csum
-= digit2i(getbyte()) * 16;
562 csum
-= digit2i(getbyte());
564 // If the check sum was correct
566 // Report that the packet was received correctly
573 memcpy(bp
, bp
+3, len
);
577 // Otherwise, report that there was a mistake.
581 DPRINTF(GDBRecv
, "recv: %s\n", bp
);
586 // Send a packet to gdb
588 BaseRemoteGDB::send(const char *bp
)
593 DPRINTF(GDBSend
, "send: %s\n", bp
);
597 // Start sending a packet
599 // Send the contents, and also keep a check sum.
600 for (csum
= 0; (c
= *p
); p
++) {
604 // Send the ending character.
606 // Send the checksum.
607 putbyte(i2digit(csum
>> 4));
608 putbyte(i2digit(csum
));
609 // Try transmitting over and over again until the other end doesn't
610 // send an error back.
612 } while ((c
& 0x7f) == GDBBadP
);
615 // Read bytes from kernel address space for debugger.
617 BaseRemoteGDB::read(Addr vaddr
, size_t size
, char *data
)
619 static Addr lastaddr
= 0;
620 static size_t lastsize
= 0;
623 DPRINTF(GDBRead
, "read: reading memory location zero!\n");
624 vaddr
= lastaddr
+ lastsize
;
627 DPRINTF(GDBRead
, "read: addr=%#x, size=%d", vaddr
, size
);
630 FSTranslatingPortProxy
&proxy
= tc
->getVirtProxy();
631 proxy
.readBlob(vaddr
, (uint8_t*)data
, size
);
633 SETranslatingPortProxy
&proxy
= tc
->getMemProxy();
634 proxy
.readBlob(vaddr
, (uint8_t*)data
, size
);
638 if (DTRACE(GDBRead
)) {
639 if (DTRACE(GDBExtra
)) {
641 mem2hex(buf
, data
, size
);
642 DPRINTFNR(": %s\n", buf
);
651 // Write bytes to kernel address space for debugger.
653 BaseRemoteGDB::write(Addr vaddr
, size_t size
, const char *data
)
655 static Addr lastaddr
= 0;
656 static size_t lastsize
= 0;
659 DPRINTF(GDBWrite
, "write: writing memory location zero!\n");
660 vaddr
= lastaddr
+ lastsize
;
663 if (DTRACE(GDBWrite
)) {
664 DPRINTFN("write: addr=%#x, size=%d", vaddr
, size
);
665 if (DTRACE(GDBExtra
)) {
667 mem2hex(buf
, data
, size
);
668 DPRINTFNR(": %s\n", buf
);
673 FSTranslatingPortProxy
&proxy
= tc
->getVirtProxy();
674 proxy
.writeBlob(vaddr
, (uint8_t*)data
, size
);
676 SETranslatingPortProxy
&proxy
= tc
->getMemProxy();
677 proxy
.writeBlob(vaddr
, (uint8_t*)data
, size
);
684 BaseRemoteGDB::singleStep()
686 if (!singleStepEvent
.scheduled())
687 scheduleInstCommitEvent(&singleStepEvent
, 1);
692 BaseRemoteGDB::clearSingleStep()
694 descheduleInstCommitEvent(&singleStepEvent
);
698 BaseRemoteGDB::setSingleStep()
700 if (!singleStepEvent
.scheduled())
701 scheduleInstCommitEvent(&singleStepEvent
, 1);
705 BaseRemoteGDB::insertSoftBreak(Addr addr
, size_t len
)
707 if (!checkBpLen(len
))
708 throw BadClient("Invalid breakpoint length\n");
710 return insertHardBreak(addr
, len
);
714 BaseRemoteGDB::removeSoftBreak(Addr addr
, size_t len
)
716 if (!checkBpLen(len
))
717 throw BadClient("Invalid breakpoint length.\n");
719 return removeHardBreak(addr
, len
);
723 BaseRemoteGDB::insertHardBreak(Addr addr
, size_t len
)
725 if (!checkBpLen(len
))
726 throw BadClient("Invalid breakpoint length\n");
728 DPRINTF(GDBMisc
, "Inserting hardware breakpoint at %#x\n", addr
);
730 HardBreakpoint
*&bkpt
= hardBreakMap
[addr
];
732 bkpt
= new HardBreakpoint(this, &sys
->pcEventQueue
, addr
);
738 BaseRemoteGDB::removeHardBreak(Addr addr
, size_t len
)
740 if (!checkBpLen(len
))
741 throw BadClient("Invalid breakpoint length\n");
743 DPRINTF(GDBMisc
, "Removing hardware breakpoint at %#x\n", addr
);
745 auto i
= hardBreakMap
.find(addr
);
746 if (i
== hardBreakMap
.end())
747 throw CmdError("E0C");
749 HardBreakpoint
*hbp
= (*i
).second
;
750 if (--hbp
->refcount
== 0) {
752 hardBreakMap
.erase(i
);
757 BaseRemoteGDB::clearTempBreakpoint(Addr
&bkpt
)
759 DPRINTF(GDBMisc
, "setTempBreakpoint: addr=%#x\n", bkpt
);
760 removeHardBreak(bkpt
, sizeof(TheISA::MachInst
));
765 BaseRemoteGDB::setTempBreakpoint(Addr bkpt
)
767 DPRINTF(GDBMisc
, "setTempBreakpoint: addr=%#x\n", bkpt
);
768 insertHardBreak(bkpt
, sizeof(TheISA::MachInst
));
772 BaseRemoteGDB::scheduleInstCommitEvent(Event
*ev
, int delta
)
774 EventQueue
*eq
= getComInstEventQueue(tc
);
775 // Here "ticks" aren't simulator ticks which measure time, they're
776 // instructions committed by the CPU.
777 eq
->schedule(ev
, eq
->getCurTick() + delta
);
781 BaseRemoteGDB::descheduleInstCommitEvent(Event
*ev
)
784 getComInstEventQueue(tc
)->deschedule(ev
);
787 std::map
<char, BaseRemoteGDB::GdbCommand
> BaseRemoteGDB::command_map
= {
789 { '?', { "KGDB_SIGNAL", &BaseRemoteGDB::cmd_signal
} },
790 // set baud (deprecated)
791 { 'b', { "KGDB_SET_BAUD", &BaseRemoteGDB::cmd_unsupported
} },
792 // set breakpoint (deprecated)
793 { 'B', { "KGDB_SET_BREAK", &BaseRemoteGDB::cmd_unsupported
} },
795 { 'c', { "KGDB_CONT", &BaseRemoteGDB::cmd_cont
} },
796 // continue with signal
797 { 'C', { "KGDB_ASYNC_CONT", &BaseRemoteGDB::cmd_async_cont
} },
798 // toggle debug flags (deprecated)
799 { 'd', { "KGDB_DEBUG", &BaseRemoteGDB::cmd_unsupported
} },
801 { 'D', { "KGDB_DETACH", &BaseRemoteGDB::cmd_detach
} },
802 // read general registers
803 { 'g', { "KGDB_REG_R", &BaseRemoteGDB::cmd_reg_r
} },
804 // write general registers
805 { 'G', { "KGDB_REG_W", &BaseRemoteGDB::cmd_reg_w
} },
807 { 'H', { "KGDB_SET_THREAD", &BaseRemoteGDB::cmd_set_thread
} },
808 // step a single cycle
809 { 'i', { "KGDB_CYCLE_STEP", &BaseRemoteGDB::cmd_unsupported
} },
810 // signal then cycle step
811 { 'I', { "KGDB_SIG_CYCLE_STEP", &BaseRemoteGDB::cmd_unsupported
} },
813 { 'k', { "KGDB_KILL", &BaseRemoteGDB::cmd_detach
} },
815 { 'm', { "KGDB_MEM_R", &BaseRemoteGDB::cmd_mem_r
} },
817 { 'M', { "KGDB_MEM_W", &BaseRemoteGDB::cmd_mem_w
} },
819 { 'p', { "KGDB_READ_REG", &BaseRemoteGDB::cmd_unsupported
} },
821 { 'P', { "KGDB_SET_REG", &BaseRemoteGDB::cmd_unsupported
} },
823 { 'q', { "KGDB_QUERY_VAR", &BaseRemoteGDB::cmd_query_var
} },
825 { 'Q', { "KGDB_SET_VAR", &BaseRemoteGDB::cmd_unsupported
} },
826 // reset system (deprecated)
827 { 'r', { "KGDB_RESET", &BaseRemoteGDB::cmd_unsupported
} },
829 { 's', { "KGDB_STEP", &BaseRemoteGDB::cmd_step
} },
831 { 'S', { "KGDB_ASYNC_STEP", &BaseRemoteGDB::cmd_async_step
} },
832 // find out if the thread is alive
833 { 'T', { "KGDB_THREAD_ALIVE", &BaseRemoteGDB::cmd_unsupported
} },
835 { 'W', { "KGDB_TARGET_EXIT", &BaseRemoteGDB::cmd_unsupported
} },
837 { 'X', { "KGDB_BINARY_DLOAD", &BaseRemoteGDB::cmd_unsupported
} },
838 // remove breakpoint or watchpoint
839 { 'z', { "KGDB_CLR_HW_BKPT", &BaseRemoteGDB::cmd_clr_hw_bkpt
} },
840 // insert breakpoint or watchpoint
841 { 'Z', { "KGDB_SET_HW_BKPT", &BaseRemoteGDB::cmd_set_hw_bkpt
} },
845 BaseRemoteGDB::checkBpLen(size_t len
)
847 return len
== sizeof(MachInst
);
851 BaseRemoteGDB::cmd_unsupported(GdbCommand::Context
&ctx
)
853 DPRINTF(GDBMisc
, "Unsupported command: %s\n", ctx
.cmd
->name
);
854 DDUMP(GDBMisc
, ctx
.data
, ctx
.len
);
860 BaseRemoteGDB::cmd_signal(GdbCommand::Context
&ctx
)
862 send(csprintf("S%02x", ctx
.type
).c_str());
867 BaseRemoteGDB::cmd_cont(GdbCommand::Context
&ctx
)
869 const char *p
= ctx
.data
;
871 Addr newPc
= hex2i(&p
);
879 BaseRemoteGDB::cmd_async_cont(GdbCommand::Context
&ctx
)
881 const char *p
= ctx
.data
;
884 Addr newPc
= hex2i(&p
);
892 BaseRemoteGDB::cmd_detach(GdbCommand::Context
&ctx
)
899 BaseRemoteGDB::cmd_reg_r(GdbCommand::Context
&ctx
)
901 char buf
[2 * regCachePtr
->size() + 1];
902 buf
[2 * regCachePtr
->size()] = '\0';
903 mem2hex(buf
, regCachePtr
->data(), regCachePtr
->size());
909 BaseRemoteGDB::cmd_reg_w(GdbCommand::Context
&ctx
)
911 const char *p
= ctx
.data
;
912 p
= hex2mem(regCachePtr
->data(), p
, regCachePtr
->size());
913 if (p
== NULL
|| *p
!= '\0')
914 throw CmdError("E01");
916 regCachePtr
->setRegs(tc
);
923 BaseRemoteGDB::cmd_set_thread(GdbCommand::Context
&ctx
)
925 const char *p
= ctx
.data
+ 1; // Ignore the subcommand byte.
927 throw CmdError("E01");
933 BaseRemoteGDB::cmd_mem_r(GdbCommand::Context
&ctx
)
935 const char *p
= ctx
.data
;
936 Addr addr
= hex2i(&p
);
938 throw CmdError("E02");
939 size_t len
= hex2i(&p
);
941 throw CmdError("E03");
943 throw CmdError("E05");
946 if (!read(addr
, len
, buf
))
947 throw CmdError("E05");
949 char temp
[2 * len
+ 1];
950 temp
[2 * len
] = '\0';
951 mem2hex(temp
, buf
, len
);
957 BaseRemoteGDB::cmd_mem_w(GdbCommand::Context
&ctx
)
959 const char *p
= ctx
.data
;
960 Addr addr
= hex2i(&p
);
962 throw CmdError("E06");
963 size_t len
= hex2i(&p
);
965 throw CmdError("E07");
966 if (len
* 2 > ctx
.len
- (p
- ctx
.data
))
967 throw CmdError("E08");
969 p
= (char *)hex2mem(buf
, p
, len
);
971 throw CmdError("E09");
973 throw CmdError("E0A");
974 if (!write(addr
, len
, buf
))
975 throw CmdError("E0B");
981 BaseRemoteGDB::cmd_query_var(GdbCommand::Context
&ctx
)
983 if (string(ctx
.data
, ctx
.len
- 1) != "C")
990 BaseRemoteGDB::cmd_async_step(GdbCommand::Context
&ctx
)
992 const char *p
= ctx
.data
;
993 hex2i(&p
); // Ignore the subcommand byte.
995 Addr newPc
= hex2i(&p
);
1003 BaseRemoteGDB::cmd_step(GdbCommand::Context
&ctx
)
1006 const char *p
= ctx
.data
;
1007 Addr newPc
= hex2i(&p
);
1015 BaseRemoteGDB::cmd_clr_hw_bkpt(GdbCommand::Context
&ctx
)
1017 const char *p
= ctx
.data
;
1020 throw CmdError("E0D");
1021 Addr addr
= hex2i(&p
);
1023 throw CmdError("E0D");
1024 size_t len
= hex2i(&p
);
1026 DPRINTF(GDBMisc
, "clear %s, addr=%#x, len=%d\n",
1027 break_type(subcmd
), addr
, len
);
1031 removeSoftBreak(addr
, len
);
1034 removeHardBreak(addr
, len
);
1040 throw Unsupported();
1048 BaseRemoteGDB::cmd_set_hw_bkpt(GdbCommand::Context
&ctx
)
1050 const char *p
= ctx
.data
;
1053 throw CmdError("E0D");
1054 Addr addr
= hex2i(&p
);
1056 throw CmdError("E0D");
1057 size_t len
= hex2i(&p
);
1059 DPRINTF(GDBMisc
, "set %s, addr=%#x, len=%d\n",
1060 break_type(subcmd
), addr
, len
);
1064 insertSoftBreak(addr
, len
);
1067 insertHardBreak(addr
, len
);
1073 throw Unsupported();