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         char *var =  cp->symbol_at(elem->name_cp_index)->as_C_string();
 529         if (strlen(var) == 4 && strcmp(var, "this") == 0) {
 530           reason.print("loaded from 'this'");
 531         } else {
 532           reason.print("loaded from local variable '%s'", var);
 533         }
 534 
 535         return TrackingStackSource(TrackingStackSource::LOCAL_VAR, bci, reason.as_string());
 536       }
 537     }
 538   }
 539 
 540   // Handle at least some cases we know.
 541   if (!method->is_static() && (slot == 0)) {
 542     reason.print("loaded from 'this'");
 543   } else {
 544     int curr = method->is_static() ? 0 : 1;
 545     SignatureStream ss(method->signature());
 546     int param_index = 0;
 547     bool found = false;
 548 
 549     for (SignatureStream ss(method->signature()); !ss.is_done(); ss.next()) {
 550       if (ss.at_return_type()) {
 551         continue;
 552       }
 553 
 554       int size = type2size[ss.type()];
 555 
 556       if ((slot >= curr) && (slot < curr + size)) {
 557         found = true;
 558         break;
 559       }
 560 
 561       param_index += 1;
 562       curr += size;
 563     }
 564 
 565     if (found) {
 566       reason.print("loaded from the parameter nr. %d of the method", 1 + param_index);
 567     } else {
 568       // This is the best we can do.
 569       reason.print("loaded from a local variable at slot %d", slot);
 570     }
 571   }
 572 
 573   return TrackingStackSource(TrackingStackSource::LOCAL_VAR, bci, reason.as_string());
 574 }
 575 
 576 static TrackingStackSource createMethodSource(int bci, Method* method, int cp_index) {
 577   // We assume outermost caller has ResourceMark.
 578   stringStream reason;
 579   reason.print("returned from '%s'", MethodBytecodePrinter::get_method_name(method, cp_index));
 580   return TrackingStackSource(TrackingStackSource::METHOD, bci, reason.as_string());
 581 }
 582 
 583 static TrackingStackSource createConstantSource(int bci) {
 584   return TrackingStackSource(TrackingStackSource::CONSTANT, bci, "loaded from a constant");
 585 }
 586 
 587 static TrackingStackSource createArraySource(int bci, TrackingStackSource const& array_source,
 588                                              TrackingStackSource const& index_source) {
 589   // We assume outermost caller has ResourceMark.
 590   stringStream reason;
 591 
 592   if (array_source.get_type() != TrackingStackSource::INVALID) {
 593     if (index_source.get_type() != TrackingStackSource::INVALID) {
 594       reason.print("loaded from an array (which itself was %s) with an index %s",
 595                    array_source.as_string(), index_source.as_string());
 596     } else {
 597       reason.print("loaded from an array (which itself was %s)", array_source.as_string());
 598     }
 599   } else {
 600     if (index_source.get_type() != TrackingStackSource::INVALID) {
 601       reason.print("loaded from an array with an index %s", index_source.as_string());
 602     } else {
 603       reason.print("loaded from an array");
 604     }
 605   }
 606 
 607   return TrackingStackSource(TrackingStackSource::ARRAY_ELEM, bci, reason.as_string());
 608 }
 609 
 610 static TrackingStackSource createFieldSource(int bci, Method* method, int cp_index,
 611                                              TrackingStackSource const& object_source) {
 612   // We assume outermost caller has ResourceMark.
 613   stringStream reason;
 614 
 615   if (object_source.get_type() != TrackingStackSource::INVALID) {
 616     reason.print("loaded from field '%s' of an object %s",
 617                  MethodBytecodePrinter::get_field_and_class(method, cp_index),
 618                  object_source.as_string());
 619   } else {
 620     reason.print("loaded from field '%s' of an object",
 621                  MethodBytecodePrinter::get_field_and_class(method, cp_index));
 622   }
 623 
 624   return TrackingStackSource(TrackingStackSource::FIELD_ELEM, bci, reason.as_string());
 625 }
 626 
 627 static TrackingStackSource createStaticFieldSource(int bci, Method* method, int cp_index) {
 628   // We assume outermost caller has ResourceMark.
 629   stringStream reason;
 630   reason.print("loaded from static field '%s'",
 631                MethodBytecodePrinter::get_field_and_class(method, cp_index));
 632 
 633   return TrackingStackSource(TrackingStackSource::FIELD_ELEM, bci, reason.as_string());
 634 }
 635 
 636 TrackingStackCreator::TrackingStackCreator(Method* method, int bci) : _method(method) {
 637   ConstMethod* const_method = method->constMethod();
 638 
 639   int len = const_method->code_size();
 640   _nr_of_entries = 0;
 641   _max_entries = 1000000;
 642   _stacks = new GrowableArray<TrackingStack*> (len+1);
 643 
 644   for (int i = 0; i <= len; ++i) {
 645     _stacks->push(NULL);
 646   }
 647 
 648   // Initialize stack a bci 0.
 649   _stacks->at_put(0, new TrackingStack());
 650 
 651   // And initialize for all exception handlers.
 652   if (const_method->has_exception_handler()) {
 653     ExceptionTableElement *et = const_method->exception_table_start();
 654     for (int i = 0; i < const_method->exception_table_length(); ++i) {
 655       u2 index = et[i].handler_pc;
 656 
 657       if (_stacks->at(index) == NULL) {
 658         _stacks->at_put(index, new TrackingStack());
 659         _stacks->at(index)->push(index, T_OBJECT);
 660       }
 661     }
 662   }
 663 
 664   _all_processed = false;
 665   _added_one = true;
 666 
 667   // Do this until each bytecode hash a stack or we haven't
 668   // added a new stack in one iteration.
 669   while (!_all_processed && _added_one) {
 670     _all_processed = true;
 671     _added_one = false;
 672 
 673     for (int i = 0; i < len; ) {
 674       i += do_instruction(i);
 675 
 676       // If we want the data only for a bci, we can possibly end early.
 677       if ((bci == i) && (_stacks->at(i) != NULL)) {
 678         _all_processed = true;
 679         break;
 680       }
 681 
 682       if (_nr_of_entries > _max_entries) {
 683         return;
 684       }
 685     }
 686   }
 687 }
 688 
 689 TrackingStackCreator::~TrackingStackCreator() {
 690   for (int i = 0; i < _stacks->length(); ++i) {
 691     delete _stacks->at(i);
 692   }
 693 }
 694 
 695 void TrackingStackCreator::merge(int bci, TrackingStack* stack) {
 696   assert(stack != _stacks->at(bci), "Cannot merge itself");
 697 
 698   if (_stacks->at(bci) != NULL) {
 699     stack->merge(*_stacks->at(bci));
 700   } else {
 701     // Got a new stack, so count the entries.
 702     _nr_of_entries += stack->get_size();
 703   }
 704 
 705   delete _stacks->at(bci);
 706   _stacks->at_put(bci, new TrackingStack(*stack));
 707 }
 708 
 709 int TrackingStackCreator::do_instruction(int bci) {
 710   ConstMethod* const_method = _method->constMethod();
 711   address code_base = _method->constMethod()->code_base();
 712 
 713   // We use the java code, since we don't want to cope with all the fast variants.
 714   int len = Bytecodes::java_length_at(_method, code_base + bci);
 715 
 716   // If we have no stack for this bci, we cannot process the bytecode now.
 717   if (_stacks->at(bci) == NULL) {
 718     _all_processed = false;
 719     return len;
 720   }
 721 
 722   TrackingStack* stack = new TrackingStack(*_stacks->at(bci));
 723 
 724   // dest_bci is != -1 if we branch.
 725   int dest_bci = -1;
 726 
 727   // This is for table and lookup switch.
 728   static const int initial_length = 2;
 729   GrowableArray<int> dests(initial_length);
 730 
 731   bool flow_ended = false;
 732 
 733   // Get the bytecode.
 734   bool is_wide = false;
 735   Bytecodes::Code raw_code = Bytecodes::code_at(_method, code_base + bci);
 736   Bytecodes::Code code = Bytecodes::java_code_at(_method, code_base + bci);
 737   int pos = bci + 1;
 738 
 739   if (code == Bytecodes::_wide) {
 740     is_wide = true;
 741     code = Bytecodes::java_code_at(_method, code_base + bci + 1);
 742     pos += 1;
 743   }
 744 
 745   // Now simulate the action of each bytecode.
 746   switch (code) {
 747     case Bytecodes::_nop:
 748     case Bytecodes::_aconst_null:
 749     case Bytecodes::_iconst_m1:
 750     case Bytecodes::_iconst_0:
 751     case Bytecodes::_iconst_1:
 752     case Bytecodes::_iconst_2:
 753     case Bytecodes::_iconst_3:
 754     case Bytecodes::_iconst_4:
 755     case Bytecodes::_iconst_5:
 756     case Bytecodes::_lconst_0:
 757     case Bytecodes::_lconst_1:
 758     case Bytecodes::_fconst_0:
 759     case Bytecodes::_fconst_1:
 760     case Bytecodes::_fconst_2:
 761     case Bytecodes::_dconst_0:
 762     case Bytecodes::_dconst_1:
 763     case Bytecodes::_bipush:
 764     case Bytecodes::_sipush:
 765     case Bytecodes::_iload:
 766     case Bytecodes::_lload:
 767     case Bytecodes::_fload:
 768     case Bytecodes::_dload:
 769     case Bytecodes::_aload:
 770     case Bytecodes::_iload_0:
 771     case Bytecodes::_iload_1:
 772     case Bytecodes::_iload_2:
 773     case Bytecodes::_iload_3:
 774     case Bytecodes::_lload_0:
 775     case Bytecodes::_lload_1:
 776     case Bytecodes::_lload_2:
 777     case Bytecodes::_lload_3:
 778     case Bytecodes::_fload_0:
 779     case Bytecodes::_fload_1:
 780     case Bytecodes::_fload_2:
 781     case Bytecodes::_fload_3:
 782     case Bytecodes::_dload_0:
 783     case Bytecodes::_dload_1:
 784     case Bytecodes::_dload_2:
 785     case Bytecodes::_dload_3:
 786     case Bytecodes::_aload_0:
 787     case Bytecodes::_aload_1:
 788     case Bytecodes::_aload_2:
 789     case Bytecodes::_aload_3:
 790     case Bytecodes::_iinc:
 791     case Bytecodes::_new:
 792       stack->push(bci, Bytecodes::result_type(code));
 793       break;
 794 
 795     case Bytecodes::_ldc:
 796     case Bytecodes::_ldc_w:
 797     case Bytecodes::_ldc2_w: {
 798       int cp_index;
 799       ConstantPool* cp = _method->constants();
 800 
 801       if (code == Bytecodes::_ldc) {
 802         cp_index = *(uint8_t*) (code_base + pos);
 803 
 804         if (raw_code == Bytecodes::_fast_aldc) {
 805           cp_index = cp->object_to_cp_index(cp_index);
 806         }
 807       } else {
 808         if (raw_code == Bytecodes::_fast_aldc_w) {
 809           cp_index = Bytes::get_native_u2(code_base + pos);
 810           cp_index = cp->object_to_cp_index(cp_index);
 811         }
 812         else {
 813           cp_index = Bytes::get_Java_u2(code_base + pos);
 814         }
 815       }
 816 
 817       constantTag tag = cp->tag_at(cp_index);
 818       if (tag.is_klass()  || tag.is_unresolved_klass() ||
 819           tag.is_method() || tag.is_interface_method() ||
 820           tag.is_field()  || tag.is_string()) {
 821         stack->push(bci, T_OBJECT);
 822       } else if (tag.is_int()) {
 823         stack->push(bci, T_INT);
 824       } else if (tag.is_long()) {
 825         stack->push(bci, T_LONG);
 826       } else if (tag.is_float()) {
 827         stack->push(bci, T_FLOAT);
 828       } else if (tag.is_double()) {
 829         stack->push(bci, T_DOUBLE);
 830       } else {
 831         assert(false, "Unexpected tag");
 832       }
 833       break;
 834     }
 835 
 836     case Bytecodes::_iaload:
 837     case Bytecodes::_faload:
 838     case Bytecodes::_aaload:
 839     case Bytecodes::_baload:
 840     case Bytecodes::_caload:
 841     case Bytecodes::_saload:
 842     case Bytecodes::_laload:
 843     case Bytecodes::_daload:
 844       stack->pop(2);
 845       stack->push(bci, Bytecodes::result_type(code));
 846       break;
 847 
 848     case Bytecodes::_istore:
 849     case Bytecodes::_lstore:
 850     case Bytecodes::_fstore:
 851     case Bytecodes::_dstore:
 852     case Bytecodes::_astore:
 853     case Bytecodes::_istore_0:
 854     case Bytecodes::_istore_1:
 855     case Bytecodes::_istore_2:
 856     case Bytecodes::_istore_3:
 857     case Bytecodes::_lstore_0:
 858     case Bytecodes::_lstore_1:
 859     case Bytecodes::_lstore_2:
 860     case Bytecodes::_lstore_3:
 861     case Bytecodes::_fstore_0:
 862     case Bytecodes::_fstore_1:
 863     case Bytecodes::_fstore_2:
 864     case Bytecodes::_fstore_3:
 865     case Bytecodes::_dstore_0:
 866     case Bytecodes::_dstore_1:
 867     case Bytecodes::_dstore_2:
 868     case Bytecodes::_dstore_3:
 869     case Bytecodes::_astore_0:
 870     case Bytecodes::_astore_1:
 871     case Bytecodes::_astore_2:
 872     case Bytecodes::_astore_3:
 873     case Bytecodes::_iastore:
 874     case Bytecodes::_lastore:
 875     case Bytecodes::_fastore:
 876     case Bytecodes::_dastore:
 877     case Bytecodes::_aastore:
 878     case Bytecodes::_bastore:
 879     case Bytecodes::_castore:
 880     case Bytecodes::_sastore:
 881     case Bytecodes::_pop:
 882     case Bytecodes::_pop2:
 883     case Bytecodes::_monitorenter:
 884     case Bytecodes::_monitorexit:
 885     case Bytecodes::_breakpoint:
 886       stack->pop(-Bytecodes::depth(code));
 887       break;
 888 
 889     case Bytecodes::_dup:
 890       stack->push_raw(stack->get_entry(0));
 891       break;
 892 
 893     case Bytecodes::_dup_x1: {
 894       TrackingStackEntry top1 = stack->get_entry(0);
 895       TrackingStackEntry top2 = stack->get_entry(1);
 896       stack->pop(2);
 897       stack->push_raw(top1);
 898       stack->push_raw(top2);
 899       stack->push_raw(top1);
 900       break;
 901     }
 902 
 903     case Bytecodes::_dup_x2: {
 904       TrackingStackEntry top1 = stack->get_entry(0);
 905       TrackingStackEntry top2 = stack->get_entry(1);
 906       TrackingStackEntry top3 = stack->get_entry(2);
 907       stack->pop(3);
 908       stack->push_raw(top1);
 909       stack->push_raw(top3);
 910       stack->push_raw(top2);
 911       stack->push_raw(top1);
 912       break;
 913     }
 914 
 915     case Bytecodes::_dup2:
 916       stack->push_raw(stack->get_entry(1));
 917       stack->push_raw(stack->get_entry(1));
 918       break;
 919 
 920     case Bytecodes::_dup2_x1: {
 921       TrackingStackEntry top1 = stack->get_entry(0);
 922       TrackingStackEntry top2 = stack->get_entry(1);
 923       TrackingStackEntry top3 = stack->get_entry(2);
 924       stack->pop(3);
 925       stack->push_raw(top2);
 926       stack->push_raw(top1);
 927       stack->push_raw(top3);
 928       stack->push_raw(top2);
 929       stack->push_raw(top1);
 930       break;
 931     }
 932 
 933     case Bytecodes::_dup2_x2: {
 934       TrackingStackEntry top1 = stack->get_entry(0);
 935       TrackingStackEntry top2 = stack->get_entry(1);
 936       TrackingStackEntry top3 = stack->get_entry(2);
 937       TrackingStackEntry top4 = stack->get_entry(3);
 938       stack->pop(4);
 939       stack->push_raw(top2);
 940       stack->push_raw(top1);
 941       stack->push_raw(top4);
 942       stack->push_raw(top3);
 943       stack->push_raw(top2);
 944       stack->push_raw(top1);
 945       break;
 946     }
 947 
 948     case Bytecodes::_swap: {
 949       TrackingStackEntry top1 = stack->get_entry(0);
 950       TrackingStackEntry top2 = stack->get_entry(1);
 951       stack->pop(2);
 952       stack->push(top1);
 953       stack->push(top2);
 954       break;
 955     }
 956 
 957     case Bytecodes::_iadd:
 958     case Bytecodes::_ladd:
 959     case Bytecodes::_fadd:
 960     case Bytecodes::_dadd:
 961     case Bytecodes::_isub:
 962     case Bytecodes::_lsub:
 963     case Bytecodes::_fsub:
 964     case Bytecodes::_dsub:
 965     case Bytecodes::_imul:
 966     case Bytecodes::_lmul:
 967     case Bytecodes::_fmul:
 968     case Bytecodes::_dmul:
 969     case Bytecodes::_idiv:
 970     case Bytecodes::_ldiv:
 971     case Bytecodes::_fdiv:
 972     case Bytecodes::_ddiv:
 973     case Bytecodes::_irem:
 974     case Bytecodes::_lrem:
 975     case Bytecodes::_frem:
 976     case Bytecodes::_drem:
 977     case Bytecodes::_iand:
 978     case Bytecodes::_land:
 979     case Bytecodes::_ior:
 980     case Bytecodes::_lor:
 981     case Bytecodes::_ixor:
 982     case Bytecodes::_lxor:
 983       stack->pop(2 * type2size[Bytecodes::result_type(code)]);
 984       stack->push(bci, Bytecodes::result_type(code));
 985       break;
 986 
 987     case Bytecodes::_ineg:
 988     case Bytecodes::_lneg:
 989     case Bytecodes::_fneg:
 990     case Bytecodes::_dneg:
 991       stack->pop(type2size[Bytecodes::result_type(code)]);
 992       stack->push(bci, Bytecodes::result_type(code));
 993       break;
 994 
 995     case Bytecodes::_ishl:
 996     case Bytecodes::_lshl:
 997     case Bytecodes::_ishr:
 998     case Bytecodes::_lshr:
 999     case Bytecodes::_iushr:
1000     case Bytecodes::_lushr:
1001       stack->pop(1 + type2size[Bytecodes::result_type(code)]);
1002       stack->push(bci, Bytecodes::result_type(code));
1003       break;
1004 
1005     case Bytecodes::_i2l:
1006     case Bytecodes::_i2f:
1007     case Bytecodes::_i2d:
1008     case Bytecodes::_f2i:
1009     case Bytecodes::_f2l:
1010     case Bytecodes::_f2d:
1011     case Bytecodes::_i2b:
1012     case Bytecodes::_i2c:
1013     case Bytecodes::_i2s:
1014       stack->pop(1);
1015       stack->push(bci, Bytecodes::result_type(code));
1016       break;
1017 
1018     case Bytecodes::_l2i:
1019     case Bytecodes::_l2f:
1020     case Bytecodes::_l2d:
1021     case Bytecodes::_d2i:
1022     case Bytecodes::_d2l:
1023     case Bytecodes::_d2f:
1024       stack->pop(2);
1025       stack->push(bci, Bytecodes::result_type(code));
1026       break;
1027 
1028     case Bytecodes::_lcmp:
1029     case Bytecodes::_fcmpl:
1030     case Bytecodes::_fcmpg:
1031     case Bytecodes::_dcmpl:
1032     case Bytecodes::_dcmpg:
1033       stack->pop(1 - Bytecodes::depth(code));
1034       stack->push(bci, T_INT);
1035       break;
1036 
1037     case Bytecodes::_ifeq:
1038     case Bytecodes::_ifne:
1039     case Bytecodes::_iflt:
1040     case Bytecodes::_ifge:
1041     case Bytecodes::_ifgt:
1042     case Bytecodes::_ifle:
1043     case Bytecodes::_if_icmpeq:
1044     case Bytecodes::_if_icmpne:
1045     case Bytecodes::_if_icmplt:
1046     case Bytecodes::_if_icmpge:
1047     case Bytecodes::_if_icmpgt:
1048     case Bytecodes::_if_icmple:
1049     case Bytecodes::_if_acmpeq:
1050     case Bytecodes::_if_acmpne:
1051     case Bytecodes::_ifnull:
1052     case Bytecodes::_ifnonnull:
1053       stack->pop(-Bytecodes::depth(code));
1054       dest_bci = bci + (int16_t) Bytes::get_Java_u2(code_base + pos);
1055       break;
1056 
1057     case Bytecodes::_jsr:
1058       // NOTE: Bytecodes has wrong depth for jsr.
1059       stack->push(bci, T_ADDRESS);
1060       dest_bci = bci + (int16_t) Bytes::get_Java_u2(code_base + pos);
1061       flow_ended = true;
1062       break;
1063 
1064     case Bytecodes::_jsr_w: {
1065       // NOTE: Bytecodes has wrong depth for jsr.
1066       stack->push(bci, T_ADDRESS);
1067       dest_bci = bci + (int32_t) Bytes::get_Java_u4(code_base + pos);
1068       flow_ended = true;
1069       break;
1070     }
1071 
1072     case Bytecodes::_ret:
1073       // We don't track local variables, so we cannot know were we
1074       // return. This makes the stacks imprecise, but we have to
1075       // live with that.
1076       flow_ended = true;
1077       break;
1078 
1079     case Bytecodes::_tableswitch: {
1080       stack->pop(1);
1081       pos = (pos + 3) & ~3;
1082       dest_bci = bci + (int32_t) Bytes::get_Java_u4(code_base + pos);
1083       int low = (int32_t) Bytes::get_Java_u4(code_base + pos + 4);
1084       int high = (int32_t) Bytes::get_Java_u4(code_base + pos + 8);
1085 
1086       for (int64_t i = low; i <= high; ++i) {
1087         dests.push(bci + (int32_t) Bytes::get_Java_u4(code_base + pos + 12 + 4 * (i - low)));
1088       }
1089 
1090       break;
1091     }
1092 
1093     case Bytecodes::_lookupswitch: {
1094       stack->pop(1);
1095       pos = (pos + 3) & ~3;
1096       dest_bci = bci + (int32_t) Bytes::get_Java_u4(code_base + pos);
1097       int nr_of_dests = (int32_t) Bytes::get_Java_u4(code_base + pos + 4);
1098 
1099       for (int i = 0; i < nr_of_dests; ++i) {
1100         dests.push(bci + (int32_t) Bytes::get_Java_u4(code_base + pos + 12 + 8 * i));
1101       }
1102 
1103       break;
1104     }
1105 
1106     case Bytecodes::_ireturn:
1107     case Bytecodes::_lreturn:
1108     case Bytecodes::_freturn:
1109     case Bytecodes::_dreturn:
1110     case Bytecodes::_areturn:
1111     case Bytecodes::_return:
1112     case Bytecodes::_athrow:
1113       stack->pop(-Bytecodes::depth(code));
1114       flow_ended = true;
1115       break;
1116 
1117     case Bytecodes::_getstatic:
1118     case Bytecodes::_getfield: {
1119       int cp_index = Bytes::get_native_u2(code_base + pos) DEBUG_ONLY(+ ConstantPool::CPCACHE_INDEX_TAG);
1120       ConstantPool* cp = _method->constants();
1121       int name_and_type_index = cp->name_and_type_ref_index_at(cp_index);
1122       int type_index = cp->signature_ref_index_at(name_and_type_index);
1123       Symbol* signature = cp->symbol_at(type_index);
1124       stack->pop(1 - Bytecodes::depth(code));
1125       stack->push(bci, char2type((char) signature->char_at(0)));
1126       break;
1127     }
1128 
1129     case Bytecodes::_putstatic:
1130     case Bytecodes::_putfield: {
1131       int cp_index = Bytes::get_native_u2(code_base + pos) DEBUG_ONLY(+ ConstantPool::CPCACHE_INDEX_TAG);
1132       ConstantPool* cp = _method->constants();
1133       int name_and_type_index = cp->name_and_type_ref_index_at(cp_index);
1134       int type_index = cp->signature_ref_index_at(name_and_type_index);
1135       Symbol* signature = cp->symbol_at(type_index);
1136       ResultTypeFinder result_type(signature);
1137       stack->pop(type2size[char2type((char) signature->char_at(0))] - Bytecodes::depth(code) - 1);
1138       break;
1139     }
1140 
1141     case Bytecodes::_invokevirtual:
1142     case Bytecodes::_invokespecial:
1143     case Bytecodes::_invokestatic:
1144     case Bytecodes::_invokeinterface:
1145     case Bytecodes::_invokedynamic: {
1146       ConstantPool* cp = _method->constants();
1147       int cp_index;
1148 
1149       if (code == Bytecodes::_invokedynamic) {
1150         cp_index = ((int) Bytes::get_native_u4(code_base + pos));
1151       } else {
1152         cp_index = Bytes::get_native_u2(code_base + pos) DEBUG_ONLY(+ ConstantPool::CPCACHE_INDEX_TAG);
1153       }
1154 
1155       int name_and_type_index = cp->name_and_type_ref_index_at(cp_index);
1156       int type_index = cp->signature_ref_index_at(name_and_type_index);
1157       Symbol* signature = cp->symbol_at(type_index);
1158 
1159       if ((code != Bytecodes::_invokestatic) && (code != Bytecodes::_invokedynamic)) {
1160         // Pop class.
1161         stack->pop(1);
1162       }
1163 
1164       stack->pop(ArgumentSizeComputer(signature).size());
1165       ResultTypeFinder result_type(signature);
1166       stack->push(bci, result_type.type());
1167       break;
1168     }
1169 
1170     case Bytecodes::_newarray:
1171     case Bytecodes::_anewarray:
1172     case Bytecodes::_instanceof:
1173       stack->pop(1);
1174       stack->push(bci, Bytecodes::result_type(code));
1175       break;
1176 
1177     case Bytecodes::_arraylength:
1178       // The return type of arraylength is wrong in the bytecodes table (T_VOID).
1179       stack->pop(1);
1180       stack->push(bci, T_INT);
1181       break;
1182 
1183     case Bytecodes::_checkcast:
1184       break;
1185 
1186     case Bytecodes::_multianewarray:
1187       stack->pop(*(uint8_t*) (code_base + pos + 2));
1188       stack->push(bci, T_OBJECT);
1189       break;
1190 
1191    case Bytecodes::_goto:
1192       stack->pop(-Bytecodes::depth(code));
1193       dest_bci = bci + (int16_t) Bytes::get_Java_u2(code_base + pos);
1194       flow_ended = true;
1195       break;
1196 
1197 
1198    case Bytecodes::_goto_w:
1199       stack->pop(-Bytecodes::depth(code));
1200       dest_bci = bci + (int32_t) Bytes::get_Java_u4(code_base + pos);
1201       flow_ended = true;
1202       break;
1203 
1204     default:
1205       // Allow at least the bcis which have stack info to work.
1206       _all_processed = false;
1207       _added_one = false;
1208       delete stack;
1209 
1210       return len;
1211   }
1212 
1213   // Put new stack to the next instruction, if we might reach if from
1214   // this bci.
1215   if (!flow_ended) {
1216     if (_stacks->at(bci + len) == NULL) {
1217       _added_one = true;
1218     }
1219 
1220     merge(bci + len, stack);
1221   }
1222 
1223   // Put the stack to the branch target too.
1224   if (dest_bci != -1) {
1225     if (_stacks->at(dest_bci) == NULL) {
1226       _added_one = true;
1227     }
1228 
1229     merge(dest_bci, stack);
1230   }
1231 
1232   // If we have more than one branch target, process these too.
1233   for (int64_t i = 0; i < dests.length(); ++i) {
1234     if (_stacks->at(dests.at(i)) == NULL) {
1235       _added_one = true;
1236     }
1237 
1238     merge(dests.at(i), stack);
1239   }
1240 
1241   delete stack;
1242 
1243   return len;
1244 }
1245 
1246 TrackingStackSource TrackingStackCreator::get_source(int bci, int slot, int max_detail) {
1247   assert(bci >= 0, "BCI too low");
1248   assert(bci < get_size(), "BCI to large");
1249 
1250   if (max_detail <= 0) {
1251     return createInvalidSource(bci);
1252   }
1253 
1254   if (_stacks->at(bci) == NULL) {
1255     return createInvalidSource(bci);
1256   }
1257 
1258   TrackingStack* stack = _stacks->at(bci);
1259   assert(slot >= 0, "Slot nr. too low");
1260   assert(slot < stack->get_size(), "Slot nr. too large");
1261 
1262   TrackingStackEntry entry = stack->get_entry(slot);
1263 
1264   if (!entry.has_bci()) {
1265     return createInvalidSource(bci);
1266   }
1267 
1268   // Get the bytecode.
1269   int source_bci = entry.get_bci();
1270   address code_base = _method->constMethod()->code_base();
1271   Bytecodes::Code code = Bytecodes::java_code_at(_method, code_base + source_bci);
1272   bool is_wide = false;
1273   int pos = source_bci + 1;
1274 
1275   if (code == Bytecodes::_wide) {
1276     is_wide = true;
1277     code = Bytecodes::java_code_at(_method, code_base + source_bci + 1);
1278     pos += 1;
1279   }
1280 
1281   switch (code) {
1282     case Bytecodes::_iload:
1283     case Bytecodes::_lload:
1284     case Bytecodes::_fload:
1285     case Bytecodes::_dload:
1286     case Bytecodes::_aload: {
1287       int index;
1288 
1289       if (is_wide) {
1290         index = Bytes::get_Java_u2(code_base + source_bci + 2);
1291       } else {
1292         index = *(uint8_t*) (code_base + source_bci + 1);
1293       }
1294 
1295       return createLocalVarSource(source_bci, _method, index);
1296     }
1297 
1298     case Bytecodes::_iload_0:
1299     case Bytecodes::_lload_0:
1300     case Bytecodes::_fload_0:
1301     case Bytecodes::_dload_0:
1302     case Bytecodes::_aload_0:
1303       return createLocalVarSource(source_bci, _method, 0);
1304 
1305     case Bytecodes::_iload_1:
1306     case Bytecodes::_lload_1:
1307     case Bytecodes::_fload_1:
1308     case Bytecodes::_dload_1:
1309     case Bytecodes::_aload_1:
1310       return createLocalVarSource(source_bci, _method, 1);
1311 
1312     case Bytecodes::_iload_2:
1313     case Bytecodes::_lload_2:
1314     case Bytecodes::_fload_2:
1315     case Bytecodes::_dload_2:
1316     case Bytecodes::_aload_2:
1317       return createLocalVarSource(source_bci, _method, 2);
1318 
1319     case Bytecodes::_lload_3:
1320     case Bytecodes::_iload_3:
1321     case Bytecodes::_fload_3:
1322     case Bytecodes::_dload_3:
1323     case Bytecodes::_aload_3:
1324       return createLocalVarSource(source_bci, _method, 3);
1325 
1326     case Bytecodes::_aconst_null:
1327     case Bytecodes::_iconst_m1:
1328     case Bytecodes::_iconst_0:
1329     case Bytecodes::_iconst_1:
1330     case Bytecodes::_iconst_2:
1331     case Bytecodes::_iconst_3:
1332     case Bytecodes::_iconst_4:
1333     case Bytecodes::_iconst_5:
1334     case Bytecodes::_lconst_0:
1335     case Bytecodes::_lconst_1:
1336     case Bytecodes::_fconst_0:
1337     case Bytecodes::_fconst_1:
1338     case Bytecodes::_fconst_2:
1339     case Bytecodes::_dconst_0:
1340     case Bytecodes::_dconst_1:
1341     case Bytecodes::_bipush:
1342     case Bytecodes::_sipush:
1343       return createConstantSource(source_bci);
1344 
1345     case Bytecodes::_iaload:
1346     case Bytecodes::_faload:
1347     case Bytecodes::_aaload:
1348     case Bytecodes::_baload:
1349     case Bytecodes::_caload:
1350     case Bytecodes::_saload:
1351     case Bytecodes::_laload:
1352     case Bytecodes::_daload: {
1353       TrackingStackSource array_source = get_source(source_bci, 1, max_detail - 1);
1354       TrackingStackSource index_source = get_source(source_bci, 0, max_detail - 1);
1355       return createArraySource(source_bci, array_source, index_source);
1356     }
1357 
1358     case Bytecodes::_invokevirtual:
1359     case Bytecodes::_invokespecial:
1360     case Bytecodes::_invokestatic:
1361     case Bytecodes::_invokeinterface: {
1362         int cp_index = Bytes::get_native_u2(code_base + pos) DEBUG_ONLY(+ ConstantPool::CPCACHE_INDEX_TAG);
1363         return createMethodSource(source_bci, _method, cp_index);
1364     }
1365 
1366     case Bytecodes::_getstatic:
1367       return createStaticFieldSource(source_bci, _method,
1368                                      Bytes::get_native_u2(code_base + pos) + ConstantPool::CPCACHE_INDEX_TAG);
1369 
1370     case Bytecodes::_getfield: {
1371       int cp_index = Bytes::get_native_u2(code_base + pos) + ConstantPool::CPCACHE_INDEX_TAG;
1372       TrackingStackSource object_source = get_source(source_bci, 0, max_detail - 1);
1373       return createFieldSource(source_bci, _method, cp_index, object_source);
1374     }
1375 
1376     default:
1377       return createInvalidSource(bci);
1378   }
1379 }
1380 
1381 int TrackingStackCreator::get_null_pointer_slot(int bci, char const** reason) {
1382   // If this NPE was created via reflection, we have no real NPE.
1383   if (_method->method_holder() == SystemDictionary::reflect_NativeConstructorAccessorImpl_klass()) {
1384     return -2;
1385   }
1386 
1387   // Get the bytecode.
1388   address code_base = _method->constMethod()->code_base();
1389   Bytecodes::Code code = Bytecodes::java_code_at(_method, code_base + bci);
1390   int pos = bci + 1;
1391 
1392   if (code == Bytecodes::_wide) {
1393     code = Bytecodes::java_code_at(_method, code_base + bci + 1);
1394     pos += 1;
1395   }
1396 
1397   int result = -1;
1398 
1399   switch (code) {
1400     case Bytecodes::_iaload:
1401       if (reason != NULL) {
1402         *reason = "while trying to load from a null int array";
1403       }
1404 
1405       result = 1;
1406       break;
1407 
1408     case Bytecodes::_faload:
1409       if (reason != NULL) {
1410         *reason = "while trying to load from a null float array";
1411       }
1412 
1413       result = 1;
1414       break;
1415 
1416     case Bytecodes::_aaload:
1417       if (reason != NULL) {
1418         *reason = "while trying to load from a null object array";
1419       }
1420 
1421       result = 1;
1422       break;
1423 
1424     case Bytecodes::_baload:
1425       if (reason != NULL) {
1426         *reason = "while trying to load from a null byte (or boolean) array";
1427       }
1428 
1429       result = 1;
1430       break;
1431 
1432     case Bytecodes::_caload:
1433       if (reason != NULL) {
1434         *reason = "while trying to load from a null char array";
1435       }
1436 
1437       result = 1;
1438       break;
1439 
1440     case Bytecodes::_saload:
1441       if (reason != NULL) {
1442         *reason = "while trying to load from a null short array";
1443       }
1444 
1445       result = 1;
1446       break;
1447 
1448     case Bytecodes::_laload:
1449       if (reason != NULL) {
1450         *reason = "while trying to load from a null long array";
1451       }
1452 
1453       result = 1;
1454       break;
1455 
1456     case Bytecodes::_daload:
1457       if (reason != NULL) {
1458         *reason = "while trying to load from a null double array";
1459       }
1460 
1461       result = 1;
1462       break;
1463 
1464     case Bytecodes::_iastore:
1465       if (reason != NULL) {
1466         *reason = "while trying to store to a null int array";
1467       }
1468 
1469       result = 2;
1470       break;
1471 
1472     case Bytecodes::_lastore:
1473       if (reason != NULL) {
1474         *reason = "while trying to store to a null long array";
1475       }
1476 
1477       result = 3;
1478       break;
1479 
1480     case Bytecodes::_fastore:
1481       if (reason != NULL) {
1482         *reason = "while trying to store to a null float array";
1483       }
1484 
1485       result = 2;
1486       break;
1487 
1488     case Bytecodes::_dastore:
1489       if (reason != NULL) {
1490         *reason = "while trying to store to a null double array";
1491       }
1492 
1493       result = 3;
1494       break;
1495 
1496     case Bytecodes::_aastore:
1497       if (reason != NULL) {
1498         *reason = "while trying to store to a null object array";
1499       }
1500 
1501       result = 2;
1502       break;
1503 
1504     case Bytecodes::_bastore:
1505       if (reason != NULL) {
1506         *reason = "while trying to store to a null byte (or boolean) array";
1507       }
1508 
1509       result = 2;
1510       break;
1511 
1512     case Bytecodes::_castore:
1513       if (reason != NULL) {
1514         *reason = "while trying to store to a null char array";
1515       }
1516 
1517       result = 2;
1518       break;
1519 
1520     case Bytecodes::_sastore:
1521       if (reason != NULL) {
1522         *reason = "while trying to store to a null short array";
1523       }
1524 
1525       result = 2;
1526       break;
1527 
1528     case Bytecodes::_getfield:
1529       {
1530         if (reason != NULL) {
1531           int cp_index = Bytes::get_native_u2(code_base + pos) DEBUG_ONLY(+ ConstantPool::CPCACHE_INDEX_TAG);
1532           ConstantPool* cp = _method->constants();
1533           int name_and_type_index = cp->name_and_type_ref_index_at(cp_index);
1534           int name_index = cp->name_ref_index_at(name_and_type_index);
1535           Symbol* name = cp->symbol_at(name_index);
1536           stringStream ss;
1537           ss.print("while trying to read the field '%s' of a null object", name->as_C_string());
1538           *reason = ss.as_string();
1539         }
1540 
1541         result = 0;
1542       }
1543 
1544       break;
1545 
1546     case Bytecodes::_arraylength:
1547       if (reason != NULL) {
1548         *reason = "while trying to get the length of a null array";
1549       }
1550 
1551       result = 0;
1552       break;
1553 
1554     case Bytecodes::_athrow:
1555       if (reason != NULL) {
1556         *reason = "while trying to throw a null exception object";
1557       }
1558 
1559       result = 0;
1560       break;
1561 
1562     case Bytecodes::_monitorenter:
1563       if (reason != NULL) {
1564         *reason = "while trying to enter a null monitor";
1565       }
1566 
1567       result = 0;
1568       break;
1569 
1570     case Bytecodes::_monitorexit:
1571       if (reason != NULL) {
1572         *reason = "while trying to exit a null monitor";
1573       }
1574 
1575       result = 0;
1576       break;
1577 
1578     case Bytecodes::_putfield:
1579       {
1580         int cp_index = Bytes::get_native_u2(code_base + pos) DEBUG_ONLY(+ ConstantPool::CPCACHE_INDEX_TAG);
1581         ConstantPool* cp = _method->constants();
1582         int name_and_type_index = cp->name_and_type_ref_index_at(cp_index);
1583         int type_index = cp->signature_ref_index_at(name_and_type_index);
1584         Symbol* signature = cp->symbol_at(type_index);
1585 
1586         if (reason != NULL) {
1587           stringStream ss;
1588           ss.print("while trying to write the field '%s' of a null object",
1589                    MethodBytecodePrinter::get_field_and_class(_method, cp_index));
1590           *reason = ss.as_string();
1591         }
1592 
1593         result = type2size[char2type((char) signature->char_at(0))];
1594       }
1595 
1596       break;
1597 
1598     case Bytecodes::_invokevirtual:
1599     case Bytecodes::_invokespecial:
1600     case Bytecodes::_invokeinterface:
1601       {
1602         int cp_index = Bytes::get_native_u2(code_base+ pos) DEBUG_ONLY(+ ConstantPool::CPCACHE_INDEX_TAG);
1603         ConstantPool* cp = _method->constants();
1604         int name_and_type_index = cp->name_and_type_ref_index_at(cp_index);
1605         int name_index = cp->name_ref_index_at(name_and_type_index);
1606         int type_index = cp->signature_ref_index_at(name_and_type_index);
1607         Symbol* name = cp->symbol_at(name_index);
1608         Symbol* signature = cp->symbol_at(type_index);
1609 
1610         // Assume the the call of a constructor can never cause a NullPointerException
1611         // (which is true in Java). This is mainly used to avoid generating wrong
1612         // messages for NullPointerExceptions created explicitly by new in Java code.
1613         if (name != vmSymbols::object_initializer_name()) {
1614           if (reason != NULL) {
1615             stringStream ss;
1616             ss.print("while trying to invoke the method '%s' on a null reference",
1617                      MethodBytecodePrinter::get_method_name(_method, cp_index));
1618             *reason = ss.as_string();
1619           }
1620 
1621           result = ArgumentSizeComputer(signature).size();
1622         }
1623         else {
1624           result = -2;
1625         }
1626       }
1627 
1628       break;
1629 
1630     default:
1631       break;
1632   }
1633 
1634   return result;
1635 }
1636