PageRenderTime 31ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/openjdkjvmti/OpenjdkJvmTi.cc

https://gitlab.com/androidopensourceproject/platform-art
C++ | 1234 lines | 1036 code | 156 blank | 42 comment | 55 complexity | 8cb6e4c73d233b2f2921a717d1a1d539 MD5 | raw file
  1. /* Copyright (C) 2016 The Android Open Source Project
  2. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  3. *
  4. * This file implements interfaces from the file jvmti.h. This implementation
  5. * is licensed under the same terms as the file jvmti.h. The
  6. * copyright and license information for the file jvmti.h follows.
  7. *
  8. * Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved.
  9. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  10. *
  11. * This code is free software; you can redistribute it and/or modify it
  12. * under the terms of the GNU General Public License version 2 only, as
  13. * published by the Free Software Foundation. Oracle designates this
  14. * particular file as subject to the "Classpath" exception as provided
  15. * by Oracle in the LICENSE file that accompanied this code.
  16. *
  17. * This code is distributed in the hope that it will be useful, but WITHOUT
  18. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  19. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  20. * version 2 for more details (a copy is included in the LICENSE file that
  21. * accompanied this code).
  22. *
  23. * You should have received a copy of the GNU General Public License version
  24. * 2 along with this work; if not, write to the Free Software Foundation,
  25. * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  26. *
  27. * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  28. * or visit www.oracle.com if you need additional information or have any
  29. * questions.
  30. */
  31. #include <memory>
  32. #include <string>
  33. #include <type_traits>
  34. #include <vector>
  35. #include <android-base/logging.h>
  36. #include <jni.h>
  37. #include "jvmti.h"
  38. #include "alloc_manager.h"
  39. #include "art_jvmti.h"
  40. #include "base/logging.h" // For gLogVerbosity.
  41. #include "base/mutex.h"
  42. #include "events-inl.h"
  43. #include "jni/jni_env_ext-inl.h"
  44. #include "obj_ptr-inl.h"
  45. #include "object_tagging.h"
  46. #include "runtime.h"
  47. #include "scoped_thread_state_change-inl.h"
  48. #include "thread-current-inl.h"
  49. #include "thread_list.h"
  50. #include "ti_allocator.h"
  51. #include "ti_breakpoint.h"
  52. #include "ti_class.h"
  53. #include "ti_dump.h"
  54. #include "ti_extension.h"
  55. #include "ti_field.h"
  56. #include "ti_heap.h"
  57. #include "ti_jni.h"
  58. #include "ti_logging.h"
  59. #include "ti_method.h"
  60. #include "ti_monitor.h"
  61. #include "ti_object.h"
  62. #include "ti_phase.h"
  63. #include "ti_properties.h"
  64. #include "ti_redefine.h"
  65. #include "ti_search.h"
  66. #include "ti_stack.h"
  67. #include "ti_thread.h"
  68. #include "ti_threadgroup.h"
  69. #include "ti_timers.h"
  70. #include "transform.h"
  71. namespace openjdkjvmti {
  72. // NB These are heap allocated to avoid the static destructors being run if an agent calls exit(3).
  73. // These should never be null.
  74. EventHandler* gEventHandler;
  75. DeoptManager* gDeoptManager;
  76. AllocationManager* gAllocManager;
  77. #define ENSURE_NON_NULL(n) \
  78. do { \
  79. if ((n) == nullptr) { \
  80. return ERR(NULL_POINTER); \
  81. } \
  82. } while (false)
  83. // Returns whether we are able to use all jvmti features.
  84. static bool IsFullJvmtiAvailable() {
  85. art::Runtime* runtime = art::Runtime::Current();
  86. return runtime->GetInstrumentation()->IsForcedInterpretOnly() || runtime->IsJavaDebuggable();
  87. }
  88. class JvmtiFunctions {
  89. private:
  90. static jvmtiError getEnvironmentError(jvmtiEnv* env) {
  91. if (env == nullptr) {
  92. return ERR(INVALID_ENVIRONMENT);
  93. } else if (art::Thread::Current() == nullptr) {
  94. return ERR(UNATTACHED_THREAD);
  95. } else {
  96. return OK;
  97. }
  98. }
  99. #define ENSURE_VALID_ENV(env) \
  100. do { \
  101. jvmtiError ensure_valid_env_ ## __LINE__ = getEnvironmentError(env); \
  102. if (ensure_valid_env_ ## __LINE__ != OK) { \
  103. return ensure_valid_env_ ## __LINE__ ; \
  104. } \
  105. } while (false)
  106. #define ENSURE_HAS_CAP(env, cap) \
  107. do { \
  108. if (ArtJvmTiEnv::AsArtJvmTiEnv(env)->capabilities.cap != 1) { \
  109. return ERR(MUST_POSSESS_CAPABILITY); \
  110. } \
  111. } while (false)
  112. public:
  113. static jvmtiError Allocate(jvmtiEnv* env, jlong size, unsigned char** mem_ptr) {
  114. jvmtiError err = getEnvironmentError(env);
  115. // Allow UNATTACHED_THREAD since we don't really care about that for this function.
  116. if (err != OK && err != ERR(UNATTACHED_THREAD)) {
  117. return err;
  118. }
  119. ENSURE_NON_NULL(mem_ptr);
  120. return AllocUtil::Allocate(env, size, mem_ptr);
  121. }
  122. static jvmtiError Deallocate(jvmtiEnv* env, unsigned char* mem) {
  123. jvmtiError err = getEnvironmentError(env);
  124. // Allow UNATTACHED_THREAD since we don't really care about that for this function.
  125. if (err != OK && err != ERR(UNATTACHED_THREAD)) {
  126. return err;
  127. }
  128. return AllocUtil::Deallocate(env, mem);
  129. }
  130. static jvmtiError GetThreadState(jvmtiEnv* env, jthread thread, jint* thread_state_ptr) {
  131. ENSURE_VALID_ENV(env);
  132. return ThreadUtil::GetThreadState(env, thread, thread_state_ptr);
  133. }
  134. static jvmtiError GetCurrentThread(jvmtiEnv* env, jthread* thread_ptr) {
  135. ENSURE_VALID_ENV(env);
  136. return ThreadUtil::GetCurrentThread(env, thread_ptr);
  137. }
  138. static jvmtiError GetAllThreads(jvmtiEnv* env, jint* threads_count_ptr, jthread** threads_ptr) {
  139. ENSURE_VALID_ENV(env);
  140. return ThreadUtil::GetAllThreads(env, threads_count_ptr, threads_ptr);
  141. }
  142. static jvmtiError SuspendThread(jvmtiEnv* env, jthread thread) {
  143. ENSURE_VALID_ENV(env);
  144. ENSURE_HAS_CAP(env, can_suspend);
  145. return ThreadUtil::SuspendThread(env, thread);
  146. }
  147. static jvmtiError SuspendThreadList(jvmtiEnv* env,
  148. jint request_count,
  149. const jthread* request_list,
  150. jvmtiError* results) {
  151. ENSURE_VALID_ENV(env);
  152. ENSURE_HAS_CAP(env, can_suspend);
  153. return ThreadUtil::SuspendThreadList(env, request_count, request_list, results);
  154. }
  155. static jvmtiError ResumeThread(jvmtiEnv* env, jthread thread) {
  156. ENSURE_VALID_ENV(env);
  157. ENSURE_HAS_CAP(env, can_suspend);
  158. return ThreadUtil::ResumeThread(env, thread);
  159. }
  160. static jvmtiError ResumeThreadList(jvmtiEnv* env,
  161. jint request_count,
  162. const jthread* request_list,
  163. jvmtiError* results) {
  164. ENSURE_VALID_ENV(env);
  165. ENSURE_HAS_CAP(env, can_suspend);
  166. return ThreadUtil::ResumeThreadList(env, request_count, request_list, results);
  167. }
  168. static jvmtiError StopThread(jvmtiEnv* env, jthread thread, jobject exception) {
  169. ENSURE_VALID_ENV(env);
  170. ENSURE_HAS_CAP(env, can_signal_thread);
  171. return ThreadUtil::StopThread(env, thread, exception);
  172. }
  173. static jvmtiError InterruptThread(jvmtiEnv* env, jthread thread) {
  174. ENSURE_VALID_ENV(env);
  175. ENSURE_HAS_CAP(env, can_signal_thread);
  176. return ThreadUtil::InterruptThread(env, thread);
  177. }
  178. static jvmtiError GetThreadInfo(jvmtiEnv* env, jthread thread, jvmtiThreadInfo* info_ptr) {
  179. ENSURE_VALID_ENV(env);
  180. return ThreadUtil::GetThreadInfo(env, thread, info_ptr);
  181. }
  182. static jvmtiError GetOwnedMonitorInfo(jvmtiEnv* env,
  183. jthread thread,
  184. jint* owned_monitor_count_ptr,
  185. jobject** owned_monitors_ptr) {
  186. ENSURE_VALID_ENV(env);
  187. ENSURE_HAS_CAP(env, can_get_owned_monitor_info);
  188. return StackUtil::GetOwnedMonitorInfo(env,
  189. thread,
  190. owned_monitor_count_ptr,
  191. owned_monitors_ptr);
  192. }
  193. static jvmtiError GetOwnedMonitorStackDepthInfo(jvmtiEnv* env,
  194. jthread thread,
  195. jint* monitor_info_count_ptr,
  196. jvmtiMonitorStackDepthInfo** monitor_info_ptr) {
  197. ENSURE_VALID_ENV(env);
  198. ENSURE_HAS_CAP(env, can_get_owned_monitor_stack_depth_info);
  199. return StackUtil::GetOwnedMonitorStackDepthInfo(env,
  200. thread,
  201. monitor_info_count_ptr,
  202. monitor_info_ptr);
  203. }
  204. static jvmtiError GetCurrentContendedMonitor(jvmtiEnv* env,
  205. jthread thread,
  206. jobject* monitor_ptr) {
  207. ENSURE_VALID_ENV(env);
  208. ENSURE_HAS_CAP(env, can_get_current_contended_monitor);
  209. return MonitorUtil::GetCurrentContendedMonitor(env, thread, monitor_ptr);
  210. }
  211. static jvmtiError RunAgentThread(jvmtiEnv* env,
  212. jthread thread,
  213. jvmtiStartFunction proc,
  214. const void* arg,
  215. jint priority) {
  216. ENSURE_VALID_ENV(env);
  217. return ThreadUtil::RunAgentThread(env, thread, proc, arg, priority);
  218. }
  219. static jvmtiError SetThreadLocalStorage(jvmtiEnv* env, jthread thread, const void* data) {
  220. ENSURE_VALID_ENV(env);
  221. return ThreadUtil::SetThreadLocalStorage(env, thread, data);
  222. }
  223. static jvmtiError GetThreadLocalStorage(jvmtiEnv* env, jthread thread, void** data_ptr) {
  224. ENSURE_VALID_ENV(env);
  225. return ThreadUtil::GetThreadLocalStorage(env, thread, data_ptr);
  226. }
  227. static jvmtiError GetTopThreadGroups(jvmtiEnv* env,
  228. jint* group_count_ptr,
  229. jthreadGroup** groups_ptr) {
  230. ENSURE_VALID_ENV(env);
  231. return ThreadGroupUtil::GetTopThreadGroups(env, group_count_ptr, groups_ptr);
  232. }
  233. static jvmtiError GetThreadGroupInfo(jvmtiEnv* env,
  234. jthreadGroup group,
  235. jvmtiThreadGroupInfo* info_ptr) {
  236. ENSURE_VALID_ENV(env);
  237. return ThreadGroupUtil::GetThreadGroupInfo(env, group, info_ptr);
  238. }
  239. static jvmtiError GetThreadGroupChildren(jvmtiEnv* env,
  240. jthreadGroup group,
  241. jint* thread_count_ptr,
  242. jthread** threads_ptr,
  243. jint* group_count_ptr,
  244. jthreadGroup** groups_ptr) {
  245. ENSURE_VALID_ENV(env);
  246. return ThreadGroupUtil::GetThreadGroupChildren(env,
  247. group,
  248. thread_count_ptr,
  249. threads_ptr,
  250. group_count_ptr,
  251. groups_ptr);
  252. }
  253. static jvmtiError GetStackTrace(jvmtiEnv* env,
  254. jthread thread,
  255. jint start_depth,
  256. jint max_frame_count,
  257. jvmtiFrameInfo* frame_buffer,
  258. jint* count_ptr) {
  259. ENSURE_VALID_ENV(env);
  260. return StackUtil::GetStackTrace(env,
  261. thread,
  262. start_depth,
  263. max_frame_count,
  264. frame_buffer,
  265. count_ptr);
  266. }
  267. static jvmtiError GetAllStackTraces(jvmtiEnv* env,
  268. jint max_frame_count,
  269. jvmtiStackInfo** stack_info_ptr,
  270. jint* thread_count_ptr) {
  271. ENSURE_VALID_ENV(env);
  272. return StackUtil::GetAllStackTraces(env, max_frame_count, stack_info_ptr, thread_count_ptr);
  273. }
  274. static jvmtiError GetThreadListStackTraces(jvmtiEnv* env,
  275. jint thread_count,
  276. const jthread* thread_list,
  277. jint max_frame_count,
  278. jvmtiStackInfo** stack_info_ptr) {
  279. ENSURE_VALID_ENV(env);
  280. return StackUtil::GetThreadListStackTraces(env,
  281. thread_count,
  282. thread_list,
  283. max_frame_count,
  284. stack_info_ptr);
  285. }
  286. static jvmtiError GetFrameCount(jvmtiEnv* env, jthread thread, jint* count_ptr) {
  287. ENSURE_VALID_ENV(env);
  288. return StackUtil::GetFrameCount(env, thread, count_ptr);
  289. }
  290. static jvmtiError PopFrame(jvmtiEnv* env, jthread thread) {
  291. ENSURE_VALID_ENV(env);
  292. ENSURE_HAS_CAP(env, can_pop_frame);
  293. return StackUtil::PopFrame(env, thread);
  294. }
  295. static jvmtiError GetFrameLocation(jvmtiEnv* env,
  296. jthread thread,
  297. jint depth,
  298. jmethodID* method_ptr,
  299. jlocation* location_ptr) {
  300. ENSURE_VALID_ENV(env);
  301. return StackUtil::GetFrameLocation(env, thread, depth, method_ptr, location_ptr);
  302. }
  303. static jvmtiError NotifyFramePop(jvmtiEnv* env, jthread thread, jint depth) {
  304. ENSURE_VALID_ENV(env);
  305. ENSURE_HAS_CAP(env, can_generate_frame_pop_events);
  306. return StackUtil::NotifyFramePop(env, thread, depth);
  307. }
  308. static jvmtiError ForceEarlyReturnObject(jvmtiEnv* env, jthread thread, jobject value) {
  309. ENSURE_VALID_ENV(env);
  310. ENSURE_HAS_CAP(env, can_force_early_return);
  311. return StackUtil::ForceEarlyReturn(env, gEventHandler, thread, value);
  312. }
  313. static jvmtiError ForceEarlyReturnInt(jvmtiEnv* env, jthread thread, jint value) {
  314. ENSURE_VALID_ENV(env);
  315. ENSURE_HAS_CAP(env, can_force_early_return);
  316. return StackUtil::ForceEarlyReturn(env, gEventHandler, thread, value);
  317. }
  318. static jvmtiError ForceEarlyReturnLong(jvmtiEnv* env, jthread thread, jlong value) {
  319. ENSURE_VALID_ENV(env);
  320. ENSURE_HAS_CAP(env, can_force_early_return);
  321. return StackUtil::ForceEarlyReturn(env, gEventHandler, thread, value);
  322. }
  323. static jvmtiError ForceEarlyReturnFloat(jvmtiEnv* env, jthread thread, jfloat value) {
  324. ENSURE_VALID_ENV(env);
  325. ENSURE_HAS_CAP(env, can_force_early_return);
  326. return StackUtil::ForceEarlyReturn(env, gEventHandler, thread, value);
  327. }
  328. static jvmtiError ForceEarlyReturnDouble(jvmtiEnv* env, jthread thread, jdouble value) {
  329. ENSURE_VALID_ENV(env);
  330. ENSURE_HAS_CAP(env, can_force_early_return);
  331. return StackUtil::ForceEarlyReturn(env, gEventHandler, thread, value);
  332. }
  333. static jvmtiError ForceEarlyReturnVoid(jvmtiEnv* env, jthread thread) {
  334. ENSURE_VALID_ENV(env);
  335. ENSURE_HAS_CAP(env, can_force_early_return);
  336. return StackUtil::ForceEarlyReturn<nullptr_t>(env, gEventHandler, thread, nullptr);
  337. }
  338. static jvmtiError FollowReferences(jvmtiEnv* env,
  339. jint heap_filter,
  340. jclass klass,
  341. jobject initial_object,
  342. const jvmtiHeapCallbacks* callbacks,
  343. const void* user_data) {
  344. ENSURE_VALID_ENV(env);
  345. ENSURE_HAS_CAP(env, can_tag_objects);
  346. HeapUtil heap_util(ArtJvmTiEnv::AsArtJvmTiEnv(env)->object_tag_table.get());
  347. return heap_util.FollowReferences(env,
  348. heap_filter,
  349. klass,
  350. initial_object,
  351. callbacks,
  352. user_data);
  353. }
  354. static jvmtiError IterateThroughHeap(jvmtiEnv* env,
  355. jint heap_filter,
  356. jclass klass,
  357. const jvmtiHeapCallbacks* callbacks,
  358. const void* user_data) {
  359. ENSURE_VALID_ENV(env);
  360. ENSURE_HAS_CAP(env, can_tag_objects);
  361. HeapUtil heap_util(ArtJvmTiEnv::AsArtJvmTiEnv(env)->object_tag_table.get());
  362. return heap_util.IterateThroughHeap(env, heap_filter, klass, callbacks, user_data);
  363. }
  364. static jvmtiError GetTag(jvmtiEnv* env, jobject object, jlong* tag_ptr) {
  365. ENSURE_VALID_ENV(env);
  366. ENSURE_HAS_CAP(env, can_tag_objects);
  367. JNIEnv* jni_env = GetJniEnv(env);
  368. if (jni_env == nullptr) {
  369. return ERR(INTERNAL);
  370. }
  371. art::ScopedObjectAccess soa(jni_env);
  372. art::ObjPtr<art::mirror::Object> obj = soa.Decode<art::mirror::Object>(object);
  373. if (!ArtJvmTiEnv::AsArtJvmTiEnv(env)->object_tag_table->GetTag(obj.Ptr(), tag_ptr)) {
  374. *tag_ptr = 0;
  375. }
  376. return ERR(NONE);
  377. }
  378. static jvmtiError SetTag(jvmtiEnv* env, jobject object, jlong tag) {
  379. ENSURE_VALID_ENV(env);
  380. ENSURE_HAS_CAP(env, can_tag_objects);
  381. if (object == nullptr) {
  382. return ERR(NULL_POINTER);
  383. }
  384. JNIEnv* jni_env = GetJniEnv(env);
  385. if (jni_env == nullptr) {
  386. return ERR(INTERNAL);
  387. }
  388. art::ScopedObjectAccess soa(jni_env);
  389. art::ObjPtr<art::mirror::Object> obj = soa.Decode<art::mirror::Object>(object);
  390. ArtJvmTiEnv::AsArtJvmTiEnv(env)->object_tag_table->Set(obj.Ptr(), tag);
  391. return ERR(NONE);
  392. }
  393. static jvmtiError GetObjectsWithTags(jvmtiEnv* env,
  394. jint tag_count,
  395. const jlong* tags,
  396. jint* count_ptr,
  397. jobject** object_result_ptr,
  398. jlong** tag_result_ptr) {
  399. ENSURE_VALID_ENV(env);
  400. ENSURE_HAS_CAP(env, can_tag_objects);
  401. JNIEnv* jni_env = GetJniEnv(env);
  402. if (jni_env == nullptr) {
  403. return ERR(INTERNAL);
  404. }
  405. art::ScopedObjectAccess soa(jni_env);
  406. return ArtJvmTiEnv::AsArtJvmTiEnv(env)->object_tag_table->GetTaggedObjects(env,
  407. tag_count,
  408. tags,
  409. count_ptr,
  410. object_result_ptr,
  411. tag_result_ptr);
  412. }
  413. static jvmtiError ForceGarbageCollection(jvmtiEnv* env) {
  414. ENSURE_VALID_ENV(env);
  415. return HeapUtil::ForceGarbageCollection(env);
  416. }
  417. static jvmtiError IterateOverObjectsReachableFromObject(
  418. jvmtiEnv* env,
  419. jobject object ATTRIBUTE_UNUSED,
  420. jvmtiObjectReferenceCallback object_reference_callback ATTRIBUTE_UNUSED,
  421. const void* user_data ATTRIBUTE_UNUSED) {
  422. ENSURE_VALID_ENV(env);
  423. ENSURE_HAS_CAP(env, can_tag_objects);
  424. return ERR(NOT_IMPLEMENTED);
  425. }
  426. static jvmtiError IterateOverReachableObjects(
  427. jvmtiEnv* env,
  428. jvmtiHeapRootCallback heap_root_callback ATTRIBUTE_UNUSED,
  429. jvmtiStackReferenceCallback stack_ref_callback ATTRIBUTE_UNUSED,
  430. jvmtiObjectReferenceCallback object_ref_callback ATTRIBUTE_UNUSED,
  431. const void* user_data ATTRIBUTE_UNUSED) {
  432. ENSURE_VALID_ENV(env);
  433. ENSURE_HAS_CAP(env, can_tag_objects);
  434. return ERR(NOT_IMPLEMENTED);
  435. }
  436. static jvmtiError IterateOverHeap(jvmtiEnv* env,
  437. jvmtiHeapObjectFilter object_filter ATTRIBUTE_UNUSED,
  438. jvmtiHeapObjectCallback heap_object_callback ATTRIBUTE_UNUSED,
  439. const void* user_data ATTRIBUTE_UNUSED) {
  440. ENSURE_VALID_ENV(env);
  441. ENSURE_HAS_CAP(env, can_tag_objects);
  442. return ERR(NOT_IMPLEMENTED);
  443. }
  444. static jvmtiError IterateOverInstancesOfClass(
  445. jvmtiEnv* env,
  446. jclass klass,
  447. jvmtiHeapObjectFilter object_filter,
  448. jvmtiHeapObjectCallback heap_object_callback,
  449. const void* user_data) {
  450. ENSURE_VALID_ENV(env);
  451. ENSURE_HAS_CAP(env, can_tag_objects);
  452. HeapUtil heap_util(ArtJvmTiEnv::AsArtJvmTiEnv(env)->object_tag_table.get());
  453. return heap_util.IterateOverInstancesOfClass(
  454. env, klass, object_filter, heap_object_callback, user_data);
  455. }
  456. static jvmtiError GetLocalObject(jvmtiEnv* env,
  457. jthread thread,
  458. jint depth,
  459. jint slot,
  460. jobject* value_ptr) {
  461. ENSURE_VALID_ENV(env);
  462. ENSURE_HAS_CAP(env, can_access_local_variables);
  463. return MethodUtil::GetLocalVariable(env, thread, depth, slot, value_ptr);
  464. }
  465. static jvmtiError GetLocalInstance(jvmtiEnv* env,
  466. jthread thread,
  467. jint depth,
  468. jobject* value_ptr) {
  469. ENSURE_VALID_ENV(env);
  470. ENSURE_HAS_CAP(env, can_access_local_variables);
  471. return MethodUtil::GetLocalInstance(env, thread, depth, value_ptr);
  472. }
  473. static jvmtiError GetLocalInt(jvmtiEnv* env,
  474. jthread thread,
  475. jint depth,
  476. jint slot,
  477. jint* value_ptr) {
  478. ENSURE_VALID_ENV(env);
  479. ENSURE_HAS_CAP(env, can_access_local_variables);
  480. return MethodUtil::GetLocalVariable(env, thread, depth, slot, value_ptr);
  481. }
  482. static jvmtiError GetLocalLong(jvmtiEnv* env,
  483. jthread thread,
  484. jint depth,
  485. jint slot,
  486. jlong* value_ptr) {
  487. ENSURE_VALID_ENV(env);
  488. ENSURE_HAS_CAP(env, can_access_local_variables);
  489. return MethodUtil::GetLocalVariable(env, thread, depth, slot, value_ptr);
  490. }
  491. static jvmtiError GetLocalFloat(jvmtiEnv* env,
  492. jthread thread,
  493. jint depth,
  494. jint slot,
  495. jfloat* value_ptr) {
  496. ENSURE_VALID_ENV(env);
  497. ENSURE_HAS_CAP(env, can_access_local_variables);
  498. return MethodUtil::GetLocalVariable(env, thread, depth, slot, value_ptr);
  499. }
  500. static jvmtiError GetLocalDouble(jvmtiEnv* env,
  501. jthread thread,
  502. jint depth,
  503. jint slot,
  504. jdouble* value_ptr) {
  505. ENSURE_VALID_ENV(env);
  506. ENSURE_HAS_CAP(env, can_access_local_variables);
  507. return MethodUtil::GetLocalVariable(env, thread, depth, slot, value_ptr);
  508. }
  509. static jvmtiError SetLocalObject(jvmtiEnv* env,
  510. jthread thread,
  511. jint depth,
  512. jint slot,
  513. jobject value) {
  514. ENSURE_VALID_ENV(env);
  515. ENSURE_HAS_CAP(env, can_access_local_variables);
  516. return MethodUtil::SetLocalVariable(env, thread, depth, slot, value);
  517. }
  518. static jvmtiError SetLocalInt(jvmtiEnv* env,
  519. jthread thread,
  520. jint depth,
  521. jint slot,
  522. jint value) {
  523. ENSURE_VALID_ENV(env);
  524. ENSURE_HAS_CAP(env, can_access_local_variables);
  525. return MethodUtil::SetLocalVariable(env, thread, depth, slot, value);
  526. }
  527. static jvmtiError SetLocalLong(jvmtiEnv* env,
  528. jthread thread,
  529. jint depth,
  530. jint slot,
  531. jlong value) {
  532. ENSURE_VALID_ENV(env);
  533. ENSURE_HAS_CAP(env, can_access_local_variables);
  534. return MethodUtil::SetLocalVariable(env, thread, depth, slot, value);
  535. }
  536. static jvmtiError SetLocalFloat(jvmtiEnv* env,
  537. jthread thread,
  538. jint depth,
  539. jint slot,
  540. jfloat value) {
  541. ENSURE_VALID_ENV(env);
  542. ENSURE_HAS_CAP(env, can_access_local_variables);
  543. return MethodUtil::SetLocalVariable(env, thread, depth, slot, value);
  544. }
  545. static jvmtiError SetLocalDouble(jvmtiEnv* env,
  546. jthread thread,
  547. jint depth,
  548. jint slot,
  549. jdouble value) {
  550. ENSURE_VALID_ENV(env);
  551. ENSURE_HAS_CAP(env, can_access_local_variables);
  552. return MethodUtil::SetLocalVariable(env, thread, depth, slot, value);
  553. }
  554. static jvmtiError SetBreakpoint(jvmtiEnv* env, jmethodID method, jlocation location) {
  555. ENSURE_VALID_ENV(env);
  556. ENSURE_HAS_CAP(env, can_generate_breakpoint_events);
  557. return BreakpointUtil::SetBreakpoint(env, method, location);
  558. }
  559. static jvmtiError ClearBreakpoint(jvmtiEnv* env, jmethodID method, jlocation location) {
  560. ENSURE_VALID_ENV(env);
  561. ENSURE_HAS_CAP(env, can_generate_breakpoint_events);
  562. return BreakpointUtil::ClearBreakpoint(env, method, location);
  563. }
  564. static jvmtiError SetFieldAccessWatch(jvmtiEnv* env, jclass klass, jfieldID field) {
  565. ENSURE_VALID_ENV(env);
  566. ENSURE_HAS_CAP(env, can_generate_field_access_events);
  567. return FieldUtil::SetFieldAccessWatch(env, klass, field);
  568. }
  569. static jvmtiError ClearFieldAccessWatch(jvmtiEnv* env, jclass klass, jfieldID field) {
  570. ENSURE_VALID_ENV(env);
  571. ENSURE_HAS_CAP(env, can_generate_field_access_events);
  572. return FieldUtil::ClearFieldAccessWatch(env, klass, field);
  573. }
  574. static jvmtiError SetFieldModificationWatch(jvmtiEnv* env, jclass klass, jfieldID field) {
  575. ENSURE_VALID_ENV(env);
  576. ENSURE_HAS_CAP(env, can_generate_field_modification_events);
  577. return FieldUtil::SetFieldModificationWatch(env, klass, field);
  578. }
  579. static jvmtiError ClearFieldModificationWatch(jvmtiEnv* env, jclass klass, jfieldID field) {
  580. ENSURE_VALID_ENV(env);
  581. ENSURE_HAS_CAP(env, can_generate_field_modification_events);
  582. return FieldUtil::ClearFieldModificationWatch(env, klass, field);
  583. }
  584. static jvmtiError GetLoadedClasses(jvmtiEnv* env, jint* class_count_ptr, jclass** classes_ptr) {
  585. ENSURE_VALID_ENV(env);
  586. HeapUtil heap_util(ArtJvmTiEnv::AsArtJvmTiEnv(env)->object_tag_table.get());
  587. return heap_util.GetLoadedClasses(env, class_count_ptr, classes_ptr);
  588. }
  589. static jvmtiError GetClassLoaderClasses(jvmtiEnv* env,
  590. jobject initiating_loader,
  591. jint* class_count_ptr,
  592. jclass** classes_ptr) {
  593. ENSURE_VALID_ENV(env);
  594. return ClassUtil::GetClassLoaderClasses(env, initiating_loader, class_count_ptr, classes_ptr);
  595. }
  596. static jvmtiError GetClassSignature(jvmtiEnv* env,
  597. jclass klass,
  598. char** signature_ptr,
  599. char** generic_ptr) {
  600. ENSURE_VALID_ENV(env);
  601. return ClassUtil::GetClassSignature(env, klass, signature_ptr, generic_ptr);
  602. }
  603. static jvmtiError GetClassStatus(jvmtiEnv* env, jclass klass, jint* status_ptr) {
  604. ENSURE_VALID_ENV(env);
  605. return ClassUtil::GetClassStatus(env, klass, status_ptr);
  606. }
  607. static jvmtiError GetSourceFileName(jvmtiEnv* env, jclass klass, char** source_name_ptr) {
  608. ENSURE_VALID_ENV(env);
  609. ENSURE_HAS_CAP(env, can_get_source_file_name);
  610. return ClassUtil::GetSourceFileName(env, klass, source_name_ptr);
  611. }
  612. static jvmtiError GetClassModifiers(jvmtiEnv* env, jclass klass, jint* modifiers_ptr) {
  613. ENSURE_VALID_ENV(env);
  614. return ClassUtil::GetClassModifiers(env, klass, modifiers_ptr);
  615. }
  616. static jvmtiError GetClassMethods(jvmtiEnv* env,
  617. jclass klass,
  618. jint* method_count_ptr,
  619. jmethodID** methods_ptr) {
  620. ENSURE_VALID_ENV(env);
  621. return ClassUtil::GetClassMethods(env, klass, method_count_ptr, methods_ptr);
  622. }
  623. static jvmtiError GetClassFields(jvmtiEnv* env,
  624. jclass klass,
  625. jint* field_count_ptr,
  626. jfieldID** fields_ptr) {
  627. ENSURE_VALID_ENV(env);
  628. return ClassUtil::GetClassFields(env, klass, field_count_ptr, fields_ptr);
  629. }
  630. static jvmtiError GetImplementedInterfaces(jvmtiEnv* env,
  631. jclass klass,
  632. jint* interface_count_ptr,
  633. jclass** interfaces_ptr) {
  634. ENSURE_VALID_ENV(env);
  635. return ClassUtil::GetImplementedInterfaces(env, klass, interface_count_ptr, interfaces_ptr);
  636. }
  637. static jvmtiError GetClassVersionNumbers(jvmtiEnv* env,
  638. jclass klass,
  639. jint* minor_version_ptr,
  640. jint* major_version_ptr) {
  641. ENSURE_VALID_ENV(env);
  642. return ClassUtil::GetClassVersionNumbers(env, klass, minor_version_ptr, major_version_ptr);
  643. }
  644. static jvmtiError GetConstantPool(jvmtiEnv* env,
  645. jclass klass ATTRIBUTE_UNUSED,
  646. jint* constant_pool_count_ptr ATTRIBUTE_UNUSED,
  647. jint* constant_pool_byte_count_ptr ATTRIBUTE_UNUSED,
  648. unsigned char** constant_pool_bytes_ptr ATTRIBUTE_UNUSED) {
  649. ENSURE_VALID_ENV(env);
  650. ENSURE_HAS_CAP(env, can_get_constant_pool);
  651. return ERR(NOT_IMPLEMENTED);
  652. }
  653. static jvmtiError IsInterface(jvmtiEnv* env, jclass klass, jboolean* is_interface_ptr) {
  654. ENSURE_VALID_ENV(env);
  655. return ClassUtil::IsInterface(env, klass, is_interface_ptr);
  656. }
  657. static jvmtiError IsArrayClass(jvmtiEnv* env,
  658. jclass klass,
  659. jboolean* is_array_class_ptr) {
  660. ENSURE_VALID_ENV(env);
  661. return ClassUtil::IsArrayClass(env, klass, is_array_class_ptr);
  662. }
  663. static jvmtiError IsModifiableClass(jvmtiEnv* env,
  664. jclass klass,
  665. jboolean* is_modifiable_class_ptr) {
  666. ENSURE_VALID_ENV(env);
  667. return Redefiner::IsModifiableClass(env, klass, is_modifiable_class_ptr);
  668. }
  669. static jvmtiError GetClassLoader(jvmtiEnv* env, jclass klass, jobject* classloader_ptr) {
  670. ENSURE_VALID_ENV(env);
  671. return ClassUtil::GetClassLoader(env, klass, classloader_ptr);
  672. }
  673. static jvmtiError GetSourceDebugExtension(jvmtiEnv* env,
  674. jclass klass,
  675. char** source_debug_extension_ptr) {
  676. ENSURE_VALID_ENV(env);
  677. ENSURE_HAS_CAP(env, can_get_source_debug_extension);
  678. return ClassUtil::GetSourceDebugExtension(env, klass, source_debug_extension_ptr);
  679. }
  680. static jvmtiError RetransformClasses(jvmtiEnv* env, jint class_count, const jclass* classes) {
  681. ENSURE_VALID_ENV(env);
  682. ENSURE_HAS_CAP(env, can_retransform_classes);
  683. return Transformer::RetransformClasses(env, class_count, classes);
  684. }
  685. static jvmtiError RedefineClasses(jvmtiEnv* env,
  686. jint class_count,
  687. const jvmtiClassDefinition* class_definitions) {
  688. ENSURE_VALID_ENV(env);
  689. ENSURE_HAS_CAP(env, can_redefine_classes);
  690. return Redefiner::RedefineClasses(env, class_count, class_definitions);
  691. }
  692. static jvmtiError GetObjectSize(jvmtiEnv* env, jobject object, jlong* size_ptr) {
  693. ENSURE_VALID_ENV(env);
  694. return ObjectUtil::GetObjectSize(env, object, size_ptr);
  695. }
  696. static jvmtiError GetObjectHashCode(jvmtiEnv* env, jobject object, jint* hash_code_ptr) {
  697. ENSURE_VALID_ENV(env);
  698. return ObjectUtil::GetObjectHashCode(env, object, hash_code_ptr);
  699. }
  700. static jvmtiError GetObjectMonitorUsage(jvmtiEnv* env,
  701. jobject object,
  702. jvmtiMonitorUsage* info_ptr) {
  703. ENSURE_VALID_ENV(env);
  704. ENSURE_HAS_CAP(env, can_get_monitor_info);
  705. return ObjectUtil::GetObjectMonitorUsage(env, object, info_ptr);
  706. }
  707. static jvmtiError GetFieldName(jvmtiEnv* env,
  708. jclass klass,
  709. jfieldID field,
  710. char** name_ptr,
  711. char** signature_ptr,
  712. char** generic_ptr) {
  713. ENSURE_VALID_ENV(env);
  714. return FieldUtil::GetFieldName(env, klass, field, name_ptr, signature_ptr, generic_ptr);
  715. }
  716. static jvmtiError GetFieldDeclaringClass(jvmtiEnv* env,
  717. jclass klass,
  718. jfieldID field,
  719. jclass* declaring_class_ptr) {
  720. ENSURE_VALID_ENV(env);
  721. return FieldUtil::GetFieldDeclaringClass(env, klass, field, declaring_class_ptr);
  722. }
  723. static jvmtiError GetFieldModifiers(jvmtiEnv* env,
  724. jclass klass,
  725. jfieldID field,
  726. jint* modifiers_ptr) {
  727. ENSURE_VALID_ENV(env);
  728. return FieldUtil::GetFieldModifiers(env, klass, field, modifiers_ptr);
  729. }
  730. static jvmtiError IsFieldSynthetic(jvmtiEnv* env,
  731. jclass klass,
  732. jfieldID field,
  733. jboolean* is_synthetic_ptr) {
  734. ENSURE_VALID_ENV(env);
  735. ENSURE_HAS_CAP(env, can_get_synthetic_attribute);
  736. return FieldUtil::IsFieldSynthetic(env, klass, field, is_synthetic_ptr);
  737. }
  738. static jvmtiError GetMethodName(jvmtiEnv* env,
  739. jmethodID method,
  740. char** name_ptr,
  741. char** signature_ptr,
  742. char** generic_ptr) {
  743. ENSURE_VALID_ENV(env);
  744. return MethodUtil::GetMethodName(env, method, name_ptr, signature_ptr, generic_ptr);
  745. }
  746. static jvmtiError GetMethodDeclaringClass(jvmtiEnv* env,
  747. jmethodID method,
  748. jclass* declaring_class_ptr) {
  749. ENSURE_VALID_ENV(env);
  750. return MethodUtil::GetMethodDeclaringClass(env, method, declaring_class_ptr);
  751. }
  752. static jvmtiError GetMethodModifiers(jvmtiEnv* env,
  753. jmethodID method,
  754. jint* modifiers_ptr) {
  755. ENSURE_VALID_ENV(env);
  756. return MethodUtil::GetMethodModifiers(env, method, modifiers_ptr);
  757. }
  758. static jvmtiError GetMaxLocals(jvmtiEnv* env,
  759. jmethodID method,
  760. jint* max_ptr) {
  761. ENSURE_VALID_ENV(env);
  762. return MethodUtil::GetMaxLocals(env, method, max_ptr);
  763. }
  764. static jvmtiError GetArgumentsSize(jvmtiEnv* env,
  765. jmethodID method,
  766. jint* size_ptr) {
  767. ENSURE_VALID_ENV(env);
  768. return MethodUtil::GetArgumentsSize(env, method, size_ptr);
  769. }
  770. static jvmtiError GetLineNumberTable(jvmtiEnv* env,
  771. jmethodID method,
  772. jint* entry_count_ptr,
  773. jvmtiLineNumberEntry** table_ptr) {
  774. ENSURE_VALID_ENV(env);
  775. ENSURE_HAS_CAP(env, can_get_line_numbers);
  776. return MethodUtil::GetLineNumberTable(env, method, entry_count_ptr, table_ptr);
  777. }
  778. static jvmtiError GetMethodLocation(jvmtiEnv* env,
  779. jmethodID method,
  780. jlocation* start_location_ptr,
  781. jlocation* end_location_ptr) {
  782. ENSURE_VALID_ENV(env);
  783. return MethodUtil::GetMethodLocation(env, method, start_location_ptr, end_location_ptr);
  784. }
  785. static jvmtiError GetLocalVariableTable(jvmtiEnv* env,
  786. jmethodID method,
  787. jint* entry_count_ptr,
  788. jvmtiLocalVariableEntry** table_ptr) {
  789. ENSURE_VALID_ENV(env);
  790. ENSURE_HAS_CAP(env, can_access_local_variables);
  791. return MethodUtil::GetLocalVariableTable(env, method, entry_count_ptr, table_ptr);
  792. }
  793. static jvmtiError GetBytecodes(jvmtiEnv* env,
  794. jmethodID method,
  795. jint* bytecode_count_ptr,
  796. unsigned char** bytecodes_ptr) {
  797. ENSURE_VALID_ENV(env);
  798. ENSURE_HAS_CAP(env, can_get_bytecodes);
  799. return MethodUtil::GetBytecodes(env, method, bytecode_count_ptr, bytecodes_ptr);
  800. }
  801. static jvmtiError IsMethodNative(jvmtiEnv* env, jmethodID method, jboolean* is_native_ptr) {
  802. ENSURE_VALID_ENV(env);
  803. return MethodUtil::IsMethodNative(env, method, is_native_ptr);
  804. }
  805. static jvmtiError IsMethodSynthetic(jvmtiEnv* env, jmethodID method, jboolean* is_synthetic_ptr) {
  806. ENSURE_VALID_ENV(env);
  807. ENSURE_HAS_CAP(env, can_get_synthetic_attribute);
  808. return MethodUtil::IsMethodSynthetic(env, method, is_synthetic_ptr);
  809. }
  810. static jvmtiError IsMethodObsolete(jvmtiEnv* env, jmethodID method, jboolean* is_obsolete_ptr) {
  811. ENSURE_VALID_ENV(env);
  812. return MethodUtil::IsMethodObsolete(env, method, is_obsolete_ptr);
  813. }
  814. static jvmtiError SetNativeMethodPrefix(jvmtiEnv* env, const char* prefix ATTRIBUTE_UNUSED) {
  815. ENSURE_VALID_ENV(env);
  816. ENSURE_HAS_CAP(env, can_set_native_method_prefix);
  817. return ERR(NOT_IMPLEMENTED);
  818. }
  819. static jvmtiError SetNativeMethodPrefixes(jvmtiEnv* env,
  820. jint prefix_count ATTRIBUTE_UNUSED,
  821. char** prefixes ATTRIBUTE_UNUSED) {
  822. ENSURE_VALID_ENV(env);
  823. ENSURE_HAS_CAP(env, can_set_native_method_prefix);
  824. return ERR(NOT_IMPLEMENTED);
  825. }
  826. static jvmtiError CreateRawMonitor(jvmtiEnv* env, const char* name, jrawMonitorID* monitor_ptr) {
  827. ENSURE_VALID_ENV(env);
  828. return MonitorUtil::CreateRawMonitor(env, name, monitor_ptr);
  829. }
  830. static jvmtiError DestroyRawMonitor(jvmtiEnv* env, jrawMonitorID monitor) {
  831. ENSURE_VALID_ENV(env);
  832. return MonitorUtil::DestroyRawMonitor(env, monitor);
  833. }
  834. static jvmtiError RawMonitorEnter(jvmtiEnv* env, jrawMonitorID monitor) {
  835. ENSURE_VALID_ENV(env);
  836. return MonitorUtil::RawMonitorEnter(env, monitor);
  837. }
  838. static jvmtiError RawMonitorExit(jvmtiEnv* env, jrawMonitorID monitor) {
  839. ENSURE_VALID_ENV(env);
  840. return MonitorUtil::RawMonitorExit(env, monitor);
  841. }
  842. static jvmtiError RawMonitorWait(jvmtiEnv* env, jrawMonitorID monitor, jlong millis) {
  843. ENSURE_VALID_ENV(env);
  844. return MonitorUtil::RawMonitorWait(env, monitor, millis);
  845. }
  846. static jvmtiError RawMonitorNotify(jvmtiEnv* env, jrawMonitorID monitor) {
  847. ENSURE_VALID_ENV(env);
  848. return MonitorUtil::RawMonitorNotify(env, monitor);
  849. }
  850. static jvmtiError RawMonitorNotifyAll(jvmtiEnv* env, jrawMonitorID monitor) {
  851. ENSURE_VALID_ENV(env);
  852. return MonitorUtil::RawMonitorNotifyAll(env, monitor);
  853. }
  854. static jvmtiError SetJNIFunctionTable(jvmtiEnv* env, const jniNativeInterface* function_table) {
  855. ENSURE_VALID_ENV(env);
  856. return JNIUtil::SetJNIFunctionTable(env, function_table);
  857. }
  858. static jvmtiError GetJNIFunctionTable(jvmtiEnv* env, jniNativeInterface** function_table) {
  859. ENSURE_VALID_ENV(env);
  860. return JNIUtil::GetJNIFunctionTable(env, function_table);
  861. }
  862. // TODO: This will require locking, so that an agent can't remove callbacks when we're dispatching
  863. // an event.
  864. static jvmtiError SetEventCallbacks(jvmtiEnv* env,
  865. const jvmtiEventCallbacks* callbacks,
  866. jint size_of_callbacks) {
  867. ENSURE_VALID_ENV(env);
  868. if (size_of_callbacks < 0) {
  869. return ERR(ILLEGAL_ARGUMENT);
  870. }
  871. if (callbacks == nullptr) {
  872. ArtJvmTiEnv::AsArtJvmTiEnv(env)->event_callbacks.reset();
  873. return ERR(NONE);
  874. }
  875. // Lock the event_info_mutex_ while we replace the callbacks.
  876. ArtJvmTiEnv* art_env = ArtJvmTiEnv::AsArtJvmTiEnv(env);
  877. art::WriterMutexLock lk(art::Thread::Current(), art_env->event_info_mutex_);
  878. std::unique_ptr<ArtJvmtiEventCallbacks> tmp(new ArtJvmtiEventCallbacks());
  879. // Copy over the extension events.
  880. tmp->CopyExtensionsFrom(art_env->event_callbacks.get());
  881. // Never overwrite the extension events.
  882. size_t copy_size = std::min(sizeof(jvmtiEventCallbacks),
  883. static_cast<size_t>(size_of_callbacks));
  884. copy_size = art::RoundDown(copy_size, sizeof(void*));
  885. // Copy non-extension events.
  886. memcpy(tmp.get(), callbacks, copy_size);
  887. // replace the event table.
  888. art_env->event_callbacks = std::move(tmp);
  889. return ERR(NONE);
  890. }
  891. static jvmtiError SetEventNotificationMode(jvmtiEnv* env,
  892. jvmtiEventMode mode,
  893. jvmtiEvent event_type,
  894. jthread event_thread,
  895. ...) {
  896. ENSURE_VALID_ENV(env);
  897. ArtJvmTiEnv* art_env = ArtJvmTiEnv::AsArtJvmTiEnv(env);
  898. return gEventHandler->SetEvent(art_env,
  899. event_thread,
  900. GetArtJvmtiEvent(art_env, event_type),
  901. mode);
  902. }
  903. static jvmtiError GenerateEvents(jvmtiEnv* env,
  904. jvmtiEvent event_type ATTRIBUTE_UNUSED) {
  905. ENSURE_VALID_ENV(env);
  906. return OK;
  907. }
  908. static jvmtiError GetExtensionFunctions(jvmtiEnv* env,
  909. jint* extension_count_ptr,
  910. jvmtiExtensionFunctionInfo** extensions) {
  911. ENSURE_VALID_ENV(env);
  912. ENSURE_NON_NULL(extension_count_ptr);
  913. ENSURE_NON_NULL(extensions);
  914. return ExtensionUtil::GetExtensionFunctions(env, extension_count_ptr, extensions);
  915. }
  916. static jvmtiError GetExtensionEvents(jvmtiEnv* env,
  917. jint* extension_count_ptr,
  918. jvmtiExtensionEventInfo** extensions) {
  919. ENSURE_VALID_ENV(env);
  920. ENSURE_NON_NULL(extension_count_ptr);
  921. ENSURE_NON_NULL(extensions);
  922. return ExtensionUtil::GetExtensionEvents(env, extension_count_ptr, extensions);
  923. }
  924. static jvmtiError SetExtensionEventCallback(jvmtiEnv* env,
  925. jint extension_event_index,
  926. jvmtiExtensionEvent callback) {
  927. ENSURE_VALID_ENV(env);
  928. return ExtensionUtil::SetExtensionEventCallback(env,
  929. extension_event_index,
  930. callback,
  931. gEventHandler);
  932. }
  933. #define FOR_ALL_CAPABILITIES(FUN) \
  934. FUN(can_tag_objects) \
  935. FUN(can_generate_field_modification_events) \
  936. FUN(can_generate_field_access_events) \
  937. FUN(can_get_bytecodes) \
  938. FUN(can_get_synthetic_attribute) \
  939. FUN(can_get_owned_monitor_info) \
  940. FUN(can_get_current_contended_monitor) \
  941. FUN(can_get_monitor_info) \
  942. FUN(can_pop_frame) \
  943. FUN(can_redefine_classes) \
  944. FUN(can_signal_thread) \
  945. FUN(can_get_source_file_name) \
  946. FUN(can_get_line_numbers) \
  947. FUN(can_get_source_debug_extension) \
  948. FUN(can_access_local_variables) \
  949. FUN(can_maintain_original_method_order) \
  950. FUN(can_generate_single_step_events) \
  951. FUN(can_generate_exception_events) \
  952. FUN(can_generate_frame_pop_events) \
  953. FUN(can_generate_breakpoint_events) \
  954. FUN(can_suspend) \
  955. FUN(can_redefine_any_class) \
  956. FUN(can_get_current_thread_cpu_time) \
  957. FUN(can_get_thread_cpu_time) \
  958. FUN(can_generate_method_entry_events) \
  959. FUN(can_generate_method_exit_events) \
  960. FUN(can_generate_all_class_hook_events) \
  961. FUN(can_generate_compiled_method_load_events) \
  962. FUN(can_generate_monitor_events) \
  963. FUN(can_generate_vm_object_alloc_events) \
  964. FUN(can_generate_native_method_bind_events) \
  965. FUN(can_generate_garbage_collection_events) \
  966. FUN(can_generate_object_free_events) \
  967. FUN(can_force_early_return) \
  968. FUN(can_get_owned_monitor_stack_depth_info) \
  969. FUN(can_get_constant_pool) \
  970. FUN(can_set_native_method_prefix) \
  971. FUN(can_retransform_classes) \
  972. FUN(can_retransform_any_class) \
  973. FUN(can_generate_resource_exhaustion_heap_events) \
  974. FUN(can_generate_resource_exhaustion_threads_events)
  975. static jvmtiError GetPotentialCapabilities(jvmtiEnv* env, jvmtiCapabilities* capabilities_ptr) {
  976. ENSURE_VALID_ENV(env);
  977. ENSURE_NON_NULL(capabilities_ptr);
  978. *capabilities_ptr = kPotentialCapabilities;
  979. if (UNLIKELY(!IsFullJvmtiAvailable())) {
  980. #define REMOVE_NONDEBUGGABLE_UNSUPPORTED(e) \
  981. do { \
  982. if (kNonDebuggableUnsupportedCapabilities.e == 1) { \
  983. capabilities_ptr->e = 0; \
  984. } \
  985. } while (false);
  986. FOR_ALL_CAPABILITIES(REMOVE_NONDEBUGGABLE_UNSUPPORTED);
  987. #undef REMOVE_NONDEBUGGABLE_UNSUPPORTED
  988. }
  989. return OK;
  990. }
  991. static jvmtiError AddCapabilities(jvmtiEnv* env, const jvmtiCapabilities* capabilities_ptr) {
  992. ENSURE_VALID_ENV(env);
  993. ENSURE_NON_NULL(capabilities_ptr);
  994. ArtJvmTiEnv* art_env = static_cast<ArtJvmTiEnv*>(env);
  995. jvmtiError ret = OK;
  996. jvmtiCapabilities changed = {};
  997. jvmtiCapabilities potential_capabilities = {};
  998. ret = env->GetPotentialCapabilities(&potential_capabilities);
  999. if (ret != OK) {
  1000. return ret;
  1001. }
  1002. #define ADD_CAPABILITY(e) \
  1003. do { \
  1004. if (capabilities_ptr->e == 1) { \
  1005. if (potential_capabilities.e == 1) { \
  1006. if (art_env->capabilities.e != 1) { \
  1007. art_env->capabilities.e = 1; \
  1008. changed.e = 1; \
  1009. }\
  1010. } else { \
  1011. ret = ERR(NOT_AVAILABLE); \
  1012. } \
  1013. } \
  1014. } while (false);
  1015. FOR_ALL_CAPABILITIES(ADD_CAPABILITY);
  1016. #undef ADD_CAPABILITY
  1017. gEventHandler->HandleChangedCapabilities(ArtJvmTiEnv::AsArtJvmTiEnv(env),
  1018. changed,
  1019. /*added=*/true);
  1020. return ret;
  1021. }
  1022. static jvmtiError RelinquishCapabilities(jvmtiEnv* env,
  1023. const jvmtiCapabilities* capabilities_ptr) {
  1024. ENSURE_VALID_ENV(env);
  1025. ENSURE_NON_NULL(capabilities_ptr);
  1026. ArtJvmTiEnv* art_env = reinterpret_cast<ArtJvmTiEnv*>(env);
  1027. jvmtiCapabilities changed = {};
  1028. #define DEL_CAPABILITY(e) \
  1029. do { \
  1030. if (capabilities_ptr->e == 1) { \
  1031. if (art_env->capabilities.e == 1) { \
  1032. art_env->capabilities.e = 0;\
  1033. changed.e = 1; \
  1034. } \
  1035. } \
  1036. } while (false);
  1037. FOR_ALL_CAPABILITIES(DEL_CAPABILITY);
  1038. #undef DEL_CAPABILITY
  1039. gEventHandler->HandleChangedCapabilities(ArtJvmTiEnv::AsArtJvmTiEnv(env),
  1040. changed,
  1041. /*added=*/false);
  1042. return OK;
  1043. }
  1044. #undef FOR_ALL_CAPABILITIES
  1045. static jvmtiError GetCapabilities(jvmtiEnv* env, jvmtiCapabilities* capabilities_ptr) {
  1046. ENSURE_VALID_ENV(env);
  1047. ENSURE_NON_NULL(capabilities_ptr);
  1048. ArtJvmTiEnv* artenv = reinterpret_cast<ArtJvmTiEnv*>(env);
  1049. *capabilities_ptr = artenv->capabilities;
  1050. return OK;
  1051. }
  1052. static jvmtiError GetCurrentThreadCpuTimerInfo(jvmtiEnv* env,
  1053. jvmtiTimerInfo* info_ptr ATTRIBUTE_UNUSED) {
  1054. ENSURE_VALID_ENV(env);
  1055. ENSURE_HAS_CAP(env, can_get_current_thread_cpu_time);
  1056. return ERR(NOT_IMPLEMENTED);
  1057. }
  1058. static jvmtiError GetCurrentThreadCpuTime(jvmtiEnv* env, jlong* nanos_ptr ATTRIBUTE_UNUSED) {
  1059. ENSURE_VALID_ENV(env);
  1060. ENSURE_HAS_CAP(env, can_get_current_thread_cpu_time);
  1061. return ERR(NOT_IMPLEMENTED);
  1062. }
  1063. static jvmtiError GetThreadCpuTimerInfo(jvmtiEnv* env,
  1064. jvmtiTimerInfo* info_ptr ATTRIBUTE_UNUSED) {
  1065. ENSURE_VALID_ENV(env);
  1066. ENSURE_HAS_CAP(env, can_get_thread_cpu_time);
  1067. return ERR(NOT_IMPLEMENTED);
  1068. }
  1069. static jvmtiError GetThreadCpuTime(jvmtiEnv* env,
  1070. jthread thread ATTRIBUTE_UNUSED,
  1071. jlong* nanos_ptr ATTRIBUTE_UNUSED) {
  1072. ENSURE_VALID_ENV(env);
  1073. ENSURE_HAS_CAP(env, can_get_thread_cpu_time);
  1074. return ERR(NOT_IMPLEMENTED);
  1075. }
  1076. static jvmtiError GetTimerInfo(jvmtiEnv* env, jvmtiTimerInfo* info_ptr) {
  1077. ENSURE_VALID_ENV(env);
  1078. return TimerUtil::GetTimerInfo(env, info_ptr);