DdmConnection.cpp revision c1d359d42b753fcc2426d66a0f782f7c300893bc
18afb7e39a83a3e31170612d562eb08508e328388Mathias Agopian/* 28afb7e39a83a3e31170612d562eb08508e328388Mathias Agopian * Copyright (C) 2011 The Android Open Source Project 38afb7e39a83a3e31170612d562eb08508e328388Mathias Agopian * 48afb7e39a83a3e31170612d562eb08508e328388Mathias Agopian * Licensed under the Apache License, Version 2.0 (the "License"); 58afb7e39a83a3e31170612d562eb08508e328388Mathias Agopian * you may not use this file except in compliance with the License. 68afb7e39a83a3e31170612d562eb08508e328388Mathias Agopian * You may obtain a copy of the License at 78afb7e39a83a3e31170612d562eb08508e328388Mathias Agopian * 88afb7e39a83a3e31170612d562eb08508e328388Mathias Agopian * http://www.apache.org/licenses/LICENSE-2.0 98afb7e39a83a3e31170612d562eb08508e328388Mathias Agopian * 108afb7e39a83a3e31170612d562eb08508e328388Mathias Agopian * Unless required by applicable law or agreed to in writing, software 118afb7e39a83a3e31170612d562eb08508e328388Mathias Agopian * distributed under the License is distributed on an "AS IS" BASIS, 128afb7e39a83a3e31170612d562eb08508e328388Mathias Agopian * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 138afb7e39a83a3e31170612d562eb08508e328388Mathias Agopian * See the License for the specific language governing permissions and 148afb7e39a83a3e31170612d562eb08508e328388Mathias Agopian * limitations under the License. 158afb7e39a83a3e31170612d562eb08508e328388Mathias Agopian */ 168afb7e39a83a3e31170612d562eb08508e328388Mathias Agopian 17c1d359d42b753fcc2426d66a0f782f7c300893bcMathias Agopian#include <dlfcn.h> 18c1d359d42b753fcc2426d66a0f782f7c300893bcMathias Agopian 198afb7e39a83a3e31170612d562eb08508e328388Mathias Agopian#include <android_runtime/AndroidRuntime.h> 208afb7e39a83a3e31170612d562eb08508e328388Mathias Agopian 218afb7e39a83a3e31170612d562eb08508e328388Mathias Agopian#include "jni.h" 228afb7e39a83a3e31170612d562eb08508e328388Mathias Agopian#include "DdmConnection.h" 238afb7e39a83a3e31170612d562eb08508e328388Mathias Agopian 248afb7e39a83a3e31170612d562eb08508e328388Mathias Agopiannamespace android { 258afb7e39a83a3e31170612d562eb08508e328388Mathias Agopian 26c1d359d42b753fcc2426d66a0f782f7c300893bcMathias Agopian 278afb7e39a83a3e31170612d562eb08508e328388Mathias Agopianvoid DdmConnection::start(const char* name) { 288afb7e39a83a3e31170612d562eb08508e328388Mathias Agopian JavaVM* vm; 298afb7e39a83a3e31170612d562eb08508e328388Mathias Agopian JNIEnv* env; 308afb7e39a83a3e31170612d562eb08508e328388Mathias Agopian 318afb7e39a83a3e31170612d562eb08508e328388Mathias Agopian // start a VM 328afb7e39a83a3e31170612d562eb08508e328388Mathias Agopian JavaVMInitArgs args; 338afb7e39a83a3e31170612d562eb08508e328388Mathias Agopian JavaVMOption opt; 348afb7e39a83a3e31170612d562eb08508e328388Mathias Agopian 358afb7e39a83a3e31170612d562eb08508e328388Mathias Agopian opt.optionString = 368afb7e39a83a3e31170612d562eb08508e328388Mathias Agopian "-agentlib:jdwp=transport=dt_android_adb,suspend=n,server=y"; 378afb7e39a83a3e31170612d562eb08508e328388Mathias Agopian 388afb7e39a83a3e31170612d562eb08508e328388Mathias Agopian args.version = JNI_VERSION_1_4; 398afb7e39a83a3e31170612d562eb08508e328388Mathias Agopian args.options = &opt; 408afb7e39a83a3e31170612d562eb08508e328388Mathias Agopian args.nOptions = 1; 418afb7e39a83a3e31170612d562eb08508e328388Mathias Agopian args.ignoreUnrecognized = JNI_FALSE; 428afb7e39a83a3e31170612d562eb08508e328388Mathias Agopian 43c1d359d42b753fcc2426d66a0f782f7c300893bcMathias Agopian 44c1d359d42b753fcc2426d66a0f782f7c300893bcMathias Agopian void* libdvm_dso = dlopen("libdvm.so", RTLD_NOW); 45c1d359d42b753fcc2426d66a0f782f7c300893bcMathias Agopian ALOGE_IF(!libdvm_dso, "DdmConnection: %s", dlerror()); 46c1d359d42b753fcc2426d66a0f782f7c300893bcMathias Agopian 47c1d359d42b753fcc2426d66a0f782f7c300893bcMathias Agopian void* libandroid_runtime_dso = dlopen("libandroid_runtime.so", RTLD_NOW); 48c1d359d42b753fcc2426d66a0f782f7c300893bcMathias Agopian ALOGE_IF(!libandroid_runtime_dso, "DdmConnection: %s", dlerror()); 49c1d359d42b753fcc2426d66a0f782f7c300893bcMathias Agopian 50c1d359d42b753fcc2426d66a0f782f7c300893bcMathias Agopian if (!libdvm_dso || !libandroid_runtime_dso) { 51c1d359d42b753fcc2426d66a0f782f7c300893bcMathias Agopian goto error; 52c1d359d42b753fcc2426d66a0f782f7c300893bcMathias Agopian } 53c1d359d42b753fcc2426d66a0f782f7c300893bcMathias Agopian 54c1d359d42b753fcc2426d66a0f782f7c300893bcMathias Agopian jint (*JNI_CreateJavaVM)(JavaVM** p_vm, JNIEnv** p_env, void* vm_args); 55c1d359d42b753fcc2426d66a0f782f7c300893bcMathias Agopian JNI_CreateJavaVM = (typeof JNI_CreateJavaVM)dlsym(libdvm_dso, "JNI_CreateJavaVM"); 56c1d359d42b753fcc2426d66a0f782f7c300893bcMathias Agopian ALOGE_IF(!JNI_CreateJavaVM, "DdmConnection: %s", dlerror()); 57c1d359d42b753fcc2426d66a0f782f7c300893bcMathias Agopian 58c1d359d42b753fcc2426d66a0f782f7c300893bcMathias Agopian jint (*registerNatives)(JNIEnv* env, jclass clazz); 59c1d359d42b753fcc2426d66a0f782f7c300893bcMathias Agopian registerNatives = (typeof registerNatives)dlsym(libandroid_runtime_dso, 60c1d359d42b753fcc2426d66a0f782f7c300893bcMathias Agopian "Java_com_android_internal_util_WithFramework_registerNatives"); 61c1d359d42b753fcc2426d66a0f782f7c300893bcMathias Agopian ALOGE_IF(!registerNatives, "DdmConnection: %s", dlerror()); 62c1d359d42b753fcc2426d66a0f782f7c300893bcMathias Agopian 63c1d359d42b753fcc2426d66a0f782f7c300893bcMathias Agopian if (!JNI_CreateJavaVM || !registerNatives) { 64c1d359d42b753fcc2426d66a0f782f7c300893bcMathias Agopian goto error; 65c1d359d42b753fcc2426d66a0f782f7c300893bcMathias Agopian } 66c1d359d42b753fcc2426d66a0f782f7c300893bcMathias Agopian 678afb7e39a83a3e31170612d562eb08508e328388Mathias Agopian if (JNI_CreateJavaVM(&vm, &env, &args) == 0) { 688afb7e39a83a3e31170612d562eb08508e328388Mathias Agopian jclass startClass; 698afb7e39a83a3e31170612d562eb08508e328388Mathias Agopian jmethodID startMeth; 708afb7e39a83a3e31170612d562eb08508e328388Mathias Agopian 718afb7e39a83a3e31170612d562eb08508e328388Mathias Agopian // register native code 72c1d359d42b753fcc2426d66a0f782f7c300893bcMathias Agopian if (registerNatives(env, 0) == 0) { 738afb7e39a83a3e31170612d562eb08508e328388Mathias Agopian // set our name by calling DdmHandleAppName.setAppName() 748afb7e39a83a3e31170612d562eb08508e328388Mathias Agopian startClass = env->FindClass("android/ddm/DdmHandleAppName"); 758afb7e39a83a3e31170612d562eb08508e328388Mathias Agopian if (startClass) { 768afb7e39a83a3e31170612d562eb08508e328388Mathias Agopian startMeth = env->GetStaticMethodID(startClass, 778afb7e39a83a3e31170612d562eb08508e328388Mathias Agopian "setAppName", "(Ljava/lang/String;)V"); 788afb7e39a83a3e31170612d562eb08508e328388Mathias Agopian if (startMeth) { 798afb7e39a83a3e31170612d562eb08508e328388Mathias Agopian jstring str = env->NewStringUTF(name); 808afb7e39a83a3e31170612d562eb08508e328388Mathias Agopian env->CallStaticVoidMethod(startClass, startMeth, str); 818afb7e39a83a3e31170612d562eb08508e328388Mathias Agopian env->DeleteLocalRef(str); 828afb7e39a83a3e31170612d562eb08508e328388Mathias Agopian } 838afb7e39a83a3e31170612d562eb08508e328388Mathias Agopian } 848afb7e39a83a3e31170612d562eb08508e328388Mathias Agopian 858afb7e39a83a3e31170612d562eb08508e328388Mathias Agopian // initialize DDMS communication by calling 868afb7e39a83a3e31170612d562eb08508e328388Mathias Agopian // DdmRegister.registerHandlers() 878afb7e39a83a3e31170612d562eb08508e328388Mathias Agopian startClass = env->FindClass("android/ddm/DdmRegister"); 888afb7e39a83a3e31170612d562eb08508e328388Mathias Agopian if (startClass) { 898afb7e39a83a3e31170612d562eb08508e328388Mathias Agopian startMeth = env->GetStaticMethodID(startClass, 908afb7e39a83a3e31170612d562eb08508e328388Mathias Agopian "registerHandlers", "()V"); 918afb7e39a83a3e31170612d562eb08508e328388Mathias Agopian if (startMeth) { 928afb7e39a83a3e31170612d562eb08508e328388Mathias Agopian env->CallStaticVoidMethod(startClass, startMeth); 938afb7e39a83a3e31170612d562eb08508e328388Mathias Agopian } 948afb7e39a83a3e31170612d562eb08508e328388Mathias Agopian } 958afb7e39a83a3e31170612d562eb08508e328388Mathias Agopian } 968afb7e39a83a3e31170612d562eb08508e328388Mathias Agopian } 97c1d359d42b753fcc2426d66a0f782f7c300893bcMathias Agopian return; 98c1d359d42b753fcc2426d66a0f782f7c300893bcMathias Agopian 99c1d359d42b753fcc2426d66a0f782f7c300893bcMathias Agopianerror: 100c1d359d42b753fcc2426d66a0f782f7c300893bcMathias Agopian if (libandroid_runtime_dso) { 101c1d359d42b753fcc2426d66a0f782f7c300893bcMathias Agopian dlclose(libandroid_runtime_dso); 102c1d359d42b753fcc2426d66a0f782f7c300893bcMathias Agopian } 103c1d359d42b753fcc2426d66a0f782f7c300893bcMathias Agopian if (libdvm_dso) { 104c1d359d42b753fcc2426d66a0f782f7c300893bcMathias Agopian dlclose(libdvm_dso); 105c1d359d42b753fcc2426d66a0f782f7c300893bcMathias Agopian } 1068afb7e39a83a3e31170612d562eb08508e328388Mathias Agopian} 1078afb7e39a83a3e31170612d562eb08508e328388Mathias Agopian 1088afb7e39a83a3e31170612d562eb08508e328388Mathias Agopian}; // namespace android 109