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.
30 * Copyright (c) 1990, 1993
31 * The Regents of the University of California. All rights reserved.
33 * This software was developed by the Computer Systems Engineering group
34 * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
35 * contributed to Berkeley.
37 * All advertising materials mentioning features or use of this software
38 * must display the following acknowledgement:
39 * This product includes software developed by the University of
40 * California, Lawrence Berkeley Laboratories.
42 * Redistribution and use in source and binary forms, with or without
43 * modification, are permitted provided that the following conditions
45 * 1. Redistributions of source code must retain the above copyright
46 * notice, this list of conditions and the following disclaimer.
47 * 2. Redistributions in binary form must reproduce the above copyright
48 * notice, this list of conditions and the following disclaimer in the
49 * documentation and/or other materials provided with the distribution.
50 * 3. All advertising materials mentioning features or use of this software
51 * must display the following acknowledgement:
52 * This product includes software developed by the University of
53 * California, Berkeley and its contributors.
54 * 4. Neither the name of the University nor the names of its contributors
55 * may be used to endorse or promote products derived from this software
56 * without specific prior written permission.
58 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
59 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
60 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
61 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
62 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
63 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
64 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
65 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
66 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
67 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
70 * @(#)kgdb_stub.c 8.4 (Berkeley) 1/12/94
74 * Copyright (c) 2001 The NetBSD Foundation, Inc.
75 * All rights reserved.
77 * This code is derived from software contributed to The NetBSD Foundation
80 * Redistribution and use in source and binary forms, with or without
81 * modification, are permitted provided that the following conditions
83 * 1. Redistributions of source code must retain the above copyright
84 * notice, this list of conditions and the following disclaimer.
85 * 2. Redistributions in binary form must reproduce the above copyright
86 * notice, this list of conditions and the following disclaimer in the
87 * documentation and/or other materials provided with the distribution.
88 * 3. All advertising materials mentioning features or use of this software
89 * must display the following acknowledgement:
90 * This product includes software developed by the NetBSD
91 * Foundation, Inc. and its contributors.
92 * 4. Neither the name of The NetBSD Foundation nor the names of its
93 * contributors may be used to endorse or promote products derived
94 * from this software without specific prior written permission.
96 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
97 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
98 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
99 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
100 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
101 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
102 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
103 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
104 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
105 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
106 * POSSIBILITY OF SUCH DAMAGE.
110 * $NetBSD: kgdb_stub.c,v 1.8 2001/07/07 22:58:00 wdk Exp $
114 * "Stub" to allow remote cpu to debug over a serial line using gdb.
117 #include <sys/signal.h>
123 #include "arch/vtophys.hh"
124 #include "base/intmath.hh"
125 #include "base/kgdb.h"
126 #include "base/remote_gdb.hh"
127 #include "base/socket.hh"
128 #include "base/trace.hh"
129 #include "config/full_system.hh"
130 #include "cpu/exec_context.hh"
131 #include "cpu/static_inst.hh"
132 #include "mem/physical.hh"
133 #include "mem/port.hh"
134 #include "sim/system.hh"
137 using namespace TheISA
;
140 vector
<RemoteGDB
*> debuggers
;
141 int current_debugger
= -1;
146 if (current_debugger
>= 0 && current_debugger
< debuggers
.size()) {
147 RemoteGDB
*gdb
= debuggers
[current_debugger
];
148 if (!gdb
->isattached())
149 gdb
->listener
->accept();
150 if (gdb
->isattached())
151 gdb
->trap(ALPHA_KENTRY_IF
);
156 ///////////////////////////////////////////////////////////
161 GDBListener::Event::Event(GDBListener
*l
, int fd
, int e
)
162 : PollEvent(fd
, e
), listener(l
)
166 GDBListener::Event::process(int revent
)
171 GDBListener::GDBListener(RemoteGDB
*g
, int p
)
172 : event(NULL
), gdb(g
), port(p
)
174 assert(!gdb
->listener
);
175 gdb
->listener
= this;
178 GDBListener::~GDBListener()
187 return gdb
->name() + ".listener";
191 GDBListener::listen()
193 while (!listener
.listen(port
, true)) {
194 DPRINTF(GDBMisc
, "Can't bind port %d\n", port
);
198 event
= new Event(this, listener
.getfd(), POLLIN
);
199 pollQueue
.schedule(event
);
202 gdb
->number
= debuggers
.size();
203 debuggers
.push_back(gdb
);
207 ccprintf(cerr
, "%d: %s: listening for remote gdb #%d on port %d\n",
208 curTick
, name(), gdb
->number
, port
);
210 ccprintf(cerr
, "%d: %s: listening for remote gdb on port %d\n",
211 curTick
, name(), port
);
216 GDBListener::accept()
218 if (!listener
.islistening())
219 panic("GDBListener::accept(): cannot accept if we're not listening!");
221 int sfd
= listener
.accept(true);
224 if (gdb
->isattached())
231 ///////////////////////////////////////////////////////////
237 void mem2hex(void *, const void *, int);
238 const char *hex2mem(void *, const char *, int);
239 Addr
hex2i(const char **);
241 RemoteGDB::Event::Event(RemoteGDB
*g
, int fd
, int e
)
242 : PollEvent(fd
, e
), gdb(g
)
246 RemoteGDB::Event::process(int revent
)
249 gdb
->trap(ALPHA_KENTRY_IF
);
250 else if (revent
& POLLNVAL
)
254 RemoteGDB::RemoteGDB(System
*_system
, ExecContext
*c
)
255 : event(NULL
), listener(NULL
), number(-1), fd(-1),
256 active(false), attached(false),
257 system(_system
), pmem(_system
->physmem
), context(c
)
259 memset(gdbregs
, 0, sizeof(gdbregs
));
262 RemoteGDB::~RemoteGDB()
271 return system
->name() + ".remote_gdb";
275 RemoteGDB::isattached()
279 RemoteGDB::attach(int f
)
283 event
= new Event(this, fd
, POLLIN
);
284 pollQueue
.schedule(event
);
287 DPRINTFN("remote gdb attached\n");
297 pollQueue
.remove(event
);
298 DPRINTFN("remote gdb detached\n");
302 gdb_command(char cmd
)
305 case KGDB_SIGNAL
: return "KGDB_SIGNAL";
306 case KGDB_SET_BAUD
: return "KGDB_SET_BAUD";
307 case KGDB_SET_BREAK
: return "KGDB_SET_BREAK";
308 case KGDB_CONT
: return "KGDB_CONT";
309 case KGDB_ASYNC_CONT
: return "KGDB_ASYNC_CONT";
310 case KGDB_DEBUG
: return "KGDB_DEBUG";
311 case KGDB_DETACH
: return "KGDB_DETACH";
312 case KGDB_REG_R
: return "KGDB_REG_R";
313 case KGDB_REG_W
: return "KGDB_REG_W";
314 case KGDB_SET_THREAD
: return "KGDB_SET_THREAD";
315 case KGDB_CYCLE_STEP
: return "KGDB_CYCLE_STEP";
316 case KGDB_SIG_CYCLE_STEP
: return "KGDB_SIG_CYCLE_STEP";
317 case KGDB_KILL
: return "KGDB_KILL";
318 case KGDB_MEM_W
: return "KGDB_MEM_W";
319 case KGDB_MEM_R
: return "KGDB_MEM_R";
320 case KGDB_SET_REG
: return "KGDB_SET_REG";
321 case KGDB_READ_REG
: return "KGDB_READ_REG";
322 case KGDB_QUERY_VAR
: return "KGDB_QUERY_VAR";
323 case KGDB_SET_VAR
: return "KGDB_SET_VAR";
324 case KGDB_RESET
: return "KGDB_RESET";
325 case KGDB_STEP
: return "KGDB_STEP";
326 case KGDB_ASYNC_STEP
: return "KGDB_ASYNC_STEP";
327 case KGDB_THREAD_ALIVE
: return "KGDB_THREAD_ALIVE";
328 case KGDB_TARGET_EXIT
: return "KGDB_TARGET_EXIT";
329 case KGDB_BINARY_DLOAD
: return "KGDB_BINARY_DLOAD";
330 case KGDB_CLR_HW_BKPT
: return "KGDB_CLR_HW_BKPT";
331 case KGDB_SET_HW_BKPT
: return "KGDB_SET_HW_BKPT";
332 case KGDB_START
: return "KGDB_START";
333 case KGDB_END
: return "KGDB_END";
334 case KGDB_GOODP
: return "KGDB_GOODP";
335 case KGDB_BADP
: return "KGDB_BADP";
336 default: return "KGDB_UNKNOWN";
340 ///////////////////////////////////////////////////////////
343 // Determine if the mapping at va..(va+len) is valid.
346 RemoteGDB::acc(Addr va
, size_t len
)
350 va
= TheISA::TruncPage(va
);
351 last_va
= TheISA::RoundPage(va
+ len
);
354 if (TheISA::IsK0Seg(va
)) {
355 if (va
< (TheISA::K0SegBase
+ pmem
->size())) {
356 DPRINTF(GDBAcc
, "acc: Mapping is valid K0SEG <= "
357 "%#x < K0SEG + size\n", va
);
360 DPRINTF(GDBAcc
, "acc: Mapping invalid %#x > K0SEG + size\n",
367 * This code says that all accesses to palcode (instruction and data)
368 * are valid since there isn't a va->pa mapping because palcode is
369 * accessed physically. At some point this should probably be cleaned up
370 * but there is no easy way to do it.
373 if (AlphaISA::PcPAL(va
) || va
< 0x10000)
376 Addr ptbr
= context
->readMiscReg(AlphaISA::IPR_PALtemp20
);
377 TheISA::PageTableEntry pte
= TheISA::kernel_pte_lookup(context
->getPhysPort(), ptbr
, va
);
379 DPRINTF(GDBAcc
, "acc: %#x pte is invalid\n", va
);
382 va
+= TheISA::PageBytes
;
383 } while (va
< last_va
);
385 DPRINTF(GDBAcc
, "acc: %#x mapping is valid\n", va
);
389 ///////////////////////////////////////////////////////////
392 // Translate a trap number into a Unix-compatible signal number.
393 // (GDB only understands Unix signal numbers.)
396 RemoteGDB::signal(int type
)
399 case ALPHA_KENTRY_INT
:
402 case ALPHA_KENTRY_UNA
:
405 case ALPHA_KENTRY_ARITH
:
408 case ALPHA_KENTRY_IF
:
411 case ALPHA_KENTRY_MM
:
415 panic("unknown signal type");
420 ///////////////////////////////////////////////////////////
421 // RemoteGDB::getregs
423 // Translate the kernel debugger register format into
424 // the GDB register format.
428 memset(gdbregs
, 0, sizeof(gdbregs
));
430 gdbregs
[KGDB_REG_PC
] = context
->readPC();
432 // @todo: Currently this is very Alpha specific.
433 if (AlphaISA::PcPAL(gdbregs
[KGDB_REG_PC
])) {
434 for (int i
= 0; i
< TheISA::NumIntArchRegs
; ++i
) {
435 gdbregs
[i
] = context
->readIntReg(AlphaISA::reg_redir
[i
]);
438 for (int i
= 0; i
< TheISA::NumIntArchRegs
; ++i
) {
439 gdbregs
[i
] = context
->readIntReg(i
);
444 for (int i
= 0; i
< TheISA::NumFloatArchRegs
; ++i
) {
445 gdbregs
[i
+ KGDB_REG_F0
] = context
->readFloatRegBits(i
);
450 ///////////////////////////////////////////////////////////
451 // RemoteGDB::setregs
453 // Translate the GDB register format into the kernel
454 // debugger register format.
459 // @todo: Currently this is very Alpha specific.
460 if (AlphaISA::PcPAL(gdbregs
[KGDB_REG_PC
])) {
461 for (int i
= 0; i
< TheISA::NumIntArchRegs
; ++i
) {
462 context
->setIntReg(AlphaISA::reg_redir
[i
], gdbregs
[i
]);
465 for (int i
= 0; i
< TheISA::NumIntArchRegs
; ++i
) {
466 context
->setIntReg(i
, gdbregs
[i
]);
471 for (int i
= 0; i
< TheISA::NumFloatArchRegs
; ++i
) {
472 context
->setFloatRegBits(i
, gdbregs
[i
+ KGDB_REG_F0
]);
475 context
->setPC(gdbregs
[KGDB_REG_PC
]);
479 RemoteGDB::setTempBreakpoint(TempBreakpoint
&bkpt
, Addr addr
)
481 DPRINTF(GDBMisc
, "setTempBreakpoint: addr=%#x\n", addr
);
484 insertHardBreak(addr
, 4);
488 RemoteGDB::clearTempBreakpoint(TempBreakpoint
&bkpt
)
490 DPRINTF(GDBMisc
, "setTempBreakpoint: addr=%#x\n",
494 removeHardBreak(bkpt
.address
, 4);
499 RemoteGDB::clearSingleStep()
501 DPRINTF(GDBMisc
, "clearSingleStep bt_addr=%#x nt_addr=%#x\n",
502 takenBkpt
.address
, notTakenBkpt
.address
);
504 if (takenBkpt
.address
!= 0)
505 clearTempBreakpoint(takenBkpt
);
507 if (notTakenBkpt
.address
!= 0)
508 clearTempBreakpoint(notTakenBkpt
);
512 RemoteGDB::setSingleStep()
514 Addr pc
= context
->readPC();
518 npc
= pc
+ sizeof(MachInst
);
520 // User was stopped at pc, e.g. the instruction at pc was not
522 MachInst inst
= read
<MachInst
>(pc
);
523 StaticInstPtr
si(inst
);
524 if (si
->hasBranchTarget(pc
, context
, bpc
)) {
525 // Don't bother setting a breakpoint on the taken branch if it
526 // is the same as the next pc
531 DPRINTF(GDBMisc
, "setSingleStep bt_addr=%#x nt_addr=%#x\n",
532 takenBkpt
.address
, notTakenBkpt
.address
);
534 setTempBreakpoint(notTakenBkpt
, npc
);
537 setTempBreakpoint(takenBkpt
, bpc
);
540 /////////////////////////
553 RemoteGDB::putbyte(uint8_t b
)
558 // Send a packet to gdb
560 RemoteGDB::send(const char *bp
)
565 DPRINTF(GDBSend
, "send: %s\n", bp
);
570 for (csum
= 0; (c
= *p
); p
++) {
575 putbyte(i2digit(csum
>> 4));
576 putbyte(i2digit(csum
));
577 } while ((c
= getbyte() & 0x7f) == KGDB_BADP
);
580 // Receive a packet from gdb
582 RemoteGDB::recv(char *bp
, int maxlen
)
591 while ((c
= getbyte()) != KGDB_START
)
594 while ((c
= getbyte()) != KGDB_END
&& len
< maxlen
) {
608 csum
-= digit2i(getbyte()) * 16;
609 csum
-= digit2i(getbyte());
618 bcopy(bp
+ 3, bp
, len
);
625 DPRINTF(GDBRecv
, "recv: %s: %s\n", gdb_command(*bp
), bp
);
630 // Read bytes from kernel address space for debugger.
632 RemoteGDB::read(Addr vaddr
, size_t size
, char *data
)
634 static Addr lastaddr
= 0;
635 static size_t lastsize
= 0;
638 DPRINTF(GDBRead
, "read: reading memory location zero!\n");
639 vaddr
= lastaddr
+ lastsize
;
642 DPRINTF(GDBRead
, "read: addr=%#x, size=%d", vaddr
, size
);
644 context
->getVirtPort(context
)->readBlob(vaddr
, (uint8_t*)data
, size
);
647 if (DTRACE(GDBRead
)) {
648 if (DTRACE(GDBExtra
)) {
650 mem2hex(buf
, data
, size
);
651 DPRINTFNR(": %s\n", buf
);
660 // Write bytes to kernel address space for debugger.
662 RemoteGDB::write(Addr vaddr
, size_t size
, const char *data
)
664 static Addr lastaddr
= 0;
665 static size_t lastsize
= 0;
668 DPRINTF(GDBWrite
, "write: writing memory location zero!\n");
669 vaddr
= lastaddr
+ lastsize
;
672 if (DTRACE(GDBWrite
)) {
673 DPRINTFN("write: addr=%#x, size=%d", vaddr
, size
);
674 if (DTRACE(GDBExtra
)) {
676 mem2hex(buf
, data
, size
);
677 DPRINTFNR(": %s\n", buf
);
682 context
->getVirtPort(context
)->writeBlob(vaddr
, (uint8_t*)data
, size
);
692 PCEventQueue
*RemoteGDB::getPcEventQueue()
694 return &system
->pcEventQueue
;
698 RemoteGDB::HardBreakpoint::HardBreakpoint(RemoteGDB
*_gdb
, Addr pc
)
699 : PCEvent(_gdb
->getPcEventQueue(), "HardBreakpoint Event", pc
),
700 gdb(_gdb
), refcount(0)
702 DPRINTF(GDBMisc
, "creating hardware breakpoint at %#x\n", evpc
);
706 RemoteGDB::HardBreakpoint::process(ExecContext
*xc
)
708 DPRINTF(GDBMisc
, "handling hardware breakpoint at %#x\n", pc());
710 if (xc
== gdb
->context
)
711 gdb
->trap(ALPHA_KENTRY_INT
);
715 RemoteGDB::insertSoftBreak(Addr addr
, size_t len
)
717 if (len
!= sizeof(MachInst
))
718 panic("invalid length\n");
720 return insertHardBreak(addr
, len
);
724 RemoteGDB::removeSoftBreak(Addr addr
, size_t len
)
726 if (len
!= sizeof(MachInst
))
727 panic("invalid length\n");
729 return removeHardBreak(addr
, len
);
733 RemoteGDB::insertHardBreak(Addr addr
, size_t len
)
735 if (len
!= sizeof(MachInst
))
736 panic("invalid length\n");
738 DPRINTF(GDBMisc
, "inserting hardware breakpoint at %#x\n", addr
);
740 HardBreakpoint
*&bkpt
= hardBreakMap
[addr
];
742 bkpt
= new HardBreakpoint(this, addr
);
750 RemoteGDB::removeHardBreak(Addr addr
, size_t len
)
752 if (len
!= sizeof(MachInst
))
753 panic("invalid length\n");
755 DPRINTF(GDBMisc
, "removing hardware breakpoint at %#x\n", addr
);
757 break_iter_t i
= hardBreakMap
.find(addr
);
758 if (i
== hardBreakMap
.end())
761 HardBreakpoint
*hbp
= (*i
).second
;
762 if (--hbp
->refcount
== 0) {
764 hardBreakMap
.erase(i
);
774 case '0': return "software breakpoint";
775 case '1': return "hardware breakpoint";
776 case '2': return "write watchpoint";
777 case '3': return "read watchpoint";
778 case '4': return "access watchpoint";
779 default: return "unknown breakpoint/watchpoint";
783 // This function does all command processing for interfacing to a
784 // remote gdb. Note that the error codes are ignored by gdb at
785 // present, but might eventually become meaningful. (XXX) It might
786 // makes sense to use POSIX errno values, because that is what the
787 // gdb/remote.c functions want to return.
789 RemoteGDB::trap(int type
)
793 char data
[KGDB_BUFLEN
+ 1];
794 char buffer
[sizeof(gdbregs
) * 2 + 256];
795 char temp
[KGDB_BUFLEN
];
797 char command
, subcmd
;
804 DPRINTF(GDBMisc
, "trap: PC=%#x NPC=%#x\n",
805 context
->readPC(), context
->readNextPC());
810 * The first entry to this function is normally through
811 * a breakpoint trap in kgdb_connect(), in which case we
812 * must advance past the breakpoint because gdb will not.
814 * On the first entry here, we expect that gdb is not yet
815 * listening to us, so just enter the interaction loop.
816 * After the debugger is "active" (connected) it will be
817 * waiting for a "signaled" message from us.
822 // Tell remote host that an exception has occurred.
823 snprintf((char *)buffer
, sizeof(buffer
), "S%02x", signal(type
));
826 // Stick frame regs into our reg cache.
830 datalen
= recv(data
, sizeof(data
));
831 data
[sizeof(data
) - 1] = 0; // Sentinel
838 // if this command came from a running gdb, answer it --
839 // the other guy has no way of knowing if we're in or out
840 // of this loop when he issues a "remote-signal".
841 snprintf((char *)buffer
, sizeof(buffer
), "S%02x", signal(type
));
846 if (2 * sizeof(gdbregs
) > sizeof(buffer
))
847 panic("buffer too small");
849 mem2hex(buffer
, gdbregs
, sizeof(gdbregs
));
854 p
= hex2mem(gdbregs
, p
, sizeof(gdbregs
));
855 if (p
== NULL
|| *p
!= '\0')
870 if (val
< 0 && val
>= KGDB_NUMREGS
) {
875 gdbregs
[val
] = hex2i(&p
);
893 if (len
> sizeof(buffer
)) {
897 if (!acc(val
, len
)) {
902 if (read(val
, (size_t)len
, (char *)buffer
)) {
903 mem2hex(temp
, buffer
, len
);
921 if (len
> datalen
- (p
- data
)) {
925 p
= hex2mem(buffer
, p
, sizeof(buffer
));
930 if (!acc(val
, len
)) {
934 if (write(val
, (size_t)len
, (char *)buffer
))
940 case KGDB_SET_THREAD
:
956 case KGDB_ASYNC_CONT
:
961 context
->setNextPC(val
+ sizeof(MachInst
));
967 if (p
- data
< datalen
) {
970 context
->setNextPC(val
+ sizeof(MachInst
));
975 case KGDB_ASYNC_STEP
:
980 context
->setNextPC(val
+ sizeof(MachInst
));
986 if (p
- data
< datalen
) {
989 context
->setNextPC(val
+ sizeof(MachInst
));
994 case KGDB_CLR_HW_BKPT
:
996 if (*p
++ != ',') send("E0D");
998 if (*p
++ != ',') send("E0D");
1001 DPRINTF(GDBMisc
, "clear %s, addr=%#x, len=%d\n",
1002 break_type(subcmd
), val
, len
);
1007 case '0': // software breakpoint
1008 ret
= removeSoftBreak(val
, len
);
1011 case '1': // hardware breakpoint
1012 ret
= removeHardBreak(val
, len
);
1015 case '2': // write watchpoint
1016 case '3': // read watchpoint
1017 case '4': // access watchpoint
1023 send(ret
? "OK" : "E0C");
1026 case KGDB_SET_HW_BKPT
:
1028 if (*p
++ != ',') send("E0D");
1030 if (*p
++ != ',') send("E0D");
1033 DPRINTF(GDBMisc
, "set %s, addr=%#x, len=%d\n",
1034 break_type(subcmd
), val
, len
);
1039 case '0': // software breakpoint
1040 ret
= insertSoftBreak(val
, len
);
1043 case '1': // hardware breakpoint
1044 ret
= insertHardBreak(val
, len
);
1047 case '2': // write watchpoint
1048 case '3': // read watchpoint
1049 case '4': // access watchpoint
1055 send(ret
? "OK" : "E0C");
1058 case KGDB_QUERY_VAR
:
1059 var
= string(p
, datalen
- 1);
1067 case KGDB_SET_BREAK
:
1069 case KGDB_CYCLE_STEP
:
1070 case KGDB_SIG_CYCLE_STEP
:
1074 case KGDB_THREAD_ALIVE
:
1075 case KGDB_TARGET_EXIT
:
1076 case KGDB_BINARY_DLOAD
:
1077 // Unsupported command
1078 DPRINTF(GDBMisc
, "Unsupported command: %s\n",
1079 gdb_command(command
));
1080 DDUMP(GDBMisc
, (uint8_t *)data
, datalen
);
1086 DPRINTF(GDBMisc
, "Unknown command: %c(%#x)\n",
1099 // Convert a hex digit into an integer.
1100 // This returns -1 if the argument passed is no valid hex digit.
1104 if (c
>= '0' && c
<= '9')
1106 else if (c
>= 'a' && c
<= 'f')
1107 return (c
- 'a' + 10);
1108 else if (c
>= 'A' && c
<= 'F')
1110 return (c
- 'A' + 10);
1115 // Convert the low 4 bits of an integer into an hex digit.
1119 return ("0123456789abcdef"[n
& 0x0f]);
1122 // Convert a byte array into an hex string.
1124 mem2hex(void *vdst
, const void *vsrc
, int len
)
1126 char *dst
= (char *)vdst
;
1127 const char *src
= (const char *)vsrc
;
1130 *dst
++ = i2digit(*src
>> 4);
1131 *dst
++ = i2digit(*src
++);
1136 // Convert an hex string into a byte array.
1137 // This returns a pointer to the character following the last valid
1138 // hex digit. If the string ends in the middle of a byte, NULL is
1141 hex2mem(void *vdst
, const char *src
, int maxlen
)
1143 char *dst
= (char *)vdst
;
1146 while (*src
&& maxlen
--) {
1147 msb
= digit2i(*src
++);
1150 lsb
= digit2i(*src
++);
1153 *dst
++ = (msb
<< 4) | lsb
;
1158 // Convert an hex string into an integer.
1159 // This returns a pointer to the character following the last valid
1162 hex2i(const char **srcp
)
1164 const char *src
= *srcp
;
1168 while ((nibble
= digit2i(*src
)) >= 0) {