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