2 * Copyright (c) 2003 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>
124 #include "cpu/exec_context.hh"
125 #include "base/intmath.hh"
126 #include "base/kgdb.h"
128 #include "mem/functional_mem/physical_memory.hh"
129 #include "base/remote_gdb.hh"
130 #include "base/socket.hh"
131 #include "base/trace.hh"
132 #include "targetarch/vtophys.hh"
133 #include "sim/system.hh"
134 #include "cpu/static_inst.hh"
139 RemoteGDB
*theDebugger
= NULL
;
145 theDebugger
->trap(ALPHA_KENTRY_IF
);
149 ///////////////////////////////////////////////////////////
154 GDBListener::Event::Event(GDBListener
*l
, int fd
, int e
)
155 : PollEvent(fd
, e
), listener(l
)
159 GDBListener::Event::process(int revent
)
164 GDBListener::GDBListener(RemoteGDB
*g
, int p
)
165 : event(NULL
), gdb(g
), port(p
)
168 GDBListener::~GDBListener()
177 return gdb
->name() + ".listener";
181 GDBListener::listen()
183 while (!listener
.listen(port
, true)) {
184 DPRINTF(GDBMisc
, "Can't bind port %d\n", port
);
188 cerr
<< "Listening for remote gdb connection on port " << port
<< endl
;
189 event
= new Event(this, listener
.getfd(), POLLIN
);
190 pollQueue
.schedule(event
);
194 GDBListener::accept()
196 if (!listener
.islistening())
197 panic("GDBListener::accept(): cannot accept if we're not listening!");
199 int sfd
= listener
.accept(true);
202 if (gdb
->isattached())
209 ///////////////////////////////////////////////////////////
215 void mem2hex(void *, const void *, int);
216 const char *hex2mem(void *, const char *, int);
217 Addr
hex2i(const char **);
219 RemoteGDB::Event::Event(RemoteGDB
*g
, int fd
, int e
)
220 : PollEvent(fd
, e
), gdb(g
)
224 RemoteGDB::Event::process(int revent
)
227 gdb
->trap(ALPHA_KENTRY_IF
);
228 else if (revent
& POLLNVAL
)
232 RemoteGDB::RemoteGDB(System
*_system
, ExecContext
*c
)
233 : event(NULL
), fd(-1), active(false), attached(false),
234 system(_system
), pmem(_system
->physmem
), context(c
)
236 memset(gdbregs
, 0, sizeof(gdbregs
));
239 RemoteGDB::~RemoteGDB()
248 return system
->name() + ".remote_gdb";
252 RemoteGDB::isattached()
256 RemoteGDB::attach(int f
)
260 event
= new Event(this, fd
, POLLIN
);
261 pollQueue
.schedule(event
);
264 DPRINTFN("remote gdb attached\n");
277 pollQueue
.remove(event
);
278 DPRINTFN("remote gdb detached\n");
282 gdb_command(char cmd
)
285 case KGDB_SIGNAL
: return "KGDB_SIGNAL";
286 case KGDB_SET_BAUD
: return "KGDB_SET_BAUD";
287 case KGDB_SET_BREAK
: return "KGDB_SET_BREAK";
288 case KGDB_CONT
: return "KGDB_CONT";
289 case KGDB_ASYNC_CONT
: return "KGDB_ASYNC_CONT";
290 case KGDB_DEBUG
: return "KGDB_DEBUG";
291 case KGDB_DETACH
: return "KGDB_DETACH";
292 case KGDB_REG_R
: return "KGDB_REG_R";
293 case KGDB_REG_W
: return "KGDB_REG_W";
294 case KGDB_SET_THREAD
: return "KGDB_SET_THREAD";
295 case KGDB_CYCLE_STEP
: return "KGDB_CYCLE_STEP";
296 case KGDB_SIG_CYCLE_STEP
: return "KGDB_SIG_CYCLE_STEP";
297 case KGDB_KILL
: return "KGDB_KILL";
298 case KGDB_MEM_W
: return "KGDB_MEM_W";
299 case KGDB_MEM_R
: return "KGDB_MEM_R";
300 case KGDB_SET_REG
: return "KGDB_SET_REG";
301 case KGDB_READ_REG
: return "KGDB_READ_REG";
302 case KGDB_QUERY_VAR
: return "KGDB_QUERY_VAR";
303 case KGDB_SET_VAR
: return "KGDB_SET_VAR";
304 case KGDB_RESET
: return "KGDB_RESET";
305 case KGDB_STEP
: return "KGDB_STEP";
306 case KGDB_ASYNC_STEP
: return "KGDB_ASYNC_STEP";
307 case KGDB_THREAD_ALIVE
: return "KGDB_THREAD_ALIVE";
308 case KGDB_TARGET_EXIT
: return "KGDB_TARGET_EXIT";
309 case KGDB_BINARY_DLOAD
: return "KGDB_BINARY_DLOAD";
310 case KGDB_CLR_HW_BKPT
: return "KGDB_CLR_HW_BKPT";
311 case KGDB_SET_HW_BKPT
: return "KGDB_SET_HW_BKPT";
312 case KGDB_START
: return "KGDB_START";
313 case KGDB_END
: return "KGDB_END";
314 case KGDB_GOODP
: return "KGDB_GOODP";
315 case KGDB_BADP
: return "KGDB_BADP";
316 default: return "KGDB_UNKNOWN";
320 ///////////////////////////////////////////////////////////
323 // Determine if the mapping at va..(va+len) is valid.
326 RemoteGDB::acc(Addr va
, size_t len
)
331 va
= alpha_trunc_page(va
);
332 last_va
= alpha_round_page(va
+ len
);
335 if (va
< ALPHA_K0SEG_BASE
) {
336 DPRINTF(GDBAcc
, "acc: Mapping is invalid %#x < K0SEG\n", va
);
340 if (va
< ALPHA_K1SEG_BASE
) {
341 if (va
< (ALPHA_K0SEG_BASE
+ pmem
->size())) {
342 DPRINTF(GDBAcc
, "acc: Mapping is valid K0SEG <= "
343 "%#x < K0SEG + size\n", va
);
346 DPRINTF(GDBAcc
, "acc: Mapping is invalid %#x < K0SEG\n",
352 Addr ptbr
= context
->regs
.ipr
[AlphaISA::IPR_PALtemp20
];
353 pte
= kernel_pte_lookup(pmem
, ptbr
, va
);
354 if (!pte
|| !entry_valid(pmem
->phys_read_qword(pte
))) {
355 DPRINTF(GDBAcc
, "acc: %#x pte is invalid\n", va
);
359 } while (va
< last_va
);
361 DPRINTF(GDBAcc
, "acc: %#x mapping is valid\n", va
);
365 ///////////////////////////////////////////////////////////
368 // Translate a trap number into a Unix-compatible signal number.
369 // (GDB only understands Unix signal numbers.)
372 RemoteGDB::signal(int type
)
375 case ALPHA_KENTRY_INT
:
378 case ALPHA_KENTRY_UNA
:
381 case ALPHA_KENTRY_ARITH
:
384 case ALPHA_KENTRY_IF
:
387 case ALPHA_KENTRY_MM
:
391 panic("unknown signal type");
396 ///////////////////////////////////////////////////////////
397 // RemoteGDB::getregs
399 // Translate the kernel debugger register format into
400 // the GDB register format.
404 memset(gdbregs
, 0, sizeof(gdbregs
));
405 memcpy(&gdbregs
[KGDB_REG_V0
], context
->regs
.intRegFile
, 32 * sizeof(uint64_t));
407 memcpy(&gdbregs
[KGDB_REG_F0
], context
->regs
.floatRegFile
.q
,
408 32 * sizeof(uint64_t));
410 gdbregs
[KGDB_REG_PC
] = context
->regs
.pc
;
413 ///////////////////////////////////////////////////////////
414 // RemoteGDB::setregs
416 // Translate the GDB register format into the kernel
417 // debugger register format.
422 memcpy(context
->regs
.intRegFile
, &gdbregs
[KGDB_REG_V0
],
423 32 * sizeof(uint64_t));
425 memcpy(context
->regs
.floatRegFile
.q
, &gdbregs
[KGDB_REG_F0
],
426 32 * sizeof(uint64_t));
428 context
->regs
.pc
= gdbregs
[KGDB_REG_PC
];
432 RemoteGDB::setTempBreakpoint(TempBreakpoint
&bkpt
, Addr addr
)
434 DPRINTF(GDBMisc
, "setTempBreakpoint: addr=%#x\n", addr
);
437 insertHardBreak(addr
, 4);
441 RemoteGDB::clearTempBreakpoint(TempBreakpoint
&bkpt
)
443 DPRINTF(GDBMisc
, "setTempBreakpoint: addr=%#x\n",
447 removeHardBreak(bkpt
.address
, 4);
452 RemoteGDB::clearSingleStep()
454 DPRINTF(GDBMisc
, "clearSingleStep bt_addr=%#x nt_addr=%#x\n",
455 takenBkpt
.address
, notTakenBkpt
.address
);
457 if (takenBkpt
.address
!= 0)
458 clearTempBreakpoint(takenBkpt
);
460 if (notTakenBkpt
.address
!= 0)
461 clearTempBreakpoint(notTakenBkpt
);
465 RemoteGDB::setSingleStep()
467 Addr pc
= context
->regs
.pc
;
471 npc
= pc
+ sizeof(MachInst
);
473 // User was stopped at pc, e.g. the instruction at pc was not
475 MachInst inst
= read
<MachInst
>(pc
);
476 StaticInstPtr
<TheISA
> si(inst
);
477 if (si
->hasBranchTarget(pc
, context
, bpc
)) {
478 // Don't bother setting a breakpoint on the taken branch if it
479 // is the same as the next pc
484 DPRINTF(GDBMisc
, "setSingleStep bt_addr=%#x nt_addr=%#x\n",
485 takenBkpt
.address
, notTakenBkpt
.address
);
487 setTempBreakpoint(notTakenBkpt
, npc
);
490 setTempBreakpoint(takenBkpt
, bpc
);
493 /////////////////////////
506 RemoteGDB::putbyte(uint8_t b
)
511 // Send a packet to gdb
513 RemoteGDB::send(const char *bp
)
518 DPRINTF(GDBSend
, "send: %s\n", bp
);
523 for (csum
= 0; (c
= *p
); p
++) {
528 putbyte(i2digit(csum
>> 4));
529 putbyte(i2digit(csum
));
530 } while ((c
= getbyte() & 0x7f) == KGDB_BADP
);
533 // Receive a packet from gdb
535 RemoteGDB::recv(char *bp
, int maxlen
)
544 while ((c
= getbyte()) != KGDB_START
)
547 while ((c
= getbyte()) != KGDB_END
&& len
< maxlen
) {
561 csum
-= digit2i(getbyte()) * 16;
562 csum
-= digit2i(getbyte());
571 bcopy(bp
+ 3, bp
, len
);
578 DPRINTF(GDBRecv
, "recv: %s: %s\n", gdb_command(*bp
), bp
);
583 // Read bytes from kernel address space for debugger.
585 RemoteGDB::read(Addr vaddr
, size_t size
, char *data
)
587 static Addr lastaddr
= 0;
588 static size_t lastsize
= 0;
593 DPRINTF(GDBRead
, "read: reading memory location zero!\n");
594 vaddr
= lastaddr
+ lastsize
;
597 DPRINTF(GDBRead
, "read: addr=%#x, size=%d", vaddr
, size
);
606 size_t count
= min((Addr
)size
,
607 VMPageSize
- (vaddr
& (VMPageSize
- 1)));
609 maddr
= vtomem(context
, vaddr
, count
);
610 memcpy(data
, maddr
, count
);
616 while (size
>= VMPageSize
) {
617 maddr
= vtomem(context
, vaddr
, count
);
618 memcpy(data
, maddr
, VMPageSize
);
626 maddr
= vtomem(context
, vaddr
, count
);
627 memcpy(data
, maddr
, size
);
631 if (DTRACE(GDBRead
)) {
632 if (DTRACE(GDBExtra
)) {
635 DPRINTFNR(": %s\n", buf
);
644 // Write bytes to kernel address space for debugger.
646 RemoteGDB::write(Addr vaddr
, size_t size
, const char *data
)
648 static Addr lastaddr
= 0;
649 static size_t lastsize
= 0;
654 DPRINTF(GDBWrite
, "write: writing memory location zero!\n");
655 vaddr
= lastaddr
+ lastsize
;
658 if (DTRACE(GDBWrite
)) {
659 DPRINTFN("write: addr=%#x, size=%d", vaddr
, size
);
660 if (DTRACE(GDBExtra
)) {
662 mem2hex(buf
, data
, size
);
663 DPRINTFNR(": %s\n", buf
);
671 size_t count
= min((Addr
)size
,
672 VMPageSize
- (vaddr
& (VMPageSize
- 1)));
674 maddr
= vtomem(context
, vaddr
, count
);
675 memcpy(maddr
, data
, count
);
681 while (size
>= VMPageSize
) {
682 maddr
= vtomem(context
, vaddr
, count
);
683 memcpy(maddr
, data
, VMPageSize
);
691 maddr
= vtomem(context
, vaddr
, count
);
692 memcpy(maddr
, data
, size
);
703 PCEventQueue
*RemoteGDB::getPcEventQueue()
705 return &system
->pcEventQueue
;
709 RemoteGDB::HardBreakpoint::HardBreakpoint(RemoteGDB
*_gdb
, Addr pc
)
710 : PCEvent(_gdb
->getPcEventQueue(), "HardBreakpoint Event", pc
),
711 gdb(_gdb
), refcount(0)
713 DPRINTF(GDBMisc
, "creating hardware breakpoint at %#x\n", evpc
);
718 RemoteGDB::HardBreakpoint::process(ExecContext
*xc
)
720 DPRINTF(GDBMisc
, "handling hardware breakpoint at %#x\n", pc());
722 if (xc
== gdb
->context
)
723 gdb
->trap(ALPHA_KENTRY_INT
);
727 RemoteGDB::insertSoftBreak(Addr addr
, size_t len
)
729 if (len
!= sizeof(MachInst
))
730 panic("invalid length\n");
732 return insertHardBreak(addr
, len
);
736 RemoteGDB::removeSoftBreak(Addr addr
, size_t len
)
738 if (len
!= sizeof(MachInst
))
739 panic("invalid length\n");
741 return removeHardBreak(addr
, len
);
745 RemoteGDB::insertHardBreak(Addr addr
, size_t len
)
747 if (len
!= sizeof(MachInst
))
748 panic("invalid length\n");
750 DPRINTF(GDBMisc
, "inserting hardware breakpoint at %#x\n", addr
);
752 HardBreakpoint
*&bkpt
= hardBreakMap
[addr
];
754 bkpt
= new HardBreakpoint(this, addr
);
762 RemoteGDB::removeHardBreak(Addr addr
, size_t len
)
764 if (len
!= sizeof(MachInst
))
765 panic("invalid length\n");
767 DPRINTF(GDBMisc
, "removing hardware breakpoint at %#x\n", addr
);
769 break_iter_t i
= hardBreakMap
.find(addr
);
770 if (i
== hardBreakMap
.end())
773 HardBreakpoint
*hbp
= (*i
).second
;
774 if (--hbp
->refcount
== 0) {
776 hardBreakMap
.erase(i
);
786 case '0': return "software breakpoint";
787 case '1': return "hardware breakpoint";
788 case '2': return "write watchpoint";
789 case '3': return "read watchpoint";
790 case '4': return "access watchpoint";
791 default: return "unknown breakpoint/watchpoint";
795 // This function does all command processing for interfacing to a
796 // remote gdb. Note that the error codes are ignored by gdb at
797 // present, but might eventually become meaningful. (XXX) It might
798 // makes sense to use POSIX errno values, because that is what the
799 // gdb/remote.c functions want to return.
801 RemoteGDB::trap(int type
)
805 char data
[KGDB_BUFLEN
+ 1];
806 char buffer
[sizeof(gdbregs
) * 2 + 256];
807 char temp
[KGDB_BUFLEN
];
809 char command
, subcmd
;
816 DPRINTF(GDBMisc
, "trap: PC=%#x NPC=%#x\n",
817 context
->regs
.pc
, context
->regs
.npc
);
822 * The first entry to this function is normally through
823 * a breakpoint trap in kgdb_connect(), in which case we
824 * must advance past the breakpoint because gdb will not.
826 * On the first entry here, we expect that gdb is not yet
827 * listening to us, so just enter the interaction loop.
828 * After the debugger is "active" (connected) it will be
829 * waiting for a "signaled" message from us.
834 // Tell remote host that an exception has occurred.
835 sprintf((char *)buffer
, "S%02x", signal(type
));
838 // Stick frame regs into our reg cache.
842 datalen
= recv(data
, sizeof(data
));
843 data
[sizeof(data
) - 1] = 0; // Sentinel
850 // if this command came from a running gdb, answer it --
851 // the other guy has no way of knowing if we're in or out
852 // of this loop when he issues a "remote-signal".
853 sprintf((char *)buffer
, "S%02x", signal(type
));
858 if (2 * sizeof(gdbregs
) > sizeof(buffer
))
859 panic("buffer too small");
861 mem2hex(buffer
, gdbregs
, sizeof(gdbregs
));
866 p
= hex2mem(gdbregs
, p
, sizeof(gdbregs
));
867 if (p
== NULL
|| *p
!= '\0')
882 if (val
< 0 && val
>= KGDB_NUMREGS
) {
887 gdbregs
[val
] = hex2i(&p
);
905 if (len
> sizeof(buffer
)) {
909 if (!acc(val
, len
)) {
914 if (read(val
, (size_t)len
, (char *)buffer
)) {
915 mem2hex(temp
, buffer
, len
);
933 if (len
> datalen
- (p
- data
)) {
937 p
= hex2mem(buffer
, p
, sizeof(buffer
));
942 if (!acc(val
, len
)) {
946 if (write(val
, (size_t)len
, (char *)buffer
))
952 case KGDB_SET_THREAD
:
968 case KGDB_ASYNC_CONT
:
972 context
->regs
.pc
= val
;
973 context
->regs
.npc
= val
+ sizeof(MachInst
);
979 if (p
- data
< datalen
) {
981 context
->regs
.pc
= val
;
982 context
->regs
.npc
= val
+ sizeof(MachInst
);
987 case KGDB_ASYNC_STEP
:
991 context
->regs
.pc
= val
;
992 context
->regs
.npc
= val
+ sizeof(MachInst
);
998 if (p
- data
< datalen
) {
1000 context
->regs
.pc
= val
;
1001 context
->regs
.npc
= val
+ sizeof(MachInst
);
1006 case KGDB_CLR_HW_BKPT
:
1008 if (*p
++ != ',') send("E0D");
1010 if (*p
++ != ',') send("E0D");
1013 DPRINTF(GDBMisc
, "clear %s, addr=%#x, len=%d\n",
1014 break_type(subcmd
), val
, len
);
1019 case '0': // software breakpoint
1020 ret
= removeSoftBreak(val
, len
);
1023 case '1': // hardware breakpoint
1024 ret
= removeHardBreak(val
, len
);
1027 case '2': // write watchpoint
1028 case '3': // read watchpoint
1029 case '4': // access watchpoint
1035 send(ret
? "OK" : "E0C");
1038 case KGDB_SET_HW_BKPT
:
1040 if (*p
++ != ',') send("E0D");
1042 if (*p
++ != ',') send("E0D");
1045 DPRINTF(GDBMisc
, "set %s, addr=%#x, len=%d\n",
1046 break_type(subcmd
), val
, len
);
1051 case '0': // software breakpoint
1052 ret
= insertSoftBreak(val
, len
);
1055 case '1': // hardware breakpoint
1056 ret
= insertHardBreak(val
, len
);
1059 case '2': // write watchpoint
1060 case '3': // read watchpoint
1061 case '4': // access watchpoint
1067 send(ret
? "OK" : "E0C");
1070 case KGDB_QUERY_VAR
:
1071 var
= string(p
, datalen
- 1);
1079 case KGDB_SET_BREAK
:
1081 case KGDB_CYCLE_STEP
:
1082 case KGDB_SIG_CYCLE_STEP
:
1086 case KGDB_THREAD_ALIVE
:
1087 case KGDB_TARGET_EXIT
:
1088 case KGDB_BINARY_DLOAD
:
1089 // Unsupported command
1090 DPRINTF(GDBMisc
, "Unsupported command: %s\n",
1091 gdb_command(command
));
1092 DDUMP(GDBMisc
, (uint8_t *)data
, datalen
);
1098 DPRINTF(GDBMisc
, "Unknown command: %c(%#x)\n",
1111 // Convert a hex digit into an integer.
1112 // This returns -1 if the argument passed is no valid hex digit.
1116 if (c
>= '0' && c
<= '9')
1118 else if (c
>= 'a' && c
<= 'f')
1119 return (c
- 'a' + 10);
1120 else if (c
>= 'A' && c
<= 'F')
1122 return (c
- 'A' + 10);
1127 // Convert the low 4 bits of an integer into an hex digit.
1131 return ("0123456789abcdef"[n
& 0x0f]);
1134 // Convert a byte array into an hex string.
1136 mem2hex(void *vdst
, const void *vsrc
, int len
)
1138 char *dst
= (char *)vdst
;
1139 const char *src
= (const char *)vsrc
;
1142 *dst
++ = i2digit(*src
>> 4);
1143 *dst
++ = i2digit(*src
++);
1148 // Convert an hex string into a byte array.
1149 // This returns a pointer to the character following the last valid
1150 // hex digit. If the string ends in the middle of a byte, NULL is
1153 hex2mem(void *vdst
, const char *src
, int maxlen
)
1155 char *dst
= (char *)vdst
;
1158 while (*src
&& maxlen
--) {
1159 msb
= digit2i(*src
++);
1162 lsb
= digit2i(*src
++);
1165 *dst
++ = (msb
<< 4) | lsb
;
1170 // Convert an hex string into an integer.
1171 // This returns a pointer to the character following the last valid
1174 hex2i(const char **srcp
)
1176 const char *src
= *srcp
;
1180 while ((nibble
= digit2i(*src
)) >= 0) {