annotate agent/src/os/bsd/MacosxDebuggerLocal.m @ 8062:5d5c577296fd

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