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 protected:
1161   bool DoExecute(Args &command, CommandReturnObject &result) override {
1162     Target *target = &GetSelectedTarget();
1163     size_t argc = command.GetArgumentCount();
1164     // check for at least 3 arguments and an odd number of parameters
1165     if (argc >= 3 && argc & 1) {
1166       uint32_t insert_idx;
1167 
1168       if (!llvm::to_integer(command.GetArgumentAtIndex(0), insert_idx)) {
1169         result.AppendErrorWithFormat(
1170             "<index> parameter is not an integer: '%s'.\n",
1171             command.GetArgumentAtIndex(0));
1172         result.SetStatus(eReturnStatusFailed);
1173         return result.Succeeded();
1174       }
1175 
1176       // shift off the index
1177       command.Shift();
1178       argc = command.GetArgumentCount();
1179 
1180       for (uint32_t i = 0; i < argc; i += 2, ++insert_idx) {
1181         const char *from = command.GetArgumentAtIndex(i);
1182         const char *to = command.GetArgumentAtIndex(i + 1);
1183 
1184         if (from[0] && to[0]) {
1185           bool last_pair = ((argc - i) == 2);
1186           target->GetImageSearchPathList().Insert(
1187               ConstString(from), ConstString(to), insert_idx, last_pair);
1188           result.SetStatus(eReturnStatusSuccessFinishNoResult);
1189         } else {
1190           if (from[0])
1191             result.AppendError("<path-prefix> can't be empty\n");
1192           else
1193             result.AppendError("<new-path-prefix> can't be empty\n");
1194           result.SetStatus(eReturnStatusFailed);
1195           return false;
1196         }
1197       }
1198     } else {
1199       result.AppendError("insert requires at least three arguments\n");
1200       result.SetStatus(eReturnStatusFailed);
1201       return result.Succeeded();
1202     }
1203     return result.Succeeded();
1204   }
1205 };
1206 
1207 #pragma mark CommandObjectTargetModulesSearchPathsList
1208 
1209 class CommandObjectTargetModulesSearchPathsList : public CommandObjectParsed {
1210 public:
1211   CommandObjectTargetModulesSearchPathsList(CommandInterpreter &interpreter)
1212       : CommandObjectParsed(interpreter, "target modules search-paths list",
1213                             "List all current image search path substitution "
1214                             "pairs in the current target.",
1215                             "target modules search-paths list",
1216                             eCommandRequiresTarget) {}
1217 
1218   ~CommandObjectTargetModulesSearchPathsList() override = default;
1219 
1220 protected:
1221   bool DoExecute(Args &command, CommandReturnObject &result) override {
1222     Target *target = &GetSelectedTarget();
1223     if (command.GetArgumentCount() != 0) {
1224       result.AppendError("list takes no arguments\n");
1225       result.SetStatus(eReturnStatusFailed);
1226       return result.Succeeded();
1227     }
1228 
1229     target->GetImageSearchPathList().Dump(&result.GetOutputStream());
1230     result.SetStatus(eReturnStatusSuccessFinishResult);
1231     return result.Succeeded();
1232   }
1233 };
1234 
1235 #pragma mark CommandObjectTargetModulesSearchPathsQuery
1236 
1237 class CommandObjectTargetModulesSearchPathsQuery : public CommandObjectParsed {
1238 public:
1239   CommandObjectTargetModulesSearchPathsQuery(CommandInterpreter &interpreter)
1240       : CommandObjectParsed(
1241             interpreter, "target modules search-paths query",
1242             "Transform a path using the first applicable image search path.",
1243             nullptr, eCommandRequiresTarget) {
1244     CommandArgumentEntry arg;
1245     CommandArgumentData path_arg;
1246 
1247     // Define the first (and only) variant of this arg.
1248     path_arg.arg_type = eArgTypeDirectoryName;
1249     path_arg.arg_repetition = eArgRepeatPlain;
1250 
1251     // There is only one variant this argument could be; put it into the
1252     // argument entry.
1253     arg.push_back(path_arg);
1254 
1255     // Push the data for the first argument into the m_arguments vector.
1256     m_arguments.push_back(arg);
1257   }
1258 
1259   ~CommandObjectTargetModulesSearchPathsQuery() override = default;
1260 
1261 protected:
1262   bool DoExecute(Args &command, CommandReturnObject &result) override {
1263     Target *target = &GetSelectedTarget();
1264     if (command.GetArgumentCount() != 1) {
1265       result.AppendError("query requires one argument\n");
1266       result.SetStatus(eReturnStatusFailed);
1267       return result.Succeeded();
1268     }
1269 
1270     ConstString orig(command.GetArgumentAtIndex(0));
1271     ConstString transformed;
1272     if (target->GetImageSearchPathList().RemapPath(orig, transformed))
1273       result.GetOutputStream().Printf("%s\n", transformed.GetCString());
1274     else
1275       result.GetOutputStream().Printf("%s\n", orig.GetCString());
1276 
1277     result.SetStatus(eReturnStatusSuccessFinishResult);
1278     return result.Succeeded();
1279   }
1280 };
1281 
1282 // Static Helper functions
1283 static void DumpModuleArchitecture(Stream &strm, Module *module,
1284                                    bool full_triple, uint32_t width) {
1285   if (module) {
1286     StreamString arch_strm;
1287 
1288     if (full_triple)
1289       module->GetArchitecture().DumpTriple(arch_strm.AsRawOstream());
1290     else
1291       arch_strm.PutCString(module->GetArchitecture().GetArchitectureName());
1292     std::string arch_str = std::string(arch_strm.GetString());
1293 
1294     if (width)
1295       strm.Printf("%-*s", width, arch_str.c_str());
1296     else
1297       strm.PutCString(arch_str);
1298   }
1299 }
1300 
1301 static void DumpModuleUUID(Stream &strm, Module *module) {
1302   if (module && module->GetUUID().IsValid())
1303     module->GetUUID().Dump(&strm);
1304   else
1305     strm.PutCString("                                    ");
1306 }
1307 
1308 static uint32_t DumpCompileUnitLineTable(CommandInterpreter &interpreter,
1309                                          Stream &strm, Module *module,
1310                                          const FileSpec &file_spec,
1311                                          lldb::DescriptionLevel desc_level) {
1312   uint32_t num_matches = 0;
1313   if (module) {
1314     SymbolContextList sc_list;
1315     num_matches = module->ResolveSymbolContextsForFileSpec(
1316         file_spec, 0, false, eSymbolContextCompUnit, sc_list);
1317 
1318     for (uint32_t i = 0; i < num_matches; ++i) {
1319       SymbolContext sc;
1320       if (sc_list.GetContextAtIndex(i, sc)) {
1321         if (i > 0)
1322           strm << "\n\n";
1323 
1324         strm << "Line table for " << sc.comp_unit->GetPrimaryFile() << " in `"
1325              << module->GetFileSpec().GetFilename() << "\n";
1326         LineTable *line_table = sc.comp_unit->GetLineTable();
1327         if (line_table)
1328           line_table->GetDescription(
1329               &strm, interpreter.GetExecutionContext().GetTargetPtr(),
1330               desc_level);
1331         else
1332           strm << "No line table";
1333       }
1334     }
1335   }
1336   return num_matches;
1337 }
1338 
1339 static void DumpFullpath(Stream &strm, const FileSpec *file_spec_ptr,
1340                          uint32_t width) {
1341   if (file_spec_ptr) {
1342     if (width > 0) {
1343       std::string fullpath = file_spec_ptr->GetPath();
1344       strm.Printf("%-*s", width, fullpath.c_str());
1345       return;
1346     } else {
1347       file_spec_ptr->Dump(strm.AsRawOstream());
1348       return;
1349     }
1350   }
1351   // Keep the width spacing correct if things go wrong...
1352   if (width > 0)
1353     strm.Printf("%-*s", width, "");
1354 }
1355 
1356 static void DumpDirectory(Stream &strm, const FileSpec *file_spec_ptr,
1357                           uint32_t width) {
1358   if (file_spec_ptr) {
1359     if (width > 0)
1360       strm.Printf("%-*s", width, file_spec_ptr->GetDirectory().AsCString(""));
1361     else
1362       file_spec_ptr->GetDirectory().Dump(&strm);
1363     return;
1364   }
1365   // Keep the width spacing correct if things go wrong...
1366   if (width > 0)
1367     strm.Printf("%-*s", width, "");
1368 }
1369 
1370 static void DumpBasename(Stream &strm, const FileSpec *file_spec_ptr,
1371                          uint32_t width) {
1372   if (file_spec_ptr) {
1373     if (width > 0)
1374       strm.Printf("%-*s", width, file_spec_ptr->GetFilename().AsCString(""));
1375     else
1376       file_spec_ptr->GetFilename().Dump(&strm);
1377     return;
1378   }
1379   // Keep the width spacing correct if things go wrong...
1380   if (width > 0)
1381     strm.Printf("%-*s", width, "");
1382 }
1383 
1384 static size_t DumpModuleObjfileHeaders(Stream &strm, ModuleList &module_list) {
1385   size_t num_dumped = 0;
1386   std::lock_guard<std::recursive_mutex> guard(module_list.GetMutex());
1387   const size_t num_modules = module_list.GetSize();
1388   if (num_modules > 0) {
1389     strm.Printf("Dumping headers for %" PRIu64 " module(s).\n",
1390                 static_cast<uint64_t>(num_modules));
1391     strm.IndentMore();
1392     for (size_t image_idx = 0; image_idx < num_modules; ++image_idx) {
1393       Module *module = module_list.GetModulePointerAtIndexUnlocked(image_idx);
1394       if (module) {
1395         if (num_dumped++ > 0) {
1396           strm.EOL();
1397           strm.EOL();
1398         }
1399         ObjectFile *objfile = module->GetObjectFile();
1400         if (objfile)
1401           objfile->Dump(&strm);
1402         else {
1403           strm.Format("No object file for module: {0:F}\n",
1404                       module->GetFileSpec());
1405         }
1406       }
1407     }
1408     strm.IndentLess();
1409   }
1410   return num_dumped;
1411 }
1412 
1413 static void DumpModuleSymtab(CommandInterpreter &interpreter, Stream &strm,
1414                              Module *module, SortOrder sort_order,
1415                              Mangled::NamePreference name_preference) {
1416   if (!module)
1417     return;
1418   if (Symtab *symtab = module->GetSymtab())
1419     symtab->Dump(&strm, interpreter.GetExecutionContext().GetTargetPtr(),
1420                  sort_order, name_preference);
1421 }
1422 
1423 static void DumpModuleSections(CommandInterpreter &interpreter, Stream &strm,
1424                                Module *module) {
1425   if (module) {
1426     SectionList *section_list = module->GetSectionList();
1427     if (section_list) {
1428       strm.Printf("Sections for '%s' (%s):\n",
1429                   module->GetSpecificationDescription().c_str(),
1430                   module->GetArchitecture().GetArchitectureName());
1431       section_list->Dump(strm.AsRawOstream(), strm.GetIndentLevel() + 2,
1432                          interpreter.GetExecutionContext().GetTargetPtr(), true,
1433                          UINT32_MAX);
1434     }
1435   }
1436 }
1437 
1438 static bool DumpModuleSymbolFile(Stream &strm, Module *module) {
1439   if (module) {
1440     if (SymbolFile *symbol_file = module->GetSymbolFile(true)) {
1441       symbol_file->Dump(strm);
1442       return true;
1443     }
1444   }
1445   return false;
1446 }
1447 
1448 static void DumpAddress(ExecutionContextScope *exe_scope,
1449                         const Address &so_addr, bool verbose, Stream &strm) {
1450   strm.IndentMore();
1451   strm.Indent("    Address: ");
1452   so_addr.Dump(&strm, exe_scope, Address::DumpStyleModuleWithFileAddress);
1453   strm.PutCString(" (");
1454   so_addr.Dump(&strm, exe_scope, Address::DumpStyleSectionNameOffset);
1455   strm.PutCString(")\n");
1456   strm.Indent("    Summary: ");
1457   const uint32_t save_indent = strm.GetIndentLevel();
1458   strm.SetIndentLevel(save_indent + 13);
1459   so_addr.Dump(&strm, exe_scope, Address::DumpStyleResolvedDescription);
1460   strm.SetIndentLevel(save_indent);
1461   // Print out detailed address information when verbose is enabled
1462   if (verbose) {
1463     strm.EOL();
1464     so_addr.Dump(&strm, exe_scope, Address::DumpStyleDetailedSymbolContext);
1465   }
1466   strm.IndentLess();
1467 }
1468 
1469 static bool LookupAddressInModule(CommandInterpreter &interpreter, Stream &strm,
1470                                   Module *module, uint32_t resolve_mask,
1471                                   lldb::addr_t raw_addr, lldb::addr_t offset,
1472                                   bool verbose) {
1473   if (module) {
1474     lldb::addr_t addr = raw_addr - offset;
1475     Address so_addr;
1476     SymbolContext sc;
1477     Target *target = interpreter.GetExecutionContext().GetTargetPtr();
1478     if (target && !target->GetSectionLoadList().IsEmpty()) {
1479       if (!target->GetSectionLoadList().ResolveLoadAddress(addr, so_addr))
1480         return false;
1481       else if (so_addr.GetModule().get() != module)
1482         return false;
1483     } else {
1484       if (!module->ResolveFileAddress(addr, so_addr))
1485         return false;
1486     }
1487 
1488     ExecutionContextScope *exe_scope =
1489         interpreter.GetExecutionContext().GetBestExecutionContextScope();
1490     DumpAddress(exe_scope, so_addr, verbose, strm);
1491     //        strm.IndentMore();
1492     //        strm.Indent ("    Address: ");
1493     //        so_addr.Dump (&strm, exe_scope,
1494     //        Address::DumpStyleModuleWithFileAddress);
1495     //        strm.PutCString (" (");
1496     //        so_addr.Dump (&strm, exe_scope,
1497     //        Address::DumpStyleSectionNameOffset);
1498     //        strm.PutCString (")\n");
1499     //        strm.Indent ("    Summary: ");
1500     //        const uint32_t save_indent = strm.GetIndentLevel ();
1501     //        strm.SetIndentLevel (save_indent + 13);
1502     //        so_addr.Dump (&strm, exe_scope,
1503     //        Address::DumpStyleResolvedDescription);
1504     //        strm.SetIndentLevel (save_indent);
1505     //        // Print out detailed address information when verbose is enabled
1506     //        if (verbose)
1507     //        {
1508     //            strm.EOL();
1509     //            so_addr.Dump (&strm, exe_scope,
1510     //            Address::DumpStyleDetailedSymbolContext);
1511     //        }
1512     //        strm.IndentLess();
1513     return true;
1514   }
1515 
1516   return false;
1517 }
1518 
1519 static uint32_t LookupSymbolInModule(CommandInterpreter &interpreter,
1520                                      Stream &strm, Module *module,
1521                                      const char *name, bool name_is_regex,
1522                                      bool verbose) {
1523   if (!module)
1524     return 0;
1525 
1526   Symtab *symtab = module->GetSymtab();
1527   if (!symtab)
1528     return 0;
1529 
1530   SymbolContext sc;
1531   std::vector<uint32_t> match_indexes;
1532   ConstString symbol_name(name);
1533   uint32_t num_matches = 0;
1534   if (name_is_regex) {
1535     RegularExpression name_regexp(symbol_name.GetStringRef());
1536     num_matches = symtab->AppendSymbolIndexesMatchingRegExAndType(
1537         name_regexp, eSymbolTypeAny, match_indexes);
1538   } else {
1539     num_matches =
1540         symtab->AppendSymbolIndexesWithName(symbol_name, match_indexes);
1541   }
1542 
1543   if (num_matches > 0) {
1544     strm.Indent();
1545     strm.Printf("%u symbols match %s'%s' in ", num_matches,
1546                 name_is_regex ? "the regular expression " : "", name);
1547     DumpFullpath(strm, &module->GetFileSpec(), 0);
1548     strm.PutCString(":\n");
1549     strm.IndentMore();
1550     for (uint32_t i = 0; i < num_matches; ++i) {
1551       Symbol *symbol = symtab->SymbolAtIndex(match_indexes[i]);
1552       if (symbol && symbol->ValueIsAddress()) {
1553         DumpAddress(
1554             interpreter.GetExecutionContext().GetBestExecutionContextScope(),
1555             symbol->GetAddressRef(), verbose, strm);
1556       }
1557     }
1558     strm.IndentLess();
1559   }
1560   return num_matches;
1561 }
1562 
1563 static void DumpSymbolContextList(ExecutionContextScope *exe_scope,
1564                                   Stream &strm, SymbolContextList &sc_list,
1565                                   bool verbose) {
1566   strm.IndentMore();
1567 
1568   const uint32_t num_matches = sc_list.GetSize();
1569 
1570   for (uint32_t i = 0; i < num_matches; ++i) {
1571     SymbolContext sc;
1572     if (sc_list.GetContextAtIndex(i, sc)) {
1573       AddressRange range;
1574 
1575       sc.GetAddressRange(eSymbolContextEverything, 0, true, range);
1576 
1577       DumpAddress(exe_scope, range.GetBaseAddress(), verbose, strm);
1578     }
1579   }
1580   strm.IndentLess();
1581 }
1582 
1583 static size_t LookupFunctionInModule(CommandInterpreter &interpreter,
1584                                      Stream &strm, Module *module,
1585                                      const char *name, bool name_is_regex,
1586                                      bool include_inlines, bool include_symbols,
1587                                      bool verbose) {
1588   if (module && name && name[0]) {
1589     SymbolContextList sc_list;
1590     size_t num_matches = 0;
1591     if (name_is_regex) {
1592       RegularExpression function_name_regex((llvm::StringRef(name)));
1593       module->FindFunctions(function_name_regex, include_symbols,
1594                             include_inlines, sc_list);
1595     } else {
1596       ConstString function_name(name);
1597       module->FindFunctions(function_name, CompilerDeclContext(),
1598                             eFunctionNameTypeAuto, include_symbols,
1599                             include_inlines, sc_list);
1600     }
1601     num_matches = sc_list.GetSize();
1602     if (num_matches) {
1603       strm.Indent();
1604       strm.Printf("%" PRIu64 " match%s found in ", (uint64_t)num_matches,
1605                   num_matches > 1 ? "es" : "");
1606       DumpFullpath(strm, &module->GetFileSpec(), 0);
1607       strm.PutCString(":\n");
1608       DumpSymbolContextList(
1609           interpreter.GetExecutionContext().GetBestExecutionContextScope(),
1610           strm, sc_list, verbose);
1611     }
1612     return num_matches;
1613   }
1614   return 0;
1615 }
1616 
1617 static size_t LookupTypeInModule(Target *target,
1618                                  CommandInterpreter &interpreter, Stream &strm,
1619                                  Module *module, const char *name_cstr,
1620                                  bool name_is_regex) {
1621   TypeList type_list;
1622   if (module && name_cstr && name_cstr[0]) {
1623     const uint32_t max_num_matches = UINT32_MAX;
1624     size_t num_matches = 0;
1625     bool name_is_fully_qualified = false;
1626 
1627     ConstString name(name_cstr);
1628     llvm::DenseSet<lldb_private::SymbolFile *> searched_symbol_files;
1629     module->FindTypes(name, name_is_fully_qualified, max_num_matches,
1630                       searched_symbol_files, type_list);
1631 
1632     if (type_list.Empty())
1633       return 0;
1634 
1635     strm.Indent();
1636     strm.Printf("%" PRIu64 " match%s found in ", (uint64_t)num_matches,
1637                 num_matches > 1 ? "es" : "");
1638     DumpFullpath(strm, &module->GetFileSpec(), 0);
1639     strm.PutCString(":\n");
1640     for (TypeSP type_sp : type_list.Types()) {
1641       if (!type_sp)
1642         continue;
1643       // Resolve the clang type so that any forward references to types
1644       // that haven't yet been parsed will get parsed.
1645       type_sp->GetFullCompilerType();
1646       type_sp->GetDescription(&strm, eDescriptionLevelFull, true, target);
1647       // Print all typedef chains
1648       TypeSP typedef_type_sp(type_sp);
1649       TypeSP typedefed_type_sp(typedef_type_sp->GetTypedefType());
1650       while (typedefed_type_sp) {
1651         strm.EOL();
1652         strm.Printf("     typedef '%s': ",
1653                     typedef_type_sp->GetName().GetCString());
1654         typedefed_type_sp->GetFullCompilerType();
1655         typedefed_type_sp->GetDescription(&strm, eDescriptionLevelFull, true,
1656                                           target);
1657         typedef_type_sp = typedefed_type_sp;
1658         typedefed_type_sp = typedef_type_sp->GetTypedefType();
1659       }
1660     }
1661     strm.EOL();
1662   }
1663   return type_list.GetSize();
1664 }
1665 
1666 static size_t LookupTypeHere(Target *target, CommandInterpreter &interpreter,
1667                              Stream &strm, Module &module,
1668                              const char *name_cstr, bool name_is_regex) {
1669   TypeList type_list;
1670   const uint32_t max_num_matches = UINT32_MAX;
1671   bool name_is_fully_qualified = false;
1672 
1673   ConstString name(name_cstr);
1674   llvm::DenseSet<SymbolFile *> searched_symbol_files;
1675   module.FindTypes(name, name_is_fully_qualified, max_num_matches,
1676                    searched_symbol_files, type_list);
1677 
1678   if (type_list.Empty())
1679     return 0;
1680 
1681   strm.Indent();
1682   strm.PutCString("Best match found in ");
1683   DumpFullpath(strm, &module.GetFileSpec(), 0);
1684   strm.PutCString(":\n");
1685 
1686   TypeSP type_sp(type_list.GetTypeAtIndex(0));
1687   if (type_sp) {
1688     // Resolve the clang type so that any forward references to types that
1689     // haven't yet been parsed will get parsed.
1690     type_sp->GetFullCompilerType();
1691     type_sp->GetDescription(&strm, eDescriptionLevelFull, true, target);
1692     // Print all typedef chains.
1693     TypeSP typedef_type_sp(type_sp);
1694     TypeSP typedefed_type_sp(typedef_type_sp->GetTypedefType());
1695     while (typedefed_type_sp) {
1696       strm.EOL();
1697       strm.Printf("     typedef '%s': ",
1698                   typedef_type_sp->GetName().GetCString());
1699       typedefed_type_sp->GetFullCompilerType();
1700       typedefed_type_sp->GetDescription(&strm, eDescriptionLevelFull, true,
1701                                         target);
1702       typedef_type_sp = typedefed_type_sp;
1703       typedefed_type_sp = typedef_type_sp->GetTypedefType();
1704     }
1705   }
1706   strm.EOL();
1707   return type_list.GetSize();
1708 }
1709 
1710 static uint32_t LookupFileAndLineInModule(CommandInterpreter &interpreter,
1711                                           Stream &strm, Module *module,
1712                                           const FileSpec &file_spec,
1713                                           uint32_t line, bool check_inlines,
1714                                           bool verbose) {
1715   if (module && file_spec) {
1716     SymbolContextList sc_list;
1717     const uint32_t num_matches = module->ResolveSymbolContextsForFileSpec(
1718         file_spec, line, check_inlines, eSymbolContextEverything, sc_list);
1719     if (num_matches > 0) {
1720       strm.Indent();
1721       strm.Printf("%u match%s found in ", num_matches,
1722                   num_matches > 1 ? "es" : "");
1723       strm << file_spec;
1724       if (line > 0)
1725         strm.Printf(":%u", line);
1726       strm << " in ";
1727       DumpFullpath(strm, &module->GetFileSpec(), 0);
1728       strm.PutCString(":\n");
1729       DumpSymbolContextList(
1730           interpreter.GetExecutionContext().GetBestExecutionContextScope(),
1731           strm, sc_list, verbose);
1732       return num_matches;
1733     }
1734   }
1735   return 0;
1736 }
1737 
1738 static size_t FindModulesByName(Target *target, const char *module_name,
1739                                 ModuleList &module_list,
1740                                 bool check_global_list) {
1741   FileSpec module_file_spec(module_name);
1742   ModuleSpec module_spec(module_file_spec);
1743 
1744   const size_t initial_size = module_list.GetSize();
1745 
1746   if (check_global_list) {
1747     // Check the global list
1748     std::lock_guard<std::recursive_mutex> guard(
1749         Module::GetAllocationModuleCollectionMutex());
1750     const size_t num_modules = Module::GetNumberAllocatedModules();
1751     ModuleSP module_sp;
1752     for (size_t image_idx = 0; image_idx < num_modules; ++image_idx) {
1753       Module *module = Module::GetAllocatedModuleAtIndex(image_idx);
1754 
1755       if (module) {
1756         if (module->MatchesModuleSpec(module_spec)) {
1757           module_sp = module->shared_from_this();
1758           module_list.AppendIfNeeded(module_sp);
1759         }
1760       }
1761     }
1762   } else {
1763     if (target) {
1764       target->GetImages().FindModules(module_spec, module_list);
1765       const size_t num_matches = module_list.GetSize();
1766 
1767       // Not found in our module list for our target, check the main shared
1768       // module list in case it is a extra file used somewhere else
1769       if (num_matches == 0) {
1770         module_spec.GetArchitecture() = target->GetArchitecture();
1771         ModuleList::FindSharedModules(module_spec, module_list);
1772       }
1773     } else {
1774       ModuleList::FindSharedModules(module_spec, module_list);
1775     }
1776   }
1777 
1778   return module_list.GetSize() - initial_size;
1779 }
1780 
1781 #pragma mark CommandObjectTargetModulesModuleAutoComplete
1782 
1783 // A base command object class that can auto complete with module file
1784 // paths
1785 
1786 class CommandObjectTargetModulesModuleAutoComplete
1787     : public CommandObjectParsed {
1788 public:
1789   CommandObjectTargetModulesModuleAutoComplete(CommandInterpreter &interpreter,
1790                                                const char *name,
1791                                                const char *help,
1792                                                const char *syntax,
1793                                                uint32_t flags = 0)
1794       : CommandObjectParsed(interpreter, name, help, syntax, flags) {
1795     CommandArgumentEntry arg;
1796     CommandArgumentData file_arg;
1797 
1798     // Define the first (and only) variant of this arg.
1799     file_arg.arg_type = eArgTypeFilename;
1800     file_arg.arg_repetition = eArgRepeatStar;
1801 
1802     // There is only one variant this argument could be; put it into the
1803     // argument entry.
1804     arg.push_back(file_arg);
1805 
1806     // Push the data for the first argument into the m_arguments vector.
1807     m_arguments.push_back(arg);
1808   }
1809 
1810   ~CommandObjectTargetModulesModuleAutoComplete() override = default;
1811 
1812   void
1813   HandleArgumentCompletion(CompletionRequest &request,
1814                            OptionElementVector &opt_element_vector) override {
1815     CommandCompletions::InvokeCommonCompletionCallbacks(
1816         GetCommandInterpreter(), CommandCompletions::eModuleCompletion, request,
1817         nullptr);
1818   }
1819 };
1820 
1821 #pragma mark CommandObjectTargetModulesSourceFileAutoComplete
1822 
1823 // A base command object class that can auto complete with module source
1824 // file paths
1825 
1826 class CommandObjectTargetModulesSourceFileAutoComplete
1827     : public CommandObjectParsed {
1828 public:
1829   CommandObjectTargetModulesSourceFileAutoComplete(
1830       CommandInterpreter &interpreter, const char *name, const char *help,
1831       const char *syntax, uint32_t flags)
1832       : CommandObjectParsed(interpreter, name, help, syntax, flags) {
1833     CommandArgumentEntry arg;
1834     CommandArgumentData source_file_arg;
1835 
1836     // Define the first (and only) variant of this arg.
1837     source_file_arg.arg_type = eArgTypeSourceFile;
1838     source_file_arg.arg_repetition = eArgRepeatPlus;
1839 
1840     // There is only one variant this argument could be; put it into the
1841     // argument entry.
1842     arg.push_back(source_file_arg);
1843 
1844     // Push the data for the first argument into the m_arguments vector.
1845     m_arguments.push_back(arg);
1846   }
1847 
1848   ~CommandObjectTargetModulesSourceFileAutoComplete() override = default;
1849 
1850   void
1851   HandleArgumentCompletion(CompletionRequest &request,
1852                            OptionElementVector &opt_element_vector) override {
1853     CommandCompletions::InvokeCommonCompletionCallbacks(
1854         GetCommandInterpreter(), CommandCompletions::eSourceFileCompletion,
1855         request, nullptr);
1856   }
1857 };
1858 
1859 #pragma mark CommandObjectTargetModulesDumpObjfile
1860 
1861 class CommandObjectTargetModulesDumpObjfile
1862     : public CommandObjectTargetModulesModuleAutoComplete {
1863 public:
1864   CommandObjectTargetModulesDumpObjfile(CommandInterpreter &interpreter)
1865       : CommandObjectTargetModulesModuleAutoComplete(
1866             interpreter, "target modules dump objfile",
1867             "Dump the object file headers from one or more target modules.",
1868             nullptr, eCommandRequiresTarget) {}
1869 
1870   ~CommandObjectTargetModulesDumpObjfile() override = default;
1871 
1872 protected:
1873   bool DoExecute(Args &command, CommandReturnObject &result) override {
1874     Target *target = &GetSelectedTarget();
1875 
1876     uint32_t addr_byte_size = target->GetArchitecture().GetAddressByteSize();
1877     result.GetOutputStream().SetAddressByteSize(addr_byte_size);
1878     result.GetErrorStream().SetAddressByteSize(addr_byte_size);
1879 
1880     size_t num_dumped = 0;
1881     if (command.GetArgumentCount() == 0) {
1882       // Dump all headers for all modules images
1883       num_dumped = DumpModuleObjfileHeaders(result.GetOutputStream(),
1884                                             target->GetImages());
1885       if (num_dumped == 0) {
1886         result.AppendError("the target has no associated executable images");
1887         result.SetStatus(eReturnStatusFailed);
1888       }
1889     } else {
1890       // Find the modules that match the basename or full path.
1891       ModuleList module_list;
1892       const char *arg_cstr;
1893       for (int arg_idx = 0;
1894            (arg_cstr = command.GetArgumentAtIndex(arg_idx)) != nullptr;
1895            ++arg_idx) {
1896         size_t num_matched =
1897             FindModulesByName(target, arg_cstr, module_list, true);
1898         if (num_matched == 0) {
1899           result.AppendWarningWithFormat(
1900               "Unable to find an image that matches '%s'.\n", arg_cstr);
1901         }
1902       }
1903       // Dump all the modules we found.
1904       num_dumped =
1905           DumpModuleObjfileHeaders(result.GetOutputStream(), module_list);
1906     }
1907 
1908     if (num_dumped > 0) {
1909       result.SetStatus(eReturnStatusSuccessFinishResult);
1910     } else {
1911       result.AppendError("no matching executable images found");
1912       result.SetStatus(eReturnStatusFailed);
1913     }
1914     return result.Succeeded();
1915   }
1916 };
1917 
1918 #pragma mark CommandObjectTargetModulesDumpSymtab
1919 
1920 static constexpr OptionEnumValueElement g_sort_option_enumeration[] = {
1921     {
1922         eSortOrderNone,
1923         "none",
1924         "No sorting, use the original symbol table order.",
1925     },
1926     {
1927         eSortOrderByAddress,
1928         "address",
1929         "Sort output by symbol address.",
1930     },
1931     {
1932         eSortOrderByName,
1933         "name",
1934         "Sort output by symbol name.",
1935     },
1936 };
1937 
1938 #define LLDB_OPTIONS_target_modules_dump_symtab
1939 #include "CommandOptions.inc"
1940 
1941 class CommandObjectTargetModulesDumpSymtab
1942     : public CommandObjectTargetModulesModuleAutoComplete {
1943 public:
1944   CommandObjectTargetModulesDumpSymtab(CommandInterpreter &interpreter)
1945       : CommandObjectTargetModulesModuleAutoComplete(
1946             interpreter, "target modules dump symtab",
1947             "Dump the symbol table from one or more target modules.", nullptr,
1948             eCommandRequiresTarget),
1949         m_options() {}
1950 
1951   ~CommandObjectTargetModulesDumpSymtab() override = default;
1952 
1953   Options *GetOptions() override { return &m_options; }
1954 
1955   class CommandOptions : public Options {
1956   public:
1957     CommandOptions() : Options(), m_sort_order(eSortOrderNone) {}
1958 
1959     ~CommandOptions() override = default;
1960 
1961     Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
1962                           ExecutionContext *execution_context) override {
1963       Status error;
1964       const int short_option = m_getopt_table[option_idx].val;
1965 
1966       switch (short_option) {
1967       case 'm':
1968         m_prefer_mangled.SetCurrentValue(true);
1969         m_prefer_mangled.SetOptionWasSet();
1970         break;
1971 
1972       case 's':
1973         m_sort_order = (SortOrder)OptionArgParser::ToOptionEnum(
1974             option_arg, GetDefinitions()[option_idx].enum_values,
1975             eSortOrderNone, error);
1976         break;
1977 
1978       default:
1979         llvm_unreachable("Unimplemented option");
1980       }
1981       return error;
1982     }
1983 
1984     void OptionParsingStarting(ExecutionContext *execution_context) override {
1985       m_sort_order = eSortOrderNone;
1986       m_prefer_mangled.Clear();
1987     }
1988 
1989     llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
1990       return llvm::makeArrayRef(g_target_modules_dump_symtab_options);
1991     }
1992 
1993     SortOrder m_sort_order;
1994     OptionValueBoolean m_prefer_mangled = {false, false};
1995   };
1996 
1997 protected:
1998   bool DoExecute(Args &command, CommandReturnObject &result) override {
1999     Target *target = &GetSelectedTarget();
2000     uint32_t num_dumped = 0;
2001     Mangled::NamePreference name_preference =
2002         (m_options.m_prefer_mangled ? Mangled::ePreferMangled
2003                                     : Mangled::ePreferDemangled);
2004 
2005     uint32_t addr_byte_size = target->GetArchitecture().GetAddressByteSize();
2006     result.GetOutputStream().SetAddressByteSize(addr_byte_size);
2007     result.GetErrorStream().SetAddressByteSize(addr_byte_size);
2008 
2009     if (command.GetArgumentCount() == 0) {
2010       // Dump all sections for all modules images
2011       std::lock_guard<std::recursive_mutex> guard(
2012           target->GetImages().GetMutex());
2013       const size_t num_modules = target->GetImages().GetSize();
2014       if (num_modules > 0) {
2015         result.GetOutputStream().Printf("Dumping symbol table for %" PRIu64
2016                                         " modules.\n",
2017                                         (uint64_t)num_modules);
2018         for (size_t image_idx = 0; image_idx < num_modules; ++image_idx) {
2019           if (num_dumped > 0) {
2020             result.GetOutputStream().EOL();
2021             result.GetOutputStream().EOL();
2022           }
2023           if (m_interpreter.WasInterrupted())
2024             break;
2025           num_dumped++;
2026           DumpModuleSymtab(
2027               m_interpreter, result.GetOutputStream(),
2028               target->GetImages().GetModulePointerAtIndexUnlocked(image_idx),
2029               m_options.m_sort_order, name_preference);
2030         }
2031       } else {
2032         result.AppendError("the target has no associated executable images");
2033         result.SetStatus(eReturnStatusFailed);
2034         return false;
2035       }
2036     } else {
2037       // Dump specified images (by basename or fullpath)
2038       const char *arg_cstr;
2039       for (int arg_idx = 0;
2040            (arg_cstr = command.GetArgumentAtIndex(arg_idx)) != nullptr;
2041            ++arg_idx) {
2042         ModuleList module_list;
2043         const size_t num_matches =
2044             FindModulesByName(target, arg_cstr, module_list, true);
2045         if (num_matches > 0) {
2046           for (size_t i = 0; i < num_matches; ++i) {
2047             Module *module = module_list.GetModulePointerAtIndex(i);
2048             if (module) {
2049               if (num_dumped > 0) {
2050                 result.GetOutputStream().EOL();
2051                 result.GetOutputStream().EOL();
2052               }
2053               if (m_interpreter.WasInterrupted())
2054                 break;
2055               num_dumped++;
2056               DumpModuleSymtab(m_interpreter, result.GetOutputStream(), module,
2057                                m_options.m_sort_order, name_preference);
2058             }
2059           }
2060         } else
2061           result.AppendWarningWithFormat(
2062               "Unable to find an image that matches '%s'.\n", arg_cstr);
2063       }
2064     }
2065 
2066     if (num_dumped > 0)
2067       result.SetStatus(eReturnStatusSuccessFinishResult);
2068     else {
2069       result.AppendError("no matching executable images found");
2070       result.SetStatus(eReturnStatusFailed);
2071     }
2072     return result.Succeeded();
2073   }
2074 
2075   CommandOptions m_options;
2076 };
2077 
2078 #pragma mark CommandObjectTargetModulesDumpSections
2079 
2080 // Image section dumping command
2081 
2082 class CommandObjectTargetModulesDumpSections
2083     : public CommandObjectTargetModulesModuleAutoComplete {
2084 public:
2085   CommandObjectTargetModulesDumpSections(CommandInterpreter &interpreter)
2086       : CommandObjectTargetModulesModuleAutoComplete(
2087             interpreter, "target modules dump sections",
2088             "Dump the sections from one or more target modules.",
2089             //"target modules dump sections [<file1> ...]")
2090             nullptr, eCommandRequiresTarget) {}
2091 
2092   ~CommandObjectTargetModulesDumpSections() override = default;
2093 
2094 protected:
2095   bool DoExecute(Args &command, CommandReturnObject &result) override {
2096     Target *target = &GetSelectedTarget();
2097     uint32_t num_dumped = 0;
2098 
2099     uint32_t addr_byte_size = target->GetArchitecture().GetAddressByteSize();
2100     result.GetOutputStream().SetAddressByteSize(addr_byte_size);
2101     result.GetErrorStream().SetAddressByteSize(addr_byte_size);
2102 
2103     if (command.GetArgumentCount() == 0) {
2104       // Dump all sections for all modules images
2105       const size_t num_modules = target->GetImages().GetSize();
2106       if (num_modules > 0) {
2107         result.GetOutputStream().Printf("Dumping sections for %" PRIu64
2108                                         " modules.\n",
2109                                         (uint64_t)num_modules);
2110         for (size_t image_idx = 0; image_idx < num_modules; ++image_idx) {
2111           if (m_interpreter.WasInterrupted())
2112             break;
2113           num_dumped++;
2114           DumpModuleSections(
2115               m_interpreter, result.GetOutputStream(),
2116               target->GetImages().GetModulePointerAtIndex(image_idx));
2117         }
2118       } else {
2119         result.AppendError("the target has no associated executable images");
2120         result.SetStatus(eReturnStatusFailed);
2121         return false;
2122       }
2123     } else {
2124       // Dump specified images (by basename or fullpath)
2125       const char *arg_cstr;
2126       for (int arg_idx = 0;
2127            (arg_cstr = command.GetArgumentAtIndex(arg_idx)) != nullptr;
2128            ++arg_idx) {
2129         ModuleList module_list;
2130         const size_t num_matches =
2131             FindModulesByName(target, arg_cstr, module_list, true);
2132         if (num_matches > 0) {
2133           for (size_t i = 0; i < num_matches; ++i) {
2134             if (m_interpreter.WasInterrupted())
2135               break;
2136             Module *module = module_list.GetModulePointerAtIndex(i);
2137             if (module) {
2138               num_dumped++;
2139               DumpModuleSections(m_interpreter, result.GetOutputStream(),
2140                                  module);
2141             }
2142           }
2143         } else {
2144           // Check the global list
2145           std::lock_guard<std::recursive_mutex> guard(
2146               Module::GetAllocationModuleCollectionMutex());
2147 
2148           result.AppendWarningWithFormat(
2149               "Unable to find an image that matches '%s'.\n", arg_cstr);
2150         }
2151       }
2152     }
2153 
2154     if (num_dumped > 0)
2155       result.SetStatus(eReturnStatusSuccessFinishResult);
2156     else {
2157       result.AppendError("no matching executable images found");
2158       result.SetStatus(eReturnStatusFailed);
2159     }
2160     return result.Succeeded();
2161   }
2162 };
2163 
2164 #pragma mark CommandObjectTargetModulesDumpClangAST
2165 
2166 // Clang AST dumping command
2167 
2168 class CommandObjectTargetModulesDumpClangAST
2169     : public CommandObjectTargetModulesModuleAutoComplete {
2170 public:
2171   CommandObjectTargetModulesDumpClangAST(CommandInterpreter &interpreter)
2172       : CommandObjectTargetModulesModuleAutoComplete(
2173             interpreter, "target modules dump ast",
2174             "Dump the clang ast for a given module's symbol file.",
2175             //"target modules dump ast [<file1> ...]")
2176             nullptr, eCommandRequiresTarget) {}
2177 
2178   ~CommandObjectTargetModulesDumpClangAST() override = default;
2179 
2180 protected:
2181   bool DoExecute(Args &command, CommandReturnObject &result) override {
2182     Target *target = &GetSelectedTarget();
2183 
2184     const size_t num_modules = target->GetImages().GetSize();
2185     if (num_modules == 0) {
2186       result.AppendError("the target has no associated executable images");
2187       result.SetStatus(eReturnStatusFailed);
2188       return false;
2189     }
2190 
2191     if (command.GetArgumentCount() == 0) {
2192       // Dump all ASTs for all modules images
2193       result.GetOutputStream().Printf("Dumping clang ast for %" PRIu64
2194                                       " modules.\n",
2195                                       (uint64_t)num_modules);
2196       for (size_t image_idx = 0; image_idx < num_modules; ++image_idx) {
2197         if (m_interpreter.WasInterrupted())
2198           break;
2199         Module *m = target->GetImages().GetModulePointerAtIndex(image_idx);
2200         if (SymbolFile *sf = m->GetSymbolFile())
2201           sf->DumpClangAST(result.GetOutputStream());
2202       }
2203       result.SetStatus(eReturnStatusSuccessFinishResult);
2204       return true;
2205     }
2206 
2207     // Dump specified ASTs (by basename or fullpath)
2208     for (const Args::ArgEntry &arg : command.entries()) {
2209       ModuleList module_list;
2210       const size_t num_matches =
2211           FindModulesByName(target, arg.c_str(), module_list, true);
2212       if (num_matches == 0) {
2213         // Check the global list
2214         std::lock_guard<std::recursive_mutex> guard(
2215             Module::GetAllocationModuleCollectionMutex());
2216 
2217         result.AppendWarningWithFormat(
2218             "Unable to find an image that matches '%s'.\n", arg.c_str());
2219         continue;
2220       }
2221 
2222       for (size_t i = 0; i < num_matches; ++i) {
2223         if (m_interpreter.WasInterrupted())
2224           break;
2225         Module *m = module_list.GetModulePointerAtIndex(i);
2226         if (SymbolFile *sf = m->GetSymbolFile())
2227           sf->DumpClangAST(result.GetOutputStream());
2228       }
2229     }
2230     result.SetStatus(eReturnStatusSuccessFinishResult);
2231     return true;
2232   }
2233 };
2234 
2235 #pragma mark CommandObjectTargetModulesDumpSymfile
2236 
2237 // Image debug symbol dumping command
2238 
2239 class CommandObjectTargetModulesDumpSymfile
2240     : public CommandObjectTargetModulesModuleAutoComplete {
2241 public:
2242   CommandObjectTargetModulesDumpSymfile(CommandInterpreter &interpreter)
2243       : CommandObjectTargetModulesModuleAutoComplete(
2244             interpreter, "target modules dump symfile",
2245             "Dump the debug symbol file for one or more target modules.",
2246             //"target modules dump symfile [<file1> ...]")
2247             nullptr, eCommandRequiresTarget) {}
2248 
2249   ~CommandObjectTargetModulesDumpSymfile() override = default;
2250 
2251 protected:
2252   bool DoExecute(Args &command, CommandReturnObject &result) override {
2253     Target *target = &GetSelectedTarget();
2254     uint32_t num_dumped = 0;
2255 
2256     uint32_t addr_byte_size = target->GetArchitecture().GetAddressByteSize();
2257     result.GetOutputStream().SetAddressByteSize(addr_byte_size);
2258     result.GetErrorStream().SetAddressByteSize(addr_byte_size);
2259 
2260     if (command.GetArgumentCount() == 0) {
2261       // Dump all sections for all modules images
2262       const ModuleList &target_modules = target->GetImages();
2263       std::lock_guard<std::recursive_mutex> guard(target_modules.GetMutex());
2264       const size_t num_modules = target_modules.GetSize();
2265       if (num_modules > 0) {
2266         result.GetOutputStream().Printf("Dumping debug symbols for %" PRIu64
2267                                         " modules.\n",
2268                                         (uint64_t)num_modules);
2269         for (uint32_t image_idx = 0; image_idx < num_modules; ++image_idx) {
2270           if (m_interpreter.WasInterrupted())
2271             break;
2272           if (DumpModuleSymbolFile(
2273                   result.GetOutputStream(),
2274                   target_modules.GetModulePointerAtIndexUnlocked(image_idx)))
2275             num_dumped++;
2276         }
2277       } else {
2278         result.AppendError("the target has no associated executable images");
2279         result.SetStatus(eReturnStatusFailed);
2280         return false;
2281       }
2282     } else {
2283       // Dump specified images (by basename or fullpath)
2284       const char *arg_cstr;
2285       for (int arg_idx = 0;
2286            (arg_cstr = command.GetArgumentAtIndex(arg_idx)) != nullptr;
2287            ++arg_idx) {
2288         ModuleList module_list;
2289         const size_t num_matches =
2290             FindModulesByName(target, arg_cstr, module_list, true);
2291         if (num_matches > 0) {
2292           for (size_t i = 0; i < num_matches; ++i) {
2293             if (m_interpreter.WasInterrupted())
2294               break;
2295             Module *module = module_list.GetModulePointerAtIndex(i);
2296             if (module) {
2297               if (DumpModuleSymbolFile(result.GetOutputStream(), module))
2298                 num_dumped++;
2299             }
2300           }
2301         } else
2302           result.AppendWarningWithFormat(
2303               "Unable to find an image that matches '%s'.\n", arg_cstr);
2304       }
2305     }
2306 
2307     if (num_dumped > 0)
2308       result.SetStatus(eReturnStatusSuccessFinishResult);
2309     else {
2310       result.AppendError("no matching executable images found");
2311       result.SetStatus(eReturnStatusFailed);
2312     }
2313     return result.Succeeded();
2314   }
2315 };
2316 
2317 #pragma mark CommandObjectTargetModulesDumpLineTable
2318 #define LLDB_OPTIONS_target_modules_dump
2319 #include "CommandOptions.inc"
2320 
2321 // Image debug line table dumping command
2322 
2323 class CommandObjectTargetModulesDumpLineTable
2324     : public CommandObjectTargetModulesSourceFileAutoComplete {
2325 public:
2326   CommandObjectTargetModulesDumpLineTable(CommandInterpreter &interpreter)
2327       : CommandObjectTargetModulesSourceFileAutoComplete(
2328             interpreter, "target modules dump line-table",
2329             "Dump the line table for one or more compilation units.", nullptr,
2330             eCommandRequiresTarget) {}
2331 
2332   ~CommandObjectTargetModulesDumpLineTable() override = default;
2333 
2334   Options *GetOptions() override { return &m_options; }
2335 
2336 protected:
2337   bool DoExecute(Args &command, CommandReturnObject &result) override {
2338     Target *target = m_exe_ctx.GetTargetPtr();
2339     uint32_t total_num_dumped = 0;
2340 
2341     uint32_t addr_byte_size = target->GetArchitecture().GetAddressByteSize();
2342     result.GetOutputStream().SetAddressByteSize(addr_byte_size);
2343     result.GetErrorStream().SetAddressByteSize(addr_byte_size);
2344 
2345     if (command.GetArgumentCount() == 0) {
2346       result.AppendError("file option must be specified.");
2347       result.SetStatus(eReturnStatusFailed);
2348       return result.Succeeded();
2349     } else {
2350       // Dump specified images (by basename or fullpath)
2351       const char *arg_cstr;
2352       for (int arg_idx = 0;
2353            (arg_cstr = command.GetArgumentAtIndex(arg_idx)) != nullptr;
2354            ++arg_idx) {
2355         FileSpec file_spec(arg_cstr);
2356 
2357         const ModuleList &target_modules = target->GetImages();
2358         std::lock_guard<std::recursive_mutex> guard(target_modules.GetMutex());
2359         const size_t num_modules = target_modules.GetSize();
2360         if (num_modules > 0) {
2361           uint32_t num_dumped = 0;
2362           for (uint32_t i = 0; i < num_modules; ++i) {
2363             if (m_interpreter.WasInterrupted())
2364               break;
2365             if (DumpCompileUnitLineTable(
2366                     m_interpreter, result.GetOutputStream(),
2367                     target_modules.GetModulePointerAtIndexUnlocked(i),
2368                     file_spec,
2369                     m_options.m_verbose ? eDescriptionLevelFull
2370                                         : eDescriptionLevelBrief))
2371               num_dumped++;
2372           }
2373           if (num_dumped == 0)
2374             result.AppendWarningWithFormat(
2375                 "No source filenames matched '%s'.\n", arg_cstr);
2376           else
2377             total_num_dumped += num_dumped;
2378         }
2379       }
2380     }
2381 
2382     if (total_num_dumped > 0)
2383       result.SetStatus(eReturnStatusSuccessFinishResult);
2384     else {
2385       result.AppendError("no source filenames matched any command arguments");
2386       result.SetStatus(eReturnStatusFailed);
2387     }
2388     return result.Succeeded();
2389   }
2390 
2391   class CommandOptions : public Options {
2392   public:
2393     CommandOptions() : Options() { OptionParsingStarting(nullptr); }
2394 
2395     Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
2396                           ExecutionContext *execution_context) override {
2397       assert(option_idx == 0 && "We only have one option.");
2398       m_verbose = true;
2399 
2400       return Status();
2401     }
2402 
2403     void OptionParsingStarting(ExecutionContext *execution_context) override {
2404       m_verbose = false;
2405     }
2406 
2407     llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
2408       return llvm::makeArrayRef(g_target_modules_dump_options);
2409     }
2410 
2411     bool m_verbose;
2412   };
2413 
2414   CommandOptions m_options;
2415 };
2416 
2417 #pragma mark CommandObjectTargetModulesDump
2418 
2419 // Dump multi-word command for target modules
2420 
2421 class CommandObjectTargetModulesDump : public CommandObjectMultiword {
2422 public:
2423   // Constructors and Destructors
2424   CommandObjectTargetModulesDump(CommandInterpreter &interpreter)
2425       : CommandObjectMultiword(
2426             interpreter, "target modules dump",
2427             "Commands for dumping information about one or "
2428             "more target modules.",
2429             "target modules dump "
2430             "[headers|symtab|sections|ast|symfile|line-table] "
2431             "[<file1> <file2> ...]") {
2432     LoadSubCommand("objfile",
2433                    CommandObjectSP(
2434                        new CommandObjectTargetModulesDumpObjfile(interpreter)));
2435     LoadSubCommand(
2436         "symtab",
2437         CommandObjectSP(new CommandObjectTargetModulesDumpSymtab(interpreter)));
2438     LoadSubCommand("sections",
2439                    CommandObjectSP(new CommandObjectTargetModulesDumpSections(
2440                        interpreter)));
2441     LoadSubCommand("symfile",
2442                    CommandObjectSP(
2443                        new CommandObjectTargetModulesDumpSymfile(interpreter)));
2444     LoadSubCommand(
2445         "ast", CommandObjectSP(
2446                    new CommandObjectTargetModulesDumpClangAST(interpreter)));
2447     LoadSubCommand("line-table",
2448                    CommandObjectSP(new CommandObjectTargetModulesDumpLineTable(
2449                        interpreter)));
2450   }
2451 
2452   ~CommandObjectTargetModulesDump() override = default;
2453 };
2454 
2455 class CommandObjectTargetModulesAdd : public CommandObjectParsed {
2456 public:
2457   CommandObjectTargetModulesAdd(CommandInterpreter &interpreter)
2458       : CommandObjectParsed(interpreter, "target modules add",
2459                             "Add a new module to the current target's modules.",
2460                             "target modules add [<module>]",
2461                             eCommandRequiresTarget),
2462         m_option_group(), m_symbol_file(LLDB_OPT_SET_1, false, "symfile", 's',
2463                                         0, eArgTypeFilename,
2464                                         "Fullpath to a stand alone debug "
2465                                         "symbols file for when debug symbols "
2466                                         "are not in the executable.") {
2467     m_option_group.Append(&m_uuid_option_group, LLDB_OPT_SET_ALL,
2468                           LLDB_OPT_SET_1);
2469     m_option_group.Append(&m_symbol_file, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
2470     m_option_group.Finalize();
2471   }
2472 
2473   ~CommandObjectTargetModulesAdd() override = default;
2474 
2475   Options *GetOptions() override { return &m_option_group; }
2476 
2477   void
2478   HandleArgumentCompletion(CompletionRequest &request,
2479                            OptionElementVector &opt_element_vector) override {
2480     CommandCompletions::InvokeCommonCompletionCallbacks(
2481         GetCommandInterpreter(), CommandCompletions::eDiskFileCompletion,
2482         request, nullptr);
2483   }
2484 
2485 protected:
2486   OptionGroupOptions m_option_group;
2487   OptionGroupUUID m_uuid_option_group;
2488   OptionGroupFile m_symbol_file;
2489 
2490   bool DoExecute(Args &args, CommandReturnObject &result) override {
2491     Target *target = &GetSelectedTarget();
2492     bool flush = false;
2493 
2494     const size_t argc = args.GetArgumentCount();
2495     if (argc == 0) {
2496       if (m_uuid_option_group.GetOptionValue().OptionWasSet()) {
2497         // We are given a UUID only, go locate the file
2498         ModuleSpec module_spec;
2499         module_spec.GetUUID() =
2500             m_uuid_option_group.GetOptionValue().GetCurrentValue();
2501         if (m_symbol_file.GetOptionValue().OptionWasSet())
2502           module_spec.GetSymbolFileSpec() =
2503               m_symbol_file.GetOptionValue().GetCurrentValue();
2504         if (Symbols::DownloadObjectAndSymbolFile(module_spec)) {
2505           ModuleSP module_sp(
2506               target->GetOrCreateModule(module_spec, true /* notify */));
2507           if (module_sp) {
2508             result.SetStatus(eReturnStatusSuccessFinishResult);
2509             return true;
2510           } else {
2511             StreamString strm;
2512             module_spec.GetUUID().Dump(&strm);
2513             if (module_spec.GetFileSpec()) {
2514               if (module_spec.GetSymbolFileSpec()) {
2515                 result.AppendErrorWithFormat(
2516                     "Unable to create the executable or symbol file with "
2517                     "UUID %s with path %s and symbol file %s",
2518                     strm.GetData(), module_spec.GetFileSpec().GetPath().c_str(),
2519                     module_spec.GetSymbolFileSpec().GetPath().c_str());
2520               } else {
2521                 result.AppendErrorWithFormat(
2522                     "Unable to create the executable or symbol file with "
2523                     "UUID %s with path %s",
2524                     strm.GetData(),
2525                     module_spec.GetFileSpec().GetPath().c_str());
2526               }
2527             } else {
2528               result.AppendErrorWithFormat("Unable to create the executable "
2529                                            "or symbol file with UUID %s",
2530                                            strm.GetData());
2531             }
2532             result.SetStatus(eReturnStatusFailed);
2533             return false;
2534           }
2535         } else {
2536           StreamString strm;
2537           module_spec.GetUUID().Dump(&strm);
2538           result.AppendErrorWithFormat(
2539               "Unable to locate the executable or symbol file with UUID %s",
2540               strm.GetData());
2541           result.SetStatus(eReturnStatusFailed);
2542           return false;
2543         }
2544       } else {
2545         result.AppendError(
2546             "one or more executable image paths must be specified");
2547         result.SetStatus(eReturnStatusFailed);
2548         return false;
2549       }
2550     } else {
2551       for (auto &entry : args.entries()) {
2552         if (entry.ref().empty())
2553           continue;
2554 
2555         FileSpec file_spec(entry.ref());
2556         if (FileSystem::Instance().Exists(file_spec)) {
2557           ModuleSpec module_spec(file_spec);
2558           if (m_uuid_option_group.GetOptionValue().OptionWasSet())
2559             module_spec.GetUUID() =
2560                 m_uuid_option_group.GetOptionValue().GetCurrentValue();
2561           if (m_symbol_file.GetOptionValue().OptionWasSet())
2562             module_spec.GetSymbolFileSpec() =
2563                 m_symbol_file.GetOptionValue().GetCurrentValue();
2564           if (!module_spec.GetArchitecture().IsValid())
2565             module_spec.GetArchitecture() = target->GetArchitecture();
2566           Status error;
2567           ModuleSP module_sp(target->GetOrCreateModule(
2568               module_spec, true /* notify */, &error));
2569           if (!module_sp) {
2570             const char *error_cstr = error.AsCString();
2571             if (error_cstr)
2572               result.AppendError(error_cstr);
2573             else
2574               result.AppendErrorWithFormat("unsupported module: %s",
2575                                            entry.c_str());
2576             result.SetStatus(eReturnStatusFailed);
2577             return false;
2578           } else {
2579             flush = true;
2580           }
2581           result.SetStatus(eReturnStatusSuccessFinishResult);
2582         } else {
2583           std::string resolved_path = file_spec.GetPath();
2584           result.SetStatus(eReturnStatusFailed);
2585           if (resolved_path != entry.ref()) {
2586             result.AppendErrorWithFormat(
2587                 "invalid module path '%s' with resolved path '%s'\n",
2588                 entry.ref().str().c_str(), resolved_path.c_str());
2589             break;
2590           }
2591           result.AppendErrorWithFormat("invalid module path '%s'\n",
2592                                        entry.c_str());
2593           break;
2594         }
2595       }
2596     }
2597 
2598     if (flush) {
2599       ProcessSP process = target->GetProcessSP();
2600       if (process)
2601         process->Flush();
2602     }
2603 
2604     return result.Succeeded();
2605   }
2606 };
2607 
2608 class CommandObjectTargetModulesLoad
2609     : public CommandObjectTargetModulesModuleAutoComplete {
2610 public:
2611   CommandObjectTargetModulesLoad(CommandInterpreter &interpreter)
2612       : CommandObjectTargetModulesModuleAutoComplete(
2613             interpreter, "target modules load",
2614             "Set the load addresses for one or more sections in a target "
2615             "module.",
2616             "target modules load [--file <module> --uuid <uuid>] <sect-name> "
2617             "<address> [<sect-name> <address> ....]",
2618             eCommandRequiresTarget),
2619         m_option_group(),
2620         m_file_option(LLDB_OPT_SET_1, false, "file", 'f', 0, eArgTypeName,
2621                       "Fullpath or basename for module to load.", ""),
2622         m_load_option(LLDB_OPT_SET_1, false, "load", 'l',
2623                       "Write file contents to the memory.", false, true),
2624         m_pc_option(LLDB_OPT_SET_1, false, "set-pc-to-entry", 'p',
2625                     "Set PC to the entry point."
2626                     " Only applicable with '--load' option.",
2627                     false, true),
2628         m_slide_option(LLDB_OPT_SET_1, false, "slide", 's', 0, eArgTypeOffset,
2629                        "Set the load address for all sections to be the "
2630                        "virtual address in the file plus the offset.",
2631                        0) {
2632     m_option_group.Append(&m_uuid_option_group, LLDB_OPT_SET_ALL,
2633                           LLDB_OPT_SET_1);
2634     m_option_group.Append(&m_file_option, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
2635     m_option_group.Append(&m_load_option, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
2636     m_option_group.Append(&m_pc_option, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
2637     m_option_group.Append(&m_slide_option, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
2638     m_option_group.Finalize();
2639   }
2640 
2641   ~CommandObjectTargetModulesLoad() override = default;
2642 
2643   Options *GetOptions() override { return &m_option_group; }
2644 
2645 protected:
2646   bool DoExecute(Args &args, CommandReturnObject &result) override {
2647     Target *target = &GetSelectedTarget();
2648     const bool load = m_load_option.GetOptionValue().GetCurrentValue();
2649     const bool set_pc = m_pc_option.GetOptionValue().GetCurrentValue();
2650 
2651     const size_t argc = args.GetArgumentCount();
2652     ModuleSpec module_spec;
2653     bool search_using_module_spec = false;
2654 
2655     // Allow "load" option to work without --file or --uuid option.
2656     if (load) {
2657       if (!m_file_option.GetOptionValue().OptionWasSet() &&
2658           !m_uuid_option_group.GetOptionValue().OptionWasSet()) {
2659         ModuleList &module_list = target->GetImages();
2660         if (module_list.GetSize() == 1) {
2661           search_using_module_spec = true;
2662           module_spec.GetFileSpec() =
2663               module_list.GetModuleAtIndex(0)->GetFileSpec();
2664         }
2665       }
2666     }
2667 
2668     if (m_file_option.GetOptionValue().OptionWasSet()) {
2669       search_using_module_spec = true;
2670       const char *arg_cstr = m_file_option.GetOptionValue().GetCurrentValue();
2671       const bool use_global_module_list = true;
2672       ModuleList module_list;
2673       const size_t num_matches = FindModulesByName(
2674           target, arg_cstr, module_list, use_global_module_list);
2675       if (num_matches == 1) {
2676         module_spec.GetFileSpec() =
2677             module_list.GetModuleAtIndex(0)->GetFileSpec();
2678       } else if (num_matches > 1) {
2679         search_using_module_spec = false;
2680         result.AppendErrorWithFormat(
2681             "more than 1 module matched by name '%s'\n", arg_cstr);
2682         result.SetStatus(eReturnStatusFailed);
2683       } else {
2684         search_using_module_spec = false;
2685         result.AppendErrorWithFormat("no object file for module '%s'\n",
2686                                      arg_cstr);
2687         result.SetStatus(eReturnStatusFailed);
2688       }
2689     }
2690 
2691     if (m_uuid_option_group.GetOptionValue().OptionWasSet()) {
2692       search_using_module_spec = true;
2693       module_spec.GetUUID() =
2694           m_uuid_option_group.GetOptionValue().GetCurrentValue();
2695     }
2696 
2697     if (search_using_module_spec) {
2698       ModuleList matching_modules;
2699       target->GetImages().FindModules(module_spec, matching_modules);
2700       const size_t num_matches = matching_modules.GetSize();
2701 
2702       char path[PATH_MAX];
2703       if (num_matches == 1) {
2704         Module *module = matching_modules.GetModulePointerAtIndex(0);
2705         if (module) {
2706           ObjectFile *objfile = module->GetObjectFile();
2707           if (objfile) {
2708             SectionList *section_list = module->GetSectionList();
2709             if (section_list) {
2710               bool changed = false;
2711               if (argc == 0) {
2712                 if (m_slide_option.GetOptionValue().OptionWasSet()) {
2713                   const addr_t slide =
2714                       m_slide_option.GetOptionValue().GetCurrentValue();
2715                   const bool slide_is_offset = true;
2716                   module->SetLoadAddress(*target, slide, slide_is_offset,
2717                                          changed);
2718                 } else {
2719                   result.AppendError("one or more section name + load "
2720                                      "address pair must be specified");
2721                   result.SetStatus(eReturnStatusFailed);
2722                   return false;
2723                 }
2724               } else {
2725                 if (m_slide_option.GetOptionValue().OptionWasSet()) {
2726                   result.AppendError("The \"--slide <offset>\" option can't "
2727                                      "be used in conjunction with setting "
2728                                      "section load addresses.\n");
2729                   result.SetStatus(eReturnStatusFailed);
2730                   return false;
2731                 }
2732 
2733                 for (size_t i = 0; i < argc; i += 2) {
2734                   const char *sect_name = args.GetArgumentAtIndex(i);
2735                   const char *load_addr_cstr = args.GetArgumentAtIndex(i + 1);
2736                   if (sect_name && load_addr_cstr) {
2737                     ConstString const_sect_name(sect_name);
2738                     addr_t load_addr;
2739                     if (llvm::to_integer(load_addr_cstr, load_addr)) {
2740                       SectionSP section_sp(
2741                           section_list->FindSectionByName(const_sect_name));
2742                       if (section_sp) {
2743                         if (section_sp->IsThreadSpecific()) {
2744                           result.AppendErrorWithFormat(
2745                               "thread specific sections are not yet "
2746                               "supported (section '%s')\n",
2747                               sect_name);
2748                           result.SetStatus(eReturnStatusFailed);
2749                           break;
2750                         } else {
2751                           if (target->GetSectionLoadList()
2752                                   .SetSectionLoadAddress(section_sp, load_addr))
2753                             changed = true;
2754                           result.AppendMessageWithFormat(
2755                               "section '%s' loaded at 0x%" PRIx64 "\n",
2756                               sect_name, load_addr);
2757                         }
2758                       } else {
2759                         result.AppendErrorWithFormat("no section found that "
2760                                                      "matches the section "
2761                                                      "name '%s'\n",
2762                                                      sect_name);
2763                         result.SetStatus(eReturnStatusFailed);
2764                         break;
2765                       }
2766                     } else {
2767                       result.AppendErrorWithFormat(
2768                           "invalid load address string '%s'\n", load_addr_cstr);
2769                       result.SetStatus(eReturnStatusFailed);
2770                       break;
2771                     }
2772                   } else {
2773                     if (sect_name)
2774                       result.AppendError("section names must be followed by "
2775                                          "a load address.\n");
2776                     else
2777                       result.AppendError("one or more section name + load "
2778                                          "address pair must be specified.\n");
2779                     result.SetStatus(eReturnStatusFailed);
2780                     break;
2781                   }
2782                 }
2783               }
2784 
2785               if (changed) {
2786                 target->ModulesDidLoad(matching_modules);
2787                 Process *process = m_exe_ctx.GetProcessPtr();
2788                 if (process)
2789                   process->Flush();
2790               }
2791               if (load) {
2792                 ProcessSP process = target->CalculateProcess();
2793                 Address file_entry = objfile->GetEntryPointAddress();
2794                 if (!process) {
2795                   result.AppendError("No process");
2796                   return false;
2797                 }
2798                 if (set_pc && !file_entry.IsValid()) {
2799                   result.AppendError("No entry address in object file");
2800                   return false;
2801                 }
2802                 std::vector<ObjectFile::LoadableData> loadables(
2803                     objfile->GetLoadableData(*target));
2804                 if (loadables.size() == 0) {
2805                   result.AppendError("No loadable sections");
2806                   return false;
2807                 }
2808                 Status error = process->WriteObjectFile(std::move(loadables));
2809                 if (error.Fail()) {
2810                   result.AppendError(error.AsCString());
2811                   return false;
2812                 }
2813                 if (set_pc) {
2814                   ThreadList &thread_list = process->GetThreadList();
2815                   RegisterContextSP reg_context(
2816                       thread_list.GetSelectedThread()->GetRegisterContext());
2817                   addr_t file_entry_addr = file_entry.GetLoadAddress(target);
2818                   if (!reg_context->SetPC(file_entry_addr)) {
2819                     result.AppendErrorWithFormat("failed to set PC value to "
2820                                                  "0x%" PRIx64 "\n",
2821                                                  file_entry_addr);
2822                     result.SetStatus(eReturnStatusFailed);
2823                   }
2824                 }
2825               }
2826             } else {
2827               module->GetFileSpec().GetPath(path, sizeof(path));
2828               result.AppendErrorWithFormat("no sections in object file '%s'\n",
2829                                            path);
2830               result.SetStatus(eReturnStatusFailed);
2831             }
2832           } else {
2833             module->GetFileSpec().GetPath(path, sizeof(path));
2834             result.AppendErrorWithFormat("no object file for module '%s'\n",
2835                                          path);
2836             result.SetStatus(eReturnStatusFailed);
2837           }
2838         } else {
2839           FileSpec *module_spec_file = module_spec.GetFileSpecPtr();
2840           if (module_spec_file) {
2841             module_spec_file->GetPath(path, sizeof(path));
2842             result.AppendErrorWithFormat("invalid module '%s'.\n", path);
2843           } else
2844             result.AppendError("no module spec");
2845           result.SetStatus(eReturnStatusFailed);
2846         }
2847       } else {
2848         std::string uuid_str;
2849 
2850         if (module_spec.GetFileSpec())
2851           module_spec.GetFileSpec().GetPath(path, sizeof(path));
2852         else
2853           path[0] = '\0';
2854 
2855         if (module_spec.GetUUIDPtr())
2856           uuid_str = module_spec.GetUUID().GetAsString();
2857         if (num_matches > 1) {
2858           result.AppendErrorWithFormat(
2859               "multiple modules match%s%s%s%s:\n", path[0] ? " file=" : "",
2860               path, !uuid_str.empty() ? " uuid=" : "", uuid_str.c_str());
2861           for (size_t i = 0; i < num_matches; ++i) {
2862             if (matching_modules.GetModulePointerAtIndex(i)
2863                     ->GetFileSpec()
2864                     .GetPath(path, sizeof(path)))
2865               result.AppendMessageWithFormat("%s\n", path);
2866           }
2867         } else {
2868           result.AppendErrorWithFormat(
2869               "no modules were found  that match%s%s%s%s.\n",
2870               path[0] ? " file=" : "", path, !uuid_str.empty() ? " uuid=" : "",
2871               uuid_str.c_str());
2872         }
2873         result.SetStatus(eReturnStatusFailed);
2874       }
2875     } else {
2876       result.AppendError("either the \"--file <module>\" or the \"--uuid "
2877                          "<uuid>\" option must be specified.\n");
2878       result.SetStatus(eReturnStatusFailed);
2879       return false;
2880     }
2881     return result.Succeeded();
2882   }
2883 
2884   OptionGroupOptions m_option_group;
2885   OptionGroupUUID m_uuid_option_group;
2886   OptionGroupString m_file_option;
2887   OptionGroupBoolean m_load_option;
2888   OptionGroupBoolean m_pc_option;
2889   OptionGroupUInt64 m_slide_option;
2890 };
2891 
2892 // List images with associated information
2893 #define LLDB_OPTIONS_target_modules_list
2894 #include "CommandOptions.inc"
2895 
2896 class CommandObjectTargetModulesList : public CommandObjectParsed {
2897 public:
2898   class CommandOptions : public Options {
2899   public:
2900     CommandOptions()
2901         : Options(), m_format_array(), m_use_global_module_list(false),
2902           m_module_addr(LLDB_INVALID_ADDRESS) {}
2903 
2904     ~CommandOptions() override = default;
2905 
2906     Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
2907                           ExecutionContext *execution_context) override {
2908       Status error;
2909 
2910       const int short_option = m_getopt_table[option_idx].val;
2911       if (short_option == 'g') {
2912         m_use_global_module_list = true;
2913       } else if (short_option == 'a') {
2914         m_module_addr = OptionArgParser::ToAddress(
2915             execution_context, option_arg, LLDB_INVALID_ADDRESS, &error);
2916       } else {
2917         unsigned long width = 0;
2918         option_arg.getAsInteger(0, width);
2919         m_format_array.push_back(std::make_pair(short_option, width));
2920       }
2921       return error;
2922     }
2923 
2924     void OptionParsingStarting(ExecutionContext *execution_context) override {
2925       m_format_array.clear();
2926       m_use_global_module_list = false;
2927       m_module_addr = LLDB_INVALID_ADDRESS;
2928     }
2929 
2930     llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
2931       return llvm::makeArrayRef(g_target_modules_list_options);
2932     }
2933 
2934     // Instance variables to hold the values for command options.
2935     typedef std::vector<std::pair<char, uint32_t>> FormatWidthCollection;
2936     FormatWidthCollection m_format_array;
2937     bool m_use_global_module_list;
2938     lldb::addr_t m_module_addr;
2939   };
2940 
2941   CommandObjectTargetModulesList(CommandInterpreter &interpreter)
2942       : CommandObjectParsed(
2943             interpreter, "target modules list",
2944             "List current executable and dependent shared library images.",
2945             "target modules list [<cmd-options>]"),
2946         m_options() {}
2947 
2948   ~CommandObjectTargetModulesList() override = default;
2949 
2950   Options *GetOptions() override { return &m_options; }
2951 
2952 protected:
2953   bool DoExecute(Args &command, CommandReturnObject &result) override {
2954     Target *target = GetDebugger().GetSelectedTarget().get();
2955     const bool use_global_module_list = m_options.m_use_global_module_list;
2956     // Define a local module list here to ensure it lives longer than any
2957     // "locker" object which might lock its contents below (through the
2958     // "module_list_ptr" variable).
2959     ModuleList module_list;
2960     if (target == nullptr && !use_global_module_list) {
2961       result.AppendError("invalid target, create a debug target using the "
2962                          "'target create' command");
2963       result.SetStatus(eReturnStatusFailed);
2964       return false;
2965     } else {
2966       if (target) {
2967         uint32_t addr_byte_size =
2968             target->GetArchitecture().GetAddressByteSize();
2969         result.GetOutputStream().SetAddressByteSize(addr_byte_size);
2970         result.GetErrorStream().SetAddressByteSize(addr_byte_size);
2971       }
2972       // Dump all sections for all modules images
2973       Stream &strm = result.GetOutputStream();
2974 
2975       if (m_options.m_module_addr != LLDB_INVALID_ADDRESS) {
2976         if (target) {
2977           Address module_address;
2978           if (module_address.SetLoadAddress(m_options.m_module_addr, target)) {
2979             ModuleSP module_sp(module_address.GetModule());
2980             if (module_sp) {
2981               PrintModule(target, module_sp.get(), 0, strm);
2982               result.SetStatus(eReturnStatusSuccessFinishResult);
2983             } else {
2984               result.AppendErrorWithFormat(
2985                   "Couldn't find module matching address: 0x%" PRIx64 ".",
2986                   m_options.m_module_addr);
2987               result.SetStatus(eReturnStatusFailed);
2988             }
2989           } else {
2990             result.AppendErrorWithFormat(
2991                 "Couldn't find module containing address: 0x%" PRIx64 ".",
2992                 m_options.m_module_addr);
2993             result.SetStatus(eReturnStatusFailed);
2994           }
2995         } else {
2996           result.AppendError(
2997               "Can only look up modules by address with a valid target.");
2998           result.SetStatus(eReturnStatusFailed);
2999         }
3000         return result.Succeeded();
3001       }
3002 
3003       size_t num_modules = 0;
3004 
3005       // This locker will be locked on the mutex in module_list_ptr if it is
3006       // non-nullptr. Otherwise it will lock the
3007       // AllocationModuleCollectionMutex when accessing the global module list
3008       // directly.
3009       std::unique_lock<std::recursive_mutex> guard(
3010           Module::GetAllocationModuleCollectionMutex(), std::defer_lock);
3011 
3012       const ModuleList *module_list_ptr = nullptr;
3013       const size_t argc = command.GetArgumentCount();
3014       if (argc == 0) {
3015         if (use_global_module_list) {
3016           guard.lock();
3017           num_modules = Module::GetNumberAllocatedModules();
3018         } else {
3019           module_list_ptr = &target->GetImages();
3020         }
3021       } else {
3022         for (const Args::ArgEntry &arg : command) {
3023           // Dump specified images (by basename or fullpath)
3024           const size_t num_matches = FindModulesByName(
3025               target, arg.c_str(), module_list, use_global_module_list);
3026           if (num_matches == 0) {
3027             if (argc == 1) {
3028               result.AppendErrorWithFormat("no modules found that match '%s'",
3029                                            arg.c_str());
3030               result.SetStatus(eReturnStatusFailed);
3031               return false;
3032             }
3033           }
3034         }
3035 
3036         module_list_ptr = &module_list;
3037       }
3038 
3039       std::unique_lock<std::recursive_mutex> lock;
3040       if (module_list_ptr != nullptr) {
3041         lock =
3042             std::unique_lock<std::recursive_mutex>(module_list_ptr->GetMutex());
3043 
3044         num_modules = module_list_ptr->GetSize();
3045       }
3046 
3047       if (num_modules > 0) {
3048         for (uint32_t image_idx = 0; image_idx < num_modules; ++image_idx) {
3049           ModuleSP module_sp;
3050           Module *module;
3051           if (module_list_ptr) {
3052             module_sp = module_list_ptr->GetModuleAtIndexUnlocked(image_idx);
3053             module = module_sp.get();
3054           } else {
3055             module = Module::GetAllocatedModuleAtIndex(image_idx);
3056             module_sp = module->shared_from_this();
3057           }
3058 
3059           const size_t indent = strm.Printf("[%3u] ", image_idx);
3060           PrintModule(target, module, indent, strm);
3061         }
3062         result.SetStatus(eReturnStatusSuccessFinishResult);
3063       } else {
3064         if (argc) {
3065           if (use_global_module_list)
3066             result.AppendError(
3067                 "the global module list has no matching modules");
3068           else
3069             result.AppendError("the target has no matching modules");
3070         } else {
3071           if (use_global_module_list)
3072             result.AppendError("the global module list is empty");
3073           else
3074             result.AppendError(
3075                 "the target has no associated executable images");
3076         }
3077         result.SetStatus(eReturnStatusFailed);
3078         return false;
3079       }
3080     }
3081     return result.Succeeded();
3082   }
3083 
3084   void PrintModule(Target *target, Module *module, int indent, Stream &strm) {
3085     if (module == nullptr) {
3086       strm.PutCString("Null module");
3087       return;
3088     }
3089 
3090     bool dump_object_name = false;
3091     if (m_options.m_format_array.empty()) {
3092       m_options.m_format_array.push_back(std::make_pair('u', 0));
3093       m_options.m_format_array.push_back(std::make_pair('h', 0));
3094       m_options.m_format_array.push_back(std::make_pair('f', 0));
3095       m_options.m_format_array.push_back(std::make_pair('S', 0));
3096     }
3097     const size_t num_entries = m_options.m_format_array.size();
3098     bool print_space = false;
3099     for (size_t i = 0; i < num_entries; ++i) {
3100       if (print_space)
3101         strm.PutChar(' ');
3102       print_space = true;
3103       const char format_char = m_options.m_format_array[i].first;
3104       uint32_t width = m_options.m_format_array[i].second;
3105       switch (format_char) {
3106       case 'A':
3107         DumpModuleArchitecture(strm, module, false, width);
3108         break;
3109 
3110       case 't':
3111         DumpModuleArchitecture(strm, module, true, width);
3112         break;
3113 
3114       case 'f':
3115         DumpFullpath(strm, &module->GetFileSpec(), width);
3116         dump_object_name = true;
3117         break;
3118 
3119       case 'd':
3120         DumpDirectory(strm, &module->GetFileSpec(), width);
3121         break;
3122 
3123       case 'b':
3124         DumpBasename(strm, &module->GetFileSpec(), width);
3125         dump_object_name = true;
3126         break;
3127 
3128       case 'h':
3129       case 'o':
3130         // Image header address
3131         {
3132           uint32_t addr_nibble_width =
3133               target ? (target->GetArchitecture().GetAddressByteSize() * 2)
3134                      : 16;
3135 
3136           ObjectFile *objfile = module->GetObjectFile();
3137           if (objfile) {
3138             Address base_addr(objfile->GetBaseAddress());
3139             if (base_addr.IsValid()) {
3140               if (target && !target->GetSectionLoadList().IsEmpty()) {
3141                 lldb::addr_t load_addr = base_addr.GetLoadAddress(target);
3142                 if (load_addr == LLDB_INVALID_ADDRESS) {
3143                   base_addr.Dump(&strm, target,
3144                                  Address::DumpStyleModuleWithFileAddress,
3145                                  Address::DumpStyleFileAddress);
3146                 } else {
3147                   if (format_char == 'o') {
3148                     // Show the offset of slide for the image
3149                     strm.Printf("0x%*.*" PRIx64, addr_nibble_width,
3150                                 addr_nibble_width,
3151                                 load_addr - base_addr.GetFileAddress());
3152                   } else {
3153                     // Show the load address of the image
3154                     strm.Printf("0x%*.*" PRIx64, addr_nibble_width,
3155                                 addr_nibble_width, load_addr);
3156                   }
3157                 }
3158                 break;
3159               }
3160               // The address was valid, but the image isn't loaded, output the
3161               // address in an appropriate format
3162               base_addr.Dump(&strm, target, Address::DumpStyleFileAddress);
3163               break;
3164             }
3165           }
3166           strm.Printf("%*s", addr_nibble_width + 2, "");
3167         }
3168         break;
3169 
3170       case 'r': {
3171         size_t ref_count = 0;
3172         ModuleSP module_sp(module->shared_from_this());
3173         if (module_sp) {
3174           // Take one away to make sure we don't count our local "module_sp"
3175           ref_count = module_sp.use_count() - 1;
3176         }
3177         if (width)
3178           strm.Printf("{%*" PRIu64 "}", width, (uint64_t)ref_count);
3179         else
3180           strm.Printf("{%" PRIu64 "}", (uint64_t)ref_count);
3181       } break;
3182 
3183       case 's':
3184       case 'S': {
3185         if (const SymbolFile *symbol_file = module->GetSymbolFile()) {
3186           const FileSpec symfile_spec =
3187               symbol_file->GetObjectFile()->GetFileSpec();
3188           if (format_char == 'S') {
3189             // Dump symbol file only if different from module file
3190             if (!symfile_spec || symfile_spec == module->GetFileSpec()) {
3191               print_space = false;
3192               break;
3193             }
3194             // Add a newline and indent past the index
3195             strm.Printf("\n%*s", indent, "");
3196           }
3197           DumpFullpath(strm, &symfile_spec, width);
3198           dump_object_name = true;
3199           break;
3200         }
3201         strm.Printf("%.*s", width, "<NONE>");
3202       } break;
3203 
3204       case 'm':
3205         strm.Format("{0:%c}", llvm::fmt_align(module->GetModificationTime(),
3206                                               llvm::AlignStyle::Left, width));
3207         break;
3208 
3209       case 'p':
3210         strm.Printf("%p", static_cast<void *>(module));
3211         break;
3212 
3213       case 'u':
3214         DumpModuleUUID(strm, module);
3215         break;
3216 
3217       default:
3218         break;
3219       }
3220     }
3221     if (dump_object_name) {
3222       const char *object_name = module->GetObjectName().GetCString();
3223       if (object_name)
3224         strm.Printf("(%s)", object_name);
3225     }
3226     strm.EOL();
3227   }
3228 
3229   CommandOptions m_options;
3230 };
3231 
3232 #pragma mark CommandObjectTargetModulesShowUnwind
3233 
3234 // Lookup unwind information in images
3235 #define LLDB_OPTIONS_target_modules_show_unwind
3236 #include "CommandOptions.inc"
3237 
3238 class CommandObjectTargetModulesShowUnwind : public CommandObjectParsed {
3239 public:
3240   enum {
3241     eLookupTypeInvalid = -1,
3242     eLookupTypeAddress = 0,
3243     eLookupTypeSymbol,
3244     eLookupTypeFunction,
3245     eLookupTypeFunctionOrSymbol,
3246     kNumLookupTypes
3247   };
3248 
3249   class CommandOptions : public Options {
3250   public:
3251     CommandOptions()
3252         : Options(), m_type(eLookupTypeInvalid), m_str(),
3253           m_addr(LLDB_INVALID_ADDRESS) {}
3254 
3255     ~CommandOptions() override = default;
3256 
3257     Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
3258                           ExecutionContext *execution_context) override {
3259       Status error;
3260 
3261       const int short_option = m_getopt_table[option_idx].val;
3262 
3263       switch (short_option) {
3264       case 'a': {
3265         m_str = std::string(option_arg);
3266         m_type = eLookupTypeAddress;
3267         m_addr = OptionArgParser::ToAddress(execution_context, option_arg,
3268                                             LLDB_INVALID_ADDRESS, &error);
3269         if (m_addr == LLDB_INVALID_ADDRESS)
3270           error.SetErrorStringWithFormat("invalid address string '%s'",
3271                                          option_arg.str().c_str());
3272         break;
3273       }
3274 
3275       case 'n':
3276         m_str = std::string(option_arg);
3277         m_type = eLookupTypeFunctionOrSymbol;
3278         break;
3279 
3280       default:
3281         llvm_unreachable("Unimplemented option");
3282       }
3283 
3284       return error;
3285     }
3286 
3287     void OptionParsingStarting(ExecutionContext *execution_context) override {
3288       m_type = eLookupTypeInvalid;
3289       m_str.clear();
3290       m_addr = LLDB_INVALID_ADDRESS;
3291     }
3292 
3293     llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
3294       return llvm::makeArrayRef(g_target_modules_show_unwind_options);
3295     }
3296 
3297     // Instance variables to hold the values for command options.
3298 
3299     int m_type;        // Should be a eLookupTypeXXX enum after parsing options
3300     std::string m_str; // Holds name lookup
3301     lldb::addr_t m_addr; // Holds the address to lookup
3302   };
3303 
3304   CommandObjectTargetModulesShowUnwind(CommandInterpreter &interpreter)
3305       : CommandObjectParsed(
3306             interpreter, "target modules show-unwind",
3307             "Show synthesized unwind instructions for a function.", nullptr,
3308             eCommandRequiresTarget | eCommandRequiresProcess |
3309                 eCommandProcessMustBeLaunched | eCommandProcessMustBePaused),
3310         m_options() {}
3311 
3312   ~CommandObjectTargetModulesShowUnwind() override = default;
3313 
3314   Options *GetOptions() override { return &m_options; }
3315 
3316 protected:
3317   bool DoExecute(Args &command, CommandReturnObject &result) override {
3318     Target *target = m_exe_ctx.GetTargetPtr();
3319     Process *process = m_exe_ctx.GetProcessPtr();
3320     ABI *abi = nullptr;
3321     if (process)
3322       abi = process->GetABI().get();
3323 
3324     if (process == nullptr) {
3325       result.AppendError(
3326           "You must have a process running to use this command.");
3327       result.SetStatus(eReturnStatusFailed);
3328       return false;
3329     }
3330 
3331     ThreadList threads(process->GetThreadList());
3332     if (threads.GetSize() == 0) {
3333       result.AppendError("The process must be paused to use this command.");
3334       result.SetStatus(eReturnStatusFailed);
3335       return false;
3336     }
3337 
3338     ThreadSP thread(threads.GetThreadAtIndex(0));
3339     if (!thread) {
3340       result.AppendError("The process must be paused to use this command.");
3341       result.SetStatus(eReturnStatusFailed);
3342       return false;
3343     }
3344 
3345     SymbolContextList sc_list;
3346 
3347     if (m_options.m_type == eLookupTypeFunctionOrSymbol) {
3348       ConstString function_name(m_options.m_str.c_str());
3349       target->GetImages().FindFunctions(function_name, eFunctionNameTypeAuto,
3350                                         true, false, sc_list);
3351     } else if (m_options.m_type == eLookupTypeAddress && target) {
3352       Address addr;
3353       if (target->GetSectionLoadList().ResolveLoadAddress(m_options.m_addr,
3354                                                           addr)) {
3355         SymbolContext sc;
3356         ModuleSP module_sp(addr.GetModule());
3357         module_sp->ResolveSymbolContextForAddress(addr,
3358                                                   eSymbolContextEverything, sc);
3359         if (sc.function || sc.symbol) {
3360           sc_list.Append(sc);
3361         }
3362       }
3363     } else {
3364       result.AppendError(
3365           "address-expression or function name option must be specified.");
3366       result.SetStatus(eReturnStatusFailed);
3367       return false;
3368     }
3369 
3370     size_t num_matches = sc_list.GetSize();
3371     if (num_matches == 0) {
3372       result.AppendErrorWithFormat("no unwind data found that matches '%s'.",
3373                                    m_options.m_str.c_str());
3374       result.SetStatus(eReturnStatusFailed);
3375       return false;
3376     }
3377 
3378     for (uint32_t idx = 0; idx < num_matches; idx++) {
3379       SymbolContext sc;
3380       sc_list.GetContextAtIndex(idx, sc);
3381       if (sc.symbol == nullptr && sc.function == nullptr)
3382         continue;
3383       if (!sc.module_sp || sc.module_sp->GetObjectFile() == nullptr)
3384         continue;
3385       AddressRange range;
3386       if (!sc.GetAddressRange(eSymbolContextFunction | eSymbolContextSymbol, 0,
3387                               false, range))
3388         continue;
3389       if (!range.GetBaseAddress().IsValid())
3390         continue;
3391       ConstString funcname(sc.GetFunctionName());
3392       if (funcname.IsEmpty())
3393         continue;
3394       addr_t start_addr = range.GetBaseAddress().GetLoadAddress(target);
3395       if (abi)
3396         start_addr = abi->FixCodeAddress(start_addr);
3397 
3398       FuncUnwindersSP func_unwinders_sp(
3399           sc.module_sp->GetUnwindTable()
3400               .GetUncachedFuncUnwindersContainingAddress(start_addr, sc));
3401       if (!func_unwinders_sp)
3402         continue;
3403 
3404       result.GetOutputStream().Printf(
3405           "UNWIND PLANS for %s`%s (start addr 0x%" PRIx64 ")\n\n",
3406           sc.module_sp->GetPlatformFileSpec().GetFilename().AsCString(),
3407           funcname.AsCString(), start_addr);
3408 
3409       UnwindPlanSP non_callsite_unwind_plan =
3410           func_unwinders_sp->GetUnwindPlanAtNonCallSite(*target, *thread);
3411       if (non_callsite_unwind_plan) {
3412         result.GetOutputStream().Printf(
3413             "Asynchronous (not restricted to call-sites) UnwindPlan is '%s'\n",
3414             non_callsite_unwind_plan->GetSourceName().AsCString());
3415       }
3416       UnwindPlanSP callsite_unwind_plan =
3417           func_unwinders_sp->GetUnwindPlanAtCallSite(*target, *thread);
3418       if (callsite_unwind_plan) {
3419         result.GetOutputStream().Printf(
3420             "Synchronous (restricted to call-sites) UnwindPlan is '%s'\n",
3421             callsite_unwind_plan->GetSourceName().AsCString());
3422       }
3423       UnwindPlanSP fast_unwind_plan =
3424           func_unwinders_sp->GetUnwindPlanFastUnwind(*target, *thread);
3425       if (fast_unwind_plan) {
3426         result.GetOutputStream().Printf(
3427             "Fast UnwindPlan is '%s'\n",
3428             fast_unwind_plan->GetSourceName().AsCString());
3429       }
3430 
3431       result.GetOutputStream().Printf("\n");
3432 
3433       UnwindPlanSP assembly_sp =
3434           func_unwinders_sp->GetAssemblyUnwindPlan(*target, *thread);
3435       if (assembly_sp) {
3436         result.GetOutputStream().Printf(
3437             "Assembly language inspection UnwindPlan:\n");
3438         assembly_sp->Dump(result.GetOutputStream(), thread.get(),
3439                           LLDB_INVALID_ADDRESS);
3440         result.GetOutputStream().Printf("\n");
3441       }
3442 
3443       UnwindPlanSP of_unwind_sp =
3444           func_unwinders_sp->GetObjectFileUnwindPlan(*target);
3445       if (of_unwind_sp) {
3446         result.GetOutputStream().Printf("object file UnwindPlan:\n");
3447         of_unwind_sp->Dump(result.GetOutputStream(), thread.get(),
3448                            LLDB_INVALID_ADDRESS);
3449         result.GetOutputStream().Printf("\n");
3450       }
3451 
3452       UnwindPlanSP of_unwind_augmented_sp =
3453           func_unwinders_sp->GetObjectFileAugmentedUnwindPlan(*target, *thread);
3454       if (of_unwind_augmented_sp) {
3455         result.GetOutputStream().Printf("object file augmented UnwindPlan:\n");
3456         of_unwind_augmented_sp->Dump(result.GetOutputStream(), thread.get(),
3457                                      LLDB_INVALID_ADDRESS);
3458         result.GetOutputStream().Printf("\n");
3459       }
3460 
3461       UnwindPlanSP ehframe_sp =
3462           func_unwinders_sp->GetEHFrameUnwindPlan(*target);
3463       if (ehframe_sp) {
3464         result.GetOutputStream().Printf("eh_frame UnwindPlan:\n");
3465         ehframe_sp->Dump(result.GetOutputStream(), thread.get(),
3466                          LLDB_INVALID_ADDRESS);
3467         result.GetOutputStream().Printf("\n");
3468       }
3469 
3470       UnwindPlanSP ehframe_augmented_sp =
3471           func_unwinders_sp->GetEHFrameAugmentedUnwindPlan(*target, *thread);
3472       if (ehframe_augmented_sp) {
3473         result.GetOutputStream().Printf("eh_frame augmented UnwindPlan:\n");
3474         ehframe_augmented_sp->Dump(result.GetOutputStream(), thread.get(),
3475                                    LLDB_INVALID_ADDRESS);
3476         result.GetOutputStream().Printf("\n");
3477       }
3478 
3479       if (UnwindPlanSP plan_sp =
3480               func_unwinders_sp->GetDebugFrameUnwindPlan(*target)) {
3481         result.GetOutputStream().Printf("debug_frame UnwindPlan:\n");
3482         plan_sp->Dump(result.GetOutputStream(), thread.get(),
3483                       LLDB_INVALID_ADDRESS);
3484         result.GetOutputStream().Printf("\n");
3485       }
3486 
3487       if (UnwindPlanSP plan_sp =
3488               func_unwinders_sp->GetDebugFrameAugmentedUnwindPlan(*target,
3489                                                                   *thread)) {
3490         result.GetOutputStream().Printf("debug_frame augmented UnwindPlan:\n");
3491         plan_sp->Dump(result.GetOutputStream(), thread.get(),
3492                       LLDB_INVALID_ADDRESS);
3493         result.GetOutputStream().Printf("\n");
3494       }
3495 
3496       UnwindPlanSP arm_unwind_sp =
3497           func_unwinders_sp->GetArmUnwindUnwindPlan(*target);
3498       if (arm_unwind_sp) {
3499         result.GetOutputStream().Printf("ARM.exidx unwind UnwindPlan:\n");
3500         arm_unwind_sp->Dump(result.GetOutputStream(), thread.get(),
3501                             LLDB_INVALID_ADDRESS);
3502         result.GetOutputStream().Printf("\n");
3503       }
3504 
3505       if (UnwindPlanSP symfile_plan_sp =
3506               func_unwinders_sp->GetSymbolFileUnwindPlan(*thread)) {
3507         result.GetOutputStream().Printf("Symbol file UnwindPlan:\n");
3508         symfile_plan_sp->Dump(result.GetOutputStream(), thread.get(),
3509                               LLDB_INVALID_ADDRESS);
3510         result.GetOutputStream().Printf("\n");
3511       }
3512 
3513       UnwindPlanSP compact_unwind_sp =
3514           func_unwinders_sp->GetCompactUnwindUnwindPlan(*target);
3515       if (compact_unwind_sp) {
3516         result.GetOutputStream().Printf("Compact unwind UnwindPlan:\n");
3517         compact_unwind_sp->Dump(result.GetOutputStream(), thread.get(),
3518                                 LLDB_INVALID_ADDRESS);
3519         result.GetOutputStream().Printf("\n");
3520       }
3521 
3522       if (fast_unwind_plan) {
3523         result.GetOutputStream().Printf("Fast UnwindPlan:\n");
3524         fast_unwind_plan->Dump(result.GetOutputStream(), thread.get(),
3525                                LLDB_INVALID_ADDRESS);
3526         result.GetOutputStream().Printf("\n");
3527       }
3528 
3529       ABISP abi_sp = process->GetABI();
3530       if (abi_sp) {
3531         UnwindPlan arch_default(lldb::eRegisterKindGeneric);
3532         if (abi_sp->CreateDefaultUnwindPlan(arch_default)) {
3533           result.GetOutputStream().Printf("Arch default UnwindPlan:\n");
3534           arch_default.Dump(result.GetOutputStream(), thread.get(),
3535                             LLDB_INVALID_ADDRESS);
3536           result.GetOutputStream().Printf("\n");
3537         }
3538 
3539         UnwindPlan arch_entry(lldb::eRegisterKindGeneric);
3540         if (abi_sp->CreateFunctionEntryUnwindPlan(arch_entry)) {
3541           result.GetOutputStream().Printf(
3542               "Arch default at entry point UnwindPlan:\n");
3543           arch_entry.Dump(result.GetOutputStream(), thread.get(),
3544                           LLDB_INVALID_ADDRESS);
3545           result.GetOutputStream().Printf("\n");
3546         }
3547       }
3548 
3549       result.GetOutputStream().Printf("\n");
3550     }
3551     return result.Succeeded();
3552   }
3553 
3554   CommandOptions m_options;
3555 };
3556 
3557 // Lookup information in images
3558 #define LLDB_OPTIONS_target_modules_lookup
3559 #include "CommandOptions.inc"
3560 
3561 class CommandObjectTargetModulesLookup : public CommandObjectParsed {
3562 public:
3563   enum {
3564     eLookupTypeInvalid = -1,
3565     eLookupTypeAddress = 0,
3566     eLookupTypeSymbol,
3567     eLookupTypeFileLine, // Line is optional
3568     eLookupTypeFunction,
3569     eLookupTypeFunctionOrSymbol,
3570     eLookupTypeType,
3571     kNumLookupTypes
3572   };
3573 
3574   class CommandOptions : public Options {
3575   public:
3576     CommandOptions() : Options() { OptionParsingStarting(nullptr); }
3577 
3578     ~CommandOptions() override = default;
3579 
3580     Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
3581                           ExecutionContext *execution_context) override {
3582       Status error;
3583 
3584       const int short_option = m_getopt_table[option_idx].val;
3585 
3586       switch (short_option) {
3587       case 'a': {
3588         m_type = eLookupTypeAddress;
3589         m_addr = OptionArgParser::ToAddress(execution_context, option_arg,
3590                                             LLDB_INVALID_ADDRESS, &error);
3591       } break;
3592 
3593       case 'o':
3594         if (option_arg.getAsInteger(0, m_offset))
3595           error.SetErrorStringWithFormat("invalid offset string '%s'",
3596                                          option_arg.str().c_str());
3597         break;
3598 
3599       case 's':
3600         m_str = std::string(option_arg);
3601         m_type = eLookupTypeSymbol;
3602         break;
3603 
3604       case 'f':
3605         m_file.SetFile(option_arg, FileSpec::Style::native);
3606         m_type = eLookupTypeFileLine;
3607         break;
3608 
3609       case 'i':
3610         m_include_inlines = false;
3611         break;
3612 
3613       case 'l':
3614         if (option_arg.getAsInteger(0, m_line_number))
3615           error.SetErrorStringWithFormat("invalid line number string '%s'",
3616                                          option_arg.str().c_str());
3617         else if (m_line_number == 0)
3618           error.SetErrorString("zero is an invalid line number");
3619         m_type = eLookupTypeFileLine;
3620         break;
3621 
3622       case 'F':
3623         m_str = std::string(option_arg);
3624         m_type = eLookupTypeFunction;
3625         break;
3626 
3627       case 'n':
3628         m_str = std::string(option_arg);
3629         m_type = eLookupTypeFunctionOrSymbol;
3630         break;
3631 
3632       case 't':
3633         m_str = std::string(option_arg);
3634         m_type = eLookupTypeType;
3635         break;
3636 
3637       case 'v':
3638         m_verbose = true;
3639         break;
3640 
3641       case 'A':
3642         m_print_all = true;
3643         break;
3644 
3645       case 'r':
3646         m_use_regex = true;
3647         break;
3648       default:
3649         llvm_unreachable("Unimplemented option");
3650       }
3651 
3652       return error;
3653     }
3654 
3655     void OptionParsingStarting(ExecutionContext *execution_context) override {
3656       m_type = eLookupTypeInvalid;
3657       m_str.clear();
3658       m_file.Clear();
3659       m_addr = LLDB_INVALID_ADDRESS;
3660       m_offset = 0;
3661       m_line_number = 0;
3662       m_use_regex = false;
3663       m_include_inlines = true;
3664       m_verbose = false;
3665       m_print_all = false;
3666     }
3667 
3668     llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
3669       return llvm::makeArrayRef(g_target_modules_lookup_options);
3670     }
3671 
3672     int m_type;        // Should be a eLookupTypeXXX enum after parsing options
3673     std::string m_str; // Holds name lookup
3674     FileSpec m_file;   // Files for file lookups
3675     lldb::addr_t m_addr; // Holds the address to lookup
3676     lldb::addr_t
3677         m_offset; // Subtract this offset from m_addr before doing lookups.
3678     uint32_t m_line_number; // Line number for file+line lookups
3679     bool m_use_regex;       // Name lookups in m_str are regular expressions.
3680     bool m_include_inlines; // Check for inline entries when looking up by
3681                             // file/line.
3682     bool m_verbose;         // Enable verbose lookup info
3683     bool m_print_all; // Print all matches, even in cases where there's a best
3684                       // match.
3685   };
3686 
3687   CommandObjectTargetModulesLookup(CommandInterpreter &interpreter)
3688       : CommandObjectParsed(interpreter, "target modules lookup",
3689                             "Look up information within executable and "
3690                             "dependent shared library images.",
3691                             nullptr, eCommandRequiresTarget),
3692         m_options() {
3693     CommandArgumentEntry arg;
3694     CommandArgumentData file_arg;
3695 
3696     // Define the first (and only) variant of this arg.
3697     file_arg.arg_type = eArgTypeFilename;
3698     file_arg.arg_repetition = eArgRepeatStar;
3699 
3700     // There is only one variant this argument could be; put it into the
3701     // argument entry.
3702     arg.push_back(file_arg);
3703 
3704     // Push the data for the first argument into the m_arguments vector.
3705     m_arguments.push_back(arg);
3706   }
3707 
3708   ~CommandObjectTargetModulesLookup() override = default;
3709 
3710   Options *GetOptions() override { return &m_options; }
3711 
3712   bool LookupHere(CommandInterpreter &interpreter, CommandReturnObject &result,
3713                   bool &syntax_error) {
3714     switch (m_options.m_type) {
3715     case eLookupTypeAddress:
3716     case eLookupTypeFileLine:
3717     case eLookupTypeFunction:
3718     case eLookupTypeFunctionOrSymbol:
3719     case eLookupTypeSymbol:
3720     default:
3721       return false;
3722     case eLookupTypeType:
3723       break;
3724     }
3725 
3726     StackFrameSP frame = m_exe_ctx.GetFrameSP();
3727 
3728     if (!frame)
3729       return false;
3730 
3731     const SymbolContext &sym_ctx(frame->GetSymbolContext(eSymbolContextModule));
3732 
3733     if (!sym_ctx.module_sp)
3734       return false;
3735 
3736     switch (m_options.m_type) {
3737     default:
3738       return false;
3739     case eLookupTypeType:
3740       if (!m_options.m_str.empty()) {
3741         if (LookupTypeHere(&GetSelectedTarget(), m_interpreter,
3742                            result.GetOutputStream(), *sym_ctx.module_sp,
3743                            m_options.m_str.c_str(), m_options.m_use_regex)) {
3744           result.SetStatus(eReturnStatusSuccessFinishResult);
3745           return true;
3746         }
3747       }
3748       break;
3749     }
3750 
3751     return false;
3752   }
3753 
3754   bool LookupInModule(CommandInterpreter &interpreter, Module *module,
3755                       CommandReturnObject &result, bool &syntax_error) {
3756     switch (m_options.m_type) {
3757     case eLookupTypeAddress:
3758       if (m_options.m_addr != LLDB_INVALID_ADDRESS) {
3759         if (LookupAddressInModule(
3760                 m_interpreter, result.GetOutputStream(), module,
3761                 eSymbolContextEverything |
3762                     (m_options.m_verbose
3763                          ? static_cast<int>(eSymbolContextVariable)
3764                          : 0),
3765                 m_options.m_addr, m_options.m_offset, m_options.m_verbose)) {
3766           result.SetStatus(eReturnStatusSuccessFinishResult);
3767           return true;
3768         }
3769       }
3770       break;
3771 
3772     case eLookupTypeSymbol:
3773       if (!m_options.m_str.empty()) {
3774         if (LookupSymbolInModule(m_interpreter, result.GetOutputStream(),
3775                                  module, m_options.m_str.c_str(),
3776                                  m_options.m_use_regex, m_options.m_verbose)) {
3777           result.SetStatus(eReturnStatusSuccessFinishResult);
3778           return true;
3779         }
3780       }
3781       break;
3782 
3783     case eLookupTypeFileLine:
3784       if (m_options.m_file) {
3785         if (LookupFileAndLineInModule(
3786                 m_interpreter, result.GetOutputStream(), module,
3787                 m_options.m_file, m_options.m_line_number,
3788                 m_options.m_include_inlines, m_options.m_verbose)) {
3789           result.SetStatus(eReturnStatusSuccessFinishResult);
3790           return true;
3791         }
3792       }
3793       break;
3794 
3795     case eLookupTypeFunctionOrSymbol:
3796     case eLookupTypeFunction:
3797       if (!m_options.m_str.empty()) {
3798         if (LookupFunctionInModule(
3799                 m_interpreter, result.GetOutputStream(), module,
3800                 m_options.m_str.c_str(), m_options.m_use_regex,
3801                 m_options.m_include_inlines,
3802                 m_options.m_type ==
3803                     eLookupTypeFunctionOrSymbol, // include symbols
3804                 m_options.m_verbose)) {
3805           result.SetStatus(eReturnStatusSuccessFinishResult);
3806           return true;
3807         }
3808       }
3809       break;
3810 
3811     case eLookupTypeType:
3812       if (!m_options.m_str.empty()) {
3813         if (LookupTypeInModule(
3814                 &GetSelectedTarget(), m_interpreter, result.GetOutputStream(),
3815                 module, m_options.m_str.c_str(), m_options.m_use_regex)) {
3816           result.SetStatus(eReturnStatusSuccessFinishResult);
3817           return true;
3818         }
3819       }
3820       break;
3821 
3822     default:
3823       m_options.GenerateOptionUsage(
3824           result.GetErrorStream(), this,
3825           GetCommandInterpreter().GetDebugger().GetTerminalWidth());
3826       syntax_error = true;
3827       break;
3828     }
3829 
3830     result.SetStatus(eReturnStatusFailed);
3831     return false;
3832   }
3833 
3834 protected:
3835   bool DoExecute(Args &command, CommandReturnObject &result) override {
3836     Target *target = &GetSelectedTarget();
3837     bool syntax_error = false;
3838     uint32_t i;
3839     uint32_t num_successful_lookups = 0;
3840     uint32_t addr_byte_size = target->GetArchitecture().GetAddressByteSize();
3841     result.GetOutputStream().SetAddressByteSize(addr_byte_size);
3842     result.GetErrorStream().SetAddressByteSize(addr_byte_size);
3843     // Dump all sections for all modules images
3844 
3845     if (command.GetArgumentCount() == 0) {
3846       ModuleSP current_module;
3847 
3848       // Where it is possible to look in the current symbol context first,
3849       // try that.  If this search was successful and --all was not passed,
3850       // don't print anything else.
3851       if (LookupHere(m_interpreter, result, syntax_error)) {
3852         result.GetOutputStream().EOL();
3853         num_successful_lookups++;
3854         if (!m_options.m_print_all) {
3855           result.SetStatus(eReturnStatusSuccessFinishResult);
3856           return result.Succeeded();
3857         }
3858       }
3859 
3860       // Dump all sections for all other modules
3861 
3862       const ModuleList &target_modules = target->GetImages();
3863       std::lock_guard<std::recursive_mutex> guard(target_modules.GetMutex());
3864       const size_t num_modules = target_modules.GetSize();
3865       if (num_modules > 0) {
3866         for (i = 0; i < num_modules && !syntax_error; ++i) {
3867           Module *module_pointer =
3868               target_modules.GetModulePointerAtIndexUnlocked(i);
3869 
3870           if (module_pointer != current_module.get() &&
3871               LookupInModule(m_interpreter,
3872                              target_modules.GetModulePointerAtIndexUnlocked(i),
3873                              result, syntax_error)) {
3874             result.GetOutputStream().EOL();
3875             num_successful_lookups++;
3876           }
3877         }
3878       } else {
3879         result.AppendError("the target has no associated executable images");
3880         result.SetStatus(eReturnStatusFailed);
3881         return false;
3882       }
3883     } else {
3884       // Dump specified images (by basename or fullpath)
3885       const char *arg_cstr;
3886       for (i = 0; (arg_cstr = command.GetArgumentAtIndex(i)) != nullptr &&
3887                   !syntax_error;
3888            ++i) {
3889         ModuleList module_list;
3890         const size_t num_matches =
3891             FindModulesByName(target, arg_cstr, module_list, false);
3892         if (num_matches > 0) {
3893           for (size_t j = 0; j < num_matches; ++j) {
3894             Module *module = module_list.GetModulePointerAtIndex(j);
3895             if (module) {
3896               if (LookupInModule(m_interpreter, module, result, syntax_error)) {
3897                 result.GetOutputStream().EOL();
3898                 num_successful_lookups++;
3899               }
3900             }
3901           }
3902         } else
3903           result.AppendWarningWithFormat(
3904               "Unable to find an image that matches '%s'.\n", arg_cstr);
3905       }
3906     }
3907 
3908     if (num_successful_lookups > 0)
3909       result.SetStatus(eReturnStatusSuccessFinishResult);
3910     else
3911       result.SetStatus(eReturnStatusFailed);
3912     return result.Succeeded();
3913   }
3914 
3915   CommandOptions m_options;
3916 };
3917 
3918 #pragma mark CommandObjectMultiwordImageSearchPaths
3919 
3920 // CommandObjectMultiwordImageSearchPaths
3921 
3922 class CommandObjectTargetModulesImageSearchPaths
3923     : public CommandObjectMultiword {
3924 public:
3925   CommandObjectTargetModulesImageSearchPaths(CommandInterpreter &interpreter)
3926       : CommandObjectMultiword(
3927             interpreter, "target modules search-paths",
3928             "Commands for managing module search paths for a target.",
3929             "target modules search-paths <subcommand> [<subcommand-options>]") {
3930     LoadSubCommand(
3931         "add", CommandObjectSP(
3932                    new CommandObjectTargetModulesSearchPathsAdd(interpreter)));
3933     LoadSubCommand(
3934         "clear", CommandObjectSP(new CommandObjectTargetModulesSearchPathsClear(
3935                      interpreter)));
3936     LoadSubCommand(
3937         "insert",
3938         CommandObjectSP(
3939             new CommandObjectTargetModulesSearchPathsInsert(interpreter)));
3940     LoadSubCommand(
3941         "list", CommandObjectSP(new CommandObjectTargetModulesSearchPathsList(
3942                     interpreter)));
3943     LoadSubCommand(
3944         "query", CommandObjectSP(new CommandObjectTargetModulesSearchPathsQuery(
3945                      interpreter)));
3946   }
3947 
3948   ~CommandObjectTargetModulesImageSearchPaths() override = default;
3949 };
3950 
3951 #pragma mark CommandObjectTargetModules
3952 
3953 // CommandObjectTargetModules
3954 
3955 class CommandObjectTargetModules : public CommandObjectMultiword {
3956 public:
3957   // Constructors and Destructors
3958   CommandObjectTargetModules(CommandInterpreter &interpreter)
3959       : CommandObjectMultiword(interpreter, "target modules",
3960                                "Commands for accessing information for one or "
3961                                "more target modules.",
3962                                "target modules <sub-command> ...") {
3963     LoadSubCommand(
3964         "add", CommandObjectSP(new CommandObjectTargetModulesAdd(interpreter)));
3965     LoadSubCommand("load", CommandObjectSP(new CommandObjectTargetModulesLoad(
3966                                interpreter)));
3967     LoadSubCommand("dump", CommandObjectSP(new CommandObjectTargetModulesDump(
3968                                interpreter)));
3969     LoadSubCommand("list", CommandObjectSP(new CommandObjectTargetModulesList(
3970                                interpreter)));
3971     LoadSubCommand(
3972         "lookup",
3973         CommandObjectSP(new CommandObjectTargetModulesLookup(interpreter)));
3974     LoadSubCommand(
3975         "search-paths",
3976         CommandObjectSP(
3977             new CommandObjectTargetModulesImageSearchPaths(interpreter)));
3978     LoadSubCommand(
3979         "show-unwind",
3980         CommandObjectSP(new CommandObjectTargetModulesShowUnwind(interpreter)));
3981   }
3982 
3983   ~CommandObjectTargetModules() override = default;
3984 
3985 private:
3986   // For CommandObjectTargetModules only
3987   CommandObjectTargetModules(const CommandObjectTargetModules &) = delete;
3988   const CommandObjectTargetModules &
3989   operator=(const CommandObjectTargetModules &) = delete;
3990 };
3991 
3992 class CommandObjectTargetSymbolsAdd : public CommandObjectParsed {
3993 public:
3994   CommandObjectTargetSymbolsAdd(CommandInterpreter &interpreter)
3995       : CommandObjectParsed(
3996             interpreter, "target symbols add",
3997             "Add a debug symbol file to one of the target's current modules by "
3998             "specifying a path to a debug symbols file or by using the options "
3999             "to specify a module.",
4000             "target symbols add <cmd-options> [<symfile>]",
4001             eCommandRequiresTarget),
4002         m_option_group(),
4003         m_file_option(
4004             LLDB_OPT_SET_1, false, "shlib", 's',
4005             CommandCompletions::eModuleCompletion, eArgTypeShlibName,
4006             "Locate the debug symbols for the shared library specified by "
4007             "name."),
4008         m_current_frame_option(
4009             LLDB_OPT_SET_2, false, "frame", 'F',
4010             "Locate the debug symbols for the currently selected frame.",
4011             false, true)
4012 
4013   {
4014     m_option_group.Append(&m_uuid_option_group, LLDB_OPT_SET_ALL,
4015                           LLDB_OPT_SET_1);
4016     m_option_group.Append(&m_file_option, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
4017     m_option_group.Append(&m_current_frame_option, LLDB_OPT_SET_2,
4018                           LLDB_OPT_SET_2);
4019     m_option_group.Finalize();
4020   }
4021 
4022   ~CommandObjectTargetSymbolsAdd() override = default;
4023 
4024   void
4025   HandleArgumentCompletion(CompletionRequest &request,
4026                            OptionElementVector &opt_element_vector) override {
4027     CommandCompletions::InvokeCommonCompletionCallbacks(
4028         GetCommandInterpreter(), CommandCompletions::eDiskFileCompletion,
4029         request, nullptr);
4030   }
4031 
4032   Options *GetOptions() override { return &m_option_group; }
4033 
4034 protected:
4035   bool AddModuleSymbols(Target *target, ModuleSpec &module_spec, bool &flush,
4036                         CommandReturnObject &result) {
4037     const FileSpec &symbol_fspec = module_spec.GetSymbolFileSpec();
4038     if (!symbol_fspec) {
4039       result.AppendError(
4040           "one or more executable image paths must be specified");
4041       result.SetStatus(eReturnStatusFailed);
4042       return false;
4043     }
4044 
4045     char symfile_path[PATH_MAX];
4046     symbol_fspec.GetPath(symfile_path, sizeof(symfile_path));
4047 
4048     if (!module_spec.GetUUID().IsValid()) {
4049       if (!module_spec.GetFileSpec() && !module_spec.GetPlatformFileSpec())
4050         module_spec.GetFileSpec().GetFilename() = symbol_fspec.GetFilename();
4051     }
4052 
4053     // Now module_spec represents a symbol file for a module that might exist
4054     // in the current target.  Let's find possible matches.
4055     ModuleList matching_modules;
4056 
4057     // First extract all module specs from the symbol file
4058     lldb_private::ModuleSpecList symfile_module_specs;
4059     if (ObjectFile::GetModuleSpecifications(module_spec.GetSymbolFileSpec(),
4060                                             0, 0, symfile_module_specs)) {
4061       // Now extract the module spec that matches the target architecture
4062       ModuleSpec target_arch_module_spec;
4063       ModuleSpec symfile_module_spec;
4064       target_arch_module_spec.GetArchitecture() = target->GetArchitecture();
4065       if (symfile_module_specs.FindMatchingModuleSpec(target_arch_module_spec,
4066                                                       symfile_module_spec)) {
4067         if (symfile_module_spec.GetUUID().IsValid()) {
4068           // It has a UUID, look for this UUID in the target modules
4069           ModuleSpec symfile_uuid_module_spec;
4070           symfile_uuid_module_spec.GetUUID() = symfile_module_spec.GetUUID();
4071           target->GetImages().FindModules(symfile_uuid_module_spec,
4072                                           matching_modules);
4073         }
4074       }
4075 
4076       if (matching_modules.IsEmpty()) {
4077         // No matches yet.  Iterate through the module specs to find a UUID
4078         // value that we can match up to an image in our target.
4079         const size_t num_symfile_module_specs = symfile_module_specs.GetSize();
4080         for (size_t i = 0;
4081              i < num_symfile_module_specs && matching_modules.IsEmpty(); ++i) {
4082           if (symfile_module_specs.GetModuleSpecAtIndex(
4083                   i, symfile_module_spec)) {
4084             if (symfile_module_spec.GetUUID().IsValid()) {
4085               // It has a UUID.  Look for this UUID in the target modules.
4086               ModuleSpec symfile_uuid_module_spec;
4087               symfile_uuid_module_spec.GetUUID() =
4088                   symfile_module_spec.GetUUID();
4089               target->GetImages().FindModules(symfile_uuid_module_spec,
4090                                               matching_modules);
4091             }
4092           }
4093         }
4094       }
4095     }
4096 
4097     // Just try to match up the file by basename if we have no matches at
4098     // this point.  For example, module foo might have symbols in foo.debug.
4099     if (matching_modules.IsEmpty())
4100       target->GetImages().FindModules(module_spec, matching_modules);
4101 
4102     while (matching_modules.IsEmpty()) {
4103       ConstString filename_no_extension(
4104           module_spec.GetFileSpec().GetFileNameStrippingExtension());
4105       // Empty string returned, let's bail
4106       if (!filename_no_extension)
4107         break;
4108 
4109       // Check if there was no extension to strip and the basename is the same
4110       if (filename_no_extension == module_spec.GetFileSpec().GetFilename())
4111         break;
4112 
4113       // Replace basename with one fewer extension
4114       module_spec.GetFileSpec().GetFilename() = filename_no_extension;
4115       target->GetImages().FindModules(module_spec, matching_modules);
4116     }
4117 
4118     if (matching_modules.GetSize() > 1) {
4119       result.AppendErrorWithFormat("multiple modules match symbol file '%s', "
4120                                    "use the --uuid option to resolve the "
4121                                    "ambiguity.\n",
4122                                    symfile_path);
4123       result.SetStatus(eReturnStatusFailed);
4124       return false;
4125     }
4126 
4127     if (matching_modules.GetSize() == 1) {
4128       ModuleSP module_sp(matching_modules.GetModuleAtIndex(0));
4129 
4130       // The module has not yet created its symbol vendor, we can just give
4131       // the existing target module the symfile path to use for when it
4132       // decides to create it!
4133       module_sp->SetSymbolFileFileSpec(symbol_fspec);
4134 
4135       SymbolFile *symbol_file =
4136           module_sp->GetSymbolFile(true, &result.GetErrorStream());
4137       if (symbol_file) {
4138         ObjectFile *object_file = symbol_file->GetObjectFile();
4139         if (object_file && object_file->GetFileSpec() == symbol_fspec) {
4140           // Provide feedback that the symfile has been successfully added.
4141           const FileSpec &module_fs = module_sp->GetFileSpec();
4142           result.AppendMessageWithFormat(
4143               "symbol file '%s' has been added to '%s'\n", symfile_path,
4144               module_fs.GetPath().c_str());
4145 
4146           // Let clients know something changed in the module if it is
4147           // currently loaded
4148           ModuleList module_list;
4149           module_list.Append(module_sp);
4150           target->SymbolsDidLoad(module_list);
4151 
4152           // Make sure we load any scripting resources that may be embedded
4153           // in the debug info files in case the platform supports that.
4154           Status error;
4155           StreamString feedback_stream;
4156           module_sp->LoadScriptingResourceInTarget(target, error,
4157                                                    &feedback_stream);
4158           if (error.Fail() && error.AsCString())
4159             result.AppendWarningWithFormat(
4160                 "unable to load scripting data for module %s - error "
4161                 "reported was %s",
4162                 module_sp->GetFileSpec()
4163                     .GetFileNameStrippingExtension()
4164                     .GetCString(),
4165                 error.AsCString());
4166           else if (feedback_stream.GetSize())
4167             result.AppendWarning(feedback_stream.GetData());
4168 
4169           flush = true;
4170           result.SetStatus(eReturnStatusSuccessFinishResult);
4171           return true;
4172         }
4173       }
4174       // Clear the symbol file spec if anything went wrong
4175       module_sp->SetSymbolFileFileSpec(FileSpec());
4176     }
4177 
4178     StreamString ss_symfile_uuid;
4179     if (module_spec.GetUUID().IsValid()) {
4180       ss_symfile_uuid << " (";
4181       module_spec.GetUUID().Dump(&ss_symfile_uuid);
4182       ss_symfile_uuid << ')';
4183     }
4184     result.AppendErrorWithFormat(
4185         "symbol file '%s'%s does not match any existing module%s\n",
4186         symfile_path, ss_symfile_uuid.GetData(),
4187         !llvm::sys::fs::is_regular_file(symbol_fspec.GetPath())
4188             ? "\n       please specify the full path to the symbol file"
4189             : "");
4190     result.SetStatus(eReturnStatusFailed);
4191     return false;
4192   }
4193 
4194   bool DoExecute(Args &args, CommandReturnObject &result) override {
4195     Target *target = m_exe_ctx.GetTargetPtr();
4196     result.SetStatus(eReturnStatusFailed);
4197     bool flush = false;
4198     ModuleSpec module_spec;
4199     const bool uuid_option_set =
4200         m_uuid_option_group.GetOptionValue().OptionWasSet();
4201     const bool file_option_set = m_file_option.GetOptionValue().OptionWasSet();
4202     const bool frame_option_set =
4203         m_current_frame_option.GetOptionValue().OptionWasSet();
4204     const size_t argc = args.GetArgumentCount();
4205 
4206     if (argc == 0) {
4207       if (uuid_option_set || file_option_set || frame_option_set) {
4208         bool success = false;
4209         bool error_set = false;
4210         if (frame_option_set) {
4211           Process *process = m_exe_ctx.GetProcessPtr();
4212           if (process) {
4213             const StateType process_state = process->GetState();
4214             if (StateIsStoppedState(process_state, true)) {
4215               StackFrame *frame = m_exe_ctx.GetFramePtr();
4216               if (frame) {
4217                 ModuleSP frame_module_sp(
4218                     frame->GetSymbolContext(eSymbolContextModule).module_sp);
4219                 if (frame_module_sp) {
4220                   if (FileSystem::Instance().Exists(
4221                           frame_module_sp->GetPlatformFileSpec())) {
4222                     module_spec.GetArchitecture() =
4223                         frame_module_sp->GetArchitecture();
4224                     module_spec.GetFileSpec() =
4225                         frame_module_sp->GetPlatformFileSpec();
4226                   }
4227                   module_spec.GetUUID() = frame_module_sp->GetUUID();
4228                   success = module_spec.GetUUID().IsValid() ||
4229                             module_spec.GetFileSpec();
4230                 } else {
4231                   result.AppendError("frame has no module");
4232                   error_set = true;
4233                 }
4234               } else {
4235                 result.AppendError("invalid current frame");
4236                 error_set = true;
4237               }
4238             } else {
4239               result.AppendErrorWithFormat("process is not stopped: %s",
4240                                            StateAsCString(process_state));
4241               error_set = true;
4242             }
4243           } else {
4244             result.AppendError(
4245                 "a process must exist in order to use the --frame option");
4246             error_set = true;
4247           }
4248         } else {
4249           if (uuid_option_set) {
4250             module_spec.GetUUID() =
4251                 m_uuid_option_group.GetOptionValue().GetCurrentValue();
4252             success |= module_spec.GetUUID().IsValid();
4253           } else if (file_option_set) {
4254             module_spec.GetFileSpec() =
4255                 m_file_option.GetOptionValue().GetCurrentValue();
4256             ModuleSP module_sp(
4257                 target->GetImages().FindFirstModule(module_spec));
4258             if (module_sp) {
4259               module_spec.GetFileSpec() = module_sp->GetFileSpec();
4260               module_spec.GetPlatformFileSpec() =
4261                   module_sp->GetPlatformFileSpec();
4262               module_spec.GetUUID() = module_sp->GetUUID();
4263               module_spec.GetArchitecture() = module_sp->GetArchitecture();
4264             } else {
4265               module_spec.GetArchitecture() = target->GetArchitecture();
4266             }
4267             success |= module_spec.GetUUID().IsValid() ||
4268                        FileSystem::Instance().Exists(module_spec.GetFileSpec());
4269           }
4270         }
4271 
4272         if (success) {
4273           if (Symbols::DownloadObjectAndSymbolFile(module_spec)) {
4274             if (module_spec.GetSymbolFileSpec())
4275               success = AddModuleSymbols(target, module_spec, flush, result);
4276           }
4277         }
4278 
4279         if (!success && !error_set) {
4280           StreamString error_strm;
4281           if (uuid_option_set) {
4282             error_strm.PutCString("unable to find debug symbols for UUID ");
4283             module_spec.GetUUID().Dump(&error_strm);
4284           } else if (file_option_set) {
4285             error_strm.PutCString(
4286                 "unable to find debug symbols for the executable file ");
4287             error_strm << module_spec.GetFileSpec();
4288           } else if (frame_option_set) {
4289             error_strm.PutCString(
4290                 "unable to find debug symbols for the current frame");
4291           }
4292           result.AppendError(error_strm.GetString());
4293         }
4294       } else {
4295         result.AppendError("one or more symbol file paths must be specified, "
4296                            "or options must be specified");
4297       }
4298     } else {
4299       if (uuid_option_set) {
4300         result.AppendError("specify either one or more paths to symbol files "
4301                            "or use the --uuid option without arguments");
4302       } else if (frame_option_set) {
4303         result.AppendError("specify either one or more paths to symbol files "
4304                            "or use the --frame option without arguments");
4305       } else if (file_option_set && argc > 1) {
4306         result.AppendError("specify at most one symbol file path when "
4307                            "--shlib option is set");
4308       } else {
4309         PlatformSP platform_sp(target->GetPlatform());
4310 
4311         for (auto &entry : args.entries()) {
4312           if (!entry.ref().empty()) {
4313             auto &symbol_file_spec = module_spec.GetSymbolFileSpec();
4314             symbol_file_spec.SetFile(entry.ref(), FileSpec::Style::native);
4315             FileSystem::Instance().Resolve(symbol_file_spec);
4316             if (file_option_set) {
4317               module_spec.GetFileSpec() =
4318                   m_file_option.GetOptionValue().GetCurrentValue();
4319             }
4320             if (platform_sp) {
4321               FileSpec symfile_spec;
4322               if (platform_sp
4323                       ->ResolveSymbolFile(*target, module_spec, symfile_spec)
4324                       .Success())
4325                 module_spec.GetSymbolFileSpec() = symfile_spec;
4326             }
4327 
4328             bool symfile_exists =
4329                 FileSystem::Instance().Exists(module_spec.GetSymbolFileSpec());
4330 
4331             if (symfile_exists) {
4332               if (!AddModuleSymbols(target, module_spec, flush, result))
4333                 break;
4334             } else {
4335               std::string resolved_symfile_path =
4336                   module_spec.GetSymbolFileSpec().GetPath();
4337               if (resolved_symfile_path != entry.ref()) {
4338                 result.AppendErrorWithFormat(
4339                     "invalid module path '%s' with resolved path '%s'\n",
4340                     entry.c_str(), resolved_symfile_path.c_str());
4341                 break;
4342               }
4343               result.AppendErrorWithFormat("invalid module path '%s'\n",
4344                                            entry.c_str());
4345               break;
4346             }
4347           }
4348         }
4349       }
4350     }
4351 
4352     if (flush) {
4353       Process *process = m_exe_ctx.GetProcessPtr();
4354       if (process)
4355         process->Flush();
4356     }
4357     return result.Succeeded();
4358   }
4359 
4360   OptionGroupOptions m_option_group;
4361   OptionGroupUUID m_uuid_option_group;
4362   OptionGroupFile m_file_option;
4363   OptionGroupBoolean m_current_frame_option;
4364 };
4365 
4366 #pragma mark CommandObjectTargetSymbols
4367 
4368 // CommandObjectTargetSymbols
4369 
4370 class CommandObjectTargetSymbols : public CommandObjectMultiword {
4371 public:
4372   // Constructors and Destructors
4373   CommandObjectTargetSymbols(CommandInterpreter &interpreter)
4374       : CommandObjectMultiword(
4375             interpreter, "target symbols",
4376             "Commands for adding and managing debug symbol files.",
4377             "target symbols <sub-command> ...") {
4378     LoadSubCommand(
4379         "add", CommandObjectSP(new CommandObjectTargetSymbolsAdd(interpreter)));
4380   }
4381 
4382   ~CommandObjectTargetSymbols() override = default;
4383 
4384 private:
4385   // For CommandObjectTargetModules only
4386   CommandObjectTargetSymbols(const CommandObjectTargetSymbols &) = delete;
4387   const CommandObjectTargetSymbols &
4388   operator=(const CommandObjectTargetSymbols &) = delete;
4389 };
4390 
4391 #pragma mark CommandObjectTargetStopHookAdd
4392 
4393 // CommandObjectTargetStopHookAdd
4394 #define LLDB_OPTIONS_target_stop_hook_add
4395 #include "CommandOptions.inc"
4396 
4397 class CommandObjectTargetStopHookAdd : public CommandObjectParsed,
4398                                        public IOHandlerDelegateMultiline {
4399 public:
4400   class CommandOptions : public Options {
4401   public:
4402     CommandOptions()
4403         : Options(), m_line_start(0), m_line_end(UINT_MAX),
4404           m_func_name_type_mask(eFunctionNameTypeAuto),
4405           m_sym_ctx_specified(false), m_thread_specified(false),
4406           m_use_one_liner(false), m_one_liner() {}
4407 
4408     ~CommandOptions() override = default;
4409 
4410     llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
4411       return llvm::makeArrayRef(g_target_stop_hook_add_options);
4412     }
4413 
4414     Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
4415                           ExecutionContext *execution_context) override {
4416       Status error;
4417       const int short_option = m_getopt_table[option_idx].val;
4418 
4419       switch (short_option) {
4420       case 'c':
4421         m_class_name = std::string(option_arg);
4422         m_sym_ctx_specified = true;
4423         break;
4424 
4425       case 'e':
4426         if (option_arg.getAsInteger(0, m_line_end)) {
4427           error.SetErrorStringWithFormat("invalid end line number: \"%s\"",
4428                                          option_arg.str().c_str());
4429           break;
4430         }
4431         m_sym_ctx_specified = true;
4432         break;
4433 
4434       case 'G': {
4435         bool value, success;
4436         value = OptionArgParser::ToBoolean(option_arg, false, &success);
4437         if (success) {
4438           m_auto_continue = value;
4439         } else
4440           error.SetErrorStringWithFormat(
4441               "invalid boolean value '%s' passed for -G option",
4442               option_arg.str().c_str());
4443       } break;
4444       case 'l':
4445         if (option_arg.getAsInteger(0, m_line_start)) {
4446           error.SetErrorStringWithFormat("invalid start line number: \"%s\"",
4447                                          option_arg.str().c_str());
4448           break;
4449         }
4450         m_sym_ctx_specified = true;
4451         break;
4452 
4453       case 'i':
4454         m_no_inlines = true;
4455         break;
4456 
4457       case 'n':
4458         m_function_name = std::string(option_arg);
4459         m_func_name_type_mask |= eFunctionNameTypeAuto;
4460         m_sym_ctx_specified = true;
4461         break;
4462 
4463       case 'f':
4464         m_file_name = std::string(option_arg);
4465         m_sym_ctx_specified = true;
4466         break;
4467 
4468       case 's':
4469         m_module_name = std::string(option_arg);
4470         m_sym_ctx_specified = true;
4471         break;
4472 
4473       case 't':
4474         if (option_arg.getAsInteger(0, m_thread_id))
4475           error.SetErrorStringWithFormat("invalid thread id string '%s'",
4476                                          option_arg.str().c_str());
4477         m_thread_specified = true;
4478         break;
4479 
4480       case 'T':
4481         m_thread_name = std::string(option_arg);
4482         m_thread_specified = true;
4483         break;
4484 
4485       case 'q':
4486         m_queue_name = std::string(option_arg);
4487         m_thread_specified = true;
4488         break;
4489 
4490       case 'x':
4491         if (option_arg.getAsInteger(0, m_thread_index))
4492           error.SetErrorStringWithFormat("invalid thread index string '%s'",
4493                                          option_arg.str().c_str());
4494         m_thread_specified = true;
4495         break;
4496 
4497       case 'o':
4498         m_use_one_liner = true;
4499         m_one_liner.push_back(std::string(option_arg));
4500         break;
4501 
4502       default:
4503         llvm_unreachable("Unimplemented option");
4504       }
4505       return error;
4506     }
4507 
4508     void OptionParsingStarting(ExecutionContext *execution_context) override {
4509       m_class_name.clear();
4510       m_function_name.clear();
4511       m_line_start = 0;
4512       m_line_end = UINT_MAX;
4513       m_file_name.clear();
4514       m_module_name.clear();
4515       m_func_name_type_mask = eFunctionNameTypeAuto;
4516       m_thread_id = LLDB_INVALID_THREAD_ID;
4517       m_thread_index = UINT32_MAX;
4518       m_thread_name.clear();
4519       m_queue_name.clear();
4520 
4521       m_no_inlines = false;
4522       m_sym_ctx_specified = false;
4523       m_thread_specified = false;
4524 
4525       m_use_one_liner = false;
4526       m_one_liner.clear();
4527       m_auto_continue = false;
4528     }
4529 
4530     std::string m_class_name;
4531     std::string m_function_name;
4532     uint32_t m_line_start;
4533     uint32_t m_line_end;
4534     std::string m_file_name;
4535     std::string m_module_name;
4536     uint32_t m_func_name_type_mask; // A pick from lldb::FunctionNameType.
4537     lldb::tid_t m_thread_id;
4538     uint32_t m_thread_index;
4539     std::string m_thread_name;
4540     std::string m_queue_name;
4541     bool m_sym_ctx_specified;
4542     bool m_no_inlines;
4543     bool m_thread_specified;
4544     // Instance variables to hold the values for one_liner options.
4545     bool m_use_one_liner;
4546     std::vector<std::string> m_one_liner;
4547     bool m_auto_continue;
4548   };
4549 
4550   CommandObjectTargetStopHookAdd(CommandInterpreter &interpreter)
4551       : CommandObjectParsed(interpreter, "target stop-hook add",
4552                             "Add a hook to be executed when the target stops.",
4553                             "target stop-hook add"),
4554         IOHandlerDelegateMultiline("DONE",
4555                                    IOHandlerDelegate::Completion::LLDBCommand),
4556         m_options() {}
4557 
4558   ~CommandObjectTargetStopHookAdd() override = default;
4559 
4560   Options *GetOptions() override { return &m_options; }
4561 
4562 protected:
4563   void IOHandlerActivated(IOHandler &io_handler, bool interactive) override {
4564     StreamFileSP output_sp(io_handler.GetOutputStreamFileSP());
4565     if (output_sp && interactive) {
4566       output_sp->PutCString(
4567           "Enter your stop hook command(s).  Type 'DONE' to end.\n");
4568       output_sp->Flush();
4569     }
4570   }
4571 
4572   void IOHandlerInputComplete(IOHandler &io_handler,
4573                               std::string &line) override {
4574     if (m_stop_hook_sp) {
4575       if (line.empty()) {
4576         StreamFileSP error_sp(io_handler.GetErrorStreamFileSP());
4577         if (error_sp) {
4578           error_sp->Printf("error: stop hook #%" PRIu64
4579                            " aborted, no commands.\n",
4580                            m_stop_hook_sp->GetID());
4581           error_sp->Flush();
4582         }
4583         Target *target = GetDebugger().GetSelectedTarget().get();
4584         if (target)
4585           target->RemoveStopHookByID(m_stop_hook_sp->GetID());
4586       } else {
4587         m_stop_hook_sp->GetCommandPointer()->SplitIntoLines(line);
4588         StreamFileSP output_sp(io_handler.GetOutputStreamFileSP());
4589         if (output_sp) {
4590           output_sp->Printf("Stop hook #%" PRIu64 " added.\n",
4591                             m_stop_hook_sp->GetID());
4592           output_sp->Flush();
4593         }
4594       }
4595       m_stop_hook_sp.reset();
4596     }
4597     io_handler.SetIsDone(true);
4598   }
4599 
4600   bool DoExecute(Args &command, CommandReturnObject &result) override {
4601     m_stop_hook_sp.reset();
4602 
4603     Target &target = GetSelectedOrDummyTarget();
4604     Target::StopHookSP new_hook_sp = target.CreateStopHook();
4605 
4606     //  First step, make the specifier.
4607     std::unique_ptr<SymbolContextSpecifier> specifier_up;
4608     if (m_options.m_sym_ctx_specified) {
4609       specifier_up = std::make_unique<SymbolContextSpecifier>(
4610           GetDebugger().GetSelectedTarget());
4611 
4612       if (!m_options.m_module_name.empty()) {
4613         specifier_up->AddSpecification(
4614             m_options.m_module_name.c_str(),
4615             SymbolContextSpecifier::eModuleSpecified);
4616       }
4617 
4618       if (!m_options.m_class_name.empty()) {
4619         specifier_up->AddSpecification(
4620             m_options.m_class_name.c_str(),
4621             SymbolContextSpecifier::eClassOrNamespaceSpecified);
4622       }
4623 
4624       if (!m_options.m_file_name.empty()) {
4625         specifier_up->AddSpecification(m_options.m_file_name.c_str(),
4626                                        SymbolContextSpecifier::eFileSpecified);
4627       }
4628 
4629       if (m_options.m_line_start != 0) {
4630         specifier_up->AddLineSpecification(
4631             m_options.m_line_start,
4632             SymbolContextSpecifier::eLineStartSpecified);
4633       }
4634 
4635       if (m_options.m_line_end != UINT_MAX) {
4636         specifier_up->AddLineSpecification(
4637             m_options.m_line_end, SymbolContextSpecifier::eLineEndSpecified);
4638       }
4639 
4640       if (!m_options.m_function_name.empty()) {
4641         specifier_up->AddSpecification(
4642             m_options.m_function_name.c_str(),
4643             SymbolContextSpecifier::eFunctionSpecified);
4644       }
4645     }
4646 
4647     if (specifier_up)
4648       new_hook_sp->SetSpecifier(specifier_up.release());
4649 
4650     // Next see if any of the thread options have been entered:
4651 
4652     if (m_options.m_thread_specified) {
4653       ThreadSpec *thread_spec = new ThreadSpec();
4654 
4655       if (m_options.m_thread_id != LLDB_INVALID_THREAD_ID) {
4656         thread_spec->SetTID(m_options.m_thread_id);
4657       }
4658 
4659       if (m_options.m_thread_index != UINT32_MAX)
4660         thread_spec->SetIndex(m_options.m_thread_index);
4661 
4662       if (!m_options.m_thread_name.empty())
4663         thread_spec->SetName(m_options.m_thread_name.c_str());
4664 
4665       if (!m_options.m_queue_name.empty())
4666         thread_spec->SetQueueName(m_options.m_queue_name.c_str());
4667 
4668       new_hook_sp->SetThreadSpecifier(thread_spec);
4669     }
4670 
4671     new_hook_sp->SetAutoContinue(m_options.m_auto_continue);
4672     if (m_options.m_use_one_liner) {
4673       // Use one-liners.
4674       for (auto cmd : m_options.m_one_liner)
4675         new_hook_sp->GetCommandPointer()->AppendString(cmd.c_str());
4676       result.AppendMessageWithFormat("Stop hook #%" PRIu64 " added.\n",
4677                                      new_hook_sp->GetID());
4678     } else {
4679       m_stop_hook_sp = new_hook_sp;
4680       m_interpreter.GetLLDBCommandsFromIOHandler("> ",   // Prompt
4681                                                  *this); // IOHandlerDelegate
4682     }
4683     result.SetStatus(eReturnStatusSuccessFinishNoResult);
4684 
4685     return result.Succeeded();
4686   }
4687 
4688 private:
4689   CommandOptions m_options;
4690   Target::StopHookSP m_stop_hook_sp;
4691 };
4692 
4693 #pragma mark CommandObjectTargetStopHookDelete
4694 
4695 // CommandObjectTargetStopHookDelete
4696 
4697 class CommandObjectTargetStopHookDelete : public CommandObjectParsed {
4698 public:
4699   CommandObjectTargetStopHookDelete(CommandInterpreter &interpreter)
4700       : CommandObjectParsed(interpreter, "target stop-hook delete",
4701                             "Delete a stop-hook.",
4702                             "target stop-hook delete [<idx>]") {}
4703 
4704   ~CommandObjectTargetStopHookDelete() override = default;
4705 
4706 protected:
4707   bool DoExecute(Args &command, CommandReturnObject &result) override {
4708     Target &target = GetSelectedOrDummyTarget();
4709     // FIXME: see if we can use the breakpoint id style parser?
4710     size_t num_args = command.GetArgumentCount();
4711     if (num_args == 0) {
4712       if (!m_interpreter.Confirm("Delete all stop hooks?", true)) {
4713         result.SetStatus(eReturnStatusFailed);
4714         return false;
4715       } else {
4716         target.RemoveAllStopHooks();
4717       }
4718     } else {
4719       for (size_t i = 0; i < num_args; i++) {
4720         lldb::user_id_t user_id;
4721         if (!llvm::to_integer(command.GetArgumentAtIndex(i), user_id)) {
4722           result.AppendErrorWithFormat("invalid stop hook id: \"%s\".\n",
4723                                        command.GetArgumentAtIndex(i));
4724           result.SetStatus(eReturnStatusFailed);
4725           return false;
4726         }
4727         if (!target.RemoveStopHookByID(user_id)) {
4728           result.AppendErrorWithFormat("unknown stop hook id: \"%s\".\n",
4729                                        command.GetArgumentAtIndex(i));
4730           result.SetStatus(eReturnStatusFailed);
4731           return false;
4732         }
4733       }
4734     }
4735     result.SetStatus(eReturnStatusSuccessFinishNoResult);
4736     return result.Succeeded();
4737   }
4738 };
4739 
4740 #pragma mark CommandObjectTargetStopHookEnableDisable
4741 
4742 // CommandObjectTargetStopHookEnableDisable
4743 
4744 class CommandObjectTargetStopHookEnableDisable : public CommandObjectParsed {
4745 public:
4746   CommandObjectTargetStopHookEnableDisable(CommandInterpreter &interpreter,
4747                                            bool enable, const char *name,
4748                                            const char *help, const char *syntax)
4749       : CommandObjectParsed(interpreter, name, help, syntax), m_enable(enable) {
4750   }
4751 
4752   ~CommandObjectTargetStopHookEnableDisable() override = default;
4753 
4754 protected:
4755   bool DoExecute(Args &command, CommandReturnObject &result) override {
4756     Target &target = GetSelectedOrDummyTarget();
4757     // FIXME: see if we can use the breakpoint id style parser?
4758     size_t num_args = command.GetArgumentCount();
4759     bool success;
4760 
4761     if (num_args == 0) {
4762       target.SetAllStopHooksActiveState(m_enable);
4763     } else {
4764       for (size_t i = 0; i < num_args; i++) {
4765         lldb::user_id_t user_id;
4766         if (!llvm::to_integer(command.GetArgumentAtIndex(i), user_id)) {
4767           result.AppendErrorWithFormat("invalid stop hook id: \"%s\".\n",
4768                                        command.GetArgumentAtIndex(i));
4769           result.SetStatus(eReturnStatusFailed);
4770           return false;
4771         }
4772         success = target.SetStopHookActiveStateByID(user_id, m_enable);
4773         if (!success) {
4774           result.AppendErrorWithFormat("unknown stop hook id: \"%s\".\n",
4775                                        command.GetArgumentAtIndex(i));
4776           result.SetStatus(eReturnStatusFailed);
4777           return false;
4778         }
4779       }
4780     }
4781     result.SetStatus(eReturnStatusSuccessFinishNoResult);
4782     return result.Succeeded();
4783   }
4784 
4785 private:
4786   bool m_enable;
4787 };
4788 
4789 #pragma mark CommandObjectTargetStopHookList
4790 
4791 // CommandObjectTargetStopHookList
4792 
4793 class CommandObjectTargetStopHookList : public CommandObjectParsed {
4794 public:
4795   CommandObjectTargetStopHookList(CommandInterpreter &interpreter)
4796       : CommandObjectParsed(interpreter, "target stop-hook list",
4797                             "List all stop-hooks.",
4798                             "target stop-hook list [<type>]") {}
4799 
4800   ~CommandObjectTargetStopHookList() override = default;
4801 
4802 protected:
4803   bool DoExecute(Args &command, CommandReturnObject &result) override {
4804     Target &target = GetSelectedOrDummyTarget();
4805 
4806     size_t num_hooks = target.GetNumStopHooks();
4807     if (num_hooks == 0) {
4808       result.GetOutputStream().PutCString("No stop hooks.\n");
4809     } else {
4810       for (size_t i = 0; i < num_hooks; i++) {
4811         Target::StopHookSP this_hook = target.GetStopHookAtIndex(i);
4812         if (i > 0)
4813           result.GetOutputStream().PutCString("\n");
4814         this_hook->GetDescription(&(result.GetOutputStream()),
4815                                   eDescriptionLevelFull);
4816       }
4817     }
4818     result.SetStatus(eReturnStatusSuccessFinishResult);
4819     return result.Succeeded();
4820   }
4821 };
4822 
4823 #pragma mark CommandObjectMultiwordTargetStopHooks
4824 
4825 // CommandObjectMultiwordTargetStopHooks
4826 
4827 class CommandObjectMultiwordTargetStopHooks : public CommandObjectMultiword {
4828 public:
4829   CommandObjectMultiwordTargetStopHooks(CommandInterpreter &interpreter)
4830       : CommandObjectMultiword(
4831             interpreter, "target stop-hook",
4832             "Commands for operating on debugger target stop-hooks.",
4833             "target stop-hook <subcommand> [<subcommand-options>]") {
4834     LoadSubCommand("add", CommandObjectSP(
4835                               new CommandObjectTargetStopHookAdd(interpreter)));
4836     LoadSubCommand(
4837         "delete",
4838         CommandObjectSP(new CommandObjectTargetStopHookDelete(interpreter)));
4839     LoadSubCommand("disable",
4840                    CommandObjectSP(new CommandObjectTargetStopHookEnableDisable(
4841                        interpreter, false, "target stop-hook disable [<id>]",
4842                        "Disable a stop-hook.", "target stop-hook disable")));
4843     LoadSubCommand("enable",
4844                    CommandObjectSP(new CommandObjectTargetStopHookEnableDisable(
4845                        interpreter, true, "target stop-hook enable [<id>]",
4846                        "Enable a stop-hook.", "target stop-hook enable")));
4847     LoadSubCommand("list", CommandObjectSP(new CommandObjectTargetStopHookList(
4848                                interpreter)));
4849   }
4850 
4851   ~CommandObjectMultiwordTargetStopHooks() override = default;
4852 };
4853 
4854 #pragma mark CommandObjectMultiwordTarget
4855 
4856 // CommandObjectMultiwordTarget
4857 
4858 CommandObjectMultiwordTarget::CommandObjectMultiwordTarget(
4859     CommandInterpreter &interpreter)
4860     : CommandObjectMultiword(interpreter, "target",
4861                              "Commands for operating on debugger targets.",
4862                              "target <subcommand> [<subcommand-options>]") {
4863   LoadSubCommand("create",
4864                  CommandObjectSP(new CommandObjectTargetCreate(interpreter)));
4865   LoadSubCommand("delete",
4866                  CommandObjectSP(new CommandObjectTargetDelete(interpreter)));
4867   LoadSubCommand("list",
4868                  CommandObjectSP(new CommandObjectTargetList(interpreter)));
4869   LoadSubCommand("select",
4870                  CommandObjectSP(new CommandObjectTargetSelect(interpreter)));
4871   LoadSubCommand("show-launch-environment",
4872                  CommandObjectSP(new CommandObjectTargetShowLaunchEnvironment(
4873                      interpreter)));
4874   LoadSubCommand(
4875       "stop-hook",
4876       CommandObjectSP(new CommandObjectMultiwordTargetStopHooks(interpreter)));
4877   LoadSubCommand("modules",
4878                  CommandObjectSP(new CommandObjectTargetModules(interpreter)));
4879   LoadSubCommand("symbols",
4880                  CommandObjectSP(new CommandObjectTargetSymbols(interpreter)));
4881   LoadSubCommand("variable",
4882                  CommandObjectSP(new CommandObjectTargetVariable(interpreter)));
4883 }
4884 
4885 CommandObjectMultiwordTarget::~CommandObjectMultiwordTarget() = default;
4886