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