jvmtiManageCapabilities.cpp 17.1 KB
Newer Older
D
duke 已提交
1
/*
2
 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
D
duke 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
19 20 21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
D
duke 已提交
22 23 24
 *
 */

25 26 27 28
#include "precompiled.hpp"
#include "jvmtifiles/jvmtiEnv.hpp"
#include "prims/jvmtiExport.hpp"
#include "prims/jvmtiManageCapabilities.hpp"
D
duke 已提交
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
static const jint CAPA_SIZE = (JVMTI_INTERNAL_CAPABILITY_COUNT + 7) / 8;

  // capabilities which are always potentially available
jvmtiCapabilities JvmtiManageCapabilities::always_capabilities;

  // capabilities which are potentially available during OnLoad
jvmtiCapabilities JvmtiManageCapabilities::onload_capabilities;

  // capabilities which are always potentially available
  // but to only one environment
jvmtiCapabilities JvmtiManageCapabilities::always_solo_capabilities;

  // capabilities which are potentially available during OnLoad
  // but to only one environment
jvmtiCapabilities JvmtiManageCapabilities::onload_solo_capabilities;

  // remaining capabilities which are always potentially available
  // but to only one environment
jvmtiCapabilities JvmtiManageCapabilities::always_solo_remaining_capabilities;

  // remaining capabilities which are potentially available during OnLoad
  // but to only one environment
jvmtiCapabilities JvmtiManageCapabilities::onload_solo_remaining_capabilities;

  // all capabilities ever acquired
jvmtiCapabilities JvmtiManageCapabilities::acquired_capabilities;

void JvmtiManageCapabilities::initialize() {
  always_capabilities = init_always_capabilities();
  if (JvmtiEnv::get_phase() != JVMTI_PHASE_ONLOAD) {
    recompute_always_capabilities();
  }
  onload_capabilities = init_onload_capabilities();
  always_solo_capabilities = init_always_solo_capabilities();
  onload_solo_capabilities = init_onload_solo_capabilities();
  always_solo_remaining_capabilities = init_always_solo_capabilities();
  onload_solo_remaining_capabilities = init_onload_solo_capabilities();
  memset(&acquired_capabilities, 0, sizeof(acquired_capabilities));
}

// if the capability sets are initialized in the onload phase then
// it happens before class data sharing (CDS) is initialized. If it
// turns out that CDS gets disabled then we must adjust the always
// capabilities. To ensure a consistent view of the capabililties
// anything we add here should already be in the onload set.
void JvmtiManageCapabilities::recompute_always_capabilities() {
  if (!UseSharedSpaces) {
    jvmtiCapabilities jc = always_capabilities;
    jc.can_generate_all_class_hook_events = 1;
    always_capabilities = jc;
  }
}


// corresponding init functions
jvmtiCapabilities JvmtiManageCapabilities::init_always_capabilities() {
  jvmtiCapabilities jc;

  memset(&jc, 0, sizeof(jc));
  jc.can_get_bytecodes = 1;
  jc.can_signal_thread = 1;
  jc.can_get_source_file_name = 1;
  jc.can_get_line_numbers = 1;
  jc.can_get_synthetic_attribute = 1;
  jc.can_get_monitor_info = 1;
  jc.can_get_constant_pool = 1;
  jc.can_generate_monitor_events = 1;
  jc.can_generate_garbage_collection_events = 1;
  jc.can_generate_compiled_method_load_events = 1;
  jc.can_generate_native_method_bind_events = 1;
  jc.can_generate_vm_object_alloc_events = 1;
  if (os::is_thread_cpu_time_supported()) {
    jc.can_get_current_thread_cpu_time = 1;
    jc.can_get_thread_cpu_time = 1;
  }
  jc.can_redefine_classes = 1;
  jc.can_redefine_any_class = 1;
  jc.can_retransform_classes = 1;
  jc.can_retransform_any_class = 1;
  jc.can_set_native_method_prefix = 1;
  jc.can_tag_objects = 1;
  jc.can_generate_object_free_events = 1;
  jc.can_generate_resource_exhaustion_heap_events = 1;
  jc.can_generate_resource_exhaustion_threads_events = 1;
  return jc;
}

