Path: blob/master/runtime/jcl/common/com_ibm_oti_vm_VM.c
6000 views
/*******************************************************************************1* Copyright (c) 1998, 2021 IBM Corp. and others2*3* This program and the accompanying materials are made available under4* the terms of the Eclipse Public License 2.0 which accompanies this5* distribution and is available at https://www.eclipse.org/legal/epl-2.0/6* or the Apache License, Version 2.0 which accompanies this distribution and7* is available at https://www.apache.org/licenses/LICENSE-2.0.8*9* This Source Code may also be made available under the following10* Secondary Licenses when the conditions for such availability set11* forth in the Eclipse Public License, v. 2.0 are satisfied: GNU12* General Public License, version 2 with the GNU Classpath13* Exception [1] and GNU General Public License, version 2 with the14* OpenJDK Assembly Exception [2].15*16* [1] https://www.gnu.org/software/classpath/license.html17* [2] http://openjdk.java.net/legal/assembly-exception.html18*19* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 OR LicenseRef-GPL-2.0 WITH Assembly-exception20*******************************************************************************/2122#include <string.h>23#include "jni.h"24#include "jcl.h"25#include "jclglob.h"26#include "jclprots.h"27#include "jcl_internal.h"28#include "util_api.h"2930/* private static native byte[][] getVMArgsImpl(); */3132jobjectArray JNICALL33Java_com_ibm_oti_vm_VM_getVMArgsImpl(JNIEnv *env, jobject recv)34{35J9VMThread *vmThread = (J9VMThread *) env;36J9JavaVM *vm = vmThread->javaVM;37JavaVMInitArgs *vmOptionsStruct = vm->vmArgsArray->actualVMArgs;38jint originalSize = vmOptionsStruct->nOptions;39jint resultSize = 0;40JavaVMOption *options = vmOptionsStruct->options;41jobjectArray result = NULL;42jint i;43jclass byteArrayClass;4445/* Count only options that begin with "-" */4647for (i = 0; i < originalSize; ++i) {48if ('-' == options[i].optionString[0]) {49resultSize += 1;50}51}5253/* Create the result array and fill it in, including only options that begin with "-" */5455byteArrayClass = (*env)->FindClass(env, "[B");56if (NULL != byteArrayClass) {57result = (*env)->NewObjectArray(env, resultSize, byteArrayClass, NULL);58if (NULL != result) {59jint writeIndex = 0;6061for (i = 0; i < originalSize; ++i) {62char * optionString = options[i].optionString;6364if ('-' == optionString[0]) {65jint optionLength = (jint) strlen(optionString);66jbyteArray option = (*env)->NewByteArray(env, optionLength);6768if (NULL == option) {69/* Don't use break here to avoid triggering the assertion below */70return NULL;71}72(*env)->SetByteArrayRegion(env, option, 0, optionLength, (jbyte*)optionString);73(*env)->SetObjectArrayElement(env, result, writeIndex, option);74(*env)->DeleteLocalRef(env, option);75writeIndex += 1;76}77}78Assert_JCL_true(writeIndex == resultSize);79}80}8182return result;83}8485/**86* @return process ID of the caller. This is upcast from UDATA.87*/88jlong JNICALL89Java_com_ibm_oti_vm_VM_getProcessId(JNIEnv *env, jclass clazz)90{9192PORT_ACCESS_FROM_VMC( ((J9VMThread *) env) );93jlong pid;94pid = (jlong) j9sysinfo_get_pid();95return pid;96}9798/**99* @return numeric user ID of the caller. This is upcast from a UDATA.100*/101jlong JNICALL102Java_com_ibm_oti_vm_VM_getUid(JNIEnv *env, jclass clazz)103{104105PORT_ACCESS_FROM_VMC( ((J9VMThread *) env) );106jlong uid;107uid = (jlong) j9sysinfo_get_euid();108return uid;109}110111jobject JNICALL112Java_com_ibm_oti_vm_VM_getNonBootstrapClassLoader(JNIEnv *env, jclass jlClass)113{114J9VMThread *currentThread = (J9VMThread*)env;115J9JavaVM *vm = currentThread->javaVM;116J9InternalVMFunctions *vmFuncs = vm->internalVMFunctions;117J9ClassLoader *bootstrapLoader = vm->systemClassLoader;118jobject result = NULL;119J9StackWalkState walkState;120121vmFuncs->internalEnterVMFromJNI(currentThread);122walkState.skipCount = 2; /* Skip this native and its caller */123walkState.flags = J9_STACKWALK_CACHE_CPS | J9_STACKWALK_VISIBLE_ONLY | J9_STACKWALK_INCLUDE_NATIVES;124walkState.walkThread = currentThread;125if (J9_STACKWALK_RC_NONE != vm->walkStackFrames(currentThread, &walkState)) {126vmFuncs->setNativeOutOfMemoryError(currentThread, 0, 0);127} else {128J9ConstantPool **cacheCursor = (J9ConstantPool**)walkState.cache;129UDATA i = 0;130for (i = 0; i < walkState.framesWalked; ++i, ++cacheCursor) {131J9ClassLoader *classLoader = J9_CLASS_FROM_CP(*cacheCursor)->classLoader;132if (classLoader != bootstrapLoader) {133result = vmFuncs->j9jni_createLocalRef(env, classLoader->classLoaderObject);134break;135}136}137vmFuncs->freeStackWalkCaches(currentThread, &walkState);138}139vmFuncs->internalExitVMToJNI(currentThread);140return result;141}142143144/**145* Set the thread as a JVM System thread type146* @return 0 if successful, -1 if failed147*/148jint JNICALL149Java_com_ibm_oti_vm_VM_markCurrentThreadAsSystemImpl(JNIEnv *env)150{151J9VMThread *vmThread = (J9VMThread *) env;152jint rc = 0;153154rc = (jint) omrthread_set_category(vmThread->osThread, J9THREAD_CATEGORY_SYSTEM_THREAD, J9THREAD_TYPE_SET_CREATE);155156return rc;157}158159/**160* Gets the J9ConstantPool address from a J9Class address161* @param j9clazz J9Class address162* @return address of J9ConstantPool163*/164jlong JNICALL165Java_com_ibm_oti_vm_VM_getJ9ConstantPoolFromJ9Class(JNIEnv *env, jclass unused, jlong j9clazz)166{167J9Class *clazz = (J9Class *)(IDATA)j9clazz;168/*169* Casting to UDATA first means the value will be zero-extended170* instead of sign-extended on platforms where jlong and UDATA171* are different sizes.172*/173return (jlong)(UDATA)clazz->ramConstantPool;174}175176177