1 /*
   2  * Copyright (c) 1997, 2017, 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 "runtime/arguments.hpp"
  28 #include "runtime/os.hpp"
  29 #include "decoder_windows.hpp"
  30 #include "windbghelp.hpp"
  31 
  32 WindowsDecoder::WindowsDecoder() {
  33   _can_decode_in_vm = true;
  34   _decoder_status = no_error;
  35   initialize();
  36 }
  37 
  38 void WindowsDecoder::initialize() {
  39   if (!has_error()) {
  40     HANDLE hProcess = ::GetCurrentProcess();
  41     WindowsDbgHelp::symSetOptions(SYMOPT_UNDNAME | SYMOPT_DEFERRED_LOADS | SYMOPT_EXACT_SYMBOLS);
  42     if (!WindowsDbgHelp::symInitialize(hProcess, NULL, TRUE)) {
  43       _decoder_status = helper_init_error;
  44       return;
  45     }
  46 
  47     // set pdb search paths
  48     char paths[MAX_PATH];
  49     int  len = sizeof(paths);
  50     if (!WindowsDbgHelp::symGetSearchPath(hProcess, paths, len)) {
  51       paths[0] = '\0';
  52     } else {
  53       // available spaces in path buffer
  54       len -= (int)strlen(paths);
  55     }
  56 
  57     char tmp_path[MAX_PATH];
  58     DWORD dwSize;
  59     HMODULE hJVM = ::GetModuleHandle("jvm.dll");
  60     tmp_path[0] = '\0';
  61     // append the path where jvm.dll is located
  62     if (hJVM != NULL && (dwSize = ::GetModuleFileName(hJVM, tmp_path, sizeof(tmp_path))) > 0) {
  63       while (dwSize > 0 && tmp_path[dwSize] != '\\') {
  64         dwSize --;
  65       }
  66 
  67       tmp_path[dwSize] = '\0';
  68 
  69       if (dwSize > 0 && len > (int)dwSize + 1) {
  70         strncat(paths, os::path_separator(), 1);
  71         strncat(paths, tmp_path, dwSize);
  72         len -= dwSize + 1;
  73       }
  74     }
  75 
  76     // append $JRE/bin. Arguments::get_java_home actually returns $JRE
  77     // path
  78     char *p = Arguments::get_java_home();
  79     assert(p != NULL, "empty java home");
  80     size_t java_home_len = strlen(p);
  81     if (len > (int)java_home_len + 5) {
  82       strncat(paths, os::path_separator(), 1);
  83       strncat(paths, p, java_home_len);
  84       strncat(paths, "\\bin", 4);
  85       len -= (int)(java_home_len + 5);
  86     }
  87 
  88     // append $JDK/bin path if it exists
  89     assert(java_home_len < MAX_PATH, "Invalid path length");
  90     // assume $JRE is under $JDK, construct $JDK/bin path and
  91     // see if it exists or not
  92     if (strncmp(&p[java_home_len - 3], "jre", 3) == 0) {
  93       strncpy(tmp_path, p, java_home_len - 3);
  94       tmp_path[java_home_len - 3] = '\0';
  95       strncat(tmp_path, "bin", 3);
  96 
  97       // if the directory exists
  98       DWORD dwAttrib = GetFileAttributes(tmp_path);
  99       if (dwAttrib != INVALID_FILE_ATTRIBUTES &&
 100           (dwAttrib & FILE_ATTRIBUTE_DIRECTORY)) {
 101         // tmp_path should have the same length as java_home_len, since we only
 102         // replaced 'jre' with 'bin'
 103         if (len > (int)java_home_len + 1) {
 104           strncat(paths, os::path_separator(), 1);
 105           strncat(paths, tmp_path, java_home_len);
 106         }
 107       }
 108     }
 109 
 110     WindowsDbgHelp::symSetSearchPath(hProcess, paths);
 111 
 112     // find out if jvm.dll contains private symbols, by decoding
 113     // current function and comparing the result
 114     address addr = (address)Decoder::demangle;
 115     char buf[MAX_PATH];
 116     if (decode(addr, buf, sizeof(buf), NULL, NULL, true /* demangle */)) {
 117       _can_decode_in_vm = !strcmp(buf, "Decoder::demangle");
 118     }
 119   }
 120 }
 121 
 122 void WindowsDecoder::uninitialize() {}
 123 
 124 bool WindowsDecoder::can_decode_C_frame_in_vm() const {
 125   return  (!has_error() && _can_decode_in_vm);
 126 }
 127 
 128 
 129 bool WindowsDecoder::decode(address addr, char *buf, int buflen, int* offset, const char* modulepath, bool demangle_name)  {
 130   if (!has_error()) {
 131     PIMAGEHLP_SYMBOL64 pSymbol;
 132     char symbolInfo[MAX_PATH + sizeof(IMAGEHLP_SYMBOL64)];
 133     pSymbol = (PIMAGEHLP_SYMBOL64)symbolInfo;
 134     pSymbol->MaxNameLength = MAX_PATH;
 135     pSymbol->SizeOfStruct = sizeof(IMAGEHLP_SYMBOL64);
 136     DWORD64 displacement;
 137     if (WindowsDbgHelp::symGetSymFromAddr64(::GetCurrentProcess(), (DWORD64)addr, &displacement, pSymbol)) {
 138       if (buf != NULL) {
 139         if (!(demangle_name && demangle(pSymbol->Name, buf, buflen))) {
 140           jio_snprintf(buf, buflen, "%s", pSymbol->Name);
 141         }
 142       }
 143       if(offset != NULL) *offset = (int)displacement;
 144       return true;
 145     }
 146   }
 147   if (buf != NULL && buflen > 0) buf[0] = '\0';
 148   if (offset != NULL) *offset = -1;
 149   return false;
 150 }
 151 
 152 bool WindowsDecoder::demangle(const char* symbol, char *buf, int buflen) {
 153   if (!has_error()) {
 154     return WindowsDbgHelp::unDecorateSymbolName(symbol, buf, buflen, UNDNAME_COMPLETE) > 0;
 155   }
 156   return false;
 157 }
 158