blob: d142b11cf7b715d1d0039031e5a86ed3fd5ac181 [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#include "v8.h"
29
30#include "codegen-inl.h"
31#include "debug.h"
32
33
34namespace v8 {
35namespace internal {
36
37#ifdef ENABLE_DEBUGGER_SUPPORT
38
39bool BreakLocationIterator::IsDebugBreakAtReturn() {
40 return Debug::IsDebugBreakAtReturn(rinfo());
41}
42
43
44// Patch the JS frame exit code with a debug break call. See
45// CodeGenerator::VisitReturnStatement and VirtualFrame::Exit in codegen-ia32.cc
46// for the precise return instructions sequence.
47void BreakLocationIterator::SetDebugBreakAtReturn() {
Steve Blockd0582a62009-12-15 09:54:21 +000048 ASSERT(Assembler::kJSReturnSequenceLength >=
49 Assembler::kCallInstructionLength);
Steve Blocka7e24c12009-10-30 11:49:00 +000050 rinfo()->PatchCodeWithCall(Debug::debug_break_return()->entry(),
Steve Blockd0582a62009-12-15 09:54:21 +000051 Assembler::kJSReturnSequenceLength - Assembler::kCallInstructionLength);
Steve Blocka7e24c12009-10-30 11:49:00 +000052}
53
54
55// Restore the JS frame exit code.
56void BreakLocationIterator::ClearDebugBreakAtReturn() {
57 rinfo()->PatchCode(original_rinfo()->pc(),
Steve Blockd0582a62009-12-15 09:54:21 +000058 Assembler::kJSReturnSequenceLength);
Steve Blocka7e24c12009-10-30 11:49:00 +000059}
60
61
62// A debug break in the frame exit code is identified by the JS frame exit code
63// having been patched with a call instruction.
64bool Debug::IsDebugBreakAtReturn(RelocInfo* rinfo) {
65 ASSERT(RelocInfo::IsJSReturn(rinfo->rmode()));
Steve Block3ce2e202009-11-05 08:53:23 +000066 return rinfo->IsPatchedReturnSequence();
Steve Blocka7e24c12009-10-30 11:49:00 +000067}
68
69
70#define __ ACCESS_MASM(masm)
71
72
73static void Generate_DebugBreakCallHelper(MacroAssembler* masm,
74 RegList pointer_regs,
75 bool convert_call_to_jmp) {
76 // Save the content of all general purpose registers in memory. This copy in
77 // memory is later pushed onto the JS expression stack for the fake JS frame
78 // generated and also to the C frame generated on top of that. In the JS
79 // frame ONLY the registers containing pointers will be pushed on the
80 // expression stack. This causes the GC to update these pointers so that
81 // they will have the correct value when returning from the debugger.
82 __ SaveRegistersToMemory(kJSCallerSaved);
83
84 // Enter an internal frame.
85 __ EnterInternalFrame();
86
87 // Store the registers containing object pointers on the expression stack to
88 // make sure that these are correctly updated during GC.
89 __ PushRegistersFromMemory(pointer_regs);
90
91#ifdef DEBUG
92 __ RecordComment("// Calling from debug break to runtime - come in - over");
93#endif
94 __ Set(eax, Immediate(0)); // no arguments
95 __ mov(ebx, Immediate(ExternalReference::debug_break()));
96
Leon Clarke4515c472010-02-03 11:58:03 +000097 CEntryStub ceb(1, ExitFrame::MODE_DEBUG);
Steve Blocka7e24c12009-10-30 11:49:00 +000098 __ CallStub(&ceb);
99
100 // Restore the register values containing object pointers from the expression
101 // stack in the reverse order as they where pushed.
102 __ PopRegistersToMemory(pointer_regs);
103
104 // Get rid of the internal frame.
105 __ LeaveInternalFrame();
106
107 // If this call did not replace a call but patched other code then there will
108 // be an unwanted return address left on the stack. Here we get rid of that.
109 if (convert_call_to_jmp) {
110 __ pop(eax);
111 }
112
113 // Finally restore all registers.
114 __ RestoreRegistersFromMemory(kJSCallerSaved);
115
116 // Now that the break point has been handled, resume normal execution by
117 // jumping to the target address intended by the caller and that was
118 // overwritten by the address of DebugBreakXXX.
119 ExternalReference after_break_target =
120 ExternalReference(Debug_Address::AfterBreakTarget());
121 __ jmp(Operand::StaticVariable(after_break_target));
122}
123
124
125void Debug::GenerateLoadICDebugBreak(MacroAssembler* masm) {
126 // Register state for IC load call (from ic-ia32.cc).
127 // ----------- S t a t e -------------
Andrei Popescu402d9372010-02-26 13:31:12 +0000128 // -- eax : receiver
Steve Blocka7e24c12009-10-30 11:49:00 +0000129 // -- ecx : name
130 // -----------------------------------
Andrei Popescu402d9372010-02-26 13:31:12 +0000131 Generate_DebugBreakCallHelper(masm, eax.bit() | ecx.bit(), false);
Steve Blocka7e24c12009-10-30 11:49:00 +0000132}
133
134
135void Debug::GenerateStoreICDebugBreak(MacroAssembler* masm) {
Leon Clarke4515c472010-02-03 11:58:03 +0000136 // Register state for IC store call (from ic-ia32.cc).
Steve Blocka7e24c12009-10-30 11:49:00 +0000137 // ----------- S t a t e -------------
138 // -- eax : value
139 // -- ecx : name
Leon Clarke4515c472010-02-03 11:58:03 +0000140 // -- edx : receiver
Steve Blocka7e24c12009-10-30 11:49:00 +0000141 // -----------------------------------
Leon Clarke4515c472010-02-03 11:58:03 +0000142 Generate_DebugBreakCallHelper(masm, eax.bit() | ecx.bit() | edx.bit(), false);
Steve Blocka7e24c12009-10-30 11:49:00 +0000143}
144
145
146void Debug::GenerateKeyedLoadICDebugBreak(MacroAssembler* masm) {
147 // Register state for keyed IC load call (from ic-ia32.cc).
148 // ----------- S t a t e -------------
Steve Block6ded16b2010-05-10 14:33:55 +0100149 // -- edx : receiver
150 // -- eax : key
Steve Blocka7e24c12009-10-30 11:49:00 +0000151 // -----------------------------------
Steve Block6ded16b2010-05-10 14:33:55 +0100152 Generate_DebugBreakCallHelper(masm, eax.bit() | edx.bit(), false);
Steve Blocka7e24c12009-10-30 11:49:00 +0000153}
154
155
156void Debug::GenerateKeyedStoreICDebugBreak(MacroAssembler* masm) {
157 // Register state for keyed IC load call (from ic-ia32.cc).
158 // ----------- S t a t e -------------
159 // -- eax : value
Steve Block6ded16b2010-05-10 14:33:55 +0100160 // -- ecx : key
161 // -- edx : receiver
Steve Blocka7e24c12009-10-30 11:49:00 +0000162 // -----------------------------------
163 // Register eax contains an object that needs to be pushed on the
164 // expression stack of the fake JS frame.
Steve Block6ded16b2010-05-10 14:33:55 +0100165 Generate_DebugBreakCallHelper(masm, eax.bit() | ecx.bit() | edx.bit(), false);
Steve Blocka7e24c12009-10-30 11:49:00 +0000166}
167
168
169void Debug::GenerateCallICDebugBreak(MacroAssembler* masm) {
170 // Register state for keyed IC call call (from ic-ia32.cc)
171 // ----------- S t a t e -------------
172 // -- eax: number of arguments
173 // -----------------------------------
174 // The number of arguments in eax is not smi encoded.
175 Generate_DebugBreakCallHelper(masm, 0, false);
176}
177
178
179void Debug::GenerateConstructCallDebugBreak(MacroAssembler* masm) {
180 // Register state just before return from JS function (from codegen-ia32.cc).
181 // eax is the actual number of arguments not encoded as a smi see comment
182 // above IC call.
183 // ----------- S t a t e -------------
184 // -- eax: number of arguments
185 // -----------------------------------
186 // The number of arguments in eax is not smi encoded.
187 Generate_DebugBreakCallHelper(masm, 0, false);
188}
189
190
191void Debug::GenerateReturnDebugBreak(MacroAssembler* masm) {
192 // Register state just before return from JS function (from codegen-ia32.cc).
193 // ----------- S t a t e -------------
194 // -- eax: return value
195 // -----------------------------------
196 Generate_DebugBreakCallHelper(masm, eax.bit(), true);
197}
198
199
200void Debug::GenerateStubNoRegistersDebugBreak(MacroAssembler* masm) {
201 // Register state for stub CallFunction (from CallFunctionStub in ic-ia32.cc).
202 // ----------- S t a t e -------------
203 // No registers used on entry.
204 // -----------------------------------
205 Generate_DebugBreakCallHelper(masm, 0, false);
206}
207
208
Steve Block6ded16b2010-05-10 14:33:55 +0100209void Debug::GeneratePlainReturnLiveEdit(MacroAssembler* masm) {
210 masm->ret(0);
211}
212
213// FrameDropper is a code replacement for a JavaScript frame with possibly
214// several frames above.
215// There is no calling conventions here, because it never actually gets called,
216// it only gets returned to.
217// Frame structure (conforms InternalFrame structure):
218// -- JSFunction
219// -- code
220// -- SMI maker
221// -- context
222// -- frame base
223void Debug::GenerateFrameDropperLiveEdit(MacroAssembler* masm) {
224 // We do not know our frame height, but set esp based on ebp.
225 __ lea(esp, Operand(ebp, -4 * kPointerSize));
226
227 __ pop(edi); // function
228
229 // Skip code self-reference and marker.
230 __ add(Operand(esp), Immediate(2 * kPointerSize));
231
232 __ pop(esi); // Context.
233 __ pop(ebp);
234
235 // Get function code.
236 __ mov(edx, FieldOperand(edi, JSFunction::kSharedFunctionInfoOffset));
237 __ mov(edx, FieldOperand(edx, SharedFunctionInfo::kCodeOffset));
238 __ lea(edx, FieldOperand(edx, Code::kHeaderSize));
239
240 // Re-run JSFunction, edi is function, esi is context.
241 __ jmp(Operand(edx));
242}
243
Steve Blocka7e24c12009-10-30 11:49:00 +0000244#undef __
245
Steve Block6ded16b2010-05-10 14:33:55 +0100246
247void Debug::SetUpFrameDropperFrame(StackFrame* bottom_js_frame,
248 Handle<Code> code) {
249 ASSERT(bottom_js_frame->is_java_script());
250
251 Address fp = bottom_js_frame->fp();
252 Memory::Object_at(fp - 4 * kPointerSize) =
253 Memory::Object_at(fp - 2 * kPointerSize); // Move edi (function).
254
255 Memory::Object_at(fp - 3 * kPointerSize) = *code;
256 Memory::Object_at(fp - 2 * kPointerSize) = Smi::FromInt(StackFrame::INTERNAL);
257}
258const int Debug::kFrameDropperFrameSize = 5;
259
260
Steve Blocka7e24c12009-10-30 11:49:00 +0000261#endif // ENABLE_DEBUGGER_SUPPORT
262
263} } // namespace v8::internal