sadis.c 9.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
/*
 * Copyright 2012, Oracle and/or its affiliates. All Rights Reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 *
 */

#include "sun_jvm_hotspot_asm_Disassembler.h"

/*
 *  This file implements a binding between Java and the hsdis
 *  dissasembler.  It should compile on Linux/Solaris and Windows.
 *  The only platform dependent pieces of the code for doing
 *  dlopen/dlsym to find the entry point in hsdis.  All the rest is
 *  standard JNI code.
 */

#ifdef _WINDOWS

#define snprintf  _snprintf
#define vsnprintf _vsnprintf

#include <windows.h>
#include <sys/types.h>
#include <sys/stat.h>
#ifdef _DEBUG
#include <crtdbg.h>
#endif

#else

#include <strings.h>
#include <dlfcn.h>
#include <link.h>

#endif

#include <limits.h>
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <errno.h>

#ifdef _WINDOWS
static int getLastErrorString(char *buf, size_t len)
{
    long errval;

    if ((errval = GetLastError()) != 0)
    {
      /* DOS error */
      size_t n = (size_t)FormatMessage(
            FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS,
            NULL,
            errval,
            0,
            buf,
            (DWORD)len,
            NULL);
      if (n > 3) {
        /* Drop final '.', CR, LF */
        if (buf[n - 1] == '\n') n--;
        if (buf[n - 1] == '\r') n--;
        if (buf[n - 1] == '.') n--;
        buf[n] = '\0';
      }
      return (int)n;
    }

    if (errno != 0)
    {
      /* C runtime error that has no corresponding DOS error code */
      const char *s = strerror(errno);
      size_t n = strlen(s);
      if (n >= len) n = len - 1;
      strncpy(buf, s, n);
      buf[n] = '\0';
      return (int)n;
    }
    return 0;
}
#endif /* _WINDOWS */

/*
 * Class:     sun_jvm_hotspot_asm_Disassembler
 * Method:    load_library
 * Signature: (Ljava/lang/String;)L
 */
JNIEXPORT jlong JNICALL Java_sun_jvm_hotspot_asm_Disassembler_load_1library(JNIEnv * env,
                                                                           jclass disclass,
                                                                           jstring jrepath_s,
                                                                           jstring libname_s) {
  uintptr_t func = 0;
  const char* error_message = NULL;
  const char* java_home;
  jboolean isCopy;
  uintptr_t *handle = NULL;

  const char * jrepath = (*env)->GetStringUTFChars(env, jrepath_s, &isCopy); // like $JAVA_HOME/jre/lib/sparc/
  const char * libname = (*env)->GetStringUTFChars(env, libname_s, &isCopy);
  char buffer[128];

  /* Load the hsdis library */
#ifdef _WINDOWS
  HINSTANCE hsdis_handle;
  hsdis_handle = LoadLibrary(libname);
  if (hsdis_handle == NULL) {
    snprintf(buffer, sizeof(buffer), "%s%s", jrepath, libname);
    hsdis_handle = LoadLibrary(buffer);
  }
  if (hsdis_handle != NULL) {
    func = (uintptr_t)GetProcAddress(hsdis_handle, "decode_instructions_virtual");
  }
  if (func == 0) {
    getLastErrorString(buffer, sizeof(buffer));
    error_message = buffer;
  }
#else
  void* hsdis_handle;
  hsdis_handle = dlopen(libname, RTLD_LAZY | RTLD_GLOBAL);
  if (hsdis_handle == NULL) {
    snprintf(buffer, sizeof(buffer), "%s%s", jrepath, libname);
    hsdis_handle = dlopen(buffer, RTLD_LAZY | RTLD_GLOBAL);
  }
  if (hsdis_handle != NULL) {
    func = (uintptr_t)dlsym(hsdis_handle, "decode_instructions_virtual");
  }
  if (func == 0) {
    error_message = dlerror();
  }
#endif

  (*env)->ReleaseStringUTFChars(env, libname_s, libname);
  (*env)->ReleaseStringUTFChars(env, jrepath_s, jrepath);

  if (func == 0) {
    /* Couldn't find entry point.  error_message should contain some
     * platform dependent error message.
     */
    jclass eclass = (*env)->FindClass(env, "sun/jvm/hotspot/debugger/DebuggerException");
    (*env)->ThrowNew(env, eclass, error_message);
  }
  return (jlong)func;
}

/* signature of decode_instructions_virtual from hsdis.h */
typedef void* (*decode_func)(uintptr_t start_va, uintptr_t end_va,
                             unsigned char* start, uintptr_t length,
                             void* (*event_callback)(void*, const char*, void*),
                             void* event_stream,
                             int (*printf_callback)(void*, const char*, ...),
                             void* printf_stream,
                             const char* options);

/* container for call back state when decoding instructions */
typedef struct {
  JNIEnv* env;
  jobject dis;
  jobject visitor;
  jmethodID handle_event;
  jmethodID raw_print;
  char buffer[4096];
} decode_env;


/* event callback binding to Disassembler.handleEvent */
static void* event_to_env(void* env_pv, const char* event, void* arg) {
  decode_env* denv = (decode_env*)env_pv;
  JNIEnv* env = denv->env;
  jstring event_string = (*env)->NewStringUTF(env, event);
  jlong result = (*env)->CallLongMethod(env, denv->dis, denv->handle_event, denv->visitor,
                                        event_string, (jlong) (uintptr_t)arg);
  if ((*env)->ExceptionOccurred(env) != NULL) {
    /* ignore exceptions for now */
    (*env)->ExceptionClear(env);
    result = 0;
  }
  return (void*)(uintptr_t)result;
}

/* printing callback binding to Disassembler.rawPrint */
static int printf_to_env(void* env_pv, const char* format, ...) {
  jstring output;
  va_list ap;
  int cnt;
  decode_env* denv = (decode_env*)env_pv;
  JNIEnv* env = denv->env;
  size_t flen = strlen(format);
  const char* raw = NULL;

  if (flen == 0)  return 0;
  if (flen < 2 ||
      strchr(format, '%') == NULL) {
    raw = format;
  } else if (format[0] == '%' && format[1] == '%' &&
             strchr(format+2, '%') == NULL) {
    // happens a lot on machines with names like %foo
    flen--;
    raw = format+1;
  }
  if (raw != NULL) {
    jstring output = (*env)->NewStringUTF(env, raw);
    (*env)->CallVoidMethod(env, denv->dis, denv->raw_print, denv->visitor, output);
    if ((*env)->ExceptionOccurred(env) != NULL) {
      /* ignore exceptions for now */
      (*env)->ExceptionClear(env);
    }
    return (int) flen;
  }
  va_start(ap, format);
  cnt = vsnprintf(denv->buffer, sizeof(denv->buffer), format, ap);
  va_end(ap);

  output = (*env)->NewStringUTF(env, denv->buffer);
  (*env)->CallVoidMethod(env, denv->dis, denv->raw_print, denv->visitor, output);
  if ((*env)->ExceptionOccurred(env) != NULL) {
    /* ignore exceptions for now */
    (*env)->ExceptionClear(env);
  }
  return cnt;
}

/*
 * Class:     sun_jvm_hotspot_asm_Disassembler
 * Method:    decode
 * Signature: (Lsun/jvm/hotspot/asm/InstructionVisitor;J[BLjava/lang/String;J)V
 */
JNIEXPORT void JNICALL Java_sun_jvm_hotspot_asm_Disassembler_decode(JNIEnv * env,
                                                                    jobject dis,
                                                                    jobject visitor,
                                                                    jlong startPc,
                                                                    jbyteArray code,
                                                                    jstring options_s,
                                                                    jlong decode_instructions_virtual) {
  jboolean isCopy;
  jbyte* start = (*env)->GetByteArrayElements(env, code, &isCopy);
  jbyte* end = start + (*env)->GetArrayLength(env, code);
  const char * options = (*env)->GetStringUTFChars(env, options_s, &isCopy);
  jclass disclass = (*env)->GetObjectClass(env, dis);

  decode_env denv;
  denv.env = env;
  denv.dis = dis;
  denv.visitor = visitor;

  /* find Disassembler.handleEvent callback */
  denv.handle_event = (*env)->GetMethodID(env, disclass, "handleEvent",
                                          "(Lsun/jvm/hotspot/asm/InstructionVisitor;Ljava/lang/String;J)J");
  if ((*env)->ExceptionOccurred(env)) {
    return;
  }

  /* find Disassembler.rawPrint callback */
  denv.raw_print = (*env)->GetMethodID(env, disclass, "rawPrint",
                                       "(Lsun/jvm/hotspot/asm/InstructionVisitor;Ljava/lang/String;)V");
  if ((*env)->ExceptionOccurred(env)) {
    return;
  }

  /* decode the buffer */
  (*(decode_func)(uintptr_t)decode_instructions_virtual)(startPc,
                                                         startPc + end - start,
                                                         (unsigned char*)start,
                                                         end - start,
                                                         &event_to_env,  (void*) &denv,
                                                         &printf_to_env, (void*) &denv,
                                                         options);

  /* cleanup */
  (*env)->ReleaseByteArrayElements(env, code, start, JNI_ABORT);
  (*env)->ReleaseStringUTFChars(env, options_s, options);
}