trace-container.cpp 7.7 KB

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