2 * Copyright (c) 2010 ARM Limited
5 * The license below extends only to copyright in the software and shall
6 * not be construed as granting a license to any other intellectual
7 * property including but not limited to intellectual property relating
8 * to a hardware implementation of the functionality of the software
9 * licensed hereunder. You may use the software subject to the license
10 * terms below provided that you ensure that this notice is replicated
11 * unmodified and in its entirety in all distributions of the software,
12 * modified or unmodified, in source code or in binary form.
14 * Copyright (c) 2006-2009 The Regents of The University of Michigan
15 * All rights reserved.
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions are
19 * met: redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer;
21 * redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution;
24 * neither the name of the copyright holders nor the names of its
25 * contributors may be used to endorse or promote products derived from
26 * this software without specific prior written permission.
28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
31 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
32 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
33 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
34 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
35 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
36 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
38 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
51 #include "arch/arm/tracechild.hh"
55 ARMTraceChild::ARMTraceChild()
59 memset(®s
, 0, sizeof(regs
));
60 memset(&oldregs
, 0, sizeof(regs
));
61 memset(&fpregs
, 0, sizeof(vfp_regs
));
62 memset(&oldfpregs
, 0, sizeof(vfp_regs
));
64 for (int x
= 0; x
< numregs
; x
++) {
65 regDiffSinceUpdate
[x
] = false;
68 assert(sizeof(regs
.uregs
)/sizeof(regs
.uregs
[0]) > CPSR
);
72 ARMTraceChild::sendState(int socket
)
75 uint64_t message
[numregs
+ 1];
78 for (int x
= 0; x
< numregs
; x
++) {
79 if (regDiffSinceUpdate
[x
]) {
80 message
[0] = message
[0] | (1ULL << x
);
81 message
[pos
++] = getRegVal(x
);
86 size_t toSend
= pos
* sizeof(message
[0]);
87 uint8_t *messagePtr
= (uint8_t *)message
;
89 sent
= write(socket
, messagePtr
, toSend
);
91 cerr
<< "Write failed! " << strerror(errno
) << endl
;
103 ARMTraceChild::getRegs(user_regs
&myregs
, int num
)
105 assert(num
<= CPSR
&& num
>= 0);
106 return myregs
.uregs
[num
];
110 ARMTraceChild::getFpRegs(vfp_regs
&my_fp_regs
, int num
)
112 assert(num
>= F0
&& num
< numregs
);
114 return my_fp_regs
.fpscr
;
117 return my_fp_regs
.fpregs
[num
];
121 ARMTraceChild::update(int pid
)
124 if (ptrace(PTRACE_GETREGS
, pid
, 0, ®s
) != 0) {
125 cerr
<< "update: " << strerror(errno
) << endl
;
129 const uint32_t get_vfp_regs
= 32;
132 if (ptrace((__ptrace_request
)get_vfp_regs
, pid
, 0, &fpregs
) != 0) {
133 cerr
<< "update: " << strerror(errno
) << endl
;
137 for (unsigned int x
= 0; x
< numregs
; x
++)
138 regDiffSinceUpdate
[x
] = (getRegVal(x
) != getOldRegVal(x
));
144 ARMTraceChild::getRegVal(int num
)
147 return getRegs(regs
, num
);
149 return (int64_t)getFpRegs(fpregs
, num
);
153 ARMTraceChild::getOldRegVal(int num
)
156 return getRegs(oldregs
, num
);
158 return (int64_t)getFpRegs(oldfpregs
, num
);
162 ARMTraceChild::outputStartState(ostream
& os
)
164 uint32_t sp
= getSP();
165 uint32_t pc
= getPC();
166 uint32_t highestInfo
= 0;
168 sprintf(obuf
, "Initial stack pointer = 0x%08x\n", sp
);
170 sprintf(obuf
, "Initial program counter = 0x%08x\n", pc
);
173 //Output the argument count
174 int32_t cargc
= ptrace(PTRACE_PEEKDATA
, pid
, sp
, 0);
175 sprintf(obuf
, "0x%08x: Argc = 0x%08x\n", sp
, cargc
);
179 //Output argv pointers
183 cargv
= ptrace(PTRACE_PEEKDATA
, pid
, sp
, 0);
184 sprintf(obuf
, "0x%08x: argv[%d] = 0x%08x\n",
185 sp
, argCount
++, cargv
);
187 if(highestInfo
< cargv
)
193 //Output the envp pointers
197 cenvp
= ptrace(PTRACE_PEEKDATA
, pid
, sp
, 0);
198 sprintf(obuf
, "0x%08x: envp[%d] = 0x%08x\n",
199 sp
, envCount
++, cenvp
);
203 uint32_t auxType
, auxVal
;
205 auxType
= ptrace(PTRACE_PEEKDATA
, pid
, sp
, 0);
207 auxVal
= ptrace(PTRACE_PEEKDATA
, pid
, sp
, 0);
209 sprintf(obuf
, "0x%08x: Auxiliary vector = {0x%08x, 0x%08x}\n",
210 sp
- 8, auxType
, auxVal
);
212 } while(auxType
!= 0 || auxVal
!= 0);
213 //Print out the argument strings, environment strings, and file name.
216 uint32_t currentStart
= sp
;
217 bool clearedInitialPadding
= false;
219 buf
= ptrace(PTRACE_PEEKDATA
, pid
, sp
, 0);
220 char * cbuf
= (char *)&buf
;
221 for (int x
= 0; x
< sizeof(uint32_t); x
++) {
225 sprintf(obuf
, "0x%08x: \"%s\"\n",
226 currentStart
, current
.c_str());
229 currentStart
= sp
+ x
+ 1;
233 clearedInitialPadding
= clearedInitialPadding
|| buf
!= 0;
234 } while(!clearedInitialPadding
|| buf
!= 0 || sp
<= highestInfo
);
239 ARMTraceChild::step()
241 const uint32_t bkpt_inst
= 0xe7f001f0;
243 uint32_t lr
= getRegVal(14);
244 uint32_t pc
= getPC();
245 uint32_t lrOp
, subsOp
;
249 // Since ARM uses software breakpoints behind the scenes, they don't work
250 // in read only areas like the page of routines provided by the kernel. The
251 // link register generally holds the address the process wants to the
252 // kernel to return to after it's done, so we'll install a software
255 // Calls into the kernel user page always follow the form:
257 // <possible MOV lr,...>
260 // So we look for this pattern and set a breakpoint on the LR at the SUB
264 subsOp
= ptrace(PTRACE_PEEKDATA
, pid
, pc
, 0);
265 if ((subsOp
& 0xFFFF0FFF) == 0xe3e00a0f)
268 if (foundMvn
&& ((subsOp
& 0xFFF0F000) == 0xe240f000)) {
270 lrOp
= ptrace(PTRACE_PEEKDATA
, pid
, lr
, 0);
271 ptrace(PTRACE_POKEDATA
, pid
, lr
, bkpt_inst
);
277 ptrace(PTRACE_POKEDATA
, pid
, lr
, lrOp
);
284 return new ARMTraceChild
;