Skip to content

Commit 7ed6442

Browse files
committed
Add GetSessionName, remove GetSessionNameFromEvent
1 parent 5b94fa5 commit 7ed6442

File tree

5 files changed

+51
-52
lines changed

5 files changed

+51
-52
lines changed

lldb/include/lldb/API/SBTarget.h

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,6 @@ class LLDB_API SBTarget {
7070
static lldb::SBModule GetModuleAtIndexFromEvent(const uint32_t idx,
7171
const lldb::SBEvent &event);
7272

73-
static const char *GetSessionNameFromEvent(const SBEvent &event);
74-
7573
static const char *GetBroadcasterClassName();
7674

7775
lldb::SBProcess GetProcess();
@@ -368,6 +366,16 @@ class LLDB_API SBTarget {
368366
/// LLDB_INVALID_GLOBALLY_UNIQUE_TARGET_ID if the target is invalid.
369367
lldb::user_id_t GetGloballyUniqueID() const;
370368

369+
/// Get the target session name for this target.
370+
///
371+
/// The target session name provides a meaningful name for IDEs or tools to
372+
/// display to help the user identify the origin and purpose of the target.
373+
///
374+
/// \return
375+
/// The target session name for this target, or nullptr if the target is
376+
/// invalid or has no target session name.
377+
const char *GetTargetSessionName() const;
378+
371379
SBError SetLabel(const char *label);
372380

373381
/// Architecture opcode byte size width accessor

lldb/include/lldb/Target/Target.h

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -557,11 +557,6 @@ class Target : public std::enable_shared_from_this<Target>,
557557
TargetEventData(const lldb::TargetSP &target_sp,
558558
const ModuleList &module_list);
559559

560-
TargetEventData(const lldb::TargetSP &target_sp, std::string session_name);
561-
562-
TargetEventData(const lldb::TargetSP &target_sp,
563-
const ModuleList &module_list, std::string session_name);
564-
565560
~TargetEventData() override;
566561

567562
static llvm::StringRef GetFlavorString();
@@ -570,8 +565,6 @@ class Target : public std::enable_shared_from_this<Target>,
570565
return TargetEventData::GetFlavorString();
571566
}
572567

573-
static llvm::StringRef GetSessionNameFromEvent(const Event *event_ptr);
574-
575568
void Dump(Stream *s) const override;
576569

577570
static const TargetEventData *GetEventDataFromEvent(const Event *event_ptr);
@@ -587,7 +580,6 @@ class Target : public std::enable_shared_from_this<Target>,
587580
private:
588581
lldb::TargetSP m_target_sp;
589582
ModuleList m_module_list;
590-
std::string m_session_name;
591583

592584
TargetEventData(const TargetEventData &) = delete;
593585
const TargetEventData &operator=(const TargetEventData &) = delete;
@@ -631,6 +623,30 @@ class Target : public std::enable_shared_from_this<Target>,
631623
/// requirements.
632624
llvm::Error SetLabel(llvm::StringRef label);
633625

626+
/// Get the target session name for this target.
627+
///
628+
/// Provides a meaningful name for IDEs or tools to display for dynamically
629+
/// created targets. Defaults to "Session {ID}" based on the globally unique
630+
/// ID.
631+
///
632+
/// \return
633+
/// The target session name for this target.
634+
const std::string &GetTargetSessionName() { return m_target_session_name; }
635+
636+
/// Set the target session name for this target.
637+
///
638+
/// This should typically be set along with the event
639+
/// eBroadcastBitNewTargetCreated. Useful for scripts or triggers that
640+
/// automatically create targets and want to provide meaningful names that
641+
/// IDEs or other tools can display to help users identify the origin and
642+
/// purpose of each target.
643+
///
644+
/// \param[in] target_session_name
645+
/// The target session name to set for this target.
646+
void SetTargetSessionName(llvm::StringRef target_session_name) {
647+
m_target_session_name = target_session_name.str();
648+
}
649+
634650
/// Find a binary on the system and return its Module,
635651
/// or return an existing Module that is already in the Target.
636652
///
@@ -1672,8 +1688,11 @@ class Target : public std::enable_shared_from_this<Target>,
16721688
bool m_is_dummy_target;
16731689
unsigned m_next_persistent_variable_index = 0;
16741690
lldb::user_id_t m_target_unique_id =
1675-
LLDB_INVALID_GLOBALLY_UNIQUE_TARGET_ID; /// The globally unique ID
1691+
LLDB_INVALID_GLOBALLY_UNIQUE_TARGET_ID; ///< The globally unique ID
16761692
/// assigned to this target
1693+
std::string m_target_session_name; ///< The target session name for this
1694+
/// target, used to name debugging
1695+
/// sessions in DAP.
16771696
/// An optional \a lldb_private::Trace object containing processor trace
16781697
/// information of this target.
16791698
lldb::TraceSP m_trace_sp;

lldb/source/API/SBTarget.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -145,14 +145,6 @@ SBModule SBTarget::GetModuleAtIndexFromEvent(const uint32_t idx,
145145
return SBModule(module_list.GetModuleAtIndex(idx));
146146
}
147147

148-
const char *SBTarget::GetSessionNameFromEvent(const SBEvent &event) {
149-
LLDB_INSTRUMENT_VA(event);
150-
151-
return ConstString(
152-
Target::TargetEventData::GetSessionNameFromEvent(event.get()))
153-
.AsCString();
154-
}
155-
156148
const char *SBTarget::GetBroadcasterClassName() {
157149
LLDB_INSTRUMENT();
158150

@@ -1649,6 +1641,14 @@ lldb::user_id_t SBTarget::GetGloballyUniqueID() const {
16491641
return LLDB_INVALID_GLOBALLY_UNIQUE_TARGET_ID;
16501642
}
16511643

1644+
const char *SBTarget::GetTargetSessionName() const {
1645+
LLDB_INSTRUMENT_VA(this);
1646+
1647+
if (TargetSP target_sp = GetSP())
1648+
return ConstString(target_sp->GetTargetSessionName().data()).AsCString();
1649+
return nullptr;
1650+
}
1651+
16521652
SBError SBTarget::SetLabel(const char *label) {
16531653
LLDB_INSTRUMENT_VA(this, label);
16541654

lldb/source/Target/Target.cpp

Lines changed: 4 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,8 @@ Target::Target(Debugger &debugger, const ArchSpec &target_arch,
186186
m_latest_stop_hook_id(0), m_valid(true), m_suppress_stop_hooks(false),
187187
m_is_dummy_target(is_dummy_target),
188188
m_target_unique_id(g_target_unique_id++),
189+
m_target_session_name(
190+
llvm::formatv("Session {0}", m_target_unique_id).str()),
189191
m_frame_recognizer_manager_up(
190192
std::make_unique<StackFrameRecognizerManager>()) {
191193
SetEventName(eBroadcastBitBreakpointChanged, "breakpoint-changed");
@@ -5170,21 +5172,11 @@ void TargetProperties::SetDebugUtilityExpression(bool debug) {
51705172
// Target::TargetEventData
51715173

51725174
Target::TargetEventData::TargetEventData(const lldb::TargetSP &target_sp)
5173-
: TargetEventData(target_sp, ModuleList(), "") {}
5175+
: EventData(), m_target_sp(target_sp), m_module_list() {}
51745176

51755177
Target::TargetEventData::TargetEventData(const lldb::TargetSP &target_sp,
51765178
const ModuleList &module_list)
5177-
: TargetEventData(target_sp, module_list, "") {}
5178-
5179-
Target::TargetEventData::TargetEventData(const lldb::TargetSP &target_sp,
5180-
std::string session_name)
5181-
: TargetEventData(target_sp, ModuleList(), std::move(session_name)) {}
5182-
5183-
Target::TargetEventData::TargetEventData(const lldb::TargetSP &target_sp,
5184-
const ModuleList &module_list,
5185-
std::string session_name)
5186-
: EventData(), m_target_sp(target_sp), m_module_list(module_list),
5187-
m_session_name(std::move(session_name)) {}
5179+
: EventData(), m_target_sp(target_sp), m_module_list(module_list) {}
51885180

51895181
Target::TargetEventData::~TargetEventData() = default;
51905182

@@ -5220,25 +5212,6 @@ TargetSP Target::TargetEventData::GetTargetFromEvent(const Event *event_ptr) {
52205212
return target_sp;
52215213
}
52225214

5223-
llvm::StringRef
5224-
Target::TargetEventData::GetSessionNameFromEvent(const Event *event_ptr) {
5225-
const TargetEventData *event_data = GetEventDataFromEvent(event_ptr);
5226-
if (!event_data)
5227-
return llvm::StringRef();
5228-
5229-
if (!event_data->m_session_name.empty())
5230-
return event_data->m_session_name;
5231-
5232-
// Generate default session name if not provided
5233-
if (event_data->m_target_sp) {
5234-
lldb::user_id_t target_id = event_data->m_target_sp->GetGloballyUniqueID();
5235-
std::string default_name = llvm::formatv("Session {0}", target_id).str();
5236-
return ConstString(default_name).GetStringRef();
5237-
}
5238-
5239-
return llvm::StringRef();
5240-
}
5241-
52425215
ModuleList
52435216
Target::TargetEventData::GetModuleListFromEvent(const Event *event_ptr) {
52445217
ModuleList module_list;

lldb/tools/lldb-dap/DAP.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1581,8 +1581,7 @@ void DAP::EventThread() {
15811581
// process name.
15821582
attach_config.try_emplace("type", "lldb");
15831583
attach_config.try_emplace("targetId", target_id);
1584-
const char *session_name =
1585-
lldb::SBTarget::GetSessionNameFromEvent(event);
1584+
const char *session_name = target.GetTargetSessionName();
15861585
attach_config.try_emplace("name", session_name);
15871586

15881587
// 2. Construct the main 'startDebugging' request arguments.

0 commit comments

Comments
 (0)