hotspot/agent/src/share/native/sadis.c
author minqi
Mon, 08 Oct 2012 16:48:52 -0700
changeset 13959 bcadb9151203
parent 13873 7b72e3873785
child 14847 92a59a418262
permissions -rw-r--r--
8000332: SA ClassDump throws exception after permgen removal Summary: In ClassWrite.writeFields(), fields count was mistakenly set to fields length which overflow the array index. Also removed a file which is leftover from 6879063 changeset. Reviewed-by: coleenp, sspitsyn Contributed-by: yumin.qi@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
13873
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
     1
/*
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
     2
 * Copyright 2012, Oracle and/or its affiliates. All Rights Reserved.
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
     4
 *
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
     7
 * published by the Free Software Foundation.
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
     8
 *
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
    13
 * accompanied this code).
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
    14
 *
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
    18
 *
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
    21
 * questions.
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
    22
 *
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
    23
 */
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
    24
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
    25
#include "sun_jvm_hotspot_asm_Disassembler.h"
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
    26
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
    27
/*
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
    28
 *  This file implements a binding between Java and the hsdis
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
    29
 *  dissasembler.  It should compile on Linux/Solaris and Windows.
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
    30
 *  The only platform dependent pieces of the code for doing
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
    31
 *  dlopen/dlsym to find the entry point in hsdis.  All the rest is
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
    32
 *  standard JNI code.
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
    33
 */
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
    34
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
    35
#ifdef _WINDOWS
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
    36
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
    37
#define snprintf  _snprintf
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
    38
#define vsnprintf _vsnprintf
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
    39
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
    40
#include <windows.h>
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
    41
#include <sys/types.h>
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
    42
#include <sys/stat.h>
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
    43
#ifdef _DEBUG
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
    44
#include <crtdbg.h>
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
    45
#endif
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
    46
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
    47
#else
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
    48
13959
bcadb9151203 8000332: SA ClassDump throws exception after permgen removal
minqi
parents: 13873
diff changeset
    49
#include <string.h>
13873
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
    50
#include <dlfcn.h>
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
    51
#include <link.h>
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
    52
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
    53
#endif
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
    54
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
    55
#include <limits.h>
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
    56
#include <stdio.h>
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
    57
#include <stdarg.h>
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
    58
#include <stdlib.h>
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
    59
#include <errno.h>
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
    60
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
    61
#ifdef _WINDOWS
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
    62
static int getLastErrorString(char *buf, size_t len)
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
    63
{
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
    64
    long errval;
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
    65
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
    66
    if ((errval = GetLastError()) != 0)
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
    67
    {
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
    68
      /* DOS error */
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
    69
      size_t n = (size_t)FormatMessage(
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
    70
            FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS,
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
    71
            NULL,
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
    72
            errval,
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
    73
            0,
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
    74
            buf,
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
    75
            (DWORD)len,
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
    76
            NULL);
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
    77
      if (n > 3) {
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
    78
        /* Drop final '.', CR, LF */
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
    79
        if (buf[n - 1] == '\n') n--;
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
    80
        if (buf[n - 1] == '\r') n--;
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
    81
        if (buf[n - 1] == '.') n--;
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
    82
        buf[n] = '\0';
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
    83
      }
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
    84
      return (int)n;
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
    85
    }
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
    86
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
    87
    if (errno != 0)
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
    88
    {
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
    89
      /* C runtime error that has no corresponding DOS error code */
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
    90
      const char *s = strerror(errno);
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
    91
      size_t n = strlen(s);
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
    92
      if (n >= len) n = len - 1;
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
    93
      strncpy(buf, s, n);
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
    94
      buf[n] = '\0';
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
    95
      return (int)n;
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
    96
    }
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
    97
    return 0;
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
    98
}
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
    99
