hotspot/agent/src/os/bsd/MacosxDebuggerLocal.m
author sla
Fri, 15 Feb 2013 08:54:12 +0100
changeset 15738 fb4662f9df5b
parent 15593 860cf6c70c06
child 15868 861a1da183b8
permissions -rw-r--r--
8008102: SA on OS X does not stop the attached process Reviewed-by: dholmes, rbackman
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10739
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
     1
/*
13873
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 10739
diff changeset
     2
 * Copyright (c) 2002, 2012, Oracle and/or its affiliates. All rights reserved.
10739
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
     4
 *
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
     7
 * published by the Free Software Foundation.
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
     8
 *
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
    13
 * accompanied this code).
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
    14
 *
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
    18
 *
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
    21
 * questions.
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
    22
 *
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
    23
 */
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
    24
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
    25
#include <objc/objc-runtime.h>
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
    26
#import <Foundation/Foundation.h>
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
    27
#import <JavaNativeFoundation/JavaNativeFoundation.h>
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
    28
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
    29
#include <JavaVM/jni.h>
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
    30
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
    31
#import <mach/mach.h>
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
    32
#import <mach/mach_types.h>
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
    33
#import <sys/sysctl.h>
13873
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 10739
diff changeset
    34
#import <stdio.h>
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 10739
diff changeset
    35
#import <stdarg.h>
10739
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
    36
#import <stdlib.h>
13873
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 10739
diff changeset
    37
#import <strings.h>
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 10739
diff changeset
    38
#import <dlfcn.h>
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 10739
diff changeset
    39
#import <limits.h>
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 10739
diff changeset
    40
#import <errno.h>
15738
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
    41
#import <sys/types.h>
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
    42
#import <sys/ptrace.h>
10739
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
    43
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
    44
jboolean debug = JNI_FALSE;
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
    45
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
    46
static jfieldID symbolicatorID = 0; // set in _init0
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
    47
static jfieldID taskID = 0; // set in _init0
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
    48
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
    49
static void putSymbolicator(JNIEnv *env, jobject this_obj, id symbolicator) {
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
    50
  (*env)->SetLongField(env, this_obj, symbolicatorID, (jlong)(intptr_t)symbolicator);
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
    51
}
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
    52
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
    53
static id getSymbolicator(JNIEnv *env, jobject this_obj) {
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
    54
  jlong ptr = (*env)->GetLongField(env, this_obj, symbolicatorID);
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
    55
  return (id)(intptr_t)ptr;
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
    56
}
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
    57
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
    58
static void putTask(JNIEnv *env, jobject this_obj, task_t task) {
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
    59
  (*env)->SetLongField(env, this_obj, taskID, (jlong)task);
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
    60
}
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
    61
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
    62
static task_t getTask(JNIEnv *env, jobject this_obj) {
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
    63
  jlong ptr = (*env)->GetLongField(env, this_obj, taskID);
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
    64
  return (task_t)ptr;
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
    65
}
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
    66
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
    67
#define CHECK_EXCEPTION_(value) if ((*env)->ExceptionOccurred(env)) { return value; }
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
    68
#define CHECK_EXCEPTION if ((*env)->ExceptionOccurred(env)) { return;}
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
    69
#define THROW_NEW_DEBUGGER_EXCEPTION_(str, value) { throw_new_debugger_exception(env, str); return value; }
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
    70
#define THROW_NEW_DEBUGGER_EXCEPTION(str) { throw_new_debugger_exception(env, str); return;}
13873
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 10739
diff changeset
    71
#define CHECK_EXCEPTION_CLEAR if ((*env)->ExceptionOccurred(env)) { (*env)->ExceptionClear(env); } 
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 10739
diff changeset
    72
#define CHECK_EXCEPTION_CLEAR_VOID if ((*env)->ExceptionOccurred(env)) { (*env)->ExceptionClear(env); return; } 
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 10739
diff changeset
    73
#define CHECK_EXCEPTION_CLEAR_(value) if ((*env)->ExceptionOccurred(env)) { (*env)->ExceptionClear(env); return value; } 
10739
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
    74
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
    75
static void throw_new_debugger_exception(JNIEnv* env, const char* errMsg) {
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
    76
  (*env)->ThrowNew(env, (*env)->FindClass(env, "sun/jvm/hotspot/debugger/DebuggerException"), errMsg);
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
    77
}
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
    78
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
    79
#if defined(__i386__)
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
    80
    #define hsdb_thread_state_t     x86_thread_state32_t
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
    81
    #define hsdb_float_state_t      x86_float_state32_t
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
    82
    #define HSDB_THREAD_STATE       x86_THREAD_STATE32
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
    83
    #define HSDB_FLOAT_STATE        x86_FLOAT_STATE32
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
    84
    #define HSDB_THREAD_STATE_COUNT x86_THREAD_STATE32_COUNT
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
    85
    #define HSDB_FLOAT_STATE_COUNT  x86_FLOAT_STATE32_COUNT
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
    86
#elif defined(__x86_64__)
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
    87
    #define hsdb_thread_state_t     x86_thread_state64_t
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
    88
    #define hsdb_float_state_t      x86_float_state64_t
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
    89
    #define HSDB_THREAD_STATE       x86_THREAD_STATE64
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
    90
    #define HSDB_FLOAT_STATE        x86_FLOAT_STATE64
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
    91
    #define HSDB_THREAD_STATE_COUNT x86_THREAD_STATE64_COUNT
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
    92
    #define HSDB_FLOAT_STATE_COUNT  x86_FLOAT_STATE64_COUNT
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
    93
#else
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
    94
    #error "Unsupported architecture"
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
    95
#endif
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
    96
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
    97
/*
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
    98
 * Class:     sun_jvm_hotspot_debugger_bsd_BsdDebuggerLocal
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
    99
 * Method:    init0
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   100
 * Signature: ()V
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   101
 */
15593
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   102
JNIEXPORT void JNICALL 
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   103
Java_sun_jvm_hotspot_debugger_bsd_BsdDebuggerLocal_init0(JNIEnv *env, jclass cls) {
10739
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   104
  symbolicatorID = (*env)->GetFieldID(env, cls, "symbolicator", "J");
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   105
  taskID = (*env)->GetFieldID(env, cls, "task", "J");
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   106
  CHECK_EXCEPTION;
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   107
}
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   108
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   109
/*
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   110
 * Class:     sun_jvm_hotspot_debugger_bsd_BsdDebuggerLocal
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   111
 * Method:    lookupByName0
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   112
 * Signature: (Ljava/lang/String;Ljava/lang/String;)J
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   113
 */
15593
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   114
JNIEXPORT jlong JNICALL 
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   115
Java_sun_jvm_hotspot_debugger_bsd_BsdDebuggerLocal_lookupByName0(
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   116
  JNIEnv *env, jobject this_obj, 
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   117
  jstring objectName, jstring symbolName) 
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   118
{
10739
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   119
  jlong address = 0;
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   120
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   121
JNF_COCOA_ENTER(env);
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   122
  NSString *symbolNameString = JNFJavaToNSString(env, symbolName);
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   123
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   124
  if (debug) {
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   125
    printf("lookupInProcess called for %s\n", [symbolNameString UTF8String]);
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   126
  }
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   127
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   128
  id symbolicator = getSymbolicator(env, this_obj);
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   129
  if (symbolicator != nil) {
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   130
    uint64_t (*dynamicCall)(id, SEL, NSString *) = (uint64_t (*)(id, SEL, NSString *))&objc_msgSend;
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   131
    address = (jlong) dynamicCall(symbolicator, @selector(addressForSymbol:), symbolNameString);
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   132
  }
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   133
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   134
  if (debug) {
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   135
    printf("address of symbol %s = %llx\n", [symbolNameString UTF8String], address);
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   136
  }
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   137
JNF_COCOA_EXIT(env);
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   138
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   139
  return address;
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   140
}
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   141
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   142
/*
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   143
 * Class:     sun_jvm_hotspot_debugger_bsd_BsdDebuggerLocal
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   144
 * Method:    readBytesFromProcess0
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   145
 * Signature: (JJ)Lsun/jvm/hotspot/debugger/ReadResult;
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   146
 */
15593
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   147
JNIEXPORT jbyteArray JNICALL
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   148
Java_sun_jvm_hotspot_debugger_bsd_BsdDebuggerLocal_readBytesFromProcess0(
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   149
  JNIEnv *env, jobject this_obj, 
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   150
  jlong addr, jlong numBytes) 
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   151
{
10739
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   152
  if (debug) printf("readBytesFromProcess called. addr = %llx numBytes = %lld\n", addr, numBytes);
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   153
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   154
  // must allocate storage instead of using former parameter buf
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   155
  jboolean isCopy;
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   156
  jbyteArray array;
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   157
  jbyte *bufPtr;
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   158
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   159
  array = (*env)->NewByteArray(env, numBytes);
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   160
  CHECK_EXCEPTION_(0);
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   161
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   162
  unsigned long alignedAddress;
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   163
  unsigned long alignedLength;
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   164
  kern_return_t result;
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   165
  vm_offset_t *pages;
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   166
  int *mapped;
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   167
  long pageCount;
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   168
  uint byteCount;
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   169
  int i;
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   170
  unsigned long remaining;
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   171
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   172
  alignedAddress = trunc_page(addr);
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   173
  if (addr != alignedAddress) {
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   174
    alignedLength += addr - alignedAddress;
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   175
  }
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   176
  alignedLength = round_page(numBytes);
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   177
  pageCount = alignedLength/vm_page_size;
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   178
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   179
  // Allocate storage for pages and flags.
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   180
  pages = malloc(pageCount * sizeof(vm_offset_t));
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   181
  mapped = calloc(pageCount, sizeof(int));
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   182
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   183
  task_t gTask = getTask(env, this_obj);
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   184
  // Try to read each of the pages.
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   185
  for (i = 0; i < pageCount; i++) {
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   186
    result = vm_read(gTask, alignedAddress + i*vm_page_size, vm_page_size, 
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   187
		     &pages[i], &byteCount);
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   188
    mapped[i] = (result == KERN_SUCCESS); 
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   189
    // assume all failures are unmapped pages
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   190
  }
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   191
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   192
  if (debug) fprintf(stderr, "%ld pages\n", pageCount);
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   193
	
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   194
  remaining = numBytes;
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   195
	
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   196
  for (i = 0; i < pageCount; i++) {
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   197
    unsigned long len = vm_page_size;
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   198
    unsigned long start = 0;
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   199
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   200
    if (i == 0) {
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   201
      start = addr - alignedAddress;
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   202
      len = vm_page_size - start;
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   203
    }
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   204
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   205
    if (i == (pageCount - 1)) {
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   206
      len = remaining;
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   207
    }
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   208
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   209
    if (mapped[i]) {
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   210
      if (debug) fprintf(stderr, "page %d mapped (len %ld start %ld)\n", i, len, start);
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   211
      (*env)->SetByteArrayRegion(env, array, 0, len, ((jbyte *) pages[i] + start));
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   212
      vm_deallocate(mach_task_self(), pages[i], vm_page_size);
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   213
    }
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   214
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   215
    remaining -= len;
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   216
  }
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   217
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   218
  free (pages);
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   219
  free (mapped);
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   220
  return array;
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   221
}
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   222
15593
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   223
10739
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   224
/*
15593
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   225
 * Lookup the thread_t that corresponds to the given thread_id.
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   226
 * The thread_id should be the result from calling thread_info() with THREAD_IDENTIFIER_INFO
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   227
 * and reading the m_ident_info.thread_id returned.
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   228
 * The returned thread_t is the mach send right to the kernel port for the corresponding thread.
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   229
 *
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   230
 * We cannot simply use the OSThread._thread_id field in the JVM. This is set to ::mach_thread_self()
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   231
 * in the VM, but that thread port is not valid for a remote debugger to access the thread.
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   232
 */
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   233
thread_t
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   234
lookupThreadFromThreadId(task_t task, jlong thread_id) {
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   235
  if (debug) {
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   236
    printf("lookupThreadFromThreadId thread_id=0x%llx\n", thread_id);
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   237
  }
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   238
  
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   239
  thread_array_t thread_list = NULL;
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   240
  mach_msg_type_number_t thread_list_count = 0;
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   241
  thread_t result_thread = 0;
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   242
  int i;
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   243
  
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   244
  // get the list of all the send rights
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   245
  kern_return_t result = task_threads(task, &thread_list, &thread_list_count);
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   246
  if (result != KERN_SUCCESS) {
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   247
    if (debug) {
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   248
      printf("task_threads returned 0x%x\n", result);
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   249
    }
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   250
    return 0;
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   251
  }
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   252
  
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   253
  for(i = 0 ; i < thread_list_count; i++) {
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   254
    thread_identifier_info_data_t m_ident_info;
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   255
    mach_msg_type_number_t count = THREAD_IDENTIFIER_INFO_COUNT;
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   256
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   257
    // get the THREAD_IDENTIFIER_INFO for the send right
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   258
    result = thread_info(thread_list[i], THREAD_IDENTIFIER_INFO, (thread_info_t) &m_ident_info, &count);
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   259
    if (result != KERN_SUCCESS) {
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   260
      if (debug) {
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   261
        printf("thread_info returned 0x%x\n", result);
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   262
      }
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   263
      break;
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   264
    }
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   265
    
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   266
    // if this is the one we're looking for, return the send right
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   267
    if (thread_id == m_ident_info.thread_id)
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   268
    {
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   269
      result_thread = thread_list[i];
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   270
      break;
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   271
    }
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   272
  }
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   273
  
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   274
  vm_size_t thread_list_size = (vm_size_t) (thread_list_count * sizeof (thread_t));
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   275
  vm_deallocate(mach_task_self(), (vm_address_t) thread_list, thread_list_count);
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   276
  
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   277
  return result_thread;
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   278
}
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   279
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   280
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   281
/*
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   282
 * Class:     sun_jvm_hotspot_debugger_bsd_BsdDebuggerLocal
10739
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   283
 * Method:    getThreadIntegerRegisterSet0
15593
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   284
 * Signature: (J)[J
10739
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   285
 */
15593
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   286
JNIEXPORT jlongArray JNICALL 
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   287
Java_sun_jvm_hotspot_debugger_bsd_BsdDebuggerLocal_getThreadIntegerRegisterSet0(
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   288
  JNIEnv *env, jobject this_obj, 
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   289
  jlong thread_id) 
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   290
{
10739
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   291
  if (debug)
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   292
    printf("getThreadRegisterSet0 called\n");
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   293
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   294
  kern_return_t result;
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   295
  thread_t tid;
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   296
  mach_msg_type_number_t count = HSDB_THREAD_STATE_COUNT;
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   297
  hsdb_thread_state_t state;
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   298
  unsigned int *r;
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   299
  int i;
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   300
  jlongArray registerArray;
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   301
  jlong *primitiveArray;
15593
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   302
  task_t gTask = getTask(env, this_obj);
10739
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   303
15593
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   304
  tid = lookupThreadFromThreadId(gTask, thread_id);
10739
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   305
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   306
  result = thread_get_state(tid, HSDB_THREAD_STATE, (thread_state_t)&state, &count);
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   307
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   308
  if (result != KERN_SUCCESS) {
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   309
    if (debug)
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   310
      printf("getregs: thread_get_state(%d) failed (%d)\n", tid, result);
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   311
    return NULL;
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   312
  }
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   313
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   314
  // 40 32-bit registers on ppc, 16 on x86. 
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   315
  // Output order is the same as the order in the ppc_thread_state/i386_thread_state struct.
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   316
#if defined(__i386__)
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   317
	r = (unsigned int *)&state;
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   318
	registerArray = (*env)->NewLongArray(env, 8);
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   319
	primitiveArray = (*env)->GetLongArrayElements(env, registerArray, NULL);
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   320
	primitiveArray[0] = r[0];  // eax
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   321
	primitiveArray[1] = r[2];  // ecx
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   322
	primitiveArray[2] = r[3];  // edx
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   323
	primitiveArray[3] = r[1];  // ebx
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   324
	primitiveArray[4] = r[7];  // esp
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   325
	primitiveArray[5] = r[6];  // ebp
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   326
	primitiveArray[6] = r[5];  // esi
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   327
	primitiveArray[7] = r[4];  // edi
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   328
	(*env)->ReleaseLongArrayElements(env, registerArray, primitiveArray, 0);
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   329
#elif defined(__x86_64__)
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   330
	/* From AMD64ThreadContext.java
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   331
	   public static final int R15 = 0;
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   332
	   public static final int R14 = 1;
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   333
	   public static final int R13 = 2;
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   334
	   public static final int R12 = 3;
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   335
	   public static final int R11 = 4;
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   336
	   public static final int R10 = 5;
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   337
	   public static final int R9  = 6;
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   338
	   public static final int R8  = 7;
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   339
	   public static final int RDI = 8;
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   340
	   public static final int RSI = 9;
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   341
	   public static final int RBP = 10;
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   342
	   public static final int RBX = 11;
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   343
	   public static final int RDX = 12;
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   344
	   public static final int RCX = 13;
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   345
	   public static final int RAX = 14;
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   346
	   public static final int TRAPNO = 15;
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   347
	   public static final int ERR = 16;
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   348
	   public static final int RIP = 17;
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   349
	   public static final int CS = 18;
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   350
	   public static final int RFL = 19;
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   351
	   public static final int RSP = 20;
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   352
	   public static final int SS = 21;
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   353
	   public static final int FS = 22;
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   354
	   public static final int GS = 23;
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   355
	   public static final int ES = 24;
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   356
	   public static final int DS = 25;
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   357
	   public static final int FSBASE = 26;
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   358
	   public static final int GSBASE = 27;
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   359
	 */
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   360
	// 64 bit
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   361
	if (debug) printf("Getting threads for a 64-bit process\n");
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   362
	registerArray = (*env)->NewLongArray(env, 28);
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   363
	primitiveArray = (*env)->GetLongArrayElements(env, registerArray, NULL);
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   364
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   365
	primitiveArray[0] = state.__r15;
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   366
	primitiveArray[1] = state.__r14;
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   367
	primitiveArray[2] = state.__r13;
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   368
	primitiveArray[3] = state.__r12;
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   369
	primitiveArray[4] = state.__r11;
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   370
	primitiveArray[5] = state.__r10;
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   371
	primitiveArray[6] = state.__r9;
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   372
	primitiveArray[7] = state.__r8;
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   373
	primitiveArray[8] = state.__rdi;
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   374
	primitiveArray[9] = state.__rsi;
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   375
	primitiveArray[10] = state.__rbp;
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   376
	primitiveArray[11] = state.__rbx;
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   377
	primitiveArray[12] = state.__rdx;
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   378
	primitiveArray[13] = state.__rcx;
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   379
	primitiveArray[14] = state.__rax;
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   380
	primitiveArray[15] = 0;             // trapno ?
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   381
	primitiveArray[16] = 0;             // err ?
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   382
	primitiveArray[17] = state.__rip;
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   383
	primitiveArray[18] = state.__cs;
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   384
	primitiveArray[19] = state.__rflags;
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   385
	primitiveArray[20] = state.__rsp;
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   386
	primitiveArray[21] = 0;            // We don't have SS
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   387
	primitiveArray[22] = state.__fs;
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   388
	primitiveArray[23] = state.__gs;
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   389
	primitiveArray[24] = 0;
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   390
	primitiveArray[25] = 0;
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   391
	primitiveArray[26] = 0;
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   392
	primitiveArray[27] = 0;
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   393
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   394
	if (debug) printf("set registers\n");
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   395
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   396
	(*env)->ReleaseLongArrayElements(env, registerArray, primitiveArray, 0);
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   397
#else
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   398
#error Unsupported architecture
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   399
#endif
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   400
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   401
  return registerArray;
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   402
}
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   403
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   404
/*
15593
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   405
 * Class:     sun_jvm_hotspot_debugger_bsd_BsdDebuggerLocal
10739
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   406
 * Method:    translateTID0
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   407
 * Signature: (I)I
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   408
 */
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   409
JNIEXPORT jint JNICALL
15593
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   410
Java_sun_jvm_hotspot_debugger_macosx_MacOSXDebuggerLocal_translateTID0(
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   411
  JNIEnv *env, jobject this_obj, jint tid) 
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   412
{
10739
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   413
  if (debug)
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   414
    printf("translateTID0 called on tid = 0x%x\n", (int)tid);
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   415
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   416
  kern_return_t result;
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   417
  thread_t foreign_tid, usable_tid;
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   418
  mach_msg_type_name_t type;
15593
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   419
  
10739
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   420
  foreign_tid = tid;
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   421
    
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   422
  task_t gTask = getTask(env, this_obj);
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   423
  result = mach_port_extract_right(gTask, foreign_tid, 
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   424
				   MACH_MSG_TYPE_COPY_SEND, 
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   425
				   &usable_tid, &type);
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   426
  if (result != KERN_SUCCESS)
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   427
    return -1;
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   428
    
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   429
  if (debug)
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   430
    printf("translateTID0: 0x%x -> 0x%x\n", foreign_tid, usable_tid);
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   431
    
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   432
  return (jint) usable_tid;
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   433
}
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   434
15738
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   435
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   436
static bool ptrace_continue(pid_t pid, int signal) {
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   437
  // pass the signal to the process so we don't swallow it
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   438
  int res;
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   439
  if ((res = ptrace(PT_CONTINUE, pid, (caddr_t)1, signal)) < 0) {
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   440
    fprintf(stderr, "attach: ptrace(PT_CONTINUE, %d) failed with %d\n", pid, res);
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   441
    return false;
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   442
  }
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   443
  return true;
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   444
}
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   445
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   446
// waits until the ATTACH has stopped the process
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   447
// by signal SIGSTOP
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   448
static bool ptrace_waitpid(pid_t pid) {
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   449
  int ret;
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   450
  int status;
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   451
  while (true) {
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   452
    // Wait for debuggee to stop.
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   453
    ret = waitpid(pid, &status, 0);
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   454
    if (ret >= 0) {
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   455
      if (WIFSTOPPED(status)) {
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   456
        // Any signal will stop the thread, make sure it is SIGSTOP. Otherwise SIGSTOP
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   457
        // will still be pending and delivered when the process is DETACHED and the process
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   458
        // will go to sleep.
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   459
        if (WSTOPSIG(status) == SIGSTOP) {
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   460
          // Debuggee stopped by SIGSTOP.
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   461
          return true;
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   462
        }
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   463
        if (!ptrace_continue(pid, WSTOPSIG(status))) {
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   464
          fprintf(stderr, "attach: Failed to correctly attach to VM. VM might HANG! [PTRACE_CONT failed, stopped by %d]\n", WSTOPSIG(status));
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   465
          return false;
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   466
        }
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   467
      } else {
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   468
        fprintf(stderr, "attach: waitpid(): Child process exited/terminated (status = 0x%x)\n", status);
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   469
        return false;
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   470
      }
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   471
    } else {
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   472
      switch (errno) {
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   473
        case EINTR:
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   474
          continue;
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   475
          break;
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   476
        case ECHILD:
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   477
          fprintf(stderr, "attach: waitpid() failed. Child process pid (%d) does not exist \n", pid);
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   478
          break;
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   479
        case EINVAL:
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   480
          fprintf(stderr, "attach: waitpid() failed. Invalid options argument.\n");
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   481
          break;
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   482
        default:
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   483
          fprintf(stderr, "attach: waitpid() failed. Unexpected error %d\n",errno);
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   484
          break;
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   485
      }
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   486
      return false;
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   487
    }
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   488
  }
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   489
}
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   490
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   491
// attach to a process/thread specified by "pid"
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   492
static bool ptrace_attach(pid_t pid) {
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   493
  int res;
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   494
  if ((res = ptrace(PT_ATTACH, pid, 0, 0)) < 0) {
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   495
    fprintf(stderr, "ptrace(PT_ATTACH, %d) failed with %d\n", pid, res);
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   496
    return false;
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   497
  } else {
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   498
    return ptrace_waitpid(pid);
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   499
  }
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   500
}
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   501
10739
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   502
/*
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   503
 * Class:     sun_jvm_hotspot_debugger_bsd_BsdDebuggerLocal
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   504
 * Method:    attach0
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   505
 * Signature: (I)V
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   506
 */
15593
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   507
JNIEXPORT void JNICALL 
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   508
Java_sun_jvm_hotspot_debugger_bsd_BsdDebuggerLocal_attach0__I(
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   509
  JNIEnv *env, jobject this_obj, jint jpid) 
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   510
{
10739
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   511
JNF_COCOA_ENTER(env);
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   512
  if (getenv("JAVA_SAPROC_DEBUG") != NULL)
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   513
    debug = JNI_TRUE;
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   514
  else
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   515
    debug = JNI_FALSE;
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   516
  if (debug) printf("attach0 called for jpid=%d\n", (int)jpid);
15738
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   517
  
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   518
  // get the task from the pid
10739
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   519
  kern_return_t result;
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   520
  task_t gTask = 0;
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   521
  result = task_for_pid(mach_task_self(), jpid, &gTask);
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   522
  if (result != KERN_SUCCESS) {
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   523
    fprintf(stderr, "attach: task_for_pid(%d) failed (%d)\n", (int)jpid, result);
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   524
    THROW_NEW_DEBUGGER_EXCEPTION("Can't attach to the process");
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   525
  }
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   526
  putTask(env, this_obj, gTask);
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   527
15738
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   528
  // use ptrace to stop the process
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   529
  // on os x, ptrace only needs to be called on the process, not the individual threads
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   530
  if (ptrace_attach(jpid) != true) {
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   531
    mach_port_deallocate(mach_task_self(), gTask);
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   532
    THROW_NEW_DEBUGGER_EXCEPTION("Can't attach to the process");
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   533
  }
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   534
10739
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   535
  id symbolicator = nil;
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   536
  id jrsSymbolicator = objc_lookUpClass("JRSSymbolicator");
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   537
  if (jrsSymbolicator != nil) {
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   538
    id (*dynamicCall)(id, SEL, pid_t) = (id (*)(id, SEL, pid_t))&objc_msgSend;
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   539
    symbolicator = dynamicCall(jrsSymbolicator, @selector(symbolicatorForPid:), (pid_t)jpid);
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   540
  }
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   541
  if (symbolicator != nil) {
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   542
    CFRetain(symbolicator); // pin symbolicator while in java heap
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   543
  }
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   544
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   545
  putSymbolicator(env, this_obj, symbolicator);
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   546
  if (symbolicator == nil) {
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   547
    THROW_NEW_DEBUGGER_EXCEPTION("Can't attach symbolicator to the process");
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   548
  }
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   549
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   550
JNF_COCOA_EXIT(env);
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   551
}
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   552
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   553
/*
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   554
 * Class:     sun_jvm_hotspot_debugger_bsd_BsdDebuggerLocal
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   555
 * Method:    detach0
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   556
 * Signature: ()V
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   557
 */
15593
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   558
JNIEXPORT void JNICALL 
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   559
Java_sun_jvm_hotspot_debugger_bsd_BsdDebuggerLocal_detach0(
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   560
  JNIEnv *env, jobject this_obj) 
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   561
{
10739
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   562
JNF_COCOA_ENTER(env);
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   563
  if (debug) printf("detach0 called\n");
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   564
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   565
  task_t gTask = getTask(env, this_obj);
15738
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   566
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   567
  // detach from the ptraced process causing it to resume execution
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   568
  int pid;
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   569
  kern_return_t k_res;
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   570
  k_res = pid_for_task(gTask, &pid);
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   571
  if (k_res != KERN_SUCCESS) {
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   572
    fprintf(stderr, "detach: pid_for_task(%d) failed (%d)\n", pid, k_res);
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   573
  }
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   574
  else {
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   575
    int res = ptrace(PT_DETACH, pid, 0, 0);
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   576
    if (res < 0) {
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   577
      fprintf(stderr, "detach: ptrace(PT_DETACH, %d) failed (%d)\n", pid, res);
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   578
    }
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   579
  }
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   580
  
10739
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   581
  mach_port_deallocate(mach_task_self(), gTask);
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   582
  id symbolicator = getSymbolicator(env, this_obj);
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   583
  if (symbolicator != nil) {
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   584
    CFRelease(symbolicator);
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   585
  }
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   586
JNF_COCOA_EXIT(env);
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   587
}
13873
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 10739
diff changeset
   588
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 10739
diff changeset
   589
/*
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 10739
diff changeset
   590
 * Class:     sun_jvm_hotspot_asm_Disassembler
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 10739
diff changeset
   591
 * Method:    load_library
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 10739
diff changeset
   592
 * Signature: (Ljava/lang/String;)L
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 10739
diff changeset
   593
 */
