| 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; | 
| Tom Cherry | 46e2ead | 2015-12-10 13:21:46 -0800 | [diff] [blame] | 37 | static uint32_t g_property_serial = -1; | 
|  | 38 | static uint32_t g_property_area_serial = -1; | 
| Yabin Cui | 1661125 | 2015-07-21 17:27:54 -0700 | [diff] [blame] | 39 | static uint64_t g_tags; | 
| Brigid Smith | a406ee6 | 2014-07-21 15:38:06 -0700 | [diff] [blame] | 40 | static int g_trace_marker_fd = -1; | 
|  | 41 |  | 
|  | 42 | static bool should_trace() { | 
| Yabin Cui | 1661125 | 2015-07-21 17:27:54 -0700 | [diff] [blame] | 43 | bool result = false; | 
|  | 44 | g_lock.lock(); | 
| Tom Cherry | 46e2ead | 2015-12-10 13:21:46 -0800 | [diff] [blame] | 45 | // debug.atrace.tags.enableflags is set to a safe non-tracing value during property | 
|  | 46 | // space initialization, so it should only be null in two cases, if there are | 
|  | 47 | // insufficient permissions for this process to access the property, in which | 
|  | 48 | // case an audit will be logged, and during boot before the property server has | 
|  | 49 | // been started, in which case we store the global property_area serial to prevent | 
|  | 50 | // the costly find operation until we see a changed property_area. | 
|  | 51 | if (!g_pinfo && g_property_area_serial != __system_property_area_serial()) { | 
|  | 52 | g_property_area_serial = __system_property_area_serial(); | 
| Yabin Cui | 1661125 | 2015-07-21 17:27:54 -0700 | [diff] [blame] | 53 | g_pinfo = __system_property_find(SYSTRACE_PROPERTY_NAME); | 
| Brigid Smith | a406ee6 | 2014-07-21 15:38:06 -0700 | [diff] [blame] | 54 | } | 
| Tom Cherry | 46e2ead | 2015-12-10 13:21:46 -0800 | [diff] [blame] | 55 | if (g_pinfo) { | 
| Yabin Cui | 1661125 | 2015-07-21 17:27:54 -0700 | [diff] [blame] | 56 | // Find out which tags have been enabled on the command line and set | 
|  | 57 | // the value of tags accordingly.  If the value of the property changes, | 
|  | 58 | // the serial will also change, so the costly system_property_read function | 
|  | 59 | // can be avoided by calling the much cheaper system_property_serial | 
|  | 60 | // first.  The values within pinfo may change, but its location is guaranteed | 
|  | 61 | // not to move. | 
|  | 62 | uint32_t cur_serial = __system_property_serial(g_pinfo); | 
| Tom Cherry | 46e2ead | 2015-12-10 13:21:46 -0800 | [diff] [blame] | 63 | if (cur_serial != g_property_serial) { | 
|  | 64 | g_property_serial = cur_serial; | 
| Yabin Cui | 1661125 | 2015-07-21 17:27:54 -0700 | [diff] [blame] | 65 | char value[PROP_VALUE_MAX]; | 
|  | 66 | __system_property_read(g_pinfo, 0, value); | 
| Tom Cherry | 46e2ead | 2015-12-10 13:21:46 -0800 | [diff] [blame] | 67 | g_tags = strtoull(value, nullptr, 0); | 
| Yabin Cui | 1661125 | 2015-07-21 17:27:54 -0700 | [diff] [blame] | 68 | } | 
|  | 69 | result = ((g_tags & ATRACE_TAG_BIONIC) != 0); | 
| Brigid Smith | a406ee6 | 2014-07-21 15:38:06 -0700 | [diff] [blame] | 70 | } | 
| Yabin Cui | 1661125 | 2015-07-21 17:27:54 -0700 | [diff] [blame] | 71 | g_lock.unlock(); | 
|  | 72 | return result; | 
|  | 73 | } | 
| Brigid Smith | a406ee6 | 2014-07-21 15:38:06 -0700 | [diff] [blame] | 74 |  | 
| Yabin Cui | 1661125 | 2015-07-21 17:27:54 -0700 | [diff] [blame] | 75 | static int get_trace_marker_fd() { | 
|  | 76 | g_lock.lock(); | 
|  | 77 | if (g_trace_marker_fd == -1) { | 
|  | 78 | g_trace_marker_fd = open("/sys/kernel/debug/tracing/trace_marker", O_CLOEXEC | O_WRONLY); | 
|  | 79 | } | 
|  | 80 | g_lock.unlock(); | 
|  | 81 | return g_trace_marker_fd; | 
| Brigid Smith | a406ee6 | 2014-07-21 15:38:06 -0700 | [diff] [blame] | 82 | } | 
|  | 83 |  | 
|  | 84 | ScopedTrace::ScopedTrace(const char* message) { | 
|  | 85 | if (!should_trace()) { | 
|  | 86 | return; | 
|  | 87 | } | 
|  | 88 |  | 
| Yabin Cui | 1661125 | 2015-07-21 17:27:54 -0700 | [diff] [blame] | 89 | int trace_marker_fd = get_trace_marker_fd(); | 
|  | 90 | if (trace_marker_fd == -1) { | 
|  | 91 | return; | 
| Brigid Smith | a406ee6 | 2014-07-21 15:38:06 -0700 | [diff] [blame] | 92 | } | 
|  | 93 |  | 
|  | 94 | // If bionic tracing has been enabled, then write the message to the | 
|  | 95 | // kernel trace_marker. | 
|  | 96 | int length = strlen(message); | 
|  | 97 | char buf[length + WRITE_OFFSET]; | 
|  | 98 | 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] | 99 |  | 
| Yabin Cui | 1661125 | 2015-07-21 17:27:54 -0700 | [diff] [blame] | 100 | // Tracing may stop just after checking property and before writing the message. | 
|  | 101 | // So the write is acceptable to fail. See b/20666100. | 
|  | 102 | TEMP_FAILURE_RETRY(write(trace_marker_fd, buf, len)); | 
| Brigid Smith | a406ee6 | 2014-07-21 15:38:06 -0700 | [diff] [blame] | 103 | } | 
|  | 104 |  | 
|  | 105 | ScopedTrace::~ScopedTrace() { | 
|  | 106 | if (!should_trace()) { | 
|  | 107 | return; | 
|  | 108 | } | 
|  | 109 |  | 
| Yabin Cui | 1661125 | 2015-07-21 17:27:54 -0700 | [diff] [blame] | 110 | int trace_marker_fd = get_trace_marker_fd(); | 
|  | 111 | if (trace_marker_fd == -1) { | 
|  | 112 | return; | 
| Brigid Smith | a406ee6 | 2014-07-21 15:38:06 -0700 | [diff] [blame] | 113 | } | 
| Yabin Cui | 1661125 | 2015-07-21 17:27:54 -0700 | [diff] [blame] | 114 |  | 
|  | 115 | TEMP_FAILURE_RETRY(write(trace_marker_fd, "E", 1)); | 
| Brigid Smith | a406ee6 | 2014-07-21 15:38:06 -0700 | [diff] [blame] | 116 | } |