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 | 121e478 | 2019-06-12 16:57:59 -0700 | [diff] [blame] | 27 | inline static void sanitizeString(char* str) { |
| 28 | while (*str) { |
| 29 | char c = *str; |
| 30 | if (c == '\n' || c == '|') { |
| 31 | *str = ' '; |
Romain Guy | 9425f92 | 2013-04-10 16:35:48 -0700 | [diff] [blame] | 32 | } |
John Reck | 121e478 | 2019-06-12 16:57:59 -0700 | [diff] [blame] | 33 | str++; |
Romain Guy | 9425f92 | 2013-04-10 16:35:48 -0700 | [diff] [blame] | 34 | } |
Romain Guy | 9425f92 | 2013-04-10 16:35:48 -0700 | [diff] [blame] | 35 | } |
| 36 | |
John Reck | d5a9dc0 | 2018-07-16 10:42:35 -0700 | [diff] [blame] | 37 | template<typename F> |
| 38 | inline static void withString(JNIEnv* env, jstring jstr, F callback) { |
John Reck | 121e478 | 2019-06-12 16:57:59 -0700 | [diff] [blame] | 39 | // We need to handle the worst case of 1 character -> 4 bytes |
| 40 | // So make a buffer of size 4097 and let it hold a string with a maximum length |
| 41 | // of 1024. The extra last byte for the null terminator. |
| 42 | std::array<char, 4097> buffer; |
| 43 | // We have no idea of knowing how much data GetStringUTFRegion wrote, so null it out in |
| 44 | // advance so we can have a reliable null terminator |
| 45 | memset(buffer.data(), 0, buffer.size()); |
| 46 | jsize size = std::min(env->GetStringLength(jstr), 1024); |
| 47 | env->GetStringUTFRegion(jstr, 0, size, buffer.data()); |
| 48 | sanitizeString(buffer.data()); |
| 49 | |
John Reck | d5a9dc0 | 2018-07-16 10:42:35 -0700 | [diff] [blame] | 50 | callback(buffer.data()); |
| 51 | } |
| 52 | |
| 53 | static jlong android_os_Trace_nativeGetEnabledTags(JNIEnv*, jclass) { |
Alex Ray | 8a6787b | 2012-11-14 18:08:49 -0800 | [diff] [blame] | 54 | return atrace_get_enabled_tags(); |
Jeff Brown | 481c157 | 2012-03-09 14:41:15 -0800 | [diff] [blame] | 55 | } |
| 56 | |
John Reck | d5a9dc0 | 2018-07-16 10:42:35 -0700 | [diff] [blame] | 57 | static void android_os_Trace_nativeTraceCounter(JNIEnv* env, jclass, |
John Reck | 77b31a5 | 2018-12-05 18:16:39 -0800 | [diff] [blame] | 58 | jlong tag, jstring nameStr, jlong value) { |
John Reck | d5a9dc0 | 2018-07-16 10:42:35 -0700 | [diff] [blame] | 59 | withString(env, nameStr, [tag, value](char* str) { |
John Reck | 77b31a5 | 2018-12-05 18:16:39 -0800 | [diff] [blame] | 60 | atrace_int64(tag, str, value); |
John Reck | d5a9dc0 | 2018-07-16 10:42:35 -0700 | [diff] [blame] | 61 | }); |
Jeff Brown | 481c157 | 2012-03-09 14:41:15 -0800 | [diff] [blame] | 62 | } |
| 63 | |
John Reck | d5a9dc0 | 2018-07-16 10:42:35 -0700 | [diff] [blame] | 64 | static void android_os_Trace_nativeTraceBegin(JNIEnv* env, jclass, |
Jeff Brown | 481c157 | 2012-03-09 14:41:15 -0800 | [diff] [blame] | 65 | jlong tag, jstring nameStr) { |
John Reck | d5a9dc0 | 2018-07-16 10:42:35 -0700 | [diff] [blame] | 66 | withString(env, nameStr, [tag](char* str) { |
| 67 | atrace_begin(tag, str); |
| 68 | }); |
Jeff Brown | 481c157 | 2012-03-09 14:41:15 -0800 | [diff] [blame] | 69 | } |
| 70 | |
John Reck | d5a9dc0 | 2018-07-16 10:42:35 -0700 | [diff] [blame] | 71 | static void android_os_Trace_nativeTraceEnd(JNIEnv*, jclass, jlong tag) { |
Alex Ray | 8a6787b | 2012-11-14 18:08:49 -0800 | [diff] [blame] | 72 | atrace_end(tag); |
Jeff Brown | 481c157 | 2012-03-09 14:41:15 -0800 | [diff] [blame] | 73 | } |
| 74 | |
John Reck | d5a9dc0 | 2018-07-16 10:42:35 -0700 | [diff] [blame] | 75 | static void android_os_Trace_nativeAsyncTraceBegin(JNIEnv* env, jclass, |
Romain Guy | 9425f92 | 2013-04-10 16:35:48 -0700 | [diff] [blame] | 76 | jlong tag, jstring nameStr, jint cookie) { |
John Reck | d5a9dc0 | 2018-07-16 10:42:35 -0700 | [diff] [blame] | 77 | withString(env, nameStr, [tag, cookie](char* str) { |
| 78 | atrace_async_begin(tag, str, cookie); |
| 79 | }); |
Romain Guy | 9425f92 | 2013-04-10 16:35:48 -0700 | [diff] [blame] | 80 | } |
| 81 | |
John Reck | d5a9dc0 | 2018-07-16 10:42:35 -0700 | [diff] [blame] | 82 | static void android_os_Trace_nativeAsyncTraceEnd(JNIEnv* env, jclass, |
Romain Guy | 9425f92 | 2013-04-10 16:35:48 -0700 | [diff] [blame] | 83 | jlong tag, jstring nameStr, jint cookie) { |
John Reck | d5a9dc0 | 2018-07-16 10:42:35 -0700 | [diff] [blame] | 84 | withString(env, nameStr, [tag, cookie](char* str) { |
| 85 | atrace_async_end(tag, str, cookie); |
| 86 | }); |
Romain Guy | 9425f92 | 2013-04-10 16:35:48 -0700 | [diff] [blame] | 87 | } |
| 88 | |
John Reck | d5a9dc0 | 2018-07-16 10:42:35 -0700 | [diff] [blame] | 89 | static void android_os_Trace_nativeSetAppTracingAllowed(JNIEnv*, jclass, jboolean allowed) { |
Jamie Gennis | f9c7d6b | 2013-03-25 14:18:25 -0700 | [diff] [blame] | 90 | atrace_set_debuggable(allowed); |
| 91 | } |
| 92 | |
John Reck | d5a9dc0 | 2018-07-16 10:42:35 -0700 | [diff] [blame] | 93 | static void android_os_Trace_nativeSetTracingEnabled(JNIEnv*, jclass, jboolean enabled) { |
Jamie Gennis | 6ad0452 | 2013-04-15 18:53:24 -0700 | [diff] [blame] | 94 | atrace_set_tracing_enabled(enabled); |
| 95 | } |
| 96 | |
Daniel Micay | 76f6a86 | 2015-09-19 17:31:01 -0400 | [diff] [blame] | 97 | static const JNINativeMethod gTraceMethods[] = { |
Jeff Brown | 481c157 | 2012-03-09 14:41:15 -0800 | [diff] [blame] | 98 | /* name, signature, funcPtr */ |
| 99 | { "nativeGetEnabledTags", |
| 100 | "()J", |
| 101 | (void*)android_os_Trace_nativeGetEnabledTags }, |
Jamie Gennis | f9c7d6b | 2013-03-25 14:18:25 -0700 | [diff] [blame] | 102 | { "nativeSetAppTracingAllowed", |
| 103 | "(Z)V", |
| 104 | (void*)android_os_Trace_nativeSetAppTracingAllowed }, |
Jamie Gennis | 6ad0452 | 2013-04-15 18:53:24 -0700 | [diff] [blame] | 105 | { "nativeSetTracingEnabled", |
| 106 | "(Z)V", |
| 107 | (void*)android_os_Trace_nativeSetTracingEnabled }, |
John Reck | 6be2cab | 2016-10-03 14:38:06 -0700 | [diff] [blame] | 108 | |
| 109 | // ----------- @FastNative ---------------- |
| 110 | |
| 111 | { "nativeTraceCounter", |
John Reck | 77b31a5 | 2018-12-05 18:16:39 -0800 | [diff] [blame] | 112 | "(JLjava/lang/String;J)V", |
John Reck | 6be2cab | 2016-10-03 14:38:06 -0700 | [diff] [blame] | 113 | (void*)android_os_Trace_nativeTraceCounter }, |
| 114 | { "nativeTraceBegin", |
| 115 | "(JLjava/lang/String;)V", |
| 116 | (void*)android_os_Trace_nativeTraceBegin }, |
| 117 | { "nativeTraceEnd", |
| 118 | "(J)V", |
| 119 | (void*)android_os_Trace_nativeTraceEnd }, |
| 120 | { "nativeAsyncTraceBegin", |
| 121 | "(JLjava/lang/String;I)V", |
| 122 | (void*)android_os_Trace_nativeAsyncTraceBegin }, |
| 123 | { "nativeAsyncTraceEnd", |
| 124 | "(JLjava/lang/String;I)V", |
| 125 | (void*)android_os_Trace_nativeAsyncTraceEnd }, |
Jeff Brown | 481c157 | 2012-03-09 14:41:15 -0800 | [diff] [blame] | 126 | }; |
| 127 | |
| 128 | int register_android_os_Trace(JNIEnv* env) { |
| 129 | int res = jniRegisterNativeMethods(env, "android/os/Trace", |
| 130 | gTraceMethods, NELEM(gTraceMethods)); |
Andreas Gampe | 987f79f | 2014-11-18 17:29:46 -0800 | [diff] [blame] | 131 | LOG_ALWAYS_FATAL_IF(res < 0, "Unable to register native methods."); |
Jeff Brown | 481c157 | 2012-03-09 14:41:15 -0800 | [diff] [blame] | 132 | |
| 133 | return 0; |
| 134 | } |
| 135 | |
| 136 | } // namespace android |