1 // Copyright 2008 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.
11 * Debugger support for the V8 JavaScript engine.
15 // Debug events which can occur in the V8 JavaScript engine.
27 class V8_EXPORT Debug {
30 * A client object passed to the v8 debugger whose ownership will be taken by
31 * it. v8 is always responsible for deleting the object.
35 virtual ~ClientData() {}
40 * A message object passed to the debug message handler.
45 * Check type of message.
47 virtual bool IsEvent() const = 0;
48 virtual bool IsResponse() const = 0;
49 virtual DebugEvent GetEvent() const = 0;
52 * Indicate whether this is a response to a continue command which will
53 * start the VM running after this is processed.
55 virtual bool WillStartRunning() const = 0;
58 * Access to execution state and event data. Don't store these cross
59 * callbacks as their content becomes invalid. These objects are from the
60 * debugger event that started the debug message loop.
62 virtual Handle<Object> GetExecutionState() const = 0;
63 virtual Handle<Object> GetEventData() const = 0;
66 * Get the debugger protocol JSON.
68 virtual Handle<String> GetJSON() const = 0;
71 * Get the context active when the debug event happened. Note this is not
72 * the current active context as the JavaScript part of the debugger is
73 * running in its own context which is entered at this point.
75 virtual Handle<Context> GetEventContext() const = 0;
78 * Client data passed with the corresponding request if any. This is the
79 * client_data data value passed into Debug::SendCommand along with the
80 * request that led to the message or NULL if the message is an event. The
81 * debugger takes ownership of the data and will delete it even if there is
84 virtual ClientData* GetClientData() const = 0;
86 virtual Isolate* GetIsolate() const = 0;
93 * An event details object passed to the debug event listener.
100 virtual DebugEvent GetEvent() const = 0;
103 * Access to execution state and event data of the debug event. Don't store
104 * these cross callbacks as their content becomes invalid.
106 virtual Handle<Object> GetExecutionState() const = 0;
107 virtual Handle<Object> GetEventData() const = 0;
110 * Get the context active when the debug event happened. Note this is not
111 * the current active context as the JavaScript part of the debugger is
112 * running in its own context which is entered at this point.
114 virtual Handle<Context> GetEventContext() const = 0;
117 * Client data passed with the corresponding callback when it was
120 virtual Handle<Value> GetCallbackData() const = 0;
123 * Client data passed to DebugBreakForCommand function. The
124 * debugger takes ownership of the data and will delete it even if
125 * there is no message handler.
127 virtual ClientData* GetClientData() const = 0;
129 virtual ~EventDetails() {}
133 * Debug event callback function.
135 * \param event_details object providing information about the debug event
137 * A EventCallback2 does not take possession of the event data,
138 * and must not rely on the data persisting after the handler returns.
140 typedef void (*EventCallback2)(const EventDetails& event_details);
143 * Debug message callback function.
145 * \param message the debug message handler message object
147 * A MessageHandler2 does not take possession of the message data,
148 * and must not rely on the data persisting after the handler returns.
150 typedef void (*MessageHandler2)(const Message& message);
153 * Debug host dispatch callback function.
155 typedef void (*HostDispatchHandler)();
158 * Callback function for the host to ensure debug messages are processed.
160 typedef void (*DebugMessageDispatchHandler)();
162 static bool SetDebugEventListener2(EventCallback2 that,
163 Handle<Value> data = Handle<Value>());
165 // Set a JavaScript debug event listener.
166 static bool SetDebugEventListener(v8::Handle<v8::Object> that,
167 Handle<Value> data = Handle<Value>());
169 // Schedule a debugger break to happen when JavaScript code is run
170 // in the given isolate.
171 static void DebugBreak(Isolate* isolate);
173 // Remove scheduled debugger break in given isolate if it has not
175 static void CancelDebugBreak(Isolate* isolate);
177 // Break execution of JavaScript in the given isolate (this method
178 // can be invoked from a non-VM thread) for further client command
179 // execution on a VM thread. Client data is then passed in
180 // EventDetails to EventCallback2 at the moment when the VM actually
182 static void DebugBreakForCommand(Isolate* isolate, ClientData* data);
184 // TODO(svenpanne) Remove this when Chrome is updated.
185 static void DebugBreakForCommand(ClientData* data, Isolate* isolate) {
186 DebugBreakForCommand(isolate, data);
189 // Message based interface. The message protocol is JSON.
190 static void SetMessageHandler2(MessageHandler2 handler);
192 static void SendCommand(Isolate* isolate,
193 const uint16_t* command, int length,
194 ClientData* client_data = NULL);
196 // Dispatch interface.
197 static void SetHostDispatchHandler(HostDispatchHandler handler,
201 * Register a callback function to be called when a debug message has been
202 * received and is ready to be processed. For the debug messages to be
203 * processed V8 needs to be entered, and in certain embedding scenarios this
204 * callback can be used to make sure V8 is entered for the debug message to
205 * be processed. Note that debug messages will only be processed if there is
206 * a V8 break. This can happen automatically by using the option
207 * --debugger-auto-break.
208 * \param provide_locker requires that V8 acquires v8::Locker for you before
211 static void SetDebugMessageDispatchHandler(
212 DebugMessageDispatchHandler handler, bool provide_locker = false);
215 * Run a JavaScript function in the debugger.
216 * \param fun the function to call
217 * \param data passed as second argument to the function
218 * With this call the debugger is entered and the function specified is called
219 * with the execution state as the first argument. This makes it possible to
220 * get access to information otherwise not available during normal JavaScript
221 * execution e.g. details on stack frames. Receiver of the function call will
222 * be the debugger context global object, however this is a subject to change.
223 * The following example shows a JavaScript function which when passed to
224 * v8::Debug::Call will return the current line of JavaScript execution.
227 * function frame_source_line(exec_state) {
228 * return exec_state.frame(0).sourceLine();
232 static Local<Value> Call(v8::Handle<v8::Function> fun,
233 Handle<Value> data = Handle<Value>());
236 * Returns a mirror object for the given object.
238 static Local<Value> GetMirror(v8::Handle<v8::Value> obj);
241 * Enable the V8 builtin debug agent. The debugger agent will listen on the
242 * supplied TCP/IP port for remote debugger connection.
243 * \param name the name of the embedding application
244 * \param port the TCP/IP port to listen on
245 * \param wait_for_connection whether V8 should pause on a first statement
246 * allowing remote debugger to connect before anything interesting happened
248 static bool EnableAgent(const char* name, int port,
249 bool wait_for_connection = false);
252 * Disable the V8 builtin debug agent. The TCP/IP connection will be closed.
254 static void DisableAgent();
257 * Makes V8 process all pending debug messages.
259 * From V8 point of view all debug messages come asynchronously (e.g. from
260 * remote debugger) but they all must be handled synchronously: V8 cannot
261 * do 2 things at one time so normal script execution must be interrupted
264 * Generally when message arrives V8 may be in one of 3 states:
265 * 1. V8 is running script; V8 will automatically interrupt and process all
267 * 2. V8 is suspended on debug breakpoint; in this state V8 is dedicated
268 * to reading and processing debug messages;
269 * 3. V8 is not running at all or has called some long-working C++ function;
270 * by default it means that processing of all debug messages will be deferred
271 * until V8 gets control again; however, embedding application may improve
272 * this by manually calling this method.
274 * It makes sense to call this method whenever a new debug message arrived and
275 * V8 is not already running. Method v8::Debug::SetDebugMessageDispatchHandler
276 * should help with the former condition.
278 * Technically this method in many senses is equivalent to executing empty
280 * 1. It does nothing except for processing all pending debug messages.
281 * 2. It should be invoked with the same precautions and from the same context
282 * as V8 script would be invoked from, because:
283 * a. with "evaluate" command it can do whatever normal script can do,
284 * including all native calls;
285 * b. no other thread should call V8 while this method is running
286 * (v8::Locker may be used here).
288 * "Evaluate" debug command behavior currently is not specified in scope
291 static void ProcessDebugMessages();
294 * Debugger is running in its own context which is entered while debugger
295 * messages are being dispatched. This is an explicit getter for this
296 * debugger context. Note that the content of the debugger context is subject
299 static Local<Context> GetDebugContext();
303 * Enable/disable LiveEdit functionality for the given Isolate
304 * (default Isolate if not provided). V8 will abort if LiveEdit is
305 * unexpectedly used. LiveEdit is enabled by default.
307 static void SetLiveEditEnabled(Isolate* isolate, bool enable);
309 // TODO(svenpanne) Remove this when Chrome is updated.
310 static void SetLiveEditEnabled(bool enable, Isolate* isolate) {
311 SetLiveEditEnabled(isolate, enable);
322 #endif // V8_V8_DEBUG_H_