< prev index next >

src/hotspot/share/jfr/recorder/stacktrace/jfrStackTrace.cpp

Print this page
@@ -1,7 +1,7 @@
  /*
-  * Copyright (c) 2011, 2025, Oracle and/or its affiliates. All rights reserved.
+  * Copyright (c) 2011, 2026, Oracle and/or its affiliates. All rights reserved.
   * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   *
   * This code is free software; you can redistribute it and/or modify it
   * under the terms of the GNU General Public License version 2 only, as
   * published by the Free Software Foundation.

@@ -20,10 +20,14 @@
   * or visit www.oracle.com if you need additional information or have any
   * questions.
   *
   */
  
+ #include "code/debugInfoRec.hpp"
+ #include "code/nmethod.hpp"
+ #include "code/pcDesc.hpp"
+ #include "jfr/periodic/sampling/jfrSampleRequest.hpp"
  #include "jfr/recorder/checkpoint/jfrCheckpointWriter.hpp"
  #include "jfr/recorder/checkpoint/types/traceid/jfrTraceId.inline.hpp"
  #include "jfr/recorder/repository/jfrChunkWriter.hpp"
  #include "jfr/recorder/stacktrace/jfrStackTrace.hpp"
  #include "jfr/recorder/stacktrace/jfrVframeStream.inline.hpp"

@@ -125,39 +129,113 @@
  static inline bool is_in_continuation(const frame& frame, JavaThread* jt) {
    return JfrThreadLocal::is_vthread(jt) &&
      (Continuation::is_frame_in_continuation(jt, frame) || Continuation::is_continuation_enterSpecial(frame));
  }
  
