blob: c23d5e2bd813360197741a99e75738675fd34833 [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
Elliott Hughes8e9aeb92017-11-10 10:22:07 -080017#include <cutils/trace.h>
18
Earl Oue4030382016-11-22 17:04:44 +080019#include "trace-dev.inc"
20
21#include <cutils/sockets.h>
22#include <sys/stat.h>
23#include <time.h>
24
25/**
26 * For tracing in container, tags are written into a socket
27 * instead of ftrace. Additional data is appended so we need extra space.
28 */
29#define CONTAINER_ATRACE_MESSAGE_LENGTH (ATRACE_MESSAGE_LENGTH + 512)
30
31static pthread_once_t atrace_once_control = PTHREAD_ONCE_INIT;
32
33// Variables used for tracing in container with socket.
34// Note that we need to manually close and reopen socket when Zygote is forking. This requires
35// writing and closing sockets on multiple threads. A rwlock is used for avoiding concurrent
36// operation on the file descriptor.
37static bool atrace_use_container_sock = false;
38static int atrace_container_sock_fd = -1;
39static pthread_mutex_t atrace_enabling_mutex = PTHREAD_MUTEX_INITIALIZER;
40static pthread_rwlock_t atrace_container_sock_rwlock = PTHREAD_RWLOCK_INITIALIZER;
41
Florian Mayerb06766c2019-12-10 12:20:03 +000042static void atrace_seq_number_changed(uint32_t, uint32_t seq_no) {
43 pthread_once(&atrace_once_control, atrace_init_once);
44 atomic_store_explicit(&last_sequence_number, seq_no, memory_order_relaxed);
45}
46
Earl Oue4030382016-11-22 17:04:44 +080047static bool atrace_init_container_sock()
48{
49 pthread_rwlock_wrlock(&atrace_container_sock_rwlock);
50 atrace_container_sock_fd =
51 socket_local_client("trace", ANDROID_SOCKET_NAMESPACE_RESERVED, SOCK_SEQPACKET);
52 if (atrace_container_sock_fd < 0) {
53 ALOGE("Error opening container trace socket: %s (%d)", strerror(errno), errno);
54 }
55 pthread_rwlock_unlock(&atrace_container_sock_rwlock);
56 return atrace_container_sock_fd != -1;
57}
58
59static void atrace_close_container_sock()
60{
61 pthread_rwlock_wrlock(&atrace_container_sock_rwlock);
62 if (atrace_container_sock_fd != -1) close(atrace_container_sock_fd);
63 atrace_container_sock_fd = -1;
64 pthread_rwlock_unlock(&atrace_container_sock_rwlock);
65}
66
67// Set whether tracing is enabled in this process. This is used to prevent
68// the Zygote process from tracing. We need to close the socket in the container when tracing is
69// disabled, and reopen it again after Zygote forking.
70void atrace_set_tracing_enabled(bool enabled)
71{
72 pthread_mutex_lock(&atrace_enabling_mutex);
73 if (atrace_use_container_sock) {
74 bool already_enabled = atomic_load_explicit(&atrace_is_enabled, memory_order_acquire);
75 if (enabled && !already_enabled) {
76 // Trace was disabled previously. Re-initialize container socket.
77 atrace_init_container_sock();
78 } else if (!enabled && already_enabled) {
79 // Trace was enabled previously. Close container socket.
80 atrace_close_container_sock();
81 }
82 }
83 atomic_store_explicit(&atrace_is_enabled, enabled, memory_order_release);
84 pthread_mutex_unlock(&atrace_enabling_mutex);
85 atrace_update_tags();
86}
87
88static void atrace_init_once()
89{
90 atrace_marker_fd = open("/sys/kernel/debug/tracing/trace_marker", O_WRONLY | O_CLOEXEC);
91 if (atrace_marker_fd < 0) {
92 // We're in container, ftrace may be disabled. In such case, we use the
93 // socket to write trace event.
94
95 // Protect the initialization of container socket from
96 // atrace_set_tracing_enabled.
97 pthread_mutex_lock(&atrace_enabling_mutex);
98 atrace_use_container_sock = true;
99 bool success = false;
100 if (atomic_load_explicit(&atrace_is_enabled, memory_order_acquire)) {
101 success = atrace_init_container_sock();
102 }
103 pthread_mutex_unlock(&atrace_enabling_mutex);
104
105 if (!success) {
106 atrace_enabled_tags = 0;
107 goto done;
108 }
109 }
110 atrace_enabled_tags = atrace_get_property();
111
112done:
113 atomic_store_explicit(&atrace_is_ready, true, memory_order_release);
114}
115
116void atrace_setup()
117{
118 pthread_once(&atrace_once_control, atrace_init_once);
119}
120
121static inline uint64_t gettime(clockid_t clk_id)
122{
123 struct timespec ts;
124 clock_gettime(clk_id, &ts);
125 return ts.tv_sec * 1000000 + ts.tv_nsec / 1000;
126}
127
128// Write trace events to container trace file. Note that we need to amend tid and time information
129// here comparing to normal ftrace, where those informations are added by kernel.
130#define WRITE_MSG_IN_CONTAINER_LOCKED(ph, sep_before_name, value_format, name, value) { \
131 char buf[CONTAINER_ATRACE_MESSAGE_LENGTH]; \
132 int pid = getpid(); \
133 int tid = gettid(); \
134 uint64_t ts = gettime(CLOCK_MONOTONIC); \
135 uint64_t tts = gettime(CLOCK_THREAD_CPUTIME_ID); \
136 int len = snprintf( \
137 buf, sizeof(buf), \
138 ph "|%d|%d|%" PRIu64 "|%" PRIu64 sep_before_name "%s" value_format, \
139 pid, tid, ts, tts, name, value); \
140 if (len >= (int) sizeof(buf)) { \
141 int name_len = strlen(name) - (len - sizeof(buf)) - 1; \
142 /* Truncate the name to make the message fit. */ \
143 if (name_len > 0) { \
144 ALOGW("Truncated name in %s: %s\n", __FUNCTION__, name); \
145 len = snprintf( \
146 buf, sizeof(buf), \
147 ph "|%d|%d|%" PRIu64 "|%" PRIu64 sep_before_name "%.*s" value_format, \
148 pid, tid, ts, tts, name_len, name, value); \
149 } else { \
150 /* Data is still too long. Drop it. */ \
151 ALOGW("Data is too long in %s: %s\n", __FUNCTION__, name); \
152 len = 0; \
153 } \
154 } \
155 if (len > 0) { \
156 write(atrace_container_sock_fd, buf, len); \
157 } \
158}
159
160#define WRITE_MSG_IN_CONTAINER(ph, sep_before_name, value_format, name, value) { \
161 pthread_rwlock_rdlock(&atrace_container_sock_rwlock); \
162 if (atrace_container_sock_fd != -1) { \
163 WRITE_MSG_IN_CONTAINER_LOCKED(ph, sep_before_name, value_format, name, value); \
164 } \
165 pthread_rwlock_unlock(&atrace_container_sock_rwlock); \
166}
167
168void atrace_begin_body(const char* name)
169{
170 if (CC_LIKELY(atrace_use_container_sock)) {
171 WRITE_MSG_IN_CONTAINER("B", "|", "%s", name, "");
172 return;
173 }
174
175 if (atrace_marker_fd < 0) return;
176
177 WRITE_MSG("B|%d|", "%s", name, "");
178}
179
180void atrace_end_body()
181{
182 if (CC_LIKELY(atrace_use_container_sock)) {
183 WRITE_MSG_IN_CONTAINER("E", "", "%s", "", "");
184 return;
185 }
186
187 if (atrace_marker_fd < 0) return;
188
189 WRITE_MSG("E|%d", "%s", "", "");
190}
191
192void atrace_async_begin_body(const char* name, int32_t cookie)
193{
194 if (CC_LIKELY(atrace_use_container_sock)) {
195 WRITE_MSG_IN_CONTAINER("S", "|", "|%d", name, cookie);
196 return;
197 }
198
199 if (atrace_marker_fd < 0) return;
200
201 WRITE_MSG("S|%d|", "|%" PRId32, name, cookie);
202}
203
204void atrace_async_end_body(const char* name, int32_t cookie)
205{
206 if (CC_LIKELY(atrace_use_container_sock)) {
207 WRITE_MSG_IN_CONTAINER("F", "|", "|%d", name, cookie);
208 return;
209 }
210
211 if (atrace_marker_fd < 0) return;
212
213 WRITE_MSG("F|%d|", "|%" PRId32, name, cookie);
214}
215
216void atrace_int_body(const char* name, int32_t value)
217{
218 if (CC_LIKELY(atrace_use_container_sock)) {
219 WRITE_MSG_IN_CONTAINER("C", "|", "|%" PRId32, name, value);
220 return;
221 }
222
223 if (atrace_marker_fd < 0) return;
224
225 WRITE_MSG("C|%d|", "|%" PRId32, name, value);
226}
227
228void atrace_int64_body(const char* name, int64_t value)
229{
230 if (CC_LIKELY(atrace_use_container_sock)) {
231 WRITE_MSG_IN_CONTAINER("C", "|", "|%" PRId64, name, value);
232 return;
233 }
234
235 if (atrace_marker_fd < 0) return;
236
237 WRITE_MSG("C|%d|", "|%" PRId64, name, value);
238}