15593
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   594
JNIEXPORT jlong JNICALL
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   595
Java_sun_jvm_hotspot_asm_Disassembler_load_1library(
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   596
  JNIEnv * env, 
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   597
  jclass disclass,
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   598
  jstring jrepath_s,
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   599
  jstring libname_s) 
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   600
{
13873
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 10739
diff changeset
   601
  uintptr_t func = 0;
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 10739
diff changeset
   602
  const char* error_message = NULL;
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 10739
diff changeset
   603
  const char* java_home;
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 10739
diff changeset
   604
  jboolean isCopy;
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 10739
diff changeset
   605
  uintptr_t *handle = NULL;
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 10739
diff changeset
   606
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 10739
diff changeset
   607
  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: 10739
diff changeset
   608
  const char * libname = (*env)->GetStringUTFChars(env, libname_s, &isCopy);
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 10739
diff changeset
   609
  char buffer[128];
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 10739
diff changeset
   610
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 10739
diff changeset
   611
  /* Load the hsdis library */
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 10739
diff changeset
   612
  void* hsdis_handle;
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 10739
diff changeset
   613
  hsdis_handle = dlopen(libname, RTLD_LAZY | RTLD_GLOBAL);
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 10739
diff changeset
   614
  if (hsdis_handle == NULL) {
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 10739
diff changeset
   615
    snprintf(buffer, sizeof(buffer), "%s%s", jrepath, libname);
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 10739
diff changeset
   616
    hsdis_handle = dlopen(buffer, RTLD_LAZY | RTLD_GLOBAL);
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 10739
diff changeset
   617
  }
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 10739
diff changeset
   618
  if (hsdis_handle != NULL) {
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 10739
diff changeset
   619
    func = (uintptr_t)dlsym(hsdis_handle, "decode_instructions_virtual");
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 10739
diff changeset
   620
  }
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 10739
diff changeset
   621
  if (func == 0) {
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 10739
diff changeset
   622
    error_message = dlerror();
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 10739
diff changeset
   623
    fprintf(stderr, "%s\n", error_message);
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 10739
diff changeset
   624
  }
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 10739
diff changeset
   625
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 10739
diff changeset
   626
  (*env)->ReleaseStringUTFChars(env, libname_s, libname);
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 10739
diff changeset
   627
  (*env)->ReleaseStringUTFChars(env, jrepath_s, jrepath);
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 10739
diff changeset
   628
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 10739
diff changeset
   629
  if (func == 0) {
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 10739
diff changeset
   630
    /* Couldn't find entry point.  error_message should contain some
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 10739
diff changeset
   631
     * platform dependent error message.
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 10739
diff changeset
   632
     */
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 10739
diff changeset
   633
    THROW_NEW_DEBUGGER_EXCEPTION(error_message);
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 10739
diff changeset
   634
  }
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 10739
diff changeset
   635
  return (jlong)func;
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 10739
diff changeset
   636
}
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 10739
diff changeset
   637
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 10739
diff changeset
   638
/* signature of decode_instructions_virtual from hsdis.h */
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 10739
diff changeset
   639
typedef void* (*decode_func)(uintptr_t start_va, uintptr_t end_va,
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 10739
diff changeset
   640
                             unsigned char* start, uintptr_t length,
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 10739
diff changeset
   641
                             void* (*event_callback)(void*, const char*, void*),
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 10739
diff changeset
   642
                             void* event_stream,
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 10739
diff changeset
   643
                             int (*printf_callback)(void*, const char*, ...),
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 10739
diff changeset
   644
                             void* printf_stream,
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 10739
diff changeset
   645
                             const char* options);
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 10739
diff changeset
   646
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 10739
diff changeset
   647
/* container for call back state when decoding instructions */
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 10739
diff changeset
   648
typedef struct {
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 10739
diff changeset
   649
  JNIEnv* env;
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 10739
diff changeset
   650
  jobject dis;
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 10739
diff changeset
   651
  jobject visitor;
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 10739
diff changeset
   652
  jmethodID handle_event;
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 10739
diff changeset
   653
  jmethodID raw_print;
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 10739
diff changeset
   654
  char buffer[4096];
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 10739
diff changeset
   655
} decode_env;
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 10739
diff changeset
   656
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 10739
diff changeset
   657
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 10739
diff changeset
   658
/* event callback binding to Disassembler.handleEvent */
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 10739
diff changeset
   659
static void* event_to_env(void* env_pv, const char* event, void* arg) {
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 10739
diff changeset
   660
  decode_env* denv = (decode_env*)env_pv;
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 10739
diff changeset
   661
  JNIEnv* env = denv->env;
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 10739
diff changeset
   662
  jstring event_string = (*env)->NewStringUTF(env, event);
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 10739
diff changeset
   663
  jlong result = (*env)->CallLongMethod(env, denv->dis, denv->handle_event, denv->visitor,
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 10739
diff changeset
   664
                                        event_string, (jlong) (uintptr_t)arg);
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 10739
diff changeset
   665
  /* ignore exceptions for now */
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 10739
diff changeset
   666
  CHECK_EXCEPTION_CLEAR_((void *)0);
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 10739
diff changeset
   667
  return (void*)(uintptr_t)result;
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 10739
diff changeset
   668
}
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 10739
diff changeset
   669
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 10739
diff changeset
   670
/* printing callback binding to Disassembler.rawPrint */
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 10739
diff changeset
   671
