• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2011 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_X64)
31 
32 #include "assembler.h"
33 #include "codegen.h"
34 #include "debug.h"
35 
36 
37 namespace v8 {
38 namespace internal {
39 
40 #ifdef ENABLE_DEBUGGER_SUPPORT
41 
IsDebugBreakAtReturn()42 bool BreakLocationIterator::IsDebugBreakAtReturn()  {
43   return Debug::IsDebugBreakAtReturn(rinfo());
44 }
45 
46 
47 // Patch the JS frame exit code with a debug break call. See
48 // CodeGenerator::VisitReturnStatement and VirtualFrame::Exit in codegen-x64.cc
49 // for the precise return instructions sequence.
SetDebugBreakAtReturn()50 void BreakLocationIterator::SetDebugBreakAtReturn()  {
51   ASSERT(Assembler::kJSReturnSequenceLength >=
52          Assembler::kCallInstructionLength);
53   rinfo()->PatchCodeWithCall(
54       Isolate::Current()->debug()->debug_break_return()->entry(),
55       Assembler::kJSReturnSequenceLength - Assembler::kCallInstructionLength);
56 }
57 
58 
59 // Restore the JS frame exit code.
ClearDebugBreakAtReturn()60 void BreakLocationIterator::ClearDebugBreakAtReturn() {
61   rinfo()->PatchCode(original_rinfo()->pc(),
62                      Assembler::kJSReturnSequenceLength);
63 }
64 
65 
66 // A debug break in the frame exit code is identified by the JS frame exit code
67 // having been patched with a call instruction.
IsDebugBreakAtReturn(v8::internal::RelocInfo * rinfo)68 bool Debug::IsDebugBreakAtReturn(v8::internal::RelocInfo* rinfo) {
69   ASSERT(RelocInfo::IsJSReturn(rinfo->rmode()));
70   return rinfo->IsPatchedReturnSequence();
71 }
72 
73 
IsDebugBreakAtSlot()74 bool BreakLocationIterator::IsDebugBreakAtSlot() {
75   ASSERT(IsDebugBreakSlot());
76   // Check whether the debug break slot instructions have been patched.
77   return !Assembler::IsNop(rinfo()->pc());
78 }
79 
80 
SetDebugBreakAtSlot()81 void BreakLocationIterator::SetDebugBreakAtSlot() {
82   ASSERT(IsDebugBreakSlot());
83   rinfo()->PatchCodeWithCall(
84       Isolate::Current()->debug()->debug_break_slot()->entry(),
85       Assembler::kDebugBreakSlotLength - Assembler::kCallInstructionLength);
86 }
87 
88 
ClearDebugBreakAtSlot()89 void BreakLocationIterator::ClearDebugBreakAtSlot() {
90   ASSERT(IsDebugBreakSlot());
91   rinfo()->PatchCode(original_rinfo()->pc(), Assembler::kDebugBreakSlotLength);
92 }
93 
94 
95 #define __ ACCESS_MASM(masm)
96 
97 
Generate_DebugBreakCallHelper(MacroAssembler * masm,RegList object_regs,RegList non_object_regs,bool convert_call_to_jmp)98 static void Generate_DebugBreakCallHelper(MacroAssembler* masm,
99                                           RegList object_regs,
100                                           RegList non_object_regs,
101                                           bool convert_call_to_jmp) {
102   // Enter an internal frame.
103   {
104     FrameScope scope(masm, StackFrame::INTERNAL);
105 
106     // Store the registers containing live values on the expression stack to
107     // make sure that these are correctly updated during GC. Non object values
108     // are stored as as two smis causing it to be untouched by GC.
109     ASSERT((object_regs & ~kJSCallerSaved) == 0);
110     ASSERT((non_object_regs & ~kJSCallerSaved) == 0);
111     ASSERT((object_regs & non_object_regs) == 0);
112     for (int i = 0; i < kNumJSCallerSaved; i++) {
113       int r = JSCallerSavedCode(i);
114       Register reg = { r };
115       ASSERT(!reg.is(kScratchRegister));
116       if ((object_regs & (1 << r)) != 0) {
117         __ push(reg);
118       }
119       // Store the 64-bit value as two smis.
120       if ((non_object_regs & (1 << r)) != 0) {
121         __ movq(kScratchRegister, reg);
122         __ Integer32ToSmi(reg, reg);
123         __ push(reg);
124         __ sar(kScratchRegister, Immediate(32));
125         __ Integer32ToSmi(kScratchRegister, kScratchRegister);
126         __ push(kScratchRegister);
127       }
128     }
129 
130 #ifdef DEBUG
131     __ RecordComment("// Calling from debug break to runtime - come in - over");
132 #endif
133     __ Set(rax, 0);  // No arguments (argc == 0).
134     __ movq(rbx, ExternalReference::debug_break(masm->isolate()));
135 
136     CEntryStub ceb(1);
137     __ CallStub(&ceb);
138 
139     // Restore the register values from the expression stack.
140     for (int i = kNumJSCallerSaved - 1; i >= 0; i--) {
141       int r = JSCallerSavedCode(i);
142       Register reg = { r };
143       if (FLAG_debug_code) {
144         __ Set(reg, kDebugZapValue);
145       }
146       if ((object_regs & (1 << r)) != 0) {
147         __ pop(reg);
148       }
149       // Reconstruct the 64-bit value from two smis.
150       if ((non_object_regs & (1 << r)) != 0) {
151         __ pop(kScratchRegister);
152         __ SmiToInteger32(kScratchRegister, kScratchRegister);
153         __ shl(kScratchRegister, Immediate(32));
154         __ pop(reg);
155         __ SmiToInteger32(reg, reg);
156         __ or_(reg, kScratchRegister);
157       }
158     }
159 
160     // Get rid of the internal frame.
161   }
162 
163   // If this call did not replace a call but patched other code then there will
164   // be an unwanted return address left on the stack. Here we get rid of that.
165   if (convert_call_to_jmp) {
166     __ addq(rsp, Immediate(kPointerSize));
167   }
168 
169   // Now that the break point has been handled, resume normal execution by
170   // jumping to the target address intended by the caller and that was
171   // overwritten by the address of DebugBreakXXX.
172   ExternalReference after_break_target =
173       ExternalReference(Debug_Address::AfterBreakTarget(), masm->isolate());
174   __ movq(kScratchRegister, after_break_target);
175   __ jmp(Operand(kScratchRegister, 0));
176 }
177 
178 
GenerateLoadICDebugBreak(MacroAssembler * masm)179 void Debug::GenerateLoadICDebugBreak(MacroAssembler* masm) {
180   // Register state for IC load call (from ic-x64.cc).
181   // ----------- S t a t e -------------
182   //  -- rax    : receiver
183   //  -- rcx    : name
184   // -----------------------------------
185   Generate_DebugBreakCallHelper(masm, rax.bit() | rcx.bit(), 0, false);
186 }
187 
188 
GenerateStoreICDebugBreak(MacroAssembler * masm)189 void Debug::GenerateStoreICDebugBreak(MacroAssembler* masm) {
190   // Register state for IC store call (from ic-x64.cc).
191   // ----------- S t a t e -------------
192   //  -- rax    : value
193   //  -- rcx    : name
194   //  -- rdx    : receiver
195   // -----------------------------------
196   Generate_DebugBreakCallHelper(
197       masm, rax.bit() | rcx.bit() | rdx.bit(), 0, false);
198 }
199 
200 
GenerateKeyedLoadICDebugBreak(MacroAssembler * masm)201 void Debug::GenerateKeyedLoadICDebugBreak(MacroAssembler* masm) {
202   // Register state for keyed IC load call (from ic-x64.cc).
203   // ----------- S t a t e -------------
204   //  -- rax     : key
205   //  -- rdx     : receiver
206   // -----------------------------------
207   Generate_DebugBreakCallHelper(masm, rax.bit() | rdx.bit(), 0, false);
208 }
209 
210 
GenerateKeyedStoreICDebugBreak(MacroAssembler * masm)211 void Debug::GenerateKeyedStoreICDebugBreak(MacroAssembler* masm) {
212   // Register state for keyed IC load call (from ic-x64.cc).
213   // ----------- S t a t e -------------
214   //  -- rax    : value
215   //  -- rcx    : key
216   //  -- rdx    : receiver
217   // -----------------------------------
218   Generate_DebugBreakCallHelper(
219       masm, rax.bit() | rcx.bit() | rdx.bit(), 0, false);
220 }
221 
222 
GenerateCallICDebugBreak(MacroAssembler * masm)223 void Debug::GenerateCallICDebugBreak(MacroAssembler* masm) {
224   // Register state for IC call call (from ic-x64.cc)
225   // ----------- S t a t e -------------
226   //  -- rcx: function name
227   // -----------------------------------
228   Generate_DebugBreakCallHelper(masm, rcx.bit(), 0, false);
229 }
230 
231 
GenerateReturnDebugBreak(MacroAssembler * masm)232 void Debug::GenerateReturnDebugBreak(MacroAssembler* masm) {
233   // Register state just before return from JS function (from codegen-x64.cc).
234   // ----------- S t a t e -------------
235   //  -- rax: return value
236   // -----------------------------------
237   Generate_DebugBreakCallHelper(masm, rax.bit(), 0, true);
238 }
239 
240 
GenerateCallFunctionStubDebugBreak(MacroAssembler * masm)241 void Debug::GenerateCallFunctionStubDebugBreak(MacroAssembler* masm) {
242   // Register state for CallFunctionStub (from code-stubs-x64.cc).
243   // ----------- S t a t e -------------
244   //  -- rdi : function
245   // -----------------------------------
246   Generate_DebugBreakCallHelper(masm, rdi.bit(), 0, false);
247 }
248 
249 
GenerateCallFunctionStubRecordDebugBreak(MacroAssembler * masm)250 void Debug::GenerateCallFunctionStubRecordDebugBreak(MacroAssembler* masm) {
251   // Register state for CallFunctionStub (from code-stubs-x64.cc).
252   // ----------- S t a t e -------------
253   //  -- rdi : function
254   //  -- rbx: cache cell for call target
255   // -----------------------------------
256   Generate_DebugBreakCallHelper(masm, rbx.bit() | rdi.bit(), 0, false);
257 }
258 
259 
GenerateCallConstructStubDebugBreak(MacroAssembler * masm)260 void Debug::GenerateCallConstructStubDebugBreak(MacroAssembler* masm) {
261   // Register state for CallConstructStub (from code-stubs-x64.cc).
262   // rax is the actual number of arguments not encoded as a smi, see comment
263   // above IC call.
264   // ----------- S t a t e -------------
265   //  -- rax: number of arguments
266   // -----------------------------------
267   // The number of arguments in rax is not smi encoded.
268   Generate_DebugBreakCallHelper(masm, rdi.bit(), rax.bit(), false);
269 }
270 
271 
GenerateCallConstructStubRecordDebugBreak(MacroAssembler * masm)272 void Debug::GenerateCallConstructStubRecordDebugBreak(MacroAssembler* masm) {
273   // Register state for CallConstructStub (from code-stubs-x64.cc).
274   // rax is the actual number of arguments not encoded as a smi, see comment
275   // above IC call.
276   // ----------- S t a t e -------------
277   //  -- rax: number of arguments
278   //  -- rbx: cache cell for call target
279   // -----------------------------------
280   // The number of arguments in rax is not smi encoded.
281   Generate_DebugBreakCallHelper(masm, rbx.bit() | rdi.bit(), rax.bit(), false);
282 }
283 
284 
GenerateSlot(MacroAssembler * masm)285 void Debug::GenerateSlot(MacroAssembler* masm) {
286   // Generate enough nop's to make space for a call instruction.
287   Label check_codesize;
288   __ bind(&check_codesize);
289   __ RecordDebugBreakSlot();
290   __ Nop(Assembler::kDebugBreakSlotLength);
291   ASSERT_EQ(Assembler::kDebugBreakSlotLength,
292             masm->SizeOfCodeGeneratedSince(&check_codesize));
293 }
294 
295 
GenerateSlotDebugBreak(MacroAssembler * masm)296 void Debug::GenerateSlotDebugBreak(MacroAssembler* masm) {
297   // In the places where a debug break slot is inserted no registers can contain
298   // object pointers.
299   Generate_DebugBreakCallHelper(masm, 0, 0, true);
300 }
301 
302 
GeneratePlainReturnLiveEdit(MacroAssembler * masm)303 void Debug::GeneratePlainReturnLiveEdit(MacroAssembler* masm) {
304   masm->ret(0);
305 }
306 
307 
GenerateFrameDropperLiveEdit(MacroAssembler * masm)308 void Debug::GenerateFrameDropperLiveEdit(MacroAssembler* masm) {
309   ExternalReference restarter_frame_function_slot =
310       ExternalReference(Debug_Address::RestarterFrameFunctionPointer(),
311                         masm->isolate());
312   __ movq(rax, restarter_frame_function_slot);
313   __ movq(Operand(rax, 0), Immediate(0));
314 
315   // We do not know our frame height, but set rsp based on rbp.
316   __ lea(rsp, Operand(rbp, -1 * kPointerSize));
317 
318   __ pop(rdi);  // Function.
319   __ pop(rbp);
320 
321   // Load context from the function.
322   __ movq(rsi, FieldOperand(rdi, JSFunction::kContextOffset));
323 
324   // Get function code.
325   __ movq(rdx, FieldOperand(rdi, JSFunction::kSharedFunctionInfoOffset));
326   __ movq(rdx, FieldOperand(rdx, SharedFunctionInfo::kCodeOffset));
327   __ lea(rdx, FieldOperand(rdx, Code::kHeaderSize));
328 
329   // Re-run JSFunction, rdi is function, rsi is context.
330   __ jmp(rdx);
331 }
332 
333 const bool Debug::kFrameDropperSupported = true;
334 
335 #undef __
336 
337 #endif  // ENABLE_DEBUGGER_SUPPORT
338 
339 } }  // namespace v8::internal
340 
341 #endif  // V8_TARGET_ARCH_X64
342