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 llvm::StringRef RemainderDir = path::parent_path(Remainder.str()); 168 if (!RemainderDir.empty()) { 169 // Append the remaining path to the resolved directory. 170 Storage.append(path::get_separator()); 171 Storage.append(RemainderDir); 172 } 173 SearchDir = Storage; 174 } else { 175 SearchDir = path::parent_path(CompletionBuffer); 176 } 177 178 size_t FullPrefixLen = CompletionBuffer.size(); 179 180 PartialItem = path::filename(CompletionBuffer); 181 if (PartialItem == ".") 182 PartialItem = llvm::StringRef(); 183 184 if (SearchDir.empty()) { 185 llvm::sys::fs::current_path(Storage); 186 SearchDir = Storage; 187 } 188 assert(!PartialItem.contains(path::get_separator())); 189 190 // SearchDir now contains the directory to search in, and Prefix contains the 191 // text we want to match against items in that directory. 192 193 std::error_code EC; 194 fs::directory_iterator Iter(SearchDir, EC, false); 195 fs::directory_iterator End; 196 for (; Iter != End && !EC; Iter.increment(EC)) { 197 auto &Entry = *Iter; 198 199 auto Name = path::filename(Entry.path()); 200 201 // Omit ".", ".." 202 if (Name == "." || Name == ".." || !Name.startswith(PartialItem)) 203 continue; 204 205 // We have a match. 206 207 llvm::ErrorOr<fs::basic_file_status> st = Entry.status(); 208 if (!st) 209 continue; 210 211 // If it's a symlink, then we treat it as a directory as long as the target 212 // is a directory. 213 bool is_dir = fs::is_directory(*st); 214 if (fs::is_symlink_file(*st)) { 215 fs::file_status target_st; 216 if (!fs::status(Entry.path(), target_st)) 217 is_dir = fs::is_directory(target_st); 218 } 219 if (only_directories && !is_dir) 220 continue; 221 222 // Shrink it back down so that it just has the original prefix the user 223 // typed and remove the part of the name which is common to the located 224 // item and what the user typed. 225 CompletionBuffer.resize(FullPrefixLen); 226 Name = Name.drop_front(PartialItem.size()); 227 CompletionBuffer.append(Name); 228 229 if (is_dir) { 230 saw_directory = true; 231 path::append(CompletionBuffer, path::get_separator()); 232 } 233 234 matches.AppendString(CompletionBuffer); 235 } 236 237 return matches.GetSize(); 238 } 239 240 int CommandCompletions::DiskFiles(CommandInterpreter &interpreter, 241 llvm::StringRef partial_file_name, 242 int match_start_point, 243 int max_return_elements, 244 SearchFilter *searcher, bool &word_complete, 245 StringList &matches) { 246 word_complete = false; 247 StandardTildeExpressionResolver Resolver; 248 return DiskFiles(partial_file_name, matches, Resolver); 249 } 250 251 int CommandCompletions::DiskFiles(const llvm::Twine &partial_file_name, 252 StringList &matches, 253 TildeExpressionResolver &Resolver) { 254 bool word_complete; 255 int ret_val = DiskFilesOrDirectories(partial_file_name, false, word_complete, 256 matches, Resolver); 257 return ret_val; 258 } 259 260 int CommandCompletions::DiskDirectories( 261 CommandInterpreter &interpreter, llvm::StringRef partial_file_name, 262 int match_start_point, int max_return_elements, SearchFilter *searcher, 263 bool &word_complete, StringList &matches) { 264 word_complete = false; 265 StandardTildeExpressionResolver Resolver; 266 return DiskDirectories(partial_file_name, matches, Resolver); 267 } 268 269 int CommandCompletions::DiskDirectories(const llvm::Twine &partial_file_name, 270 StringList &matches, 271 TildeExpressionResolver &Resolver) { 272 bool word_complete; 273 int ret_val = DiskFilesOrDirectories(partial_file_name, true, word_complete, 274 matches, Resolver); 275 return ret_val; 276 } 277 278 int CommandCompletions::Modules(CommandInterpreter &interpreter, 279 llvm::StringRef partial_file_name, 280 int match_start_point, int max_return_elements, 281 SearchFilter *searcher, bool &word_complete, 282 StringList &matches) { 283 word_complete = true; 284 ModuleCompleter completer(interpreter, partial_file_name, match_start_point, 285 max_return_elements, matches); 286 287 if (searcher == nullptr) { 288 lldb::TargetSP target_sp = interpreter.GetDebugger().GetSelectedTarget(); 289 SearchFilterForUnconstrainedSearches null_searcher(target_sp); 290 completer.DoCompletion(&null_searcher); 291 } else { 292 completer.DoCompletion(searcher); 293 } 294 return matches.GetSize(); 295 } 296 297 int CommandCompletions::Symbols(CommandInterpreter &interpreter, 298 llvm::StringRef partial_file_name, 299 int match_start_point, int max_return_elements, 300 SearchFilter *searcher, bool &word_complete, 301 StringList &matches) { 302 word_complete = true; 303 SymbolCompleter completer(interpreter, partial_file_name, match_start_point, 304 max_return_elements, matches); 305 306 if (searcher == nullptr) { 307 lldb::TargetSP target_sp = interpreter.GetDebugger().GetSelectedTarget(); 308 SearchFilterForUnconstrainedSearches null_searcher(target_sp); 309 completer.DoCompletion(&null_searcher); 310 } else { 311 completer.DoCompletion(searcher); 312 } 313 return matches.GetSize(); 314 } 315 316 int CommandCompletions::SettingsNames( 317 CommandInterpreter &interpreter, llvm::StringRef partial_setting_name, 318 int match_start_point, int max_return_elements, SearchFilter *searcher, 319 bool &word_complete, StringList &matches) { 320 // Cache the full setting name list 321 static StringList g_property_names; 322 if (g_property_names.GetSize() == 0) { 323 // Generate the full setting name list on demand 324 lldb::OptionValuePropertiesSP properties_sp( 325 interpreter.GetDebugger().GetValueProperties()); 326 if (properties_sp) { 327 StreamString strm; 328 properties_sp->DumpValue(nullptr, strm, OptionValue::eDumpOptionName); 329 const std::string &str = strm.GetString(); 330 g_property_names.SplitIntoLines(str.c_str(), str.size()); 331 } 332 } 333 334 size_t exact_matches_idx = SIZE_MAX; 335 const size_t num_matches = g_property_names.AutoComplete( 336 partial_setting_name, matches, exact_matches_idx); 337 word_complete = exact_matches_idx != SIZE_MAX; 338 return num_matches; 339 } 340 341 int CommandCompletions::PlatformPluginNames( 342 CommandInterpreter &interpreter, llvm::StringRef partial_name, 343 int match_start_point, int max_return_elements, SearchFilter *searcher, 344 bool &word_complete, lldb_private::StringList &matches) { 345 const uint32_t num_matches = 346 PluginManager::AutoCompletePlatformName(partial_name, matches); 347 word_complete = num_matches == 1; 348 return num_matches; 349 } 350 351 int CommandCompletions::ArchitectureNames( 352 CommandInterpreter &interpreter, llvm::StringRef partial_name, 353 int match_start_point, int max_return_elements, SearchFilter *searcher, 354 bool &word_complete, lldb_private::StringList &matches) { 355 const uint32_t num_matches = ArchSpec::AutoComplete(partial_name, matches); 356 word_complete = num_matches == 1; 357 return num_matches; 358 } 359 360 int CommandCompletions::VariablePath( 361 CommandInterpreter &interpreter, llvm::StringRef partial_name, 362 int match_start_point, int max_return_elements, SearchFilter *searcher, 363 bool &word_complete, lldb_private::StringList &matches) { 364 return Variable::AutoComplete(interpreter.GetExecutionContext(), partial_name, 365 matches, word_complete); 366 } 367 368 CommandCompletions::Completer::Completer(CommandInterpreter &interpreter, 369 llvm::StringRef completion_str, 370 int match_start_point, 371 int max_return_elements, 372 StringList &matches) 373 : m_interpreter(interpreter), m_completion_str(completion_str), 374 m_match_start_point(match_start_point), 375 m_max_return_elements(max_return_elements), m_matches(matches) {} 376 377 CommandCompletions::Completer::~Completer() = default; 378 379 //---------------------------------------------------------------------- 380 // SourceFileCompleter 381 //---------------------------------------------------------------------- 382 383 CommandCompletions::SourceFileCompleter::SourceFileCompleter( 384 CommandInterpreter &interpreter, bool include_support_files, 385 llvm::StringRef completion_str, int match_start_point, 386 int max_return_elements, StringList &matches) 387 : CommandCompletions::Completer(interpreter, completion_str, 388 match_start_point, max_return_elements, 389 matches), 390 m_include_support_files(include_support_files), m_matching_files() { 391 FileSpec partial_spec(m_completion_str, false); 392 m_file_name = partial_spec.GetFilename().GetCString(); 393 m_dir_name = partial_spec.GetDirectory().GetCString(); 394 } 395 396 Searcher::Depth CommandCompletions::SourceFileCompleter::GetDepth() { 397 return eDepthCompUnit; 398 } 399 400 Searcher::CallbackReturn 401 CommandCompletions::SourceFileCompleter::SearchCallback(SearchFilter &filter, 402 SymbolContext &context, 403 Address *addr, 404 bool complete) { 405 if (context.comp_unit != nullptr) { 406 if (m_include_support_files) { 407 FileSpecList supporting_files = context.comp_unit->GetSupportFiles(); 408 for (size_t sfiles = 0; sfiles < supporting_files.GetSize(); sfiles++) { 409 const FileSpec &sfile_spec = 410 supporting_files.GetFileSpecAtIndex(sfiles); 411 const char *sfile_file_name = sfile_spec.GetFilename().GetCString(); 412 const char *sfile_dir_name = sfile_spec.GetFilename().GetCString(); 413 bool match = false; 414 if (m_file_name && sfile_file_name && 415 strstr(sfile_file_name, m_file_name) == sfile_file_name) 416 match = true; 417 if (match && m_dir_name && sfile_dir_name && 418 strstr(sfile_dir_name, m_dir_name) != sfile_dir_name) 419 match = false; 420 421 if (match) { 422 m_matching_files.AppendIfUnique(sfile_spec); 423 } 424 } 425 } else { 426 const char *cur_file_name = context.comp_unit->GetFilename().GetCString(); 427 const char *cur_dir_name = context.comp_unit->GetDirectory().GetCString(); 428 429 bool match = false; 430 if (m_file_name && cur_file_name && 431 strstr(cur_file_name, m_file_name) == cur_file_name) 432 match = true; 433 434 if (match && m_dir_name && cur_dir_name && 435 strstr(cur_dir_name, m_dir_name) != cur_dir_name) 436 match = false; 437 438 if (match) { 439 m_matching_files.AppendIfUnique(context.comp_unit); 440 } 441 } 442 } 443 return Searcher::eCallbackReturnContinue; 444 } 445 446 size_t 447 CommandCompletions::SourceFileCompleter::DoCompletion(SearchFilter *filter) { 448 filter->Search(*this); 449 // Now convert the filelist to completions: 450 for (size_t i = 0; i < m_matching_files.GetSize(); i++) { 451 m_matches.AppendString( 452 m_matching_files.GetFileSpecAtIndex(i).GetFilename().GetCString()); 453 } 454 return m_matches.GetSize(); 455 } 456 457 //---------------------------------------------------------------------- 458 // SymbolCompleter 459 //---------------------------------------------------------------------- 460 461 static bool regex_chars(const char comp) { 462 return (comp == '[' || comp == ']' || comp == '(' || comp == ')' || 463 comp == '{' || comp == '}' || comp == '+' || comp == '.' || 464 comp == '*' || comp == '|' || comp == '^' || comp == '$' || 465 comp == '\\' || comp == '?'); 466 } 467 468 CommandCompletions::SymbolCompleter::SymbolCompleter( 469 CommandInterpreter &interpreter, llvm::StringRef completion_str, 470 int match_start_point, int max_return_elements, StringList &matches) 471 : CommandCompletions::Completer(interpreter, completion_str, 472 match_start_point, max_return_elements, 473 matches) { 474 std::string regex_str; 475 if (!completion_str.empty()) { 476 regex_str.append("^"); 477 regex_str.append(completion_str); 478 } else { 479 // Match anything since the completion string is empty 480 regex_str.append("."); 481 } 482 std::string::iterator pos = 483 find_if(regex_str.begin() + 1, regex_str.end(), regex_chars); 484 while (pos < regex_str.end()) { 485 pos = regex_str.insert(pos, '\\'); 486 pos = find_if(pos + 2, regex_str.end(), regex_chars); 487 } 488 m_regex.Compile(regex_str); 489 } 490 491 Searcher::Depth CommandCompletions::SymbolCompleter::GetDepth() { 492 return eDepthModule; 493 } 494 495 Searcher::CallbackReturn CommandCompletions::SymbolCompleter::SearchCallback( 496 SearchFilter &filter, SymbolContext &context, Address *addr, 497 bool complete) { 498 if (context.module_sp) { 499 SymbolContextList sc_list; 500 const bool include_symbols = true; 501 const bool include_inlines = true; 502 const bool append = true; 503 context.module_sp->FindFunctions(m_regex, include_symbols, include_inlines, 504 append, sc_list); 505 506 SymbolContext sc; 507 // Now add the functions & symbols to the list - only add if unique: 508 for (uint32_t i = 0; i < sc_list.GetSize(); i++) { 509 if (sc_list.GetContextAtIndex(i, sc)) { 510 ConstString func_name = sc.GetFunctionName(Mangled::ePreferDemangled); 511 if (!func_name.IsEmpty()) 512 m_match_set.insert(func_name); 513 } 514 } 515 } 516 return Searcher::eCallbackReturnContinue; 517 } 518 519 size_t CommandCompletions::SymbolCompleter::DoCompletion(SearchFilter *filter) { 520 filter->Search(*this); 521 collection::iterator pos = m_match_set.begin(), end = m_match_set.end(); 522 for (pos = m_match_set.begin(); pos != end; pos++) 523 m_matches.AppendString((*pos).GetCString()); 524 525 return m_matches.GetSize(); 526 } 527 528 //---------------------------------------------------------------------- 529 // ModuleCompleter 530 //---------------------------------------------------------------------- 531 CommandCompletions::ModuleCompleter::ModuleCompleter( 532 CommandInterpreter &interpreter, llvm::StringRef completion_str, 533 int match_start_point, int max_return_elements, StringList &matches) 534 : CommandCompletions::Completer(interpreter, completion_str, 535 match_start_point, max_return_elements, 536 matches) { 537 FileSpec partial_spec(m_completion_str, false); 538 m_file_name = partial_spec.GetFilename().GetCString(); 539 m_dir_name = partial_spec.GetDirectory().GetCString(); 540 } 541 542 Searcher::Depth CommandCompletions::ModuleCompleter::GetDepth() { 543 return eDepthModule; 544 } 545 546 Searcher::CallbackReturn CommandCompletions::ModuleCompleter::SearchCallback( 547 SearchFilter &filter, SymbolContext &context, Address *addr, 548 bool complete) { 549 if (context.module_sp) { 550 const char *cur_file_name = 551 context.module_sp->GetFileSpec().GetFilename().GetCString(); 552 const char *cur_dir_name = 553 context.module_sp->GetFileSpec().GetDirectory().GetCString(); 554 555 bool match = false; 556 if (m_file_name && cur_file_name && 557 strstr(cur_file_name, m_file_name) == cur_file_name) 558 match = true; 559 560 if (match && m_dir_name && cur_dir_name && 561 strstr(cur_dir_name, m_dir_name) != cur_dir_name) 562 match = false; 563 564 if (match) { 565 m_matches.AppendString(cur_file_name); 566 } 567 } 568 return Searcher::eCallbackReturnContinue; 569 } 570 571 size_t CommandCompletions::ModuleCompleter::DoCompletion(SearchFilter *filter) { 572 filter->Search(*this); 573 return m_matches.GetSize(); 574 } 575