1 //===-- CommandCompletions.cpp ----------------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include <sys/stat.h>
10 #if defined(__APPLE__) || defined(__linux__)
11 #include <pwd.h>
12 #endif
13 
14 #include "llvm/ADT/SmallString.h"
15 #include "llvm/ADT/StringSet.h"
16 
17 #include "lldb/Core/FileSpecList.h"
18 #include "lldb/Core/Module.h"
19 #include "lldb/Core/PluginManager.h"
20 #include "lldb/Host/FileSystem.h"
21 #include "lldb/Interpreter/CommandCompletions.h"
22 #include "lldb/Interpreter/CommandInterpreter.h"
23 #include "lldb/Interpreter/OptionValueProperties.h"
24 #include "lldb/Symbol/CompileUnit.h"
25 #include "lldb/Symbol/Variable.h"
26 #include "lldb/Target/Target.h"
27 #include "lldb/Utility/Args.h"
28 #include "lldb/Utility/FileSpec.h"
29 #include "lldb/Utility/StreamString.h"
30 #include "lldb/Utility/TildeExpressionResolver.h"
31 
32 #include "llvm/ADT/SmallString.h"
33 #include "llvm/Support/FileSystem.h"
34 #include "llvm/Support/Path.h"
35 
36 using namespace lldb_private;
37 
38 CommandCompletions::CommonCompletionElement
39     CommandCompletions::g_common_completions[] = {
40         {eCustomCompletion, nullptr},
41         {eSourceFileCompletion, CommandCompletions::SourceFiles},
42         {eDiskFileCompletion, CommandCompletions::DiskFiles},
43         {eDiskDirectoryCompletion, CommandCompletions::DiskDirectories},
44         {eSymbolCompletion, CommandCompletions::Symbols},
45         {eModuleCompletion, CommandCompletions::Modules},
46         {eSettingsNameCompletion, CommandCompletions::SettingsNames},
47         {ePlatformPluginCompletion, CommandCompletions::PlatformPluginNames},
48         {eArchitectureCompletion, CommandCompletions::ArchitectureNames},
49         {eVariablePathCompletion, CommandCompletions::VariablePath},
50         {eNoCompletion, nullptr} // This one has to be last in the list.
51 };
52 
53 bool CommandCompletions::InvokeCommonCompletionCallbacks(
54     CommandInterpreter &interpreter, uint32_t completion_mask,
55     CompletionRequest &request, SearchFilter *searcher) {
56   bool handled = false;
57 
58   if (completion_mask & eCustomCompletion)
59     return false;
60 
61   for (int i = 0;; i++) {
62     if (g_common_completions[i].type == eNoCompletion)
63       break;
64     else if ((g_common_completions[i].type & completion_mask) ==
65                  g_common_completions[i].type &&
66              g_common_completions[i].callback != nullptr) {
67       handled = true;
68       g_common_completions[i].callback(interpreter, request, searcher);
69     }
70   }
71   return handled;
72 }
73 
74 void CommandCompletions::SourceFiles(CommandInterpreter &interpreter,
75                                      CompletionRequest &request,
76                                      SearchFilter *searcher) {
77   // Find some way to switch "include support files..."
78   SourceFileCompleter completer(interpreter, false, request);
79 
80   if (searcher == nullptr) {
81     lldb::TargetSP target_sp = interpreter.GetDebugger().GetSelectedTarget();
82     SearchFilterForUnconstrainedSearches null_searcher(target_sp);
83     completer.DoCompletion(&null_searcher);
84   } else {
85     completer.DoCompletion(searcher);
86   }
87 }
88 
89 static void DiskFilesOrDirectories(const llvm::Twine &partial_name,
90                                    bool only_directories,
91                                    CompletionRequest &request,
92                                    TildeExpressionResolver &Resolver) {
93   llvm::SmallString<256> CompletionBuffer;
94   llvm::SmallString<256> Storage;
95   partial_name.toVector(CompletionBuffer);
96 
97   if (CompletionBuffer.size() >= PATH_MAX)
98     return;
99 
100   namespace path = llvm::sys::path;
101 
102   llvm::StringRef SearchDir;
103   llvm::StringRef PartialItem;
104 
105   if (CompletionBuffer.startswith("~")) {
106     llvm::StringRef Buffer(CompletionBuffer);
107     size_t FirstSep =
108         Buffer.find_if([](char c) { return path::is_separator(c); });
109 
110     llvm::StringRef Username = Buffer.take_front(FirstSep);
111     llvm::StringRef Remainder;
112     if (FirstSep != llvm::StringRef::npos)
113       Remainder = Buffer.drop_front(FirstSep + 1);
114 
115     llvm::SmallString<256> Resolved;
116     if (!Resolver.ResolveExact(Username, Resolved)) {
117       // We couldn't resolve it as a full username.  If there were no slashes
118       // then this might be a partial username.   We try to resolve it as such
119       // but after that, we're done regardless of any matches.
120       if (FirstSep == llvm::StringRef::npos) {
121         llvm::StringSet<> MatchSet;
122         Resolver.ResolvePartial(Username, MatchSet);
123         for (const auto &S : MatchSet) {
124           Resolved = S.getKey();
125           path::append(Resolved, path::get_separator());
126           request.AddCompletion(Resolved, "", CompletionMode::Partial);
127         }
128       }
129       return;
130     }
131 
132     // If there was no trailing slash, then we're done as soon as we resolve
133     // the expression to the correct directory.  Otherwise we need to continue
134     // looking for matches within that directory.
135     if (FirstSep == llvm::StringRef::npos) {
136       // Make sure it ends with a separator.
137       path::append(CompletionBuffer, path::get_separator());
138       request.AddCompletion(CompletionBuffer, "", CompletionMode::Partial);
139       return;
140     }
141 
142     // We want to keep the form the user typed, so we special case this to
143     // search in the fully resolved directory, but CompletionBuffer keeps the
144     // unmodified form that the user typed.
145     Storage = Resolved;
146     llvm::StringRef RemainderDir = path::parent_path(Remainder);
147     if (!RemainderDir.empty()) {
148       // Append the remaining path to the resolved directory.
149       Storage.append(path::get_separator());
150       Storage.append(RemainderDir);
151     }
152     SearchDir = Storage;
153   } else {
154     SearchDir = path::parent_path(CompletionBuffer);
155   }
156 
157   size_t FullPrefixLen = CompletionBuffer.size();
158 
159   PartialItem = path::filename(CompletionBuffer);
160 
161   // path::filename() will return "." when the passed path ends with a
162   // directory separator. We have to filter those out, but only when the
163   // "." doesn't come from the completion request itself.
164   if (PartialItem == "." && path::is_separator(CompletionBuffer.back()))
165     PartialItem = llvm::StringRef();
166 
167   if (SearchDir.empty()) {
168     llvm::sys::fs::current_path(Storage);
169     SearchDir = Storage;
170   }
171   assert(!PartialItem.contains(path::get_separator()));
172 
173   // SearchDir now contains the directory to search in, and Prefix contains the
174   // text we want to match against items in that directory.
175 
176   FileSystem &fs = FileSystem::Instance();
177   std::error_code EC;
178   llvm::vfs::directory_iterator Iter = fs.DirBegin(SearchDir, EC);
179   llvm::vfs::directory_iterator End;
180   for (; Iter != End && !EC; Iter.increment(EC)) {
181     auto &Entry = *Iter;
182     llvm::ErrorOr<llvm::vfs::Status> Status = fs.GetStatus(Entry.path());
183 
184     if (!Status)
185       continue;
186 
187     auto Name = path::filename(Entry.path());
188 
189     // Omit ".", ".."
190     if (Name == "." || Name == ".." || !Name.startswith(PartialItem))
191       continue;
192 
193     bool is_dir = Status->isDirectory();
194 
195     // If it's a symlink, then we treat it as a directory as long as the target
196     // is a directory.
197     if (Status->isSymlink()) {
198       FileSpec symlink_filespec(Entry.path());
199       FileSpec resolved_filespec;
200       auto error = fs.ResolveSymbolicLink(symlink_filespec, resolved_filespec);
201       if (error.Success())
202         is_dir = fs.IsDirectory(symlink_filespec);
203     }
204 
205     if (only_directories && !is_dir)
206       continue;
207 
208     // Shrink it back down so that it just has the original prefix the user
209     // typed and remove the part of the name which is common to the located
210     // item and what the user typed.
211     CompletionBuffer.resize(FullPrefixLen);
212     Name = Name.drop_front(PartialItem.size());
213     CompletionBuffer.append(Name);
214 
215     if (is_dir) {
216       path::append(CompletionBuffer, path::get_separator());
217     }
218 
219     CompletionMode mode =
220         is_dir ? CompletionMode::Partial : CompletionMode::Normal;
221     request.AddCompletion(CompletionBuffer, "", mode);
222   }
223 }
224 
225 static void DiskFilesOrDirectories(const llvm::Twine &partial_name,
226                                    bool only_directories, StringList &matches,
227                                    TildeExpressionResolver &Resolver) {
228   CompletionResult result;
229   std::string partial_name_str = partial_name.str();
230   CompletionRequest request(partial_name_str, partial_name_str.size(), result);
231   DiskFilesOrDirectories(partial_name, only_directories, request, Resolver);
232   result.GetMatches(matches);
233 }
234 
235 static void DiskFilesOrDirectories(CompletionRequest &request,
236                                    bool only_directories) {
237   StandardTildeExpressionResolver resolver;
238   DiskFilesOrDirectories(request.GetCursorArgumentPrefix(), only_directories,
239                          request, resolver);
240 }
241 
242 void CommandCompletions::DiskFiles(CommandInterpreter &interpreter,
243                                    CompletionRequest &request,
244                                    SearchFilter *searcher) {
245   DiskFilesOrDirectories(request, /*only_dirs*/ false);
246 }
247 
248 void CommandCompletions::DiskFiles(const llvm::Twine &partial_file_name,
249                                    StringList &matches,
250                                    TildeExpressionResolver &Resolver) {
251   DiskFilesOrDirectories(partial_file_name, false, matches, Resolver);
252 }
253 
254 void CommandCompletions::DiskDirectories(CommandInterpreter &interpreter,
255                                          CompletionRequest &request,
256                                          SearchFilter *searcher) {
257   DiskFilesOrDirectories(request, /*only_dirs*/ true);
258 }
259 
260 void CommandCompletions::DiskDirectories(const llvm::Twine &partial_file_name,
261                                          StringList &matches,
262                                          TildeExpressionResolver &Resolver) {
263   DiskFilesOrDirectories(partial_file_name, true, matches, Resolver);
264 }
265 
266 void CommandCompletions::Modules(CommandInterpreter &interpreter,
267                                  CompletionRequest &request,
268                                  SearchFilter *searcher) {
269   ModuleCompleter completer(interpreter, request);
270 
271   if (searcher == nullptr) {
272     lldb::TargetSP target_sp = interpreter.GetDebugger().GetSelectedTarget();
273     SearchFilterForUnconstrainedSearches null_searcher(target_sp);
274     completer.DoCompletion(&null_searcher);
275   } else {
276     completer.DoCompletion(searcher);
277   }
278 }
279 
280 void CommandCompletions::Symbols(CommandInterpreter &interpreter,
281                                  CompletionRequest &request,
282                                  SearchFilter *searcher) {
283   SymbolCompleter completer(interpreter, request);
284 
285   if (searcher == nullptr) {
286     lldb::TargetSP target_sp = interpreter.GetDebugger().GetSelectedTarget();
287     SearchFilterForUnconstrainedSearches null_searcher(target_sp);
288     completer.DoCompletion(&null_searcher);
289   } else {
290     completer.DoCompletion(searcher);
291   }
292 }
293 
294 void CommandCompletions::SettingsNames(CommandInterpreter &interpreter,
295                                        CompletionRequest &request,
296                                        SearchFilter *searcher) {
297   // Cache the full setting name list
298   static StringList g_property_names;
299   if (g_property_names.GetSize() == 0) {
300     // Generate the full setting name list on demand
301     lldb::OptionValuePropertiesSP properties_sp(
302         interpreter.GetDebugger().GetValueProperties());
303     if (properties_sp) {
304       StreamString strm;
305       properties_sp->DumpValue(nullptr, strm, OptionValue::eDumpOptionName);
306       const std::string &str = strm.GetString();
307       g_property_names.SplitIntoLines(str.c_str(), str.size());
308     }
309   }
310 
311   for (const std::string &s : g_property_names)
312     request.TryCompleteCurrentArg(s);
313 }
314 
315 void CommandCompletions::PlatformPluginNames(CommandInterpreter &interpreter,
316                                              CompletionRequest &request,
317                                              SearchFilter *searcher) {
318   PluginManager::AutoCompletePlatformName(request.GetCursorArgumentPrefix(),
319                                           request);
320 }
321 
322 void CommandCompletions::ArchitectureNames(CommandInterpreter &interpreter,
323                                            CompletionRequest &request,
324                                            SearchFilter *searcher) {
325   ArchSpec::AutoComplete(request);
326 }
327 
328 void CommandCompletions::VariablePath(CommandInterpreter &interpreter,
329                                       CompletionRequest &request,
330                                       SearchFilter *searcher) {
331   Variable::AutoComplete(interpreter.GetExecutionContext(), request);
332 }
333 
334 CommandCompletions::Completer::Completer(CommandInterpreter &interpreter,
335                                          CompletionRequest &request)
336     : m_interpreter(interpreter), m_request(request) {}
337 
338 CommandCompletions::Completer::~Completer() = default;
339 
340 // SourceFileCompleter
341 
342 CommandCompletions::SourceFileCompleter::SourceFileCompleter(
343     CommandInterpreter &interpreter, bool include_support_files,
344     CompletionRequest &request)
345     : CommandCompletions::Completer(interpreter, request),
346       m_include_support_files(include_support_files), m_matching_files() {
347   FileSpec partial_spec(m_request.GetCursorArgumentPrefix());
348   m_file_name = partial_spec.GetFilename().GetCString();
349   m_dir_name = partial_spec.GetDirectory().GetCString();
350 }
351 
352 lldb::SearchDepth CommandCompletions::SourceFileCompleter::GetDepth() {
353   return lldb::eSearchDepthCompUnit;
354 }
355 
356 Searcher::CallbackReturn
357 CommandCompletions::SourceFileCompleter::SearchCallback(SearchFilter &filter,
358                                                         SymbolContext &context,
359                                                         Address *addr) {
360   if (context.comp_unit != nullptr) {
361     if (m_include_support_files) {
362       FileSpecList supporting_files = context.comp_unit->GetSupportFiles();
363       for (size_t sfiles = 0; sfiles < supporting_files.GetSize(); sfiles++) {
364         const FileSpec &sfile_spec =
365             supporting_files.GetFileSpecAtIndex(sfiles);
366         const char *sfile_file_name = sfile_spec.GetFilename().GetCString();
367         const char *sfile_dir_name = sfile_spec.GetFilename().GetCString();
368         bool match = false;
369         if (m_file_name && sfile_file_name &&
370             strstr(sfile_file_name, m_file_name) == sfile_file_name)
371           match = true;
372         if (match && m_dir_name && sfile_dir_name &&
373             strstr(sfile_dir_name, m_dir_name) != sfile_dir_name)
374           match = false;
375 
376         if (match) {
377           m_matching_files.AppendIfUnique(sfile_spec);
378         }
379       }
380     } else {
381       const char *cur_file_name = context.comp_unit->GetFilename().GetCString();
382       const char *cur_dir_name = context.comp_unit->GetDirectory().GetCString();
383 
384       bool match = false;
385       if (m_file_name && cur_file_name &&
386           strstr(cur_file_name, m_file_name) == cur_file_name)
387         match = true;
388 
389       if (match && m_dir_name && cur_dir_name &&
390           strstr(cur_dir_name, m_dir_name) != cur_dir_name)
391         match = false;
392 
393       if (match) {
394         m_matching_files.AppendIfUnique(context.comp_unit);
395       }
396     }
397   }
398   return Searcher::eCallbackReturnContinue;
399 }
400 
401 void CommandCompletions::SourceFileCompleter::DoCompletion(
402     SearchFilter *filter) {
403   filter->Search(*this);
404   // Now convert the filelist to completions:
405   for (size_t i = 0; i < m_matching_files.GetSize(); i++) {
406     m_request.AddCompletion(
407         m_matching_files.GetFileSpecAtIndex(i).GetFilename().GetCString());
408   }
409 }
410 
411 // SymbolCompleter
412 
413 static bool regex_chars(const char comp) {
414   return (comp == '[' || comp == ']' || comp == '(' || comp == ')' ||
415           comp == '{' || comp == '}' || comp == '+' || comp == '.' ||
416           comp == '*' || comp == '|' || comp == '^' || comp == '$' ||
417           comp == '\\' || comp == '?');
418 }
419 
420 CommandCompletions::SymbolCompleter::SymbolCompleter(
421     CommandInterpreter &interpreter, CompletionRequest &request)
422     : CommandCompletions::Completer(interpreter, request) {
423   std::string regex_str;
424   if (!m_request.GetCursorArgumentPrefix().empty()) {
425     regex_str.append("^");
426     regex_str.append(m_request.GetCursorArgumentPrefix());
427   } else {
428     // Match anything since the completion string is empty
429     regex_str.append(".");
430   }
431   std::string::iterator pos =
432       find_if(regex_str.begin() + 1, regex_str.end(), regex_chars);
433   while (pos < regex_str.end()) {
434     pos = regex_str.insert(pos, '\\');
435     pos = find_if(pos + 2, regex_str.end(), regex_chars);
436   }
437   m_regex = RegularExpression(regex_str);
438 }
439 
440 lldb::SearchDepth CommandCompletions::SymbolCompleter::GetDepth() {
441   return lldb::eSearchDepthModule;
442 }
443 
444 Searcher::CallbackReturn CommandCompletions::SymbolCompleter::SearchCallback(
445     SearchFilter &filter, SymbolContext &context, Address *addr) {
446   if (context.module_sp) {
447     SymbolContextList sc_list;
448     const bool include_symbols = true;
449     const bool include_inlines = true;
450     context.module_sp->FindFunctions(m_regex, include_symbols, include_inlines,
451                                      sc_list);
452 
453     SymbolContext sc;
454     // Now add the functions & symbols to the list - only add if unique:
455     for (uint32_t i = 0; i < sc_list.GetSize(); i++) {
456       if (sc_list.GetContextAtIndex(i, sc)) {
457         ConstString func_name = sc.GetFunctionName(Mangled::ePreferDemangled);
458         // Ensure that the function name matches the regex. This is more than a
459         // sanity check. It is possible that the demangled function name does
460         // not start with the prefix, for example when it's in an anonymous
461         // namespace.
462         if (!func_name.IsEmpty() && m_regex.Execute(func_name.GetStringRef()))
463           m_match_set.insert(func_name);
464       }
465     }
466   }
467   return Searcher::eCallbackReturnContinue;
468 }
469 
470 void CommandCompletions::SymbolCompleter::DoCompletion(SearchFilter *filter) {
471   filter->Search(*this);
472   collection::iterator pos = m_match_set.begin(), end = m_match_set.end();
473   for (pos = m_match_set.begin(); pos != end; pos++)
474     m_request.AddCompletion((*pos).GetCString());
475 }
476 
477 // ModuleCompleter
478 CommandCompletions::ModuleCompleter::ModuleCompleter(
479     CommandInterpreter &interpreter, CompletionRequest &request)
480     : CommandCompletions::Completer(interpreter, request) {
481   FileSpec partial_spec(m_request.GetCursorArgumentPrefix());
482   m_file_name = partial_spec.GetFilename().GetCString();
483   m_dir_name = partial_spec.GetDirectory().GetCString();
484 }
485 
486 lldb::SearchDepth CommandCompletions::ModuleCompleter::GetDepth() {
487   return lldb::eSearchDepthModule;
488 }
489 
490 Searcher::CallbackReturn CommandCompletions::ModuleCompleter::SearchCallback(
491     SearchFilter &filter, SymbolContext &context, Address *addr) {
492   if (context.module_sp) {
493     const char *cur_file_name =
494         context.module_sp->GetFileSpec().GetFilename().GetCString();
495     const char *cur_dir_name =
496         context.module_sp->GetFileSpec().GetDirectory().GetCString();
497 
498     bool match = false;
499     if (m_file_name && cur_file_name &&
500         strstr(cur_file_name, m_file_name) == cur_file_name)
501       match = true;
502 
503     if (match && m_dir_name && cur_dir_name &&
504         strstr(cur_dir_name, m_dir_name) != cur_dir_name)
505       match = false;
506 
507     if (match) {
508       m_request.AddCompletion(cur_file_name);
509     }
510   }
511   return Searcher::eCallbackReturnContinue;
512 }
513 
514 void CommandCompletions::ModuleCompleter::DoCompletion(SearchFilter *filter) {
515   filter->Search(*this);
516 }
517