Began re-writing the PTracePortal code, in the vain hope that it might help.
[aesalon.git] / src / interface / PTracePortal.h
blobe1c8dce9f4476c46d1311cc97cac8b90e6bbd4eb
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 "PTraceSignalObserver.h"
12 #include "platform/MemoryAddress.h"
13 #include "misc/SmartPointer.h"
15 namespace Aesalon {
16 namespace Interface {
18 class PTraceException : public Misc::Exception {
19 public:
20 PTraceException(std::string message) : Misc::Exception(message) {}
21 virtual ~PTraceException() {}
24 class PTracePortal {
25 public:
26 /** An enum representing the different registers available. */
27 enum register_e {
28 #if AESALON_PLATFORM == AESALON_PLATFORM_x86_64
29 RAX,
30 RBX,
31 RCX,
32 RDX,
33 R9,
34 R10,
35 R11,
36 R12,
37 R13,
38 R14,
39 R15,
40 RBP,
41 RSP,
42 RIP,
43 RDI,
44 RSI,
45 #elif AESALON_PLATFORM == AESALON_PLATFORM_x86
46 EAX,
47 EBX,
48 ECX,
49 EDX,
50 EBP,
51 ESP,
52 EIP,
53 EDI,
54 ESI,
55 #endif
56 CS,
60 typedef std::vector<Misc::SmartPointer<Breakpoint> > breakpoint_list_t;
62 typedef std::vector<Misc::SmartPointer<PTraceSignalObserver> > signal_observer_list_t;
63 private:
64 /** The PID of the attached process. */
65 pid_t pid;
67 /** The vector of breakpoints. */
68 breakpoint_list_t breakpoint_list;
70 /** Adds a breakpoint onto the list.
71 @param breakpoint The breakpoint to add.
73 void add_breakpoint(Misc::SmartPointer<Breakpoint> breakpoint) {
74 breakpoint_list.push_back(breakpoint);
77 signal_observer_list_t signal_observer_list;
78 public:
79 /** Generic constructor for PTracePortal.
80 @param pid The PID of the child process.
82 PTracePortal(pid_t pid) : pid(pid) {}
83 /** Virtual destructor, does nothing. */
84 virtual ~PTracePortal() {}
86 /** Retrieve the value of a specific register.
87 @param which The register to get the value of.
88 @return The value of the specified register.
90 Platform::MemoryAddress get_register(register_e which) const;
92 /** Read a Word of memory.
93 @param address The address to read.
94 @return The value at @a address.
96 Word read_memory(Platform::MemoryAddress address) const;
97 /** Write a Word of memory.
98 @param address The address to write to.
99 @param value The value to write.
101 void write_memory(Platform::MemoryAddress address, Word value);
102 /** Write a Byte of memory.
103 @param address The address to write to.
104 @param value The value to write.
106 void write_memory(Platform::MemoryAddress address, Byte value);
108 /** Attaches onto the PID @a pid. */
109 void attach();
111 /** Places a breakpoint at a specified address.
112 @param address The address to place the breakpoint at.
114 void place_breakpoint(Platform::MemoryAddress address);
116 /** Returns a breakpoint by iterator.
117 @param which The breakpoint to find.
118 @return A SmartPointer to the given breakpoint. Not valid if the breakpoint iterator does not exist.
120 Misc::SmartPointer<Breakpoint> get_breakpoint(breakpoint_list_t::size_type which) const {
121 return breakpoint_list.at(which);
123 /** Returns a breakpoint by address.
124 @param address The address to search for.
125 @return A SmartPointer to the given breakpoint. Not valid if no breakpoint exists at @a address.
127 /*Misc::SmartPointer<Breakpoint> get_breakpoint_by_address(Platform::MemoryAddress address) const;*/
129 /** Handles a signal from the child.
130 @return The signal caught.
132 int handle_signal();
133 /** Continues execution, with a given signal sent to the child.
134 @param signal The signal to send; none if zero.
136 void continue_execution(int signal = 0);
137 /** Tells the child to execute a single instruction. */
138 void single_step();
140 void wait_for_signal();
142 void add_signal_observer(Misc::SmartPointer<PTraceSignalObserver> new_observer) {
143 signal_observer_list.push_back(new_observer);
147 } // namespace Interface
148 } // namespace Aesalon
150 #endif