src/hotspot/os/windows/symbolengine.cpp
author stuefe
Tue, 06 Mar 2018 08:16:45 +0100
changeset 49355 30f39453f0a8
parent 49039 0f81d08a578e
permissions -rw-r--r--
8193429: "assert(false) failed: GetModuleFileName failed (126)" in symbolengine.cpp Reviewed-by: clanger, gtriantafill
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
47666
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
     1
/*
49039
0f81d08a578e 8197827: [windows] Small cleanups after JDK-8185712
stuefe
parents: 47666
diff changeset
     2
 * Copyright (c) 2017, 2018, Oracle and/or its affiliates. All rights reserved.
47666
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
     4
 *
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
     7
 * published by the Free Software Foundation.
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
     8
 *
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
    13
 * accompanied this code).
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
    14
 *
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
    18
 *
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
    21
 * questions.
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
    22
 *
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
    23
 */
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
    24
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
    25
#include "precompiled.hpp"
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
    26
#include "utilities/globalDefinitions.hpp"
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
    27
#include "symbolengine.hpp"
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
    28
#include "utilities/debug.hpp"
49039
0f81d08a578e 8197827: [windows] Small cleanups after JDK-8185712
stuefe
parents: 47666
diff changeset
    29
#include "utilities/ostream.hpp"
47666
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
    30
#include "windbghelp.hpp"
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
    31
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
    32
#include <windows.h>
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
    33
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
    34
#include <imagehlp.h>
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
    35
#include <psapi.h>
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
    36
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
    37
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
    38
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
    39
// This code may be invoked normally but also as part of error reporting
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
    40
// In the latter case, we may run under tight memory constraints (native oom)
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
    41
// or in a stack overflow situation or the C heap may be corrupted. We may
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
    42
// run very early before VM initialization or very late when C exit handlers
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
    43
// run. In all these cases, callstacks would still be nice, so lets be robust.
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
    44
//
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
    45
// We need a number of buffers - for the pdb search path, module handle
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
    46
// lists, for demangled symbols, etc.
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
    47
//
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
    48
// These buffers, while typically small, may need to be large for corner
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
    49
// cases (e.g. templatized C++ symbols, or many DLLs loaded). Where do we
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
    50
// allocate them?
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
    51
//
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
    52
// We may be in error handling for a stack overflow, so lets not put them on
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
    53
// the stack.
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
    54
//
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
    55
// Dynamically allocating them may fail if we are handling a native OOM. It
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
    56
// is also a bit dangerous, as the C heap may be corrupted already.
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
    57
//
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
    58
// That leaves pre-allocating them globally, which is safe and should always
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
    59
// work (if we synchronize access) but incurs an undesirable footprint for
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
    60
// non-error cases.
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
    61
//
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
    62
// We follow a two-way strategy: Allocate the buffers on the C heap in a
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
    63
// reasonable large size. Failing that, fall back to static preallocated
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
    64
// buffers. The size of the latter is large enough to handle common scenarios
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
    65
// but small enough not to drive up the footprint too much (several kb).
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
    66
//
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
    67
// We keep these buffers around once allocated, for subsequent requests. This
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
    68
// means that by running the initialization early at a safe time - before
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
    69
// any error happens - buffers can be pre-allocated. This increases the chance
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
    70
// of useful callstacks in error scenarios in exchange for a some cycles spent
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
    71
// at startup. This behavior can be controlled with -XX:+InitializeDbgHelpEarly
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
    72
// and is off by default.
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
    73
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
    74
///////
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
    75
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
    76
// A simple buffer which attempts to allocate an optimal size but will
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
    77
// fall back to a static minimally sized array on allocation error.
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
    78
template <class T, int MINIMAL_CAPACITY, int OPTIMAL_CAPACITY>
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
    79
