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