From 04a4c0910b08fcd3961ad8cc74f99535efdee39a Mon Sep 17 00:00:00 2001 From: Raphael Isemann Date: Sat, 31 Aug 2019 09:41:25 +0000 Subject: [PATCH] [lldb] Unify target checking in CommandObject Summary: We currently have several CommandObjects that manually reimplement the checking for a selected target or a target in the execution context (which is the selected target when they are invoked). This patch removes all these checks and replaces them by setting the eCommandRequiresTarget flag that Pavel suggested. With this flag we are doing the same check but without having to duplicate this code in all these CommandObjects. I also added a `GetSelectedTarget()` variant of the `GetSelectedOrDummyTarget()` function to the CommandObject that checks that the flag is set and then returns a reference to the target. I didn't rewrite all the `target` variables from `Target *` to `Target &` in this patch as last time this change caused a lot of merge conflicts in Swift and I would prefer having that in a separate NFC commit. Reviewers: labath, clayborg Reviewed By: labath, clayborg Subscribers: clayborg, JDevlieghere, jingham, amccarth, abidh, lldb-commits Tags: #lldb Differential Revision: https://reviews.llvm.org/D66863 llvm-svn: 370571 --- lldb/include/lldb/Interpreter/CommandObject.h | 1 + .../CommandObjectBreakpointCommand.cpp | 17 +- .../Commands/CommandObjectDisassemble.cpp | 11 +- lldb/source/Commands/CommandObjectProcess.cpp | 12 +- lldb/source/Commands/CommandObjectTarget.cpp | 1344 ++++++++--------- lldb/source/Commands/CommandObjectThread.cpp | 15 +- .../Commands/CommandObjectWatchpoint.cpp | 35 +- .../CommandObjectWatchpointCommand.cpp | 39 +- lldb/source/Interpreter/CommandObject.cpp | 9 + 9 files changed, 676 insertions(+), 807 deletions(-) diff --git a/lldb/include/lldb/Interpreter/CommandObject.h b/lldb/include/lldb/Interpreter/CommandObject.h index e526c1db26b2..2dad84f036c0 100644 --- a/lldb/include/lldb/Interpreter/CommandObject.h +++ b/lldb/include/lldb/Interpreter/CommandObject.h @@ -331,6 +331,7 @@ protected: // selected target, or if no target is present you want to prime the dummy // target with entities that will be copied over to new targets. Target &GetSelectedOrDummyTarget(bool prefer_dummy = false); + Target &GetSelectedTarget(); Target &GetDummyTarget(); // If a command needs to use the "current" thread, use this call. Command diff --git a/lldb/source/Commands/CommandObjectBreakpointCommand.cpp b/lldb/source/Commands/CommandObjectBreakpointCommand.cpp index 5988b1c53023..11fdb85e8445 100644 --- a/lldb/source/Commands/CommandObjectBreakpointCommand.cpp +++ b/lldb/source/Commands/CommandObjectBreakpointCommand.cpp @@ -589,10 +589,10 @@ private: class CommandObjectBreakpointCommandList : public CommandObjectParsed { public: CommandObjectBreakpointCommandList(CommandInterpreter &interpreter) - : CommandObjectParsed(interpreter, "list", "List the script or set of " - "commands to be executed when " - "the breakpoint is hit.", - nullptr) { + : CommandObjectParsed(interpreter, "list", + "List the script or set of commands to be " + "executed when the breakpoint is hit.", + nullptr, eCommandRequiresTarget) { CommandArgumentEntry arg; CommandArgumentData bp_id_arg; @@ -612,14 +612,7 @@ public: protected: bool DoExecute(Args &command, CommandReturnObject &result) override { - Target *target = GetDebugger().GetSelectedTarget().get(); - - if (target == nullptr) { - result.AppendError("There is not a current executable; there are no " - "breakpoints for which to list commands"); - result.SetStatus(eReturnStatusFailed); - return false; - } + Target *target = &GetSelectedTarget(); const BreakpointList &breakpoints = target->GetBreakpointList(); size_t num_breakpoints = breakpoints.GetSize(); diff --git a/lldb/source/Commands/CommandObjectDisassemble.cpp b/lldb/source/Commands/CommandObjectDisassemble.cpp index 924c99bca538..7c5859126bfd 100644 --- a/lldb/source/Commands/CommandObjectDisassemble.cpp +++ b/lldb/source/Commands/CommandObjectDisassemble.cpp @@ -212,20 +212,15 @@ CommandObjectDisassemble::CommandObjectDisassemble( "Disassemble specified instructions in the current target. " "Defaults to the current function for the current thread and " "stack frame.", - "disassemble []"), + "disassemble []", eCommandRequiresTarget), m_options() {} CommandObjectDisassemble::~CommandObjectDisassemble() = default; bool CommandObjectDisassemble::DoExecute(Args &command, CommandReturnObject &result) { - Target *target = GetDebugger().GetSelectedTarget().get(); - if (target == nullptr) { - result.AppendError("invalid target, create a debug target using the " - "'target create' command"); - result.SetStatus(eReturnStatusFailed); - return false; - } + Target *target = &GetSelectedTarget(); + if (!m_options.arch.IsValid()) m_options.arch = target->GetArchitecture(); diff --git a/lldb/source/Commands/CommandObjectProcess.cpp b/lldb/source/Commands/CommandObjectProcess.cpp index e934b7cc5591..eb559cd9ba7b 100644 --- a/lldb/source/Commands/CommandObjectProcess.cpp +++ b/lldb/source/Commands/CommandObjectProcess.cpp @@ -1290,7 +1290,7 @@ public: "Manage LLDB handling of OS signals for the " "current target process. Defaults to showing " "current policy.", - nullptr), + nullptr, eCommandRequiresTarget), m_options() { SetHelpLong("\nIf no signals are specified, update them all. If no update " "option is specified, list the current values."); @@ -1375,15 +1375,7 @@ public: protected: bool DoExecute(Args &signal_args, CommandReturnObject &result) override { - TargetSP target_sp = GetDebugger().GetSelectedTarget(); - - if (!target_sp) { - result.AppendError("No current target;" - " cannot handle signals until you have a valid target " - "and process.\n"); - result.SetStatus(eReturnStatusFailed); - return false; - } + Target *target_sp = &GetSelectedTarget(); ProcessSP process_sp = target_sp->GetProcessSP(); diff --git a/lldb/source/Commands/CommandObjectTarget.cpp b/lldb/source/Commands/CommandObjectTarget.cpp index d02492f7a8c2..a7384a0a0d78 100644 --- a/lldb/source/Commands/CommandObjectTarget.cpp +++ b/lldb/source/Commands/CommandObjectTarget.cpp @@ -1030,7 +1030,7 @@ public: : CommandObjectParsed(interpreter, "target modules search-paths add", "Add new image search paths substitution pairs to " "the current target.", - nullptr) { + nullptr, eCommandRequiresTarget) { CommandArgumentEntry arg; CommandArgumentData old_prefix_arg; CommandArgumentData new_prefix_arg; @@ -1059,42 +1059,37 @@ public: protected: bool DoExecute(Args &command, CommandReturnObject &result) override { - Target *target = GetDebugger().GetSelectedTarget().get(); - if (target) { - const size_t argc = command.GetArgumentCount(); - if (argc & 1) { - result.AppendError("add requires an even number of arguments\n"); - result.SetStatus(eReturnStatusFailed); - } else { - for (size_t i = 0; i < argc; i += 2) { - const char *from = command.GetArgumentAtIndex(i); - const char *to = command.GetArgumentAtIndex(i + 1); + Target *target = &GetSelectedTarget(); + const size_t argc = command.GetArgumentCount(); + if (argc & 1) { + result.AppendError("add requires an even number of arguments\n"); + result.SetStatus(eReturnStatusFailed); + } else { + for (size_t i = 0; i < argc; i += 2) { + const char *from = command.GetArgumentAtIndex(i); + const char *to = command.GetArgumentAtIndex(i + 1); - if (from[0] && to[0]) { - Log *log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_HOST); - if (log) { - LLDB_LOGF(log, - "target modules search path adding ImageSearchPath " - "pair: '%s' -> '%s'", - from, to); - } - bool last_pair = ((argc - i) == 2); - target->GetImageSearchPathList().Append( - ConstString(from), ConstString(to), - last_pair); // Notify if this is the last pair - result.SetStatus(eReturnStatusSuccessFinishNoResult); - } else { - if (from[0]) - result.AppendError(" can't be empty\n"); - else - result.AppendError(" can't be empty\n"); - result.SetStatus(eReturnStatusFailed); + if (from[0] && to[0]) { + Log *log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_HOST); + if (log) { + LLDB_LOGF(log, + "target modules search path adding ImageSearchPath " + "pair: '%s' -> '%s'", + from, to); } + bool last_pair = ((argc - i) == 2); + target->GetImageSearchPathList().Append( + ConstString(from), ConstString(to), + last_pair); // Notify if this is the last pair + result.SetStatus(eReturnStatusSuccessFinishNoResult); + } else { + if (from[0]) + result.AppendError(" can't be empty\n"); + else + result.AppendError(" can't be empty\n"); + result.SetStatus(eReturnStatusFailed); } } - } else { - result.AppendError("invalid target\n"); - result.SetStatus(eReturnStatusFailed); } return result.Succeeded(); } @@ -1108,21 +1103,17 @@ public: : CommandObjectParsed(interpreter, "target modules search-paths clear", "Clear all current image search path substitution " "pairs from the current target.", - "target modules search-paths clear") {} + "target modules search-paths clear", + eCommandRequiresTarget) {} ~CommandObjectTargetModulesSearchPathsClear() override = default; protected: bool DoExecute(Args &command, CommandReturnObject &result) override { - Target *target = GetDebugger().GetSelectedTarget().get(); - if (target) { - bool notify = true; - target->GetImageSearchPathList().Clear(notify); - result.SetStatus(eReturnStatusSuccessFinishNoResult); - } else { - result.AppendError("invalid target\n"); - result.SetStatus(eReturnStatusFailed); - } + Target *target = &GetSelectedTarget(); + bool notify = true; + target->GetImageSearchPathList().Clear(notify); + result.SetStatus(eReturnStatusSuccessFinishNoResult); return result.Succeeded(); } }; @@ -1135,7 +1126,7 @@ public: : CommandObjectParsed(interpreter, "target modules search-paths insert", "Insert a new image search path substitution pair " "into the current target at the specified index.", - nullptr) { + nullptr, eCommandRequiresTarget) { CommandArgumentEntry arg1; CommandArgumentEntry arg2; CommandArgumentData index_arg; @@ -1175,55 +1166,49 @@ public: protected: bool DoExecute(Args &command, CommandReturnObject &result) override { - Target *target = GetDebugger().GetSelectedTarget().get(); - if (target) { - size_t argc = command.GetArgumentCount(); - // check for at least 3 arguments and an odd number of parameters - if (argc >= 3 && argc & 1) { - bool success = false; + Target *target = &GetSelectedTarget(); + size_t argc = command.GetArgumentCount(); + // check for at least 3 arguments and an odd number of parameters + if (argc >= 3 && argc & 1) { + bool success = false; - uint32_t insert_idx = StringConvert::ToUInt32( - command.GetArgumentAtIndex(0), UINT32_MAX, 0, &success); + uint32_t insert_idx = StringConvert::ToUInt32( + command.GetArgumentAtIndex(0), UINT32_MAX, 0, &success); - if (!success) { - result.AppendErrorWithFormat( - " parameter is not an integer: '%s'.\n", - command.GetArgumentAtIndex(0)); - result.SetStatus(eReturnStatusFailed); - return result.Succeeded(); - } - - // shift off the index - command.Shift(); - argc = command.GetArgumentCount(); - - for (uint32_t i = 0; i < argc; i += 2, ++insert_idx) { - const char *from = command.GetArgumentAtIndex(i); - const char *to = command.GetArgumentAtIndex(i + 1); - - if (from[0] && to[0]) { - bool last_pair = ((argc - i) == 2); - target->GetImageSearchPathList().Insert( - ConstString(from), ConstString(to), insert_idx, last_pair); - result.SetStatus(eReturnStatusSuccessFinishNoResult); - } else { - if (from[0]) - result.AppendError(" can't be empty\n"); - else - result.AppendError(" can't be empty\n"); - result.SetStatus(eReturnStatusFailed); - return false; - } - } - } else { - result.AppendError("insert requires at least three arguments\n"); + if (!success) { + result.AppendErrorWithFormat( + " parameter is not an integer: '%s'.\n", + command.GetArgumentAtIndex(0)); result.SetStatus(eReturnStatusFailed); return result.Succeeded(); } + // shift off the index + command.Shift(); + argc = command.GetArgumentCount(); + + for (uint32_t i = 0; i < argc; i += 2, ++insert_idx) { + const char *from = command.GetArgumentAtIndex(i); + const char *to = command.GetArgumentAtIndex(i + 1); + + if (from[0] && to[0]) { + bool last_pair = ((argc - i) == 2); + target->GetImageSearchPathList().Insert( + ConstString(from), ConstString(to), insert_idx, last_pair); + result.SetStatus(eReturnStatusSuccessFinishNoResult); + } else { + if (from[0]) + result.AppendError(" can't be empty\n"); + else + result.AppendError(" can't be empty\n"); + result.SetStatus(eReturnStatusFailed); + return false; + } + } } else { - result.AppendError("invalid target\n"); + result.AppendError("insert requires at least three arguments\n"); result.SetStatus(eReturnStatusFailed); + return result.Succeeded(); } return result.Succeeded(); } @@ -1237,26 +1222,22 @@ public: : CommandObjectParsed(interpreter, "target modules search-paths list", "List all current image search path substitution " "pairs in the current target.", - "target modules search-paths list") {} + "target modules search-paths list", + eCommandRequiresTarget) {} ~CommandObjectTargetModulesSearchPathsList() override = default; protected: bool DoExecute(Args &command, CommandReturnObject &result) override { - Target *target = GetDebugger().GetSelectedTarget().get(); - if (target) { - if (command.GetArgumentCount() != 0) { - result.AppendError("list takes no arguments\n"); - result.SetStatus(eReturnStatusFailed); - return result.Succeeded(); - } - - target->GetImageSearchPathList().Dump(&result.GetOutputStream()); - result.SetStatus(eReturnStatusSuccessFinishResult); - } else { - result.AppendError("invalid target\n"); + Target *target = &GetSelectedTarget(); + if (command.GetArgumentCount() != 0) { + result.AppendError("list takes no arguments\n"); result.SetStatus(eReturnStatusFailed); + return result.Succeeded(); } + + target->GetImageSearchPathList().Dump(&result.GetOutputStream()); + result.SetStatus(eReturnStatusSuccessFinishResult); return result.Succeeded(); } }; @@ -1269,7 +1250,7 @@ public: : CommandObjectParsed( interpreter, "target modules search-paths query", "Transform a path using the first applicable image search path.", - nullptr) { + nullptr, eCommandRequiresTarget) { CommandArgumentEntry arg; CommandArgumentData path_arg; @@ -1289,26 +1270,21 @@ public: protected: bool DoExecute(Args &command, CommandReturnObject &result) override { - Target *target = GetDebugger().GetSelectedTarget().get(); - if (target) { - if (command.GetArgumentCount() != 1) { - result.AppendError("query requires one argument\n"); - result.SetStatus(eReturnStatusFailed); - return result.Succeeded(); - } - - ConstString orig(command.GetArgumentAtIndex(0)); - ConstString transformed; - if (target->GetImageSearchPathList().RemapPath(orig, transformed)) - result.GetOutputStream().Printf("%s\n", transformed.GetCString()); - else - result.GetOutputStream().Printf("%s\n", orig.GetCString()); - - result.SetStatus(eReturnStatusSuccessFinishResult); - } else { - result.AppendError("invalid target\n"); + Target *target = &GetSelectedTarget(); + if (command.GetArgumentCount() != 1) { + result.AppendError("query requires one argument\n"); result.SetStatus(eReturnStatusFailed); + return result.Succeeded(); } + + ConstString orig(command.GetArgumentAtIndex(0)); + ConstString transformed; + if (target->GetImageSearchPathList().RemapPath(orig, transformed)) + result.GetOutputStream().Printf("%s\n", transformed.GetCString()); + else + result.GetOutputStream().Printf("%s\n", orig.GetCString()); + + result.SetStatus(eReturnStatusSuccessFinishResult); return result.Succeeded(); } }; @@ -1824,8 +1800,9 @@ public: CommandObjectTargetModulesModuleAutoComplete(CommandInterpreter &interpreter, const char *name, const char *help, - const char *syntax) - : CommandObjectParsed(interpreter, name, help, syntax) { + const char *syntax, + uint32_t flags = 0) + : CommandObjectParsed(interpreter, name, help, syntax, flags) { CommandArgumentEntry arg; CommandArgumentData file_arg; @@ -1899,19 +1876,13 @@ public: : CommandObjectTargetModulesModuleAutoComplete( interpreter, "target modules dump objfile", "Dump the object file headers from one or more target modules.", - nullptr) {} + nullptr, eCommandRequiresTarget) {} ~CommandObjectTargetModulesDumpObjfile() override = default; protected: bool DoExecute(Args &command, CommandReturnObject &result) override { - Target *target = GetDebugger().GetSelectedTarget().get(); - if (target == nullptr) { - result.AppendError("invalid target, create a debug target using the " - "'target create' command"); - result.SetStatus(eReturnStatusFailed); - return false; - } + Target *target = &GetSelectedTarget(); uint32_t addr_byte_size = target->GetArchitecture().GetAddressByteSize(); result.GetOutputStream().SetAddressByteSize(addr_byte_size); @@ -1984,7 +1955,8 @@ public: CommandObjectTargetModulesDumpSymtab(CommandInterpreter &interpreter) : CommandObjectTargetModulesModuleAutoComplete( interpreter, "target modules dump symtab", - "Dump the symbol table from one or more target modules.", nullptr), + "Dump the symbol table from one or more target modules.", nullptr, + eCommandRequiresTarget), m_options() {} ~CommandObjectTargetModulesDumpSymtab() override = default; @@ -2028,82 +2000,75 @@ public: protected: bool DoExecute(Args &command, CommandReturnObject &result) override { - Target *target = GetDebugger().GetSelectedTarget().get(); - if (target == nullptr) { - result.AppendError("invalid target, create a debug target using the " - "'target create' command"); - result.SetStatus(eReturnStatusFailed); - return false; - } else { - uint32_t num_dumped = 0; + Target *target = &GetSelectedTarget(); + uint32_t num_dumped = 0; - uint32_t addr_byte_size = target->GetArchitecture().GetAddressByteSize(); - result.GetOutputStream().SetAddressByteSize(addr_byte_size); - result.GetErrorStream().SetAddressByteSize(addr_byte_size); + uint32_t addr_byte_size = target->GetArchitecture().GetAddressByteSize(); + result.GetOutputStream().SetAddressByteSize(addr_byte_size); + result.GetErrorStream().SetAddressByteSize(addr_byte_size); - if (command.GetArgumentCount() == 0) { - // Dump all sections for all modules images - std::lock_guard guard( - target->GetImages().GetMutex()); - const size_t num_modules = target->GetImages().GetSize(); - if (num_modules > 0) { - result.GetOutputStream().Printf("Dumping symbol table for %" PRIu64 - " modules.\n", - (uint64_t)num_modules); - for (size_t image_idx = 0; image_idx < num_modules; ++image_idx) { - if (num_dumped > 0) { - result.GetOutputStream().EOL(); - result.GetOutputStream().EOL(); - } - if (m_interpreter.WasInterrupted()) - break; - num_dumped++; - DumpModuleSymtab( - m_interpreter, result.GetOutputStream(), - target->GetImages().GetModulePointerAtIndexUnlocked(image_idx), - m_options.m_sort_order); + if (command.GetArgumentCount() == 0) { + // Dump all sections for all modules images + std::lock_guard guard( + target->GetImages().GetMutex()); + const size_t num_modules = target->GetImages().GetSize(); + if (num_modules > 0) { + result.GetOutputStream().Printf("Dumping symbol table for %" PRIu64 + " modules.\n", + (uint64_t)num_modules); + for (size_t image_idx = 0; image_idx < num_modules; ++image_idx) { + if (num_dumped > 0) { + result.GetOutputStream().EOL(); + result.GetOutputStream().EOL(); } - } else { - result.AppendError("the target has no associated executable images"); - result.SetStatus(eReturnStatusFailed); - return false; + if (m_interpreter.WasInterrupted()) + break; + num_dumped++; + DumpModuleSymtab( + m_interpreter, result.GetOutputStream(), + target->GetImages().GetModulePointerAtIndexUnlocked(image_idx), + m_options.m_sort_order); } } else { - // Dump specified images (by basename or fullpath) - const char *arg_cstr; - for (int arg_idx = 0; - (arg_cstr = command.GetArgumentAtIndex(arg_idx)) != nullptr; - ++arg_idx) { - ModuleList module_list; - const size_t num_matches = - FindModulesByName(target, arg_cstr, module_list, true); - if (num_matches > 0) { - for (size_t i = 0; i < num_matches; ++i) { - Module *module = module_list.GetModulePointerAtIndex(i); - if (module) { - if (num_dumped > 0) { - result.GetOutputStream().EOL(); - result.GetOutputStream().EOL(); - } - if (m_interpreter.WasInterrupted()) - break; - num_dumped++; - DumpModuleSymtab(m_interpreter, result.GetOutputStream(), - module, m_options.m_sort_order); - } - } - } else - result.AppendWarningWithFormat( - "Unable to find an image that matches '%s'.\n", arg_cstr); - } - } - - if (num_dumped > 0) - result.SetStatus(eReturnStatusSuccessFinishResult); - else { - result.AppendError("no matching executable images found"); + result.AppendError("the target has no associated executable images"); result.SetStatus(eReturnStatusFailed); + return false; } + } else { + // Dump specified images (by basename or fullpath) + const char *arg_cstr; + for (int arg_idx = 0; + (arg_cstr = command.GetArgumentAtIndex(arg_idx)) != nullptr; + ++arg_idx) { + ModuleList module_list; + const size_t num_matches = + FindModulesByName(target, arg_cstr, module_list, true); + if (num_matches > 0) { + for (size_t i = 0; i < num_matches; ++i) { + Module *module = module_list.GetModulePointerAtIndex(i); + if (module) { + if (num_dumped > 0) { + result.GetOutputStream().EOL(); + result.GetOutputStream().EOL(); + } + if (m_interpreter.WasInterrupted()) + break; + num_dumped++; + DumpModuleSymtab(m_interpreter, result.GetOutputStream(), module, + m_options.m_sort_order); + } + } + } else + result.AppendWarningWithFormat( + "Unable to find an image that matches '%s'.\n", arg_cstr); + } + } + + if (num_dumped > 0) + result.SetStatus(eReturnStatusSuccessFinishResult); + else { + result.AppendError("no matching executable images found"); + result.SetStatus(eReturnStatusFailed); } return result.Succeeded(); } @@ -2123,82 +2088,75 @@ public: interpreter, "target modules dump sections", "Dump the sections from one or more target modules.", //"target modules dump sections [ ...]") - nullptr) {} + nullptr, eCommandRequiresTarget) {} ~CommandObjectTargetModulesDumpSections() override = default; protected: bool DoExecute(Args &command, CommandReturnObject &result) override { - Target *target = GetDebugger().GetSelectedTarget().get(); - if (target == nullptr) { - result.AppendError("invalid target, create a debug target using the " - "'target create' command"); - result.SetStatus(eReturnStatusFailed); - return false; - } else { - uint32_t num_dumped = 0; + Target *target = &GetSelectedTarget(); + uint32_t num_dumped = 0; - uint32_t addr_byte_size = target->GetArchitecture().GetAddressByteSize(); - result.GetOutputStream().SetAddressByteSize(addr_byte_size); - result.GetErrorStream().SetAddressByteSize(addr_byte_size); + uint32_t addr_byte_size = target->GetArchitecture().GetAddressByteSize(); + result.GetOutputStream().SetAddressByteSize(addr_byte_size); + result.GetErrorStream().SetAddressByteSize(addr_byte_size); - if (command.GetArgumentCount() == 0) { - // Dump all sections for all modules images - const size_t num_modules = target->GetImages().GetSize(); - if (num_modules > 0) { - result.GetOutputStream().Printf("Dumping sections for %" PRIu64 - " modules.\n", - (uint64_t)num_modules); - for (size_t image_idx = 0; image_idx < num_modules; ++image_idx) { - if (m_interpreter.WasInterrupted()) - break; - num_dumped++; - DumpModuleSections( - m_interpreter, result.GetOutputStream(), - target->GetImages().GetModulePointerAtIndex(image_idx)); - } - } else { - result.AppendError("the target has no associated executable images"); - result.SetStatus(eReturnStatusFailed); - return false; + if (command.GetArgumentCount() == 0) { + // Dump all sections for all modules images + const size_t num_modules = target->GetImages().GetSize(); + if (num_modules > 0) { + result.GetOutputStream().Printf("Dumping sections for %" PRIu64 + " modules.\n", + (uint64_t)num_modules); + for (size_t image_idx = 0; image_idx < num_modules; ++image_idx) { + if (m_interpreter.WasInterrupted()) + break; + num_dumped++; + DumpModuleSections( + m_interpreter, result.GetOutputStream(), + target->GetImages().GetModulePointerAtIndex(image_idx)); } } else { - // Dump specified images (by basename or fullpath) - const char *arg_cstr; - for (int arg_idx = 0; - (arg_cstr = command.GetArgumentAtIndex(arg_idx)) != nullptr; - ++arg_idx) { - ModuleList module_list; - const size_t num_matches = - FindModulesByName(target, arg_cstr, module_list, true); - if (num_matches > 0) { - for (size_t i = 0; i < num_matches; ++i) { - if (m_interpreter.WasInterrupted()) - break; - Module *module = module_list.GetModulePointerAtIndex(i); - if (module) { - num_dumped++; - DumpModuleSections(m_interpreter, result.GetOutputStream(), - module); - } + result.AppendError("the target has no associated executable images"); + result.SetStatus(eReturnStatusFailed); + return false; + } + } else { + // Dump specified images (by basename or fullpath) + const char *arg_cstr; + for (int arg_idx = 0; + (arg_cstr = command.GetArgumentAtIndex(arg_idx)) != nullptr; + ++arg_idx) { + ModuleList module_list; + const size_t num_matches = + FindModulesByName(target, arg_cstr, module_list, true); + if (num_matches > 0) { + for (size_t i = 0; i < num_matches; ++i) { + if (m_interpreter.WasInterrupted()) + break; + Module *module = module_list.GetModulePointerAtIndex(i); + if (module) { + num_dumped++; + DumpModuleSections(m_interpreter, result.GetOutputStream(), + module); } - } else { - // Check the global list - std::lock_guard guard( - Module::GetAllocationModuleCollectionMutex()); - - result.AppendWarningWithFormat( - "Unable to find an image that matches '%s'.\n", arg_cstr); } + } else { + // Check the global list + std::lock_guard guard( + Module::GetAllocationModuleCollectionMutex()); + + result.AppendWarningWithFormat( + "Unable to find an image that matches '%s'.\n", arg_cstr); } } + } - if (num_dumped > 0) - result.SetStatus(eReturnStatusSuccessFinishResult); - else { - result.AppendError("no matching executable images found"); - result.SetStatus(eReturnStatusFailed); - } + if (num_dumped > 0) + result.SetStatus(eReturnStatusSuccessFinishResult); + else { + result.AppendError("no matching executable images found"); + result.SetStatus(eReturnStatusFailed); } return result.Succeeded(); } @@ -2216,19 +2174,13 @@ public: interpreter, "target modules dump ast", "Dump the clang ast for a given module's symbol file.", //"target modules dump ast [ ...]") - nullptr) {} + nullptr, eCommandRequiresTarget) {} ~CommandObjectTargetModulesDumpClangAST() override = default; protected: bool DoExecute(Args &command, CommandReturnObject &result) override { - Target *target = GetDebugger().GetSelectedTarget().get(); - if (target == nullptr) { - result.AppendError("invalid target, create a debug target using the " - "'target create' command"); - result.SetStatus(eReturnStatusFailed); - return false; - } + Target *target = &GetSelectedTarget(); const size_t num_modules = target->GetImages().GetSize(); if (num_modules == 0) { @@ -2293,78 +2245,71 @@ public: interpreter, "target modules dump symfile", "Dump the debug symbol file for one or more target modules.", //"target modules dump symfile [ ...]") - nullptr) {} + nullptr, eCommandRequiresTarget) {} ~CommandObjectTargetModulesDumpSymfile() override = default; protected: bool DoExecute(Args &command, CommandReturnObject &result) override { - Target *target = GetDebugger().GetSelectedTarget().get(); - if (target == nullptr) { - result.AppendError("invalid target, create a debug target using the " - "'target create' command"); - result.SetStatus(eReturnStatusFailed); - return false; - } else { - uint32_t num_dumped = 0; + Target *target = &GetSelectedTarget(); + uint32_t num_dumped = 0; - uint32_t addr_byte_size = target->GetArchitecture().GetAddressByteSize(); - result.GetOutputStream().SetAddressByteSize(addr_byte_size); - result.GetErrorStream().SetAddressByteSize(addr_byte_size); + uint32_t addr_byte_size = target->GetArchitecture().GetAddressByteSize(); + result.GetOutputStream().SetAddressByteSize(addr_byte_size); + result.GetErrorStream().SetAddressByteSize(addr_byte_size); - if (command.GetArgumentCount() == 0) { - // Dump all sections for all modules images - const ModuleList &target_modules = target->GetImages(); - std::lock_guard guard(target_modules.GetMutex()); - const size_t num_modules = target_modules.GetSize(); - if (num_modules > 0) { - result.GetOutputStream().Printf("Dumping debug symbols for %" PRIu64 - " modules.\n", - (uint64_t)num_modules); - for (uint32_t image_idx = 0; image_idx < num_modules; ++image_idx) { - if (m_interpreter.WasInterrupted()) - break; - if (DumpModuleSymbolFile( - result.GetOutputStream(), - target_modules.GetModulePointerAtIndexUnlocked(image_idx))) - num_dumped++; - } - } else { - result.AppendError("the target has no associated executable images"); - result.SetStatus(eReturnStatusFailed); - return false; + if (command.GetArgumentCount() == 0) { + // Dump all sections for all modules images + const ModuleList &target_modules = target->GetImages(); + std::lock_guard guard(target_modules.GetMutex()); + const size_t num_modules = target_modules.GetSize(); + if (num_modules > 0) { + result.GetOutputStream().Printf("Dumping debug symbols for %" PRIu64 + " modules.\n", + (uint64_t)num_modules); + for (uint32_t image_idx = 0; image_idx < num_modules; ++image_idx) { + if (m_interpreter.WasInterrupted()) + break; + if (DumpModuleSymbolFile( + result.GetOutputStream(), + target_modules.GetModulePointerAtIndexUnlocked(image_idx))) + num_dumped++; } } else { - // Dump specified images (by basename or fullpath) - const char *arg_cstr; - for (int arg_idx = 0; - (arg_cstr = command.GetArgumentAtIndex(arg_idx)) != nullptr; - ++arg_idx) { - ModuleList module_list; - const size_t num_matches = - FindModulesByName(target, arg_cstr, module_list, true); - if (num_matches > 0) { - for (size_t i = 0; i < num_matches; ++i) { - if (m_interpreter.WasInterrupted()) - break; - Module *module = module_list.GetModulePointerAtIndex(i); - if (module) { - if (DumpModuleSymbolFile(result.GetOutputStream(), module)) - num_dumped++; - } - } - } else - result.AppendWarningWithFormat( - "Unable to find an image that matches '%s'.\n", arg_cstr); - } - } - - if (num_dumped > 0) - result.SetStatus(eReturnStatusSuccessFinishResult); - else { - result.AppendError("no matching executable images found"); + result.AppendError("the target has no associated executable images"); result.SetStatus(eReturnStatusFailed); + return false; } + } else { + // Dump specified images (by basename or fullpath) + const char *arg_cstr; + for (int arg_idx = 0; + (arg_cstr = command.GetArgumentAtIndex(arg_idx)) != nullptr; + ++arg_idx) { + ModuleList module_list; + const size_t num_matches = + FindModulesByName(target, arg_cstr, module_list, true); + if (num_matches > 0) { + for (size_t i = 0; i < num_matches; ++i) { + if (m_interpreter.WasInterrupted()) + break; + Module *module = module_list.GetModulePointerAtIndex(i); + if (module) { + if (DumpModuleSymbolFile(result.GetOutputStream(), module)) + num_dumped++; + } + } + } else + result.AppendWarningWithFormat( + "Unable to find an image that matches '%s'.\n", arg_cstr); + } + } + + if (num_dumped > 0) + result.SetStatus(eReturnStatusSuccessFinishResult); + else { + result.AppendError("no matching executable images found"); + result.SetStatus(eReturnStatusFailed); } return result.Succeeded(); } @@ -2513,10 +2458,11 @@ public: CommandObjectTargetModulesAdd(CommandInterpreter &interpreter) : CommandObjectParsed(interpreter, "target modules add", "Add a new module to the current target's modules.", - "target modules add []"), - m_option_group(), - m_symbol_file(LLDB_OPT_SET_1, false, "symfile", 's', 0, - eArgTypeFilename, "Fullpath to a stand alone debug " + "target modules add []", + eCommandRequiresTarget), + m_option_group(), m_symbol_file(LLDB_OPT_SET_1, false, "symfile", 's', + 0, eArgTypeFilename, + "Fullpath to a stand alone debug " "symbols file for when debug symbols " "are not in the executable.") { m_option_group.Append(&m_uuid_option_group, LLDB_OPT_SET_ALL, @@ -2543,125 +2489,117 @@ protected: OptionGroupFile m_symbol_file; bool DoExecute(Args &args, CommandReturnObject &result) override { - Target *target = GetDebugger().GetSelectedTarget().get(); - if (target == nullptr) { - result.AppendError("invalid target, create a debug target using the " - "'target create' command"); - result.SetStatus(eReturnStatusFailed); - return false; - } else { - bool flush = false; + Target *target = &GetSelectedTarget(); + bool flush = false; - const size_t argc = args.GetArgumentCount(); - if (argc == 0) { - if (m_uuid_option_group.GetOptionValue().OptionWasSet()) { - // We are given a UUID only, go locate the file - ModuleSpec module_spec; - module_spec.GetUUID() = - m_uuid_option_group.GetOptionValue().GetCurrentValue(); - if (m_symbol_file.GetOptionValue().OptionWasSet()) - module_spec.GetSymbolFileSpec() = - m_symbol_file.GetOptionValue().GetCurrentValue(); - if (Symbols::DownloadObjectAndSymbolFile(module_spec)) { - ModuleSP module_sp(target->GetOrCreateModule(module_spec, - true /* notify */)); - if (module_sp) { - result.SetStatus(eReturnStatusSuccessFinishResult); - return true; - } else { - StreamString strm; - module_spec.GetUUID().Dump(&strm); - if (module_spec.GetFileSpec()) { - if (module_spec.GetSymbolFileSpec()) { - result.AppendErrorWithFormat( - "Unable to create the executable or symbol file with " - "UUID %s with path %s and symbol file %s", - strm.GetData(), - module_spec.GetFileSpec().GetPath().c_str(), - module_spec.GetSymbolFileSpec().GetPath().c_str()); - } else { - result.AppendErrorWithFormat( - "Unable to create the executable or symbol file with " - "UUID %s with path %s", - strm.GetData(), - module_spec.GetFileSpec().GetPath().c_str()); - } - } else { - result.AppendErrorWithFormat("Unable to create the executable " - "or symbol file with UUID %s", - strm.GetData()); - } - result.SetStatus(eReturnStatusFailed); - return false; - } + const size_t argc = args.GetArgumentCount(); + if (argc == 0) { + if (m_uuid_option_group.GetOptionValue().OptionWasSet()) { + // We are given a UUID only, go locate the file + ModuleSpec module_spec; + module_spec.GetUUID() = + m_uuid_option_group.GetOptionValue().GetCurrentValue(); + if (m_symbol_file.GetOptionValue().OptionWasSet()) + module_spec.GetSymbolFileSpec() = + m_symbol_file.GetOptionValue().GetCurrentValue(); + if (Symbols::DownloadObjectAndSymbolFile(module_spec)) { + ModuleSP module_sp( + target->GetOrCreateModule(module_spec, true /* notify */)); + if (module_sp) { + result.SetStatus(eReturnStatusSuccessFinishResult); + return true; } else { StreamString strm; module_spec.GetUUID().Dump(&strm); - result.AppendErrorWithFormat( - "Unable to locate the executable or symbol file with UUID %s", - strm.GetData()); + if (module_spec.GetFileSpec()) { + if (module_spec.GetSymbolFileSpec()) { + result.AppendErrorWithFormat( + "Unable to create the executable or symbol file with " + "UUID %s with path %s and symbol file %s", + strm.GetData(), module_spec.GetFileSpec().GetPath().c_str(), + module_spec.GetSymbolFileSpec().GetPath().c_str()); + } else { + result.AppendErrorWithFormat( + "Unable to create the executable or symbol file with " + "UUID %s with path %s", + strm.GetData(), + module_spec.GetFileSpec().GetPath().c_str()); + } + } else { + result.AppendErrorWithFormat("Unable to create the executable " + "or symbol file with UUID %s", + strm.GetData()); + } result.SetStatus(eReturnStatusFailed); return false; } } else { - result.AppendError( - "one or more executable image paths must be specified"); + StreamString strm; + module_spec.GetUUID().Dump(&strm); + result.AppendErrorWithFormat( + "Unable to locate the executable or symbol file with UUID %s", + strm.GetData()); result.SetStatus(eReturnStatusFailed); return false; } } else { - for (auto &entry : args.entries()) { - if (entry.ref.empty()) - continue; + result.AppendError( + "one or more executable image paths must be specified"); + result.SetStatus(eReturnStatusFailed); + return false; + } + } else { + for (auto &entry : args.entries()) { + if (entry.ref.empty()) + continue; - FileSpec file_spec(entry.ref); - if (FileSystem::Instance().Exists(file_spec)) { - ModuleSpec module_spec(file_spec); - if (m_uuid_option_group.GetOptionValue().OptionWasSet()) - module_spec.GetUUID() = - m_uuid_option_group.GetOptionValue().GetCurrentValue(); - if (m_symbol_file.GetOptionValue().OptionWasSet()) - module_spec.GetSymbolFileSpec() = - m_symbol_file.GetOptionValue().GetCurrentValue(); - if (!module_spec.GetArchitecture().IsValid()) - module_spec.GetArchitecture() = target->GetArchitecture(); - Status error; - ModuleSP module_sp(target->GetOrCreateModule(module_spec, - true /* notify */, &error)); - if (!module_sp) { - const char *error_cstr = error.AsCString(); - if (error_cstr) - result.AppendError(error_cstr); - else - result.AppendErrorWithFormat("unsupported module: %s", - entry.c_str()); - result.SetStatus(eReturnStatusFailed); - return false; - } else { - flush = true; - } - result.SetStatus(eReturnStatusSuccessFinishResult); - } else { - std::string resolved_path = file_spec.GetPath(); + FileSpec file_spec(entry.ref); + if (FileSystem::Instance().Exists(file_spec)) { + ModuleSpec module_spec(file_spec); + if (m_uuid_option_group.GetOptionValue().OptionWasSet()) + module_spec.GetUUID() = + m_uuid_option_group.GetOptionValue().GetCurrentValue(); + if (m_symbol_file.GetOptionValue().OptionWasSet()) + module_spec.GetSymbolFileSpec() = + m_symbol_file.GetOptionValue().GetCurrentValue(); + if (!module_spec.GetArchitecture().IsValid()) + module_spec.GetArchitecture() = target->GetArchitecture(); + Status error; + ModuleSP module_sp(target->GetOrCreateModule( + module_spec, true /* notify */, &error)); + if (!module_sp) { + const char *error_cstr = error.AsCString(); + if (error_cstr) + result.AppendError(error_cstr); + else + result.AppendErrorWithFormat("unsupported module: %s", + entry.c_str()); result.SetStatus(eReturnStatusFailed); - if (resolved_path != entry.ref) { - result.AppendErrorWithFormat( - "invalid module path '%s' with resolved path '%s'\n", - entry.ref.str().c_str(), resolved_path.c_str()); - break; - } - result.AppendErrorWithFormat("invalid module path '%s'\n", - entry.c_str()); + return false; + } else { + flush = true; + } + result.SetStatus(eReturnStatusSuccessFinishResult); + } else { + std::string resolved_path = file_spec.GetPath(); + result.SetStatus(eReturnStatusFailed); + if (resolved_path != entry.ref) { + result.AppendErrorWithFormat( + "invalid module path '%s' with resolved path '%s'\n", + entry.ref.str().c_str(), resolved_path.c_str()); break; } + result.AppendErrorWithFormat("invalid module path '%s'\n", + entry.c_str()); + break; } } + } - if (flush) { - ProcessSP process = target->GetProcessSP(); - if (process) - process->Flush(); - } + if (flush) { + ProcessSP process = target->GetProcessSP(); + if (process) + process->Flush(); } return result.Succeeded(); @@ -2673,11 +2611,12 @@ class CommandObjectTargetModulesLoad public: CommandObjectTargetModulesLoad(CommandInterpreter &interpreter) : CommandObjectTargetModulesModuleAutoComplete( - interpreter, "target modules load", "Set the load addresses for " - "one or more sections in a " - "target module.", + interpreter, "target modules load", + "Set the load addresses for one or more sections in a target " + "module.", "target modules load [--file --uuid ] " - "
[
....]"), + "
[
....]", + eCommandRequiresTarget), m_option_group(), m_file_option(LLDB_OPT_SET_1, false, "file", 'f', 0, eArgTypeName, "Fullpath or basename for module to load.", ""), @@ -2706,249 +2645,241 @@ public: protected: bool DoExecute(Args &args, CommandReturnObject &result) override { - Target *target = GetDebugger().GetSelectedTarget().get(); + Target *target = &GetSelectedTarget(); const bool load = m_load_option.GetOptionValue().GetCurrentValue(); const bool set_pc = m_pc_option.GetOptionValue().GetCurrentValue(); - if (target == nullptr) { - result.AppendError("invalid target, create a debug target using the " - "'target create' command"); - result.SetStatus(eReturnStatusFailed); - return false; - } else { - const size_t argc = args.GetArgumentCount(); - ModuleSpec module_spec; - bool search_using_module_spec = false; - // Allow "load" option to work without --file or --uuid option. - if (load) { - if (!m_file_option.GetOptionValue().OptionWasSet() && - !m_uuid_option_group.GetOptionValue().OptionWasSet()) { - ModuleList &module_list = target->GetImages(); - if (module_list.GetSize() == 1) { - search_using_module_spec = true; - module_spec.GetFileSpec() = - module_list.GetModuleAtIndex(0)->GetFileSpec(); - } - } - } + const size_t argc = args.GetArgumentCount(); + ModuleSpec module_spec; + bool search_using_module_spec = false; - if (m_file_option.GetOptionValue().OptionWasSet()) { - search_using_module_spec = true; - const char *arg_cstr = m_file_option.GetOptionValue().GetCurrentValue(); - const bool use_global_module_list = true; - ModuleList module_list; - const size_t num_matches = FindModulesByName( - target, arg_cstr, module_list, use_global_module_list); - if (num_matches == 1) { + // Allow "load" option to work without --file or --uuid option. + if (load) { + if (!m_file_option.GetOptionValue().OptionWasSet() && + !m_uuid_option_group.GetOptionValue().OptionWasSet()) { + ModuleList &module_list = target->GetImages(); + if (module_list.GetSize() == 1) { + search_using_module_spec = true; module_spec.GetFileSpec() = module_list.GetModuleAtIndex(0)->GetFileSpec(); - } else if (num_matches > 1) { - search_using_module_spec = false; - result.AppendErrorWithFormat( - "more than 1 module matched by name '%s'\n", arg_cstr); - result.SetStatus(eReturnStatusFailed); - } else { - search_using_module_spec = false; - result.AppendErrorWithFormat("no object file for module '%s'\n", - arg_cstr); - result.SetStatus(eReturnStatusFailed); } } + } - if (m_uuid_option_group.GetOptionValue().OptionWasSet()) { - search_using_module_spec = true; - module_spec.GetUUID() = - m_uuid_option_group.GetOptionValue().GetCurrentValue(); + if (m_file_option.GetOptionValue().OptionWasSet()) { + search_using_module_spec = true; + const char *arg_cstr = m_file_option.GetOptionValue().GetCurrentValue(); + const bool use_global_module_list = true; + ModuleList module_list; + const size_t num_matches = FindModulesByName( + target, arg_cstr, module_list, use_global_module_list); + if (num_matches == 1) { + module_spec.GetFileSpec() = + module_list.GetModuleAtIndex(0)->GetFileSpec(); + } else if (num_matches > 1) { + search_using_module_spec = false; + result.AppendErrorWithFormat( + "more than 1 module matched by name '%s'\n", arg_cstr); + result.SetStatus(eReturnStatusFailed); + } else { + search_using_module_spec = false; + result.AppendErrorWithFormat("no object file for module '%s'\n", + arg_cstr); + result.SetStatus(eReturnStatusFailed); } + } - if (search_using_module_spec) { - ModuleList matching_modules; - const size_t num_matches = - target->GetImages().FindModules(module_spec, matching_modules); + if (m_uuid_option_group.GetOptionValue().OptionWasSet()) { + search_using_module_spec = true; + module_spec.GetUUID() = + m_uuid_option_group.GetOptionValue().GetCurrentValue(); + } - char path[PATH_MAX]; - if (num_matches == 1) { - Module *module = matching_modules.GetModulePointerAtIndex(0); - if (module) { - ObjectFile *objfile = module->GetObjectFile(); - if (objfile) { - SectionList *section_list = module->GetSectionList(); - if (section_list) { - bool changed = false; - if (argc == 0) { - if (m_slide_option.GetOptionValue().OptionWasSet()) { - const addr_t slide = - m_slide_option.GetOptionValue().GetCurrentValue(); - const bool slide_is_offset = true; - module->SetLoadAddress(*target, slide, slide_is_offset, - changed); - } else { - result.AppendError("one or more section name + load " - "address pair must be specified"); - result.SetStatus(eReturnStatusFailed); - return false; - } + if (search_using_module_spec) { + ModuleList matching_modules; + const size_t num_matches = + target->GetImages().FindModules(module_spec, matching_modules); + + char path[PATH_MAX]; + if (num_matches == 1) { + Module *module = matching_modules.GetModulePointerAtIndex(0); + if (module) { + ObjectFile *objfile = module->GetObjectFile(); + if (objfile) { + SectionList *section_list = module->GetSectionList(); + if (section_list) { + bool changed = false; + if (argc == 0) { + if (m_slide_option.GetOptionValue().OptionWasSet()) { + const addr_t slide = + m_slide_option.GetOptionValue().GetCurrentValue(); + const bool slide_is_offset = true; + module->SetLoadAddress(*target, slide, slide_is_offset, + changed); } else { - if (m_slide_option.GetOptionValue().OptionWasSet()) { - result.AppendError("The \"--slide \" option can't " - "be used in conjunction with setting " - "section load addresses.\n"); - result.SetStatus(eReturnStatusFailed); - return false; - } + result.AppendError("one or more section name + load " + "address pair must be specified"); + result.SetStatus(eReturnStatusFailed); + return false; + } + } else { + if (m_slide_option.GetOptionValue().OptionWasSet()) { + result.AppendError("The \"--slide \" option can't " + "be used in conjunction with setting " + "section load addresses.\n"); + result.SetStatus(eReturnStatusFailed); + return false; + } - for (size_t i = 0; i < argc; i += 2) { - const char *sect_name = args.GetArgumentAtIndex(i); - const char *load_addr_cstr = args.GetArgumentAtIndex(i + 1); - if (sect_name && load_addr_cstr) { - ConstString const_sect_name(sect_name); - bool success = false; - addr_t load_addr = StringConvert::ToUInt64( - load_addr_cstr, LLDB_INVALID_ADDRESS, 0, &success); - if (success) { - SectionSP section_sp( - section_list->FindSectionByName(const_sect_name)); - if (section_sp) { - if (section_sp->IsThreadSpecific()) { - result.AppendErrorWithFormat( - "thread specific sections are not yet " - "supported (section '%s')\n", - sect_name); - result.SetStatus(eReturnStatusFailed); - break; - } else { - if (target->GetSectionLoadList() - .SetSectionLoadAddress(section_sp, - load_addr)) - changed = true; - result.AppendMessageWithFormat( - "section '%s' loaded at 0x%" PRIx64 "\n", - sect_name, load_addr); - } - } else { - result.AppendErrorWithFormat("no section found that " - "matches the section " - "name '%s'\n", - sect_name); + for (size_t i = 0; i < argc; i += 2) { + const char *sect_name = args.GetArgumentAtIndex(i); + const char *load_addr_cstr = args.GetArgumentAtIndex(i + 1); + if (sect_name && load_addr_cstr) { + ConstString const_sect_name(sect_name); + bool success = false; + addr_t load_addr = StringConvert::ToUInt64( + load_addr_cstr, LLDB_INVALID_ADDRESS, 0, &success); + if (success) { + SectionSP section_sp( + section_list->FindSectionByName(const_sect_name)); + if (section_sp) { + if (section_sp->IsThreadSpecific()) { + result.AppendErrorWithFormat( + "thread specific sections are not yet " + "supported (section '%s')\n", + sect_name); result.SetStatus(eReturnStatusFailed); break; + } else { + if (target->GetSectionLoadList() + .SetSectionLoadAddress(section_sp, load_addr)) + changed = true; + result.AppendMessageWithFormat( + "section '%s' loaded at 0x%" PRIx64 "\n", + sect_name, load_addr); } } else { - result.AppendErrorWithFormat( - "invalid load address string '%s'\n", - load_addr_cstr); + result.AppendErrorWithFormat("no section found that " + "matches the section " + "name '%s'\n", + sect_name); result.SetStatus(eReturnStatusFailed); break; } } else { - if (sect_name) - result.AppendError("section names must be followed by " - "a load address.\n"); - else - result.AppendError("one or more section name + load " - "address pair must be specified.\n"); + result.AppendErrorWithFormat( + "invalid load address string '%s'\n", load_addr_cstr); result.SetStatus(eReturnStatusFailed); break; } + } else { + if (sect_name) + result.AppendError("section names must be followed by " + "a load address.\n"); + else + result.AppendError("one or more section name + load " + "address pair must be specified.\n"); + result.SetStatus(eReturnStatusFailed); + break; } } + } - if (changed) { - target->ModulesDidLoad(matching_modules); - Process *process = m_exe_ctx.GetProcessPtr(); - if (process) - process->Flush(); + if (changed) { + target->ModulesDidLoad(matching_modules); + Process *process = m_exe_ctx.GetProcessPtr(); + if (process) + process->Flush(); + } + if (load) { + ProcessSP process = target->CalculateProcess(); + Address file_entry = objfile->GetEntryPointAddress(); + if (!process) { + result.AppendError("No process"); + return false; } - if (load) { - ProcessSP process = target->CalculateProcess(); - Address file_entry = objfile->GetEntryPointAddress(); - if (!process) { - result.AppendError("No process"); - return false; - } - if (set_pc && !file_entry.IsValid()) { - result.AppendError("No entry address in object file"); - return false; - } - std::vector loadables( - objfile->GetLoadableData(*target)); - if (loadables.size() == 0) { - result.AppendError("No loadable sections"); - return false; - } - Status error = process->WriteObjectFile(std::move(loadables)); - if (error.Fail()) { - result.AppendError(error.AsCString()); - return false; - } - if (set_pc) { - ThreadList &thread_list = process->GetThreadList(); - RegisterContextSP reg_context( - thread_list.GetSelectedThread()->GetRegisterContext()); - addr_t file_entry_addr = file_entry.GetLoadAddress(target); - if (!reg_context->SetPC(file_entry_addr)) { - result.AppendErrorWithFormat("failed to set PC value to " - "0x%" PRIx64 "\n", - file_entry_addr); - result.SetStatus(eReturnStatusFailed); - } + if (set_pc && !file_entry.IsValid()) { + result.AppendError("No entry address in object file"); + return false; + } + std::vector loadables( + objfile->GetLoadableData(*target)); + if (loadables.size() == 0) { + result.AppendError("No loadable sections"); + return false; + } + Status error = process->WriteObjectFile(std::move(loadables)); + if (error.Fail()) { + result.AppendError(error.AsCString()); + return false; + } + if (set_pc) { + ThreadList &thread_list = process->GetThreadList(); + RegisterContextSP reg_context( + thread_list.GetSelectedThread()->GetRegisterContext()); + addr_t file_entry_addr = file_entry.GetLoadAddress(target); + if (!reg_context->SetPC(file_entry_addr)) { + result.AppendErrorWithFormat("failed to set PC value to " + "0x%" PRIx64 "\n", + file_entry_addr); + result.SetStatus(eReturnStatusFailed); } } - } else { - module->GetFileSpec().GetPath(path, sizeof(path)); - result.AppendErrorWithFormat( - "no sections in object file '%s'\n", path); - result.SetStatus(eReturnStatusFailed); } } else { module->GetFileSpec().GetPath(path, sizeof(path)); - result.AppendErrorWithFormat("no object file for module '%s'\n", + result.AppendErrorWithFormat("no sections in object file '%s'\n", path); result.SetStatus(eReturnStatusFailed); } } else { - FileSpec *module_spec_file = module_spec.GetFileSpecPtr(); - if (module_spec_file) { - module_spec_file->GetPath(path, sizeof(path)); - result.AppendErrorWithFormat("invalid module '%s'.\n", path); - } else - result.AppendError("no module spec"); + module->GetFileSpec().GetPath(path, sizeof(path)); + result.AppendErrorWithFormat("no object file for module '%s'\n", + path); result.SetStatus(eReturnStatusFailed); } } else { - std::string uuid_str; - - if (module_spec.GetFileSpec()) - module_spec.GetFileSpec().GetPath(path, sizeof(path)); - else - path[0] = '\0'; - - if (module_spec.GetUUIDPtr()) - uuid_str = module_spec.GetUUID().GetAsString(); - if (num_matches > 1) { - result.AppendErrorWithFormat( - "multiple modules match%s%s%s%s:\n", path[0] ? " file=" : "", - path, !uuid_str.empty() ? " uuid=" : "", uuid_str.c_str()); - for (size_t i = 0; i < num_matches; ++i) { - if (matching_modules.GetModulePointerAtIndex(i) - ->GetFileSpec() - .GetPath(path, sizeof(path))) - result.AppendMessageWithFormat("%s\n", path); - } - } else { - result.AppendErrorWithFormat( - "no modules were found that match%s%s%s%s.\n", - path[0] ? " file=" : "", path, - !uuid_str.empty() ? " uuid=" : "", uuid_str.c_str()); - } + FileSpec *module_spec_file = module_spec.GetFileSpecPtr(); + if (module_spec_file) { + module_spec_file->GetPath(path, sizeof(path)); + result.AppendErrorWithFormat("invalid module '%s'.\n", path); + } else + result.AppendError("no module spec"); result.SetStatus(eReturnStatusFailed); } } else { - result.AppendError("either the \"--file \" or the \"--uuid " - "\" option must be specified.\n"); + std::string uuid_str; + + if (module_spec.GetFileSpec()) + module_spec.GetFileSpec().GetPath(path, sizeof(path)); + else + path[0] = '\0'; + + if (module_spec.GetUUIDPtr()) + uuid_str = module_spec.GetUUID().GetAsString(); + if (num_matches > 1) { + result.AppendErrorWithFormat( + "multiple modules match%s%s%s%s:\n", path[0] ? " file=" : "", + path, !uuid_str.empty() ? " uuid=" : "", uuid_str.c_str()); + for (size_t i = 0; i < num_matches; ++i) { + if (matching_modules.GetModulePointerAtIndex(i) + ->GetFileSpec() + .GetPath(path, sizeof(path))) + result.AppendMessageWithFormat("%s\n", path); + } + } else { + result.AppendErrorWithFormat( + "no modules were found that match%s%s%s%s.\n", + path[0] ? " file=" : "", path, !uuid_str.empty() ? " uuid=" : "", + uuid_str.c_str()); + } result.SetStatus(eReturnStatusFailed); - return false; } + } else { + result.AppendError("either the \"--file \" or the \"--uuid " + "\" option must be specified.\n"); + result.SetStatus(eReturnStatusFailed); + return false; } return result.Succeeded(); } @@ -3891,91 +3822,82 @@ public: protected: bool DoExecute(Args &command, CommandReturnObject &result) override { - Target *target = GetDebugger().GetSelectedTarget().get(); - if (target == nullptr) { - result.AppendError("invalid target, create a debug target using the " - "'target create' command"); - result.SetStatus(eReturnStatusFailed); - return false; - } else { - bool syntax_error = false; - uint32_t i; - uint32_t num_successful_lookups = 0; - uint32_t addr_byte_size = target->GetArchitecture().GetAddressByteSize(); - result.GetOutputStream().SetAddressByteSize(addr_byte_size); - result.GetErrorStream().SetAddressByteSize(addr_byte_size); - // Dump all sections for all modules images + Target *target = &GetSelectedTarget(); + bool syntax_error = false; + uint32_t i; + uint32_t num_successful_lookups = 0; + uint32_t addr_byte_size = target->GetArchitecture().GetAddressByteSize(); + result.GetOutputStream().SetAddressByteSize(addr_byte_size); + result.GetErrorStream().SetAddressByteSize(addr_byte_size); + // Dump all sections for all modules images - if (command.GetArgumentCount() == 0) { - ModuleSP current_module; + if (command.GetArgumentCount() == 0) { + ModuleSP current_module; - // Where it is possible to look in the current symbol context first, - // try that. If this search was successful and --all was not passed, - // don't print anything else. - if (LookupHere(m_interpreter, result, syntax_error)) { - result.GetOutputStream().EOL(); - num_successful_lookups++; - if (!m_options.m_print_all) { - result.SetStatus(eReturnStatusSuccessFinishResult); - return result.Succeeded(); - } - } - - // Dump all sections for all other modules - - const ModuleList &target_modules = target->GetImages(); - std::lock_guard guard(target_modules.GetMutex()); - const size_t num_modules = target_modules.GetSize(); - if (num_modules > 0) { - for (i = 0; i < num_modules && !syntax_error; ++i) { - Module *module_pointer = - target_modules.GetModulePointerAtIndexUnlocked(i); - - if (module_pointer != current_module.get() && - LookupInModule( - m_interpreter, - target_modules.GetModulePointerAtIndexUnlocked(i), result, - syntax_error)) { - result.GetOutputStream().EOL(); - num_successful_lookups++; - } - } - } else { - result.AppendError("the target has no associated executable images"); - result.SetStatus(eReturnStatusFailed); - return false; - } - } else { - // Dump specified images (by basename or fullpath) - const char *arg_cstr; - for (i = 0; (arg_cstr = command.GetArgumentAtIndex(i)) != nullptr && - !syntax_error; - ++i) { - ModuleList module_list; - const size_t num_matches = - FindModulesByName(target, arg_cstr, module_list, false); - if (num_matches > 0) { - for (size_t j = 0; j < num_matches; ++j) { - Module *module = module_list.GetModulePointerAtIndex(j); - if (module) { - if (LookupInModule(m_interpreter, module, result, - syntax_error)) { - result.GetOutputStream().EOL(); - num_successful_lookups++; - } - } - } - } else - result.AppendWarningWithFormat( - "Unable to find an image that matches '%s'.\n", arg_cstr); + // Where it is possible to look in the current symbol context first, + // try that. If this search was successful and --all was not passed, + // don't print anything else. + if (LookupHere(m_interpreter, result, syntax_error)) { + result.GetOutputStream().EOL(); + num_successful_lookups++; + if (!m_options.m_print_all) { + result.SetStatus(eReturnStatusSuccessFinishResult); + return result.Succeeded(); } } - if (num_successful_lookups > 0) - result.SetStatus(eReturnStatusSuccessFinishResult); - else + // Dump all sections for all other modules + + const ModuleList &target_modules = target->GetImages(); + std::lock_guard guard(target_modules.GetMutex()); + const size_t num_modules = target_modules.GetSize(); + if (num_modules > 0) { + for (i = 0; i < num_modules && !syntax_error; ++i) { + Module *module_pointer = + target_modules.GetModulePointerAtIndexUnlocked(i); + + if (module_pointer != current_module.get() && + LookupInModule(m_interpreter, + target_modules.GetModulePointerAtIndexUnlocked(i), + result, syntax_error)) { + result.GetOutputStream().EOL(); + num_successful_lookups++; + } + } + } else { + result.AppendError("the target has no associated executable images"); result.SetStatus(eReturnStatusFailed); + return false; + } + } else { + // Dump specified images (by basename or fullpath) + const char *arg_cstr; + for (i = 0; (arg_cstr = command.GetArgumentAtIndex(i)) != nullptr && + !syntax_error; + ++i) { + ModuleList module_list; + const size_t num_matches = + FindModulesByName(target, arg_cstr, module_list, false); + if (num_matches > 0) { + for (size_t j = 0; j < num_matches; ++j) { + Module *module = module_list.GetModulePointerAtIndex(j); + if (module) { + if (LookupInModule(m_interpreter, module, result, syntax_error)) { + result.GetOutputStream().EOL(); + num_successful_lookups++; + } + } + } + } else + result.AppendWarningWithFormat( + "Unable to find an image that matches '%s'.\n", arg_cstr); + } } + + if (num_successful_lookups > 0) + result.SetStatus(eReturnStatusSuccessFinishResult); + else + result.SetStatus(eReturnStatusFailed); return result.Succeeded(); } diff --git a/lldb/source/Commands/CommandObjectThread.cpp b/lldb/source/Commands/CommandObjectThread.cpp index 2edba1d8da1e..ccb512282064 100644 --- a/lldb/source/Commands/CommandObjectThread.cpp +++ b/lldb/source/Commands/CommandObjectThread.cpp @@ -819,13 +819,6 @@ public: bool DoExecute(Args &command, CommandReturnObject &result) override { bool synchronous_execution = m_interpreter.GetSynchronous(); - if (!GetDebugger().GetSelectedTarget()) { - result.AppendError("invalid target, create a debug target using the " - "'target create' command"); - result.SetStatus(eReturnStatusFailed); - return false; - } - Process *process = m_exe_ctx.GetProcessPtr(); if (process == nullptr) { result.AppendError("no process exists. Cannot continue"); @@ -1091,13 +1084,7 @@ protected: bool DoExecute(Args &command, CommandReturnObject &result) override { bool synchronous_execution = m_interpreter.GetSynchronous(); - Target *target = GetDebugger().GetSelectedTarget().get(); - if (target == nullptr) { - result.AppendError("invalid target, create a debug target using the " - "'target create' command"); - result.SetStatus(eReturnStatusFailed); - return false; - } + Target *target = &GetSelectedTarget(); Process *process = m_exe_ctx.GetProcessPtr(); if (process == nullptr) { diff --git a/lldb/source/Commands/CommandObjectWatchpoint.cpp b/lldb/source/Commands/CommandObjectWatchpoint.cpp index 5ddec4769306..67fd27887bea 100644 --- a/lldb/source/Commands/CommandObjectWatchpoint.cpp +++ b/lldb/source/Commands/CommandObjectWatchpoint.cpp @@ -40,11 +40,6 @@ static void AddWatchpointDescription(Stream *s, Watchpoint *wp, static bool CheckTargetForWatchpointOperations(Target *target, CommandReturnObject &result) { - if (target == nullptr) { - result.AppendError("Invalid target. No existing target or watchpoints."); - result.SetStatus(eReturnStatusFailed); - return false; - } bool process_is_valid = target->GetProcessSP() && target->GetProcessSP()->IsAlive(); if (!process_is_valid) { @@ -156,7 +151,8 @@ public: CommandObjectWatchpointList(CommandInterpreter &interpreter) : CommandObjectParsed( interpreter, "watchpoint list", - "List all watchpoints at configurable levels of detail.", nullptr), + "List all watchpoints at configurable levels of detail.", nullptr, + eCommandRequiresTarget), m_options() { CommandArgumentEntry arg; CommandObject::AddIDsArgumentData(arg, eArgTypeWatchpointID, @@ -217,12 +213,7 @@ public: protected: bool DoExecute(Args &command, CommandReturnObject &result) override { - Target *target = GetDebugger().GetSelectedTarget().get(); - if (target == nullptr) { - result.AppendError("Invalid target. No current target or watchpoints."); - result.SetStatus(eReturnStatusSuccessFinishNoResult); - return true; - } + Target *target = &GetSelectedTarget(); if (target->GetProcessSP() && target->GetProcessSP()->IsAlive()) { uint32_t num_supported_hardware_watchpoints; @@ -292,7 +283,7 @@ public: : CommandObjectParsed(interpreter, "enable", "Enable the specified disabled watchpoint(s). If " "no watchpoints are specified, enable all of them.", - nullptr) { + nullptr, eCommandRequiresTarget) { CommandArgumentEntry arg; CommandObject::AddIDsArgumentData(arg, eArgTypeWatchpointID, eArgTypeWatchpointIDRange); @@ -305,7 +296,7 @@ public: protected: bool DoExecute(Args &command, CommandReturnObject &result) override { - Target *target = GetDebugger().GetSelectedTarget().get(); + Target *target = &GetSelectedTarget(); if (!CheckTargetForWatchpointOperations(target, result)) return false; @@ -362,7 +353,7 @@ public: "Disable the specified watchpoint(s) without " "removing it/them. If no watchpoints are " "specified, disable them all.", - nullptr) { + nullptr, eCommandRequiresTarget) { CommandArgumentEntry arg; CommandObject::AddIDsArgumentData(arg, eArgTypeWatchpointID, eArgTypeWatchpointIDRange); @@ -375,7 +366,7 @@ public: protected: bool DoExecute(Args &command, CommandReturnObject &result) override { - Target *target = GetDebugger().GetSelectedTarget().get(); + Target *target = &GetSelectedTarget(); if (!CheckTargetForWatchpointOperations(target, result)) return false; @@ -434,7 +425,7 @@ public: : CommandObjectParsed(interpreter, "watchpoint delete", "Delete the specified watchpoint(s). If no " "watchpoints are specified, delete them all.", - nullptr) { + nullptr, eCommandRequiresTarget) { CommandArgumentEntry arg; CommandObject::AddIDsArgumentData(arg, eArgTypeWatchpointID, eArgTypeWatchpointIDRange); @@ -447,7 +438,7 @@ public: protected: bool DoExecute(Args &command, CommandReturnObject &result) override { - Target *target = GetDebugger().GetSelectedTarget().get(); + Target *target = &GetSelectedTarget(); if (!CheckTargetForWatchpointOperations(target, result)) return false; @@ -511,7 +502,7 @@ public: : CommandObjectParsed(interpreter, "watchpoint ignore", "Set ignore count on the specified watchpoint(s). " "If no watchpoints are specified, set them all.", - nullptr), + nullptr, eCommandRequiresTarget), m_options() { CommandArgumentEntry arg; CommandObject::AddIDsArgumentData(arg, eArgTypeWatchpointID, @@ -564,7 +555,7 @@ public: protected: bool DoExecute(Args &command, CommandReturnObject &result) override { - Target *target = GetDebugger().GetSelectedTarget().get(); + Target *target = &GetSelectedTarget(); if (!CheckTargetForWatchpointOperations(target, result)) return false; @@ -631,7 +622,7 @@ public: "If no watchpoint is specified, act on the last created " "watchpoint. " "Passing an empty argument clears the modification.", - nullptr), + nullptr, eCommandRequiresTarget), m_options() { CommandArgumentEntry arg; CommandObject::AddIDsArgumentData(arg, eArgTypeWatchpointID, @@ -685,7 +676,7 @@ public: protected: bool DoExecute(Args &command, CommandReturnObject &result) override { - Target *target = GetDebugger().GetSelectedTarget().get(); + Target *target = &GetSelectedTarget(); if (!CheckTargetForWatchpointOperations(target, result)) return false; diff --git a/lldb/source/Commands/CommandObjectWatchpointCommand.cpp b/lldb/source/Commands/CommandObjectWatchpointCommand.cpp index e0a3bb88325e..4e46abac9514 100644 --- a/lldb/source/Commands/CommandObjectWatchpointCommand.cpp +++ b/lldb/source/Commands/CommandObjectWatchpointCommand.cpp @@ -59,7 +59,7 @@ public: : CommandObjectParsed(interpreter, "add", "Add a set of LLDB commands to a watchpoint, to be " "executed whenever the watchpoint is hit.", - nullptr), + nullptr, eCommandRequiresTarget), IOHandlerDelegateMultiline("DONE", IOHandlerDelegate::Completion::LLDBCommand), m_options() { @@ -389,14 +389,7 @@ are no syntax errors may indicate that a function was declared but never called. protected: bool DoExecute(Args &command, CommandReturnObject &result) override { - Target *target = GetDebugger().GetSelectedTarget().get(); - - if (target == nullptr) { - result.AppendError("There is not a current executable; there are no " - "watchpoints to which to add commands"); - result.SetStatus(eReturnStatusFailed); - return false; - } + Target *target = &GetSelectedTarget(); const WatchpointList &watchpoints = target->GetWatchpointList(); size_t num_watchpoints = watchpoints.GetSize(); @@ -486,7 +479,7 @@ public: CommandObjectWatchpointCommandDelete(CommandInterpreter &interpreter) : CommandObjectParsed(interpreter, "delete", "Delete the set of commands from a watchpoint.", - nullptr) { + nullptr, eCommandRequiresTarget) { CommandArgumentEntry arg; CommandArgumentData wp_id_arg; @@ -506,14 +499,7 @@ public: protected: bool DoExecute(Args &command, CommandReturnObject &result) override { - Target *target = GetDebugger().GetSelectedTarget().get(); - - if (target == nullptr) { - result.AppendError("There is not a current executable; there are no " - "watchpoints from which to delete commands"); - result.SetStatus(eReturnStatusFailed); - return false; - } + Target *target = &GetSelectedTarget(); const WatchpointList &watchpoints = target->GetWatchpointList(); size_t num_watchpoints = watchpoints.GetSize(); @@ -562,10 +548,10 @@ protected: class CommandObjectWatchpointCommandList : public CommandObjectParsed { public: CommandObjectWatchpointCommandList(CommandInterpreter &interpreter) - : CommandObjectParsed(interpreter, "list", "List the script or set of " - "commands to be executed when " - "the watchpoint is hit.", - nullptr) { + : CommandObjectParsed(interpreter, "list", + "List the script or set of commands to be executed " + "when the watchpoint is hit.", + nullptr, eCommandRequiresTarget) { CommandArgumentEntry arg; CommandArgumentData wp_id_arg; @@ -585,14 +571,7 @@ public: protected: bool DoExecute(Args &command, CommandReturnObject &result) override { - Target *target = GetDebugger().GetSelectedTarget().get(); - - if (target == nullptr) { - result.AppendError("There is not a current executable; there are no " - "watchpoints for which to list commands"); - result.SetStatus(eReturnStatusFailed); - return false; - } + Target *target = &GetSelectedTarget(); const WatchpointList &watchpoints = target->GetWatchpointList(); size_t num_watchpoints = watchpoints.GetSize(); diff --git a/lldb/source/Interpreter/CommandObject.cpp b/lldb/source/Interpreter/CommandObject.cpp index 82939b8afbf6..747e48972363 100644 --- a/lldb/source/Interpreter/CommandObject.cpp +++ b/lldb/source/Interpreter/CommandObject.cpp @@ -925,6 +925,15 @@ Target &CommandObject::GetSelectedOrDummyTarget(bool prefer_dummy) { return *m_interpreter.GetDebugger().GetSelectedOrDummyTarget(prefer_dummy); } +Target &CommandObject::GetSelectedTarget() { + assert(m_flags.AnySet(eCommandRequiresTarget | eCommandProcessMustBePaused | + eCommandProcessMustBeLaunched | eCommandRequiresFrame | + eCommandRequiresThread | eCommandRequiresProcess | + eCommandRequiresRegContext) && + "GetSelectedTarget called from object that may have no target"); + return *m_interpreter.GetDebugger().GetSelectedTarget(); +} + Thread *CommandObject::GetDefaultThread() { Thread *thread_to_use = m_exe_ctx.GetThreadPtr(); if (thread_to_use)