Skip to content

Commit 8bb2cef

Browse files
alexkozyCommit bot
authored andcommitted
[inspector] introduce debug-interface.h
debug-interface.h contains part of v8-debug.h that is used by src/inspector. BUG=v8:5510 R=dgozman@chromium.org, yangguo@chromium.org Review-Url: https://codereview.chromium.org/2423713003 Cr-Commit-Position: refs/heads/master@{#40404}
1 parent 7bbfe5c commit 8bb2cef

11 files changed

Lines changed: 197 additions & 29 deletions

File tree

BUILD.gn

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1287,6 +1287,7 @@ v8_source_set("v8_base") {
12871287
"src/debug/debug-evaluate.h",
12881288
"src/debug/debug-frames.cc",
12891289
"src/debug/debug-frames.h",
1290+
"src/debug/debug-interface.h",
12901291
"src/debug/debug-scopes.cc",
12911292
"src/debug/debug-scopes.h",
12921293
"src/debug/debug.cc",

src/api.cc

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8764,6 +8764,46 @@ MaybeLocal<Array> Debug::GetInternalProperties(Isolate* v8_isolate,
87648764
return Utils::ToLocal(result);
87658765
}
87668766

8767+
bool DebugInterface::SetDebugEventListener(Isolate* isolate,
8768+
DebugInterface::EventCallback that,
8769+
Local<Value> data) {
8770+
i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
8771+
ENTER_V8(i_isolate);
8772+
i::HandleScope scope(i_isolate);
8773+
i::Handle<i::Object> foreign = i_isolate->factory()->undefined_value();
8774+
if (that != NULL) {
8775+
foreign = i_isolate->factory()->NewForeign(FUNCTION_ADDR(that));
8776+
}
8777+
i_isolate->debug()->SetEventListener(foreign, Utils::OpenHandle(*data, true));
8778+
return true;
8779+
}
8780+
8781+
Local<Context> DebugInterface::GetDebugContext(Isolate* isolate) {
8782+
return Debug::GetDebugContext(isolate);
8783+
}
8784+
8785+
MaybeLocal<Value> DebugInterface::Call(Local<Context> context,
8786+
v8::Local<v8::Function> fun,
8787+
v8::Local<v8::Value> data) {
8788+
return Debug::Call(context, fun, data);
8789+
}
8790+
8791+
void DebugInterface::SetLiveEditEnabled(Isolate* isolate, bool enable) {
8792+
Debug::SetLiveEditEnabled(isolate, enable);
8793+
}
8794+
8795+
void DebugInterface::DebugBreak(Isolate* isolate) {
8796+
Debug::DebugBreak(isolate);
8797+
}
8798+
8799+
void DebugInterface::CancelDebugBreak(Isolate* isolate) {
8800+
Debug::CancelDebugBreak(isolate);
8801+
}
8802+
8803+
MaybeLocal<Array> DebugInterface::GetInternalProperties(Isolate* isolate,
8804+
Local<Value> value) {
8805+
return Debug::GetInternalProperties(isolate, value);
8806+
}
87678807

87688808
Local<String> CpuProfileNode::GetFunctionName() const {
87698809
const i::ProfileNode* node = reinterpret_cast<const i::ProfileNode*>(this);

src/debug/debug-interface.h

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
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_DEBUG_DEBUG_INTERFACE_H_
6+
#define V8_DEBUG_DEBUG_INTERFACE_H_
7+
8+
#include "include/v8-debug.h"
9+
#include "include/v8.h"
10+
11+
namespace v8 {
12+
13+
class DebugInterface {
14+
public:
15+
/**
16+
* An event details object passed to the debug event listener.
17+
*/
18+
class EventDetails : public v8::Debug::EventDetails {
19+
public:
20+
/**
21+
* Event type.
22+
*/
23+
virtual v8::DebugEvent GetEvent() const = 0;
24+
25+
/**
26+
* Access to execution state and event data of the debug event. Don't store
27+
* these cross callbacks as their content becomes invalid.
28+
*/
29+
virtual Local<Object> GetExecutionState() const = 0;
30+
virtual Local<Object> GetEventData() const = 0;
31+
32+
/**
33+
* Get the context active when the debug event happened. Note this is not
34+
* the current active context as the JavaScript part of the debugger is
35+
* running in its own context which is entered at this point.
36+
*/
37+
virtual Local<Context> GetEventContext() const = 0;
38+
39+
/**
40+
* Client data passed with the corresponding callback when it was
41+
* registered.
42+
*/
43+
virtual Local<Value> GetCallbackData() const = 0;
44+
45+
virtual ~EventDetails() {}
46+
};
47+
48+
/**
49+
* Debug event callback function.
50+
*
51+
* \param event_details object providing information about the debug event
52+
*
53+
* A EventCallback does not take possession of the event data,
54+
* and must not rely on the data persisting after the handler returns.
55+
*/
56+
typedef void (*EventCallback)(const EventDetails& event_details);
57+
58+
static bool SetDebugEventListener(Isolate* isolate, EventCallback that,
59+
Local<Value> data = Local<Value>());
60+
61+
/**
62+
* Debugger is running in its own context which is entered while debugger
63+
* messages are being dispatched. This is an explicit getter for this
64+
* debugger context. Note that the content of the debugger context is subject
65+
* to change. The Context exists only when the debugger is active, i.e. at
66+
* least one DebugEventListener or MessageHandler is set.
67+
*/
68+
static Local<Context> GetDebugContext(Isolate* isolate);
69+
70+
/**
71+
* Run a JavaScript function in the debugger.
72+
* \param fun the function to call
73+
* \param data passed as second argument to the function
74+
* With this call the debugger is entered and the function specified is called
75+
* with the execution state as the first argument. This makes it possible to
76+
* get access to information otherwise not available during normal JavaScript
77+
* execution e.g. details on stack frames. Receiver of the function call will
78+
* be the debugger context global object, however this is a subject to change.
79+
* The following example shows a JavaScript function which when passed to
80+
* v8::Debug::Call will return the current line of JavaScript execution.
81+
*
82+
* \code
83+
* function frame_source_line(exec_state) {
84+
* return exec_state.frame(0).sourceLine();
85+
* }
86+
* \endcode
87+
*/
88+
// TODO(dcarney): data arg should be a MaybeLocal
89+
static MaybeLocal<Value> Call(Local<Context> context,
90+
v8::Local<v8::Function> fun,
91+
Local<Value> data = Local<Value>());
92+
93+
/**
94+
* Enable/disable LiveEdit functionality for the given Isolate
95+
* (default Isolate if not provided). V8 will abort if LiveEdit is
96+
* unexpectedly used. LiveEdit is enabled by default.
97+
*/
98+
static void SetLiveEditEnabled(Isolate* isolate, bool enable);
99+
100+
// Schedule a debugger break to happen when JavaScript code is run
101+
// in the given isolate.
102+
static void DebugBreak(Isolate* isolate);
103+
104+
// Remove scheduled debugger break in given isolate if it has not
105+
// happened yet.
106+
static void CancelDebugBreak(Isolate* isolate);
107+
108+
/**
109+
* Returns array of internal properties specific to the value type. Result has
110+
* the following format: [<name>, <value>,...,<name>, <value>]. Result array
111+
* will be allocated in the current context.
112+
*/
113+
static MaybeLocal<Array> GetInternalProperties(Isolate* isolate,
114+
Local<Value> value);
115+
};
116+
117+
} // namespace v8
118+
119+
#endif // V8_DEBUG_DEBUG_INTERFACE_H_

src/debug/debug.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1841,8 +1841,8 @@ void Debug::CallEventCallback(v8::DebugEvent event,
18411841
in_debug_event_listener_ = true;
18421842
if (event_listener_->IsForeign()) {
18431843
// Invoke the C debug event listener.
1844-
v8::Debug::EventCallback callback =
1845-
FUNCTION_CAST<v8::Debug::EventCallback>(
1844+
v8::DebugInterface::EventCallback callback =
1845+
FUNCTION_CAST<v8::DebugInterface::EventCallback>(
18461846
Handle<Foreign>::cast(event_listener_)->foreign_address());
18471847
EventDetailsImpl event_details(event,
18481848
Handle<JSObject>::cast(exec_state),

src/debug/debug.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "src/base/atomicops.h"
1212
#include "src/base/hashmap.h"
1313
#include "src/base/platform/platform.h"
14+
#include "src/debug/debug-interface.h"
1415
#include "src/execution.h"
1516
#include "src/factory.h"
1617
#include "src/flags.h"
@@ -290,7 +291,7 @@ class MessageImpl: public v8::Debug::Message {
290291

291292

292293
// Details of the debug event delivered to the debug event listener.
293-
class EventDetailsImpl : public v8::Debug::EventDetails {
294+
class EventDetailsImpl : public v8::DebugInterface::EventDetails {
294295
public:
295296
EventDetailsImpl(DebugEvent event,
296297
Handle<JSObject> exec_state,

src/inspector/DEPS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,6 @@ include_rules = [
66
"+src/base/platform/platform.h",
77
"+src/inspector",
88
"+src/tracing",
9+
"-include/v8-debug.h",
10+
"+src/debug/debug-interface.h",
911
]

src/inspector/java-script-call-frame.cc

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,9 @@
3030

3131
#include "src/inspector/java-script-call-frame.h"
3232

33+
#include "src/debug/debug-interface.h"
3334
#include "src/inspector/string-util.h"
3435

35-
#include "include/v8-debug.h"
36-
3736
namespace v8_inspector {
3837

3938
JavaScriptCallFrame::JavaScriptCallFrame(v8::Local<v8::Context> debuggerContext,
@@ -130,10 +129,10 @@ v8::MaybeLocal<v8::Value> JavaScriptCallFrame::restart() {
130129
v8::Local<v8::Function> restartFunction = v8::Local<v8::Function>::Cast(
131130
callFrame->Get(context, toV8StringInternalized(m_isolate, "restart"))
132131
.ToLocalChecked());
133-
v8::Debug::SetLiveEditEnabled(m_isolate, true);
132+
v8::DebugInterface::SetLiveEditEnabled(m_isolate, true);
134133
v8::MaybeLocal<v8::Value> result = restartFunction->Call(
135134
m_debuggerContext.Get(m_isolate), callFrame, 0, nullptr);
136-
v8::Debug::SetLiveEditEnabled(m_isolate, false);
135+
v8::DebugInterface::SetLiveEditEnabled(m_isolate, false);
137136
return result;
138137
}
139138

src/inspector/v8-debugger-agent-impl.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
#include <algorithm>
88

9+
#include "src/debug/debug-interface.h"
910
#include "src/inspector/injected-script.h"
1011
#include "src/inspector/inspected-context.h"
1112
#include "src/inspector/java-script-call-frame.h"
@@ -914,7 +915,7 @@ std::unique_ptr<Array<CallFrame>> V8DebuggerAgentImpl::currentCallFrames(
914915
ErrorString ignored;
915916
v8::HandleScope handles(m_isolate);
916917
v8::Local<v8::Context> debuggerContext =
917-
v8::Debug::GetDebugContext(m_isolate);
918+
v8::DebugInterface::GetDebugContext(m_isolate);
918919
v8::Context::Scope contextScope(debuggerContext);
919920

920921
v8::Local<v8::Array> objects = v8::Array::New(m_isolate);

src/inspector/v8-debugger.cc

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,11 @@ void V8Debugger::enable() {
6363
if (m_enableCount++) return;
6464
DCHECK(!enabled());
6565
v8::HandleScope scope(m_isolate);
66-
v8::Debug::SetDebugEventListener(m_isolate, &V8Debugger::v8DebugEventCallback,
67-
v8::External::New(m_isolate, this));
68-
m_debuggerContext.Reset(m_isolate, v8::Debug::GetDebugContext(m_isolate));
66+
v8::DebugInterface::SetDebugEventListener(m_isolate,
67+
&V8Debugger::v8DebugEventCallback,
68+
v8::External::New(m_isolate, this));
69+
m_debuggerContext.Reset(m_isolate,
70+
v8::DebugInterface::GetDebugContext(m_isolate));
6971
compileDebuggerScript();
7072
}
7173

@@ -76,7 +78,7 @@ void V8Debugger::disable() {
7678
m_debuggerScript.Reset();
7779
m_debuggerContext.Reset();
7880
allAsyncTasksCanceled();
79-
v8::Debug::SetDebugEventListener(m_isolate, nullptr);
81+
v8::DebugInterface::SetDebugEventListener(m_isolate, nullptr);
8082
}
8183

8284
bool V8Debugger::enabled() const { return !m_debuggerScript.IsEmpty(); }
@@ -171,7 +173,7 @@ String16 V8Debugger::setBreakpoint(const String16& sourceID,
171173
->Get(context, toV8StringInternalized(m_isolate, "setBreakpoint"))
172174
.ToLocalChecked());
173175
v8::Local<v8::Value> breakpointId =
174-
v8::Debug::Call(debuggerContext(), setBreakpointFunction, info)
176+
v8::DebugInterface::Call(debuggerContext(), setBreakpointFunction, info)
175177
.ToLocalChecked();
176178
if (!breakpointId->IsString()) return "";
177179
*actualLineNumber =
@@ -206,7 +208,7 @@ void V8Debugger::removeBreakpoint(const String16& breakpointId) {
206208
->Get(context,
207209
toV8StringInternalized(m_isolate, "removeBreakpoint"))
208210
.ToLocalChecked());
209-
v8::Debug::Call(debuggerContext(), removeBreakpointFunction, info)
211+
v8::DebugInterface::Call(debuggerContext(), removeBreakpointFunction, info)
210212
.ToLocalChecked();
211213
}
212214

@@ -219,7 +221,8 @@ void V8Debugger::clearBreakpoints() {
219221
m_debuggerScript.Get(m_isolate)
220222
->Get(context, toV8StringInternalized(m_isolate, "clearBreakpoints"))
221223
.ToLocalChecked());
222-
v8::Debug::Call(debuggerContext(), clearBreakpoints).ToLocalChecked();
224+
v8::DebugInterface::Call(debuggerContext(), clearBreakpoints)
225+
.ToLocalChecked();
223226
}
224227

225228
void V8Debugger::setBreakpointsActivated(bool activated) {
@@ -243,7 +246,7 @@ void V8Debugger::setBreakpointsActivated(bool activated) {
243246
->Get(context, toV8StringInternalized(m_isolate,
244247
"setBreakpointsActivated"))
245248
.ToLocalChecked());
246-
v8::Debug::Call(debuggerContext(), setBreakpointsActivated, info)
249+
v8::DebugInterface::Call(debuggerContext(), setBreakpointsActivated, info)
247250
.ToLocalChecked();
248251

249252
m_breakpointsActivated = activated;
@@ -276,9 +279,9 @@ void V8Debugger::setPauseOnExceptionsState(
276279
void V8Debugger::setPauseOnNextStatement(bool pause) {
277280
if (m_runningNestedMessageLoop) return;
278281
if (pause)
279-
v8::Debug::DebugBreak(m_isolate);
282+
v8::DebugInterface::DebugBreak(m_isolate);
280283
else
281-
v8::Debug::CancelDebugBreak(m_isolate);
284+
v8::DebugInterface::CancelDebugBreak(m_isolate);
282285
}
283286

284287
bool V8Debugger::canBreakProgram() {
@@ -306,7 +309,7 @@ void V8Debugger::breakProgram() {
306309
v8::ConstructorBehavior::kThrow)
307310
.ToLocal(&breakFunction))
308311
return;
309-
v8::Debug::Call(debuggerContext(), breakFunction).ToLocalChecked();
312+
v8::DebugInterface::Call(debuggerContext(), breakFunction).ToLocalChecked();
310313
}
311314

312315
void V8Debugger::continueProgram() {
@@ -359,11 +362,11 @@ bool V8Debugger::setScriptSource(
359362
class EnableLiveEditScope {
360363
public:
361364
explicit EnableLiveEditScope(v8::Isolate* isolate) : m_isolate(isolate) {
362-
v8::Debug::SetLiveEditEnabled(m_isolate, true);
365+
v8::DebugInterface::SetLiveEditEnabled(m_isolate, true);
363366
inLiveEditScope = true;
364367
}
365368
~EnableLiveEditScope() {
366-
v8::Debug::SetLiveEditEnabled(m_isolate, false);
369+
v8::DebugInterface::SetLiveEditEnabled(m_isolate, false);
367370
inLiveEditScope = false;
368371
}
369372

@@ -459,8 +462,8 @@ JavaScriptCallFrames V8Debugger::currentCallFrames(int limit) {
459462
toV8StringInternalized(m_isolate, "currentCallFrames"))
460463
.ToLocalChecked());
461464
currentCallFramesV8 =
462-
v8::Debug::Call(debuggerContext(), currentCallFramesFunction,
463-
v8::Integer::New(m_isolate, limit))
465+
v8::DebugInterface::Call(debuggerContext(), currentCallFramesFunction,
466+
v8::Integer::New(m_isolate, limit))
464467
.ToLocalChecked();
465468
} else {
466469
v8::Local<v8::Value> argv[] = {m_executionState,
@@ -559,7 +562,7 @@ void V8Debugger::handleProgramBreak(v8::Local<v8::Context> pausedContext,
559562
}
560563

561564
void V8Debugger::v8DebugEventCallback(
562-
const v8::Debug::EventDetails& eventDetails) {
565+
const v8::DebugInterface::EventDetails& eventDetails) {
563566
V8Debugger* thisPtr = toV8Debugger(eventDetails.GetCallbackData());
564567
thisPtr->handleV8DebugEvent(eventDetails);
565568
}
@@ -580,7 +583,7 @@ v8::Local<v8::Value> V8Debugger::callInternalGetterFunction(
580583
}
581584

582585
void V8Debugger::handleV8DebugEvent(
583-
const v8::Debug::EventDetails& eventDetails) {
586+
const v8::DebugInterface::EventDetails& eventDetails) {
584587
if (!enabled()) return;
585588
v8::DebugEvent event = eventDetails.GetEvent();
586589
if (event != v8::AsyncTaskEvent && event != v8::Break &&
@@ -729,7 +732,8 @@ v8::MaybeLocal<v8::Value> V8Debugger::functionScopes(
729732
v8::MaybeLocal<v8::Array> V8Debugger::internalProperties(
730733
v8::Local<v8::Context> context, v8::Local<v8::Value> value) {
731734
v8::Local<v8::Array> properties;
732-
if (!v8::Debug::GetInternalProperties(m_isolate, value).ToLocal(&properties))
735+
if (!v8::DebugInterface::GetInternalProperties(m_isolate, value)
736+
.ToLocal(&properties))
733737
return v8::MaybeLocal<v8::Array>();
734738
if (value->IsFunction()) {
735739
v8::Local<v8::Function> function = value.As<v8::Function>();

0 commit comments

Comments
 (0)