remote_gdb.hh revision 8700
17199Sgblack@eecs.umich.edu/*
27199Sgblack@eecs.umich.edu * Copyright (c) 2002-2005 The Regents of The University of Michigan
37199Sgblack@eecs.umich.edu * All rights reserved.
47199Sgblack@eecs.umich.edu *
57199Sgblack@eecs.umich.edu * Redistribution and use in source and binary forms, with or without
67199Sgblack@eecs.umich.edu * modification, are permitted provided that the following conditions are
77199Sgblack@eecs.umich.edu * met: redistributions of source code must retain the above copyright
87199Sgblack@eecs.umich.edu * notice, this list of conditions and the following disclaimer;
97199Sgblack@eecs.umich.edu * redistributions in binary form must reproduce the above copyright
107199Sgblack@eecs.umich.edu * notice, this list of conditions and the following disclaimer in the
117199Sgblack@eecs.umich.edu * documentation and/or other materials provided with the distribution;
127199Sgblack@eecs.umich.edu * neither the name of the copyright holders nor the names of its
137199Sgblack@eecs.umich.edu * contributors may be used to endorse or promote products derived from
147199Sgblack@eecs.umich.edu * this software without specific prior written permission.
157199Sgblack@eecs.umich.edu *
167199Sgblack@eecs.umich.edu * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
177199Sgblack@eecs.umich.edu * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
187199Sgblack@eecs.umich.edu * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
197199Sgblack@eecs.umich.edu * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
207199Sgblack@eecs.umich.edu * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
217199Sgblack@eecs.umich.edu * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
227199Sgblack@eecs.umich.edu * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
237199Sgblack@eecs.umich.edu * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
247199Sgblack@eecs.umich.edu * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
257199Sgblack@eecs.umich.edu * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
267199Sgblack@eecs.umich.edu * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
277199Sgblack@eecs.umich.edu *
287199Sgblack@eecs.umich.edu * Authors: Nathan Binkert
297199Sgblack@eecs.umich.edu */
307199Sgblack@eecs.umich.edu
317199Sgblack@eecs.umich.edu#ifndef __REMOTE_GDB_HH__
327199Sgblack@eecs.umich.edu#define __REMOTE_GDB_HH__
337199Sgblack@eecs.umich.edu
347199Sgblack@eecs.umich.edu#include <sys/signal.h>
357199Sgblack@eecs.umich.edu
367199Sgblack@eecs.umich.edu#include <map>
377199Sgblack@eecs.umich.edu
387199Sgblack@eecs.umich.edu#include "arch/types.hh"
397199Sgblack@eecs.umich.edu#include "base/pollevent.hh"
407199Sgblack@eecs.umich.edu#include "base/socket.hh"
417199Sgblack@eecs.umich.edu#include "cpu/pc_event.hh"
427199Sgblack@eecs.umich.edu
437199Sgblack@eecs.umich.educlass System;
447199Sgblack@eecs.umich.educlass ThreadContext;
457199Sgblack@eecs.umich.educlass PhysicalMemory;
467199Sgblack@eecs.umich.edu
477199Sgblack@eecs.umich.educlass GDBListener;
487199Sgblack@eecs.umich.edu
497199Sgblack@eecs.umich.eduenum GDBCommands
507199Sgblack@eecs.umich.edu{
517199Sgblack@eecs.umich.edu    GDBSignal              = '?', // last signal
527199Sgblack@eecs.umich.edu    GDBSetBaud             = 'b', // set baud (depracated)
537199Sgblack@eecs.umich.edu    GDBSetBreak            = 'B', // set breakpoint (depracated)
547199Sgblack@eecs.umich.edu    GDBCont                = 'c', // resume
557199Sgblack@eecs.umich.edu    GDBAsyncCont           = 'C', // continue with signal
567199Sgblack@eecs.umich.edu    GDBDebug               = 'd', // toggle debug flags (deprecated)
577199Sgblack@eecs.umich.edu    GDBDetach              = 'D', // detach remote gdb
587202Sgblack@eecs.umich.edu    GDBRegR                = 'g', // read general registers
597202Sgblack@eecs.umich.edu    GDBRegW                = 'G', // write general registers
607202Sgblack@eecs.umich.edu    GDBSetThread           = 'H', // set thread
617202Sgblack@eecs.umich.edu    GDBCycleStep           = 'i', // step a single cycle
627202Sgblack@eecs.umich.edu    GDBSigCycleStep        = 'I', // signal then cycle step
637202Sgblack@eecs.umich.edu    GDBKill                = 'k', // kill program
647202Sgblack@eecs.umich.edu    GDBMemR                = 'm', // read memory
657202Sgblack@eecs.umich.edu    GDBMemW                = 'M', // write memory
667202Sgblack@eecs.umich.edu    GDBReadReg             = 'p', // read register
677202Sgblack@eecs.umich.edu    GDBSetReg              = 'P', // write register
687202Sgblack@eecs.umich.edu    GDBQueryVar            = 'q', // query variable
697202Sgblack@eecs.umich.edu    GDBSetVar              = 'Q', // set variable
707202Sgblack@eecs.umich.edu    GDBReset               = 'r', // reset system.  (Deprecated)
717202Sgblack@eecs.umich.edu    GDBStep                = 's', // step
727202Sgblack@eecs.umich.edu    GDBAsyncStep           = 'S', // signal and step
737202Sgblack@eecs.umich.edu    GDBThreadAlive         = 'T', // find out if the thread is alive
747202Sgblack@eecs.umich.edu    GDBTargetExit          = 'W', // target exited
757202Sgblack@eecs.umich.edu    GDBBinaryDload         = 'X', // write memory
767202Sgblack@eecs.umich.edu    GDBClrHwBkpt           = 'z', // remove breakpoint or watchpoint
777202Sgblack@eecs.umich.edu    GDBSetHwBkpt           = 'Z'  // insert breakpoint or watchpoint
787202Sgblack@eecs.umich.edu};
797202Sgblack@eecs.umich.edu
807202Sgblack@eecs.umich.educonst char GDBStart = '$';
817202Sgblack@eecs.umich.educonst char GDBEnd = '#';
827202Sgblack@eecs.umich.educonst char GDBGoodP = '+';
837202Sgblack@eecs.umich.educonst char GDBBadP = '-';
847202Sgblack@eecs.umich.edu
857202Sgblack@eecs.umich.educonst int GDBPacketBufLen = 1024;
867202Sgblack@eecs.umich.edu
877202Sgblack@eecs.umich.educlass BaseRemoteGDB
887202Sgblack@eecs.umich.edu{
897202Sgblack@eecs.umich.edu  private:
907202Sgblack@eecs.umich.edu    friend void debugger();
917202Sgblack@eecs.umich.edu    friend class GDBListener;
927202Sgblack@eecs.umich.edu
937202Sgblack@eecs.umich.edu    //Helper functions
947202Sgblack@eecs.umich.edu  protected:
957202Sgblack@eecs.umich.edu    int digit2i(char);
967202Sgblack@eecs.umich.edu    char i2digit(int);
977202Sgblack@eecs.umich.edu    Addr hex2i(const char **);
987202Sgblack@eecs.umich.edu    //Address formats, break types, and gdb commands may change
997202Sgblack@eecs.umich.edu    //between architectures, so they're defined as virtual
1007202Sgblack@eecs.umich.edu    //functions.
1017202Sgblack@eecs.umich.edu    virtual void mem2hex(void *, const void *, int);
1027202Sgblack@eecs.umich.edu    virtual const char * hex2mem(void *, const char *, int);
1037202Sgblack@eecs.umich.edu    virtual const char * break_type(char c);
1047202Sgblack@eecs.umich.edu    virtual const char * gdb_command(char cmd);
1057202Sgblack@eecs.umich.edu
1067202Sgblack@eecs.umich.edu  protected:
1077202Sgblack@eecs.umich.edu    class Event : public PollEvent
1087202Sgblack@eecs.umich.edu    {
1097202Sgblack@eecs.umich.edu      protected:
1107202Sgblack@eecs.umich.edu        BaseRemoteGDB *gdb;
1117202Sgblack@eecs.umich.edu
1127202Sgblack@eecs.umich.edu      public:
1137202Sgblack@eecs.umich.edu        Event(BaseRemoteGDB *g, int fd, int e);
1147202Sgblack@eecs.umich.edu        void process(int revent);
1157202Sgblack@eecs.umich.edu    };
1167202Sgblack@eecs.umich.edu
1177202Sgblack@eecs.umich.edu    friend class Event;
1187202Sgblack@eecs.umich.edu    Event *event;
1197202Sgblack@eecs.umich.edu    GDBListener *listener;
1207209Sgblack@eecs.umich.edu    int number;
1217209Sgblack@eecs.umich.edu
1227209Sgblack@eecs.umich.edu  protected:
1237209Sgblack@eecs.umich.edu    //The socket commands come in through
1247209Sgblack@eecs.umich.edu    int fd;
1257209Sgblack@eecs.umich.edu
1267209Sgblack@eecs.umich.edu  protected:
1277209Sgblack@eecs.umich.edu#ifdef notyet
1287209Sgblack@eecs.umich.edu    label_t recover;
1297209Sgblack@eecs.umich.edu#endif
1307209Sgblack@eecs.umich.edu    bool active;
1317209Sgblack@eecs.umich.edu    bool attached;
1327209Sgblack@eecs.umich.edu
1337209Sgblack@eecs.umich.edu    System *system;
1347209Sgblack@eecs.umich.edu    PhysicalMemory *pmem;
1357209Sgblack@eecs.umich.edu    ThreadContext *context;
1367209Sgblack@eecs.umich.edu
1377209Sgblack@eecs.umich.edu  protected:
1387209Sgblack@eecs.umich.edu    class GdbRegCache
1397209Sgblack@eecs.umich.edu    {
1407209Sgblack@eecs.umich.edu      public:
1417209Sgblack@eecs.umich.edu        GdbRegCache(size_t newSize) : regs(new uint64_t[newSize]), size(newSize)
1427209Sgblack@eecs.umich.edu        {}
1437209Sgblack@eecs.umich.edu        ~GdbRegCache()
1447209Sgblack@eecs.umich.edu        {
1457209Sgblack@eecs.umich.edu            delete [] regs;
1467209Sgblack@eecs.umich.edu        }
1477209Sgblack@eecs.umich.edu
1487209Sgblack@eecs.umich.edu        uint64_t * regs;
1497209Sgblack@eecs.umich.edu        size_t size;
1507209Sgblack@eecs.umich.edu        size_t bytes() { return size * sizeof(uint64_t); }
1517209Sgblack@eecs.umich.edu    };
1527209Sgblack@eecs.umich.edu
1537209Sgblack@eecs.umich.edu    GdbRegCache gdbregs;
1547209Sgblack@eecs.umich.edu
1557209Sgblack@eecs.umich.edu  protected:
1567226Sgblack@eecs.umich.edu    uint8_t getbyte();
1577249Sgblack@eecs.umich.edu    void putbyte(uint8_t b);
1587249Sgblack@eecs.umich.edu
1597249Sgblack@eecs.umich.edu    int recv(char *data, int len);
1607249Sgblack@eecs.umich.edu    void send(const char *data);
1617249Sgblack@eecs.umich.edu
1627249Sgblack@eecs.umich.edu  protected:
1637249Sgblack@eecs.umich.edu    // Machine memory
1647249Sgblack@eecs.umich.edu    virtual bool read(Addr addr, size_t size, char *data);
1657249Sgblack@eecs.umich.edu    virtual bool write(Addr addr, size_t size, const char *data);
1667249Sgblack@eecs.umich.edu
1677249Sgblack@eecs.umich.edu    template <class T> T read(Addr addr);
1687249Sgblack@eecs.umich.edu    template <class T> void write(Addr addr, T data);
1697249Sgblack@eecs.umich.edu
1707249Sgblack@eecs.umich.edu  public:
1717249Sgblack@eecs.umich.edu    BaseRemoteGDB(System *system, ThreadContext *context, size_t cacheSize);
1727249Sgblack@eecs.umich.edu    virtual ~BaseRemoteGDB();
1737249Sgblack@eecs.umich.edu
1747249Sgblack@eecs.umich.edu    void replaceThreadContext(ThreadContext *tc) { context = tc; }
1757249Sgblack@eecs.umich.edu
1767249Sgblack@eecs.umich.edu    void attach(int fd);
1777251Sgblack@eecs.umich.edu    void detach();
1787251Sgblack@eecs.umich.edu    bool isattached();
1797251Sgblack@eecs.umich.edu
1807252Sgblack@eecs.umich.edu    virtual bool acc(Addr addr, size_t len) = 0;
1817251Sgblack@eecs.umich.edu    bool trap(int type);
1827251Sgblack@eecs.umich.edu    virtual bool breakpoint()
1837251Sgblack@eecs.umich.edu    {
1847251Sgblack@eecs.umich.edu        return trap(SIGTRAP);
1857251Sgblack@eecs.umich.edu    }
1867251Sgblack@eecs.umich.edu
1877226Sgblack@eecs.umich.edu  protected:
1887226Sgblack@eecs.umich.edu    virtual void getregs() = 0;
1897226Sgblack@eecs.umich.edu    virtual void setregs() = 0;
1907232Sgblack@eecs.umich.edu
1917226Sgblack@eecs.umich.edu    virtual void clearSingleStep() = 0;
1927226Sgblack@eecs.umich.edu    virtual void setSingleStep() = 0;
1937226Sgblack@eecs.umich.edu
1947226Sgblack@eecs.umich.edu    PCEventQueue *getPcEventQueue();
1957226Sgblack@eecs.umich.edu
1967232Sgblack@eecs.umich.edu  protected:
1977226Sgblack@eecs.umich.edu    class HardBreakpoint : public PCEvent
1987226Sgblack@eecs.umich.edu    {
1997232Sgblack@eecs.umich.edu      private:
2007232Sgblack@eecs.umich.edu        BaseRemoteGDB *gdb;
2017226Sgblack@eecs.umich.edu
2027226Sgblack@eecs.umich.edu      public:
2037226Sgblack@eecs.umich.edu        int refcount;
2047226Sgblack@eecs.umich.edu
2057226Sgblack@eecs.umich.edu      public:
2067232Sgblack@eecs.umich.edu        HardBreakpoint(BaseRemoteGDB *_gdb, Addr addr);
2077226Sgblack@eecs.umich.edu        std::string name() { return gdb->name() + ".hwbkpt"; }
2087226Sgblack@eecs.umich.edu
2097226Sgblack@eecs.umich.edu        virtual void process(ThreadContext *tc);
2107226Sgblack@eecs.umich.edu    };
2117226Sgblack@eecs.umich.edu    friend class HardBreakpoint;
2127232Sgblack@eecs.umich.edu
2137226Sgblack@eecs.umich.edu    typedef std::map<Addr, HardBreakpoint *> break_map_t;
2147226Sgblack@eecs.umich.edu    typedef break_map_t::iterator break_iter_t;
2157232Sgblack@eecs.umich.edu    break_map_t hardBreakMap;
2167232Sgblack@eecs.umich.edu
2177226Sgblack@eecs.umich.edu    bool insertSoftBreak(Addr addr, size_t len);
2187226Sgblack@eecs.umich.edu    bool removeSoftBreak(Addr addr, size_t len);
2197226Sgblack@eecs.umich.edu    virtual bool insertHardBreak(Addr addr, size_t len);
2207226Sgblack@eecs.umich.edu    bool removeHardBreak(Addr addr, size_t len);
2217226Sgblack@eecs.umich.edu
2227226Sgblack@eecs.umich.edu  protected:
2237226Sgblack@eecs.umich.edu    void clearTempBreakpoint(Addr &bkpt);
2247226Sgblack@eecs.umich.edu    void setTempBreakpoint(Addr bkpt);
2257232Sgblack@eecs.umich.edu
2267226Sgblack@eecs.umich.edu  public:
2277226Sgblack@eecs.umich.edu    std::string name();
2287232Sgblack@eecs.umich.edu};
2297226Sgblack@eecs.umich.edu
2307226Sgblack@eecs.umich.edutemplate <class T>
2317226Sgblack@eecs.umich.eduinline T
2327226Sgblack@eecs.umich.eduBaseRemoteGDB::read(Addr addr)
2337232Sgblack@eecs.umich.edu{
2347226Sgblack@eecs.umich.edu    T temp;
2357226Sgblack@eecs.umich.edu    read(addr, sizeof(T), (char *)&temp);
2367232Sgblack@eecs.umich.edu    return temp;
2377232Sgblack@eecs.umich.edu}
2387226Sgblack@eecs.umich.edu
2397226Sgblack@eecs.umich.edutemplate <class T>
2407226Sgblack@eecs.umich.eduinline void
2417226Sgblack@eecs.umich.eduBaseRemoteGDB::write(Addr addr, T data)
2427226Sgblack@eecs.umich.edu{ write(addr, sizeof(T), (const char *)&data); }
2437226Sgblack@eecs.umich.edu
2447226Sgblack@eecs.umich.educlass GDBListener
2457226Sgblack@eecs.umich.edu{
2467232Sgblack@eecs.umich.edu  protected:
2477226Sgblack@eecs.umich.edu    class Event : public PollEvent
2487226Sgblack@eecs.umich.edu    {
2497232Sgblack@eecs.umich.edu      protected:
2507226Sgblack@eecs.umich.edu        GDBListener *listener;
2517226Sgblack@eecs.umich.edu
2527226Sgblack@eecs.umich.edu      public:
2537226Sgblack@eecs.umich.edu        Event(GDBListener *l, int fd, int e);
2547232Sgblack@eecs.umich.edu        void process(int revent);
2557226Sgblack@eecs.umich.edu    };
2567226Sgblack@eecs.umich.edu
2577232Sgblack@eecs.umich.edu    friend class Event;
2587232Sgblack@eecs.umich.edu    Event *event;
2597226Sgblack@eecs.umich.edu
2607234Sgblack@eecs.umich.edu  protected:
2617234Sgblack@eecs.umich.edu    ListenSocket listener;
2627234Sgblack@eecs.umich.edu    BaseRemoteGDB *gdb;
2637234Sgblack@eecs.umich.edu    int port;
2647234Sgblack@eecs.umich.edu
2657234Sgblack@eecs.umich.edu  public:
2667234Sgblack@eecs.umich.edu    GDBListener(BaseRemoteGDB *g, int p);
2677234Sgblack@eecs.umich.edu    ~GDBListener();
2687234Sgblack@eecs.umich.edu
2697234Sgblack@eecs.umich.edu    void accept();
2707234Sgblack@eecs.umich.edu    void listen();
2717234Sgblack@eecs.umich.edu    std::string name();
2727234Sgblack@eecs.umich.edu};
2737234Sgblack@eecs.umich.edu
2747234Sgblack@eecs.umich.edu#endif /* __REMOTE_GDB_H__ */
2757234Sgblack@eecs.umich.edu