blob: fb4429dc3e315808354ca14f1149a4c76cd53bfc [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,
50 RegList pointer_regs,
51 bool convert_call_to_jmp) {
52 // Save the content of all general purpose registers in memory. This copy in
53 // memory is later pushed onto the JS expression stack for the fake JS frame
54 // generated and also to the C frame generated on top of that. In the JS
55 // frame ONLY the registers containing pointers will be pushed on the
56 // expression stack. This causes the GC to update these pointers so that
57 // they will have the correct value when returning from the debugger.
58 __ SaveRegistersToMemory(kJSCallerSaved);
59
60 // Enter an internal frame.
61 __ EnterInternalFrame();
62
63 // Store the registers containing object pointers on the expression stack to
64 // make sure that these are correctly updated during GC.
65 __ PushRegistersFromMemory(pointer_regs);
66
67#ifdef DEBUG
68 __ RecordComment("// Calling from debug break to runtime - come in - over");
69#endif
70 __ xor_(rax, rax); // No arguments (argc == 0).
71 __ movq(rbx, ExternalReference::debug_break());
72
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000073 CEntryStub ceb(1, ExitFrame::MODE_DEBUG);
sgjesse@chromium.org911335c2009-08-19 12:59:44 +000074 __ CallStub(&ceb);
75
76 // Restore the register values containing object pointers from the expression
77 // stack in the reverse order as they where pushed.
78 __ PopRegistersToMemory(pointer_regs);
79
80 // Get rid of the internal frame.
81 __ LeaveInternalFrame();
82
83 // If this call did not replace a call but patched other code then there will
84 // be an unwanted return address left on the stack. Here we get rid of that.
85 if (convert_call_to_jmp) {
86 __ pop(rax);
87 }
88
89 // Finally restore all registers.
90 __ RestoreRegistersFromMemory(kJSCallerSaved);
91
92 // Now that the break point has been handled, resume normal execution by
93 // jumping to the target address intended by the caller and that was
94 // overwritten by the address of DebugBreakXXX.
95 ExternalReference after_break_target =
96 ExternalReference(Debug_Address::AfterBreakTarget());
97 __ movq(kScratchRegister, after_break_target);
98 __ jmp(Operand(kScratchRegister, 0));
99}
100
101
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000102void Debug::GenerateCallICDebugBreak(MacroAssembler* masm) {
ricow@chromium.org65fae842010-08-25 15:26:24 +0000103 // Register state for IC call call (from ic-x64.cc)
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000104 // ----------- S t a t e -------------
ricow@chromium.org65fae842010-08-25 15:26:24 +0000105 // -- rcx: function name
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000106 // -----------------------------------
ricow@chromium.org65fae842010-08-25 15:26:24 +0000107 Generate_DebugBreakCallHelper(masm, rcx.bit(), false);
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000108}
109
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000110
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000111void Debug::GenerateConstructCallDebugBreak(MacroAssembler* masm) {
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000112 // Register state just before return from JS function (from codegen-x64.cc).
113 // rax is the actual number of arguments not encoded as a smi, see comment
114 // above IC call.
115 // ----------- S t a t e -------------
116 // -- rax: number of arguments
117 // -----------------------------------
118 // The number of arguments in rax is not smi encoded.
119 Generate_DebugBreakCallHelper(masm, 0, false);
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000120}
121
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000122
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000123void Debug::GenerateKeyedLoadICDebugBreak(MacroAssembler* masm) {
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000124 // Register state for keyed IC load call (from ic-x64.cc).
125 // ----------- S t a t e -------------
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +0000126 // -- rax : key
127 // -- rdx : receiver
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000128 // -----------------------------------
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +0000129 Generate_DebugBreakCallHelper(masm, rax.bit() | rdx.bit(), false);
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000130}
131
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000132
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000133void Debug::GenerateKeyedStoreICDebugBreak(MacroAssembler* masm) {
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000134 // Register state for keyed IC load call (from ic-x64.cc).
135 // ----------- S t a t e -------------
136 // -- rax : value
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000137 // -- rcx : key
138 // -- rdx : receiver
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000139 // -----------------------------------
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000140 Generate_DebugBreakCallHelper(masm, rax.bit() | rcx.bit() | rdx.bit(), false);
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000141}
142
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000143
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000144void Debug::GenerateLoadICDebugBreak(MacroAssembler* masm) {
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000145 // Register state for IC load call (from ic-x64.cc).
146 // ----------- S t a t e -------------
sgjesse@chromium.org82dbbab2010-06-02 08:57:44 +0000147 // -- rax : receiver
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000148 // -- rcx : name
149 // -----------------------------------
sgjesse@chromium.org82dbbab2010-06-02 08:57:44 +0000150 Generate_DebugBreakCallHelper(masm, rax.bit() | rcx.bit(), false);
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000151}
152
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000153
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000154void Debug::GenerateReturnDebugBreak(MacroAssembler* masm) {
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000155 // Register state just before return from JS function (from codegen-x64.cc).
156 // ----------- S t a t e -------------
157 // -- rax: return value
158 // -----------------------------------
159 Generate_DebugBreakCallHelper(masm, rax.bit(), true);
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000160}
161
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000162
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000163void Debug::GenerateStoreICDebugBreak(MacroAssembler* masm) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000164 // Register state for IC store call (from ic-x64.cc).
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000165 // ----------- S t a t e -------------
166 // -- rax : value
167 // -- rcx : name
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000168 // -- rdx : receiver
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000169 // -----------------------------------
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000170 Generate_DebugBreakCallHelper(masm, rax.bit() | rcx.bit() | rdx.bit(), false);
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000171}
172
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000173
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000174void Debug::GenerateStubNoRegistersDebugBreak(MacroAssembler* masm) {
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000175 // Register state for stub CallFunction (from CallFunctionStub in ic-x64.cc).
176 // ----------- S t a t e -------------
177 // No registers used on entry.
178 // -----------------------------------
179 Generate_DebugBreakCallHelper(masm, 0, false);
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000180}
181
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000182
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000183void Debug::GenerateSlot(MacroAssembler* masm) {
184 // Generate enough nop's to make space for a call instruction.
185 Label check_codesize;
186 __ bind(&check_codesize);
187 __ RecordDebugBreakSlot();
188 for (int i = 0; i < Assembler::kDebugBreakSlotLength; i++) {
189 __ nop();
190 }
191 ASSERT_EQ(Assembler::kDebugBreakSlotLength,
192 masm->SizeOfCodeGeneratedSince(&check_codesize));
193}
194
195
196void Debug::GenerateSlotDebugBreak(MacroAssembler* masm) {
197 // In the places where a debug break slot is inserted no registers can contain
198 // object pointers.
199 Generate_DebugBreakCallHelper(masm, 0, true);
200}
201
202
ager@chromium.org357bf652010-04-12 11:30:10 +0000203void Debug::GeneratePlainReturnLiveEdit(MacroAssembler* masm) {
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000204 masm->ret(0);
ager@chromium.org357bf652010-04-12 11:30:10 +0000205}
206
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000207
ager@chromium.org357bf652010-04-12 11:30:10 +0000208void Debug::GenerateFrameDropperLiveEdit(MacroAssembler* masm) {
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000209 ExternalReference restarter_frame_function_slot =
210 ExternalReference(Debug_Address::RestarterFrameFunctionPointer());
211 __ movq(rax, restarter_frame_function_slot);
212 __ movq(Operand(rax, 0), Immediate(0));
213
214 // We do not know our frame height, but set rsp based on rbp.
215 __ lea(rsp, Operand(rbp, -1 * kPointerSize));
216
217 __ pop(rdi); // Function.
218 __ pop(rbp);
219
220 // Load context from the function.
221 __ movq(rsi, FieldOperand(rdi, JSFunction::kContextOffset));
222
223 // Get function code.
224 __ movq(rdx, FieldOperand(rdi, JSFunction::kSharedFunctionInfoOffset));
225 __ movq(rdx, FieldOperand(rdx, SharedFunctionInfo::kCodeOffset));
226 __ lea(rdx, FieldOperand(rdx, Code::kHeaderSize));
227
228 // Re-run JSFunction, rdi is function, rsi is context.
229 __ jmp(rdx);
ager@chromium.org357bf652010-04-12 11:30:10 +0000230}
231
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000232const bool Debug::kFrameDropperSupported = true;
233
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000234#undef __
235
236
ager@chromium.org357bf652010-04-12 11:30:10 +0000237
238
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000239void BreakLocationIterator::ClearDebugBreakAtReturn() {
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000240 rinfo()->PatchCode(original_rinfo()->pc(),
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000241 Assembler::kJSReturnSequenceLength);
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000242}
243
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000244
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000245bool BreakLocationIterator::IsDebugBreakAtReturn() {
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000246 return Debug::IsDebugBreakAtReturn(rinfo());
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000247}
248
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000249
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000250void BreakLocationIterator::SetDebugBreakAtReturn() {
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000251 ASSERT(Assembler::kJSReturnSequenceLength >=
252 Assembler::kCallInstructionLength);
ager@chromium.orga1645e22009-09-09 19:27:10 +0000253 rinfo()->PatchCodeWithCall(Debug::debug_break_return()->entry(),
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000254 Assembler::kJSReturnSequenceLength - Assembler::kCallInstructionLength);
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000255}
256
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000257
258bool BreakLocationIterator::IsDebugBreakAtSlot() {
259 ASSERT(IsDebugBreakSlot());
260 // Check whether the debug break slot instructions have been patched.
261 return !Assembler::IsNop(rinfo()->pc());
262}
263
264
265void BreakLocationIterator::SetDebugBreakAtSlot() {
266 ASSERT(IsDebugBreakSlot());
267 rinfo()->PatchCodeWithCall(
268 Debug::debug_break_slot()->entry(),
269 Assembler::kDebugBreakSlotLength - Assembler::kCallInstructionLength);
270}
271
272
273void BreakLocationIterator::ClearDebugBreakAtSlot() {
274 ASSERT(IsDebugBreakSlot());
275 rinfo()->PatchCode(original_rinfo()->pc(), Assembler::kDebugBreakSlotLength);
276}
277
278
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000279#endif // ENABLE_DEBUGGER_SUPPORT
280
281} } // namespace v8::internal
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000282
283#endif // V8_TARGET_ARCH_X64