blob: 4f4e5da22c8d0c726a617caf199929b35a7d6656 [file] [log] [blame]
Jeff Brown481c1572012-03-09 14:41:15 -08001/*
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 Murashkinc7282eb2013-10-17 18:19:48 -070018// #define LOG_NDEBUG 0
Jeff Brown481c1572012-03-09 14:41:15 -080019
Dan Albert46d84442014-11-18 16:07:51 -080020#include <inttypes.h>
21
Mark Salyzyn52eb4e02016-09-28 16:15:30 -070022#include <cutils/trace.h>
23#include <utils/String8.h>
24#include <log/log.h>
25
Steven Moreland2279b252017-07-19 09:50:45 -070026#include <nativehelper/JNIHelp.h>
27#include <nativehelper/ScopedUtfChars.h>
28#include <nativehelper/ScopedStringChars.h>
Jamie Gennisf9c7d6b2013-03-25 14:18:25 -070029
Jeff Brown481c1572012-03-09 14:41:15 -080030namespace android {
31
Romain Guy9425f922013-04-10 16:35:48 -070032static 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 Brown481c1572012-03-09 14:41:15 -080044static jlong android_os_Trace_nativeGetEnabledTags(JNIEnv* env, jclass clazz) {
Alex Ray8a6787b2012-11-14 18:08:49 -080045 return atrace_get_enabled_tags();
Jeff Brown481c1572012-03-09 14:41:15 -080046}
47
48static void android_os_Trace_nativeTraceCounter(JNIEnv* env, jclass clazz,
49 jlong tag, jstring nameStr, jint value) {
50 ScopedUtfChars name(env, nameStr);
Igor Murashkinc7282eb2013-10-17 18:19:48 -070051
Dan Albert46d84442014-11-18 16:07:51 -080052 ALOGV("%s: %" PRId64 " %s %d", __FUNCTION__, tag, name.c_str(), value);
Alex Ray8a6787b2012-11-14 18:08:49 -080053 atrace_int(tag, name.c_str(), value);
Jeff Brown481c1572012-03-09 14:41:15 -080054}
55
56static void android_os_Trace_nativeTraceBegin(JNIEnv* env, jclass clazz,
57 jlong tag, jstring nameStr) {
Jamie Gennisf9c7d6b2013-03-25 14:18:25 -070058 ScopedStringChars jchars(env, nameStr);
Romain Guy9425f922013-04-10 16:35:48 -070059 String8 utf8Chars(reinterpret_cast<const char16_t*>(jchars.get()), jchars.size());
60 sanitizeString(utf8Chars);
Igor Murashkinc7282eb2013-10-17 18:19:48 -070061
Dan Albert46d84442014-11-18 16:07:51 -080062 ALOGV("%s: %" PRId64 " %s", __FUNCTION__, tag, utf8Chars.string());
Jamie Gennisf9c7d6b2013-03-25 14:18:25 -070063 atrace_begin(tag, utf8Chars.string());
Jeff Brown481c1572012-03-09 14:41:15 -080064}
65
66static void android_os_Trace_nativeTraceEnd(JNIEnv* env, jclass clazz,
67 jlong tag) {
Igor Murashkinc7282eb2013-10-17 18:19:48 -070068
Dan Albert46d84442014-11-18 16:07:51 -080069 ALOGV("%s: %" PRId64, __FUNCTION__, tag);
Alex Ray8a6787b2012-11-14 18:08:49 -080070 atrace_end(tag);
Jeff Brown481c1572012-03-09 14:41:15 -080071}
72
Romain Guy9425f922013-04-10 16:35:48 -070073static void android_os_Trace_nativeAsyncTraceBegin(JNIEnv* env, jclass clazz,
74 jlong tag, jstring nameStr, jint cookie) {
Romain Guy9425f922013-04-10 16:35:48 -070075 ScopedStringChars jchars(env, nameStr);
76 String8 utf8Chars(reinterpret_cast<const char16_t*>(jchars.get()), jchars.size());
77 sanitizeString(utf8Chars);
Igor Murashkinc7282eb2013-10-17 18:19:48 -070078
Dan Albert46d84442014-11-18 16:07:51 -080079 ALOGV("%s: %" PRId64 " %s %d", __FUNCTION__, tag, utf8Chars.string(), cookie);
Romain Guy9425f922013-04-10 16:35:48 -070080 atrace_async_begin(tag, utf8Chars.string(), cookie);
81}
82
83static void android_os_Trace_nativeAsyncTraceEnd(JNIEnv* env, jclass clazz,
84 jlong tag, jstring nameStr, jint cookie) {
Romain Guy9425f922013-04-10 16:35:48 -070085 ScopedStringChars jchars(env, nameStr);
86 String8 utf8Chars(reinterpret_cast<const char16_t*>(jchars.get()), jchars.size());
87 sanitizeString(utf8Chars);
Igor Murashkinc7282eb2013-10-17 18:19:48 -070088
Dan Albert46d84442014-11-18 16:07:51 -080089 ALOGV("%s: %" PRId64 " %s %d", __FUNCTION__, tag, utf8Chars.string(), cookie);
Romain Guy9425f922013-04-10 16:35:48 -070090 atrace_async_end(tag, utf8Chars.string(), cookie);
91}
92
Jamie Gennisf9c7d6b2013-03-25 14:18:25 -070093static void android_os_Trace_nativeSetAppTracingAllowed(JNIEnv* env,
94 jclass clazz, jboolean allowed) {
Igor Murashkinc7282eb2013-10-17 18:19:48 -070095 ALOGV("%s: %d", __FUNCTION__, allowed);
96
Jamie Gennisf9c7d6b2013-03-25 14:18:25 -070097 atrace_set_debuggable(allowed);
98}
99
Jamie Gennis6ad04522013-04-15 18:53:24 -0700100static void android_os_Trace_nativeSetTracingEnabled(JNIEnv* env,
101 jclass clazz, jboolean enabled) {
Igor Murashkinc7282eb2013-10-17 18:19:48 -0700102 ALOGV("%s: %d", __FUNCTION__, enabled);
103
Jamie Gennis6ad04522013-04-15 18:53:24 -0700104 atrace_set_tracing_enabled(enabled);
105}
106
Daniel Micay76f6a862015-09-19 17:31:01 -0400107static const JNINativeMethod gTraceMethods[] = {
Jeff Brown481c1572012-03-09 14:41:15 -0800108 /* name, signature, funcPtr */
109 { "nativeGetEnabledTags",
110 "()J",
111 (void*)android_os_Trace_nativeGetEnabledTags },
Jamie Gennisf9c7d6b2013-03-25 14:18:25 -0700112 { "nativeSetAppTracingAllowed",
113 "(Z)V",
114 (void*)android_os_Trace_nativeSetAppTracingAllowed },
Jamie Gennis6ad04522013-04-15 18:53:24 -0700115 { "nativeSetTracingEnabled",
116 "(Z)V",
117 (void*)android_os_Trace_nativeSetTracingEnabled },
John Reck6be2cab2016-10-03 14:38:06 -0700118
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 Brown481c1572012-03-09 14:41:15 -0800136};
137
138int register_android_os_Trace(JNIEnv* env) {
139 int res = jniRegisterNativeMethods(env, "android/os/Trace",
140 gTraceMethods, NELEM(gTraceMethods));
Andreas Gampe987f79f2014-11-18 17:29:46 -0800141 LOG_ALWAYS_FATAL_IF(res < 0, "Unable to register native methods.");
Jeff Brown481c1572012-03-09 14:41:15 -0800142
143 return 0;
144}
145
146} // namespace android