1 //===-- llvm-bcanalyzer.cpp - Bitcode Analyzer --------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 // This tool may be invoked in the following manner:
10 // llvm-bcanalyzer [options] - Read LLVM bitcode from stdin
11 // llvm-bcanalyzer [options] x.bc - Read LLVM bitcode from the x.bc file
14 // --help - Output information about command line switches
15 // --dump - Dump low-level bitcode structure in readable format
16 // --dump-blockinfo - Dump the BLOCKINFO_BLOCK, when used with --dump
18 // This tool provides analytical information about a bitcode file. It is
19 // intended as an aid to developers of bitcode reading and writing software. It
20 // produces on std::out a summary of the bitcode file that shows various
21 // statistics about the contents of the file. By default this information is
22 // detailed and contains information about individual bitcode blocks and the
23 // functions in the module.
24 // The tool is also able to print a bitcode file in a straight forward text
25 // format that shows the containment and relationships of the information in
26 // the bitcode file (-dump option).
28 //===----------------------------------------------------------------------===//
30 #include "llvm/Bitcode/BitcodeAnalyzer.h"
31 #include "llvm/Support/CommandLine.h"
32 #include "llvm/Support/Error.h"
33 #include "llvm/Support/InitLLVM.h"
34 #include "llvm/Support/MemoryBuffer.h"
35 #include "llvm/Support/WithColor.h"
36 #include "llvm/Support/raw_ostream.h"
41 static cl::OptionCategory
BCAnalyzerCategory("BC Analyzer Options");
43 static cl::opt
<std::string
> InputFilename(cl::Positional
,
44 cl::desc("<input bitcode>"),
46 cl::cat(BCAnalyzerCategory
));
48 static cl::opt
<bool> Dump("dump", cl::desc("Dump low level bitcode trace"),
49 cl::cat(BCAnalyzerCategory
));
51 static cl::opt
<bool> DumpBlockinfo("dump-blockinfo",
52 cl::desc("Include BLOCKINFO details in low"
54 cl::cat(BCAnalyzerCategory
));
56 //===----------------------------------------------------------------------===//
57 // Bitcode specific analysis.
58 //===----------------------------------------------------------------------===//
60 static cl::opt
<bool> NoHistogram("disable-histogram",
61 cl::desc("Do not print per-code histogram"),
62 cl::cat(BCAnalyzerCategory
));
64 static cl::opt
<bool> NonSymbolic("non-symbolic",
65 cl::desc("Emit numeric info in dump even if"
66 " symbolic info is available"),
67 cl::cat(BCAnalyzerCategory
));
69 static cl::opt
<std::string
>
70 BlockInfoFilename("block-info",
71 cl::desc("Use the BLOCK_INFO from the given file"),
72 cl::cat(BCAnalyzerCategory
));
75 ShowBinaryBlobs("show-binary-blobs",
76 cl::desc("Print binary blobs using hex escapes"),
77 cl::cat(BCAnalyzerCategory
));
79 static cl::opt
<std::string
> CheckHash(
81 cl::desc("Check module hash using the argument as a string table"),
82 cl::cat(BCAnalyzerCategory
));
84 static Error
reportError(StringRef Message
) {
85 return createStringError(std::errc::illegal_byte_sequence
, Message
.data());
88 static Expected
<std::unique_ptr
<MemoryBuffer
>> openBitcodeFile(StringRef Path
) {
89 // Read the input file.
90 Expected
<std::unique_ptr
<MemoryBuffer
>> MemBufOrErr
=
91 errorOrToExpected(MemoryBuffer::getFileOrSTDIN(Path
));
92 if (Error E
= MemBufOrErr
.takeError())
95 std::unique_ptr
<MemoryBuffer
> MemBuf
= std::move(*MemBufOrErr
);
97 if (MemBuf
->getBufferSize() & 3)
99 "Bitcode stream should be a multiple of 4 bytes in length");
100 return std::move(MemBuf
);
103 int main(int argc
, char **argv
) {
104 InitLLVM
X(argc
, argv
);
106 cl::HideUnrelatedOptions({&BCAnalyzerCategory
, &getColorCategory()});
107 cl::ParseCommandLineOptions(argc
, argv
, "llvm-bcanalyzer file analyzer\n");
108 ExitOnError
ExitOnErr("llvm-bcanalyzer: ");
110 std::unique_ptr
<MemoryBuffer
> MB
= ExitOnErr(openBitcodeFile(InputFilename
));
111 std::unique_ptr
<MemoryBuffer
> BlockInfoMB
= nullptr;
112 if (!BlockInfoFilename
.empty())
113 BlockInfoMB
= ExitOnErr(openBitcodeFile(BlockInfoFilename
));
115 BitcodeAnalyzer
BA(MB
->getBuffer(),
117 ? std::optional
<StringRef
>(BlockInfoMB
->getBuffer())
120 BCDumpOptions
O(outs());
121 O
.Histogram
= !NoHistogram
;
122 O
.Symbolic
= !NonSymbolic
;
123 O
.ShowBinaryBlobs
= ShowBinaryBlobs
;
124 O
.DumpBlockinfo
= DumpBlockinfo
;
126 ExitOnErr(BA
.analyze(
127 Dump
? std::optional
<BCDumpOptions
>(O
) : std::optional
<BCDumpOptions
>(),
128 CheckHash
.empty() ? std::nullopt
: std::optional
<StringRef
>(CheckHash
)));
133 BA
.printStats(O
, StringRef(InputFilename
.getValue()));