blob: 8e8ff768c7e50d7b848e37b46ecee5048d45c1e6 [file] [log] [blame]
Brigid Smitha406ee62014-07-21 15:38:06 -07001/*
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 Hughes40360b32014-12-29 13:29:50 -080018#include <errno.h>
Brigid Smitha406ee62014-07-21 15:38:06 -070019#include <fcntl.h>
20#include <stdio.h>
21#include <stdlib.h>
Elliott Hughes05fc1d72015-01-28 18:02:33 -080022#include <string.h>
Brigid Smitha406ee62014-07-21 15:38:06 -070023
Yabin Cui16611252015-07-21 17:27:54 -070024#include "private/bionic_lock.h"
Brigid Smitha406ee62014-07-21 15:38:06 -070025#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 Cui16611252015-07-21 17:27:54 -070033constexpr char SYSTRACE_PROPERTY_NAME[] = "debug.atrace.tags.enableflags";
34
35static Lock g_lock;
36static const prop_info* g_pinfo;
Tom Cherry46e2ead2015-12-10 13:21:46 -080037static uint32_t g_property_serial = -1;
38static uint32_t g_property_area_serial = -1;
Yabin Cui16611252015-07-21 17:27:54 -070039static uint64_t g_tags;
Brigid Smitha406ee62014-07-21 15:38:06 -070040static int g_trace_marker_fd = -1;
41
42static bool should_trace() {
Yabin Cui16611252015-07-21 17:27:54 -070043 bool result = false;
44 g_lock.lock();
Tom Cherry46e2ead2015-12-10 13:21:46 -080045 // 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.
Dimitry Ivanov2a4a5e72017-03-20 10:54:52 -070051 if (g_pinfo == nullptr && g_property_area_serial != __system_property_area_serial()) {
Tom Cherry46e2ead2015-12-10 13:21:46 -080052 g_property_area_serial = __system_property_area_serial();
Yabin Cui16611252015-07-21 17:27:54 -070053 g_pinfo = __system_property_find(SYSTRACE_PROPERTY_NAME);
Brigid Smitha406ee62014-07-21 15:38:06 -070054 }
Dimitry Ivanov2a4a5e72017-03-20 10:54:52 -070055
Tom Cherry46e2ead2015-12-10 13:21:46 -080056 if (g_pinfo) {
Yabin Cui16611252015-07-21 17:27:54 -070057 // Find out which tags have been enabled on the command line and set
58 // the value of tags accordingly. If the value of the property changes,
59 // the serial will also change, so the costly system_property_read function
60 // can be avoided by calling the much cheaper system_property_serial
61 // first. The values within pinfo may change, but its location is guaranteed
62 // not to move.
63 uint32_t cur_serial = __system_property_serial(g_pinfo);
Tom Cherry46e2ead2015-12-10 13:21:46 -080064 if (cur_serial != g_property_serial) {
Dimitry Ivanov2a4a5e72017-03-20 10:54:52 -070065 __system_property_read_callback(g_pinfo,
66 [] (void*, const char*, const char* value, uint32_t serial) {
67 g_property_serial = serial;
68 g_tags = strtoull(value, nullptr, 0);
69 }, nullptr);
Yabin Cui16611252015-07-21 17:27:54 -070070 }
71 result = ((g_tags & ATRACE_TAG_BIONIC) != 0);
Brigid Smitha406ee62014-07-21 15:38:06 -070072 }
Yabin Cui16611252015-07-21 17:27:54 -070073 g_lock.unlock();
74 return result;
75}
Brigid Smitha406ee62014-07-21 15:38:06 -070076
Yabin Cui16611252015-07-21 17:27:54 -070077static 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 Smitha406ee62014-07-21 15:38:06 -070084}
85
Dimitry Ivanov2a4a5e72017-03-20 10:54:52 -070086void bionic_trace_begin(const char* message) {
Brigid Smitha406ee62014-07-21 15:38:06 -070087 if (!should_trace()) {
88 return;
89 }
90
Yabin Cui16611252015-07-21 17:27:54 -070091 int trace_marker_fd = get_trace_marker_fd();
92 if (trace_marker_fd == -1) {
93 return;
Brigid Smitha406ee62014-07-21 15:38:06 -070094 }
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 Smitha406ee62014-07-21 15:38:06 -0700101
Yabin Cui16611252015-07-21 17:27:54 -0700102 // 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 Smitha406ee62014-07-21 15:38:06 -0700105}
106
Dimitry Ivanov2a4a5e72017-03-20 10:54:52 -0700107void bionic_trace_end() {
Brigid Smitha406ee62014-07-21 15:38:06 -0700108 if (!should_trace()) {
109 return;
110 }
111
Yabin Cui16611252015-07-21 17:27:54 -0700112 int trace_marker_fd = get_trace_marker_fd();
113 if (trace_marker_fd == -1) {
114 return;
Brigid Smitha406ee62014-07-21 15:38:06 -0700115 }
Yabin Cui16611252015-07-21 17:27:54 -0700116
117 TEMP_FAILURE_RETRY(write(trace_marker_fd, "E", 1));
Brigid Smitha406ee62014-07-21 15:38:06 -0700118}
Dimitry Ivanov2a4a5e72017-03-20 10:54:52 -0700119
120ScopedTrace::ScopedTrace(const char* message) : called_end_(false) {
121 bionic_trace_begin(message);
122}
123
124ScopedTrace::~ScopedTrace() {
125 End();
126}
127
128void ScopedTrace::End() {
129 if (!called_end_) {
130 bionic_trace_end();
131 called_end_ = true;
132 }
133}