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/raw_ostream.h"
39 static cl::opt
<std::string
>
40 InputFilename(cl::Positional
, cl::desc("<input bitcode>"), cl::init("-"));
42 static cl::opt
<bool> Dump("dump", cl::desc("Dump low level bitcode trace"));
44 //===----------------------------------------------------------------------===//
45 // Bitcode specific analysis.
46 //===----------------------------------------------------------------------===//
48 static cl::opt
<bool> NoHistogram("disable-histogram",
49 cl::desc("Do not print per-code histogram"));
51 static cl::opt
<bool> NonSymbolic("non-symbolic",
52 cl::desc("Emit numeric info in dump even if"
53 " symbolic info is available"));
55 static cl::opt
<std::string
>
56 BlockInfoFilename("block-info",
57 cl::desc("Use the BLOCK_INFO from the given file"));
60 ShowBinaryBlobs("show-binary-blobs",
61 cl::desc("Print binary blobs using hex escapes"));
63 static cl::opt
<std::string
> CheckHash(
65 cl::desc("Check module hash using the argument as a string table"));
67 static Error
reportError(StringRef Message
) {
68 return createStringError(std::errc::illegal_byte_sequence
, Message
.data());
71 static Expected
<std::unique_ptr
<MemoryBuffer
>> openBitcodeFile(StringRef Path
) {
72 // Read the input file.
73 Expected
<std::unique_ptr
<MemoryBuffer
>> MemBufOrErr
=
74 errorOrToExpected(MemoryBuffer::getFileOrSTDIN(Path
));
75 if (Error E
= MemBufOrErr
.takeError())
78 std::unique_ptr
<MemoryBuffer
> MemBuf
= std::move(*MemBufOrErr
);
80 if (MemBuf
->getBufferSize() & 3)
82 "Bitcode stream should be a multiple of 4 bytes in length");
83 return std::move(MemBuf
);
86 int main(int argc
, char **argv
) {
87 InitLLVM
X(argc
, argv
);
88 cl::ParseCommandLineOptions(argc
, argv
, "llvm-bcanalyzer file analyzer\n");
89 ExitOnError
ExitOnErr("llvm-bcanalyzer: ");
91 std::unique_ptr
<MemoryBuffer
> MB
= ExitOnErr(openBitcodeFile(InputFilename
));
92 std::unique_ptr
<MemoryBuffer
> BlockInfoMB
= nullptr;
93 if (!BlockInfoFilename
.empty())
94 BlockInfoMB
= ExitOnErr(openBitcodeFile(BlockInfoFilename
));
96 BitcodeAnalyzer
BA(MB
->getBuffer(),
97 BlockInfoMB
? Optional
<StringRef
>(BlockInfoMB
->getBuffer())
100 BCDumpOptions
O(outs());
101 O
.Histogram
= !NoHistogram
;
102 O
.Symbolic
= !NonSymbolic
;
103 O
.ShowBinaryBlobs
= ShowBinaryBlobs
;
106 BA
.analyze(O
, CheckHash
.empty() ? None
: Optional
<StringRef
>(CheckHash
)));
111 BA
.printStats(O
, StringRef(InputFilename
.getValue()));