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