Debian Codegraph @

js/public/ProfilingFrameIterator.h

raw
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 js_ProfilingFrameIterator_h
8 #define js_ProfilingFrameIterator_h
9
10 #include "mozilla/Assertions.h"
11 #include "mozilla/Attributes.h"
12 #include "mozilla/Maybe.h"
13
14 #include "jstypes.h"
15
16 #include "js/GCAnnotations.h"
17 #include "js/ProfilingCategory.h"
18 #include "js/TypeDecls.h"
19
20 namespace js {
21 class Activation;
22 namespace jit {
23 class JitActivation;
24 class JSJitProfilingFrameIterator;
25 class JitcodeGlobalEntry;
26 } // namespace jit
27 namespace wasm {
28 class ProfilingFrameIterator;
29 } // namespace wasm
30 } // namespace js
31
32 namespace JS {
33
34 // This iterator can be used to walk the stack of a thread suspended at an
35 // arbitrary pc. To provide accurate results, profiling must have been enabled
36 // (via EnableRuntimeProfilingStack) before executing the callstack being
37 // unwound.
38 //
39 // Note that the caller must not do anything that could cause GC to happen while
40 // the iterator is alive, since this could invalidate Ion code and cause its
41 // contents to become out of date.
42 class MOZ_NON_PARAM JS_PUBLIC_API ProfilingFrameIterator {
43 public:
44 enum class Kind : bool { JSJit, Wasm };
45
46 private:
47 JSContext* cx_;
48 mozilla::Maybe<uint64_t> samplePositionInProfilerBuffer_;
49 js::Activation* activation_;
50 // For each JitActivation, this records the lowest (most recent) stack
51 // address. This will usually be either the exitFP of the activation or the
52 // frame or stack pointer of currently executing JIT/Wasm code. The Gecko
53 // profiler uses this to skip native frames between the activation and
54 // endStackAddress_.
55 void* endStackAddress_ = nullptr;
56 Kind kind_;
57
58 static const unsigned StorageSpace = 9 * sizeof(void*);
59 alignas(void*) unsigned char storage_[StorageSpace];
60
61 void* storage() { return storage_; }
62 const void* storage() const { return storage_; }
63
64 js::wasm::ProfilingFrameIterator& wasmIter() {
65 MOZ_ASSERT(!done());
66 MOZ_ASSERT(isWasm());
67 return *static_cast<js::wasm::ProfilingFrameIterator*>(storage());
68 }
69 const js::wasm::ProfilingFrameIterator& wasmIter() const {
70 MOZ_ASSERT(!done());
71 MOZ_ASSERT(isWasm());
72 return *static_cast<const js::wasm::ProfilingFrameIterator*>(storage());
73 }
74
75 js::jit::JSJitProfilingFrameIterator& jsJitIter() {
76 MOZ_ASSERT(!done());
77 MOZ_ASSERT(isJSJit());
78 return *static_cast<js::jit::JSJitProfilingFrameIterator*>(storage());
79 }
80
81 const js::jit::JSJitProfilingFrameIterator& jsJitIter() const {
82 MOZ_ASSERT(!done());
83 MOZ_ASSERT(isJSJit());
84 return *static_cast<const js::jit::JSJitProfilingFrameIterator*>(storage());
85 }
86
87 void maybeSetEndStackAddress(void* addr) {
88 // If endStackAddress_ has already been set, don't change it because we
89 // want this to correspond to the most recent frame.
90 if (!endStackAddress_) {
91 endStackAddress_ = addr;
92 }
93 }
94
95 void settleFrames();
96 void settle();
97
98 public:
99 struct RegisterState {
100 RegisterState()
101 : pc(nullptr),
102 sp(nullptr),
103 fp(nullptr),
104 unused1(nullptr),
105 unused2(nullptr) {}
106 void* pc;
107 void* sp;
108 void* fp;
109 union {
110 // Value of the LR register on ARM platforms.
111 void* lr;
112 // The return address during a tail call operation.
113 // Note that for ARM is still the value of LR register.
114 void* tempRA;
115 // Undefined on non-ARM plaforms outside tail calls operations.
116 void* unused1;
117 };
118 union {
119 // The FP reference during a tail call operation.
120 void* tempFP;
121 // Undefined outside tail calls operations.
122 void* unused2;
123 };
124 };
125
126 ProfilingFrameIterator(
127 JSContext* cx, const RegisterState& state,
128 const mozilla::Maybe<uint64_t>& samplePositionInProfilerBuffer =
129 mozilla::Nothing());
130 ~ProfilingFrameIterator();
131 void operator++();
132 bool done() const { return !activation_; }
133
134 // Assuming the stack grows down (we do), the return value:
135 // - always points into the stack
136 // - is weakly monotonically increasing (may be equal for successive frames)
137 // - will compare greater than newer native and psuedo-stack frame addresses
138 // and less than older native and psuedo-stack frame addresses
139 // The exception is at the point of stack switching between the main stack
140 // and a suspendable one (see WebAssembly JS Promise Integration proposal).
141 void* stackAddress() const;
142
143 enum FrameKind {
144 Frame_BaselineInterpreter,
145 Frame_Baseline,
146 Frame_Ion,
147 Frame_WasmBaseline,
148 Frame_WasmIon,
149 Frame_WasmOther,
150 };
151
152 struct Frame {
153 FrameKind kind;
154 void* stackAddress;
155 union {
156 void* returnAddress_;
157 jsbytecode* interpreterPC_;
158 };
159 void* activation;
160 void* endStackAddress;
161 const char* label;
162 JSScript* interpreterScript;
163 uint64_t realmID;
164
165 public:
166 void* returnAddress() const {
167 MOZ_ASSERT(kind != Frame_BaselineInterpreter);
168 return returnAddress_;
169 }
170 jsbytecode* interpreterPC() const {
171 MOZ_ASSERT(kind == Frame_BaselineInterpreter);
172 return interpreterPC_;
173 }
174 ProfilingCategoryPair profilingCategory() const {
175 switch (kind) {
176 case FrameKind::Frame_BaselineInterpreter:
177 return JS::ProfilingCategoryPair::JS_BaselineInterpret;
178 case FrameKind::Frame_Baseline:
179 return JS::ProfilingCategoryPair::JS_Baseline;
180 case FrameKind::Frame_Ion:
181 return JS::ProfilingCategoryPair::JS_IonMonkey;
182 case FrameKind::Frame_WasmBaseline:
183 return JS::ProfilingCategoryPair::JS_WasmBaseline;
184 case FrameKind::Frame_WasmIon:
185 return JS::ProfilingCategoryPair::JS_WasmIon;
186 case FrameKind::Frame_WasmOther:
187 return JS::ProfilingCategoryPair::JS_WasmOther;
188 }
189 MOZ_CRASH();
190 }
191 } JS_HAZ_GC_INVALIDATED;
192
193 bool isWasm() const;
194 bool isJSJit() const;
195
196 uint32_t extractStack(Frame* frames, uint32_t offset, uint32_t end) const;
197
198 mozilla::Maybe<Frame> getPhysicalFrameWithoutLabel() const;
199
200 // Return the registers from the native caller frame.
201 // Nothing{} if this iterator is NOT pointing at a native-to-JIT entry frame,
202 // or if the information is not accessible/implemented on this platform.
203 mozilla::Maybe<RegisterState> getCppEntryRegisters() const;
204
205 private:
206 mozilla::Maybe<Frame> getPhysicalFrameAndEntry(
207 const js::jit::JitcodeGlobalEntry** entry) const;
208
209 void iteratorConstruct(const RegisterState& state);
210 void iteratorConstruct();
211 void iteratorDestroy();
212 bool iteratorDone();
213 } JS_HAZ_GC_INVALIDATED;
214
215 JS_PUBLIC_API bool IsProfilingEnabledForContext(JSContext* cx);
216
217 /**
218 * After each sample run, this method should be called with the current buffer
219 * position at which the buffer contents start. This will update the
220 * corresponding field on the JSRuntime.
221 *
222 * See the field |profilerSampleBufferRangeStart| on JSRuntime for documentation
223 * about what this value is used for.
224 */
225 JS_PUBLIC_API void SetJSContextProfilerSampleBufferRangeStart(
226 JSContext* cx, uint64_t rangeStart);
227
228 class ProfiledFrameRange;
229
230 // A handle to the underlying JitcodeGlobalEntry, so as to avoid repeated
231 // lookups on JitcodeGlobalTable.
232 class MOZ_STACK_CLASS ProfiledFrameHandle {
233 friend class ProfiledFrameRange;
234
235 JSRuntime* rt_;
236 js::jit::JitcodeGlobalEntry& entry_;
237 void* addr_;
238 void* canonicalAddr_;
239 const char* label_;
240 uint32_t depth_;
241
242 ProfiledFrameHandle(JSRuntime* rt, js::jit::JitcodeGlobalEntry& entry,
243 void* addr, const char* label, uint32_t depth);
244
245 public:
246 const char* label() const { return label_; }
247 uint32_t depth() const { return depth_; }
248 void* canonicalAddress() const { return canonicalAddr_; }
249
250 JS_PUBLIC_API ProfilingFrameIterator::FrameKind frameKind() const;
251
252 JS_PUBLIC_API uint64_t realmID() const;
253 };
254
255 class ProfiledFrameRange {
256 public:
257 class Iter final {
258 public:
259 Iter(const ProfiledFrameRange& range, uint32_t index)
260 : range_(range), index_(index) {}
261
262 JS_PUBLIC_API ProfiledFrameHandle operator*() const;
263
264 // Provide the bare minimum of iterator methods that are needed for
265 // C++ ranged for loops.
266 Iter& operator++() {
267 ++index_;
268 return *this;
269 }
270 bool operator==(const Iter& rhs) const { return index_ == rhs.index_; }
271 bool operator!=(const Iter& rhs) const { return !(*this == rhs); }
272
273 private:
274 const ProfiledFrameRange& range_;
275 uint32_t index_;
276 };
277
278 Iter begin() const { return Iter(*this, 0); }
279 Iter end() const { return Iter(*this, depth_); }
280
281 private:
282 friend JS_PUBLIC_API ProfiledFrameRange GetProfiledFrames(JSContext* cx,
283 void* addr);
284
285 ProfiledFrameRange(JSRuntime* rt, void* addr,
286 js::jit::JitcodeGlobalEntry* entry)
287 : rt_(rt), addr_(addr), entry_(entry), depth_(0) {}
288
289 JSRuntime* rt_;
290 void* addr_;
291 js::jit::JitcodeGlobalEntry* entry_;
292 // Assume maximum inlining depth is <64
293 const char* labels_[64];
294 uint32_t depth_;
295 };
296
297 // Returns a range that can be iterated over using C++ ranged for loops.
298 JS_PUBLIC_API ProfiledFrameRange GetProfiledFrames(JSContext* cx, void* addr);
299
300 } // namespace JS
301
302 #endif /* js_ProfilingFrameIterator_h */