1 //===-- CommandInterpreter.cpp --------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include <cstdlib>
10 #include <limits>
11 #include <memory>
12 #include <string>
13 #include <vector>
14 
15 #include "Commands/CommandObjectApropos.h"
16 #include "Commands/CommandObjectBreakpoint.h"
17 #include "Commands/CommandObjectCommands.h"
18 #include "Commands/CommandObjectDisassemble.h"
19 #include "Commands/CommandObjectExpression.h"
20 #include "Commands/CommandObjectFrame.h"
21 #include "Commands/CommandObjectGUI.h"
22 #include "Commands/CommandObjectHelp.h"
23 #include "Commands/CommandObjectLanguage.h"
24 #include "Commands/CommandObjectLog.h"
25 #include "Commands/CommandObjectMemory.h"
26 #include "Commands/CommandObjectPlatform.h"
27 #include "Commands/CommandObjectPlugin.h"
28 #include "Commands/CommandObjectProcess.h"
29 #include "Commands/CommandObjectQuit.h"
30 #include "Commands/CommandObjectRegexCommand.h"
31 #include "Commands/CommandObjectRegister.h"
32 #include "Commands/CommandObjectReproducer.h"
33 #include "Commands/CommandObjectScript.h"
34 #include "Commands/CommandObjectSession.h"
35 #include "Commands/CommandObjectSettings.h"
36 #include "Commands/CommandObjectSource.h"
37 #include "Commands/CommandObjectStats.h"
38 #include "Commands/CommandObjectTarget.h"
39 #include "Commands/CommandObjectThread.h"
40 #include "Commands/CommandObjectTrace.h"
41 #include "Commands/CommandObjectType.h"
42 #include "Commands/CommandObjectVersion.h"
43 #include "Commands/CommandObjectWatchpoint.h"
44 
45 #include "lldb/Core/Debugger.h"
46 #include "lldb/Core/PluginManager.h"
47 #include "lldb/Core/StreamFile.h"
48 #include "lldb/Utility/LLDBLog.h"
49 #include "lldb/Utility/Log.h"
50 #include "lldb/Utility/Reproducer.h"
51 #include "lldb/Utility/State.h"
52 #include "lldb/Utility/Stream.h"
53 #include "lldb/Utility/Timer.h"
54 
55 #include "lldb/Host/Config.h"
56 #if LLDB_ENABLE_LIBEDIT
57 #include "lldb/Host/Editline.h"
58 #endif
59 #include "lldb/Host/File.h"
60 #include "lldb/Host/FileCache.h"
61 #include "lldb/Host/Host.h"
62 #include "lldb/Host/HostInfo.h"
63 
64 #include "lldb/Interpreter/CommandCompletions.h"
65 #include "lldb/Interpreter/CommandInterpreter.h"
66 #include "lldb/Interpreter/CommandReturnObject.h"
67 #include "lldb/Interpreter/OptionValueProperties.h"
68 #include "lldb/Interpreter/Options.h"
69 #include "lldb/Interpreter/Property.h"
70 #include "lldb/Utility/Args.h"
71 
72 #include "lldb/Target/Language.h"
73 #include "lldb/Target/Process.h"
74 #include "lldb/Target/StopInfo.h"
75 #include "lldb/Target/TargetList.h"
76 #include "lldb/Target/Thread.h"
77 #include "lldb/Target/UnixSignals.h"
78 
79 #include "llvm/ADT/STLExtras.h"
80 #include "llvm/ADT/ScopeExit.h"
81 #include "llvm/ADT/SmallString.h"
82 #include "llvm/Support/FormatAdapters.h"
83 #include "llvm/Support/Path.h"
84 #include "llvm/Support/PrettyStackTrace.h"
85 #include "llvm/Support/ScopedPrinter.h"
86 
87 using namespace lldb;
88 using namespace lldb_private;
89 
90 static const char *k_white_space = " \t\v";
91 
92 static constexpr const char *InitFileWarning =
93     "There is a .lldbinit file in the current directory which is not being "
94     "read.\n"
95     "To silence this warning without sourcing in the local .lldbinit,\n"
96     "add the following to the lldbinit file in your home directory:\n"
97     "    settings set target.load-cwd-lldbinit false\n"
98     "To allow lldb to source .lldbinit files in the current working "
99     "directory,\n"
100     "set the value of this variable to true.  Only do so if you understand "
101     "and\n"
102     "accept the security risk.";
103 
104 #define LLDB_PROPERTIES_interpreter
105 #include "InterpreterProperties.inc"
106 
107 enum {
108 #define LLDB_PROPERTIES_interpreter
109 #include "InterpreterPropertiesEnum.inc"
110 };
111 
112 ConstString &CommandInterpreter::GetStaticBroadcasterClass() {
113   static ConstString class_name("lldb.commandInterpreter");
114   return class_name;
115 }
116 
117 CommandInterpreter::CommandInterpreter(Debugger &debugger,
118                                        bool synchronous_execution)
119     : Broadcaster(debugger.GetBroadcasterManager(),
120                   CommandInterpreter::GetStaticBroadcasterClass().AsCString()),
121       Properties(OptionValuePropertiesSP(
122           new OptionValueProperties(ConstString("interpreter")))),
123       IOHandlerDelegate(IOHandlerDelegate::Completion::LLDBCommand),
124       m_debugger(debugger), m_synchronous_execution(true),
125       m_skip_lldbinit_files(false), m_skip_app_init_files(false),
126       m_comment_char('#'), m_batch_command_mode(false),
127       m_truncation_warning(eNoTruncation), m_command_source_depth(0) {
128   SetEventName(eBroadcastBitThreadShouldExit, "thread-should-exit");
129   SetEventName(eBroadcastBitResetPrompt, "reset-prompt");
130   SetEventName(eBroadcastBitQuitCommandReceived, "quit");
131   SetSynchronous(synchronous_execution);
132   CheckInWithManager();
133   m_collection_sp->Initialize(g_interpreter_properties);
134 }
135 
136 bool CommandInterpreter::GetExpandRegexAliases() const {
137   const uint32_t idx = ePropertyExpandRegexAliases;
138   return m_collection_sp->GetPropertyAtIndexAsBoolean(
139       nullptr, idx, g_interpreter_properties[idx].default_uint_value != 0);
140 }
141 
142 bool CommandInterpreter::GetPromptOnQuit() const {
143   const uint32_t idx = ePropertyPromptOnQuit;
144   return m_collection_sp->GetPropertyAtIndexAsBoolean(
145       nullptr, idx, g_interpreter_properties[idx].default_uint_value != 0);
146 }
147 
148 void CommandInterpreter::SetPromptOnQuit(bool enable) {
149   const uint32_t idx = ePropertyPromptOnQuit;
150   m_collection_sp->SetPropertyAtIndexAsBoolean(nullptr, idx, enable);
151 }
152 
153 bool CommandInterpreter::GetSaveSessionOnQuit() const {
154   const uint32_t idx = ePropertySaveSessionOnQuit;
155   return m_collection_sp->GetPropertyAtIndexAsBoolean(
156       nullptr, idx, g_interpreter_properties[idx].default_uint_value != 0);
157 }
158 
159 void CommandInterpreter::SetSaveSessionOnQuit(bool enable) {
160   const uint32_t idx = ePropertySaveSessionOnQuit;
161   m_collection_sp->SetPropertyAtIndexAsBoolean(nullptr, idx, enable);
162 }
163 
164 FileSpec CommandInterpreter::GetSaveSessionDirectory() const {
165   const uint32_t idx = ePropertySaveSessionDirectory;
166   return m_collection_sp->GetPropertyAtIndexAsFileSpec(nullptr, idx);
167 }
168 
169 void CommandInterpreter::SetSaveSessionDirectory(llvm::StringRef path) {
170   const uint32_t idx = ePropertySaveSessionDirectory;
171   m_collection_sp->SetPropertyAtIndexAsString(nullptr, idx, path);
172 }
173 
174 bool CommandInterpreter::GetEchoCommands() const {
175   const uint32_t idx = ePropertyEchoCommands;
176   return m_collection_sp->GetPropertyAtIndexAsBoolean(
177       nullptr, idx, g_interpreter_properties[idx].default_uint_value != 0);
178 }
179 
180 void CommandInterpreter::SetEchoCommands(bool enable) {
181   const uint32_t idx = ePropertyEchoCommands;
182   m_collection_sp->SetPropertyAtIndexAsBoolean(nullptr, idx, enable);
183 }
184 
185 bool CommandInterpreter::GetEchoCommentCommands() const {
186   const uint32_t idx = ePropertyEchoCommentCommands;
187   return m_collection_sp->GetPropertyAtIndexAsBoolean(
188       nullptr, idx, g_interpreter_properties[idx].default_uint_value != 0);
189 }
190 
191 void CommandInterpreter::SetEchoCommentCommands(bool enable) {
192   const uint32_t idx = ePropertyEchoCommentCommands;
193   m_collection_sp->SetPropertyAtIndexAsBoolean(nullptr, idx, enable);
194 }
195 
196 void CommandInterpreter::AllowExitCodeOnQuit(bool allow) {
197   m_allow_exit_code = allow;
198   if (!allow)
199     m_quit_exit_code.reset();
200 }
201 
202 bool CommandInterpreter::SetQuitExitCode(int exit_code) {
203   if (!m_allow_exit_code)
204     return false;
205   m_quit_exit_code = exit_code;
206   return true;
207 }
208 
209 int CommandInterpreter::GetQuitExitCode(bool &exited) const {
210   exited = m_quit_exit_code.hasValue();
211   if (exited)
212     return *m_quit_exit_code;
213   return 0;
214 }
215 
216 void CommandInterpreter::ResolveCommand(const char *command_line,
217                                         CommandReturnObject &result) {
218   std::string command = command_line;
219   if (ResolveCommandImpl(command, result) != nullptr) {
220     result.AppendMessageWithFormat("%s", command.c_str());
221     result.SetStatus(eReturnStatusSuccessFinishResult);
222   }
223 }
224 
225 bool CommandInterpreter::GetStopCmdSourceOnError() const {
226   const uint32_t idx = ePropertyStopCmdSourceOnError;
227   return m_collection_sp->GetPropertyAtIndexAsBoolean(
228       nullptr, idx, g_interpreter_properties[idx].default_uint_value != 0);
229 }
230 
231 bool CommandInterpreter::GetSpaceReplPrompts() const {
232   const uint32_t idx = ePropertySpaceReplPrompts;
233   return m_collection_sp->GetPropertyAtIndexAsBoolean(
234       nullptr, idx, g_interpreter_properties[idx].default_uint_value != 0);
235 }
236 
237 bool CommandInterpreter::GetRepeatPreviousCommand() const {
238   const uint32_t idx = ePropertyRepeatPreviousCommand;
239   return m_collection_sp->GetPropertyAtIndexAsBoolean(
240       nullptr, idx, g_interpreter_properties[idx].default_uint_value != 0);
241 }
242 
243 void CommandInterpreter::Initialize() {
244   LLDB_SCOPED_TIMER();
245 
246   CommandReturnObject result(m_debugger.GetUseColor());
247 
248   LoadCommandDictionary();
249 
250   // An alias arguments vector to reuse - reset it before use...
251   OptionArgVectorSP alias_arguments_vector_sp(new OptionArgVector);
252 
253   // Set up some initial aliases.
254   CommandObjectSP cmd_obj_sp = GetCommandSPExact("quit");
255   if (cmd_obj_sp) {
256     AddAlias("q", cmd_obj_sp);
257     AddAlias("exit", cmd_obj_sp);
258   }
259 
260   cmd_obj_sp = GetCommandSPExact("_regexp-attach");
261   if (cmd_obj_sp)
262     AddAlias("attach", cmd_obj_sp)->SetSyntax(cmd_obj_sp->GetSyntax());
263 
264   cmd_obj_sp = GetCommandSPExact("process detach");
265   if (cmd_obj_sp) {
266     AddAlias("detach", cmd_obj_sp);
267   }
268 
269   cmd_obj_sp = GetCommandSPExact("process continue");
270   if (cmd_obj_sp) {
271     AddAlias("c", cmd_obj_sp);
272     AddAlias("continue", cmd_obj_sp);
273   }
274 
275   cmd_obj_sp = GetCommandSPExact("_regexp-break");
276   if (cmd_obj_sp)
277     AddAlias("b", cmd_obj_sp)->SetSyntax(cmd_obj_sp->GetSyntax());
278 
279   cmd_obj_sp = GetCommandSPExact("_regexp-tbreak");
280   if (cmd_obj_sp)
281     AddAlias("tbreak", cmd_obj_sp)->SetSyntax(cmd_obj_sp->GetSyntax());
282 
283   cmd_obj_sp = GetCommandSPExact("thread step-inst");
284   if (cmd_obj_sp) {
285     AddAlias("stepi", cmd_obj_sp);
286     AddAlias("si", cmd_obj_sp);
287   }
288 
289   cmd_obj_sp = GetCommandSPExact("thread step-inst-over");
290   if (cmd_obj_sp) {
291     AddAlias("nexti", cmd_obj_sp);
292     AddAlias("ni", cmd_obj_sp);
293   }
294 
295   cmd_obj_sp = GetCommandSPExact("thread step-in");
296   if (cmd_obj_sp) {
297     AddAlias("s", cmd_obj_sp);
298     AddAlias("step", cmd_obj_sp);
299     CommandAlias *sif_alias = AddAlias(
300         "sif", cmd_obj_sp, "--end-linenumber block --step-in-target %1");
301     if (sif_alias) {
302       sif_alias->SetHelp("Step through the current block, stopping if you step "
303                          "directly into a function whose name matches the "
304                          "TargetFunctionName.");
305       sif_alias->SetSyntax("sif <TargetFunctionName>");
306     }
307   }
308 
309   cmd_obj_sp = GetCommandSPExact("thread step-over");
310   if (cmd_obj_sp) {
311     AddAlias("n", cmd_obj_sp);
312     AddAlias("next", cmd_obj_sp);
313   }
314 
315   cmd_obj_sp = GetCommandSPExact("thread step-out");
316   if (cmd_obj_sp) {
317     AddAlias("finish", cmd_obj_sp);
318   }
319 
320   cmd_obj_sp = GetCommandSPExact("frame select");
321   if (cmd_obj_sp) {
322     AddAlias("f", cmd_obj_sp);
323   }
324 
325   cmd_obj_sp = GetCommandSPExact("thread select");
326   if (cmd_obj_sp) {
327     AddAlias("t", cmd_obj_sp);
328   }
329 
330   cmd_obj_sp = GetCommandSPExact("_regexp-jump");
331   if (cmd_obj_sp) {
332     AddAlias("j", cmd_obj_sp)->SetSyntax(cmd_obj_sp->GetSyntax());
333     AddAlias("jump", cmd_obj_sp)->SetSyntax(cmd_obj_sp->GetSyntax());
334   }
335 
336   cmd_obj_sp = GetCommandSPExact("_regexp-list");
337   if (cmd_obj_sp) {
338     AddAlias("l", cmd_obj_sp)->SetSyntax(cmd_obj_sp->GetSyntax());
339     AddAlias("list", cmd_obj_sp)->SetSyntax(cmd_obj_sp->GetSyntax());
340   }
341 
342   cmd_obj_sp = GetCommandSPExact("_regexp-env");
343   if (cmd_obj_sp)
344     AddAlias("env", cmd_obj_sp)->SetSyntax(cmd_obj_sp->GetSyntax());
345 
346   cmd_obj_sp = GetCommandSPExact("memory read");
347   if (cmd_obj_sp)
348     AddAlias("x", cmd_obj_sp);
349 
350   cmd_obj_sp = GetCommandSPExact("_regexp-up");
351   if (cmd_obj_sp)
352     AddAlias("up", cmd_obj_sp)->SetSyntax(cmd_obj_sp->GetSyntax());
353 
354   cmd_obj_sp = GetCommandSPExact("_regexp-down");
355   if (cmd_obj_sp)
356     AddAlias("down", cmd_obj_sp)->SetSyntax(cmd_obj_sp->GetSyntax());
357 
358   cmd_obj_sp = GetCommandSPExact("_regexp-display");
359   if (cmd_obj_sp)
360     AddAlias("display", cmd_obj_sp)->SetSyntax(cmd_obj_sp->GetSyntax());
361 
362   cmd_obj_sp = GetCommandSPExact("disassemble");
363   if (cmd_obj_sp)
364     AddAlias("dis", cmd_obj_sp);
365 
366   cmd_obj_sp = GetCommandSPExact("disassemble");
367   if (cmd_obj_sp)
368     AddAlias("di", cmd_obj_sp);
369 
370   cmd_obj_sp = GetCommandSPExact("_regexp-undisplay");
371   if (cmd_obj_sp)
372     AddAlias("undisplay", cmd_obj_sp)->SetSyntax(cmd_obj_sp->GetSyntax());
373 
374   cmd_obj_sp = GetCommandSPExact("_regexp-bt");
375   if (cmd_obj_sp)
376     AddAlias("bt", cmd_obj_sp)->SetSyntax(cmd_obj_sp->GetSyntax());
377 
378   cmd_obj_sp = GetCommandSPExact("target create");
379   if (cmd_obj_sp)
380     AddAlias("file", cmd_obj_sp);
381 
382   cmd_obj_sp = GetCommandSPExact("target modules");
383   if (cmd_obj_sp)
384     AddAlias("image", cmd_obj_sp);
385 
386   alias_arguments_vector_sp = std::make_shared<OptionArgVector>();
387 
388   cmd_obj_sp = GetCommandSPExact("expression");
389   if (cmd_obj_sp) {
390     AddAlias("p", cmd_obj_sp, "--")->SetHelpLong("");
391     AddAlias("print", cmd_obj_sp, "--")->SetHelpLong("");
392     AddAlias("call", cmd_obj_sp, "--")->SetHelpLong("");
393     if (auto *po = AddAlias("po", cmd_obj_sp, "-O --")) {
394       po->SetHelp("Evaluate an expression on the current thread.  Displays any "
395                   "returned value with formatting "
396                   "controlled by the type's author.");
397       po->SetHelpLong("");
398     }
399     CommandAlias *parray_alias =
400         AddAlias("parray", cmd_obj_sp, "--element-count %1 --");
401     if (parray_alias) {
402         parray_alias->SetHelp
403           ("parray <COUNT> <EXPRESSION> -- lldb will evaluate EXPRESSION "
404            "to get a typed-pointer-to-an-array in memory, and will display "
405            "COUNT elements of that type from the array.");
406         parray_alias->SetHelpLong("");
407     }
408     CommandAlias *poarray_alias = AddAlias("poarray", cmd_obj_sp,
409              "--object-description --element-count %1 --");
410     if (poarray_alias) {
411       poarray_alias->SetHelp("poarray <COUNT> <EXPRESSION> -- lldb will "
412           "evaluate EXPRESSION to get the address of an array of COUNT "
413           "objects in memory, and will call po on them.");
414       poarray_alias->SetHelpLong("");
415     }
416   }
417 
418   cmd_obj_sp = GetCommandSPExact("platform shell");
419   if (cmd_obj_sp) {
420     CommandAlias *shell_alias = AddAlias("shell", cmd_obj_sp, " --host --");
421     if (shell_alias) {
422       shell_alias->SetHelp("Run a shell command on the host.");
423       shell_alias->SetHelpLong("");
424       shell_alias->SetSyntax("shell <shell-command>");
425     }
426   }
427 
428   cmd_obj_sp = GetCommandSPExact("process kill");
429   if (cmd_obj_sp) {
430     AddAlias("kill", cmd_obj_sp);
431   }
432 
433   cmd_obj_sp = GetCommandSPExact("process launch");
434   if (cmd_obj_sp) {
435     alias_arguments_vector_sp = std::make_shared<OptionArgVector>();
436 #if defined(__APPLE__)
437 #if defined(TARGET_OS_IPHONE)
438     AddAlias("r", cmd_obj_sp, "--");
439     AddAlias("run", cmd_obj_sp, "--");
440 #else
441     AddAlias("r", cmd_obj_sp, "--shell-expand-args true --");
442     AddAlias("run", cmd_obj_sp, "--shell-expand-args true --");
443 #endif
444 #else
445     StreamString defaultshell;
446     defaultshell.Printf("--shell=%s --",
447                         HostInfo::GetDefaultShell().GetPath().c_str());
448     AddAlias("r", cmd_obj_sp, defaultshell.GetString());
449     AddAlias("run", cmd_obj_sp, defaultshell.GetString());
450 #endif
451   }
452 
453   cmd_obj_sp = GetCommandSPExact("target symbols add");
454   if (cmd_obj_sp) {
455     AddAlias("add-dsym", cmd_obj_sp);
456   }
457 
458   cmd_obj_sp = GetCommandSPExact("breakpoint set");
459   if (cmd_obj_sp) {
460     AddAlias("rbreak", cmd_obj_sp, "--func-regex %1");
461   }
462 
463   cmd_obj_sp = GetCommandSPExact("frame variable");
464   if (cmd_obj_sp) {
465     AddAlias("v", cmd_obj_sp);
466     AddAlias("var", cmd_obj_sp);
467     AddAlias("vo", cmd_obj_sp, "--object-description");
468   }
469 
470   cmd_obj_sp = GetCommandSPExact("register");
471   if (cmd_obj_sp) {
472     AddAlias("re", cmd_obj_sp);
473   }
474 
475   cmd_obj_sp = GetCommandSPExact("session history");
476   if (cmd_obj_sp) {
477     AddAlias("history", cmd_obj_sp);
478   }
479 }
480 
481 void CommandInterpreter::Clear() {
482   m_command_io_handler_sp.reset();
483 }
484 
485 const char *CommandInterpreter::ProcessEmbeddedScriptCommands(const char *arg) {
486   // This function has not yet been implemented.
487 
488   // Look for any embedded script command
489   // If found,
490   //    get interpreter object from the command dictionary,
491   //    call execute_one_command on it,
492   //    get the results as a string,
493   //    substitute that string for current stuff.
494 
495   return arg;
496 }
497 
498 #define REGISTER_COMMAND_OBJECT(NAME, CLASS)                                   \
499   m_command_dict[NAME] = std::make_shared<CLASS>(*this);
500 
501 void CommandInterpreter::LoadCommandDictionary() {
502   LLDB_SCOPED_TIMER();
503 
504   REGISTER_COMMAND_OBJECT("apropos", CommandObjectApropos);
505   REGISTER_COMMAND_OBJECT("breakpoint", CommandObjectMultiwordBreakpoint);
506   REGISTER_COMMAND_OBJECT("command", CommandObjectMultiwordCommands);
507   REGISTER_COMMAND_OBJECT("disassemble", CommandObjectDisassemble);
508   REGISTER_COMMAND_OBJECT("expression", CommandObjectExpression);
509   REGISTER_COMMAND_OBJECT("frame", CommandObjectMultiwordFrame);
510   REGISTER_COMMAND_OBJECT("gui", CommandObjectGUI);
511   REGISTER_COMMAND_OBJECT("help", CommandObjectHelp);
512   REGISTER_COMMAND_OBJECT("log", CommandObjectLog);
513   REGISTER_COMMAND_OBJECT("memory", CommandObjectMemory);
514   REGISTER_COMMAND_OBJECT("platform", CommandObjectPlatform);
515   REGISTER_COMMAND_OBJECT("plugin", CommandObjectPlugin);
516   REGISTER_COMMAND_OBJECT("process", CommandObjectMultiwordProcess);
517   REGISTER_COMMAND_OBJECT("quit", CommandObjectQuit);
518   REGISTER_COMMAND_OBJECT("register", CommandObjectRegister);
519   REGISTER_COMMAND_OBJECT("reproducer", CommandObjectReproducer);
520   REGISTER_COMMAND_OBJECT("script", CommandObjectScript);
521   REGISTER_COMMAND_OBJECT("settings", CommandObjectMultiwordSettings);
522   REGISTER_COMMAND_OBJECT("session", CommandObjectSession);
523   REGISTER_COMMAND_OBJECT("source", CommandObjectMultiwordSource);
524   REGISTER_COMMAND_OBJECT("statistics", CommandObjectStats);
525   REGISTER_COMMAND_OBJECT("target", CommandObjectMultiwordTarget);
526   REGISTER_COMMAND_OBJECT("thread", CommandObjectMultiwordThread);
527   REGISTER_COMMAND_OBJECT("trace", CommandObjectTrace);
528   REGISTER_COMMAND_OBJECT("type", CommandObjectType);
529   REGISTER_COMMAND_OBJECT("version", CommandObjectVersion);
530   REGISTER_COMMAND_OBJECT("watchpoint", CommandObjectMultiwordWatchpoint);
531   REGISTER_COMMAND_OBJECT("language", CommandObjectLanguage);
532 
533   // clang-format off
534   const char *break_regexes[][2] = {
535       {"^(.*[^[:space:]])[[:space:]]*:[[:space:]]*([[:digit:]]+)[[:space:]]*:[[:space:]]*([[:digit:]]+)[[:space:]]*$",
536        "breakpoint set --file '%1' --line %2 --column %3"},
537       {"^(.*[^[:space:]])[[:space:]]*:[[:space:]]*([[:digit:]]+)[[:space:]]*$",
538        "breakpoint set --file '%1' --line %2"},
539       {"^/([^/]+)/$", "breakpoint set --source-pattern-regexp '%1'"},
540       {"^([[:digit:]]+)[[:space:]]*$", "breakpoint set --line %1"},
541       {"^\\*?(0x[[:xdigit:]]+)[[:space:]]*$", "breakpoint set --address %1"},
542       {"^[\"']?([-+]?\\[.*\\])[\"']?[[:space:]]*$",
543        "breakpoint set --name '%1'"},
544       {"^(-.*)$", "breakpoint set %1"},
545       {"^(.*[^[:space:]])`(.*[^[:space:]])[[:space:]]*$",
546        "breakpoint set --name '%2' --shlib '%1'"},
547       {"^\\&(.*[^[:space:]])[[:space:]]*$",
548        "breakpoint set --name '%1' --skip-prologue=0"},
549       {"^[\"']?(.*[^[:space:]\"'])[\"']?[[:space:]]*$",
550        "breakpoint set --name '%1'"}};
551   // clang-format on
552 
553   size_t num_regexes = llvm::array_lengthof(break_regexes);
554 
555   std::unique_ptr<CommandObjectRegexCommand> break_regex_cmd_up(
556       new CommandObjectRegexCommand(
557           *this, "_regexp-break",
558           "Set a breakpoint using one of several shorthand formats.",
559           "\n"
560           "_regexp-break <filename>:<linenum>:<colnum>\n"
561           "              main.c:12:21          // Break at line 12 and column "
562           "21 of main.c\n\n"
563           "_regexp-break <filename>:<linenum>\n"
564           "              main.c:12             // Break at line 12 of "
565           "main.c\n\n"
566           "_regexp-break <linenum>\n"
567           "              12                    // Break at line 12 of current "
568           "file\n\n"
569           "_regexp-break 0x<address>\n"
570           "              0x1234000             // Break at address "
571           "0x1234000\n\n"
572           "_regexp-break <name>\n"
573           "              main                  // Break in 'main' after the "
574           "prologue\n\n"
575           "_regexp-break &<name>\n"
576           "              &main                 // Break at first instruction "
577           "in 'main'\n\n"
578           "_regexp-break <module>`<name>\n"
579           "              libc.so`malloc        // Break in 'malloc' from "
580           "'libc.so'\n\n"
581           "_regexp-break /<source-regex>/\n"
582           "              /break here/          // Break on source lines in "
583           "current file\n"
584           "                                    // containing text 'break "
585           "here'.\n",
586           3,
587           CommandCompletions::eSymbolCompletion |
588               CommandCompletions::eSourceFileCompletion,
589           false));
590 
591   if (break_regex_cmd_up) {
592     bool success = true;
593     for (size_t i = 0; i < num_regexes; i++) {
594       success = break_regex_cmd_up->AddRegexCommand(break_regexes[i][0],
595                                                     break_regexes[i][1]);
596       if (!success)
597         break;
598     }
599     success =
600         break_regex_cmd_up->AddRegexCommand("^$", "breakpoint list --full");
601 
602     if (success) {
603       CommandObjectSP break_regex_cmd_sp(break_regex_cmd_up.release());
604       m_command_dict[std::string(break_regex_cmd_sp->GetCommandName())] =
605           break_regex_cmd_sp;
606     }
607   }
608 
609   std::unique_ptr<CommandObjectRegexCommand> tbreak_regex_cmd_up(
610       new CommandObjectRegexCommand(
611           *this, "_regexp-tbreak",
612           "Set a one-shot breakpoint using one of several shorthand formats.",
613           "\n"
614           "_regexp-break <filename>:<linenum>:<colnum>\n"
615           "              main.c:12:21          // Break at line 12 and column "
616           "21 of main.c\n\n"
617           "_regexp-break <filename>:<linenum>\n"
618           "              main.c:12             // Break at line 12 of "
619           "main.c\n\n"
620           "_regexp-break <linenum>\n"
621           "              12                    // Break at line 12 of current "
622           "file\n\n"
623           "_regexp-break 0x<address>\n"
624           "              0x1234000             // Break at address "
625           "0x1234000\n\n"
626           "_regexp-break <name>\n"
627           "              main                  // Break in 'main' after the "
628           "prologue\n\n"
629           "_regexp-break &<name>\n"
630           "              &main                 // Break at first instruction "
631           "in 'main'\n\n"
632           "_regexp-break <module>`<name>\n"
633           "              libc.so`malloc        // Break in 'malloc' from "
634           "'libc.so'\n\n"
635           "_regexp-break /<source-regex>/\n"
636           "              /break here/          // Break on source lines in "
637           "current file\n"
638           "                                    // containing text 'break "
639           "here'.\n",
640           2,
641           CommandCompletions::eSymbolCompletion |
642               CommandCompletions::eSourceFileCompletion,
643           false));
644 
645   if (tbreak_regex_cmd_up) {
646     bool success = true;
647     for (size_t i = 0; i < num_regexes; i++) {
648       std::string command = break_regexes[i][1];
649       command += " -o 1";
650       success =
651           tbreak_regex_cmd_up->AddRegexCommand(break_regexes[i][0], command);
652       if (!success)
653         break;
654     }
655     success =
656         tbreak_regex_cmd_up->AddRegexCommand("^$", "breakpoint list --full");
657 
658     if (success) {
659       CommandObjectSP tbreak_regex_cmd_sp(tbreak_regex_cmd_up.release());
660       m_command_dict[std::string(tbreak_regex_cmd_sp->GetCommandName())] =
661           tbreak_regex_cmd_sp;
662     }
663   }
664 
665   std::unique_ptr<CommandObjectRegexCommand> attach_regex_cmd_up(
666       new CommandObjectRegexCommand(
667           *this, "_regexp-attach", "Attach to process by ID or name.",
668           "_regexp-attach <pid> | <process-name>", 2, 0, false));
669   if (attach_regex_cmd_up) {
670     if (attach_regex_cmd_up->AddRegexCommand("^([0-9]+)[[:space:]]*$",
671                                              "process attach --pid %1") &&
672         attach_regex_cmd_up->AddRegexCommand(
673             "^(-.*|.* -.*)$", "process attach %1") && // Any options that are
674                                                       // specified get passed to
675                                                       // 'process attach'
676         attach_regex_cmd_up->AddRegexCommand("^(.+)$",
677                                              "process attach --name '%1'") &&
678         attach_regex_cmd_up->AddRegexCommand("^$", "process attach")) {
679       CommandObjectSP attach_regex_cmd_sp(attach_regex_cmd_up.release());
680       m_command_dict[std::string(attach_regex_cmd_sp->GetCommandName())] =
681           attach_regex_cmd_sp;
682     }
683   }
684 
685   std::unique_ptr<CommandObjectRegexCommand> down_regex_cmd_up(
686       new CommandObjectRegexCommand(*this, "_regexp-down",
687                                     "Select a newer stack frame.  Defaults to "
688                                     "moving one frame, a numeric argument can "
689                                     "specify an arbitrary number.",
690                                     "_regexp-down [<count>]", 2, 0, false));
691   if (down_regex_cmd_up) {
692     if (down_regex_cmd_up->AddRegexCommand("^$", "frame select -r -1") &&
693         down_regex_cmd_up->AddRegexCommand("^([0-9]+)$",
694                                            "frame select -r -%1")) {
695       CommandObjectSP down_regex_cmd_sp(down_regex_cmd_up.release());
696       m_command_dict[std::string(down_regex_cmd_sp->GetCommandName())] =
697           down_regex_cmd_sp;
698     }
699   }
700 
701   std::unique_ptr<CommandObjectRegexCommand> up_regex_cmd_up(
702       new CommandObjectRegexCommand(
703           *this, "_regexp-up",
704           "Select an older stack frame.  Defaults to moving one "
705           "frame, a numeric argument can specify an arbitrary number.",
706           "_regexp-up [<count>]", 2, 0, false));
707   if (up_regex_cmd_up) {
708     if (up_regex_cmd_up->AddRegexCommand("^$", "frame select -r 1") &&
709         up_regex_cmd_up->AddRegexCommand("^([0-9]+)$", "frame select -r %1")) {
710       CommandObjectSP up_regex_cmd_sp(up_regex_cmd_up.release());
711       m_command_dict[std::string(up_regex_cmd_sp->GetCommandName())] =
712           up_regex_cmd_sp;
713     }
714   }
715 
716   std::unique_ptr<CommandObjectRegexCommand> display_regex_cmd_up(
717       new CommandObjectRegexCommand(
718           *this, "_regexp-display",
719           "Evaluate an expression at every stop (see 'help target stop-hook'.)",
720           "_regexp-display expression", 2, 0, false));
721   if (display_regex_cmd_up) {
722     if (display_regex_cmd_up->AddRegexCommand(
723             "^(.+)$", "target stop-hook add -o \"expr -- %1\"")) {
724       CommandObjectSP display_regex_cmd_sp(display_regex_cmd_up.release());
725       m_command_dict[std::string(display_regex_cmd_sp->GetCommandName())] =
726           display_regex_cmd_sp;
727     }
728   }
729 
730   std::unique_ptr<CommandObjectRegexCommand> undisplay_regex_cmd_up(
731       new CommandObjectRegexCommand(*this, "_regexp-undisplay",
732                                     "Stop displaying expression at every "
733                                     "stop (specified by stop-hook index.)",
734                                     "_regexp-undisplay stop-hook-number", 2, 0,
735                                     false));
736   if (undisplay_regex_cmd_up) {
737     if (undisplay_regex_cmd_up->AddRegexCommand("^([0-9]+)$",
738                                                 "target stop-hook delete %1")) {
739       CommandObjectSP undisplay_regex_cmd_sp(undisplay_regex_cmd_up.release());
740       m_command_dict[std::string(undisplay_regex_cmd_sp->GetCommandName())] =
741           undisplay_regex_cmd_sp;
742     }
743   }
744 
745   std::unique_ptr<CommandObjectRegexCommand> connect_gdb_remote_cmd_up(
746       new CommandObjectRegexCommand(
747           *this, "gdb-remote",
748           "Connect to a process via remote GDB server.\n"
749           "If no host is specifed, localhost is assumed.\n"
750           "gdb-remote is an abbreviation for 'process connect --plugin "
751           "gdb-remote connect://<hostname>:<port>'\n",
752           "gdb-remote [<hostname>:]<portnum>", 2, 0, false));
753   if (connect_gdb_remote_cmd_up) {
754     if (connect_gdb_remote_cmd_up->AddRegexCommand(
755             "^([^:]+|\\[[0-9a-fA-F:]+.*\\]):([0-9]+)$",
756             "process connect --plugin gdb-remote connect://%1:%2") &&
757         connect_gdb_remote_cmd_up->AddRegexCommand(
758             "^([[:digit:]]+)$",
759             "process connect --plugin gdb-remote connect://localhost:%1")) {
760       CommandObjectSP command_sp(connect_gdb_remote_cmd_up.release());
761       m_command_dict[std::string(command_sp->GetCommandName())] = command_sp;
762     }
763   }
764 
765   std::unique_ptr<CommandObjectRegexCommand> connect_kdp_remote_cmd_up(
766       new CommandObjectRegexCommand(
767           *this, "kdp-remote",
768           "Connect to a process via remote KDP server.\n"
769           "If no UDP port is specified, port 41139 is assumed.\n"
770           "kdp-remote is an abbreviation for 'process connect --plugin "
771           "kdp-remote udp://<hostname>:<port>'\n",
772           "kdp-remote <hostname>[:<portnum>]", 2, 0, false));
773   if (connect_kdp_remote_cmd_up) {
774     if (connect_kdp_remote_cmd_up->AddRegexCommand(
775             "^([^:]+:[[:digit:]]+)$",
776             "process connect --plugin kdp-remote udp://%1") &&
777         connect_kdp_remote_cmd_up->AddRegexCommand(
778             "^(.+)$", "process connect --plugin kdp-remote udp://%1:41139")) {
779       CommandObjectSP command_sp(connect_kdp_remote_cmd_up.release());
780       m_command_dict[std::string(command_sp->GetCommandName())] = command_sp;
781     }
782   }
783 
784   std::unique_ptr<CommandObjectRegexCommand> bt_regex_cmd_up(
785       new CommandObjectRegexCommand(
786           *this, "_regexp-bt",
787           "Show the current thread's call stack.  Any numeric argument "
788           "displays at most that many "
789           "frames.  The argument 'all' displays all threads.  Use 'settings"
790           " set frame-format' to customize the printing of individual frames "
791           "and 'settings set thread-format' to customize the thread header.",
792           "bt [<digit> | all]", 2, 0, false));
793   if (bt_regex_cmd_up) {
794     // accept but don't document "bt -c <number>" -- before bt was a regex
795     // command if you wanted to backtrace three frames you would do "bt -c 3"
796     // but the intention is to have this emulate the gdb "bt" command and so
797     // now "bt 3" is the preferred form, in line with gdb.
798     if (bt_regex_cmd_up->AddRegexCommand("^([[:digit:]]+)[[:space:]]*$",
799                                          "thread backtrace -c %1") &&
800         bt_regex_cmd_up->AddRegexCommand("^-c ([[:digit:]]+)[[:space:]]*$",
801                                          "thread backtrace -c %1") &&
802         bt_regex_cmd_up->AddRegexCommand("^all[[:space:]]*$", "thread backtrace all") &&
803         bt_regex_cmd_up->AddRegexCommand("^[[:space:]]*$", "thread backtrace")) {
804       CommandObjectSP command_sp(bt_regex_cmd_up.release());
805       m_command_dict[std::string(command_sp->GetCommandName())] = command_sp;
806     }
807   }
808 
809   std::unique_ptr<CommandObjectRegexCommand> list_regex_cmd_up(
810       new CommandObjectRegexCommand(
811           *this, "_regexp-list",
812           "List relevant source code using one of several shorthand formats.",
813           "\n"
814           "_regexp-list <file>:<line>   // List around specific file/line\n"
815           "_regexp-list <line>          // List current file around specified "
816           "line\n"
817           "_regexp-list <function-name> // List specified function\n"
818           "_regexp-list 0x<address>     // List around specified address\n"
819           "_regexp-list -[<count>]      // List previous <count> lines\n"
820           "_regexp-list                 // List subsequent lines",
821           2, CommandCompletions::eSourceFileCompletion, false));
822   if (list_regex_cmd_up) {
823     if (list_regex_cmd_up->AddRegexCommand("^([0-9]+)[[:space:]]*$",
824                                            "source list --line %1") &&
825         list_regex_cmd_up->AddRegexCommand(
826             "^(.*[^[:space:]])[[:space:]]*:[[:space:]]*([[:digit:]]+)[[:space:]"
827             "]*$",
828             "source list --file '%1' --line %2") &&
829         list_regex_cmd_up->AddRegexCommand(
830             "^\\*?(0x[[:xdigit:]]+)[[:space:]]*$",
831             "source list --address %1") &&
832         list_regex_cmd_up->AddRegexCommand("^-[[:space:]]*$",
833                                            "source list --reverse") &&
834         list_regex_cmd_up->AddRegexCommand(
835             "^-([[:digit:]]+)[[:space:]]*$",
836             "source list --reverse --count %1") &&
837         list_regex_cmd_up->AddRegexCommand("^(.+)$",
838                                            "source list --name \"%1\"") &&
839         list_regex_cmd_up->AddRegexCommand("^$", "source list")) {
840       CommandObjectSP list_regex_cmd_sp(list_regex_cmd_up.release());
841       m_command_dict[std::string(list_regex_cmd_sp->GetCommandName())] =
842           list_regex_cmd_sp;
843     }
844   }
845 
846   std::unique_ptr<CommandObjectRegexCommand> env_regex_cmd_up(
847       new CommandObjectRegexCommand(
848           *this, "_regexp-env",
849           "Shorthand for viewing and setting environment variables.",
850           "\n"
851           "_regexp-env                  // Show environment\n"
852           "_regexp-env <name>=<value>   // Set an environment variable",
853           2, 0, false));
854   if (env_regex_cmd_up) {
855     if (env_regex_cmd_up->AddRegexCommand("^$",
856                                           "settings show target.env-vars") &&
857         env_regex_cmd_up->AddRegexCommand("^([A-Za-z_][A-Za-z_0-9]*=.*)$",
858                                           "settings set target.env-vars %1")) {
859       CommandObjectSP env_regex_cmd_sp(env_regex_cmd_up.release());
860       m_command_dict[std::string(env_regex_cmd_sp->GetCommandName())] =
861           env_regex_cmd_sp;
862     }
863   }
864 
865   std::unique_ptr<CommandObjectRegexCommand> jump_regex_cmd_up(
866       new CommandObjectRegexCommand(
867           *this, "_regexp-jump", "Set the program counter to a new address.",
868           "\n"
869           "_regexp-jump <line>\n"
870           "_regexp-jump +<line-offset> | -<line-offset>\n"
871           "_regexp-jump <file>:<line>\n"
872           "_regexp-jump *<addr>\n",
873           2, 0, false));
874   if (jump_regex_cmd_up) {
875     if (jump_regex_cmd_up->AddRegexCommand("^\\*(.*)$",
876                                            "thread jump --addr %1") &&
877         jump_regex_cmd_up->AddRegexCommand("^([0-9]+)$",
878                                            "thread jump --line %1") &&
879         jump_regex_cmd_up->AddRegexCommand("^([^:]+):([0-9]+)$",
880                                            "thread jump --file %1 --line %2") &&
881         jump_regex_cmd_up->AddRegexCommand("^([+\\-][0-9]+)$",
882                                            "thread jump --by %1")) {
883       CommandObjectSP jump_regex_cmd_sp(jump_regex_cmd_up.release());
884       m_command_dict[std::string(jump_regex_cmd_sp->GetCommandName())] =
885           jump_regex_cmd_sp;
886     }
887   }
888 }
889 
890 int CommandInterpreter::GetCommandNamesMatchingPartialString(
891     const char *cmd_str, bool include_aliases, StringList &matches,
892     StringList &descriptions) {
893   AddNamesMatchingPartialString(m_command_dict, cmd_str, matches,
894                                 &descriptions);
895 
896   if (include_aliases) {
897     AddNamesMatchingPartialString(m_alias_dict, cmd_str, matches,
898                                   &descriptions);
899   }
900 
901   return matches.GetSize();
902 }
903 
904 CommandObjectMultiword *CommandInterpreter::VerifyUserMultiwordCmdPath(
905     Args &path, bool leaf_is_command, Status &result) {
906   result.Clear();
907 
908   auto get_multi_or_report_error =
909       [&result](CommandObjectSP cmd_sp,
910                            const char *name) -> CommandObjectMultiword * {
911     if (!cmd_sp) {
912       result.SetErrorStringWithFormat("Path component: '%s' not found", name);
913       return nullptr;
914     }
915     if (!cmd_sp->IsUserCommand()) {
916       result.SetErrorStringWithFormat("Path component: '%s' is not a user "
917                                       "command",
918                                       name);
919       return nullptr;
920     }
921     CommandObjectMultiword *cmd_as_multi = cmd_sp->GetAsMultiwordCommand();
922     if (!cmd_as_multi) {
923       result.SetErrorStringWithFormat("Path component: '%s' is not a container "
924                                       "command",
925                                       name);
926       return nullptr;
927     }
928     return cmd_as_multi;
929   };
930 
931   size_t num_args = path.GetArgumentCount();
932   if (num_args == 0) {
933     result.SetErrorString("empty command path");
934     return nullptr;
935   }
936 
937   if (num_args == 1 && leaf_is_command) {
938     // We just got a leaf command to be added to the root.  That's not an error,
939     // just return null for the container.
940     return nullptr;
941   }
942 
943   // Start by getting the root command from the interpreter.
944   const char *cur_name = path.GetArgumentAtIndex(0);
945   CommandObjectSP cur_cmd_sp = GetCommandSPExact(cur_name);
946   CommandObjectMultiword *cur_as_multi =
947       get_multi_or_report_error(cur_cmd_sp, cur_name);
948   if (cur_as_multi == nullptr)
949     return nullptr;
950 
951   size_t num_path_elements = num_args - (leaf_is_command ? 1 : 0);
952   for (size_t cursor = 1; cursor < num_path_elements && cur_as_multi != nullptr;
953        cursor++) {
954     cur_name = path.GetArgumentAtIndex(cursor);
955     cur_cmd_sp = cur_as_multi->GetSubcommandSPExact(cur_name);
956     cur_as_multi = get_multi_or_report_error(cur_cmd_sp, cur_name);
957   }
958   return cur_as_multi;
959 }
960 
961 CommandObjectSP
962 CommandInterpreter::GetCommandSP(llvm::StringRef cmd_str, bool include_aliases,
963                                  bool exact, StringList *matches,
964                                  StringList *descriptions) const {
965   CommandObjectSP command_sp;
966 
967   std::string cmd = std::string(cmd_str);
968 
969   if (HasCommands()) {
970     auto pos = m_command_dict.find(cmd);
971     if (pos != m_command_dict.end())
972       command_sp = pos->second;
973   }
974 
975   if (include_aliases && HasAliases()) {
976     auto alias_pos = m_alias_dict.find(cmd);
977     if (alias_pos != m_alias_dict.end())
978       command_sp = alias_pos->second;
979   }
980 
981   if (HasUserCommands()) {
982     auto pos = m_user_dict.find(cmd);
983     if (pos != m_user_dict.end())
984       command_sp = pos->second;
985   }
986 
987   if (HasUserMultiwordCommands()) {
988     auto pos = m_user_mw_dict.find(cmd);
989     if (pos != m_user_mw_dict.end())
990       command_sp = pos->second;
991   }
992 
993   if (!exact && !command_sp) {
994     // We will only get into here if we didn't find any exact matches.
995 
996     CommandObjectSP user_match_sp, user_mw_match_sp, alias_match_sp,
997         real_match_sp;
998 
999     StringList local_matches;
1000     if (matches == nullptr)
1001       matches = &local_matches;
1002 
1003     unsigned int num_cmd_matches = 0;
1004     unsigned int num_alias_matches = 0;
1005     unsigned int num_user_matches = 0;
1006     unsigned int num_user_mw_matches = 0;
1007 
1008     // Look through the command dictionaries one by one, and if we get only one
1009     // match from any of them in toto, then return that, otherwise return an
1010     // empty CommandObjectSP and the list of matches.
1011 
1012     if (HasCommands()) {
1013       num_cmd_matches = AddNamesMatchingPartialString(m_command_dict, cmd_str,
1014                                                       *matches, descriptions);
1015     }
1016 
1017     if (num_cmd_matches == 1) {
1018       cmd.assign(matches->GetStringAtIndex(0));
1019       auto pos = m_command_dict.find(cmd);
1020       if (pos != m_command_dict.end())
1021         real_match_sp = pos->second;
1022     }
1023 
1024     if (include_aliases && HasAliases()) {
1025       num_alias_matches = AddNamesMatchingPartialString(m_alias_dict, cmd_str,
1026                                                         *matches, descriptions);
1027     }
1028 
1029     if (num_alias_matches == 1) {
1030       cmd.assign(matches->GetStringAtIndex(num_cmd_matches));
1031       auto alias_pos = m_alias_dict.find(cmd);
1032       if (alias_pos != m_alias_dict.end())
1033         alias_match_sp = alias_pos->second;
1034     }
1035 
1036     if (HasUserCommands()) {
1037       num_user_matches = AddNamesMatchingPartialString(m_user_dict, cmd_str,
1038                                                        *matches, descriptions);
1039     }
1040 
1041     if (num_user_matches == 1) {
1042       cmd.assign(
1043           matches->GetStringAtIndex(num_cmd_matches + num_alias_matches));
1044 
1045       auto pos = m_user_dict.find(cmd);
1046       if (pos != m_user_dict.end())
1047         user_match_sp = pos->second;
1048     }
1049 
1050     if (HasUserMultiwordCommands()) {
1051       num_user_mw_matches = AddNamesMatchingPartialString(
1052           m_user_mw_dict, cmd_str, *matches, descriptions);
1053     }
1054 
1055     if (num_user_mw_matches == 1) {
1056       cmd.assign(matches->GetStringAtIndex(num_cmd_matches + num_alias_matches +
1057                                            num_user_matches));
1058 
1059       auto pos = m_user_mw_dict.find(cmd);
1060       if (pos != m_user_mw_dict.end())
1061         user_mw_match_sp = pos->second;
1062     }
1063 
1064     // If we got exactly one match, return that, otherwise return the match
1065     // list.
1066 
1067     if (num_user_matches + num_user_mw_matches + num_cmd_matches +
1068             num_alias_matches ==
1069         1) {
1070       if (num_cmd_matches)
1071         return real_match_sp;
1072       else if (num_alias_matches)
1073         return alias_match_sp;
1074       else if (num_user_mw_matches)
1075         return user_mw_match_sp;
1076       else
1077         return user_match_sp;
1078     }
1079   } else if (matches && command_sp) {
1080     matches->AppendString(cmd_str);
1081     if (descriptions)
1082       descriptions->AppendString(command_sp->GetHelp());
1083   }
1084 
1085   return command_sp;
1086 }
1087 
1088 bool CommandInterpreter::AddCommand(llvm::StringRef name,
1089                                     const lldb::CommandObjectSP &cmd_sp,
1090                                     bool can_replace) {
1091   if (cmd_sp.get())
1092     lldbassert((this == &cmd_sp->GetCommandInterpreter()) &&
1093                "tried to add a CommandObject from a different interpreter");
1094 
1095   if (name.empty())
1096     return false;
1097 
1098   cmd_sp->SetIsUserCommand(false);
1099 
1100   std::string name_sstr(name);
1101   auto name_iter = m_command_dict.find(name_sstr);
1102   if (name_iter != m_command_dict.end()) {
1103     if (!can_replace || !name_iter->second->IsRemovable())
1104       return false;
1105     name_iter->second = cmd_sp;
1106   } else {
1107     m_command_dict[name_sstr] = cmd_sp;
1108   }
1109   return true;
1110 }
1111 
1112 Status CommandInterpreter::AddUserCommand(llvm::StringRef name,
1113                                           const lldb::CommandObjectSP &cmd_sp,
1114                                           bool can_replace) {
1115   Status result;
1116   if (cmd_sp.get())
1117     lldbassert((this == &cmd_sp->GetCommandInterpreter()) &&
1118                "tried to add a CommandObject from a different interpreter");
1119   if (name.empty()) {
1120     result.SetErrorString("can't use the empty string for a command name");
1121     return result;
1122   }
1123   // do not allow replacement of internal commands
1124   if (CommandExists(name)) {
1125     result.SetErrorString("can't replace builtin command");
1126     return result;
1127   }
1128 
1129   if (UserCommandExists(name)) {
1130     if (!can_replace) {
1131       result.SetErrorString("user command exists and force replace not set");
1132       return result;
1133     }
1134     if (cmd_sp->IsMultiwordObject()) {
1135       if (!m_user_mw_dict[std::string(name)]->IsRemovable()) {
1136         result.SetErrorString(
1137             "can't replace explicitly non-removable multi-word command");
1138         return result;
1139       }
1140     } else {
1141       if (!m_user_dict[std::string(name)]->IsRemovable()) {
1142         result.SetErrorString("can't replace explicitly non-removable command");
1143         return result;
1144       }
1145     }
1146   }
1147 
1148   cmd_sp->SetIsUserCommand(true);
1149 
1150   if (cmd_sp->IsMultiwordObject())
1151     m_user_mw_dict[std::string(name)] = cmd_sp;
1152   else
1153     m_user_dict[std::string(name)] = cmd_sp;
1154   return result;
1155 }
1156 
1157 CommandObjectSP
1158 CommandInterpreter::GetCommandSPExact(llvm::StringRef cmd_str,
1159                                       bool include_aliases) const {
1160   // Break up the command string into words, in case it's a multi-word command.
1161   Args cmd_words(cmd_str);
1162 
1163   if (cmd_str.empty())
1164     return {};
1165 
1166   if (cmd_words.GetArgumentCount() == 1)
1167     return GetCommandSP(cmd_str, include_aliases, true);
1168 
1169   // We have a multi-word command (seemingly), so we need to do more work.
1170   // First, get the cmd_obj_sp for the first word in the command.
1171   CommandObjectSP cmd_obj_sp =
1172       GetCommandSP(cmd_words.GetArgumentAtIndex(0), include_aliases, true);
1173   if (!cmd_obj_sp)
1174     return {};
1175 
1176   // Loop through the rest of the words in the command (everything passed in
1177   // was supposed to be part of a command name), and find the appropriate
1178   // sub-command SP for each command word....
1179   size_t end = cmd_words.GetArgumentCount();
1180   for (size_t i = 1; i < end; ++i) {
1181     if (!cmd_obj_sp->IsMultiwordObject()) {
1182       // We have more words in the command name, but we don't have a
1183       // multiword object. Fail and return.
1184       return {};
1185     }
1186 
1187     cmd_obj_sp = cmd_obj_sp->GetSubcommandSP(cmd_words.GetArgumentAtIndex(i));
1188     if (!cmd_obj_sp) {
1189       // The sub-command name was invalid.  Fail and return.
1190       return {};
1191     }
1192   }
1193 
1194   // We successfully looped through all the command words and got valid
1195   // command objects for them.
1196   return cmd_obj_sp;
1197 }
1198 
1199 CommandObject *
1200 CommandInterpreter::GetCommandObject(llvm::StringRef cmd_str,
1201                                      StringList *matches,
1202                                      StringList *descriptions) const {
1203   CommandObject *command_obj =
1204       GetCommandSP(cmd_str, false, true, matches, descriptions).get();
1205 
1206   // If we didn't find an exact match to the command string in the commands,
1207   // look in the aliases.
1208 
1209   if (command_obj)
1210     return command_obj;
1211 
1212   command_obj = GetCommandSP(cmd_str, true, true, matches, descriptions).get();
1213 
1214   if (command_obj)
1215     return command_obj;
1216 
1217   // If there wasn't an exact match then look for an inexact one in just the
1218   // commands
1219   command_obj = GetCommandSP(cmd_str, false, false, nullptr).get();
1220 
1221   // Finally, if there wasn't an inexact match among the commands, look for an
1222   // inexact match in both the commands and aliases.
1223 
1224   if (command_obj) {
1225     if (matches)
1226       matches->AppendString(command_obj->GetCommandName());
1227     if (descriptions)
1228       descriptions->AppendString(command_obj->GetHelp());
1229     return command_obj;
1230   }
1231 
1232   return GetCommandSP(cmd_str, true, false, matches, descriptions).get();
1233 }
1234 
1235 CommandObject *CommandInterpreter::GetUserCommandObject(
1236     llvm::StringRef cmd, StringList *matches, StringList *descriptions) const {
1237   std::string cmd_str(cmd);
1238   auto find_exact = [&](const CommandObject::CommandMap &map) {
1239     auto found_elem = map.find(std::string(cmd));
1240     if (found_elem == map.end())
1241       return (CommandObject *)nullptr;
1242     CommandObject *exact_cmd = found_elem->second.get();
1243     if (exact_cmd) {
1244       if (matches)
1245         matches->AppendString(exact_cmd->GetCommandName());
1246       if (descriptions)
1247         descriptions->AppendString(exact_cmd->GetHelp());
1248       return exact_cmd;
1249     }
1250     return (CommandObject *)nullptr;
1251   };
1252 
1253   CommandObject *exact_cmd = find_exact(GetUserCommands());
1254   if (exact_cmd)
1255     return exact_cmd;
1256 
1257   exact_cmd = find_exact(GetUserMultiwordCommands());
1258   if (exact_cmd)
1259     return exact_cmd;
1260 
1261   // We didn't have an exact command, so now look for partial matches.
1262   StringList tmp_list;
1263   StringList *matches_ptr = matches ? matches : &tmp_list;
1264   AddNamesMatchingPartialString(GetUserCommands(), cmd_str, *matches_ptr);
1265   AddNamesMatchingPartialString(GetUserMultiwordCommands(),
1266                                 cmd_str, *matches_ptr);
1267 
1268   return {};
1269 }
1270 
1271 bool CommandInterpreter::CommandExists(llvm::StringRef cmd) const {
1272   return m_command_dict.find(std::string(cmd)) != m_command_dict.end();
1273 }
1274 
1275 bool CommandInterpreter::GetAliasFullName(llvm::StringRef cmd,
1276                                           std::string &full_name) const {
1277   bool exact_match =
1278       (m_alias_dict.find(std::string(cmd)) != m_alias_dict.end());
1279   if (exact_match) {
1280     full_name.assign(std::string(cmd));
1281     return exact_match;
1282   } else {
1283     StringList matches;
1284     size_t num_alias_matches;
1285     num_alias_matches =
1286         AddNamesMatchingPartialString(m_alias_dict, cmd, matches);
1287     if (num_alias_matches == 1) {
1288       // Make sure this isn't shadowing a command in the regular command space:
1289       StringList regular_matches;
1290       const bool include_aliases = false;
1291       const bool exact = false;
1292       CommandObjectSP cmd_obj_sp(
1293           GetCommandSP(cmd, include_aliases, exact, &regular_matches));
1294       if (cmd_obj_sp || regular_matches.GetSize() > 0)
1295         return false;
1296       else {
1297         full_name.assign(matches.GetStringAtIndex(0));
1298         return true;
1299       }
1300     } else
1301       return false;
1302   }
1303 }
1304 
1305 bool CommandInterpreter::AliasExists(llvm::StringRef cmd) const {
1306   return m_alias_dict.find(std::string(cmd)) != m_alias_dict.end();
1307 }
1308 
1309 bool CommandInterpreter::UserCommandExists(llvm::StringRef cmd) const {
1310   return m_user_dict.find(std::string(cmd)) != m_user_dict.end();
1311 }
1312 
1313 bool CommandInterpreter::UserMultiwordCommandExists(llvm::StringRef cmd) const {
1314   return m_user_mw_dict.find(std::string(cmd)) != m_user_mw_dict.end();
1315 }
1316 
1317 CommandAlias *
1318 CommandInterpreter::AddAlias(llvm::StringRef alias_name,
1319                              lldb::CommandObjectSP &command_obj_sp,
1320                              llvm::StringRef args_string) {
1321   if (command_obj_sp.get())
1322     lldbassert((this == &command_obj_sp->GetCommandInterpreter()) &&
1323                "tried to add a CommandObject from a different interpreter");
1324 
1325   std::unique_ptr<CommandAlias> command_alias_up(
1326       new CommandAlias(*this, command_obj_sp, args_string, alias_name));
1327 
1328   if (command_alias_up && command_alias_up->IsValid()) {
1329     m_alias_dict[std::string(alias_name)] =
1330         CommandObjectSP(command_alias_up.get());
1331     return command_alias_up.release();
1332   }
1333 
1334   return nullptr;
1335 }
1336 
1337 bool CommandInterpreter::RemoveAlias(llvm::StringRef alias_name) {
1338   auto pos = m_alias_dict.find(std::string(alias_name));
1339   if (pos != m_alias_dict.end()) {
1340     m_alias_dict.erase(pos);
1341     return true;
1342   }
1343   return false;
1344 }
1345 
1346 bool CommandInterpreter::RemoveCommand(llvm::StringRef cmd) {
1347   auto pos = m_command_dict.find(std::string(cmd));
1348   if (pos != m_command_dict.end()) {
1349     if (pos->second->IsRemovable()) {
1350       // Only regular expression objects or python commands are removable
1351       m_command_dict.erase(pos);
1352       return true;
1353     }
1354   }
1355   return false;
1356 }
1357 
1358 bool CommandInterpreter::RemoveUser(llvm::StringRef user_name) {
1359   CommandObject::CommandMap::iterator pos =
1360       m_user_dict.find(std::string(user_name));
1361   if (pos != m_user_dict.end()) {
1362     m_user_dict.erase(pos);
1363     return true;
1364   }
1365   return false;
1366 }
1367 
1368 bool CommandInterpreter::RemoveUserMultiword(llvm::StringRef multi_name) {
1369   CommandObject::CommandMap::iterator pos =
1370       m_user_mw_dict.find(std::string(multi_name));
1371   if (pos != m_user_mw_dict.end()) {
1372     m_user_mw_dict.erase(pos);
1373     return true;
1374   }
1375   return false;
1376 }
1377 
1378 void CommandInterpreter::GetHelp(CommandReturnObject &result,
1379                                  uint32_t cmd_types) {
1380   llvm::StringRef help_prologue(GetDebugger().GetIOHandlerHelpPrologue());
1381   if (!help_prologue.empty()) {
1382     OutputFormattedHelpText(result.GetOutputStream(), llvm::StringRef(),
1383                             help_prologue);
1384   }
1385 
1386   CommandObject::CommandMap::const_iterator pos;
1387   size_t max_len = FindLongestCommandWord(m_command_dict);
1388 
1389   if ((cmd_types & eCommandTypesBuiltin) == eCommandTypesBuiltin) {
1390     result.AppendMessage("Debugger commands:");
1391     result.AppendMessage("");
1392 
1393     for (pos = m_command_dict.begin(); pos != m_command_dict.end(); ++pos) {
1394       if (!(cmd_types & eCommandTypesHidden) &&
1395           (pos->first.compare(0, 1, "_") == 0))
1396         continue;
1397 
1398       OutputFormattedHelpText(result.GetOutputStream(), pos->first, "--",
1399                               pos->second->GetHelp(), max_len);
1400     }
1401     result.AppendMessage("");
1402   }
1403 
1404   if (!m_alias_dict.empty() &&
1405       ((cmd_types & eCommandTypesAliases) == eCommandTypesAliases)) {
1406     result.AppendMessageWithFormat(
1407         "Current command abbreviations "
1408         "(type '%shelp command alias' for more info):\n",
1409         GetCommandPrefix());
1410     result.AppendMessage("");
1411     max_len = FindLongestCommandWord(m_alias_dict);
1412 
1413     for (auto alias_pos = m_alias_dict.begin(); alias_pos != m_alias_dict.end();
1414          ++alias_pos) {
1415       OutputFormattedHelpText(result.GetOutputStream(), alias_pos->first, "--",
1416                               alias_pos->second->GetHelp(), max_len);
1417     }
1418     result.AppendMessage("");
1419   }
1420 
1421   if (!m_user_dict.empty() &&
1422       ((cmd_types & eCommandTypesUserDef) == eCommandTypesUserDef)) {
1423     result.AppendMessage("Current user-defined commands:");
1424     result.AppendMessage("");
1425     max_len = FindLongestCommandWord(m_user_dict);
1426     for (pos = m_user_dict.begin(); pos != m_user_dict.end(); ++pos) {
1427       OutputFormattedHelpText(result.GetOutputStream(), pos->first, "--",
1428                               pos->second->GetHelp(), max_len);
1429     }
1430     result.AppendMessage("");
1431   }
1432 
1433   if (!m_user_mw_dict.empty() &&
1434       ((cmd_types & eCommandTypesUserMW) == eCommandTypesUserMW)) {
1435     result.AppendMessage("Current user-defined container commands:");
1436     result.AppendMessage("");
1437     max_len = FindLongestCommandWord(m_user_mw_dict);
1438     for (pos = m_user_dict.begin(); pos != m_user_mw_dict.end(); ++pos) {
1439       OutputFormattedHelpText(result.GetOutputStream(), pos->first, "--",
1440                               pos->second->GetHelp(), max_len);
1441     }
1442     result.AppendMessage("");
1443   }
1444 
1445   result.AppendMessageWithFormat(
1446       "For more information on any command, type '%shelp <command-name>'.\n",
1447       GetCommandPrefix());
1448 }
1449 
1450 CommandObject *CommandInterpreter::GetCommandObjectForCommand(
1451     llvm::StringRef &command_string) {
1452   // This function finds the final, lowest-level, alias-resolved command object
1453   // whose 'Execute' function will eventually be invoked by the given command
1454   // line.
1455 
1456   CommandObject *cmd_obj = nullptr;
1457   size_t start = command_string.find_first_not_of(k_white_space);
1458   size_t end = 0;
1459   bool done = false;
1460   while (!done) {
1461     if (start != std::string::npos) {
1462       // Get the next word from command_string.
1463       end = command_string.find_first_of(k_white_space, start);
1464       if (end == std::string::npos)
1465         end = command_string.size();
1466       std::string cmd_word =
1467           std::string(command_string.substr(start, end - start));
1468 
1469       if (cmd_obj == nullptr)
1470         // Since cmd_obj is NULL we are on our first time through this loop.
1471         // Check to see if cmd_word is a valid command or alias.
1472         cmd_obj = GetCommandObject(cmd_word);
1473       else if (cmd_obj->IsMultiwordObject()) {
1474         // Our current object is a multi-word object; see if the cmd_word is a
1475         // valid sub-command for our object.
1476         CommandObject *sub_cmd_obj =
1477             cmd_obj->GetSubcommandObject(cmd_word.c_str());
1478         if (sub_cmd_obj)
1479           cmd_obj = sub_cmd_obj;
1480         else // cmd_word was not a valid sub-command word, so we are done
1481           done = true;
1482       } else
1483         // We have a cmd_obj and it is not a multi-word object, so we are done.
1484         done = true;
1485 
1486       // If we didn't find a valid command object, or our command object is not
1487       // a multi-word object, or we are at the end of the command_string, then
1488       // we are done.  Otherwise, find the start of the next word.
1489 
1490       if (!cmd_obj || !cmd_obj->IsMultiwordObject() ||
1491           end >= command_string.size())
1492         done = true;
1493       else
1494         start = command_string.find_first_not_of(k_white_space, end);
1495     } else
1496       // Unable to find any more words.
1497       done = true;
1498   }
1499 
1500   command_string = command_string.substr(end);
1501   return cmd_obj;
1502 }
1503 
1504 static const char *k_valid_command_chars =
1505     "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_";
1506 static void StripLeadingSpaces(std::string &s) {
1507   if (!s.empty()) {
1508     size_t pos = s.find_first_not_of(k_white_space);
1509     if (pos == std::string::npos)
1510       s.clear();
1511     else if (pos == 0)
1512       return;
1513     s.erase(0, pos);
1514   }
1515 }
1516 
1517 static size_t FindArgumentTerminator(const std::string &s) {
1518   const size_t s_len = s.size();
1519   size_t offset = 0;
1520   while (offset < s_len) {
1521     size_t pos = s.find("--", offset);
1522     if (pos == std::string::npos)
1523       break;
1524     if (pos > 0) {
1525       if (llvm::isSpace(s[pos - 1])) {
1526         // Check if the string ends "\s--" (where \s is a space character) or
1527         // if we have "\s--\s".
1528         if ((pos + 2 >= s_len) || llvm::isSpace(s[pos + 2])) {
1529           return pos;
1530         }
1531       }
1532     }
1533     offset = pos + 2;
1534   }
1535   return std::string::npos;
1536 }
1537 
1538 static bool ExtractCommand(std::string &command_string, std::string &command,
1539                            std::string &suffix, char &quote_char) {
1540   command.clear();
1541   suffix.clear();
1542   StripLeadingSpaces(command_string);
1543 
1544   bool result = false;
1545   quote_char = '\0';
1546 
1547   if (!command_string.empty()) {
1548     const char first_char = command_string[0];
1549     if (first_char == '\'' || first_char == '"') {
1550       quote_char = first_char;
1551       const size_t end_quote_pos = command_string.find(quote_char, 1);
1552       if (end_quote_pos == std::string::npos) {
1553         command.swap(command_string);
1554         command_string.erase();
1555       } else {
1556         command.assign(command_string, 1, end_quote_pos - 1);
1557         if (end_quote_pos + 1 < command_string.size())
1558           command_string.erase(0, command_string.find_first_not_of(
1559                                       k_white_space, end_quote_pos + 1));
1560         else
1561           command_string.erase();
1562       }
1563     } else {
1564       const size_t first_space_pos =
1565           command_string.find_first_of(k_white_space);
1566       if (first_space_pos == std::string::npos) {
1567         command.swap(command_string);
1568         command_string.erase();
1569       } else {
1570         command.assign(command_string, 0, first_space_pos);
1571         command_string.erase(0, command_string.find_first_not_of(
1572                                     k_white_space, first_space_pos));
1573       }
1574     }
1575     result = true;
1576   }
1577 
1578   if (!command.empty()) {
1579     // actual commands can't start with '-' or '_'
1580     if (command[0] != '-' && command[0] != '_') {
1581       size_t pos = command.find_first_not_of(k_valid_command_chars);
1582       if (pos > 0 && pos != std::string::npos) {
1583         suffix.assign(command.begin() + pos, command.end());
1584         command.erase(pos);
1585       }
1586     }
1587   }
1588 
1589   return result;
1590 }
1591 
1592 CommandObject *CommandInterpreter::BuildAliasResult(
1593     llvm::StringRef alias_name, std::string &raw_input_string,
1594     std::string &alias_result, CommandReturnObject &result) {
1595   CommandObject *alias_cmd_obj = nullptr;
1596   Args cmd_args(raw_input_string);
1597   alias_cmd_obj = GetCommandObject(alias_name);
1598   StreamString result_str;
1599 
1600   if (!alias_cmd_obj || !alias_cmd_obj->IsAlias()) {
1601     alias_result.clear();
1602     return alias_cmd_obj;
1603   }
1604   std::pair<CommandObjectSP, OptionArgVectorSP> desugared =
1605       ((CommandAlias *)alias_cmd_obj)->Desugar();
1606   OptionArgVectorSP option_arg_vector_sp = desugared.second;
1607   alias_cmd_obj = desugared.first.get();
1608   std::string alias_name_str = std::string(alias_name);
1609   if ((cmd_args.GetArgumentCount() == 0) ||
1610       (alias_name_str != cmd_args.GetArgumentAtIndex(0)))
1611     cmd_args.Unshift(alias_name_str);
1612 
1613   result_str.Printf("%s", alias_cmd_obj->GetCommandName().str().c_str());
1614 
1615   if (!option_arg_vector_sp.get()) {
1616     alias_result = std::string(result_str.GetString());
1617     return alias_cmd_obj;
1618   }
1619   OptionArgVector *option_arg_vector = option_arg_vector_sp.get();
1620 
1621   int value_type;
1622   std::string option;
1623   std::string value;
1624   for (const auto &entry : *option_arg_vector) {
1625     std::tie(option, value_type, value) = entry;
1626     if (option == "<argument>") {
1627       result_str.Printf(" %s", value.c_str());
1628       continue;
1629     }
1630 
1631     result_str.Printf(" %s", option.c_str());
1632     if (value_type == OptionParser::eNoArgument)
1633       continue;
1634 
1635     if (value_type != OptionParser::eOptionalArgument)
1636       result_str.Printf(" ");
1637     int index = GetOptionArgumentPosition(value.c_str());
1638     if (index == 0)
1639       result_str.Printf("%s", value.c_str());
1640     else if (static_cast<size_t>(index) >= cmd_args.GetArgumentCount()) {
1641 
1642       result.AppendErrorWithFormat("Not enough arguments provided; you "
1643                                    "need at least %d arguments to use "
1644                                    "this alias.\n",
1645                                    index);
1646       return nullptr;
1647     } else {
1648       size_t strpos = raw_input_string.find(cmd_args.GetArgumentAtIndex(index));
1649       if (strpos != std::string::npos)
1650         raw_input_string = raw_input_string.erase(
1651             strpos, strlen(cmd_args.GetArgumentAtIndex(index)));
1652       result_str.Printf("%s", cmd_args.GetArgumentAtIndex(index));
1653     }
1654   }
1655 
1656   alias_result = std::string(result_str.GetString());
1657   return alias_cmd_obj;
1658 }
1659 
1660 Status CommandInterpreter::PreprocessCommand(std::string &command) {
1661   // The command preprocessor needs to do things to the command line before any
1662   // parsing of arguments or anything else is done. The only current stuff that
1663   // gets preprocessed is anything enclosed in backtick ('`') characters is
1664   // evaluated as an expression and the result of the expression must be a
1665   // scalar that can be substituted into the command. An example would be:
1666   // (lldb) memory read `$rsp + 20`
1667   Status error; // Status for any expressions that might not evaluate
1668   size_t start_backtick;
1669   size_t pos = 0;
1670   while ((start_backtick = command.find('`', pos)) != std::string::npos) {
1671     // Stop if an error was encountered during the previous iteration.
1672     if (error.Fail())
1673       break;
1674 
1675     if (start_backtick > 0 && command[start_backtick - 1] == '\\') {
1676       // The backtick was preceded by a '\' character, remove the slash and
1677       // don't treat the backtick as the start of an expression.
1678       command.erase(start_backtick - 1, 1);
1679       // No need to add one to start_backtick since we just deleted a char.
1680       pos = start_backtick;
1681       continue;
1682     }
1683 
1684     const size_t expr_content_start = start_backtick + 1;
1685     const size_t end_backtick = command.find('`', expr_content_start);
1686 
1687     if (end_backtick == std::string::npos) {
1688       // Stop if there's no end backtick.
1689       break;
1690     }
1691 
1692     if (end_backtick == expr_content_start) {
1693       // Skip over empty expression. (two backticks in a row)
1694       command.erase(start_backtick, 2);
1695       continue;
1696     }
1697 
1698     std::string expr_str(command, expr_content_start,
1699                          end_backtick - expr_content_start);
1700 
1701     ExecutionContext exe_ctx(GetExecutionContext());
1702 
1703     // Get a dummy target to allow for calculator mode while processing
1704     // backticks. This also helps break the infinite loop caused when target is
1705     // null.
1706     Target *exe_target = exe_ctx.GetTargetPtr();
1707     Target &target = exe_target ? *exe_target : m_debugger.GetDummyTarget();
1708 
1709     ValueObjectSP expr_result_valobj_sp;
1710 
1711     EvaluateExpressionOptions options;
1712     options.SetCoerceToId(false);
1713     options.SetUnwindOnError(true);
1714     options.SetIgnoreBreakpoints(true);
1715     options.SetKeepInMemory(false);
1716     options.SetTryAllThreads(true);
1717     options.SetTimeout(llvm::None);
1718 
1719     ExpressionResults expr_result =
1720         target.EvaluateExpression(expr_str.c_str(), exe_ctx.GetFramePtr(),
1721                                   expr_result_valobj_sp, options);
1722 
1723     if (expr_result == eExpressionCompleted) {
1724       Scalar scalar;
1725       if (expr_result_valobj_sp)
1726         expr_result_valobj_sp =
1727             expr_result_valobj_sp->GetQualifiedRepresentationIfAvailable(
1728                 expr_result_valobj_sp->GetDynamicValueType(), true);
1729       if (expr_result_valobj_sp->ResolveValue(scalar)) {
1730         command.erase(start_backtick, end_backtick - start_backtick + 1);
1731         StreamString value_strm;
1732         const bool show_type = false;
1733         scalar.GetValue(&value_strm, show_type);
1734         size_t value_string_size = value_strm.GetSize();
1735         if (value_string_size) {
1736           command.insert(start_backtick, std::string(value_strm.GetString()));
1737           pos = start_backtick + value_string_size;
1738           continue;
1739         } else {
1740           error.SetErrorStringWithFormat("expression value didn't result "
1741                                          "in a scalar value for the "
1742                                          "expression '%s'",
1743                                          expr_str.c_str());
1744           break;
1745         }
1746       } else {
1747         error.SetErrorStringWithFormat("expression value didn't result "
1748                                        "in a scalar value for the "
1749                                        "expression '%s'",
1750                                        expr_str.c_str());
1751         break;
1752       }
1753 
1754       continue;
1755     }
1756 
1757     if (expr_result_valobj_sp)
1758       error = expr_result_valobj_sp->GetError();
1759 
1760     if (error.Success()) {
1761       switch (expr_result) {
1762       case eExpressionSetupError:
1763         error.SetErrorStringWithFormat(
1764             "expression setup error for the expression '%s'", expr_str.c_str());
1765         break;
1766       case eExpressionParseError:
1767         error.SetErrorStringWithFormat(
1768             "expression parse error for the expression '%s'", expr_str.c_str());
1769         break;
1770       case eExpressionResultUnavailable:
1771         error.SetErrorStringWithFormat(
1772             "expression error fetching result for the expression '%s'",
1773             expr_str.c_str());
1774         break;
1775       case eExpressionCompleted:
1776         break;
1777       case eExpressionDiscarded:
1778         error.SetErrorStringWithFormat(
1779             "expression discarded for the expression '%s'", expr_str.c_str());
1780         break;
1781       case eExpressionInterrupted:
1782         error.SetErrorStringWithFormat(
1783             "expression interrupted for the expression '%s'", expr_str.c_str());
1784         break;
1785       case eExpressionHitBreakpoint:
1786         error.SetErrorStringWithFormat(
1787             "expression hit breakpoint for the expression '%s'",
1788             expr_str.c_str());
1789         break;
1790       case eExpressionTimedOut:
1791         error.SetErrorStringWithFormat(
1792             "expression timed out for the expression '%s'", expr_str.c_str());
1793         break;
1794       case eExpressionStoppedForDebug:
1795         error.SetErrorStringWithFormat("expression stop at entry point "
1796                                        "for debugging for the "
1797                                        "expression '%s'",
1798                                        expr_str.c_str());
1799         break;
1800       case eExpressionThreadVanished:
1801         error.SetErrorStringWithFormat(
1802             "expression thread vanished for the expression '%s'",
1803             expr_str.c_str());
1804         break;
1805       }
1806     }
1807   }
1808   return error;
1809 }
1810 
1811 bool CommandInterpreter::HandleCommand(const char *command_line,
1812                                        LazyBool lazy_add_to_history,
1813                                        const ExecutionContext &override_context,
1814                                        CommandReturnObject &result) {
1815 
1816   OverrideExecutionContext(override_context);
1817   bool status = HandleCommand(command_line, lazy_add_to_history, result);
1818   RestoreExecutionContext();
1819   return status;
1820 }
1821 
1822 bool CommandInterpreter::HandleCommand(const char *command_line,
1823                                        LazyBool lazy_add_to_history,
1824                                        CommandReturnObject &result) {
1825 
1826   std::string command_string(command_line);
1827   std::string original_command_string(command_line);
1828 
1829   Log *log = GetLog(LLDBLog::Commands);
1830   llvm::PrettyStackTraceFormat stack_trace("HandleCommand(command = \"%s\")",
1831                                    command_line);
1832 
1833   LLDB_LOGF(log, "Processing command: %s", command_line);
1834   LLDB_SCOPED_TIMERF("Processing command: %s.", command_line);
1835 
1836   if (WasInterrupted()) {
1837     result.AppendError("interrupted");
1838     return false;
1839   }
1840 
1841   bool add_to_history;
1842   if (lazy_add_to_history == eLazyBoolCalculate)
1843     add_to_history = (m_command_source_depth == 0);
1844   else
1845     add_to_history = (lazy_add_to_history == eLazyBoolYes);
1846 
1847   m_transcript_stream << "(lldb) " << command_line << '\n';
1848 
1849   bool empty_command = false;
1850   bool comment_command = false;
1851   if (command_string.empty())
1852     empty_command = true;
1853   else {
1854     const char *k_space_characters = "\t\n\v\f\r ";
1855 
1856     size_t non_space = command_string.find_first_not_of(k_space_characters);
1857     // Check for empty line or comment line (lines whose first non-space
1858     // character is the comment character for this interpreter)
1859     if (non_space == std::string::npos)
1860       empty_command = true;
1861     else if (command_string[non_space] == m_comment_char)
1862       comment_command = true;
1863     else if (command_string[non_space] == CommandHistory::g_repeat_char) {
1864       llvm::StringRef search_str(command_string);
1865       search_str = search_str.drop_front(non_space);
1866       if (auto hist_str = m_command_history.FindString(search_str)) {
1867         add_to_history = false;
1868         command_string = std::string(*hist_str);
1869         original_command_string = std::string(*hist_str);
1870       } else {
1871         result.AppendErrorWithFormat("Could not find entry: %s in history",
1872                                      command_string.c_str());
1873         return false;
1874       }
1875     }
1876   }
1877 
1878   if (empty_command) {
1879     if (!GetRepeatPreviousCommand()) {
1880       result.SetStatus(eReturnStatusSuccessFinishNoResult);
1881       return true;
1882     }
1883 
1884     if (m_command_history.IsEmpty()) {
1885       result.AppendError("empty command");
1886       return false;
1887     }
1888 
1889     command_line = m_repeat_command.c_str();
1890     command_string = command_line;
1891     original_command_string = command_line;
1892     if (m_repeat_command.empty()) {
1893       result.AppendError("No auto repeat.");
1894       return false;
1895     }
1896 
1897     add_to_history = false;
1898   } else if (comment_command) {
1899     result.SetStatus(eReturnStatusSuccessFinishNoResult);
1900     return true;
1901   }
1902 
1903   Status error(PreprocessCommand(command_string));
1904 
1905   if (error.Fail()) {
1906     result.AppendError(error.AsCString());
1907     return false;
1908   }
1909 
1910   // Phase 1.
1911 
1912   // Before we do ANY kind of argument processing, we need to figure out what
1913   // the real/final command object is for the specified command.  This gets
1914   // complicated by the fact that the user could have specified an alias, and,
1915   // in translating the alias, there may also be command options and/or even
1916   // data (including raw text strings) that need to be found and inserted into
1917   // the command line as part of the translation.  So this first step is plain
1918   // look-up and replacement, resulting in:
1919   //    1. the command object whose Execute method will actually be called
1920   //    2. a revised command string, with all substitutions and replacements
1921   //       taken care of
1922   // From 1 above, we can determine whether the Execute function wants raw
1923   // input or not.
1924 
1925   CommandObject *cmd_obj = ResolveCommandImpl(command_string, result);
1926 
1927   // Although the user may have abbreviated the command, the command_string now
1928   // has the command expanded to the full name.  For example, if the input was
1929   // "br s -n main", command_string is now "breakpoint set -n main".
1930   if (log) {
1931     llvm::StringRef command_name = cmd_obj ? cmd_obj->GetCommandName() : "<not found>";
1932     LLDB_LOGF(log, "HandleCommand, cmd_obj : '%s'", command_name.str().c_str());
1933     LLDB_LOGF(log, "HandleCommand, (revised) command_string: '%s'",
1934               command_string.c_str());
1935     const bool wants_raw_input =
1936         (cmd_obj != nullptr) ? cmd_obj->WantsRawCommandString() : false;
1937     LLDB_LOGF(log, "HandleCommand, wants_raw_input:'%s'",
1938               wants_raw_input ? "True" : "False");
1939   }
1940 
1941   // Phase 2.
1942   // Take care of things like setting up the history command & calling the
1943   // appropriate Execute method on the CommandObject, with the appropriate
1944   // arguments.
1945 
1946   if (cmd_obj != nullptr) {
1947     // If we got here when empty_command was true, then this command is a
1948     // stored "repeat command" which we should give a chance to produce it's
1949     // repeat command, even though we don't add repeat commands to the history.
1950     if (add_to_history || empty_command) {
1951       Args command_args(command_string);
1952       llvm::Optional<std::string> repeat_command =
1953           cmd_obj->GetRepeatCommand(command_args, 0);
1954       if (repeat_command)
1955         m_repeat_command.assign(*repeat_command);
1956       else
1957         m_repeat_command.assign(original_command_string);
1958     }
1959 
1960     if (add_to_history)
1961       m_command_history.AppendString(original_command_string);
1962 
1963     std::string remainder;
1964     const std::size_t actual_cmd_name_len = cmd_obj->GetCommandName().size();
1965     if (actual_cmd_name_len < command_string.length())
1966       remainder = command_string.substr(actual_cmd_name_len);
1967 
1968     // Remove any initial spaces
1969     size_t pos = remainder.find_first_not_of(k_white_space);
1970     if (pos != 0 && pos != std::string::npos)
1971       remainder.erase(0, pos);
1972 
1973     LLDB_LOGF(
1974         log, "HandleCommand, command line after removing command name(s): '%s'",
1975         remainder.c_str());
1976 
1977     cmd_obj->Execute(remainder.c_str(), result);
1978   }
1979 
1980   LLDB_LOGF(log, "HandleCommand, command %s",
1981             (result.Succeeded() ? "succeeded" : "did not succeed"));
1982 
1983   m_transcript_stream << result.GetOutputData();
1984   m_transcript_stream << result.GetErrorData();
1985 
1986   return result.Succeeded();
1987 }
1988 
1989 void CommandInterpreter::HandleCompletionMatches(CompletionRequest &request) {
1990   bool look_for_subcommand = false;
1991 
1992   // For any of the command completions a unique match will be a complete word.
1993 
1994   if (request.GetParsedLine().GetArgumentCount() == 0) {
1995     // We got nothing on the command line, so return the list of commands
1996     bool include_aliases = true;
1997     StringList new_matches, descriptions;
1998     GetCommandNamesMatchingPartialString("", include_aliases, new_matches,
1999                                          descriptions);
2000     request.AddCompletions(new_matches, descriptions);
2001   } else if (request.GetCursorIndex() == 0) {
2002     // The cursor is in the first argument, so just do a lookup in the
2003     // dictionary.
2004     StringList new_matches, new_descriptions;
2005     CommandObject *cmd_obj =
2006         GetCommandObject(request.GetParsedLine().GetArgumentAtIndex(0),
2007                          &new_matches, &new_descriptions);
2008 
2009     if (new_matches.GetSize() && cmd_obj && cmd_obj->IsMultiwordObject() &&
2010         new_matches.GetStringAtIndex(0) != nullptr &&
2011         strcmp(request.GetParsedLine().GetArgumentAtIndex(0),
2012                new_matches.GetStringAtIndex(0)) == 0) {
2013       if (request.GetParsedLine().GetArgumentCount() != 1) {
2014         look_for_subcommand = true;
2015         new_matches.DeleteStringAtIndex(0);
2016         new_descriptions.DeleteStringAtIndex(0);
2017         request.AppendEmptyArgument();
2018       }
2019     }
2020     request.AddCompletions(new_matches, new_descriptions);
2021   }
2022 
2023   if (request.GetCursorIndex() > 0 || look_for_subcommand) {
2024     // We are completing further on into a commands arguments, so find the
2025     // command and tell it to complete the command. First see if there is a
2026     // matching initial command:
2027     CommandObject *command_object =
2028         GetCommandObject(request.GetParsedLine().GetArgumentAtIndex(0));
2029     if (command_object) {
2030       request.ShiftArguments();
2031       command_object->HandleCompletion(request);
2032     }
2033   }
2034 }
2035 
2036 void CommandInterpreter::HandleCompletion(CompletionRequest &request) {
2037 
2038   // Don't complete comments, and if the line we are completing is just the
2039   // history repeat character, substitute the appropriate history line.
2040   llvm::StringRef first_arg = request.GetParsedLine().GetArgumentAtIndex(0);
2041 
2042   if (!first_arg.empty()) {
2043     if (first_arg.front() == m_comment_char)
2044       return;
2045     if (first_arg.front() == CommandHistory::g_repeat_char) {
2046       if (auto hist_str = m_command_history.FindString(first_arg))
2047         request.AddCompletion(*hist_str, "Previous command history event",
2048                               CompletionMode::RewriteLine);
2049       return;
2050     }
2051   }
2052 
2053   HandleCompletionMatches(request);
2054 }
2055 
2056 llvm::Optional<std::string>
2057 CommandInterpreter::GetAutoSuggestionForCommand(llvm::StringRef line) {
2058   if (line.empty())
2059     return llvm::None;
2060   const size_t s = m_command_history.GetSize();
2061   for (int i = s - 1; i >= 0; --i) {
2062     llvm::StringRef entry = m_command_history.GetStringAtIndex(i);
2063     if (entry.consume_front(line))
2064       return entry.str();
2065   }
2066   return llvm::None;
2067 }
2068 
2069 void CommandInterpreter::UpdatePrompt(llvm::StringRef new_prompt) {
2070   EventSP prompt_change_event_sp(
2071       new Event(eBroadcastBitResetPrompt, new EventDataBytes(new_prompt)));
2072   ;
2073   BroadcastEvent(prompt_change_event_sp);
2074   if (m_command_io_handler_sp)
2075     m_command_io_handler_sp->SetPrompt(new_prompt);
2076 }
2077 
2078 bool CommandInterpreter::Confirm(llvm::StringRef message, bool default_answer) {
2079   // Check AutoConfirm first:
2080   if (m_debugger.GetAutoConfirm())
2081     return default_answer;
2082 
2083   IOHandlerConfirm *confirm =
2084       new IOHandlerConfirm(m_debugger, message, default_answer);
2085   IOHandlerSP io_handler_sp(confirm);
2086   m_debugger.RunIOHandlerSync(io_handler_sp);
2087   return confirm->GetResponse();
2088 }
2089 
2090 const CommandAlias *
2091 CommandInterpreter::GetAlias(llvm::StringRef alias_name) const {
2092   OptionArgVectorSP ret_val;
2093 
2094   auto pos = m_alias_dict.find(std::string(alias_name));
2095   if (pos != m_alias_dict.end())
2096     return (CommandAlias *)pos->second.get();
2097 
2098   return nullptr;
2099 }
2100 
2101 bool CommandInterpreter::HasCommands() const { return (!m_command_dict.empty()); }
2102 
2103 bool CommandInterpreter::HasAliases() const { return (!m_alias_dict.empty()); }
2104 
2105 bool CommandInterpreter::HasUserCommands() const { return (!m_user_dict.empty()); }
2106 
2107 bool CommandInterpreter::HasUserMultiwordCommands() const {
2108   return (!m_user_mw_dict.empty());
2109 }
2110 
2111 bool CommandInterpreter::HasAliasOptions() const { return HasAliases(); }
2112 
2113 void CommandInterpreter::BuildAliasCommandArgs(CommandObject *alias_cmd_obj,
2114                                                const char *alias_name,
2115                                                Args &cmd_args,
2116                                                std::string &raw_input_string,
2117                                                CommandReturnObject &result) {
2118   OptionArgVectorSP option_arg_vector_sp =
2119       GetAlias(alias_name)->GetOptionArguments();
2120 
2121   bool wants_raw_input = alias_cmd_obj->WantsRawCommandString();
2122 
2123   // Make sure that the alias name is the 0th element in cmd_args
2124   std::string alias_name_str = alias_name;
2125   if (alias_name_str != cmd_args.GetArgumentAtIndex(0))
2126     cmd_args.Unshift(alias_name_str);
2127 
2128   Args new_args(alias_cmd_obj->GetCommandName());
2129   if (new_args.GetArgumentCount() == 2)
2130     new_args.Shift();
2131 
2132   if (option_arg_vector_sp.get()) {
2133     if (wants_raw_input) {
2134       // We have a command that both has command options and takes raw input.
2135       // Make *sure* it has a " -- " in the right place in the
2136       // raw_input_string.
2137       size_t pos = raw_input_string.find(" -- ");
2138       if (pos == std::string::npos) {
2139         // None found; assume it goes at the beginning of the raw input string
2140         raw_input_string.insert(0, " -- ");
2141       }
2142     }
2143 
2144     OptionArgVector *option_arg_vector = option_arg_vector_sp.get();
2145     const size_t old_size = cmd_args.GetArgumentCount();
2146     std::vector<bool> used(old_size + 1, false);
2147 
2148     used[0] = true;
2149 
2150     int value_type;
2151     std::string option;
2152     std::string value;
2153     for (const auto &option_entry : *option_arg_vector) {
2154       std::tie(option, value_type, value) = option_entry;
2155       if (option == "<argument>") {
2156         if (!wants_raw_input || (value != "--")) {
2157           // Since we inserted this above, make sure we don't insert it twice
2158           new_args.AppendArgument(value);
2159         }
2160         continue;
2161       }
2162 
2163       if (value_type != OptionParser::eOptionalArgument)
2164         new_args.AppendArgument(option);
2165 
2166       if (value == "<no-argument>")
2167         continue;
2168 
2169       int index = GetOptionArgumentPosition(value.c_str());
2170       if (index == 0) {
2171         // value was NOT a positional argument; must be a real value
2172         if (value_type != OptionParser::eOptionalArgument)
2173           new_args.AppendArgument(value);
2174         else {
2175           new_args.AppendArgument(option + value);
2176         }
2177 
2178       } else if (static_cast<size_t>(index) >= cmd_args.GetArgumentCount()) {
2179         result.AppendErrorWithFormat("Not enough arguments provided; you "
2180                                      "need at least %d arguments to use "
2181                                      "this alias.\n",
2182                                      index);
2183         return;
2184       } else {
2185         // Find and remove cmd_args.GetArgumentAtIndex(i) from raw_input_string
2186         size_t strpos =
2187             raw_input_string.find(cmd_args.GetArgumentAtIndex(index));
2188         if (strpos != std::string::npos) {
2189           raw_input_string = raw_input_string.erase(
2190               strpos, strlen(cmd_args.GetArgumentAtIndex(index)));
2191         }
2192 
2193         if (value_type != OptionParser::eOptionalArgument)
2194           new_args.AppendArgument(cmd_args.GetArgumentAtIndex(index));
2195         else {
2196           new_args.AppendArgument(option + cmd_args.GetArgumentAtIndex(index));
2197         }
2198         used[index] = true;
2199       }
2200     }
2201 
2202     for (auto entry : llvm::enumerate(cmd_args.entries())) {
2203       if (!used[entry.index()] && !wants_raw_input)
2204         new_args.AppendArgument(entry.value().ref());
2205     }
2206 
2207     cmd_args.Clear();
2208     cmd_args.SetArguments(new_args.GetArgumentCount(),
2209                           new_args.GetConstArgumentVector());
2210   } else {
2211     result.SetStatus(eReturnStatusSuccessFinishNoResult);
2212     // This alias was not created with any options; nothing further needs to be
2213     // done, unless it is a command that wants raw input, in which case we need
2214     // to clear the rest of the data from cmd_args, since its in the raw input
2215     // string.
2216     if (wants_raw_input) {
2217       cmd_args.Clear();
2218       cmd_args.SetArguments(new_args.GetArgumentCount(),
2219                             new_args.GetConstArgumentVector());
2220     }
2221     return;
2222   }
2223 
2224   result.SetStatus(eReturnStatusSuccessFinishNoResult);
2225 }
2226 
2227 int CommandInterpreter::GetOptionArgumentPosition(const char *in_string) {
2228   int position = 0; // Any string that isn't an argument position, i.e. '%'
2229                     // followed by an integer, gets a position
2230                     // of zero.
2231 
2232   const char *cptr = in_string;
2233 
2234   // Does it start with '%'
2235   if (cptr[0] == '%') {
2236     ++cptr;
2237 
2238     // Is the rest of it entirely digits?
2239     if (isdigit(cptr[0])) {
2240       const char *start = cptr;
2241       while (isdigit(cptr[0]))
2242         ++cptr;
2243 
2244       // We've gotten to the end of the digits; are we at the end of the
2245       // string?
2246       if (cptr[0] == '\0')
2247         position = atoi(start);
2248     }
2249   }
2250 
2251   return position;
2252 }
2253 
2254 static void GetHomeInitFile(llvm::SmallVectorImpl<char> &init_file,
2255                             llvm::StringRef suffix = {}) {
2256   std::string init_file_name = ".lldbinit";
2257   if (!suffix.empty()) {
2258     init_file_name.append("-");
2259     init_file_name.append(suffix.str());
2260   }
2261 
2262   FileSystem::Instance().GetHomeDirectory(init_file);
2263   llvm::sys::path::append(init_file, init_file_name);
2264 
2265   FileSystem::Instance().Resolve(init_file);
2266 }
2267 
2268 static void GetHomeREPLInitFile(llvm::SmallVectorImpl<char> &init_file,
2269                                 LanguageType language) {
2270   if (language == eLanguageTypeUnknown) {
2271     LanguageSet repl_languages = Language::GetLanguagesSupportingREPLs();
2272     if (auto main_repl_language = repl_languages.GetSingularLanguage())
2273       language = *main_repl_language;
2274     else
2275       return;
2276   }
2277 
2278   std::string init_file_name =
2279       (llvm::Twine(".lldbinit-") +
2280        llvm::Twine(Language::GetNameForLanguageType(language)) +
2281        llvm::Twine("-repl"))
2282           .str();
2283   FileSystem::Instance().GetHomeDirectory(init_file);
2284   llvm::sys::path::append(init_file, init_file_name);
2285   FileSystem::Instance().Resolve(init_file);
2286 }
2287 
2288 static void GetCwdInitFile(llvm::SmallVectorImpl<char> &init_file) {
2289   llvm::StringRef s = ".lldbinit";
2290   init_file.assign(s.begin(), s.end());
2291   FileSystem::Instance().Resolve(init_file);
2292 }
2293 
2294 void CommandInterpreter::SourceInitFile(FileSpec file,
2295                                         CommandReturnObject &result) {
2296   assert(!m_skip_lldbinit_files);
2297 
2298   if (!FileSystem::Instance().Exists(file)) {
2299     result.SetStatus(eReturnStatusSuccessFinishNoResult);
2300     return;
2301   }
2302 
2303   // Use HandleCommand to 'source' the given file; this will do the actual
2304   // broadcasting of the commands back to any appropriate listener (see
2305   // CommandObjectSource::Execute for more details).
2306   const bool saved_batch = SetBatchCommandMode(true);
2307   CommandInterpreterRunOptions options;
2308   options.SetSilent(true);
2309   options.SetPrintErrors(true);
2310   options.SetStopOnError(false);
2311   options.SetStopOnContinue(true);
2312   HandleCommandsFromFile(file, options, result);
2313   SetBatchCommandMode(saved_batch);
2314 }
2315 
2316 void CommandInterpreter::SourceInitFileCwd(CommandReturnObject &result) {
2317   if (m_skip_lldbinit_files) {
2318     result.SetStatus(eReturnStatusSuccessFinishNoResult);
2319     return;
2320   }
2321 
2322   llvm::SmallString<128> init_file;
2323   GetCwdInitFile(init_file);
2324   if (!FileSystem::Instance().Exists(init_file)) {
2325     result.SetStatus(eReturnStatusSuccessFinishNoResult);
2326     return;
2327   }
2328 
2329   LoadCWDlldbinitFile should_load =
2330       Target::GetGlobalProperties().GetLoadCWDlldbinitFile();
2331 
2332   switch (should_load) {
2333   case eLoadCWDlldbinitFalse:
2334     result.SetStatus(eReturnStatusSuccessFinishNoResult);
2335     break;
2336   case eLoadCWDlldbinitTrue:
2337     SourceInitFile(FileSpec(init_file.str()), result);
2338     break;
2339   case eLoadCWDlldbinitWarn: {
2340     llvm::SmallString<128> home_init_file;
2341     GetHomeInitFile(home_init_file);
2342     if (llvm::sys::path::parent_path(init_file) ==
2343         llvm::sys::path::parent_path(home_init_file)) {
2344       result.SetStatus(eReturnStatusSuccessFinishNoResult);
2345     } else {
2346       result.AppendError(InitFileWarning);
2347     }
2348   }
2349   }
2350 }
2351 
2352 /// We will first see if there is an application specific ".lldbinit" file
2353 /// whose name is "~/.lldbinit" followed by a "-" and the name of the program.
2354 /// If this file doesn't exist, we fall back to the REPL init file or the
2355 /// default home init file in "~/.lldbinit".
2356 void CommandInterpreter::SourceInitFileHome(CommandReturnObject &result,
2357                                             bool is_repl) {
2358   if (m_skip_lldbinit_files) {
2359     result.SetStatus(eReturnStatusSuccessFinishNoResult);
2360     return;
2361   }
2362 
2363   llvm::SmallString<128> init_file;
2364 
2365   if (is_repl)
2366     GetHomeREPLInitFile(init_file, GetDebugger().GetREPLLanguage());
2367 
2368   if (init_file.empty())
2369     GetHomeInitFile(init_file);
2370 
2371   if (!m_skip_app_init_files) {
2372     llvm::StringRef program_name =
2373         HostInfo::GetProgramFileSpec().GetFilename().GetStringRef();
2374     llvm::SmallString<128> program_init_file;
2375     GetHomeInitFile(program_init_file, program_name);
2376     if (FileSystem::Instance().Exists(program_init_file))
2377       init_file = program_init_file;
2378   }
2379 
2380   SourceInitFile(FileSpec(init_file.str()), result);
2381 }
2382 
2383 const char *CommandInterpreter::GetCommandPrefix() {
2384   const char *prefix = GetDebugger().GetIOHandlerCommandPrefix();
2385   return prefix == nullptr ? "" : prefix;
2386 }
2387 
2388 PlatformSP CommandInterpreter::GetPlatform(bool prefer_target_platform) {
2389   PlatformSP platform_sp;
2390   if (prefer_target_platform) {
2391     ExecutionContext exe_ctx(GetExecutionContext());
2392     Target *target = exe_ctx.GetTargetPtr();
2393     if (target)
2394       platform_sp = target->GetPlatform();
2395   }
2396 
2397   if (!platform_sp)
2398     platform_sp = m_debugger.GetPlatformList().GetSelectedPlatform();
2399   return platform_sp;
2400 }
2401 
2402 bool CommandInterpreter::DidProcessStopAbnormally() const {
2403   auto exe_ctx = GetExecutionContext();
2404   TargetSP target_sp = exe_ctx.GetTargetSP();
2405   if (!target_sp)
2406     return false;
2407 
2408   ProcessSP process_sp(target_sp->GetProcessSP());
2409   if (!process_sp)
2410     return false;
2411 
2412   if (eStateStopped != process_sp->GetState())
2413     return false;
2414 
2415   for (const auto &thread_sp : process_sp->GetThreadList().Threads()) {
2416     StopInfoSP stop_info = thread_sp->GetStopInfo();
2417     if (!stop_info)
2418       return false;
2419 
2420     const StopReason reason = stop_info->GetStopReason();
2421     if (reason == eStopReasonException ||
2422         reason == eStopReasonInstrumentation ||
2423         reason == eStopReasonProcessorTrace)
2424       return true;
2425 
2426     if (reason == eStopReasonSignal) {
2427       const auto stop_signal = static_cast<int32_t>(stop_info->GetValue());
2428       UnixSignalsSP signals_sp = process_sp->GetUnixSignals();
2429       if (!signals_sp || !signals_sp->SignalIsValid(stop_signal))
2430         // The signal is unknown, treat it as abnormal.
2431         return true;
2432 
2433       const auto sigint_num = signals_sp->GetSignalNumberFromName("SIGINT");
2434       const auto sigstop_num = signals_sp->GetSignalNumberFromName("SIGSTOP");
2435       if ((stop_signal != sigint_num) && (stop_signal != sigstop_num))
2436         // The signal very likely implies a crash.
2437         return true;
2438     }
2439   }
2440 
2441   return false;
2442 }
2443 
2444 void
2445 CommandInterpreter::HandleCommands(const StringList &commands,
2446                                    const ExecutionContext &override_context,
2447                                    const CommandInterpreterRunOptions &options,
2448                                    CommandReturnObject &result) {
2449 
2450   OverrideExecutionContext(override_context);
2451   HandleCommands(commands, options, result);
2452   RestoreExecutionContext();
2453 }
2454 
2455 void CommandInterpreter::HandleCommands(const StringList &commands,
2456                                         const CommandInterpreterRunOptions &options,
2457                                         CommandReturnObject &result) {
2458   size_t num_lines = commands.GetSize();
2459 
2460   // If we are going to continue past a "continue" then we need to run the
2461   // commands synchronously. Make sure you reset this value anywhere you return
2462   // from the function.
2463 
2464   bool old_async_execution = m_debugger.GetAsyncExecution();
2465 
2466   if (!options.GetStopOnContinue()) {
2467     m_debugger.SetAsyncExecution(false);
2468   }
2469 
2470   for (size_t idx = 0; idx < num_lines && !WasInterrupted(); idx++) {
2471     const char *cmd = commands.GetStringAtIndex(idx);
2472     if (cmd[0] == '\0')
2473       continue;
2474 
2475     if (options.GetEchoCommands()) {
2476       // TODO: Add Stream support.
2477       result.AppendMessageWithFormat("%s %s\n",
2478                                      m_debugger.GetPrompt().str().c_str(), cmd);
2479     }
2480 
2481     CommandReturnObject tmp_result(m_debugger.GetUseColor());
2482     tmp_result.SetInteractive(result.GetInteractive());
2483     tmp_result.SetSuppressImmediateOutput(true);
2484 
2485     // We might call into a regex or alias command, in which case the
2486     // add_to_history will get lost.  This m_command_source_depth dingus is the
2487     // way we turn off adding to the history in that case, so set it up here.
2488     if (!options.GetAddToHistory())
2489       m_command_source_depth++;
2490     bool success = HandleCommand(cmd, options.m_add_to_history, tmp_result);
2491     if (!options.GetAddToHistory())
2492       m_command_source_depth--;
2493 
2494     if (options.GetPrintResults()) {
2495       if (tmp_result.Succeeded())
2496         result.AppendMessage(tmp_result.GetOutputData());
2497     }
2498 
2499     if (!success || !tmp_result.Succeeded()) {
2500       llvm::StringRef error_msg = tmp_result.GetErrorData();
2501       if (error_msg.empty())
2502         error_msg = "<unknown error>.\n";
2503       if (options.GetStopOnError()) {
2504         result.AppendErrorWithFormat(
2505             "Aborting reading of commands after command #%" PRIu64
2506             ": '%s' failed with %s",
2507             (uint64_t)idx, cmd, error_msg.str().c_str());
2508         m_debugger.SetAsyncExecution(old_async_execution);
2509         return;
2510       } else if (options.GetPrintResults()) {
2511         result.AppendMessageWithFormat(
2512             "Command #%" PRIu64 " '%s' failed with %s", (uint64_t)idx + 1, cmd,
2513             error_msg.str().c_str());
2514       }
2515     }
2516 
2517     if (result.GetImmediateOutputStream())
2518       result.GetImmediateOutputStream()->Flush();
2519 
2520     if (result.GetImmediateErrorStream())
2521       result.GetImmediateErrorStream()->Flush();
2522 
2523     // N.B. Can't depend on DidChangeProcessState, because the state coming
2524     // into the command execution could be running (for instance in Breakpoint
2525     // Commands. So we check the return value to see if it is has running in
2526     // it.
2527     if ((tmp_result.GetStatus() == eReturnStatusSuccessContinuingNoResult) ||
2528         (tmp_result.GetStatus() == eReturnStatusSuccessContinuingResult)) {
2529       if (options.GetStopOnContinue()) {
2530         // If we caused the target to proceed, and we're going to stop in that
2531         // case, set the status in our real result before returning.  This is
2532         // an error if the continue was not the last command in the set of
2533         // commands to be run.
2534         if (idx != num_lines - 1)
2535           result.AppendErrorWithFormat(
2536               "Aborting reading of commands after command #%" PRIu64
2537               ": '%s' continued the target.\n",
2538               (uint64_t)idx + 1, cmd);
2539         else
2540           result.AppendMessageWithFormat("Command #%" PRIu64
2541                                          " '%s' continued the target.\n",
2542                                          (uint64_t)idx + 1, cmd);
2543 
2544         result.SetStatus(tmp_result.GetStatus());
2545         m_debugger.SetAsyncExecution(old_async_execution);
2546 
2547         return;
2548       }
2549     }
2550 
2551     // Also check for "stop on crash here:
2552     if (tmp_result.GetDidChangeProcessState() && options.GetStopOnCrash() &&
2553         DidProcessStopAbnormally()) {
2554       if (idx != num_lines - 1)
2555         result.AppendErrorWithFormat(
2556             "Aborting reading of commands after command #%" PRIu64
2557             ": '%s' stopped with a signal or exception.\n",
2558             (uint64_t)idx + 1, cmd);
2559       else
2560         result.AppendMessageWithFormat(
2561             "Command #%" PRIu64 " '%s' stopped with a signal or exception.\n",
2562             (uint64_t)idx + 1, cmd);
2563 
2564       result.SetStatus(tmp_result.GetStatus());
2565       m_debugger.SetAsyncExecution(old_async_execution);
2566 
2567       return;
2568     }
2569   }
2570 
2571   result.SetStatus(eReturnStatusSuccessFinishResult);
2572   m_debugger.SetAsyncExecution(old_async_execution);
2573 }
2574 
2575 // Make flags that we can pass into the IOHandler so our delegates can do the
2576 // right thing
2577 enum {
2578   eHandleCommandFlagStopOnContinue = (1u << 0),
2579   eHandleCommandFlagStopOnError = (1u << 1),
2580   eHandleCommandFlagEchoCommand = (1u << 2),
2581   eHandleCommandFlagEchoCommentCommand = (1u << 3),
2582   eHandleCommandFlagPrintResult = (1u << 4),
2583   eHandleCommandFlagPrintErrors = (1u << 5),
2584   eHandleCommandFlagStopOnCrash = (1u << 6)
2585 };
2586 
2587 void CommandInterpreter::HandleCommandsFromFile(
2588     FileSpec &cmd_file, const ExecutionContext &context,
2589     const CommandInterpreterRunOptions &options, CommandReturnObject &result) {
2590   OverrideExecutionContext(context);
2591   HandleCommandsFromFile(cmd_file, options, result);
2592   RestoreExecutionContext();
2593 }
2594 
2595 void CommandInterpreter::HandleCommandsFromFile(FileSpec &cmd_file,
2596     const CommandInterpreterRunOptions &options, CommandReturnObject &result) {
2597   if (!FileSystem::Instance().Exists(cmd_file)) {
2598     result.AppendErrorWithFormat(
2599         "Error reading commands from file %s - file not found.\n",
2600         cmd_file.GetFilename().AsCString("<Unknown>"));
2601     return;
2602   }
2603 
2604   std::string cmd_file_path = cmd_file.GetPath();
2605   auto input_file_up =
2606       FileSystem::Instance().Open(cmd_file, File::eOpenOptionReadOnly);
2607   if (!input_file_up) {
2608     std::string error = llvm::toString(input_file_up.takeError());
2609     result.AppendErrorWithFormatv(
2610         "error: an error occurred read file '{0}': {1}\n", cmd_file_path,
2611         llvm::fmt_consume(input_file_up.takeError()));
2612     return;
2613   }
2614   FileSP input_file_sp = FileSP(std::move(input_file_up.get()));
2615 
2616   Debugger &debugger = GetDebugger();
2617 
2618   uint32_t flags = 0;
2619 
2620   if (options.m_stop_on_continue == eLazyBoolCalculate) {
2621     if (m_command_source_flags.empty()) {
2622       // Stop on continue by default
2623       flags |= eHandleCommandFlagStopOnContinue;
2624     } else if (m_command_source_flags.back() &
2625                eHandleCommandFlagStopOnContinue) {
2626       flags |= eHandleCommandFlagStopOnContinue;
2627     }
2628   } else if (options.m_stop_on_continue == eLazyBoolYes) {
2629     flags |= eHandleCommandFlagStopOnContinue;
2630   }
2631 
2632   if (options.m_stop_on_error == eLazyBoolCalculate) {
2633     if (m_command_source_flags.empty()) {
2634       if (GetStopCmdSourceOnError())
2635         flags |= eHandleCommandFlagStopOnError;
2636     } else if (m_command_source_flags.back() & eHandleCommandFlagStopOnError) {
2637       flags |= eHandleCommandFlagStopOnError;
2638     }
2639   } else if (options.m_stop_on_error == eLazyBoolYes) {
2640     flags |= eHandleCommandFlagStopOnError;
2641   }
2642 
2643   // stop-on-crash can only be set, if it is present in all levels of
2644   // pushed flag sets.
2645   if (options.GetStopOnCrash()) {
2646     if (m_command_source_flags.empty()) {
2647       flags |= eHandleCommandFlagStopOnCrash;
2648     } else if (m_command_source_flags.back() & eHandleCommandFlagStopOnCrash) {
2649       flags |= eHandleCommandFlagStopOnCrash;
2650     }
2651   }
2652 
2653   if (options.m_echo_commands == eLazyBoolCalculate) {
2654     if (m_command_source_flags.empty()) {
2655       // Echo command by default
2656       flags |= eHandleCommandFlagEchoCommand;
2657     } else if (m_command_source_flags.back() & eHandleCommandFlagEchoCommand) {
2658       flags |= eHandleCommandFlagEchoCommand;
2659     }
2660   } else if (options.m_echo_commands == eLazyBoolYes) {
2661     flags |= eHandleCommandFlagEchoCommand;
2662   }
2663 
2664   // We will only ever ask for this flag, if we echo commands in general.
2665   if (options.m_echo_comment_commands == eLazyBoolCalculate) {
2666     if (m_command_source_flags.empty()) {
2667       // Echo comments by default
2668       flags |= eHandleCommandFlagEchoCommentCommand;
2669     } else if (m_command_source_flags.back() &
2670                eHandleCommandFlagEchoCommentCommand) {
2671       flags |= eHandleCommandFlagEchoCommentCommand;
2672     }
2673   } else if (options.m_echo_comment_commands == eLazyBoolYes) {
2674     flags |= eHandleCommandFlagEchoCommentCommand;
2675   }
2676 
2677   if (options.m_print_results == eLazyBoolCalculate) {
2678     if (m_command_source_flags.empty()) {
2679       // Print output by default
2680       flags |= eHandleCommandFlagPrintResult;
2681     } else if (m_command_source_flags.back() & eHandleCommandFlagPrintResult) {
2682       flags |= eHandleCommandFlagPrintResult;
2683     }
2684   } else if (options.m_print_results == eLazyBoolYes) {
2685     flags |= eHandleCommandFlagPrintResult;
2686   }
2687 
2688   if (options.m_print_errors == eLazyBoolCalculate) {
2689     if (m_command_source_flags.empty()) {
2690       // Print output by default
2691       flags |= eHandleCommandFlagPrintErrors;
2692     } else if (m_command_source_flags.back() & eHandleCommandFlagPrintErrors) {
2693       flags |= eHandleCommandFlagPrintErrors;
2694     }
2695   } else if (options.m_print_errors == eLazyBoolYes) {
2696     flags |= eHandleCommandFlagPrintErrors;
2697   }
2698 
2699   if (flags & eHandleCommandFlagPrintResult) {
2700     debugger.GetOutputFile().Printf("Executing commands in '%s'.\n",
2701                                     cmd_file_path.c_str());
2702   }
2703 
2704   // Used for inheriting the right settings when "command source" might
2705   // have nested "command source" commands
2706   lldb::StreamFileSP empty_stream_sp;
2707   m_command_source_flags.push_back(flags);
2708   IOHandlerSP io_handler_sp(new IOHandlerEditline(
2709       debugger, IOHandler::Type::CommandInterpreter, input_file_sp,
2710       empty_stream_sp, // Pass in an empty stream so we inherit the top
2711                        // input reader output stream
2712       empty_stream_sp, // Pass in an empty stream so we inherit the top
2713                        // input reader error stream
2714       flags,
2715       nullptr, // Pass in NULL for "editline_name" so no history is saved,
2716                // or written
2717       debugger.GetPrompt(), llvm::StringRef(),
2718       false, // Not multi-line
2719       debugger.GetUseColor(), 0, *this, nullptr));
2720   const bool old_async_execution = debugger.GetAsyncExecution();
2721 
2722   // Set synchronous execution if we are not stopping on continue
2723   if ((flags & eHandleCommandFlagStopOnContinue) == 0)
2724     debugger.SetAsyncExecution(false);
2725 
2726   m_command_source_depth++;
2727   m_command_source_dirs.push_back(cmd_file.CopyByRemovingLastPathComponent());
2728 
2729   debugger.RunIOHandlerSync(io_handler_sp);
2730   if (!m_command_source_flags.empty())
2731     m_command_source_flags.pop_back();
2732 
2733   m_command_source_dirs.pop_back();
2734   m_command_source_depth--;
2735 
2736   result.SetStatus(eReturnStatusSuccessFinishNoResult);
2737   debugger.SetAsyncExecution(old_async_execution);
2738 }
2739 
2740 bool CommandInterpreter::GetSynchronous() { return m_synchronous_execution; }
2741 
2742 void CommandInterpreter::SetSynchronous(bool value) {
2743   // Asynchronous mode is not supported during reproducer replay.
2744   if (repro::Reproducer::Instance().GetLoader())
2745     return;
2746   m_synchronous_execution = value;
2747 }
2748 
2749 void CommandInterpreter::OutputFormattedHelpText(Stream &strm,
2750                                                  llvm::StringRef prefix,
2751                                                  llvm::StringRef help_text) {
2752   const uint32_t max_columns = m_debugger.GetTerminalWidth();
2753 
2754   size_t line_width_max = max_columns - prefix.size();
2755   if (line_width_max < 16)
2756     line_width_max = help_text.size() + prefix.size();
2757 
2758   strm.IndentMore(prefix.size());
2759   bool prefixed_yet = false;
2760   // Even if we have no help text we still want to emit the command name.
2761   if (help_text.empty())
2762     help_text = "No help text";
2763   while (!help_text.empty()) {
2764     // Prefix the first line, indent subsequent lines to line up
2765     if (!prefixed_yet) {
2766       strm << prefix;
2767       prefixed_yet = true;
2768     } else
2769       strm.Indent();
2770 
2771     // Never print more than the maximum on one line.
2772     llvm::StringRef this_line = help_text.substr(0, line_width_max);
2773 
2774     // Always break on an explicit newline.
2775     std::size_t first_newline = this_line.find_first_of("\n");
2776 
2777     // Don't break on space/tab unless the text is too long to fit on one line.
2778     std::size_t last_space = llvm::StringRef::npos;
2779     if (this_line.size() != help_text.size())
2780       last_space = this_line.find_last_of(" \t");
2781 
2782     // Break at whichever condition triggered first.
2783     this_line = this_line.substr(0, std::min(first_newline, last_space));
2784     strm.PutCString(this_line);
2785     strm.EOL();
2786 
2787     // Remove whitespace / newlines after breaking.
2788     help_text = help_text.drop_front(this_line.size()).ltrim();
2789   }
2790   strm.IndentLess(prefix.size());
2791 }
2792 
2793 void CommandInterpreter::OutputFormattedHelpText(Stream &strm,
2794                                                  llvm::StringRef word_text,
2795                                                  llvm::StringRef separator,
2796                                                  llvm::StringRef help_text,
2797                                                  size_t max_word_len) {
2798   StreamString prefix_stream;
2799   prefix_stream.Printf("  %-*s %*s ", (int)max_word_len, word_text.data(),
2800                        (int)separator.size(), separator.data());
2801   OutputFormattedHelpText(strm, prefix_stream.GetString(), help_text);
2802 }
2803 
2804 void CommandInterpreter::OutputHelpText(Stream &strm, llvm::StringRef word_text,
2805                                         llvm::StringRef separator,
2806                                         llvm::StringRef help_text,
2807                                         uint32_t max_word_len) {
2808   int indent_size = max_word_len + separator.size() + 2;
2809 
2810   strm.IndentMore(indent_size);
2811 
2812   StreamString text_strm;
2813   text_strm.Printf("%-*s ", (int)max_word_len, word_text.data());
2814   text_strm << separator << " " << help_text;
2815 
2816   const uint32_t max_columns = m_debugger.GetTerminalWidth();
2817 
2818   llvm::StringRef text = text_strm.GetString();
2819 
2820   uint32_t chars_left = max_columns;
2821 
2822   auto nextWordLength = [](llvm::StringRef S) {
2823     size_t pos = S.find(' ');
2824     return pos == llvm::StringRef::npos ? S.size() : pos;
2825   };
2826 
2827   while (!text.empty()) {
2828     if (text.front() == '\n' ||
2829         (text.front() == ' ' && nextWordLength(text.ltrim(' ')) > chars_left)) {
2830       strm.EOL();
2831       strm.Indent();
2832       chars_left = max_columns - indent_size;
2833       if (text.front() == '\n')
2834         text = text.drop_front();
2835       else
2836         text = text.ltrim(' ');
2837     } else {
2838       strm.PutChar(text.front());
2839       --chars_left;
2840       text = text.drop_front();
2841     }
2842   }
2843 
2844   strm.EOL();
2845   strm.IndentLess(indent_size);
2846 }
2847 
2848 void CommandInterpreter::FindCommandsForApropos(
2849     llvm::StringRef search_word, StringList &commands_found,
2850     StringList &commands_help, const CommandObject::CommandMap &command_map) {
2851   for (const auto &pair : command_map) {
2852     llvm::StringRef command_name = pair.first;
2853     CommandObject *cmd_obj = pair.second.get();
2854 
2855     const bool search_short_help = true;
2856     const bool search_long_help = false;
2857     const bool search_syntax = false;
2858     const bool search_options = false;
2859     if (command_name.contains_insensitive(search_word) ||
2860         cmd_obj->HelpTextContainsWord(search_word, search_short_help,
2861                                       search_long_help, search_syntax,
2862                                       search_options)) {
2863       commands_found.AppendString(command_name);
2864       commands_help.AppendString(cmd_obj->GetHelp());
2865     }
2866 
2867     if (auto *multiword_cmd = cmd_obj->GetAsMultiwordCommand()) {
2868       StringList subcommands_found;
2869       FindCommandsForApropos(search_word, subcommands_found, commands_help,
2870                              multiword_cmd->GetSubcommandDictionary());
2871       for (const auto &subcommand_name : subcommands_found) {
2872         std::string qualified_name =
2873             (command_name + " " + subcommand_name).str();
2874         commands_found.AppendString(qualified_name);
2875       }
2876     }
2877   }
2878 }
2879 
2880 void CommandInterpreter::FindCommandsForApropos(llvm::StringRef search_word,
2881                                                 StringList &commands_found,
2882                                                 StringList &commands_help,
2883                                                 bool search_builtin_commands,
2884                                                 bool search_user_commands,
2885                                                 bool search_alias_commands,
2886                                                 bool search_user_mw_commands) {
2887   CommandObject::CommandMap::const_iterator pos;
2888 
2889   if (search_builtin_commands)
2890     FindCommandsForApropos(search_word, commands_found, commands_help,
2891                            m_command_dict);
2892 
2893   if (search_user_commands)
2894     FindCommandsForApropos(search_word, commands_found, commands_help,
2895                            m_user_dict);
2896 
2897   if (search_user_mw_commands)
2898     FindCommandsForApropos(search_word, commands_found, commands_help,
2899                            m_user_mw_dict);
2900 
2901   if (search_alias_commands)
2902     FindCommandsForApropos(search_word, commands_found, commands_help,
2903                            m_alias_dict);
2904 }
2905 
2906 ExecutionContext CommandInterpreter::GetExecutionContext() const {
2907   return !m_overriden_exe_contexts.empty()
2908              ? m_overriden_exe_contexts.top()
2909              : m_debugger.GetSelectedExecutionContext();
2910 }
2911 
2912 void CommandInterpreter::OverrideExecutionContext(
2913     const ExecutionContext &override_context) {
2914   m_overriden_exe_contexts.push(override_context);
2915 }
2916 
2917 void CommandInterpreter::RestoreExecutionContext() {
2918   if (!m_overriden_exe_contexts.empty())
2919     m_overriden_exe_contexts.pop();
2920 }
2921 
2922 void CommandInterpreter::GetProcessOutput() {
2923   if (ProcessSP process_sp = GetExecutionContext().GetProcessSP())
2924     m_debugger.FlushProcessOutput(*process_sp, /*flush_stdout*/ true,
2925                                   /*flush_stderr*/ true);
2926 }
2927 
2928 void CommandInterpreter::StartHandlingCommand() {
2929   auto idle_state = CommandHandlingState::eIdle;
2930   if (m_command_state.compare_exchange_strong(
2931           idle_state, CommandHandlingState::eInProgress))
2932     lldbassert(m_iohandler_nesting_level == 0);
2933   else
2934     lldbassert(m_iohandler_nesting_level > 0);
2935   ++m_iohandler_nesting_level;
2936 }
2937 
2938 void CommandInterpreter::FinishHandlingCommand() {
2939   lldbassert(m_iohandler_nesting_level > 0);
2940   if (--m_iohandler_nesting_level == 0) {
2941     auto prev_state = m_command_state.exchange(CommandHandlingState::eIdle);
2942     lldbassert(prev_state != CommandHandlingState::eIdle);
2943   }
2944 }
2945 
2946 bool CommandInterpreter::InterruptCommand() {
2947   auto in_progress = CommandHandlingState::eInProgress;
2948   return m_command_state.compare_exchange_strong(
2949       in_progress, CommandHandlingState::eInterrupted);
2950 }
2951 
2952 bool CommandInterpreter::WasInterrupted() const {
2953   bool was_interrupted =
2954       (m_command_state == CommandHandlingState::eInterrupted);
2955   lldbassert(!was_interrupted || m_iohandler_nesting_level > 0);
2956   return was_interrupted;
2957 }
2958 
2959 void CommandInterpreter::PrintCommandOutput(Stream &stream,
2960                                             llvm::StringRef str) {
2961   // Split the output into lines and poll for interrupt requests
2962   const char *data = str.data();
2963   size_t size = str.size();
2964   while (size > 0 && !WasInterrupted()) {
2965     size_t chunk_size = 0;
2966     for (; chunk_size < size; ++chunk_size) {
2967       lldbassert(data[chunk_size] != '\0');
2968       if (data[chunk_size] == '\n') {
2969         ++chunk_size;
2970         break;
2971       }
2972     }
2973     chunk_size = stream.Write(data, chunk_size);
2974     lldbassert(size >= chunk_size);
2975     data += chunk_size;
2976     size -= chunk_size;
2977   }
2978   if (size > 0) {
2979     stream.Printf("\n... Interrupted.\n");
2980   }
2981 }
2982 
2983 bool CommandInterpreter::EchoCommandNonInteractive(
2984     llvm::StringRef line, const Flags &io_handler_flags) const {
2985   if (!io_handler_flags.Test(eHandleCommandFlagEchoCommand))
2986     return false;
2987 
2988   llvm::StringRef command = line.trim();
2989   if (command.empty())
2990     return true;
2991 
2992   if (command.front() == m_comment_char)
2993     return io_handler_flags.Test(eHandleCommandFlagEchoCommentCommand);
2994 
2995   return true;
2996 }
2997 
2998 void CommandInterpreter::IOHandlerInputComplete(IOHandler &io_handler,
2999                                                 std::string &line) {
3000     // If we were interrupted, bail out...
3001     if (WasInterrupted())
3002       return;
3003 
3004   const bool is_interactive = io_handler.GetIsInteractive();
3005   if (!is_interactive) {
3006     // When we are not interactive, don't execute blank lines. This will happen
3007     // sourcing a commands file. We don't want blank lines to repeat the
3008     // previous command and cause any errors to occur (like redefining an
3009     // alias, get an error and stop parsing the commands file).
3010     if (line.empty())
3011       return;
3012 
3013     // When using a non-interactive file handle (like when sourcing commands
3014     // from a file) we need to echo the command out so we don't just see the
3015     // command output and no command...
3016     if (EchoCommandNonInteractive(line, io_handler.GetFlags()))
3017       io_handler.GetOutputStreamFileSP()->Printf(
3018           "%s%s\n", io_handler.GetPrompt(), line.c_str());
3019   }
3020 
3021   StartHandlingCommand();
3022 
3023   OverrideExecutionContext(m_debugger.GetSelectedExecutionContext());
3024   auto finalize = llvm::make_scope_exit([this]() {
3025     RestoreExecutionContext();
3026   });
3027 
3028   lldb_private::CommandReturnObject result(m_debugger.GetUseColor());
3029   HandleCommand(line.c_str(), eLazyBoolCalculate, result);
3030 
3031   // Now emit the command output text from the command we just executed
3032   if ((result.Succeeded() &&
3033        io_handler.GetFlags().Test(eHandleCommandFlagPrintResult)) ||
3034       io_handler.GetFlags().Test(eHandleCommandFlagPrintErrors)) {
3035     // Display any STDOUT/STDERR _prior_ to emitting the command result text
3036     GetProcessOutput();
3037 
3038     if (!result.GetImmediateOutputStream()) {
3039       llvm::StringRef output = result.GetOutputData();
3040       PrintCommandOutput(*io_handler.GetOutputStreamFileSP(), output);
3041     }
3042 
3043     // Now emit the command error text from the command we just executed
3044     if (!result.GetImmediateErrorStream()) {
3045       llvm::StringRef error = result.GetErrorData();
3046       PrintCommandOutput(*io_handler.GetErrorStreamFileSP(), error);
3047     }
3048   }
3049 
3050   FinishHandlingCommand();
3051 
3052   switch (result.GetStatus()) {
3053   case eReturnStatusInvalid:
3054   case eReturnStatusSuccessFinishNoResult:
3055   case eReturnStatusSuccessFinishResult:
3056   case eReturnStatusStarted:
3057     break;
3058 
3059   case eReturnStatusSuccessContinuingNoResult:
3060   case eReturnStatusSuccessContinuingResult:
3061     if (io_handler.GetFlags().Test(eHandleCommandFlagStopOnContinue))
3062       io_handler.SetIsDone(true);
3063     break;
3064 
3065   case eReturnStatusFailed:
3066     m_result.IncrementNumberOfErrors();
3067     if (io_handler.GetFlags().Test(eHandleCommandFlagStopOnError)) {
3068       m_result.SetResult(lldb::eCommandInterpreterResultCommandError);
3069       io_handler.SetIsDone(true);
3070     }
3071     break;
3072 
3073   case eReturnStatusQuit:
3074     m_result.SetResult(lldb::eCommandInterpreterResultQuitRequested);
3075     io_handler.SetIsDone(true);
3076     break;
3077   }
3078 
3079   // Finally, if we're going to stop on crash, check that here:
3080   if (m_result.IsResult(lldb::eCommandInterpreterResultSuccess) &&
3081       result.GetDidChangeProcessState() &&
3082       io_handler.GetFlags().Test(eHandleCommandFlagStopOnCrash) &&
3083       DidProcessStopAbnormally()) {
3084     io_handler.SetIsDone(true);
3085     m_result.SetResult(lldb::eCommandInterpreterResultInferiorCrash);
3086   }
3087 }
3088 
3089 bool CommandInterpreter::IOHandlerInterrupt(IOHandler &io_handler) {
3090   ExecutionContext exe_ctx(GetExecutionContext());
3091   Process *process = exe_ctx.GetProcessPtr();
3092 
3093   if (InterruptCommand())
3094     return true;
3095 
3096   if (process) {
3097     StateType state = process->GetState();
3098     if (StateIsRunningState(state)) {
3099       process->Halt();
3100       return true; // Don't do any updating when we are running
3101     }
3102   }
3103 
3104   ScriptInterpreter *script_interpreter =
3105       m_debugger.GetScriptInterpreter(false);
3106   if (script_interpreter) {
3107     if (script_interpreter->Interrupt())
3108       return true;
3109   }
3110   return false;
3111 }
3112 
3113 bool CommandInterpreter::SaveTranscript(
3114     CommandReturnObject &result, llvm::Optional<std::string> output_file) {
3115   if (output_file == llvm::None || output_file->empty()) {
3116     std::string now = llvm::to_string(std::chrono::system_clock::now());
3117     std::replace(now.begin(), now.end(), ' ', '_');
3118     const std::string file_name = "lldb_session_" + now + ".log";
3119 
3120     FileSpec save_location = GetSaveSessionDirectory();
3121 
3122     if (!save_location)
3123       save_location = HostInfo::GetGlobalTempDir();
3124 
3125     FileSystem::Instance().Resolve(save_location);
3126     save_location.AppendPathComponent(file_name);
3127     output_file = save_location.GetPath();
3128   }
3129 
3130   auto error_out = [&](llvm::StringRef error_message, std::string description) {
3131     LLDB_LOG(GetLog(LLDBLog::Commands), "{0} ({1}:{2})", error_message,
3132              output_file, description);
3133     result.AppendErrorWithFormatv(
3134         "Failed to save session's transcripts to {0}!", *output_file);
3135     return false;
3136   };
3137 
3138   File::OpenOptions flags = File::eOpenOptionWriteOnly |
3139                             File::eOpenOptionCanCreate |
3140                             File::eOpenOptionTruncate;
3141 
3142   auto opened_file = FileSystem::Instance().Open(FileSpec(*output_file), flags);
3143 
3144   if (!opened_file)
3145     return error_out("Unable to create file",
3146                      llvm::toString(opened_file.takeError()));
3147 
3148   FileUP file = std::move(opened_file.get());
3149 
3150   size_t byte_size = m_transcript_stream.GetSize();
3151 
3152   Status error = file->Write(m_transcript_stream.GetData(), byte_size);
3153 
3154   if (error.Fail() || byte_size != m_transcript_stream.GetSize())
3155     return error_out("Unable to write to destination file",
3156                      "Bytes written do not match transcript size.");
3157 
3158   result.SetStatus(eReturnStatusSuccessFinishNoResult);
3159   result.AppendMessageWithFormat("Session's transcripts saved to %s\n",
3160                                  output_file->c_str());
3161 
3162   return true;
3163 }
3164 
3165 FileSpec CommandInterpreter::GetCurrentSourceDir() {
3166   if (m_command_source_dirs.empty())
3167     return {};
3168   return m_command_source_dirs.back();
3169 }
3170 
3171 void CommandInterpreter::GetLLDBCommandsFromIOHandler(
3172     const char *prompt, IOHandlerDelegate &delegate, void *baton) {
3173   Debugger &debugger = GetDebugger();
3174   IOHandlerSP io_handler_sp(
3175       new IOHandlerEditline(debugger, IOHandler::Type::CommandList,
3176                             "lldb", // Name of input reader for history
3177                             llvm::StringRef(prompt), // Prompt
3178                             llvm::StringRef(),       // Continuation prompt
3179                             true,                    // Get multiple lines
3180                             debugger.GetUseColor(),
3181                             0,         // Don't show line numbers
3182                             delegate,  // IOHandlerDelegate
3183                             nullptr)); // FileShadowCollector
3184 
3185   if (io_handler_sp) {
3186     io_handler_sp->SetUserData(baton);
3187     debugger.RunIOHandlerAsync(io_handler_sp);
3188   }
3189 }
3190 
3191 void CommandInterpreter::GetPythonCommandsFromIOHandler(
3192     const char *prompt, IOHandlerDelegate &delegate, void *baton) {
3193   Debugger &debugger = GetDebugger();
3194   IOHandlerSP io_handler_sp(
3195       new IOHandlerEditline(debugger, IOHandler::Type::PythonCode,
3196                             "lldb-python", // Name of input reader for history
3197                             llvm::StringRef(prompt), // Prompt
3198                             llvm::StringRef(),       // Continuation prompt
3199                             true,                    // Get multiple lines
3200                             debugger.GetUseColor(),
3201                             0,         // Don't show line numbers
3202                             delegate,  // IOHandlerDelegate
3203                             nullptr)); // FileShadowCollector
3204 
3205   if (io_handler_sp) {
3206     io_handler_sp->SetUserData(baton);
3207     debugger.RunIOHandlerAsync(io_handler_sp);
3208   }
3209 }
3210 
3211 bool CommandInterpreter::IsActive() {
3212   return m_debugger.IsTopIOHandler(m_command_io_handler_sp);
3213 }
3214 
3215 lldb::IOHandlerSP
3216 CommandInterpreter::GetIOHandler(bool force_create,
3217                                  CommandInterpreterRunOptions *options) {
3218   // Always re-create the IOHandlerEditline in case the input changed. The old
3219   // instance might have had a non-interactive input and now it does or vice
3220   // versa.
3221   if (force_create || !m_command_io_handler_sp) {
3222     // Always re-create the IOHandlerEditline in case the input changed. The
3223     // old instance might have had a non-interactive input and now it does or
3224     // vice versa.
3225     uint32_t flags = 0;
3226 
3227     if (options) {
3228       if (options->m_stop_on_continue == eLazyBoolYes)
3229         flags |= eHandleCommandFlagStopOnContinue;
3230       if (options->m_stop_on_error == eLazyBoolYes)
3231         flags |= eHandleCommandFlagStopOnError;
3232       if (options->m_stop_on_crash == eLazyBoolYes)
3233         flags |= eHandleCommandFlagStopOnCrash;
3234       if (options->m_echo_commands != eLazyBoolNo)
3235         flags |= eHandleCommandFlagEchoCommand;
3236       if (options->m_echo_comment_commands != eLazyBoolNo)
3237         flags |= eHandleCommandFlagEchoCommentCommand;
3238       if (options->m_print_results != eLazyBoolNo)
3239         flags |= eHandleCommandFlagPrintResult;
3240       if (options->m_print_errors != eLazyBoolNo)
3241         flags |= eHandleCommandFlagPrintErrors;
3242     } else {
3243       flags = eHandleCommandFlagEchoCommand | eHandleCommandFlagPrintResult |
3244               eHandleCommandFlagPrintErrors;
3245     }
3246 
3247     m_command_io_handler_sp = std::make_shared<IOHandlerEditline>(
3248         m_debugger, IOHandler::Type::CommandInterpreter,
3249         m_debugger.GetInputFileSP(), m_debugger.GetOutputStreamSP(),
3250         m_debugger.GetErrorStreamSP(), flags, "lldb", m_debugger.GetPrompt(),
3251         llvm::StringRef(), // Continuation prompt
3252         false, // Don't enable multiple line input, just single line commands
3253         m_debugger.GetUseColor(),
3254         0,     // Don't show line numbers
3255         *this, // IOHandlerDelegate
3256         GetDebugger().GetInputRecorder());
3257   }
3258   return m_command_io_handler_sp;
3259 }
3260 
3261 CommandInterpreterRunResult CommandInterpreter::RunCommandInterpreter(
3262     CommandInterpreterRunOptions &options) {
3263   // Always re-create the command interpreter when we run it in case any file
3264   // handles have changed.
3265   bool force_create = true;
3266   m_debugger.RunIOHandlerAsync(GetIOHandler(force_create, &options));
3267   m_result = CommandInterpreterRunResult();
3268 
3269   if (options.GetAutoHandleEvents())
3270     m_debugger.StartEventHandlerThread();
3271 
3272   if (options.GetSpawnThread()) {
3273     m_debugger.StartIOHandlerThread();
3274   } else {
3275     m_debugger.RunIOHandlers();
3276 
3277     if (options.GetAutoHandleEvents())
3278       m_debugger.StopEventHandlerThread();
3279   }
3280 
3281   return m_result;
3282 }
3283 
3284 CommandObject *
3285 CommandInterpreter::ResolveCommandImpl(std::string &command_line,
3286                                        CommandReturnObject &result) {
3287   std::string scratch_command(command_line); // working copy so we don't modify
3288                                              // command_line unless we succeed
3289   CommandObject *cmd_obj = nullptr;
3290   StreamString revised_command_line;
3291   bool wants_raw_input = false;
3292   std::string next_word;
3293   StringList matches;
3294   bool done = false;
3295   while (!done) {
3296     char quote_char = '\0';
3297     std::string suffix;
3298     ExtractCommand(scratch_command, next_word, suffix, quote_char);
3299     if (cmd_obj == nullptr) {
3300       std::string full_name;
3301       bool is_alias = GetAliasFullName(next_word, full_name);
3302       cmd_obj = GetCommandObject(next_word, &matches);
3303       bool is_real_command =
3304           (!is_alias) || (cmd_obj != nullptr && !cmd_obj->IsAlias());
3305       if (!is_real_command) {
3306         matches.Clear();
3307         std::string alias_result;
3308         cmd_obj =
3309             BuildAliasResult(full_name, scratch_command, alias_result, result);
3310         revised_command_line.Printf("%s", alias_result.c_str());
3311         if (cmd_obj) {
3312           wants_raw_input = cmd_obj->WantsRawCommandString();
3313         }
3314       } else {
3315         if (cmd_obj) {
3316           llvm::StringRef cmd_name = cmd_obj->GetCommandName();
3317           revised_command_line.Printf("%s", cmd_name.str().c_str());
3318           wants_raw_input = cmd_obj->WantsRawCommandString();
3319         } else {
3320           revised_command_line.Printf("%s", next_word.c_str());
3321         }
3322       }
3323     } else {
3324       if (cmd_obj->IsMultiwordObject()) {
3325         CommandObject *sub_cmd_obj =
3326             cmd_obj->GetSubcommandObject(next_word.c_str());
3327         if (sub_cmd_obj) {
3328           // The subcommand's name includes the parent command's name, so
3329           // restart rather than append to the revised_command_line.
3330           llvm::StringRef sub_cmd_name = sub_cmd_obj->GetCommandName();
3331           revised_command_line.Clear();
3332           revised_command_line.Printf("%s", sub_cmd_name.str().c_str());
3333           cmd_obj = sub_cmd_obj;
3334           wants_raw_input = cmd_obj->WantsRawCommandString();
3335         } else {
3336           if (quote_char)
3337             revised_command_line.Printf(" %c%s%s%c", quote_char,
3338                                         next_word.c_str(), suffix.c_str(),
3339                                         quote_char);
3340           else
3341             revised_command_line.Printf(" %s%s", next_word.c_str(),
3342                                         suffix.c_str());
3343           done = true;
3344         }
3345       } else {
3346         if (quote_char)
3347           revised_command_line.Printf(" %c%s%s%c", quote_char,
3348                                       next_word.c_str(), suffix.c_str(),
3349                                       quote_char);
3350         else
3351           revised_command_line.Printf(" %s%s", next_word.c_str(),
3352                                       suffix.c_str());
3353         done = true;
3354       }
3355     }
3356 
3357     if (cmd_obj == nullptr) {
3358       const size_t num_matches = matches.GetSize();
3359       if (matches.GetSize() > 1) {
3360         StreamString error_msg;
3361         error_msg.Printf("Ambiguous command '%s'. Possible matches:\n",
3362                          next_word.c_str());
3363 
3364         for (uint32_t i = 0; i < num_matches; ++i) {
3365           error_msg.Printf("\t%s\n", matches.GetStringAtIndex(i));
3366         }
3367         result.AppendRawError(error_msg.GetString());
3368       } else {
3369         // We didn't have only one match, otherwise we wouldn't get here.
3370         lldbassert(num_matches == 0);
3371         result.AppendErrorWithFormat("'%s' is not a valid command.\n",
3372                                      next_word.c_str());
3373       }
3374       return nullptr;
3375     }
3376 
3377     if (cmd_obj->IsMultiwordObject()) {
3378       if (!suffix.empty()) {
3379         result.AppendErrorWithFormat(
3380             "command '%s' did not recognize '%s%s%s' as valid (subcommand "
3381             "might be invalid).\n",
3382             cmd_obj->GetCommandName().str().c_str(),
3383             next_word.empty() ? "" : next_word.c_str(),
3384             next_word.empty() ? " -- " : " ", suffix.c_str());
3385         return nullptr;
3386       }
3387     } else {
3388       // If we found a normal command, we are done
3389       done = true;
3390       if (!suffix.empty()) {
3391         switch (suffix[0]) {
3392         case '/':
3393           // GDB format suffixes
3394           {
3395             Options *command_options = cmd_obj->GetOptions();
3396             if (command_options &&
3397                 command_options->SupportsLongOption("gdb-format")) {
3398               std::string gdb_format_option("--gdb-format=");
3399               gdb_format_option += (suffix.c_str() + 1);
3400 
3401               std::string cmd = std::string(revised_command_line.GetString());
3402               size_t arg_terminator_idx = FindArgumentTerminator(cmd);
3403               if (arg_terminator_idx != std::string::npos) {
3404                 // Insert the gdb format option before the "--" that terminates
3405                 // options
3406                 gdb_format_option.append(1, ' ');
3407                 cmd.insert(arg_terminator_idx, gdb_format_option);
3408                 revised_command_line.Clear();
3409                 revised_command_line.PutCString(cmd);
3410               } else
3411                 revised_command_line.Printf(" %s", gdb_format_option.c_str());
3412 
3413               if (wants_raw_input &&
3414                   FindArgumentTerminator(cmd) == std::string::npos)
3415                 revised_command_line.PutCString(" --");
3416             } else {
3417               result.AppendErrorWithFormat(
3418                   "the '%s' command doesn't support the --gdb-format option\n",
3419                   cmd_obj->GetCommandName().str().c_str());
3420               return nullptr;
3421             }
3422           }
3423           break;
3424 
3425         default:
3426           result.AppendErrorWithFormat(
3427               "unknown command shorthand suffix: '%s'\n", suffix.c_str());
3428           return nullptr;
3429         }
3430       }
3431     }
3432     if (scratch_command.empty())
3433       done = true;
3434   }
3435 
3436   if (!scratch_command.empty())
3437     revised_command_line.Printf(" %s", scratch_command.c_str());
3438 
3439   if (cmd_obj != nullptr)
3440     command_line = std::string(revised_command_line.GetString());
3441 
3442   return cmd_obj;
3443 }
3444