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 "arch/vtophys.hh"
135 using namespace TheISA
;
138 vector
<RemoteGDB
*> debuggers
;
139 int current_debugger
= -1;
144 if (current_debugger
>= 0 && current_debugger
< debuggers
.size()) {
145 RemoteGDB
*gdb
= debuggers
[current_debugger
];
146 if (!gdb
->isattached())
147 gdb
->listener
->accept();
148 if (gdb
->isattached())
149 gdb
->trap(ALPHA_KENTRY_IF
);
154 ///////////////////////////////////////////////////////////
159 GDBListener::Event::Event(GDBListener
*l
, int fd
, int e
)
160 : PollEvent(fd
, e
), listener(l
)
164 GDBListener::Event::process(int revent
)
169 GDBListener::GDBListener(RemoteGDB
*g
, int p
)
170 : event(NULL
), gdb(g
), port(p
)
172 assert(!gdb
->listener
);
173 gdb
->listener
= this;
176 GDBListener::~GDBListener()
185 return gdb
->name() + ".listener";
189 GDBListener::listen()
191 while (!listener
.listen(port
, true)) {
192 DPRINTF(GDBMisc
, "Can't bind port %d\n", port
);
196 event
= new Event(this, listener
.getfd(), POLLIN
);
197 pollQueue
.schedule(event
);
200 gdb
->number
= debuggers
.size();
201 debuggers
.push_back(gdb
);
205 ccprintf(cerr
, "%d: %s: listening for remote gdb #%d on port %d\n",
206 curTick
, name(), gdb
->number
, port
);
208 ccprintf(cerr
, "%d: %s: listening for remote gdb on port %d\n",
209 curTick
, name(), port
);
214 GDBListener::accept()
216 if (!listener
.islistening())
217 panic("GDBListener::accept(): cannot accept if we're not listening!");
219 int sfd
= listener
.accept(true);
222 if (gdb
->isattached())
229 ///////////////////////////////////////////////////////////
235 void mem2hex(void *, const void *, int);
236 const char *hex2mem(void *, const char *, int);
237 Addr
hex2i(const char **);
239 RemoteGDB::Event::Event(RemoteGDB
*g
, int fd
, int e
)
240 : PollEvent(fd
, e
), gdb(g
)
244 RemoteGDB::Event::process(int revent
)
247 gdb
->trap(ALPHA_KENTRY_IF
);
248 else if (revent
& POLLNVAL
)
252 RemoteGDB::RemoteGDB(System
*_system
, ExecContext
*c
)
253 : event(NULL
), listener(NULL
), number(-1), fd(-1),
254 active(false), attached(false),
255 system(_system
), pmem(_system
->physmem
), context(c
)
257 memset(gdbregs
, 0, sizeof(gdbregs
));
260 RemoteGDB::~RemoteGDB()
269 return system
->name() + ".remote_gdb";
273 RemoteGDB::isattached()
277 RemoteGDB::attach(int f
)
281 event
= new Event(this, fd
, POLLIN
);
282 pollQueue
.schedule(event
);
285 DPRINTFN("remote gdb attached\n");
295 pollQueue
.remove(event
);
296 DPRINTFN("remote gdb detached\n");
300 gdb_command(char cmd
)
303 case KGDB_SIGNAL
: return "KGDB_SIGNAL";
304 case KGDB_SET_BAUD
: return "KGDB_SET_BAUD";
305 case KGDB_SET_BREAK
: return "KGDB_SET_BREAK";
306 case KGDB_CONT
: return "KGDB_CONT";
307 case KGDB_ASYNC_CONT
: return "KGDB_ASYNC_CONT";
308 case KGDB_DEBUG
: return "KGDB_DEBUG";
309 case KGDB_DETACH
: return "KGDB_DETACH";
310 case KGDB_REG_R
: return "KGDB_REG_R";
311 case KGDB_REG_W
: return "KGDB_REG_W";
312 case KGDB_SET_THREAD
: return "KGDB_SET_THREAD";
313 case KGDB_CYCLE_STEP
: return "KGDB_CYCLE_STEP";
314 case KGDB_SIG_CYCLE_STEP
: return "KGDB_SIG_CYCLE_STEP";
315 case KGDB_KILL
: return "KGDB_KILL";
316 case KGDB_MEM_W
: return "KGDB_MEM_W";
317 case KGDB_MEM_R
: return "KGDB_MEM_R";
318 case KGDB_SET_REG
: return "KGDB_SET_REG";
319 case KGDB_READ_REG
: return "KGDB_READ_REG";
320 case KGDB_QUERY_VAR
: return "KGDB_QUERY_VAR";
321 case KGDB_SET_VAR
: return "KGDB_SET_VAR";
322 case KGDB_RESET
: return "KGDB_RESET";
323 case KGDB_STEP
: return "KGDB_STEP";
324 case KGDB_ASYNC_STEP
: return "KGDB_ASYNC_STEP";
325 case KGDB_THREAD_ALIVE
: return "KGDB_THREAD_ALIVE";
326 case KGDB_TARGET_EXIT
: return "KGDB_TARGET_EXIT";
327 case KGDB_BINARY_DLOAD
: return "KGDB_BINARY_DLOAD";
328 case KGDB_CLR_HW_BKPT
: return "KGDB_CLR_HW_BKPT";
329 case KGDB_SET_HW_BKPT
: return "KGDB_SET_HW_BKPT";
330 case KGDB_START
: return "KGDB_START";
331 case KGDB_END
: return "KGDB_END";
332 case KGDB_GOODP
: return "KGDB_GOODP";
333 case KGDB_BADP
: return "KGDB_BADP";
334 default: return "KGDB_UNKNOWN";
338 ///////////////////////////////////////////////////////////
341 // Determine if the mapping at va..(va+len) is valid.
344 RemoteGDB::acc(Addr va
, size_t len
)
348 va
= TheISA::TruncPage(va
);
349 last_va
= TheISA::RoundPage(va
+ len
);
352 if (TheISA::IsK0Seg(va
)) {
353 if (va
< (TheISA::K0SegBase
+ pmem
->size())) {
354 DPRINTF(GDBAcc
, "acc: Mapping is valid K0SEG <= "
355 "%#x < K0SEG + size\n", va
);
358 DPRINTF(GDBAcc
, "acc: Mapping invalid %#x > K0SEG + size\n",
365 * This code says that all accesses to palcode (instruction and data)
366 * are valid since there isn't a va->pa mapping because palcode is
367 * accessed physically. At some point this should probably be cleaned up
368 * but there is no easy way to do it.
371 if (AlphaISA::PcPAL(va
) || va
< 0x10000)
374 Addr ptbr
= context
->readMiscReg(AlphaISA::IPR_PALtemp20
);
375 TheISA::PageTableEntry pte
= kernel_pte_lookup(pmem
, ptbr
, va
);
377 DPRINTF(GDBAcc
, "acc: %#x pte is invalid\n", va
);
380 va
+= TheISA::PageBytes
;
381 } while (va
< last_va
);
383 DPRINTF(GDBAcc
, "acc: %#x mapping is valid\n", va
);
387 ///////////////////////////////////////////////////////////
390 // Translate a trap number into a Unix-compatible signal number.
391 // (GDB only understands Unix signal numbers.)
394 RemoteGDB::signal(int type
)
397 case ALPHA_KENTRY_INT
:
400 case ALPHA_KENTRY_UNA
:
403 case ALPHA_KENTRY_ARITH
:
406 case ALPHA_KENTRY_IF
:
409 case ALPHA_KENTRY_MM
:
413 panic("unknown signal type");
418 ///////////////////////////////////////////////////////////
419 // RemoteGDB::getregs
421 // Translate the kernel debugger register format into
422 // the GDB register format.
426 memset(gdbregs
, 0, sizeof(gdbregs
));
428 gdbregs
[KGDB_REG_PC
] = context
->readPC();
430 // @todo: Currently this is very Alpha specific.
431 if (AlphaISA::PcPAL(gdbregs
[KGDB_REG_PC
])) {
432 for (int i
= 0; i
< TheISA::NumIntArchRegs
; ++i
) {
433 gdbregs
[i
] = context
->readIntReg(AlphaISA::reg_redir
[i
]);
436 for (int i
= 0; i
< TheISA::NumIntArchRegs
; ++i
) {
437 gdbregs
[i
] = context
->readIntReg(i
);
442 for (int i
= 0; i
< TheISA::NumFloatArchRegs
; ++i
) {
443 gdbregs
[i
+ KGDB_REG_F0
] = context
->readFloatRegInt(i
);
448 ///////////////////////////////////////////////////////////
449 // RemoteGDB::setregs
451 // Translate the GDB register format into the kernel
452 // debugger register format.
457 // @todo: Currently this is very Alpha specific.
458 if (AlphaISA::PcPAL(gdbregs
[KGDB_REG_PC
])) {
459 for (int i
= 0; i
< TheISA::NumIntArchRegs
; ++i
) {
460 context
->setIntReg(AlphaISA::reg_redir
[i
], gdbregs
[i
]);
463 for (int i
= 0; i
< TheISA::NumIntArchRegs
; ++i
) {
464 context
->setIntReg(i
, gdbregs
[i
]);
469 for (int i
= 0; i
< TheISA::NumFloatArchRegs
; ++i
) {
470 context
->setFloatRegInt(i
, gdbregs
[i
+ KGDB_REG_F0
]);
473 context
->regs
.pc
= gdbregs
[KGDB_REG_PC
];
477 RemoteGDB::setTempBreakpoint(TempBreakpoint
&bkpt
, Addr addr
)
479 DPRINTF(GDBMisc
, "setTempBreakpoint: addr=%#x\n", addr
);
482 insertHardBreak(addr
, 4);
486 RemoteGDB::clearTempBreakpoint(TempBreakpoint
&bkpt
)
488 DPRINTF(GDBMisc
, "setTempBreakpoint: addr=%#x\n",
492 removeHardBreak(bkpt
.address
, 4);
497 RemoteGDB::clearSingleStep()
499 DPRINTF(GDBMisc
, "clearSingleStep bt_addr=%#x nt_addr=%#x\n",
500 takenBkpt
.address
, notTakenBkpt
.address
);
502 if (takenBkpt
.address
!= 0)
503 clearTempBreakpoint(takenBkpt
);
505 if (notTakenBkpt
.address
!= 0)
506 clearTempBreakpoint(notTakenBkpt
);
510 RemoteGDB::setSingleStep()
512 Addr pc
= context
->regs
.pc
;
516 npc
= pc
+ sizeof(MachInst
);
518 // User was stopped at pc, e.g. the instruction at pc was not
520 MachInst inst
= read
<MachInst
>(pc
);
521 StaticInstPtr
si(inst
);
522 if (si
->hasBranchTarget(pc
, context
, bpc
)) {
523 // Don't bother setting a breakpoint on the taken branch if it
524 // is the same as the next pc
529 DPRINTF(GDBMisc
, "setSingleStep bt_addr=%#x nt_addr=%#x\n",
530 takenBkpt
.address
, notTakenBkpt
.address
);
532 setTempBreakpoint(notTakenBkpt
, npc
);
535 setTempBreakpoint(takenBkpt
, bpc
);
538 /////////////////////////
551 RemoteGDB::putbyte(uint8_t b
)
556 // Send a packet to gdb
558 RemoteGDB::send(const char *bp
)
563 DPRINTF(GDBSend
, "send: %s\n", bp
);
568 for (csum
= 0; (c
= *p
); p
++) {
573 putbyte(i2digit(csum
>> 4));
574 putbyte(i2digit(csum
));
575 } while ((c
= getbyte() & 0x7f) == KGDB_BADP
);
578 // Receive a packet from gdb
580 RemoteGDB::recv(char *bp
, int maxlen
)
589 while ((c
= getbyte()) != KGDB_START
)
592 while ((c
= getbyte()) != KGDB_END
&& len
< maxlen
) {
606 csum
-= digit2i(getbyte()) * 16;
607 csum
-= digit2i(getbyte());
616 bcopy(bp
+ 3, bp
, len
);
623 DPRINTF(GDBRecv
, "recv: %s: %s\n", gdb_command(*bp
), bp
);
628 // Read bytes from kernel address space for debugger.
630 RemoteGDB::read(Addr vaddr
, size_t size
, char *data
)
632 static Addr lastaddr
= 0;
633 static size_t lastsize
= 0;
638 DPRINTF(GDBRead
, "read: reading memory location zero!\n");
639 vaddr
= lastaddr
+ lastsize
;
642 DPRINTF(GDBRead
, "read: addr=%#x, size=%d", vaddr
, size
);
651 size_t count
= min((Addr
)size
,
652 VMPageSize
- (vaddr
& (VMPageSize
- 1)));
654 maddr
= vtomem(context
, vaddr
, count
);
655 memcpy(data
, maddr
, count
);
661 while (size
>= VMPageSize
) {
662 maddr
= vtomem(context
, vaddr
, count
);
663 memcpy(data
, maddr
, VMPageSize
);
671 maddr
= vtomem(context
, vaddr
, count
);
672 memcpy(data
, maddr
, size
);
676 if (DTRACE(GDBRead
)) {
677 if (DTRACE(GDBExtra
)) {
680 DPRINTFNR(": %s\n", buf
);
689 // Write bytes to kernel address space for debugger.
691 RemoteGDB::write(Addr vaddr
, size_t size
, const char *data
)
693 static Addr lastaddr
= 0;
694 static size_t lastsize
= 0;
699 DPRINTF(GDBWrite
, "write: writing memory location zero!\n");
700 vaddr
= lastaddr
+ lastsize
;
703 if (DTRACE(GDBWrite
)) {
704 DPRINTFN("write: addr=%#x, size=%d", vaddr
, size
);
705 if (DTRACE(GDBExtra
)) {
707 mem2hex(buf
, data
, size
);
708 DPRINTFNR(": %s\n", buf
);
716 size_t count
= min((Addr
)size
,
717 VMPageSize
- (vaddr
& (VMPageSize
- 1)));
719 maddr
= vtomem(context
, vaddr
, count
);
720 memcpy(maddr
, data
, count
);
726 while (size
>= VMPageSize
) {
727 maddr
= vtomem(context
, vaddr
, count
);
728 memcpy(maddr
, data
, VMPageSize
);
736 maddr
= vtomem(context
, vaddr
, count
);
737 memcpy(maddr
, data
, size
);
748 PCEventQueue
*RemoteGDB::getPcEventQueue()
750 return &system
->pcEventQueue
;
754 RemoteGDB::HardBreakpoint::HardBreakpoint(RemoteGDB
*_gdb
, Addr pc
)
755 : PCEvent(_gdb
->getPcEventQueue(), "HardBreakpoint Event", pc
),
756 gdb(_gdb
), refcount(0)
758 DPRINTF(GDBMisc
, "creating hardware breakpoint at %#x\n", evpc
);
762 RemoteGDB::HardBreakpoint::process(ExecContext
*xc
)
764 DPRINTF(GDBMisc
, "handling hardware breakpoint at %#x\n", pc());
766 if (xc
== gdb
->context
)
767 gdb
->trap(ALPHA_KENTRY_INT
);
771 RemoteGDB::insertSoftBreak(Addr addr
, size_t len
)
773 if (len
!= sizeof(MachInst
))
774 panic("invalid length\n");
776 return insertHardBreak(addr
, len
);
780 RemoteGDB::removeSoftBreak(Addr addr
, size_t len
)
782 if (len
!= sizeof(MachInst
))
783 panic("invalid length\n");
785 return removeHardBreak(addr
, len
);
789 RemoteGDB::insertHardBreak(Addr addr
, size_t len
)
791 if (len
!= sizeof(MachInst
))
792 panic("invalid length\n");
794 DPRINTF(GDBMisc
, "inserting hardware breakpoint at %#x\n", addr
);
796 HardBreakpoint
*&bkpt
= hardBreakMap
[addr
];
798 bkpt
= new HardBreakpoint(this, addr
);
806 RemoteGDB::removeHardBreak(Addr addr
, size_t len
)
808 if (len
!= sizeof(MachInst
))
809 panic("invalid length\n");
811 DPRINTF(GDBMisc
, "removing hardware breakpoint at %#x\n", addr
);
813 break_iter_t i
= hardBreakMap
.find(addr
);
814 if (i
== hardBreakMap
.end())
817 HardBreakpoint
*hbp
= (*i
).second
;
818 if (--hbp
->refcount
== 0) {
820 hardBreakMap
.erase(i
);
830 case '0': return "software breakpoint";
831 case '1': return "hardware breakpoint";
832 case '2': return "write watchpoint";
833 case '3': return "read watchpoint";
834 case '4': return "access watchpoint";
835 default: return "unknown breakpoint/watchpoint";
839 // This function does all command processing for interfacing to a
840 // remote gdb. Note that the error codes are ignored by gdb at
841 // present, but might eventually become meaningful. (XXX) It might
842 // makes sense to use POSIX errno values, because that is what the
843 // gdb/remote.c functions want to return.
845 RemoteGDB::trap(int type
)
849 char data
[KGDB_BUFLEN
+ 1];
850 char buffer
[sizeof(gdbregs
) * 2 + 256];
851 char temp
[KGDB_BUFLEN
];
853 char command
, subcmd
;
860 DPRINTF(GDBMisc
, "trap: PC=%#x NPC=%#x\n",
861 context
->regs
.pc
, context
->regs
.npc
);
866 * The first entry to this function is normally through
867 * a breakpoint trap in kgdb_connect(), in which case we
868 * must advance past the breakpoint because gdb will not.
870 * On the first entry here, we expect that gdb is not yet
871 * listening to us, so just enter the interaction loop.
872 * After the debugger is "active" (connected) it will be
873 * waiting for a "signaled" message from us.
878 // Tell remote host that an exception has occurred.
879 snprintf((char *)buffer
, sizeof(buffer
), "S%02x", signal(type
));
882 // Stick frame regs into our reg cache.
886 datalen
= recv(data
, sizeof(data
));
887 data
[sizeof(data
) - 1] = 0; // Sentinel
894 // if this command came from a running gdb, answer it --
895 // the other guy has no way of knowing if we're in or out
896 // of this loop when he issues a "remote-signal".
897 snprintf((char *)buffer
, sizeof(buffer
), "S%02x", signal(type
));
902 if (2 * sizeof(gdbregs
) > sizeof(buffer
))
903 panic("buffer too small");
905 mem2hex(buffer
, gdbregs
, sizeof(gdbregs
));
910 p
= hex2mem(gdbregs
, p
, sizeof(gdbregs
));
911 if (p
== NULL
|| *p
!= '\0')
926 if (val
< 0 && val
>= KGDB_NUMREGS
) {
931 gdbregs
[val
] = hex2i(&p
);
949 if (len
> sizeof(buffer
)) {
953 if (!acc(val
, len
)) {
958 if (read(val
, (size_t)len
, (char *)buffer
)) {
959 mem2hex(temp
, buffer
, len
);
977 if (len
> datalen
- (p
- data
)) {
981 p
= hex2mem(buffer
, p
, sizeof(buffer
));
986 if (!acc(val
, len
)) {
990 if (write(val
, (size_t)len
, (char *)buffer
))
996 case KGDB_SET_THREAD
:
1012 case KGDB_ASYNC_CONT
:
1016 context
->regs
.pc
= val
;
1017 context
->regs
.npc
= val
+ sizeof(MachInst
);
1023 if (p
- data
< datalen
) {
1025 context
->regs
.pc
= val
;
1026 context
->regs
.npc
= val
+ sizeof(MachInst
);
1031 case KGDB_ASYNC_STEP
:
1035 context
->regs
.pc
= val
;
1036 context
->regs
.npc
= val
+ sizeof(MachInst
);
1042 if (p
- data
< datalen
) {
1044 context
->regs
.pc
= val
;
1045 context
->regs
.npc
= val
+ sizeof(MachInst
);
1050 case KGDB_CLR_HW_BKPT
:
1052 if (*p
++ != ',') send("E0D");
1054 if (*p
++ != ',') send("E0D");
1057 DPRINTF(GDBMisc
, "clear %s, addr=%#x, len=%d\n",
1058 break_type(subcmd
), val
, len
);
1063 case '0': // software breakpoint
1064 ret
= removeSoftBreak(val
, len
);
1067 case '1': // hardware breakpoint
1068 ret
= removeHardBreak(val
, len
);
1071 case '2': // write watchpoint
1072 case '3': // read watchpoint
1073 case '4': // access watchpoint
1079 send(ret
? "OK" : "E0C");
1082 case KGDB_SET_HW_BKPT
:
1084 if (*p
++ != ',') send("E0D");
1086 if (*p
++ != ',') send("E0D");
1089 DPRINTF(GDBMisc
, "set %s, addr=%#x, len=%d\n",
1090 break_type(subcmd
), val
, len
);
1095 case '0': // software breakpoint
1096 ret
= insertSoftBreak(val
, len
);
1099 case '1': // hardware breakpoint
1100 ret
= insertHardBreak(val
, len
);
1103 case '2': // write watchpoint
1104 case '3': // read watchpoint
1105 case '4': // access watchpoint
1111 send(ret
? "OK" : "E0C");
1114 case KGDB_QUERY_VAR
:
1115 var
= string(p
, datalen
- 1);
1123 case KGDB_SET_BREAK
:
1125 case KGDB_CYCLE_STEP
:
1126 case KGDB_SIG_CYCLE_STEP
:
1130 case KGDB_THREAD_ALIVE
:
1131 case KGDB_TARGET_EXIT
:
1132 case KGDB_BINARY_DLOAD
:
1133 // Unsupported command
1134 DPRINTF(GDBMisc
, "Unsupported command: %s\n",
1135 gdb_command(command
));
1136 DDUMP(GDBMisc
, (uint8_t *)data
, datalen
);
1142 DPRINTF(GDBMisc
, "Unknown command: %c(%#x)\n",
1155 // Convert a hex digit into an integer.
1156 // This returns -1 if the argument passed is no valid hex digit.
1160 if (c
>= '0' && c
<= '9')
1162 else if (c
>= 'a' && c
<= 'f')
1163 return (c
- 'a' + 10);
1164 else if (c
>= 'A' && c
<= 'F')
1166 return (c
- 'A' + 10);
1171 // Convert the low 4 bits of an integer into an hex digit.
1175 return ("0123456789abcdef"[n
& 0x0f]);
1178 // Convert a byte array into an hex string.
1180 mem2hex(void *vdst
, const void *vsrc
, int len
)
1182 char *dst
= (char *)vdst
;
1183 const char *src
= (const char *)vsrc
;
1186 *dst
++ = i2digit(*src
>> 4);
1187 *dst
++ = i2digit(*src
++);
1192 // Convert an hex string into a byte array.
1193 // This returns a pointer to the character following the last valid
1194 // hex digit. If the string ends in the middle of a byte, NULL is
1197 hex2mem(void *vdst
, const char *src
, int maxlen
)
1199 char *dst
= (char *)vdst
;
1202 while (*src
&& maxlen
--) {
1203 msb
= digit2i(*src
++);
1206 lsb
= digit2i(*src
++);
1209 *dst
++ = (msb
<< 4) | lsb
;
1214 // Convert an hex string into an integer.
1215 // This returns a pointer to the character following the last valid
1218 hex2i(const char **srcp
)
1220 const char *src
= *srcp
;
1224 while ((nibble
= digit2i(*src
)) >= 0) {