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