blob: 80205bc508075ab395a349d1d62b1eb6cd7b8ece [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>
Florian Mayerf1247072021-02-11 19:31:48 +000024#include <fnmatch.h>
Earl Oue4030382016-11-22 17:04:44 +080025#include <limits.h>
26#include <pthread.h>
27#include <stdatomic.h>
Earl Oue4030382016-11-22 17:04:44 +080028#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 Cherry350164c2019-10-23 16:25:55 +000036#include <log/log_properties.h>
Earl Oue4030382016-11-22 17:04:44 +080037
Florian Mayerb06766c2019-12-10 12:20:03 +000038#if defined(__BIONIC__)
39#define _REALLY_INCLUDE_SYS__SYSTEM_PROPERTIES_H_
40#include <sys/_system_properties.h>
41#endif
42
Earl Oue4030382016-11-22 17:04:44 +080043/**
44 * Maximum size of a message that can be logged to the trace buffer.
45 * Note this message includes a tag, the pid, and the string given as the name.
46 * Names should be kept short to get the most use of the trace buffer.
47 */
48#define ATRACE_MESSAGE_LENGTH 1024
49
Florian Mayerb06766c2019-12-10 12:20:03 +000050constexpr uint32_t kSeqNoNotInit = static_cast<uint32_t>(-1);
51
52atomic_bool atrace_is_ready = ATOMIC_VAR_INIT(false);
53int atrace_marker_fd = -1;
54uint64_t atrace_enabled_tags = ATRACE_TAG_NOT_READY;
55static bool atrace_is_debuggable = false;
56static atomic_bool atrace_is_enabled = ATOMIC_VAR_INIT(true);
57static pthread_mutex_t atrace_tags_mutex = PTHREAD_MUTEX_INITIALIZER;
58
59/**
60 * Sequence number of debug.atrace.tags.enableflags the last time the enabled
61 * tags were reloaded.
62 **/
63static _Atomic(uint32_t) last_sequence_number = ATOMIC_VAR_INIT(kSeqNoNotInit);
64
65#if defined(__BIONIC__)
66// All zero prop_info that has a sequence number of 0. This is easier than
67// depending on implementation details of the property implementation.
68//
69// prop_info is static_assert-ed to be 96 bytes, which cannot change due to
70// ABI compatibility.
71alignas(uint64_t) static char empty_pi[96];
72static const prop_info* atrace_property_info = reinterpret_cast<const prop_info*>(empty_pi);
73#endif
74
Florian Mayerb06766c2019-12-10 12:20:03 +000075/**
76 * This is called when the sequence number of debug.atrace.tags.enableflags
77 * changes and we need to reload the enabled tags.
78 **/
79static void atrace_seq_number_changed(uint32_t prev_seq_no, uint32_t seq_no);
80
81void atrace_init() {
82#if defined(__BIONIC__)
83 uint32_t seq_no = __system_property_serial(atrace_property_info); // Acquire semantics.
84#else
85 uint32_t seq_no = 0;
86#endif
87 uint32_t prev_seq_no = atomic_load_explicit(&last_sequence_number, memory_order_relaxed);
88 if (CC_UNLIKELY(seq_no != prev_seq_no)) {
89 atrace_seq_number_changed(prev_seq_no, seq_no);
90 }
91}
92
93uint64_t atrace_get_enabled_tags()
94{
95 atrace_init();
96 return atrace_enabled_tags;
97}
Earl Oue4030382016-11-22 17:04:44 +080098
99// Set whether this process is debuggable, which determines whether
100// application-level tracing is allowed when the ro.debuggable system property
101// is not set to '1'.
102void atrace_set_debuggable(bool debuggable)
103{
104 atrace_is_debuggable = debuggable;
105 atrace_update_tags();
106}
107
108// Check whether the given command line matches one of the comma-separated
109// values listed in the app_cmdlines property.
110static bool atrace_is_cmdline_match(const char* cmdline)
111{
112 int count = property_get_int32("debug.atrace.app_number", 0);
113
114 char buf[PROPERTY_KEY_MAX];
115 char value[PROPERTY_VALUE_MAX];
116
117 for (int i = 0; i < count; i++) {
118 snprintf(buf, sizeof(buf), "debug.atrace.app_%d", i);
119 property_get(buf, value, "");
Florian Mayerf1247072021-02-11 19:31:48 +0000120 if (fnmatch(value, cmdline, FNM_NOESCAPE) == 0) {
Earl Oue4030382016-11-22 17:04:44 +0800121 return true;
122 }
123 }
124
125 return false;
126}
127
128// Determine whether application-level tracing is enabled for this process.
129static bool atrace_is_app_tracing_enabled()
130{
Tom Cherry15800942019-02-20 12:51:33 -0800131 bool sys_debuggable = property_get_bool("ro.debuggable", 0);
Earl Oue4030382016-11-22 17:04:44 +0800132 bool result = false;
133
134 if (sys_debuggable || atrace_is_debuggable) {
135 // Check whether tracing is enabled for this process.
136 FILE * file = fopen("/proc/self/cmdline", "re");
137 if (file) {
138 char cmdline[4096];
139 if (fgets(cmdline, sizeof(cmdline), file)) {
140 result = atrace_is_cmdline_match(cmdline);
141 } else {
142 ALOGE("Error reading cmdline: %s (%d)", strerror(errno), errno);
143 }
144 fclose(file);
145 } else {
146 ALOGE("Error opening /proc/self/cmdline: %s (%d)", strerror(errno),
147 errno);
148 }
149 }
150
151 return result;
152}
153
154// Read the sysprop and return the value tags should be set to
155static uint64_t atrace_get_property()
156{
157 char value[PROPERTY_VALUE_MAX];
158 char *endptr;
159 uint64_t tags;
160
161 property_get("debug.atrace.tags.enableflags", value, "0");
162 errno = 0;
163 tags = strtoull(value, &endptr, 0);
164 if (value[0] == '\0' || *endptr != '\0') {
165 ALOGE("Error parsing trace property: Not a number: %s", value);
166 return 0;
167 } else if (errno == ERANGE || tags == ULLONG_MAX) {
168 ALOGE("Error parsing trace property: Number too large: %s", value);
169 return 0;
170 }
171
172 // Only set the "app" tag if this process was selected for app-level debug
173 // tracing.
174 if (atrace_is_app_tracing_enabled()) {
175 tags |= ATRACE_TAG_APP;
176 } else {
177 tags &= ~ATRACE_TAG_APP;
178 }
179
180 return (tags | ATRACE_TAG_ALWAYS) & ATRACE_TAG_VALID_MASK;
181}
182
183// Update tags if tracing is ready. Useful as a sysprop change callback.
184void atrace_update_tags()
185{
186 uint64_t tags;
Florian Mayer923880e2020-02-26 11:44:04 +0000187 if (atomic_load_explicit(&atrace_is_enabled, memory_order_acquire)) {
188 tags = atrace_get_property();
189 pthread_mutex_lock(&atrace_tags_mutex);
190 atrace_enabled_tags = tags;
191 pthread_mutex_unlock(&atrace_tags_mutex);
192 } else {
193 // Tracing is disabled for this process, so we simply don't
194 // initialize the tags.
195 pthread_mutex_lock(&atrace_tags_mutex);
196 atrace_enabled_tags = ATRACE_TAG_NOT_READY;
197 pthread_mutex_unlock(&atrace_tags_mutex);
Earl Oue4030382016-11-22 17:04:44 +0800198 }
199}
200
201#define WRITE_MSG(format_begin, format_end, name, value) { \
Tim Murray0f851542020-04-21 10:05:09 -0700202 char buf[ATRACE_MESSAGE_LENGTH] __attribute__((uninitialized)); \
Earl Oue4030382016-11-22 17:04:44 +0800203 int pid = getpid(); \
204 int len = snprintf(buf, sizeof(buf), format_begin "%s" format_end, pid, \
205 name, value); \
206 if (len >= (int) sizeof(buf)) { \
207 /* Given the sizeof(buf), and all of the current format buffers, \
208 * it is impossible for name_len to be < 0 if len >= sizeof(buf). */ \
209 int name_len = strlen(name) - (len - sizeof(buf)) - 1; \
210 /* Truncate the name to make the message fit. */ \
211 ALOGW("Truncated name in %s: %s\n", __FUNCTION__, name); \
212 len = snprintf(buf, sizeof(buf), format_begin "%.*s" format_end, pid, \
213 name_len, name, value); \
214 } \
215 write(atrace_marker_fd, buf, len); \
216}
217
218#endif // __TRACE_DEV_INC