blob: 9754a44faf4579e78d83d9ec3d4d13a50724088b [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>
Jamie Gennis774f9292013-02-25 18:15:40 -080021#include <stdbool.h>
Alex Ray0a346432012-11-14 17:25:28 -080022#include <stdlib.h>
23#include <string.h>
24#include <sys/types.h>
25#include <cutils/atomic.h>
26#include <cutils/compiler.h>
27#include <cutils/properties.h>
28#include <cutils/trace.h>
29
30#define LOG_TAG "cutils-trace"
31#include <cutils/log.h>
32
Jamie Gennisb13ea452013-04-15 18:50:22 -070033volatile int32_t atrace_is_ready = 0;
34int atrace_marker_fd = -1;
35uint64_t atrace_enabled_tags = ATRACE_TAG_NOT_READY;
36static bool atrace_is_debuggable = false;
37static volatile int32_t atrace_is_enabled = 1;
38static pthread_once_t atrace_once_control = PTHREAD_ONCE_INIT;
39static pthread_mutex_t atrace_tags_mutex = PTHREAD_MUTEX_INITIALIZER;
Jamie Gennis774f9292013-02-25 18:15:40 -080040
41// Set whether this process is debuggable, which determines whether
42// application-level tracing is allowed when the ro.debuggable system property
43// is not set to '1'.
44void atrace_set_debuggable(bool debuggable)
45{
46 atrace_is_debuggable = debuggable;
47 atrace_update_tags();
48}
49
Jamie Gennisb13ea452013-04-15 18:50:22 -070050// Set whether tracing is enabled in this process. This is used to prevent
51// the Zygote process from tracing.
52void atrace_set_tracing_enabled(bool enabled)
53{
54 android_atomic_release_store(enabled ? 1 : 0, &atrace_is_enabled);
55 atrace_update_tags();
56}
57
Jamie Gennis774f9292013-02-25 18:15:40 -080058// Check whether the given command line matches one of the comma-separated
59// values listed in the app_cmdlines property.
Jamie Gennisb13ea452013-04-15 18:50:22 -070060static bool atrace_is_cmdline_match(const char* cmdline)
61{
Jamie Gennis774f9292013-02-25 18:15:40 -080062 char value[PROPERTY_VALUE_MAX];
63 char* start = value;
64
65 property_get("debug.atrace.app_cmdlines", value, "");
66
67 while (start != NULL) {
68 char* end = strchr(start, ',');
69
70 if (end != NULL) {
71 *end = '\0';
72 end++;
73 }
74
75 if (strcmp(cmdline, start) == 0) {
76 return true;
77 }
78
79 start = end;
80 }
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;
89 bool proc_debuggable = false;
90 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.
101 FILE * file = fopen("/proc/self/cmdline", "r");
102 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;
152 if (CC_UNLIKELY(android_atomic_acquire_load(&atrace_is_ready))) {
Jamie Gennisb13ea452013-04-15 18:50:22 -0700153 if (android_atomic_acquire_load(&atrace_is_enabled)) {
154 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{
170 atrace_marker_fd = open("/sys/kernel/debug/tracing/trace_marker", O_WRONLY);
171 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:
180 android_atomic_release_store(1, &atrace_is_ready);
181}
182
183void atrace_setup()
184{
185 pthread_once(&atrace_once_control, atrace_init_once);
186}