blob: 2c1056f5790ad1afd11d593198f9a78e00325c8d [file] [log] [blame]
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001// Copyright 2010 the V8 project authors. All rights reserved.
ager@chromium.org5ec48922009-05-05 07:25:34 +00002// 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
kasperl@chromium.org71affb52009-05-26 05:44:31 +000028
29#include "v8.h"
30
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +000031#if defined(V8_TARGET_ARCH_X64)
32
kasperl@chromium.org71affb52009-05-26 05:44:31 +000033#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) {
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +000043 ASSERT(RelocInfo::IsJSReturn(rinfo->rmode()));
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +000044 return rinfo->IsPatchedReturnSequence();
kasperl@chromium.org71affb52009-05-26 05:44:31 +000045}
46
sgjesse@chromium.org911335c2009-08-19 12:59:44 +000047#define __ ACCESS_MASM(masm)
48
49static void Generate_DebugBreakCallHelper(MacroAssembler* masm,
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +000050 RegList object_regs,
51 RegList non_object_regs,
sgjesse@chromium.org911335c2009-08-19 12:59:44 +000052 bool convert_call_to_jmp) {
sgjesse@chromium.org911335c2009-08-19 12:59:44 +000053 // Enter an internal frame.
54 __ EnterInternalFrame();
55
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +000056 // 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 }
sgjesse@chromium.org911335c2009-08-19 12:59:44 +000079
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
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +000086 CEntryStub ceb(1);
sgjesse@chromium.org911335c2009-08-19 12:59:44 +000087 __ CallStub(&ceb);
88
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +000089 // 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 }
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000109
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) {
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000116 __ addq(rsp, Immediate(kPointerSize));
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000117 }
118
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000119 // 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
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000129void Debug::GenerateCallICDebugBreak(MacroAssembler* masm) {
ricow@chromium.org65fae842010-08-25 15:26:24 +0000130 // Register state for IC call call (from ic-x64.cc)
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000131 // ----------- S t a t e -------------
ricow@chromium.org65fae842010-08-25 15:26:24 +0000132 // -- rcx: function name
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000133 // -----------------------------------
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000134 Generate_DebugBreakCallHelper(masm, rcx.bit(), 0, false);
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000135}
136
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000137
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000138void Debug::GenerateConstructCallDebugBreak(MacroAssembler* masm) {
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000139 // 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.
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000146 Generate_DebugBreakCallHelper(masm, rdi.bit(), rax.bit(), false);
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000147}
148
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000149
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000150void Debug::GenerateKeyedLoadICDebugBreak(MacroAssembler* masm) {
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000151 // Register state for keyed IC load call (from ic-x64.cc).
152 // ----------- S t a t e -------------
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +0000153 // -- rax : key
154 // -- rdx : receiver
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000155 // -----------------------------------
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000156 Generate_DebugBreakCallHelper(masm, rax.bit() | rdx.bit(), 0, false);
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000157}
158
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000159
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000160void Debug::GenerateKeyedStoreICDebugBreak(MacroAssembler* masm) {
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000161 // Register state for keyed IC load call (from ic-x64.cc).
162 // ----------- S t a t e -------------
163 // -- rax : value
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000164 // -- rcx : key
165 // -- rdx : receiver
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000166 // -----------------------------------
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000167 Generate_DebugBreakCallHelper(
168 masm, rax.bit() | rcx.bit() | rdx.bit(), 0, false);
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000169}
170
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000171
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000172void Debug::GenerateLoadICDebugBreak(MacroAssembler* masm) {
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000173 // Register state for IC load call (from ic-x64.cc).
174 // ----------- S t a t e -------------
sgjesse@chromium.org82dbbab2010-06-02 08:57:44 +0000175 // -- rax : receiver
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000176 // -- rcx : name
177 // -----------------------------------
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000178 Generate_DebugBreakCallHelper(masm, rax.bit() | rcx.bit(), 0, false);
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000179}
180
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000181
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000182void Debug::GenerateReturnDebugBreak(MacroAssembler* masm) {
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000183 // Register state just before return from JS function (from codegen-x64.cc).
184 // ----------- S t a t e -------------
185 // -- rax: return value
186 // -----------------------------------
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000187 Generate_DebugBreakCallHelper(masm, rax.bit(), 0, true);
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000188}
189
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000190
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000191void Debug::GenerateStoreICDebugBreak(MacroAssembler* masm) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000192 // Register state for IC store call (from ic-x64.cc).
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000193 // ----------- S t a t e -------------
194 // -- rax : value
195 // -- rcx : name
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000196 // -- rdx : receiver
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000197 // -----------------------------------
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000198 Generate_DebugBreakCallHelper(
199 masm, rax.bit() | rcx.bit() | rdx.bit(), 0, false);
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000200}
201
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000202
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000203void Debug::GenerateStubNoRegistersDebugBreak(MacroAssembler* masm) {
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000204 // Register state for stub CallFunction (from CallFunctionStub in ic-x64.cc).
205 // ----------- S t a t e -------------
206 // No registers used on entry.
207 // -----------------------------------
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000208 Generate_DebugBreakCallHelper(masm, 0, 0, false);
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000209}
210
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000211
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000212void 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.
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000228 Generate_DebugBreakCallHelper(masm, 0, 0, true);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000229}
230
231
ager@chromium.org357bf652010-04-12 11:30:10 +0000232void Debug::GeneratePlainReturnLiveEdit(MacroAssembler* masm) {
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000233 masm->ret(0);
ager@chromium.org357bf652010-04-12 11:30:10 +0000234}
235
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000236
ager@chromium.org357bf652010-04-12 11:30:10 +0000237void Debug::GenerateFrameDropperLiveEdit(MacroAssembler* masm) {
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000238 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);
ager@chromium.org357bf652010-04-12 11:30:10 +0000259}
260
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000261const bool Debug::kFrameDropperSupported = true;
262
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000263#undef __
264
265
ager@chromium.org357bf652010-04-12 11:30:10 +0000266
267
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000268void BreakLocationIterator::ClearDebugBreakAtReturn() {
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000269 rinfo()->PatchCode(original_rinfo()->pc(),
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000270 Assembler::kJSReturnSequenceLength);
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000271}
272
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000273
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000274bool BreakLocationIterator::IsDebugBreakAtReturn() {
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000275 return Debug::IsDebugBreakAtReturn(rinfo());
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000276}
277
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000278
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000279void BreakLocationIterator::SetDebugBreakAtReturn() {
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000280 ASSERT(Assembler::kJSReturnSequenceLength >=
281 Assembler::kCallInstructionLength);
ager@chromium.orga1645e22009-09-09 19:27:10 +0000282 rinfo()->PatchCodeWithCall(Debug::debug_break_return()->entry(),
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000283 Assembler::kJSReturnSequenceLength - Assembler::kCallInstructionLength);
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000284}
285
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000286
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
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000308#endif // ENABLE_DEBUGGER_SUPPORT
309
310} } // namespace v8::internal
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000311
312#endif // V8_TARGET_ARCH_X64