remote_gdb.hh revision 8229
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
343960Sgblack@eecs.umich.edu#include <sys/signal.h>
3577SN/A
368229Snate@binkert.org#include <map>
378229Snate@binkert.org
382986Sgblack@eecs.umich.edu#include "arch/types.hh"
3956SN/A#include "base/pollevent.hh"
4056SN/A#include "base/socket.hh"
418229Snate@binkert.org#include "cpu/pc_event.hh"
422SN/A
432SN/Aclass System;
442680Sktlim@umich.educlass ThreadContext;
452SN/Aclass PhysicalMemory;
462SN/A
471910SN/Aclass GDBListener;
483536Sgblack@eecs.umich.edu
493536Sgblack@eecs.umich.eduenum GDBCommands
502SN/A{
513536Sgblack@eecs.umich.edu    GDBSignal              = '?', // last signal
523536Sgblack@eecs.umich.edu    GDBSetBaud             = 'b', // set baud (depracated)
533536Sgblack@eecs.umich.edu    GDBSetBreak            = 'B', // set breakpoint (depracated)
543536Sgblack@eecs.umich.edu    GDBCont                = 'c', // resume
553536Sgblack@eecs.umich.edu    GDBAsyncCont           = 'C', // continue with signal
563536Sgblack@eecs.umich.edu    GDBDebug               = 'd', // toggle debug flags (deprecated)
573536Sgblack@eecs.umich.edu    GDBDetach              = 'D', // detach remote gdb
583536Sgblack@eecs.umich.edu    GDBRegR                = 'g', // read general registers
593536Sgblack@eecs.umich.edu    GDBRegW                = 'G', // write general registers
603536Sgblack@eecs.umich.edu    GDBSetThread           = 'H', // set thread
613536Sgblack@eecs.umich.edu    GDBCycleStep           = 'i', // step a single cycle
623536Sgblack@eecs.umich.edu    GDBSigCycleStep        = 'I', // signal then cycle step
633536Sgblack@eecs.umich.edu    GDBKill                = 'k', // kill program
643536Sgblack@eecs.umich.edu    GDBMemR                = 'm', // read memory
653536Sgblack@eecs.umich.edu    GDBMemW                = 'M', // write memory
663536Sgblack@eecs.umich.edu    GDBReadReg             = 'p', // read register
673536Sgblack@eecs.umich.edu    GDBSetReg              = 'P', // write register
683536Sgblack@eecs.umich.edu    GDBQueryVar            = 'q', // query variable
693536Sgblack@eecs.umich.edu    GDBSetVar              = 'Q', // set variable
703536Sgblack@eecs.umich.edu    GDBReset               = 'r', // reset system.  (Deprecated)
713536Sgblack@eecs.umich.edu    GDBStep                = 's', // step
723536Sgblack@eecs.umich.edu    GDBAsyncStep           = 'S', // signal and step
733536Sgblack@eecs.umich.edu    GDBThreadAlive         = 'T', // find out if the thread is alive
743536Sgblack@eecs.umich.edu    GDBTargetExit          = 'W', // target exited
753536Sgblack@eecs.umich.edu    GDBBinaryDload         = 'X', // write memory
763536Sgblack@eecs.umich.edu    GDBClrHwBkpt           = 'z', // remove breakpoint or watchpoint
773536Sgblack@eecs.umich.edu    GDBSetHwBkpt           = 'Z'  // insert breakpoint or watchpoint
783536Sgblack@eecs.umich.edu};
793536Sgblack@eecs.umich.edu
803536Sgblack@eecs.umich.educonst char GDBStart = '$';
813536Sgblack@eecs.umich.educonst char GDBEnd = '#';
823536Sgblack@eecs.umich.educonst char GDBGoodP = '+';
833536Sgblack@eecs.umich.educonst char GDBBadP = '-';
843536Sgblack@eecs.umich.edu
853536Sgblack@eecs.umich.educonst int GDBPacketBufLen = 1024;
863536Sgblack@eecs.umich.edu
873536Sgblack@eecs.umich.educlass BaseRemoteGDB
883536Sgblack@eecs.umich.edu{
891910SN/A  private:
901910SN/A    friend void debugger();
911910SN/A    friend class GDBListener;
921910SN/A
933536Sgblack@eecs.umich.edu    //Helper functions
943536Sgblack@eecs.umich.edu  protected:
953536Sgblack@eecs.umich.edu    int digit2i(char);
963536Sgblack@eecs.umich.edu    char i2digit(int);
973536Sgblack@eecs.umich.edu    Addr hex2i(const char **);
983536Sgblack@eecs.umich.edu    //Address formats, break types, and gdb commands may change
993536Sgblack@eecs.umich.edu    //between architectures, so they're defined as virtual
1003536Sgblack@eecs.umich.edu    //functions.
1013536Sgblack@eecs.umich.edu    virtual void mem2hex(void *, const void *, int);
1023536Sgblack@eecs.umich.edu    virtual const char * hex2mem(void *, const char *, int);
1033536Sgblack@eecs.umich.edu    virtual const char * break_type(char c);
1043536Sgblack@eecs.umich.edu    virtual const char * gdb_command(char cmd);
1053536Sgblack@eecs.umich.edu
1062SN/A  protected:
1072SN/A    class Event : public PollEvent
1082SN/A    {
1092SN/A      protected:
1103536Sgblack@eecs.umich.edu        BaseRemoteGDB *gdb;
1112SN/A
1122SN/A      public:
1133536Sgblack@eecs.umich.edu        Event(BaseRemoteGDB *g, int fd, int e);
1142SN/A        void process(int revent);
1152SN/A    };
1162SN/A
1172SN/A    friend class Event;
1182SN/A    Event *event;
1191910SN/A    GDBListener *listener;
1201910SN/A    int number;
1212SN/A
1222SN/A  protected:
1233536Sgblack@eecs.umich.edu    //The socket commands come in through
1242SN/A    int fd;
1252SN/A
1262SN/A  protected:
1272SN/A#ifdef notyet
1282SN/A    label_t recover;
1292SN/A#endif
1302SN/A    bool active;
1312SN/A    bool attached;
1322SN/A
1332SN/A    System *system;
1342SN/A    PhysicalMemory *pmem;
1352680Sktlim@umich.edu    ThreadContext *context;
1362SN/A
1372SN/A  protected:
1383536Sgblack@eecs.umich.edu    class GdbRegCache
1393536Sgblack@eecs.umich.edu    {
1403536Sgblack@eecs.umich.edu      public:
1413536Sgblack@eecs.umich.edu        GdbRegCache(size_t newSize) : regs(new uint64_t[newSize]), size(newSize)
1423536Sgblack@eecs.umich.edu        {}
1433536Sgblack@eecs.umich.edu        ~GdbRegCache()
1443536Sgblack@eecs.umich.edu        {
1453536Sgblack@eecs.umich.edu            delete [] regs;
1463536Sgblack@eecs.umich.edu        }
1473536Sgblack@eecs.umich.edu
1483536Sgblack@eecs.umich.edu        uint64_t * regs;
1493536Sgblack@eecs.umich.edu        size_t size;
1503579Sgblack@eecs.umich.edu        size_t bytes() { return size * sizeof(uint64_t); }
1513536Sgblack@eecs.umich.edu    };
1523536Sgblack@eecs.umich.edu
1533536Sgblack@eecs.umich.edu    GdbRegCache gdbregs;
1543536Sgblack@eecs.umich.edu
1553536Sgblack@eecs.umich.edu  protected:
1562SN/A    uint8_t getbyte();
1572SN/A    void putbyte(uint8_t b);
1582SN/A
1592SN/A    int recv(char *data, int len);
1602SN/A    void send(const char *data);
1612SN/A
1622SN/A  protected:
1632SN/A    // Machine memory
1643536Sgblack@eecs.umich.edu    virtual bool read(Addr addr, size_t size, char *data);
1653536Sgblack@eecs.umich.edu    virtual bool write(Addr addr, size_t size, const char *data);
1662SN/A
1672SN/A    template <class T> T read(Addr addr);
1682SN/A    template <class T> void write(Addr addr, T data);
1692SN/A
1702SN/A  public:
1713536Sgblack@eecs.umich.edu    BaseRemoteGDB(System *system, ThreadContext *context, size_t cacheSize);
1723536Sgblack@eecs.umich.edu    virtual ~BaseRemoteGDB();
1732SN/A
1742680Sktlim@umich.edu    void replaceThreadContext(ThreadContext *tc) { context = tc; }
175180SN/A
1762SN/A    void attach(int fd);
1772SN/A    void detach();
1782SN/A    bool isattached();
1792SN/A
1803536Sgblack@eecs.umich.edu    virtual bool acc(Addr addr, size_t len) = 0;
1812SN/A    bool trap(int type);
1823960Sgblack@eecs.umich.edu    virtual bool breakpoint()
1833960Sgblack@eecs.umich.edu    {
1843960Sgblack@eecs.umich.edu        return trap(SIGTRAP);
1853960Sgblack@eecs.umich.edu    }
1862SN/A
1872SN/A  protected:
1883536Sgblack@eecs.umich.edu    virtual void getregs() = 0;
1893536Sgblack@eecs.umich.edu    virtual void setregs() = 0;
1902SN/A
1913536Sgblack@eecs.umich.edu    virtual void clearSingleStep() = 0;
1923536Sgblack@eecs.umich.edu    virtual void setSingleStep() = 0;
1932SN/A
1942SN/A    PCEventQueue *getPcEventQueue();
1952SN/A
1962SN/A  protected:
1972SN/A    class HardBreakpoint : public PCEvent
1982SN/A    {
1992SN/A      private:
2003536Sgblack@eecs.umich.edu        BaseRemoteGDB *gdb;
2012SN/A
2022SN/A      public:
203507SN/A        int refcount;
204507SN/A
205507SN/A      public:
2063536Sgblack@eecs.umich.edu        HardBreakpoint(BaseRemoteGDB *_gdb, Addr addr);
207507SN/A        std::string name() { return gdb->name() + ".hwbkpt"; }
2082SN/A
2092680Sktlim@umich.edu        virtual void process(ThreadContext *tc);
2102SN/A    };
2112SN/A    friend class HardBreakpoint;
2122SN/A
2132SN/A    typedef std::map<Addr, HardBreakpoint *> break_map_t;
2142SN/A    typedef break_map_t::iterator break_iter_t;
2152SN/A    break_map_t hardBreakMap;
2162SN/A
2172SN/A    bool insertSoftBreak(Addr addr, size_t len);
2182SN/A    bool removeSoftBreak(Addr addr, size_t len);
2192SN/A    bool insertHardBreak(Addr addr, size_t len);
2202SN/A    bool removeHardBreak(Addr addr, size_t len);
2212SN/A
2223550Sgblack@eecs.umich.edu  protected:
2233550Sgblack@eecs.umich.edu    void clearTempBreakpoint(Addr &bkpt);
2243550Sgblack@eecs.umich.edu    void setTempBreakpoint(Addr bkpt);
2253550Sgblack@eecs.umich.edu
226507SN/A  public:
227507SN/A    std::string name();
2282SN/A};
2292SN/A
2302SN/Atemplate <class T>
2312SN/Ainline T
2323536Sgblack@eecs.umich.eduBaseRemoteGDB::read(Addr addr)
2332SN/A{
2342SN/A    T temp;
2352SN/A    read(addr, sizeof(T), (char *)&temp);
2362SN/A    return temp;
2372SN/A}
2382SN/A
2392SN/Atemplate <class T>
2402SN/Ainline void
2413536Sgblack@eecs.umich.eduBaseRemoteGDB::write(Addr addr, T data)
2422SN/A{ write(addr, sizeof(T), (const char *)&data); }
2432SN/A
2442SN/Aclass GDBListener
2452SN/A{
2462SN/A  protected:
2472SN/A    class Event : public PollEvent
2482SN/A    {
2492SN/A      protected:
2502SN/A        GDBListener *listener;
2512SN/A
2522SN/A      public:
2532SN/A        Event(GDBListener *l, int fd, int e);
2542SN/A        void process(int revent);
2552SN/A    };
2562SN/A
2572SN/A    friend class Event;
2582SN/A    Event *event;
2592SN/A
2602SN/A  protected:
2612SN/A    ListenSocket listener;
2623536Sgblack@eecs.umich.edu    BaseRemoteGDB *gdb;
2632SN/A    int port;
2642SN/A
2652SN/A  public:
2663536Sgblack@eecs.umich.edu    GDBListener(BaseRemoteGDB *g, int p);
2672SN/A    ~GDBListener();
2682SN/A
2692SN/A    void accept();
2702SN/A    void listen();
271507SN/A    std::string name();
2722SN/A};
2732SN/A
2742SN/A#endif /* __REMOTE_GDB_H__ */
275