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()
175 GDBListener::listen()
177 while (!listener
.listen(port
, true)) {
178 DPRINTF(RGDB
, "GDBListener(listen): Can't bind port %d\n", port
);
182 cerr
<< "Listening for remote gdb connection on port " << port
<< endl
;
183 event
= new Event(this, listener
.getfd(), POLLIN
);
184 pollQueue
.schedule(event
);
188 GDBListener::accept()
190 if (!listener
.islistening())
191 panic("GDBListener(accept): cannot accept a connection if we're not listening!");
193 int sfd
= listener
.accept(true);
196 if (gdb
->isattached())
203 ///////////////////////////////////////////////////////////
209 void mem2hex(void *, const void *, int);
210 const char *hex2mem(void *, const char *, int);
211 Addr
hex2i(const char **);
213 RemoteGDB::Event::Event(RemoteGDB
*g
, int fd
, int e
)
214 : PollEvent(fd
, e
), gdb(g
)
218 RemoteGDB::Event::process(int revent
)
219 { gdb
->trap(ALPHA_KENTRY_IF
); }
221 RemoteGDB::RemoteGDB(System
*_system
, ExecContext
*c
)
222 : event(NULL
), fd(-1), active(false), attached(false),
223 system(_system
), pmem(_system
->physmem
), context(c
)
225 memset(gdbregs
, 0, sizeof(gdbregs
));
228 RemoteGDB::~RemoteGDB()
235 RemoteGDB::isattached()
239 RemoteGDB::attach(int f
)
243 event
= new Event(this, fd
, POLLIN
);
244 pollQueue
.schedule(event
);
247 DPRINTFN("remote gdb attached\n");
260 pollQueue
.remove(event
);
261 DPRINTFN("remote gdb detached\n");
265 gdb_command(char cmd
)
268 case KGDB_SIGNAL
: return "KGDB_SIGNAL";
269 case KGDB_SET_BAUD
: return "KGDB_SET_BAUD";
270 case KGDB_SET_BREAK
: return "KGDB_SET_BREAK";
271 case KGDB_CONT
: return "KGDB_CONT";
272 case KGDB_ASYNC_CONT
: return "KGDB_ASYNC_CONT";
273 case KGDB_DEBUG
: return "KGDB_DEBUG";
274 case KGDB_DETACH
: return "KGDB_DETACH";
275 case KGDB_REG_R
: return "KGDB_REG_R";
276 case KGDB_REG_W
: return "KGDB_REG_W";
277 case KGDB_SET_THREAD
: return "KGDB_SET_THREAD";
278 case KGDB_CYCLE_STEP
: return "KGDB_CYCLE_STEP";
279 case KGDB_SIG_CYCLE_STEP
: return "KGDB_SIG_CYCLE_STEP";
280 case KGDB_KILL
: return "KGDB_KILL";
281 case KGDB_MEM_W
: return "KGDB_MEM_W";
282 case KGDB_MEM_R
: return "KGDB_MEM_R";
283 case KGDB_SET_REG
: return "KGDB_SET_REG";
284 case KGDB_READ_REG
: return "KGDB_READ_REG";
285 case KGDB_QUERY_VAR
: return "KGDB_QUERY_VAR";
286 case KGDB_SET_VAR
: return "KGDB_SET_VAR";
287 case KGDB_RESET
: return "KGDB_RESET";
288 case KGDB_STEP
: return "KGDB_STEP";
289 case KGDB_ASYNC_STEP
: return "KGDB_ASYNC_STEP";
290 case KGDB_THREAD_ALIVE
: return "KGDB_THREAD_ALIVE";
291 case KGDB_TARGET_EXIT
: return "KGDB_TARGET_EXIT";
292 case KGDB_BINARY_DLOAD
: return "KGDB_BINARY_DLOAD";
293 case KGDB_CLR_HW_BKPT
: return "KGDB_CLR_HW_BKPT";
294 case KGDB_SET_HW_BKPT
: return "KGDB_SET_HW_BKPT";
295 case KGDB_START
: return "KGDB_START";
296 case KGDB_END
: return "KGDB_END";
297 case KGDB_GOODP
: return "KGDB_GOODP";
298 case KGDB_BADP
: return "KGDB_BADP";
299 default: return "KGDB_UNKNOWN";
303 ///////////////////////////////////////////////////////////
306 // Determine if the mapping at va..(va+len) is valid.
309 RemoteGDB::acc(Addr va
, size_t len
)
314 va
= alpha_trunc_page(va
);
315 last_va
= alpha_round_page(va
+ len
);
318 if (va
< ALPHA_K0SEG_BASE
) {
319 DPRINTF(RGDB
, "RGDB(acc): Mapping is invalid %#x < K0SEG\n", va
);
323 if (va
< ALPHA_K1SEG_BASE
) {
324 if (va
< (ALPHA_K0SEG_BASE
+ pmem
->getSize())) {
325 DPRINTF(RGDB
, "RGDB(acc): Mapping is valid K0SEG <= "
326 "%#x < K0SEG + size\n", va
);
329 DPRINTF(RGDB
, "RGDB(acc): Mapping is invalid %#x < K0SEG\n",
335 Addr ptbr
= context
->regs
.ipr
[AlphaISA::IPR_PALtemp20
];
336 pte
= kernel_pte_lookup(pmem
, ptbr
, va
);
337 if (!pte
|| !entry_valid(pmem
->phys_read_qword(pte
))) {
338 DPRINTF(RGDB
, "RGDB(acc): %#x pte is invalid\n", va
);
342 } while (va
< last_va
);
344 DPRINTF(RGDB
, "RGDB(acc): %#x mapping is valid\n", va
);
348 ///////////////////////////////////////////////////////////
351 // Translate a trap number into a Unix-compatible signal number.
352 // (GDB only understands Unix signal numbers.)
355 RemoteGDB::signal(int type
)
358 case ALPHA_KENTRY_UNA
:
361 case ALPHA_KENTRY_ARITH
:
364 case ALPHA_KENTRY_IF
:
367 case ALPHA_KENTRY_MM
:
371 panic("unknown signal type");
376 ///////////////////////////////////////////////////////////
377 // RemoteGDB::getregs
379 // Translate the kernel debugger register format into
380 // the GDB register format.
384 memset(gdbregs
, 0, sizeof(gdbregs
));
385 memcpy(&gdbregs
[KGDB_REG_V0
], context
->regs
.intRegFile
, 32 * sizeof(uint64_t));
387 memcpy(&gdbregs
[KGDB_REG_F0
], context
->regs
.floatRegFile
.q
,
388 32 * sizeof(uint64_t));
390 gdbregs
[KGDB_REG_PC
] = context
->regs
.pc
;
393 ///////////////////////////////////////////////////////////
394 // RemoteGDB::setregs
396 // Translate the GDB register format into the kernel
397 // debugger register format.
402 memcpy(context
->regs
.intRegFile
, &gdbregs
[KGDB_REG_V0
], 32 * sizeof(uint64_t));
404 memcpy(context
->regs
.floatRegFile
.q
, &gdbregs
[KGDB_REG_F0
],
405 32 * sizeof(uint64_t));
407 context
->regs
.pc
= gdbregs
[KGDB_REG_PC
];
411 RemoteGDB::setTempBreakpoint(TempBreakpoint
&bkpt
, Addr addr
)
413 DPRINTF(RGDB
, "RGDB(setTempBreakpoint): addr=%#x\n", addr
);
416 insertHardBreak(addr
, 4);
420 RemoteGDB::clearTempBreakpoint(TempBreakpoint
&bkpt
)
422 DPRINTF(RGDB
, "RGDB(setTempBreakpoint): addr=%#x\n",
426 removeHardBreak(bkpt
.address
, 4);
431 RemoteGDB::clearSingleStep()
433 DPRINTF(RGDB
, "clearSingleStep bt_addr=%#x nt_addr=%#x\n",
434 takenBkpt
.address
, notTakenBkpt
.address
);
436 if (takenBkpt
.address
!= 0)
437 clearTempBreakpoint(takenBkpt
);
439 if (notTakenBkpt
.address
!= 0)
440 clearTempBreakpoint(notTakenBkpt
);
444 RemoteGDB::setSingleStep()
446 Addr pc
= context
->regs
.pc
;
450 npc
= pc
+ sizeof(MachInst
);
452 // User was stopped at pc, e.g. the instruction at pc was not
454 MachInst inst
= read
<MachInst
>(pc
);
455 StaticInstPtr
<TheISA
> si(inst
);
456 if (si
->hasBranchTarget(pc
, context
, bpc
)) {
457 // Don't bother setting a breakpoint on the taken branch if it
458 // is the same as the next pc
463 DPRINTF(RGDB
, "setSingleStep bt_addr=%#x nt_addr=%#x\n",
464 takenBkpt
.address
, notTakenBkpt
.address
);
466 setTempBreakpoint(notTakenBkpt
, npc
);
469 setTempBreakpoint(takenBkpt
, bpc
);
472 /////////////////////////
485 RemoteGDB::putbyte(uint8_t b
)
490 // Send a packet to gdb
492 RemoteGDB::send(const char *bp
)
497 // DPRINTF(RGDB, "RGDB(send): %s\n", bp);
502 for (csum
= 0; (c
= *p
); p
++) {
507 putbyte(i2digit(csum
>> 4));
508 putbyte(i2digit(csum
));
509 } while ((c
= getbyte() & 0x7f) == KGDB_BADP
);
512 // Receive a packet from gdb
514 RemoteGDB::recv(char *bp
, int maxlen
)
523 while ((c
= getbyte()) != KGDB_START
)
526 while ((c
= getbyte()) != KGDB_END
&& len
< maxlen
) {
540 csum
-= digit2i(getbyte()) * 16;
541 csum
-= digit2i(getbyte());
550 bcopy(bp
+ 3, bp
, len
);
557 // DPRINTF(RGDB, "RGDB(recv): %s: %s\n", gdb_command(*bp), bp);
562 // Read bytes from kernel address space for debugger.
564 RemoteGDB::read(Addr vaddr
, size_t size
, char *data
)
566 static Addr lastaddr
= 0;
567 static size_t lastsize
= 0;
572 DPRINTF(RGDB
, "\nRGDB(read): reading memory location zero!\n");
573 vaddr
= lastaddr
+ lastsize
;
576 DPRINTF(RGDB
, "RGDB(read): addr=%#x, size=%d", vaddr
, size
);
585 size_t count
= min((Addr
)size
,
586 VMPageSize
- (vaddr
& (VMPageSize
- 1)));
588 maddr
= vtomem(context
, vaddr
, count
);
589 memcpy(data
, maddr
, count
);
595 while (size
>= VMPageSize
) {
596 maddr
= vtomem(context
, vaddr
, count
);
597 memcpy(data
, maddr
, VMPageSize
);
605 maddr
= vtomem(context
, vaddr
, count
);
606 memcpy(data
, maddr
, size
);
613 cprintf(": %s\n", buf
);
620 // Write bytes to kernel address space for debugger.
622 RemoteGDB::write(Addr vaddr
, size_t size
, const char *data
)
624 static Addr lastaddr
= 0;
625 static size_t lastsize
= 0;
630 DPRINTF(RGDB
, "RGDB(write): writing memory location zero!\n");
631 vaddr
= lastaddr
+ lastsize
;
636 mem2hex(buf
, data
, size
);
637 cprintf("RGDB(write): addr=%#x, size=%d: %s\n", vaddr
, size
, buf
);
643 size_t count
= min((Addr
)size
,
644 VMPageSize
- (vaddr
& (VMPageSize
- 1)));
646 maddr
= vtomem(context
, vaddr
, count
);
647 memcpy(maddr
, data
, count
);
653 while (size
>= VMPageSize
) {
654 maddr
= vtomem(context
, vaddr
, count
);
655 memcpy(maddr
, data
, VMPageSize
);
663 maddr
= vtomem(context
, vaddr
, count
);
664 memcpy(maddr
, data
, size
);
675 PCEventQueue
*RemoteGDB::getPcEventQueue()
677 return &system
->pcEventQueue
;
681 RemoteGDB::HardBreakpoint::HardBreakpoint(RemoteGDB
*_gdb
, Addr pc
)
682 : PCEvent(_gdb
->getPcEventQueue(), "HardBreakpoint Event", pc
),
683 gdb(_gdb
), refcount(0)
685 DPRINTF(RGDB
, "creating hardware breakpoint at %#x\n", evpc
);
690 RemoteGDB::HardBreakpoint::process(ExecContext
*xc
)
692 DPRINTF(RGDB
, "handling hardware breakpoint at %#x\n", pc());
694 if (xc
== gdb
->context
)
695 gdb
->trap(ALPHA_KENTRY_IF
);
699 RemoteGDB::insertSoftBreak(Addr addr
, size_t len
)
701 if (len
!= sizeof(MachInst
))
702 panic("invalid length\n");
704 return insertHardBreak(addr
, len
);
708 RemoteGDB::removeSoftBreak(Addr addr
, size_t len
)
710 if (len
!= sizeof(MachInst
))
711 panic("invalid length\n");
713 return removeHardBreak(addr
, len
);
717 RemoteGDB::insertHardBreak(Addr addr
, size_t len
)
719 if (len
!= sizeof(MachInst
))
720 panic("invalid length\n");
722 DPRINTF(RGDB
, "inserting hardware breakpoint at %#x\n", addr
);
724 HardBreakpoint
*&bkpt
= hardBreakMap
[addr
];
726 bkpt
= new HardBreakpoint(this, addr
);
733 break_iter_t i
= hardBreakMap
.find(addr
);
734 if (i
== hardBreakMap
.end()) {
735 HardBreakpoint
*bkpt
= new HardBreakpoint(this, addr
);
736 hardBreakMap
[addr
] = bkpt
;
737 i
= hardBreakMap
.insert(make_pair(addr
, bkpt
));
738 if (i
== hardBreakMap
.end())
742 (*i
).second
->refcount
++;
747 RemoteGDB::removeHardBreak(Addr addr
, size_t len
)
749 if (len
!= sizeof(MachInst
))
750 panic("invalid length\n");
752 DPRINTF(RGDB
, "removing hardware breakpoint at %#x\n", addr
);
754 break_iter_t i
= hardBreakMap
.find(addr
);
755 if (i
== hardBreakMap
.end())
758 HardBreakpoint
*hbp
= (*i
).second
;
759 if (--hbp
->refcount
== 0) {
761 hardBreakMap
.erase(i
);
771 case '0': return "software breakpoint";
772 case '1': return "hardware breakpoint";
773 case '2': return "write watchpoint";
774 case '3': return "read watchpoint";
775 case '4': return "access watchpoint";
776 default: return "unknown breakpoint/watchpoint";
780 // This function does all command processing for interfacing to a
781 // remote gdb. Note that the error codes are ignored by gdb at
782 // present, but might eventually become meaningful. (XXX) It might
783 // makes sense to use POSIX errno values, because that is what the
784 // gdb/remote.c functions want to return.
786 RemoteGDB::trap(int type
)
790 char data
[KGDB_BUFLEN
+ 1];
791 char buffer
[sizeof(gdbregs
) * 2 + 256];
792 char temp
[KGDB_BUFLEN
];
794 char command
, subcmd
;
801 DPRINTF(RGDB
, "RGDB(trap): PC=%#x NPC=%#x\n",
802 context
->regs
.pc
, context
->regs
.npc
);
807 * The first entry to this function is normally through
808 * a breakpoint trap in kgdb_connect(), in which case we
809 * must advance past the breakpoint because gdb will not.
811 * On the first entry here, we expect that gdb is not yet
812 * listening to us, so just enter the interaction loop.
813 * After the debugger is "active" (connected) it will be
814 * waiting for a "signaled" message from us.
817 if (!IS_BREAKPOINT_TRAP(type
, 0)) {
818 // No debugger active -- let trap handle this.
823 // Tell remote host that an exception has occurred.
824 sprintf((char *)buffer
, "S%02x", signal(type
));
828 // Stick frame regs into our reg cache.
832 datalen
= recv(data
, sizeof(data
));
833 data
[sizeof(data
) - 1] = 0; // Sentinel
840 // if this command came from a running gdb, answer it --
841 // the other guy has no way of knowing if we're in or out
842 // of this loop when he issues a "remote-signal".
843 sprintf((char *)buffer
, "S%02x", signal(type
));
848 if (2 * sizeof(gdbregs
) > sizeof(buffer
))
849 panic("buffer too small");
851 mem2hex(buffer
, gdbregs
, sizeof(gdbregs
));
856 p
= hex2mem(gdbregs
, p
, sizeof(gdbregs
));
857 if (p
== NULL
|| *p
!= '\0')
872 if (val
< 0 && val
>= KGDB_NUMREGS
) {
877 gdbregs
[val
] = hex2i(&p
);
895 if (len
> sizeof(buffer
)) {
899 if (!acc(val
, len
)) {
904 if (read(val
, (size_t)len
, (char *)buffer
)) {
905 mem2hex(temp
, buffer
, len
);
923 if (len
> datalen
- (p
- data
)) {
927 p
= hex2mem(buffer
, p
, sizeof(buffer
));
932 if (!acc(val
, len
)) {
936 if (write(val
, (size_t)len
, (char *)buffer
))
942 case KGDB_SET_THREAD
:
958 case KGDB_ASYNC_CONT
:
962 context
->regs
.pc
= val
;
963 context
->regs
.npc
= val
+ sizeof(MachInst
);
969 if (p
- data
< datalen
) {
971 context
->regs
.pc
= val
;
972 context
->regs
.npc
= val
+ sizeof(MachInst
);
977 case KGDB_ASYNC_STEP
:
981 context
->regs
.pc
= val
;
982 context
->regs
.npc
= val
+ sizeof(MachInst
);
988 if (p
- data
< datalen
) {
990 context
->regs
.pc
= val
;
991 context
->regs
.npc
= val
+ sizeof(MachInst
);
996 case KGDB_CLR_HW_BKPT
:
998 if (*p
++ != ',') send("E0D");
1000 if (*p
++ != ',') send("E0D");
1003 DPRINTF(RGDB
, "kgdb: clear %s, addr=%#x, len=%d\n",
1004 break_type(subcmd
), val
, len
);
1009 case '0': // software breakpoint
1010 ret
= removeSoftBreak(val
, len
);
1013 case '1': // hardware breakpoint
1014 ret
= removeHardBreak(val
, len
);
1017 case '2': // write watchpoint
1018 case '3': // read watchpoint
1019 case '4': // access watchpoint
1025 send(ret
? "OK" : "E0C");
1028 case KGDB_SET_HW_BKPT
:
1030 if (*p
++ != ',') send("E0D");
1032 if (*p
++ != ',') send("E0D");
1035 DPRINTF(RGDB
, "kgdb: set %s, addr=%#x, len=%d\n",
1036 break_type(subcmd
), val
, len
);
1041 case '0': // software breakpoint
1042 ret
= insertSoftBreak(val
, len
);
1045 case '1': // hardware breakpoint
1046 ret
= insertHardBreak(val
, len
);
1049 case '2': // write watchpoint
1050 case '3': // read watchpoint
1051 case '4': // access watchpoint
1057 send(ret
? "OK" : "E0C");
1060 case KGDB_QUERY_VAR
:
1061 var
= string(p
, datalen
- 1);
1069 case KGDB_SET_BREAK
:
1071 case KGDB_CYCLE_STEP
:
1072 case KGDB_SIG_CYCLE_STEP
:
1076 case KGDB_THREAD_ALIVE
:
1077 case KGDB_TARGET_EXIT
:
1078 case KGDB_BINARY_DLOAD
:
1079 // Unsupported command
1080 DPRINTF(RGDB
, "kgdb: Unsupported command: %s\n",
1081 gdb_command(command
));
1082 DDUMP(RGDB
, (uint8_t *)data
, datalen
);
1088 DPRINTF(RGDB
, "kgdb: Unknown command: %c(%#x)\n",
1101 // Convert a hex digit into an integer.
1102 // This returns -1 if the argument passed is no valid hex digit.
1106 if (c
>= '0' && c
<= '9')
1108 else if (c
>= 'a' && c
<= 'f')
1109 return (c
- 'a' + 10);
1110 else if (c
>= 'A' && c
<= 'F')
1112 return (c
- 'A' + 10);
1117 // Convert the low 4 bits of an integer into an hex digit.
1121 return ("0123456789abcdef"[n
& 0x0f]);
1124 // Convert a byte array into an hex string.
1126 mem2hex(void *vdst
, const void *vsrc
, int len
)
1128 char *dst
= (char *)vdst
;
1129 const char *src
= (const char *)vsrc
;
1132 *dst
++ = i2digit(*src
>> 4);
1133 *dst
++ = i2digit(*src
++);
1138 // Convert an hex string into a byte array.
1139 // This returns a pointer to the character following the last valid
1140 // hex digit. If the string ends in the middle of a byte, NULL is
1143 hex2mem(void *vdst
, const char *src
, int maxlen
)
1145 char *dst
= (char *)vdst
;
1148 while (*src
&& maxlen
--) {
1149 msb
= digit2i(*src
++);
1152 lsb
= digit2i(*src
++);
1155 *dst
++ = (msb
<< 4) | lsb
;
1160 // Convert an hex string into an integer.
1161 // This returns a pointer to the character following the last valid
1164 hex2i(const char **srcp
)
1166 const char *src
= *srcp
;
1170 while ((nibble
= digit2i(*src
)) >= 0) {