src/hotspot/share/utilities/nativeCallStack.cpp
author stefank
Mon, 25 Nov 2019 12:33:15 +0100
changeset 59252 623722a6aeb9
parent 50965 29eaf3feab30
permissions -rw-r--r--
8234740: Harmonize parameter order in Atomic - cmpxchg Reviewed-by: rehn, dholmes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
25946
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
     1
/*
49721
ea0cc7c74e75 8201242: Include source file/line number when reporting native call stack on supported platforms
zgu
parents: 47216
diff changeset
     2
 * Copyright (c) 2014, 2018, Oracle and/or its affiliates. All rights reserved.
25946
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
     4
 *
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
     7
 * published by the Free Software Foundation.
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
     8
 *
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    13
 * accompanied this code).
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    14
 *
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    18
 *
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    21
 * questions.
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    22
 *
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    23
 */
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    24
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    25
#include "precompiled.hpp"
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    26
#include "runtime/os.hpp"
49721
ea0cc7c74e75 8201242: Include source file/line number when reporting native call stack on supported platforms
zgu
parents: 47216
diff changeset
    27
#include "utilities/decoder.hpp"
25946
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    28
#include "utilities/globalDefinitions.hpp"
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    29
#include "utilities/nativeCallStack.hpp"
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    30
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    31
NativeCallStack::NativeCallStack(int toSkip, bool fillStack) :
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    32
  _hash_value(0) {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    33
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    34
  if (fillStack) {
40383
1ebc8c5aed30 8133747: NMT includes an extra stack frame due to assumption NMT is making on tail calls being used
cjplummer
parents: 29574
diff changeset
    35
    // We need to skip the NativeCallStack::NativeCallStack frame if a tail call is NOT used
1ebc8c5aed30 8133747: NMT includes an extra stack frame due to assumption NMT is making on tail calls being used
cjplummer
parents: 29574
diff changeset
    36
    // to call os::get_native_stack. A tail call is used if _NMT_NOINLINE_ is not defined
1ebc8c5aed30 8133747: NMT includes an extra stack frame due to assumption NMT is making on tail calls being used
cjplummer
parents: 29574
diff changeset
    37
    // (which means this is not a slowdebug build), and we are on 64-bit (except Windows).
1ebc8c5aed30 8133747: NMT includes an extra stack frame due to assumption NMT is making on tail calls being used
cjplummer
parents: 29574
diff changeset
    38
    // This is not necessarily a rule, but what has been obvserved to date.
49725
e740e1a38c96 8200550: Xcode 9.3 produce warning -Wexpansion-to-defined
kbarrett
parents: 49721
diff changeset
    39
#if (defined(_NMT_NOINLINE_) || defined(_WINDOWS) || !defined(_LP64))
e740e1a38c96 8200550: Xcode 9.3 produce warning -Wexpansion-to-defined
kbarrett
parents: 49721
diff changeset
    40
    // Not a tail call.
40383
1ebc8c5aed30 8133747: NMT includes an extra stack frame due to assumption NMT is making on tail calls being used
cjplummer
parents: 29574
diff changeset
    41
    toSkip++;
1ebc8c5aed30 8133747: NMT includes an extra stack frame due to assumption NMT is making on tail calls being used
cjplummer
parents: 29574
diff changeset
    42
#if (defined(_NMT_NOINLINE_) && defined(BSD) && defined(_LP64))
1ebc8c5aed30 8133747: NMT includes an extra stack frame due to assumption NMT is making on tail calls being used
cjplummer
parents: 29574
diff changeset
    43
    // Mac OS X slowdebug builds have this odd behavior where NativeCallStack::NativeCallStack
1ebc8c5aed30 8133747: NMT includes an extra stack frame due to assumption NMT is making on tail calls being used
cjplummer
parents: 29574
diff changeset
    44
    // appears as two frames, so we need to skip an extra frame.
1ebc8c5aed30 8133747: NMT includes an extra stack frame due to assumption NMT is making on tail calls being used
cjplummer
parents: 29574
diff changeset
    45
    toSkip++;
49725
e740e1a38c96 8200550: Xcode 9.3 produce warning -Wexpansion-to-defined
kbarrett
parents: 49721
diff changeset
    46
#endif // Special-case for BSD.
e740e1a38c96 8200550: Xcode 9.3 produce warning -Wexpansion-to-defined
kbarrett
parents: 49721
diff changeset
    47
#endif // Not a tail call.
25946
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    48
    os::get_native_stack(_stack, NMT_TrackingStackDepth, toSkip);
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    49
  } else {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    50
    for (int index = 0; index < NMT_TrackingStackDepth; index ++) {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    51
      _stack[index] = NULL;
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    52
    }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    53
  }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    54
}
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    55
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    56
NativeCallStack::NativeCallStack(address* pc, int frameCount) {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    57
  int frameToCopy = (frameCount < NMT_TrackingStackDepth) ?
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    58
    frameCount : NMT_TrackingStackDepth;
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    59
  int index;
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    60
  for (index = 0; index < frameToCopy; index ++) {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    61
    _stack[index] = pc[index];
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    62
  }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    63
  for (; index < NMT_TrackingStackDepth; index ++) {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    64
    _stack[index] = NULL;
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    65
  }
29462
f2e2922222a4 8069124: runtime/NMT/MallocSiteHashOverflow.java failing in nightlies
ctornqvi
parents: 26144
diff changeset
    66
  _hash_value = 0;
25946
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    67
}
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    68
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    69
// number of stack frames captured
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    70
int NativeCallStack::frames() const {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    71
  int index;
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    72
  for (index = 0; index < NMT_TrackingStackDepth; index ++) {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    73
    if (_stack[index] == NULL) {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    74
      break;
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    75
    }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    76
  }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    77
  return index;
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    78
}
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    79
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    80
// Hash code. Any better algorithm?
29462
f2e2922222a4 8069124: runtime/NMT/MallocSiteHashOverflow.java failing in nightlies
ctornqvi
parents: 26144
diff changeset
    81
