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 "base/intmath.hh"
124 #include "base/kgdb.h"
125 #include "base/remote_gdb.hh"
126 #include "base/socket.hh"
127 #include "base/trace.hh"
128 #include "cpu/exec_context.hh"
129 #include "cpu/static_inst.hh"
130 #include "mem/functional/physical.hh"
131 #include "sim/system.hh"
132 #include "targetarch/vtophys.hh"
137 vector
<RemoteGDB
*> debuggers
;
138 int current_debugger
= -1;
143 if (current_debugger
>= 0 && current_debugger
< debuggers
.size()) {
144 RemoteGDB
*gdb
= debuggers
[current_debugger
];
145 if (!gdb
->isattached())
146 gdb
->listener
->accept();
147 if (gdb
->isattached())
148 gdb
->trap(ALPHA_KENTRY_IF
);
153 ///////////////////////////////////////////////////////////
158 GDBListener::Event::Event(GDBListener
*l
, int fd
, int e
)
159 : PollEvent(fd
, e
), listener(l
)
163 GDBListener::Event::process(int revent
)
168 GDBListener::GDBListener(RemoteGDB
*g
, int p
)
169 : event(NULL
), gdb(g
), port(p
)
171 assert(!gdb
->listener
);
172 gdb
->listener
= this;
175 GDBListener::~GDBListener()
184 return gdb
->name() + ".listener";
188 GDBListener::listen()
190 while (!listener
.listen(port
, true)) {
191 DPRINTF(GDBMisc
, "Can't bind port %d\n", port
);
195 event
= new Event(this, listener
.getfd(), POLLIN
);
196 pollQueue
.schedule(event
);
199 gdb
->number
= debuggers
.size();
200 debuggers
.push_back(gdb
);
204 ccprintf(cerr
, "%d: %s: listening for remote gdb #%d on port %d\n",
205 curTick
, name(), gdb
->number
, port
);
207 ccprintf(cerr
, "%d: %s: listening for remote gdb on port %d\n",
208 curTick
, name(), port
);
213 GDBListener::accept()
215 if (!listener
.islistening())
216 panic("GDBListener::accept(): cannot accept if we're not listening!");
218 int sfd
= listener
.accept(true);
221 if (gdb
->isattached())
228 ///////////////////////////////////////////////////////////
234 void mem2hex(void *, const void *, int);
235 const char *hex2mem(void *, const char *, int);
236 Addr
hex2i(const char **);
238 RemoteGDB::Event::Event(RemoteGDB
*g
, int fd
, int e
)
239 : PollEvent(fd
, e
), gdb(g
)
243 RemoteGDB::Event::process(int revent
)
246 gdb
->trap(ALPHA_KENTRY_IF
);
247 else if (revent
& POLLNVAL
)
251 RemoteGDB::RemoteGDB(System
*_system
, ExecContext
*c
)
252 : event(NULL
), listener(NULL
), number(-1), fd(-1),
253 active(false), attached(false),
254 system(_system
), pmem(_system
->physmem
), context(c
)
256 memset(gdbregs
, 0, sizeof(gdbregs
));
259 RemoteGDB::~RemoteGDB()
268 return system
->name() + ".remote_gdb";
272 RemoteGDB::isattached()
276 RemoteGDB::attach(int f
)
280 event
= new Event(this, fd
, POLLIN
);
281 pollQueue
.schedule(event
);
284 DPRINTFN("remote gdb attached\n");
294 pollQueue
.remove(event
);
295 DPRINTFN("remote gdb detached\n");
299 gdb_command(char cmd
)
302 case KGDB_SIGNAL
: return "KGDB_SIGNAL";
303 case KGDB_SET_BAUD
: return "KGDB_SET_BAUD";
304 case KGDB_SET_BREAK
: return "KGDB_SET_BREAK";
305 case KGDB_CONT
: return "KGDB_CONT";
306 case KGDB_ASYNC_CONT
: return "KGDB_ASYNC_CONT";
307 case KGDB_DEBUG
: return "KGDB_DEBUG";
308 case KGDB_DETACH
: return "KGDB_DETACH";
309 case KGDB_REG_R
: return "KGDB_REG_R";
310 case KGDB_REG_W
: return "KGDB_REG_W";
311 case KGDB_SET_THREAD
: return "KGDB_SET_THREAD";
312 case KGDB_CYCLE_STEP
: return "KGDB_CYCLE_STEP";
313 case KGDB_SIG_CYCLE_STEP
: return "KGDB_SIG_CYCLE_STEP";
314 case KGDB_KILL
: return "KGDB_KILL";
315 case KGDB_MEM_W
: return "KGDB_MEM_W";
316 case KGDB_MEM_R
: return "KGDB_MEM_R";
317 case KGDB_SET_REG
: return "KGDB_SET_REG";
318 case KGDB_READ_REG
: return "KGDB_READ_REG";
319 case KGDB_QUERY_VAR
: return "KGDB_QUERY_VAR";
320 case KGDB_SET_VAR
: return "KGDB_SET_VAR";
321 case KGDB_RESET
: return "KGDB_RESET";
322 case KGDB_STEP
: return "KGDB_STEP";
323 case KGDB_ASYNC_STEP
: return "KGDB_ASYNC_STEP";
324 case KGDB_THREAD_ALIVE
: return "KGDB_THREAD_ALIVE";
325 case KGDB_TARGET_EXIT
: return "KGDB_TARGET_EXIT";
326 case KGDB_BINARY_DLOAD
: return "KGDB_BINARY_DLOAD";
327 case KGDB_CLR_HW_BKPT
: return "KGDB_CLR_HW_BKPT";
328 case KGDB_SET_HW_BKPT
: return "KGDB_SET_HW_BKPT";
329 case KGDB_START
: return "KGDB_START";
330 case KGDB_END
: return "KGDB_END";
331 case KGDB_GOODP
: return "KGDB_GOODP";
332 case KGDB_BADP
: return "KGDB_BADP";
333 default: return "KGDB_UNKNOWN";
337 ///////////////////////////////////////////////////////////
340 // Determine if the mapping at va..(va+len) is valid.
343 RemoteGDB::acc(Addr va
, size_t len
)
347 va
= TheISA::TruncPage(va
);
348 last_va
= TheISA::RoundPage(va
+ len
);
351 if (TheISA::IsK0Seg(va
)) {
352 if (va
< (TheISA::K0SegBase
+ pmem
->size())) {
353 DPRINTF(GDBAcc
, "acc: Mapping is valid K0SEG <= "
354 "%#x < K0SEG + size\n", va
);
357 DPRINTF(GDBAcc
, "acc: Mapping invalid %#x > K0SEG + size\n",
364 * This code says that all accesses to palcode (instruction and data)
365 * are valid since there isn't a va->pa mapping because palcode is
366 * accessed physically. At some point this should probably be cleaned up
367 * but there is no easy way to do it.
370 if (AlphaISA::PcPAL(va
) || va
< 0x10000)
373 Addr ptbr
= context
->regs
.ipr
[AlphaISA::IPR_PALtemp20
];
374 TheISA::PageTableEntry pte
= kernel_pte_lookup(pmem
, ptbr
, va
);
376 DPRINTF(GDBAcc
, "acc: %#x pte is invalid\n", va
);
379 va
+= TheISA::PageBytes
;
380 } while (va
< last_va
);
382 DPRINTF(GDBAcc
, "acc: %#x mapping is valid\n", va
);
386 ///////////////////////////////////////////////////////////
389 // Translate a trap number into a Unix-compatible signal number.
390 // (GDB only understands Unix signal numbers.)
393 RemoteGDB::signal(int type
)
396 case ALPHA_KENTRY_INT
:
399 case ALPHA_KENTRY_UNA
:
402 case ALPHA_KENTRY_ARITH
:
405 case ALPHA_KENTRY_IF
:
408 case ALPHA_KENTRY_MM
:
412 panic("unknown signal type");
417 ///////////////////////////////////////////////////////////
418 // RemoteGDB::getregs
420 // Translate the kernel debugger register format into
421 // the GDB register format.
425 memset(gdbregs
, 0, sizeof(gdbregs
));
426 memcpy(&gdbregs
[KGDB_REG_V0
], context
->regs
.intRegFile
, 32 * sizeof(uint64_t));
428 memcpy(&gdbregs
[KGDB_REG_F0
], context
->regs
.floatRegFile
.q
,
429 32 * sizeof(uint64_t));
431 gdbregs
[KGDB_REG_PC
] = context
->regs
.pc
;
434 ///////////////////////////////////////////////////////////
435 // RemoteGDB::setregs
437 // Translate the GDB register format into the kernel
438 // debugger register format.
443 memcpy(context
->regs
.intRegFile
, &gdbregs
[KGDB_REG_V0
],
444 32 * sizeof(uint64_t));
446 memcpy(context
->regs
.floatRegFile
.q
, &gdbregs
[KGDB_REG_F0
],
447 32 * sizeof(uint64_t));
449 context
->regs
.pc
= gdbregs
[KGDB_REG_PC
];
453 RemoteGDB::setTempBreakpoint(TempBreakpoint
&bkpt
, Addr addr
)
455 DPRINTF(GDBMisc
, "setTempBreakpoint: addr=%#x\n", addr
);
458 insertHardBreak(addr
, 4);
462 RemoteGDB::clearTempBreakpoint(TempBreakpoint
&bkpt
)
464 DPRINTF(GDBMisc
, "setTempBreakpoint: addr=%#x\n",
468 removeHardBreak(bkpt
.address
, 4);
473 RemoteGDB::clearSingleStep()
475 DPRINTF(GDBMisc
, "clearSingleStep bt_addr=%#x nt_addr=%#x\n",
476 takenBkpt
.address
, notTakenBkpt
.address
);
478 if (takenBkpt
.address
!= 0)
479 clearTempBreakpoint(takenBkpt
);
481 if (notTakenBkpt
.address
!= 0)
482 clearTempBreakpoint(notTakenBkpt
);
486 RemoteGDB::setSingleStep()
488 Addr pc
= context
->regs
.pc
;
492 npc
= pc
+ sizeof(MachInst
);
494 // User was stopped at pc, e.g. the instruction at pc was not
496 MachInst inst
= read
<MachInst
>(pc
);
497 StaticInstPtr
<TheISA
> si(inst
);
498 if (si
->hasBranchTarget(pc
, context
, bpc
)) {
499 // Don't bother setting a breakpoint on the taken branch if it
500 // is the same as the next pc
505 DPRINTF(GDBMisc
, "setSingleStep bt_addr=%#x nt_addr=%#x\n",
506 takenBkpt
.address
, notTakenBkpt
.address
);
508 setTempBreakpoint(notTakenBkpt
, npc
);
511 setTempBreakpoint(takenBkpt
, bpc
);
514 /////////////////////////
527 RemoteGDB::putbyte(uint8_t b
)
532 // Send a packet to gdb
534 RemoteGDB::send(const char *bp
)
539 DPRINTF(GDBSend
, "send: %s\n", bp
);
544 for (csum
= 0; (c
= *p
); p
++) {
549 putbyte(i2digit(csum
>> 4));
550 putbyte(i2digit(csum
));
551 } while ((c
= getbyte() & 0x7f) == KGDB_BADP
);
554 // Receive a packet from gdb
556 RemoteGDB::recv(char *bp
, int maxlen
)
565 while ((c
= getbyte()) != KGDB_START
)
568 while ((c
= getbyte()) != KGDB_END
&& len
< maxlen
) {
582 csum
-= digit2i(getbyte()) * 16;
583 csum
-= digit2i(getbyte());
592 bcopy(bp
+ 3, bp
, len
);
599 DPRINTF(GDBRecv
, "recv: %s: %s\n", gdb_command(*bp
), bp
);
604 // Read bytes from kernel address space for debugger.
606 RemoteGDB::read(Addr vaddr
, size_t size
, char *data
)
608 static Addr lastaddr
= 0;
609 static size_t lastsize
= 0;
614 DPRINTF(GDBRead
, "read: reading memory location zero!\n");
615 vaddr
= lastaddr
+ lastsize
;
618 DPRINTF(GDBRead
, "read: addr=%#x, size=%d", vaddr
, size
);
627 size_t count
= min((Addr
)size
,
628 VMPageSize
- (vaddr
& (VMPageSize
- 1)));
630 maddr
= vtomem(context
, vaddr
, count
);
631 memcpy(data
, maddr
, count
);
637 while (size
>= VMPageSize
) {
638 maddr
= vtomem(context
, vaddr
, count
);
639 memcpy(data
, maddr
, VMPageSize
);
647 maddr
= vtomem(context
, vaddr
, count
);
648 memcpy(data
, maddr
, size
);
652 if (DTRACE(GDBRead
)) {
653 if (DTRACE(GDBExtra
)) {
656 DPRINTFNR(": %s\n", buf
);
665 // Write bytes to kernel address space for debugger.
667 RemoteGDB::write(Addr vaddr
, size_t size
, const char *data
)
669 static Addr lastaddr
= 0;
670 static size_t lastsize
= 0;
675 DPRINTF(GDBWrite
, "write: writing memory location zero!\n");
676 vaddr
= lastaddr
+ lastsize
;
679 if (DTRACE(GDBWrite
)) {
680 DPRINTFN("write: addr=%#x, size=%d", vaddr
, size
);
681 if (DTRACE(GDBExtra
)) {
683 mem2hex(buf
, data
, size
);
684 DPRINTFNR(": %s\n", buf
);
692 size_t count
= min((Addr
)size
,
693 VMPageSize
- (vaddr
& (VMPageSize
- 1)));
695 maddr
= vtomem(context
, vaddr
, count
);
696 memcpy(maddr
, data
, count
);
702 while (size
>= VMPageSize
) {
703 maddr
= vtomem(context
, vaddr
, count
);
704 memcpy(maddr
, data
, VMPageSize
);
712 maddr
= vtomem(context
, vaddr
, count
);
713 memcpy(maddr
, data
, size
);
724 PCEventQueue
*RemoteGDB::getPcEventQueue()
726 return &system
->pcEventQueue
;
730 RemoteGDB::HardBreakpoint::HardBreakpoint(RemoteGDB
*_gdb
, Addr pc
)
731 : PCEvent(_gdb
->getPcEventQueue(), "HardBreakpoint Event", pc
),
732 gdb(_gdb
), refcount(0)
734 DPRINTF(GDBMisc
, "creating hardware breakpoint at %#x\n", evpc
);
738 RemoteGDB::HardBreakpoint::process(ExecContext
*xc
)
740 DPRINTF(GDBMisc
, "handling hardware breakpoint at %#x\n", pc());
742 if (xc
== gdb
->context
)
743 gdb
->trap(ALPHA_KENTRY_INT
);
747 RemoteGDB::insertSoftBreak(Addr addr
, size_t len
)
749 if (len
!= sizeof(MachInst
))
750 panic("invalid length\n");
752 return insertHardBreak(addr
, len
);
756 RemoteGDB::removeSoftBreak(Addr addr
, size_t len
)
758 if (len
!= sizeof(MachInst
))
759 panic("invalid length\n");
761 return removeHardBreak(addr
, len
);
765 RemoteGDB::insertHardBreak(Addr addr
, size_t len
)
767 if (len
!= sizeof(MachInst
))
768 panic("invalid length\n");
770 DPRINTF(GDBMisc
, "inserting hardware breakpoint at %#x\n", addr
);
772 HardBreakpoint
*&bkpt
= hardBreakMap
[addr
];
774 bkpt
= new HardBreakpoint(this, addr
);
782 RemoteGDB::removeHardBreak(Addr addr
, size_t len
)
784 if (len
!= sizeof(MachInst
))
785 panic("invalid length\n");
787 DPRINTF(GDBMisc
, "removing hardware breakpoint at %#x\n", addr
);
789 break_iter_t i
= hardBreakMap
.find(addr
);
790 if (i
== hardBreakMap
.end())
793 HardBreakpoint
*hbp
= (*i
).second
;
794 if (--hbp
->refcount
== 0) {
796 hardBreakMap
.erase(i
);
806 case '0': return "software breakpoint";
807 case '1': return "hardware breakpoint";
808 case '2': return "write watchpoint";
809 case '3': return "read watchpoint";
810 case '4': return "access watchpoint";
811 default: return "unknown breakpoint/watchpoint";
815 // This function does all command processing for interfacing to a
816 // remote gdb. Note that the error codes are ignored by gdb at
817 // present, but might eventually become meaningful. (XXX) It might
818 // makes sense to use POSIX errno values, because that is what the
819 // gdb/remote.c functions want to return.
821 RemoteGDB::trap(int type
)
825 char data
[KGDB_BUFLEN
+ 1];
826 char buffer
[sizeof(gdbregs
) * 2 + 256];
827 char temp
[KGDB_BUFLEN
];
829 char command
, subcmd
;
836 DPRINTF(GDBMisc
, "trap: PC=%#x NPC=%#x\n",
837 context
->regs
.pc
, context
->regs
.npc
);
842 * The first entry to this function is normally through
843 * a breakpoint trap in kgdb_connect(), in which case we
844 * must advance past the breakpoint because gdb will not.
846 * On the first entry here, we expect that gdb is not yet
847 * listening to us, so just enter the interaction loop.
848 * After the debugger is "active" (connected) it will be
849 * waiting for a "signaled" message from us.
854 // Tell remote host that an exception has occurred.
855 snprintf((char *)buffer
, sizeof(buffer
), "S%02x", signal(type
));
858 // Stick frame regs into our reg cache.
862 datalen
= recv(data
, sizeof(data
));
863 data
[sizeof(data
) - 1] = 0; // Sentinel
870 // if this command came from a running gdb, answer it --
871 // the other guy has no way of knowing if we're in or out
872 // of this loop when he issues a "remote-signal".
873 snprintf((char *)buffer
, sizeof(buffer
), "S%02x", signal(type
));
878 if (2 * sizeof(gdbregs
) > sizeof(buffer
))
879 panic("buffer too small");
881 mem2hex(buffer
, gdbregs
, sizeof(gdbregs
));
886 p
= hex2mem(gdbregs
, p
, sizeof(gdbregs
));
887 if (p
== NULL
|| *p
!= '\0')
902 if (val
< 0 && val
>= KGDB_NUMREGS
) {
907 gdbregs
[val
] = hex2i(&p
);
925 if (len
> sizeof(buffer
)) {
929 if (!acc(val
, len
)) {
934 if (read(val
, (size_t)len
, (char *)buffer
)) {
935 mem2hex(temp
, buffer
, len
);
953 if (len
> datalen
- (p
- data
)) {
957 p
= hex2mem(buffer
, p
, sizeof(buffer
));
962 if (!acc(val
, len
)) {
966 if (write(val
, (size_t)len
, (char *)buffer
))
972 case KGDB_SET_THREAD
:
988 case KGDB_ASYNC_CONT
:
992 context
->regs
.pc
= val
;
993 context
->regs
.npc
= val
+ sizeof(MachInst
);
999 if (p
- data
< datalen
) {
1001 context
->regs
.pc
= val
;
1002 context
->regs
.npc
= val
+ sizeof(MachInst
);
1007 case KGDB_ASYNC_STEP
:
1011 context
->regs
.pc
= val
;
1012 context
->regs
.npc
= val
+ sizeof(MachInst
);
1018 if (p
- data
< datalen
) {
1020 context
->regs
.pc
= val
;
1021 context
->regs
.npc
= val
+ sizeof(MachInst
);
1026 case KGDB_CLR_HW_BKPT
:
1028 if (*p
++ != ',') send("E0D");
1030 if (*p
++ != ',') send("E0D");
1033 DPRINTF(GDBMisc
, "clear %s, addr=%#x, len=%d\n",
1034 break_type(subcmd
), val
, len
);
1039 case '0': // software breakpoint
1040 ret
= removeSoftBreak(val
, len
);
1043 case '1': // hardware breakpoint
1044 ret
= removeHardBreak(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_SET_HW_BKPT
:
1060 if (*p
++ != ',') send("E0D");
1062 if (*p
++ != ',') send("E0D");
1065 DPRINTF(GDBMisc
, "set %s, addr=%#x, len=%d\n",
1066 break_type(subcmd
), val
, len
);
1071 case '0': // software breakpoint
1072 ret
= insertSoftBreak(val
, len
);
1075 case '1': // hardware breakpoint
1076 ret
= insertHardBreak(val
, len
);
1079 case '2': // write watchpoint
1080 case '3': // read watchpoint
1081 case '4': // access watchpoint
1087 send(ret
? "OK" : "E0C");
1090 case KGDB_QUERY_VAR
:
1091 var
= string(p
, datalen
- 1);
1099 case KGDB_SET_BREAK
:
1101 case KGDB_CYCLE_STEP
:
1102 case KGDB_SIG_CYCLE_STEP
:
1106 case KGDB_THREAD_ALIVE
:
1107 case KGDB_TARGET_EXIT
:
1108 case KGDB_BINARY_DLOAD
:
1109 // Unsupported command
1110 DPRINTF(GDBMisc
, "Unsupported command: %s\n",
1111 gdb_command(command
));
1112 DDUMP(GDBMisc
, (uint8_t *)data
, datalen
);
1118 DPRINTF(GDBMisc
, "Unknown command: %c(%#x)\n",
1131 // Convert a hex digit into an integer.
1132 // This returns -1 if the argument passed is no valid hex digit.
1136 if (c
>= '0' && c
<= '9')
1138 else if (c
>= 'a' && c
<= 'f')
1139 return (c
- 'a' + 10);
1140 else if (c
>= 'A' && c
<= 'F')
1142 return (c
- 'A' + 10);
1147 // Convert the low 4 bits of an integer into an hex digit.
1151 return ("0123456789abcdef"[n
& 0x0f]);
1154 // Convert a byte array into an hex string.
1156 mem2hex(void *vdst
, const void *vsrc
, int len
)
1158 char *dst
= (char *)vdst
;
1159 const char *src
= (const char *)vsrc
;
1162 *dst
++ = i2digit(*src
>> 4);
1163 *dst
++ = i2digit(*src
++);
1168 // Convert an hex string into a byte array.
1169 // This returns a pointer to the character following the last valid
1170 // hex digit. If the string ends in the middle of a byte, NULL is
1173 hex2mem(void *vdst
, const char *src
, int maxlen
)
1175 char *dst
= (char *)vdst
;
1178 while (*src
&& maxlen
--) {
1179 msb
= digit2i(*src
++);
1182 lsb
= digit2i(*src
++);
1185 *dst
++ = (msb
<< 4) | lsb
;
1190 // Convert an hex string into an integer.
1191 // This returns a pointer to the character following the last valid
1194 hex2i(const char **srcp
)
1196 const char *src
= *srcp
;
1200 while ((nibble
= digit2i(*src
)) >= 0) {