ROSE  0.9.9.109
BinaryDebugger.h
1 #ifndef ROSE_BinaryAnalysis_BinaryDebugger_H
2 #define ROSE_BinaryAnalysis_BinaryDebugger_H
3 
4 #include <Sawyer/BitVector.h>
5 
6 namespace Rose {
7 namespace BinaryAnalysis {
8 
13 public:
14  enum DetachMode { KILL, DETACH, CONTINUE, NOTHING };
15 private:
17  enum RegPageStatus { REGPAGE_NONE, REGPAGE_REGS, REGPAGE_FPREGS };
18 
19  int child_; // process being debugged (int, not pid_t, for Windows portability)
20  DetachMode howDetach_; // how to detach from the subordinate
21  int wstat_; // last status from waitpid
22  AddressIntervalSet breakpoints_; // list of breakpoint addresses
23  int sendSignal_; // pending signal
24  UserRegDefs userRegDefs_; // how registers map to user_regs_struct in <sys/user.h>
25  UserRegDefs userFpRegDefs_; // how registers map to user_fpregs_struct in <sys/user.h>
26  size_t kernelWordSize_; // cached width in bits of kernel's words
27  uint8_t regsPage_[512]; // latest register information read from subordinate
28  RegPageStatus regsPageStatus_; // what are the contents of regPage_?
29 
30 public:
32  : child_(0), howDetach_(KILL), wstat_(-1), sendSignal_(0), kernelWordSize_(0), regsPageStatus_(REGPAGE_NONE) {
33  init();
34  }
35 
36  BinaryDebugger(int pid)
37  : child_(0), howDetach_(KILL), wstat_(-1), sendSignal_(0), kernelWordSize_(0), regsPageStatus_(REGPAGE_NONE) {
38  init();
39  attach(pid);
40  }
41 
42  BinaryDebugger(const std::string &exeName)
43  : child_(0), howDetach_(KILL), wstat_(-1), sendSignal_(0), kernelWordSize_(0), regsPageStatus_(REGPAGE_NONE) {
44  init();
45  attach(exeName);
46  }
47 
48  BinaryDebugger(const std::vector<std::string> &exeNameAndArgs)
49  : child_(0), howDetach_(KILL), wstat_(-1), sendSignal_(0), kernelWordSize_(0), regsPageStatus_(REGPAGE_NONE) {
50  init();
51  attach(exeNameAndArgs);
52  }
53 
54  ~BinaryDebugger() {
55  detach();
56  }
57 
58 private:
59  BinaryDebugger(const BinaryDebugger&); // not copyable
60  BinaryDebugger& operator=(const BinaryDebugger&); // not copyable
61 
62 public:
67  void attach(int pid, bool attach=true);
68 
74  void attach(const std::string &fileName);
75  void attach(const std::vector<std::string> &fileNameAndArgs);
79  int isAttached() { return child_; }
80 
82  void detach();
83 
85  void terminate();
86 
88  void executionAddress(rose_addr_t va);
89 
91  rose_addr_t executionAddress();
92 
94  void setBreakpoint(const AddressInterval&);
95 
97  void clearBreakpoint(const AddressInterval&);
98 
100  void clearBreakpoints() { breakpoints_.clear(); }
101 
103  void singleStep();
104 
106  void runToBreakpoint();
107 
112  void runToSyscall();
113 
116  size_t kernelWordSize();
117 
126 
131  size_t readMemory(rose_addr_t va, size_t nBytes, uint8_t *buffer);
132 
134  bool isTerminated();
135 
137  std::string howTerminated();
138 
139 private:
140  // Initialize tables during construction
141  void init();
142 
143  // Wait for subordinate or throw on error
144  void waitForChild();
145 
146 };
147 
148 } // namespace
149 } // namespace
150 
151 #endif
void clearBreakpoints()
Remove all breakpoints.
size_t kernelWordSize()
Obtain and cache kernel's word size in bits.
Main namespace for the ROSE library.
Describes (part of) a physical CPU register.
void setBreakpoint(const AddressInterval &)
Set breakpoints.
void terminate()
Terminate the subordinate.
bool isTerminated()
Returns true if the subordinate terminated.
Sawyer::Container::BitVector readRegister(RegisterDescriptor)
Read subordinate register.
std::string howTerminated()
String describing how the subordinate process terminated.
rose_addr_t executionAddress()
Get execution address.
void clearBreakpoint(const AddressInterval &)
Remove breakpoints.
void clear()
Remove all values.
Definition: IntervalSet.h:487
int isAttached()
Returns true if attached to a subordinate.
void detach()
Detach from the subordinate.
void runToSyscall()
Run until the next system call.
size_t readMemory(rose_addr_t va, size_t nBytes, uint8_t *buffer)
Read subordinate memory.
void singleStep()
Execute one instruction.
void runToBreakpoint()
Run until the next breakpoint is reached.
void attach(int pid, bool attach=true)
Attach to an existing process.