blob: f3fdda40ee25c303880139da3699d321afa40137 [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{
Hridya Valsaraju5cdb5d42020-02-07 10:43:12 -080090 atrace_marker_fd = open("/sys/kernel/tracing/trace_marker", O_WRONLY | O_CLOEXEC);
Earl Oue4030382016-11-22 17:04:44 +080091 if (atrace_marker_fd < 0) {
Hridya Valsaraju5cdb5d42020-02-07 10:43:12 -080092 // try debugfs
93 atrace_marker_fd = open("/sys/kernel/debug/tracing/trace_marker", O_WRONLY | O_CLOEXEC);
94 if (atrace_marker_fd < 0) {
95 // We're in container, ftrace may be disabled. In such case, we use the
96 // socket to write trace event.
Earl Oue4030382016-11-22 17:04:44 +080097
Hridya Valsaraju5cdb5d42020-02-07 10:43:12 -080098 // Protect the initialization of container socket from
99 // atrace_set_tracing_enabled.
100 pthread_mutex_lock(&atrace_enabling_mutex);
101 atrace_use_container_sock = true;
102 bool success = false;
103 if (atomic_load_explicit(&atrace_is_enabled, memory_order_acquire)) {
104 success = atrace_init_container_sock();
105 }
106 pthread_mutex_unlock(&atrace_enabling_mutex);
Earl Oue4030382016-11-22 17:04:44 +0800107
Hridya Valsaraju5cdb5d42020-02-07 10:43:12 -0800108 if (!success) {
109 atrace_enabled_tags = 0;
110 goto done;
111 }
Earl Oue4030382016-11-22 17:04:44 +0800112 }
113 }
114 atrace_enabled_tags = atrace_get_property();
115
116done:
117 atomic_store_explicit(&atrace_is_ready, true, memory_order_release);
118}
119
120void atrace_setup()
121{
122 pthread_once(&atrace_once_control, atrace_init_once);
123}
124
125static inline uint64_t gettime(clockid_t clk_id)
126{
127 struct timespec ts;
128 clock_gettime(clk_id, &ts);
129 return ts.tv_sec * 1000000 + ts.tv_nsec / 1000;
130}
131
132// Write trace events to container trace file. Note that we need to amend tid and time information
133// here comparing to normal ftrace, where those informations are added by kernel.
Ray Ye9a542402022-03-15 23:28:01 +0000134#define WRITE_MSG_IN_CONTAINER_LOCKED(ph, sep_before_name, value_format, \
135 track_name, name, value) { \
Earl Oue4030382016-11-22 17:04:44 +0800136 char buf[CONTAINER_ATRACE_MESSAGE_LENGTH]; \
Ray Ye9a542402022-03-15 23:28:01 +0000137 const char* track_name_sep = track_name[0] != '\0' ? "|" : ""; \
Earl Oue4030382016-11-22 17:04:44 +0800138 int pid = getpid(); \
139 int tid = gettid(); \
140 uint64_t ts = gettime(CLOCK_MONOTONIC); \
141 uint64_t tts = gettime(CLOCK_THREAD_CPUTIME_ID); \
142 int len = snprintf( \
143 buf, sizeof(buf), \
Ray Ye9a542402022-03-15 23:28:01 +0000144 ph "|%d|%d|%" PRIu64 "|%" PRIu64 sep_before_name "%s%s%s" value_format, \
145 pid, tid, ts, tts, track_name, track_name_sep, name, value); \
Earl Oue4030382016-11-22 17:04:44 +0800146 if (len >= (int) sizeof(buf)) { \
147 int name_len = strlen(name) - (len - sizeof(buf)) - 1; \
148 /* Truncate the name to make the message fit. */ \
149 if (name_len > 0) { \
Earl Oue4030382016-11-22 17:04:44 +0800150 len = snprintf( \
Ray Ye9a542402022-03-15 23:28:01 +0000151 buf, sizeof(buf), \
152 ph "|%d|%d|%" PRIu64 "|%" PRIu64 sep_before_name "%s%s%.*s" value_format, \
153 pid, tid, ts, tts, track_name, track_name_sep, name_len, name, value); \
Earl Oue4030382016-11-22 17:04:44 +0800154 } else { \
Ray Ye9a542402022-03-15 23:28:01 +0000155 int track_name_len = 0; \
156 if (track_name[0] != '\0') { \
157 track_name_len = strlen(track_name) - (len - strlen(name) - sizeof(buf)) - 2; \
158 } \
159 if (track_name_len <= 0){ \
160 /* Data is still too long. Drop it. */ \
161 len = 0; \
162 } else { \
163 /* Truncate the trackName and name to make the message fit. */ \
164 len = snprintf( \
165 buf, sizeof(buf), \
166 ph "|%d|%d|%" PRIu64 "|%" PRIu64 sep_before_name "%.*s|%.1s" value_format, \
167 pid, tid, ts, tts, track_name_len, track_name, name, value); \
168 } \
Earl Oue4030382016-11-22 17:04:44 +0800169 } \
170 } \
171 if (len > 0) { \
172 write(atrace_container_sock_fd, buf, len); \
173 } \
174}
175
Ray Ye9a542402022-03-15 23:28:01 +0000176#define WRITE_MSG_IN_CONTAINER(ph, sep_before_name, value_format, track_name, name, value) { \
Earl Oue4030382016-11-22 17:04:44 +0800177 pthread_rwlock_rdlock(&atrace_container_sock_rwlock); \
178 if (atrace_container_sock_fd != -1) { \
Ray Ye9a542402022-03-15 23:28:01 +0000179 WRITE_MSG_IN_CONTAINER_LOCKED(ph, sep_before_name, value_format, track_name, name, value); \
Earl Oue4030382016-11-22 17:04:44 +0800180 } \
181 pthread_rwlock_unlock(&atrace_container_sock_rwlock); \
182}
183
184void atrace_begin_body(const char* name)
185{
186 if (CC_LIKELY(atrace_use_container_sock)) {
Ray Ye9a542402022-03-15 23:28:01 +0000187 WRITE_MSG_IN_CONTAINER("B", "|", "%s", "", name, "");
Earl Oue4030382016-11-22 17:04:44 +0800188 return;
189 }
190
191 if (atrace_marker_fd < 0) return;
192
Ray Ye9a542402022-03-15 23:28:01 +0000193 WRITE_MSG("B|%d|", "%s", "", name, "");
Earl Oue4030382016-11-22 17:04:44 +0800194}
195
196void atrace_end_body()
197{
198 if (CC_LIKELY(atrace_use_container_sock)) {
Ray Ye9a542402022-03-15 23:28:01 +0000199 WRITE_MSG_IN_CONTAINER("E", "", "%s", "", "", "");
Earl Oue4030382016-11-22 17:04:44 +0800200 return;
201 }
202
203 if (atrace_marker_fd < 0) return;
204
Ray Ye9a542402022-03-15 23:28:01 +0000205 WRITE_MSG("E|%d", "%s", "", "", "");
Earl Oue4030382016-11-22 17:04:44 +0800206}
207
208void atrace_async_begin_body(const char* name, int32_t cookie)
209{
210 if (CC_LIKELY(atrace_use_container_sock)) {
Ray Ye9a542402022-03-15 23:28:01 +0000211 WRITE_MSG_IN_CONTAINER("S", "|", "|%d", "", name, cookie);
Earl Oue4030382016-11-22 17:04:44 +0800212 return;
213 }
214
215 if (atrace_marker_fd < 0) return;
216
Ray Ye9a542402022-03-15 23:28:01 +0000217 WRITE_MSG("S|%d|", "|%" PRId32, "", name, cookie);
Earl Oue4030382016-11-22 17:04:44 +0800218}
219
220void atrace_async_end_body(const char* name, int32_t cookie)
221{
222 if (CC_LIKELY(atrace_use_container_sock)) {
Ray Ye9a542402022-03-15 23:28:01 +0000223 WRITE_MSG_IN_CONTAINER("F", "|", "|%d", "", name, cookie);
Earl Oue4030382016-11-22 17:04:44 +0800224 return;
225 }
226
227 if (atrace_marker_fd < 0) return;
228
Ray Ye9a542402022-03-15 23:28:01 +0000229 WRITE_MSG("F|%d|", "|%" PRId32, "", name, cookie);
Earl Oue4030382016-11-22 17:04:44 +0800230}
231
Lucas Dupin2c2c5d92021-12-08 16:37:22 -0800232void atrace_instant_body(const char* name) {
233 if (CC_LIKELY(atrace_use_container_sock)) {
Ray Ye9a542402022-03-15 23:28:01 +0000234 WRITE_MSG_IN_CONTAINER("I", "|", "%s", "", name, "");
Lucas Dupin2c2c5d92021-12-08 16:37:22 -0800235 return;
236 }
237
238 if (atrace_marker_fd < 0) return;
239
Ray Ye9a542402022-03-15 23:28:01 +0000240 WRITE_MSG("I|%d|", "%s", "", name, "");
Lucas Dupin2c2c5d92021-12-08 16:37:22 -0800241}
242
Ray Ye9a542402022-03-15 23:28:01 +0000243void atrace_instant_for_track_body(const char* track_name, const char* name) {
Lucas Dupin2c2c5d92021-12-08 16:37:22 -0800244 if (CC_LIKELY(atrace_use_container_sock)) {
Ray Ye9a542402022-03-15 23:28:01 +0000245 WRITE_MSG_IN_CONTAINER("N", "|", "%s", track_name, name, "");
Lucas Dupin2c2c5d92021-12-08 16:37:22 -0800246 return;
247 }
248
249 if (atrace_marker_fd < 0) return;
250
Ray Ye9a542402022-03-15 23:28:01 +0000251 WRITE_MSG("N|%d|", "%s", track_name, name, "");
Lucas Dupin2c2c5d92021-12-08 16:37:22 -0800252}
253
Earl Oue4030382016-11-22 17:04:44 +0800254void atrace_int_body(const char* name, int32_t value)
255{
256 if (CC_LIKELY(atrace_use_container_sock)) {
Ray Ye9a542402022-03-15 23:28:01 +0000257 WRITE_MSG_IN_CONTAINER("C", "|", "|%" PRId32, "", name, value);
Earl Oue4030382016-11-22 17:04:44 +0800258 return;
259 }
260
261 if (atrace_marker_fd < 0) return;
262
Ray Ye9a542402022-03-15 23:28:01 +0000263 WRITE_MSG("C|%d|", "|%" PRId32, "", name, value);
Earl Oue4030382016-11-22 17:04:44 +0800264}
265
266void atrace_int64_body(const char* name, int64_t value)
267{
268 if (CC_LIKELY(atrace_use_container_sock)) {
Ray Ye9a542402022-03-15 23:28:01 +0000269 WRITE_MSG_IN_CONTAINER("C", "|", "|%" PRId64, "", name, value);
Earl Oue4030382016-11-22 17:04:44 +0800270 return;
271 }
272
273 if (atrace_marker_fd < 0) return;
274
Ray Ye9a542402022-03-15 23:28:01 +0000275 WRITE_MSG("C|%d|", "|%" PRId64, "", name, value);
Earl Oue4030382016-11-22 17:04:44 +0800276}