1.1 --- a/agent/src/os/bsd/MacosxDebuggerLocal.m Wed Feb 06 14:31:37 2013 -0800 1.2 +++ b/agent/src/os/bsd/MacosxDebuggerLocal.m Fri Feb 08 12:48:24 2013 +0100 1.3 @@ -97,7 +97,8 @@ 1.4 * Method: init0 1.5 * Signature: ()V 1.6 */ 1.7 -JNIEXPORT void JNICALL Java_sun_jvm_hotspot_debugger_bsd_BsdDebuggerLocal_init0(JNIEnv *env, jclass cls) { 1.8 +JNIEXPORT void JNICALL 1.9 +Java_sun_jvm_hotspot_debugger_bsd_BsdDebuggerLocal_init0(JNIEnv *env, jclass cls) { 1.10 symbolicatorID = (*env)->GetFieldID(env, cls, "symbolicator", "J"); 1.11 taskID = (*env)->GetFieldID(env, cls, "task", "J"); 1.12 CHECK_EXCEPTION; 1.13 @@ -108,7 +109,11 @@ 1.14 * Method: lookupByName0 1.15 * Signature: (Ljava/lang/String;Ljava/lang/String;)J 1.16 */ 1.17 -JNIEXPORT jlong JNICALL Java_sun_jvm_hotspot_debugger_bsd_BsdDebuggerLocal_lookupByName0(JNIEnv *env, jobject this_obj, jstring objectName, jstring symbolName) { 1.18 +JNIEXPORT jlong JNICALL 1.19 +Java_sun_jvm_hotspot_debugger_bsd_BsdDebuggerLocal_lookupByName0( 1.20 + JNIEnv *env, jobject this_obj, 1.21 + jstring objectName, jstring symbolName) 1.22 +{ 1.23 jlong address = 0; 1.24 1.25 JNF_COCOA_ENTER(env); 1.26 @@ -137,7 +142,11 @@ 1.27 * Method: readBytesFromProcess0 1.28 * Signature: (JJ)Lsun/jvm/hotspot/debugger/ReadResult; 1.29 */ 1.30 -JNIEXPORT jbyteArray JNICALL Java_sun_jvm_hotspot_debugger_bsd_BsdDebuggerLocal_readBytesFromProcess0(JNIEnv *env, jobject this_obj, jlong addr, jlong numBytes) { 1.31 +JNIEXPORT jbyteArray JNICALL 1.32 +Java_sun_jvm_hotspot_debugger_bsd_BsdDebuggerLocal_readBytesFromProcess0( 1.33 + JNIEnv *env, jobject this_obj, 1.34 + jlong addr, jlong numBytes) 1.35 +{ 1.36 if (debug) printf("readBytesFromProcess called. addr = %llx numBytes = %lld\n", addr, numBytes); 1.37 1.38 // must allocate storage instead of using former parameter buf 1.39 @@ -209,12 +218,74 @@ 1.40 return array; 1.41 } 1.42 1.43 + 1.44 /* 1.45 - * Class: sun_jvm_hotspot_debugger_macosx_MacOSXDebuggerLocal 1.46 + * Lookup the thread_t that corresponds to the given thread_id. 1.47 + * The thread_id should be the result from calling thread_info() with THREAD_IDENTIFIER_INFO 1.48 + * and reading the m_ident_info.thread_id returned. 1.49 + * The returned thread_t is the mach send right to the kernel port for the corresponding thread. 1.50 + * 1.51 + * We cannot simply use the OSThread._thread_id field in the JVM. This is set to ::mach_thread_self() 1.52 + * in the VM, but that thread port is not valid for a remote debugger to access the thread. 1.53 + */ 1.54 +thread_t 1.55 +lookupThreadFromThreadId(task_t task, jlong thread_id) { 1.56 + if (debug) { 1.57 + printf("lookupThreadFromThreadId thread_id=0x%llx\n", thread_id); 1.58 + } 1.59 + 1.60 + thread_array_t thread_list = NULL; 1.61 + mach_msg_type_number_t thread_list_count = 0; 1.62 + thread_t result_thread = 0; 1.63 + int i; 1.64 + 1.65 + // get the list of all the send rights 1.66 + kern_return_t result = task_threads(task, &thread_list, &thread_list_count); 1.67 + if (result != KERN_SUCCESS) { 1.68 + if (debug) { 1.69 + printf("task_threads returned 0x%x\n", result); 1.70 + } 1.71 + return 0; 1.72 + } 1.73 + 1.74 + for(i = 0 ; i < thread_list_count; i++) { 1.75 + thread_identifier_info_data_t m_ident_info; 1.76 + mach_msg_type_number_t count = THREAD_IDENTIFIER_INFO_COUNT; 1.77 + 1.78 + // get the THREAD_IDENTIFIER_INFO for the send right 1.79 + result = thread_info(thread_list[i], THREAD_IDENTIFIER_INFO, (thread_info_t) &m_ident_info, &count); 1.80 + if (result != KERN_SUCCESS) { 1.81 + if (debug) { 1.82 + printf("thread_info returned 0x%x\n", result); 1.83 + } 1.84 + break; 1.85 + } 1.86 + 1.87 + // if this is the one we're looking for, return the send right 1.88 + if (thread_id == m_ident_info.thread_id) 1.89 + { 1.90 + result_thread = thread_list[i]; 1.91 + break; 1.92 + } 1.93 + } 1.94 + 1.95 + vm_size_t thread_list_size = (vm_size_t) (thread_list_count * sizeof (thread_t)); 1.96 + vm_deallocate(mach_task_self(), (vm_address_t) thread_list, thread_list_count); 1.97 + 1.98 + return result_thread; 1.99 +} 1.100 + 1.101 + 1.102 +/* 1.103 + * Class: sun_jvm_hotspot_debugger_bsd_BsdDebuggerLocal 1.104 * Method: getThreadIntegerRegisterSet0 1.105 - * Signature: (I)[J 1.106 + * Signature: (J)[J 1.107 */ 1.108 -JNIEXPORT jlongArray JNICALL Java_sun_jvm_hotspot_debugger_bsd_BsdDebuggerLocal_getThreadIntegerRegisterSet0(JNIEnv *env, jobject this_obj, jint lwp_id) { 1.109 +JNIEXPORT jlongArray JNICALL 1.110 +Java_sun_jvm_hotspot_debugger_bsd_BsdDebuggerLocal_getThreadIntegerRegisterSet0( 1.111 + JNIEnv *env, jobject this_obj, 1.112 + jlong thread_id) 1.113 +{ 1.114 if (debug) 1.115 printf("getThreadRegisterSet0 called\n"); 1.116 1.117 @@ -226,8 +297,9 @@ 1.118 int i; 1.119 jlongArray registerArray; 1.120 jlong *primitiveArray; 1.121 + task_t gTask = getTask(env, this_obj); 1.122 1.123 - tid = lwp_id; 1.124 + tid = lookupThreadFromThreadId(gTask, thread_id); 1.125 1.126 result = thread_get_state(tid, HSDB_THREAD_STATE, (thread_state_t)&state, &count); 1.127 1.128 @@ -328,19 +400,21 @@ 1.129 } 1.130 1.131 /* 1.132 - * Class: sun_jvm_hotspot_debugger_macosx_MacOSXDebuggerLocal 1.133 + * Class: sun_jvm_hotspot_debugger_bsd_BsdDebuggerLocal 1.134 * Method: translateTID0 1.135 * Signature: (I)I 1.136 */ 1.137 JNIEXPORT jint JNICALL 1.138 -Java_sun_jvm_hotspot_debugger_macosx_MacOSXDebuggerLocal_translateTID0(JNIEnv *env, jobject this_obj, jint tid) { 1.139 +Java_sun_jvm_hotspot_debugger_macosx_MacOSXDebuggerLocal_translateTID0( 1.140 + JNIEnv *env, jobject this_obj, jint tid) 1.141 +{ 1.142 if (debug) 1.143 printf("translateTID0 called on tid = 0x%x\n", (int)tid); 1.144 1.145 kern_return_t result; 1.146 thread_t foreign_tid, usable_tid; 1.147 mach_msg_type_name_t type; 1.148 - 1.149 + 1.150 foreign_tid = tid; 1.151 1.152 task_t gTask = getTask(env, this_obj); 1.153 @@ -361,7 +435,10 @@ 1.154 * Method: attach0 1.155 * Signature: (I)V 1.156 */ 1.157 -JNIEXPORT void JNICALL Java_sun_jvm_hotspot_debugger_bsd_BsdDebuggerLocal_attach0__I(JNIEnv *env, jobject this_obj, jint jpid) { 1.158 +JNIEXPORT void JNICALL 1.159 +Java_sun_jvm_hotspot_debugger_bsd_BsdDebuggerLocal_attach0__I( 1.160 + JNIEnv *env, jobject this_obj, jint jpid) 1.161 +{ 1.162 JNF_COCOA_ENTER(env); 1.163 if (getenv("JAVA_SAPROC_DEBUG") != NULL) 1.164 debug = JNI_TRUE; 1.165 @@ -401,7 +478,10 @@ 1.166 * Method: detach0 1.167 * Signature: ()V 1.168 */ 1.169 -JNIEXPORT void JNICALL Java_sun_jvm_hotspot_debugger_bsd_BsdDebuggerLocal_detach0(JNIEnv *env, jobject this_obj) { 1.170 +JNIEXPORT void JNICALL 1.171 +Java_sun_jvm_hotspot_debugger_bsd_BsdDebuggerLocal_detach0( 1.172 + JNIEnv *env, jobject this_obj) 1.173 +{ 1.174 JNF_COCOA_ENTER(env); 1.175 if (debug) printf("detach0 called\n"); 1.176 1.177 @@ -419,10 +499,13 @@ 1.178 * Method: load_library 1.179 * Signature: (Ljava/lang/String;)L 1.180 */ 1.181 -JNIEXPORT jlong JNICALL Java_sun_jvm_hotspot_asm_Disassembler_load_1library(JNIEnv * env, 1.182 - jclass disclass, 1.183 - jstring jrepath_s, 1.184 - jstring libname_s) { 1.185 +JNIEXPORT jlong JNICALL 1.186 +Java_sun_jvm_hotspot_asm_Disassembler_load_1library( 1.187 + JNIEnv * env, 1.188 + jclass disclass, 1.189 + jstring jrepath_s, 1.190 + jstring libname_s) 1.191 +{ 1.192 uintptr_t func = 0; 1.193 const char* error_message = NULL; 1.194 const char* java_home; 1.195 @@ -533,13 +616,16 @@ 1.196 * Method: decode 1.197 * Signature: (Lsun/jvm/hotspot/asm/InstructionVisitor;J[BLjava/lang/String;J)V 1.198 */ 1.199 -JNIEXPORT void JNICALL Java_sun_jvm_hotspot_asm_Disassembler_decode(JNIEnv * env, 1.200 - jobject dis, 1.201 - jobject visitor, 1.202 - jlong startPc, 1.203 - jbyteArray code, 1.204 - jstring options_s, 1.205 - jlong decode_instructions_virtual) { 1.206 +JNIEXPORT void JNICALL 1.207 +Java_sun_jvm_hotspot_asm_Disassembler_decode( 1.208 + JNIEnv * env, 1.209 + jobject dis, 1.210 + jobject visitor, 1.211 + jlong startPc, 1.212 + jbyteArray code, 1.213 + jstring options_s, 1.214 + jlong decode_instructions_virtual) 1.215 +{ 1.216 jboolean isCopy; 1.217 jbyte* start = (*env)->GetByteArrayElements(env, code, &isCopy); 1.218 jbyte* end = start + (*env)->GetArrayLength(env, code);