java - Debugging JNI Code with Xcode 4 / OS X 10.6 -


i've been tearing hair out trying figure out that's braindead in windows / visual studio. have jni library under os x 10.6 invoke java executable , debug under xcode: stepping through code, examining variables in jni library, setting breakpoints, etc.

here working jni example compiles , runs fine command line. if tell me how set in xcode, i'd eternally grateful. ideally, i'd step-by-step instructions starting scratch in xcode , cutting , pasting code below appropriate, resulting in debuggable under xcode 4 , allowing me set breakpoints in jni code, examine variables, see stacktraces, etc.

as token of gratitude, i'll send $20 first person gives me such instructions can reproduce on end if give me paypal address.

thanks in advance!

helloworld.java

class helloworld {     public native string displayhelloworld();      static {         system.loadlibrary("helloworldimp");     }      public static void main(string[] args) {         system.out.println("--> "+new helloworld().displayhelloworld());     } } 

helloworldimp.mm

#include <stdio.h> #include <jni.h>  #include <string>  #import <foundation/foundation.h>  #ifdef __cplusplus extern "c" { #endif  jniexport jstring jnicall java_helloworld_displayhelloworld(jnienv *env, jobject obj) {     nsautoreleasepool * pool = [[nsautoreleasepool alloc] init];      nsstring* name = @"yo cocoa";     std::string s = [name utf8string];          jstring ret = env->newstringutf(s.c_str());      [pool drain];      return ret; }  #ifdef __cplusplus } #endif 

buildjni.sh

gcc -bundle -i/system/library/frameworks/javavm.framework/headers -lstdc++ -o libhelloworldimp.jnilib -framework foundation helloworldimp.mm 

here go.

building lib
know jni libs on mac must named in form libfoo.jnilib. load lib java calling system.loadlibrary("foo");
1. create new dynamic library project. name libsomething. that's half naming battle.
2. search target build settings executable extension. default set dylib. modify jnilib. that's second half of naming battle.
3. add javavm framework linked frameworks. has jni stuff in it.
4. generate native header file usual.
5. include in project , create corresponding .m file.
6. have modify include path jni.h. on mac <javavm/jni.h> or use javanativefoundation framework. recommend latter.
7. (optional) javanativefoundation provides number of macros , methods make converting data/methods to/from java/objective-c lot easier. it's not documented, it's worth investigating. if decide use jnf, use open triangle on javavm framework. see jnf subframework. drag top level equal javavm. can start using it. import <javanativefoundation/javanativefoundation.h> google search on jnf_cocoa_enter see examples of how use jnf

you should able build valid jni library. assuming have no bugs in code, you're done. ;-)

debugging
tried found on various web pages debugging jni under xcode work. sort of worked, hang when stepping on cocoa calls. however, debugging gdb does work. it's not bad if run gdb within emacs. drawback, of course, you'll need come speed on emacs , gdb, if aren't already. here's gist:
1. compile lib debugging symbols
2. set breakpoint in java calling code after lib has been loaded, before calling code want debug. alternatively, put showmessagedialog there. block java program @ right point.
3. open activity monitor , note pid of java program.
4. start emacs. if on lion, terminal emacs comes should work. on sl terminal emacs doesn't have of gdb magic. in both cases, however, recommend download , use mac gui emacs. there's more 1 available, use 1 got here. if use emacs in terminal, you'll want go termina/preferences/settings/keyboard , down toward bottom. there's checkbox "use option meta key". you'll want checked.
5. emacs running press m-x (option x). type gdb , press enter. you'll see "gdb --annotate=2 xxxxxxxxxx". press backspace until annotate setting. enter "pid xxx" xxx pid of java calling app. should end line looking "gdb --annotate=2 pid xxxx". press enter.
6. should see gdb loading symbols jni lib.
7. press m-x. type gdb-many-windows. press enter.
8. should see multiple windows in emacs. gdb command window, local variables, source code, , others.
9. enter break somefunctionname in command window. press enter.
10. enter cont in command window. (this causes execution continue). press enter.
11. continue in java debugger, or press enter in java dialog.
12. should see gdb hit breakpoint in jni code.
13. study on emacs , gdb. in particular check out po command in gdb.
14. , bob's uncle.

enjoy!


Comments

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -