LLDB mainline
CommandCompletions.cpp
Go to the documentation of this file.
1//===-- CommandCompletions.cpp --------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "llvm/ADT/STLExtras.h"
10#include "llvm/ADT/SmallString.h"
11#include "llvm/ADT/StringRef.h"
12#include "llvm/ADT/StringSet.h"
13
15#include "lldb/Core/Module.h"
27#include "lldb/Target/Process.h"
29#include "lldb/Target/Thread.h"
34
35#include "llvm/Support/FileSystem.h"
36#include "llvm/Support/Path.h"
37
38using namespace lldb_private;
39
40// This is the command completion callback that is used to complete the
41// argument of the option it is bound to (in the OptionDefinition table
42// below).
43typedef void (*CompletionCallback)(CommandInterpreter &interpreter,
44 CompletionRequest &request,
45 // A search filter to limit the search...
47
52
54 CommandInterpreter &interpreter, uint32_t completion_mask,
55 CompletionRequest &request, SearchFilter *searcher) {
56 bool handled = false;
57
58 const CommonCompletionElement common_completions[] = {
59 {lldb::eNoCompletion, nullptr},
92 nullptr} // This one has to be last in the list.
93 };
94
95 for (int i = 0; request.ShouldAddCompletions(); i++) {
96 if (common_completions[i].type == lldb::eTerminatorCompletion)
97 break;
98 else if ((common_completions[i].type & completion_mask) ==
99 common_completions[i].type &&
100 common_completions[i].callback != nullptr) {
101 handled = true;
102 common_completions[i].callback(interpreter, request, searcher);
103 }
104 }
105 return handled;
106}
107
108namespace {
109// The Completer class is a convenient base class for building searchers that
110// go along with the SearchFilter passed to the standard Completer functions.
111class Completer : public Searcher {
112public:
113 Completer(CommandInterpreter &interpreter, CompletionRequest &request)
114 : m_interpreter(interpreter), m_request(request) {}
115
116 ~Completer() override = default;
117
118 CallbackReturn SearchCallback(SearchFilter &filter, SymbolContext &context,
119 Address *addr) override = 0;
120
121 lldb::SearchDepth GetDepth() override = 0;
122
123 virtual void DoCompletion(SearchFilter *filter) = 0;
124
125protected:
126 CommandInterpreter &m_interpreter;
127 CompletionRequest &m_request;
128
129private:
130 Completer(const Completer &) = delete;
131 const Completer &operator=(const Completer &) = delete;
132};
133} // namespace
134
135// SourceFileCompleter implements the source file completer
136namespace {
137class SourceFileCompleter : public Completer {
138public:
139 SourceFileCompleter(CommandInterpreter &interpreter,
140 CompletionRequest &request)
141 : Completer(interpreter, request) {
142 FileSpec partial_spec(m_request.GetCursorArgumentPrefix());
143 m_file_name = partial_spec.GetFilename().GetCString();
144 m_dir_name = partial_spec.GetDirectory().GetCString();
145 }
146
147 lldb::SearchDepth GetDepth() override { return lldb::eSearchDepthCompUnit; }
148
149 Searcher::CallbackReturn SearchCallback(SearchFilter &filter,
150 SymbolContext &context,
151 Address *addr) override {
152 if (context.comp_unit != nullptr) {
153 const char *cur_file_name =
155 const char *cur_dir_name =
157
158 bool match = false;
159 if (m_file_name && cur_file_name &&
160 strstr(cur_file_name, m_file_name) == cur_file_name)
161 match = true;
162
163 if (match && m_dir_name && cur_dir_name &&
164 strstr(cur_dir_name, m_dir_name) != cur_dir_name)
165 match = false;
166
167 if (match) {
168 m_matching_files.AppendIfUnique(context.comp_unit->GetPrimaryFile());
169 }
170 }
171 return m_matching_files.GetSize() >= m_request.GetMaxNumberOfCompletionsToAdd()
174 }
175
176 void DoCompletion(SearchFilter *filter) override {
177 filter->Search(*this);
178 // Now convert the filelist to completions:
179 for (size_t i = 0; i < m_matching_files.GetSize(); i++) {
180 m_request.AddCompletion(
181 m_matching_files.GetFileSpecAtIndex(i).GetFilename().GetCString());
182 }
183 }
184
185private:
186 FileSpecList m_matching_files;
187 const char *m_file_name;
188 const char *m_dir_name;
189
190 SourceFileCompleter(const SourceFileCompleter &) = delete;
191 const SourceFileCompleter &operator=(const SourceFileCompleter &) = delete;
192};
193} // namespace
194
195static bool regex_chars(const char comp) {
196 return llvm::StringRef("[](){}+.*|^$\\?").contains(comp);
197}
198
199namespace {
200class SymbolCompleter : public Completer {
201
202public:
203 SymbolCompleter(CommandInterpreter &interpreter, CompletionRequest &request)
204 : Completer(interpreter, request) {
205 std::string regex_str;
206 if (!m_request.GetCursorArgumentPrefix().empty()) {
207 regex_str.append("^");
208 regex_str.append(std::string(m_request.GetCursorArgumentPrefix()));
209 } else {
210 // Match anything since the completion string is empty
211 regex_str.append(".");
212 }
213 std::string::iterator pos =
214 find_if(regex_str.begin() + 1, regex_str.end(), regex_chars);
215 while (pos < regex_str.end()) {
216 pos = regex_str.insert(pos, '\\');
217 pos = find_if(pos + 2, regex_str.end(), regex_chars);
218 }
219 m_regex = RegularExpression(regex_str);
220 }
221
222 lldb::SearchDepth GetDepth() override { return lldb::eSearchDepthModule; }
223
224 Searcher::CallbackReturn SearchCallback(SearchFilter &filter,
225 SymbolContext &context,
226 Address *addr) override {
227 if (context.module_sp) {
228 SymbolContextList sc_list;
229 ModuleFunctionSearchOptions function_options;
230 function_options.include_symbols = true;
231 function_options.include_inlines = true;
232 context.module_sp->FindFunctions(m_regex, function_options, sc_list);
233
234 // Now add the functions & symbols to the list - only add if unique:
235 for (const SymbolContext &sc : sc_list) {
236 if (m_match_set.size() >= m_request.GetMaxNumberOfCompletionsToAdd())
237 break;
238
239 ConstString func_name = sc.GetFunctionName(Mangled::ePreferDemangled);
240 // Ensure that the function name matches the regex. This is more than
241 // a sanity check. It is possible that the demangled function name
242 // does not start with the prefix, for example when it's in an
243 // anonymous namespace.
244 if (!func_name.IsEmpty() && m_regex.Execute(func_name.GetStringRef()))
245 m_match_set.insert(func_name);
246 }
247 }
248 return m_match_set.size() >= m_request.GetMaxNumberOfCompletionsToAdd()
251 }
252
253 void DoCompletion(SearchFilter *filter) override {
254 filter->Search(*this);
255 collection::iterator pos = m_match_set.begin(), end = m_match_set.end();
256 for (pos = m_match_set.begin(); pos != end; pos++)
257 m_request.AddCompletion((*pos).GetCString());
258 }
259
260private:
261 RegularExpression m_regex;
262 typedef std::set<ConstString> collection;
263 collection m_match_set;
264
265 SymbolCompleter(const SymbolCompleter &) = delete;
266 const SymbolCompleter &operator=(const SymbolCompleter &) = delete;
267};
268} // namespace
269
270namespace {
271class ModuleCompleter : public Completer {
272public:
273 ModuleCompleter(CommandInterpreter &interpreter, CompletionRequest &request)
274 : Completer(interpreter, request) {
275 llvm::StringRef request_str = m_request.GetCursorArgumentPrefix();
276 // We can match the full path, or the file name only. The full match will be
277 // attempted always, the file name match only if the request does not
278 // contain a path separator.
279
280 // Preserve both the path as spelled by the user (used for completion) and
281 // the canonical version (used for matching).
282 m_spelled_path = request_str;
283 m_canonical_path = FileSpec(m_spelled_path).GetPath();
284 if (!m_spelled_path.empty() &&
285 llvm::sys::path::is_separator(m_spelled_path.back()) &&
286 !llvm::StringRef(m_canonical_path).ends_with(m_spelled_path.back())) {
287 m_canonical_path += m_spelled_path.back();
288 }
289
290 if (llvm::find_if(request_str, [](char c) {
291 return llvm::sys::path::is_separator(c);
292 }) == request_str.end())
293 m_file_name = request_str;
294 }
295
296 lldb::SearchDepth GetDepth() override { return lldb::eSearchDepthModule; }
297
298 Searcher::CallbackReturn SearchCallback(SearchFilter &filter,
299 SymbolContext &context,
300 Address *addr) override {
301 if (context.module_sp) {
302 // Attempt a full path match.
303 std::string cur_path = context.module_sp->GetFileSpec().GetPath();
304 llvm::StringRef cur_path_view = cur_path;
305 if (cur_path_view.consume_front(m_canonical_path))
306 m_request.AddCompletion((m_spelled_path + cur_path_view).str());
307
308 // And a file name match.
309 if (m_file_name) {
310 llvm::StringRef cur_file_name =
311 context.module_sp->GetFileSpec().GetFilename().GetStringRef();
312 if (cur_file_name.starts_with(*m_file_name))
313 m_request.AddCompletion(cur_file_name);
314 }
315 }
316 return m_request.ShouldAddCompletions() ? Searcher::eCallbackReturnContinue
318 }
319
320 void DoCompletion(SearchFilter *filter) override { filter->Search(*this); }
321
322private:
323 std::optional<llvm::StringRef> m_file_name;
324 llvm::StringRef m_spelled_path;
325 std::string m_canonical_path;
326
327 ModuleCompleter(const ModuleCompleter &) = delete;
328 const ModuleCompleter &operator=(const ModuleCompleter &) = delete;
329};
330} // namespace
331
333 CompletionRequest &request,
334 SearchFilter *searcher) {
335 SourceFileCompleter completer(interpreter, request);
336
337 if (searcher == nullptr) {
338 lldb::TargetSP target_sp = interpreter.GetDebugger().GetSelectedTarget();
339 SearchFilterForUnconstrainedSearches null_searcher(target_sp);
340 completer.DoCompletion(&null_searcher);
341 } else {
342 completer.DoCompletion(searcher);
343 }
344}
345
346static void DiskFilesOrDirectories(const llvm::Twine &partial_name,
347 bool only_directories,
348 CompletionRequest &request,
349 TildeExpressionResolver &Resolver) {
350 llvm::SmallString<256> CompletionBuffer;
351 llvm::SmallString<256> Storage;
352 partial_name.toVector(CompletionBuffer);
353
354 if (CompletionBuffer.size() >= PATH_MAX)
355 return;
356
357 namespace path = llvm::sys::path;
358
359 llvm::StringRef SearchDir;
360 llvm::StringRef PartialItem;
361
362 if (CompletionBuffer.starts_with("~")) {
363 llvm::StringRef Buffer = CompletionBuffer;
364 size_t FirstSep =
365 Buffer.find_if([](char c) { return path::is_separator(c); });
366
367 llvm::StringRef Username = Buffer.take_front(FirstSep);
368 llvm::StringRef Remainder;
369 if (FirstSep != llvm::StringRef::npos)
370 Remainder = Buffer.drop_front(FirstSep + 1);
371
372 llvm::SmallString<256> Resolved;
373 if (!Resolver.ResolveExact(Username, Resolved)) {
374 // We couldn't resolve it as a full username. If there were no slashes
375 // then this might be a partial username. We try to resolve it as such
376 // but after that, we're done regardless of any matches.
377 if (FirstSep == llvm::StringRef::npos) {
378 llvm::StringSet<> MatchSet;
379 Resolver.ResolvePartial(Username, MatchSet);
380 for (const auto &S : MatchSet) {
381 Resolved = S.getKey();
382 path::append(Resolved, path::get_separator());
383 request.AddCompletion(Resolved, "", CompletionMode::Partial);
384 }
385 }
386 return;
387 }
388
389 // If there was no trailing slash, then we're done as soon as we resolve
390 // the expression to the correct directory. Otherwise we need to continue
391 // looking for matches within that directory.
392 if (FirstSep == llvm::StringRef::npos) {
393 // Make sure it ends with a separator.
394 path::append(CompletionBuffer, path::get_separator());
395 request.AddCompletion(CompletionBuffer, "", CompletionMode::Partial);
396 return;
397 }
398
399 // We want to keep the form the user typed, so we special case this to
400 // search in the fully resolved directory, but CompletionBuffer keeps the
401 // unmodified form that the user typed.
402 Storage = Resolved;
403 llvm::StringRef RemainderDir = path::parent_path(Remainder);
404 if (!RemainderDir.empty()) {
405 // Append the remaining path to the resolved directory.
406 Storage.append(path::get_separator());
407 Storage.append(RemainderDir);
408 }
409 SearchDir = Storage;
410 } else if (CompletionBuffer == path::root_directory(CompletionBuffer)) {
411 SearchDir = CompletionBuffer;
412 } else {
413 SearchDir = path::parent_path(CompletionBuffer);
414 }
415
416 size_t FullPrefixLen = CompletionBuffer.size();
417
418 PartialItem = path::filename(CompletionBuffer);
419
420 // path::filename() will return "." when the passed path ends with a
421 // directory separator or the separator when passed the disk root directory.
422 // We have to filter those out, but only when the "." doesn't come from the
423 // completion request itself.
424 if ((PartialItem == "." || PartialItem == path::get_separator()) &&
425 path::is_separator(CompletionBuffer.back()))
426 PartialItem = llvm::StringRef();
427
428 if (SearchDir.empty()) {
429 llvm::sys::fs::current_path(Storage);
430 SearchDir = Storage;
431 }
432 assert(!PartialItem.contains(path::get_separator()));
433
434 // SearchDir now contains the directory to search in, and Prefix contains the
435 // text we want to match against items in that directory.
436
438 std::error_code EC;
439 llvm::vfs::directory_iterator Iter = fs.DirBegin(SearchDir, EC);
440 llvm::vfs::directory_iterator End;
441 for (; Iter != End && !EC && request.ShouldAddCompletions();
442 Iter.increment(EC)) {
443 auto &Entry = *Iter;
444 llvm::ErrorOr<llvm::vfs::Status> Status = fs.GetStatus(Entry.path());
445
446 if (!Status)
447 continue;
448
449 auto Name = path::filename(Entry.path());
450
451 // Omit ".", ".."
452 if (Name == "." || Name == ".." || !Name.starts_with(PartialItem))
453 continue;
454
455 bool is_dir = Status->isDirectory();
456
457 // If it's a symlink, then we treat it as a directory as long as the target
458 // is a directory.
459 if (Status->isSymlink()) {
460 FileSpec symlink_filespec(Entry.path());
461 FileSpec resolved_filespec;
462 auto error = fs.ResolveSymbolicLink(symlink_filespec, resolved_filespec);
463 if (error.Success())
464 is_dir = fs.IsDirectory(symlink_filespec);
465 }
466
467 if (only_directories && !is_dir)
468 continue;
469
470 // Shrink it back down so that it just has the original prefix the user
471 // typed and remove the part of the name which is common to the located
472 // item and what the user typed.
473 CompletionBuffer.resize(FullPrefixLen);
474 Name = Name.drop_front(PartialItem.size());
475 CompletionBuffer.append(Name);
476
477 if (is_dir) {
478 path::append(CompletionBuffer, path::get_separator());
479 }
480
481 CompletionMode mode =
483 request.AddCompletion(CompletionBuffer, "", mode);
484 }
485}
486
487static void DiskFilesOrDirectories(const llvm::Twine &partial_name,
488 bool only_directories, StringList &matches,
489 TildeExpressionResolver &Resolver) {
490 CompletionResult result;
491 std::string partial_name_str = partial_name.str();
492 CompletionRequest request(partial_name_str, partial_name_str.size(), result);
493 DiskFilesOrDirectories(partial_name, only_directories, request, Resolver);
494 result.GetMatches(matches);
495}
496
498 bool only_directories) {
500 DiskFilesOrDirectories(request.GetCursorArgumentPrefix(), only_directories,
501 request, resolver);
502}
503
505 CompletionRequest &request,
506 SearchFilter *searcher) {
507 DiskFilesOrDirectories(request, /*only_dirs*/ false);
508}
509
510void CommandCompletions::DiskFiles(const llvm::Twine &partial_file_name,
511 StringList &matches,
512 TildeExpressionResolver &Resolver) {
513 DiskFilesOrDirectories(partial_file_name, false, matches, Resolver);
514}
515
517 CompletionRequest &request,
518 SearchFilter *searcher) {
519 DiskFilesOrDirectories(request, /*only_dirs*/ true);
520}
521
522void CommandCompletions::DiskDirectories(const llvm::Twine &partial_file_name,
523 StringList &matches,
524 TildeExpressionResolver &Resolver) {
525 DiskFilesOrDirectories(partial_file_name, true, matches, Resolver);
526}
527
529 CompletionRequest &request,
530 SearchFilter *searcher) {
531 lldb::PlatformSP platform_sp =
533 if (platform_sp)
534 platform_sp->AutoCompleteDiskFileOrDirectory(request, false);
535}
536
538 CompletionRequest &request,
539 SearchFilter *searcher) {
540 lldb::PlatformSP platform_sp =
542 if (platform_sp)
543 platform_sp->AutoCompleteDiskFileOrDirectory(request, true);
544}
545
547 CompletionRequest &request,
548 SearchFilter *searcher) {
549 ModuleCompleter completer(interpreter, request);
550
551 if (searcher == nullptr) {
552 lldb::TargetSP target_sp = interpreter.GetDebugger().GetSelectedTarget();
553 SearchFilterForUnconstrainedSearches null_searcher(target_sp);
554 completer.DoCompletion(&null_searcher);
555 } else {
556 completer.DoCompletion(searcher);
557 }
558}
559
561 CompletionRequest &request,
562 SearchFilter *searcher) {
563 const ExecutionContext &exe_ctx = interpreter.GetExecutionContext();
564 if (!exe_ctx.HasTargetScope())
565 return;
566
567 exe_ctx.GetTargetPtr()->GetImages().ForEach(
568 [&request](const lldb::ModuleSP &module) {
569 StreamString strm;
570 module->GetDescription(strm.AsRawOstream(),
571 lldb::eDescriptionLevelInitial);
572 request.TryCompleteCurrentArg(module->GetUUID().GetAsString(),
573 strm.GetString());
575 });
576}
577
579 CompletionRequest &request,
580 SearchFilter *searcher) {
581 SymbolCompleter completer(interpreter, request);
582
583 if (searcher == nullptr) {
584 lldb::TargetSP target_sp = interpreter.GetDebugger().GetSelectedTarget();
585 SearchFilterForUnconstrainedSearches null_searcher(target_sp);
586 completer.DoCompletion(&null_searcher);
587 } else {
588 completer.DoCompletion(searcher);
589 }
590}
591
593 CompletionRequest &request,
594 SearchFilter *searcher) {
595 // Cache the full setting name list
596 static StringList g_property_names;
597 if (g_property_names.GetSize() == 0) {
598 // Generate the full setting name list on demand
599 lldb::OptionValuePropertiesSP properties_sp(
600 interpreter.GetDebugger().GetValueProperties());
601 if (properties_sp) {
602 StreamString strm;
603 properties_sp->DumpValue(nullptr, strm, OptionValue::eDumpOptionName);
604 const std::string &str = std::string(strm.GetString());
605 g_property_names.SplitIntoLines(str.c_str(), str.size());
606 }
607 }
608
609 for (const std::string &s : g_property_names)
610 request.TryCompleteCurrentArg(s);
611}
612
619
621 CompletionRequest &request,
622 SearchFilter *searcher) {
623 ArchSpec::AutoComplete(request);
624}
625
627 CompletionRequest &request,
628 SearchFilter *searcher) {
629 Variable::AutoComplete(interpreter.GetExecutionContext(), request);
630}
631
633 CompletionRequest &request,
634 SearchFilter *searcher) {
635 std::string reg_prefix;
636 if (request.GetCursorArgumentPrefix().starts_with("$"))
637 reg_prefix = "$";
638
639 RegisterContext *reg_ctx =
641 if (!reg_ctx)
642 return;
643
644 const size_t reg_num = reg_ctx->GetRegisterCount();
645 for (size_t reg_idx = 0; reg_idx < reg_num; ++reg_idx) {
646 const RegisterInfo *reg_info = reg_ctx->GetRegisterInfoAtIndex(reg_idx);
647 request.TryCompleteCurrentArg(reg_prefix + reg_info->name,
648 reg_info->alt_name);
649 }
650}
651
653 CompletionRequest &request,
654 SearchFilter *searcher) {
655 lldb::TargetSP target = interpreter.GetDebugger().GetSelectedTarget();
656 if (!target)
657 return;
658
659 const BreakpointList &breakpoints = target->GetBreakpointList();
660
661 std::unique_lock<std::recursive_mutex> lock;
662 target->GetBreakpointList().GetListMutex(lock);
663
664 size_t num_breakpoints = breakpoints.GetSize();
665 if (num_breakpoints == 0)
666 return;
667
668 for (size_t i = 0; i < num_breakpoints; ++i) {
669 lldb::BreakpointSP bp = breakpoints.GetBreakpointAtIndex(i);
670
671 StreamString s;
672 bp->GetDescription(&s, lldb::eDescriptionLevelBrief);
673 llvm::StringRef bp_info = s.GetString();
674
675 const size_t colon_pos = bp_info.find_first_of(':');
676 if (colon_pos != llvm::StringRef::npos)
677 bp_info = bp_info.drop_front(colon_pos + 2);
678
679 request.TryCompleteCurrentArg(std::to_string(bp->GetID()), bp_info);
680 }
681}
682
684 CompletionRequest &request,
685 SearchFilter *searcher) {
686 lldb::TargetSP target = interpreter.GetDebugger().GetSelectedTarget();
687 if (!target)
688 return;
689
690 std::vector<std::string> name_list;
691 target->GetBreakpointNames(name_list);
692
693 for (const std::string &name : name_list)
694 request.TryCompleteCurrentArg(name);
695}
696
704 CompletionRequest &request,
705 SearchFilter *searcher) {
706 // Currently the only valid options for disassemble -F are default, and for
707 // Intel architectures, att and intel.
708 static const char *flavors[] = {"default", "att", "intel"};
709 for (const char *flavor : flavors) {
710 request.TryCompleteCurrentArg(flavor);
711 }
712}
713
715 CompletionRequest &request,
716 SearchFilter *searcher) {
717 lldb::PlatformSP platform_sp(interpreter.GetPlatform(true));
718 if (!platform_sp)
719 return;
720 ProcessInstanceInfoList process_infos;
721 ProcessInstanceInfoMatch match_info;
722 platform_sp->FindProcesses(match_info, process_infos);
723 for (const ProcessInstanceInfo &info : process_infos)
724 request.TryCompleteCurrentArg(std::to_string(info.GetProcessID()),
725 info.GetNameAsStringRef());
726}
727
729 CompletionRequest &request,
730 SearchFilter *searcher) {
731 lldb::PlatformSP platform_sp(interpreter.GetPlatform(true));
732 if (!platform_sp)
733 return;
734 ProcessInstanceInfoList process_infos;
735 ProcessInstanceInfoMatch match_info;
736 platform_sp->FindProcesses(match_info, process_infos);
737 for (const ProcessInstanceInfo &info : process_infos)
738 request.TryCompleteCurrentArg(info.GetNameAsStringRef());
739}
740
742 CompletionRequest &request,
743 SearchFilter *searcher) {
744 for (int bit :
745 Language::GetLanguagesSupportingTypeSystems().bitvector.set_bits()) {
746 request.TryCompleteCurrentArg(
748 }
749}
750
752 CompletionRequest &request,
753 SearchFilter *searcher) {
754 const ExecutionContext &exe_ctx = interpreter.GetExecutionContext();
755 if (!exe_ctx.HasProcessScope())
756 return;
757
758 lldb::ThreadSP thread_sp = exe_ctx.GetThreadSP();
759 Debugger &dbg = interpreter.GetDebugger();
760 const uint32_t frame_num = thread_sp->GetStackFrameCount();
761 for (uint32_t i = 0; i < frame_num; ++i) {
762 lldb::StackFrameSP frame_sp = thread_sp->GetStackFrameAtIndex(i);
763 StreamString strm;
764 // Dumping frames can be slow, allow interruption.
765 if (INTERRUPT_REQUESTED(dbg, "Interrupted in frame completion"))
766 break;
767 frame_sp->Dump(&strm, false, true);
768 request.TryCompleteCurrentArg(std::to_string(i), strm.GetString());
769 }
770}
771
773 CompletionRequest &request,
774 SearchFilter *searcher) {
775 const lldb::TargetSP target_sp =
776 interpreter.GetExecutionContext().GetTargetSP();
777 if (!target_sp)
778 return;
779
780 const size_t num = target_sp->GetNumStopHooks();
781 for (size_t idx = 0; idx < num; ++idx) {
782 StreamString strm;
783 // The value 11 is an offset to make the completion description looks
784 // neater.
785 strm.SetIndentLevel(11);
786 const Target::StopHookSP stophook_sp = target_sp->GetStopHookAtIndex(idx);
787 stophook_sp->GetDescription(strm, lldb::eDescriptionLevelInitial);
788 request.TryCompleteCurrentArg(std::to_string(stophook_sp->GetID()),
789 strm.GetString());
790 }
791}
792
794 CompletionRequest &request,
795 SearchFilter *searcher) {
796 const ExecutionContext &exe_ctx = interpreter.GetExecutionContext();
797 if (!exe_ctx.HasProcessScope())
798 return;
799
800 ThreadList &threads = exe_ctx.GetProcessPtr()->GetThreadList();
801 lldb::ThreadSP thread_sp;
802 for (uint32_t idx = 0; (thread_sp = threads.GetThreadAtIndex(idx)); ++idx) {
803 StreamString strm;
804 thread_sp->GetStatus(strm, 0, 1, 1, true, /*show_hidden*/ true);
805 request.TryCompleteCurrentArg(std::to_string(thread_sp->GetIndexID()),
806 strm.GetString());
807 }
808}
809
811 CompletionRequest &request,
812 SearchFilter *searcher) {
813 const ExecutionContext &exe_ctx = interpreter.GetExecutionContext();
814 if (!exe_ctx.HasTargetScope())
815 return;
816
817 const WatchpointList &wp_list = exe_ctx.GetTargetPtr()->GetWatchpointList();
818 for (lldb::WatchpointSP wp_sp : wp_list.Watchpoints()) {
819 StreamString strm;
820 wp_sp->Dump(&strm);
821 request.TryCompleteCurrentArg(std::to_string(wp_sp->GetID()),
822 strm.GetString());
823 }
824}
825
827 CompletionRequest &request,
828 SearchFilter *searcher) {
830 [&request](const lldb::TypeCategoryImplSP &category_sp) {
831 request.TryCompleteCurrentArg(category_sp->GetName(),
832 category_sp->GetDescription());
833 return true;
834 });
835}
836
838 CompletionRequest &request,
839 SearchFilter *searcher) {
840 const ExecutionContext &exe_ctx = interpreter.GetExecutionContext();
841 if (!exe_ctx.HasProcessScope())
842 return;
843
844 ThreadList &threads = exe_ctx.GetProcessPtr()->GetThreadList();
845 lldb::ThreadSP thread_sp;
846 for (uint32_t idx = 0; (thread_sp = threads.GetThreadAtIndex(idx)); ++idx) {
847 StreamString strm;
848 thread_sp->GetStatus(strm, 0, 1, 1, true, /*show_hidden*/ true);
849 request.TryCompleteCurrentArg(std::to_string(thread_sp->GetID()),
850 strm.GetString());
851 }
852}
853
860
862 CommandInterpreter &interpreter, CompletionRequest &request,
863 OptionElementVector &opt_element_vector) {
864 // The only arguments constitute a command path, however, there might be
865 // options interspersed among the arguments, and we need to skip those. Do that
866 // by copying the args vector, and just dropping all the option bits:
867 Args args = request.GetParsedLine();
868 std::vector<size_t> to_delete;
869 for (auto &elem : opt_element_vector) {
870 to_delete.push_back(elem.opt_pos);
871 if (elem.opt_arg_pos != 0)
872 to_delete.push_back(elem.opt_arg_pos);
873 }
874 sort(to_delete.begin(), to_delete.end(), std::greater<size_t>());
875 for (size_t idx : to_delete)
876 args.DeleteArgumentAtIndex(idx);
877
878 // At this point, we should only have args, so now lookup the command up to
879 // the cursor element.
880
881 // There's nothing here but options. It doesn't seem very useful here to
882 // dump all the commands, so just return.
883 size_t num_args = args.GetArgumentCount();
884 if (num_args == 0)
885 return;
886
887 // There's just one argument, so we should complete its name:
888 StringList matches;
889 if (num_args == 1) {
890 interpreter.GetUserCommandObject(args.GetArgumentAtIndex(0), &matches,
891 nullptr);
892 request.AddCompletions(matches);
893 return;
894 }
895
896 // There was more than one path element, lets find the containing command:
899 interpreter.VerifyUserMultiwordCmdPath(args, true, error);
900
901 // Something was wrong somewhere along the path, but I don't think there's
902 // a good way to go back and fill in the missing elements:
903 if (error.Fail())
904 return;
905
906 // This should never happen. We already handled the case of one argument
907 // above, and we can only get Success & nullptr back if there's a one-word
908 // leaf.
909 assert(mwc != nullptr);
910
911 mwc->GetSubcommandObject(args.GetArgumentAtIndex(num_args - 1), &matches);
912 if (matches.GetSize() == 0)
913 return;
914
915 request.AddCompletions(matches);
916}
static void DiskFilesOrDirectories(const llvm::Twine &partial_name, bool only_directories, CompletionRequest &request, TildeExpressionResolver &Resolver)
static bool regex_chars(const char comp)
void(* CompletionCallback)(CommandInterpreter &interpreter, CompletionRequest &request, lldb_private::SearchFilter *searcher)
static llvm::raw_ostream & error(Stream &strm)
#define INTERRUPT_REQUESTED(debugger,...)
This handy define will keep you from having to generate a report for the interruption by hand.
Definition Debugger.h:458
FormatEntity::Entry Entry
A section + offset based address class.
Definition Address.h:62
static void AutoComplete(CompletionRequest &request)
Definition ArchSpec.cpp:280
A command line argument class.
Definition Args.h:33
void DeleteArgumentAtIndex(size_t idx)
Deletes the argument value at index if idx is a valid argument index.
Definition Args.cpp:359
size_t GetArgumentCount() const
Gets the number of arguments left in this command object.
Definition Args.h:120
const char * GetArgumentAtIndex(size_t idx) const
Gets the NULL terminated C string argument pointer for the argument at index idx.
Definition Args.cpp:273
General Outline: Allows adding and removing breakpoints and find by ID and index.
void GetListMutex(std::unique_lock< std::recursive_mutex > &lock)
Sets the passed in Locker to hold the Breakpoint List mutex.
size_t GetSize() const
Returns the number of elements in this breakpoint list.
lldb::BreakpointSP GetBreakpointAtIndex(size_t i) const
Returns a shared pointer to the breakpoint with index i.
static void DisassemblyFlavors(CommandInterpreter &interpreter, CompletionRequest &request, SearchFilter *searcher)
static bool InvokeCommonCompletionCallbacks(CommandInterpreter &interpreter, uint32_t completion_mask, lldb_private::CompletionRequest &request, SearchFilter *searcher)
static void ArchitectureNames(CommandInterpreter &interpreter, CompletionRequest &request, SearchFilter *searcher)
static void DiskDirectories(CommandInterpreter &interpreter, CompletionRequest &request, SearchFilter *searcher)
static void RemoteDiskDirectories(CommandInterpreter &interpreter, CompletionRequest &request, SearchFilter *searcher)
static void ManagedPlugins(CommandInterpreter &interpreter, CompletionRequest &request, SearchFilter *searcher)
static void SourceFiles(CommandInterpreter &interpreter, CompletionRequest &request, SearchFilter *searcher)
static void Registers(CommandInterpreter &interpreter, CompletionRequest &request, SearchFilter *searcher)
static void TypeLanguages(CommandInterpreter &interpreter, CompletionRequest &request, SearchFilter *searcher)
static void DiskFiles(CommandInterpreter &interpreter, CompletionRequest &request, SearchFilter *searcher)
static void ProcessPluginNames(CommandInterpreter &interpreter, CompletionRequest &request, SearchFilter *searcher)
static void PlatformPluginNames(CommandInterpreter &interpreter, CompletionRequest &request, SearchFilter *searcher)
static void Breakpoints(CommandInterpreter &interpreter, CompletionRequest &request, SearchFilter *searcher)
static void ThreadIDs(CommandInterpreter &interpreter, CompletionRequest &request, SearchFilter *searcher)
static void ProcessIDs(CommandInterpreter &interpreter, CompletionRequest &request, SearchFilter *searcher)
static void Symbols(CommandInterpreter &interpreter, CompletionRequest &request, SearchFilter *searcher)
static void StopHookIDs(CommandInterpreter &interpreter, CompletionRequest &request, SearchFilter *searcher)
static void ModuleUUIDs(CommandInterpreter &interpreter, CompletionRequest &request, SearchFilter *searcher)
static void ThreadIndexes(CommandInterpreter &interpreter, CompletionRequest &request, SearchFilter *searcher)
static void SettingsNames(CommandInterpreter &interpreter, CompletionRequest &request, SearchFilter *searcher)
static void CompleteModifiableCmdPathArgs(CommandInterpreter &interpreter, CompletionRequest &request, OptionElementVector &opt_element_vector)
This completer works for commands whose only arguments are a command path.
static void FrameIndexes(CommandInterpreter &interpreter, CompletionRequest &request, SearchFilter *searcher)
static void TypeCategoryNames(CommandInterpreter &interpreter, CompletionRequest &request, SearchFilter *searcher)
static void WatchPointIDs(CommandInterpreter &interpreter, CompletionRequest &request, SearchFilter *searcher)
static void Modules(CommandInterpreter &interpreter, CompletionRequest &request, SearchFilter *searcher)
static void RemoteDiskFiles(CommandInterpreter &interpreter, CompletionRequest &request, SearchFilter *searcher)
static void ProcessNames(CommandInterpreter &interpreter, CompletionRequest &request, SearchFilter *searcher)
static void VariablePath(CommandInterpreter &interpreter, CompletionRequest &request, SearchFilter *searcher)
static void BreakpointNames(CommandInterpreter &interpreter, CompletionRequest &request, SearchFilter *searcher)
ExecutionContext GetExecutionContext() const
CommandObject * GetUserCommandObject(llvm::StringRef cmd, StringList *matches=nullptr, StringList *descriptions=nullptr) const
CommandObjectMultiword * VerifyUserMultiwordCmdPath(Args &path, bool leaf_is_command, Status &result)
Look up the command pointed to by path encoded in the arguments of the incoming command object.
lldb::PlatformSP GetPlatform(bool prefer_target_platform)
CommandObject * GetSubcommandObject(llvm::StringRef sub_cmd, StringList *matches=nullptr) override
const FileSpec & GetPrimaryFile() const
Return the primary source spec associated with this compile unit.
"lldb/Utility/ArgCompletionRequest.h"
void AddCompletion(llvm::StringRef completion, llvm::StringRef description="", CompletionMode mode=CompletionMode::Normal)
Adds a possible completion string.
void AddCompletions(const StringList &completions)
Adds multiple possible completion strings.
llvm::StringRef GetCursorArgumentPrefix() const
bool ShouldAddCompletions() const
Returns true if the maximum number of completions has not been reached yet, hence we should keep addi...
void TryCompleteCurrentArg(llvm::StringRef completion, llvm::StringRef description="")
Adds a possible completion string if the completion would complete the current argument.
void GetMatches(StringList &matches) const
Adds all collected completion matches to the given list.
bool IsEmpty() const
Test for empty string.
llvm::StringRef GetStringRef() const
Get the string value as a llvm::StringRef.
const char * GetCString() const
Get the string value as a C string.
static void ForEach(TypeCategoryMap::ForEachCallback callback)
A class to manage flag bits.
Definition Debugger.h:80
lldb::TargetSP GetSelectedTarget()
Definition Debugger.h:180
PlatformList & GetPlatformList()
Definition Debugger.h:195
"lldb/Target/ExecutionContext.h" A class that contains an execution context.
bool HasProcessScope() const
Returns true the ExecutionContext object contains a valid target and process.
const lldb::TargetSP & GetTargetSP() const
Get accessor to get the target shared pointer.
Target * GetTargetPtr() const
Returns a pointer to the target object.
const lldb::ThreadSP & GetThreadSP() const
Get accessor to get the thread shared pointer.
bool HasTargetScope() const
Returns true the ExecutionContext object contains a valid target.
Process * GetProcessPtr() const
Returns a pointer to the process object.
RegisterContext * GetRegisterContext() const
A file utility class.
Definition FileSpec.h:57
const ConstString & GetFilename() const
Filename string const get accessor.
Definition FileSpec.h:251
const ConstString & GetDirectory() const
Directory string const get accessor.
Definition FileSpec.h:234
Status ResolveSymbolicLink(const FileSpec &src, FileSpec &dst)
llvm::vfs::directory_iterator DirBegin(const FileSpec &file_spec, std::error_code &ec)
Get a directory iterator.
llvm::ErrorOr< llvm::vfs::Status > GetStatus(const FileSpec &file_spec) const
Returns the Status object for the given file.
bool IsDirectory(const FileSpec &file_spec) const
Returns whether the given path is a directory.
static FileSystem & Instance()
static LanguageSet GetLanguagesSupportingTypeSystems()
Definition Language.cpp:424
static const char * GetNameForLanguageType(lldb::LanguageType language)
Definition Language.cpp:266
void ForEach(std::function< IterationAction(const lldb::ModuleSP &module_sp)> const &callback) const
Applies 'callback' to each module in this ModuleList.
lldb::PlatformSP GetSelectedPlatform()
Select the active platform.
Definition Platform.h:1103
static void AutoCompletePlatformName(llvm::StringRef partial_name, CompletionRequest &request)
static void AutoCompleteProcessName(llvm::StringRef partial_name, CompletionRequest &request)
static void AutoCompletePluginName(llvm::StringRef partial_name, CompletionRequest &request)
ThreadList & GetThreadList()
Definition Process.h:2253
lldb::OptionValuePropertiesSP GetValueProperties() const
virtual const RegisterInfo * GetRegisterInfoAtIndex(size_t reg)=0
virtual size_t GetRegisterCount()=0
"lldb/Core/SearchFilter.h" This is a SearchFilter that searches through all modules.
General Outline: Provides the callback and search depth for the SearchFilter search.
virtual void Search(Searcher &searcher)
Call this method to do the search using the Searcher.
General Outline: Provides the callback and search depth for the SearchFilter search.
An error handling class.
Definition Status.h:118
llvm::StringRef GetString() const
void SetIndentLevel(unsigned level)
Set the current indentation level.
Definition Stream.cpp:190
size_t SplitIntoLines(const std::string &lines)
Defines a symbol context baton that can be handed other debug core functions.
lldb::ModuleSP module_sp
The Module for a given query.
CompileUnit * comp_unit
The CompileUnit for a given query.
std::shared_ptr< StopHook > StopHookSP
Definition Target.h:1460
const ModuleList & GetImages() const
Get accessor for the images for this process.
Definition Target.h:1014
WatchpointList & GetWatchpointList()
Definition Target.h:807
lldb::ThreadSP GetThreadAtIndex(uint32_t idx, bool can_update=true)
virtual bool ResolveExact(llvm::StringRef Expr, llvm::SmallVectorImpl< char > &Output)=0
Resolve a Tilde Expression contained according to bash rules.
virtual bool ResolvePartial(llvm::StringRef Expr, llvm::StringSet<> &Output)=0
Auto-complete a tilde expression with all matching values.
static void AutoComplete(const ExecutionContext &exe_ctx, CompletionRequest &request)
Definition Variable.cpp:720
This class is used by Watchpoint to manage a list of watchpoints,.
WatchpointIterable Watchpoints() const
A class that represents a running process on the host machine.
static uint32_t bit(const uint32_t val, const uint32_t msbit)
Definition ARMUtils.h:270
std::vector< OptionArgElement > OptionElementVector
Definition Options.h:43
@ Partial
The current token has been partially completed.
@ Normal
The current token has been completed.
std::vector< ProcessInstanceInfo > ProcessInstanceInfoList
Definition Host.h:32
@ eRemoteDiskDirectoryCompletion
@ eFrameIndexCompletion
@ eModuleUUIDCompletion
@ eDisassemblyFlavorCompletion
@ eVariablePathCompletion
@ eDiskDirectoryCompletion
@ eTypeCategoryNameCompletion
@ ePlatformPluginCompletion
@ eSettingsNameCompletion
@ eSourceFileCompletion
@ eTypeLanguageCompletion
@ eStopHookIDCompletion
@ eWatchpointIDCompletion
@ eBreakpointNameCompletion
@ eProcessPluginCompletion
@ eRemoteDiskFileCompletion
@ eBreakpointCompletion
@ eThreadIndexCompletion
@ eArchitectureCompletion
@ eProcessNameCompletion
@ eManagedPluginCompletion
@ eTerminatorCompletion
std::shared_ptr< lldb_private::OptionValueProperties > OptionValuePropertiesSP
std::shared_ptr< lldb_private::StackFrame > StackFrameSP
@ eDescriptionLevelBrief
@ eDescriptionLevelInitial
std::shared_ptr< lldb_private::Thread > ThreadSP
std::shared_ptr< lldb_private::Platform > PlatformSP
LanguageType
Programming language type.
std::shared_ptr< lldb_private::Breakpoint > BreakpointSP
std::shared_ptr< lldb_private::Watchpoint > WatchpointSP
@ eSearchDepthCompUnit
std::shared_ptr< lldb_private::TypeCategoryImpl > TypeCategoryImplSP
std::shared_ptr< lldb_private::Target > TargetSP
std::shared_ptr< lldb_private::Module > ModuleSP
bool include_inlines
Include inlined functions.
Definition Module.h:70
bool include_symbols
Include the symbol table.
Definition Module.h:68
Every register is described in detail including its name, alternate name (optional),...
const char * alt_name
Alternate name of this register, can be NULL.
const char * name
Name of this register, can't be NULL.
#define PATH_MAX