remote_gdb.hh revision 3960
12SN/A/*
21762SN/A * Copyright (c) 2002-2005 The Regents of The University of Michigan
32SN/A * All rights reserved.
42SN/A *
52SN/A * Redistribution and use in source and binary forms, with or without
62SN/A * modification, are permitted provided that the following conditions are
72SN/A * met: redistributions of source code must retain the above copyright
82SN/A * notice, this list of conditions and the following disclaimer;
92SN/A * redistributions in binary form must reproduce the above copyright
102SN/A * notice, this list of conditions and the following disclaimer in the
112SN/A * documentation and/or other materials provided with the distribution;
122SN/A * neither the name of the copyright holders nor the names of its
132SN/A * contributors may be used to endorse or promote products derived from
142SN/A * this software without specific prior written permission.
152SN/A *
162SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
172SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
182SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
192SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
202SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
212SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
222SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
232SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
242SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
252SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
262SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
272665Ssaidi@eecs.umich.edu *
282665Ssaidi@eecs.umich.edu * Authors: Nathan Binkert
292SN/A */
302SN/A
312SN/A#ifndef __REMOTE_GDB_HH__
322SN/A#define __REMOTE_GDB_HH__
332SN/A
3477SN/A#include <map>
353960Sgblack@eecs.umich.edu#include <sys/signal.h>
3677SN/A
372986Sgblack@eecs.umich.edu#include "arch/types.hh"
3856SN/A#include "cpu/pc_event.hh"
3956SN/A#include "base/pollevent.hh"
4056SN/A#include "base/socket.hh"
412SN/A
422SN/Aclass System;
432680Sktlim@umich.educlass ThreadContext;
442SN/Aclass PhysicalMemory;
452SN/A
461910SN/Aclass GDBListener;
473536Sgblack@eecs.umich.edu
483536Sgblack@eecs.umich.eduenum GDBCommands
492SN/A{
503536Sgblack@eecs.umich.edu    GDBSignal              = '?', // last signal
513536Sgblack@eecs.umich.edu    GDBSetBaud             = 'b', // set baud (depracated)
523536Sgblack@eecs.umich.edu    GDBSetBreak            = 'B', // set breakpoint (depracated)
533536Sgblack@eecs.umich.edu    GDBCont                = 'c', // resume
543536Sgblack@eecs.umich.edu    GDBAsyncCont           = 'C', // continue with signal
553536Sgblack@eecs.umich.edu    GDBDebug               = 'd', // toggle debug flags (deprecated)
563536Sgblack@eecs.umich.edu    GDBDetach              = 'D', // detach remote gdb
573536Sgblack@eecs.umich.edu    GDBRegR                = 'g', // read general registers
583536Sgblack@eecs.umich.edu    GDBRegW                = 'G', // write general registers
593536Sgblack@eecs.umich.edu    GDBSetThread           = 'H', // set thread
603536Sgblack@eecs.umich.edu    GDBCycleStep           = 'i', // step a single cycle
613536Sgblack@eecs.umich.edu    GDBSigCycleStep        = 'I', // signal then cycle step
623536Sgblack@eecs.umich.edu    GDBKill                = 'k', // kill program
633536Sgblack@eecs.umich.edu    GDBMemR                = 'm', // read memory
643536Sgblack@eecs.umich.edu    GDBMemW                = 'M', // write memory
653536Sgblack@eecs.umich.edu    GDBReadReg             = 'p', // read register
663536Sgblack@eecs.umich.edu    GDBSetReg              = 'P', // write register
673536Sgblack@eecs.umich.edu    GDBQueryVar            = 'q', // query variable
683536Sgblack@eecs.umich.edu    GDBSetVar              = 'Q', // set variable
693536Sgblack@eecs.umich.edu    GDBReset               = 'r', // reset system.  (Deprecated)
703536Sgblack@eecs.umich.edu    GDBStep                = 's', // step
713536Sgblack@eecs.umich.edu    GDBAsyncStep           = 'S', // signal and step
723536Sgblack@eecs.umich.edu    GDBThreadAlive         = 'T', // find out if the thread is alive
733536Sgblack@eecs.umich.edu    GDBTargetExit          = 'W', // target exited
743536Sgblack@eecs.umich.edu    GDBBinaryDload         = 'X', // write memory
753536Sgblack@eecs.umich.edu    GDBClrHwBkpt           = 'z', // remove breakpoint or watchpoint
763536Sgblack@eecs.umich.edu    GDBSetHwBkpt           = 'Z'  // insert breakpoint or watchpoint
773536Sgblack@eecs.umich.edu};
783536Sgblack@eecs.umich.edu
793536Sgblack@eecs.umich.educonst char GDBStart = '$';
803536Sgblack@eecs.umich.educonst char GDBEnd = '#';
813536Sgblack@eecs.umich.educonst char GDBGoodP = '+';
823536Sgblack@eecs.umich.educonst char GDBBadP = '-';
833536Sgblack@eecs.umich.edu
843536Sgblack@eecs.umich.educonst int GDBPacketBufLen = 1024;
853536Sgblack@eecs.umich.edu
863536Sgblack@eecs.umich.educlass BaseRemoteGDB
873536Sgblack@eecs.umich.edu{
881910SN/A  private:
891910SN/A    friend void debugger();
901910SN/A    friend class GDBListener;
911910SN/A
923536Sgblack@eecs.umich.edu    //Helper functions
933536Sgblack@eecs.umich.edu  protected:
943536Sgblack@eecs.umich.edu    int digit2i(char);
953536Sgblack@eecs.umich.edu    char i2digit(int);
963536Sgblack@eecs.umich.edu    Addr hex2i(const char **);
973536Sgblack@eecs.umich.edu    //Address formats, break types, and gdb commands may change
983536Sgblack@eecs.umich.edu    //between architectures, so they're defined as virtual
993536Sgblack@eecs.umich.edu    //functions.
1003536Sgblack@eecs.umich.edu    virtual void mem2hex(void *, const void *, int);
1013536Sgblack@eecs.umich.edu    virtual const char * hex2mem(void *, const char *, int);
1023536Sgblack@eecs.umich.edu    virtual const char * break_type(char c);
1033536Sgblack@eecs.umich.edu    virtual const char * gdb_command(char cmd);
1043536Sgblack@eecs.umich.edu
1052SN/A  protected:
1062SN/A    class Event : public PollEvent
1072SN/A    {
1082SN/A      protected:
1093536Sgblack@eecs.umich.edu        BaseRemoteGDB *gdb;
1102SN/A
1112SN/A      public:
1123536Sgblack@eecs.umich.edu        Event(BaseRemoteGDB *g, int fd, int e);
1132SN/A        void process(int revent);
1142SN/A    };
1152SN/A
1162SN/A    friend class Event;
1172SN/A    Event *event;
1181910SN/A    GDBListener *listener;
1191910SN/A    int number;
1202SN/A
1212SN/A  protected:
1223536Sgblack@eecs.umich.edu    //The socket commands come in through
1232SN/A    int fd;
1242SN/A
1252SN/A  protected:
1262SN/A#ifdef notyet
1272SN/A    label_t recover;
1282SN/A#endif
1292SN/A    bool active;
1302SN/A    bool attached;
1312SN/A
1322SN/A    System *system;
1332SN/A    PhysicalMemory *pmem;
1342680Sktlim@umich.edu    ThreadContext *context;
1352SN/A
1362SN/A  protected:
1373536Sgblack@eecs.umich.edu    class GdbRegCache
1383536Sgblack@eecs.umich.edu    {
1393536Sgblack@eecs.umich.edu      public:
1403536Sgblack@eecs.umich.edu        GdbRegCache(size_t newSize) : regs(new uint64_t[newSize]), size(newSize)
1413536Sgblack@eecs.umich.edu        {}
1423536Sgblack@eecs.umich.edu        ~GdbRegCache()
1433536Sgblack@eecs.umich.edu        {
1443536Sgblack@eecs.umich.edu            delete [] regs;
1453536Sgblack@eecs.umich.edu        }
1463536Sgblack@eecs.umich.edu
1473536Sgblack@eecs.umich.edu        uint64_t * regs;
1483536Sgblack@eecs.umich.edu        size_t size;
1493579Sgblack@eecs.umich.edu        size_t bytes() { return size * sizeof(uint64_t); }
1503536Sgblack@eecs.umich.edu    };
1513536Sgblack@eecs.umich.edu
1523536Sgblack@eecs.umich.edu    GdbRegCache gdbregs;
1533536Sgblack@eecs.umich.edu
1543536Sgblack@eecs.umich.edu  protected:
1552SN/A    uint8_t getbyte();
1562SN/A    void putbyte(uint8_t b);
1572SN/A
1582SN/A    int recv(char *data, int len);
1592SN/A    void send(const char *data);
1602SN/A
1612SN/A  protected:
1622SN/A    // Machine memory
1633536Sgblack@eecs.umich.edu    virtual bool read(Addr addr, size_t size, char *data);
1643536Sgblack@eecs.umich.edu    virtual bool write(Addr addr, size_t size, const char *data);
1652SN/A
1662SN/A    template <class T> T read(Addr addr);
1672SN/A    template <class T> void write(Addr addr, T data);
1682SN/A
1692SN/A  public:
1703536Sgblack@eecs.umich.edu    BaseRemoteGDB(System *system, ThreadContext *context, size_t cacheSize);
1713536Sgblack@eecs.umich.edu    virtual ~BaseRemoteGDB();
1722SN/A
1732680Sktlim@umich.edu    void replaceThreadContext(ThreadContext *tc) { context = tc; }
174180SN/A
1752SN/A    void attach(int fd);
1762SN/A    void detach();
1772SN/A    bool isattached();
1782SN/A
1793536Sgblack@eecs.umich.edu    virtual bool acc(Addr addr, size_t len) = 0;
1802SN/A    bool trap(int type);
1813960Sgblack@eecs.umich.edu    virtual bool breakpoint()
1823960Sgblack@eecs.umich.edu    {
1833960Sgblack@eecs.umich.edu        return trap(SIGTRAP);
1843960Sgblack@eecs.umich.edu    }
1852SN/A
1862SN/A  protected:
1873536Sgblack@eecs.umich.edu    virtual void getregs() = 0;
1883536Sgblack@eecs.umich.edu    virtual void setregs() = 0;
1892SN/A
1903536Sgblack@eecs.umich.edu    virtual void clearSingleStep() = 0;
1913536Sgblack@eecs.umich.edu    virtual void setSingleStep() = 0;
1922SN/A
1932SN/A    PCEventQueue *getPcEventQueue();
1942SN/A
1952SN/A  protected:
1962SN/A    class HardBreakpoint : public PCEvent
1972SN/A    {
1982SN/A      private:
1993536Sgblack@eecs.umich.edu        BaseRemoteGDB *gdb;
2002SN/A
2012SN/A      public:
202507SN/A        int refcount;
203507SN/A
204507SN/A      public:
2053536Sgblack@eecs.umich.edu        HardBreakpoint(BaseRemoteGDB *_gdb, Addr addr);
206507SN/A        std::string name() { return gdb->name() + ".hwbkpt"; }
2072SN/A
2082680Sktlim@umich.edu        virtual void process(ThreadContext *tc);
2092SN/A    };
2102SN/A    friend class HardBreakpoint;
2112SN/A
2122SN/A    typedef std::map<Addr, HardBreakpoint *> break_map_t;
2132SN/A    typedef break_map_t::iterator break_iter_t;
2142SN/A    break_map_t hardBreakMap;
2152SN/A
2162SN/A    bool insertSoftBreak(Addr addr, size_t len);
2172SN/A    bool removeSoftBreak(Addr addr, size_t len);
2182SN/A    bool insertHardBreak(Addr addr, size_t len);
2192SN/A    bool removeHardBreak(Addr addr, size_t len);
2202SN/A
2213550Sgblack@eecs.umich.edu  protected:
2223550Sgblack@eecs.umich.edu    void clearTempBreakpoint(Addr &bkpt);
2233550Sgblack@eecs.umich.edu    void setTempBreakpoint(Addr bkpt);
2243550Sgblack@eecs.umich.edu
225507SN/A  public:
226507SN/A    std::string name();
2272SN/A};
2282SN/A
2292SN/Atemplate <class T>
2302SN/Ainline T
2313536Sgblack@eecs.umich.eduBaseRemoteGDB::read(Addr addr)
2322SN/A{
2332SN/A    T temp;
2342SN/A    read(addr, sizeof(T), (char *)&temp);
2352SN/A    return temp;
2362SN/A}
2372SN/A
2382SN/Atemplate <class T>
2392SN/Ainline void
2403536Sgblack@eecs.umich.eduBaseRemoteGDB::write(Addr addr, T data)
2412SN/A{ write(addr, sizeof(T), (const char *)&data); }
2422SN/A
2432SN/Aclass GDBListener
2442SN/A{
2452SN/A  protected:
2462SN/A    class Event : public PollEvent
2472SN/A    {
2482SN/A      protected:
2492SN/A        GDBListener *listener;
2502SN/A
2512SN/A      public:
2522SN/A        Event(GDBListener *l, int fd, int e);
2532SN/A        void process(int revent);
2542SN/A    };
2552SN/A
2562SN/A    friend class Event;
2572SN/A    Event *event;
2582SN/A
2592SN/A  protected:
2602SN/A    ListenSocket listener;
2613536Sgblack@eecs.umich.edu    BaseRemoteGDB *gdb;
2622SN/A    int port;
2632SN/A
2642SN/A  public:
2653536Sgblack@eecs.umich.edu    GDBListener(BaseRemoteGDB *g, int p);
2662SN/A    ~GDBListener();
2672SN/A
2682SN/A    void accept();
2692SN/A    void listen();
270507SN/A    std::string name();
2712SN/A};
2722SN/A
2732SN/A#endif /* __REMOTE_GDB_H__ */
274