DdmConnection.cpp revision 92516c84ef2a167524007a0584fb26018b5cb9c4
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 24namespace android { 25 26 27void DdmConnection::start(const char* name) { 28 JavaVM* vm; 29 JNIEnv* env; 30 31 // start a VM 32 JavaVMInitArgs args; 33 JavaVMOption opt; 34 35 opt.optionString = 36 "-agentlib:jdwp=transport=dt_android_adb,suspend=n,server=y"; 37 38 args.version = JNI_VERSION_1_4; 39 args.options = &opt; 40 args.nOptions = 1; 41 args.ignoreUnrecognized = JNI_FALSE; 42 43 44 void* libdvm_dso = dlopen("libdvm.so", RTLD_NOW); 45 ALOGE_IF(!libdvm_dso, "DdmConnection: %s", dlerror()); 46 47 void* libandroid_runtime_dso = dlopen("libandroid_runtime.so", RTLD_NOW); 48 ALOGE_IF(!libandroid_runtime_dso, "DdmConnection: %s", dlerror()); 49 50 if (!libdvm_dso || !libandroid_runtime_dso) { 51 goto error; 52 } 53 54 jint (*JNI_CreateJavaVM)(JavaVM** p_vm, JNIEnv** p_env, void* vm_args); 55 JNI_CreateJavaVM = (typeof JNI_CreateJavaVM)dlsym(libdvm_dso, "JNI_CreateJavaVM"); 56 ALOGE_IF(!JNI_CreateJavaVM, "DdmConnection: %s", dlerror()); 57 58 jint (*registerNatives)(JNIEnv* env, jclass clazz); 59 registerNatives = (typeof registerNatives)dlsym(libandroid_runtime_dso, 60 "Java_com_android_internal_util_WithFramework_registerNatives"); 61 ALOGE_IF(!registerNatives, "DdmConnection: %s", dlerror()); 62 63 if (!JNI_CreateJavaVM || !registerNatives) { 64 goto error; 65 } 66 67 if (JNI_CreateJavaVM(&vm, &env, &args) == 0) { 68 jclass startClass; 69 jmethodID startMeth; 70 71 // register native code 72 if (registerNatives(env, 0) == 0) { 73 // set our name by calling DdmHandleAppName.setAppName() 74 startClass = env->FindClass("android/ddm/DdmHandleAppName"); 75 if (startClass) { 76 startMeth = env->GetStaticMethodID(startClass, 77 "setAppName", "(Ljava/lang/String;)V"); 78 if (startMeth) { 79 jstring str = env->NewStringUTF(name); 80 env->CallStaticVoidMethod(startClass, startMeth, str); 81 env->DeleteLocalRef(str); 82 } 83 } 84 85 // initialize DDMS communication by calling 86 // DdmRegister.registerHandlers() 87 startClass = env->FindClass("android/ddm/DdmRegister"); 88 if (startClass) { 89 startMeth = env->GetStaticMethodID(startClass, 90 "registerHandlers", "()V"); 91 if (startMeth) { 92 env->CallStaticVoidMethod(startClass, startMeth); 93 } 94 } 95 } 96 } 97 return; 98 99error: 100 if (libandroid_runtime_dso) { 101 dlclose(libandroid_runtime_dso); 102 } 103 if (libdvm_dso) { 104 dlclose(libdvm_dso); 105 } 106} 107 108}; // namespace android 109