1 #ifndef AESALON_MONITOR_PTRACE_PORTAL_H
2 #define AESALON_MONITOR_PTRACE_PORTAL_H
9 #include "Breakpoint.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"
25 typedef std::vector
<Misc::SmartPointer
<Breakpoint
> > breakpoint_list_t
;
27 typedef std::vector
<Misc::SmartPointer
<SignalObserver
> > signal_observer_list_t
;
28 typedef std::vector
<Misc::SmartPointer
<BreakpointObserver
> > breakpoint_observer_list_t
;
30 /** The PID of the attached process. */
33 /** The vector of breakpoints. */
34 breakpoint_list_t breakpoint_list
;
36 /** Adds a breakpoint onto the list.
37 @param breakpoint The breakpoint to add.
39 void add_breakpoint(Misc::SmartPointer
<Breakpoint
> breakpoint
) {
40 breakpoint_list
.push_back(breakpoint
);
43 signal_observer_list_t signal_observer_list
;
45 breakpoint_observer_list_t breakpoint_observer_list
;
49 int wait_for_signal();
51 /** Generic constructor for PTracePortal.
52 @param argument_list The arguments to spawn the child with.
54 Portal(Misc::SmartPointer
<Platform::ArgumentList
> argument_list
);
55 /** Virtual destructor, does nothing. */
58 /** Retrieve the value of a specific register.
59 @param which The register to get the value of.
60 @return The value of the specified register.
62 Platform::MemoryAddress
get_register(ASM::Register which
) const;
64 void set_register(ASM::Register which
, Word new_value
);
66 /** Read a Word of memory.
67 @param address The address to read.
68 @return The value at @a address.
70 Word
read_memory(Platform::MemoryAddress address
) const;
71 /** Write a Word of memory.
72 @param address The address to write to.
73 @param value The value to write.
75 void write_memory(Platform::MemoryAddress address
, Word value
);
76 /** Write a Byte of memory.
77 @param address The address to write to.
78 @param value The value to write.
80 void write_memory(Platform::MemoryAddress address
, Byte value
);
82 /** Attaches onto the PID @a pid. */
84 /** Detaches from running program. */
87 /** Places a breakpoint at a specified address.
88 @param address The address to place the breakpoint at.
89 @return The ID of the newly-inserted breakpoint.
91 std::size_t place_breakpoint(Platform::MemoryAddress address
);
93 void remove_breakpoint(Platform::MemoryAddress address
);
95 /** Returns a breakpoint by iterator.
96 @param which The breakpoint to find.
97 @return A SmartPointer to the given breakpoint. Not valid if the breakpoint iterator does not exist.
99 Misc::SmartPointer
<Breakpoint
> get_breakpoint(breakpoint_list_t::size_type which
) const {
100 return breakpoint_list
.at(which
);
103 Misc::SmartPointer
<Breakpoint
> get_breakpoint_by_id(std::size_t which
) const;
105 /** Returns a breakpoint by address.
106 @param address The address to search for.
107 @return A SmartPointer to the given breakpoint. Not valid if no breakpoint exists at @a address.
109 Misc::SmartPointer
<Breakpoint
> get_breakpoint_by_address(Platform::MemoryAddress address
) const;
111 void handle_breakpoint();
113 void handle_signal();
114 /** Continues execution, with a given signal sent to the child.
115 @param signal The signal to send; none if zero.
117 void continue_execution(int signal
= 0);
118 /** Tells the child to execute a single instruction. */
121 void add_signal_observer(Misc::SmartPointer
<SignalObserver
> new_observer
) {
122 signal_observer_list
.push_back(new_observer
);
124 void add_breakpoint_observer(Misc::SmartPointer
<BreakpointObserver
> new_observer
) {
125 breakpoint_observer_list
.push_back(new_observer
);
128 /** Reads the memory map in /proc for the child process to determine the address libc is lodaded into.
129 @return The address of libc, or 0 if libc is not currently loaded.
131 Word
get_libc_offset();
133 void wait_for_breakpoint(Misc::SmartPointer
<Breakpoint
> breakpoint
);
136 } // namespace PTrace
137 } // namespace Monitor
138 } // namespace Aesalon