remote_gdb.hh revision 10589
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/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;
1332680Sktlim@umich.edu    ThreadContext *context;
1342SN/A
1352SN/A  protected:
1363536Sgblack@eecs.umich.edu    class GdbRegCache
1373536Sgblack@eecs.umich.edu    {
1383536Sgblack@eecs.umich.edu      public:
1393536Sgblack@eecs.umich.edu        GdbRegCache(size_t newSize) : regs(new uint64_t[newSize]), size(newSize)
1403536Sgblack@eecs.umich.edu        {}
1413536Sgblack@eecs.umich.edu        ~GdbRegCache()
1423536Sgblack@eecs.umich.edu        {
1433536Sgblack@eecs.umich.edu            delete [] regs;
1443536Sgblack@eecs.umich.edu        }
1453536Sgblack@eecs.umich.edu
1463536Sgblack@eecs.umich.edu        uint64_t * regs;
1473536Sgblack@eecs.umich.edu        size_t size;
1483579Sgblack@eecs.umich.edu        size_t bytes() { return size * sizeof(uint64_t); }
1493536Sgblack@eecs.umich.edu    };
1503536Sgblack@eecs.umich.edu
1513536Sgblack@eecs.umich.edu    GdbRegCache gdbregs;
1523536Sgblack@eecs.umich.edu
1533536Sgblack@eecs.umich.edu  protected:
1542SN/A    uint8_t getbyte();
1552SN/A    void putbyte(uint8_t b);
1562SN/A
1572SN/A    int recv(char *data, int len);
1582SN/A    void send(const char *data);
1592SN/A
1602SN/A  protected:
1612SN/A    // Machine memory
1623536Sgblack@eecs.umich.edu    virtual bool read(Addr addr, size_t size, char *data);
1633536Sgblack@eecs.umich.edu    virtual bool write(Addr addr, size_t size, const char *data);
1642SN/A
1652SN/A    template <class T> T read(Addr addr);
1662SN/A    template <class T> void write(Addr addr, T data);
1672SN/A
1682SN/A  public:
1693536Sgblack@eecs.umich.edu    BaseRemoteGDB(System *system, ThreadContext *context, size_t cacheSize);
1703536Sgblack@eecs.umich.edu    virtual ~BaseRemoteGDB();
1712SN/A
1722680Sktlim@umich.edu    void replaceThreadContext(ThreadContext *tc) { context = tc; }
173180SN/A
1742SN/A    void attach(int fd);
1752SN/A    void detach();
1762SN/A    bool isattached();
1772SN/A
1783536Sgblack@eecs.umich.edu    virtual bool acc(Addr addr, size_t len) = 0;
1792SN/A    bool trap(int type);
1803960Sgblack@eecs.umich.edu    virtual bool breakpoint()
1813960Sgblack@eecs.umich.edu    {
1823960Sgblack@eecs.umich.edu        return trap(SIGTRAP);
1833960Sgblack@eecs.umich.edu    }
1842SN/A
1852SN/A  protected:
1863536Sgblack@eecs.umich.edu    virtual void getregs() = 0;
1873536Sgblack@eecs.umich.edu    virtual void setregs() = 0;
1882SN/A
1893536Sgblack@eecs.umich.edu    virtual void clearSingleStep() = 0;
1903536Sgblack@eecs.umich.edu    virtual void setSingleStep() = 0;
1912SN/A
1922SN/A    PCEventQueue *getPcEventQueue();
1932SN/A
1942SN/A  protected:
19510589Sgabeblack@google.com    virtual bool checkBpLen(size_t len);
19610589Sgabeblack@google.com
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);
2078737Skoansin.tan@gmail.com        const std::string name() const { 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);
2198700Ssteve.reinhardt@amd.com    virtual 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