1 /*
   2  * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
   3  * Copyright (c) 2019 SAP SE. All rights reserved.
   4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   5  *
   6  * This code is free software; you can redistribute it and/or modify it
   7  * under the terms of the GNU General Public License version 2 only, as
   8  * published by the Free Software Foundation.
   9  *
  10  * This code is distributed in the hope that it will be useful, but WITHOUT
  11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  13  * version 2 for more details (a copy is included in the LICENSE file that
  14  * accompanied this code).
  15  *
  16  * You should have received a copy of the GNU General Public License version
  17  * 2 along with this work; if not, write to the Free Software Foundation,
  18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  19  *
  20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  21  * or visit www.oracle.com if you need additional information or have any
  22  * questions.
  23  *
  24  */
  25 
  26 #include "precompiled.hpp"
  27 #include "classfile/bytecodeUtils.hpp"
  28 #include "classfile/systemDictionary.hpp"
  29 #include "gc/shared/gcLocker.hpp"
  30 #include "memory/resourceArea.hpp"
  31 #include "runtime/signature.hpp"
  32 #include "utilities/events.hpp"
  33 
  34 int MethodBytecodePrinter::get_s8(Method* method, int* pos) {
  35   signed char* bcp = *pos + (signed char*) (method->constMethod()->code_base());
  36   *pos += 1;
  37 
  38   return *bcp;
  39 }
  40 
  41 int MethodBytecodePrinter::get_s16(Method* method, int* pos) {
  42   int16_t result = (int16_t) Bytes::get_Java_u2(method->constMethod()->code_base() + *pos);
  43   *pos += 2;
  44 
  45   return result;
  46 }
  47 
  48 int MethodBytecodePrinter::get_s32(Method* method, int* pos) {
  49   int32_t result = (int32_t) Bytes::get_Java_u4(method->constMethod()->code_base() + *pos);
  50   *pos += 4;
  51 
  52   return result;
  53 }
  54 
  55 int MethodBytecodePrinter::get_u8(Method* method, int* pos)  {
  56   uint8_t* bcp = *pos + (uint8_t*) (method->constMethod()->code_base());
  57   *pos += 1;
  58 
  59   return *bcp;
  60 }
  61 
  62 int MethodBytecodePrinter::get_u16(Method* method, int* pos)  {
  63   uint16_t result = Bytes::get_Java_u2(method->constMethod()->code_base() + *pos);
  64   *pos += 2;
  65 
  66   return result;
  67 }
  68 
  69 int MethodBytecodePrinter::get_u16_native(Method* method, int* pos)  {
  70   uint16_t result = Bytes::get_native_u2(method->constMethod()->code_base() + *pos);
  71   *pos += 2;
  72 
  73   return result;
  74 }
  75 
  76 void MethodBytecodePrinter::print_branch_target(outputStream& stream, Method* method, int target_bci) {
  77   int line_nr = method->line_number_from_bci(target_bci);
  78 
  79   if (line_nr != -1) {
  80     stream.print(" -> %d (", line_nr);
  81   } else {
  82     stream.print(" -> ");
  83   }
  84 
  85   stream.print("0x%04x", target_bci);
  86 
  87   if (line_nr != -1) {
  88     stream.print(")");
  89   }
  90 }
  91 
  92 void MethodBytecodePrinter::print_branch_target_16(outputStream& stream, Method* method, int bci, int* pos) {
  93   int target_bci = bci + get_s16(method, pos);
  94   print_branch_target(stream, method, target_bci);
  95 }
  96 
  97 void MethodBytecodePrinter::print_branch_target_32(outputStream& stream, Method* method, int bci, int* pos) {
  98   int target_bci = bci + get_s32(method, pos);
  99   print_branch_target(stream, method, target_bci);
 100 }
 101 
 102 void MethodBytecodePrinter::print_cp_entry(outputStream& stream, Method* method, int cp_index) {
 103   ConstantPool* cp = method->constants();
 104   constantTag tag = cp->tag_at(cp_index);
 105 
 106   // Handle only that types which are possible here. Note that methods and
 107   // fields are handeled in print_native_cp_entry_16() instead.
 108   if (tag.is_klass() || tag.is_unresolved_klass()) {
 109     stream.print(" %s", cp->klass_name_at(cp_index)->as_C_string());
 110   } else if (tag.is_string()) {
 111     stream.print(" '%s'", cp->string_at_noresolve(cp_index));
 112   } else if (tag.is_int()) {
 113     stream.print(" #%d", cp->int_at(cp_index));
 114   } else if (tag.is_float()) {
 115     stream.print(" #%f", cp->float_at(cp_index));
 116   } else if (tag.is_long()) {
 117     stream.print(" #");
 118     stream.print_jlong(cp->long_at(cp_index));
 119   } else if (tag.is_double()) {
 120     stream.print(" #%f", cp->double_at(cp_index));
 121   } else if (tag.is_utf8()) {
 122     stream.print(" %s", cp->symbol_at(cp_index)->as_C_string());
 123   } else if (tag.is_string()) {
 124     stream.print(" \"%s\"", cp->string_at_noresolve(cp_index));
 125   } else {
 126     stream.print(" unknown cp type %d", tag.value());
 127   }
 128 }
 129 
 130 void MethodBytecodePrinter::print_cp_entry_8(outputStream& stream, Method* method, int* pos) {
 131   int cp_index = get_u8(method, pos);
 132   print_cp_entry(stream, method, cp_index);
 133 }
 134 
 135 void MethodBytecodePrinter::print_cp_entry_16(outputStream& stream, Method* method, int* pos) {
 136   int cp_index = get_u16(method, pos);
 137   print_cp_entry(stream, method, cp_index);
 138 }
 139 
 140 void MethodBytecodePrinter::print_obj_entry_8(outputStream& stream, Method* method, int* pos) {
 141   int cp_index = method->constants()->object_to_cp_index(get_u8(method, pos));
 142   print_cp_entry(stream, method, cp_index);
 143 }
 144 
 145 void MethodBytecodePrinter::print_obj_entry_16(outputStream& stream, Method* method, int* pos) {
 146   int cp_index = method->constants()->object_to_cp_index(get_u16_native(method, pos));
 147   print_cp_entry(stream, method, cp_index);
 148 }
 149 
 150 void MethodBytecodePrinter::print_native_cp_entry_16(outputStream& stream, Method* method, int* pos) {
 151   ConstantPool* cp = method->constants();
 152   int raw_index = get_u16_native(method, pos);
 153   int cp_index = cp->cache()->entry_at(raw_index)->constant_pool_index();
 154   int class_index = cp->uncached_klass_ref_index_at(cp_index);
 155   char *klass_name = cp->klass_name_at(class_index)->as_C_string();
 156   char *name = cp->uncached_name_ref_at(cp_index)->as_C_string();
 157   char *type = cp->uncached_signature_ref_at(cp_index)->as_C_string();
 158 
 159   if (type[0] == '(') {
 160     // Method
 161     stream.print(" %s.%s%s", klass_name, name, type);
 162   } else {
 163     // Field
 164     stream.print(" %s.%s", klass_name, name);
 165   }
 166 }
 167 
 168 void MethodBytecodePrinter::print_local_var(outputStream& stream, Method* method, int bci, int var_index) {
 169   ConstMethod* const_method = method->constMethod();
 170 
 171   if (const_method->has_localvariable_table()) {
 172     int len = const_method->localvariable_table_length();
 173 
 174     // Find the local variable entry for this slot and bci.
 175     for (int i = 0; i < len; ++i) {
 176       LocalVariableTableElement* elem = const_method->localvariable_table_start() + i;
 177 
 178       if ((elem->start_bci <= bci) && (elem->start_bci + elem->length > bci)) {
 179         if (elem->slot == var_index) {
 180           print_cp_entry(stream, method, elem->name_cp_index);
 181         }
 182       }
 183     }
 184   } else {
 185     stream.print(" <localVar #%d>", var_index);
 186   }
 187 }
 188 
 189 void MethodBytecodePrinter::print_local_var_8(outputStream& stream, Method* method, int bci, int* pos) {
 190   int var_index = get_u8(method, pos);
 191   print_local_var(stream, method, bci, var_index);
 192 }
 193 
 194 void MethodBytecodePrinter::print_local_var_16(outputStream& stream, Method* method, int bci, int* pos) {
 195   int var_index = get_u16(method, pos);
 196   print_local_var(stream, method, bci, var_index);
 197 }
 198 
 199 void MethodBytecodePrinter::print_signed_8(outputStream& stream, Method* method, int* pos) {
 200   int val = get_s8(method, pos);
 201   stream.print("%s%d", " #", val);
 202 }
 203 
 204 void MethodBytecodePrinter::print_signed_16(outputStream& stream, Method* method, int* pos) {
 205   int val = get_s16(method, pos);
 206   stream.print("%s%d", " #", val);
 207 }
 208 
 209 void MethodBytecodePrinter::print_unsigned_8(outputStream& stream, Method* method, int* pos) {
 210   int val = get_u8(method, pos);
 211   stream.print("%s%d", " #", val);
 212 }
 213 
 214 void MethodBytecodePrinter::print_unsigned_16(outputStream& stream, Method* method, int* pos) {
 215   int val = get_u16(method, pos);
 216   stream.print("%s%d", " #", val);
 217 }
 218 
 219 void print_stacks(Method* method);
 220 
 221 void MethodBytecodePrinter::print(outputStream& stream, Method* method, int start_bci, int end_bci,
 222                                   bool add_line_nr, bool add_bci, int marked_bci, char const* prefix) {
 223   assert(start_bci <= end_bci, "Invalid range");
 224 
 225   ConstMethod* const_method = method->constMethod();
 226   address base = method->code_base();
 227   int code_size  = const_method->code_size();
 228   int len;
 229   int last_line = -1;
 230 
 231   for (int bci = 0; bci < code_size; bci += len) {
 232     len = Bytecodes::java_length_at(method, base + bci);
 233 
 234     // Don't write bytecodes in front of the range.
 235     if (bci < start_bci) {
 236       continue;
 237     }
 238 
 239     // Don't write bytecodes after the range.
 240     if (bci >= end_bci) {
 241       return;
 242     }
 243 
 244     // If this is a wide prefix, remember that and get the real bytecode.
 245     bool is_wide = false;
 246 
 247     // Get the bytecode (and let java_code_at() handle breapoints).
 248     Bytecodes::Code code = Bytecodes::java_code_at(method, base + bci);
 249     Bytecodes::Code raw_code = Bytecodes::code_at(method, base + bci);
 250 
 251     if (code == Bytecodes::_wide) {
 252       is_wide = true;
 253       code = Bytecodes::java_code_at(method, base + bci + 1);
 254       raw_code = Bytecodes::code_at(method, base + bci + 1);
 255     }
 256 
 257     // Add prefix
 258     stream.print("%s", prefix);
 259 
 260     // Get the format specified and the name of that bytecode. For the
 261     // format we have to use the real code.
 262     char const* name = Bytecodes::name(code);
 263 
 264     // Add java line-nr if we have reached a new one.
 265     if (add_line_nr) {
 266       int line_nr = method->line_number_from_bci(bci);
 267 
 268       if (line_nr != -1) {
 269         if (last_line != line_nr) {
 270           last_line = line_nr;
 271           stream.print("%5d  ", line_nr);
 272         } else {
 273           stream.print("       ");
 274         }
 275       }
 276       else {
 277         stream.print("       ");
 278       }
 279     }
 280 
 281     // Print bci if needed.
 282     if (add_bci) {
 283       stream.print("0x%04x  ", bci);
 284     }
 285 
 286     // Print mark if this is the bytecode to be marked.
 287     if (bci == marked_bci) {
 288       stream.print("--> ");
 289     } else {
 290       stream.print("    ");
 291     }
 292 
 293     // Print the bytecode name.
 294     stream.print("%s", name);
 295 
 296     // Print the bytecode arguments.
 297     int pos = bci + 1;
 298 
 299     // If wide, get the format string after the first 'w'.
 300     if (is_wide) {
 301       pos += 1; // Skip 'wide' prefix.
 302     }
 303 
 304     int offset = add_bci ? 8 : 0;
 305     offset += add_line_nr ? 7 : 0;
 306     offset += 4; // Marker
 307 
 308     // Handle tableswitch differently.
 309     if (code == Bytecodes::_tableswitch) {
 310       // The index table is aligned to 4 bytes.
 311       pos = (pos + 3) & ~3;
 312 
 313       int default_target = bci + get_s32(method, &pos);
 314       int low = get_s32(method, &pos);
 315       int high = get_s32(method, &pos);
 316       int indent = offset + 4;
 317 
 318       stream.print("\n");
 319 
 320       for (int64_t i = low; i <= high; ++i) {
 321         int target = bci + get_s32(method, &pos);
 322 
 323         stream.print("%*scase " INT64_FORMAT ": goto", indent, "", i);
 324         print_branch_target(stream, method, target);
 325         stream.print("\n");
 326       }
 327 
 328       stream.print("%*sdefault: goto", indent, "");
 329       print_branch_target(stream, method, default_target);
 330       stream.print("\n");
 331 
 332       continue;
 333     }
 334 
 335     // Handle lookupswitch differently.
 336     if (code == Bytecodes::_lookupswitch) {
 337       // The index table is aligned to 4 bytes.
 338       pos = (pos + 3) & ~3;
 339 
 340       int default_target = bci + get_s32(method, &pos);
 341       int cases = get_s32(method, &pos);
 342       int indent = offset + 4;
 343 
 344       stream.print("\n");
 345 
 346       for (int i = 0; i < cases; ++i) {
 347         int value = get_s32(method, &pos);
 348         int target = bci + get_s32(method, &pos);
 349 
 350         stream.print("%*scase %d: goto", indent, "", value);
 351         print_branch_target(stream, method, target);
 352         stream.print("\n");
 353       }
 354 
 355       stream.print("%*sdefault: goto", indent, "");
 356       print_branch_target(stream, method, default_target);
 357       stream.print("\n");
 358 
 359       continue;
 360     }
 361     stream.print("\n");
 362   }
 363 }
 364 
 365 char const* MethodBytecodePrinter::get_bytecodes(Method* method, int start_bci, int end_bci,
 366                                                  bool add_line_nr, bool add_bci, int marked_bci,
 367                                                  char const* prefix) {
 368   stringStream stream;
 369   print(stream, method, start_bci, end_bci, add_line_nr, add_bci, marked_bci, prefix);
 370 
 371   return stream.as_string();
 372 }
 373 
 374 char const* MethodBytecodePrinter::get_klass_name(Method* method, int cp_index) {
 375   ConstantPool* cp = method->constants();
 376   int class_index = cp->klass_ref_index_at(cp_index);
 377   Symbol* clazz = cp->klass_at_noresolve(class_index);
 378   char* result = clazz->as_C_string();
 379   char* pos = result;
 380 
 381   while (*pos) {
 382     if (*pos == '/') {
 383       *pos = '.';
 384     }
 385 
 386     ++pos;
 387   }
 388 
 389   return result;
 390 }
 391 
 392 char const* MethodBytecodePrinter::get_method_name(Method* method, int cp_index) {
 393   char const* class_name = get_klass_name(method, cp_index);
 394   ConstantPool* cp = method->constants();
 395   int name_and_type_index = cp->name_and_type_ref_index_at(cp_index);
 396   int name_index = cp->name_ref_index_at(name_and_type_index);
 397   int type_index = cp->signature_ref_index_at(name_and_type_index);
 398   Symbol* name = cp->symbol_at(name_index);
 399   Symbol* signature = cp->symbol_at(type_index);
 400 
 401   stringStream ss;
 402   ss.print("%s%s%s%s%s%s", get_klass_name(method, cp_index), ".", name->as_C_string(),
 403            "(", signature->as_C_string(), ")");
 404   return ss.as_string();
 405 }
 406 
 407 char const* MethodBytecodePrinter::get_field_and_class(Method* method, int cp_index) {
 408   char const* class_name = get_klass_name(method, cp_index);
 409   ConstantPool* cp = method->constants();
 410   int name_and_type_index = cp->name_and_type_ref_index_at(cp_index);
 411   int name_index = cp->name_ref_index_at(name_and_type_index);
 412   Symbol* name = cp->symbol_at(name_index);
 413 
 414   stringStream ss;
 415   ss.print("%s%s%s", get_klass_name(method, cp_index), ".", name->as_C_string());
 416   return ss.as_string();
 417 }
 418 
 419 TrackingStackEntry::TrackingStackEntry(BasicType type) : _entry(INVALID + type * SCALE) { }
 420 
 421 TrackingStackEntry::TrackingStackEntry(int bci, BasicType type) : _entry(bci + type * SCALE) {
 422   assert(bci >= 0, "BCI must be >= 0");
 423   assert(bci < 65536, "BCI must be < 65536");
 424 }
 425 
 426 int TrackingStackEntry::get_bci() {
 427   return _entry % SCALE;
 428 }
 429 
 430 BasicType TrackingStackEntry::get_type() {
 431   return BasicType (_entry / SCALE);
 432 }
 433 
 434 TrackingStackEntry TrackingStackEntry::merge(TrackingStackEntry other) {
 435   if (get_type() != other.get_type()) {
 436     if (((get_type() == T_OBJECT) || (get_type() == T_ARRAY)) &&
 437         ((other.get_type() == T_OBJECT) || (other.get_type() == T_ARRAY))) {
 438       if (get_bci() == other.get_bci()) {
 439         return TrackingStackEntry(get_bci(), T_OBJECT);
 440       } else {
 441         return TrackingStackEntry(T_OBJECT);
 442       }
 443     } else {
 444       return TrackingStackEntry(T_CONFLICT);
 445     }
 446   }
 447 
 448   if (get_bci() == other.get_bci()) {
 449     return *this;
 450   } else {
 451     return TrackingStackEntry(get_type());
 452   }
 453 }
 454 
 455 
 456 TrackingStack::TrackingStack(const TrackingStack &copy) {
 457   for (int i = 0; i < copy.get_size(); i++) {
 458     push_raw(copy._stack.at(i));
 459   }
 460 }
 461 
 462 void TrackingStack::push_raw(TrackingStackEntry entry) {
 463   if (entry.get_type() == T_VOID) {
 464     return;
 465   }
 466 
 467   _stack.push(entry);
 468 }
 469 
 470 void TrackingStack::push(TrackingStackEntry entry) {
 471   if (type2size[entry.get_type()] == 2) {
 472     push_raw(entry);
 473     push_raw(entry);
 474   } else {
 475     push_raw(entry);
 476   }
 477 }
 478 
 479 void TrackingStack::push(int bci, BasicType type) {
 480   push(TrackingStackEntry(bci, type));
 481 }
 482 
 483 void TrackingStack::pop(int slots) {
 484   for (int i = 0; i < slots; ++i) {
 485     _stack.pop();
 486   }
 487 
 488   assert(get_size() >= 0, "Popped too many slots");
 489 }
 490 
 491 void TrackingStack::merge(TrackingStack const& other) {
 492   assert(get_size() == other.get_size(), "Stacks not of same size");
 493 
 494   for (int i = get_size() - 1; i >= 0; --i) {
 495     _stack.at(i).merge(other._stack.at(i));
 496   }
 497 }
 498 
 499 int TrackingStack::get_size() const {
 500   return _stack.length();
 501 }
 502 
 503 TrackingStackEntry TrackingStack::get_entry(int slot) {
 504   assert(slot >= 0, "Slot < 0");
 505   assert(slot < get_size(), "Slot >= size");
 506 
 507   return _stack.at(get_size() - slot - 1);
 508 }
 509 
 510 
 511 
 512 static TrackingStackSource createInvalidSource(int bci) {
 513   return TrackingStackSource(TrackingStackSource::INVALID, bci, "invalid");
 514 }
 515 
 516 static TrackingStackSource createLocalVarSource(int bci, Method* method, int slot) {
 517   // We assume outermost caller has ResourceMark.
 518   stringStream reason;
 519 
 520   if (method->has_localvariable_table()) {
 521     for (int i = 0; i < method->localvariable_table_length(); i++) {
 522       LocalVariableTableElement* elem = method->localvariable_table_start() + i;
 523       int start = elem->start_bci;
 524       int end = start + elem->length;
 525 
 526       if ((bci >= start) && (bci < end) && (elem->slot == slot)) {
 527         ConstantPool* cp = method->constants();
 528         reason.print("loaded from local variable '%s'",
 529                      cp->symbol_at(elem->name_cp_index)->as_C_string());
 530 
 531         return TrackingStackSource(TrackingStackSource::LOCAL_VAR, bci, reason.as_string());
 532       }
 533     }
 534   }
 535 
 536   // Handle at least some cases we know.
 537   if (!method->is_static() && (slot == 0)) {
 538     reason.print("loaded from local variable 'this'");
 539   } else {
 540     int curr = method->is_static() ? 0 : 1;
 541     SignatureStream ss(method->signature());
 542     int param_index = 0;
 543     bool found = false;
 544 
 545     for (SignatureStream ss(method->signature()); !ss.is_done(); ss.next()) {
 546       if (ss.at_return_type()) {
 547         continue;
 548       }
 549 
 550       int size = type2size[ss.type()];
 551 
 552       if ((slot >= curr) && (slot < curr + size)) {
 553         found = true;
 554         break;
 555       }
 556 
 557       param_index += 1;
 558       curr += size;
 559     }
 560 
 561     if (found) {
 562       if (param_index < 5) {
 563         static char const* s[] = {"first", "second", "third", "forth", "fifth"};
 564         reason.print("loaded from the %s parameter of the method", s[param_index]);
 565       } else {
 566         reason.print("loaded from the parameter nr. %d of the method", 1 + param_index);
 567       }
 568     } else {
 569       // This is the best we can do.
 570       reason.print("loaded from a local variable at slot %d", slot);
 571     }
 572   }
 573 
 574   return TrackingStackSource(TrackingStackSource::LOCAL_VAR, bci, reason.as_string());
 575 }
 576 
 577 static TrackingStackSource createMethodSource(int bci, Method* method, int cp_index) {
 578   // We assume outermost caller has ResourceMark.
 579   stringStream reason;
 580   reason.print("returned from %s", MethodBytecodePrinter::get_method_name(method, cp_index));
 581   return TrackingStackSource(TrackingStackSource::METHOD, bci, reason.as_string());
 582 }
 583 
 584 static TrackingStackSource createConstantSource(int bci) {
 585   return TrackingStackSource(TrackingStackSource::CONSTANT, bci, "loaded from a constant");
 586 }
 587 
 588 static TrackingStackSource createArraySource(int bci, TrackingStackSource const& array_source,
 589                                              TrackingStackSource const& index_source) {
 590   // We assume outermost caller has ResourceMark.
 591   stringStream reason;
 592 
 593   if (array_source.get_type() != TrackingStackSource::INVALID) {
 594     if (index_source.get_type() != TrackingStackSource::INVALID) {
 595       reason.print("loaded from an array (which itself was %s) with an index %s",
 596                    array_source.as_string(), index_source.as_string());
 597     } else {
 598       reason.print("loaded from an array (which itself was %s)", array_source.as_string());
 599     }
 600   } else {
 601     if (index_source.get_type() != TrackingStackSource::INVALID) {
 602       reason.print("loaded from an array with an index %s", index_source.as_string());
 603     } else {
 604       reason.print("loaded from an array");
 605     }
 606   }
 607 
 608   return TrackingStackSource(TrackingStackSource::ARRAY_ELEM, bci, reason.as_string());
 609 }
 610 
 611 static TrackingStackSource createFieldSource(int bci, Method* method, int cp_index,
 612                                              TrackingStackSource const& object_source) {
 613   // We assume outermost caller has ResourceMark.
 614   stringStream reason;
 615 
 616   if (object_source.get_type() != TrackingStackSource::INVALID) {
 617     reason.print("loaded from field %s of an object %s",
 618                  MethodBytecodePrinter::get_field_and_class(method, cp_index),
 619                  object_source.as_string());
 620   } else {
 621     reason.print("loaded from field %s of an object",
 622                  MethodBytecodePrinter::get_field_and_class(method, cp_index));
 623   }
 624 
 625   return TrackingStackSource(TrackingStackSource::FIELD_ELEM, bci, reason.as_string());
 626 }
 627 
 628 static TrackingStackSource createStaticFieldSource(int bci, Method* method, int cp_index) {
 629   // We assume outermost caller has ResourceMark.
 630   stringStream reason;
 631   reason.print("loaded from static field %s",
 632                MethodBytecodePrinter::get_field_and_class(method, cp_index));
 633 
 634   return TrackingStackSource(TrackingStackSource::FIELD_ELEM, bci, reason.as_string());
 635 }
 636 
 637 TrackingStackCreator::TrackingStackCreator(Method* method, int bci) : _method(method) {
 638   ConstMethod* const_method = method->constMethod();
 639 
 640   int len = const_method->code_size();
 641   _nr_of_entries = 0;
 642   _max_entries = 1000000;
 643   _stacks = new GrowableArray<TrackingStack*> (len+1);
 644 
 645   for (int i = 0; i <= len; ++i) {
 646     _stacks->push(NULL);
 647   }
 648 
 649   // Initialize stack a bci 0.
 650   _stacks->at_put(0, new TrackingStack());
 651 
 652   // And initialize for all exception handlers.
 653   if (const_method->has_exception_handler()) {
 654     ExceptionTableElement *et = const_method->exception_table_start();
 655     for (int i = 0; i < const_method->exception_table_length(); ++i) {
 656       u2 index = et[i].handler_pc;
 657 
 658       if (_stacks->at(index) == NULL) {
 659         _stacks->at_put(index, new TrackingStack());
 660         _stacks->at(index)->push(index, T_OBJECT);
 661       }
 662     }
 663   }
 664 
 665   _all_processed = false;
 666   _added_one = true;
 667 
 668   // Do this until each bytecode hash a stack or we haven't
 669   // added a new stack in one iteration.
 670   while (!_all_processed && _added_one) {
 671     _all_processed = true;
 672     _added_one = false;
 673 
 674     for (int i = 0; i < len; ) {
 675       i += do_instruction(i);
 676 
 677       // If we want the data only for a bci, we can possibly end early.
 678       if ((bci == i) && (_stacks->at(i) != NULL)) {
 679         _all_processed = true;
 680         break;
 681       }
 682 
 683       if (_nr_of_entries > _max_entries) {
 684         return;
 685       }
 686     }
 687   }
 688 }
 689 
 690 TrackingStackCreator::~TrackingStackCreator() {
 691   for (int i = 0; i < _stacks->length(); ++i) {
 692     delete _stacks->at(i);
 693   }
 694 }
 695 
 696 void TrackingStackCreator::merge(int bci, TrackingStack* stack) {
 697   assert(stack != _stacks->at(bci), "Cannot merge itself");
 698 
 699   if (_stacks->at(bci) != NULL) {
 700     stack->merge(*_stacks->at(bci));
 701   } else {
 702     // Got a new stack, so count the entries.
 703     _nr_of_entries += stack->get_size();
 704   }
 705 
 706   delete _stacks->at(bci);
 707   _stacks->at_put(bci, new TrackingStack(*stack));
 708 }
 709 
 710 int TrackingStackCreator::do_instruction(int bci) {
 711   ConstMethod* const_method = _method->constMethod();
 712   address code_base = _method->constMethod()->code_base();
 713 
 714   // We use the java code, since we don't want to cope with all the fast variants.
 715   int len = Bytecodes::java_length_at(_method, code_base + bci);
 716 
 717   // If we have no stack for this bci, we cannot process the bytecode now.
 718   if (_stacks->at(bci) == NULL) {
 719     _all_processed = false;
 720     return len;
 721   }
 722 
 723   TrackingStack* stack = new TrackingStack(*_stacks->at(bci));
 724 
 725   // dest_bci is != -1 if we branch.
 726   int dest_bci = -1;
 727 
 728   // This is for table and lookup switch.
 729   static const int initial_length = 2;
 730   GrowableArray<int> dests(initial_length);
 731 
 732   bool flow_ended = false;
 733 
 734   // Get the bytecode.
 735   bool is_wide = false;
 736   Bytecodes::Code raw_code = Bytecodes::code_at(_method, code_base + bci);
 737   Bytecodes::Code code = Bytecodes::java_code_at(_method, code_base + bci);
 738   int pos = bci + 1;
 739 
 740   if (code == Bytecodes::_wide) {
 741     is_wide = true;
 742     code = Bytecodes::java_code_at(_method, code_base + bci + 1);
 743     pos += 1;
 744   }
 745 
 746   // Now simulate the action of each bytecode.
 747   switch (code) {
 748     case Bytecodes::_nop:
 749     case Bytecodes::_aconst_null:
 750     case Bytecodes::_iconst_m1:
 751     case Bytecodes::_iconst_0:
 752     case Bytecodes::_iconst_1:
 753     case Bytecodes::_iconst_2:
 754     case Bytecodes::_iconst_3:
 755     case Bytecodes::_iconst_4:
 756     case Bytecodes::_iconst_5:
 757     case Bytecodes::_lconst_0:
 758     case Bytecodes::_lconst_1:
 759     case Bytecodes::_fconst_0:
 760     case Bytecodes::_fconst_1:
 761     case Bytecodes::_fconst_2:
 762     case Bytecodes::_dconst_0:
 763     case Bytecodes::_dconst_1:
 764     case Bytecodes::_bipush:
 765     case Bytecodes::_sipush:
 766     case Bytecodes::_iload:
 767     case Bytecodes::_lload:
 768     case Bytecodes::_fload:
 769     case Bytecodes::_dload:
 770     case Bytecodes::_aload:
 771     case Bytecodes::_iload_0:
 772     case Bytecodes::_iload_1:
 773     case Bytecodes::_iload_2:
 774     case Bytecodes::_iload_3:
 775     case Bytecodes::_lload_0:
 776     case Bytecodes::_lload_1:
 777     case Bytecodes::_lload_2:
 778     case Bytecodes::_lload_3:
 779     case Bytecodes::_fload_0:
 780     case Bytecodes::_fload_1:
 781     case Bytecodes::_fload_2:
 782     case Bytecodes::_fload_3:
 783     case Bytecodes::_dload_0:
 784     case Bytecodes::_dload_1:
 785     case Bytecodes::_dload_2:
 786     case Bytecodes::_dload_3:
 787     case Bytecodes::_aload_0:
 788     case Bytecodes::_aload_1:
 789     case Bytecodes::_aload_2:
 790     case Bytecodes::_aload_3:
 791     case Bytecodes::_iinc:
 792     case Bytecodes::_new:
 793       stack->push(bci, Bytecodes::result_type(code));
 794       break;
 795 
 796     case Bytecodes::_ldc:
 797     case Bytecodes::_ldc_w:
 798     case Bytecodes::_ldc2_w: {
 799       int cp_index;
 800       ConstantPool* cp = _method->constants();
 801 
 802       if (code == Bytecodes::_ldc) {
 803         cp_index = *(uint8_t*) (code_base + pos);
 804 
 805         if (raw_code == Bytecodes::_fast_aldc) {
 806           cp_index = cp->object_to_cp_index(cp_index);
 807         }
 808       } else {
 809         if (raw_code == Bytecodes::_fast_aldc_w) {
 810           cp_index = Bytes::get_native_u2(code_base + pos);
 811           cp_index = cp->object_to_cp_index(cp_index);
 812         }
 813         else {
 814           cp_index = Bytes::get_Java_u2(code_base + pos);
 815         }
 816       }
 817 
 818       constantTag tag = cp->tag_at(cp_index);
 819       if (tag.is_klass()  || tag.is_unresolved_klass() ||
 820           tag.is_method() || tag.is_interface_method() ||
 821           tag.is_field()  || tag.is_string()) {
 822         stack->push(bci, T_OBJECT);
 823       } else if (tag.is_int()) {
 824         stack->push(bci, T_INT);
 825       } else if (tag.is_long()) {
 826         stack->push(bci, T_LONG);
 827       } else if (tag.is_float()) {
 828         stack->push(bci, T_FLOAT);
 829       } else if (tag.is_double()) {
 830         stack->push(bci, T_DOUBLE);
 831       } else {
 832         assert(false, "Unexpected tag");
 833       }
 834       break;
 835     }
 836 
 837     case Bytecodes::_iaload:
 838     case Bytecodes::_faload:
 839     case Bytecodes::_aaload:
 840     case Bytecodes::_baload:
 841     case Bytecodes::_caload:
 842     case Bytecodes::_saload:
 843     case Bytecodes::_laload:
 844     case Bytecodes::_daload:
 845       stack->pop(2);
 846       stack->push(bci, Bytecodes::result_type(code));
 847       break;
 848 
 849     case Bytecodes::_istore:
 850     case Bytecodes::_lstore:
 851     case Bytecodes::_fstore:
 852     case Bytecodes::_dstore:
 853     case Bytecodes::_astore:
 854     case Bytecodes::_istore_0:
 855     case Bytecodes::_istore_1:
 856     case Bytecodes::_istore_2:
 857     case Bytecodes::_istore_3:
 858     case Bytecodes::_lstore_0:
 859     case Bytecodes::_lstore_1:
 860     case Bytecodes::_lstore_2:
 861     case Bytecodes::_lstore_3:
 862     case Bytecodes::_fstore_0:
 863     case Bytecodes::_fstore_1:
 864     case Bytecodes::_fstore_2:
 865     case Bytecodes::_fstore_3:
 866     case Bytecodes::_dstore_0:
 867     case Bytecodes::_dstore_1:
 868     case Bytecodes::_dstore_2:
 869     case Bytecodes::_dstore_3:
 870     case Bytecodes::_astore_0:
 871     case Bytecodes::_astore_1:
 872     case Bytecodes::_astore_2:
 873     case Bytecodes::_astore_3:
 874     case Bytecodes::_iastore:
 875     case Bytecodes::_lastore:
 876     case Bytecodes::_fastore:
 877     case Bytecodes::_dastore:
 878     case Bytecodes::_aastore:
 879     case Bytecodes::_bastore:
 880     case Bytecodes::_castore:
 881     case Bytecodes::_sastore:
 882     case Bytecodes::_pop:
 883     case Bytecodes::_pop2:
 884     case Bytecodes::_monitorenter:
 885     case Bytecodes::_monitorexit:
 886     case Bytecodes::_breakpoint:
 887       stack->pop(-Bytecodes::depth(code));
 888       break;
 889 
 890     case Bytecodes::_dup:
 891       stack->push_raw(stack->get_entry(0));
 892       break;
 893 
 894     case Bytecodes::_dup_x1: {
 895       TrackingStackEntry top1 = stack->get_entry(0);
 896       TrackingStackEntry top2 = stack->get_entry(1);
 897       stack->pop(2);
 898       stack->push_raw(top1);
 899       stack->push_raw(top2);
 900       stack->push_raw(top1);
 901       break;
 902     }
 903 
 904     case Bytecodes::_dup_x2: {
 905       TrackingStackEntry top1 = stack->get_entry(0);
 906       TrackingStackEntry top2 = stack->get_entry(1);
 907       TrackingStackEntry top3 = stack->get_entry(2);
 908       stack->pop(3);
 909       stack->push_raw(top1);
 910       stack->push_raw(top3);
 911       stack->push_raw(top2);
 912       stack->push_raw(top1);
 913       break;
 914     }
 915 
 916     case Bytecodes::_dup2:
 917       stack->push_raw(stack->get_entry(1));
 918       stack->push_raw(stack->get_entry(1));
 919       break;
 920 
 921     case Bytecodes::_dup2_x1: {
 922       TrackingStackEntry top1 = stack->get_entry(0);
 923       TrackingStackEntry top2 = stack->get_entry(1);
 924       TrackingStackEntry top3 = stack->get_entry(2);
 925       stack->pop(3);
 926       stack->push_raw(top2);
 927       stack->push_raw(top1);
 928       stack->push_raw(top3);
 929       stack->push_raw(top2);
 930       stack->push_raw(top1);
 931       break;
 932     }
 933 
 934     case Bytecodes::_dup2_x2: {
 935       TrackingStackEntry top1 = stack->get_entry(0);
 936       TrackingStackEntry top2 = stack->get_entry(1);
 937       TrackingStackEntry top3 = stack->get_entry(2);
 938       TrackingStackEntry top4 = stack->get_entry(3);
 939       stack->pop(4);
 940       stack->push_raw(top2);
 941       stack->push_raw(top1);
 942       stack->push_raw(top4);
 943       stack->push_raw(top3);
 944       stack->push_raw(top2);
 945       stack->push_raw(top1);
 946       break;
 947     }
 948 
 949     case Bytecodes::_swap: {
 950       TrackingStackEntry top1 = stack->get_entry(0);
 951       TrackingStackEntry top2 = stack->get_entry(1);
 952       stack->pop(2);
 953       stack->push(top1);
 954       stack->push(top2);
 955       break;
 956     }
 957 
 958     case Bytecodes::_iadd:
 959     case Bytecodes::_ladd:
 960     case Bytecodes::_fadd:
 961     case Bytecodes::_dadd:
 962     case Bytecodes::_isub:
 963     case Bytecodes::_lsub:
 964     case Bytecodes::_fsub:
 965     case Bytecodes::_dsub:
 966     case Bytecodes::_imul:
 967     case Bytecodes::_lmul:
 968     case Bytecodes::_fmul:
 969     case Bytecodes::_dmul:
 970     case Bytecodes::_idiv:
 971     case Bytecodes::_ldiv:
 972     case Bytecodes::_fdiv:
 973     case Bytecodes::_ddiv:
 974     case Bytecodes::_irem:
 975     case Bytecodes::_lrem:
 976     case Bytecodes::_frem:
 977     case Bytecodes::_drem:
 978     case Bytecodes::_iand:
 979     case Bytecodes::_land:
 980     case Bytecodes::_ior:
 981     case Bytecodes::_lor:
 982     case Bytecodes::_ixor:
 983     case Bytecodes::_lxor:
 984       stack->pop(2 * type2size[Bytecodes::result_type(code)]);
 985       stack->push(bci, Bytecodes::result_type(code));
 986       break;
 987 
 988     case Bytecodes::_ineg:
 989     case Bytecodes::_lneg:
 990     case Bytecodes::_fneg:
 991     case Bytecodes::_dneg:
 992       stack->pop(type2size[Bytecodes::result_type(code)]);
 993       stack->push(bci, Bytecodes::result_type(code));
 994       break;
 995 
 996     case Bytecodes::_ishl:
 997     case Bytecodes::_lshl:
 998     case Bytecodes::_ishr:
 999     case Bytecodes::_lshr:
1000     case Bytecodes::_iushr:
1001     case Bytecodes::_lushr:
1002       stack->pop(1 + type2size[Bytecodes::result_type(code)]);
1003       stack->push(bci, Bytecodes::result_type(code));
1004       break;
1005 
1006     case Bytecodes::_i2l:
1007     case Bytecodes::_i2f:
1008     case Bytecodes::_i2d:
1009     case Bytecodes::_f2i:
1010     case Bytecodes::_f2l:
1011     case Bytecodes::_f2d:
1012     case Bytecodes::_i2b:
1013     case Bytecodes::_i2c:
1014     case Bytecodes::_i2s:
1015       stack->pop(1);
1016       stack->push(bci, Bytecodes::result_type(code));
1017       break;
1018 
1019     case Bytecodes::_l2i:
1020     case Bytecodes::_l2f:
1021     case Bytecodes::_l2d:
1022     case Bytecodes::_d2i:
1023     case Bytecodes::_d2l:
1024     case Bytecodes::_d2f:
1025       stack->pop(2);
1026       stack->push(bci, Bytecodes::result_type(code));
1027       break;
1028 
1029     case Bytecodes::_lcmp:
1030     case Bytecodes::_fcmpl:
1031     case Bytecodes::_fcmpg:
1032     case Bytecodes::_dcmpl:
1033     case Bytecodes::_dcmpg:
1034       stack->pop(1 - Bytecodes::depth(code));
1035       stack->push(bci, T_INT);
1036       break;
1037 
1038     case Bytecodes::_ifeq:
1039     case Bytecodes::_ifne:
1040     case Bytecodes::_iflt:
1041     case Bytecodes::_ifge:
1042     case Bytecodes::_ifgt:
1043     case Bytecodes::_ifle:
1044     case Bytecodes::_if_icmpeq:
1045     case Bytecodes::_if_icmpne:
1046     case Bytecodes::_if_icmplt:
1047     case Bytecodes::_if_icmpge:
1048     case Bytecodes::_if_icmpgt:
1049     case Bytecodes::_if_icmple:
1050     case Bytecodes::_if_acmpeq:
1051     case Bytecodes::_if_acmpne:
1052     case Bytecodes::_ifnull:
1053     case Bytecodes::_ifnonnull:
1054       stack->pop(-Bytecodes::depth(code));
1055       dest_bci = bci + (int16_t) Bytes::get_Java_u2(code_base + pos);
1056       break;
1057 
1058     case Bytecodes::_jsr:
1059       // NOTE: Bytecodes has wrong depth for jsr.
1060       stack->push(bci, T_ADDRESS);
1061       dest_bci = bci + (int16_t) Bytes::get_Java_u2(code_base + pos);
1062       flow_ended = true;
1063       break;
1064 
1065     case Bytecodes::_jsr_w: {
1066       // NOTE: Bytecodes has wrong depth for jsr.
1067       stack->push(bci, T_ADDRESS);
1068       dest_bci = bci + (int32_t) Bytes::get_Java_u4(code_base + pos);
1069       flow_ended = true;
1070       break;
1071     }
1072 
1073     case Bytecodes::_ret:
1074       // We don't track local variables, so we cannot know were we
1075       // return. This makes the stacks imprecise, but we have to
1076       // live with that.
1077       flow_ended = true;
1078       break;
1079 
1080     case Bytecodes::_tableswitch: {
1081       stack->pop(1);
1082       pos = (pos + 3) & ~3;
1083       dest_bci = bci + (int32_t) Bytes::get_Java_u4(code_base + pos);
1084       int low = (int32_t) Bytes::get_Java_u4(code_base + pos + 4);
1085       int high = (int32_t) Bytes::get_Java_u4(code_base + pos + 8);
1086 
1087       for (int64_t i = low; i <= high; ++i) {
1088         dests.push(bci + (int32_t) Bytes::get_Java_u4(code_base + pos + 12 + 4 * (i - low)));
1089       }
1090 
1091       break;
1092     }
1093 
1094     case Bytecodes::_lookupswitch: {
1095       stack->pop(1);
1096       pos = (pos + 3) & ~3;
1097       dest_bci = bci + (int32_t) Bytes::get_Java_u4(code_base + pos);
1098       int nr_of_dests = (int32_t) Bytes::get_Java_u4(code_base + pos + 4);
1099 
1100       for (int i = 0; i < nr_of_dests; ++i) {
1101         dests.push(bci + (int32_t) Bytes::get_Java_u4(code_base + pos + 12 + 8 * i));
1102       }
1103 
1104       break;
1105     }
1106 
1107     case Bytecodes::_ireturn:
1108     case Bytecodes::_lreturn:
1109     case Bytecodes::_freturn:
1110     case Bytecodes::_dreturn:
1111     case Bytecodes::_areturn:
1112     case Bytecodes::_return:
1113     case Bytecodes::_athrow:
1114       stack->pop(-Bytecodes::depth(code));
1115       flow_ended = true;
1116       break;
1117 
1118     case Bytecodes::_getstatic:
1119     case Bytecodes::_getfield: {
1120       int cp_index = Bytes::get_native_u2(code_base + pos) DEBUG_ONLY(+ ConstantPool::CPCACHE_INDEX_TAG);
1121       ConstantPool* cp = _method->constants();
1122       int name_and_type_index = cp->name_and_type_ref_index_at(cp_index);
1123       int type_index = cp->signature_ref_index_at(name_and_type_index);
1124       Symbol* signature = cp->symbol_at(type_index);
1125       stack->pop(1 - Bytecodes::depth(code));
1126       stack->push(bci, char2type((char) signature->char_at(0)));
1127       break;
1128     }
1129 
1130     case Bytecodes::_putstatic:
1131     case Bytecodes::_putfield: {
1132       int cp_index = Bytes::get_native_u2(code_base + pos) DEBUG_ONLY(+ ConstantPool::CPCACHE_INDEX_TAG);
1133       ConstantPool* cp = _method->constants();
1134       int name_and_type_index = cp->name_and_type_ref_index_at(cp_index);
1135       int type_index = cp->signature_ref_index_at(name_and_type_index);
1136       Symbol* signature = cp->symbol_at(type_index);
1137       ResultTypeFinder result_type(signature);
1138       stack->pop(type2size[char2type((char) signature->char_at(0))] - Bytecodes::depth(code) - 1);
1139       break;
1140     }
1141 
1142     case Bytecodes::_invokevirtual:
1143     case Bytecodes::_invokespecial:
1144     case Bytecodes::_invokestatic:
1145     case Bytecodes::_invokeinterface:
1146     case Bytecodes::_invokedynamic: {
1147       ConstantPool* cp = _method->constants();
1148       int cp_index;
1149 
1150       if (code == Bytecodes::_invokedynamic) {
1151         cp_index = ((int) Bytes::get_native_u4(code_base + pos));
1152       } else {
1153         cp_index = Bytes::get_native_u2(code_base + pos) DEBUG_ONLY(+ ConstantPool::CPCACHE_INDEX_TAG);
1154       }
1155 
1156       int name_and_type_index = cp->name_and_type_ref_index_at(cp_index);
1157       int type_index = cp->signature_ref_index_at(name_and_type_index);
1158       Symbol* signature = cp->symbol_at(type_index);
1159 
1160       if ((code != Bytecodes::_invokestatic) && (code != Bytecodes::_invokedynamic)) {
1161         // Pop class.
1162         stack->pop(1);
1163       }
1164 
1165       stack->pop(ArgumentSizeComputer(signature).size());
1166       ResultTypeFinder result_type(signature);
1167       stack->push(bci, result_type.type());
1168       break;
1169     }
1170 
1171     case Bytecodes::_newarray:
1172     case Bytecodes::_anewarray:
1173     case Bytecodes::_instanceof:
1174       stack->pop(1);
1175       stack->push(bci, Bytecodes::result_type(code));
1176       break;
1177 
1178     case Bytecodes::_arraylength:
1179       // The return type of arraylength is wrong in the bytecodes table (T_VOID).
1180       stack->pop(1);
1181       stack->push(bci, T_INT);
1182       break;
1183 
1184     case Bytecodes::_checkcast:
1185       break;
1186 
1187     case Bytecodes::_multianewarray:
1188       stack->pop(*(uint8_t*) (code_base + pos + 2));
1189       stack->push(bci, T_OBJECT);
1190       break;
1191 
1192    case Bytecodes::_goto:
1193       stack->pop(-Bytecodes::depth(code));
1194       dest_bci = bci + (int16_t) Bytes::get_Java_u2(code_base + pos);
1195       flow_ended = true;
1196       break;
1197 
1198 
1199    case Bytecodes::_goto_w:
1200       stack->pop(-Bytecodes::depth(code));
1201       dest_bci = bci + (int32_t) Bytes::get_Java_u4(code_base + pos);
1202       flow_ended = true;
1203       break;
1204 
1205     default:
1206       // Allow at least the bcis which have stack info to work.
1207       _all_processed = false;
1208       _added_one = false;
1209       delete stack;
1210 
1211       return len;
1212   }
1213 
1214   // Put new stack to the next instruction, if we might reach if from
1215   // this bci.
1216   if (!flow_ended) {
1217     if (_stacks->at(bci + len) == NULL) {
1218       _added_one = true;
1219     }
1220 
1221     merge(bci + len, stack);
1222   }
1223 
1224   // Put the stack to the branch target too.
1225   if (dest_bci != -1) {
1226     if (_stacks->at(dest_bci) == NULL) {
1227       _added_one = true;
1228     }
1229 
1230     merge(dest_bci, stack);
1231   }
1232 
1233   // If we have more than one branch target, process these too.
1234   for (int64_t i = 0; i < dests.length(); ++i) {
1235     if (_stacks->at(dests.at(i)) == NULL) {
1236       _added_one = true;
1237     }
1238 
1239     merge(dests.at(i), stack);
1240   }
1241 
1242   delete stack;
1243 
1244   return len;
1245 }
1246 
1247 TrackingStackSource TrackingStackCreator::get_source(int bci, int slot, int max_detail) {
1248   assert(bci >= 0, "BCI too low");
1249   assert(bci < get_size(), "BCI to large");
1250 
1251   if (max_detail <= 0) {
1252     return createInvalidSource(bci);
1253   }
1254 
1255   if (_stacks->at(bci) == NULL) {
1256     return createInvalidSource(bci);
1257   }
1258 
1259   TrackingStack* stack = _stacks->at(bci);
1260   assert(slot >= 0, "Slot nr. too low");
1261   assert(slot < stack->get_size(), "Slot nr. too large");
1262 
1263   TrackingStackEntry entry = stack->get_entry(slot);
1264 
1265   if (!entry.has_bci()) {
1266     return createInvalidSource(bci);
1267   }
1268 
1269   // Get the bytecode.
1270   int source_bci = entry.get_bci();
1271   address code_base = _method->constMethod()->code_base();
1272   Bytecodes::Code code = Bytecodes::java_code_at(_method, code_base + source_bci);
1273   bool is_wide = false;
1274   int pos = source_bci + 1;
1275 
1276   if (code == Bytecodes::_wide) {
1277     is_wide = true;
1278     code = Bytecodes::java_code_at(_method, code_base + source_bci + 1);
1279     pos += 1;
1280   }
1281 
1282   switch (code) {
1283     case Bytecodes::_iload:
1284     case Bytecodes::_lload:
1285     case Bytecodes::_fload:
1286     case Bytecodes::_dload:
1287     case Bytecodes::_aload: {
1288       int index;
1289 
1290       if (is_wide) {
1291         index = Bytes::get_Java_u2(code_base + source_bci + 2);
1292       } else {
1293         index = *(uint8_t*) (code_base + source_bci + 1);
1294       }
1295 
1296       return createLocalVarSource(source_bci, _method, index);
1297     }
1298 
1299     case Bytecodes::_iload_0:
1300     case Bytecodes::_lload_0:
1301     case Bytecodes::_fload_0:
1302     case Bytecodes::_dload_0:
1303     case Bytecodes::_aload_0:
1304       return createLocalVarSource(source_bci, _method, 0);
1305 
1306     case Bytecodes::_iload_1:
1307     case Bytecodes::_lload_1:
1308     case Bytecodes::_fload_1:
1309     case Bytecodes::_dload_1:
1310     case Bytecodes::_aload_1:
1311       return createLocalVarSource(source_bci, _method, 1);
1312 
1313     case Bytecodes::_iload_2:
1314     case Bytecodes::_lload_2:
1315     case Bytecodes::_fload_2:
1316     case Bytecodes::_dload_2:
1317     case Bytecodes::_aload_2:
1318       return createLocalVarSource(source_bci, _method, 2);
1319 
1320     case Bytecodes::_lload_3:
1321     case Bytecodes::_iload_3:
1322     case Bytecodes::_fload_3:
1323     case Bytecodes::_dload_3:
1324     case Bytecodes::_aload_3:
1325       return createLocalVarSource(source_bci, _method, 3);
1326 
1327     case Bytecodes::_aconst_null:
1328     case Bytecodes::_iconst_m1:
1329     case Bytecodes::_iconst_0:
1330     case Bytecodes::_iconst_1:
1331     case Bytecodes::_iconst_2:
1332     case Bytecodes::_iconst_3:
1333     case Bytecodes::_iconst_4:
1334     case Bytecodes::_iconst_5:
1335     case Bytecodes::_lconst_0:
1336     case Bytecodes::_lconst_1:
1337     case Bytecodes::_fconst_0:
1338     case Bytecodes::_fconst_1:
1339     case Bytecodes::_fconst_2:
1340     case Bytecodes::_dconst_0:
1341     case Bytecodes::_dconst_1:
1342     case Bytecodes::_bipush:
1343     case Bytecodes::_sipush:
1344       return createConstantSource(source_bci);
1345 
1346     case Bytecodes::_iaload:
1347     case Bytecodes::_faload:
1348     case Bytecodes::_aaload:
1349     case Bytecodes::_baload:
1350     case Bytecodes::_caload:
1351     case Bytecodes::_saload:
1352     case Bytecodes::_laload:
1353     case Bytecodes::_daload: {
1354       TrackingStackSource array_source = get_source(source_bci, 1, max_detail - 1);
1355       TrackingStackSource index_source = get_source(source_bci, 0, max_detail - 1);
1356       return createArraySource(source_bci, array_source, index_source);
1357     }
1358 
1359     case Bytecodes::_invokevirtual:
1360     case Bytecodes::_invokespecial:
1361     case Bytecodes::_invokestatic:
1362     case Bytecodes::_invokeinterface: {
1363         int cp_index = Bytes::get_native_u2(code_base + pos) DEBUG_ONLY(+ ConstantPool::CPCACHE_INDEX_TAG);
1364         return createMethodSource(source_bci, _method, cp_index);
1365     }
1366 
1367     case Bytecodes::_getstatic:
1368       return createStaticFieldSource(source_bci, _method,
1369                                      Bytes::get_native_u2(code_base + pos) + ConstantPool::CPCACHE_INDEX_TAG);
1370 
1371     case Bytecodes::_getfield: {
1372       int cp_index = Bytes::get_native_u2(code_base + pos) + ConstantPool::CPCACHE_INDEX_TAG;
1373       TrackingStackSource object_source = get_source(source_bci, 0, max_detail - 1);
1374       return createFieldSource(source_bci, _method, cp_index, object_source);
1375     }
1376 
1377     default:
1378       return createInvalidSource(bci);
1379   }
1380 }
1381 
1382 int TrackingStackCreator::get_null_pointer_slot(int bci, char const** reason) {
1383   // If this NPE was created via reflection, we have no real NPE.
1384   if (_method->method_holder() == SystemDictionary::reflect_NativeConstructorAccessorImpl_klass()) {
1385     return -2;
1386   }
1387 
1388   // Get the bytecode.
1389   address code_base = _method->constMethod()->code_base();
1390   Bytecodes::Code code = Bytecodes::java_code_at(_method, code_base + bci);
1391   int pos = bci + 1;
1392 
1393   if (code == Bytecodes::_wide) {
1394     code = Bytecodes::java_code_at(_method, code_base + bci + 1);
1395     pos += 1;
1396   }
1397 
1398   int result = -1;
1399 
1400   switch (code) {
1401     case Bytecodes::_iaload:
1402       if (reason != NULL) {
1403         *reason = "while trying to load from a null int array";
1404       }
1405 
1406       result = 1;
1407       break;
1408 
1409     case Bytecodes::_faload:
1410       if (reason != NULL) {
1411         *reason = "while trying to load from a null float array";
1412       }
1413 
1414       result = 1;
1415       break;
1416 
1417     case Bytecodes::_aaload:
1418       if (reason != NULL) {
1419         *reason = "while trying to load from a null object array";
1420       }
1421 
1422       result = 1;
1423       break;
1424 
1425     case Bytecodes::_baload:
1426       if (reason != NULL) {
1427         *reason = "while trying to load from a null byte (or boolean) array";
1428       }
1429 
1430       result = 1;
1431       break;
1432 
1433     case Bytecodes::_caload:
1434       if (reason != NULL) {
1435         *reason = "while trying to load from a null char array";
1436       }
1437 
1438       result = 1;
1439       break;
1440 
1441     case Bytecodes::_saload:
1442       if (reason != NULL) {
1443         *reason = "while trying to load from a null short array";
1444       }
1445 
1446       result = 1;
1447       break;
1448 
1449     case Bytecodes::_laload:
1450       if (reason != NULL) {
1451         *reason = "while trying to load from a null long array";
1452       }
1453 
1454       result = 1;
1455       break;
1456 
1457     case Bytecodes::_daload:
1458       if (reason != NULL) {
1459         *reason = "while trying to load from a null double array";
1460       }
1461 
1462       result = 1;
1463       break;
1464 
1465     case Bytecodes::_iastore:
1466       if (reason != NULL) {
1467         *reason = "while trying to store to a null int array";
1468       }
1469 
1470       result = 2;
1471       break;
1472 
1473     case Bytecodes::_lastore:
1474       if (reason != NULL) {
1475         *reason = "while trying to store to a null long array";
1476       }
1477 
1478       result = 3;
1479       break;
1480 
1481     case Bytecodes::_fastore:
1482       if (reason != NULL) {
1483         *reason = "while trying to store to a null float array";
1484       }
1485 
1486       result = 2;
1487       break;
1488 
1489     case Bytecodes::_dastore:
1490       if (reason != NULL) {
1491         *reason = "while trying to store to a null double array";
1492       }
1493 
1494       result = 3;
1495       break;
1496 
1497     case Bytecodes::_aastore:
1498       if (reason != NULL) {
1499         *reason = "while trying to store to a null object array";
1500       }
1501 
1502       result = 2;
1503       break;
1504 
1505     case Bytecodes::_bastore:
1506       if (reason != NULL) {
1507         *reason = "while trying to store to a null byte (or boolean) array";
1508       }
1509 
1510       result = 2;
1511       break;
1512 
1513     case Bytecodes::_castore:
1514       if (reason != NULL) {
1515         *reason = "while trying to store to a null char array";
1516       }
1517 
1518       result = 2;
1519       break;
1520 
1521     case Bytecodes::_sastore:
1522       if (reason != NULL) {
1523         *reason = "while trying to store to a null short array";
1524       }
1525 
1526       result = 2;
1527       break;
1528 
1529     case Bytecodes::_getfield:
1530       {
1531         if (reason != NULL) {
1532           int cp_index = Bytes::get_native_u2(code_base + pos) DEBUG_ONLY(+ ConstantPool::CPCACHE_INDEX_TAG);
1533           ConstantPool* cp = _method->constants();
1534           int name_and_type_index = cp->name_and_type_ref_index_at(cp_index);
1535           int name_index = cp->name_ref_index_at(name_and_type_index);
1536           Symbol* name = cp->symbol_at(name_index);
1537           stringStream ss;
1538           ss.print("while trying to read the field '%s' of a null object", name->as_C_string());
1539           *reason = ss.as_string();
1540         }
1541 
1542         result = 0;
1543       }
1544 
1545       break;
1546 
1547     case Bytecodes::_arraylength:
1548       if (reason != NULL) {
1549         *reason = "while trying to get the length of a null array";
1550       }
1551 
1552       result = 0;
1553       break;
1554 
1555     case Bytecodes::_athrow:
1556       if (reason != NULL) {
1557         *reason = "while trying to throw a null exception object";
1558       }
1559 
1560       result = 0;
1561       break;
1562 
1563     case Bytecodes::_monitorenter:
1564       if (reason != NULL) {
1565         *reason = "while trying to enter a null monitor";
1566       }
1567 
1568       result = 0;
1569       break;
1570 
1571     case Bytecodes::_monitorexit:
1572       if (reason != NULL) {
1573         *reason = "while trying to exit a null monitor";
1574       }
1575 
1576       result = 0;
1577       break;
1578 
1579     case Bytecodes::_putfield:
1580       {
1581         int cp_index = Bytes::get_native_u2(code_base + pos) DEBUG_ONLY(+ ConstantPool::CPCACHE_INDEX_TAG);
1582         ConstantPool* cp = _method->constants();
1583         int name_and_type_index = cp->name_and_type_ref_index_at(cp_index);
1584         int type_index = cp->signature_ref_index_at(name_and_type_index);
1585         Symbol* signature = cp->symbol_at(type_index);
1586 
1587         if (reason != NULL) {
1588           stringStream ss;
1589           ss.print("while trying to write the field %s of a null object",
1590                    MethodBytecodePrinter::get_field_and_class(_method, cp_index));
1591           *reason = ss.as_string();
1592         }
1593 
1594         result = type2size[char2type((char) signature->char_at(0))];
1595       }
1596 
1597       break;
1598 
1599     case Bytecodes::_invokevirtual:
1600     case Bytecodes::_invokespecial:
1601     case Bytecodes::_invokeinterface:
1602       {
1603         int cp_index = Bytes::get_native_u2(code_base+ pos) DEBUG_ONLY(+ ConstantPool::CPCACHE_INDEX_TAG);
1604         ConstantPool* cp = _method->constants();
1605         int name_and_type_index = cp->name_and_type_ref_index_at(cp_index);
1606         int name_index = cp->name_ref_index_at(name_and_type_index);
1607         int type_index = cp->signature_ref_index_at(name_and_type_index);
1608         Symbol* name = cp->symbol_at(name_index);
1609         Symbol* signature = cp->symbol_at(type_index);
1610 
1611         // Assume the the call of a constructor can never cause a NullPointerException
1612         // (which is true in Java). This is mainly used to avoid generating wrong
1613         // messages for NullPointerExceptions created explicitely by new in Java code.
1614         if (name != vmSymbols::object_initializer_name()) {
1615           if (reason != NULL) {
1616             stringStream ss;
1617             ss.print("while trying to invoke the method %s on a null reference",
1618                      MethodBytecodePrinter::get_method_name(_method, cp_index));
1619             *reason = ss.as_string();
1620           }
1621 
1622           result = ArgumentSizeComputer(signature).size();
1623         }
1624         else {
1625           result = -2;
1626         }
1627       }
1628 
1629       break;
1630 
1631     default:
1632       break;
1633   }
1634 
1635   return result;
1636 }
1637