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