zgu@7074: /* zgu@7074: * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. zgu@7074: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. zgu@7074: * zgu@7074: * This code is free software; you can redistribute it and/or modify it zgu@7074: * under the terms of the GNU General Public License version 2 only, as zgu@7074: * published by the Free Software Foundation. zgu@7074: * zgu@7074: * This code is distributed in the hope that it will be useful, but WITHOUT zgu@7074: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or zgu@7074: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License zgu@7074: * version 2 for more details (a copy is included in the LICENSE file that zgu@7074: * accompanied this code). zgu@7074: * zgu@7074: * You should have received a copy of the GNU General Public License version zgu@7074: * 2 along with this work; if not, write to the Free Software Foundation, zgu@7074: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. zgu@7074: * zgu@7074: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA zgu@7074: * or visit www.oracle.com if you need additional information or have any zgu@7074: * questions. zgu@7074: * zgu@7074: */ zgu@7074: zgu@7074: #include "precompiled.hpp" zgu@7074: #include "runtime/os.hpp" zgu@7074: #include "utilities/globalDefinitions.hpp" zgu@7074: #include "utilities/nativeCallStack.hpp" zgu@7074: zgu@7078: const NativeCallStack NativeCallStack::EMPTY_STACK(0, false); zgu@7074: zgu@7074: NativeCallStack::NativeCallStack(int toSkip, bool fillStack) : zgu@7074: _hash_value(0) { zgu@7074: zgu@7074: #if !PLATFORM_NATIVE_STACK_WALKING_SUPPORTED zgu@7074: fillStack = false; zgu@7074: #endif zgu@7074: zgu@7074: if (fillStack) { zgu@7074: os::get_native_stack(_stack, NMT_TrackingStackDepth, toSkip); zgu@7074: } else { zgu@7074: for (int index = 0; index < NMT_TrackingStackDepth; index ++) { zgu@7074: _stack[index] = NULL; zgu@7074: } zgu@7074: } zgu@7074: } zgu@7074: zgu@7074: NativeCallStack::NativeCallStack(address* pc, int frameCount) { zgu@7074: int frameToCopy = (frameCount < NMT_TrackingStackDepth) ? zgu@7074: frameCount : NMT_TrackingStackDepth; zgu@7074: int index; zgu@7074: for (index = 0; index < frameToCopy; index ++) { zgu@7074: _stack[index] = pc[index]; zgu@7074: } zgu@7074: for (; index < NMT_TrackingStackDepth; index ++) { zgu@7074: _stack[index] = NULL; zgu@7074: } zgu@7074: } zgu@7074: zgu@7074: // number of stack frames captured zgu@7074: int NativeCallStack::frames() const { zgu@7074: int index; zgu@7074: for (index = 0; index < NMT_TrackingStackDepth; index ++) { zgu@7074: if (_stack[index] == NULL) { zgu@7074: break; zgu@7074: } zgu@7074: } zgu@7074: return index; zgu@7074: } zgu@7074: zgu@7074: // Hash code. Any better algorithm? zgu@7074: int NativeCallStack::hash() const { zgu@7074: long hash_val = _hash_value; zgu@7074: if (hash_val == 0) { zgu@7074: long pc; zgu@7074: int index; zgu@7074: for (index = 0; index < NMT_TrackingStackDepth; index ++) { zgu@7074: pc = (long)_stack[index]; zgu@7074: if (pc == 0) break; zgu@7074: hash_val += pc; zgu@7074: } zgu@7074: zgu@7074: NativeCallStack* p = const_cast(this); zgu@7074: p->_hash_value = (int)(hash_val & 0xFFFFFFFF); zgu@7074: } zgu@7074: return _hash_value; zgu@7074: } zgu@7074: zgu@7074: void NativeCallStack::print_on(outputStream* out) const { zgu@7074: print_on(out, 0); zgu@7074: } zgu@7074: zgu@7074: // Decode and print this call path zgu@7074: void NativeCallStack::print_on(outputStream* out, int indent) const { zgu@7074: address pc; zgu@7074: char buf[1024]; zgu@7074: int offset; zgu@7074: if (is_empty()) { zgu@7074: for (int index = 0; index < indent; index ++) out->print(" "); zgu@7074: #if PLATFORM_NATIVE_STACK_WALKING_SUPPORTED zgu@7074: out->print("[BOOTSTRAP]"); zgu@7074: #else zgu@7074: out->print("[No stack]"); zgu@7074: #endif zgu@7074: } else { zgu@7074: for (int frame = 0; frame < NMT_TrackingStackDepth; frame ++) { zgu@7074: pc = get_frame(frame); zgu@7074: if (pc == NULL) break; zgu@7074: // Print indent zgu@7074: for (int index = 0; index < indent; index ++) out->print(" "); zgu@7074: if (os::dll_address_to_function_name(pc, buf, sizeof(buf), &offset)) { zgu@7074: out->print_cr("[" PTR_FORMAT "] %s+0x%x", p2i(pc), buf, offset); zgu@7074: } else { zgu@7074: out->print_cr("[" PTR_FORMAT "]", p2i(pc)); zgu@7074: } zgu@7074: } zgu@7074: } zgu@7074: } zgu@7074: