00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054 #include "hprof.h"
00055
00056
00057
00058
00059
00060 typedef struct ClassKey {
00061 StringIndex sig_string_index;
00062 LoaderIndex loader_index;
00063 } ClassKey;
00064
00065
00066
00067 typedef struct MethodInfo {
00068 StringIndex name_index;
00069 StringIndex sig_index;
00070 jmethodID method_id;
00071 } MethodInfo;
00072
00073
00074
00075 typedef struct ClassInfo {
00076 jclass classref;
00077 MethodInfo *method;
00078 int method_count;
00079 ObjectIndex object_index;
00080 SerialNumber serial_num;
00081 ClassStatus status;
00082 ClassIndex super;
00083 StringIndex name;
00084 jint inst_size;
00085 jint field_count;
00086 FieldInfo *field;
00087 } ClassInfo;
00088
00089
00090
00091 static ClassKey*
00092 get_pkey(ClassIndex index)
00093 {
00094 void *key_ptr;
00095 int key_len;
00096
00097 table_get_key(gdata->class_table, index, (void*)&key_ptr, &key_len);
00098 HPROF_ASSERT(key_len==sizeof(ClassKey));
00099 HPROF_ASSERT(key_ptr!=NULL);
00100 return (ClassKey*)key_ptr;
00101 }
00102
00103 static void
00104 fillin_pkey(const char *sig, LoaderIndex loader_index, ClassKey *pkey)
00105 {
00106 static ClassKey empty_key;
00107
00108 HPROF_ASSERT(loader_index!=0);
00109 *pkey = empty_key;
00110 pkey->sig_string_index = string_find_or_create(sig);
00111 pkey->loader_index = loader_index;
00112 }
00113
00114 static ClassInfo *
00115 get_info(ClassIndex index)
00116 {
00117 ClassInfo *info;
00118
00119 info = (ClassInfo*)table_get_info(gdata->class_table, index);
00120 return info;
00121 }
00122
00123 static void
00124 fill_info(TableIndex index, ClassKey *pkey)
00125 {
00126 ClassInfo *info;
00127 char *sig;
00128
00129 info = get_info(index);
00130 info->serial_num = gdata->class_serial_number_counter++;
00131 info->method_count = 0;
00132 info->inst_size = -1;
00133 info->field_count = -1;
00134 info->field = NULL;
00135 sig = string_get(pkey->sig_string_index);
00136 if ( sig[0] != JVM_SIGNATURE_CLASS ) {
00137 info->name = pkey->sig_string_index;
00138 } else {
00139 int len;
00140
00141 len = string_get_len(pkey->sig_string_index);
00142 if ( len > 2 ) {
00143 char *name;
00144
00145
00146 name = HPROF_MALLOC(len-1);
00147 (void)memcpy(name, sig+1, len-2);
00148 name[len-2] = 0;
00149 info->name = string_find_or_create(name);
00150 HPROF_FREE(name);
00151 } else {
00152
00153 info->name = pkey->sig_string_index;
00154 }
00155 }
00156 }
00157
00158 static ClassIndex
00159 find_entry(ClassKey *pkey)
00160 {
00161 ClassIndex index;
00162
00163 index = table_find_entry(gdata->class_table,
00164 (void*)pkey, (int)sizeof(ClassKey));
00165 return index;
00166 }
00167
00168 static ClassIndex
00169 create_entry(ClassKey *pkey)
00170 {
00171 ClassIndex index;
00172
00173 index = table_create_entry(gdata->class_table,
00174 (void*)pkey, (int)sizeof(ClassKey), NULL);
00175 fill_info(index, pkey);
00176 return index;
00177 }
00178
00179 static ClassIndex
00180 find_or_create_entry(ClassKey *pkey)
00181 {
00182 ClassIndex index;
00183
00184 HPROF_ASSERT(pkey!=NULL);
00185 HPROF_ASSERT(pkey->loader_index!=0);
00186 index = find_entry(pkey);
00187 if ( index == 0 ) {
00188 index = create_entry(pkey);
00189 }
00190 return index;
00191 }
00192
00193 static void
00194 delete_classref(JNIEnv *env, ClassInfo *info, jclass klass)
00195 {
00196 jclass ref;
00197 int i;
00198
00199 HPROF_ASSERT(env!=NULL);
00200 HPROF_ASSERT(info!=NULL);
00201
00202 for ( i = 0 ; i < info->method_count ; i++ ) {
00203 info->method[i].method_id = NULL;
00204 }
00205 ref = info->classref;
00206 if ( klass != NULL ) {
00207 info->classref = newGlobalReference(env, klass);
00208 } else {
00209 info->classref = NULL;
00210 }
00211 if ( ref != NULL ) {
00212 deleteGlobalReference(env, ref);
00213 }
00214 }
00215
00216 static void
00217 cleanup_item(TableIndex index, void *key_ptr, int key_len,
00218 void *info_ptr, void *arg)
00219 {
00220 ClassInfo *info;
00221
00222
00223 HPROF_ASSERT(key_ptr!=NULL);
00224 HPROF_ASSERT(key_len==sizeof(ClassKey));
00225 HPROF_ASSERT(info_ptr!=NULL);
00226 info = (ClassInfo *)info_ptr;
00227 if ( info->method_count > 0 ) {
00228 HPROF_FREE((void*)info->method);
00229 info->method_count = 0;
00230 info->method = NULL;
00231 }
00232 if ( info->field != NULL ) {
00233 HPROF_FREE((void*)info->field);
00234 info->field_count = 0;
00235 info->field = NULL;
00236 }
00237 }
00238
00239 static void
00240 delete_ref_item(TableIndex index, void *key_ptr, int key_len,
00241 void *info_ptr, void *arg)
00242 {
00243 delete_classref((JNIEnv*)arg, (ClassInfo*)info_ptr, NULL);
00244 }
00245
00246 static void
00247 list_item(TableIndex index, void *key_ptr, int key_len,
00248 void *info_ptr, void *arg)
00249 {
00250 ClassInfo *info;
00251 ClassKey key;
00252 char *sig;
00253 int i;
00254
00255 HPROF_ASSERT(key_ptr!=NULL);
00256 HPROF_ASSERT(key_len==sizeof(ClassKey));
00257 HPROF_ASSERT(info_ptr!=NULL);
00258 key = *((ClassKey*)key_ptr);
00259 sig = string_get(key.sig_string_index);
00260 info = (ClassInfo *)info_ptr;
00261 debug_message(
00262 "0x%08x: Class %s, SN=%u, status=0x%08x, ref=%p,"
00263 " method_count=%d\n",
00264 index,
00265 (const char *)sig,
00266 info->serial_num,
00267 info->status,
00268 (void*)info->classref,
00269 info->method_count);
00270 if ( info->method_count > 0 ) {
00271 for ( i = 0 ; i < info->method_count ; i++ ) {
00272 debug_message(
00273 " Method %d: \"%s\", sig=\"%s\", method=%p\n",
00274 i,
00275 string_get(info->method[i].name_index),
00276 string_get(info->method[i].sig_index),
00277 (void*)info->method[i].method_id);
00278 }
00279 }
00280 }
00281
00282 static void
00283 all_status_remove(TableIndex index, void *key_ptr, int key_len,
00284 void *info_ptr, void *arg)
00285 {
00286 ClassInfo *info;
00287 ClassStatus status;
00288
00289 HPROF_ASSERT(info_ptr!=NULL);
00290
00291 status = (ClassStatus)(long)(ptrdiff_t)arg;
00292 info = (ClassInfo *)info_ptr;
00293 info->status &= (~status);
00294 }
00295
00296 static void
00297 unload_walker(TableIndex index, void *key_ptr, int key_len,
00298 void *info_ptr, void *arg)
00299 {
00300 ClassInfo *info;
00301
00302 HPROF_ASSERT(info_ptr!=NULL);
00303 info = (ClassInfo *)info_ptr;
00304 if ( ! ( info->status & CLASS_IN_LOAD_LIST ) ) {
00305 if ( ! (info->status & (CLASS_SPECIAL|CLASS_SYSTEM|CLASS_UNLOADED)) ) {
00306 io_write_class_unload(info->serial_num, info->object_index);
00307 info->status |= CLASS_UNLOADED;
00308 delete_classref((JNIEnv*)arg, info, NULL);
00309 }
00310 }
00311 }
00312
00313
00314
00315 void
00316 class_init(void)
00317 {
00318 HPROF_ASSERT(gdata->class_table==NULL);
00319 gdata->class_table = table_initialize("Class", 512, 512, 511,
00320 (int)sizeof(ClassInfo));
00321 }
00322
00323 ClassIndex
00324 class_find_or_create(const char *sig, LoaderIndex loader_index)
00325 {
00326 ClassKey key;
00327
00328 fillin_pkey(sig, loader_index, &key);
00329 return find_or_create_entry(&key);
00330 }
00331
00332 ClassIndex
00333 class_create(const char *sig, LoaderIndex loader_index)
00334 {
00335 ClassKey key;
00336
00337 fillin_pkey(sig, loader_index, &key);
00338 return create_entry(&key);
00339 }
00340
00341 void
00342 class_prime_system_classes(void)
00343 {
00344
00345
00346
00347
00348 static const char * signatures[] =
00349 {
00350 "Ljava/lang/Object;",
00351 "Ljava/io/Serializable;",
00352 "Ljava/lang/String;",
00353 "Ljava/lang/Class;",
00354 "Ljava/lang/ClassLoader;",
00355 "Ljava/lang/System;",
00356 "Ljava/lang/Thread;",
00357 "Ljava/lang/ThreadGroup;",
00358 };
00359 int n_signatures;
00360 int i;
00361 LoaderIndex loader_index;
00362
00363 n_signatures = (int)sizeof(signatures)/(int)sizeof(signatures[0]);
00364 loader_index = loader_find_or_create(NULL, NULL);
00365 for ( i = 0 ; i < n_signatures ; i++ ) {
00366 ClassInfo *info;
00367 ClassIndex index;
00368 ClassKey key;
00369
00370 fillin_pkey(signatures[i], loader_index, &key);
00371 index = find_or_create_entry(&key);
00372 info = get_info(index);
00373 info->status |= CLASS_SYSTEM;
00374 }
00375 }
00376
00377 void
00378 class_add_status(ClassIndex index, ClassStatus status)
00379 {
00380 ClassInfo *info;
00381
00382 info = get_info(index);
00383 info->status |= status;
00384 }
00385
00386 ClassStatus
00387 class_get_status(ClassIndex index)
00388 {
00389 ClassInfo *info;
00390
00391 info = get_info(index);
00392 return info->status;
00393 }
00394
00395 StringIndex
00396 class_get_signature(ClassIndex index)
00397 {
00398 ClassKey *pkey;
00399
00400 pkey = get_pkey(index);
00401 return pkey->sig_string_index;
00402 }
00403
00404 SerialNumber
00405 class_get_serial_number(ClassIndex index)
00406 {
00407 ClassInfo *info;
00408
00409 if ( index == 0 ) {
00410 return 0;
00411 }
00412 info = get_info(index);
00413 return info->serial_num;
00414 }
00415
00416 void
00417 class_all_status_remove(ClassStatus status)
00418 {
00419 table_walk_items(gdata->class_table, &all_status_remove,
00420 (void*)(ptrdiff_t)(long)status);
00421 }
00422
00423 void
00424 class_do_unloads(JNIEnv *env)
00425 {
00426 table_walk_items(gdata->class_table, &unload_walker, (void*)env);
00427 }
00428
00429 void
00430 class_list(void)
00431 {
00432 debug_message(
00433 "--------------------- Class Table ------------------------\n");
00434 table_walk_items(gdata->class_table, &list_item, NULL);
00435 debug_message(
00436 "----------------------------------------------------------\n");
00437 }
00438
00439 void
00440 class_cleanup(void)
00441 {
00442 table_cleanup(gdata->class_table, &cleanup_item, NULL);
00443 gdata->class_table = NULL;
00444 }
00445
00446 void
00447 class_delete_global_references(JNIEnv* env)
00448 {
00449 table_walk_items(gdata->class_table, &delete_ref_item, (void*)env);
00450 }
00451
00452 void
00453 class_set_methods(ClassIndex index, const char **name, const char **sig,
00454 int count)
00455 {
00456 ClassInfo *info;
00457 int i;
00458
00459 info = get_info(index);
00460 if ( info->method_count > 0 ) {
00461 HPROF_FREE((void*)info->method);
00462 info->method_count = 0;
00463 info->method = NULL;
00464 }
00465 info->method_count = count;
00466 if ( count > 0 ) {
00467 info->method = (MethodInfo *)HPROF_MALLOC(count*(int)sizeof(MethodInfo));
00468 for ( i = 0 ; i < count ; i++ ) {
00469 info->method[i].name_index = string_find_or_create(name[i]);
00470 info->method[i].sig_index = string_find_or_create(sig[i]);
00471 info->method[i].method_id = NULL;
00472 }
00473 }
00474 }
00475
00476 jclass
00477 class_new_classref(JNIEnv *env, ClassIndex index, jclass classref)
00478 {
00479 ClassInfo *info;
00480
00481 HPROF_ASSERT(classref!=NULL);
00482 info = get_info(index);
00483 if ( ! isSameObject(env, classref, info->classref) ) {
00484 delete_classref(env, info, classref);
00485 }
00486 return info->classref;
00487 }
00488
00489 jclass
00490 class_get_class(JNIEnv *env, ClassIndex index)
00491 {
00492 ClassInfo *info;
00493 jclass clazz;
00494
00495 info = get_info(index);
00496 clazz = info->classref;
00497 if ( env != NULL && clazz == NULL ) {
00498 WITH_LOCAL_REFS(env, 1) {
00499 jclass new_clazz;
00500 char *class_name;
00501
00502 class_name = string_get(info->name);
00503
00504
00505
00506
00507 new_clazz = findClass(env, class_name);
00508 if ( new_clazz == NULL ) {
00509 HPROF_ERROR(JNI_TRUE, "Cannot load class with findClass");
00510 }
00511 HPROF_ASSERT(new_clazz!=NULL);
00512 clazz = class_new_classref(env, index, new_clazz);
00513 } END_WITH_LOCAL_REFS;
00514 HPROF_ASSERT(clazz!=NULL);
00515 }
00516 return clazz;
00517 }
00518
00519 jmethodID
00520 class_get_methodID(JNIEnv *env, ClassIndex index, MethodIndex mnum)
00521 {
00522 ClassInfo *info;
00523 jmethodID method;
00524
00525 info = get_info(index);
00526 HPROF_ASSERT(mnum < info->method_count);
00527 method = info->method[mnum].method_id;
00528 if ( method == NULL ) {
00529 char * name;
00530 char * sig;
00531 jclass clazz;
00532
00533 name = (char *)string_get(info->method[mnum].name_index);
00534 HPROF_ASSERT(name!=NULL);
00535 sig = (char *)string_get(info->method[mnum].sig_index);
00536 HPROF_ASSERT(sig!=NULL);
00537 clazz = class_get_class(env, index);
00538 if ( clazz != NULL ) {
00539 method = getMethodID(env, clazz, name, sig);
00540 HPROF_ASSERT(method!=NULL);
00541 info = get_info(index);
00542 info->method[mnum].method_id = method;
00543 }
00544 }
00545 return method;
00546 }
00547
00548 void
00549 class_set_inst_size(ClassIndex index, jint inst_size)
00550 {
00551 ClassInfo *info;
00552
00553 info = get_info(index);
00554 info->inst_size = inst_size;
00555 }
00556
00557 jint
00558 class_get_inst_size(ClassIndex index)
00559 {
00560 ClassInfo *info;
00561
00562 info = get_info(index);
00563 return info->inst_size;
00564 }
00565
00566 void
00567 class_set_object_index(ClassIndex index, ObjectIndex object_index)
00568 {
00569 ClassInfo *info;
00570
00571 info = get_info(index);
00572 info->object_index = object_index;
00573 }
00574
00575 ObjectIndex
00576 class_get_object_index(ClassIndex index)
00577 {
00578 ClassInfo *info;
00579
00580 info = get_info(index);
00581 return info->object_index;
00582 }
00583
00584 ClassIndex
00585 class_get_super(ClassIndex index)
00586 {
00587 ClassInfo *info;
00588
00589 info = get_info(index);
00590 return info->super;
00591 }
00592
00593 void
00594 class_set_super(ClassIndex index, ClassIndex super)
00595 {
00596 ClassInfo *info;
00597
00598 info = get_info(index);
00599 info->super = super;
00600 }
00601
00602 LoaderIndex
00603 class_get_loader(ClassIndex index)
00604 {
00605 ClassKey *pkey;
00606
00607 pkey = get_pkey(index);
00608 HPROF_ASSERT(pkey->loader_index!=0);
00609 return pkey->loader_index;
00610 }
00611
00612
00613 jint
00614 class_get_all_fields(JNIEnv *env, ClassIndex index,
00615 jint *pfield_count, FieldInfo **pfield)
00616 {
00617 ClassInfo *info;
00618 FieldInfo *finfo;
00619 jint count;
00620 jint ret;
00621
00622 count = 0;
00623 finfo = NULL;
00624 ret = 1;
00625
00626 info = get_info(index);
00627 if ( info != NULL ) {
00628 if ( info->field_count >= 0 ) {
00629
00630 count = info->field_count;
00631 finfo = info->field;
00632 ret = 0;
00633 } else {
00634 jclass klass;
00635
00636 klass = info->classref;
00637 if ( klass == NULL || isSameObject(env, klass, NULL) ) {
00638
00639
00640
00641
00642
00643
00644
00645
00646
00647 HPROF_ERROR(JNI_FALSE, "Missing jclass when fields needed");
00648 } else {
00649 jint status;
00650
00651 status = getClassStatus(klass);
00652 if ( status &
00653 (JVMTI_CLASS_STATUS_PRIMITIVE|JVMTI_CLASS_STATUS_ARRAY) ) {
00654
00655 info->field_count = count;
00656 info->field = finfo;
00657 ret = 0;
00658 } else if ( status & JVMTI_CLASS_STATUS_PREPARED ) {
00659
00660 getAllClassFieldInfo(env, klass, &count, &finfo);
00661
00662 info->field_count = count;
00663 info->field = finfo;
00664 ret = 0;
00665 }
00666 }
00667 }
00668 }
00669 *pfield_count = count;
00670 *pfield = finfo;
00671 return ret;
00672 }
00673