v8  3.14.5(node0.10.28)
V8 is Google's open source JavaScript engine
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
debug-ia32.cc
Go to the documentation of this file.
1 // Copyright 2012 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 
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.
49 void BreakLocationIterator::SetDebugBreakAtReturn() {
52  Isolate* isolate = Isolate::Current();
53  rinfo()->PatchCodeWithCall(isolate->debug()->debug_break_return()->entry(),
55 }
56 
57 
58 // Restore the JS frame exit code.
59 void BreakLocationIterator::ClearDebugBreakAtReturn() {
60  rinfo()->PatchCode(original_rinfo()->pc(),
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.
67 bool Debug::IsDebugBreakAtReturn(RelocInfo* rinfo) {
68  ASSERT(RelocInfo::IsJSReturn(rinfo->rmode()));
69  return rinfo->IsPatchedReturnSequence();
70 }
71 
72 
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 
80 void BreakLocationIterator::SetDebugBreakAtSlot() {
81  ASSERT(IsDebugBreakSlot());
82  Isolate* isolate = Isolate::Current();
83  rinfo()->PatchCodeWithCall(
84  isolate->debug()->debug_break_slot()->entry(),
86 }
87 
88 
89 void BreakLocationIterator::ClearDebugBreakAtSlot() {
90  ASSERT(IsDebugBreakSlot());
91  rinfo()->PatchCode(original_rinfo()->pc(), Assembler::kDebugBreakSlotLength);
92 }
93 
94 // All debug break stubs support padding for LiveEdit.
95 const bool Debug::FramePaddingLayout::kIsSupported = true;
96 
97 
98 #define __ ACCESS_MASM(masm)
99 
100 static void Generate_DebugBreakCallHelper(MacroAssembler* masm,
101  RegList object_regs,
102  RegList non_object_regs,
103  bool convert_call_to_jmp) {
104  // Enter an internal frame.
105  {
106  FrameScope scope(masm, StackFrame::INTERNAL);
107 
108  // Load padding words on stack.
109  for (int i = 0; i < Debug::FramePaddingLayout::kInitialSize; i++) {
110  __ push(Immediate(Smi::FromInt(
111  Debug::FramePaddingLayout::kPaddingValue)));
112  }
113  __ push(Immediate(Smi::FromInt(Debug::FramePaddingLayout::kInitialSize)));
114 
115  // Store the registers containing live values on the expression stack to
116  // make sure that these are correctly updated during GC. Non object values
117  // are stored as a smi causing it to be untouched by GC.
118  ASSERT((object_regs & ~kJSCallerSaved) == 0);
119  ASSERT((non_object_regs & ~kJSCallerSaved) == 0);
120  ASSERT((object_regs & non_object_regs) == 0);
121  for (int i = 0; i < kNumJSCallerSaved; i++) {
122  int r = JSCallerSavedCode(i);
123  Register reg = { r };
124  if ((object_regs & (1 << r)) != 0) {
125  __ push(reg);
126  }
127  if ((non_object_regs & (1 << r)) != 0) {
128  if (FLAG_debug_code) {
129  __ test(reg, Immediate(0xc0000000));
130  __ Assert(zero, "Unable to encode value as smi");
131  }
132  __ SmiTag(reg);
133  __ push(reg);
134  }
135  }
136 
137 #ifdef DEBUG
138  __ RecordComment("// Calling from debug break to runtime - come in - over");
139 #endif
140  __ Set(eax, Immediate(0)); // No arguments.
141  __ mov(ebx, Immediate(ExternalReference::debug_break(masm->isolate())));
142 
143  CEntryStub ceb(1);
144  __ CallStub(&ceb);
145 
146  // Automatically find register that could be used after register restore.
147  // We need one register for padding skip instructions.
148  Register unused_reg = { -1 };
149 
150  // Restore the register values containing object pointers from the
151  // expression stack.
152  for (int i = kNumJSCallerSaved; --i >= 0;) {
153  int r = JSCallerSavedCode(i);
154  Register reg = { r };
155  if (FLAG_debug_code) {
156  __ Set(reg, Immediate(kDebugZapValue));
157  }
158  bool taken = reg.code() == esi.code();
159  if ((object_regs & (1 << r)) != 0) {
160  __ pop(reg);
161  taken = true;
162  }
163  if ((non_object_regs & (1 << r)) != 0) {
164  __ pop(reg);
165  __ SmiUntag(reg);
166  taken = true;
167  }
168  if (!taken) {
169  unused_reg = reg;
170  }
171  }
172 
173  ASSERT(unused_reg.code() != -1);
174 
175  // Read current padding counter and skip corresponding number of words.
176  __ pop(unused_reg);
177  // We divide stored value by 2 (untagging) and multiply it by word's size.
179  __ lea(esp, Operand(esp, unused_reg, times_half_pointer_size, 0));
180 
181  // Get rid of the internal frame.
182  }
183 
184  // If this call did not replace a call but patched other code then there will
185  // be an unwanted return address left on the stack. Here we get rid of that.
186  if (convert_call_to_jmp) {
187  __ add(esp, Immediate(kPointerSize));
188  }
189 
190  // Now that the break point has been handled, resume normal execution by
191  // jumping to the target address intended by the caller and that was
192  // overwritten by the address of DebugBreakXXX.
193  ExternalReference after_break_target =
194  ExternalReference(Debug_Address::AfterBreakTarget(), masm->isolate());
195  __ jmp(Operand::StaticVariable(after_break_target));
196 }
197 
198 
199 void Debug::GenerateLoadICDebugBreak(MacroAssembler* masm) {
200  // Register state for IC load call (from ic-ia32.cc).
201  // ----------- S t a t e -------------
202  // -- ecx : name
203  // -- edx : receiver
204  // -----------------------------------
205  Generate_DebugBreakCallHelper(masm, ecx.bit() | edx.bit(), 0, false);
206 }
207 
208 
209 void Debug::GenerateStoreICDebugBreak(MacroAssembler* masm) {
210  // Register state for IC store call (from ic-ia32.cc).
211  // ----------- S t a t e -------------
212  // -- eax : value
213  // -- ecx : name
214  // -- edx : receiver
215  // -----------------------------------
216  Generate_DebugBreakCallHelper(
217  masm, eax.bit() | ecx.bit() | edx.bit(), 0, false);
218 }
219 
220 
221 void Debug::GenerateKeyedLoadICDebugBreak(MacroAssembler* masm) {
222  // Register state for keyed IC load call (from ic-ia32.cc).
223  // ----------- S t a t e -------------
224  // -- ecx : key
225  // -- edx : receiver
226  // -----------------------------------
227  Generate_DebugBreakCallHelper(masm, ecx.bit() | edx.bit(), 0, false);
228 }
229 
230 
231 void Debug::GenerateKeyedStoreICDebugBreak(MacroAssembler* masm) {
232  // Register state for keyed IC load call (from ic-ia32.cc).
233  // ----------- S t a t e -------------
234  // -- eax : value
235  // -- ecx : key
236  // -- edx : receiver
237  // -----------------------------------
238  Generate_DebugBreakCallHelper(
239  masm, eax.bit() | ecx.bit() | edx.bit(), 0, false);
240 }
241 
242 
243 void Debug::GenerateCallICDebugBreak(MacroAssembler* masm) {
244  // Register state for keyed IC call call (from ic-ia32.cc)
245  // ----------- S t a t e -------------
246  // -- ecx: name
247  // -----------------------------------
248  Generate_DebugBreakCallHelper(masm, ecx.bit(), 0, false);
249 }
250 
251 
252 void Debug::GenerateReturnDebugBreak(MacroAssembler* masm) {
253  // Register state just before return from JS function (from codegen-ia32.cc).
254  // ----------- S t a t e -------------
255  // -- eax: return value
256  // -----------------------------------
257  Generate_DebugBreakCallHelper(masm, eax.bit(), 0, true);
258 }
259 
260 
261 void Debug::GenerateCallFunctionStubDebugBreak(MacroAssembler* masm) {
262  // Register state for CallFunctionStub (from code-stubs-ia32.cc).
263  // ----------- S t a t e -------------
264  // -- edi: function
265  // -----------------------------------
266  Generate_DebugBreakCallHelper(masm, edi.bit(), 0, false);
267 }
268 
269 
270 void Debug::GenerateCallFunctionStubRecordDebugBreak(MacroAssembler* masm) {
271  // Register state for CallFunctionStub (from code-stubs-ia32.cc).
272  // ----------- S t a t e -------------
273  // -- ebx: cache cell for call target
274  // -- edi: function
275  // -----------------------------------
276  Generate_DebugBreakCallHelper(masm, ebx.bit() | edi.bit(), 0, false);
277 }
278 
279 
280 void Debug::GenerateCallConstructStubDebugBreak(MacroAssembler* masm) {
281  // Register state for CallConstructStub (from code-stubs-ia32.cc).
282  // eax is the actual number of arguments not encoded as a smi see comment
283  // above IC call.
284  // ----------- S t a t e -------------
285  // -- eax: number of arguments (not smi)
286  // -- edi: constructor function
287  // -----------------------------------
288  // The number of arguments in eax is not smi encoded.
289  Generate_DebugBreakCallHelper(masm, edi.bit(), eax.bit(), false);
290 }
291 
292 
293 void Debug::GenerateCallConstructStubRecordDebugBreak(MacroAssembler* masm) {
294  // Register state for CallConstructStub (from code-stubs-ia32.cc).
295  // eax is the actual number of arguments not encoded as a smi see comment
296  // above IC call.
297  // ----------- S t a t e -------------
298  // -- eax: number of arguments (not smi)
299  // -- ebx: cache cell for call target
300  // -- edi: constructor function
301  // -----------------------------------
302  // The number of arguments in eax is not smi encoded.
303  Generate_DebugBreakCallHelper(masm, ebx.bit() | edi.bit(), eax.bit(), false);
304 }
305 
306 
307 void Debug::GenerateSlot(MacroAssembler* masm) {
308  // Generate enough nop's to make space for a call instruction.
309  Label check_codesize;
310  __ bind(&check_codesize);
311  __ RecordDebugBreakSlot();
314  masm->SizeOfCodeGeneratedSince(&check_codesize));
315 }
316 
317 
318 void Debug::GenerateSlotDebugBreak(MacroAssembler* masm) {
319  // In the places where a debug break slot is inserted no registers can contain
320  // object pointers.
321  Generate_DebugBreakCallHelper(masm, 0, 0, true);
322 }
323 
324 
325 void Debug::GeneratePlainReturnLiveEdit(MacroAssembler* masm) {
326  masm->ret(0);
327 }
328 
329 
330 void Debug::GenerateFrameDropperLiveEdit(MacroAssembler* masm) {
331  ExternalReference restarter_frame_function_slot =
332  ExternalReference(Debug_Address::RestarterFrameFunctionPointer(),
333  masm->isolate());
334  __ mov(Operand::StaticVariable(restarter_frame_function_slot), Immediate(0));
335 
336  // We do not know our frame height, but set esp based on ebp.
337  __ lea(esp, Operand(ebp, -1 * kPointerSize));
338 
339  __ pop(edi); // Function.
340  __ pop(ebp);
341 
342  // Load context from the function.
344 
345  // Get function code.
349 
350  // Re-run JSFunction, edi is function, esi is context.
351  __ jmp(edx);
352 }
353 
354 const bool Debug::kFrameDropperSupported = true;
355 
356 #undef __
357 
358 #endif // ENABLE_DEBUGGER_SUPPORT
359 
360 } } // namespace v8::internal
361 
362 #endif // V8_TARGET_ARCH_IA32
static const int kCodeOffset
Definition: objects.h:5796
static Smi * FromInt(int value)
Definition: objects-inl.h:981
const Register esp
uint32_t RegList
Definition: frames.h:38
static const int kCallInstructionLength
#define ASSERT(condition)
Definition: checks.h:270
const RegList kJSCallerSaved
Definition: frames-arm.h:47
static const int kContextOffset
Definition: objects.h:6187
const Register edi
int JSCallerSavedCode(int n)
Definition: frames.cc:1403
const Register ebp
STATIC_ASSERT((FixedDoubleArray::kHeaderSize &kDoubleAlignmentMask)==0)
const Register eax
const int kPointerSize
Definition: globals.h:220
static const int kJSReturnSequenceLength
Operand FieldOperand(Register object, int offset)
const Register ecx
#define __
const Register pc
const int kNumJSCallerSaved
Definition: frames-arm.h:53
static const int kDebugBreakSlotLength
const Register ebx
const int kSmiShiftSize
Definition: v8.h:4060
const int kSmiTagSize
Definition: v8.h:4015
static const int kHeaderSize
Definition: objects.h:4549
#define ASSERT_EQ(v1, v2)
Definition: checks.h:271
const Register esi
const uint32_t kDebugZapValue
Definition: v8globals.h:84
const Register edx
static const int kSharedFunctionInfoOffset
Definition: objects.h:6185