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