1 /*
   2  * Copyright (c) 1997, 2011, 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     _pfnSymSetOptions(SYMOPT_UNDNAME | SYMOPT_DEFERRED_LOADS);
  64     if (!_pfnSymInitialize(GetCurrentProcess(), NULL, TRUE)) {
  65       _pfnSymGetSymFromAddr64 = NULL;
  66       _pfnUndecorateSymbolName = NULL;
  67       ::FreeLibrary(handle);
  68       _dbghelp_handle = NULL;
  69       _decoder_status = helper_init_error;
  70       return;
  71     }
  72 
  73      // find out if jvm.dll contains private symbols, by decoding
  74      // current function and comparing the result
  75      address addr = (address)Decoder::decode;
  76      char buf[MAX_PATH];
  77      if (decode(addr, buf, sizeof(buf), NULL)) {
  78        _can_decode_in_vm = !strcmp(buf, "Decoder::decode");
  79      }
  80   }
  81 }
  82 
  83 void WindowsDecoder::uninitialize() {
  84   _pfnSymGetSymFromAddr64 = NULL;
  85   _pfnUndecorateSymbolName = NULL;
  86   if (_dbghelp_handle != NULL) {
  87     ::FreeLibrary(_dbghelp_handle);
  88   }
  89   _dbghelp_handle = NULL;
  90 }
  91 
  92 bool WindowsDecoder::can_decode_C_frame_in_vm() const {
  93   return  (!has_error() && _can_decode_in_vm);
  94 }
  95 
  96 
  97 bool WindowsDecoder::decode(address addr, char *buf, int buflen, int* offset, const char* modulepath)  {
  98   if (_pfnSymGetSymFromAddr64 != NULL) {
  99     PIMAGEHLP_SYMBOL64 pSymbol;
 100     char symbolInfo[MAX_PATH + sizeof(IMAGEHLP_SYMBOL64)];
 101     pSymbol = (PIMAGEHLP_SYMBOL64)symbolInfo;
 102     pSymbol->MaxNameLength = MAX_PATH;
 103     pSymbol->SizeOfStruct = sizeof(IMAGEHLP_SYMBOL64);
 104     DWORD64 displacement;
 105     if (_pfnSymGetSymFromAddr64(::GetCurrentProcess(), (DWORD64)addr, &displacement, pSymbol)) {
 106       if (buf != NULL) {
 107         if (demangle(pSymbol->Name, buf, buflen)) {
 108           jio_snprintf(buf, buflen, "%s", pSymbol->Name);
 109         }
 110       }
 111       if(offset != NULL) *offset = (int)displacement;
 112       return true;
 113     }
 114   }
 115   if (buf != NULL && buflen > 0) buf[0] = '\0';
 116   if (offset != NULL) *offset = -1;
 117   return false;
 118 }
 119 
 120 bool WindowsDecoder::demangle(const char* symbol, char *buf, int buflen) {
 121   return _pfnUndecorateSymbolName != NULL &&
 122          _pfnUndecorateSymbolName(symbol, buf, buflen, UNDNAME_COMPLETE);
 123 }
 124