unsigned int NativeCallStack::hash() const {
f2e2922222a4 8069124: runtime/NMT/MallocSiteHashOverflow.java failing in nightlies
ctornqvi
parents: 26144
diff changeset
    82
  uintptr_t hash_val = _hash_value;
25946
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    83
  if (hash_val == 0) {
29462
f2e2922222a4 8069124: runtime/NMT/MallocSiteHashOverflow.java failing in nightlies
ctornqvi
parents: 26144
diff changeset
    84
    for (int index = 0; index < NMT_TrackingStackDepth; index++) {
f2e2922222a4 8069124: runtime/NMT/MallocSiteHashOverflow.java failing in nightlies
ctornqvi
parents: 26144
diff changeset
    85
      if (_stack[index] == NULL) break;
f2e2922222a4 8069124: runtime/NMT/MallocSiteHashOverflow.java failing in nightlies
ctornqvi
parents: 26144
diff changeset
    86
      hash_val += (uintptr_t)_stack[index];
25946
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    87
    }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    88
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    89
    NativeCallStack* p = const_cast<NativeCallStack*>(this);
29462
f2e2922222a4 8069124: runtime/NMT/MallocSiteHashOverflow.java failing in nightlies
ctornqvi
parents: 26144
diff changeset
    90
    p->_hash_value = (unsigned int)(hash_val & 0xFFFFFFFF);
25946
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    91
  }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    92
  return _hash_value;
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    93
}
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    94
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    95
void NativeCallStack::print_on(outputStream* out) const {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    96
  print_on(out, 0);
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    97
}
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    98
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    99
// Decode and print this call path
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   100
void NativeCallStack::print_on(outputStream* out, int indent) const {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   101
  address pc;
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   102
  char    buf[1024];
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   103
  int     offset;
49721
ea0cc7c74e75 8201242: Include source file/line number when reporting native call stack on supported platforms
zgu
parents: 47216
diff changeset
   104
  int     line_no;
25946
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   105
  if (is_empty()) {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   106
    for (int index = 0; index < indent; index ++) out->print(" ");
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   107
    out->print("[BOOTSTRAP]");
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   108
  } else {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   109
    for (int frame = 0; frame < NMT_TrackingStackDepth; frame ++) {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   110
      pc = get_frame(frame);
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   111
      if (pc == NULL) break;
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   112
      // Print indent
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   113
      for (int index = 0; index < indent; index ++) out->print(" ");
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   114
      if (os::dll_address_to_function_name(pc, buf, sizeof(buf), &offset)) {
49721
ea0cc7c74e75 8201242: Include source file/line number when reporting native call stack on supported platforms
zgu
parents: 47216
diff changeset
   115
        out->print("[" PTR_FORMAT "] %s+0x%x", p2i(pc), buf, offset);
25946
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   116
      } else {
49721
ea0cc7c74e75 8201242: Include source file/line number when reporting native call stack on supported platforms
zgu
parents: 47216
diff changeset
   117
        out->print("[" PTR_FORMAT "]", p2i(pc));
25946
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   118
      }
49721
ea0cc7c74e75 8201242: Include source file/line number when reporting native call stack on supported platforms
zgu
parents: 47216
diff changeset
   119
ea0cc7c74e75 8201242: Include source file/line number when reporting native call stack on supported platforms
zgu
parents: 47216
diff changeset
   120
      if (Decoder::get_source_info(pc, buf, sizeof(buf), &line_no)) {
ea0cc7c74e75 8201242: Include source file/line number when reporting native call stack on supported platforms
zgu
parents: 47216
diff changeset
   121
        out->print("  (%s:%d)", buf, line_no);
ea0cc7c74e75 8201242: Include source file/line number when reporting native call stack on supported platforms
zgu
parents: 47216
diff changeset
   122
      }
ea0cc7c74e75 8201242: Include source file/line number when reporting native call stack on supported platforms
zgu
parents: 47216
diff changeset
   123
      out->cr();
25946
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   124
    }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   125
  }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   126
}