1 /*
   2  * Copyright (c) 2011, 2019, 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 "precompiled.hpp"
  26 #include "jfr/recorder/jfrEventSetting.inline.hpp"
  27 #include "jfr/recorder/access/jfrStackTraceMark.hpp"
  28 #include "jfr/recorder/access/jfrThreadData.hpp"
  29 #include "jfr/recorder/stacktrace/jfrStackTraceRepository.hpp"
  30 #include "runtime/thread.inline.hpp"
  31 
  32 JfrStackTraceMark::JfrStackTraceMark() : _t(Thread::current()), _previous_id(0), _previous_hash(0) {
  33   JfrThreadData* const trace_data = _t->trace_data();
  34   if (trace_data->has_cached_stack_trace()) {
  35     _previous_id = trace_data->cached_stack_trace_id();
  36     _previous_hash = trace_data->cached_stack_trace_hash();
  37   }
  38   trace_data->set_cached_stack_trace_id(JfrStackTraceRepository::record(Thread::current()));
  39 }
  40 
  41 JfrStackTraceMark::JfrStackTraceMark(Thread* t) : _t(t), _previous_id(0), _previous_hash(0) {
  42   JfrThreadData* const trace_data = _t->trace_data();
  43   if (trace_data->has_cached_stack_trace()) {
  44     _previous_id = trace_data->cached_stack_trace_id();
  45     _previous_hash = trace_data->cached_stack_trace_hash();
  46   }
  47   trace_data->set_cached_stack_trace_id(JfrStackTraceRepository::record(t));
  48 }
  49 
  50 JfrStackTraceMark::JfrStackTraceMark(TraceEventId eventId) : _t(NULL), _previous_id(0), _previous_hash(0) {
  51   if (JfrEventSetting::has_stacktrace(eventId)) {
  52     _t = Thread::current();
  53     JfrThreadData* const trace_data = _t->trace_data();
  54     if (trace_data->has_cached_stack_trace()) {
  55       _previous_id = trace_data->cached_stack_trace_id();
  56       _previous_hash = trace_data->cached_stack_trace_hash();
  57     }
  58     trace_data->set_cached_stack_trace_id(JfrStackTraceRepository::record(_t));
  59   }
  60 }
  61 
  62 JfrStackTraceMark::JfrStackTraceMark(TraceEventId eventId, Thread* t) : _t(NULL), _previous_id(0), _previous_hash(0) {
  63   if (JfrEventSetting::has_stacktrace(eventId)) {
  64     _t = t;
  65     JfrThreadData* const trace_data = _t->trace_data();
  66     if (trace_data->has_cached_stack_trace()) {
  67       _previous_id = trace_data->cached_stack_trace_id();
  68       _previous_hash = trace_data->cached_stack_trace_hash();
  69     }
  70     trace_data->set_cached_stack_trace_id(JfrStackTraceRepository::record(_t));
  71   }
  72 }
  73 
  74 JfrStackTraceMark::~JfrStackTraceMark() {
  75   if (_previous_id != 0) {
  76     _t->trace_data()->set_cached_stack_trace_id(_previous_id, _previous_hash);
  77   } else {
  78     if (_t != NULL) {
  79       _t->trace_data()->clear_cached_stack_trace();
  80     }
  81   }
  82 }