blob: d5b7e7768c2b5eed349b55fc4b4651eac0bd95af [file] [log] [blame]
Leon Clarke4515c472010-02-03 11:58:03 +00001// Copyright 2010 the V8 project authors. All rights reserved.
Steve Blocka7e24c12009-10-30 11:49:00 +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
28
29#include "v8.h"
30
Leon Clarkef7060e22010-06-03 12:02:55 +010031#if defined(V8_TARGET_ARCH_X64)
32
Steve Blocka7e24c12009-10-30 11:49:00 +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) {
43 ASSERT(RelocInfo::IsJSReturn(rinfo->rmode()));
Steve Block3ce2e202009-11-05 08:53:23 +000044 return rinfo->IsPatchedReturnSequence();
Steve Blocka7e24c12009-10-30 11:49:00 +000045}
46
47#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
Leon Clarke4515c472010-02-03 11:58:03 +000073 CEntryStub ceb(1, ExitFrame::MODE_DEBUG);
Steve Blocka7e24c12009-10-30 11:49:00 +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
102void Debug::GenerateCallICDebugBreak(MacroAssembler* masm) {
103 // Register state for keyed IC call call (from ic-x64.cc)
104 // ----------- S t a t e -------------
105 // -- rax: number of arguments
106 // -----------------------------------
107 // The number of arguments in rax is not smi encoded.
108 Generate_DebugBreakCallHelper(masm, 0, false);
109}
110
111
112void Debug::GenerateConstructCallDebugBreak(MacroAssembler* masm) {
113 // Register state just before return from JS function (from codegen-x64.cc).
114 // rax is the actual number of arguments not encoded as a smi, see comment
115 // above IC call.
116 // ----------- S t a t e -------------
117 // -- rax: number of arguments
118 // -----------------------------------
119 // The number of arguments in rax is not smi encoded.
120 Generate_DebugBreakCallHelper(masm, 0, false);
121}
122
123
124void Debug::GenerateKeyedLoadICDebugBreak(MacroAssembler* masm) {
125 // Register state for keyed IC load call (from ic-x64.cc).
126 // ----------- S t a t e -------------
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100127 // -- rax : key
128 // -- rdx : receiver
Steve Blocka7e24c12009-10-30 11:49:00 +0000129 // -----------------------------------
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100130 Generate_DebugBreakCallHelper(masm, rax.bit() | rdx.bit(), false);
Steve Blocka7e24c12009-10-30 11:49:00 +0000131}
132
133
134void Debug::GenerateKeyedStoreICDebugBreak(MacroAssembler* masm) {
135 // Register state for keyed IC load call (from ic-x64.cc).
136 // ----------- S t a t e -------------
137 // -- rax : value
Leon Clarkef7060e22010-06-03 12:02:55 +0100138 // -- rcx : key
139 // -- rdx : receiver
Steve Blocka7e24c12009-10-30 11:49:00 +0000140 // -----------------------------------
Leon Clarkef7060e22010-06-03 12:02:55 +0100141 Generate_DebugBreakCallHelper(masm, rax.bit() | rcx.bit() | rdx.bit(), false);
Steve Blocka7e24c12009-10-30 11:49:00 +0000142}
143
144
145void Debug::GenerateLoadICDebugBreak(MacroAssembler* masm) {
146 // Register state for IC load call (from ic-x64.cc).
147 // ----------- S t a t e -------------
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100148 // -- rax : receiver
Steve Blocka7e24c12009-10-30 11:49:00 +0000149 // -- rcx : name
150 // -----------------------------------
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100151 Generate_DebugBreakCallHelper(masm, rax.bit() | rcx.bit(), false);
Steve Blocka7e24c12009-10-30 11:49:00 +0000152}
153
154
155void Debug::GenerateReturnDebugBreak(MacroAssembler* masm) {
156 // Register state just before return from JS function (from codegen-x64.cc).
157 // ----------- S t a t e -------------
158 // -- rax: return value
159 // -----------------------------------
160 Generate_DebugBreakCallHelper(masm, rax.bit(), true);
161}
162
163
164void Debug::GenerateStoreICDebugBreak(MacroAssembler* masm) {
Leon Clarke4515c472010-02-03 11:58:03 +0000165 // Register state for IC store call (from ic-x64.cc).
Steve Blocka7e24c12009-10-30 11:49:00 +0000166 // ----------- S t a t e -------------
167 // -- rax : value
168 // -- rcx : name
Leon Clarke4515c472010-02-03 11:58:03 +0000169 // -- rdx : receiver
Steve Blocka7e24c12009-10-30 11:49:00 +0000170 // -----------------------------------
Leon Clarke4515c472010-02-03 11:58:03 +0000171 Generate_DebugBreakCallHelper(masm, rax.bit() | rcx.bit() | rdx.bit(), false);
Steve Blocka7e24c12009-10-30 11:49:00 +0000172}
173
174
175void Debug::GenerateStubNoRegistersDebugBreak(MacroAssembler* masm) {
176 // Register state for stub CallFunction (from CallFunctionStub in ic-x64.cc).
177 // ----------- S t a t e -------------
178 // No registers used on entry.
179 // -----------------------------------
180 Generate_DebugBreakCallHelper(masm, 0, false);
181}
182
183
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100184void Debug::GenerateSlot(MacroAssembler* masm) {
185 // Generate enough nop's to make space for a call instruction.
186 Label check_codesize;
187 __ bind(&check_codesize);
188 __ RecordDebugBreakSlot();
189 for (int i = 0; i < Assembler::kDebugBreakSlotLength; i++) {
190 __ nop();
191 }
192 ASSERT_EQ(Assembler::kDebugBreakSlotLength,
193 masm->SizeOfCodeGeneratedSince(&check_codesize));
194}
195
196
197void Debug::GenerateSlotDebugBreak(MacroAssembler* masm) {
198 // In the places where a debug break slot is inserted no registers can contain
199 // object pointers.
200 Generate_DebugBreakCallHelper(masm, 0, true);
201}
202
203
Steve Block6ded16b2010-05-10 14:33:55 +0100204void Debug::GeneratePlainReturnLiveEdit(MacroAssembler* masm) {
Iain Merrick75681382010-08-19 15:07:18 +0100205 masm->ret(0);
Steve Block6ded16b2010-05-10 14:33:55 +0100206}
207
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100208
Steve Block6ded16b2010-05-10 14:33:55 +0100209void Debug::GenerateFrameDropperLiveEdit(MacroAssembler* masm) {
Iain Merrick75681382010-08-19 15:07:18 +0100210 ExternalReference restarter_frame_function_slot =
211 ExternalReference(Debug_Address::RestarterFrameFunctionPointer());
212 __ movq(rax, restarter_frame_function_slot);
213 __ movq(Operand(rax, 0), Immediate(0));
214
215 // We do not know our frame height, but set rsp based on rbp.
216 __ lea(rsp, Operand(rbp, -1 * kPointerSize));
217
218 __ pop(rdi); // Function.
219 __ pop(rbp);
220
221 // Load context from the function.
222 __ movq(rsi, FieldOperand(rdi, JSFunction::kContextOffset));
223
224 // Get function code.
225 __ movq(rdx, FieldOperand(rdi, JSFunction::kSharedFunctionInfoOffset));
226 __ movq(rdx, FieldOperand(rdx, SharedFunctionInfo::kCodeOffset));
227 __ lea(rdx, FieldOperand(rdx, Code::kHeaderSize));
228
229 // Re-run JSFunction, rdi is function, rsi is context.
230 __ jmp(rdx);
Steve Block6ded16b2010-05-10 14:33:55 +0100231}
232
Iain Merrick75681382010-08-19 15:07:18 +0100233const bool Debug::kFrameDropperSupported = true;
234
Steve Blocka7e24c12009-10-30 11:49:00 +0000235#undef __
236
237
Steve Block6ded16b2010-05-10 14:33:55 +0100238
239
Steve Blocka7e24c12009-10-30 11:49:00 +0000240void BreakLocationIterator::ClearDebugBreakAtReturn() {
241 rinfo()->PatchCode(original_rinfo()->pc(),
Steve Blockd0582a62009-12-15 09:54:21 +0000242 Assembler::kJSReturnSequenceLength);
Steve Blocka7e24c12009-10-30 11:49:00 +0000243}
244
245
246bool BreakLocationIterator::IsDebugBreakAtReturn() {
247 return Debug::IsDebugBreakAtReturn(rinfo());
248}
249
250
251void BreakLocationIterator::SetDebugBreakAtReturn() {
Steve Blockd0582a62009-12-15 09:54:21 +0000252 ASSERT(Assembler::kJSReturnSequenceLength >=
253 Assembler::kCallInstructionLength);
Steve Blocka7e24c12009-10-30 11:49:00 +0000254 rinfo()->PatchCodeWithCall(Debug::debug_break_return()->entry(),
Steve Blockd0582a62009-12-15 09:54:21 +0000255 Assembler::kJSReturnSequenceLength - Assembler::kCallInstructionLength);
Steve Blocka7e24c12009-10-30 11:49:00 +0000256}
257
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100258
259bool BreakLocationIterator::IsDebugBreakAtSlot() {
260 ASSERT(IsDebugBreakSlot());
261 // Check whether the debug break slot instructions have been patched.
262 return !Assembler::IsNop(rinfo()->pc());
263}
264
265
266void BreakLocationIterator::SetDebugBreakAtSlot() {
267 ASSERT(IsDebugBreakSlot());
268 rinfo()->PatchCodeWithCall(
269 Debug::debug_break_slot()->entry(),
270 Assembler::kDebugBreakSlotLength - Assembler::kCallInstructionLength);
271}
272
273
274void BreakLocationIterator::ClearDebugBreakAtSlot() {
275 ASSERT(IsDebugBreakSlot());
276 rinfo()->PatchCode(original_rinfo()->pc(), Assembler::kDebugBreakSlotLength);
277}
278
279
Steve Blocka7e24c12009-10-30 11:49:00 +0000280#endif // ENABLE_DEBUGGER_SUPPORT
281
282} } // namespace v8::internal
Leon Clarkef7060e22010-06-03 12:02:55 +0100283
284#endif // V8_TARGET_ARCH_X64