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