Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/native/sun/misc/VM.c
38829 views
/*1* Copyright (c) 2004, 2011, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425#include <string.h>2627#include "jni.h"28#include "jni_util.h"29#include "jlong.h"30#include "jvm.h"31#include "jdk_util.h"3233#include "sun_misc_VM.h"3435typedef jintArray (JNICALL *GET_THREAD_STATE_VALUES_FN)(JNIEnv *, jint);36typedef jobjectArray (JNICALL *GET_THREAD_STATE_NAMES_FN)(JNIEnv *, jint, jintArray);3738static GET_THREAD_STATE_VALUES_FN GetThreadStateValues_fp = NULL;39static GET_THREAD_STATE_NAMES_FN GetThreadStateNames_fp = NULL;4041static void get_thread_state_info(JNIEnv *env, jint state,42jobjectArray stateValues,43jobjectArray stateNames) {44char errmsg[128];45jintArray values;46jobjectArray names;4748values = (*GetThreadStateValues_fp)(env, state);49if (values == NULL) {50sprintf(errmsg, "Mismatched VM version: Thread state (%d) "51"not supported", state);52JNU_ThrowInternalError(env, errmsg);53return;54}55/* state is also used as the index in the array */56(*env)->SetObjectArrayElement(env, stateValues, state, values);5758names = (*GetThreadStateNames_fp)(env, state, values);59if (names == NULL) {60sprintf(errmsg, "Mismatched VM version: Thread state (%d) "61"not supported", state);62JNU_ThrowInternalError(env, errmsg);63return;64}65(*env)->SetObjectArrayElement(env, stateNames, state, names);66}6768JNIEXPORT void JNICALL69Java_sun_misc_VM_getThreadStateValues(JNIEnv *env, jclass cls,70jobjectArray values,71jobjectArray names)72{73char errmsg[128];7475// check if the number of Thread.State enum constants76// matches the number of states defined in jvm.h77jsize len1 = (*env)->GetArrayLength(env, values);78jsize len2 = (*env)->GetArrayLength(env, names);79if (len1 != JAVA_THREAD_STATE_COUNT || len2 != JAVA_THREAD_STATE_COUNT) {80sprintf(errmsg, "Mismatched VM version: JAVA_THREAD_STATE_COUNT = %d "81" but JDK expects %d / %d",82JAVA_THREAD_STATE_COUNT, len1, len2);83JNU_ThrowInternalError(env, errmsg);84return;85}8687if (GetThreadStateValues_fp == NULL) {88GetThreadStateValues_fp = (GET_THREAD_STATE_VALUES_FN)89JDK_FindJvmEntry("JVM_GetThreadStateValues");90if (GetThreadStateValues_fp == NULL) {91JNU_ThrowInternalError(env,92"Mismatched VM version: JVM_GetThreadStateValues not found");93return;94}9596GetThreadStateNames_fp = (GET_THREAD_STATE_NAMES_FN)97JDK_FindJvmEntry("JVM_GetThreadStateNames");98if (GetThreadStateNames_fp == NULL) {99JNU_ThrowInternalError(env,100"Mismatched VM version: JVM_GetThreadStateNames not found");101return ;102}103}104105get_thread_state_info(env, JAVA_THREAD_STATE_NEW, values, names);106get_thread_state_info(env, JAVA_THREAD_STATE_RUNNABLE, values, names);107get_thread_state_info(env, JAVA_THREAD_STATE_BLOCKED, values, names);108get_thread_state_info(env, JAVA_THREAD_STATE_WAITING, values, names);109get_thread_state_info(env, JAVA_THREAD_STATE_TIMED_WAITING, values, names);110get_thread_state_info(env, JAVA_THREAD_STATE_TERMINATED, values, names);111}112113JNIEXPORT jobject JNICALL114Java_sun_misc_VM_latestUserDefinedLoader0(JNIEnv *env, jclass cls) {115return JVM_LatestUserDefinedLoader(env);116}117118typedef void (JNICALL *GetJvmVersionInfo_fp)(JNIEnv*, jvm_version_info*, size_t);119120JNIEXPORT void JNICALL121Java_sun_misc_VM_initialize(JNIEnv *env, jclass cls) {122GetJvmVersionInfo_fp func_p;123124if (!JDK_InitJvmHandle()) {125JNU_ThrowInternalError(env, "Handle for JVM not found for symbol lookup");126return;127}128129func_p = (GetJvmVersionInfo_fp) JDK_FindJvmEntry("JVM_GetVersionInfo");130if (func_p != NULL) {131jvm_version_info info;132133memset(&info, 0, sizeof(info));134135/* obtain the JVM version info */136(*func_p)(env, &info, sizeof(info));137}138}139140141142