1 /*
   2  * Copyright (c) 2014, 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 "runtime/os.hpp"
  27 #include "utilities/decoder.hpp"
  28 #include "utilities/globalDefinitions.hpp"
  29 #include "utilities/nativeCallStack.hpp"
  30 
  31 const NativeCallStack NativeCallStack::EMPTY_STACK(0, false);
  32 
  33 NativeCallStack::NativeCallStack(int toSkip, bool fillStack) :
  34   _hash_value(0) {
  35 
  36   if (fillStack) {
  37     // We need to skip the NativeCallStack::NativeCallStack frame if a tail call is NOT used
  38     // to call os::get_native_stack. A tail call is used if _NMT_NOINLINE_ is not defined
  39     // (which means this is not a slowdebug build), and we are on 64-bit (except Windows).
  40     // This is not necessarily a rule, but what has been obvserved to date.
  41 #if (defined(_NMT_NOINLINE_) || defined(_WINDOWS) || !defined(_LP64))
  42     // Not a tail call.
  43     toSkip++;
  44 #if (defined(_NMT_NOINLINE_) && defined(BSD) && defined(_LP64))
  45     // Mac OS X slowdebug builds have this odd behavior where NativeCallStack::NativeCallStack
  46     // appears as two frames, so we need to skip an extra frame.
  47     toSkip++;
  48 #endif // Special-case for BSD.
  49 #endif // Not a tail call.
  50     os::get_native_stack(_stack, NMT_TrackingStackDepth, toSkip);
  51   } else {
  52     for (int index = 0; index < NMT_TrackingStackDepth; index ++) {
  53       _stack[index] = NULL;
  54     }
  55   }
  56 }
  57 
  58 NativeCallStack::NativeCallStack(address* pc, int frameCount) {
  59   int frameToCopy = (frameCount < NMT_TrackingStackDepth) ?
  60     frameCount : NMT_TrackingStackDepth;
  61   int index;
  62   for (index = 0; index < frameToCopy; index ++) {
  63     _stack[index] = pc[index];
  64   }
  65   for (; index < NMT_TrackingStackDepth; index ++) {
  66     _stack[index] = NULL;
  67   }
  68   _hash_value = 0;
  69 }
  70 
  71 // number of stack frames captured
  72 int NativeCallStack::frames() const {
  73   int index;
  74   for (index = 0; index < NMT_TrackingStackDepth; index ++) {
  75     if (_stack[index] == NULL) {
  76       break;
  77     }
  78   }
  79   return index;
  80 }
  81 
  82 // Hash code. Any better algorithm?
  83 unsigned int NativeCallStack::hash() const {
  84   uintptr_t hash_val = _hash_value;
  85   if (hash_val == 0) {
  86     for (int index = 0; index < NMT_TrackingStackDepth; index++) {
  87       if (_stack[index] == NULL) break;
  88       hash_val += (uintptr_t)_stack[index];
  89     }
  90 
  91     NativeCallStack* p = const_cast<NativeCallStack*>(this);
  92     p->_hash_value = (unsigned int)(hash_val & 0xFFFFFFFF);
  93   }
  94   return _hash_value;
  95 }
  96 
  97 void NativeCallStack::print_on(outputStream* out) const {
  98   print_on(out, 0);
  99 }
 100 
 101 // Decode and print this call path
 102 void NativeCallStack::print_on(outputStream* out, int indent) const {
 103   address pc;
 104   char    buf[1024];
 105   int     offset;
 106   int     line_no;
 107   if (is_empty()) {
 108     for (int index = 0; index < indent; index ++) out->print(" ");
 109     out->print("[BOOTSTRAP]");
 110   } else {
 111     for (int frame = 0; frame < NMT_TrackingStackDepth; frame ++) {
 112       pc = get_frame(frame);
 113       if (pc == NULL) break;
 114       // Print indent
 115       for (int index = 0; index < indent; index ++) out->print(" ");
 116       if (os::dll_address_to_function_name(pc, buf, sizeof(buf), &offset)) {
 117         out->print("[" PTR_FORMAT "] %s+0x%x", p2i(pc), buf, offset);
 118       } else {
 119         out->print("[" PTR_FORMAT "]", p2i(pc));
 120       }
 121 
 122       if (Decoder::get_source_info(pc, buf, sizeof(buf), &line_no)) {
 123         out->print("  (%s:%d)", buf, line_no);
 124       }
 125       out->cr();
 126     }
 127   }
 128 }
 129