[Lldb-commits] [lldb] [lldb] Add `scripting extension instances` command (PR #226043)

Jonas Devlieghere via lldb-commits lldb-commits at lists.llvm.org
Fri Sep 25 11:59:18 PDT 2026


================
@@ -0,0 +1,153 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "lldb/Interpreter/ScriptedInstanceRegistry.h"
+#include "lldb/Core/PluginManager.h"
+#include "lldb/Interpreter/ScriptInterpreter.h"
+#include "lldb/Utility/AnsiTerminal.h"
+#include "lldb/Utility/Stream.h"
+
+#include "llvm/ADT/StringMap.h"
+
+#include <tuple>
+
+using namespace lldb;
+using namespace lldb_private;
+
+static bool HasArgs(const ScriptedInstanceInfo &info) {
+  return info.args_sp && info.args_sp->GetSize();
+}
+
+std::vector<ScriptedInstanceGroup> ScriptedInstanceRegistry::GetInstanceGroups(
+    llvm::ArrayRef<ScriptedExtension> extensions) const {
+  // Unknown names map to eScriptedExtensionInvalid, the zero value.
+  llvm::StringMap<ScriptedExtension> extension_by_plugin;
+  for (uint32_t i = 0; i < PluginManager::GetNumScriptedInterfaces(); i++)
+    extension_by_plugin[PluginManager::GetScriptedInterfaceNameAtIndex(i)] =
+        PluginManager::GetScriptedInterfaceExtensionAtIndex(i);
+
+  std::vector<ScriptedInstanceGroup> groups;
+  for (ScriptedInstanceInfo &info : GetInstances()) {
+    ScriptedExtension extension = extension_by_plugin.lookup(info.plugin_name);
+    if (!extensions.empty() && !llvm::is_contained(extensions, extension))
+      continue;
+
+    auto it = llvm::find_if(groups, [&](const ScriptedInstanceGroup &group) {
+      return group.class_name == info.class_name &&
+             group.source_path == info.source_path &&
+             group.extension == extension;
+    });
+    if (it == groups.end()) {
+      it = groups.insert(groups.end(), ScriptedInstanceGroup());
+      it->class_name = info.class_name;
+      it->extension = extension;
+      it->source_path = info.source_path;
+    }
+    it->instances.push_back(std::move(info));
+  }
+
+  llvm::sort(groups, [](const ScriptedInstanceGroup &lhs,
+                        const ScriptedInstanceGroup &rhs) {
+    return std::make_tuple(llvm::StringRef(lhs.class_name),
+                           lhs.source_path.GetPath(), lhs.extension) <
+           std::make_tuple(llvm::StringRef(rhs.class_name),
+                           rhs.source_path.GetPath(), rhs.extension);
+  });
+  return groups;
+}
+
+StructuredData::DictionarySP ScriptedInstanceGroup::ToStructuredData() const {
+  auto group_sp = std::make_shared<StructuredData::Dictionary>();
+  group_sp->AddStringItem("class_name", class_name);
+  group_sp->AddStringItem("extension",
+                          ScriptInterpreter::ExtensionToString(extension));
+  if (source_path)
+    group_sp->AddStringItem("source_path", source_path.GetPath());
+
+  auto instances_sp = std::make_shared<StructuredData::Array>();
+  for (const ScriptedInstanceInfo &info : instances) {
+    auto instance_sp = std::make_shared<StructuredData::Dictionary>();
+    instance_sp->AddStringItem("uuid", info.uuid.GetAsString());
+    if (info.object_address != LLDB_INVALID_ADDRESS)
+      instance_sp->AddIntegerItem("address", info.object_address);
+    if (HasArgs(info))
+      instance_sp->AddItem("args", info.args_sp);
+    instances_sp->AddItem(instance_sp);
+  }
+  group_sp->AddItem("instances", instances_sp);
+  return group_sp;
+}
+
+void ScriptedInstanceGroup::Dump(Stream &s, bool use_color) const {
+  constexpr llvm::StringLiteral label_code =
+      ANSI_ESCAPE1(ANSI_FG_COLOR_GREEN) ANSI_ESCAPE1(ANSI_CTRL_BOLD);
+  constexpr llvm::StringLiteral name_code =
+      ANSI_ESCAPE1(ANSI_FG_COLOR_CYAN) ANSI_ESCAPE1(ANSI_CTRL_BOLD);
+  constexpr llvm::StringLiteral reset_code = ANSI_ESCAPE1(ANSI_CTRL_NORMAL);
----------------
JDevlieghere wrote:

This feels like it doesn't belong here. You should use `ansi::FormatAnsiTerminalCodes`. It also seems that this, like all the other colors, should be configurable as different terminal teams interpret those colors differently.

https://github.com/llvm/llvm-project/pull/226043


More information about the lldb-commits mailing list