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 | |
Bowgo Tsai | d0ecf0b | 2020-04-13 13:07:43 +0800 | [diff] [blame] | 17 | #include "private/bionic_systrace.h" |
| 18 | |
Elliott Hughes | 40360b3 | 2014-12-29 13:29:50 -0800 | [diff] [blame] | 19 | #include <errno.h> |
Brigid Smith | a406ee6 | 2014-07-21 15:38:06 -0700 | [diff] [blame] | 20 | #include <fcntl.h> |
| 21 | #include <stdio.h> |
| 22 | #include <stdlib.h> |
Elliott Hughes | 05fc1d7 | 2015-01-28 18:02:33 -0800 | [diff] [blame] | 23 | #include <string.h> |
Brigid Smith | a406ee6 | 2014-07-21 15:38:06 -0700 | [diff] [blame] | 24 | |
Josh Gao | e1d121b | 2019-07-11 13:00:26 -0700 | [diff] [blame] | 25 | #include <async_safe/log.h> |
Elliott Hughes | e4ddb3c | 2017-04-17 14:12:25 -0700 | [diff] [blame] | 26 | #include <cutils/trace.h> // For ATRACE_TAG_BIONIC. |
Brigid Smith | a406ee6 | 2014-07-21 15:38:06 -0700 | [diff] [blame] | 27 | |
Bowgo Tsai | d0ecf0b | 2020-04-13 13:07:43 +0800 | [diff] [blame] | 28 | #include "private/CachedProperty.h" |
| 29 | #include "private/bionic_lock.h" |
| 30 | |
Brigid Smith | a406ee6 | 2014-07-21 15:38:06 -0700 | [diff] [blame] | 31 | #define WRITE_OFFSET 32 |
| 32 | |
Yabin Cui | 1661125 | 2015-07-21 17:27:54 -0700 | [diff] [blame] | 33 | static Lock g_lock; |
Brigid Smith | a406ee6 | 2014-07-21 15:38:06 -0700 | [diff] [blame] | 34 | |
Bowgo Tsai | d0ecf0b | 2020-04-13 13:07:43 +0800 | [diff] [blame] | 35 | bool should_trace(const uint64_t enable_tags) { |
Bowgo Tsai | 1e1c784 | 2020-08-24 15:19:52 +0800 | [diff] [blame] | 36 | static uint64_t tags_val; |
| 37 | static CachedProperty tags_prop(kTraceTagsProp); |
Bowgo Tsai | d0ecf0b | 2020-04-13 13:07:43 +0800 | [diff] [blame] | 38 | g_lock.lock(); |
Bowgo Tsai | 1e1c784 | 2020-08-24 15:19:52 +0800 | [diff] [blame] | 39 | if (tags_prop.DidChange()) { |
| 40 | tags_val = strtoull(tags_prop.Get(), nullptr, 0); |
Bowgo Tsai | d0ecf0b | 2020-04-13 13:07:43 +0800 | [diff] [blame] | 41 | } |
| 42 | g_lock.unlock(); |
Bowgo Tsai | 1e1c784 | 2020-08-24 15:19:52 +0800 | [diff] [blame] | 43 | return tags_val & enable_tags; |
Bowgo Tsai | d0ecf0b | 2020-04-13 13:07:43 +0800 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | int get_trace_marker_fd() { |
Bowgo Tsai | 1e1c784 | 2020-08-24 15:19:52 +0800 | [diff] [blame] | 47 | static int opened_trace_marker_fd = -1; |
Yabin Cui | 1661125 | 2015-07-21 17:27:54 -0700 | [diff] [blame] | 48 | g_lock.lock(); |
Bowgo Tsai | 1e1c784 | 2020-08-24 15:19:52 +0800 | [diff] [blame] | 49 | if (opened_trace_marker_fd == -1) { |
| 50 | opened_trace_marker_fd = open("/sys/kernel/tracing/trace_marker", O_CLOEXEC | O_WRONLY); |
| 51 | if (opened_trace_marker_fd == -1) { |
| 52 | opened_trace_marker_fd = open("/sys/kernel/debug/tracing/trace_marker", O_CLOEXEC | O_WRONLY); |
Hridya Valsaraju | ad5f772 | 2020-02-07 11:53:08 -0800 | [diff] [blame] | 53 | } |
Yabin Cui | 1661125 | 2015-07-21 17:27:54 -0700 | [diff] [blame] | 54 | } |
| 55 | g_lock.unlock(); |
Bowgo Tsai | 1e1c784 | 2020-08-24 15:19:52 +0800 | [diff] [blame] | 56 | return opened_trace_marker_fd; |
Brigid Smith | a406ee6 | 2014-07-21 15:38:06 -0700 | [diff] [blame] | 57 | } |
| 58 | |
Bowgo Tsai | d0ecf0b | 2020-04-13 13:07:43 +0800 | [diff] [blame] | 59 | // event could be 'B' for begin or 'E' for end. |
| 60 | void output_trace(const char* message, const char event) { |
Yabin Cui | 1661125 | 2015-07-21 17:27:54 -0700 | [diff] [blame] | 61 | int trace_marker_fd = get_trace_marker_fd(); |
| 62 | if (trace_marker_fd == -1) { |
| 63 | return; |
Brigid Smith | a406ee6 | 2014-07-21 15:38:06 -0700 | [diff] [blame] | 64 | } |
| 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]; |
Bowgo Tsai | d0ecf0b | 2020-04-13 13:07:43 +0800 | [diff] [blame] | 70 | size_t len = |
| 71 | async_safe_format_buffer(buf, length + WRITE_OFFSET, "%c|%d|%s", event, getpid(), message); |
Brigid Smith | a406ee6 | 2014-07-21 15:38:06 -0700 | [diff] [blame] | 72 | |
Yabin Cui | 1661125 | 2015-07-21 17:27:54 -0700 | [diff] [blame] | 73 | // Tracing may stop just after checking property and before writing the message. |
| 74 | // So the write is acceptable to fail. See b/20666100. |
| 75 | TEMP_FAILURE_RETRY(write(trace_marker_fd, buf, len)); |
Brigid Smith | a406ee6 | 2014-07-21 15:38:06 -0700 | [diff] [blame] | 76 | } |
| 77 | |
Bowgo Tsai | d0ecf0b | 2020-04-13 13:07:43 +0800 | [diff] [blame] | 78 | void bionic_trace_begin(const char* message) { |
| 79 | if (!should_trace()) { |
| 80 | return; |
| 81 | } |
| 82 | |
| 83 | output_trace(message); |
| 84 | } |
| 85 | |
Dimitry Ivanov | 2a4a5e7 | 2017-03-20 10:54:52 -0700 | [diff] [blame] | 86 | void bionic_trace_end() { |
Brigid Smith | a406ee6 | 2014-07-21 15:38:06 -0700 | [diff] [blame] | 87 | if (!should_trace()) { |
| 88 | return; |
| 89 | } |
| 90 | |
Yabin Cui | 1661125 | 2015-07-21 17:27:54 -0700 | [diff] [blame] | 91 | int trace_marker_fd = get_trace_marker_fd(); |
| 92 | if (trace_marker_fd == -1) { |
| 93 | return; |
Brigid Smith | a406ee6 | 2014-07-21 15:38:06 -0700 | [diff] [blame] | 94 | } |
Yabin Cui | 1661125 | 2015-07-21 17:27:54 -0700 | [diff] [blame] | 95 | |
Philip Cuadra | 7fc82c2 | 2019-01-25 11:05:21 -0800 | [diff] [blame] | 96 | TEMP_FAILURE_RETRY(write(trace_marker_fd, "E|", 2)); |
Brigid Smith | a406ee6 | 2014-07-21 15:38:06 -0700 | [diff] [blame] | 97 | } |
Dimitry Ivanov | 2a4a5e7 | 2017-03-20 10:54:52 -0700 | [diff] [blame] | 98 | |
| 99 | ScopedTrace::ScopedTrace(const char* message) : called_end_(false) { |
| 100 | bionic_trace_begin(message); |
| 101 | } |
| 102 | |
| 103 | ScopedTrace::~ScopedTrace() { |
| 104 | End(); |
| 105 | } |
| 106 | |
| 107 | void ScopedTrace::End() { |
| 108 | if (!called_end_) { |
| 109 | bionic_trace_end(); |
| 110 | called_end_ = true; |
| 111 | } |
| 112 | } |