1 /*
2 * Copyright (c) 2003, 2018, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24 #include <stdio.h>
25 #include "jvmti.h"
26 #include "agent_common.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 #define PASSED 0
45 #define STATUS_FAILED 2
46
47 typedef struct {
48 jlocation start;
49 jlocation end;
50 } info;
51
52 static jvmtiEnv *jvmti;
53 static jint result = PASSED;
54 static info meth_tab[] = {
55 {0, 4}, /* 0 <init> */
56 {0, 0}, /* 1 meth1 */
57 {0, 5} /* 2 meth2 */
58 };
59
checkMeth(JNIEnv * env,jclass cl,char * name,char * sig,int stat,int meth_ind)60 void checkMeth(JNIEnv *env, jclass cl, char *name, char *sig,
61 int stat, int meth_ind) {
62 jvmtiError ans;
63 int err = 0;
64 jmethodID mid;
65 jlocation start;
66 jlocation end;
67 jlocation exp_start = meth_tab[meth_ind].start;
68 jlocation exp_end = meth_tab[meth_ind].end;
69
70 if (stat) {
71 mid = JNI_ENV_PTR(env)->
72 GetStaticMethodID(JNI_ENV_ARG(env, cl), name, sig);
73 } else {
74 mid = JNI_ENV_PTR(env)->GetMethodID(JNI_ENV_ARG(env, cl), name, sig);
75 }
76 if (mid == NULL) {
77 printf("Name = %s, sig = %s: mid = NULL\n", name, sig);
78 result = STATUS_FAILED;
79 return;
80 }
81 ans = (*jvmti)->GetMethodLocation(jvmti, mid, &start, &end);
82 if (ans != JVMTI_ERROR_NONE) {
83 printf("Name = %s, sig = %s:\n", name, sig);
84 printf(" Failed get method location: err = %d\n", ans);
85 result = STATUS_FAILED;
86 return;
87 }
88 if (start != exp_start) {
89 result = STATUS_FAILED;
90 err = 1;
91 printf("Name = %s, sig = %s:\n", name, sig);
92 printf(" first location expected: 0x%x, got: 0x%08x%08x\n",
93 (jint)exp_start, (jint)(start >> 32), (jint)start);
94 }
95 if (end != exp_end) {
96 result = STATUS_FAILED;
97 if (!err) {
98 printf("Name = %s, sig = %s:\n", name, sig);
99 }
100 printf(" last location expected: 0x%x, got: 0x%08x%08x\n",
101 (jint)exp_end, (jint)(end >> 32), (jint)end);
102 }
103 }
104
105 #ifdef STATIC_BUILD
Agent_OnLoad_methloc001(JavaVM * jvm,char * options,void * reserved)106 JNIEXPORT jint JNICALL Agent_OnLoad_methloc001(JavaVM *jvm, char *options, void *reserved) {
107 return Agent_Initialize(jvm, options, reserved);
108 }
Agent_OnAttach_methloc001(JavaVM * jvm,char * options,void * reserved)109 JNIEXPORT jint JNICALL Agent_OnAttach_methloc001(JavaVM *jvm, char *options, void *reserved) {
110 return Agent_Initialize(jvm, options, reserved);
111 }
JNI_OnLoad_methloc001(JavaVM * jvm,char * options,void * reserved)112 JNIEXPORT jint JNI_OnLoad_methloc001(JavaVM *jvm, char *options, void *reserved) {
113 return JNI_VERSION_1_8;
114 }
115 #endif
Agent_Initialize(JavaVM * jvm,char * options,void * reserved)116 jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {
117 jint res;
118
119 res = JNI_ENV_PTR(jvm)->GetEnv(JNI_ENV_ARG(jvm, (void **) &jvmti),
120 JVMTI_VERSION_1_1);
121 if (res != JNI_OK || jvmti == NULL) {
122 printf("Wrong result of a valid call to GetEnv !\n");
123 return JNI_ERR;
124 }
125
126 return JNI_OK;
127 }
128
Java_nsk_jvmti_GetMethodLocation_methloc001_check(JNIEnv * env,jclass cls)129 JNIEXPORT jint JNICALL Java_nsk_jvmti_GetMethodLocation_methloc001_check(JNIEnv *env, jclass cls) {
130 checkMeth(env, cls, "<init>", "()V", 0, 0);
131 checkMeth(env, cls, "meth1", "()V", 0, 1);
132 checkMeth(env, cls, "meth2", "(I)[F", 1, 2);
133 return result;
134 }
135
136 #ifdef __cplusplus
137 }
138 #endif
139