android_os_Trace.cpp revision 987f79f60bb1f0a4bcd3ef22e57301c743f0b94f
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 <JNIHelp.h>
21#include <ScopedUtfChars.h>
22#include <ScopedStringChars.h>
23
24#include <utils/String8.h>
25
26#include <cutils/trace.h>
27#include <cutils/log.h>
28
29namespace android {
30
31static void sanitizeString(String8& utf8Chars) {
32    size_t size = utf8Chars.size();
33    char* str = utf8Chars.lockBuffer(size);
34    for (size_t i = 0; i < size; i++) {
35        char c = str[i];
36        if (c == '\0' || c == '\n' || c == '|') {
37            str[i] = ' ';
38        }
39    }
40    utf8Chars.unlockBuffer();
41}
42
43static jlong android_os_Trace_nativeGetEnabledTags(JNIEnv* env, jclass clazz) {
44    return atrace_get_enabled_tags();
45}
46
47static void android_os_Trace_nativeTraceCounter(JNIEnv* env, jclass clazz,
48        jlong tag, jstring nameStr, jint value) {
49    ScopedUtfChars name(env, nameStr);
50
51    ALOGV("%s: %lld %s %d", __FUNCTION__, tag, name.c_str(), value);
52    atrace_int(tag, name.c_str(), value);
53}
54
55static void android_os_Trace_nativeTraceBegin(JNIEnv* env, jclass clazz,
56        jlong tag, jstring nameStr) {
57    ScopedStringChars jchars(env, nameStr);
58    String8 utf8Chars(reinterpret_cast<const char16_t*>(jchars.get()), jchars.size());
59    sanitizeString(utf8Chars);
60
61    ALOGV("%s: %lld %s", __FUNCTION__, tag, utf8Chars.string());
62    atrace_begin(tag, utf8Chars.string());
63}
64
65static void android_os_Trace_nativeTraceEnd(JNIEnv* env, jclass clazz,
66        jlong tag) {
67
68    ALOGV("%s: %lld", __FUNCTION__, tag);
69    atrace_end(tag);
70}
71
72static void android_os_Trace_nativeAsyncTraceBegin(JNIEnv* env, jclass clazz,
73        jlong tag, jstring nameStr, jint cookie) {
74    ScopedStringChars jchars(env, nameStr);
75    String8 utf8Chars(reinterpret_cast<const char16_t*>(jchars.get()), jchars.size());
76    sanitizeString(utf8Chars);
77
78    ALOGV("%s: %lld %s %d", __FUNCTION__, tag, utf8Chars.string(), cookie);
79    atrace_async_begin(tag, utf8Chars.string(), cookie);
80}
81
82static void android_os_Trace_nativeAsyncTraceEnd(JNIEnv* env, jclass clazz,
83        jlong tag, jstring nameStr, jint cookie) {
84    ScopedStringChars jchars(env, nameStr);
85    String8 utf8Chars(reinterpret_cast<const char16_t*>(jchars.get()), jchars.size());
86    sanitizeString(utf8Chars);
87
88    ALOGV("%s: %lld %s %d", __FUNCTION__, tag, utf8Chars.string(), cookie);
89    atrace_async_end(tag, utf8Chars.string(), cookie);
90}
91
92static void android_os_Trace_nativeSetAppTracingAllowed(JNIEnv* env,
93        jclass clazz, jboolean allowed) {
94    ALOGV("%s: %d", __FUNCTION__, allowed);
95
96    atrace_set_debuggable(allowed);
97}
98
99static void android_os_Trace_nativeSetTracingEnabled(JNIEnv* env,
100        jclass clazz, jboolean enabled) {
101    ALOGV("%s: %d", __FUNCTION__, enabled);
102
103    atrace_set_tracing_enabled(enabled);
104}
105
106static JNINativeMethod gTraceMethods[] = {
107    /* name, signature, funcPtr */
108    { "nativeGetEnabledTags",
109            "()J",
110            (void*)android_os_Trace_nativeGetEnabledTags },
111    { "nativeTraceCounter",
112            "(JLjava/lang/String;I)V",
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 },
126    { "nativeSetAppTracingAllowed",
127            "(Z)V",
128            (void*)android_os_Trace_nativeSetAppTracingAllowed },
129    { "nativeSetTracingEnabled",
130            "(Z)V",
131            (void*)android_os_Trace_nativeSetTracingEnabled },
132};
133
134int register_android_os_Trace(JNIEnv* env) {
135    int res = jniRegisterNativeMethods(env, "android/os/Trace",
136            gTraceMethods, NELEM(gTraceMethods));
137    LOG_ALWAYS_FATAL_IF(res < 0, "Unable to register native methods.");
138
139    return 0;
140}
141
142} // namespace android
143