2
* Copyright (c) 2003, 2023, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation.
9
* This code is distributed in the hope that it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12
* version 2 for more details (a copy is included in the LICENSE file that
13
* accompanied this code).
15
* You should have received a copy of the GNU General Public License version
16
* 2 along with this work; if not, write to the Free Software Foundation,
17
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
19
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
25
#include "precompiled.hpp"
26
#include "jvmtifiles/jvmtiEnv.hpp"
27
#include "logging/log.hpp"
28
#include "runtime/mutexLocker.hpp"
29
#include "prims/jvmtiExport.hpp"
30
#include "prims/jvmtiManageCapabilities.hpp"
32
static const jint CAPA_SIZE = (JVMTI_INTERNAL_CAPABILITY_COUNT + 7) / 8;
34
// capabilities which are always potentially available
35
jvmtiCapabilities JvmtiManageCapabilities::always_capabilities;
37
// capabilities which are potentially available during OnLoad
38
jvmtiCapabilities JvmtiManageCapabilities::onload_capabilities;
40
// capabilities which are always potentially available
41
// but to only one environment
42
jvmtiCapabilities JvmtiManageCapabilities::always_solo_capabilities;
44
// capabilities which are potentially available during OnLoad
45
// but to only one environment
46
jvmtiCapabilities JvmtiManageCapabilities::onload_solo_capabilities;
48
// remaining capabilities which are always potentially available
49
// but to only one environment
50
jvmtiCapabilities JvmtiManageCapabilities::always_solo_remaining_capabilities;
52
// remaining capabilities which are potentially available during OnLoad
53
// but to only one environment
54
jvmtiCapabilities JvmtiManageCapabilities::onload_solo_remaining_capabilities;
56
// all capabilities ever acquired
57
jvmtiCapabilities JvmtiManageCapabilities::acquired_capabilities;
59
int JvmtiManageCapabilities::_can_support_virtual_threads_count = 0;
61
Mutex* JvmtiManageCapabilities::_capabilities_lock = nullptr;
63
void JvmtiManageCapabilities::initialize() {
64
_capabilities_lock = new Mutex(Mutex::nosafepoint, "Capabilities_lock");
65
always_capabilities = init_always_capabilities();
66
onload_capabilities = init_onload_capabilities();
67
always_solo_capabilities = init_always_solo_capabilities();
68
onload_solo_capabilities = init_onload_solo_capabilities();
69
always_solo_remaining_capabilities = init_always_solo_capabilities();
70
onload_solo_remaining_capabilities = init_onload_solo_capabilities();
71
memset(&acquired_capabilities, 0, sizeof(acquired_capabilities));
75
// corresponding init functions
76
jvmtiCapabilities JvmtiManageCapabilities::init_always_capabilities() {
79
memset(&jc, 0, sizeof(jc));
80
jc.can_get_bytecodes = 1;
81
jc.can_signal_thread = 1;
82
jc.can_get_source_file_name = 1;
83
jc.can_get_line_numbers = 1;
84
jc.can_get_synthetic_attribute = 1;
85
jc.can_get_monitor_info = 1;
86
jc.can_get_constant_pool = 1;
87
jc.can_generate_all_class_hook_events = 1;
88
jc.can_generate_monitor_events = 1;
89
jc.can_generate_garbage_collection_events = 1;
90
jc.can_generate_compiled_method_load_events = 1;
91
jc.can_generate_native_method_bind_events = 1;
92
jc.can_generate_vm_object_alloc_events = 1;
93
if (os::is_thread_cpu_time_supported()) {
94
jc.can_get_current_thread_cpu_time = 1;
95
jc.can_get_thread_cpu_time = 1;
97
jc.can_redefine_classes = 1;
98
jc.can_redefine_any_class = 1;
99
jc.can_retransform_classes = 1;
100
jc.can_retransform_any_class = 1;
101
jc.can_set_native_method_prefix = 1;
102
jc.can_tag_objects = 1;
103
jc.can_generate_object_free_events = 1;
104
jc.can_generate_resource_exhaustion_heap_events = 1;
105
jc.can_generate_resource_exhaustion_threads_events = 1;
106
jc.can_support_virtual_threads = 1;
110
jvmtiCapabilities JvmtiManageCapabilities::init_onload_capabilities() {
111
jvmtiCapabilities jc;
113
memset(&jc, 0, sizeof(jc));
115
jc.can_pop_frame = 1;
116
jc.can_force_early_return = 1;
118
jc.can_get_source_debug_extension = 1;
119
jc.can_access_local_variables = 1;
120
jc.can_maintain_original_method_order = 1;
121
jc.can_generate_single_step_events = 1;
122
jc.can_generate_exception_events = 1;
123
jc.can_generate_frame_pop_events = 1;
124
jc.can_generate_method_entry_events = 1;
125
jc.can_generate_method_exit_events = 1;
126
jc.can_get_owned_monitor_info = 1;
127
jc.can_get_owned_monitor_stack_depth_info = 1;
128
jc.can_get_current_contended_monitor = 1;
129
jc.can_generate_early_vmstart = 1;
130
jc.can_generate_early_class_hook_events = 1;
135
jvmtiCapabilities JvmtiManageCapabilities::init_always_solo_capabilities() {
136
jvmtiCapabilities jc;
138
memset(&jc, 0, sizeof(jc));
140
jc.can_generate_sampled_object_alloc_events = 1;
145
jvmtiCapabilities JvmtiManageCapabilities::init_onload_solo_capabilities() {
146
jvmtiCapabilities jc;
148
memset(&jc, 0, sizeof(jc));
149
jc.can_generate_field_modification_events = 1;
150
jc.can_generate_field_access_events = 1;
151
jc.can_generate_breakpoint_events = 1;
156
jvmtiCapabilities *JvmtiManageCapabilities::either(const jvmtiCapabilities *a, const jvmtiCapabilities *b,
157
jvmtiCapabilities *result) {
158
char *ap = (char *)a;
159
char *bp = (char *)b;
160
char *resultp = (char *)result;
162
for (int i = 0; i < CAPA_SIZE; ++i) {
163
*resultp++ = *ap++ | *bp++;
170
jvmtiCapabilities *JvmtiManageCapabilities::both(const jvmtiCapabilities *a, const jvmtiCapabilities *b,
171
jvmtiCapabilities *result) {
172
char *ap = (char *)a;
173
char *bp = (char *)b;
174
char *resultp = (char *)result;
176
for (int i = 0; i < CAPA_SIZE; ++i) {
177
*resultp++ = *ap++ & *bp++;
184
jvmtiCapabilities *JvmtiManageCapabilities::exclude(const jvmtiCapabilities *a, const jvmtiCapabilities *b,
185
jvmtiCapabilities *result) {
186
char *ap = (char *)a;
187
char *bp = (char *)b;
188
char *resultp = (char *)result;
190
for (int i = 0; i < CAPA_SIZE; ++i) {
191
*resultp++ = *ap++ & (char)~*bp++;
198
bool JvmtiManageCapabilities::has_some(const jvmtiCapabilities *a) {
199
char *ap = (char *)a;
201
for (int i = 0; i < CAPA_SIZE; ++i) {
211
void JvmtiManageCapabilities::copy_capabilities(const jvmtiCapabilities *from, jvmtiCapabilities *to) {
212
char *ap = (char *)from;
213
char *resultp = (char *)to;
215
for (int i = 0; i < CAPA_SIZE; ++i) {
222
void JvmtiManageCapabilities::get_potential_capabilities_nolock(const jvmtiCapabilities *current,
223
const jvmtiCapabilities *prohibited,
224
jvmtiCapabilities *result) {
225
// exclude prohibited capabilities, must be before adding current
226
exclude(&always_capabilities, prohibited, result);
228
// must include current since it may possess solo capabilities and now prohibited
229
either(result, current, result);
231
// add other remaining
232
either(result, &always_solo_remaining_capabilities, result);
234
// if this is during OnLoad more capabilities are available
235
if (JvmtiEnv::get_phase() == JVMTI_PHASE_ONLOAD) {
236
either(result, &onload_capabilities, result);
237
either(result, &onload_solo_remaining_capabilities, result);
241
void JvmtiManageCapabilities::get_potential_capabilities(const jvmtiCapabilities* current,
242
const jvmtiCapabilities* prohibited,
243
jvmtiCapabilities* result) {
244
CapabilitiesMutexLocker ml;
245
get_potential_capabilities_nolock(current, prohibited, result);
248
jvmtiError JvmtiManageCapabilities::add_capabilities(const jvmtiCapabilities *current,
249
const jvmtiCapabilities *prohibited,
250
const jvmtiCapabilities *desired,
251
jvmtiCapabilities *result) {
252
CapabilitiesMutexLocker ml;
254
// check that the capabilities being added are potential capabilities
255
jvmtiCapabilities temp;
256
get_potential_capabilities_nolock(current, prohibited, &temp);
257
if (has_some(exclude(desired, &temp, &temp))) {
258
return JVMTI_ERROR_NOT_AVAILABLE;
261
// add to the set of ever acquired capabilities
262
either(&acquired_capabilities, desired, &acquired_capabilities);
264
// onload capabilities that got added are now permanent - so, also remove from onload
265
both(&onload_capabilities, desired, &temp);
266
either(&always_capabilities, &temp, &always_capabilities);
267
exclude(&onload_capabilities, &temp, &onload_capabilities);
269
// same for solo capabilities (transferred capabilities in the remaining sets handled as part of standard grab - below)
270
both(&onload_solo_capabilities, desired, &temp);
271
either(&always_solo_capabilities, &temp, &always_solo_capabilities);
272
exclude(&onload_solo_capabilities, &temp, &onload_solo_capabilities);
274
// remove solo capabilities that are now taken
275
exclude(&always_solo_remaining_capabilities, desired, &always_solo_remaining_capabilities);
276
exclude(&onload_solo_remaining_capabilities, desired, &onload_solo_remaining_capabilities);
278
if (desired->can_support_virtual_threads != 0 && current->can_support_virtual_threads == 0) {
279
_can_support_virtual_threads_count++;
283
either(current, desired, result);
287
return JVMTI_ERROR_NONE;
291
void JvmtiManageCapabilities::relinquish_capabilities(const jvmtiCapabilities *current,
292
const jvmtiCapabilities *unwanted,
293
jvmtiCapabilities *result) {
294
CapabilitiesMutexLocker ml;
296
jvmtiCapabilities to_trash;
297
jvmtiCapabilities temp;
299
// can't give up what you don't have
300
both(current, unwanted, &to_trash);
302
// restore solo capabilities but only those that belong
303
either(&always_solo_remaining_capabilities, both(&always_solo_capabilities, &to_trash, &temp),
304
&always_solo_remaining_capabilities);
305
either(&onload_solo_remaining_capabilities, both(&onload_solo_capabilities, &to_trash, &temp),
306
&onload_solo_remaining_capabilities);
308
if (to_trash.can_support_virtual_threads != 0) {
309
assert(current->can_support_virtual_threads != 0, "sanity check");
310
assert(_can_support_virtual_threads_count > 0, "sanity check");
311
_can_support_virtual_threads_count--;
317
exclude(current, unwanted, result);
321
void JvmtiManageCapabilities::update() {
322
jvmtiCapabilities avail;
325
either(&always_capabilities, &always_solo_capabilities, &avail);
328
avail.can_generate_field_access_events ||
329
avail.can_generate_field_modification_events ||
330
avail.can_generate_single_step_events ||
331
avail.can_generate_frame_pop_events ||
332
avail.can_generate_method_entry_events ||
333
avail.can_generate_method_exit_events;
335
bool enter_all_methods =
337
avail.can_generate_breakpoint_events;
338
if (enter_all_methods) {
339
// Disable these when tracking the bytecodes
340
UseFastEmptyMethods = false;
341
UseFastAccessorMethods = false;
345
if (avail.can_generate_breakpoint_events
346
|| avail.can_generate_field_access_events
347
|| avail.can_generate_field_modification_events)
349
RewriteFrequentPairs = false;
351
// The BytecodeInterpreter is specialized only with RewriteBytecodes
352
// for simplicity. If we want to disable RewriteFrequentPairs, we
353
// need to disable RewriteBytecodes as well.
354
RewriteBytecodes = false;
358
// If can_redefine_classes is enabled in the onload phase then we know that the
359
// dependency information recorded by the compiler is complete.
360
if ((avail.can_redefine_classes || avail.can_retransform_classes) &&
361
JvmtiEnv::get_phase() == JVMTI_PHASE_ONLOAD) {
362
JvmtiExport::set_all_dependencies_are_recorded(true);
365
JvmtiExport::set_can_get_source_debug_extension(avail.can_get_source_debug_extension);
366
JvmtiExport::set_can_maintain_original_method_order(avail.can_maintain_original_method_order);
367
JvmtiExport::set_can_post_interpreter_events(interp_events);
368
JvmtiExport::set_can_hotswap_or_post_breakpoint(
369
avail.can_generate_breakpoint_events ||
370
avail.can_redefine_classes ||
371
avail.can_retransform_classes);
372
JvmtiExport::set_can_modify_any_class(
373
avail.can_generate_breakpoint_events ||
374
avail.can_generate_all_class_hook_events);
375
JvmtiExport::set_can_walk_any_space(
376
avail.can_tag_objects); // disable sharing in onload phase
377
// This controls whether the compilers keep extra locals live to
378
// improve the debugging experience so only set them if the selected
379
// capabilities look like a debugger.
380
JvmtiExport::set_can_access_local_variables(
381
avail.can_access_local_variables ||
382
avail.can_generate_breakpoint_events ||
383
avail.can_generate_frame_pop_events);
384
JvmtiExport::set_can_post_on_exceptions(
385
avail.can_generate_exception_events ||
386
avail.can_generate_frame_pop_events ||
387
avail.can_generate_method_exit_events);
388
JvmtiExport::set_can_post_breakpoint(avail.can_generate_breakpoint_events);
389
JvmtiExport::set_can_post_field_access(avail.can_generate_field_access_events);
390
JvmtiExport::set_can_post_field_modification(avail.can_generate_field_modification_events);
391
JvmtiExport::set_can_post_method_entry(avail.can_generate_method_entry_events);
392
JvmtiExport::set_can_post_method_exit(avail.can_generate_method_exit_events ||
393
avail.can_generate_frame_pop_events);
394
JvmtiExport::set_can_post_frame_pop(avail.can_generate_frame_pop_events);
395
JvmtiExport::set_can_pop_frame(avail.can_pop_frame);
396
JvmtiExport::set_can_force_early_return(avail.can_force_early_return);
397
JvmtiExport::set_can_support_virtual_threads(_can_support_virtual_threads_count != 0);
398
JvmtiExport::set_should_clean_up_heap_objects(avail.can_generate_breakpoint_events);
399
JvmtiExport::set_can_get_owned_monitor_info(avail.can_get_owned_monitor_info ||
400
avail.can_get_owned_monitor_stack_depth_info);
405
void JvmtiManageCapabilities:: print(const jvmtiCapabilities* cap) {
406
log_trace(jvmti)("----- capabilities -----");
407
if (cap->can_tag_objects)
408
log_trace(jvmti)("can_tag_objects");
409
if (cap->can_generate_field_modification_events)
410
log_trace(jvmti)("can_generate_field_modification_events");
411
if (cap->can_generate_field_access_events)
412
log_trace(jvmti)("can_generate_field_access_events");
413
if (cap->can_get_bytecodes)
414
log_trace(jvmti)("can_get_bytecodes");
415
if (cap->can_get_synthetic_attribute)
416
log_trace(jvmti)("can_get_synthetic_attribute");
417
if (cap->can_get_owned_monitor_info)
418
log_trace(jvmti)("can_get_owned_monitor_info");
419
if (cap->can_get_current_contended_monitor)
420
log_trace(jvmti)("can_get_current_contended_monitor");
421
if (cap->can_get_monitor_info)
422
log_trace(jvmti)("can_get_monitor_info");
423
if (cap->can_get_constant_pool)
424
log_trace(jvmti)("can_get_constant_pool");
425
if (cap->can_pop_frame)
426
log_trace(jvmti)("can_pop_frame");
427
if (cap->can_force_early_return)
428
log_trace(jvmti)("can_force_early_return");
429
if (cap->can_redefine_classes)
430
log_trace(jvmti)("can_redefine_classes");
431
if (cap->can_retransform_classes)
432
log_trace(jvmti)("can_retransform_classes");
433
if (cap->can_signal_thread)
434
log_trace(jvmti)("can_signal_thread");
435
if (cap->can_get_source_file_name)
436
log_trace(jvmti)("can_get_source_file_name");
437
if (cap->can_get_line_numbers)
438
log_trace(jvmti)("can_get_line_numbers");
439
if (cap->can_get_source_debug_extension)
440
log_trace(jvmti)("can_get_source_debug_extension");
441
if (cap->can_access_local_variables)
442
log_trace(jvmti)("can_access_local_variables");
443
if (cap->can_maintain_original_method_order)
444
log_trace(jvmti)("can_maintain_original_method_order");
445
if (cap->can_generate_single_step_events)
446
log_trace(jvmti)("can_generate_single_step_events");
447
if (cap->can_generate_exception_events)
448
log_trace(jvmti)("can_generate_exception_events");
449
if (cap->can_generate_frame_pop_events)
450
log_trace(jvmti)("can_generate_frame_pop_events");
451
if (cap->can_generate_breakpoint_events)
452
log_trace(jvmti)("can_generate_breakpoint_events");
453
if (cap->can_generate_sampled_object_alloc_events)
454
log_trace(jvmti)("can_generate_sampled_object_alloc_events");
455
if (cap->can_suspend)
456
log_trace(jvmti)("can_suspend");
457
if (cap->can_redefine_any_class )
458
log_trace(jvmti)("can_redefine_any_class");
459
if (cap->can_retransform_any_class )
460
log_trace(jvmti)("can_retransform_any_class");
461
if (cap->can_get_current_thread_cpu_time)
462
log_trace(jvmti)("can_get_current_thread_cpu_time");
463
if (cap->can_get_thread_cpu_time)
464
log_trace(jvmti)("can_get_thread_cpu_time");
465
if (cap->can_generate_method_entry_events)
466
log_trace(jvmti)("can_generate_method_entry_events");
467
if (cap->can_generate_method_exit_events)
468
log_trace(jvmti)("can_generate_method_exit_events");
469
if (cap->can_generate_all_class_hook_events)
470
log_trace(jvmti)("can_generate_all_class_hook_events");
471
if (cap->can_generate_compiled_method_load_events)
472
log_trace(jvmti)("can_generate_compiled_method_load_events");
473
if (cap->can_generate_monitor_events)
474
log_trace(jvmti)("can_generate_monitor_events");
475
if (cap->can_generate_vm_object_alloc_events)
476
log_trace(jvmti)("can_generate_vm_object_alloc_events");
477
if (cap->can_generate_native_method_bind_events)
478
log_trace(jvmti)("can_generate_native_method_bind_events");
479
if (cap->can_generate_garbage_collection_events)
480
log_trace(jvmti)("can_generate_garbage_collection_events");
481
if (cap->can_generate_object_free_events)
482
log_trace(jvmti)("can_generate_object_free_events");
483
if (cap->can_generate_resource_exhaustion_heap_events)
484
log_trace(jvmti)("can_generate_resource_exhaustion_heap_events");
485
if (cap->can_generate_resource_exhaustion_threads_events)
486
log_trace(jvmti)("can_generate_resource_exhaustion_threads_events");
487
if (cap->can_generate_early_vmstart)
488
log_trace(jvmti)("can_generate_early_vmstart");
489
if (cap->can_generate_early_class_hook_events)
490
log_trace(jvmti)("can_generate_early_class_hook_events");
491
if (cap->can_support_virtual_threads)
492
log_trace(jvmti)("can_support_virtual_threads");