void JNICALL callbackThreadStart(prms_THRD_START)
{
    check_THRD_START;

    if (flag) return;

    jvmtiPhase phase;
    jvmtiThreadInfo tinfo;
    jvmtiError result;

    result = jvmti_env->GetPhase(&phase);
    fprintf(stderr, "\tnative: GetPhase result = %d (must be zero) \n", result);
    fprintf(stderr, "\tnative: current phase is %d (must be 4 (LIVE-phase)) \n", phase);
    if ((result != JVMTI_ERROR_NONE) || (phase != JVMTI_PHASE_LIVE)) return;
    result = jvmti_env->GetThreadInfo(thread, &tinfo);
    fprintf(stderr, "\tnative: GetThreadInfo result = %d (must be zero) \n", result);
    fprintf(stderr, "\tnative: current thread name is %s (must be zero) \n", tinfo.name);

    if (result != JVMTI_ERROR_NONE) return;
    if (strcmp(tinfo.name, "agent")) return;


    fprintf(stderr, "\tnative: JNI: funcs start\n");

    jclass clazz = jni_env->FindClass("java/lang/Thread");

    if (clazz)
        fprintf(stderr, "\tnative: JNI: FindClass - Ok\n");
    else return;

    jmethodID mid = jni_env->GetMethodID(clazz, "<init>", "()V");

    if (mid)
        fprintf(stderr, "\tnative: JNI: GetMethodID - Ok\n");
    else return;

    jthread thread_1 = jni_env->NewObject(clazz, mid, "native_agent_thread");

    if (thread_1)
        fprintf(stderr, "\tnative: JNI: NewObject - Ok\n");
    else return;

    util = true;
    flag = true;

    result = jvmti_env->InterruptThread(thread_1);
    fprintf(stderr, "\tnative: InterruptThread result = %d (must be JVMTI_ERROR_THREAD_NOT_ALIVE(15)) \n", result);

    if ((int)result == 666)
        fprintf(stderr, "\n\tInterruptThread: FUNCTION IS NOT IMPLEMENTED NOW !!!!!");

    if (result != JVMTI_ERROR_THREAD_NOT_ALIVE) return;

    jint state = 0;
    result = jvmti_env->GetThreadState(thread_1, &state);
    fprintf(stderr, "\tnative: GetThreadState result = %d (must be zero) \n", result);
    fprintf(stderr, "\tnative: state of tested thread is %d \n", state);
    fprintf(stderr, "\tnative: state of tested thread is ");
    thread_state_output_as_string(state);
    fprintf(stderr, "\n"); fflush(stderr);

    if (result != JVMTI_ERROR_NONE) return;
    test = true;
}
void JNICALL callbackThreadStart(prms_THRD_START)
{
    check_THRD_START;

    if (flag) return;

    jvmtiPhase phase;
    jvmtiThreadInfo tinfo;
    jvmtiError result;
    jint tcount;
    jthread* threads;
    jthread my_thread = NULL;
    long ethalon = JVMTI_THREAD_STATE_ALIVE | JVMTI_THREAD_STATE_RUNNABLE | JVMTI_THREAD_STATE_INTERRUPTED;

    /* Anti-Warnings code */
    if (jni_env == NULL)
        fprintf(stderr, "\tnative: callbackThreadStart was called with jni_env = NULL\n");

    result = jvmti_env->GetPhase(&phase);

    fprintf(stderr, "\tnative: GetPhase result = %d (must be zero) \n", result);
    fprintf(stderr, "\tnative: current phase is %d (must be 4 (LIVE-phase)) \n", phase);
    if ((result != JVMTI_ERROR_NONE) || (phase != JVMTI_PHASE_LIVE)) return;
    result = jvmti_env->GetThreadInfo(thread, &tinfo);
    fprintf(stderr, "\tnative: GetThreadInfo result = %d (must be zero) \n", result);
    fprintf(stderr, "\tnative: current thread name is %s (must be zero) \n", tinfo.name);
    if (result != JVMTI_ERROR_NONE) return;
    if (strcmp(tinfo.name, "agent")) return;
    fprintf(stderr, "\tnative: test started\n");

    flag = true;

    if (!util) return;

    result = jvmti_env->GetAllThreads(&tcount, &threads);
    fprintf(stderr, "\tnative: GetAllThreads result = %d (must be zero) \n", result);
    if (result != JVMTI_ERROR_NONE) return;

    for ( int i = 0; i < tcount; i++ )
    {
        result = jvmti_env->GetThreadInfo(threads[i], &tinfo);
        fprintf(stderr, "\tnative: GetThreadInfo result = %d (must be zero) \n", result);
        fprintf(stderr, "\tnative: current thread name is %s (must be zero) \n", tinfo.name);
        if (result != JVMTI_ERROR_NONE) continue;
        if (strcmp(tinfo.name, "SuperPuperTest")) continue;
        my_thread = threads[i];
        fprintf(stderr, "\tnative: tested thread was found = %p\n", my_thread);
        break;
    }

    jint state = 0;

    if (!my_thread) return;
    result = jvmti_env->StopThread(my_thread, stored_exception);
    fprintf(stderr, "\tnative: StopThread result = %d (must be zero) \n", result);

    if ((int)result == 666)
        fprintf(stderr, "\n\tStopThread: FUNCTION IS NOT IMPLEMENTED NOW !!!!!");

    if (result != JVMTI_ERROR_NONE) return;
    result = jvmti_env->GetThreadState(my_thread, &state);
    fprintf(stderr, "\tnative: GetThreadState result = %d (must be zero) \n", result);
    fprintf(stderr, "\tnative: state of tested thread is %d \n", state);
    fprintf(stderr, "\tnative: state of tested thread is ");
    thread_state_output_as_string(state);
    fprintf(stderr, "\n"); fflush(stderr);

    if (result != JVMTI_ERROR_NONE) return;
    if (state == ethalon) test = true;
}
Exemplo n.º 3
0
void JNICALL callbackThreadStart(prms_THRD_START)
{
    check_THRD_START;

    if (testStarted) return;

    jvmtiPhase phase;
    jvmtiThreadInfo tinfo;
    jint tcount = 0;
    jint ind = 0;
    int i = 0, j = 0;
    jthread* threads;
    jthread my_threads[3] = {NULL, NULL, NULL};
    jvmtiError result;
    int ethalon_state = JVMTI_THREAD_STATE_ALIVE | JVMTI_THREAD_STATE_RUNNABLE;

    result = jvmti_env->GetPhase(&phase);
    fprintf(stderr, "\tnative: GetPhase result = %d (must be zero) \n", result);
    fprintf(stderr, "\tnative: current phase is %d (must be 4 (LIVE-phase)) \n", phase);

    if ((result != JVMTI_ERROR_NONE) || (phase != JVMTI_PHASE_LIVE)) return;
    result = jvmti_env->GetThreadInfo(thread, &tinfo);
    fprintf(stderr, "\tnative: GetThreadInfo result = %d (must be zero) \n", result);
    if (result != JVMTI_ERROR_NONE) return;
    fprintf(stderr, "\tnative: started thread name is %s (must be \"agent\") \n", tinfo.name);
    if (strcmp(tinfo.name, "agent")) return;
    fprintf(stderr, "\tnative: test is started\n");

    testStarted = true;
    
    result = jvmti_env->GetAllThreads(&tcount, &threads);
    fprintf(stderr, "\tnative: GetAllThreads result = %d (must be zero) \n", result);
    if (result != JVMTI_ERROR_NONE) return;

    for ( i = 0; i < tcount; i++ )
        if (jvmti_env->GetThreadInfo(threads[i], &tinfo) == JVMTI_ERROR_NONE)
            if (!strncmp(tinfo.name, "SuperPuperTest", 14))
            {
                my_threads[ind] = threads[i];
                if ((ind++) == 4) return;
            }

    if (ind != 3) return;
    fprintf(stderr, "\tnative: all tested threads were found \n");
    ind = 0;
    jint state[3];
    jvmtiError results[3] = { JVMTI_ERROR_NONE, JVMTI_ERROR_NONE, JVMTI_ERROR_NONE};

    for ( j = 0; j < 3; j++ )
    {
		if (results[j] != JVMTI_ERROR_NONE){ 			
			jvmti_env->GetThreadState(my_threads[j], &(state[j]));		
			continue;
		}
        result = jvmti_env->GetThreadState(my_threads[j], &(state[j]));
        fprintf(stderr, "\tnative: GetThreadState result = %d (must be zero) \n", result);
        if (result != JVMTI_ERROR_NONE) continue;
        fprintf(stderr, "\tnative: state of thread %d is %d\n", j, state[j]);
        fprintf(stderr, "\tnative: state of thread %d is ", j);
        thread_state_output_as_string(state[j]);
        fprintf(stderr, "\n"); fflush(stderr);
    }

    { /*** Turn OFF capability ***/
        jvmtiCapabilities zero_caps;
        jvmtiError result;
        make_caps_zero(&zero_caps);
        zero_caps.can_suspend = 1;
        result = jvmti_env->RelinquishCapabilities(&zero_caps);
        if (result != JVMTI_ERROR_NONE)
        {
            fprintf(stderr,
                "Error during RelinquishCapabilities. Test Stopped\n");
            fflush(stderr);
            return;
        }
    }

     //useless
		/*{ Check of capability 
        jvmtiCapabilities full_caps;
        jvmtiCapabilities caps;
        make_caps_set_all(&full_caps);
        jvmtiError result = jvmti_env->GetCapabilities(&caps);
        if (result != JVMTI_ERROR_NONE)
        {
            fprintf(stderr,
                "Error during GetCapabilities. Test Stopped\n");
            fflush(stderr);
            return;
        }
        
		int diff = cmp_caps_and_output_results(full_caps, caps,"potential", "current");
		}*/
    
    fprintf(stderr, "\tnative: ResumeThreadList w/o capability \n");
    result = jvmti_env->ResumeThreadList(3, my_threads, results);
    fprintf(stderr, "\tnative: ResumeThreadList result = %d (must be JVMTI_ERROR_MUST_POSSESS_CAPABILITY (99)) \n", result);
    fflush(stderr);
    if ( result != JVMTI_ERROR_MUST_POSSESS_CAPABILITY) return;

    successful = true;
}
void JNICALL callbackThreadStart(prms_THRD_START)
{
    check_THRD_START;

    if (flag) return;

    jint state = 0;
    jvmtiPhase phase;
    jvmtiThreadInfo tinfo;
    jvmtiError result;
    jthread* threads;
    jint tcount;
    jthread my_thread = NULL;

    result = jvmti_env->GetPhase(&phase);
    fprintf(stderr, "\tnative: GetPhase result = %d (must be zero) \n", result);
    fprintf(stderr, "\tnative: current phase is %d (must be 4 (LIVE-phase)) \n", phase);
    if ((result != JVMTI_ERROR_NONE) || (phase != JVMTI_PHASE_LIVE)) return;
    result = jvmti_env->GetThreadInfo(thread, &tinfo);
    fprintf(stderr, "\tnative: GetThreadInfo result = %d (must be zero) \n", result);
    if (result != JVMTI_ERROR_NONE) return;
    fprintf(stderr, "\tnative: started thread name is %s (must be \"agent\") \n", tinfo.name);
    if (strcmp(tinfo.name, "agent")) return;
    fprintf(stderr, "\tnative: test is started\n");

    flag = true;
    util = true;

    result = jvmti_env->GetAllThreads(&tcount, &threads);
    fprintf(stderr, "\tnative: GetAllThreads result = %d (must be zero) \n", result);
    if (result != JVMTI_ERROR_NONE) return;

    for ( int i = 0; i < tcount; i++ )
        if (jvmti_env->GetThreadInfo(threads[i], &tinfo) == JVMTI_ERROR_NONE)
            if (!strcmp(tinfo.name, "SuperPuperTest"))
            {
                fprintf(stderr, "\tnative: tested thread was found\n");
                my_thread = threads[i];
                break;
            }

    if (!my_thread)
    {
        fprintf(stderr, "\tnative: tested thread is NULL. Test stopped.\n");
        return;
    }

    result = jvmti_env->SuspendThread(my_thread);

    fprintf(stderr, "\tnative: SuspendThread result = %d (must be zero) \n", result);

    if (result != JVMTI_ERROR_NONE) return;

    result = jvmti_env->GetThreadState(my_thread, &state);
    fprintf(stderr, "\tnative: GetThreadState result = %d (must be zero) \n", result);
    fprintf(stderr, "\tnative: state of tested thread is %d \n", state);
    fprintf(stderr, "\tnative: state of tested thread is ");
    thread_state_output_as_string(state);
    fprintf(stderr, "\n"); fflush(stderr);

    if ( result != JVMTI_ERROR_NONE) return;
    int ethalon_state = JVMTI_THREAD_STATE_ALIVE |
            JVMTI_THREAD_STATE_RUNNABLE |
            JVMTI_THREAD_STATE_SUSPENDED;

    if (state == ethalon_state) test = true;

    result = jvmti_env->ResumeThread(my_thread);
    fprintf(stderr, "\tnative: ResumeThread result = %d (must be zero) \n", result);

    if (result != JVMTI_ERROR_NONE) return;
    result = jvmti_env->GetThreadState(my_thread, &state);
    fprintf(stderr, "\tnative: GetThreadState result = %d (must be zero) \n", result);
    fprintf(stderr, "\tnative: state of tested thread is %d \n", state);
    fprintf(stderr, "\tnative: state of tested thread is ");
    thread_state_output_as_string(state);
    fprintf(stderr, "\n"); fflush(stderr);
}
void JNICALL callbackThreadStart(prms_THRD_START)
{
    check_THRD_START;

    if (testStarted) return;

    jvmtiPhase phase;
    jvmtiThreadInfo tinfo;
    jint tcount = 0;
    jint ind = 0;
    int i = 0, j = 0;
    jthread* threads;
    jthread my_threads[3] = {NULL, NULL, NULL};
    jvmtiError result;
    jint state[3];

    int ethalon_state = JVMTI_THREAD_STATE_SUSPENDED;

    result = jvmti_env->GetPhase(&phase);
    fprintf(stderr, "\tnative: GetPhase result = %d (must be zero) \n", result);
    fprintf(stderr, "\tnative: current phase is %d (must be 4 (LIVE-phase)) \n", phase);
    if ((result != JVMTI_ERROR_NONE) || (phase != JVMTI_PHASE_LIVE)) return;
    result = jvmti_env->GetThreadInfo(thread, &tinfo);
    fprintf(stderr, "\tnative: GetThreadInfo result = %d (must be zero) \n", result);
    if (result != JVMTI_ERROR_NONE) return;
    fprintf(stderr, "\tnative: started thread name is %s (must be \"agent\") \n", tinfo.name);
    if (strcmp(tinfo.name, "agent")) return;
    fprintf(stderr, "\tnative: test is started\n");

    testStarted = true;


    result = jvmti_env->GetAllThreads(&tcount, &threads);
    fprintf(stderr, "\tnative: GetAllThreads result = %d (must be zero) \n", result);
    if (result != JVMTI_ERROR_NONE) return;
    for ( i = 0; i < tcount; i++ )
        if (jvmti_env->GetThreadInfo(threads[i], &tinfo) == JVMTI_ERROR_NONE)
            if (!strncmp(tinfo.name, "SuperPuperTest", 14))
            {
                my_threads[ind] = threads[i];
                if ((ind++) == 4) return;
            }

    if (ind != 3) return;

    fprintf(stderr, "\tnative: all tested threads were found \n");
    ind = 0;
    jvmtiError results[3] = { JVMTI_ERROR_NONE, JVMTI_ERROR_NONE, JVMTI_ERROR_NONE};
    result = jvmti_env->SuspendThreadList(3, my_threads, results);
    fprintf(stderr, "\tnative: SuspendThreadList result = %d (must be zero) \n", result);
    for ( i = 0; i < 3; i++)
        fprintf(stderr, "\tnative: result[%d] = %d (must be zero) \n", i, results[i]);

    fflush(stderr);

    if (result != JVMTI_ERROR_NONE) return;

    for ( j = 0; j < 3; j++ )
    {
		if (results[j] != JVMTI_ERROR_NONE){
			jvmti_env->GetThreadState(my_threads[j], &(state[j]));		
			continue;
		}
        result = jvmti_env->GetThreadState(my_threads[j], &(state[j]));
        fprintf(stderr, "\tnative: GetThreadState result = %d (must be zero) \n", result);
		if (result != JVMTI_ERROR_NONE) continue;
        fprintf(stderr, "\tnative: state of thread %d is %d\n", j, state[j]);
        fprintf(stderr, "\tnative: state of thread %d is ", j);
        thread_state_output_as_string(state[j]);
        fprintf(stderr, "\n"); fflush(stderr);
//compare with ethalon state	
	successful = (state[j]&ethalon_state)&&successful;
    }

    //if ((state[0] & ethalon_state) && (state[1] & ethalon_state) && (state[2] & ethalon_state)) successful = true;
    if (jvmti_env->ResumeThreadList(successful, my_threads, results) ==
            JVMTI_ERROR_NONE) return;
}
void JNICALL callbackThreadStart(prms_THRD_START)
{
    check_THRD_START;

    if (flag) return;

    jvmtiPhase phase;
    jvmtiThreadInfo tinfo;
    jvmtiError result;

    result = jvmti_env->GetPhase(&phase);
    fprintf(stderr, "\tnative: GetPhase result = %d (must be zero) \n", result);
    fprintf(stderr, "\tnative: current phase is %d (must be 4 (LIVE-phase)) \n", phase);
    if ((result != JVMTI_ERROR_NONE) || (phase != JVMTI_PHASE_LIVE)) return;
    result = jvmti_env->GetThreadInfo(thread, &tinfo);
    fprintf(stderr, "\tnative: GetThreadInfo result = %d (must be zero) \n", result);
    if (result != JVMTI_ERROR_NONE) return;
    fprintf(stderr, "\tnative: started thread name is %s (must be \"agent\") \n", tinfo.name);
    if (strcmp(tinfo.name, "agent")) return;
    fprintf(stderr, "\tnative: test is started\n");

    flag = true;
    util = true;

    fprintf(stderr, "\tnative: JNI: funcs start\n");

    jclass clazz = jni_env->FindClass("java/lang/Thread");

    if (clazz)
        fprintf(stderr, "\tnative: JNI: FindClass - Ok\n");
    else return;

    jmethodID mid = jni_env->GetMethodID(clazz, "<init>", "()V");

    if (mid)
        fprintf(stderr, "\tnative: JNI: GetMethodID - Ok\n");
    else return;

    jthread thread_1 = jni_env->NewObject(clazz, mid, "native_agent_thread");

    if (thread_1)
        fprintf(stderr, "\tnative: JNI: NewObject - Ok\n");
    else return;

    fprintf(stderr, "\tnative: SuspendThread with not alive thread\n");
    fflush(stderr);
    jint state = 0;
    result = jvmti_env->GetThreadState(thread_1, &state);
    fprintf(stderr, "\tnative: GetThreadState result = %d (must be zero) \n", result);
    fprintf(stderr, "\tnative: state of tested thread is %d \n", state);
    fprintf(stderr, "\tnative: state of tested thread is ");
    thread_state_output_as_string(state);
    fprintf(stderr, "\n"); fflush(stderr);
    if ( result != JVMTI_ERROR_NONE) return;
    int ethalon_state = JVMTI_THREAD_STATE_ALIVE |
            JVMTI_THREAD_STATE_RUNNABLE;

    fprintf(stderr, "\tnative: ResumeThread for NOT alive thread \n");
    result = jvmti_env->ResumeThread(thread_1);
    fprintf(stderr, "\tnative: ResumeThread result = %d (must be JVMTI_ERROR_THREAD_NOT_ALIVE (15)) \n", result);

    if ( result != JVMTI_ERROR_THREAD_NOT_ALIVE) return;

    test = true;
}