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>
122 #include "arch/vtophys.hh"
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 "config/full_system.hh"
129 #include "cpu/exec_context.hh"
130 #include "cpu/static_inst.hh"
131 #include "mem/physical.hh"
132 #include "mem/port.hh"
133 #include "sim/system.hh"
136 using namespace TheISA
;
139 vector
<RemoteGDB
*> debuggers
;
140 int current_debugger
= -1;
145 if (current_debugger
>= 0 && current_debugger
< debuggers
.size()) {
146 RemoteGDB
*gdb
= debuggers
[current_debugger
];
147 if (!gdb
->isattached())
148 gdb
->listener
->accept();
149 if (gdb
->isattached())
150 gdb
->trap(ALPHA_KENTRY_IF
);
155 ///////////////////////////////////////////////////////////
160 GDBListener::Event::Event(GDBListener
*l
, int fd
, int e
)
161 : PollEvent(fd
, e
), listener(l
)
165 GDBListener::Event::process(int revent
)
170 GDBListener::GDBListener(RemoteGDB
*g
, int p
)
171 : event(NULL
), gdb(g
), port(p
)
173 assert(!gdb
->listener
);
174 gdb
->listener
= this;
177 GDBListener::~GDBListener()
186 return gdb
->name() + ".listener";
190 GDBListener::listen()
192 while (!listener
.listen(port
, true)) {
193 DPRINTF(GDBMisc
, "Can't bind port %d\n", port
);
197 event
= new Event(this, listener
.getfd(), POLLIN
);
198 pollQueue
.schedule(event
);
201 gdb
->number
= debuggers
.size();
202 debuggers
.push_back(gdb
);
206 ccprintf(cerr
, "%d: %s: listening for remote gdb #%d on port %d\n",
207 curTick
, name(), gdb
->number
, port
);
209 ccprintf(cerr
, "%d: %s: listening for remote gdb on port %d\n",
210 curTick
, name(), port
);
215 GDBListener::accept()
217 if (!listener
.islistening())
218 panic("GDBListener::accept(): cannot accept if we're not listening!");
220 int sfd
= listener
.accept(true);
223 if (gdb
->isattached())
230 ///////////////////////////////////////////////////////////
236 void mem2hex(void *, const void *, int);
237 const char *hex2mem(void *, const char *, int);
238 Addr
hex2i(const char **);
240 RemoteGDB::Event::Event(RemoteGDB
*g
, int fd
, int e
)
241 : PollEvent(fd
, e
), gdb(g
)
245 RemoteGDB::Event::process(int revent
)
248 gdb
->trap(ALPHA_KENTRY_IF
);
249 else if (revent
& POLLNVAL
)
253 RemoteGDB::RemoteGDB(System
*_system
, ExecContext
*c
)
254 : event(NULL
), listener(NULL
), number(-1), fd(-1),
255 active(false), attached(false),
256 system(_system
), pmem(_system
->physmem
), context(c
)
258 memset(gdbregs
, 0, sizeof(gdbregs
));
261 RemoteGDB::~RemoteGDB()
270 return system
->name() + ".remote_gdb";
274 RemoteGDB::isattached()
278 RemoteGDB::attach(int f
)
282 event
= new Event(this, fd
, POLLIN
);
283 pollQueue
.schedule(event
);
286 DPRINTFN("remote gdb attached\n");
296 pollQueue
.remove(event
);
297 DPRINTFN("remote gdb detached\n");
301 gdb_command(char cmd
)
304 case KGDB_SIGNAL
: return "KGDB_SIGNAL";
305 case KGDB_SET_BAUD
: return "KGDB_SET_BAUD";
306 case KGDB_SET_BREAK
: return "KGDB_SET_BREAK";
307 case KGDB_CONT
: return "KGDB_CONT";
308 case KGDB_ASYNC_CONT
: return "KGDB_ASYNC_CONT";
309 case KGDB_DEBUG
: return "KGDB_DEBUG";
310 case KGDB_DETACH
: return "KGDB_DETACH";
311 case KGDB_REG_R
: return "KGDB_REG_R";
312 case KGDB_REG_W
: return "KGDB_REG_W";
313 case KGDB_SET_THREAD
: return "KGDB_SET_THREAD";
314 case KGDB_CYCLE_STEP
: return "KGDB_CYCLE_STEP";
315 case KGDB_SIG_CYCLE_STEP
: return "KGDB_SIG_CYCLE_STEP";
316 case KGDB_KILL
: return "KGDB_KILL";
317 case KGDB_MEM_W
: return "KGDB_MEM_W";
318 case KGDB_MEM_R
: return "KGDB_MEM_R";
319 case KGDB_SET_REG
: return "KGDB_SET_REG";
320 case KGDB_READ_REG
: return "KGDB_READ_REG";
321 case KGDB_QUERY_VAR
: return "KGDB_QUERY_VAR";
322 case KGDB_SET_VAR
: return "KGDB_SET_VAR";
323 case KGDB_RESET
: return "KGDB_RESET";
324 case KGDB_STEP
: return "KGDB_STEP";
325 case KGDB_ASYNC_STEP
: return "KGDB_ASYNC_STEP";
326 case KGDB_THREAD_ALIVE
: return "KGDB_THREAD_ALIVE";
327 case KGDB_TARGET_EXIT
: return "KGDB_TARGET_EXIT";
328 case KGDB_BINARY_DLOAD
: return "KGDB_BINARY_DLOAD";
329 case KGDB_CLR_HW_BKPT
: return "KGDB_CLR_HW_BKPT";
330 case KGDB_SET_HW_BKPT
: return "KGDB_SET_HW_BKPT";
331 case KGDB_START
: return "KGDB_START";
332 case KGDB_END
: return "KGDB_END";
333 case KGDB_GOODP
: return "KGDB_GOODP";
334 case KGDB_BADP
: return "KGDB_BADP";
335 default: return "KGDB_UNKNOWN";
339 ///////////////////////////////////////////////////////////
342 // Determine if the mapping at va..(va+len) is valid.
345 RemoteGDB::acc(Addr va
, size_t len
)
349 va
= TheISA::TruncPage(va
);
350 last_va
= TheISA::RoundPage(va
+ len
);
353 if (TheISA::IsK0Seg(va
)) {
354 if (va
< (TheISA::K0SegBase
+ pmem
->size())) {
355 DPRINTF(GDBAcc
, "acc: Mapping is valid K0SEG <= "
356 "%#x < K0SEG + size\n", va
);
359 DPRINTF(GDBAcc
, "acc: Mapping invalid %#x > K0SEG + size\n",
366 * This code says that all accesses to palcode (instruction and data)
367 * are valid since there isn't a va->pa mapping because palcode is
368 * accessed physically. At some point this should probably be cleaned up
369 * but there is no easy way to do it.
372 if (AlphaISA::PcPAL(va
) || va
< 0x10000)
375 Addr ptbr
= context
->readMiscReg(AlphaISA::IPR_PALtemp20
);
376 TheISA::PageTableEntry pte
= TheISA::kernel_pte_lookup(context
->getPhysPort(), ptbr
, va
);
378 DPRINTF(GDBAcc
, "acc: %#x pte is invalid\n", va
);
381 va
+= TheISA::PageBytes
;
382 } while (va
< last_va
);
384 DPRINTF(GDBAcc
, "acc: %#x mapping is valid\n", va
);
388 ///////////////////////////////////////////////////////////
391 // Translate a trap number into a Unix-compatible signal number.
392 // (GDB only understands Unix signal numbers.)
395 RemoteGDB::signal(int type
)
398 case ALPHA_KENTRY_INT
:
401 case ALPHA_KENTRY_UNA
:
404 case ALPHA_KENTRY_ARITH
:
407 case ALPHA_KENTRY_IF
:
410 case ALPHA_KENTRY_MM
:
414 panic("unknown signal type");
419 ///////////////////////////////////////////////////////////
420 // RemoteGDB::getregs
422 // Translate the kernel debugger register format into
423 // the GDB register format.
427 memset(gdbregs
, 0, sizeof(gdbregs
));
429 gdbregs
[KGDB_REG_PC
] = context
->readPC();
431 // @todo: Currently this is very Alpha specific.
432 if (AlphaISA::PcPAL(gdbregs
[KGDB_REG_PC
])) {
433 for (int i
= 0; i
< TheISA::NumIntArchRegs
; ++i
) {
434 gdbregs
[i
] = context
->readIntReg(AlphaISA::reg_redir
[i
]);
437 for (int i
= 0; i
< TheISA::NumIntArchRegs
; ++i
) {
438 gdbregs
[i
] = context
->readIntReg(i
);
443 for (int i
= 0; i
< TheISA::NumFloatArchRegs
; ++i
) {
444 gdbregs
[i
+ KGDB_REG_F0
] = context
->readFloatRegBits(i
);
449 ///////////////////////////////////////////////////////////
450 // RemoteGDB::setregs
452 // Translate the GDB register format into the kernel
453 // debugger register format.
458 // @todo: Currently this is very Alpha specific.
459 if (AlphaISA::PcPAL(gdbregs
[KGDB_REG_PC
])) {
460 for (int i
= 0; i
< TheISA::NumIntArchRegs
; ++i
) {
461 context
->setIntReg(AlphaISA::reg_redir
[i
], gdbregs
[i
]);
464 for (int i
= 0; i
< TheISA::NumIntArchRegs
; ++i
) {
465 context
->setIntReg(i
, gdbregs
[i
]);
470 for (int i
= 0; i
< TheISA::NumFloatArchRegs
; ++i
) {
471 context
->setFloatRegBits(i
, gdbregs
[i
+ KGDB_REG_F0
]);
474 context
->setPC(gdbregs
[KGDB_REG_PC
]);
478 RemoteGDB::setTempBreakpoint(TempBreakpoint
&bkpt
, Addr addr
)
480 DPRINTF(GDBMisc
, "setTempBreakpoint: addr=%#x\n", addr
);
483 insertHardBreak(addr
, 4);
487 RemoteGDB::clearTempBreakpoint(TempBreakpoint
&bkpt
)
489 DPRINTF(GDBMisc
, "setTempBreakpoint: addr=%#x\n",
493 removeHardBreak(bkpt
.address
, 4);
498 RemoteGDB::clearSingleStep()
500 DPRINTF(GDBMisc
, "clearSingleStep bt_addr=%#x nt_addr=%#x\n",
501 takenBkpt
.address
, notTakenBkpt
.address
);
503 if (takenBkpt
.address
!= 0)
504 clearTempBreakpoint(takenBkpt
);
506 if (notTakenBkpt
.address
!= 0)
507 clearTempBreakpoint(notTakenBkpt
);
511 RemoteGDB::setSingleStep()
513 Addr pc
= context
->readPC();
517 npc
= pc
+ sizeof(MachInst
);
519 // User was stopped at pc, e.g. the instruction at pc was not
521 MachInst inst
= read
<MachInst
>(pc
);
522 StaticInstPtr
si(inst
);
523 if (si
->hasBranchTarget(pc
, context
, bpc
)) {
524 // Don't bother setting a breakpoint on the taken branch if it
525 // is the same as the next pc
530 DPRINTF(GDBMisc
, "setSingleStep bt_addr=%#x nt_addr=%#x\n",
531 takenBkpt
.address
, notTakenBkpt
.address
);
533 setTempBreakpoint(notTakenBkpt
, npc
);
536 setTempBreakpoint(takenBkpt
, bpc
);
539 /////////////////////////
552 RemoteGDB::putbyte(uint8_t b
)
557 // Send a packet to gdb
559 RemoteGDB::send(const char *bp
)
564 DPRINTF(GDBSend
, "send: %s\n", bp
);
569 for (csum
= 0; (c
= *p
); p
++) {
574 putbyte(i2digit(csum
>> 4));
575 putbyte(i2digit(csum
));
576 } while ((c
= getbyte() & 0x7f) == KGDB_BADP
);
579 // Receive a packet from gdb
581 RemoteGDB::recv(char *bp
, int maxlen
)
590 while ((c
= getbyte()) != KGDB_START
)
593 while ((c
= getbyte()) != KGDB_END
&& len
< maxlen
) {
607 csum
-= digit2i(getbyte()) * 16;
608 csum
-= digit2i(getbyte());
617 bcopy(bp
+ 3, bp
, len
);
624 DPRINTF(GDBRecv
, "recv: %s: %s\n", gdb_command(*bp
), bp
);
629 // Read bytes from kernel address space for debugger.
631 RemoteGDB::read(Addr vaddr
, size_t size
, char *data
)
633 static Addr lastaddr
= 0;
634 static size_t lastsize
= 0;
637 DPRINTF(GDBRead
, "read: reading memory location zero!\n");
638 vaddr
= lastaddr
+ lastsize
;
641 DPRINTF(GDBRead
, "read: addr=%#x, size=%d", vaddr
, size
);
643 context
->getVirtPort(context
)->readBlob(vaddr
, (uint8_t*)data
, size
);
646 if (DTRACE(GDBRead
)) {
647 if (DTRACE(GDBExtra
)) {
649 mem2hex(buf
, data
, size
);
650 DPRINTFNR(": %s\n", buf
);
659 // Write bytes to kernel address space for debugger.
661 RemoteGDB::write(Addr vaddr
, size_t size
, const char *data
)
663 static Addr lastaddr
= 0;
664 static size_t lastsize
= 0;
667 DPRINTF(GDBWrite
, "write: writing memory location zero!\n");
668 vaddr
= lastaddr
+ lastsize
;
671 if (DTRACE(GDBWrite
)) {
672 DPRINTFN("write: addr=%#x, size=%d", vaddr
, size
);
673 if (DTRACE(GDBExtra
)) {
675 mem2hex(buf
, data
, size
);
676 DPRINTFNR(": %s\n", buf
);
681 context
->getVirtPort(context
)->writeBlob(vaddr
, (uint8_t*)data
, size
);
691 PCEventQueue
*RemoteGDB::getPcEventQueue()
693 return &system
->pcEventQueue
;
697 RemoteGDB::HardBreakpoint::HardBreakpoint(RemoteGDB
*_gdb
, Addr pc
)
698 : PCEvent(_gdb
->getPcEventQueue(), "HardBreakpoint Event", pc
),
699 gdb(_gdb
), refcount(0)
701 DPRINTF(GDBMisc
, "creating hardware breakpoint at %#x\n", evpc
);
705 RemoteGDB::HardBreakpoint::process(ExecContext
*xc
)
707 DPRINTF(GDBMisc
, "handling hardware breakpoint at %#x\n", pc());
709 if (xc
== gdb
->context
)
710 gdb
->trap(ALPHA_KENTRY_INT
);
714 RemoteGDB::insertSoftBreak(Addr addr
, size_t len
)
716 if (len
!= sizeof(MachInst
))
717 panic("invalid length\n");
719 return insertHardBreak(addr
, len
);
723 RemoteGDB::removeSoftBreak(Addr addr
, size_t len
)
725 if (len
!= sizeof(MachInst
))
726 panic("invalid length\n");
728 return removeHardBreak(addr
, len
);
732 RemoteGDB::insertHardBreak(Addr addr
, size_t len
)
734 if (len
!= sizeof(MachInst
))
735 panic("invalid length\n");
737 DPRINTF(GDBMisc
, "inserting hardware breakpoint at %#x\n", addr
);
739 HardBreakpoint
*&bkpt
= hardBreakMap
[addr
];
741 bkpt
= new HardBreakpoint(this, addr
);
749 RemoteGDB::removeHardBreak(Addr addr
, size_t len
)
751 if (len
!= sizeof(MachInst
))
752 panic("invalid length\n");
754 DPRINTF(GDBMisc
, "removing hardware breakpoint at %#x\n", addr
);
756 break_iter_t i
= hardBreakMap
.find(addr
);
757 if (i
== hardBreakMap
.end())
760 HardBreakpoint
*hbp
= (*i
).second
;
761 if (--hbp
->refcount
== 0) {
763 hardBreakMap
.erase(i
);
773 case '0': return "software breakpoint";
774 case '1': return "hardware breakpoint";
775 case '2': return "write watchpoint";
776 case '3': return "read watchpoint";
777 case '4': return "access watchpoint";
778 default: return "unknown breakpoint/watchpoint";
782 // This function does all command processing for interfacing to a
783 // remote gdb. Note that the error codes are ignored by gdb at
784 // present, but might eventually become meaningful. (XXX) It might
785 // makes sense to use POSIX errno values, because that is what the
786 // gdb/remote.c functions want to return.
788 RemoteGDB::trap(int type
)
792 char data
[KGDB_BUFLEN
+ 1];
793 char buffer
[sizeof(gdbregs
) * 2 + 256];
794 char temp
[KGDB_BUFLEN
];
796 char command
, subcmd
;
803 DPRINTF(GDBMisc
, "trap: PC=%#x NPC=%#x\n",
804 context
->readPC(), context
->readNextPC());
809 * The first entry to this function is normally through
810 * a breakpoint trap in kgdb_connect(), in which case we
811 * must advance past the breakpoint because gdb will not.
813 * On the first entry here, we expect that gdb is not yet
814 * listening to us, so just enter the interaction loop.
815 * After the debugger is "active" (connected) it will be
816 * waiting for a "signaled" message from us.
821 // Tell remote host that an exception has occurred.
822 snprintf((char *)buffer
, sizeof(buffer
), "S%02x", signal(type
));
825 // Stick frame regs into our reg cache.
829 datalen
= recv(data
, sizeof(data
));
830 data
[sizeof(data
) - 1] = 0; // Sentinel
837 // if this command came from a running gdb, answer it --
838 // the other guy has no way of knowing if we're in or out
839 // of this loop when he issues a "remote-signal".
840 snprintf((char *)buffer
, sizeof(buffer
), "S%02x", signal(type
));
845 if (2 * sizeof(gdbregs
) > sizeof(buffer
))
846 panic("buffer too small");
848 mem2hex(buffer
, gdbregs
, sizeof(gdbregs
));
853 p
= hex2mem(gdbregs
, p
, sizeof(gdbregs
));
854 if (p
== NULL
|| *p
!= '\0')
869 if (val
< 0 && val
>= KGDB_NUMREGS
) {
874 gdbregs
[val
] = hex2i(&p
);
892 if (len
> sizeof(buffer
)) {
896 if (!acc(val
, len
)) {
901 if (read(val
, (size_t)len
, (char *)buffer
)) {
902 mem2hex(temp
, buffer
, len
);
920 if (len
> datalen
- (p
- data
)) {
924 p
= hex2mem(buffer
, p
, sizeof(buffer
));
929 if (!acc(val
, len
)) {
933 if (write(val
, (size_t)len
, (char *)buffer
))
939 case KGDB_SET_THREAD
:
955 case KGDB_ASYNC_CONT
:
960 context
->setNextPC(val
+ sizeof(MachInst
));
966 if (p
- data
< datalen
) {
969 context
->setNextPC(val
+ sizeof(MachInst
));
974 case KGDB_ASYNC_STEP
:
979 context
->setNextPC(val
+ sizeof(MachInst
));
985 if (p
- data
< datalen
) {
988 context
->setNextPC(val
+ sizeof(MachInst
));
993 case KGDB_CLR_HW_BKPT
:
995 if (*p
++ != ',') send("E0D");
997 if (*p
++ != ',') send("E0D");
1000 DPRINTF(GDBMisc
, "clear %s, addr=%#x, len=%d\n",
1001 break_type(subcmd
), val
, len
);
1006 case '0': // software breakpoint
1007 ret
= removeSoftBreak(val
, len
);
1010 case '1': // hardware breakpoint
1011 ret
= removeHardBreak(val
, len
);
1014 case '2': // write watchpoint
1015 case '3': // read watchpoint
1016 case '4': // access watchpoint
1022 send(ret
? "OK" : "E0C");
1025 case KGDB_SET_HW_BKPT
:
1027 if (*p
++ != ',') send("E0D");
1029 if (*p
++ != ',') send("E0D");
1032 DPRINTF(GDBMisc
, "set %s, addr=%#x, len=%d\n",
1033 break_type(subcmd
), val
, len
);
1038 case '0': // software breakpoint
1039 ret
= insertSoftBreak(val
, len
);
1042 case '1': // hardware breakpoint
1043 ret
= insertHardBreak(val
, len
);
1046 case '2': // write watchpoint
1047 case '3': // read watchpoint
1048 case '4': // access watchpoint
1054 send(ret
? "OK" : "E0C");
1057 case KGDB_QUERY_VAR
:
1058 var
= string(p
, datalen
- 1);
1066 case KGDB_SET_BREAK
:
1068 case KGDB_CYCLE_STEP
:
1069 case KGDB_SIG_CYCLE_STEP
:
1073 case KGDB_THREAD_ALIVE
:
1074 case KGDB_TARGET_EXIT
:
1075 case KGDB_BINARY_DLOAD
:
1076 // Unsupported command
1077 DPRINTF(GDBMisc
, "Unsupported command: %s\n",
1078 gdb_command(command
));
1079 DDUMP(GDBMisc
, (uint8_t *)data
, datalen
);
1085 DPRINTF(GDBMisc
, "Unknown command: %c(%#x)\n",
1098 // Convert a hex digit into an integer.
1099 // This returns -1 if the argument passed is no valid hex digit.
1103 if (c
>= '0' && c
<= '9')
1105 else if (c
>= 'a' && c
<= 'f')
1106 return (c
- 'a' + 10);
1107 else if (c
>= 'A' && c
<= 'F')
1109 return (c
- 'A' + 10);
1114 // Convert the low 4 bits of an integer into an hex digit.
1118 return ("0123456789abcdef"[n
& 0x0f]);
1121 // Convert a byte array into an hex string.
1123 mem2hex(void *vdst
, const void *vsrc
, int len
)
1125 char *dst
= (char *)vdst
;
1126 const char *src
= (const char *)vsrc
;
1129 *dst
++ = i2digit(*src
>> 4);
1130 *dst
++ = i2digit(*src
++);
1135 // Convert an hex string into a byte array.
1136 // This returns a pointer to the character following the last valid
1137 // hex digit. If the string ends in the middle of a byte, NULL is
1140 hex2mem(void *vdst
, const char *src
, int maxlen
)
1142 char *dst
= (char *)vdst
;
1145 while (*src
&& maxlen
--) {
1146 msb
= digit2i(*src
++);
1149 lsb
= digit2i(*src
++);
1152 *dst
++ = (msb
<< 4) | lsb
;
1157 // Convert an hex string into an integer.
1158 // This returns a pointer to the character following the last valid
1161 hex2i(const char **srcp
)
1163 const char *src
= *srcp
;
1167 while ((nibble
= digit2i(*src
)) >= 0) {