1 /*
  2  * Copyright (c) 2011, 2020, 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 #ifndef SHARE_JFR_RECORDER_STACKTRACE_JFRSTACKTRACE_HPP
 26 #define SHARE_JFR_RECORDER_STACKTRACE_JFRSTACKTRACE_HPP
 27 
 28 #include "jfr/utilities/jfrAllocation.hpp"
 29 #include "jfr/utilities/jfrTypes.hpp"
 30 
 31 class frame;
 32 class InstanceKlass;
 33 class JavaThread;
 34 class JfrCheckpointWriter;
 35 class JfrChunkWriter;
 36 
 37 class JfrStackFrame {
 38   friend class ObjectSampleCheckpoint;
 39  private:
 40   const InstanceKlass* _klass;
 41   traceid _methodid;
 42   mutable int _line;
 43   int _bci;
 44   u1 _type;
 45 
 46  public:
 47   JfrStackFrame(const traceid& id, int bci, int type, const InstanceKlass* klass);
 48   JfrStackFrame(const traceid& id, int bci, int type, int lineno, const InstanceKlass* klass);
 49 
 50   bool equals(const JfrStackFrame& rhs) const;
 51   void write(JfrChunkWriter& cw) const;
 52   void write(JfrCheckpointWriter& cpw) const;
 53   void resolve_lineno() const;
 54 
 55   enum {
 56     FRAME_INTERPRETER = 0,
 57     FRAME_JIT,
 58     FRAME_INLINE,
 59     FRAME_NATIVE,
 60     NUM_FRAME_TYPES
 61   };
 62 };
 63 
 64 class JfrStackTrace : public JfrCHeapObj {
 65   friend class JfrNativeSamplerCallback;
 66   friend class JfrStackTraceRepository;
 67   friend class ObjectSampleCheckpoint;
 68   friend class ObjectSampler;
 69   friend class OSThreadSampler;
 70   friend class StackTraceResolver;
 71  private:
 72   const JfrStackTrace* _next;
 73   JfrStackFrame* _frames;
 74   traceid _id;
 75   unsigned int _hash;
 76   u4 _nr_of_frames;
 77   u4 _max_frames;
 78   bool _frames_ownership;
 79   bool _reached_root;
 80   mutable bool _lineno;
 81   mutable bool _written;
 82 
 83   const JfrStackTrace* next() const { return _next; }
 84 
 85   bool should_write() const { return !_written; }
 86   void write(JfrChunkWriter& cw) const;
 87   void write(JfrCheckpointWriter& cpw) const;
 88   bool equals(const JfrStackTrace& rhs) const;
 89 
 90   void set_id(traceid id) { _id = id; }
 91   void set_nr_of_frames(u4 nr_of_frames) { _nr_of_frames = nr_of_frames; }
 92   void set_hash(unsigned int hash) { _hash = hash; }
 93   void set_reached_root(bool reached_root) { _reached_root = reached_root; }
 94   void resolve_linenos() const;
 95 
 96   bool record_thread(JavaThread& thread, frame& frame);
 97   bool record_safe(JavaThread* thread, int skip);
 98 
 99   bool have_lineno() const { return _lineno; }
100   bool full_stacktrace() const { return _reached_root; }
101 
102   JfrStackTrace(traceid id, const JfrStackTrace& trace, const JfrStackTrace* next);
103   JfrStackTrace(JfrStackFrame* frames, u4 max_frames);
104   ~JfrStackTrace();
105 
106  public:
107   unsigned int hash() const { return _hash; }
108   traceid id() const { return _id; }
109 };
110 
111 #endif // SHARE_JFR_RECORDER_STACKTRACE_JFRSTACKTRACE_HPP