1 /*
   2  * Copyright (c) 2003, 2018, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 #ifndef NSK_JVMTI_TOOLS_DEFINED
  25 #define NSK_JVMTI_TOOLS_DEFINED
  26 
  27 /*************************************************************/
  28 
  29 #include "jvmti.h"
  30 
  31 /*************************************************************/
  32 
  33 #include "nsk_tools.h"
  34 #include "jni_tools.h"
  35 #include "JVMTITools.h"
  36 
  37 
  38 #ifdef __cplusplus
  39 extern "C" {
  40 #endif
  41 
  42 
  43 /******************** Diagnostics errors *********************/
  44 
  45 /**
  46  * Call JVMTI function in action, check error code to be
  47  * JVMTI_ERROR_NONE and complain error otherwise.
  48  * Also trace action execution if tracing mode is on.
  49  */
  50 #define NSK_JVMTI_VERIFY(action)  \
  51     (nsk_ltrace(NSK_TRACE_BEFORE,__FILE__,__LINE__,"%s\n",#action), \
  52         nsk_jvmti_lverify(NSK_TRUE,action,JVMTI_ERROR_NONE, \
  53                             __FILE__,__LINE__,"%s\n",#action))
  54 
  55 /**
  56  * Call JVMTI function in action, check error code to be
  57  * not JVMTI_ERROR_NONE and complain error otherwise.
  58  * Also trace action execution if tracing mode is on.
  59  */
  60 #define NSK_JVMTI_VERIFY_NEGATIVE(action)  \
  61     (nsk_ltrace(NSK_TRACE_BEFORE,__FILE__,__LINE__,"%s\n",#action), \
  62         nsk_jvmti_lverify(NSK_FALSE,action,JVMTI_ERROR_NONE, \
  63                             __FILE__,__LINE__,"%s\n",#action))
  64 
  65 /**
  66  * Call JVMTI function in action, check error code to be
  67  * equal to 'code' and complain error otherwise.
  68  * Also trace action execution if tracing mode is on.
  69  */
  70 #define NSK_JVMTI_VERIFY_CODE(code, action)  \
  71     (nsk_ltrace(NSK_TRACE_BEFORE,__FILE__,__LINE__,"%s\n",#action), \
  72         nsk_jvmti_lverify(NSK_TRUE,action,code,__FILE__,__LINE__,"%s\n",#action))
  73 
  74 
  75 /********************* Initialization ************************/
  76 
  77 /**
  78  * Initialize framework and setup command line options for the JVMTI test.
  79  * If something fails, complains an error and returns 0 (NSK_FALSE).
  80  * On success returns 1 (NSK_TRUE).
  81  */
  82 int nsk_jvmti_parseOptions(const char options[]);
  83 
  84 /**
  85  * Creates JVMTI environment for the JVMTI test.
  86  * If something fails, complains an error and returns NULL.
  87  */
  88 jvmtiEnv* nsk_jvmti_createJVMTIEnv(JavaVM* jvm, void* reserved);
  89 
  90 /**
  91  * Register function to be run in agent thread.
  92  * If something fails, complains an error and returns 0 (NSK_FALSE).
  93  * On success returns 1 (NSK_TRUE).
  94  */
  95 int nsk_jvmti_setAgentProc(jvmtiStartFunction proc, void* arg);
  96 
  97 /**
  98  * Initialize multiple agent
  99  */
 100 int nsk_jvmti_init_MA(jvmtiEventCallbacks* callbacks);
 101 
 102 
 103 /********************** Agent thread *************************/
 104 
 105 /**
 106  * Returns thread object associated with agent thread..
 107  * If something fails, complains an error and returns NULL.
 108  */
 109 jthread nsk_jvmti_getAgentThread();
 110 
 111 /**
 112  * Returns JNI environment constructed for agent thread.
 113  * If something fails, complains an error and returns NULL.
 114  */
 115 JNIEnv* nsk_jvmti_getAgentJNIEnv();
 116 
 117 /**
 118  * Returns JVMTI environment constructed for agent.
 119  * If something fails, complains an error and returns NULL.
 120  */
 121 jvmtiEnv* nsk_jvmti_getAgentJVMTIEnv();
 122 
 123 /**
 124  * Waits for next synchronization point with debuggee class,
 125  * Then synchronizes current status and pauses debuggee thread.
 126  * If something fails, complains an error and returns 0 (NSK_FALSE).
 127  * On success returns 1 (NSK_TRUE).
 128  */
 129 int nsk_jvmti_waitForSync(jlong timeout);
 130 
 131 /**
 132  * Allow debuggee thread to continue execution after pausing on synchronization.
 133  * If something fails, complains an error and returns 0 (NSK_FALSE).
 134  * On success returns 1 (NSK_TRUE).
 135  */
 136 int nsk_jvmti_resumeSync();
 137 
 138 /**
 139  * Sleep current thread for given timeout in milliseconds.
 140  */
 141 void nsk_jvmti_sleep(jlong timeout);
 142 
 143 /**
 144  * Reset agent data to prepare for another run.
 145  */
 146 void nsk_jvmti_resetAgentData();
 147 
 148 /*********************** Agent status ************************/
 149 
 150 #define NSK_STATUS_PASSED       0
 151 #define NSK_STATUS_FAILED       2
 152 #define NSK_STATUS_BASE         95
 153 
 154 /**
 155  * Sets NSK_STATUS_FAILED as current agent status.
 156  */
 157 void nsk_jvmti_setFailStatus();
 158 
 159 /**
 160  * Returns 1 (NSK_TRUE) is current agent status is not NSK_STATUS_PASSED.
 161  * Returns 0 (NSK_FALSE) otherwise.
 162  */
 163 int  nsk_jvmti_isFailStatus();
 164 
 165 /**
 166  * Returns current agent status.
 167  */
 168 jint nsk_jvmti_getStatus();
 169 
 170 
 171 /********************* Classes and threads ******************/
 172 
 173 /**
 174  * Finds first class with given signatire among loaded classes.
 175  * If no class found or something fails, complains an error and returns NULL.
 176  * On success creates and returns global reference to the found class.
 177  */
 178 jclass nsk_jvmti_classBySignature(const char signature[]);
 179 
 180 /**
 181  * Finds first thread with given name among alive threads.
 182  * If no thread found or something fails, complains an error and returns NULL.
 183  * On success creates and returns global reference to the found thread.
 184  */
 185 jthread nsk_jvmti_threadByName(const char name[]);
 186 
 187 
 188 /******************* Breakpoints and locations ***************/
 189 
 190 /**
 191  * Requests all capabilities needed for finding line location.
 192  * If something fails, complains an error and returns 0 (NSK_FALSE).
 193  * On success returns 1 (NSK_TRUE).
 194  */
 195 int nsk_jvmti_addLocationCapabilities();
 196 
 197 /**
 198  * Requests all capabilities needed for setting breakpoints.
 199  * If something fails, complains an error and returns 0 (NSK_FALSE).
 200  * On success returns 1 (NSK_TRUE).
 201  */
 202 int nsk_jvmti_addBreakpointCapabilities();
 203 
 204 #define NSK_JVMTI_INVALID_JLOCATION     -2
 205 
 206 /**
 207  * Returns jlocation for given method line.
 208  * If something fails, complains an error and returns NSK_JVMTI_INVALID_JLOCATION.
 209  */
 210 jlocation nsk_jvmti_getLineLocation(jclass cls, jmethodID method, int line);
 211 
 212 /**
 213  * Sets breakpoint to the given method line and return breakpoint location.
 214  * If something fails, complains an error and returns NSK_JVMTI_INVALID_JLOCATION.
 215  */
 216 jlocation nsk_jvmti_setLineBreakpoint(jclass cls, jmethodID method, int line);
 217 
 218 /**
 219  * Removes breakpoint from the given method line and return breakpoint location.
 220  * If something fails, complains an error and returns NSK_JVMTI_INVALID_JLOCATION.
 221  */
 222 jlocation nsk_jvmti_clearLineBreakpoint(jclass cls, jmethodID method, int line);
 223 
 224 
 225 /********************* Events management *********************/
 226 
 227 /**
 228  * Enables or disables all events of given list for given thread or NULL.
 229  * If something fails, complains an error and returns 0 (NSK_FALSE).
 230  */
 231 int nsk_jvmti_enableEvents(jvmtiEventMode enable, int size,
 232                             jvmtiEvent list[], jthread thread);
 233 
 234 /**
 235  * Returns:
 236  *      NSK_TRUE if given event is of optional functionality.
 237  *      NSK_FALSE if given event is of required functionality.
 238  */
 239 int nsk_jvmti_isOptionalEvent(jvmtiEvent event);
 240 
 241 /**
 242  * Shows possessed capabilities
 243  */
 244 void nsk_jvmti_showPossessedCapabilities(jvmtiEnv *jvmti);
 245 
 246 /**
 247  * This method enables a single event
 248  * Return NSK_TRUE when on success and NSK_FALSE on failure.
 249  */
 250 int nsk_jvmti_enableNotification(jvmtiEnv *jvmti, jvmtiEvent event, jthread thread);
 251 
 252 /**
 253  * This method disables a single event
 254  * Return NSK_TRUE when on success and NSK_FALSE on failure.
 255  */
 256 
 257 int nsk_jvmti_disableNotification(jvmtiEnv *jvmti, jvmtiEvent event, jthread thread);
 258 
 259 
 260 /******************** Access test options ********************/
 261 
 262 /**
 263  * Returns value of given option name; or NULL if no such option found.
 264  * If search name is NULL then complains an error and returns NULL.
 265  */
 266 const char* nsk_jvmti_findOptionValue(const char name[]);
 267 
 268 /**
 269  * Returns string value of given option; or defaultValue if no such option found.
 270  * If options is specified but has empty value then complains an error and returns NULL.
 271  */
 272 const char* nsk_jvmti_findOptionStringValue(const char name[], const char* defaultValue);
 273 
 274 /**
 275  * Returns integer value of given option; or defaultValue if no such option found.
 276  * If options is specified but has no integer value then complains an error and returns -1.
 277  */
 278 int nsk_jvmti_findOptionIntValue(const char name[], int defaultValue);
 279 
 280 /**
 281  * Returns number of parsed options.
 282  */
 283 int nsk_jvmti_getOptionsCount();
 284 
 285 /**
 286  * Returns name of i-th parsed option.
 287  * If no such option then complains an error and returns NULL.
 288  */
 289 const char* nsk_jvmti_getOptionName(int i);
 290 
 291 /**
 292  * Returns value of i-th parsed option.
 293  * If no such option then complains an error and returns NULL.
 294  */
 295 const char* nsk_jvmti_getOptionValue(int i);
 296 
 297 
 298 /******************** Access system options ******************/
 299 
 300 /**
 301  * Returns value of -waittime option or default value if not specified.
 302  */
 303 int  nsk_jvmti_getWaitTime();
 304 
 305 /**
 306  * Sets specified waittime value.
 307  */
 308 void nsk_jvmti_setWaitTime(int waittime);
 309 
 310 
 311 /*************************************************************/
 312 
 313 /**
 314  * If positive, assert jvmtiError is equal to expected; or
 315  * if !positive, assert jvmtiError is not equal to expected.
 316  * Assert means: complain if the assertion is false.
 317  * Return the assertion value, either NSK_TRUE or NSK_FALSE.
 318  * Anyway, trace if "nsk_tools" mode is verbose.
 319  */
 320 int nsk_jvmti_lverify(int positive, jvmtiError code, jvmtiError expected,
 321                         const char file[], int line, const char format[], ...);
 322 
 323 /************************************************************/
 324 
 325 
 326 /**
 327  * This method could be useful for hotswap testcases developed under.`
 328  * nsk/jvmti/scenarios/hotswap.
 329  *
 330  */
 331  /**
 332   * This method will try to redefine the class (classToRedefine) by loading
 333   * physical file.  <b>pathToNewByteCode</b> option which is passed
 334   * on OnLoad Phase also used.
 335   *
 336   * So This method will do a file read pathToByteCode+fileName+.class (total path).
 337   * Constrcuts a class objects and does a redefine of the class.
 338   * On successfull redefine this method will return eaither JNI_TRUE or JNI_FALSE
 339   *
 340   * Hint::
 341   *     1)
 342   *      If there are many redefine on same testcase, then please try to use
 343   *      integer value (newclass00, newclass01, newclass02 , ....) way.
 344   *
 345   *     2) When you compile these please do keep, a metatag on testcase as
 346   *     # build : native classes classes.redef
 347   *
 348   *     3) When you do build these classes are psysically located in build as.
 349   *
 350   * TESTBASE/bin/newclass0* directory.
 351   * eg: for nks/jvmti/scenarios/hotswap/HS204/hs204t001 you should see
 352   * TESTBASE/bin/newclass0* /nsk/hotswap/HS204/hs204t001/MyClass.class
 353   *
 354   */
 355 
 356 int nsk_jvmti_redefineClass(jvmtiEnv * jvmti,
 357         jclass classToRedefine,
 358         const char * fileName);
 359 /**
 360  * changed this signature with Ekaterina's suggestion to move.
 361  *
 362  */
 363 void nsk_jvmti_getFileName(int redefineCnt, const char * dir,  char * buf, size_t bufsize);
 364 
 365 /**
 366  * This method sets agent status to failed, This would enables native agent to set its status.
 367  * There is <b>nsk_jvmti_setFailStatus()</b> method which is in sync with debugge/debugger combination.
 368  * For non-debugger agents, this method can be used. There is java wrapper for this status,
 369  * defined in java nsk.share.jvmti.RedefineAgent class as boolean : agentStatus().
 370  *
 371  */
 372 void nsk_jvmti_agentFailed();
 373 
 374 int isThreadExpected(jvmtiEnv *jvmti, jthread thread);
 375 
 376 jint createRawMonitor(jvmtiEnv *env, const char *name, jrawMonitorID *monitor);
 377 
 378 void exitOnError(jvmtiError error);
 379 
 380 /**
 381    Wrappers for corresponded JVMTI functions check error code and force exit on error
 382  */
 383 void rawMonitorEnter(jvmtiEnv *env, jrawMonitorID monitor);
 384 void rawMonitorExit(jvmtiEnv *env, jrawMonitorID monitor);
 385 void rawMonitorNotify(jvmtiEnv *env, jrawMonitorID monitor);
 386 void rawMonitorWait(jvmtiEnv *env, jrawMonitorID monitor, jlong millis);
 387 void getPhase(jvmtiEnv *env, jvmtiPhase *phase);
 388 
 389 /*******************************************************************/
 390 
 391 #if (defined(WIN32) || defined(_WIN32))
 392 #define snprintf _snprintf
 393 #endif
 394 
 395 
 396 #ifdef __cplusplus
 397 }
 398 #endif
 399 
 400 #endif