java interface: add script to automatically generate lib loader C code traceformat-2.6
authorPierre-Marc Fournier <pierre-marc.fournier@polymtl.ca>
Thu, 13 May 2010 16:41:41 +0000 (12:41 -0400)
committerPierre-Marc Fournier <pierre-marc.fournier@polymtl.ca>
Thu, 13 May 2010 16:41:41 +0000 (12:41 -0400)
scripts/autogen_lttvtraceread_loader.sh [new file with mode: 0644]

diff --git a/scripts/autogen_lttvtraceread_loader.sh b/scripts/autogen_lttvtraceread_loader.sh
new file mode 100644 (file)
index 0000000..87c3e88
--- /dev/null
@@ -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 <jni.h>"
+echo -e "#include <ltt/trace.h>"
+echo -e "#include <ltt/time.h>"
+echo -e "#include <ltt/marker.h>"
+echo -e "#include <glib.h>"
+echo -e ""
+echo -e "#include <stdlib.h>"
+echo -e "#include <stdio.h>"
+echo -e "#include <string.h>"
+echo -e "#include <stdint.h>"
+echo -e "#include <dlfcn.h>"
+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; n<nb_id; n++) {"
+echo -e "                if ( version_table[n].static_handle != NULL ) {"
+echo -e "                        isEmpty = 0;"
+echo -e "                }"
+echo -e "        }"
+echo -e ""
+echo -e "        if ( isEmpty == 1 ) {"
+echo -e "                freeAllHandle();"
+echo -e "        }"
+echo -e "}"
+echo -e ""
+echo -e "JNIEXPORT void JNICALL Java_org_eclipse_linuxtools_lttng_jni_JniTrace_ltt_1freeHandle(JNIEnv *env, jobject jobj, jint lib_id) {"
+echo -e "        /* Call function to free the memory */"
+echo -e "        freeHandle(lib_id);"
+echo -e "}"
+echo -e ""
+echo -e "JNIEXPORT jint JNICALL Java_org_eclipse_linuxtools_lttng_jni_JniTrace_ltt_1initializeHandle(JNIEnv *env, jobject jobj, jstring libname) {"
+echo -e ""
+echo -e "        jint lib_id = -1;"
+echo -e "        const char* c_path = (*env)->GetStringUTFChars(env, libname, 0);"
+echo -e ""
+echo -e "        int isLoaded = 0;"
+echo -e "        int n;"
+echo -e "        for ( n=0; n<nb_id; n++) {"
+echo -e "                if ( strncmp(version_table[n].libname, c_path, strlen(version_table[n].libname) ) == 0 ) {"
+echo -e "                        isLoaded = 1;"
+echo -e "                        lib_id = version_table[n].id;"
+echo -e "                }"
+echo -e "        }"
+echo -e ""
+echo -e "        if ( isLoaded == 0 ) {"
+echo -e "                void *new_handle = dlopen(c_path, RTLD_LAZY );"
+echo -e ""
+echo -e "                if (!new_handle ) {"
+echo -e "                        printf (\"WARNING : Failed to initialize library handle from %s!\\\n\", c_path);"
+echo -e "                }"
+echo -e "                else {"
+echo -e "                        lib_id = nb_id;"
+echo -e "                        nb_id++;"
+echo -e ""
+echo -e "                        void* new_version_table = malloc(sizeof(struct version_correlation)*(nb_id) );"
+echo -e "                        void* new_function_tables = malloc(sizeof(struct function_tables)*(nb_id) );"
+echo -e ""
+echo -e "                        if ( nb_id > 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
This page took 0.025166 seconds and 4 git commands to generate.