Skip to content

Commit f45e749

Browse files
authored
Merge pull request #7126 from medismailben/stable/20221013
[lldb/Target] Add ability to set a label to targets
2 parents b1b8ed9 + 4464585 commit f45e749

File tree

10 files changed

+220
-12
lines changed

10 files changed

+220
-12
lines changed

lldb/bindings/python/static-binding/LLDBWrapPython.cpp

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60815,6 +60815,70 @@ SWIGINTERN PyObject *_wrap_SBTarget_GetABIName(PyObject *self, PyObject *args) {
6081560815
}
6081660816

6081760817

60818+
SWIGINTERN PyObject *_wrap_SBTarget_GetLabel(PyObject *self, PyObject *args) {
60819+
PyObject *resultobj = 0;
60820+
lldb::SBTarget *arg1 = (lldb::SBTarget *) 0 ;
60821+
void *argp1 = 0 ;
60822+
int res1 = 0 ;
60823+
PyObject *swig_obj[1] ;
60824+
char *result = 0 ;
60825+
60826+
if (!args) SWIG_fail;
60827+
swig_obj[0] = args;
60828+
res1 = SWIG_ConvertPtr(swig_obj[0], &argp1,SWIGTYPE_p_lldb__SBTarget, 0 | 0 );
60829+
if (!SWIG_IsOK(res1)) {
60830+
SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "SBTarget_GetLabel" "', argument " "1"" of type '" "lldb::SBTarget const *""'");
60831+
}
60832+
arg1 = reinterpret_cast< lldb::SBTarget * >(argp1);
60833+
{
60834+
SWIG_PYTHON_THREAD_BEGIN_ALLOW;
60835+
result = (char *)((lldb::SBTarget const *)arg1)->GetLabel();
60836+
SWIG_PYTHON_THREAD_END_ALLOW;
60837+
}
60838+
resultobj = SWIG_FromCharPtr((const char *)result);
60839+
return resultobj;
60840+
fail:
60841+
return NULL;
60842+
}
60843+
60844+
60845+
SWIGINTERN PyObject *_wrap_SBTarget_SetLabel(PyObject *self, PyObject *args) {
60846+
PyObject *resultobj = 0;
60847+
lldb::SBTarget *arg1 = (lldb::SBTarget *) 0 ;
60848+
char *arg2 = (char *) 0 ;
60849+
void *argp1 = 0 ;
60850+
int res1 = 0 ;
60851+
int res2 ;
60852+
char *buf2 = 0 ;
60853+
int alloc2 = 0 ;
60854+
PyObject *swig_obj[2] ;
60855+
lldb::SBError result;
60856+
60857+
if (!SWIG_Python_UnpackTuple(args, "SBTarget_SetLabel", 2, 2, swig_obj)) SWIG_fail;
60858+
res1 = SWIG_ConvertPtr(swig_obj[0], &argp1,SWIGTYPE_p_lldb__SBTarget, 0 | 0 );
60859+
if (!SWIG_IsOK(res1)) {
60860+
SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "SBTarget_SetLabel" "', argument " "1"" of type '" "lldb::SBTarget *""'");
60861+
}
60862+
arg1 = reinterpret_cast< lldb::SBTarget * >(argp1);
60863+
res2 = SWIG_AsCharPtrAndSize(swig_obj[1], &buf2, NULL, &alloc2);
60864+
if (!SWIG_IsOK(res2)) {
60865+
SWIG_exception_fail(SWIG_ArgError(res2), "in method '" "SBTarget_SetLabel" "', argument " "2"" of type '" "char const *""'");
60866+
}
60867+
arg2 = reinterpret_cast< char * >(buf2);
60868+
{
60869+
SWIG_PYTHON_THREAD_BEGIN_ALLOW;
60870+
result = (arg1)->SetLabel((char const *)arg2);
60871+
SWIG_PYTHON_THREAD_END_ALLOW;
60872+
}
60873+
resultobj = SWIG_NewPointerObj((new lldb::SBError(result)), SWIGTYPE_p_lldb__SBError, SWIG_POINTER_OWN | 0 );
60874+
if (alloc2 == SWIG_NEWOBJ) delete[] buf2;
60875+
return resultobj;
60876+
fail:
60877+
if (alloc2 == SWIG_NEWOBJ) delete[] buf2;
60878+
return NULL;
60879+
}
60880+
60881+
6081860882
SWIGINTERN PyObject *_wrap_SBTarget_GetDataByteSize(PyObject *self, PyObject *args) {
6081960883
PyObject *resultobj = 0;
6082060884
lldb::SBTarget *arg1 = (lldb::SBTarget *) 0 ;
@@ -90469,6 +90533,8 @@ static PyMethodDef SwigMethods[] = {
9046990533
{ "SBTarget_GetAddressByteSize", _wrap_SBTarget_GetAddressByteSize, METH_O, "SBTarget_GetAddressByteSize(SBTarget self) -> uint32_t"},
9047090534
{ "SBTarget_GetTriple", _wrap_SBTarget_GetTriple, METH_O, "SBTarget_GetTriple(SBTarget self) -> char const *"},
9047190535
{ "SBTarget_GetABIName", _wrap_SBTarget_GetABIName, METH_O, "SBTarget_GetABIName(SBTarget self) -> char const *"},
90536+
{ "SBTarget_GetLabel", _wrap_SBTarget_GetLabel, METH_O, "SBTarget_GetLabel(SBTarget self) -> char const *"},
90537+
{ "SBTarget_SetLabel", _wrap_SBTarget_SetLabel, METH_VARARGS, "SBTarget_SetLabel(SBTarget self, char const * label) -> SBError"},
9047290538
{ "SBTarget_GetDataByteSize", _wrap_SBTarget_GetDataByteSize, METH_O, "\n"
9047390539
"SBTarget_GetDataByteSize(SBTarget self) -> uint32_t\n"
9047490540
"\n"

lldb/bindings/python/static-binding/lldb.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10594,6 +10594,14 @@ def GetABIName(self):
1059410594
r"""GetABIName(SBTarget self) -> char const *"""
1059510595
return _lldb.SBTarget_GetABIName(self)
1059610596

10597+
def GetLabel(self):
10598+
r"""GetLabel(SBTarget self) -> char const *"""
10599+
return _lldb.SBTarget_GetLabel(self)
10600+
10601+
def SetLabel(self, label):
10602+
r"""SetLabel(SBTarget self, char const * label) -> SBError"""
10603+
return _lldb.SBTarget_SetLabel(self, label)
10604+
1059710605
def GetDataByteSize(self):
1059810606
r"""
1059910607
GetDataByteSize(SBTarget self) -> uint32_t

lldb/include/lldb/API/SBTarget.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,10 @@ class LLDB_API SBTarget {
328328

329329
const char *GetABIName();
330330

331+
const char *GetLabel() const;
332+
333+
SBError SetLabel(const char *label);
334+
331335
/// Architecture data byte width accessor
332336
///
333337
/// \return

lldb/include/lldb/Target/Target.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -633,6 +633,17 @@ class Target : public std::enable_shared_from_this<Target>,
633633

634634
bool IsDummyTarget() const { return m_is_dummy_target; }
635635

636+
const std::string &GetLabel() const { return m_label; }
637+
638+
/// Set a label for a target.
639+
///
640+
/// The label cannot be used by another target or be only integral.
641+
///
642+
/// \return
643+
/// The label for this target or an error if the label didn't match the
644+
/// requirements.
645+
llvm::Error SetLabel(llvm::StringRef label);
646+
636647
/// Find a binary on the system and return its Module,
637648
/// or return an existing Module that is already in the Target.
638649
///
@@ -1643,6 +1654,7 @@ class Target : public std::enable_shared_from_this<Target>,
16431654
/// detect that code is running on the private state thread.
16441655
std::recursive_mutex m_private_mutex;
16451656
Arch m_arch;
1657+
std::string m_label;
16461658
ModuleList m_images; ///< The list of images for this process (shared
16471659
/// libraries and anything dynamically loaded).
16481660
SectionLoadHistory m_section_load_history;

lldb/include/lldb/Target/TargetList.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ class TargetList : public Broadcaster {
115115
/// in \a target_sp which can then be properly released.
116116
bool DeleteTarget(lldb::TargetSP &target_sp);
117117

118-
int GetNumTargets() const;
118+
size_t GetNumTargets() const;
119119

120120
lldb::TargetSP GetTargetAtIndex(uint32_t index) const;
121121

lldb/source/API/SBTarget.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1637,6 +1637,26 @@ const char *SBTarget::GetABIName() {
16371637
return nullptr;
16381638
}
16391639

1640+
const char *SBTarget::GetLabel() const {
1641+
LLDB_INSTRUMENT_VA(this);
1642+
1643+
TargetSP target_sp(GetSP());
1644+
if (!target_sp)
1645+
return nullptr;
1646+
1647+
return ConstString(target_sp->GetLabel().data()).AsCString();
1648+
}
1649+
1650+
SBError SBTarget::SetLabel(const char *label) {
1651+
LLDB_INSTRUMENT_VA(this, label);
1652+
1653+
TargetSP target_sp(GetSP());
1654+
if (!target_sp)
1655+
return Status("Couldn't get internal target object.");
1656+
1657+
return Status(target_sp->SetLabel(label));
1658+
}
1659+
16401660
uint32_t SBTarget::GetDataByteSize() {
16411661
LLDB_INSTRUMENT_VA(this);
16421662

lldb/source/Commands/CommandObjectTarget.cpp

Lines changed: 46 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,14 @@ static void DumpTargetInfo(uint32_t target_idx, Target *target,
8282
if (!exe_valid)
8383
::strcpy(exe_path, "<none>");
8484

85-
strm.Printf("%starget #%u: %s", prefix_cstr ? prefix_cstr : "", target_idx,
86-
exe_path);
85+
std::string formatted_label = "";
86+
const std::string &label = target->GetLabel();
87+
if (!label.empty()) {
88+
formatted_label = " (" + label + ")";
89+
}
90+
91+
strm.Printf("%starget #%u%s: %s", prefix_cstr ? prefix_cstr : "", target_idx,
92+
formatted_label.data(), exe_path);
8793

8894
uint32_t properties = 0;
8995
if (target_arch.IsValid()) {
@@ -209,6 +215,8 @@ class CommandObjectTargetCreate : public CommandObjectParsed {
209215
m_platform_options(true), // Include the --platform option.
210216
m_core_file(LLDB_OPT_SET_1, false, "core", 'c', 0, eArgTypeFilename,
211217
"Fullpath to a core file to use for this target."),
218+
m_label(LLDB_OPT_SET_1, false, "label", 'l', 0, eArgTypeName,
219+
"Optional name for this target.", nullptr),
212220
m_symbol_file(LLDB_OPT_SET_1, false, "symfile", 's', 0,
213221
eArgTypeFilename,
214222
"Fullpath to a stand alone debug "
@@ -234,6 +242,7 @@ class CommandObjectTargetCreate : public CommandObjectParsed {
234242
m_option_group.Append(&m_arch_option, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
235243
m_option_group.Append(&m_platform_options, LLDB_OPT_SET_ALL, 1);
236244
m_option_group.Append(&m_core_file, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
245+
m_option_group.Append(&m_label, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
237246
m_option_group.Append(&m_symbol_file, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
238247
m_option_group.Append(&m_remote_file, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
239248
m_option_group.Append(&m_add_dependents, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
@@ -302,6 +311,14 @@ class CommandObjectTargetCreate : public CommandObjectParsed {
302311
return false;
303312
}
304313

314+
const llvm::StringRef label =
315+
m_label.GetOptionValue().GetCurrentValueAsRef();
316+
if (!label.empty()) {
317+
if (auto E = target_sp->SetLabel(label))
318+
result.SetError(std::move(E));
319+
return false;
320+
}
321+
305322
auto on_error = llvm::make_scope_exit(
306323
[&target_list = debugger.GetTargetList(), &target_sp]() {
307324
target_list.DeleteTarget(target_sp);
@@ -454,6 +471,7 @@ class CommandObjectTargetCreate : public CommandObjectParsed {
454471
OptionGroupArchitecture m_arch_option;
455472
OptionGroupPlatform m_platform_options;
456473
OptionGroupFile m_core_file;
474+
OptionGroupString m_label;
457475
OptionGroupFile m_symbol_file;
458476
OptionGroupFile m_remote_file;
459477
OptionGroupDependents m_add_dependents;
@@ -502,11 +520,11 @@ class CommandObjectTargetSelect : public CommandObjectParsed {
502520
protected:
503521
bool DoExecute(Args &args, CommandReturnObject &result) override {
504522
if (args.GetArgumentCount() == 1) {
505-
const char *target_idx_arg = args.GetArgumentAtIndex(0);
506-
uint32_t target_idx;
507-
if (llvm::to_integer(target_idx_arg, target_idx)) {
508-
TargetList &target_list = GetDebugger().GetTargetList();
509-
const uint32_t num_targets = target_list.GetNumTargets();
523+
const char *target_identifier = args.GetArgumentAtIndex(0);
524+
uint32_t target_idx = LLDB_INVALID_INDEX32;
525+
TargetList &target_list = GetDebugger().GetTargetList();
526+
const uint32_t num_targets = target_list.GetNumTargets();
527+
if (llvm::to_integer(target_identifier, target_idx)) {
510528
if (target_idx < num_targets) {
511529
target_list.SetSelectedTarget(target_idx);
512530
Stream &strm = result.GetOutputStream();
@@ -525,8 +543,26 @@ class CommandObjectTargetSelect : public CommandObjectParsed {
525543
}
526544
}
527545
} else {
528-
result.AppendErrorWithFormat("invalid index string value '%s'\n",
529-
target_idx_arg);
546+
for (size_t i = 0; i < num_targets; i++) {
547+
if (TargetSP target_sp = target_list.GetTargetAtIndex(i)) {
548+
const std::string &label = target_sp->GetLabel();
549+
if (!label.empty() && label == target_identifier) {
550+
target_idx = i;
551+
break;
552+
}
553+
}
554+
}
555+
556+
if (target_idx != LLDB_INVALID_INDEX32) {
557+
target_list.SetSelectedTarget(target_idx);
558+
Stream &strm = result.GetOutputStream();
559+
bool show_stopped_process_status = false;
560+
DumpTargetList(target_list, show_stopped_process_status, strm);
561+
result.SetStatus(eReturnStatusSuccessFinishResult);
562+
} else {
563+
result.AppendErrorWithFormat("invalid index string value '%s'\n",
564+
target_identifier);
565+
}
530566
}
531567
} else {
532568
result.AppendError(
@@ -575,7 +611,7 @@ class CommandObjectTargetDelete : public CommandObjectParsed {
575611
TargetSP target_sp;
576612

577613
if (m_all_option.GetOptionValue()) {
578-
for (int i = 0; i < target_list.GetNumTargets(); ++i)
614+
for (size_t i = 0; i < target_list.GetNumTargets(); ++i)
579615
delete_target_list.push_back(target_list.GetTargetAtIndex(i));
580616
} else if (argc > 0) {
581617
const uint32_t num_targets = target_list.GetNumTargets();

lldb/source/Target/Target.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@
6969

7070
#include <memory>
7171
#include <mutex>
72+
#include <optional>
73+
#include <sstream>
7274

7375
#ifdef LLDB_ENABLE_SWIFT
7476
#include "Plugins/TypeSystem/Swift/SwiftASTContext.h"
@@ -2930,6 +2932,26 @@ void Target::SetDefaultArchitecture(const ArchSpec &arch) {
29302932
Target::GetGlobalProperties().SetDefaultArchitecture(arch);
29312933
}
29322934

2935+
llvm::Error Target::SetLabel(llvm::StringRef label) {
2936+
size_t n = LLDB_INVALID_INDEX32;
2937+
if (llvm::to_integer(label, n))
2938+
return llvm::make_error<llvm::StringError>(
2939+
"Cannot use integer as target label.", llvm::inconvertibleErrorCode());
2940+
TargetList &targets = GetDebugger().GetTargetList();
2941+
for (size_t i = 0; i < targets.GetNumTargets(); i++) {
2942+
TargetSP target_sp = targets.GetTargetAtIndex(i);
2943+
if (target_sp && target_sp->GetLabel() == label) {
2944+
return llvm::make_error<llvm::StringError>(
2945+
llvm::formatv("Cannot use label '{0}' since it's set in target #{1}.",
2946+
label, i),
2947+
llvm::inconvertibleErrorCode());
2948+
}
2949+
}
2950+
2951+
m_label = label.str();
2952+
return llvm::Error::success();
2953+
}
2954+
29332955
Target *Target::GetTargetFromContexts(const ExecutionContext *exe_ctx_ptr,
29342956
const SymbolContext *sc_ptr) {
29352957
// The target can either exist in the "process" of ExecutionContext, or in

lldb/source/Target/TargetList.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -491,7 +491,7 @@ uint32_t TargetList::SignalIfRunning(lldb::pid_t pid, int signo) {
491491
return num_signals_sent;
492492
}
493493

494-
int TargetList::GetNumTargets() const {
494+
size_t TargetList::GetNumTargets() const {
495495
std::lock_guard<std::recursive_mutex> guard(m_target_list_mutex);
496496
return m_target_list.size();
497497
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# REQUIRES: python
2+
# UNSUPPORTED: system-windows
3+
# RUN: %lldb -b -o 'settings set interpreter.stop-command-source-on-error false' -s %s 2>&1 | FileCheck %s
4+
5+
target create -l "ls" /bin/ls
6+
target list
7+
# CHECK: * target #0 (ls): /bin/ls
8+
9+
script lldb.target.SetLabel("")
10+
target list
11+
# CHECK: * target #0: /bin/ls
12+
13+
target create -l "cat" /bin/cat
14+
target list
15+
# CHECK: target #0: /bin/ls
16+
# CHECK-NEXT: * target #1 (cat): /bin/cat
17+
18+
target create -l "cat" /bin/cat
19+
# CHECK: Cannot use label 'cat' since it's set in target #1.
20+
21+
target create -l 42 /bin/cat
22+
# CHECK: error: Cannot use integer as target label.
23+
24+
target select 0
25+
# CHECK: * target #0: /bin/ls
26+
# CHECK-NEXT: target #1 (cat): /bin/cat
27+
28+
target select cat
29+
# CHECK: target #0: /bin/ls
30+
# CHECK-NEXT: * target #1 (cat): /bin/cat
31+
32+
script lldb.target.GetLabel()
33+
# CHECK: 'cat'
34+
35+
script lldb.debugger.GetTargetAtIndex(0).SetLabel('Not cat')
36+
# CHECK: success
37+
38+
target list
39+
# CHECK: target #0 (Not cat): /bin/ls
40+
# CHECK-NEXT: * target #1 (cat): /bin/cat

0 commit comments

Comments
 (0)