ROSE  0.9.9.139
BinaryDebugger.h
1 #ifndef ROSE_BinaryAnalysis_BinaryDebugger_H
2 #define ROSE_BinaryAnalysis_BinaryDebugger_H
3 
4 #include <boost/noncopyable.hpp>
5 #include <Sawyer/BitVector.h>
6 
7 namespace Rose {
8 namespace BinaryAnalysis {
9 
13 class BinaryDebugger: private boost::noncopyable {
14 public:
15  enum DetachMode { KILL, DETACH, CONTINUE, NOTHING };
16 
18  enum Flag {
19  ATTACH = 0x00000001,
20  REDIRECT_INPUT = 0x00000002,
21  REDIRECT_OUTPUT = 0x00000004,
22  REDIRECT_ERROR = 0x00000008,
23  CLOSE_FILES = 0x00000010,
24  DEFAULT_FLAGS = 0x00000013
25  };
26 
27 private:
29  enum RegPageStatus { REGPAGE_NONE, REGPAGE_REGS, REGPAGE_FPREGS };
30 
31  int child_; // process being debugged (int, not pid_t, for Windows portability)
32  DetachMode howDetach_; // how to detach from the subordinate
33  int wstat_; // last status from waitpid
34  AddressIntervalSet breakpoints_; // list of breakpoint addresses
35  int sendSignal_; // pending signal
36  UserRegDefs userRegDefs_; // how registers map to user_regs_struct in <sys/user.h>
37  UserRegDefs userFpRegDefs_; // how registers map to user_fpregs_struct in <sys/user.h>
38  size_t kernelWordSize_; // cached width in bits of kernel's words
39  uint8_t regsPage_[512]; // latest register information read from subordinate
40  RegPageStatus regsPageStatus_; // what are the contents of regPage_?
41  unsigned flags_; // operational flags; Flag bit vector
42 
43 public:
44  BinaryDebugger()
45  : child_(0), howDetach_(KILL), wstat_(-1), sendSignal_(0), kernelWordSize_(0), regsPageStatus_(REGPAGE_NONE),
46  flags_(DEFAULT_FLAGS) {
47  init();
48  }
49 
50  BinaryDebugger(int pid, unsigned flags = DEFAULT_FLAGS)
51  : child_(0), howDetach_(KILL), wstat_(-1), sendSignal_(0), kernelWordSize_(0), regsPageStatus_(REGPAGE_NONE),
52  flags_(flags) {
53  init();
54  attach(pid, flags);
55  }
56 
57  BinaryDebugger(const std::string &exeName, unsigned flags = DEFAULT_FLAGS)
58  : child_(0), howDetach_(KILL), wstat_(-1), sendSignal_(0), kernelWordSize_(0), regsPageStatus_(REGPAGE_NONE),
59  flags_(flags) {
60  init();
61  attach(exeName, flags);
62  }
63 
64  BinaryDebugger(const std::vector<std::string> &exeNameAndArgs, unsigned flags = DEFAULT_FLAGS)
65  : child_(0), howDetach_(KILL), wstat_(-1), sendSignal_(0), kernelWordSize_(0), regsPageStatus_(REGPAGE_NONE),
66  flags_(flags) {
67  init();
68  attach(exeNameAndArgs, flags);
69  }
70 
71  ~BinaryDebugger() {
72  detach();
73  }
74 
75 public:
81  void attach(int pid, unsigned flags = DEFAULT_FLAGS);
82 
88  void attach(const std::string &fileName, unsigned flags = DEFAULT_FLAGS);
89  void attach(const std::vector<std::string> &fileNameAndArgs, unsigned flags = DEFAULT_FLAGS);
93  int isAttached() { return child_; }
94 
96  void detach();
97 
99  void terminate();
100 
102  void executionAddress(rose_addr_t va);
103 
105  rose_addr_t executionAddress();
106 
108  void setBreakpoint(const AddressInterval&);
109 
111  void clearBreakpoint(const AddressInterval&);
112 
114  void clearBreakpoints() { breakpoints_.clear(); }
115 
117  void singleStep();
118 
120  void runToBreakpoint();
121 
126  void runToSyscall();
127 
130  size_t kernelWordSize();
131 
140 
145  size_t readMemory(rose_addr_t va, size_t nBytes, uint8_t *buffer);
146 
148  bool isTerminated();
149 
151  std::string howTerminated();
152 
153 private:
154  // Initialize tables during construction
155  void init();
156 
157  // Wait for subordinate or throw on error
158  void waitForChild();
159 
160  // Open /dev/null with the specified flags as the indicated file descriptor, closing what was previously on that
161  // descriptor. If an error occurs, the targetFd is closed anyway.
162  void devNullTo(int targetFd, int openFlags);
163 
164 };
165 
166 } // namespace
167 } // namespace
168 
169 #endif
void clearBreakpoints()
Remove all breakpoints.
Redirect standard error to /dev/null.
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
void attach(int pid, unsigned flags=DEFAULT_FLAGS)
Attach to an existing process.
int isAttached()
Returns true if attached to a subordinate.
Flag
Flags controlling operation.
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.