src/os/windows/vm/decoder_windows.cpp

Sat, 11 Dec 2010 13:20:56 -0500

author
zgu
date
Sat, 11 Dec 2010 13:20:56 -0500
changeset 2364
2d4762ec74af
child 3031
b1cbb0907b36
permissions
-rw-r--r--

7003748: Decode C stack frames when symbols are presented (PhoneHome project)
Summary: Implemented in-process C native stack frame decoding when symbols are available.
Reviewed-by: coleenp, never

     1 /*
     2  * Copyright (c) 1997, 2010, 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  */
    25 #include "precompiled.hpp"
    26 #include "prims/jvm.h"
    27 #include "utilities/decoder.hpp"
    29 HMODULE                   Decoder::_dbghelp_handle = NULL;
    30 bool                      Decoder::_can_decode_in_vm = false;
    31 pfn_SymGetSymFromAddr64   Decoder::_pfnSymGetSymFromAddr64 = NULL;
    32 pfn_UndecorateSymbolName  Decoder::_pfnUndecorateSymbolName = NULL;
    34 void Decoder::initialize() {
    35   if (!_initialized) {
    36     _initialized = true;
    38     HMODULE handle = ::LoadLibrary("dbghelp.dll");
    39     if (!handle) {
    40       _decoder_status = helper_not_found;
    41         return;
    42     }
    44     _dbghelp_handle = handle;
    46     pfn_SymSetOptions _pfnSymSetOptions = (pfn_SymSetOptions)::GetProcAddress(handle, "SymSetOptions");
    47     pfn_SymInitialize _pfnSymInitialize = (pfn_SymInitialize)::GetProcAddress(handle, "SymInitialize");
    48     _pfnSymGetSymFromAddr64 = (pfn_SymGetSymFromAddr64)::GetProcAddress(handle, "SymGetSymFromAddr64");
    49     _pfnUndecorateSymbolName = (pfn_UndecorateSymbolName)GetProcAddress(handle, "UnDecorateSymbolName");
    51     if (_pfnSymSetOptions == NULL || _pfnSymInitialize == NULL || _pfnSymGetSymFromAddr64 == NULL) {
    52       _pfnSymGetSymFromAddr64 = NULL;
    53       _pfnUndecorateSymbolName = NULL;
    54       ::FreeLibrary(handle);
    55       _dbghelp_handle = NULL;
    56       _decoder_status = helper_func_error;
    57       return;
    58     }
    60     _pfnSymSetOptions(SYMOPT_UNDNAME | SYMOPT_DEFERRED_LOADS);
    61     if (!_pfnSymInitialize(GetCurrentProcess(), NULL, TRUE)) {
    62       _pfnSymGetSymFromAddr64 = NULL;
    63       _pfnUndecorateSymbolName = NULL;
    64       ::FreeLibrary(handle);
    65       _dbghelp_handle = NULL;
    66       _decoder_status = helper_init_error;
    67       return;
    68     }
    70      // find out if jvm.dll contains private symbols, by decoding
    71      // current function and comparing the result
    72      address addr = (address)Decoder::initialize;
    73      char buf[MAX_PATH];
    74      if (decode(addr, buf, sizeof(buf), NULL) == no_error) {
    75        _can_decode_in_vm = !strcmp(buf, "Decoder::initialize");
    76      }
    77   }
    78 }
    80 void Decoder::uninitialize() {
    81   assert(_initialized, "Decoder not yet initialized");
    82   _pfnSymGetSymFromAddr64 = NULL;
    83   _pfnUndecorateSymbolName = NULL;
    84   if (_dbghelp_handle != NULL) {
    85     ::FreeLibrary(_dbghelp_handle);
    86   }
    87   _initialized = false;
    88 }
    90 bool Decoder::can_decode_C_frame_in_vm() {
    91   initialize();
    92   return  _can_decode_in_vm;
    93 }
    96 Decoder::decoder_status Decoder::decode(address addr, char *buf, int buflen, int *offset) {
    97   assert(_initialized, "Decoder not yet initialized");
    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 no_error;
   113     }
   114   }
   115   return helper_not_found;
   116 }
   118 bool Decoder::demangle(const char* symbol, char *buf, int buflen) {
   119   assert(_initialized, "Decoder not yet initialized");
   120   return _pfnUndecorateSymbolName != NULL &&
   121          _pfnUndecorateSymbolName(symbol, buf, buflen, UNDNAME_COMPLETE);
   122 }

mercurial