Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 1 | // Copyright 2010 the V8 project authors. All rights reserved. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 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() { |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 48 | ASSERT(Assembler::kJSReturnSequenceLength >= |
| 49 | Assembler::kCallInstructionLength); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 50 | rinfo()->PatchCodeWithCall(Debug::debug_break_return()->entry(), |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 51 | Assembler::kJSReturnSequenceLength - Assembler::kCallInstructionLength); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | |
| 55 | // Restore the JS frame exit code. |
| 56 | void BreakLocationIterator::ClearDebugBreakAtReturn() { |
| 57 | rinfo()->PatchCode(original_rinfo()->pc(), |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 58 | Assembler::kJSReturnSequenceLength); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 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())); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 66 | return rinfo->IsPatchedReturnSequence(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 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 | |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 97 | CEntryStub ceb(1, ExitFrame::MODE_DEBUG); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 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 ------------- |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 128 | // -- eax : receiver |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 129 | // -- ecx : name |
| 130 | // ----------------------------------- |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 131 | Generate_DebugBreakCallHelper(masm, eax.bit() | ecx.bit(), false); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | |
| 135 | void Debug::GenerateStoreICDebugBreak(MacroAssembler* masm) { |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 136 | // Register state for IC store call (from ic-ia32.cc). |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 137 | // ----------- S t a t e ------------- |
| 138 | // -- eax : value |
| 139 | // -- ecx : name |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 140 | // -- edx : receiver |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 141 | // ----------------------------------- |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 142 | Generate_DebugBreakCallHelper(masm, eax.bit() | ecx.bit() | edx.bit(), false); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 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 ------------- |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame^] | 149 | // -- edx : receiver |
| 150 | // -- eax : key |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 151 | // ----------------------------------- |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame^] | 152 | Generate_DebugBreakCallHelper(masm, eax.bit() | edx.bit(), false); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | |
| 156 | void Debug::GenerateKeyedStoreICDebugBreak(MacroAssembler* masm) { |
| 157 | // Register state for keyed IC load call (from ic-ia32.cc). |
| 158 | // ----------- S t a t e ------------- |
| 159 | // -- eax : value |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame^] | 160 | // -- ecx : key |
| 161 | // -- edx : receiver |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 162 | // ----------------------------------- |
| 163 | // Register eax contains an object that needs to be pushed on the |
| 164 | // expression stack of the fake JS frame. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame^] | 165 | Generate_DebugBreakCallHelper(masm, eax.bit() | ecx.bit() | edx.bit(), false); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | |
| 169 | void Debug::GenerateCallICDebugBreak(MacroAssembler* masm) { |
| 170 | // Register state for keyed IC call call (from ic-ia32.cc) |
| 171 | // ----------- S t a t e ------------- |
| 172 | // -- eax: number of arguments |
| 173 | // ----------------------------------- |
| 174 | // The number of arguments in eax is not smi encoded. |
| 175 | Generate_DebugBreakCallHelper(masm, 0, false); |
| 176 | } |
| 177 | |
| 178 | |
| 179 | void Debug::GenerateConstructCallDebugBreak(MacroAssembler* masm) { |
| 180 | // Register state just before return from JS function (from codegen-ia32.cc). |
| 181 | // eax is the actual number of arguments not encoded as a smi see comment |
| 182 | // above IC call. |
| 183 | // ----------- S t a t e ------------- |
| 184 | // -- eax: number of arguments |
| 185 | // ----------------------------------- |
| 186 | // The number of arguments in eax is not smi encoded. |
| 187 | Generate_DebugBreakCallHelper(masm, 0, false); |
| 188 | } |
| 189 | |
| 190 | |
| 191 | void Debug::GenerateReturnDebugBreak(MacroAssembler* masm) { |
| 192 | // Register state just before return from JS function (from codegen-ia32.cc). |
| 193 | // ----------- S t a t e ------------- |
| 194 | // -- eax: return value |
| 195 | // ----------------------------------- |
| 196 | Generate_DebugBreakCallHelper(masm, eax.bit(), true); |
| 197 | } |
| 198 | |
| 199 | |
| 200 | void Debug::GenerateStubNoRegistersDebugBreak(MacroAssembler* masm) { |
| 201 | // Register state for stub CallFunction (from CallFunctionStub in ic-ia32.cc). |
| 202 | // ----------- S t a t e ------------- |
| 203 | // No registers used on entry. |
| 204 | // ----------------------------------- |
| 205 | Generate_DebugBreakCallHelper(masm, 0, false); |
| 206 | } |
| 207 | |
| 208 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame^] | 209 | void Debug::GeneratePlainReturnLiveEdit(MacroAssembler* masm) { |
| 210 | masm->ret(0); |
| 211 | } |
| 212 | |
| 213 | // FrameDropper is a code replacement for a JavaScript frame with possibly |
| 214 | // several frames above. |
| 215 | // There is no calling conventions here, because it never actually gets called, |
| 216 | // it only gets returned to. |
| 217 | // Frame structure (conforms InternalFrame structure): |
| 218 | // -- JSFunction |
| 219 | // -- code |
| 220 | // -- SMI maker |
| 221 | // -- context |
| 222 | // -- frame base |
| 223 | void Debug::GenerateFrameDropperLiveEdit(MacroAssembler* masm) { |
| 224 | // We do not know our frame height, but set esp based on ebp. |
| 225 | __ lea(esp, Operand(ebp, -4 * kPointerSize)); |
| 226 | |
| 227 | __ pop(edi); // function |
| 228 | |
| 229 | // Skip code self-reference and marker. |
| 230 | __ add(Operand(esp), Immediate(2 * kPointerSize)); |
| 231 | |
| 232 | __ pop(esi); // Context. |
| 233 | __ pop(ebp); |
| 234 | |
| 235 | // Get function code. |
| 236 | __ mov(edx, FieldOperand(edi, JSFunction::kSharedFunctionInfoOffset)); |
| 237 | __ mov(edx, FieldOperand(edx, SharedFunctionInfo::kCodeOffset)); |
| 238 | __ lea(edx, FieldOperand(edx, Code::kHeaderSize)); |
| 239 | |
| 240 | // Re-run JSFunction, edi is function, esi is context. |
| 241 | __ jmp(Operand(edx)); |
| 242 | } |
| 243 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 244 | #undef __ |
| 245 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame^] | 246 | |
| 247 | void Debug::SetUpFrameDropperFrame(StackFrame* bottom_js_frame, |
| 248 | Handle<Code> code) { |
| 249 | ASSERT(bottom_js_frame->is_java_script()); |
| 250 | |
| 251 | Address fp = bottom_js_frame->fp(); |
| 252 | Memory::Object_at(fp - 4 * kPointerSize) = |
| 253 | Memory::Object_at(fp - 2 * kPointerSize); // Move edi (function). |
| 254 | |
| 255 | Memory::Object_at(fp - 3 * kPointerSize) = *code; |
| 256 | Memory::Object_at(fp - 2 * kPointerSize) = Smi::FromInt(StackFrame::INTERNAL); |
| 257 | } |
| 258 | const int Debug::kFrameDropperFrameSize = 5; |
| 259 | |
| 260 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 261 | #endif // ENABLE_DEBUGGER_SUPPORT |
| 262 | |
| 263 | } } // namespace v8::internal |