#endif /* _WINDOWS */
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   100
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   101
/*
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   102
 * Class:     sun_jvm_hotspot_asm_Disassembler
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   103
 * Method:    load_library
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   104
 * Signature: (Ljava/lang/String;)L
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   105
 */
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   106
JNIEXPORT jlong JNICALL Java_sun_jvm_hotspot_asm_Disassembler_load_1library(JNIEnv * env,
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   107
                                                                           jclass disclass,
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   108
                                                                           jstring jrepath_s,
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   109
                                                                           jstring libname_s) {
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   110
  uintptr_t func = 0;
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   111
  const char* error_message = NULL;
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   112
  const char* java_home;
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   113
  jboolean isCopy;
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   114
  uintptr_t *handle = NULL;
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   115
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   116
  const char * jrepath = (*env)->GetStringUTFChars(env, jrepath_s, &isCopy); // like $JAVA_HOME/jre/lib/sparc/
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   117
  const char * libname = (*env)->GetStringUTFChars(env, libname_s, &isCopy);
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   118
  char buffer[128];
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   119
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   120
  /* Load the hsdis library */
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   121
#ifdef _WINDOWS
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   122
  HINSTANCE hsdis_handle;
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   123
  hsdis_handle = LoadLibrary(libname);
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   124
  if (hsdis_handle == NULL) {
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   125
    snprintf(buffer, sizeof(buffer), "%s%s", jrepath, libname);
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   126
    hsdis_handle = LoadLibrary(buffer);
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   127
  }
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   128
  if (hsdis_handle != NULL) {
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   129
    func = (uintptr_t)GetProcAddress(hsdis_handle, "decode_instructions_virtual");
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   130
  }
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   131
  if (func == 0) {
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   132
    getLastErrorString(buffer, sizeof(buffer));
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   133
    error_message = buffer;
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   134
  }
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   135
#else
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   136
  void* hsdis_handle;
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   137
  hsdis_handle = dlopen(libname, RTLD_LAZY | RTLD_GLOBAL);
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   138
  if (hsdis_handle == NULL) {
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   139
    snprintf(buffer, sizeof(buffer), "%s%s", jrepath, libname);
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   140
    hsdis_handle = dlopen(buffer, RTLD_LAZY | RTLD_GLOBAL);
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   141
  }
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   142
  if (hsdis_handle != NULL) {
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   143
    func = (uintptr_t)dlsym(hsdis_handle, "decode_instructions_virtual");
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   144
  }
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   145
  if (func == 0) {
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   146
    error_message = dlerror();
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   147
  }
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   148
#endif
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   149
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   150
  (*env)->ReleaseStringUTFChars(env, libname_s, libname);
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   151
  (*env)->ReleaseStringUTFChars(env, jrepath_s, jrepath);
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   152
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   153
  if (func == 0) {
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   154
    /* Couldn't find entry point.  error_message should contain some
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   155
     * platform dependent error message.
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   156
     */
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   157
    jclass eclass = (*env)->FindClass(env, "sun/jvm/hotspot/debugger/DebuggerException");
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   158
    (*env)->ThrowNew(env, eclass, error_message);
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   159
  }
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   160
  return (jlong)func;
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   161
}
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   162
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   163
/* signature of decode_instructions_virtual from hsdis.h */
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   164
typedef void* (*decode_func)(uintptr_t start_va, uintptr_t end_va,
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   165
                             unsigned char* start, uintptr_t length,
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   166
                             void* (*event_callback)(void*, const char*, void*),
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   167
                             void* event_stream,
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   168
                             int (*printf_callback)(void*, const char*, ...),
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   169
                             void* printf_stream,
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   170
                             const char* options);
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   171
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   172
/* container for call back state when decoding instructions */
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   173
typedef struct {
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   174
  JNIEnv* env;
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   175
  jobject dis;
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   176
  jobject visitor;
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   177
  jmethodID handle_event;
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   178
  jmethodID raw_print;
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   179
  char buffer[4096];
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   180
} decode_env;
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   181
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   182
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   183
/* event callback binding to Disassembler.handleEvent */
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   184
static void* event_to_env(void* env_pv, const char* event, void* arg) {
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   185
  decode_env* denv = (decode_env*)env_pv;
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   186
  JNIEnv* env = denv->env;
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   187
  jstring event_string = (*env)->NewStringUTF(env, event);
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   188
  jlong result = (*env)->CallLongMethod(env, denv->dis, denv->handle_event, denv->visitor,
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   189
                                        event_string, (jlong) (uintptr_t)arg);
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   190
  if ((*env)->ExceptionOccurred(env) != NULL) {
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   191
    /* ignore exceptions for now */
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   192
    (*env)->ExceptionClear(env);
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   193
    result = 0;
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   194
  }
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   195
  return (void*)(uintptr_t)result;
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   196
}
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   197
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   198
/* printing callback binding to Disassembler.rawPrint */
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   199
static int printf_to_env(void* env_pv, const char* format, ...) {
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   200
  jstring output;
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   201
  va_list ap;
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   202
  int cnt;
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   203
  decode_env* denv = (decode_env*)env_pv;
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   204
  JNIEnv* env = denv->env;
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   205
  size_t flen = strlen(format);
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   206
  const char* raw = NULL;
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   207
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   208
  if (flen == 0)  return 0;
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   209
  if (flen < 2 ||
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   210
      strchr(format, '%') == NULL) {
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   211
    raw = format;
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   212
  } else if (format[0] == '%' && format[1] == '%' &&
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   213
             strchr(format+2, '%') == NULL) {
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   214
    // happens a lot on machines with names like %foo
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   215
    flen--;
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   216
    raw = format+1;
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   217
  }
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   218
  if (raw != NULL) {
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   219
    jstring output = (*env)->NewStringUTF(env, raw);
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   220
    (*env)->CallVoidMethod(env, denv->dis, denv->raw_print, denv->visitor, output);
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   221
    if ((*env)->ExceptionOccurred(env) != NULL) {
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   222
      /* ignore exceptions for now */
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   223
      (*env)->ExceptionClear(env);
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   224
    }
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   225
    return (int) flen;
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   226
  }
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   227
  va_start(ap, format);
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   228
  cnt = vsnprintf(denv->buffer, sizeof(denv->buffer), format, ap);
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   229
  va_end(ap);
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   230
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   231
  output = (*env)->NewStringUTF(env, denv->buffer);
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   232
  (*env)->CallVoidMethod(env, denv->dis, denv->raw_print, denv->visitor, output);
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   233
  if ((*env)->ExceptionOccurred(env) != NULL) {
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   234
    /* ignore exceptions for now */
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   235
    (*env)->ExceptionClear(env);
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   236
  }
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   237
  return cnt;
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   238
}
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   239
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   240
/*
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   241
 * Class:     sun_jvm_hotspot_asm_Disassembler
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   242
 * Method:    decode
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   243
 * Signature: (Lsun/jvm/hotspot/asm/InstructionVisitor;J[BLjava/lang/String;J)V
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   244
 */
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   245
JNIEXPORT void JNICALL Java_sun_jvm_hotspot_asm_Disassembler_decode(JNIEnv * env,
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   246
                                                                    jobject dis,
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   247
                                                                    jobject visitor,
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   248
                                                                    jlong startPc,
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   249
                                                                    jbyteArray code,
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   250
                                                                    jstring options_s,
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   251
                                                                    jlong decode_instructions_virtual) {
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   252
  jboolean isCopy;
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   253
  jbyte* start = (*env)->GetByteArrayElements(env, code, &isCopy);
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   254
  jbyte* end = start + (*env)->GetArrayLength(env, code);
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   255
  const char * options = (*env)->GetStringUTFChars(env, options_s, &isCopy);
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   256
  jclass disclass = (*env)->GetObjectClass(env, dis);
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   257
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   258
  decode_env denv;
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   259
  denv.env = env;
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   260
  denv.dis = dis;
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   261
  denv.visitor = visitor;
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   262
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   263
  /* find Disassembler.handleEvent callback */
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   264
  denv.handle_event = (*env)->GetMethodID(env, disclass, "handleEvent",
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   265
                                          "(Lsun/jvm/hotspot/asm/InstructionVisitor;Ljava/lang/String;J)J");
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   266
  if ((*env)->ExceptionOccurred(env)) {
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   267
    return;
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   268
  }
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   269
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   270
  /* find Disassembler.rawPrint callback */
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   271
  denv.raw_print = (*env)->GetMethodID(env, disclass, "rawPrint",
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   272
                                       "(Lsun/jvm/hotspot/asm/InstructionVisitor;Ljava/lang/String;)V");
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   273
  if ((*env)->ExceptionOccurred(env)) {
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   274
    return;
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   275
  }
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   276
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   277
  /* decode the buffer */
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   278
  (*(decode_func)(uintptr_t)decode_instructions_virtual)(startPc,
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   279
                                                         startPc + end - start,
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   280
                                                         (unsigned char*)start,
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   281
                                                         end - start,
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   282
                                                         &event_to_env,  (void*) &denv,
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   283
                                                         &printf_to_env, (void*) &denv,
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   284
                                                         options);
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   285
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   286
  /* cleanup */
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   287
  (*env)->ReleaseByteArrayElements(env, code, start, JNI_ABORT);
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   288
  (*env)->ReleaseStringUTFChars(env, options_s, options);
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents:
diff changeset
   289
}