Earl Ou | e403038 | 2016-11-22 17:04:44 +0800 | [diff] [blame] | 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> |
Earl Ou | e403038 | 2016-11-22 17:04:44 +0800 | [diff] [blame] | 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> |
Tom Cherry | 350164c | 2019-10-23 16:25:55 +0000 | [diff] [blame] | 35 | #include <log/log_properties.h> |
Earl Ou | e403038 | 2016-11-22 17:04:44 +0800 | [diff] [blame] | 36 | |
Florian Mayer | b06766c | 2019-12-10 12:20:03 +0000 | [diff] [blame^] | 37 | #if defined(__BIONIC__) |
| 38 | #define _REALLY_INCLUDE_SYS__SYSTEM_PROPERTIES_H_ |
| 39 | #include <sys/_system_properties.h> |
| 40 | #endif |
| 41 | |
Earl Ou | e403038 | 2016-11-22 17:04:44 +0800 | [diff] [blame] | 42 | /** |
| 43 | * Maximum size of a message that can be logged to the trace buffer. |
| 44 | * Note this message includes a tag, the pid, and the string given as the name. |
| 45 | * Names should be kept short to get the most use of the trace buffer. |
| 46 | */ |
| 47 | #define ATRACE_MESSAGE_LENGTH 1024 |
| 48 | |
Florian Mayer | b06766c | 2019-12-10 12:20:03 +0000 | [diff] [blame^] | 49 | constexpr uint32_t kSeqNoNotInit = static_cast<uint32_t>(-1); |
| 50 | |
| 51 | atomic_bool atrace_is_ready = ATOMIC_VAR_INIT(false); |
| 52 | int atrace_marker_fd = -1; |
| 53 | uint64_t atrace_enabled_tags = ATRACE_TAG_NOT_READY; |
| 54 | static bool atrace_is_debuggable = false; |
| 55 | static atomic_bool atrace_is_enabled = ATOMIC_VAR_INIT(true); |
| 56 | static pthread_mutex_t atrace_tags_mutex = PTHREAD_MUTEX_INITIALIZER; |
| 57 | |
| 58 | /** |
| 59 | * Sequence number of debug.atrace.tags.enableflags the last time the enabled |
| 60 | * tags were reloaded. |
| 61 | **/ |
| 62 | static _Atomic(uint32_t) last_sequence_number = ATOMIC_VAR_INIT(kSeqNoNotInit); |
| 63 | |
| 64 | #if defined(__BIONIC__) |
| 65 | // All zero prop_info that has a sequence number of 0. This is easier than |
| 66 | // depending on implementation details of the property implementation. |
| 67 | // |
| 68 | // prop_info is static_assert-ed to be 96 bytes, which cannot change due to |
| 69 | // ABI compatibility. |
| 70 | alignas(uint64_t) static char empty_pi[96]; |
| 71 | static const prop_info* atrace_property_info = reinterpret_cast<const prop_info*>(empty_pi); |
| 72 | #endif |
| 73 | |
| 74 | #if ATRACE_SHMEM |
| 75 | |
| 76 | /** |
| 77 | * This is called when the sequence number of debug.atrace.tags.enableflags |
| 78 | * changes and we need to reload the enabled tags. |
| 79 | **/ |
| 80 | static void atrace_seq_number_changed(uint32_t prev_seq_no, uint32_t seq_no); |
| 81 | |
| 82 | void atrace_init() { |
| 83 | #if defined(__BIONIC__) |
| 84 | uint32_t seq_no = __system_property_serial(atrace_property_info); // Acquire semantics. |
| 85 | #else |
| 86 | uint32_t seq_no = 0; |
| 87 | #endif |
| 88 | uint32_t prev_seq_no = atomic_load_explicit(&last_sequence_number, memory_order_relaxed); |
| 89 | if (CC_UNLIKELY(seq_no != prev_seq_no)) { |
| 90 | atrace_seq_number_changed(prev_seq_no, seq_no); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | uint64_t atrace_get_enabled_tags() |
| 95 | { |
| 96 | atrace_init(); |
| 97 | return atrace_enabled_tags; |
| 98 | } |
| 99 | #endif |
Earl Ou | e403038 | 2016-11-22 17:04:44 +0800 | [diff] [blame] | 100 | |
| 101 | // Set whether this process is debuggable, which determines whether |
| 102 | // application-level tracing is allowed when the ro.debuggable system property |
| 103 | // is not set to '1'. |
| 104 | void atrace_set_debuggable(bool debuggable) |
| 105 | { |
| 106 | atrace_is_debuggable = debuggable; |
| 107 | atrace_update_tags(); |
| 108 | } |
| 109 | |
| 110 | // Check whether the given command line matches one of the comma-separated |
| 111 | // values listed in the app_cmdlines property. |
| 112 | static bool atrace_is_cmdline_match(const char* cmdline) |
| 113 | { |
| 114 | int count = property_get_int32("debug.atrace.app_number", 0); |
| 115 | |
| 116 | char buf[PROPERTY_KEY_MAX]; |
| 117 | char value[PROPERTY_VALUE_MAX]; |
| 118 | |
| 119 | for (int i = 0; i < count; i++) { |
| 120 | snprintf(buf, sizeof(buf), "debug.atrace.app_%d", i); |
| 121 | property_get(buf, value, ""); |
Daniel Colascione | 642ef98 | 2018-02-09 20:07:23 -0800 | [diff] [blame] | 122 | if (strcmp(value, "*") == 0 || strcmp(value, cmdline) == 0) { |
Earl Ou | e403038 | 2016-11-22 17:04:44 +0800 | [diff] [blame] | 123 | return true; |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | return false; |
| 128 | } |
| 129 | |
| 130 | // Determine whether application-level tracing is enabled for this process. |
| 131 | static bool atrace_is_app_tracing_enabled() |
| 132 | { |
Tom Cherry | 1580094 | 2019-02-20 12:51:33 -0800 | [diff] [blame] | 133 | bool sys_debuggable = property_get_bool("ro.debuggable", 0); |
Earl Ou | e403038 | 2016-11-22 17:04:44 +0800 | [diff] [blame] | 134 | bool result = false; |
| 135 | |
| 136 | if (sys_debuggable || atrace_is_debuggable) { |
| 137 | // Check whether tracing is enabled for this process. |
| 138 | FILE * file = fopen("/proc/self/cmdline", "re"); |
| 139 | if (file) { |
| 140 | char cmdline[4096]; |
| 141 | if (fgets(cmdline, sizeof(cmdline), file)) { |
| 142 | result = atrace_is_cmdline_match(cmdline); |
| 143 | } else { |
| 144 | ALOGE("Error reading cmdline: %s (%d)", strerror(errno), errno); |
| 145 | } |
| 146 | fclose(file); |
| 147 | } else { |
| 148 | ALOGE("Error opening /proc/self/cmdline: %s (%d)", strerror(errno), |
| 149 | errno); |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | return result; |
| 154 | } |
| 155 | |
| 156 | // Read the sysprop and return the value tags should be set to |
| 157 | static uint64_t atrace_get_property() |
| 158 | { |
| 159 | char value[PROPERTY_VALUE_MAX]; |
| 160 | char *endptr; |
| 161 | uint64_t tags; |
| 162 | |
| 163 | property_get("debug.atrace.tags.enableflags", value, "0"); |
| 164 | errno = 0; |
| 165 | tags = strtoull(value, &endptr, 0); |
| 166 | if (value[0] == '\0' || *endptr != '\0') { |
| 167 | ALOGE("Error parsing trace property: Not a number: %s", value); |
| 168 | return 0; |
| 169 | } else if (errno == ERANGE || tags == ULLONG_MAX) { |
| 170 | ALOGE("Error parsing trace property: Number too large: %s", value); |
| 171 | return 0; |
| 172 | } |
| 173 | |
| 174 | // Only set the "app" tag if this process was selected for app-level debug |
| 175 | // tracing. |
| 176 | if (atrace_is_app_tracing_enabled()) { |
| 177 | tags |= ATRACE_TAG_APP; |
| 178 | } else { |
| 179 | tags &= ~ATRACE_TAG_APP; |
| 180 | } |
| 181 | |
| 182 | return (tags | ATRACE_TAG_ALWAYS) & ATRACE_TAG_VALID_MASK; |
| 183 | } |
| 184 | |
| 185 | // Update tags if tracing is ready. Useful as a sysprop change callback. |
| 186 | void atrace_update_tags() |
| 187 | { |
| 188 | uint64_t tags; |
Florian Mayer | b06766c | 2019-12-10 12:20:03 +0000 | [diff] [blame^] | 189 | if (ATRACE_SHMEM || CC_UNLIKELY(atomic_load_explicit(&atrace_is_ready, memory_order_acquire))) { |
Earl Ou | e403038 | 2016-11-22 17:04:44 +0800 | [diff] [blame] | 190 | if (atomic_load_explicit(&atrace_is_enabled, memory_order_acquire)) { |
| 191 | tags = atrace_get_property(); |
| 192 | pthread_mutex_lock(&atrace_tags_mutex); |
| 193 | atrace_enabled_tags = tags; |
| 194 | pthread_mutex_unlock(&atrace_tags_mutex); |
| 195 | } else { |
| 196 | // Tracing is disabled for this process, so we simply don't |
| 197 | // initialize the tags. |
| 198 | pthread_mutex_lock(&atrace_tags_mutex); |
| 199 | atrace_enabled_tags = ATRACE_TAG_NOT_READY; |
| 200 | pthread_mutex_unlock(&atrace_tags_mutex); |
| 201 | } |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | #define WRITE_MSG(format_begin, format_end, name, value) { \ |
| 206 | char buf[ATRACE_MESSAGE_LENGTH]; \ |
| 207 | int pid = getpid(); \ |
| 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 | } |
| 221 | |
| 222 | #endif // __TRACE_DEV_INC |