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 "misc/ArgumentList.h"
14 #include "misc/SmartPointer.h"
15 #include "PTraceException.h"
16 #include "asm/Register.h"
24 typedef std::vector
<Misc::SmartPointer
<Breakpoint
> > breakpoint_list_t
;
26 typedef std::vector
<Misc::SmartPointer
<SignalObserver
> > signal_observer_list_t
;
28 /** The PID of the attached process. */
31 /** The vector of breakpoints. */
32 breakpoint_list_t breakpoint_list
;
34 /** Adds a breakpoint onto the list.
35 @param breakpoint The breakpoint to add.
37 void add_breakpoint(Misc::SmartPointer
<Breakpoint
> breakpoint
) {
38 breakpoint_list
.push_back(breakpoint
);
41 signal_observer_list_t signal_observer_list
;
45 int wait_for_signal();
47 Misc::SmartPointer
<BreakpointObserver
> initial_observer
;
48 Misc::SmartPointer
<BreakpointObserver
> malloc_observer
;
49 Misc::SmartPointer
<BreakpointObserver
> free_observer
;
50 Misc::SmartPointer
<BreakpointObserver
> realloc_observer
;
52 /** Generic constructor for PTracePortal.
53 @param argument_list The arguments to spawn the child with.
55 Portal(Misc::SmartPointer
<Misc::ArgumentList
> argument_list
);
56 /** Virtual destructor, does nothing. */
59 /** Retrieve the value of a specific register.
60 @param which The register to get the value of.
61 @return The value of the specified register.
63 Word
get_register(ASM::Register which
) const;
65 void set_register(ASM::Register which
, Word new_value
);
67 /** Read a Word of memory.
68 @param address The address to read.
69 @return The value at @a address.
71 Word
read_memory(Word address
) const;
72 /** Write a Word of memory.
73 @param address The address to write to.
74 @param value The value to write.
76 void write_memory(Word address
, Word value
);
77 /** Write a Byte of memory.
78 @param address The address to write to.
79 @param value The value to write.
81 void write_memory(Word address
, Byte value
);
83 /** Attaches onto the PID @a pid. */
85 /** Detaches from running program. */
88 /** Places a breakpoint at a specified address.
89 @param address The address to place the breakpoint at.
90 @param observer The initial observer to use for the breakpoint.
91 @return The ID of the newly-inserted breakpoint.
93 std::size_t place_breakpoint(Word address
, Misc::SmartPointer
<BreakpointObserver
> observer
);
95 void remove_breakpoint(Word address
);
97 /** Returns a breakpoint by iterator.
98 @param which The breakpoint to find.
99 @return A SmartPointer to the given breakpoint. Not valid if the breakpoint iterator does not exist.
101 Misc::SmartPointer
<Breakpoint
> get_breakpoint(breakpoint_list_t::size_type which
) const {
102 return breakpoint_list
.at(which
);
105 Misc::SmartPointer
<Breakpoint
> get_breakpoint_by_id(std::size_t which
) const;
107 /** Returns a breakpoint by address.
108 @param address The address to search for.
109 @return A SmartPointer to the given breakpoint. Not valid if no breakpoint exists at @a address.
111 Misc::SmartPointer
<Breakpoint
> get_breakpoint_by_address(Word address
) const;
113 void handle_breakpoint();
115 void handle_signal();
116 /** Continues execution, with a given signal sent to the child.
117 @param signal The signal to send; none if zero.
119 void continue_execution(int signal
= 0);
120 /** Tells the child to execute a single instruction. */
123 void add_signal_observer(Misc::SmartPointer
<SignalObserver
> new_observer
) {
124 signal_observer_list
.push_back(new_observer
);
127 Misc::SmartPointer
<BreakpointObserver
> get_malloc_observer() const { return malloc_observer
; }
128 Misc::SmartPointer
<BreakpointObserver
> get_free_observer() const { return free_observer
; }
129 Misc::SmartPointer
<BreakpointObserver
> get_realloc_observer() const { return realloc_observer
; }
131 /** Reads the memory map in /proc for the child process to determine the address libc is lodaded into.
132 @return The address of libc, or 0 if libc is not currently loaded.
134 Word
get_libc_offset();
137 } // namespace PTrace
138 } // namespace Monitor
139 } // namespace Aesalon