src/os/windows/vm/decoder_windows.cpp

changeset 0
f90c822e73f8
child 6876
710a3c8b516e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/os/windows/vm/decoder_windows.cpp	Wed Apr 27 01:25:04 2016 +0800
     1.3 @@ -0,0 +1,278 @@
     1.4 +/*
     1.5 + * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.
    1.11 + *
    1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.15 + * version 2 for more details (a copy is included in the LICENSE file that
    1.16 + * accompanied this code).
    1.17 + *
    1.18 + * You should have received a copy of the GNU General Public License version
    1.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.21 + *
    1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.23 + * or visit www.oracle.com if you need additional information or have any
    1.24 + * questions.
    1.25 + *
    1.26 + */
    1.27 +
    1.28 +#include "precompiled.hpp"
    1.29 +#include "prims/jvm.h"
    1.30 +#include "runtime/arguments.hpp"
    1.31 +#include "decoder_windows.hpp"
    1.32 +
    1.33 +WindowsDecoder::WindowsDecoder() {
    1.34 +  _dbghelp_handle = NULL;
    1.35 +  _can_decode_in_vm = false;
    1.36 +  _pfnSymGetSymFromAddr64 = NULL;
    1.37 +  _pfnUndecorateSymbolName = NULL;
    1.38 +#ifdef AMD64
    1.39 +  _pfnStackWalk64 = NULL;
    1.40 +  _pfnSymFunctionTableAccess64 = NULL;
    1.41 +  _pfnSymGetModuleBase64 = NULL;
    1.42 +#endif
    1.43 +  _decoder_status = no_error;
    1.44 +  initialize();
    1.45 +}
    1.46 +
    1.47 +void WindowsDecoder::initialize() {
    1.48 +  if (!has_error() && _dbghelp_handle == NULL) {
    1.49 +    HMODULE handle = ::LoadLibrary("dbghelp.dll");
    1.50 +    if (!handle) {
    1.51 +      _decoder_status = helper_not_found;
    1.52 +      return;
    1.53 +    }
    1.54 +
    1.55 +    _dbghelp_handle = handle;
    1.56 +
    1.57 +    pfn_SymSetOptions _pfnSymSetOptions = (pfn_SymSetOptions)::GetProcAddress(handle, "SymSetOptions");
    1.58 +    pfn_SymInitialize _pfnSymInitialize = (pfn_SymInitialize)::GetProcAddress(handle, "SymInitialize");
    1.59 +    _pfnSymGetSymFromAddr64 = (pfn_SymGetSymFromAddr64)::GetProcAddress(handle, "SymGetSymFromAddr64");
    1.60 +    _pfnUndecorateSymbolName = (pfn_UndecorateSymbolName)::GetProcAddress(handle, "UnDecorateSymbolName");
    1.61 +
    1.62 +    if (_pfnSymSetOptions == NULL || _pfnSymInitialize == NULL || _pfnSymGetSymFromAddr64 == NULL) {
    1.63 +      uninitialize();
    1.64 +      _decoder_status = helper_func_error;
    1.65 +      return;
    1.66 +    }
    1.67 +
    1.68 +#ifdef AMD64
    1.69 +    _pfnStackWalk64 = (pfn_StackWalk64)::GetProcAddress(handle, "StackWalk64");
    1.70 +    _pfnSymFunctionTableAccess64 = (pfn_SymFunctionTableAccess64)::GetProcAddress(handle, "SymFunctionTableAccess64");
    1.71 +    _pfnSymGetModuleBase64 = (pfn_SymGetModuleBase64)::GetProcAddress(handle, "SymGetModuleBase64");
    1.72 +    if (_pfnStackWalk64 == NULL || _pfnSymFunctionTableAccess64 == NULL || _pfnSymGetModuleBase64 == NULL) {
    1.73 +      // We can't call StackWalk64 to walk the stack, but we are still
    1.74 +      // able to decode the symbols. Let's limp on.
    1.75 +      _pfnStackWalk64 = NULL;
    1.76 +      _pfnSymFunctionTableAccess64 = NULL;
    1.77 +      _pfnSymGetModuleBase64 = NULL;
    1.78 +    }
    1.79 +#endif
    1.80 +
    1.81 +    HANDLE hProcess = ::GetCurrentProcess();
    1.82 +    _pfnSymSetOptions(SYMOPT_UNDNAME | SYMOPT_DEFERRED_LOADS | SYMOPT_EXACT_SYMBOLS);
    1.83 +    if (!_pfnSymInitialize(hProcess, NULL, TRUE)) {
    1.84 +      _pfnSymGetSymFromAddr64 = NULL;
    1.85 +      _pfnUndecorateSymbolName = NULL;
    1.86 +      ::FreeLibrary(handle);
    1.87 +      _dbghelp_handle = NULL;
    1.88 +      _decoder_status = helper_init_error;
    1.89 +      return;
    1.90 +    }
    1.91 +
    1.92 +    // set pdb search paths
    1.93 +    pfn_SymSetSearchPath  _pfn_SymSetSearchPath =
    1.94 +      (pfn_SymSetSearchPath)::GetProcAddress(handle, "SymSetSearchPath");
    1.95 +    pfn_SymGetSearchPath  _pfn_SymGetSearchPath =
    1.96 +      (pfn_SymGetSearchPath)::GetProcAddress(handle, "SymGetSearchPath");
    1.97 +    if (_pfn_SymSetSearchPath != NULL && _pfn_SymGetSearchPath != NULL) {
    1.98 +      char paths[MAX_PATH];
    1.99 +      int  len = sizeof(paths);
   1.100 +      if (!_pfn_SymGetSearchPath(hProcess, paths, len)) {
   1.101 +        paths[0] = '\0';
   1.102 +      } else {
   1.103 +        // available spaces in path buffer
   1.104 +        len -= (int)strlen(paths);
   1.105 +      }
   1.106 +
   1.107 +      char tmp_path[MAX_PATH];
   1.108 +      DWORD dwSize;
   1.109 +      HMODULE hJVM = ::GetModuleHandle("jvm.dll");
   1.110 +      tmp_path[0] = '\0';
   1.111 +      // append the path where jvm.dll is located
   1.112 +      if (hJVM != NULL && (dwSize = ::GetModuleFileName(hJVM, tmp_path, sizeof(tmp_path))) > 0) {
   1.113 +        while (dwSize > 0 && tmp_path[dwSize] != '\\') {
   1.114 +          dwSize --;
   1.115 +        }
   1.116 +
   1.117 +        tmp_path[dwSize] = '\0';
   1.118 +
   1.119 +        if (dwSize > 0 && len > (int)dwSize + 1) {
   1.120 +          strncat(paths, os::path_separator(), 1);
   1.121 +          strncat(paths, tmp_path, dwSize);
   1.122 +          len -= dwSize + 1;
   1.123 +        }
   1.124 +      }
   1.125 +
   1.126 +      // append $JRE/bin. Arguments::get_java_home actually returns $JRE
   1.127 +      // path
   1.128 +      char *p = Arguments::get_java_home();
   1.129 +      assert(p != NULL, "empty java home");
   1.130 +      size_t java_home_len = strlen(p);
   1.131 +      if (len > (int)java_home_len + 5) {
   1.132 +        strncat(paths, os::path_separator(), 1);
   1.133 +        strncat(paths, p, java_home_len);
   1.134 +        strncat(paths, "\\bin", 4);
   1.135 +        len -= (int)(java_home_len + 5);
   1.136 +      }
   1.137 +
   1.138 +      // append $JDK/bin path if it exists
   1.139 +      assert(java_home_len < MAX_PATH, "Invalid path length");
   1.140 +      // assume $JRE is under $JDK, construct $JDK/bin path and
   1.141 +      // see if it exists or not
   1.142 +      if (strncmp(&p[java_home_len - 3], "jre", 3) == 0) {
   1.143 +        strncpy(tmp_path, p, java_home_len - 3);
   1.144 +        tmp_path[java_home_len - 3] = '\0';
   1.145 +        strncat(tmp_path, "bin", 3);
   1.146 +
   1.147 +        // if the directory exists
   1.148 +        DWORD dwAttrib = GetFileAttributes(tmp_path);
   1.149 +        if (dwAttrib != INVALID_FILE_ATTRIBUTES &&
   1.150 +            (dwAttrib & FILE_ATTRIBUTE_DIRECTORY)) {
   1.151 +          // tmp_path should have the same length as java_home_len, since we only
   1.152 +          // replaced 'jre' with 'bin'
   1.153 +          if (len > (int)java_home_len + 1) {
   1.154 +            strncat(paths, os::path_separator(), 1);
   1.155 +            strncat(paths, tmp_path, java_home_len);
   1.156 +          }
   1.157 +        }
   1.158 +      }
   1.159 +
   1.160 +      _pfn_SymSetSearchPath(hProcess, paths);
   1.161 +    }
   1.162 +
   1.163 +     // find out if jvm.dll contains private symbols, by decoding
   1.164 +     // current function and comparing the result
   1.165 +     address addr = (address)Decoder::demangle;
   1.166 +     char buf[MAX_PATH];
   1.167 +     if (decode(addr, buf, sizeof(buf), NULL)) {
   1.168 +       _can_decode_in_vm = !strcmp(buf, "Decoder::demangle");
   1.169 +     }
   1.170 +  }
   1.171 +}
   1.172 +
   1.173 +void WindowsDecoder::uninitialize() {
   1.174 +  _pfnSymGetSymFromAddr64 = NULL;
   1.175 +  _pfnUndecorateSymbolName = NULL;
   1.176 +#ifdef AMD64
   1.177 +  _pfnStackWalk64 = NULL;
   1.178 +  _pfnSymFunctionTableAccess64 = NULL;
   1.179 +  _pfnSymGetModuleBase64 = NULL;
   1.180 +#endif
   1.181 +  if (_dbghelp_handle != NULL) {
   1.182 +    ::FreeLibrary(_dbghelp_handle);
   1.183 +  }
   1.184 +  _dbghelp_handle = NULL;
   1.185 +}
   1.186 +
   1.187 +bool WindowsDecoder::can_decode_C_frame_in_vm() const {
   1.188 +  return  (!has_error() && _can_decode_in_vm);
   1.189 +}
   1.190 +
   1.191 +
   1.192 +bool WindowsDecoder::decode(address addr, char *buf, int buflen, int* offset, const char* modulepath)  {
   1.193 +  if (_pfnSymGetSymFromAddr64 != NULL) {
   1.194 +    PIMAGEHLP_SYMBOL64 pSymbol;
   1.195 +    char symbolInfo[MAX_PATH + sizeof(IMAGEHLP_SYMBOL64)];
   1.196 +    pSymbol = (PIMAGEHLP_SYMBOL64)symbolInfo;
   1.197 +    pSymbol->MaxNameLength = MAX_PATH;
   1.198 +    pSymbol->SizeOfStruct = sizeof(IMAGEHLP_SYMBOL64);
   1.199 +    DWORD64 displacement;
   1.200 +    if (_pfnSymGetSymFromAddr64(::GetCurrentProcess(), (DWORD64)addr, &displacement, pSymbol)) {
   1.201 +      if (buf != NULL) {
   1.202 +        if (demangle(pSymbol->Name, buf, buflen)) {
   1.203 +          jio_snprintf(buf, buflen, "%s", pSymbol->Name);
   1.204 +        }
   1.205 +      }
   1.206 +      if(offset != NULL) *offset = (int)displacement;
   1.207 +      return true;
   1.208 +    }
   1.209 +  }
   1.210 +  if (buf != NULL && buflen > 0) buf[0] = '\0';
   1.211 +  if (offset != NULL) *offset = -1;
   1.212 +  return false;
   1.213 +}
   1.214 +
   1.215 +bool WindowsDecoder::demangle(const char* symbol, char *buf, int buflen) {
   1.216 +  return _pfnUndecorateSymbolName != NULL &&
   1.217 +         _pfnUndecorateSymbolName(symbol, buf, buflen, UNDNAME_COMPLETE);
   1.218 +}
   1.219 +
   1.220 +#ifdef AMD64
   1.221 +BOOL WindowsDbgHelp::StackWalk64(DWORD MachineType,
   1.222 +                                 HANDLE hProcess,
   1.223 +                                 HANDLE hThread,
   1.224 +                                 LPSTACKFRAME64 StackFrame,
   1.225 +                                 PVOID ContextRecord,
   1.226 +                                 PREAD_PROCESS_MEMORY_ROUTINE64 ReadMemoryRoutine,
   1.227 +                                 PFUNCTION_TABLE_ACCESS_ROUTINE64 FunctionTableAccessRoutine,
   1.228 +                                 PGET_MODULE_BASE_ROUTINE64 GetModuleBaseRoutine,
   1.229 +                                 PTRANSLATE_ADDRESS_ROUTINE64 TranslateAddress) {
   1.230 +  DecoderLocker locker;
   1.231 +  WindowsDecoder* wd = (WindowsDecoder*)locker.decoder();
   1.232 +
   1.233 +  if (!wd->has_error() && wd->_pfnStackWalk64) {
   1.234 +    return wd->_pfnStackWalk64(MachineType,
   1.235 +                               hProcess,
   1.236 +                               hThread,
   1.237 +                               StackFrame,
   1.238 +                               ContextRecord,
   1.239 +                               ReadMemoryRoutine,
   1.240 +                               FunctionTableAccessRoutine,
   1.241 +                               GetModuleBaseRoutine,
   1.242 +                               TranslateAddress);
   1.243 +  } else {
   1.244 +    return false;
   1.245 +  }
   1.246 +}
   1.247 +
   1.248 +PVOID WindowsDbgHelp::SymFunctionTableAccess64(HANDLE hProcess, DWORD64 AddrBase) {
   1.249 +  DecoderLocker locker;
   1.250 +  WindowsDecoder* wd = (WindowsDecoder*)locker.decoder();
   1.251 +
   1.252 +  if (!wd->has_error() && wd->_pfnSymFunctionTableAccess64) {
   1.253 +    return wd->_pfnSymFunctionTableAccess64(hProcess, AddrBase);
   1.254 +  } else {
   1.255 +    return NULL;
   1.256 +  }
   1.257 +}
   1.258 +
   1.259 +pfn_SymFunctionTableAccess64 WindowsDbgHelp::pfnSymFunctionTableAccess64() {
   1.260 +  DecoderLocker locker;
   1.261 +  WindowsDecoder* wd = (WindowsDecoder*)locker.decoder();
   1.262 +
   1.263 +  if (!wd->has_error()) {
   1.264 +    return wd->_pfnSymFunctionTableAccess64;
   1.265 +  } else {
   1.266 +    return NULL;
   1.267 +  }
   1.268 +}
   1.269 +
   1.270 +pfn_SymGetModuleBase64 WindowsDbgHelp::pfnSymGetModuleBase64() {
   1.271 +  DecoderLocker locker;
   1.272 +  WindowsDecoder* wd = (WindowsDecoder*)locker.decoder();
   1.273 +
   1.274 +  if (!wd->has_error()) {
   1.275 +    return wd->_pfnSymGetModuleBase64;
   1.276 +  } else {
   1.277 +    return NULL;
   1.278 +  }
   1.279 +}
   1.280 +
   1.281 +#endif // AMD64

mercurial