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