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/MemoryBuffer.h" 19 #include "llvm/Support/PrettyStackTrace.h" 20 #include "llvm/Support/Program.h" 21 #include "llvm/Support/Signals.h" 22 #include <cctype> 23 #include <string> 24 25 using namespace llvm; 26 using namespace llvm::object; 27 28 static cl::list<std::string> InputFileNames(cl::Positional, 29 cl::desc("<input object files>"), 30 cl::ZeroOrMore); 31 32 static void strings(raw_ostream &OS, StringRef Contents) { 33 const char *P = nullptr, *E = nullptr, *S = nullptr; 34 for (P = Contents.begin(), E = Contents.end(); P < E; ++P) { 35 if (std::isgraph(*P) || std::isblank(*P)) { 36 if (S == nullptr) 37 S = P; 38 } else if (S) { 39 if (P - S > 3) 40 OS << StringRef(S, P - S) << '\n'; 41 S = nullptr; 42 } 43 } 44 if (S && E - S > 3) 45 OS << StringRef(S, E - S) << '\n'; 46 } 47 48 int main(int argc, char **argv) { 49 sys::PrintStackTraceOnErrorSignal(argv[0]); 50 PrettyStackTraceProgram X(argc, argv); 51 52 cl::ParseCommandLineOptions(argc, argv, "llvm string dumper\n"); 53 54 if (InputFileNames.empty()) 55 InputFileNames.push_back("-"); 56 57 for (const auto &File : InputFileNames) { 58 ErrorOr<std::unique_ptr<MemoryBuffer>> Buffer = 59 MemoryBuffer::getFileOrSTDIN(File); 60 if (std::error_code EC = Buffer.getError()) 61 errs() << File << ": " << EC.message() << '\n'; 62 else 63 strings(llvm::outs(), Buffer.get()->getMemBufferRef().getBuffer()); 64 } 65 66 return EXIT_SUCCESS; 67 } 68