1 /* 2 * Copyright (c) 2012, 2018, 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 "code/debugInfoRec.hpp" 27 #include "code/nmethod.hpp" 28 #include "code/pcDesc.hpp" 29 #include "jfr/periodic/sampling/jfrCallTrace.hpp" 30 #include "oops/method.hpp" 31 #include "runtime/javaCalls.hpp" 32 #include "runtime/frame.inline.hpp" 33 #include "runtime/registerMap.hpp" 34 #include "runtime/thread.inline.hpp" 35 36 bool JfrGetCallTrace::find_top_frame(frame& top_frame, Method** method, frame& first_frame) { 37 assert(top_frame.cb() != NULL, "invariant"); 38 RegisterMap map(_thread, false); 39 frame candidate = top_frame; 40 for (int i = 0; i < MaxJavaStackTraceDepth * 2; ++i) { 41 if (candidate.is_entry_frame()) { 42 JavaCallWrapper *jcw = candidate.entry_frame_call_wrapper_if_safe(_thread); 43 if (jcw == NULL || jcw->is_first_frame()) { 44 return false; 45 } 46 } 47 48 if (candidate.is_interpreted_frame()) { 49 JavaThreadState state = _thread->thread_state(); 50 const bool known_valid = (state == _thread_in_native || state == _thread_in_vm || state == _thread_blocked); 51 if (known_valid || candidate.is_interpreted_frame_valid(_thread)) { 52 Method* im = candidate.interpreter_frame_method(); 53 if (known_valid && !im->is_valid_method()) { 54 return false; 55 } 56 *method = im; 57 first_frame = candidate; 58 return true; 59 } 60 } 61 62 if (candidate.cb()->is_nmethod()) { 63 // first check to make sure that we have a sane stack, 64 // the PC is actually inside the code part of the codeBlob, 65 // and we are past is_frame_complete_at (stack has been setup) 66 if (!candidate.safe_for_sender(_thread)) { 67 return false; 68 } 69 nmethod* nm = (nmethod*)candidate.cb(); 70 *method = nm->method(); 71 72 if (_in_java) { 73 PcDesc* pc_desc = nm->pc_desc_near(candidate.pc() + 1); 74 if (pc_desc == NULL || pc_desc->scope_decode_offset() == DebugInformationRecorder::serialized_null) { 75 return false; 76 } 77 candidate.set_pc(pc_desc->real_pc(nm)); 78 assert(nm->pc_desc_at(candidate.pc()) != NULL, "invalid pc"); 79 } 80 first_frame = candidate; 81 return true; 82 } 83 84 if (!candidate.safe_for_sender(_thread) || 85 candidate.is_stub_frame() || 86 candidate.cb()->frame_size() <= 0) { 87 return false; 88 } 89 90 candidate = candidate.sender(&map); 91 if (candidate.cb() == NULL) { 92 return false; 93 } 94 } 95 return false; 96 } 97 98 bool JfrGetCallTrace::get_topframe(void* ucontext, frame& topframe) { 99 if (!_thread->pd_get_top_frame_for_profiling(&topframe, ucontext, _in_java)) { 100 return false; 101 } 102 103 if (topframe.cb() == NULL) { 104 return false; 105 } 106 107 frame first_java_frame; 108 Method* method = NULL; 109 if (find_top_frame(topframe, &method, first_java_frame)) { 110 if (method == NULL) { 111 return false; 112 } 113 topframe = first_java_frame; 114 return true; 115 } 116 return false; 117 }