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 #include <stdio.h>
25 #include <string.h>
26 #include "jvmti.h"
27 #include "jvmti_common.h"
28 
29 extern "C" {
30 
31 static jvmtiEnv *jvmti_env = NULL;
32 
33 jint Agent_OnLoad(JavaVM *jvm, char *options, void *reserved) {
34     jint res = jvm->GetEnv((void **) &jvmti_env, JVMTI_VERSION_1_1);
35     if (res != JNI_OK || jvmti_env == NULL) {
36         LOG("Wrong test_passed of a valid call to GetEnv!\n");
37         return JNI_ERR;
38     }
39 
40     return JNI_OK;
41 }
42 
43 JNIEXPORT jboolean JNICALL
44 Java_GetAllThreadsNullTest_check(JNIEnv *env, jclass cls) {
45     jvmtiError err;
46     jint threadsCountPtr;
47     jthread *threadsPtr;
48     jboolean test_passed = JNI_TRUE;
49 
50     if (jvmti_env == NULL) {
51         LOG("JVMTI client was not properly loaded!\n");
52         return JNI_FALSE;
53     }
54 
55     LOG(">>> (threadsCountPtr) null pointer check ...\n");
56 
57     err = jvmti_env->GetAllThreads(NULL, &threadsPtr);
58     if (err != JVMTI_ERROR_NULL_POINTER) {
59         LOG("(threadsCountPtr) error expected: JVMTI_ERROR_NULL_POINTER,\n");
60         LOG("           got: %s (%d)\n", TranslateError(err), err);
61         test_passed = JNI_FALSE;
62     }
63 
64     LOG(">>> (threadsPtr) null pointer check ...\n");
65 
66     err = jvmti_env->GetAllThreads(&threadsCountPtr, NULL);
67     if (err != JVMTI_ERROR_NULL_POINTER) {
68         LOG("(threadsPtr) error expected: JVMTI_ERROR_NULL_POINTER,\n");
69         LOG("           got: %s (%d)\n", TranslateError(err), err);
70         test_passed = JNI_FALSE;
71     }
72 
73     err = jvmti_env->GetAllThreads(NULL, NULL);
74     if (err != JVMTI_ERROR_NULL_POINTER) {
75       LOG("(threadsPtr) error expected: JVMTI_ERROR_NULL_POINTER,\n");
76       LOG("           got: %s (%d)\n", TranslateError(err), err);
77       test_passed = JNI_FALSE;
78     }
79 
80     LOG(">>> done\n");
81 
82     return test_passed;
83 }
84 
85 }