1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <signal.h>
18 #include <sys/types.h>
19 
20 #include <atomic>
21 
22 #include "android-base/logging.h"
23 #include "android-base/macros.h"
24 #include "jni.h"
25 #include "jvmti.h"
26 
27 // Test infrastructure
28 #include "jvmti_helper.h"
29 #include "test_env.h"
30 
31 namespace art {
32 namespace Test933MiscEvents {
33 
34 static std::atomic<bool> saw_dump_request(false);
35 
DumpRequestCallback(jvmtiEnv * jenv ATTRIBUTE_UNUSED)36 static void DumpRequestCallback(jvmtiEnv* jenv ATTRIBUTE_UNUSED) {
37   printf("Received dump request.\n");
38   saw_dump_request.store(true, std::memory_order::memory_order_relaxed);
39 }
40 
Java_art_Test933_testSigQuit(JNIEnv * env,jclass Main_klass ATTRIBUTE_UNUSED)41 extern "C" JNIEXPORT void JNICALL Java_art_Test933_testSigQuit(
42     JNIEnv* env, jclass Main_klass ATTRIBUTE_UNUSED) {
43   jvmtiEventCallbacks callbacks;
44   memset(&callbacks, 0, sizeof(jvmtiEventCallbacks));
45   callbacks.DataDumpRequest = DumpRequestCallback;
46   jvmtiError ret = jvmti_env->SetEventCallbacks(&callbacks, sizeof(callbacks));
47   if (JvmtiErrorToException(env, jvmti_env, ret)) {
48     return;
49   }
50 
51   ret = jvmti_env->SetEventNotificationMode(JVMTI_ENABLE,
52                                             JVMTI_EVENT_DATA_DUMP_REQUEST,
53                                             nullptr);
54   if (JvmtiErrorToException(env, jvmti_env, ret)) {
55     return;
56   }
57 
58   // Send sigquit to self.
59   kill(getpid(), SIGQUIT);
60 
61   // Busy-wait for request.
62   for (;;) {
63     sleep(1);
64     if (saw_dump_request.load(std::memory_order::memory_order_relaxed)) {
65       break;
66     }
67   }
68 
69   ret = jvmti_env->SetEventNotificationMode(JVMTI_DISABLE, JVMTI_EVENT_DATA_DUMP_REQUEST, nullptr);
70   JvmtiErrorToException(env, jvmti_env, ret);
71 }
72 
73 }  // namespace Test933MiscEvents
74 }  // namespace art
75