1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776
|
//===-- CommandCompletions.cpp ----------------------------------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "lldb/lldb-python.h"
// C Includes
#include <sys/stat.h>
#if defined(__APPLE__) || defined(__linux__)
#include <pwd.h>
#endif
// C++ Includes
// Other libraries and framework includes
// Project includes
#include "lldb/Host/FileSpec.h"
#include "lldb/Core/FileSpecList.h"
#include "lldb/Core/PluginManager.h"
#include "lldb/Core/Module.h"
#include "lldb/Interpreter/Args.h"
#include "lldb/Interpreter/CommandCompletions.h"
#include "lldb/Interpreter/CommandInterpreter.h"
#include "lldb/Symbol/CompileUnit.h"
#include "lldb/Symbol/Variable.h"
#include "lldb/Target/Target.h"
#include "lldb/Utility/CleanUp.h"
using namespace lldb_private;
CommandCompletions::CommonCompletionElement
CommandCompletions::g_common_completions[] =
{
{eCustomCompletion, NULL},
{eSourceFileCompletion, CommandCompletions::SourceFiles},
{eDiskFileCompletion, CommandCompletions::DiskFiles},
{eDiskDirectoryCompletion, CommandCompletions::DiskDirectories},
{eSymbolCompletion, CommandCompletions::Symbols},
{eModuleCompletion, CommandCompletions::Modules},
{eSettingsNameCompletion, CommandCompletions::SettingsNames},
{ePlatformPluginCompletion, CommandCompletions::PlatformPluginNames},
{eArchitectureCompletion, CommandCompletions::ArchitectureNames},
{eVariablePathCompletion, CommandCompletions::VariablePath},
{eNoCompletion, NULL} // This one has to be last in the list.
};
bool
CommandCompletions::InvokeCommonCompletionCallbacks
(
CommandInterpreter &interpreter,
uint32_t completion_mask,
const char *completion_str,
int match_start_point,
int max_return_elements,
SearchFilter *searcher,
bool &word_complete,
StringList &matches
)
{
bool handled = false;
if (completion_mask & eCustomCompletion)
return false;
for (int i = 0; ; i++)
{
if (g_common_completions[i].type == eNoCompletion)
break;
else if ((g_common_completions[i].type & completion_mask) == g_common_completions[i].type
&& g_common_completions[i].callback != NULL)
{
handled = true;
g_common_completions[i].callback (interpreter,
completion_str,
match_start_point,
max_return_elements,
searcher,
word_complete,
matches);
}
}
return handled;
}
int
CommandCompletions::SourceFiles
(
CommandInterpreter &interpreter,
const char *partial_file_name,
int match_start_point,
int max_return_elements,
SearchFilter *searcher,
bool &word_complete,
StringList &matches
)
{
word_complete = true;
// Find some way to switch "include support files..."
SourceFileCompleter completer (interpreter,
false,
partial_file_name,
match_start_point,
max_return_elements,
matches);
if (searcher == NULL)
{
lldb::TargetSP target_sp = interpreter.GetDebugger().GetSelectedTarget();
SearchFilter null_searcher (target_sp);
completer.DoCompletion (&null_searcher);
}
else
{
completer.DoCompletion (searcher);
}
return matches.GetSize();
}
typedef struct DiskFilesOrDirectoriesBaton
{
const char *remainder;
char *partial_name_copy;
bool only_directories;
bool *saw_directory;
StringList *matches;
char *end_ptr;
size_t baselen;
} DiskFilesOrDirectoriesBaton;
FileSpec::EnumerateDirectoryResult DiskFilesOrDirectoriesCallback(void *baton, FileSpec::FileType file_type, const FileSpec &spec)
{
const char *name = spec.GetFilename().AsCString();
const DiskFilesOrDirectoriesBaton *parameters = (DiskFilesOrDirectoriesBaton*)baton;
char *end_ptr = parameters->end_ptr;
char *partial_name_copy = parameters->partial_name_copy;
const char *remainder = parameters->remainder;
// Omit ".", ".." and any . files if the match string doesn't start with .
if (name[0] == '.')
{
if (name[1] == '\0')
return FileSpec::eEnumerateDirectoryResultNext;
else if (name[1] == '.' && name[2] == '\0')
return FileSpec::eEnumerateDirectoryResultNext;
else if (remainder[0] != '.')
return FileSpec::eEnumerateDirectoryResultNext;
}
// If we found a directory, we put a "/" at the end of the name.
if (remainder[0] == '\0' || strstr(name, remainder) == name)
{
if (strlen(name) + parameters->baselen >= PATH_MAX)
return FileSpec::eEnumerateDirectoryResultNext;
strcpy(end_ptr, name);
bool isa_directory = false;
if (file_type == FileSpec::eFileTypeDirectory)
isa_directory = true;
else if (file_type == FileSpec::eFileTypeSymbolicLink)
{
struct stat stat_buf;
if ((stat(partial_name_copy, &stat_buf) == 0) && S_ISDIR(stat_buf.st_mode))
isa_directory = true;
}
if (isa_directory)
{
*parameters->saw_directory = true;
size_t len = strlen(parameters->partial_name_copy);
partial_name_copy[len] = '/';
partial_name_copy[len + 1] = '\0';
}
if (parameters->only_directories && !isa_directory)
return FileSpec::eEnumerateDirectoryResultNext;
parameters->matches->AppendString(partial_name_copy);
}
return FileSpec::eEnumerateDirectoryResultNext;
}
static int
DiskFilesOrDirectories
(
const char *partial_file_name,
bool only_directories,
bool &saw_directory,
StringList &matches
)
{
// I'm going to use the "glob" function with GLOB_TILDE for user directory expansion.
// If it is not defined on your host system, you'll need to implement it yourself...
size_t partial_name_len = strlen(partial_file_name);
if (partial_name_len >= PATH_MAX)
return matches.GetSize();
// This copy of the string will be cut up into the directory part, and the remainder. end_ptr
// below will point to the place of the remainder in this string. Then when we've resolved the
// containing directory, and opened it, we'll read the directory contents and overwrite the
// partial_name_copy starting from end_ptr with each of the matches. Thus we will preserve
// the form the user originally typed.
char partial_name_copy[PATH_MAX];
memcpy(partial_name_copy, partial_file_name, partial_name_len);
partial_name_copy[partial_name_len] = '\0';
// We'll need to save a copy of the remainder for comparison, which we do here.
char remainder[PATH_MAX];
// end_ptr will point past the last / in partial_name_copy, or if there is no slash to the beginning of the string.
char *end_ptr;
end_ptr = strrchr(partial_name_copy, '/');
// This will store the resolved form of the containing directory
char containing_part[PATH_MAX];
if (end_ptr == NULL)
{
// There's no directory. If the thing begins with a "~" then this is a bare
// user name.
if (*partial_name_copy == '~')
{
// Nothing here but the user name. We could just put a slash on the end,
// but for completeness sake we'll resolve the user name and only put a slash
// on the end if it exists.
char resolved_username[PATH_MAX];
size_t resolved_username_len = FileSpec::ResolveUsername (partial_name_copy, resolved_username,
sizeof (resolved_username));
// Not sure how this would happen, a username longer than PATH_MAX? Still...
if (resolved_username_len >= sizeof (resolved_username))
return matches.GetSize();
else if (resolved_username_len == 0)
{
// The user name didn't resolve, let's look in the password database for matches.
// The user name database contains duplicates, and is not in alphabetical order, so
// we'll use a set to manage that for us.
FileSpec::ResolvePartialUsername (partial_name_copy, matches);
if (matches.GetSize() > 0)
saw_directory = true;
return matches.GetSize();
}
else
{
//The thing exists, put a '/' on the end, and return it...
// FIXME: complete user names here:
partial_name_copy[partial_name_len] = '/';
partial_name_copy[partial_name_len+1] = '\0';
matches.AppendString(partial_name_copy);
saw_directory = true;
return matches.GetSize();
}
}
else
{
// The containing part is the CWD, and the whole string is the remainder.
containing_part[0] = '.';
containing_part[1] = '\0';
strcpy(remainder, partial_name_copy);
end_ptr = partial_name_copy;
}
}
else
{
if (end_ptr == partial_name_copy)
{
// We're completing a file or directory in the root volume.
containing_part[0] = '/';
containing_part[1] = '\0';
}
else
{
size_t len = end_ptr - partial_name_copy;
memcpy(containing_part, partial_name_copy, len);
containing_part[len] = '\0';
}
// Push end_ptr past the final "/" and set remainder.
end_ptr++;
strcpy(remainder, end_ptr);
}
// Look for a user name in the containing part, and if it's there, resolve it and stick the
// result back into the containing_part:
if (*partial_name_copy == '~')
{
size_t resolved_username_len = FileSpec::ResolveUsername(containing_part,
containing_part,
sizeof (containing_part));
// User name doesn't exist, we're not getting any further...
if (resolved_username_len == 0 || resolved_username_len >= sizeof (containing_part))
return matches.GetSize();
}
// Okay, containing_part is now the directory we want to open and look for files:
size_t baselen = end_ptr - partial_name_copy;
DiskFilesOrDirectoriesBaton parameters;
parameters.remainder = remainder;
parameters.partial_name_copy = partial_name_copy;
parameters.only_directories = only_directories;
parameters.saw_directory = &saw_directory;
parameters.matches = &matches;
parameters.end_ptr = end_ptr;
parameters.baselen = baselen;
FileSpec::EnumerateDirectory(containing_part, true, true, true, DiskFilesOrDirectoriesCallback, ¶meters);
return matches.GetSize();
}
int
CommandCompletions::DiskFiles
(
CommandInterpreter &interpreter,
const char *partial_file_name,
int match_start_point,
int max_return_elements,
SearchFilter *searcher,
bool &word_complete,
StringList &matches
)
{
int ret_val = DiskFilesOrDirectories (partial_file_name,
false,
word_complete,
matches);
word_complete = !word_complete;
return ret_val;
}
int
CommandCompletions::DiskDirectories
(
CommandInterpreter &interpreter,
const char *partial_file_name,
int match_start_point,
int max_return_elements,
SearchFilter *searcher,
bool &word_complete,
StringList &matches
)
{
int ret_val = DiskFilesOrDirectories (partial_file_name,
true,
word_complete,
matches);
word_complete = false;
return ret_val;
}
int
CommandCompletions::Modules
(
CommandInterpreter &interpreter,
const char *partial_file_name,
int match_start_point,
int max_return_elements,
SearchFilter *searcher,
bool &word_complete,
StringList &matches
)
{
word_complete = true;
ModuleCompleter completer (interpreter,
partial_file_name,
match_start_point,
max_return_elements,
matches);
if (searcher == NULL)
{
lldb::TargetSP target_sp = interpreter.GetDebugger().GetSelectedTarget();
SearchFilter null_searcher (target_sp);
completer.DoCompletion (&null_searcher);
}
else
{
completer.DoCompletion (searcher);
}
return matches.GetSize();
}
int
CommandCompletions::Symbols
(
CommandInterpreter &interpreter,
const char *partial_file_name,
int match_start_point,
int max_return_elements,
SearchFilter *searcher,
bool &word_complete,
StringList &matches)
{
word_complete = true;
SymbolCompleter completer (interpreter,
partial_file_name,
match_start_point,
max_return_elements,
matches);
if (searcher == NULL)
{
lldb::TargetSP target_sp = interpreter.GetDebugger().GetSelectedTarget();
SearchFilter null_searcher (target_sp);
completer.DoCompletion (&null_searcher);
}
else
{
completer.DoCompletion (searcher);
}
return matches.GetSize();
}
int
CommandCompletions::SettingsNames (CommandInterpreter &interpreter,
const char *partial_setting_name,
int match_start_point,
int max_return_elements,
SearchFilter *searcher,
bool &word_complete,
StringList &matches)
{
// Cache the full setting name list
static StringList g_property_names;
if (g_property_names.GetSize() == 0)
{
// Generate the full setting name list on demand
lldb::OptionValuePropertiesSP properties_sp (interpreter.GetDebugger().GetValueProperties());
if (properties_sp)
{
StreamString strm;
properties_sp->DumpValue(NULL, strm, OptionValue::eDumpOptionName);
const std::string &str = strm.GetString();
g_property_names.SplitIntoLines(str.c_str(), str.size());
}
}
size_t exact_matches_idx = SIZE_MAX;
const size_t num_matches = g_property_names.AutoComplete (partial_setting_name, matches, exact_matches_idx);
word_complete = exact_matches_idx != SIZE_MAX;
return num_matches;
}
int
CommandCompletions::PlatformPluginNames (CommandInterpreter &interpreter,
const char *partial_name,
int match_start_point,
int max_return_elements,
SearchFilter *searcher,
bool &word_complete,
lldb_private::StringList &matches)
{
const uint32_t num_matches = PluginManager::AutoCompletePlatformName(partial_name, matches);
word_complete = num_matches == 1;
return num_matches;
}
int
CommandCompletions::ArchitectureNames (CommandInterpreter &interpreter,
const char *partial_name,
int match_start_point,
int max_return_elements,
SearchFilter *searcher,
bool &word_complete,
lldb_private::StringList &matches)
{
const uint32_t num_matches = ArchSpec::AutoComplete (partial_name, matches);
word_complete = num_matches == 1;
return num_matches;
}
int
CommandCompletions::VariablePath (CommandInterpreter &interpreter,
const char *partial_name,
int match_start_point,
int max_return_elements,
SearchFilter *searcher,
bool &word_complete,
lldb_private::StringList &matches)
{
return Variable::AutoComplete (interpreter.GetExecutionContext(), partial_name, matches, word_complete);
}
CommandCompletions::Completer::Completer
(
CommandInterpreter &interpreter,
const char *completion_str,
int match_start_point,
int max_return_elements,
StringList &matches
) :
m_interpreter (interpreter),
m_completion_str (completion_str),
m_match_start_point (match_start_point),
m_max_return_elements (max_return_elements),
m_matches (matches)
{
}
CommandCompletions::Completer::~Completer ()
{
}
//----------------------------------------------------------------------
// SourceFileCompleter
//----------------------------------------------------------------------
CommandCompletions::SourceFileCompleter::SourceFileCompleter
(
CommandInterpreter &interpreter,
bool include_support_files,
const char *completion_str,
int match_start_point,
int max_return_elements,
StringList &matches
) :
CommandCompletions::Completer (interpreter, completion_str, match_start_point, max_return_elements, matches),
m_include_support_files (include_support_files),
m_matching_files()
{
FileSpec partial_spec (m_completion_str.c_str(), false);
m_file_name = partial_spec.GetFilename().GetCString();
m_dir_name = partial_spec.GetDirectory().GetCString();
}
Searcher::Depth
CommandCompletions::SourceFileCompleter::GetDepth()
{
return eDepthCompUnit;
}
Searcher::CallbackReturn
CommandCompletions::SourceFileCompleter::SearchCallback (
SearchFilter &filter,
SymbolContext &context,
Address *addr,
bool complete
)
{
if (context.comp_unit != NULL)
{
if (m_include_support_files)
{
FileSpecList supporting_files = context.comp_unit->GetSupportFiles();
for (size_t sfiles = 0; sfiles < supporting_files.GetSize(); sfiles++)
{
const FileSpec &sfile_spec = supporting_files.GetFileSpecAtIndex(sfiles);
const char *sfile_file_name = sfile_spec.GetFilename().GetCString();
const char *sfile_dir_name = sfile_spec.GetFilename().GetCString();
bool match = false;
if (m_file_name && sfile_file_name
&& strstr (sfile_file_name, m_file_name) == sfile_file_name)
match = true;
if (match && m_dir_name && sfile_dir_name
&& strstr (sfile_dir_name, m_dir_name) != sfile_dir_name)
match = false;
if (match)
{
m_matching_files.AppendIfUnique(sfile_spec);
}
}
}
else
{
const char *cur_file_name = context.comp_unit->GetFilename().GetCString();
const char *cur_dir_name = context.comp_unit->GetDirectory().GetCString();
bool match = false;
if (m_file_name && cur_file_name
&& strstr (cur_file_name, m_file_name) == cur_file_name)
match = true;
if (match && m_dir_name && cur_dir_name
&& strstr (cur_dir_name, m_dir_name) != cur_dir_name)
match = false;
if (match)
{
m_matching_files.AppendIfUnique(context.comp_unit);
}
}
}
return Searcher::eCallbackReturnContinue;
}
size_t
CommandCompletions::SourceFileCompleter::DoCompletion (SearchFilter *filter)
{
filter->Search (*this);
// Now convert the filelist to completions:
for (size_t i = 0; i < m_matching_files.GetSize(); i++)
{
m_matches.AppendString (m_matching_files.GetFileSpecAtIndex(i).GetFilename().GetCString());
}
return m_matches.GetSize();
}
//----------------------------------------------------------------------
// SymbolCompleter
//----------------------------------------------------------------------
static bool
regex_chars (const char comp)
{
if (comp == '[' || comp == ']' ||
comp == '(' || comp == ')' ||
comp == '{' || comp == '}' ||
comp == '+' ||
comp == '.' ||
comp == '*' ||
comp == '|' ||
comp == '^' ||
comp == '$' ||
comp == '\\' ||
comp == '?')
return true;
else
return false;
}
CommandCompletions::SymbolCompleter::SymbolCompleter
(
CommandInterpreter &interpreter,
const char *completion_str,
int match_start_point,
int max_return_elements,
StringList &matches
) :
CommandCompletions::Completer (interpreter, completion_str, match_start_point, max_return_elements, matches)
{
std::string regex_str;
if (completion_str && completion_str[0])
{
regex_str.append("^");
regex_str.append(completion_str);
}
else
{
// Match anything since the completion string is empty
regex_str.append(".");
}
std::string::iterator pos = find_if(regex_str.begin() + 1, regex_str.end(), regex_chars);
while (pos < regex_str.end())
{
pos = regex_str.insert(pos, '\\');
pos = find_if(pos + 2, regex_str.end(), regex_chars);
}
m_regex.Compile(regex_str.c_str());
}
Searcher::Depth
CommandCompletions::SymbolCompleter::GetDepth()
{
return eDepthModule;
}
Searcher::CallbackReturn
CommandCompletions::SymbolCompleter::SearchCallback (
SearchFilter &filter,
SymbolContext &context,
Address *addr,
bool complete
)
{
if (context.module_sp)
{
SymbolContextList sc_list;
const bool include_symbols = true;
const bool include_inlines = true;
const bool append = true;
context.module_sp->FindFunctions (m_regex, include_symbols, include_inlines, append, sc_list);
SymbolContext sc;
// Now add the functions & symbols to the list - only add if unique:
for (uint32_t i = 0; i < sc_list.GetSize(); i++)
{
if (sc_list.GetContextAtIndex(i, sc))
{
ConstString func_name = sc.GetFunctionName(Mangled::ePreferDemangled);
if (!func_name.IsEmpty())
m_match_set.insert (func_name);
}
}
}
return Searcher::eCallbackReturnContinue;
}
size_t
CommandCompletions::SymbolCompleter::DoCompletion (SearchFilter *filter)
{
filter->Search (*this);
collection::iterator pos = m_match_set.begin(), end = m_match_set.end();
for (pos = m_match_set.begin(); pos != end; pos++)
m_matches.AppendString((*pos).GetCString());
return m_matches.GetSize();
}
//----------------------------------------------------------------------
// ModuleCompleter
//----------------------------------------------------------------------
CommandCompletions::ModuleCompleter::ModuleCompleter
(
CommandInterpreter &interpreter,
const char *completion_str,
int match_start_point,
int max_return_elements,
StringList &matches
) :
CommandCompletions::Completer (interpreter, completion_str, match_start_point, max_return_elements, matches)
{
FileSpec partial_spec (m_completion_str.c_str(), false);
m_file_name = partial_spec.GetFilename().GetCString();
m_dir_name = partial_spec.GetDirectory().GetCString();
}
Searcher::Depth
CommandCompletions::ModuleCompleter::GetDepth()
{
return eDepthModule;
}
Searcher::CallbackReturn
CommandCompletions::ModuleCompleter::SearchCallback (
SearchFilter &filter,
SymbolContext &context,
Address *addr,
bool complete
)
{
if (context.module_sp)
{
const char *cur_file_name = context.module_sp->GetFileSpec().GetFilename().GetCString();
const char *cur_dir_name = context.module_sp->GetFileSpec().GetDirectory().GetCString();
bool match = false;
if (m_file_name && cur_file_name
&& strstr (cur_file_name, m_file_name) == cur_file_name)
match = true;
if (match && m_dir_name && cur_dir_name
&& strstr (cur_dir_name, m_dir_name) != cur_dir_name)
match = false;
if (match)
{
m_matches.AppendString (cur_file_name);
}
}
return Searcher::eCallbackReturnContinue;
}
size_t
CommandCompletions::ModuleCompleter::DoCompletion (SearchFilter *filter)
{
filter->Search (*this);
return m_matches.GetSize();
}
|