debug-arm.cc revision 7f4d5bd8c03935e2c0cd412e561b8fc5a6a880ae
1// Copyright 2006-2008 the V8 project authors. All rights reserved.
2// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6//     * Redistributions of source code must retain the above copyright
7//       notice, this list of conditions and the following disclaimer.
8//     * Redistributions in binary form must reproduce the above
9//       copyright notice, this list of conditions and the following
10//       disclaimer in the documentation and/or other materials provided
11//       with the distribution.
12//     * Neither the name of Google Inc. nor the names of its
13//       contributors may be used to endorse or promote products derived
14//       from this software without specific prior written permission.
15//
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.
27
28#include "v8.h"
29
30#if defined(V8_TARGET_ARCH_ARM)
31
32#include "codegen-inl.h"
33#include "debug.h"
34
35namespace v8 {
36namespace internal {
37
38#ifdef ENABLE_DEBUGGER_SUPPORT
39bool BreakLocationIterator::IsDebugBreakAtReturn() {
40  return Debug::IsDebugBreakAtReturn(rinfo());
41}
42
43
44void BreakLocationIterator::SetDebugBreakAtReturn() {
45  // Patch the code changing the return from JS function sequence from
46  //   mov sp, fp
47  //   ldmia sp!, {fp, lr}
48  //   add sp, sp, #4
49  //   bx lr
50  // to a call to the debug break return code.
51  // #if USE_BLX
52  //   ldr ip, [pc, #0]
53  //   blx ip
54  // #else
55  //   mov lr, pc
56  //   ldr pc, [pc, #-4]
57  // #endif
58  //   <debug break return code entry point address>
59  //   bktp 0
60  CodePatcher patcher(rinfo()->pc(), Assembler::kJSReturnSequenceInstructions);
61#ifdef USE_BLX
62  patcher.masm()->ldr(v8::internal::ip, MemOperand(v8::internal::pc, 0));
63  patcher.masm()->blx(v8::internal::ip);
64#else
65  patcher.masm()->mov(v8::internal::lr, v8::internal::pc);
66  patcher.masm()->ldr(v8::internal::pc, MemOperand(v8::internal::pc, -4));
67#endif
68  patcher.Emit(Debug::debug_break_return()->entry());
69  patcher.masm()->bkpt(0);
70}
71
72
73// Restore the JS frame exit code.
74void BreakLocationIterator::ClearDebugBreakAtReturn() {
75  rinfo()->PatchCode(original_rinfo()->pc(),
76                     Assembler::kJSReturnSequenceInstructions);
77}
78
79
80// A debug break in the frame exit code is identified by the JS frame exit code
81// having been patched with a call instruction.
82bool Debug::IsDebugBreakAtReturn(RelocInfo* rinfo) {
83  ASSERT(RelocInfo::IsJSReturn(rinfo->rmode()));
84  return rinfo->IsPatchedReturnSequence();
85}
86
87
88bool BreakLocationIterator::IsDebugBreakAtSlot() {
89  ASSERT(IsDebugBreakSlot());
90  // Check whether the debug break slot instructions have been patched.
91  return rinfo()->IsPatchedDebugBreakSlotSequence();
92}
93
94
95void BreakLocationIterator::SetDebugBreakAtSlot() {
96  ASSERT(IsDebugBreakSlot());
97  // Patch the code changing the debug break slot code from
98  //   mov r2, r2
99  //   mov r2, r2
100  //   mov r2, r2
101  // to a call to the debug break slot code.
102  // #if USE_BLX
103  //   ldr ip, [pc, #0]
104  //   blx ip
105  // #else
106  //   mov lr, pc
107  //   ldr pc, [pc, #-4]
108  // #endif
109  //   <debug break slot code entry point address>
110  CodePatcher patcher(rinfo()->pc(), Assembler::kDebugBreakSlotInstructions);
111#ifdef USE_BLX
112  patcher.masm()->ldr(v8::internal::ip, MemOperand(v8::internal::pc, 0));
113  patcher.masm()->blx(v8::internal::ip);
114#else
115  patcher.masm()->mov(v8::internal::lr, v8::internal::pc);
116  patcher.masm()->ldr(v8::internal::pc, MemOperand(v8::internal::pc, -4));
117#endif
118  patcher.Emit(Debug::debug_break_return()->entry());
119}
120
121
122void BreakLocationIterator::ClearDebugBreakAtSlot() {
123  ASSERT(IsDebugBreakSlot());
124  rinfo()->PatchCode(original_rinfo()->pc(),
125                     Assembler::kDebugBreakSlotInstructions);
126}
127
128
129#define __ ACCESS_MASM(masm)
130
131
132static void Generate_DebugBreakCallHelper(MacroAssembler* masm,
133                                          RegList pointer_regs) {
134  // Save the content of all general purpose registers in memory. This copy in
135  // memory is later pushed onto the JS expression stack for the fake JS frame
136  // generated and also to the C frame generated on top of that. In the JS
137  // frame ONLY the registers containing pointers will be pushed on the
138  // expression stack. This causes the GC to update these  pointers so that
139  // they will have the correct value when returning from the debugger.
140  __ SaveRegistersToMemory(kJSCallerSaved);
141
142  __ EnterInternalFrame();
143
144  // Store the registers containing object pointers on the expression stack to
145  // make sure that these are correctly updated during GC.
146  // Use sp as base to push.
147  __ CopyRegistersFromMemoryToStack(sp, pointer_regs);
148
149#ifdef DEBUG
150  __ RecordComment("// Calling from debug break to runtime - come in - over");
151#endif
152  __ mov(r0, Operand(0));  // no arguments
153  __ mov(r1, Operand(ExternalReference::debug_break()));
154
155  CEntryStub ceb(1, ExitFrame::MODE_DEBUG);
156  __ CallStub(&ceb);
157
158  // Restore the register values containing object pointers from the expression
159  // stack in the reverse order as they where pushed.
160  // Use sp as base to pop.
161  __ CopyRegistersFromStackToMemory(sp, r3, pointer_regs);
162
163  __ LeaveInternalFrame();
164
165  // Finally restore all registers.
166  __ RestoreRegistersFromMemory(kJSCallerSaved);
167
168  // Now that the break point has been handled, resume normal execution by
169  // jumping to the target address intended by the caller and that was
170  // overwritten by the address of DebugBreakXXX.
171  __ mov(ip, Operand(ExternalReference(Debug_Address::AfterBreakTarget())));
172  __ ldr(ip, MemOperand(ip));
173  __ Jump(ip);
174}
175
176
177void Debug::GenerateLoadICDebugBreak(MacroAssembler* masm) {
178  // Calling convention for IC load (from ic-arm.cc).
179  // ----------- S t a t e -------------
180  //  -- r2    : name
181  //  -- lr    : return address
182  //  -- r0    : receiver
183  //  -- [sp]  : receiver
184  // -----------------------------------
185  // Registers r0 and r2 contain objects that need to be pushed on the
186  // expression stack of the fake JS frame.
187  Generate_DebugBreakCallHelper(masm, r0.bit() | r2.bit());
188}
189
190
191void Debug::GenerateStoreICDebugBreak(MacroAssembler* masm) {
192  // Calling convention for IC store (from ic-arm.cc).
193  // ----------- S t a t e -------------
194  //  -- r0    : value
195  //  -- r1    : receiver
196  //  -- r2    : name
197  //  -- lr    : return address
198  // -----------------------------------
199  // Registers r0, r1, and r2 contain objects that need to be pushed on the
200  // expression stack of the fake JS frame.
201  Generate_DebugBreakCallHelper(masm, r0.bit() | r1.bit() | r2.bit());
202}
203
204
205void Debug::GenerateKeyedLoadICDebugBreak(MacroAssembler* masm) {
206  // ---------- S t a t e --------------
207  //  -- lr     : return address
208  //  -- r0     : key
209  //  -- sp[0]  : key
210  //  -- sp[4]  : receiver
211  Generate_DebugBreakCallHelper(masm, r0.bit());
212}
213
214
215void Debug::GenerateKeyedStoreICDebugBreak(MacroAssembler* masm) {
216  // ---------- S t a t e --------------
217  //  -- r0     : value
218  //  -- r1     : key
219  //  -- r2     : receiver
220  //  -- lr     : return address
221  Generate_DebugBreakCallHelper(masm, r0.bit() | r1.bit() | r2.bit());
222}
223
224
225void Debug::GenerateCallICDebugBreak(MacroAssembler* masm) {
226  // Calling convention for IC call (from ic-arm.cc)
227  // ----------- S t a t e -------------
228  //  -- r0: number of arguments
229  //  -- r1: receiver
230  //  -- lr: return address
231  // -----------------------------------
232  // Register r1 contains an object that needs to be pushed on the expression
233  // stack of the fake JS frame. r0 is the actual number of arguments not
234  // encoded as a smi, therefore it cannot be on the expression stack of the
235  // fake JS frame as it can easily be an invalid pointer (e.g. 1). r0 will be
236  // pushed on the stack of the C frame and restored from there.
237  Generate_DebugBreakCallHelper(masm, r1.bit());
238}
239
240
241void Debug::GenerateConstructCallDebugBreak(MacroAssembler* masm) {
242  // In places other than IC call sites it is expected that r0 is TOS which
243  // is an object - this is not generally the case so this should be used with
244  // care.
245  Generate_DebugBreakCallHelper(masm, r0.bit());
246}
247
248
249void Debug::GenerateReturnDebugBreak(MacroAssembler* masm) {
250  // In places other than IC call sites it is expected that r0 is TOS which
251  // is an object - this is not generally the case so this should be used with
252  // care.
253  Generate_DebugBreakCallHelper(masm, r0.bit());
254}
255
256
257void Debug::GenerateStubNoRegistersDebugBreak(MacroAssembler* masm) {
258  // ----------- S t a t e -------------
259  //  No registers used on entry.
260  // -----------------------------------
261  Generate_DebugBreakCallHelper(masm, 0);
262}
263
264
265void Debug::GenerateSlot(MacroAssembler* masm) {
266  // Generate enough nop's to make space for a call instruction. Avoid emitting
267  // the constant pool in the debug break slot code.
268  Assembler::BlockConstPoolScope block_const_pool(masm);
269  Label check_codesize;
270  __ bind(&check_codesize);
271  __ RecordDebugBreakSlot();
272  for (int i = 0; i < Assembler::kDebugBreakSlotInstructions; i++) {
273    __ nop(2);
274  }
275  ASSERT_EQ(Assembler::kDebugBreakSlotInstructions,
276            masm->InstructionsGeneratedSince(&check_codesize));
277}
278
279
280void Debug::GenerateSlotDebugBreak(MacroAssembler* masm) {
281  // In the places where a debug break slot is inserted no registers can contain
282  // object pointers.
283  Generate_DebugBreakCallHelper(masm, 0);
284}
285
286
287void Debug::GeneratePlainReturnLiveEdit(MacroAssembler* masm) {
288  masm->Abort("LiveEdit frame dropping is not supported on arm");
289}
290
291
292void Debug::GenerateFrameDropperLiveEdit(MacroAssembler* masm) {
293  masm->Abort("LiveEdit frame dropping is not supported on arm");
294}
295
296#undef __
297
298
299void Debug::SetUpFrameDropperFrame(StackFrame* bottom_js_frame,
300                                   Handle<Code> code) {
301  UNREACHABLE();
302}
303const int Debug::kFrameDropperFrameSize = -1;
304
305#endif  // ENABLE_DEBUGGER_SUPPORT
306
307} }  // namespace v8::internal
308
309#endif  // V8_TARGET_ARCH_ARM
310