Path: blob/main/contrib/llvm-project/llvm/tools/llvm-bcanalyzer/llvm-bcanalyzer.cpp
35231 views
//===-- llvm-bcanalyzer.cpp - Bitcode Analyzer --------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// This tool may be invoked in the following manner:9// llvm-bcanalyzer [options] - Read LLVM bitcode from stdin10// llvm-bcanalyzer [options] x.bc - Read LLVM bitcode from the x.bc file11//12// Options:13// --help - Output information about command line switches14// --dump - Dump low-level bitcode structure in readable format15// --dump-blockinfo - Dump the BLOCKINFO_BLOCK, when used with --dump16//17// This tool provides analytical information about a bitcode file. It is18// intended as an aid to developers of bitcode reading and writing software. It19// produces on std::out a summary of the bitcode file that shows various20// statistics about the contents of the file. By default this information is21// detailed and contains information about individual bitcode blocks and the22// functions in the module.23// The tool is also able to print a bitcode file in a straight forward text24// format that shows the containment and relationships of the information in25// the bitcode file (-dump option).26//27//===----------------------------------------------------------------------===//2829#include "llvm/Bitcode/BitcodeAnalyzer.h"30#include "llvm/Support/CommandLine.h"31#include "llvm/Support/Error.h"32#include "llvm/Support/InitLLVM.h"33#include "llvm/Support/MemoryBuffer.h"34#include "llvm/Support/WithColor.h"35#include "llvm/Support/raw_ostream.h"36#include <memory>37#include <optional>38using namespace llvm;3940static cl::OptionCategory BCAnalyzerCategory("BC Analyzer Options");4142static cl::opt<std::string> InputFilename(cl::Positional,43cl::desc("<input bitcode>"),44cl::init("-"),45cl::cat(BCAnalyzerCategory));4647static cl::opt<bool> Dump("dump", cl::desc("Dump low level bitcode trace"),48cl::cat(BCAnalyzerCategory));4950static cl::opt<bool> DumpBlockinfo("dump-blockinfo",51cl::desc("Include BLOCKINFO details in low"52" level dump"),53cl::cat(BCAnalyzerCategory));5455//===----------------------------------------------------------------------===//56// Bitcode specific analysis.57//===----------------------------------------------------------------------===//5859static cl::opt<bool> NoHistogram("disable-histogram",60cl::desc("Do not print per-code histogram"),61cl::cat(BCAnalyzerCategory));6263static cl::opt<bool> NonSymbolic("non-symbolic",64cl::desc("Emit numeric info in dump even if"65" symbolic info is available"),66cl::cat(BCAnalyzerCategory));6768static cl::opt<std::string>69BlockInfoFilename("block-info",70cl::desc("Use the BLOCK_INFO from the given file"),71cl::cat(BCAnalyzerCategory));7273static cl::opt<bool>74ShowBinaryBlobs("show-binary-blobs",75cl::desc("Print binary blobs using hex escapes"),76cl::cat(BCAnalyzerCategory));7778static cl::opt<std::string> CheckHash(79"check-hash",80cl::desc("Check module hash using the argument as a string table"),81cl::cat(BCAnalyzerCategory));8283static Error reportError(StringRef Message) {84return createStringError(std::errc::illegal_byte_sequence, Message.data());85}8687static Expected<std::unique_ptr<MemoryBuffer>> openBitcodeFile(StringRef Path) {88// Read the input file.89Expected<std::unique_ptr<MemoryBuffer>> MemBufOrErr =90errorOrToExpected(MemoryBuffer::getFileOrSTDIN(Path));91if (Error E = MemBufOrErr.takeError())92return std::move(E);9394std::unique_ptr<MemoryBuffer> MemBuf = std::move(*MemBufOrErr);9596if (MemBuf->getBufferSize() & 3)97return reportError(98"Bitcode stream should be a multiple of 4 bytes in length");99return std::move(MemBuf);100}101102int main(int argc, char **argv) {103InitLLVM X(argc, argv);104105cl::HideUnrelatedOptions({&BCAnalyzerCategory, &getColorCategory()});106cl::ParseCommandLineOptions(argc, argv, "llvm-bcanalyzer file analyzer\n");107ExitOnError ExitOnErr("llvm-bcanalyzer: ");108109std::unique_ptr<MemoryBuffer> MB = ExitOnErr(openBitcodeFile(InputFilename));110std::unique_ptr<MemoryBuffer> BlockInfoMB = nullptr;111if (!BlockInfoFilename.empty())112BlockInfoMB = ExitOnErr(openBitcodeFile(BlockInfoFilename));113114BitcodeAnalyzer BA(MB->getBuffer(),115BlockInfoMB116? std::optional<StringRef>(BlockInfoMB->getBuffer())117: std::nullopt);118119BCDumpOptions O(outs());120O.Histogram = !NoHistogram;121O.Symbolic = !NonSymbolic;122O.ShowBinaryBlobs = ShowBinaryBlobs;123O.DumpBlockinfo = DumpBlockinfo;124125ExitOnErr(BA.analyze(126Dump ? std::optional<BCDumpOptions>(O) : std::optional<BCDumpOptions>(),127CheckHash.empty() ? std::nullopt : std::optional<StringRef>(CheckHash)));128129if (Dump)130outs() << "\n\n";131132BA.printStats(O, StringRef(InputFilename.getValue()));133return 0;134}135136137