blob: 10521cf0137099fa8804375abf6fc4bbeaba5d5a [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
17#include <cutils/trace.h>
Elliott Hughes40360b32014-12-29 13:29:50 -080018#include <errno.h>
Brigid Smitha406ee62014-07-21 15:38:06 -070019#include <fcntl.h>
20#include <stdio.h>
21#include <stdlib.h>
Elliott Hughes05fc1d72015-01-28 18:02:33 -080022#include <string.h>
Brigid Smitha406ee62014-07-21 15:38:06 -070023
24#include "private/bionic_systrace.h"
25#include "private/libc_logging.h"
26
27#define _REALLY_INCLUDE_SYS__SYSTEM_PROPERTIES_H_
28#include <sys/_system_properties.h>
29
30#define WRITE_OFFSET 32
31
32static const prop_info* g_pinfo = NULL;
33static uint32_t g_serial = -1;
34static uint64_t g_tags = 0;
35static int g_trace_marker_fd = -1;
36
37static bool should_trace() {
38 // If g_pinfo is null, this means that systrace hasn't been run and it's safe to
39 // assume that no trace writing will need to take place. However, to avoid running
40 // this costly find check each time, we set it to a non-tracing value so that next
41 // time, it will just check the serial to see if the value has been changed.
42 // this function also deals with the bootup case, during which the call to property
43 // set will fail if the property server hasn't yet started.
44 if (g_pinfo == NULL) {
45 g_pinfo = __system_property_find("debug.atrace.tags.enableflags");
46 if (g_pinfo == NULL) {
47 __system_property_set("debug.atrace.tags.enableflags", "0");
48 g_pinfo = __system_property_find("debug.atrace.tags.enableflags");
49 if (g_pinfo == NULL) {
50 return false;
51 }
52 }
53 }
54
55 // Find out which tags have been enabled on the command line and set
56 // the value of tags accordingly. If the value of the property changes,
57 // the serial will also change, so the costly system_property_read function
58 // can be avoided by calling the much cheaper system_property_serial
59 // first. The values within pinfo may change, but its location is guaranteed
60 // not to move.
61 const uint32_t cur_serial = __system_property_serial(g_pinfo);
62 if (cur_serial != g_serial) {
63 g_serial = cur_serial;
64 char value[PROP_VALUE_MAX];
65 __system_property_read(g_pinfo, 0, value);
66 g_tags = strtoull(value, NULL, 0);
67 }
68
69 // Finally, verify that this tag value enables bionic tracing.
70 return ((g_tags & ATRACE_TAG_BIONIC) != 0);
71}
72
73ScopedTrace::ScopedTrace(const char* message) {
74 if (!should_trace()) {
75 return;
76 }
77
78 if (g_trace_marker_fd == -1) {
Elliott Hughesf73183f2014-08-26 16:20:59 -070079 g_trace_marker_fd = open("/sys/kernel/debug/tracing/trace_marker", O_CLOEXEC | O_WRONLY);
Brigid Smitha406ee62014-07-21 15:38:06 -070080 if (g_trace_marker_fd == -1) {
81 __libc_fatal("Could not open kernel trace file: %s\n", strerror(errno));
82 }
83 }
84
85 // If bionic tracing has been enabled, then write the message to the
86 // kernel trace_marker.
87 int length = strlen(message);
88 char buf[length + WRITE_OFFSET];
89 size_t len = snprintf(buf, length + WRITE_OFFSET, "B|%d|%s", getpid(), message);
90 ssize_t wbytes = TEMP_FAILURE_RETRY(write(g_trace_marker_fd, buf, len));
91
92 // Error while writing
93 if (static_cast<size_t>(wbytes) != len) {
94 __libc_fatal("Could not write to kernel trace file: %s\n", strerror(errno));
95 }
96}
97
98ScopedTrace::~ScopedTrace() {
99 if (!should_trace()) {
100 return;
101 }
102
103 ssize_t wbytes = TEMP_FAILURE_RETRY(write(g_trace_marker_fd, "E", 1));
104
105 // Error while writing
106 if (static_cast<size_t>(wbytes) != 1) {
107 __libc_fatal("Could not write to kernel trace file: %s\n", strerror(errno));
108 }
109}