jvmtiCapabilities JvmtiManageCapabilities::init_onload_capabilities() {
  jvmtiCapabilities jc;

  memset(&jc, 0, sizeof(jc));
120
#ifndef ZERO
D
duke 已提交
121 122
  jc.can_pop_frame = 1;
  jc.can_force_early_return = 1;
123
#endif // !ZERO
D
duke 已提交
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321
  jc.can_get_source_debug_extension = 1;
  jc.can_access_local_variables = 1;
  jc.can_maintain_original_method_order = 1;
  jc.can_generate_all_class_hook_events = 1;
  jc.can_generate_single_step_events = 1;
  jc.can_generate_exception_events = 1;
  jc.can_generate_frame_pop_events = 1;
  jc.can_generate_method_entry_events = 1;
  jc.can_generate_method_exit_events = 1;
  jc.can_get_owned_monitor_info = 1;
  jc.can_get_owned_monitor_stack_depth_info = 1;
  jc.can_get_current_contended_monitor = 1;
  // jc.can_get_monitor_info = 1;
  jc.can_tag_objects = 1;                 // TODO: this should have been removed
  jc.can_generate_object_free_events = 1; // TODO: this should have been removed
  return jc;
}


jvmtiCapabilities JvmtiManageCapabilities::init_always_solo_capabilities() {
  jvmtiCapabilities jc;

  memset(&jc, 0, sizeof(jc));
  jc.can_suspend = 1;
  return jc;
}


jvmtiCapabilities JvmtiManageCapabilities::init_onload_solo_capabilities() {
  jvmtiCapabilities jc;

  memset(&jc, 0, sizeof(jc));
  jc.can_generate_field_modification_events = 1;
  jc.can_generate_field_access_events = 1;
  jc.can_generate_breakpoint_events = 1;
  return jc;
}


jvmtiCapabilities *JvmtiManageCapabilities::either(const jvmtiCapabilities *a, const jvmtiCapabilities *b,
                                                   jvmtiCapabilities *result) {
  char *ap = (char *)a;
  char *bp = (char *)b;
  char *resultp = (char *)result;

  for (int i = 0; i < CAPA_SIZE; ++i) {
    *resultp++ = *ap++ | *bp++;
  }

  return result;
}


jvmtiCapabilities *JvmtiManageCapabilities::both(const jvmtiCapabilities *a, const jvmtiCapabilities *b,
                                                    jvmtiCapabilities *result) {
  char *ap = (char *)a;
  char *bp = (char *)b;
  char *resultp = (char *)result;

  for (int i = 0; i < CAPA_SIZE; ++i) {
    *resultp++ = *ap++ & *bp++;
  }

  return result;
}


jvmtiCapabilities *JvmtiManageCapabilities::exclude(const jvmtiCapabilities *a, const jvmtiCapabilities *b,
                                                    jvmtiCapabilities *result) {
  char *ap = (char *)a;
  char *bp = (char *)b;
  char *resultp = (char *)result;

  for (int i = 0; i < CAPA_SIZE; ++i) {
    *resultp++ = *ap++ & ~*bp++;
  }

  return result;
}


bool JvmtiManageCapabilities::has_some(const jvmtiCapabilities *a) {
  char *ap = (char *)a;

  for (int i = 0; i < CAPA_SIZE; ++i) {
    if (*ap++ != 0) {
      return true;
    }
  }

  return false;
}


void JvmtiManageCapabilities::copy_capabilities(const jvmtiCapabilities *from, jvmtiCapabilities *to) {
  char *ap = (char *)from;
  char *resultp = (char *)to;

  for (int i = 0; i < CAPA_SIZE; ++i) {
    *resultp++ = *ap++;
  }
}


void JvmtiManageCapabilities::get_potential_capabilities(const jvmtiCapabilities *current,
                                                         const jvmtiCapabilities *prohibited,
                                                         jvmtiCapabilities *result) {
  // exclude prohibited capabilities, must be before adding current
  exclude(&always_capabilities, prohibited, result);

  // must include current since it may possess solo capabilities and now prohibited
  either(result, current, result);

  // add other remaining
  either(result, &always_solo_remaining_capabilities, result);

  // if this is during OnLoad more capabilities are available
  if (JvmtiEnv::get_phase() == JVMTI_PHASE_ONLOAD) {
    either(result, &onload_capabilities, result);
    either(result, &onload_solo_remaining_capabilities, result);
  }
}

