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 #include "v8.h" 29 30 #include "codegen-inl.h" 31 #include "debug.h" 32 33 34 namespace v8 { 35 namespace internal { 36 37 #ifdef ENABLE_DEBUGGER_SUPPORT 38 39 bool 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. 47 void BreakLocationIterator::SetDebugBreakAtReturn() { 48 ASSERT(Assembler::kJSReturnSequenceLength >= 49 Assembler::kCallInstructionLength); 50 rinfo()->PatchCodeWithCall(Debug::debug_break_return()->entry(), 51 Assembler::kJSReturnSequenceLength - Assembler::kCallInstructionLength); 52 } 53 54 55 // Restore the JS frame exit code. 56 void BreakLocationIterator::ClearDebugBreakAtReturn() { 57 rinfo()->PatchCode(original_rinfo()->pc(), 58 Assembler::kJSReturnSequenceLength); 59 } 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. 64 bool Debug::IsDebugBreakAtReturn(RelocInfo* rinfo) { 65 ASSERT(RelocInfo::IsJSReturn(rinfo->rmode())); 66 return rinfo->IsPatchedReturnSequence(); 67 } 68 69 70 #define __ ACCESS_MASM(masm) 71 72 73 static 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 97 CEntryStub ceb(1, ExitFrame::MODE_DEBUG); 98 __ 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 125 void Debug::GenerateLoadICDebugBreak(MacroAssembler* masm) { 126 // Register state for IC load call (from ic-ia32.cc). 127 // ----------- S t a t e ------------- 128 // -- eax : receiver 129 // -- ecx : name 130 // ----------------------------------- 131 Generate_DebugBreakCallHelper(masm, eax.bit() | ecx.bit(), false); 132 } 133 134 135 void Debug::GenerateStoreICDebugBreak(MacroAssembler* masm) { 136 // Register state for IC store call (from ic-ia32.cc). 137 // ----------- S t a t e ------------- 138 // -- eax : value 139 // -- ecx : name 140 // -- edx : receiver 141 // ----------------------------------- 142 Generate_DebugBreakCallHelper(masm, eax.bit() | ecx.bit() | edx.bit(), false); 143 } 144 145 146 void Debug::GenerateKeyedLoadICDebugBreak(MacroAssembler* masm) { 147 // Register state for keyed IC load call (from ic-ia32.cc). 148 // ----------- S t a t e ------------- 149 // No registers used on entry. 150 // ----------------------------------- 151 Generate_DebugBreakCallHelper(masm, 0, false); 152 } 153 154 155 void Debug::GenerateKeyedStoreICDebugBreak(MacroAssembler* masm) { 156 // Register state for keyed IC load call (from ic-ia32.cc). 157 // ----------- S t a t e ------------- 158 // -- eax : value 159 // ----------------------------------- 160 // Register eax contains an object that needs to be pushed on the 161 // expression stack of the fake JS frame. 162 Generate_DebugBreakCallHelper(masm, eax.bit(), false); 163 } 164 165 166 void Debug::GenerateCallICDebugBreak(MacroAssembler* masm) { 167 // Register state for keyed IC call call (from ic-ia32.cc) 168 // ----------- S t a t e ------------- 169 // -- eax: number of arguments 170 // ----------------------------------- 171 // The number of arguments in eax is not smi encoded. 172 Generate_DebugBreakCallHelper(masm, 0, false); 173 } 174 175 176 void Debug::GenerateConstructCallDebugBreak(MacroAssembler* masm) { 177 // Register state just before return from JS function (from codegen-ia32.cc). 178 // eax is the actual number of arguments not encoded as a smi see comment 179 // above IC call. 180 // ----------- S t a t e ------------- 181 // -- eax: number of arguments 182 // ----------------------------------- 183 // The number of arguments in eax is not smi encoded. 184 Generate_DebugBreakCallHelper(masm, 0, false); 185 } 186 187 188 void Debug::GenerateReturnDebugBreak(MacroAssembler* masm) { 189 // Register state just before return from JS function (from codegen-ia32.cc). 190 // ----------- S t a t e ------------- 191 // -- eax: return value 192 // ----------------------------------- 193 Generate_DebugBreakCallHelper(masm, eax.bit(), true); 194 } 195 196 197 void Debug::GenerateStubNoRegistersDebugBreak(MacroAssembler* masm) { 198 // Register state for stub CallFunction (from CallFunctionStub in ic-ia32.cc). 199 // ----------- S t a t e ------------- 200 // No registers used on entry. 201 // ----------------------------------- 202 Generate_DebugBreakCallHelper(masm, 0, false); 203 } 204 205 206 #undef __ 207 208 #endif // ENABLE_DEBUGGER_SUPPORT 209 210 } } // namespace v8::internal 211