Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

270 lines
8.7 KiB

  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.
  4. #ifndef V8_V8_DEBUG_H_
  5. #define V8_V8_DEBUG_H_
  6. #include "v8.h"
  7. /**
  8. * Debugger support for the V8 JavaScript engine.
  9. */
  10. namespace v8 {
  11. // Debug events which can occur in the V8 JavaScript engine.
  12. enum DebugEvent {
  13. Break = 1,
  14. Exception = 2,
  15. NewFunction = 3,
  16. BeforeCompile = 4,
  17. AfterCompile = 5,
  18. CompileError = 6,
  19. PromiseEvent = 7,
  20. AsyncTaskEvent = 8,
  21. BreakForCommand = 9
  22. };
  23. class V8_EXPORT Debug {
  24. public:
  25. /**
  26. * A client object passed to the v8 debugger whose ownership will be taken by
  27. * it. v8 is always responsible for deleting the object.
  28. */
  29. class ClientData {
  30. public:
  31. virtual ~ClientData() {}
  32. };
  33. /**
  34. * A message object passed to the debug message handler.
  35. */
  36. class Message {
  37. public:
  38. /**
  39. * Check type of message.
  40. */
  41. virtual bool IsEvent() const = 0;
  42. virtual bool IsResponse() const = 0;
  43. virtual DebugEvent GetEvent() const = 0;
  44. /**
  45. * Indicate whether this is a response to a continue command which will
  46. * start the VM running after this is processed.
  47. */
  48. virtual bool WillStartRunning() const = 0;
  49. /**
  50. * Access to execution state and event data. Don't store these cross
  51. * callbacks as their content becomes invalid. These objects are from the
  52. * debugger event that started the debug message loop.
  53. */
  54. virtual Handle<Object> GetExecutionState() const = 0;
  55. virtual Handle<Object> GetEventData() const = 0;
  56. /**
  57. * Get the debugger protocol JSON.
  58. */
  59. virtual Handle<String> GetJSON() const = 0;
  60. /**
  61. * Get the context active when the debug event happened. Note this is not
  62. * the current active context as the JavaScript part of the debugger is
  63. * running in its own context which is entered at this point.
  64. */
  65. virtual Handle<Context> GetEventContext() const = 0;
  66. /**
  67. * Client data passed with the corresponding request if any. This is the
  68. * client_data data value passed into Debug::SendCommand along with the
  69. * request that led to the message or NULL if the message is an event. The
  70. * debugger takes ownership of the data and will delete it even if there is
  71. * no message handler.
  72. */
  73. virtual ClientData* GetClientData() const = 0;
  74. virtual Isolate* GetIsolate() const = 0;
  75. virtual ~Message() {}
  76. };
  77. /**
  78. * An event details object passed to the debug event listener.
  79. */
  80. class EventDetails {
  81. public:
  82. /**
  83. * Event type.
  84. */
  85. virtual DebugEvent GetEvent() const = 0;
  86. /**
  87. * Access to execution state and event data of the debug event. Don't store
  88. * these cross callbacks as their content becomes invalid.
  89. */
  90. virtual Handle<Object> GetExecutionState() const = 0;
  91. virtual Handle<Object> GetEventData() const = 0;
  92. /**
  93. * Get the context active when the debug event happened. Note this is not
  94. * the current active context as the JavaScript part of the debugger is
  95. * running in its own context which is entered at this point.
  96. */
  97. virtual Handle<Context> GetEventContext() const = 0;
  98. /**
  99. * Client data passed with the corresponding callback when it was
  100. * registered.
  101. */
  102. virtual Handle<Value> GetCallbackData() const = 0;
  103. /**
  104. * Client data passed to DebugBreakForCommand function. The
  105. * debugger takes ownership of the data and will delete it even if
  106. * there is no message handler.
  107. */
  108. virtual ClientData* GetClientData() const = 0;
  109. virtual ~EventDetails() {}
  110. };
  111. /**
  112. * Debug event callback function.
  113. *
  114. * \param event_details object providing information about the debug event
  115. *
  116. * A EventCallback2 does not take possession of the event data,
  117. * and must not rely on the data persisting after the handler returns.
  118. */
  119. typedef void (*EventCallback)(const EventDetails& event_details);
  120. /**
  121. * Debug message callback function.
  122. *
  123. * \param message the debug message handler message object
  124. *
  125. * A MessageHandler2 does not take possession of the message data,
  126. * and must not rely on the data persisting after the handler returns.
  127. */
  128. typedef void (*MessageHandler)(const Message& message);
  129. /**
  130. * Callback function for the host to ensure debug messages are processed.
  131. */
  132. typedef void (*DebugMessageDispatchHandler)();
  133. static bool SetDebugEventListener(EventCallback that,
  134. Handle<Value> data = Handle<Value>());
  135. // Schedule a debugger break to happen when JavaScript code is run
  136. // in the given isolate.
  137. static void DebugBreak(Isolate* isolate);
  138. // Remove scheduled debugger break in given isolate if it has not
  139. // happened yet.
  140. static void CancelDebugBreak(Isolate* isolate);
  141. // Check if a debugger break is scheduled in the given isolate.
  142. static bool CheckDebugBreak(Isolate* isolate);
  143. // Break execution of JavaScript in the given isolate (this method
  144. // can be invoked from a non-VM thread) for further client command
  145. // execution on a VM thread. Client data is then passed in
  146. // EventDetails to EventCallback2 at the moment when the VM actually
  147. // stops.
  148. static void DebugBreakForCommand(Isolate* isolate, ClientData* data);
  149. // Message based interface. The message protocol is JSON.
  150. static void SetMessageHandler(MessageHandler handler);
  151. static void SendCommand(Isolate* isolate,
  152. const uint16_t* command, int length,
  153. ClientData* client_data = NULL);
  154. /**
  155. * Run a JavaScript function in the debugger.
  156. * \param fun the function to call
  157. * \param data passed as second argument to the function
  158. * With this call the debugger is entered and the function specified is called
  159. * with the execution state as the first argument. This makes it possible to
  160. * get access to information otherwise not available during normal JavaScript
  161. * execution e.g. details on stack frames. Receiver of the function call will
  162. * be the debugger context global object, however this is a subject to change.
  163. * The following example shows a JavaScript function which when passed to
  164. * v8::Debug::Call will return the current line of JavaScript execution.
  165. *
  166. * \code
  167. * function frame_source_line(exec_state) {
  168. * return exec_state.frame(0).sourceLine();
  169. * }
  170. * \endcode
  171. */
  172. static Local<Value> Call(v8::Handle<v8::Function> fun,
  173. Handle<Value> data = Handle<Value>());
  174. /**
  175. * Returns a mirror object for the given object.
  176. */
  177. static Local<Value> GetMirror(v8::Handle<v8::Value> obj);
  178. /**
  179. * Makes V8 process all pending debug messages.
  180. *
  181. * From V8 point of view all debug messages come asynchronously (e.g. from
  182. * remote debugger) but they all must be handled synchronously: V8 cannot
  183. * do 2 things at one time so normal script execution must be interrupted
  184. * for a while.
  185. *
  186. * Generally when message arrives V8 may be in one of 3 states:
  187. * 1. V8 is running script; V8 will automatically interrupt and process all
  188. * pending messages;
  189. * 2. V8 is suspended on debug breakpoint; in this state V8 is dedicated
  190. * to reading and processing debug messages;
  191. * 3. V8 is not running at all or has called some long-working C++ function;
  192. * by default it means that processing of all debug messages will be deferred
  193. * until V8 gets control again; however, embedding application may improve
  194. * this by manually calling this method.
  195. *
  196. * Technically this method in many senses is equivalent to executing empty
  197. * script:
  198. * 1. It does nothing except for processing all pending debug messages.
  199. * 2. It should be invoked with the same precautions and from the same context
  200. * as V8 script would be invoked from, because:
  201. * a. with "evaluate" command it can do whatever normal script can do,
  202. * including all native calls;
  203. * b. no other thread should call V8 while this method is running
  204. * (v8::Locker may be used here).
  205. *
  206. * "Evaluate" debug command behavior currently is not specified in scope
  207. * of this method.
  208. */
  209. static void ProcessDebugMessages();
  210. /**
  211. * Debugger is running in its own context which is entered while debugger
  212. * messages are being dispatched. This is an explicit getter for this
  213. * debugger context. Note that the content of the debugger context is subject
  214. * to change.
  215. */
  216. static Local<Context> GetDebugContext();
  217. /**
  218. * Enable/disable LiveEdit functionality for the given Isolate
  219. * (default Isolate if not provided). V8 will abort if LiveEdit is
  220. * unexpectedly used. LiveEdit is enabled by default.
  221. */
  222. static void SetLiveEditEnabled(Isolate* isolate, bool enable);
  223. };
  224. } // namespace v8
  225. #undef EXPORT
  226. #endif // V8_V8_DEBUG_H_