Home | History | Annotate | Download | only in bionic
      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 <errno.h>
     18 #include <fcntl.h>
     19 #include <stdio.h>
     20 #include <stdlib.h>
     21 #include <string.h>
     22 
     23 #include "private/bionic_lock.h"
     24 #include "private/bionic_systrace.h"
     25 #include "private/CachedProperty.h"
     26 
     27 #include <cutils/trace.h> // For ATRACE_TAG_BIONIC.
     28 
     29 #define WRITE_OFFSET   32
     30 
     31 static Lock g_lock;
     32 static int g_trace_marker_fd = -1;
     33 
     34 static bool should_trace() {
     35   static CachedProperty g_debug_atrace_tags_enableflags("debug.atrace.tags.enableflags");
     36   static uint64_t g_tags;
     37 
     38   g_lock.lock();
     39   if (g_debug_atrace_tags_enableflags.DidChange()) {
     40     g_tags = strtoull(g_debug_atrace_tags_enableflags.Get(), nullptr, 0);
     41   }
     42   g_lock.unlock();
     43   return ((g_tags & ATRACE_TAG_BIONIC) != 0);
     44 }
     45 
     46 static int get_trace_marker_fd() {
     47   g_lock.lock();
     48   if (g_trace_marker_fd == -1) {
     49     g_trace_marker_fd = open("/sys/kernel/debug/tracing/trace_marker", O_CLOEXEC | O_WRONLY);
     50   }
     51   g_lock.unlock();
     52   return g_trace_marker_fd;
     53 }
     54 
     55 void bionic_trace_begin(const char* message) {
     56   if (!should_trace()) {
     57     return;
     58   }
     59 
     60   int trace_marker_fd = get_trace_marker_fd();
     61   if (trace_marker_fd == -1) {
     62     return;
     63   }
     64 
     65   // If bionic tracing has been enabled, then write the message to the
     66   // kernel trace_marker.
     67   int length = strlen(message);
     68   char buf[length + WRITE_OFFSET];
     69   size_t len = snprintf(buf, length + WRITE_OFFSET, "B|%d|%s", getpid(), message);
     70 
     71   // Tracing may stop just after checking property and before writing the message.
     72   // So the write is acceptable to fail. See b/20666100.
     73   TEMP_FAILURE_RETRY(write(trace_marker_fd, buf, len));
     74 }
     75 
     76 void bionic_trace_end() {
     77   if (!should_trace()) {
     78     return;
     79   }
     80 
     81   int trace_marker_fd = get_trace_marker_fd();
     82   if (trace_marker_fd == -1) {
     83     return;
     84   }
     85 
     86   TEMP_FAILURE_RETRY(write(trace_marker_fd, "E", 1));
     87 }
     88 
     89 ScopedTrace::ScopedTrace(const char* message) : called_end_(false) {
     90   bionic_trace_begin(message);
     91 }
     92 
     93 ScopedTrace::~ScopedTrace() {
     94   End();
     95 }
     96 
     97 void ScopedTrace::End() {
     98   if (!called_end_) {
     99     bionic_trace_end();
    100     called_end_ = true;
    101   }
    102 }
    103