Home | History | Annotate | Download | only in 1901-get-bytecodes
      1 /*
      2  * Copyright (C) 2013 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 #include <iostream>
     18 #include <pthread.h>
     19 #include <stdio.h>
     20 #include <vector>
     21 
     22 #include "android-base/logging.h"
     23 #include "jni.h"
     24 #include "scoped_local_ref.h"
     25 #include "scoped_primitive_array.h"
     26 
     27 #include "jvmti.h"
     28 
     29 // Test infrastructure
     30 #include "jvmti_helper.h"
     31 #include "test_env.h"
     32 
     33 namespace art {
     34 namespace Test1901Bytecodes {
     35 
     36 extern "C" JNIEXPORT jbyteArray JNICALL Java_art_Test1901_getBytecodes(JNIEnv* env,
     37                                                                        jclass,
     38                                                                        jobject jmethod) {
     39   jmethodID method = env->FromReflectedMethod(jmethod);
     40   if (env->ExceptionCheck()) {
     41     return nullptr;
     42   }
     43   unsigned char* bytecodes = nullptr;
     44   jint bytecodes_size = 0;
     45   if (JvmtiErrorToException(env,
     46                             jvmti_env,
     47                             jvmti_env->GetBytecodes(method, &bytecodes_size, &bytecodes))) {
     48     return nullptr;
     49   }
     50   jbyteArray out = env->NewByteArray(bytecodes_size);
     51   if (env->ExceptionCheck()) {
     52     return nullptr;
     53   } else if (bytecodes_size == 0) {
     54     return out;
     55   }
     56   jbyte* bytes = env->GetByteArrayElements(out, /* is_copy */ nullptr);
     57   memcpy(bytes, bytecodes, bytecodes_size);
     58   env->ReleaseByteArrayElements(out, bytes, 0);
     59   return out;
     60 }
     61 
     62 }  // namespace Test1901Bytecodes
     63 }  // namespace art
     64