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