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