1 /*
2 * Copyright (c) 2011, 2025, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24
25 #include "jfr/recorder/checkpoint/jfrCheckpointWriter.hpp"
26 #include "jfr/recorder/checkpoint/types/traceid/jfrTraceId.inline.hpp"
27 #include "jfr/recorder/repository/jfrChunkWriter.hpp"
28 #include "jfr/recorder/stacktrace/jfrStackTrace.hpp"
29 #include "jfr/recorder/stacktrace/jfrVframeStream.inline.hpp"
30 #include "jfr/recorder/storage/jfrBuffer.hpp"
31 #include "jfr/support/jfrThreadLocal.hpp"
32 #include "jfrStackFilter.hpp"
33 #include "jfrStackFilterRegistry.hpp"
34 #include "memory/allocation.inline.hpp"
35 #include "nmt/memTag.hpp"
36 #include "oops/instanceKlass.inline.hpp"
37 #include "runtime/continuation.hpp"
38 #include "runtime/continuationEntry.inline.hpp"
39 #include "runtime/handles.inline.hpp"
40 #include "runtime/vframe.inline.hpp"
41 #include "utilities/growableArray.hpp"
42
43 static inline void copy_frames(JfrStackFrames* lhs_frames, const JfrStackFrames* rhs_frames) {
44 assert(lhs_frames != nullptr, "invariant");
110 _written = true;
111 }
112
113 bool JfrStackTrace::equals(const JfrStackTrace& rhs) const {
114 if (_reached_root != rhs._reached_root || _frames->length() != rhs.number_of_frames() || _hash != rhs._hash) {
115 return false;
116 }
117 for (int i = 0; i < _frames->length(); ++i) {
118 if (!_frames->at(i).equals(rhs._frames->at(i))) {
119 return false;
120 }
121 }
122 return true;
123 }
124
125 static inline bool is_in_continuation(const frame& frame, JavaThread* jt) {
126 return JfrThreadLocal::is_vthread(jt) &&
127 (Continuation::is_frame_in_continuation(jt, frame) || Continuation::is_continuation_enterSpecial(frame));
128 }
129
130 static inline bool is_interpreter(const JfrSampleRequest& request) {
131 return request._sample_bcp != nullptr;
132 }
133
134 void JfrStackTrace::record_interpreter_top_frame(const JfrSampleRequest& request) {
135 assert(_hash == 0, "invariant");
136 assert(_count == 0, "invariant");
137 assert(_frames != nullptr, "invariant");
138 assert(_frames->length() == 0, "invariant");
139 _hash = 1;
140 const Method* method = reinterpret_cast<Method*>(request._sample_pc);
141 assert(method != nullptr, "invariant");
142 const traceid mid = JfrTraceId::load(method);
143 const int bci = method->is_native() ? 0 : method->bci_from(reinterpret_cast<address>(request._sample_bcp));
144 const u1 type = method->is_native() ? JfrStackFrame::FRAME_NATIVE : JfrStackFrame::FRAME_INTERPRETER;
145 _hash = (_hash * 31) + mid;
146 _hash = (_hash * 31) + bci;
147 _hash = (_hash * 31) + type;
148 _frames->append(JfrStackFrame(mid, bci, type, method->method_holder()));
149 _count++;
150 }
151
152 bool JfrStackTrace::record(JavaThread* jt, const frame& frame, bool in_continuation, const JfrSampleRequest& request) {
153 if (is_interpreter(request)) {
154 record_interpreter_top_frame(request);
155 if (frame.pc() == nullptr) {
156 // No sender frame. Done.
157 return true;
158 }
159 }
160 return record(jt, frame, in_continuation, 0);
161 }
162
163 bool JfrStackTrace::record(JavaThread* jt, int skip, int64_t stack_filter_id) {
164 assert(jt != nullptr, "invariant");
165 assert(jt == JavaThread::current(), "invariant");
166 if (!jt->has_last_Java_frame()) {
167 return false;
168 }
169 const frame last_frame = jt->last_frame();
170 return record(jt, last_frame, is_in_continuation(last_frame, jt), skip, stack_filter_id);
171 }
172
173 bool JfrStackTrace::record(JavaThread* jt, const frame& frame, bool in_continuation, int skip, int64_t stack_filter_id /* -1 */) {
174 // Must use ResetNoHandleMark here to bypass if any NoHandleMark exist on stack.
175 // This is because RegisterMap uses Handles to support continuations.
176 ResetNoHandleMark rnhm;
177 return record_inner(jt, frame, in_continuation, skip, stack_filter_id);
178 }
179
180 bool JfrStackTrace::record_inner(JavaThread* jt, const frame& frame, bool in_continuation, int skip, int64_t stack_filter_id /* -1 */) {
181 assert(jt != nullptr, "invariant");
182 assert(!_lineno, "invariant");
183 assert(_frames != nullptr, "invariant");
184 assert(_frames->length() == 0 || _frames->length() == 1, "invariant");
185 assert(!in_continuation || is_in_continuation(frame, jt), "invariant");
186 Thread* const current_thread = Thread::current();
187 HandleMark hm(current_thread); // RegisterMap uses Handles to support continuations.
188 JfrVframeStream vfs(jt, frame, in_continuation, false);
189 _reached_root = true;
190 for (int i = 0; i < skip; ++i) {
191 if (vfs.at_end()) {
192 break;
193 }
194 vfs.next_vframe();
195 }
196 const JfrStackFilter* stack_filter = stack_filter_id < 0 ? nullptr : JfrStackFilterRegistry::lookup(stack_filter_id);
197 if (_hash == 0) {
198 _hash = 1;
199 }
200 while (!vfs.at_end()) {
201 if (_count >= _max_frames) {
202 _reached_root = false;
203 break;
204 }
205 const Method* method = vfs.method();
206 if (stack_filter != nullptr) {
207 if (stack_filter->match(method)) {
208 vfs.next_vframe();
209 continue;
210 }
211 }
212 const traceid mid = JfrTraceId::load(method);
213 u1 type = vfs.is_interpreted_frame() ? JfrStackFrame::FRAME_INTERPRETER : JfrStackFrame::FRAME_JIT;
214 int bci = 0;
215 if (method->is_native()) {
216 type = JfrStackFrame::FRAME_NATIVE;
217 } else {
218 bci = vfs.bci();
219 }
220
221 const intptr_t* const frame_id = vfs.frame_id();
222 vfs.next_vframe();
223 if (type == JfrStackFrame::FRAME_JIT && !vfs.at_end() && frame_id == vfs.frame_id()) {
224 // This frame and the caller frame are both the same physical
225 // frame, so this frame is inlined into the caller.
226 type = JfrStackFrame::FRAME_INLINE;
227 }
228 _hash = (_hash * 31) + mid;
229 _hash = (_hash * 31) + bci;
230 _hash = (_hash * 31) + type;
231 _frames->append(JfrStackFrame(mid, bci, type, method->method_holder()));
232 _count++;
233 }
234 return _count > 0;
235 }
236
237 void JfrStackTrace::resolve_linenos() const {
238 assert(!_lineno, "invariant");
239 for (int i = 0; i < _frames->length(); i++) {
240 _frames->at(i).resolve_lineno();
241 }
242 _lineno = true;
243 }
|
1 /*
2 * Copyright (c) 2011, 2026, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24
25 #include "code/debugInfoRec.hpp"
26 #include "code/nmethod.hpp"
27 #include "code/pcDesc.hpp"
28 #include "jfr/periodic/sampling/jfrSampleRequest.hpp"
29 #include "jfr/recorder/checkpoint/jfrCheckpointWriter.hpp"
30 #include "jfr/recorder/checkpoint/types/traceid/jfrTraceId.inline.hpp"
31 #include "jfr/recorder/repository/jfrChunkWriter.hpp"
32 #include "jfr/recorder/stacktrace/jfrStackTrace.hpp"
33 #include "jfr/recorder/stacktrace/jfrVframeStream.inline.hpp"
34 #include "jfr/recorder/storage/jfrBuffer.hpp"
35 #include "jfr/support/jfrThreadLocal.hpp"
36 #include "jfrStackFilter.hpp"
37 #include "jfrStackFilterRegistry.hpp"
38 #include "memory/allocation.inline.hpp"
39 #include "nmt/memTag.hpp"
40 #include "oops/instanceKlass.inline.hpp"
41 #include "runtime/continuation.hpp"
42 #include "runtime/continuationEntry.inline.hpp"
43 #include "runtime/handles.inline.hpp"
44 #include "runtime/vframe.inline.hpp"
45 #include "utilities/growableArray.hpp"
46
47 static inline void copy_frames(JfrStackFrames* lhs_frames, const JfrStackFrames* rhs_frames) {
48 assert(lhs_frames != nullptr, "invariant");
114 _written = true;
115 }
116
117 bool JfrStackTrace::equals(const JfrStackTrace& rhs) const {
118 if (_reached_root != rhs._reached_root || _frames->length() != rhs.number_of_frames() || _hash != rhs._hash) {
119 return false;
120 }
121 for (int i = 0; i < _frames->length(); ++i) {
122 if (!_frames->at(i).equals(rhs._frames->at(i))) {
123 return false;
124 }
125 }
126 return true;
127 }
128
129 static inline bool is_in_continuation(const frame& frame, JavaThread* jt) {
130 return JfrThreadLocal::is_vthread(jt) &&
131 (Continuation::is_frame_in_continuation(jt, frame) || Continuation::is_continuation_enterSpecial(frame));
132 }
133
134 inline void JfrStackTrace::record_frame(const Method* method, int bci, u1 frame_type) {
135 assert(method != nullptr, "invariant");
136 const traceid mid = JfrTraceId::load(method);
137 _hash = (_hash * 31) + mid;
138 _hash = (_hash * 31) + bci;
139 _hash = (_hash * 31) + frame_type;
140 _frames->append(JfrStackFrame(mid, bci, frame_type, method->method_holder()));
141 _count++;
142 }
143
144 void JfrStackTrace::record_interpreter_top_frame(const JfrSampleRequest& request) {
145 assert(_hash == 0, "invariant");
146 assert(_count == 0, "invariant");
147 assert(_frames != nullptr, "invariant");
148 assert(_frames->length() == 0, "invariant");
149 _hash = 1;
150 const Method* method = reinterpret_cast<Method*>(request._sample_pc);
151 assert(method != nullptr, "invariant");
152 const int bci = method->is_native() ? 0 : method->bci_from(reinterpret_cast<address>(request._sample_bcp));
153 const u1 type = method->is_native() ? JfrStackFrame::FRAME_NATIVE : JfrStackFrame::FRAME_INTERPRETER;
154 record_frame(method, bci, type);
155 }
156
157 class JfrUnpackNeedStackRepair {
158 private:
159 const PcDesc* const _pc_desc;
160 const nmethod* const _nm;
161 const Method* _method;
162 int _decode_offset;
163 int _bci;
164
165 public:
166 JfrUnpackNeedStackRepair(const PcDesc* pc_desc, const nmethod* nm) : _pc_desc(pc_desc),
167 _nm(nm),
168 _method(nullptr),
169 _decode_offset(_pc_desc->scope_decode_offset()),
170 _bci(0) {
171 assert(_pc_desc != nullptr, "invariant");
172 assert(_nm != nullptr, "invariant");
173 assert(_nm->needs_stack_repair(), "invariant");
174 assert(!_nm->is_native_method(), "invariant");
175 assert(_decode_offset != DebugInformationRecorder::serialized_null, "invariant");
176 }
177
178 bool has_next() const {
179 return _decode_offset != DebugInformationRecorder::serialized_null;
180 }
181
182 void next() {
183 assert(has_next(), "invariant");
184 DebugInfoReadStream reader(_nm, _decode_offset);
185 _decode_offset = reader.read_int();
186 _method = reader.read_method();
187 _bci = reader.read_bci();
188 }
189
190 const Method* method() const {
191 return _method;
192 }
193
194 int normalized_bci() const {
195 return _bci == InvocationEntryBci ? 0 : _bci;
196 }
197 };
198
199 void JfrStackTrace::record_stack_repair_top_frame(const JfrSampleRequest& request) {
200 assert(p2i(request._sample_bcp) == JfrSampleRequestFrameType::NEEDS_STACK_REPAIR, "invariant");
201 assert(_hash == 0, "invariant");
202 assert(_count == 0, "invariant");
203 assert(_frames != nullptr, "invariant");
204 assert(_frames->length() == 0, "invariant");
205 _hash = 1;
206 JfrUnpackNeedStackRepair unpack(static_cast<PcDesc*>(request._sample_pc),
207 static_cast<nmethod*>(request._sample_sp));
208 while (unpack.has_next()) {
209 unpack.next();
210 record_frame(unpack.method(), unpack.normalized_bci(), unpack.has_next() ? JfrStackFrame::FRAME_INLINE : JfrStackFrame::FRAME_JIT);
211 }
212 }
213
214 static inline JfrSampleRequestFrameType frame_type(const JfrSampleRequest& request) {
215 const intptr_t value = p2i(request._sample_bcp);
216 if (value == 0) {
217 return JfrSampleRequestFrameType::NONE;
218 }
219 return value == JfrSampleRequestFrameType::NEEDS_STACK_REPAIR ? JfrSampleRequestFrameType::NEEDS_STACK_REPAIR :
220 JfrSampleRequestFrameType::INTERPRETER;
221 }
222
223 bool JfrStackTrace::record(JavaThread* jt, const frame& frame, bool in_continuation, const JfrSampleRequest& request) {
224 const JfrSampleRequestFrameType ft = frame_type(request);
225 if (ft == JfrSampleRequestFrameType::NONE) {
226 return record(jt, frame, in_continuation, 0);
227 }
228 if (ft == JfrSampleRequestFrameType::INTERPRETER) {
229 record_interpreter_top_frame(request);
230 } else {
231 assert(ft == JfrSampleRequestFrameType::NEEDS_STACK_REPAIR, "invariant");
232 record_stack_repair_top_frame(request);
233 }
234 if (frame.pc() == nullptr) {
235 // No sender frame. Done.
236 return true;
237 }
238 return record(jt, frame, in_continuation, 0);
239 }
240
241 bool JfrStackTrace::record(JavaThread* jt, int skip, int64_t stack_filter_id) {
242 assert(jt != nullptr, "invariant");
243 assert(jt == JavaThread::current(), "invariant");
244 if (!jt->has_last_Java_frame()) {
245 return false;
246 }
247 const frame last_frame = jt->last_frame();
248 return record(jt, last_frame, is_in_continuation(last_frame, jt), skip, stack_filter_id);
249 }
250
251 bool JfrStackTrace::record(JavaThread* jt, const frame& frame, bool in_continuation, int skip, int64_t stack_filter_id /* -1 */) {
252 // Must use ResetNoHandleMark here to bypass if any NoHandleMark exist on stack.
253 // This is because RegisterMap uses Handles to support continuations.
254 ResetNoHandleMark rnhm;
255 return record_inner(jt, frame, in_continuation, skip, stack_filter_id);
256 }
257
258 bool JfrStackTrace::record_inner(JavaThread* jt, const frame& frame, bool in_continuation, int skip, int64_t stack_filter_id /* -1 */) {
259 assert(jt != nullptr, "invariant");
260 assert(!_lineno, "invariant");
261 assert(_frames != nullptr, "invariant");
262 assert(!in_continuation || is_in_continuation(frame, jt), "invariant");
263 Thread* const current_thread = Thread::current();
264 HandleMark hm(current_thread); // RegisterMap uses Handles to support continuations.
265 JfrVframeStream vfs(jt, frame, in_continuation, false);
266 _reached_root = true;
267 for (int i = 0; i < skip; ++i) {
268 if (vfs.at_end()) {
269 break;
270 }
271 vfs.next_vframe();
272 }
273 const JfrStackFilter* stack_filter = stack_filter_id < 0 ? nullptr : JfrStackFilterRegistry::lookup(stack_filter_id);
274 if (_hash == 0) {
275 _hash = 1;
276 }
277 while (!vfs.at_end()) {
278 if (_count >= _max_frames) {
279 _reached_root = false;
280 break;
281 }
282 const Method* method = vfs.method();
283 if (stack_filter != nullptr) {
284 if (stack_filter->match(method)) {
285 vfs.next_vframe();
286 continue;
287 }
288 }
289 u1 type = vfs.is_interpreted_frame() ? JfrStackFrame::FRAME_INTERPRETER : JfrStackFrame::FRAME_JIT;
290 int bci = 0;
291 if (method->is_native()) {
292 type = JfrStackFrame::FRAME_NATIVE;
293 } else {
294 bci = vfs.normalized_bci();
295 }
296
297 const intptr_t* const frame_id = vfs.frame_id();
298 vfs.next_vframe();
299 if (type == JfrStackFrame::FRAME_JIT && !vfs.at_end() && frame_id == vfs.frame_id()) {
300 // This frame and the caller frame are both the same physical
301 // frame, so this frame is inlined into the caller.
302 type = JfrStackFrame::FRAME_INLINE;
303 }
304 record_frame(method, bci, type);
305 }
306 return _count > 0;
307 }
308
309 void JfrStackTrace::resolve_linenos() const {
310 assert(!_lineno, "invariant");
311 for (int i = 0; i < _frames->length(); i++) {
312 _frames->at(i).resolve_lineno();
313 }
314 _lineno = true;
315 }
|