remote_gdb.hh revision 8931:7a1dfb191e3f
1/*
2 * Copyright (c) 2002-2005 The Regents of The University of Michigan
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 *
28 * Authors: Nathan Binkert
29 */
30
31#ifndef __REMOTE_GDB_HH__
32#define __REMOTE_GDB_HH__
33
34#include <sys/signal.h>
35
36#include <map>
37
38#include "arch/types.hh"
39#include "base/pollevent.hh"
40#include "base/socket.hh"
41#include "cpu/pc_event.hh"
42
43class System;
44class ThreadContext;
45
46class GDBListener;
47
48enum GDBCommands
49{
50    GDBSignal              = '?', // last signal
51    GDBSetBaud             = 'b', // set baud (depracated)
52    GDBSetBreak            = 'B', // set breakpoint (depracated)
53    GDBCont                = 'c', // resume
54    GDBAsyncCont           = 'C', // continue with signal
55    GDBDebug               = 'd', // toggle debug flags (deprecated)
56    GDBDetach              = 'D', // detach remote gdb
57    GDBRegR                = 'g', // read general registers
58    GDBRegW                = 'G', // write general registers
59    GDBSetThread           = 'H', // set thread
60    GDBCycleStep           = 'i', // step a single cycle
61    GDBSigCycleStep        = 'I', // signal then cycle step
62    GDBKill                = 'k', // kill program
63    GDBMemR                = 'm', // read memory
64    GDBMemW                = 'M', // write memory
65    GDBReadReg             = 'p', // read register
66    GDBSetReg              = 'P', // write register
67    GDBQueryVar            = 'q', // query variable
68    GDBSetVar              = 'Q', // set variable
69    GDBReset               = 'r', // reset system.  (Deprecated)
70    GDBStep                = 's', // step
71    GDBAsyncStep           = 'S', // signal and step
72    GDBThreadAlive         = 'T', // find out if the thread is alive
73    GDBTargetExit          = 'W', // target exited
74    GDBBinaryDload         = 'X', // write memory
75    GDBClrHwBkpt           = 'z', // remove breakpoint or watchpoint
76    GDBSetHwBkpt           = 'Z'  // insert breakpoint or watchpoint
77};
78
79const char GDBStart = '$';
80const char GDBEnd = '#';
81const char GDBGoodP = '+';
82const char GDBBadP = '-';
83
84const int GDBPacketBufLen = 1024;
85
86class BaseRemoteGDB
87{
88  private:
89    friend void debugger();
90    friend class GDBListener;
91
92    //Helper functions
93  protected:
94    int digit2i(char);
95    char i2digit(int);
96    Addr hex2i(const char **);
97    //Address formats, break types, and gdb commands may change
98    //between architectures, so they're defined as virtual
99    //functions.
100    virtual void mem2hex(void *, const void *, int);
101    virtual const char * hex2mem(void *, const char *, int);
102    virtual const char * break_type(char c);
103    virtual const char * gdb_command(char cmd);
104
105  protected:
106    class Event : public PollEvent
107    {
108      protected:
109        BaseRemoteGDB *gdb;
110
111      public:
112        Event(BaseRemoteGDB *g, int fd, int e);
113        void process(int revent);
114    };
115
116    friend class Event;
117    Event *event;
118    GDBListener *listener;
119    int number;
120
121  protected:
122    //The socket commands come in through
123    int fd;
124
125  protected:
126#ifdef notyet
127    label_t recover;
128#endif
129    bool active;
130    bool attached;
131
132    System *system;
133    ThreadContext *context;
134
135  protected:
136    class GdbRegCache
137    {
138      public:
139        GdbRegCache(size_t newSize) : regs(new uint64_t[newSize]), size(newSize)
140        {}
141        ~GdbRegCache()
142        {
143            delete [] regs;
144        }
145
146        uint64_t * regs;
147        size_t size;
148        size_t bytes() { return size * sizeof(uint64_t); }
149    };
150
151    GdbRegCache gdbregs;
152
153  protected:
154    uint8_t getbyte();
155    void putbyte(uint8_t b);
156
157    int recv(char *data, int len);
158    void send(const char *data);
159
160  protected:
161    // Machine memory
162    virtual bool read(Addr addr, size_t size, char *data);
163    virtual bool write(Addr addr, size_t size, const char *data);
164
165    template <class T> T read(Addr addr);
166    template <class T> void write(Addr addr, T data);
167
168  public:
169    BaseRemoteGDB(System *system, ThreadContext *context, size_t cacheSize);
170    virtual ~BaseRemoteGDB();
171
172    void replaceThreadContext(ThreadContext *tc) { context = tc; }
173
174    void attach(int fd);
175    void detach();
176    bool isattached();
177
178    virtual bool acc(Addr addr, size_t len) = 0;
179    bool trap(int type);
180    virtual bool breakpoint()
181    {
182        return trap(SIGTRAP);
183    }
184
185  protected:
186    virtual void getregs() = 0;
187    virtual void setregs() = 0;
188
189    virtual void clearSingleStep() = 0;
190    virtual void setSingleStep() = 0;
191
192    PCEventQueue *getPcEventQueue();
193
194  protected:
195    class HardBreakpoint : public PCEvent
196    {
197      private:
198        BaseRemoteGDB *gdb;
199
200      public:
201        int refcount;
202
203      public:
204        HardBreakpoint(BaseRemoteGDB *_gdb, Addr addr);
205        const std::string name() const { return gdb->name() + ".hwbkpt"; }
206
207        virtual void process(ThreadContext *tc);
208    };
209    friend class HardBreakpoint;
210
211    typedef std::map<Addr, HardBreakpoint *> break_map_t;
212    typedef break_map_t::iterator break_iter_t;
213    break_map_t hardBreakMap;
214
215    bool insertSoftBreak(Addr addr, size_t len);
216    bool removeSoftBreak(Addr addr, size_t len);
217    virtual bool insertHardBreak(Addr addr, size_t len);
218    bool removeHardBreak(Addr addr, size_t len);
219
220  protected:
221    void clearTempBreakpoint(Addr &bkpt);
222    void setTempBreakpoint(Addr bkpt);
223
224  public:
225    std::string name();
226};
227
228template <class T>
229inline T
230BaseRemoteGDB::read(Addr addr)
231{
232    T temp;
233    read(addr, sizeof(T), (char *)&temp);
234    return temp;
235}
236
237template <class T>
238inline void
239BaseRemoteGDB::write(Addr addr, T data)
240{ write(addr, sizeof(T), (const char *)&data); }
241
242class GDBListener
243{
244  protected:
245    class Event : public PollEvent
246    {
247      protected:
248        GDBListener *listener;
249
250      public:
251        Event(GDBListener *l, int fd, int e);
252        void process(int revent);
253    };
254
255    friend class Event;
256    Event *event;
257
258  protected:
259    ListenSocket listener;
260    BaseRemoteGDB *gdb;
261    int port;
262
263  public:
264    GDBListener(BaseRemoteGDB *g, int p);
265    ~GDBListener();
266
267    void accept();
268    void listen();
269    std::string name();
270};
271
272#endif /* __REMOTE_GDB_H__ */
273