Moved PTrace* into PTrace::.
[aesalon.git] / src / interface / ptrace / Portal.h
blobf0dbb89464148adcd15530a656dab85498e845c3
1 #ifndef AESALON_INTERFACE_PTRACE_PORTAL_H
2 #define AESALON_INTERFACE_PTRACE_PORTAL_H
4 #include <sys/types.h>
5 #include <sys/user.h>
7 #include <vector>
9 #include "Breakpoint.h"
10 #include "Types.h"
11 #include "SignalObserver.h"
12 #include "platform/MemoryAddress.h"
13 #include "misc/SmartPointer.h"
15 namespace Aesalon {
16 namespace Interface {
17 namespace PTrace {
19 class PTraceException : public Misc::Exception {
20 public:
21 PTraceException(std::string message) : Misc::Exception(message) {}
22 virtual ~PTraceException() {}
25 class Portal {
26 public:
27 /** An enum representing the different registers available. */
28 enum register_e {
29 #if AESALON_PLATFORM == AESALON_PLATFORM_x86_64
30 RAX,
31 RBX,
32 RCX,
33 RDX,
34 R9,
35 R10,
36 R11,
37 R12,
38 R13,
39 R14,
40 R15,
41 RBP,
42 RSP,
43 RIP,
44 RDI,
45 RSI,
46 #elif AESALON_PLATFORM == AESALON_PLATFORM_x86
47 EAX,
48 EBX,
49 ECX,
50 EDX,
51 EBP,
52 ESP,
53 EIP,
54 EDI,
55 ESI,
56 #endif
57 CS,
61 typedef std::vector<Misc::SmartPointer<Breakpoint> > breakpoint_list_t;
63 typedef std::vector<Misc::SmartPointer<PTraceSignalObserver> > signal_observer_list_t;
64 private:
65 /** The PID of the attached process. */
66 pid_t pid;
68 /** The vector of breakpoints. */
69 breakpoint_list_t breakpoint_list;
71 /** Adds a breakpoint onto the list.
72 @param breakpoint The breakpoint to add.
74 void add_breakpoint(Misc::SmartPointer<Breakpoint> breakpoint) {
75 breakpoint_list.push_back(breakpoint);
78 signal_observer_list_t signal_observer_list;
79 public:
80 /** Generic constructor for PTracePortal.
81 @param pid The PID of the child process.
83 Portal(pid_t pid) : pid(pid) {}
84 /** Virtual destructor, does nothing. */
85 virtual ~Portal() {}
87 /** Retrieve the value of a specific register.
88 @param which The register to get the value of.
89 @return The value of the specified register.
91 Platform::MemoryAddress get_register(register_e which) const;
93 /** Read a Word of memory.
94 @param address The address to read.
95 @return The value at @a address.
97 Word read_memory(Platform::MemoryAddress address) const;
98 /** Write a Word of memory.
99 @param address The address to write to.
100 @param value The value to write.
102 void write_memory(Platform::MemoryAddress address, Word value);
103 /** Write a Byte of memory.
104 @param address The address to write to.
105 @param value The value to write.
107 void write_memory(Platform::MemoryAddress address, Byte value);
109 /** Attaches onto the PID @a pid. */
110 void attach();
112 /** Places a breakpoint at a specified address.
113 @param address The address to place the breakpoint at.
115 void place_breakpoint(Platform::MemoryAddress address);
117 /** Returns a breakpoint by iterator.
118 @param which The breakpoint to find.
119 @return A SmartPointer to the given breakpoint. Not valid if the breakpoint iterator does not exist.
121 Misc::SmartPointer<Breakpoint> get_breakpoint(breakpoint_list_t::size_type which) const {
122 return breakpoint_list.at(which);
124 /** Returns a breakpoint by address.
125 @param address The address to search for.
126 @return A SmartPointer to the given breakpoint. Not valid if no breakpoint exists at @a address.
128 /*Misc::SmartPointer<Breakpoint> get_breakpoint_by_address(Platform::MemoryAddress address) const;*/
130 void handle_signal();
131 /** Continues execution, with a given signal sent to the child.
132 @param signal The signal to send; none if zero.
134 void continue_execution(int signal = 0);
135 /** Tells the child to execute a single instruction. */
136 void single_step();
138 void wait_for_signal();
140 void add_signal_observer(Misc::SmartPointer<PTraceSignalObserver> new_observer) {
141 signal_observer_list.push_back(new_observer);
145 } // namespace PTrace
146 } // namespace Interface
147 } // namespace Aesalon
149 #endif