Jeff Brown | 481c157 | 2012-03-09 14:41:15 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 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 | |
John Reck | d5a9dc0 | 2018-07-16 10:42:35 -0700 | [diff] [blame] | 17 | #include <jni.h> |
Dan Albert | 46d8444 | 2014-11-18 16:07:51 -0800 | [diff] [blame] | 18 | |
Mark Salyzyn | 52eb4e0 | 2016-09-28 16:15:30 -0700 | [diff] [blame] | 19 | #include <cutils/trace.h> |
Mark Salyzyn | 52eb4e0 | 2016-09-28 16:15:30 -0700 | [diff] [blame] | 20 | #include <log/log.h> |
Steven Moreland | 2279b25 | 2017-07-19 09:50:45 -0700 | [diff] [blame] | 21 | #include <nativehelper/JNIHelp.h> |
John Reck | d5a9dc0 | 2018-07-16 10:42:35 -0700 | [diff] [blame] | 22 | |
| 23 | #include <array> |
Jamie Gennis | f9c7d6b | 2013-03-25 14:18:25 -0700 | [diff] [blame] | 24 | |
Jeff Brown | 481c157 | 2012-03-09 14:41:15 -0800 | [diff] [blame] | 25 | namespace android { |
| 26 | |
John Reck | d5a9dc0 | 2018-07-16 10:42:35 -0700 | [diff] [blame] | 27 | inline static void sanitizeString(char* str, size_t size) { |
Romain Guy | 9425f92 | 2013-04-10 16:35:48 -0700 | [diff] [blame] | 28 | for (size_t i = 0; i < size; i++) { |
| 29 | char c = str[i]; |
| 30 | if (c == '\0' || c == '\n' || c == '|') { |
| 31 | str[i] = ' '; |
| 32 | } |
| 33 | } |
Romain Guy | 9425f92 | 2013-04-10 16:35:48 -0700 | [diff] [blame] | 34 | } |
| 35 | |
John Reck | d5a9dc0 | 2018-07-16 10:42:35 -0700 | [diff] [blame] | 36 | inline static void getString(JNIEnv* env, jstring jstring, char* outBuffer, jsize maxSize) { |
| 37 | jsize size = std::min(env->GetStringLength(jstring), maxSize); |
| 38 | env->GetStringUTFRegion(jstring, 0, size, outBuffer); |
| 39 | sanitizeString(outBuffer, size); |
| 40 | outBuffer[size] = '\0'; |
| 41 | } |
| 42 | |
| 43 | template<typename F> |
| 44 | inline static void withString(JNIEnv* env, jstring jstr, F callback) { |
| 45 | std::array<char, 1024> buffer; |
| 46 | getString(env, jstr, buffer.data(), buffer.size()); |
| 47 | callback(buffer.data()); |
| 48 | } |
| 49 | |
| 50 | static jlong android_os_Trace_nativeGetEnabledTags(JNIEnv*, jclass) { |
Alex Ray | 8a6787b | 2012-11-14 18:08:49 -0800 | [diff] [blame] | 51 | return atrace_get_enabled_tags(); |
Jeff Brown | 481c157 | 2012-03-09 14:41:15 -0800 | [diff] [blame] | 52 | } |
| 53 | |
John Reck | d5a9dc0 | 2018-07-16 10:42:35 -0700 | [diff] [blame] | 54 | static void android_os_Trace_nativeTraceCounter(JNIEnv* env, jclass, |
John Reck | 77b31a5 | 2018-12-05 18:16:39 -0800 | [diff] [blame] | 55 | jlong tag, jstring nameStr, jlong value) { |
John Reck | d5a9dc0 | 2018-07-16 10:42:35 -0700 | [diff] [blame] | 56 | withString(env, nameStr, [tag, value](char* str) { |
John Reck | 77b31a5 | 2018-12-05 18:16:39 -0800 | [diff] [blame] | 57 | atrace_int64(tag, str, value); |
John Reck | d5a9dc0 | 2018-07-16 10:42:35 -0700 | [diff] [blame] | 58 | }); |
Jeff Brown | 481c157 | 2012-03-09 14:41:15 -0800 | [diff] [blame] | 59 | } |
| 60 | |
John Reck | d5a9dc0 | 2018-07-16 10:42:35 -0700 | [diff] [blame] | 61 | static void android_os_Trace_nativeTraceBegin(JNIEnv* env, jclass, |
Jeff Brown | 481c157 | 2012-03-09 14:41:15 -0800 | [diff] [blame] | 62 | jlong tag, jstring nameStr) { |
John Reck | d5a9dc0 | 2018-07-16 10:42:35 -0700 | [diff] [blame] | 63 | withString(env, nameStr, [tag](char* str) { |
| 64 | atrace_begin(tag, str); |
| 65 | }); |
Jeff Brown | 481c157 | 2012-03-09 14:41:15 -0800 | [diff] [blame] | 66 | } |
| 67 | |
John Reck | d5a9dc0 | 2018-07-16 10:42:35 -0700 | [diff] [blame] | 68 | static void android_os_Trace_nativeTraceEnd(JNIEnv*, jclass, jlong tag) { |
Alex Ray | 8a6787b | 2012-11-14 18:08:49 -0800 | [diff] [blame] | 69 | atrace_end(tag); |
Jeff Brown | 481c157 | 2012-03-09 14:41:15 -0800 | [diff] [blame] | 70 | } |
| 71 | |
John Reck | d5a9dc0 | 2018-07-16 10:42:35 -0700 | [diff] [blame] | 72 | static void android_os_Trace_nativeAsyncTraceBegin(JNIEnv* env, jclass, |
Romain Guy | 9425f92 | 2013-04-10 16:35:48 -0700 | [diff] [blame] | 73 | jlong tag, jstring nameStr, jint cookie) { |
John Reck | d5a9dc0 | 2018-07-16 10:42:35 -0700 | [diff] [blame] | 74 | withString(env, nameStr, [tag, cookie](char* str) { |
| 75 | atrace_async_begin(tag, str, cookie); |
| 76 | }); |
Romain Guy | 9425f92 | 2013-04-10 16:35:48 -0700 | [diff] [blame] | 77 | } |
| 78 | |
John Reck | d5a9dc0 | 2018-07-16 10:42:35 -0700 | [diff] [blame] | 79 | static void android_os_Trace_nativeAsyncTraceEnd(JNIEnv* env, jclass, |
Romain Guy | 9425f92 | 2013-04-10 16:35:48 -0700 | [diff] [blame] | 80 | jlong tag, jstring nameStr, jint cookie) { |
John Reck | d5a9dc0 | 2018-07-16 10:42:35 -0700 | [diff] [blame] | 81 | withString(env, nameStr, [tag, cookie](char* str) { |
| 82 | atrace_async_end(tag, str, cookie); |
| 83 | }); |
Romain Guy | 9425f92 | 2013-04-10 16:35:48 -0700 | [diff] [blame] | 84 | } |
| 85 | |
John Reck | d5a9dc0 | 2018-07-16 10:42:35 -0700 | [diff] [blame] | 86 | static void android_os_Trace_nativeSetAppTracingAllowed(JNIEnv*, jclass, jboolean allowed) { |
Jamie Gennis | f9c7d6b | 2013-03-25 14:18:25 -0700 | [diff] [blame] | 87 | atrace_set_debuggable(allowed); |
| 88 | } |
| 89 | |
John Reck | d5a9dc0 | 2018-07-16 10:42:35 -0700 | [diff] [blame] | 90 | static void android_os_Trace_nativeSetTracingEnabled(JNIEnv*, jclass, jboolean enabled) { |
Jamie Gennis | 6ad0452 | 2013-04-15 18:53:24 -0700 | [diff] [blame] | 91 | atrace_set_tracing_enabled(enabled); |
| 92 | } |
| 93 | |
Daniel Micay | 76f6a86 | 2015-09-19 17:31:01 -0400 | [diff] [blame] | 94 | static const JNINativeMethod gTraceMethods[] = { |
Jeff Brown | 481c157 | 2012-03-09 14:41:15 -0800 | [diff] [blame] | 95 | /* name, signature, funcPtr */ |
| 96 | { "nativeGetEnabledTags", |
| 97 | "()J", |
| 98 | (void*)android_os_Trace_nativeGetEnabledTags }, |
Jamie Gennis | f9c7d6b | 2013-03-25 14:18:25 -0700 | [diff] [blame] | 99 | { "nativeSetAppTracingAllowed", |
| 100 | "(Z)V", |
| 101 | (void*)android_os_Trace_nativeSetAppTracingAllowed }, |
Jamie Gennis | 6ad0452 | 2013-04-15 18:53:24 -0700 | [diff] [blame] | 102 | { "nativeSetTracingEnabled", |
| 103 | "(Z)V", |
| 104 | (void*)android_os_Trace_nativeSetTracingEnabled }, |
John Reck | 6be2cab | 2016-10-03 14:38:06 -0700 | [diff] [blame] | 105 | |
| 106 | // ----------- @FastNative ---------------- |
| 107 | |
| 108 | { "nativeTraceCounter", |
John Reck | 77b31a5 | 2018-12-05 18:16:39 -0800 | [diff] [blame] | 109 | "(JLjava/lang/String;J)V", |
John Reck | 6be2cab | 2016-10-03 14:38:06 -0700 | [diff] [blame] | 110 | (void*)android_os_Trace_nativeTraceCounter }, |
| 111 | { "nativeTraceBegin", |
| 112 | "(JLjava/lang/String;)V", |
| 113 | (void*)android_os_Trace_nativeTraceBegin }, |
| 114 | { "nativeTraceEnd", |
| 115 | "(J)V", |
| 116 | (void*)android_os_Trace_nativeTraceEnd }, |
| 117 | { "nativeAsyncTraceBegin", |
| 118 | "(JLjava/lang/String;I)V", |
| 119 | (void*)android_os_Trace_nativeAsyncTraceBegin }, |
| 120 | { "nativeAsyncTraceEnd", |
| 121 | "(JLjava/lang/String;I)V", |
| 122 | (void*)android_os_Trace_nativeAsyncTraceEnd }, |
Jeff Brown | 481c157 | 2012-03-09 14:41:15 -0800 | [diff] [blame] | 123 | }; |
| 124 | |
| 125 | int register_android_os_Trace(JNIEnv* env) { |
| 126 | int res = jniRegisterNativeMethods(env, "android/os/Trace", |
| 127 | gTraceMethods, NELEM(gTraceMethods)); |
Andreas Gampe | 987f79f | 2014-11-18 17:29:46 -0800 | [diff] [blame] | 128 | LOG_ALWAYS_FATAL_IF(res < 0, "Unable to register native methods."); |
Jeff Brown | 481c157 | 2012-03-09 14:41:15 -0800 | [diff] [blame] | 129 | |
| 130 | return 0; |
| 131 | } |
| 132 | |
| 133 | } // namespace android |