cpu: Fix retries on barrier/store in Minor's store buffer
[gem5.git] / src / base / remote_gdb.cc
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 The Regents of the University of California
33 * 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 <signal.h>
120 #include <sys/signal.h>
121 #include <unistd.h>
122
123 #include <cstdio>
124 #include <string>
125
126 #include "arch/vtophys.hh"
127 #include "base/intmath.hh"
128 #include "base/remote_gdb.hh"
129 #include "base/socket.hh"
130 #include "base/trace.hh"
131 #include "config/the_isa.hh"
132 #include "cpu/static_inst.hh"
133 #include "cpu/thread_context.hh"
134 #include "debug/GDBAll.hh"
135 #include "mem/port.hh"
136 #include "mem/fs_translating_port_proxy.hh"
137 #include "mem/se_translating_port_proxy.hh"
138 #include "sim/full_system.hh"
139 #include "sim/system.hh"
140
141 using namespace std;
142 using namespace Debug;
143 using namespace TheISA;
144
145 #ifndef NDEBUG
146 vector<BaseRemoteGDB *> debuggers;
147
148 void
149 debugger()
150 {
151 static int current_debugger = -1;
152 if (current_debugger >= 0 && current_debugger < (int)debuggers.size()) {
153 BaseRemoteGDB *gdb = debuggers[current_debugger];
154 if (!gdb->isattached())
155 gdb->listener->accept();
156 if (gdb->isattached())
157 gdb->trap(SIGILL);
158 }
159 }
160 #endif
161
162 ///////////////////////////////////////////////////////////
163 //
164 //
165 //
166
167 GDBListener::Event::Event(GDBListener *l, int fd, int e)
168 : PollEvent(fd, e), listener(l)
169 {}
170
171 void
172 GDBListener::Event::process(int revent)
173 {
174 listener->accept();
175 }
176
177 GDBListener::GDBListener(BaseRemoteGDB *g, int p)
178 : event(NULL), gdb(g), port(p)
179 {
180 assert(!gdb->listener);
181 gdb->listener = this;
182 }
183
184 GDBListener::~GDBListener()
185 {
186 if (event)
187 delete event;
188 }
189
190 string
191 GDBListener::name()
192 {
193 return gdb->name() + ".listener";
194 }
195
196 void
197 GDBListener::listen()
198 {
199 if (ListenSocket::allDisabled()) {
200 warn_once("Sockets disabled, not accepting gdb connections");
201 return;
202 }
203
204 while (!listener.listen(port, true)) {
205 DPRINTF(GDBMisc, "Can't bind port %d\n", port);
206 port++;
207 }
208
209 event = new Event(this, listener.getfd(), POLLIN);
210 pollQueue.schedule(event);
211
212 #ifndef NDEBUG
213 gdb->number = debuggers.size();
214 debuggers.push_back(gdb);
215 #endif
216
217 #ifndef NDEBUG
218 ccprintf(cerr, "%d: %s: listening for remote gdb #%d on port %d\n",
219 curTick(), name(), gdb->number, port);
220 #else
221 ccprintf(cerr, "%d: %s: listening for remote gdb on port %d\n",
222 curTick(), name(), port);
223 #endif
224 }
225
226 void
227 GDBListener::accept()
228 {
229 if (!listener.islistening())
230 panic("GDBListener::accept(): cannot accept if we're not listening!");
231
232 int sfd = listener.accept(true);
233
234 if (sfd != -1) {
235 if (gdb->isattached())
236 close(sfd);
237 else
238 gdb->attach(sfd);
239 }
240 }
241
242 BaseRemoteGDB::Event::Event(BaseRemoteGDB *g, int fd, int e)
243 : PollEvent(fd, e), gdb(g)
244 {}
245
246 void
247 BaseRemoteGDB::Event::process(int revent)
248 {
249 if (revent & POLLIN)
250 gdb->trap(SIGILL);
251 else if (revent & POLLNVAL)
252 gdb->detach();
253 }
254
255 BaseRemoteGDB::BaseRemoteGDB(System *_system, ThreadContext *c, size_t cacheSize)
256 : event(NULL), listener(NULL), number(-1), fd(-1),
257 active(false), attached(false),
258 system(_system), context(c),
259 gdbregs(cacheSize)
260 {
261 memset(gdbregs.regs, 0, gdbregs.bytes());
262 }
263
264 BaseRemoteGDB::~BaseRemoteGDB()
265 {
266 if (event)
267 delete event;
268 }
269
270 string
271 BaseRemoteGDB::name()
272 {
273 return system->name() + ".remote_gdb";
274 }
275
276 bool
277 BaseRemoteGDB::isattached()
278 { return attached; }
279
280 void
281 BaseRemoteGDB::attach(int f)
282 {
283 fd = f;
284
285 event = new Event(this, fd, POLLIN);
286 pollQueue.schedule(event);
287
288 attached = true;
289 DPRINTFN("remote gdb attached\n");
290 }
291
292 void
293 BaseRemoteGDB::detach()
294 {
295 attached = false;
296 close(fd);
297 fd = -1;
298
299 pollQueue.remove(event);
300 DPRINTFN("remote gdb detached\n");
301 }
302
303 const char *
304 BaseRemoteGDB::gdb_command(char cmd)
305 {
306 switch (cmd) {
307 case GDBSignal: return "KGDB_SIGNAL";
308 case GDBSetBaud: return "KGDB_SET_BAUD";
309 case GDBSetBreak: return "KGDB_SET_BREAK";
310 case GDBCont: return "KGDB_CONT";
311 case GDBAsyncCont: return "KGDB_ASYNC_CONT";
312 case GDBDebug: return "KGDB_DEBUG";
313 case GDBDetach: return "KGDB_DETACH";
314 case GDBRegR: return "KGDB_REG_R";
315 case GDBRegW: return "KGDB_REG_W";
316 case GDBSetThread: return "KGDB_SET_THREAD";
317 case GDBCycleStep: return "KGDB_CYCLE_STEP";
318 case GDBSigCycleStep: return "KGDB_SIG_CYCLE_STEP";
319 case GDBKill: return "KGDB_KILL";
320 case GDBMemW: return "KGDB_MEM_W";
321 case GDBMemR: return "KGDB_MEM_R";
322 case GDBSetReg: return "KGDB_SET_REG";
323 case GDBReadReg: return "KGDB_READ_REG";
324 case GDBQueryVar: return "KGDB_QUERY_VAR";
325 case GDBSetVar: return "KGDB_SET_VAR";
326 case GDBReset: return "KGDB_RESET";
327 case GDBStep: return "KGDB_STEP";
328 case GDBAsyncStep: return "KGDB_ASYNC_STEP";
329 case GDBThreadAlive: return "KGDB_THREAD_ALIVE";
330 case GDBTargetExit: return "KGDB_TARGET_EXIT";
331 case GDBBinaryDload: return "KGDB_BINARY_DLOAD";
332 case GDBClrHwBkpt: return "KGDB_CLR_HW_BKPT";
333 case GDBSetHwBkpt: return "KGDB_SET_HW_BKPT";
334 case GDBStart: return "KGDB_START";
335 case GDBEnd: return "KGDB_END";
336 case GDBGoodP: return "KGDB_GOODP";
337 case GDBBadP: return "KGDB_BADP";
338 default: return "KGDB_UNKNOWN";
339 }
340 }
341
342 /////////////////////////
343 //
344 //
345
346 uint8_t
347 BaseRemoteGDB::getbyte()
348 {
349 uint8_t b;
350 if (::read(fd, &b, 1) != 1)
351 warn("could not read byte from debugger");
352 return b;
353 }
354
355 void
356 BaseRemoteGDB::putbyte(uint8_t b)
357 {
358 if (::write(fd, &b, 1) != 1)
359 warn("could not write byte to debugger");
360 }
361
362 // Send a packet to gdb
363 void
364 BaseRemoteGDB::send(const char *bp)
365 {
366 const char *p;
367 uint8_t csum, c;
368
369 DPRINTF(GDBSend, "send: %s\n", bp);
370
371 do {
372 p = bp;
373 //Start sending a packet
374 putbyte(GDBStart);
375 //Send the contents, and also keep a check sum.
376 for (csum = 0; (c = *p); p++) {
377 putbyte(c);
378 csum += c;
379 }
380 //Send the ending character.
381 putbyte(GDBEnd);
382 //Sent the checksum.
383 putbyte(i2digit(csum >> 4));
384 putbyte(i2digit(csum));
385 //Try transmitting over and over again until the other end doesn't send an
386 //error back.
387 } while ((c = getbyte() & 0x7f) == GDBBadP);
388 }
389
390 // Receive a packet from gdb
391 int
392 BaseRemoteGDB::recv(char *bp, int maxlen)
393 {
394 char *p;
395 int c, csum;
396 int len;
397
398 do {
399 p = bp;
400 csum = len = 0;
401 //Find the beginning of a packet
402 while ((c = getbyte()) != GDBStart)
403 ;
404
405 //Read until you find the end of the data in the packet, and keep
406 //track of the check sum.
407 while ((c = getbyte()) != GDBEnd && len < maxlen) {
408 c &= 0x7f;
409 csum += c;
410 *p++ = c;
411 len++;
412 }
413
414 //Mask the check sum, and terminate the command string.
415 csum &= 0xff;
416 *p = '\0';
417
418 //If the command was too long, report an error.
419 if (len >= maxlen) {
420 putbyte(GDBBadP);
421 continue;
422 }
423
424 //Bring in the checksum. If the check sum matches, csum will be 0.
425 csum -= digit2i(getbyte()) * 16;
426 csum -= digit2i(getbyte());
427
428 //If the check sum was correct
429 if (csum == 0) {
430 //Report that the packet was received correctly
431 putbyte(GDBGoodP);
432 // Sequence present?
433 if (bp[2] == ':') {
434 putbyte(bp[0]);
435 putbyte(bp[1]);
436 len -= 3;
437 memcpy(bp, bp+3, len);
438 }
439 break;
440 }
441 //Otherwise, report that there was a mistake.
442 putbyte(GDBBadP);
443 } while (1);
444
445 DPRINTF(GDBRecv, "recv: %s: %s\n", gdb_command(*bp), bp);
446
447 return (len);
448 }
449
450 // Read bytes from kernel address space for debugger.
451 bool
452 BaseRemoteGDB::read(Addr vaddr, size_t size, char *data)
453 {
454 static Addr lastaddr = 0;
455 static size_t lastsize = 0;
456
457 if (vaddr < 10) {
458 DPRINTF(GDBRead, "read: reading memory location zero!\n");
459 vaddr = lastaddr + lastsize;
460 }
461
462 DPRINTF(GDBRead, "read: addr=%#x, size=%d", vaddr, size);
463
464 if (FullSystem) {
465 FSTranslatingPortProxy &proxy = context->getVirtProxy();
466 proxy.readBlob(vaddr, (uint8_t*)data, size);
467 } else {
468 SETranslatingPortProxy &proxy = context->getMemProxy();
469 proxy.readBlob(vaddr, (uint8_t*)data, size);
470 }
471
472 #if TRACING_ON
473 if (DTRACE(GDBRead)) {
474 if (DTRACE(GDBExtra)) {
475 char buf[1024];
476 mem2hex(buf, data, size);
477 DPRINTFNR(": %s\n", buf);
478 } else
479 DPRINTFNR("\n");
480 }
481 #endif
482
483 return true;
484 }
485
486 // Write bytes to kernel address space for debugger.
487 bool
488 BaseRemoteGDB::write(Addr vaddr, size_t size, const char *data)
489 {
490 static Addr lastaddr = 0;
491 static size_t lastsize = 0;
492
493 if (vaddr < 10) {
494 DPRINTF(GDBWrite, "write: writing memory location zero!\n");
495 vaddr = lastaddr + lastsize;
496 }
497
498 if (DTRACE(GDBWrite)) {
499 DPRINTFN("write: addr=%#x, size=%d", vaddr, size);
500 if (DTRACE(GDBExtra)) {
501 char buf[1024];
502 mem2hex(buf, data, size);
503 DPRINTFNR(": %s\n", buf);
504 } else
505 DPRINTFNR("\n");
506 }
507 if (FullSystem) {
508 FSTranslatingPortProxy &proxy = context->getVirtProxy();
509 proxy.writeBlob(vaddr, (uint8_t*)data, size);
510 } else {
511 SETranslatingPortProxy &proxy = context->getMemProxy();
512 proxy.writeBlob(vaddr, (uint8_t*)data, size);
513 }
514
515 return true;
516 }
517
518 PCEventQueue *BaseRemoteGDB::getPcEventQueue()
519 {
520 return &system->pcEventQueue;
521 }
522
523 BaseRemoteGDB::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
530 void
531 BaseRemoteGDB::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
539 bool
540 BaseRemoteGDB::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
548 bool
549 BaseRemoteGDB::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
557 bool
558 BaseRemoteGDB::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
574 bool
575 BaseRemoteGDB::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
595 void
596 BaseRemoteGDB::setTempBreakpoint(Addr bkpt)
597 {
598 DPRINTF(GDBMisc, "setTempBreakpoint: addr=%#x\n", bkpt);
599 insertHardBreak(bkpt, sizeof(TheISA::MachInst));
600 }
601
602 void
603 BaseRemoteGDB::clearTempBreakpoint(Addr &bkpt)
604 {
605 DPRINTF(GDBMisc, "setTempBreakpoint: addr=%#x\n", bkpt);
606 removeHardBreak(bkpt, sizeof(TheISA::MachInst));
607 bkpt = 0;
608 }
609
610 const char *
611 BaseRemoteGDB::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.
628 bool
629 BaseRemoteGDB::trap(int type)
630 {
631 uint64_t val;
632 size_t datalen, len;
633 char data[GDBPacketBufLen + 1];
634 char *buffer;
635 size_t bufferSize;
636 const char *p;
637 char command, subcmd;
638 string var;
639 bool ret;
640
641 if (!attached)
642 return false;
643
644 bufferSize = gdbregs.bytes() * 2 + 256;
645 buffer = (char*)malloc(bufferSize);
646
647 DPRINTF(GDBMisc, "trap: PC=%s\n", context->pcState());
648
649 clearSingleStep();
650
651 /*
652 * The first entry to this function is normally through
653 * a breakpoint trap in kgdb_connect(), in which case we
654 * must advance past the breakpoint because gdb will not.
655 *
656 * On the first entry here, we expect that gdb is not yet
657 * listening to us, so just enter the interaction loop.
658 * After the debugger is "active" (connected) it will be
659 * waiting for a "signaled" message from us.
660 */
661 if (!active) {
662 active = true;
663 } else {
664 // Tell remote host that an exception has occurred.
665 snprintf((char *)buffer, bufferSize, "S%02x", type);
666 send(buffer);
667 }
668
669 // Stick frame regs into our reg cache.
670 getregs();
671
672 for (;;) {
673 datalen = recv(data, sizeof(data));
674 data[sizeof(data) - 1] = 0; // Sentinel
675 command = data[0];
676 subcmd = 0;
677 p = data + 1;
678 switch (command) {
679
680 case GDBSignal:
681 // if this command came from a running gdb, answer it --
682 // the other guy has no way of knowing if we're in or out
683 // of this loop when he issues a "remote-signal".
684 snprintf((char *)buffer, bufferSize,
685 "S%02x", type);
686 send(buffer);
687 continue;
688
689 case GDBRegR:
690 if (2 * gdbregs.bytes() > bufferSize)
691 panic("buffer too small");
692
693 mem2hex(buffer, gdbregs.regs, gdbregs.bytes());
694 send(buffer);
695 continue;
696
697 case GDBRegW:
698 p = hex2mem(gdbregs.regs, p, gdbregs.bytes());
699 if (p == NULL || *p != '\0')
700 send("E01");
701 else {
702 setregs();
703 send("OK");
704 }
705 continue;
706
707 #if 0
708 case GDBSetReg:
709 val = hex2i(&p);
710 if (*p++ != '=') {
711 send("E01");
712 continue;
713 }
714 if (val < 0 && val >= KGDB_NUMREGS) {
715 send("E01");
716 continue;
717 }
718
719 gdbregs.regs[val] = hex2i(&p);
720 setregs();
721 send("OK");
722
723 continue;
724 #endif
725
726 case GDBMemR:
727 val = hex2i(&p);
728 if (*p++ != ',') {
729 send("E02");
730 continue;
731 }
732 len = hex2i(&p);
733 if (*p != '\0') {
734 send("E03");
735 continue;
736 }
737 if (len > bufferSize) {
738 send("E04");
739 continue;
740 }
741 if (!acc(val, len)) {
742 send("E05");
743 continue;
744 }
745
746 if (read(val, (size_t)len, (char *)buffer)) {
747 // variable length array would be nice, but C++ doesn't
748 // officially support those...
749 char *temp = new char[2*len+1];
750 mem2hex(temp, buffer, len);
751 send(temp);
752 delete [] temp;
753 } else {
754 send("E05");
755 }
756 continue;
757
758 case GDBMemW:
759 val = hex2i(&p);
760 if (*p++ != ',') {
761 send("E06");
762 continue;
763 }
764 len = hex2i(&p);
765 if (*p++ != ':') {
766 send("E07");
767 continue;
768 }
769 if (len > datalen - (p - data)) {
770 send("E08");
771 continue;
772 }
773 p = hex2mem(buffer, p, bufferSize);
774 if (p == NULL) {
775 send("E09");
776 continue;
777 }
778 if (!acc(val, len)) {
779 send("E0A");
780 continue;
781 }
782 if (write(val, (size_t)len, (char *)buffer))
783 send("OK");
784 else
785 send("E0B");
786 continue;
787
788 case GDBSetThread:
789 subcmd = *p++;
790 val = hex2i(&p);
791 if (val == 0)
792 send("OK");
793 else
794 send("E01");
795 continue;
796
797 case GDBDetach:
798 case GDBKill:
799 active = false;
800 clearSingleStep();
801 detach();
802 goto out;
803
804 case GDBAsyncCont:
805 subcmd = hex2i(&p);
806 if (*p++ == ';') {
807 val = hex2i(&p);
808 context->pcState(val);
809 }
810 clearSingleStep();
811 goto out;
812
813 case GDBCont:
814 if (p - data < (ptrdiff_t)datalen) {
815 val = hex2i(&p);
816 context->pcState(val);
817 }
818 clearSingleStep();
819 goto out;
820
821 case GDBAsyncStep:
822 subcmd = hex2i(&p);
823 if (*p++ == ';') {
824 val = hex2i(&p);
825 context->pcState(val);
826 }
827 setSingleStep();
828 goto out;
829
830 case GDBStep:
831 if (p - data < (ptrdiff_t)datalen) {
832 val = hex2i(&p);
833 context->pcState(val);
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:
940 free(buffer);
941 return true;
942 }
943
944 // Convert a hex digit into an integer.
945 // This returns -1 if the argument passed is no valid hex digit.
946 int
947 BaseRemoteGDB::digit2i(char c)
948 {
949 if (c >= '0' && c <= '9')
950 return (c - '0');
951 else if (c >= 'a' && c <= 'f')
952 return (c - 'a' + 10);
953 else if (c >= 'A' && c <= 'F')
954
955 return (c - 'A' + 10);
956 else
957 return (-1);
958 }
959
960 // Convert the low 4 bits of an integer into an hex digit.
961 char
962 BaseRemoteGDB::i2digit(int n)
963 {
964 return ("0123456789abcdef"[n & 0x0f]);
965 }
966
967 // Convert a byte array into an hex string.
968 void
969 BaseRemoteGDB::mem2hex(void *vdst, const void *vsrc, int len)
970 {
971 char *dst = (char *)vdst;
972 const char *src = (const char *)vsrc;
973
974 while (len--) {
975 *dst++ = i2digit(*src >> 4);
976 *dst++ = i2digit(*src++);
977 }
978 *dst = '\0';
979 }
980
981 // Convert an hex string into a byte array.
982 // This returns a pointer to the character following the last valid
983 // hex digit. If the string ends in the middle of a byte, NULL is
984 // returned.
985 const char *
986 BaseRemoteGDB::hex2mem(void *vdst, const char *src, int maxlen)
987 {
988 char *dst = (char *)vdst;
989 int msb, lsb;
990
991 while (*src && maxlen--) {
992 msb = digit2i(*src++);
993 if (msb < 0)
994 return (src - 1);
995 lsb = digit2i(*src++);
996 if (lsb < 0)
997 return (NULL);
998 *dst++ = (msb << 4) | lsb;
999 }
1000 return (src);
1001 }
1002
1003 // Convert an hex string into an integer.
1004 // This returns a pointer to the character following the last valid
1005 // hex digit.
1006 Addr
1007 BaseRemoteGDB::hex2i(const char **srcp)
1008 {
1009 const char *src = *srcp;
1010 Addr r = 0;
1011 int nibble;
1012
1013 while ((nibble = digit2i(*src)) >= 0) {
1014 r *= 16;
1015 r += nibble;
1016 src++;
1017 }
1018 *srcp = src;
1019 return (r);
1020 }
1021