remote_gdb.hh revision 3579:e9976b62c259
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 <map>
35
36#include "arch/types.hh"
37#include "cpu/pc_event.hh"
38#include "base/pollevent.hh"
39#include "base/socket.hh"
40
41class System;
42class ThreadContext;
43class PhysicalMemory;
44
45class GDBListener;
46
47enum GDBCommands
48{
49    GDBSignal              = '?', // last signal
50    GDBSetBaud             = 'b', // set baud (depracated)
51    GDBSetBreak            = 'B', // set breakpoint (depracated)
52    GDBCont                = 'c', // resume
53    GDBAsyncCont           = 'C', // continue with signal
54    GDBDebug               = 'd', // toggle debug flags (deprecated)
55    GDBDetach              = 'D', // detach remote gdb
56    GDBRegR                = 'g', // read general registers
57    GDBRegW                = 'G', // write general registers
58    GDBSetThread           = 'H', // set thread
59    GDBCycleStep           = 'i', // step a single cycle
60    GDBSigCycleStep        = 'I', // signal then cycle step
61    GDBKill                = 'k', // kill program
62    GDBMemR                = 'm', // read memory
63    GDBMemW                = 'M', // write memory
64    GDBReadReg             = 'p', // read register
65    GDBSetReg              = 'P', // write register
66    GDBQueryVar            = 'q', // query variable
67    GDBSetVar              = 'Q', // set variable
68    GDBReset               = 'r', // reset system.  (Deprecated)
69    GDBStep                = 's', // step
70    GDBAsyncStep           = 'S', // signal and step
71    GDBThreadAlive         = 'T', // find out if the thread is alive
72    GDBTargetExit          = 'W', // target exited
73    GDBBinaryDload         = 'X', // write memory
74    GDBClrHwBkpt           = 'z', // remove breakpoint or watchpoint
75    GDBSetHwBkpt           = 'Z'  // insert breakpoint or watchpoint
76};
77
78const char GDBStart = '$';
79const char GDBEnd = '#';
80const char GDBGoodP = '+';
81const char GDBBadP = '-';
82
83const int GDBPacketBufLen = 1024;
84
85class BaseRemoteGDB
86{
87  private:
88    friend void debugger();
89    friend class GDBListener;
90
91    //Helper functions
92  protected:
93    int digit2i(char);
94    char i2digit(int);
95    Addr hex2i(const char **);
96    //Address formats, break types, and gdb commands may change
97    //between architectures, so they're defined as virtual
98    //functions.
99    virtual void mem2hex(void *, const void *, int);
100    virtual const char * hex2mem(void *, const char *, int);
101    virtual const char * break_type(char c);
102    virtual const char * gdb_command(char cmd);
103
104  protected:
105    class Event : public PollEvent
106    {
107      protected:
108        BaseRemoteGDB *gdb;
109
110      public:
111        Event(BaseRemoteGDB *g, int fd, int e);
112        void process(int revent);
113    };
114
115    friend class Event;
116    Event *event;
117    GDBListener *listener;
118    int number;
119
120  protected:
121    //The socket commands come in through
122    int fd;
123
124  protected:
125#ifdef notyet
126    label_t recover;
127#endif
128    bool active;
129    bool attached;
130
131    System *system;
132    PhysicalMemory *pmem;
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
181  protected:
182    virtual void getregs() = 0;
183    virtual void setregs() = 0;
184
185    virtual void clearSingleStep() = 0;
186    virtual void setSingleStep() = 0;
187
188    PCEventQueue *getPcEventQueue();
189
190  protected:
191    class HardBreakpoint : public PCEvent
192    {
193      private:
194        BaseRemoteGDB *gdb;
195
196      public:
197        int refcount;
198
199      public:
200        HardBreakpoint(BaseRemoteGDB *_gdb, Addr addr);
201        std::string name() { return gdb->name() + ".hwbkpt"; }
202
203        virtual void process(ThreadContext *tc);
204    };
205    friend class HardBreakpoint;
206
207    typedef std::map<Addr, HardBreakpoint *> break_map_t;
208    typedef break_map_t::iterator break_iter_t;
209    break_map_t hardBreakMap;
210
211    bool insertSoftBreak(Addr addr, size_t len);
212    bool removeSoftBreak(Addr addr, size_t len);
213    bool insertHardBreak(Addr addr, size_t len);
214    bool removeHardBreak(Addr addr, size_t len);
215
216  protected:
217    void clearTempBreakpoint(Addr &bkpt);
218    void setTempBreakpoint(Addr bkpt);
219
220  public:
221    std::string name();
222};
223
224template <class T>
225inline T
226BaseRemoteGDB::read(Addr addr)
227{
228    T temp;
229    read(addr, sizeof(T), (char *)&temp);
230    return temp;
231}
232
233template <class T>
234inline void
235BaseRemoteGDB::write(Addr addr, T data)
236{ write(addr, sizeof(T), (const char *)&data); }
237
238class GDBListener
239{
240  protected:
241    class Event : public PollEvent
242    {
243      protected:
244        GDBListener *listener;
245
246      public:
247        Event(GDBListener *l, int fd, int e);
248        void process(int revent);
249    };
250
251    friend class Event;
252    Event *event;
253
254  protected:
255    ListenSocket listener;
256    BaseRemoteGDB *gdb;
257    int port;
258
259  public:
260    GDBListener(BaseRemoteGDB *g, int p);
261    ~GDBListener();
262
263    void accept();
264    void listen();
265    std::string name();
266};
267
268#endif /* __REMOTE_GDB_H__ */
269