changeset 55313:ea4774a27d9f

8223040: Add a AsyncGetCallTrace test Summary: Add a AsyncGetCallTrace test Reviewed-by: sspitsyn, dcubed
author jcbeyler
date Wed, 08 May 2019 15:36:10 -0700
parents ef73702a906e
children c2551d161358
files make/test/JtregNativeHotspot.gmk test/hotspot/jtreg/serviceability/AsyncGetCallTrace/MyPackage/ASGCTBaseTest.java test/hotspot/jtreg/serviceability/AsyncGetCallTrace/libAsyncGetCallTraceTest.cpp
diffstat 3 files changed, 291 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/make/test/JtregNativeHotspot.gmk	Fri May 10 13:37:00 2019 -0700
+++ b/make/test/JtregNativeHotspot.gmk	Wed May 08 15:36:10 2019 -0700
@@ -864,9 +864,10 @@
     BUILD_HOTSPOT_JTREG_EXECUTABLES_LIBS_exestack-gap := -ljvm -lpthread
     BUILD_TEST_exeinvoke_exeinvoke.c_OPTIMIZATION := NONE
     BUILD_HOTSPOT_JTREG_EXECUTABLES_LIBS_exeFPRegs := -ldl
+    BUILD_HOTSPOT_JTREG_LIBRARIES_LDFLAGS_libAsyncGetCallTraceTest := -ldl
 else
   BUILD_HOTSPOT_JTREG_EXCLUDE += libtest-rw.c libtest-rwx.c libTestJNI.c \
