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.
28 * Authors: Nathan Binkert
32 * Copyright (c) 1990, 1993
33 * The Regents of the University of California. All rights reserved.
35 * This software was developed by the Computer Systems Engineering group
36 * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
37 * contributed to Berkeley.
39 * All advertising materials mentioning features or use of this software
40 * must display the following acknowledgement:
41 * This product includes software developed by the University of
42 * California, Lawrence Berkeley Laboratories.
44 * Redistribution and use in source and binary forms, with or without
45 * modification, are permitted provided that the following conditions
47 * 1. Redistributions of source code must retain the above copyright
48 * notice, this list of conditions and the following disclaimer.
49 * 2. Redistributions in binary form must reproduce the above copyright
50 * notice, this list of conditions and the following disclaimer in the
51 * documentation and/or other materials provided with the distribution.
52 * 3. All advertising materials mentioning features or use of this software
53 * must display the following acknowledgement:
54 * This product includes software developed by the University of
55 * California, Berkeley and its contributors.
56 * 4. Neither the name of the University nor the names of its contributors
57 * may be used to endorse or promote products derived from this software
58 * without specific prior written permission.
60 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
61 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
62 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
63 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
64 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
65 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
66 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
67 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
68 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
69 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
72 * @(#)kgdb_stub.c 8.4 (Berkeley) 1/12/94
76 * Copyright (c) 2001 The NetBSD Foundation, Inc.
77 * All rights reserved.
79 * This code is derived from software contributed to The NetBSD Foundation
82 * Redistribution and use in source and binary forms, with or without
83 * modification, are permitted provided that the following conditions
85 * 1. Redistributions of source code must retain the above copyright
86 * notice, this list of conditions and the following disclaimer.
87 * 2. Redistributions in binary form must reproduce the above copyright
88 * notice, this list of conditions and the following disclaimer in the
89 * documentation and/or other materials provided with the distribution.
90 * 3. All advertising materials mentioning features or use of this software
91 * must display the following acknowledgement:
92 * This product includes software developed by the NetBSD
93 * Foundation, Inc. and its contributors.
94 * 4. Neither the name of The NetBSD Foundation nor the names of its
95 * contributors may be used to endorse or promote products derived
96 * from this software without specific prior written permission.
98 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
99 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
100 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
101 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
102 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
103 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
104 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
105 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
106 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
107 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
108 * POSSIBILITY OF SUCH DAMAGE.
112 * $NetBSD: kgdb_stub.c,v 1.8 2001/07/07 22:58:00 wdk Exp $
116 * "Stub" to allow remote cpu to debug over a serial line using gdb.
119 #include <sys/signal.h>
124 #include "arch/vtophys.hh"
125 #include "base/intmath.hh"
126 #include "base/kgdb.h"
127 #include "base/remote_gdb.hh"
128 #include "base/socket.hh"
129 #include "base/trace.hh"
130 #include "config/full_system.hh"
131 #include "cpu/thread_context.hh"
132 #include "cpu/static_inst.hh"
133 #include "mem/physical.hh"
134 #include "mem/port.hh"
135 #include "sim/system.hh"
138 using namespace TheISA
;
141 vector
<RemoteGDB
*> debuggers
;
142 int current_debugger
= -1;
147 if (current_debugger
>= 0 && current_debugger
< debuggers
.size()) {
148 RemoteGDB
*gdb
= debuggers
[current_debugger
];
149 if (!gdb
->isattached())
150 gdb
->listener
->accept();
151 if (gdb
->isattached())
152 gdb
->trap(ALPHA_KENTRY_IF
);
157 ///////////////////////////////////////////////////////////
162 GDBListener::Event::Event(GDBListener
*l
, int fd
, int e
)
163 : PollEvent(fd
, e
), listener(l
)
167 GDBListener::Event::process(int revent
)
172 GDBListener::GDBListener(RemoteGDB
*g
, int p
)
173 : event(NULL
), gdb(g
), port(p
)
175 assert(!gdb
->listener
);
176 gdb
->listener
= this;
179 GDBListener::~GDBListener()
188 return gdb
->name() + ".listener";
192 GDBListener::listen()
194 while (!listener
.listen(port
, true)) {
195 DPRINTF(GDBMisc
, "Can't bind port %d\n", port
);
199 event
= new Event(this, listener
.getfd(), POLLIN
);
200 pollQueue
.schedule(event
);
203 gdb
->number
= debuggers
.size();
204 debuggers
.push_back(gdb
);
208 ccprintf(cerr
, "%d: %s: listening for remote gdb #%d on port %d\n",
209 curTick
, name(), gdb
->number
, port
);
211 ccprintf(cerr
, "%d: %s: listening for remote gdb on port %d\n",
212 curTick
, name(), port
);
217 GDBListener::accept()
219 if (!listener
.islistening())
220 panic("GDBListener::accept(): cannot accept if we're not listening!");
222 int sfd
= listener
.accept(true);
225 if (gdb
->isattached())
232 ///////////////////////////////////////////////////////////
238 void mem2hex(void *, const void *, int);
239 const char *hex2mem(void *, const char *, int);
240 Addr
hex2i(const char **);
242 RemoteGDB::Event::Event(RemoteGDB
*g
, int fd
, int e
)
243 : PollEvent(fd
, e
), gdb(g
)
247 RemoteGDB::Event::process(int revent
)
250 gdb
->trap(ALPHA_KENTRY_IF
);
251 else if (revent
& POLLNVAL
)
255 RemoteGDB::RemoteGDB(System
*_system
, ThreadContext
*c
)
256 : event(NULL
), listener(NULL
), number(-1), fd(-1),
257 active(false), attached(false),
258 system(_system
), pmem(_system
->physmem
), context(c
)
260 memset(gdbregs
, 0, sizeof(gdbregs
));
263 RemoteGDB::~RemoteGDB()
272 return system
->name() + ".remote_gdb";
276 RemoteGDB::isattached()
280 RemoteGDB::attach(int f
)
284 event
= new Event(this, fd
, POLLIN
);
285 pollQueue
.schedule(event
);
288 DPRINTFN("remote gdb attached\n");
298 pollQueue
.remove(event
);
299 DPRINTFN("remote gdb detached\n");
303 gdb_command(char cmd
)
306 case KGDB_SIGNAL
: return "KGDB_SIGNAL";
307 case KGDB_SET_BAUD
: return "KGDB_SET_BAUD";
308 case KGDB_SET_BREAK
: return "KGDB_SET_BREAK";
309 case KGDB_CONT
: return "KGDB_CONT";
310 case KGDB_ASYNC_CONT
: return "KGDB_ASYNC_CONT";
311 case KGDB_DEBUG
: return "KGDB_DEBUG";
312 case KGDB_DETACH
: return "KGDB_DETACH";
313 case KGDB_REG_R
: return "KGDB_REG_R";
314 case KGDB_REG_W
: return "KGDB_REG_W";
315 case KGDB_SET_THREAD
: return "KGDB_SET_THREAD";
316 case KGDB_CYCLE_STEP
: return "KGDB_CYCLE_STEP";
317 case KGDB_SIG_CYCLE_STEP
: return "KGDB_SIG_CYCLE_STEP";
318 case KGDB_KILL
: return "KGDB_KILL";
319 case KGDB_MEM_W
: return "KGDB_MEM_W";
320 case KGDB_MEM_R
: return "KGDB_MEM_R";
321 case KGDB_SET_REG
: return "KGDB_SET_REG";
322 case KGDB_READ_REG
: return "KGDB_READ_REG";
323 case KGDB_QUERY_VAR
: return "KGDB_QUERY_VAR";
324 case KGDB_SET_VAR
: return "KGDB_SET_VAR";
325 case KGDB_RESET
: return "KGDB_RESET";
326 case KGDB_STEP
: return "KGDB_STEP";
327 case KGDB_ASYNC_STEP
: return "KGDB_ASYNC_STEP";
328 case KGDB_THREAD_ALIVE
: return "KGDB_THREAD_ALIVE";
329 case KGDB_TARGET_EXIT
: return "KGDB_TARGET_EXIT";
330 case KGDB_BINARY_DLOAD
: return "KGDB_BINARY_DLOAD";
331 case KGDB_CLR_HW_BKPT
: return "KGDB_CLR_HW_BKPT";
332 case KGDB_SET_HW_BKPT
: return "KGDB_SET_HW_BKPT";
333 case KGDB_START
: return "KGDB_START";
334 case KGDB_END
: return "KGDB_END";
335 case KGDB_GOODP
: return "KGDB_GOODP";
336 case KGDB_BADP
: return "KGDB_BADP";
337 default: return "KGDB_UNKNOWN";
341 ///////////////////////////////////////////////////////////
344 // Determine if the mapping at va..(va+len) is valid.
347 RemoteGDB::acc(Addr va
, size_t len
)
351 va
= TheISA::TruncPage(va
);
352 last_va
= TheISA::RoundPage(va
+ len
);
355 if (TheISA::IsK0Seg(va
)) {
356 if (va
< (TheISA::K0SegBase
+ pmem
->size())) {
357 DPRINTF(GDBAcc
, "acc: Mapping is valid K0SEG <= "
358 "%#x < K0SEG + size\n", va
);
361 DPRINTF(GDBAcc
, "acc: Mapping invalid %#x > K0SEG + size\n",
368 * This code says that all accesses to palcode (instruction and data)
369 * are valid since there isn't a va->pa mapping because palcode is
370 * accessed physically. At some point this should probably be cleaned up
371 * but there is no easy way to do it.
374 if (AlphaISA::PcPAL(va
) || va
< 0x10000)
377 Addr ptbr
= context
->readMiscReg(AlphaISA::IPR_PALtemp20
);
378 TheISA::PageTableEntry pte
= TheISA::kernel_pte_lookup(context
->getPhysPort(), ptbr
, va
);
380 DPRINTF(GDBAcc
, "acc: %#x pte is invalid\n", va
);
383 va
+= TheISA::PageBytes
;
384 } while (va
< last_va
);
386 DPRINTF(GDBAcc
, "acc: %#x mapping is valid\n", va
);
390 ///////////////////////////////////////////////////////////
393 // Translate a trap number into a Unix-compatible signal number.
394 // (GDB only understands Unix signal numbers.)
397 RemoteGDB::signal(int type
)
400 case ALPHA_KENTRY_INT
:
403 case ALPHA_KENTRY_UNA
:
406 case ALPHA_KENTRY_ARITH
:
409 case ALPHA_KENTRY_IF
:
412 case ALPHA_KENTRY_MM
:
416 panic("unknown signal type");
421 ///////////////////////////////////////////////////////////
422 // RemoteGDB::getregs
424 // Translate the kernel debugger register format into
425 // the GDB register format.
429 memset(gdbregs
, 0, sizeof(gdbregs
));
431 gdbregs
[KGDB_REG_PC
] = context
->readPC();
433 // @todo: Currently this is very Alpha specific.
434 if (AlphaISA::PcPAL(gdbregs
[KGDB_REG_PC
])) {
435 for (int i
= 0; i
< TheISA::NumIntArchRegs
; ++i
) {
436 gdbregs
[i
] = context
->readIntReg(AlphaISA::reg_redir
[i
]);
439 for (int i
= 0; i
< TheISA::NumIntArchRegs
; ++i
) {
440 gdbregs
[i
] = context
->readIntReg(i
);
445 for (int i
= 0; i
< TheISA::NumFloatArchRegs
; ++i
) {
446 gdbregs
[i
+ KGDB_REG_F0
] = context
->readFloatRegBits(i
);
451 ///////////////////////////////////////////////////////////
452 // RemoteGDB::setregs
454 // Translate the GDB register format into the kernel
455 // debugger register format.
460 // @todo: Currently this is very Alpha specific.
461 if (AlphaISA::PcPAL(gdbregs
[KGDB_REG_PC
])) {
462 for (int i
= 0; i
< TheISA::NumIntArchRegs
; ++i
) {
463 context
->setIntReg(AlphaISA::reg_redir
[i
], gdbregs
[i
]);
466 for (int i
= 0; i
< TheISA::NumIntArchRegs
; ++i
) {
467 context
->setIntReg(i
, gdbregs
[i
]);
472 for (int i
= 0; i
< TheISA::NumFloatArchRegs
; ++i
) {
473 context
->setFloatRegBits(i
, gdbregs
[i
+ KGDB_REG_F0
]);
476 context
->setPC(gdbregs
[KGDB_REG_PC
]);
480 RemoteGDB::setTempBreakpoint(TempBreakpoint
&bkpt
, Addr addr
)
482 DPRINTF(GDBMisc
, "setTempBreakpoint: addr=%#x\n", addr
);
485 insertHardBreak(addr
, 4);
489 RemoteGDB::clearTempBreakpoint(TempBreakpoint
&bkpt
)
491 DPRINTF(GDBMisc
, "setTempBreakpoint: addr=%#x\n",
495 removeHardBreak(bkpt
.address
, 4);
500 RemoteGDB::clearSingleStep()
502 DPRINTF(GDBMisc
, "clearSingleStep bt_addr=%#x nt_addr=%#x\n",
503 takenBkpt
.address
, notTakenBkpt
.address
);
505 if (takenBkpt
.address
!= 0)
506 clearTempBreakpoint(takenBkpt
);
508 if (notTakenBkpt
.address
!= 0)
509 clearTempBreakpoint(notTakenBkpt
);
513 RemoteGDB::setSingleStep()
515 Addr pc
= context
->readPC();
519 npc
= pc
+ sizeof(MachInst
);
521 // User was stopped at pc, e.g. the instruction at pc was not
523 MachInst inst
= read
<MachInst
>(pc
);
524 StaticInstPtr
si(inst
);
525 if (si
->hasBranchTarget(pc
, context
, bpc
)) {
526 // Don't bother setting a breakpoint on the taken branch if it
527 // is the same as the next pc
532 DPRINTF(GDBMisc
, "setSingleStep bt_addr=%#x nt_addr=%#x\n",
533 takenBkpt
.address
, notTakenBkpt
.address
);
535 setTempBreakpoint(notTakenBkpt
, npc
);
538 setTempBreakpoint(takenBkpt
, bpc
);
541 /////////////////////////
554 RemoteGDB::putbyte(uint8_t b
)
559 // Send a packet to gdb
561 RemoteGDB::send(const char *bp
)
566 DPRINTF(GDBSend
, "send: %s\n", bp
);
571 for (csum
= 0; (c
= *p
); p
++) {
576 putbyte(i2digit(csum
>> 4));
577 putbyte(i2digit(csum
));
578 } while ((c
= getbyte() & 0x7f) == KGDB_BADP
);
581 // Receive a packet from gdb
583 RemoteGDB::recv(char *bp
, int maxlen
)
592 while ((c
= getbyte()) != KGDB_START
)
595 while ((c
= getbyte()) != KGDB_END
&& len
< maxlen
) {
609 csum
-= digit2i(getbyte()) * 16;
610 csum
-= digit2i(getbyte());
619 bcopy(bp
+ 3, bp
, len
);
626 DPRINTF(GDBRecv
, "recv: %s: %s\n", gdb_command(*bp
), bp
);
631 // Read bytes from kernel address space for debugger.
633 RemoteGDB::read(Addr vaddr
, size_t size
, char *data
)
635 static Addr lastaddr
= 0;
636 static size_t lastsize
= 0;
639 DPRINTF(GDBRead
, "read: reading memory location zero!\n");
640 vaddr
= lastaddr
+ lastsize
;
643 DPRINTF(GDBRead
, "read: addr=%#x, size=%d", vaddr
, size
);
645 context
->getVirtPort(context
)->readBlob(vaddr
, (uint8_t*)data
, size
);
648 if (DTRACE(GDBRead
)) {
649 if (DTRACE(GDBExtra
)) {
651 mem2hex(buf
, data
, size
);
652 DPRINTFNR(": %s\n", buf
);
661 // Write bytes to kernel address space for debugger.
663 RemoteGDB::write(Addr vaddr
, size_t size
, const char *data
)
665 static Addr lastaddr
= 0;
666 static size_t lastsize
= 0;
669 DPRINTF(GDBWrite
, "write: writing memory location zero!\n");
670 vaddr
= lastaddr
+ lastsize
;
673 if (DTRACE(GDBWrite
)) {
674 DPRINTFN("write: addr=%#x, size=%d", vaddr
, size
);
675 if (DTRACE(GDBExtra
)) {
677 mem2hex(buf
, data
, size
);
678 DPRINTFNR(": %s\n", buf
);
683 context
->getVirtPort(context
)->writeBlob(vaddr
, (uint8_t*)data
, size
);
693 PCEventQueue
*RemoteGDB::getPcEventQueue()
695 return &system
->pcEventQueue
;
699 RemoteGDB::HardBreakpoint::HardBreakpoint(RemoteGDB
*_gdb
, Addr pc
)
700 : PCEvent(_gdb
->getPcEventQueue(), "HardBreakpoint Event", pc
),
701 gdb(_gdb
), refcount(0)
703 DPRINTF(GDBMisc
, "creating hardware breakpoint at %#x\n", evpc
);
707 RemoteGDB::HardBreakpoint::process(ThreadContext
*tc
)
709 DPRINTF(GDBMisc
, "handling hardware breakpoint at %#x\n", pc());
711 if (tc
== gdb
->context
)
712 gdb
->trap(ALPHA_KENTRY_INT
);
716 RemoteGDB::insertSoftBreak(Addr addr
, size_t len
)
718 if (len
!= sizeof(MachInst
))
719 panic("invalid length\n");
721 return insertHardBreak(addr
, len
);
725 RemoteGDB::removeSoftBreak(Addr addr
, size_t len
)
727 if (len
!= sizeof(MachInst
))
728 panic("invalid length\n");
730 return removeHardBreak(addr
, len
);
734 RemoteGDB::insertHardBreak(Addr addr
, size_t len
)
736 if (len
!= sizeof(MachInst
))
737 panic("invalid length\n");
739 DPRINTF(GDBMisc
, "inserting hardware breakpoint at %#x\n", addr
);
741 HardBreakpoint
*&bkpt
= hardBreakMap
[addr
];
743 bkpt
= new HardBreakpoint(this, addr
);
751 RemoteGDB::removeHardBreak(Addr addr
, size_t len
)
753 if (len
!= sizeof(MachInst
))
754 panic("invalid length\n");
756 DPRINTF(GDBMisc
, "removing hardware breakpoint at %#x\n", addr
);
758 break_iter_t i
= hardBreakMap
.find(addr
);
759 if (i
== hardBreakMap
.end())
762 HardBreakpoint
*hbp
= (*i
).second
;
763 if (--hbp
->refcount
== 0) {
765 hardBreakMap
.erase(i
);
775 case '0': return "software breakpoint";
776 case '1': return "hardware breakpoint";
777 case '2': return "write watchpoint";
778 case '3': return "read watchpoint";
779 case '4': return "access watchpoint";
780 default: return "unknown breakpoint/watchpoint";
784 // This function does all command processing for interfacing to a
785 // remote gdb. Note that the error codes are ignored by gdb at
786 // present, but might eventually become meaningful. (XXX) It might
787 // makes sense to use POSIX errno values, because that is what the
788 // gdb/remote.c functions want to return.
790 RemoteGDB::trap(int type
)
794 char data
[KGDB_BUFLEN
+ 1];
795 char buffer
[sizeof(gdbregs
) * 2 + 256];
796 char temp
[KGDB_BUFLEN
];
798 char command
, subcmd
;
805 DPRINTF(GDBMisc
, "trap: PC=%#x NPC=%#x\n",
806 context
->readPC(), context
->readNextPC());
811 * The first entry to this function is normally through
812 * a breakpoint trap in kgdb_connect(), in which case we
813 * must advance past the breakpoint because gdb will not.
815 * On the first entry here, we expect that gdb is not yet
816 * listening to us, so just enter the interaction loop.
817 * After the debugger is "active" (connected) it will be
818 * waiting for a "signaled" message from us.
823 // Tell remote host that an exception has occurred.
824 snprintf((char *)buffer
, sizeof(buffer
), "S%02x", signal(type
));
827 // Stick frame regs into our reg cache.
831 datalen
= recv(data
, sizeof(data
));
832 data
[sizeof(data
) - 1] = 0; // Sentinel
839 // if this command came from a running gdb, answer it --
840 // the other guy has no way of knowing if we're in or out
841 // of this loop when he issues a "remote-signal".
842 snprintf((char *)buffer
, sizeof(buffer
), "S%02x", signal(type
));
847 if (2 * sizeof(gdbregs
) > sizeof(buffer
))
848 panic("buffer too small");
850 mem2hex(buffer
, gdbregs
, sizeof(gdbregs
));
855 p
= hex2mem(gdbregs
, p
, sizeof(gdbregs
));
856 if (p
== NULL
|| *p
!= '\0')
871 if (val
< 0 && val
>= KGDB_NUMREGS
) {
876 gdbregs
[val
] = hex2i(&p
);
894 if (len
> sizeof(buffer
)) {
898 if (!acc(val
, len
)) {
903 if (read(val
, (size_t)len
, (char *)buffer
)) {
904 mem2hex(temp
, buffer
, len
);
922 if (len
> datalen
- (p
- data
)) {
926 p
= hex2mem(buffer
, p
, sizeof(buffer
));
931 if (!acc(val
, len
)) {
935 if (write(val
, (size_t)len
, (char *)buffer
))
941 case KGDB_SET_THREAD
:
957 case KGDB_ASYNC_CONT
:
962 context
->setNextPC(val
+ sizeof(MachInst
));
968 if (p
- data
< datalen
) {
971 context
->setNextPC(val
+ sizeof(MachInst
));
976 case KGDB_ASYNC_STEP
:
981 context
->setNextPC(val
+ sizeof(MachInst
));
987 if (p
- data
< datalen
) {
990 context
->setNextPC(val
+ sizeof(MachInst
));
995 case KGDB_CLR_HW_BKPT
:
997 if (*p
++ != ',') send("E0D");
999 if (*p
++ != ',') send("E0D");
1002 DPRINTF(GDBMisc
, "clear %s, addr=%#x, len=%d\n",
1003 break_type(subcmd
), val
, len
);
1008 case '0': // software breakpoint
1009 ret
= removeSoftBreak(val
, len
);
1012 case '1': // hardware breakpoint
1013 ret
= removeHardBreak(val
, len
);
1016 case '2': // write watchpoint
1017 case '3': // read watchpoint
1018 case '4': // access watchpoint
1024 send(ret
? "OK" : "E0C");
1027 case KGDB_SET_HW_BKPT
:
1029 if (*p
++ != ',') send("E0D");
1031 if (*p
++ != ',') send("E0D");
1034 DPRINTF(GDBMisc
, "set %s, addr=%#x, len=%d\n",
1035 break_type(subcmd
), val
, len
);
1040 case '0': // software breakpoint
1041 ret
= insertSoftBreak(val
, len
);
1044 case '1': // hardware breakpoint
1045 ret
= insertHardBreak(val
, len
);
1048 case '2': // write watchpoint
1049 case '3': // read watchpoint
1050 case '4': // access watchpoint
1056 send(ret
? "OK" : "E0C");
1059 case KGDB_QUERY_VAR
:
1060 var
= string(p
, datalen
- 1);
1068 case KGDB_SET_BREAK
:
1070 case KGDB_CYCLE_STEP
:
1071 case KGDB_SIG_CYCLE_STEP
:
1075 case KGDB_THREAD_ALIVE
:
1076 case KGDB_TARGET_EXIT
:
1077 case KGDB_BINARY_DLOAD
:
1078 // Unsupported command
1079 DPRINTF(GDBMisc
, "Unsupported command: %s\n",
1080 gdb_command(command
));
1081 DDUMP(GDBMisc
, (uint8_t *)data
, datalen
);
1087 DPRINTF(GDBMisc
, "Unknown command: %c(%#x)\n",
1100 // Convert a hex digit into an integer.
1101 // This returns -1 if the argument passed is no valid hex digit.
1105 if (c
>= '0' && c
<= '9')
1107 else if (c
>= 'a' && c
<= 'f')
1108 return (c
- 'a' + 10);
1109 else if (c
>= 'A' && c
<= 'F')
1111 return (c
- 'A' + 10);
1116 // Convert the low 4 bits of an integer into an hex digit.
1120 return ("0123456789abcdef"[n
& 0x0f]);
1123 // Convert a byte array into an hex string.
1125 mem2hex(void *vdst
, const void *vsrc
, int len
)
1127 char *dst
= (char *)vdst
;
1128 const char *src
= (const char *)vsrc
;
1131 *dst
++ = i2digit(*src
>> 4);
1132 *dst
++ = i2digit(*src
++);
1137 // Convert an hex string into a byte array.
1138 // This returns a pointer to the character following the last valid
1139 // hex digit. If the string ends in the middle of a byte, NULL is
1142 hex2mem(void *vdst
, const char *src
, int maxlen
)
1144 char *dst
= (char *)vdst
;
1147 while (*src
&& maxlen
--) {
1148 msb
= digit2i(*src
++);
1151 lsb
= digit2i(*src
++);
1154 *dst
++ = (msb
<< 4) | lsb
;
1159 // Convert an hex string into an integer.
1160 // This returns a pointer to the character following the last valid
1163 hex2i(const char **srcp
)
1165 const char *src
= *srcp
;
1169 while ((nibble
= digit2i(*src
)) >= 0) {