blob: 2b3f8c8c55af8ffa374b99a3bcbb12776e52e4a0 [file] [log] [blame]
Alex Light4c174282017-07-05 10:18:18 -07001/*
2 * Copyright (C) 2013 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
Alex Light4c174282017-07-05 10:18:18 -070017#include <pthread.h>
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070018
19#include <cstdio>
20#include <iostream>
Alex Light4c174282017-07-05 10:18:18 -070021#include <vector>
22
23#include "android-base/logging.h"
24#include "jni.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070025#include "jvmti.h"
26
Alex Light4c174282017-07-05 10:18:18 -070027#include "scoped_local_ref.h"
28#include "scoped_primitive_array.h"
29
Alex Light4c174282017-07-05 10:18:18 -070030// Test infrastructure
31#include "jvmti_helper.h"
32#include "test_env.h"
33
34namespace art {
35namespace Test1901Bytecodes {
36
37extern "C" JNIEXPORT jbyteArray JNICALL Java_art_Test1901_getBytecodes(JNIEnv* env,
38 jclass,
39 jobject jmethod) {
40 jmethodID method = env->FromReflectedMethod(jmethod);
41 if (env->ExceptionCheck()) {
42 return nullptr;
43 }
44 unsigned char* bytecodes = nullptr;
45 jint bytecodes_size = 0;
46 if (JvmtiErrorToException(env,
47 jvmti_env,
48 jvmti_env->GetBytecodes(method, &bytecodes_size, &bytecodes))) {
49 return nullptr;
50 }
51 jbyteArray out = env->NewByteArray(bytecodes_size);
52 if (env->ExceptionCheck()) {
53 return nullptr;
54 } else if (bytecodes_size == 0) {
55 return out;
56 }
57 jbyte* bytes = env->GetByteArrayElements(out, /* is_copy */ nullptr);
58 memcpy(bytes, bytecodes, bytecodes_size);
59 env->ReleaseByteArrayElements(out, bytes, 0);
60 return out;
61}
62
63} // namespace Test1901Bytecodes
64} // namespace art