Added ReallocObserver(), and associated code as well.
[aesalon.git] / src / monitor / ptrace / Portal.h
blob9000d9fe32e252092ba76b5dcf233d41dd398e32
1 #ifndef AESALON_MONITOR_PTRACE_PORTAL_H
2 #define AESALON_MONITOR_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 "BreakpointObserver.h"
13 #include "platform/MemoryAddress.h"
14 #include "platform/ArgumentList.h"
15 #include "misc/SmartPointer.h"
16 #include "PTraceException.h"
17 #include "asm/Register.h"
19 namespace Aesalon {
20 namespace Monitor {
21 namespace PTrace {
23 class Portal {
24 protected:
25 typedef std::vector<Misc::SmartPointer<Breakpoint> > breakpoint_list_t;
27 typedef std::vector<Misc::SmartPointer<SignalObserver> > signal_observer_list_t;
28 private:
29 /** The PID of the attached process. */
30 pid_t pid;
32 /** The vector of breakpoints. */
33 breakpoint_list_t breakpoint_list;
35 /** Adds a breakpoint onto the list.
36 @param breakpoint The breakpoint to add.
38 void add_breakpoint(Misc::SmartPointer<Breakpoint> breakpoint) {
39 breakpoint_list.push_back(breakpoint);
42 signal_observer_list_t signal_observer_list;
44 Word libc_offset;
46 int wait_for_signal();
48 Misc::SmartPointer<BreakpointObserver> initial_observer;
49 Misc::SmartPointer<BreakpointObserver> malloc_observer;
50 Misc::SmartPointer<BreakpointObserver> free_observer;
51 Misc::SmartPointer<BreakpointObserver> realloc_observer;
52 public:
53 /** Generic constructor for PTracePortal.
54 @param argument_list The arguments to spawn the child with.
56 Portal(Misc::SmartPointer<Platform::ArgumentList> argument_list);
57 /** Virtual destructor, does nothing. */
58 virtual ~Portal() {}
60 /** Retrieve the value of a specific register.
61 @param which The register to get the value of.
62 @return The value of the specified register.
64 Platform::MemoryAddress get_register(ASM::Register which) const;
66 void set_register(ASM::Register which, Word new_value);
68 /** Read a Word of memory.
69 @param address The address to read.
70 @return The value at @a address.
72 Word read_memory(Platform::MemoryAddress address) const;
73 /** Write a Word of memory.
74 @param address The address to write to.
75 @param value The value to write.
77 void write_memory(Platform::MemoryAddress address, Word value);
78 /** Write a Byte of memory.
79 @param address The address to write to.
80 @param value The value to write.
82 void write_memory(Platform::MemoryAddress address, Byte value);
84 /** Attaches onto the PID @a pid. */
85 void attach();
86 /** Detaches from running program. */
87 void detach();
89 /** Places a breakpoint at a specified address.
90 @param address The address to place the breakpoint at.
91 @param observer The initial observer to use for the breakpoint.
92 @return The ID of the newly-inserted breakpoint.
94 std::size_t place_breakpoint(Platform::MemoryAddress address, Misc::SmartPointer<BreakpointObserver> observer);
96 void remove_breakpoint(Platform::MemoryAddress address);
98 /** Returns a breakpoint by iterator.
99 @param which The breakpoint to find.
100 @return A SmartPointer to the given breakpoint. Not valid if the breakpoint iterator does not exist.
102 Misc::SmartPointer<Breakpoint> get_breakpoint(breakpoint_list_t::size_type which) const {
103 return breakpoint_list.at(which);
106 Misc::SmartPointer<Breakpoint> get_breakpoint_by_id(std::size_t which) const;
108 /** Returns a breakpoint by address.
109 @param address The address to search for.
110 @return A SmartPointer to the given breakpoint. Not valid if no breakpoint exists at @a address.
112 Misc::SmartPointer<Breakpoint> get_breakpoint_by_address(Platform::MemoryAddress address) const;
114 void handle_breakpoint();
116 void handle_signal();
117 /** Continues execution, with a given signal sent to the child.
118 @param signal The signal to send; none if zero.
120 void continue_execution(int signal = 0);
121 /** Tells the child to execute a single instruction. */
122 void single_step();
124 void add_signal_observer(Misc::SmartPointer<SignalObserver> new_observer) {
125 signal_observer_list.push_back(new_observer);
128 Misc::SmartPointer<BreakpointObserver> get_malloc_observer() const { return malloc_observer; }
129 Misc::SmartPointer<BreakpointObserver> get_free_observer() const { return free_observer; }
130 Misc::SmartPointer<BreakpointObserver> get_realloc_observer() const { return realloc_observer; }
132 /** Reads the memory map in /proc for the child process to determine the address libc is lodaded into.
133 @return The address of libc, or 0 if libc is not currently loaded.
135 Word get_libc_offset();
138 } // namespace PTrace
139 } // namespace Monitor
140 } // namespace Aesalon
142 #endif