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