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