1 /*
   2  * Copyright (c) 2014, 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 "runtime/os.hpp"
  27 #include "utilities/globalDefinitions.hpp"
  28 #include "utilities/nativeCallStack.hpp"
  29 
  30 const NativeCallStack NativeCallStack::EMPTY_STACK(0, false);
  31 
  32 NativeCallStack::NativeCallStack(int toSkip, bool fillStack) :
  33   _hash_value(0) {
  34 
  35 #if !PLATFORM_NATIVE_STACK_WALKING_SUPPORTED
  36   fillStack = false;
  37 #endif
  38 
  39   if (fillStack) {
  40     os::get_native_stack(_stack, NMT_TrackingStackDepth, toSkip);
  41   } else {
  42     for (int index = 0; index < NMT_TrackingStackDepth; index ++) {
  43       _stack[index] = NULL;
  44     }
  45   }
  46 }
  47 
  48 NativeCallStack::NativeCallStack(address* pc, int frameCount) {
  49   int frameToCopy = (frameCount < NMT_TrackingStackDepth) ?
  50     frameCount : NMT_TrackingStackDepth;
  51   int index;
  52   for (index = 0; index < frameToCopy; index ++) {
  53     _stack[index] = pc[index];
  54   }
  55   for (; index < NMT_TrackingStackDepth; index ++) {
  56     _stack[index] = NULL;
  57   }
  58   _hash_value = 0;
  59 }
  60 
  61 // number of stack frames captured
  62 int NativeCallStack::frames() const {
  63   int index;
  64   for (index = 0; index < NMT_TrackingStackDepth; index ++) {
  65     if (_stack[index] == NULL) {
  66       break;
  67     }
  68   }
  69   return index;
  70 }
  71 
  72 // Hash code. Any better algorithm?
  73 unsigned int NativeCallStack::hash() const {
  74   long hash_val = _hash_value;
  75   if (hash_val == 0) {
  76     long pc;
  77     int  index;
  78     for (index = 0; index < NMT_TrackingStackDepth; index ++) {
  79       pc = (long)_stack[index];
  80       if (pc == 0) break;
  81       hash_val += pc;
  82     }
  83 
  84     NativeCallStack* p = const_cast<NativeCallStack*>(this);
  85     p->_hash_value = (unsigned int)(hash_val & 0xFFFFFFFF);
  86   }
  87   return _hash_value;
  88 }
  89 
  90 void NativeCallStack::print_on(outputStream* out) const {
  91   print_on(out, 0);
  92 }
  93 
  94 // Decode and print this call path
  95 void NativeCallStack::print_on(outputStream* out, int indent) const {
  96   address pc;
  97   char    buf[1024];
  98   int     offset;
  99   if (is_empty()) {
 100     for (int index = 0; index < indent; index ++) out->print(" ");
 101 #if PLATFORM_NATIVE_STACK_WALKING_SUPPORTED
 102     out->print("[BOOTSTRAP]");
 103 #else
 104     out->print("[No stack]");
 105 #endif
 106   } else {
 107     for (int frame = 0; frame < NMT_TrackingStackDepth; frame ++) {
 108       pc = get_frame(frame);
 109       if (pc == NULL) break;
 110       // Print indent
 111       for (int index = 0; index < indent; index ++) out->print(" ");
 112       if (os::dll_address_to_function_name(pc, buf, sizeof(buf), &offset)) {
 113         out->print_cr("[" PTR_FORMAT "] %s+0x%x", p2i(pc), buf, offset);
 114       } else {
 115         out->print_cr("[" PTR_FORMAT "]", p2i(pc));
 116       }
 117     }
 118   }
 119 }
 120