blob: 3bc6dc343ce4ba602fb07e410eb8558f01f627f0 [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
Christopher Ferrisa3a6c8e2024-07-11 23:54:48 +000052atomic_bool atrace_is_ready = false;
Florian Mayerb06766c2019-12-10 12:20:03 +000053int atrace_marker_fd = -1;
54uint64_t atrace_enabled_tags = ATRACE_TAG_NOT_READY;
Christopher Ferrisa3a6c8e2024-07-11 23:54:48 +000055static atomic_bool atrace_is_enabled = true;
Florian Mayerb06766c2019-12-10 12:20:03 +000056static 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 **/
Christopher Ferrisa3a6c8e2024-07-11 23:54:48 +000062static _Atomic(uint32_t) last_sequence_number = kSeqNoNotInit;
Florian Mayerb06766c2019-12-10 12:20:03 +000063
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
Earl Oue4030382016-11-22 17:04:44 +080098// Check whether the given command line matches one of the comma-separated
99// values listed in the app_cmdlines property.
100static bool atrace_is_cmdline_match(const char* cmdline)
101{
102 int count = property_get_int32("debug.atrace.app_number", 0);
103
104 char buf[PROPERTY_KEY_MAX];
105 char value[PROPERTY_VALUE_MAX];
106
107 for (int i = 0; i < count; i++) {
108 snprintf(buf, sizeof(buf), "debug.atrace.app_%d", i);
109 property_get(buf, value, "");
Florian Mayerf1247072021-02-11 19:31:48 +0000110 if (fnmatch(value, cmdline, FNM_NOESCAPE) == 0) {
Earl Oue4030382016-11-22 17:04:44 +0800111 return true;
112 }
113 }
114
115 return false;
116}
117
118// Determine whether application-level tracing is enabled for this process.
119static bool atrace_is_app_tracing_enabled()
120{
Earl Oue4030382016-11-22 17:04:44 +0800121 bool result = false;
122
Florian Mayercae942d2021-02-12 16:54:51 +0000123 // Check whether tracing is enabled for this process.
124 FILE * file = fopen("/proc/self/cmdline", "re");
125 if (file) {
126 char cmdline[4096];
127 if (fgets(cmdline, sizeof(cmdline), file)) {
128 result = atrace_is_cmdline_match(cmdline);
Earl Oue4030382016-11-22 17:04:44 +0800129 } else {
Florian Mayercae942d2021-02-12 16:54:51 +0000130 ALOGE("Error reading cmdline: %s (%d)", strerror(errno), errno);
Earl Oue4030382016-11-22 17:04:44 +0800131 }
Florian Mayercae942d2021-02-12 16:54:51 +0000132 fclose(file);
133 } else {
134 ALOGE("Error opening /proc/self/cmdline: %s (%d)", strerror(errno),
135 errno);
Earl Oue4030382016-11-22 17:04:44 +0800136 }
137
138 return result;
139}
140
141// Read the sysprop and return the value tags should be set to
142static uint64_t atrace_get_property()
143{
144 char value[PROPERTY_VALUE_MAX];
145 char *endptr;
146 uint64_t tags;
147
148 property_get("debug.atrace.tags.enableflags", value, "0");
149 errno = 0;
150 tags = strtoull(value, &endptr, 0);
151 if (value[0] == '\0' || *endptr != '\0') {
152 ALOGE("Error parsing trace property: Not a number: %s", value);
153 return 0;
154 } else if (errno == ERANGE || tags == ULLONG_MAX) {
155 ALOGE("Error parsing trace property: Number too large: %s", value);
156 return 0;
157 }
158
159 // Only set the "app" tag if this process was selected for app-level debug
160 // tracing.
161 if (atrace_is_app_tracing_enabled()) {
162 tags |= ATRACE_TAG_APP;
163 } else {
164 tags &= ~ATRACE_TAG_APP;
165 }
166
167 return (tags | ATRACE_TAG_ALWAYS) & ATRACE_TAG_VALID_MASK;
168}
169
170// Update tags if tracing is ready. Useful as a sysprop change callback.
171void atrace_update_tags()
172{
173 uint64_t tags;
Florian Mayer923880e2020-02-26 11:44:04 +0000174 if (atomic_load_explicit(&atrace_is_enabled, memory_order_acquire)) {
175 tags = atrace_get_property();
176 pthread_mutex_lock(&atrace_tags_mutex);
177 atrace_enabled_tags = tags;
178 pthread_mutex_unlock(&atrace_tags_mutex);
179 } else {
180 // Tracing is disabled for this process, so we simply don't
181 // initialize the tags.
182 pthread_mutex_lock(&atrace_tags_mutex);
183 atrace_enabled_tags = ATRACE_TAG_NOT_READY;
184 pthread_mutex_unlock(&atrace_tags_mutex);
Earl Oue4030382016-11-22 17:04:44 +0800185 }
186}
187
Ray Ye9a542402022-03-15 23:28:01 +0000188#define WRITE_MSG(format_begin, format_end, track_name, name, value) { \
Tim Murray0f851542020-04-21 10:05:09 -0700189 char buf[ATRACE_MESSAGE_LENGTH] __attribute__((uninitialized)); \
Ray Ye9a542402022-03-15 23:28:01 +0000190 const char* track_name_sep = track_name[0] != '\0' ? "|" : ""; \
Earl Oue4030382016-11-22 17:04:44 +0800191 int pid = getpid(); \
Ray Ye9a542402022-03-15 23:28:01 +0000192 int len = snprintf(buf, sizeof(buf), format_begin "%s%s%s" format_end, pid, \
193 track_name, track_name_sep, name, value); \
Earl Oue4030382016-11-22 17:04:44 +0800194 if (len >= (int) sizeof(buf)) { \
Earl Oue4030382016-11-22 17:04:44 +0800195 int name_len = strlen(name) - (len - sizeof(buf)) - 1; \
196 /* Truncate the name to make the message fit. */ \
Ray Ye9a542402022-03-15 23:28:01 +0000197 if (name_len > 0) { \
198 len = snprintf(buf, sizeof(buf), format_begin "%s%s%.*s" format_end, pid, \
199 track_name, track_name_sep, name_len, name, value); \
200 } else { \
201 int track_name_len = 0; \
202 if (track_name[0] != '\0') { \
203 track_name_len = strlen(track_name) - (len - strlen(name) - sizeof(buf)) - 2; \
204 } \
205 if (track_name_len <= 0) { \
206 /* Data is still too long. Drop it. */ \
207 len = 0; \
208 } else { \
209 /* Truncate the trackName and name to make the message fit */ \
210 len = snprintf(buf, sizeof(buf), format_begin "%.*s|%.1s" format_end, pid, \
211 track_name_len, track_name, name, value); \
212 } \
213 } \
Earl Oue4030382016-11-22 17:04:44 +0800214 } \
Ray Ye9a542402022-03-15 23:28:01 +0000215 if (len > 0) { \
216 write(atrace_marker_fd, buf, len); \
217 } \
Earl Oue4030382016-11-22 17:04:44 +0800218}
219
220#endif // __TRACE_DEV_INC