Skip to content

Commit 9854d30

Browse files
committed
[lldb] Unify printing of internal and user stop hooks
Unify the printing of internal and user (non-internal) stop hooks to make it more similar to how we treat it for breakpoints. This means replacing the `GetNumStopHooks()` and `GetStopHookAtIndex()` with a copy of the stop hooks (in C++20, we can change this to a constant view). These functions were ever only used to iterate the entire collection of stop hooks and the implementation of `GetStopHookAtIndex()` turns this into a `N^2` operation. This change also allows us to cleanup a bit of code at the call site.
1 parent 3370a82 commit 9854d30

File tree

4 files changed

+26
-37
lines changed

4 files changed

+26
-37
lines changed

lldb/include/lldb/Target/Target.h

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1552,23 +1552,7 @@ class Target : public std::enable_shared_from_this<Target>,
15521552

15531553
void SetAllStopHooksActiveState(bool active_state);
15541554

1555-
const std::vector<StopHookSP> &GetInternalStopHooks() const {
1556-
return m_internal_stop_hooks;
1557-
}
1558-
1559-
size_t GetNumStopHooks() const { return m_stop_hooks.size(); }
1560-
1561-
StopHookSP GetStopHookAtIndex(size_t index) {
1562-
if (index >= GetNumStopHooks())
1563-
return StopHookSP();
1564-
StopHookCollection::iterator pos = m_stop_hooks.begin();
1565-
1566-
while (index > 0) {
1567-
pos++;
1568-
index--;
1569-
}
1570-
return (*pos).second;
1571-
}
1555+
const std::vector<StopHookSP> GetStopHooks(bool internal = false) const;
15721556

15731557
lldb::PlatformSP GetPlatform() { return m_platform_sp; }
15741558

lldb/source/Commands/CommandCompletions.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -777,13 +777,11 @@ void CommandCompletions::StopHookIDs(CommandInterpreter &interpreter,
777777
if (!target_sp)
778778
return;
779779

780-
const size_t num = target_sp->GetNumStopHooks();
781-
for (size_t idx = 0; idx < num; ++idx) {
780+
for (auto &stophook_sp : target_sp->GetStopHooks()) {
782781
StreamString strm;
783782
// The value 11 is an offset to make the completion description looks
784783
// neater.
785784
strm.SetIndentLevel(11);
786-
const Target::StopHookSP stophook_sp = target_sp->GetStopHookAtIndex(idx);
787785
stophook_sp->GetDescription(strm, lldb::eDescriptionLevelInitial);
788786
request.TryCompleteCurrentArg(std::to_string(stophook_sp->GetID()),
789787
strm.GetString());

lldb/source/Commands/CommandObjectTarget.cpp

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5273,27 +5273,17 @@ class CommandObjectTargetStopHookList : public CommandObjectParsed {
52735273
void DoExecute(Args &command, CommandReturnObject &result) override {
52745274
Target &target = GetTarget();
52755275

5276-
if (m_options.m_internal) {
5277-
for (auto &hook : target.GetInternalStopHooks()) {
5278-
hook->GetDescription(result.GetOutputStream(), eDescriptionLevelFull);
5276+
bool printed_hook = false;
5277+
for (auto &hook : target.GetStopHooks(m_options.m_internal)) {
5278+
if (printed_hook)
52795279
result.GetOutputStream().PutCString("\n");
5280-
}
5281-
result.SetStatus(eReturnStatusSuccessFinishResult);
5282-
return;
5280+
hook->GetDescription(result.GetOutputStream(), eDescriptionLevelFull);
5281+
printed_hook = true;
52835282
}
52845283

5285-
size_t num_hooks = target.GetNumStopHooks();
5286-
if (num_hooks == 0) {
5284+
if (!printed_hook)
52875285
result.GetOutputStream().PutCString("No stop hooks.\n");
5288-
} else {
5289-
for (size_t i = 0; i < num_hooks; i++) {
5290-
Target::StopHookSP this_hook = target.GetStopHookAtIndex(i);
5291-
if (i > 0)
5292-
result.GetOutputStream().PutCString("\n");
5293-
this_hook->GetDescription(result.GetOutputStream(),
5294-
eDescriptionLevelFull);
5295-
}
5296-
}
5286+
52975287
result.SetStatus(eReturnStatusSuccessFinishResult);
52985288
}
52995289

lldb/source/Target/Target.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3107,6 +3107,23 @@ void Target::SetAllStopHooksActiveState(bool active_state) {
31073107
}
31083108
}
31093109

3110+
// FIXME: Ideally we would like to return a `const &` (const reference) instead
3111+
// of creating copy here, but that is not possible due to different container
3112+
// types. In C++20, we should be able to use `std::ranges::views::values` to
3113+
// adapt the key-pair entries in the `std::map` (behind `StopHookCollection`)
3114+
// to avoid creating the copy.
3115+
const std::vector<Target::StopHookSP>
3116+
Target::GetStopHooks(bool internal) const {
3117+
if (internal)
3118+
return m_internal_stop_hooks;
3119+
3120+
std::vector<StopHookSP> stop_hooks;
3121+
for (auto &[_, hook] : m_stop_hooks)
3122+
stop_hooks.push_back(hook);
3123+
3124+
return stop_hooks;
3125+
}
3126+
31103127
bool Target::RunStopHooks(bool at_initial_stop) {
31113128
if (m_suppress_stop_hooks)
31123129
return false;

0 commit comments

Comments
 (0)