debug-x64.cc revision c7cc028aaeedbbfa11c11d0b7b243b3d9e837ed9
1// Copyright 2011 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 28#include "v8.h" 29 30#if defined(V8_TARGET_ARCH_X64) 31 32#include "assembler.h" 33#include "codegen.h" 34#include "debug.h" 35 36 37namespace v8 { 38namespace internal { 39 40#ifdef ENABLE_DEBUGGER_SUPPORT 41 42bool BreakLocationIterator::IsDebugBreakAtReturn() { 43 return Debug::IsDebugBreakAtReturn(rinfo()); 44} 45 46 47// Patch the JS frame exit code with a debug break call. See 48// CodeGenerator::VisitReturnStatement and VirtualFrame::Exit in codegen-x64.cc 49// for the precise return instructions sequence. 50void BreakLocationIterator::SetDebugBreakAtReturn() { 51 ASSERT(Assembler::kJSReturnSequenceLength >= 52 Assembler::kCallInstructionLength); 53 rinfo()->PatchCodeWithCall( 54 Isolate::Current()->debug()->debug_break_return()->entry(), 55 Assembler::kJSReturnSequenceLength - Assembler::kCallInstructionLength); 56} 57 58 59// Restore the JS frame exit code. 60void BreakLocationIterator::ClearDebugBreakAtReturn() { 61 rinfo()->PatchCode(original_rinfo()->pc(), 62 Assembler::kJSReturnSequenceLength); 63} 64 65 66// A debug break in the frame exit code is identified by the JS frame exit code 67// having been patched with a call instruction. 68bool Debug::IsDebugBreakAtReturn(v8::internal::RelocInfo* rinfo) { 69 ASSERT(RelocInfo::IsJSReturn(rinfo->rmode())); 70 return rinfo->IsPatchedReturnSequence(); 71} 72 73 74bool BreakLocationIterator::IsDebugBreakAtSlot() { 75 ASSERT(IsDebugBreakSlot()); 76 // Check whether the debug break slot instructions have been patched. 77 return !Assembler::IsNop(rinfo()->pc()); 78} 79 80 81void BreakLocationIterator::SetDebugBreakAtSlot() { 82 ASSERT(IsDebugBreakSlot()); 83 rinfo()->PatchCodeWithCall( 84 Isolate::Current()->debug()->debug_break_slot()->entry(), 85 Assembler::kDebugBreakSlotLength - Assembler::kCallInstructionLength); 86} 87 88 89void BreakLocationIterator::ClearDebugBreakAtSlot() { 90 ASSERT(IsDebugBreakSlot()); 91 rinfo()->PatchCode(original_rinfo()->pc(), Assembler::kDebugBreakSlotLength); 92} 93 94 95#define __ ACCESS_MASM(masm) 96 97 98static void Generate_DebugBreakCallHelper(MacroAssembler* masm, 99 RegList object_regs, 100 RegList non_object_regs, 101 bool convert_call_to_jmp) { 102 // Enter an internal frame. 103 { 104 FrameScope scope(masm, StackFrame::INTERNAL); 105 106 // Store the registers containing live values on the expression stack to 107 // make sure that these are correctly updated during GC. Non object values 108 // are stored as as two smis causing it to be untouched by GC. 109 ASSERT((object_regs & ~kJSCallerSaved) == 0); 110 ASSERT((non_object_regs & ~kJSCallerSaved) == 0); 111 ASSERT((object_regs & non_object_regs) == 0); 112 for (int i = 0; i < kNumJSCallerSaved; i++) { 113 int r = JSCallerSavedCode(i); 114 Register reg = { r }; 115 ASSERT(!reg.is(kScratchRegister)); 116 if ((object_regs & (1 << r)) != 0) { 117 __ push(reg); 118 } 119 // Store the 64-bit value as two smis. 120 if ((non_object_regs & (1 << r)) != 0) { 121 __ movq(kScratchRegister, reg); 122 __ Integer32ToSmi(reg, reg); 123 __ push(reg); 124 __ sar(kScratchRegister, Immediate(32)); 125 __ Integer32ToSmi(kScratchRegister, kScratchRegister); 126 __ push(kScratchRegister); 127 } 128 } 129 130#ifdef DEBUG 131 __ RecordComment("// Calling from debug break to runtime - come in - over"); 132#endif 133 __ Set(rax, 0); // No arguments (argc == 0). 134 __ movq(rbx, ExternalReference::debug_break(masm->isolate())); 135 136 CEntryStub ceb(1); 137 __ CallStub(&ceb); 138 139 // Restore the register values from the expression stack. 140 for (int i = kNumJSCallerSaved - 1; i >= 0; i--) { 141 int r = JSCallerSavedCode(i); 142 Register reg = { r }; 143 if (FLAG_debug_code) { 144 __ Set(reg, kDebugZapValue); 145 } 146 if ((object_regs & (1 << r)) != 0) { 147 __ pop(reg); 148 } 149 // Reconstruct the 64-bit value from two smis. 150 if ((non_object_regs & (1 << r)) != 0) { 151 __ pop(kScratchRegister); 152 __ SmiToInteger32(kScratchRegister, kScratchRegister); 153 __ shl(kScratchRegister, Immediate(32)); 154 __ pop(reg); 155 __ SmiToInteger32(reg, reg); 156 __ or_(reg, kScratchRegister); 157 } 158 } 159 160 // Get rid of the internal frame. 161 } 162 163 // If this call did not replace a call but patched other code then there will 164 // be an unwanted return address left on the stack. Here we get rid of that. 165 if (convert_call_to_jmp) { 166 __ addq(rsp, Immediate(kPointerSize)); 167 } 168 169 // Now that the break point has been handled, resume normal execution by 170 // jumping to the target address intended by the caller and that was 171 // overwritten by the address of DebugBreakXXX. 172 ExternalReference after_break_target = 173 ExternalReference(Debug_Address::AfterBreakTarget(), masm->isolate()); 174 __ movq(kScratchRegister, after_break_target); 175 __ jmp(Operand(kScratchRegister, 0)); 176} 177 178 179void Debug::GenerateLoadICDebugBreak(MacroAssembler* masm) { 180 // Register state for IC load call (from ic-x64.cc). 181 // ----------- S t a t e ------------- 182 // -- rax : receiver 183 // -- rcx : name 184 // ----------------------------------- 185 Generate_DebugBreakCallHelper(masm, rax.bit() | rcx.bit(), 0, false); 186} 187 188 189void Debug::GenerateStoreICDebugBreak(MacroAssembler* masm) { 190 // Register state for IC store call (from ic-x64.cc). 191 // ----------- S t a t e ------------- 192 // -- rax : value 193 // -- rcx : name 194 // -- rdx : receiver 195 // ----------------------------------- 196 Generate_DebugBreakCallHelper( 197 masm, rax.bit() | rcx.bit() | rdx.bit(), 0, false); 198} 199 200 201void Debug::GenerateKeyedLoadICDebugBreak(MacroAssembler* masm) { 202 // Register state for keyed IC load call (from ic-x64.cc). 203 // ----------- S t a t e ------------- 204 // -- rax : key 205 // -- rdx : receiver 206 // ----------------------------------- 207 Generate_DebugBreakCallHelper(masm, rax.bit() | rdx.bit(), 0, false); 208} 209 210 211void Debug::GenerateKeyedStoreICDebugBreak(MacroAssembler* masm) { 212 // Register state for keyed IC load call (from ic-x64.cc). 213 // ----------- S t a t e ------------- 214 // -- rax : value 215 // -- rcx : key 216 // -- rdx : receiver 217 // ----------------------------------- 218 Generate_DebugBreakCallHelper( 219 masm, rax.bit() | rcx.bit() | rdx.bit(), 0, false); 220} 221 222 223void Debug::GenerateCallICDebugBreak(MacroAssembler* masm) { 224 // Register state for IC call call (from ic-x64.cc) 225 // ----------- S t a t e ------------- 226 // -- rcx: function name 227 // ----------------------------------- 228 Generate_DebugBreakCallHelper(masm, rcx.bit(), 0, false); 229} 230 231 232void Debug::GenerateConstructCallDebugBreak(MacroAssembler* masm) { 233 // Register state just before return from JS function (from codegen-x64.cc). 234 // rax is the actual number of arguments not encoded as a smi, see comment 235 // above IC call. 236 // ----------- S t a t e ------------- 237 // -- rax: number of arguments 238 // ----------------------------------- 239 // The number of arguments in rax is not smi encoded. 240 Generate_DebugBreakCallHelper(masm, rdi.bit(), rax.bit(), false); 241} 242 243 244void Debug::GenerateReturnDebugBreak(MacroAssembler* masm) { 245 // Register state just before return from JS function (from codegen-x64.cc). 246 // ----------- S t a t e ------------- 247 // -- rax: return value 248 // ----------------------------------- 249 Generate_DebugBreakCallHelper(masm, rax.bit(), 0, true); 250} 251 252 253void Debug::GenerateCallFunctionStubDebugBreak(MacroAssembler* masm) { 254 // Register state for stub CallFunction (from CallFunctionStub in ic-x64.cc). 255 // ----------- S t a t e ------------- 256 // -- rdi : function 257 // ----------------------------------- 258 Generate_DebugBreakCallHelper(masm, rdi.bit(), 0, false); 259} 260 261 262void Debug::GenerateSlot(MacroAssembler* masm) { 263 // Generate enough nop's to make space for a call instruction. 264 Label check_codesize; 265 __ bind(&check_codesize); 266 __ RecordDebugBreakSlot(); 267 __ Nop(Assembler::kDebugBreakSlotLength); 268 ASSERT_EQ(Assembler::kDebugBreakSlotLength, 269 masm->SizeOfCodeGeneratedSince(&check_codesize)); 270} 271 272 273void Debug::GenerateSlotDebugBreak(MacroAssembler* masm) { 274 // In the places where a debug break slot is inserted no registers can contain 275 // object pointers. 276 Generate_DebugBreakCallHelper(masm, 0, 0, true); 277} 278 279 280void Debug::GeneratePlainReturnLiveEdit(MacroAssembler* masm) { 281 masm->ret(0); 282} 283 284 285void Debug::GenerateFrameDropperLiveEdit(MacroAssembler* masm) { 286 ExternalReference restarter_frame_function_slot = 287 ExternalReference(Debug_Address::RestarterFrameFunctionPointer(), 288 masm->isolate()); 289 __ movq(rax, restarter_frame_function_slot); 290 __ movq(Operand(rax, 0), Immediate(0)); 291 292 // We do not know our frame height, but set rsp based on rbp. 293 __ lea(rsp, Operand(rbp, -1 * kPointerSize)); 294 295 __ pop(rdi); // Function. 296 __ pop(rbp); 297 298 // Load context from the function. 299 __ movq(rsi, FieldOperand(rdi, JSFunction::kContextOffset)); 300 301 // Get function code. 302 __ movq(rdx, FieldOperand(rdi, JSFunction::kSharedFunctionInfoOffset)); 303 __ movq(rdx, FieldOperand(rdx, SharedFunctionInfo::kCodeOffset)); 304 __ lea(rdx, FieldOperand(rdx, Code::kHeaderSize)); 305 306 // Re-run JSFunction, rdi is function, rsi is context. 307 __ jmp(rdx); 308} 309 310const bool Debug::kFrameDropperSupported = true; 311 312#undef __ 313 314#endif // ENABLE_DEBUGGER_SUPPORT 315 316} } // namespace v8::internal 317 318#endif // V8_TARGET_ARCH_X64 319