4006
|
1 /*
|
6782
|
2 * Copyright (c) 2002, 2012, Oracle and/or its affiliates. All rights reserved.
|
4006
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 *
|
|
5 * This code is free software; you can redistribute it and/or modify it
|
|
6 * under the terms of the GNU General Public License version 2 only, as
|
|
7 * published by the Free Software Foundation.
|
|
8 *
|
|
9 * This code is distributed in the hope that it will be useful, but WITHOUT
|
|
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
12 * version 2 for more details (a copy is included in the LICENSE file that
|
|
13 * accompanied this code).
|
|
14 *
|
|
15 * You should have received a copy of the GNU General Public License version
|
|
16 * 2 along with this work; if not, write to the Free Software Foundation,
|
|
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
18 *
|
|
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
20 * or visit www.oracle.com if you need additional information or have any
|
|
21 * questions.
|
|
22 *
|
|
23 */
|
|
24
|
|
25 #include <objc/objc-runtime.h>
|
|
26 #import <Foundation/Foundation.h>
|
|
27 #import <JavaNativeFoundation/JavaNativeFoundation.h>
|
|
28
|
|
29 #include <JavaVM/jni.h>
|
|
30
|
|
31 #import <mach/mach.h>
|
|
32 #import <mach/mach_types.h>
|
|
33 #import <sys/sysctl.h>
|
6782
|
34 #import <stdio.h>
|
|
35 #import <stdarg.h>
|
4006
|
36 #import <stdlib.h>
|
6782
|
37 #import <strings.h>
|
|
38 #import <dlfcn.h>
|
|
39 #import <limits.h>
|
|
40 #import <errno.h>
|
4006
|
41
|
|
42 jboolean debug = JNI_FALSE;
|
|
43
|
|
44 static jfieldID symbolicatorID = 0; // set in _init0
|
|
45 static jfieldID taskID = 0; // set in _init0
|
|
46
|
|
47 static void putSymbolicator(JNIEnv *env, jobject this_obj, id symbolicator) {
|
|
48 (*env)->SetLongField(env, this_obj, symbolicatorID, (jlong)(intptr_t)symbolicator);
|
|
49 }
|
|
50
|
|
51 static id getSymbolicator(JNIEnv *env, jobject this_obj) {
|
|
52 jlong ptr = (*env)->GetLongField(env, this_obj, symbolicatorID);
|
|
53 return (id)(intptr_t)ptr;
|
|
54 }
|
|
55
|
|
56 static void putTask(JNIEnv *env, jobject this_obj, task_t task) {
|
|
57 (*env)->SetLongField(env, this_obj, taskID, (jlong)task);
|
|
58 }
|
|
59
|
|
60 static task_t getTask(JNIEnv *env, jobject this_obj) {
|
|
61 jlong ptr = (*env)->GetLongField(env, this_obj, taskID);
|
|
62 return (task_t)ptr;
|
|
63 }
|
|
64
|
|
65 #define CHECK_EXCEPTION_(value) if ((*env)->ExceptionOccurred(env)) { return value; }
|
|
66 #define CHECK_EXCEPTION if ((*env)->ExceptionOccurred(env)) { return;}
|
|
67 #define THROW_NEW_DEBUGGER_EXCEPTION_(str, value) { throw_new_debugger_exception(env, str); return value; }
|
|
68 #define THROW_NEW_DEBUGGER_EXCEPTION(str) { throw_new_debugger_exception(env, str); return;}
|
6782
|
69 #define CHECK_EXCEPTION_CLEAR if ((*env)->ExceptionOccurred(env)) { (*env)->ExceptionClear(env); }
|
|
70 #define CHECK_EXCEPTION_CLEAR_VOID if ((*env)->ExceptionOccurred(env)) { (*env)->ExceptionClear(env); return; }
|
|
71 #define CHECK_EXCEPTION_CLEAR_(value) if ((*env)->ExceptionOccurred(env)) { (*env)->ExceptionClear(env); return value; }
|
4006
|
72
|
|
73 static void throw_new_debugger_exception(JNIEnv* env, const char* errMsg) {
|
|
74 (*env)->ThrowNew(env, (*env)->FindClass(env, "sun/jvm/hotspot/debugger/DebuggerException"), errMsg);
|
|
75 }
|
|
76
|
|
77 #if defined(__i386__)
|
|
78 #define hsdb_thread_state_t x86_thread_state32_t
|
|
79 #define hsdb_float_state_t x86_float_state32_t
|
|
80 #define HSDB_THREAD_STATE x86_THREAD_STATE32
|
|
81 #define HSDB_FLOAT_STATE x86_FLOAT_STATE32
|
|
82 #define HSDB_THREAD_STATE_COUNT x86_THREAD_STATE32_COUNT
|
|
83 #define HSDB_FLOAT_STATE_COUNT x86_FLOAT_STATE32_COUNT
|
|
84 #elif defined(__x86_64__)
|
|
85 #define hsdb_thread_state_t x86_thread_state64_t
|
|
86 #define hsdb_float_state_t x86_float_state64_t
|
|
87 #define HSDB_THREAD_STATE x86_THREAD_STATE64
|
|
88 #define HSDB_FLOAT_STATE x86_FLOAT_STATE64
|
|
89 #define HSDB_THREAD_STATE_COUNT x86_THREAD_STATE64_COUNT
|
|
90 #define HSDB_FLOAT_STATE_COUNT x86_FLOAT_STATE64_COUNT
|
|
91 #else
|
|
92 #error "Unsupported architecture"
|
|
93 #endif
|
|
94
|
|
95 /*
|
|
96 * Class: sun_jvm_hotspot_debugger_bsd_BsdDebuggerLocal
|
|
97 * Method: init0
|
|
98 * Signature: ()V
|
|
99 */
|
|
100 JNIEXPORT void JNICALL Java_sun_jvm_hotspot_debugger_bsd_BsdDebuggerLocal_init0(JNIEnv *env, jclass cls) {
|
|
101 symbolicatorID = (*env)->GetFieldID(env, cls, "symbolicator", "J");
|
|
102 taskID = (*env)->GetFieldID(env, cls, "task", "J");
|
|
103 CHECK_EXCEPTION;
|
|
104 }
|
|
105
|
|
106 /*
|
|
107 * Class: sun_jvm_hotspot_debugger_bsd_BsdDebuggerLocal
|
|
108 * Method: lookupByName0
|
|
109 * Signature: (Ljava/lang/String;Ljava/lang/String;)J
|
|
110 */
|
|
111 JNIEXPORT jlong JNICALL Java_sun_jvm_hotspot_debugger_bsd_BsdDebuggerLocal_lookupByName0(JNIEnv *env, jobject this_obj, jstring objectName, jstring symbolName) {
|
|
112 jlong address = 0;
|
|
113
|
|
114 JNF_COCOA_ENTER(env);
|
|
115 NSString *symbolNameString = JNFJavaToNSString(env, symbolName);
|
|
116
|
|
117 if (debug) {
|
|
118 printf("lookupInProcess called for %s\n", [symbolNameString UTF8String]);
|
|
119 }
|
|
120
|
|
121 id symbolicator = getSymbolicator(env, this_obj);
|
|
122 if (symbolicator != nil) {
|
|
123 uint64_t (*dynamicCall)(id, SEL, NSString *) = (uint64_t (*)(id, SEL, NSString *))&objc_msgSend;
|
|
124 address = (jlong) dynamicCall(symbolicator, @selector(addressForSymbol:), symbolNameString);
|
|
125 }
|
|
126
|
|
127 if (debug) {
|
|
128 printf("address of symbol %s = %llx\n", [symbolNameString UTF8String], address);
|
|
129 }
|
|
130 JNF_COCOA_EXIT(env);
|
|
131
|
|
132 return address;
|
|
133 }
|
|
134
|
|
135 /*
|
|
136 * Class: sun_jvm_hotspot_debugger_bsd_BsdDebuggerLocal
|
|
137 * Method: readBytesFromProcess0
|
|
138 * Signature: (JJ)Lsun/jvm/hotspot/debugger/ReadResult;
|
|
139 */
|
|
140 JNIEXPORT jbyteArray JNICALL Java_sun_jvm_hotspot_debugger_bsd_BsdDebuggerLocal_readBytesFromProcess0(JNIEnv *env, jobject this_obj, jlong addr, jlong numBytes) {
|
|
141 if (debug) printf("readBytesFromProcess called. addr = %llx numBytes = %lld\n", addr, numBytes);
|
|
142
|
|
143 // must allocate storage instead of using former parameter buf
|
|
144 jboolean isCopy;
|
|
145 jbyteArray array;
|
|
146 jbyte *bufPtr;
|
|
147
|
|
148 array = (*env)->NewByteArray(env, numBytes);
|
|
149 CHECK_EXCEPTION_(0);
|
|
150
|
|
151 unsigned long alignedAddress;
|
|
152 unsigned long alignedLength;
|
|
153 kern_return_t result;
|
|
154 vm_offset_t *pages;
|
|
155 int *mapped;
|
|
156 long pageCount;
|
|
157 uint byteCount;
|
|
158 int i;
|
|
159 unsigned long remaining;
|
|
160
|
|
161 alignedAddress = trunc_page(addr);
|
|
162 if (addr != alignedAddress) {
|
|
163 alignedLength += addr - alignedAddress;
|
|
164 }
|
|
165 alignedLength = round_page(numBytes);
|
|
166 pageCount = alignedLength/vm_page_size;
|
|
167
|
|
168 // Allocate storage for pages and flags.
|
|
169 pages = malloc(pageCount * sizeof(vm_offset_t));
|
|
170 mapped = calloc(pageCount, sizeof(int));
|
|
171
|
|
172 task_t gTask = getTask(env, this_obj);
|
|
173 // Try to read each of the pages.
|
|
174 for (i = 0; i < pageCount; i++) {
|
|
175 result = vm_read(gTask, alignedAddress + i*vm_page_size, vm_page_size,
|
|
176 &pages[i], &byteCount);
|
|
177 mapped[i] = (result == KERN_SUCCESS);
|
|
178 // assume all failures are unmapped pages
|
|
179 }
|
|
180
|
|
181 if (debug) fprintf(stderr, "%ld pages\n", pageCount);
|
|
182
|
|
183 remaining = numBytes;
|
|
184
|
|
185 for (i = 0; i < pageCount; i++) {
|
|
186 unsigned long len = vm_page_size;
|
|
187 unsigned long start = 0;
|
|
188
|
|
189 if (i == 0) {
|
|
190 start = addr - alignedAddress;
|
|
191 len = vm_page_size - start;
|
|
192 }
|
|
193
|
|
194 if (i == (pageCount - 1)) {
|
|
195 len = remaining;
|
|
196 }
|
|
197
|
|
198 if (mapped[i]) {
|
|
199 if (debug) fprintf(stderr, "page %d mapped (len %ld start %ld)\n", i, len, start);
|
|
200 (*env)->SetByteArrayRegion(env, array, 0, len, ((jbyte *) pages[i] + start));
|
|
201 vm_deallocate(mach_task_self(), pages[i], vm_page_size);
|
|
202 }
|
|
203
|
|
204 remaining -= len;
|
|
205 }
|
|
206
|
|
207 free (pages);
|
|
208 free (mapped);
|
|
209 return array;
|
|
210 }
|
|
211
|
|
212 /*
|
|
213 * Class: sun_jvm_hotspot_debugger_macosx_MacOSXDebuggerLocal
|
|
214 * Method: getThreadIntegerRegisterSet0
|
|
215 * Signature: (I)[J
|
|
216 */
|
|
217 JNIEXPORT jlongArray JNICALL Java_sun_jvm_hotspot_debugger_bsd_BsdDebuggerLocal_getThreadIntegerRegisterSet0(JNIEnv *env, jobject this_obj, jint lwp_id) {
|
|
218 if (debug)
|
|
219 printf("getThreadRegisterSet0 called\n");
|
|
220
|
|
221 kern_return_t result;
|
|
222 thread_t tid;
|
|
223 mach_msg_type_number_t count = HSDB_THREAD_STATE_COUNT;
|
|
224 hsdb_thread_state_t state;
|
|
225 unsigned int *r;
|
|
226 int i;
|
|
227 jlongArray registerArray;
|
|
228 jlong *primitiveArray;
|
|
229
|
|
230 tid = lwp_id;
|
|
231
|
|
232 result = thread_get_state(tid, HSDB_THREAD_STATE, (thread_state_t)&state, &count);
|
|
233
|
|
234 if (result != KERN_SUCCESS) {
|
|
235 if (debug)
|
|
236 printf("getregs: thread_get_state(%d) failed (%d)\n", tid, result);
|
|
237 return NULL;
|
|
238 }
|
|
239
|
|
240 // 40 32-bit registers on ppc, 16 on x86.
|
|
241 // Output order is the same as the order in the ppc_thread_state/i386_thread_state struct.
|
|
242 #if defined(__i386__)
|
|
243 r = (unsigned int *)&state;
|
|
244 registerArray = (*env)->NewLongArray(env, 8);
|
|
245 primitiveArray = (*env)->GetLongArrayElements(env, registerArray, NULL);
|
|
246 primitiveArray[0] = r[0]; // eax
|
|
247 primitiveArray[1] = r[2]; // ecx
|
|
248 primitiveArray[2] = r[3]; // edx
|
|
249 primitiveArray[3] = r[1]; // ebx
|
|
250 primitiveArray[4] = r[7]; // esp
|
|
251 primitiveArray[5] = r[6]; // ebp
|
|
252 primitiveArray[6] = r[5]; // esi
|
|
253 primitiveArray[7] = r[4]; // edi
|
|
254 (*env)->ReleaseLongArrayElements(env, registerArray, primitiveArray, 0);
|
|
255 #elif defined(__x86_64__)
|
|
256 /* From AMD64ThreadContext.java
|
|
257 public static final int R15 = 0;
|
|
258 public static final int R14 = 1;
|
|
259 public static final int R13 = 2;
|
|
260 public static final int R12 = 3;
|
|
261 public static final int R11 = 4;
|
|
262 public static final int R10 = 5;
|
|
263 public static final int R9 = 6;
|
|
264 public static final int R8 = 7;
|
|
265 public static final int RDI = 8;
|
|
266 public static final int RSI = 9;
|
|
267 public static final int RBP = 10;
|
|
268 public static final int RBX = 11;
|
|
269 public static final int RDX = 12;
|
|
270 public static final int RCX = 13;
|
|
271 public static final int RAX = 14;
|
|
272 public static final int TRAPNO = 15;
|
|
273 public static final int ERR = 16;
|
|
274 public static final int RIP = 17;
|
|
275 public static final int CS = 18;
|
|
276 public static final int RFL = 19;
|
|
277 public static final int RSP = 20;
|
|
278 public static final int SS = 21;
|
|
279 public static final int FS = 22;
|
|
280 public static final int GS = 23;
|
|
281 public static final int ES = 24;
|
|
282 public static final int DS = 25;
|
|
283 public static final int FSBASE = 26;
|
|
284 public static final int GSBASE = 27;
|
|
285 */
|
|
286 // 64 bit
|
|
287 if (debug) printf("Getting threads for a 64-bit process\n");
|
|
288 registerArray = (*env)->NewLongArray(env, 28);
|
|
289 primitiveArray = (*env)->GetLongArrayElements(env, registerArray, NULL);
|
|
290
|
|
291 primitiveArray[0] = state.__r15;
|
|
292 primitiveArray[1] = state.__r14;
|
|
293 primitiveArray[2] = state.__r13;
|
|
294 primitiveArray[3] = state.__r12;
|
|
295 primitiveArray[4] = state.__r11;
|
|
296 primitiveArray[5] = state.__r10;
|
|
297 primitiveArray[6] = state.__r9;
|
|
298 primitiveArray[7] = state.__r8;
|
|
299 primitiveArray[8] = state.__rdi;
|
|
300 primitiveArray[9] = state.__rsi;
|
|
301 primitiveArray[10] = state.__rbp;
|
|
302 primitiveArray[11] = state.__rbx;
|
|
303 primitiveArray[12] = state.__rdx;
|
|
304 primitiveArray[13] = state.__rcx;
|
|
305 primitiveArray[14] = state.__rax;
|
|
306 primitiveArray[15] = 0; // trapno ?
|
|
307 primitiveArray[16] = 0; // err ?
|
|
308 primitiveArray[17] = state.__rip;
|
|
309 primitiveArray[18] = state.__cs;
|
|
310 primitiveArray[19] = state.__rflags;
|
|
311 primitiveArray[20] = state.__rsp;
|
|
312 primitiveArray[21] = 0; // We don't have SS
|
|
313 primitiveArray[22] = state.__fs;
|
|
314 primitiveArray[23] = state.__gs;
|
|
315 primitiveArray[24] = 0;
|
|
316 primitiveArray[25] = 0;
|
|
317 primitiveArray[26] = 0;
|
|
318 primitiveArray[27] = 0;
|
|
319
|
|
320 if (debug) printf("set registers\n");
|
|
321
|
|
322 (*env)->ReleaseLongArrayElements(env, registerArray, primitiveArray, 0);
|
|
323 #else
|
|
324 #error Unsupported architecture
|
|
325 #endif
|
|
326
|
|
327 return registerArray;
|
|
328 }
|
|
329
|
|
330 /*
|
|
331 * Class: sun_jvm_hotspot_debugger_macosx_MacOSXDebuggerLocal
|
|
332 * Method: translateTID0
|
|
333 * Signature: (I)I
|
|
334 */
|
|
335 JNIEXPORT jint JNICALL
|
|
336 Java_sun_jvm_hotspot_debugger_macosx_MacOSXDebuggerLocal_translateTID0(JNIEnv *env, jobject this_obj, jint tid) {
|
|
337 if (debug)
|
|
338 printf("translateTID0 called on tid = 0x%x\n", (int)tid);
|
|
339
|
|
340 kern_return_t result;
|
|
341 thread_t foreign_tid, usable_tid;
|
|
342 mach_msg_type_name_t type;
|
|
343
|
|
344 foreign_tid = tid;
|
|
345
|
|
346 task_t gTask = getTask(env, this_obj);
|
|
347 result = mach_port_extract_right(gTask, foreign_tid,
|
|
348 MACH_MSG_TYPE_COPY_SEND,
|
|
349 &usable_tid, &type);
|
|
350 if (result != KERN_SUCCESS)
|
|
351 return -1;
|
|
352
|
|
353 if (debug)
|
|
354 printf("translateTID0: 0x%x -> 0x%x\n", foreign_tid, usable_tid);
|
|
355
|
|
356 return (jint) usable_tid;
|
|
357 }
|
|
358
|
|
359 /*
|
|
360 * Class: sun_jvm_hotspot_debugger_bsd_BsdDebuggerLocal
|
|
361 * Method: attach0
|
|
362 * Signature: (I)V
|
|
363 */
|
|
364 JNIEXPORT void JNICALL Java_sun_jvm_hotspot_debugger_bsd_BsdDebuggerLocal_attach0__I(JNIEnv *env, jobject this_obj, jint jpid) {
|
|
365 JNF_COCOA_ENTER(env);
|
|
366 if (getenv("JAVA_SAPROC_DEBUG") != NULL)
|
|
367 debug = JNI_TRUE;
|
|
368 else
|
|
369 debug = JNI_FALSE;
|
|
370 if (debug) printf("attach0 called for jpid=%d\n", (int)jpid);
|
|
371
|
|
372 kern_return_t result;
|
|
373 task_t gTask = 0;
|
|
374 result = task_for_pid(mach_task_self(), jpid, &gTask);
|
|
375 if (result != KERN_SUCCESS) {
|
|
376 fprintf(stderr, "attach: task_for_pid(%d) failed (%d)\n", (int)jpid, result);
|
|
377 THROW_NEW_DEBUGGER_EXCEPTION("Can't attach to the process");
|
|
378 }
|
|
379 putTask(env, this_obj, gTask);
|
|
380
|
|
381 id symbolicator = nil;
|
|
382 id jrsSymbolicator = objc_lookUpClass("JRSSymbolicator");
|
|
383 if (jrsSymbolicator != nil) {
|
|
384 id (*dynamicCall)(id, SEL, pid_t) = (id (*)(id, SEL, pid_t))&objc_msgSend;
|
|
385 symbolicator = dynamicCall(jrsSymbolicator, @selector(symbolicatorForPid:), (pid_t)jpid);
|
|
386 }
|
|
387 if (symbolicator != nil) {
|
|
388 CFRetain(symbolicator); // pin symbolicator while in java heap
|
|
389 }
|
|
390
|
|
391 putSymbolicator(env, this_obj, symbolicator);
|
|
392 if (symbolicator == nil) {
|
|
393 THROW_NEW_DEBUGGER_EXCEPTION("Can't attach symbolicator to the process");
|
|
394 }
|
|
395
|
|
396 JNF_COCOA_EXIT(env);
|
|
397 }
|
|
398
|
|
399 /*
|
|
400 * Class: sun_jvm_hotspot_debugger_bsd_BsdDebuggerLocal
|
|
401 * Method: detach0
|
|
402 * Signature: ()V
|
|
403 */
|
|
404 JNIEXPORT void JNICALL Java_sun_jvm_hotspot_debugger_bsd_BsdDebuggerLocal_detach0(JNIEnv *env, jobject this_obj) {
|
|
405 JNF_COCOA_ENTER(env);
|
|
406 if (debug) printf("detach0 called\n");
|
|
407
|
|
408 task_t gTask = getTask(env, this_obj);
|
|
409 mach_port_deallocate(mach_task_self(), gTask);
|
|
410 id symbolicator = getSymbolicator(env, this_obj);
|
|
411 if (symbolicator != nil) {
|
|
412 CFRelease(symbolicator);
|
|
413 }
|
|
414 JNF_COCOA_EXIT(env);
|
|
415 }
|
6782
|
416
|
|
417 /*
|
|
418 * Class: sun_jvm_hotspot_asm_Disassembler
|
|
419 * Method: load_library
|
|
420 * Signature: (Ljava/lang/String;)L
|
|
421 */
|
|
422 JNIEXPORT jlong JNICALL Java_sun_jvm_hotspot_asm_Disassembler_load_1library(JNIEnv * env,
|
|
423 jclass disclass,
|
|
424 jstring jrepath_s,
|
|
425 jstring libname_s) {
|
|
426 uintptr_t func = 0;
|
|
427 const char* error_message = NULL;
|
|
428 const char* java_home;
|
|
429 jboolean isCopy;
|
|
430 uintptr_t *handle = NULL;
|
|
431
|
|
432 const char * jrepath = (*env)->GetStringUTFChars(env, jrepath_s, &isCopy); // like $JAVA_HOME/jre/lib/sparc/
|
|
433 const char * libname = (*env)->GetStringUTFChars(env, libname_s, &isCopy);
|
|
434 char buffer[128];
|
|
435
|
|
436 /* Load the hsdis library */
|
|
437 void* hsdis_handle;
|
|
438 hsdis_handle = dlopen(libname, RTLD_LAZY | RTLD_GLOBAL);
|
|
439 if (hsdis_handle == NULL) {
|
|
440 snprintf(buffer, sizeof(buffer), "%s%s", jrepath, libname);
|
|
441 hsdis_handle = dlopen(buffer, RTLD_LAZY | RTLD_GLOBAL);
|
|
442 }
|
|
443 if (hsdis_handle != NULL) {
|
|
444 func = (uintptr_t)dlsym(hsdis_handle, "decode_instructions_virtual");
|
|
445 }
|
|
446 if (func == 0) {
|
|
447 error_message = dlerror();
|
|
448 fprintf(stderr, "%s\n", error_message);
|
|
449 }
|
|
450
|
|
451 (*env)->ReleaseStringUTFChars(env, libname_s, libname);
|
|
452 (*env)->ReleaseStringUTFChars(env, jrepath_s, jrepath);
|
|
453
|
|
454 if (func == 0) {
|
|
455 /* Couldn't find entry point. error_message should contain some
|
|
456 * platform dependent error message.
|
|
457 */
|
|
458 THROW_NEW_DEBUGGER_EXCEPTION(error_message);
|
|
459 }
|
|
460 return (jlong)func;
|
|
461 }
|
|
462
|
|
463 /* signature of decode_instructions_virtual from hsdis.h */
|
|
464 typedef void* (*decode_func)(uintptr_t start_va, uintptr_t end_va,
|
|
465 unsigned char* start, uintptr_t length,
|
|
466 void* (*event_callback)(void*, const char*, void*),
|
|
467 void* event_stream,
|
|
468 int (*printf_callback)(void*, const char*, ...),
|
|
469 void* printf_stream,
|
|
470 const char* options);
|
|
471
|
|
472 /* container for call back state when decoding instructions */
|
|
473 typedef struct {
|
|
474 JNIEnv* env;
|
|
475 jobject dis;
|
|
476 jobject visitor;
|
|
477 jmethodID handle_event;
|
|
478 jmethodID raw_print;
|
|
479 char buffer[4096];
|
|
480 } decode_env;
|
|
481
|
|
482
|
|
483 /* event callback binding to Disassembler.handleEvent */
|
|
484 static void* event_to_env(void* env_pv, const char* event, void* arg) {
|
|
485 decode_env* denv = (decode_env*)env_pv;
|
|
486 JNIEnv* env = denv->env;
|
|
487 jstring event_string = (*env)->NewStringUTF(env, event);
|
|
488 jlong result = (*env)->CallLongMethod(env, denv->dis, denv->handle_event, denv->visitor,
|
|
489 event_string, (jlong) (uintptr_t)arg);
|
|
490 /* ignore exceptions for now */
|
|
491 CHECK_EXCEPTION_CLEAR_((void *)0);
|
|
492 return (void*)(uintptr_t)result;
|
|
493 }
|
|
494
|
|
495 /* printing callback binding to Disassembler.rawPrint */
|
|
496 static int printf_to_env(void* env_pv, const char* format, ...) {
|
|
497 jstring output;
|
|
498 va_list ap;
|
|
499 int cnt;
|
|
500 decode_env* denv = (decode_env*)env_pv;
|
|
501 JNIEnv* env = denv->env;
|
|
502 size_t flen = strlen(format);
|
|
503 const char* raw = NULL;
|
|
504
|
|
505 if (flen == 0) return 0;
|
|
506 if (flen < 2 ||
|
|
507 strchr(format, '%') == NULL) {
|
|
508 raw = format;
|
|
509 } else if (format[0] == '%' && format[1] == '%' &&
|
|
510 strchr(format+2, '%') == NULL) {
|
|
511 // happens a lot on machines with names like %foo
|
|
512 flen--;
|
|
513 raw = format+1;
|
|
514 }
|
|
515 if (raw != NULL) {
|
|
516 jstring output = (*env)->NewStringUTF(env, raw);
|
|
517 (*env)->CallVoidMethod(env, denv->dis, denv->raw_print, denv->visitor, output);
|
|
518 CHECK_EXCEPTION_CLEAR;
|
|
519 return (int) flen;
|
|
520 }
|
|
521 va_start(ap, format);
|
|
522 cnt = vsnprintf(denv->buffer, sizeof(denv->buffer), format, ap);
|
|
523 va_end(ap);
|
|
524
|
|
525 output = (*env)->NewStringUTF(env, denv->buffer);
|
|
526 (*env)->CallVoidMethod(env, denv->dis, denv->raw_print, denv->visitor, output);
|
|
527 CHECK_EXCEPTION_CLEAR;
|
|
528 return cnt;
|
|
529 }
|
|
530
|
|
531 /*
|
|
532 * Class: sun_jvm_hotspot_asm_Disassembler
|
|
533 * Method: decode
|
|
534 * Signature: (Lsun/jvm/hotspot/asm/InstructionVisitor;J[BLjava/lang/String;J)V
|
|
535 */
|
|
536 JNIEXPORT void JNICALL Java_sun_jvm_hotspot_asm_Disassembler_decode(JNIEnv * env,
|
|
537 jobject dis,
|
|
538 jobject visitor,
|
|
539 jlong startPc,
|
|
540 jbyteArray code,
|
|
541 jstring options_s,
|
|
542 jlong decode_instructions_virtual) {
|
|
543 jboolean isCopy;
|
|
544 jbyte* start = (*env)->GetByteArrayElements(env, code, &isCopy);
|
|
545 jbyte* end = start + (*env)->GetArrayLength(env, code);
|
|
546 const char * options = (*env)->GetStringUTFChars(env, options_s, &isCopy);
|
|
547 jclass disclass = (*env)->GetObjectClass(env, dis);
|
|
548
|
|
549 decode_env denv;
|
|
550 denv.env = env;
|
|
551 denv.dis = dis;
|
|
552 denv.visitor = visitor;
|
|
553
|
|
554 /* find Disassembler.handleEvent callback */
|
|
555 denv.handle_event = (*env)->GetMethodID(env, disclass, "handleEvent",
|
|
556 "(Lsun/jvm/hotspot/asm/InstructionVisitor;Ljava/lang/String;J)J");
|
|
557 CHECK_EXCEPTION_CLEAR_VOID
|
|
558
|
|
559 /* find Disassembler.rawPrint callback */
|
|
560 denv.raw_print = (*env)->GetMethodID(env, disclass, "rawPrint",
|
|
561 "(Lsun/jvm/hotspot/asm/InstructionVisitor;Ljava/lang/String;)V");
|
|
562 CHECK_EXCEPTION_CLEAR_VOID
|
|
563
|
|
564 /* decode the buffer */
|
|
565 (*(decode_func)(uintptr_t)decode_instructions_virtual)(startPc,
|
|
566 startPc + end - start,
|
|
567 (unsigned char*)start,
|
|
568 end - start,
|
|
569 &event_to_env, (void*) &denv,
|
|
570 &printf_to_env, (void*) &denv,
|
|
571 options);
|
|
572
|
|
573 /* cleanup */
|
|
574 (*env)->ReleaseByteArrayElements(env, code, start, JNI_ABORT);
|
|
575 (*env)->ReleaseStringUTFChars(env, options_s, options);
|
|
576 }
|