1 // Copyright 2010 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 29 #include "v8.h" 30 31 #include "codegen-inl.h" 32 #include "debug.h" 33 34 35 namespace v8 { 36 namespace internal { 37 38 #ifdef ENABLE_DEBUGGER_SUPPORT 39 40 bool Debug::IsDebugBreakAtReturn(v8::internal::RelocInfo* rinfo) { 41 ASSERT(RelocInfo::IsJSReturn(rinfo->rmode())); 42 return rinfo->IsPatchedReturnSequence(); 43 } 44 45 #define __ ACCESS_MASM(masm) 46 47 static void Generate_DebugBreakCallHelper(MacroAssembler* masm, 48 RegList pointer_regs, 49 bool convert_call_to_jmp) { 50 // Save the content of all general purpose registers in memory. This copy in 51 // memory is later pushed onto the JS expression stack for the fake JS frame 52 // generated and also to the C frame generated on top of that. In the JS 53 // frame ONLY the registers containing pointers will be pushed on the 54 // expression stack. This causes the GC to update these pointers so that 55 // they will have the correct value when returning from the debugger. 56 __ SaveRegistersToMemory(kJSCallerSaved); 57 58 // Enter an internal frame. 59 __ EnterInternalFrame(); 60 61 // Store the registers containing object pointers on the expression stack to 62 // make sure that these are correctly updated during GC. 63 __ PushRegistersFromMemory(pointer_regs); 64 65 #ifdef DEBUG 66 __ RecordComment("// Calling from debug break to runtime - come in - over"); 67 #endif 68 __ xor_(rax, rax); // No arguments (argc == 0). 69 __ movq(rbx, ExternalReference::debug_break()); 70 71 CEntryStub ceb(1, ExitFrame::MODE_DEBUG); 72 __ CallStub(&ceb); 73 74 // Restore the register values containing object pointers from the expression 75 // stack in the reverse order as they where pushed. 76 __ PopRegistersToMemory(pointer_regs); 77 78 // Get rid of the internal frame. 79 __ LeaveInternalFrame(); 80 81 // If this call did not replace a call but patched other code then there will 82 // be an unwanted return address left on the stack. Here we get rid of that. 83 if (convert_call_to_jmp) { 84 __ pop(rax); 85 } 86 87 // Finally restore all registers. 88 __ RestoreRegistersFromMemory(kJSCallerSaved); 89 90 // Now that the break point has been handled, resume normal execution by 91 // jumping to the target address intended by the caller and that was 92 // overwritten by the address of DebugBreakXXX. 93 ExternalReference after_break_target = 94 ExternalReference(Debug_Address::AfterBreakTarget()); 95 __ movq(kScratchRegister, after_break_target); 96 __ jmp(Operand(kScratchRegister, 0)); 97 } 98 99 100 void Debug::GenerateCallICDebugBreak(MacroAssembler* masm) { 101 // Register state for keyed IC call call (from ic-x64.cc) 102 // ----------- S t a t e ------------- 103 // -- rax: number of arguments 104 // ----------------------------------- 105 // The number of arguments in rax is not smi encoded. 106 Generate_DebugBreakCallHelper(masm, 0, false); 107 } 108 109 110 void Debug::GenerateConstructCallDebugBreak(MacroAssembler* masm) { 111 // Register state just before return from JS function (from codegen-x64.cc). 112 // rax is the actual number of arguments not encoded as a smi, see comment 113 // above IC call. 114 // ----------- S t a t e ------------- 115 // -- rax: number of arguments 116 // ----------------------------------- 117 // The number of arguments in rax is not smi encoded. 118 Generate_DebugBreakCallHelper(masm, 0, false); 119 } 120 121 122 void Debug::GenerateKeyedLoadICDebugBreak(MacroAssembler* masm) { 123 // Register state for keyed IC load call (from ic-x64.cc). 124 // ----------- S t a t e ------------- 125 // No registers used on entry. 126 // ----------------------------------- 127 Generate_DebugBreakCallHelper(masm, 0, false); 128 } 129 130 131 void Debug::GenerateKeyedStoreICDebugBreak(MacroAssembler* masm) { 132 // Register state for keyed IC load call (from ic-x64.cc). 133 // ----------- S t a t e ------------- 134 // -- rax : value 135 // ----------------------------------- 136 // Register rax contains an object that needs to be pushed on the 137 // expression stack of the fake JS frame. 138 Generate_DebugBreakCallHelper(masm, rax.bit(), false); 139 } 140 141 142 void Debug::GenerateLoadICDebugBreak(MacroAssembler* masm) { 143 // Register state for IC load call (from ic-x64.cc). 144 // ----------- S t a t e ------------- 145 // -- rcx : name 146 // ----------------------------------- 147 Generate_DebugBreakCallHelper(masm, rcx.bit(), false); 148 } 149 150 151 void Debug::GenerateReturnDebugBreak(MacroAssembler* masm) { 152 // Register state just before return from JS function (from codegen-x64.cc). 153 // ----------- S t a t e ------------- 154 // -- rax: return value 155 // ----------------------------------- 156 Generate_DebugBreakCallHelper(masm, rax.bit(), true); 157 } 158 159 160 void Debug::GenerateStoreICDebugBreak(MacroAssembler* masm) { 161 // Register state for IC store call (from ic-x64.cc). 162 // ----------- S t a t e ------------- 163 // -- rax : value 164 // -- rcx : name 165 // -- rdx : receiver 166 // ----------------------------------- 167 Generate_DebugBreakCallHelper(masm, rax.bit() | rcx.bit() | rdx.bit(), false); 168 } 169 170 171 void Debug::GenerateStubNoRegistersDebugBreak(MacroAssembler* masm) { 172 // Register state for stub CallFunction (from CallFunctionStub in ic-x64.cc). 173 // ----------- S t a t e ------------- 174 // No registers used on entry. 175 // ----------------------------------- 176 Generate_DebugBreakCallHelper(masm, 0, false); 177 } 178 179 180 #undef __ 181 182 183 void BreakLocationIterator::ClearDebugBreakAtReturn() { 184 rinfo()->PatchCode(original_rinfo()->pc(), 185 Assembler::kJSReturnSequenceLength); 186 } 187 188 189 bool BreakLocationIterator::IsDebugBreakAtReturn() { 190 return Debug::IsDebugBreakAtReturn(rinfo()); 191 } 192 193 194 void BreakLocationIterator::SetDebugBreakAtReturn() { 195 ASSERT(Assembler::kJSReturnSequenceLength >= 196 Assembler::kCallInstructionLength); 197 rinfo()->PatchCodeWithCall(Debug::debug_break_return()->entry(), 198 Assembler::kJSReturnSequenceLength - Assembler::kCallInstructionLength); 199 } 200 201 #endif // ENABLE_DEBUGGER_SUPPORT 202 203 } } // namespace v8::internal 204