1 /* 2 * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 * 23 */ 24 25 #include "precompiled.hpp" 26 #include "prims/jvm.h" 27 #include "decoder_windows.hpp" 28 29 WindowsDecoder::WindowsDecoder() { 30 _dbghelp_handle = NULL; 31 _can_decode_in_vm = false; 32 _pfnSymGetSymFromAddr64 = NULL; 33 _pfnUndecorateSymbolName = NULL; 34 35 _decoder_status = no_error; 36 initialize(); 37 } 38 39 void WindowsDecoder::initialize() { 40 if (!has_error() && _dbghelp_handle == NULL) { 41 HMODULE handle = ::LoadLibrary("dbghelp.dll"); 42 if (!handle) { 43 _decoder_status = helper_not_found; 44 return; 45 } 46 47 _dbghelp_handle = handle; 48 49 pfn_SymSetOptions _pfnSymSetOptions = (pfn_SymSetOptions)::GetProcAddress(handle, "SymSetOptions"); 50 pfn_SymInitialize _pfnSymInitialize = (pfn_SymInitialize)::GetProcAddress(handle, "SymInitialize"); 51 _pfnSymGetSymFromAddr64 = (pfn_SymGetSymFromAddr64)::GetProcAddress(handle, "SymGetSymFromAddr64"); 52 _pfnUndecorateSymbolName = (pfn_UndecorateSymbolName)::GetProcAddress(handle, "UnDecorateSymbolName"); 53 54 if (_pfnSymSetOptions == NULL || _pfnSymInitialize == NULL || _pfnSymGetSymFromAddr64 == NULL) { 55 _pfnSymGetSymFromAddr64 = NULL; 56 _pfnUndecorateSymbolName = NULL; 57 ::FreeLibrary(handle); 58 _dbghelp_handle = NULL; 59 _decoder_status = helper_func_error; 60 return; 61 } 62 63 HANDLE hProcess = ::GetCurrentProcess(); 64 _pfnSymSetOptions(SYMOPT_UNDNAME | SYMOPT_DEFERRED_LOADS | SYMOPT_EXACT_SYMBOLS); 65 if (!_pfnSymInitialize(hProcess, NULL, TRUE)) { 66 _pfnSymGetSymFromAddr64 = NULL; 67 _pfnUndecorateSymbolName = NULL; 68 ::FreeLibrary(handle); 69 _dbghelp_handle = NULL; 70 _decoder_status = helper_init_error; 71 return; 72 } 73 74 // set pdb search paths 75 pfn_SymSetSearchPath _pfn_SymSetSearchPath = 76 (pfn_SymSetSearchPath)::GetProcAddress(handle, "SymSetSearchPath"); 77 pfn_SymGetSearchPath _pfn_SymGetSearchPath = 78 (pfn_SymGetSearchPath)::GetProcAddress(handle, "SymGetSearchPath"); 79 if (_pfn_SymSetSearchPath != NULL && _pfn_SymGetSearchPath != NULL) { 80 char paths[MAX_PATH]; 81 int len = sizeof(paths); 82 if (!_pfn_SymGetSearchPath(hProcess, paths, len)) { 83 paths[0] = '\0'; 84 } else { 85 // available spaces in path buffer 86 len -= (int)strlen(paths); 87 } 88 89 char tmp_path[MAX_PATH]; 90 DWORD dwSize; 91 HMODULE hJVM = ::GetModuleHandle("jvm.dll"); 92 tmp_path[0] = '\0'; 93 // append the path where jvm.dll is located 94 if (hJVM != NULL && (dwSize = ::GetModuleFileName(hJVM, tmp_path, sizeof(tmp_path))) > 0) { 95 while (dwSize > 0 && tmp_path[dwSize] != '\\') { 96 dwSize --; 97 } 98 99 tmp_path[dwSize] = '\0'; 100 101 if (dwSize > 0 && len > (int)dwSize + 1) { 102 strncat(paths, os::path_separator(), 1); 103 strncat(paths, tmp_path, dwSize); 104 len -= dwSize + 1; 105 } 106 } 107 108 // append $JRE/bin. Arguments::get_java_home actually returns $JRE 109 // path 110 char *p = Arguments::get_java_home(); 111 assert(p != NULL, "empty java home"); 112 size_t java_home_len = strlen(p); 113 if (len > (int)java_home_len + 5) { 114 strncat(paths, os::path_separator(), 1); 115 strncat(paths, p, java_home_len); 116 strncat(paths, "\\bin", 4); 117 len -= (int)(java_home_len + 5); 118 } 119 120 // append $JDK/bin path if it exists 121 assert(java_home_len < MAX_PATH, "Invalid path length"); 122 // assume $JRE is under $JDK, construct $JDK/bin path and 123 // see if it exists or not 124 if (strncmp(&p[java_home_len - 3], "jre", 3) == 0) { 125 strncpy(tmp_path, p, java_home_len - 3); 126 tmp_path[java_home_len - 3] = '\0'; 127 strncat(tmp_path, "bin", 3); 128 129 // if the directory exists 130 DWORD dwAttrib = GetFileAttributes(tmp_path); 131 if (dwAttrib != INVALID_FILE_ATTRIBUTES && 132 (dwAttrib & FILE_ATTRIBUTE_DIRECTORY)) { 133 // tmp_path should have the same length as java_home_len, since we only 134 // replaced 'jre' with 'bin' 135 if (len > (int)java_home_len + 1) { 136 strncat(paths, os::path_separator(), 1); 137 strncat(paths, tmp_path, java_home_len); 138 } 139 } 140 } 141 142 _pfn_SymSetSearchPath(hProcess, paths); 143 } 144 145 // find out if jvm.dll contains private symbols, by decoding 146 // current function and comparing the result 147 address addr = (address)Decoder::demangle; 148 char buf[MAX_PATH]; 149 if (decode(addr, buf, sizeof(buf), NULL)) { 150 _can_decode_in_vm = !strcmp(buf, "Decoder::demangle"); 151 } 152 } 153 } 154 155 void WindowsDecoder::uninitialize() { 156 _pfnSymGetSymFromAddr64 = NULL; 157 _pfnUndecorateSymbolName = NULL; 158 if (_dbghelp_handle != NULL) { 159 ::FreeLibrary(_dbghelp_handle); 160 } 161 _dbghelp_handle = NULL; 162 } 163 164 bool WindowsDecoder::can_decode_C_frame_in_vm() const { 165 return (!has_error() && _can_decode_in_vm); 166 } 167 168 169 bool WindowsDecoder::decode(address addr, char *buf, int buflen, int* offset, const char* modulepath) { 170 if (_pfnSymGetSymFromAddr64 != NULL) { 171 PIMAGEHLP_SYMBOL64 pSymbol; 172 char symbolInfo[MAX_PATH + sizeof(IMAGEHLP_SYMBOL64)]; 173 pSymbol = (PIMAGEHLP_SYMBOL64)symbolInfo; 174 pSymbol->MaxNameLength = MAX_PATH; 175 pSymbol->SizeOfStruct = sizeof(IMAGEHLP_SYMBOL64); 176 DWORD64 displacement; 177 if (_pfnSymGetSymFromAddr64(::GetCurrentProcess(), (DWORD64)addr, &displacement, pSymbol)) { 178 if (buf != NULL) { 179 if (demangle(pSymbol->Name, buf, buflen)) { 180 jio_snprintf(buf, buflen, "%s", pSymbol->Name); 181 } 182 } 183 if(offset != NULL) *offset = (int)displacement; 184 return true; 185 } 186 } 187 if (buf != NULL && buflen > 0) buf[0] = '\0'; 188 if (offset != NULL) *offset = -1; 189 return false; 190 } 191 192 bool WindowsDecoder::demangle(const char* symbol, char *buf, int buflen) { 193 return _pfnUndecorateSymbolName != NULL && 194 _pfnUndecorateSymbolName(symbol, buf, buflen, UNDNAME_COMPLETE); 195 } 196