From: Pierre-Marc Fournier Date: Thu, 13 May 2010 16:41:41 +0000 (-0400) Subject: java interface: add script to automatically generate lib loader C code X-Git-Tag: 0.12.32~13 X-Git-Url: http://git.lttng.org/?p=lttv.git;a=commitdiff_plain;h=refs%2Fheads%2Ftraceformat-2.6 java interface: add script to automatically generate lib loader C code --- diff --git a/scripts/autogen_lttvtraceread_loader.sh b/scripts/autogen_lttvtraceread_loader.sh new file mode 100644 index 00000000..87c3e884 --- /dev/null +++ b/scripts/autogen_lttvtraceread_loader.sh @@ -0,0 +1,177 @@ +#!/bin/bash + +JNI_PATH="$1" + +if [ "$JNI_PATH" = "" ]; then + echo "No jni path given!" + exit 1 +fi + + + +echo -e "" +echo -e "/* Important to get consistent size_t type */" +echo -e "#define _FILE_OFFSET_BITS 64" +echo -e "" +echo -e "#include " +echo -e "#include " +echo -e "#include " +echo -e "#include " +echo -e "#include " +echo -e "" +echo -e "#include " +echo -e "#include " +echo -e "#include " +echo -e "#include " +echo -e "#include " +echo -e "" +echo -e "int nb_id = 0;" +echo -e "" +echo -e "struct version_correlation {" +echo -e " int id;" +echo -e " char *libname;" +echo -e " void *static_handle;" +echo -e "};" +echo -e "" +echo -e "struct version_correlation *version_table = NULL;" +echo -e "" +echo -e "struct function_tables {" +for x in `grep JNIEXPORT $JNI_PATH | grep -v getTraceVersion | sed -e "s/{//g" | sed -e "s/ /#/g"`; do + echo -e " `echo $x | sed -e "s/#/ /g" | sed -e "s/JNICALL.*/JNICALL/g"` (*`echo $x | sed -e "s/#/ /g" | sed -e "s/.*JNICALL //g" | cut -d\( -f1 | sed -e "s/ / /g"`)(`echo $x | sed -e "s/#/ /g" | cut -d\( -f2 | sed -e "s/ / /g" | sed -e "s/) /)/g"`;" +done +echo -e "};" +echo -e "" +echo -e "struct function_tables *version_functions_table = NULL;" +echo -e "" +echo -e "void ignore_and_drop_message(const gchar *log_domain, GLogLevelFlags log_level, const gchar *message, gpointer user_data) {" +echo -e "}" +echo -e "" +echo -e "JNIEXPORT void JNICALL Java_org_eclipse_linuxtools_lttng_jni_factory_JniTraceVersion_ltt_1getTraceVersion(JNIEnv *env, jobject jobj, jstring tracepath) {" +echo -e "" +echo -e " void *handle = dlopen(\"liblttvtraceread.so\", RTLD_LAZY );" +echo -e "" +echo -e " if (!handle ) {" +echo -e " printf (\"WARNING : Failed to initialize library handle from %s!\\\n\", \"liblttvtraceread.so\");" +echo -e " }" +echo -e " JNIEXPORT void JNICALL (*functor)(JNIEnv *env, jobject jobj, jstring tracepath);" +echo -e " functor=dlsym(handle, \"Java_org_eclipse_linuxtools_lttng_jni_factory_JniTraceVersion_ltt_1getTraceVersion\");" +echo -e "" +echo -e " char *error = dlerror();" +echo -e " if ( error != NULL) {" +echo -e " printf (\"Call failed with : %s\\\n\", error);" +echo -e " }" +echo -e " else {" +echo -e " (*functor)(env, jobj, tracepath);" +echo -e " }" +echo -e "}" +echo -e "" +echo -e "void freeAllHandle() {" +echo -e " if ( version_table != NULL ) {" +echo -e " free(version_table);" +echo -e " version_table = NULL;" +echo -e " }" +echo -e "" +echo -e " if ( version_functions_table != NULL ) {" +echo -e " free(version_functions_table);" +echo -e " version_functions_table = NULL;" +echo -e " }" +echo -e "}" +echo -e "" +echo -e "void freeHandle(int handle_id) {" +echo -e " if ( handle_id >= nb_id ) {" +echo -e " if (version_table[handle_id].static_handle != NULL) {" +echo -e " /* Memory will be freed by dlclose as well */" +echo -e " dlclose(version_table[handle_id].static_handle);" +echo -e " version_table[handle_id].static_handle = NULL;" +echo -e " free(version_table[handle_id].libname);" +echo -e " version_table[handle_id].libname = NULL;" +echo -e " }" +echo -e " }" +echo -e "" +echo -e " int isEmpty = 1;" +echo -e " int n;" +echo -e " for ( n=0; nGetStringUTFChars(env, libname, 0);" +echo -e "" +echo -e " int isLoaded = 0;" +echo -e " int n;" +echo -e " for ( n=0; n 1) {" +echo -e " memcpy(new_version_table,version_table, sizeof(struct version_correlation)*(nb_id-1) );" +echo -e " free( version_table );" +echo -e "" +echo -e " memcpy(new_function_tables,version_functions_table, sizeof(struct function_tables)*(nb_id-1) );" +echo -e " free( version_functions_table);" +echo -e " }" +echo -e "" +echo -e " version_table = (struct version_correlation *)new_version_table;" +echo -e " version_table[lib_id].id = lib_id;" +echo -e " version_table[lib_id].libname = (char*)malloc( strlen(c_path) );" +echo -e " strncpy(version_table[lib_id].libname, c_path, strlen(c_path));" +echo -e " version_table[lib_id].static_handle = new_handle;" +echo -e "" +echo -e " version_functions_table = (struct function_tables*)new_function_tables;" +echo -e "" +for x in `grep JNIEXPORT $JNI_PATH | grep -v getTraceVersion |sed -e "s/{//g" | sed -e "s/ /#/g"`; do + echo -e " version_functions_table[lib_id].`echo $x | sed -e "s/#/ /g" | sed -e "s/.*JNICALL //g" | cut -d\( -f1 | sed -e "s/ / /g"` = dlsym(version_table[lib_id].static_handle, \"`echo $x | sed -e "s/#/ /g" | sed -e "s/.*JNICALL //g" | cut -d\( -f1 | sed -e "s/ / /g"`\");" +done +echo -e " }" +echo -e " }" +echo -e "" +echo -e " return lib_id;" +echo -e "}" +echo -e "" + +for x in `grep JNIEXPORT $JNI_PATH | grep -v getTraceVersion | sed -e "s/{//g" | sed -e "s/ /#/g"`; do + echo -e "` echo $x | sed -e "s/#/ /g" | sed -e "s/ / /g" | sed -e "s/env, jobject jobj,/env, jobject jobj, jint lib_id,/g" | sed -e "s/env, jclass accessClass,/env, jclass accessClass, jint lib_id,/g"`{"; + + TEST=`echo $x | sed -e "s/#/ /g" | awk {print'$2'}` + + ARG=`echo $x | sed -e "s/#/ /g" | cut -d\( -f2 | sed -e "s/JNIEnv \*//g" | sed -e "s/jobject//g" | sed -e "s/jstring//g" | sed -e "s/jlong//g" | sed -e "s/jint//g" | sed -e "s/jclass//g" | sed -e "s/jboolean//g" | sed -e "s/jbyteArray//g" | sed -e "s/ / /g" | sed -e "s/) /)/g"` + + if [ "$TEST" = "void" ]; then + echo -e " (version_functions_table[lib_id].`echo $x | sed -e "s/#/ /g" | sed -e "s/.*JNICALL //g" | cut -d\( -f1 | sed -e "s/ / /g"`)($ARG;" + else + echo -e " return (version_functions_table[lib_id].`echo $x | sed -e "s/#/ /g" | sed -e "s/.*JNICALL //g" | cut -d\( -f1 | sed -e "s/ / /g"`)($ARG;" + fi + + echo -e "}\n\n" + +done