static int printf_to_env(void* env_pv, const char* format, ...) {
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 10739
diff changeset
   672
  jstring output;
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 10739
diff changeset
   673
  va_list ap;
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 10739
diff changeset
   674
  int cnt;
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 10739
diff changeset
   675
  decode_env* denv = (decode_env*)env_pv;
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 10739
diff changeset
   676
  JNIEnv* env = denv->env;
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 10739
diff changeset
   677
  size_t flen = strlen(format);
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 10739
diff changeset
   678
  const char* raw = NULL;
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 10739
diff changeset
   679
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 10739
diff changeset
   680
  if (flen == 0)  return 0;
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 10739
diff changeset
   681
  if (flen < 2 ||
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 10739
diff changeset
   682
      strchr(format, '%') == NULL) {
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 10739
diff changeset
   683
    raw = format;
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 10739
diff changeset
   684
  } else if (format[0] == '%' && format[1] == '%' &&
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 10739
diff changeset
   685
             strchr(format+2, '%') == NULL) {
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 10739
diff changeset
   686
    // happens a lot on machines with names like %foo
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 10739
diff changeset
   687
    flen--;
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 10739
diff changeset
   688
    raw = format+1;
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 10739
diff changeset
   689
  }
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 10739
diff changeset
   690
  if (raw != NULL) {
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 10739
diff changeset
   691
    jstring output = (*env)->NewStringUTF(env, raw);
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 10739
diff changeset
   692
    (*env)->CallVoidMethod(env, denv->dis, denv->raw_print, denv->visitor, output);
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 10739
diff changeset
   693
    CHECK_EXCEPTION_CLEAR;
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 10739
diff changeset
   694
    return (int) flen;
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 10739
diff changeset
   695
  }
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 10739
diff changeset
   696
  va_start(ap, format);
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 10739
diff changeset
   697
  cnt = vsnprintf(denv->buffer, sizeof(denv->buffer), format, ap);
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 10739
diff changeset
   698
  va_end(ap);
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 10739
diff changeset
   699
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 10739
diff changeset
   700
  output = (*env)->NewStringUTF(env, denv->buffer);
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 10739
diff changeset
   701
  (*env)->CallVoidMethod(env, denv->dis, denv->raw_print, denv->visitor, output);
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 10739
diff changeset
   702
  CHECK_EXCEPTION_CLEAR;
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 10739
diff changeset
   703
  return cnt;
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 10739
diff changeset
   704
}
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 10739
diff changeset
   705
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 10739
diff changeset
   706
/*
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 10739
diff changeset
   707
 * Class:     sun_jvm_hotspot_asm_Disassembler
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 10739
diff changeset
   708
 * Method:    decode
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 10739
diff changeset
   709
 * Signature: (Lsun/jvm/hotspot/asm/InstructionVisitor;J[BLjava/lang/String;J)V
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 10739
diff changeset
   710
 */
15593
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   711
JNIEXPORT void JNICALL
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   712
Java_sun_jvm_hotspot_asm_Disassembler_decode(
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   713
   JNIEnv * env,
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   714
   jobject dis,
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   715
   jobject visitor,
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   716
   jlong startPc,
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   717
   jbyteArray code,
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   718
   jstring options_s,
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   719
   jlong decode_instructions_virtual) 
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   720
{
13873
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 10739
diff changeset
   721
  jboolean isCopy;
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 10739
diff changeset
   722
  jbyte* start = (*env)->GetByteArrayElements(env, code, &isCopy);
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 10739
diff changeset
   723
  jbyte* end = start + (*env)->GetArrayLength(env, code);
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 10739
diff changeset
   724
  const char * options = (*env)->GetStringUTFChars(env, options_s, &isCopy);
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 10739
diff changeset
   725
  jclass disclass = (*env)->GetObjectClass(env, dis);
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 10739
diff changeset
   726
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 10739
diff changeset
   727
  decode_env denv;
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 10739
diff changeset
   728
  denv.env = env;
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 10739
diff changeset
   729
  denv.dis = dis;
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 10739
diff changeset
   730
  denv.visitor = visitor;
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 10739
diff changeset
   731
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 10739
diff changeset
   732
  /* find Disassembler.handleEvent callback */
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 10739
diff changeset
   733
  denv.handle_event = (*env)->GetMethodID(env, disclass, "handleEvent",
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 10739
diff changeset
   734
                                          "(Lsun/jvm/hotspot/asm/InstructionVisitor;Ljava/lang/String;J)J");
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 10739
diff changeset
   735
  CHECK_EXCEPTION_CLEAR_VOID
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 10739
diff changeset
   736
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 10739
diff changeset
   737
  /* find Disassembler.rawPrint callback */
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 10739
diff changeset
   738
  denv.raw_print = (*env)->GetMethodID(env, disclass, "rawPrint",
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 10739
diff changeset
   739
                                       "(Lsun/jvm/hotspot/asm/InstructionVisitor;Ljava/lang/String;)V");
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 10739
diff changeset
   740
  CHECK_EXCEPTION_CLEAR_VOID
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 10739
diff changeset
   741
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 10739
diff changeset
   742
  /* decode the buffer */
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 10739
diff changeset
   743
  (*(decode_func)(uintptr_t)decode_instructions_virtual)(startPc,
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 10739
diff changeset
   744
                                                         startPc + end - start,
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 10739
diff changeset
   745
                                                         (unsigned char*)start,
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 10739
diff changeset
   746
                                                         end - start,
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 10739
diff changeset
   747
                                                         &event_to_env,  (void*) &denv,
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 10739
diff changeset
   748
                                                         &printf_to_env, (void*) &denv,
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 10739
diff changeset
   749
                                                         options);
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 10739
diff changeset
   750
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 10739
diff changeset
   751
  /* cleanup */
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 10739
diff changeset
   752
  (*env)->ReleaseByteArrayElements(env, code, start, JNI_ABORT);
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 10739
diff changeset
   753
  (*env)->ReleaseStringUTFChars(env, options_s, options);
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 10739
diff changeset
   754
}