Andreas Gampe | eb0cea1 | 2017-01-23 08:50:04 -0800 | [diff] [blame^] | 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 <atomic> |
| 18 | #include <signal.h> |
| 19 | #include <sys/types.h> |
| 20 | |
| 21 | #include "base/logging.h" |
| 22 | #include "base/macros.h" |
| 23 | #include "jni.h" |
| 24 | #include "openjdkjvmti/jvmti.h" |
| 25 | |
| 26 | #include "ti-agent/common_helper.h" |
| 27 | #include "ti-agent/common_load.h" |
| 28 | |
| 29 | namespace art { |
| 30 | namespace Test933MiscEvents { |
| 31 | |
| 32 | static std::atomic<bool> saw_dump_request(false); |
| 33 | |
| 34 | static void DumpRequestCallback(jvmtiEnv* jenv ATTRIBUTE_UNUSED) { |
| 35 | printf("Received dump request.\n"); |
| 36 | saw_dump_request.store(true, std::memory_order::memory_order_relaxed); |
| 37 | } |
| 38 | |
| 39 | extern "C" JNIEXPORT void JNICALL Java_Main_testSigQuit( |
| 40 | JNIEnv* env, jclass Main_klass ATTRIBUTE_UNUSED) { |
| 41 | jvmtiEventCallbacks callbacks; |
| 42 | memset(&callbacks, 0, sizeof(jvmtiEventCallbacks)); |
| 43 | callbacks.DataDumpRequest = DumpRequestCallback; |
| 44 | jvmtiError ret = jvmti_env->SetEventCallbacks(&callbacks, sizeof(callbacks)); |
| 45 | if (JvmtiErrorToException(env, ret)) { |
| 46 | return; |
| 47 | } |
| 48 | |
| 49 | ret = jvmti_env->SetEventNotificationMode(JVMTI_ENABLE, |
| 50 | JVMTI_EVENT_DATA_DUMP_REQUEST, |
| 51 | nullptr); |
| 52 | if (JvmtiErrorToException(env, ret)) { |
| 53 | return; |
| 54 | } |
| 55 | |
| 56 | // Send sigquit to self. |
| 57 | kill(getpid(), SIGQUIT); |
| 58 | |
| 59 | // Busy-wait for request. |
| 60 | for (;;) { |
| 61 | sleep(1); |
| 62 | if (saw_dump_request.load(std::memory_order::memory_order_relaxed)) { |
| 63 | break; |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | ret = jvmti_env->SetEventNotificationMode(JVMTI_DISABLE, JVMTI_EVENT_DATA_DUMP_REQUEST, nullptr); |
| 68 | JvmtiErrorToException(env, ret); |
| 69 | } |
| 70 | |
| 71 | } // namespace Test933MiscEvents |
| 72 | } // namespace art |