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