From: Philippe E. <ph...@us...> - 2007-11-11 14:36:58
|
Update of /cvsroot/oprofile/oprofile/agents/jvmti In directory sc8-pr-cvs3.sourceforge.net:/tmp/cvs-serv4124/agents/jvmti Modified Files: Tag: JIT_SUPPORT libjvmti_oprofile.c Log Message: cleanup error handling in jvmti agent Index: libjvmti_oprofile.c =================================================================== RCS file: /cvsroot/oprofile/oprofile/agents/jvmti/Attic/libjvmti_oprofile.c,v retrieving revision 1.1.2.2 retrieving revision 1.1.2.3 diff -u -p -d -r1.1.2.2 -r1.1.2.3 --- libjvmti_oprofile.c 11 Nov 2007 11:39:27 -0000 1.1.2.2 +++ libjvmti_oprofile.c 11 Nov 2007 14:37:01 -0000 1.1.2.3 @@ -32,14 +32,23 @@ static int debug = 0; -void JNICALL cb_compiled_method_load( - jvmtiEnv * jvmti, - jmethodID method, - jint code_size, - const void * code_addr, - jint map_length, - const jvmtiAddrLocationMap * map, - const void * compile_info) +/** + * Handle an error or a warning, return 0 if the checked error is + * JVMTI_ERROR_NONE, i.e. success + */ +static int handle_error(jvmtiError err, char const * msg, int severe) +{ + if (err != JVMTI_ERROR_NONE) { + fprintf(stderr, "%s: %s, err code %i\n", + severe ? "Error" : "Warning", msg, err); + } + return err != JVMTI_ERROR_NONE; +} + +static void JNICALL cb_compiled_method_load(jvmtiEnv * jvmti, + jmethodID method, jint code_size, void const * code_addr, + jint map_length, jvmtiAddrLocationMap const * map, + void const * compile_info) { /* Get the declaring class of the method */ jclass declaring_class; @@ -54,23 +63,18 @@ void JNICALL cb_compiled_method_load( err = (*jvmti)->GetMethodDeclaringClass(jvmti, method, &declaring_class); - if (err != JVMTI_ERROR_NONE) { - fprintf(stderr, "Error, GetMethodDeclaringClass, rc=%i\n", - err); - goto cleanup; - } + if (handle_error(err, "GetMethodDeclaringClass()", 1)) + goto cleanup2; + err = (*jvmti)->GetClassSignature(jvmti, declaring_class, &class_signature, NULL); - if (err != JVMTI_ERROR_NONE) { - fprintf(stderr, "Error, GetClassSignature, rc=%i\n", err); - goto cleanup; - } + if (handle_error(err, "GetClassSignature()", 1)) + goto cleanup1; + err = (*jvmti)->GetMethodName(jvmti, method, &method_name, &method_signature, NULL); - if (err != JVMTI_ERROR_NONE) { - fprintf(stderr, "Error, GeMethodName, rc=%i\n", err); + if (handle_error(err, "GetMethodName()", 1)) goto cleanup; - } if (debug) { fprintf(stderr, "load: declaring_class=%p, class=%s, " @@ -82,67 +86,55 @@ void JNICALL cb_compiled_method_load( // produce a symbol name out of class name and method name { int cnt = strlen(method_name) + strlen(class_signature) + 2; - char buf[cnt]; - int len = 0; - buf[0] = 0; - if (*class_signature) { - strncat(buf, class_signature + 1, cnt - 1); - len = strlen(buf); - /* cut away the ; */ - if (len>0) - buf[len-1] = '/'; - } - strncat(buf, method_name, cnt-len - 1); - op_write_native_code(buf, code_addr, code_size); - } + char buf[cnt]; + int len = 0; + buf[0] = 0; + if (*class_signature) { + strncat(buf, class_signature + 1, cnt - 1); + len = strlen(buf); + /* cut away the ; */ + if (len > 0) + buf[len-1] = '/'; + } + strncat(buf, method_name, cnt-len - 1); + op_write_native_code(buf, code_addr, code_size); + } cleanup: - if (class_signature!=NULL) - (*jvmti)->Deallocate(jvmti, class_signature); - if (method_name!=NULL) - (*jvmti)->Deallocate(jvmti, method_name); - if (method_signature!=NULL) - (*jvmti)->Deallocate(jvmti, method_signature); + (*jvmti)->Deallocate(jvmti, method_name); + (*jvmti)->Deallocate(jvmti, method_signature); +cleanup1: + (*jvmti)->Deallocate(jvmti, class_signature); +cleanup2: ; } -void JNICALL cb_compiled_method_unload( - jvmtiEnv * jvmti_env, - jmethodID method, - const void * code_addr) +static void JNICALL cb_compiled_method_unload(jvmtiEnv * jvmti_env, + jmethodID method, void const * code_addr) { jvmti_env = jvmti_env; /* shut up compiler warning */ method = method; - if (debug) { - fprintf(stderr, "unload: addr=%llx \n", - (unsigned long long) (uintptr_t) code_addr); - } + if (debug) + fprintf(stderr, "unload: addr=%p\n", code_addr); op_unload_native_code(code_addr); } -void JNICALL cb_dynamic_code_generated( - jvmtiEnv * jvmti_env, - const char * name, - const void * code_addr, - jint code_size) +static void JNICALL cb_dynamic_code_generated(jvmtiEnv * jvmti_env, + char const * name, void const * code_addr, jint code_size) { jvmti_env = jvmti_env; /* shut up compiler warning */ if (debug) { - fprintf(stderr, "dyncode: name=%s, addr=%llx, size=%i \n", - name, - (unsigned long long) (uintptr_t) code_addr, - code_size); + fprintf(stderr, "dyncode: name=%s, addr=%p, size=%i \n", + name, code_addr, code_size); } op_write_native_code(name, code_addr, code_size); } -JNIEXPORT jint JNICALL Agent_OnLoad( - JavaVM * jvm, - char * options, - void * reserved) +JNIEXPORT jint JNICALL +Agent_OnLoad(JavaVM * jvm, char * options, void * reserved) { jint rc; - jvmtiEnv *jvmti = NULL; + jvmtiEnv * jvmti = NULL; jvmtiEventCallbacks callbacks; jvmtiCapabilities caps; jvmtiError error; @@ -156,27 +148,23 @@ JNIEXPORT jint JNICALL Agent_OnLoad( err = op_open_agent(); if (err) { - fprintf(stderr, "ERROR: op_open_agent, rc=%i\n", err); + fprintf(stderr, "Error: op_open_agent(), rc=%i\n", err); return -1; } rc = (*jvm)->GetEnv(jvm, (void *)&jvmti, JVMTI_VERSION_1); if (rc != JNI_OK) { - fprintf(stderr, "ERROR: GetEnv(), rc=%i\n", rc); + fprintf(stderr, "Error: GetEnv(), rc=%i\n", rc); return -1; } error = (*jvmti)->GetCapabilities(jvmti, &caps); - if (error != JNI_OK) { - fprintf(stderr, "ERROR: GetCapabilities(), rc=%i\n", error); + if (handle_error(error, "GetCapabilities()", 1)) return -1; - } caps.can_generate_compiled_method_load_events = 1; error = (*jvmti)->AddCapabilities(jvmti, &caps); - if (error != JNI_OK) { - fprintf(stderr, "ERROR: AddCapabilities(), rc=%i\n", error); + if (handle_error(error, "AddCapabilities()", 1)) return -1; - } memset(&callbacks, 0, sizeof(callbacks)); callbacks.CompiledMethodLoad = cb_compiled_method_load; @@ -184,17 +172,24 @@ JNIEXPORT jint JNICALL Agent_OnLoad( callbacks.DynamicCodeGenerated = cb_dynamic_code_generated; error = (*jvmti)->SetEventCallbacks(jvmti, &callbacks, sizeof(callbacks)); - if (error != JNI_OK) { - fprintf(stderr, "ERROR: SetEventCallbacks(), rc=%i\n", error); + if (handle_error(error, "SetEventCallbacks()", 1)) return -1; - } error = (*jvmti)->SetEventNotificationMode(jvmti, JVMTI_ENABLE, JVMTI_EVENT_COMPILED_METHOD_LOAD, NULL); + if (handle_error(error, "SetEventNotificationMode() " + "JVMTI_EVENT_COMPILED_METHOD_LOAD", 1)) + return -1; error = (*jvmti)->SetEventNotificationMode(jvmti, JVMTI_ENABLE, JVMTI_EVENT_COMPILED_METHOD_UNLOAD, NULL); + if (handle_error(error, "SetEventNotificationMode() " + "JVMTI_EVENT_COMPILED_METHOD_UNLOAD", 1)) + return -1; error = (*jvmti)->SetEventNotificationMode(jvmti, JVMTI_ENABLE, JVMTI_EVENT_DYNAMIC_CODE_GENERATED, NULL); + if (handle_error(error, "SetEventNotificationMode() " + "JVMTI_EVENT_DYNAMIC_CODE_GENERATED", 1)) + return -1; return 0; } |