gem5  v19.0.0.0
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
remote_gdb.hh
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018 ARM Limited
3  *
4  * The license below extends only to copyright in the software and shall
5  * not be construed as granting a license to any other intellectual
6  * property including but not limited to intellectual property relating
7  * to a hardware implementation of the functionality of the software
8  * licensed hereunder. You may use the software subject to the license
9  * terms below provided that you ensure that this notice is replicated
10  * unmodified and in its entirety in all distributions of the software,
11  * modified or unmodified, in source code or in binary form.
12  *
13  * Copyright 2015 LabWare
14  * Copyright 2014 Google, Inc.
15  * Copyright (c) 2002-2005 The Regents of The University of Michigan
16  * All rights reserved.
17  *
18  * Redistribution and use in source and binary forms, with or without
19  * modification, are permitted provided that the following conditions are
20  * met: redistributions of source code must retain the above copyright
21  * notice, this list of conditions and the following disclaimer;
22  * redistributions in binary form must reproduce the above copyright
23  * notice, this list of conditions and the following disclaimer in the
24  * documentation and/or other materials provided with the distribution;
25  * neither the name of the copyright holders nor the names of its
26  * contributors may be used to endorse or promote products derived from
27  * this software without specific prior written permission.
28  *
29  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
30  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
31  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
32  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
33  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
34  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
35  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
36  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
37  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
38  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
39  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40  *
41  * Authors: Nathan Binkert
42  * Boris Shingarov
43  */
44 
45 #ifndef __REMOTE_GDB_HH__
46 #define __REMOTE_GDB_HH__
47 
48 #include <sys/signal.h>
49 
50 #include <exception>
51 #include <map>
52 #include <string>
53 
54 #include "arch/types.hh"
55 #include "base/intmath.hh"
56 #include "base/pollevent.hh"
57 #include "base/socket.hh"
58 #include "cpu/pc_event.hh"
59 
60 class System;
61 class ThreadContext;
62 
63 class BaseRemoteGDB;
64 class HardBreakpoint;
65 
74 {
75  public:
76 
82  virtual char *data() const = 0;
83 
88  virtual size_t size() const = 0;
89 
93  virtual void getRegs(ThreadContext*) = 0;
94 
99  virtual void setRegs(ThreadContext*) const = 0;
100 
107  virtual const std::string name() const = 0;
108 
110  {}
112  {}
113 
114  protected:
116 };
117 
118 class BaseRemoteGDB
119 {
120  friend class HardBreakpoint;
121  public:
122 
123  /*
124  * Interface to other parts of the simulator.
125  */
126  BaseRemoteGDB(System *system, ThreadContext *context, int _port);
127  virtual ~BaseRemoteGDB();
128 
129  std::string name();
130 
131  void listen();
132  void connect();
133 
134  int port() const;
135 
136  void attach(int fd);
137  void detach();
138  bool isAttached() { return attached; }
139 
140  void replaceThreadContext(ThreadContext *_tc) { tc = _tc; }
141 
142  bool trap(int type);
143  bool breakpoint() { return trap(SIGTRAP); }
144 
145  private:
146  /*
147  * Connection to the external GDB.
148  */
149  void incomingData(int revent);
150  void connectWrapper(int revent) { connect(); }
151 
152  template <void (BaseRemoteGDB::*F)(int revent)>
153  class SocketEvent : public PollEvent
154  {
155  protected:
157 
158  public:
159  SocketEvent(BaseRemoteGDB *gdb, int fd, int e) :
160  PollEvent(fd, e), gdb(gdb)
161  {}
162 
163  void process(int revent) { (gdb->*F)(revent); }
164  };
165 
168 
169  friend ConnectEvent;
170  friend DataEvent;
171 
172  ConnectEvent *connectEvent;
173  DataEvent *dataEvent;
174 
176  int _port;
177 
178  // The socket commands come in through.
179  int fd;
180 
181  // Transfer data to/from GDB.
182  uint8_t getbyte();
183  void putbyte(uint8_t b);
184 
185  void recv(std::vector<char> &bp);
186  void send(const char *data);
187 
188  /*
189  * Simulator side debugger state.
190  */
191  bool active;
192  bool attached;
193 
196 
198 
199  class TrapEvent : public Event
200  {
201  protected:
202  int _type;
204 
205  public:
207  {}
208 
209  void type(int t) { _type = t; }
210  void process() { gdb->trap(_type); }
211  } trapEvent;
212 
213  /*
214  * The interface to the simulated system.
215  */
216  // Machine memory.
217  bool read(Addr addr, size_t size, char *data);
218  bool write(Addr addr, size_t size, const char *data);
219 
220  template <class T> T read(Addr addr);
221  template <class T> void write(Addr addr, T data);
222 
223  // Single step.
224  void singleStep();
226 
227  void clearSingleStep();
228  void setSingleStep();
229 
231  void scheduleInstCommitEvent(Event *ev, int delta);
233  void descheduleInstCommitEvent(Event *ev);
234 
235  // Breakpoints.
236  void insertSoftBreak(Addr addr, size_t len);
237  void removeSoftBreak(Addr addr, size_t len);
238  void insertHardBreak(Addr addr, size_t len);
239  void removeHardBreak(Addr addr, size_t len);
240 
241  void clearTempBreakpoint(Addr &bkpt);
242  void setTempBreakpoint(Addr bkpt);
243 
244  /*
245  * GDB commands.
246  */
247  struct GdbCommand
248  {
249  public:
250  struct Context
251  {
252  const GdbCommand *cmd;
253  char cmd_byte;
254  int type;
255  char *data;
256  int len;
257  };
258 
259  typedef bool (BaseRemoteGDB::*Func)(Context &ctx);
260 
261  const char * const name;
262  const Func func;
263 
264  GdbCommand(const char *_name, Func _func) : name(_name), func(_func) {}
265  };
266 
267  static std::map<char, GdbCommand> command_map;
268 
269  bool cmd_unsupported(GdbCommand::Context &ctx);
270 
271  bool cmd_signal(GdbCommand::Context &ctx);
272  bool cmd_cont(GdbCommand::Context &ctx);
273  bool cmd_async_cont(GdbCommand::Context &ctx);
274  bool cmd_detach(GdbCommand::Context &ctx);
275  bool cmd_reg_r(GdbCommand::Context &ctx);
276  bool cmd_reg_w(GdbCommand::Context &ctx);
277  bool cmd_set_thread(GdbCommand::Context &ctx);
278  bool cmd_mem_r(GdbCommand::Context &ctx);
279  bool cmd_mem_w(GdbCommand::Context &ctx);
280  bool cmd_query_var(GdbCommand::Context &ctx);
281  bool cmd_step(GdbCommand::Context &ctx);
282  bool cmd_async_step(GdbCommand::Context &ctx);
283  bool cmd_clr_hw_bkpt(GdbCommand::Context &ctx);
284  bool cmd_set_hw_bkpt(GdbCommand::Context &ctx);
285 
286  protected:
287  ThreadContext *context() { return tc; }
288  System *system() { return sys; }
289 
290  void encodeBinaryData(const std::string &unencoded,
291  std::string &encoded) const;
292 
293  void encodeXferResponse(const std::string &unencoded,
294  std::string &encoded, size_t offset, size_t unencoded_length) const;
295 
296  // To be implemented by subclasses.
297  virtual bool checkBpLen(size_t len);
298 
299  virtual BaseGdbRegCache *gdbRegs() = 0;
300 
301  virtual bool acc(Addr addr, size_t len) = 0;
302 
303  virtual std::vector<std::string> availableFeatures() const;
304 
312  virtual bool getXferFeaturesRead(const std::string &annex,
313  std::string &output);
314 };
315 
316 template <class T>
317 inline T
319 {
320  T temp;
321  read(addr, sizeof(T), (char *)&temp);
322  return temp;
323 }
324 
325 template <class T>
326 inline void
328 {
329  write(addr, sizeof(T), (const char *)&data);
330 }
331 
332 #endif /* __REMOTE_GDB_H__ */
ThreadContext * tc
Definition: remote_gdb.hh:195
bool isAttached()
Definition: remote_gdb.hh:138
bool read(Addr addr, size_t size, char *data)
Definition: remote_gdb.cc:609
void connectWrapper(int revent)
Definition: remote_gdb.hh:150
bool trap(int type)
Definition: remote_gdb.cc:417
static void output(const char *filename)
Definition: debug.cc:63
bool breakpoint()
Definition: remote_gdb.hh:143
ConnectEvent * connectEvent
Definition: remote_gdb.hh:172
virtual void getRegs(ThreadContext *)=0
Fill the raw buffer from the registers in the ThreadContext.
ip6_addr_t addr
Definition: inet.hh:335
virtual const std::string name() const =0
Return the name to use in places like DPRINTF.
Definition: system.hh:77
Bitfield< 23, 0 > offset
Definition: types.hh:154
ThreadContext is the external interface to all thread state for anything outside of the CPU...
ListenSocket listener
Definition: remote_gdb.hh:175
DataEvent * dataEvent
Definition: remote_gdb.hh:173
virtual void setRegs(ThreadContext *) const =0
Set the ThreadContext&#39;s registers from the values in the raw buffer.
EventWrapper< BaseRemoteGDB, &BaseRemoteGDB::singleStep > singleStepEvent
Definition: remote_gdb.hh:225
Bitfield< 7 > b
uint8_t type
Definition: inet.hh:333
BaseGdbRegCache * regCachePtr
Definition: remote_gdb.hh:197
Bitfield< 4 > g
Definition: dt_constants.hh:85
SocketEvent(BaseRemoteGDB *gdb, int fd, int e)
Definition: remote_gdb.hh:159
void replaceThreadContext(ThreadContext *_tc)
Definition: remote_gdb.hh:140
BaseRemoteGDB * gdb
Definition: remote_gdb.hh:115
Concrete subclasses of this abstract class represent how the register values are transmitted on the w...
Definition: remote_gdb.hh:73
static std::map< char, GdbCommand > command_map
Definition: remote_gdb.hh:267
Bitfield< 18, 16 > len
friend ConnectEvent
Definition: remote_gdb.hh:169
SocketEvent<&BaseRemoteGDB::incomingData > DataEvent
Definition: remote_gdb.hh:167
TrapEvent(BaseRemoteGDB *g)
Definition: remote_gdb.hh:206
uint64_t Addr
Address type This will probably be moved somewhere else in the near future.
Definition: types.hh:142
bool write(Addr addr, size_t size, const char *data)
Definition: remote_gdb.cc:640
Bitfield< 15 > system
Definition: misc.hh:999
virtual ~BaseGdbRegCache()
Definition: remote_gdb.hh:111
GdbCommand(const char *_name, Func _func)
Definition: remote_gdb.hh:264
Bitfield< 9 > e
virtual char * data() const =0
Return the pointer to the raw bytes buffer containing the register values.
ThreadContext * context()
Definition: remote_gdb.hh:287
Definition: eventq.hh:189
const char *const name
Definition: remote_gdb.hh:261
friend DataEvent
Definition: remote_gdb.hh:170
BaseGdbRegCache(BaseRemoteGDB *g)
Definition: remote_gdb.hh:109
Bitfield< 5 > t
System * sys
Definition: remote_gdb.hh:194
Bitfield< 14, 12 > fd
Definition: types.hh:160
void process(int revent)
Definition: remote_gdb.hh:163
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)...
BaseRemoteGDB * gdb
Definition: remote_gdb.hh:203
SocketEvent<&BaseRemoteGDB::connectWrapper > ConnectEvent
Definition: remote_gdb.hh:166
System * system()
Definition: remote_gdb.hh:288

Generated on Fri Feb 28 2020 16:26:56 for gem5 by doxygen 1.8.13