1 /*
   2  * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
   3  * Copyright (c) 2018, Google and/or its affiliates. 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 #include <assert.h>
  26 #include <stdio.h>
  27 #include <stdlib.h>
  28 #include <string.h>
  29 #include "jvmti.h"
  30 
  31 #ifdef __cplusplus
  32 extern "C" {
  33 #endif
  34 
  35 #ifndef JNI_ENV_ARG
  36 
  37 #ifdef __cplusplus
  38 #define JNI_ENV_ARG(x)
  39 #define JNI_ENV_ARG2(x, y) y
  40 #define JNI_ENV_ARG3(x, y, z) y, z
  41 #define JNI_ENV_ARG4(x, y, z, w) y, z, w
  42 #define JNI_ENV_PTR(x) x
  43 #else
  44 #define JNI_ENV_ARG(x) x
  45 #define JNI_ENV_ARG2(x, y) x, y
  46 #define JNI_ENV_ARG3(x, y, z) x, y, z
  47 #define JNI_ENV_ARG4(x, y, z, w) x, y, z, w
  48 #define JNI_ENV_PTR(x) (*x)
  49 #endif
  50 
  51 #endif
  52 
  53 #define TRUE 1
  54 #define FALSE 0
  55 #define PRINT_OUT 0
  56 
  57 static jvmtiEnv *jvmti = NULL;
  58 static jvmtiEnv *second_jvmti = NULL;
  59 
  60 typedef struct _ObjectTrace{
  61   jweak object;
  62   jlong size;
  63   jvmtiFrameInfo* frames;
  64   size_t frame_count;
  65   jthread thread;
  66 } ObjectTrace;
  67 
  68 typedef struct _EventStorage {
  69   int live_object_additions;
  70   int live_object_size;
  71   int live_object_count;
  72   ObjectTrace** live_objects;
  73 
  74   int garbage_history_size;
  75   int garbage_history_index;
  76   ObjectTrace** garbage_collected_objects;
  77 
  78   // Two separate monitors to separate storage data race and the compaction field
  79   // data race.
  80   jrawMonitorID storage_monitor;
  81 
  82   int compaction_required;
  83   jrawMonitorID compaction_monitor;
  84 } EventStorage;
  85 
  86 typedef struct _ExpectedContentFrame {
  87   const char *name;
  88   const char *signature;
  89   const char *file_name;
  90   int line_number;
  91 } ExpectedContentFrame;
  92 
  93 static
  94 void event_storage_lock(EventStorage* storage) {
  95   (*jvmti)->RawMonitorEnter(jvmti, storage->storage_monitor);
  96 }
  97 
  98 static
  99 void event_storage_unlock(EventStorage* storage) {
 100   (*jvmti)->RawMonitorExit(jvmti, storage->storage_monitor);
 101 }
 102 
 103 static
 104 void event_storage_lock_compaction(EventStorage* storage) {
 105   (*jvmti)->RawMonitorEnter(jvmti, storage->compaction_monitor);
 106 }
 107 
 108 static
 109 void event_storage_unlock_compaction(EventStorage* storage) {
 110   (*jvmti)->RawMonitorExit(jvmti, storage->compaction_monitor);
 111 }
 112 
 113 // Given a method and a location, this method gets the line number.
 114 static
 115 jint get_line_number(jvmtiEnv* jvmti, jmethodID method,
 116                      jlocation location) {
 117   // Read the line number table.
 118   jvmtiLineNumberEntry *table_ptr = 0;
 119   jint line_number_table_entries;
 120   int l;
 121   jlocation last_location;
 122   int jvmti_error = (*jvmti)->GetLineNumberTable(jvmti, method,
 123                                                  &line_number_table_entries,
 124                                                  &table_ptr);
 125 
 126   if (JVMTI_ERROR_NONE != jvmti_error) {
 127     return -1;
 128   }
 129   if (line_number_table_entries <= 0) {
 130     return -1;
 131   }
 132   if (line_number_table_entries == 1) {
 133     return table_ptr[0].line_number;
 134   }
 135 
 136   // Go through all the line numbers...
 137   last_location = table_ptr[0].start_location;
 138   for (l = 1; l < line_number_table_entries; l++) {
 139     // ... and if you see one that is in the right place for your
 140     // location, you've found the line number!
 141     if ((location < table_ptr[l].start_location) &&
 142         (location >= last_location)) {
 143       return table_ptr[l - 1].line_number;
 144     }
 145     last_location = table_ptr[l].start_location;
 146   }
 147 
 148   if (location >= last_location) {
 149     return table_ptr[line_number_table_entries - 1].line_number;
 150   } else {
 151     return -1;
 152   }
 153 }
 154 
 155 static void print_out_frames(JNIEnv* env, ObjectTrace* trace) {
 156   jvmtiFrameInfo* frames = trace->frames;
 157   size_t i;
 158   for (i = 0; i < trace->frame_count; i++) {
 159     // Get basic information out of the trace.
 160     jlocation bci = frames[i].location;
 161     jmethodID methodid = frames[i].method;
 162     char *name = NULL, *signature = NULL, *file_name = NULL;
 163     jclass declaring_class;
 164     int line_number;
 165     jvmtiError err;
 166 
 167     if (bci < 0) {
 168       fprintf(stderr, "\tNative frame\n");
 169       continue;
 170     }
 171 
 172     // Transform into usable information.
 173     line_number = get_line_number(jvmti, methodid, bci);
 174     if (JVMTI_ERROR_NONE !=
 175         (*jvmti)->GetMethodName(jvmti, methodid, &name, &signature, 0)) {
 176       fprintf(stderr, "\tUnknown method name\n");
 177       continue;
 178     }
 179 
 180     if (JVMTI_ERROR_NONE !=
 181         (*jvmti)->GetMethodDeclaringClass(jvmti, methodid, &declaring_class)) {
 182       fprintf(stderr, "\tUnknown class\n");
 183       continue;
 184     }
 185 
 186     err = (*jvmti)->GetSourceFileName(jvmti, declaring_class,
 187                                       &file_name);
 188     if (err != JVMTI_ERROR_NONE) {
 189       fprintf(stderr, "\tUnknown file\n");
 190       continue;
 191     }
 192 
 193     // Compare now, none should be NULL.
 194     if (name == NULL) {
 195       fprintf(stderr, "\tUnknown name\n");
 196       continue;
 197     }
 198 
 199     if (file_name == NULL) {
 200       fprintf(stderr, "\tUnknown file\n");
 201       continue;
 202     }
 203 
 204     if (signature == NULL) {
 205       fprintf(stderr, "\tUnknown signature\n");
 206       continue;
 207     }
 208 
 209     fprintf(stderr, "\t%s%s (%s: %d)\n",
 210             name, signature, file_name, line_number);
 211   }
 212 }
 213 
 214 static jboolean check_sample_content(JNIEnv* env,
 215                                      ObjectTrace* trace,
 216                                      ExpectedContentFrame *expected,
 217                                      size_t expected_count,
 218                                      jboolean check_lines,
 219                                      int print_out_comparisons) {
 220   jvmtiFrameInfo* frames;
 221   size_t i;
 222 
 223   if (expected_count > trace->frame_count) {
 224     return FALSE;
 225   }
 226 
 227   frames = trace->frames;
 228   for (i = 0; i < expected_count; i++) {
 229     // Get basic information out of the trace.
 230     jlocation bci = frames[i].location;
 231     jmethodID methodid = frames[i].method;
 232     char *name = NULL, *signature = NULL, *file_name = NULL;
 233     jclass declaring_class;
 234     int line_number;
 235     jboolean differ;
 236     jvmtiError err;
 237 
 238     if (bci < 0 && expected[i].line_number != -1) {
 239       return FALSE;
 240     }
 241 
 242     // Transform into usable information.
 243     line_number = get_line_number(jvmti, methodid, bci);
 244     (*jvmti)->GetMethodName(jvmti, methodid, &name, &signature, 0);
 245 
 246     if (JVMTI_ERROR_NONE !=
 247         (*jvmti)->GetMethodDeclaringClass(jvmti, methodid, &declaring_class)) {
 248       return FALSE;
 249     }
 250 
 251     err = (*jvmti)->GetSourceFileName(jvmti, declaring_class,
 252                                       &file_name);
 253     if (err != JVMTI_ERROR_NONE) {
 254       return FALSE;
 255     }
 256 
 257     // Compare now, none should be NULL.
 258     if (name == NULL) {
 259       return FALSE;
 260     }
 261 
 262     if (file_name == NULL) {
 263       return FALSE;
 264     }
 265 
 266     if (signature == NULL) {
 267       return FALSE;
 268     }
 269 
 270     differ = (strcmp(name, expected[i].name) ||
 271               strcmp(signature, expected[i].signature) ||
 272               strcmp(file_name, expected[i].file_name) ||
 273               (check_lines && line_number != expected[i].line_number));
 274 
 275     if (print_out_comparisons) {
 276       fprintf(stderr, "\tComparing: (check_lines: %d)\n", check_lines);
 277       fprintf(stderr, "\t\tNames: %s and %s\n", name, expected[i].name);
 278       fprintf(stderr, "\t\tSignatures: %s and %s\n", signature, expected[i].signature);
 279       fprintf(stderr, "\t\tFile name: %s and %s\n", file_name, expected[i].file_name);
 280       fprintf(stderr, "\t\tLines: %d and %d\n", line_number, expected[i].line_number);
 281       fprintf(stderr, "\t\tResult is %d\n", differ);
 282     }
 283 
 284     if (differ) {
 285       return FALSE;
 286     }
 287   }
 288 
 289   return TRUE;
 290 }
 291 
 292 // Static native API for various tests.
 293 static int fill_native_frames(JNIEnv* env, jobjectArray frames,
 294                               ExpectedContentFrame* native_frames, size_t size) {
 295   size_t i;
 296   for (i = 0; i < size; i++) {
 297     jclass frame_class = NULL;
 298     jfieldID line_number_field_id = 0;
 299     int line_number = 0;
 300     jfieldID string_id = 0;
 301     jstring string_object = NULL;
 302     const char* method = NULL;
 303     const char* file_name = NULL;
 304     const char* signature = NULL;
 305 
 306     jobject obj = JNI_ENV_PTR(env)->GetObjectArrayElement(
 307         JNI_ENV_ARG3(env, frames, (jsize) i));
 308 
 309     if (JNI_ENV_PTR(env)->ExceptionOccurred(JNI_ENV_ARG(env))) {
 310       fprintf(stderr, "fill_native_frames: Exception in jni GetObjectArrayElement\n");
 311       return -1;
 312     }
 313 
 314     frame_class = JNI_ENV_PTR(env)->GetObjectClass(JNI_ENV_ARG2(env, obj));
 315 
 316     if (JNI_ENV_PTR(env)->ExceptionOccurred(JNI_ENV_ARG(env))) {
 317       fprintf(stderr, "fill_native_frames: Exception in jni GetObjectClass\n");
 318       return -1;
 319     }
 320 
 321     line_number_field_id =
 322         JNI_ENV_PTR(env)->GetFieldID(JNI_ENV_ARG4(env, frame_class, "lineNumber", "I"));
 323 
 324     if (JNI_ENV_PTR(env)->ExceptionOccurred(JNI_ENV_ARG(env))) {
 325       fprintf(stderr, "fill_native_frames: Exception in jni GetFieldID\n");
 326       return -1;
 327     }
 328 
 329     line_number = JNI_ENV_PTR(env)->GetIntField(JNI_ENV_ARG3(env, obj, line_number_field_id));
 330 
 331     if (JNI_ENV_PTR(env)->ExceptionOccurred(JNI_ENV_ARG(env))) {
 332       fprintf(stderr, "fill_native_frames: Exception in jni GetIntField\n");
 333       return -1;
 334     }
 335 
 336     string_id = JNI_ENV_PTR(env)->GetFieldID(
 337         JNI_ENV_ARG4(env, frame_class, "method", "Ljava/lang/String;"));
 338 
 339     if (JNI_ENV_PTR(env)->ExceptionOccurred(JNI_ENV_ARG(env))) {
 340       fprintf(stderr, "fill_native_frames: Exception in jni GetFieldID\n");
 341       return -1;
 342     }
 343 
 344     string_object = (jstring) JNI_ENV_PTR(env)->GetObjectField(
 345         JNI_ENV_ARG3(env, obj, string_id));
 346 
 347     if (JNI_ENV_PTR(env)->ExceptionOccurred(JNI_ENV_ARG(env))) {
 348       fprintf(stderr, "fill_native_frames: Exception in jni GetObjectField\n");
 349       return -1;
 350     }
 351 
 352     method = JNI_ENV_PTR(env)->GetStringUTFChars(JNI_ENV_ARG3(env, string_object, 0));
 353 
 354     if (JNI_ENV_PTR(env)->ExceptionOccurred(JNI_ENV_ARG(env))) {
 355       fprintf(stderr, "Exception in jni GetStringUTFChars\n");
 356       return -1;
 357     }
 358 
 359     string_id = JNI_ENV_PTR(env)->GetFieldID(
 360         JNI_ENV_ARG4(env, frame_class, "fileName", "Ljava/lang/String;"));
 361 
 362     if (JNI_ENV_PTR(env)->ExceptionOccurred(JNI_ENV_ARG(env))) {
 363       fprintf(stderr, "Exception in jni GetFieldID\n");
 364       return -1;
 365     }
 366 
 367     string_object =
 368         (jstring) (JNI_ENV_PTR(env)->GetObjectField(
 369             JNI_ENV_ARG3(env, obj, string_id)));
 370 
 371     if (JNI_ENV_PTR(env)->ExceptionOccurred(JNI_ENV_ARG(env))) {
 372       fprintf(stderr, "fill_native_frames: Exception in second jni GetObjectField\n");
 373       return -1;
 374     }
 375 
 376     file_name = JNI_ENV_PTR(env)->GetStringUTFChars(
 377         JNI_ENV_ARG3(env, string_object, 0));
 378 
 379     if (JNI_ENV_PTR(env)->ExceptionOccurred(JNI_ENV_ARG(env))) {
 380       fprintf(stderr, "fill_native_frames: Exception in jni GetStringUTFChars\n");
 381       return -1;
 382     }
 383 
 384     string_id = JNI_ENV_PTR(env)->GetFieldID(
 385         JNI_ENV_ARG4(env, frame_class, "signature", "Ljava/lang/String;"));
 386 
 387     if (JNI_ENV_PTR(env)->ExceptionOccurred(JNI_ENV_ARG(env))) {
 388       fprintf(stderr, "fill_native_frames: Exception in second jni GetFieldID\n");
 389       return -1;
 390     }
 391 
 392     string_object =
 393         (jstring) (JNI_ENV_PTR(env)->GetObjectField(
 394             JNI_ENV_ARG3(env, obj, string_id)));
 395 
 396     if (JNI_ENV_PTR(env)->ExceptionOccurred(JNI_ENV_ARG(env))) {
 397       fprintf(stderr, "fill_native_frames: Exception in third jni GetObjectField\n");
 398       return -1;
 399     }
 400 
 401     signature = JNI_ENV_PTR(env)->GetStringUTFChars(
 402         JNI_ENV_ARG3(env, string_object, 0));
 403 
 404     if (JNI_ENV_PTR(env)->ExceptionOccurred(JNI_ENV_ARG(env))) {
 405       fprintf(stderr, "fill_native_frames: Exception in jni GetStringUTFChars\n");
 406       return -1;
 407     }
 408 
 409     native_frames[i].name = method;
 410     native_frames[i].file_name = file_name;
 411     native_frames[i].signature = signature;
 412     native_frames[i].line_number = line_number;
 413   }
 414 
 415   return 0;
 416 }
 417 
 418 // Internal storage system implementation.
 419 static EventStorage global_event_storage;
 420 static EventStorage second_global_event_storage;
 421 
 422 static void event_storage_set_compaction_required(EventStorage* storage) {
 423   event_storage_lock_compaction(storage);
 424   storage->compaction_required = 1;
 425   event_storage_unlock_compaction(storage);
 426 }
 427 
 428 static int event_storage_get_compaction_required(EventStorage* storage) {
 429   int result;
 430   event_storage_lock_compaction(storage);
 431   result = storage->compaction_required;
 432   event_storage_unlock_compaction(storage);
 433   return result;
 434 }
 435 
 436 static void event_storage_set_garbage_history(EventStorage* storage, int value) {
 437   size_t size;
 438   event_storage_lock(storage);
 439   global_event_storage.garbage_history_size = value;
 440   free(global_event_storage.garbage_collected_objects);
 441   size = sizeof(*global_event_storage.garbage_collected_objects) * value;
 442   global_event_storage.garbage_collected_objects = malloc(size);
 443   memset(global_event_storage.garbage_collected_objects, 0, size);
 444   event_storage_unlock(storage);
 445 }
 446 
 447 // No mutex here, it is handled by the caller.
 448 static void event_storage_add_garbage_collected_object(EventStorage* storage,
 449                                                        ObjectTrace* object) {
 450   int idx = storage->garbage_history_index;
 451   ObjectTrace* old_object = storage->garbage_collected_objects[idx];
 452   if (old_object != NULL) {
 453     free(old_object->frames);
 454     free(storage->garbage_collected_objects[idx]);
 455   }
 456 
 457   storage->garbage_collected_objects[idx] = object;
 458   storage->garbage_history_index = (idx + 1) % storage->garbage_history_size;
 459 }
 460 
 461 static int event_storage_get_count(EventStorage* storage) {
 462   int result;
 463   event_storage_lock(storage);
 464   result = storage->live_object_count;
 465   event_storage_unlock(storage);
 466   return result;
 467 }
 468 
 469 static double event_storage_get_average_size(EventStorage* storage) {
 470   double accumulation = 0;
 471   int max_size;
 472   int i;
 473 
 474   event_storage_lock(storage);
 475   max_size = storage->live_object_count;
 476 
 477   for (i = 0; i < max_size; i++) {
 478     accumulation += storage->live_objects[i]->size;
 479   }
 480 
 481   event_storage_unlock(storage);
 482   return accumulation / max_size;
 483 }
 484 
 485 static jboolean event_storage_contains(JNIEnv* env,
 486                                        EventStorage* storage,
 487                                        ExpectedContentFrame* frames,
 488                                        size_t size,
 489                                        jboolean check_lines) {
 490   int i;
 491   event_storage_lock(storage);
 492   fprintf(stderr, "Checking storage count %d\n", storage->live_object_count);
 493   for (i = 0; i < storage->live_object_count; i++) {
 494     ObjectTrace* trace = storage->live_objects[i];
 495 
 496     if (check_sample_content(env, trace, frames, size, check_lines, PRINT_OUT)) {
 497       event_storage_unlock(storage);
 498       return TRUE;
 499     }
 500   }
 501   event_storage_unlock(storage);
 502   return FALSE;
 503 }
 504 
 505 static jboolean event_storage_garbage_contains(JNIEnv* env,
 506                                                EventStorage* storage,
 507                                                ExpectedContentFrame* frames,
 508                                                size_t size,
 509                                                jboolean check_lines) {
 510   int i;
 511   event_storage_lock(storage);
 512   fprintf(stderr, "Checking garbage storage count %d\n",
 513           storage->garbage_history_size);
 514   for (i = 0; i < storage->garbage_history_size; i++) {
 515     ObjectTrace* trace = storage->garbage_collected_objects[i];
 516 
 517     if (trace == NULL) {
 518       continue;
 519     }
 520 
 521     if (check_sample_content(env, trace, frames, size, check_lines, PRINT_OUT)) {
 522       event_storage_unlock(storage);
 523       return TRUE;
 524     }
 525   }
 526   event_storage_unlock(storage);
 527   return FALSE;
 528 }
 529 
 530 // No mutex here, handled by the caller.
 531 static void event_storage_augment_storage(EventStorage* storage) {
 532   int new_max = (storage->live_object_size * 2) + 1;
 533   ObjectTrace** new_objects = malloc(new_max * sizeof(*new_objects));
 534 
 535   int current_count = storage->live_object_count;
 536   memcpy(new_objects, storage->live_objects, current_count * sizeof(*new_objects));
 537   free(storage->live_objects);
 538   storage->live_objects = new_objects;
 539   storage->live_object_size = new_max;
 540 }
 541 
 542 static void event_storage_add(EventStorage* storage,
 543                               JNIEnv* jni,
 544                               jthread thread,
 545                               jobject object,
 546                               jclass klass,
 547                               jlong size) {
 548   jvmtiFrameInfo frames[64];
 549   jint count;
 550   jvmtiError err;
 551 
 552   err = (*jvmti)->GetStackTrace(jvmti, thread, 0, 64, frames, &count);
 553   if (err == JVMTI_ERROR_NONE && count >= 1) {
 554     ObjectTrace* live_object;
 555     jvmtiFrameInfo* allocated_frames = (jvmtiFrameInfo*) malloc(count * sizeof(*allocated_frames));
 556     memcpy(allocated_frames, frames, count * sizeof(*allocated_frames));
 557 
 558     live_object = (ObjectTrace*) malloc(sizeof(*live_object));
 559     live_object->frames = allocated_frames;
 560     live_object->frame_count = count;
 561     live_object->size = size;
 562     live_object->thread = thread;
 563     live_object->object = (*jni)->NewWeakGlobalRef(jni, object);
 564 
 565     if (JNI_ENV_PTR(jni)->ExceptionOccurred(JNI_ENV_ARG(jni))) {
 566       JNI_ENV_PTR(jni)->FatalError(
 567           JNI_ENV_ARG2(jni, "Error in event_storage_add: Exception in jni NewWeakGlobalRef"));
 568     }
 569 
 570     // Only now lock and get things done quickly.
 571     event_storage_lock(storage);
 572 
 573     storage->live_object_additions++;
 574 
 575     if (storage->live_object_count >= storage->live_object_size) {
 576       event_storage_augment_storage(storage);
 577     }
 578     assert(storage->live_object_count < storage->live_object_size);
 579 
 580     if (PRINT_OUT) {
 581       fprintf(stderr, "Adding trace for thread %p, frame_count %d, storage %p\n",
 582               thread, count, storage);
 583       print_out_frames(jni, live_object);
 584     }
 585     storage->live_objects[storage->live_object_count] = live_object;
 586     storage->live_object_count++;
 587 
 588     event_storage_unlock(storage);
 589   }
 590 }
 591 
 592 static void event_storage_compact(EventStorage* storage, JNIEnv* jni) {
 593   int max, i, dest;
 594   ObjectTrace** live_objects;
 595 
 596   event_storage_lock_compaction(storage);
 597   storage->compaction_required = 0;
 598   event_storage_unlock_compaction(storage);
 599 
 600   event_storage_lock(storage);
 601 
 602   max = storage->live_object_count;
 603   live_objects = storage->live_objects;
 604 
 605   for (i = 0, dest = 0; i < max; i++) {
 606     ObjectTrace* live_object = live_objects[i];
 607     jweak object = live_object->object;
 608 
 609     if (!(*jni)->IsSameObject(jni, object, NULL)) {
 610       if (dest != i) {
 611         live_objects[dest] = live_object;
 612         dest++;
 613       }
 614     } else {
 615       (*jni)->DeleteWeakGlobalRef(jni, object);
 616       live_object->object = NULL;
 617 
 618       event_storage_add_garbage_collected_object(storage, live_object);
 619     }
 620   }
 621 
 622   storage->live_object_count = dest;
 623   event_storage_unlock(storage);
 624 }
 625 
 626 static void event_storage_free_objects(ObjectTrace** array, int max) {
 627   int i;
 628   for (i = 0; i < max; i++) {
 629     free(array[i]), array[i] = NULL;
 630   }
 631 }
 632 
 633 static void event_storage_reset(EventStorage* storage) {
 634   event_storage_lock(storage);
 635 
 636   // Reset everything except the mutex and the garbage collection.
 637   event_storage_free_objects(storage->live_objects,
 638                              storage->live_object_count);
 639   storage->live_object_additions = 0;
 640   storage->live_object_size = 0;
 641   storage->live_object_count = 0;
 642   free(storage->live_objects), storage->live_objects = NULL;
 643 
 644   event_storage_free_objects(storage->garbage_collected_objects,
 645                              storage->garbage_history_size);
 646 
 647   storage->compaction_required = 0;
 648   storage->garbage_history_index = 0;
 649 
 650   event_storage_unlock(storage);
 651 }
 652 
 653 static int event_storage_number_additions(EventStorage* storage) {
 654   int result;
 655   event_storage_lock(storage);
 656   result = storage->live_object_additions;
 657   event_storage_unlock(storage);
 658   return result;
 659 }
 660 
 661 // Start of the JVMTI agent code.
 662 static const char *EXC_CNAME = "java/lang/Exception";
 663 
 664 static int check_error(jvmtiError err, const char *s) {
 665   if (err != JVMTI_ERROR_NONE) {
 666     printf("  ## %s error: %d\n", s, err);
 667     return 1;
 668   }
 669   return 0;
 670 }
 671 
 672 static int check_capability_error(jvmtiError err, const char *s) {
 673   if (err != JVMTI_ERROR_NONE) {
 674     if (err == JVMTI_ERROR_MUST_POSSESS_CAPABILITY) {
 675       return 0;
 676     }
 677     fprintf(stderr, "  ## %s error: %d\n", s, err);
 678   }
 679   return 1;
 680 }
 681 
 682 static jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved);
 683 
 684 JNIEXPORT
 685 jint JNICALL Agent_OnLoad(JavaVM *jvm, char *options, void *reserved) {
 686   return Agent_Initialize(jvm, options, reserved);
 687 }
 688 
 689 JNIEXPORT
 690 jint JNICALL Agent_OnAttach(JavaVM *jvm, char *options, void *reserved) {
 691   return Agent_Initialize(jvm, options, reserved);
 692 }
 693 
 694 JNIEXPORT
 695 jint JNICALL JNI_OnLoad(JavaVM *jvm, void *reserved) {
 696   return JNI_VERSION_1_8;
 697 }
 698 
 699 #define MAX_THREADS 500
 700 
 701 typedef struct ThreadStats {
 702   int number_threads;
 703   int counts[MAX_THREADS];
 704   char* threads[MAX_THREADS];
 705 } ThreadStats;
 706 
 707 static ThreadStats thread_stats;
 708 
 709 JNIEXPORT jboolean JNICALL
 710 Java_MyPackage_HeapMonitorThreadDisabledTest_checkThreadSamplesOnlyFrom(
 711     JNIEnv* env, jclass cls, jthread thread) {
 712   int i;
 713   jvmtiThreadInfo info;
 714   int err;
 715   char* expected_name;
 716   int found_thread = FALSE;
 717 
 718   err = (*jvmti)->GetThreadInfo(jvmti, thread, &info);
 719   expected_name = info.name;
 720 
 721   if (err != JVMTI_ERROR_NONE) {
 722     fprintf(stderr, "Failed to get thread information\n");
 723     return FALSE;
 724   }
 725 
 726   for (i = 0; i < thread_stats.number_threads; i++) {
 727     if (strcmp(expected_name, thread_stats.threads[i])) {
 728       return FALSE;
 729     } else {
 730       found_thread = TRUE;
 731     }
 732   }
 733   return found_thread;
 734 }
 735 
 736 static void add_thread_count(jthread thread) {
 737   int i;
 738   jvmtiThreadInfo info;
 739   const char* name;
 740   char* end;
 741   int idx;
 742   int err;
 743 
 744   err = (*jvmti)->GetThreadInfo(jvmti, thread, &info);
 745   if (err != JVMTI_ERROR_NONE) {
 746     return;
 747   }
 748 
 749   event_storage_lock(&global_event_storage);
 750   for (i = 0; i < thread_stats.number_threads; i++) {
 751     if (!strcmp(thread_stats.threads[i], info.name)) {
 752       thread_stats.counts[i]++;
 753       event_storage_unlock(&global_event_storage);
 754       return;
 755     }
 756   }
 757 
 758   thread_stats.threads[thread_stats.number_threads] = info.name;
 759   thread_stats.counts[thread_stats.number_threads]++;
 760   thread_stats.number_threads++;
 761   event_storage_unlock(&global_event_storage);
 762 }
 763 
 764 JNIEXPORT void JNICALL
 765 Java_MyPackage_HeapMonitorThreadDisabledTest_enableSamplingEvents(
 766     JNIEnv* env, jclass cls, jthread thread) {
 767   fprintf(stderr, "Enabling for %p\n", thread);
 768   check_error((*jvmti)->SetEventNotificationMode(
 769       jvmti, JVMTI_ENABLE, JVMTI_EVENT_SAMPLED_OBJECT_ALLOC, thread),
 770               "Set event notifications for a single thread");
 771 }
 772 
 773 static void print_thread_stats() {
 774   int i;
 775   event_storage_lock(&global_event_storage);
 776   fprintf(stderr, "Thread count:\n");
 777   for (i = 0; i < thread_stats.number_threads; i++) {
 778     fprintf(stderr, "\t%s: %d\n", thread_stats.threads[i], thread_stats.counts[i]);
 779   }
 780   event_storage_unlock(&global_event_storage);
 781 }
 782 
 783 JNIEXPORT
 784 void JNICALL SampledObjectAlloc(jvmtiEnv *jvmti_env,
 785                                 JNIEnv* jni_env,
 786                                 jthread thread,
 787                                 jobject object,
 788                                 jclass object_klass,
 789                                 jlong size) {
 790   add_thread_count(thread);
 791 
 792   if (event_storage_get_compaction_required(&global_event_storage)) {
 793     event_storage_compact(&global_event_storage, jni_env);
 794   }
 795 
 796   event_storage_add(&global_event_storage, jni_env, thread, object,
 797                     object_klass, size);
 798 }
 799 
 800 JNIEXPORT
 801 void JNICALL VMObjectAlloc(jvmtiEnv *jvmti_env,
 802                            JNIEnv* jni_env,
 803                            jthread thread,
 804                            jobject object,
 805                            jclass object_klass,
 806                            jlong size) {
 807   event_storage_add(&second_global_event_storage, jni_env, thread, object,
 808                     object_klass, size);
 809 }
 810 
 811 JNIEXPORT
 812 void JNICALL GarbageCollectionFinish(jvmtiEnv *jvmti_env) {
 813   event_storage_set_compaction_required(&global_event_storage);
 814 }
 815 
 816 static int enable_notifications() {
 817   if (check_error((*jvmti)->SetEventNotificationMode(
 818       jvmti, JVMTI_ENABLE, JVMTI_EVENT_GARBAGE_COLLECTION_FINISH, NULL),
 819                      "Set event notifications")) {
 820     return 1;
 821   }
 822 
 823   return check_error((*jvmti)->SetEventNotificationMode(
 824       jvmti, JVMTI_ENABLE, JVMTI_EVENT_SAMPLED_OBJECT_ALLOC, NULL),
 825                      "Set event notifications");
 826 }
 827 
 828 static
 829 jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {
 830   jint res;
 831   jvmtiEventCallbacks callbacks;
 832   jvmtiCapabilities caps;
 833 
 834   res = JNI_ENV_PTR(jvm)->GetEnv(JNI_ENV_ARG3(jvm, (void **) &jvmti,
 835                                  JVMTI_VERSION_9));
 836   if (res != JNI_OK || jvmti == NULL) {
 837     fprintf(stderr, "Error: wrong result of a valid call to GetEnv!\n");
 838     return JNI_ERR;
 839   }
 840 
 841   // Get second jvmti environment.
 842   res = JNI_ENV_PTR(jvm)->GetEnv(JNI_ENV_ARG3(jvm, (void **) &second_jvmti,
 843                                  JVMTI_VERSION_9));
 844   if (res != JNI_OK || second_jvmti == NULL) {
 845     fprintf(stderr, "Error: wrong result of a valid second call to GetEnv!\n");
 846     return JNI_ERR;
 847   }
 848 
 849   if (PRINT_OUT) {
 850     fprintf(stderr, "Storage is at %p, secondary is at %p\n",
 851             &global_event_storage, &second_global_event_storage);
 852   }
 853 
 854   (*jvmti)->CreateRawMonitor(jvmti, "storage_monitor",
 855                              &global_event_storage.storage_monitor);
 856   (*jvmti)->CreateRawMonitor(jvmti, "second_storage_monitor",
 857                              &second_global_event_storage.storage_monitor);
 858 
 859   (*jvmti)->CreateRawMonitor(jvmti, "compaction_monitor",
 860                              &global_event_storage.compaction_monitor);
 861   (*jvmti)->CreateRawMonitor(jvmti, "second_compaction_monitor",
 862                              &second_global_event_storage.compaction_monitor);
 863 
 864   event_storage_set_garbage_history(&global_event_storage, 200);
 865   event_storage_set_garbage_history(&second_global_event_storage, 200);
 866 
 867   memset(&callbacks, 0, sizeof(callbacks));
 868   callbacks.SampledObjectAlloc = &SampledObjectAlloc;
 869   callbacks.VMObjectAlloc = &VMObjectAlloc;
 870   callbacks.GarbageCollectionFinish = &GarbageCollectionFinish;
 871 
 872   memset(&caps, 0, sizeof(caps));
 873   // Get line numbers, sample events, filename, and gc events for the tests.
 874   caps.can_get_line_numbers = 1;
 875   caps.can_get_source_file_name = 1;
 876   caps.can_generate_garbage_collection_events = 1;
 877   caps.can_generate_sampled_object_alloc_events = 1;
 878   caps.can_generate_vm_object_alloc_events = 1;
 879   if (check_error((*jvmti)->AddCapabilities(jvmti, &caps), "Add capabilities")) {
 880     return JNI_ERR;
 881   }
 882 
 883   if (check_error((*jvmti)->SetEventCallbacks(jvmti, &callbacks,
 884                                               sizeof(jvmtiEventCallbacks)),
 885                   " Set Event Callbacks")) {
 886     return JNI_ERR;
 887   }
 888   return JNI_OK;
 889 }
 890 
 891 JNIEXPORT void JNICALL
 892 Java_MyPackage_HeapMonitor_setSamplingInterval(JNIEnv* env, jclass cls, jint value) {
 893   (*jvmti)->SetHeapSamplingInterval(jvmti, value);
 894 }
 895 
 896 JNIEXPORT jboolean JNICALL
 897 Java_MyPackage_HeapMonitor_eventStorageIsEmpty(JNIEnv* env, jclass cls) {
 898   return event_storage_get_count(&global_event_storage) == 0;
 899 }
 900 
 901 JNIEXPORT jint JNICALL
 902 Java_MyPackage_HeapMonitor_getEventStorageElementCount(JNIEnv* env, jclass cls) {
 903   return event_storage_get_count(&global_event_storage);
 904 }
 905 
 906 JNIEXPORT void JNICALL
 907 Java_MyPackage_HeapMonitor_enableSamplingEvents(JNIEnv* env, jclass cls) {
 908   enable_notifications();
 909 }
 910 
 911 JNIEXPORT void JNICALL
 912 Java_MyPackage_HeapMonitor_disableSamplingEvents(JNIEnv* env, jclass cls) {
 913   check_error((*jvmti)->SetEventNotificationMode(
 914       jvmti, JVMTI_DISABLE, JVMTI_EVENT_SAMPLED_OBJECT_ALLOC, NULL),
 915               "Set event notifications");
 916 
 917   check_error((*jvmti)->SetEventNotificationMode(
 918       jvmti, JVMTI_DISABLE, JVMTI_EVENT_GARBAGE_COLLECTION_FINISH, NULL),
 919               "Garbage Collection Finish");
 920 }
 921 
 922 JNIEXPORT jboolean JNICALL
 923 Java_MyPackage_HeapMonitor_obtainedEvents(JNIEnv* env, jclass cls,
 924                                           jobjectArray frames,
 925                                           jboolean check_lines) {
 926   jboolean result;
 927   jsize size = JNI_ENV_PTR(env)->GetArrayLength(JNI_ENV_ARG2(env, frames));
 928   ExpectedContentFrame *native_frames;
 929 
 930   if (JNI_ENV_PTR(env)->ExceptionOccurred(JNI_ENV_ARG(env))) {
 931     JNI_ENV_PTR(env)->FatalError(
 932         JNI_ENV_ARG2(env, "obtainedEvents failed with the GetArrayLength call"));
 933   }
 934 
 935   native_frames = malloc(size * sizeof(*native_frames));
 936 
 937   if (native_frames == NULL) {
 938     JNI_ENV_PTR(env)->FatalError(
 939         JNI_ENV_ARG2(env, "Error in obtainedEvents: malloc returned NULL for native_frames allocation\n"));
 940   }
 941 
 942   if (fill_native_frames(env, frames, native_frames, size) != 0) {
 943     JNI_ENV_PTR(env)->FatalError(
 944         JNI_ENV_ARG2(env, "Error in obtainedEvents: fill_native_frames returned failed status\n"));
 945   }
 946   result = event_storage_contains(env, &global_event_storage, native_frames,
 947                                   size, check_lines);
 948 
 949   free(native_frames), native_frames = NULL;
 950   return result;
 951 }
 952 
 953 JNIEXPORT jboolean JNICALL
 954 Java_MyPackage_HeapMonitor_garbageContains(JNIEnv* env, jclass cls,
 955                                            jobjectArray frames,
 956                                            jboolean check_lines) {
 957   jboolean result;
 958   jsize size = JNI_ENV_PTR(env)->GetArrayLength(JNI_ENV_ARG2(env, frames));
 959   ExpectedContentFrame *native_frames;
 960 
 961   if (JNI_ENV_PTR(env)->ExceptionOccurred(JNI_ENV_ARG(env))) {
 962     JNI_ENV_PTR(env)->FatalError(
 963         JNI_ENV_ARG2(env, "garbageContains failed with the GetArrayLength call"));
 964   }
 965 
 966   native_frames = malloc(size * sizeof(*native_frames));
 967 
 968   if (native_frames == NULL) {
 969     JNI_ENV_PTR(env)->FatalError(
 970         JNI_ENV_ARG2(env, "Error in garbageContains: malloc returned NULL for native_frames allocation\n"));
 971   }
 972 
 973   if (fill_native_frames(env, frames, native_frames, size) != 0) {
 974     JNI_ENV_PTR(env)->FatalError(
 975         JNI_ENV_ARG2(env, "Error in garbageContains: fill_native_frames returned failed status\n"));
 976   }
 977   result = event_storage_garbage_contains(env, &global_event_storage,
 978                                           native_frames, size, check_lines);
 979 
 980   free(native_frames), native_frames = NULL;
 981   return result;
 982 }
 983 
 984 JNIEXPORT void JNICALL
 985 Java_MyPackage_HeapMonitor_forceGarbageCollection(JNIEnv* env, jclass cls) {
 986   check_error((*jvmti)->ForceGarbageCollection(jvmti),
 987               "Forced Garbage Collection");
 988 }
 989 
 990 JNIEXPORT void JNICALL
 991 Java_MyPackage_HeapMonitor_resetEventStorage(JNIEnv* env, jclass cls) {
 992   event_storage_reset(&global_event_storage);
 993   event_storage_reset(&second_global_event_storage);
 994 }
 995 
 996 JNIEXPORT jboolean JNICALL
 997 Java_MyPackage_HeapMonitorNoCapabilityTest_allSamplingMethodsFail(JNIEnv *env,
 998                                                                   jclass cls) {
 999   jvmtiCapabilities caps;
1000   memset(&caps, 0, sizeof(caps));
1001   caps.can_generate_sampled_object_alloc_events = 1;
1002   if (check_error((*jvmti)->RelinquishCapabilities(jvmti, &caps),
1003                   "Add capabilities\n")){
1004     return FALSE;
1005   }
1006 
1007   if (check_capability_error((*jvmti)->SetHeapSamplingInterval(jvmti, 1<<19),
1008                              "Set Heap Sampling Interval")) {
1009     return FALSE;
1010   }
1011   return TRUE;
1012 }
1013 
1014 JNIEXPORT jboolean JNICALL
1015 Java_MyPackage_HeapMonitorIllegalArgumentTest_testIllegalArgument(JNIEnv *env,
1016                                                                   jclass cls) {
1017   if (check_error((*jvmti)->SetHeapSamplingInterval(jvmti, 0),
1018                   "Sampling interval 0 failed\n")){
1019     return FALSE;
1020   }
1021 
1022   if (check_error((*jvmti)->SetHeapSamplingInterval(jvmti, 1024),
1023                   "Sampling interval 1024 failed\n")){
1024     return FALSE;
1025   }
1026 
1027   if (!check_error((*jvmti)->SetHeapSamplingInterval(jvmti, -1),
1028                    "Sampling interval -1 passed\n")){
1029     return FALSE;
1030   }
1031 
1032   if (!check_error((*jvmti)->SetHeapSamplingInterval(jvmti, -1024),
1033                    "Sampling interval -1024 passed\n")){
1034     return FALSE;
1035   }
1036 
1037   return TRUE;
1038 }
1039 
1040 JNIEXPORT jdouble JNICALL
1041 Java_MyPackage_HeapMonitor_getAverageSize(JNIEnv *env, jclass cls) {
1042   return event_storage_get_average_size(&global_event_storage);
1043 }
1044 
1045 typedef struct sThreadsFound {
1046   jthread* threads;
1047   int num_threads;
1048 } ThreadsFound;
1049 
1050 JNIEXPORT jboolean JNICALL
1051 Java_MyPackage_HeapMonitorThreadTest_checkSamples(JNIEnv* env, jclass cls,
1052                                                   jint num_threads) {
1053   print_thread_stats();
1054   // Ensure we got stacks from at least num_threads.
1055   return thread_stats.number_threads >= num_threads;
1056 }
1057 
1058 JNIEXPORT
1059 void JNICALL SampledObjectAlloc2(jvmtiEnv *jvmti_env,
1060                                  JNIEnv* jni_env,
1061                                  jthread thread,
1062                                  jobject object,
1063                                  jclass object_klass,
1064                                  jlong size) {
1065   // Nop for now, two agents are not yet implemented.
1066   assert(0);
1067 }
1068 
1069 JNIEXPORT jboolean JNICALL
1070 Java_MyPackage_HeapMonitorTwoAgentsTest_enablingSamplingInSecondaryAgent(
1071     JNIEnv* env, jclass cls) {
1072   // Currently this method should be failing directly at the AddCapability step
1073   // but the implementation is correct for when multi-agent support is enabled.
1074   jvmtiCapabilities caps;
1075   jvmtiEventCallbacks callbacks;
1076 
1077   memset(&caps, 0, sizeof(caps));
1078   caps.can_generate_sampled_object_alloc_events = 1;
1079   if (check_error((*second_jvmti)->AddCapabilities(second_jvmti, &caps),
1080                   "Set the capability for second agent")) {
1081     return FALSE;
1082   }
1083 
1084   memset(&callbacks, 0, sizeof(callbacks));
1085   callbacks.SampledObjectAlloc = &SampledObjectAlloc2;
1086 
1087   if (check_error((*second_jvmti)->SetEventCallbacks(second_jvmti, &callbacks,
1088                                                      sizeof(jvmtiEventCallbacks)),
1089                   " Set Event Callbacks for second agent")) {
1090     return FALSE;
1091   }
1092 
1093   return TRUE;
1094 }
1095 
1096 JNIEXPORT void JNICALL
1097 Java_MyPackage_HeapMonitor_enableVMEvents(JNIEnv* env, jclass cls) {
1098   check_error((*jvmti)->SetEventNotificationMode(
1099       jvmti, JVMTI_ENABLE, JVMTI_EVENT_VM_OBJECT_ALLOC, NULL),
1100               "Set vm event notifications");
1101 }
1102 
1103 JNIEXPORT jint JNICALL
1104 Java_MyPackage_HeapMonitorVMEventsTest_vmEvents(JNIEnv* env, jclass cls) {
1105   return event_storage_number_additions(&second_global_event_storage);
1106 }
1107 
1108 JNIEXPORT jint JNICALL
1109 Java_MyPackage_HeapMonitor_sampledEvents(JNIEnv* env, jclass cls) {
1110   return event_storage_number_additions(&global_event_storage);
1111 }
1112 
1113 static jobject allocate_object(JNIEnv* env) {
1114   // Construct an Object.
1115   jclass cls = JNI_ENV_PTR(env)->FindClass(JNI_ENV_ARG2(env, "java/lang/Object"));
1116   jmethodID constructor;
1117   jobject result;
1118 
1119   if (JNI_ENV_PTR(env)->ExceptionOccurred(JNI_ENV_ARG(env)) || cls == NULL) {
1120     JNI_ENV_PTR(env)->FatalError(
1121         JNI_ENV_ARG2(env, "Error in jni FindClass: Cannot find Object class\n"));
1122   }
1123 
1124   constructor = JNI_ENV_PTR(env)->GetMethodID(JNI_ENV_ARG4(env, cls, "<init>", "()V"));
1125   if (JNI_ENV_PTR(env)->ExceptionOccurred(JNI_ENV_ARG(env)) || constructor == NULL) {
1126     JNI_ENV_PTR(env)->FatalError(
1127         JNI_ENV_ARG2(env, "Error in jni GetMethodID: Cannot find Object class constructor\n"));
1128   }
1129 
1130   // Call back constructor to allocate a new instance, with an int argument
1131   result = JNI_ENV_PTR(env)->NewObject(JNI_ENV_ARG3(env, cls, constructor));
1132 
1133   if (JNI_ENV_PTR(env)->ExceptionOccurred(JNI_ENV_ARG(env)) || result == NULL) {
1134     JNI_ENV_PTR(env)->FatalError(
1135         JNI_ENV_ARG2(env, "Error in jni NewObject: Cannot allocate an object\n"));
1136   }
1137   return result;
1138 }
1139 
1140 // Ensure we got a callback for the test.
1141 static int did_recursive_callback_test;
1142 
1143 JNIEXPORT
1144 void JNICALL RecursiveSampledObjectAlloc(jvmtiEnv *jvmti_env,
1145                                          JNIEnv* jni_env,
1146                                          jthread thread,
1147                                          jobject object,
1148                                          jclass object_klass,
1149                                          jlong size) {
1150   // Basically ensure that if we were to allocate objects, we would not have an
1151   // infinite recursion here.
1152   int i;
1153   for (i = 0; i < 1000; i++) {
1154     if (allocate_object(jni_env) == NULL) {
1155       JNI_ENV_PTR(jni_env)->FatalError(JNI_ENV_ARG2(jni_env, "allocate_object returned NULL\n"));
1156     }
1157   }
1158 
1159   did_recursive_callback_test = 1;
1160 }
1161 
1162 JNIEXPORT jboolean JNICALL
1163 Java_MyPackage_HeapMonitorRecursiveTest_didCallback(JNIEnv* env, jclass cls) {
1164   return did_recursive_callback_test != 0;
1165 }
1166 
1167 JNIEXPORT void JNICALL
1168 Java_MyPackage_HeapMonitorRecursiveTest_setCallbackToCallAllocateSomeMore(JNIEnv* env, jclass cls) {
1169   jvmtiEventCallbacks callbacks;
1170 
1171   memset(&callbacks, 0, sizeof(callbacks));
1172   callbacks.SampledObjectAlloc = &RecursiveSampledObjectAlloc;
1173 
1174   if (check_error((*jvmti)->SetEventCallbacks(jvmti, &callbacks,
1175                                               sizeof(jvmtiEventCallbacks)),
1176                   " Set Event Callbacks")) {
1177     JNI_ENV_PTR(env)->FatalError(JNI_ENV_ARG2(env, "Cannot reset the callback."));
1178   }
1179 }
1180 
1181 #ifdef __cplusplus
1182 }
1183 #endif