Revert r354244 "[DAGCombiner] Eliminate dead stores to stack."
[llvm-complete.git] / tools / llvm-xray / xray-fdr-dump.cpp
blobe0c07039a8914bf215f7d5e7a668436f1ef957d6
1 //===- xray-fdr-dump.cpp: XRay FDR Trace Dump Tool ------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // Implements the FDR trace dumping tool, using the libraries for handling FDR
10 // mode traces specifically.
12 //===----------------------------------------------------------------------===//
13 #include "xray-registry.h"
14 #include "llvm/Support/CommandLine.h"
15 #include "llvm/Support/FileSystem.h"
16 #include "llvm/XRay/BlockIndexer.h"
17 #include "llvm/XRay/BlockPrinter.h"
18 #include "llvm/XRay/BlockVerifier.h"
19 #include "llvm/XRay/FDRRecordConsumer.h"
20 #include "llvm/XRay/FDRRecordProducer.h"
21 #include "llvm/XRay/FDRRecords.h"
22 #include "llvm/XRay/FileHeaderReader.h"
23 #include "llvm/XRay/RecordPrinter.h"
25 using namespace llvm;
26 using namespace xray;
28 static cl::SubCommand Dump("fdr-dump", "FDR Trace Dump");
29 static cl::opt<std::string> DumpInput(cl::Positional,
30 cl::desc("<xray fdr mode log>"),
31 cl::Required, cl::sub(Dump));
32 static cl::opt<bool> DumpVerify("verify",
33 cl::desc("verify structure of the log"),
34 cl::init(false), cl::sub(Dump));
36 static CommandRegistration Unused(&Dump, []() -> Error {
37 // Open the file provided.
38 int Fd;
39 if (auto EC = sys::fs::openFileForRead(DumpInput, Fd))
40 return createStringError(EC, "Cannot open file '%s' for read.",
41 DumpInput.c_str());
43 uint64_t FileSize;
44 if (auto EC = sys::fs::file_size(DumpInput, FileSize))
45 return createStringError(EC, "Failed to get file size for '%s'.",
46 DumpInput.c_str());
48 std::error_code EC;
49 sys::fs::mapped_file_region MappedFile(
50 Fd, sys::fs::mapped_file_region::mapmode::readonly, FileSize, 0, EC);
52 DataExtractor DE(StringRef(MappedFile.data(), MappedFile.size()), true, 8);
53 uint32_t OffsetPtr = 0;
55 auto FileHeaderOrError = readBinaryFormatHeader(DE, OffsetPtr);
56 if (!FileHeaderOrError)
57 return FileHeaderOrError.takeError();
58 auto &H = FileHeaderOrError.get();
60 FileBasedRecordProducer P(H, DE, OffsetPtr);
62 RecordPrinter RP(outs(), "\n");
63 if (!DumpVerify) {
64 PipelineConsumer C({&RP});
65 while (DE.isValidOffsetForDataOfSize(OffsetPtr, 1)) {
66 auto R = P.produce();
67 if (!R)
68 return R.takeError();
69 if (auto E = C.consume(std::move(R.get())))
70 return E;
72 return Error::success();
75 BlockPrinter BP(outs(), RP);
76 std::vector<std::unique_ptr<Record>> Records;
77 LogBuilderConsumer C(Records);
78 while (DE.isValidOffsetForDataOfSize(OffsetPtr, 1)) {
79 auto R = P.produce();
80 if (!R) {
81 // Print records we've found so far.
82 for (auto &Ptr : Records)
83 if (auto E = Ptr->apply(RP))
84 return joinErrors(std::move(E), R.takeError());
85 return R.takeError();
87 if (auto E = C.consume(std::move(R.get())))
88 return E;
91 // Once we have a trace, we then index the blocks.
92 BlockIndexer::Index Index;
93 BlockIndexer BI(Index);
94 for (auto &Ptr : Records)
95 if (auto E = Ptr->apply(BI))
96 return E;
98 if (auto E = BI.flush())
99 return E;
101 // Then we validate while printing each block.
102 BlockVerifier BV;
103 for (auto ProcessThreadBlocks : Index) {
104 auto &Blocks = ProcessThreadBlocks.second;
105 for (auto &B : Blocks) {
106 for (auto *R : B.Records) {
107 if (auto E = R->apply(BV))
108 return E;
109 if (auto E = R->apply(BP))
110 return E;
112 BV.reset();
113 BP.reset();
116 outs().flush();
117 return Error::success();