| Alex Ray | 0a34643 | 2012-11-14 17:25:28 -0800 | [diff] [blame] | 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 |  | 
| Mark Salyzyn | 23ed4c2 | 2016-09-28 13:33:27 -0700 | [diff] [blame] | 17 | #define LOG_TAG "cutils-trace" | 
|  | 18 |  | 
| Alex Ray | 0a34643 | 2012-11-14 17:25:28 -0800 | [diff] [blame] | 19 | #include <errno.h> | 
|  | 20 | #include <fcntl.h> | 
|  | 21 | #include <limits.h> | 
|  | 22 | #include <pthread.h> | 
| Yabin Cui | a8ac32c | 2015-04-15 14:50:27 -0700 | [diff] [blame] | 23 | #include <stdatomic.h> | 
| Jamie Gennis | 774f929 | 2013-02-25 18:15:40 -0800 | [diff] [blame] | 24 | #include <stdbool.h> | 
| Alex Ray | 0a34643 | 2012-11-14 17:25:28 -0800 | [diff] [blame] | 25 | #include <stdlib.h> | 
|  | 26 | #include <string.h> | 
|  | 27 | #include <sys/types.h> | 
| Mark Salyzyn | ff2dcd9 | 2016-09-28 15:54:45 -0700 | [diff] [blame] | 28 |  | 
| Alex Ray | 0a34643 | 2012-11-14 17:25:28 -0800 | [diff] [blame] | 29 | #include <cutils/compiler.h> | 
|  | 30 | #include <cutils/properties.h> | 
|  | 31 | #include <cutils/trace.h> | 
| Mark Salyzyn | b5aa4e7 | 2016-03-28 15:39:26 -0700 | [diff] [blame] | 32 | #include <private/android_logger.h> | 
| Alex Ray | 0a34643 | 2012-11-14 17:25:28 -0800 | [diff] [blame] | 33 |  | 
| Chih-Hung Hsieh | 2d3150e | 2014-10-13 16:30:24 -0700 | [diff] [blame] | 34 | /** | 
|  | 35 | * Maximum size of a message that can be logged to the trace buffer. | 
|  | 36 | * Note this message includes a tag, the pid, and the string given as the name. | 
|  | 37 | * Names should be kept short to get the most use of the trace buffer. | 
|  | 38 | */ | 
|  | 39 | #define ATRACE_MESSAGE_LENGTH 1024 | 
|  | 40 |  | 
| Yabin Cui | a8ac32c | 2015-04-15 14:50:27 -0700 | [diff] [blame] | 41 | atomic_bool             atrace_is_ready      = ATOMIC_VAR_INIT(false); | 
| Jamie Gennis | b13ea45 | 2013-04-15 18:50:22 -0700 | [diff] [blame] | 42 | int                     atrace_marker_fd     = -1; | 
|  | 43 | uint64_t                atrace_enabled_tags  = ATRACE_TAG_NOT_READY; | 
|  | 44 | static bool             atrace_is_debuggable = false; | 
| Yabin Cui | a8ac32c | 2015-04-15 14:50:27 -0700 | [diff] [blame] | 45 | static atomic_bool      atrace_is_enabled    = ATOMIC_VAR_INIT(true); | 
| Jamie Gennis | b13ea45 | 2013-04-15 18:50:22 -0700 | [diff] [blame] | 46 | static pthread_once_t   atrace_once_control  = PTHREAD_ONCE_INIT; | 
|  | 47 | static pthread_mutex_t  atrace_tags_mutex    = PTHREAD_MUTEX_INITIALIZER; | 
| Jamie Gennis | 774f929 | 2013-02-25 18:15:40 -0800 | [diff] [blame] | 48 |  | 
|  | 49 | // Set whether this process is debuggable, which determines whether | 
|  | 50 | // application-level tracing is allowed when the ro.debuggable system property | 
|  | 51 | // is not set to '1'. | 
|  | 52 | void atrace_set_debuggable(bool debuggable) | 
|  | 53 | { | 
|  | 54 | atrace_is_debuggable = debuggable; | 
|  | 55 | atrace_update_tags(); | 
|  | 56 | } | 
|  | 57 |  | 
| Jamie Gennis | b13ea45 | 2013-04-15 18:50:22 -0700 | [diff] [blame] | 58 | // Set whether tracing is enabled in this process.  This is used to prevent | 
|  | 59 | // the Zygote process from tracing. | 
|  | 60 | void atrace_set_tracing_enabled(bool enabled) | 
|  | 61 | { | 
| Yabin Cui | a8ac32c | 2015-04-15 14:50:27 -0700 | [diff] [blame] | 62 | atomic_store_explicit(&atrace_is_enabled, enabled, memory_order_release); | 
| Jamie Gennis | b13ea45 | 2013-04-15 18:50:22 -0700 | [diff] [blame] | 63 | atrace_update_tags(); | 
|  | 64 | } | 
|  | 65 |  | 
| Jamie Gennis | 774f929 | 2013-02-25 18:15:40 -0800 | [diff] [blame] | 66 | // Check whether the given command line matches one of the comma-separated | 
|  | 67 | // values listed in the app_cmdlines property. | 
| Jamie Gennis | b13ea45 | 2013-04-15 18:50:22 -0700 | [diff] [blame] | 68 | static bool atrace_is_cmdline_match(const char* cmdline) | 
|  | 69 | { | 
| sergeyv | c19588c | 2016-04-28 19:33:09 -0700 | [diff] [blame] | 70 | int count = property_get_int32("debug.atrace.app_number", 0); | 
|  | 71 |  | 
|  | 72 | char buf[PROPERTY_KEY_MAX]; | 
| Jamie Gennis | 774f929 | 2013-02-25 18:15:40 -0800 | [diff] [blame] | 73 | char value[PROPERTY_VALUE_MAX]; | 
| Jamie Gennis | 774f929 | 2013-02-25 18:15:40 -0800 | [diff] [blame] | 74 |  | 
| sergeyv | c19588c | 2016-04-28 19:33:09 -0700 | [diff] [blame] | 75 | for (int i = 0; i < count; i++) { | 
|  | 76 | snprintf(buf, sizeof(buf), "debug.atrace.app_%d", i); | 
|  | 77 | property_get(buf, value, ""); | 
|  | 78 | if (strcmp(value, cmdline) == 0) { | 
| Jamie Gennis | 774f929 | 2013-02-25 18:15:40 -0800 | [diff] [blame] | 79 | return true; | 
|  | 80 | } | 
| Jamie Gennis | 774f929 | 2013-02-25 18:15:40 -0800 | [diff] [blame] | 81 | } | 
|  | 82 |  | 
|  | 83 | return false; | 
|  | 84 | } | 
|  | 85 |  | 
|  | 86 | // Determine whether application-level tracing is enabled for this process. | 
|  | 87 | static bool atrace_is_app_tracing_enabled() | 
|  | 88 | { | 
| Mark Salyzyn | b5aa4e7 | 2016-03-28 15:39:26 -0700 | [diff] [blame] | 89 | bool sys_debuggable = __android_log_is_debuggable(); | 
| Jamie Gennis | 774f929 | 2013-02-25 18:15:40 -0800 | [diff] [blame] | 90 | bool result = false; | 
|  | 91 |  | 
| Jamie Gennis | 774f929 | 2013-02-25 18:15:40 -0800 | [diff] [blame] | 92 | if (sys_debuggable || atrace_is_debuggable) { | 
|  | 93 | // Check whether tracing is enabled for this process. | 
| Nick Kralevich | dee1ef4 | 2015-12-16 12:32:26 -0800 | [diff] [blame] | 94 | FILE * file = fopen("/proc/self/cmdline", "re"); | 
| Jamie Gennis | 774f929 | 2013-02-25 18:15:40 -0800 | [diff] [blame] | 95 | if (file) { | 
|  | 96 | char cmdline[4096]; | 
|  | 97 | if (fgets(cmdline, sizeof(cmdline), file)) { | 
|  | 98 | result = atrace_is_cmdline_match(cmdline); | 
|  | 99 | } else { | 
|  | 100 | ALOGE("Error reading cmdline: %s (%d)", strerror(errno), errno); | 
|  | 101 | } | 
|  | 102 | fclose(file); | 
|  | 103 | } else { | 
|  | 104 | ALOGE("Error opening /proc/self/cmdline: %s (%d)", strerror(errno), | 
|  | 105 | errno); | 
|  | 106 | } | 
|  | 107 | } | 
|  | 108 |  | 
|  | 109 | return result; | 
|  | 110 | } | 
| Alex Ray | 0a34643 | 2012-11-14 17:25:28 -0800 | [diff] [blame] | 111 |  | 
|  | 112 | // Read the sysprop and return the value tags should be set to | 
|  | 113 | static uint64_t atrace_get_property() | 
|  | 114 | { | 
|  | 115 | char value[PROPERTY_VALUE_MAX]; | 
|  | 116 | char *endptr; | 
|  | 117 | uint64_t tags; | 
|  | 118 |  | 
|  | 119 | property_get("debug.atrace.tags.enableflags", value, "0"); | 
|  | 120 | errno = 0; | 
|  | 121 | tags = strtoull(value, &endptr, 0); | 
|  | 122 | if (value[0] == '\0' || *endptr != '\0') { | 
|  | 123 | ALOGE("Error parsing trace property: Not a number: %s", value); | 
|  | 124 | return 0; | 
|  | 125 | } else if (errno == ERANGE || tags == ULLONG_MAX) { | 
|  | 126 | ALOGE("Error parsing trace property: Number too large: %s", value); | 
|  | 127 | return 0; | 
|  | 128 | } | 
| Jamie Gennis | 774f929 | 2013-02-25 18:15:40 -0800 | [diff] [blame] | 129 |  | 
|  | 130 | // Only set the "app" tag if this process was selected for app-level debug | 
|  | 131 | // tracing. | 
|  | 132 | if (atrace_is_app_tracing_enabled()) { | 
|  | 133 | tags |= ATRACE_TAG_APP; | 
|  | 134 | } else { | 
|  | 135 | tags &= ~ATRACE_TAG_APP; | 
|  | 136 | } | 
|  | 137 |  | 
| Alex Ray | 0a34643 | 2012-11-14 17:25:28 -0800 | [diff] [blame] | 138 | return (tags | ATRACE_TAG_ALWAYS) & ATRACE_TAG_VALID_MASK; | 
|  | 139 | } | 
|  | 140 |  | 
| Alex Ray | e7bb7bc | 2012-11-20 01:39:09 -0800 | [diff] [blame] | 141 | // Update tags if tracing is ready. Useful as a sysprop change callback. | 
|  | 142 | void atrace_update_tags() | 
|  | 143 | { | 
|  | 144 | uint64_t tags; | 
| Yabin Cui | a8ac32c | 2015-04-15 14:50:27 -0700 | [diff] [blame] | 145 | if (CC_UNLIKELY(atomic_load_explicit(&atrace_is_ready, memory_order_acquire))) { | 
|  | 146 | if (atomic_load_explicit(&atrace_is_enabled, memory_order_acquire)) { | 
| Jamie Gennis | b13ea45 | 2013-04-15 18:50:22 -0700 | [diff] [blame] | 147 | tags = atrace_get_property(); | 
|  | 148 | pthread_mutex_lock(&atrace_tags_mutex); | 
|  | 149 | atrace_enabled_tags = tags; | 
|  | 150 | pthread_mutex_unlock(&atrace_tags_mutex); | 
|  | 151 | } else { | 
|  | 152 | // Tracing is disabled for this process, so we simply don't | 
|  | 153 | // initialize the tags. | 
|  | 154 | pthread_mutex_lock(&atrace_tags_mutex); | 
|  | 155 | atrace_enabled_tags = ATRACE_TAG_NOT_READY; | 
|  | 156 | pthread_mutex_unlock(&atrace_tags_mutex); | 
|  | 157 | } | 
| Alex Ray | e7bb7bc | 2012-11-20 01:39:09 -0800 | [diff] [blame] | 158 | } | 
|  | 159 | } | 
|  | 160 |  | 
| Alex Ray | 0a34643 | 2012-11-14 17:25:28 -0800 | [diff] [blame] | 161 | static void atrace_init_once() | 
|  | 162 | { | 
| Nick Kralevich | dee1ef4 | 2015-12-16 12:32:26 -0800 | [diff] [blame] | 163 | atrace_marker_fd = open("/sys/kernel/debug/tracing/trace_marker", O_WRONLY | O_CLOEXEC); | 
| Alex Ray | 0a34643 | 2012-11-14 17:25:28 -0800 | [diff] [blame] | 164 | if (atrace_marker_fd == -1) { | 
|  | 165 | ALOGE("Error opening trace file: %s (%d)", strerror(errno), errno); | 
|  | 166 | atrace_enabled_tags = 0; | 
|  | 167 | goto done; | 
|  | 168 | } | 
|  | 169 |  | 
|  | 170 | atrace_enabled_tags = atrace_get_property(); | 
|  | 171 |  | 
|  | 172 | done: | 
| Yabin Cui | a8ac32c | 2015-04-15 14:50:27 -0700 | [diff] [blame] | 173 | atomic_store_explicit(&atrace_is_ready, true, memory_order_release); | 
| Alex Ray | 0a34643 | 2012-11-14 17:25:28 -0800 | [diff] [blame] | 174 | } | 
|  | 175 |  | 
|  | 176 | void atrace_setup() | 
|  | 177 | { | 
|  | 178 | pthread_once(&atrace_once_control, atrace_init_once); | 
|  | 179 | } | 
| Chih-Hung Hsieh | 2d3150e | 2014-10-13 16:30:24 -0700 | [diff] [blame] | 180 |  | 
|  | 181 | void atrace_begin_body(const char* name) | 
|  | 182 | { | 
|  | 183 | char buf[ATRACE_MESSAGE_LENGTH]; | 
| Chih-Hung Hsieh | 2d3150e | 2014-10-13 16:30:24 -0700 | [diff] [blame] | 184 |  | 
| Christopher Ferris | 82d8489 | 2016-02-23 18:02:20 -0800 | [diff] [blame] | 185 | int len = snprintf(buf, sizeof(buf), "B|%d|%s", getpid(), name); | 
|  | 186 | if (len >= (int) sizeof(buf)) { | 
|  | 187 | ALOGW("Truncated name in %s: %s\n", __FUNCTION__, name); | 
|  | 188 | len = sizeof(buf) - 1; | 
|  | 189 | } | 
| Chih-Hung Hsieh | 2d3150e | 2014-10-13 16:30:24 -0700 | [diff] [blame] | 190 | write(atrace_marker_fd, buf, len); | 
|  | 191 | } | 
|  | 192 |  | 
| Colin Cross | 9993e79 | 2016-09-16 10:12:52 -0700 | [diff] [blame] | 193 | void atrace_end_body() | 
|  | 194 | { | 
|  | 195 | char c = 'E'; | 
|  | 196 | write(atrace_marker_fd, &c, 1); | 
|  | 197 | } | 
|  | 198 |  | 
| Christopher Ferris | 82d8489 | 2016-02-23 18:02:20 -0800 | [diff] [blame] | 199 | #define WRITE_MSG(format_begin, format_end, pid, name, value) { \ | 
|  | 200 | char buf[ATRACE_MESSAGE_LENGTH]; \ | 
|  | 201 | int len = snprintf(buf, sizeof(buf), format_begin "%s" format_end, pid, \ | 
|  | 202 | name, value); \ | 
|  | 203 | if (len >= (int) sizeof(buf)) { \ | 
|  | 204 | /* Given the sizeof(buf), and all of the current format buffers, \ | 
|  | 205 | * it is impossible for name_len to be < 0 if len >= sizeof(buf). */ \ | 
|  | 206 | int name_len = strlen(name) - (len - sizeof(buf)) - 1; \ | 
|  | 207 | /* Truncate the name to make the message fit. */ \ | 
|  | 208 | ALOGW("Truncated name in %s: %s\n", __FUNCTION__, name); \ | 
|  | 209 | len = snprintf(buf, sizeof(buf), format_begin "%.*s" format_end, pid, \ | 
|  | 210 | name_len, name, value); \ | 
|  | 211 | } \ | 
|  | 212 | write(atrace_marker_fd, buf, len); \ | 
|  | 213 | } | 
| Chih-Hung Hsieh | 2d3150e | 2014-10-13 16:30:24 -0700 | [diff] [blame] | 214 |  | 
|  | 215 | void atrace_async_begin_body(const char* name, int32_t cookie) | 
|  | 216 | { | 
| Christopher Ferris | 82d8489 | 2016-02-23 18:02:20 -0800 | [diff] [blame] | 217 | WRITE_MSG("S|%d|", "|%" PRId32, getpid(), name, cookie); | 
| Chih-Hung Hsieh | 2d3150e | 2014-10-13 16:30:24 -0700 | [diff] [blame] | 218 | } | 
|  | 219 |  | 
|  | 220 | void atrace_async_end_body(const char* name, int32_t cookie) | 
|  | 221 | { | 
| Christopher Ferris | 82d8489 | 2016-02-23 18:02:20 -0800 | [diff] [blame] | 222 | WRITE_MSG("F|%d|", "|%" PRId32, getpid(), name, cookie); | 
| Chih-Hung Hsieh | 2d3150e | 2014-10-13 16:30:24 -0700 | [diff] [blame] | 223 | } | 
|  | 224 |  | 
|  | 225 | void atrace_int_body(const char* name, int32_t value) | 
|  | 226 | { | 
| Christopher Ferris | 82d8489 | 2016-02-23 18:02:20 -0800 | [diff] [blame] | 227 | WRITE_MSG("C|%d|", "|%" PRId32, getpid(), name, value); | 
| Chih-Hung Hsieh | 2d3150e | 2014-10-13 16:30:24 -0700 | [diff] [blame] | 228 | } | 
|  | 229 |  | 
|  | 230 | void atrace_int64_body(const char* name, int64_t value) | 
|  | 231 | { | 
| Christopher Ferris | 82d8489 | 2016-02-23 18:02:20 -0800 | [diff] [blame] | 232 | WRITE_MSG("C|%d|", "|%" PRId64, getpid(), name, value); | 
| Chih-Hung Hsieh | 2d3150e | 2014-10-13 16:30:24 -0700 | [diff] [blame] | 233 | } |