mib created this revision. mib added reviewers: JDevlieghere, bulbazord, jingham. mib added a project: LLDB. Herald added a project: All. mib requested review of this revision. Herald added a subscriber: lldb-commits.
This patch add the ability for the user to set a name for a target. This can be very useful when debugging targets with the same executables in the same session. Names can be set either at the target creation in the command interpreter or at any time using the SBAPI. Target names show up in the `target list` output, following the target index, and they also allow the user to switch targets using them. rdar://105016191 Signed-off-by: Med Ismail Bennani <ism...@bennani.ma> Repository: rG LLVM Github Monorepo https://reviews.llvm.org/D151859 Files: lldb/include/lldb/API/SBTarget.h lldb/include/lldb/Target/Target.h lldb/source/API/SBTarget.cpp lldb/source/Commands/CommandObjectTarget.cpp
Index: lldb/source/Commands/CommandObjectTarget.cpp =================================================================== --- lldb/source/Commands/CommandObjectTarget.cpp +++ lldb/source/Commands/CommandObjectTarget.cpp @@ -82,8 +82,14 @@ if (!exe_valid) ::strcpy(exe_path, "<none>"); - strm.Printf("%starget #%u: %s", prefix_cstr ? prefix_cstr : "", target_idx, - exe_path); + std::string formatted_name = ""; + const std::string &name = target->GetName(); + if (!name.empty()) { + formatted_name = " (" + name + ")"; + } + + strm.Printf("%starget #%u%s: %s", prefix_cstr ? prefix_cstr : "", target_idx, + formatted_name.data(), exe_path); uint32_t properties = 0; if (target_arch.IsValid()) { @@ -209,6 +215,8 @@ m_platform_options(true), // Include the --platform option. m_core_file(LLDB_OPT_SET_1, false, "core", 'c', 0, eArgTypeFilename, "Fullpath to a core file to use for this target."), + m_name(LLDB_OPT_SET_1, false, "name", 'n', 0, eArgTypeName, + "Optional name for this target.", nullptr), m_symbol_file(LLDB_OPT_SET_1, false, "symfile", 's', 0, eArgTypeFilename, "Fullpath to a stand alone debug " @@ -234,6 +242,7 @@ m_option_group.Append(&m_arch_option, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1); m_option_group.Append(&m_platform_options, LLDB_OPT_SET_ALL, 1); m_option_group.Append(&m_core_file, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1); + m_option_group.Append(&m_name, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1); m_option_group.Append(&m_symbol_file, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1); m_option_group.Append(&m_remote_file, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1); m_option_group.Append(&m_add_dependents, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1); @@ -303,6 +312,10 @@ return false; } + llvm::StringRef name = m_name.GetOptionValue().GetCurrentValueAsRef(); + if (!name.empty()) + target_sp->SetName(name); + auto on_error = llvm::make_scope_exit( [&target_list = debugger.GetTargetList(), &target_sp]() { target_list.DeleteTarget(target_sp); @@ -455,6 +468,7 @@ OptionGroupArchitecture m_arch_option; OptionGroupPlatform m_platform_options; OptionGroupFile m_core_file; + OptionGroupString m_name; OptionGroupFile m_symbol_file; OptionGroupFile m_remote_file; OptionGroupDependents m_add_dependents; @@ -504,10 +518,10 @@ bool DoExecute(Args &args, CommandReturnObject &result) override { if (args.GetArgumentCount() == 1) { const char *target_idx_arg = args.GetArgumentAtIndex(0); - uint32_t target_idx; + uint32_t target_idx = LLDB_INVALID_INDEX32; + TargetList &target_list = GetDebugger().GetTargetList(); + const uint32_t num_targets = target_list.GetNumTargets(); if (llvm::to_integer(target_idx_arg, target_idx)) { - TargetList &target_list = GetDebugger().GetTargetList(); - const uint32_t num_targets = target_list.GetNumTargets(); if (target_idx < num_targets) { target_list.SetSelectedTarget(target_idx); Stream &strm = result.GetOutputStream(); @@ -526,8 +540,28 @@ } } } else { - result.AppendErrorWithFormat("invalid index string value '%s'\n", - target_idx_arg); + for (size_t i = 0; i < num_targets; i++) { + if (TargetSP target_sp = target_list.GetTargetAtIndex(i)) { + const std::string &name = target_sp->GetName(); + if (!name.empty()) { + if (name == target_idx_arg) { + target_idx = i; + break; + } + } + } + } + + if (target_idx != LLDB_INVALID_INDEX32) { + target_list.SetSelectedTarget(target_idx); + Stream &strm = result.GetOutputStream(); + bool show_stopped_process_status = false; + DumpTargetList(target_list, show_stopped_process_status, strm); + result.SetStatus(eReturnStatusSuccessFinishResult); + } else { + result.AppendErrorWithFormat("invalid index string value '%s'\n", + target_idx_arg); + } } } else { result.AppendError( Index: lldb/source/API/SBTarget.cpp =================================================================== --- lldb/source/API/SBTarget.cpp +++ lldb/source/API/SBTarget.cpp @@ -1601,6 +1601,26 @@ return const_name.GetCString(); } +const char *SBTarget::GetName() const { + LLDB_INSTRUMENT_VA(this); + + TargetSP target_sp(GetSP()); + if (!target_sp) + return nullptr; + + return ConstString(target_sp->GetName().data()).AsCString(); +} + +void SBTarget::SetName(const char *name) { + LLDB_INSTRUMENT_VA(this, name); + + TargetSP target_sp(GetSP()); + if (!target_sp) + return; + + target_sp->SetName(name); +} + uint32_t SBTarget::GetDataByteSize() { LLDB_INSTRUMENT_VA(this); Index: lldb/include/lldb/Target/Target.h =================================================================== --- lldb/include/lldb/Target/Target.h +++ lldb/include/lldb/Target/Target.h @@ -554,6 +554,10 @@ bool IsDummyTarget() const { return m_is_dummy_target; } + const std::string &GetName() const { return m_name; } + + void SetName(llvm::StringRef name) { m_name = name.str(); } + /// Find a binary on the system and return its Module, /// or return an existing Module that is already in the Target. /// @@ -1520,6 +1524,7 @@ /// detect that code is running on the private state thread. std::recursive_mutex m_private_mutex; Arch m_arch; + std::string m_name; ModuleList m_images; ///< The list of images for this process (shared /// libraries and anything dynamically loaded). SectionLoadHistory m_section_load_history; Index: lldb/include/lldb/API/SBTarget.h =================================================================== --- lldb/include/lldb/API/SBTarget.h +++ lldb/include/lldb/API/SBTarget.h @@ -328,6 +328,10 @@ const char *GetABIName(); + const char *GetName() const; + + void SetName(const char *name); + /// Architecture data byte width accessor /// /// \return
_______________________________________________ lldb-commits mailing list lldb-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits