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
17 // This tool provides analytical information about a bitcode file. It is
18 // intended as an aid to developers of bitcode reading and writing software. It
19 // produces on std::out a summary of the bitcode file that shows various
20 // statistics about the contents of the file. By default this information is
21 // detailed and contains information about individual bitcode blocks and the
22 // functions in the module.
23 // The tool is also able to print a bitcode file in a straight forward text
24 // format that shows the containment and relationships of the information in
25 // the bitcode file (-dump option).
27 //===----------------------------------------------------------------------===//
29 #include "llvm/ADT/Optional.h"
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"
40 static cl::OptionCategory
BCAnalyzerCategory("BC Analyzer Options");
42 static cl::opt
<std::string
> InputFilename(cl::Positional
,
43 cl::desc("<input bitcode>"),
45 cl::cat(BCAnalyzerCategory
));
47 static cl::opt
<bool> Dump("dump", cl::desc("Dump low level bitcode trace"),
48 cl::cat(BCAnalyzerCategory
));
50 //===----------------------------------------------------------------------===//
51 // Bitcode specific analysis.
52 //===----------------------------------------------------------------------===//
54 static cl::opt
<bool> NoHistogram("disable-histogram",
55 cl::desc("Do not print per-code histogram"),
56 cl::cat(BCAnalyzerCategory
));
58 static cl::opt
<bool> NonSymbolic("non-symbolic",
59 cl::desc("Emit numeric info in dump even if"
60 " symbolic info is available"),
61 cl::cat(BCAnalyzerCategory
));
63 static cl::opt
<std::string
>
64 BlockInfoFilename("block-info",
65 cl::desc("Use the BLOCK_INFO from the given file"),
66 cl::cat(BCAnalyzerCategory
));
69 ShowBinaryBlobs("show-binary-blobs",
70 cl::desc("Print binary blobs using hex escapes"),
71 cl::cat(BCAnalyzerCategory
));
73 static cl::opt
<std::string
> CheckHash(
75 cl::desc("Check module hash using the argument as a string table"),
76 cl::cat(BCAnalyzerCategory
));
78 static Error
reportError(StringRef Message
) {
79 return createStringError(std::errc::illegal_byte_sequence
, Message
.data());
82 static Expected
<std::unique_ptr
<MemoryBuffer
>> openBitcodeFile(StringRef Path
) {
83 // Read the input file.
84 Expected
<std::unique_ptr
<MemoryBuffer
>> MemBufOrErr
=
85 errorOrToExpected(MemoryBuffer::getFileOrSTDIN(Path
));
86 if (Error E
= MemBufOrErr
.takeError())
89 std::unique_ptr
<MemoryBuffer
> MemBuf
= std::move(*MemBufOrErr
);
91 if (MemBuf
->getBufferSize() & 3)
93 "Bitcode stream should be a multiple of 4 bytes in length");
94 return std::move(MemBuf
);
97 int main(int argc
, char **argv
) {
98 InitLLVM
X(argc
, argv
);
100 cl::HideUnrelatedOptions({&BCAnalyzerCategory
, &getColorCategory()});
101 cl::ParseCommandLineOptions(argc
, argv
, "llvm-bcanalyzer file analyzer\n");
102 ExitOnError
ExitOnErr("llvm-bcanalyzer: ");
104 std::unique_ptr
<MemoryBuffer
> MB
= ExitOnErr(openBitcodeFile(InputFilename
));
105 std::unique_ptr
<MemoryBuffer
> BlockInfoMB
= nullptr;
106 if (!BlockInfoFilename
.empty())
107 BlockInfoMB
= ExitOnErr(openBitcodeFile(BlockInfoFilename
));
109 BitcodeAnalyzer
BA(MB
->getBuffer(),
110 BlockInfoMB
? Optional
<StringRef
>(BlockInfoMB
->getBuffer())
113 BCDumpOptions
O(outs());
114 O
.Histogram
= !NoHistogram
;
115 O
.Symbolic
= !NonSymbolic
;
116 O
.ShowBinaryBlobs
= ShowBinaryBlobs
;
118 ExitOnErr(BA
.analyze(
119 Dump
? Optional
<BCDumpOptions
>(O
) : Optional
<BCDumpOptions
>(None
),
120 CheckHash
.empty() ? None
: Optional
<StringRef
>(CheckHash
)));
125 BA
.printStats(O
, StringRef(InputFilename
.getValue()));