2 * Copyright (C) 2011 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include <sys/types.h>
24 #include "DdmConnection.h"
28 void DdmConnection_start(const char* name) {
29 ALOGI("DdmConnection_start");
30 DdmConnection::start(name);
33 void DdmConnection::start(const char* name) {
42 "-agentlib:jdwp=transport=dt_android_adb,suspend=n,server=y";
44 args.version = JNI_VERSION_1_4;
47 args.ignoreUnrecognized = JNI_FALSE;
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());
56 void* libandroid_runtime_dso = dlopen("libandroid_runtime.so", RTLD_NOW);
57 ALOGE_IF(!libandroid_runtime_dso, "DdmConnection: %s", dlerror());
59 if (!libart_dso || !libandroid_runtime_dso) {
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());
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());
74 if (!JNI_CreateJavaVM || !registerNatives) {
78 if (JNI_CreateJavaVM(&vm, &env, &args) == 0) {
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");
87 startMeth = env->GetStaticMethodID(startClass,
88 "setAppName", "(Ljava/lang/String;I)V");
90 jstring str = env->NewStringUTF(name);
91 env->CallStaticVoidMethod(startClass, startMeth, str, getuid());
92 env->DeleteLocalRef(str);
96 // initialize DDMS communication by calling
97 // DdmRegister.registerHandlers()
98 startClass = env->FindClass("android/ddm/DdmRegister");
100 startMeth = env->GetStaticMethodID(startClass,
101 "registerHandlers", "()V");
103 env->CallStaticVoidMethod(startClass, startMeth);
111 if (libandroid_runtime_dso) {
112 dlclose(libandroid_runtime_dso);
119 }; // namespace android