1// Copyright (c) 2012 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "ppapi/shared_impl/ppb_trace_event_impl.h"
6
7#include "base/basictypes.h"
8#include "base/debug/trace_event.h"
9#include "base/threading/platform_thread.h"
10#include "ppapi/thunk/thunk.h"
11
12
13namespace ppapi {
14
15// PPB_Trace_Event_Dev is a shared implementation because Trace Events can be
16// sent from either the plugin process or renderer process depending on whether
17// the plugin is in- or out-of-process.  Also, for NaCl plugins these functions
18// will be executed from untrusted code and handled appropriately by tracing
19// functionality in the IRT.
20
21// static
22void* TraceEventImpl::GetCategoryEnabled(const char* category_name) {
23  // This casting is here because all mem_t return types in Pepper are void* and
24  // non-const.  All mem_t parameters are const void* so there is no way to
25  // return a pointer type to the caller without some const_cast.  The pointer
26  // type the tracing system works with is normally unsigned char*.
27  return const_cast<void*>(static_cast<const void*>(
28      base::debug::TraceLog::GetInstance()->GetCategoryGroupEnabled(
29          category_name)));
30}
31
32// static
33void TraceEventImpl::AddTraceEvent(
34    int8_t phase,
35    const void* category_enabled,
36    const char* name,
37    uint64_t id,
38    uint32_t num_args,
39    const char* arg_names[],
40    const uint8_t arg_types[],
41    const uint64_t arg_values[],
42    uint8_t flags) {
43
44  COMPILE_ASSERT(sizeof(unsigned long long) == sizeof(uint64_t), msg);
45
46  base::debug::TraceLog::GetInstance()->AddTraceEvent(phase,
47      static_cast<const unsigned char*>(category_enabled), name, id, num_args,
48      arg_names, arg_types,
49      // This cast is necessary for LP64 systems, where uint64_t is defined as
50      // an unsigned long int, but trace_event internals are hermetic and
51      // accepts an |unsigned long long*|.  The pointer types are compatible but
52      // the compiler throws an error without an explicit cast.
53      reinterpret_cast<const unsigned long long*>(arg_values), NULL, flags);
54}
55
56// static
57void TraceEventImpl::AddTraceEventWithThreadIdAndTimestamp(
58    int8_t phase,
59    const void* category_enabled,
60    const char* name,
61    uint64_t id,
62    int32_t thread_id,
63    int64_t timestamp,
64    uint32_t num_args,
65    const char* arg_names[],
66    const uint8_t arg_types[],
67    const uint64_t arg_values[],
68    uint8_t flags) {
69  base::debug::TraceLog::GetInstance()->AddTraceEventWithThreadIdAndTimestamp(
70      phase,
71      static_cast<const unsigned char*>(category_enabled), name, id,
72      thread_id,
73      base::TimeTicks::FromInternalValue(timestamp),
74      num_args, arg_names, arg_types,
75      // This cast is necessary for LP64 systems, where uint64_t is defined as
76      // an unsigned long int, but trace_event internals are hermetic and
77      // accepts an |unsigned long long*|.  The pointer types are compatible but
78      // the compiler throws an error without an explicit cast.
79      reinterpret_cast<const unsigned long long*>(arg_values), NULL, flags);
80}
81
82// static
83int64_t TraceEventImpl::Now() {
84  return base::TimeTicks::NowFromSystemTraceTime().ToInternalValue();
85}
86
87// static
88void TraceEventImpl::SetThreadName(const char* thread_name) {
89  base::PlatformThread::SetName(thread_name);
90}
91
92namespace {
93
94const PPB_Trace_Event_Dev_0_1 g_ppb_trace_event_thunk_0_1 = {
95  &TraceEventImpl::GetCategoryEnabled,
96  &TraceEventImpl::AddTraceEvent,
97  &TraceEventImpl::SetThreadName,
98};
99
100const PPB_Trace_Event_Dev_0_2 g_ppb_trace_event_thunk_0_2 = {
101  &TraceEventImpl::GetCategoryEnabled,
102  &TraceEventImpl::AddTraceEvent,
103  &TraceEventImpl::AddTraceEventWithThreadIdAndTimestamp,
104  &TraceEventImpl::Now,
105  &TraceEventImpl::SetThreadName,
106};
107
108}  // namespace ppapi
109
110}  // namespace
111
112namespace ppapi {
113namespace thunk {
114
115const PPB_Trace_Event_Dev_0_1* GetPPB_Trace_Event_Dev_0_1_Thunk() {
116  return &g_ppb_trace_event_thunk_0_1;
117}
118
119const PPB_Trace_Event_Dev_0_2* GetPPB_Trace_Event_Dev_0_2_Thunk() {
120  return &g_ppb_trace_event_thunk_0_2;
121}
122
123}  // namespace thunk
124}  // namespace ppapi
125