blob: 099ab458c01e43c908dfd52aa8a6cf729ab36005 [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
17#include <errno.h>
18#include <fcntl.h>
19#include <limits.h>
20#include <pthread.h>
Yabin Cuia8ac32c2015-04-15 14:50:27 -070021#include <stdatomic.h>
Jamie Gennis774f9292013-02-25 18:15:40 -080022#include <stdbool.h>
Alex Ray0a346432012-11-14 17:25:28 -080023#include <stdlib.h>
24#include <string.h>
25#include <sys/types.h>
Alex Ray0a346432012-11-14 17:25:28 -080026#include <cutils/compiler.h>
27#include <cutils/properties.h>
28#include <cutils/trace.h>
29
30#define LOG_TAG "cutils-trace"
Mark Salyzyn12717162014-04-29 15:49:14 -070031#include <log/log.h>
Alex Ray0a346432012-11-14 17:25:28 -080032
Chih-Hung Hsieh2d3150e2014-10-13 16:30:24 -070033/**
34 * Maximum size of a message that can be logged to the trace buffer.
35 * Note this message includes a tag, the pid, and the string given as the name.
36 * Names should be kept short to get the most use of the trace buffer.
37 */
38#define ATRACE_MESSAGE_LENGTH 1024
39
Yabin Cuia8ac32c2015-04-15 14:50:27 -070040atomic_bool atrace_is_ready = ATOMIC_VAR_INIT(false);
Jamie Gennisb13ea452013-04-15 18:50:22 -070041int atrace_marker_fd = -1;
42uint64_t atrace_enabled_tags = ATRACE_TAG_NOT_READY;
43static bool atrace_is_debuggable = false;
Yabin Cuia8ac32c2015-04-15 14:50:27 -070044static atomic_bool atrace_is_enabled = ATOMIC_VAR_INIT(true);
Jamie Gennisb13ea452013-04-15 18:50:22 -070045static pthread_once_t atrace_once_control = PTHREAD_ONCE_INIT;
46static pthread_mutex_t atrace_tags_mutex = PTHREAD_MUTEX_INITIALIZER;
Jamie Gennis774f9292013-02-25 18:15:40 -080047
48// Set whether this process is debuggable, which determines whether
49// application-level tracing is allowed when the ro.debuggable system property
50// is not set to '1'.
51void atrace_set_debuggable(bool debuggable)
52{
53 atrace_is_debuggable = debuggable;
54 atrace_update_tags();
55}
56
Jamie Gennisb13ea452013-04-15 18:50:22 -070057// Set whether tracing is enabled in this process. This is used to prevent
58// the Zygote process from tracing.
59void atrace_set_tracing_enabled(bool enabled)
60{
Yabin Cuia8ac32c2015-04-15 14:50:27 -070061 atomic_store_explicit(&atrace_is_enabled, enabled, memory_order_release);
Jamie Gennisb13ea452013-04-15 18:50:22 -070062 atrace_update_tags();
63}
64
Jamie Gennis774f9292013-02-25 18:15:40 -080065// Check whether the given command line matches one of the comma-separated
66// values listed in the app_cmdlines property.
Jamie Gennisb13ea452013-04-15 18:50:22 -070067static bool atrace_is_cmdline_match(const char* cmdline)
68{
sergeyvc19588c2016-04-28 19:33:09 -070069 int count = property_get_int32("debug.atrace.app_number", 0);
70
71 char buf[PROPERTY_KEY_MAX];
Jamie Gennis774f9292013-02-25 18:15:40 -080072 char value[PROPERTY_VALUE_MAX];
Jamie Gennis774f9292013-02-25 18:15:40 -080073
sergeyvc19588c2016-04-28 19:33:09 -070074 for (int i = 0; i < count; i++) {
75 snprintf(buf, sizeof(buf), "debug.atrace.app_%d", i);
76 property_get(buf, value, "");
77 if (strcmp(value, cmdline) == 0) {
Jamie Gennis774f9292013-02-25 18:15:40 -080078 return true;
79 }
Jamie Gennis774f9292013-02-25 18:15:40 -080080 }
81
82 return false;
83}
84
85// Determine whether application-level tracing is enabled for this process.
86static bool atrace_is_app_tracing_enabled()
87{
88 bool sys_debuggable = false;
Jamie Gennis774f9292013-02-25 18:15:40 -080089 char value[PROPERTY_VALUE_MAX];
90 bool result = false;
91
92 // Check whether the system is debuggable.
93 property_get("ro.debuggable", value, "0");
94 if (value[0] == '1') {
95 sys_debuggable = true;
96 }
97
98 if (sys_debuggable || atrace_is_debuggable) {
99 // Check whether tracing is enabled for this process.
Nick Kralevichdee1ef42015-12-16 12:32:26 -0800100 FILE * file = fopen("/proc/self/cmdline", "re");
Jamie Gennis774f9292013-02-25 18:15:40 -0800101 if (file) {
102 char cmdline[4096];
103 if (fgets(cmdline, sizeof(cmdline), file)) {
104 result = atrace_is_cmdline_match(cmdline);
105 } else {
106 ALOGE("Error reading cmdline: %s (%d)", strerror(errno), errno);
107 }
108 fclose(file);
109 } else {
110 ALOGE("Error opening /proc/self/cmdline: %s (%d)", strerror(errno),
111 errno);
112 }
113 }
114
115 return result;
116}
Alex Ray0a346432012-11-14 17:25:28 -0800117
118// Read the sysprop and return the value tags should be set to
119static uint64_t atrace_get_property()
120{
121 char value[PROPERTY_VALUE_MAX];
122 char *endptr;
123 uint64_t tags;
124
125 property_get("debug.atrace.tags.enableflags", value, "0");
126 errno = 0;
127 tags = strtoull(value, &endptr, 0);
128 if (value[0] == '\0' || *endptr != '\0') {
129 ALOGE("Error parsing trace property: Not a number: %s", value);
130 return 0;
131 } else if (errno == ERANGE || tags == ULLONG_MAX) {
132 ALOGE("Error parsing trace property: Number too large: %s", value);
133 return 0;
134 }
Jamie Gennis774f9292013-02-25 18:15:40 -0800135
136 // Only set the "app" tag if this process was selected for app-level debug
137 // tracing.
138 if (atrace_is_app_tracing_enabled()) {
139 tags |= ATRACE_TAG_APP;
140 } else {
141 tags &= ~ATRACE_TAG_APP;
142 }
143
Alex Ray0a346432012-11-14 17:25:28 -0800144 return (tags | ATRACE_TAG_ALWAYS) & ATRACE_TAG_VALID_MASK;
145}
146
Alex Raye7bb7bc2012-11-20 01:39:09 -0800147// Update tags if tracing is ready. Useful as a sysprop change callback.
148void atrace_update_tags()
149{
150 uint64_t tags;
Yabin Cuia8ac32c2015-04-15 14:50:27 -0700151 if (CC_UNLIKELY(atomic_load_explicit(&atrace_is_ready, memory_order_acquire))) {
152 if (atomic_load_explicit(&atrace_is_enabled, memory_order_acquire)) {
Jamie Gennisb13ea452013-04-15 18:50:22 -0700153 tags = atrace_get_property();
154 pthread_mutex_lock(&atrace_tags_mutex);
155 atrace_enabled_tags = tags;
156 pthread_mutex_unlock(&atrace_tags_mutex);
157 } else {
158 // Tracing is disabled for this process, so we simply don't
159 // initialize the tags.
160 pthread_mutex_lock(&atrace_tags_mutex);
161 atrace_enabled_tags = ATRACE_TAG_NOT_READY;
162 pthread_mutex_unlock(&atrace_tags_mutex);
163 }
Alex Raye7bb7bc2012-11-20 01:39:09 -0800164 }
165}
166
Alex Ray0a346432012-11-14 17:25:28 -0800167static void atrace_init_once()
168{
Nick Kralevichdee1ef42015-12-16 12:32:26 -0800169 atrace_marker_fd = open("/sys/kernel/debug/tracing/trace_marker", O_WRONLY | O_CLOEXEC);
Alex Ray0a346432012-11-14 17:25:28 -0800170 if (atrace_marker_fd == -1) {
171 ALOGE("Error opening trace file: %s (%d)", strerror(errno), errno);
172 atrace_enabled_tags = 0;
173 goto done;
174 }
175
176 atrace_enabled_tags = atrace_get_property();
177
178done:
Yabin Cuia8ac32c2015-04-15 14:50:27 -0700179 atomic_store_explicit(&atrace_is_ready, true, memory_order_release);
Alex Ray0a346432012-11-14 17:25:28 -0800180}
181
182void atrace_setup()
183{
184 pthread_once(&atrace_once_control, atrace_init_once);
185}
Chih-Hung Hsieh2d3150e2014-10-13 16:30:24 -0700186
187void atrace_begin_body(const char* name)
188{
189 char buf[ATRACE_MESSAGE_LENGTH];
Chih-Hung Hsieh2d3150e2014-10-13 16:30:24 -0700190
Christopher Ferris82d84892016-02-23 18:02:20 -0800191 int len = snprintf(buf, sizeof(buf), "B|%d|%s", getpid(), name);
192 if (len >= (int) sizeof(buf)) {
193 ALOGW("Truncated name in %s: %s\n", __FUNCTION__, name);
194 len = sizeof(buf) - 1;
195 }
Chih-Hung Hsieh2d3150e2014-10-13 16:30:24 -0700196 write(atrace_marker_fd, buf, len);
197}
198
Colin Cross9993e792016-09-16 10:12:52 -0700199void atrace_end_body()
200{
201 char c = 'E';
202 write(atrace_marker_fd, &c, 1);
203}
204
Christopher Ferris82d84892016-02-23 18:02:20 -0800205#define WRITE_MSG(format_begin, format_end, pid, name, value) { \
206 char buf[ATRACE_MESSAGE_LENGTH]; \
207 int len = snprintf(buf, sizeof(buf), format_begin "%s" format_end, pid, \
208 name, value); \
209 if (len >= (int) sizeof(buf)) { \
210 /* Given the sizeof(buf), and all of the current format buffers, \
211 * it is impossible for name_len to be < 0 if len >= sizeof(buf). */ \
212 int name_len = strlen(name) - (len - sizeof(buf)) - 1; \
213 /* Truncate the name to make the message fit. */ \
214 ALOGW("Truncated name in %s: %s\n", __FUNCTION__, name); \
215 len = snprintf(buf, sizeof(buf), format_begin "%.*s" format_end, pid, \
216 name_len, name, value); \
217 } \
218 write(atrace_marker_fd, buf, len); \
219}
Chih-Hung Hsieh2d3150e2014-10-13 16:30:24 -0700220
221void atrace_async_begin_body(const char* name, int32_t cookie)
222{
Christopher Ferris82d84892016-02-23 18:02:20 -0800223 WRITE_MSG("S|%d|", "|%" PRId32, getpid(), name, cookie);
Chih-Hung Hsieh2d3150e2014-10-13 16:30:24 -0700224}
225
226void atrace_async_end_body(const char* name, int32_t cookie)
227{
Christopher Ferris82d84892016-02-23 18:02:20 -0800228 WRITE_MSG("F|%d|", "|%" PRId32, getpid(), name, cookie);
Chih-Hung Hsieh2d3150e2014-10-13 16:30:24 -0700229}
230
231void atrace_int_body(const char* name, int32_t value)
232{
Christopher Ferris82d84892016-02-23 18:02:20 -0800233 WRITE_MSG("C|%d|", "|%" PRId32, getpid(), name, value);
Chih-Hung Hsieh2d3150e2014-10-13 16:30:24 -0700234}
235
236void atrace_int64_body(const char* name, int64_t value)
237{
Christopher Ferris82d84892016-02-23 18:02:20 -0800238 WRITE_MSG("C|%d|", "|%" PRId64, getpid(), name, value);
Chih-Hung Hsieh2d3150e2014-10-13 16:30:24 -0700239}