gem5  v20.1.0.0
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 
42 #ifndef __REMOTE_GDB_HH__
43 #define __REMOTE_GDB_HH__
44 
45 #include <sys/signal.h>
46 
47 #include <exception>
48 #include <map>
49 #include <string>
50 
51 #include "arch/types.hh"
52 #include "base/intmath.hh"
53 #include "base/pollevent.hh"
54 #include "base/socket.hh"
55 #include "cpu/pc_event.hh"
56 
57 class System;
58 class ThreadContext;
59 
60 class BaseRemoteGDB;
61 class HardBreakpoint;
62 
71 {
72  public:
73 
81  virtual char *data() const = 0;
82 
89  virtual size_t size() const = 0;
90 
96  virtual void getRegs(ThreadContext*) = 0;
97 
104  virtual void setRegs(ThreadContext*) const = 0;
105 
114  virtual const std::string name() const = 0;
115 
120  {}
122  {}
123 
124  protected:
126 };
127 
128 class BaseRemoteGDB
129 {
130  friend class HardBreakpoint;
131  public:
132 
142  virtual ~BaseRemoteGDB();
143 
144  std::string name();
145 
146  void listen();
147  void connect();
148 
149  int port() const;
150 
151  void attach(int fd);
152  void detach();
153  bool isAttached() { return attached; }
154 
155  void replaceThreadContext(ThreadContext *_tc) { tc = _tc; }
156 
157  bool trap(int type);
158  bool breakpoint() { return trap(SIGTRAP); }
159  // end of api_remote_gdb
161 
162  private:
163  /*
164  * Connection to the external GDB.
165  */
166  void incomingData(int revent);
167  void connectWrapper(int revent) { connect(); }
168 
169  template <void (BaseRemoteGDB::*F)(int revent)>
170  class SocketEvent : public PollEvent
171  {
172  protected:
174 
175  public:
177  PollEvent(fd, e), gdb(gdb)
178  {}
179 
180  void process(int revent) { (gdb->*F)(revent); }
181  };
182 
185 
186  friend ConnectEvent;
187  friend DataEvent;
188 
191 
193  int _port;
194 
195  // The socket commands come in through.
196  int fd;
197 
198  // Transfer data to/from GDB.
199  uint8_t getbyte();
200  void putbyte(uint8_t b);
201 
202  void recv(std::vector<char> &bp);
203  void send(const char *data);
204 
205  /*
206  * Simulator side debugger state.
207  */
208  bool active;
209  bool attached;
210 
213 
215 
216  class TrapEvent : public Event
217  {
218  protected:
219  int _type;
221 
222  public:
224  {}
225 
226  void type(int t) { _type = t; }
227  void process() { gdb->trap(_type); }
228  } trapEvent;
229 
230  /*
231  * The interface to the simulated system.
232  */
233  // Machine memory.
234  bool read(Addr addr, size_t size, char *data);
235  bool write(Addr addr, size_t size, const char *data);
236 
237  template <class T> T read(Addr addr);
238  template <class T> void write(Addr addr, T data);
239 
240  // Single step.
241  void singleStep();
243 
244  void clearSingleStep();
245  void setSingleStep();
246 
248  void scheduleInstCommitEvent(Event *ev, int delta);
251 
252  // Breakpoints.
253  void insertSoftBreak(Addr addr, size_t len);
254  void removeSoftBreak(Addr addr, size_t len);
255  void insertHardBreak(Addr addr, size_t len);
256  void removeHardBreak(Addr addr, size_t len);
257 
258  void clearTempBreakpoint(Addr &bkpt);
259  void setTempBreakpoint(Addr bkpt);
260 
261  /*
262  * GDB commands.
263  */
264  struct GdbCommand
265  {
266  public:
267  struct Context
268  {
269  const GdbCommand *cmd;
270  char cmd_byte;
271  int type;
272  char *data;
273  int len;
274  };
275 
276  typedef bool (BaseRemoteGDB::*Func)(Context &ctx);
277 
278  const char * const name;
279  const Func func;
280 
281  GdbCommand(const char *_name, Func _func) : name(_name), func(_func) {}
282  };
283 
284  static std::map<char, GdbCommand> command_map;
285 
287 
288  bool cmd_signal(GdbCommand::Context &ctx);
289  bool cmd_cont(GdbCommand::Context &ctx);
291  bool cmd_detach(GdbCommand::Context &ctx);
292  bool cmd_reg_r(GdbCommand::Context &ctx);
293  bool cmd_reg_w(GdbCommand::Context &ctx);
295  bool cmd_mem_r(GdbCommand::Context &ctx);
296  bool cmd_mem_w(GdbCommand::Context &ctx);
298  bool cmd_step(GdbCommand::Context &ctx);
302 
303  protected:
304  ThreadContext *context() { return tc; }
305  System *system() { return sys; }
306 
307  void encodeBinaryData(const std::string &unencoded,
308  std::string &encoded) const;
309 
310  void encodeXferResponse(const std::string &unencoded,
311  std::string &encoded, size_t offset, size_t unencoded_length) const;
312 
313  // To be implemented by subclasses.
314  virtual bool checkBpLen(size_t len);
315 
316  virtual BaseGdbRegCache *gdbRegs() = 0;
317 
318  virtual bool acc(Addr addr, size_t len) = 0;
319 
321 
329  virtual bool getXferFeaturesRead(const std::string &annex,
330  std::string &output);
331 };
332 
333 template <class T>
334 inline T
336 {
337  T temp;
338  read(addr, sizeof(T), (char *)&temp);
339  return temp;
340 }
341 
342 template <class T>
343 inline void
345 {
346  write(addr, sizeof(T), (const char *)&data);
347 }
348 
349 #endif /* __REMOTE_GDB_H__ */
BaseRemoteGDB::command_map
static std::map< char, GdbCommand > command_map
Definition: remote_gdb.hh:284
BaseRemoteGDB::~BaseRemoteGDB
virtual ~BaseRemoteGDB()
Definition: remote_gdb.hh:51
BaseRemoteGDB::TrapEvent::type
void type(int t)
Definition: remote_gdb.hh:226
BaseRemoteGDB::port
int port() const
Definition: remote_gdb.cc:374
BaseRemoteGDB::attach
void attach(int fd)
Definition: remote_gdb.cc:382
output
static void output(const char *filename)
Definition: debug.cc:60
BaseRemoteGDB::attached
bool attached
Definition: remote_gdb.hh:209
BaseRemoteGDB::breakpoint
bool breakpoint()
Definition: remote_gdb.hh:158
BaseRemoteGDB::singleStep
void singleStep()
Definition: remote_gdb.cc:645
socket.hh
BaseGdbRegCache::size
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::cmd_set_hw_bkpt
bool cmd_set_hw_bkpt(GdbCommand::Context &ctx)
Definition: remote_gdb.cc:1082
BaseRemoteGDB::DataEvent
friend DataEvent
Definition: remote_gdb.hh:187
ListenSocket
Definition: socket.hh:32
BaseRemoteGDB::encodeXferResponse
void encodeXferResponse(const std::string &unencoded, std::string &encoded, size_t offset, size_t unencoded_length) const
Definition: remote_gdb.cc:1013
data
const char data[]
Definition: circlebuf.test.cc:42
BaseRemoteGDB::putbyte
void putbyte(uint8_t b)
Definition: remote_gdb.cc:513
BaseRemoteGDB::cmd_async_step
bool cmd_async_step(GdbCommand::Context &ctx)
Definition: remote_gdb.cc:1024
BaseRemoteGDB::GdbCommand
Definition: remote_gdb.hh:264
HardBreakpoint
Definition: remote_gdb.cc:164
BaseGdbRegCache::~BaseGdbRegCache
virtual ~BaseGdbRegCache()
Definition: remote_gdb.hh:121
BaseRemoteGDB::cmd_signal
bool cmd_signal(GdbCommand::Context &ctx)
Definition: remote_gdb.cc:820
BaseRemoteGDB::encodeBinaryData
void encodeBinaryData(const std::string &unencoded, std::string &encoded) const
Definition: remote_gdb.cc:999
BaseRemoteGDB::TrapEvent::process
void process()
Definition: remote_gdb.hh:227
BaseRemoteGDB::TrapEvent::_type
int _type
Definition: remote_gdb.hh:219
BaseRemoteGDB::connectWrapper
void connectWrapper(int revent)
Definition: remote_gdb.hh:167
BaseRemoteGDB::replaceThreadContext
void replaceThreadContext(ThreadContext *_tc)
Definition: remote_gdb.hh:155
type
uint8_t type
Definition: inet.hh:421
BaseRemoteGDB::cmd_cont
bool cmd_cont(GdbCommand::Context &ctx)
Definition: remote_gdb.cc:827
EventWrapper< BaseRemoteGDB, &BaseRemoteGDB::singleStep >
BaseRemoteGDB::cmd_unsupported
bool cmd_unsupported(GdbCommand::Context &ctx)
Definition: remote_gdb.cc:811
BaseRemoteGDB::removeHardBreak
void removeHardBreak(Addr addr, size_t len)
Definition: remote_gdb.cc:699
BaseRemoteGDB::cmd_set_thread
bool cmd_set_thread(GdbCommand::Context &ctx)
Definition: remote_gdb.cc:883
BaseRemoteGDB::ConnectEvent
friend ConnectEvent
Definition: remote_gdb.hh:186
BaseGdbRegCache::gdb
BaseRemoteGDB * gdb
Definition: remote_gdb.hh:125
BaseGdbRegCache::BaseGdbRegCache
BaseGdbRegCache(BaseRemoteGDB *g)
Definition: remote_gdb.hh:119
BaseRemoteGDB::connect
void connect()
Definition: remote_gdb.cc:358
BaseRemoteGDB::incomingData
void incomingData(int revent)
Definition: remote_gdb.cc:486
BaseRemoteGDB::GdbCommand::Func
bool(BaseRemoteGDB::* Func)(Context &ctx)
Definition: remote_gdb.hh:276
BaseRemoteGDB::DataEvent
SocketEvent<&BaseRemoteGDB::incomingData > DataEvent
Definition: remote_gdb.hh:184
std::vector< char >
BaseRemoteGDB::singleStepEvent
EventWrapper< BaseRemoteGDB, &BaseRemoteGDB::singleStep > singleStepEvent
Definition: remote_gdb.hh:242
BaseRemoteGDB::cmd_mem_r
bool cmd_mem_r(GdbCommand::Context &ctx)
Definition: remote_gdb.cc:893
BaseGdbRegCache::name
virtual const std::string name() const =0
Return the name to use in places like DPRINTF.
BaseRemoteGDB::tc
ThreadContext * tc
Definition: remote_gdb.hh:212
BaseRemoteGDB::GdbCommand::Context::len
int len
Definition: remote_gdb.hh:273
BaseRemoteGDB::clearSingleStep
void clearSingleStep()
Definition: remote_gdb.cc:653
BaseRemoteGDB::SocketEvent::process
void process(int revent)
Definition: remote_gdb.hh:180
BaseRemoteGDB::context
ThreadContext * context()
Definition: remote_gdb.hh:304
BaseRemoteGDB::insertSoftBreak
void insertSoftBreak(Addr addr, size_t len)
Definition: remote_gdb.cc:666
BaseRemoteGDB::setSingleStep
void setSingleStep()
Definition: remote_gdb.cc:659
BaseRemoteGDB::GdbCommand::func
const Func func
Definition: remote_gdb.hh:279
BaseRemoteGDB::listener
ListenSocket listener
Definition: remote_gdb.hh:192
BaseRemoteGDB::SocketEvent
Definition: remote_gdb.hh:170
BaseRemoteGDB::clearTempBreakpoint
void clearTempBreakpoint(Addr &bkpt)
Definition: remote_gdb.cc:718
ThreadContext
ThreadContext is the external interface to all thread state for anything outside of the CPU.
Definition: thread_context.hh:88
BaseRemoteGDB::trap
bool trap(int type)
Definition: remote_gdb.cc:412
Event
Definition: eventq.hh:246
BaseRemoteGDB::GdbCommand::Context::cmd
const GdbCommand * cmd
Definition: remote_gdb.hh:269
System
Definition: system.hh:73
BaseRemoteGDB::_port
int _port
Definition: remote_gdb.hh:193
MipsISA::g
Bitfield< 4 > g
Definition: dt_constants.hh:83
BaseRemoteGDB::scheduleInstCommitEvent
void scheduleInstCommitEvent(Event *ev, int delta)
Schedule an event which will be triggered "delta" instructions later.
Definition: remote_gdb.cc:733
BaseRemoteGDB::system
System * system()
Definition: remote_gdb.hh:305
BaseRemoteGDB::getbyte
uint8_t getbyte()
Definition: remote_gdb.cc:503
BaseRemoteGDB::write
bool write(Addr addr, size_t size, const char *data)
Definition: remote_gdb.cc:627
pollevent.hh
BaseRemoteGDB::GdbCommand::GdbCommand
GdbCommand(const char *_name, Func _func)
Definition: remote_gdb.hh:281
BaseRemoteGDB::gdbRegs
virtual BaseGdbRegCache * gdbRegs()=0
BaseRemoteGDB::TrapEvent::gdb
BaseRemoteGDB * gdb
Definition: remote_gdb.hh:220
BaseRemoteGDB::descheduleInstCommitEvent
void descheduleInstCommitEvent(Event *ev)
Deschedule an instruction count based event.
Definition: remote_gdb.cc:741
BaseRemoteGDB::GdbCommand::name
const char *const name
Definition: remote_gdb.hh:278
BaseGdbRegCache::data
virtual char * data() const =0
Return the pointer to the raw bytes buffer containing the register values.
BaseRemoteGDB::sys
System * sys
Definition: remote_gdb.hh:211
BaseRemoteGDB::cmd_mem_w
bool cmd_mem_w(GdbCommand::Context &ctx)
Definition: remote_gdb.cc:917
BaseRemoteGDB
Definition: remote_gdb.hh:43
Addr
uint64_t Addr
Address type This will probably be moved somewhere else in the near future.
Definition: types.hh:142
BaseRemoteGDB::dataEvent
DataEvent * dataEvent
Definition: remote_gdb.hh:190
BaseRemoteGDB::acc
virtual bool acc(Addr addr, size_t len)=0
BaseRemoteGDB::TrapEvent::TrapEvent
TrapEvent(BaseRemoteGDB *g)
Definition: remote_gdb.hh:223
BaseRemoteGDB::isAttached
bool isAttached()
Definition: remote_gdb.hh:153
BaseRemoteGDB::connectEvent
ConnectEvent * connectEvent
Definition: remote_gdb.hh:189
BaseGdbRegCache
Concrete subclasses of this abstract class represent how the register values are transmitted on the w...
Definition: remote_gdb.hh:70
BaseGdbRegCache::setRegs
virtual void setRegs(ThreadContext *) const =0
Set the ThreadContext's registers from the values in the raw buffer.
BaseRemoteGDB::cmd_async_cont
bool cmd_async_cont(GdbCommand::Context &ctx)
Definition: remote_gdb.cc:839
ArmISA::e
Bitfield< 9 > e
Definition: miscregs_types.hh:61
BaseRemoteGDB::GdbCommand::Context::cmd_byte
char cmd_byte
Definition: remote_gdb.hh:270
BaseRemoteGDB::cmd_detach
bool cmd_detach(GdbCommand::Context &ctx)
Definition: remote_gdb.cc:852
BaseRemoteGDB::SocketEvent::gdb
BaseRemoteGDB * gdb
Definition: remote_gdb.hh:173
BaseRemoteGDB::getXferFeaturesRead
virtual bool getXferFeaturesRead(const std::string &annex, std::string &output)
Get an XML target description.
Definition: remote_gdb.cc:992
BaseRemoteGDB::GdbCommand::Context::type
int type
Definition: remote_gdb.hh:271
BaseRemoteGDB::removeSoftBreak
void removeSoftBreak(Addr addr, size_t len)
Definition: remote_gdb.cc:675
BaseRemoteGDB::availableFeatures
virtual std::vector< std::string > availableFeatures() const
Definition: remote_gdb.cc:986
ArmISA::b
Bitfield< 7 > b
Definition: miscregs_types.hh:376
BaseRemoteGDB::cmd_query_var
bool cmd_query_var(GdbCommand::Context &ctx)
Definition: remote_gdb.cc:941
pc_event.hh
BaseRemoteGDB::cmd_clr_hw_bkpt
bool cmd_clr_hw_bkpt(GdbCommand::Context &ctx)
Definition: remote_gdb.cc:1049
ArmISA::t
Bitfield< 5 > t
Definition: miscregs_types.hh:67
BaseRemoteGDB::active
bool active
Definition: remote_gdb.hh:208
BaseRemoteGDB::fd
int fd
Definition: remote_gdb.hh:196
BaseRemoteGDB::cmd_step
bool cmd_step(GdbCommand::Context &ctx)
Definition: remote_gdb.cc:1037
ArmISA::len
Bitfield< 18, 16 > len
Definition: miscregs_types.hh:439
addr
ip6_addr_t addr
Definition: inet.hh:423
BaseRemoteGDB::name
std::string name()
Definition: remote_gdb.cc:332
BaseRemoteGDB::insertHardBreak
void insertHardBreak(Addr addr, size_t len)
Definition: remote_gdb.cc:684
BaseRemoteGDB::checkBpLen
virtual bool checkBpLen(size_t len)
Definition: remote_gdb.cc:805
BaseRemoteGDB::ConnectEvent
SocketEvent<&BaseRemoteGDB::connectWrapper > ConnectEvent
Definition: remote_gdb.hh:183
BaseRemoteGDB::TrapEvent
Definition: remote_gdb.hh:216
PollEvent
Definition: pollevent.hh:41
BaseRemoteGDB::GdbCommand::Context
Definition: remote_gdb.hh:267
intmath.hh
BaseRemoteGDB::listen
void listen()
Definition: remote_gdb.cc:338
BaseRemoteGDB::cmd_reg_r
bool cmd_reg_r(GdbCommand::Context &ctx)
Definition: remote_gdb.cc:859
BaseRemoteGDB::trapEvent
BaseRemoteGDB::TrapEvent trapEvent
BaseRemoteGDB::regCachePtr
BaseGdbRegCache * regCachePtr
Definition: remote_gdb.hh:214
BaseRemoteGDB::setTempBreakpoint
void setTempBreakpoint(Addr bkpt)
Definition: remote_gdb.cc:726
BaseRemoteGDB::SocketEvent::SocketEvent
SocketEvent(BaseRemoteGDB *gdb, int fd, int e)
Definition: remote_gdb.hh:176
BaseRemoteGDB::read
bool read(Addr addr, size_t size, char *data)
Definition: remote_gdb.cc:604
BaseRemoteGDB::GdbCommand::Context::data
char * data
Definition: remote_gdb.hh:272
BaseRemoteGDB::detach
void detach()
Definition: remote_gdb.cc:394
BaseRemoteGDB::recv
void recv(std::vector< char > &bp)
Definition: remote_gdb.cc:523
BaseRemoteGDB::cmd_reg_w
bool cmd_reg_w(GdbCommand::Context &ctx)
Definition: remote_gdb.cc:869
BaseRemoteGDB::BaseRemoteGDB
BaseRemoteGDB(System *system, ThreadContext *context, int _port)
Interface to other parts of the simulator.
Definition: remote_gdb.cc:317
BaseGdbRegCache::getRegs
virtual void getRegs(ThreadContext *)=0
Fill the raw buffer from the registers in the ThreadContext.
ArmISA::offset
Bitfield< 23, 0 > offset
Definition: types.hh:153
BaseRemoteGDB::send
void send(const char *data)
Definition: remote_gdb.cc:575

Generated on Wed Sep 30 2020 14:02:01 for gem5 by doxygen 1.8.17