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