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