1/*
2 * Copyright (c) 2018 ARM Limited
3 *
4 * The license below extends only to copyright in the software and shall
5 * not be construed as granting a license to any other intellectual
6 * property including but not limited to intellectual property relating
7 * to a hardware implementation of the functionality of the software
8 * licensed hereunder.  You may use the software subject to the license
9 * terms below provided that you ensure that this notice is replicated
10 * unmodified and in its entirety in all distributions of the software,
11 * modified or unmodified, in source code or in binary form.
12 *
13 * Copyright 2015 LabWare
14 * Copyright 2014 Google, Inc.
15 * Copyright (c) 2002-2005 The Regents of The University of Michigan
16 * All rights reserved.
17 *
18 * Redistribution and use in source and binary forms, with or without
19 * modification, are permitted provided that the following conditions are
20 * met: redistributions of source code must retain the above copyright
21 * notice, this list of conditions and the following disclaimer;
22 * redistributions in binary form must reproduce the above copyright
23 * notice, this list of conditions and the following disclaimer in the
24 * documentation and/or other materials provided with the distribution;
25 * neither the name of the copyright holders nor the names of its
26 * contributors may be used to endorse or promote products derived from
27 * this software without specific prior written permission.
28 *
29 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
30 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
31 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
32 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
33 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
34 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
35 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
36 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
37 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
38 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
39 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40 *
41 * Authors: Nathan Binkert
42 *          Boris Shingarov
43 */
44
45/*
46 * Copyright (c) 1990, 1993 The Regents of the University of California
47 * All rights reserved
48 *
49 * This software was developed by the Computer Systems Engineering group
50 * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
51 * contributed to Berkeley.
52 *
53 * All advertising materials mentioning features or use of this software
54 * must display the following acknowledgement:
55 *      This product includes software developed by the University of
56 *      California, Lawrence Berkeley Laboratories.
57 *
58 * Redistribution and use in source and binary forms, with or without
59 * modification, are permitted provided that the following conditions
60 * are met:
61 * 1. Redistributions of source code must retain the above copyright
62 *    notice, this list of conditions and the following disclaimer.
63 * 2. Redistributions in binary form must reproduce the above copyright
64 *    notice, this list of conditions and the following disclaimer in the
65 *    documentation and/or other materials provided with the distribution.
66 * 3. All advertising materials mentioning features or use of this software
67 *    must display the following acknowledgement:
68 *      This product includes software developed by the University of
69 *      California, Berkeley and its contributors.
70 * 4. Neither the name of the University nor the names of its contributors
71 *    may be used to endorse or promote products derived from this software
72 *    without specific prior written permission.
73 *
74 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
75 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
76 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
77 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
78 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
79 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
80 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
81 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
82 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
83 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
84 * SUCH DAMAGE.
85 *
86 *      @(#)kgdb_stub.c 8.4 (Berkeley) 1/12/94
87 */
88
89/*-
90 * Copyright (c) 2001 The NetBSD Foundation, Inc.
91 * All rights reserved.
92 *
93 * This code is derived from software contributed to The NetBSD Foundation
94 * by Jason R. Thorpe.
95 *
96 * Redistribution and use in source and binary forms, with or without
97 * modification, are permitted provided that the following conditions
98 * are met:
99 * 1. Redistributions of source code must retain the above copyright
100 *    notice, this list of conditions and the following disclaimer.
101 * 2. Redistributions in binary form must reproduce the above copyright
102 *    notice, this list of conditions and the following disclaimer in the
103 *    documentation and/or other materials provided with the distribution.
104 * 3. All advertising materials mentioning features or use of this software
105 *    must display the following acknowledgement:
106 *      This product includes software developed by the NetBSD
107 *      Foundation, Inc. and its contributors.
108 * 4. Neither the name of The NetBSD Foundation nor the names of its
109 *    contributors may be used to endorse or promote products derived
110 *    from this software without specific prior written permission.
111 *
112 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
113 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
114 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
115 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
116 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
117 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
118 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
119 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
120 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
121 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
122 * POSSIBILITY OF SUCH DAMAGE.
123 */
124
125/*
126 * $NetBSD: kgdb_stub.c,v 1.8 2001/07/07 22:58:00 wdk Exp $
127 *
128 * Taken from NetBSD
129 *
130 * "Stub" to allow remote cpu to debug over a serial line using gdb.
131 */
132
133#include "base/remote_gdb.hh"
134
135#include <sys/signal.h>
136#include <unistd.h>
137
138#include <csignal>
139#include <cstdint>
140#include <cstdio>
141#include <sstream>
142#include <string>
143
144#include "arch/vtophys.hh"
145#include "base/intmath.hh"
146#include "base/socket.hh"
147#include "base/trace.hh"
148#include "config/the_isa.hh"
149#include "cpu/base.hh"
150#include "cpu/static_inst.hh"
151#include "cpu/thread_context.hh"
152#include "debug/GDBAll.hh"
153#include "mem/fs_translating_port_proxy.hh"
154#include "mem/port.hh"
155#include "mem/se_translating_port_proxy.hh"
156#include "sim/full_system.hh"
157#include "sim/system.hh"
158
159using namespace std;
160using namespace TheISA;
161
162static const char GDBStart = '$';
163static const char GDBEnd = '#';
164static const char GDBGoodP = '+';
165static const char GDBBadP = '-';
166
167vector<BaseRemoteGDB *> debuggers;
168
169class HardBreakpoint : public PCEvent
170{
171  private:
172    BaseRemoteGDB *gdb;
173
174  public:
175    int refcount;
176
177  public:
178    HardBreakpoint(BaseRemoteGDB *_gdb, PCEventQueue *q, Addr pc)
179        : PCEvent(q, "HardBreakpoint Event", pc),
180          gdb(_gdb), refcount(0)
181    {
182        DPRINTF(GDBMisc, "creating hardware breakpoint at %#x\n", evpc);
183    }
184
185    const std::string name() const override { return gdb->name() + ".hwbkpt"; }
186
187    void
188    process(ThreadContext *tc) override
189    {
190        DPRINTF(GDBMisc, "handling hardware breakpoint at %#x\n", pc());
191
192        if (tc == gdb->tc)
193            gdb->trap(SIGTRAP);
194    }
195};
196
197namespace {
198
199// Exception to throw when the connection to the client is broken.
200struct BadClient
201{
202    const char *warning;
203    BadClient(const char *_warning=NULL) : warning(_warning)
204    {}
205};
206
207// Exception to throw when an error needs to be reported to the client.
208struct CmdError
209{
210    string error;
211    CmdError(std::string _error) : error(_error)
212    {}
213};
214
215// Exception to throw when something isn't supported.
216class Unsupported {};
217
218// Convert a hex digit into an integer.
219// This returns -1 if the argument passed is no valid hex digit.
220int
221digit2i(char c)
222{
223    if (c >= '0' && c <= '9')
224        return (c - '0');
225    else if (c >= 'a' && c <= 'f')
226        return (c - 'a' + 10);
227    else if (c >= 'A' && c <= 'F')
228        return (c - 'A' + 10);
229    else
230        return (-1);
231}
232
233// Convert the low 4 bits of an integer into an hex digit.
234char
235i2digit(int n)
236{
237    return ("0123456789abcdef"[n & 0x0f]);
238}
239
240// Convert a byte array into an hex string.
241void
242mem2hex(char *vdst, const char *vsrc, int len)
243{
244    char *dst = vdst;
245    const char *src = vsrc;
246
247    while (len--) {
248        *dst++ = i2digit(*src >> 4);
249        *dst++ = i2digit(*src++);
250    }
251    *dst = '\0';
252}
253
254// Convert an hex string into a byte array.
255// This returns a pointer to the character following the last valid
256// hex digit. If the string ends in the middle of a byte, NULL is
257// returned.
258const char *
259hex2mem(char *vdst, const char *src, int maxlen)
260{
261    char *dst = vdst;
262    int msb, lsb;
263
264    while (*src && maxlen--) {
265        msb = digit2i(*src++);
266        if (msb < 0)
267            return (src - 1);
268        lsb = digit2i(*src++);
269        if (lsb < 0)
270            return (NULL);
271        *dst++ = (msb << 4) | lsb;
272    }
273    return src;
274}
275
276// Convert an hex string into an integer.
277// This returns a pointer to the character following the last valid
278// hex digit.
279Addr
280hex2i(const char **srcp)
281{
282    const char *src = *srcp;
283    Addr r = 0;
284    int nibble;
285
286    while ((nibble = digit2i(*src)) >= 0) {
287        r *= 16;
288        r += nibble;
289        src++;
290    }
291    *srcp = src;
292    return r;
293}
294
295enum GdbBreakpointType {
296    GdbSoftBp = '0',
297    GdbHardBp = '1',
298    GdbWriteWp = '2',
299    GdbReadWp = '3',
300    GdbAccWp = '4',
301};
302
303#ifndef NDEBUG
304const char *
305break_type(char c)
306{
307    switch(c) {
308      case GdbSoftBp: return "software breakpoint";
309      case GdbHardBp: return "hardware breakpoint";
310      case GdbWriteWp: return "write watchpoint";
311      case GdbReadWp: return "read watchpoint";
312      case GdbAccWp: return "access watchpoint";
313      default: return "unknown breakpoint/watchpoint";
314    }
315}
316#endif
317
318std::map<Addr, HardBreakpoint *> hardBreakMap;
319
320EventQueue *
321getComInstEventQueue(ThreadContext *tc)
322{
323    return tc->getCpuPtr()->comInstEventQueue[tc->threadId()];
324}
325
326}
327
328BaseRemoteGDB::BaseRemoteGDB(System *_system, ThreadContext *c, int _port) :
329        connectEvent(nullptr), dataEvent(nullptr), _port(_port), fd(-1),
330        active(false), attached(false), sys(_system), tc(c),
331        trapEvent(this), singleStepEvent(*this)
332{
333    debuggers.push_back(this);
334}
335
336BaseRemoteGDB::~BaseRemoteGDB()
337{
338    delete connectEvent;
339    delete dataEvent;
340}
341
342string
343BaseRemoteGDB::name()
344{
345    return sys->name() + ".remote_gdb";
346}
347
348void
349BaseRemoteGDB::listen()
350{
351    if (ListenSocket::allDisabled()) {
352        warn_once("Sockets disabled, not accepting gdb connections");
353        return;
354    }
355
356    while (!listener.listen(_port, true)) {
357        DPRINTF(GDBMisc, "Can't bind port %d\n", _port);
358        _port++;
359    }
360
361    connectEvent = new ConnectEvent(this, listener.getfd(), POLLIN);
362    pollQueue.schedule(connectEvent);
363
364    ccprintf(cerr, "%d: %s: listening for remote gdb on port %d\n",
365             curTick(), name(), _port);
366}
367
368void
369BaseRemoteGDB::connect()
370{
371    panic_if(!listener.islistening(),
372             "Cannot accept GDB connections if we're not listening!");
373
374    int sfd = listener.accept(true);
375
376    if (sfd != -1) {
377        if (isAttached())
378            close(sfd);
379        else
380            attach(sfd);
381    }
382}
383
384int
385BaseRemoteGDB::port() const
386{
387    panic_if(!listener.islistening(),
388             "Remote GDB port is unknown until listen() has been called.\n");
389    return _port;
390}
391
392void
393BaseRemoteGDB::attach(int f)
394{
395    fd = f;
396
397    dataEvent = new DataEvent(this, fd, POLLIN);
398    pollQueue.schedule(dataEvent);
399
400    attached = true;
401    DPRINTFN("remote gdb attached\n");
402}
403
404void
405BaseRemoteGDB::detach()
406{
407    attached = false;
408    active = false;
409    clearSingleStep();
410    close(fd);
411    fd = -1;
412
413    pollQueue.remove(dataEvent);
414    DPRINTFN("remote gdb detached\n");
415}
416
417// This function does all command processing for interfacing to a
418// remote gdb.  Note that the error codes are ignored by gdb at
419// present, but might eventually become meaningful. (XXX) It might
420// makes sense to use POSIX errno values, because that is what the
421// gdb/remote.c functions want to return.
422bool
423BaseRemoteGDB::trap(int type)
424{
425
426    if (!attached)
427        return false;
428
429    DPRINTF(GDBMisc, "trap: PC=%s\n", tc->pcState());
430
431    clearSingleStep();
432
433    /*
434     * The first entry to this function is normally through
435     * a breakpoint trap in kgdb_connect(), in which case we
436     * must advance past the breakpoint because gdb will not.
437     *
438     * On the first entry here, we expect that gdb is not yet
439     * listening to us, so just enter the interaction loop.
440     * After the debugger is "active" (connected) it will be
441     * waiting for a "signaled" message from us.
442     */
443    if (!active) {
444        active = true;
445    } else {
446        // Tell remote host that an exception has occurred.
447        send(csprintf("S%02x", type).c_str());
448    }
449
450    // Stick frame regs into our reg cache.
451    regCachePtr = gdbRegs();
452    regCachePtr->getRegs(tc);
453
454    GdbCommand::Context cmdCtx;
455    cmdCtx.type = type;
456    std::vector<char> data;
457
458    for (;;) {
459        try {
460            recv(data);
461            if (data.size() == 1)
462                throw BadClient();
463            cmdCtx.cmd_byte = data[0];
464            cmdCtx.data = data.data() + 1;
465            // One for sentinel, one for cmd_byte.
466            cmdCtx.len = data.size() - 2;
467
468            auto cmdIt = command_map.find(cmdCtx.cmd_byte);
469            if (cmdIt == command_map.end()) {
470                DPRINTF(GDBMisc, "Unknown command: %c(%#x)\n",
471                        cmdCtx.cmd_byte, cmdCtx.cmd_byte);
472                throw Unsupported();
473            }
474            cmdCtx.cmd = &(cmdIt->second);
475
476            if (!(this->*(cmdCtx.cmd->func))(cmdCtx))
477                break;
478
479        } catch (BadClient &e) {
480            if (e.warning)
481                warn(e.warning);
482            detach();
483            break;
484        } catch (Unsupported &e) {
485            send("");
486        } catch (CmdError &e) {
487            send(e.error.c_str());
488        } catch (...) {
489            panic("Unrecognzied GDB exception.");
490        }
491    }
492
493    return true;
494}
495
496void
497BaseRemoteGDB::incomingData(int revent)
498{
499    if (trapEvent.scheduled()) {
500        warn("GDB trap event has already been scheduled!");
501        return;
502    }
503
504    if (revent & POLLIN) {
505        trapEvent.type(SIGILL);
506        scheduleInstCommitEvent(&trapEvent, 0);
507    } else if (revent & POLLNVAL) {
508        descheduleInstCommitEvent(&trapEvent);
509        detach();
510    }
511}
512
513uint8_t
514BaseRemoteGDB::getbyte()
515{
516    uint8_t b;
517    if (::read(fd, &b, sizeof(b)) == sizeof(b))
518        return b;
519
520    throw BadClient("Couldn't read data from debugger.");
521}
522
523void
524BaseRemoteGDB::putbyte(uint8_t b)
525{
526    if (::write(fd, &b, sizeof(b)) == sizeof(b))
527        return;
528
529    throw BadClient("Couldn't write data to the debugger.");
530}
531
532// Receive a packet from gdb
533void
534BaseRemoteGDB::recv(std::vector<char>& bp)
535{
536    uint8_t c;
537    int csum;
538    bp.resize(0);
539
540    do {
541        csum = 0;
542        // Find the beginning of a packet
543        while ((c = getbyte()) != GDBStart);
544
545        // Read until you find the end of the data in the packet, and keep
546        // track of the check sum.
547        while (true) {
548            c = getbyte();
549            if (c == GDBEnd)
550                break;
551            c &= 0x7f;
552            csum += c;
553            bp.push_back(c);
554        }
555
556        // Mask the check sum.
557        csum &= 0xff;
558
559        // Bring in the checksum. If the check sum matches, csum will be 0.
560        csum -= digit2i(getbyte()) * 16;
561        csum -= digit2i(getbyte());
562
563        // If the check sum was correct
564        if (csum == 0) {
565            // Report that the packet was received correctly
566            putbyte(GDBGoodP);
567            // Sequence present?
568            if (bp.size() > 2 && bp[2] == ':') {
569                putbyte(bp[0]);
570                putbyte(bp[1]);
571                auto begin = std::begin(bp);
572                bp.erase(begin, std::next(begin, 3));
573            }
574            break;
575        }
576        // Otherwise, report that there was a mistake.
577        putbyte(GDBBadP);
578    } while (1);
579    // Sentinel.
580    bp.push_back('\0');
581    DPRINTF(GDBRecv, "recv:  %s\n", bp.data());
582}
583
584// Send a packet to gdb
585void
586BaseRemoteGDB::send(const char *bp)
587{
588    const char *p;
589    uint8_t csum, c;
590
591    DPRINTF(GDBSend, "send:  %s\n", bp);
592
593    do {
594        p = bp;
595        // Start sending a packet
596        putbyte(GDBStart);
597        // Send the contents, and also keep a check sum.
598        for (csum = 0; (c = *p); p++) {
599            putbyte(c);
600            csum += c;
601        }
602        // Send the ending character.
603        putbyte(GDBEnd);
604        // Send the checksum.
605        putbyte(i2digit(csum >> 4));
606        putbyte(i2digit(csum));
607        // Try transmitting over and over again until the other end doesn't
608        // send an error back.
609        c = getbyte();
610    } while ((c & 0x7f) == GDBBadP);
611}
612
613// Read bytes from kernel address space for debugger.
614bool
615BaseRemoteGDB::read(Addr vaddr, size_t size, char *data)
616{
617    static Addr lastaddr = 0;
618    static size_t lastsize = 0;
619
620    if (vaddr < 10) {
621      DPRINTF(GDBRead, "read:  reading memory location zero!\n");
622      vaddr = lastaddr + lastsize;
623    }
624
625    DPRINTF(GDBRead, "read:  addr=%#x, size=%d", vaddr, size);
626
627    PortProxy &proxy = tc->getVirtProxy();
628    proxy.readBlob(vaddr, data, size);
629
630#if TRACING_ON
631    if (DTRACE(GDBRead)) {
632        if (DTRACE(GDBExtra)) {
633            char buf[1024];
634            mem2hex(buf, data, size);
635            DPRINTFNR(": %s\n", buf);
636        } else
637            DPRINTFNR("\n");
638    }
639#endif
640
641    return true;
642}
643
644// Write bytes to kernel address space for debugger.
645bool
646BaseRemoteGDB::write(Addr vaddr, size_t size, const char *data)
647{
648    static Addr lastaddr = 0;
649    static size_t lastsize = 0;
650
651    if (vaddr < 10) {
652      DPRINTF(GDBWrite, "write: writing memory location zero!\n");
653      vaddr = lastaddr + lastsize;
654    }
655
656    if (DTRACE(GDBWrite)) {
657        DPRINTFN("write: addr=%#x, size=%d", vaddr, size);
658        if (DTRACE(GDBExtra)) {
659            char buf[1024];
660            mem2hex(buf, data, size);
661            DPRINTFNR(": %s\n", buf);
662        } else
663            DPRINTFNR("\n");
664    }
665    PortProxy &proxy = tc->getVirtProxy();
666    proxy.writeBlob(vaddr, data, size);
667
668    return true;
669}
670
671void
672BaseRemoteGDB::singleStep()
673{
674    if (!singleStepEvent.scheduled())
675        scheduleInstCommitEvent(&singleStepEvent, 1);
676    trap(SIGTRAP);
677}
678
679void
680BaseRemoteGDB::clearSingleStep()
681{
682    descheduleInstCommitEvent(&singleStepEvent);
683}
684
685void
686BaseRemoteGDB::setSingleStep()
687{
688    if (!singleStepEvent.scheduled())
689        scheduleInstCommitEvent(&singleStepEvent, 1);
690}
691
692void
693BaseRemoteGDB::insertSoftBreak(Addr addr, size_t len)
694{
695    if (!checkBpLen(len))
696        throw BadClient("Invalid breakpoint length\n");
697
698    return insertHardBreak(addr, len);
699}
700
701void
702BaseRemoteGDB::removeSoftBreak(Addr addr, size_t len)
703{
704    if (!checkBpLen(len))
705        throw BadClient("Invalid breakpoint length.\n");
706
707    return removeHardBreak(addr, len);
708}
709
710void
711BaseRemoteGDB::insertHardBreak(Addr addr, size_t len)
712{
713    if (!checkBpLen(len))
714        throw BadClient("Invalid breakpoint length\n");
715
716    DPRINTF(GDBMisc, "Inserting hardware breakpoint at %#x\n", addr);
717
718    HardBreakpoint *&bkpt = hardBreakMap[addr];
719    if (bkpt == 0)
720        bkpt = new HardBreakpoint(this, &sys->pcEventQueue, addr);
721
722    bkpt->refcount++;
723}
724
725void
726BaseRemoteGDB::removeHardBreak(Addr addr, size_t len)
727{
728    if (!checkBpLen(len))
729        throw BadClient("Invalid breakpoint length\n");
730
731    DPRINTF(GDBMisc, "Removing hardware breakpoint at %#x\n", addr);
732
733    auto i = hardBreakMap.find(addr);
734    if (i == hardBreakMap.end())
735        throw CmdError("E0C");
736
737    HardBreakpoint *hbp = (*i).second;
738    if (--hbp->refcount == 0) {
739        delete hbp;
740        hardBreakMap.erase(i);
741    }
742}
743
744void
745BaseRemoteGDB::clearTempBreakpoint(Addr &bkpt)
746{
747    DPRINTF(GDBMisc, "setTempBreakpoint: addr=%#x\n", bkpt);
748    removeHardBreak(bkpt, sizeof(TheISA::MachInst));
749    bkpt = 0;
750}
751
752void
753BaseRemoteGDB::setTempBreakpoint(Addr bkpt)
754{
755    DPRINTF(GDBMisc, "setTempBreakpoint: addr=%#x\n", bkpt);
756    insertHardBreak(bkpt, sizeof(TheISA::MachInst));
757}
758
759void
760BaseRemoteGDB::scheduleInstCommitEvent(Event *ev, int delta)
761{
762    EventQueue *eq = getComInstEventQueue(tc);
763    // Here "ticks" aren't simulator ticks which measure time, they're
764    // instructions committed by the CPU.
765    eq->schedule(ev, eq->getCurTick() + delta);
766}
767
768void
769BaseRemoteGDB::descheduleInstCommitEvent(Event *ev)
770{
771    if (ev->scheduled())
772        getComInstEventQueue(tc)->deschedule(ev);
773}
774
775std::map<char, BaseRemoteGDB::GdbCommand> BaseRemoteGDB::command_map = {
776    // last signal
777    { '?', { "KGDB_SIGNAL", &BaseRemoteGDB::cmd_signal } },
778    // set baud (deprecated)
779    { 'b', { "KGDB_SET_BAUD", &BaseRemoteGDB::cmd_unsupported } },
780    // set breakpoint (deprecated)
781    { 'B', { "KGDB_SET_BREAK", &BaseRemoteGDB::cmd_unsupported } },
782    // resume
783    { 'c', { "KGDB_CONT", &BaseRemoteGDB::cmd_cont } },
784    // continue with signal
785    { 'C', { "KGDB_ASYNC_CONT", &BaseRemoteGDB::cmd_async_cont } },
786    // toggle debug flags (deprecated)
787    { 'd', { "KGDB_DEBUG", &BaseRemoteGDB::cmd_unsupported } },
788    // detach remote gdb
789    { 'D', { "KGDB_DETACH", &BaseRemoteGDB::cmd_detach } },
790    // read general registers
791    { 'g', { "KGDB_REG_R", &BaseRemoteGDB::cmd_reg_r } },
792    // write general registers
793    { 'G', { "KGDB_REG_W", &BaseRemoteGDB::cmd_reg_w } },
794    // set thread
795    { 'H', { "KGDB_SET_THREAD", &BaseRemoteGDB::cmd_set_thread } },
796    // step a single cycle
797    { 'i', { "KGDB_CYCLE_STEP", &BaseRemoteGDB::cmd_unsupported } },
798    // signal then cycle step
799    { 'I', { "KGDB_SIG_CYCLE_STEP", &BaseRemoteGDB::cmd_unsupported } },
800    // kill program
801    { 'k', { "KGDB_KILL", &BaseRemoteGDB::cmd_detach } },
802    // read memory
803    { 'm', { "KGDB_MEM_R", &BaseRemoteGDB::cmd_mem_r } },
804    // write memory
805    { 'M', { "KGDB_MEM_W", &BaseRemoteGDB::cmd_mem_w } },
806    // read register
807    { 'p', { "KGDB_READ_REG", &BaseRemoteGDB::cmd_unsupported } },
808    // write register
809    { 'P', { "KGDB_SET_REG", &BaseRemoteGDB::cmd_unsupported } },
810    // query variable
811    { 'q', { "KGDB_QUERY_VAR", &BaseRemoteGDB::cmd_query_var } },
812    // set variable
813    { 'Q', { "KGDB_SET_VAR", &BaseRemoteGDB::cmd_unsupported } },
814    // reset system (deprecated)
815    { 'r', { "KGDB_RESET", &BaseRemoteGDB::cmd_unsupported } },
816    // step
817    { 's', { "KGDB_STEP", &BaseRemoteGDB::cmd_step } },
818    // signal and step
819    { 'S', { "KGDB_ASYNC_STEP", &BaseRemoteGDB::cmd_async_step } },
820    // find out if the thread is alive
821    { 'T', { "KGDB_THREAD_ALIVE", &BaseRemoteGDB::cmd_unsupported } },
822    // target exited
823    { 'W', { "KGDB_TARGET_EXIT", &BaseRemoteGDB::cmd_unsupported } },
824    // write memory
825    { 'X', { "KGDB_BINARY_DLOAD", &BaseRemoteGDB::cmd_unsupported } },
826    // remove breakpoint or watchpoint
827    { 'z', { "KGDB_CLR_HW_BKPT", &BaseRemoteGDB::cmd_clr_hw_bkpt } },
828    // insert breakpoint or watchpoint
829    { 'Z', { "KGDB_SET_HW_BKPT", &BaseRemoteGDB::cmd_set_hw_bkpt } },
830};
831
832bool
833BaseRemoteGDB::checkBpLen(size_t len)
834{
835    return len == sizeof(MachInst);
836}
837
838bool
839BaseRemoteGDB::cmd_unsupported(GdbCommand::Context &ctx)
840{
841    DPRINTF(GDBMisc, "Unsupported command: %s\n", ctx.cmd->name);
842    DDUMP(GDBMisc, ctx.data, ctx.len);
843    throw Unsupported();
844}
845
846
847bool
848BaseRemoteGDB::cmd_signal(GdbCommand::Context &ctx)
849{
850    send(csprintf("S%02x", ctx.type).c_str());
851    return true;
852}
853
854bool
855BaseRemoteGDB::cmd_cont(GdbCommand::Context &ctx)
856{
857    const char *p = ctx.data;
858    if (ctx.len) {
859        Addr newPc = hex2i(&p);
860        tc->pcState(newPc);
861    }
862    clearSingleStep();
863    return false;
864}
865
866bool
867BaseRemoteGDB::cmd_async_cont(GdbCommand::Context &ctx)
868{
869    const char *p = ctx.data;
870    hex2i(&p);
871    if (*p++ == ';') {
872        Addr newPc = hex2i(&p);
873        tc->pcState(newPc);
874    }
875    clearSingleStep();
876    return false;
877}
878
879bool
880BaseRemoteGDB::cmd_detach(GdbCommand::Context &ctx)
881{
882    detach();
883    return false;
884}
885
886bool
887BaseRemoteGDB::cmd_reg_r(GdbCommand::Context &ctx)
888{
889    char buf[2 * regCachePtr->size() + 1];
890    buf[2 * regCachePtr->size()] = '\0';
891    mem2hex(buf, regCachePtr->data(), regCachePtr->size());
892    send(buf);
893    return true;
894}
895
896bool
897BaseRemoteGDB::cmd_reg_w(GdbCommand::Context &ctx)
898{
899    const char *p = ctx.data;
900    p = hex2mem(regCachePtr->data(), p, regCachePtr->size());
901    if (p == NULL || *p != '\0')
902        throw CmdError("E01");
903
904    regCachePtr->setRegs(tc);
905    send("OK");
906
907    return true;
908}
909
910bool
911BaseRemoteGDB::cmd_set_thread(GdbCommand::Context &ctx)
912{
913    const char *p = ctx.data + 1; // Ignore the subcommand byte.
914    if (hex2i(&p) != 0)
915        throw CmdError("E01");
916    send("OK");
917    return true;
918}
919
920bool
921BaseRemoteGDB::cmd_mem_r(GdbCommand::Context &ctx)
922{
923    const char *p = ctx.data;
924    Addr addr = hex2i(&p);
925    if (*p++ != ',')
926        throw CmdError("E02");
927    size_t len = hex2i(&p);
928    if (*p != '\0')
929        throw CmdError("E03");
930    if (!acc(addr, len))
931        throw CmdError("E05");
932
933    char buf[len];
934    if (!read(addr, len, buf))
935        throw CmdError("E05");
936
937    char temp[2 * len + 1];
938    temp[2 * len] = '\0';
939    mem2hex(temp, buf, len);
940    send(temp);
941    return true;
942}
943
944bool
945BaseRemoteGDB::cmd_mem_w(GdbCommand::Context &ctx)
946{
947    const char *p = ctx.data;
948    Addr addr = hex2i(&p);
949    if (*p++ != ',')
950        throw CmdError("E06");
951    size_t len = hex2i(&p);
952    if (*p++ != ':')
953        throw CmdError("E07");
954    if (len * 2 > ctx.len - (p - ctx.data))
955        throw CmdError("E08");
956    char buf[len];
957    p = (char *)hex2mem(buf, p, len);
958    if (p == NULL)
959        throw CmdError("E09");
960    if (!acc(addr, len))
961        throw CmdError("E0A");
962    if (!write(addr, len, buf))
963        throw CmdError("E0B");
964    send("OK");
965    return true;
966}
967
968bool
969BaseRemoteGDB::cmd_query_var(GdbCommand::Context &ctx)
970{
971    string s(ctx.data, ctx.len - 1);
972    string xfer_read_prefix = "Xfer:features:read:";
973    if (s.rfind("Supported:", 0) == 0) {
974        std::ostringstream oss;
975        // This reply field mandatory. We can receive arbitrarily
976        // long packets, so we could choose it to be arbitrarily large.
977        // This is just an arbitrary filler value that seems to work.
978        oss << "PacketSize=1024";
979        for (const auto& feature : availableFeatures())
980            oss << ';' << feature;
981        send(oss.str().c_str());
982    } else if (s.rfind(xfer_read_prefix, 0) == 0) {
983        size_t offset, length;
984        auto value_string = s.substr(xfer_read_prefix.length());
985        auto colon_pos = value_string.find(':');
986        auto comma_pos = value_string.find(',');
987        if (colon_pos == std::string::npos || comma_pos == std::string::npos)
988            throw CmdError("E00");
989        std::string annex;
990        if (!getXferFeaturesRead(value_string.substr(0, colon_pos), annex))
991            throw CmdError("E00");
992        try {
993            offset = std::stoull(
994                value_string.substr(colon_pos + 1, comma_pos), NULL, 16);
995            length = std::stoull(
996                value_string.substr(comma_pos + 1), NULL, 16);
997        } catch (std::invalid_argument& e) {
998            throw CmdError("E00");
999        } catch (std::out_of_range& e) {
1000            throw CmdError("E00");
1001        }
1002        std::string encoded;
1003        encodeXferResponse(annex, encoded, offset, length);
1004        send(encoded.c_str());
1005    } else if (s == "C") {
1006        send("QC0");
1007    } else {
1008        throw Unsupported();
1009    }
1010    return true;
1011}
1012
1013std::vector<std::string>
1014BaseRemoteGDB::availableFeatures() const
1015{
1016    return {};
1017};
1018
1019bool
1020BaseRemoteGDB::getXferFeaturesRead(
1021    const std::string &annex, std::string &output)
1022{
1023    return false;
1024}
1025
1026void
1027BaseRemoteGDB::encodeBinaryData(
1028    const std::string &unencoded, std::string &encoded) const
1029{
1030    for (const char& c : unencoded) {
1031        if (c == '$' || c == '#' || c == '}' || c == '*') {
1032            encoded += '}';
1033            encoded += c ^ 0x20;
1034        } else {
1035            encoded += c;
1036        }
1037    }
1038}
1039
1040void
1041BaseRemoteGDB::encodeXferResponse(const std::string &unencoded,
1042    std::string &encoded, size_t offset, size_t unencoded_length) const
1043{
1044    if (offset + unencoded_length < unencoded.length())
1045        encoded += 'm';
1046    else
1047        encoded += 'l';
1048    encodeBinaryData(unencoded.substr(offset, unencoded_length), encoded);
1049}
1050
1051bool
1052BaseRemoteGDB::cmd_async_step(GdbCommand::Context &ctx)
1053{
1054    const char *p = ctx.data;
1055    hex2i(&p); // Ignore the subcommand byte.
1056    if (*p++ == ';') {
1057        Addr newPc = hex2i(&p);
1058        tc->pcState(newPc);
1059    }
1060    setSingleStep();
1061    return false;
1062}
1063
1064bool
1065BaseRemoteGDB::cmd_step(GdbCommand::Context &ctx)
1066{
1067    if (ctx.len) {
1068        const char *p = ctx.data;
1069        Addr newPc = hex2i(&p);
1070        tc->pcState(newPc);
1071    }
1072    setSingleStep();
1073    return false;
1074}
1075
1076bool
1077BaseRemoteGDB::cmd_clr_hw_bkpt(GdbCommand::Context &ctx)
1078{
1079    const char *p = ctx.data;
1080    char subcmd = *p++;
1081    if (*p++ != ',')
1082        throw CmdError("E0D");
1083    Addr addr = hex2i(&p);
1084    if (*p++ != ',')
1085        throw CmdError("E0D");
1086    size_t len = hex2i(&p);
1087
1088    DPRINTF(GDBMisc, "clear %s, addr=%#x, len=%d\n",
1089            break_type(subcmd), addr, len);
1090
1091    switch (subcmd) {
1092      case GdbSoftBp:
1093        removeSoftBreak(addr, len);
1094        break;
1095      case GdbHardBp:
1096        removeHardBreak(addr, len);
1097        break;
1098      case GdbWriteWp:
1099      case GdbReadWp:
1100      case GdbAccWp:
1101      default: // unknown
1102        throw Unsupported();
1103    }
1104    send("OK");
1105
1106    return true;
1107}
1108
1109bool
1110BaseRemoteGDB::cmd_set_hw_bkpt(GdbCommand::Context &ctx)
1111{
1112    const char *p = ctx.data;
1113    char subcmd = *p++;
1114    if (*p++ != ',')
1115        throw CmdError("E0D");
1116    Addr addr = hex2i(&p);
1117    if (*p++ != ',')
1118        throw CmdError("E0D");
1119    size_t len = hex2i(&p);
1120
1121    DPRINTF(GDBMisc, "set %s, addr=%#x, len=%d\n",
1122            break_type(subcmd), addr, len);
1123
1124    switch (subcmd) {
1125      case GdbSoftBp:
1126        insertSoftBreak(addr, len);
1127        break;
1128      case GdbHardBp:
1129        insertHardBreak(addr, len);
1130        break;
1131      case GdbWriteWp:
1132      case GdbReadWp:
1133      case GdbAccWp:
1134      default: // unknown
1135        throw Unsupported();
1136    }
1137    send("OK");
1138
1139    return true;
1140}
1141