1 #ifndef ROSE_BinaryAnalysis_Debugger_H
2 #define ROSE_BinaryAnalysis_Debugger_H
4 #include <featureTests.h>
5 #ifdef ROSE_ENABLE_BINARY_ANALYSIS
8 #include <boost/filesystem.hpp>
9 #include <boost/noncopyable.hpp>
10 #include <boost/regex.hpp>
11 #include <Disassembler.h>
12 #include <Sawyer/BitVector.h>
13 #include <Sawyer/Message.h>
14 #include <Sawyer/Optional.h>
15 #include <Sawyer/Trace.h>
54 unsigned long persona_;
57 boost::filesystem::path program_;
58 std::vector<std::string> arguments_;
59 boost::filesystem::path workingDirectory_;
60 std::vector<boost::regex> clearEnvVars_;
61 std::map<std::string, std::string> setEnvVars_;
69 : persona_(getPersonality()), pid_(-1) {}
73 : flags_(
DEFAULT_FLAGS), persona_(getPersonality()), pid_(pid) {}
77 : flags_(
DEFAULT_FLAGS), persona_(getPersonality()), program_(name), pid_(-1) {}
80 Specimen(
const boost::filesystem::path &name,
const std::vector<std::string> &args)
81 : flags_(
DEFAULT_FLAGS), persona_(getPersonality()), program_(name), arguments_(args), pid_(-1) {}
84 Specimen(
const std::vector<std::string> &nameAndArgs)
85 : flags_(
DEFAULT_FLAGS), persona_(getPersonality()), program_(nameAndArgs.front()),
86 arguments_(nameAndArgs.begin()+1, nameAndArgs.end()), pid_(-1) {
99 void program(
const boost::filesystem::path &name) {
153 return workingDirectory_;
156 workingDirectory_ = name;
216 void print(std::ostream &out)
const;
219 char** prepareEnvAdjustments()
const;
227 enum RegPageStatus { REGPAGE_NONE, REGPAGE_REGS, REGPAGE_FPREGS };
235 UserRegDefs userRegDefs_;
236 UserRegDefs userFpRegDefs_;
237 size_t kernelWordSize_;
238 uint8_t regsPage_[512];
239 RegPageStatus regsPageStatus_;
248 : child_(0), autoDetach_(
KILL), wstat_(-1), sendSignal_(0), kernelWordSize_(0), regsPageStatus_(REGPAGE_NONE),
249 disassembler_(NULL) {
255 : child_(0), autoDetach_(
KILL), wstat_(-1), sendSignal_(0), kernelWordSize_(0), regsPageStatus_(REGPAGE_NONE),
256 disassembler_(NULL) {
358 template<
class Filter>
363 FilterAction action = filter(va);
404 size_t writeMemory(rose_addr_t va,
size_t nBytes,
const uint8_t *bytes);
411 size_t n =
writeMemory(va,
sizeof(T), (
const uint8_t*)&value);
412 ASSERT_always_require(n ==
sizeof(T));
419 size_t readMemory(rose_addr_t va,
size_t nBytes, uint8_t *buffer);
439 return disassembler_;
452 int remoteOpenFile(
const boost::filesystem::path &fileName,
unsigned flags, mode_t mode);
460 rose_addr_t
remoteMmap(rose_addr_t va,
size_t nBytes,
unsigned prot,
unsigned flags,
const boost::filesystem::path&,
476 void devNullTo(
int targetFd,
int openFlags);
479 static unsigned long getPersonality();
480 static void setPersonality(
unsigned long);
487 std::ostream& operator<<(std::ostream&,
const Debugger::Specimen&);
Close all file descriptors > 2.
void print(std::ostream &out) const
Print some basic info about the specimen.
Records and replays traces.
const std::vector< std::string > & arguments() const
Property: Program command-line arguments.
int remoteSystemCall(int syscallNumber, std::vector< uint64_t > args)
Cause the subordinate to execute a system call.
int isAttached()
Returns true if attached to a subordinate.
void clearBreakpoints()
Remove all breakpoints.
void singleStep()
Execute one instruction.
void append(const Label &label)
Append a label to a trace.
Attach to existing process.
const size_t UNLIMITED(-1)
Effictively unlimited size.
Sawyer::Container::BitVector readMemory(rose_addr_t va, size_t nBytes, ByteOrder::Endianness order)
Read subordinate memory as a bit vector.
void workingDirectory(const boost::filesystem::path &name)
Property: Current working directory for running a program.
Debugger(const Specimen &specimen)
Construct a debugger attached to a specimen.
Sawyer::Container::Trace< rose_addr_t > trace()
Run the program and return an execution trace.
boost::filesystem::path workingDirectory() const
Property: Current working directory for running a program.
int remoteOpenFile(const boost::filesystem::path &fileName, unsigned flags, mode_t mode)
Cause the subordinate to open a file.
DetachMode
How to detach from a process when the debugger is destroyed.
Redirect output to /dev/null.
size_t kernelWordSize()
Obtain and cache kernel's word size in bits.
void eraseMatchingEnvironmentVariables(const boost::regex &)
Remove some environment variables.
Specimen(const boost::filesystem::path &name, const std::vector< std::string > &args)
Construct a specimen description for a program with arguments.
bool isSet(Enum e) const
Test whether a bit is set.
Redirect input from /dev/null.
static Ptr instance()
Create a debugger object that isn't attached to any subordinate process.
Redirect standard error to /dev/null.
void runToBreakpoint()
Run until the next breakpoint is reached.
Main namespace for the ROSE library.
boost::filesystem::path program() const
Property: Name of executable program to run.
Sawyer::SharedPointer< class Debugger > DebuggerPtr
Shared-ownership pointer to Debugger.
void detach(Sawyer::Optional< DetachMode > mode=Sawyer::Nothing())
Detach from the subordinate.
Sawyer::SharedPointer< Debugger > Ptr
Shared-ownership pointer to Debugger.
Sawyer::Container::BitVector readRegister(RegisterDescriptor)
Read subordinate register.
void attach(const Specimen &, Sawyer::Optional< DetachMode > onDelete=Sawyer::Nothing())
Attach to a specimen.
Reference-counting intrusive smart pointer.
int waitpidStatus() const
Returns the last status from a call to waitpid.
Specimen(int pid)
Construct a specimen description for a process.
Specimen()
Default construct an empty specimen descriptor.
bool randomizedAddresses() const
Property: Whether to randomize addresses of a process.
void writeMemory(rose_addr_t va, const T &value)
Write subordinate memory.
const BitFlags< Flag > & flags() const
Property: Operational flags.
Sawyer::Container::Trace< rose_addr_t > trace(Filter &filter)
Run the program and return an execution trace.
void writeRegister(RegisterDescriptor, const Sawyer::Container::BitVector &)
Write subordinate register.
void persona(unsigned long bits)
Property: Personality flags.
int remoteCloseFile(unsigned remoteFd)
Cause the subordinate to close a file.
Describes (part of) a physical CPU register.
static void initDiagnostics()
Initialize diagnostic output.
void terminate()
Terminate the subordinate.
Describes the specimen to be debugged.
int process() const
Property: Process ID.
void arguments(const std::vector< std::string > &args)
Property: Program command-line arguments.
bool isClear(Enum e) const
Test whether a bit is clear.
void setBreakpoint(const AddressInterval &)
Set breakpoints.
BitFlags< FilterActionFlags > FilterAction
Return value for tracing.
static Ptr instance(const Specimen &specimen)
Create a debugger and start debugging a specimen.
Disassembler * disassembler() const
Disassembler.
Specimen(const std::vector< std::string > &nameAndArgs)
Construct a specimen description from combined program and arguments.
void clear()
Remove all values.
FilterActionFlags
Action for trace filter callback.
void eraseAllEnvironmentVariables()
Remove all environment variables.
void process(int pid)
Property: Process ID.
void runToSyscall()
Run until the next system call.
void program(const boost::filesystem::path &name)
Property: Name of executable program to run.
static Sawyer::Message::Facility mlog
Diagnostic facility for debugger.
Specimen(const boost::filesystem::path &name)
Construct a specimen description for a program with no arguments.
Base class for reference counted objects.
size_t writeMemory(rose_addr_t va, size_t nBytes, const uint8_t *bytes)
Writes some bytes to subordinate memory.
void clearBreakpoint(const AddressInterval &)
Remove breakpoints.
void eraseEnvironmentVariable(const std::string &)
Remove an environment variable.
std::string howTerminated()
String describing how the subordinate process terminated.
const RegisterDictionary * registerDictionary() const
Available registers.
unsigned long persona() const
Property: Personality flags.
Defines registers available for a particular architecture.
void insertEnvironmentVariable(const std::string &name, const std::string &value)
Add an environment variable.
Simply detach leaving process in current state.
rose_addr_t remoteMmap(rose_addr_t va, size_t nBytes, unsigned prot, unsigned flags, const boost::filesystem::path &, off_t offset)
Map a new memory region in the subordinate.
bool isTerminated()
Returns true if the subordinate terminated.
Virtual base class for instruction disassemblers.
Abort tracing, either appending or rejecting the current address.
Detach from process and cause it to continue running.
rose_addr_t executionAddress()
Get execution address.
BitFlags< Flag > & flags()
Property: Operational flags.
Flag
Flags controlling operation.
Reject the current address, not appending it to the trace.
std::string readCString(rose_addr_t va, size_t maxBytes=UNLIMITED)
Read C-style NUL-terminated string from subordinate.