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