xref: /freebsd/contrib/llvm-project/llvm/tools/llvm-bcanalyzer/llvm-bcanalyzer.cpp (revision 5ca8e32633c4ffbbcd6762e5888b6a4ba0708c6c)
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 //      --dump-blockinfo  - Dump the BLOCKINFO_BLOCK, when used with --dump
17 //
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).
27 //
28 //===----------------------------------------------------------------------===//
29 
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 #include <optional>
39 using namespace llvm;
40 
41 static cl::OptionCategory BCAnalyzerCategory("BC Analyzer Options");
42 
43 static cl::opt<std::string> InputFilename(cl::Positional,
44                                           cl::desc("<input bitcode>"),
45                                           cl::init("-"),
46                                           cl::cat(BCAnalyzerCategory));
47 
48 static cl::opt<bool> Dump("dump", cl::desc("Dump low level bitcode trace"),
49                           cl::cat(BCAnalyzerCategory));
50 
51 static cl::opt<bool> DumpBlockinfo("dump-blockinfo",
52                                    cl::desc("Include BLOCKINFO details in low"
53                                             " level dump"),
54                                    cl::cat(BCAnalyzerCategory));
55 
56 //===----------------------------------------------------------------------===//
57 // Bitcode specific analysis.
58 //===----------------------------------------------------------------------===//
59 
60 static cl::opt<bool> NoHistogram("disable-histogram",
61                                  cl::desc("Do not print per-code histogram"),
62                                  cl::cat(BCAnalyzerCategory));
63 
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));
68 
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));
73 
74 static cl::opt<bool>
75     ShowBinaryBlobs("show-binary-blobs",
76                     cl::desc("Print binary blobs using hex escapes"),
77                     cl::cat(BCAnalyzerCategory));
78 
79 static cl::opt<std::string> CheckHash(
80     "check-hash",
81     cl::desc("Check module hash using the argument as a string table"),
82     cl::cat(BCAnalyzerCategory));
83 
84 static Error reportError(StringRef Message) {
85   return createStringError(std::errc::illegal_byte_sequence, Message.data());
86 }
87 
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())
93     return std::move(E);
94 
95   std::unique_ptr<MemoryBuffer> MemBuf = std::move(*MemBufOrErr);
96 
97   if (MemBuf->getBufferSize() & 3)
98     return reportError(
99         "Bitcode stream should be a multiple of 4 bytes in length");
100   return std::move(MemBuf);
101 }
102 
103 int main(int argc, char **argv) {
104   InitLLVM X(argc, argv);
105 
106   cl::HideUnrelatedOptions({&BCAnalyzerCategory, &getColorCategory()});
107   cl::ParseCommandLineOptions(argc, argv, "llvm-bcanalyzer file analyzer\n");
108   ExitOnError ExitOnErr("llvm-bcanalyzer: ");
109 
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));
114 
115   BitcodeAnalyzer BA(MB->getBuffer(),
116                      BlockInfoMB
117                          ? std::optional<StringRef>(BlockInfoMB->getBuffer())
118                          : std::nullopt);
119 
120   BCDumpOptions O(outs());
121   O.Histogram = !NoHistogram;
122   O.Symbolic = !NonSymbolic;
123   O.ShowBinaryBlobs = ShowBinaryBlobs;
124   O.DumpBlockinfo = DumpBlockinfo;
125 
126   ExitOnErr(BA.analyze(
127       Dump ? std::optional<BCDumpOptions>(O) : std::optional<BCDumpOptions>(),
128       CheckHash.empty() ? std::nullopt : std::optional<StringRef>(CheckHash)));
129 
130   if (Dump)
131     outs() << "\n\n";
132 
133   BA.printStats(O, StringRef(InputFilename.getValue()));
134   return 0;
135 }
136