| 1 | #!/bin/bash |
| 2 | |
| 3 | JNI_PATH="$1" |
| 4 | |
| 5 | if [ "$JNI_PATH" = "" ]; then |
| 6 | echo "No jni path given!" |
| 7 | exit 1 |
| 8 | fi |
| 9 | |
| 10 | |
| 11 | |
| 12 | echo -e "" |
| 13 | echo -e "/* Important to get consistent size_t type */" |
| 14 | echo -e "#define _FILE_OFFSET_BITS 64" |
| 15 | echo -e "" |
| 16 | echo -e "#include <jni.h>" |
| 17 | echo -e "#include <ltt/trace.h>" |
| 18 | echo -e "#include <ltt/time.h>" |
| 19 | echo -e "#include <ltt/marker.h>" |
| 20 | echo -e "#include <glib.h>" |
| 21 | echo -e "" |
| 22 | echo -e "#include <stdlib.h>" |
| 23 | echo -e "#include <stdio.h>" |
| 24 | echo -e "#include <string.h>" |
| 25 | echo -e "#include <stdint.h>" |
| 26 | echo -e "#include <dlfcn.h>" |
| 27 | echo -e "" |
| 28 | echo -e "int nb_id = 0;" |
| 29 | echo -e "" |
| 30 | echo -e "struct version_correlation {" |
| 31 | echo -e " int id;" |
| 32 | echo -e " char *libname;" |
| 33 | echo -e " void *static_handle;" |
| 34 | echo -e "};" |
| 35 | echo -e "" |
| 36 | echo -e "struct version_correlation *version_table = NULL;" |
| 37 | echo -e "" |
| 38 | echo -e "struct function_tables {" |
| 39 | for x in `grep JNIEXPORT $JNI_PATH | grep -v getTraceVersion | sed -e "s/{//g" | sed -e "s/ /#/g"`; do |
| 40 | 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"`;" |
| 41 | done |
| 42 | echo -e "};" |
| 43 | echo -e "" |
| 44 | echo -e "struct function_tables *version_functions_table = NULL;" |
| 45 | echo -e "" |
| 46 | echo -e "void ignore_and_drop_message(const gchar *log_domain, GLogLevelFlags log_level, const gchar *message, gpointer user_data) {" |
| 47 | echo -e "}" |
| 48 | echo -e "" |
| 49 | echo -e "JNIEXPORT void JNICALL Java_org_eclipse_linuxtools_lttng_jni_factory_JniTraceVersion_ltt_1getTraceVersion(JNIEnv *env, jobject jobj, jstring tracepath) {" |
| 50 | echo -e "" |
| 51 | echo -e " void *handle = dlopen(\"liblttvtraceread.so\", RTLD_LAZY );" |
| 52 | echo -e "" |
| 53 | echo -e " if (!handle ) {" |
| 54 | echo -e " printf (\"WARNING : Failed to initialize library handle from %s!\\\n\", \"liblttvtraceread.so\");" |
| 55 | echo -e " }" |
| 56 | echo -e " JNIEXPORT void JNICALL (*functor)(JNIEnv *env, jobject jobj, jstring tracepath);" |
| 57 | echo -e " functor=dlsym(handle, \"Java_org_eclipse_linuxtools_lttng_jni_factory_JniTraceVersion_ltt_1getTraceVersion\");" |
| 58 | echo -e "" |
| 59 | echo -e " char *error = dlerror();" |
| 60 | echo -e " if ( error != NULL) {" |
| 61 | echo -e " printf (\"Call failed with : %s\\\n\", error);" |
| 62 | echo -e " }" |
| 63 | echo -e " else {" |
| 64 | echo -e " (*functor)(env, jobj, tracepath);" |
| 65 | echo -e " }" |
| 66 | echo -e "}" |
| 67 | echo -e "" |
| 68 | echo -e "void freeAllHandle() {" |
| 69 | echo -e " if ( version_table != NULL ) {" |
| 70 | echo -e " free(version_table);" |
| 71 | echo -e " version_table = NULL;" |
| 72 | echo -e " }" |
| 73 | echo -e "" |
| 74 | echo -e " if ( version_functions_table != NULL ) {" |
| 75 | echo -e " free(version_functions_table);" |
| 76 | echo -e " version_functions_table = NULL;" |
| 77 | echo -e " }" |
| 78 | echo -e "}" |
| 79 | echo -e "" |
| 80 | echo -e "void freeHandle(int handle_id) {" |
| 81 | echo -e " if ( handle_id >= nb_id ) {" |
| 82 | echo -e " if (version_table[handle_id].static_handle != NULL) {" |
| 83 | echo -e " /* Memory will be freed by dlclose as well */" |
| 84 | echo -e " dlclose(version_table[handle_id].static_handle);" |
| 85 | echo -e " version_table[handle_id].static_handle = NULL;" |
| 86 | echo -e " free(version_table[handle_id].libname);" |
| 87 | echo -e " version_table[handle_id].libname = NULL;" |
| 88 | echo -e " }" |
| 89 | echo -e " }" |
| 90 | echo -e "" |
| 91 | echo -e " int isEmpty = 1;" |
| 92 | echo -e " int n;" |
| 93 | echo -e " for ( n=0; n<nb_id; n++) {" |
| 94 | echo -e " if ( version_table[n].static_handle != NULL ) {" |
| 95 | echo -e " isEmpty = 0;" |
| 96 | echo -e " }" |
| 97 | echo -e " }" |
| 98 | echo -e "" |
| 99 | echo -e " if ( isEmpty == 1 ) {" |
| 100 | echo -e " freeAllHandle();" |
| 101 | echo -e " }" |
| 102 | echo -e "}" |
| 103 | echo -e "" |
| 104 | echo -e "JNIEXPORT void JNICALL Java_org_eclipse_linuxtools_lttng_jni_JniTrace_ltt_1freeHandle(JNIEnv *env, jobject jobj, jint lib_id) {" |
| 105 | echo -e " /* Call function to free the memory */" |
| 106 | echo -e " freeHandle(lib_id);" |
| 107 | echo -e "}" |
| 108 | echo -e "" |
| 109 | echo -e "JNIEXPORT jint JNICALL Java_org_eclipse_linuxtools_lttng_jni_JniTrace_ltt_1initializeHandle(JNIEnv *env, jobject jobj, jstring libname) {" |
| 110 | echo -e "" |
| 111 | echo -e " jint lib_id = -1;" |
| 112 | echo -e " const char* c_path = (*env)->GetStringUTFChars(env, libname, 0);" |
| 113 | echo -e "" |
| 114 | echo -e " int isLoaded = 0;" |
| 115 | echo -e " int n;" |
| 116 | echo -e " for ( n=0; n<nb_id; n++) {" |
| 117 | echo -e " if ( strncmp(version_table[n].libname, c_path, strlen(version_table[n].libname) ) == 0 ) {" |
| 118 | echo -e " isLoaded = 1;" |
| 119 | echo -e " lib_id = version_table[n].id;" |
| 120 | echo -e " }" |
| 121 | echo -e " }" |
| 122 | echo -e "" |
| 123 | echo -e " if ( isLoaded == 0 ) {" |
| 124 | echo -e " void *new_handle = dlopen(c_path, RTLD_LAZY );" |
| 125 | echo -e "" |
| 126 | echo -e " if (!new_handle ) {" |
| 127 | echo -e " printf (\"WARNING : Failed to initialize library handle from %s!\\\n\", c_path);" |
| 128 | echo -e " }" |
| 129 | echo -e " else {" |
| 130 | echo -e " lib_id = nb_id;" |
| 131 | echo -e " nb_id++;" |
| 132 | echo -e "" |
| 133 | echo -e " void* new_version_table = malloc(sizeof(struct version_correlation)*(nb_id) );" |
| 134 | echo -e " void* new_function_tables = malloc(sizeof(struct function_tables)*(nb_id) );" |
| 135 | echo -e "" |
| 136 | echo -e " if ( nb_id > 1) {" |
| 137 | echo -e " memcpy(new_version_table,version_table, sizeof(struct version_correlation)*(nb_id-1) );" |
| 138 | echo -e " free( version_table );" |
| 139 | echo -e "" |
| 140 | echo -e " memcpy(new_function_tables,version_functions_table, sizeof(struct function_tables)*(nb_id-1) );" |
| 141 | echo -e " free( version_functions_table);" |
| 142 | echo -e " }" |
| 143 | echo -e "" |
| 144 | echo -e " version_table = (struct version_correlation *)new_version_table;" |
| 145 | echo -e " version_table[lib_id].id = lib_id;" |
| 146 | echo -e " version_table[lib_id].libname = (char*)malloc( strlen(c_path) );" |
| 147 | echo -e " strncpy(version_table[lib_id].libname, c_path, strlen(c_path));" |
| 148 | echo -e " version_table[lib_id].static_handle = new_handle;" |
| 149 | echo -e "" |
| 150 | echo -e " version_functions_table = (struct function_tables*)new_function_tables;" |
| 151 | echo -e "" |
| 152 | for x in `grep JNIEXPORT $JNI_PATH | grep -v getTraceVersion |sed -e "s/{//g" | sed -e "s/ /#/g"`; do |
| 153 | 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"`\");" |
| 154 | done |
| 155 | echo -e " }" |
| 156 | echo -e " }" |
| 157 | echo -e "" |
| 158 | echo -e " return lib_id;" |
| 159 | echo -e "}" |
| 160 | echo -e "" |
| 161 | |
| 162 | for x in `grep JNIEXPORT $JNI_PATH | grep -v getTraceVersion | sed -e "s/{//g" | sed -e "s/ /#/g"`; do |
| 163 | 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"`{"; |
| 164 | |
| 165 | TEST=`echo $x | sed -e "s/#/ /g" | awk {print'$2'}` |
| 166 | |
| 167 | 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"` |
| 168 | |
| 169 | if [ "$TEST" = "void" ]; then |
| 170 | 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;" |
| 171 | else |
| 172 | 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;" |
| 173 | fi |
| 174 | |
| 175 | echo -e "}\n\n" |
| 176 | |
| 177 | done |