blob: 5699f6e7f06d1a769c59e277c9fa35f031defbfa [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
Elliott Hughes40360b32014-12-29 13:29:50 -080017#include <errno.h>
Brigid Smitha406ee62014-07-21 15:38:06 -070018#include <fcntl.h>
19#include <stdio.h>
20#include <stdlib.h>
Elliott Hughes05fc1d72015-01-28 18:02:33 -080021#include <string.h>
Brigid Smitha406ee62014-07-21 15:38:06 -070022
Yabin Cui16611252015-07-21 17:27:54 -070023#include "private/bionic_lock.h"
Brigid Smitha406ee62014-07-21 15:38:06 -070024#include "private/bionic_systrace.h"
Elliott Hughese4ddb3c2017-04-17 14:12:25 -070025#include "private/CachedProperty.h"
Brigid Smitha406ee62014-07-21 15:38:06 -070026#include "private/libc_logging.h"
27
Elliott Hughese4ddb3c2017-04-17 14:12:25 -070028#include <cutils/trace.h> // For ATRACE_TAG_BIONIC.
Brigid Smitha406ee62014-07-21 15:38:06 -070029
30#define WRITE_OFFSET 32
31
Yabin Cui16611252015-07-21 17:27:54 -070032static Lock g_lock;
Brigid Smitha406ee62014-07-21 15:38:06 -070033static int g_trace_marker_fd = -1;
34
35static bool should_trace() {
Elliott Hughese4ddb3c2017-04-17 14:12:25 -070036 static CachedProperty g_debug_atrace_tags_enableflags("debug.atrace.tags.enableflags");
37 static uint64_t g_tags;
Dimitry Ivanov2a4a5e72017-03-20 10:54:52 -070038
Elliott Hughese4ddb3c2017-04-17 14:12:25 -070039 g_lock.lock();
40 if (g_debug_atrace_tags_enableflags.DidChange()) {
41 g_tags = strtoull(g_debug_atrace_tags_enableflags.Get(), nullptr, 0);
Brigid Smitha406ee62014-07-21 15:38:06 -070042 }
Yabin Cui16611252015-07-21 17:27:54 -070043 g_lock.unlock();
Elliott Hughese4ddb3c2017-04-17 14:12:25 -070044 return ((g_tags & ATRACE_TAG_BIONIC) != 0);
Yabin Cui16611252015-07-21 17:27:54 -070045}
Brigid Smitha406ee62014-07-21 15:38:06 -070046
Yabin Cui16611252015-07-21 17:27:54 -070047static int get_trace_marker_fd() {
48 g_lock.lock();
49 if (g_trace_marker_fd == -1) {
50 g_trace_marker_fd = open("/sys/kernel/debug/tracing/trace_marker", O_CLOEXEC | O_WRONLY);
51 }
52 g_lock.unlock();
53 return g_trace_marker_fd;
Brigid Smitha406ee62014-07-21 15:38:06 -070054}
55
Dimitry Ivanov2a4a5e72017-03-20 10:54:52 -070056void bionic_trace_begin(const char* message) {
Brigid Smitha406ee62014-07-21 15:38:06 -070057 if (!should_trace()) {
58 return;
59 }
60
Yabin Cui16611252015-07-21 17:27:54 -070061 int trace_marker_fd = get_trace_marker_fd();
62 if (trace_marker_fd == -1) {
63 return;
Brigid Smitha406ee62014-07-21 15:38:06 -070064 }
65
66 // If bionic tracing has been enabled, then write the message to the
67 // kernel trace_marker.
68 int length = strlen(message);
69 char buf[length + WRITE_OFFSET];
70 size_t len = snprintf(buf, length + WRITE_OFFSET, "B|%d|%s", getpid(), message);
Brigid Smitha406ee62014-07-21 15:38:06 -070071
Yabin Cui16611252015-07-21 17:27:54 -070072 // Tracing may stop just after checking property and before writing the message.
73 // So the write is acceptable to fail. See b/20666100.
74 TEMP_FAILURE_RETRY(write(trace_marker_fd, buf, len));
Brigid Smitha406ee62014-07-21 15:38:06 -070075}
76
Dimitry Ivanov2a4a5e72017-03-20 10:54:52 -070077void bionic_trace_end() {
Brigid Smitha406ee62014-07-21 15:38:06 -070078 if (!should_trace()) {
79 return;
80 }
81
Yabin Cui16611252015-07-21 17:27:54 -070082 int trace_marker_fd = get_trace_marker_fd();
83 if (trace_marker_fd == -1) {
84 return;
Brigid Smitha406ee62014-07-21 15:38:06 -070085 }
Yabin Cui16611252015-07-21 17:27:54 -070086
87 TEMP_FAILURE_RETRY(write(trace_marker_fd, "E", 1));
Brigid Smitha406ee62014-07-21 15:38:06 -070088}
Dimitry Ivanov2a4a5e72017-03-20 10:54:52 -070089
90ScopedTrace::ScopedTrace(const char* message) : called_end_(false) {
91 bionic_trace_begin(message);
92}
93
94ScopedTrace::~ScopedTrace() {
95 End();
96}
97
98void ScopedTrace::End() {
99 if (!called_end_) {
100 bionic_trace_end();
101 called_end_ = true;
102 }
103}