hotspot/src/os/windows/vm/decoder_windows.cpp
author zgu
Mon, 30 Jul 2012 10:25:52 -0400
changeset 13340 40c424a6ff53
parent 11483 4d3f4bca0019
child 13963 e5b53c306fb5
permissions -rw-r--r--
7186778: MachO decoder implementation for MacOSX Summary: Implementation of decoder for Apple's MacOSX. The implementation is based on the patch provided by Kevin Walls. Reviewed-by: coleenp, kamg, kevinw
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
7447
32c42d627f41 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
     1
/*
11483
4d3f4bca0019 7071311: Decoder enhancement
zgu
parents: 10246
diff changeset
     2
 * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
7447
32c42d627f41 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
32c42d627f41 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
     4
 *
32c42d627f41 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
32c42d627f41 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
32c42d627f41 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
     7
 * published by the Free Software Foundation.
32c42d627f41 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
     8
 *
32c42d627f41 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
32c42d627f41 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
32c42d627f41 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
32c42d627f41 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
32c42d627f41 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
    13
 * accompanied this code).
32c42d627f41 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
    14
 *
32c42d627f41 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
32c42d627f41 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
32c42d627f41 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
32c42d627f41 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
    18
 *
32c42d627f41 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
32c42d627f41 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
32c42d627f41 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
    21
 * questions.
32c42d627f41 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
    22
 *
32c42d627f41 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
    23
 */
32c42d627f41 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
    24
32c42d627f41 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
    25
#include "precompiled.hpp"
32c42d627f41 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
    26
#include "prims/jvm.h"
11483
4d3f4bca0019 7071311: Decoder enhancement
zgu
parents: 10246
diff changeset
    27
#include "decoder_windows.hpp"
4d3f4bca0019 7071311: Decoder enhancement
zgu
parents: 10246
diff changeset
    28
4d3f4bca0019 7071311: Decoder enhancement
zgu
parents: 10246
diff changeset
    29
WindowsDecoder::WindowsDecoder() {
4d3f4bca0019 7071311: Decoder enhancement
zgu
parents: 10246
diff changeset
    30
  _dbghelp_handle = NULL;
4d3f4bca0019 7071311: Decoder enhancement
zgu
parents: 10246
diff changeset
    31
  _can_decode_in_vm = false;
4d3f4bca0019 7071311: Decoder enhancement
zgu
parents: 10246
diff changeset
    32
  _pfnSymGetSymFromAddr64 = NULL;
4d3f4bca0019 7071311: Decoder enhancement
zgu
parents: 10246
diff changeset
    33
  _pfnUndecorateSymbolName = NULL;
7447
32c42d627f41 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
    34
11483
4d3f4bca0019 7071311: Decoder enhancement
zgu
parents: 10246
diff changeset
    35
  _decoder_status = no_error;
4d3f4bca0019 7071311: Decoder enhancement
zgu
parents: 10246
diff changeset
    36
  initialize();
4d3f4bca0019 7071311: Decoder enhancement
zgu
parents: 10246
diff changeset
    37
}
7447
32c42d627f41 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
    38
11483
4d3f4bca0019 7071311: Decoder enhancement
zgu
parents: 10246
diff changeset
    39
void WindowsDecoder::initialize() {
4d3f4bca0019 7071311: Decoder enhancement
zgu
parents: 10246
diff changeset
    40
  if (!has_error() && _dbghelp_handle == NULL) {
4d3f4bca0019 7071311: Decoder enhancement
zgu
parents: 10246
diff changeset
    41
    HMODULE handle = ::LoadLibrary("dbghelp.dll");
7447
32c42d627f41 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
    42
    if (!handle) {
32c42d627f41 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
    43
      _decoder_status = helper_not_found;
11483
4d3f4bca0019 7071311: Decoder enhancement
zgu
parents: 10246
diff changeset
    44
      return;
7447
32c42d627f41 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
    45
    }
32c42d627f41 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
    46
32c42d627f41 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
    47
    _dbghelp_handle = handle;
32c42d627f41 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
    48
32c42d627f41 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
    49
    pfn_SymSetOptions _pfnSymSetOptions = (pfn_SymSetOptions)::GetProcAddress(handle, "SymSetOptions");
32c42d627f41 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
    50
    pfn_SymInitialize _pfnSymInitialize = (pfn_SymInitialize)::GetProcAddress(handle, "SymInitialize");
32c42d627f41 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
    51
    _pfnSymGetSymFromAddr64 = (pfn_SymGetSymFromAddr64)::GetProcAddress(handle, "SymGetSymFromAddr64");
32c42d627f41 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
    52
    _pfnUndecorateSymbolName = (pfn_UndecorateSymbolName)GetProcAddress(handle, "UnDecorateSymbolName");
32c42d627f41 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
    53
32c42d627f41 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
    54
    if (_pfnSymSetOptions == NULL || _pfnSymInitialize == NULL || _pfnSymGetSymFromAddr64 == NULL) {
32c42d627f41 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
    55
      _pfnSymGetSymFromAddr64 = NULL;
32c42d627f41 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
    56
      _pfnUndecorateSymbolName = NULL;
32c42d627f41 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
    57
      ::FreeLibrary(handle);
32c42d627f41 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
    58
      _dbghelp_handle = NULL;
32c42d627f41 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
    59
      _decoder_status = helper_func_error;
32c42d627f41 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
    60
      return;
32c42d627f41 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
    61
    }
32c42d627f41 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
    62
32c42d627f41 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
    63
    _pfnSymSetOptions(SYMOPT_UNDNAME | SYMOPT_DEFERRED_LOADS);
32c42d627f41 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
    64
    if (!_pfnSymInitialize(GetCurrentProcess(), NULL, TRUE)) {
32c42d627f41 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
    65
      _pfnSymGetSymFromAddr64 = NULL;
32c42d627f41 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
    66
      _pfnUndecorateSymbolName = NULL;
32c42d627f41 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
    67
      ::FreeLibrary(handle);
32c42d627f41 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
    68
      _dbghelp_handle = NULL;
32c42d627f41 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
    69
      _decoder_status = helper_init_error;
32c42d627f41 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
    70
      return;
32c42d627f41 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
    71
    }
32c42d627f41 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
    72
32c42d627f41 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
    73
     // find out if jvm.dll contains private symbols, by decoding
32c42d627f41 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
    74
     // current function and comparing the result
13340
40c424a6ff53 7186778: MachO decoder implementation for MacOSX
zgu
parents: 11483
diff changeset
    75
     address addr = (address)Decoder::demangle;
7447
32c42d627f41 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
    76
     char buf[MAX_PATH];
11483
4d3f4bca0019 7071311: Decoder enhancement
zgu
parents: 10246
diff changeset
    77
     if (decode(addr, buf, sizeof(buf), NULL)) {
13340
40c424a6ff53 7186778: MachO decoder implementation for MacOSX
zgu
parents: 11483
diff changeset
    78
       _can_decode_in_vm = !strcmp(buf, "Decoder::demangle");
7447
32c42d627f41 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
    79
     }
32c42d627f41 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
    80
  }
32c42d627f41 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
    81
}
32c42d627f41 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
    82
