Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1 | // Copyright 2015 the V8 project authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "src/debug/debug-frames.h" |
| 6 | |
| 7 | #include "src/frames-inl.h" |
| 8 | |
| 9 | namespace v8 { |
| 10 | namespace internal { |
| 11 | |
| 12 | FrameInspector::FrameInspector(JavaScriptFrame* frame, |
| 13 | int inlined_jsframe_index, Isolate* isolate) |
| 14 | : frame_(frame), deoptimized_frame_(NULL), isolate_(isolate) { |
| 15 | has_adapted_arguments_ = frame_->has_adapted_arguments(); |
| 16 | is_bottommost_ = inlined_jsframe_index == 0; |
| 17 | is_optimized_ = frame_->is_optimized(); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 18 | is_interpreted_ = frame_->is_interpreted(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 19 | // Calculate the deoptimized frame. |
| 20 | if (frame->is_optimized()) { |
| 21 | // TODO(turbofan): Revisit once we support deoptimization. |
| 22 | if (frame->LookupCode()->is_turbofanned() && |
| 23 | frame->function()->shared()->asm_function() && |
| 24 | !FLAG_turbo_asm_deoptimization) { |
| 25 | is_optimized_ = false; |
| 26 | return; |
| 27 | } |
| 28 | |
| 29 | deoptimized_frame_ = Deoptimizer::DebuggerInspectableFrame( |
| 30 | frame, inlined_jsframe_index, isolate); |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | |
| 35 | FrameInspector::~FrameInspector() { |
| 36 | // Get rid of the calculated deoptimized frame if any. |
| 37 | if (deoptimized_frame_ != NULL) { |
| 38 | Deoptimizer::DeleteDebuggerInspectableFrame(deoptimized_frame_, isolate_); |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | |
| 43 | int FrameInspector::GetParametersCount() { |
| 44 | return is_optimized_ ? deoptimized_frame_->parameters_count() |
| 45 | : frame_->ComputeParametersCount(); |
| 46 | } |
| 47 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 48 | Handle<Object> FrameInspector::GetFunction() { |
| 49 | return is_optimized_ ? deoptimized_frame_->GetFunction() |
| 50 | : handle(frame_->function(), isolate_); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 51 | } |
| 52 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 53 | Handle<Object> FrameInspector::GetParameter(int index) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 54 | return is_optimized_ ? deoptimized_frame_->GetParameter(index) |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 55 | : handle(frame_->GetParameter(index), isolate_); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 56 | } |
| 57 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 58 | Handle<Object> FrameInspector::GetExpression(int index) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 59 | // TODO(turbofan): Revisit once we support deoptimization. |
| 60 | if (frame_->LookupCode()->is_turbofanned() && |
| 61 | frame_->function()->shared()->asm_function() && |
| 62 | !FLAG_turbo_asm_deoptimization) { |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 63 | return isolate_->factory()->undefined_value(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 64 | } |
| 65 | return is_optimized_ ? deoptimized_frame_->GetExpression(index) |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 66 | : handle(frame_->GetExpression(index), isolate_); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | |
| 70 | int FrameInspector::GetSourcePosition() { |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 71 | if (is_optimized_) { |
| 72 | return deoptimized_frame_->GetSourcePosition(); |
| 73 | } else if (is_interpreted_) { |
| 74 | InterpretedFrame* frame = reinterpret_cast<InterpretedFrame*>(frame_); |
| 75 | BytecodeArray* bytecode_array = |
| 76 | frame->function()->shared()->bytecode_array(); |
| 77 | return bytecode_array->SourcePosition(frame->GetBytecodeOffset()); |
| 78 | } else { |
| 79 | Code* code = frame_->LookupCode(); |
| 80 | int offset = static_cast<int>(frame_->pc() - code->instruction_start()); |
| 81 | return code->SourcePosition(offset); |
| 82 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | |
| 86 | bool FrameInspector::IsConstructor() { |
| 87 | return is_optimized_ && !is_bottommost_ |
| 88 | ? deoptimized_frame_->HasConstructStub() |
| 89 | : frame_->IsConstructor(); |
| 90 | } |
| 91 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 92 | Handle<Object> FrameInspector::GetContext() { |
| 93 | return is_optimized_ ? deoptimized_frame_->GetContext() |
| 94 | : handle(frame_->context(), isolate_); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | |
| 98 | // To inspect all the provided arguments the frame might need to be |
| 99 | // replaced with the arguments frame. |
| 100 | void FrameInspector::SetArgumentsFrame(JavaScriptFrame* frame) { |
| 101 | DCHECK(has_adapted_arguments_); |
| 102 | frame_ = frame; |
| 103 | is_optimized_ = frame_->is_optimized(); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 104 | is_interpreted_ = frame_->is_interpreted(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 105 | DCHECK(!is_optimized_); |
| 106 | } |
| 107 | |
| 108 | |
| 109 | // Create a plain JSObject which materializes the local scope for the specified |
| 110 | // frame. |
| 111 | void FrameInspector::MaterializeStackLocals(Handle<JSObject> target, |
| 112 | Handle<ScopeInfo> scope_info) { |
| 113 | HandleScope scope(isolate_); |
| 114 | // First fill all parameters. |
| 115 | for (int i = 0; i < scope_info->ParameterCount(); ++i) { |
| 116 | // Do not materialize the parameter if it is shadowed by a context local. |
| 117 | // TODO(yangguo): check whether this is necessary, now that we materialize |
| 118 | // context locals as well. |
| 119 | Handle<String> name(scope_info->ParameterName(i)); |
| 120 | if (ParameterIsShadowedByContextLocal(scope_info, name)) continue; |
| 121 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 122 | Handle<Object> value = |
| 123 | i < GetParametersCount() |
| 124 | ? GetParameter(i) |
| 125 | : Handle<Object>::cast(isolate_->factory()->undefined_value()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 126 | DCHECK(!value->IsTheHole()); |
| 127 | |
| 128 | JSObject::SetOwnPropertyIgnoreAttributes(target, name, value, NONE).Check(); |
| 129 | } |
| 130 | |
| 131 | // Second fill all stack locals. |
| 132 | for (int i = 0; i < scope_info->StackLocalCount(); ++i) { |
| 133 | if (scope_info->LocalIsSynthetic(i)) continue; |
| 134 | Handle<String> name(scope_info->StackLocalName(i)); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 135 | Handle<Object> value = GetExpression(scope_info->StackLocalIndex(i)); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 136 | // TODO(yangguo): We convert optimized out values to {undefined} when they |
| 137 | // are passed to the debugger. Eventually we should handle them somehow. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 138 | if (value->IsTheHole()) value = isolate_->factory()->undefined_value(); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 139 | if (value->IsOptimizedOut()) value = isolate_->factory()->undefined_value(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 140 | JSObject::SetOwnPropertyIgnoreAttributes(target, name, value, NONE).Check(); |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | |
| 145 | void FrameInspector::MaterializeStackLocals(Handle<JSObject> target, |
| 146 | Handle<JSFunction> function) { |
| 147 | Handle<SharedFunctionInfo> shared(function->shared()); |
| 148 | Handle<ScopeInfo> scope_info(shared->scope_info()); |
| 149 | MaterializeStackLocals(target, scope_info); |
| 150 | } |
| 151 | |
| 152 | |
| 153 | void FrameInspector::UpdateStackLocalsFromMaterializedObject( |
| 154 | Handle<JSObject> target, Handle<ScopeInfo> scope_info) { |
| 155 | if (is_optimized_) { |
| 156 | // Optimized frames are not supported. Simply give up. |
| 157 | return; |
| 158 | } |
| 159 | |
| 160 | HandleScope scope(isolate_); |
| 161 | |
| 162 | // Parameters. |
| 163 | for (int i = 0; i < scope_info->ParameterCount(); ++i) { |
| 164 | // Shadowed parameters were not materialized. |
| 165 | Handle<String> name(scope_info->ParameterName(i)); |
| 166 | if (ParameterIsShadowedByContextLocal(scope_info, name)) continue; |
| 167 | |
| 168 | DCHECK(!frame_->GetParameter(i)->IsTheHole()); |
| 169 | Handle<Object> value = |
| 170 | Object::GetPropertyOrElement(target, name).ToHandleChecked(); |
| 171 | frame_->SetParameterValue(i, *value); |
| 172 | } |
| 173 | |
| 174 | // Stack locals. |
| 175 | for (int i = 0; i < scope_info->StackLocalCount(); ++i) { |
| 176 | if (scope_info->LocalIsSynthetic(i)) continue; |
| 177 | int index = scope_info->StackLocalIndex(i); |
| 178 | if (frame_->GetExpression(index)->IsTheHole()) continue; |
| 179 | Handle<Object> value = |
| 180 | Object::GetPropertyOrElement( |
| 181 | target, handle(scope_info->StackLocalName(i), isolate_)) |
| 182 | .ToHandleChecked(); |
| 183 | frame_->SetExpression(index, *value); |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | |
| 188 | bool FrameInspector::ParameterIsShadowedByContextLocal( |
| 189 | Handle<ScopeInfo> info, Handle<String> parameter_name) { |
| 190 | VariableMode mode; |
| 191 | InitializationFlag init_flag; |
| 192 | MaybeAssignedFlag maybe_assigned_flag; |
| 193 | return ScopeInfo::ContextSlotIndex(info, parameter_name, &mode, &init_flag, |
| 194 | &maybe_assigned_flag) != -1; |
| 195 | } |
| 196 | |
| 197 | |
| 198 | SaveContext* DebugFrameHelper::FindSavedContextForFrame( |
| 199 | Isolate* isolate, JavaScriptFrame* frame) { |
| 200 | SaveContext* save = isolate->save_context(); |
| 201 | while (save != NULL && !save->IsBelowFrame(frame)) { |
| 202 | save = save->prev(); |
| 203 | } |
| 204 | DCHECK(save != NULL); |
| 205 | return save; |
| 206 | } |
| 207 | |
| 208 | |
| 209 | int DebugFrameHelper::FindIndexedNonNativeFrame(JavaScriptFrameIterator* it, |
| 210 | int index) { |
| 211 | int count = -1; |
| 212 | for (; !it->done(); it->Advance()) { |
| 213 | List<FrameSummary> frames(FLAG_max_inlining_levels + 1); |
| 214 | it->frame()->Summarize(&frames); |
| 215 | for (int i = frames.length() - 1; i >= 0; i--) { |
| 216 | // Omit functions from native and extension scripts. |
| 217 | if (!frames[i].function()->shared()->IsSubjectToDebugging()) continue; |
| 218 | if (++count == index) return i; |
| 219 | } |
| 220 | } |
| 221 | return -1; |
| 222 | } |
| 223 | |
| 224 | |
| 225 | } // namespace internal |
| 226 | } // namespace v8 |