1 /*
   2  * Copyright (c) 2004, 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 #include <stdlib.h>
  25 #include <string.h>
  26 #include "jni_tools.h"
  27 #include "agent_common.h"
  28 #include "jvmti_tools.h"
  29 
  30 extern "C" {
  31 
  32 #define MAX_DEPTH 1024
  33 
  34 /* ========================================================================== */
  35 
  36 static const int NUMBER_OF_SAMPLES = 1000;
  37 static const int DISPLAYING_FREQUENCY = 100;
  38 static const jlong SAMPLING_INTERVAL = 10;
  39 
  40 /* scaffold objects */
  41 static jlong timeout = 0;
  42 
  43 /* test objects */
  44 static jthread thread = NULL;
  45 static jrawMonitorID waitLock = NULL;
  46 static jrawMonitorID frameLock = NULL;
  47 static int sampleCount = 0;
  48 static volatile int depth = 0;
  49 static jvmtiFrameInfo sampleStack[MAX_DEPTH];
  50 static jint frameCount = 0;
  51 static jvmtiFrameInfo frameBuffer[MAX_DEPTH];
  52 
  53 /* ========================================================================== */
  54 
  55 static int prepare(jvmtiEnv* jvmti, JNIEnv* jni) {
  56     const char* THREAD_NAME = "Debuggee Thread";
  57     jvmtiThreadInfo info;
  58     jthread *threads = NULL;
  59     jint threads_count = 0;
  60     int i;
  61 
  62     NSK_DISPLAY0("Prepare: find tested thread\n");
  63 
  64     /* get all live threads */
  65     if (!NSK_JVMTI_VERIFY(
  66            NSK_CPP_STUB3(GetAllThreads, jvmti, &threads_count, &threads)))
  67         return NSK_FALSE;
  68 
  69     if (!NSK_VERIFY(threads_count > 0 && threads != NULL))
  70         return NSK_FALSE;
  71 
  72     /* find tested thread */
  73     for (i = 0; i < threads_count; i++) {
  74         if (!NSK_VERIFY(threads[i] != NULL))
  75             return NSK_FALSE;
  76 
  77         /* get thread information */
  78         if (!NSK_JVMTI_VERIFY(
  79                 NSK_CPP_STUB3(GetThreadInfo, jvmti, threads[i], &info)))
  80             return NSK_FALSE;
  81 
  82         NSK_DISPLAY3("    thread #%d (%s): %p\n", i, info.name, threads[i]);
  83 
  84         /* find by name */
  85         if (info.name != NULL && (strcmp(info.name, THREAD_NAME) == 0)) {
  86             thread = threads[i];
  87         }
  88 
  89         if (info.name != NULL) {
  90             if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB2(
  91                     Deallocate, jvmti, (unsigned char*)info.name)))
  92                 return NSK_FALSE;
  93         }
  94     }
  95 
  96     /* deallocate threads list */
  97     if (!NSK_JVMTI_VERIFY(
  98             NSK_CPP_STUB2(Deallocate, jvmti, (unsigned char*)threads)))
  99         return NSK_FALSE;
 100 
 101     if (thread == NULL) {
 102         NSK_COMPLAIN0("Debuggee thread not found");
 103         return NSK_FALSE;
 104     }
 105 
 106     if (!NSK_JVMTI_VERIFY(
 107             NSK_CPP_STUB3(CreateRawMonitor, jvmti, "waitLock", &waitLock)))
 108         return NSK_FALSE;
 109 
 110     return NSK_TRUE;
 111 }
 112 
 113 /* ============================================================================= */
 114 
 115 static int wait_for(jvmtiEnv* jvmti, jlong millis) {
 116 
 117     if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB2(RawMonitorEnter, jvmti, waitLock)))
 118         return NSK_FALSE;
 119 
 120     if (!NSK_JVMTI_VERIFY(
 121             NSK_CPP_STUB3(RawMonitorWait, jvmti, waitLock, millis)))
 122         nsk_jvmti_setFailStatus();
 123 
 124     if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB2(RawMonitorExit, jvmti, waitLock)))
 125         return NSK_FALSE;
 126 
 127     return NSK_TRUE;
 128 }
 129 
 130 static int displayFrameInfo(jvmtiEnv* jvmti, jint i) {
 131     char buffer[32];
 132     char *name = NULL;
 133     char *signature = NULL;
 134 
 135     if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB5(GetMethodName, jvmti,
 136             frameBuffer[frameCount-1-i].method, &name, &signature, NULL)))
 137         return NSK_FALSE;
 138 
 139     NSK_DISPLAY4("    got[%d] method: %s%s, location: %s\n", i, name,
 140         signature, jlong_to_string(frameBuffer[frameCount-1-i].location, buffer));
 141     if (name != NULL)
 142         NSK_CPP_STUB2(Deallocate, jvmti, (unsigned char*)name);
 143     if (signature != NULL)
 144         NSK_CPP_STUB2(Deallocate, jvmti, (unsigned char*)signature);
 145 
 146     if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB5(GetMethodName, jvmti,
 147             sampleStack[i].method, &name, &signature, NULL)))
 148         return NSK_FALSE;
 149 
 150     NSK_DISPLAY4("    exp[%d] method: %s%s, location: %s\n", i, name,
 151         signature, jlong_to_string(sampleStack[i].location, buffer));
 152     if (name != NULL)
 153         NSK_CPP_STUB2(Deallocate, jvmti, (unsigned char*)name);
 154     if (signature != NULL)
 155         NSK_CPP_STUB2(Deallocate, jvmti, (unsigned char*)signature);
 156 
 157     return NSK_TRUE;
 158 }
 159 
 160 static int complainFrameInfo(jvmtiEnv* jvmti, jint i) {
 161     char buffer[32];
 162     char *name = NULL;
 163     char *signature = NULL;
 164 
 165     if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB5(GetMethodName, jvmti,
 166             frameBuffer[frameCount-1-i].method, &name, &signature, NULL)))
 167         return NSK_FALSE;
 168 
 169     NSK_COMPLAIN3("    got: method=%s%s, location=%s\n", name, signature,
 170         jlong_to_string(frameBuffer[frameCount-1-i].location, buffer));
 171     if (name != NULL)
 172         NSK_CPP_STUB2(Deallocate, jvmti, (unsigned char*)name);
 173     if (signature != NULL)
 174         NSK_CPP_STUB2(Deallocate, jvmti, (unsigned char*)signature);
 175 
 176     if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB5(GetMethodName, jvmti,
 177             sampleStack[i].method, &name, &signature, NULL)))
 178         return NSK_FALSE;
 179 
 180     NSK_COMPLAIN3("    expected: method=%s%s, location=%s\n", name, signature,
 181         jlong_to_string(sampleStack[i].location, buffer));
 182     if (name != NULL)
 183         NSK_CPP_STUB2(Deallocate, jvmti, (unsigned char*)name);
 184     if (signature != NULL)
 185         NSK_CPP_STUB2(Deallocate, jvmti, (unsigned char*)signature);
 186 
 187     return NSK_TRUE;
 188 }
 189 
 190 static int checkStackTrace(jvmtiEnv* jvmti, JNIEnv* jni) {
 191     jint i;
 192     int res = NSK_TRUE;
 193     int displayFlag =
 194         (nsk_getVerboseMode() && (sampleCount % DISPLAYING_FREQUENCY) == 0);
 195 
 196     if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB2(RawMonitorEnter, jvmti, frameLock)))
 197         return NSK_FALSE;
 198 
 199     /* get stack trace */
 200     if (!NSK_JVMTI_VERIFY(
 201             NSK_CPP_STUB6(GetStackTrace, jvmti, thread,
 202                 0, MAX_DEPTH, frameBuffer, &frameCount))) {
 203         res = NSK_FALSE;
 204     } else {
 205         if (displayFlag) {
 206             NSK_DISPLAY3("Sample #%d, frameCount: %d, depth: %d\n",
 207                 sampleCount, frameCount, depth);
 208         }
 209         if (!NSK_VERIFY(frameCount >= depth)) {
 210             NSK_COMPLAIN3("Sample #%d, wrong frameCount: %d, expected >= %d\n",
 211                 sampleCount, frameCount, depth);
 212             res = NSK_FALSE;
 213         } else {
 214             for (i = 0; i < depth; i++) {
 215                 if (displayFlag && !displayFrameInfo(jvmti, i))
 216                     res = NSK_FALSE;
 217                 if (!NSK_VERIFY(sampleStack[i].method ==
 218                         frameBuffer[frameCount-1-i].method) ||
 219                     !NSK_VERIFY(sampleStack[i].location ==
 220                         frameBuffer[frameCount-1-i].location)) {
 221 
 222                     NSK_COMPLAIN3("Sample #%d, depth=%d, wrong frame [%d]:\n",
 223                         sampleCount, depth, i);
 224                     complainFrameInfo(jvmti, i);
 225                     res = NSK_FALSE;
 226                 }
 227             }
 228         }
 229     }
 230 
 231     if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB2(RawMonitorExit, jvmti, frameLock)))
 232         return NSK_FALSE;
 233 
 234     return res;
 235 }
 236 
 237 /* ========================================================================== */
 238 
 239 /* agent algorithm */
 240 static void JNICALL
 241 agentProc(jvmtiEnv* jvmti, JNIEnv* jni, void* arg) {
 242 
 243     /* wait for initial sync */
 244     if (!nsk_jvmti_waitForSync(timeout))
 245         return;
 246 
 247     if (!prepare(jvmti, jni)) {
 248         nsk_jvmti_setFailStatus();
 249         return;
 250     }
 251 
 252     /* sample stack of the tested thread */
 253     for (sampleCount = 0;
 254          sampleCount < NUMBER_OF_SAMPLES && !nsk_jvmti_isFailStatus();
 255          sampleCount++) {
 256         wait_for(jvmti, SAMPLING_INTERVAL);
 257         if (!checkStackTrace(jvmti, jni))
 258             nsk_jvmti_setFailStatus();
 259     }
 260 
 261     /* resume debugee after last sync */
 262     if (!nsk_jvmti_resumeSync())
 263         return;
 264 }
 265 
 266 /* ========================================================================== */
 267 
 268 JNIEXPORT jint JNICALL
 269 Java_nsk_jvmti_scenarios_sampling_SP07_sp07t001Thread_wrapper(JNIEnv* jni,
 270         jobject obj, jint i) {
 271     jint result = 0;
 272     jclass klass = NULL;
 273     jmethodID method = NULL;
 274     jvmtiEnv* jvmti = nsk_jvmti_getAgentJVMTIEnv();
 275 
 276     if (!NSK_VERIFY(depth < MAX_DEPTH)) {
 277         nsk_jvmti_setFailStatus();
 278         return 0;
 279     }
 280 
 281     if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB2(RawMonitorEnter, jvmti, frameLock)))
 282         return NSK_FALSE;
 283 
 284     if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB5(GetFrameLocation, jvmti, NULL, 1,
 285             &sampleStack[depth].method, &sampleStack[depth].location))) {
 286         nsk_jvmti_setFailStatus();
 287         return 0;
 288     }
 289 
 290     depth++;
 291 
 292     if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB5(GetFrameLocation, jvmti, NULL, 0,
 293             &sampleStack[depth].method, &sampleStack[depth].location))) {
 294         nsk_jvmti_setFailStatus();
 295         return 0;
 296     }
 297 
 298     depth++;
 299 
 300     if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB2(RawMonitorExit, jvmti, frameLock)))
 301         return NSK_FALSE;
 302 
 303     if (!NSK_JNI_VERIFY(jni, (klass = NSK_CPP_STUB2(GetObjectClass,
 304             jni, obj)) != NULL)) {
 305         nsk_jvmti_setFailStatus();
 306         return 0;
 307     }
 308 
 309     if (!NSK_JNI_VERIFY(jni, (method = NSK_CPP_STUB4(GetMethodID,
 310             jni, klass, "fibonacci", "(I)I")) != NULL)) {
 311         nsk_jvmti_setFailStatus();
 312         return 0;
 313     }
 314 
 315     result = NSK_CPP_STUB4(CallIntMethod, jni, obj, method, i);
 316 
 317     if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB2(RawMonitorEnter, jvmti, frameLock)))
 318         return NSK_FALSE;
 319 
 320     depth--;
 321     depth--;
 322 
 323     if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB2(RawMonitorExit, jvmti, frameLock)))
 324         return NSK_FALSE;
 325 
 326     return result;
 327 }
 328 
 329 /* ========================================================================== */
 330 
 331 /* agent library initialization */
 332 #ifdef STATIC_BUILD
 333 JNIEXPORT jint JNICALL Agent_OnLoad_sp07t001(JavaVM *jvm, char *options, void *reserved) {
 334     return Agent_Initialize(jvm, options, reserved);
 335 }
 336 JNIEXPORT jint JNICALL Agent_OnAttach_sp07t001(JavaVM *jvm, char *options, void *reserved) {
 337     return Agent_Initialize(jvm, options, reserved);
 338 }
 339 JNIEXPORT jint JNI_OnLoad_sp07t001(JavaVM *jvm, char *options, void *reserved) {
 340     return JNI_VERSION_1_8;
 341 }
 342 #endif
 343 jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {
 344     jvmtiEnv* jvmti = NULL;
 345 
 346     /* init framework and parse options */
 347     if (!NSK_VERIFY(nsk_jvmti_parseOptions(options)))
 348         return JNI_ERR;
 349 
 350     timeout = nsk_jvmti_getWaitTime() * 60000;
 351     NSK_DISPLAY1("Timeout: %d msc\n", (int)timeout);
 352 
 353     /* create JVMTI environment */
 354     if (!NSK_VERIFY((jvmti =
 355             nsk_jvmti_createJVMTIEnv(jvm, reserved)) != NULL))
 356         return JNI_ERR;
 357 
 358     if (!NSK_JVMTI_VERIFY(
 359             NSK_CPP_STUB3(CreateRawMonitor, jvmti, "frameLock", &frameLock)))
 360         return NSK_FALSE;
 361 
 362     /* register agent proc and arg */
 363     if (!NSK_VERIFY(nsk_jvmti_setAgentProc(agentProc, NULL)))
 364         return JNI_ERR;
 365 
 366     return JNI_OK;
 367 }
 368 
 369 /* ========================================================================== */
 370 
 371 }