Home | History | Annotate | Download | only in surfaceflinger
      1 /*
      2  * Copyright (C) 2011 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 <dlfcn.h>
     18 #include <sys/types.h>
     19 #include <unistd.h>
     20 
     21 #include <log/log.h>
     22 
     23 #include "jni.h"
     24 #include "DdmConnection.h"
     25 
     26 namespace android {
     27 
     28 void DdmConnection_start(const char* name) {
     29     ALOGI("DdmConnection_start");
     30     DdmConnection::start(name);
     31 }
     32 
     33 void DdmConnection::start(const char* name) {
     34     JavaVM* vm;
     35     JNIEnv* env;
     36 
     37     // start a VM
     38     JavaVMInitArgs args;
     39     JavaVMOption opt;
     40 
     41     opt.optionString =
     42         "-agentlib:jdwp=transport=dt_android_adb,suspend=n,server=y";
     43 
     44     args.version = JNI_VERSION_1_4;
     45     args.options = &opt;
     46     args.nOptions = 1;
     47     args.ignoreUnrecognized = JNI_FALSE;
     48 
     49 
     50     // TODO: Should this just link against libnativehelper and use its
     51     // JNI_CreateJavaVM wrapper that essential does this dlopen/dlsym
     52     // work based on the current system default runtime?
     53     void* libart_dso = dlopen("libart.so", RTLD_NOW);
     54     ALOGE_IF(!libart_dso, "DdmConnection: %s", dlerror());
     55 
     56     void* libandroid_runtime_dso = dlopen("libandroid_runtime.so", RTLD_NOW);
     57     ALOGE_IF(!libandroid_runtime_dso, "DdmConnection: %s", dlerror());
     58 
     59     if (!libart_dso || !libandroid_runtime_dso) {
     60         goto error;
     61     }
     62 
     63     jint (*JNI_CreateJavaVM)(JavaVM** p_vm, JNIEnv** p_env, void* vm_args);
     64     JNI_CreateJavaVM = reinterpret_cast<decltype(JNI_CreateJavaVM)>(
     65             dlsym(libart_dso, "JNI_CreateJavaVM"));
     66     ALOGE_IF(!JNI_CreateJavaVM, "DdmConnection: %s", dlerror());
     67 
     68     jint (*registerNatives)(JNIEnv* env, jclass clazz);
     69     registerNatives = reinterpret_cast<decltype(registerNatives)>(
     70             dlsym(libandroid_runtime_dso,
     71                 "Java_com_android_internal_util_WithFramework_registerNatives"));
     72     ALOGE_IF(!registerNatives, "DdmConnection: %s", dlerror());
     73 
     74     if (!JNI_CreateJavaVM || !registerNatives) {
     75         goto error;
     76     }
     77 
     78     if (JNI_CreateJavaVM(&vm, &env, &args) == 0) {
     79         jclass startClass;
     80         jmethodID startMeth;
     81 
     82         // register native code
     83         if (registerNatives(env, 0) == 0) {
     84             // set our name by calling DdmHandleAppName.setAppName()
     85             startClass = env->FindClass("android/ddm/DdmHandleAppName");
     86             if (startClass) {
     87                 startMeth = env->GetStaticMethodID(startClass,
     88                         "setAppName", "(Ljava/lang/String;I)V");
     89                 if (startMeth) {
     90                     jstring str = env->NewStringUTF(name);
     91                     env->CallStaticVoidMethod(startClass, startMeth, str, getuid());
     92                     env->DeleteLocalRef(str);
     93                 }
     94             }
     95 
     96             // initialize DDMS communication by calling
     97             // DdmRegister.registerHandlers()
     98             startClass = env->FindClass("android/ddm/DdmRegister");
     99             if (startClass) {
    100                 startMeth = env->GetStaticMethodID(startClass,
    101                         "registerHandlers", "()V");
    102                 if (startMeth) {
    103                     env->CallStaticVoidMethod(startClass, startMeth);
    104                 }
    105             }
    106         }
    107     }
    108     return;
    109 
    110 error:
    111     if (libandroid_runtime_dso) {
    112         dlclose(libandroid_runtime_dso);
    113     }
    114     if (libart_dso) {
    115         dlclose(libart_dso);
    116     }
    117 }
    118 
    119 }; // namespace android
    120