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