1/* 2 * Copyright (C) 2017 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#ifndef __TRACE_DEV_INC 18#define __TRACE_DEV_INC 19 20#define LOG_TAG "cutils-trace" 21 22#include <errno.h> 23#include <fcntl.h> 24#include <limits.h> 25#include <pthread.h> 26#include <stdatomic.h> 27#include <stdlib.h> 28#include <string.h> 29#include <sys/types.h> 30 31#include <cutils/compiler.h> 32#include <cutils/properties.h> 33#include <cutils/trace.h> 34#include <log/log.h> 35#include <log/log_properties.h> 36 37/** 38 * Maximum size of a message that can be logged to the trace buffer. 39 * Note this message includes a tag, the pid, and the string given as the name. 40 * Names should be kept short to get the most use of the trace buffer. 41 */ 42#define ATRACE_MESSAGE_LENGTH 1024 43 44atomic_bool atrace_is_ready = ATOMIC_VAR_INIT(false); 45int atrace_marker_fd = -1; 46uint64_t atrace_enabled_tags = ATRACE_TAG_NOT_READY; 47static bool atrace_is_debuggable = false; 48static atomic_bool atrace_is_enabled = ATOMIC_VAR_INIT(true); 49static pthread_mutex_t atrace_tags_mutex = PTHREAD_MUTEX_INITIALIZER; 50 51// Set whether this process is debuggable, which determines whether 52// application-level tracing is allowed when the ro.debuggable system property 53// is not set to '1'. 54void atrace_set_debuggable(bool debuggable) 55{ 56 atrace_is_debuggable = debuggable; 57 atrace_update_tags(); 58} 59 60// Check whether the given command line matches one of the comma-separated 61// values listed in the app_cmdlines property. 62static bool atrace_is_cmdline_match(const char* cmdline) 63{ 64 int count = property_get_int32("debug.atrace.app_number", 0); 65 66 char buf[PROPERTY_KEY_MAX]; 67 char value[PROPERTY_VALUE_MAX]; 68 69 for (int i = 0; i < count; i++) { 70 snprintf(buf, sizeof(buf), "debug.atrace.app_%d", i); 71 property_get(buf, value, ""); 72 if (strcmp(value, "*") == 0 || strcmp(value, cmdline) == 0) { 73 return true; 74 } 75 } 76 77 return false; 78} 79 80// Determine whether application-level tracing is enabled for this process. 81static bool atrace_is_app_tracing_enabled() 82{ 83 bool sys_debuggable = property_get_bool("ro.debuggable", 0); 84 bool result = false; 85 86 if (sys_debuggable || atrace_is_debuggable) { 87 // Check whether tracing is enabled for this process. 88 FILE * file = fopen("/proc/self/cmdline", "re"); 89 if (file) { 90 char cmdline[4096]; 91 if (fgets(cmdline, sizeof(cmdline), file)) { 92 result = atrace_is_cmdline_match(cmdline); 93 } else { 94 ALOGE("Error reading cmdline: %s (%d)", strerror(errno), errno); 95 } 96 fclose(file); 97 } else { 98 ALOGE("Error opening /proc/self/cmdline: %s (%d)", strerror(errno), 99 errno); 100 } 101 } 102 103 return result; 104} 105 106// Read the sysprop and return the value tags should be set to 107static uint64_t atrace_get_property() 108{ 109 char value[PROPERTY_VALUE_MAX]; 110 char *endptr; 111 uint64_t tags; 112 113 property_get("debug.atrace.tags.enableflags", value, "0"); 114 errno = 0; 115 tags = strtoull(value, &endptr, 0); 116 if (value[0] == '\0' || *endptr != '\0') { 117 ALOGE("Error parsing trace property: Not a number: %s", value); 118 return 0; 119 } else if (errno == ERANGE || tags == ULLONG_MAX) { 120 ALOGE("Error parsing trace property: Number too large: %s", value); 121 return 0; 122 } 123 124 // Only set the "app" tag if this process was selected for app-level debug 125 // tracing. 126 if (atrace_is_app_tracing_enabled()) { 127 tags |= ATRACE_TAG_APP; 128 } else { 129 tags &= ~ATRACE_TAG_APP; 130 } 131 132 return (tags | ATRACE_TAG_ALWAYS) & ATRACE_TAG_VALID_MASK; 133} 134 135// Update tags if tracing is ready. Useful as a sysprop change callback. 136void atrace_update_tags() 137{ 138 uint64_t tags; 139 if (CC_UNLIKELY(atomic_load_explicit(&atrace_is_ready, memory_order_acquire))) { 140 if (atomic_load_explicit(&atrace_is_enabled, memory_order_acquire)) { 141 tags = atrace_get_property(); 142 pthread_mutex_lock(&atrace_tags_mutex); 143 atrace_enabled_tags = tags; 144 pthread_mutex_unlock(&atrace_tags_mutex); 145 } else { 146 // Tracing is disabled for this process, so we simply don't 147 // initialize the tags. 148 pthread_mutex_lock(&atrace_tags_mutex); 149 atrace_enabled_tags = ATRACE_TAG_NOT_READY; 150 pthread_mutex_unlock(&atrace_tags_mutex); 151 } 152 } 153} 154 155#define WRITE_MSG(format_begin, format_end, name, value) { \ 156 char buf[ATRACE_MESSAGE_LENGTH]; \ 157 int pid = getpid(); \ 158 int len = snprintf(buf, sizeof(buf), format_begin "%s" format_end, pid, \ 159 name, value); \ 160 if (len >= (int) sizeof(buf)) { \ 161 /* Given the sizeof(buf), and all of the current format buffers, \ 162 * it is impossible for name_len to be < 0 if len >= sizeof(buf). */ \ 163 int name_len = strlen(name) - (len - sizeof(buf)) - 1; \ 164 /* Truncate the name to make the message fit. */ \ 165 ALOGW("Truncated name in %s: %s\n", __FUNCTION__, name); \ 166 len = snprintf(buf, sizeof(buf), format_begin "%.*s" format_end, pid, \ 167 name_len, name, value); \ 168 } \ 169 write(atrace_marker_fd, buf, len); \ 170} 171 172#endif // __TRACE_DEV_INC 173