Brigid Smith | a406ee6 | 2014-07-21 15:38:06 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 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 <cutils/trace.h> |
Elliott Hughes | 40360b3 | 2014-12-29 13:29:50 -0800 | [diff] [blame] | 18 | #include <errno.h> |
Brigid Smith | a406ee6 | 2014-07-21 15:38:06 -0700 | [diff] [blame] | 19 | #include <fcntl.h> |
| 20 | #include <stdio.h> |
| 21 | #include <stdlib.h> |
Elliott Hughes | 05fc1d7 | 2015-01-28 18:02:33 -0800 | [diff] [blame] | 22 | #include <string.h> |
Brigid Smith | a406ee6 | 2014-07-21 15:38:06 -0700 | [diff] [blame] | 23 | |
Yabin Cui | 1661125 | 2015-07-21 17:27:54 -0700 | [diff] [blame] | 24 | #include "private/bionic_lock.h" |
Brigid Smith | a406ee6 | 2014-07-21 15:38:06 -0700 | [diff] [blame] | 25 | #include "private/bionic_systrace.h" |
| 26 | #include "private/libc_logging.h" |
| 27 | |
| 28 | #define _REALLY_INCLUDE_SYS__SYSTEM_PROPERTIES_H_ |
| 29 | #include <sys/_system_properties.h> |
| 30 | |
| 31 | #define WRITE_OFFSET 32 |
| 32 | |
Yabin Cui | 1661125 | 2015-07-21 17:27:54 -0700 | [diff] [blame] | 33 | constexpr char SYSTRACE_PROPERTY_NAME[] = "debug.atrace.tags.enableflags"; |
| 34 | |
| 35 | static Lock g_lock; |
| 36 | static const prop_info* g_pinfo; |
Brigid Smith | a406ee6 | 2014-07-21 15:38:06 -0700 | [diff] [blame] | 37 | static uint32_t g_serial = -1; |
Yabin Cui | 1661125 | 2015-07-21 17:27:54 -0700 | [diff] [blame] | 38 | static uint64_t g_tags; |
Brigid Smith | a406ee6 | 2014-07-21 15:38:06 -0700 | [diff] [blame] | 39 | static int g_trace_marker_fd = -1; |
| 40 | |
| 41 | static bool should_trace() { |
Yabin Cui | 1661125 | 2015-07-21 17:27:54 -0700 | [diff] [blame] | 42 | bool result = false; |
| 43 | g_lock.lock(); |
Brigid Smith | a406ee6 | 2014-07-21 15:38:06 -0700 | [diff] [blame] | 44 | // If g_pinfo is null, this means that systrace hasn't been run and it's safe to |
| 45 | // assume that no trace writing will need to take place. However, to avoid running |
| 46 | // this costly find check each time, we set it to a non-tracing value so that next |
| 47 | // time, it will just check the serial to see if the value has been changed. |
| 48 | // this function also deals with the bootup case, during which the call to property |
| 49 | // set will fail if the property server hasn't yet started. |
| 50 | if (g_pinfo == NULL) { |
Yabin Cui | 1661125 | 2015-07-21 17:27:54 -0700 | [diff] [blame] | 51 | g_pinfo = __system_property_find(SYSTRACE_PROPERTY_NAME); |
Brigid Smith | a406ee6 | 2014-07-21 15:38:06 -0700 | [diff] [blame] | 52 | if (g_pinfo == NULL) { |
Yabin Cui | 1661125 | 2015-07-21 17:27:54 -0700 | [diff] [blame] | 53 | __system_property_set(SYSTRACE_PROPERTY_NAME, "0"); |
| 54 | g_pinfo = __system_property_find(SYSTRACE_PROPERTY_NAME); |
Brigid Smith | a406ee6 | 2014-07-21 15:38:06 -0700 | [diff] [blame] | 55 | } |
| 56 | } |
Yabin Cui | 1661125 | 2015-07-21 17:27:54 -0700 | [diff] [blame] | 57 | if (g_pinfo != NULL) { |
| 58 | // Find out which tags have been enabled on the command line and set |
| 59 | // the value of tags accordingly. If the value of the property changes, |
| 60 | // the serial will also change, so the costly system_property_read function |
| 61 | // can be avoided by calling the much cheaper system_property_serial |
| 62 | // first. The values within pinfo may change, but its location is guaranteed |
| 63 | // not to move. |
| 64 | uint32_t cur_serial = __system_property_serial(g_pinfo); |
| 65 | if (cur_serial != g_serial) { |
| 66 | g_serial = cur_serial; |
| 67 | char value[PROP_VALUE_MAX]; |
| 68 | __system_property_read(g_pinfo, 0, value); |
| 69 | g_tags = strtoull(value, NULL, 0); |
| 70 | } |
| 71 | result = ((g_tags & ATRACE_TAG_BIONIC) != 0); |
Brigid Smith | a406ee6 | 2014-07-21 15:38:06 -0700 | [diff] [blame] | 72 | } |
Yabin Cui | 1661125 | 2015-07-21 17:27:54 -0700 | [diff] [blame] | 73 | g_lock.unlock(); |
| 74 | return result; |
| 75 | } |
Brigid Smith | a406ee6 | 2014-07-21 15:38:06 -0700 | [diff] [blame] | 76 | |
Yabin Cui | 1661125 | 2015-07-21 17:27:54 -0700 | [diff] [blame] | 77 | static int get_trace_marker_fd() { |
| 78 | g_lock.lock(); |
| 79 | if (g_trace_marker_fd == -1) { |
| 80 | g_trace_marker_fd = open("/sys/kernel/debug/tracing/trace_marker", O_CLOEXEC | O_WRONLY); |
| 81 | } |
| 82 | g_lock.unlock(); |
| 83 | return g_trace_marker_fd; |
Brigid Smith | a406ee6 | 2014-07-21 15:38:06 -0700 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | ScopedTrace::ScopedTrace(const char* message) { |
| 87 | if (!should_trace()) { |
| 88 | return; |
| 89 | } |
| 90 | |
Yabin Cui | 1661125 | 2015-07-21 17:27:54 -0700 | [diff] [blame] | 91 | int trace_marker_fd = get_trace_marker_fd(); |
| 92 | if (trace_marker_fd == -1) { |
| 93 | return; |
Brigid Smith | a406ee6 | 2014-07-21 15:38:06 -0700 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | // If bionic tracing has been enabled, then write the message to the |
| 97 | // kernel trace_marker. |
| 98 | int length = strlen(message); |
| 99 | char buf[length + WRITE_OFFSET]; |
| 100 | size_t len = snprintf(buf, length + WRITE_OFFSET, "B|%d|%s", getpid(), message); |
Brigid Smith | a406ee6 | 2014-07-21 15:38:06 -0700 | [diff] [blame] | 101 | |
Yabin Cui | 1661125 | 2015-07-21 17:27:54 -0700 | [diff] [blame] | 102 | // Tracing may stop just after checking property and before writing the message. |
| 103 | // So the write is acceptable to fail. See b/20666100. |
| 104 | TEMP_FAILURE_RETRY(write(trace_marker_fd, buf, len)); |
Brigid Smith | a406ee6 | 2014-07-21 15:38:06 -0700 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | ScopedTrace::~ScopedTrace() { |
| 108 | if (!should_trace()) { |
| 109 | return; |
| 110 | } |
| 111 | |
Yabin Cui | 1661125 | 2015-07-21 17:27:54 -0700 | [diff] [blame] | 112 | int trace_marker_fd = get_trace_marker_fd(); |
| 113 | if (trace_marker_fd == -1) { |
| 114 | return; |
Brigid Smith | a406ee6 | 2014-07-21 15:38:06 -0700 | [diff] [blame] | 115 | } |
Yabin Cui | 1661125 | 2015-07-21 17:27:54 -0700 | [diff] [blame] | 116 | |
| 117 | TEMP_FAILURE_RETRY(write(trace_marker_fd, "E", 1)); |
Brigid Smith | a406ee6 | 2014-07-21 15:38:06 -0700 | [diff] [blame] | 118 | } |