blob: 659c2c8e49e505f2e8c8d7335532846fcbfb6cef [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
Keun young Park63f165f2012-08-31 10:53:36 -070026void DdmConnection_start(const char* name) {
27 ALOGI("DdmConnection_start");
28 DdmConnection::start(name);
29}
Mathias Agopianc1d359d2012-08-04 20:09:03 -070030
Mathias Agopian8afb7e32011-08-15 20:44:40 -070031void 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
Mathias Agopianc1d359d2012-08-04 20:09:03 -070047
Brian Carlstromaefe55f2014-05-28 21:27:47 -070048 // 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());
Mathias Agopianc1d359d2012-08-04 20:09:03 -070053
54 void* libandroid_runtime_dso = dlopen("libandroid_runtime.so", RTLD_NOW);
55 ALOGE_IF(!libandroid_runtime_dso, "DdmConnection: %s", dlerror());
56
Brian Carlstromaefe55f2014-05-28 21:27:47 -070057 if (!libart_dso || !libandroid_runtime_dso) {
Mathias Agopianc1d359d2012-08-04 20:09:03 -070058 goto error;
59 }
60
61 jint (*JNI_CreateJavaVM)(JavaVM** p_vm, JNIEnv** p_env, void* vm_args);
Dan Stozaf10c46e2014-11-11 10:32:31 -080062 JNI_CreateJavaVM = reinterpret_cast<decltype(JNI_CreateJavaVM)>(
63 dlsym(libart_dso, "JNI_CreateJavaVM"));
Mathias Agopianc1d359d2012-08-04 20:09:03 -070064 ALOGE_IF(!JNI_CreateJavaVM, "DdmConnection: %s", dlerror());
65
66 jint (*registerNatives)(JNIEnv* env, jclass clazz);
Dan Stozaf10c46e2014-11-11 10:32:31 -080067 registerNatives = reinterpret_cast<decltype(registerNatives)>(
68 dlsym(libandroid_runtime_dso,
69 "Java_com_android_internal_util_WithFramework_registerNatives"));
Mathias Agopianc1d359d2012-08-04 20:09:03 -070070 ALOGE_IF(!registerNatives, "DdmConnection: %s", dlerror());
71
72 if (!JNI_CreateJavaVM || !registerNatives) {
73 goto error;
74 }
75
Mathias Agopian8afb7e32011-08-15 20:44:40 -070076 if (JNI_CreateJavaVM(&vm, &env, &args) == 0) {
77 jclass startClass;
78 jmethodID startMeth;
79
80 // register native code
Mathias Agopianc1d359d2012-08-04 20:09:03 -070081 if (registerNatives(env, 0) == 0) {
Mathias Agopian8afb7e32011-08-15 20:44:40 -070082 // set our name by calling DdmHandleAppName.setAppName()
83 startClass = env->FindClass("android/ddm/DdmHandleAppName");
84 if (startClass) {
85 startMeth = env->GetStaticMethodID(startClass,
Mathias Agopian1b3aeb42012-10-07 16:41:12 -070086 "setAppName", "(Ljava/lang/String;I)V");
Mathias Agopian8afb7e32011-08-15 20:44:40 -070087 if (startMeth) {
88 jstring str = env->NewStringUTF(name);
Mathias Agopian1b3aeb42012-10-07 16:41:12 -070089 env->CallStaticVoidMethod(startClass, startMeth, str, getuid());
Mathias Agopian8afb7e32011-08-15 20:44:40 -070090 env->DeleteLocalRef(str);
91 }
92 }
93
94 // initialize DDMS communication by calling
95 // DdmRegister.registerHandlers()
96 startClass = env->FindClass("android/ddm/DdmRegister");
97 if (startClass) {
98 startMeth = env->GetStaticMethodID(startClass,
99 "registerHandlers", "()V");
100 if (startMeth) {
101 env->CallStaticVoidMethod(startClass, startMeth);
102 }
103 }
104 }
105 }
Mathias Agopianc1d359d2012-08-04 20:09:03 -0700106 return;
107
108error:
109 if (libandroid_runtime_dso) {
110 dlclose(libandroid_runtime_dso);
111 }
Brian Carlstromaefe55f2014-05-28 21:27:47 -0700112 if (libart_dso) {
113 dlclose(libart_dso);
Mathias Agopianc1d359d2012-08-04 20:09:03 -0700114 }
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700115}
116
117}; // namespace android