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 <string.h>
26 #include "jvmti.h"
27 #include "agent_common.h"
28
29 #ifdef __cplusplus
30 extern "C" {
31 #endif
32
33 #ifndef JNI_ENV_ARG
34
35 #ifdef __cplusplus
36 #define JNI_ENV_ARG(x, y) y
37 #define JNI_ENV_ARG1(x)
38 #define JNI_ENV_PTR(x) x
39 #else
40 #define JNI_ENV_ARG(x,y) x, y
41 #define JNI_ENV_ARG1(x) x
42 #define JNI_ENV_PTR(x) (*x)
43 #endif
44
45 #endif
46
47 #define JVMTI_ENV_ARG JNI_ENV_ARG
48 #define JVMTI_ENV_ARG1 JNI_ENV_ARG1
49 #define JVMTI_ENV_PTR JNI_ENV_PTR
50
51 #define JVMTI_ERROR_CHECK(str,res) if ( res != JVMTI_ERROR_NONE) { printf(str); printf(" %d\n",res); return res;}
52 #define JVMTI_ERROR_CHECK_EXPECTED_ERROR(str,res,err) if ( res != err) { printf(str); printf("unexpected error %d\n",res); return res;}
53
54 #define JVMTI_ERROR_CHECK_VOID(str,res) if ( res != JVMTI_ERROR_NONE) { printf(str); printf(" %d\n",res); iGlobalStatus = 2; }
55
56 #define JVMTI_ERROR_CHECK_EXPECTED_ERROR_VOID(str,res,err) if ( res != err) { printf(str); printf(" unexpected error %d\n",res); iGlobalStatus = 2; }
57
58
59
60 jvmtiEnv *jvmti;
61 jint iGlobalStatus = 0;
62 static jvmtiCapabilities jvmti_caps;
63 static jvmtiEventCallbacks callbacks;
64
65
66 int printdump = 0;
67
68
69 void debug_printf(const char *fmt, ...) {
70 va_list args;
71
72 va_start(args, fmt);
73 if (printdump) {
74 vprintf(fmt, args);
75 }
76 va_end(args);
77 }
78
79
80 void JNICALL vmInit(jvmtiEnv *jvmti_env, JNIEnv *env, jthread thread) {
81 jvmtiError res;
82 debug_printf("VMInit event received\n");
83 res = JVMTI_ENV_PTR(jvmti_env)->DisposeEnvironment(JVMTI_ENV_ARG1(jvmti_env));
84 JVMTI_ERROR_CHECK_VOID("DisposeEnvironment returned error", res);
85 }
86
87
88 void init_callbacks() {
89
90 callbacks.VMInit = vmInit;
91
92 }
93
94
95 #ifdef STATIC_BUILD
96 JNIEXPORT jint JNICALL Agent_OnLoad_JvmtiTest(JavaVM *jvm, char *options, void *reserved) {
97 return Agent_Initialize(jvm, options, reserved);
98 }
99 JNIEXPORT jint JNICALL Agent_OnAttach_JvmtiTest(JavaVM *jvm, char *options, void *reserved) {
100 return Agent_Initialize(jvm, options, reserved);
101 }
102 JNIEXPORT jint JNI_OnLoad_JvmtiTest(JavaVM *jvm, char *options, void *reserved) {
103 return JNI_VERSION_1_8;
104 }
105 #endif
106 jint Agent_Initialize(JavaVM * jvm, char *options, void *reserved) {
107 jint res;
108 jint count;
109 char **properties;
110 int i;
111
112 if (options && strlen(options) > 0) {
113 if (strstr(options, "printdump")) {
114 printdump = 1;
115 }
116 }
117
118 res = JNI_ENV_PTR(jvm)->
119 GetEnv(JNI_ENV_ARG(jvm, (void **) &jvmti), JVMTI_VERSION_1_1);
120 if (res < 0) {
121 printf("Wrong result of a valid call to GetEnv!\n");
122 return JNI_ERR;
123 }
124
125 /* Enable event call backs. */
126 init_callbacks();
127 res = JVMTI_ENV_PTR(jvmti)->SetEventCallbacks(JVMTI_ENV_ARG(jvmti, &callbacks), sizeof(callbacks));
128 JVMTI_ERROR_CHECK("SetEventCallbacks returned error", res);
129
130 /* Add capabilities */
131 res = JVMTI_ENV_PTR(jvmti)->GetPotentialCapabilities(JVMTI_ENV_ARG(jvmti, &jvmti_caps));
132 JVMTI_ERROR_CHECK("GetPotentialCapabilities returned error", res);
133
134 res = JVMTI_ENV_PTR(jvmti)->AddCapabilities(JVMTI_ENV_ARG(jvmti, &jvmti_caps));
135 JVMTI_ERROR_CHECK("GetAddCapabilities returned error", res);
136
137
138 res = JVMTI_ENV_PTR(jvmti)->SetEventNotificationMode(JVMTI_ENV_ARG(jvmti,JVMTI_ENABLE),JVMTI_EVENT_VM_INIT,NULL);
139 JVMTI_ERROR_CHECK("SetEventNotificationMode for VM_INIT returned error", res);
140
141 res = JVMTI_ENV_PTR(jvmti)->GetSystemProperties(JVMTI_ENV_ARG(jvmti, &count), &properties);
142
143 JVMTI_ERROR_CHECK("GetSystemProperties returned error", res);
144
145 for (i=0; i< count; i++) {
146 char *value;
147
148 res = JVMTI_ENV_PTR(jvmti)->GetSystemProperty(JVMTI_ENV_ARG(jvmti, (const char *)properties[i]), &value);
149 JVMTI_ERROR_CHECK("GetSystemProperty returned error", res);
150 debug_printf(" %s %s \n", properties[i], value);
151
152 res = JVMTI_ENV_PTR(jvmti)->SetSystemProperty(JVMTI_ENV_ARG(jvmti, (const char *)properties[i]), value);
153 debug_printf("SetSystemProperty returned error %d\n", res);
154
155 }
156
157 return JNI_OK;
158 }
159
160
161 JNIEXPORT jint JNICALL
162 Java_nsk_jvmti_unit_functions_Dispose_JvmtiTest_GetResult(JNIEnv * env, jclass cls) {
163 return iGlobalStatus;
164 }
165
166
167 #ifdef __cplusplus
168 }
169 #endif
|
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 <string.h>
26 #include "jvmti.h"
27 #include "agent_common.h"
28
29 extern "C" {
30
31
32 #define JVMTI_ERROR_CHECK(str,res) if ( res != JVMTI_ERROR_NONE) { printf(str); printf(" %d\n",res); return res;}
33 #define JVMTI_ERROR_CHECK_EXPECTED_ERROR(str,res,err) if ( res != err) { printf(str); printf("unexpected error %d\n",res); return res;}
34
35 #define JVMTI_ERROR_CHECK_VOID(str,res) if ( res != JVMTI_ERROR_NONE) { printf(str); printf(" %d\n",res); iGlobalStatus = 2; }
36
37 #define JVMTI_ERROR_CHECK_EXPECTED_ERROR_VOID(str,res,err) if ( res != err) { printf(str); printf(" unexpected error %d\n",res); iGlobalStatus = 2; }
38
39
40
41 jvmtiEnv *jvmti;
42 jint iGlobalStatus = 0;
43 static jvmtiCapabilities jvmti_caps;
44 static jvmtiEventCallbacks callbacks;
45
46
47 int printdump = 0;
48
49
50 void debug_printf(const char *fmt, ...) {
51 va_list args;
52
53 va_start(args, fmt);
54 if (printdump) {
55 vprintf(fmt, args);
56 }
57 va_end(args);
58 }
59
60
61 void JNICALL vmInit(jvmtiEnv *jvmti_env, JNIEnv *env, jthread thread) {
62 jvmtiError res;
63 debug_printf("VMInit event received\n");
64 res = jvmti_env->DisposeEnvironment();
65 JVMTI_ERROR_CHECK_VOID("DisposeEnvironment returned error", res);
66 }
67
68
69 void init_callbacks() {
70
71 callbacks.VMInit = vmInit;
72
73 }
74
75
76 #ifdef STATIC_BUILD
77 JNIEXPORT jint JNICALL Agent_OnLoad_JvmtiTest(JavaVM *jvm, char *options, void *reserved) {
78 return Agent_Initialize(jvm, options, reserved);
79 }
80 JNIEXPORT jint JNICALL Agent_OnAttach_JvmtiTest(JavaVM *jvm, char *options, void *reserved) {
81 return Agent_Initialize(jvm, options, reserved);
82 }
83 JNIEXPORT jint JNI_OnLoad_JvmtiTest(JavaVM *jvm, char *options, void *reserved) {
84 return JNI_VERSION_1_8;
85 }
86 #endif
87 jint Agent_Initialize(JavaVM * jvm, char *options, void *reserved) {
88 jint res;
89 jint count;
90 char **properties;
91 int i;
92
93 if (options && strlen(options) > 0) {
94 if (strstr(options, "printdump")) {
95 printdump = 1;
96 }
97 }
98
99 res = jvm->GetEnv((void **) &jvmti, JVMTI_VERSION_1_1);
100 if (res < 0) {
101 printf("Wrong result of a valid call to GetEnv!\n");
102 return JNI_ERR;
103 }
104
105 /* Enable event call backs. */
106 init_callbacks();
107 res = jvmti->SetEventCallbacks(&callbacks, sizeof(callbacks));
108 JVMTI_ERROR_CHECK("SetEventCallbacks returned error", res);
109
110 /* Add capabilities */
111 res = jvmti->GetPotentialCapabilities(&jvmti_caps);
112 JVMTI_ERROR_CHECK("GetPotentialCapabilities returned error", res);
113
114 res = jvmti->AddCapabilities(&jvmti_caps);
115 JVMTI_ERROR_CHECK("GetAddCapabilities returned error", res);
116
117
118 res = jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_VM_INIT, NULL);
119 JVMTI_ERROR_CHECK("SetEventNotificationMode for VM_INIT returned error", res);
120
121 res = jvmti->GetSystemProperties(&count, &properties);
122
123 JVMTI_ERROR_CHECK("GetSystemProperties returned error", res);
124
125 for (i=0; i< count; i++) {
126 char *value;
127
128 res = jvmti->GetSystemProperty((const char *) properties[i], &value);
129 JVMTI_ERROR_CHECK("GetSystemProperty returned error", res);
130 debug_printf(" %s %s \n", properties[i], value);
131
132 res = jvmti->SetSystemProperty((const char *) properties[i], value);
133 debug_printf("SetSystemProperty returned error %d\n", res);
134 }
135
136 return JNI_OK;
137 }
138
139
140 JNIEXPORT jint JNICALL
141 Java_nsk_jvmti_unit_functions_Dispose_JvmtiTest_GetResult(JNIEnv * env, jclass cls) {
142 return iGlobalStatus;
143 }
144
145
146 }
|