jvmtiError JvmtiManageCapabilities::add_capabilities(const jvmtiCapabilities *current,
                                                     const jvmtiCapabilities *prohibited,
                                                     const jvmtiCapabilities *desired,
                                                     jvmtiCapabilities *result) {
  // check that the capabilities being added are potential capabilities
  jvmtiCapabilities temp;
  get_potential_capabilities(current, prohibited, &temp);
  if (has_some(exclude(desired, &temp, &temp))) {
    return JVMTI_ERROR_NOT_AVAILABLE;
  }

  // add to the set of ever acquired capabilities
  either(&acquired_capabilities, desired, &acquired_capabilities);

  // onload capabilities that got added are now permanent - so, also remove from onload
  both(&onload_capabilities, desired, &temp);
  either(&always_capabilities, &temp, &always_capabilities);
  exclude(&onload_capabilities, &temp, &onload_capabilities);

  // same for solo capabilities (transferred capabilities in the remaining sets handled as part of standard grab - below)
  both(&onload_solo_capabilities, desired, &temp);
  either(&always_solo_capabilities, &temp, &always_solo_capabilities);
  exclude(&onload_solo_capabilities, &temp, &onload_solo_capabilities);

  // remove solo capabilities that are now taken
  exclude(&always_solo_remaining_capabilities, desired, &always_solo_remaining_capabilities);
  exclude(&onload_solo_remaining_capabilities, desired, &onload_solo_remaining_capabilities);

  // return the result
  either(current, desired, result);

  update();

  return JVMTI_ERROR_NONE;
}


void JvmtiManageCapabilities::relinquish_capabilities(const jvmtiCapabilities *current,
                                                      const jvmtiCapabilities *unwanted,
                                                      jvmtiCapabilities *result) {
  jvmtiCapabilities to_trash;
  jvmtiCapabilities temp;

  // can't give up what you don't have
  both(current, unwanted, &to_trash);

  // restore solo capabilities but only those that belong
  either(&always_solo_remaining_capabilities, both(&always_solo_capabilities, &to_trash, &temp),
         &always_solo_remaining_capabilities);
  either(&onload_solo_remaining_capabilities, both(&onload_solo_capabilities, &to_trash, &temp),
         &onload_solo_remaining_capabilities);

  update();

  // return the result
  exclude(current, unwanted, result);
}


void JvmtiManageCapabilities::update() {
  jvmtiCapabilities avail;

  // all capabilities
  either(&always_capabilities, &always_solo_capabilities, &avail);

  bool interp_events =
    avail.can_generate_field_access_events ||
    avail.can_generate_field_modification_events ||
    avail.can_generate_single_step_events ||
    avail.can_generate_frame_pop_events ||
    avail.can_generate_method_entry_events ||
    avail.can_generate_method_exit_events;
  bool enter_all_methods =
    interp_events ||
    avail.can_generate_breakpoint_events;
322 323 324 325 326
  if (enter_all_methods) {
    // Disable these when tracking the bytecodes
    UseFastEmptyMethods = false;
    UseFastAccessorMethods = false;
  }
D
duke 已提交
327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350

  if (avail.can_generate_breakpoint_events) {
    RewriteFrequentPairs = false;
  }

  // If can_redefine_classes is enabled in the onload phase then we know that the
  // dependency information recorded by the compiler is complete.
  if ((avail.can_redefine_classes || avail.can_retransform_classes) &&
      JvmtiEnv::get_phase() == JVMTI_PHASE_ONLOAD) {
    JvmtiExport::set_all_dependencies_are_recorded(true);
  }

  JvmtiExport::set_can_get_source_debug_extension(avail.can_get_source_debug_extension);
  JvmtiExport::set_can_maintain_original_method_order(avail.can_maintain_original_method_order);
  JvmtiExport::set_can_post_interpreter_events(interp_events);
  JvmtiExport::set_can_hotswap_or_post_breakpoint(
    avail.can_generate_breakpoint_events ||
    avail.can_redefine_classes ||
    avail.can_retransform_classes);
  JvmtiExport::set_can_modify_any_class(
    avail.can_generate_breakpoint_events ||
    avail.can_generate_all_class_hook_events);
  JvmtiExport::set_can_walk_any_space(
    avail.can_tag_objects);   // disable sharing in onload phase
351 352 353
  // This controls whether the compilers keep extra locals live to
  // improve the debugging experience so only set them if the selected
  // capabilities look like a debugger.
D
duke 已提交
354
  JvmtiExport::set_can_access_local_variables(
355 356 357
    avail.can_access_local_variables ||
    avail.can_generate_breakpoint_events ||
    avail.can_generate_frame_pop_events);
358
  JvmtiExport::set_can_post_on_exceptions(
D
duke 已提交
359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457
    avail.can_generate_exception_events ||
    avail.can_generate_frame_pop_events ||
    avail.can_generate_method_exit_events);
  JvmtiExport::set_can_post_breakpoint(avail.can_generate_breakpoint_events);
  JvmtiExport::set_can_post_field_access(avail.can_generate_field_access_events);
  JvmtiExport::set_can_post_field_modification(avail.can_generate_field_modification_events);
  JvmtiExport::set_can_post_method_entry(avail.can_generate_method_entry_events);
  JvmtiExport::set_can_post_method_exit(avail.can_generate_method_exit_events ||
                                        avail.can_generate_frame_pop_events);
  JvmtiExport::set_can_pop_frame(avail.can_pop_frame);
  JvmtiExport::set_can_force_early_return(avail.can_force_early_return);
  JvmtiExport::set_should_clean_up_heap_objects(avail.can_generate_breakpoint_events);
}

