ROSE  0.11.145.0
Debugger/Base.h
1 #ifndef ROSE_BinaryAnalysis_Debugger_Base_H
2 #define ROSE_BinaryAnalysis_Debugger_Base_H
3 #include <featureTests.h>
4 #ifdef ROSE_ENABLE_BINARY_ANALYSIS
5 #include <Rose/BinaryAnalysis/Debugger/BasicTypes.h>
6 
7 #include <Rose/BinaryAnalysis/Debugger/ThreadId.h>
8 
9 #include <Sawyer/BitVector.h>
10 #include <Sawyer/SharedObject.h>
11 #include <Sawyer/Trace.h>
12 
13 namespace Rose {
14 namespace BinaryAnalysis {
15 namespace Debugger {
16 
20 class Base: public Sawyer::SharedObject {
21 public:
23  using Ptr = Debugger::Ptr;
24 
25 protected:
26  Disassembler::BasePtr disassembler_; // how to disassemble instructions
27 
28 protected:
29  Base();
30 
31  // Debuggers are not copyable
32  Base(const Base&) = delete;
33  Base(Base&&) = delete;
34  Base& operator=(const Base&) = delete;
35  Base& operator=(Base&&) = delete;
36 
37 public:
38  virtual ~Base();
39 
41  // Properties
43 public:
44 
47 
49  // Process state
51 public:
52 
56  virtual bool isAttached() = 0;
57 
62  virtual void detach() = 0;
63 
67  virtual void terminate() = 0;
68 
70  virtual bool isTerminated() = 0;
71 
73  virtual std::string howTerminated() = 0;
74 
76  // Threads
78 public:
79 
83  virtual std::vector<ThreadId> threadIds() = 0;
84 
86  // Break points
88 public:
89 
91  virtual void setBreakPoint(const AddressInterval&) = 0;
92 
94  virtual void clearBreakPoint(const AddressInterval&) = 0;
95 
97  virtual void clearBreakPoints() = 0;
98 
100  // Execution
102 public:
103 
110  virtual void executionAddress(ThreadId, rose_addr_t);
111  virtual rose_addr_t executionAddress(ThreadId);
115  virtual void singleStep(ThreadId) = 0;
116 
118  virtual void runToBreakPoint(ThreadId) = 0;
119 
122 
128  template<class Filter>
131  while (!isTerminated()) {
132  rose_addr_t va = executionAddress(tid);
133  FilterAction action = filter(va);
134  if (action.isClear(FilterActionFlag::REJECT))
135  retval.append(va);
136  if (action.isSet(FilterActionFlag::STOP))
137  return retval;
138  singleStep(tid);
139  }
140  return retval;
141  }
142 
144  // Registers
146 public:
147 
150 
152  virtual std::string registerName(RegisterDescriptor);
153 
163 
168  virtual void writeRegister(ThreadId, RegisterDescriptor, uint64_t value) = 0;
171  virtual std::vector<RegisterDescriptor> availableRegisters() = 0;
172 
177 
182  virtual void writeAllRegisters(ThreadId, const Sawyer::Container::BitVector&) = 0;
183 
185  // Memory
187 public:
188 
192  virtual size_t readMemory(rose_addr_t va, size_t nBytes, uint8_t *buffer) = 0;
193 
197  virtual std::vector<uint8_t> readMemory(rose_addr_t va, size_t nBytes) = 0;
198 
204  virtual Sawyer::Container::BitVector readMemory(rose_addr_t va, size_t nBytes, ByteOrder::Endianness order) = 0;
205 
209  virtual size_t writeMemory(rose_addr_t va, size_t nBytes, const uint8_t *bytes) = 0;
210 
214  template<typename T>
215  void writeMemory(rose_addr_t va, const T &value) {
216  size_t n = writeMemory(va, sizeof(T), (const uint8_t*)&value);
217  ASSERT_always_require(n == sizeof(T));
218  }
219 
225  virtual std::string readCString(rose_addr_t va, size_t maxBytes = UNLIMITED);
226 
227 
228 
229 
230 };
231 
232 } // namespace
233 } // namespace
234 } // namespace
235 
236 #endif
237 #endif
Records and replays traces.
Definition: Trace.h:262
void append(const Label &label)
Append a label to a trace.
Definition: Trace.h:476
Sawyer::Container::Trace< rose_addr_t > trace(ThreadId tid, Filter &filter)
Run the program and return an execution trace.
virtual void executionAddress(ThreadId, rose_addr_t)
Execution address.
virtual void setBreakPoint(const AddressInterval &)=0
Set breakpoints.
void writeMemory(rose_addr_t va, const T &value)
Write subordinate memory.
bool isSet(Enum e) const
Test whether a bit is set.
Main namespace for the ROSE library.
Stores a vector of enum bit flags.
Definition: Rose/BitFlags.h:59
virtual Sawyer::Container::Trace< rose_addr_t > trace()
Run the program and return an execution trace.
const size_t UNLIMITED(static_cast< size_t >(-1))
Effictively unlimited size.
Reject the current address, not appending it to the trace.
virtual std::string howTerminated()=0
String describing how the subordinate process terminated.
virtual bool isAttached()=0
Tests whether this debugger is attached to a specimen.
virtual Disassembler::BasePtr disassembler()
Property: Disassembler.
virtual void singleStep(ThreadId)=0
Execute one machine instruction.
Abort tracing, either appending or rejecting the current address.
virtual void writeAllRegisters(ThreadId, const Sawyer::Container::BitVector &)=0
Write all registers as a single bit vector.
Describes (part of) a physical CPU register.
virtual std::string readCString(rose_addr_t va, size_t maxBytes=UNLIMITED)
Read C-style NUL-terminated string from subordinate.
virtual size_t readMemory(rose_addr_t va, size_t nBytes, uint8_t *buffer)=0
Read subordinate memory.
virtual bool isTerminated()=0
Returns true if the subordinate terminated.
virtual void detach()=0
Detach from a specimen.
virtual Sawyer::Container::BitVector readAllRegisters(ThreadId)=0
Read all available register values as a single bit vector.
bool isClear(Enum e) const
Test whether a bit is clear.
virtual Sawyer::Container::BitVector readRegister(ThreadId, RegisterDescriptor)=0
Read subordinate register.
virtual std::string registerName(RegisterDescriptor)
Convert a register descriptor to a register name.
Base class for reference counted objects.
Definition: SharedObject.h:64
virtual void terminate()=0
Terminate the specimen.
virtual void clearBreakPoints()=0
Remove all breakpoints.
virtual RegisterDictionaryPtr registerDictionary()
Register dictionary for the architecture.
virtual size_t writeMemory(rose_addr_t va, size_t nBytes, const uint8_t *bytes)=0
Writes some bytes to subordinate memory.
virtual void writeRegister(ThreadId, RegisterDescriptor, const Sawyer::Container::BitVector &)=0
Write subordinate register.
virtual std::vector< ThreadId > threadIds()=0
List of subordinate threads.
virtual void runToBreakPoint(ThreadId)=0
Run until the next breakpoint is reached.
Base class for debuggers.
Definition: Debugger/Base.h:20
virtual void clearBreakPoint(const AddressInterval &)=0
Remove breakpoints.