1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
|
/*
* Copyright (c) 2003, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
#include <stdio.h>
#include <string.h>
#include "jvmti.h"
#include "agent_common.hpp"
#include "JVMTITools.hpp"
extern "C" {
#define PASSED 0
#define STATUS_FAILED 2
typedef struct {
const char *name;
const char *sig;
} meth_info;
typedef struct {
const char *name;
jint mcount;
meth_info *meths;
} class_info;
static jvmtiEnv *jvmti = nullptr;
static jint result = PASSED;
static jboolean printdump = JNI_FALSE;
static meth_info m0[] = {
{ "<init>", "(Lnsk/jvmti/GetClassMethods/getclmthd007;)V" },
{ "meth_1", "(Ljava/lang/String;)V" }
};
static meth_info m1[] = {
{ "meth_n1", "()V" },
{ "meth_def1", "()V" }
};
static meth_info m2[] = {
{ "<init>", "()V" },
{ "meth_n1", "()V" },
{ "meth_n2", "()I" },
{ "<clinit>", "()V" }
};
static meth_info m3[] = {
{ "<init>", "()V" }
};
static meth_info m4[] = {
{ "<init>", "()V" },
{ "meth_o2", "()V" }
};
static meth_info m5[] = {
{ "<init>", "()V" },
{ "meth_o3", "()I" }
};
static meth_info m6[] = {
{ "meth_i1", "()I" }
};
static meth_info m7[] = {
{ "meth_i2", "()I" }
};
static meth_info m8[] = {
{ "<init>", "()V" },
{ "meth_i2", "()I" }
};
static meth_info m9[] = {
{ "<init>", "()V" },
{ "meth_i1", "()I" }
};
static class_info classes[] = {
{ "InnerClass1", 2, m0 },
{ "InnerInterface", 2, m1 },
{ "InnerClass2", 4, m2 },
{ "OuterClass1", 1, m3 },
{ "OuterClass2", 2, m4 },
{ "OuterClass3", 2, m5 },
{ "OuterInterface1", 1, m6 },
{ "OuterInterface2", 1, m7 },
{ "OuterClass4", 2, m8 },
{ "OuterClass5", 2, m9 }
};
#ifdef STATIC_BUILD
JNIEXPORT jint JNICALL Agent_OnLoad_getclmthd007(JavaVM *jvm, char *options, void *reserved) {
return Agent_Initialize(jvm, options, reserved);
}
JNIEXPORT jint JNICALL Agent_OnAttach_getclmthd007(JavaVM *jvm, char *options, void *reserved) {
return Agent_Initialize(jvm, options, reserved);
}
JNIEXPORT jint JNI_OnLoad_getclmthd007(JavaVM *jvm, char *options, void *reserved) {
return JNI_VERSION_1_8;
}
#endif
jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {
jint res;
if (options != nullptr && strcmp(options, "printdump") == 0) {
printdump = JNI_TRUE;
}
res = jvm->GetEnv((void **) &jvmti, JVMTI_VERSION_1_1);
if (res != JNI_OK || jvmti == nullptr) {
printf("Wrong result of a valid call to GetEnv!\n");
return JNI_ERR;
}
return JNI_OK;
}
JNIEXPORT void JNICALL
Java_nsk_jvmti_GetClassMethods_getclmthd007_check(JNIEnv *env,
jclass cls, jint i, jclass clazz) {
jvmtiError err;
jint mcount;
jmethodID *methods;
char *name, *sig, *generic;
int j, k;
int failed = JNI_FALSE; // enable debugging on failure
if (jvmti == nullptr) {
printf("JVMTI client was not properly loaded!\n");
result = STATUS_FAILED;
return;
}
if (printdump == JNI_TRUE) {
printf(">>> %s:\n", classes[i].name);
}
err = jvmti->GetClassMethods(clazz, &mcount, &methods);
if (err != JVMTI_ERROR_NONE) {
printf("(GetClassMethods#%d) unexpected error: %s (%d)\n",
i, TranslateError(err), err);
result = STATUS_FAILED;
return;
}
if (mcount != classes[i].mcount) {
printf("(%d) wrong number of methods: %d, expected: %d\n",
i, mcount, classes[i].mcount);
result = STATUS_FAILED;
failed = JNI_TRUE; // show the methods found
printf(">>> %s:\n", classes[i].name);
}
for (k = 0; k < mcount; k++) {
if (methods[k] == nullptr) {
printf("(%d:%d) methodID = null\n", i, k);
result = STATUS_FAILED;
} else if (printdump == JNI_TRUE || failed == JNI_TRUE) {
err = jvmti->GetMethodName(methods[k],
&name, &sig, &generic);
if (err == JVMTI_ERROR_NONE) {
printf(">>> [%d]: %s%s\n", k, name, sig);
}
}
}
for (j = 0; j < classes[i].mcount; j++) {
/* search the returned table for each expected entry */
for (k = 0; k < mcount; k++) {
if (methods[k] != nullptr) {
err = jvmti->GetMethodName(methods[k],
&name, &sig, &generic);
if (err != JVMTI_ERROR_NONE) {
printf("(GetMethodName#%d:%d) unexpected error: %s (%d)\n",
i, k, TranslateError(err), err);
result = STATUS_FAILED;
} else {
if (name != nullptr && sig != nullptr &&
strcmp(name, classes[i].meths[j].name) == 0 &&
strcmp(sig, classes[i].meths[j].sig) == 0) break;
}
}
}
if (k == mcount) {
printf("(%d:%d) method not found: \"%s%s\"", i, j,
classes[i].meths[j].name, classes[i].meths[j].sig);
result = STATUS_FAILED;
}
}
}
JNIEXPORT int JNICALL
Java_nsk_jvmti_GetClassMethods_getclmthd007_getRes(JNIEnv *env, jclass cls) {
return result;
}
}
|