util.c

来自「This is a resource based on j2me embedde」· C语言 代码 · 共 2,125 行 · 第 1/5 页

C
2,125
字号
        case JDWP_TAG(CHAR):            (void)outStream_writeChar(out,                       JNI_FUNC_PTR(env,GetStaticCharField)(env, clazz, field));            break;            case JDWP_TAG(FLOAT):            (void)outStream_writeFloat(out,                       JNI_FUNC_PTR(env,GetStaticFloatField)(env, clazz, field));            break;            case JDWP_TAG(DOUBLE):            (void)outStream_writeDouble(out,                       JNI_FUNC_PTR(env,GetStaticDoubleField)(env, clazz, field));            break;            case JDWP_TAG(INT):            (void)outStream_writeInt(out,                       JNI_FUNC_PTR(env,GetStaticIntField)(env, clazz, field));            break;            case JDWP_TAG(LONG):            (void)outStream_writeLong(out,                       JNI_FUNC_PTR(env,GetStaticLongField)(env, clazz, field));            break;            case JDWP_TAG(SHORT):            (void)outStream_writeShort(out,                       JNI_FUNC_PTR(env,GetStaticShortField)(env, clazz, field));            break;            case JDWP_TAG(BOOLEAN):            (void)outStream_writeBoolean(out,                       JNI_FUNC_PTR(env,GetStaticBooleanField)(env, clazz, field));            break;    }}void sharedGetFieldValues(PacketInputStream *in, PacketOutputStream *out,                     jboolean isStatic){    JNIEnv *env = getEnv();    jint length;    jobject object;    jclass clazz;    object = NULL;    clazz  = NULL;        if (isStatic) {        clazz = inStream_readClassRef(env, in);    } else {        object = inStream_readObjectRef(env, in);    }    length = inStream_readInt(in);    if (inStream_error(in)) {        return;    }    WITH_LOCAL_REFS(env, length + 1) { /* +1 for class with instance fields */        int i;                (void)outStream_writeInt(out, length);        for (i = 0; (i < length) && !outStream_error(out); i++) {            jfieldID field = inStream_readFieldID(in);            if (isStatic) {                writeStaticFieldValue(env, out, clazz, field);            } else {                writeFieldValue(env, out, object, field);            }        }    } END_WITH_LOCAL_REFS(env);}jboolean sharedInvoke(PacketInputStream *in, PacketOutputStream *out){    jvalue *arguments = NULL;    jint options;    jvmtiError error;    jbyte invokeType;    jclass clazz;    jmethodID method;    jint argumentCount;    jobject instance;    jthread thread;    JNIEnv *env;    /*     * Instance methods start with the instance, thread and class,      * and statics and constructors start with the class and then the     * thread.     */    env = getEnv();    if (inStream_command(in) == JDWP_COMMAND(ObjectReference, InvokeMethod)) {        instance = inStream_readObjectRef(env, in);        thread = inStream_readThreadRef(env, in);        clazz = inStream_readClassRef(env, in);    } else { /* static method or constructor */        instance = NULL;        clazz = inStream_readClassRef(env, in);        thread = inStream_readThreadRef(env, in);    }    /*     * ... and the rest of the packet is identical for all commands     */    method = inStream_readMethodID(in);    argumentCount = inStream_readInt(in);    if (inStream_error(in)) {        return JNI_TRUE;    }    /* If count == 0, don't try and allocate 0 bytes, you'll get NULL */    if ( argumentCount > 0 ) {        int i;        /*LINTED*/        arguments = jvmtiAllocate(argumentCount * (jint)sizeof(*arguments));        if (arguments == NULL) {            outStream_setError(out, JDWP_ERROR(OUT_OF_MEMORY));            return JNI_TRUE;        }        for (i = 0; (i < argumentCount) && !inStream_error(in); i++) {            arguments[i] = inStream_readValue(in, NULL);        }        if (inStream_error(in)) {            return JNI_TRUE;        }    }        options = inStream_readInt(in);    if (inStream_error(in)) {        if ( arguments != NULL ) {            jvmtiDeallocate(arguments);        }        return JNI_TRUE;    }    if (inStream_command(in) == JDWP_COMMAND(ClassType, NewInstance)) {        invokeType = INVOKE_CONSTRUCTOR;    } else if (inStream_command(in) == JDWP_COMMAND(ClassType, InvokeMethod)) {        invokeType = INVOKE_STATIC;    } else if (inStream_command(in) == JDWP_COMMAND(ObjectReference, InvokeMethod)) {        invokeType = INVOKE_INSTANCE;    } else {        outStream_setError(out, JDWP_ERROR(INTERNAL));        if ( arguments != NULL ) {            jvmtiDeallocate(arguments);        }        return JNI_TRUE;    }    /*     * Request the invoke. If there are no errors in the request,     * the interrupting thread will actually do the invoke and a      * reply will be generated subsequently, so we don't reply here.     */    error = invoker_requestInvoke(invokeType, (jbyte)options, inStream_id(in),                                   thread, clazz, method,                                   instance, arguments, argumentCount);    if (error != JVMTI_ERROR_NONE) {        outStream_setError(out, map2jdwpError(error));        if ( arguments != NULL ) {            jvmtiDeallocate(arguments);        }        return JNI_TRUE;    }    return JNI_FALSE;   /* Don't reply */}jint uniqueID(void){    static jint currentID = 0;    return currentID++;}int filterDebugThreads(jthread *threads, int count){    int i;    int current;    /* Squish out all of the debugger-spawned threads */    for (i = 0, current = 0; i < count; i++) {        jthread thread = threads[i];        if (!threadControl_isDebugThread(thread)) {            if (i > current) {                threads[current] = thread;            }            current++;        }    }    return current;}jbyte referenceTypeTag(jclass clazz) {    jbyte tag;     if (isInterface(clazz)) {        tag = JDWP_TYPE_TAG(INTERFACE);    } else if (isArrayClass(clazz)) {        tag = JDWP_TYPE_TAG(ARRAY);    } else {        tag = JDWP_TYPE_TAG(CLASS);    }    return tag;}/** * Get field modifiers */jvmtiError fieldModifiers(jclass clazz, jfieldID field, jint *pmodifiers){    jvmtiError error;      *pmodifiers = 0;    error = JVMTI_FUNC_PTR(gdata->jvmti,GetFieldModifiers)            (gdata->jvmti, clazz, field, pmodifiers);    return error;}/** * Get method modifiers */jvmtiError methodModifiers(jmethodID method, jint *pmodifiers){    jvmtiError error;      *pmodifiers = 0;    error = JVMTI_FUNC_PTR(gdata->jvmti,GetMethodModifiers)            (gdata->jvmti, method, pmodifiers);    return error;}/* Returns a local ref to the declaring class for a method, or NULL. */jvmtiErrormethodClass(jmethodID method, jclass *pclazz){    jvmtiError error;    *pclazz = NULL;    error = FUNC_PTR(gdata->jvmti,GetMethodDeclaringClass)                                (gdata->jvmti, method, pclazz);    return error;}/* Returns a local ref to the declaring class for a method, or NULL. */jvmtiErrormethodLocation(jmethodID method, jlocation *ploc1, jlocation *ploc2){    jvmtiError error;    error = JVMTI_FUNC_PTR(gdata->jvmti,GetMethodLocation)                                (gdata->jvmti, method, ploc1, ploc2);    return error;}/** * Get method signature */jvmtiError methodSignature(jmethodID method,         char **pname, char **psignature, char **pgeneric_signature){    jvmtiError error;    char *name = NULL;    char *signature = NULL;    char *generic_signature = NULL;      error = FUNC_PTR(gdata->jvmti,GetMethodName)            (gdata->jvmti, method, &name, &signature, &generic_signature);        if ( pname != NULL ) {        *pname = name;    } else if ( name != NULL )  {        jvmtiDeallocate(name);    }     if ( psignature != NULL ) {        *psignature = signature;    } else if ( signature != NULL ) {        jvmtiDeallocate(signature);    }     if ( pgeneric_signature != NULL ) {        *pgeneric_signature = generic_signature;    } else if ( generic_signature != NULL )  {        jvmtiDeallocate(generic_signature);    }     return error;}/* * Get the return type key of the method  *     V or B C D F I J S Z L  [ */jvmtiErrormethodReturnType(jmethodID method, char *typeKey){    char       *signature;    jvmtiError  error;        signature = NULL;    error     = methodSignature(method, NULL, &signature, NULL);    if (error == JVMTI_ERROR_NONE) {        if (signature == NULL ) {            error = AGENT_ERROR_INVALID_TAG;        } else {            char * xx;                        xx = strchr(signature, ')');            if (xx == NULL || *(xx + 1) == 0) {                error = AGENT_ERROR_INVALID_TAG;            } else {               *typeKey = *(xx + 1);            }            jvmtiDeallocate(signature);        }    }    return error;}/** * Return class loader for a class (must be inside a WITH_LOCAL_REFS) */jvmtiErrorclassLoader(jclass clazz, jobject *pclazz){    jvmtiError error;    *pclazz = NULL;    error = JVMTI_FUNC_PTR(gdata->jvmti,GetClassLoader)            (gdata->jvmti, clazz, pclazz);    return error;}/** * Get field signature */jvmtiError fieldSignature(jclass clazz, jfieldID field,         char **pname, char **psignature, char **pgeneric_signature){    jvmtiError error;    char *name = NULL;    char *signature = NULL;    char *generic_signature = NULL;      error = JVMTI_FUNC_PTR(gdata->jvmti,GetFieldName)            (gdata->jvmti, clazz, field, &name, &signature, &generic_signature);    if ( pname != NULL ) {        *pname = name;    } else if ( name != NULL )  {        jvmtiDeallocate(name);    }     if ( psignature != NULL ) {        *psignature = signature;    } else if ( signature != NULL )  {        jvmtiDeallocate(signature);    }     if ( pgeneric_signature != NULL ) {        *pgeneric_signature = generic_signature;    } else if ( generic_signature != NULL )  {        jvmtiDeallocate(generic_signature);    }     return error;}JNIEnv *getEnv(void){    JNIEnv *env = NULL;    jint rc;    rc = FUNC_PTR(gdata->jvm,GetEnv)                (gdata->jvm, (void **)&env, JNI_VERSION_1_2);    if (rc != JNI_OK) {        ERROR_MESSAGE(("JDWP Unable to get JNI 1.2 environment, jvm->GetEnv() return code = %d",                 rc));        EXIT_ERROR(AGENT_ERROR_NO_JNI_ENV,NULL);    }    return env;}jvmtiError spawnNewThread(jvmtiStartFunction func, void *arg, char *name){    JNIEnv *env = getEnv();    jvmtiError error;    LOG_MISC(("Spawning new thread: %s", name));    WITH_LOCAL_REFS(env, 3) {        jthread thread;        jstring nameString;                nameString = JNI_FUNC_PTR(env,NewStringUTF)(env, name);        if (JNI_FUNC_PTR(env,ExceptionOccurred)(env)) {            JNI_FUNC_PTR(env,ExceptionClear)(env);            error = AGENT_ERROR_OUT_OF_MEMORY;            goto err;        }        thread = JNI_FUNC_PTR(env,NewObject)                        (env, gdata->threadClass, gdata->threadConstructor,                                    gdata->systemThreadGroup, nameString);        if (JNI_FUNC_PTR(env,ExceptionOccurred)(env)) {            JNI_FUNC_PTR(env,ExceptionClear)(env);            error = AGENT_ERROR_OUT_OF_MEMORY;            goto err;        }        /*

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?