Changed BidirectionalPipe::BidirectionalPipe() to have a "trace" argument.
[aesalon.git] / src / interface / PTracePortal.cpp
blobd4a57661b4a8bbb2ca7732290b555130ecb5c584
1 #include <sys/user.h>
2 #include <sys/ptrace.h>
3 #include <errno.h>
5 #include "PTracePortal.h"
7 namespace Aesalon {
8 namespace Interface {
10 Platform::MemoryAddress PTracePortal::get_register(register_e which) const {
11 struct user_regs_struct regs;
12 ptrace(PTRACE_GETREGS, pid, 0, &regs);
13 switch(which) {
14 #if AESALON_PLATFORM == AESALON_PLATFORM_x86_64
15 case RAX:
16 return regs.rax;
17 case RBX:
18 return regs.rbx;
19 case RCX:
20 return regs.rcx;
21 case RDX:
22 return regs.rdx;
23 case R9:
24 return regs.r9;
25 case R10:
26 return regs.r10;
27 case R11:
28 return regs.r11;
29 case R12:
30 return regs.r12;
31 case R13:
32 return regs.r13;
33 case R14:
34 return regs.r14;
35 case R15:
36 return regs.r15;
37 case RDI:
38 return regs.rdi;
39 case RSI:
40 return regs.rsi;
41 case RBP:
42 return regs.rbp;
43 case RSP:
44 return regs.rsp;
45 case RIP:
46 return regs.rip;
47 #elif AESALON_PLATFORM == AESALON_PLATFORM_x86
48 case EAX:
49 return regs.eax;
50 /* TODO: implement the rest of the 32-bit registers */
51 #endif
52 case CS:
53 return regs.cs;
54 case SS:
55 return regs.ss;
56 default:
57 return 0;
61 Word PTracePortal::read_memory(Platform::MemoryAddress address) const {
62 Word ret = ptrace(PTRACE_PEEKDATA, pid, address, NULL);
63 if(errno) return 0;
64 return ret;
67 void PTracePortal::write_memory(Platform::MemoryAddress address, Word value) {
68 ptrace(PTRACE_POKEDATA, pid, address, value);
71 void PTracePortal::write_memory(Platform::MemoryAddress address, Byte value) {
72 Word original = read_memory(address);
73 /* Clear the first eight bits of original */
74 original &= ~Word(0xff);
75 /* Set first eight bits to new value */
76 original |= value;
77 ptrace(PTRACE_POKEDATA, pid, address, value);
81 void PTracePortal::attach() {
82 ptrace(PTRACE_ATTACH, pid, NULL, NULL);
85 void PTracePortal::place_breakpoint(Platform::MemoryAddress address) {
86 add_breakpoint(new Breakpoint(address, read_memory(address) & 0xff));
87 write_memory(address, Byte(0xcc));
90 Misc::SmartPointer<Breakpoint> PTracePortal::get_breakpoint_by_address(Platform::MemoryAddress address) const {
91 breakpoint_list_t::const_iterator i = breakpoint_list.begin();
92 for(; i != breakpoint_list.end(); i ++) {
93 if((*i)->get_address() == address) return *i;
95 return NULL;
98 } // namespace Interface
99 } // namespace Aesalon