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"
18// #define LOG_NDEBUG 0
19
20#include <inttypes.h>
21
22#include <cutils/trace.h>
23#include <utils/String8.h>
24#include <log/log.h>
25
26#include <JNIHelp.h>
27#include <ScopedUtfChars.h>
28#include <ScopedStringChars.h>
29
30namespace android {
31
32static 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
44static jlong android_os_Trace_nativeGetEnabledTags(JNIEnv* env, jclass clazz) {
45    return atrace_get_enabled_tags();
46}
47
48static void android_os_Trace_nativeTraceCounter(JNIEnv* env, jclass clazz,
49        jlong tag, jstring nameStr, jint value) {
50    ScopedUtfChars name(env, nameStr);
51
52    ALOGV("%s: %" PRId64 " %s %d", __FUNCTION__, tag, name.c_str(), value);
53    atrace_int(tag, name.c_str(), value);
54}
55
56static void android_os_Trace_nativeTraceBegin(JNIEnv* env, jclass clazz,
57        jlong tag, jstring nameStr) {
58    ScopedStringChars jchars(env, nameStr);
59    String8 utf8Chars(reinterpret_cast<const char16_t*>(jchars.get()), jchars.size());
60    sanitizeString(utf8Chars);
61
62    ALOGV("%s: %" PRId64 " %s", __FUNCTION__, tag, utf8Chars.string());
63    atrace_begin(tag, utf8Chars.string());
64}
65
66static void android_os_Trace_nativeTraceEnd(JNIEnv* env, jclass clazz,
67        jlong tag) {
68
69    ALOGV("%s: %" PRId64, __FUNCTION__, tag);
70    atrace_end(tag);
71}
72
73static void android_os_Trace_nativeAsyncTraceBegin(JNIEnv* env, jclass clazz,
74        jlong tag, jstring nameStr, jint cookie) {
75    ScopedStringChars jchars(env, nameStr);
76    String8 utf8Chars(reinterpret_cast<const char16_t*>(jchars.get()), jchars.size());
77    sanitizeString(utf8Chars);
78
79    ALOGV("%s: %" PRId64 " %s %d", __FUNCTION__, tag, utf8Chars.string(), cookie);
80    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) {
85    ScopedStringChars jchars(env, nameStr);
86    String8 utf8Chars(reinterpret_cast<const char16_t*>(jchars.get()), jchars.size());
87    sanitizeString(utf8Chars);
88
89    ALOGV("%s: %" PRId64 " %s %d", __FUNCTION__, tag, utf8Chars.string(), cookie);
90    atrace_async_end(tag, utf8Chars.string(), cookie);
91}
92
93static void android_os_Trace_nativeSetAppTracingAllowed(JNIEnv* env,
94        jclass clazz, jboolean allowed) {
95    ALOGV("%s: %d", __FUNCTION__, allowed);
96
97    atrace_set_debuggable(allowed);
98}
99
100static void android_os_Trace_nativeSetTracingEnabled(JNIEnv* env,
101        jclass clazz, jboolean enabled) {
102    ALOGV("%s: %d", __FUNCTION__, enabled);
103
104    atrace_set_tracing_enabled(enabled);
105}
106
107static const JNINativeMethod gTraceMethods[] = {
108    /* name, signature, funcPtr */
109    { "nativeGetEnabledTags",
110            "()J",
111            (void*)android_os_Trace_nativeGetEnabledTags },
112    { "nativeSetAppTracingAllowed",
113            "(Z)V",
114            (void*)android_os_Trace_nativeSetAppTracingAllowed },
115    { "nativeSetTracingEnabled",
116            "(Z)V",
117            (void*)android_os_Trace_nativeSetTracingEnabled },
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 },
136};
137
138int register_android_os_Trace(JNIEnv* env) {
139    int res = jniRegisterNativeMethods(env, "android/os/Trace",
140            gTraceMethods, NELEM(gTraceMethods));
141    LOG_ALWAYS_FATAL_IF(res < 0, "Unable to register native methods.");
142
143    return 0;
144}
145
146} // namespace android
147