xref: /llvm-project/llvm/lib/XRay/FDRTraceExpander.cpp (revision adcd02683856c30ba6f349279509acecd90063df)
1985c2b92SDean Michael Berris //===- FDRTraceExpander.cpp -----------------------------------------------===//
2985c2b92SDean Michael Berris //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6985c2b92SDean Michael Berris //
7985c2b92SDean Michael Berris //===----------------------------------------------------------------------===//
8985c2b92SDean Michael Berris #include "llvm/XRay/FDRTraceExpander.h"
9985c2b92SDean Michael Berris 
10985c2b92SDean Michael Berris namespace llvm {
11985c2b92SDean Michael Berris namespace xray {
12985c2b92SDean Michael Berris 
resetCurrentRecord()13985c2b92SDean Michael Berris void TraceExpander::resetCurrentRecord() {
1425f8d204SDean Michael Berris   if (BuildingRecord)
15985c2b92SDean Michael Berris     C(CurrentRecord);
1625f8d204SDean Michael Berris   BuildingRecord = false;
17985c2b92SDean Michael Berris   CurrentRecord.CallArgs.clear();
1825f8d204SDean Michael Berris   CurrentRecord.Data.clear();
19985c2b92SDean Michael Berris }
20985c2b92SDean Michael Berris 
visit(BufferExtents &)21985c2b92SDean Michael Berris Error TraceExpander::visit(BufferExtents &) {
22985c2b92SDean Michael Berris   resetCurrentRecord();
23985c2b92SDean Michael Berris   return Error::success();
24985c2b92SDean Michael Berris }
25985c2b92SDean Michael Berris 
visit(WallclockRecord &)26985c2b92SDean Michael Berris Error TraceExpander::visit(WallclockRecord &) { return Error::success(); }
27985c2b92SDean Michael Berris 
visit(NewCPUIDRecord & R)28985c2b92SDean Michael Berris Error TraceExpander::visit(NewCPUIDRecord &R) {
29985c2b92SDean Michael Berris   CPUId = R.cpuid();
30985c2b92SDean Michael Berris   BaseTSC = R.tsc();
31985c2b92SDean Michael Berris   return Error::success();
32985c2b92SDean Michael Berris }
33985c2b92SDean Michael Berris 
visit(TSCWrapRecord & R)34985c2b92SDean Michael Berris Error TraceExpander::visit(TSCWrapRecord &R) {
35985c2b92SDean Michael Berris   BaseTSC = R.tsc();
36985c2b92SDean Michael Berris   return Error::success();
37985c2b92SDean Michael Berris }
38985c2b92SDean Michael Berris 
visit(CustomEventRecord & R)3925f8d204SDean Michael Berris Error TraceExpander::visit(CustomEventRecord &R) {
40985c2b92SDean Michael Berris   resetCurrentRecord();
4125f8d204SDean Michael Berris   if (!IgnoringRecords) {
4225f8d204SDean Michael Berris     CurrentRecord.TSC = R.tsc();
4325f8d204SDean Michael Berris     CurrentRecord.CPU = R.cpu();
4425f8d204SDean Michael Berris     CurrentRecord.PId = PID;
4525f8d204SDean Michael Berris     CurrentRecord.TId = TID;
4625f8d204SDean Michael Berris     CurrentRecord.Type = RecordTypes::CUSTOM_EVENT;
47*adcd0268SBenjamin Kramer     CurrentRecord.Data = std::string(R.data());
4825f8d204SDean Michael Berris     BuildingRecord = true;
4925f8d204SDean Michael Berris   }
50985c2b92SDean Michael Berris   return Error::success();
51985c2b92SDean Michael Berris }
52985c2b92SDean Michael Berris 
visit(CustomEventRecordV5 & R)5359439dd0SDean Michael Berris Error TraceExpander::visit(CustomEventRecordV5 &R) {
5459439dd0SDean Michael Berris   resetCurrentRecord();
5559439dd0SDean Michael Berris   if (!IgnoringRecords) {
5659439dd0SDean Michael Berris     BaseTSC += R.delta();
5759439dd0SDean Michael Berris     CurrentRecord.TSC = BaseTSC;
5859439dd0SDean Michael Berris     CurrentRecord.CPU = CPUId;
5959439dd0SDean Michael Berris     CurrentRecord.PId = PID;
6059439dd0SDean Michael Berris     CurrentRecord.TId = TID;
6159439dd0SDean Michael Berris     CurrentRecord.Type = RecordTypes::CUSTOM_EVENT;
62*adcd0268SBenjamin Kramer     CurrentRecord.Data = std::string(R.data());
6359439dd0SDean Michael Berris     BuildingRecord = true;
6459439dd0SDean Michael Berris   }
6559439dd0SDean Michael Berris   return Error::success();
6659439dd0SDean Michael Berris }
6759439dd0SDean Michael Berris 
visit(TypedEventRecord & R)6859439dd0SDean Michael Berris Error TraceExpander::visit(TypedEventRecord &R) {
6959439dd0SDean Michael Berris   resetCurrentRecord();
7059439dd0SDean Michael Berris   if (!IgnoringRecords) {
7159439dd0SDean Michael Berris     BaseTSC += R.delta();
7259439dd0SDean Michael Berris     CurrentRecord.TSC = BaseTSC;
7359439dd0SDean Michael Berris     CurrentRecord.CPU = CPUId;
7459439dd0SDean Michael Berris     CurrentRecord.PId = PID;
7559439dd0SDean Michael Berris     CurrentRecord.TId = TID;
7659439dd0SDean Michael Berris     CurrentRecord.RecordType = R.eventType();
7759439dd0SDean Michael Berris     CurrentRecord.Type = RecordTypes::TYPED_EVENT;
78*adcd0268SBenjamin Kramer     CurrentRecord.Data = std::string(R.data());
7959439dd0SDean Michael Berris     BuildingRecord = true;
8059439dd0SDean Michael Berris   }
8159439dd0SDean Michael Berris   return Error::success();
8259439dd0SDean Michael Berris }
8359439dd0SDean Michael Berris 
visit(CallArgRecord & R)84985c2b92SDean Michael Berris Error TraceExpander::visit(CallArgRecord &R) {
85985c2b92SDean Michael Berris   CurrentRecord.CallArgs.push_back(R.arg());
86985c2b92SDean Michael Berris   CurrentRecord.Type = RecordTypes::ENTER_ARG;
87985c2b92SDean Michael Berris   return Error::success();
88985c2b92SDean Michael Berris }
89985c2b92SDean Michael Berris 
visit(PIDRecord & R)90985c2b92SDean Michael Berris Error TraceExpander::visit(PIDRecord &R) {
91985c2b92SDean Michael Berris   PID = R.pid();
92985c2b92SDean Michael Berris   return Error::success();
93985c2b92SDean Michael Berris }
94985c2b92SDean Michael Berris 
visit(NewBufferRecord & R)95985c2b92SDean Michael Berris Error TraceExpander::visit(NewBufferRecord &R) {
96985c2b92SDean Michael Berris   if (IgnoringRecords)
97985c2b92SDean Michael Berris     IgnoringRecords = false;
98985c2b92SDean Michael Berris   TID = R.tid();
99985c2b92SDean Michael Berris   if (LogVersion == 2)
100985c2b92SDean Michael Berris     PID = R.tid();
101985c2b92SDean Michael Berris   return Error::success();
102985c2b92SDean Michael Berris }
103985c2b92SDean Michael Berris 
visit(EndBufferRecord &)104985c2b92SDean Michael Berris Error TraceExpander::visit(EndBufferRecord &) {
105985c2b92SDean Michael Berris   IgnoringRecords = true;
106985c2b92SDean Michael Berris   resetCurrentRecord();
107985c2b92SDean Michael Berris   return Error::success();
108985c2b92SDean Michael Berris }
109985c2b92SDean Michael Berris 
visit(FunctionRecord & R)110985c2b92SDean Michael Berris Error TraceExpander::visit(FunctionRecord &R) {
111985c2b92SDean Michael Berris   resetCurrentRecord();
112985c2b92SDean Michael Berris   if (!IgnoringRecords) {
113985c2b92SDean Michael Berris     BaseTSC += R.delta();
114985c2b92SDean Michael Berris     CurrentRecord.Type = R.recordType();
115985c2b92SDean Michael Berris     CurrentRecord.FuncId = R.functionId();
116985c2b92SDean Michael Berris     CurrentRecord.TSC = BaseTSC;
117985c2b92SDean Michael Berris     CurrentRecord.PId = PID;
118985c2b92SDean Michael Berris     CurrentRecord.TId = TID;
119985c2b92SDean Michael Berris     CurrentRecord.CPU = CPUId;
12025f8d204SDean Michael Berris     BuildingRecord = true;
121985c2b92SDean Michael Berris   }
122985c2b92SDean Michael Berris   return Error::success();
123985c2b92SDean Michael Berris }
124985c2b92SDean Michael Berris 
flush()125985c2b92SDean Michael Berris Error TraceExpander::flush() {
126985c2b92SDean Michael Berris   resetCurrentRecord();
127985c2b92SDean Michael Berris   return Error::success();
128985c2b92SDean Michael Berris }
129985c2b92SDean Michael Berris 
130985c2b92SDean Michael Berris } // namespace xray
131985c2b92SDean Michael Berris } // namespace llvm
132