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