2 * Copyright (c) 2007 The Regents of The University of Michigan
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 #include <sys/ptrace.h>
36 #include "tracechild_amd64.hh"
40 char * AMD64TraceChild::regNames
[numregs
] = {
42 "rax", "rbx", "rcx", "rdx",
45 //Base pointer and stack pointer
47 //New 64 bit mode registers
48 "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
49 //Segmentation registers
50 "cs", "ds", "es", "fs", "gs", "ss", "fs_base", "gs_base",
56 bool AMD64TraceChild::sendState(int socket
)
59 for(int x
= 0; x
<= R15
; x
++)
61 regVal
= getRegVal(x
);
62 if(write(socket
, ®Val
, sizeof(regVal
)) == -1)
64 cerr
<< "Write failed! " << strerror(errno
) << endl
;
69 regVal
= getRegVal(RIP
);
70 if(write(socket
, ®Val
, sizeof(regVal
)) == -1)
72 cerr
<< "Write failed! " << strerror(errno
) << endl
;
79 int64_t AMD64TraceChild::getRegs(user_regs_struct
& myregs
, int num
)
81 assert(num
< numregs
&& num
>= 0);
85 case RAX
: return myregs
.rax
;
86 case RBX
: return myregs
.rbx
;
87 case RCX
: return myregs
.rcx
;
88 case RDX
: return myregs
.rdx
;
90 case RSI
: return myregs
.rsi
;
91 case RDI
: return myregs
.rdi
;
92 //Base pointer and stack pointer
93 case RBP
: return myregs
.rbp
;
94 case RSP
: return myregs
.rsp
;
95 //New 64 bit mode registers
96 case R8
: return myregs
.r8
;
97 case R9
: return myregs
.r9
;
98 case R10
: return myregs
.r10
;
99 case R11
: return myregs
.r11
;
100 case R12
: return myregs
.r12
;
101 case R13
: return myregs
.r13
;
102 case R14
: return myregs
.r14
;
103 case R15
: return myregs
.r15
;
104 //Segmentation registers
105 case CS
: return myregs
.cs
;
106 case DS
: return myregs
.ds
;
107 case ES
: return myregs
.es
;
108 case FS
: return myregs
.fs
;
109 case GS
: return myregs
.gs
;
110 case SS
: return myregs
.ss
;
111 case FS_BASE
: return myregs
.fs_base
;
112 case GS_BASE
: return myregs
.gs_base
;
114 case RIP
: return myregs
.rip
;
116 case EFLAGS
: return myregs
.eflags
;
123 bool AMD64TraceChild::update(int pid
)
126 if(ptrace(PTRACE_GETREGS
, pid
, 0, ®s
) != 0)
128 cerr
<< "update: " << strerror(errno
) << endl
;
131 for(unsigned int x
= 0; x
< numregs
; x
++)
132 regDiffSinceUpdate
[x
] = (getRegVal(x
) != getOldRegVal(x
));
136 AMD64TraceChild::AMD64TraceChild()
138 for(unsigned int x
= 0; x
< numregs
; x
++)
139 regDiffSinceUpdate
[x
] = false;
142 int64_t AMD64TraceChild::getRegVal(int num
)
144 return getRegs(regs
, num
);
147 int64_t AMD64TraceChild::getOldRegVal(int num
)
149 return getRegs(oldregs
, num
);
152 char * AMD64TraceChild::printReg(int num
)
154 sprintf(printBuffer
, "0x%08X", getRegVal(num
));
158 ostream
& AMD64TraceChild::outputStartState(ostream
& os
)
160 uint64_t sp
= getSP();
161 uint64_t pc
= getPC();
163 sprintf(obuf
, "Initial stack pointer = 0x%016llx\n", sp
);
165 sprintf(obuf
, "Initial program counter = 0x%016llx\n", pc
);
168 //Output the argument count
169 uint64_t cargc
= ptrace(PTRACE_PEEKDATA
, pid
, sp
, 0);
170 sprintf(obuf
, "0x%016llx: Argc = 0x%016llx\n", sp
, cargc
);
174 //Output argv pointers
179 cargv
= ptrace(PTRACE_PEEKDATA
, pid
, sp
, 0);
180 sprintf(obuf
, "0x%016llx: argv[%d] = 0x%016llx\n",
181 sp
, argCount
++, cargv
);
186 //Output the envp pointers
191 cenvp
= ptrace(PTRACE_PEEKDATA
, pid
, sp
, 0);
192 sprintf(obuf
, "0x%016llx: envp[%d] = 0x%016llx\n",
193 sp
, envCount
++, cenvp
);
197 uint64_t auxType
, auxVal
;
200 auxType
= ptrace(PTRACE_PEEKDATA
, pid
, sp
, 0);
202 auxVal
= ptrace(PTRACE_PEEKDATA
, pid
, sp
, 0);
204 sprintf(obuf
, "0x%016llx: Auxiliary vector = {0x%016llx, 0x%016llx}\n",
205 sp
- 16, auxType
, auxVal
);
207 } while(auxType
!= 0 || auxVal
!= 0);
208 //Print out the argument strings, environment strings, and file name.
211 uint64_t currentStart
= sp
;
212 bool clearedInitialPadding
= false;
215 buf
= ptrace(PTRACE_PEEKDATA
, pid
, sp
, 0);
216 char * cbuf
= (char *)&buf
;
217 for(int x
= 0; x
< sizeof(uint64_t); x
++)
223 sprintf(obuf
, "0x%016llx: \"%s\"\n",
224 currentStart
, current
.c_str());
227 currentStart
= sp
+ x
+ 1;
231 clearedInitialPadding
= clearedInitialPadding
|| buf
!= 0;
232 } while(!clearedInitialPadding
|| buf
!= 0);
236 TraceChild
* genTraceChild()
238 return new AMD64TraceChild
;