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 
FrameInspector(StandardFrame * frame,int inlined_jsframe_index,Isolate * isolate)12 FrameInspector::FrameInspector(StandardFrame* frame, int inlined_jsframe_index,
13                                Isolate* isolate)
14     : frame_(frame), deoptimized_frame_(NULL), isolate_(isolate) {
15   JavaScriptFrame* js_frame =
16       frame->is_java_script() ? javascript_frame() : nullptr;
17   DCHECK(js_frame || frame->is_wasm());
18   has_adapted_arguments_ = js_frame && js_frame->has_adapted_arguments();
19   is_bottommost_ = inlined_jsframe_index == 0;
20   is_optimized_ = frame_->is_optimized();
21   is_interpreted_ = frame_->is_interpreted();
22   // Calculate the deoptimized frame.
23   if (frame->is_optimized()) {
24     DCHECK(js_frame != nullptr);
25     // TODO(turbofan): Revisit once we support deoptimization.
26     if (js_frame->LookupCode()->is_turbofanned() &&
27         js_frame->function()->shared()->asm_function() &&
28         !FLAG_turbo_asm_deoptimization) {
29       is_optimized_ = false;
30       return;
31     }
32 
33     deoptimized_frame_ = Deoptimizer::DebuggerInspectableFrame(
34         js_frame, inlined_jsframe_index, isolate);
35   }
36 }
37 
~FrameInspector()38 FrameInspector::~FrameInspector() {
39   // Get rid of the calculated deoptimized frame if any.
40   if (deoptimized_frame_ != nullptr) {
41     delete deoptimized_frame_;
42   }
43 }
44 
GetParametersCount()45 int FrameInspector::GetParametersCount() {
46   return is_optimized_ ? deoptimized_frame_->parameters_count()
47                        : frame_->ComputeParametersCount();
48 }
49 
GetScript()50 Handle<Script> FrameInspector::GetScript() {
51   Object* script = is_optimized_
52                        ? deoptimized_frame_->GetFunction()->shared()->script()
53                        : frame_->script();
54   return handle(Script::cast(script), isolate_);
55 }
56 
GetFunction()57 Handle<JSFunction> FrameInspector::GetFunction() {
58   DCHECK(!frame_->is_wasm());
59   return is_optimized_ ? deoptimized_frame_->GetFunction()
60                        : handle(javascript_frame()->function(), isolate_);
61 }
62 
GetParameter(int index)63 Handle<Object> FrameInspector::GetParameter(int index) {
64   return is_optimized_ ? deoptimized_frame_->GetParameter(index)
65                        : handle(frame_->GetParameter(index), isolate_);
66 }
67 
GetExpression(int index)68 Handle<Object> FrameInspector::GetExpression(int index) {
69   // TODO(turbofan): Revisit once we support deoptimization.
70   if (frame_->is_java_script() &&
71       javascript_frame()->LookupCode()->is_turbofanned() &&
72       javascript_frame()->function()->shared()->asm_function() &&
73       !FLAG_turbo_asm_deoptimization) {
74     return isolate_->factory()->undefined_value();
75   }
76   return is_optimized_ ? deoptimized_frame_->GetExpression(index)
77                        : handle(frame_->GetExpression(index), isolate_);
78 }
79 
GetSourcePosition()80 int FrameInspector::GetSourcePosition() {
81   return is_optimized_ ? deoptimized_frame_->GetSourcePosition()
82                        : frame_->position();
83 }
84 
IsConstructor()85 bool FrameInspector::IsConstructor() {
86   return is_optimized_ && !is_bottommost_
87              ? deoptimized_frame_->HasConstructStub()
88              : frame_->IsConstructor();
89 }
90 
GetContext()91 Handle<Object> FrameInspector::GetContext() {
92   return is_optimized_ ? deoptimized_frame_->GetContext()
93                        : handle(frame_->context(), isolate_);
94 }
95 
96 
97 // To inspect all the provided arguments the frame might need to be
98 // replaced with the arguments frame.
SetArgumentsFrame(StandardFrame * frame)99 void FrameInspector::SetArgumentsFrame(StandardFrame* frame) {
100   DCHECK(has_adapted_arguments_);
101   DCHECK(frame->is_arguments_adaptor());
102   frame_ = frame;
103   is_optimized_ = frame_->is_optimized();
104   is_interpreted_ = frame_->is_interpreted();
105   DCHECK(!is_optimized_);
106 }
107 
108 
109 // Create a plain JSObject which materializes the local scope for the specified
110 // frame.
MaterializeStackLocals(Handle<JSObject> target,Handle<ScopeInfo> scope_info)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 (ScopeInfo::VariableIsSynthetic(*name)) continue;
121     if (ParameterIsShadowedByContextLocal(scope_info, name)) continue;
122 
123     Handle<Object> value =
124         i < GetParametersCount()
125             ? GetParameter(i)
126             : Handle<Object>::cast(isolate_->factory()->undefined_value());
127     DCHECK(!value->IsTheHole(isolate_));
128 
129     JSObject::SetOwnPropertyIgnoreAttributes(target, name, value, NONE).Check();
130   }
131 
132   // Second fill all stack locals.
133   for (int i = 0; i < scope_info->StackLocalCount(); ++i) {
134     Handle<String> name(scope_info->StackLocalName(i));
135     if (ScopeInfo::VariableIsSynthetic(*name)) continue;
136     Handle<Object> value = GetExpression(scope_info->StackLocalIndex(i));
137     // TODO(yangguo): We convert optimized out values to {undefined} when they
138     // are passed to the debugger. Eventually we should handle them somehow.
139     if (value->IsTheHole(isolate_)) {
140       value = isolate_->factory()->undefined_value();
141     }
142     if (value->IsOptimizedOut(isolate_)) {
143       value = isolate_->factory()->undefined_value();
144     }
145     JSObject::SetOwnPropertyIgnoreAttributes(target, name, value, NONE).Check();
146   }
147 }
148 
149 
MaterializeStackLocals(Handle<JSObject> target,Handle<JSFunction> function)150 void FrameInspector::MaterializeStackLocals(Handle<JSObject> target,
151                                             Handle<JSFunction> function) {
152   Handle<SharedFunctionInfo> shared(function->shared());
153   Handle<ScopeInfo> scope_info(shared->scope_info());
154   MaterializeStackLocals(target, scope_info);
155 }
156 
157 
UpdateStackLocalsFromMaterializedObject(Handle<JSObject> target,Handle<ScopeInfo> scope_info)158 void FrameInspector::UpdateStackLocalsFromMaterializedObject(
159     Handle<JSObject> target, Handle<ScopeInfo> scope_info) {
160   // Optimized frames and wasm frames are not supported. Simply give up.
161   if (is_optimized_ || frame_->is_wasm()) return;
162 
163   HandleScope scope(isolate_);
164 
165   // Parameters.
166   for (int i = 0; i < scope_info->ParameterCount(); ++i) {
167     // Shadowed parameters were not materialized.
168     Handle<String> name(scope_info->ParameterName(i));
169     if (ScopeInfo::VariableIsSynthetic(*name)) continue;
170     if (ParameterIsShadowedByContextLocal(scope_info, name)) continue;
171 
172     DCHECK(!javascript_frame()->GetParameter(i)->IsTheHole(isolate_));
173     Handle<Object> value =
174         Object::GetPropertyOrElement(target, name).ToHandleChecked();
175     javascript_frame()->SetParameterValue(i, *value);
176   }
177 
178   // Stack locals.
179   for (int i = 0; i < scope_info->StackLocalCount(); ++i) {
180     Handle<String> name(scope_info->StackLocalName(i));
181     if (ScopeInfo::VariableIsSynthetic(*name)) continue;
182     int index = scope_info->StackLocalIndex(i);
183     if (frame_->GetExpression(index)->IsTheHole(isolate_)) continue;
184     Handle<Object> value =
185         Object::GetPropertyOrElement(target, name).ToHandleChecked();
186     frame_->SetExpression(index, *value);
187   }
188 }
189 
190 
ParameterIsShadowedByContextLocal(Handle<ScopeInfo> info,Handle<String> parameter_name)191 bool FrameInspector::ParameterIsShadowedByContextLocal(
192     Handle<ScopeInfo> info, Handle<String> parameter_name) {
193   VariableMode mode;
194   InitializationFlag init_flag;
195   MaybeAssignedFlag maybe_assigned_flag;
196   return ScopeInfo::ContextSlotIndex(info, parameter_name, &mode, &init_flag,
197                                      &maybe_assigned_flag) != -1;
198 }
199 
FindSavedContextForFrame(Isolate * isolate,StandardFrame * frame)200 SaveContext* DebugFrameHelper::FindSavedContextForFrame(Isolate* isolate,
201                                                         StandardFrame* frame) {
202   SaveContext* save = isolate->save_context();
203   while (save != NULL && !save->IsBelowFrame(frame)) {
204     save = save->prev();
205   }
206   DCHECK(save != NULL);
207   return save;
208 }
209 
FindIndexedNonNativeFrame(StackTraceFrameIterator * it,int index)210 int DebugFrameHelper::FindIndexedNonNativeFrame(StackTraceFrameIterator* it,
211                                                 int index) {
212   int count = -1;
213   for (; !it->done(); it->Advance()) {
214     if (it->is_wasm()) {
215       if (++count == index) return 0;
216       continue;
217     }
218     List<FrameSummary> frames(FLAG_max_inlining_levels + 1);
219     it->javascript_frame()->Summarize(&frames);
220     for (int i = frames.length() - 1; i >= 0; i--) {
221       // Omit functions from native and extension scripts.
222       if (!frames[i].function()->shared()->IsSubjectToDebugging()) continue;
223       if (++count == index) return i;
224     }
225   }
226   return -1;
227 }
228 
229 
230 }  // namespace internal
231 }  // namespace v8
232