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");
 49   assert(rhs_frames != nullptr, "invariant");
 50   assert(rhs_frames->length() > 0, "invariant");
 51   assert(lhs_frames->capacity() == rhs_frames->length(), "invariant");
 52   assert(lhs_frames->length() == rhs_frames->length(), "invariant");
 53   assert(lhs_frames->capacity() == lhs_frames->length(), "invariant");
 54   memcpy(lhs_frames->adr_at(0), rhs_frames->adr_at(0), rhs_frames->length() * sizeof(JfrStackFrame));
 55 }
 56 
 57 JfrStackTrace::JfrStackTrace() :
 58   _next(nullptr),
 59   _frames(new JfrStackFrames(JfrOptionSet::stackdepth())), // ResourceArea
 60   _id(0),
 61   _hash(0),
 62   _count(0),
 63   _max_frames(JfrOptionSet::stackdepth()),
 64   _frames_ownership(false),
 65   _reached_root(false),
 66   _lineno(false),
 67   _written(false) {}
 68 
 69 JfrStackTrace::JfrStackTrace(traceid id, const JfrStackTrace& trace, const JfrStackTrace* next) :
 70   _next(next),
 71   _frames(new (mtTracing) JfrStackFrames(trace.number_of_frames(), trace.number_of_frames(), mtTracing)), // CHeap
 72   _id(id),
 73   _hash(trace._hash),
 74   _count(trace._count),
 75   _max_frames(trace._max_frames),
 76   _frames_ownership(true),
 77   _reached_root(trace._reached_root),
 78   _lineno(trace._lineno),
 79   _written(false) {
 80   copy_frames(_frames, trace._frames);
 81 }
 82 
 83 JfrStackTrace::~JfrStackTrace() {
 84   if (_frames_ownership) {
 85     delete _frames;
 86   }
 87 }
 88 
 89 int JfrStackTrace::number_of_frames() const {
 90   assert(_frames != nullptr, "invariant");
 91   return _frames->length();
 92 }
 93 
 94 template <typename Writer>
 95 static void write_stacktrace(Writer& w, traceid id, bool reached_root, const JfrStackFrames* frames) {
 96   w.write(static_cast<u8>(id));
 97   w.write(static_cast<u1>(!reached_root));
 98   const int nr_of_frames = frames->length();
 99   w.write(static_cast<u4>(nr_of_frames));
100   for (int i = 0; i < nr_of_frames; ++i) {
101     frames->at(i).write(w);
102   }
103 }
104 
105 void JfrStackTrace::write(JfrChunkWriter& sw) const {
106   assert(!_written, "invariant");
107   write_stacktrace(sw, _id, _reached_root, _frames);
108   _written = true;
109 }
110 
111 void JfrStackTrace::write(JfrCheckpointWriter& cpw) const {
112   assert(!_written, "invariant");
113   write_stacktrace(cpw, _id, _reached_root, _frames);
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 }