1 //===-- llvm-strings.cpp - Printable String dumping utility ---------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This program is a utility that works like binutils "strings", that is, it
11 // prints out printable strings in a binary, objdump, or archive file.
13 //===----------------------------------------------------------------------===//
15 #include "llvm/Object/Binary.h"
16 #include "llvm/Support/CommandLine.h"
17 #include "llvm/Support/Error.h"
18 #include "llvm/Support/Format.h"
19 #include "llvm/Support/InitLLVM.h"
20 #include "llvm/Support/MemoryBuffer.h"
21 #include "llvm/Support/Program.h"
26 using namespace llvm::object;
28 static cl::list<std::string> InputFileNames(cl::Positional,
29 cl::desc("<input object files>"),
33 PrintFileName("print-file-name",
34 cl::desc("Print the name of the file before each string"));
35 static cl::alias PrintFileNameShort("f", cl::desc(""),
36 cl::aliasopt(PrintFileName));
39 MinLength("bytes", cl::desc("Print sequences of the specified length"),
41 static cl::alias MinLengthShort("n", cl::desc(""), cl::aliasopt(MinLength));
45 cl::desc("Check all sections, not just the data section"));
46 static cl::alias AllSectionsShort("a", cl::desc(""),
47 cl::aliasopt(AllSections));
49 enum radix { none, octal, hexadecimal, decimal };
51 Radix("radix", cl::desc("print the offset within the file"),
52 cl::values(clEnumValN(octal, "o", "octal"),
53 clEnumValN(hexadecimal, "x", "hexadecimal"),
54 clEnumValN(decimal, "d", "decimal")),
56 static cl::alias RadixShort("t", cl::desc(""), cl::aliasopt(Radix));
58 static void strings(raw_ostream &OS, StringRef FileName, StringRef Contents) {
59 auto print = [&OS, FileName](unsigned Offset, StringRef L) {
60 if (L.size() < static_cast<size_t>(MinLength))
63 OS << FileName << ": ";
68 OS << format("%7o ", Offset);
71 OS << format("%7x ", Offset);
74 OS << format("%7u ", Offset);
80 const char *B = Contents.begin();
81 const char *P = nullptr, *E = nullptr, *S = nullptr;
82 for (P = Contents.begin(), E = Contents.end(); P < E; ++P) {
83 if (isPrint(*P) || *P == '\t') {
87 print(S - B, StringRef(S, P - S));
92 print(S - B, StringRef(S, E - S));
95 int main(int argc, char **argv) {
96 InitLLVM X(argc, argv);
98 cl::ParseCommandLineOptions(argc, argv, "llvm string dumper\n");
100 errs() << "invalid minimum string length 0\n";
104 if (InputFileNames.empty())
105 InputFileNames.push_back("-");
107 for (const auto &File : InputFileNames) {
108 ErrorOr<std::unique_ptr<MemoryBuffer>> Buffer =
109 MemoryBuffer::getFileOrSTDIN(File);
110 if (std::error_code EC = Buffer.getError())
111 errs() << File << ": " << EC.message() << '\n';
113 strings(llvm::outs(), File == "-" ? "{standard input}" : File,
114 Buffer.get()->getMemBufferRef().getBuffer());