summaryrefslogtreecommitdiffstats
path: root/base/debug/trace_event_android.cc
blob: 29e684a659d6e17aa51f0087aae3860b16d4c206 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/debug/trace_event_impl.h"

#include <fcntl.h>

#include "base/debug/trace_event.h"
#include "base/logging.h"
#include "base/stringprintf.h"

namespace {

int g_atrace_fd = -1;
const char* kATraceMarkerFile = "/sys/kernel/debug/tracing/trace_marker";

}  // namespace

namespace base {
namespace debug {

// static
void TraceLog::InitATrace() {
  DCHECK(g_atrace_fd == -1);
  g_atrace_fd = open(kATraceMarkerFile, O_WRONLY | O_APPEND);
  if (g_atrace_fd == -1)
    LOG(WARNING) << "Couldn't open " << kATraceMarkerFile;
}

void TraceLog::SendToATrace(char phase,
                            const char* category,
                            const char* name,
                            int num_args,
                            const char** arg_names,
                            const unsigned char* arg_types,
                            const unsigned long long* arg_values) {
  if (g_atrace_fd == -1)
    return;

  switch (phase) {
    case TRACE_EVENT_PHASE_BEGIN:
    case TRACE_EVENT_PHASE_INSTANT: {
      std::string out = StringPrintf("B|%d|%s-%s", getpid(), category, name);
      for (int i = 0; i < num_args; ++i) {
        out += (i == 0 ? '|' : ';');
        out += arg_names[i];
        out += '=';
        TraceEvent::TraceValue value;
        value.as_uint = arg_values[i];
        std::string::size_type value_start = out.length();
        TraceEvent::AppendValueAsJSON(arg_types[i], value, &out);
        // Remove the quotes which may confuse the atrace script.
        ReplaceSubstringsAfterOffset(&out, value_start, "\\\"", "'");
        ReplaceSubstringsAfterOffset(&out, value_start, "\"", "");
        // Replace chars used for separators with similar chars in the value.
        std::replace(out.begin() + value_start, out.end(), ';', ',');
        std::replace(out.begin() + value_start, out.end(), '|', '!');
      }
      write(g_atrace_fd, out.c_str(), out.size());

      if (phase != TRACE_EVENT_PHASE_INSTANT)
        break;
      // Fall through. Simulate an instance event with a pair of begin/end.
    }
    case TRACE_EVENT_PHASE_END: {
      // Though a single 'E' is enough, here append pid and name so that
      // unpaired events can be found easily.
      std::string out = StringPrintf("E|%d|%s-%s", getpid(), category, name);
      write(g_atrace_fd, out.c_str(), out.size());
      break;
    }
    case TRACE_EVENT_PHASE_COUNTER:
      for (int i = 0; i < num_args; ++i) {
        DCHECK(arg_types[i] == TRACE_VALUE_TYPE_INT);
        std::string out = StringPrintf(
            "C|%d|%s-%s-%s|%d",
            getpid(), category, name,
            arg_names[i], static_cast<int>(arg_values[i]));
        write(g_atrace_fd, out.c_str(), out.size());
      }
      break;

    default:
      // Do nothing.
      break;
  }
}

// Must be called with lock_ locked.
void TraceLog::ApplyATraceEnabledFlag(unsigned char* category_enabled) {
  if (g_atrace_fd != -1)
    *category_enabled |= ATRACE_ENABLED;
}

}  // namespace debug
}  // namespace base