trace-dev.inc 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. #ifndef __TRACE_DEV_INC
  17. #define __TRACE_DEV_INC
  18. #define LOG_TAG "cutils-trace"
  19. #include <errno.h>
  20. #include <fcntl.h>
  21. #include <limits.h>
  22. #include <pthread.h>
  23. #include <stdatomic.h>
  24. #include <stdlib.h>
  25. #include <string.h>
  26. #include <sys/types.h>
  27. #include <cutils/compiler.h>
  28. #include <cutils/properties.h>
  29. #include <cutils/trace.h>
  30. #include <log/log.h>
  31. #include <log/log_properties.h>
  32. /**
  33. * Maximum size of a message that can be logged to the trace buffer.
  34. * Note this message includes a tag, the pid, and the string given as the name.
  35. * Names should be kept short to get the most use of the trace buffer.
  36. */
  37. #define ATRACE_MESSAGE_LENGTH 1024
  38. atomic_bool atrace_is_ready = ATOMIC_VAR_INIT(false);
  39. int atrace_marker_fd = -1;
  40. uint64_t atrace_enabled_tags = ATRACE_TAG_NOT_READY;
  41. static bool atrace_is_debuggable = false;
  42. static atomic_bool atrace_is_enabled = ATOMIC_VAR_INIT(true);
  43. static pthread_mutex_t atrace_tags_mutex = PTHREAD_MUTEX_INITIALIZER;
  44. // Set whether this process is debuggable, which determines whether
  45. // application-level tracing is allowed when the ro.debuggable system property
  46. // is not set to '1'.
  47. void atrace_set_debuggable(bool debuggable)
  48. {
  49. atrace_is_debuggable = debuggable;
  50. atrace_update_tags();
  51. }
  52. // Check whether the given command line matches one of the comma-separated
  53. // values listed in the app_cmdlines property.
  54. static bool atrace_is_cmdline_match(const char* cmdline)
  55. {
  56. int count = property_get_int32("debug.atrace.app_number", 0);
  57. char buf[PROPERTY_KEY_MAX];
  58. char value[PROPERTY_VALUE_MAX];
  59. for (int i = 0; i < count; i++) {
  60. snprintf(buf, sizeof(buf), "debug.atrace.app_%d", i);
  61. property_get(buf, value, "");
  62. if (strcmp(value, "*") == 0 || strcmp(value, cmdline) == 0) {
  63. return true;
  64. }
  65. }
  66. return false;
  67. }
  68. // Determine whether application-level tracing is enabled for this process.
  69. static bool atrace_is_app_tracing_enabled()
  70. {
  71. bool sys_debuggable = property_get_bool("ro.debuggable", 0);
  72. bool result = false;
  73. if (sys_debuggable || atrace_is_debuggable) {
  74. // Check whether tracing is enabled for this process.
  75. FILE * file = fopen("/proc/self/cmdline", "re");
  76. if (file) {
  77. char cmdline[4096];
  78. if (fgets(cmdline, sizeof(cmdline), file)) {
  79. result = atrace_is_cmdline_match(cmdline);
  80. } else {
  81. ALOGE("Error reading cmdline: %s (%d)", strerror(errno), errno);
  82. }
  83. fclose(file);
  84. } else {
  85. ALOGE("Error opening /proc/self/cmdline: %s (%d)", strerror(errno),
  86. errno);
  87. }
  88. }
  89. return result;
  90. }
  91. // Read the sysprop and return the value tags should be set to
  92. static uint64_t atrace_get_property()
  93. {
  94. char value[PROPERTY_VALUE_MAX];
  95. char *endptr;
  96. uint64_t tags;
  97. property_get("debug.atrace.tags.enableflags", value, "0");
  98. errno = 0;
  99. tags = strtoull(value, &endptr, 0);
  100. if (value[0] == '\0' || *endptr != '\0') {
  101. ALOGE("Error parsing trace property: Not a number: %s", value);
  102. return 0;
  103. } else if (errno == ERANGE || tags == ULLONG_MAX) {
  104. ALOGE("Error parsing trace property: Number too large: %s", value);
  105. return 0;
  106. }
  107. // Only set the "app" tag if this process was selected for app-level debug
  108. // tracing.
  109. if (atrace_is_app_tracing_enabled()) {
  110. tags |= ATRACE_TAG_APP;
  111. } else {
  112. tags &= ~ATRACE_TAG_APP;
  113. }
  114. return (tags | ATRACE_TAG_ALWAYS) & ATRACE_TAG_VALID_MASK;
  115. }
  116. // Update tags if tracing is ready. Useful as a sysprop change callback.
  117. void atrace_update_tags()
  118. {
  119. uint64_t tags;
  120. if (CC_UNLIKELY(atomic_load_explicit(&atrace_is_ready, memory_order_acquire))) {
  121. if (atomic_load_explicit(&atrace_is_enabled, memory_order_acquire)) {
  122. tags = atrace_get_property();
  123. pthread_mutex_lock(&atrace_tags_mutex);
  124. atrace_enabled_tags = tags;
  125. pthread_mutex_unlock(&atrace_tags_mutex);
  126. } else {
  127. // Tracing is disabled for this process, so we simply don't
  128. // initialize the tags.
  129. pthread_mutex_lock(&atrace_tags_mutex);
  130. atrace_enabled_tags = ATRACE_TAG_NOT_READY;
  131. pthread_mutex_unlock(&atrace_tags_mutex);
  132. }
  133. }
  134. }
  135. #define WRITE_MSG(format_begin, format_end, name, value) { \
  136. char buf[ATRACE_MESSAGE_LENGTH]; \
  137. int pid = getpid(); \
  138. int len = snprintf(buf, sizeof(buf), format_begin "%s" format_end, pid, \
  139. name, value); \
  140. if (len >= (int) sizeof(buf)) { \
  141. /* Given the sizeof(buf), and all of the current format buffers, \
  142. * it is impossible for name_len to be < 0 if len >= sizeof(buf). */ \
  143. int name_len = strlen(name) - (len - sizeof(buf)) - 1; \
  144. /* Truncate the name to make the message fit. */ \
  145. ALOGW("Truncated name in %s: %s\n", __FUNCTION__, name); \
  146. len = snprintf(buf, sizeof(buf), format_begin "%.*s" format_end, pid, \
  147. name_len, name, value); \
  148. } \
  149. write(atrace_marker_fd, buf, len); \
  150. }
  151. #endif // __TRACE_DEV_INC