llvm-bcanalyzer.cpp 4.3 KB

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