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