1 //===-- llvm-strings.cpp - Printable String dumping utility ---------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 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. 12 // 13 //===----------------------------------------------------------------------===// 14 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/MemoryBuffer.h" 20 #include "llvm/Support/PrettyStackTrace.h" 21 #include "llvm/Support/Program.h" 22 #include "llvm/Support/Signals.h" 23 #include <cctype> 24 #include <string> 25 26 using namespace llvm; 27 using namespace llvm::object; 28 29 static cl::list<std::string> InputFileNames(cl::Positional, 30 cl::desc("<input object files>"), 31 cl::ZeroOrMore); 32 33 static cl::opt<bool> 34 PrintFileName("print-file-name", 35 cl::desc("Print the name of the file before each string")); 36 static cl::alias PrintFileNameShort("f", cl::desc(""), 37 cl::aliasopt(PrintFileName)); 38 39 static cl::opt<int> 40 MinLength("bytes", cl::desc("Print sequences of the specified length"), 41 cl::init(4)); 42 static cl::alias MinLengthShort("n", cl::desc(""), cl::aliasopt(MinLength)); 43 44 enum radix { none, octal, hexadecimal, decimal }; 45 static cl::opt<radix> 46 Radix("radix", cl::desc("print the offset within the file"), 47 cl::values(clEnumValN(octal, "o", "octal"), 48 clEnumValN(hexadecimal, "x", "hexadecimal"), 49 clEnumValN(decimal, "d", "decimal")), 50 cl::init(none)); 51 static cl::alias RadixShort("t", cl::desc(""), cl::aliasopt(Radix)); 52 53 static void strings(raw_ostream &OS, StringRef FileName, StringRef Contents) { 54 auto print = [&OS, FileName](unsigned Offset, StringRef L) { 55 if (L.size() < static_cast<size_t>(MinLength)) 56 return; 57 if (PrintFileName) 58 OS << FileName << ":"; 59 switch (Radix) { 60 case none: 61 break; 62 case octal: 63 OS << format("%8o", Offset); 64 break; 65 case hexadecimal: 66 OS << format("%8x", Offset); 67 break; 68 case decimal: 69 OS << format("%8u", Offset); 70 break; 71 } 72 OS << " " << L << '\n'; 73 }; 74 75 const char *B = Contents.begin(); 76 const char *P = nullptr, *E = nullptr, *S = nullptr; 77 for (P = Contents.begin(), E = Contents.end(); P < E; ++P) { 78 if (std::isgraph(*P) || std::isblank(*P)) { 79 if (S == nullptr) 80 S = P; 81 } else if (S) { 82 print(S - B, StringRef(S, P - S)); 83 S = nullptr; 84 } 85 } 86 if (S) 87 print(S - B, StringRef(S, E - S)); 88 } 89 90 int main(int argc, char **argv) { 91 sys::PrintStackTraceOnErrorSignal(argv[0]); 92 PrettyStackTraceProgram X(argc, argv); 93 94 cl::ParseCommandLineOptions(argc, argv, "llvm string dumper\n"); 95 if (MinLength == 0) { 96 errs() << "invalid minimum string length 0\n"; 97 return EXIT_FAILURE; 98 } 99 100 if (InputFileNames.empty()) 101 InputFileNames.push_back("-"); 102 103 for (const auto &File : InputFileNames) { 104 ErrorOr<std::unique_ptr<MemoryBuffer>> Buffer = 105 MemoryBuffer::getFileOrSTDIN(File); 106 if (std::error_code EC = Buffer.getError()) 107 errs() << File << ": " << EC.message() << '\n'; 108 else 109 strings(llvm::outs(), File == "-" ? "{standard input}" : File, 110 Buffer.get()->getMemBufferRef().getBuffer()); 111 } 112 113 return EXIT_SUCCESS; 114 } 115