blob: 047f88941722406c850b7b29e16a3d093f0e75c8 [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 Gennis774f9292013-02-25 18:15:40 -080033int32_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 pthread_once_t atrace_once_control = PTHREAD_ONCE_INIT;
38static pthread_mutex_t atrace_tags_mutex = PTHREAD_MUTEX_INITIALIZER;
39
40// Set whether this process is debuggable, which determines whether
41// application-level tracing is allowed when the ro.debuggable system property
42// is not set to '1'.
43void atrace_set_debuggable(bool debuggable)
44{
45 atrace_is_debuggable = debuggable;
46 atrace_update_tags();
47}
48
49// Check whether the given command line matches one of the comma-separated
50// values listed in the app_cmdlines property.
51static bool atrace_is_cmdline_match(const char* cmdline) {
52 char value[PROPERTY_VALUE_MAX];
53 char* start = value;
54
55 property_get("debug.atrace.app_cmdlines", value, "");
56
57 while (start != NULL) {
58 char* end = strchr(start, ',');
59
60 if (end != NULL) {
61 *end = '\0';
62 end++;
63 }
64
65 if (strcmp(cmdline, start) == 0) {
66 return true;
67 }
68
69 start = end;
70 }
71
72 return false;
73}
74
75// Determine whether application-level tracing is enabled for this process.
76static bool atrace_is_app_tracing_enabled()
77{
78 bool sys_debuggable = false;
79 bool proc_debuggable = false;
80 char value[PROPERTY_VALUE_MAX];
81 bool result = false;
82
83 // Check whether the system is debuggable.
84 property_get("ro.debuggable", value, "0");
85 if (value[0] == '1') {
86 sys_debuggable = true;
87 }
88
89 if (sys_debuggable || atrace_is_debuggable) {
90 // Check whether tracing is enabled for this process.
91 FILE * file = fopen("/proc/self/cmdline", "r");
92 if (file) {
93 char cmdline[4096];
94 if (fgets(cmdline, sizeof(cmdline), file)) {
95 result = atrace_is_cmdline_match(cmdline);
96 } else {
97 ALOGE("Error reading cmdline: %s (%d)", strerror(errno), errno);
98 }
99 fclose(file);
100 } else {
101 ALOGE("Error opening /proc/self/cmdline: %s (%d)", strerror(errno),
102 errno);
103 }
104 }
105
106 return result;
107}
Alex Ray0a346432012-11-14 17:25:28 -0800108
109// Read the sysprop and return the value tags should be set to
110static uint64_t atrace_get_property()
111{
112 char value[PROPERTY_VALUE_MAX];
113 char *endptr;
114 uint64_t tags;
115
116 property_get("debug.atrace.tags.enableflags", value, "0");
117 errno = 0;
118 tags = strtoull(value, &endptr, 0);
119 if (value[0] == '\0' || *endptr != '\0') {
120 ALOGE("Error parsing trace property: Not a number: %s", value);
121 return 0;
122 } else if (errno == ERANGE || tags == ULLONG_MAX) {
123 ALOGE("Error parsing trace property: Number too large: %s", value);
124 return 0;
125 }
Jamie Gennis774f9292013-02-25 18:15:40 -0800126
127 // Only set the "app" tag if this process was selected for app-level debug
128 // tracing.
129 if (atrace_is_app_tracing_enabled()) {
130 tags |= ATRACE_TAG_APP;
131 } else {
132 tags &= ~ATRACE_TAG_APP;
133 }
134
Alex Ray0a346432012-11-14 17:25:28 -0800135 return (tags | ATRACE_TAG_ALWAYS) & ATRACE_TAG_VALID_MASK;
136}
137
Alex Raye7bb7bc2012-11-20 01:39:09 -0800138// Update tags if tracing is ready. Useful as a sysprop change callback.
139void atrace_update_tags()
140{
141 uint64_t tags;
142 if (CC_UNLIKELY(android_atomic_acquire_load(&atrace_is_ready))) {
143 tags = atrace_get_property();
144 pthread_mutex_lock(&atrace_tags_mutex);
145 atrace_enabled_tags = tags;
146 pthread_mutex_unlock(&atrace_tags_mutex);
147 }
148}
149
Alex Ray0a346432012-11-14 17:25:28 -0800150static void atrace_init_once()
151{
152 atrace_marker_fd = open("/sys/kernel/debug/tracing/trace_marker", O_WRONLY);
153 if (atrace_marker_fd == -1) {
154 ALOGE("Error opening trace file: %s (%d)", strerror(errno), errno);
155 atrace_enabled_tags = 0;
156 goto done;
157 }
158
159 atrace_enabled_tags = atrace_get_property();
160
161done:
162 android_atomic_release_store(1, &atrace_is_ready);
163}
164
165void atrace_setup()
166{
167 pthread_once(&atrace_once_control, atrace_init_once);
168}