--- /dev/null
+//===- RecordPrinter.h - FDR Record Printer -------------------------------===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// An implementation of the RecordVisitor which prints an individual record's
+// data in an adhoc format, suitable for human inspection.
+//
+//===----------------------------------------------------------------------===//
+#ifndef LLVM_INCLUDE_LLVM_XRAY_RECORDPRINTER_H_
+#define LLVM_INCLUDE_LLVM_XRAY_RECORDPRINTER_H_
+
+#include "llvm/Support/raw_ostream.h"
+#include "llvm/XRay/FDRRecords.h"
+
+namespace llvm {
+namespace xray {
+
+class RecordPrinter : public RecordVisitor {
+ raw_ostream &OS;
+ std::string Delim;
+
+public:
+ explicit RecordPrinter(raw_ostream &O, std::string D)
+ : RecordVisitor(), OS(O), Delim(std::move(D)) {}
+
+ explicit RecordPrinter(raw_ostream &O) : RecordPrinter(O, ""){};
+
+ Error visit(BufferExtents &) override;
+ Error visit(WallclockRecord &) override;
+ Error visit(NewCPUIDRecord &) override;
+ Error visit(TSCWrapRecord &) override;
+ Error visit(CustomEventRecord &) override;
+ Error visit(CallArgRecord &) override;
+ Error visit(PIDRecord &) override;
+ Error visit(NewBufferRecord &) override;
+ Error visit(EndBufferRecord &) override;
+ Error visit(FunctionRecord &) override;
+};
+
+} // namespace xray
+} // namespace llvm
+
+#endif // LLVM_INCLUDE_LLVM_XRAY_RECORDPRINTER_H
--- /dev/null
+//===- RecordPrinter.cpp - FDR Record Printer -----------------------------===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+#include "llvm/XRay/RecordPrinter.h"
+
+#include "llvm/Support/FormatVariadic.h"
+
+namespace llvm {
+namespace xray {
+
+Error RecordPrinter::visit(BufferExtents &R) {
+ OS << formatv("<Buffer: size = {0} bytes>", R.size()) << Delim;
+ return Error::success();
+}
+
+Error RecordPrinter::visit(WallclockRecord &R) {
+ OS << formatv("<Wall Time: seconds = {0}.{1,0+6}>", R.seconds(), R.nanos())
+ << Delim;
+ return Error::success();
+}
+
+Error RecordPrinter::visit(NewCPUIDRecord &R) {
+ OS << formatv("<CPU ID: {0}>", R.cpuid()) << Delim;
+ return Error::success();
+}
+
+Error RecordPrinter::visit(TSCWrapRecord &R) {
+ OS << formatv("<TSC Wrap: base = {0}>", R.tsc()) << Delim;
+ return Error::success();
+}
+
+Error RecordPrinter::visit(CustomEventRecord &R) {
+ OS << formatv("<Custom Event: tsc = {0}, size = {1}, data = '{2}'>", R.tsc(),
+ R.size(), R.data())
+ << Delim;
+ return Error::success();
+}
+
+Error RecordPrinter::visit(CallArgRecord &R) {
+ OS << formatv("<Call Argument: data = {0} (hex = {0:x})>", R.arg()) << Delim;
+ return Error::success();
+}
+
+Error RecordPrinter::visit(PIDRecord &R) {
+ OS << formatv("<PID: {0}>", R.pid()) << Delim;
+ return Error::success();
+}
+
+Error RecordPrinter::visit(NewBufferRecord &R) {
+ OS << formatv("<Thread ID: {0}>", R.tid()) << Delim;
+ return Error::success();
+}
+
+Error RecordPrinter::visit(EndBufferRecord &R) {
+ OS << "<End of Buffer>" << Delim;
+ return Error::success();
+}
+
+Error RecordPrinter::visit(FunctionRecord &R) {
+ // FIXME: Support symbolization here?
+ switch (R.recordType()) {
+ case RecordTypes::ENTER:
+ OS << formatv("<Function Enter: #{0} delta = +{0}>", R.functionId(),
+ R.delta());
+ break;
+ case RecordTypes::ENTER_ARG:
+ OS << formatv("<Function Enter With Arg: #{0} delta = +{0}>",
+ R.functionId(), R.delta());
+ break;
+ case RecordTypes::EXIT:
+ OS << formatv("<Function Exit: #{0} delta = +{0}>", R.functionId(),
+ R.delta());
+ break;
+ case RecordTypes::TAIL_EXIT:
+ OS << formatv("<Function Tail Exit: #{0} delta = +{0}>", R.functionId(),
+ R.delta());
+ break;
+ }
+ OS << Delim;
+ return Error::success();
+}
+
+} // namespace xray
+} // namespace llvm
--- /dev/null
+//===- llvm/unittest/XRay/FDRRecordPrinterTest.cpp --------------*- C++ -*-===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+#include "llvm/Support/raw_ostream.h"
+#include "llvm/XRay/FDRRecords.h"
+#include "llvm/XRay/RecordPrinter.h"
+#include "gmock/gmock.h"
+#include "gtest/gtest.h"
+#include <string>
+
+namespace llvm {
+namespace xray {
+namespace {
+
+using ::testing::Eq;
+
+template <class RecordType> struct Helper {};
+
+template <> struct Helper<BufferExtents> {
+ static std::unique_ptr<Record> construct() {
+ return make_unique<BufferExtents>(1);
+ }
+
+ static constexpr char Expected[] = "<Buffer: size = 1 bytes>";
+};
+constexpr char Helper<BufferExtents>::Expected[];
+
+template <> struct Helper<WallclockRecord> {
+ static std::unique_ptr<Record> construct() {
+ return make_unique<WallclockRecord>(1, 2);
+ }
+
+ static constexpr char Expected[] = "<Wall Time: seconds = 1.000002>";
+};
+constexpr char Helper<WallclockRecord>::Expected[];
+
+template <> struct Helper<NewCPUIDRecord> {
+ static std::unique_ptr<Record> construct() {
+ return make_unique<NewCPUIDRecord>(1);
+ }
+
+ static constexpr char Expected[] = "<CPU ID: 1>";
+};
+constexpr char Helper<NewCPUIDRecord>::Expected[];
+
+template <> struct Helper<TSCWrapRecord> {
+ static std::unique_ptr<Record> construct() {
+ return make_unique<TSCWrapRecord>(1);
+ }
+
+ static constexpr char Expected[] = "<TSC Wrap: base = 1>";
+};
+constexpr char Helper<TSCWrapRecord>::Expected[];
+
+template <> struct Helper<CustomEventRecord> {
+ static std::unique_ptr<Record> construct() {
+ return make_unique<CustomEventRecord>(4, 1, "data");
+ }
+
+ static constexpr char Expected[] =
+ "<Custom Event: tsc = 1, size = 4, data = 'data'>";
+};
+constexpr char Helper<CustomEventRecord>::Expected[];
+
+template <> struct Helper<CallArgRecord> {
+ static std::unique_ptr<Record> construct() {
+ return make_unique<CallArgRecord>(1);
+ }
+
+ static constexpr char Expected[] = "<Call Argument: data = 1 (hex = 0x1)>";
+};
+constexpr char Helper<CallArgRecord>::Expected[];
+
+template <> struct Helper<PIDRecord> {
+ static std::unique_ptr<Record> construct() {
+ return make_unique<PIDRecord>(1);
+ }
+
+ static constexpr char Expected[] = "<PID: 1>";
+};
+constexpr char Helper<PIDRecord>::Expected[];
+
+template <> struct Helper<NewBufferRecord> {
+ static std::unique_ptr<Record> construct() {
+ return make_unique<NewBufferRecord>(1);
+ }
+
+ static constexpr char Expected[] = "<Thread ID: 1>";
+};
+constexpr char Helper<NewBufferRecord>::Expected[];
+
+template <> struct Helper<EndBufferRecord> {
+ static std::unique_ptr<Record> construct() {
+ return make_unique<EndBufferRecord>();
+ }
+
+ static constexpr char Expected[] = "<End of Buffer>";
+};
+constexpr char Helper<EndBufferRecord>::Expected[];
+
+template <class T> class PrinterTest : public ::testing::Test {
+protected:
+ std::string Data;
+ raw_string_ostream OS;
+ RecordPrinter P;
+ std::unique_ptr<Record> R;
+
+public:
+ PrinterTest() : Data(), OS(Data), P(OS), R(Helper<T>::construct()) {}
+};
+
+TYPED_TEST_CASE_P(PrinterTest);
+
+TYPED_TEST_P(PrinterTest, PrintsRecord) {
+ ASSERT_NE(nullptr, this->R);
+ ASSERT_FALSE(errorToBool(this->R->apply(this->P)));
+ this->OS.flush();
+ EXPECT_THAT(this->Data, Eq(Helper<TypeParam>::Expected));
+}
+
+REGISTER_TYPED_TEST_CASE_P(PrinterTest, PrintsRecord);
+using FDRRecordTypes =
+ ::testing::Types<BufferExtents, NewBufferRecord, EndBufferRecord,
+ NewCPUIDRecord, TSCWrapRecord, WallclockRecord,
+ CustomEventRecord, CallArgRecord, BufferExtents,
+ PIDRecord>;
+INSTANTIATE_TYPED_TEST_CASE_P(Records, PrinterTest, FDRRecordTypes);
+
+TEST(FDRRecordPrinterTest, WriteFunctionRecordEnter) {
+ std::string Data;
+ raw_string_ostream OS(Data);
+ RecordPrinter P(OS);
+ FunctionRecord R(RecordTypes::ENTER, 1, 2);
+ ASSERT_FALSE(errorToBool(R.apply(P)));
+ OS.flush();
+ EXPECT_THAT(Data, Eq("<Function Enter: #1 delta = +1>"));
+}
+
+TEST(FDRRecordPrinterTest, WriteFunctionRecordExit) {
+ std::string Data;
+ raw_string_ostream OS(Data);
+ RecordPrinter P(OS);
+ FunctionRecord R(RecordTypes::EXIT, 1, 2);
+ ASSERT_FALSE(errorToBool(R.apply(P)));
+ OS.flush();
+ EXPECT_THAT(Data, Eq("<Function Exit: #1 delta = +1>"));
+}
+
+TEST(FDRRecordPrinterTest, WriteFunctionRecordTailExit) {
+ std::string Data;
+ raw_string_ostream OS(Data);
+ RecordPrinter P(OS);
+ FunctionRecord R(RecordTypes::TAIL_EXIT, 1, 2);
+ ASSERT_FALSE(errorToBool(R.apply(P)));
+ OS.flush();
+ EXPECT_THAT(Data, Eq("<Function Tail Exit: #1 delta = +1>"));
+}
+
+TEST(FDRRecordPrinterTest, WriteFunctionRecordEnterArg) {
+ std::string Data;
+ raw_string_ostream OS(Data);
+ RecordPrinter P(OS);
+ FunctionRecord R(RecordTypes::ENTER_ARG, 1, 2);
+ ASSERT_FALSE(errorToBool(R.apply(P)));
+ OS.flush();
+ EXPECT_THAT(Data, Eq("<Function Enter With Arg: #1 delta = +1>"));
+}
+
+} // namespace
+} // namespace xray
+} // namespace llvm