1 /*
   2  * Copyright (c) 2019, Red Hat, Inc. All rights reserved.
   3  *
   4  * This code is free software; you can redistribute it and/or modify it
   5  * under the terms of the GNU General Public License version 2 only, as
   6  * published by the Free Software Foundation.
   7  *
   8  * This code is distributed in the hope that it will be useful, but WITHOUT
   9  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  11  * version 2 for more details (a copy is included in the LICENSE file that
  12  * accompanied this code).
  13  *
  14  * You should have received a copy of the GNU General Public License version
  15  * 2 along with this work; if not, write to the Free Software Foundation,
  16  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  17  *
  18  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  19  * or visit www.oracle.com if you need additional information or have any
  20  * questions.
  21  *
  22  */
  23 
  24 #include <stdio.h>
  25 #include <string.h>
  26 #include "jvmti.h"
  27 
  28 #ifdef __cplusplus
  29 extern "C" {
  30 #endif
  31 
  32 #ifndef JNI_ENV_ARG
  33 
  34 #ifdef __cplusplus
  35 #define JNI_ENV_ARG(x, y) y
  36 #define JNI_ENV_PTR(x) x
  37 #else
  38 #define JNI_ENV_ARG(x,y) x, y
  39 #define JNI_ENV_PTR(x) (*x)
  40 #endif
  41 
  42 #endif
  43 
  44 static const char *EXC_CNAME = "java/lang/Exception";
  45 
  46 static jvmtiEnv *jvmti = NULL;
  47 
  48 static jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved);
  49 
  50 JNIEXPORT
  51 jint JNICALL Agent_OnLoad(JavaVM *jvm, char *options, void *reserved) {
  52     return Agent_Initialize(jvm, options, reserved);
  53 }
  54 
  55 JNIEXPORT
  56 jint JNICALL Agent_OnAttach(JavaVM *jvm, char *options, void *reserved) {
  57     return Agent_Initialize(jvm, options, reserved);
  58 }
  59 
  60 JNIEXPORT
  61 jint JNICALL JNI_OnLoad(JavaVM *jvm, void *reserved) {
  62     return JNI_VERSION_1_8;
  63 }
  64 
  65 static
  66 jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {
  67     jvmtiCapabilities capabilities;
  68     jint res = JNI_ENV_PTR(jvm)->GetEnv(JNI_ENV_ARG(jvm, (void **) &jvmti),
  69                                         JVMTI_VERSION);
  70     if (res != JNI_OK || jvmti == NULL) {
  71         printf("    Error: wrong result of a valid call to GetEnv!\n");
  72         return JNI_ERR;
  73     }
  74 
  75     (void)memset(&capabilities, 0, sizeof(capabilities));
  76     capabilities.can_tag_objects = 1;
  77     capabilities.can_generate_garbage_collection_events = 1;
  78     (*jvmti)->AddCapabilities(jvmti, &capabilities);
  79 
  80     return JNI_OK;
  81 }
  82 
  83 static
  84 void throw_exc(JNIEnv *env, char *msg) {
  85     jclass exc_class = JNI_ENV_PTR(env)->FindClass(JNI_ENV_ARG(env, EXC_CNAME));
  86     jint rt = JNI_OK;
  87 
  88     if (exc_class == NULL) {
  89         printf("throw_exc: Error in FindClass(env, %s)\n", EXC_CNAME);
  90         return;
  91     }
  92     rt = JNI_ENV_PTR(env)->ThrowNew(JNI_ENV_ARG(env, exc_class), msg);
  93     if (rt == JNI_ERR) {
  94         printf("throw_exc: Error in JNI ThrowNew(env, %s)\n", msg);
  95     }
  96 }
  97 
  98 JNIEXPORT jint JNICALL
  99 Java_TestGetLoadedClasses_getLoadedClasses(JNIEnv *env, jclass cls) {
 100     jint totalCount = 0;
 101     jclass* classes;
 102     if (jvmti == NULL) {
 103         throw_exc(env, "JVMTI client was not properly loaded!\n");
 104         return 0;
 105     }
 106 
 107     (*jvmti)->GetLoadedClasses(jvmti, &totalCount, &classes);
 108     (*jvmti)->Deallocate(jvmti, (unsigned char*)classes);
 109     return totalCount;
 110 }
 111 
 112 #ifdef __cplusplus
 113 }
 114 #endif