24
24
25
25
#include < objc/objc-runtime.h>
26
26
#import < Foundation/Foundation.h>
27
- #import < JavaNativeFoundation/JavaNativeFoundation.h>
28
- #import < JavaRuntimeSupport/JavaRuntimeSupport.h>
29
27
30
28
#include < jni.h>
31
29
@@ -260,6 +258,39 @@ jlong lookupByNameIncore(
260
258
return addr;
261
259
}
262
260
261
+ /* Create a pool and initiate a try block to catch any exception */
262
+ #define JNI_COCOA_ENTER (env ) \
263
+ NSAutoreleasePool *pool = [[NSAutoreleasePool alloc ] init ]; \
264
+ @try {
265
+
266
+ /* Don't allow NSExceptions to escape to Java.
267
+ * If there is a Java exception that has been thrown that should escape.
268
+ * And ensure we drain the auto-release pool.
269
+ */
270
+ #define JNI_COCOA_EXIT (env ) \
271
+ } \
272
+ @catch (NSException *e) { \
273
+ NSLog (@" %@ " , [e callStackSymbols ]); \
274
+ } \
275
+ @finally { \
276
+ [pool drain ]; \
277
+ };
278
+
279
+ static NSString * JavaStringToNSString (JNIEnv *env, jstring jstr) {
280
+
281
+ if (jstr == NULL ) {
282
+ return NULL ;
283
+ }
284
+ jsize len = (*env)->GetStringLength (env, jstr);
285
+ const jchar *chars = (*env)->GetStringChars (env, jstr, NULL );
286
+ if (chars == NULL ) {
287
+ return NULL ;
288
+ }
289
+ NSString *result = [NSString stringWithCharacters: (UniChar *)chars length: len];
290
+ (*env)->ReleaseStringChars (env, jstr, chars);
291
+ return result;
292
+ }
293
+
263
294
/*
264
295
* Class: sun_jvm_hotspot_debugger_bsd_BsdDebuggerLocal
265
296
* Method: lookupByName0
@@ -277,8 +308,9 @@ jlong lookupByNameIncore(
277
308
278
309
jlong address = 0 ;
279
310
280
- JNF_COCOA_ENTER (env);
281
- NSString *symbolNameString = JNFJavaToNSString (env, symbolName);
311
+ JNI_COCOA_ENTER (env);
312
+
313
+ NSString *symbolNameString = JavaStringToNSString (env, symbolName);
282
314
283
315
print_debug (" lookupInProcess called for %s \n " , [symbolNameString UTF8String ]);
284
316
@@ -289,7 +321,7 @@ jlong lookupByNameIncore(
289
321
}
290
322
291
323
print_debug (" address of symbol %s = %llx \n " , [symbolNameString UTF8String ], address);
292
- JNF_COCOA_EXIT (env);
324
+ JNI_COCOA_EXIT (env);
293
325
294
326
return address;
295
327
}
@@ -812,7 +844,7 @@ static bool wait_for_exception() {
812
844
{
813
845
print_debug (" attach0 called for jpid=%d \n " , (int )jpid);
814
846
815
- JNF_COCOA_ENTER (env);
847
+ JNI_COCOA_ENTER (env);
816
848
817
849
kern_return_t result;
818
850
task_t gTask = 0 ;
@@ -926,7 +958,7 @@ static bool wait_for_exception() {
926
958
THROW_NEW_DEBUGGER_EXCEPTION (" Can't attach symbolicator to the process" );
927
959
}
928
960
929
- JNF_COCOA_EXIT (env);
961
+ JNI_COCOA_EXIT (env);
930
962
}
931
963
932
964
/* * For core file,
@@ -1020,7 +1052,8 @@ static void detach_cleanup(task_t gTask, JNIEnv *env, jobject this_obj, bool thr
1020
1052
Prelease (ph);
1021
1053
return ;
1022
1054
}
1023
- JNF_COCOA_ENTER (env);
1055
+
1056
+ JNI_COCOA_ENTER (env);
1024
1057
1025
1058
task_t gTask = getTask (env, this_obj);
1026
1059
kern_return_t k_res = 0 ;
@@ -1071,5 +1104,5 @@ static void detach_cleanup(task_t gTask, JNIEnv *env, jobject this_obj, bool thr
1071
1104
1072
1105
detach_cleanup (gTask , env, this_obj, false );
1073
1106
1074
- JNF_COCOA_EXIT (env);
1107
+ JNI_COCOA_EXIT (env);
1075
1108
}
0 commit comments