blob: 433b38e54276d151d1523226c4bff38e10718ea6 [file] [log] [blame]
Mathias Agopian8afb7e32011-08-15 20:44:40 -07001/*
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
Mathias Agopianc1d359d2012-08-04 20:09:03 -070017#include <dlfcn.h>
18
Greg Hackmann92516c82012-08-06 13:55:56 -070019#include <cutils/log.h>
Mathias Agopian8afb7e32011-08-15 20:44:40 -070020
21#include "jni.h"
22#include "DdmConnection.h"
23
Mathias Agopian8afb7e32011-08-15 20:44:40 -070024namespace android {
25
Mathias Agopianc1d359d2012-08-04 20:09:03 -070026
Mathias Agopian8afb7e32011-08-15 20:44:40 -070027void 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
Mathias Agopianc1d359d2012-08-04 20:09:03 -070043
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
Mathias Agopian8afb7e32011-08-15 20:44:40 -070067 if (JNI_CreateJavaVM(&vm, &env, &args) == 0) {
68 jclass startClass;
69 jmethodID startMeth;
70
71 // register native code
Mathias Agopianc1d359d2012-08-04 20:09:03 -070072 if (registerNatives(env, 0) == 0) {
Mathias Agopian8afb7e32011-08-15 20:44:40 -070073 // 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 }
Mathias Agopianc1d359d2012-08-04 20:09:03 -070097 return;
98
99error:
100 if (libandroid_runtime_dso) {
101 dlclose(libandroid_runtime_dso);
102 }
103 if (libdvm_dso) {
104 dlclose(libdvm_dso);
105 }
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700106}
107
108}; // namespace android