blob: cf5cd8203839a95cac86fd0034fa7f05402cb5b5 [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
Bowgo Tsai8f14b652021-07-15 10:13:33 +000023#include "private/bionic_lock.h"
24#include "private/bionic_systrace.h"
25#include "private/CachedProperty.h"
26
Josh Gaoe1d121b2019-07-11 13:00:26 -070027#include <async_safe/log.h>
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;
Bowgo Tsai8f14b652021-07-15 10:13:33 +000033static CachedProperty g_debug_atrace_tags_enableflags("debug.atrace.tags.enableflags");
Bowgo Tsai13a960f2021-07-15 09:46:53 +000034static uint64_t g_tags;
35static int g_trace_marker_fd = -1;
36
Bowgo Tsai8f14b652021-07-15 10:13:33 +000037static bool should_trace() {
Bowgo Tsaid0ecf0b2020-04-13 13:07:43 +080038 g_lock.lock();
Bowgo Tsai8f14b652021-07-15 10:13:33 +000039 if (g_debug_atrace_tags_enableflags.DidChange()) {
40 g_tags = strtoull(g_debug_atrace_tags_enableflags.Get(), nullptr, 0);
Bowgo Tsaid0ecf0b2020-04-13 13:07:43 +080041 }
42 g_lock.unlock();
Bowgo Tsai8f14b652021-07-15 10:13:33 +000043 return ((g_tags & ATRACE_TAG_BIONIC) != 0);
Bowgo Tsaid0ecf0b2020-04-13 13:07:43 +080044}
45
Bowgo Tsai8f14b652021-07-15 10:13:33 +000046static int get_trace_marker_fd() {
Yabin Cui16611252015-07-21 17:27:54 -070047 g_lock.lock();
Bowgo Tsai13a960f2021-07-15 09:46:53 +000048 if (g_trace_marker_fd == -1) {
49 g_trace_marker_fd = open("/sys/kernel/tracing/trace_marker", O_CLOEXEC | O_WRONLY);
50 if (g_trace_marker_fd == -1) {
51 g_trace_marker_fd = open("/sys/kernel/debug/tracing/trace_marker", O_CLOEXEC | O_WRONLY);
Hridya Valsarajuad5f7722020-02-07 11:53:08 -080052 }
Yabin Cui16611252015-07-21 17:27:54 -070053 }
54 g_lock.unlock();
Bowgo Tsai13a960f2021-07-15 09:46:53 +000055 return g_trace_marker_fd;
Brigid Smitha406ee62014-07-21 15:38:06 -070056}
57
Bowgo Tsai8f14b652021-07-15 10:13:33 +000058void bionic_trace_begin(const char* message) {
59 if (!should_trace()) {
60 return;
61 }
62
Yabin Cui16611252015-07-21 17:27:54 -070063 int trace_marker_fd = get_trace_marker_fd();
64 if (trace_marker_fd == -1) {
65 return;
Brigid Smitha406ee62014-07-21 15:38:06 -070066 }
67
68 // If bionic tracing has been enabled, then write the message to the
69 // kernel trace_marker.
70 int length = strlen(message);
71 char buf[length + WRITE_OFFSET];
Bowgo Tsai8f14b652021-07-15 10:13:33 +000072 size_t len = async_safe_format_buffer(buf, length + WRITE_OFFSET, "B|%d|%s", getpid(), message);
Brigid Smitha406ee62014-07-21 15:38:06 -070073
Yabin Cui16611252015-07-21 17:27:54 -070074 // Tracing may stop just after checking property and before writing the message.
75 // So the write is acceptable to fail. See b/20666100.
76 TEMP_FAILURE_RETRY(write(trace_marker_fd, buf, len));
Brigid Smitha406ee62014-07-21 15:38:06 -070077}
78
Dimitry Ivanov2a4a5e72017-03-20 10:54:52 -070079void bionic_trace_end() {
Brigid Smitha406ee62014-07-21 15:38:06 -070080 if (!should_trace()) {
81 return;
82 }
83
Yabin Cui16611252015-07-21 17:27:54 -070084 int trace_marker_fd = get_trace_marker_fd();
85 if (trace_marker_fd == -1) {
86 return;
Brigid Smitha406ee62014-07-21 15:38:06 -070087 }
Yabin Cui16611252015-07-21 17:27:54 -070088
Lalit Maganti2aa3f7c2021-08-26 15:46:50 +010089 // This code is intentionally "sub-optimal"; do not optimize this by inlining
90 // the E| string into the write.
91 //
92 // This is because if the const char* string passed to write(trace_marker) is not
93 // in resident memory (e.g. the page of the .rodata section that contains it has
94 // been paged out, or the anonymous page that contained a heap-based string is
95 // swapped in zram), the ftrace code will NOT page it in and instead report
96 // <faulted>.
97 //
98 // We "fix" this by putting the string on the stack, which is more unlikely
99 // to be paged out and pass the pointer to that instead.
100 //
101 // See b/197620214 for more context on this.
102 volatile char buf[2]{'E', '|'};
103 TEMP_FAILURE_RETRY(write(trace_marker_fd, const_cast<const char*>(buf), 2));
Brigid Smitha406ee62014-07-21 15:38:06 -0700104}
Dimitry Ivanov2a4a5e72017-03-20 10:54:52 -0700105
106ScopedTrace::ScopedTrace(const char* message) : called_end_(false) {
107 bionic_trace_begin(message);
108}
109
110ScopedTrace::~ScopedTrace() {
111 End();
112}
113
114void ScopedTrace::End() {
115 if (!called_end_) {
116 bionic_trace_end();
117 called_end_ = true;
118 }
119}