1 // Copyright 2016 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 #ifndef V8_INSPECTOR_V8DEBUGGER_H_
6 #define V8_INSPECTOR_V8DEBUGGER_H_
7 
8 #include <vector>
9 
10 #include "src/base/macros.h"
11 #include "src/debug/debug-interface.h"
12 #include "src/inspector/java-script-call-frame.h"
13 #include "src/inspector/protocol/Forward.h"
14 #include "src/inspector/protocol/Runtime.h"
15 #include "src/inspector/v8-debugger-script.h"
16 
17 #include "include/v8-inspector.h"
18 
19 namespace v8_inspector {
20 
21 struct ScriptBreakpoint;
22 class V8DebuggerAgentImpl;
23 class V8InspectorImpl;
24 class V8StackTraceImpl;
25 
26 using protocol::Response;
27 
28 class V8Debugger {
29  public:
30   V8Debugger(v8::Isolate*, V8InspectorImpl*);
31   ~V8Debugger();
32 
33   static int contextId(v8::Local<v8::Context>);
34   static int getGroupId(v8::Local<v8::Context>);
35   int markContext(const V8ContextInfo&);
36 
37   bool enabled() const;
38 
39   String16 setBreakpoint(const String16& sourceID, const ScriptBreakpoint&,
40                          int* actualLineNumber, int* actualColumnNumber);
41   void removeBreakpoint(const String16& breakpointId);
42   void setBreakpointsActivated(bool);
breakpointsActivated()43   bool breakpointsActivated() const { return m_breakpointsActivated; }
44 
45   v8::DebugInterface::ExceptionBreakState getPauseOnExceptionsState();
46   void setPauseOnExceptionsState(v8::DebugInterface::ExceptionBreakState);
47   void setPauseOnNextStatement(bool);
48   bool canBreakProgram();
49   void breakProgram();
50   void continueProgram();
51   void stepIntoStatement();
52   void stepOverStatement();
53   void stepOutOfFunction();
54   void clearStepping();
55 
56   Response setScriptSource(
57       const String16& sourceID, v8::Local<v8::String> newSource, bool dryRun,
58       protocol::Maybe<protocol::Runtime::ExceptionDetails>*,
59       JavaScriptCallFrames* newCallFrames, protocol::Maybe<bool>* stackChanged,
60       bool* compileError);
61   JavaScriptCallFrames currentCallFrames(int limit = 0);
62 
63   // Each script inherits debug data from v8::Context where it has been
64   // compiled.
65   // Only scripts whose debug data matches |contextGroupId| will be reported.
66   // Passing 0 will result in reporting all scripts.
67   void getCompiledScripts(int contextGroupId,
68                           std::vector<std::unique_ptr<V8DebuggerScript>>&);
69   void enable();
70   void disable();
71 
72   bool isPaused();
pausedContext()73   v8::Local<v8::Context> pausedContext() { return m_pausedContext; }
74 
maxAsyncCallChainDepth()75   int maxAsyncCallChainDepth() { return m_maxAsyncCallStackDepth; }
76   V8StackTraceImpl* currentAsyncCallChain();
77   void setAsyncCallStackDepth(V8DebuggerAgentImpl*, int);
78   std::unique_ptr<V8StackTraceImpl> createStackTrace(v8::Local<v8::StackTrace>);
79   std::unique_ptr<V8StackTraceImpl> captureStackTrace(bool fullStack);
80 
81   v8::MaybeLocal<v8::Array> internalProperties(v8::Local<v8::Context>,
82                                                v8::Local<v8::Value>);
83 
84   void asyncTaskScheduled(const StringView& taskName, void* task,
85                           bool recurring);
86   void asyncTaskScheduled(const String16& taskName, void* task, bool recurring);
87   void asyncTaskCanceled(void* task);
88   void asyncTaskStarted(void* task);
89   void asyncTaskFinished(void* task);
90   void allAsyncTasksCanceled();
91 
92   void muteScriptParsedEvents();
93   void unmuteScriptParsedEvents();
94 
inspector()95   V8InspectorImpl* inspector() { return m_inspector; }
96 
97  private:
98   void compileDebuggerScript();
99   v8::MaybeLocal<v8::Value> callDebuggerMethod(const char* functionName,
100                                                int argc,
101                                                v8::Local<v8::Value> argv[]);
102   v8::Local<v8::Context> debuggerContext() const;
103   void clearBreakpoints();
104 
105   static void breakProgramCallback(const v8::FunctionCallbackInfo<v8::Value>&);
106   void handleProgramBreak(v8::Local<v8::Context> pausedContext,
107                           v8::Local<v8::Object> executionState,
108                           v8::Local<v8::Value> exception,
109                           v8::Local<v8::Array> hitBreakpoints,
110                           bool isPromiseRejection = false,
111                           bool isUncaught = false);
112   static void v8DebugEventCallback(const v8::DebugInterface::EventDetails&);
113   v8::Local<v8::Value> callInternalGetterFunction(v8::Local<v8::Object>,
114                                                   const char* functionName);
115   void handleV8DebugEvent(const v8::DebugInterface::EventDetails&);
116   void handleV8AsyncTaskEvent(v8::Local<v8::Context>,
117                               v8::Local<v8::Object> executionState,
118                               v8::Local<v8::Object> eventData);
119 
120   v8::Local<v8::Value> collectionEntries(v8::Local<v8::Context>,
121                                          v8::Local<v8::Object>);
122   v8::Local<v8::Value> generatorObjectLocation(v8::Local<v8::Context>,
123                                                v8::Local<v8::Object>);
124   v8::Local<v8::Value> functionLocation(v8::Local<v8::Context>,
125                                         v8::Local<v8::Function>);
126   v8::MaybeLocal<v8::Value> functionScopes(v8::Local<v8::Context>,
127                                            v8::Local<v8::Function>);
128 
129   v8::Isolate* m_isolate;
130   V8InspectorImpl* m_inspector;
131   int m_lastContextId;
132   int m_enableCount;
133   bool m_breakpointsActivated;
134   v8::Global<v8::Object> m_debuggerScript;
135   v8::Global<v8::Context> m_debuggerContext;
136   v8::Local<v8::Object> m_executionState;
137   v8::Local<v8::Context> m_pausedContext;
138   bool m_runningNestedMessageLoop;
139   int m_ignoreScriptParsedEventsCounter;
140 
141   using AsyncTaskToStackTrace =
142       protocol::HashMap<void*, std::unique_ptr<V8StackTraceImpl>>;
143   AsyncTaskToStackTrace m_asyncTaskStacks;
144   protocol::HashSet<void*> m_recurringTasks;
145   int m_maxAsyncCallStackDepth;
146   std::vector<void*> m_currentTasks;
147   std::vector<std::unique_ptr<V8StackTraceImpl>> m_currentStacks;
148   protocol::HashMap<V8DebuggerAgentImpl*, int> m_maxAsyncCallStackDepthMap;
149 
150   v8::DebugInterface::ExceptionBreakState m_pauseOnExceptionsState;
151 
152   DISALLOW_COPY_AND_ASSIGN(V8Debugger);
153 };
154 
155 }  // namespace v8_inspector
156 
157 #endif  // V8_INSPECTOR_V8DEBUGGER_H_
158