remote_gdb.cc (3960:1dca397b2bab) remote_gdb.cc (3980:9bcb2a2e9bb8)
1/*
2 * Copyright (c) 2002-2005 The Regents of The University of Michigan
3 * All rights reserved.
4 *
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.
15 *
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.
27 *
28 * Authors: Nathan Binkert
29 */
30
31/*
32 * Copyright (c) 1990, 1993
33 * The Regents of the University of California. All rights reserved.
34 *
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.
38 *
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.
43 *
44 * Redistribution and use in source and binary forms, with or without
45 * modification, are permitted provided that the following conditions
46 * are met:
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.
59 *
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
70 * SUCH DAMAGE.
71 *
72 * @(#)kgdb_stub.c 8.4 (Berkeley) 1/12/94
73 */
74
75/*-
76 * Copyright (c) 2001 The NetBSD Foundation, Inc.
77 * All rights reserved.
78 *
79 * This code is derived from software contributed to The NetBSD Foundation
80 * by Jason R. Thorpe.
81 *
82 * Redistribution and use in source and binary forms, with or without
83 * modification, are permitted provided that the following conditions
84 * are met:
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.
97 *
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.
109 */
110
111/*
112 * $NetBSD: kgdb_stub.c,v 1.8 2001/07/07 22:58:00 wdk Exp $
113 *
114 * Taken from NetBSD
115 *
116 * "Stub" to allow remote cpu to debug over a serial line using gdb.
117 */
118
119#include <sys/signal.h>
120
121#include <string>
122#include <unistd.h>
123
124#include "config/full_system.hh"
125
126#if FULL_SYSTEM
127#include "arch/vtophys.hh"
128#endif
129
130#include "base/intmath.hh"
131#include "base/remote_gdb.hh"
132#include "base/socket.hh"
133#include "base/trace.hh"
134#include "cpu/thread_context.hh"
135#include "cpu/static_inst.hh"
136//#include "mem/physical.hh"
137#include "mem/port.hh"
138#include "mem/translating_port.hh"
139#include "sim/system.hh"
140
141using namespace std;
142using namespace TheISA;
143
144#ifndef NDEBUG
145vector<BaseRemoteGDB *> debuggers;
146
147void
148debugger()
149{
150 static int current_debugger = -1;
151 if (current_debugger >= 0 && current_debugger < debuggers.size()) {
152 BaseRemoteGDB *gdb = debuggers[current_debugger];
153 if (!gdb->isattached())
154 gdb->listener->accept();
155 if (gdb->isattached())
156 gdb->trap(SIGILL);
157 }
158}
159#endif
160
161///////////////////////////////////////////////////////////
162//
163//
164//
165
166GDBListener::Event::Event(GDBListener *l, int fd, int e)
167 : PollEvent(fd, e), listener(l)
168{}
169
170void
171GDBListener::Event::process(int revent)
172{
173 listener->accept();
174}
175
176GDBListener::GDBListener(BaseRemoteGDB *g, int p)
177 : event(NULL), gdb(g), port(p)
178{
179 assert(!gdb->listener);
180 gdb->listener = this;
181}
182
183GDBListener::~GDBListener()
184{
185 if (event)
186 delete event;
187}
188
189string
190GDBListener::name()
191{
192 return gdb->name() + ".listener";
193}
194
195void
196GDBListener::listen()
197{
198 while (!listener.listen(port, true)) {
199 DPRINTF(GDBMisc, "Can't bind port %d\n", port);
200 port++;
201 }
202
203 event = new Event(this, listener.getfd(), POLLIN);
204 pollQueue.schedule(event);
205
206#ifndef NDEBUG
207 gdb->number = debuggers.size();
208 debuggers.push_back(gdb);
209#endif
210
211#ifndef NDEBUG
212 ccprintf(cerr, "%d: %s: listening for remote gdb #%d on port %d\n",
213 curTick, name(), gdb->number, port);
214#else
215 ccprintf(cerr, "%d: %s: listening for remote gdb on port %d\n",
216 curTick, name(), port);
217#endif
218}
219
220void
221GDBListener::accept()
222{
223 if (!listener.islistening())
224 panic("GDBListener::accept(): cannot accept if we're not listening!");
225
226 int sfd = listener.accept(true);
227
228 if (sfd != -1) {
229 if (gdb->isattached())
230 close(sfd);
231 else
232 gdb->attach(sfd);
233 }
234}
235
236BaseRemoteGDB::Event::Event(BaseRemoteGDB *g, int fd, int e)
237 : PollEvent(fd, e), gdb(g)
238{}
239
240void
241BaseRemoteGDB::Event::process(int revent)
242{
243 if (revent & POLLIN)
244 gdb->trap(SIGILL);
245 else if (revent & POLLNVAL)
246 gdb->detach();
247}
248
249BaseRemoteGDB::BaseRemoteGDB(System *_system, ThreadContext *c, size_t cacheSize)
250 : event(NULL), listener(NULL), number(-1), fd(-1),
251 active(false), attached(false),
252 system(_system), pmem(_system->physmem), context(c),
253 gdbregs(cacheSize)
254{
255 memset(gdbregs.regs, 0, gdbregs.bytes());
256}
257
258BaseRemoteGDB::~BaseRemoteGDB()
259{
260 if (event)
261 delete event;
262}
263
264string
265BaseRemoteGDB::name()
266{
267 return system->name() + ".remote_gdb";
268}
269
270bool
271BaseRemoteGDB::isattached()
272{ return attached; }
273
274void
275BaseRemoteGDB::attach(int f)
276{
277 fd = f;
278
279 event = new Event(this, fd, POLLIN);
280 pollQueue.schedule(event);
281
282 attached = true;
283 DPRINTFN("remote gdb attached\n");
284}
285
286void
287BaseRemoteGDB::detach()
288{
289 attached = false;
290 close(fd);
291 fd = -1;
292
293 pollQueue.remove(event);
294 DPRINTFN("remote gdb detached\n");
295}
296
297const char *
298BaseRemoteGDB::gdb_command(char cmd)
299{
300 switch (cmd) {
301 case GDBSignal: return "KGDB_SIGNAL";
302 case GDBSetBaud: return "KGDB_SET_BAUD";
303 case GDBSetBreak: return "KGDB_SET_BREAK";
304 case GDBCont: return "KGDB_CONT";
305 case GDBAsyncCont: return "KGDB_ASYNC_CONT";
306 case GDBDebug: return "KGDB_DEBUG";
307 case GDBDetach: return "KGDB_DETACH";
308 case GDBRegR: return "KGDB_REG_R";
309 case GDBRegW: return "KGDB_REG_W";
310 case GDBSetThread: return "KGDB_SET_THREAD";
311 case GDBCycleStep: return "KGDB_CYCLE_STEP";
312 case GDBSigCycleStep: return "KGDB_SIG_CYCLE_STEP";
313 case GDBKill: return "KGDB_KILL";
314 case GDBMemW: return "KGDB_MEM_W";
315 case GDBMemR: return "KGDB_MEM_R";
316 case GDBSetReg: return "KGDB_SET_REG";
317 case GDBReadReg: return "KGDB_READ_REG";
318 case GDBQueryVar: return "KGDB_QUERY_VAR";
319 case GDBSetVar: return "KGDB_SET_VAR";
320 case GDBReset: return "KGDB_RESET";
321 case GDBStep: return "KGDB_STEP";
322 case GDBAsyncStep: return "KGDB_ASYNC_STEP";
323 case GDBThreadAlive: return "KGDB_THREAD_ALIVE";
324 case GDBTargetExit: return "KGDB_TARGET_EXIT";
325 case GDBBinaryDload: return "KGDB_BINARY_DLOAD";
326 case GDBClrHwBkpt: return "KGDB_CLR_HW_BKPT";
327 case GDBSetHwBkpt: return "KGDB_SET_HW_BKPT";
328 case GDBStart: return "KGDB_START";
329 case GDBEnd: return "KGDB_END";
330 case GDBGoodP: return "KGDB_GOODP";
331 case GDBBadP: return "KGDB_BADP";
332 default: return "KGDB_UNKNOWN";
333 }
334}
335
336/////////////////////////
337//
338//
339
340uint8_t
341BaseRemoteGDB::getbyte()
342{
343 uint8_t b;
344 ::read(fd, &b, 1);
345 return b;
346}
347
348void
349BaseRemoteGDB::putbyte(uint8_t b)
350{
351 ::write(fd, &b, 1);
352}
353
354// Send a packet to gdb
355void
356BaseRemoteGDB::send(const char *bp)
357{
358 const char *p;
359 uint8_t csum, c;
360
361 DPRINTF(GDBSend, "send: %s\n", bp);
362
363 do {
364 p = bp;
365 //Start sending a packet
366 putbyte(GDBStart);
367 //Send the contents, and also keep a check sum.
368 for (csum = 0; (c = *p); p++) {
369 putbyte(c);
370 csum += c;
371 }
372 //Send the ending character.
373 putbyte(GDBEnd);
374 //Sent the checksum.
375 putbyte(i2digit(csum >> 4));
376 putbyte(i2digit(csum));
377 //Try transmitting over and over again until the other end doesn't send an
378 //error back.
379 } while ((c = getbyte() & 0x7f) == GDBBadP);
380}
381
382// Receive a packet from gdb
383int
384BaseRemoteGDB::recv(char *bp, int maxlen)
385{
386 char *p;
387 int c, csum;
388 int len;
389
390 do {
391 p = bp;
392 csum = len = 0;
393 //Find the beginning of a packet
394 while ((c = getbyte()) != GDBStart)
395 ;
396
397 //Read until you find the end of the data in the packet, and keep
398 //track of the check sum.
399 while ((c = getbyte()) != GDBEnd && len < maxlen) {
400 c &= 0x7f;
401 csum += c;
402 *p++ = c;
403 len++;
404 }
405
406 //Mask the check sum, and terminate the command string.
407 csum &= 0xff;
408 *p = '\0';
409
410 //If the command was too long, report an error.
411 if (len >= maxlen) {
412 putbyte(GDBBadP);
413 continue;
414 }
415
416 //Bring in the checksum. If the check sum matches, csum will be 0.
417 csum -= digit2i(getbyte()) * 16;
418 csum -= digit2i(getbyte());
419
420 //If the check sum was correct
421 if (csum == 0) {
422 //Report that the packet was received correctly
423 putbyte(GDBGoodP);
424 // Sequence present?
425 if (bp[2] == ':') {
426 putbyte(bp[0]);
427 putbyte(bp[1]);
428 len -= 3;
429 memcpy(bp, bp+3, len);
430 }
431 break;
432 }
433 //Otherwise, report that there was a mistake.
434 putbyte(GDBBadP);
435 } while (1);
436
437 DPRINTF(GDBRecv, "recv: %s: %s\n", gdb_command(*bp), bp);
438
439 return (len);
440}
441
442// Read bytes from kernel address space for debugger.
443bool
444BaseRemoteGDB::read(Addr vaddr, size_t size, char *data)
445{
446 static Addr lastaddr = 0;
447 static size_t lastsize = 0;
448
449 if (vaddr < 10) {
450 DPRINTF(GDBRead, "read: reading memory location zero!\n");
451 vaddr = lastaddr + lastsize;
452 }
453
454 DPRINTF(GDBRead, "read: addr=%#x, size=%d", vaddr, size);
455
456#if FULL_SYSTEM
457 VirtualPort *port = context->getVirtPort(context);
458#else
459 TranslatingPort *port = context->getMemPort();
460#endif
461 port->readBlob(vaddr, (uint8_t*)data, size);
462#if FULL_SYSTEM
463 context->delVirtPort(port);
464#else
465 delete port;
466#endif
467
468#if TRACING_ON
469 if (DTRACE(GDBRead)) {
470 if (DTRACE(GDBExtra)) {
471 char buf[1024];
472 mem2hex(buf, data, size);
473 DPRINTFNR(": %s\n", buf);
474 } else
475 DPRINTFNR("\n");
476 }
477#endif
478
479 return true;
480}
481
482// Write bytes to kernel address space for debugger.
483bool
484BaseRemoteGDB::write(Addr vaddr, size_t size, const char *data)
485{
486 static Addr lastaddr = 0;
487 static size_t lastsize = 0;
488
489 if (vaddr < 10) {
490 DPRINTF(GDBWrite, "write: writing memory location zero!\n");
491 vaddr = lastaddr + lastsize;
492 }
493
494 if (DTRACE(GDBWrite)) {
495 DPRINTFN("write: addr=%#x, size=%d", vaddr, size);
496 if (DTRACE(GDBExtra)) {
497 char buf[1024];
498 mem2hex(buf, data, size);
499 DPRINTFNR(": %s\n", buf);
500 } else
501 DPRINTFNR("\n");
502 }
503#if FULL_SYSTEM
504 VirtualPort *port = context->getVirtPort(context);
505#else
506 TranslatingPort *port = context->getMemPort();
507#endif
508 port->writeBlob(vaddr, (uint8_t*)data, size);
509#if FULL_SYSTEM
510 context->delVirtPort(port);
511#else
512 delete port;
513#endif
514
515 return true;
516}
517
518PCEventQueue *BaseRemoteGDB::getPcEventQueue()
519{
520 return &system->pcEventQueue;
521}
522
523BaseRemoteGDB::HardBreakpoint::HardBreakpoint(BaseRemoteGDB *_gdb, Addr pc)
524 : PCEvent(_gdb->getPcEventQueue(), "HardBreakpoint Event", pc),
525 gdb(_gdb), refcount(0)
526{
527 DPRINTF(GDBMisc, "creating hardware breakpoint at %#x\n", evpc);
528}
529
530void
531BaseRemoteGDB::HardBreakpoint::process(ThreadContext *tc)
532{
533 DPRINTF(GDBMisc, "handling hardware breakpoint at %#x\n", pc());
534
535 if (tc == gdb->context)
536 gdb->trap(SIGTRAP);
537}
538
539bool
540BaseRemoteGDB::insertSoftBreak(Addr addr, size_t len)
541{
542 if (len != sizeof(TheISA::MachInst))
543 panic("invalid length\n");
544
545 return insertHardBreak(addr, len);
546}
547
548bool
549BaseRemoteGDB::removeSoftBreak(Addr addr, size_t len)
550{
551 if (len != sizeof(MachInst))
552 panic("invalid length\n");
553
554 return removeHardBreak(addr, len);
555}
556
557bool
558BaseRemoteGDB::insertHardBreak(Addr addr, size_t len)
559{
560 if (len != sizeof(MachInst))
561 panic("invalid length\n");
562
563 DPRINTF(GDBMisc, "inserting hardware breakpoint at %#x\n", addr);
564
565 HardBreakpoint *&bkpt = hardBreakMap[addr];
566 if (bkpt == 0)
567 bkpt = new HardBreakpoint(this, addr);
568
569 bkpt->refcount++;
570
571 return true;
572}
573
574bool
575BaseRemoteGDB::removeHardBreak(Addr addr, size_t len)
576{
577 if (len != sizeof(MachInst))
578 panic("invalid length\n");
579
580 DPRINTF(GDBMisc, "removing hardware breakpoint at %#x\n", addr);
581
582 break_iter_t i = hardBreakMap.find(addr);
583 if (i == hardBreakMap.end())
584 return false;
585
586 HardBreakpoint *hbp = (*i).second;
587 if (--hbp->refcount == 0) {
588 delete hbp;
589 hardBreakMap.erase(i);
590 }
591
592 return true;
593}
594
595void
596BaseRemoteGDB::setTempBreakpoint(Addr bkpt)
597{
598 DPRINTF(GDBMisc, "setTempBreakpoint: addr=%#x\n", bkpt);
599 insertHardBreak(bkpt, sizeof(TheISA::MachInst));
600}
601
602void
603BaseRemoteGDB::clearTempBreakpoint(Addr &bkpt)
604{
605 DPRINTF(GDBMisc, "setTempBreakpoint: addr=%#x\n", bkpt);
606 removeHardBreak(bkpt, sizeof(TheISA::MachInst));
607 bkpt = 0;
608}
609
610const char *
611BaseRemoteGDB::break_type(char c)
612{
613 switch(c) {
614 case '0': return "software breakpoint";
615 case '1': return "hardware breakpoint";
616 case '2': return "write watchpoint";
617 case '3': return "read watchpoint";
618 case '4': return "access watchpoint";
619 default: return "unknown breakpoint/watchpoint";
620 }
621}
622
623// This function does all command processing for interfacing to a
624// remote gdb. Note that the error codes are ignored by gdb at
625// present, but might eventually become meaningful. (XXX) It might
626// makes sense to use POSIX errno values, because that is what the
627// gdb/remote.c functions want to return.
628bool
629BaseRemoteGDB::trap(int type)
630{
631 uint64_t val;
632 size_t datalen, len;
633 char data[GDBPacketBufLen + 1];
1/*
2 * Copyright (c) 2002-2005 The Regents of The University of Michigan
3 * All rights reserved.
4 *
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.
15 *
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.
27 *
28 * Authors: Nathan Binkert
29 */
30
31/*
32 * Copyright (c) 1990, 1993
33 * The Regents of the University of California. All rights reserved.
34 *
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.
38 *
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.
43 *
44 * Redistribution and use in source and binary forms, with or without
45 * modification, are permitted provided that the following conditions
46 * are met:
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.
59 *
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
70 * SUCH DAMAGE.
71 *
72 * @(#)kgdb_stub.c 8.4 (Berkeley) 1/12/94
73 */
74
75/*-
76 * Copyright (c) 2001 The NetBSD Foundation, Inc.
77 * All rights reserved.
78 *
79 * This code is derived from software contributed to The NetBSD Foundation
80 * by Jason R. Thorpe.
81 *
82 * Redistribution and use in source and binary forms, with or without
83 * modification, are permitted provided that the following conditions
84 * are met:
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.
97 *
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.
109 */
110
111/*
112 * $NetBSD: kgdb_stub.c,v 1.8 2001/07/07 22:58:00 wdk Exp $
113 *
114 * Taken from NetBSD
115 *
116 * "Stub" to allow remote cpu to debug over a serial line using gdb.
117 */
118
119#include <sys/signal.h>
120
121#include <string>
122#include <unistd.h>
123
124#include "config/full_system.hh"
125
126#if FULL_SYSTEM
127#include "arch/vtophys.hh"
128#endif
129
130#include "base/intmath.hh"
131#include "base/remote_gdb.hh"
132#include "base/socket.hh"
133#include "base/trace.hh"
134#include "cpu/thread_context.hh"
135#include "cpu/static_inst.hh"
136//#include "mem/physical.hh"
137#include "mem/port.hh"
138#include "mem/translating_port.hh"
139#include "sim/system.hh"
140
141using namespace std;
142using namespace TheISA;
143
144#ifndef NDEBUG
145vector<BaseRemoteGDB *> debuggers;
146
147void
148debugger()
149{
150 static int current_debugger = -1;
151 if (current_debugger >= 0 && current_debugger < debuggers.size()) {
152 BaseRemoteGDB *gdb = debuggers[current_debugger];
153 if (!gdb->isattached())
154 gdb->listener->accept();
155 if (gdb->isattached())
156 gdb->trap(SIGILL);
157 }
158}
159#endif
160
161///////////////////////////////////////////////////////////
162//
163//
164//
165
166GDBListener::Event::Event(GDBListener *l, int fd, int e)
167 : PollEvent(fd, e), listener(l)
168{}
169
170void
171GDBListener::Event::process(int revent)
172{
173 listener->accept();
174}
175
176GDBListener::GDBListener(BaseRemoteGDB *g, int p)
177 : event(NULL), gdb(g), port(p)
178{
179 assert(!gdb->listener);
180 gdb->listener = this;
181}
182
183GDBListener::~GDBListener()
184{
185 if (event)
186 delete event;
187}
188
189string
190GDBListener::name()
191{
192 return gdb->name() + ".listener";
193}
194
195void
196GDBListener::listen()
197{
198 while (!listener.listen(port, true)) {
199 DPRINTF(GDBMisc, "Can't bind port %d\n", port);
200 port++;
201 }
202
203 event = new Event(this, listener.getfd(), POLLIN);
204 pollQueue.schedule(event);
205
206#ifndef NDEBUG
207 gdb->number = debuggers.size();
208 debuggers.push_back(gdb);
209#endif
210
211#ifndef NDEBUG
212 ccprintf(cerr, "%d: %s: listening for remote gdb #%d on port %d\n",
213 curTick, name(), gdb->number, port);
214#else
215 ccprintf(cerr, "%d: %s: listening for remote gdb on port %d\n",
216 curTick, name(), port);
217#endif
218}
219
220void
221GDBListener::accept()
222{
223 if (!listener.islistening())
224 panic("GDBListener::accept(): cannot accept if we're not listening!");
225
226 int sfd = listener.accept(true);
227
228 if (sfd != -1) {
229 if (gdb->isattached())
230 close(sfd);
231 else
232 gdb->attach(sfd);
233 }
234}
235
236BaseRemoteGDB::Event::Event(BaseRemoteGDB *g, int fd, int e)
237 : PollEvent(fd, e), gdb(g)
238{}
239
240void
241BaseRemoteGDB::Event::process(int revent)
242{
243 if (revent & POLLIN)
244 gdb->trap(SIGILL);
245 else if (revent & POLLNVAL)
246 gdb->detach();
247}
248
249BaseRemoteGDB::BaseRemoteGDB(System *_system, ThreadContext *c, size_t cacheSize)
250 : event(NULL), listener(NULL), number(-1), fd(-1),
251 active(false), attached(false),
252 system(_system), pmem(_system->physmem), context(c),
253 gdbregs(cacheSize)
254{
255 memset(gdbregs.regs, 0, gdbregs.bytes());
256}
257
258BaseRemoteGDB::~BaseRemoteGDB()
259{
260 if (event)
261 delete event;
262}
263
264string
265BaseRemoteGDB::name()
266{
267 return system->name() + ".remote_gdb";
268}
269
270bool
271BaseRemoteGDB::isattached()
272{ return attached; }
273
274void
275BaseRemoteGDB::attach(int f)
276{
277 fd = f;
278
279 event = new Event(this, fd, POLLIN);
280 pollQueue.schedule(event);
281
282 attached = true;
283 DPRINTFN("remote gdb attached\n");
284}
285
286void
287BaseRemoteGDB::detach()
288{
289 attached = false;
290 close(fd);
291 fd = -1;
292
293 pollQueue.remove(event);
294 DPRINTFN("remote gdb detached\n");
295}
296
297const char *
298BaseRemoteGDB::gdb_command(char cmd)
299{
300 switch (cmd) {
301 case GDBSignal: return "KGDB_SIGNAL";
302 case GDBSetBaud: return "KGDB_SET_BAUD";
303 case GDBSetBreak: return "KGDB_SET_BREAK";
304 case GDBCont: return "KGDB_CONT";
305 case GDBAsyncCont: return "KGDB_ASYNC_CONT";
306 case GDBDebug: return "KGDB_DEBUG";
307 case GDBDetach: return "KGDB_DETACH";
308 case GDBRegR: return "KGDB_REG_R";
309 case GDBRegW: return "KGDB_REG_W";
310 case GDBSetThread: return "KGDB_SET_THREAD";
311 case GDBCycleStep: return "KGDB_CYCLE_STEP";
312 case GDBSigCycleStep: return "KGDB_SIG_CYCLE_STEP";
313 case GDBKill: return "KGDB_KILL";
314 case GDBMemW: return "KGDB_MEM_W";
315 case GDBMemR: return "KGDB_MEM_R";
316 case GDBSetReg: return "KGDB_SET_REG";
317 case GDBReadReg: return "KGDB_READ_REG";
318 case GDBQueryVar: return "KGDB_QUERY_VAR";
319 case GDBSetVar: return "KGDB_SET_VAR";
320 case GDBReset: return "KGDB_RESET";
321 case GDBStep: return "KGDB_STEP";
322 case GDBAsyncStep: return "KGDB_ASYNC_STEP";
323 case GDBThreadAlive: return "KGDB_THREAD_ALIVE";
324 case GDBTargetExit: return "KGDB_TARGET_EXIT";
325 case GDBBinaryDload: return "KGDB_BINARY_DLOAD";
326 case GDBClrHwBkpt: return "KGDB_CLR_HW_BKPT";
327 case GDBSetHwBkpt: return "KGDB_SET_HW_BKPT";
328 case GDBStart: return "KGDB_START";
329 case GDBEnd: return "KGDB_END";
330 case GDBGoodP: return "KGDB_GOODP";
331 case GDBBadP: return "KGDB_BADP";
332 default: return "KGDB_UNKNOWN";
333 }
334}
335
336/////////////////////////
337//
338//
339
340uint8_t
341BaseRemoteGDB::getbyte()
342{
343 uint8_t b;
344 ::read(fd, &b, 1);
345 return b;
346}
347
348void
349BaseRemoteGDB::putbyte(uint8_t b)
350{
351 ::write(fd, &b, 1);
352}
353
354// Send a packet to gdb
355void
356BaseRemoteGDB::send(const char *bp)
357{
358 const char *p;
359 uint8_t csum, c;
360
361 DPRINTF(GDBSend, "send: %s\n", bp);
362
363 do {
364 p = bp;
365 //Start sending a packet
366 putbyte(GDBStart);
367 //Send the contents, and also keep a check sum.
368 for (csum = 0; (c = *p); p++) {
369 putbyte(c);
370 csum += c;
371 }
372 //Send the ending character.
373 putbyte(GDBEnd);
374 //Sent the checksum.
375 putbyte(i2digit(csum >> 4));
376 putbyte(i2digit(csum));
377 //Try transmitting over and over again until the other end doesn't send an
378 //error back.
379 } while ((c = getbyte() & 0x7f) == GDBBadP);
380}
381
382// Receive a packet from gdb
383int
384BaseRemoteGDB::recv(char *bp, int maxlen)
385{
386 char *p;
387 int c, csum;
388 int len;
389
390 do {
391 p = bp;
392 csum = len = 0;
393 //Find the beginning of a packet
394 while ((c = getbyte()) != GDBStart)
395 ;
396
397 //Read until you find the end of the data in the packet, and keep
398 //track of the check sum.
399 while ((c = getbyte()) != GDBEnd && len < maxlen) {
400 c &= 0x7f;
401 csum += c;
402 *p++ = c;
403 len++;
404 }
405
406 //Mask the check sum, and terminate the command string.
407 csum &= 0xff;
408 *p = '\0';
409
410 //If the command was too long, report an error.
411 if (len >= maxlen) {
412 putbyte(GDBBadP);
413 continue;
414 }
415
416 //Bring in the checksum. If the check sum matches, csum will be 0.
417 csum -= digit2i(getbyte()) * 16;
418 csum -= digit2i(getbyte());
419
420 //If the check sum was correct
421 if (csum == 0) {
422 //Report that the packet was received correctly
423 putbyte(GDBGoodP);
424 // Sequence present?
425 if (bp[2] == ':') {
426 putbyte(bp[0]);
427 putbyte(bp[1]);
428 len -= 3;
429 memcpy(bp, bp+3, len);
430 }
431 break;
432 }
433 //Otherwise, report that there was a mistake.
434 putbyte(GDBBadP);
435 } while (1);
436
437 DPRINTF(GDBRecv, "recv: %s: %s\n", gdb_command(*bp), bp);
438
439 return (len);
440}
441
442// Read bytes from kernel address space for debugger.
443bool
444BaseRemoteGDB::read(Addr vaddr, size_t size, char *data)
445{
446 static Addr lastaddr = 0;
447 static size_t lastsize = 0;
448
449 if (vaddr < 10) {
450 DPRINTF(GDBRead, "read: reading memory location zero!\n");
451 vaddr = lastaddr + lastsize;
452 }
453
454 DPRINTF(GDBRead, "read: addr=%#x, size=%d", vaddr, size);
455
456#if FULL_SYSTEM
457 VirtualPort *port = context->getVirtPort(context);
458#else
459 TranslatingPort *port = context->getMemPort();
460#endif
461 port->readBlob(vaddr, (uint8_t*)data, size);
462#if FULL_SYSTEM
463 context->delVirtPort(port);
464#else
465 delete port;
466#endif
467
468#if TRACING_ON
469 if (DTRACE(GDBRead)) {
470 if (DTRACE(GDBExtra)) {
471 char buf[1024];
472 mem2hex(buf, data, size);
473 DPRINTFNR(": %s\n", buf);
474 } else
475 DPRINTFNR("\n");
476 }
477#endif
478
479 return true;
480}
481
482// Write bytes to kernel address space for debugger.
483bool
484BaseRemoteGDB::write(Addr vaddr, size_t size, const char *data)
485{
486 static Addr lastaddr = 0;
487 static size_t lastsize = 0;
488
489 if (vaddr < 10) {
490 DPRINTF(GDBWrite, "write: writing memory location zero!\n");
491 vaddr = lastaddr + lastsize;
492 }
493
494 if (DTRACE(GDBWrite)) {
495 DPRINTFN("write: addr=%#x, size=%d", vaddr, size);
496 if (DTRACE(GDBExtra)) {
497 char buf[1024];
498 mem2hex(buf, data, size);
499 DPRINTFNR(": %s\n", buf);
500 } else
501 DPRINTFNR("\n");
502 }
503#if FULL_SYSTEM
504 VirtualPort *port = context->getVirtPort(context);
505#else
506 TranslatingPort *port = context->getMemPort();
507#endif
508 port->writeBlob(vaddr, (uint8_t*)data, size);
509#if FULL_SYSTEM
510 context->delVirtPort(port);
511#else
512 delete port;
513#endif
514
515 return true;
516}
517
518PCEventQueue *BaseRemoteGDB::getPcEventQueue()
519{
520 return &system->pcEventQueue;
521}
522
523BaseRemoteGDB::HardBreakpoint::HardBreakpoint(BaseRemoteGDB *_gdb, Addr pc)
524 : PCEvent(_gdb->getPcEventQueue(), "HardBreakpoint Event", pc),
525 gdb(_gdb), refcount(0)
526{
527 DPRINTF(GDBMisc, "creating hardware breakpoint at %#x\n", evpc);
528}
529
530void
531BaseRemoteGDB::HardBreakpoint::process(ThreadContext *tc)
532{
533 DPRINTF(GDBMisc, "handling hardware breakpoint at %#x\n", pc());
534
535 if (tc == gdb->context)
536 gdb->trap(SIGTRAP);
537}
538
539bool
540BaseRemoteGDB::insertSoftBreak(Addr addr, size_t len)
541{
542 if (len != sizeof(TheISA::MachInst))
543 panic("invalid length\n");
544
545 return insertHardBreak(addr, len);
546}
547
548bool
549BaseRemoteGDB::removeSoftBreak(Addr addr, size_t len)
550{
551 if (len != sizeof(MachInst))
552 panic("invalid length\n");
553
554 return removeHardBreak(addr, len);
555}
556
557bool
558BaseRemoteGDB::insertHardBreak(Addr addr, size_t len)
559{
560 if (len != sizeof(MachInst))
561 panic("invalid length\n");
562
563 DPRINTF(GDBMisc, "inserting hardware breakpoint at %#x\n", addr);
564
565 HardBreakpoint *&bkpt = hardBreakMap[addr];
566 if (bkpt == 0)
567 bkpt = new HardBreakpoint(this, addr);
568
569 bkpt->refcount++;
570
571 return true;
572}
573
574bool
575BaseRemoteGDB::removeHardBreak(Addr addr, size_t len)
576{
577 if (len != sizeof(MachInst))
578 panic("invalid length\n");
579
580 DPRINTF(GDBMisc, "removing hardware breakpoint at %#x\n", addr);
581
582 break_iter_t i = hardBreakMap.find(addr);
583 if (i == hardBreakMap.end())
584 return false;
585
586 HardBreakpoint *hbp = (*i).second;
587 if (--hbp->refcount == 0) {
588 delete hbp;
589 hardBreakMap.erase(i);
590 }
591
592 return true;
593}
594
595void
596BaseRemoteGDB::setTempBreakpoint(Addr bkpt)
597{
598 DPRINTF(GDBMisc, "setTempBreakpoint: addr=%#x\n", bkpt);
599 insertHardBreak(bkpt, sizeof(TheISA::MachInst));
600}
601
602void
603BaseRemoteGDB::clearTempBreakpoint(Addr &bkpt)
604{
605 DPRINTF(GDBMisc, "setTempBreakpoint: addr=%#x\n", bkpt);
606 removeHardBreak(bkpt, sizeof(TheISA::MachInst));
607 bkpt = 0;
608}
609
610const char *
611BaseRemoteGDB::break_type(char c)
612{
613 switch(c) {
614 case '0': return "software breakpoint";
615 case '1': return "hardware breakpoint";
616 case '2': return "write watchpoint";
617 case '3': return "read watchpoint";
618 case '4': return "access watchpoint";
619 default: return "unknown breakpoint/watchpoint";
620 }
621}
622
623// This function does all command processing for interfacing to a
624// remote gdb. Note that the error codes are ignored by gdb at
625// present, but might eventually become meaningful. (XXX) It might
626// makes sense to use POSIX errno values, because that is what the
627// gdb/remote.c functions want to return.
628bool
629BaseRemoteGDB::trap(int type)
630{
631 uint64_t val;
632 size_t datalen, len;
633 char data[GDBPacketBufLen + 1];
634 char buffer[gdbregs.bytes() * 2 + 256];
634 char *buffer;
635 const char *p;
636 char command, subcmd;
637 string var;
638 bool ret;
639
640 if (!attached)
641 return false;
642
635 const char *p;
636 char command, subcmd;
637 string var;
638 bool ret;
639
640 if (!attached)
641 return false;
642
643 buffer = (char*)malloc(gdbregs.bytes() * 2 + 256);
644
643 DPRINTF(GDBMisc, "trap: PC=%#x NPC=%#x\n",
644 context->readPC(), context->readNextPC());
645
646 clearSingleStep();
647
648 /*
649 * The first entry to this function is normally through
650 * a breakpoint trap in kgdb_connect(), in which case we
651 * must advance past the breakpoint because gdb will not.
652 *
653 * On the first entry here, we expect that gdb is not yet
654 * listening to us, so just enter the interaction loop.
655 * After the debugger is "active" (connected) it will be
656 * waiting for a "signaled" message from us.
657 */
658 if (!active)
659 active = true;
660 else
661 // Tell remote host that an exception has occurred.
662 snprintf((char *)buffer, sizeof(buffer), "S%02x", type);
663 send(buffer);
664
665 // Stick frame regs into our reg cache.
666 getregs();
667
668 for (;;) {
669 datalen = recv(data, sizeof(data));
670 data[sizeof(data) - 1] = 0; // Sentinel
671 command = data[0];
672 subcmd = 0;
673 p = data + 1;
674 switch (command) {
675
676 case GDBSignal:
677 // if this command came from a running gdb, answer it --
678 // the other guy has no way of knowing if we're in or out
679 // of this loop when he issues a "remote-signal".
680 snprintf((char *)buffer, sizeof(buffer),
681 "S%02x", type);
682 send(buffer);
683 continue;
684
685 case GDBRegR:
686 if (2 * gdbregs.bytes() > sizeof(buffer))
687 panic("buffer too small");
688
689 mem2hex(buffer, gdbregs.regs, gdbregs.bytes());
690 send(buffer);
691 continue;
692
693 case GDBRegW:
694 p = hex2mem(gdbregs.regs, p, gdbregs.bytes());
695 if (p == NULL || *p != '\0')
696 send("E01");
697 else {
698 setregs();
699 send("OK");
700 }
701 continue;
702
703#if 0
704 case GDBSetReg:
705 val = hex2i(&p);
706 if (*p++ != '=') {
707 send("E01");
708 continue;
709 }
710 if (val < 0 && val >= KGDB_NUMREGS) {
711 send("E01");
712 continue;
713 }
714
715 gdbregs.regs[val] = hex2i(&p);
716 setregs();
717 send("OK");
718
719 continue;
720#endif
721
722 case GDBMemR:
723 val = hex2i(&p);
724 if (*p++ != ',') {
725 send("E02");
726 continue;
727 }
728 len = hex2i(&p);
729 if (*p != '\0') {
730 send("E03");
731 continue;
732 }
733 if (len > sizeof(buffer)) {
734 send("E04");
735 continue;
736 }
737 if (!acc(val, len)) {
738 send("E05");
739 continue;
740 }
741
742 if (read(val, (size_t)len, (char *)buffer)) {
743 // variable length array would be nice, but C++ doesn't
744 // officially support those...
745 char *temp = new char[2*len+1];
746 mem2hex(temp, buffer, len);
747 send(temp);
748 delete [] temp;
749 } else {
750 send("E05");
751 }
752 continue;
753
754 case GDBMemW:
755 val = hex2i(&p);
756 if (*p++ != ',') {
757 send("E06");
758 continue;
759 }
760 len = hex2i(&p);
761 if (*p++ != ':') {
762 send("E07");
763 continue;
764 }
765 if (len > datalen - (p - data)) {
766 send("E08");
767 continue;
768 }
769 p = hex2mem(buffer, p, sizeof(buffer));
770 if (p == NULL) {
771 send("E09");
772 continue;
773 }
774 if (!acc(val, len)) {
775 send("E0A");
776 continue;
777 }
778 if (write(val, (size_t)len, (char *)buffer))
779 send("OK");
780 else
781 send("E0B");
782 continue;
783
784 case GDBSetThread:
785 subcmd = *p++;
786 val = hex2i(&p);
787 if (val == 0)
788 send("OK");
789 else
790 send("E01");
791 continue;
792
793 case GDBDetach:
794 case GDBKill:
795 active = false;
796 clearSingleStep();
797 detach();
798 goto out;
799
800 case GDBAsyncCont:
801 subcmd = hex2i(&p);
802 if (*p++ == ';') {
803 val = hex2i(&p);
804 context->setPC(val);
805 context->setNextPC(val + sizeof(MachInst));
806 }
807 clearSingleStep();
808 goto out;
809
810 case GDBCont:
811 if (p - data < datalen) {
812 val = hex2i(&p);
813 context->setPC(val);
814 context->setNextPC(val + sizeof(MachInst));
815 }
816 clearSingleStep();
817 goto out;
818
819 case GDBAsyncStep:
820 subcmd = hex2i(&p);
821 if (*p++ == ';') {
822 val = hex2i(&p);
823 context->setPC(val);
824 context->setNextPC(val + sizeof(MachInst));
825 }
826 setSingleStep();
827 goto out;
828
829 case GDBStep:
830 if (p - data < datalen) {
831 val = hex2i(&p);
832 context->setPC(val);
833 context->setNextPC(val + sizeof(MachInst));
834 }
835 setSingleStep();
836 goto out;
837
838 case GDBClrHwBkpt:
839 subcmd = *p++;
840 if (*p++ != ',') send("E0D");
841 val = hex2i(&p);
842 if (*p++ != ',') send("E0D");
843 len = hex2i(&p);
844
845 DPRINTF(GDBMisc, "clear %s, addr=%#x, len=%d\n",
846 break_type(subcmd), val, len);
847
848 ret = false;
849
850 switch (subcmd) {
851 case '0': // software breakpoint
852 ret = removeSoftBreak(val, len);
853 break;
854
855 case '1': // hardware breakpoint
856 ret = removeHardBreak(val, len);
857 break;
858
859 case '2': // write watchpoint
860 case '3': // read watchpoint
861 case '4': // access watchpoint
862 default: // unknown
863 send("");
864 break;
865 }
866
867 send(ret ? "OK" : "E0C");
868 continue;
869
870 case GDBSetHwBkpt:
871 subcmd = *p++;
872 if (*p++ != ',') send("E0D");
873 val = hex2i(&p);
874 if (*p++ != ',') send("E0D");
875 len = hex2i(&p);
876
877 DPRINTF(GDBMisc, "set %s, addr=%#x, len=%d\n",
878 break_type(subcmd), val, len);
879
880 ret = false;
881
882 switch (subcmd) {
883 case '0': // software breakpoint
884 ret = insertSoftBreak(val, len);
885 break;
886
887 case '1': // hardware breakpoint
888 ret = insertHardBreak(val, len);
889 break;
890
891 case '2': // write watchpoint
892 case '3': // read watchpoint
893 case '4': // access watchpoint
894 default: // unknown
895 send("");
896 break;
897 }
898
899 send(ret ? "OK" : "E0C");
900 continue;
901
902 case GDBQueryVar:
903 var = string(p, datalen - 1);
904 if (var == "C")
905 send("QC0");
906 else
907 send("");
908 continue;
909
910 case GDBSetBaud:
911 case GDBSetBreak:
912 case GDBDebug:
913 case GDBCycleStep:
914 case GDBSigCycleStep:
915 case GDBReadReg:
916 case GDBSetVar:
917 case GDBReset:
918 case GDBThreadAlive:
919 case GDBTargetExit:
920 case GDBBinaryDload:
921 // Unsupported command
922 DPRINTF(GDBMisc, "Unsupported command: %s\n",
923 gdb_command(command));
924 DDUMP(GDBMisc, (uint8_t *)data, datalen);
925 send("");
926 continue;
927
928 default:
929 // Unknown command.
930 DPRINTF(GDBMisc, "Unknown command: %c(%#x)\n",
931 command, command);
932 send("");
933 continue;
934
935
936 }
937 }
938
939 out:
645 DPRINTF(GDBMisc, "trap: PC=%#x NPC=%#x\n",
646 context->readPC(), context->readNextPC());
647
648 clearSingleStep();
649
650 /*
651 * The first entry to this function is normally through
652 * a breakpoint trap in kgdb_connect(), in which case we
653 * must advance past the breakpoint because gdb will not.
654 *
655 * On the first entry here, we expect that gdb is not yet
656 * listening to us, so just enter the interaction loop.
657 * After the debugger is "active" (connected) it will be
658 * waiting for a "signaled" message from us.
659 */
660 if (!active)
661 active = true;
662 else
663 // Tell remote host that an exception has occurred.
664 snprintf((char *)buffer, sizeof(buffer), "S%02x", type);
665 send(buffer);
666
667 // Stick frame regs into our reg cache.
668 getregs();
669
670 for (;;) {
671 datalen = recv(data, sizeof(data));
672 data[sizeof(data) - 1] = 0; // Sentinel
673 command = data[0];
674 subcmd = 0;
675 p = data + 1;
676 switch (command) {
677
678 case GDBSignal:
679 // if this command came from a running gdb, answer it --
680 // the other guy has no way of knowing if we're in or out
681 // of this loop when he issues a "remote-signal".
682 snprintf((char *)buffer, sizeof(buffer),
683 "S%02x", type);
684 send(buffer);
685 continue;
686
687 case GDBRegR:
688 if (2 * gdbregs.bytes() > sizeof(buffer))
689 panic("buffer too small");
690
691 mem2hex(buffer, gdbregs.regs, gdbregs.bytes());
692 send(buffer);
693 continue;
694
695 case GDBRegW:
696 p = hex2mem(gdbregs.regs, p, gdbregs.bytes());
697 if (p == NULL || *p != '\0')
698 send("E01");
699 else {
700 setregs();
701 send("OK");
702 }
703 continue;
704
705#if 0
706 case GDBSetReg:
707 val = hex2i(&p);
708 if (*p++ != '=') {
709 send("E01");
710 continue;
711 }
712 if (val < 0 && val >= KGDB_NUMREGS) {
713 send("E01");
714 continue;
715 }
716
717 gdbregs.regs[val] = hex2i(&p);
718 setregs();
719 send("OK");
720
721 continue;
722#endif
723
724 case GDBMemR:
725 val = hex2i(&p);
726 if (*p++ != ',') {
727 send("E02");
728 continue;
729 }
730 len = hex2i(&p);
731 if (*p != '\0') {
732 send("E03");
733 continue;
734 }
735 if (len > sizeof(buffer)) {
736 send("E04");
737 continue;
738 }
739 if (!acc(val, len)) {
740 send("E05");
741 continue;
742 }
743
744 if (read(val, (size_t)len, (char *)buffer)) {
745 // variable length array would be nice, but C++ doesn't
746 // officially support those...
747 char *temp = new char[2*len+1];
748 mem2hex(temp, buffer, len);
749 send(temp);
750 delete [] temp;
751 } else {
752 send("E05");
753 }
754 continue;
755
756 case GDBMemW:
757 val = hex2i(&p);
758 if (*p++ != ',') {
759 send("E06");
760 continue;
761 }
762 len = hex2i(&p);
763 if (*p++ != ':') {
764 send("E07");
765 continue;
766 }
767 if (len > datalen - (p - data)) {
768 send("E08");
769 continue;
770 }
771 p = hex2mem(buffer, p, sizeof(buffer));
772 if (p == NULL) {
773 send("E09");
774 continue;
775 }
776 if (!acc(val, len)) {
777 send("E0A");
778 continue;
779 }
780 if (write(val, (size_t)len, (char *)buffer))
781 send("OK");
782 else
783 send("E0B");
784 continue;
785
786 case GDBSetThread:
787 subcmd = *p++;
788 val = hex2i(&p);
789 if (val == 0)
790 send("OK");
791 else
792 send("E01");
793 continue;
794
795 case GDBDetach:
796 case GDBKill:
797 active = false;
798 clearSingleStep();
799 detach();
800 goto out;
801
802 case GDBAsyncCont:
803 subcmd = hex2i(&p);
804 if (*p++ == ';') {
805 val = hex2i(&p);
806 context->setPC(val);
807 context->setNextPC(val + sizeof(MachInst));
808 }
809 clearSingleStep();
810 goto out;
811
812 case GDBCont:
813 if (p - data < datalen) {
814 val = hex2i(&p);
815 context->setPC(val);
816 context->setNextPC(val + sizeof(MachInst));
817 }
818 clearSingleStep();
819 goto out;
820
821 case GDBAsyncStep:
822 subcmd = hex2i(&p);
823 if (*p++ == ';') {
824 val = hex2i(&p);
825 context->setPC(val);
826 context->setNextPC(val + sizeof(MachInst));
827 }
828 setSingleStep();
829 goto out;
830
831 case GDBStep:
832 if (p - data < datalen) {
833 val = hex2i(&p);
834 context->setPC(val);
835 context->setNextPC(val + sizeof(MachInst));
836 }
837 setSingleStep();
838 goto out;
839
840 case GDBClrHwBkpt:
841 subcmd = *p++;
842 if (*p++ != ',') send("E0D");
843 val = hex2i(&p);
844 if (*p++ != ',') send("E0D");
845 len = hex2i(&p);
846
847 DPRINTF(GDBMisc, "clear %s, addr=%#x, len=%d\n",
848 break_type(subcmd), val, len);
849
850 ret = false;
851
852 switch (subcmd) {
853 case '0': // software breakpoint
854 ret = removeSoftBreak(val, len);
855 break;
856
857 case '1': // hardware breakpoint
858 ret = removeHardBreak(val, len);
859 break;
860
861 case '2': // write watchpoint
862 case '3': // read watchpoint
863 case '4': // access watchpoint
864 default: // unknown
865 send("");
866 break;
867 }
868
869 send(ret ? "OK" : "E0C");
870 continue;
871
872 case GDBSetHwBkpt:
873 subcmd = *p++;
874 if (*p++ != ',') send("E0D");
875 val = hex2i(&p);
876 if (*p++ != ',') send("E0D");
877 len = hex2i(&p);
878
879 DPRINTF(GDBMisc, "set %s, addr=%#x, len=%d\n",
880 break_type(subcmd), val, len);
881
882 ret = false;
883
884 switch (subcmd) {
885 case '0': // software breakpoint
886 ret = insertSoftBreak(val, len);
887 break;
888
889 case '1': // hardware breakpoint
890 ret = insertHardBreak(val, len);
891 break;
892
893 case '2': // write watchpoint
894 case '3': // read watchpoint
895 case '4': // access watchpoint
896 default: // unknown
897 send("");
898 break;
899 }
900
901 send(ret ? "OK" : "E0C");
902 continue;
903
904 case GDBQueryVar:
905 var = string(p, datalen - 1);
906 if (var == "C")
907 send("QC0");
908 else
909 send("");
910 continue;
911
912 case GDBSetBaud:
913 case GDBSetBreak:
914 case GDBDebug:
915 case GDBCycleStep:
916 case GDBSigCycleStep:
917 case GDBReadReg:
918 case GDBSetVar:
919 case GDBReset:
920 case GDBThreadAlive:
921 case GDBTargetExit:
922 case GDBBinaryDload:
923 // Unsupported command
924 DPRINTF(GDBMisc, "Unsupported command: %s\n",
925 gdb_command(command));
926 DDUMP(GDBMisc, (uint8_t *)data, datalen);
927 send("");
928 continue;
929
930 default:
931 // Unknown command.
932 DPRINTF(GDBMisc, "Unknown command: %c(%#x)\n",
933 command, command);
934 send("");
935 continue;
936
937
938 }
939 }
940
941 out:
942 free(buffer);
940 return true;
941}
942
943// Convert a hex digit into an integer.
944// This returns -1 if the argument passed is no valid hex digit.
945int
946BaseRemoteGDB::digit2i(char c)
947{
948 if (c >= '0' && c <= '9')
949 return (c - '0');
950 else if (c >= 'a' && c <= 'f')
951 return (c - 'a' + 10);
952 else if (c >= 'A' && c <= 'F')
953
954 return (c - 'A' + 10);
955 else
956 return (-1);
957}
958
959// Convert the low 4 bits of an integer into an hex digit.
960char
961BaseRemoteGDB::i2digit(int n)
962{
963 return ("0123456789abcdef"[n & 0x0f]);
964}
965
966// Convert a byte array into an hex string.
967void
968BaseRemoteGDB::mem2hex(void *vdst, const void *vsrc, int len)
969{
970 char *dst = (char *)vdst;
971 const char *src = (const char *)vsrc;
972
973 while (len--) {
974 *dst++ = i2digit(*src >> 4);
975 *dst++ = i2digit(*src++);
976 }
977 *dst = '\0';
978}
979
980// Convert an hex string into a byte array.
981// This returns a pointer to the character following the last valid
982// hex digit. If the string ends in the middle of a byte, NULL is
983// returned.
984const char *
985BaseRemoteGDB::hex2mem(void *vdst, const char *src, int maxlen)
986{
987 char *dst = (char *)vdst;
988 int msb, lsb;
989
990 while (*src && maxlen--) {
991 msb = digit2i(*src++);
992 if (msb < 0)
993 return (src - 1);
994 lsb = digit2i(*src++);
995 if (lsb < 0)
996 return (NULL);
997 *dst++ = (msb << 4) | lsb;
998 }
999 return (src);
1000}
1001
1002// Convert an hex string into an integer.
1003// This returns a pointer to the character following the last valid
1004// hex digit.
1005Addr
1006BaseRemoteGDB::hex2i(const char **srcp)
1007{
1008 const char *src = *srcp;
1009 Addr r = 0;
1010 int nibble;
1011
1012 while ((nibble = digit2i(*src)) >= 0) {
1013 r *= 16;
1014 r += nibble;
1015 src++;
1016 }
1017 *srcp = src;
1018 return (r);
1019}
1020
943 return true;
944}
945
946// Convert a hex digit into an integer.
947// This returns -1 if the argument passed is no valid hex digit.
948int
949BaseRemoteGDB::digit2i(char c)
950{
951 if (c >= '0' && c <= '9')
952 return (c - '0');
953 else if (c >= 'a' && c <= 'f')
954 return (c - 'a' + 10);
955 else if (c >= 'A' && c <= 'F')
956
957 return (c - 'A' + 10);
958 else
959 return (-1);
960}
961
962// Convert the low 4 bits of an integer into an hex digit.
963char
964BaseRemoteGDB::i2digit(int n)
965{
966 return ("0123456789abcdef"[n & 0x0f]);
967}
968
969// Convert a byte array into an hex string.
970void
971BaseRemoteGDB::mem2hex(void *vdst, const void *vsrc, int len)
972{
973 char *dst = (char *)vdst;
974 const char *src = (const char *)vsrc;
975
976 while (len--) {
977 *dst++ = i2digit(*src >> 4);
978 *dst++ = i2digit(*src++);
979 }
980 *dst = '\0';
981}
982
983// Convert an hex string into a byte array.
984// This returns a pointer to the character following the last valid
985// hex digit. If the string ends in the middle of a byte, NULL is
986// returned.
987const char *
988BaseRemoteGDB::hex2mem(void *vdst, const char *src, int maxlen)
989{
990 char *dst = (char *)vdst;
991 int msb, lsb;
992
993 while (*src && maxlen--) {
994 msb = digit2i(*src++);
995 if (msb < 0)
996 return (src - 1);
997 lsb = digit2i(*src++);
998 if (lsb < 0)
999 return (NULL);
1000 *dst++ = (msb << 4) | lsb;
1001 }
1002 return (src);
1003}
1004
1005// Convert an hex string into an integer.
1006// This returns a pointer to the character following the last valid
1007// hex digit.
1008Addr
1009BaseRemoteGDB::hex2i(const char **srcp)
1010{
1011 const char *src = *srcp;
1012 Addr r = 0;
1013 int nibble;
1014
1015 while ((nibble = digit2i(*src)) >= 0) {
1016 r *= 16;
1017 r += nibble;
1018 src++;
1019 }
1020 *srcp = src;
1021 return (r);
1022}
1023