#ifndef PRODUCT

void JvmtiManageCapabilities:: print(const jvmtiCapabilities* cap) {
  tty->print_cr("----- capabilities -----");
  if (cap->can_tag_objects)
    tty->print_cr("can_tag_objects");
  if (cap->can_generate_field_modification_events)
    tty->print_cr("can_generate_field_modification_events");
  if (cap->can_generate_field_access_events)
    tty->print_cr("can_generate_field_access_events");
  if (cap->can_get_bytecodes)
    tty->print_cr("can_get_bytecodes");
  if (cap->can_get_synthetic_attribute)
    tty->print_cr("can_get_synthetic_attribute");
  if (cap->can_get_owned_monitor_info)
    tty->print_cr("can_get_owned_monitor_info");
  if (cap->can_get_current_contended_monitor)
    tty->print_cr("can_get_current_contended_monitor");
  if (cap->can_get_monitor_info)
    tty->print_cr("can_get_monitor_info");
  if (cap->can_get_constant_pool)
    tty->print_cr("can_get_constant_pool");
  if (cap->can_pop_frame)
    tty->print_cr("can_pop_frame");
  if (cap->can_force_early_return)
    tty->print_cr("can_force_early_return");
  if (cap->can_redefine_classes)
    tty->print_cr("can_redefine_classes");
  if (cap->can_retransform_classes)
    tty->print_cr("can_retransform_classes");
  if (cap->can_signal_thread)
    tty->print_cr("can_signal_thread");
  if (cap->can_get_source_file_name)
    tty->print_cr("can_get_source_file_name");
  if (cap->can_get_line_numbers)
    tty->print_cr("can_get_line_numbers");
  if (cap->can_get_source_debug_extension)
    tty->print_cr("can_get_source_debug_extension");
  if (cap->can_access_local_variables)
    tty->print_cr("can_access_local_variables");
  if (cap->can_maintain_original_method_order)
    tty->print_cr("can_maintain_original_method_order");
  if (cap->can_generate_single_step_events)
    tty->print_cr("can_generate_single_step_events");
  if (cap->can_generate_exception_events)
    tty->print_cr("can_generate_exception_events");
  if (cap->can_generate_frame_pop_events)
    tty->print_cr("can_generate_frame_pop_events");
  if (cap->can_generate_breakpoint_events)
    tty->print_cr("can_generate_breakpoint_events");
  if (cap->can_suspend)
    tty->print_cr("can_suspend");
  if (cap->can_redefine_any_class )
    tty->print_cr("can_redefine_any_class");
  if (cap->can_retransform_any_class )
    tty->print_cr("can_retransform_any_class");
  if (cap->can_get_current_thread_cpu_time)
    tty->print_cr("can_get_current_thread_cpu_time");
  if (cap->can_get_thread_cpu_time)
    tty->print_cr("can_get_thread_cpu_time");
  if (cap->can_generate_method_entry_events)
    tty->print_cr("can_generate_method_entry_events");
  if (cap->can_generate_method_exit_events)
    tty->print_cr("can_generate_method_exit_events");
  if (cap->can_generate_all_class_hook_events)
    tty->print_cr("can_generate_all_class_hook_events");
  if (cap->can_generate_compiled_method_load_events)
    tty->print_cr("can_generate_compiled_method_load_events");
  if (cap->can_generate_monitor_events)
    tty->print_cr("can_generate_monitor_events");
  if (cap->can_generate_vm_object_alloc_events)
    tty->print_cr("can_generate_vm_object_alloc_events");
  if (cap->can_generate_native_method_bind_events)
    tty->print_cr("can_generate_native_method_bind_events");
  if (cap->can_generate_garbage_collection_events)
    tty->print_cr("can_generate_garbage_collection_events");
  if (cap->can_generate_object_free_events)
    tty->print_cr("can_generate_object_free_events");
  if (cap->can_generate_resource_exhaustion_heap_events)
    tty->print_cr("can_generate_resource_exhaustion_heap_events");
  if (cap->can_generate_resource_exhaustion_threads_events)
    tty->print_cr("can_generate_resource_exhaustion_threads_events");
}

#endif