remote_gdb.hh revision 10595
12SN/A/*
210595Sgabeblack@google.com * Copyright 2014 Google, Inc.
31762SN/A * Copyright (c) 2002-2005 The Regents of The University of Michigan
42SN/A * All rights reserved.
52SN/A *
62SN/A * Redistribution and use in source and binary forms, with or without
72SN/A * modification, are permitted provided that the following conditions are
82SN/A * met: redistributions of source code must retain the above copyright
92SN/A * notice, this list of conditions and the following disclaimer;
102SN/A * redistributions in binary form must reproduce the above copyright
112SN/A * notice, this list of conditions and the following disclaimer in the
122SN/A * documentation and/or other materials provided with the distribution;
132SN/A * neither the name of the copyright holders nor the names of its
142SN/A * contributors may be used to endorse or promote products derived from
152SN/A * this software without specific prior written permission.
162SN/A *
172SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
182SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
192SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
202SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
212SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
222SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
232SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
242SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
252SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
262SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
272SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
282665Ssaidi@eecs.umich.edu *
292665Ssaidi@eecs.umich.edu * Authors: Nathan Binkert
302SN/A */
312SN/A
322SN/A#ifndef __REMOTE_GDB_HH__
332SN/A#define __REMOTE_GDB_HH__
342SN/A
353960Sgblack@eecs.umich.edu#include <sys/signal.h>
3677SN/A
378229Snate@binkert.org#include <map>
388229Snate@binkert.org
392986Sgblack@eecs.umich.edu#include "arch/types.hh"
4010595Sgabeblack@google.com#include "base/intmath.hh"
4156SN/A#include "base/pollevent.hh"
4256SN/A#include "base/socket.hh"
438229Snate@binkert.org#include "cpu/pc_event.hh"
442SN/A
452SN/Aclass System;
462680Sktlim@umich.educlass ThreadContext;
472SN/A
481910SN/Aclass GDBListener;
493536Sgblack@eecs.umich.edu
503536Sgblack@eecs.umich.eduenum GDBCommands
512SN/A{
523536Sgblack@eecs.umich.edu    GDBSignal              = '?', // last signal
533536Sgblack@eecs.umich.edu    GDBSetBaud             = 'b', // set baud (depracated)
543536Sgblack@eecs.umich.edu    GDBSetBreak            = 'B', // set breakpoint (depracated)
553536Sgblack@eecs.umich.edu    GDBCont                = 'c', // resume
563536Sgblack@eecs.umich.edu    GDBAsyncCont           = 'C', // continue with signal
573536Sgblack@eecs.umich.edu    GDBDebug               = 'd', // toggle debug flags (deprecated)
583536Sgblack@eecs.umich.edu    GDBDetach              = 'D', // detach remote gdb
593536Sgblack@eecs.umich.edu    GDBRegR                = 'g', // read general registers
603536Sgblack@eecs.umich.edu    GDBRegW                = 'G', // write general registers
613536Sgblack@eecs.umich.edu    GDBSetThread           = 'H', // set thread
623536Sgblack@eecs.umich.edu    GDBCycleStep           = 'i', // step a single cycle
633536Sgblack@eecs.umich.edu    GDBSigCycleStep        = 'I', // signal then cycle step
643536Sgblack@eecs.umich.edu    GDBKill                = 'k', // kill program
653536Sgblack@eecs.umich.edu    GDBMemR                = 'm', // read memory
663536Sgblack@eecs.umich.edu    GDBMemW                = 'M', // write memory
673536Sgblack@eecs.umich.edu    GDBReadReg             = 'p', // read register
683536Sgblack@eecs.umich.edu    GDBSetReg              = 'P', // write register
693536Sgblack@eecs.umich.edu    GDBQueryVar            = 'q', // query variable
703536Sgblack@eecs.umich.edu    GDBSetVar              = 'Q', // set variable
713536Sgblack@eecs.umich.edu    GDBReset               = 'r', // reset system.  (Deprecated)
723536Sgblack@eecs.umich.edu    GDBStep                = 's', // step
733536Sgblack@eecs.umich.edu    GDBAsyncStep           = 'S', // signal and step
743536Sgblack@eecs.umich.edu    GDBThreadAlive         = 'T', // find out if the thread is alive
753536Sgblack@eecs.umich.edu    GDBTargetExit          = 'W', // target exited
763536Sgblack@eecs.umich.edu    GDBBinaryDload         = 'X', // write memory
773536Sgblack@eecs.umich.edu    GDBClrHwBkpt           = 'z', // remove breakpoint or watchpoint
783536Sgblack@eecs.umich.edu    GDBSetHwBkpt           = 'Z'  // insert breakpoint or watchpoint
793536Sgblack@eecs.umich.edu};
803536Sgblack@eecs.umich.edu
813536Sgblack@eecs.umich.educonst char GDBStart = '$';
823536Sgblack@eecs.umich.educonst char GDBEnd = '#';
833536Sgblack@eecs.umich.educonst char GDBGoodP = '+';
843536Sgblack@eecs.umich.educonst char GDBBadP = '-';
853536Sgblack@eecs.umich.edu
863536Sgblack@eecs.umich.educonst int GDBPacketBufLen = 1024;
873536Sgblack@eecs.umich.edu
883536Sgblack@eecs.umich.educlass BaseRemoteGDB
893536Sgblack@eecs.umich.edu{
901910SN/A  private:
911910SN/A    friend void debugger();
921910SN/A    friend class GDBListener;
931910SN/A
943536Sgblack@eecs.umich.edu    //Helper functions
953536Sgblack@eecs.umich.edu  protected:
963536Sgblack@eecs.umich.edu    int digit2i(char);
973536Sgblack@eecs.umich.edu    char i2digit(int);
983536Sgblack@eecs.umich.edu    Addr hex2i(const char **);
993536Sgblack@eecs.umich.edu    //Address formats, break types, and gdb commands may change
1003536Sgblack@eecs.umich.edu    //between architectures, so they're defined as virtual
1013536Sgblack@eecs.umich.edu    //functions.
1023536Sgblack@eecs.umich.edu    virtual void mem2hex(void *, const void *, int);
1033536Sgblack@eecs.umich.edu    virtual const char * hex2mem(void *, const char *, int);
1043536Sgblack@eecs.umich.edu    virtual const char * break_type(char c);
1053536Sgblack@eecs.umich.edu    virtual const char * gdb_command(char cmd);
1063536Sgblack@eecs.umich.edu
1072SN/A  protected:
1082SN/A    class Event : public PollEvent
1092SN/A    {
1102SN/A      protected:
1113536Sgblack@eecs.umich.edu        BaseRemoteGDB *gdb;
1122SN/A
1132SN/A      public:
1143536Sgblack@eecs.umich.edu        Event(BaseRemoteGDB *g, int fd, int e);
1152SN/A        void process(int revent);
1162SN/A    };
1172SN/A
1182SN/A    friend class Event;
1192SN/A    Event *event;
1201910SN/A    GDBListener *listener;
1211910SN/A    int number;
1222SN/A
1232SN/A  protected:
1243536Sgblack@eecs.umich.edu    //The socket commands come in through
1252SN/A    int fd;
1262SN/A
1272SN/A  protected:
1282SN/A#ifdef notyet
1292SN/A    label_t recover;
1302SN/A#endif
1312SN/A    bool active;
1322SN/A    bool attached;
1332SN/A
1342SN/A    System *system;
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:
14110595Sgabeblack@google.com        GdbRegCache(size_t newSize) :
14210595Sgabeblack@google.com            regs64(new uint64_t[divCeil(newSize, sizeof(uint64_t))]),
14310595Sgabeblack@google.com            size(newSize)
1443536Sgblack@eecs.umich.edu        {}
1453536Sgblack@eecs.umich.edu        ~GdbRegCache()
1463536Sgblack@eecs.umich.edu        {
14710595Sgabeblack@google.com            delete [] regs64;
1483536Sgblack@eecs.umich.edu        }
1493536Sgblack@eecs.umich.edu
15010595Sgabeblack@google.com        union {
15110595Sgabeblack@google.com            uint64_t *regs64;
15210595Sgabeblack@google.com            uint32_t *regs32;
15310595Sgabeblack@google.com            uint16_t *regs16;
15410595Sgabeblack@google.com            uint8_t *regs8;
15510595Sgabeblack@google.com            void *regs;
15610595Sgabeblack@google.com        };
15710595Sgabeblack@google.com        // Size of cache in bytes.
1583536Sgblack@eecs.umich.edu        size_t size;
15910595Sgabeblack@google.com        size_t bytes() { return size; }
1603536Sgblack@eecs.umich.edu    };
1613536Sgblack@eecs.umich.edu
1623536Sgblack@eecs.umich.edu    GdbRegCache gdbregs;
1633536Sgblack@eecs.umich.edu
1643536Sgblack@eecs.umich.edu  protected:
1652SN/A    uint8_t getbyte();
1662SN/A    void putbyte(uint8_t b);
1672SN/A
1682SN/A    int recv(char *data, int len);
1692SN/A    void send(const char *data);
1702SN/A
1712SN/A  protected:
1722SN/A    // Machine memory
1733536Sgblack@eecs.umich.edu    virtual bool read(Addr addr, size_t size, char *data);
1743536Sgblack@eecs.umich.edu    virtual bool write(Addr addr, size_t size, const char *data);
1752SN/A
1762SN/A    template <class T> T read(Addr addr);
1772SN/A    template <class T> void write(Addr addr, T data);
1782SN/A
1792SN/A  public:
1803536Sgblack@eecs.umich.edu    BaseRemoteGDB(System *system, ThreadContext *context, size_t cacheSize);
1813536Sgblack@eecs.umich.edu    virtual ~BaseRemoteGDB();
1822SN/A
1832680Sktlim@umich.edu    void replaceThreadContext(ThreadContext *tc) { context = tc; }
184180SN/A
1852SN/A    void attach(int fd);
1862SN/A    void detach();
1872SN/A    bool isattached();
1882SN/A
1893536Sgblack@eecs.umich.edu    virtual bool acc(Addr addr, size_t len) = 0;
1902SN/A    bool trap(int type);
1913960Sgblack@eecs.umich.edu    virtual bool breakpoint()
1923960Sgblack@eecs.umich.edu    {
1933960Sgblack@eecs.umich.edu        return trap(SIGTRAP);
1943960Sgblack@eecs.umich.edu    }
1952SN/A
1962SN/A  protected:
1973536Sgblack@eecs.umich.edu    virtual void getregs() = 0;
1983536Sgblack@eecs.umich.edu    virtual void setregs() = 0;
1992SN/A
2003536Sgblack@eecs.umich.edu    virtual void clearSingleStep() = 0;
2013536Sgblack@eecs.umich.edu    virtual void setSingleStep() = 0;
2022SN/A
2032SN/A    PCEventQueue *getPcEventQueue();
2042SN/A
2052SN/A  protected:
20610589Sgabeblack@google.com    virtual bool checkBpLen(size_t len);
20710589Sgabeblack@google.com
2082SN/A    class HardBreakpoint : public PCEvent
2092SN/A    {
2102SN/A      private:
2113536Sgblack@eecs.umich.edu        BaseRemoteGDB *gdb;
2122SN/A
2132SN/A      public:
214507SN/A        int refcount;
215507SN/A
216507SN/A      public:
2173536Sgblack@eecs.umich.edu        HardBreakpoint(BaseRemoteGDB *_gdb, Addr addr);
2188737Skoansin.tan@gmail.com        const std::string name() const { return gdb->name() + ".hwbkpt"; }
2192SN/A
2202680Sktlim@umich.edu        virtual void process(ThreadContext *tc);
2212SN/A    };
2222SN/A    friend class HardBreakpoint;
2232SN/A
2242SN/A    typedef std::map<Addr, HardBreakpoint *> break_map_t;
2252SN/A    typedef break_map_t::iterator break_iter_t;
2262SN/A    break_map_t hardBreakMap;
2272SN/A
2282SN/A    bool insertSoftBreak(Addr addr, size_t len);
2292SN/A    bool removeSoftBreak(Addr addr, size_t len);
2308700Ssteve.reinhardt@amd.com    virtual bool insertHardBreak(Addr addr, size_t len);
2312SN/A    bool removeHardBreak(Addr addr, size_t len);
2322SN/A
2333550Sgblack@eecs.umich.edu  protected:
2343550Sgblack@eecs.umich.edu    void clearTempBreakpoint(Addr &bkpt);
2353550Sgblack@eecs.umich.edu    void setTempBreakpoint(Addr bkpt);
2363550Sgblack@eecs.umich.edu
237507SN/A  public:
238507SN/A    std::string name();
2392SN/A};
2402SN/A
2412SN/Atemplate <class T>
2422SN/Ainline T
2433536Sgblack@eecs.umich.eduBaseRemoteGDB::read(Addr addr)
2442SN/A{
2452SN/A    T temp;
2462SN/A    read(addr, sizeof(T), (char *)&temp);
2472SN/A    return temp;
2482SN/A}
2492SN/A
2502SN/Atemplate <class T>
2512SN/Ainline void
2523536Sgblack@eecs.umich.eduBaseRemoteGDB::write(Addr addr, T data)
2532SN/A{ write(addr, sizeof(T), (const char *)&data); }
2542SN/A
2552SN/Aclass GDBListener
2562SN/A{
2572SN/A  protected:
2582SN/A    class Event : public PollEvent
2592SN/A    {
2602SN/A      protected:
2612SN/A        GDBListener *listener;
2622SN/A
2632SN/A      public:
2642SN/A        Event(GDBListener *l, int fd, int e);
2652SN/A        void process(int revent);
2662SN/A    };
2672SN/A
2682SN/A    friend class Event;
2692SN/A    Event *event;
2702SN/A
2712SN/A  protected:
2722SN/A    ListenSocket listener;
2733536Sgblack@eecs.umich.edu    BaseRemoteGDB *gdb;
2742SN/A    int port;
2752SN/A
2762SN/A  public:
2773536Sgblack@eecs.umich.edu    GDBListener(BaseRemoteGDB *g, int p);
2782SN/A    ~GDBListener();
2792SN/A
2802SN/A    void accept();
2812SN/A    void listen();
282507SN/A    std::string name();
2832SN/A};
2842SN/A
2852SN/A#endif /* __REMOTE_GDB_H__ */
286