Home | History | Annotate | Download | only in jni
      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 
     29 namespace android {
     30 
     31 static 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 
     43 static jlong android_os_Trace_nativeGetEnabledTags(JNIEnv* env, jclass clazz) {
     44     return atrace_get_enabled_tags();
     45 }
     46 
     47 static 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 
     55 static void android_os_Trace_nativeTraceBegin(JNIEnv* env, jclass clazz,
     56         jlong tag, jstring nameStr) {
     57     const size_t MAX_SECTION_NAME_LEN = 127;
     58     ScopedStringChars jchars(env, nameStr);
     59     String8 utf8Chars(reinterpret_cast<const char16_t*>(jchars.get()), jchars.size());
     60     sanitizeString(utf8Chars);
     61 
     62     ALOGV("%s: %lld %s", __FUNCTION__, tag, utf8Chars.string());
     63     atrace_begin(tag, utf8Chars.string());
     64 }
     65 
     66 static void android_os_Trace_nativeTraceEnd(JNIEnv* env, jclass clazz,
     67         jlong tag) {
     68 
     69     ALOGV("%s: %lld", __FUNCTION__, tag);
     70     atrace_end(tag);
     71 }
     72 
     73 static void android_os_Trace_nativeAsyncTraceBegin(JNIEnv* env, jclass clazz,
     74         jlong tag, jstring nameStr, jint cookie) {
     75     const size_t MAX_SECTION_NAME_LEN = 127;
     76     ScopedStringChars jchars(env, nameStr);
     77     String8 utf8Chars(reinterpret_cast<const char16_t*>(jchars.get()), jchars.size());
     78     sanitizeString(utf8Chars);
     79 
     80     ALOGV("%s: %lld %s %d", __FUNCTION__, tag, utf8Chars.string(), cookie);
     81     atrace_async_begin(tag, utf8Chars.string(), cookie);
     82 }
     83 
     84 static void android_os_Trace_nativeAsyncTraceEnd(JNIEnv* env, jclass clazz,
     85         jlong tag, jstring nameStr, jint cookie) {
     86     const size_t MAX_SECTION_NAME_LEN = 127;
     87     ScopedStringChars jchars(env, nameStr);
     88     String8 utf8Chars(reinterpret_cast<const char16_t*>(jchars.get()), jchars.size());
     89     sanitizeString(utf8Chars);
     90 
     91     ALOGV("%s: %lld %s %d", __FUNCTION__, tag, utf8Chars.string(), cookie);
     92     atrace_async_end(tag, utf8Chars.string(), cookie);
     93 }
     94 
     95 static void android_os_Trace_nativeSetAppTracingAllowed(JNIEnv* env,
     96         jclass clazz, jboolean allowed) {
     97     ALOGV("%s: %d", __FUNCTION__, allowed);
     98 
     99     atrace_set_debuggable(allowed);
    100 }
    101 
    102 static void android_os_Trace_nativeSetTracingEnabled(JNIEnv* env,
    103         jclass clazz, jboolean enabled) {
    104     ALOGV("%s: %d", __FUNCTION__, enabled);
    105 
    106     atrace_set_tracing_enabled(enabled);
    107 }
    108 
    109 static JNINativeMethod gTraceMethods[] = {
    110     /* name, signature, funcPtr */
    111     { "nativeGetEnabledTags",
    112             "()J",
    113             (void*)android_os_Trace_nativeGetEnabledTags },
    114     { "nativeTraceCounter",
    115             "(JLjava/lang/String;I)V",
    116             (void*)android_os_Trace_nativeTraceCounter },
    117     { "nativeTraceBegin",
    118             "(JLjava/lang/String;)V",
    119             (void*)android_os_Trace_nativeTraceBegin },
    120     { "nativeTraceEnd",
    121             "(J)V",
    122             (void*)android_os_Trace_nativeTraceEnd },
    123     { "nativeAsyncTraceBegin",
    124             "(JLjava/lang/String;I)V",
    125             (void*)android_os_Trace_nativeAsyncTraceBegin },
    126     { "nativeAsyncTraceEnd",
    127             "(JLjava/lang/String;I)V",
    128             (void*)android_os_Trace_nativeAsyncTraceEnd },
    129     { "nativeSetAppTracingAllowed",
    130             "(Z)V",
    131             (void*)android_os_Trace_nativeSetAppTracingAllowed },
    132     { "nativeSetTracingEnabled",
    133             "(Z)V",
    134             (void*)android_os_Trace_nativeSetTracingEnabled },
    135 };
    136 
    137 int register_android_os_Trace(JNIEnv* env) {
    138     int res = jniRegisterNativeMethods(env, "android/os/Trace",
    139             gTraceMethods, NELEM(gTraceMethods));
    140     LOG_FATAL_IF(res < 0, "Unable to register native methods.");
    141 
    142     return 0;
    143 }
    144 
    145 } // namespace android
    146