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