2016-11-11 11:44:12 +08:00
|
|
|
//===-- llvm-strings.cpp - Printable String dumping utility ---------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This program is a utility that works like binutils "strings", that is, it
|
|
|
|
// prints out printable strings in a binary, objdump, or archive file.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/Object/Binary.h"
|
|
|
|
#include "llvm/Support/CommandLine.h"
|
|
|
|
#include "llvm/Support/Error.h"
|
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
|
|
|
#include "llvm/Support/PrettyStackTrace.h"
|
|
|
|
#include "llvm/Support/Program.h"
|
|
|
|
#include "llvm/Support/Signals.h"
|
2016-11-11 12:00:59 +08:00
|
|
|
#include <cctype>
|
2016-11-11 11:44:12 +08:00
|
|
|
#include <string>
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
using namespace llvm::object;
|
|
|
|
|
|
|
|
static cl::list<std::string> InputFileNames(cl::Positional,
|
|
|
|
cl::desc("<input object files>"),
|
|
|
|
cl::ZeroOrMore);
|
|
|
|
|
2016-11-15 05:10:41 +08:00
|
|
|
static cl::opt<bool>
|
|
|
|
PrintFileName("print-file-name",
|
|
|
|
cl::desc("Print the name of the file before each string"));
|
|
|
|
static cl::alias PrintFileNameShort("f", cl::desc(""),
|
|
|
|
cl::aliasopt(PrintFileName));
|
|
|
|
|
|
|
|
static void strings(raw_ostream &OS, StringRef FileName, StringRef Contents) {
|
|
|
|
auto print = [&OS, FileName](StringRef L) {
|
|
|
|
if (PrintFileName)
|
|
|
|
OS << FileName << ": ";
|
|
|
|
OS << L << '\n';
|
|
|
|
};
|
|
|
|
|
2016-11-12 11:39:21 +08:00
|
|
|
const char *P = nullptr, *E = nullptr, *S = nullptr;
|
|
|
|
for (P = Contents.begin(), E = Contents.end(); P < E; ++P) {
|
2016-11-11 11:44:12 +08:00
|
|
|
if (std::isgraph(*P) || std::isblank(*P)) {
|
|
|
|
if (S == nullptr)
|
|
|
|
S = P;
|
|
|
|
} else if (S) {
|
|
|
|
if (P - S > 3)
|
2016-11-15 05:10:41 +08:00
|
|
|
print(StringRef(S, P - S));
|
2016-11-11 11:44:12 +08:00
|
|
|
S = nullptr;
|
|
|
|
}
|
|
|
|
}
|
2016-11-12 11:39:21 +08:00
|
|
|
if (S && E - S > 3)
|
2016-11-15 05:10:41 +08:00
|
|
|
print(StringRef(S, E - S));
|
2016-11-11 11:44:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char **argv) {
|
|
|
|
sys::PrintStackTraceOnErrorSignal(argv[0]);
|
|
|
|
PrettyStackTraceProgram X(argc, argv);
|
|
|
|
|
|
|
|
cl::ParseCommandLineOptions(argc, argv, "llvm string dumper\n");
|
|
|
|
|
|
|
|
if (InputFileNames.empty())
|
|
|
|
InputFileNames.push_back("-");
|
|
|
|
|
2016-11-13 02:37:04 +08:00
|
|
|
for (const auto &File : InputFileNames) {
|
|
|
|
ErrorOr<std::unique_ptr<MemoryBuffer>> Buffer =
|
|
|
|
MemoryBuffer::getFileOrSTDIN(File);
|
|
|
|
if (std::error_code EC = Buffer.getError())
|
|
|
|
errs() << File << ": " << EC.message() << '\n';
|
|
|
|
else
|
2016-11-15 05:10:41 +08:00
|
|
|
strings(llvm::outs(), File == "-" ? "{standard input}" : File,
|
|
|
|
Buffer.get()->getMemBufferRef().getBuffer());
|
2016-11-13 02:37:04 +08:00
|
|
|
}
|
|
|
|
|
2016-11-11 11:44:12 +08:00
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|