blob: fc7a870f7c8eaec107466ac5a918b36452f3658c [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>
Earl Oue4030382016-11-22 17:04:44 +080035
36/**
37 * Maximum size of a message that can be logged to the trace buffer.
38 * Note this message includes a tag, the pid, and the string given as the name.
39 * Names should be kept short to get the most use of the trace buffer.
40 */
41#define ATRACE_MESSAGE_LENGTH 1024
42
43atomic_bool atrace_is_ready = ATOMIC_VAR_INIT(false);
44int atrace_marker_fd = -1;
45uint64_t atrace_enabled_tags = ATRACE_TAG_NOT_READY;
46static bool atrace_is_debuggable = false;
47static atomic_bool atrace_is_enabled = ATOMIC_VAR_INIT(true);
48static pthread_mutex_t atrace_tags_mutex = PTHREAD_MUTEX_INITIALIZER;
49
50// Set whether this process is debuggable, which determines whether
51// application-level tracing is allowed when the ro.debuggable system property
52// is not set to '1'.
53void atrace_set_debuggable(bool debuggable)
54{
55 atrace_is_debuggable = debuggable;
56 atrace_update_tags();
57}
58
59// Check whether the given command line matches one of the comma-separated
60// values listed in the app_cmdlines property.
61static bool atrace_is_cmdline_match(const char* cmdline)
62{
63 int count = property_get_int32("debug.atrace.app_number", 0);
64
65 char buf[PROPERTY_KEY_MAX];
66 char value[PROPERTY_VALUE_MAX];
67
68 for (int i = 0; i < count; i++) {
69 snprintf(buf, sizeof(buf), "debug.atrace.app_%d", i);
70 property_get(buf, value, "");
Daniel Colascione642ef982018-02-09 20:07:23 -080071 if (strcmp(value, "*") == 0 || strcmp(value, cmdline) == 0) {
Earl Oue4030382016-11-22 17:04:44 +080072 return true;
73 }
74 }
75
76 return false;
77}
78
79// Determine whether application-level tracing is enabled for this process.
80static bool atrace_is_app_tracing_enabled()
81{
Tom Cherry15800942019-02-20 12:51:33 -080082 bool sys_debuggable = property_get_bool("ro.debuggable", 0);
Earl Oue4030382016-11-22 17:04:44 +080083 bool result = false;
84
85 if (sys_debuggable || atrace_is_debuggable) {
86 // Check whether tracing is enabled for this process.
87 FILE * file = fopen("/proc/self/cmdline", "re");
88 if (file) {
89 char cmdline[4096];
90 if (fgets(cmdline, sizeof(cmdline), file)) {
91 result = atrace_is_cmdline_match(cmdline);
92 } else {
93 ALOGE("Error reading cmdline: %s (%d)", strerror(errno), errno);
94 }
95 fclose(file);
96 } else {
97 ALOGE("Error opening /proc/self/cmdline: %s (%d)", strerror(errno),
98 errno);
99 }
100 }
101
102 return result;
103}
104
105// Read the sysprop and return the value tags should be set to
106static uint64_t atrace_get_property()
107{
108 char value[PROPERTY_VALUE_MAX];
109 char *endptr;
110 uint64_t tags;
111
112 property_get("debug.atrace.tags.enableflags", value, "0");
113 errno = 0;
114 tags = strtoull(value, &endptr, 0);
115 if (value[0] == '\0' || *endptr != '\0') {
116 ALOGE("Error parsing trace property: Not a number: %s", value);
117 return 0;
118 } else if (errno == ERANGE || tags == ULLONG_MAX) {
119 ALOGE("Error parsing trace property: Number too large: %s", value);
120 return 0;
121 }
122
123 // Only set the "app" tag if this process was selected for app-level debug
124 // tracing.
125 if (atrace_is_app_tracing_enabled()) {
126 tags |= ATRACE_TAG_APP;
127 } else {
128 tags &= ~ATRACE_TAG_APP;
129 }
130
131 return (tags | ATRACE_TAG_ALWAYS) & ATRACE_TAG_VALID_MASK;
132}
133
134// Update tags if tracing is ready. Useful as a sysprop change callback.
135void atrace_update_tags()
136{
137 uint64_t tags;
138 if (CC_UNLIKELY(atomic_load_explicit(&atrace_is_ready, memory_order_acquire))) {
139 if (atomic_load_explicit(&atrace_is_enabled, memory_order_acquire)) {
140 tags = atrace_get_property();
141 pthread_mutex_lock(&atrace_tags_mutex);
142 atrace_enabled_tags = tags;
143 pthread_mutex_unlock(&atrace_tags_mutex);
144 } else {
145 // Tracing is disabled for this process, so we simply don't
146 // initialize the tags.
147 pthread_mutex_lock(&atrace_tags_mutex);
148 atrace_enabled_tags = ATRACE_TAG_NOT_READY;
149 pthread_mutex_unlock(&atrace_tags_mutex);
150 }
151 }
152}
153
154#define WRITE_MSG(format_begin, format_end, name, value) { \
155 char buf[ATRACE_MESSAGE_LENGTH]; \
156 int pid = getpid(); \
157 int len = snprintf(buf, sizeof(buf), format_begin "%s" format_end, pid, \
158 name, value); \
159 if (len >= (int) sizeof(buf)) { \
160 /* Given the sizeof(buf), and all of the current format buffers, \
161 * it is impossible for name_len to be < 0 if len >= sizeof(buf). */ \
162 int name_len = strlen(name) - (len - sizeof(buf)) - 1; \
163 /* Truncate the name to make the message fit. */ \
164 ALOGW("Truncated name in %s: %s\n", __FUNCTION__, name); \
165 len = snprintf(buf, sizeof(buf), format_begin "%.*s" format_end, pid, \
166 name_len, name, value); \
167 } \
168 write(atrace_marker_fd, buf, len); \
169}
170
171#endif // __TRACE_DEV_INC