xref: /llvm-project/llvm/tools/llvm-strings/llvm-strings.cpp (revision ba04861831591c7da647b579ef97f21762609e7a)
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     default:
61     case none:
62       break;
63     case octal:
64       OS << format("%8o", Offset);
65       break;
66     case hexadecimal:
67       OS << format("%8x", Offset);
68       break;
69     case decimal:
70       OS << format("%8u", Offset);
71       break;
72     }
73     OS << " " << L << '\n';
74   };
75 
76   const char *B = Contents.begin();
77   const char *P = nullptr, *E = nullptr, *S = nullptr;
78   for (P = Contents.begin(), E = Contents.end(); P < E; ++P) {
79     if (std::isgraph(*P) || std::isblank(*P)) {
80       if (S == nullptr)
81         S = P;
82     } else if (S) {
83       print(S - B, StringRef(S, P - S));
84       S = nullptr;
85     }
86   }
87   if (S)
88     print(S - B, StringRef(S, E - S));
89 }
90 
91 int main(int argc, char **argv) {
92   sys::PrintStackTraceOnErrorSignal(argv[0]);
93   PrettyStackTraceProgram X(argc, argv);
94 
95   cl::ParseCommandLineOptions(argc, argv, "llvm string dumper\n");
96   if (MinLength == 0) {
97     errs() << "invalid minimum string length 0\n";
98     return EXIT_FAILURE;
99   }
100 
101   if (InputFileNames.empty())
102     InputFileNames.push_back("-");
103 
104   for (const auto &File : InputFileNames) {
105     ErrorOr<std::unique_ptr<MemoryBuffer>> Buffer =
106         MemoryBuffer::getFileOrSTDIN(File);
107     if (std::error_code EC = Buffer.getError())
108       errs() << File << ": " << EC.message() << '\n';
109     else
110       strings(llvm::outs(), File == "-" ? "{standard input}" : File,
111               Buffer.get()->getMemBufferRef().getBuffer());
112   }
113 
114   return EXIT_SUCCESS;
115 }
116