Skip to content

Commit b31b8a8

Browse files
committed
Support multiple values in plugin list
1 parent 735dd1f commit b31b8a8

File tree

2 files changed

+70
-21
lines changed

2 files changed

+70
-21
lines changed

lldb/source/Commands/CommandObjectPlugin.cpp

Lines changed: 46 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -197,20 +197,49 @@ List only the plugin 'foo' matching a fully qualified name exactly
197197
protected:
198198
void DoExecute(Args &command, CommandReturnObject &result) override {
199199
size_t argc = command.GetArgumentCount();
200-
if (argc > 1) {
201-
result.AppendError("'plugin list' requires zero or one arguments");
202-
return;
203-
}
204-
llvm::StringRef pattern = argc ? command[0].ref() : "";
205200
result.SetStatus(eReturnStatusSuccessFinishResult);
206201

207-
bool found_matching = false;
208-
if (m_options.m_json_format) {
209-
llvm::json::Object obj = PluginManager::GetJSON(pattern);
210-
found_matching = obj.size() > 0;
211-
if (found_matching)
212-
result.AppendMessage(ConvertJSONToPrettyString(std::move(obj)));
213-
} else {
202+
// Create a temporary vector to hold the patterns to simplify the logic
203+
// for the case when the user passes no patterns
204+
std::vector<llvm::StringRef> patterns;
205+
patterns.reserve(argc == 0 ? 1 : argc);
206+
if (argc == 0)
207+
patterns.push_back("");
208+
else
209+
for (size_t i = 0; i < argc; ++i)
210+
patterns.push_back(command[i].ref());
211+
212+
if (m_options.m_json_format)
213+
OutputJsonFormat(patterns, result);
214+
else
215+
OutputTextFormat(patterns, result);
216+
}
217+
218+
private:
219+
void OutputJsonFormat(const std::vector<llvm::StringRef> &patterns,
220+
CommandReturnObject &result) {
221+
llvm::json::Object obj;
222+
bool found_empty = false;
223+
for (const llvm::StringRef pattern : patterns) {
224+
llvm::json::Object pat_obj = PluginManager::GetJSON(pattern);
225+
if (pat_obj.empty()) {
226+
found_empty = true;
227+
result.AppendErrorWithFormat(
228+
"Found no matching plugins for pattern '%s'", pattern.data());
229+
break;
230+
}
231+
for (auto &entry : pat_obj) {
232+
obj[entry.first] = std::move(entry.second);
233+
}
234+
}
235+
if (!found_empty) {
236+
result.AppendMessage(ConvertJSONToPrettyString(std::move(obj)));
237+
}
238+
}
239+
240+
void OutputTextFormat(const std::vector<llvm::StringRef> &patterns,
241+
CommandReturnObject &result) {
242+
for (const llvm::StringRef pattern : patterns) {
214243
int num_matching = ActOnMatchingPlugins(
215244
pattern, [&](const PluginNamespace &plugin_namespace,
216245
const std::vector<RegisteredPluginInfo> &plugins) {
@@ -221,11 +250,12 @@ List only the plugin 'foo' matching a fully qualified name exactly
221250
plugin.name.data(), plugin.description.data());
222251
}
223252
});
224-
found_matching = num_matching > 0;
253+
if (num_matching == 0) {
254+
result.AppendErrorWithFormat(
255+
"Found no matching plugins for pattern '%s'", pattern.data());
256+
break;
257+
}
225258
}
226-
227-
if (!found_matching)
228-
result.AppendErrorWithFormat("Found no matching plugins");
229259
}
230260

231261
PluginListCommandOptions m_options;

lldb/test/Shell/Commands/command-plugin-list.test

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,20 @@ plugin list system-runtime
2727
# CHECK: system-runtime
2828
# CHECK: [+] systemruntime-macosx System runtime plugin for Mac OS X native libraries
2929

30+
# Test plugin list on multiple args works.
31+
plugin list system-runtime instrumentation-runtime.AddressSanitizer
32+
# CHECK-LABEL: plugin list system-runtime instrumentation-runtime.AddressSanitizer
33+
# CHECK: system-runtime
34+
# CHECK: [+] systemruntime-macosx System runtime plugin for Mac OS X native libraries
35+
# CHECK: instrumentation-runtime
36+
# CHECK: [+] AddressSanitizer AddressSanitizer instrumentation runtime plugin.
37+
3038
# Test json output for plugin list.
3139
plugin list --json
3240
# CHECK-LABEL plugin list --json
3341
# CHECK: {
34-
# CHECK-DAG: "system-runtime":
3542
# CHECK-DAG: "instrumentation-runtime":
43+
# CHECK-DAG: "system-runtime":
3644
# CHECK: }
3745

3846
# Test json output for plugin list with a namespace
@@ -47,17 +55,28 @@ plugin list system-runtime --json
4755
# CHECK: ]
4856
# CHECK: }
4957

58+
# Test json output for listing multiple plugins
59+
plugin list --json system-runtime instrumentation-runtime.AddressSanitizer
60+
# CHECK-LABEL plugin list --json system-runtime instrumentation-runtime.AddressSanitizer
61+
# CHECK: {
62+
# CHECK-DAG: "instrumentation-runtime":
63+
# CHECK-DAG: "name": "AddressSanitizer"
64+
# CHECK-DAG: "system-runtime":
65+
# CHECK: }
66+
5067

5168
# Test plugin list does not match a plugin name by substring.
5269
# RUN: %lldb -o "plugin list macosx" 2>&1 | FileCheck %s --check-prefix=ERROR_PLUGIN_NOT_FOUND
5370

5471
# Test plugin list does not match a plugin namespace by substring.
5572
# RUN: %lldb -o "plugin list system-runtime." 2>&1 | FileCheck %s --check-prefix=ERROR_PLUGIN_NOT_FOUND
5673

74+
# Test plugin list returns an error for unknown second argument
75+
# RUN: %lldb -o "plugin list system-runtime foo" 2>&1 | FileCheck %s --check-prefix=ERROR_PLUGIN_NOT_FOUND
76+
77+
# Test plugin list returns an error for unknown second argument
78+
# RUN: %lldb -o "plugin list --json system-runtime foo" 2>&1 | FileCheck %s --check-prefix=ERROR_PLUGIN_NOT_FOUND
79+
5780
# Test plugin list for unknown plugin returns an error.
5881
# RUN: %lldb -o "plugin list some-plugin-that-does-not-exist" 2>&1 | FileCheck %s --check-prefix=ERROR_PLUGIN_NOT_FOUND
5982
# ERROR_PLUGIN_NOT_FOUND: error: Found no matching plugins
60-
61-
# Test plugin list fails on multiple arguments.
62-
# RUN: %lldb -o "plugin list a b c" 2>&1 | FileCheck %s --check-prefix=ERROR_ARGUMENTS
63-
# ERROR_ARGUMENTS: error: 'plugin list' requires zero or one arguments

0 commit comments

Comments
 (0)