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/. */
6 
7 #ifndef vm_Probes_h
8 #define vm_Probes_h
9 
10 #ifdef INCLUDE_MOZILLA_DTRACE
11 #  include "javascript-trace.h"
12 #endif
13 
14 #include "vm/Stack.h"
15 
16 namespace js {
17 
18 namespace probes {
19 
20 /*
21  * Static probes
22  *
23  * The probe points defined in this file are scattered around the SpiderMonkey
24  * source tree. The presence of probes::SomeEvent() means that someEvent is
25  * about to happen or has happened. To the extent possible, probes should be
26  * inserted in all paths associated with a given event, regardless of the
27  * active runmode (interpreter/traceJIT/methodJIT/ionJIT).
28  *
29  * When a probe fires, it is handled by any probe handling backends that have
30  * been compiled in. By default, most probes do nothing or at least do nothing
31  * expensive, so the presence of the probe should have negligible effect on
32  * running time. (Probes in slow paths may do something by default, as long as
33  * there is no noticeable slowdown.)
34  *
35  * For some probes, the mere existence of the probe is too expensive even if it
36  * does nothing when called. For example, just having consistent information
37  * available for a function call entry/exit probe causes the JITs to
38  * de-optimize function calls. In those cases, the JITs may query at compile
39  * time whether a probe is desired, and omit the probe invocation if not. If a
40  * probe is runtime-disabled at compilation time, it is not guaranteed to fire
41  * within a compiled function if it is later enabled.
42  *
43  * Not all backends handle all of the probes listed here.
44  */
45 
46 /*
47  * Internal use only: remember whether "profiling", whatever that means, is
48  * currently active. Used for state management.
49  */
50 extern bool ProfilingActive;
51 
52 extern const char nullName[];
53 extern const char anonymousName[];
54 
55 /*
56  * Test whether we are tracking JS function call enter/exit. The JITs use this
57  * to decide whether they can optimize in a way that would prevent probes from
58  * firing.
59  */
60 bool CallTrackingActive(JSContext*);
61 
62 /* Entering a JS function */
63 bool EnterScript(JSContext*, JSScript*, JSFunction*, InterpreterFrame*);
64 
65 /* About to leave a JS function */
66 void ExitScript(JSContext*, JSScript*, JSFunction*, bool popProfilerFrame);
67 
68 /* Executing a script */
69 bool StartExecution(JSScript* script);
70 
71 /* Script has completed execution */
72 bool StopExecution(JSScript* script);
73 
74 /*
75  * Object has been created. |obj| must exist (its class and size are read)
76  */
77 bool CreateObject(JSContext* cx, JSObject* obj);
78 
79 /*
80  * Object is about to be finalized. |obj| must still exist (its class is
81  * read)
82  */
83 bool FinalizeObject(JSObject* obj);
84 
85 /*
86  * Internal: DTrace-specific functions to be called during probes::EnterScript
87  * and probes::ExitScript. These will not be inlined, but the argument
88  * marshalling required for these probe points is expensive enough that it
89  * shouldn't really matter.
90  */
91 void DTraceEnterJSFun(JSContext* cx, JSFunction* fun, JSScript* script);
92 void DTraceExitJSFun(JSContext* cx, JSFunction* fun, JSScript* script);
93 
94 }  // namespace probes
95 
96 #ifdef INCLUDE_MOZILLA_DTRACE
ObjectClassname(JSObject * obj)97 static const char* ObjectClassname(JSObject* obj) {
98   if (!obj) {
99     return "(null object)";
100   }
101   const JSClass* clasp = obj->getClass();
102   if (!clasp) {
103     return "(null)";
104   }
105   const char* class_name = clasp->name;
106   if (!class_name) {
107     return "(null class name)";
108   }
109   return class_name;
110 }
111 #endif
112 
CreateObject(JSContext * cx,JSObject * obj)113 inline bool probes::CreateObject(JSContext* cx, JSObject* obj) {
114   bool ok = true;
115 
116 #ifdef INCLUDE_MOZILLA_DTRACE
117   if (JAVASCRIPT_OBJECT_CREATE_ENABLED()) {
118     JAVASCRIPT_OBJECT_CREATE(ObjectClassname(obj), (uintptr_t)obj);
119   }
120 #endif
121 
122   return ok;
123 }
124 
FinalizeObject(JSObject * obj)125 inline bool probes::FinalizeObject(JSObject* obj) {
126   bool ok = true;
127 
128 #ifdef INCLUDE_MOZILLA_DTRACE
129   if (JAVASCRIPT_OBJECT_FINALIZE_ENABLED()) {
130     const JSClass* clasp = obj->getClass();
131 
132     /* the first arg is nullptr - reserved for future use (filename?) */
133     JAVASCRIPT_OBJECT_FINALIZE(nullptr, (char*)clasp->name, (uintptr_t)obj);
134   }
135 #endif
136 
137   return ok;
138 }
139 
140 } /* namespace js */
141 
142 #endif /* vm_Probes_h */
143