blob: 6abf4e095bccadacdb6ef3fa4a3fc06fcbc76966 [file] [log] [blame]
Steve Blocka7e24c12009-10-30 11:49:00 +00001// Copyright 2008 the V8 project authors. All rights reserved.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
Steve Blocka7e24c12009-10-30 11:49:00 +00004
5#ifndef V8_V8_DEBUG_H_
6#define V8_V8_DEBUG_H_
7
8#include "v8.h"
9
Steve Blocka7e24c12009-10-30 11:49:00 +000010/**
11 * Debugger support for the V8 JavaScript engine.
12 */
13namespace v8 {
14
15// Debug events which can occur in the V8 JavaScript engine.
16enum DebugEvent {
17 Break = 1,
18 Exception = 2,
19 NewFunction = 3,
20 BeforeCompile = 4,
21 AfterCompile = 5,
Ben Murdochb8a8cc12014-11-26 15:28:44 +000022 CompileError = 6,
23 PromiseEvent = 7,
24 AsyncTaskEvent = 8,
25 BreakForCommand = 9
Steve Blocka7e24c12009-10-30 11:49:00 +000026};
27
28
Ben Murdochb8a8cc12014-11-26 15:28:44 +000029class V8_EXPORT Debug {
Steve Blocka7e24c12009-10-30 11:49:00 +000030 public:
31 /**
32 * A client object passed to the v8 debugger whose ownership will be taken by
33 * it. v8 is always responsible for deleting the object.
34 */
35 class ClientData {
36 public:
37 virtual ~ClientData() {}
38 };
39
40
41 /**
42 * A message object passed to the debug message handler.
43 */
44 class Message {
45 public:
46 /**
47 * Check type of message.
48 */
49 virtual bool IsEvent() const = 0;
50 virtual bool IsResponse() const = 0;
51 virtual DebugEvent GetEvent() const = 0;
52
53 /**
54 * Indicate whether this is a response to a continue command which will
55 * start the VM running after this is processed.
56 */
57 virtual bool WillStartRunning() const = 0;
58
59 /**
60 * Access to execution state and event data. Don't store these cross
61 * callbacks as their content becomes invalid. These objects are from the
62 * debugger event that started the debug message loop.
63 */
64 virtual Handle<Object> GetExecutionState() const = 0;
65 virtual Handle<Object> GetEventData() const = 0;
66
67 /**
68 * Get the debugger protocol JSON.
69 */
70 virtual Handle<String> GetJSON() const = 0;
71
72 /**
73 * Get the context active when the debug event happened. Note this is not
74 * the current active context as the JavaScript part of the debugger is
Ben Murdoch257744e2011-11-30 15:57:28 +000075 * running in its own context which is entered at this point.
Steve Blocka7e24c12009-10-30 11:49:00 +000076 */
77 virtual Handle<Context> GetEventContext() const = 0;
78
79 /**
80 * Client data passed with the corresponding request if any. This is the
81 * client_data data value passed into Debug::SendCommand along with the
82 * request that led to the message or NULL if the message is an event. The
83 * debugger takes ownership of the data and will delete it even if there is
84 * no message handler.
85 */
86 virtual ClientData* GetClientData() const = 0;
87
Ben Murdochb8a8cc12014-11-26 15:28:44 +000088 virtual Isolate* GetIsolate() const = 0;
89
Steve Blocka7e24c12009-10-30 11:49:00 +000090 virtual ~Message() {}
91 };
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -080092
Steve Blocka7e24c12009-10-30 11:49:00 +000093
94 /**
Leon Clarkef7060e22010-06-03 12:02:55 +010095 * An event details object passed to the debug event listener.
96 */
97 class EventDetails {
98 public:
99 /**
100 * Event type.
101 */
102 virtual DebugEvent GetEvent() const = 0;
103
104 /**
105 * Access to execution state and event data of the debug event. Don't store
106 * these cross callbacks as their content becomes invalid.
107 */
108 virtual Handle<Object> GetExecutionState() const = 0;
109 virtual Handle<Object> GetEventData() const = 0;
110
111 /**
112 * Get the context active when the debug event happened. Note this is not
113 * the current active context as the JavaScript part of the debugger is
Ben Murdoch257744e2011-11-30 15:57:28 +0000114 * running in its own context which is entered at this point.
Leon Clarkef7060e22010-06-03 12:02:55 +0100115 */
116 virtual Handle<Context> GetEventContext() const = 0;
117
118 /**
Ben Murdoch257744e2011-11-30 15:57:28 +0000119 * Client data passed with the corresponding callback when it was
120 * registered.
Leon Clarkef7060e22010-06-03 12:02:55 +0100121 */
122 virtual Handle<Value> GetCallbackData() const = 0;
123
Ben Murdoch3bec4d22010-07-22 14:51:16 +0100124 /**
125 * Client data passed to DebugBreakForCommand function. The
126 * debugger takes ownership of the data and will delete it even if
127 * there is no message handler.
128 */
129 virtual ClientData* GetClientData() const = 0;
130
Leon Clarkef7060e22010-06-03 12:02:55 +0100131 virtual ~EventDetails() {}
132 };
133
Leon Clarkef7060e22010-06-03 12:02:55 +0100134 /**
135 * Debug event callback function.
136 *
137 * \param event_details object providing information about the debug event
138 *
139 * A EventCallback2 does not take possession of the event data,
140 * and must not rely on the data persisting after the handler returns.
141 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000142 typedef void (*EventCallback)(const EventDetails& event_details);
Steve Blocka7e24c12009-10-30 11:49:00 +0000143
144 /**
145 * Debug message callback function.
146 *
147 * \param message the debug message handler message object
Steve Block44f0eee2011-05-26 01:26:41 +0100148 *
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000149 * A MessageHandler2 does not take possession of the message data,
Steve Blocka7e24c12009-10-30 11:49:00 +0000150 * and must not rely on the data persisting after the handler returns.
151 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000152 typedef void (*MessageHandler)(const Message& message);
Steve Blocka7e24c12009-10-30 11:49:00 +0000153
Steve Blockd0582a62009-12-15 09:54:21 +0000154 /**
155 * Callback function for the host to ensure debug messages are processed.
156 */
157 typedef void (*DebugMessageDispatchHandler)();
158
Steve Blocka7e24c12009-10-30 11:49:00 +0000159 static bool SetDebugEventListener(EventCallback that,
160 Handle<Value> data = Handle<Value>());
Steve Blocka7e24c12009-10-30 11:49:00 +0000161
Steve Block44f0eee2011-05-26 01:26:41 +0100162 // Schedule a debugger break to happen when JavaScript code is run
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000163 // in the given isolate.
164 static void DebugBreak(Isolate* isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +0000165
Steve Block44f0eee2011-05-26 01:26:41 +0100166 // Remove scheduled debugger break in given isolate if it has not
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000167 // happened yet.
168 static void CancelDebugBreak(Isolate* isolate);
169
170 // Check if a debugger break is scheduled in the given isolate.
171 static bool CheckDebugBreak(Isolate* isolate);
Ben Murdochf87a2032010-10-22 12:50:53 +0100172
Steve Block44f0eee2011-05-26 01:26:41 +0100173 // Break execution of JavaScript in the given isolate (this method
174 // can be invoked from a non-VM thread) for further client command
175 // execution on a VM thread. Client data is then passed in
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000176 // EventDetails to EventCallback2 at the moment when the VM actually
177 // stops.
178 static void DebugBreakForCommand(Isolate* isolate, ClientData* data);
Ben Murdoch3bec4d22010-07-22 14:51:16 +0100179
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000180 // Message based interface. The message protocol is JSON.
181 static void SetMessageHandler(MessageHandler handler);
Steve Block44f0eee2011-05-26 01:26:41 +0100182
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000183 static void SendCommand(Isolate* isolate,
184 const uint16_t* command, int length,
185 ClientData* client_data = NULL);
Steve Blockd0582a62009-12-15 09:54:21 +0000186
Steve Blocka7e24c12009-10-30 11:49:00 +0000187 /**
188 * Run a JavaScript function in the debugger.
189 * \param fun the function to call
190 * \param data passed as second argument to the function
191 * With this call the debugger is entered and the function specified is called
192 * with the execution state as the first argument. This makes it possible to
193 * get access to information otherwise not available during normal JavaScript
Steve Block6ded16b2010-05-10 14:33:55 +0100194 * execution e.g. details on stack frames. Receiver of the function call will
195 * be the debugger context global object, however this is a subject to change.
Ben Murdoch257744e2011-11-30 15:57:28 +0000196 * The following example shows a JavaScript function which when passed to
Steve Block6ded16b2010-05-10 14:33:55 +0100197 * v8::Debug::Call will return the current line of JavaScript execution.
Steve Blocka7e24c12009-10-30 11:49:00 +0000198 *
199 * \code
200 * function frame_source_line(exec_state) {
201 * return exec_state.frame(0).sourceLine();
202 * }
203 * \endcode
204 */
205 static Local<Value> Call(v8::Handle<v8::Function> fun,
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000206 Handle<Value> data = Handle<Value>());
Steve Blocka7e24c12009-10-30 11:49:00 +0000207
208 /**
209 * Returns a mirror object for the given object.
210 */
211 static Local<Value> GetMirror(v8::Handle<v8::Value> obj);
212
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100213 /**
Leon Clarkee46be812010-01-19 14:06:41 +0000214 * Makes V8 process all pending debug messages.
215 *
216 * From V8 point of view all debug messages come asynchronously (e.g. from
217 * remote debugger) but they all must be handled synchronously: V8 cannot
218 * do 2 things at one time so normal script execution must be interrupted
219 * for a while.
220 *
221 * Generally when message arrives V8 may be in one of 3 states:
222 * 1. V8 is running script; V8 will automatically interrupt and process all
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000223 * pending messages;
Leon Clarkee46be812010-01-19 14:06:41 +0000224 * 2. V8 is suspended on debug breakpoint; in this state V8 is dedicated
225 * to reading and processing debug messages;
226 * 3. V8 is not running at all or has called some long-working C++ function;
Ben Murdoch257744e2011-11-30 15:57:28 +0000227 * by default it means that processing of all debug messages will be deferred
Leon Clarkee46be812010-01-19 14:06:41 +0000228 * until V8 gets control again; however, embedding application may improve
229 * this by manually calling this method.
230 *
Leon Clarkee46be812010-01-19 14:06:41 +0000231 * Technically this method in many senses is equivalent to executing empty
232 * script:
233 * 1. It does nothing except for processing all pending debug messages.
234 * 2. It should be invoked with the same precautions and from the same context
235 * as V8 script would be invoked from, because:
236 * a. with "evaluate" command it can do whatever normal script can do,
237 * including all native calls;
238 * b. no other thread should call V8 while this method is running
239 * (v8::Locker may be used here).
240 *
241 * "Evaluate" debug command behavior currently is not specified in scope
242 * of this method.
243 */
244 static void ProcessDebugMessages();
Steve Block6ded16b2010-05-10 14:33:55 +0100245
246 /**
Ben Murdoch257744e2011-11-30 15:57:28 +0000247 * Debugger is running in its own context which is entered while debugger
Steve Block6ded16b2010-05-10 14:33:55 +0100248 * messages are being dispatched. This is an explicit getter for this
249 * debugger context. Note that the content of the debugger context is subject
250 * to change.
251 */
252 static Local<Context> GetDebugContext();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000253
254
255 /**
256 * Enable/disable LiveEdit functionality for the given Isolate
257 * (default Isolate if not provided). V8 will abort if LiveEdit is
258 * unexpectedly used. LiveEdit is enabled by default.
259 */
260 static void SetLiveEditEnabled(Isolate* isolate, bool enable);
Steve Blocka7e24c12009-10-30 11:49:00 +0000261};
262
263
264} // namespace v8
265
266
267#undef EXPORT
268
269
270#endif // V8_V8_DEBUG_H_