remote_gdb.hh revision 2107
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
29#ifndef __REMOTE_GDB_HH__
30#define __REMOTE_GDB_HH__
31
32#include <map>
33
34#include "base/kgdb.h"
35#include "cpu/pc_event.hh"
36#include "base/pollevent.hh"
37#include "base/socket.hh"
38
39class System;
40class ExecContext;
41class PhysicalMemory;
42
43class GDBListener;
44class RemoteGDB
45{
46  protected:
47    typedef TheISA::Addr Addr;
48    typedef TheISA::MachInst MachInst;
49  private:
50    friend void debugger();
51    friend class GDBListener;
52
53  protected:
54    class Event : public PollEvent
55    {
56      protected:
57        RemoteGDB *gdb;
58
59      public:
60        Event(RemoteGDB *g, int fd, int e);
61        void process(int revent);
62    };
63
64    friend class Event;
65    Event *event;
66    GDBListener *listener;
67    int number;
68
69  protected:
70    int fd;
71    uint64_t gdbregs[KGDB_NUMREGS];
72
73  protected:
74#ifdef notyet
75    label_t recover;
76#endif
77    bool active;
78    bool attached;
79
80    System *system;
81    PhysicalMemory *pmem;
82    ExecContext *context;
83
84  protected:
85    uint8_t getbyte();
86    void putbyte(uint8_t b);
87
88    int recv(char *data, int len);
89    void send(const char *data);
90
91  protected:
92    // Machine memory
93    bool read(Addr addr, size_t size, char *data);
94    bool write(Addr addr, size_t size, const char *data);
95
96    template <class T> T read(Addr addr);
97    template <class T> void write(Addr addr, T data);
98
99  public:
100    RemoteGDB(System *system, ExecContext *context);
101    ~RemoteGDB();
102
103    void replaceExecContext(ExecContext *xc) { context = xc; }
104
105    void attach(int fd);
106    void detach();
107    bool isattached();
108
109    bool acc(Addr addr, size_t len);
110    static int signal(int type);
111    bool trap(int type);
112
113  protected:
114    void getregs();
115    void setregs();
116
117    void clearSingleStep();
118    void setSingleStep();
119
120    PCEventQueue *getPcEventQueue();
121
122  protected:
123    class HardBreakpoint : public PCEvent
124    {
125      private:
126        RemoteGDB *gdb;
127
128      public:
129        int refcount;
130
131      public:
132        HardBreakpoint(RemoteGDB *_gdb, Addr addr);
133        std::string name() { return gdb->name() + ".hwbkpt"; }
134
135        virtual void process(ExecContext *xc);
136    };
137    friend class HardBreakpoint;
138
139    typedef std::map<Addr, HardBreakpoint *> break_map_t;
140    typedef break_map_t::iterator break_iter_t;
141    break_map_t hardBreakMap;
142
143    bool insertSoftBreak(Addr addr, size_t len);
144    bool removeSoftBreak(Addr addr, size_t len);
145    bool insertHardBreak(Addr addr, size_t len);
146    bool removeHardBreak(Addr addr, size_t len);
147
148  protected:
149    struct TempBreakpoint {
150        Addr	address;		// set here
151        MachInst	bkpt_inst;		// saved instruction at bkpt
152        int		init_count;		// number of times to skip bkpt
153        int		count;			// current count
154    };
155
156    TempBreakpoint notTakenBkpt;
157    TempBreakpoint takenBkpt;
158
159    void clearTempBreakpoint(TempBreakpoint &bkpt);
160    void setTempBreakpoint(TempBreakpoint &bkpt, Addr addr);
161
162  public:
163    std::string name();
164};
165
166template <class T>
167inline T
168RemoteGDB::read(Addr addr)
169{
170    T temp;
171    read(addr, sizeof(T), (char *)&temp);
172    return temp;
173}
174
175template <class T>
176inline void
177RemoteGDB::write(Addr addr, T data)
178{ write(addr, sizeof(T), (const char *)&data); }
179
180class GDBListener
181{
182  protected:
183    class Event : public PollEvent
184    {
185      protected:
186        GDBListener *listener;
187
188      public:
189        Event(GDBListener *l, int fd, int e);
190        void process(int revent);
191    };
192
193    friend class Event;
194    Event *event;
195
196  protected:
197    ListenSocket listener;
198    RemoteGDB *gdb;
199    int port;
200
201  public:
202    GDBListener(RemoteGDB *g, int p);
203    ~GDBListener();
204
205    void accept();
206    void listen();
207    std::string name();
208};
209
210#endif /* __REMOTE_GDB_H__ */
211