blob: b91c7bef381d4550667ff7daabd91644ccf2e010 [file] [log] [blame]
Alex Ray0a346432012-11-14 17:25:28 -08001/*
2 * Copyright (C) 2012 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
Mark Salyzyn23ed4c22016-09-28 13:33:27 -070017#define LOG_TAG "cutils-trace"
18
Alex Ray0a346432012-11-14 17:25:28 -080019#include <errno.h>
20#include <fcntl.h>
21#include <limits.h>
22#include <pthread.h>
Yabin Cuia8ac32c2015-04-15 14:50:27 -070023#include <stdatomic.h>
Jamie Gennis774f9292013-02-25 18:15:40 -080024#include <stdbool.h>
Alex Ray0a346432012-11-14 17:25:28 -080025#include <stdlib.h>
26#include <string.h>
27#include <sys/types.h>
Alex Ray0a346432012-11-14 17:25:28 -080028#include <cutils/compiler.h>
29#include <cutils/properties.h>
30#include <cutils/trace.h>
31
Mark Salyzyn23ed4c22016-09-28 13:33:27 -070032#include <android/log.h>
Alex Ray0a346432012-11-14 17:25:28 -080033
Chih-Hung Hsieh2d3150e2014-10-13 16:30:24 -070034/**
35 * Maximum size of a message that can be logged to the trace buffer.
36 * Note this message includes a tag, the pid, and the string given as the name.
37 * Names should be kept short to get the most use of the trace buffer.
38 */
39#define ATRACE_MESSAGE_LENGTH 1024
40
Yabin Cuia8ac32c2015-04-15 14:50:27 -070041atomic_bool atrace_is_ready = ATOMIC_VAR_INIT(false);
Jamie Gennisb13ea452013-04-15 18:50:22 -070042int atrace_marker_fd = -1;
43uint64_t atrace_enabled_tags = ATRACE_TAG_NOT_READY;
44static bool atrace_is_debuggable = false;
Yabin Cuia8ac32c2015-04-15 14:50:27 -070045static atomic_bool atrace_is_enabled = ATOMIC_VAR_INIT(true);
Jamie Gennisb13ea452013-04-15 18:50:22 -070046static pthread_once_t atrace_once_control = PTHREAD_ONCE_INIT;
47static pthread_mutex_t atrace_tags_mutex = PTHREAD_MUTEX_INITIALIZER;
Jamie Gennis774f9292013-02-25 18:15:40 -080048
49// Set whether this process is debuggable, which determines whether
50// application-level tracing is allowed when the ro.debuggable system property
51// is not set to '1'.
52void atrace_set_debuggable(bool debuggable)
53{
54 atrace_is_debuggable = debuggable;
55 atrace_update_tags();
56}
57
Jamie Gennisb13ea452013-04-15 18:50:22 -070058// Set whether tracing is enabled in this process. This is used to prevent
59// the Zygote process from tracing.
60void atrace_set_tracing_enabled(bool enabled)
61{
Yabin Cuia8ac32c2015-04-15 14:50:27 -070062 atomic_store_explicit(&atrace_is_enabled, enabled, memory_order_release);
Jamie Gennisb13ea452013-04-15 18:50:22 -070063 atrace_update_tags();
64}
65
Jamie Gennis774f9292013-02-25 18:15:40 -080066// Check whether the given command line matches one of the comma-separated
67// values listed in the app_cmdlines property.
Jamie Gennisb13ea452013-04-15 18:50:22 -070068static bool atrace_is_cmdline_match(const char* cmdline)
69{
sergeyvc19588c2016-04-28 19:33:09 -070070 int count = property_get_int32("debug.atrace.app_number", 0);
71
72 char buf[PROPERTY_KEY_MAX];
Jamie Gennis774f9292013-02-25 18:15:40 -080073 char value[PROPERTY_VALUE_MAX];
Jamie Gennis774f9292013-02-25 18:15:40 -080074
sergeyvc19588c2016-04-28 19:33:09 -070075 for (int i = 0; i < count; i++) {
76 snprintf(buf, sizeof(buf), "debug.atrace.app_%d", i);
77 property_get(buf, value, "");
78 if (strcmp(value, cmdline) == 0) {
Jamie Gennis774f9292013-02-25 18:15:40 -080079 return true;
80 }
Jamie Gennis774f9292013-02-25 18:15:40 -080081 }
82
83 return false;
84}
85
86// Determine whether application-level tracing is enabled for this process.
87static bool atrace_is_app_tracing_enabled()
88{
89 bool sys_debuggable = false;
Jamie Gennis774f9292013-02-25 18:15:40 -080090 char value[PROPERTY_VALUE_MAX];
91 bool result = false;
92
93 // Check whether the system is debuggable.
94 property_get("ro.debuggable", value, "0");
95 if (value[0] == '1') {
96 sys_debuggable = true;
97 }
98
99 if (sys_debuggable || atrace_is_debuggable) {
100 // Check whether tracing is enabled for this process.
Nick Kralevichdee1ef42015-12-16 12:32:26 -0800101 FILE * file = fopen("/proc/self/cmdline", "re");
Jamie Gennis774f9292013-02-25 18:15:40 -0800102 if (file) {
103 char cmdline[4096];
104 if (fgets(cmdline, sizeof(cmdline), file)) {
105 result = atrace_is_cmdline_match(cmdline);
106 } else {
107 ALOGE("Error reading cmdline: %s (%d)", strerror(errno), errno);
108 }
109 fclose(file);
110 } else {
111 ALOGE("Error opening /proc/self/cmdline: %s (%d)", strerror(errno),
112 errno);
113 }
114 }
115
116 return result;
117}
Alex Ray0a346432012-11-14 17:25:28 -0800118
119// Read the sysprop and return the value tags should be set to
120static uint64_t atrace_get_property()
121{
122 char value[PROPERTY_VALUE_MAX];
123 char *endptr;
124 uint64_t tags;
125
126 property_get("debug.atrace.tags.enableflags", value, "0");
127 errno = 0;
128 tags = strtoull(value, &endptr, 0);
129 if (value[0] == '\0' || *endptr != '\0') {
130 ALOGE("Error parsing trace property: Not a number: %s", value);
131 return 0;
132 } else if (errno == ERANGE || tags == ULLONG_MAX) {
133 ALOGE("Error parsing trace property: Number too large: %s", value);
134 return 0;
135 }
Jamie Gennis774f9292013-02-25 18:15:40 -0800136
137 // Only set the "app" tag if this process was selected for app-level debug
138 // tracing.
139 if (atrace_is_app_tracing_enabled()) {
140 tags |= ATRACE_TAG_APP;
141 } else {
142 tags &= ~ATRACE_TAG_APP;
143 }
144
Alex Ray0a346432012-11-14 17:25:28 -0800145 return (tags | ATRACE_TAG_ALWAYS) & ATRACE_TAG_VALID_MASK;
146}
147
Alex Raye7bb7bc2012-11-20 01:39:09 -0800148// Update tags if tracing is ready. Useful as a sysprop change callback.
149void atrace_update_tags()
150{
151 uint64_t tags;
Yabin Cuia8ac32c2015-04-15 14:50:27 -0700152 if (CC_UNLIKELY(atomic_load_explicit(&atrace_is_ready, memory_order_acquire))) {
153 if (atomic_load_explicit(&atrace_is_enabled, memory_order_acquire)) {
Jamie Gennisb13ea452013-04-15 18:50:22 -0700154 tags = atrace_get_property();
155 pthread_mutex_lock(&atrace_tags_mutex);
156 atrace_enabled_tags = tags;
157 pthread_mutex_unlock(&atrace_tags_mutex);
158 } else {
159 // Tracing is disabled for this process, so we simply don't
160 // initialize the tags.
161 pthread_mutex_lock(&atrace_tags_mutex);
162 atrace_enabled_tags = ATRACE_TAG_NOT_READY;
163 pthread_mutex_unlock(&atrace_tags_mutex);
164 }
Alex Raye7bb7bc2012-11-20 01:39:09 -0800165 }
166}
167
Alex Ray0a346432012-11-14 17:25:28 -0800168static void atrace_init_once()
169{
Nick Kralevichdee1ef42015-12-16 12:32:26 -0800170 atrace_marker_fd = open("/sys/kernel/debug/tracing/trace_marker", O_WRONLY | O_CLOEXEC);
Alex Ray0a346432012-11-14 17:25:28 -0800171 if (atrace_marker_fd == -1) {
172 ALOGE("Error opening trace file: %s (%d)", strerror(errno), errno);
173 atrace_enabled_tags = 0;
174 goto done;
175 }
176
177 atrace_enabled_tags = atrace_get_property();
178
179done:
Yabin Cuia8ac32c2015-04-15 14:50:27 -0700180 atomic_store_explicit(&atrace_is_ready, true, memory_order_release);
Alex Ray0a346432012-11-14 17:25:28 -0800181}
182
183void atrace_setup()
184{
185 pthread_once(&atrace_once_control, atrace_init_once);
186}
Chih-Hung Hsieh2d3150e2014-10-13 16:30:24 -0700187
188void atrace_begin_body(const char* name)
189{
190 char buf[ATRACE_MESSAGE_LENGTH];
Chih-Hung Hsieh2d3150e2014-10-13 16:30:24 -0700191
Christopher Ferris82d84892016-02-23 18:02:20 -0800192 int len = snprintf(buf, sizeof(buf), "B|%d|%s", getpid(), name);
193 if (len >= (int) sizeof(buf)) {
194 ALOGW("Truncated name in %s: %s\n", __FUNCTION__, name);
195 len = sizeof(buf) - 1;
196 }
Chih-Hung Hsieh2d3150e2014-10-13 16:30:24 -0700197 write(atrace_marker_fd, buf, len);
198}
199
Colin Cross9993e792016-09-16 10:12:52 -0700200void atrace_end_body()
201{
202 char c = 'E';
203 write(atrace_marker_fd, &c, 1);
204}
205
Christopher Ferris82d84892016-02-23 18:02:20 -0800206#define WRITE_MSG(format_begin, format_end, pid, name, value) { \
207 char buf[ATRACE_MESSAGE_LENGTH]; \
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}
Chih-Hung Hsieh2d3150e2014-10-13 16:30:24 -0700221
222void atrace_async_begin_body(const char* name, int32_t cookie)
223{
Christopher Ferris82d84892016-02-23 18:02:20 -0800224 WRITE_MSG("S|%d|", "|%" PRId32, getpid(), name, cookie);
Chih-Hung Hsieh2d3150e2014-10-13 16:30:24 -0700225}
226
227void atrace_async_end_body(const char* name, int32_t cookie)
228{
Christopher Ferris82d84892016-02-23 18:02:20 -0800229 WRITE_MSG("F|%d|", "|%" PRId32, getpid(), name, cookie);
Chih-Hung Hsieh2d3150e2014-10-13 16:30:24 -0700230}
231
232void atrace_int_body(const char* name, int32_t value)
233{
Christopher Ferris82d84892016-02-23 18:02:20 -0800234 WRITE_MSG("C|%d|", "|%" PRId32, getpid(), name, value);
Chih-Hung Hsieh2d3150e2014-10-13 16:30:24 -0700235}
236
237void atrace_int64_body(const char* name, int64_t value)
238{
Christopher Ferris82d84892016-02-23 18:02:20 -0800239 WRITE_MSG("C|%d|", "|%" PRId64, getpid(), name, value);
Chih-Hung Hsieh2d3150e2014-10-13 16:30:24 -0700240}