Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions lldb/include/lldb/API/SBTarget.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class LLDB_API SBTarget {
eBroadcastBitWatchpointChanged = (1 << 3),
eBroadcastBitSymbolsLoaded = (1 << 4),
eBroadcastBitSymbolsChanged = (1 << 5),
eBroadcastBitNewTargetCreated = (1 << 6),
};

// Constructors
Expand Down Expand Up @@ -365,6 +366,16 @@ class LLDB_API SBTarget {
/// LLDB_INVALID_GLOBALLY_UNIQUE_TARGET_ID if the target is invalid.
lldb::user_id_t GetGloballyUniqueID() const;

/// Get the target session name for this target.
///
/// The target session name provides a meaningful name for IDEs or tools to
/// display to help the user identify the origin and purpose of the target.
///
/// \return
/// The target session name for this target, or nullptr if the target is
/// invalid or has no target session name.
const char *GetTargetSessionName() const;

SBError SetLabel(const char *label);

/// Architecture opcode byte size width accessor
Expand Down
30 changes: 29 additions & 1 deletion lldb/include/lldb/Target/Target.h
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,7 @@ class Target : public std::enable_shared_from_this<Target>,
eBroadcastBitWatchpointChanged = (1 << 3),
eBroadcastBitSymbolsLoaded = (1 << 4),
eBroadcastBitSymbolsChanged = (1 << 5),
eBroadcastBitNewTargetCreated = (1 << 6),
};

// These two functions fill out the Broadcaster interface:
Expand Down Expand Up @@ -622,6 +623,30 @@ class Target : public std::enable_shared_from_this<Target>,
/// requirements.
llvm::Error SetLabel(llvm::StringRef label);

/// Get the target session name for this target.
///
/// Provides a meaningful name for IDEs or tools to display for dynamically
/// created targets. Defaults to "Session {ID}" based on the globally unique
/// ID.
///
/// \return
/// The target session name for this target.
const std::string &GetTargetSessionName() { return m_target_session_name; }

/// Set the target session name for this target.
///
/// This should typically be set along with the event
/// eBroadcastBitNewTargetCreated. Useful for scripts or triggers that
/// automatically create targets and want to provide meaningful names that
/// IDEs or other tools can display to help users identify the origin and
/// purpose of each target.
///
/// \param[in] target_session_name
/// The target session name to set for this target.
void SetTargetSessionName(llvm::StringRef target_session_name) {
m_target_session_name = target_session_name.str();
}

/// Find a binary on the system and return its Module,
/// or return an existing Module that is already in the Target.
///
Expand Down Expand Up @@ -1663,8 +1688,11 @@ class Target : public std::enable_shared_from_this<Target>,
bool m_is_dummy_target;
unsigned m_next_persistent_variable_index = 0;
lldb::user_id_t m_target_unique_id =
LLDB_INVALID_GLOBALLY_UNIQUE_TARGET_ID; /// The globally unique ID
LLDB_INVALID_GLOBALLY_UNIQUE_TARGET_ID; ///< The globally unique ID
/// assigned to this target
std::string m_target_session_name; ///< The target session name for this
/// target, used to name debugging
/// sessions in DAP.
/// An optional \a lldb_private::Trace object containing processor trace
/// information of this target.
lldb::TraceSP m_trace_sp;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -785,6 +785,7 @@ def request_attach(
*,
program: Optional[str] = None,
pid: Optional[int] = None,
targetId: Optional[int] = None,
waitFor=False,
initCommands: Optional[list[str]] = None,
preRunCommands: Optional[list[str]] = None,
Expand All @@ -804,6 +805,8 @@ def request_attach(
args_dict["pid"] = pid
if program is not None:
args_dict["program"] = program
if targetId is not None:
args_dict["targetId"] = targetId
if waitFor:
args_dict["waitFor"] = waitFor
args_dict["initCommands"] = self.init_commands
Expand Down
8 changes: 8 additions & 0 deletions lldb/source/API/SBTarget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1641,6 +1641,14 @@ lldb::user_id_t SBTarget::GetGloballyUniqueID() const {
return LLDB_INVALID_GLOBALLY_UNIQUE_TARGET_ID;
}

const char *SBTarget::GetTargetSessionName() const {
LLDB_INSTRUMENT_VA(this);

if (TargetSP target_sp = GetSP())
return ConstString(target_sp->GetTargetSessionName().data()).AsCString();
return nullptr;
}

SBError SBTarget::SetLabel(const char *label) {
LLDB_INSTRUMENT_VA(this, label);

Expand Down
3 changes: 3 additions & 0 deletions lldb/source/Target/Target.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,13 +186,16 @@ Target::Target(Debugger &debugger, const ArchSpec &target_arch,
m_latest_stop_hook_id(0), m_valid(true), m_suppress_stop_hooks(false),
m_is_dummy_target(is_dummy_target),
m_target_unique_id(g_target_unique_id++),
m_target_session_name(
llvm::formatv("Session {0}", m_target_unique_id).str()),
m_frame_recognizer_manager_up(
std::make_unique<StackFrameRecognizerManager>()) {
SetEventName(eBroadcastBitBreakpointChanged, "breakpoint-changed");
SetEventName(eBroadcastBitModulesLoaded, "modules-loaded");
SetEventName(eBroadcastBitModulesUnloaded, "modules-unloaded");
SetEventName(eBroadcastBitWatchpointChanged, "watchpoint-changed");
SetEventName(eBroadcastBitSymbolsLoaded, "symbols-loaded");
SetEventName(eBroadcastBitNewTargetCreated, "new-target-created");

CheckInWithManager();

Expand Down
11 changes: 11 additions & 0 deletions lldb/test/API/tools/lldb-dap/attach/TestDAP_attach.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,14 @@ def test_by_name_waitFor(self):
self.spawn_thread.start()
self.attach(program=program, waitFor=True)
self.continue_and_verify_pid()

def test_attach_with_invalid_targetId(self):
"""
Test that attaching with an invalid targetId fails with the expected
error message.
"""
self.build_and_create_debug_adapter()

resp = self.attach(targetId=99999, expectFailure=True)
self.assertFalse(resp["success"])
self.assertIn("Unable to find existing target", resp["body"]["error"]["format"])
1 change: 1 addition & 0 deletions lldb/tools/lldb-dap/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ add_lldb_library(lldbDAP
DAP.cpp
DAPError.cpp
DAPLog.cpp
DAPSessionManager.cpp
EventHelper.cpp
ExceptionBreakpoint.cpp
FifoFiles.cpp
Expand Down
Loading
Loading