#include "jni.h"
#include "j9.h"
#include "j9port.h"
#include "jvmti.h"
#include "jvmtinls.h"
#include "jclprots.h"
#define LOG_OPTION_DATA_SIZE 256
static void raiseException(JNIEnv *env, const char *type, U_32 moduleNumber, U_32 messageNumber, const char *message);
jstring JNICALL
Java_com_ibm_jvm_Log_QueryOptionsImpl(JNIEnv *env, jclass clazz)
{
J9VMThread *thr = (J9VMThread *)env;
J9JavaVM *vm = thr->javaVM;
I_32 bufferSize = 0;
jstring options = NULL;
char *nativeOptions = NULL;
UDATA rc = 0;
PORT_ACCESS_FROM_JAVAVM(vm);
nativeOptions = j9mem_allocate_memory(LOG_OPTION_DATA_SIZE, J9MEM_CATEGORY_VM_JCL);
if (NULL == nativeOptions) {
vm->internalVMFunctions->throwNativeOOMError(env, 0, 0);
return NULL;
}
rc = vm->internalVMFunctions->queryLogOptions(vm, LOG_OPTION_DATA_SIZE, (void *)nativeOptions, &bufferSize);
if (JVMTI_ERROR_NONE != rc) {
raiseException(env, "java/lang/RuntimeException", J9NLS_JVMTI_COM_IBM_LOG_QUERY_OPT_ERROR,
"Could not query JVM log options");
j9mem_free_memory(nativeOptions);
return NULL;
}
options = (*env)->NewStringUTF(env, nativeOptions);
j9mem_free_memory(nativeOptions);
if (NULL == options) {
raiseException(env, "java/lang/RuntimeException", J9NLS_JVMTI_COM_IBM_LOG_NATIVE_STRING_ERROR,
"Could not convert JVM log options native string");
}
return options;
}
jint JNICALL
Java_com_ibm_jvm_Log_SetOptionsImpl(JNIEnv *env, jclass clazz, jstring options)
{
J9VMThread *thr = (J9VMThread *)env;
J9JavaVM *vm = thr->javaVM;
const char *nativeOptions = NULL;
UDATA rc = 0;
nativeOptions = (*env)->GetStringUTFChars(env, options, 0);
if (NULL == nativeOptions) {
return JNI_ERR;
}
rc = vm->internalVMFunctions->setLogOptions(vm, (char *)nativeOptions);
(*env)->ReleaseStringUTFChars(env, options, nativeOptions);
if (JVMTI_ERROR_NONE != rc) {
raiseException(env, "java/lang/RuntimeException", J9NLS_JVMTI_COM_IBM_LOG_SET_OPT_ERROR,
"Could not set JVM log options");
return JNI_ERR;
}
return JNI_OK;
}
static void
raiseException(JNIEnv *env, const char *type, U_32 moduleNumber, U_32 messageNumber, const char *message)
{
const char *nlsMessage = NULL;
jclass exceptionClass = (*env)->FindClass(env, type);
PORT_ACCESS_FROM_ENV(env);
nlsMessage = OMRPORT_FROM_J9PORT(PORTLIB)->nls_lookup_message(OMRPORT_FROM_J9PORT(PORTLIB), J9NLS_ERROR, moduleNumber, messageNumber, message);
if (NULL == exceptionClass) {
return;
}
(*env)->ThrowNew(env, exceptionClass, nlsMessage);
}