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