• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/builtins/accessors.h"
8 #include "src/execution/frames-inl.h"
9 #include "src/wasm/wasm-objects-inl.h"
10 
11 namespace v8 {
12 namespace internal {
13 
FrameInspector(CommonFrame * frame,int inlined_frame_index,Isolate * isolate)14 FrameInspector::FrameInspector(CommonFrame* frame, int inlined_frame_index,
15                                Isolate* isolate)
16     : frame_(frame),
17       inlined_frame_index_(inlined_frame_index),
18       isolate_(isolate) {
19   // Extract the relevant information from the frame summary and discard it.
20   FrameSummary summary = FrameSummary::Get(frame, inlined_frame_index);
21   summary.EnsureSourcePositionsAvailable();
22 
23   is_constructor_ = summary.is_constructor();
24   source_position_ = summary.SourcePosition();
25   function_name_ = summary.FunctionName();
26   script_ = Handle<Script>::cast(summary.script());
27   receiver_ = summary.receiver();
28 
29   if (summary.IsJavaScript()) {
30     function_ = summary.AsJavaScript().function();
31   }
32 
33   JavaScriptFrame* js_frame =
34       frame->is_java_script() ? javascript_frame() : nullptr;
35   DCHECK(js_frame || frame->is_wasm());
36   has_adapted_arguments_ = js_frame && js_frame->has_adapted_arguments();
37   is_optimized_ = frame_->is_optimized();
38   is_interpreted_ = frame_->is_interpreted();
39 
40   // Calculate the deoptimized frame.
41   if (is_optimized_) {
42     DCHECK_NOT_NULL(js_frame);
43     deoptimized_frame_.reset(Deoptimizer::DebuggerInspectableFrame(
44         js_frame, inlined_frame_index, isolate));
45   }
46 }
47 
48 // Destructor needs to be defined in the .cc file, because it instantiates
49 // std::unique_ptr destructors but the types are not known in the header.
50 FrameInspector::~FrameInspector() = default;
51 
javascript_frame()52 JavaScriptFrame* FrameInspector::javascript_frame() {
53   return frame_->is_arguments_adaptor() ? ArgumentsAdaptorFrame::cast(frame_)
54                                         : JavaScriptFrame::cast(frame_);
55 }
56 
GetParameter(int index)57 Handle<Object> FrameInspector::GetParameter(int index) {
58   if (is_optimized_) return deoptimized_frame_->GetParameter(index);
59   DCHECK(IsJavaScript());
60   return handle(javascript_frame()->GetParameter(index), isolate_);
61 }
62 
GetExpression(int index)63 Handle<Object> FrameInspector::GetExpression(int index) {
64   return is_optimized_ ? deoptimized_frame_->GetExpression(index)
65                        : handle(frame_->GetExpression(index), isolate_);
66 }
67 
GetContext()68 Handle<Object> FrameInspector::GetContext() {
69   return deoptimized_frame_ ? deoptimized_frame_->GetContext()
70                             : handle(frame_->context(), isolate_);
71 }
72 
IsWasm()73 bool FrameInspector::IsWasm() { return frame_->is_wasm(); }
74 
IsJavaScript()75 bool FrameInspector::IsJavaScript() { return frame_->is_java_script(); }
76 
ParameterIsShadowedByContextLocal(Handle<ScopeInfo> info,Handle<String> parameter_name)77 bool FrameInspector::ParameterIsShadowedByContextLocal(
78     Handle<ScopeInfo> info, Handle<String> parameter_name) {
79   VariableMode mode;
80   InitializationFlag init_flag;
81   MaybeAssignedFlag maybe_assigned_flag;
82   IsStaticFlag is_static_flag;
83   return ScopeInfo::ContextSlotIndex(*info, *parameter_name, &mode, &init_flag,
84                                      &maybe_assigned_flag,
85                                      &is_static_flag) != -1;
86 }
87 
RedirectActiveFunctions(SharedFunctionInfo shared,Mode mode)88 RedirectActiveFunctions::RedirectActiveFunctions(SharedFunctionInfo shared,
89                                                  Mode mode)
90     : shared_(shared), mode_(mode) {
91   DCHECK(shared.HasBytecodeArray());
92   if (mode == Mode::kUseDebugBytecode) {
93     DCHECK(shared.HasDebugInfo());
94   }
95 }
96 
VisitThread(Isolate * isolate,ThreadLocalTop * top)97 void RedirectActiveFunctions::VisitThread(Isolate* isolate,
98                                           ThreadLocalTop* top) {
99   for (JavaScriptFrameIterator it(isolate, top); !it.done(); it.Advance()) {
100     JavaScriptFrame* frame = it.frame();
101     JSFunction function = frame->function();
102     if (!frame->is_interpreted()) continue;
103     if (function.shared() != shared_) continue;
104     InterpretedFrame* interpreted_frame =
105         reinterpret_cast<InterpretedFrame*>(frame);
106     BytecodeArray bytecode = mode_ == Mode::kUseDebugBytecode
107                                  ? shared_.GetDebugInfo().DebugBytecodeArray()
108                                  : shared_.GetBytecodeArray();
109     interpreted_frame->PatchBytecodeArray(bytecode);
110   }
111 }
112 
113 }  // namespace internal
114 }  // namespace v8
115