Merge ktlim@zizzer:/bk/m5
[gem5.git] / 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
29 /*
30 * Copyright (c) 1990, 1993
31 * The Regents of the University of California. All rights reserved.
32 *
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.
36 *
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.
41 *
42 * Redistribution and use in source and binary forms, with or without
43 * modification, are permitted provided that the following conditions
44 * are met:
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.
57 *
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
68 * SUCH DAMAGE.
69 *
70 * @(#)kgdb_stub.c 8.4 (Berkeley) 1/12/94
71 */
72
73 /*-
74 * Copyright (c) 2001 The NetBSD Foundation, Inc.
75 * All rights reserved.
76 *
77 * This code is derived from software contributed to The NetBSD Foundation
78 * by Jason R. Thorpe.
79 *
80 * Redistribution and use in source and binary forms, with or without
81 * modification, are permitted provided that the following conditions
82 * are met:
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.
95 *
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.
107 */
108
109 /*
110 * $NetBSD: kgdb_stub.c,v 1.8 2001/07/07 22:58:00 wdk Exp $
111 *
112 * Taken from NetBSD
113 *
114 * "Stub" to allow remote cpu to debug over a serial line using gdb.
115 */
116
117 #include <sys/signal.h>
118
119 #include <cstdio>
120 #include <string>
121 #include <unistd.h>
122
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"
133
134 using namespace std;
135 using namespace TheISA;
136
137 #ifndef NDEBUG
138 vector<RemoteGDB *> debuggers;
139 int current_debugger = -1;
140
141 void
142 debugger()
143 {
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);
150 }
151 }
152 #endif
153
154 ///////////////////////////////////////////////////////////
155 //
156 //
157 //
158
159 GDBListener::Event::Event(GDBListener *l, int fd, int e)
160 : PollEvent(fd, e), listener(l)
161 {}
162
163 void
164 GDBListener::Event::process(int revent)
165 {
166 listener->accept();
167 }
168
169 GDBListener::GDBListener(RemoteGDB *g, int p)
170 : event(NULL), gdb(g), port(p)
171 {
172 assert(!gdb->listener);
173 gdb->listener = this;
174 }
175
176 GDBListener::~GDBListener()
177 {
178 if (event)
179 delete event;
180 }
181
182 string
183 GDBListener::name()
184 {
185 return gdb->name() + ".listener";
186 }
187
188 void
189 GDBListener::listen()
190 {
191 while (!listener.listen(port, true)) {
192 DPRINTF(GDBMisc, "Can't bind port %d\n", port);
193 port++;
194 }
195
196 event = new Event(this, listener.getfd(), POLLIN);
197 pollQueue.schedule(event);
198
199 #ifndef NDEBUG
200 gdb->number = debuggers.size();
201 debuggers.push_back(gdb);
202 #endif
203
204 #ifndef NDEBUG
205 ccprintf(cerr, "%d: %s: listening for remote gdb #%d on port %d\n",
206 curTick, name(), gdb->number, port);
207 #else
208 ccprintf(cerr, "%d: %s: listening for remote gdb on port %d\n",
209 curTick, name(), port);
210 #endif
211 }
212
213 void
214 GDBListener::accept()
215 {
216 if (!listener.islistening())
217 panic("GDBListener::accept(): cannot accept if we're not listening!");
218
219 int sfd = listener.accept(true);
220
221 if (sfd != -1) {
222 if (gdb->isattached())
223 close(sfd);
224 else
225 gdb->attach(sfd);
226 }
227 }
228
229 ///////////////////////////////////////////////////////////
230 //
231 //
232 //
233 int digit2i(char);
234 char i2digit(int);
235 void mem2hex(void *, const void *, int);
236 const char *hex2mem(void *, const char *, int);
237 Addr hex2i(const char **);
238
239 RemoteGDB::Event::Event(RemoteGDB *g, int fd, int e)
240 : PollEvent(fd, e), gdb(g)
241 {}
242
243 void
244 RemoteGDB::Event::process(int revent)
245 {
246 if (revent & POLLIN)
247 gdb->trap(ALPHA_KENTRY_IF);
248 else if (revent & POLLNVAL)
249 gdb->detach();
250 }
251
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)
256 {
257 memset(gdbregs, 0, sizeof(gdbregs));
258 }
259
260 RemoteGDB::~RemoteGDB()
261 {
262 if (event)
263 delete event;
264 }
265
266 string
267 RemoteGDB::name()
268 {
269 return system->name() + ".remote_gdb";
270 }
271
272 bool
273 RemoteGDB::isattached()
274 { return attached; }
275
276 void
277 RemoteGDB::attach(int f)
278 {
279 fd = f;
280
281 event = new Event(this, fd, POLLIN);
282 pollQueue.schedule(event);
283
284 attached = true;
285 DPRINTFN("remote gdb attached\n");
286 }
287
288 void
289 RemoteGDB::detach()
290 {
291 attached = false;
292 close(fd);
293 fd = -1;
294
295 pollQueue.remove(event);
296 DPRINTFN("remote gdb detached\n");
297 }
298
299 const char *
300 gdb_command(char cmd)
301 {
302 switch (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";
335 }
336 }
337
338 ///////////////////////////////////////////////////////////
339 // RemoteGDB::acc
340 //
341 // Determine if the mapping at va..(va+len) is valid.
342 //
343 bool
344 RemoteGDB::acc(Addr va, size_t len)
345 {
346 Addr last_va;
347
348 va = TheISA::TruncPage(va);
349 last_va = TheISA::RoundPage(va + len);
350
351 do {
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);
356 return true;
357 } else {
358 DPRINTF(GDBAcc, "acc: Mapping invalid %#x > K0SEG + size\n",
359 va);
360 return false;
361 }
362 }
363
364 /**
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.
369 */
370
371 if (AlphaISA::PcPAL(va) || va < 0x10000)
372 return true;
373
374 Addr ptbr = context->readMiscReg(AlphaISA::IPR_PALtemp20);
375 TheISA::PageTableEntry pte = kernel_pte_lookup(pmem, ptbr, va);
376 if (!pte.valid()) {
377 DPRINTF(GDBAcc, "acc: %#x pte is invalid\n", va);
378 return false;
379 }
380 va += TheISA::PageBytes;
381 } while (va < last_va);
382
383 DPRINTF(GDBAcc, "acc: %#x mapping is valid\n", va);
384 return true;
385 }
386
387 ///////////////////////////////////////////////////////////
388 // RemoteGDB::signal
389 //
390 // Translate a trap number into a Unix-compatible signal number.
391 // (GDB only understands Unix signal numbers.)
392 //
393 int
394 RemoteGDB::signal(int type)
395 {
396 switch (type) {
397 case ALPHA_KENTRY_INT:
398 return (SIGTRAP);
399
400 case ALPHA_KENTRY_UNA:
401 return (SIGBUS);
402
403 case ALPHA_KENTRY_ARITH:
404 return (SIGFPE);
405
406 case ALPHA_KENTRY_IF:
407 return (SIGILL);
408
409 case ALPHA_KENTRY_MM:
410 return (SIGSEGV);
411
412 default:
413 panic("unknown signal type");
414 return 0;
415 }
416 }
417
418 ///////////////////////////////////////////////////////////
419 // RemoteGDB::getregs
420 //
421 // Translate the kernel debugger register format into
422 // the GDB register format.
423 void
424 RemoteGDB::getregs()
425 {
426 memset(gdbregs, 0, sizeof(gdbregs));
427
428 gdbregs[KGDB_REG_PC] = context->readPC();
429
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]);
434 }
435 } else {
436 for (int i = 0; i < TheISA::NumIntArchRegs; ++i) {
437 gdbregs[i] = context->readIntReg(i);
438 }
439 }
440
441 #ifdef KGDB_FP_REGS
442 for (int i = 0; i < TheISA::NumFloatArchRegs; ++i) {
443 gdbregs[i + KGDB_REG_F0] = context->readFloatRegInt(i);
444 }
445 #endif
446 }
447
448 ///////////////////////////////////////////////////////////
449 // RemoteGDB::setregs
450 //
451 // Translate the GDB register format into the kernel
452 // debugger register format.
453 //
454 void
455 RemoteGDB::setregs()
456 {
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]);
461 }
462 } else {
463 for (int i = 0; i < TheISA::NumIntArchRegs; ++i) {
464 context->setIntReg(i, gdbregs[i]);
465 }
466 }
467
468 #ifdef KGDB_FP_REGS
469 for (int i = 0; i < TheISA::NumFloatArchRegs; ++i) {
470 context->setFloatRegInt(i, gdbregs[i + KGDB_REG_F0]);
471 }
472 #endif
473 context->regs.pc = gdbregs[KGDB_REG_PC];
474 }
475
476 void
477 RemoteGDB::setTempBreakpoint(TempBreakpoint &bkpt, Addr addr)
478 {
479 DPRINTF(GDBMisc, "setTempBreakpoint: addr=%#x\n", addr);
480
481 bkpt.address = addr;
482 insertHardBreak(addr, 4);
483 }
484
485 void
486 RemoteGDB::clearTempBreakpoint(TempBreakpoint &bkpt)
487 {
488 DPRINTF(GDBMisc, "setTempBreakpoint: addr=%#x\n",
489 bkpt.address);
490
491
492 removeHardBreak(bkpt.address, 4);
493 bkpt.address = 0;
494 }
495
496 void
497 RemoteGDB::clearSingleStep()
498 {
499 DPRINTF(GDBMisc, "clearSingleStep bt_addr=%#x nt_addr=%#x\n",
500 takenBkpt.address, notTakenBkpt.address);
501
502 if (takenBkpt.address != 0)
503 clearTempBreakpoint(takenBkpt);
504
505 if (notTakenBkpt.address != 0)
506 clearTempBreakpoint(notTakenBkpt);
507 }
508
509 void
510 RemoteGDB::setSingleStep()
511 {
512 Addr pc = context->regs.pc;
513 Addr npc, bpc;
514 bool set_bt = false;
515
516 npc = pc + sizeof(MachInst);
517
518 // User was stopped at pc, e.g. the instruction at pc was not
519 // executed.
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
525 if (bpc != npc)
526 set_bt = true;
527 }
528
529 DPRINTF(GDBMisc, "setSingleStep bt_addr=%#x nt_addr=%#x\n",
530 takenBkpt.address, notTakenBkpt.address);
531
532 setTempBreakpoint(notTakenBkpt, npc);
533
534 if (set_bt)
535 setTempBreakpoint(takenBkpt, bpc);
536 }
537
538 /////////////////////////
539 //
540 //
541
542 uint8_t
543 RemoteGDB::getbyte()
544 {
545 uint8_t b;
546 ::read(fd, &b, 1);
547 return b;
548 }
549
550 void
551 RemoteGDB::putbyte(uint8_t b)
552 {
553 ::write(fd, &b, 1);
554 }
555
556 // Send a packet to gdb
557 void
558 RemoteGDB::send(const char *bp)
559 {
560 const char *p;
561 uint8_t csum, c;
562
563 DPRINTF(GDBSend, "send: %s\n", bp);
564
565 do {
566 p = bp;
567 putbyte(KGDB_START);
568 for (csum = 0; (c = *p); p++) {
569 putbyte(c);
570 csum += c;
571 }
572 putbyte(KGDB_END);
573 putbyte(i2digit(csum >> 4));
574 putbyte(i2digit(csum));
575 } while ((c = getbyte() & 0x7f) == KGDB_BADP);
576 }
577
578 // Receive a packet from gdb
579 int
580 RemoteGDB::recv(char *bp, int maxlen)
581 {
582 char *p;
583 int c, csum;
584 int len;
585
586 do {
587 p = bp;
588 csum = len = 0;
589 while ((c = getbyte()) != KGDB_START)
590 ;
591
592 while ((c = getbyte()) != KGDB_END && len < maxlen) {
593 c &= 0x7f;
594 csum += c;
595 *p++ = c;
596 len++;
597 }
598 csum &= 0xff;
599 *p = '\0';
600
601 if (len >= maxlen) {
602 putbyte(KGDB_BADP);
603 continue;
604 }
605
606 csum -= digit2i(getbyte()) * 16;
607 csum -= digit2i(getbyte());
608
609 if (csum == 0) {
610 putbyte(KGDB_GOODP);
611 // Sequence present?
612 if (bp[2] == ':') {
613 putbyte(bp[0]);
614 putbyte(bp[1]);
615 len -= 3;
616 bcopy(bp + 3, bp, len);
617 }
618 break;
619 }
620 putbyte(KGDB_BADP);
621 } while (1);
622
623 DPRINTF(GDBRecv, "recv: %s: %s\n", gdb_command(*bp), bp);
624
625 return (len);
626 }
627
628 // Read bytes from kernel address space for debugger.
629 bool
630 RemoteGDB::read(Addr vaddr, size_t size, char *data)
631 {
632 static Addr lastaddr = 0;
633 static size_t lastsize = 0;
634
635 uint8_t *maddr;
636
637 if (vaddr < 10) {
638 DPRINTF(GDBRead, "read: reading memory location zero!\n");
639 vaddr = lastaddr + lastsize;
640 }
641
642 DPRINTF(GDBRead, "read: addr=%#x, size=%d", vaddr, size);
643 #if TRACING_ON
644 char *d = data;
645 size_t s = size;
646 #endif
647
648 lastaddr = vaddr;
649 lastsize = size;
650
651 size_t count = min((Addr)size,
652 VMPageSize - (vaddr & (VMPageSize - 1)));
653
654 maddr = vtomem(context, vaddr, count);
655 memcpy(data, maddr, count);
656
657 vaddr += count;
658 data += count;
659 size -= count;
660
661 while (size >= VMPageSize) {
662 maddr = vtomem(context, vaddr, count);
663 memcpy(data, maddr, VMPageSize);
664
665 vaddr += VMPageSize;
666 data += VMPageSize;
667 size -= VMPageSize;
668 }
669
670 if (size > 0) {
671 maddr = vtomem(context, vaddr, count);
672 memcpy(data, maddr, size);
673 }
674
675 #if TRACING_ON
676 if (DTRACE(GDBRead)) {
677 if (DTRACE(GDBExtra)) {
678 char buf[1024];
679 mem2hex(buf, d, s);
680 DPRINTFNR(": %s\n", buf);
681 } else
682 DPRINTFNR("\n");
683 }
684 #endif
685
686 return true;
687 }
688
689 // Write bytes to kernel address space for debugger.
690 bool
691 RemoteGDB::write(Addr vaddr, size_t size, const char *data)
692 {
693 static Addr lastaddr = 0;
694 static size_t lastsize = 0;
695
696 uint8_t *maddr;
697
698 if (vaddr < 10) {
699 DPRINTF(GDBWrite, "write: writing memory location zero!\n");
700 vaddr = lastaddr + lastsize;
701 }
702
703 if (DTRACE(GDBWrite)) {
704 DPRINTFN("write: addr=%#x, size=%d", vaddr, size);
705 if (DTRACE(GDBExtra)) {
706 char buf[1024];
707 mem2hex(buf, data, size);
708 DPRINTFNR(": %s\n", buf);
709 } else
710 DPRINTFNR("\n");
711 }
712
713 lastaddr = vaddr;
714 lastsize = size;
715
716 size_t count = min((Addr)size,
717 VMPageSize - (vaddr & (VMPageSize - 1)));
718
719 maddr = vtomem(context, vaddr, count);
720 memcpy(maddr, data, count);
721
722 vaddr += count;
723 data += count;
724 size -= count;
725
726 while (size >= VMPageSize) {
727 maddr = vtomem(context, vaddr, count);
728 memcpy(maddr, data, VMPageSize);
729
730 vaddr += VMPageSize;
731 data += VMPageSize;
732 size -= VMPageSize;
733 }
734
735 if (size > 0) {
736 maddr = vtomem(context, vaddr, count);
737 memcpy(maddr, data, size);
738 }
739
740 #ifdef IMB
741 alpha_pal_imb();
742 #endif
743
744 return true;
745 }
746
747
748 PCEventQueue *RemoteGDB::getPcEventQueue()
749 {
750 return &system->pcEventQueue;
751 }
752
753
754 RemoteGDB::HardBreakpoint::HardBreakpoint(RemoteGDB *_gdb, Addr pc)
755 : PCEvent(_gdb->getPcEventQueue(), "HardBreakpoint Event", pc),
756 gdb(_gdb), refcount(0)
757 {
758 DPRINTF(GDBMisc, "creating hardware breakpoint at %#x\n", evpc);
759 }
760
761 void
762 RemoteGDB::HardBreakpoint::process(ExecContext *xc)
763 {
764 DPRINTF(GDBMisc, "handling hardware breakpoint at %#x\n", pc());
765
766 if (xc == gdb->context)
767 gdb->trap(ALPHA_KENTRY_INT);
768 }
769
770 bool
771 RemoteGDB::insertSoftBreak(Addr addr, size_t len)
772 {
773 if (len != sizeof(MachInst))
774 panic("invalid length\n");
775
776 return insertHardBreak(addr, len);
777 }
778
779 bool
780 RemoteGDB::removeSoftBreak(Addr addr, size_t len)
781 {
782 if (len != sizeof(MachInst))
783 panic("invalid length\n");
784
785 return removeHardBreak(addr, len);
786 }
787
788 bool
789 RemoteGDB::insertHardBreak(Addr addr, size_t len)
790 {
791 if (len != sizeof(MachInst))
792 panic("invalid length\n");
793
794 DPRINTF(GDBMisc, "inserting hardware breakpoint at %#x\n", addr);
795
796 HardBreakpoint *&bkpt = hardBreakMap[addr];
797 if (bkpt == 0)
798 bkpt = new HardBreakpoint(this, addr);
799
800 bkpt->refcount++;
801
802 return true;
803 }
804
805 bool
806 RemoteGDB::removeHardBreak(Addr addr, size_t len)
807 {
808 if (len != sizeof(MachInst))
809 panic("invalid length\n");
810
811 DPRINTF(GDBMisc, "removing hardware breakpoint at %#x\n", addr);
812
813 break_iter_t i = hardBreakMap.find(addr);
814 if (i == hardBreakMap.end())
815 return false;
816
817 HardBreakpoint *hbp = (*i).second;
818 if (--hbp->refcount == 0) {
819 delete hbp;
820 hardBreakMap.erase(i);
821 }
822
823 return true;
824 }
825
826 const char *
827 break_type(char c)
828 {
829 switch(c) {
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";
836 }
837 }
838
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.
844 bool
845 RemoteGDB::trap(int type)
846 {
847 uint64_t val;
848 size_t datalen, len;
849 char data[KGDB_BUFLEN + 1];
850 char buffer[sizeof(gdbregs) * 2 + 256];
851 char temp[KGDB_BUFLEN];
852 const char *p;
853 char command, subcmd;
854 string var;
855 bool ret;
856
857 if (!attached)
858 return false;
859
860 DPRINTF(GDBMisc, "trap: PC=%#x NPC=%#x\n",
861 context->regs.pc, context->regs.npc);
862
863 clearSingleStep();
864
865 /*
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.
869 *
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.
874 */
875 if (!active)
876 active = true;
877 else
878 // Tell remote host that an exception has occurred.
879 snprintf((char *)buffer, sizeof(buffer), "S%02x", signal(type));
880 send(buffer);
881
882 // Stick frame regs into our reg cache.
883 getregs();
884
885 for (;;) {
886 datalen = recv(data, sizeof(data));
887 data[sizeof(data) - 1] = 0; // Sentinel
888 command = data[0];
889 subcmd = 0;
890 p = data + 1;
891 switch (command) {
892
893 case KGDB_SIGNAL:
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));
898 send(buffer);
899 continue;
900
901 case KGDB_REG_R:
902 if (2 * sizeof(gdbregs) > sizeof(buffer))
903 panic("buffer too small");
904
905 mem2hex(buffer, gdbregs, sizeof(gdbregs));
906 send(buffer);
907 continue;
908
909 case KGDB_REG_W:
910 p = hex2mem(gdbregs, p, sizeof(gdbregs));
911 if (p == NULL || *p != '\0')
912 send("E01");
913 else {
914 setregs();
915 send("OK");
916 }
917 continue;
918
919 #if 0
920 case KGDB_SET_REG:
921 val = hex2i(&p);
922 if (*p++ != '=') {
923 send("E01");
924 continue;
925 }
926 if (val < 0 && val >= KGDB_NUMREGS) {
927 send("E01");
928 continue;
929 }
930
931 gdbregs[val] = hex2i(&p);
932 setregs();
933 send("OK");
934
935 continue;
936 #endif
937
938 case KGDB_MEM_R:
939 val = hex2i(&p);
940 if (*p++ != ',') {
941 send("E02");
942 continue;
943 }
944 len = hex2i(&p);
945 if (*p != '\0') {
946 send("E03");
947 continue;
948 }
949 if (len > sizeof(buffer)) {
950 send("E04");
951 continue;
952 }
953 if (!acc(val, len)) {
954 send("E05");
955 continue;
956 }
957
958 if (read(val, (size_t)len, (char *)buffer)) {
959 mem2hex(temp, buffer, len);
960 send(temp);
961 } else {
962 send("E05");
963 }
964 continue;
965
966 case KGDB_MEM_W:
967 val = hex2i(&p);
968 if (*p++ != ',') {
969 send("E06");
970 continue;
971 }
972 len = hex2i(&p);
973 if (*p++ != ':') {
974 send("E07");
975 continue;
976 }
977 if (len > datalen - (p - data)) {
978 send("E08");
979 continue;
980 }
981 p = hex2mem(buffer, p, sizeof(buffer));
982 if (p == NULL) {
983 send("E09");
984 continue;
985 }
986 if (!acc(val, len)) {
987 send("E0A");
988 continue;
989 }
990 if (write(val, (size_t)len, (char *)buffer))
991 send("OK");
992 else
993 send("E0B");
994 continue;
995
996 case KGDB_SET_THREAD:
997 subcmd = *p++;
998 val = hex2i(&p);
999 if (val == 0)
1000 send("OK");
1001 else
1002 send("E01");
1003 continue;
1004
1005 case KGDB_DETACH:
1006 case KGDB_KILL:
1007 active = false;
1008 clearSingleStep();
1009 detach();
1010 goto out;
1011
1012 case KGDB_ASYNC_CONT:
1013 subcmd = hex2i(&p);
1014 if (*p++ == ';') {
1015 val = hex2i(&p);
1016 context->regs.pc = val;
1017 context->regs.npc = val + sizeof(MachInst);
1018 }
1019 clearSingleStep();
1020 goto out;
1021
1022 case KGDB_CONT:
1023 if (p - data < datalen) {
1024 val = hex2i(&p);
1025 context->regs.pc = val;
1026 context->regs.npc = val + sizeof(MachInst);
1027 }
1028 clearSingleStep();
1029 goto out;
1030
1031 case KGDB_ASYNC_STEP:
1032 subcmd = hex2i(&p);
1033 if (*p++ == ';') {
1034 val = hex2i(&p);
1035 context->regs.pc = val;
1036 context->regs.npc = val + sizeof(MachInst);
1037 }
1038 setSingleStep();
1039 goto out;
1040
1041 case KGDB_STEP:
1042 if (p - data < datalen) {
1043 val = hex2i(&p);
1044 context->regs.pc = val;
1045 context->regs.npc = val + sizeof(MachInst);
1046 }
1047 setSingleStep();
1048 goto out;
1049
1050 case KGDB_CLR_HW_BKPT:
1051 subcmd = *p++;
1052 if (*p++ != ',') send("E0D");
1053 val = hex2i(&p);
1054 if (*p++ != ',') send("E0D");
1055 len = hex2i(&p);
1056
1057 DPRINTF(GDBMisc, "clear %s, addr=%#x, len=%d\n",
1058 break_type(subcmd), val, len);
1059
1060 ret = false;
1061
1062 switch (subcmd) {
1063 case '0': // software breakpoint
1064 ret = removeSoftBreak(val, len);
1065 break;
1066
1067 case '1': // hardware breakpoint
1068 ret = removeHardBreak(val, len);
1069 break;
1070
1071 case '2': // write watchpoint
1072 case '3': // read watchpoint
1073 case '4': // access watchpoint
1074 default: // unknown
1075 send("");
1076 break;
1077 }
1078
1079 send(ret ? "OK" : "E0C");
1080 continue;
1081
1082 case KGDB_SET_HW_BKPT:
1083 subcmd = *p++;
1084 if (*p++ != ',') send("E0D");
1085 val = hex2i(&p);
1086 if (*p++ != ',') send("E0D");
1087 len = hex2i(&p);
1088
1089 DPRINTF(GDBMisc, "set %s, addr=%#x, len=%d\n",
1090 break_type(subcmd), val, len);
1091
1092 ret = false;
1093
1094 switch (subcmd) {
1095 case '0': // software breakpoint
1096 ret = insertSoftBreak(val, len);
1097 break;
1098
1099 case '1': // hardware breakpoint
1100 ret = insertHardBreak(val, len);
1101 break;
1102
1103 case '2': // write watchpoint
1104 case '3': // read watchpoint
1105 case '4': // access watchpoint
1106 default: // unknown
1107 send("");
1108 break;
1109 }
1110
1111 send(ret ? "OK" : "E0C");
1112 continue;
1113
1114 case KGDB_QUERY_VAR:
1115 var = string(p, datalen - 1);
1116 if (var == "C")
1117 send("QC0");
1118 else
1119 send("");
1120 continue;
1121
1122 case KGDB_SET_BAUD:
1123 case KGDB_SET_BREAK:
1124 case KGDB_DEBUG:
1125 case KGDB_CYCLE_STEP:
1126 case KGDB_SIG_CYCLE_STEP:
1127 case KGDB_READ_REG:
1128 case KGDB_SET_VAR:
1129 case KGDB_RESET:
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);
1137 send("");
1138 continue;
1139
1140 default:
1141 // Unknown command.
1142 DPRINTF(GDBMisc, "Unknown command: %c(%#x)\n",
1143 command, command);
1144 send("");
1145 continue;
1146
1147
1148 }
1149 }
1150
1151 out:
1152 return true;
1153 }
1154
1155 // Convert a hex digit into an integer.
1156 // This returns -1 if the argument passed is no valid hex digit.
1157 int
1158 digit2i(char c)
1159 {
1160 if (c >= '0' && c <= '9')
1161 return (c - '0');
1162 else if (c >= 'a' && c <= 'f')
1163 return (c - 'a' + 10);
1164 else if (c >= 'A' && c <= 'F')
1165
1166 return (c - 'A' + 10);
1167 else
1168 return (-1);
1169 }
1170
1171 // Convert the low 4 bits of an integer into an hex digit.
1172 char
1173 i2digit(int n)
1174 {
1175 return ("0123456789abcdef"[n & 0x0f]);
1176 }
1177
1178 // Convert a byte array into an hex string.
1179 void
1180 mem2hex(void *vdst, const void *vsrc, int len)
1181 {
1182 char *dst = (char *)vdst;
1183 const char *src = (const char *)vsrc;
1184
1185 while (len--) {
1186 *dst++ = i2digit(*src >> 4);
1187 *dst++ = i2digit(*src++);
1188 }
1189 *dst = '\0';
1190 }
1191
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
1195 // returned.
1196 const char *
1197 hex2mem(void *vdst, const char *src, int maxlen)
1198 {
1199 char *dst = (char *)vdst;
1200 int msb, lsb;
1201
1202 while (*src && maxlen--) {
1203 msb = digit2i(*src++);
1204 if (msb < 0)
1205 return (src - 1);
1206 lsb = digit2i(*src++);
1207 if (lsb < 0)
1208 return (NULL);
1209 *dst++ = (msb << 4) | lsb;
1210 }
1211 return (src);
1212 }
1213
1214 // Convert an hex string into an integer.
1215 // This returns a pointer to the character following the last valid
1216 // hex digit.
1217 Addr
1218 hex2i(const char **srcp)
1219 {
1220 const char *src = *srcp;
1221 Addr r = 0;
1222 int nibble;
1223
1224 while ((nibble = digit2i(*src)) >= 0) {
1225 r *= 16;
1226 r += nibble;
1227 src++;
1228 }
1229 *srcp = src;
1230 return (r);
1231 }
1232