gem5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
remote_gdb.hh
Go to the documentation of this file.
1 /*
2  * Copyright 2015 LabWare
3  * Copyright 2014 Google, Inc.
4  * Copyright (c) 2002-2005 The Regents of The University of Michigan
5  * All rights reserved.
6  *
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.
17  *
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.
29  *
30  * Authors: Nathan Binkert
31  * Boris Shingarov
32  */
33 
34 #ifndef __REMOTE_GDB_HH__
35 #define __REMOTE_GDB_HH__
36 
37 #include <sys/signal.h>
38 
39 #include <exception>
40 #include <map>
41 #include <string>
42 
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"
48 
49 class System;
50 class ThreadContext;
51 
52 class GDBListener;
53 
54 class BaseRemoteGDB;
55 
56 struct GdbCommand
57 {
58  public:
59  struct Context
60  {
61  const GdbCommand *cmd;
62  char cmd_byte;
63  int type;
64  char *data;
65  int len;
66  };
67 
68  typedef bool (BaseRemoteGDB::*Func)(Context &ctx);
69 
70  const char * const name;
71  const Func func;
72 
73  GdbCommand(const char *_name, Func _func) : name(_name), func(_func)
74  {}
75 };
76 
77 class BaseRemoteGDB
78 {
79  private:
80  friend void debugger();
81  friend class GDBListener;
82 
83  protected:
85  struct BadClient
86  {
87  const char *warning;
88  BadClient(const char *_warning=NULL) : warning(_warning)
89  {}
90  };
92  struct CmdError
93  {
94  std::string error;
95  CmdError(std::string _error) : error(_error)
96  {}
97  };
99  class Unsupported {};
100 
101  // Helper functions
102  protected:
103  int digit2i(char);
104  char i2digit(int);
105  Addr hex2i(const char **);
106  // Address formats, break types, and gdb commands may change
107  // between architectures, so they're defined as virtual
108  // functions.
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);
112 
113  protected:
114  static std::map<char, GdbCommand> command_map;
115 
117 
118  bool cmd_signal(GdbCommand::Context &ctx);
119  bool cmd_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);
125  bool cmd_mem_r(GdbCommand::Context &ctx);
126  bool cmd_mem_w(GdbCommand::Context &ctx);
128  bool cmd_step(GdbCommand::Context &ctx);
132 
133  protected:
134  class InputEvent : public PollEvent
135  {
136  protected:
138 
139  public:
140  InputEvent(BaseRemoteGDB *g, int fd, int e);
141  void process(int revent);
142  };
143 
144  class TrapEvent : public Event
145  {
146  protected:
147  int _type;
149 
150  public:
152  {}
153 
154  void type(int t) { _type = t; }
155  void process();
156  };
157 
158  friend class InputEvent;
162  int number;
163 
164  protected:
165  // The socket commands come in through
166  int fd;
167 
168  protected:
169  bool active;
170  bool attached;
171 
174 
175  protected:
184  {
185  public:
186 
192  virtual char *data() const = 0;
193 
198  virtual size_t size() const = 0;
199 
203  virtual void getRegs(ThreadContext*) = 0;
204 
209  virtual void setRegs(ThreadContext*) const = 0;
210 
217  virtual const std::string name() const = 0;
218 
220  {}
222  {}
223 
224  protected:
226  };
227 
229 
230  protected:
231  uint8_t getbyte();
232  void putbyte(uint8_t b);
233 
234  int recv(char *data, int len);
235  void send(const char *data);
236 
237  protected:
238  // Machine memory
239  virtual bool read(Addr addr, size_t size, char *data);
240  virtual bool write(Addr addr, size_t size, const char *data);
241 
242  template <class T> T read(Addr addr);
243  template <class T> void write(Addr addr, T data);
244 
245  public:
247  virtual ~BaseRemoteGDB();
248  virtual BaseGdbRegCache *gdbRegs() = 0;
249 
251 
252  void attach(int fd);
253  void detach();
254  bool isattached();
255 
256  virtual bool acc(Addr addr, size_t len) = 0;
257  bool trap(int type);
258  virtual bool breakpoint()
259  {
260  return trap(SIGTRAP);
261  }
262 
263  protected:
264  class SingleStepEvent : public Event
265  {
266  protected:
268 
269  public:
271  {}
272 
273  void process();
274  };
275 
277 
278  void clearSingleStep();
279  void setSingleStep();
280 
283 
285  void scheduleInstCommitEvent(Event *ev, int delta);
288 
289  protected:
290  virtual bool checkBpLen(size_t len);
291 
292  class HardBreakpoint : public PCEvent
293  {
294  private:
296 
297  public:
298  int refcount;
299 
300  public:
302  const std::string name() const { return gdb->name() + ".hwbkpt"; }
303 
304  virtual void process(ThreadContext *tc);
305  };
306  friend class HardBreakpoint;
307 
308  typedef std::map<Addr, HardBreakpoint *> break_map_t;
309  typedef break_map_t::iterator break_iter_t;
311 
312  void insertSoftBreak(Addr addr, size_t len);
313  void removeSoftBreak(Addr addr, size_t len);
314  virtual void insertHardBreak(Addr addr, size_t len);
315  void removeHardBreak(Addr addr, size_t len);
316 
317  protected:
318  void clearTempBreakpoint(Addr &bkpt);
319  void setTempBreakpoint(Addr bkpt);
320 
321  public:
322  std::string name();
323 };
324 
325 template <class T>
326 inline T
328 {
329  T temp;
330  read(addr, sizeof(T), (char *)&temp);
331  return temp;
332 }
333 
334 template <class T>
335 inline void
337 { write(addr, sizeof(T), (const char *)&data); }
338 
340 {
341  protected:
342  class InputEvent : public PollEvent
343  {
344  protected:
346 
347  public:
348  InputEvent(GDBListener *l, int fd, int e);
349  void process(int revent);
350  };
351 
352  friend class InputEvent;
354 
355  protected:
358  int port;
359 
360  public:
361  GDBListener(BaseRemoteGDB *g, int p);
362  ~GDBListener();
363 
364  void accept();
365  void listen();
366  std::string name();
367 };
368 
369 #endif /* __REMOTE_GDB_H__ */
SingleStepEvent(BaseRemoteGDB *g)
Definition: remote_gdb.hh:270
virtual void setRegs(ThreadContext *) const =0
Set the ThreadContext's registers from the values in the raw buffer.
const Func func
Definition: remote_gdb.hh:71
void removeSoftBreak(Addr addr, size_t len)
Definition: remote_gdb.cc:593
void descheduleInstCommitEvent(Event *ev)
Deschedule an instruction count based event.
Definition: remote_gdb.cc:555
TrapEvent trapEvent
Definition: remote_gdb.hh:160
void listen()
Definition: remote_gdb.cc:208
std::string name()
Definition: remote_gdb.cc:202
virtual bool read(Addr addr, size_t size, char *data)
Definition: remote_gdb.cc:454
int recv(char *data, int len)
Definition: remote_gdb.cc:391
friend void debugger()
Definition: remote_gdb.cc:161
GDBListener * listener
Definition: remote_gdb.hh:345
bool trap(int type)
Definition: remote_gdb.cc:966
HardBreakpoint(BaseRemoteGDB *_gdb, Addr addr)
Definition: remote_gdb.cc:567
void insertSoftBreak(Addr addr, size_t len)
Definition: remote_gdb.cc:584
BaseGdbRegCache(BaseRemoteGDB *g)
Definition: remote_gdb.hh:219
std::map< Addr, HardBreakpoint * > break_map_t
Definition: remote_gdb.hh:308
void send(const char *data)
Definition: remote_gdb.cc:362
void clearTempBreakpoint(Addr &bkpt)
Definition: remote_gdb.cc:643
break_map_t::iterator break_iter_t
Definition: remote_gdb.hh:309
SingleStepEvent singleStepEvent
Definition: remote_gdb.hh:276
BaseRemoteGDB * gdb
Definition: remote_gdb.hh:357
BadClient(const char *_warning=NULL)
Definition: remote_gdb.hh:88
bool cmd_async_cont(GdbCommand::Context &ctx)
Definition: remote_gdb.cc:758
ip6_addr_t addr
Definition: inet.hh:335
bool cmd_reg_r(GdbCommand::Context &ctx)
Definition: remote_gdb.cc:778
void process(int revent)
Definition: remote_gdb.cc:258
InputEvent(BaseRemoteGDB *g, int fd, int e)
Definition: remote_gdb.cc:253
void putbyte(uint8_t b)
Definition: remote_gdb.cc:352
ListenSocket listener
Definition: remote_gdb.hh:356
void setTempBreakpoint(Addr bkpt)
Definition: remote_gdb.cc:636
void process(int revent)
Definition: remote_gdb.cc:184
Definition: system.hh:83
GDBListener(BaseRemoteGDB *g, int p)
Definition: remote_gdb.cc:189
GDBListener * listener
Definition: remote_gdb.hh:161
System * system
Definition: remote_gdb.hh:172
bool cmd_reg_w(GdbCommand::Context &ctx)
Definition: remote_gdb.cc:788
void setSingleStep()
Definition: remote_gdb.cc:527
ThreadContext is the external interface to all thread state for anything outside of the CPU...
Concrete subclasses of this abstract class represent how the register values are transmitted on the w...
Definition: remote_gdb.hh:183
virtual char * data() const =0
Return the pointer to the raw bytes buffer containing the register values.
const char data[]
Definition: circlebuf.cc:43
void scheduleInstCommitEvent(Event *ev, int delta)
Schedule an event which will be triggered "delta" instructions later.
Definition: remote_gdb.cc:546
GdbCommand(const char *_name, Func _func)
Definition: remote_gdb.hh:73
bool cmd_detach(GdbCommand::Context &ctx)
Definition: remote_gdb.cc:771
Bitfield< 7 > b
Definition: miscregs.hh:1564
ThreadContext * context
Definition: remote_gdb.hh:173
const std::string name() const
Definition: remote_gdb.hh:302
BaseGdbRegCache * regCachePtr
Definition: remote_gdb.hh:228
Bitfield< 4 > g
Definition: dt_constants.hh:85
Queue of events sorted in time order.
Definition: eventq.hh:488
bool cmd_query_var(GdbCommand::Context &ctx)
Definition: remote_gdb.cc:860
void clearSingleStep()
Definition: remote_gdb.cc:521
Exception to throw when an error needs to be reported to the client.
Definition: remote_gdb.hh:92
bool cmd_cont(GdbCommand::Context &ctx)
Definition: remote_gdb.cc:746
void replaceThreadContext(ThreadContext *tc)
Definition: remote_gdb.hh:250
virtual bool acc(Addr addr, size_t len)=0
bool cmd_set_thread(GdbCommand::Context &ctx)
Definition: remote_gdb.cc:802
Exception to throw when something isn't supported.
Definition: remote_gdb.hh:99
bool(BaseRemoteGDB::* Func)(Context &ctx)
Definition: remote_gdb.hh:68
virtual const char * hex2mem(char *, const char *, int)
Definition: remote_gdb.cc:1081
bool cmd_set_hw_bkpt(GdbCommand::Context &ctx)
Definition: remote_gdb.cc:927
virtual bool checkBpLen(size_t len)
Definition: remote_gdb.cc:562
virtual const char * break_type(char c)
Definition: remote_gdb.cc:659
virtual void getRegs(ThreadContext *)=0
Fill the raw buffer from the registers in the ThreadContext.
const char *const name
Definition: remote_gdb.hh:70
TrapEvent(BaseRemoteGDB *g)
Definition: remote_gdb.hh:151
uint64_t Addr
Address type This will probably be moved somewhere else in the near future.
Definition: types.hh:142
virtual bool write(Addr addr, size_t size, const char *data)
Definition: remote_gdb.cc:490
virtual BaseGdbRegCache * gdbRegs()=0
std::string name()
Definition: remote_gdb.cc:303
Addr hex2i(const char **)
Definition: remote_gdb.cc:1102
bool cmd_clr_hw_bkpt(GdbCommand::Context &ctx)
Definition: remote_gdb.cc:894
bool isattached()
Definition: remote_gdb.cc:309
EventQueue * getComInstEventQueue()
Definition: remote_gdb.cc:539
Bitfield< 9 > e
Definition: miscregs.hh:1376
virtual const std::string name() const =0
Return the name to use in places like DPRINTF.
void accept()
Definition: remote_gdb.cc:238
type
Definition: misc.hh:728
int size()
Definition: pagetable.hh:146
Bitfield< 29 > c
Definition: miscregs.hh:1365
void attach(int fd)
Definition: remote_gdb.cc:313
bool cmd_unsupported(GdbCommand::Context &ctx)
Definition: remote_gdb.cc:730
virtual ~BaseRemoteGDB()
Definition: remote_gdb.hh:53
bool cmd_mem_w(GdbCommand::Context &ctx)
Definition: remote_gdb.cc:836
CmdError(std::string _error)
Definition: remote_gdb.hh:95
Definition: eventq.hh:185
bool cmd_mem_r(GdbCommand::Context &ctx)
Definition: remote_gdb.cc:812
break_map_t hardBreakMap
Definition: remote_gdb.hh:310
Bitfield< 18, 16 > len
Definition: miscregs.hh:1626
uint8_t getbyte()
Definition: remote_gdb.cc:342
char i2digit(int)
Definition: remote_gdb.cc:1057
int digit2i(char)
Definition: remote_gdb.cc:1043
bool cmd_async_step(GdbCommand::Context &ctx)
Definition: remote_gdb.cc:869
virtual void mem2hex(char *, const char *, int)
Definition: remote_gdb.cc:1064
Exception to throw when the connection to the client is broken.
Definition: remote_gdb.hh:85
bool cmd_signal(GdbCommand::Context &ctx)
Definition: remote_gdb.cc:739
PCEventQueue * getPcEventQueue()
Definition: remote_gdb.cc:533
InputEvent(GDBListener *l, int fd, int e)
Definition: remote_gdb.cc:179
static std::map< char, GdbCommand > command_map
Definition: remote_gdb.hh:114
Bitfield< 5 > t
Definition: miscregs.hh:1382
bool cmd_step(GdbCommand::Context &ctx)
Definition: remote_gdb.cc:882
BaseRemoteGDB(System *system, ThreadContext *context)
Definition: remote_gdb.cc:289
const GdbCommand * cmd
Definition: remote_gdb.hh:61
Bitfield< 14, 12 > fd
Definition: types.hh:155
void removeHardBreak(Addr addr, size_t len)
Definition: remote_gdb.cc:617
Bitfield< 0 > p
virtual void insertHardBreak(Addr addr, size_t len)
Definition: remote_gdb.cc:602
Bitfield< 5 > l
virtual bool breakpoint()
Definition: remote_gdb.hh:258
BaseRemoteGDB * gdb
Definition: remote_gdb.hh:148
virtual size_t size() const =0
Return the size of the raw buffer, in bytes (i.e., half of the number of digits in the g/G packet)...
InputEvent * inputEvent
Definition: remote_gdb.hh:159
virtual void process(ThreadContext *tc)
Definition: remote_gdb.cc:575
InputEvent * inputEvent
Definition: remote_gdb.hh:353

Generated on Fri Jun 9 2017 13:03:36 for gem5 by doxygen 1.8.6