Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/share/aod/aod.cpp
41161 views
/*1* Copyright (c) 2008, 2018, 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.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/22#include <string.h>23#include <stdlib.h>24#include <jni.h>25#include <jni_tools.h>26#include <nsk_tools.h>27#include <aod.h>2829extern "C" {3031static volatile int internalError = 0;3233/*34* This function can be used to inform AOD framework that some non critical for test logic35* error happened inside shared function (e.g. JVMTI Deallocate failed).36*37* If this function was called status of all finishing AOD agents is changed to failed.38*/3940void nsk_aod_internal_error() {41NSK_COMPLAIN0("WARNING: some error happened inside common function, see log for details\n");42internalError = 1;43}4445void nsk_free_options(Options* options) {46if (options != NULL) {47int i;48for (i = 0; i < NSK_AOD_MAX_OPTIONS; i++) {49if (options->names[i] != NULL) {50free(options->names[i]);51}52if (options->values[i] != NULL) {53free(options->values[i]);54}55}56free(options);57}58}59/*60* Work with agent options61*/6263/*64* Parse options and create structure Options65*/66static Options* nsk_aod_createOptionsObject(char* optionsString) {67int i = 0;68Options* options;69char* name;70char* value;71char* sep;7273if (optionsString == NULL) {74NSK_COMPLAIN0("options were not passed to the native agent\n");75return NULL;76}77options = (Options*) malloc(sizeof(Options));78memset(options, 0, sizeof(Options));79options->size = 0;80name = optionsString;81while (name != NULL && i < NSK_AOD_MAX_OPTIONS) {82sep = strchr(name, '=');83if (sep == NULL) { // name not found84NSK_COMPLAIN1("Invalid options format: '%s'\n", optionsString);85nsk_free_options(options);86return NULL;87}88*sep = '\0';89options->names[i] = strdup(name);90value = sep + 1;91if (*value == '\0') { // value not found92NSK_COMPLAIN1("Option '%s' is empty\n", options->names[i]);93nsk_free_options(options);94return NULL;95}96sep = strchr(value, ' ');97if (sep != NULL) {98*sep = '\0';99name = sep + 1;100} else {101name = strchr(value, '\0');102}103options->values[i] = strdup(value);104i++;105106if (*name == '\0') {107name = NULL;108}109}110if (name != NULL) {111NSK_COMPLAIN1("WARNING: not all options were parsed, only %d options can be specified\n",112NSK_AOD_MAX_OPTIONS);113}114options->size = i;115return options;116}117118Options* nsk_aod_createOptions(char* optionsString) {119Options* options;120121if (!NSK_VERIFY((options = (Options*) nsk_aod_createOptionsObject(optionsString)) != NULL))122return NULL;123124if (!NSK_VERIFY(nsk_aod_optionSpecified(options, NSK_AOD_AGENT_NAME_OPTION))) {125NSK_COMPLAIN0("Agent name wasn't specified\n");126return NULL;127}128129/*130* verbose mode is true by default131*/132nsk_setVerboseMode(NSK_TRUE);133134if (nsk_aod_optionSpecified(options, NSK_AOD_VERBOSE_OPTION)) {135if (strcmp(nsk_aod_getOptionValue(options, NSK_AOD_VERBOSE_OPTION), "false") == 0)136nsk_setVerboseMode(NSK_FALSE);137}138139return options;140}141142const char* nsk_aod_getOptionValue(Options* options, const char* option) {143int i;144145if (!NSK_VERIFY(options != NULL)) {146NSK_COMPLAIN0("Options NULL\n");147return NULL;148}149150for (i = 0; i < options->size; i++) {151if (strcmp(option, options->names[i]) == 0) {152return options->values[i];153}154}155156NSK_COMPLAIN1("Option '%s' isn't defined\n", option);157158return NULL;159}160161int nsk_aod_optionSpecified(Options* options, const char* option) {162int i;163164if (!NSK_VERIFY(options != NULL)) {165NSK_COMPLAIN0("Options NULL\n");166return NSK_FALSE;167}168169for (i = 0; i < options->size; i++) {170if (strcmp(option, options->names[i]) == 0) {171return NSK_TRUE;172}173}174175return NSK_FALSE;176}177178/*179* Agent synchronization with target application180*/181182static const char* TARGET_APP_CLASS_NAME = "nsk/share/aod/TargetApplicationWaitingAgents";183184static const char* AGENT_LOADED_METHOD_NAME = "agentLoaded";185static const char* AGENT_LOADED_METHOD_SIGNATURE = "(Ljava/lang/String;)V";186187static const char* AGENT_FINISHED_METHOD_NAME = "agentFinished";188static const char* AGENT_FINISHED_METHOD_SIGNATURE = "(Ljava/lang/String;Z)V";189190static jclass targetAppClass = NULL;191static jmethodID agentLoadedMethod = NULL;192static jmethodID agentFinishedMethod = NULL;193194// this function is used to notify target application that native agent has been loaded195int nsk_aod_agentLoaded(JNIEnv* jni, const char* agentName) {196jstring agentNameString;197198NSK_DISPLAY1("Agent %s is loaded\n", agentName);199200if (targetAppClass == NULL) {201/*202* FindClass returns local reference, to cache reference to target application class203* global reference should be created204*/205jclass localTargetAppClass;206if (!NSK_JNI_VERIFY(jni, (localTargetAppClass =207jni->FindClass(TARGET_APP_CLASS_NAME)) != NULL)) {208return NSK_FALSE;209}210211if (!NSK_JNI_VERIFY(jni, (targetAppClass = (jclass)212jni->NewGlobalRef(localTargetAppClass)) != NULL)) {213return NSK_FALSE;214}215}216217if (agentLoadedMethod == NULL) {218if (!NSK_JNI_VERIFY(jni, (agentLoadedMethod =219jni->GetStaticMethodID(targetAppClass, AGENT_LOADED_METHOD_NAME, AGENT_LOADED_METHOD_SIGNATURE)) != NULL))220return NSK_FALSE;221}222223if (!NSK_JNI_VERIFY(jni, (agentNameString =224jni->NewStringUTF(agentName)) != NULL))225return NSK_FALSE;226227jni->CallStaticVoidMethod(targetAppClass, agentLoadedMethod, agentNameString);228229return NSK_TRUE;230}231232// this function is used to notify target application that native agent has been finished execution233int nsk_aod_agentFinished(JNIEnv* jni, const char* agentName, int success) {234jstring agentNameString;235236if (!targetAppClass) {237NSK_COMPLAIN1(238"%s: TEST LOGIC ERROR: method 'agentFinished' was called before "239"targetAppClass was initialized\n",240agentName);241return NSK_FALSE;242}243244if (internalError && success) {245success = 0;246NSK_COMPLAIN1(247"Status of agent '%s' is 'passed', but some error happened during test execution "248"(see log for details), change agent status to 'failed'\n",249agentName);250}251252NSK_DISPLAY2("Agent %s finished (success: %d)\n", agentName, success);253254if (agentFinishedMethod == NULL) {255if (!NSK_JNI_VERIFY(jni, (agentFinishedMethod =256jni->GetStaticMethodID(targetAppClass, AGENT_FINISHED_METHOD_NAME, AGENT_FINISHED_METHOD_SIGNATURE)) != NULL))257return NSK_FALSE;258}259260if (!NSK_JNI_VERIFY(jni, (agentNameString = jni->NewStringUTF(agentName)) != NULL))261return NSK_FALSE;262263jni->CallStaticVoidMethod(targetAppClass, agentFinishedMethod, agentNameString, success ? JNI_TRUE : JNI_FALSE);264265return NSK_TRUE;266}267268/*269* Auxiliary functions270*/271272// JNI env creation273274JNIEnv* nsk_aod_createJNIEnv(JavaVM* vm) {275JNIEnv* jni;276vm->GetEnv((void**)&jni, JNI_VERSION_1_2);277278NSK_VERIFY(jni != NULL);279280return jni;281}282283}284285286