android_os_Trace.cpp revision 9425f923fae8977d09d924436148d3808032ea98
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 JNINativeMethod gTraceMethods[] = {
90    /* name, signature, funcPtr */
91    { "nativeGetEnabledTags",
92            "()J",
93            (void*)android_os_Trace_nativeGetEnabledTags },
94    { "nativeTraceCounter",
95            "(JLjava/lang/String;I)V",
96            (void*)android_os_Trace_nativeTraceCounter },
97    { "nativeTraceBegin",
98            "(JLjava/lang/String;)V",
99            (void*)android_os_Trace_nativeTraceBegin },
100    { "nativeTraceEnd",
101            "(J)V",
102            (void*)android_os_Trace_nativeTraceEnd },
103    { "nativeAsyncTraceBegin",
104            "(JLjava/lang/String;I)V",
105            (void*)android_os_Trace_nativeAsyncTraceBegin },
106    { "nativeAsyncTraceEnd",
107            "(JLjava/lang/String;I)V",
108            (void*)android_os_Trace_nativeAsyncTraceEnd },
109    { "nativeSetAppTracingAllowed",
110            "(Z)V",
111            (void*)android_os_Trace_nativeSetAppTracingAllowed },
112};
113
114int register_android_os_Trace(JNIEnv* env) {
115    int res = jniRegisterNativeMethods(env, "android/os/Trace",
116            gTraceMethods, NELEM(gTraceMethods));
117    LOG_FATAL_IF(res < 0, "Unable to register native methods.");
118
119    return 0;
120}
121
122} // namespace android
123