xref: /freebsd/contrib/llvm-project/llvm/tools/llvm-bcanalyzer/llvm-bcanalyzer.cpp (revision 963f5dc7a30624e95d72fb7f87b8892651164e46)
1 //===-- llvm-bcanalyzer.cpp - Bitcode Analyzer --------------------------===//
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 // 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
12 //
13 //  Options:
14 //      --help      - Output information about command line switches
15 //      --dump      - Dump low-level bitcode structure in readable format
16 //
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).
26 //
27 //===----------------------------------------------------------------------===//
28 
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"
37 #include <memory>
38 using namespace llvm;
39 
40 static cl::OptionCategory BCAnalyzerCategory("BC Analyzer Options");
41 
42 static cl::opt<std::string> InputFilename(cl::Positional,
43                                           cl::desc("<input bitcode>"),
44                                           cl::init("-"),
45                                           cl::cat(BCAnalyzerCategory));
46 
47 static cl::opt<bool> Dump("dump", cl::desc("Dump low level bitcode trace"),
48                           cl::cat(BCAnalyzerCategory));
49 
50 //===----------------------------------------------------------------------===//
51 // Bitcode specific analysis.
52 //===----------------------------------------------------------------------===//
53 
54 static cl::opt<bool> NoHistogram("disable-histogram",
55                                  cl::desc("Do not print per-code histogram"),
56                                  cl::cat(BCAnalyzerCategory));
57 
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));
62 
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));
67 
68 static cl::opt<bool>
69     ShowBinaryBlobs("show-binary-blobs",
70                     cl::desc("Print binary blobs using hex escapes"),
71                     cl::cat(BCAnalyzerCategory));
72 
73 static cl::opt<std::string> CheckHash(
74     "check-hash",
75     cl::desc("Check module hash using the argument as a string table"),
76     cl::cat(BCAnalyzerCategory));
77 
78 static Error reportError(StringRef Message) {
79   return createStringError(std::errc::illegal_byte_sequence, Message.data());
80 }
81 
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())
87     return std::move(E);
88 
89   std::unique_ptr<MemoryBuffer> MemBuf = std::move(*MemBufOrErr);
90 
91   if (MemBuf->getBufferSize() & 3)
92     return reportError(
93         "Bitcode stream should be a multiple of 4 bytes in length");
94   return std::move(MemBuf);
95 }
96 
97 int main(int argc, char **argv) {
98   InitLLVM X(argc, argv);
99 
100   cl::HideUnrelatedOptions({&BCAnalyzerCategory, &getColorCategory()});
101   cl::ParseCommandLineOptions(argc, argv, "llvm-bcanalyzer file analyzer\n");
102   ExitOnError ExitOnErr("llvm-bcanalyzer: ");
103 
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));
108 
109   BitcodeAnalyzer BA(MB->getBuffer(),
110                      BlockInfoMB ? Optional<StringRef>(BlockInfoMB->getBuffer())
111                                  : None);
112 
113   BCDumpOptions O(outs());
114   O.Histogram = !NoHistogram;
115   O.Symbolic = !NonSymbolic;
116   O.ShowBinaryBlobs = ShowBinaryBlobs;
117 
118   ExitOnErr(BA.analyze(
119       Dump ? Optional<BCDumpOptions>(O) : Optional<BCDumpOptions>(None),
120       CheckHash.empty() ? None : Optional<StringRef>(CheckHash)));
121 
122   if (Dump)
123     outs() << "\n\n";
124 
125   BA.printStats(O, StringRef(InputFilename.getValue()));
126   return 0;
127 }
128