Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/share/jni/jni_tools.cpp
41161 views
/*1* Copyright (c) 2003, 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*/2223#include <stdlib.h>24#include <stdio.h>25#include <stdarg.h>26#include <string.h>27#include <ctype.h>2829/*************************************************************/30#if (defined(WIN32) || defined(_WIN32))31#include <windows.h>32#else33#include <unistd.h>34#include <sys/time.h>35#endif36/*************************************************************/3738#include "jni.h"3940/*************************************************************/4142#include "nsk_tools.h"43#include "jni_tools.h"4445/*************************************************************/4647extern "C" {4849/*************************************************************/5051int nsk_jni_check_exception(JNIEnv* jni, const char file[], int line)52{53jthrowable throwable;5455NSK_TRACE(throwable = jni->ExceptionOccurred());56if (throwable != NULL) {57nsk_lcomplain(file, line, "Exception in JNI call (cleared):\n");58NSK_TRACE(jni->ExceptionDescribe());59NSK_TRACE(jni->ExceptionClear());60return NSK_TRUE;61}62return NSK_FALSE;63}6465int nsk_jni_lverify(int positive, JNIEnv* jni, int status,66const char file[], int line, const char format[], ...)67{68int failure=0;69int negative = !positive;70va_list ap;71va_start(ap,format);7273nsk_lvtrace(NSK_TRACE_AFTER,file,line,format,ap);74if (status == negative) {75nsk_lvcomplain(file,line,format,ap);76nsk_printf("# verified JNI assertion is FALSE\n");77failure=1;78}7980failure = nsk_jni_check_exception(jni, file, line) || failure;8182va_end(ap);83return !failure;84}8586int nsk_jni_lverify_void(JNIEnv* jni, const char file[], int line,87const char format[], ...)88{89int failure=0;90va_list ap;91va_start(ap,format);9293nsk_lvtrace(NSK_TRACE_AFTER,file,line,format,ap);94failure = nsk_jni_check_exception(jni, file, line);9596if (failure)97nsk_lvcomplain(file,line,format,ap);9899va_end(ap);100return !failure;101}102103char *jlong_to_string(jlong value, char *string) {104char buffer[32];105char *pbuf, *pstr;106107pstr = string;108if (value == 0) {109*pstr++ = '0';110} else {111if (value < 0) {112*pstr++ = '-';113value = -value;114}115pbuf = buffer;116while (value != 0) {117*pbuf++ = '0' + (char)(value % 10);118value = value / 10;119}120while (pbuf != buffer) {121*pstr++ = *--pbuf;122}123}124*pstr = '\0';125126return string;127}128129char *julong_to_string(julong value, char *string) {130char buffer[32];131char *pbuf, *pstr;132133pstr = string;134if (value == 0) {135*pstr++ = '0';136} else {137pbuf = buffer;138while (value != 0) {139*pbuf++ = '0' + (char)(value % 10);140value = value / 10;141}142while (pbuf != buffer) {143*pstr++ = *--pbuf;144}145}146*pstr = '\0';147148return string;149}150151void mssleep(long millis) {152#if (defined(WIN32) || defined(_WIN32))153Sleep(millis);154#else155/* Using select for portable sleep */156/* Not using usleep because of it's possible interaction with SIGALRM */157struct timeval timeout;158timeout.tv_sec = millis / 1000;159timeout.tv_usec = (millis % 1000) * 1000;160select(0, NULL, NULL, NULL, &timeout);161#endif162}163164void165jni_print_vmargs(JavaVMInitArgs vmargs)166{167int i = 0;168169printf("JavaVMInitArgs:\n");170printf(" version = %d\n", vmargs.version);171printf(" ignoreUnrecognized = %d\n", vmargs.ignoreUnrecognized);172173printf(" vmargs.nOptions = %d\n", vmargs.nOptions);174for (i = 0; i < vmargs.nOptions; i++) {175printf(" options[%d].optionString = %s\n", i, vmargs.options[i].optionString);176printf(" options[%d].extraInfo = %p\n", i, vmargs.options[i].extraInfo);177}178}179180JavaVMOption*181jni_create_vmoptions(int size, char *args[], int argsCnt)182{183int i;184JavaVMOption *options = NULL;185186if (size <= 0)187return options;188189options = (JavaVMOption*)calloc(size, sizeof(JavaVMOption));190191for (i=0; i<argsCnt; i++)192options[i].optionString = args[i];193194return options;195}196197/*************************************************************/198199}200201202