1 //===-- CommandObjectTarget.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 "CommandObjectTarget.h"
10 
11 #include "lldb/Core/Debugger.h"
12 #include "lldb/Core/IOHandler.h"
13 #include "lldb/Core/Module.h"
14 #include "lldb/Core/ModuleSpec.h"
15 #include "lldb/Core/Section.h"
16 #include "lldb/Core/ValueObjectVariable.h"
17 #include "lldb/DataFormatters/ValueObjectPrinter.h"
18 #include "lldb/Host/OptionParser.h"
19 #include "lldb/Host/StringConvert.h"
20 #include "lldb/Interpreter/CommandInterpreter.h"
21 #include "lldb/Interpreter/CommandReturnObject.h"
22 #include "lldb/Interpreter/OptionArgParser.h"
23 #include "lldb/Interpreter/OptionGroupArchitecture.h"
24 #include "lldb/Interpreter/OptionGroupBoolean.h"
25 #include "lldb/Interpreter/OptionGroupFile.h"
26 #include "lldb/Interpreter/OptionGroupFormat.h"
27 #include "lldb/Interpreter/OptionGroupString.h"
28 #include "lldb/Interpreter/OptionGroupUInt64.h"
29 #include "lldb/Interpreter/OptionGroupUUID.h"
30 #include "lldb/Interpreter/OptionGroupValueObjectDisplay.h"
31 #include "lldb/Interpreter/OptionGroupVariable.h"
32 #include "lldb/Interpreter/Options.h"
33 #include "lldb/Symbol/CompileUnit.h"
34 #include "lldb/Symbol/FuncUnwinders.h"
35 #include "lldb/Symbol/LineTable.h"
36 #include "lldb/Symbol/LocateSymbolFile.h"
37 #include "lldb/Symbol/ObjectFile.h"
38 #include "lldb/Symbol/SymbolFile.h"
39 #include "lldb/Symbol/UnwindPlan.h"
40 #include "lldb/Symbol/VariableList.h"
41 #include "lldb/Target/ABI.h"
42 #include "lldb/Target/Process.h"
43 #include "lldb/Target/RegisterContext.h"
44 #include "lldb/Target/SectionLoadList.h"
45 #include "lldb/Target/StackFrame.h"
46 #include "lldb/Target/Thread.h"
47 #include "lldb/Target/ThreadSpec.h"
48 #include "lldb/Utility/Args.h"
49 #include "lldb/Utility/State.h"
50 #include "lldb/Utility/Timer.h"
51 
52 #include "llvm/Support/FileSystem.h"
53 #include "llvm/Support/FormatAdapters.h"
54 
55 
56 using namespace lldb;
57 using namespace lldb_private;
58 
59 static void DumpTargetInfo(uint32_t target_idx, Target *target,
60                            const char *prefix_cstr,
61                            bool show_stopped_process_status, Stream &strm) {
62   const ArchSpec &target_arch = target->GetArchitecture();
63 
64   Module *exe_module = target->GetExecutableModulePointer();
65   char exe_path[PATH_MAX];
66   bool exe_valid = false;
67   if (exe_module)
68     exe_valid = exe_module->GetFileSpec().GetPath(exe_path, sizeof(exe_path));
69 
70   if (!exe_valid)
71     ::strcpy(exe_path, "<none>");
72 
73   strm.Printf("%starget #%u: %s", prefix_cstr ? prefix_cstr : "", target_idx,
74               exe_path);
75 
76   uint32_t properties = 0;
77   if (target_arch.IsValid()) {
78     strm.Printf("%sarch=", properties++ > 0 ? ", " : " ( ");
79     target_arch.DumpTriple(strm.AsRawOstream());
80     properties++;
81   }
82   PlatformSP platform_sp(target->GetPlatform());
83   if (platform_sp)
84     strm.Printf("%splatform=%s", properties++ > 0 ? ", " : " ( ",
85                 platform_sp->GetName().GetCString());
86 
87   ProcessSP process_sp(target->GetProcessSP());
88   bool show_process_status = false;
89   if (process_sp) {
90     lldb::pid_t pid = process_sp->GetID();
91     StateType state = process_sp->GetState();
92     if (show_stopped_process_status)
93       show_process_status = StateIsStoppedState(state, true);
94     const char *state_cstr = StateAsCString(state);
95     if (pid != LLDB_INVALID_PROCESS_ID)
96       strm.Printf("%spid=%" PRIu64, properties++ > 0 ? ", " : " ( ", pid);
97     strm.Printf("%sstate=%s", properties++ > 0 ? ", " : " ( ", state_cstr);
98   }
99   if (properties > 0)
100     strm.PutCString(" )\n");
101   else
102     strm.EOL();
103   if (show_process_status) {
104     const bool only_threads_with_stop_reason = true;
105     const uint32_t start_frame = 0;
106     const uint32_t num_frames = 1;
107     const uint32_t num_frames_with_source = 1;
108     const bool stop_format = false;
109     process_sp->GetStatus(strm);
110     process_sp->GetThreadStatus(strm, only_threads_with_stop_reason,
111                                 start_frame, num_frames, num_frames_with_source,
112                                 stop_format);
113   }
114 }
115 
116 static uint32_t DumpTargetList(TargetList &target_list,
117                                bool show_stopped_process_status, Stream &strm) {
118   const uint32_t num_targets = target_list.GetNumTargets();
119   if (num_targets) {
120     TargetSP selected_target_sp(target_list.GetSelectedTarget());
121     strm.PutCString("Current targets:\n");
122     for (uint32_t i = 0; i < num_targets; ++i) {
123       TargetSP target_sp(target_list.GetTargetAtIndex(i));
124       if (target_sp) {
125         bool is_selected = target_sp.get() == selected_target_sp.get();
126         DumpTargetInfo(i, target_sp.get(), is_selected ? "* " : "  ",
127                        show_stopped_process_status, strm);
128       }
129     }
130   }
131   return num_targets;
132 }
133 
134 // Note that the negation in the argument name causes a slightly confusing
135 // mapping of the enum values.
136 static constexpr OptionEnumValueElement g_dependents_enumaration[] = {
137     {
138         eLoadDependentsDefault,
139         "default",
140         "Only load dependents when the target is an executable.",
141     },
142     {
143         eLoadDependentsNo,
144         "true",
145         "Don't load dependents, even if the target is an executable.",
146     },
147     {
148         eLoadDependentsYes,
149         "false",
150         "Load dependents, even if the target is not an executable.",
151     },
152 };
153 
154 #define LLDB_OPTIONS_target_dependents
155 #include "CommandOptions.inc"
156 
157 class OptionGroupDependents : public OptionGroup {
158 public:
159   OptionGroupDependents() {}
160 
161   ~OptionGroupDependents() override {}
162 
163   llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
164     return llvm::makeArrayRef(g_target_dependents_options);
165   }
166 
167   Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_value,
168                         ExecutionContext *execution_context) override {
169     Status error;
170 
171     // For compatibility no value means don't load dependents.
172     if (option_value.empty()) {
173       m_load_dependent_files = eLoadDependentsNo;
174       return error;
175     }
176 
177     const char short_option =
178         g_target_dependents_options[option_idx].short_option;
179     if (short_option == 'd') {
180       LoadDependentFiles tmp_load_dependents;
181       tmp_load_dependents = (LoadDependentFiles)OptionArgParser::ToOptionEnum(
182           option_value, g_target_dependents_options[option_idx].enum_values, 0,
183           error);
184       if (error.Success())
185         m_load_dependent_files = tmp_load_dependents;
186     } else {
187       error.SetErrorStringWithFormat("unrecognized short option '%c'",
188                                      short_option);
189     }
190 
191     return error;
192   }
193 
194   Status SetOptionValue(uint32_t, const char *, ExecutionContext *) = delete;
195 
196   void OptionParsingStarting(ExecutionContext *execution_context) override {
197     m_load_dependent_files = eLoadDependentsDefault;
198   }
199 
200   LoadDependentFiles m_load_dependent_files;
201 
202 private:
203   DISALLOW_COPY_AND_ASSIGN(OptionGroupDependents);
204 };
205 
206 #pragma mark CommandObjectTargetCreate
207 
208 // "target create"
209 
210 class CommandObjectTargetCreate : public CommandObjectParsed {
211 public:
212   CommandObjectTargetCreate(CommandInterpreter &interpreter)
213       : CommandObjectParsed(
214             interpreter, "target create",
215             "Create a target using the argument as the main executable.",
216             nullptr),
217         m_option_group(), m_arch_option(),
218         m_core_file(LLDB_OPT_SET_1, false, "core", 'c', 0, eArgTypeFilename,
219                     "Fullpath to a core file to use for this target."),
220         m_platform_path(LLDB_OPT_SET_1, false, "platform-path", 'P', 0,
221                         eArgTypePath,
222                         "Path to the remote file to use for this target."),
223         m_symbol_file(LLDB_OPT_SET_1, false, "symfile", 's', 0,
224                       eArgTypeFilename,
225                       "Fullpath to a stand alone debug "
226                       "symbols file for when debug symbols "
227                       "are not in the executable."),
228         m_remote_file(
229             LLDB_OPT_SET_1, false, "remote-file", 'r', 0, eArgTypeFilename,
230             "Fullpath to the file on the remote host if debugging remotely."),
231         m_add_dependents() {
232     CommandArgumentEntry arg;
233     CommandArgumentData file_arg;
234 
235     // Define the first (and only) variant of this arg.
236     file_arg.arg_type = eArgTypeFilename;
237     file_arg.arg_repetition = eArgRepeatPlain;
238 
239     // There is only one variant this argument could be; put it into the
240     // argument entry.
241     arg.push_back(file_arg);
242 
243     // Push the data for the first argument into the m_arguments vector.
244     m_arguments.push_back(arg);
245 
246     m_option_group.Append(&m_arch_option, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
247     m_option_group.Append(&m_core_file, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
248     m_option_group.Append(&m_platform_path, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
249     m_option_group.Append(&m_symbol_file, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
250     m_option_group.Append(&m_remote_file, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
251     m_option_group.Append(&m_add_dependents, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
252     m_option_group.Finalize();
253   }
254 
255   ~CommandObjectTargetCreate() override = default;
256 
257   Options *GetOptions() override { return &m_option_group; }
258 
259   void
260   HandleArgumentCompletion(CompletionRequest &request,
261                            OptionElementVector &opt_element_vector) override {
262     CommandCompletions::InvokeCommonCompletionCallbacks(
263         GetCommandInterpreter(), CommandCompletions::eDiskFileCompletion,
264         request, nullptr);
265   }
266 
267 protected:
268   bool DoExecute(Args &command, CommandReturnObject &result) override {
269     const size_t argc = command.GetArgumentCount();
270     FileSpec core_file(m_core_file.GetOptionValue().GetCurrentValue());
271     FileSpec remote_file(m_remote_file.GetOptionValue().GetCurrentValue());
272 
273     if (core_file) {
274       if (!FileSystem::Instance().Exists(core_file)) {
275         result.AppendErrorWithFormat("core file '%s' doesn't exist",
276                                      core_file.GetPath().c_str());
277         result.SetStatus(eReturnStatusFailed);
278         return false;
279       }
280       if (!FileSystem::Instance().Readable(core_file)) {
281         result.AppendErrorWithFormat("core file '%s' is not readable",
282                                      core_file.GetPath().c_str());
283         result.SetStatus(eReturnStatusFailed);
284         return false;
285       }
286     }
287 
288     if (argc == 1 || core_file || remote_file) {
289       FileSpec symfile(m_symbol_file.GetOptionValue().GetCurrentValue());
290       if (symfile) {
291         if (FileSystem::Instance().Exists(symfile)) {
292           if (!FileSystem::Instance().Readable(symfile)) {
293             result.AppendErrorWithFormat("symbol file '%s' is not readable",
294                                          symfile.GetPath().c_str());
295             result.SetStatus(eReturnStatusFailed);
296             return false;
297           }
298         } else {
299           char symfile_path[PATH_MAX];
300           symfile.GetPath(symfile_path, sizeof(symfile_path));
301           result.AppendErrorWithFormat("invalid symbol file path '%s'",
302                                        symfile_path);
303           result.SetStatus(eReturnStatusFailed);
304           return false;
305         }
306       }
307 
308       const char *file_path = command.GetArgumentAtIndex(0);
309       static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
310       Timer scoped_timer(func_cat, "(lldb) target create '%s'", file_path);
311       FileSpec file_spec;
312 
313       if (file_path) {
314         file_spec.SetFile(file_path, FileSpec::Style::native);
315         FileSystem::Instance().Resolve(file_spec);
316       }
317 
318       bool must_set_platform_path = false;
319 
320       Debugger &debugger = GetDebugger();
321 
322       TargetSP target_sp;
323       llvm::StringRef arch_cstr = m_arch_option.GetArchitectureName();
324       Status error(debugger.GetTargetList().CreateTarget(
325           debugger, file_path, arch_cstr,
326           m_add_dependents.m_load_dependent_files, nullptr, target_sp));
327 
328       if (target_sp) {
329         // Only get the platform after we create the target because we might
330         // have switched platforms depending on what the arguments were to
331         // CreateTarget() we can't rely on the selected platform.
332 
333         PlatformSP platform_sp = target_sp->GetPlatform();
334 
335         if (remote_file) {
336           if (platform_sp) {
337             // I have a remote file.. two possible cases
338             if (file_spec && FileSystem::Instance().Exists(file_spec)) {
339               // if the remote file does not exist, push it there
340               if (!platform_sp->GetFileExists(remote_file)) {
341                 Status err = platform_sp->PutFile(file_spec, remote_file);
342                 if (err.Fail()) {
343                   result.AppendError(err.AsCString());
344                   result.SetStatus(eReturnStatusFailed);
345                   return false;
346                 }
347               }
348             } else {
349               // there is no local file and we need one
350               // in order to make the remote ---> local transfer we need a
351               // platform
352               // TODO: if the user has passed in a --platform argument, use it
353               // to fetch the right platform
354               if (!platform_sp) {
355                 result.AppendError(
356                     "unable to perform remote debugging without a platform");
357                 result.SetStatus(eReturnStatusFailed);
358                 return false;
359               }
360               if (file_path) {
361                 // copy the remote file to the local file
362                 Status err = platform_sp->GetFile(remote_file, file_spec);
363                 if (err.Fail()) {
364                   result.AppendError(err.AsCString());
365                   result.SetStatus(eReturnStatusFailed);
366                   return false;
367                 }
368               } else {
369                 // make up a local file
370                 result.AppendError("remote --> local transfer without local "
371                                    "path is not implemented yet");
372                 result.SetStatus(eReturnStatusFailed);
373                 return false;
374               }
375             }
376           } else {
377             result.AppendError("no platform found for target");
378             result.SetStatus(eReturnStatusFailed);
379             return false;
380           }
381         }
382 
383         if (symfile || remote_file) {
384           ModuleSP module_sp(target_sp->GetExecutableModule());
385           if (module_sp) {
386             if (symfile)
387               module_sp->SetSymbolFileFileSpec(symfile);
388             if (remote_file) {
389               std::string remote_path = remote_file.GetPath();
390               target_sp->SetArg0(remote_path.c_str());
391               module_sp->SetPlatformFileSpec(remote_file);
392             }
393           }
394         }
395 
396         debugger.GetTargetList().SetSelectedTarget(target_sp.get());
397         if (must_set_platform_path) {
398           ModuleSpec main_module_spec(file_spec);
399           ModuleSP module_sp =
400               target_sp->GetOrCreateModule(main_module_spec, true /* notify */);
401           if (module_sp)
402             module_sp->SetPlatformFileSpec(remote_file);
403         }
404         if (core_file) {
405           char core_path[PATH_MAX];
406           core_file.GetPath(core_path, sizeof(core_path));
407           if (FileSystem::Instance().Exists(core_file)) {
408             if (!FileSystem::Instance().Readable(core_file)) {
409               result.AppendMessageWithFormat(
410                   "Core file '%s' is not readable.\n", core_path);
411               result.SetStatus(eReturnStatusFailed);
412               return false;
413             }
414             FileSpec core_file_dir;
415             core_file_dir.GetDirectory() = core_file.GetDirectory();
416             target_sp->AppendExecutableSearchPaths(core_file_dir);
417 
418             ProcessSP process_sp(target_sp->CreateProcess(
419                 GetDebugger().GetListener(), llvm::StringRef(), &core_file));
420 
421             if (process_sp) {
422               // Seems weird that we Launch a core file, but that is what we
423               // do!
424               error = process_sp->LoadCore();
425 
426               if (error.Fail()) {
427                 result.AppendError(
428                     error.AsCString("can't find plug-in for core file"));
429                 result.SetStatus(eReturnStatusFailed);
430                 return false;
431               } else {
432                 result.AppendMessageWithFormat(
433                     "Core file '%s' (%s) was loaded.\n", core_path,
434                     target_sp->GetArchitecture().GetArchitectureName());
435                 result.SetStatus(eReturnStatusSuccessFinishNoResult);
436               }
437             } else {
438               result.AppendErrorWithFormat(
439                   "Unable to find process plug-in for core file '%s'\n",
440                   core_path);
441               result.SetStatus(eReturnStatusFailed);
442             }
443           } else {
444             result.AppendErrorWithFormat("Core file '%s' does not exist\n",
445                                          core_path);
446             result.SetStatus(eReturnStatusFailed);
447           }
448         } else {
449           result.AppendMessageWithFormat(
450               "Current executable set to '%s' (%s).\n",
451               file_spec.GetPath().c_str(),
452               target_sp->GetArchitecture().GetArchitectureName());
453           result.SetStatus(eReturnStatusSuccessFinishNoResult);
454         }
455       } else {
456         result.AppendError(error.AsCString());
457         result.SetStatus(eReturnStatusFailed);
458       }
459     } else {
460       result.AppendErrorWithFormat("'%s' takes exactly one executable path "
461                                    "argument, or use the --core option.\n",
462                                    m_cmd_name.c_str());
463       result.SetStatus(eReturnStatusFailed);
464     }
465     return result.Succeeded();
466   }
467 
468 private:
469   OptionGroupOptions m_option_group;
470   OptionGroupArchitecture m_arch_option;
471   OptionGroupFile m_core_file;
472   OptionGroupFile m_platform_path;
473   OptionGroupFile m_symbol_file;
474   OptionGroupFile m_remote_file;
475   OptionGroupDependents m_add_dependents;
476 };
477 
478 #pragma mark CommandObjectTargetList
479 
480 // "target list"
481 
482 class CommandObjectTargetList : public CommandObjectParsed {
483 public:
484   CommandObjectTargetList(CommandInterpreter &interpreter)
485       : CommandObjectParsed(
486             interpreter, "target list",
487             "List all current targets in the current debug session.", nullptr) {
488   }
489 
490   ~CommandObjectTargetList() override = default;
491 
492 protected:
493   bool DoExecute(Args &args, CommandReturnObject &result) override {
494     if (args.GetArgumentCount() == 0) {
495       Stream &strm = result.GetOutputStream();
496 
497       bool show_stopped_process_status = false;
498       if (DumpTargetList(GetDebugger().GetTargetList(),
499                          show_stopped_process_status, strm) == 0) {
500         strm.PutCString("No targets.\n");
501       }
502       result.SetStatus(eReturnStatusSuccessFinishResult);
503     } else {
504       result.AppendError("the 'target list' command takes no arguments\n");
505       result.SetStatus(eReturnStatusFailed);
506     }
507     return result.Succeeded();
508   }
509 };
510 
511 #pragma mark CommandObjectTargetSelect
512 
513 // "target select"
514 
515 class CommandObjectTargetSelect : public CommandObjectParsed {
516 public:
517   CommandObjectTargetSelect(CommandInterpreter &interpreter)
518       : CommandObjectParsed(
519             interpreter, "target select",
520             "Select a target as the current target by target index.", nullptr) {
521   }
522 
523   ~CommandObjectTargetSelect() override = default;
524 
525 protected:
526   bool DoExecute(Args &args, CommandReturnObject &result) override {
527     if (args.GetArgumentCount() == 1) {
528       bool success = false;
529       const char *target_idx_arg = args.GetArgumentAtIndex(0);
530       uint32_t target_idx =
531           StringConvert::ToUInt32(target_idx_arg, UINT32_MAX, 0, &success);
532       if (success) {
533         TargetList &target_list = GetDebugger().GetTargetList();
534         const uint32_t num_targets = target_list.GetNumTargets();
535         if (target_idx < num_targets) {
536           TargetSP target_sp(target_list.GetTargetAtIndex(target_idx));
537           if (target_sp) {
538             Stream &strm = result.GetOutputStream();
539             target_list.SetSelectedTarget(target_sp.get());
540             bool show_stopped_process_status = false;
541             DumpTargetList(target_list, show_stopped_process_status, strm);
542             result.SetStatus(eReturnStatusSuccessFinishResult);
543           } else {
544             result.AppendErrorWithFormat("target #%u is NULL in target list\n",
545                                          target_idx);
546             result.SetStatus(eReturnStatusFailed);
547           }
548         } else {
549           if (num_targets > 0) {
550             result.AppendErrorWithFormat(
551                 "index %u is out of range, valid target indexes are 0 - %u\n",
552                 target_idx, num_targets - 1);
553           } else {
554             result.AppendErrorWithFormat(
555                 "index %u is out of range since there are no active targets\n",
556                 target_idx);
557           }
558           result.SetStatus(eReturnStatusFailed);
559         }
560       } else {
561         result.AppendErrorWithFormat("invalid index string value '%s'\n",
562                                      target_idx_arg);
563         result.SetStatus(eReturnStatusFailed);
564       }
565     } else {
566       result.AppendError(
567           "'target select' takes a single argument: a target index\n");
568       result.SetStatus(eReturnStatusFailed);
569     }
570     return result.Succeeded();
571   }
572 };
573 
574 #pragma mark CommandObjectTargetDelete
575 
576 // "target delete"
577 
578 class CommandObjectTargetDelete : public CommandObjectParsed {
579 public:
580   CommandObjectTargetDelete(CommandInterpreter &interpreter)
581       : CommandObjectParsed(interpreter, "target delete",
582                             "Delete one or more targets by target index.",
583                             nullptr),
584         m_option_group(), m_all_option(LLDB_OPT_SET_1, false, "all", 'a',
585                                        "Delete all targets.", false, true),
586         m_cleanup_option(
587             LLDB_OPT_SET_1, false, "clean", 'c',
588             "Perform extra cleanup to minimize memory consumption after "
589             "deleting the target.  "
590             "By default, LLDB will keep in memory any modules previously "
591             "loaded by the target as well "
592             "as all of its debug info.  Specifying --clean will unload all of "
593             "these shared modules and "
594             "cause them to be reparsed again the next time the target is run",
595             false, true) {
596     m_option_group.Append(&m_all_option, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
597     m_option_group.Append(&m_cleanup_option, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
598     m_option_group.Finalize();
599   }
600 
601   ~CommandObjectTargetDelete() override = default;
602 
603   Options *GetOptions() override { return &m_option_group; }
604 
605 protected:
606   bool DoExecute(Args &args, CommandReturnObject &result) override {
607     const size_t argc = args.GetArgumentCount();
608     std::vector<TargetSP> delete_target_list;
609     TargetList &target_list = GetDebugger().GetTargetList();
610     TargetSP target_sp;
611 
612     if (m_all_option.GetOptionValue()) {
613       for (int i = 0; i < target_list.GetNumTargets(); ++i)
614         delete_target_list.push_back(target_list.GetTargetAtIndex(i));
615     } else if (argc > 0) {
616       const uint32_t num_targets = target_list.GetNumTargets();
617       // Bail out if don't have any targets.
618       if (num_targets == 0) {
619         result.AppendError("no targets to delete");
620         result.SetStatus(eReturnStatusFailed);
621         return false;
622       }
623 
624       for (auto &entry : args.entries()) {
625         uint32_t target_idx;
626         if (entry.ref().getAsInteger(0, target_idx)) {
627           result.AppendErrorWithFormat("invalid target index '%s'\n",
628                                        entry.c_str());
629           result.SetStatus(eReturnStatusFailed);
630           return false;
631         }
632         if (target_idx < num_targets) {
633           target_sp = target_list.GetTargetAtIndex(target_idx);
634           if (target_sp) {
635             delete_target_list.push_back(target_sp);
636             continue;
637           }
638         }
639         if (num_targets > 1)
640           result.AppendErrorWithFormat("target index %u is out of range, valid "
641                                        "target indexes are 0 - %u\n",
642                                        target_idx, num_targets - 1);
643         else
644           result.AppendErrorWithFormat(
645               "target index %u is out of range, the only valid index is 0\n",
646               target_idx);
647 
648         result.SetStatus(eReturnStatusFailed);
649         return false;
650       }
651     } else {
652       target_sp = target_list.GetSelectedTarget();
653       if (!target_sp) {
654         result.AppendErrorWithFormat("no target is currently selected\n");
655         result.SetStatus(eReturnStatusFailed);
656         return false;
657       }
658       delete_target_list.push_back(target_sp);
659     }
660 
661     const size_t num_targets_to_delete = delete_target_list.size();
662     for (size_t idx = 0; idx < num_targets_to_delete; ++idx) {
663       target_sp = delete_target_list[idx];
664       target_list.DeleteTarget(target_sp);
665       target_sp->Destroy();
666     }
667     // If "--clean" was specified, prune any orphaned shared modules from the
668     // global shared module list
669     if (m_cleanup_option.GetOptionValue()) {
670       const bool mandatory = true;
671       ModuleList::RemoveOrphanSharedModules(mandatory);
672     }
673     result.GetOutputStream().Printf("%u targets deleted.\n",
674                                     (uint32_t)num_targets_to_delete);
675     result.SetStatus(eReturnStatusSuccessFinishResult);
676 
677     return true;
678   }
679 
680   OptionGroupOptions m_option_group;
681   OptionGroupBoolean m_all_option;
682   OptionGroupBoolean m_cleanup_option;
683 };
684 
685 class CommandObjectTargetShowLaunchEnvironment : public CommandObjectParsed {
686 public:
687   CommandObjectTargetShowLaunchEnvironment(CommandInterpreter &interpreter)
688       : CommandObjectParsed(
689             interpreter, "target show-launch-environment",
690             "Shows the environment being passed to the process when launched, "
691             "taking info account 3 settings: target.env-vars, "
692             "target.inherit-env and target.unset-env-vars.",
693             nullptr, eCommandRequiresTarget) {}
694 
695   ~CommandObjectTargetShowLaunchEnvironment() override = default;
696 
697 protected:
698   bool DoExecute(Args &args, CommandReturnObject &result) override {
699     Target *target = m_exe_ctx.GetTargetPtr();
700     Environment env = target->GetEnvironment();
701 
702     std::vector<Environment::value_type *> env_vector;
703     env_vector.reserve(env.size());
704     for (auto &KV : env)
705       env_vector.push_back(&KV);
706     std::sort(env_vector.begin(), env_vector.end(),
707               [](Environment::value_type *a, Environment::value_type *b) {
708                 return a->first() < b->first();
709               });
710 
711     auto &strm = result.GetOutputStream();
712     for (auto &KV : env_vector)
713       strm.Format("{0}={1}\n", KV->first(), KV->second);
714 
715     result.SetStatus(eReturnStatusSuccessFinishResult);
716     return result.Succeeded();
717   }
718 };
719 
720 #pragma mark CommandObjectTargetVariable
721 
722 // "target variable"
723 
724 class CommandObjectTargetVariable : public CommandObjectParsed {
725   static const uint32_t SHORT_OPTION_FILE = 0x66696c65; // 'file'
726   static const uint32_t SHORT_OPTION_SHLB = 0x73686c62; // 'shlb'
727 
728 public:
729   CommandObjectTargetVariable(CommandInterpreter &interpreter)
730       : CommandObjectParsed(interpreter, "target variable",
731                             "Read global variables for the current target, "
732                             "before or while running a process.",
733                             nullptr, eCommandRequiresTarget),
734         m_option_group(),
735         m_option_variable(false), // Don't include frame options
736         m_option_format(eFormatDefault),
737         m_option_compile_units(LLDB_OPT_SET_1, false, "file", SHORT_OPTION_FILE,
738                                0, eArgTypeFilename,
739                                "A basename or fullpath to a file that contains "
740                                "global variables. This option can be "
741                                "specified multiple times."),
742         m_option_shared_libraries(
743             LLDB_OPT_SET_1, false, "shlib", SHORT_OPTION_SHLB, 0,
744             eArgTypeFilename,
745             "A basename or fullpath to a shared library to use in the search "
746             "for global "
747             "variables. This option can be specified multiple times."),
748         m_varobj_options() {
749     CommandArgumentEntry arg;
750     CommandArgumentData var_name_arg;
751 
752     // Define the first (and only) variant of this arg.
753     var_name_arg.arg_type = eArgTypeVarName;
754     var_name_arg.arg_repetition = eArgRepeatPlus;
755 
756     // There is only one variant this argument could be; put it into the
757     // argument entry.
758     arg.push_back(var_name_arg);
759 
760     // Push the data for the first argument into the m_arguments vector.
761     m_arguments.push_back(arg);
762 
763     m_option_group.Append(&m_varobj_options, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
764     m_option_group.Append(&m_option_variable, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
765     m_option_group.Append(&m_option_format,
766                           OptionGroupFormat::OPTION_GROUP_FORMAT |
767                               OptionGroupFormat::OPTION_GROUP_GDB_FMT,
768                           LLDB_OPT_SET_1);
769     m_option_group.Append(&m_option_compile_units, LLDB_OPT_SET_ALL,
770                           LLDB_OPT_SET_1);
771     m_option_group.Append(&m_option_shared_libraries, LLDB_OPT_SET_ALL,
772                           LLDB_OPT_SET_1);
773     m_option_group.Finalize();
774   }
775 
776   ~CommandObjectTargetVariable() override = default;
777 
778   void DumpValueObject(Stream &s, VariableSP &var_sp, ValueObjectSP &valobj_sp,
779                        const char *root_name) {
780     DumpValueObjectOptions options(m_varobj_options.GetAsDumpOptions());
781 
782     if (!valobj_sp->GetTargetSP()->GetDisplayRuntimeSupportValues() &&
783         valobj_sp->IsRuntimeSupportValue())
784       return;
785 
786     switch (var_sp->GetScope()) {
787     case eValueTypeVariableGlobal:
788       if (m_option_variable.show_scope)
789         s.PutCString("GLOBAL: ");
790       break;
791 
792     case eValueTypeVariableStatic:
793       if (m_option_variable.show_scope)
794         s.PutCString("STATIC: ");
795       break;
796 
797     case eValueTypeVariableArgument:
798       if (m_option_variable.show_scope)
799         s.PutCString("   ARG: ");
800       break;
801 
802     case eValueTypeVariableLocal:
803       if (m_option_variable.show_scope)
804         s.PutCString(" LOCAL: ");
805       break;
806 
807     case eValueTypeVariableThreadLocal:
808       if (m_option_variable.show_scope)
809         s.PutCString("THREAD: ");
810       break;
811 
812     default:
813       break;
814     }
815 
816     if (m_option_variable.show_decl) {
817       bool show_fullpaths = false;
818       bool show_module = true;
819       if (var_sp->DumpDeclaration(&s, show_fullpaths, show_module))
820         s.PutCString(": ");
821     }
822 
823     const Format format = m_option_format.GetFormat();
824     if (format != eFormatDefault)
825       options.SetFormat(format);
826 
827     options.SetRootValueObjectName(root_name);
828 
829     valobj_sp->Dump(s, options);
830   }
831 
832   static size_t GetVariableCallback(void *baton, const char *name,
833                                     VariableList &variable_list) {
834     size_t old_size = variable_list.GetSize();
835     Target *target = static_cast<Target *>(baton);
836     if (target)
837       target->GetImages().FindGlobalVariables(ConstString(name), UINT32_MAX,
838                                               variable_list);
839     return variable_list.GetSize() - old_size;
840   }
841 
842   Options *GetOptions() override { return &m_option_group; }
843 
844 protected:
845   void DumpGlobalVariableList(const ExecutionContext &exe_ctx,
846                               const SymbolContext &sc,
847                               const VariableList &variable_list, Stream &s) {
848     if (variable_list.Empty())
849       return;
850     if (sc.module_sp) {
851       if (sc.comp_unit) {
852         s.Format("Global variables for {0} in {1}:\n",
853                  sc.comp_unit->GetPrimaryFile(), sc.module_sp->GetFileSpec());
854       } else {
855         s.Printf("Global variables for %s\n",
856                  sc.module_sp->GetFileSpec().GetPath().c_str());
857       }
858     } else if (sc.comp_unit) {
859       s.Format("Global variables for {0}\n", sc.comp_unit->GetPrimaryFile());
860     }
861 
862     for (VariableSP var_sp : variable_list) {
863       if (!var_sp)
864         continue;
865       ValueObjectSP valobj_sp(ValueObjectVariable::Create(
866           exe_ctx.GetBestExecutionContextScope(), var_sp));
867 
868       if (valobj_sp)
869         DumpValueObject(s, var_sp, valobj_sp, var_sp->GetName().GetCString());
870     }
871   }
872 
873   bool DoExecute(Args &args, CommandReturnObject &result) override {
874     Target *target = m_exe_ctx.GetTargetPtr();
875     const size_t argc = args.GetArgumentCount();
876     Stream &s = result.GetOutputStream();
877 
878     if (argc > 0) {
879 
880       // TODO: Convert to entry-based iteration.  Requires converting
881       // DumpValueObject.
882       for (size_t idx = 0; idx < argc; ++idx) {
883         VariableList variable_list;
884         ValueObjectList valobj_list;
885 
886         const char *arg = args.GetArgumentAtIndex(idx);
887         size_t matches = 0;
888         bool use_var_name = false;
889         if (m_option_variable.use_regex) {
890           RegularExpression regex(llvm::StringRef::withNullAsEmpty(arg));
891           if (!regex.IsValid()) {
892             result.GetErrorStream().Printf(
893                 "error: invalid regular expression: '%s'\n", arg);
894             result.SetStatus(eReturnStatusFailed);
895             return false;
896           }
897           use_var_name = true;
898           target->GetImages().FindGlobalVariables(regex, UINT32_MAX,
899                                                   variable_list);
900           matches = variable_list.GetSize();
901         } else {
902           Status error(Variable::GetValuesForVariableExpressionPath(
903               arg, m_exe_ctx.GetBestExecutionContextScope(),
904               GetVariableCallback, target, variable_list, valobj_list));
905           matches = variable_list.GetSize();
906         }
907 
908         if (matches == 0) {
909           result.GetErrorStream().Printf(
910               "error: can't find global variable '%s'\n", arg);
911           result.SetStatus(eReturnStatusFailed);
912           return false;
913         } else {
914           for (uint32_t global_idx = 0; global_idx < matches; ++global_idx) {
915             VariableSP var_sp(variable_list.GetVariableAtIndex(global_idx));
916             if (var_sp) {
917               ValueObjectSP valobj_sp(
918                   valobj_list.GetValueObjectAtIndex(global_idx));
919               if (!valobj_sp)
920                 valobj_sp = ValueObjectVariable::Create(
921                     m_exe_ctx.GetBestExecutionContextScope(), var_sp);
922 
923               if (valobj_sp)
924                 DumpValueObject(s, var_sp, valobj_sp,
925                                 use_var_name ? var_sp->GetName().GetCString()
926                                              : arg);
927             }
928           }
929         }
930       }
931     } else {
932       const FileSpecList &compile_units =
933           m_option_compile_units.GetOptionValue().GetCurrentValue();
934       const FileSpecList &shlibs =
935           m_option_shared_libraries.GetOptionValue().GetCurrentValue();
936       SymbolContextList sc_list;
937       const size_t num_compile_units = compile_units.GetSize();
938       const size_t num_shlibs = shlibs.GetSize();
939       if (num_compile_units == 0 && num_shlibs == 0) {
940         bool success = false;
941         StackFrame *frame = m_exe_ctx.GetFramePtr();
942         CompileUnit *comp_unit = nullptr;
943         if (frame) {
944           SymbolContext sc = frame->GetSymbolContext(eSymbolContextCompUnit);
945           if (sc.comp_unit) {
946             const bool can_create = true;
947             VariableListSP comp_unit_varlist_sp(
948                 sc.comp_unit->GetVariableList(can_create));
949             if (comp_unit_varlist_sp) {
950               size_t count = comp_unit_varlist_sp->GetSize();
951               if (count > 0) {
952                 DumpGlobalVariableList(m_exe_ctx, sc, *comp_unit_varlist_sp, s);
953                 success = true;
954               }
955             }
956           }
957         }
958         if (!success) {
959           if (frame) {
960             if (comp_unit)
961               result.AppendErrorWithFormatv(
962                   "no global variables in current compile unit: {0}\n",
963                   comp_unit->GetPrimaryFile());
964             else
965               result.AppendErrorWithFormat(
966                   "no debug information for frame %u\n",
967                   frame->GetFrameIndex());
968           } else
969             result.AppendError("'target variable' takes one or more global "
970                                "variable names as arguments\n");
971           result.SetStatus(eReturnStatusFailed);
972         }
973       } else {
974         SymbolContextList sc_list;
975         // We have one or more compile unit or shlib
976         if (num_shlibs > 0) {
977           for (size_t shlib_idx = 0; shlib_idx < num_shlibs; ++shlib_idx) {
978             const FileSpec module_file(shlibs.GetFileSpecAtIndex(shlib_idx));
979             ModuleSpec module_spec(module_file);
980 
981             ModuleSP module_sp(
982                 target->GetImages().FindFirstModule(module_spec));
983             if (module_sp) {
984               if (num_compile_units > 0) {
985                 for (size_t cu_idx = 0; cu_idx < num_compile_units; ++cu_idx)
986                   module_sp->FindCompileUnits(
987                       compile_units.GetFileSpecAtIndex(cu_idx), sc_list);
988               } else {
989                 SymbolContext sc;
990                 sc.module_sp = module_sp;
991                 sc_list.Append(sc);
992               }
993             } else {
994               // Didn't find matching shlib/module in target...
995               result.AppendErrorWithFormat(
996                   "target doesn't contain the specified shared library: %s\n",
997                   module_file.GetPath().c_str());
998             }
999           }
1000         } else {
1001           // No shared libraries, we just want to find globals for the compile
1002           // units files that were specified
1003           for (size_t cu_idx = 0; cu_idx < num_compile_units; ++cu_idx)
1004             target->GetImages().FindCompileUnits(
1005                 compile_units.GetFileSpecAtIndex(cu_idx), sc_list);
1006         }
1007 
1008         const uint32_t num_scs = sc_list.GetSize();
1009         if (num_scs > 0) {
1010           SymbolContext sc;
1011           for (uint32_t sc_idx = 0; sc_idx < num_scs; ++sc_idx) {
1012             if (sc_list.GetContextAtIndex(sc_idx, sc)) {
1013               if (sc.comp_unit) {
1014                 const bool can_create = true;
1015                 VariableListSP comp_unit_varlist_sp(
1016                     sc.comp_unit->GetVariableList(can_create));
1017                 if (comp_unit_varlist_sp)
1018                   DumpGlobalVariableList(m_exe_ctx, sc, *comp_unit_varlist_sp,
1019                                          s);
1020               } else if (sc.module_sp) {
1021                 // Get all global variables for this module
1022                 lldb_private::RegularExpression all_globals_regex(
1023                     llvm::StringRef(
1024                         ".")); // Any global with at least one character
1025                 VariableList variable_list;
1026                 sc.module_sp->FindGlobalVariables(all_globals_regex, UINT32_MAX,
1027                                                   variable_list);
1028                 DumpGlobalVariableList(m_exe_ctx, sc, variable_list, s);
1029               }
1030             }
1031           }
1032         }
1033       }
1034     }
1035 
1036     if (m_interpreter.TruncationWarningNecessary()) {
1037       result.GetOutputStream().Printf(m_interpreter.TruncationWarningText(),
1038                                       m_cmd_name.c_str());
1039       m_interpreter.TruncationWarningGiven();
1040     }
1041 
1042     return result.Succeeded();
1043   }
1044 
1045   OptionGroupOptions m_option_group;
1046   OptionGroupVariable m_option_variable;
1047   OptionGroupFormat m_option_format;
1048   OptionGroupFileList m_option_compile_units;
1049   OptionGroupFileList m_option_shared_libraries;
1050   OptionGroupValueObjectDisplay m_varobj_options;
1051 };
1052 
1053 #pragma mark CommandObjectTargetModulesSearchPathsAdd
1054 
1055 class CommandObjectTargetModulesSearchPathsAdd : public CommandObjectParsed {
1056 public:
1057   CommandObjectTargetModulesSearchPathsAdd(CommandInterpreter &interpreter)
1058       : CommandObjectParsed(interpreter, "target modules search-paths add",
1059                             "Add new image search paths substitution pairs to "
1060                             "the current target.",
1061                             nullptr, eCommandRequiresTarget) {
1062     CommandArgumentEntry arg;
1063     CommandArgumentData old_prefix_arg;
1064     CommandArgumentData new_prefix_arg;
1065 
1066     // Define the first variant of this arg pair.
1067     old_prefix_arg.arg_type = eArgTypeOldPathPrefix;
1068     old_prefix_arg.arg_repetition = eArgRepeatPairPlus;
1069 
1070     // Define the first variant of this arg pair.
1071     new_prefix_arg.arg_type = eArgTypeNewPathPrefix;
1072     new_prefix_arg.arg_repetition = eArgRepeatPairPlus;
1073 
1074     // There are two required arguments that must always occur together, i.e.
1075     // an argument "pair".  Because they must always occur together, they are
1076     // treated as two variants of one argument rather than two independent
1077     // arguments.  Push them both into the first argument position for
1078     // m_arguments...
1079 
1080     arg.push_back(old_prefix_arg);
1081     arg.push_back(new_prefix_arg);
1082 
1083     m_arguments.push_back(arg);
1084   }
1085 
1086   ~CommandObjectTargetModulesSearchPathsAdd() override = default;
1087 
1088 protected:
1089   bool DoExecute(Args &command, CommandReturnObject &result) override {
1090     Target *target = &GetSelectedTarget();
1091     const size_t argc = command.GetArgumentCount();
1092     if (argc & 1) {
1093       result.AppendError("add requires an even number of arguments\n");
1094       result.SetStatus(eReturnStatusFailed);
1095     } else {
1096       for (size_t i = 0; i < argc; i += 2) {
1097         const char *from = command.GetArgumentAtIndex(i);
1098         const char *to = command.GetArgumentAtIndex(i + 1);
1099 
1100         if (from[0] && to[0]) {
1101           Log *log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_HOST);
1102           if (log) {
1103             LLDB_LOGF(log,
1104                       "target modules search path adding ImageSearchPath "
1105                       "pair: '%s' -> '%s'",
1106                       from, to);
1107           }
1108           bool last_pair = ((argc - i) == 2);
1109           target->GetImageSearchPathList().Append(
1110               ConstString(from), ConstString(to),
1111               last_pair); // Notify if this is the last pair
1112           result.SetStatus(eReturnStatusSuccessFinishNoResult);
1113         } else {
1114           if (from[0])
1115             result.AppendError("<path-prefix> can't be empty\n");
1116           else
1117             result.AppendError("<new-path-prefix> can't be empty\n");
1118           result.SetStatus(eReturnStatusFailed);
1119         }
1120       }
1121     }
1122     return result.Succeeded();
1123   }
1124 };
1125 
1126 #pragma mark CommandObjectTargetModulesSearchPathsClear
1127 
1128 class CommandObjectTargetModulesSearchPathsClear : public CommandObjectParsed {
1129 public:
1130   CommandObjectTargetModulesSearchPathsClear(CommandInterpreter &interpreter)
1131       : CommandObjectParsed(interpreter, "target modules search-paths clear",
1132                             "Clear all current image search path substitution "
1133                             "pairs from the current target.",
1134                             "target modules search-paths clear",
1135                             eCommandRequiresTarget) {}
1136 
1137   ~CommandObjectTargetModulesSearchPathsClear() override = default;
1138 
1139 protected:
1140   bool DoExecute(Args &command, CommandReturnObject &result) override {
1141     Target *target = &GetSelectedTarget();
1142     bool notify = true;
1143     target->GetImageSearchPathList().Clear(notify);
1144     result.SetStatus(eReturnStatusSuccessFinishNoResult);
1145     return result.Succeeded();
1146   }
1147 };
1148 
1149 #pragma mark CommandObjectTargetModulesSearchPathsInsert
1150 
1151 class CommandObjectTargetModulesSearchPathsInsert : public CommandObjectParsed {
1152 public:
1153   CommandObjectTargetModulesSearchPathsInsert(CommandInterpreter &interpreter)
1154       : CommandObjectParsed(interpreter, "target modules search-paths insert",
1155                             "Insert a new image search path substitution pair "
1156                             "into the current target at the specified index.",
1157                             nullptr, eCommandRequiresTarget) {
1158     CommandArgumentEntry arg1;
1159     CommandArgumentEntry arg2;
1160     CommandArgumentData index_arg;
1161     CommandArgumentData old_prefix_arg;
1162     CommandArgumentData new_prefix_arg;
1163 
1164     // Define the first and only variant of this arg.
1165     index_arg.arg_type = eArgTypeIndex;
1166     index_arg.arg_repetition = eArgRepeatPlain;
1167 
1168     // Put the one and only variant into the first arg for m_arguments:
1169     arg1.push_back(index_arg);
1170 
1171     // Define the first variant of this arg pair.
1172     old_prefix_arg.arg_type = eArgTypeOldPathPrefix;
1173     old_prefix_arg.arg_repetition = eArgRepeatPairPlus;
1174 
1175     // Define the first variant of this arg pair.
1176     new_prefix_arg.arg_type = eArgTypeNewPathPrefix;
1177     new_prefix_arg.arg_repetition = eArgRepeatPairPlus;
1178 
1179     // There are two required arguments that must always occur together, i.e.
1180     // an argument "pair".  Because they must always occur together, they are
1181     // treated as two variants of one argument rather than two independent
1182     // arguments.  Push them both into the same argument position for
1183     // m_arguments...
1184 
1185     arg2.push_back(old_prefix_arg);
1186     arg2.push_back(new_prefix_arg);
1187 
1188     // Add arguments to m_arguments.
1189     m_arguments.push_back(arg1);
1190     m_arguments.push_back(arg2);
1191   }
1192 
1193   ~CommandObjectTargetModulesSearchPathsInsert() override = default;
1194 
1195 protected:
1196   bool DoExecute(Args &command, CommandReturnObject &result) override {
1197     Target *target = &GetSelectedTarget();
1198     size_t argc = command.GetArgumentCount();
1199     // check for at least 3 arguments and an odd number of parameters
1200     if (argc >= 3 && argc & 1) {
1201       bool success = false;
1202 
1203       uint32_t insert_idx = StringConvert::ToUInt32(
1204           command.GetArgumentAtIndex(0), UINT32_MAX, 0, &success);
1205 
1206       if (!success) {
1207         result.AppendErrorWithFormat(
1208             "<index> parameter is not an integer: '%s'.\n",
1209             command.GetArgumentAtIndex(0));
1210         result.SetStatus(eReturnStatusFailed);
1211         return result.Succeeded();
1212       }
1213 
1214       // shift off the index
1215       command.Shift();
1216       argc = command.GetArgumentCount();
1217 
1218       for (uint32_t i = 0; i < argc; i += 2, ++insert_idx) {
1219         const char *from = command.GetArgumentAtIndex(i);
1220         const char *to = command.GetArgumentAtIndex(i + 1);
1221 
1222         if (from[0] && to[0]) {
1223           bool last_pair = ((argc - i) == 2);
1224           target->GetImageSearchPathList().Insert(
1225               ConstString(from), ConstString(to), insert_idx, last_pair);
1226           result.SetStatus(eReturnStatusSuccessFinishNoResult);
1227         } else {
1228           if (from[0])
1229             result.AppendError("<path-prefix> can't be empty\n");
1230           else
1231             result.AppendError("<new-path-prefix> can't be empty\n");
1232           result.SetStatus(eReturnStatusFailed);
1233           return false;
1234         }
1235       }
1236     } else {
1237       result.AppendError("insert requires at least three arguments\n");
1238       result.SetStatus(eReturnStatusFailed);
1239       return result.Succeeded();
1240     }
1241     return result.Succeeded();
1242   }
1243 };
1244 
1245 #pragma mark CommandObjectTargetModulesSearchPathsList
1246 
1247 class CommandObjectTargetModulesSearchPathsList : public CommandObjectParsed {
1248 public:
1249   CommandObjectTargetModulesSearchPathsList(CommandInterpreter &interpreter)
1250       : CommandObjectParsed(interpreter, "target modules search-paths list",
1251                             "List all current image search path substitution "
1252                             "pairs in the current target.",
1253                             "target modules search-paths list",
1254                             eCommandRequiresTarget) {}
1255 
1256   ~CommandObjectTargetModulesSearchPathsList() override = default;
1257 
1258 protected:
1259   bool DoExecute(Args &command, CommandReturnObject &result) override {
1260     Target *target = &GetSelectedTarget();
1261     if (command.GetArgumentCount() != 0) {
1262       result.AppendError("list takes no arguments\n");
1263       result.SetStatus(eReturnStatusFailed);
1264       return result.Succeeded();
1265     }
1266 
1267     target->GetImageSearchPathList().Dump(&result.GetOutputStream());
1268     result.SetStatus(eReturnStatusSuccessFinishResult);
1269     return result.Succeeded();
1270   }
1271 };
1272 
1273 #pragma mark CommandObjectTargetModulesSearchPathsQuery
1274 
1275 class CommandObjectTargetModulesSearchPathsQuery : public CommandObjectParsed {
1276 public:
1277   CommandObjectTargetModulesSearchPathsQuery(CommandInterpreter &interpreter)
1278       : CommandObjectParsed(
1279             interpreter, "target modules search-paths query",
1280             "Transform a path using the first applicable image search path.",
1281             nullptr, eCommandRequiresTarget) {
1282     CommandArgumentEntry arg;
1283     CommandArgumentData path_arg;
1284 
1285     // Define the first (and only) variant of this arg.
1286     path_arg.arg_type = eArgTypeDirectoryName;
1287     path_arg.arg_repetition = eArgRepeatPlain;
1288 
1289     // There is only one variant this argument could be; put it into the
1290     // argument entry.
1291     arg.push_back(path_arg);
1292 
1293     // Push the data for the first argument into the m_arguments vector.
1294     m_arguments.push_back(arg);
1295   }
1296 
1297   ~CommandObjectTargetModulesSearchPathsQuery() override = default;
1298 
1299 protected:
1300   bool DoExecute(Args &command, CommandReturnObject &result) override {
1301     Target *target = &GetSelectedTarget();
1302     if (command.GetArgumentCount() != 1) {
1303       result.AppendError("query requires one argument\n");
1304       result.SetStatus(eReturnStatusFailed);
1305       return result.Succeeded();
1306     }
1307 
1308     ConstString orig(command.GetArgumentAtIndex(0));
1309     ConstString transformed;
1310     if (target->GetImageSearchPathList().RemapPath(orig, transformed))
1311       result.GetOutputStream().Printf("%s\n", transformed.GetCString());
1312     else
1313       result.GetOutputStream().Printf("%s\n", orig.GetCString());
1314 
1315     result.SetStatus(eReturnStatusSuccessFinishResult);
1316     return result.Succeeded();
1317   }
1318 };
1319 
1320 // Static Helper functions
1321 static void DumpModuleArchitecture(Stream &strm, Module *module,
1322                                    bool full_triple, uint32_t width) {
1323   if (module) {
1324     StreamString arch_strm;
1325 
1326     if (full_triple)
1327       module->GetArchitecture().DumpTriple(arch_strm.AsRawOstream());
1328     else
1329       arch_strm.PutCString(module->GetArchitecture().GetArchitectureName());
1330     std::string arch_str = std::string(arch_strm.GetString());
1331 
1332     if (width)
1333       strm.Printf("%-*s", width, arch_str.c_str());
1334     else
1335       strm.PutCString(arch_str);
1336   }
1337 }
1338 
1339 static void DumpModuleUUID(Stream &strm, Module *module) {
1340   if (module && module->GetUUID().IsValid())
1341     module->GetUUID().Dump(&strm);
1342   else
1343     strm.PutCString("                                    ");
1344 }
1345 
1346 static uint32_t DumpCompileUnitLineTable(CommandInterpreter &interpreter,
1347                                          Stream &strm, Module *module,
1348                                          const FileSpec &file_spec,
1349                                          lldb::DescriptionLevel desc_level) {
1350   uint32_t num_matches = 0;
1351   if (module) {
1352     SymbolContextList sc_list;
1353     num_matches = module->ResolveSymbolContextsForFileSpec(
1354         file_spec, 0, false, eSymbolContextCompUnit, sc_list);
1355 
1356     for (uint32_t i = 0; i < num_matches; ++i) {
1357       SymbolContext sc;
1358       if (sc_list.GetContextAtIndex(i, sc)) {
1359         if (i > 0)
1360           strm << "\n\n";
1361 
1362         strm << "Line table for " << sc.comp_unit->GetPrimaryFile() << " in `"
1363              << module->GetFileSpec().GetFilename() << "\n";
1364         LineTable *line_table = sc.comp_unit->GetLineTable();
1365         if (line_table)
1366           line_table->GetDescription(
1367               &strm, interpreter.GetExecutionContext().GetTargetPtr(),
1368               desc_level);
1369         else
1370           strm << "No line table";
1371       }
1372     }
1373   }
1374   return num_matches;
1375 }
1376 
1377 static void DumpFullpath(Stream &strm, const FileSpec *file_spec_ptr,
1378                          uint32_t width) {
1379   if (file_spec_ptr) {
1380     if (width > 0) {
1381       std::string fullpath = file_spec_ptr->GetPath();
1382       strm.Printf("%-*s", width, fullpath.c_str());
1383       return;
1384     } else {
1385       file_spec_ptr->Dump(strm.AsRawOstream());
1386       return;
1387     }
1388   }
1389   // Keep the width spacing correct if things go wrong...
1390   if (width > 0)
1391     strm.Printf("%-*s", width, "");
1392 }
1393 
1394 static void DumpDirectory(Stream &strm, const FileSpec *file_spec_ptr,
1395                           uint32_t width) {
1396   if (file_spec_ptr) {
1397     if (width > 0)
1398       strm.Printf("%-*s", width, file_spec_ptr->GetDirectory().AsCString(""));
1399     else
1400       file_spec_ptr->GetDirectory().Dump(&strm);
1401     return;
1402   }
1403   // Keep the width spacing correct if things go wrong...
1404   if (width > 0)
1405     strm.Printf("%-*s", width, "");
1406 }
1407 
1408 static void DumpBasename(Stream &strm, const FileSpec *file_spec_ptr,
1409                          uint32_t width) {
1410   if (file_spec_ptr) {
1411     if (width > 0)
1412       strm.Printf("%-*s", width, file_spec_ptr->GetFilename().AsCString(""));
1413     else
1414       file_spec_ptr->GetFilename().Dump(&strm);
1415     return;
1416   }
1417   // Keep the width spacing correct if things go wrong...
1418   if (width > 0)
1419     strm.Printf("%-*s", width, "");
1420 }
1421 
1422 static size_t DumpModuleObjfileHeaders(Stream &strm, ModuleList &module_list) {
1423   size_t num_dumped = 0;
1424   std::lock_guard<std::recursive_mutex> guard(module_list.GetMutex());
1425   const size_t num_modules = module_list.GetSize();
1426   if (num_modules > 0) {
1427     strm.Printf("Dumping headers for %" PRIu64 " module(s).\n",
1428                 static_cast<uint64_t>(num_modules));
1429     strm.IndentMore();
1430     for (size_t image_idx = 0; image_idx < num_modules; ++image_idx) {
1431       Module *module = module_list.GetModulePointerAtIndexUnlocked(image_idx);
1432       if (module) {
1433         if (num_dumped++ > 0) {
1434           strm.EOL();
1435           strm.EOL();
1436         }
1437         ObjectFile *objfile = module->GetObjectFile();
1438         if (objfile)
1439           objfile->Dump(&strm);
1440         else {
1441           strm.Format("No object file for module: {0:F}\n",
1442                       module->GetFileSpec());
1443         }
1444       }
1445     }
1446     strm.IndentLess();
1447   }
1448   return num_dumped;
1449 }
1450 
1451 static void DumpModuleSymtab(CommandInterpreter &interpreter, Stream &strm,
1452                              Module *module, SortOrder sort_order,
1453                              Mangled::NamePreference name_preference) {
1454   if (!module)
1455     return;
1456   if (Symtab *symtab = module->GetSymtab())
1457     symtab->Dump(&strm, interpreter.GetExecutionContext().GetTargetPtr(),
1458                  sort_order, name_preference);
1459 }
1460 
1461 static void DumpModuleSections(CommandInterpreter &interpreter, Stream &strm,
1462                                Module *module) {
1463   if (module) {
1464     SectionList *section_list = module->GetSectionList();
1465     if (section_list) {
1466       strm.Printf("Sections for '%s' (%s):\n",
1467                   module->GetSpecificationDescription().c_str(),
1468                   module->GetArchitecture().GetArchitectureName());
1469       strm.IndentMore();
1470       section_list->Dump(&strm,
1471                          interpreter.GetExecutionContext().GetTargetPtr(), true,
1472                          UINT32_MAX);
1473       strm.IndentLess();
1474     }
1475   }
1476 }
1477 
1478 static bool DumpModuleSymbolFile(Stream &strm, Module *module) {
1479   if (module) {
1480     if (SymbolFile *symbol_file = module->GetSymbolFile(true)) {
1481       symbol_file->Dump(strm);
1482       return true;
1483     }
1484   }
1485   return false;
1486 }
1487 
1488 static void DumpAddress(ExecutionContextScope *exe_scope,
1489                         const Address &so_addr, bool verbose, Stream &strm) {
1490   strm.IndentMore();
1491   strm.Indent("    Address: ");
1492   so_addr.Dump(&strm, exe_scope, Address::DumpStyleModuleWithFileAddress);
1493   strm.PutCString(" (");
1494   so_addr.Dump(&strm, exe_scope, Address::DumpStyleSectionNameOffset);
1495   strm.PutCString(")\n");
1496   strm.Indent("    Summary: ");
1497   const uint32_t save_indent = strm.GetIndentLevel();
1498   strm.SetIndentLevel(save_indent + 13);
1499   so_addr.Dump(&strm, exe_scope, Address::DumpStyleResolvedDescription);
1500   strm.SetIndentLevel(save_indent);
1501   // Print out detailed address information when verbose is enabled
1502   if (verbose) {
1503     strm.EOL();
1504     so_addr.Dump(&strm, exe_scope, Address::DumpStyleDetailedSymbolContext);
1505   }
1506   strm.IndentLess();
1507 }
1508 
1509 static bool LookupAddressInModule(CommandInterpreter &interpreter, Stream &strm,
1510                                   Module *module, uint32_t resolve_mask,
1511                                   lldb::addr_t raw_addr, lldb::addr_t offset,
1512                                   bool verbose) {
1513   if (module) {
1514     lldb::addr_t addr = raw_addr - offset;
1515     Address so_addr;
1516     SymbolContext sc;
1517     Target *target = interpreter.GetExecutionContext().GetTargetPtr();
1518     if (target && !target->GetSectionLoadList().IsEmpty()) {
1519       if (!target->GetSectionLoadList().ResolveLoadAddress(addr, so_addr))
1520         return false;
1521       else if (so_addr.GetModule().get() != module)
1522         return false;
1523     } else {
1524       if (!module->ResolveFileAddress(addr, so_addr))
1525         return false;
1526     }
1527 
1528     ExecutionContextScope *exe_scope =
1529         interpreter.GetExecutionContext().GetBestExecutionContextScope();
1530     DumpAddress(exe_scope, so_addr, verbose, strm);
1531     //        strm.IndentMore();
1532     //        strm.Indent ("    Address: ");
1533     //        so_addr.Dump (&strm, exe_scope,
1534     //        Address::DumpStyleModuleWithFileAddress);
1535     //        strm.PutCString (" (");
1536     //        so_addr.Dump (&strm, exe_scope,
1537     //        Address::DumpStyleSectionNameOffset);
1538     //        strm.PutCString (")\n");
1539     //        strm.Indent ("    Summary: ");
1540     //        const uint32_t save_indent = strm.GetIndentLevel ();
1541     //        strm.SetIndentLevel (save_indent + 13);
1542     //        so_addr.Dump (&strm, exe_scope,
1543     //        Address::DumpStyleResolvedDescription);
1544     //        strm.SetIndentLevel (save_indent);
1545     //        // Print out detailed address information when verbose is enabled
1546     //        if (verbose)
1547     //        {
1548     //            strm.EOL();
1549     //            so_addr.Dump (&strm, exe_scope,
1550     //            Address::DumpStyleDetailedSymbolContext);
1551     //        }
1552     //        strm.IndentLess();
1553     return true;
1554   }
1555 
1556   return false;
1557 }
1558 
1559 static uint32_t LookupSymbolInModule(CommandInterpreter &interpreter,
1560                                      Stream &strm, Module *module,
1561                                      const char *name, bool name_is_regex,
1562                                      bool verbose) {
1563   if (!module)
1564     return 0;
1565 
1566   Symtab *symtab = module->GetSymtab();
1567   if (!symtab)
1568     return 0;
1569 
1570   SymbolContext sc;
1571   std::vector<uint32_t> match_indexes;
1572   ConstString symbol_name(name);
1573   uint32_t num_matches = 0;
1574   if (name_is_regex) {
1575     RegularExpression name_regexp(symbol_name.GetStringRef());
1576     num_matches = symtab->AppendSymbolIndexesMatchingRegExAndType(
1577         name_regexp, eSymbolTypeAny, match_indexes);
1578   } else {
1579     num_matches =
1580         symtab->AppendSymbolIndexesWithName(symbol_name, match_indexes);
1581   }
1582 
1583   if (num_matches > 0) {
1584     strm.Indent();
1585     strm.Printf("%u symbols match %s'%s' in ", num_matches,
1586                 name_is_regex ? "the regular expression " : "", name);
1587     DumpFullpath(strm, &module->GetFileSpec(), 0);
1588     strm.PutCString(":\n");
1589     strm.IndentMore();
1590     for (uint32_t i = 0; i < num_matches; ++i) {
1591       Symbol *symbol = symtab->SymbolAtIndex(match_indexes[i]);
1592       if (symbol && symbol->ValueIsAddress()) {
1593         DumpAddress(
1594             interpreter.GetExecutionContext().GetBestExecutionContextScope(),
1595             symbol->GetAddressRef(), verbose, strm);
1596       }
1597     }
1598     strm.IndentLess();
1599   }
1600   return num_matches;
1601 }
1602 
1603 static void DumpSymbolContextList(ExecutionContextScope *exe_scope,
1604                                   Stream &strm, SymbolContextList &sc_list,
1605                                   bool verbose) {
1606   strm.IndentMore();
1607 
1608   const uint32_t num_matches = sc_list.GetSize();
1609 
1610   for (uint32_t i = 0; i < num_matches; ++i) {
1611     SymbolContext sc;
1612     if (sc_list.GetContextAtIndex(i, sc)) {
1613       AddressRange range;
1614 
1615       sc.GetAddressRange(eSymbolContextEverything, 0, true, range);
1616 
1617       DumpAddress(exe_scope, range.GetBaseAddress(), verbose, strm);
1618     }
1619   }
1620   strm.IndentLess();
1621 }
1622 
1623 static size_t LookupFunctionInModule(CommandInterpreter &interpreter,
1624                                      Stream &strm, Module *module,
1625                                      const char *name, bool name_is_regex,
1626                                      bool include_inlines, bool include_symbols,
1627                                      bool verbose) {
1628   if (module && name && name[0]) {
1629     SymbolContextList sc_list;
1630     size_t num_matches = 0;
1631     if (name_is_regex) {
1632       RegularExpression function_name_regex((llvm::StringRef(name)));
1633       module->FindFunctions(function_name_regex, include_symbols,
1634                             include_inlines, sc_list);
1635     } else {
1636       ConstString function_name(name);
1637       module->FindFunctions(function_name, CompilerDeclContext(),
1638                             eFunctionNameTypeAuto, include_symbols,
1639                             include_inlines, sc_list);
1640     }
1641     num_matches = sc_list.GetSize();
1642     if (num_matches) {
1643       strm.Indent();
1644       strm.Printf("%" PRIu64 " match%s found in ", (uint64_t)num_matches,
1645                   num_matches > 1 ? "es" : "");
1646       DumpFullpath(strm, &module->GetFileSpec(), 0);
1647       strm.PutCString(":\n");
1648       DumpSymbolContextList(
1649           interpreter.GetExecutionContext().GetBestExecutionContextScope(),
1650           strm, sc_list, verbose);
1651     }
1652     return num_matches;
1653   }
1654   return 0;
1655 }
1656 
1657 static size_t LookupTypeInModule(CommandInterpreter &interpreter, Stream &strm,
1658                                  Module *module, const char *name_cstr,
1659                                  bool name_is_regex) {
1660   TypeList type_list;
1661   if (module && name_cstr && name_cstr[0]) {
1662     const uint32_t max_num_matches = UINT32_MAX;
1663     size_t num_matches = 0;
1664     bool name_is_fully_qualified = false;
1665 
1666     ConstString name(name_cstr);
1667     llvm::DenseSet<lldb_private::SymbolFile *> searched_symbol_files;
1668     module->FindTypes(name, name_is_fully_qualified, max_num_matches,
1669                       searched_symbol_files, type_list);
1670 
1671     if (type_list.Empty())
1672       return 0;
1673 
1674     strm.Indent();
1675     strm.Printf("%" PRIu64 " match%s found in ", (uint64_t)num_matches,
1676                 num_matches > 1 ? "es" : "");
1677     DumpFullpath(strm, &module->GetFileSpec(), 0);
1678     strm.PutCString(":\n");
1679     for (TypeSP type_sp : type_list.Types()) {
1680       if (!type_sp)
1681         continue;
1682       // Resolve the clang type so that any forward references to types
1683       // that haven't yet been parsed will get parsed.
1684       type_sp->GetFullCompilerType();
1685       type_sp->GetDescription(&strm, eDescriptionLevelFull, true);
1686       // Print all typedef chains
1687       TypeSP typedef_type_sp(type_sp);
1688       TypeSP typedefed_type_sp(typedef_type_sp->GetTypedefType());
1689       while (typedefed_type_sp) {
1690         strm.EOL();
1691         strm.Printf("     typedef '%s': ",
1692                     typedef_type_sp->GetName().GetCString());
1693         typedefed_type_sp->GetFullCompilerType();
1694         typedefed_type_sp->GetDescription(&strm, eDescriptionLevelFull, true);
1695         typedef_type_sp = typedefed_type_sp;
1696         typedefed_type_sp = typedef_type_sp->GetTypedefType();
1697       }
1698     }
1699     strm.EOL();
1700   }
1701   return type_list.GetSize();
1702 }
1703 
1704 static size_t LookupTypeHere(CommandInterpreter &interpreter, Stream &strm,
1705                              Module &module, const char *name_cstr,
1706                              bool name_is_regex) {
1707   TypeList type_list;
1708   const uint32_t max_num_matches = UINT32_MAX;
1709   bool name_is_fully_qualified = false;
1710 
1711   ConstString name(name_cstr);
1712   llvm::DenseSet<SymbolFile *> searched_symbol_files;
1713   module.FindTypes(name, name_is_fully_qualified, max_num_matches,
1714                    searched_symbol_files, type_list);
1715 
1716   if (type_list.Empty())
1717     return 0;
1718 
1719   strm.Indent();
1720   strm.PutCString("Best match found in ");
1721   DumpFullpath(strm, &module.GetFileSpec(), 0);
1722   strm.PutCString(":\n");
1723 
1724   TypeSP type_sp(type_list.GetTypeAtIndex(0));
1725   if (type_sp) {
1726     // Resolve the clang type so that any forward references to types that
1727     // haven't yet been parsed will get parsed.
1728     type_sp->GetFullCompilerType();
1729     type_sp->GetDescription(&strm, eDescriptionLevelFull, true);
1730     // Print all typedef chains
1731     TypeSP typedef_type_sp(type_sp);
1732     TypeSP typedefed_type_sp(typedef_type_sp->GetTypedefType());
1733     while (typedefed_type_sp) {
1734       strm.EOL();
1735       strm.Printf("     typedef '%s': ",
1736                   typedef_type_sp->GetName().GetCString());
1737       typedefed_type_sp->GetFullCompilerType();
1738       typedefed_type_sp->GetDescription(&strm, eDescriptionLevelFull, true);
1739       typedef_type_sp = typedefed_type_sp;
1740       typedefed_type_sp = typedef_type_sp->GetTypedefType();
1741     }
1742   }
1743   strm.EOL();
1744   return type_list.GetSize();
1745 }
1746 
1747 static uint32_t LookupFileAndLineInModule(CommandInterpreter &interpreter,
1748                                           Stream &strm, Module *module,
1749                                           const FileSpec &file_spec,
1750                                           uint32_t line, bool check_inlines,
1751                                           bool verbose) {
1752   if (module && file_spec) {
1753     SymbolContextList sc_list;
1754     const uint32_t num_matches = module->ResolveSymbolContextsForFileSpec(
1755         file_spec, line, check_inlines, eSymbolContextEverything, sc_list);
1756     if (num_matches > 0) {
1757       strm.Indent();
1758       strm.Printf("%u match%s found in ", num_matches,
1759                   num_matches > 1 ? "es" : "");
1760       strm << file_spec;
1761       if (line > 0)
1762         strm.Printf(":%u", line);
1763       strm << " in ";
1764       DumpFullpath(strm, &module->GetFileSpec(), 0);
1765       strm.PutCString(":\n");
1766       DumpSymbolContextList(
1767           interpreter.GetExecutionContext().GetBestExecutionContextScope(),
1768           strm, sc_list, verbose);
1769       return num_matches;
1770     }
1771   }
1772   return 0;
1773 }
1774 
1775 static size_t FindModulesByName(Target *target, const char *module_name,
1776                                 ModuleList &module_list,
1777                                 bool check_global_list) {
1778   FileSpec module_file_spec(module_name);
1779   ModuleSpec module_spec(module_file_spec);
1780 
1781   const size_t initial_size = module_list.GetSize();
1782 
1783   if (check_global_list) {
1784     // Check the global list
1785     std::lock_guard<std::recursive_mutex> guard(
1786         Module::GetAllocationModuleCollectionMutex());
1787     const size_t num_modules = Module::GetNumberAllocatedModules();
1788     ModuleSP module_sp;
1789     for (size_t image_idx = 0; image_idx < num_modules; ++image_idx) {
1790       Module *module = Module::GetAllocatedModuleAtIndex(image_idx);
1791 
1792       if (module) {
1793         if (module->MatchesModuleSpec(module_spec)) {
1794           module_sp = module->shared_from_this();
1795           module_list.AppendIfNeeded(module_sp);
1796         }
1797       }
1798     }
1799   } else {
1800     if (target) {
1801       target->GetImages().FindModules(module_spec, module_list);
1802       const size_t num_matches = module_list.GetSize();
1803 
1804       // Not found in our module list for our target, check the main shared
1805       // module list in case it is a extra file used somewhere else
1806       if (num_matches == 0) {
1807         module_spec.GetArchitecture() = target->GetArchitecture();
1808         ModuleList::FindSharedModules(module_spec, module_list);
1809       }
1810     } else {
1811       ModuleList::FindSharedModules(module_spec, module_list);
1812     }
1813   }
1814 
1815   return module_list.GetSize() - initial_size;
1816 }
1817 
1818 #pragma mark CommandObjectTargetModulesModuleAutoComplete
1819 
1820 // A base command object class that can auto complete with module file
1821 // paths
1822 
1823 class CommandObjectTargetModulesModuleAutoComplete
1824     : public CommandObjectParsed {
1825 public:
1826   CommandObjectTargetModulesModuleAutoComplete(CommandInterpreter &interpreter,
1827                                                const char *name,
1828                                                const char *help,
1829                                                const char *syntax,
1830                                                uint32_t flags = 0)
1831       : CommandObjectParsed(interpreter, name, help, syntax, flags) {
1832     CommandArgumentEntry arg;
1833     CommandArgumentData file_arg;
1834 
1835     // Define the first (and only) variant of this arg.
1836     file_arg.arg_type = eArgTypeFilename;
1837     file_arg.arg_repetition = eArgRepeatStar;
1838 
1839     // There is only one variant this argument could be; put it into the
1840     // argument entry.
1841     arg.push_back(file_arg);
1842 
1843     // Push the data for the first argument into the m_arguments vector.
1844     m_arguments.push_back(arg);
1845   }
1846 
1847   ~CommandObjectTargetModulesModuleAutoComplete() override = default;
1848 
1849   void
1850   HandleArgumentCompletion(CompletionRequest &request,
1851                            OptionElementVector &opt_element_vector) override {
1852     CommandCompletions::InvokeCommonCompletionCallbacks(
1853         GetCommandInterpreter(), CommandCompletions::eModuleCompletion, request,
1854         nullptr);
1855   }
1856 };
1857 
1858 #pragma mark CommandObjectTargetModulesSourceFileAutoComplete
1859 
1860 // A base command object class that can auto complete with module source
1861 // file paths
1862 
1863 class CommandObjectTargetModulesSourceFileAutoComplete
1864     : public CommandObjectParsed {
1865 public:
1866   CommandObjectTargetModulesSourceFileAutoComplete(
1867       CommandInterpreter &interpreter, const char *name, const char *help,
1868       const char *syntax, uint32_t flags)
1869       : CommandObjectParsed(interpreter, name, help, syntax, flags) {
1870     CommandArgumentEntry arg;
1871     CommandArgumentData source_file_arg;
1872 
1873     // Define the first (and only) variant of this arg.
1874     source_file_arg.arg_type = eArgTypeSourceFile;
1875     source_file_arg.arg_repetition = eArgRepeatPlus;
1876 
1877     // There is only one variant this argument could be; put it into the
1878     // argument entry.
1879     arg.push_back(source_file_arg);
1880 
1881     // Push the data for the first argument into the m_arguments vector.
1882     m_arguments.push_back(arg);
1883   }
1884 
1885   ~CommandObjectTargetModulesSourceFileAutoComplete() override = default;
1886 
1887   void
1888   HandleArgumentCompletion(CompletionRequest &request,
1889                            OptionElementVector &opt_element_vector) override {
1890     CommandCompletions::InvokeCommonCompletionCallbacks(
1891         GetCommandInterpreter(), CommandCompletions::eSourceFileCompletion,
1892         request, nullptr);
1893   }
1894 };
1895 
1896 #pragma mark CommandObjectTargetModulesDumpObjfile
1897 
1898 class CommandObjectTargetModulesDumpObjfile
1899     : public CommandObjectTargetModulesModuleAutoComplete {
1900 public:
1901   CommandObjectTargetModulesDumpObjfile(CommandInterpreter &interpreter)
1902       : CommandObjectTargetModulesModuleAutoComplete(
1903             interpreter, "target modules dump objfile",
1904             "Dump the object file headers from one or more target modules.",
1905             nullptr, eCommandRequiresTarget) {}
1906 
1907   ~CommandObjectTargetModulesDumpObjfile() override = default;
1908 
1909 protected:
1910   bool DoExecute(Args &command, CommandReturnObject &result) override {
1911     Target *target = &GetSelectedTarget();
1912 
1913     uint32_t addr_byte_size = target->GetArchitecture().GetAddressByteSize();
1914     result.GetOutputStream().SetAddressByteSize(addr_byte_size);
1915     result.GetErrorStream().SetAddressByteSize(addr_byte_size);
1916 
1917     size_t num_dumped = 0;
1918     if (command.GetArgumentCount() == 0) {
1919       // Dump all headers for all modules images
1920       num_dumped = DumpModuleObjfileHeaders(result.GetOutputStream(),
1921                                             target->GetImages());
1922       if (num_dumped == 0) {
1923         result.AppendError("the target has no associated executable images");
1924         result.SetStatus(eReturnStatusFailed);
1925       }
1926     } else {
1927       // Find the modules that match the basename or full path.
1928       ModuleList module_list;
1929       const char *arg_cstr;
1930       for (int arg_idx = 0;
1931            (arg_cstr = command.GetArgumentAtIndex(arg_idx)) != nullptr;
1932            ++arg_idx) {
1933         size_t num_matched =
1934             FindModulesByName(target, arg_cstr, module_list, true);
1935         if (num_matched == 0) {
1936           result.AppendWarningWithFormat(
1937               "Unable to find an image that matches '%s'.\n", arg_cstr);
1938         }
1939       }
1940       // Dump all the modules we found.
1941       num_dumped =
1942           DumpModuleObjfileHeaders(result.GetOutputStream(), module_list);
1943     }
1944 
1945     if (num_dumped > 0) {
1946       result.SetStatus(eReturnStatusSuccessFinishResult);
1947     } else {
1948       result.AppendError("no matching executable images found");
1949       result.SetStatus(eReturnStatusFailed);
1950     }
1951     return result.Succeeded();
1952   }
1953 };
1954 
1955 #pragma mark CommandObjectTargetModulesDumpSymtab
1956 
1957 static constexpr OptionEnumValueElement g_sort_option_enumeration[] = {
1958     {
1959         eSortOrderNone,
1960         "none",
1961         "No sorting, use the original symbol table order.",
1962     },
1963     {
1964         eSortOrderByAddress,
1965         "address",
1966         "Sort output by symbol address.",
1967     },
1968     {
1969         eSortOrderByName,
1970         "name",
1971         "Sort output by symbol name.",
1972     },
1973 };
1974 
1975 #define LLDB_OPTIONS_target_modules_dump_symtab
1976 #include "CommandOptions.inc"
1977 
1978 class CommandObjectTargetModulesDumpSymtab
1979     : public CommandObjectTargetModulesModuleAutoComplete {
1980 public:
1981   CommandObjectTargetModulesDumpSymtab(CommandInterpreter &interpreter)
1982       : CommandObjectTargetModulesModuleAutoComplete(
1983             interpreter, "target modules dump symtab",
1984             "Dump the symbol table from one or more target modules.", nullptr,
1985             eCommandRequiresTarget),
1986         m_options() {}
1987 
1988   ~CommandObjectTargetModulesDumpSymtab() override = default;
1989 
1990   Options *GetOptions() override { return &m_options; }
1991 
1992   class CommandOptions : public Options {
1993   public:
1994     CommandOptions() : Options(), m_sort_order(eSortOrderNone) {}
1995 
1996     ~CommandOptions() override = default;
1997 
1998     Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
1999                           ExecutionContext *execution_context) override {
2000       Status error;
2001       const int short_option = m_getopt_table[option_idx].val;
2002 
2003       switch (short_option) {
2004       case 'm':
2005         m_prefer_mangled.SetCurrentValue(true);
2006         m_prefer_mangled.SetOptionWasSet();
2007         break;
2008 
2009       case 's':
2010         m_sort_order = (SortOrder)OptionArgParser::ToOptionEnum(
2011             option_arg, GetDefinitions()[option_idx].enum_values,
2012             eSortOrderNone, error);
2013         break;
2014 
2015       default:
2016         llvm_unreachable("Unimplemented option");
2017       }
2018       return error;
2019     }
2020 
2021     void OptionParsingStarting(ExecutionContext *execution_context) override {
2022       m_sort_order = eSortOrderNone;
2023       m_prefer_mangled.Clear();
2024     }
2025 
2026     llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
2027       return llvm::makeArrayRef(g_target_modules_dump_symtab_options);
2028     }
2029 
2030     SortOrder m_sort_order;
2031     OptionValueBoolean m_prefer_mangled = {false, false};
2032   };
2033 
2034 protected:
2035   bool DoExecute(Args &command, CommandReturnObject &result) override {
2036     Target *target = &GetSelectedTarget();
2037     uint32_t num_dumped = 0;
2038     Mangled::NamePreference name_preference =
2039         (m_options.m_prefer_mangled ? Mangled::ePreferMangled
2040                                     : Mangled::ePreferDemangled);
2041 
2042     uint32_t addr_byte_size = target->GetArchitecture().GetAddressByteSize();
2043     result.GetOutputStream().SetAddressByteSize(addr_byte_size);
2044     result.GetErrorStream().SetAddressByteSize(addr_byte_size);
2045 
2046     if (command.GetArgumentCount() == 0) {
2047       // Dump all sections for all modules images
2048       std::lock_guard<std::recursive_mutex> guard(
2049           target->GetImages().GetMutex());
2050       const size_t num_modules = target->GetImages().GetSize();
2051       if (num_modules > 0) {
2052         result.GetOutputStream().Printf("Dumping symbol table for %" PRIu64
2053                                         " modules.\n",
2054                                         (uint64_t)num_modules);
2055         for (size_t image_idx = 0; image_idx < num_modules; ++image_idx) {
2056           if (num_dumped > 0) {
2057             result.GetOutputStream().EOL();
2058             result.GetOutputStream().EOL();
2059           }
2060           if (m_interpreter.WasInterrupted())
2061             break;
2062           num_dumped++;
2063           DumpModuleSymtab(
2064               m_interpreter, result.GetOutputStream(),
2065               target->GetImages().GetModulePointerAtIndexUnlocked(image_idx),
2066               m_options.m_sort_order, name_preference);
2067         }
2068       } else {
2069         result.AppendError("the target has no associated executable images");
2070         result.SetStatus(eReturnStatusFailed);
2071         return false;
2072       }
2073     } else {
2074       // Dump specified images (by basename or fullpath)
2075       const char *arg_cstr;
2076       for (int arg_idx = 0;
2077            (arg_cstr = command.GetArgumentAtIndex(arg_idx)) != nullptr;
2078            ++arg_idx) {
2079         ModuleList module_list;
2080         const size_t num_matches =
2081             FindModulesByName(target, arg_cstr, module_list, true);
2082         if (num_matches > 0) {
2083           for (size_t i = 0; i < num_matches; ++i) {
2084             Module *module = module_list.GetModulePointerAtIndex(i);
2085             if (module) {
2086               if (num_dumped > 0) {
2087                 result.GetOutputStream().EOL();
2088                 result.GetOutputStream().EOL();
2089               }
2090               if (m_interpreter.WasInterrupted())
2091                 break;
2092               num_dumped++;
2093               DumpModuleSymtab(m_interpreter, result.GetOutputStream(), module,
2094                                m_options.m_sort_order, name_preference);
2095             }
2096           }
2097         } else
2098           result.AppendWarningWithFormat(
2099               "Unable to find an image that matches '%s'.\n", arg_cstr);
2100       }
2101     }
2102 
2103     if (num_dumped > 0)
2104       result.SetStatus(eReturnStatusSuccessFinishResult);
2105     else {
2106       result.AppendError("no matching executable images found");
2107       result.SetStatus(eReturnStatusFailed);
2108     }
2109     return result.Succeeded();
2110   }
2111 
2112   CommandOptions m_options;
2113 };
2114 
2115 #pragma mark CommandObjectTargetModulesDumpSections
2116 
2117 // Image section dumping command
2118 
2119 class CommandObjectTargetModulesDumpSections
2120     : public CommandObjectTargetModulesModuleAutoComplete {
2121 public:
2122   CommandObjectTargetModulesDumpSections(CommandInterpreter &interpreter)
2123       : CommandObjectTargetModulesModuleAutoComplete(
2124             interpreter, "target modules dump sections",
2125             "Dump the sections from one or more target modules.",
2126             //"target modules dump sections [<file1> ...]")
2127             nullptr, eCommandRequiresTarget) {}
2128 
2129   ~CommandObjectTargetModulesDumpSections() override = default;
2130 
2131 protected:
2132   bool DoExecute(Args &command, CommandReturnObject &result) override {
2133     Target *target = &GetSelectedTarget();
2134     uint32_t num_dumped = 0;
2135 
2136     uint32_t addr_byte_size = target->GetArchitecture().GetAddressByteSize();
2137     result.GetOutputStream().SetAddressByteSize(addr_byte_size);
2138     result.GetErrorStream().SetAddressByteSize(addr_byte_size);
2139 
2140     if (command.GetArgumentCount() == 0) {
2141       // Dump all sections for all modules images
2142       const size_t num_modules = target->GetImages().GetSize();
2143       if (num_modules > 0) {
2144         result.GetOutputStream().Printf("Dumping sections for %" PRIu64
2145                                         " modules.\n",
2146                                         (uint64_t)num_modules);
2147         for (size_t image_idx = 0; image_idx < num_modules; ++image_idx) {
2148           if (m_interpreter.WasInterrupted())
2149             break;
2150           num_dumped++;
2151           DumpModuleSections(
2152               m_interpreter, result.GetOutputStream(),
2153               target->GetImages().GetModulePointerAtIndex(image_idx));
2154         }
2155       } else {
2156         result.AppendError("the target has no associated executable images");
2157         result.SetStatus(eReturnStatusFailed);
2158         return false;
2159       }
2160     } else {
2161       // Dump specified images (by basename or fullpath)
2162       const char *arg_cstr;
2163       for (int arg_idx = 0;
2164            (arg_cstr = command.GetArgumentAtIndex(arg_idx)) != nullptr;
2165            ++arg_idx) {
2166         ModuleList module_list;
2167         const size_t num_matches =
2168             FindModulesByName(target, arg_cstr, module_list, true);
2169         if (num_matches > 0) {
2170           for (size_t i = 0; i < num_matches; ++i) {
2171             if (m_interpreter.WasInterrupted())
2172               break;
2173             Module *module = module_list.GetModulePointerAtIndex(i);
2174             if (module) {
2175               num_dumped++;
2176               DumpModuleSections(m_interpreter, result.GetOutputStream(),
2177                                  module);
2178             }
2179           }
2180         } else {
2181           // Check the global list
2182           std::lock_guard<std::recursive_mutex> guard(
2183               Module::GetAllocationModuleCollectionMutex());
2184 
2185           result.AppendWarningWithFormat(
2186               "Unable to find an image that matches '%s'.\n", arg_cstr);
2187         }
2188       }
2189     }
2190 
2191     if (num_dumped > 0)
2192       result.SetStatus(eReturnStatusSuccessFinishResult);
2193     else {
2194       result.AppendError("no matching executable images found");
2195       result.SetStatus(eReturnStatusFailed);
2196     }
2197     return result.Succeeded();
2198   }
2199 };
2200 
2201 #pragma mark CommandObjectTargetModulesDumpClangAST
2202 
2203 // Clang AST dumping command
2204 
2205 class CommandObjectTargetModulesDumpClangAST
2206     : public CommandObjectTargetModulesModuleAutoComplete {
2207 public:
2208   CommandObjectTargetModulesDumpClangAST(CommandInterpreter &interpreter)
2209       : CommandObjectTargetModulesModuleAutoComplete(
2210             interpreter, "target modules dump ast",
2211             "Dump the clang ast for a given module's symbol file.",
2212             //"target modules dump ast [<file1> ...]")
2213             nullptr, eCommandRequiresTarget) {}
2214 
2215   ~CommandObjectTargetModulesDumpClangAST() override = default;
2216 
2217 protected:
2218   bool DoExecute(Args &command, CommandReturnObject &result) override {
2219     Target *target = &GetSelectedTarget();
2220 
2221     const size_t num_modules = target->GetImages().GetSize();
2222     if (num_modules == 0) {
2223       result.AppendError("the target has no associated executable images");
2224       result.SetStatus(eReturnStatusFailed);
2225       return false;
2226     }
2227 
2228     if (command.GetArgumentCount() == 0) {
2229       // Dump all ASTs for all modules images
2230       result.GetOutputStream().Printf("Dumping clang ast for %" PRIu64
2231                                       " modules.\n",
2232                                       (uint64_t)num_modules);
2233       for (size_t image_idx = 0; image_idx < num_modules; ++image_idx) {
2234         if (m_interpreter.WasInterrupted())
2235           break;
2236         Module *m = target->GetImages().GetModulePointerAtIndex(image_idx);
2237         if (SymbolFile *sf = m->GetSymbolFile())
2238           sf->DumpClangAST(result.GetOutputStream());
2239       }
2240       result.SetStatus(eReturnStatusSuccessFinishResult);
2241       return true;
2242     }
2243 
2244     // Dump specified ASTs (by basename or fullpath)
2245     for (const Args::ArgEntry &arg : command.entries()) {
2246       ModuleList module_list;
2247       const size_t num_matches =
2248           FindModulesByName(target, arg.c_str(), module_list, true);
2249       if (num_matches == 0) {
2250         // Check the global list
2251         std::lock_guard<std::recursive_mutex> guard(
2252             Module::GetAllocationModuleCollectionMutex());
2253 
2254         result.AppendWarningWithFormat(
2255             "Unable to find an image that matches '%s'.\n", arg.c_str());
2256         continue;
2257       }
2258 
2259       for (size_t i = 0; i < num_matches; ++i) {
2260         if (m_interpreter.WasInterrupted())
2261           break;
2262         Module *m = module_list.GetModulePointerAtIndex(i);
2263         if (SymbolFile *sf = m->GetSymbolFile())
2264           sf->DumpClangAST(result.GetOutputStream());
2265       }
2266     }
2267     result.SetStatus(eReturnStatusSuccessFinishResult);
2268     return true;
2269   }
2270 };
2271 
2272 #pragma mark CommandObjectTargetModulesDumpSymfile
2273 
2274 // Image debug symbol dumping command
2275 
2276 class CommandObjectTargetModulesDumpSymfile
2277     : public CommandObjectTargetModulesModuleAutoComplete {
2278 public:
2279   CommandObjectTargetModulesDumpSymfile(CommandInterpreter &interpreter)
2280       : CommandObjectTargetModulesModuleAutoComplete(
2281             interpreter, "target modules dump symfile",
2282             "Dump the debug symbol file for one or more target modules.",
2283             //"target modules dump symfile [<file1> ...]")
2284             nullptr, eCommandRequiresTarget) {}
2285 
2286   ~CommandObjectTargetModulesDumpSymfile() override = default;
2287 
2288 protected:
2289   bool DoExecute(Args &command, CommandReturnObject &result) override {
2290     Target *target = &GetSelectedTarget();
2291     uint32_t num_dumped = 0;
2292 
2293     uint32_t addr_byte_size = target->GetArchitecture().GetAddressByteSize();
2294     result.GetOutputStream().SetAddressByteSize(addr_byte_size);
2295     result.GetErrorStream().SetAddressByteSize(addr_byte_size);
2296 
2297     if (command.GetArgumentCount() == 0) {
2298       // Dump all sections for all modules images
2299       const ModuleList &target_modules = target->GetImages();
2300       std::lock_guard<std::recursive_mutex> guard(target_modules.GetMutex());
2301       const size_t num_modules = target_modules.GetSize();
2302       if (num_modules > 0) {
2303         result.GetOutputStream().Printf("Dumping debug symbols for %" PRIu64
2304                                         " modules.\n",
2305                                         (uint64_t)num_modules);
2306         for (uint32_t image_idx = 0; image_idx < num_modules; ++image_idx) {
2307           if (m_interpreter.WasInterrupted())
2308             break;
2309           if (DumpModuleSymbolFile(
2310                   result.GetOutputStream(),
2311                   target_modules.GetModulePointerAtIndexUnlocked(image_idx)))
2312             num_dumped++;
2313         }
2314       } else {
2315         result.AppendError("the target has no associated executable images");
2316         result.SetStatus(eReturnStatusFailed);
2317         return false;
2318       }
2319     } else {
2320       // Dump specified images (by basename or fullpath)
2321       const char *arg_cstr;
2322       for (int arg_idx = 0;
2323            (arg_cstr = command.GetArgumentAtIndex(arg_idx)) != nullptr;
2324            ++arg_idx) {
2325         ModuleList module_list;
2326         const size_t num_matches =
2327             FindModulesByName(target, arg_cstr, module_list, true);
2328         if (num_matches > 0) {
2329           for (size_t i = 0; i < num_matches; ++i) {
2330             if (m_interpreter.WasInterrupted())
2331               break;
2332             Module *module = module_list.GetModulePointerAtIndex(i);
2333             if (module) {
2334               if (DumpModuleSymbolFile(result.GetOutputStream(), module))
2335                 num_dumped++;
2336             }
2337           }
2338         } else
2339           result.AppendWarningWithFormat(
2340               "Unable to find an image that matches '%s'.\n", arg_cstr);
2341       }
2342     }
2343 
2344     if (num_dumped > 0)
2345       result.SetStatus(eReturnStatusSuccessFinishResult);
2346     else {
2347       result.AppendError("no matching executable images found");
2348       result.SetStatus(eReturnStatusFailed);
2349     }
2350     return result.Succeeded();
2351   }
2352 };
2353 
2354 #pragma mark CommandObjectTargetModulesDumpLineTable
2355 #define LLDB_OPTIONS_target_modules_dump
2356 #include "CommandOptions.inc"
2357 
2358 // Image debug line table dumping command
2359 
2360 class CommandObjectTargetModulesDumpLineTable
2361     : public CommandObjectTargetModulesSourceFileAutoComplete {
2362 public:
2363   CommandObjectTargetModulesDumpLineTable(CommandInterpreter &interpreter)
2364       : CommandObjectTargetModulesSourceFileAutoComplete(
2365             interpreter, "target modules dump line-table",
2366             "Dump the line table for one or more compilation units.", nullptr,
2367             eCommandRequiresTarget) {}
2368 
2369   ~CommandObjectTargetModulesDumpLineTable() override = default;
2370 
2371   Options *GetOptions() override { return &m_options; }
2372 
2373 protected:
2374   bool DoExecute(Args &command, CommandReturnObject &result) override {
2375     Target *target = m_exe_ctx.GetTargetPtr();
2376     uint32_t total_num_dumped = 0;
2377 
2378     uint32_t addr_byte_size = target->GetArchitecture().GetAddressByteSize();
2379     result.GetOutputStream().SetAddressByteSize(addr_byte_size);
2380     result.GetErrorStream().SetAddressByteSize(addr_byte_size);
2381 
2382     if (command.GetArgumentCount() == 0) {
2383       result.AppendError("file option must be specified.");
2384       result.SetStatus(eReturnStatusFailed);
2385       return result.Succeeded();
2386     } else {
2387       // Dump specified images (by basename or fullpath)
2388       const char *arg_cstr;
2389       for (int arg_idx = 0;
2390            (arg_cstr = command.GetArgumentAtIndex(arg_idx)) != nullptr;
2391            ++arg_idx) {
2392         FileSpec file_spec(arg_cstr);
2393 
2394         const ModuleList &target_modules = target->GetImages();
2395         std::lock_guard<std::recursive_mutex> guard(target_modules.GetMutex());
2396         const size_t num_modules = target_modules.GetSize();
2397         if (num_modules > 0) {
2398           uint32_t num_dumped = 0;
2399           for (uint32_t i = 0; i < num_modules; ++i) {
2400             if (m_interpreter.WasInterrupted())
2401               break;
2402             if (DumpCompileUnitLineTable(
2403                     m_interpreter, result.GetOutputStream(),
2404                     target_modules.GetModulePointerAtIndexUnlocked(i),
2405                     file_spec,
2406                     m_options.m_verbose ? eDescriptionLevelFull
2407                                         : eDescriptionLevelBrief))
2408               num_dumped++;
2409           }
2410           if (num_dumped == 0)
2411             result.AppendWarningWithFormat(
2412                 "No source filenames matched '%s'.\n", arg_cstr);
2413           else
2414             total_num_dumped += num_dumped;
2415         }
2416       }
2417     }
2418 
2419     if (total_num_dumped > 0)
2420       result.SetStatus(eReturnStatusSuccessFinishResult);
2421     else {
2422       result.AppendError("no source filenames matched any command arguments");
2423       result.SetStatus(eReturnStatusFailed);
2424     }
2425     return result.Succeeded();
2426   }
2427 
2428   class CommandOptions : public Options {
2429   public:
2430     CommandOptions() : Options() { OptionParsingStarting(nullptr); }
2431 
2432     Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
2433                           ExecutionContext *execution_context) override {
2434       assert(option_idx == 0 && "We only have one option.");
2435       m_verbose = true;
2436 
2437       return Status();
2438     }
2439 
2440     void OptionParsingStarting(ExecutionContext *execution_context) override {
2441       m_verbose = false;
2442     }
2443 
2444     llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
2445       return llvm::makeArrayRef(g_target_modules_dump_options);
2446     }
2447 
2448     bool m_verbose;
2449   };
2450 
2451   CommandOptions m_options;
2452 };
2453 
2454 #pragma mark CommandObjectTargetModulesDump
2455 
2456 // Dump multi-word command for target modules
2457 
2458 class CommandObjectTargetModulesDump : public CommandObjectMultiword {
2459 public:
2460   // Constructors and Destructors
2461   CommandObjectTargetModulesDump(CommandInterpreter &interpreter)
2462       : CommandObjectMultiword(
2463             interpreter, "target modules dump",
2464             "Commands for dumping information about one or "
2465             "more target modules.",
2466             "target modules dump "
2467             "[headers|symtab|sections|ast|symfile|line-table] "
2468             "[<file1> <file2> ...]") {
2469     LoadSubCommand("objfile",
2470                    CommandObjectSP(
2471                        new CommandObjectTargetModulesDumpObjfile(interpreter)));
2472     LoadSubCommand(
2473         "symtab",
2474         CommandObjectSP(new CommandObjectTargetModulesDumpSymtab(interpreter)));
2475     LoadSubCommand("sections",
2476                    CommandObjectSP(new CommandObjectTargetModulesDumpSections(
2477                        interpreter)));
2478     LoadSubCommand("symfile",
2479                    CommandObjectSP(
2480                        new CommandObjectTargetModulesDumpSymfile(interpreter)));
2481     LoadSubCommand(
2482         "ast", CommandObjectSP(
2483                    new CommandObjectTargetModulesDumpClangAST(interpreter)));
2484     LoadSubCommand("line-table",
2485                    CommandObjectSP(new CommandObjectTargetModulesDumpLineTable(
2486                        interpreter)));
2487   }
2488 
2489   ~CommandObjectTargetModulesDump() override = default;
2490 };
2491 
2492 class CommandObjectTargetModulesAdd : public CommandObjectParsed {
2493 public:
2494   CommandObjectTargetModulesAdd(CommandInterpreter &interpreter)
2495       : CommandObjectParsed(interpreter, "target modules add",
2496                             "Add a new module to the current target's modules.",
2497                             "target modules add [<module>]",
2498                             eCommandRequiresTarget),
2499         m_option_group(), m_symbol_file(LLDB_OPT_SET_1, false, "symfile", 's',
2500                                         0, eArgTypeFilename,
2501                                         "Fullpath to a stand alone debug "
2502                                         "symbols file for when debug symbols "
2503                                         "are not in the executable.") {
2504     m_option_group.Append(&m_uuid_option_group, LLDB_OPT_SET_ALL,
2505                           LLDB_OPT_SET_1);
2506     m_option_group.Append(&m_symbol_file, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
2507     m_option_group.Finalize();
2508   }
2509 
2510   ~CommandObjectTargetModulesAdd() override = default;
2511 
2512   Options *GetOptions() override { return &m_option_group; }
2513 
2514   void
2515   HandleArgumentCompletion(CompletionRequest &request,
2516                            OptionElementVector &opt_element_vector) override {
2517     CommandCompletions::InvokeCommonCompletionCallbacks(
2518         GetCommandInterpreter(), CommandCompletions::eDiskFileCompletion,
2519         request, nullptr);
2520   }
2521 
2522 protected:
2523   OptionGroupOptions m_option_group;
2524   OptionGroupUUID m_uuid_option_group;
2525   OptionGroupFile m_symbol_file;
2526 
2527   bool DoExecute(Args &args, CommandReturnObject &result) override {
2528     Target *target = &GetSelectedTarget();
2529     bool flush = false;
2530 
2531     const size_t argc = args.GetArgumentCount();
2532     if (argc == 0) {
2533       if (m_uuid_option_group.GetOptionValue().OptionWasSet()) {
2534         // We are given a UUID only, go locate the file
2535         ModuleSpec module_spec;
2536         module_spec.GetUUID() =
2537             m_uuid_option_group.GetOptionValue().GetCurrentValue();
2538         if (m_symbol_file.GetOptionValue().OptionWasSet())
2539           module_spec.GetSymbolFileSpec() =
2540               m_symbol_file.GetOptionValue().GetCurrentValue();
2541         if (Symbols::DownloadObjectAndSymbolFile(module_spec)) {
2542           ModuleSP module_sp(
2543               target->GetOrCreateModule(module_spec, true /* notify */));
2544           if (module_sp) {
2545             result.SetStatus(eReturnStatusSuccessFinishResult);
2546             return true;
2547           } else {
2548             StreamString strm;
2549             module_spec.GetUUID().Dump(&strm);
2550             if (module_spec.GetFileSpec()) {
2551               if (module_spec.GetSymbolFileSpec()) {
2552                 result.AppendErrorWithFormat(
2553                     "Unable to create the executable or symbol file with "
2554                     "UUID %s with path %s and symbol file %s",
2555                     strm.GetData(), module_spec.GetFileSpec().GetPath().c_str(),
2556                     module_spec.GetSymbolFileSpec().GetPath().c_str());
2557               } else {
2558                 result.AppendErrorWithFormat(
2559                     "Unable to create the executable or symbol file with "
2560                     "UUID %s with path %s",
2561                     strm.GetData(),
2562                     module_spec.GetFileSpec().GetPath().c_str());
2563               }
2564             } else {
2565               result.AppendErrorWithFormat("Unable to create the executable "
2566                                            "or symbol file with UUID %s",
2567                                            strm.GetData());
2568             }
2569             result.SetStatus(eReturnStatusFailed);
2570             return false;
2571           }
2572         } else {
2573           StreamString strm;
2574           module_spec.GetUUID().Dump(&strm);
2575           result.AppendErrorWithFormat(
2576               "Unable to locate the executable or symbol file with UUID %s",
2577               strm.GetData());
2578           result.SetStatus(eReturnStatusFailed);
2579           return false;
2580         }
2581       } else {
2582         result.AppendError(
2583             "one or more executable image paths must be specified");
2584         result.SetStatus(eReturnStatusFailed);
2585         return false;
2586       }
2587     } else {
2588       for (auto &entry : args.entries()) {
2589         if (entry.ref().empty())
2590           continue;
2591 
2592         FileSpec file_spec(entry.ref());
2593         if (FileSystem::Instance().Exists(file_spec)) {
2594           ModuleSpec module_spec(file_spec);
2595           if (m_uuid_option_group.GetOptionValue().OptionWasSet())
2596             module_spec.GetUUID() =
2597                 m_uuid_option_group.GetOptionValue().GetCurrentValue();
2598           if (m_symbol_file.GetOptionValue().OptionWasSet())
2599             module_spec.GetSymbolFileSpec() =
2600                 m_symbol_file.GetOptionValue().GetCurrentValue();
2601           if (!module_spec.GetArchitecture().IsValid())
2602             module_spec.GetArchitecture() = target->GetArchitecture();
2603           Status error;
2604           ModuleSP module_sp(target->GetOrCreateModule(
2605               module_spec, true /* notify */, &error));
2606           if (!module_sp) {
2607             const char *error_cstr = error.AsCString();
2608             if (error_cstr)
2609               result.AppendError(error_cstr);
2610             else
2611               result.AppendErrorWithFormat("unsupported module: %s",
2612                                            entry.c_str());
2613             result.SetStatus(eReturnStatusFailed);
2614             return false;
2615           } else {
2616             flush = true;
2617           }
2618           result.SetStatus(eReturnStatusSuccessFinishResult);
2619         } else {
2620           std::string resolved_path = file_spec.GetPath();
2621           result.SetStatus(eReturnStatusFailed);
2622           if (resolved_path != entry.ref()) {
2623             result.AppendErrorWithFormat(
2624                 "invalid module path '%s' with resolved path '%s'\n",
2625                 entry.ref().str().c_str(), resolved_path.c_str());
2626             break;
2627           }
2628           result.AppendErrorWithFormat("invalid module path '%s'\n",
2629                                        entry.c_str());
2630           break;
2631         }
2632       }
2633     }
2634 
2635     if (flush) {
2636       ProcessSP process = target->GetProcessSP();
2637       if (process)
2638         process->Flush();
2639     }
2640 
2641     return result.Succeeded();
2642   }
2643 };
2644 
2645 class CommandObjectTargetModulesLoad
2646     : public CommandObjectTargetModulesModuleAutoComplete {
2647 public:
2648   CommandObjectTargetModulesLoad(CommandInterpreter &interpreter)
2649       : CommandObjectTargetModulesModuleAutoComplete(
2650             interpreter, "target modules load",
2651             "Set the load addresses for one or more sections in a target "
2652             "module.",
2653             "target modules load [--file <module> --uuid <uuid>] <sect-name> "
2654             "<address> [<sect-name> <address> ....]",
2655             eCommandRequiresTarget),
2656         m_option_group(),
2657         m_file_option(LLDB_OPT_SET_1, false, "file", 'f', 0, eArgTypeName,
2658                       "Fullpath or basename for module to load.", ""),
2659         m_load_option(LLDB_OPT_SET_1, false, "load", 'l',
2660                       "Write file contents to the memory.", false, true),
2661         m_pc_option(LLDB_OPT_SET_1, false, "set-pc-to-entry", 'p',
2662                     "Set PC to the entry point."
2663                     " Only applicable with '--load' option.",
2664                     false, true),
2665         m_slide_option(LLDB_OPT_SET_1, false, "slide", 's', 0, eArgTypeOffset,
2666                        "Set the load address for all sections to be the "
2667                        "virtual address in the file plus the offset.",
2668                        0) {
2669     m_option_group.Append(&m_uuid_option_group, LLDB_OPT_SET_ALL,
2670                           LLDB_OPT_SET_1);
2671     m_option_group.Append(&m_file_option, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
2672     m_option_group.Append(&m_load_option, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
2673     m_option_group.Append(&m_pc_option, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
2674     m_option_group.Append(&m_slide_option, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
2675     m_option_group.Finalize();
2676   }
2677 
2678   ~CommandObjectTargetModulesLoad() override = default;
2679 
2680   Options *GetOptions() override { return &m_option_group; }
2681 
2682 protected:
2683   bool DoExecute(Args &args, CommandReturnObject &result) override {
2684     Target *target = &GetSelectedTarget();
2685     const bool load = m_load_option.GetOptionValue().GetCurrentValue();
2686     const bool set_pc = m_pc_option.GetOptionValue().GetCurrentValue();
2687 
2688     const size_t argc = args.GetArgumentCount();
2689     ModuleSpec module_spec;
2690     bool search_using_module_spec = false;
2691 
2692     // Allow "load" option to work without --file or --uuid option.
2693     if (load) {
2694       if (!m_file_option.GetOptionValue().OptionWasSet() &&
2695           !m_uuid_option_group.GetOptionValue().OptionWasSet()) {
2696         ModuleList &module_list = target->GetImages();
2697         if (module_list.GetSize() == 1) {
2698           search_using_module_spec = true;
2699           module_spec.GetFileSpec() =
2700               module_list.GetModuleAtIndex(0)->GetFileSpec();
2701         }
2702       }
2703     }
2704 
2705     if (m_file_option.GetOptionValue().OptionWasSet()) {
2706       search_using_module_spec = true;
2707       const char *arg_cstr = m_file_option.GetOptionValue().GetCurrentValue();
2708       const bool use_global_module_list = true;
2709       ModuleList module_list;
2710       const size_t num_matches = FindModulesByName(
2711           target, arg_cstr, module_list, use_global_module_list);
2712       if (num_matches == 1) {
2713         module_spec.GetFileSpec() =
2714             module_list.GetModuleAtIndex(0)->GetFileSpec();
2715       } else if (num_matches > 1) {
2716         search_using_module_spec = false;
2717         result.AppendErrorWithFormat(
2718             "more than 1 module matched by name '%s'\n", arg_cstr);
2719         result.SetStatus(eReturnStatusFailed);
2720       } else {
2721         search_using_module_spec = false;
2722         result.AppendErrorWithFormat("no object file for module '%s'\n",
2723                                      arg_cstr);
2724         result.SetStatus(eReturnStatusFailed);
2725       }
2726     }
2727 
2728     if (m_uuid_option_group.GetOptionValue().OptionWasSet()) {
2729       search_using_module_spec = true;
2730       module_spec.GetUUID() =
2731           m_uuid_option_group.GetOptionValue().GetCurrentValue();
2732     }
2733 
2734     if (search_using_module_spec) {
2735       ModuleList matching_modules;
2736       target->GetImages().FindModules(module_spec, matching_modules);
2737       const size_t num_matches = matching_modules.GetSize();
2738 
2739       char path[PATH_MAX];
2740       if (num_matches == 1) {
2741         Module *module = matching_modules.GetModulePointerAtIndex(0);
2742         if (module) {
2743           ObjectFile *objfile = module->GetObjectFile();
2744           if (objfile) {
2745             SectionList *section_list = module->GetSectionList();
2746             if (section_list) {
2747               bool changed = false;
2748               if (argc == 0) {
2749                 if (m_slide_option.GetOptionValue().OptionWasSet()) {
2750                   const addr_t slide =
2751                       m_slide_option.GetOptionValue().GetCurrentValue();
2752                   const bool slide_is_offset = true;
2753                   module->SetLoadAddress(*target, slide, slide_is_offset,
2754                                          changed);
2755                 } else {
2756                   result.AppendError("one or more section name + load "
2757                                      "address pair must be specified");
2758                   result.SetStatus(eReturnStatusFailed);
2759                   return false;
2760                 }
2761               } else {
2762                 if (m_slide_option.GetOptionValue().OptionWasSet()) {
2763                   result.AppendError("The \"--slide <offset>\" option can't "
2764                                      "be used in conjunction with setting "
2765                                      "section load addresses.\n");
2766                   result.SetStatus(eReturnStatusFailed);
2767                   return false;
2768                 }
2769 
2770                 for (size_t i = 0; i < argc; i += 2) {
2771                   const char *sect_name = args.GetArgumentAtIndex(i);
2772                   const char *load_addr_cstr = args.GetArgumentAtIndex(i + 1);
2773                   if (sect_name && load_addr_cstr) {
2774                     ConstString const_sect_name(sect_name);
2775                     bool success = false;
2776                     addr_t load_addr = StringConvert::ToUInt64(
2777                         load_addr_cstr, LLDB_INVALID_ADDRESS, 0, &success);
2778                     if (success) {
2779                       SectionSP section_sp(
2780                           section_list->FindSectionByName(const_sect_name));
2781                       if (section_sp) {
2782                         if (section_sp->IsThreadSpecific()) {
2783                           result.AppendErrorWithFormat(
2784                               "thread specific sections are not yet "
2785                               "supported (section '%s')\n",
2786                               sect_name);
2787                           result.SetStatus(eReturnStatusFailed);
2788                           break;
2789                         } else {
2790                           if (target->GetSectionLoadList()
2791                                   .SetSectionLoadAddress(section_sp, load_addr))
2792                             changed = true;
2793                           result.AppendMessageWithFormat(
2794                               "section '%s' loaded at 0x%" PRIx64 "\n",
2795                               sect_name, load_addr);
2796                         }
2797                       } else {
2798                         result.AppendErrorWithFormat("no section found that "
2799                                                      "matches the section "
2800                                                      "name '%s'\n",
2801                                                      sect_name);
2802                         result.SetStatus(eReturnStatusFailed);
2803                         break;
2804                       }
2805                     } else {
2806                       result.AppendErrorWithFormat(
2807                           "invalid load address string '%s'\n", load_addr_cstr);
2808                       result.SetStatus(eReturnStatusFailed);
2809                       break;
2810                     }
2811                   } else {
2812                     if (sect_name)
2813                       result.AppendError("section names must be followed by "
2814                                          "a load address.\n");
2815                     else
2816                       result.AppendError("one or more section name + load "
2817                                          "address pair must be specified.\n");
2818                     result.SetStatus(eReturnStatusFailed);
2819                     break;
2820                   }
2821                 }
2822               }
2823 
2824               if (changed) {
2825                 target->ModulesDidLoad(matching_modules);
2826                 Process *process = m_exe_ctx.GetProcessPtr();
2827                 if (process)
2828                   process->Flush();
2829               }
2830               if (load) {
2831                 ProcessSP process = target->CalculateProcess();
2832                 Address file_entry = objfile->GetEntryPointAddress();
2833                 if (!process) {
2834                   result.AppendError("No process");
2835                   return false;
2836                 }
2837                 if (set_pc && !file_entry.IsValid()) {
2838                   result.AppendError("No entry address in object file");
2839                   return false;
2840                 }
2841                 std::vector<ObjectFile::LoadableData> loadables(
2842                     objfile->GetLoadableData(*target));
2843                 if (loadables.size() == 0) {
2844                   result.AppendError("No loadable sections");
2845                   return false;
2846                 }
2847                 Status error = process->WriteObjectFile(std::move(loadables));
2848                 if (error.Fail()) {
2849                   result.AppendError(error.AsCString());
2850                   return false;
2851                 }
2852                 if (set_pc) {
2853                   ThreadList &thread_list = process->GetThreadList();
2854                   RegisterContextSP reg_context(
2855                       thread_list.GetSelectedThread()->GetRegisterContext());
2856                   addr_t file_entry_addr = file_entry.GetLoadAddress(target);
2857                   if (!reg_context->SetPC(file_entry_addr)) {
2858                     result.AppendErrorWithFormat("failed to set PC value to "
2859                                                  "0x%" PRIx64 "\n",
2860                                                  file_entry_addr);
2861                     result.SetStatus(eReturnStatusFailed);
2862                   }
2863                 }
2864               }
2865             } else {
2866               module->GetFileSpec().GetPath(path, sizeof(path));
2867               result.AppendErrorWithFormat("no sections in object file '%s'\n",
2868                                            path);
2869               result.SetStatus(eReturnStatusFailed);
2870             }
2871           } else {
2872             module->GetFileSpec().GetPath(path, sizeof(path));
2873             result.AppendErrorWithFormat("no object file for module '%s'\n",
2874                                          path);
2875             result.SetStatus(eReturnStatusFailed);
2876           }
2877         } else {
2878           FileSpec *module_spec_file = module_spec.GetFileSpecPtr();
2879           if (module_spec_file) {
2880             module_spec_file->GetPath(path, sizeof(path));
2881             result.AppendErrorWithFormat("invalid module '%s'.\n", path);
2882           } else
2883             result.AppendError("no module spec");
2884           result.SetStatus(eReturnStatusFailed);
2885         }
2886       } else {
2887         std::string uuid_str;
2888 
2889         if (module_spec.GetFileSpec())
2890           module_spec.GetFileSpec().GetPath(path, sizeof(path));
2891         else
2892           path[0] = '\0';
2893 
2894         if (module_spec.GetUUIDPtr())
2895           uuid_str = module_spec.GetUUID().GetAsString();
2896         if (num_matches > 1) {
2897           result.AppendErrorWithFormat(
2898               "multiple modules match%s%s%s%s:\n", path[0] ? " file=" : "",
2899               path, !uuid_str.empty() ? " uuid=" : "", uuid_str.c_str());
2900           for (size_t i = 0; i < num_matches; ++i) {
2901             if (matching_modules.GetModulePointerAtIndex(i)
2902                     ->GetFileSpec()
2903                     .GetPath(path, sizeof(path)))
2904               result.AppendMessageWithFormat("%s\n", path);
2905           }
2906         } else {
2907           result.AppendErrorWithFormat(
2908               "no modules were found  that match%s%s%s%s.\n",
2909               path[0] ? " file=" : "", path, !uuid_str.empty() ? " uuid=" : "",
2910               uuid_str.c_str());
2911         }
2912         result.SetStatus(eReturnStatusFailed);
2913       }
2914     } else {
2915       result.AppendError("either the \"--file <module>\" or the \"--uuid "
2916                          "<uuid>\" option must be specified.\n");
2917       result.SetStatus(eReturnStatusFailed);
2918       return false;
2919     }
2920     return result.Succeeded();
2921   }
2922 
2923   OptionGroupOptions m_option_group;
2924   OptionGroupUUID m_uuid_option_group;
2925   OptionGroupString m_file_option;
2926   OptionGroupBoolean m_load_option;
2927   OptionGroupBoolean m_pc_option;
2928   OptionGroupUInt64 m_slide_option;
2929 };
2930 
2931 // List images with associated information
2932 #define LLDB_OPTIONS_target_modules_list
2933 #include "CommandOptions.inc"
2934 
2935 class CommandObjectTargetModulesList : public CommandObjectParsed {
2936 public:
2937   class CommandOptions : public Options {
2938   public:
2939     CommandOptions()
2940         : Options(), m_format_array(), m_use_global_module_list(false),
2941           m_module_addr(LLDB_INVALID_ADDRESS) {}
2942 
2943     ~CommandOptions() override = default;
2944 
2945     Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
2946                           ExecutionContext *execution_context) override {
2947       Status error;
2948 
2949       const int short_option = m_getopt_table[option_idx].val;
2950       if (short_option == 'g') {
2951         m_use_global_module_list = true;
2952       } else if (short_option == 'a') {
2953         m_module_addr = OptionArgParser::ToAddress(
2954             execution_context, option_arg, LLDB_INVALID_ADDRESS, &error);
2955       } else {
2956         unsigned long width = 0;
2957         option_arg.getAsInteger(0, width);
2958         m_format_array.push_back(std::make_pair(short_option, width));
2959       }
2960       return error;
2961     }
2962 
2963     void OptionParsingStarting(ExecutionContext *execution_context) override {
2964       m_format_array.clear();
2965       m_use_global_module_list = false;
2966       m_module_addr = LLDB_INVALID_ADDRESS;
2967     }
2968 
2969     llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
2970       return llvm::makeArrayRef(g_target_modules_list_options);
2971     }
2972 
2973     // Instance variables to hold the values for command options.
2974     typedef std::vector<std::pair<char, uint32_t>> FormatWidthCollection;
2975     FormatWidthCollection m_format_array;
2976     bool m_use_global_module_list;
2977     lldb::addr_t m_module_addr;
2978   };
2979 
2980   CommandObjectTargetModulesList(CommandInterpreter &interpreter)
2981       : CommandObjectParsed(
2982             interpreter, "target modules list",
2983             "List current executable and dependent shared library images.",
2984             "target modules list [<cmd-options>]"),
2985         m_options() {}
2986 
2987   ~CommandObjectTargetModulesList() override = default;
2988 
2989   Options *GetOptions() override { return &m_options; }
2990 
2991 protected:
2992   bool DoExecute(Args &command, CommandReturnObject &result) override {
2993     Target *target = GetDebugger().GetSelectedTarget().get();
2994     const bool use_global_module_list = m_options.m_use_global_module_list;
2995     // Define a local module list here to ensure it lives longer than any
2996     // "locker" object which might lock its contents below (through the
2997     // "module_list_ptr" variable).
2998     ModuleList module_list;
2999     if (target == nullptr && !use_global_module_list) {
3000       result.AppendError("invalid target, create a debug target using the "
3001                          "'target create' command");
3002       result.SetStatus(eReturnStatusFailed);
3003       return false;
3004     } else {
3005       if (target) {
3006         uint32_t addr_byte_size =
3007             target->GetArchitecture().GetAddressByteSize();
3008         result.GetOutputStream().SetAddressByteSize(addr_byte_size);
3009         result.GetErrorStream().SetAddressByteSize(addr_byte_size);
3010       }
3011       // Dump all sections for all modules images
3012       Stream &strm = result.GetOutputStream();
3013 
3014       if (m_options.m_module_addr != LLDB_INVALID_ADDRESS) {
3015         if (target) {
3016           Address module_address;
3017           if (module_address.SetLoadAddress(m_options.m_module_addr, target)) {
3018             ModuleSP module_sp(module_address.GetModule());
3019             if (module_sp) {
3020               PrintModule(target, module_sp.get(), 0, strm);
3021               result.SetStatus(eReturnStatusSuccessFinishResult);
3022             } else {
3023               result.AppendErrorWithFormat(
3024                   "Couldn't find module matching address: 0x%" PRIx64 ".",
3025                   m_options.m_module_addr);
3026               result.SetStatus(eReturnStatusFailed);
3027             }
3028           } else {
3029             result.AppendErrorWithFormat(
3030                 "Couldn't find module containing address: 0x%" PRIx64 ".",
3031                 m_options.m_module_addr);
3032             result.SetStatus(eReturnStatusFailed);
3033           }
3034         } else {
3035           result.AppendError(
3036               "Can only look up modules by address with a valid target.");
3037           result.SetStatus(eReturnStatusFailed);
3038         }
3039         return result.Succeeded();
3040       }
3041 
3042       size_t num_modules = 0;
3043 
3044       // This locker will be locked on the mutex in module_list_ptr if it is
3045       // non-nullptr. Otherwise it will lock the
3046       // AllocationModuleCollectionMutex when accessing the global module list
3047       // directly.
3048       std::unique_lock<std::recursive_mutex> guard(
3049           Module::GetAllocationModuleCollectionMutex(), std::defer_lock);
3050 
3051       const ModuleList *module_list_ptr = nullptr;
3052       const size_t argc = command.GetArgumentCount();
3053       if (argc == 0) {
3054         if (use_global_module_list) {
3055           guard.lock();
3056           num_modules = Module::GetNumberAllocatedModules();
3057         } else {
3058           module_list_ptr = &target->GetImages();
3059         }
3060       } else {
3061         // TODO: Convert to entry based iteration.  Requires converting
3062         // FindModulesByName.
3063         for (size_t i = 0; i < argc; ++i) {
3064           // Dump specified images (by basename or fullpath)
3065           const char *arg_cstr = command.GetArgumentAtIndex(i);
3066           const size_t num_matches = FindModulesByName(
3067               target, arg_cstr, module_list, use_global_module_list);
3068           if (num_matches == 0) {
3069             if (argc == 1) {
3070               result.AppendErrorWithFormat("no modules found that match '%s'",
3071                                            arg_cstr);
3072               result.SetStatus(eReturnStatusFailed);
3073               return false;
3074             }
3075           }
3076         }
3077 
3078         module_list_ptr = &module_list;
3079       }
3080 
3081       std::unique_lock<std::recursive_mutex> lock;
3082       if (module_list_ptr != nullptr) {
3083         lock =
3084             std::unique_lock<std::recursive_mutex>(module_list_ptr->GetMutex());
3085 
3086         num_modules = module_list_ptr->GetSize();
3087       }
3088 
3089       if (num_modules > 0) {
3090         for (uint32_t image_idx = 0; image_idx < num_modules; ++image_idx) {
3091           ModuleSP module_sp;
3092           Module *module;
3093           if (module_list_ptr) {
3094             module_sp = module_list_ptr->GetModuleAtIndexUnlocked(image_idx);
3095             module = module_sp.get();
3096           } else {
3097             module = Module::GetAllocatedModuleAtIndex(image_idx);
3098             module_sp = module->shared_from_this();
3099           }
3100 
3101           const size_t indent = strm.Printf("[%3u] ", image_idx);
3102           PrintModule(target, module, indent, strm);
3103         }
3104         result.SetStatus(eReturnStatusSuccessFinishResult);
3105       } else {
3106         if (argc) {
3107           if (use_global_module_list)
3108             result.AppendError(
3109                 "the global module list has no matching modules");
3110           else
3111             result.AppendError("the target has no matching modules");
3112         } else {
3113           if (use_global_module_list)
3114             result.AppendError("the global module list is empty");
3115           else
3116             result.AppendError(
3117                 "the target has no associated executable images");
3118         }
3119         result.SetStatus(eReturnStatusFailed);
3120         return false;
3121       }
3122     }
3123     return result.Succeeded();
3124   }
3125 
3126   void PrintModule(Target *target, Module *module, int indent, Stream &strm) {
3127     if (module == nullptr) {
3128       strm.PutCString("Null module");
3129       return;
3130     }
3131 
3132     bool dump_object_name = false;
3133     if (m_options.m_format_array.empty()) {
3134       m_options.m_format_array.push_back(std::make_pair('u', 0));
3135       m_options.m_format_array.push_back(std::make_pair('h', 0));
3136       m_options.m_format_array.push_back(std::make_pair('f', 0));
3137       m_options.m_format_array.push_back(std::make_pair('S', 0));
3138     }
3139     const size_t num_entries = m_options.m_format_array.size();
3140     bool print_space = false;
3141     for (size_t i = 0; i < num_entries; ++i) {
3142       if (print_space)
3143         strm.PutChar(' ');
3144       print_space = true;
3145       const char format_char = m_options.m_format_array[i].first;
3146       uint32_t width = m_options.m_format_array[i].second;
3147       switch (format_char) {
3148       case 'A':
3149         DumpModuleArchitecture(strm, module, false, width);
3150         break;
3151 
3152       case 't':
3153         DumpModuleArchitecture(strm, module, true, width);
3154         break;
3155 
3156       case 'f':
3157         DumpFullpath(strm, &module->GetFileSpec(), width);
3158         dump_object_name = true;
3159         break;
3160 
3161       case 'd':
3162         DumpDirectory(strm, &module->GetFileSpec(), width);
3163         break;
3164 
3165       case 'b':
3166         DumpBasename(strm, &module->GetFileSpec(), width);
3167         dump_object_name = true;
3168         break;
3169 
3170       case 'h':
3171       case 'o':
3172         // Image header address
3173         {
3174           uint32_t addr_nibble_width =
3175               target ? (target->GetArchitecture().GetAddressByteSize() * 2)
3176                      : 16;
3177 
3178           ObjectFile *objfile = module->GetObjectFile();
3179           if (objfile) {
3180             Address base_addr(objfile->GetBaseAddress());
3181             if (base_addr.IsValid()) {
3182               if (target && !target->GetSectionLoadList().IsEmpty()) {
3183                 lldb::addr_t load_addr = base_addr.GetLoadAddress(target);
3184                 if (load_addr == LLDB_INVALID_ADDRESS) {
3185                   base_addr.Dump(&strm, target,
3186                                  Address::DumpStyleModuleWithFileAddress,
3187                                  Address::DumpStyleFileAddress);
3188                 } else {
3189                   if (format_char == 'o') {
3190                     // Show the offset of slide for the image
3191                     strm.Printf("0x%*.*" PRIx64, addr_nibble_width,
3192                                 addr_nibble_width,
3193                                 load_addr - base_addr.GetFileAddress());
3194                   } else {
3195                     // Show the load address of the image
3196                     strm.Printf("0x%*.*" PRIx64, addr_nibble_width,
3197                                 addr_nibble_width, load_addr);
3198                   }
3199                 }
3200                 break;
3201               }
3202               // The address was valid, but the image isn't loaded, output the
3203               // address in an appropriate format
3204               base_addr.Dump(&strm, target, Address::DumpStyleFileAddress);
3205               break;
3206             }
3207           }
3208           strm.Printf("%*s", addr_nibble_width + 2, "");
3209         }
3210         break;
3211 
3212       case 'r': {
3213         size_t ref_count = 0;
3214         ModuleSP module_sp(module->shared_from_this());
3215         if (module_sp) {
3216           // Take one away to make sure we don't count our local "module_sp"
3217           ref_count = module_sp.use_count() - 1;
3218         }
3219         if (width)
3220           strm.Printf("{%*" PRIu64 "}", width, (uint64_t)ref_count);
3221         else
3222           strm.Printf("{%" PRIu64 "}", (uint64_t)ref_count);
3223       } break;
3224 
3225       case 's':
3226       case 'S': {
3227         if (const SymbolFile *symbol_file = module->GetSymbolFile()) {
3228           const FileSpec symfile_spec =
3229               symbol_file->GetObjectFile()->GetFileSpec();
3230           if (format_char == 'S') {
3231             // Dump symbol file only if different from module file
3232             if (!symfile_spec || symfile_spec == module->GetFileSpec()) {
3233               print_space = false;
3234               break;
3235             }
3236             // Add a newline and indent past the index
3237             strm.Printf("\n%*s", indent, "");
3238           }
3239           DumpFullpath(strm, &symfile_spec, width);
3240           dump_object_name = true;
3241           break;
3242         }
3243         strm.Printf("%.*s", width, "<NONE>");
3244       } break;
3245 
3246       case 'm':
3247         strm.Format("{0:%c}", llvm::fmt_align(module->GetModificationTime(),
3248                                               llvm::AlignStyle::Left, width));
3249         break;
3250 
3251       case 'p':
3252         strm.Printf("%p", static_cast<void *>(module));
3253         break;
3254 
3255       case 'u':
3256         DumpModuleUUID(strm, module);
3257         break;
3258 
3259       default:
3260         break;
3261       }
3262     }
3263     if (dump_object_name) {
3264       const char *object_name = module->GetObjectName().GetCString();
3265       if (object_name)
3266         strm.Printf("(%s)", object_name);
3267     }
3268     strm.EOL();
3269   }
3270 
3271   CommandOptions m_options;
3272 };
3273 
3274 #pragma mark CommandObjectTargetModulesShowUnwind
3275 
3276 // Lookup unwind information in images
3277 #define LLDB_OPTIONS_target_modules_show_unwind
3278 #include "CommandOptions.inc"
3279 
3280 class CommandObjectTargetModulesShowUnwind : public CommandObjectParsed {
3281 public:
3282   enum {
3283     eLookupTypeInvalid = -1,
3284     eLookupTypeAddress = 0,
3285     eLookupTypeSymbol,
3286     eLookupTypeFunction,
3287     eLookupTypeFunctionOrSymbol,
3288     kNumLookupTypes
3289   };
3290 
3291   class CommandOptions : public Options {
3292   public:
3293     CommandOptions()
3294         : Options(), m_type(eLookupTypeInvalid), m_str(),
3295           m_addr(LLDB_INVALID_ADDRESS) {}
3296 
3297     ~CommandOptions() override = default;
3298 
3299     Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
3300                           ExecutionContext *execution_context) override {
3301       Status error;
3302 
3303       const int short_option = m_getopt_table[option_idx].val;
3304 
3305       switch (short_option) {
3306       case 'a': {
3307         m_str = std::string(option_arg);
3308         m_type = eLookupTypeAddress;
3309         m_addr = OptionArgParser::ToAddress(execution_context, option_arg,
3310                                             LLDB_INVALID_ADDRESS, &error);
3311         if (m_addr == LLDB_INVALID_ADDRESS)
3312           error.SetErrorStringWithFormat("invalid address string '%s'",
3313                                          option_arg.str().c_str());
3314         break;
3315       }
3316 
3317       case 'n':
3318         m_str = std::string(option_arg);
3319         m_type = eLookupTypeFunctionOrSymbol;
3320         break;
3321 
3322       default:
3323         llvm_unreachable("Unimplemented option");
3324       }
3325 
3326       return error;
3327     }
3328 
3329     void OptionParsingStarting(ExecutionContext *execution_context) override {
3330       m_type = eLookupTypeInvalid;
3331       m_str.clear();
3332       m_addr = LLDB_INVALID_ADDRESS;
3333     }
3334 
3335     llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
3336       return llvm::makeArrayRef(g_target_modules_show_unwind_options);
3337     }
3338 
3339     // Instance variables to hold the values for command options.
3340 
3341     int m_type;        // Should be a eLookupTypeXXX enum after parsing options
3342     std::string m_str; // Holds name lookup
3343     lldb::addr_t m_addr; // Holds the address to lookup
3344   };
3345 
3346   CommandObjectTargetModulesShowUnwind(CommandInterpreter &interpreter)
3347       : CommandObjectParsed(
3348             interpreter, "target modules show-unwind",
3349             "Show synthesized unwind instructions for a function.", nullptr,
3350             eCommandRequiresTarget | eCommandRequiresProcess |
3351                 eCommandProcessMustBeLaunched | eCommandProcessMustBePaused),
3352         m_options() {}
3353 
3354   ~CommandObjectTargetModulesShowUnwind() override = default;
3355 
3356   Options *GetOptions() override { return &m_options; }
3357 
3358 protected:
3359   bool DoExecute(Args &command, CommandReturnObject &result) override {
3360     Target *target = m_exe_ctx.GetTargetPtr();
3361     Process *process = m_exe_ctx.GetProcessPtr();
3362     ABI *abi = nullptr;
3363     if (process)
3364       abi = process->GetABI().get();
3365 
3366     if (process == nullptr) {
3367       result.AppendError(
3368           "You must have a process running to use this command.");
3369       result.SetStatus(eReturnStatusFailed);
3370       return false;
3371     }
3372 
3373     ThreadList threads(process->GetThreadList());
3374     if (threads.GetSize() == 0) {
3375       result.AppendError("The process must be paused to use this command.");
3376       result.SetStatus(eReturnStatusFailed);
3377       return false;
3378     }
3379 
3380     ThreadSP thread(threads.GetThreadAtIndex(0));
3381     if (!thread) {
3382       result.AppendError("The process must be paused to use this command.");
3383       result.SetStatus(eReturnStatusFailed);
3384       return false;
3385     }
3386 
3387     SymbolContextList sc_list;
3388 
3389     if (m_options.m_type == eLookupTypeFunctionOrSymbol) {
3390       ConstString function_name(m_options.m_str.c_str());
3391       target->GetImages().FindFunctions(function_name, eFunctionNameTypeAuto,
3392                                         true, false, sc_list);
3393     } else if (m_options.m_type == eLookupTypeAddress && target) {
3394       Address addr;
3395       if (target->GetSectionLoadList().ResolveLoadAddress(m_options.m_addr,
3396                                                           addr)) {
3397         SymbolContext sc;
3398         ModuleSP module_sp(addr.GetModule());
3399         module_sp->ResolveSymbolContextForAddress(addr,
3400                                                   eSymbolContextEverything, sc);
3401         if (sc.function || sc.symbol) {
3402           sc_list.Append(sc);
3403         }
3404       }
3405     } else {
3406       result.AppendError(
3407           "address-expression or function name option must be specified.");
3408       result.SetStatus(eReturnStatusFailed);
3409       return false;
3410     }
3411 
3412     size_t num_matches = sc_list.GetSize();
3413     if (num_matches == 0) {
3414       result.AppendErrorWithFormat("no unwind data found that matches '%s'.",
3415                                    m_options.m_str.c_str());
3416       result.SetStatus(eReturnStatusFailed);
3417       return false;
3418     }
3419 
3420     for (uint32_t idx = 0; idx < num_matches; idx++) {
3421       SymbolContext sc;
3422       sc_list.GetContextAtIndex(idx, sc);
3423       if (sc.symbol == nullptr && sc.function == nullptr)
3424         continue;
3425       if (!sc.module_sp || sc.module_sp->GetObjectFile() == nullptr)
3426         continue;
3427       AddressRange range;
3428       if (!sc.GetAddressRange(eSymbolContextFunction | eSymbolContextSymbol, 0,
3429                               false, range))
3430         continue;
3431       if (!range.GetBaseAddress().IsValid())
3432         continue;
3433       ConstString funcname(sc.GetFunctionName());
3434       if (funcname.IsEmpty())
3435         continue;
3436       addr_t start_addr = range.GetBaseAddress().GetLoadAddress(target);
3437       if (abi)
3438         start_addr = abi->FixCodeAddress(start_addr);
3439 
3440       FuncUnwindersSP func_unwinders_sp(
3441           sc.module_sp->GetUnwindTable()
3442               .GetUncachedFuncUnwindersContainingAddress(start_addr, sc));
3443       if (!func_unwinders_sp)
3444         continue;
3445 
3446       result.GetOutputStream().Printf(
3447           "UNWIND PLANS for %s`%s (start addr 0x%" PRIx64 ")\n\n",
3448           sc.module_sp->GetPlatformFileSpec().GetFilename().AsCString(),
3449           funcname.AsCString(), start_addr);
3450 
3451       UnwindPlanSP non_callsite_unwind_plan =
3452           func_unwinders_sp->GetUnwindPlanAtNonCallSite(*target, *thread);
3453       if (non_callsite_unwind_plan) {
3454         result.GetOutputStream().Printf(
3455             "Asynchronous (not restricted to call-sites) UnwindPlan is '%s'\n",
3456             non_callsite_unwind_plan->GetSourceName().AsCString());
3457       }
3458       UnwindPlanSP callsite_unwind_plan =
3459           func_unwinders_sp->GetUnwindPlanAtCallSite(*target, *thread);
3460       if (callsite_unwind_plan) {
3461         result.GetOutputStream().Printf(
3462             "Synchronous (restricted to call-sites) UnwindPlan is '%s'\n",
3463             callsite_unwind_plan->GetSourceName().AsCString());
3464       }
3465       UnwindPlanSP fast_unwind_plan =
3466           func_unwinders_sp->GetUnwindPlanFastUnwind(*target, *thread);
3467       if (fast_unwind_plan) {
3468         result.GetOutputStream().Printf(
3469             "Fast UnwindPlan is '%s'\n",
3470             fast_unwind_plan->GetSourceName().AsCString());
3471       }
3472 
3473       result.GetOutputStream().Printf("\n");
3474 
3475       UnwindPlanSP assembly_sp =
3476           func_unwinders_sp->GetAssemblyUnwindPlan(*target, *thread);
3477       if (assembly_sp) {
3478         result.GetOutputStream().Printf(
3479             "Assembly language inspection UnwindPlan:\n");
3480         assembly_sp->Dump(result.GetOutputStream(), thread.get(),
3481                           LLDB_INVALID_ADDRESS);
3482         result.GetOutputStream().Printf("\n");
3483       }
3484 
3485       UnwindPlanSP of_unwind_sp =
3486           func_unwinders_sp->GetObjectFileUnwindPlan(*target);
3487       if (of_unwind_sp) {
3488         result.GetOutputStream().Printf("object file UnwindPlan:\n");
3489         of_unwind_sp->Dump(result.GetOutputStream(), thread.get(),
3490                            LLDB_INVALID_ADDRESS);
3491         result.GetOutputStream().Printf("\n");
3492       }
3493 
3494       UnwindPlanSP of_unwind_augmented_sp =
3495           func_unwinders_sp->GetObjectFileAugmentedUnwindPlan(*target, *thread);
3496       if (of_unwind_augmented_sp) {
3497         result.GetOutputStream().Printf("object file augmented UnwindPlan:\n");
3498         of_unwind_augmented_sp->Dump(result.GetOutputStream(), thread.get(),
3499                                      LLDB_INVALID_ADDRESS);
3500         result.GetOutputStream().Printf("\n");
3501       }
3502 
3503       UnwindPlanSP ehframe_sp =
3504           func_unwinders_sp->GetEHFrameUnwindPlan(*target);
3505       if (ehframe_sp) {
3506         result.GetOutputStream().Printf("eh_frame UnwindPlan:\n");
3507         ehframe_sp->Dump(result.GetOutputStream(), thread.get(),
3508                          LLDB_INVALID_ADDRESS);
3509         result.GetOutputStream().Printf("\n");
3510       }
3511 
3512       UnwindPlanSP ehframe_augmented_sp =
3513           func_unwinders_sp->GetEHFrameAugmentedUnwindPlan(*target, *thread);
3514       if (ehframe_augmented_sp) {
3515         result.GetOutputStream().Printf("eh_frame augmented UnwindPlan:\n");
3516         ehframe_augmented_sp->Dump(result.GetOutputStream(), thread.get(),
3517                                    LLDB_INVALID_ADDRESS);
3518         result.GetOutputStream().Printf("\n");
3519       }
3520 
3521       if (UnwindPlanSP plan_sp =
3522               func_unwinders_sp->GetDebugFrameUnwindPlan(*target)) {
3523         result.GetOutputStream().Printf("debug_frame UnwindPlan:\n");
3524         plan_sp->Dump(result.GetOutputStream(), thread.get(),
3525                       LLDB_INVALID_ADDRESS);
3526         result.GetOutputStream().Printf("\n");
3527       }
3528 
3529       if (UnwindPlanSP plan_sp =
3530               func_unwinders_sp->GetDebugFrameAugmentedUnwindPlan(*target,
3531                                                                   *thread)) {
3532         result.GetOutputStream().Printf("debug_frame augmented UnwindPlan:\n");
3533         plan_sp->Dump(result.GetOutputStream(), thread.get(),
3534                       LLDB_INVALID_ADDRESS);
3535         result.GetOutputStream().Printf("\n");
3536       }
3537 
3538       UnwindPlanSP arm_unwind_sp =
3539           func_unwinders_sp->GetArmUnwindUnwindPlan(*target);
3540       if (arm_unwind_sp) {
3541         result.GetOutputStream().Printf("ARM.exidx unwind UnwindPlan:\n");
3542         arm_unwind_sp->Dump(result.GetOutputStream(), thread.get(),
3543                             LLDB_INVALID_ADDRESS);
3544         result.GetOutputStream().Printf("\n");
3545       }
3546 
3547       if (UnwindPlanSP symfile_plan_sp =
3548               func_unwinders_sp->GetSymbolFileUnwindPlan(*thread)) {
3549         result.GetOutputStream().Printf("Symbol file UnwindPlan:\n");
3550         symfile_plan_sp->Dump(result.GetOutputStream(), thread.get(),
3551                               LLDB_INVALID_ADDRESS);
3552         result.GetOutputStream().Printf("\n");
3553       }
3554 
3555       UnwindPlanSP compact_unwind_sp =
3556           func_unwinders_sp->GetCompactUnwindUnwindPlan(*target);
3557       if (compact_unwind_sp) {
3558         result.GetOutputStream().Printf("Compact unwind UnwindPlan:\n");
3559         compact_unwind_sp->Dump(result.GetOutputStream(), thread.get(),
3560                                 LLDB_INVALID_ADDRESS);
3561         result.GetOutputStream().Printf("\n");
3562       }
3563 
3564       if (fast_unwind_plan) {
3565         result.GetOutputStream().Printf("Fast UnwindPlan:\n");
3566         fast_unwind_plan->Dump(result.GetOutputStream(), thread.get(),
3567                                LLDB_INVALID_ADDRESS);
3568         result.GetOutputStream().Printf("\n");
3569       }
3570 
3571       ABISP abi_sp = process->GetABI();
3572       if (abi_sp) {
3573         UnwindPlan arch_default(lldb::eRegisterKindGeneric);
3574         if (abi_sp->CreateDefaultUnwindPlan(arch_default)) {
3575           result.GetOutputStream().Printf("Arch default UnwindPlan:\n");
3576           arch_default.Dump(result.GetOutputStream(), thread.get(),
3577                             LLDB_INVALID_ADDRESS);
3578           result.GetOutputStream().Printf("\n");
3579         }
3580 
3581         UnwindPlan arch_entry(lldb::eRegisterKindGeneric);
3582         if (abi_sp->CreateFunctionEntryUnwindPlan(arch_entry)) {
3583           result.GetOutputStream().Printf(
3584               "Arch default at entry point UnwindPlan:\n");
3585           arch_entry.Dump(result.GetOutputStream(), thread.get(),
3586                           LLDB_INVALID_ADDRESS);
3587           result.GetOutputStream().Printf("\n");
3588         }
3589       }
3590 
3591       result.GetOutputStream().Printf("\n");
3592     }
3593     return result.Succeeded();
3594   }
3595 
3596   CommandOptions m_options;
3597 };
3598 
3599 // Lookup information in images
3600 #define LLDB_OPTIONS_target_modules_lookup
3601 #include "CommandOptions.inc"
3602 
3603 class CommandObjectTargetModulesLookup : public CommandObjectParsed {
3604 public:
3605   enum {
3606     eLookupTypeInvalid = -1,
3607     eLookupTypeAddress = 0,
3608     eLookupTypeSymbol,
3609     eLookupTypeFileLine, // Line is optional
3610     eLookupTypeFunction,
3611     eLookupTypeFunctionOrSymbol,
3612     eLookupTypeType,
3613     kNumLookupTypes
3614   };
3615 
3616   class CommandOptions : public Options {
3617   public:
3618     CommandOptions() : Options() { OptionParsingStarting(nullptr); }
3619 
3620     ~CommandOptions() override = default;
3621 
3622     Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
3623                           ExecutionContext *execution_context) override {
3624       Status error;
3625 
3626       const int short_option = m_getopt_table[option_idx].val;
3627 
3628       switch (short_option) {
3629       case 'a': {
3630         m_type = eLookupTypeAddress;
3631         m_addr = OptionArgParser::ToAddress(execution_context, option_arg,
3632                                             LLDB_INVALID_ADDRESS, &error);
3633       } break;
3634 
3635       case 'o':
3636         if (option_arg.getAsInteger(0, m_offset))
3637           error.SetErrorStringWithFormat("invalid offset string '%s'",
3638                                          option_arg.str().c_str());
3639         break;
3640 
3641       case 's':
3642         m_str = std::string(option_arg);
3643         m_type = eLookupTypeSymbol;
3644         break;
3645 
3646       case 'f':
3647         m_file.SetFile(option_arg, FileSpec::Style::native);
3648         m_type = eLookupTypeFileLine;
3649         break;
3650 
3651       case 'i':
3652         m_include_inlines = false;
3653         break;
3654 
3655       case 'l':
3656         if (option_arg.getAsInteger(0, m_line_number))
3657           error.SetErrorStringWithFormat("invalid line number string '%s'",
3658                                          option_arg.str().c_str());
3659         else if (m_line_number == 0)
3660           error.SetErrorString("zero is an invalid line number");
3661         m_type = eLookupTypeFileLine;
3662         break;
3663 
3664       case 'F':
3665         m_str = std::string(option_arg);
3666         m_type = eLookupTypeFunction;
3667         break;
3668 
3669       case 'n':
3670         m_str = std::string(option_arg);
3671         m_type = eLookupTypeFunctionOrSymbol;
3672         break;
3673 
3674       case 't':
3675         m_str = std::string(option_arg);
3676         m_type = eLookupTypeType;
3677         break;
3678 
3679       case 'v':
3680         m_verbose = true;
3681         break;
3682 
3683       case 'A':
3684         m_print_all = true;
3685         break;
3686 
3687       case 'r':
3688         m_use_regex = true;
3689         break;
3690       default:
3691         llvm_unreachable("Unimplemented option");
3692       }
3693 
3694       return error;
3695     }
3696 
3697     void OptionParsingStarting(ExecutionContext *execution_context) override {
3698       m_type = eLookupTypeInvalid;
3699       m_str.clear();
3700       m_file.Clear();
3701       m_addr = LLDB_INVALID_ADDRESS;
3702       m_offset = 0;
3703       m_line_number = 0;
3704       m_use_regex = false;
3705       m_include_inlines = true;
3706       m_verbose = false;
3707       m_print_all = false;
3708     }
3709 
3710     llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
3711       return llvm::makeArrayRef(g_target_modules_lookup_options);
3712     }
3713 
3714     int m_type;        // Should be a eLookupTypeXXX enum after parsing options
3715     std::string m_str; // Holds name lookup
3716     FileSpec m_file;   // Files for file lookups
3717     lldb::addr_t m_addr; // Holds the address to lookup
3718     lldb::addr_t
3719         m_offset; // Subtract this offset from m_addr before doing lookups.
3720     uint32_t m_line_number; // Line number for file+line lookups
3721     bool m_use_regex;       // Name lookups in m_str are regular expressions.
3722     bool m_include_inlines; // Check for inline entries when looking up by
3723                             // file/line.
3724     bool m_verbose;         // Enable verbose lookup info
3725     bool m_print_all; // Print all matches, even in cases where there's a best
3726                       // match.
3727   };
3728 
3729   CommandObjectTargetModulesLookup(CommandInterpreter &interpreter)
3730       : CommandObjectParsed(interpreter, "target modules lookup",
3731                             "Look up information within executable and "
3732                             "dependent shared library images.",
3733                             nullptr, eCommandRequiresTarget),
3734         m_options() {
3735     CommandArgumentEntry arg;
3736     CommandArgumentData file_arg;
3737 
3738     // Define the first (and only) variant of this arg.
3739     file_arg.arg_type = eArgTypeFilename;
3740     file_arg.arg_repetition = eArgRepeatStar;
3741 
3742     // There is only one variant this argument could be; put it into the
3743     // argument entry.
3744     arg.push_back(file_arg);
3745 
3746     // Push the data for the first argument into the m_arguments vector.
3747     m_arguments.push_back(arg);
3748   }
3749 
3750   ~CommandObjectTargetModulesLookup() override = default;
3751 
3752   Options *GetOptions() override { return &m_options; }
3753 
3754   bool LookupHere(CommandInterpreter &interpreter, CommandReturnObject &result,
3755                   bool &syntax_error) {
3756     switch (m_options.m_type) {
3757     case eLookupTypeAddress:
3758     case eLookupTypeFileLine:
3759     case eLookupTypeFunction:
3760     case eLookupTypeFunctionOrSymbol:
3761     case eLookupTypeSymbol:
3762     default:
3763       return false;
3764     case eLookupTypeType:
3765       break;
3766     }
3767 
3768     StackFrameSP frame = m_exe_ctx.GetFrameSP();
3769 
3770     if (!frame)
3771       return false;
3772 
3773     const SymbolContext &sym_ctx(frame->GetSymbolContext(eSymbolContextModule));
3774 
3775     if (!sym_ctx.module_sp)
3776       return false;
3777 
3778     switch (m_options.m_type) {
3779     default:
3780       return false;
3781     case eLookupTypeType:
3782       if (!m_options.m_str.empty()) {
3783         if (LookupTypeHere(m_interpreter, result.GetOutputStream(),
3784                            *sym_ctx.module_sp, m_options.m_str.c_str(),
3785                            m_options.m_use_regex)) {
3786           result.SetStatus(eReturnStatusSuccessFinishResult);
3787           return true;
3788         }
3789       }
3790       break;
3791     }
3792 
3793     return false;
3794   }
3795 
3796   bool LookupInModule(CommandInterpreter &interpreter, Module *module,
3797                       CommandReturnObject &result, bool &syntax_error) {
3798     switch (m_options.m_type) {
3799     case eLookupTypeAddress:
3800       if (m_options.m_addr != LLDB_INVALID_ADDRESS) {
3801         if (LookupAddressInModule(
3802                 m_interpreter, result.GetOutputStream(), module,
3803                 eSymbolContextEverything |
3804                     (m_options.m_verbose
3805                          ? static_cast<int>(eSymbolContextVariable)
3806                          : 0),
3807                 m_options.m_addr, m_options.m_offset, m_options.m_verbose)) {
3808           result.SetStatus(eReturnStatusSuccessFinishResult);
3809           return true;
3810         }
3811       }
3812       break;
3813 
3814     case eLookupTypeSymbol:
3815       if (!m_options.m_str.empty()) {
3816         if (LookupSymbolInModule(m_interpreter, result.GetOutputStream(),
3817                                  module, m_options.m_str.c_str(),
3818                                  m_options.m_use_regex, m_options.m_verbose)) {
3819           result.SetStatus(eReturnStatusSuccessFinishResult);
3820           return true;
3821         }
3822       }
3823       break;
3824 
3825     case eLookupTypeFileLine:
3826       if (m_options.m_file) {
3827         if (LookupFileAndLineInModule(
3828                 m_interpreter, result.GetOutputStream(), module,
3829                 m_options.m_file, m_options.m_line_number,
3830                 m_options.m_include_inlines, m_options.m_verbose)) {
3831           result.SetStatus(eReturnStatusSuccessFinishResult);
3832           return true;
3833         }
3834       }
3835       break;
3836 
3837     case eLookupTypeFunctionOrSymbol:
3838     case eLookupTypeFunction:
3839       if (!m_options.m_str.empty()) {
3840         if (LookupFunctionInModule(
3841                 m_interpreter, result.GetOutputStream(), module,
3842                 m_options.m_str.c_str(), m_options.m_use_regex,
3843                 m_options.m_include_inlines,
3844                 m_options.m_type ==
3845                     eLookupTypeFunctionOrSymbol, // include symbols
3846                 m_options.m_verbose)) {
3847           result.SetStatus(eReturnStatusSuccessFinishResult);
3848           return true;
3849         }
3850       }
3851       break;
3852 
3853     case eLookupTypeType:
3854       if (!m_options.m_str.empty()) {
3855         if (LookupTypeInModule(m_interpreter, result.GetOutputStream(), module,
3856                                m_options.m_str.c_str(),
3857                                m_options.m_use_regex)) {
3858           result.SetStatus(eReturnStatusSuccessFinishResult);
3859           return true;
3860         }
3861       }
3862       break;
3863 
3864     default:
3865       m_options.GenerateOptionUsage(
3866           result.GetErrorStream(), this,
3867           GetCommandInterpreter().GetDebugger().GetTerminalWidth());
3868       syntax_error = true;
3869       break;
3870     }
3871 
3872     result.SetStatus(eReturnStatusFailed);
3873     return false;
3874   }
3875 
3876 protected:
3877   bool DoExecute(Args &command, CommandReturnObject &result) override {
3878     Target *target = &GetSelectedTarget();
3879     bool syntax_error = false;
3880     uint32_t i;
3881     uint32_t num_successful_lookups = 0;
3882     uint32_t addr_byte_size = target->GetArchitecture().GetAddressByteSize();
3883     result.GetOutputStream().SetAddressByteSize(addr_byte_size);
3884     result.GetErrorStream().SetAddressByteSize(addr_byte_size);
3885     // Dump all sections for all modules images
3886 
3887     if (command.GetArgumentCount() == 0) {
3888       ModuleSP current_module;
3889 
3890       // Where it is possible to look in the current symbol context first,
3891       // try that.  If this search was successful and --all was not passed,
3892       // don't print anything else.
3893       if (LookupHere(m_interpreter, result, syntax_error)) {
3894         result.GetOutputStream().EOL();
3895         num_successful_lookups++;
3896         if (!m_options.m_print_all) {
3897           result.SetStatus(eReturnStatusSuccessFinishResult);
3898           return result.Succeeded();
3899         }
3900       }
3901 
3902       // Dump all sections for all other modules
3903 
3904       const ModuleList &target_modules = target->GetImages();
3905       std::lock_guard<std::recursive_mutex> guard(target_modules.GetMutex());
3906       const size_t num_modules = target_modules.GetSize();
3907       if (num_modules > 0) {
3908         for (i = 0; i < num_modules && !syntax_error; ++i) {
3909           Module *module_pointer =
3910               target_modules.GetModulePointerAtIndexUnlocked(i);
3911 
3912           if (module_pointer != current_module.get() &&
3913               LookupInModule(m_interpreter,
3914                              target_modules.GetModulePointerAtIndexUnlocked(i),
3915                              result, syntax_error)) {
3916             result.GetOutputStream().EOL();
3917             num_successful_lookups++;
3918           }
3919         }
3920       } else {
3921         result.AppendError("the target has no associated executable images");
3922         result.SetStatus(eReturnStatusFailed);
3923         return false;
3924       }
3925     } else {
3926       // Dump specified images (by basename or fullpath)
3927       const char *arg_cstr;
3928       for (i = 0; (arg_cstr = command.GetArgumentAtIndex(i)) != nullptr &&
3929                   !syntax_error;
3930            ++i) {
3931         ModuleList module_list;
3932         const size_t num_matches =
3933             FindModulesByName(target, arg_cstr, module_list, false);
3934         if (num_matches > 0) {
3935           for (size_t j = 0; j < num_matches; ++j) {
3936             Module *module = module_list.GetModulePointerAtIndex(j);
3937             if (module) {
3938               if (LookupInModule(m_interpreter, module, result, syntax_error)) {
3939                 result.GetOutputStream().EOL();
3940                 num_successful_lookups++;
3941               }
3942             }
3943           }
3944         } else
3945           result.AppendWarningWithFormat(
3946               "Unable to find an image that matches '%s'.\n", arg_cstr);
3947       }
3948     }
3949 
3950     if (num_successful_lookups > 0)
3951       result.SetStatus(eReturnStatusSuccessFinishResult);
3952     else
3953       result.SetStatus(eReturnStatusFailed);
3954     return result.Succeeded();
3955   }
3956 
3957   CommandOptions m_options;
3958 };
3959 
3960 #pragma mark CommandObjectMultiwordImageSearchPaths
3961 
3962 // CommandObjectMultiwordImageSearchPaths
3963 
3964 class CommandObjectTargetModulesImageSearchPaths
3965     : public CommandObjectMultiword {
3966 public:
3967   CommandObjectTargetModulesImageSearchPaths(CommandInterpreter &interpreter)
3968       : CommandObjectMultiword(
3969             interpreter, "target modules search-paths",
3970             "Commands for managing module search paths for a target.",
3971             "target modules search-paths <subcommand> [<subcommand-options>]") {
3972     LoadSubCommand(
3973         "add", CommandObjectSP(
3974                    new CommandObjectTargetModulesSearchPathsAdd(interpreter)));
3975     LoadSubCommand(
3976         "clear", CommandObjectSP(new CommandObjectTargetModulesSearchPathsClear(
3977                      interpreter)));
3978     LoadSubCommand(
3979         "insert",
3980         CommandObjectSP(
3981             new CommandObjectTargetModulesSearchPathsInsert(interpreter)));
3982     LoadSubCommand(
3983         "list", CommandObjectSP(new CommandObjectTargetModulesSearchPathsList(
3984                     interpreter)));
3985     LoadSubCommand(
3986         "query", CommandObjectSP(new CommandObjectTargetModulesSearchPathsQuery(
3987                      interpreter)));
3988   }
3989 
3990   ~CommandObjectTargetModulesImageSearchPaths() override = default;
3991 };
3992 
3993 #pragma mark CommandObjectTargetModules
3994 
3995 // CommandObjectTargetModules
3996 
3997 class CommandObjectTargetModules : public CommandObjectMultiword {
3998 public:
3999   // Constructors and Destructors
4000   CommandObjectTargetModules(CommandInterpreter &interpreter)
4001       : CommandObjectMultiword(interpreter, "target modules",
4002                                "Commands for accessing information for one or "
4003                                "more target modules.",
4004                                "target modules <sub-command> ...") {
4005     LoadSubCommand(
4006         "add", CommandObjectSP(new CommandObjectTargetModulesAdd(interpreter)));
4007     LoadSubCommand("load", CommandObjectSP(new CommandObjectTargetModulesLoad(
4008                                interpreter)));
4009     LoadSubCommand("dump", CommandObjectSP(new CommandObjectTargetModulesDump(
4010                                interpreter)));
4011     LoadSubCommand("list", CommandObjectSP(new CommandObjectTargetModulesList(
4012                                interpreter)));
4013     LoadSubCommand(
4014         "lookup",
4015         CommandObjectSP(new CommandObjectTargetModulesLookup(interpreter)));
4016     LoadSubCommand(
4017         "search-paths",
4018         CommandObjectSP(
4019             new CommandObjectTargetModulesImageSearchPaths(interpreter)));
4020     LoadSubCommand(
4021         "show-unwind",
4022         CommandObjectSP(new CommandObjectTargetModulesShowUnwind(interpreter)));
4023   }
4024 
4025   ~CommandObjectTargetModules() override = default;
4026 
4027 private:
4028   // For CommandObjectTargetModules only
4029   DISALLOW_COPY_AND_ASSIGN(CommandObjectTargetModules);
4030 };
4031 
4032 class CommandObjectTargetSymbolsAdd : public CommandObjectParsed {
4033 public:
4034   CommandObjectTargetSymbolsAdd(CommandInterpreter &interpreter)
4035       : CommandObjectParsed(
4036             interpreter, "target symbols add",
4037             "Add a debug symbol file to one of the target's current modules by "
4038             "specifying a path to a debug symbols file or by using the options "
4039             "to specify a module.",
4040             "target symbols add <cmd-options> [<symfile>]",
4041             eCommandRequiresTarget),
4042         m_option_group(),
4043         m_file_option(
4044             LLDB_OPT_SET_1, false, "shlib", 's',
4045             CommandCompletions::eModuleCompletion, eArgTypeShlibName,
4046             "Locate the debug symbols for the shared library specified by "
4047             "name."),
4048         m_current_frame_option(
4049             LLDB_OPT_SET_2, false, "frame", 'F',
4050             "Locate the debug symbols for the currently selected frame.",
4051             false, true)
4052 
4053   {
4054     m_option_group.Append(&m_uuid_option_group, LLDB_OPT_SET_ALL,
4055                           LLDB_OPT_SET_1);
4056     m_option_group.Append(&m_file_option, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
4057     m_option_group.Append(&m_current_frame_option, LLDB_OPT_SET_2,
4058                           LLDB_OPT_SET_2);
4059     m_option_group.Finalize();
4060   }
4061 
4062   ~CommandObjectTargetSymbolsAdd() override = default;
4063 
4064   void
4065   HandleArgumentCompletion(CompletionRequest &request,
4066                            OptionElementVector &opt_element_vector) override {
4067     CommandCompletions::InvokeCommonCompletionCallbacks(
4068         GetCommandInterpreter(), CommandCompletions::eDiskFileCompletion,
4069         request, nullptr);
4070   }
4071 
4072   Options *GetOptions() override { return &m_option_group; }
4073 
4074 protected:
4075   bool AddModuleSymbols(Target *target, ModuleSpec &module_spec, bool &flush,
4076                         CommandReturnObject &result) {
4077     const FileSpec &symbol_fspec = module_spec.GetSymbolFileSpec();
4078     if (!symbol_fspec) {
4079       result.AppendError(
4080           "one or more executable image paths must be specified");
4081       result.SetStatus(eReturnStatusFailed);
4082       return false;
4083     }
4084 
4085     char symfile_path[PATH_MAX];
4086     symbol_fspec.GetPath(symfile_path, sizeof(symfile_path));
4087 
4088     if (!module_spec.GetUUID().IsValid()) {
4089       if (!module_spec.GetFileSpec() && !module_spec.GetPlatformFileSpec())
4090         module_spec.GetFileSpec().GetFilename() = symbol_fspec.GetFilename();
4091     }
4092 
4093     // Now module_spec represents a symbol file for a module that might exist
4094     // in the current target.  Let's find possible matches.
4095     ModuleList matching_modules;
4096 
4097     // First extract all module specs from the symbol file
4098     lldb_private::ModuleSpecList symfile_module_specs;
4099     if (ObjectFile::GetModuleSpecifications(module_spec.GetSymbolFileSpec(),
4100                                             0, 0, symfile_module_specs)) {
4101       // Now extract the module spec that matches the target architecture
4102       ModuleSpec target_arch_module_spec;
4103       ModuleSpec symfile_module_spec;
4104       target_arch_module_spec.GetArchitecture() = target->GetArchitecture();
4105       if (symfile_module_specs.FindMatchingModuleSpec(target_arch_module_spec,
4106                                                       symfile_module_spec)) {
4107         if (symfile_module_spec.GetUUID().IsValid()) {
4108           // It has a UUID, look for this UUID in the target modules
4109           ModuleSpec symfile_uuid_module_spec;
4110           symfile_uuid_module_spec.GetUUID() = symfile_module_spec.GetUUID();
4111           target->GetImages().FindModules(symfile_uuid_module_spec,
4112                                           matching_modules);
4113         }
4114       }
4115 
4116       if (matching_modules.IsEmpty()) {
4117         // No matches yet.  Iterate through the module specs to find a UUID
4118         // value that we can match up to an image in our target.
4119         const size_t num_symfile_module_specs = symfile_module_specs.GetSize();
4120         for (size_t i = 0;
4121              i < num_symfile_module_specs && matching_modules.IsEmpty(); ++i) {
4122           if (symfile_module_specs.GetModuleSpecAtIndex(
4123                   i, symfile_module_spec)) {
4124             if (symfile_module_spec.GetUUID().IsValid()) {
4125               // It has a UUID.  Look for this UUID in the target modules.
4126               ModuleSpec symfile_uuid_module_spec;
4127               symfile_uuid_module_spec.GetUUID() =
4128                   symfile_module_spec.GetUUID();
4129               target->GetImages().FindModules(symfile_uuid_module_spec,
4130                                               matching_modules);
4131             }
4132           }
4133         }
4134       }
4135     }
4136 
4137     // Just try to match up the file by basename if we have no matches at
4138     // this point.  For example, module foo might have symbols in foo.debug.
4139     if (matching_modules.IsEmpty())
4140       target->GetImages().FindModules(module_spec, matching_modules);
4141 
4142     while (matching_modules.IsEmpty()) {
4143       ConstString filename_no_extension(
4144           module_spec.GetFileSpec().GetFileNameStrippingExtension());
4145       // Empty string returned, let's bail
4146       if (!filename_no_extension)
4147         break;
4148 
4149       // Check if there was no extension to strip and the basename is the same
4150       if (filename_no_extension == module_spec.GetFileSpec().GetFilename())
4151         break;
4152 
4153       // Replace basename with one fewer extension
4154       module_spec.GetFileSpec().GetFilename() = filename_no_extension;
4155       target->GetImages().FindModules(module_spec, matching_modules);
4156     }
4157 
4158     if (matching_modules.GetSize() > 1) {
4159       result.AppendErrorWithFormat("multiple modules match symbol file '%s', "
4160                                    "use the --uuid option to resolve the "
4161                                    "ambiguity.\n",
4162                                    symfile_path);
4163       result.SetStatus(eReturnStatusFailed);
4164       return false;
4165     }
4166 
4167     if (matching_modules.GetSize() == 1) {
4168       ModuleSP module_sp(matching_modules.GetModuleAtIndex(0));
4169 
4170       // The module has not yet created its symbol vendor, we can just give
4171       // the existing target module the symfile path to use for when it
4172       // decides to create it!
4173       module_sp->SetSymbolFileFileSpec(symbol_fspec);
4174 
4175       SymbolFile *symbol_file =
4176           module_sp->GetSymbolFile(true, &result.GetErrorStream());
4177       if (symbol_file) {
4178         ObjectFile *object_file = symbol_file->GetObjectFile();
4179         if (object_file && object_file->GetFileSpec() == symbol_fspec) {
4180           // Provide feedback that the symfile has been successfully added.
4181           const FileSpec &module_fs = module_sp->GetFileSpec();
4182           result.AppendMessageWithFormat(
4183               "symbol file '%s' has been added to '%s'\n", symfile_path,
4184               module_fs.GetPath().c_str());
4185 
4186           // Let clients know something changed in the module if it is
4187           // currently loaded
4188           ModuleList module_list;
4189           module_list.Append(module_sp);
4190           target->SymbolsDidLoad(module_list);
4191 
4192           // Make sure we load any scripting resources that may be embedded
4193           // in the debug info files in case the platform supports that.
4194           Status error;
4195           StreamString feedback_stream;
4196           module_sp->LoadScriptingResourceInTarget(target, error,
4197                                                    &feedback_stream);
4198           if (error.Fail() && error.AsCString())
4199             result.AppendWarningWithFormat(
4200                 "unable to load scripting data for module %s - error "
4201                 "reported was %s",
4202                 module_sp->GetFileSpec()
4203                     .GetFileNameStrippingExtension()
4204                     .GetCString(),
4205                 error.AsCString());
4206           else if (feedback_stream.GetSize())
4207             result.AppendWarning(feedback_stream.GetData());
4208 
4209           flush = true;
4210           result.SetStatus(eReturnStatusSuccessFinishResult);
4211           return true;
4212         }
4213       }
4214       // Clear the symbol file spec if anything went wrong
4215       module_sp->SetSymbolFileFileSpec(FileSpec());
4216     }
4217 
4218     StreamString ss_symfile_uuid;
4219     if (module_spec.GetUUID().IsValid()) {
4220       ss_symfile_uuid << " (";
4221       module_spec.GetUUID().Dump(&ss_symfile_uuid);
4222       ss_symfile_uuid << ')';
4223     }
4224     result.AppendErrorWithFormat(
4225         "symbol file '%s'%s does not match any existing module%s\n",
4226         symfile_path, ss_symfile_uuid.GetData(),
4227         !llvm::sys::fs::is_regular_file(symbol_fspec.GetPath())
4228             ? "\n       please specify the full path to the symbol file"
4229             : "");
4230     result.SetStatus(eReturnStatusFailed);
4231     return false;
4232   }
4233 
4234   bool DoExecute(Args &args, CommandReturnObject &result) override {
4235     Target *target = m_exe_ctx.GetTargetPtr();
4236     result.SetStatus(eReturnStatusFailed);
4237     bool flush = false;
4238     ModuleSpec module_spec;
4239     const bool uuid_option_set =
4240         m_uuid_option_group.GetOptionValue().OptionWasSet();
4241     const bool file_option_set = m_file_option.GetOptionValue().OptionWasSet();
4242     const bool frame_option_set =
4243         m_current_frame_option.GetOptionValue().OptionWasSet();
4244     const size_t argc = args.GetArgumentCount();
4245 
4246     if (argc == 0) {
4247       if (uuid_option_set || file_option_set || frame_option_set) {
4248         bool success = false;
4249         bool error_set = false;
4250         if (frame_option_set) {
4251           Process *process = m_exe_ctx.GetProcessPtr();
4252           if (process) {
4253             const StateType process_state = process->GetState();
4254             if (StateIsStoppedState(process_state, true)) {
4255               StackFrame *frame = m_exe_ctx.GetFramePtr();
4256               if (frame) {
4257                 ModuleSP frame_module_sp(
4258                     frame->GetSymbolContext(eSymbolContextModule).module_sp);
4259                 if (frame_module_sp) {
4260                   if (FileSystem::Instance().Exists(
4261                           frame_module_sp->GetPlatformFileSpec())) {
4262                     module_spec.GetArchitecture() =
4263                         frame_module_sp->GetArchitecture();
4264                     module_spec.GetFileSpec() =
4265                         frame_module_sp->GetPlatformFileSpec();
4266                   }
4267                   module_spec.GetUUID() = frame_module_sp->GetUUID();
4268                   success = module_spec.GetUUID().IsValid() ||
4269                             module_spec.GetFileSpec();
4270                 } else {
4271                   result.AppendError("frame has no module");
4272                   error_set = true;
4273                 }
4274               } else {
4275                 result.AppendError("invalid current frame");
4276                 error_set = true;
4277               }
4278             } else {
4279               result.AppendErrorWithFormat("process is not stopped: %s",
4280                                            StateAsCString(process_state));
4281               error_set = true;
4282             }
4283           } else {
4284             result.AppendError(
4285                 "a process must exist in order to use the --frame option");
4286             error_set = true;
4287           }
4288         } else {
4289           if (uuid_option_set) {
4290             module_spec.GetUUID() =
4291                 m_uuid_option_group.GetOptionValue().GetCurrentValue();
4292             success |= module_spec.GetUUID().IsValid();
4293           } else if (file_option_set) {
4294             module_spec.GetFileSpec() =
4295                 m_file_option.GetOptionValue().GetCurrentValue();
4296             ModuleSP module_sp(
4297                 target->GetImages().FindFirstModule(module_spec));
4298             if (module_sp) {
4299               module_spec.GetFileSpec() = module_sp->GetFileSpec();
4300               module_spec.GetPlatformFileSpec() =
4301                   module_sp->GetPlatformFileSpec();
4302               module_spec.GetUUID() = module_sp->GetUUID();
4303               module_spec.GetArchitecture() = module_sp->GetArchitecture();
4304             } else {
4305               module_spec.GetArchitecture() = target->GetArchitecture();
4306             }
4307             success |= module_spec.GetUUID().IsValid() ||
4308                        FileSystem::Instance().Exists(module_spec.GetFileSpec());
4309           }
4310         }
4311 
4312         if (success) {
4313           if (Symbols::DownloadObjectAndSymbolFile(module_spec)) {
4314             if (module_spec.GetSymbolFileSpec())
4315               success = AddModuleSymbols(target, module_spec, flush, result);
4316           }
4317         }
4318 
4319         if (!success && !error_set) {
4320           StreamString error_strm;
4321           if (uuid_option_set) {
4322             error_strm.PutCString("unable to find debug symbols for UUID ");
4323             module_spec.GetUUID().Dump(&error_strm);
4324           } else if (file_option_set) {
4325             error_strm.PutCString(
4326                 "unable to find debug symbols for the executable file ");
4327             error_strm << module_spec.GetFileSpec();
4328           } else if (frame_option_set) {
4329             error_strm.PutCString(
4330                 "unable to find debug symbols for the current frame");
4331           }
4332           result.AppendError(error_strm.GetString());
4333         }
4334       } else {
4335         result.AppendError("one or more symbol file paths must be specified, "
4336                            "or options must be specified");
4337       }
4338     } else {
4339       if (uuid_option_set) {
4340         result.AppendError("specify either one or more paths to symbol files "
4341                            "or use the --uuid option without arguments");
4342       } else if (frame_option_set) {
4343         result.AppendError("specify either one or more paths to symbol files "
4344                            "or use the --frame option without arguments");
4345       } else if (file_option_set && argc > 1) {
4346         result.AppendError("specify at most one symbol file path when "
4347                            "--shlib option is set");
4348       } else {
4349         PlatformSP platform_sp(target->GetPlatform());
4350 
4351         for (auto &entry : args.entries()) {
4352           if (!entry.ref().empty()) {
4353             auto &symbol_file_spec = module_spec.GetSymbolFileSpec();
4354             symbol_file_spec.SetFile(entry.ref(), FileSpec::Style::native);
4355             FileSystem::Instance().Resolve(symbol_file_spec);
4356             if (file_option_set) {
4357               module_spec.GetFileSpec() =
4358                   m_file_option.GetOptionValue().GetCurrentValue();
4359             }
4360             if (platform_sp) {
4361               FileSpec symfile_spec;
4362               if (platform_sp
4363                       ->ResolveSymbolFile(*target, module_spec, symfile_spec)
4364                       .Success())
4365                 module_spec.GetSymbolFileSpec() = symfile_spec;
4366             }
4367 
4368             ArchSpec arch;
4369             bool symfile_exists =
4370                 FileSystem::Instance().Exists(module_spec.GetSymbolFileSpec());
4371 
4372             if (symfile_exists) {
4373               if (!AddModuleSymbols(target, module_spec, flush, result))
4374                 break;
4375             } else {
4376               std::string resolved_symfile_path =
4377                   module_spec.GetSymbolFileSpec().GetPath();
4378               if (resolved_symfile_path != entry.ref()) {
4379                 result.AppendErrorWithFormat(
4380                     "invalid module path '%s' with resolved path '%s'\n",
4381                     entry.c_str(), resolved_symfile_path.c_str());
4382                 break;
4383               }
4384               result.AppendErrorWithFormat("invalid module path '%s'\n",
4385                                            entry.c_str());
4386               break;
4387             }
4388           }
4389         }
4390       }
4391     }
4392 
4393     if (flush) {
4394       Process *process = m_exe_ctx.GetProcessPtr();
4395       if (process)
4396         process->Flush();
4397     }
4398     return result.Succeeded();
4399   }
4400 
4401   OptionGroupOptions m_option_group;
4402   OptionGroupUUID m_uuid_option_group;
4403   OptionGroupFile m_file_option;
4404   OptionGroupBoolean m_current_frame_option;
4405 };
4406 
4407 #pragma mark CommandObjectTargetSymbols
4408 
4409 // CommandObjectTargetSymbols
4410 
4411 class CommandObjectTargetSymbols : public CommandObjectMultiword {
4412 public:
4413   // Constructors and Destructors
4414   CommandObjectTargetSymbols(CommandInterpreter &interpreter)
4415       : CommandObjectMultiword(
4416             interpreter, "target symbols",
4417             "Commands for adding and managing debug symbol files.",
4418             "target symbols <sub-command> ...") {
4419     LoadSubCommand(
4420         "add", CommandObjectSP(new CommandObjectTargetSymbolsAdd(interpreter)));
4421   }
4422 
4423   ~CommandObjectTargetSymbols() override = default;
4424 
4425 private:
4426   // For CommandObjectTargetModules only
4427   DISALLOW_COPY_AND_ASSIGN(CommandObjectTargetSymbols);
4428 };
4429 
4430 #pragma mark CommandObjectTargetStopHookAdd
4431 
4432 // CommandObjectTargetStopHookAdd
4433 #define LLDB_OPTIONS_target_stop_hook_add
4434 #include "CommandOptions.inc"
4435 
4436 class CommandObjectTargetStopHookAdd : public CommandObjectParsed,
4437                                        public IOHandlerDelegateMultiline {
4438 public:
4439   class CommandOptions : public Options {
4440   public:
4441     CommandOptions()
4442         : Options(), m_line_start(0), m_line_end(UINT_MAX),
4443           m_func_name_type_mask(eFunctionNameTypeAuto),
4444           m_sym_ctx_specified(false), m_thread_specified(false),
4445           m_use_one_liner(false), m_one_liner() {}
4446 
4447     ~CommandOptions() override = default;
4448 
4449     llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
4450       return llvm::makeArrayRef(g_target_stop_hook_add_options);
4451     }
4452 
4453     Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
4454                           ExecutionContext *execution_context) override {
4455       Status error;
4456       const int short_option = m_getopt_table[option_idx].val;
4457 
4458       switch (short_option) {
4459       case 'c':
4460         m_class_name = std::string(option_arg);
4461         m_sym_ctx_specified = true;
4462         break;
4463 
4464       case 'e':
4465         if (option_arg.getAsInteger(0, m_line_end)) {
4466           error.SetErrorStringWithFormat("invalid end line number: \"%s\"",
4467                                          option_arg.str().c_str());
4468           break;
4469         }
4470         m_sym_ctx_specified = true;
4471         break;
4472 
4473       case 'G': {
4474         bool value, success;
4475         value = OptionArgParser::ToBoolean(option_arg, false, &success);
4476         if (success) {
4477           m_auto_continue = value;
4478         } else
4479           error.SetErrorStringWithFormat(
4480               "invalid boolean value '%s' passed for -G option",
4481               option_arg.str().c_str());
4482       } break;
4483       case 'l':
4484         if (option_arg.getAsInteger(0, m_line_start)) {
4485           error.SetErrorStringWithFormat("invalid start line number: \"%s\"",
4486                                          option_arg.str().c_str());
4487           break;
4488         }
4489         m_sym_ctx_specified = true;
4490         break;
4491 
4492       case 'i':
4493         m_no_inlines = true;
4494         break;
4495 
4496       case 'n':
4497         m_function_name = std::string(option_arg);
4498         m_func_name_type_mask |= eFunctionNameTypeAuto;
4499         m_sym_ctx_specified = true;
4500         break;
4501 
4502       case 'f':
4503         m_file_name = std::string(option_arg);
4504         m_sym_ctx_specified = true;
4505         break;
4506 
4507       case 's':
4508         m_module_name = std::string(option_arg);
4509         m_sym_ctx_specified = true;
4510         break;
4511 
4512       case 't':
4513         if (option_arg.getAsInteger(0, m_thread_id))
4514           error.SetErrorStringWithFormat("invalid thread id string '%s'",
4515                                          option_arg.str().c_str());
4516         m_thread_specified = true;
4517         break;
4518 
4519       case 'T':
4520         m_thread_name = std::string(option_arg);
4521         m_thread_specified = true;
4522         break;
4523 
4524       case 'q':
4525         m_queue_name = std::string(option_arg);
4526         m_thread_specified = true;
4527         break;
4528 
4529       case 'x':
4530         if (option_arg.getAsInteger(0, m_thread_index))
4531           error.SetErrorStringWithFormat("invalid thread index string '%s'",
4532                                          option_arg.str().c_str());
4533         m_thread_specified = true;
4534         break;
4535 
4536       case 'o':
4537         m_use_one_liner = true;
4538         m_one_liner.push_back(std::string(option_arg));
4539         break;
4540 
4541       default:
4542         llvm_unreachable("Unimplemented option");
4543       }
4544       return error;
4545     }
4546 
4547     void OptionParsingStarting(ExecutionContext *execution_context) override {
4548       m_class_name.clear();
4549       m_function_name.clear();
4550       m_line_start = 0;
4551       m_line_end = UINT_MAX;
4552       m_file_name.clear();
4553       m_module_name.clear();
4554       m_func_name_type_mask = eFunctionNameTypeAuto;
4555       m_thread_id = LLDB_INVALID_THREAD_ID;
4556       m_thread_index = UINT32_MAX;
4557       m_thread_name.clear();
4558       m_queue_name.clear();
4559 
4560       m_no_inlines = false;
4561       m_sym_ctx_specified = false;
4562       m_thread_specified = false;
4563 
4564       m_use_one_liner = false;
4565       m_one_liner.clear();
4566       m_auto_continue = false;
4567     }
4568 
4569     std::string m_class_name;
4570     std::string m_function_name;
4571     uint32_t m_line_start;
4572     uint32_t m_line_end;
4573     std::string m_file_name;
4574     std::string m_module_name;
4575     uint32_t m_func_name_type_mask; // A pick from lldb::FunctionNameType.
4576     lldb::tid_t m_thread_id;
4577     uint32_t m_thread_index;
4578     std::string m_thread_name;
4579     std::string m_queue_name;
4580     bool m_sym_ctx_specified;
4581     bool m_no_inlines;
4582     bool m_thread_specified;
4583     // Instance variables to hold the values for one_liner options.
4584     bool m_use_one_liner;
4585     std::vector<std::string> m_one_liner;
4586     bool m_auto_continue;
4587   };
4588 
4589   CommandObjectTargetStopHookAdd(CommandInterpreter &interpreter)
4590       : CommandObjectParsed(interpreter, "target stop-hook add",
4591                             "Add a hook to be executed when the target stops.",
4592                             "target stop-hook add"),
4593         IOHandlerDelegateMultiline("DONE",
4594                                    IOHandlerDelegate::Completion::LLDBCommand),
4595         m_options() {}
4596 
4597   ~CommandObjectTargetStopHookAdd() override = default;
4598 
4599   Options *GetOptions() override { return &m_options; }
4600 
4601 protected:
4602   void IOHandlerActivated(IOHandler &io_handler, bool interactive) override {
4603     StreamFileSP output_sp(io_handler.GetOutputStreamFileSP());
4604     if (output_sp && interactive) {
4605       output_sp->PutCString(
4606           "Enter your stop hook command(s).  Type 'DONE' to end.\n");
4607       output_sp->Flush();
4608     }
4609   }
4610 
4611   void IOHandlerInputComplete(IOHandler &io_handler,
4612                               std::string &line) override {
4613     if (m_stop_hook_sp) {
4614       if (line.empty()) {
4615         StreamFileSP error_sp(io_handler.GetErrorStreamFileSP());
4616         if (error_sp) {
4617           error_sp->Printf("error: stop hook #%" PRIu64
4618                            " aborted, no commands.\n",
4619                            m_stop_hook_sp->GetID());
4620           error_sp->Flush();
4621         }
4622         Target *target = GetDebugger().GetSelectedTarget().get();
4623         if (target)
4624           target->RemoveStopHookByID(m_stop_hook_sp->GetID());
4625       } else {
4626         m_stop_hook_sp->GetCommandPointer()->SplitIntoLines(line);
4627         StreamFileSP output_sp(io_handler.GetOutputStreamFileSP());
4628         if (output_sp) {
4629           output_sp->Printf("Stop hook #%" PRIu64 " added.\n",
4630                             m_stop_hook_sp->GetID());
4631           output_sp->Flush();
4632         }
4633       }
4634       m_stop_hook_sp.reset();
4635     }
4636     io_handler.SetIsDone(true);
4637   }
4638 
4639   bool DoExecute(Args &command, CommandReturnObject &result) override {
4640     m_stop_hook_sp.reset();
4641 
4642     Target &target = GetSelectedOrDummyTarget();
4643     Target::StopHookSP new_hook_sp = target.CreateStopHook();
4644 
4645     //  First step, make the specifier.
4646     std::unique_ptr<SymbolContextSpecifier> specifier_up;
4647     if (m_options.m_sym_ctx_specified) {
4648       specifier_up.reset(
4649           new SymbolContextSpecifier(GetDebugger().GetSelectedTarget()));
4650 
4651       if (!m_options.m_module_name.empty()) {
4652         specifier_up->AddSpecification(
4653             m_options.m_module_name.c_str(),
4654             SymbolContextSpecifier::eModuleSpecified);
4655       }
4656 
4657       if (!m_options.m_class_name.empty()) {
4658         specifier_up->AddSpecification(
4659             m_options.m_class_name.c_str(),
4660             SymbolContextSpecifier::eClassOrNamespaceSpecified);
4661       }
4662 
4663       if (!m_options.m_file_name.empty()) {
4664         specifier_up->AddSpecification(m_options.m_file_name.c_str(),
4665                                        SymbolContextSpecifier::eFileSpecified);
4666       }
4667 
4668       if (m_options.m_line_start != 0) {
4669         specifier_up->AddLineSpecification(
4670             m_options.m_line_start,
4671             SymbolContextSpecifier::eLineStartSpecified);
4672       }
4673 
4674       if (m_options.m_line_end != UINT_MAX) {
4675         specifier_up->AddLineSpecification(
4676             m_options.m_line_end, SymbolContextSpecifier::eLineEndSpecified);
4677       }
4678 
4679       if (!m_options.m_function_name.empty()) {
4680         specifier_up->AddSpecification(
4681             m_options.m_function_name.c_str(),
4682             SymbolContextSpecifier::eFunctionSpecified);
4683       }
4684     }
4685 
4686     if (specifier_up)
4687       new_hook_sp->SetSpecifier(specifier_up.release());
4688 
4689     // Next see if any of the thread options have been entered:
4690 
4691     if (m_options.m_thread_specified) {
4692       ThreadSpec *thread_spec = new ThreadSpec();
4693 
4694       if (m_options.m_thread_id != LLDB_INVALID_THREAD_ID) {
4695         thread_spec->SetTID(m_options.m_thread_id);
4696       }
4697 
4698       if (m_options.m_thread_index != UINT32_MAX)
4699         thread_spec->SetIndex(m_options.m_thread_index);
4700 
4701       if (!m_options.m_thread_name.empty())
4702         thread_spec->SetName(m_options.m_thread_name.c_str());
4703 
4704       if (!m_options.m_queue_name.empty())
4705         thread_spec->SetQueueName(m_options.m_queue_name.c_str());
4706 
4707       new_hook_sp->SetThreadSpecifier(thread_spec);
4708     }
4709 
4710     new_hook_sp->SetAutoContinue(m_options.m_auto_continue);
4711     if (m_options.m_use_one_liner) {
4712       // Use one-liners.
4713       for (auto cmd : m_options.m_one_liner)
4714         new_hook_sp->GetCommandPointer()->AppendString(cmd.c_str());
4715       result.AppendMessageWithFormat("Stop hook #%" PRIu64 " added.\n",
4716                                      new_hook_sp->GetID());
4717     } else {
4718       m_stop_hook_sp = new_hook_sp;
4719       m_interpreter.GetLLDBCommandsFromIOHandler("> ",   // Prompt
4720                                                  *this); // IOHandlerDelegate
4721     }
4722     result.SetStatus(eReturnStatusSuccessFinishNoResult);
4723 
4724     return result.Succeeded();
4725   }
4726 
4727 private:
4728   CommandOptions m_options;
4729   Target::StopHookSP m_stop_hook_sp;
4730 };
4731 
4732 #pragma mark CommandObjectTargetStopHookDelete
4733 
4734 // CommandObjectTargetStopHookDelete
4735 
4736 class CommandObjectTargetStopHookDelete : public CommandObjectParsed {
4737 public:
4738   CommandObjectTargetStopHookDelete(CommandInterpreter &interpreter)
4739       : CommandObjectParsed(interpreter, "target stop-hook delete",
4740                             "Delete a stop-hook.",
4741                             "target stop-hook delete [<idx>]") {}
4742 
4743   ~CommandObjectTargetStopHookDelete() override = default;
4744 
4745 protected:
4746   bool DoExecute(Args &command, CommandReturnObject &result) override {
4747     Target &target = GetSelectedOrDummyTarget();
4748     // FIXME: see if we can use the breakpoint id style parser?
4749     size_t num_args = command.GetArgumentCount();
4750     if (num_args == 0) {
4751       if (!m_interpreter.Confirm("Delete all stop hooks?", true)) {
4752         result.SetStatus(eReturnStatusFailed);
4753         return false;
4754       } else {
4755         target.RemoveAllStopHooks();
4756       }
4757     } else {
4758       bool success;
4759       for (size_t i = 0; i < num_args; i++) {
4760         lldb::user_id_t user_id = StringConvert::ToUInt32(
4761             command.GetArgumentAtIndex(i), 0, 0, &success);
4762         if (!success) {
4763           result.AppendErrorWithFormat("invalid stop hook id: \"%s\".\n",
4764                                        command.GetArgumentAtIndex(i));
4765           result.SetStatus(eReturnStatusFailed);
4766           return false;
4767         }
4768         success = target.RemoveStopHookByID(user_id);
4769         if (!success) {
4770           result.AppendErrorWithFormat("unknown stop hook id: \"%s\".\n",
4771                                        command.GetArgumentAtIndex(i));
4772           result.SetStatus(eReturnStatusFailed);
4773           return false;
4774         }
4775       }
4776     }
4777     result.SetStatus(eReturnStatusSuccessFinishNoResult);
4778     return result.Succeeded();
4779   }
4780 };
4781 
4782 #pragma mark CommandObjectTargetStopHookEnableDisable
4783 
4784 // CommandObjectTargetStopHookEnableDisable
4785 
4786 class CommandObjectTargetStopHookEnableDisable : public CommandObjectParsed {
4787 public:
4788   CommandObjectTargetStopHookEnableDisable(CommandInterpreter &interpreter,
4789                                            bool enable, const char *name,
4790                                            const char *help, const char *syntax)
4791       : CommandObjectParsed(interpreter, name, help, syntax), m_enable(enable) {
4792   }
4793 
4794   ~CommandObjectTargetStopHookEnableDisable() override = default;
4795 
4796 protected:
4797   bool DoExecute(Args &command, CommandReturnObject &result) override {
4798     Target &target = GetSelectedOrDummyTarget();
4799     // FIXME: see if we can use the breakpoint id style parser?
4800     size_t num_args = command.GetArgumentCount();
4801     bool success;
4802 
4803     if (num_args == 0) {
4804       target.SetAllStopHooksActiveState(m_enable);
4805     } else {
4806       for (size_t i = 0; i < num_args; i++) {
4807         lldb::user_id_t user_id = StringConvert::ToUInt32(
4808             command.GetArgumentAtIndex(i), 0, 0, &success);
4809         if (!success) {
4810           result.AppendErrorWithFormat("invalid stop hook id: \"%s\".\n",
4811                                        command.GetArgumentAtIndex(i));
4812           result.SetStatus(eReturnStatusFailed);
4813           return false;
4814         }
4815         success = target.SetStopHookActiveStateByID(user_id, m_enable);
4816         if (!success) {
4817           result.AppendErrorWithFormat("unknown stop hook id: \"%s\".\n",
4818                                        command.GetArgumentAtIndex(i));
4819           result.SetStatus(eReturnStatusFailed);
4820           return false;
4821         }
4822       }
4823     }
4824     result.SetStatus(eReturnStatusSuccessFinishNoResult);
4825     return result.Succeeded();
4826   }
4827 
4828 private:
4829   bool m_enable;
4830 };
4831 
4832 #pragma mark CommandObjectTargetStopHookList
4833 
4834 // CommandObjectTargetStopHookList
4835 
4836 class CommandObjectTargetStopHookList : public CommandObjectParsed {
4837 public:
4838   CommandObjectTargetStopHookList(CommandInterpreter &interpreter)
4839       : CommandObjectParsed(interpreter, "target stop-hook list",
4840                             "List all stop-hooks.",
4841                             "target stop-hook list [<type>]") {}
4842 
4843   ~CommandObjectTargetStopHookList() override = default;
4844 
4845 protected:
4846   bool DoExecute(Args &command, CommandReturnObject &result) override {
4847     Target &target = GetSelectedOrDummyTarget();
4848 
4849     size_t num_hooks = target.GetNumStopHooks();
4850     if (num_hooks == 0) {
4851       result.GetOutputStream().PutCString("No stop hooks.\n");
4852     } else {
4853       for (size_t i = 0; i < num_hooks; i++) {
4854         Target::StopHookSP this_hook = target.GetStopHookAtIndex(i);
4855         if (i > 0)
4856           result.GetOutputStream().PutCString("\n");
4857         this_hook->GetDescription(&(result.GetOutputStream()),
4858                                   eDescriptionLevelFull);
4859       }
4860     }
4861     result.SetStatus(eReturnStatusSuccessFinishResult);
4862     return result.Succeeded();
4863   }
4864 };
4865 
4866 #pragma mark CommandObjectMultiwordTargetStopHooks
4867 
4868 // CommandObjectMultiwordTargetStopHooks
4869 
4870 class CommandObjectMultiwordTargetStopHooks : public CommandObjectMultiword {
4871 public:
4872   CommandObjectMultiwordTargetStopHooks(CommandInterpreter &interpreter)
4873       : CommandObjectMultiword(
4874             interpreter, "target stop-hook",
4875             "Commands for operating on debugger target stop-hooks.",
4876             "target stop-hook <subcommand> [<subcommand-options>]") {
4877     LoadSubCommand("add", CommandObjectSP(
4878                               new CommandObjectTargetStopHookAdd(interpreter)));
4879     LoadSubCommand(
4880         "delete",
4881         CommandObjectSP(new CommandObjectTargetStopHookDelete(interpreter)));
4882     LoadSubCommand("disable",
4883                    CommandObjectSP(new CommandObjectTargetStopHookEnableDisable(
4884                        interpreter, false, "target stop-hook disable [<id>]",
4885                        "Disable a stop-hook.", "target stop-hook disable")));
4886     LoadSubCommand("enable",
4887                    CommandObjectSP(new CommandObjectTargetStopHookEnableDisable(
4888                        interpreter, true, "target stop-hook enable [<id>]",
4889                        "Enable a stop-hook.", "target stop-hook enable")));
4890     LoadSubCommand("list", CommandObjectSP(new CommandObjectTargetStopHookList(
4891                                interpreter)));
4892   }
4893 
4894   ~CommandObjectMultiwordTargetStopHooks() override = default;
4895 };
4896 
4897 #pragma mark CommandObjectMultiwordTarget
4898 
4899 // CommandObjectMultiwordTarget
4900 
4901 CommandObjectMultiwordTarget::CommandObjectMultiwordTarget(
4902     CommandInterpreter &interpreter)
4903     : CommandObjectMultiword(interpreter, "target",
4904                              "Commands for operating on debugger targets.",
4905                              "target <subcommand> [<subcommand-options>]") {
4906   LoadSubCommand("create",
4907                  CommandObjectSP(new CommandObjectTargetCreate(interpreter)));
4908   LoadSubCommand("delete",
4909                  CommandObjectSP(new CommandObjectTargetDelete(interpreter)));
4910   LoadSubCommand("list",
4911                  CommandObjectSP(new CommandObjectTargetList(interpreter)));
4912   LoadSubCommand("select",
4913                  CommandObjectSP(new CommandObjectTargetSelect(interpreter)));
4914   LoadSubCommand("show-launch-environment",
4915                  CommandObjectSP(new CommandObjectTargetShowLaunchEnvironment(
4916                      interpreter)));
4917   LoadSubCommand(
4918       "stop-hook",
4919       CommandObjectSP(new CommandObjectMultiwordTargetStopHooks(interpreter)));
4920   LoadSubCommand("modules",
4921                  CommandObjectSP(new CommandObjectTargetModules(interpreter)));
4922   LoadSubCommand("symbols",
4923                  CommandObjectSP(new CommandObjectTargetSymbols(interpreter)));
4924   LoadSubCommand("variable",
4925                  CommandObjectSP(new CommandObjectTargetVariable(interpreter)));
4926 }
4927 
4928 CommandObjectMultiwordTarget::~CommandObjectMultiwordTarget() = default;
4929