DdmConnection.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. #include <dlfcn.h>
  17. #include <cutils/log.h>
  18. #include "jni.h"
  19. #include "DdmConnection.h"
  20. namespace android {
  21. void DdmConnection_start(const char* name) {
  22. ALOGI("DdmConnection_start");
  23. DdmConnection::start(name);
  24. }
  25. void DdmConnection::start(const char* name) {
  26. JavaVM* vm;
  27. JNIEnv* env;
  28. // start a VM
  29. JavaVMInitArgs args;
  30. JavaVMOption opt;
  31. opt.optionString =
  32. "-agentlib:jdwp=transport=dt_android_adb,suspend=n,server=y";
  33. args.version = JNI_VERSION_1_4;
  34. args.options = &opt;
  35. args.nOptions = 1;
  36. args.ignoreUnrecognized = JNI_FALSE;
  37. // TODO: Should this just link against libnativehelper and use its
  38. // JNI_CreateJavaVM wrapper that essential does this dlopen/dlsym
  39. // work based on the current system default runtime?
  40. void* libart_dso = dlopen("libart.so", RTLD_NOW);
  41. ALOGE_IF(!libart_dso, "DdmConnection: %s", dlerror());
  42. void* libandroid_runtime_dso = dlopen("libandroid_runtime.so", RTLD_NOW);
  43. ALOGE_IF(!libandroid_runtime_dso, "DdmConnection: %s", dlerror());
  44. if (!libart_dso || !libandroid_runtime_dso) {
  45. goto error;
  46. }
  47. jint (*JNI_CreateJavaVM)(JavaVM** p_vm, JNIEnv** p_env, void* vm_args);
  48. JNI_CreateJavaVM = reinterpret_cast<decltype(JNI_CreateJavaVM)>(
  49. dlsym(libart_dso, "JNI_CreateJavaVM"));
  50. ALOGE_IF(!JNI_CreateJavaVM, "DdmConnection: %s", dlerror());
  51. jint (*registerNatives)(JNIEnv* env, jclass clazz);
  52. registerNatives = reinterpret_cast<decltype(registerNatives)>(
  53. dlsym(libandroid_runtime_dso,
  54. "Java_com_android_internal_util_WithFramework_registerNatives"));
  55. ALOGE_IF(!registerNatives, "DdmConnection: %s", dlerror());
  56. if (!JNI_CreateJavaVM || !registerNatives) {
  57. goto error;
  58. }
  59. if (JNI_CreateJavaVM(&vm, &env, &args) == 0) {
  60. jclass startClass;
  61. jmethodID startMeth;
  62. // register native code
  63. if (registerNatives(env, 0) == 0) {
  64. // set our name by calling DdmHandleAppName.setAppName()
  65. startClass = env->FindClass("android/ddm/DdmHandleAppName");
  66. if (startClass) {
  67. startMeth = env->GetStaticMethodID(startClass,
  68. "setAppName", "(Ljava/lang/String;I)V");
  69. if (startMeth) {
  70. jstring str = env->NewStringUTF(name);
  71. env->CallStaticVoidMethod(startClass, startMeth, str, getuid());
  72. env->DeleteLocalRef(str);
  73. }
  74. }
  75. // initialize DDMS communication by calling
  76. // DdmRegister.registerHandlers()
  77. startClass = env->FindClass("android/ddm/DdmRegister");
  78. if (startClass) {
  79. startMeth = env->GetStaticMethodID(startClass,
  80. "registerHandlers", "()V");
  81. if (startMeth) {
  82. env->CallStaticVoidMethod(startClass, startMeth);
  83. }
  84. }
  85. }
  86. }
  87. return;
  88. error:
  89. if (libandroid_runtime_dso) {
  90. dlclose(libandroid_runtime_dso);
  91. }
  92. if (libart_dso) {
  93. dlclose(libart_dso);
  94. }
  95. }
  96. }; // namespace android