blob: 06f4505143372d246a2ce240712a57885b3e0d20 [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
Bowgo Tsaid0ecf0b2020-04-13 13:07:43 +080017#include "private/bionic_systrace.h"
18
Elliott Hughes40360b32014-12-29 13:29:50 -080019#include <errno.h>
Brigid Smitha406ee62014-07-21 15:38:06 -070020#include <fcntl.h>
21#include <stdio.h>
22#include <stdlib.h>
Elliott Hughes05fc1d72015-01-28 18:02:33 -080023#include <string.h>
Brigid Smitha406ee62014-07-21 15:38:06 -070024
Josh Gaoe1d121b2019-07-11 13:00:26 -070025#include <async_safe/log.h>
Elliott Hughese4ddb3c2017-04-17 14:12:25 -070026#include <cutils/trace.h> // For ATRACE_TAG_BIONIC.
Brigid Smitha406ee62014-07-21 15:38:06 -070027
Bowgo Tsaid0ecf0b2020-04-13 13:07:43 +080028#include "private/CachedProperty.h"
29#include "private/bionic_lock.h"
30
Brigid Smitha406ee62014-07-21 15:38:06 -070031#define WRITE_OFFSET 32
32
Yabin Cui16611252015-07-21 17:27:54 -070033static Lock g_lock;
Wei Li2cb5f7f2018-01-26 15:00:32 +080034static uint64_t g_tags;
Brigid Smitha406ee62014-07-21 15:38:06 -070035static int g_trace_marker_fd = -1;
36
Bowgo Tsaid0ecf0b2020-04-13 13:07:43 +080037static CachedProperty& GetTagsProp() {
38 static CachedProperty cached_property(kTraceTagsProp);
39 return cached_property;
Yabin Cui16611252015-07-21 17:27:54 -070040}
Brigid Smitha406ee62014-07-21 15:38:06 -070041
Bowgo Tsaid0ecf0b2020-04-13 13:07:43 +080042bool should_trace(const uint64_t enable_tags) {
43 g_lock.lock();
44 if (GetTagsProp().DidChange()) {
45 g_tags = strtoull(GetTagsProp().Get(), nullptr, 0);
46 }
47 g_lock.unlock();
48 return g_tags & enable_tags;
49}
50
51int get_trace_marker_fd() {
Yabin Cui16611252015-07-21 17:27:54 -070052 g_lock.lock();
53 if (g_trace_marker_fd == -1) {
Hridya Valsarajuad5f7722020-02-07 11:53:08 -080054 g_trace_marker_fd = open("/sys/kernel/tracing/trace_marker", O_CLOEXEC | O_WRONLY);
55 if (g_trace_marker_fd == -1) {
56 g_trace_marker_fd = open("/sys/kernel/debug/tracing/trace_marker", O_CLOEXEC | O_WRONLY);
57 }
Yabin Cui16611252015-07-21 17:27:54 -070058 }
59 g_lock.unlock();
60 return g_trace_marker_fd;
Brigid Smitha406ee62014-07-21 15:38:06 -070061}
62
Bowgo Tsaid0ecf0b2020-04-13 13:07:43 +080063// event could be 'B' for begin or 'E' for end.
64void output_trace(const char* message, const char event) {
Yabin Cui16611252015-07-21 17:27:54 -070065 int trace_marker_fd = get_trace_marker_fd();
66 if (trace_marker_fd == -1) {
67 return;
Brigid Smitha406ee62014-07-21 15:38:06 -070068 }
69
70 // If bionic tracing has been enabled, then write the message to the
71 // kernel trace_marker.
72 int length = strlen(message);
73 char buf[length + WRITE_OFFSET];
Bowgo Tsaid0ecf0b2020-04-13 13:07:43 +080074 size_t len =
75 async_safe_format_buffer(buf, length + WRITE_OFFSET, "%c|%d|%s", event, getpid(), message);
Brigid Smitha406ee62014-07-21 15:38:06 -070076
Yabin Cui16611252015-07-21 17:27:54 -070077 // Tracing may stop just after checking property and before writing the message.
78 // So the write is acceptable to fail. See b/20666100.
79 TEMP_FAILURE_RETRY(write(trace_marker_fd, buf, len));
Brigid Smitha406ee62014-07-21 15:38:06 -070080}
81
Bowgo Tsaid0ecf0b2020-04-13 13:07:43 +080082void bionic_trace_begin(const char* message) {
83 if (!should_trace()) {
84 return;
85 }
86
87 output_trace(message);
88}
89
Dimitry Ivanov2a4a5e72017-03-20 10:54:52 -070090void bionic_trace_end() {
Brigid Smitha406ee62014-07-21 15:38:06 -070091 if (!should_trace()) {
92 return;
93 }
94
Yabin Cui16611252015-07-21 17:27:54 -070095 int trace_marker_fd = get_trace_marker_fd();
96 if (trace_marker_fd == -1) {
97 return;
Brigid Smitha406ee62014-07-21 15:38:06 -070098 }
Yabin Cui16611252015-07-21 17:27:54 -070099
Philip Cuadra7fc82c22019-01-25 11:05:21 -0800100 TEMP_FAILURE_RETRY(write(trace_marker_fd, "E|", 2));
Brigid Smitha406ee62014-07-21 15:38:06 -0700101}
Dimitry Ivanov2a4a5e72017-03-20 10:54:52 -0700102
103ScopedTrace::ScopedTrace(const char* message) : called_end_(false) {
104 bionic_trace_begin(message);
105}
106
107ScopedTrace::~ScopedTrace() {
108 End();
109}
110
111void ScopedTrace::End() {
112 if (!called_end_) {
113 bionic_trace_end();
114 called_end_ = true;
115 }
116}