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