180814287SRaphael Isemann //===-- CommandCompletions.cpp --------------------------------------------===//
2c9af18a5SGreg Clayton //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6c9af18a5SGreg Clayton //
7c9af18a5SGreg Clayton //===----------------------------------------------------------------------===//
8c9af18a5SGreg Clayton 
9c8ecc2a9SEugene Zelenko #include "llvm/ADT/SmallString.h"
102cc5a18dSZachary Turner #include "llvm/ADT/StringSet.h"
11c8ecc2a9SEugene Zelenko 
124f3559dbSGongyu Deng #include "lldb/Breakpoint/Watchpoint.h"
139b62fd2fSGreg Clayton #include "lldb/Core/FileSpecList.h"
141f746071SGreg Clayton #include "lldb/Core/Module.h"
15190fadcdSZachary Turner #include "lldb/Core/PluginManager.h"
16188f1ac3SGongyu Deng #include "lldb/DataFormatters/DataVisualization.h"
17190fadcdSZachary Turner #include "lldb/Host/FileSystem.h"
189b62fd2fSGreg Clayton #include "lldb/Interpreter/CommandCompletions.h"
199b62fd2fSGreg Clayton #include "lldb/Interpreter/CommandInterpreter.h"
20633a29cfSZachary Turner #include "lldb/Interpreter/OptionValueProperties.h"
211f746071SGreg Clayton #include "lldb/Symbol/CompileUnit.h"
22f21feadcSGreg Clayton #include "lldb/Symbol/Variable.h"
233ce57e01SGongyu Deng #include "lldb/Target/Language.h"
24a952fe23SGongyu Deng #include "lldb/Target/Process.h"
25a14f4a75SGongyu Deng #include "lldb/Target/RegisterContext.h"
2666fa73faSGongyu Deng #include "lldb/Target/Thread.h"
275713a05bSZachary Turner #include "lldb/Utility/FileSpec.h"
282f3df613SZachary Turner #include "lldb/Utility/StreamString.h"
292cc5a18dSZachary Turner #include "lldb/Utility/TildeExpressionResolver.h"
30c9af18a5SGreg Clayton 
317d86ee5aSZachary Turner #include "llvm/Support/FileSystem.h"
322cc5a18dSZachary Turner #include "llvm/Support/Path.h"
33190fadcdSZachary Turner 
34c9af18a5SGreg Clayton using namespace lldb_private;
35c9af18a5SGreg Clayton 
36440460f1SRaphael Isemann // This is the command completion callback that is used to complete the
37440460f1SRaphael Isemann // argument of the option it is bound to (in the OptionDefinition table
38440460f1SRaphael Isemann // below).
39440460f1SRaphael Isemann typedef void (*CompletionCallback)(CommandInterpreter &interpreter,
40440460f1SRaphael Isemann                                    CompletionRequest &request,
41440460f1SRaphael Isemann                                    // A search filter to limit the search...
42440460f1SRaphael Isemann                                    lldb_private::SearchFilter *searcher);
43440460f1SRaphael Isemann 
44440460f1SRaphael Isemann struct CommonCompletionElement {
45440460f1SRaphael Isemann   uint32_t type;
46440460f1SRaphael Isemann   CompletionCallback callback;
47440460f1SRaphael Isemann };
48440460f1SRaphael Isemann 
49440460f1SRaphael Isemann bool CommandCompletions::InvokeCommonCompletionCallbacks(
50440460f1SRaphael Isemann     CommandInterpreter &interpreter, uint32_t completion_mask,
51440460f1SRaphael Isemann     CompletionRequest &request, SearchFilter *searcher) {
52440460f1SRaphael Isemann   bool handled = false;
53440460f1SRaphael Isemann 
54440460f1SRaphael Isemann   const CommonCompletionElement common_completions[] = {
55c9af18a5SGreg Clayton       {eSourceFileCompletion, CommandCompletions::SourceFiles},
56558ce124SJim Ingham       {eDiskFileCompletion, CommandCompletions::DiskFiles},
57558ce124SJim Ingham       {eDiskDirectoryCompletion, CommandCompletions::DiskDirectories},
58c9af18a5SGreg Clayton       {eSymbolCompletion, CommandCompletions::Symbols},
59c9af18a5SGreg Clayton       {eModuleCompletion, CommandCompletions::Modules},
60419f1be7SGongyu Deng       {eModuleUUIDCompletion, CommandCompletions::ModuleUUIDs},
613df9a8dfSCaroline Tice       {eSettingsNameCompletion, CommandCompletions::SettingsNames},
62ab65b34fSGreg Clayton       {ePlatformPluginCompletion, CommandCompletions::PlatformPluginNames},
63ab65b34fSGreg Clayton       {eArchitectureCompletion, CommandCompletions::ArchitectureNames},
64f21feadcSGreg Clayton       {eVariablePathCompletion, CommandCompletions::VariablePath},
65a14f4a75SGongyu Deng       {eRegisterCompletion, CommandCompletions::Registers},
660e50b9a4SGongyu Deng       {eBreakpointCompletion, CommandCompletions::Breakpoints},
67763bc230SGongyu Deng       {eProcessPluginCompletion, CommandCompletions::ProcessPluginNames},
688a5e2969SGongyu Deng       {eDisassemblyFlavorCompletion, CommandCompletions::DisassemblyFlavors},
693ce57e01SGongyu Deng       {eTypeLanguageCompletion, CommandCompletions::TypeLanguages},
7066fa73faSGongyu Deng       {eFrameIndexCompletion, CommandCompletions::FrameIndexes},
71b2b7dbb4SGongyu Deng       {eStopHookIDCompletion, CommandCompletions::StopHookIDs},
72a952fe23SGongyu Deng       {eThreadIndexCompletion, CommandCompletions::ThreadIndexes},
734f3559dbSGongyu Deng       {eWatchPointIDCompletion, CommandCompletions::WatchPointIDs},
7422e63cbaSGongyu Deng       {eBreakpointNameCompletion, CommandCompletions::BreakpointNames},
7519311f5cSGongyu Deng       {eProcessIDCompletion, CommandCompletions::ProcessIDs},
7619311f5cSGongyu Deng       {eProcessNameCompletion, CommandCompletions::ProcessNames},
773cd8d7b1SGongyu Deng       {eRemoteDiskFileCompletion, CommandCompletions::RemoteDiskFiles},
78188f1ac3SGongyu Deng       {eRemoteDiskDirectoryCompletion,
79188f1ac3SGongyu Deng        CommandCompletions::RemoteDiskDirectories},
80188f1ac3SGongyu Deng       {eTypeCategoryNameCompletion, CommandCompletions::TypeCategoryNames},
81c8ecc2a9SEugene Zelenko       {eNoCompletion, nullptr} // This one has to be last in the list.
82c9af18a5SGreg Clayton   };
83c9af18a5SGreg Clayton 
84b9c1b51eSKate Stone   for (int i = 0;; i++) {
85440460f1SRaphael Isemann     if (common_completions[i].type == eNoCompletion)
86c9af18a5SGreg Clayton       break;
87440460f1SRaphael Isemann     else if ((common_completions[i].type & completion_mask) ==
88440460f1SRaphael Isemann                  common_completions[i].type &&
89440460f1SRaphael Isemann              common_completions[i].callback != nullptr) {
90c9af18a5SGreg Clayton       handled = true;
91440460f1SRaphael Isemann       common_completions[i].callback(interpreter, request, searcher);
92c9af18a5SGreg Clayton     }
93c9af18a5SGreg Clayton   }
94c9af18a5SGreg Clayton   return handled;
95c9af18a5SGreg Clayton }
96c9af18a5SGreg Clayton 
97572fc897SRaphael Isemann namespace {
98572fc897SRaphael Isemann // The Completer class is a convenient base class for building searchers that
99572fc897SRaphael Isemann // go along with the SearchFilter passed to the standard Completer functions.
100572fc897SRaphael Isemann class Completer : public Searcher {
101572fc897SRaphael Isemann public:
102572fc897SRaphael Isemann   Completer(CommandInterpreter &interpreter, CompletionRequest &request)
103572fc897SRaphael Isemann       : m_interpreter(interpreter), m_request(request) {}
104572fc897SRaphael Isemann 
105572fc897SRaphael Isemann   ~Completer() override = default;
106572fc897SRaphael Isemann 
107572fc897SRaphael Isemann   CallbackReturn SearchCallback(SearchFilter &filter, SymbolContext &context,
108572fc897SRaphael Isemann                                 Address *addr) override = 0;
109572fc897SRaphael Isemann 
110572fc897SRaphael Isemann   lldb::SearchDepth GetDepth() override = 0;
111572fc897SRaphael Isemann 
112572fc897SRaphael Isemann   virtual void DoCompletion(SearchFilter *filter) = 0;
113572fc897SRaphael Isemann 
114572fc897SRaphael Isemann protected:
115572fc897SRaphael Isemann   CommandInterpreter &m_interpreter;
116572fc897SRaphael Isemann   CompletionRequest &m_request;
117572fc897SRaphael Isemann 
118572fc897SRaphael Isemann private:
119eaebcbc6SKonrad Kleine   Completer(const Completer &) = delete;
120eaebcbc6SKonrad Kleine   const Completer &operator=(const Completer &) = delete;
121572fc897SRaphael Isemann };
122572fc897SRaphael Isemann } // namespace
123572fc897SRaphael Isemann 
124572fc897SRaphael Isemann // SourceFileCompleter implements the source file completer
125572fc897SRaphael Isemann namespace {
126572fc897SRaphael Isemann class SourceFileCompleter : public Completer {
127572fc897SRaphael Isemann public:
128572fc897SRaphael Isemann   SourceFileCompleter(CommandInterpreter &interpreter,
129572fc897SRaphael Isemann                       CompletionRequest &request)
130572fc897SRaphael Isemann       : Completer(interpreter, request), m_matching_files() {
131572fc897SRaphael Isemann     FileSpec partial_spec(m_request.GetCursorArgumentPrefix());
132572fc897SRaphael Isemann     m_file_name = partial_spec.GetFilename().GetCString();
133572fc897SRaphael Isemann     m_dir_name = partial_spec.GetDirectory().GetCString();
134572fc897SRaphael Isemann   }
135572fc897SRaphael Isemann 
136572fc897SRaphael Isemann   lldb::SearchDepth GetDepth() override { return lldb::eSearchDepthCompUnit; }
137572fc897SRaphael Isemann 
138572fc897SRaphael Isemann   Searcher::CallbackReturn SearchCallback(SearchFilter &filter,
139572fc897SRaphael Isemann                                           SymbolContext &context,
140572fc897SRaphael Isemann                                           Address *addr) override {
141572fc897SRaphael Isemann     if (context.comp_unit != nullptr) {
142572fc897SRaphael Isemann       const char *cur_file_name =
143572fc897SRaphael Isemann           context.comp_unit->GetPrimaryFile().GetFilename().GetCString();
144572fc897SRaphael Isemann       const char *cur_dir_name =
145572fc897SRaphael Isemann           context.comp_unit->GetPrimaryFile().GetDirectory().GetCString();
146572fc897SRaphael Isemann 
147572fc897SRaphael Isemann       bool match = false;
148572fc897SRaphael Isemann       if (m_file_name && cur_file_name &&
149572fc897SRaphael Isemann           strstr(cur_file_name, m_file_name) == cur_file_name)
150572fc897SRaphael Isemann         match = true;
151572fc897SRaphael Isemann 
152572fc897SRaphael Isemann       if (match && m_dir_name && cur_dir_name &&
153572fc897SRaphael Isemann           strstr(cur_dir_name, m_dir_name) != cur_dir_name)
154572fc897SRaphael Isemann         match = false;
155572fc897SRaphael Isemann 
156572fc897SRaphael Isemann       if (match) {
157572fc897SRaphael Isemann         m_matching_files.AppendIfUnique(context.comp_unit->GetPrimaryFile());
158572fc897SRaphael Isemann       }
159572fc897SRaphael Isemann     }
160572fc897SRaphael Isemann     return Searcher::eCallbackReturnContinue;
161572fc897SRaphael Isemann   }
162572fc897SRaphael Isemann 
163572fc897SRaphael Isemann   void DoCompletion(SearchFilter *filter) override {
164572fc897SRaphael Isemann     filter->Search(*this);
165572fc897SRaphael Isemann     // Now convert the filelist to completions:
166572fc897SRaphael Isemann     for (size_t i = 0; i < m_matching_files.GetSize(); i++) {
167572fc897SRaphael Isemann       m_request.AddCompletion(
168572fc897SRaphael Isemann           m_matching_files.GetFileSpecAtIndex(i).GetFilename().GetCString());
169572fc897SRaphael Isemann     }
170572fc897SRaphael Isemann   }
171572fc897SRaphael Isemann 
172572fc897SRaphael Isemann private:
173572fc897SRaphael Isemann   FileSpecList m_matching_files;
174572fc897SRaphael Isemann   const char *m_file_name;
175572fc897SRaphael Isemann   const char *m_dir_name;
176572fc897SRaphael Isemann 
177eaebcbc6SKonrad Kleine   SourceFileCompleter(const SourceFileCompleter &) = delete;
178eaebcbc6SKonrad Kleine   const SourceFileCompleter &operator=(const SourceFileCompleter &) = delete;
179572fc897SRaphael Isemann };
180572fc897SRaphael Isemann } // namespace
181572fc897SRaphael Isemann 
182572fc897SRaphael Isemann static bool regex_chars(const char comp) {
183572fc897SRaphael Isemann   return llvm::StringRef("[](){}+.*|^$\\?").contains(comp);
184572fc897SRaphael Isemann }
185572fc897SRaphael Isemann 
186572fc897SRaphael Isemann namespace {
187572fc897SRaphael Isemann class SymbolCompleter : public Completer {
188572fc897SRaphael Isemann 
189572fc897SRaphael Isemann public:
190572fc897SRaphael Isemann   SymbolCompleter(CommandInterpreter &interpreter, CompletionRequest &request)
191572fc897SRaphael Isemann       : Completer(interpreter, request) {
192572fc897SRaphael Isemann     std::string regex_str;
193572fc897SRaphael Isemann     if (!m_request.GetCursorArgumentPrefix().empty()) {
194572fc897SRaphael Isemann       regex_str.append("^");
195572fc897SRaphael Isemann       regex_str.append(std::string(m_request.GetCursorArgumentPrefix()));
196572fc897SRaphael Isemann     } else {
197572fc897SRaphael Isemann       // Match anything since the completion string is empty
198572fc897SRaphael Isemann       regex_str.append(".");
199572fc897SRaphael Isemann     }
200572fc897SRaphael Isemann     std::string::iterator pos =
201572fc897SRaphael Isemann         find_if(regex_str.begin() + 1, regex_str.end(), regex_chars);
202572fc897SRaphael Isemann     while (pos < regex_str.end()) {
203572fc897SRaphael Isemann       pos = regex_str.insert(pos, '\\');
204572fc897SRaphael Isemann       pos = find_if(pos + 2, regex_str.end(), regex_chars);
205572fc897SRaphael Isemann     }
206572fc897SRaphael Isemann     m_regex = RegularExpression(regex_str);
207572fc897SRaphael Isemann   }
208572fc897SRaphael Isemann 
209572fc897SRaphael Isemann   lldb::SearchDepth GetDepth() override { return lldb::eSearchDepthModule; }
210572fc897SRaphael Isemann 
211572fc897SRaphael Isemann   Searcher::CallbackReturn SearchCallback(SearchFilter &filter,
212572fc897SRaphael Isemann                                           SymbolContext &context,
213572fc897SRaphael Isemann                                           Address *addr) override {
214572fc897SRaphael Isemann     if (context.module_sp) {
215572fc897SRaphael Isemann       SymbolContextList sc_list;
216572fc897SRaphael Isemann       const bool include_symbols = true;
217572fc897SRaphael Isemann       const bool include_inlines = true;
218572fc897SRaphael Isemann       context.module_sp->FindFunctions(m_regex, include_symbols,
219572fc897SRaphael Isemann                                        include_inlines, sc_list);
220572fc897SRaphael Isemann 
221572fc897SRaphael Isemann       SymbolContext sc;
222572fc897SRaphael Isemann       // Now add the functions & symbols to the list - only add if unique:
223572fc897SRaphael Isemann       for (uint32_t i = 0; i < sc_list.GetSize(); i++) {
224572fc897SRaphael Isemann         if (sc_list.GetContextAtIndex(i, sc)) {
225572fc897SRaphael Isemann           ConstString func_name = sc.GetFunctionName(Mangled::ePreferDemangled);
226572fc897SRaphael Isemann           // Ensure that the function name matches the regex. This is more than
227572fc897SRaphael Isemann           // a sanity check. It is possible that the demangled function name
228572fc897SRaphael Isemann           // does not start with the prefix, for example when it's in an
229572fc897SRaphael Isemann           // anonymous namespace.
230572fc897SRaphael Isemann           if (!func_name.IsEmpty() && m_regex.Execute(func_name.GetStringRef()))
231572fc897SRaphael Isemann             m_match_set.insert(func_name);
232572fc897SRaphael Isemann         }
233572fc897SRaphael Isemann       }
234572fc897SRaphael Isemann     }
235572fc897SRaphael Isemann     return Searcher::eCallbackReturnContinue;
236572fc897SRaphael Isemann   }
237572fc897SRaphael Isemann 
238572fc897SRaphael Isemann   void DoCompletion(SearchFilter *filter) override {
239572fc897SRaphael Isemann     filter->Search(*this);
240572fc897SRaphael Isemann     collection::iterator pos = m_match_set.begin(), end = m_match_set.end();
241572fc897SRaphael Isemann     for (pos = m_match_set.begin(); pos != end; pos++)
242572fc897SRaphael Isemann       m_request.AddCompletion((*pos).GetCString());
243572fc897SRaphael Isemann   }
244572fc897SRaphael Isemann 
245572fc897SRaphael Isemann private:
246572fc897SRaphael Isemann   RegularExpression m_regex;
247572fc897SRaphael Isemann   typedef std::set<ConstString> collection;
248572fc897SRaphael Isemann   collection m_match_set;
249572fc897SRaphael Isemann 
250eaebcbc6SKonrad Kleine   SymbolCompleter(const SymbolCompleter &) = delete;
251eaebcbc6SKonrad Kleine   const SymbolCompleter &operator=(const SymbolCompleter &) = delete;
252572fc897SRaphael Isemann };
253572fc897SRaphael Isemann } // namespace
254572fc897SRaphael Isemann 
255572fc897SRaphael Isemann namespace {
256572fc897SRaphael Isemann class ModuleCompleter : public Completer {
257572fc897SRaphael Isemann public:
258572fc897SRaphael Isemann   ModuleCompleter(CommandInterpreter &interpreter, CompletionRequest &request)
259572fc897SRaphael Isemann       : Completer(interpreter, request) {
260572fc897SRaphael Isemann     FileSpec partial_spec(m_request.GetCursorArgumentPrefix());
261572fc897SRaphael Isemann     m_file_name = partial_spec.GetFilename().GetCString();
262572fc897SRaphael Isemann     m_dir_name = partial_spec.GetDirectory().GetCString();
263572fc897SRaphael Isemann   }
264572fc897SRaphael Isemann 
265572fc897SRaphael Isemann   lldb::SearchDepth GetDepth() override { return lldb::eSearchDepthModule; }
266572fc897SRaphael Isemann 
267572fc897SRaphael Isemann   Searcher::CallbackReturn SearchCallback(SearchFilter &filter,
268572fc897SRaphael Isemann                                           SymbolContext &context,
269572fc897SRaphael Isemann                                           Address *addr) override {
270572fc897SRaphael Isemann     if (context.module_sp) {
271572fc897SRaphael Isemann       const char *cur_file_name =
272572fc897SRaphael Isemann           context.module_sp->GetFileSpec().GetFilename().GetCString();
273572fc897SRaphael Isemann       const char *cur_dir_name =
274572fc897SRaphael Isemann           context.module_sp->GetFileSpec().GetDirectory().GetCString();
275572fc897SRaphael Isemann 
276572fc897SRaphael Isemann       bool match = false;
277572fc897SRaphael Isemann       if (m_file_name && cur_file_name &&
278572fc897SRaphael Isemann           strstr(cur_file_name, m_file_name) == cur_file_name)
279572fc897SRaphael Isemann         match = true;
280572fc897SRaphael Isemann 
281572fc897SRaphael Isemann       if (match && m_dir_name && cur_dir_name &&
282572fc897SRaphael Isemann           strstr(cur_dir_name, m_dir_name) != cur_dir_name)
283572fc897SRaphael Isemann         match = false;
284572fc897SRaphael Isemann 
285572fc897SRaphael Isemann       if (match) {
286572fc897SRaphael Isemann         m_request.AddCompletion(cur_file_name);
287572fc897SRaphael Isemann       }
288572fc897SRaphael Isemann     }
289572fc897SRaphael Isemann     return Searcher::eCallbackReturnContinue;
290572fc897SRaphael Isemann   }
291572fc897SRaphael Isemann 
292572fc897SRaphael Isemann   void DoCompletion(SearchFilter *filter) override { filter->Search(*this); }
293572fc897SRaphael Isemann 
294572fc897SRaphael Isemann private:
295572fc897SRaphael Isemann   const char *m_file_name;
296572fc897SRaphael Isemann   const char *m_dir_name;
297572fc897SRaphael Isemann 
298eaebcbc6SKonrad Kleine   ModuleCompleter(const ModuleCompleter &) = delete;
299eaebcbc6SKonrad Kleine   const ModuleCompleter &operator=(const ModuleCompleter &) = delete;
300572fc897SRaphael Isemann };
301572fc897SRaphael Isemann } // namespace
302572fc897SRaphael Isemann 
303ae34ed2cSRaphael Isemann void CommandCompletions::SourceFiles(CommandInterpreter &interpreter,
304a2e76c0bSRaphael Isemann                                      CompletionRequest &request,
305a2e76c0bSRaphael Isemann                                      SearchFilter *searcher) {
30662174682SRaphael Isemann   SourceFileCompleter completer(interpreter, request);
307c9af18a5SGreg Clayton 
308b9c1b51eSKate Stone   if (searcher == nullptr) {
3092976d00aSJim Ingham     lldb::TargetSP target_sp = interpreter.GetDebugger().GetSelectedTarget();
31033df7cd3SJim Ingham     SearchFilterForUnconstrainedSearches null_searcher(target_sp);
311c9af18a5SGreg Clayton     completer.DoCompletion(&null_searcher);
312b9c1b51eSKate Stone   } else {
313c9af18a5SGreg Clayton     completer.DoCompletion(searcher);
314c9af18a5SGreg Clayton   }
315c9af18a5SGreg Clayton }
316c9af18a5SGreg Clayton 
317ae34ed2cSRaphael Isemann static void DiskFilesOrDirectories(const llvm::Twine &partial_name,
3185edee822SRaphael Isemann                                    bool only_directories,
3195edee822SRaphael Isemann                                    CompletionRequest &request,
3202cc5a18dSZachary Turner                                    TildeExpressionResolver &Resolver) {
3212cc5a18dSZachary Turner   llvm::SmallString<256> CompletionBuffer;
3222cc5a18dSZachary Turner   llvm::SmallString<256> Storage;
3232cc5a18dSZachary Turner   partial_name.toVector(CompletionBuffer);
324b2f1fb29SVirgile Bello 
3252cc5a18dSZachary Turner   if (CompletionBuffer.size() >= PATH_MAX)
326ae34ed2cSRaphael Isemann     return;
327b2f1fb29SVirgile Bello 
3282cc5a18dSZachary Turner   namespace path = llvm::sys::path;
329b2f1fb29SVirgile Bello 
3302cc5a18dSZachary Turner   llvm::StringRef SearchDir;
3312cc5a18dSZachary Turner   llvm::StringRef PartialItem;
3322cc5a18dSZachary Turner 
3332cc5a18dSZachary Turner   if (CompletionBuffer.startswith("~")) {
3341def2579SDavid Blaikie     llvm::StringRef Buffer = CompletionBuffer;
3355c5091fcSZachary Turner     size_t FirstSep =
3365c5091fcSZachary Turner         Buffer.find_if([](char c) { return path::is_separator(c); });
3372cc5a18dSZachary Turner 
3382cc5a18dSZachary Turner     llvm::StringRef Username = Buffer.take_front(FirstSep);
3392cc5a18dSZachary Turner     llvm::StringRef Remainder;
3402cc5a18dSZachary Turner     if (FirstSep != llvm::StringRef::npos)
3412cc5a18dSZachary Turner       Remainder = Buffer.drop_front(FirstSep + 1);
3422cc5a18dSZachary Turner 
343b3f44ad9SStella Stamenova     llvm::SmallString<256> Resolved;
3442cc5a18dSZachary Turner     if (!Resolver.ResolveExact(Username, Resolved)) {
3452cc5a18dSZachary Turner       // We couldn't resolve it as a full username.  If there were no slashes
3462cc5a18dSZachary Turner       // then this might be a partial username.   We try to resolve it as such
3472cc5a18dSZachary Turner       // but after that, we're done regardless of any matches.
3482cc5a18dSZachary Turner       if (FirstSep == llvm::StringRef::npos) {
3492cc5a18dSZachary Turner         llvm::StringSet<> MatchSet;
350a2e76c0bSRaphael Isemann         Resolver.ResolvePartial(Username, MatchSet);
3512cc5a18dSZachary Turner         for (const auto &S : MatchSet) {
3522cc5a18dSZachary Turner           Resolved = S.getKey();
3532cc5a18dSZachary Turner           path::append(Resolved, path::get_separator());
3545edee822SRaphael Isemann           request.AddCompletion(Resolved, "", CompletionMode::Partial);
355b2f1fb29SVirgile Bello         }
356b2f1fb29SVirgile Bello       }
357ae34ed2cSRaphael Isemann       return;
3582cc5a18dSZachary Turner     }
3590734e6a5SZachary Turner 
36005097246SAdrian Prantl     // If there was no trailing slash, then we're done as soon as we resolve
36105097246SAdrian Prantl     // the expression to the correct directory.  Otherwise we need to continue
3622cc5a18dSZachary Turner     // looking for matches within that directory.
3632cc5a18dSZachary Turner     if (FirstSep == llvm::StringRef::npos) {
3642cc5a18dSZachary Turner       // Make sure it ends with a separator.
3652cc5a18dSZachary Turner       path::append(CompletionBuffer, path::get_separator());
3665edee822SRaphael Isemann       request.AddCompletion(CompletionBuffer, "", CompletionMode::Partial);
367ae34ed2cSRaphael Isemann       return;
3682cc5a18dSZachary Turner     }
3692cc5a18dSZachary Turner 
3702cc5a18dSZachary Turner     // We want to keep the form the user typed, so we special case this to
3712cc5a18dSZachary Turner     // search in the fully resolved directory, but CompletionBuffer keeps the
3722cc5a18dSZachary Turner     // unmodified form that the user typed.
3732cc5a18dSZachary Turner     Storage = Resolved;
37488ec2e4eSPavel Labath     llvm::StringRef RemainderDir = path::parent_path(Remainder);
3754621e0b0SRaphael Isemann     if (!RemainderDir.empty()) {
3764621e0b0SRaphael Isemann       // Append the remaining path to the resolved directory.
3774621e0b0SRaphael Isemann       Storage.append(path::get_separator());
3784621e0b0SRaphael Isemann       Storage.append(RemainderDir);
3794621e0b0SRaphael Isemann     }
38017843885SRaphael Isemann     SearchDir = Storage;
3810734e6a5SZachary Turner   } else {
3822cc5a18dSZachary Turner     SearchDir = path::parent_path(CompletionBuffer);
3832cc5a18dSZachary Turner   }
3842cc5a18dSZachary Turner 
3852cc5a18dSZachary Turner   size_t FullPrefixLen = CompletionBuffer.size();
3862cc5a18dSZachary Turner 
3872cc5a18dSZachary Turner   PartialItem = path::filename(CompletionBuffer);
38878a10a7aSFrederic Riss 
38978a10a7aSFrederic Riss   // path::filename() will return "." when the passed path ends with a
39078a10a7aSFrederic Riss   // directory separator. We have to filter those out, but only when the
39178a10a7aSFrederic Riss   // "." doesn't come from the completion request itself.
39278a10a7aSFrederic Riss   if (PartialItem == "." && path::is_separator(CompletionBuffer.back()))
3932cc5a18dSZachary Turner     PartialItem = llvm::StringRef();
3942cc5a18dSZachary Turner 
395426d1379SZachary Turner   if (SearchDir.empty()) {
396426d1379SZachary Turner     llvm::sys::fs::current_path(Storage);
397426d1379SZachary Turner     SearchDir = Storage;
398426d1379SZachary Turner   }
3992cc5a18dSZachary Turner   assert(!PartialItem.contains(path::get_separator()));
4002cc5a18dSZachary Turner 
4012cc5a18dSZachary Turner   // SearchDir now contains the directory to search in, and Prefix contains the
4022cc5a18dSZachary Turner   // text we want to match against items in that directory.
4032cc5a18dSZachary Turner 
404edaf2bccSJonas Devlieghere   FileSystem &fs = FileSystem::Instance();
4052cc5a18dSZachary Turner   std::error_code EC;
406edaf2bccSJonas Devlieghere   llvm::vfs::directory_iterator Iter = fs.DirBegin(SearchDir, EC);
407edaf2bccSJonas Devlieghere   llvm::vfs::directory_iterator End;
4082cc5a18dSZachary Turner   for (; Iter != End && !EC; Iter.increment(EC)) {
4092cc5a18dSZachary Turner     auto &Entry = *Iter;
410edaf2bccSJonas Devlieghere     llvm::ErrorOr<llvm::vfs::Status> Status = fs.GetStatus(Entry.path());
411edaf2bccSJonas Devlieghere 
412edaf2bccSJonas Devlieghere     if (!Status)
413edaf2bccSJonas Devlieghere       continue;
4142cc5a18dSZachary Turner 
4152cc5a18dSZachary Turner     auto Name = path::filename(Entry.path());
4162cc5a18dSZachary Turner 
4172cc5a18dSZachary Turner     // Omit ".", ".."
4182cc5a18dSZachary Turner     if (Name == "." || Name == ".." || !Name.startswith(PartialItem))
4192cc5a18dSZachary Turner       continue;
4202cc5a18dSZachary Turner 
421edaf2bccSJonas Devlieghere     bool is_dir = Status->isDirectory();
4222cc5a18dSZachary Turner 
4232cc5a18dSZachary Turner     // If it's a symlink, then we treat it as a directory as long as the target
4242cc5a18dSZachary Turner     // is a directory.
425edaf2bccSJonas Devlieghere     if (Status->isSymlink()) {
426edaf2bccSJonas Devlieghere       FileSpec symlink_filespec(Entry.path());
427edaf2bccSJonas Devlieghere       FileSpec resolved_filespec;
428edaf2bccSJonas Devlieghere       auto error = fs.ResolveSymbolicLink(symlink_filespec, resolved_filespec);
429edaf2bccSJonas Devlieghere       if (error.Success())
430edaf2bccSJonas Devlieghere         is_dir = fs.IsDirectory(symlink_filespec);
4312cc5a18dSZachary Turner     }
432edaf2bccSJonas Devlieghere 
4332cc5a18dSZachary Turner     if (only_directories && !is_dir)
4342cc5a18dSZachary Turner       continue;
4352cc5a18dSZachary Turner 
4362cc5a18dSZachary Turner     // Shrink it back down so that it just has the original prefix the user
4372cc5a18dSZachary Turner     // typed and remove the part of the name which is common to the located
4382cc5a18dSZachary Turner     // item and what the user typed.
4392cc5a18dSZachary Turner     CompletionBuffer.resize(FullPrefixLen);
4402cc5a18dSZachary Turner     Name = Name.drop_front(PartialItem.size());
4412cc5a18dSZachary Turner     CompletionBuffer.append(Name);
4422cc5a18dSZachary Turner 
4432cc5a18dSZachary Turner     if (is_dir) {
4442cc5a18dSZachary Turner       path::append(CompletionBuffer, path::get_separator());
4450734e6a5SZachary Turner     }
4460734e6a5SZachary Turner 
4475edee822SRaphael Isemann     CompletionMode mode =
4485edee822SRaphael Isemann         is_dir ? CompletionMode::Partial : CompletionMode::Normal;
4495edee822SRaphael Isemann     request.AddCompletion(CompletionBuffer, "", mode);
450d5bd3a1eSZachary Turner   }
451558ce124SJim Ingham }
452558ce124SJim Ingham 
4535edee822SRaphael Isemann static void DiskFilesOrDirectories(const llvm::Twine &partial_name,
4545edee822SRaphael Isemann                                    bool only_directories, StringList &matches,
4555edee822SRaphael Isemann                                    TildeExpressionResolver &Resolver) {
4565edee822SRaphael Isemann   CompletionResult result;
4575edee822SRaphael Isemann   std::string partial_name_str = partial_name.str();
4585edee822SRaphael Isemann   CompletionRequest request(partial_name_str, partial_name_str.size(), result);
4595edee822SRaphael Isemann   DiskFilesOrDirectories(partial_name, only_directories, request, Resolver);
4605edee822SRaphael Isemann   result.GetMatches(matches);
4615edee822SRaphael Isemann }
4625edee822SRaphael Isemann 
463ae34ed2cSRaphael Isemann static void DiskFilesOrDirectories(CompletionRequest &request,
4641a6d7ab5SRaphael Isemann                                    bool only_directories) {
46572787ac6SJonas Devlieghere   StandardTildeExpressionResolver resolver;
4661a6d7ab5SRaphael Isemann   DiskFilesOrDirectories(request.GetCursorArgumentPrefix(), only_directories,
4675edee822SRaphael Isemann                          request, resolver);
4681a6d7ab5SRaphael Isemann }
4691a6d7ab5SRaphael Isemann 
470ae34ed2cSRaphael Isemann void CommandCompletions::DiskFiles(CommandInterpreter &interpreter,
471a2e76c0bSRaphael Isemann                                    CompletionRequest &request,
472a2e76c0bSRaphael Isemann                                    SearchFilter *searcher) {
473ae34ed2cSRaphael Isemann   DiskFilesOrDirectories(request, /*only_dirs*/ false);
4742cc5a18dSZachary Turner }
4752cc5a18dSZachary Turner 
476ae34ed2cSRaphael Isemann void CommandCompletions::DiskFiles(const llvm::Twine &partial_file_name,
4772cc5a18dSZachary Turner                                    StringList &matches,
4782cc5a18dSZachary Turner                                    TildeExpressionResolver &Resolver) {
479ae34ed2cSRaphael Isemann   DiskFilesOrDirectories(partial_file_name, false, matches, Resolver);
480558ce124SJim Ingham }
481558ce124SJim Ingham 
482ae34ed2cSRaphael Isemann void CommandCompletions::DiskDirectories(CommandInterpreter &interpreter,
483a2e76c0bSRaphael Isemann                                          CompletionRequest &request,
484a2e76c0bSRaphael Isemann                                          SearchFilter *searcher) {
485ae34ed2cSRaphael Isemann   DiskFilesOrDirectories(request, /*only_dirs*/ true);
4862cc5a18dSZachary Turner }
4872cc5a18dSZachary Turner 
488ae34ed2cSRaphael Isemann void CommandCompletions::DiskDirectories(const llvm::Twine &partial_file_name,
4892cc5a18dSZachary Turner                                          StringList &matches,
4902cc5a18dSZachary Turner                                          TildeExpressionResolver &Resolver) {
491ae34ed2cSRaphael Isemann   DiskFilesOrDirectories(partial_file_name, true, matches, Resolver);
492558ce124SJim Ingham }
493558ce124SJim Ingham 
4943cd8d7b1SGongyu Deng void CommandCompletions::RemoteDiskFiles(CommandInterpreter &interpreter,
4953cd8d7b1SGongyu Deng                                          CompletionRequest &request,
4963cd8d7b1SGongyu Deng                                          SearchFilter *searcher) {
4973cd8d7b1SGongyu Deng   lldb::PlatformSP platform_sp =
4983cd8d7b1SGongyu Deng       interpreter.GetDebugger().GetPlatformList().GetSelectedPlatform();
4993cd8d7b1SGongyu Deng   if (platform_sp)
5003cd8d7b1SGongyu Deng     platform_sp->AutoCompleteDiskFileOrDirectory(request, false);
5013cd8d7b1SGongyu Deng }
5023cd8d7b1SGongyu Deng 
5033cd8d7b1SGongyu Deng void CommandCompletions::RemoteDiskDirectories(CommandInterpreter &interpreter,
5043cd8d7b1SGongyu Deng                                                CompletionRequest &request,
5053cd8d7b1SGongyu Deng                                                SearchFilter *searcher) {
5063cd8d7b1SGongyu Deng   lldb::PlatformSP platform_sp =
5073cd8d7b1SGongyu Deng       interpreter.GetDebugger().GetPlatformList().GetSelectedPlatform();
5083cd8d7b1SGongyu Deng   if (platform_sp)
5093cd8d7b1SGongyu Deng     platform_sp->AutoCompleteDiskFileOrDirectory(request, true);
5103cd8d7b1SGongyu Deng }
5113cd8d7b1SGongyu Deng 
512ae34ed2cSRaphael Isemann void CommandCompletions::Modules(CommandInterpreter &interpreter,
513a2e76c0bSRaphael Isemann                                  CompletionRequest &request,
514a2e76c0bSRaphael Isemann                                  SearchFilter *searcher) {
515a2e76c0bSRaphael Isemann   ModuleCompleter completer(interpreter, request);
516c9af18a5SGreg Clayton 
517b9c1b51eSKate Stone   if (searcher == nullptr) {
5182976d00aSJim Ingham     lldb::TargetSP target_sp = interpreter.GetDebugger().GetSelectedTarget();
51933df7cd3SJim Ingham     SearchFilterForUnconstrainedSearches null_searcher(target_sp);
520c9af18a5SGreg Clayton     completer.DoCompletion(&null_searcher);
521b9c1b51eSKate Stone   } else {
522c9af18a5SGreg Clayton     completer.DoCompletion(searcher);
523c9af18a5SGreg Clayton   }
524c9af18a5SGreg Clayton }
525c9af18a5SGreg Clayton 
526419f1be7SGongyu Deng void CommandCompletions::ModuleUUIDs(CommandInterpreter &interpreter,
527419f1be7SGongyu Deng                                      CompletionRequest &request,
528419f1be7SGongyu Deng                                      SearchFilter *searcher) {
529419f1be7SGongyu Deng   const ExecutionContext &exe_ctx = interpreter.GetExecutionContext();
530419f1be7SGongyu Deng   if (!exe_ctx.HasTargetScope())
531419f1be7SGongyu Deng     return;
532419f1be7SGongyu Deng 
533419f1be7SGongyu Deng   exe_ctx.GetTargetPtr()->GetImages().ForEach(
534419f1be7SGongyu Deng       [&request](const lldb::ModuleSP &module) {
535419f1be7SGongyu Deng         StreamString strm;
536419f1be7SGongyu Deng         module->GetDescription(strm.AsRawOstream(),
537419f1be7SGongyu Deng                                lldb::eDescriptionLevelInitial);
538419f1be7SGongyu Deng         request.TryCompleteCurrentArg(module->GetUUID().GetAsString(),
539419f1be7SGongyu Deng                                       strm.GetString());
540419f1be7SGongyu Deng         return true;
541419f1be7SGongyu Deng       });
542419f1be7SGongyu Deng }
543419f1be7SGongyu Deng 
544ae34ed2cSRaphael Isemann void CommandCompletions::Symbols(CommandInterpreter &interpreter,
545a2e76c0bSRaphael Isemann                                  CompletionRequest &request,
546a2e76c0bSRaphael Isemann                                  SearchFilter *searcher) {
547a2e76c0bSRaphael Isemann   SymbolCompleter completer(interpreter, request);
548c9af18a5SGreg Clayton 
549b9c1b51eSKate Stone   if (searcher == nullptr) {
5502976d00aSJim Ingham     lldb::TargetSP target_sp = interpreter.GetDebugger().GetSelectedTarget();
55133df7cd3SJim Ingham     SearchFilterForUnconstrainedSearches null_searcher(target_sp);
552c9af18a5SGreg Clayton     completer.DoCompletion(&null_searcher);
553b9c1b51eSKate Stone   } else {
554c9af18a5SGreg Clayton     completer.DoCompletion(searcher);
555c9af18a5SGreg Clayton   }
556c9af18a5SGreg Clayton }
557c9af18a5SGreg Clayton 
558ae34ed2cSRaphael Isemann void CommandCompletions::SettingsNames(CommandInterpreter &interpreter,
559a2e76c0bSRaphael Isemann                                        CompletionRequest &request,
560a2e76c0bSRaphael Isemann                                        SearchFilter *searcher) {
56167cc0636SGreg Clayton   // Cache the full setting name list
56267cc0636SGreg Clayton   static StringList g_property_names;
563b9c1b51eSKate Stone   if (g_property_names.GetSize() == 0) {
56467cc0636SGreg Clayton     // Generate the full setting name list on demand
565b9c1b51eSKate Stone     lldb::OptionValuePropertiesSP properties_sp(
566b9c1b51eSKate Stone         interpreter.GetDebugger().GetValueProperties());
567b9c1b51eSKate Stone     if (properties_sp) {
56867cc0636SGreg Clayton       StreamString strm;
569c8ecc2a9SEugene Zelenko       properties_sp->DumpValue(nullptr, strm, OptionValue::eDumpOptionName);
570adcd0268SBenjamin Kramer       const std::string &str = std::string(strm.GetString());
57167cc0636SGreg Clayton       g_property_names.SplitIntoLines(str.c_str(), str.size());
57267cc0636SGreg Clayton     }
57367cc0636SGreg Clayton   }
5743df9a8dfSCaroline Tice 
57593ca36d7SRaphael Isemann   for (const std::string &s : g_property_names)
57693ca36d7SRaphael Isemann     request.TryCompleteCurrentArg(s);
577b8639f5cSRaphael Isemann }
578b8639f5cSRaphael Isemann 
579ae34ed2cSRaphael Isemann void CommandCompletions::PlatformPluginNames(CommandInterpreter &interpreter,
580a2e76c0bSRaphael Isemann                                              CompletionRequest &request,
581a2e76c0bSRaphael Isemann                                              SearchFilter *searcher) {
582ae34ed2cSRaphael Isemann   PluginManager::AutoCompletePlatformName(request.GetCursorArgumentPrefix(),
583ae34ed2cSRaphael Isemann                                           request);
584ab65b34fSGreg Clayton }
585ab65b34fSGreg Clayton 
586ae34ed2cSRaphael Isemann void CommandCompletions::ArchitectureNames(CommandInterpreter &interpreter,
587a2e76c0bSRaphael Isemann                                            CompletionRequest &request,
588a2e76c0bSRaphael Isemann                                            SearchFilter *searcher) {
589ae34ed2cSRaphael Isemann   ArchSpec::AutoComplete(request);
590ab65b34fSGreg Clayton }
591ab65b34fSGreg Clayton 
592ae34ed2cSRaphael Isemann void CommandCompletions::VariablePath(CommandInterpreter &interpreter,
593a2e76c0bSRaphael Isemann                                       CompletionRequest &request,
594a2e76c0bSRaphael Isemann                                       SearchFilter *searcher) {
595ae34ed2cSRaphael Isemann   Variable::AutoComplete(interpreter.GetExecutionContext(), request);
596f21feadcSGreg Clayton }
597a14f4a75SGongyu Deng 
598a14f4a75SGongyu Deng void CommandCompletions::Registers(CommandInterpreter &interpreter,
599a14f4a75SGongyu Deng                                    CompletionRequest &request,
600a14f4a75SGongyu Deng                                    SearchFilter *searcher) {
601a14f4a75SGongyu Deng   std::string reg_prefix = "";
602a14f4a75SGongyu Deng   if (request.GetCursorArgumentPrefix().startswith("$"))
603a14f4a75SGongyu Deng     reg_prefix = "$";
604a14f4a75SGongyu Deng 
605a14f4a75SGongyu Deng   RegisterContext *reg_ctx =
606a14f4a75SGongyu Deng       interpreter.GetExecutionContext().GetRegisterContext();
607a14f4a75SGongyu Deng   const size_t reg_num = reg_ctx->GetRegisterCount();
608a14f4a75SGongyu Deng   for (size_t reg_idx = 0; reg_idx < reg_num; ++reg_idx) {
609a14f4a75SGongyu Deng     const RegisterInfo *reg_info = reg_ctx->GetRegisterInfoAtIndex(reg_idx);
610a14f4a75SGongyu Deng     request.TryCompleteCurrentArg(reg_prefix + reg_info->name,
611a14f4a75SGongyu Deng                                   reg_info->alt_name);
612a14f4a75SGongyu Deng   }
613a14f4a75SGongyu Deng }
6140e50b9a4SGongyu Deng 
6150e50b9a4SGongyu Deng void CommandCompletions::Breakpoints(CommandInterpreter &interpreter,
6160e50b9a4SGongyu Deng                                      CompletionRequest &request,
6170e50b9a4SGongyu Deng                                      SearchFilter *searcher) {
6180e50b9a4SGongyu Deng   lldb::TargetSP target = interpreter.GetDebugger().GetSelectedTarget();
6190e50b9a4SGongyu Deng   if (!target)
6200e50b9a4SGongyu Deng     return;
6210e50b9a4SGongyu Deng 
6220e50b9a4SGongyu Deng   const BreakpointList &breakpoints = target->GetBreakpointList();
6230e50b9a4SGongyu Deng 
6240e50b9a4SGongyu Deng   std::unique_lock<std::recursive_mutex> lock;
6250e50b9a4SGongyu Deng   target->GetBreakpointList().GetListMutex(lock);
6260e50b9a4SGongyu Deng 
6270e50b9a4SGongyu Deng   size_t num_breakpoints = breakpoints.GetSize();
6280e50b9a4SGongyu Deng   if (num_breakpoints == 0)
6290e50b9a4SGongyu Deng     return;
6300e50b9a4SGongyu Deng 
6310e50b9a4SGongyu Deng   for (size_t i = 0; i < num_breakpoints; ++i) {
6320e50b9a4SGongyu Deng     lldb::BreakpointSP bp = breakpoints.GetBreakpointAtIndex(i);
6330e50b9a4SGongyu Deng 
6340e50b9a4SGongyu Deng     StreamString s;
6350e50b9a4SGongyu Deng     bp->GetDescription(&s, lldb::eDescriptionLevelBrief);
6360e50b9a4SGongyu Deng     llvm::StringRef bp_info = s.GetString();
6370e50b9a4SGongyu Deng 
6380e50b9a4SGongyu Deng     const size_t colon_pos = bp_info.find_first_of(':');
6390e50b9a4SGongyu Deng     if (colon_pos != llvm::StringRef::npos)
6400e50b9a4SGongyu Deng       bp_info = bp_info.drop_front(colon_pos + 2);
6410e50b9a4SGongyu Deng 
6420e50b9a4SGongyu Deng     request.TryCompleteCurrentArg(std::to_string(bp->GetID()), bp_info);
6430e50b9a4SGongyu Deng   }
6440e50b9a4SGongyu Deng }
645763bc230SGongyu Deng 
64622e63cbaSGongyu Deng void CommandCompletions::BreakpointNames(CommandInterpreter &interpreter,
64722e63cbaSGongyu Deng                                          CompletionRequest &request,
64822e63cbaSGongyu Deng                                          SearchFilter *searcher) {
64922e63cbaSGongyu Deng   lldb::TargetSP target = interpreter.GetDebugger().GetSelectedTarget();
65022e63cbaSGongyu Deng   if (!target)
65122e63cbaSGongyu Deng     return;
65222e63cbaSGongyu Deng 
65322e63cbaSGongyu Deng   std::vector<std::string> name_list;
65422e63cbaSGongyu Deng   target->GetBreakpointNames(name_list);
65522e63cbaSGongyu Deng 
65622e63cbaSGongyu Deng   for (const std::string &name : name_list)
65722e63cbaSGongyu Deng     request.TryCompleteCurrentArg(name);
65822e63cbaSGongyu Deng }
65922e63cbaSGongyu Deng 
660763bc230SGongyu Deng void CommandCompletions::ProcessPluginNames(CommandInterpreter &interpreter,
661763bc230SGongyu Deng                                             CompletionRequest &request,
662763bc230SGongyu Deng                                             SearchFilter *searcher) {
663763bc230SGongyu Deng   PluginManager::AutoCompleteProcessName(request.GetCursorArgumentPrefix(),
664763bc230SGongyu Deng                                          request);
665763bc230SGongyu Deng }
6668a5e2969SGongyu Deng void CommandCompletions::DisassemblyFlavors(CommandInterpreter &interpreter,
6678a5e2969SGongyu Deng                                             CompletionRequest &request,
6688a5e2969SGongyu Deng                                             SearchFilter *searcher) {
6698a5e2969SGongyu Deng   // Currently the only valid options for disassemble -F are default, and for
6708a5e2969SGongyu Deng   // Intel architectures, att and intel.
6718a5e2969SGongyu Deng   static const char *flavors[] = {"default", "att", "intel"};
6728a5e2969SGongyu Deng   for (const char *flavor : flavors) {
6738a5e2969SGongyu Deng     request.TryCompleteCurrentArg(flavor);
6748a5e2969SGongyu Deng   }
6758a5e2969SGongyu Deng }
6763ce57e01SGongyu Deng 
67719311f5cSGongyu Deng void CommandCompletions::ProcessIDs(CommandInterpreter &interpreter,
67819311f5cSGongyu Deng                                     CompletionRequest &request,
67919311f5cSGongyu Deng                                     SearchFilter *searcher) {
68019311f5cSGongyu Deng   lldb::PlatformSP platform_sp(interpreter.GetPlatform(true));
68119311f5cSGongyu Deng   if (!platform_sp)
68219311f5cSGongyu Deng     return;
68319311f5cSGongyu Deng   ProcessInstanceInfoList process_infos;
68419311f5cSGongyu Deng   ProcessInstanceInfoMatch match_info;
68519311f5cSGongyu Deng   platform_sp->FindProcesses(match_info, process_infos);
68619311f5cSGongyu Deng   for (const ProcessInstanceInfo &info : process_infos)
68719311f5cSGongyu Deng     request.TryCompleteCurrentArg(std::to_string(info.GetProcessID()),
68819311f5cSGongyu Deng                                   info.GetNameAsStringRef());
68919311f5cSGongyu Deng }
69019311f5cSGongyu Deng 
69119311f5cSGongyu Deng void CommandCompletions::ProcessNames(CommandInterpreter &interpreter,
69219311f5cSGongyu Deng                                       CompletionRequest &request,
69319311f5cSGongyu Deng                                       SearchFilter *searcher) {
69419311f5cSGongyu Deng   lldb::PlatformSP platform_sp(interpreter.GetPlatform(true));
69519311f5cSGongyu Deng   if (!platform_sp)
69619311f5cSGongyu Deng     return;
69719311f5cSGongyu Deng   ProcessInstanceInfoList process_infos;
69819311f5cSGongyu Deng   ProcessInstanceInfoMatch match_info;
69919311f5cSGongyu Deng   platform_sp->FindProcesses(match_info, process_infos);
70019311f5cSGongyu Deng   for (const ProcessInstanceInfo &info : process_infos)
70119311f5cSGongyu Deng     request.TryCompleteCurrentArg(info.GetNameAsStringRef());
70219311f5cSGongyu Deng }
70319311f5cSGongyu Deng 
7043ce57e01SGongyu Deng void CommandCompletions::TypeLanguages(CommandInterpreter &interpreter,
7053ce57e01SGongyu Deng                                        CompletionRequest &request,
7063ce57e01SGongyu Deng                                        SearchFilter *searcher) {
7073ce57e01SGongyu Deng   for (int bit :
7083ce57e01SGongyu Deng        Language::GetLanguagesSupportingTypeSystems().bitvector.set_bits()) {
7093ce57e01SGongyu Deng     request.TryCompleteCurrentArg(
7103ce57e01SGongyu Deng         Language::GetNameForLanguageType(static_cast<lldb::LanguageType>(bit)));
7113ce57e01SGongyu Deng   }
7123ce57e01SGongyu Deng }
71366fa73faSGongyu Deng 
71466fa73faSGongyu Deng void CommandCompletions::FrameIndexes(CommandInterpreter &interpreter,
71566fa73faSGongyu Deng                                       CompletionRequest &request,
71666fa73faSGongyu Deng                                       SearchFilter *searcher) {
71766fa73faSGongyu Deng   const ExecutionContext &exe_ctx = interpreter.GetExecutionContext();
71866fa73faSGongyu Deng   if (!exe_ctx.HasProcessScope())
71966fa73faSGongyu Deng     return;
72066fa73faSGongyu Deng 
72166fa73faSGongyu Deng   lldb::ThreadSP thread_sp = exe_ctx.GetThreadSP();
72266fa73faSGongyu Deng   const uint32_t frame_num = thread_sp->GetStackFrameCount();
72366fa73faSGongyu Deng   for (uint32_t i = 0; i < frame_num; ++i) {
72466fa73faSGongyu Deng     lldb::StackFrameSP frame_sp = thread_sp->GetStackFrameAtIndex(i);
72566fa73faSGongyu Deng     StreamString strm;
72666fa73faSGongyu Deng     frame_sp->Dump(&strm, false, true);
72766fa73faSGongyu Deng     request.TryCompleteCurrentArg(std::to_string(i), strm.GetString());
72866fa73faSGongyu Deng   }
72966fa73faSGongyu Deng }
730b2b7dbb4SGongyu Deng 
731b2b7dbb4SGongyu Deng void CommandCompletions::StopHookIDs(CommandInterpreter &interpreter,
732b2b7dbb4SGongyu Deng                                      CompletionRequest &request,
733b2b7dbb4SGongyu Deng                                      SearchFilter *searcher) {
734b2b7dbb4SGongyu Deng   const lldb::TargetSP target_sp =
735b2b7dbb4SGongyu Deng       interpreter.GetExecutionContext().GetTargetSP();
736b2b7dbb4SGongyu Deng   if (!target_sp)
737b2b7dbb4SGongyu Deng     return;
738b2b7dbb4SGongyu Deng 
739b2b7dbb4SGongyu Deng   const size_t num = target_sp->GetNumStopHooks();
740b2b7dbb4SGongyu Deng   for (size_t idx = 0; idx < num; ++idx) {
741b2b7dbb4SGongyu Deng     StreamString strm;
742b2b7dbb4SGongyu Deng     // The value 11 is an offset to make the completion description looks
743b2b7dbb4SGongyu Deng     // neater.
744b2b7dbb4SGongyu Deng     strm.SetIndentLevel(11);
745b2b7dbb4SGongyu Deng     const Target::StopHookSP stophook_sp = target_sp->GetStopHookAtIndex(idx);
746b2b7dbb4SGongyu Deng     stophook_sp->GetDescription(&strm, lldb::eDescriptionLevelInitial);
747b2b7dbb4SGongyu Deng     request.TryCompleteCurrentArg(std::to_string(stophook_sp->GetID()),
748b2b7dbb4SGongyu Deng                                   strm.GetString());
749b2b7dbb4SGongyu Deng   }
750b2b7dbb4SGongyu Deng }
751a952fe23SGongyu Deng 
752a952fe23SGongyu Deng void CommandCompletions::ThreadIndexes(CommandInterpreter &interpreter,
753a952fe23SGongyu Deng                                        CompletionRequest &request,
754a952fe23SGongyu Deng                                        SearchFilter *searcher) {
755a952fe23SGongyu Deng   const ExecutionContext &exe_ctx = interpreter.GetExecutionContext();
756a952fe23SGongyu Deng   if (!exe_ctx.HasProcessScope())
757a952fe23SGongyu Deng     return;
758a952fe23SGongyu Deng 
759a952fe23SGongyu Deng   ThreadList &threads = exe_ctx.GetProcessPtr()->GetThreadList();
760a952fe23SGongyu Deng   lldb::ThreadSP thread_sp;
761a952fe23SGongyu Deng   for (uint32_t idx = 0; (thread_sp = threads.GetThreadAtIndex(idx)); ++idx) {
762a952fe23SGongyu Deng     StreamString strm;
763a952fe23SGongyu Deng     thread_sp->GetStatus(strm, 0, 1, 1, true);
764a952fe23SGongyu Deng     request.TryCompleteCurrentArg(std::to_string(thread_sp->GetIndexID()),
765a952fe23SGongyu Deng                                   strm.GetString());
766a952fe23SGongyu Deng   }
767a952fe23SGongyu Deng }
7684f3559dbSGongyu Deng 
7694f3559dbSGongyu Deng void CommandCompletions::WatchPointIDs(CommandInterpreter &interpreter,
7704f3559dbSGongyu Deng                                        CompletionRequest &request,
7714f3559dbSGongyu Deng                                        SearchFilter *searcher) {
7724f3559dbSGongyu Deng   const ExecutionContext &exe_ctx = interpreter.GetExecutionContext();
7734f3559dbSGongyu Deng   if (!exe_ctx.HasTargetScope())
7744f3559dbSGongyu Deng     return;
7754f3559dbSGongyu Deng 
7764f3559dbSGongyu Deng   const WatchpointList &wp_list = exe_ctx.GetTargetPtr()->GetWatchpointList();
777*7b54b1cdSMichał Górny   for (lldb::WatchpointSP wp_sp : wp_list.Watchpoints()) {
7784f3559dbSGongyu Deng     StreamString strm;
7794f3559dbSGongyu Deng     wp_sp->Dump(&strm);
7804f3559dbSGongyu Deng     request.TryCompleteCurrentArg(std::to_string(wp_sp->GetID()),
7814f3559dbSGongyu Deng                                   strm.GetString());
7824f3559dbSGongyu Deng   }
7834f3559dbSGongyu Deng }
784188f1ac3SGongyu Deng 
785188f1ac3SGongyu Deng void CommandCompletions::TypeCategoryNames(CommandInterpreter &interpreter,
786188f1ac3SGongyu Deng                                            CompletionRequest &request,
787188f1ac3SGongyu Deng                                            SearchFilter *searcher) {
788188f1ac3SGongyu Deng   DataVisualization::Categories::ForEach(
789188f1ac3SGongyu Deng       [&request](const lldb::TypeCategoryImplSP &category_sp) {
790188f1ac3SGongyu Deng         request.TryCompleteCurrentArg(category_sp->GetName(),
791188f1ac3SGongyu Deng                                       category_sp->GetDescription());
792188f1ac3SGongyu Deng         return true;
793188f1ac3SGongyu Deng       });
794188f1ac3SGongyu Deng }
795