2 * Copyright 2015 LabWare
3 * Copyright 2014 Google, Inc.
4 * Copyright (c) 2002-2005 The Regents of The University of Michigan
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are
9 * met: redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer;
11 * redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution;
14 * neither the name of the copyright holders nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 * Authors: Nathan Binkert
34 #ifndef __REMOTE_GDB_HH__
35 #define __REMOTE_GDB_HH__
37 #include <sys/signal.h>
43 #include "arch/types.hh"
44 #include "base/intmath.hh"
45 #include "base/pollevent.hh"
46 #include "base/socket.hh"
47 #include "cpu/pc_event.hh"
61 const GdbCommand *cmd;
68 typedef bool (BaseRemoteGDB::*Func)(Context &ctx);
70 const char * const name;
73 GdbCommand(const char *_name, Func _func) : name(_name), func(_func)
80 friend void debugger();
81 friend class GDBListener;
84 /// Exception to throw when the connection to the client is broken.
88 BadClient(const char *_warning=NULL) : warning(_warning)
91 /// Exception to throw when an error needs to be reported to the client.
95 CmdError(std::string _error) : error(_error)
98 /// Exception to throw when something isn't supported.
105 Addr hex2i(const char **);
106 // Address formats, break types, and gdb commands may change
107 // between architectures, so they're defined as virtual
109 virtual void mem2hex(char *, const char *, int);
110 virtual const char * hex2mem(char *, const char *, int);
111 virtual const char * break_type(char c);
114 static std::map<char, GdbCommand> command_map;
116 bool cmd_unsupported(GdbCommand::Context &ctx);
118 bool cmd_signal(GdbCommand::Context &ctx);
119 bool cmd_cont(GdbCommand::Context &ctx);
120 bool cmd_async_cont(GdbCommand::Context &ctx);
121 bool cmd_detach(GdbCommand::Context &ctx);
122 bool cmd_reg_r(GdbCommand::Context &ctx);
123 bool cmd_reg_w(GdbCommand::Context &ctx);
124 bool cmd_set_thread(GdbCommand::Context &ctx);
125 bool cmd_mem_r(GdbCommand::Context &ctx);
126 bool cmd_mem_w(GdbCommand::Context &ctx);
127 bool cmd_query_var(GdbCommand::Context &ctx);
128 bool cmd_step(GdbCommand::Context &ctx);
129 bool cmd_async_step(GdbCommand::Context &ctx);
130 bool cmd_clr_hw_bkpt(GdbCommand::Context &ctx);
131 bool cmd_set_hw_bkpt(GdbCommand::Context &ctx);
134 class InputEvent : public PollEvent
140 InputEvent(BaseRemoteGDB *g, int fd, int e);
141 void process(int revent);
144 class TrapEvent : public Event
151 TrapEvent(BaseRemoteGDB *g) : gdb(g)
154 void type(int t) { _type = t; }
158 friend class InputEvent;
159 InputEvent *inputEvent;
161 GDBListener *listener;
165 // The socket commands come in through
173 ThreadContext *context;
177 * Concrete subclasses of this abstract class represent how the
178 * register values are transmitted on the wire. Usually each
179 * architecture should define one subclass, but there can be more
180 * if there is more than one possible wire format. For example,
181 * ARM defines both AArch32GdbRegCache and AArch64GdbRegCache.
183 class BaseGdbRegCache
188 * Return the pointer to the raw bytes buffer containing the
189 * register values. Each byte of this buffer is literally
190 * encoded as two hex digits in the g or G RSP packet.
192 virtual char *data() const = 0;
195 * Return the size of the raw buffer, in bytes
196 * (i.e., half of the number of digits in the g/G packet).
198 virtual size_t size() const = 0;
201 * Fill the raw buffer from the registers in the ThreadContext.
203 virtual void getRegs(ThreadContext*) = 0;
206 * Set the ThreadContext's registers from the values
209 virtual void setRegs(ThreadContext*) const = 0;
212 * Return the name to use in places like DPRINTF.
213 * Having each concrete superclass redefine this member
214 * is useful in situations where the class of the regCache
215 * can change on the fly.
217 virtual const std::string name() const = 0;
219 BaseGdbRegCache(BaseRemoteGDB *g) : gdb(g)
221 virtual ~BaseGdbRegCache()
228 BaseGdbRegCache *regCachePtr;
232 void putbyte(uint8_t b);
234 int recv(char *data, int len);
235 void send(const char *data);
239 virtual bool read(Addr addr, size_t size, char *data);
240 virtual bool write(Addr addr, size_t size, const char *data);
242 template <class T> T read(Addr addr);
243 template <class T> void write(Addr addr, T data);
246 BaseRemoteGDB(System *system, ThreadContext *context);
247 virtual ~BaseRemoteGDB();
248 virtual BaseGdbRegCache *gdbRegs() = 0;
250 void replaceThreadContext(ThreadContext *tc) { context = tc; }
256 virtual bool acc(Addr addr, size_t len) = 0;
258 virtual bool breakpoint()
260 return trap(SIGTRAP);
263 void processSingleStepEvent();
264 EventFunctionWrapper singleStepEvent;
266 void clearSingleStep();
267 void setSingleStep();
269 PCEventQueue *getPcEventQueue();
270 EventQueue *getComInstEventQueue();
272 /// Schedule an event which will be triggered "delta" instructions later.
273 void scheduleInstCommitEvent(Event *ev, int delta);
274 /// Deschedule an instruction count based event.
275 void descheduleInstCommitEvent(Event *ev);
278 virtual bool checkBpLen(size_t len);
280 class HardBreakpoint : public PCEvent
289 HardBreakpoint(BaseRemoteGDB *_gdb, Addr addr);
290 const std::string name() const { return gdb->name() + ".hwbkpt"; }
292 virtual void process(ThreadContext *tc);
294 friend class HardBreakpoint;
296 typedef std::map<Addr, HardBreakpoint *> break_map_t;
297 typedef break_map_t::iterator break_iter_t;
298 break_map_t hardBreakMap;
300 void insertSoftBreak(Addr addr, size_t len);
301 void removeSoftBreak(Addr addr, size_t len);
302 virtual void insertHardBreak(Addr addr, size_t len);
303 void removeHardBreak(Addr addr, size_t len);
306 void clearTempBreakpoint(Addr &bkpt);
307 void setTempBreakpoint(Addr bkpt);
315 BaseRemoteGDB::read(Addr addr)
318 read(addr, sizeof(T), (char *)&temp);
324 BaseRemoteGDB::write(Addr addr, T data)
325 { write(addr, sizeof(T), (const char *)&data); }
330 class InputEvent : public PollEvent
333 GDBListener *listener;
336 InputEvent(GDBListener *l, int fd, int e);
337 void process(int revent);
340 friend class InputEvent;
341 InputEvent *inputEvent;
344 ListenSocket listener;
349 GDBListener(BaseRemoteGDB *g, int p);
359 #endif /* __REMOTE_GDB_H__ */