xref: /llvm-project/llvm/tools/llvm-strings/llvm-strings.cpp (revision f7009b42f8a9d916f0ed69274089fae32e5a1bbc)
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 cl::opt<bool>
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));
37 
38 static cl::opt<int>
39     MinLength("bytes", cl::desc("Print sequences of the specified length"),
40               cl::init(4));
41 static cl::alias MinLengthShort("n", cl::desc(""), cl::aliasopt(MinLength));
42 
43 static void strings(raw_ostream &OS, StringRef FileName, StringRef Contents) {
44   auto print = [&OS, FileName](StringRef L) {
45     if (L.size() < static_cast<size_t>(MinLength))
46       return;
47     if (PrintFileName)
48       OS << FileName << ": ";
49     OS << L << '\n';
50   };
51 
52   const char *P = nullptr, *E = nullptr, *S = nullptr;
53   for (P = Contents.begin(), E = Contents.end(); P < E; ++P) {
54     if (std::isgraph(*P) || std::isblank(*P)) {
55       if (S == nullptr)
56         S = P;
57     } else if (S) {
58       print(StringRef(S, P - S));
59       S = nullptr;
60     }
61   }
62   if (S)
63     print(StringRef(S, E - S));
64 }
65 
66 int main(int argc, char **argv) {
67   sys::PrintStackTraceOnErrorSignal(argv[0]);
68   PrettyStackTraceProgram X(argc, argv);
69 
70   cl::ParseCommandLineOptions(argc, argv, "llvm string dumper\n");
71   if (MinLength == 0) {
72     errs() << "invalid minimum string length 0\n";
73     return EXIT_FAILURE;
74   }
75 
76   if (InputFileNames.empty())
77     InputFileNames.push_back("-");
78 
79   for (const auto &File : InputFileNames) {
80     ErrorOr<std::unique_ptr<MemoryBuffer>> Buffer =
81         MemoryBuffer::getFileOrSTDIN(File);
82     if (std::error_code EC = Buffer.getError())
83       errs() << File << ": " << EC.message() << '\n';
84     else
85       strings(llvm::outs(), File == "-" ? "{standard input}" : File,
86               Buffer.get()->getMemBufferRef().getBuffer());
87   }
88 
89   return EXIT_SUCCESS;
90 }
91