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