blob: a57a4c59eb049ffd470f05a1d1a056a711d70812 [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
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 **/
80static void atrace_seq_number_changed(uint32_t prev_seq_no, uint32_t seq_no);
81
82void 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
94uint64_t atrace_get_enabled_tags()
95{
96 atrace_init();
97 return atrace_enabled_tags;
98}
99#endif
Earl Oue4030382016-11-22 17:04:44 +0800100
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'.
104void 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.
112static 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 Colascione642ef982018-02-09 20:07:23 -0800122 if (strcmp(value, "*") == 0 || strcmp(value, cmdline) == 0) {
Earl Oue4030382016-11-22 17:04:44 +0800123 return true;
124 }
125 }
126
127 return false;
128}
129
130// Determine whether application-level tracing is enabled for this process.
131static bool atrace_is_app_tracing_enabled()
132{
Tom Cherry15800942019-02-20 12:51:33 -0800133 bool sys_debuggable = property_get_bool("ro.debuggable", 0);
Earl Oue4030382016-11-22 17:04:44 +0800134 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
157static 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.
186void atrace_update_tags()
187{
188 uint64_t tags;
Florian Mayerb06766c2019-12-10 12:20:03 +0000189 if (ATRACE_SHMEM || CC_UNLIKELY(atomic_load_explicit(&atrace_is_ready, memory_order_acquire))) {
Earl Oue4030382016-11-22 17:04:44 +0800190 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