Debug.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  2. * This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. // Interfaces by which the embedding can interact with the Debugger API.
  6. #ifndef js_Debug_h
  7. #define js_Debug_h
  8. #include "mozilla/Assertions.h"
  9. #include "mozilla/Attributes.h"
  10. #include "mozilla/MemoryReporting.h"
  11. #include "jsapi.h"
  12. #include "jspubtd.h"
  13. #include "js/GCAPI.h"
  14. #include "js/RootingAPI.h"
  15. #include "js/TypeDecls.h"
  16. namespace js {
  17. class Debugger;
  18. } // namespace js
  19. namespace JS {
  20. namespace dbg {
  21. // Helping embedding code build objects for Debugger
  22. // -------------------------------------------------
  23. //
  24. // Some Debugger API features lean on the embedding application to construct
  25. // their result values. For example, Debugger.Frame.prototype.scriptEntryReason
  26. // calls hooks provided by the embedding to construct values explaining why it
  27. // invoked JavaScript; if F is a frame called from a mouse click event handler,
  28. // F.scriptEntryReason would return an object of the form:
  29. //
  30. // { eventType: "mousedown", event: <object> }
  31. //
  32. // where <object> is a Debugger.Object whose referent is the event being
  33. // dispatched.
  34. //
  35. // However, Debugger implements a trust boundary. Debuggee code may be
  36. // considered untrusted; debugger code needs to be protected from debuggee
  37. // getters, setters, proxies, Object.watch watchpoints, and any other feature
  38. // that might accidentally cause debugger code to set the debuggee running. The
  39. // Debugger API tries to make it easy to write safe debugger code by only
  40. // offering access to debuggee objects via Debugger.Object instances, which
  41. // ensure that only those operations whose explicit purpose is to invoke
  42. // debuggee code do so. But this protective membrane is only helpful if we
  43. // interpose Debugger.Object instances in all the necessary spots.
  44. //
  45. // SpiderMonkey's compartment system also implements a trust boundary. The
  46. // debuggee and debugger are always in different compartments. Inter-compartment
  47. // work requires carefully tracking which compartment each JSObject or JS::Value
  48. // belongs to, and ensuring that is is correctly wrapped for each operation.
  49. //
  50. // It seems precarious to expect the embedding's hooks to implement these trust
  51. // boundaries. Instead, the JS::dbg::Builder API segregates the code which
  52. // constructs trusted objects from that which deals with untrusted objects.
  53. // Trusted objects have an entirely different C++ type, so code that improperly
  54. // mixes trusted and untrusted objects is caught at compile time.
  55. //
  56. // In the structure shown above, there are two trusted objects, and one
  57. // untrusted object:
  58. //
  59. // - The overall object, with the 'eventType' and 'event' properties, is a
  60. // trusted object. We're going to return it to D.F.p.scriptEntryReason's
  61. // caller, which will handle it directly.
  62. //
  63. // - The Debugger.Object instance appearing as the value of the 'event' property
  64. // is a trusted object. It belongs to the same Debugger instance as the
  65. // Debugger.Frame instance whose scriptEntryReason accessor was called, and
  66. // presents a safe reflection-oriented API for inspecting its referent, which
  67. // is:
  68. //
  69. // - The actual event object, an untrusted object, and the referent of the
  70. // Debugger.Object above. (Content can do things like replacing accessors on
  71. // Event.prototype.)
  72. //
  73. // Using JS::dbg::Builder, all objects and values the embedding deals with
  74. // directly are considered untrusted, and are assumed to be debuggee values. The
  75. // only way to construct trusted objects is to use Builder's own methods, which
  76. // return a separate Object type. The only way to set a property on a trusted
  77. // object is through that Object type. The actual trusted object is never
  78. // exposed to the embedding.
  79. //
  80. // So, for example, the embedding might use code like the following to construct
  81. // the object shown above, given a Builder passed to it by Debugger:
  82. //
  83. // bool
  84. // MyScriptEntryReason::explain(JSContext* cx,
  85. // Builder& builder,
  86. // Builder::Object& result)
  87. // {
  88. // JSObject* eventObject = ... obtain debuggee event object somehow ...;
  89. // if (!eventObject)
  90. // return false;
  91. // result = builder.newObject(cx);
  92. // return result &&
  93. // result.defineProperty(cx, "eventType", SafelyFetchType(eventObject)) &&
  94. // result.defineProperty(cx, "event", eventObject);
  95. // }
  96. //
  97. //
  98. // Object::defineProperty also accepts an Object as the value to store on the
  99. // property. By its type, we know that the value is trusted, so we set it
  100. // directly as the property's value, without interposing a Debugger.Object
  101. // wrapper. This allows the embedding to builted nested structures of trusted
  102. // objects.
  103. //
  104. // The Builder and Builder::Object methods take care of doing whatever
  105. // compartment switching and wrapping are necessary to construct the trusted
  106. // values in the Debugger's compartment.
  107. //
  108. // The Object type is self-rooting. Construction, assignment, and destruction
  109. // all properly root the referent object.
  110. class BuilderOrigin;
  111. class Builder {
  112. // The Debugger instance whose client we are building a value for. We build
  113. // objects in this object's compartment.
  114. PersistentRootedObject debuggerObject;
  115. // debuggerObject's Debugger structure, for convenience.
  116. js::Debugger* debugger;
  117. // Check that |thing| is in the same compartment as our debuggerObject. Used
  118. // for assertions when constructing BuiltThings. We can overload this as we
  119. // add more instantiations of BuiltThing.
  120. #if DEBUG
  121. void assertBuilt(JSObject* obj);
  122. #else
  123. void assertBuilt(JSObject* obj) { }
  124. #endif
  125. protected:
  126. // A reference to a trusted object or value. At the moment, we only use it
  127. // with JSObject*.
  128. template<typename T>
  129. class BuiltThing {
  130. friend class BuilderOrigin;
  131. protected:
  132. // The Builder to which this trusted thing belongs.
  133. Builder& owner;
  134. // A rooted reference to our value.
  135. PersistentRooted<T> value;
  136. BuiltThing(JSContext* cx, Builder& owner_, T value_ = GCPolicy<T>::initial())
  137. : owner(owner_), value(cx, value_)
  138. {
  139. owner.assertBuilt(value_);
  140. }
  141. // Forward some things from our owner, for convenience.
  142. js::Debugger* debugger() const { return owner.debugger; }
  143. JSObject* debuggerObject() const { return owner.debuggerObject; }
  144. public:
  145. BuiltThing(const BuiltThing& rhs) : owner(rhs.owner), value(rhs.value) { }
  146. BuiltThing& operator=(const BuiltThing& rhs) {
  147. MOZ_ASSERT(&owner == &rhs.owner);
  148. owner.assertBuilt(rhs.value);
  149. value = rhs.value;
  150. return *this;
  151. }
  152. explicit operator bool() const {
  153. // If we ever instantiate BuiltThing<Value>, this might not suffice.
  154. return value;
  155. }
  156. private:
  157. BuiltThing() = delete;
  158. };
  159. public:
  160. // A reference to a trusted object, possibly null. Instances of Object are
  161. // always properly rooted. They can be copied and assigned, as if they were
  162. // pointers.
  163. class Object: private BuiltThing<JSObject*> {
  164. friend class Builder; // for construction
  165. friend class BuilderOrigin; // for unwrapping
  166. typedef BuiltThing<JSObject*> Base;
  167. // This is private, because only Builders can create Objects that
  168. // actually point to something (hence the 'friend' declaration).
  169. Object(JSContext* cx, Builder& owner_, HandleObject obj) : Base(cx, owner_, obj.get()) { }
  170. bool definePropertyToTrusted(JSContext* cx, const char* name,
  171. JS::MutableHandleValue value);
  172. public:
  173. Object(JSContext* cx, Builder& owner_) : Base(cx, owner_, nullptr) { }
  174. Object(const Object& rhs) : Base(rhs) { }
  175. // Our automatically-generated assignment operator can see our base
  176. // class's assignment operator, so we don't need to write one out here.
  177. // Set the property named |name| on this object to |value|.
  178. //
  179. // If |value| is a string or primitive, re-wrap it for the debugger's
  180. // compartment.
  181. //
  182. // If |value| is an object, assume it is a debuggee object and make a
  183. // Debugger.Object instance referring to it. Set that as the propery's
  184. // value.
  185. //
  186. // If |value| is another trusted object, store it directly as the
  187. // property's value.
  188. //
  189. // On error, report the problem on cx and return false.
  190. bool defineProperty(JSContext* cx, const char* name, JS::HandleValue value);
  191. bool defineProperty(JSContext* cx, const char* name, JS::HandleObject value);
  192. bool defineProperty(JSContext* cx, const char* name, Object& value);
  193. using Base::operator bool;
  194. };
  195. // Build an empty object for direct use by debugger code, owned by this
  196. // Builder. If an error occurs, report it on cx and return a false Object.
  197. Object newObject(JSContext* cx);
  198. protected:
  199. Builder(JSContext* cx, js::Debugger* debugger);
  200. };
  201. // Debugger itself instantiates this subclass of Builder, which can unwrap
  202. // BuiltThings that belong to it.
  203. class BuilderOrigin : public Builder {
  204. template<typename T>
  205. T unwrapAny(const BuiltThing<T>& thing) {
  206. MOZ_ASSERT(&thing.owner == this);
  207. return thing.value.get();
  208. }
  209. public:
  210. BuilderOrigin(JSContext* cx, js::Debugger* debugger_)
  211. : Builder(cx, debugger_)
  212. { }
  213. JSObject* unwrap(Object& object) { return unwrapAny(object); }
  214. };
  215. // Finding the size of blocks allocated with malloc
  216. // ------------------------------------------------
  217. //
  218. // Debugger.Memory wants to be able to report how many bytes items in memory are
  219. // consuming. To do this, it needs a function that accepts a pointer to a block,
  220. // and returns the number of bytes allocated to that block. SpiderMonkey itself
  221. // doesn't know which function is appropriate to use, but the embedding does.
  222. // Tell Debuggers in |cx| to use |mallocSizeOf| to find the size of
  223. // malloc'd blocks.
  224. JS_PUBLIC_API(void)
  225. SetDebuggerMallocSizeOf(JSContext* cx, mozilla::MallocSizeOf mallocSizeOf);
  226. // Get the MallocSizeOf function that the given context is using to find the
  227. // size of malloc'd blocks.
  228. JS_PUBLIC_API(mozilla::MallocSizeOf)
  229. GetDebuggerMallocSizeOf(JSContext* cx);
  230. // Debugger and Garbage Collection Events
  231. // --------------------------------------
  232. //
  233. // The Debugger wants to report about its debuggees' GC cycles, however entering
  234. // JS after a GC is troublesome since SpiderMonkey will often do something like
  235. // force a GC and then rely on the nursery being empty. If we call into some
  236. // Debugger's hook after the GC, then JS runs and the nursery won't be
  237. // empty. Instead, we rely on embedders to call back into SpiderMonkey after a
  238. // GC and notify Debuggers to call their onGarbageCollection hook.
  239. // For each Debugger that observed a debuggee involved in the given GC event,
  240. // call its `onGarbageCollection` hook.
  241. JS_PUBLIC_API(bool)
  242. FireOnGarbageCollectionHook(JSContext* cx, GarbageCollectionEvent::Ptr&& data);
  243. // Handlers for observing Promises
  244. // -------------------------------
  245. //
  246. // The Debugger wants to observe behavior of promises, which are implemented by
  247. // Gecko with webidl and which SpiderMonkey knows nothing about. On the other
  248. // hand, Gecko knows nothing about which (if any) debuggers are observing a
  249. // promise's global. The compromise is that Gecko is responsible for calling
  250. // these handlers at the appropriate times, and SpiderMonkey will handle
  251. // notifying any Debugger instances that are observing the given promise's
  252. // global.
  253. // Notify any Debugger instances observing this promise's global that a new
  254. // promise was allocated.
  255. JS_PUBLIC_API(void)
  256. onNewPromise(JSContext* cx, HandleObject promise);
  257. // Notify any Debugger instances observing this promise's global that the
  258. // promise has settled (ie, it has either been fulfilled or rejected). Note that
  259. // this is *not* equivalent to the promise resolution (ie, the promise's fate
  260. // getting locked in) because you can resolve a promise with another pending
  261. // promise, in which case neither promise has settled yet.
  262. //
  263. // It is Gecko's responsibility to ensure that this is never called on the same
  264. // promise more than once (because a promise can only make the transition from
  265. // unsettled to settled once).
  266. JS_PUBLIC_API(void)
  267. onPromiseSettled(JSContext* cx, HandleObject promise);
  268. // Return true if the given value is a Debugger object, false otherwise.
  269. JS_PUBLIC_API(bool)
  270. IsDebugger(JSObject& obj);
  271. // Append each of the debuggee global objects observed by the Debugger object
  272. // |dbgObj| to |vector|. Returns true on success, false on failure.
  273. JS_PUBLIC_API(bool)
  274. GetDebuggeeGlobals(JSContext* cx, JSObject& dbgObj, AutoObjectVector& vector);
  275. // Hooks for reporting where JavaScript execution began.
  276. //
  277. // Our performance tools would like to be able to label blocks of JavaScript
  278. // execution with the function name and source location where execution began:
  279. // the event handler, the callback, etc.
  280. //
  281. // Construct an instance of this class on the stack, providing a JSContext
  282. // belonging to the runtime in which execution will occur. Each time we enter
  283. // JavaScript --- specifically, each time we push a JavaScript stack frame that
  284. // has no older JS frames younger than this AutoEntryMonitor --- we will
  285. // call the appropriate |Entry| member function to indicate where we've begun
  286. // execution.
  287. class MOZ_STACK_CLASS JS_PUBLIC_API(AutoEntryMonitor) {
  288. JSRuntime* runtime_;
  289. AutoEntryMonitor* savedMonitor_;
  290. public:
  291. explicit AutoEntryMonitor(JSContext* cx);
  292. ~AutoEntryMonitor();
  293. // SpiderMonkey reports the JavaScript entry points occuring within this
  294. // AutoEntryMonitor's scope to the following member functions, which the
  295. // embedding is expected to override.
  296. //
  297. // It is important to note that |asyncCause| is owned by the caller and its
  298. // lifetime must outlive the lifetime of the AutoEntryMonitor object. It is
  299. // strongly encouraged that |asyncCause| be a string constant or similar
  300. // statically allocated string.
  301. // We have begun executing |function|. Note that |function| may not be the
  302. // actual closure we are running, but only the canonical function object to
  303. // which the script refers.
  304. virtual void Entry(JSContext* cx, JSFunction* function,
  305. HandleValue asyncStack,
  306. const char* asyncCause) = 0;
  307. // Execution has begun at the entry point of |script|, which is not a
  308. // function body. (This is probably being executed by 'eval' or some
  309. // JSAPI equivalent.)
  310. virtual void Entry(JSContext* cx, JSScript* script,
  311. HandleValue asyncStack,
  312. const char* asyncCause) = 0;
  313. // Execution of the function or script has ended.
  314. virtual void Exit(JSContext* cx) { }
  315. };
  316. } // namespace dbg
  317. } // namespace JS
  318. #endif /* js_Debug_h */