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 (add_to_history) {
1948       Args command_args(command_string);
1949       const char *repeat_command = cmd_obj->GetRepeatCommand(command_args, 0);
1950       if (repeat_command != nullptr)
1951         m_repeat_command.assign(repeat_command);
1952       else
1953         m_repeat_command.assign(original_command_string);
1954 
1955       m_command_history.AppendString(original_command_string);
1956     }
1957 
1958     std::string remainder;
1959     const std::size_t actual_cmd_name_len = cmd_obj->GetCommandName().size();
1960     if (actual_cmd_name_len < command_string.length())
1961       remainder = command_string.substr(actual_cmd_name_len);
1962 
1963     // Remove any initial spaces
1964     size_t pos = remainder.find_first_not_of(k_white_space);
1965     if (pos != 0 && pos != std::string::npos)
1966       remainder.erase(0, pos);
1967 
1968     LLDB_LOGF(
1969         log, "HandleCommand, command line after removing command name(s): '%s'",
1970         remainder.c_str());
1971 
1972     cmd_obj->Execute(remainder.c_str(), result);
1973   }
1974 
1975   LLDB_LOGF(log, "HandleCommand, command %s",
1976             (result.Succeeded() ? "succeeded" : "did not succeed"));
1977 
1978   m_transcript_stream << result.GetOutputData();
1979   m_transcript_stream << result.GetErrorData();
1980 
1981   return result.Succeeded();
1982 }
1983 
1984 void CommandInterpreter::HandleCompletionMatches(CompletionRequest &request) {
1985   bool look_for_subcommand = false;
1986 
1987   // For any of the command completions a unique match will be a complete word.
1988 
1989   if (request.GetParsedLine().GetArgumentCount() == 0) {
1990     // We got nothing on the command line, so return the list of commands
1991     bool include_aliases = true;
1992     StringList new_matches, descriptions;
1993     GetCommandNamesMatchingPartialString("", include_aliases, new_matches,
1994                                          descriptions);
1995     request.AddCompletions(new_matches, descriptions);
1996   } else if (request.GetCursorIndex() == 0) {
1997     // The cursor is in the first argument, so just do a lookup in the
1998     // dictionary.
1999     StringList new_matches, new_descriptions;
2000     CommandObject *cmd_obj =
2001         GetCommandObject(request.GetParsedLine().GetArgumentAtIndex(0),
2002                          &new_matches, &new_descriptions);
2003 
2004     if (new_matches.GetSize() && cmd_obj && cmd_obj->IsMultiwordObject() &&
2005         new_matches.GetStringAtIndex(0) != nullptr &&
2006         strcmp(request.GetParsedLine().GetArgumentAtIndex(0),
2007                new_matches.GetStringAtIndex(0)) == 0) {
2008       if (request.GetParsedLine().GetArgumentCount() != 1) {
2009         look_for_subcommand = true;
2010         new_matches.DeleteStringAtIndex(0);
2011         new_descriptions.DeleteStringAtIndex(0);
2012         request.AppendEmptyArgument();
2013       }
2014     }
2015     request.AddCompletions(new_matches, new_descriptions);
2016   }
2017 
2018   if (request.GetCursorIndex() > 0 || look_for_subcommand) {
2019     // We are completing further on into a commands arguments, so find the
2020     // command and tell it to complete the command. First see if there is a
2021     // matching initial command:
2022     CommandObject *command_object =
2023         GetCommandObject(request.GetParsedLine().GetArgumentAtIndex(0));
2024     if (command_object) {
2025       request.ShiftArguments();
2026       command_object->HandleCompletion(request);
2027     }
2028   }
2029 }
2030 
2031 void CommandInterpreter::HandleCompletion(CompletionRequest &request) {
2032 
2033   // Don't complete comments, and if the line we are completing is just the
2034   // history repeat character, substitute the appropriate history line.
2035   llvm::StringRef first_arg = request.GetParsedLine().GetArgumentAtIndex(0);
2036 
2037   if (!first_arg.empty()) {
2038     if (first_arg.front() == m_comment_char)
2039       return;
2040     if (first_arg.front() == CommandHistory::g_repeat_char) {
2041       if (auto hist_str = m_command_history.FindString(first_arg))
2042         request.AddCompletion(*hist_str, "Previous command history event",
2043                               CompletionMode::RewriteLine);
2044       return;
2045     }
2046   }
2047 
2048   HandleCompletionMatches(request);
2049 }
2050 
2051 llvm::Optional<std::string>
2052 CommandInterpreter::GetAutoSuggestionForCommand(llvm::StringRef line) {
2053   if (line.empty())
2054     return llvm::None;
2055   const size_t s = m_command_history.GetSize();
2056   for (int i = s - 1; i >= 0; --i) {
2057     llvm::StringRef entry = m_command_history.GetStringAtIndex(i);
2058     if (entry.consume_front(line))
2059       return entry.str();
2060   }
2061   return llvm::None;
2062 }
2063 
2064 void CommandInterpreter::UpdatePrompt(llvm::StringRef new_prompt) {
2065   EventSP prompt_change_event_sp(
2066       new Event(eBroadcastBitResetPrompt, new EventDataBytes(new_prompt)));
2067   ;
2068   BroadcastEvent(prompt_change_event_sp);
2069   if (m_command_io_handler_sp)
2070     m_command_io_handler_sp->SetPrompt(new_prompt);
2071 }
2072 
2073 bool CommandInterpreter::Confirm(llvm::StringRef message, bool default_answer) {
2074   // Check AutoConfirm first:
2075   if (m_debugger.GetAutoConfirm())
2076     return default_answer;
2077 
2078   IOHandlerConfirm *confirm =
2079       new IOHandlerConfirm(m_debugger, message, default_answer);
2080   IOHandlerSP io_handler_sp(confirm);
2081   m_debugger.RunIOHandlerSync(io_handler_sp);
2082   return confirm->GetResponse();
2083 }
2084 
2085 const CommandAlias *
2086 CommandInterpreter::GetAlias(llvm::StringRef alias_name) const {
2087   OptionArgVectorSP ret_val;
2088 
2089   auto pos = m_alias_dict.find(std::string(alias_name));
2090   if (pos != m_alias_dict.end())
2091     return (CommandAlias *)pos->second.get();
2092 
2093   return nullptr;
2094 }
2095 
2096 bool CommandInterpreter::HasCommands() const { return (!m_command_dict.empty()); }
2097 
2098 bool CommandInterpreter::HasAliases() const { return (!m_alias_dict.empty()); }
2099 
2100 bool CommandInterpreter::HasUserCommands() const { return (!m_user_dict.empty()); }
2101 
2102 bool CommandInterpreter::HasUserMultiwordCommands() const {
2103   return (!m_user_mw_dict.empty());
2104 }
2105 
2106 bool CommandInterpreter::HasAliasOptions() const { return HasAliases(); }
2107 
2108 void CommandInterpreter::BuildAliasCommandArgs(CommandObject *alias_cmd_obj,
2109                                                const char *alias_name,
2110                                                Args &cmd_args,
2111                                                std::string &raw_input_string,
2112                                                CommandReturnObject &result) {
2113   OptionArgVectorSP option_arg_vector_sp =
2114       GetAlias(alias_name)->GetOptionArguments();
2115 
2116   bool wants_raw_input = alias_cmd_obj->WantsRawCommandString();
2117 
2118   // Make sure that the alias name is the 0th element in cmd_args
2119   std::string alias_name_str = alias_name;
2120   if (alias_name_str != cmd_args.GetArgumentAtIndex(0))
2121     cmd_args.Unshift(alias_name_str);
2122 
2123   Args new_args(alias_cmd_obj->GetCommandName());
2124   if (new_args.GetArgumentCount() == 2)
2125     new_args.Shift();
2126 
2127   if (option_arg_vector_sp.get()) {
2128     if (wants_raw_input) {
2129       // We have a command that both has command options and takes raw input.
2130       // Make *sure* it has a " -- " in the right place in the
2131       // raw_input_string.
2132       size_t pos = raw_input_string.find(" -- ");
2133       if (pos == std::string::npos) {
2134         // None found; assume it goes at the beginning of the raw input string
2135         raw_input_string.insert(0, " -- ");
2136       }
2137     }
2138 
2139     OptionArgVector *option_arg_vector = option_arg_vector_sp.get();
2140     const size_t old_size = cmd_args.GetArgumentCount();
2141     std::vector<bool> used(old_size + 1, false);
2142 
2143     used[0] = true;
2144 
2145     int value_type;
2146     std::string option;
2147     std::string value;
2148     for (const auto &option_entry : *option_arg_vector) {
2149       std::tie(option, value_type, value) = option_entry;
2150       if (option == "<argument>") {
2151         if (!wants_raw_input || (value != "--")) {
2152           // Since we inserted this above, make sure we don't insert it twice
2153           new_args.AppendArgument(value);
2154         }
2155         continue;
2156       }
2157 
2158       if (value_type != OptionParser::eOptionalArgument)
2159         new_args.AppendArgument(option);
2160 
2161       if (value == "<no-argument>")
2162         continue;
2163 
2164       int index = GetOptionArgumentPosition(value.c_str());
2165       if (index == 0) {
2166         // value was NOT a positional argument; must be a real value
2167         if (value_type != OptionParser::eOptionalArgument)
2168           new_args.AppendArgument(value);
2169         else {
2170           new_args.AppendArgument(option + value);
2171         }
2172 
2173       } else if (static_cast<size_t>(index) >= cmd_args.GetArgumentCount()) {
2174         result.AppendErrorWithFormat("Not enough arguments provided; you "
2175                                      "need at least %d arguments to use "
2176                                      "this alias.\n",
2177                                      index);
2178         return;
2179       } else {
2180         // Find and remove cmd_args.GetArgumentAtIndex(i) from raw_input_string
2181         size_t strpos =
2182             raw_input_string.find(cmd_args.GetArgumentAtIndex(index));
2183         if (strpos != std::string::npos) {
2184           raw_input_string = raw_input_string.erase(
2185               strpos, strlen(cmd_args.GetArgumentAtIndex(index)));
2186         }
2187 
2188         if (value_type != OptionParser::eOptionalArgument)
2189           new_args.AppendArgument(cmd_args.GetArgumentAtIndex(index));
2190         else {
2191           new_args.AppendArgument(option + cmd_args.GetArgumentAtIndex(index));
2192         }
2193         used[index] = true;
2194       }
2195     }
2196 
2197     for (auto entry : llvm::enumerate(cmd_args.entries())) {
2198       if (!used[entry.index()] && !wants_raw_input)
2199         new_args.AppendArgument(entry.value().ref());
2200     }
2201 
2202     cmd_args.Clear();
2203     cmd_args.SetArguments(new_args.GetArgumentCount(),
2204                           new_args.GetConstArgumentVector());
2205   } else {
2206     result.SetStatus(eReturnStatusSuccessFinishNoResult);
2207     // This alias was not created with any options; nothing further needs to be
2208     // done, unless it is a command that wants raw input, in which case we need
2209     // to clear the rest of the data from cmd_args, since its in the raw input
2210     // string.
2211     if (wants_raw_input) {
2212       cmd_args.Clear();
2213       cmd_args.SetArguments(new_args.GetArgumentCount(),
2214                             new_args.GetConstArgumentVector());
2215     }
2216     return;
2217   }
2218 
2219   result.SetStatus(eReturnStatusSuccessFinishNoResult);
2220 }
2221 
2222 int CommandInterpreter::GetOptionArgumentPosition(const char *in_string) {
2223   int position = 0; // Any string that isn't an argument position, i.e. '%'
2224                     // followed by an integer, gets a position
2225                     // of zero.
2226 
2227   const char *cptr = in_string;
2228 
2229   // Does it start with '%'
2230   if (cptr[0] == '%') {
2231     ++cptr;
2232 
2233     // Is the rest of it entirely digits?
2234     if (isdigit(cptr[0])) {
2235       const char *start = cptr;
2236       while (isdigit(cptr[0]))
2237         ++cptr;
2238 
2239       // We've gotten to the end of the digits; are we at the end of the
2240       // string?
2241       if (cptr[0] == '\0')
2242         position = atoi(start);
2243     }
2244   }
2245 
2246   return position;
2247 }
2248 
2249 static void GetHomeInitFile(llvm::SmallVectorImpl<char> &init_file,
2250                             llvm::StringRef suffix = {}) {
2251   std::string init_file_name = ".lldbinit";
2252   if (!suffix.empty()) {
2253     init_file_name.append("-");
2254     init_file_name.append(suffix.str());
2255   }
2256 
2257   FileSystem::Instance().GetHomeDirectory(init_file);
2258   llvm::sys::path::append(init_file, init_file_name);
2259 
2260   FileSystem::Instance().Resolve(init_file);
2261 }
2262 
2263 static void GetHomeREPLInitFile(llvm::SmallVectorImpl<char> &init_file,
2264                                 LanguageType language) {
2265   if (language == eLanguageTypeUnknown) {
2266     LanguageSet repl_languages = Language::GetLanguagesSupportingREPLs();
2267     if (auto main_repl_language = repl_languages.GetSingularLanguage())
2268       language = *main_repl_language;
2269     else
2270       return;
2271   }
2272 
2273   std::string init_file_name =
2274       (llvm::Twine(".lldbinit-") +
2275        llvm::Twine(Language::GetNameForLanguageType(language)) +
2276        llvm::Twine("-repl"))
2277           .str();
2278   FileSystem::Instance().GetHomeDirectory(init_file);
2279   llvm::sys::path::append(init_file, init_file_name);
2280   FileSystem::Instance().Resolve(init_file);
2281 }
2282 
2283 static void GetCwdInitFile(llvm::SmallVectorImpl<char> &init_file) {
2284   llvm::StringRef s = ".lldbinit";
2285   init_file.assign(s.begin(), s.end());
2286   FileSystem::Instance().Resolve(init_file);
2287 }
2288 
2289 void CommandInterpreter::SourceInitFile(FileSpec file,
2290                                         CommandReturnObject &result) {
2291   assert(!m_skip_lldbinit_files);
2292 
2293   if (!FileSystem::Instance().Exists(file)) {
2294     result.SetStatus(eReturnStatusSuccessFinishNoResult);
2295     return;
2296   }
2297 
2298   // Use HandleCommand to 'source' the given file; this will do the actual
2299   // broadcasting of the commands back to any appropriate listener (see
2300   // CommandObjectSource::Execute for more details).
2301   const bool saved_batch = SetBatchCommandMode(true);
2302   CommandInterpreterRunOptions options;
2303   options.SetSilent(true);
2304   options.SetPrintErrors(true);
2305   options.SetStopOnError(false);
2306   options.SetStopOnContinue(true);
2307   HandleCommandsFromFile(file, options, result);
2308   SetBatchCommandMode(saved_batch);
2309 }
2310 
2311 void CommandInterpreter::SourceInitFileCwd(CommandReturnObject &result) {
2312   if (m_skip_lldbinit_files) {
2313     result.SetStatus(eReturnStatusSuccessFinishNoResult);
2314     return;
2315   }
2316 
2317   llvm::SmallString<128> init_file;
2318   GetCwdInitFile(init_file);
2319   if (!FileSystem::Instance().Exists(init_file)) {
2320     result.SetStatus(eReturnStatusSuccessFinishNoResult);
2321     return;
2322   }
2323 
2324   LoadCWDlldbinitFile should_load =
2325       Target::GetGlobalProperties().GetLoadCWDlldbinitFile();
2326 
2327   switch (should_load) {
2328   case eLoadCWDlldbinitFalse:
2329     result.SetStatus(eReturnStatusSuccessFinishNoResult);
2330     break;
2331   case eLoadCWDlldbinitTrue:
2332     SourceInitFile(FileSpec(init_file.str()), result);
2333     break;
2334   case eLoadCWDlldbinitWarn: {
2335     llvm::SmallString<128> home_init_file;
2336     GetHomeInitFile(home_init_file);
2337     if (llvm::sys::path::parent_path(init_file) ==
2338         llvm::sys::path::parent_path(home_init_file)) {
2339       result.SetStatus(eReturnStatusSuccessFinishNoResult);
2340     } else {
2341       result.AppendError(InitFileWarning);
2342     }
2343   }
2344   }
2345 }
2346 
2347 /// We will first see if there is an application specific ".lldbinit" file
2348 /// whose name is "~/.lldbinit" followed by a "-" and the name of the program.
2349 /// If this file doesn't exist, we fall back to the REPL init file or the
2350 /// default home init file in "~/.lldbinit".
2351 void CommandInterpreter::SourceInitFileHome(CommandReturnObject &result,
2352                                             bool is_repl) {
2353   if (m_skip_lldbinit_files) {
2354     result.SetStatus(eReturnStatusSuccessFinishNoResult);
2355     return;
2356   }
2357 
2358   llvm::SmallString<128> init_file;
2359 
2360   if (is_repl)
2361     GetHomeREPLInitFile(init_file, GetDebugger().GetREPLLanguage());
2362 
2363   if (init_file.empty())
2364     GetHomeInitFile(init_file);
2365 
2366   if (!m_skip_app_init_files) {
2367     llvm::StringRef program_name =
2368         HostInfo::GetProgramFileSpec().GetFilename().GetStringRef();
2369     llvm::SmallString<128> program_init_file;
2370     GetHomeInitFile(program_init_file, program_name);
2371     if (FileSystem::Instance().Exists(program_init_file))
2372       init_file = program_init_file;
2373   }
2374 
2375   SourceInitFile(FileSpec(init_file.str()), result);
2376 }
2377 
2378 const char *CommandInterpreter::GetCommandPrefix() {
2379   const char *prefix = GetDebugger().GetIOHandlerCommandPrefix();
2380   return prefix == nullptr ? "" : prefix;
2381 }
2382 
2383 PlatformSP CommandInterpreter::GetPlatform(bool prefer_target_platform) {
2384   PlatformSP platform_sp;
2385   if (prefer_target_platform) {
2386     ExecutionContext exe_ctx(GetExecutionContext());
2387     Target *target = exe_ctx.GetTargetPtr();
2388     if (target)
2389       platform_sp = target->GetPlatform();
2390   }
2391 
2392   if (!platform_sp)
2393     platform_sp = m_debugger.GetPlatformList().GetSelectedPlatform();
2394   return platform_sp;
2395 }
2396 
2397 bool CommandInterpreter::DidProcessStopAbnormally() const {
2398   auto exe_ctx = GetExecutionContext();
2399   TargetSP target_sp = exe_ctx.GetTargetSP();
2400   if (!target_sp)
2401     return false;
2402 
2403   ProcessSP process_sp(target_sp->GetProcessSP());
2404   if (!process_sp)
2405     return false;
2406 
2407   if (eStateStopped != process_sp->GetState())
2408     return false;
2409 
2410   for (const auto &thread_sp : process_sp->GetThreadList().Threads()) {
2411     StopInfoSP stop_info = thread_sp->GetStopInfo();
2412     if (!stop_info)
2413       return false;
2414 
2415     const StopReason reason = stop_info->GetStopReason();
2416     if (reason == eStopReasonException ||
2417         reason == eStopReasonInstrumentation ||
2418         reason == eStopReasonProcessorTrace)
2419       return true;
2420 
2421     if (reason == eStopReasonSignal) {
2422       const auto stop_signal = static_cast<int32_t>(stop_info->GetValue());
2423       UnixSignalsSP signals_sp = process_sp->GetUnixSignals();
2424       if (!signals_sp || !signals_sp->SignalIsValid(stop_signal))
2425         // The signal is unknown, treat it as abnormal.
2426         return true;
2427 
2428       const auto sigint_num = signals_sp->GetSignalNumberFromName("SIGINT");
2429       const auto sigstop_num = signals_sp->GetSignalNumberFromName("SIGSTOP");
2430       if ((stop_signal != sigint_num) && (stop_signal != sigstop_num))
2431         // The signal very likely implies a crash.
2432         return true;
2433     }
2434   }
2435 
2436   return false;
2437 }
2438 
2439 void
2440 CommandInterpreter::HandleCommands(const StringList &commands,
2441                                    const ExecutionContext &override_context,
2442                                    const CommandInterpreterRunOptions &options,
2443                                    CommandReturnObject &result) {
2444 
2445   OverrideExecutionContext(override_context);
2446   HandleCommands(commands, options, result);
2447   RestoreExecutionContext();
2448 }
2449 
2450 void CommandInterpreter::HandleCommands(const StringList &commands,
2451                                         const CommandInterpreterRunOptions &options,
2452                                         CommandReturnObject &result) {
2453   size_t num_lines = commands.GetSize();
2454 
2455   // If we are going to continue past a "continue" then we need to run the
2456   // commands synchronously. Make sure you reset this value anywhere you return
2457   // from the function.
2458 
2459   bool old_async_execution = m_debugger.GetAsyncExecution();
2460 
2461   if (!options.GetStopOnContinue()) {
2462     m_debugger.SetAsyncExecution(false);
2463   }
2464 
2465   for (size_t idx = 0; idx < num_lines && !WasInterrupted(); idx++) {
2466     const char *cmd = commands.GetStringAtIndex(idx);
2467     if (cmd[0] == '\0')
2468       continue;
2469 
2470     if (options.GetEchoCommands()) {
2471       // TODO: Add Stream support.
2472       result.AppendMessageWithFormat("%s %s\n",
2473                                      m_debugger.GetPrompt().str().c_str(), cmd);
2474     }
2475 
2476     CommandReturnObject tmp_result(m_debugger.GetUseColor());
2477     tmp_result.SetInteractive(result.GetInteractive());
2478     tmp_result.SetSuppressImmediateOutput(true);
2479 
2480     // We might call into a regex or alias command, in which case the
2481     // add_to_history will get lost.  This m_command_source_depth dingus is the
2482     // way we turn off adding to the history in that case, so set it up here.
2483     if (!options.GetAddToHistory())
2484       m_command_source_depth++;
2485     bool success = HandleCommand(cmd, options.m_add_to_history, tmp_result);
2486     if (!options.GetAddToHistory())
2487       m_command_source_depth--;
2488 
2489     if (options.GetPrintResults()) {
2490       if (tmp_result.Succeeded())
2491         result.AppendMessage(tmp_result.GetOutputData());
2492     }
2493 
2494     if (!success || !tmp_result.Succeeded()) {
2495       llvm::StringRef error_msg = tmp_result.GetErrorData();
2496       if (error_msg.empty())
2497         error_msg = "<unknown error>.\n";
2498       if (options.GetStopOnError()) {
2499         result.AppendErrorWithFormat(
2500             "Aborting reading of commands after command #%" PRIu64
2501             ": '%s' failed with %s",
2502             (uint64_t)idx, cmd, error_msg.str().c_str());
2503         m_debugger.SetAsyncExecution(old_async_execution);
2504         return;
2505       } else if (options.GetPrintResults()) {
2506         result.AppendMessageWithFormat(
2507             "Command #%" PRIu64 " '%s' failed with %s", (uint64_t)idx + 1, cmd,
2508             error_msg.str().c_str());
2509       }
2510     }
2511 
2512     if (result.GetImmediateOutputStream())
2513       result.GetImmediateOutputStream()->Flush();
2514 
2515     if (result.GetImmediateErrorStream())
2516       result.GetImmediateErrorStream()->Flush();
2517 
2518     // N.B. Can't depend on DidChangeProcessState, because the state coming
2519     // into the command execution could be running (for instance in Breakpoint
2520     // Commands. So we check the return value to see if it is has running in
2521     // it.
2522     if ((tmp_result.GetStatus() == eReturnStatusSuccessContinuingNoResult) ||
2523         (tmp_result.GetStatus() == eReturnStatusSuccessContinuingResult)) {
2524       if (options.GetStopOnContinue()) {
2525         // If we caused the target to proceed, and we're going to stop in that
2526         // case, set the status in our real result before returning.  This is
2527         // an error if the continue was not the last command in the set of
2528         // commands to be run.
2529         if (idx != num_lines - 1)
2530           result.AppendErrorWithFormat(
2531               "Aborting reading of commands after command #%" PRIu64
2532               ": '%s' continued the target.\n",
2533               (uint64_t)idx + 1, cmd);
2534         else
2535           result.AppendMessageWithFormat("Command #%" PRIu64
2536                                          " '%s' continued the target.\n",
2537                                          (uint64_t)idx + 1, cmd);
2538 
2539         result.SetStatus(tmp_result.GetStatus());
2540         m_debugger.SetAsyncExecution(old_async_execution);
2541 
2542         return;
2543       }
2544     }
2545 
2546     // Also check for "stop on crash here:
2547     if (tmp_result.GetDidChangeProcessState() && options.GetStopOnCrash() &&
2548         DidProcessStopAbnormally()) {
2549       if (idx != num_lines - 1)
2550         result.AppendErrorWithFormat(
2551             "Aborting reading of commands after command #%" PRIu64
2552             ": '%s' stopped with a signal or exception.\n",
2553             (uint64_t)idx + 1, cmd);
2554       else
2555         result.AppendMessageWithFormat(
2556             "Command #%" PRIu64 " '%s' stopped with a signal or exception.\n",
2557             (uint64_t)idx + 1, cmd);
2558 
2559       result.SetStatus(tmp_result.GetStatus());
2560       m_debugger.SetAsyncExecution(old_async_execution);
2561 
2562       return;
2563     }
2564   }
2565 
2566   result.SetStatus(eReturnStatusSuccessFinishResult);
2567   m_debugger.SetAsyncExecution(old_async_execution);
2568 }
2569 
2570 // Make flags that we can pass into the IOHandler so our delegates can do the
2571 // right thing
2572 enum {
2573   eHandleCommandFlagStopOnContinue = (1u << 0),
2574   eHandleCommandFlagStopOnError = (1u << 1),
2575   eHandleCommandFlagEchoCommand = (1u << 2),
2576   eHandleCommandFlagEchoCommentCommand = (1u << 3),
2577   eHandleCommandFlagPrintResult = (1u << 4),
2578   eHandleCommandFlagPrintErrors = (1u << 5),
2579   eHandleCommandFlagStopOnCrash = (1u << 6)
2580 };
2581 
2582 void CommandInterpreter::HandleCommandsFromFile(
2583     FileSpec &cmd_file, const ExecutionContext &context,
2584     const CommandInterpreterRunOptions &options, CommandReturnObject &result) {
2585   OverrideExecutionContext(context);
2586   HandleCommandsFromFile(cmd_file, options, result);
2587   RestoreExecutionContext();
2588 }
2589 
2590 void CommandInterpreter::HandleCommandsFromFile(FileSpec &cmd_file,
2591     const CommandInterpreterRunOptions &options, CommandReturnObject &result) {
2592   if (!FileSystem::Instance().Exists(cmd_file)) {
2593     result.AppendErrorWithFormat(
2594         "Error reading commands from file %s - file not found.\n",
2595         cmd_file.GetFilename().AsCString("<Unknown>"));
2596     return;
2597   }
2598 
2599   std::string cmd_file_path = cmd_file.GetPath();
2600   auto input_file_up =
2601       FileSystem::Instance().Open(cmd_file, File::eOpenOptionReadOnly);
2602   if (!input_file_up) {
2603     std::string error = llvm::toString(input_file_up.takeError());
2604     result.AppendErrorWithFormatv(
2605         "error: an error occurred read file '{0}': {1}\n", cmd_file_path,
2606         llvm::fmt_consume(input_file_up.takeError()));
2607     return;
2608   }
2609   FileSP input_file_sp = FileSP(std::move(input_file_up.get()));
2610 
2611   Debugger &debugger = GetDebugger();
2612 
2613   uint32_t flags = 0;
2614 
2615   if (options.m_stop_on_continue == eLazyBoolCalculate) {
2616     if (m_command_source_flags.empty()) {
2617       // Stop on continue by default
2618       flags |= eHandleCommandFlagStopOnContinue;
2619     } else if (m_command_source_flags.back() &
2620                eHandleCommandFlagStopOnContinue) {
2621       flags |= eHandleCommandFlagStopOnContinue;
2622     }
2623   } else if (options.m_stop_on_continue == eLazyBoolYes) {
2624     flags |= eHandleCommandFlagStopOnContinue;
2625   }
2626 
2627   if (options.m_stop_on_error == eLazyBoolCalculate) {
2628     if (m_command_source_flags.empty()) {
2629       if (GetStopCmdSourceOnError())
2630         flags |= eHandleCommandFlagStopOnError;
2631     } else if (m_command_source_flags.back() & eHandleCommandFlagStopOnError) {
2632       flags |= eHandleCommandFlagStopOnError;
2633     }
2634   } else if (options.m_stop_on_error == eLazyBoolYes) {
2635     flags |= eHandleCommandFlagStopOnError;
2636   }
2637 
2638   // stop-on-crash can only be set, if it is present in all levels of
2639   // pushed flag sets.
2640   if (options.GetStopOnCrash()) {
2641     if (m_command_source_flags.empty()) {
2642       flags |= eHandleCommandFlagStopOnCrash;
2643     } else if (m_command_source_flags.back() & eHandleCommandFlagStopOnCrash) {
2644       flags |= eHandleCommandFlagStopOnCrash;
2645     }
2646   }
2647 
2648   if (options.m_echo_commands == eLazyBoolCalculate) {
2649     if (m_command_source_flags.empty()) {
2650       // Echo command by default
2651       flags |= eHandleCommandFlagEchoCommand;
2652     } else if (m_command_source_flags.back() & eHandleCommandFlagEchoCommand) {
2653       flags |= eHandleCommandFlagEchoCommand;
2654     }
2655   } else if (options.m_echo_commands == eLazyBoolYes) {
2656     flags |= eHandleCommandFlagEchoCommand;
2657   }
2658 
2659   // We will only ever ask for this flag, if we echo commands in general.
2660   if (options.m_echo_comment_commands == eLazyBoolCalculate) {
2661     if (m_command_source_flags.empty()) {
2662       // Echo comments by default
2663       flags |= eHandleCommandFlagEchoCommentCommand;
2664     } else if (m_command_source_flags.back() &
2665                eHandleCommandFlagEchoCommentCommand) {
2666       flags |= eHandleCommandFlagEchoCommentCommand;
2667     }
2668   } else if (options.m_echo_comment_commands == eLazyBoolYes) {
2669     flags |= eHandleCommandFlagEchoCommentCommand;
2670   }
2671 
2672   if (options.m_print_results == eLazyBoolCalculate) {
2673     if (m_command_source_flags.empty()) {
2674       // Print output by default
2675       flags |= eHandleCommandFlagPrintResult;
2676     } else if (m_command_source_flags.back() & eHandleCommandFlagPrintResult) {
2677       flags |= eHandleCommandFlagPrintResult;
2678     }
2679   } else if (options.m_print_results == eLazyBoolYes) {
2680     flags |= eHandleCommandFlagPrintResult;
2681   }
2682 
2683   if (options.m_print_errors == eLazyBoolCalculate) {
2684     if (m_command_source_flags.empty()) {
2685       // Print output by default
2686       flags |= eHandleCommandFlagPrintErrors;
2687     } else if (m_command_source_flags.back() & eHandleCommandFlagPrintErrors) {
2688       flags |= eHandleCommandFlagPrintErrors;
2689     }
2690   } else if (options.m_print_errors == eLazyBoolYes) {
2691     flags |= eHandleCommandFlagPrintErrors;
2692   }
2693 
2694   if (flags & eHandleCommandFlagPrintResult) {
2695     debugger.GetOutputFile().Printf("Executing commands in '%s'.\n",
2696                                     cmd_file_path.c_str());
2697   }
2698 
2699   // Used for inheriting the right settings when "command source" might
2700   // have nested "command source" commands
2701   lldb::StreamFileSP empty_stream_sp;
2702   m_command_source_flags.push_back(flags);
2703   IOHandlerSP io_handler_sp(new IOHandlerEditline(
2704       debugger, IOHandler::Type::CommandInterpreter, input_file_sp,
2705       empty_stream_sp, // Pass in an empty stream so we inherit the top
2706                        // input reader output stream
2707       empty_stream_sp, // Pass in an empty stream so we inherit the top
2708                        // input reader error stream
2709       flags,
2710       nullptr, // Pass in NULL for "editline_name" so no history is saved,
2711                // or written
2712       debugger.GetPrompt(), llvm::StringRef(),
2713       false, // Not multi-line
2714       debugger.GetUseColor(), 0, *this, nullptr));
2715   const bool old_async_execution = debugger.GetAsyncExecution();
2716 
2717   // Set synchronous execution if we are not stopping on continue
2718   if ((flags & eHandleCommandFlagStopOnContinue) == 0)
2719     debugger.SetAsyncExecution(false);
2720 
2721   m_command_source_depth++;
2722   m_command_source_dirs.push_back(cmd_file.CopyByRemovingLastPathComponent());
2723 
2724   debugger.RunIOHandlerSync(io_handler_sp);
2725   if (!m_command_source_flags.empty())
2726     m_command_source_flags.pop_back();
2727 
2728   m_command_source_dirs.pop_back();
2729   m_command_source_depth--;
2730 
2731   result.SetStatus(eReturnStatusSuccessFinishNoResult);
2732   debugger.SetAsyncExecution(old_async_execution);
2733 }
2734 
2735 bool CommandInterpreter::GetSynchronous() { return m_synchronous_execution; }
2736 
2737 void CommandInterpreter::SetSynchronous(bool value) {
2738   // Asynchronous mode is not supported during reproducer replay.
2739   if (repro::Reproducer::Instance().GetLoader())
2740     return;
2741   m_synchronous_execution = value;
2742 }
2743 
2744 void CommandInterpreter::OutputFormattedHelpText(Stream &strm,
2745                                                  llvm::StringRef prefix,
2746                                                  llvm::StringRef help_text) {
2747   const uint32_t max_columns = m_debugger.GetTerminalWidth();
2748 
2749   size_t line_width_max = max_columns - prefix.size();
2750   if (line_width_max < 16)
2751     line_width_max = help_text.size() + prefix.size();
2752 
2753   strm.IndentMore(prefix.size());
2754   bool prefixed_yet = false;
2755   // Even if we have no help text we still want to emit the command name.
2756   if (help_text.empty())
2757     help_text = "No help text";
2758   while (!help_text.empty()) {
2759     // Prefix the first line, indent subsequent lines to line up
2760     if (!prefixed_yet) {
2761       strm << prefix;
2762       prefixed_yet = true;
2763     } else
2764       strm.Indent();
2765 
2766     // Never print more than the maximum on one line.
2767     llvm::StringRef this_line = help_text.substr(0, line_width_max);
2768 
2769     // Always break on an explicit newline.
2770     std::size_t first_newline = this_line.find_first_of("\n");
2771 
2772     // Don't break on space/tab unless the text is too long to fit on one line.
2773     std::size_t last_space = llvm::StringRef::npos;
2774     if (this_line.size() != help_text.size())
2775       last_space = this_line.find_last_of(" \t");
2776 
2777     // Break at whichever condition triggered first.
2778     this_line = this_line.substr(0, std::min(first_newline, last_space));
2779     strm.PutCString(this_line);
2780     strm.EOL();
2781 
2782     // Remove whitespace / newlines after breaking.
2783     help_text = help_text.drop_front(this_line.size()).ltrim();
2784   }
2785   strm.IndentLess(prefix.size());
2786 }
2787 
2788 void CommandInterpreter::OutputFormattedHelpText(Stream &strm,
2789                                                  llvm::StringRef word_text,
2790                                                  llvm::StringRef separator,
2791                                                  llvm::StringRef help_text,
2792                                                  size_t max_word_len) {
2793   StreamString prefix_stream;
2794   prefix_stream.Printf("  %-*s %*s ", (int)max_word_len, word_text.data(),
2795                        (int)separator.size(), separator.data());
2796   OutputFormattedHelpText(strm, prefix_stream.GetString(), help_text);
2797 }
2798 
2799 void CommandInterpreter::OutputHelpText(Stream &strm, llvm::StringRef word_text,
2800                                         llvm::StringRef separator,
2801                                         llvm::StringRef help_text,
2802                                         uint32_t max_word_len) {
2803   int indent_size = max_word_len + separator.size() + 2;
2804 
2805   strm.IndentMore(indent_size);
2806 
2807   StreamString text_strm;
2808   text_strm.Printf("%-*s ", (int)max_word_len, word_text.data());
2809   text_strm << separator << " " << help_text;
2810 
2811   const uint32_t max_columns = m_debugger.GetTerminalWidth();
2812 
2813   llvm::StringRef text = text_strm.GetString();
2814 
2815   uint32_t chars_left = max_columns;
2816 
2817   auto nextWordLength = [](llvm::StringRef S) {
2818     size_t pos = S.find(' ');
2819     return pos == llvm::StringRef::npos ? S.size() : pos;
2820   };
2821 
2822   while (!text.empty()) {
2823     if (text.front() == '\n' ||
2824         (text.front() == ' ' && nextWordLength(text.ltrim(' ')) > chars_left)) {
2825       strm.EOL();
2826       strm.Indent();
2827       chars_left = max_columns - indent_size;
2828       if (text.front() == '\n')
2829         text = text.drop_front();
2830       else
2831         text = text.ltrim(' ');
2832     } else {
2833       strm.PutChar(text.front());
2834       --chars_left;
2835       text = text.drop_front();
2836     }
2837   }
2838 
2839   strm.EOL();
2840   strm.IndentLess(indent_size);
2841 }
2842 
2843 void CommandInterpreter::FindCommandsForApropos(
2844     llvm::StringRef search_word, StringList &commands_found,
2845     StringList &commands_help, const CommandObject::CommandMap &command_map) {
2846   for (const auto &pair : command_map) {
2847     llvm::StringRef command_name = pair.first;
2848     CommandObject *cmd_obj = pair.second.get();
2849 
2850     const bool search_short_help = true;
2851     const bool search_long_help = false;
2852     const bool search_syntax = false;
2853     const bool search_options = false;
2854     if (command_name.contains_insensitive(search_word) ||
2855         cmd_obj->HelpTextContainsWord(search_word, search_short_help,
2856                                       search_long_help, search_syntax,
2857                                       search_options)) {
2858       commands_found.AppendString(command_name);
2859       commands_help.AppendString(cmd_obj->GetHelp());
2860     }
2861 
2862     if (auto *multiword_cmd = cmd_obj->GetAsMultiwordCommand()) {
2863       StringList subcommands_found;
2864       FindCommandsForApropos(search_word, subcommands_found, commands_help,
2865                              multiword_cmd->GetSubcommandDictionary());
2866       for (const auto &subcommand_name : subcommands_found) {
2867         std::string qualified_name =
2868             (command_name + " " + subcommand_name).str();
2869         commands_found.AppendString(qualified_name);
2870       }
2871     }
2872   }
2873 }
2874 
2875 void CommandInterpreter::FindCommandsForApropos(llvm::StringRef search_word,
2876                                                 StringList &commands_found,
2877                                                 StringList &commands_help,
2878                                                 bool search_builtin_commands,
2879                                                 bool search_user_commands,
2880                                                 bool search_alias_commands,
2881                                                 bool search_user_mw_commands) {
2882   CommandObject::CommandMap::const_iterator pos;
2883 
2884   if (search_builtin_commands)
2885     FindCommandsForApropos(search_word, commands_found, commands_help,
2886                            m_command_dict);
2887 
2888   if (search_user_commands)
2889     FindCommandsForApropos(search_word, commands_found, commands_help,
2890                            m_user_dict);
2891 
2892   if (search_user_mw_commands)
2893     FindCommandsForApropos(search_word, commands_found, commands_help,
2894                            m_user_mw_dict);
2895 
2896   if (search_alias_commands)
2897     FindCommandsForApropos(search_word, commands_found, commands_help,
2898                            m_alias_dict);
2899 }
2900 
2901 ExecutionContext CommandInterpreter::GetExecutionContext() const {
2902   return !m_overriden_exe_contexts.empty()
2903              ? m_overriden_exe_contexts.top()
2904              : m_debugger.GetSelectedExecutionContext();
2905 }
2906 
2907 void CommandInterpreter::OverrideExecutionContext(
2908     const ExecutionContext &override_context) {
2909   m_overriden_exe_contexts.push(override_context);
2910 }
2911 
2912 void CommandInterpreter::RestoreExecutionContext() {
2913   if (!m_overriden_exe_contexts.empty())
2914     m_overriden_exe_contexts.pop();
2915 }
2916 
2917 void CommandInterpreter::GetProcessOutput() {
2918   if (ProcessSP process_sp = GetExecutionContext().GetProcessSP())
2919     m_debugger.FlushProcessOutput(*process_sp, /*flush_stdout*/ true,
2920                                   /*flush_stderr*/ true);
2921 }
2922 
2923 void CommandInterpreter::StartHandlingCommand() {
2924   auto idle_state = CommandHandlingState::eIdle;
2925   if (m_command_state.compare_exchange_strong(
2926           idle_state, CommandHandlingState::eInProgress))
2927     lldbassert(m_iohandler_nesting_level == 0);
2928   else
2929     lldbassert(m_iohandler_nesting_level > 0);
2930   ++m_iohandler_nesting_level;
2931 }
2932 
2933 void CommandInterpreter::FinishHandlingCommand() {
2934   lldbassert(m_iohandler_nesting_level > 0);
2935   if (--m_iohandler_nesting_level == 0) {
2936     auto prev_state = m_command_state.exchange(CommandHandlingState::eIdle);
2937     lldbassert(prev_state != CommandHandlingState::eIdle);
2938   }
2939 }
2940 
2941 bool CommandInterpreter::InterruptCommand() {
2942   auto in_progress = CommandHandlingState::eInProgress;
2943   return m_command_state.compare_exchange_strong(
2944       in_progress, CommandHandlingState::eInterrupted);
2945 }
2946 
2947 bool CommandInterpreter::WasInterrupted() const {
2948   bool was_interrupted =
2949       (m_command_state == CommandHandlingState::eInterrupted);
2950   lldbassert(!was_interrupted || m_iohandler_nesting_level > 0);
2951   return was_interrupted;
2952 }
2953 
2954 void CommandInterpreter::PrintCommandOutput(Stream &stream,
2955                                             llvm::StringRef str) {
2956   // Split the output into lines and poll for interrupt requests
2957   const char *data = str.data();
2958   size_t size = str.size();
2959   while (size > 0 && !WasInterrupted()) {
2960     size_t chunk_size = 0;
2961     for (; chunk_size < size; ++chunk_size) {
2962       lldbassert(data[chunk_size] != '\0');
2963       if (data[chunk_size] == '\n') {
2964         ++chunk_size;
2965         break;
2966       }
2967     }
2968     chunk_size = stream.Write(data, chunk_size);
2969     lldbassert(size >= chunk_size);
2970     data += chunk_size;
2971     size -= chunk_size;
2972   }
2973   if (size > 0) {
2974     stream.Printf("\n... Interrupted.\n");
2975   }
2976 }
2977 
2978 bool CommandInterpreter::EchoCommandNonInteractive(
2979     llvm::StringRef line, const Flags &io_handler_flags) const {
2980   if (!io_handler_flags.Test(eHandleCommandFlagEchoCommand))
2981     return false;
2982 
2983   llvm::StringRef command = line.trim();
2984   if (command.empty())
2985     return true;
2986 
2987   if (command.front() == m_comment_char)
2988     return io_handler_flags.Test(eHandleCommandFlagEchoCommentCommand);
2989 
2990   return true;
2991 }
2992 
2993 void CommandInterpreter::IOHandlerInputComplete(IOHandler &io_handler,
2994                                                 std::string &line) {
2995     // If we were interrupted, bail out...
2996     if (WasInterrupted())
2997       return;
2998 
2999   const bool is_interactive = io_handler.GetIsInteractive();
3000   if (!is_interactive) {
3001     // When we are not interactive, don't execute blank lines. This will happen
3002     // sourcing a commands file. We don't want blank lines to repeat the
3003     // previous command and cause any errors to occur (like redefining an
3004     // alias, get an error and stop parsing the commands file).
3005     if (line.empty())
3006       return;
3007 
3008     // When using a non-interactive file handle (like when sourcing commands
3009     // from a file) we need to echo the command out so we don't just see the
3010     // command output and no command...
3011     if (EchoCommandNonInteractive(line, io_handler.GetFlags()))
3012       io_handler.GetOutputStreamFileSP()->Printf(
3013           "%s%s\n", io_handler.GetPrompt(), line.c_str());
3014   }
3015 
3016   StartHandlingCommand();
3017 
3018   OverrideExecutionContext(m_debugger.GetSelectedExecutionContext());
3019   auto finalize = llvm::make_scope_exit([this]() {
3020     RestoreExecutionContext();
3021   });
3022 
3023   lldb_private::CommandReturnObject result(m_debugger.GetUseColor());
3024   HandleCommand(line.c_str(), eLazyBoolCalculate, result);
3025 
3026   // Now emit the command output text from the command we just executed
3027   if ((result.Succeeded() &&
3028        io_handler.GetFlags().Test(eHandleCommandFlagPrintResult)) ||
3029       io_handler.GetFlags().Test(eHandleCommandFlagPrintErrors)) {
3030     // Display any STDOUT/STDERR _prior_ to emitting the command result text
3031     GetProcessOutput();
3032 
3033     if (!result.GetImmediateOutputStream()) {
3034       llvm::StringRef output = result.GetOutputData();
3035       PrintCommandOutput(*io_handler.GetOutputStreamFileSP(), output);
3036     }
3037 
3038     // Now emit the command error text from the command we just executed
3039     if (!result.GetImmediateErrorStream()) {
3040       llvm::StringRef error = result.GetErrorData();
3041       PrintCommandOutput(*io_handler.GetErrorStreamFileSP(), error);
3042     }
3043   }
3044 
3045   FinishHandlingCommand();
3046 
3047   switch (result.GetStatus()) {
3048   case eReturnStatusInvalid:
3049   case eReturnStatusSuccessFinishNoResult:
3050   case eReturnStatusSuccessFinishResult:
3051   case eReturnStatusStarted:
3052     break;
3053 
3054   case eReturnStatusSuccessContinuingNoResult:
3055   case eReturnStatusSuccessContinuingResult:
3056     if (io_handler.GetFlags().Test(eHandleCommandFlagStopOnContinue))
3057       io_handler.SetIsDone(true);
3058     break;
3059 
3060   case eReturnStatusFailed:
3061     m_result.IncrementNumberOfErrors();
3062     if (io_handler.GetFlags().Test(eHandleCommandFlagStopOnError)) {
3063       m_result.SetResult(lldb::eCommandInterpreterResultCommandError);
3064       io_handler.SetIsDone(true);
3065     }
3066     break;
3067 
3068   case eReturnStatusQuit:
3069     m_result.SetResult(lldb::eCommandInterpreterResultQuitRequested);
3070     io_handler.SetIsDone(true);
3071     break;
3072   }
3073 
3074   // Finally, if we're going to stop on crash, check that here:
3075   if (m_result.IsResult(lldb::eCommandInterpreterResultSuccess) &&
3076       result.GetDidChangeProcessState() &&
3077       io_handler.GetFlags().Test(eHandleCommandFlagStopOnCrash) &&
3078       DidProcessStopAbnormally()) {
3079     io_handler.SetIsDone(true);
3080     m_result.SetResult(lldb::eCommandInterpreterResultInferiorCrash);
3081   }
3082 }
3083 
3084 bool CommandInterpreter::IOHandlerInterrupt(IOHandler &io_handler) {
3085   ExecutionContext exe_ctx(GetExecutionContext());
3086   Process *process = exe_ctx.GetProcessPtr();
3087 
3088   if (InterruptCommand())
3089     return true;
3090 
3091   if (process) {
3092     StateType state = process->GetState();
3093     if (StateIsRunningState(state)) {
3094       process->Halt();
3095       return true; // Don't do any updating when we are running
3096     }
3097   }
3098 
3099   ScriptInterpreter *script_interpreter =
3100       m_debugger.GetScriptInterpreter(false);
3101   if (script_interpreter) {
3102     if (script_interpreter->Interrupt())
3103       return true;
3104   }
3105   return false;
3106 }
3107 
3108 bool CommandInterpreter::SaveTranscript(
3109     CommandReturnObject &result, llvm::Optional<std::string> output_file) {
3110   if (output_file == llvm::None || output_file->empty()) {
3111     std::string now = llvm::to_string(std::chrono::system_clock::now());
3112     std::replace(now.begin(), now.end(), ' ', '_');
3113     const std::string file_name = "lldb_session_" + now + ".log";
3114 
3115     FileSpec save_location = GetSaveSessionDirectory();
3116 
3117     if (!save_location)
3118       save_location = HostInfo::GetGlobalTempDir();
3119 
3120     FileSystem::Instance().Resolve(save_location);
3121     save_location.AppendPathComponent(file_name);
3122     output_file = save_location.GetPath();
3123   }
3124 
3125   auto error_out = [&](llvm::StringRef error_message, std::string description) {
3126     LLDB_LOG(GetLog(LLDBLog::Commands), "{0} ({1}:{2})", error_message,
3127              output_file, description);
3128     result.AppendErrorWithFormatv(
3129         "Failed to save session's transcripts to {0}!", *output_file);
3130     return false;
3131   };
3132 
3133   File::OpenOptions flags = File::eOpenOptionWriteOnly |
3134                             File::eOpenOptionCanCreate |
3135                             File::eOpenOptionTruncate;
3136 
3137   auto opened_file = FileSystem::Instance().Open(FileSpec(*output_file), flags);
3138 
3139   if (!opened_file)
3140     return error_out("Unable to create file",
3141                      llvm::toString(opened_file.takeError()));
3142 
3143   FileUP file = std::move(opened_file.get());
3144 
3145   size_t byte_size = m_transcript_stream.GetSize();
3146 
3147   Status error = file->Write(m_transcript_stream.GetData(), byte_size);
3148 
3149   if (error.Fail() || byte_size != m_transcript_stream.GetSize())
3150     return error_out("Unable to write to destination file",
3151                      "Bytes written do not match transcript size.");
3152 
3153   result.SetStatus(eReturnStatusSuccessFinishNoResult);
3154   result.AppendMessageWithFormat("Session's transcripts saved to %s\n",
3155                                  output_file->c_str());
3156 
3157   return true;
3158 }
3159 
3160 FileSpec CommandInterpreter::GetCurrentSourceDir() {
3161   if (m_command_source_dirs.empty())
3162     return {};
3163   return m_command_source_dirs.back();
3164 }
3165 
3166 void CommandInterpreter::GetLLDBCommandsFromIOHandler(
3167     const char *prompt, IOHandlerDelegate &delegate, void *baton) {
3168   Debugger &debugger = GetDebugger();
3169   IOHandlerSP io_handler_sp(
3170       new IOHandlerEditline(debugger, IOHandler::Type::CommandList,
3171                             "lldb", // Name of input reader for history
3172                             llvm::StringRef(prompt), // Prompt
3173                             llvm::StringRef(),       // Continuation prompt
3174                             true,                    // Get multiple lines
3175                             debugger.GetUseColor(),
3176                             0,         // Don't show line numbers
3177                             delegate,  // IOHandlerDelegate
3178                             nullptr)); // FileShadowCollector
3179 
3180   if (io_handler_sp) {
3181     io_handler_sp->SetUserData(baton);
3182     debugger.RunIOHandlerAsync(io_handler_sp);
3183   }
3184 }
3185 
3186 void CommandInterpreter::GetPythonCommandsFromIOHandler(
3187     const char *prompt, IOHandlerDelegate &delegate, void *baton) {
3188   Debugger &debugger = GetDebugger();
3189   IOHandlerSP io_handler_sp(
3190       new IOHandlerEditline(debugger, IOHandler::Type::PythonCode,
3191                             "lldb-python", // Name of input reader for history
3192                             llvm::StringRef(prompt), // Prompt
3193                             llvm::StringRef(),       // Continuation prompt
3194                             true,                    // Get multiple lines
3195                             debugger.GetUseColor(),
3196                             0,         // Don't show line numbers
3197                             delegate,  // IOHandlerDelegate
3198                             nullptr)); // FileShadowCollector
3199 
3200   if (io_handler_sp) {
3201     io_handler_sp->SetUserData(baton);
3202     debugger.RunIOHandlerAsync(io_handler_sp);
3203   }
3204 }
3205 
3206 bool CommandInterpreter::IsActive() {
3207   return m_debugger.IsTopIOHandler(m_command_io_handler_sp);
3208 }
3209 
3210 lldb::IOHandlerSP
3211 CommandInterpreter::GetIOHandler(bool force_create,
3212                                  CommandInterpreterRunOptions *options) {
3213   // Always re-create the IOHandlerEditline in case the input changed. The old
3214   // instance might have had a non-interactive input and now it does or vice
3215   // versa.
3216   if (force_create || !m_command_io_handler_sp) {
3217     // Always re-create the IOHandlerEditline in case the input changed. The
3218     // old instance might have had a non-interactive input and now it does or
3219     // vice versa.
3220     uint32_t flags = 0;
3221 
3222     if (options) {
3223       if (options->m_stop_on_continue == eLazyBoolYes)
3224         flags |= eHandleCommandFlagStopOnContinue;
3225       if (options->m_stop_on_error == eLazyBoolYes)
3226         flags |= eHandleCommandFlagStopOnError;
3227       if (options->m_stop_on_crash == eLazyBoolYes)
3228         flags |= eHandleCommandFlagStopOnCrash;
3229       if (options->m_echo_commands != eLazyBoolNo)
3230         flags |= eHandleCommandFlagEchoCommand;
3231       if (options->m_echo_comment_commands != eLazyBoolNo)
3232         flags |= eHandleCommandFlagEchoCommentCommand;
3233       if (options->m_print_results != eLazyBoolNo)
3234         flags |= eHandleCommandFlagPrintResult;
3235       if (options->m_print_errors != eLazyBoolNo)
3236         flags |= eHandleCommandFlagPrintErrors;
3237     } else {
3238       flags = eHandleCommandFlagEchoCommand | eHandleCommandFlagPrintResult |
3239               eHandleCommandFlagPrintErrors;
3240     }
3241 
3242     m_command_io_handler_sp = std::make_shared<IOHandlerEditline>(
3243         m_debugger, IOHandler::Type::CommandInterpreter,
3244         m_debugger.GetInputFileSP(), m_debugger.GetOutputStreamSP(),
3245         m_debugger.GetErrorStreamSP(), flags, "lldb", m_debugger.GetPrompt(),
3246         llvm::StringRef(), // Continuation prompt
3247         false, // Don't enable multiple line input, just single line commands
3248         m_debugger.GetUseColor(),
3249         0,     // Don't show line numbers
3250         *this, // IOHandlerDelegate
3251         GetDebugger().GetInputRecorder());
3252   }
3253   return m_command_io_handler_sp;
3254 }
3255 
3256 CommandInterpreterRunResult CommandInterpreter::RunCommandInterpreter(
3257     CommandInterpreterRunOptions &options) {
3258   // Always re-create the command interpreter when we run it in case any file
3259   // handles have changed.
3260   bool force_create = true;
3261   m_debugger.RunIOHandlerAsync(GetIOHandler(force_create, &options));
3262   m_result = CommandInterpreterRunResult();
3263 
3264   if (options.GetAutoHandleEvents())
3265     m_debugger.StartEventHandlerThread();
3266 
3267   if (options.GetSpawnThread()) {
3268     m_debugger.StartIOHandlerThread();
3269   } else {
3270     m_debugger.RunIOHandlers();
3271 
3272     if (options.GetAutoHandleEvents())
3273       m_debugger.StopEventHandlerThread();
3274   }
3275 
3276   return m_result;
3277 }
3278 
3279 CommandObject *
3280 CommandInterpreter::ResolveCommandImpl(std::string &command_line,
3281                                        CommandReturnObject &result) {
3282   std::string scratch_command(command_line); // working copy so we don't modify
3283                                              // command_line unless we succeed
3284   CommandObject *cmd_obj = nullptr;
3285   StreamString revised_command_line;
3286   bool wants_raw_input = false;
3287   std::string next_word;
3288   StringList matches;
3289   bool done = false;
3290   while (!done) {
3291     char quote_char = '\0';
3292     std::string suffix;
3293     ExtractCommand(scratch_command, next_word, suffix, quote_char);
3294     if (cmd_obj == nullptr) {
3295       std::string full_name;
3296       bool is_alias = GetAliasFullName(next_word, full_name);
3297       cmd_obj = GetCommandObject(next_word, &matches);
3298       bool is_real_command =
3299           (!is_alias) || (cmd_obj != nullptr && !cmd_obj->IsAlias());
3300       if (!is_real_command) {
3301         matches.Clear();
3302         std::string alias_result;
3303         cmd_obj =
3304             BuildAliasResult(full_name, scratch_command, alias_result, result);
3305         revised_command_line.Printf("%s", alias_result.c_str());
3306         if (cmd_obj) {
3307           wants_raw_input = cmd_obj->WantsRawCommandString();
3308         }
3309       } else {
3310         if (cmd_obj) {
3311           llvm::StringRef cmd_name = cmd_obj->GetCommandName();
3312           revised_command_line.Printf("%s", cmd_name.str().c_str());
3313           wants_raw_input = cmd_obj->WantsRawCommandString();
3314         } else {
3315           revised_command_line.Printf("%s", next_word.c_str());
3316         }
3317       }
3318     } else {
3319       if (cmd_obj->IsMultiwordObject()) {
3320         CommandObject *sub_cmd_obj =
3321             cmd_obj->GetSubcommandObject(next_word.c_str());
3322         if (sub_cmd_obj) {
3323           // The subcommand's name includes the parent command's name, so
3324           // restart rather than append to the revised_command_line.
3325           llvm::StringRef sub_cmd_name = sub_cmd_obj->GetCommandName();
3326           revised_command_line.Clear();
3327           revised_command_line.Printf("%s", sub_cmd_name.str().c_str());
3328           cmd_obj = sub_cmd_obj;
3329           wants_raw_input = cmd_obj->WantsRawCommandString();
3330         } else {
3331           if (quote_char)
3332             revised_command_line.Printf(" %c%s%s%c", quote_char,
3333                                         next_word.c_str(), suffix.c_str(),
3334                                         quote_char);
3335           else
3336             revised_command_line.Printf(" %s%s", next_word.c_str(),
3337                                         suffix.c_str());
3338           done = true;
3339         }
3340       } else {
3341         if (quote_char)
3342           revised_command_line.Printf(" %c%s%s%c", quote_char,
3343                                       next_word.c_str(), suffix.c_str(),
3344                                       quote_char);
3345         else
3346           revised_command_line.Printf(" %s%s", next_word.c_str(),
3347                                       suffix.c_str());
3348         done = true;
3349       }
3350     }
3351 
3352     if (cmd_obj == nullptr) {
3353       const size_t num_matches = matches.GetSize();
3354       if (matches.GetSize() > 1) {
3355         StreamString error_msg;
3356         error_msg.Printf("Ambiguous command '%s'. Possible matches:\n",
3357                          next_word.c_str());
3358 
3359         for (uint32_t i = 0; i < num_matches; ++i) {
3360           error_msg.Printf("\t%s\n", matches.GetStringAtIndex(i));
3361         }
3362         result.AppendRawError(error_msg.GetString());
3363       } else {
3364         // We didn't have only one match, otherwise we wouldn't get here.
3365         lldbassert(num_matches == 0);
3366         result.AppendErrorWithFormat("'%s' is not a valid command.\n",
3367                                      next_word.c_str());
3368       }
3369       return nullptr;
3370     }
3371 
3372     if (cmd_obj->IsMultiwordObject()) {
3373       if (!suffix.empty()) {
3374         result.AppendErrorWithFormat(
3375             "command '%s' did not recognize '%s%s%s' as valid (subcommand "
3376             "might be invalid).\n",
3377             cmd_obj->GetCommandName().str().c_str(),
3378             next_word.empty() ? "" : next_word.c_str(),
3379             next_word.empty() ? " -- " : " ", suffix.c_str());
3380         return nullptr;
3381       }
3382     } else {
3383       // If we found a normal command, we are done
3384       done = true;
3385       if (!suffix.empty()) {
3386         switch (suffix[0]) {
3387         case '/':
3388           // GDB format suffixes
3389           {
3390             Options *command_options = cmd_obj->GetOptions();
3391             if (command_options &&
3392                 command_options->SupportsLongOption("gdb-format")) {
3393               std::string gdb_format_option("--gdb-format=");
3394               gdb_format_option += (suffix.c_str() + 1);
3395 
3396               std::string cmd = std::string(revised_command_line.GetString());
3397               size_t arg_terminator_idx = FindArgumentTerminator(cmd);
3398               if (arg_terminator_idx != std::string::npos) {
3399                 // Insert the gdb format option before the "--" that terminates
3400                 // options
3401                 gdb_format_option.append(1, ' ');
3402                 cmd.insert(arg_terminator_idx, gdb_format_option);
3403                 revised_command_line.Clear();
3404                 revised_command_line.PutCString(cmd);
3405               } else
3406                 revised_command_line.Printf(" %s", gdb_format_option.c_str());
3407 
3408               if (wants_raw_input &&
3409                   FindArgumentTerminator(cmd) == std::string::npos)
3410                 revised_command_line.PutCString(" --");
3411             } else {
3412               result.AppendErrorWithFormat(
3413                   "the '%s' command doesn't support the --gdb-format option\n",
3414                   cmd_obj->GetCommandName().str().c_str());
3415               return nullptr;
3416             }
3417           }
3418           break;
3419 
3420         default:
3421           result.AppendErrorWithFormat(
3422               "unknown command shorthand suffix: '%s'\n", suffix.c_str());
3423           return nullptr;
3424         }
3425       }
3426     }
3427     if (scratch_command.empty())
3428       done = true;
3429   }
3430 
3431   if (!scratch_command.empty())
3432     revised_command_line.Printf(" %s", scratch_command.c_str());
3433 
3434   if (cmd_obj != nullptr)
3435     command_line = std::string(revised_command_line.GetString());
3436 
3437   return cmd_obj;
3438 }
3439