rev 50392 : JEP 331

   1 /*
   2  * Copyright (c) 2003, 2018, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   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.
   8  *
   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).
  14  *
  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.
  18  *
  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
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "jvmtifiles/jvmtiEnv.hpp"
  27 #include "logging/log.hpp"
  28 #include "prims/jvmtiExport.hpp"
  29 #include "prims/jvmtiManageCapabilities.hpp"
  30 
  31 static const jint CAPA_SIZE = (JVMTI_INTERNAL_CAPABILITY_COUNT + 7) / 8;
  32 
  33   // capabilities which are always potentially available
  34 jvmtiCapabilities JvmtiManageCapabilities::always_capabilities;
  35 
  36   // capabilities which are potentially available during OnLoad
  37 jvmtiCapabilities JvmtiManageCapabilities::onload_capabilities;
  38 
  39   // capabilities which are always potentially available
  40   // but to only one environment
  41 jvmtiCapabilities JvmtiManageCapabilities::always_solo_capabilities;
  42 
  43   // capabilities which are potentially available during OnLoad
  44   // but to only one environment
  45 jvmtiCapabilities JvmtiManageCapabilities::onload_solo_capabilities;
  46 
  47   // remaining capabilities which are always potentially available
  48   // but to only one environment
  49 jvmtiCapabilities JvmtiManageCapabilities::always_solo_remaining_capabilities;
  50 
  51   // remaining capabilities which are potentially available during OnLoad
  52   // but to only one environment
  53 jvmtiCapabilities JvmtiManageCapabilities::onload_solo_remaining_capabilities;
  54 
  55   // all capabilities ever acquired
  56 jvmtiCapabilities JvmtiManageCapabilities::acquired_capabilities;
  57 
  58 void JvmtiManageCapabilities::initialize() {
  59   always_capabilities = init_always_capabilities();
  60   onload_capabilities = init_onload_capabilities();
  61   always_solo_capabilities = init_always_solo_capabilities();
  62   onload_solo_capabilities = init_onload_solo_capabilities();
  63   always_solo_remaining_capabilities = init_always_solo_capabilities();
  64   onload_solo_remaining_capabilities = init_onload_solo_capabilities();
  65   memset(&acquired_capabilities, 0, sizeof(acquired_capabilities));
  66 }
  67 
  68 
  69 // corresponding init functions
  70 jvmtiCapabilities JvmtiManageCapabilities::init_always_capabilities() {
  71   jvmtiCapabilities jc;
  72 
  73   memset(&jc, 0, sizeof(jc));
  74   jc.can_get_bytecodes = 1;
  75   jc.can_signal_thread = 1;
  76   jc.can_get_source_file_name = 1;
  77   jc.can_get_line_numbers = 1;
  78   jc.can_get_synthetic_attribute = 1;
  79   jc.can_get_monitor_info = 1;
  80   jc.can_get_constant_pool = 1;
  81   jc.can_generate_all_class_hook_events = 1;
  82   jc.can_generate_monitor_events = 1;
  83   jc.can_generate_garbage_collection_events = 1;
  84   jc.can_generate_compiled_method_load_events = 1;
  85   jc.can_generate_native_method_bind_events = 1;
  86   jc.can_generate_vm_object_alloc_events = 1;
  87   if (os::is_thread_cpu_time_supported()) {
  88     jc.can_get_current_thread_cpu_time = 1;
  89     jc.can_get_thread_cpu_time = 1;
  90   }
  91   jc.can_redefine_classes = 1;
  92   jc.can_redefine_any_class = 1;
  93   jc.can_retransform_classes = 1;
  94   jc.can_retransform_any_class = 1;
  95   jc.can_set_native_method_prefix = 1;
  96   jc.can_tag_objects = 1;
  97   jc.can_generate_object_free_events = 1;
  98   jc.can_generate_resource_exhaustion_heap_events = 1;
  99   jc.can_generate_resource_exhaustion_threads_events = 1;
 100   return jc;
 101 }
 102 
 103 jvmtiCapabilities JvmtiManageCapabilities::init_onload_capabilities() {
 104   jvmtiCapabilities jc;
 105 
 106   memset(&jc, 0, sizeof(jc));
 107 #ifndef ZERO
 108   jc.can_pop_frame = 1;
 109   jc.can_force_early_return = 1;
 110 #endif // !ZERO
 111   jc.can_get_source_debug_extension = 1;
 112   jc.can_access_local_variables = 1;
 113   jc.can_maintain_original_method_order = 1;
 114   jc.can_generate_single_step_events = 1;
 115   jc.can_generate_exception_events = 1;
 116   jc.can_generate_frame_pop_events = 1;
 117   jc.can_generate_method_entry_events = 1;
 118   jc.can_generate_method_exit_events = 1;
 119   jc.can_get_owned_monitor_info = 1;
 120   jc.can_get_owned_monitor_stack_depth_info = 1;
 121   jc.can_get_current_contended_monitor = 1;
 122   jc.can_generate_early_vmstart = 1;
 123   jc.can_generate_early_class_hook_events = 1;
 124   return jc;
 125 }
 126 
 127 
 128 jvmtiCapabilities JvmtiManageCapabilities::init_always_solo_capabilities() {
 129   jvmtiCapabilities jc;
 130 
 131   memset(&jc, 0, sizeof(jc));
 132   jc.can_suspend = 1;

 133   return jc;
 134 }
 135 
 136 
 137 jvmtiCapabilities JvmtiManageCapabilities::init_onload_solo_capabilities() {
 138   jvmtiCapabilities jc;
 139 
 140   memset(&jc, 0, sizeof(jc));
 141   jc.can_generate_field_modification_events = 1;
 142   jc.can_generate_field_access_events = 1;
 143   jc.can_generate_breakpoint_events = 1;
 144   return jc;
 145 }
 146 
 147 
 148 jvmtiCapabilities *JvmtiManageCapabilities::either(const jvmtiCapabilities *a, const jvmtiCapabilities *b,
 149                                                    jvmtiCapabilities *result) {
 150   char *ap = (char *)a;
 151   char *bp = (char *)b;
 152   char *resultp = (char *)result;
 153 
 154   for (int i = 0; i < CAPA_SIZE; ++i) {
 155     *resultp++ = *ap++ | *bp++;
 156   }
 157 
 158   return result;
 159 }
 160 
 161 
 162 jvmtiCapabilities *JvmtiManageCapabilities::both(const jvmtiCapabilities *a, const jvmtiCapabilities *b,
 163                                                     jvmtiCapabilities *result) {
 164   char *ap = (char *)a;
 165   char *bp = (char *)b;
 166   char *resultp = (char *)result;
 167 
 168   for (int i = 0; i < CAPA_SIZE; ++i) {
 169     *resultp++ = *ap++ & *bp++;
 170   }
 171 
 172   return result;
 173 }
 174 
 175 
 176 jvmtiCapabilities *JvmtiManageCapabilities::exclude(const jvmtiCapabilities *a, const jvmtiCapabilities *b,
 177                                                     jvmtiCapabilities *result) {
 178   char *ap = (char *)a;
 179   char *bp = (char *)b;
 180   char *resultp = (char *)result;
 181 
 182   for (int i = 0; i < CAPA_SIZE; ++i) {
 183     *resultp++ = *ap++ & ~*bp++;
 184   }
 185 
 186   return result;
 187 }
 188 
 189 
 190 bool JvmtiManageCapabilities::has_some(const jvmtiCapabilities *a) {
 191   char *ap = (char *)a;
 192 
 193   for (int i = 0; i < CAPA_SIZE; ++i) {
 194     if (*ap++ != 0) {
 195       return true;
 196     }
 197   }
 198 
 199   return false;
 200 }
 201 
 202 
 203 void JvmtiManageCapabilities::copy_capabilities(const jvmtiCapabilities *from, jvmtiCapabilities *to) {
 204   char *ap = (char *)from;
 205   char *resultp = (char *)to;
 206 
 207   for (int i = 0; i < CAPA_SIZE; ++i) {
 208     *resultp++ = *ap++;
 209   }
 210 }
 211 
 212 
 213 void JvmtiManageCapabilities::get_potential_capabilities(const jvmtiCapabilities *current,
 214                                                          const jvmtiCapabilities *prohibited,
 215                                                          jvmtiCapabilities *result) {
 216   // exclude prohibited capabilities, must be before adding current
 217   exclude(&always_capabilities, prohibited, result);
 218 
 219   // must include current since it may possess solo capabilities and now prohibited
 220   either(result, current, result);
 221 
 222   // add other remaining
 223   either(result, &always_solo_remaining_capabilities, result);
 224 
 225   // if this is during OnLoad more capabilities are available
 226   if (JvmtiEnv::get_phase() == JVMTI_PHASE_ONLOAD) {
 227     either(result, &onload_capabilities, result);
 228     either(result, &onload_solo_remaining_capabilities, result);
 229   }
 230 }
 231 
 232 jvmtiError JvmtiManageCapabilities::add_capabilities(const jvmtiCapabilities *current,
 233                                                      const jvmtiCapabilities *prohibited,
 234                                                      const jvmtiCapabilities *desired,
 235                                                      jvmtiCapabilities *result) {
 236   // check that the capabilities being added are potential capabilities
 237   jvmtiCapabilities temp;
 238   get_potential_capabilities(current, prohibited, &temp);
 239   if (has_some(exclude(desired, &temp, &temp))) {
 240     return JVMTI_ERROR_NOT_AVAILABLE;
 241   }
 242 
 243   // add to the set of ever acquired capabilities
 244   either(&acquired_capabilities, desired, &acquired_capabilities);
 245 
 246   // onload capabilities that got added are now permanent - so, also remove from onload
 247   both(&onload_capabilities, desired, &temp);
 248   either(&always_capabilities, &temp, &always_capabilities);
 249   exclude(&onload_capabilities, &temp, &onload_capabilities);
 250 
 251   // same for solo capabilities (transferred capabilities in the remaining sets handled as part of standard grab - below)
 252   both(&onload_solo_capabilities, desired, &temp);
 253   either(&always_solo_capabilities, &temp, &always_solo_capabilities);
 254   exclude(&onload_solo_capabilities, &temp, &onload_solo_capabilities);
 255 
 256   // remove solo capabilities that are now taken
 257   exclude(&always_solo_remaining_capabilities, desired, &always_solo_remaining_capabilities);
 258   exclude(&onload_solo_remaining_capabilities, desired, &onload_solo_remaining_capabilities);
 259 
 260   // return the result
 261   either(current, desired, result);
 262 
 263   update();
 264 
 265   return JVMTI_ERROR_NONE;
 266 }
 267 
 268 
 269 void JvmtiManageCapabilities::relinquish_capabilities(const jvmtiCapabilities *current,
 270                                                       const jvmtiCapabilities *unwanted,
 271                                                       jvmtiCapabilities *result) {
 272   jvmtiCapabilities to_trash;
 273   jvmtiCapabilities temp;
 274 
 275   // can't give up what you don't have
 276   both(current, unwanted, &to_trash);
 277 
 278   // restore solo capabilities but only those that belong
 279   either(&always_solo_remaining_capabilities, both(&always_solo_capabilities, &to_trash, &temp),
 280          &always_solo_remaining_capabilities);
 281   either(&onload_solo_remaining_capabilities, both(&onload_solo_capabilities, &to_trash, &temp),
 282          &onload_solo_remaining_capabilities);
 283 
 284   update();
 285 
 286   // return the result
 287   exclude(current, unwanted, result);
 288 }
 289 
 290 
 291 void JvmtiManageCapabilities::update() {
 292   jvmtiCapabilities avail;
 293 
 294   // all capabilities
 295   either(&always_capabilities, &always_solo_capabilities, &avail);
 296 
 297   bool interp_events =
 298     avail.can_generate_field_access_events ||
 299     avail.can_generate_field_modification_events ||
 300     avail.can_generate_single_step_events ||
 301     avail.can_generate_frame_pop_events ||
 302     avail.can_generate_method_entry_events ||
 303     avail.can_generate_method_exit_events;
 304 #ifdef ZERO
 305   bool enter_all_methods =
 306     interp_events ||
 307     avail.can_generate_breakpoint_events;
 308   if (enter_all_methods) {
 309     // Disable these when tracking the bytecodes
 310     UseFastEmptyMethods = false;
 311     UseFastAccessorMethods = false;
 312   }
 313 #endif // ZERO
 314 
 315   if (avail.can_generate_breakpoint_events
 316        || avail.can_generate_field_access_events
 317        || avail.can_generate_field_modification_events)
 318   {
 319     RewriteFrequentPairs = false;
 320   }
 321 
 322   // If can_redefine_classes is enabled in the onload phase then we know that the
 323   // dependency information recorded by the compiler is complete.
 324   if ((avail.can_redefine_classes || avail.can_retransform_classes) &&
 325       JvmtiEnv::get_phase() == JVMTI_PHASE_ONLOAD) {
 326     JvmtiExport::set_all_dependencies_are_recorded(true);
 327   }
 328 
 329   JvmtiExport::set_can_get_source_debug_extension(avail.can_get_source_debug_extension);
 330   JvmtiExport::set_can_maintain_original_method_order(avail.can_maintain_original_method_order);
 331   JvmtiExport::set_can_post_interpreter_events(interp_events);
 332   JvmtiExport::set_can_hotswap_or_post_breakpoint(
 333     avail.can_generate_breakpoint_events ||
 334     avail.can_redefine_classes ||
 335     avail.can_retransform_classes);
 336   JvmtiExport::set_can_modify_any_class(
 337     avail.can_generate_breakpoint_events ||
 338     avail.can_generate_all_class_hook_events);
 339   JvmtiExport::set_can_walk_any_space(
 340     avail.can_tag_objects);   // disable sharing in onload phase
 341   // This controls whether the compilers keep extra locals live to
 342   // improve the debugging experience so only set them if the selected
 343   // capabilities look like a debugger.
 344   JvmtiExport::set_can_access_local_variables(
 345     avail.can_access_local_variables ||
 346     avail.can_generate_breakpoint_events ||
 347     avail.can_generate_frame_pop_events);
 348   JvmtiExport::set_can_post_on_exceptions(
 349     avail.can_generate_exception_events ||
 350     avail.can_generate_frame_pop_events ||
 351     avail.can_generate_method_exit_events);
 352   JvmtiExport::set_can_post_breakpoint(avail.can_generate_breakpoint_events);
 353   JvmtiExport::set_can_post_field_access(avail.can_generate_field_access_events);
 354   JvmtiExport::set_can_post_field_modification(avail.can_generate_field_modification_events);
 355   JvmtiExport::set_can_post_method_entry(avail.can_generate_method_entry_events);
 356   JvmtiExport::set_can_post_method_exit(avail.can_generate_method_exit_events ||
 357                                         avail.can_generate_frame_pop_events);
 358   JvmtiExport::set_can_pop_frame(avail.can_pop_frame);
 359   JvmtiExport::set_can_force_early_return(avail.can_force_early_return);
 360   JvmtiExport::set_should_clean_up_heap_objects(avail.can_generate_breakpoint_events);
 361 }
 362 
 363 #ifndef PRODUCT
 364 
 365 void JvmtiManageCapabilities:: print(const jvmtiCapabilities* cap) {
 366   log_trace(jvmti)("----- capabilities -----");
 367   if (cap->can_tag_objects)
 368     log_trace(jvmti)("can_tag_objects");
 369   if (cap->can_generate_field_modification_events)
 370     log_trace(jvmti)("can_generate_field_modification_events");
 371   if (cap->can_generate_field_access_events)
 372     log_trace(jvmti)("can_generate_field_access_events");
 373   if (cap->can_get_bytecodes)
 374     log_trace(jvmti)("can_get_bytecodes");
 375   if (cap->can_get_synthetic_attribute)
 376     log_trace(jvmti)("can_get_synthetic_attribute");
 377   if (cap->can_get_owned_monitor_info)
 378     log_trace(jvmti)("can_get_owned_monitor_info");
 379   if (cap->can_get_current_contended_monitor)
 380     log_trace(jvmti)("can_get_current_contended_monitor");
 381   if (cap->can_get_monitor_info)
 382     log_trace(jvmti)("can_get_monitor_info");
 383   if (cap->can_get_constant_pool)
 384     log_trace(jvmti)("can_get_constant_pool");
 385   if (cap->can_pop_frame)
 386     log_trace(jvmti)("can_pop_frame");
 387   if (cap->can_force_early_return)
 388     log_trace(jvmti)("can_force_early_return");
 389   if (cap->can_redefine_classes)
 390     log_trace(jvmti)("can_redefine_classes");
 391   if (cap->can_retransform_classes)
 392     log_trace(jvmti)("can_retransform_classes");
 393   if (cap->can_signal_thread)
 394     log_trace(jvmti)("can_signal_thread");
 395   if (cap->can_get_source_file_name)
 396     log_trace(jvmti)("can_get_source_file_name");
 397   if (cap->can_get_line_numbers)
 398     log_trace(jvmti)("can_get_line_numbers");
 399   if (cap->can_get_source_debug_extension)
 400     log_trace(jvmti)("can_get_source_debug_extension");
 401   if (cap->can_access_local_variables)
 402     log_trace(jvmti)("can_access_local_variables");
 403   if (cap->can_maintain_original_method_order)
 404     log_trace(jvmti)("can_maintain_original_method_order");
 405   if (cap->can_generate_single_step_events)
 406     log_trace(jvmti)("can_generate_single_step_events");
 407   if (cap->can_generate_exception_events)
 408     log_trace(jvmti)("can_generate_exception_events");
 409   if (cap->can_generate_frame_pop_events)
 410     log_trace(jvmti)("can_generate_frame_pop_events");
 411   if (cap->can_generate_breakpoint_events)
 412     log_trace(jvmti)("can_generate_breakpoint_events");


 413   if (cap->can_suspend)
 414     log_trace(jvmti)("can_suspend");
 415   if (cap->can_redefine_any_class )
 416     log_trace(jvmti)("can_redefine_any_class");
 417   if (cap->can_retransform_any_class )
 418     log_trace(jvmti)("can_retransform_any_class");
 419   if (cap->can_get_current_thread_cpu_time)
 420     log_trace(jvmti)("can_get_current_thread_cpu_time");
 421   if (cap->can_get_thread_cpu_time)
 422     log_trace(jvmti)("can_get_thread_cpu_time");
 423   if (cap->can_generate_method_entry_events)
 424     log_trace(jvmti)("can_generate_method_entry_events");
 425   if (cap->can_generate_method_exit_events)
 426     log_trace(jvmti)("can_generate_method_exit_events");
 427   if (cap->can_generate_all_class_hook_events)
 428     log_trace(jvmti)("can_generate_all_class_hook_events");
 429   if (cap->can_generate_compiled_method_load_events)
 430     log_trace(jvmti)("can_generate_compiled_method_load_events");
 431   if (cap->can_generate_monitor_events)
 432     log_trace(jvmti)("can_generate_monitor_events");
 433   if (cap->can_generate_vm_object_alloc_events)
 434     log_trace(jvmti)("can_generate_vm_object_alloc_events");
 435   if (cap->can_generate_native_method_bind_events)
 436     log_trace(jvmti)("can_generate_native_method_bind_events");
 437   if (cap->can_generate_garbage_collection_events)
 438     log_trace(jvmti)("can_generate_garbage_collection_events");
 439   if (cap->can_generate_object_free_events)
 440     log_trace(jvmti)("can_generate_object_free_events");
 441   if (cap->can_generate_resource_exhaustion_heap_events)
 442     log_trace(jvmti)("can_generate_resource_exhaustion_heap_events");
 443   if (cap->can_generate_resource_exhaustion_threads_events)
 444     log_trace(jvmti)("can_generate_resource_exhaustion_threads_events");
 445   if (cap->can_generate_early_vmstart)
 446     log_trace(jvmti)("can_generate_early_vmstart");
 447   if (cap->can_generate_early_class_hook_events)
 448     log_trace(jvmti)("can_generate_early_class_hook_events");
 449 }
 450 
 451 #endif
--- EOF ---