1 // Copyright 2012 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #if V8_TARGET_ARCH_X64
6 
7 #include "src/debug/debug.h"
8 
9 #include "src/assembler.h"
10 #include "src/codegen.h"
11 #include "src/debug/liveedit.h"
12 
13 namespace v8 {
14 namespace internal {
15 
16 #define __ ACCESS_MASM(masm)
17 
18 
EmitDebugBreakSlot(MacroAssembler * masm)19 void EmitDebugBreakSlot(MacroAssembler* masm) {
20   Label check_codesize;
21   __ bind(&check_codesize);
22   __ Nop(Assembler::kDebugBreakSlotLength);
23   DCHECK_EQ(Assembler::kDebugBreakSlotLength,
24             masm->SizeOfCodeGeneratedSince(&check_codesize));
25 }
26 
27 
GenerateSlot(MacroAssembler * masm,RelocInfo::Mode mode)28 void DebugCodegen::GenerateSlot(MacroAssembler* masm, RelocInfo::Mode mode) {
29   // Generate enough nop's to make space for a call instruction.
30   masm->RecordDebugBreakSlot(mode);
31   EmitDebugBreakSlot(masm);
32 }
33 
34 
ClearDebugBreakSlot(Isolate * isolate,Address pc)35 void DebugCodegen::ClearDebugBreakSlot(Isolate* isolate, Address pc) {
36   CodePatcher patcher(isolate, pc, Assembler::kDebugBreakSlotLength);
37   EmitDebugBreakSlot(patcher.masm());
38 }
39 
40 
PatchDebugBreakSlot(Isolate * isolate,Address pc,Handle<Code> code)41 void DebugCodegen::PatchDebugBreakSlot(Isolate* isolate, Address pc,
42                                        Handle<Code> code) {
43   DCHECK(code->is_debug_stub());
44   static const int kSize = Assembler::kDebugBreakSlotLength;
45   CodePatcher patcher(isolate, pc, kSize);
46   Label check_codesize;
47   patcher.masm()->bind(&check_codesize);
48   patcher.masm()->movp(kScratchRegister, reinterpret_cast<void*>(code->entry()),
49                        Assembler::RelocInfoNone());
50   patcher.masm()->call(kScratchRegister);
51   // Check that the size of the code generated is as expected.
52   DCHECK_EQ(kSize, patcher.masm()->SizeOfCodeGeneratedSince(&check_codesize));
53 }
54 
DebugBreakSlotIsPatched(Address pc)55 bool DebugCodegen::DebugBreakSlotIsPatched(Address pc) {
56   return !Assembler::IsNop(pc);
57 }
58 
GenerateDebugBreakStub(MacroAssembler * masm,DebugBreakCallHelperMode mode)59 void DebugCodegen::GenerateDebugBreakStub(MacroAssembler* masm,
60                                           DebugBreakCallHelperMode mode) {
61   __ RecordComment("Debug break");
62 
63   // Enter an internal frame.
64   {
65     FrameScope scope(masm, StackFrame::INTERNAL);
66 
67     // Load padding words on stack.
68     for (int i = 0; i < LiveEdit::kFramePaddingInitialSize; i++) {
69       __ Push(Smi::FromInt(LiveEdit::kFramePaddingValue));
70     }
71     __ Push(Smi::FromInt(LiveEdit::kFramePaddingInitialSize));
72 
73     // Push arguments for DebugBreak call.
74     if (mode == SAVE_RESULT_REGISTER) {
75       // Break on return.
76       __ Push(rax);
77     } else {
78       // Non-return breaks.
79       __ Push(masm->isolate()->factory()->the_hole_value());
80     }
81     __ Set(rax, 1);
82     __ Move(rbx, ExternalReference(Runtime::FunctionForId(Runtime::kDebugBreak),
83                                    masm->isolate()));
84 
85     CEntryStub ceb(masm->isolate(), 1);
86     __ CallStub(&ceb);
87 
88     if (FLAG_debug_code) {
89       for (int i = 0; i < kNumJSCallerSaved; ++i) {
90         Register reg = {JSCallerSavedCode(i)};
91         // Do not clobber rax if mode is SAVE_RESULT_REGISTER. It will
92         // contain return value of the function.
93         if (!(reg.is(rax) && (mode == SAVE_RESULT_REGISTER))) {
94           __ Set(reg, kDebugZapValue);
95         }
96       }
97     }
98 
99     // Read current padding counter and skip corresponding number of words.
100     __ Pop(kScratchRegister);
101     __ SmiToInteger32(kScratchRegister, kScratchRegister);
102     __ leap(rsp, Operand(rsp, kScratchRegister, times_pointer_size, 0));
103 
104     // Get rid of the internal frame.
105   }
106 
107   // This call did not replace a call , so there will be an unwanted
108   // return address left on the stack. Here we get rid of that.
109   __ addp(rsp, Immediate(kPCOnStackSize));
110 
111   // Now that the break point has been handled, resume normal execution by
112   // jumping to the target address intended by the caller and that was
113   // overwritten by the address of DebugBreakXXX.
114   ExternalReference after_break_target =
115       ExternalReference::debug_after_break_target_address(masm->isolate());
116   __ Move(kScratchRegister, after_break_target);
117   __ Jump(Operand(kScratchRegister, 0));
118 }
119 
120 
GenerateFrameDropperLiveEdit(MacroAssembler * masm)121 void DebugCodegen::GenerateFrameDropperLiveEdit(MacroAssembler* masm) {
122   // We do not know our frame height, but set rsp based on rbp.
123   __ leap(rsp, Operand(rbp, FrameDropperFrameConstants::kFunctionOffset));
124   __ Pop(rdi);  // Function.
125   __ addp(rsp,
126           Immediate(-FrameDropperFrameConstants::kCodeOffset));  // INTERNAL
127                                                                  // frame marker
128                                                                  // and code
129   __ popq(rbp);
130 
131   ParameterCount dummy(0);
132   __ FloodFunctionIfStepping(rdi, no_reg, dummy, dummy);
133 
134   // Load context from the function.
135   __ movp(rsi, FieldOperand(rdi, JSFunction::kContextOffset));
136 
137   // Clear new.target as a safety measure.
138   __ LoadRoot(rdx, Heap::kUndefinedValueRootIndex);
139 
140   // Get function code.
141   __ movp(rbx, FieldOperand(rdi, JSFunction::kSharedFunctionInfoOffset));
142   __ movp(rbx, FieldOperand(rbx, SharedFunctionInfo::kCodeOffset));
143   __ leap(rbx, FieldOperand(rbx, Code::kHeaderSize));
144 
145   // Re-run JSFunction, rdi is function, rsi is context.
146   __ jmp(rbx);
147 }
148 
149 const bool LiveEdit::kFrameDropperSupported = true;
150 
151 #undef __
152 
153 }  // namespace internal
154 }  // namespace v8
155 
156 #endif  // V8_TARGET_ARCH_X64
157