< prev index next >

src/hotspot/share/interpreter/bytecodeUtils.cpp

Print this page

1093 }
1094 
1095 #define INVALID_BYTECODE_ENCOUNTERED -1
1096 #define NPE_EXPLICIT_CONSTRUCTED -2
1097 int ExceptionMessageBuilder::get_NPE_null_slot(int bci) {
1098   // Get the bytecode.
1099   address code_base = _method->constMethod()->code_base();
1100   Bytecodes::Code code = Bytecodes::java_code_at(_method, code_base + bci);
1101   int pos = bci + 1;  // Position of argument of the bytecode.
1102   if (code == Bytecodes::_wide) {
1103     code = Bytecodes::java_code_at(_method, code_base + bci + 1);
1104     pos += 1;
1105   }
1106 
1107   switch (code) {
1108     case Bytecodes::_getfield:
1109     case Bytecodes::_arraylength:
1110     case Bytecodes::_athrow:
1111     case Bytecodes::_monitorenter:
1112     case Bytecodes::_monitorexit:

1113       return 0;
1114     case Bytecodes::_iaload:
1115     case Bytecodes::_faload:
1116     case Bytecodes::_aaload:
1117     case Bytecodes::_baload:
1118     case Bytecodes::_caload:
1119     case Bytecodes::_saload:
1120     case Bytecodes::_laload:
1121     case Bytecodes::_daload:
1122       return 1;
1123     case Bytecodes::_iastore:
1124     case Bytecodes::_fastore:
1125     case Bytecodes::_aastore:
1126     case Bytecodes::_bastore:
1127     case Bytecodes::_castore:
1128     case Bytecodes::_sastore:
1129       return 2;
1130     case Bytecodes::_lastore:
1131     case Bytecodes::_dastore:
1132       return 3;

1153         // messages for NullPointerExceptions created explicitly by new in Java code.
1154         if (name != vmSymbols::object_initializer_name()) {
1155           int     type_index = cp->signature_ref_index_at(name_and_type_index);
1156           Symbol* signature  = cp->symbol_at(type_index);
1157           // The 'this' parameter was null. Return the slot of it.
1158           return ArgumentSizeComputer(signature).size();
1159         } else {
1160           return NPE_EXPLICIT_CONSTRUCTED;
1161         }
1162       }
1163 
1164     default:
1165       break;
1166   }
1167 
1168   return INVALID_BYTECODE_ENCOUNTERED;
1169 }
1170 
1171 bool ExceptionMessageBuilder::print_NPE_cause(outputStream* os, int bci, int slot) {
1172   if (print_NPE_cause0(os, bci, slot, _max_cause_detail, false, " because \"")) {
1173     os->print("\" is null");






1174     return true;
1175   }
1176   return false;
1177 }
1178 
1179 // Recursively print what was null.
1180 //
1181 // Go to the bytecode that pushed slot 'slot' on the operand stack
1182 // at bytecode 'bci'. Compute a message for that bytecode. If
1183 // necessary (array, field), recur further.
1184 // At most do max_detail recursions.
1185 // Prefix is used to print a proper beginning of the whole
1186 // sentence.
1187 // inner_expr is used to omit some text, like 'static' in
1188 // inner expressions like array subscripts.
1189 //
1190 // Returns true if something was printed.
1191 //
1192 bool ExceptionMessageBuilder::print_NPE_cause0(outputStream* os, int bci, int slot,
1193                                                int max_detail,

1415     case Bytecodes::_getfield: {
1416         int cp_index = Bytes::get_native_u2(code_base + pos);
1417         ConstantPool* cp = _method->constants();
1418         int name_and_type_index = cp->name_and_type_ref_index_at(cp_index, code);
1419         int name_index = cp->name_ref_index_at(name_and_type_index);
1420         Symbol* name = cp->symbol_at(name_index);
1421         os->print("Cannot read field \"%s\"", name->as_C_string());
1422       } break;
1423     case Bytecodes::_putfield: {
1424         int cp_index = Bytes::get_native_u2(code_base + pos);
1425         os->print("Cannot assign field \"%s\"", get_field_name(_method, cp_index, code));
1426       } break;
1427     case Bytecodes::_invokevirtual:
1428     case Bytecodes::_invokespecial:
1429     case Bytecodes::_invokeinterface: {
1430         int cp_index = Bytes::get_native_u2(code_base+ pos);
1431         os->print("Cannot invoke \"");
1432         print_method_name(os, _method, cp_index, code);
1433         os->print("\"");
1434       } break;





1435 
1436     default:
1437       assert(0, "We should have checked this bytecode in get_NPE_null_slot().");
1438       break;
1439   }
1440 }
1441 
1442 // Main API
1443 bool BytecodeUtils::get_NPE_message_at(outputStream* ss, Method* method, int bci) {
1444 
1445   NoSafepointVerifier _nsv;   // Cannot use this object over a safepoint.
1446 
1447   // If this NPE was created via reflection, we have no real NPE.
1448   if (method->method_holder() ==
1449       vmClasses::reflect_DirectConstructorHandleAccessor_NativeAccessor_klass()) {
1450     return false;
1451   }
1452 
1453   // Analyse the bytecodes.
1454   ResourceMark rm;

1093 }
1094 
1095 #define INVALID_BYTECODE_ENCOUNTERED -1
1096 #define NPE_EXPLICIT_CONSTRUCTED -2
1097 int ExceptionMessageBuilder::get_NPE_null_slot(int bci) {
1098   // Get the bytecode.
1099   address code_base = _method->constMethod()->code_base();
1100   Bytecodes::Code code = Bytecodes::java_code_at(_method, code_base + bci);
1101   int pos = bci + 1;  // Position of argument of the bytecode.
1102   if (code == Bytecodes::_wide) {
1103     code = Bytecodes::java_code_at(_method, code_base + bci + 1);
1104     pos += 1;
1105   }
1106 
1107   switch (code) {
1108     case Bytecodes::_getfield:
1109     case Bytecodes::_arraylength:
1110     case Bytecodes::_athrow:
1111     case Bytecodes::_monitorenter:
1112     case Bytecodes::_monitorexit:
1113     case Bytecodes::_checkcast:
1114       return 0;
1115     case Bytecodes::_iaload:
1116     case Bytecodes::_faload:
1117     case Bytecodes::_aaload:
1118     case Bytecodes::_baload:
1119     case Bytecodes::_caload:
1120     case Bytecodes::_saload:
1121     case Bytecodes::_laload:
1122     case Bytecodes::_daload:
1123       return 1;
1124     case Bytecodes::_iastore:
1125     case Bytecodes::_fastore:
1126     case Bytecodes::_aastore:
1127     case Bytecodes::_bastore:
1128     case Bytecodes::_castore:
1129     case Bytecodes::_sastore:
1130       return 2;
1131     case Bytecodes::_lastore:
1132     case Bytecodes::_dastore:
1133       return 3;

1154         // messages for NullPointerExceptions created explicitly by new in Java code.
1155         if (name != vmSymbols::object_initializer_name()) {
1156           int     type_index = cp->signature_ref_index_at(name_and_type_index);
1157           Symbol* signature  = cp->symbol_at(type_index);
1158           // The 'this' parameter was null. Return the slot of it.
1159           return ArgumentSizeComputer(signature).size();
1160         } else {
1161           return NPE_EXPLICIT_CONSTRUCTED;
1162         }
1163       }
1164 
1165     default:
1166       break;
1167   }
1168 
1169   return INVALID_BYTECODE_ENCOUNTERED;
1170 }
1171 
1172 bool ExceptionMessageBuilder::print_NPE_cause(outputStream* os, int bci, int slot) {
1173   if (print_NPE_cause0(os, bci, slot, _max_cause_detail, false, " because \"")) {
1174     address code_base = _method->constMethod()->code_base();
1175     Bytecodes::Code code = Bytecodes::java_code_at(_method, code_base + bci);
1176     if (code == Bytecodes::_aastore) {
1177       os->print("\" is null or is a null-free array and there's an attempt to store null in it");
1178     } else {
1179       os->print("\" is null");
1180     }
1181     return true;
1182   }
1183   return false;
1184 }
1185 
1186 // Recursively print what was null.
1187 //
1188 // Go to the bytecode that pushed slot 'slot' on the operand stack
1189 // at bytecode 'bci'. Compute a message for that bytecode. If
1190 // necessary (array, field), recur further.
1191 // At most do max_detail recursions.
1192 // Prefix is used to print a proper beginning of the whole
1193 // sentence.
1194 // inner_expr is used to omit some text, like 'static' in
1195 // inner expressions like array subscripts.
1196 //
1197 // Returns true if something was printed.
1198 //
1199 bool ExceptionMessageBuilder::print_NPE_cause0(outputStream* os, int bci, int slot,
1200                                                int max_detail,

1422     case Bytecodes::_getfield: {
1423         int cp_index = Bytes::get_native_u2(code_base + pos);
1424         ConstantPool* cp = _method->constants();
1425         int name_and_type_index = cp->name_and_type_ref_index_at(cp_index, code);
1426         int name_index = cp->name_ref_index_at(name_and_type_index);
1427         Symbol* name = cp->symbol_at(name_index);
1428         os->print("Cannot read field \"%s\"", name->as_C_string());
1429       } break;
1430     case Bytecodes::_putfield: {
1431         int cp_index = Bytes::get_native_u2(code_base + pos);
1432         os->print("Cannot assign field \"%s\"", get_field_name(_method, cp_index, code));
1433       } break;
1434     case Bytecodes::_invokevirtual:
1435     case Bytecodes::_invokespecial:
1436     case Bytecodes::_invokeinterface: {
1437         int cp_index = Bytes::get_native_u2(code_base+ pos);
1438         os->print("Cannot invoke \"");
1439         print_method_name(os, _method, cp_index, code);
1440         os->print("\"");
1441       } break;
1442     case Bytecodes::_checkcast: {
1443         int cp_index = Bytes::get_Java_u2(code_base + pos);
1444         ConstantPool* cp = _method->constants();
1445         os->print("Cannot cast to null-free type \"%s\"", cp->klass_at_noresolve(cp_index)->as_C_string());
1446       } break;
1447 
1448     default:
1449       assert(0, "We should have checked this bytecode in get_NPE_null_slot().");
1450       break;
1451   }
1452 }
1453 
1454 // Main API
1455 bool BytecodeUtils::get_NPE_message_at(outputStream* ss, Method* method, int bci) {
1456 
1457   NoSafepointVerifier _nsv;   // Cannot use this object over a safepoint.
1458 
1459   // If this NPE was created via reflection, we have no real NPE.
1460   if (method->method_holder() ==
1461       vmClasses::reflect_DirectConstructorHandleAccessor_NativeAccessor_klass()) {
1462     return false;
1463   }
1464 
1465   // Analyse the bytecodes.
1466   ResourceMark rm;
< prev index next >