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