gem5  v21.0.1.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 <cstdint>
48 #include <exception>
49 #include <map>
50 #include <string>
51 
52 #include "arch/types.hh"
53 #include "base/pollevent.hh"
54 #include "base/socket.hh"
55 #include "base/types.hh"
56 #include "cpu/pc_event.hh"
57 #include "sim/eventq.hh"
58 
59 class System;
60 class ThreadContext;
61 
62 class BaseRemoteGDB;
63 class HardBreakpoint;
64 
73 {
74  public:
75 
83  virtual char *data() const = 0;
84 
91  virtual size_t size() const = 0;
92 
98  virtual void getRegs(ThreadContext*) = 0;
99 
106  virtual void setRegs(ThreadContext*) const = 0;
107 
116  virtual const std::string name() const = 0;
117 
122  {}
124  {}
125 
126  protected:
128 };
129 
130 class BaseRemoteGDB
131 {
132  friend class HardBreakpoint;
133  public:
134 
144  virtual ~BaseRemoteGDB();
145 
146  std::string name();
147 
148  void listen();
149  void connect();
150 
151  int port() const;
152 
153  void attach(int fd);
154  void detach();
155  bool isAttached() { return attached; }
156 
157  void replaceThreadContext(ThreadContext *_tc) { tc = _tc; }
158 
159  bool trap(int type);
160  bool breakpoint() { return trap(SIGTRAP); }
161  // end of api_remote_gdb
163 
164  private:
165  /*
166  * Connection to the external GDB.
167  */
168  void incomingData(int revent);
169  void connectWrapper(int revent) { connect(); }
170 
171  template <void (BaseRemoteGDB::*F)(int revent)>
172  class SocketEvent : public PollEvent
173  {
174  protected:
176 
177  public:
179  PollEvent(fd, e), gdb(gdb)
180  {}
181 
182  void process(int revent) { (gdb->*F)(revent); }
183  };
184 
187 
188  friend ConnectEvent;
189  friend DataEvent;
190 
193 
195  int _port;
196 
197  // The socket commands come in through.
198  int fd;
199 
200  // Transfer data to/from GDB.
201  uint8_t getbyte();
202  void putbyte(uint8_t b);
203 
204  void recv(std::vector<char> &bp);
205  void send(const char *data);
206 
207  /*
208  * Simulator side debugger state.
209  */
210  bool active;
211  bool attached;
212 
215 
217 
218  class TrapEvent : public Event
219  {
220  protected:
221  int _type;
223 
224  public:
226  {}
227 
228  void type(int t) { _type = t; }
229  void process() { gdb->trap(_type); }
230  } trapEvent;
231 
232  /*
233  * The interface to the simulated system.
234  */
235  // Machine memory.
236  bool read(Addr addr, size_t size, char *data);
237  bool write(Addr addr, size_t size, const char *data);
238 
239  template <class T> T read(Addr addr);
240  template <class T> void write(Addr addr, T data);
241 
242  // Single step.
243  void singleStep();
245 
246  void clearSingleStep();
247  void setSingleStep();
248 
250  void scheduleInstCommitEvent(Event *ev, int delta);
253 
254  // Breakpoints.
255  void insertSoftBreak(Addr addr, size_t len);
256  void removeSoftBreak(Addr addr, size_t len);
257  void insertHardBreak(Addr addr, size_t len);
258  void removeHardBreak(Addr addr, size_t len);
259 
260  /*
261  * GDB commands.
262  */
263  struct GdbCommand
264  {
265  public:
266  struct Context
267  {
268  const GdbCommand *cmd;
269  char cmd_byte;
270  int type;
271  char *data;
272  int len;
273  };
274 
275  typedef bool (BaseRemoteGDB::*Func)(Context &ctx);
276 
277  const char * const name;
278  const Func func;
279 
280  GdbCommand(const char *_name, Func _func) : name(_name), func(_func) {}
281  };
282 
283  static std::map<char, GdbCommand> command_map;
284 
286 
287  bool cmd_signal(GdbCommand::Context &ctx);
288  bool cmd_cont(GdbCommand::Context &ctx);
290  bool cmd_detach(GdbCommand::Context &ctx);
291  bool cmd_reg_r(GdbCommand::Context &ctx);
292  bool cmd_reg_w(GdbCommand::Context &ctx);
294  bool cmd_mem_r(GdbCommand::Context &ctx);
295  bool cmd_mem_w(GdbCommand::Context &ctx);
297  bool cmd_step(GdbCommand::Context &ctx);
301 
302  protected:
303  ThreadContext *context() { return tc; }
304  System *system() { return sys; }
305 
306  void encodeBinaryData(const std::string &unencoded,
307  std::string &encoded) const;
308 
309  void encodeXferResponse(const std::string &unencoded,
310  std::string &encoded, size_t offset, size_t unencoded_length) const;
311 
312  // To be implemented by subclasses.
313  virtual bool checkBpLen(size_t len);
314 
315  virtual BaseGdbRegCache *gdbRegs() = 0;
316 
317  virtual bool acc(Addr addr, size_t len) = 0;
318 
320 
328  virtual bool getXferFeaturesRead(const std::string &annex,
329  std::string &output);
330 };
331 
332 template <class T>
333 inline T
335 {
336  T temp;
337  read(addr, sizeof(T), (char *)&temp);
338  return temp;
339 }
340 
341 template <class T>
342 inline void
344 {
345  write(addr, sizeof(T), (const char *)&data);
346 }
347 
348 #endif /* __REMOTE_GDB_H__ */
BaseRemoteGDB::command_map
static std::map< char, GdbCommand > command_map
Definition: remote_gdb.hh:283
BaseRemoteGDB::~BaseRemoteGDB
virtual ~BaseRemoteGDB()
Definition: remote_gdb.hh:51
BaseRemoteGDB::TrapEvent::type
void type(int t)
Definition: remote_gdb.hh:228
BaseRemoteGDB::port
int port() const
Definition: remote_gdb.cc:370
BaseRemoteGDB::attach
void attach(int fd)
Definition: remote_gdb.cc:378
output
static void output(const char *filename)
Definition: debug.cc:60
BaseRemoteGDB::attached
bool attached
Definition: remote_gdb.hh:211
BaseRemoteGDB::breakpoint
bool breakpoint()
Definition: remote_gdb.hh:160
BaseRemoteGDB::singleStep
void singleStep()
Definition: remote_gdb.cc:641
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:1063
BaseRemoteGDB::DataEvent
friend DataEvent
Definition: remote_gdb.hh:189
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:994
data
const char data[]
Definition: circlebuf.test.cc:47
BaseRemoteGDB::putbyte
void putbyte(uint8_t b)
Definition: remote_gdb.cc:509
BaseRemoteGDB::cmd_async_step
bool cmd_async_step(GdbCommand::Context &ctx)
Definition: remote_gdb.cc:1005
BaseRemoteGDB::GdbCommand
Definition: remote_gdb.hh:263
HardBreakpoint
Definition: remote_gdb.cc:160
BaseGdbRegCache::~BaseGdbRegCache
virtual ~BaseGdbRegCache()
Definition: remote_gdb.hh:123
BaseRemoteGDB::cmd_signal
bool cmd_signal(GdbCommand::Context &ctx)
Definition: remote_gdb.cc:801
BaseRemoteGDB::encodeBinaryData
void encodeBinaryData(const std::string &unencoded, std::string &encoded) const
Definition: remote_gdb.cc:980
BaseRemoteGDB::TrapEvent::process
void process()
Definition: remote_gdb.hh:229
BaseRemoteGDB::TrapEvent::_type
int _type
Definition: remote_gdb.hh:221
BaseRemoteGDB::connectWrapper
void connectWrapper(int revent)
Definition: remote_gdb.hh:169
BaseRemoteGDB::replaceThreadContext
void replaceThreadContext(ThreadContext *_tc)
Definition: remote_gdb.hh:157
BaseRemoteGDB::cmd_cont
bool cmd_cont(GdbCommand::Context &ctx)
Definition: remote_gdb.cc:808
EventWrapper< BaseRemoteGDB, &BaseRemoteGDB::singleStep >
BaseRemoteGDB::cmd_unsupported
bool cmd_unsupported(GdbCommand::Context &ctx)
Definition: remote_gdb.cc:792
BaseRemoteGDB::removeHardBreak
void removeHardBreak(Addr addr, size_t len)
Definition: remote_gdb.cc:695
BaseRemoteGDB::cmd_set_thread
bool cmd_set_thread(GdbCommand::Context &ctx)
Definition: remote_gdb.cc:864
BaseRemoteGDB::ConnectEvent
friend ConnectEvent
Definition: remote_gdb.hh:188
BaseGdbRegCache::gdb
BaseRemoteGDB * gdb
Definition: remote_gdb.hh:127
BaseGdbRegCache::BaseGdbRegCache
BaseGdbRegCache(BaseRemoteGDB *g)
Definition: remote_gdb.hh:121
BaseRemoteGDB::connect
void connect()
Definition: remote_gdb.cc:354
BaseRemoteGDB::incomingData
void incomingData(int revent)
Definition: remote_gdb.cc:482
BaseRemoteGDB::GdbCommand::Func
bool(BaseRemoteGDB::* Func)(Context &ctx)
Definition: remote_gdb.hh:275
BaseRemoteGDB::DataEvent
SocketEvent<&BaseRemoteGDB::incomingData > DataEvent
Definition: remote_gdb.hh:186
std::vector< char >
BaseRemoteGDB::singleStepEvent
EventWrapper< BaseRemoteGDB, &BaseRemoteGDB::singleStep > singleStepEvent
Definition: remote_gdb.hh:244
BaseRemoteGDB::cmd_mem_r
bool cmd_mem_r(GdbCommand::Context &ctx)
Definition: remote_gdb.cc:874
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:214
BaseRemoteGDB::GdbCommand::Context::len
int len
Definition: remote_gdb.hh:272
BaseRemoteGDB::clearSingleStep
void clearSingleStep()
Definition: remote_gdb.cc:649
BaseRemoteGDB::SocketEvent::process
void process(int revent)
Definition: remote_gdb.hh:182
BaseRemoteGDB::context
ThreadContext * context()
Definition: remote_gdb.hh:303
BaseRemoteGDB::insertSoftBreak
void insertSoftBreak(Addr addr, size_t len)
Definition: remote_gdb.cc:662
BaseRemoteGDB::setSingleStep
void setSingleStep()
Definition: remote_gdb.cc:655
BaseRemoteGDB::GdbCommand::func
const Func func
Definition: remote_gdb.hh:278
BaseRemoteGDB::listener
ListenSocket listener
Definition: remote_gdb.hh:194
BaseRemoteGDB::SocketEvent
Definition: remote_gdb.hh:172
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:408
Event
Definition: eventq.hh:248
BaseRemoteGDB::GdbCommand::Context::cmd
const GdbCommand * cmd
Definition: remote_gdb.hh:268
System
Definition: system.hh:73
BaseRemoteGDB::_port
int _port
Definition: remote_gdb.hh:195
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:714
BaseRemoteGDB::system
System * system()
Definition: remote_gdb.hh:304
BaseRemoteGDB::getbyte
uint8_t getbyte()
Definition: remote_gdb.cc:499
BaseRemoteGDB::write
bool write(Addr addr, size_t size, const char *data)
Definition: remote_gdb.cc:623
pollevent.hh
BaseRemoteGDB::GdbCommand::GdbCommand
GdbCommand(const char *_name, Func _func)
Definition: remote_gdb.hh:280
BaseRemoteGDB::gdbRegs
virtual BaseGdbRegCache * gdbRegs()=0
BaseRemoteGDB::TrapEvent::gdb
BaseRemoteGDB * gdb
Definition: remote_gdb.hh:222
BaseRemoteGDB::descheduleInstCommitEvent
void descheduleInstCommitEvent(Event *ev)
Deschedule an instruction count based event.
Definition: remote_gdb.cc:722
BaseRemoteGDB::GdbCommand::name
const char *const name
Definition: remote_gdb.hh:277
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:213
BaseRemoteGDB::cmd_mem_w
bool cmd_mem_w(GdbCommand::Context &ctx)
Definition: remote_gdb.cc:898
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:148
BaseRemoteGDB::dataEvent
DataEvent * dataEvent
Definition: remote_gdb.hh:192
BaseRemoteGDB::acc
virtual bool acc(Addr addr, size_t len)=0
BaseRemoteGDB::TrapEvent::TrapEvent
TrapEvent(BaseRemoteGDB *g)
Definition: remote_gdb.hh:225
BaseRemoteGDB::isAttached
bool isAttached()
Definition: remote_gdb.hh:155
BaseRemoteGDB::connectEvent
ConnectEvent * connectEvent
Definition: remote_gdb.hh:191
BaseGdbRegCache
Concrete subclasses of this abstract class represent how the register values are transmitted on the w...
Definition: remote_gdb.hh:72
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:820
ArmISA::e
Bitfield< 9 > e
Definition: miscregs_types.hh:61
BaseRemoteGDB::GdbCommand::Context::cmd_byte
char cmd_byte
Definition: remote_gdb.hh:269
X86ISA::addr
Bitfield< 3 > addr
Definition: types.hh:80
BaseRemoteGDB::cmd_detach
bool cmd_detach(GdbCommand::Context &ctx)
Definition: remote_gdb.cc:833
BaseRemoteGDB::SocketEvent::gdb
BaseRemoteGDB * gdb
Definition: remote_gdb.hh:175
BaseRemoteGDB::getXferFeaturesRead
virtual bool getXferFeaturesRead(const std::string &annex, std::string &output)
Get an XML target description.
Definition: remote_gdb.cc:973
BaseRemoteGDB::GdbCommand::Context::type
int type
Definition: remote_gdb.hh:270
BaseRemoteGDB::removeSoftBreak
void removeSoftBreak(Addr addr, size_t len)
Definition: remote_gdb.cc:671
BaseRemoteGDB::availableFeatures
virtual std::vector< std::string > availableFeatures() const
Definition: remote_gdb.cc:967
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:922
pc_event.hh
BaseRemoteGDB::cmd_clr_hw_bkpt
bool cmd_clr_hw_bkpt(GdbCommand::Context &ctx)
Definition: remote_gdb.cc:1030
types.hh
ArmISA::t
Bitfield< 5 > t
Definition: miscregs_types.hh:67
BaseRemoteGDB::active
bool active
Definition: remote_gdb.hh:210
BaseRemoteGDB::fd
int fd
Definition: remote_gdb.hh:198
BaseRemoteGDB::cmd_step
bool cmd_step(GdbCommand::Context &ctx)
Definition: remote_gdb.cc:1018
ArmISA::len
Bitfield< 18, 16 > len
Definition: miscregs_types.hh:439
BaseRemoteGDB::insertHardBreak
void insertHardBreak(Addr addr, size_t len)
Definition: remote_gdb.cc:680
X86ISA::type
type
Definition: misc.hh:727
BaseRemoteGDB::checkBpLen
virtual bool checkBpLen(size_t len)
Definition: remote_gdb.cc:786
BaseRemoteGDB::name
std::string name()
Definition: remote_gdb.cc:328
BaseRemoteGDB::ConnectEvent
SocketEvent<&BaseRemoteGDB::connectWrapper > ConnectEvent
Definition: remote_gdb.hh:185
BaseRemoteGDB::TrapEvent
Definition: remote_gdb.hh:218
PollEvent
Definition: pollevent.hh:41
BaseRemoteGDB::GdbCommand::Context
Definition: remote_gdb.hh:266
BaseRemoteGDB::listen
void listen()
Definition: remote_gdb.cc:334
BaseRemoteGDB::cmd_reg_r
bool cmd_reg_r(GdbCommand::Context &ctx)
Definition: remote_gdb.cc:840
BaseRemoteGDB::trapEvent
BaseRemoteGDB::TrapEvent trapEvent
BaseRemoteGDB::regCachePtr
BaseGdbRegCache * regCachePtr
Definition: remote_gdb.hh:216
BaseRemoteGDB::SocketEvent::SocketEvent
SocketEvent(BaseRemoteGDB *gdb, int fd, int e)
Definition: remote_gdb.hh:178
BaseRemoteGDB::read
bool read(Addr addr, size_t size, char *data)
Definition: remote_gdb.cc:600
BaseRemoteGDB::GdbCommand::Context::data
char * data
Definition: remote_gdb.hh:271
BaseRemoteGDB::detach
void detach()
Definition: remote_gdb.cc:390
BaseRemoteGDB::recv
void recv(std::vector< char > &bp)
Definition: remote_gdb.cc:519
BaseRemoteGDB::cmd_reg_w
bool cmd_reg_w(GdbCommand::Context &ctx)
Definition: remote_gdb.cc:850
BaseRemoteGDB::BaseRemoteGDB
BaseRemoteGDB(System *system, ThreadContext *context, int _port)
Interface to other parts of the simulator.
Definition: remote_gdb.cc:313
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:571
eventq.hh

Generated on Tue Jun 22 2021 15:28:21 for gem5 by doxygen 1.8.17