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> |
Florian Mayer | f124707 | 2021-02-11 19:31:48 +0000 | [diff] [blame] | 24 | #include <fnmatch.h> |
Earl Ou | e403038 | 2016-11-22 17:04:44 +0800 | [diff] [blame] | 25 | #include <limits.h> |
| 26 | #include <pthread.h> |
| 27 | #include <stdatomic.h> |
Earl Ou | e403038 | 2016-11-22 17:04:44 +0800 | [diff] [blame] | 28 | #include <stdlib.h> |
| 29 | #include <string.h> |
| 30 | #include <sys/types.h> |
| 31 | |
| 32 | #include <cutils/compiler.h> |
| 33 | #include <cutils/properties.h> |
| 34 | #include <cutils/trace.h> |
| 35 | #include <log/log.h> |
Tom Cherry | 350164c | 2019-10-23 16:25:55 +0000 | [diff] [blame] | 36 | #include <log/log_properties.h> |
Earl Ou | e403038 | 2016-11-22 17:04:44 +0800 | [diff] [blame] | 37 | |
Florian Mayer | b06766c | 2019-12-10 12:20:03 +0000 | [diff] [blame] | 38 | #if defined(__BIONIC__) |
Elliott Hughes | 30203af | 2024-08-09 15:55:38 +0000 | [diff] [blame^] | 39 | #include <sys/system_properties.h> |
Florian Mayer | b06766c | 2019-12-10 12:20:03 +0000 | [diff] [blame] | 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 | |
Christopher Ferris | a3a6c8e | 2024-07-11 23:54:48 +0000 | [diff] [blame] | 51 | atomic_bool atrace_is_ready = false; |
Florian Mayer | b06766c | 2019-12-10 12:20:03 +0000 | [diff] [blame] | 52 | int atrace_marker_fd = -1; |
| 53 | uint64_t atrace_enabled_tags = ATRACE_TAG_NOT_READY; |
Christopher Ferris | a3a6c8e | 2024-07-11 23:54:48 +0000 | [diff] [blame] | 54 | static atomic_bool atrace_is_enabled = true; |
Florian Mayer | b06766c | 2019-12-10 12:20:03 +0000 | [diff] [blame] | 55 | static pthread_mutex_t atrace_tags_mutex = PTHREAD_MUTEX_INITIALIZER; |
| 56 | |
| 57 | /** |
| 58 | * Sequence number of debug.atrace.tags.enableflags the last time the enabled |
| 59 | * tags were reloaded. |
| 60 | **/ |
Christopher Ferris | a3a6c8e | 2024-07-11 23:54:48 +0000 | [diff] [blame] | 61 | static _Atomic(uint32_t) last_sequence_number = kSeqNoNotInit; |
Florian Mayer | b06766c | 2019-12-10 12:20:03 +0000 | [diff] [blame] | 62 | |
| 63 | #if defined(__BIONIC__) |
| 64 | // All zero prop_info that has a sequence number of 0. This is easier than |
| 65 | // depending on implementation details of the property implementation. |
| 66 | // |
| 67 | // prop_info is static_assert-ed to be 96 bytes, which cannot change due to |
| 68 | // ABI compatibility. |
| 69 | alignas(uint64_t) static char empty_pi[96]; |
| 70 | static const prop_info* atrace_property_info = reinterpret_cast<const prop_info*>(empty_pi); |
| 71 | #endif |
| 72 | |
Florian Mayer | b06766c | 2019-12-10 12:20:03 +0000 | [diff] [blame] | 73 | /** |
| 74 | * This is called when the sequence number of debug.atrace.tags.enableflags |
| 75 | * changes and we need to reload the enabled tags. |
| 76 | **/ |
| 77 | static void atrace_seq_number_changed(uint32_t prev_seq_no, uint32_t seq_no); |
| 78 | |
| 79 | void atrace_init() { |
| 80 | #if defined(__BIONIC__) |
| 81 | uint32_t seq_no = __system_property_serial(atrace_property_info); // Acquire semantics. |
| 82 | #else |
| 83 | uint32_t seq_no = 0; |
| 84 | #endif |
| 85 | uint32_t prev_seq_no = atomic_load_explicit(&last_sequence_number, memory_order_relaxed); |
| 86 | if (CC_UNLIKELY(seq_no != prev_seq_no)) { |
| 87 | atrace_seq_number_changed(prev_seq_no, seq_no); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | uint64_t atrace_get_enabled_tags() |
| 92 | { |
| 93 | atrace_init(); |
| 94 | return atrace_enabled_tags; |
| 95 | } |
Earl Ou | e403038 | 2016-11-22 17:04:44 +0800 | [diff] [blame] | 96 | |
Earl Ou | e403038 | 2016-11-22 17:04:44 +0800 | [diff] [blame] | 97 | // Check whether the given command line matches one of the comma-separated |
| 98 | // values listed in the app_cmdlines property. |
| 99 | static bool atrace_is_cmdline_match(const char* cmdline) |
| 100 | { |
| 101 | int count = property_get_int32("debug.atrace.app_number", 0); |
| 102 | |
| 103 | char buf[PROPERTY_KEY_MAX]; |
| 104 | char value[PROPERTY_VALUE_MAX]; |
| 105 | |
| 106 | for (int i = 0; i < count; i++) { |
| 107 | snprintf(buf, sizeof(buf), "debug.atrace.app_%d", i); |
| 108 | property_get(buf, value, ""); |
Florian Mayer | f124707 | 2021-02-11 19:31:48 +0000 | [diff] [blame] | 109 | if (fnmatch(value, cmdline, FNM_NOESCAPE) == 0) { |
Earl Ou | e403038 | 2016-11-22 17:04:44 +0800 | [diff] [blame] | 110 | return true; |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | return false; |
| 115 | } |
| 116 | |
| 117 | // Determine whether application-level tracing is enabled for this process. |
| 118 | static bool atrace_is_app_tracing_enabled() |
| 119 | { |
Earl Ou | e403038 | 2016-11-22 17:04:44 +0800 | [diff] [blame] | 120 | bool result = false; |
| 121 | |
Florian Mayer | cae942d | 2021-02-12 16:54:51 +0000 | [diff] [blame] | 122 | // Check whether tracing is enabled for this process. |
| 123 | FILE * file = fopen("/proc/self/cmdline", "re"); |
| 124 | if (file) { |
| 125 | char cmdline[4096]; |
| 126 | if (fgets(cmdline, sizeof(cmdline), file)) { |
| 127 | result = atrace_is_cmdline_match(cmdline); |
Earl Ou | e403038 | 2016-11-22 17:04:44 +0800 | [diff] [blame] | 128 | } else { |
Florian Mayer | cae942d | 2021-02-12 16:54:51 +0000 | [diff] [blame] | 129 | ALOGE("Error reading cmdline: %s (%d)", strerror(errno), errno); |
Earl Ou | e403038 | 2016-11-22 17:04:44 +0800 | [diff] [blame] | 130 | } |
Florian Mayer | cae942d | 2021-02-12 16:54:51 +0000 | [diff] [blame] | 131 | fclose(file); |
| 132 | } else { |
| 133 | ALOGE("Error opening /proc/self/cmdline: %s (%d)", strerror(errno), |
| 134 | errno); |
Earl Ou | e403038 | 2016-11-22 17:04:44 +0800 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | return result; |
| 138 | } |
| 139 | |
| 140 | // Read the sysprop and return the value tags should be set to |
| 141 | static uint64_t atrace_get_property() |
| 142 | { |
| 143 | char value[PROPERTY_VALUE_MAX]; |
| 144 | char *endptr; |
| 145 | uint64_t tags; |
| 146 | |
| 147 | property_get("debug.atrace.tags.enableflags", value, "0"); |
| 148 | errno = 0; |
| 149 | tags = strtoull(value, &endptr, 0); |
| 150 | if (value[0] == '\0' || *endptr != '\0') { |
| 151 | ALOGE("Error parsing trace property: Not a number: %s", value); |
| 152 | return 0; |
| 153 | } else if (errno == ERANGE || tags == ULLONG_MAX) { |
| 154 | ALOGE("Error parsing trace property: Number too large: %s", value); |
| 155 | return 0; |
| 156 | } |
| 157 | |
| 158 | // Only set the "app" tag if this process was selected for app-level debug |
| 159 | // tracing. |
| 160 | if (atrace_is_app_tracing_enabled()) { |
| 161 | tags |= ATRACE_TAG_APP; |
| 162 | } else { |
| 163 | tags &= ~ATRACE_TAG_APP; |
| 164 | } |
| 165 | |
| 166 | return (tags | ATRACE_TAG_ALWAYS) & ATRACE_TAG_VALID_MASK; |
| 167 | } |
| 168 | |
| 169 | // Update tags if tracing is ready. Useful as a sysprop change callback. |
| 170 | void atrace_update_tags() |
| 171 | { |
| 172 | uint64_t tags; |
Florian Mayer | 923880e | 2020-02-26 11:44:04 +0000 | [diff] [blame] | 173 | if (atomic_load_explicit(&atrace_is_enabled, memory_order_acquire)) { |
| 174 | tags = atrace_get_property(); |
| 175 | pthread_mutex_lock(&atrace_tags_mutex); |
| 176 | atrace_enabled_tags = tags; |
| 177 | pthread_mutex_unlock(&atrace_tags_mutex); |
| 178 | } else { |
| 179 | // Tracing is disabled for this process, so we simply don't |
| 180 | // initialize the tags. |
| 181 | pthread_mutex_lock(&atrace_tags_mutex); |
| 182 | atrace_enabled_tags = ATRACE_TAG_NOT_READY; |
| 183 | pthread_mutex_unlock(&atrace_tags_mutex); |
Earl Ou | e403038 | 2016-11-22 17:04:44 +0800 | [diff] [blame] | 184 | } |
| 185 | } |
| 186 | |
Ray Ye | 9a54240 | 2022-03-15 23:28:01 +0000 | [diff] [blame] | 187 | #define WRITE_MSG(format_begin, format_end, track_name, name, value) { \ |
Tim Murray | 0f85154 | 2020-04-21 10:05:09 -0700 | [diff] [blame] | 188 | char buf[ATRACE_MESSAGE_LENGTH] __attribute__((uninitialized)); \ |
Ray Ye | 9a54240 | 2022-03-15 23:28:01 +0000 | [diff] [blame] | 189 | const char* track_name_sep = track_name[0] != '\0' ? "|" : ""; \ |
Earl Ou | e403038 | 2016-11-22 17:04:44 +0800 | [diff] [blame] | 190 | int pid = getpid(); \ |
Ray Ye | 9a54240 | 2022-03-15 23:28:01 +0000 | [diff] [blame] | 191 | int len = snprintf(buf, sizeof(buf), format_begin "%s%s%s" format_end, pid, \ |
| 192 | track_name, track_name_sep, name, value); \ |
Earl Ou | e403038 | 2016-11-22 17:04:44 +0800 | [diff] [blame] | 193 | if (len >= (int) sizeof(buf)) { \ |
Earl Ou | e403038 | 2016-11-22 17:04:44 +0800 | [diff] [blame] | 194 | int name_len = strlen(name) - (len - sizeof(buf)) - 1; \ |
| 195 | /* Truncate the name to make the message fit. */ \ |
Ray Ye | 9a54240 | 2022-03-15 23:28:01 +0000 | [diff] [blame] | 196 | if (name_len > 0) { \ |
| 197 | len = snprintf(buf, sizeof(buf), format_begin "%s%s%.*s" format_end, pid, \ |
| 198 | track_name, track_name_sep, name_len, name, value); \ |
| 199 | } else { \ |
| 200 | int track_name_len = 0; \ |
| 201 | if (track_name[0] != '\0') { \ |
| 202 | track_name_len = strlen(track_name) - (len - strlen(name) - sizeof(buf)) - 2; \ |
| 203 | } \ |
| 204 | if (track_name_len <= 0) { \ |
| 205 | /* Data is still too long. Drop it. */ \ |
| 206 | len = 0; \ |
| 207 | } else { \ |
| 208 | /* Truncate the trackName and name to make the message fit */ \ |
| 209 | len = snprintf(buf, sizeof(buf), format_begin "%.*s|%.1s" format_end, pid, \ |
| 210 | track_name_len, track_name, name, value); \ |
| 211 | } \ |
| 212 | } \ |
Earl Ou | e403038 | 2016-11-22 17:04:44 +0800 | [diff] [blame] | 213 | } \ |
Ray Ye | 9a54240 | 2022-03-15 23:28:01 +0000 | [diff] [blame] | 214 | if (len > 0) { \ |
| 215 | write(atrace_marker_fd, buf, len); \ |
| 216 | } \ |
Earl Ou | e403038 | 2016-11-22 17:04:44 +0800 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | #endif // __TRACE_DEV_INC |