amccarth created this revision.
amccarth added a reviewer: clayborg.
amccarth added a subscriber: lldb-commits.
I set out to add functionality to dump headers from a binary, only to discover
that the functionality already existed but wasn't (as far as I can tell) hooked
up to the command line. (It's accessible through the script bridge by
enumerating modules and dumping them.) This patch adds a command line `target
modules dump headers` to do this interactively.
I'm not wedded to the particulars of the name, so if you think it should be
called something else, I'm open to suggestions.
http://reviews.llvm.org/D18464
Files:
source/Commands/CommandObjectTarget.cpp
Index: source/Commands/CommandObjectTarget.cpp
===================================================================
--- source/Commands/CommandObjectTarget.cpp
+++ source/Commands/CommandObjectTarget.cpp
@@ -1544,6 +1544,37 @@
strm.Printf("%-*s", width, "");
}
+static size_t
+DumpModuleHeaders(Stream &strm, ModuleList &module_list)
+{
+ size_t num_dumped = 0;
+ Mutex::Locker modules_locker(module_list.GetMutex());
+ const size_t num_modules = module_list.GetSize();
+ if (num_modules > 0)
+ {
+ strm.Printf("Dumping headers for %" PRIu64 " module(s).\n", static_cast<uint64_t>(num_modules));
+ strm.IndentMore();
+ for (size_t image_idx = 0; image_idx < num_modules; ++image_idx)
+ {
+ Module *module = module_list.GetModulePointerAtIndexUnlocked(image_idx);
+ if (module)
+ {
+ if (num_dumped++ > 0)
+ {
+ strm.EOL();
+ strm.EOL();
+ }
+ strm.Printf("Headers for '%s':\n", module->GetSpecificationDescription().c_str());
+ strm.IndentMore();
+ module->Dump(&strm);
+ strm.IndentLess();
+ }
+ }
+ strm.IndentLess();
+ }
+ return num_dumped;
+}
+
static void
DumpModuleSymtab (CommandInterpreter &interpreter, Stream &strm, Module *module, SortOrder sort_order)
{
@@ -1941,7 +1972,6 @@
ModuleList &module_list,
bool check_global_list)
{
-// Dump specified images (by basename or fullpath)
FileSpec module_file_spec(module_name, false);
ModuleSpec module_spec (module_file_spec);
@@ -2108,6 +2138,76 @@
}
};
+#pragma mark CommandObjectTargetModulesDumpHeaders
+
+class CommandObjectTargetModulesDumpHeaders : public CommandObjectTargetModulesModuleAutoComplete
+{
+public:
+ CommandObjectTargetModulesDumpHeaders(CommandInterpreter &interpreter)
+ : CommandObjectTargetModulesModuleAutoComplete(interpreter, "target modules dump headers",
+ "Dump the headers from one or more target modules.", nullptr)
+ {
+ }
+
+ ~CommandObjectTargetModulesDumpHeaders() override = default;
+
+protected:
+ bool
+ DoExecute(Args &command, CommandReturnObject &result) override
+ {
+ Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
+ if (target == nullptr)
+ {
+ result.AppendError("invalid target, create a debug target using the 'target create' command");
+ result.SetStatus(eReturnStatusFailed);
+ return false;
+ }
+
+ uint32_t addr_byte_size = target->GetArchitecture().GetAddressByteSize();
+ result.GetOutputStream().SetAddressByteSize(addr_byte_size);
+ result.GetErrorStream().SetAddressByteSize(addr_byte_size);
+
+ size_t num_dumped = 0;
+ if (command.GetArgumentCount() == 0)
+ {
+ // Dump all headers for all modules images
+ num_dumped = DumpModuleHeaders(result.GetOutputStream(), target->GetImages());
+ if (num_dumped == 0)
+ {
+ result.AppendError("the target has no associated executable images");
+ result.SetStatus(eReturnStatusFailed);
+ }
+ }
+ else
+ {
+ // Find the modules that match the basename or full path.
+ ModuleList module_list;
+ const char *arg_cstr;
+ for (int arg_idx = 0; (arg_cstr = command.GetArgumentAtIndex(arg_idx)) != nullptr; ++arg_idx)
+ {
+ size_t num_matched = FindModulesByName(target, arg_cstr, module_list, true);
+ if (num_matched == 0)
+ {
+ result.AppendWarningWithFormat("Unable to find an image that matches '%s'.\n", arg_cstr);
+ }
+ }
+ // Dump all the modules we found.
+ num_dumped = DumpModuleHeaders(result.GetOutputStream(), module_list);
+ }
+
+ if (num_dumped > 0)
+ {
+ result.SetStatus(eReturnStatusSuccessFinishResult);
+ }
+ else
+ {
+ result.AppendError("no matching executable images found");
+ result.SetStatus(eReturnStatusFailed);
+ }
+ return result.Succeeded();
+ }
+};
+
#pragma mark CommandObjectTargetModulesDumpSymtab
class CommandObjectTargetModulesDumpSymtab : public CommandObjectTargetModulesModuleAutoComplete
@@ -2579,12 +2679,13 @@
//------------------------------------------------------------------
// Constructors and Destructors
//------------------------------------------------------------------
- CommandObjectTargetModulesDump(CommandInterpreter &interpreter) :
- CommandObjectMultiword (interpreter,
- "target modules dump",
- "A set of commands for dumping information about one or more target modules.",
- "target modules dump [symtab|sections|symfile|line-table] [<file1> <file2> ...]")
+ CommandObjectTargetModulesDump(CommandInterpreter &interpreter)
+ : CommandObjectMultiword(
+ interpreter, "target modules dump",
+ "A set of commands for dumping information about one or more target modules.",
+ "target modules dump [headers|symtab|sections|symfile|line-table] [<file1> <file2> ...]")
{
+ LoadSubCommand("headers", CommandObjectSP(new CommandObjectTargetModulesDumpHeaders(interpreter)));
LoadSubCommand ("symtab", CommandObjectSP (new CommandObjectTargetModulesDumpSymtab (interpreter)));
LoadSubCommand ("sections", CommandObjectSP (new CommandObjectTargetModulesDumpSections (interpreter)));
LoadSubCommand ("symfile", CommandObjectSP (new CommandObjectTargetModulesDumpSymfile (interpreter)));
_______________________________________________
lldb-commits mailing list
[email protected]
http://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits