llvm-bcanalyzer.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. #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>
  38. using namespace llvm;
  39. static cl::OptionCategory BCAnalyzerCategory("BC Analyzer Options");
  40. static cl::opt<std::string> InputFilename(cl::Positional,
  41. cl::desc("<input bitcode>"),
  42. cl::init("-"),
  43. cl::cat(BCAnalyzerCategory));
  44. static cl::opt<bool> Dump("dump", cl::desc("Dump low level bitcode trace"),
  45. cl::cat(BCAnalyzerCategory));
  46. static cl::opt<bool> DumpBlockinfo("dump-blockinfo",
  47. cl::desc("Include BLOCKINFO details in low"
  48. " level dump"),
  49. cl::cat(BCAnalyzerCategory));
  50. //===----------------------------------------------------------------------===//
  51. // Bitcode specific analysis.
  52. //===----------------------------------------------------------------------===//
  53. static cl::opt<bool> NoHistogram("disable-histogram",
  54. cl::desc("Do not print per-code histogram"),
  55. cl::cat(BCAnalyzerCategory));
  56. static cl::opt<bool> NonSymbolic("non-symbolic",
  57. cl::desc("Emit numeric info in dump even if"
  58. " symbolic info is available"),
  59. cl::cat(BCAnalyzerCategory));
  60. static cl::opt<std::string>
  61. BlockInfoFilename("block-info",
  62. cl::desc("Use the BLOCK_INFO from the given file"),
  63. cl::cat(BCAnalyzerCategory));
  64. static cl::opt<bool>
  65. ShowBinaryBlobs("show-binary-blobs",
  66. cl::desc("Print binary blobs using hex escapes"),
  67. cl::cat(BCAnalyzerCategory));
  68. static cl::opt<std::string> CheckHash(
  69. "check-hash",
  70. cl::desc("Check module hash using the argument as a string table"),
  71. cl::cat(BCAnalyzerCategory));
  72. static Error reportError(StringRef Message) {
  73. return createStringError(std::errc::illegal_byte_sequence, Message.data());
  74. }
  75. static Expected<std::unique_ptr<MemoryBuffer>> openBitcodeFile(StringRef Path) {
  76. // Read the input file.
  77. Expected<std::unique_ptr<MemoryBuffer>> MemBufOrErr =
  78. errorOrToExpected(MemoryBuffer::getFileOrSTDIN(Path));
  79. if (Error E = MemBufOrErr.takeError())
  80. return std::move(E);
  81. std::unique_ptr<MemoryBuffer> MemBuf = std::move(*MemBufOrErr);
  82. if (MemBuf->getBufferSize() & 3)
  83. return reportError(
  84. "Bitcode stream should be a multiple of 4 bytes in length");
  85. return std::move(MemBuf);
  86. }
  87. int main(int argc, char **argv) {
  88. InitLLVM X(argc, argv);
  89. cl::HideUnrelatedOptions({&BCAnalyzerCategory, &getColorCategory()});
  90. cl::ParseCommandLineOptions(argc, argv, "llvm-bcanalyzer file analyzer\n");
  91. ExitOnError ExitOnErr("llvm-bcanalyzer: ");
  92. std::unique_ptr<MemoryBuffer> MB = ExitOnErr(openBitcodeFile(InputFilename));
  93. std::unique_ptr<MemoryBuffer> BlockInfoMB = nullptr;
  94. if (!BlockInfoFilename.empty())
  95. BlockInfoMB = ExitOnErr(openBitcodeFile(BlockInfoFilename));
  96. BitcodeAnalyzer BA(MB->getBuffer(),
  97. BlockInfoMB
  98. ? std::optional<StringRef>(BlockInfoMB->getBuffer())
  99. : std::nullopt);
  100. BCDumpOptions O(outs());
  101. O.Histogram = !NoHistogram;
  102. O.Symbolic = !NonSymbolic;
  103. O.ShowBinaryBlobs = ShowBinaryBlobs;
  104. O.DumpBlockinfo = DumpBlockinfo;
  105. ExitOnErr(BA.analyze(
  106. Dump ? std::optional<BCDumpOptions>(O) : std::optional<BCDumpOptions>(),
  107. CheckHash.empty() ? std::nullopt : std::optional<StringRef>(CheckHash)));
  108. if (Dump)
  109. outs() << "\n\n";
  110. BA.printStats(O, StringRef(InputFilename.getValue()));
  111. return 0;
  112. }