Bug 1587789 - Remove isXBLAnonymous functions defined and used in the inspector....
[gecko.git] / mfbt / RecordReplay.h
blob8abe2fc4d57df094b6180dc05cadf474a32c534c
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 /* Public API for Web Replay. */
9 #ifndef mozilla_RecordReplay_h
10 #define mozilla_RecordReplay_h
12 #include "mozilla/Attributes.h"
13 #include "mozilla/GuardObjects.h"
14 #include "mozilla/TemplateLib.h"
15 #include "mozilla/Types.h"
16 #include "mozilla/Utf8.h"
18 #include <functional>
19 #include <stdarg.h>
21 struct PLDHashTableOps;
22 struct JSContext;
23 class JSObject;
25 namespace mozilla {
26 namespace recordreplay {
28 // Record/Replay Overview.
30 // Firefox content processes can be specified to record or replay their
31 // behavior. Whether a process is recording or replaying is initialized at the
32 // start of the main() routine, and is afterward invariant for the process.
34 // Recording and replaying works by controlling non-determinism in the browser:
35 // non-deterministic behaviors are initially recorded, then later replayed
36 // exactly to force the browser to behave deterministically. Two types of
37 // non-deterministic behaviors are captured: intra-thread and inter-thread.
38 // Intra-thread non-deterministic behaviors are non-deterministic even in the
39 // absence of actions by other threads, and inter-thread non-deterministic
40 // behaviors are those affected by interleaving execution with other threads.
42 // Intra-thread non-determinism is recorded and replayed as a stream of events
43 // for each thread. Most events originate from calls to system library
44 // functions (for i/o and such); the record/replay system handles these
45 // internally by redirecting these library functions so that code can be
46 // injected and the event recorded/replayed. Events can also be manually
47 // performed using the RecordReplayValue and RecordReplayBytes APIs below.
49 // Inter-thread non-determinism is recorded and replayed by keeping track of
50 // the order in which threads acquire locks or perform atomic accesses. If the
51 // program is data race free, then reproducing the order of these operations
52 // will give an interleaving that is functionally (if not exactly) the same
53 // as during the recording. As for intra-thread non-determinism, system library
54 // redirections are used to capture most inter-thread non-determinism, but the
55 // {Begin,End}OrderedAtomicAccess APIs below can be used to add new ordering
56 // constraints.
58 // Some behaviors can differ between recording and replay. Mainly, pointer
59 // values can differ, and JS GCs can occur at different points (a more complete
60 // list is at the URL below). Some of the APIs below are used to accommodate
61 // these behaviors and keep the replaying process on track.
63 // A third process type, middleman processes, are normal content processes
64 // which facilitate communication with recording and replaying processes,
65 // managing the graphics data they generate, and running devtools code that
66 // interacts with them.
68 // This file contains the main public API for places where mozilla code needs
69 // to interact with the record/replay system. There are a few additional public
70 // APIs in toolkit/recordreplay/ipc, for the IPC performed by
71 // recording/replaying processes and middleman processes.
73 // A more complete description of Web Replay can be found at this URL:
74 // https://developer.mozilla.org/en-US/docs/WebReplay
76 ///////////////////////////////////////////////////////////////////////////////
77 // Public API
78 ///////////////////////////////////////////////////////////////////////////////
80 // Recording and replaying is only enabled on Mac nightlies.
81 #if defined(XP_MACOSX) && defined(NIGHTLY_BUILD)
83 extern MFBT_DATA bool gIsRecordingOrReplaying;
84 extern MFBT_DATA bool gIsRecording;
85 extern MFBT_DATA bool gIsReplaying;
86 extern MFBT_DATA bool gIsMiddleman;
88 // Get the kind of recording/replaying process this is, if any.
89 static inline bool IsRecordingOrReplaying() { return gIsRecordingOrReplaying; }
90 static inline bool IsRecording() { return gIsRecording; }
91 static inline bool IsReplaying() { return gIsReplaying; }
92 static inline bool IsMiddleman() { return gIsMiddleman; }
94 #else // XP_MACOSX && NIGHTLY_BUILD
96 // On unsupported platforms, getting the kind of process is a no-op.
97 static inline bool IsRecordingOrReplaying() { return false; }
98 static inline bool IsRecording() { return false; }
99 static inline bool IsReplaying() { return false; }
100 static inline bool IsMiddleman() { return false; }
102 #endif // XP_MACOSX && NIGHTLY_BUILD
104 // Mark a region which occurs atomically wrt the recording. No two threads can
105 // be in an atomic region at once, and the order in which atomic sections are
106 // executed by the various threads for the same aValue will be the same in the
107 // replay as in the recording. These calls have no effect when not recording or
108 // replaying.
109 static inline void BeginOrderedAtomicAccess(const void* aValue);
110 static inline void EndOrderedAtomicAccess();
112 // RAII class for an atomic access.
113 struct MOZ_RAII AutoOrderedAtomicAccess {
114 explicit AutoOrderedAtomicAccess(const void* aValue) {
115 BeginOrderedAtomicAccess(aValue);
117 ~AutoOrderedAtomicAccess() { EndOrderedAtomicAccess(); }
120 // Mark a region where thread events are passed through the record/replay
121 // system. While recording, no information from system calls or other events
122 // will be recorded for the thread. While replaying, system calls and other
123 // events are performed normally.
124 static inline void BeginPassThroughThreadEvents();
125 static inline void EndPassThroughThreadEvents();
127 // Whether events in this thread are passed through.
128 static inline bool AreThreadEventsPassedThrough();
130 // RAII class for regions where thread events are passed through.
131 struct MOZ_RAII AutoPassThroughThreadEvents {
132 AutoPassThroughThreadEvents() { BeginPassThroughThreadEvents(); }
133 ~AutoPassThroughThreadEvents() { EndPassThroughThreadEvents(); }
136 // As for AutoPassThroughThreadEvents, but may be used when events are already
137 // passed through.
138 struct MOZ_RAII AutoEnsurePassThroughThreadEvents {
139 AutoEnsurePassThroughThreadEvents()
140 : mPassedThrough(AreThreadEventsPassedThrough()) {
141 if (!mPassedThrough) BeginPassThroughThreadEvents();
144 ~AutoEnsurePassThroughThreadEvents() {
145 if (!mPassedThrough) EndPassThroughThreadEvents();
148 private:
149 bool mPassedThrough;
152 // Mark a region where thread events are not allowed to occur. The process will
153 // crash immediately if an event does happen.
154 static inline void BeginDisallowThreadEvents();
155 static inline void EndDisallowThreadEvents();
157 // Whether events in this thread are disallowed.
158 static inline bool AreThreadEventsDisallowed();
160 // RAII class for a region where thread events are disallowed.
161 struct MOZ_RAII AutoDisallowThreadEvents {
162 AutoDisallowThreadEvents() { BeginDisallowThreadEvents(); }
163 ~AutoDisallowThreadEvents() { EndDisallowThreadEvents(); }
166 // Record or replay a value in the current thread's event stream.
167 static inline size_t RecordReplayValue(size_t aValue);
169 // Record or replay the contents of a range of memory in the current thread's
170 // event stream.
171 static inline void RecordReplayBytes(void* aData, size_t aSize);
173 // During recording or replay, mark the recording as unusable. There are some
174 // behaviors that can't be reliably recorded or replayed. For more information,
175 // see 'Unrecordable Executions' in the URL above.
176 static inline void InvalidateRecording(const char* aWhy);
178 // API for ensuring deterministic recording and replaying of PLDHashTables.
179 // This allows PLDHashTables to behave deterministically by generating a custom
180 // set of operations for each table and requiring no other instrumentation.
181 // (PLHashTables have a similar mechanism, though it is not exposed here.)
182 static inline const PLDHashTableOps* GeneratePLDHashTableCallbacks(
183 const PLDHashTableOps* aOps);
184 static inline const PLDHashTableOps* UnwrapPLDHashTableCallbacks(
185 const PLDHashTableOps* aOps);
186 static inline void DestroyPLDHashTableCallbacks(const PLDHashTableOps* aOps);
187 static inline void MovePLDHashTableContents(const PLDHashTableOps* aFirstOps,
188 const PLDHashTableOps* aSecondOps);
190 // Prevent a JS object from ever being collected while recording or replaying.
191 // GC behavior is non-deterministic when recording/replaying, and preventing
192 // an object from being collected ensures that finalizers which might interact
193 // with the recording will not execute.
194 static inline void HoldJSObject(JSObject* aJSObj);
196 // Some devtools operations which execute in a replaying process can cause code
197 // to run which did not run while recording. For example, the JS debugger can
198 // run arbitrary JS while paused at a breakpoint, by doing an eval(). In such
199 // cases we say that execution has diverged from the recording, and if recorded
200 // events are encountered the associated devtools operation fails. This API can
201 // be used to test for such cases and avoid causing the operation to fail.
202 static inline bool HasDivergedFromRecording();
204 // API for debugging inconsistent behavior between recording and replay.
205 // By calling Assert or AssertBytes a thread event will be inserted and any
206 // inconsistent execution order of events will be detected (as for normal
207 // thread events) and reported to the console.
209 // RegisterThing/UnregisterThing associate arbitrary pointers with indexes that
210 // will be consistent between recording/replaying and can be used in assertion
211 // strings.
212 static inline void RecordReplayAssert(const char* aFormat, ...);
213 static inline void RecordReplayAssertBytes(const void* aData, size_t aSize);
214 static inline void RegisterThing(void* aThing);
215 static inline void UnregisterThing(void* aThing);
216 static inline size_t ThingIndex(void* aThing);
218 // Helper for record/replay asserts, try to determine a name for a C++ object
219 // with virtual methods based on its vtable.
220 static inline const char* VirtualThingName(void* aThing);
222 // Enum which describes whether to preserve behavior between recording and
223 // replay sessions.
224 enum class Behavior { DontPreserve, Preserve };
226 // Determine whether this is a recording/replaying or middleman process, and
227 // initialize record/replay state if so.
228 MFBT_API void Initialize(int aArgc, char* aArgv[]);
230 // Kinds of recording/replaying processes that can be spawned.
231 enum class ProcessKind {
232 Recording,
233 Replaying,
234 MiddlemanRecording,
235 MiddlemanReplaying
238 // Command line option for specifying the record/replay kind of a process.
239 static const char gProcessKindOption[] = "-recordReplayKind";
241 // Command line option for specifying the recording file to use.
242 static const char gRecordingFileOption[] = "-recordReplayFile";
244 ///////////////////////////////////////////////////////////////////////////////
245 // JS interface
246 ///////////////////////////////////////////////////////////////////////////////
248 // Get the counter used to keep track of how much progress JS execution has
249 // made while running on the main thread. Progress must advance whenever a JS
250 // function is entered or loop entry point is reached, so that no script
251 // location may be hit twice while the progress counter is the same. See
252 // JSControl.h for more.
253 typedef uint64_t ProgressCounter;
254 MFBT_API ProgressCounter* ExecutionProgressCounter();
256 static inline void AdvanceExecutionProgressCounter() {
257 ++*ExecutionProgressCounter();
260 // Get an identifier for the current execution point which can be used to warp
261 // here later.
262 MFBT_API ProgressCounter NewTimeWarpTarget();
264 // Return whether a script should update the progress counter when it runs.
265 MFBT_API bool ShouldUpdateProgressCounter(const char* aURL);
267 // Define a RecordReplayControl object on the specified global object, with
268 // methods specialized to the current recording/replaying or middleman process
269 // kind.
270 MFBT_API bool DefineRecordReplayControlObject(JSContext* aCx, JSObject* aObj);
272 // Notify the infrastructure that some URL which contains JavaScript or CSS is
273 // being parsed. This is used to provide the complete contents of the URL to
274 // devtools code when it is inspecting the state of this process; that devtools
275 // code can't simply fetch the URL itself since it may have been changed since
276 // the recording was made or may no longer exist. The token for a parse may not
277 // be used in other parses until after EndContentParse() is called.
278 MFBT_API void BeginContentParse(const void* aToken, const char* aURL,
279 const char* aContentType);
281 // Add some UTF-8 parse data to an existing content parse.
282 MFBT_API void AddContentParseData8(const void* aToken,
283 const Utf8Unit* aUtf8Buffer, size_t aLength);
285 // Add some UTF-16 parse data to an existing content parse.
286 MFBT_API void AddContentParseData16(const void* aToken, const char16_t* aBuffer,
287 size_t aLength);
289 // Mark a content parse as having completed.
290 MFBT_API void EndContentParse(const void* aToken);
292 // Perform an entire content parse of UTF-8 data.
293 static inline void NoteContentParse(const void* aToken, const char* aURL,
294 const char* aContentType,
295 const Utf8Unit* aUtf8Buffer,
296 size_t aLength) {
297 BeginContentParse(aToken, aURL, aContentType);
298 AddContentParseData8(aToken, aUtf8Buffer, aLength);
299 EndContentParse(aToken);
302 // Perform an entire content parse of UTF-16 data.
303 static inline void NoteContentParse(const void* aToken, const char* aURL,
304 const char* aContentType,
305 const char16_t* aBuffer, size_t aLength) {
306 BeginContentParse(aToken, aURL, aContentType);
307 AddContentParseData16(aToken, aBuffer, aLength);
308 EndContentParse(aToken);
311 ///////////////////////////////////////////////////////////////////////////////
312 // API inline function implementation
313 ///////////////////////////////////////////////////////////////////////////////
315 // Define inline wrappers on builds where recording/replaying is enabled.
316 #if defined(XP_MACOSX) && defined(NIGHTLY_BUILD)
318 # define MOZ_MAKE_RECORD_REPLAY_WRAPPER_VOID(aName, aFormals, aActuals) \
319 MFBT_API void Internal##aName aFormals; \
320 static inline void aName aFormals { \
321 if (IsRecordingOrReplaying()) { \
322 Internal##aName aActuals; \
326 # define MOZ_MAKE_RECORD_REPLAY_WRAPPER(aName, aReturnType, aDefaultValue, \
327 aFormals, aActuals) \
328 MFBT_API aReturnType Internal##aName aFormals; \
329 static inline aReturnType aName aFormals { \
330 if (IsRecordingOrReplaying()) { \
331 return Internal##aName aActuals; \
333 return aDefaultValue; \
336 // Define inline wrappers on other builds. Avoiding references to the out of
337 // line method avoids link errors when e.g. using Atomic<> but not linking
338 // against MFBT.
339 #else
341 # define MOZ_MAKE_RECORD_REPLAY_WRAPPER_VOID(aName, aFormals, aActuals) \
342 static inline void aName aFormals {}
344 # define MOZ_MAKE_RECORD_REPLAY_WRAPPER(aName, aReturnType, aDefaultValue, \
345 aFormals, aActuals) \
346 static inline aReturnType aName aFormals { return aDefaultValue; }
348 #endif
350 MOZ_MAKE_RECORD_REPLAY_WRAPPER_VOID(BeginOrderedAtomicAccess,
351 (const void* aValue), (aValue))
352 MOZ_MAKE_RECORD_REPLAY_WRAPPER_VOID(EndOrderedAtomicAccess, (), ())
353 MOZ_MAKE_RECORD_REPLAY_WRAPPER_VOID(BeginPassThroughThreadEvents, (), ())
354 MOZ_MAKE_RECORD_REPLAY_WRAPPER_VOID(EndPassThroughThreadEvents, (), ())
355 MOZ_MAKE_RECORD_REPLAY_WRAPPER(AreThreadEventsPassedThrough, bool, false, (),
357 MOZ_MAKE_RECORD_REPLAY_WRAPPER_VOID(BeginDisallowThreadEvents, (), ())
358 MOZ_MAKE_RECORD_REPLAY_WRAPPER_VOID(EndDisallowThreadEvents, (), ())
359 MOZ_MAKE_RECORD_REPLAY_WRAPPER(AreThreadEventsDisallowed, bool, false, (), ())
360 MOZ_MAKE_RECORD_REPLAY_WRAPPER(RecordReplayValue, size_t, aValue,
361 (size_t aValue), (aValue))
362 MOZ_MAKE_RECORD_REPLAY_WRAPPER_VOID(RecordReplayBytes,
363 (void* aData, size_t aSize), (aData, aSize))
364 MOZ_MAKE_RECORD_REPLAY_WRAPPER(HasDivergedFromRecording, bool, false, (), ())
365 MOZ_MAKE_RECORD_REPLAY_WRAPPER(GeneratePLDHashTableCallbacks,
366 const PLDHashTableOps*, aOps,
367 (const PLDHashTableOps* aOps), (aOps))
368 MOZ_MAKE_RECORD_REPLAY_WRAPPER(UnwrapPLDHashTableCallbacks,
369 const PLDHashTableOps*, aOps,
370 (const PLDHashTableOps* aOps), (aOps))
371 MOZ_MAKE_RECORD_REPLAY_WRAPPER_VOID(DestroyPLDHashTableCallbacks,
372 (const PLDHashTableOps* aOps), (aOps))
373 MOZ_MAKE_RECORD_REPLAY_WRAPPER_VOID(MovePLDHashTableContents,
374 (const PLDHashTableOps* aFirstOps,
375 const PLDHashTableOps* aSecondOps),
376 (aFirstOps, aSecondOps))
377 MOZ_MAKE_RECORD_REPLAY_WRAPPER_VOID(InvalidateRecording, (const char* aWhy),
378 (aWhy))
379 MOZ_MAKE_RECORD_REPLAY_WRAPPER_VOID(HoldJSObject, (JSObject * aObject),
380 (aObject))
381 MOZ_MAKE_RECORD_REPLAY_WRAPPER_VOID(RecordReplayAssertBytes,
382 (const void* aData, size_t aSize),
383 (aData, aSize))
384 MOZ_MAKE_RECORD_REPLAY_WRAPPER_VOID(RegisterThing, (void* aThing), (aThing))
385 MOZ_MAKE_RECORD_REPLAY_WRAPPER_VOID(UnregisterThing, (void* aThing), (aThing))
386 MOZ_MAKE_RECORD_REPLAY_WRAPPER(ThingIndex, size_t, 0, (void* aThing), (aThing))
387 MOZ_MAKE_RECORD_REPLAY_WRAPPER(VirtualThingName, const char*, nullptr,
388 (void* aThing), (aThing))
390 #undef MOZ_MAKE_RECORD_REPLAY_WRAPPER_VOID
391 #undef MOZ_MAKERECORDREPLAYWRAPPER
393 MFBT_API void InternalRecordReplayAssert(const char* aFormat, va_list aArgs);
395 static inline void RecordReplayAssert(const char* aFormat, ...) {
396 if (IsRecordingOrReplaying()) {
397 va_list ap;
398 va_start(ap, aFormat);
399 InternalRecordReplayAssert(aFormat, ap);
400 va_end(ap);
404 } // namespace recordreplay
405 } // namespace mozilla
407 #endif /* mozilla_RecordReplay_h */