blob: f2bb62bc696e8ff7b4171d2172ad75d553f93577 [file] [log] [blame]
ager@chromium.org5ec48922009-05-05 07:25:34 +00001// Copyright 2009 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
kasperl@chromium.org71affb52009-05-26 05:44:31 +000028
29#include "v8.h"
30
31#include "codegen-inl.h"
32#include "debug.h"
33
34
35namespace v8 {
36namespace internal {
37
38#ifdef ENABLE_DEBUGGER_SUPPORT
39
40bool Debug::IsDebugBreakAtReturn(v8::internal::RelocInfo* rinfo) {
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +000041 ASSERT(RelocInfo::IsJSReturn(rinfo->rmode()));
sgjesse@chromium.org911335c2009-08-19 12:59:44 +000042 // 11th byte of patch is 0x49 (REX.WB byte of computed jump/call to r10),
43 // 11th byte of JS return is 0xCC (int3).
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +000044 ASSERT(*(rinfo->pc() + 10) == 0x49 || *(rinfo->pc() + 10) == 0xCC);
sgjesse@chromium.org911335c2009-08-19 12:59:44 +000045 return (*(rinfo->pc() + 10) != 0xCC);
kasperl@chromium.org71affb52009-05-26 05:44:31 +000046}
47
sgjesse@chromium.org911335c2009-08-19 12:59:44 +000048#define __ ACCESS_MASM(masm)
49
50static void Generate_DebugBreakCallHelper(MacroAssembler* masm,
51 RegList pointer_regs,
52 bool convert_call_to_jmp) {
53 // Save the content of all general purpose registers in memory. This copy in
54 // memory is later pushed onto the JS expression stack for the fake JS frame
55 // generated and also to the C frame generated on top of that. In the JS
56 // frame ONLY the registers containing pointers will be pushed on the
57 // expression stack. This causes the GC to update these pointers so that
58 // they will have the correct value when returning from the debugger.
59 __ SaveRegistersToMemory(kJSCallerSaved);
60
61 // Enter an internal frame.
62 __ EnterInternalFrame();
63
64 // Store the registers containing object pointers on the expression stack to
65 // make sure that these are correctly updated during GC.
66 __ PushRegistersFromMemory(pointer_regs);
67
68#ifdef DEBUG
69 __ RecordComment("// Calling from debug break to runtime - come in - over");
70#endif
71 __ xor_(rax, rax); // No arguments (argc == 0).
72 __ movq(rbx, ExternalReference::debug_break());
73
74 CEntryDebugBreakStub ceb;
75 __ CallStub(&ceb);
76
77 // Restore the register values containing object pointers from the expression
78 // stack in the reverse order as they where pushed.
79 __ PopRegistersToMemory(pointer_regs);
80
81 // Get rid of the internal frame.
82 __ LeaveInternalFrame();
83
84 // If this call did not replace a call but patched other code then there will
85 // be an unwanted return address left on the stack. Here we get rid of that.
86 if (convert_call_to_jmp) {
87 __ pop(rax);
88 }
89
90 // Finally restore all registers.
91 __ RestoreRegistersFromMemory(kJSCallerSaved);
92
93 // Now that the break point has been handled, resume normal execution by
94 // jumping to the target address intended by the caller and that was
95 // overwritten by the address of DebugBreakXXX.
96 ExternalReference after_break_target =
97 ExternalReference(Debug_Address::AfterBreakTarget());
98 __ movq(kScratchRegister, after_break_target);
99 __ jmp(Operand(kScratchRegister, 0));
100}
101
102
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000103void Debug::GenerateCallICDebugBreak(MacroAssembler* masm) {
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000104 // Register state for keyed IC call call (from ic-x64.cc)
105 // ----------- S t a t e -------------
106 // -- rax: number of arguments
107 // -----------------------------------
108 // The number of arguments in rax is not smi encoded.
109 Generate_DebugBreakCallHelper(masm, 0, false);
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000110}
111
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000112
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000113void Debug::GenerateConstructCallDebugBreak(MacroAssembler* masm) {
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000114 // Register state just before return from JS function (from codegen-x64.cc).
115 // rax is the actual number of arguments not encoded as a smi, see comment
116 // above IC call.
117 // ----------- S t a t e -------------
118 // -- rax: number of arguments
119 // -----------------------------------
120 // The number of arguments in rax is not smi encoded.
121 Generate_DebugBreakCallHelper(masm, 0, false);
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000122}
123
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000124
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000125void Debug::GenerateKeyedLoadICDebugBreak(MacroAssembler* masm) {
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000126 // Register state for keyed IC load call (from ic-x64.cc).
127 // ----------- S t a t e -------------
128 // No registers used on entry.
129 // -----------------------------------
130 Generate_DebugBreakCallHelper(masm, 0, false);
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000131}
132
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000133
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000134void Debug::GenerateKeyedStoreICDebugBreak(MacroAssembler* masm) {
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000135 // Register state for keyed IC load call (from ic-x64.cc).
136 // ----------- S t a t e -------------
137 // -- rax : value
138 // -----------------------------------
139 // Register rax contains an object that needs to be pushed on the
140 // expression stack of the fake JS frame.
141 Generate_DebugBreakCallHelper(masm, rax.bit(), false);
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000142}
143
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000144
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000145void Debug::GenerateLoadICDebugBreak(MacroAssembler* masm) {
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000146 // Register state for IC load call (from ic-x64.cc).
147 // ----------- S t a t e -------------
148 // -- rcx : name
149 // -----------------------------------
150 Generate_DebugBreakCallHelper(masm, 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::GenerateReturnDebugBreakEntry(MacroAssembler* masm) {
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000164 // OK to clobber rbx as we are returning from a JS function through the code
165 // generated by CodeGenerator::GenerateReturnSequence()
166 ExternalReference debug_break_return =
167 ExternalReference(Debug_Address::DebugBreakReturn());
168 __ movq(rbx, debug_break_return);
169 __ movq(rbx, Operand(rbx, 0));
170 __ addq(rbx, Immediate(Code::kHeaderSize - kHeapObjectTag));
171 __ jmp(rbx);
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000172}
173
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000174
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000175void Debug::GenerateStoreICDebugBreak(MacroAssembler* masm) {
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000176 // REgister state for IC store call (from ic-x64.cc).
177 // ----------- S t a t e -------------
178 // -- rax : value
179 // -- rcx : name
180 // -----------------------------------
181 Generate_DebugBreakCallHelper(masm, rax.bit() | rcx.bit(), false);
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000182}
183
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000184
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000185void Debug::GenerateStubNoRegistersDebugBreak(MacroAssembler* masm) {
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000186 // Register state for stub CallFunction (from CallFunctionStub in ic-x64.cc).
187 // ----------- S t a t e -------------
188 // No registers used on entry.
189 // -----------------------------------
190 Generate_DebugBreakCallHelper(masm, 0, false);
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000191}
192
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000193
194#undef __
195
196
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000197void BreakLocationIterator::ClearDebugBreakAtReturn() {
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000198 rinfo()->PatchCode(original_rinfo()->pc(),
199 Debug::kX64JSReturnSequenceLength);
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000200}
201
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000202
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000203bool BreakLocationIterator::IsDebugBreakAtReturn() {
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000204 return Debug::IsDebugBreakAtReturn(rinfo());
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000205}
206
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000207
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000208void BreakLocationIterator::SetDebugBreakAtReturn() {
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000209 ASSERT(Debug::kX64JSReturnSequenceLength >= Debug::kX64CallInstructionLength);
210 rinfo()->PatchCodeWithCall(Debug::debug_break_return_entry()->entry(),
211 Debug::kX64JSReturnSequenceLength - Debug::kX64CallInstructionLength);
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000212}
213
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000214#endif // ENABLE_DEBUGGER_SUPPORT
215
216} } // namespace v8::internal