blob: 654342649337453c4ab0796c666c328d2c52f57c [file] [log] [blame]
Earl Oue4030382016-11-22 17:04:44 +08001/*
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 Oue4030382016-11-22 17:04:44 +080027#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 Cherry350164c2019-10-23 16:25:55 +000035#include <log/log_properties.h>
Earl Oue4030382016-11-22 17:04:44 +080036
Florian Mayerb06766c2019-12-10 12:20:03 +000037#if defined(__BIONIC__)
38#define _REALLY_INCLUDE_SYS__SYSTEM_PROPERTIES_H_
39#include <sys/_system_properties.h>
40#endif
41
Earl Oue4030382016-11-22 17:04:44 +080042/**
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 Mayerb06766c2019-12-10 12:20:03 +000049constexpr uint32_t kSeqNoNotInit = static_cast<uint32_t>(-1);
50
51atomic_bool atrace_is_ready = ATOMIC_VAR_INIT(false);
52int atrace_marker_fd = -1;
53uint64_t atrace_enabled_tags = ATRACE_TAG_NOT_READY;
54static bool atrace_is_debuggable = false;
55static atomic_bool atrace_is_enabled = ATOMIC_VAR_INIT(true);
56static 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 **/
62static _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.
70alignas(uint64_t) static char empty_pi[96];
71static const prop_info* atrace_property_info = reinterpret_cast<const prop_info*>(empty_pi);
72#endif
73
Florian Mayerb06766c2019-12-10 12:20:03 +000074/**
75 * This is called when the sequence number of debug.atrace.tags.enableflags
76 * changes and we need to reload the enabled tags.
77 **/
78static void atrace_seq_number_changed(uint32_t prev_seq_no, uint32_t seq_no);
79
80void atrace_init() {
81#if defined(__BIONIC__)
82 uint32_t seq_no = __system_property_serial(atrace_property_info); // Acquire semantics.
83#else
84 uint32_t seq_no = 0;
85#endif
86 uint32_t prev_seq_no = atomic_load_explicit(&last_sequence_number, memory_order_relaxed);
87 if (CC_UNLIKELY(seq_no != prev_seq_no)) {
88 atrace_seq_number_changed(prev_seq_no, seq_no);
89 }
90}
91
92uint64_t atrace_get_enabled_tags()
93{
94 atrace_init();
95 return atrace_enabled_tags;
96}
Earl Oue4030382016-11-22 17:04:44 +080097
98// Set whether this process is debuggable, which determines whether
99// application-level tracing is allowed when the ro.debuggable system property
100// is not set to '1'.
101void atrace_set_debuggable(bool debuggable)
102{
103 atrace_is_debuggable = debuggable;
104 atrace_update_tags();
105}
106
107// Check whether the given command line matches one of the comma-separated
108// values listed in the app_cmdlines property.
109static bool atrace_is_cmdline_match(const char* cmdline)
110{
111 int count = property_get_int32("debug.atrace.app_number", 0);
112
113 char buf[PROPERTY_KEY_MAX];
114 char value[PROPERTY_VALUE_MAX];
115
116 for (int i = 0; i < count; i++) {
117 snprintf(buf, sizeof(buf), "debug.atrace.app_%d", i);
118 property_get(buf, value, "");
Daniel Colascione642ef982018-02-09 20:07:23 -0800119 if (strcmp(value, "*") == 0 || strcmp(value, cmdline) == 0) {
Earl Oue4030382016-11-22 17:04:44 +0800120 return true;
121 }
122 }
123
124 return false;
125}
126
127// Determine whether application-level tracing is enabled for this process.
128static bool atrace_is_app_tracing_enabled()
129{
Tom Cherry15800942019-02-20 12:51:33 -0800130 bool sys_debuggable = property_get_bool("ro.debuggable", 0);
Earl Oue4030382016-11-22 17:04:44 +0800131 bool result = false;
132
133 if (sys_debuggable || atrace_is_debuggable) {
134 // Check whether tracing is enabled for this process.
135 FILE * file = fopen("/proc/self/cmdline", "re");
136 if (file) {
137 char cmdline[4096];
138 if (fgets(cmdline, sizeof(cmdline), file)) {
139 result = atrace_is_cmdline_match(cmdline);
140 } else {
141 ALOGE("Error reading cmdline: %s (%d)", strerror(errno), errno);
142 }
143 fclose(file);
144 } else {
145 ALOGE("Error opening /proc/self/cmdline: %s (%d)", strerror(errno),
146 errno);
147 }
148 }
149
150 return result;
151}
152
153// Read the sysprop and return the value tags should be set to
154static uint64_t atrace_get_property()
155{
156 char value[PROPERTY_VALUE_MAX];
157 char *endptr;
158 uint64_t tags;
159
160 property_get("debug.atrace.tags.enableflags", value, "0");
161 errno = 0;
162 tags = strtoull(value, &endptr, 0);
163 if (value[0] == '\0' || *endptr != '\0') {
164 ALOGE("Error parsing trace property: Not a number: %s", value);
165 return 0;
166 } else if (errno == ERANGE || tags == ULLONG_MAX) {
167 ALOGE("Error parsing trace property: Number too large: %s", value);
168 return 0;
169 }
170
171 // Only set the "app" tag if this process was selected for app-level debug
172 // tracing.
173 if (atrace_is_app_tracing_enabled()) {
174 tags |= ATRACE_TAG_APP;
175 } else {
176 tags &= ~ATRACE_TAG_APP;
177 }
178
179 return (tags | ATRACE_TAG_ALWAYS) & ATRACE_TAG_VALID_MASK;
180}
181
182// Update tags if tracing is ready. Useful as a sysprop change callback.
183void atrace_update_tags()
184{
185 uint64_t tags;
Florian Mayer923880e2020-02-26 11:44:04 +0000186 if (atomic_load_explicit(&atrace_is_enabled, memory_order_acquire)) {
187 tags = atrace_get_property();
188 pthread_mutex_lock(&atrace_tags_mutex);
189 atrace_enabled_tags = tags;
190 pthread_mutex_unlock(&atrace_tags_mutex);
191 } else {
192 // Tracing is disabled for this process, so we simply don't
193 // initialize the tags.
194 pthread_mutex_lock(&atrace_tags_mutex);
195 atrace_enabled_tags = ATRACE_TAG_NOT_READY;
196 pthread_mutex_unlock(&atrace_tags_mutex);
Earl Oue4030382016-11-22 17:04:44 +0800197 }
198}
199
200#define WRITE_MSG(format_begin, format_end, name, value) { \
Tim Murray0f851542020-04-21 10:05:09 -0700201 char buf[ATRACE_MESSAGE_LENGTH] __attribute__((uninitialized)); \
Earl Oue4030382016-11-22 17:04:44 +0800202 int pid = getpid(); \
203 int len = snprintf(buf, sizeof(buf), format_begin "%s" format_end, pid, \
204 name, value); \
205 if (len >= (int) sizeof(buf)) { \
206 /* Given the sizeof(buf), and all of the current format buffers, \
207 * it is impossible for name_len to be < 0 if len >= sizeof(buf). */ \
208 int name_len = strlen(name) - (len - sizeof(buf)) - 1; \
209 /* Truncate the name to make the message fit. */ \
210 ALOGW("Truncated name in %s: %s\n", __FUNCTION__, name); \
211 len = snprintf(buf, sizeof(buf), format_begin "%.*s" format_end, pid, \
212 name_len, name, value); \
213 } \
214 write(atrace_marker_fd, buf, len); \
215}
216
217#endif // __TRACE_DEV_INC