- static inline bool is_interpreter(const JfrSampleRequest& request) {
-   return request._sample_bcp != nullptr;
+ inline void JfrStackTrace::record_frame(const Method* method, int bci, u1 frame_type) {
+   assert(method != nullptr, "invariant");
+   const traceid mid = JfrTraceId::load(method);
+   _hash = (_hash * 31) + mid;
+   _hash = (_hash * 31) + bci;
+   _hash = (_hash * 31) + frame_type;
+   _frames->append(JfrStackFrame(mid, bci, frame_type, method->method_holder()));
+   _count++;
  }
  
  void JfrStackTrace::record_interpreter_top_frame(const JfrSampleRequest& request) {
    assert(_hash == 0, "invariant");
    assert(_count == 0, "invariant");
    assert(_frames != nullptr, "invariant");
    assert(_frames->length() == 0, "invariant");
    _hash = 1;
    const Method* method = reinterpret_cast<Method*>(request._sample_pc);
    assert(method != nullptr, "invariant");
-   const traceid mid = JfrTraceId::load(method);
    const int bci = method->is_native() ? 0 : method->bci_from(reinterpret_cast<address>(request._sample_bcp));
    const u1 type = method->is_native() ? JfrStackFrame::FRAME_NATIVE : JfrStackFrame::FRAME_INTERPRETER;
-   _hash = (_hash * 31) + mid;
-   _hash = (_hash * 31) + bci;
-   _hash = (_hash * 31) + type;
-   _frames->append(JfrStackFrame(mid, bci, type, method->method_holder()));
-   _count++;
+   record_frame(method, bci, type);
+ }
+ 
+ class JfrUnpackNeedStackRepair {
+  private:
+   const PcDesc* const _pc_desc;
+   const nmethod* const _nm;
+   const Method* _method;
+   int _decode_offset;
+   int _bci;
+ 
+  public:
+   JfrUnpackNeedStackRepair(const PcDesc* pc_desc, const nmethod* nm) : _pc_desc(pc_desc),
+                                                                        _nm(nm),
+                                                                        _method(nullptr),
+                                                                        _decode_offset(_pc_desc->scope_decode_offset()),
+                                                                        _bci(0) {
+     assert(_pc_desc != nullptr, "invariant");
+     assert(_nm != nullptr, "invariant");
+     assert(_nm->needs_stack_repair(), "invariant");
+     assert(!_nm->is_native_method(), "invariant");
+     assert(_decode_offset != DebugInformationRecorder::serialized_null, "invariant");
+   }
+ 
+   bool has_next() const {
+     return _decode_offset != DebugInformationRecorder::serialized_null;
+   }
+ 
+   void next() {
+     assert(has_next(), "invariant");
+     DebugInfoReadStream reader(_nm, _decode_offset);
+     _decode_offset = reader.read_int();
+     _method = reader.read_method();
+     _bci = reader.read_bci();
+   }
+ 
+   const Method* method() const {
+     return _method;
+   }
+ 
+   int normalized_bci() const {
+     return _bci == InvocationEntryBci ? 0 : _bci;
+   }
+ };
+ 
+ void JfrStackTrace::record_stack_repair_top_frame(const JfrSampleRequest& request) {
+   assert(p2i(request._sample_bcp) == JfrSampleRequestFrameType::NEEDS_STACK_REPAIR, "invariant");
+   assert(_hash == 0, "invariant");
+   assert(_count == 0, "invariant");
+   assert(_frames != nullptr, "invariant");
+   assert(_frames->length() == 0, "invariant");
+   _hash = 1;
+   JfrUnpackNeedStackRepair unpack(static_cast<PcDesc*>(request._sample_pc),
+                                   static_cast<nmethod*>(request._sample_sp));
+   while (unpack.has_next()) {
+     unpack.next();
+     record_frame(unpack.method(), unpack.normalized_bci(), unpack.has_next() ? JfrStackFrame::FRAME_INLINE : JfrStackFrame::FRAME_JIT);
+   }
+ }
+ 
+ static inline JfrSampleRequestFrameType frame_type(const JfrSampleRequest& request) {
+   const intptr_t value = p2i(request._sample_bcp);
+   if (value == 0) {
+     return JfrSampleRequestFrameType::NONE;
+   }
+   return value == JfrSampleRequestFrameType::NEEDS_STACK_REPAIR ? JfrSampleRequestFrameType::NEEDS_STACK_REPAIR :
+                                                                     JfrSampleRequestFrameType::INTERPRETER;
  }
  
  bool JfrStackTrace::record(JavaThread* jt, const frame& frame, bool in_continuation, const JfrSampleRequest& request) {
-   if (is_interpreter(request)) {
+   const JfrSampleRequestFrameType ft = frame_type(request);
+   if (ft == JfrSampleRequestFrameType::NONE) {
+     return record(jt, frame, in_continuation, 0);
+   }
+   if (ft == JfrSampleRequestFrameType::INTERPRETER) {
      record_interpreter_top_frame(request);
-     if (frame.pc() == nullptr) {
-       // No sender frame. Done.
-       return true;
-     }
+   } else {
+     assert(ft == JfrSampleRequestFrameType::NEEDS_STACK_REPAIR, "invariant");
+     record_stack_repair_top_frame(request);
+   }
+   if (frame.pc() == nullptr) {
+     // No sender frame. Done.
+     return true;
    }
    return record(jt, frame, in_continuation, 0);
  }
  
  bool JfrStackTrace::record(JavaThread* jt, int skip, int64_t stack_filter_id) {

@@ -179,11 +257,10 @@
  
  bool JfrStackTrace::record_inner(JavaThread* jt, const frame& frame, bool in_continuation, int skip, int64_t stack_filter_id /* -1 */) {
    assert(jt != nullptr, "invariant");
    assert(!_lineno, "invariant");
    assert(_frames != nullptr, "invariant");
-   assert(_frames->length() == 0 || _frames->length() == 1, "invariant");
    assert(!in_continuation || is_in_continuation(frame, jt), "invariant");
    Thread* const current_thread = Thread::current();
    HandleMark hm(current_thread); // RegisterMap uses Handles to support continuations.
    JfrVframeStream vfs(jt, frame, in_continuation, false);
    _reached_root = true;

@@ -207,31 +284,26 @@
        if (stack_filter->match(method)) {
          vfs.next_vframe();
          continue;
        }
      }
-     const traceid mid = JfrTraceId::load(method);
      u1 type = vfs.is_interpreted_frame() ? JfrStackFrame::FRAME_INTERPRETER : JfrStackFrame::FRAME_JIT;
      int bci = 0;
      if (method->is_native()) {
        type = JfrStackFrame::FRAME_NATIVE;
      } else {
-       bci = vfs.bci();
+       bci = vfs.normalized_bci();
      }
  
      const intptr_t* const frame_id = vfs.frame_id();
      vfs.next_vframe();
      if (type == JfrStackFrame::FRAME_JIT && !vfs.at_end() && frame_id == vfs.frame_id()) {
        // This frame and the caller frame are both the same physical
        // frame, so this frame is inlined into the caller.
        type = JfrStackFrame::FRAME_INLINE;
      }
-     _hash = (_hash * 31) + mid;
-     _hash = (_hash * 31) + bci;
-     _hash = (_hash * 31) + type;
-     _frames->append(JfrStackFrame(mid, bci, type, method->method_holder()));
-     _count++;
+     record_frame(method, bci, type);
    }
    return _count > 0;
  }
  
  void JfrStackTrace::resolve_linenos() const {
< prev index next >