11483
4d3f4bca0019 7071311: Decoder enhancement
zgu
parents: 10246
diff changeset
    83
void WindowsDecoder::uninitialize() {
7447
32c42d627f41 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
    84
  _pfnSymGetSymFromAddr64 = NULL;
32c42d627f41 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
    85
  _pfnUndecorateSymbolName = NULL;
32c42d627f41 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
    86
  if (_dbghelp_handle != NULL) {
32c42d627f41 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
    87
    ::FreeLibrary(_dbghelp_handle);
32c42d627f41 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
    88
  }
11483
4d3f4bca0019 7071311: Decoder enhancement
zgu
parents: 10246
diff changeset
    89
  _dbghelp_handle = NULL;
7447
32c42d627f41 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
    90
}
32c42d627f41 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
    91
11483
4d3f4bca0019 7071311: Decoder enhancement
zgu
parents: 10246
diff changeset
    92
bool WindowsDecoder::can_decode_C_frame_in_vm() const {
4d3f4bca0019 7071311: Decoder enhancement
zgu
parents: 10246
diff changeset
    93
  return  (!has_error() && _can_decode_in_vm);
7447
32c42d627f41 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
    94
}
32c42d627f41 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
    95
32c42d627f41 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
    96
11483
4d3f4bca0019 7071311: Decoder enhancement
zgu
parents: 10246
diff changeset
    97
bool WindowsDecoder::decode(address addr, char *buf, int buflen, int* offset, const char* modulepath)  {
7447
32c42d627f41 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
    98
  if (_pfnSymGetSymFromAddr64 != NULL) {
32c42d627f41 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
    99
    PIMAGEHLP_SYMBOL64 pSymbol;
32c42d627f41 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
   100
    char symbolInfo[MAX_PATH + sizeof(IMAGEHLP_SYMBOL64)];
32c42d627f41 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
   101
    pSymbol = (PIMAGEHLP_SYMBOL64)symbolInfo;
32c42d627f41 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
   102
    pSymbol->MaxNameLength = MAX_PATH;
32c42d627f41 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
   103
    pSymbol->SizeOfStruct = sizeof(IMAGEHLP_SYMBOL64);
32c42d627f41 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
   104
    DWORD64 displacement;
32c42d627f41 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
   105
    if (_pfnSymGetSymFromAddr64(::GetCurrentProcess(), (DWORD64)addr, &displacement, pSymbol)) {
32c42d627f41 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
   106
      if (buf != NULL) {
11483
4d3f4bca0019 7071311: Decoder enhancement
zgu
parents: 10246
diff changeset
   107
        if (demangle(pSymbol->Name, buf, buflen)) {
7447
32c42d627f41 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
   108
          jio_snprintf(buf, buflen, "%s", pSymbol->Name);
32c42d627f41 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
   109
        }
32c42d627f41 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
   110
      }
11483
4d3f4bca0019 7071311: Decoder enhancement
zgu
parents: 10246
diff changeset
   111
      if(offset != NULL) *offset = (int)displacement;
4d3f4bca0019 7071311: Decoder enhancement
zgu
parents: 10246
diff changeset
   112
      return true;
7447
32c42d627f41 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
   113
    }
32c42d627f41 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
   114
  }
11483
4d3f4bca0019 7071311: Decoder enhancement
zgu
parents: 10246
diff changeset
   115
  if (buf != NULL && buflen > 0) buf[0] = '\0';
4d3f4bca0019 7071311: Decoder enhancement
zgu
parents: 10246
diff changeset
   116
  if (offset != NULL) *offset = -1;
4d3f4bca0019 7071311: Decoder enhancement
zgu
parents: 10246
diff changeset
   117
  return false;
7447
32c42d627f41 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
   118
}
32c42d627f41 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
   119
11483
4d3f4bca0019 7071311: Decoder enhancement
zgu
parents: 10246
diff changeset
   120
bool WindowsDecoder::demangle(const char* symbol, char *buf, int buflen) {
7447
32c42d627f41 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
   121
  return _pfnUndecorateSymbolName != NULL &&
32c42d627f41 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
   122
         _pfnUndecorateSymbolName(symbol, buf, buflen, UNDNAME_COMPLETE);
32c42d627f41 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
   123
}
32c42d627f41 7003748: Decode C stack frames when symbols are presented (PhoneHome project)
zgu
parents:
diff changeset
   124