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