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