jni_android.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. // Copyright (c) 2012 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef BASE_ANDROID_JNI_ANDROID_H_
  5. #define BASE_ANDROID_JNI_ANDROID_H_
  6. #include <jni.h>
  7. #include <sys/types.h>
  8. #include <atomic>
  9. #include <string>
  10. #include "base/android/scoped_java_ref.h"
  11. #include "base/atomicops.h"
  12. #include "base/base_export.h"
  13. #include "base/compiler_specific.h"
  14. #include "base/debug/debugging_buildflags.h"
  15. #include "base/debug/stack_trace.h"
  16. #include "base/macros.h"
  17. #if BUILDFLAG(CAN_UNWIND_WITH_FRAME_POINTERS)
  18. // When profiling is enabled (enable_profiling=true) this macro is added to
  19. // all generated JNI stubs so that it becomes the last thing that runs before
  20. // control goes into Java.
  21. //
  22. // This macro saves stack frame pointer of the current function. Saved value
  23. // used later by JNI_LINK_SAVED_FRAME_POINTER.
  24. #define JNI_SAVE_FRAME_POINTER \
  25. base::android::JNIStackFrameSaver jni_frame_saver(__builtin_frame_address(0))
  26. // When profiling is enabled (enable_profiling=true) this macro is added to
  27. // all generated JNI callbacks so that it becomes the first thing that runs
  28. // after control returns from Java.
  29. //
  30. // This macro links stack frame of the current function to the stack frame
  31. // saved by JNI_SAVE_FRAME_POINTER, allowing frame-based unwinding
  32. // (used by the heap profiler) to produce complete traces.
  33. #define JNI_LINK_SAVED_FRAME_POINTER \
  34. base::debug::ScopedStackFrameLinker jni_frame_linker( \
  35. __builtin_frame_address(0), \
  36. base::android::JNIStackFrameSaver::SavedFrame())
  37. #else
  38. // Frame-based stack unwinding is not supported, do nothing.
  39. #define JNI_SAVE_FRAME_POINTER
  40. #define JNI_LINK_SAVED_FRAME_POINTER
  41. #endif // BUILDFLAG(CAN_UNWIND_WITH_FRAME_POINTERS)
  42. namespace base {
  43. namespace android {
  44. // Used to mark symbols to be exported in a shared library's symbol table.
  45. #define JNI_EXPORT __attribute__ ((visibility("default")))
  46. // Contains the registration method information for initializing JNI bindings.
  47. struct RegistrationMethod {
  48. const char* name;
  49. bool (*func)(JNIEnv* env);
  50. };
  51. // Attaches the current thread to the VM (if necessary) and return the JNIEnv*.
  52. BASE_EXPORT JNIEnv* AttachCurrentThread();
  53. // Same to AttachCurrentThread except that thread name will be set to
  54. // |thread_name| if it is the first call. Otherwise, thread_name won't be
  55. // changed. AttachCurrentThread() doesn't regard underlying platform thread
  56. // name, but just resets it to "Thread-???". This function should be called
  57. // right after new thread is created if it is important to keep thread name.
  58. BASE_EXPORT JNIEnv* AttachCurrentThreadWithName(const std::string& thread_name);
  59. // Detaches the current thread from VM if it is attached.
  60. BASE_EXPORT void DetachFromVM();
  61. // Initializes the global JVM.
  62. BASE_EXPORT void InitVM(JavaVM* vm);
  63. // Returns true if the global JVM has been initialized.
  64. BASE_EXPORT bool IsVMInitialized();
  65. // Initializes the global ClassLoader used by the GetClass and LazyGetClass
  66. // methods. This is needed because JNI will use the base ClassLoader when there
  67. // is no Java code on the stack. The base ClassLoader doesn't know about any of
  68. // the application classes and will fail to lookup anything other than system
  69. // classes.
  70. BASE_EXPORT void InitReplacementClassLoader(
  71. JNIEnv* env,
  72. const JavaRef<jobject>& class_loader);
  73. // Finds the class named |class_name| and returns it.
  74. // Use this method instead of invoking directly the JNI FindClass method (to
  75. // prevent leaking local references).
  76. // This method triggers a fatal assertion if the class could not be found.
  77. // Use HasClass if you need to check whether the class exists.
  78. BASE_EXPORT ScopedJavaLocalRef<jclass> GetClass(JNIEnv* env,
  79. const char* class_name);
  80. // The method will initialize |atomic_class_id| to contain a global ref to the
  81. // class. And will return that ref on subsequent calls. It's the caller's
  82. // responsibility to release the ref when it is no longer needed.
  83. // The caller is responsible to zero-initialize |atomic_method_id|.
  84. // It's fine to simultaneously call this on multiple threads referencing the
  85. // same |atomic_method_id|.
  86. BASE_EXPORT jclass LazyGetClass(
  87. JNIEnv* env,
  88. const char* class_name,
  89. std::atomic<jclass>* atomic_class_id);
  90. // This class is a wrapper for JNIEnv Get(Static)MethodID.
  91. class BASE_EXPORT MethodID {
  92. public:
  93. enum Type {
  94. TYPE_STATIC,
  95. TYPE_INSTANCE,
  96. };
  97. // Returns the method ID for the method with the specified name and signature.
  98. // This method triggers a fatal assertion if the method could not be found.
  99. template<Type type>
  100. static jmethodID Get(JNIEnv* env,
  101. jclass clazz,
  102. const char* method_name,
  103. const char* jni_signature);
  104. // The caller is responsible to zero-initialize |atomic_method_id|.
  105. // It's fine to simultaneously call this on multiple threads referencing the
  106. // same |atomic_method_id|.
  107. template<Type type>
  108. static jmethodID LazyGet(JNIEnv* env,
  109. jclass clazz,
  110. const char* method_name,
  111. const char* jni_signature,
  112. std::atomic<jmethodID>* atomic_method_id);
  113. };
  114. // Returns true if an exception is pending in the provided JNIEnv*.
  115. BASE_EXPORT bool HasException(JNIEnv* env);
  116. // If an exception is pending in the provided JNIEnv*, this function clears it
  117. // and returns true.
  118. BASE_EXPORT bool ClearException(JNIEnv* env);
  119. // This function will call CHECK() macro if there's any pending exception.
  120. BASE_EXPORT void CheckException(JNIEnv* env);
  121. // This returns a string representation of the java stack trace.
  122. BASE_EXPORT std::string GetJavaExceptionInfo(JNIEnv* env,
  123. jthrowable java_throwable);
  124. #if BUILDFLAG(CAN_UNWIND_WITH_FRAME_POINTERS)
  125. // Saves caller's PC and stack frame in a thread-local variable.
  126. // Implemented only when profiling is enabled (enable_profiling=true).
  127. class BASE_EXPORT JNIStackFrameSaver {
  128. public:
  129. JNIStackFrameSaver(void* current_fp);
  130. ~JNIStackFrameSaver();
  131. static void* SavedFrame();
  132. private:
  133. void* previous_fp_;
  134. DISALLOW_COPY_AND_ASSIGN(JNIStackFrameSaver);
  135. };
  136. #endif // BUILDFLAG(CAN_UNWIND_WITH_FRAME_POINTERS)
  137. } // namespace android
  138. } // namespace base
  139. #endif // BASE_ANDROID_JNI_ANDROID_H_