1 /*
2  * Copyright (C) 2020 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 "perfetto/tracing/track_event_legacy.h"
18 
19 #include "perfetto/tracing/track.h"
20 
21 namespace perfetto {
22 namespace legacy {
23 
24 template <>
ConvertThreadId(const PerfettoLegacyCurrentThreadId &)25 ThreadTrack ConvertThreadId(const PerfettoLegacyCurrentThreadId&) {
26   // Because of the short-circuit in PERFETTO_INTERNAL_LEGACY_EVENT, we should
27   // never get here.
28   PERFETTO_DCHECK(false);
29   return ThreadTrack::Current();
30 }
31 
32 }  // namespace legacy
33 
34 namespace internal {
35 
Write(protos::pbzero::TrackEvent::LegacyEvent * event,uint32_t event_flags) const36 void LegacyTraceId::Write(protos::pbzero::TrackEvent::LegacyEvent* event,
37                           uint32_t event_flags) const {
38   // Legacy flow events always use bind_id.
39   if (event_flags &
40       (legacy::kTraceEventFlagFlowOut | legacy::kTraceEventFlagFlowIn)) {
41     // Flow bind_ids don't have scopes, so we need to mangle in-process ones to
42     // avoid collisions.
43     if (id_flags_ & legacy::kTraceEventFlagHasLocalId) {
44       event->set_bind_id(raw_id_ ^ ProcessTrack::Current().uuid);
45     } else {
46       event->set_bind_id(raw_id_);
47     }
48     return;
49   }
50 
51   uint32_t scope_flags = id_flags_ & (legacy::kTraceEventFlagHasId |
52                                       legacy::kTraceEventFlagHasLocalId |
53                                       legacy::kTraceEventFlagHasGlobalId);
54   switch (scope_flags) {
55     case legacy::kTraceEventFlagHasId:
56       event->set_unscoped_id(raw_id_);
57       break;
58     case legacy::kTraceEventFlagHasLocalId:
59       event->set_local_id(raw_id_);
60       break;
61     case legacy::kTraceEventFlagHasGlobalId:
62       event->set_global_id(raw_id_);
63       break;
64   }
65   if (scope_)
66     event->set_id_scope(scope_);
67 }
68 
69 }  // namespace internal
70 }  // namespace perfetto
71