| 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 |  | 
| Elliott Hughes | 40360b3 | 2014-12-29 13:29:50 -0800 | [diff] [blame] | 17 | #include <errno.h> | 
| Brigid Smith | a406ee6 | 2014-07-21 15:38:06 -0700 | [diff] [blame] | 18 | #include <fcntl.h> | 
|  | 19 | #include <stdio.h> | 
|  | 20 | #include <stdlib.h> | 
| Elliott Hughes | 05fc1d7 | 2015-01-28 18:02:33 -0800 | [diff] [blame] | 21 | #include <string.h> | 
| Brigid Smith | a406ee6 | 2014-07-21 15:38:06 -0700 | [diff] [blame] | 22 |  | 
| Yabin Cui | 1661125 | 2015-07-21 17:27:54 -0700 | [diff] [blame] | 23 | #include "private/bionic_lock.h" | 
| Brigid Smith | a406ee6 | 2014-07-21 15:38:06 -0700 | [diff] [blame] | 24 | #include "private/bionic_systrace.h" | 
| Elliott Hughes | e4ddb3c | 2017-04-17 14:12:25 -0700 | [diff] [blame] | 25 | #include "private/CachedProperty.h" | 
| Brigid Smith | a406ee6 | 2014-07-21 15:38:06 -0700 | [diff] [blame] | 26 |  | 
| Elliott Hughes | e4ddb3c | 2017-04-17 14:12:25 -0700 | [diff] [blame] | 27 | #include <cutils/trace.h> // For ATRACE_TAG_BIONIC. | 
| Brigid Smith | a406ee6 | 2014-07-21 15:38:06 -0700 | [diff] [blame] | 28 |  | 
|  | 29 | #define WRITE_OFFSET   32 | 
|  | 30 |  | 
| Yabin Cui | 1661125 | 2015-07-21 17:27:54 -0700 | [diff] [blame] | 31 | static Lock g_lock; | 
| Wei Li | 2cb5f7f | 2018-01-26 15:00:32 +0800 | [diff] [blame] | 32 | static CachedProperty g_debug_atrace_tags_enableflags("debug.atrace.tags.enableflags"); | 
|  | 33 | static uint64_t g_tags; | 
| Brigid Smith | a406ee6 | 2014-07-21 15:38:06 -0700 | [diff] [blame] | 34 | static int g_trace_marker_fd = -1; | 
|  | 35 |  | 
|  | 36 | static bool should_trace() { | 
| Elliott Hughes | e4ddb3c | 2017-04-17 14:12:25 -0700 | [diff] [blame] | 37 | g_lock.lock(); | 
|  | 38 | if (g_debug_atrace_tags_enableflags.DidChange()) { | 
|  | 39 | g_tags = strtoull(g_debug_atrace_tags_enableflags.Get(), nullptr, 0); | 
| Brigid Smith | a406ee6 | 2014-07-21 15:38:06 -0700 | [diff] [blame] | 40 | } | 
| Yabin Cui | 1661125 | 2015-07-21 17:27:54 -0700 | [diff] [blame] | 41 | g_lock.unlock(); | 
| Elliott Hughes | e4ddb3c | 2017-04-17 14:12:25 -0700 | [diff] [blame] | 42 | return ((g_tags & ATRACE_TAG_BIONIC) != 0); | 
| Yabin Cui | 1661125 | 2015-07-21 17:27:54 -0700 | [diff] [blame] | 43 | } | 
| Brigid Smith | a406ee6 | 2014-07-21 15:38:06 -0700 | [diff] [blame] | 44 |  | 
| Yabin Cui | 1661125 | 2015-07-21 17:27:54 -0700 | [diff] [blame] | 45 | static int get_trace_marker_fd() { | 
|  | 46 | g_lock.lock(); | 
|  | 47 | if (g_trace_marker_fd == -1) { | 
|  | 48 | g_trace_marker_fd = open("/sys/kernel/debug/tracing/trace_marker", O_CLOEXEC | O_WRONLY); | 
|  | 49 | } | 
|  | 50 | g_lock.unlock(); | 
|  | 51 | return g_trace_marker_fd; | 
| Brigid Smith | a406ee6 | 2014-07-21 15:38:06 -0700 | [diff] [blame] | 52 | } | 
|  | 53 |  | 
| Dimitry Ivanov | 2a4a5e7 | 2017-03-20 10:54:52 -0700 | [diff] [blame] | 54 | void bionic_trace_begin(const char* message) { | 
| Brigid Smith | a406ee6 | 2014-07-21 15:38:06 -0700 | [diff] [blame] | 55 | if (!should_trace()) { | 
|  | 56 | return; | 
|  | 57 | } | 
|  | 58 |  | 
| Yabin Cui | 1661125 | 2015-07-21 17:27:54 -0700 | [diff] [blame] | 59 | int trace_marker_fd = get_trace_marker_fd(); | 
|  | 60 | if (trace_marker_fd == -1) { | 
|  | 61 | return; | 
| Brigid Smith | a406ee6 | 2014-07-21 15:38:06 -0700 | [diff] [blame] | 62 | } | 
|  | 63 |  | 
|  | 64 | // If bionic tracing has been enabled, then write the message to the | 
|  | 65 | // kernel trace_marker. | 
|  | 66 | int length = strlen(message); | 
|  | 67 | char buf[length + WRITE_OFFSET]; | 
|  | 68 | size_t len = snprintf(buf, length + WRITE_OFFSET, "B|%d|%s", getpid(), message); | 
| Brigid Smith | a406ee6 | 2014-07-21 15:38:06 -0700 | [diff] [blame] | 69 |  | 
| Yabin Cui | 1661125 | 2015-07-21 17:27:54 -0700 | [diff] [blame] | 70 | // Tracing may stop just after checking property and before writing the message. | 
|  | 71 | // So the write is acceptable to fail. See b/20666100. | 
|  | 72 | TEMP_FAILURE_RETRY(write(trace_marker_fd, buf, len)); | 
| Brigid Smith | a406ee6 | 2014-07-21 15:38:06 -0700 | [diff] [blame] | 73 | } | 
|  | 74 |  | 
| Dimitry Ivanov | 2a4a5e7 | 2017-03-20 10:54:52 -0700 | [diff] [blame] | 75 | void bionic_trace_end() { | 
| Brigid Smith | a406ee6 | 2014-07-21 15:38:06 -0700 | [diff] [blame] | 76 | if (!should_trace()) { | 
|  | 77 | return; | 
|  | 78 | } | 
|  | 79 |  | 
| Yabin Cui | 1661125 | 2015-07-21 17:27:54 -0700 | [diff] [blame] | 80 | int trace_marker_fd = get_trace_marker_fd(); | 
|  | 81 | if (trace_marker_fd == -1) { | 
|  | 82 | return; | 
| Brigid Smith | a406ee6 | 2014-07-21 15:38:06 -0700 | [diff] [blame] | 83 | } | 
| Yabin Cui | 1661125 | 2015-07-21 17:27:54 -0700 | [diff] [blame] | 84 |  | 
| Philip Cuadra | 7fc82c2 | 2019-01-25 11:05:21 -0800 | [diff] [blame] | 85 | TEMP_FAILURE_RETRY(write(trace_marker_fd, "E|", 2)); | 
| Brigid Smith | a406ee6 | 2014-07-21 15:38:06 -0700 | [diff] [blame] | 86 | } | 
| Dimitry Ivanov | 2a4a5e7 | 2017-03-20 10:54:52 -0700 | [diff] [blame] | 87 |  | 
|  | 88 | ScopedTrace::ScopedTrace(const char* message) : called_end_(false) { | 
|  | 89 | bionic_trace_begin(message); | 
|  | 90 | } | 
|  | 91 |  | 
|  | 92 | ScopedTrace::~ScopedTrace() { | 
|  | 93 | End(); | 
|  | 94 | } | 
|  | 95 |  | 
|  | 96 | void ScopedTrace::End() { | 
|  | 97 | if (!called_end_) { | 
|  | 98 | bionic_trace_end(); | 
|  | 99 | called_end_ = true; | 
|  | 100 | } | 
|  | 101 | } |