-      exeinvoke.c exestack-gap.c
+      exeinvoke.c exestack-gap.c libAsyncGetCallTraceTest.cpp
 endif
 
 BUILD_HOTSPOT_JTREG_EXECUTABLES_LIBS_exesigtest := -ljvm
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/hotspot/jtreg/serviceability/AsyncGetCallTrace/MyPackage/ASGCTBaseTest.java	Wed May 08 15:36:10 2019 -0700
@@ -0,0 +1,53 @@
+/*
+ * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2019, Google and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+package MyPackage;
+
+/**
+ * @test
+ * @summary Verifies that AsyncGetCallTrace is call-able and provides sane information.
+ * @compile ASGCTBaseTest.java
+ * @requires (os.family == "linux")
+ * @run main/othervm/native -agentlib:AsyncGetCallTraceTest MyPackage.ASGCTBaseTest
+ */
+
+public class ASGCTBaseTest {
+  static {
+    try {
+      System.loadLibrary("AsyncGetCallTraceTest");
+    } catch (UnsatisfiedLinkError ule) {
+      System.err.println("Could not load AsyncGetCallTrace library");
+      System.err.println("java.library.path: " + System.getProperty("java.library.path"));
+      throw ule;
+    }
+  }
+
+  private static native boolean checkAsyncGetCallTraceCall();
+
+  public static void main(String[] args) {
+    if (!checkAsyncGetCallTraceCall()) {
+      throw new RuntimeException("AsyncGetCallTrace call failed.");
+    }
+  }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/hotspot/jtreg/serviceability/AsyncGetCallTrace/libAsyncGetCallTraceTest.cpp	Wed May 08 15:36:10 2019 -0700
@@ -0,0 +1,236 @@
+/*
+ * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2019, Google and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+#include <assert.h>
+#include <dlfcn.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include "jvmti.h"
+
+static jvmtiEnv* jvmti;
+
+template <class T>
+class JvmtiDeallocator {
+ public:
+  JvmtiDeallocator() {
+    elem_ = NULL;
+  }
+
+  ~JvmtiDeallocator() {
+    jvmti->Deallocate(reinterpret_cast<unsigned char*>(elem_));
+  }
+
+  T* get_addr() {
+    return &elem_;
+  }
+
+  T get() {
+    return elem_;
+  }
+
+ private:
+  T elem_;
+};
+
+static void GetJMethodIDs(jclass klass) {
+  jint method_count = 0;
+  JvmtiDeallocator<jmethodID*> methods;
+  jvmtiError err = jvmti->GetClassMethods(klass, &method_count, methods.get_addr());
+
+  // If ever the GetClassMethods fails, just ignore it, it was worth a try.
+  if (err != JVMTI_ERROR_NONE) {
+    fprintf(stderr, "GetJMethodIDs: Error in GetClassMethods: %d\n", err);
+  }
+}
+
+// AsyncGetCallTrace needs class loading events to be turned on!
+static void JNICALL OnClassLoad(jvmtiEnv *jvmti, JNIEnv *jni_env,
+                                jthread thread, jclass klass) {
+}
+
+static void JNICALL OnClassPrepare(jvmtiEnv *jvmti, JNIEnv *jni_env,
+                                   jthread thread, jclass klass) {
+  // We need to do this to "prime the pump" and get jmethodIDs primed.
+  GetJMethodIDs(klass);
+}
+
+static void JNICALL OnVMInit(jvmtiEnv *jvmti, JNIEnv *jni_env, jthread thread) {
+  jint class_count = 0;
+
+  // Get any previously loaded classes that won't have gone through the
+  // OnClassPrepare callback to prime the jmethods for AsyncGetCallTrace.
+  JvmtiDeallocator<jclass*> classes;
+  jvmtiError err = jvmti->GetLoadedClasses(&class_count, classes.get_addr());
+  if (err != JVMTI_ERROR_NONE) {
+    fprintf(stderr, "OnVMInit: Error in GetLoadedClasses: %d\n", err);
+    return;
+  }
+
+  // Prime any class already loaded and try to get the jmethodIDs set up.
+  jclass *classList = classes.get();
+  for (int i = 0; i < class_count; ++i) {
+    GetJMethodIDs(classList[i]);
+  }
+}
+
+extern "C" {
+
+static
+jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {
+  jint res = jvm->GetEnv((void **) &jvmti, JVMTI_VERSION);
+  if (res != JNI_OK || jvmti == NULL) {
+    fprintf(stderr, "Error: wrong result of a valid call to GetEnv!\n");
+    return JNI_ERR;
+  }
+
+  jvmtiError err;
+  jvmtiCapabilities caps;
+  memset(&caps, 0, sizeof(caps));
+  caps.can_get_line_numbers = 1;
+  caps.can_get_source_file_name = 1;
+
+  err = jvmti->AddCapabilities(&caps);
+  if (err != JVMTI_ERROR_NONE) {
+    fprintf(stderr, "AgentInitialize: Error in AddCapabilities: %d\n", err);
+    return JNI_ERR;
+  }
+
+  jvmtiEventCallbacks callbacks;
+  memset(&callbacks, 0, sizeof(callbacks));
+  callbacks.ClassLoad = &OnClassLoad;
+  callbacks.VMInit = &OnVMInit;
+  callbacks.ClassPrepare = &OnClassPrepare;
+
+  err = jvmti->SetEventCallbacks(&callbacks, sizeof(jvmtiEventCallbacks));
+  if (err != JVMTI_ERROR_NONE) {
+    fprintf(stderr, "AgentInitialize: Error in SetEventCallbacks: %d\n", err);
+    return JNI_ERR;
+  }
+
+  err = jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_CLASS_LOAD, NULL);
+  if (err != JVMTI_ERROR_NONE) {
+    fprintf(stderr, "AgentInitialize: Error in SetEventNotificationMode for CLASS_LOAD: %d\n", err);
+    return JNI_ERR;
+  }
+
+  err = jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_CLASS_PREPARE, NULL);
+  if (err != JVMTI_ERROR_NONE) {
+    fprintf(stderr,
+            "AgentInitialize: Error in SetEventNotificationMode for CLASS_PREPARE: %d\n",
+            err);
+    return JNI_ERR;
+  }
+
+  err = jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_VM_INIT, NULL);
+  if (err != JVMTI_ERROR_NONE) {
+    fprintf(
+        stderr, "AgentInitialize: Error in SetEventNotificationMode for VM_INIT: %d\n",
+        err);
+    return JNI_ERR;
+  }
+
+  return JNI_OK;
+}
+
+JNIEXPORT
+jint JNICALL Agent_OnLoad(JavaVM *jvm, char *options, void *reserved) {
+  return Agent_Initialize(jvm, options, reserved);
+}
+
+JNIEXPORT
+jint JNICALL Agent_OnAttach(JavaVM *jvm, char *options, void *reserved) {
+  return Agent_Initialize(jvm, options, reserved);
+}
+
+JNIEXPORT
+jint JNICALL JNI_OnLoad(JavaVM *jvm, void *reserved) {
+  return JNI_VERSION_1_8;
+}
+
+// A copy of the ASGCT data structures.
+typedef struct {
+    jint lineno;                      // line number in the source file
+    jmethodID method_id;              // method executed in this frame
+} ASGCT_CallFrame;
+
+typedef struct {
+    JNIEnv *env_id;                   // Env where trace was recorded
+    jint num_frames;                  // number of frames in this trace
+    ASGCT_CallFrame *frames;          // frames
+} ASGCT_CallTrace;
+
+typedef void (*ASGCTType)(ASGCT_CallTrace *, jint, void *);
+
+JNIEXPORT jboolean JNICALL
+Java_MyPackage_ASGCTBaseTest_checkAsyncGetCallTraceCall(JNIEnv* env, jclass cls) {
+  ASGCTType agct = reinterpret_cast<ASGCTType>(dlsym(NULL, "AsyncGetCallTrace"));
+
+  const int MAX_DEPTH = 16;
+  ASGCT_CallTrace trace;
+  ASGCT_CallFrame frames[MAX_DEPTH];
+  trace.frames = frames;
+  trace.env_id = env;
+  trace.num_frames = 0;
+
+  if (agct == NULL) {
+    fprintf(stderr, "AsyncGetCallTrace not found.\n");
+    return false;
+  }
+
+  agct(&trace, MAX_DEPTH, NULL);
+
+  // For now, just check that the first frame is (-3, checkAsyncGetCallTraceCall).
+  if (trace.num_frames <= 0) {
+    fprintf(stderr, "The num_frames must be positive: %d\n", trace.num_frames);
+    return false;
+  }
+
+  // AsyncGetCallTrace returns -3 as line number for a native frame.
+  if (trace.frames[0].lineno != -3) {
+    fprintf(stderr, "lineno is not -3 as expected: %d\n", trace.frames[0].lineno);
+    return false;
+  }
+
+  JvmtiDeallocator<char*> name;
+  if (trace.frames[0].method_id == NULL) {
+    fprintf(stderr, "First frame method_id is NULL\n");
+    return false;
+  }
+
+  jvmtiError err = jvmti->GetMethodName(trace.frames[0].method_id, name.get_addr(), NULL, NULL);
+  if (err != JVMTI_ERROR_NONE) {
+    fprintf(stderr, "checkAsyncGetCallTrace: Error in GetMethodName: %d\n", err);
+    return false;
+  }
+
+  if (name.get() == NULL) {
+    fprintf(stderr, "Name is NULL\n");
+    return false;
+  }
+
+  return strcmp(name.get(), "checkAsyncGetCallTraceCall") == 0;
+}
+
+}