LLDB mainline
CommandInterpreter.cpp
Go to the documentation of this file.
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 <chrono>
10#include <cstdlib>
11#include <limits>
12#include <memory>
13#include <optional>
14#include <string>
15#include <vector>
16
48
49#include "lldb/Core/Debugger.h"
50#include "lldb/Core/Module.h"
52#include "lldb/Core/Telemetry.h"
57#include "lldb/Utility/Log.h"
58#include "lldb/Utility/State.h"
59#include "lldb/Utility/Stream.h"
61#include "lldb/Utility/Timer.h"
62
63#include "lldb/Host/Config.h"
64#include "lldb/lldb-forward.h"
65#if LLDB_ENABLE_LIBEDIT
66#include "lldb/Host/Editline.h"
67#endif
68#include "lldb/Host/File.h"
69#include "lldb/Host/FileCache.h"
70#include "lldb/Host/Host.h"
71#include "lldb/Host/HostInfo.h"
72
79#include "lldb/Utility/Args.h"
80
82#include "lldb/Target/Process.h"
85#include "lldb/Target/Thread.h"
87
88#include "llvm/ADT/STLExtras.h"
89#include "llvm/ADT/ScopeExit.h"
90#include "llvm/ADT/SmallString.h"
91#include "llvm/Support/FormatAdapters.h"
92#include "llvm/Support/Path.h"
93#include "llvm/Support/PrettyStackTrace.h"
94#include "llvm/Support/ScopedPrinter.h"
95#include "llvm/Telemetry/Telemetry.h"
96
97#if defined(__APPLE__)
98#include <TargetConditionals.h>
99#endif
100
101using namespace lldb;
102using namespace lldb_private;
103
104static const char *k_white_space = " \t\v";
105
106static constexpr const char *InitFileWarning =
107 "There is a .lldbinit file in the current directory which is not being "
108 "read.\n"
109 "To silence this warning without sourcing in the local .lldbinit,\n"
110 "add the following to the lldbinit file in your home directory:\n"
111 " settings set target.load-cwd-lldbinit false\n"
112 "To allow lldb to source .lldbinit files in the current working "
113 "directory,\n"
114 "set the value of this variable to true. Only do so if you understand "
115 "and\n"
116 "accept the security risk.";
117
118const char *CommandInterpreter::g_no_argument = "<no-argument>";
119const char *CommandInterpreter::g_need_argument = "<need-argument>";
120const char *CommandInterpreter::g_argument = "<argument>";
121
122#define LLDB_PROPERTIES_interpreter
123#include "InterpreterProperties.inc"
124
125enum {
126#define LLDB_PROPERTIES_interpreter
127#include "InterpreterPropertiesEnum.inc"
128};
129
131 static constexpr llvm::StringLiteral class_name("lldb.commandInterpreter");
132 return class_name;
133}
134
136 bool synchronous_execution)
137 : Broadcaster(debugger.GetBroadcasterManager(),
139 Properties(std::make_shared<OptionValueProperties>("interpreter")),
141 m_debugger(debugger), m_synchronous_execution(true),
146 SetEventName(eBroadcastBitThreadShouldExit, "thread-should-exit");
147 SetEventName(eBroadcastBitResetPrompt, "reset-prompt");
149 SetSynchronous(synchronous_execution);
151 m_collection_sp->Initialize(g_interpreter_properties_def);
152}
153
155 const uint32_t idx = ePropertyExpandRegexAliases;
157 idx, g_interpreter_properties[idx].default_uint_value != 0);
158}
159
161 const uint32_t idx = ePropertyPromptOnQuit;
163 idx, g_interpreter_properties[idx].default_uint_value != 0);
164}
165
167 const uint32_t idx = ePropertyPromptOnQuit;
168 SetPropertyAtIndex(idx, enable);
169}
170
172 const uint32_t idx = ePropertySaveTranscript;
174 idx, g_interpreter_properties[idx].default_uint_value != 0);
175}
176
178 const uint32_t idx = ePropertySaveTranscript;
179 SetPropertyAtIndex(idx, enable);
180}
181
183 const uint32_t idx = ePropertySaveSessionOnQuit;
185 idx, g_interpreter_properties[idx].default_uint_value != 0);
186}
187
189 const uint32_t idx = ePropertySaveSessionOnQuit;
190 SetPropertyAtIndex(idx, enable);
191}
192
194 const uint32_t idx = ePropertyOpenTranscriptInEditor;
196 idx, g_interpreter_properties[idx].default_uint_value != 0);
197}
198
200 const uint32_t idx = ePropertyOpenTranscriptInEditor;
201 SetPropertyAtIndex(idx, enable);
202}
203
205 const uint32_t idx = ePropertySaveSessionDirectory;
206 return GetPropertyAtIndexAs<FileSpec>(idx, {});
207}
208
210 const uint32_t idx = ePropertySaveSessionDirectory;
211 SetPropertyAtIndex(idx, path);
212}
213
215 const uint32_t idx = ePropertyEchoCommands;
217 idx, g_interpreter_properties[idx].default_uint_value != 0);
218}
219
221 const uint32_t idx = ePropertyEchoCommands;
222 SetPropertyAtIndex(idx, enable);
223}
224
226 const uint32_t idx = ePropertyEchoCommentCommands;
228 idx, g_interpreter_properties[idx].default_uint_value != 0);
229}
230
232 const uint32_t idx = ePropertyEchoCommentCommands;
233 SetPropertyAtIndex(idx, enable);
234}
235
237 m_allow_exit_code = allow;
238 if (!allow)
239 m_quit_exit_code.reset();
240}
241
244 return false;
245 m_quit_exit_code = exit_code;
246 return true;
247}
248
249int CommandInterpreter::GetQuitExitCode(bool &exited) const {
250 exited = m_quit_exit_code.has_value();
251 if (exited)
252 return *m_quit_exit_code;
253 return 0;
254}
255
256void CommandInterpreter::ResolveCommand(const char *command_line,
257 CommandReturnObject &result) {
258 std::string command = command_line;
259 if (ResolveCommandImpl(command, result) != nullptr) {
260 result.AppendMessageWithFormat("%s", command.c_str());
262 }
263}
264
266 const uint32_t idx = ePropertyStopCmdSourceOnError;
268 idx, g_interpreter_properties[idx].default_uint_value != 0);
269}
270
272 const uint32_t idx = ePropertySpaceReplPrompts;
274 idx, g_interpreter_properties[idx].default_uint_value != 0);
275}
276
278 const uint32_t idx = ePropertyRepeatPreviousCommand;
280 idx, g_interpreter_properties[idx].default_uint_value != 0);
281}
282
284 const uint32_t idx = ePropertyRequireCommandOverwrite;
286 idx, g_interpreter_properties[idx].default_uint_value != 0);
287}
288
291
293
294 // An alias arguments vector to reuse - reset it before use...
295 OptionArgVectorSP alias_arguments_vector_sp(new OptionArgVector);
296
297 // Set up some initial aliases.
298 CommandObjectSP cmd_obj_sp = GetCommandSPExact("quit");
299 if (cmd_obj_sp) {
300 AddAlias("q", cmd_obj_sp);
301 AddAlias("exit", cmd_obj_sp);
302 }
303
304 cmd_obj_sp = GetCommandSPExact("_regexp-attach");
305 if (cmd_obj_sp)
306 AddAlias("attach", cmd_obj_sp)->SetSyntax(cmd_obj_sp->GetSyntax());
307
308 cmd_obj_sp = GetCommandSPExact("process detach");
309 if (cmd_obj_sp) {
310 AddAlias("detach", cmd_obj_sp);
311 }
312
313 cmd_obj_sp = GetCommandSPExact("process continue");
314 if (cmd_obj_sp) {
315 AddAlias("c", cmd_obj_sp);
316 AddAlias("continue", cmd_obj_sp);
317 }
318
319 // At this point, I'm leaving "b" command aliased to "_regexp-break". There's
320 // a catch-all regexp in the command that takes any unrecognized input and
321 // runs it as `break set <input>` and switching the command to break add
322 // would change that behavior. People who want to use the break add for the
323 // "b" alias can do so in their .lldbinit.
324 cmd_obj_sp = GetCommandSPExact("_regexp-break");
325 if (cmd_obj_sp)
326 AddAlias("b", cmd_obj_sp)->SetSyntax(cmd_obj_sp->GetSyntax());
327
328 cmd_obj_sp = GetCommandSPExact("_regexp-tbreak");
329 if (cmd_obj_sp)
330 AddAlias("tbreak", cmd_obj_sp)->SetSyntax(cmd_obj_sp->GetSyntax());
331
332 cmd_obj_sp = GetCommandSPExact("thread step-inst");
333 if (cmd_obj_sp) {
334 AddAlias("stepi", cmd_obj_sp);
335 AddAlias("si", cmd_obj_sp);
336 }
337
338 cmd_obj_sp = GetCommandSPExact("thread step-inst-over");
339 if (cmd_obj_sp) {
340 AddAlias("nexti", cmd_obj_sp);
341 AddAlias("ni", cmd_obj_sp);
342 }
343
344 cmd_obj_sp = GetCommandSPExact("_regexp-step");
345 if (cmd_obj_sp) {
346 AddAlias("s", cmd_obj_sp);
347 AddAlias("step", cmd_obj_sp);
348 CommandAlias *sif_alias = AddAlias(
349 "sif", cmd_obj_sp, "--end-linenumber block --step-in-target %1");
350 if (sif_alias) {
351 sif_alias->SetHelp("Step through the current block, stopping if you step "
352 "directly into a function whose name matches the "
353 "TargetFunctionName.");
354 sif_alias->SetSyntax("sif <TargetFunctionName>");
355 }
356 }
357
358 cmd_obj_sp = GetCommandSPExact("thread step-over");
359 if (cmd_obj_sp) {
360 AddAlias("n", cmd_obj_sp);
361 AddAlias("next", cmd_obj_sp);
362 }
363
364 cmd_obj_sp = GetCommandSPExact("thread step-out");
365 if (cmd_obj_sp) {
366 AddAlias("finish", cmd_obj_sp);
367 }
368
369 cmd_obj_sp = GetCommandSPExact("frame select");
370 if (cmd_obj_sp) {
371 AddAlias("f", cmd_obj_sp);
372 }
373
374 cmd_obj_sp = GetCommandSPExact("thread select");
375 if (cmd_obj_sp) {
376 AddAlias("t", cmd_obj_sp);
377 }
378
379 cmd_obj_sp = GetCommandSPExact("_regexp-jump");
380 if (cmd_obj_sp) {
381 AddAlias("j", cmd_obj_sp)->SetSyntax(cmd_obj_sp->GetSyntax());
382 AddAlias("jump", cmd_obj_sp)->SetSyntax(cmd_obj_sp->GetSyntax());
383 }
384
385 cmd_obj_sp = GetCommandSPExact("_regexp-list");
386 if (cmd_obj_sp) {
387 AddAlias("l", cmd_obj_sp)->SetSyntax(cmd_obj_sp->GetSyntax());
388 AddAlias("list", cmd_obj_sp)->SetSyntax(cmd_obj_sp->GetSyntax());
389 }
390
391 cmd_obj_sp = GetCommandSPExact("_regexp-env");
392 if (cmd_obj_sp)
393 AddAlias("env", cmd_obj_sp)->SetSyntax(cmd_obj_sp->GetSyntax());
394
395 cmd_obj_sp = GetCommandSPExact("memory read");
396 if (cmd_obj_sp)
397 AddAlias("x", cmd_obj_sp);
398
399 cmd_obj_sp = GetCommandSPExact("_regexp-up");
400 if (cmd_obj_sp)
401 AddAlias("up", cmd_obj_sp)->SetSyntax(cmd_obj_sp->GetSyntax());
402
403 cmd_obj_sp = GetCommandSPExact("_regexp-down");
404 if (cmd_obj_sp)
405 AddAlias("down", cmd_obj_sp)->SetSyntax(cmd_obj_sp->GetSyntax());
406
407 cmd_obj_sp = GetCommandSPExact("_regexp-display");
408 if (cmd_obj_sp)
409 AddAlias("display", cmd_obj_sp)->SetSyntax(cmd_obj_sp->GetSyntax());
410
411 cmd_obj_sp = GetCommandSPExact("disassemble");
412 if (cmd_obj_sp)
413 AddAlias("dis", cmd_obj_sp);
414
415 cmd_obj_sp = GetCommandSPExact("disassemble");
416 if (cmd_obj_sp)
417 AddAlias("di", cmd_obj_sp);
418
419 cmd_obj_sp = GetCommandSPExact("_regexp-undisplay");
420 if (cmd_obj_sp)
421 AddAlias("undisplay", cmd_obj_sp)->SetSyntax(cmd_obj_sp->GetSyntax());
422
423 cmd_obj_sp = GetCommandSPExact("_regexp-bt");
424 if (cmd_obj_sp)
425 AddAlias("bt", cmd_obj_sp)->SetSyntax(cmd_obj_sp->GetSyntax());
426
427 cmd_obj_sp = GetCommandSPExact("target create");
428 if (cmd_obj_sp)
429 AddAlias("file", cmd_obj_sp);
430
431 cmd_obj_sp = GetCommandSPExact("target modules");
432 if (cmd_obj_sp)
433 AddAlias("image", cmd_obj_sp);
434
435 alias_arguments_vector_sp = std::make_shared<OptionArgVector>();
436
437 cmd_obj_sp = GetCommandSPExact("dwim-print");
438 if (cmd_obj_sp) {
439 AddAlias("p", cmd_obj_sp, "--")->SetHelpLong("");
440 AddAlias("print", cmd_obj_sp, "--")->SetHelpLong("");
441 if (auto *po = AddAlias("po", cmd_obj_sp, "-O --")) {
442 po->SetHelp("Evaluate an expression on the current thread. Displays any "
443 "returned value with formatting "
444 "controlled by the type's author.");
445 po->SetHelpLong("");
446 }
447 }
448
449 cmd_obj_sp = GetCommandSPExact("expression");
450 if (cmd_obj_sp) {
451 // Ensure `e` runs `expression`.
452 AddAlias("e", cmd_obj_sp);
453 AddAlias("call", cmd_obj_sp, "--")->SetHelpLong("");
454 CommandAlias *parray_alias =
455 AddAlias("parray", cmd_obj_sp, "--element-count %1 --");
456 if (parray_alias) {
457 parray_alias->SetHelp(
458 "parray <COUNT> <EXPRESSION> -- lldb will evaluate EXPRESSION "
459 "to get a typed-pointer-to-an-array in memory, and will display "
460 "COUNT elements of that type from the array.");
461 parray_alias->SetHelpLong("");
462 }
463 CommandAlias *poarray_alias = AddAlias(
464 "poarray", cmd_obj_sp, "--object-description --element-count %1 --");
465 if (poarray_alias) {
466 poarray_alias->SetHelp(
467 "poarray <COUNT> <EXPRESSION> -- lldb will "
468 "evaluate EXPRESSION to get the address of an array of COUNT "
469 "objects in memory, and will call po on them.");
470 poarray_alias->SetHelpLong("");
471 }
472 }
473
474 cmd_obj_sp = GetCommandSPExact("platform shell");
475 if (cmd_obj_sp) {
476 CommandAlias *shell_alias = AddAlias("shell", cmd_obj_sp, " --host --");
477 if (shell_alias) {
478 shell_alias->SetHelp("Run a shell command on the host.");
479 shell_alias->SetHelpLong("");
480 shell_alias->SetSyntax("shell <shell-command>");
481 }
482 }
483
484 cmd_obj_sp = GetCommandSPExact("process kill");
485 if (cmd_obj_sp) {
486 AddAlias("kill", cmd_obj_sp);
487 }
488
489 cmd_obj_sp = GetCommandSPExact("process launch");
490 if (cmd_obj_sp) {
491 alias_arguments_vector_sp = std::make_shared<OptionArgVector>();
492#if defined(__APPLE__)
493#if TARGET_OS_IPHONE
494 AddAlias("r", cmd_obj_sp, "--");
495 AddAlias("run", cmd_obj_sp, "--");
496#else
497 AddAlias("r", cmd_obj_sp, "--shell-expand-args true --");
498 AddAlias("run", cmd_obj_sp, "--shell-expand-args true --");
499#endif
500#else
501 StreamString defaultshell;
502 defaultshell.Printf("--shell=%s --",
503 HostInfo::GetDefaultShell().GetPath().c_str());
504 AddAlias("r", cmd_obj_sp, defaultshell.GetString());
505 AddAlias("run", cmd_obj_sp, defaultshell.GetString());
506#endif
507 }
508
509 cmd_obj_sp = GetCommandSPExact("target symbols add");
510 if (cmd_obj_sp) {
511 AddAlias("add-dsym", cmd_obj_sp);
512 }
513
514 cmd_obj_sp = GetCommandSPExact("breakpoint set");
515 if (cmd_obj_sp) {
516 AddAlias("rbreak", cmd_obj_sp, "--func-regex %1");
517 }
518
519 cmd_obj_sp = GetCommandSPExact("frame variable");
520 if (cmd_obj_sp) {
521 AddAlias("v", cmd_obj_sp);
522 AddAlias("var", cmd_obj_sp);
523 AddAlias("vo", cmd_obj_sp, "--object-description");
524 }
525
526 cmd_obj_sp = GetCommandSPExact("register");
527 if (cmd_obj_sp) {
528 AddAlias("re", cmd_obj_sp);
529 }
530
531 cmd_obj_sp = GetCommandSPExact("scripting run");
532 if (cmd_obj_sp) {
533 AddAlias("script", cmd_obj_sp);
534 }
535
536 cmd_obj_sp = GetCommandSPExact("session history");
537 if (cmd_obj_sp) {
538 AddAlias("history", cmd_obj_sp);
539 }
540
541 cmd_obj_sp = GetCommandSPExact("help");
542 if (cmd_obj_sp) {
543 AddAlias("h", cmd_obj_sp);
544 }
545}
546
548
550 // This function has not yet been implemented.
551
552 // Look for any embedded script command
553 // If found,
554 // get interpreter object from the command dictionary,
555 // call execute_one_command on it,
556 // get the results as a string,
557 // substitute that string for current stuff.
558
559 return arg;
560}
561
562#define REGISTER_COMMAND_OBJECT(NAME, CLASS) \
563 m_command_dict[NAME] = std::make_shared<CLASS>(*this);
564
567
598
599 // clang-format off
600 const char *break_regexes[][2] = {
601 {"^(.*[^[:space:]])[[:space:]]*:[[:space:]]*([[:digit:]]+)[[:space:]]*:[[:space:]]*([[:digit:]]+)[[:space:]]*$",
602 "breakpoint set --file '%1' --line %2 --column %3"},
603 {"^(.*[^[:space:]])[[:space:]]*:[[:space:]]*([[:digit:]]+)[[:space:]]*$",
604 "breakpoint set --file '%1' --line %2"},
605 {"^/([^/]+)/$", "breakpoint set --source-pattern-regexp '%1'"},
606 {"^([[:digit:]]+)[[:space:]]*$", "breakpoint set --line %1"},
607 {"^\\*?(0x[[:xdigit:]]+)[[:space:]]*$", "breakpoint set --address %1"},
608 {"^[\"']?([-+]?\\[.*\\])[\"']?[[:space:]]*$",
609 "breakpoint set --name '%1'"},
610 {"^(-.*)$", "breakpoint set %1"},
611 {"^(.*[^[:space:]])`(.*[^[:space:]])[[:space:]]*$",
612 "breakpoint set --name '%2' --shlib '%1'"},
613 {"^\\&(.*[^[:space:]])[[:space:]]*$",
614 "breakpoint set --name '%1' --skip-prologue=0"},
615 {"^[\"']?(.*[^[:space:]\"'])[\"']?[[:space:]]*$",
616 "breakpoint set --name '%1'"}};
617 // clang-format on
618
619 size_t num_regexes = std::size(break_regexes);
620
621 std::unique_ptr<CommandObjectRegexCommand> break_regex_cmd_up(
623 *this, "_regexp-break",
624 "Set a breakpoint using one of several shorthand formats, or list "
625 "the existing breakpoints if no arguments are provided.",
626 "\n"
627 "_regexp-break <filename>:<linenum>:<colnum>\n"
628 " main.c:12:21 // Break at line 12 and column "
629 "21 of main.c\n\n"
630 "_regexp-break <filename>:<linenum>\n"
631 " main.c:12 // Break at line 12 of "
632 "main.c\n\n"
633 "_regexp-break <linenum>\n"
634 " 12 // Break at line 12 of current "
635 "file\n\n"
636 "_regexp-break 0x<address>\n"
637 " 0x1234000 // Break at address "
638 "0x1234000\n\n"
639 "_regexp-break <name>\n"
640 " main // Break in 'main' after the "
641 "prologue\n\n"
642 "_regexp-break &<name>\n"
643 " &main // Break at first instruction "
644 "in 'main'\n\n"
645 "_regexp-break <module>`<name>\n"
646 " libc.so`malloc // Break in 'malloc' from "
647 "'libc.so'\n\n"
648 "_regexp-break /<source-regex>/\n"
649 " /break here/ // Break on source lines in "
650 "current file\n"
651 " // containing text 'break "
652 "here'.\n"
653 "_regexp-break\n"
654 " // List the existing "
655 "breakpoints\n",
657
658 if (break_regex_cmd_up) {
659 bool success = true;
660 for (size_t i = 0; i < num_regexes; i++) {
661 success = break_regex_cmd_up->AddRegexCommand(break_regexes[i][0],
662 break_regexes[i][1]);
663 if (!success)
664 break;
665 }
666 success =
667 break_regex_cmd_up->AddRegexCommand("^$", "breakpoint list --full");
668
669 if (success) {
670 CommandObjectSP break_regex_cmd_sp(break_regex_cmd_up.release());
671 m_command_dict[std::string(break_regex_cmd_sp->GetCommandName())] =
672 break_regex_cmd_sp;
673 }
674 }
675
676 // clang-format off
677 // FIXME: It would be simpler to just use the linespec's directly here, but
678 // the `b` alias allows "foo.c : 12 : 45" but the linespec parser
679 // is more rigorous, and doesn't strip spaces, so the two are not equivalent.
680 const char *break_add_regexes[][2] = {
681 {"^(.*[^[:space:]])[[:space:]]*:[[:space:]]*([[:digit:]]+)[[:space:]]*:[[:space:]]*([[:digit:]]+)[[:space:]]*$",
682 "breakpoint add file --file '%1' --line %2 --column %3"},
683 {"^(.*[^[:space:]])[[:space:]]*:[[:space:]]*([[:digit:]]+)[[:space:]]*$",
684 "breakpoint add file --file '%1' --line %2"},
685 {"^/([^/]+)/$", "breakpoint add pattern -- %1"},
686 {"^([[:digit:]]+)[[:space:]]*$",
687 "breakpoint add file --line %1"},
688 {"^\\*?(0x[[:xdigit:]]+)[[:space:]]*$",
689 "breakpoint add address %1"},
690 {"^[\"']?([-+]?\\[.*\\])[\"']?[[:space:]]*$",
691 "breakpoint add name '%1'"},
692 {"^(-.*)$",
693 "breakpoint add name '%1'"},
694 {"^(.*[^[:space:]])`(.*[^[:space:]])[[:space:]]*$",
695 "breakpoint add name '%2' --shlib '%1'"},
696 {"^\\&(.*[^[:space:]])[[:space:]]*$",
697 "breakpoint add name '%1' --skip-prologue=0"},
698 {"^[\"']?(.*[^[:space:]\"'])[\"']?[[:space:]]*$",
699 "breakpoint add name '%1'"}};
700 // clang-format on
701
702 size_t num_add_regexes = std::size(break_add_regexes);
703
704 std::unique_ptr<CommandObjectRegexCommand> break_add_regex_cmd_up(
706 *this, "_regexp-break-add",
707 "Set a breakpoint using one of several shorthand formats, or list "
708 "the existing breakpoints if no arguments are provided.",
709 "\n"
710 "_regexp-break-add <filename>:<linenum>:<colnum>\n"
711 " main.c:12:21 // Break at line 12 and column "
712 "21 of main.c\n\n"
713 "_regexp-break-add <filename>:<linenum>\n"
714 " main.c:12 // Break at line 12 of "
715 "main.c\n\n"
716 "_regexp-break-add <linenum>\n"
717 " 12 // Break at line 12 of current "
718 "file\n\n"
719 "_regexp-break-add 0x<address>\n"
720 " 0x1234000 // Break at address "
721 "0x1234000\n\n"
722 "_regexp-break-add <name>\n"
723 " main // Break in 'main' after the "
724 "prologue\n\n"
725 "_regexp-break-add &<name>\n"
726 " &main // Break at first instruction "
727 "in 'main'\n\n"
728 "_regexp-break-add <module>`<name>\n"
729 " libc.so`malloc // Break in 'malloc' from "
730 "'libc.so'\n\n"
731 "_regexp-break-add /<source-regex>/\n"
732 " /break here/ // Break on source lines in "
733 "current file\n"
734 " // containing text 'break "
735 "here'.\n"
736 "_regexp-break-add\n"
737 " // List the existing "
738 "breakpoints\n",
740
741 if (break_add_regex_cmd_up) {
742 bool success = true;
743 for (size_t i = 0; i < num_add_regexes; i++) {
744 success = break_add_regex_cmd_up->AddRegexCommand(
745 break_add_regexes[i][0], break_add_regexes[i][1]);
746 if (!success)
747 break;
748 }
749 success =
750 break_add_regex_cmd_up->AddRegexCommand("^$", "breakpoint list --full");
751
752 if (success) {
753 CommandObjectSP break_add_regex_cmd_sp(break_add_regex_cmd_up.release());
754 m_command_dict[std::string(break_add_regex_cmd_sp->GetCommandName())] =
755 break_add_regex_cmd_sp;
756 }
757 }
758
759 std::unique_ptr<CommandObjectRegexCommand> tbreak_regex_cmd_up(
761 *this, "_regexp-tbreak",
762 "Set a one-shot breakpoint using one of several shorthand formats.",
763 "\n"
764 "_regexp-break <filename>:<linenum>:<colnum>\n"
765 " main.c:12:21 // Break at line 12 and column "
766 "21 of main.c\n\n"
767 "_regexp-break <filename>:<linenum>\n"
768 " main.c:12 // Break at line 12 of "
769 "main.c\n\n"
770 "_regexp-break <linenum>\n"
771 " 12 // Break at line 12 of current "
772 "file\n\n"
773 "_regexp-break 0x<address>\n"
774 " 0x1234000 // Break at address "
775 "0x1234000\n\n"
776 "_regexp-break <name>\n"
777 " main // Break in 'main' after the "
778 "prologue\n\n"
779 "_regexp-break &<name>\n"
780 " &main // Break at first instruction "
781 "in 'main'\n\n"
782 "_regexp-break <module>`<name>\n"
783 " libc.so`malloc // Break in 'malloc' from "
784 "'libc.so'\n\n"
785 "_regexp-break /<source-regex>/\n"
786 " /break here/ // Break on source lines in "
787 "current file\n"
788 " // containing text 'break "
789 "here'.\n",
791
792 if (tbreak_regex_cmd_up) {
793 bool success = true;
794 for (size_t i = 0; i < num_regexes; i++) {
795 std::string command = break_regexes[i][1];
796 command += " -o 1";
797 success =
798 tbreak_regex_cmd_up->AddRegexCommand(break_regexes[i][0], command);
799 if (!success)
800 break;
801 }
802 success =
803 tbreak_regex_cmd_up->AddRegexCommand("^$", "breakpoint list --full");
804
805 if (success) {
806 CommandObjectSP tbreak_regex_cmd_sp(tbreak_regex_cmd_up.release());
807 m_command_dict[std::string(tbreak_regex_cmd_sp->GetCommandName())] =
808 tbreak_regex_cmd_sp;
809 }
810 }
811
812 std::unique_ptr<CommandObjectRegexCommand> attach_regex_cmd_up(
814 *this, "_regexp-attach", "Attach to process by ID or name.",
815 "_regexp-attach <pid> | <process-name>", 0, false));
816 if (attach_regex_cmd_up) {
817 if (attach_regex_cmd_up->AddRegexCommand("^([0-9]+)[[:space:]]*$",
818 "process attach --pid %1") &&
819 attach_regex_cmd_up->AddRegexCommand(
820 "^(-.*|.* -.*)$", "process attach %1") && // Any options that are
821 // specified get passed to
822 // 'process attach'
823 attach_regex_cmd_up->AddRegexCommand("^(.+)$",
824 "process attach --name '%1'") &&
825 attach_regex_cmd_up->AddRegexCommand("^$", "process attach")) {
826 CommandObjectSP attach_regex_cmd_sp(attach_regex_cmd_up.release());
827 m_command_dict[std::string(attach_regex_cmd_sp->GetCommandName())] =
828 attach_regex_cmd_sp;
829 }
830 }
831
832 std::unique_ptr<CommandObjectRegexCommand> down_regex_cmd_up(
833 new CommandObjectRegexCommand(*this, "_regexp-down",
834 "Select a newer stack frame. Defaults to "
835 "moving one frame, a numeric argument can "
836 "specify an arbitrary number.",
837 "_regexp-down [<count>]", 0, false));
838 if (down_regex_cmd_up) {
839 if (down_regex_cmd_up->AddRegexCommand("^$", "frame select -r -1") &&
840 down_regex_cmd_up->AddRegexCommand("^([0-9]+)$",
841 "frame select -r -%1")) {
842 CommandObjectSP down_regex_cmd_sp(down_regex_cmd_up.release());
843 m_command_dict[std::string(down_regex_cmd_sp->GetCommandName())] =
844 down_regex_cmd_sp;
845 }
846 }
847
848 std::unique_ptr<CommandObjectRegexCommand> up_regex_cmd_up(
850 *this, "_regexp-up",
851 "Select an older stack frame. Defaults to moving one "
852 "frame, a numeric argument can specify an arbitrary number.",
853 "_regexp-up [<count>]", 0, false));
854 if (up_regex_cmd_up) {
855 if (up_regex_cmd_up->AddRegexCommand("^$", "frame select -r 1") &&
856 up_regex_cmd_up->AddRegexCommand("^([0-9]+)$", "frame select -r %1")) {
857 CommandObjectSP up_regex_cmd_sp(up_regex_cmd_up.release());
858 m_command_dict[std::string(up_regex_cmd_sp->GetCommandName())] =
859 up_regex_cmd_sp;
860 }
861 }
862
863 std::unique_ptr<CommandObjectRegexCommand> display_regex_cmd_up(
865 *this, "_regexp-display",
866 "Evaluate an expression at every stop (see 'help target stop-hook'.)",
867 "_regexp-display expression", 0, false));
868 if (display_regex_cmd_up) {
869 if (display_regex_cmd_up->AddRegexCommand(
870 "^(.+)$", "target stop-hook add -o \"expr -- %1\"")) {
871 CommandObjectSP display_regex_cmd_sp(display_regex_cmd_up.release());
872 m_command_dict[std::string(display_regex_cmd_sp->GetCommandName())] =
873 display_regex_cmd_sp;
874 }
875 }
876
877 std::unique_ptr<CommandObjectRegexCommand> undisplay_regex_cmd_up(
878 new CommandObjectRegexCommand(*this, "_regexp-undisplay",
879 "Stop displaying expression at every "
880 "stop (specified by stop-hook index.)",
881 "_regexp-undisplay stop-hook-number", 0,
882 false));
883 if (undisplay_regex_cmd_up) {
884 if (undisplay_regex_cmd_up->AddRegexCommand("^([0-9]+)$",
885 "target stop-hook delete %1")) {
886 CommandObjectSP undisplay_regex_cmd_sp(undisplay_regex_cmd_up.release());
887 m_command_dict[std::string(undisplay_regex_cmd_sp->GetCommandName())] =
888 undisplay_regex_cmd_sp;
889 }
890 }
891
892 std::unique_ptr<CommandObjectRegexCommand> connect_gdb_remote_cmd_up(
894 *this, "gdb-remote",
895 "Connect to a process via remote GDB server.\n"
896 "If no host is specified, localhost is assumed.\n"
897 "gdb-remote is an abbreviation for 'process connect --plugin "
898 "gdb-remote connect://<hostname>:<port>'\n",
899 "gdb-remote [<hostname>:]<portnum>", 0, false));
900 if (connect_gdb_remote_cmd_up) {
901 if (connect_gdb_remote_cmd_up->AddRegexCommand(
902 "^([^:]+|\\[[0-9a-fA-F:]+.*\\]):([0-9]+)$",
903 "process connect --plugin gdb-remote connect://%1:%2") &&
904 connect_gdb_remote_cmd_up->AddRegexCommand(
905 "^([[:digit:]]+)$",
906 "process connect --plugin gdb-remote connect://localhost:%1")) {
907 CommandObjectSP command_sp(connect_gdb_remote_cmd_up.release());
908 m_command_dict[std::string(command_sp->GetCommandName())] = command_sp;
909 }
910 }
911
912 std::unique_ptr<CommandObjectRegexCommand> connect_kdp_remote_cmd_up(
914 *this, "kdp-remote",
915 "Connect to a process via remote KDP server.\n"
916 "If no UDP port is specified, port 41139 is assumed.\n"
917 "kdp-remote is an abbreviation for 'process connect --plugin "
918 "kdp-remote udp://<hostname>:<port>'\n",
919 "kdp-remote <hostname>[:<portnum>]", 0, false));
920 if (connect_kdp_remote_cmd_up) {
921 if (connect_kdp_remote_cmd_up->AddRegexCommand(
922 "^([^:]+:[[:digit:]]+)$",
923 "process connect --plugin kdp-remote udp://%1") &&
924 connect_kdp_remote_cmd_up->AddRegexCommand(
925 "^(.+)$", "process connect --plugin kdp-remote udp://%1:41139")) {
926 CommandObjectSP command_sp(connect_kdp_remote_cmd_up.release());
927 m_command_dict[std::string(command_sp->GetCommandName())] = command_sp;
928 }
929 }
930
931 std::unique_ptr<CommandObjectRegexCommand> bt_regex_cmd_up(
933 *this, "_regexp-bt",
934 "Show backtrace of the current thread's call stack. Any numeric "
935 "argument displays at most that many frames. The argument 'all' "
936 "displays all threads. Use 'settings set frame-format' to customize "
937 "the printing of individual frames and 'settings set thread-format' "
938 "to customize the thread header. Frame recognizers may filter the "
939 "list. Use 'thread backtrace -u (--unfiltered)' to see them all.",
940 "bt [<digit> | all]", 0, false));
941 if (bt_regex_cmd_up) {
942 // accept but don't document "bt -c <number>" -- before bt was a regex
943 // command if you wanted to backtrace three frames you would do "bt -c 3"
944 // but the intention is to have this emulate the gdb "bt" command and so
945 // now "bt 3" is the preferred form, in line with gdb.
946 if (bt_regex_cmd_up->AddRegexCommand("^([[:digit:]]+)[[:space:]]*$",
947 "thread backtrace -c %1") &&
948 bt_regex_cmd_up->AddRegexCommand("^(-[^[:space:]].*)$",
949 "thread backtrace %1") &&
950 bt_regex_cmd_up->AddRegexCommand("^all[[:space:]]*$",
951 "thread backtrace all") &&
952 bt_regex_cmd_up->AddRegexCommand("^[[:space:]]*$",
953 "thread backtrace")) {
954 CommandObjectSP command_sp(bt_regex_cmd_up.release());
955 m_command_dict[std::string(command_sp->GetCommandName())] = command_sp;
956 }
957 }
958
959 std::unique_ptr<CommandObjectRegexCommand> list_regex_cmd_up(
961 *this, "_regexp-list",
962 "List relevant source code using one of several shorthand formats.",
963 "\n"
964 "_regexp-list <file>:<line> // List around specific file/line\n"
965 "_regexp-list <line> // List current file around specified "
966 "line\n"
967 "_regexp-list <function-name> // List specified function\n"
968 "_regexp-list 0x<address> // List around specified address\n"
969 "_regexp-list -[<count>] // List previous <count> lines\n"
970 "_regexp-list // List subsequent lines",
972 if (list_regex_cmd_up) {
973 if (list_regex_cmd_up->AddRegexCommand("^([0-9]+)[[:space:]]*$",
974 "source list --line %1") &&
975 list_regex_cmd_up->AddRegexCommand(
976 "^(.*[^[:space:]])[[:space:]]*:[[:space:]]*([[:digit:]]+)[[:space:]"
977 "]*$",
978 "source list --file '%1' --line %2") &&
979 list_regex_cmd_up->AddRegexCommand(
980 "^\\*?(0x[[:xdigit:]]+)[[:space:]]*$",
981 "source list --address %1") &&
982 list_regex_cmd_up->AddRegexCommand("^-[[:space:]]*$",
983 "source list --reverse") &&
984 list_regex_cmd_up->AddRegexCommand(
985 "^-([[:digit:]]+)[[:space:]]*$",
986 "source list --reverse --count %1") &&
987 list_regex_cmd_up->AddRegexCommand("^(.+)$",
988 "source list --name \"%1\"") &&
989 list_regex_cmd_up->AddRegexCommand("^$", "source list")) {
990 CommandObjectSP list_regex_cmd_sp(list_regex_cmd_up.release());
991 m_command_dict[std::string(list_regex_cmd_sp->GetCommandName())] =
992 list_regex_cmd_sp;
993 }
994 }
995
996 std::unique_ptr<CommandObjectRegexCommand> env_regex_cmd_up(
998 *this, "_regexp-env",
999 "Shorthand for viewing and setting environment variables.",
1000 "\n"
1001 "_regexp-env // Show environment\n"
1002 "_regexp-env <name>=<value> // Set an environment variable",
1003 0, false));
1004 if (env_regex_cmd_up) {
1005 if (env_regex_cmd_up->AddRegexCommand("^$",
1006 "settings show target.env-vars") &&
1007 env_regex_cmd_up->AddRegexCommand("^([A-Za-z_][A-Za-z_0-9]*=.*)$",
1008 "settings set target.env-vars %1")) {
1009 CommandObjectSP env_regex_cmd_sp(env_regex_cmd_up.release());
1010 m_command_dict[std::string(env_regex_cmd_sp->GetCommandName())] =
1011 env_regex_cmd_sp;
1012 }
1013 }
1014
1015 std::unique_ptr<CommandObjectRegexCommand> jump_regex_cmd_up(
1017 *this, "_regexp-jump", "Set the program counter to a new address.",
1018 "\n"
1019 "_regexp-jump <line>\n"
1020 "_regexp-jump +<line-offset> | -<line-offset>\n"
1021 "_regexp-jump <file>:<line>\n"
1022 "_regexp-jump *<addr>\n",
1023 0, false));
1024 if (jump_regex_cmd_up) {
1025 if (jump_regex_cmd_up->AddRegexCommand("^\\*(.*)$",
1026 "thread jump --addr %1") &&
1027 jump_regex_cmd_up->AddRegexCommand("^([0-9]+)$",
1028 "thread jump --line %1") &&
1029 jump_regex_cmd_up->AddRegexCommand("^([^:]+):([0-9]+)$",
1030 "thread jump --file %1 --line %2") &&
1031 jump_regex_cmd_up->AddRegexCommand("^([+\\-][0-9]+)$",
1032 "thread jump --by %1")) {
1033 CommandObjectSP jump_regex_cmd_sp(jump_regex_cmd_up.release());
1034 m_command_dict[std::string(jump_regex_cmd_sp->GetCommandName())] =
1035 jump_regex_cmd_sp;
1036 }
1037 }
1038
1039 std::shared_ptr<CommandObjectRegexCommand> step_regex_cmd_sp(
1041 *this, "_regexp-step",
1042 "Single step, optionally to a specific function.",
1043 "\n"
1044 "_regexp-step // Single step\n"
1045 "_regexp-step <function-name> // Step into the named function\n",
1046 0, false));
1047 if (step_regex_cmd_sp) {
1048 if (step_regex_cmd_sp->AddRegexCommand("^[[:space:]]*$",
1049 "thread step-in") &&
1050 step_regex_cmd_sp->AddRegexCommand("^[[:space:]]*(-.+)$",
1051 "thread step-in %1") &&
1052 step_regex_cmd_sp->AddRegexCommand(
1053 "^[[:space:]]*(.+)[[:space:]]*$",
1054 "thread step-in --end-linenumber block --step-in-target %1")) {
1055 m_command_dict[std::string(step_regex_cmd_sp->GetCommandName())] =
1056 step_regex_cmd_sp;
1057 }
1058 }
1059}
1060
1062 const char *cmd_str, bool include_aliases, StringList &matches,
1063 StringList &descriptions) {
1065 &descriptions);
1066
1067 if (include_aliases) {
1069 &descriptions);
1070 }
1071
1072 return matches.GetSize();
1073}
1074
1077 Status &result) {
1078 result.Clear();
1079
1080 auto get_multi_or_report_error =
1081 [&result](CommandObjectSP cmd_sp,
1082 const char *name) -> CommandObjectMultiword * {
1083 if (!cmd_sp) {
1085 "Path component: '%s' not found", name);
1086 return nullptr;
1087 }
1088 if (!cmd_sp->IsUserCommand()) {
1090 "Path component: '%s' is not a user "
1091 "command",
1092 name);
1093 return nullptr;
1094 }
1095 CommandObjectMultiword *cmd_as_multi = cmd_sp->GetAsMultiwordCommand();
1096 if (!cmd_as_multi) {
1098 "Path component: '%s' is not a container "
1099 "command",
1100 name);
1101 return nullptr;
1102 }
1103 return cmd_as_multi;
1104 };
1105
1106 size_t num_args = path.GetArgumentCount();
1107 if (num_args == 0) {
1108 result = Status::FromErrorString("empty command path");
1109 return nullptr;
1110 }
1111
1112 if (num_args == 1 && leaf_is_command) {
1113 // We just got a leaf command to be added to the root. That's not an error,
1114 // just return null for the container.
1115 return nullptr;
1116 }
1117
1118 // Start by getting the root command from the interpreter.
1119 const char *cur_name = path.GetArgumentAtIndex(0);
1120 CommandObjectSP cur_cmd_sp = GetCommandSPExact(cur_name);
1121 CommandObjectMultiword *cur_as_multi =
1122 get_multi_or_report_error(cur_cmd_sp, cur_name);
1123 if (cur_as_multi == nullptr)
1124 return nullptr;
1125
1126 size_t num_path_elements = num_args - (leaf_is_command ? 1 : 0);
1127 for (size_t cursor = 1; cursor < num_path_elements && cur_as_multi != nullptr;
1128 cursor++) {
1129 cur_name = path.GetArgumentAtIndex(cursor);
1130 cur_cmd_sp = cur_as_multi->GetSubcommandSPExact(cur_name);
1131 cur_as_multi = get_multi_or_report_error(cur_cmd_sp, cur_name);
1132 }
1133 return cur_as_multi;
1134}
1135
1137 auto frame_sp = GetExecutionContext().GetFrameSP();
1138 if (!frame_sp)
1139 return {};
1140 auto frame_language =
1141 Language::GetPrimaryLanguage(frame_sp->GuessLanguage().AsLanguageType());
1142
1143 auto it = m_command_dict.find("language");
1144 if (it == m_command_dict.end())
1145 return {};
1146 // The root "language" command.
1147 CommandObjectSP language_cmd_sp = it->second;
1148
1149 auto *plugin = Language::FindPlugin(frame_language);
1150 if (!plugin)
1151 return {};
1152 // "cplusplus", "objc", etc.
1153 auto lang_name = plugin->GetPluginName();
1154
1155 return language_cmd_sp->GetSubcommandSPExact(lang_name);
1156}
1157
1159CommandInterpreter::GetCommandSP(llvm::StringRef cmd_str, bool include_aliases,
1160 bool exact, StringList *matches,
1161 StringList *descriptions) const {
1162 CommandObjectSP command_sp;
1163
1164 std::string cmd = std::string(cmd_str);
1165
1166 if (HasCommands()) {
1167 auto pos = m_command_dict.find(cmd);
1168 if (pos != m_command_dict.end())
1169 command_sp = pos->second;
1170 }
1171
1172 if (include_aliases && HasAliases()) {
1173 auto alias_pos = m_alias_dict.find(cmd);
1174 if (alias_pos != m_alias_dict.end())
1175 command_sp = alias_pos->second;
1176 }
1177
1178 if (HasUserCommands()) {
1179 auto pos = m_user_dict.find(cmd);
1180 if (pos != m_user_dict.end())
1181 command_sp = pos->second;
1182 }
1183
1185 auto pos = m_user_mw_dict.find(cmd);
1186 if (pos != m_user_mw_dict.end())
1187 command_sp = pos->second;
1188 }
1189
1190 StringList local_matches;
1191
1192 if (!exact && !command_sp) {
1193 // We will only get into here if we didn't find any exact matches.
1194
1195 CommandObjectSP user_match_sp, user_mw_match_sp, alias_match_sp,
1196 real_match_sp;
1197
1198 if (matches == nullptr)
1199 matches = &local_matches;
1200
1201 unsigned int num_cmd_matches = 0;
1202 unsigned int num_alias_matches = 0;
1203 unsigned int num_user_matches = 0;
1204 unsigned int num_user_mw_matches = 0;
1205
1206 // Look through the command dictionaries one by one, and if we get only one
1207 // match from any of them in toto, then return that, otherwise return an
1208 // empty CommandObjectSP and the list of matches.
1209
1210 if (HasCommands()) {
1211 num_cmd_matches = AddNamesMatchingPartialString(m_command_dict, cmd_str,
1212 *matches, descriptions);
1213 }
1214
1215 if (num_cmd_matches == 1) {
1216 cmd.assign(matches->GetStringAtIndex(0));
1217 auto pos = m_command_dict.find(cmd);
1218 if (pos != m_command_dict.end())
1219 real_match_sp = pos->second;
1220 }
1221
1222 if (include_aliases && HasAliases()) {
1223 num_alias_matches = AddNamesMatchingPartialString(m_alias_dict, cmd_str,
1224 *matches, descriptions);
1225 }
1226
1227 if (num_alias_matches == 1) {
1228 cmd.assign(matches->GetStringAtIndex(num_cmd_matches));
1229 auto alias_pos = m_alias_dict.find(cmd);
1230 if (alias_pos != m_alias_dict.end())
1231 alias_match_sp = alias_pos->second;
1232 }
1233
1234 if (HasUserCommands()) {
1235 num_user_matches = AddNamesMatchingPartialString(m_user_dict, cmd_str,
1236 *matches, descriptions);
1237 }
1238
1239 if (num_user_matches == 1) {
1240 cmd.assign(
1241 matches->GetStringAtIndex(num_cmd_matches + num_alias_matches));
1242
1243 auto pos = m_user_dict.find(cmd);
1244 if (pos != m_user_dict.end())
1245 user_match_sp = pos->second;
1246 }
1247
1249 num_user_mw_matches = AddNamesMatchingPartialString(
1250 m_user_mw_dict, cmd_str, *matches, descriptions);
1251 }
1252
1253 if (num_user_mw_matches == 1) {
1254 cmd.assign(matches->GetStringAtIndex(num_cmd_matches + num_alias_matches +
1255 num_user_matches));
1256
1257 auto pos = m_user_mw_dict.find(cmd);
1258 if (pos != m_user_mw_dict.end())
1259 user_mw_match_sp = pos->second;
1260 }
1261
1262 // If we got exactly one match, return that, otherwise return the match
1263 // list.
1264
1265 if (num_user_matches + num_user_mw_matches + num_cmd_matches +
1266 num_alias_matches ==
1267 1) {
1268 if (num_cmd_matches)
1269 return real_match_sp;
1270 else if (num_alias_matches)
1271 return alias_match_sp;
1272 else if (num_user_mw_matches)
1273 return user_mw_match_sp;
1274 else
1275 return user_match_sp;
1276 }
1277 }
1278
1279 // When no single match is found, attempt to resolve the command as a language
1280 // plugin subcommand.
1281 if (!command_sp) {
1282 // The `language` subcommand ("language objc", "language cplusplus", etc).
1283 CommandObjectMultiword *lang_subcmd = nullptr;
1284 if (auto lang_subcmd_sp = GetFrameLanguageCommand()) {
1285 lang_subcmd = lang_subcmd_sp->GetAsMultiwordCommand();
1286 command_sp = lang_subcmd_sp->GetSubcommandSPExact(cmd_str);
1287 }
1288
1289 if (!command_sp && !exact && lang_subcmd) {
1290 StringList lang_matches;
1292 cmd_str, lang_matches, descriptions);
1293 if (matches)
1294 matches->AppendList(lang_matches);
1295 if (lang_matches.GetSize() == 1) {
1296 const auto &lang_dict = lang_subcmd->GetSubcommandDictionary();
1297 auto pos = lang_dict.find(lang_matches[0]);
1298 if (pos != lang_dict.end())
1299 return pos->second;
1300 }
1301 }
1302 }
1303
1304 if (matches && command_sp) {
1305 matches->AppendString(cmd_str);
1306 if (descriptions)
1307 descriptions->AppendString(command_sp->GetHelp());
1308 }
1309
1310 return command_sp;
1311}
1312
1313bool CommandInterpreter::AddCommand(llvm::StringRef name,
1314 const lldb::CommandObjectSP &cmd_sp,
1315 bool can_replace) {
1316 if (cmd_sp.get())
1317 lldbassert((this == &cmd_sp->GetCommandInterpreter()) &&
1318 "tried to add a CommandObject from a different interpreter");
1319
1320 if (name.empty())
1321 return false;
1322
1323 cmd_sp->SetIsUserCommand(false);
1324
1325 std::string name_sstr(name);
1326 auto name_iter = m_command_dict.find(name_sstr);
1327 if (name_iter != m_command_dict.end()) {
1328 if (!can_replace || !name_iter->second->IsRemovable())
1329 return false;
1330 name_iter->second = cmd_sp;
1331 } else {
1332 m_command_dict[name_sstr] = cmd_sp;
1333 }
1334 return true;
1335}
1336
1338 const lldb::CommandObjectSP &cmd_sp,
1339 bool can_replace) {
1340 Status result;
1341 if (cmd_sp.get())
1342 lldbassert((this == &cmd_sp->GetCommandInterpreter()) &&
1343 "tried to add a CommandObject from a different interpreter");
1344 if (name.empty()) {
1345 result = Status::FromErrorString(
1346 "can't use the empty string for a command name");
1347 return result;
1348 }
1349 // do not allow replacement of internal commands
1350 if (CommandExists(name)) {
1351 result = Status::FromErrorString("can't replace builtin command");
1352 return result;
1353 }
1354
1355 if (UserCommandExists(name)) {
1356 if (!can_replace) {
1358 "user command \"{0}\" already exists and force replace was not set "
1359 "by --overwrite or 'settings set interpreter.require-overwrite "
1360 "false'",
1361 name);
1362 return result;
1363 }
1364 if (cmd_sp->IsMultiwordObject()) {
1365 if (!m_user_mw_dict[std::string(name)]->IsRemovable()) {
1366 result = Status::FromErrorString(
1367 "can't replace explicitly non-removable multi-word command");
1368 return result;
1369 }
1370 } else {
1371 if (!m_user_dict[std::string(name)]->IsRemovable()) {
1372 result = Status::FromErrorString(
1373 "can't replace explicitly non-removable command");
1374 return result;
1375 }
1376 }
1377 }
1378
1379 cmd_sp->SetIsUserCommand(true);
1380
1381 if (cmd_sp->IsMultiwordObject())
1382 m_user_mw_dict[std::string(name)] = cmd_sp;
1383 else
1384 m_user_dict[std::string(name)] = cmd_sp;
1385 return result;
1386}
1387
1390 bool include_aliases) const {
1391 // Break up the command string into words, in case it's a multi-word command.
1392 Args cmd_words(cmd_str);
1393
1394 if (cmd_str.empty())
1395 return {};
1396
1397 if (cmd_words.GetArgumentCount() == 1)
1398 return GetCommandSP(cmd_str, include_aliases, true);
1399
1400 // We have a multi-word command (seemingly), so we need to do more work.
1401 // First, get the cmd_obj_sp for the first word in the command.
1402 CommandObjectSP cmd_obj_sp =
1403 GetCommandSP(cmd_words.GetArgumentAtIndex(0), include_aliases, true);
1404 if (!cmd_obj_sp)
1405 return {};
1406
1407 // Loop through the rest of the words in the command (everything passed in
1408 // was supposed to be part of a command name), and find the appropriate
1409 // sub-command SP for each command word....
1410 size_t end = cmd_words.GetArgumentCount();
1411 for (size_t i = 1; i < end; ++i) {
1412 if (!cmd_obj_sp->IsMultiwordObject()) {
1413 // We have more words in the command name, but we don't have a
1414 // multiword object. Fail and return.
1415 return {};
1416 }
1417
1418 cmd_obj_sp = cmd_obj_sp->GetSubcommandSP(cmd_words.GetArgumentAtIndex(i));
1419 if (!cmd_obj_sp) {
1420 // The sub-command name was invalid. Fail and return.
1421 return {};
1422 }
1423 }
1424
1425 // We successfully looped through all the command words and got valid
1426 // command objects for them.
1427 return cmd_obj_sp;
1428}
1429
1432 StringList *matches,
1433 StringList *descriptions) const {
1434 // Try to find a match among commands and aliases. Allowing inexact matches,
1435 // but perferring exact matches.
1436 return GetCommandSP(cmd_str, /*include_aliases=*/true, /*exact=*/false,
1437 matches, descriptions)
1438 .get();
1439}
1440
1442 llvm::StringRef cmd, StringList *matches, StringList *descriptions) const {
1443 std::string cmd_str(cmd);
1444 auto find_exact = [&](const CommandObject::CommandMap &map) {
1445 auto found_elem = map.find(cmd);
1446 if (found_elem == map.end())
1447 return (CommandObject *)nullptr;
1448 CommandObject *exact_cmd = found_elem->second.get();
1449 if (exact_cmd) {
1450 if (matches)
1451 matches->AppendString(exact_cmd->GetCommandName());
1452 if (descriptions)
1453 descriptions->AppendString(exact_cmd->GetHelp());
1454 return exact_cmd;
1455 }
1456 return (CommandObject *)nullptr;
1457 };
1458
1459 CommandObject *exact_cmd = find_exact(GetUserCommands());
1460 if (exact_cmd)
1461 return exact_cmd;
1462
1463 exact_cmd = find_exact(GetUserMultiwordCommands());
1464 if (exact_cmd)
1465 return exact_cmd;
1466
1467 // We didn't have an exact command, so now look for partial matches.
1468 StringList tmp_list;
1469 StringList *matches_ptr = matches ? matches : &tmp_list;
1470 AddNamesMatchingPartialString(GetUserCommands(), cmd_str, *matches_ptr);
1472 *matches_ptr);
1473
1474 return {};
1475}
1476
1478 llvm::StringRef cmd, StringList *matches, StringList *descriptions) const {
1479 auto find_exact =
1480 [&](const CommandObject::CommandMap &map) -> CommandObject * {
1481 auto found_elem = map.find(cmd);
1482 if (found_elem == map.end())
1483 return (CommandObject *)nullptr;
1484 CommandObject *exact_cmd = found_elem->second.get();
1485 if (!exact_cmd)
1486 return nullptr;
1487
1488 if (matches)
1489 matches->AppendString(exact_cmd->GetCommandName());
1490
1491 if (descriptions)
1492 descriptions->AppendString(exact_cmd->GetHelp());
1493
1494 return exact_cmd;
1495 return nullptr;
1496 };
1497
1498 CommandObject *exact_cmd = find_exact(GetAliases());
1499 if (exact_cmd)
1500 return exact_cmd;
1501
1502 // We didn't have an exact command, so now look for partial matches.
1503 StringList tmp_list;
1504 StringList *matches_ptr = matches ? matches : &tmp_list;
1505 AddNamesMatchingPartialString(GetAliases(), cmd, *matches_ptr);
1506
1507 return {};
1508}
1509
1510bool CommandInterpreter::CommandExists(llvm::StringRef cmd) const {
1511 return m_command_dict.find(cmd) != m_command_dict.end();
1512}
1513
1515 std::string &full_name) const {
1516 bool exact_match = (m_alias_dict.find(cmd) != m_alias_dict.end());
1517 if (exact_match) {
1518 full_name.assign(std::string(cmd));
1519 return exact_match;
1520 } else {
1521 StringList matches;
1522 size_t num_alias_matches;
1523 num_alias_matches =
1525 if (num_alias_matches == 1) {
1526 // Make sure this isn't shadowing a command in the regular command space:
1527 StringList regular_matches;
1528 const bool include_aliases = false;
1529 const bool exact = false;
1530 CommandObjectSP cmd_obj_sp(
1531 GetCommandSP(cmd, include_aliases, exact, &regular_matches));
1532 if (cmd_obj_sp || regular_matches.GetSize() > 0)
1533 return false;
1534 else {
1535 full_name.assign(matches.GetStringAtIndex(0));
1536 return true;
1537 }
1538 } else
1539 return false;
1540 }
1541}
1542
1543bool CommandInterpreter::AliasExists(llvm::StringRef cmd) const {
1544 return m_alias_dict.find(cmd) != m_alias_dict.end();
1545}
1546
1547bool CommandInterpreter::UserCommandExists(llvm::StringRef cmd) const {
1548 return llvm::is_contained(m_user_dict, cmd) ||
1549 llvm::is_contained(m_user_mw_dict, cmd);
1550}
1551
1552bool CommandInterpreter::UserMultiwordCommandExists(llvm::StringRef cmd) const {
1553 return m_user_mw_dict.find(cmd) != m_user_mw_dict.end();
1554}
1555
1557CommandInterpreter::AddAlias(llvm::StringRef alias_name,
1558 lldb::CommandObjectSP &command_obj_sp,
1559 llvm::StringRef args_string) {
1560 if (command_obj_sp.get())
1561 lldbassert((this == &command_obj_sp->GetCommandInterpreter()) &&
1562 "tried to add a CommandObject from a different interpreter");
1563
1564 std::unique_ptr<CommandAlias> command_alias_up(
1565 new CommandAlias(*this, command_obj_sp, args_string, alias_name));
1566
1567 if (command_alias_up && command_alias_up->IsValid()) {
1568 m_alias_dict[std::string(alias_name)] =
1569 CommandObjectSP(command_alias_up.get());
1570 return command_alias_up.release();
1571 }
1572
1573 return nullptr;
1574}
1575
1576bool CommandInterpreter::RemoveAlias(llvm::StringRef alias_name) {
1577 auto pos = m_alias_dict.find(alias_name);
1578 if (pos != m_alias_dict.end()) {
1579 m_alias_dict.erase(pos);
1580 return true;
1581 }
1582 return false;
1583}
1584
1585bool CommandInterpreter::RemoveCommand(llvm::StringRef cmd, bool force) {
1586 auto pos = m_command_dict.find(cmd);
1587 if (pos != m_command_dict.end()) {
1588 if (force || pos->second->IsRemovable()) {
1589 // Only regular expression objects or python commands are removable under
1590 // normal circumstances.
1591 m_command_dict.erase(pos);
1592 return true;
1593 }
1594 }
1595 return false;
1596}
1597
1598bool CommandInterpreter::RemoveUser(llvm::StringRef user_name) {
1599 CommandObject::CommandMap::iterator pos = m_user_dict.find(user_name);
1600 if (pos != m_user_dict.end()) {
1601 m_user_dict.erase(pos);
1602 return true;
1603 }
1604 return false;
1605}
1606
1607bool CommandInterpreter::RemoveUserMultiword(llvm::StringRef multi_name) {
1608 CommandObject::CommandMap::iterator pos = m_user_mw_dict.find(multi_name);
1609 if (pos != m_user_mw_dict.end()) {
1610 m_user_mw_dict.erase(pos);
1611 return true;
1612 }
1613 return false;
1614}
1615
1617 uint32_t cmd_types) {
1618 llvm::StringRef help_prologue(GetDebugger().GetIOHandlerHelpPrologue());
1619 if (!help_prologue.empty()) {
1620 OutputFormattedHelpText(result.GetOutputStream(), llvm::StringRef(),
1621 help_prologue);
1622 }
1623
1624 CommandObject::CommandMap::const_iterator pos;
1625 size_t max_len = FindLongestCommandWord(m_command_dict);
1626
1627 if ((cmd_types & eCommandTypesBuiltin) == eCommandTypesBuiltin) {
1628 result.AppendMessage("Debugger commands:");
1629 result.AppendMessage("");
1630
1631 for (pos = m_command_dict.begin(); pos != m_command_dict.end(); ++pos) {
1632 if (!(cmd_types & eCommandTypesHidden) &&
1633 (pos->first.compare(0, 1, "_") == 0))
1634 continue;
1635
1636 OutputFormattedHelpText(result.GetOutputStream(), pos->first, "--",
1637 pos->second->GetHelp(), max_len);
1638 }
1639 result.AppendMessage("");
1640 }
1641
1642 if (!m_alias_dict.empty() &&
1643 ((cmd_types & eCommandTypesAliases) == eCommandTypesAliases)) {
1645 "Current command abbreviations "
1646 "(type '{0}help command alias' for more info):",
1648 result.AppendMessage("");
1650
1651 for (auto alias_pos = m_alias_dict.begin(); alias_pos != m_alias_dict.end();
1652 ++alias_pos) {
1653 OutputFormattedHelpText(result.GetOutputStream(), alias_pos->first, "--",
1654 alias_pos->second->GetHelp(), max_len);
1655 }
1656 result.AppendMessage("");
1657 }
1658
1659 if (!m_user_dict.empty() &&
1660 ((cmd_types & eCommandTypesUserDef) == eCommandTypesUserDef)) {
1661 result.AppendMessage("Current user-defined commands:");
1662 result.AppendMessage("");
1664 for (pos = m_user_dict.begin(); pos != m_user_dict.end(); ++pos) {
1665 OutputFormattedHelpText(result.GetOutputStream(), pos->first, "--",
1666 pos->second->GetHelp(), max_len);
1667 }
1668 result.AppendMessage("");
1669 }
1670
1671 if (!m_user_mw_dict.empty() &&
1672 ((cmd_types & eCommandTypesUserMW) == eCommandTypesUserMW)) {
1673 result.AppendMessage("Current user-defined container commands:");
1674 result.AppendMessage("");
1676 for (pos = m_user_mw_dict.begin(); pos != m_user_mw_dict.end(); ++pos) {
1677 OutputFormattedHelpText(result.GetOutputStream(), pos->first, "--",
1678 pos->second->GetHelp(), max_len);
1679 }
1680 result.AppendMessage("");
1681 }
1682
1684 "For more information on any command, type '{0}help <command-name>'.",
1686}
1687
1689 llvm::StringRef &command_string) {
1690 // This function finds the final, lowest-level, alias-resolved command object
1691 // whose 'Execute' function will eventually be invoked by the given command
1692 // line.
1693
1694 CommandObject *cmd_obj = nullptr;
1695 size_t start = command_string.find_first_not_of(k_white_space);
1696 size_t end = 0;
1697 bool done = false;
1698 while (!done) {
1699 if (start != std::string::npos) {
1700 // Get the next word from command_string.
1701 end = command_string.find_first_of(k_white_space, start);
1702 if (end == std::string::npos)
1703 end = command_string.size();
1704 std::string cmd_word =
1705 std::string(command_string.substr(start, end - start));
1706
1707 if (cmd_obj == nullptr)
1708 // Since cmd_obj is NULL we are on our first time through this loop.
1709 // Check to see if cmd_word is a valid command or alias.
1710 cmd_obj = GetCommandObject(cmd_word);
1711 else if (cmd_obj->IsMultiwordObject()) {
1712 // Our current object is a multi-word object; see if the cmd_word is a
1713 // valid sub-command for our object.
1714 CommandObject *sub_cmd_obj =
1715 cmd_obj->GetSubcommandObject(cmd_word.c_str());
1716 if (sub_cmd_obj)
1717 cmd_obj = sub_cmd_obj;
1718 else // cmd_word was not a valid sub-command word, so we are done
1719 done = true;
1720 } else
1721 // We have a cmd_obj and it is not a multi-word object, so we are done.
1722 done = true;
1723
1724 // If we didn't find a valid command object, or our command object is not
1725 // a multi-word object, or we are at the end of the command_string, then
1726 // we are done. Otherwise, find the start of the next word.
1727
1728 if (!cmd_obj || !cmd_obj->IsMultiwordObject() ||
1729 end >= command_string.size())
1730 done = true;
1731 else
1732 start = command_string.find_first_not_of(k_white_space, end);
1733 } else
1734 // Unable to find any more words.
1735 done = true;
1736 }
1737
1738 command_string = command_string.substr(end);
1739 return cmd_obj;
1740}
1741
1742static const char *k_valid_command_chars =
1743 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_";
1744static void StripLeadingSpaces(std::string &s) {
1745 if (!s.empty()) {
1746 size_t pos = s.find_first_not_of(k_white_space);
1747 if (pos == std::string::npos)
1748 s.clear();
1749 else if (pos == 0)
1750 return;
1751 s.erase(0, pos);
1752 }
1753}
1754
1755static size_t FindArgumentTerminator(const std::string &s) {
1756 const size_t s_len = s.size();
1757 size_t offset = 0;
1758 while (offset < s_len) {
1759 size_t pos = s.find("--", offset);
1760 if (pos == std::string::npos)
1761 break;
1762 if (pos > 0) {
1763 if (llvm::isSpace(s[pos - 1])) {
1764 // Check if the string ends "\s--" (where \s is a space character) or
1765 // if we have "\s--\s".
1766 if ((pos + 2 >= s_len) || llvm::isSpace(s[pos + 2])) {
1767 return pos;
1768 }
1769 }
1770 }
1771 offset = pos + 2;
1772 }
1773 return std::string::npos;
1774}
1775
1776static bool ExtractCommand(std::string &command_string, std::string &command,
1777 std::string &suffix, char &quote_char) {
1778 command.clear();
1779 suffix.clear();
1780 StripLeadingSpaces(command_string);
1781
1782 bool result = false;
1783 quote_char = '\0';
1784
1785 if (!command_string.empty()) {
1786 const char first_char = command_string[0];
1787 if (first_char == '\'' || first_char == '"') {
1788 quote_char = first_char;
1789 const size_t end_quote_pos = command_string.find(quote_char, 1);
1790 if (end_quote_pos == std::string::npos) {
1791 command.swap(command_string);
1792 command_string.erase();
1793 } else {
1794 command.assign(command_string, 1, end_quote_pos - 1);
1795 if (end_quote_pos + 1 < command_string.size())
1796 command_string.erase(0, command_string.find_first_not_of(
1797 k_white_space, end_quote_pos + 1));
1798 else
1799 command_string.erase();
1800 }
1801 } else {
1802 const size_t first_space_pos =
1803 command_string.find_first_of(k_white_space);
1804 if (first_space_pos == std::string::npos) {
1805 command.swap(command_string);
1806 command_string.erase();
1807 } else {
1808 command.assign(command_string, 0, first_space_pos);
1809 command_string.erase(0, command_string.find_first_not_of(
1810 k_white_space, first_space_pos));
1811 }
1812 }
1813 result = true;
1814 }
1815
1816 if (!command.empty()) {
1817 // actual commands can't start with '-' or '_'
1818 if (command[0] != '-' && command[0] != '_') {
1819 size_t pos = command.find_first_not_of(k_valid_command_chars);
1820 if (pos > 0 && pos != std::string::npos) {
1821 suffix.assign(command.begin() + pos, command.end());
1822 command.erase(pos);
1823 }
1824 }
1825 }
1826
1827 return result;
1828}
1829
1831 llvm::StringRef alias_name, std::string &raw_input_string,
1832 std::string &alias_result, CommandReturnObject &result) {
1833 CommandObject *alias_cmd_obj = nullptr;
1834 Args cmd_args(raw_input_string);
1835 alias_cmd_obj = GetCommandObject(alias_name);
1836 StreamString result_str;
1837
1838 if (!alias_cmd_obj || !alias_cmd_obj->IsAlias()) {
1839 alias_result.clear();
1840 return alias_cmd_obj;
1841 }
1842 std::pair<CommandObjectSP, OptionArgVectorSP> desugared =
1843 ((CommandAlias *)alias_cmd_obj)->Desugar();
1844 OptionArgVectorSP option_arg_vector_sp = desugared.second;
1845 alias_cmd_obj = desugared.first.get();
1846 std::string alias_name_str = std::string(alias_name);
1847 if ((cmd_args.GetArgumentCount() == 0) ||
1848 (alias_name_str != cmd_args.GetArgumentAtIndex(0)))
1849 cmd_args.Unshift(alias_name_str);
1850
1851 result_str.Printf("%s", alias_cmd_obj->GetCommandName().str().c_str());
1852
1853 if (!option_arg_vector_sp.get()) {
1854 alias_result = std::string(result_str.GetString());
1855 return alias_cmd_obj;
1856 }
1857 OptionArgVector *option_arg_vector = option_arg_vector_sp.get();
1858
1859 int value_type;
1860 std::string option;
1861 std::string value;
1862 for (const auto &entry : *option_arg_vector) {
1863 std::tie(option, value_type, value) = entry;
1864 if (option == g_argument) {
1865 result_str.Printf(" %s", value.c_str());
1866 continue;
1867 }
1868
1869 result_str.Printf(" %s", option.c_str());
1870 if (value_type == OptionParser::eNoArgument)
1871 continue;
1872
1873 if (value_type != OptionParser::eOptionalArgument)
1874 result_str.Printf(" ");
1875 int index = GetOptionArgumentPosition(value.c_str());
1876 if (index == 0)
1877 result_str.Printf("%s", value.c_str());
1878 else if (static_cast<size_t>(index) >= cmd_args.GetArgumentCount()) {
1879
1880 result.AppendErrorWithFormat("Not enough arguments provided; you "
1881 "need at least %d arguments to use "
1882 "this alias.\n",
1883 index);
1884 return nullptr;
1885 } else {
1886 const Args::ArgEntry &entry = cmd_args[index];
1887 size_t strpos = raw_input_string.find(entry.c_str());
1888 const char quote_char = entry.GetQuoteChar();
1889 if (strpos != std::string::npos) {
1890 const size_t start_fudge = quote_char == '\0' ? 0 : 1;
1891 const size_t len_fudge = quote_char == '\0' ? 0 : 2;
1892
1893 // Make sure we aren't going outside the bounds of the cmd string:
1894 if (strpos < start_fudge) {
1895 result.AppendError("unmatched quote at command beginning");
1896 return nullptr;
1897 }
1898 llvm::StringRef arg_text = entry.ref();
1899 if (strpos - start_fudge + arg_text.size() + len_fudge >
1900 raw_input_string.size()) {
1901 result.AppendError("unmatched quote at command end");
1902 return nullptr;
1903 }
1904 raw_input_string = raw_input_string.erase(
1905 strpos - start_fudge,
1906 strlen(cmd_args.GetArgumentAtIndex(index)) + len_fudge);
1907 }
1908 if (quote_char == '\0')
1909 result_str.Printf("%s", cmd_args.GetArgumentAtIndex(index));
1910 else
1911 result_str.Printf("%c%s%c", quote_char, entry.c_str(), quote_char);
1912 }
1913 }
1914
1915 alias_result = std::string(result_str.GetString());
1916 return alias_cmd_obj;
1917}
1918
1920 // The command preprocessor needs to do things to the command line before any
1921 // parsing of arguments or anything else is done. The only current stuff that
1922 // gets preprocessed is anything enclosed in backtick ('`') characters is
1923 // evaluated as an expression and the result of the expression must be a
1924 // scalar that can be substituted into the command. An example would be:
1925 // (lldb) memory read `$rsp + 20`
1926 Status error; // Status for any expressions that might not evaluate
1927 size_t start_backtick;
1928 size_t pos = 0;
1929 while ((start_backtick = command.find('`', pos)) != std::string::npos) {
1930 // Stop if an error was encountered during the previous iteration.
1931 if (error.Fail())
1932 break;
1933
1934 if (start_backtick > 0 && command[start_backtick - 1] == '\\') {
1935 // The backtick was preceded by a '\' character, remove the slash and
1936 // don't treat the backtick as the start of an expression.
1937 command.erase(start_backtick - 1, 1);
1938 // No need to add one to start_backtick since we just deleted a char.
1939 pos = start_backtick;
1940 continue;
1941 }
1942
1943 const size_t expr_content_start = start_backtick + 1;
1944 const size_t end_backtick = command.find('`', expr_content_start);
1945
1946 if (end_backtick == std::string::npos) {
1947 // Stop if there's no end backtick.
1948 break;
1949 }
1950
1951 if (end_backtick == expr_content_start) {
1952 // Skip over empty expression. (two backticks in a row)
1953 command.erase(start_backtick, 2);
1954 continue;
1955 }
1956
1957 std::string expr_str(command, expr_content_start,
1958 end_backtick - expr_content_start);
1959 error = PreprocessToken(expr_str);
1960 // We always stop at the first error:
1961 if (error.Fail())
1962 break;
1963
1964 command.erase(start_backtick, end_backtick - start_backtick + 1);
1965 command.insert(start_backtick, std::string(expr_str));
1966 pos = start_backtick + expr_str.size();
1967 }
1968 return error;
1969}
1970
1972 Status error;
1974
1975 // Get a dummy target to allow for calculator mode while processing
1976 // backticks. This also helps break the infinite loop caused when target is
1977 // null.
1978 Target *exe_target = exe_ctx.GetTargetPtr();
1979 Target &target = exe_target ? *exe_target : m_debugger.GetDummyTarget();
1980
1981 ValueObjectSP expr_result_valobj_sp;
1982
1984 options.SetCoerceToId(false);
1985 options.SetUnwindOnError(true);
1986 options.SetIgnoreBreakpoints(true);
1987 options.SetKeepInMemory(false);
1988 options.SetTryAllThreads(true);
1989 options.SetTimeout(std::nullopt);
1990
1991 ExpressionResults expr_result = target.EvaluateExpression(
1992 expr_str.c_str(), exe_ctx.GetFramePtr(), expr_result_valobj_sp, options);
1993
1994 if (expr_result == eExpressionCompleted) {
1995 Scalar scalar;
1996 if (expr_result_valobj_sp)
1997 expr_result_valobj_sp =
1998 expr_result_valobj_sp->GetQualifiedRepresentationIfAvailable(
1999 expr_result_valobj_sp->GetDynamicValueType(), true);
2000 if (expr_result_valobj_sp->ResolveValue(scalar)) {
2001
2002 StreamString value_strm;
2003 const bool show_type = false;
2004 scalar.GetValue(value_strm, show_type);
2005 size_t value_string_size = value_strm.GetSize();
2006 if (value_string_size) {
2007 expr_str = value_strm.GetData();
2008 } else {
2009 error =
2010 Status::FromErrorStringWithFormat("expression value didn't result "
2011 "in a scalar value for the "
2012 "expression '%s'",
2013 expr_str.c_str());
2014 }
2015 } else {
2016 error =
2017 Status::FromErrorStringWithFormat("expression value didn't result "
2018 "in a scalar value for the "
2019 "expression '%s'",
2020 expr_str.c_str());
2021 }
2022 return error;
2023 }
2024
2025 // If we have an error from the expression evaluation it will be in the
2026 // ValueObject error, which won't be success and we will just report it.
2027 // But if for some reason we didn't get a value object at all, then we will
2028 // make up some helpful errors from the expression result.
2029 if (expr_result_valobj_sp)
2030 error = expr_result_valobj_sp->GetError().Clone();
2031
2032 if (error.Success()) {
2033 std::string result = lldb_private::toString(expr_result) +
2034 "for the expression '" + expr_str + "'";
2035 error = Status(result);
2036 }
2037 return error;
2038}
2039
2040bool CommandInterpreter::HandleCommand(const char *command_line,
2041 LazyBool lazy_add_to_history,
2042 const ExecutionContext &override_context,
2043 CommandReturnObject &result) {
2044
2045 OverrideExecutionContext(override_context);
2046 bool status = HandleCommand(command_line, lazy_add_to_history, result);
2048 return status;
2049}
2050
2051bool CommandInterpreter::HandleCommand(const char *command_line,
2052 LazyBool lazy_add_to_history,
2053 CommandReturnObject &result,
2054 bool force_repeat_command) {
2055 // These are assigned later in the function but they must be declared before
2056 // the ScopedDispatcher object because we need their destructions to occur
2057 // after the dispatcher's dtor call, which may reference them.
2058 // TODO: This function could be refactored?
2059 std::string parsed_command_args;
2060 CommandObject *cmd_obj = nullptr;
2061
2063 const bool detailed_command_telemetry =
2065 ->GetConfig()
2067 const int command_id = telemetry::CommandInfo::GetNextID();
2068
2069 std::string command_string(command_line);
2070 std::string original_command_string(command_string);
2071 std::string real_original_command_string(command_string);
2072
2074 info->command_id = command_id;
2075 if (Target *target = GetExecutionContext().GetTargetPtr()) {
2076 // If we have a target attached to this command, then get the UUID.
2077 info->target_uuid = target->GetExecutableModule() != nullptr
2078 ? target->GetExecutableModule()->GetUUID()
2079 : UUID();
2080 }
2081 if (detailed_command_telemetry)
2082 info->original_command = original_command_string;
2083 // The rest (eg., command_name, args, etc) hasn't been parsed yet;
2084 // Those will be collected by the on-exit-callback.
2085 });
2086
2087 helper.DispatchOnExit([&cmd_obj, &parsed_command_args, &result,
2088 detailed_command_telemetry, command_id](
2090 // TODO: this is logging the time the command-handler finishes.
2091 // But we may want a finer-grain durations too?
2092 // (ie., the execute_time recorded below?)
2093 info->command_id = command_id;
2094 llvm::StringRef command_name =
2095 cmd_obj ? cmd_obj->GetCommandName() : "<not found>";
2096 info->command_name = command_name.str();
2097 info->ret_status = result.GetStatus();
2098 if (std::string error_str = result.GetErrorString(); !error_str.empty())
2099 info->error_data = std::move(error_str);
2100
2101 if (detailed_command_telemetry)
2102 info->args = parsed_command_args;
2103 });
2104
2106 LLDB_LOGF(log, "Processing command: %s", command_line);
2107 LLDB_SCOPED_TIMERF("Processing command: %s.", command_line);
2108
2109 // Set the command in the CommandReturnObject here so that it's there even if
2110 // the command is interrupted.
2111 result.SetCommand(command_line);
2112
2113 if (INTERRUPT_REQUESTED(GetDebugger(), "Interrupted initiating command")) {
2114 result.AppendError("... Interrupted");
2115 return false;
2116 }
2117
2118 bool add_to_history;
2119 if (lazy_add_to_history == eLazyBoolCalculate)
2120 add_to_history = (m_command_source_depth == 0);
2121 else
2122 add_to_history = (lazy_add_to_history == eLazyBoolYes);
2123
2124 // The same `transcript_item` will be used below to add output and error of
2125 // the command.
2126 StructuredData::DictionarySP transcript_item;
2127 if (GetSaveTranscript()) {
2128 m_transcript_stream << "(lldb) " << command_line << '\n';
2129
2130 transcript_item = std::make_shared<StructuredData::Dictionary>();
2131 transcript_item->AddStringItem("command", command_line);
2132 transcript_item->AddIntegerItem(
2133 "timestampInEpochSeconds",
2134 std::chrono::duration_cast<std::chrono::seconds>(
2135 std::chrono::system_clock::now().time_since_epoch())
2136 .count());
2137 m_transcript.AddItem(transcript_item);
2138 }
2139
2140 bool empty_command = false;
2141 bool comment_command = false;
2142 if (command_string.empty())
2143 empty_command = true;
2144 else {
2145 const char *k_space_characters = "\t\n\v\f\r ";
2146
2147 size_t non_space = command_string.find_first_not_of(k_space_characters);
2148 // Check for empty line or comment line (lines whose first non-space
2149 // character is the comment character for this interpreter)
2150 if (non_space == std::string::npos)
2151 empty_command = true;
2152 else if (command_string[non_space] == m_comment_char)
2153 comment_command = true;
2154 else if (command_string[non_space] == CommandHistory::g_repeat_char) {
2155 llvm::StringRef search_str(command_string);
2156 search_str = search_str.drop_front(non_space);
2157 if (auto hist_str = m_command_history.FindString(search_str)) {
2158 add_to_history = false;
2159 command_string = std::string(*hist_str);
2160 original_command_string = std::string(*hist_str);
2161 } else {
2162 result.AppendErrorWithFormat("Could not find entry: %s in history",
2163 command_string.c_str());
2164 return false;
2165 }
2166 }
2167 }
2168
2169 if (empty_command) {
2170 if (!GetRepeatPreviousCommand()) {
2172 return true;
2173 }
2174
2175 if (m_command_history.IsEmpty()) {
2176 result.AppendError("empty command");
2177 return false;
2178 }
2179
2180 command_line = m_repeat_command.c_str();
2181 command_string = command_line;
2182 original_command_string = command_line;
2183 if (m_repeat_command.empty()) {
2184 result.AppendError("no auto repeat");
2185 return false;
2186 }
2187
2188 add_to_history = false;
2189 } else if (comment_command) {
2191 return true;
2192 }
2193
2194 // Phase 1.
2195
2196 // Before we do ANY kind of argument processing, we need to figure out what
2197 // the real/final command object is for the specified command. This gets
2198 // complicated by the fact that the user could have specified an alias, and,
2199 // in translating the alias, there may also be command options and/or even
2200 // data (including raw text strings) that need to be found and inserted into
2201 // the command line as part of the translation. So this first step is plain
2202 // look-up and replacement, resulting in:
2203 // 1. the command object whose Execute method will actually be called
2204 // 2. a revised command string, with all substitutions and replacements
2205 // taken care of
2206 // From 1 above, we can determine whether the Execute function wants raw
2207 // input or not.
2208
2209 cmd_obj = ResolveCommandImpl(command_string, result);
2210
2211 // We have to preprocess the whole command string for Raw commands, since we
2212 // don't know the structure of the command. For parsed commands, we only
2213 // treat backticks as quote characters specially.
2214 // FIXME: We probably want to have raw commands do their own preprocessing.
2215 // For instance, I don't think people expect substitution in expr expressions.
2216 if (cmd_obj && cmd_obj->WantsRawCommandString()) {
2217 Status error(PreprocessCommand(command_string));
2218
2219 if (error.Fail()) {
2220 result.AppendError(error.AsCString());
2221 return false;
2222 }
2223 }
2224
2225 // Although the user may have abbreviated the command, the command_string now
2226 // has the command expanded to the full name. For example, if the input was
2227 // "br s -n main", command_string is now "breakpoint set -n main".
2228 if (log) {
2229 llvm::StringRef command_name =
2230 cmd_obj ? cmd_obj->GetCommandName() : "<not found>";
2231 LLDB_LOGF(log, "HandleCommand, cmd_obj : '%s'", command_name.str().c_str());
2232 LLDB_LOGF(log, "HandleCommand, (revised) command_string: '%s'",
2233 command_string.c_str());
2234 const bool wants_raw_input =
2235 (cmd_obj != nullptr) ? cmd_obj->WantsRawCommandString() : false;
2236 LLDB_LOGF(log, "HandleCommand, wants_raw_input:'%s'",
2237 wants_raw_input ? "True" : "False");
2238 }
2239
2240 // Phase 2.
2241 // Take care of things like setting up the history command & calling the
2242 // appropriate Execute method on the CommandObject, with the appropriate
2243 // arguments.
2244 StatsDuration execute_time;
2245 if (cmd_obj != nullptr) {
2246 bool generate_repeat_command = add_to_history;
2247 // If we got here when empty_command was true, then this command is a
2248 // stored "repeat command" which we should give a chance to produce it's
2249 // repeat command, even though we don't add repeat commands to the history.
2250 generate_repeat_command |= empty_command;
2251 // For `command regex`, the regex command (ex `bt`) is added to history, but
2252 // the resolved command (ex `thread backtrace`) is _not_ added to history.
2253 // However, the resolved command must be given the opportunity to provide a
2254 // repeat command. `force_repeat_command` supports this case.
2255 generate_repeat_command |= force_repeat_command;
2256 if (generate_repeat_command) {
2257 Args command_args(command_string);
2258 std::optional<std::string> repeat_command =
2259 cmd_obj->GetRepeatCommand(command_args, 0);
2260 if (repeat_command) {
2261 LLDB_LOGF(log, "Repeat command: %s", repeat_command->data());
2262 m_repeat_command.assign(*repeat_command);
2263 } else {
2264 m_repeat_command.assign(original_command_string);
2265 }
2266 }
2267
2268 if (add_to_history)
2269 m_command_history.AppendString(original_command_string);
2270
2271 const std::size_t actual_cmd_name_len = cmd_obj->GetCommandName().size();
2272 if (actual_cmd_name_len < command_string.length())
2273 parsed_command_args = command_string.substr(actual_cmd_name_len);
2274
2275 // Remove any initial spaces
2276 size_t pos = parsed_command_args.find_first_not_of(k_white_space);
2277 if (pos != 0 && pos != std::string::npos)
2278 parsed_command_args.erase(0, pos);
2279
2280 LLDB_LOGF(
2281 log, "HandleCommand, command line after removing command name(s): '%s'",
2282 parsed_command_args.c_str());
2283
2284 // To test whether or not transcript should be saved, `transcript_item` is
2285 // used instead of `GetSaveTranscript()`. This is because the latter will
2286 // fail when the command is "settings set interpreter.save-transcript true".
2287 if (transcript_item) {
2288 transcript_item->AddStringItem("commandName", cmd_obj->GetCommandName());
2289 transcript_item->AddStringItem("commandArguments", parsed_command_args);
2290 }
2291
2292 ElapsedTime elapsed(execute_time);
2293 cmd_obj->SetOriginalCommandString(real_original_command_string);
2294 // Set the indent to the position of the command in the command line.
2295 pos = real_original_command_string.rfind(parsed_command_args);
2296 std::optional<uint16_t> indent;
2297 if (pos != std::string::npos)
2298 indent = pos;
2299 result.SetDiagnosticIndent(indent);
2300 cmd_obj->Execute(parsed_command_args.c_str(), result);
2301 }
2302
2303 LLDB_LOGF(log, "HandleCommand, command %s",
2304 (result.Succeeded() ? "succeeded" : "did not succeed"));
2305
2306 // To test whether or not transcript should be saved, `transcript_item` is
2307 // used instead of `GetSaveTrasncript()`. This is because the latter will
2308 // fail when the command is "settings set interpreter.save-transcript true".
2309 if (transcript_item) {
2312
2313 transcript_item->AddStringItem("output", result.GetOutputString());
2314 transcript_item->AddStringItem("error", result.GetErrorString());
2315 transcript_item->AddFloatItem("durationInSeconds",
2316 execute_time.get().count());
2317 }
2318
2319 return result.Succeeded();
2320}
2321
2323 bool look_for_subcommand = false;
2324
2325 // For any of the command completions a unique match will be a complete word.
2326
2327 if (request.GetParsedLine().GetArgumentCount() == 0) {
2328 // We got nothing on the command line, so return the list of commands
2329 bool include_aliases = true;
2330 StringList new_matches, descriptions;
2331 GetCommandNamesMatchingPartialString("", include_aliases, new_matches,
2332 descriptions);
2333 request.AddCompletions(new_matches, descriptions);
2334 } else if (request.GetCursorIndex() == 0) {
2335 // The cursor is in the first argument, so just do a lookup in the
2336 // dictionary.
2337 StringList new_matches, new_descriptions;
2338 CommandObject *cmd_obj =
2340 &new_matches, &new_descriptions);
2341
2342 if (new_matches.GetSize() && cmd_obj && cmd_obj->IsMultiwordObject() &&
2343 new_matches.GetStringAtIndex(0) != nullptr &&
2344 strcmp(request.GetParsedLine().GetArgumentAtIndex(0),
2345 new_matches.GetStringAtIndex(0)) == 0) {
2346 if (request.GetParsedLine().GetArgumentCount() != 1) {
2347 look_for_subcommand = true;
2348 new_matches.DeleteStringAtIndex(0);
2349 new_descriptions.DeleteStringAtIndex(0);
2350 request.AppendEmptyArgument();
2351 }
2352 }
2353 request.AddCompletions(new_matches, new_descriptions);
2354 }
2355
2356 if (request.GetCursorIndex() > 0 || look_for_subcommand) {
2357 // We are completing further on into a commands arguments, so find the
2358 // command and tell it to complete the command. First see if there is a
2359 // matching initial command:
2360 CommandObject *command_object =
2362 if (command_object) {
2363 request.ShiftArguments();
2364 command_object->HandleCompletion(request);
2365 }
2366 }
2367}
2368
2370
2371 // Don't complete comments, and if the line we are completing is just the
2372 // history repeat character, substitute the appropriate history line.
2373 llvm::StringRef first_arg = request.GetParsedLine().GetArgumentAtIndex(0);
2374
2375 if (!first_arg.empty()) {
2376 if (first_arg.front() == m_comment_char)
2377 return;
2378 if (first_arg.front() == CommandHistory::g_repeat_char) {
2379 if (auto hist_str = m_command_history.FindString(first_arg))
2380 request.AddCompletion(*hist_str, "Previous command history event",
2382 return;
2383 }
2384 }
2385
2386 HandleCompletionMatches(request);
2387}
2388
2389std::optional<std::string>
2391 if (line.empty())
2392 return std::nullopt;
2393 const size_t s = m_command_history.GetSize();
2394 for (int i = s - 1; i >= 0; --i) {
2395 llvm::StringRef entry = m_command_history.GetStringAtIndex(i);
2396 if (entry.consume_front(line))
2397 return entry.str();
2398 }
2399 return std::nullopt;
2400}
2401
2402void CommandInterpreter::UpdatePrompt(llvm::StringRef new_prompt) {
2403 EventSP prompt_change_event_sp(
2404 new Event(eBroadcastBitResetPrompt, new EventDataBytes(new_prompt)));
2405
2406 BroadcastEvent(prompt_change_event_sp);
2408 m_command_io_handler_sp->SetPrompt(new_prompt);
2409}
2410
2413 m_command_io_handler_sp->SetUseColor(use_color);
2414}
2415
2416bool CommandInterpreter::Confirm(llvm::StringRef message, bool default_answer) {
2417 // Check AutoConfirm first:
2418 if (m_debugger.GetAutoConfirm())
2419 return default_answer;
2420
2421 IOHandlerConfirm *confirm =
2422 new IOHandlerConfirm(m_debugger, message, default_answer);
2423 IOHandlerSP io_handler_sp(confirm);
2424 m_debugger.RunIOHandlerSync(io_handler_sp);
2425 return confirm->GetResponse();
2426}
2427
2428const CommandAlias *
2429CommandInterpreter::GetAlias(llvm::StringRef alias_name) const {
2430 OptionArgVectorSP ret_val;
2431
2432 auto pos = m_alias_dict.find(alias_name);
2433 if (pos != m_alias_dict.end())
2434 return (CommandAlias *)pos->second.get();
2435
2436 return nullptr;
2437}
2438
2440 return (!m_command_dict.empty());
2441}
2442
2443bool CommandInterpreter::HasAliases() const { return (!m_alias_dict.empty()); }
2444
2446 return (!m_user_dict.empty());
2447}
2448
2450 return (!m_user_mw_dict.empty());
2451}
2452
2454
2456 const char *alias_name,
2457 Args &cmd_args,
2458 std::string &raw_input_string,
2459 CommandReturnObject &result) {
2460 OptionArgVectorSP option_arg_vector_sp =
2461 GetAlias(alias_name)->GetOptionArguments();
2462
2463 bool wants_raw_input = alias_cmd_obj->WantsRawCommandString();
2464
2465 // Make sure that the alias name is the 0th element in cmd_args
2466 std::string alias_name_str = alias_name;
2467 if (alias_name_str != cmd_args.GetArgumentAtIndex(0))
2468 cmd_args.Unshift(alias_name_str);
2469
2470 Args new_args(alias_cmd_obj->GetCommandName());
2471 if (new_args.GetArgumentCount() == 2)
2472 new_args.Shift();
2473
2474 if (option_arg_vector_sp.get()) {
2475 if (wants_raw_input) {
2476 // We have a command that both has command options and takes raw input.
2477 // Make *sure* it has a " -- " in the right place in the
2478 // raw_input_string.
2479 size_t pos = raw_input_string.find(" -- ");
2480 if (pos == std::string::npos) {
2481 // None found; assume it goes at the beginning of the raw input string
2482 raw_input_string.insert(0, " -- ");
2483 }
2484 }
2485
2486 OptionArgVector *option_arg_vector = option_arg_vector_sp.get();
2487 const size_t old_size = cmd_args.GetArgumentCount();
2488 std::vector<bool> used(old_size + 1, false);
2489
2490 used[0] = true;
2491
2492 int value_type;
2493 std::string option;
2494 std::string value;
2495 for (const auto &option_entry : *option_arg_vector) {
2496 std::tie(option, value_type, value) = option_entry;
2497 if (option == g_argument) {
2498 if (!wants_raw_input || (value != "--")) {
2499 // Since we inserted this above, make sure we don't insert it twice
2500 new_args.AppendArgument(value);
2501 }
2502 continue;
2503 }
2504
2505 if (value_type != OptionParser::eOptionalArgument)
2506 new_args.AppendArgument(option);
2507
2508 if (value == g_no_argument)
2509 continue;
2510
2511 int index = GetOptionArgumentPosition(value.c_str());
2512 if (index == 0) {
2513 // value was NOT a positional argument; must be a real value
2514 if (value_type != OptionParser::eOptionalArgument)
2515 new_args.AppendArgument(value);
2516 else {
2517 new_args.AppendArgument(option + value);
2518 }
2519
2520 } else if (static_cast<size_t>(index) >= cmd_args.GetArgumentCount()) {
2521 result.AppendErrorWithFormat("Not enough arguments provided; you "
2522 "need at least %d arguments to use "
2523 "this alias.\n",
2524 index);
2525 return;
2526 } else {
2527 // Find and remove cmd_args.GetArgumentAtIndex(i) from raw_input_string
2528 size_t strpos =
2529 raw_input_string.find(cmd_args.GetArgumentAtIndex(index));
2530 if (strpos != std::string::npos) {
2531 raw_input_string = raw_input_string.erase(
2532 strpos, strlen(cmd_args.GetArgumentAtIndex(index)));
2533 }
2534
2535 if (value_type != OptionParser::eOptionalArgument)
2536 new_args.AppendArgument(cmd_args.GetArgumentAtIndex(index));
2537 else {
2538 new_args.AppendArgument(option + cmd_args.GetArgumentAtIndex(index));
2539 }
2540 used[index] = true;
2541 }
2542 }
2543
2544 for (auto entry : llvm::enumerate(cmd_args.entries())) {
2545 if (!used[entry.index()] && !wants_raw_input)
2546 new_args.AppendArgument(entry.value().ref());
2547 }
2548
2549 cmd_args.Clear();
2550 cmd_args.SetArguments(new_args.GetArgumentCount(),
2551 new_args.GetConstArgumentVector());
2552 } else {
2554 // This alias was not created with any options; nothing further needs to be
2555 // done, unless it is a command that wants raw input, in which case we need
2556 // to clear the rest of the data from cmd_args, since its in the raw input
2557 // string.
2558 if (wants_raw_input) {
2559 cmd_args.Clear();
2560 cmd_args.SetArguments(new_args.GetArgumentCount(),
2561 new_args.GetConstArgumentVector());
2562 }
2563 return;
2564 }
2565
2567}
2568
2570 int position = 0; // Any string that isn't an argument position, i.e. '%'
2571 // followed by an integer, gets a position
2572 // of zero.
2573
2574 const char *cptr = in_string;
2575
2576 // Does it start with '%'
2577 if (cptr[0] == '%') {
2578 ++cptr;
2579
2580 // Is the rest of it entirely digits?
2581 if (isdigit(cptr[0])) {
2582 const char *start = cptr;
2583 while (isdigit(cptr[0]))
2584 ++cptr;
2585
2586 // We've gotten to the end of the digits; are we at the end of the
2587 // string?
2588 if (cptr[0] == '\0')
2589 position = atoi(start);
2590 }
2591 }
2592
2593 return position;
2594}
2595
2596static void GetHomeInitFile(FileSpec &init_file, llvm::StringRef suffix = {}) {
2597 std::string init_file_name = ".lldbinit";
2598 if (!suffix.empty()) {
2599 init_file_name.append("-");
2600 init_file_name.append(suffix.str());
2601 }
2602
2603 init_file =
2604 HostInfo::GetUserHomeDir().CopyByAppendingPathComponent(init_file_name);
2605}
2606
2607static void GetHomeREPLInitFile(FileSpec &init_file, LanguageType language) {
2608 if (language == eLanguageTypeUnknown) {
2610 if (auto main_repl_language = repl_languages.GetSingularLanguage())
2611 language = *main_repl_language;
2612 else
2613 return;
2614 }
2615
2616 std::string init_file_name =
2617 (llvm::Twine(".lldbinit-") +
2618 llvm::Twine(Language::GetNameForLanguageType(language)) +
2619 llvm::Twine("-repl"))
2620 .str();
2621
2622 init_file =
2623 HostInfo::GetUserHomeDir().CopyByAppendingPathComponent(init_file_name);
2624}
2625
2627 llvm::StringRef s = ".lldbinit";
2628 init_file.assign(s.begin(), s.end());
2629 FileSystem::Instance().Resolve(init_file);
2630}
2631
2633 CommandReturnObject &result) {
2634 assert(!m_skip_lldbinit_files);
2635
2636 if (!FileSystem::Instance().Exists(file)) {
2638 return;
2639 }
2640
2641 // Use HandleCommand to 'source' the given file; this will do the actual
2642 // broadcasting of the commands back to any appropriate listener (see
2643 // CommandObjectSource::Execute for more details).
2644 const bool saved_batch = SetBatchCommandMode(true);
2646 options.SetSilent(true);
2647 options.SetPrintErrors(true);
2648 options.SetStopOnError(false);
2649 options.SetStopOnContinue(true);
2650 HandleCommandsFromFile(file, options, result);
2651 SetBatchCommandMode(saved_batch);
2652}
2653
2657 return;
2658 }
2659
2660 llvm::SmallString<128> init_file;
2661 GetCwdInitFile(init_file);
2662 if (!FileSystem::Instance().Exists(init_file)) {
2664 return;
2665 }
2666
2667 LoadCWDlldbinitFile should_load =
2669
2670 switch (should_load) {
2673 break;
2675 SourceInitFile(FileSpec(init_file.str()), result);
2676 break;
2677 case eLoadCWDlldbinitWarn: {
2678 FileSpec home_init_file;
2679 GetHomeInitFile(home_init_file);
2680 if (llvm::sys::path::parent_path(init_file) ==
2681 llvm::sys::path::parent_path(home_init_file.GetPath())) {
2683 } else {
2685 }
2686 }
2687 }
2688}
2689
2690/// We will first see if there is an application specific ".lldbinit" file
2691/// whose name is "~/.lldbinit" followed by a "-" and the name of the program.
2692/// If this file doesn't exist, we fall back to the REPL init file or the
2693/// default home init file in "~/.lldbinit".
2695 bool is_repl) {
2698 return;
2699 }
2700
2701 FileSpec init_file;
2702
2703 if (is_repl)
2704 GetHomeREPLInitFile(init_file, GetDebugger().GetREPLLanguage());
2705
2706 if (init_file.GetPath().empty())
2707 GetHomeInitFile(init_file);
2708
2709 if (!m_skip_app_init_files) {
2710 llvm::StringRef program_name =
2711 HostInfo::GetProgramFileSpec().GetFilename().GetStringRef();
2712 FileSpec program_init_file;
2713 GetHomeInitFile(program_init_file, program_name);
2714 if (FileSystem::Instance().Exists(program_init_file))
2715 init_file = program_init_file;
2716 }
2717
2718 SourceInitFile(init_file, result);
2719}
2720
2722#ifdef LLDB_GLOBAL_INIT_DIRECTORY
2723 if (!m_skip_lldbinit_files) {
2724 FileSpec init_file(LLDB_GLOBAL_INIT_DIRECTORY);
2725 if (init_file)
2726 init_file.MakeAbsolute(HostInfo::GetShlibDir());
2727
2728 init_file.AppendPathComponent("lldbinit");
2729 SourceInitFile(init_file, result);
2730 return;
2731 }
2732#endif
2734}
2735
2737 const char *prefix = GetDebugger().GetIOHandlerCommandPrefix();
2738 return prefix == nullptr ? "" : prefix;
2739}
2740
2741PlatformSP CommandInterpreter::GetPlatform(bool prefer_target_platform) {
2742 PlatformSP platform_sp;
2743 if (prefer_target_platform) {
2745 Target *target = exe_ctx.GetTargetPtr();
2746 if (target)
2747 platform_sp = target->GetPlatform();
2748 }
2749
2750 if (!platform_sp)
2751 platform_sp = m_debugger.GetPlatformList().GetSelectedPlatform();
2752 return platform_sp;
2753}
2754
2756 auto exe_ctx = GetExecutionContext();
2757 TargetSP target_sp = exe_ctx.GetTargetSP();
2758 if (!target_sp)
2759 return false;
2760
2761 ProcessSP process_sp(target_sp->GetProcessSP());
2762 if (!process_sp)
2763 return false;
2764
2765 if (eStateStopped != process_sp->GetState())
2766 return false;
2767
2768 for (const auto &thread_sp : process_sp->GetThreadList().Threads()) {
2769 StopInfoSP stop_info = thread_sp->GetStopInfo();
2770 if (!stop_info) {
2771 // If there's no stop_info, keep iterating through the other threads;
2772 // it's enough that any thread has got a stop_info that indicates
2773 // an abnormal stop, to consider the process to be stopped abnormally.
2774 continue;
2775 }
2776
2777 const StopReason reason = stop_info->GetStopReason();
2778 if (reason == eStopReasonException ||
2779 reason == eStopReasonInstrumentation ||
2780 reason == eStopReasonProcessorTrace || reason == eStopReasonInterrupt ||
2782 return true;
2783
2784 if (reason == eStopReasonSignal) {
2785 const auto stop_signal = static_cast<int32_t>(stop_info->GetValue());
2786 UnixSignalsSP signals_sp = process_sp->GetUnixSignals();
2787 if (!signals_sp || !signals_sp->SignalIsValid(stop_signal))
2788 // The signal is unknown, treat it as abnormal.
2789 return true;
2790
2791 const auto sigint_num = signals_sp->GetSignalNumberFromName("SIGINT");
2792 const auto sigstop_num = signals_sp->GetSignalNumberFromName("SIGSTOP");
2793 if ((stop_signal != sigint_num) && (stop_signal != sigstop_num))
2794 // The signal very likely implies a crash.
2795 return true;
2796 }
2797 }
2798
2799 return false;
2800}
2801
2803 const StringList &commands, const ExecutionContext &override_context,
2804 const CommandInterpreterRunOptions &options, CommandReturnObject &result) {
2805
2806 OverrideExecutionContext(override_context);
2807 HandleCommands(commands, options, result);
2809}
2810
2812 const StringList &commands, const CommandInterpreterRunOptions &options,
2813 CommandReturnObject &result) {
2814 size_t num_lines = commands.GetSize();
2815
2816 // If we are going to continue past a "continue" then we need to run the
2817 // commands synchronously. Make sure you reset this value anywhere you return
2818 // from the function.
2819
2820 bool old_async_execution = m_debugger.GetAsyncExecution();
2821
2822 if (!options.GetStopOnContinue()) {
2823 m_debugger.SetAsyncExecution(false);
2824 }
2825
2826 for (size_t idx = 0; idx < num_lines; idx++) {
2827 const char *cmd = commands.GetStringAtIndex(idx);
2828 if (cmd[0] == '\0')
2829 continue;
2830
2831 if (options.GetEchoCommands()) {
2832 // TODO: Add Stream support.
2834 "{0} {1}", m_debugger.GetPrompt().str().c_str(), cmd);
2835 }
2836
2837 CommandReturnObject tmp_result(m_debugger.GetUseColor());
2838 tmp_result.SetInteractive(result.GetInteractive());
2839 tmp_result.SetSuppressImmediateOutput(true);
2840
2841 // We might call into a regex or alias command, in which case the
2842 // add_to_history will get lost. This m_command_source_depth dingus is the
2843 // way we turn off adding to the history in that case, so set it up here.
2844 if (!options.GetAddToHistory())
2846 bool success = HandleCommand(cmd, options.m_add_to_history, tmp_result);
2847 if (!options.GetAddToHistory())
2849
2850 if (options.GetPrintResults()) {
2851 if (tmp_result.Succeeded())
2852 result.AppendMessage(tmp_result.GetOutputString());
2853 }
2854
2855 if (!success || !tmp_result.Succeeded()) {
2856 std::string error_msg = tmp_result.GetErrorString();
2857 if (error_msg.empty())
2858 error_msg = "<unknown error>.\n";
2859 if (options.GetStopOnError()) {
2860 result.AppendErrorWithFormatv("Aborting reading of commands after "
2861 "command #{0}: '{1}' failed with {2}",
2862 (uint64_t)idx, cmd, error_msg);
2863 m_debugger.SetAsyncExecution(old_async_execution);
2864 return;
2865 }
2866 if (options.GetPrintResults()) {
2867 result.AppendMessageWithFormatv("Command #{0} '{1}' failed with {2}",
2868 (uint64_t)idx + 1, cmd, error_msg);
2869 }
2870 }
2871
2872 if (result.GetImmediateOutputStream())
2873 result.GetImmediateOutputStream()->Flush();
2874
2875 if (result.GetImmediateErrorStream())
2876 result.GetImmediateErrorStream()->Flush();
2877
2878 // N.B. Can't depend on DidChangeProcessState, because the state coming
2879 // into the command execution could be running (for instance in Breakpoint
2880 // Commands. So we check the return value to see if it is has running in
2881 // it.
2882 if ((tmp_result.GetStatus() == eReturnStatusSuccessContinuingNoResult) ||
2884 if (options.GetStopOnContinue()) {
2885 // If we caused the target to proceed, and we're going to stop in that
2886 // case, set the status in our real result before returning. This is
2887 // an error if the continue was not the last command in the set of
2888 // commands to be run.
2889 if (idx != num_lines - 1)
2890 result.AppendErrorWithFormat(
2891 "Aborting reading of commands after command #%" PRIu64
2892 ": '%s' continued the target.\n",
2893 (uint64_t)idx + 1, cmd);
2894 else
2896 "Command #{0} '{1}' continued the target.", (uint64_t)idx + 1,
2897 cmd);
2898
2899 result.SetStatus(tmp_result.GetStatus());
2900 m_debugger.SetAsyncExecution(old_async_execution);
2901
2902 return;
2903 }
2904 }
2905
2906 // Also check for "stop on crash here:
2907 if (tmp_result.GetDidChangeProcessState() && options.GetStopOnCrash() &&
2909 if (idx != num_lines - 1)
2910 result.AppendErrorWithFormat(
2911 "Aborting reading of commands after command #%" PRIu64
2912 ": '%s' stopped with a signal or exception.\n",
2913 (uint64_t)idx + 1, cmd);
2914 else
2916 "Command #{0} '{1}' stopped with a signal or exception.",
2917 (uint64_t)idx + 1, cmd);
2918
2919 result.SetStatus(tmp_result.GetStatus());
2920 m_debugger.SetAsyncExecution(old_async_execution);
2921
2922 return;
2923 }
2924 }
2925
2927 m_debugger.SetAsyncExecution(old_async_execution);
2928}
2929
2930// Make flags that we can pass into the IOHandler so our delegates can do the
2931// right thing
2932enum {
2941};
2942
2944 FileSpec &cmd_file, const ExecutionContext &context,
2945 const CommandInterpreterRunOptions &options, CommandReturnObject &result) {
2946 OverrideExecutionContext(context);
2947 HandleCommandsFromFile(cmd_file, options, result);
2949}
2950
2952 FileSpec &cmd_file, const CommandInterpreterRunOptions &options,
2953 CommandReturnObject &result) {
2954 if (!FileSystem::Instance().Exists(cmd_file)) {
2955 result.AppendErrorWithFormat(
2956 "Error reading commands from file %s - file not found.\n",
2957 cmd_file.GetFilename().AsCString("<Unknown>"));
2958 return;
2959 }
2960
2961 std::string cmd_file_path = cmd_file.GetPath();
2962 auto input_file_up =
2964 if (!input_file_up) {
2965 std::string error = llvm::toString(input_file_up.takeError());
2967 "error: an error occurred read file '{0}': {1}\n", cmd_file_path,
2968 llvm::fmt_consume(input_file_up.takeError()));
2969 return;
2970 }
2971 FileSP input_file_sp = FileSP(std::move(input_file_up.get()));
2972
2973 Debugger &debugger = GetDebugger();
2974
2975 uint32_t flags = 0;
2976
2977 if (options.m_stop_on_continue == eLazyBoolCalculate) {
2978 if (m_command_source_flags.empty()) {
2979 // Stop on continue by default
2981 } else if (m_command_source_flags.back() &
2984 }
2985 } else if (options.m_stop_on_continue == eLazyBoolYes) {
2987 }
2988
2989 if (options.m_stop_on_error == eLazyBoolCalculate) {
2990 if (m_command_source_flags.empty()) {
2995 }
2996 } else if (options.m_stop_on_error == eLazyBoolYes) {
2998 }
2999
3000 // stop-on-crash can only be set, if it is present in all levels of
3001 // pushed flag sets.
3002 if (options.GetStopOnCrash()) {
3003 if (m_command_source_flags.empty()) {
3007 }
3008 }
3009
3010 if (options.m_echo_commands == eLazyBoolCalculate) {
3011 if (m_command_source_flags.empty()) {
3012 // Echo command by default
3016 }
3017 } else if (options.m_echo_commands == eLazyBoolYes) {
3019 }
3020
3021 // We will only ever ask for this flag, if we echo commands in general.
3023 if (m_command_source_flags.empty()) {
3024 // Echo comments by default
3026 } else if (m_command_source_flags.back() &
3029 }
3030 } else if (options.m_echo_comment_commands == eLazyBoolYes) {
3032 }
3033
3034 if (options.m_print_results == eLazyBoolCalculate) {
3035 if (m_command_source_flags.empty()) {
3036 // Print output by default
3040 }
3041 } else if (options.m_print_results == eLazyBoolYes) {
3043 }
3044
3045 if (options.m_print_errors == eLazyBoolCalculate) {
3046 if (m_command_source_flags.empty()) {
3047 // Print output by default
3051 }
3052 } else if (options.m_print_errors == eLazyBoolYes) {
3054 }
3055
3056 if (flags & eHandleCommandFlagPrintResult) {
3057 debugger.GetOutputFileSP()->Printf("Executing commands in '%s'.\n",
3058 cmd_file_path.c_str());
3059 }
3060
3061 // Used for inheriting the right settings when "command source" might
3062 // have nested "command source" commands
3063 lldb::LockableStreamFileSP empty_stream_sp;
3064 m_command_source_flags.push_back(flags);
3065 IOHandlerSP io_handler_sp(new IOHandlerEditline(
3066 debugger, IOHandler::Type::CommandInterpreter, input_file_sp,
3067 empty_stream_sp, // Pass in an empty stream so we inherit the top
3068 // input reader output stream
3069 empty_stream_sp, // Pass in an empty stream so we inherit the top
3070 // input reader error stream
3071 flags,
3072 nullptr, // Pass in NULL for "editline_name" so no history is saved,
3073 // or written
3074 debugger.GetPrompt(), llvm::StringRef(),
3075 false, // Not multi-line
3076 debugger.GetUseColor(), 0, *this));
3077 const bool old_async_execution = debugger.GetAsyncExecution();
3078
3079 // Set synchronous execution if we are not stopping on continue
3080 if ((flags & eHandleCommandFlagStopOnContinue) == 0)
3081 debugger.SetAsyncExecution(false);
3082
3085
3086 debugger.RunIOHandlerSync(io_handler_sp);
3087 if (!m_command_source_flags.empty())
3088 m_command_source_flags.pop_back();
3089
3090 m_command_source_dirs.pop_back();
3092
3094 debugger.SetAsyncExecution(old_async_execution);
3095}
3096
3098
3102
3104 llvm::StringRef prefix,
3105 llvm::StringRef help_text) {
3106 const uint32_t max_columns = m_debugger.GetTerminalWidth();
3107
3108 size_t line_width_max = max_columns - prefix.size();
3109 if (line_width_max < 16)
3110 line_width_max = help_text.size() + prefix.size();
3111
3112 strm.IndentMore(prefix.size());
3113 bool prefixed_yet = false;
3114 // Even if we have no help text we still want to emit the command name.
3115 if (help_text.empty())
3116 help_text = "No help text";
3117 while (!help_text.empty()) {
3118 // Prefix the first line, indent subsequent lines to line up
3119 if (!prefixed_yet) {
3120 strm << prefix;
3121 prefixed_yet = true;
3122 } else
3123 strm.Indent();
3124
3125 // Never print more than the maximum on one line.
3126 llvm::StringRef this_line = help_text.substr(0, line_width_max);
3127
3128 // Always break on an explicit newline.
3129 std::size_t first_newline = this_line.find_first_of("\n");
3130
3131 // Don't break on space/tab unless the text is too long to fit on one line.
3132 std::size_t last_space = llvm::StringRef::npos;
3133 if (this_line.size() != help_text.size())
3134 last_space = this_line.find_last_of(" \t");
3135
3136 // Break at whichever condition triggered first.
3137 this_line = this_line.substr(0, std::min(first_newline, last_space));
3138 strm.PutCString(this_line);
3139 strm.EOL();
3140
3141 // Remove whitespace / newlines after breaking.
3142 help_text = help_text.drop_front(this_line.size()).ltrim();
3143 }
3144 strm.IndentLess(prefix.size());
3145}
3146
3148 llvm::StringRef word_text,
3149 llvm::StringRef separator,
3150 llvm::StringRef help_text,
3151 size_t max_word_len) {
3152 StreamString prefix_stream;
3153 prefix_stream.Printf(" %-*s %*s ", (int)max_word_len, word_text.data(),
3154 (int)separator.size(), separator.data());
3155 OutputFormattedHelpText(strm, prefix_stream.GetString(), help_text);
3156}
3157
3158void CommandInterpreter::OutputHelpText(Stream &strm, llvm::StringRef word_text,
3159 llvm::StringRef separator,
3160 llvm::StringRef help_text,
3161 uint32_t max_word_len) {
3162 int indent_size = max_word_len + separator.size() + 2;
3163
3164 strm.IndentMore(indent_size);
3165
3166 StreamString text_strm;
3167 text_strm.Printf("%-*s ", (int)max_word_len, word_text.data());
3168 text_strm << separator << " " << help_text;
3169
3170 const uint32_t max_columns = m_debugger.GetTerminalWidth();
3171
3172 llvm::StringRef text = text_strm.GetString();
3173
3174 uint32_t chars_left = max_columns;
3175
3176 auto nextWordLength = [](llvm::StringRef S) {
3177 size_t pos = S.find(' ');
3178 return pos == llvm::StringRef::npos ? S.size() : pos;
3179 };
3180
3181 while (!text.empty()) {
3182 if (text.front() == '\n' ||
3183 (text.front() == ' ' && nextWordLength(text.ltrim(' ')) > chars_left)) {
3184 strm.EOL();
3185 strm.Indent();
3186 chars_left = max_columns - indent_size;
3187 if (text.front() == '\n')
3188 text = text.drop_front();
3189 else
3190 text = text.ltrim(' ');
3191 } else {
3192 strm.PutChar(text.front());
3193 --chars_left;
3194 text = text.drop_front();
3195 }
3196 }
3197
3198 strm.EOL();
3199 strm.IndentLess(indent_size);
3200}
3201
3203 llvm::StringRef search_word, StringList &commands_found,
3204 StringList &commands_help, const CommandObject::CommandMap &command_map) {
3205 for (const auto &pair : command_map) {
3206 llvm::StringRef command_name = pair.first;
3207 CommandObject *cmd_obj = pair.second.get();
3208
3209 const bool search_short_help = true;
3210 const bool search_long_help = false;
3211 const bool search_syntax = false;
3212 const bool search_options = false;
3213 if (command_name.contains_insensitive(search_word) ||
3214 cmd_obj->HelpTextContainsWord(search_word, search_short_help,
3215 search_long_help, search_syntax,
3216 search_options)) {
3217 commands_found.AppendString(command_name);
3218 commands_help.AppendString(cmd_obj->GetHelp());
3219 }
3220
3221 if (auto *multiword_cmd = cmd_obj->GetAsMultiwordCommand()) {
3222 StringList subcommands_found;
3223 FindCommandsForApropos(search_word, subcommands_found, commands_help,
3224 multiword_cmd->GetSubcommandDictionary());
3225 for (const auto &subcommand_name : subcommands_found) {
3226 std::string qualified_name =
3227 (command_name + " " + subcommand_name).str();
3228 commands_found.AppendString(qualified_name);
3229 }
3230 }
3231 }
3232}
3233
3234void CommandInterpreter::FindCommandsForApropos(llvm::StringRef search_word,
3235 StringList &commands_found,
3236 StringList &commands_help,
3237 bool search_builtin_commands,
3238 bool search_user_commands,
3239 bool search_alias_commands,
3240 bool search_user_mw_commands) {
3241 CommandObject::CommandMap::const_iterator pos;
3242
3243 if (search_builtin_commands)
3244 FindCommandsForApropos(search_word, commands_found, commands_help,
3246
3247 if (search_user_commands)
3248 FindCommandsForApropos(search_word, commands_found, commands_help,
3249 m_user_dict);
3250
3251 if (search_user_mw_commands)
3252 FindCommandsForApropos(search_word, commands_found, commands_help,
3254
3255 if (search_alias_commands)
3256 FindCommandsForApropos(search_word, commands_found, commands_help,
3257 m_alias_dict);
3258}
3259
3261 return !m_overriden_exe_contexts.empty()
3263 : m_debugger.GetSelectedExecutionContext();
3264}
3265
3267 const ExecutionContext &override_context) {
3268 m_overriden_exe_contexts.push(override_context);
3269}
3270
3275
3277 if (ProcessSP process_sp = GetExecutionContext().GetProcessSP())
3278 m_debugger.FlushProcessOutput(*process_sp, /*flush_stdout*/ true,
3279 /*flush_stderr*/ true);
3280}
3281
3291
3299
3301 auto in_progress = CommandHandlingState::eInProgress;
3302 return m_command_state.compare_exchange_strong(
3304}
3305
3307 if (!m_debugger.IsIOHandlerThreadCurrentThread())
3308 return false;
3309
3310 bool was_interrupted =
3312 lldbassert(!was_interrupted || m_iohandler_nesting_level > 0);
3313 return was_interrupted;
3314}
3315
3317 llvm::StringRef str,
3318 bool is_stdout) {
3319
3320 lldb::LockableStreamFileSP stream = is_stdout
3321 ? io_handler.GetOutputStreamFileSP()
3322 : io_handler.GetErrorStreamFileSP();
3323 // Split the output into lines and poll for interrupt requests
3324 bool had_output = !str.empty();
3325 while (!str.empty()) {
3326 llvm::StringRef line;
3327 std::tie(line, str) = str.split('\n');
3328 {
3329 LockedStreamFile stream_file = stream->Lock();
3330 stream_file.Write(line.data(), line.size());
3331 stream_file.Write("\n", 1);
3332 }
3333 }
3334
3335 LockedStreamFile stream_file = stream->Lock();
3336 if (had_output &&
3337 INTERRUPT_REQUESTED(GetDebugger(), "Interrupted dumping command output"))
3338 stream_file.Printf("\n... Interrupted.\n");
3339 stream_file.Flush();
3340}
3341
3343 llvm::StringRef line, const Flags &io_handler_flags) const {
3344 if (!io_handler_flags.Test(eHandleCommandFlagEchoCommand))
3345 return false;
3346
3347 llvm::StringRef command = line.trim();
3348 if (command.empty())
3349 return true;
3350
3351 if (command.front() == m_comment_char)
3352 return io_handler_flags.Test(eHandleCommandFlagEchoCommentCommand);
3353
3354 return true;
3355}
3356
3358 std::string &line) {
3359 // If we were interrupted, bail out...
3360 if (WasInterrupted())
3361 return;
3362
3363 const bool is_interactive = io_handler.GetIsInteractive();
3364 const bool allow_repeats =
3366
3367 if (!is_interactive && !allow_repeats) {
3368 // When we are not interactive, don't execute blank lines. This will happen
3369 // sourcing a commands file. We don't want blank lines to repeat the
3370 // previous command and cause any errors to occur (like redefining an
3371 // alias, get an error and stop parsing the commands file).
3372 // But obey the AllowRepeats flag if the user has set it.
3373 if (line.empty())
3374 return;
3375 }
3376 if (!is_interactive) {
3377 // When using a non-interactive file handle (like when sourcing commands
3378 // from a file) we need to echo the command out so we don't just see the
3379 // command output and no command...
3380 if (EchoCommandNonInteractive(line, io_handler.GetFlags())) {
3381 LockedStreamFile locked_stream =
3382 io_handler.GetOutputStreamFileSP()->Lock();
3383 locked_stream.Printf("%s%s\n", io_handler.GetPrompt(), line.c_str());
3384 }
3385 }
3386
3388
3389 ExecutionContext exe_ctx = m_debugger.GetSelectedExecutionContext();
3390 bool pushed_exe_ctx = false;
3391 if (exe_ctx.HasTargetScope()) {
3392 OverrideExecutionContext(exe_ctx);
3393 pushed_exe_ctx = true;
3394 }
3395 llvm::scope_exit finalize([this, pushed_exe_ctx]() {
3396 if (pushed_exe_ctx)
3398 });
3399
3400 lldb_private::CommandReturnObject result(m_debugger.GetUseColor());
3401 HandleCommand(line.c_str(), eLazyBoolCalculate, result);
3402
3403 // Now emit the command output text from the command we just executed
3404 if ((result.Succeeded() &&
3407 auto DefaultPrintCallback = [&](const CommandReturnObject &result) {
3408 // Display any inline diagnostics first.
3409 const bool inline_diagnostics = !result.GetImmediateErrorStream() &&
3411 if (inline_diagnostics) {
3412 unsigned prompt_len = m_debugger.GetPrompt().size();
3413 if (auto indent = result.GetDiagnosticIndent()) {
3414 std::string diags =
3415 result.GetInlineDiagnosticString(prompt_len + *indent);
3416 PrintCommandOutput(io_handler, diags, true);
3417 }
3418 }
3419
3420 // Display any STDOUT/STDERR _prior_ to emitting the command result text.
3422
3423 if (!result.GetImmediateOutputStream()) {
3424 llvm::StringRef output = result.GetOutputString();
3425 PrintCommandOutput(io_handler, output, true);
3426 }
3427
3428 // Now emit the command error text from the command we just executed.
3429 if (!result.GetImmediateErrorStream()) {
3430 std::string error = result.GetErrorString(!inline_diagnostics);
3431 PrintCommandOutput(io_handler, error, false);
3432 }
3433 };
3434
3435 if (m_print_callback) {
3436 const auto callback_result = m_print_callback(result);
3437 if (callback_result == eCommandReturnObjectPrintCallbackSkipped)
3438 DefaultPrintCallback(result);
3439 } else {
3440 DefaultPrintCallback(result);
3441 }
3442 }
3443
3445
3446 switch (result.GetStatus()) {
3451 break;
3452
3456 io_handler.SetIsDone(true);
3457 break;
3458
3460 m_result.IncrementNumberOfErrors();
3461 if (io_handler.GetFlags().Test(eHandleCommandFlagStopOnError)) {
3463 io_handler.SetIsDone(true);
3464 }
3465 break;
3466
3467 case eReturnStatusQuit:
3469 io_handler.SetIsDone(true);
3470 break;
3471 }
3472
3473 // Finally, if we're going to stop on crash, check that here:
3475 result.GetDidChangeProcessState() &&
3478 io_handler.SetIsDone(true);
3480 }
3481}
3482
3485 Process *process = exe_ctx.GetProcessPtr();
3486
3487 if (InterruptCommand())
3488 return true;
3489
3490 if (process) {
3491 StateType state = process->GetState();
3492 if (StateIsRunningState(state)) {
3493 process->Halt();
3494 return true; // Don't do any updating when we are running
3495 }
3496 }
3497
3498 ScriptInterpreter *script_interpreter =
3499 m_debugger.GetScriptInterpreter(false);
3500 if (script_interpreter) {
3501 if (script_interpreter->Interrupt())
3502 return true;
3503 }
3504 return false;
3505}
3506
3508 CommandReturnObject &result, std::optional<std::string> output_file) {
3509 if (output_file == std::nullopt || output_file->empty()) {
3510 std::string now = llvm::to_string(std::chrono::system_clock::now());
3511 llvm::replace(now, ' ', '_');
3512 // Can't have file name with colons on Windows
3513 llvm::replace(now, ':', '-');
3514 const std::string file_name = "lldb_session_" + now + ".log";
3515
3516 FileSpec save_location = GetSaveSessionDirectory();
3517
3518 if (!save_location)
3519 save_location = HostInfo::GetGlobalTempDir();
3520
3521 FileSystem::Instance().Resolve(save_location);
3522 save_location.AppendPathComponent(file_name);
3523 output_file = save_location.GetPath();
3524 }
3525
3526 auto error_out = [&](llvm::StringRef error_message, std::string description) {
3527 LLDB_LOG(GetLog(LLDBLog::Commands), "{0} ({1}:{2})", error_message,
3528 output_file, description);
3530 "Failed to save session's transcripts to {0}!", *output_file);
3531 return false;
3532 };
3533
3537
3538 auto opened_file = FileSystem::Instance().Open(FileSpec(*output_file), flags);
3539
3540 if (!opened_file)
3541 return error_out("Unable to create file",
3542 llvm::toString(opened_file.takeError()));
3543
3544 FileUP file = std::move(opened_file.get());
3545
3546 size_t byte_size = m_transcript_stream.GetSize();
3547
3548 Status error = file->Write(m_transcript_stream.GetData(), byte_size);
3549
3550 if (error.Fail() || byte_size != m_transcript_stream.GetSize())
3551 return error_out("Unable to write to destination file",
3552 "Bytes written do not match transcript size.");
3553
3555 result.AppendMessageWithFormatv("Session's transcripts saved to {0}",
3556 output_file->c_str());
3557 if (!GetSaveTranscript())
3558 result.AppendError(
3559 "Note: the setting interpreter.save-transcript is set to false, so the "
3560 "transcript might not have been recorded.");
3561
3563 const FileSpec file_spec;
3564 error = file->GetFileSpec(const_cast<FileSpec &>(file_spec));
3565 if (error.Success()) {
3566 if (llvm::Error e = Host::OpenFileInExternalEditor(
3567 m_debugger.GetExternalEditor(), file_spec, 1))
3568 result.AppendError(llvm::toString(std::move(e)));
3569 }
3570 }
3571
3572 return true;
3573}
3574
3576 return (GetIOHandler() ? GetIOHandler()->GetIsInteractive() : false);
3577}
3578
3580 if (m_command_source_dirs.empty())
3581 return {};
3582 return m_command_source_dirs.back();
3583}
3584
3586 const char *prompt, IOHandlerDelegate &delegate, void *baton) {
3587 Debugger &debugger = GetDebugger();
3588 IOHandlerSP io_handler_sp(
3590 "lldb", // Name of input reader for history
3591 llvm::StringRef(prompt), // Prompt
3592 llvm::StringRef(), // Continuation prompt
3593 true, // Get multiple lines
3594 debugger.GetUseColor(),
3595 0, // Don't show line numbers
3596 delegate)); // IOHandlerDelegate
3597
3598 if (io_handler_sp) {
3599 io_handler_sp->SetUserData(baton);
3600 debugger.RunIOHandlerAsync(io_handler_sp);
3601 }
3602}
3603
3605 const char *prompt, IOHandlerDelegate &delegate, void *baton) {
3606 Debugger &debugger = GetDebugger();
3607 IOHandlerSP io_handler_sp(
3609 "lldb-python", // Name of input reader for history
3610 llvm::StringRef(prompt), // Prompt
3611 llvm::StringRef(), // Continuation prompt
3612 true, // Get multiple lines
3613 debugger.GetUseColor(),
3614 0, // Don't show line numbers
3615 delegate)); // IOHandlerDelegate
3616
3617 if (io_handler_sp) {
3618 io_handler_sp->SetUserData(baton);
3619 debugger.RunIOHandlerAsync(io_handler_sp);
3620 }
3621}
3622
3624 return m_debugger.IsTopIOHandler(m_command_io_handler_sp);
3625}
3626
3630 // Always re-create the IOHandlerEditline in case the input changed. The old
3631 // instance might have had a non-interactive input and now it does or vice
3632 // versa.
3633 if (force_create || !m_command_io_handler_sp) {
3634 // Always re-create the IOHandlerEditline in case the input changed. The
3635 // old instance might have had a non-interactive input and now it does or
3636 // vice versa.
3637 uint32_t flags = 0;
3638
3639 if (options) {
3640 if (options->m_stop_on_continue == eLazyBoolYes)
3642 if (options->m_stop_on_error == eLazyBoolYes)
3644 if (options->m_stop_on_crash == eLazyBoolYes)
3646 if (options->m_echo_commands != eLazyBoolNo)
3648 if (options->m_echo_comment_commands != eLazyBoolNo)
3650 if (options->m_print_results != eLazyBoolNo)
3652 if (options->m_print_errors != eLazyBoolNo)
3654 if (options->m_allow_repeats == eLazyBoolYes)
3656 } else {
3659 }
3660
3661 m_command_io_handler_sp = std::make_shared<IOHandlerEditline>(
3663 m_debugger.GetInputFileSP(), m_debugger.GetOutputStreamSP(),
3664 m_debugger.GetErrorStreamSP(), flags, "lldb", m_debugger.GetPrompt(),
3665 llvm::StringRef(), // Continuation prompt
3666 false, // Don't enable multiple line input, just single line commands
3667 m_debugger.GetUseColor(),
3668 0, // Don't show line numbers
3669 *this); // IOHandlerDelegate
3670 }
3672}
3673
3676 // Always re-create the command interpreter when we run it in case any file
3677 // handles have changed.
3678 bool force_create = true;
3679 m_debugger.RunIOHandlerAsync(GetIOHandler(force_create, &options));
3681
3682 if (options.GetAutoHandleEvents())
3683 m_debugger.StartEventHandlerThread();
3684
3685 if (options.GetSpawnThread()) {
3686 m_debugger.StartIOHandlerThread();
3687 } else {
3688 // If the current thread is not managed by a host thread, we won't detect
3689 // that this IS the CommandInterpreter IOHandler thread, so make it so:
3690 HostThread new_io_handler_thread(Host::GetCurrentThread());
3691 HostThread old_io_handler_thread =
3692 m_debugger.SetIOHandlerThread(new_io_handler_thread);
3693 m_debugger.RunIOHandlers();
3694 m_debugger.SetIOHandlerThread(old_io_handler_thread);
3695
3696 if (options.GetAutoHandleEvents())
3697 m_debugger.StopEventHandlerThread();
3698 }
3699
3700 return m_result;
3701}
3702
3705 CommandReturnObject &result) {
3706 std::string scratch_command(command_line); // working copy so we don't modify
3707 // command_line unless we succeed
3708 CommandObject *cmd_obj = nullptr;
3709 StreamString revised_command_line;
3710 bool wants_raw_input = false;
3711 std::string next_word;
3712 StringList matches;
3713 bool done = false;
3714
3715 auto build_alias_cmd = [&](std::string &full_name) {
3716 revised_command_line.Clear();
3717 matches.Clear();
3718 std::string alias_result;
3719 cmd_obj =
3720 BuildAliasResult(full_name, scratch_command, alias_result, result);
3721 revised_command_line.Printf("%s", alias_result.c_str());
3722 if (cmd_obj) {
3723 wants_raw_input = cmd_obj->WantsRawCommandString();
3724 }
3725 };
3726
3727 while (!done) {
3728 char quote_char = '\0';
3729 std::string suffix;
3730 ExtractCommand(scratch_command, next_word, suffix, quote_char);
3731 if (cmd_obj == nullptr) {
3732 std::string full_name;
3733 bool is_alias = GetAliasFullName(next_word, full_name);
3734 cmd_obj = GetCommandObject(next_word, &matches);
3735 bool is_real_command =
3736 (!is_alias) || (cmd_obj != nullptr && !cmd_obj->IsAlias());
3737 if (!is_real_command) {
3738 build_alias_cmd(full_name);
3739 } else {
3740 if (cmd_obj) {
3741 llvm::StringRef cmd_name = cmd_obj->GetCommandName();
3742 revised_command_line.Printf("%s", cmd_name.str().c_str());
3743 wants_raw_input = cmd_obj->WantsRawCommandString();
3744 } else {
3745 revised_command_line.Printf("%s", next_word.c_str());
3746 }
3747 }
3748 } else {
3749 if (cmd_obj->IsMultiwordObject()) {
3750 CommandObject *sub_cmd_obj =
3751 cmd_obj->GetSubcommandObject(next_word.c_str());
3752 if (sub_cmd_obj) {
3753 // The subcommand's name includes the parent command's name, so
3754 // restart rather than append to the revised_command_line.
3755 llvm::StringRef sub_cmd_name = sub_cmd_obj->GetCommandName();
3756 revised_command_line.Clear();
3757 revised_command_line.Printf("%s", sub_cmd_name.str().c_str());
3758 cmd_obj = sub_cmd_obj;
3759 wants_raw_input = cmd_obj->WantsRawCommandString();
3760 } else {
3761 if (quote_char)
3762 revised_command_line.Printf(" %c%s%s%c", quote_char,
3763 next_word.c_str(), suffix.c_str(),
3764 quote_char);
3765 else
3766 revised_command_line.Printf(" %s%s", next_word.c_str(),
3767 suffix.c_str());
3768 done = true;
3769 }
3770 } else {
3771 if (quote_char)
3772 revised_command_line.Printf(" %c%s%s%c", quote_char,
3773 next_word.c_str(), suffix.c_str(),
3774 quote_char);
3775 else
3776 revised_command_line.Printf(" %s%s", next_word.c_str(),
3777 suffix.c_str());
3778 done = true;
3779 }
3780 }
3781
3782 if (cmd_obj == nullptr) {
3783 const size_t num_matches = matches.GetSize();
3784 if (matches.GetSize() > 1) {
3785 StringList alias_matches;
3786 GetAliasCommandObject(next_word, &alias_matches);
3787
3788 if (alias_matches.GetSize() == 1) {
3789 std::string full_name;
3790 GetAliasFullName(alias_matches.GetStringAtIndex(0), full_name);
3791 build_alias_cmd(full_name);
3792 done = static_cast<bool>(cmd_obj);
3793 } else {
3794 StreamString error_msg;
3795 error_msg.Printf("ambiguous command '%s'. Possible matches:\n",
3796 next_word.c_str());
3797 for (uint32_t i = 0; i < num_matches; ++i)
3798 error_msg.Printf("\t%s\n", matches.GetStringAtIndex(i));
3799 result.AppendError(error_msg.GetString());
3800 }
3801 } else {
3802 // We didn't have only one match, otherwise we wouldn't get here.
3803 lldbassert(num_matches == 0);
3804 result.AppendErrorWithFormat("'%s' is not a valid command.\n",
3805 next_word.c_str());
3806 }
3807 if (!done)
3808 return nullptr;
3809 }
3810
3811 if (cmd_obj->IsMultiwordObject()) {
3812 if (!suffix.empty()) {
3813 result.AppendErrorWithFormat(
3814 "command '%s' did not recognize '%s%s%s' as valid (subcommand "
3815 "might be invalid).\n",
3816 cmd_obj->GetCommandName().str().c_str(),
3817 next_word.empty() ? "" : next_word.c_str(),
3818 next_word.empty() ? " -- " : " ", suffix.c_str());
3819 return nullptr;
3820 }
3821 } else {
3822 // If we found a normal command, we are done
3823 done = true;
3824 if (!suffix.empty()) {
3825 switch (suffix[0]) {
3826 case '/':
3827 // GDB format suffixes
3828 {
3829 Options *command_options = cmd_obj->GetOptions();
3830 if (command_options &&
3831 command_options->SupportsLongOption("gdb-format")) {
3832 std::string gdb_format_option("--gdb-format=");
3833 gdb_format_option += (suffix.c_str() + 1);
3834
3835 std::string cmd = std::string(revised_command_line.GetString());
3836 size_t arg_terminator_idx = FindArgumentTerminator(cmd);
3837 if (arg_terminator_idx != std::string::npos) {
3838 // Insert the gdb format option before the "--" that terminates
3839 // options
3840 gdb_format_option.append(1, ' ');
3841 cmd.insert(arg_terminator_idx, gdb_format_option);
3842 revised_command_line.Clear();
3843 revised_command_line.PutCString(cmd);
3844 } else
3845 revised_command_line.Printf(" %s", gdb_format_option.c_str());
3846
3847 if (wants_raw_input &&
3848 FindArgumentTerminator(cmd) == std::string::npos)
3849 revised_command_line.PutCString(" --");
3850 } else {
3851 result.AppendErrorWithFormat(
3852 "the '%s' command doesn't support the --gdb-format option\n",
3853 cmd_obj->GetCommandName().str().c_str());
3854 return nullptr;
3855 }
3856 }
3857 break;
3858
3859 default:
3860 result.AppendErrorWithFormat(
3861 "unknown command shorthand suffix: '%s'\n", suffix.c_str());
3862 return nullptr;
3863 }
3864 }
3865 }
3866 if (scratch_command.empty())
3867 done = true;
3868 }
3869
3870 if (!scratch_command.empty())
3871 revised_command_line.Printf(" %s", scratch_command.c_str());
3872
3873 if (cmd_obj != nullptr)
3874 command_line = std::string(revised_command_line.GetString());
3875
3876 return cmd_obj;
3877}
3878
3880 llvm::json::Object stats;
3881 for (const auto &command_usage : m_command_usages)
3882 stats.try_emplace(command_usage.getKey(), command_usage.getValue());
3883 return stats;
3884}
3885
3889
static const char * k_valid_command_chars
static constexpr const char * InitFileWarning
static size_t FindArgumentTerminator(const std::string &s)
@ eHandleCommandFlagAllowRepeats
@ eHandleCommandFlagStopOnCrash
@ eHandleCommandFlagEchoCommentCommand
@ eHandleCommandFlagStopOnError
@ eHandleCommandFlagStopOnContinue
@ eHandleCommandFlagPrintErrors
@ eHandleCommandFlagEchoCommand
@ eHandleCommandFlagPrintResult
static void GetHomeInitFile(FileSpec &init_file, llvm::StringRef suffix={})
#define REGISTER_COMMAND_OBJECT(NAME, CLASS)
static void StripLeadingSpaces(std::string &s)
static void GetCwdInitFile(llvm::SmallVectorImpl< char > &init_file)
static void GetHomeREPLInitFile(FileSpec &init_file, LanguageType language)
static const char * k_white_space
static bool ExtractCommand(std::string &command_string, std::string &command, std::string &suffix, char &quote_char)
static llvm::raw_ostream & error(Stream &strm)
#define INTERRUPT_REQUESTED(debugger,...)
This handy define will keep you from having to generate a report for the interruption by hand.
Definition Debugger.h:494
#define lldbassert(x)
Definition LLDBAssert.h:16
#define LLDB_LOG(log,...)
The LLDB_LOG* macros defined below are the way to emit log messages.
Definition Log.h:369
#define LLDB_LOGF(log,...)
Definition Log.h:383
static double elapsed(const StatsTimepoint &start, const StatsTimepoint &end)
#define LLDB_SCOPED_TIMER()
Definition Timer.h:83
#define LLDB_SCOPED_TIMERF(...)
Definition Timer.h:86
A command line argument class.
Definition Args.h:33
void Unshift(llvm::StringRef arg_str, char quote_char='\0')
Inserts a class owned copy of arg_str at the beginning of the argument vector.
Definition Args.cpp:303
void Shift()
Shifts the first argument C string value of the array off the argument array.
Definition Args.cpp:295
void SetArguments(size_t argc, const char **argv)
Sets the argument vector value, optionally copying all arguments into an internal buffer.
Definition Args.cpp:367
size_t GetArgumentCount() const
Gets the number of arguments left in this command object.
Definition Args.h:120
void AppendArgument(llvm::StringRef arg_str, char quote_char='\0')
Appends a new argument to the end of the list argument list.
Definition Args.cpp:332
llvm::ArrayRef< ArgEntry > entries() const
Definition Args.h:132
const char * GetArgumentAtIndex(size_t idx) const
Gets the NULL terminated C string argument pointer for the argument at index idx.
Definition Args.cpp:273
const char ** GetConstArgumentVector() const
Gets the argument vector.
Definition Args.cpp:289
void Clear()
Clear the arguments.
Definition Args.cpp:388
Broadcaster(lldb::BroadcasterManagerSP manager_sp, std::string name)
Construct with a broadcaster with a name.
void SetEventName(uint32_t event_mask, const char *name)
Set the name for an event bit.
void BroadcastEvent(lldb::EventSP &event_sp)
Broadcast an event which has no associated data.
OptionArgVectorSP GetOptionArguments() const
void SetHelp(llvm::StringRef str) override
void SetHelpLong(llvm::StringRef str) override
static const char g_repeat_char
bool EchoCommandNonInteractive(llvm::StringRef line, const Flags &io_handler_flags) const
void UpdatePrompt(llvm::StringRef prompt)
bool IOHandlerInterrupt(IOHandler &io_handler) override
lldb::CommandObjectSP GetFrameLanguageCommand() const
Return the language specific command object for the current frame.
void SourceInitFileHome(CommandReturnObject &result, bool is_repl)
We will first see if there is an application specific ".lldbinit" file whose name is "~/....
std::optional< std::string > GetAutoSuggestionForCommand(llvm::StringRef line)
Returns the auto-suggestion string that should be added to the given command line.
void IOHandlerInputComplete(IOHandler &io_handler, std::string &line) override
Called when a line or lines have been retrieved.
CommandReturnObjectCallback m_print_callback
An optional callback to handle printing the CommandReturnObject.
std::stack< ExecutionContext > m_overriden_exe_contexts
void OutputFormattedHelpText(Stream &strm, llvm::StringRef prefix, llvm::StringRef help_text)
bool Confirm(llvm::StringRef message, bool default_answer)
bool UserMultiwordCommandExists(llvm::StringRef cmd) const
Determine whether a root-level user multiword command with this name exists.
static llvm::StringRef GetStaticBroadcasterClass()
CommandObject * GetAliasCommandObject(llvm::StringRef cmd, StringList *matches=nullptr, StringList *descriptions=nullptr) const
bool RemoveAlias(llvm::StringRef alias_name)
void SetSaveSessionDirectory(llvm::StringRef path)
std::function< lldb::CommandReturnObjectCallbackResult( CommandReturnObject &)> CommandReturnObjectCallback
void SourceInitFile(FileSpec file, CommandReturnObject &result)
CommandAlias * AddAlias(llvm::StringRef alias_name, lldb::CommandObjectSP &command_obj_sp, llvm::StringRef args_string=llvm::StringRef())
int GetCommandNamesMatchingPartialString(const char *cmd_cstr, bool include_aliases, StringList &matches, StringList &descriptions)
void FindCommandsForApropos(llvm::StringRef word, StringList &commands_found, StringList &commands_help, bool search_builtin_commands, bool search_user_commands, bool search_alias_commands, bool search_user_mw_commands)
CommandObject * GetCommandObject(llvm::StringRef cmd, StringList *matches=nullptr, StringList *descriptions=nullptr) const
std::atomic< CommandHandlingState > m_command_state
Status PreprocessCommand(std::string &command)
CommandObject::CommandMap m_alias_dict
CommandObject * ResolveCommandImpl(std::string &command_line, CommandReturnObject &result)
CommandObject::CommandMap m_command_dict
ChildrenOmissionWarningStatus m_truncation_warning
Whether we truncated a value's list of children and whether the user has been told.
void HandleCompletion(CompletionRequest &request)
CommandInterpreterRunResult m_result
@ eCommandTypesBuiltin
native commands such as "frame"
@ eCommandTypesHidden
commands prefixed with an underscore
@ eCommandTypesUserMW
multiword commands (command containers)
@ eCommandTypesAliases
aliases such as "po"
ChildrenOmissionWarningStatus m_max_depth_warning
Whether we reached the maximum child nesting depth and whether the user has been told.
void ResolveCommand(const char *command_line, CommandReturnObject &result)
bool SetQuitExitCode(int exit_code)
Sets the exit code for the quit command.
CommandInterpreterRunResult RunCommandInterpreter(CommandInterpreterRunOptions &options)
bool HandleCommand(const char *command_line, LazyBool add_to_history, const ExecutionContext &override_context, CommandReturnObject &result)
CommandObject::CommandMap m_user_dict
ExecutionContext GetExecutionContext() const
const CommandObject::CommandMap & GetUserCommands() const
CommandObject * GetUserCommandObject(llvm::StringRef cmd, StringList *matches=nullptr, StringList *descriptions=nullptr) const
void SourceInitFileGlobal(CommandReturnObject &result)
bool GetAliasFullName(llvm::StringRef cmd, std::string &full_name) const
CommandObjectMultiword * VerifyUserMultiwordCmdPath(Args &path, bool leaf_is_command, Status &result)
Look up the command pointed to by path encoded in the arguments of the incoming command object.
void HandleCompletionMatches(CompletionRequest &request)
Status AddUserCommand(llvm::StringRef name, const lldb::CommandObjectSP &cmd_sp, bool can_replace)
bool AddCommand(llvm::StringRef name, const lldb::CommandObjectSP &cmd_sp, bool can_replace)
void PrintCommandOutput(IOHandler &io_handler, llvm::StringRef str, bool is_stdout)
bool AliasExists(llvm::StringRef cmd) const
Determine whether an alias command with this name exists.
int GetOptionArgumentPosition(const char *in_string)
Picks the number out of a string of the form "%NNN", otherwise return 0.
void GetHelp(CommandReturnObject &result, uint32_t types=eCommandTypesAllThem)
bool CommandExists(llvm::StringRef cmd) const
Determine whether a root level, built-in command with this name exists.
bool SaveTranscript(CommandReturnObject &result, std::optional< std::string > output_file=std::nullopt)
Save the current debugger session transcript to a file on disk.
int GetQuitExitCode(bool &exited) const
Returns the exit code that the user has specified when running the 'quit' command.
lldb::PlatformSP GetPlatform(bool prefer_target_platform)
void GetPythonCommandsFromIOHandler(const char *prompt, IOHandlerDelegate &delegate, void *baton=nullptr)
lldb::CommandObjectSP GetCommandSPExact(llvm::StringRef cmd, bool include_aliases=false) const
CommandInterpreter(Debugger &debugger, bool synchronous_execution)
const CommandAlias * GetAlias(llvm::StringRef alias_name) const
bool RemoveUser(llvm::StringRef alias_name)
void GetLLDBCommandsFromIOHandler(const char *prompt, IOHandlerDelegate &delegate, void *baton=nullptr)
lldb::CommandObjectSP GetCommandSP(llvm::StringRef cmd, bool include_aliases=true, bool exact=true, StringList *matches=nullptr, StringList *descriptions=nullptr) const
void OutputHelpText(Stream &stream, llvm::StringRef command_word, llvm::StringRef separator, llvm::StringRef help_text, uint32_t max_word_len)
CommandObject * GetCommandObjectForCommand(llvm::StringRef &command_line)
Status PreprocessToken(std::string &token)
bool RemoveUserMultiword(llvm::StringRef multiword_name)
@ eNoOmission
No children were omitted.
bool UserCommandExists(llvm::StringRef cmd) const
Determine whether a root-level user command with this name exists.
lldb::IOHandlerSP GetIOHandler(bool force_create=false, CommandInterpreterRunOptions *options=nullptr)
const CommandObject::CommandMap & GetUserMultiwordCommands() const
void BuildAliasCommandArgs(CommandObject *alias_cmd_obj, const char *alias_name, Args &cmd_args, std::string &raw_input_string, CommandReturnObject &result)
std::vector< uint32_t > m_command_source_flags
CommandObject * BuildAliasResult(llvm::StringRef alias_name, std::string &raw_input_string, std::string &alias_result, CommandReturnObject &result)
void OverrideExecutionContext(const ExecutionContext &override_context)
const char * ProcessEmbeddedScriptCommands(const char *arg)
void AllowExitCodeOnQuit(bool allow)
Specify if the command interpreter should allow that the user can specify a custom exit code when cal...
CommandObject::CommandMap m_user_mw_dict
StreamString m_transcript_stream
Turn on settings interpreter.save-transcript for LLDB to populate this stream.
void SetPrintCallback(CommandReturnObjectCallback callback)
void HandleCommandsFromFile(FileSpec &file, const ExecutionContext &context, const CommandInterpreterRunOptions &options, CommandReturnObject &result)
Execute a list of commands from a file.
void SourceInitFileCwd(CommandReturnObject &result)
std::vector< FileSpec > m_command_source_dirs
A stack of directory paths.
void HandleCommands(const StringList &commands, const ExecutionContext &context, const CommandInterpreterRunOptions &options, CommandReturnObject &result)
Execute a list of commands in sequence.
StructuredData::Array m_transcript
Contains a list of handled commands and their details.
bool RemoveCommand(llvm::StringRef cmd, bool force=false)
Remove a command if it is removable (python or regex command).
const StructuredData::Array & GetTranscript() const
const CommandObject::CommandMap & GetAliases() const
Implements dwim-print, a printing command that chooses the most direct, efficient,...
CommandObject::CommandMap & GetSubcommandDictionary()
lldb::CommandObjectSP GetSubcommandSPExact(llvm::StringRef sub_cmd) override
CommandObjectMultiword * GetAsMultiwordCommand() override
virtual bool WantsRawCommandString()=0
void SetOriginalCommandString(std::string s)
Set the command input as it appeared in the terminal.
llvm::StringRef GetCommandName() const
bool HelpTextContainsWord(llvm::StringRef search_word, bool search_short_help=true, bool search_long_help=true, bool search_syntax=true, bool search_options=true)
virtual void Execute(const char *args_string, CommandReturnObject &result)=0
std::map< std::string, lldb::CommandObjectSP, std::less<> > CommandMap
virtual std::optional< std::string > GetRepeatCommand(Args &current_command_args, uint32_t index)
Get the command that appropriate for a "repeat" of the current command.
virtual CommandObject * GetSubcommandObject(llvm::StringRef sub_cmd, StringList *matches=nullptr)
virtual CommandObjectMultiword * GetAsMultiwordCommand()
virtual Options * GetOptions()
void SetSyntax(llvm::StringRef str)
virtual void HandleCompletion(CompletionRequest &request)
This default version handles calling option argument completions and then calls HandleArgumentComplet...
virtual llvm::StringRef GetHelp()
void AppendMessage(llvm::StringRef in_string)
void void AppendError(llvm::StringRef in_string)
std::string GetErrorString(bool with_diagnostics=true) const
Return the errors as a string.
llvm::StringRef GetOutputString() const
void SetStatus(lldb::ReturnStatus status)
void AppendErrorWithFormat(const char *format,...) __attribute__((format(printf
void AppendMessageWithFormat(const char *format,...) __attribute__((format(printf
void void AppendMessageWithFormatv(const char *format, Args &&...args)
void SetDiagnosticIndent(std::optional< uint16_t > indent)
lldb::StreamSP GetImmediateErrorStream() const
void SetCommand(std::string command)
lldb::StreamSP GetImmediateOutputStream() const
void void AppendWarning(llvm::StringRef in_string)
void AppendErrorWithFormatv(const char *format, Args &&...args)
"lldb/Utility/ArgCompletionRequest.h"
void AddCompletion(llvm::StringRef completion, llvm::StringRef description="", CompletionMode mode=CompletionMode::Normal)
Adds a possible completion string.
void AddCompletions(const StringList &completions)
Adds multiple possible completion strings.
void ShiftArguments()
Drops the first argument from the argument list.
void AppendEmptyArgument()
Adds an empty argument at the end of the argument list and moves the cursor to this new argument.
const char * AsCString(const char *value_if_empty=nullptr) const
Get the string value as a C string.
void SetAsyncExecution(bool async)
bool GetShowInlineDiagnostics() const
Definition Debugger.cpp:771
lldb::FileSP GetOutputFileSP()
Definition Debugger.h:165
const char * GetIOHandlerCommandPrefix()
bool GetUseColor() const
Definition Debugger.cpp:524
void RunIOHandlerAsync(const lldb::IOHandlerSP &reader_sp, bool cancel_top_handler=true)
Run the given IO handler and return immediately.
void RunIOHandlerSync(const lldb::IOHandlerSP &reader_sp)
Run the given IO handler and block until it's complete.
llvm::StringRef GetPrompt() const
Definition Debugger.cpp:397
A class that measures elapsed time in an exception safe way.
Definition Statistics.h:76
void SetUnwindOnError(bool unwind=false)
Definition Target.h:374
void SetKeepInMemory(bool keep=true)
Definition Target.h:384
void SetCoerceToId(bool coerce=true)
Definition Target.h:370
void SetTryAllThreads(bool try_others=true)
Definition Target.h:407
void SetTimeout(const Timeout< std::micro > &timeout)
Definition Target.h:395
void SetIgnoreBreakpoints(bool ignore=false)
Definition Target.h:378
"lldb/Target/ExecutionContext.h" A class that contains an execution context.
StackFrame * GetFramePtr() const
Returns a pointer to the frame object.
const lldb::StackFrameSP & GetFrameSP() const
Get accessor to get the frame shared pointer.
Target * GetTargetPtr() const
Returns a pointer to the target object.
bool HasTargetScope() const
Returns true the ExecutionContext object contains a valid target.
Process * GetProcessPtr() const
Returns a pointer to the process object.
A file utility class.
Definition FileSpec.h:57
FileSpec CopyByAppendingPathComponent(llvm::StringRef component) const
Definition FileSpec.cpp:425
void AppendPathComponent(llvm::StringRef component)
Definition FileSpec.cpp:454
const ConstString & GetFilename() const
Filename string const get accessor.
Definition FileSpec.h:250
void MakeAbsolute(const FileSpec &dir)
Make the FileSpec absolute by treating it relative to dir.
Definition FileSpec.cpp:537
size_t GetPath(char *path, size_t max_path_length, bool denormalize=true) const
Extract the full path to the file.
Definition FileSpec.cpp:374
FileSpec CopyByRemovingLastPathComponent() const
Definition FileSpec.cpp:431
int Open(const char *path, int flags, int mode=0600)
Wraps open in a platform-independent way.
static FileSystem & Instance()
void Resolve(llvm::SmallVectorImpl< char > &path, bool force_make_absolute=false)
Resolve path to make it canonical.
@ eOpenOptionReadOnly
Definition File.h:51
@ eOpenOptionWriteOnly
Definition File.h:52
@ eOpenOptionCanCreate
Definition File.h:56
@ eOpenOptionTruncate
Definition File.h:57
A class to manage flags.
Definition Flags.h:22
bool Test(ValueType bit) const
Test a single flag bit.
Definition Flags.h:96
static lldb::thread_t GetCurrentThread()
Get the thread token (the one returned by ThreadCreate when the thread was created) for the calling t...
static bool IsInteractiveGraphicSession()
Check if we're running in an interactive graphical session.
static llvm::Error OpenFileInExternalEditor(llvm::StringRef editor, const FileSpec &file_spec, uint32_t line_no)
IOHandlerDelegate(Completion completion=Completion::None)
Definition IOHandler.h:188
virtual const char * GetPrompt()
Definition IOHandler.h:95
bool GetIsInteractive()
Check if the input is being supplied interactively by a user.
Definition IOHandler.cpp:97
lldb::LockableStreamFileSP GetErrorStreamFileSP()
Definition IOHandler.cpp:95
lldb::LockableStreamFileSP GetOutputStreamFileSP()
Definition IOHandler.cpp:93
void SetIsDone(bool b)
Definition IOHandler.h:81
static LanguageSet GetLanguagesSupportingREPLs()
Definition Language.cpp:475
static Language * FindPlugin(lldb::LanguageType language)
Definition Language.cpp:84
static const char * GetNameForLanguageType(lldb::LanguageType language)
Returns the internal LLDB name for the specified language.
Definition Language.cpp:305
static lldb::LanguageType GetPrimaryLanguage(lldb::LanguageType language)
Definition Language.cpp:408
A command line option parsing protocol class.
Definition Options.h:58
bool SupportsLongOption(const char *long_option)
Definition Options.cpp:283
A plug-in interface definition class for debugging a process.
Definition Process.h:354
lldb::StateType GetState()
Get accessor for the current process state.
Definition Process.cpp:1252
Status Halt(bool clear_thread_plans=false, bool use_run_lock=true)
Halts a running process.
Definition Process.cpp:3409
lldb::OptionValuePropertiesSP m_collection_sp
T GetPropertyAtIndexAs(uint32_t idx, T default_value, const ExecutionContext *exe_ctx=nullptr) const
bool SetPropertyAtIndex(uint32_t idx, T t, const ExecutionContext *exe_ctx=nullptr) const
void GetValue(Stream &s, bool show_type) const
Definition Scalar.cpp:186
An error handling class.
Definition Status.h:118
void Clear()
Clear the object state.
Definition Status.cpp:214
static Status FromErrorStringWithFormat(const char *format,...) __attribute__((format(printf
Definition Status.cpp:106
static Status FromErrorString(const char *str)
Definition Status.h:141
static Status static Status FromErrorStringWithFormatv(const char *format, Args &&...args)
Definition Status.h:151
void Flush() override
Flush the stream.
const char * GetData() const
llvm::StringRef GetString() const
A stream class that can stream formatted output to a file.
Definition Stream.h:28
size_t Write(const void *src, size_t src_len)
Output character bytes to the stream.
Definition Stream.h:110
size_t Indent(llvm::StringRef s="")
Indent the current line in the stream.
Definition Stream.cpp:155
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
Definition Stream.cpp:132
size_t PutCString(llvm::StringRef cstr)
Output a C string to the stream.
Definition Stream.cpp:63
size_t PutChar(char ch)
Definition Stream.cpp:129
size_t EOL()
Output and End of Line character to the stream.
Definition Stream.cpp:153
void IndentLess(unsigned amount=2)
Decrement the current indentation level.
Definition Stream.cpp:202
void IndentMore(unsigned amount=2)
Increment the current indentation level.
Definition Stream.cpp:199
void AppendList(const char **strv, int strc)
void AppendString(const std::string &s)
const char * GetStringAtIndex(size_t idx) const
void DeleteStringAtIndex(size_t id)
std::shared_ptr< Dictionary > DictionarySP
LoadCWDlldbinitFile GetLoadCWDlldbinitFile() const
Definition Target.cpp:5108
static TargetProperties & GetGlobalProperties()
Definition Target.cpp:3283
lldb::PlatformSP GetPlatform()
Definition Target.h:1678
lldb::ExpressionResults EvaluateExpression(llvm::StringRef expression, ExecutionContextScope *exe_scope, lldb::ValueObjectSP &result_valobj_sp, const EvaluateExpressionOptions &options=EvaluateExpressionOptions(), std::string *fixed_expression=nullptr, ValueObject *ctx_obj=nullptr)
Definition Target.cpp:2836
Represents UUID's of various sizes.
Definition UUID.h:27
static TelemetryManager * GetInstance()
A class that represents a running process on the host machine.
Log * GetLog(Cat mask)
Retrieve the Log object for the channel associated with the given log enum.
Definition Log.h:332
size_t FindLongestCommandWord(std::map< std::string, ValueType, std::less<> > &dict)
std::shared_ptr< OptionArgVector > OptionArgVectorSP
Definition Options.h:30
bool StateIsRunningState(lldb::StateType state)
Check if a state represents a state where the process or thread is running.
Definition State.cpp:68
int AddNamesMatchingPartialString(const std::map< std::string, ValueType, std::less<> > &in_map, llvm::StringRef cmd_str, StringList &matches, StringList *descriptions=nullptr)
@ RewriteLine
The full line has been rewritten by the completion.
LoadCWDlldbinitFile
Definition Target.h:63
@ eLoadCWDlldbinitTrue
Definition Target.h:64
@ eLoadCWDlldbinitFalse
Definition Target.h:65
@ eLoadCWDlldbinitWarn
Definition Target.h:66
std::string toString(FormatterBytecode::OpCodes op)
std::vector< std::tuple< std::string, int, std::string > > OptionArgVector
Definition Options.h:29
@ eSourceFileCompletion
std::shared_ptr< lldb_private::IOHandler > IOHandlerSP
std::shared_ptr< lldb_private::CommandObject > CommandObjectSP
std::shared_ptr< lldb_private::ValueObject > ValueObjectSP
std::unique_ptr< lldb_private::File > FileUP
@ eCommandInterpreterResultInferiorCrash
Stopped because the corresponding option was set and the inferior crashed.
@ eCommandInterpreterResultSuccess
Command interpreter finished successfully.
@ eCommandInterpreterResultCommandError
Stopped because the corresponding option was set and a command returned an error.
@ eCommandInterpreterResultQuitRequested
Stopped because quit was requested.
std::shared_ptr< lldb_private::UnixSignals > UnixSignalsSP
std::shared_ptr< lldb_private::Platform > PlatformSP
StateType
Process and Thread States.
@ eStateStopped
Process or thread is stopped and can be examined.
LanguageType
Programming language type.
@ eLanguageTypeUnknown
Unknown or invalid language value.
ExpressionResults
The results of expression evaluation.
@ eExpressionCompleted
std::shared_ptr< lldb_private::Process > ProcessSP
std::shared_ptr< lldb_private::Event > EventSP
@ eReturnStatusStarted
@ eReturnStatusSuccessContinuingResult
@ eReturnStatusFailed
@ eReturnStatusSuccessContinuingNoResult
@ eReturnStatusSuccessFinishResult
@ eReturnStatusInvalid
@ eReturnStatusSuccessFinishNoResult
std::shared_ptr< lldb_private::LockableStreamFile > LockableStreamFileSP
std::shared_ptr< lldb_private::StopInfo > StopInfoSP
StopReason
Thread stop reasons.
@ eStopReasonInstrumentation
@ eStopReasonHistoryBoundary
@ eStopReasonInterrupt
Thread requested interrupt.
@ eStopReasonProcessorTrace
@ eStopReasonException
std::shared_ptr< lldb_private::Target > TargetSP
std::shared_ptr< lldb_private::File > FileSP
@ eCommandReturnObjectPrintCallbackSkipped
The callback deferred printing the command return object.
const char * c_str() const
Definition Args.h:51
llvm::StringRef ref() const
Definition Args.h:50
char GetQuoteChar() const
Definition Args.h:55
A SmallBitVector that represents a set of source languages (lldb::LanguageType).
Definition Type.h:38
std::optional< lldb::LanguageType > GetSingularLanguage()
If the set contains a single language only, return it.
std::optional< std::string > original_command
These two fields are not collected by default due to PII risks.
Definition Telemetry.h:135
std::string command_name
The command name(eg., "breakpoint set")
Definition Telemetry.h:130
std::optional< lldb::ReturnStatus > ret_status
Return status of a command and any error description in case of error.
Definition Telemetry.h:139
UUID target_uuid
If the command is/can be associated with a target entry this field contains that target's UUID.
Definition Telemetry.h:122
std::optional< std::string > args
Definition Telemetry.h:136
uint64_t command_id
A unique ID for a command so the manager can match the start entry with its end entry.
Definition Telemetry.h:128
std::optional< std::string > error_data
Definition Telemetry.h:140
Helper RAII class for collecting telemetry.
Definition Telemetry.h:269
void DispatchOnExit(llvm::unique_function< void(Info *info)> final_callback)
Definition Telemetry.h:287
void DispatchNow(llvm::unique_function< void(Info *info)> populate_fields_cb)
Definition Telemetry.h:293