annotate src/share/native/sun/management/Flag.c @ 2282:83c34a6b1458

6924497: HotSpotDiagnosticsMXBean.getDiagnosticOptions throws NPE Summary: Check if the element in the flags array is non-null to filter unsupported flags Reviewed-by: dcubed
author mchung
date Mon, 08 Feb 2010 23:02:28 -0800
parents 37a05a11f281
children f8c9a5e3f5db
rev   line source
duke@0 1 /*
duke@0 2 * Copyright 2003-2005 Sun Microsystems, Inc. All Rights Reserved.
duke@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@0 4 *
duke@0 5 * This code is free software; you can redistribute it and/or modify it
duke@0 6 * under the terms of the GNU General Public License version 2 only, as
duke@0 7 * published by the Free Software Foundation. Sun designates this
duke@0 8 * particular file as subject to the "Classpath" exception as provided
duke@0 9 * by Sun in the LICENSE file that accompanied this code.
duke@0 10 *
duke@0 11 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@0 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@0 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@0 14 * version 2 for more details (a copy is included in the LICENSE file that
duke@0 15 * accompanied this code).
duke@0 16 *
duke@0 17 * You should have received a copy of the GNU General Public License version
duke@0 18 * 2 along with this work; if not, write to the Free Software Foundation,
duke@0 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@0 20 *
duke@0 21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
duke@0 22 * CA 95054 USA or visit www.sun.com if you need additional information or
duke@0 23 * have any questions.
duke@0 24 */
duke@0 25
duke@0 26 #include <stdlib.h>
duke@0 27 #include <stdio.h>
duke@0 28 #include <jni.h>
duke@0 29 #include "management.h"
duke@0 30 #include "sun_management_Flag.h"
duke@0 31
duke@0 32 static jobject default_origin = NULL;
duke@0 33 static jobject vm_creation_origin = NULL;
duke@0 34 static jobject mgmt_origin = NULL;
duke@0 35 static jobject envvar_origin = NULL;
duke@0 36 static jobject config_file_origin = NULL;
duke@0 37 static jobject ergo_origin = NULL;
duke@0 38 static jobject other_origin = NULL;
duke@0 39
duke@0 40 JNIEXPORT jint JNICALL
duke@0 41 Java_sun_management_Flag_getInternalFlagCount
duke@0 42 (JNIEnv *env, jclass cls)
duke@0 43 {
duke@0 44 jlong count = jmm_interface->GetLongAttribute(env, NULL,
duke@0 45 JMM_VM_GLOBAL_COUNT);
duke@0 46 return (jint) count;
duke@0 47 }
duke@0 48
duke@0 49 JNIEXPORT jobjectArray JNICALL
duke@0 50 Java_sun_management_Flag_getAllFlagNames
duke@0 51 (JNIEnv *env, jclass cls)
duke@0 52 {
duke@0 53 return jmm_interface->GetVMGlobalNames(env);
duke@0 54 }
duke@0 55
duke@0 56 static jobject find_origin_constant(JNIEnv* env, const char* enum_name) {
duke@0 57 jvalue field;
duke@0 58 field = JNU_GetStaticFieldByName(env,
duke@0 59 NULL,
duke@0 60 "com/sun/management/VMOption$Origin",
duke@0 61 enum_name,
duke@0 62 "Lcom/sun/management/VMOption$Origin;");
duke@0 63 return (*env)->NewGlobalRef(env, field.l);
duke@0 64 }
duke@0 65
duke@0 66 JNIEXPORT void JNICALL
duke@0 67 Java_sun_management_Flag_initialize
duke@0 68 (JNIEnv *env, jclass cls)
duke@0 69 {
duke@0 70 default_origin = find_origin_constant(env, "DEFAULT");
duke@0 71 vm_creation_origin = find_origin_constant(env, "VM_CREATION");
duke@0 72 mgmt_origin = find_origin_constant(env, "MANAGEMENT");
duke@0 73 envvar_origin = find_origin_constant(env, "ENVIRON_VAR");
duke@0 74 config_file_origin = find_origin_constant(env, "CONFIG_FILE");
duke@0 75 ergo_origin = find_origin_constant(env, "ERGONOMIC");
duke@0 76 other_origin = find_origin_constant(env, "OTHER");
duke@0 77 }
duke@0 78
duke@0 79 JNIEXPORT jint JNICALL
duke@0 80 Java_sun_management_Flag_getFlags
duke@0 81 (JNIEnv *env, jclass cls, jobjectArray names, jobjectArray flags, jint count)
duke@0 82 {
duke@0 83 char errmsg[128];
duke@0 84
duke@0 85 jint num_flags, i, index;
duke@0 86 jmmVMGlobal* globals;
mchung@2282 87 size_t gsize;
duke@0 88 const char* class_name = "sun/management/Flag";
duke@0 89 const char* signature = "(Ljava/lang/String;Ljava/lang/Object;ZZLcom/sun/management/VMOption$Origin;)V";
duke@0 90 jobject origin;
duke@0 91 jobject valueObj;
duke@0 92 jobject flag;
duke@0 93
duke@0 94 if (flags == NULL) {
duke@0 95 JNU_ThrowNullPointerException(env, 0);
duke@0 96 return 0;
duke@0 97 }
duke@0 98
duke@0 99 if (count == 0) {
duke@0 100 JNU_ThrowIllegalArgumentException(env, 0);
duke@0 101 return 0;
duke@0 102 }
duke@0 103
mchung@2282 104 gsize = count * sizeof(jmmVMGlobal);
mchung@2282 105 globals = (jmmVMGlobal*) malloc(gsize);
duke@0 106 if (globals == NULL) {
duke@0 107 JNU_ThrowOutOfMemoryError(env, 0);
duke@0 108 return 0;
duke@0 109 }
duke@0 110
mchung@2282 111 memset(globals, 0, gsize);
duke@0 112 num_flags = jmm_interface->GetVMGlobals(env, names, globals, count);
duke@0 113 if (num_flags == 0) {
duke@0 114 free(globals);
duke@0 115 return 0;
duke@0 116 }
duke@0 117
duke@0 118 index = 0;
duke@0 119 for (i = 0; i < count; i++) {
duke@0 120 if (globals[i].name == NULL) {
duke@0 121 continue;
duke@0 122 }
duke@0 123 switch (globals[i].type) {
duke@0 124 case JMM_VMGLOBAL_TYPE_JBOOLEAN:
duke@0 125 valueObj = JNU_NewObjectByName(env, "java/lang/Boolean", "(Z)V",
duke@0 126 globals[i].value.z);
duke@0 127 break;
duke@0 128 case JMM_VMGLOBAL_TYPE_JSTRING:
duke@0 129 valueObj = globals[i].value.l;
duke@0 130 break;
duke@0 131 case JMM_VMGLOBAL_TYPE_JLONG:
duke@0 132 valueObj = JNU_NewObjectByName(env, "java/lang/Long", "(J)V",
duke@0 133 globals[i].value.j);
duke@0 134 break;
duke@0 135 default:
duke@0 136 // unsupported type
duke@0 137 sprintf(errmsg, "Unsupported VMGlobal Type %d", globals[i].type);
duke@0 138 JNU_ThrowInternalError(env, errmsg);
duke@0 139 free(globals);
duke@0 140 return 0;
duke@0 141 }
duke@0 142 switch (globals[i].origin) {
duke@0 143 case JMM_VMGLOBAL_ORIGIN_DEFAULT:
duke@0 144 origin = default_origin;
duke@0 145 break;
duke@0 146 case JMM_VMGLOBAL_ORIGIN_COMMAND_LINE:
duke@0 147 origin = vm_creation_origin;
duke@0 148 break;
duke@0 149 case JMM_VMGLOBAL_ORIGIN_MANAGEMENT:
duke@0 150 origin = mgmt_origin;
duke@0 151 break;
duke@0 152 case JMM_VMGLOBAL_ORIGIN_ENVIRON_VAR:
duke@0 153 origin = envvar_origin;
duke@0 154 break;
duke@0 155 case JMM_VMGLOBAL_ORIGIN_CONFIG_FILE:
duke@0 156 origin = config_file_origin;
duke@0 157 break;
duke@0 158 case JMM_VMGLOBAL_ORIGIN_ERGONOMIC:
duke@0 159 origin = ergo_origin;
duke@0 160 break;
duke@0 161 case JMM_VMGLOBAL_ORIGIN_OTHER:
duke@0 162 origin = other_origin;
duke@0 163 break;
duke@0 164 default:
duke@0 165 // unknown origin
duke@0 166 origin = other_origin;
duke@0 167 break;
duke@0 168 }
duke@0 169 flag = JNU_NewObjectByName(env, class_name, signature, globals[i].name,
duke@0 170 valueObj, globals[i].writeable,
duke@0 171 globals[i].external, origin);
duke@0 172 if (flag == NULL) {
duke@0 173 free(globals);
duke@0 174 JNU_ThrowOutOfMemoryError(env, 0);
duke@0 175 return 0;
duke@0 176 }
duke@0 177 (*env)->SetObjectArrayElement(env, flags, index, flag);
duke@0 178 index++;
duke@0 179 }
duke@0 180
duke@0 181 if (index != num_flags) {
duke@0 182 JNU_ThrowInternalError(env, "Number of Flag objects created unmatched");
duke@0 183 free(globals);
duke@0 184 return 0;
duke@0 185 }
duke@0 186
duke@0 187 free(globals);
duke@0 188
duke@0 189 /* return the number of Flag objects created */
duke@0 190 return num_flags;
duke@0 191 }
duke@0 192
duke@0 193 JNIEXPORT void JNICALL
duke@0 194 Java_sun_management_Flag_setLongValue
duke@0 195 (JNIEnv *env, jclass cls, jstring name, jlong value)
duke@0 196 {
duke@0 197 jvalue v;
duke@0 198 v.j = value;
duke@0 199
duke@0 200 jmm_interface->SetVMGlobal(env, name, v);
duke@0 201 }
duke@0 202
duke@0 203 JNIEXPORT void JNICALL
duke@0 204 Java_sun_management_Flag_setBooleanValue
duke@0 205 (JNIEnv *env, jclass cls, jstring name, jboolean value)
duke@0 206 {
duke@0 207 jvalue v;
duke@0 208 v.z = value;
duke@0 209
duke@0 210 jmm_interface->SetVMGlobal(env, name, v);
duke@0 211 }
duke@0 212
duke@0 213 JNIEXPORT void JNICALL
duke@0 214 Java_sun_management_Flag_setStringValue
duke@0 215 (JNIEnv *env, jclass cls, jstring name, jstring value)
duke@0 216 {
duke@0 217 jvalue v;
duke@0 218 v.l = value;
duke@0 219
duke@0 220 jmm_interface->SetVMGlobal(env, name, v);
duke@0 221 }