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