class SimpleBufferWithFallback {
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
    80
  T _fallback_buffer[MINIMAL_CAPACITY];
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
    81
  T* _p;
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
    82
  int _capacity;
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
    83
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
    84
  // A sentinel at the end of the buffer to catch overflows.
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
    85
  void imprint_sentinel() {
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
    86
    assert(_p && _capacity > 0, "Buffer must be allocated");
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
    87
    _p[_capacity - 1] = (T)'X';
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
    88
    _capacity --;
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
    89
  }
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
    90
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
    91
public:
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
    92
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
    93
  SimpleBufferWithFallback<T, MINIMAL_CAPACITY, OPTIMAL_CAPACITY> ()
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
    94
    : _p(NULL), _capacity(0)
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
    95
  {}
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
    96
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
    97
  // Note: no destructor because these buffers should, once
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
    98
  // allocated, live until process end.
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
    99
  // ~SimpleBufferWithFallback()
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   100
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   101
  // Note: We use raw ::malloc/::free here instead of os::malloc()/os::free
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   102
  // to prevent circularities or secondary crashes during error reporting.
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   103
  virtual void initialize () {
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   104
    assert(_p == NULL && _capacity == 0, "Only call once.");
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   105
    const size_t bytes = OPTIMAL_CAPACITY * sizeof(T);
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   106
    T* q = (T*) ::malloc(bytes);
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   107
    if (q != NULL) {
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   108
      _p = q;
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   109
      _capacity = OPTIMAL_CAPACITY;
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   110
    } else {
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   111
      _p = _fallback_buffer;
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   112
      _capacity = (int)(sizeof(_fallback_buffer) / sizeof(T));
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   113
    }
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   114
    _p[0] = '\0';
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   115
    imprint_sentinel();
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   116
  }
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   117
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   118
  // We need a way to reset the buffer to fallback size for one special
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   119
  // case, where two buffers need to be of identical capacity.
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   120
  void reset_to_fallback_capacity() {
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   121
    if (_p != _fallback_buffer) {
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   122
      ::free(_p);
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   123
    }
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   124
    _p = _fallback_buffer;
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   125
    _capacity = (int)(sizeof(_fallback_buffer) / sizeof(T));
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   126
    _p[0] = '\0';
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   127
    imprint_sentinel();
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   128
  }
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   129
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   130
  T* ptr()                { return _p; }
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   131
  const T* ptr() const    { return _p; }
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   132
  int capacity() const    { return _capacity; }
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   133
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   134
#ifdef ASSERT
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   135
  void check() const {
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   136
    assert(_p[_capacity] == (T)'X', "sentinel lost");
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   137
  }
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   138
#else
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   139
  void check() const {}
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   140
#endif
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   141
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   142
};
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   143
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   144
////
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   145
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   146
// ModuleHandleArray: a list holding module handles. Needs to be large enough
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   147
// to hold one handle per loaded DLL.
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   148
// Note: a standard OpenJDK loads normally ~30 libraries, including system
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   149
// libraries, without third party libraries.
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   150
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   151
typedef SimpleBufferWithFallback <HMODULE, 48, 512> ModuleHandleArrayBase;
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   152
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   153
class ModuleHandleArray : public ModuleHandleArrayBase {
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   154
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   155
  int _num; // Number of handles in this array (may be < capacity).
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   156
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   157
public:
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   158
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   159
  void initialize() {
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   160
    ModuleHandleArrayBase::initialize();
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   161
    _num = 0;
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   162
  }
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   163
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   164
  int num() const { return _num; }
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   165
  void set_num(int n) {
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   166
    assert(n <= capacity(), "Too large");
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   167
    _num = n;
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   168
  }
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   169
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   170
  // Compare with another list; returns true if all handles are equal (incl.
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   171
  // sort order)
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   172
  bool equals(const ModuleHandleArray& other) const {
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   173
    if (_num != other._num) {
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   174
      return false;
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   175
    }
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   176
    if (::memcmp(ptr(), other.ptr(), _num * sizeof(HMODULE)) != 0) {
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   177
      return false;
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   178
    }
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   179
    return true;
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   180
  }
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   181
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   182
  // Copy content from other list.
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   183
  void copy_content_from(ModuleHandleArray& other) {
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   184
    assert(capacity() == other.capacity(), "Different capacities.");
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   185
    memcpy(ptr(), other.ptr(), other._num * sizeof(HMODULE));
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   186
    _num = other._num;
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   187
  }
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   188
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   189
};
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   190
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   191
////
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   192
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   193
// PathBuffer: a buffer to hold and work with a pdb search PATH - a concatenation
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   194
// of multiple directories separated by ';'.
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   195
// A single directory name can be (NTFS) as long as 32K, but in reality is
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   196
// seldom larger than the (historical) MAX_PATH of 260.
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   197
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   198
#define MINIMUM_PDB_PATH_LENGTH  MAX_PATH * 4
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   199
#define OPTIMAL_PDB_PATH_LENGTH  MAX_PATH * 64
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   200
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   201
typedef SimpleBufferWithFallback<char, MINIMUM_PDB_PATH_LENGTH, OPTIMAL_PDB_PATH_LENGTH> PathBufferBase;
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   202
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   203
class PathBuffer: public PathBufferBase {
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   204
public:
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   205
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   206
  // Search PDB path for a directory. Search is case insensitive. Returns
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   207
  // true if directory was found in the path, false otherwise.
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   208
  bool contains_directory(const char* directory) {
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   209
    if (ptr() == NULL) {
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   210
      return false;
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   211
    }
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   212
    const size_t len = strlen(directory);
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   213
    if (len == 0) {
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   214
      return false;
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   215
    }
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   216
    char* p = ptr();
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   217
    for(;;) {
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   218
      char* q = strchr(p, ';');
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   219
      if (q != NULL) {
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   220
        if (len == (q - p)) {
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   221
          if (strnicmp(p, directory, len) == 0) {
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   222
            return true;
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   223
          }
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   224
        }
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   225
        p = q + 1;
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   226
      } else {
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   227
        // tail
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   228
        return stricmp(p, directory) == 0 ? true : false;
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   229
      }
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   230
    }
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   231
    return false;
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   232
  }
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   233
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   234
  // Appends the given directory to the path. Returns false if internal
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   235
  // buffer size was not sufficient.
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   236
  bool append_directory(const char* directory) {
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   237
    const size_t len = strlen(directory);
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   238
    if (len == 0) {
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   239
      return false;
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   240
    }
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   241
    char* p = ptr();
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   242
    const size_t len_now = strlen(p);
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   243
    const size_t needs_capacity = len_now + 1 + len + 1; // xxx;yy\0
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   244
    if (needs_capacity > (size_t)capacity()) {
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   245
      return false; // OOM
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   246
    }
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   247
    if (len_now > 0) { // Not the first path element.
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   248
      p += len_now;
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   249
      *p = ';';
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   250
      p ++;
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   251
    }
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   252
    strcpy(p, directory);
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   253
    return true;
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   254
  }
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   255
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   256
};
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   257
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   258
// A simple buffer to hold one single file name. A file name can be (NTFS) as
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   259
// long as 32K, but in reality is seldom larger than MAX_PATH.
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   260
typedef SimpleBufferWithFallback<char, MAX_PATH, 8 * K> FileNameBuffer;
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   261
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   262
// A buffer to hold a C++ symbol. Usually small, but symbols may be larger for
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   263
// templates.
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   264
#define MINIMUM_SYMBOL_NAME_LEN 128
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   265
#define OPTIMAL_SYMBOL_NAME_LEN 1024
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   266
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   267
typedef SimpleBufferWithFallback<uint8_t,
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   268
        sizeof(IMAGEHLP_SYMBOL64) + MINIMUM_SYMBOL_NAME_LEN,
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   269
        sizeof(IMAGEHLP_SYMBOL64) + OPTIMAL_SYMBOL_NAME_LEN> SymbolBuffer;
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   270
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   271
static struct {
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   272
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   273
  // Two buffers to hold lists of loaded modules. handles across invocations of
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   274
  // SymbolEngine::recalc_search_path().
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   275
  ModuleHandleArray loaded_modules;
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   276
  ModuleHandleArray last_loaded_modules;
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   277
  // Buffer to retrieve and assemble the pdb search path.
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   278
  PathBuffer search_path;
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   279
  // Buffer to retrieve directory names for loaded modules.
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   280
  FileNameBuffer dir_name;
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   281
  // Buffer to retrieve decoded symbol information (in SymbolEngine::decode)
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   282
  SymbolBuffer decode_buffer;
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   283
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   284
  void initialize() {
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   285
    search_path.initialize();
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   286
    dir_name.initialize();
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   287
    decode_buffer.initialize();
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   288
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   289
    loaded_modules.initialize();
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   290
    last_loaded_modules.initialize();
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   291
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   292
    // Note: both module lists must have the same capacity. If one allocation
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   293
    // did fail, let them both fall back to the fallback size.
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   294
    if (loaded_modules.capacity() != last_loaded_modules.capacity()) {
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   295
      loaded_modules.reset_to_fallback_capacity();
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   296
      last_loaded_modules.reset_to_fallback_capacity();
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   297
    }
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   298
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   299
    assert(search_path.capacity() > 0 && dir_name.capacity() > 0 &&
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   300
            decode_buffer.capacity() > 0 && loaded_modules.capacity() > 0 &&
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   301
            last_loaded_modules.capacity() > 0, "Init error.");
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   302
  }
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   303
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   304
} g_buffers;
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   305
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   306
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   307
// Scan the loaded modules.
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   308
//
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   309
// For each loaded module, add the directory it is located in to the pdb search
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   310
// path, but avoid duplicates. Prior search path content is preserved.
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   311
//
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   312
// If p_search_path_was_updated is not NULL, points to a bool which, upon
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   313
// successful return from the function, contains true if the search path
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   314
// was updated, false if no update was needed because no new DLLs were
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   315
// loaded or unloaded.
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   316
//
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   317
// Returns true for success, false for error.
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   318
static bool recalc_search_path_locked(bool* p_search_path_was_updated) {
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   319
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   320
  if (p_search_path_was_updated) {
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   321
    *p_search_path_was_updated = false;
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   322
  }
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   323
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   324
  HANDLE hProcess = ::GetCurrentProcess();
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   325
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   326
  BOOL success = false;
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   327
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   328
  // 1) Retrieve current set search path.
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   329
  //    (PDB search path is a global setting and someone might have modified
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   330
  //     it, so take care not to remove directories, just to add our own).
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   331
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   332
  if (!WindowsDbgHelp::symGetSearchPath(hProcess, g_buffers.search_path.ptr(),
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   333
                                       (int)g_buffers.search_path.capacity())) {
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   334
    return false;
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   335
  }
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   336
  DEBUG_ONLY(g_buffers.search_path.check();)
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   337
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   338
  // 2) Retrieve list of modules handles of all currently loaded modules.
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   339
  DWORD bytes_needed = 0;
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   340
  const DWORD buffer_capacity_bytes = (DWORD)g_buffers.loaded_modules.capacity() * sizeof(HMODULE);
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   341
  success = ::EnumProcessModules(hProcess, g_buffers.loaded_modules.ptr(),
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   342
                                 buffer_capacity_bytes, &bytes_needed);
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   343
  DEBUG_ONLY(g_buffers.loaded_modules.check();)
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   344
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   345
  // Note: EnumProcessModules is sloppily defined in terms of whether a
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   346
  // too-small output buffer counts as error. Will it truncate but still
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   347
  // return TRUE? Nobody knows and the manpage is not telling. So we count
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   348
  // truncation it as error, disregarding the return value.
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   349
  if (!success || bytes_needed > buffer_capacity_bytes) {
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   350
    return false;
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   351
  } else {
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   352
    const int num_modules = bytes_needed / sizeof(HMODULE);
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   353
    g_buffers.loaded_modules.set_num(num_modules);
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   354
  }
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   355
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   356
  // Compare the list of module handles with the last list. If the lists are
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   357
  // identical, no additional dlls were loaded and we can stop.
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   358
  if (g_buffers.loaded_modules.equals(g_buffers.last_loaded_modules)) {
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   359
    return true;
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   360
  } else {
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   361
    // Remember the new set of module handles and continue.
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   362
    g_buffers.last_loaded_modules.copy_content_from(g_buffers.loaded_modules);
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   363
  }
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   364
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   365
  // 3) For each loaded module: retrieve directory from which it was loaded.
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   366
  //    Add directory to search path (but avoid duplicates).
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   367
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   368
  bool did_modify_searchpath = false;
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   369
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   370
  for (int i = 0; i < (int)g_buffers.loaded_modules.num(); i ++) {
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   371
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   372
    const HMODULE hMod = g_buffers.loaded_modules.ptr()[i];
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   373
    char* const filebuffer = g_buffers.dir_name.ptr();
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   374
    const int file_buffer_capacity = g_buffers.dir_name.capacity();
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   375
    const int len_returned = (int)::GetModuleFileName(hMod, filebuffer, (DWORD)file_buffer_capacity);
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   376
    DEBUG_ONLY(g_buffers.dir_name.check();)
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   377
    if (len_returned == 0) {
49355
30f39453f0a8 8193429: "assert(false) failed: GetModuleFileName failed (126)" in symbolengine.cpp
stuefe
parents: 49039
diff changeset
   378
      // This may happen when a module gets unloaded after our call to EnumProcessModules.
30f39453f0a8 8193429: "assert(false) failed: GetModuleFileName failed (126)" in symbolengine.cpp
stuefe
parents: 49039
diff changeset
   379
      // It should be rare but may sporadically happen. Just ignore and continue with the
30f39453f0a8 8193429: "assert(false) failed: GetModuleFileName failed (126)" in symbolengine.cpp
stuefe
parents: 49039
diff changeset
   380
      // next module.
30f39453f0a8 8193429: "assert(false) failed: GetModuleFileName failed (126)" in symbolengine.cpp
stuefe
parents: 49039
diff changeset
   381
      continue;
47666
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   382
    } else if (len_returned == file_buffer_capacity) {
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   383
      // Truncation. Just skip this module and continue with the next module.
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   384
      continue;
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   385
    }
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   386
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   387
    // Cut file name part off.
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   388
    char* last_slash = ::strrchr(filebuffer, '\\');
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   389
    if (last_slash == NULL) {
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   390
      last_slash = ::strrchr(filebuffer, '/');
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   391
    }
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   392
    if (last_slash) {
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   393
      *last_slash = '\0';
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   394
    }
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   395
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   396
    // If this is already part of the search path, ignore it, otherwise
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   397
    // append to search path.
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   398
    if (!g_buffers.search_path.contains_directory(filebuffer)) {
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   399
      if (!g_buffers.search_path.append_directory(filebuffer)) {
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   400
        return false; // oom
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   401
      }
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   402
      DEBUG_ONLY(g_buffers.search_path.check();)
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   403
      did_modify_searchpath = true;
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   404
    }
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   405
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   406
  } // for each loaded module.
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   407
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   408
  // If we did not modify the search path, nothing further needs to be done.
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   409
  if (!did_modify_searchpath) {
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   410
    return true;
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   411
  }
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   412
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   413
  // Set the search path to its new value.
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   414
  if (!WindowsDbgHelp::symSetSearchPath(hProcess, g_buffers.search_path.ptr())) {
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   415
    return false;
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   416
  }
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   417
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   418
  if (p_search_path_was_updated) {
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   419
    *p_search_path_was_updated = true;
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   420
  }
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   421
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   422
  return true;
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   423
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   424
}
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   425
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   426
static bool demangle_locked(const char* symbol, char *buf, int buflen) {
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   427
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   428
  return WindowsDbgHelp::unDecorateSymbolName(symbol, buf, buflen, UNDNAME_COMPLETE) > 0;
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   429
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   430
}
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   431
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   432
static bool decode_locked(const void* addr, char* buf, int buflen, int* offset, bool do_demangle) {
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   433
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   434
  assert(g_buffers.decode_buffer.capacity() >= (sizeof(IMAGEHLP_SYMBOL64) + MINIMUM_SYMBOL_NAME_LEN),
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   435
         "Decode buffer too small.");
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   436
  assert(buf != NULL && buflen > 0 && offset != NULL, "invalid output buffer.");
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   437
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   438
  DWORD64 displacement;
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   439
  PIMAGEHLP_SYMBOL64 pSymbol = NULL;
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   440
  bool success = false;
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   441
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   442
  pSymbol = (PIMAGEHLP_SYMBOL64) g_buffers.decode_buffer.ptr();
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   443
  pSymbol->SizeOfStruct = sizeof(IMAGEHLP_SYMBOL64);
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   444
  pSymbol->MaxNameLength = (DWORD)(g_buffers.decode_buffer.capacity() - sizeof(IMAGEHLP_SYMBOL64) - 1);
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   445
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   446
  // It is unclear how SymGetSymFromAddr64 handles truncation. Experiments
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   447
  // show it will return TRUE but not zero terminate (which is a really bad
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   448
  // combination). Lets be super careful.
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   449
  ::memset(pSymbol->Name, 0, pSymbol->MaxNameLength); // To catch truncation.
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   450
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   451
  if (WindowsDbgHelp::symGetSymFromAddr64(::GetCurrentProcess(), (DWORD64)addr, &displacement, pSymbol)) {
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   452
    success = true;
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   453
    if (pSymbol->Name[pSymbol->MaxNameLength - 1] != '\0') {
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   454
      // Symbol was truncated. Do not attempt to demangle. Instead, zero terminate the
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   455
      // truncated string. We still return success - the truncated string may still
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   456
      // be usable for the caller.
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   457
      pSymbol->Name[pSymbol->MaxNameLength - 1] = '\0';
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   458
      do_demangle = false;
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   459
    }
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   460
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   461
    // Attempt to demangle.
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   462
    if (do_demangle && demangle_locked(pSymbol->Name, buf, buflen)) {
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   463
      // ok.
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   464
    } else {
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   465
      ::strncpy(buf, pSymbol->Name, buflen - 1);
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   466
    }
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   467
    buf[buflen - 1] = '\0';
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   468
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   469
    *offset = (int)displacement;
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   470
  }
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   471
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   472
  DEBUG_ONLY(g_buffers.decode_buffer.check();)
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   473
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   474
  return success;
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   475
}
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   476
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   477
static enum {
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   478
  state_uninitialized = 0,
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   479
  state_ready = 1,
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   480
  state_error = 2
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   481
} g_state = state_uninitialized;
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   482
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   483
static void initialize() {
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   484
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   485
  assert(g_state == state_uninitialized, "wrong sequence");
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   486
  g_state = state_error;
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   487
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   488
  // 1) Initialize buffers.
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   489
  g_buffers.initialize();
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   490
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   491
  // 1) Call SymInitialize
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   492
  HANDLE hProcess = ::GetCurrentProcess();
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   493
  WindowsDbgHelp::symSetOptions(SYMOPT_FAIL_CRITICAL_ERRORS | SYMOPT_DEFERRED_LOADS |
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   494
                        SYMOPT_EXACT_SYMBOLS | SYMOPT_LOAD_LINES);
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   495
  if (!WindowsDbgHelp::symInitialize(hProcess, NULL, TRUE)) {
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   496
    return;
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   497
  }
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   498
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   499
  // Note: we ignore any errors from this point on. The symbol engine may be
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   500
  // usable enough.
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   501
  g_state = state_ready;
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   502
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   503
  (void)recalc_search_path_locked(NULL);
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   504
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   505
}
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   506
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   507
///////////////////// External functions //////////////////////////
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   508
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   509
// All outside facing functions are synchronized. Also, we run
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   510
// initialization on first touch.
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   511
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   512
static CRITICAL_SECTION g_cs;
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   513
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   514
namespace { // Do not export.
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   515
  class SymbolEngineEntry {
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   516
   public:
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   517
    SymbolEngineEntry() {
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   518
      ::EnterCriticalSection(&g_cs);
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   519
      if (g_state == state_uninitialized) {
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   520
        initialize();
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   521
      }
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   522
    }
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   523
    ~SymbolEngineEntry() {
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   524
      ::LeaveCriticalSection(&g_cs);
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   525
    }
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   526
  };
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   527
}
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   528
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   529
// Called at DLL_PROCESS_ATTACH.
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   530
void SymbolEngine::pre_initialize() {
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   531
  ::InitializeCriticalSection(&g_cs);
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   532
}
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   533
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   534
bool SymbolEngine::decode(const void* addr, char* buf, int buflen, int* offset, bool do_demangle) {
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   535
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   536
  assert(buf != NULL && buflen > 0 && offset != NULL, "Argument error");
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   537
  buf[0] = '\0';
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   538
  *offset = -1;
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   539
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   540
  if (addr == NULL) {
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   541
    return false;
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   542
  }
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   543
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   544
  SymbolEngineEntry entry_guard;
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   545
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   546
  // Try decoding the symbol once. If we fail, attempt to rebuild the
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   547
  // symbol search path - maybe the pc points to a dll whose pdb file is
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   548
  // outside our search path. Then do attempt the decode again.
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   549
  bool success = decode_locked(addr, buf, buflen, offset, do_demangle);
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   550
  if (!success) {
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   551
    bool did_update_search_path = false;
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   552
    if (recalc_search_path_locked(&did_update_search_path)) {
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   553
      if (did_update_search_path) {
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   554
        success = decode_locked(addr, buf, buflen, offset, do_demangle);
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   555
      }
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   556
    }
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   557
  }
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   558
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   559
  return success;
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   560
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   561
}
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   562
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   563
bool SymbolEngine::demangle(const char* symbol, char *buf, int buflen) {
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   564
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   565
  SymbolEngineEntry entry_guard;
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   566
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   567
  return demangle_locked(symbol, buf, buflen);
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   568
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   569
}
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   570
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   571
bool SymbolEngine::recalc_search_path(bool* p_search_path_was_updated) {
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   572
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   573
  SymbolEngineEntry entry_guard;
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   574
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   575
  return recalc_search_path_locked(p_search_path_was_updated);
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   576
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   577
}
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   578
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   579
bool SymbolEngine::get_source_info(const void* addr, char* buf, size_t buflen,
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   580
                                   int* line_no)
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   581
{
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   582
  assert(buf != NULL && buflen > 0 && line_no != NULL, "Argument error");
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   583
  buf[0] = '\0';
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   584
  *line_no = -1;
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   585
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   586
  if (addr == NULL) {
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   587
    return false;
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   588
  }
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   589
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   590
  SymbolEngineEntry entry_guard;
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   591
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   592
  IMAGEHLP_LINE64 lineinfo;
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   593
  memset(&lineinfo, 0, sizeof(lineinfo));
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   594
  lineinfo.SizeOfStruct = sizeof(lineinfo);
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   595
  DWORD displacement;
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   596
  if (WindowsDbgHelp::symGetLineFromAddr64(::GetCurrentProcess(), (DWORD64)addr,
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   597
                                           &displacement, &lineinfo)) {
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   598
    if (buf != NULL && buflen > 0 && lineinfo.FileName != NULL) {
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   599
      // We only return the file name, not the whole path.
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   600
      char* p = lineinfo.FileName;
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   601
      char* q = strrchr(lineinfo.FileName, '\\');
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   602
      if (q) {
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   603
        p = q + 1;
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   604
      }
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   605
      ::strncpy(buf, p, buflen - 1);
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   606
      buf[buflen - 1] = '\0';
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   607
    }
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   608
    if (line_no != 0) {
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   609
      *line_no = lineinfo.LineNumber;
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   610
    }
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   611
    return true;
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   612
  }
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   613
  return false;
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   614
}
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   615
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   616
// Print one liner describing state (if library loaded, which functions are
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   617
// missing - if any, and the dbhelp API version)
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   618
void SymbolEngine::print_state_on(outputStream* st) {
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   619
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   620
  SymbolEngineEntry entry_guard;
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   621
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   622
  st->print("symbol engine: ");
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   623
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   624
  if (g_state == state_uninitialized) {
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   625
    st->print("uninitialized.");
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   626
  } else if (g_state == state_error) {
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   627
    st->print("initialization error.");
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   628
  } else {
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   629
    st->print("initialized successfully");
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   630
    st->print(" - sym options: 0x%X", WindowsDbgHelp::symGetOptions());
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   631
    st->print(" - pdb path: ");
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   632
    if (WindowsDbgHelp::symGetSearchPath(::GetCurrentProcess(),
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   633
                                          g_buffers.search_path.ptr(),
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   634
                                          (int)g_buffers.search_path.capacity())) {
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   635
      st->print_raw(g_buffers.search_path.ptr());
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   636
    } else {
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   637
      st->print_raw("(cannot be retrieved)");
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   638
    }
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   639
  }
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   640
  st->cr();
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   641
19219ec3f176 8185712: [windows] Improve native symbol decoder
goetz
parents:
diff changeset
   642
}