Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1 | //===-- CommandInterpreter.cpp ----------------------------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #include <string> |
Caroline Tice | bd5c63e | 2010-10-12 21:57:09 +0000 | [diff] [blame] | 11 | #include <vector> |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 12 | |
| 13 | #include <getopt.h> |
| 14 | #include <stdlib.h> |
| 15 | |
Greg Clayton | 5c28dd1 | 2011-06-23 17:59:56 +0000 | [diff] [blame] | 16 | #include "CommandObjectScript.h" |
Peter Collingbourne | 921fac0 | 2011-06-23 20:37:26 +0000 | [diff] [blame] | 17 | #include "lldb/Interpreter/CommandObjectRegexCommand.h" |
Greg Clayton | 5c28dd1 | 2011-06-23 17:59:56 +0000 | [diff] [blame] | 18 | |
Eli Friedman | ccdb9ec | 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 | ccdb9ec | 2010-06-13 02:17:17 +0000 | [diff] [blame] | 22 | #include "../Commands/CommandObjectDisassemble.h" |
| 23 | #include "../Commands/CommandObjectExpression.h" |
Eli Friedman | ccdb9ec | 2010-06-13 02:17:17 +0000 | [diff] [blame] | 24 | #include "../Commands/CommandObjectFrame.h" |
| 25 | #include "../Commands/CommandObjectHelp.h" |
Eli Friedman | ccdb9ec | 2010-06-13 02:17:17 +0000 | [diff] [blame] | 26 | #include "../Commands/CommandObjectLog.h" |
| 27 | #include "../Commands/CommandObjectMemory.h" |
Greg Clayton | b1888f2 | 2011-03-19 01:12:21 +0000 | [diff] [blame] | 28 | #include "../Commands/CommandObjectPlatform.h" |
Eli Friedman | ccdb9ec | 2010-06-13 02:17:17 +0000 | [diff] [blame] | 29 | #include "../Commands/CommandObjectProcess.h" |
| 30 | #include "../Commands/CommandObjectQuit.h" |
Eli Friedman | ccdb9ec | 2010-06-13 02:17:17 +0000 | [diff] [blame] | 31 | #include "../Commands/CommandObjectRegister.h" |
Eli Friedman | ccdb9ec | 2010-06-13 02:17:17 +0000 | [diff] [blame] | 32 | #include "../Commands/CommandObjectSettings.h" |
Eli Friedman | ccdb9ec | 2010-06-13 02:17:17 +0000 | [diff] [blame] | 33 | #include "../Commands/CommandObjectSource.h" |
Jim Ingham | 767af88 | 2010-07-07 03:36:20 +0000 | [diff] [blame] | 34 | #include "../Commands/CommandObjectCommands.h" |
Eli Friedman | ccdb9ec | 2010-06-13 02:17:17 +0000 | [diff] [blame] | 35 | #include "../Commands/CommandObjectSyntax.h" |
| 36 | #include "../Commands/CommandObjectTarget.h" |
| 37 | #include "../Commands/CommandObjectThread.h" |
Greg Clayton | 5c28dd1 | 2011-06-23 17:59:56 +0000 | [diff] [blame] | 38 | #include "../Commands/CommandObjectType.h" |
Johnny Chen | 902e018 | 2010-12-23 20:21:44 +0000 | [diff] [blame] | 39 | #include "../Commands/CommandObjectVersion.h" |
Johnny Chen | 01acfa7 | 2011-09-22 18:04:58 +0000 | [diff] [blame] | 40 | #include "../Commands/CommandObjectWatchpoint.h" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 41 | |
Jim Ingham | 84cdc15 | 2010-06-15 19:49:27 +0000 | [diff] [blame] | 42 | #include "lldb/Interpreter/Args.h" |
Caroline Tice | 5ddbe21 | 2011-05-06 21:37:15 +0000 | [diff] [blame] | 43 | #include "lldb/Interpreter/Options.h" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 44 | #include "lldb/Core/Debugger.h" |
Jim Ingham | 5e16ef5 | 2010-10-04 19:49:29 +0000 | [diff] [blame] | 45 | #include "lldb/Core/InputReader.h" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 46 | #include "lldb/Core/Stream.h" |
| 47 | #include "lldb/Core/Timer.h" |
Greg Clayton | cd54803 | 2011-02-01 01:31:41 +0000 | [diff] [blame] | 48 | #include "lldb/Host/Host.h" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 49 | #include "lldb/Target/Process.h" |
| 50 | #include "lldb/Target/Thread.h" |
| 51 | #include "lldb/Target/TargetList.h" |
Greg Clayton | e98ac25 | 2010-11-10 04:57:04 +0000 | [diff] [blame] | 52 | #include "lldb/Utility/CleanUp.h" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 53 | |
| 54 | #include "lldb/Interpreter/CommandReturnObject.h" |
| 55 | #include "lldb/Interpreter/CommandInterpreter.h" |
Caroline Tice | 0aa2e55 | 2011-01-14 00:29:16 +0000 | [diff] [blame] | 56 | #include "lldb/Interpreter/ScriptInterpreterNone.h" |
| 57 | #include "lldb/Interpreter/ScriptInterpreterPython.h" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 58 | |
| 59 | using namespace lldb; |
| 60 | using namespace lldb_private; |
| 61 | |
| 62 | CommandInterpreter::CommandInterpreter |
| 63 | ( |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 64 | Debugger &debugger, |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 65 | ScriptLanguage script_language, |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 66 | bool synchronous_execution |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 67 | ) : |
Greg Clayton | 49ce682 | 2010-10-31 03:01:06 +0000 | [diff] [blame] | 68 | Broadcaster ("lldb.command-interpreter"), |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 69 | m_debugger (debugger), |
Greg Clayton | 887aa28 | 2010-10-11 01:05:37 +0000 | [diff] [blame] | 70 | m_synchronous_execution (synchronous_execution), |
Caroline Tice | 0aa2e55 | 2011-01-14 00:29:16 +0000 | [diff] [blame] | 71 | m_skip_lldbinit_files (false), |
Jim Ingham | 574c3d6 | 2011-08-12 23:34:31 +0000 | [diff] [blame] | 72 | m_skip_app_init_files (false), |
Jim Ingham | 949d5ac | 2011-02-18 00:54:25 +0000 | [diff] [blame] | 73 | m_script_interpreter_ap (), |
Caroline Tice | 892fadd | 2011-06-16 16:27:19 +0000 | [diff] [blame] | 74 | m_comment_char ('#'), |
Jim Ingham | 6247dbe | 2011-07-12 03:12:18 +0000 | [diff] [blame] | 75 | m_repeat_char ('!'), |
Enrico Granata | 293a7b3 | 2011-08-15 19:24:02 +0000 | [diff] [blame] | 76 | m_batch_command_mode (false), |
| 77 | m_truncation_warning(eNoTruncation) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 78 | { |
Caroline Tice | 6e4c5ce | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 79 | const char *dbg_name = debugger.GetInstanceName().AsCString(); |
| 80 | std::string lang_name = ScriptInterpreter::LanguageToString (script_language); |
| 81 | StreamString var_name; |
| 82 | var_name.Printf ("[%s].script-lang", dbg_name); |
Caroline Tice | 1d2aefd | 2010-09-09 06:25:08 +0000 | [diff] [blame] | 83 | debugger.GetSettingsController()->SetVariable (var_name.GetData(), lang_name.c_str(), |
Greg Clayton | b344843 | 2011-03-24 21:19:54 +0000 | [diff] [blame] | 84 | eVarSetOperationAssign, false, |
Greg Clayton | 49ce682 | 2010-10-31 03:01:06 +0000 | [diff] [blame] | 85 | m_debugger.GetInstanceName().AsCString()); |
| 86 | SetEventName (eBroadcastBitThreadShouldExit, "thread-should-exit"); |
| 87 | SetEventName (eBroadcastBitResetPrompt, "reset-prompt"); |
| 88 | SetEventName (eBroadcastBitQuitCommandReceived, "quit"); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | void |
| 92 | CommandInterpreter::Initialize () |
| 93 | { |
| 94 | Timer scoped_timer (__PRETTY_FUNCTION__, __PRETTY_FUNCTION__); |
| 95 | |
| 96 | CommandReturnObject result; |
| 97 | |
| 98 | LoadCommandDictionary (); |
| 99 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 100 | // Set up some initial aliases. |
Caroline Tice | 5ddbe21 | 2011-05-06 21:37:15 +0000 | [diff] [blame] | 101 | CommandObjectSP cmd_obj_sp = GetCommandSPExact ("quit", false); |
| 102 | if (cmd_obj_sp) |
| 103 | { |
| 104 | AddAlias ("q", cmd_obj_sp); |
| 105 | AddAlias ("exit", cmd_obj_sp); |
| 106 | } |
| 107 | |
| 108 | cmd_obj_sp = GetCommandSPExact ("process continue", false); |
| 109 | if (cmd_obj_sp) |
| 110 | { |
| 111 | AddAlias ("c", cmd_obj_sp); |
| 112 | AddAlias ("continue", cmd_obj_sp); |
| 113 | } |
| 114 | |
| 115 | cmd_obj_sp = GetCommandSPExact ("_regexp-break",false); |
| 116 | if (cmd_obj_sp) |
| 117 | AddAlias ("b", cmd_obj_sp); |
| 118 | |
| 119 | cmd_obj_sp = GetCommandSPExact ("thread backtrace", false); |
| 120 | if (cmd_obj_sp) |
| 121 | AddAlias ("bt", cmd_obj_sp); |
| 122 | |
| 123 | cmd_obj_sp = GetCommandSPExact ("thread step-inst", false); |
| 124 | if (cmd_obj_sp) |
Jason Molenda | 47eb00e | 2011-10-22 00:47:41 +0000 | [diff] [blame] | 125 | { |
| 126 | AddAlias ("stepi", cmd_obj_sp); |
Caroline Tice | 5ddbe21 | 2011-05-06 21:37:15 +0000 | [diff] [blame] | 127 | AddAlias ("si", cmd_obj_sp); |
Jason Molenda | 47eb00e | 2011-10-22 00:47:41 +0000 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | cmd_obj_sp = GetCommandSPExact ("thread step-inst-over", false); |
| 131 | if (cmd_obj_sp) |
| 132 | { |
| 133 | AddAlias ("nexti", cmd_obj_sp); |
| 134 | AddAlias ("ni", cmd_obj_sp); |
| 135 | } |
Caroline Tice | 5ddbe21 | 2011-05-06 21:37:15 +0000 | [diff] [blame] | 136 | |
| 137 | cmd_obj_sp = GetCommandSPExact ("thread step-in", false); |
| 138 | if (cmd_obj_sp) |
| 139 | { |
| 140 | AddAlias ("s", cmd_obj_sp); |
| 141 | AddAlias ("step", cmd_obj_sp); |
| 142 | } |
| 143 | |
| 144 | cmd_obj_sp = GetCommandSPExact ("thread step-over", false); |
| 145 | if (cmd_obj_sp) |
| 146 | { |
| 147 | AddAlias ("n", cmd_obj_sp); |
| 148 | AddAlias ("next", cmd_obj_sp); |
| 149 | } |
| 150 | |
| 151 | cmd_obj_sp = GetCommandSPExact ("thread step-out", false); |
| 152 | if (cmd_obj_sp) |
| 153 | { |
| 154 | AddAlias ("f", cmd_obj_sp); |
| 155 | AddAlias ("finish", cmd_obj_sp); |
| 156 | } |
| 157 | |
| 158 | cmd_obj_sp = GetCommandSPExact ("source list", false); |
| 159 | if (cmd_obj_sp) |
| 160 | { |
| 161 | AddAlias ("l", cmd_obj_sp); |
| 162 | AddAlias ("list", cmd_obj_sp); |
| 163 | } |
| 164 | |
| 165 | cmd_obj_sp = GetCommandSPExact ("memory read", false); |
| 166 | if (cmd_obj_sp) |
| 167 | AddAlias ("x", cmd_obj_sp); |
| 168 | |
| 169 | cmd_obj_sp = GetCommandSPExact ("_regexp-up", false); |
| 170 | if (cmd_obj_sp) |
| 171 | AddAlias ("up", cmd_obj_sp); |
| 172 | |
| 173 | cmd_obj_sp = GetCommandSPExact ("_regexp-down", false); |
| 174 | if (cmd_obj_sp) |
| 175 | AddAlias ("down", cmd_obj_sp); |
| 176 | |
Jason Molenda | 3f2ec9b | 2011-10-25 02:11:20 +0000 | [diff] [blame] | 177 | cmd_obj_sp = GetCommandSPExact ("_regexp-display", false); |
Jason Molenda | 730cae0 | 2011-10-22 01:30:52 +0000 | [diff] [blame] | 178 | if (cmd_obj_sp) |
| 179 | AddAlias ("display", cmd_obj_sp); |
Jim Ingham | 9d1acc1 | 2011-10-24 18:37:00 +0000 | [diff] [blame] | 180 | |
| 181 | cmd_obj_sp = GetCommandSPExact ("disassemble", false); |
| 182 | if (cmd_obj_sp) |
| 183 | AddAlias ("dis", cmd_obj_sp); |
| 184 | |
| 185 | cmd_obj_sp = GetCommandSPExact ("disassemble", false); |
| 186 | if (cmd_obj_sp) |
| 187 | AddAlias ("di", cmd_obj_sp); |
| 188 | |
| 189 | |
Jason Molenda | 730cae0 | 2011-10-22 01:30:52 +0000 | [diff] [blame] | 190 | |
Jason Molenda | 3f2ec9b | 2011-10-25 02:11:20 +0000 | [diff] [blame] | 191 | cmd_obj_sp = GetCommandSPExact ("_regexp-undisplay", false); |
Jason Molenda | 730cae0 | 2011-10-22 01:30:52 +0000 | [diff] [blame] | 192 | if (cmd_obj_sp) |
| 193 | AddAlias ("undisplay", cmd_obj_sp); |
| 194 | |
Caroline Tice | 5ddbe21 | 2011-05-06 21:37:15 +0000 | [diff] [blame] | 195 | cmd_obj_sp = GetCommandSPExact ("target create", false); |
| 196 | if (cmd_obj_sp) |
| 197 | AddAlias ("file", cmd_obj_sp); |
| 198 | |
| 199 | cmd_obj_sp = GetCommandSPExact ("target modules", false); |
| 200 | if (cmd_obj_sp) |
| 201 | AddAlias ("image", cmd_obj_sp); |
| 202 | |
| 203 | |
| 204 | OptionArgVectorSP alias_arguments_vector_sp (new OptionArgVector); |
Jim Ingham | e56493f | 2011-03-22 02:29:32 +0000 | [diff] [blame] | 205 | |
Caroline Tice | 5ddbe21 | 2011-05-06 21:37:15 +0000 | [diff] [blame] | 206 | cmd_obj_sp = GetCommandSPExact ("expression", false); |
| 207 | if (cmd_obj_sp) |
| 208 | { |
| 209 | AddAlias ("expr", cmd_obj_sp); |
| 210 | |
| 211 | ProcessAliasOptionsArgs (cmd_obj_sp, "--", alias_arguments_vector_sp); |
| 212 | AddAlias ("p", cmd_obj_sp); |
| 213 | AddAlias ("print", cmd_obj_sp); |
| 214 | AddOrReplaceAliasOptions ("p", alias_arguments_vector_sp); |
| 215 | AddOrReplaceAliasOptions ("print", alias_arguments_vector_sp); |
| 216 | |
| 217 | alias_arguments_vector_sp.reset (new OptionArgVector); |
| 218 | ProcessAliasOptionsArgs (cmd_obj_sp, "-o --", alias_arguments_vector_sp); |
| 219 | AddAlias ("po", cmd_obj_sp); |
| 220 | AddOrReplaceAliasOptions ("po", alias_arguments_vector_sp); |
| 221 | } |
| 222 | |
| 223 | cmd_obj_sp = GetCommandSPExact ("process launch", false); |
| 224 | if (cmd_obj_sp) |
| 225 | { |
| 226 | alias_arguments_vector_sp.reset (new OptionArgVector); |
| 227 | ProcessAliasOptionsArgs (cmd_obj_sp, "--", alias_arguments_vector_sp); |
| 228 | AddAlias ("r", cmd_obj_sp); |
| 229 | AddAlias ("run", cmd_obj_sp); |
| 230 | AddOrReplaceAliasOptions ("r", alias_arguments_vector_sp); |
| 231 | AddOrReplaceAliasOptions ("run", alias_arguments_vector_sp); |
| 232 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 233 | } |
| 234 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 235 | const char * |
| 236 | CommandInterpreter::ProcessEmbeddedScriptCommands (const char *arg) |
| 237 | { |
| 238 | // This function has not yet been implemented. |
| 239 | |
| 240 | // Look for any embedded script command |
| 241 | // If found, |
| 242 | // get interpreter object from the command dictionary, |
| 243 | // call execute_one_command on it, |
| 244 | // get the results as a string, |
| 245 | // substitute that string for current stuff. |
| 246 | |
| 247 | return arg; |
| 248 | } |
| 249 | |
| 250 | |
| 251 | void |
| 252 | CommandInterpreter::LoadCommandDictionary () |
| 253 | { |
| 254 | Timer scoped_timer (__PRETTY_FUNCTION__, __PRETTY_FUNCTION__); |
| 255 | |
| 256 | // **** IMPORTANT **** IMPORTANT *** IMPORTANT *** **** IMPORTANT **** IMPORTANT *** IMPORTANT *** |
| 257 | // |
| 258 | // Command objects that are used as cross reference objects (i.e. they inherit from CommandObjectCrossref) |
| 259 | // *MUST* be created and put into the command dictionary *BEFORE* any multi-word commands (which may use |
| 260 | // the cross-referencing stuff) are created!!! |
| 261 | // |
| 262 | // **** IMPORTANT **** IMPORTANT *** IMPORTANT *** **** IMPORTANT **** IMPORTANT *** IMPORTANT *** |
| 263 | |
| 264 | |
| 265 | // Command objects that inherit from CommandObjectCrossref must be created before other command objects |
| 266 | // are created. This is so that when another command is created that needs to go into a crossref object, |
| 267 | // the crossref object exists and is ready to take the cross reference. Put the cross referencing command |
| 268 | // objects into the CommandDictionary now, so they are ready for use when the other commands get created. |
| 269 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 270 | // Non-CommandObjectCrossref commands can now be created. |
| 271 | |
Caroline Tice | 5bc8c97 | 2010-09-20 20:44:43 +0000 | [diff] [blame] | 272 | lldb::ScriptLanguage script_language = m_debugger.GetScriptLanguage(); |
Caroline Tice | 6e4c5ce | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 273 | |
Greg Clayton | 238c0a1 | 2010-09-18 01:14:36 +0000 | [diff] [blame] | 274 | m_command_dict["apropos"] = CommandObjectSP (new CommandObjectApropos (*this)); |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 275 | m_command_dict["breakpoint"]= CommandObjectSP (new CommandObjectMultiwordBreakpoint (*this)); |
Greg Clayton | 238c0a1 | 2010-09-18 01:14:36 +0000 | [diff] [blame] | 276 | //m_command_dict["call"] = CommandObjectSP (new CommandObjectCall (*this)); |
Johnny Chen | 9e4c3d7 | 2011-04-21 00:39:18 +0000 | [diff] [blame] | 277 | m_command_dict["command"] = CommandObjectSP (new CommandObjectMultiwordCommands (*this)); |
Greg Clayton | 238c0a1 | 2010-09-18 01:14:36 +0000 | [diff] [blame] | 278 | m_command_dict["disassemble"] = CommandObjectSP (new CommandObjectDisassemble (*this)); |
| 279 | m_command_dict["expression"]= CommandObjectSP (new CommandObjectExpression (*this)); |
Greg Clayton | abe0fed | 2011-04-18 08:33:37 +0000 | [diff] [blame] | 280 | // m_command_dict["file"] = CommandObjectSP (new CommandObjectFile (*this)); |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 281 | m_command_dict["frame"] = CommandObjectSP (new CommandObjectMultiwordFrame (*this)); |
Greg Clayton | 238c0a1 | 2010-09-18 01:14:36 +0000 | [diff] [blame] | 282 | m_command_dict["help"] = CommandObjectSP (new CommandObjectHelp (*this)); |
Greg Clayton | e1f50b9 | 2011-05-03 22:09:39 +0000 | [diff] [blame] | 283 | /// m_command_dict["image"] = CommandObjectSP (new CommandObjectImage (*this)); |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 284 | m_command_dict["log"] = CommandObjectSP (new CommandObjectLog (*this)); |
| 285 | m_command_dict["memory"] = CommandObjectSP (new CommandObjectMemory (*this)); |
Greg Clayton | b1888f2 | 2011-03-19 01:12:21 +0000 | [diff] [blame] | 286 | m_command_dict["platform"] = CommandObjectSP (new CommandObjectPlatform (*this)); |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 287 | m_command_dict["process"] = CommandObjectSP (new CommandObjectMultiwordProcess (*this)); |
Greg Clayton | 238c0a1 | 2010-09-18 01:14:36 +0000 | [diff] [blame] | 288 | m_command_dict["quit"] = CommandObjectSP (new CommandObjectQuit (*this)); |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 289 | m_command_dict["register"] = CommandObjectSP (new CommandObjectRegister (*this)); |
Greg Clayton | 238c0a1 | 2010-09-18 01:14:36 +0000 | [diff] [blame] | 290 | m_command_dict["script"] = CommandObjectSP (new CommandObjectScript (*this, script_language)); |
Caroline Tice | 6e4c5ce | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 291 | m_command_dict["settings"] = CommandObjectSP (new CommandObjectMultiwordSettings (*this)); |
Jim Ingham | 767af88 | 2010-07-07 03:36:20 +0000 | [diff] [blame] | 292 | m_command_dict["source"] = CommandObjectSP (new CommandObjectMultiwordSource (*this)); |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 293 | m_command_dict["target"] = CommandObjectSP (new CommandObjectMultiwordTarget (*this)); |
| 294 | m_command_dict["thread"] = CommandObjectSP (new CommandObjectMultiwordThread (*this)); |
Enrico Granata | 6b1596d | 2011-08-16 23:24:13 +0000 | [diff] [blame] | 295 | m_command_dict["type"] = CommandObjectSP (new CommandObjectType (*this)); |
Johnny Chen | 902e018 | 2010-12-23 20:21:44 +0000 | [diff] [blame] | 296 | m_command_dict["version"] = CommandObjectSP (new CommandObjectVersion (*this)); |
Johnny Chen | 01acfa7 | 2011-09-22 18:04:58 +0000 | [diff] [blame] | 297 | m_command_dict["watchpoint"]= CommandObjectSP (new CommandObjectMultiwordWatchpoint (*this)); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 298 | |
| 299 | std::auto_ptr<CommandObjectRegexCommand> |
Greg Clayton | 238c0a1 | 2010-09-18 01:14:36 +0000 | [diff] [blame] | 300 | break_regex_cmd_ap(new CommandObjectRegexCommand (*this, |
Greg Clayton | b72d0f0 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 301 | "_regexp-break", |
Caroline Tice | c1ad82e | 2010-09-07 22:38:08 +0000 | [diff] [blame] | 302 | "Set a breakpoint using a regular expression to specify the location.", |
Greg Clayton | b72d0f0 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 303 | "_regexp-break [<filename>:<linenum>]\n_regexp-break [<address>]\n_regexp-break <...>", 2)); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 304 | if (break_regex_cmd_ap.get()) |
| 305 | { |
| 306 | if (break_regex_cmd_ap->AddRegexCommand("^(.*[^[:space:]])[[:space:]]*:[[:space:]]*([[:digit:]]+)[[:space:]]*$", "breakpoint set --file '%1' --line %2") && |
| 307 | break_regex_cmd_ap->AddRegexCommand("^(0x[[:xdigit:]]+)[[:space:]]*$", "breakpoint set --address %1") && |
| 308 | break_regex_cmd_ap->AddRegexCommand("^[\"']?([-+]\\[.*\\])[\"']?[[:space:]]*$", "breakpoint set --name '%1'") && |
Greg Clayton | b72d0f0 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 309 | break_regex_cmd_ap->AddRegexCommand("^$", "breakpoint list --full") && |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 310 | break_regex_cmd_ap->AddRegexCommand("^(-.*)$", "breakpoint set %1") && |
Greg Clayton | b01000f | 2011-01-17 03:46:26 +0000 | [diff] [blame] | 311 | break_regex_cmd_ap->AddRegexCommand("^(.*[^[:space:]])`(.*[^[:space:]])[[:space:]]*$", "breakpoint set --name '%2' --shlib '%1'") && |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 312 | break_regex_cmd_ap->AddRegexCommand("^(.*[^[:space:]])[[:space:]]*$", "breakpoint set --name '%1'")) |
| 313 | { |
| 314 | CommandObjectSP break_regex_cmd_sp(break_regex_cmd_ap.release()); |
| 315 | m_command_dict[break_regex_cmd_sp->GetCommandName ()] = break_regex_cmd_sp; |
| 316 | } |
| 317 | } |
Jim Ingham | e56493f | 2011-03-22 02:29:32 +0000 | [diff] [blame] | 318 | |
| 319 | std::auto_ptr<CommandObjectRegexCommand> |
| 320 | down_regex_cmd_ap(new CommandObjectRegexCommand (*this, |
Greg Clayton | b72d0f0 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 321 | "_regexp-down", |
| 322 | "Go down \"n\" frames in the stack (1 frame by default).", |
| 323 | "_regexp-down [n]", 2)); |
Jim Ingham | e56493f | 2011-03-22 02:29:32 +0000 | [diff] [blame] | 324 | if (down_regex_cmd_ap.get()) |
| 325 | { |
| 326 | if (down_regex_cmd_ap->AddRegexCommand("^$", "frame select -r -1") && |
| 327 | down_regex_cmd_ap->AddRegexCommand("^([0-9]+)$", "frame select -r -%1")) |
| 328 | { |
| 329 | CommandObjectSP down_regex_cmd_sp(down_regex_cmd_ap.release()); |
| 330 | m_command_dict[down_regex_cmd_sp->GetCommandName ()] = down_regex_cmd_sp; |
| 331 | } |
| 332 | } |
| 333 | |
| 334 | std::auto_ptr<CommandObjectRegexCommand> |
| 335 | up_regex_cmd_ap(new CommandObjectRegexCommand (*this, |
Greg Clayton | b72d0f0 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 336 | "_regexp-up", |
| 337 | "Go up \"n\" frames in the stack (1 frame by default).", |
| 338 | "_regexp-up [n]", 2)); |
Jim Ingham | e56493f | 2011-03-22 02:29:32 +0000 | [diff] [blame] | 339 | if (up_regex_cmd_ap.get()) |
| 340 | { |
| 341 | if (up_regex_cmd_ap->AddRegexCommand("^$", "frame select -r 1") && |
| 342 | up_regex_cmd_ap->AddRegexCommand("^([0-9]+)$", "frame select -r %1")) |
| 343 | { |
| 344 | CommandObjectSP up_regex_cmd_sp(up_regex_cmd_ap.release()); |
| 345 | m_command_dict[up_regex_cmd_sp->GetCommandName ()] = up_regex_cmd_sp; |
| 346 | } |
| 347 | } |
Jason Molenda | 730cae0 | 2011-10-22 01:30:52 +0000 | [diff] [blame] | 348 | |
| 349 | std::auto_ptr<CommandObjectRegexCommand> |
| 350 | display_regex_cmd_ap(new CommandObjectRegexCommand (*this, |
Jason Molenda | 3f2ec9b | 2011-10-25 02:11:20 +0000 | [diff] [blame] | 351 | "_regexp-display", |
Jason Molenda | 730cae0 | 2011-10-22 01:30:52 +0000 | [diff] [blame] | 352 | "Add an expression evaluation stop-hook.", |
Jason Molenda | 3f2ec9b | 2011-10-25 02:11:20 +0000 | [diff] [blame] | 353 | "_regexp-display expression", 2)); |
Jason Molenda | 730cae0 | 2011-10-22 01:30:52 +0000 | [diff] [blame] | 354 | if (display_regex_cmd_ap.get()) |
| 355 | { |
| 356 | if (display_regex_cmd_ap->AddRegexCommand("^(.+)$", "target stop-hook add -o \"expr -- %1\"")) |
| 357 | { |
| 358 | CommandObjectSP display_regex_cmd_sp(display_regex_cmd_ap.release()); |
| 359 | m_command_dict[display_regex_cmd_sp->GetCommandName ()] = display_regex_cmd_sp; |
| 360 | } |
| 361 | } |
| 362 | |
| 363 | std::auto_ptr<CommandObjectRegexCommand> |
| 364 | undisplay_regex_cmd_ap(new CommandObjectRegexCommand (*this, |
Jason Molenda | 3f2ec9b | 2011-10-25 02:11:20 +0000 | [diff] [blame] | 365 | "_regexp-undisplay", |
Jason Molenda | 730cae0 | 2011-10-22 01:30:52 +0000 | [diff] [blame] | 366 | "Remove an expression evaluation stop-hook.", |
Jason Molenda | 3f2ec9b | 2011-10-25 02:11:20 +0000 | [diff] [blame] | 367 | "_regexp-undisplay stop-hook-number", 2)); |
Jason Molenda | 730cae0 | 2011-10-22 01:30:52 +0000 | [diff] [blame] | 368 | if (undisplay_regex_cmd_ap.get()) |
| 369 | { |
| 370 | if (undisplay_regex_cmd_ap->AddRegexCommand("^([0-9]+)$", "target stop-hook delete %1")) |
| 371 | { |
| 372 | CommandObjectSP undisplay_regex_cmd_sp(undisplay_regex_cmd_ap.release()); |
| 373 | m_command_dict[undisplay_regex_cmd_sp->GetCommandName ()] = undisplay_regex_cmd_sp; |
| 374 | } |
| 375 | } |
| 376 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 377 | } |
| 378 | |
| 379 | int |
| 380 | CommandInterpreter::GetCommandNamesMatchingPartialString (const char *cmd_str, bool include_aliases, |
| 381 | StringList &matches) |
| 382 | { |
| 383 | CommandObject::AddNamesMatchingPartialString (m_command_dict, cmd_str, matches); |
| 384 | |
| 385 | if (include_aliases) |
| 386 | { |
| 387 | CommandObject::AddNamesMatchingPartialString (m_alias_dict, cmd_str, matches); |
| 388 | } |
| 389 | |
| 390 | return matches.GetSize(); |
| 391 | } |
| 392 | |
| 393 | CommandObjectSP |
| 394 | CommandInterpreter::GetCommandSP (const char *cmd_cstr, bool include_aliases, bool exact, StringList *matches) |
| 395 | { |
| 396 | CommandObject::CommandMap::iterator pos; |
| 397 | CommandObjectSP ret_val; |
| 398 | |
| 399 | std::string cmd(cmd_cstr); |
| 400 | |
| 401 | if (HasCommands()) |
| 402 | { |
| 403 | pos = m_command_dict.find(cmd); |
| 404 | if (pos != m_command_dict.end()) |
| 405 | ret_val = pos->second; |
| 406 | } |
| 407 | |
| 408 | if (include_aliases && HasAliases()) |
| 409 | { |
| 410 | pos = m_alias_dict.find(cmd); |
| 411 | if (pos != m_alias_dict.end()) |
| 412 | ret_val = pos->second; |
| 413 | } |
| 414 | |
| 415 | if (HasUserCommands()) |
| 416 | { |
| 417 | pos = m_user_dict.find(cmd); |
| 418 | if (pos != m_user_dict.end()) |
| 419 | ret_val = pos->second; |
| 420 | } |
| 421 | |
| 422 | if (!exact && ret_val == NULL) |
| 423 | { |
Jim Ingham | d40f8a6 | 2010-07-06 22:46:59 +0000 | [diff] [blame] | 424 | // We will only get into here if we didn't find any exact matches. |
| 425 | |
| 426 | CommandObjectSP user_match_sp, alias_match_sp, real_match_sp; |
| 427 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 428 | StringList local_matches; |
| 429 | if (matches == NULL) |
| 430 | matches = &local_matches; |
| 431 | |
Jim Ingham | d40f8a6 | 2010-07-06 22:46:59 +0000 | [diff] [blame] | 432 | unsigned int num_cmd_matches = 0; |
| 433 | unsigned int num_alias_matches = 0; |
| 434 | unsigned int num_user_matches = 0; |
| 435 | |
| 436 | // Look through the command dictionaries one by one, and if we get only one match from any of |
| 437 | // them in toto, then return that, otherwise return an empty CommandObjectSP and the list of matches. |
| 438 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 439 | if (HasCommands()) |
| 440 | { |
| 441 | num_cmd_matches = CommandObject::AddNamesMatchingPartialString (m_command_dict, cmd_cstr, *matches); |
| 442 | } |
| 443 | |
| 444 | if (num_cmd_matches == 1) |
| 445 | { |
| 446 | cmd.assign(matches->GetStringAtIndex(0)); |
| 447 | pos = m_command_dict.find(cmd); |
| 448 | if (pos != m_command_dict.end()) |
Jim Ingham | d40f8a6 | 2010-07-06 22:46:59 +0000 | [diff] [blame] | 449 | real_match_sp = pos->second; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 450 | } |
| 451 | |
Jim Ingham | 9a57417 | 2010-06-24 20:28:42 +0000 | [diff] [blame] | 452 | if (include_aliases && HasAliases()) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 453 | { |
| 454 | num_alias_matches = CommandObject::AddNamesMatchingPartialString (m_alias_dict, cmd_cstr, *matches); |
| 455 | |
| 456 | } |
| 457 | |
Jim Ingham | d40f8a6 | 2010-07-06 22:46:59 +0000 | [diff] [blame] | 458 | if (num_alias_matches == 1) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 459 | { |
| 460 | cmd.assign(matches->GetStringAtIndex (num_cmd_matches)); |
| 461 | pos = m_alias_dict.find(cmd); |
| 462 | if (pos != m_alias_dict.end()) |
Jim Ingham | d40f8a6 | 2010-07-06 22:46:59 +0000 | [diff] [blame] | 463 | alias_match_sp = pos->second; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 464 | } |
| 465 | |
Jim Ingham | 9a57417 | 2010-06-24 20:28:42 +0000 | [diff] [blame] | 466 | if (HasUserCommands()) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 467 | { |
| 468 | num_user_matches = CommandObject::AddNamesMatchingPartialString (m_user_dict, cmd_cstr, *matches); |
| 469 | } |
| 470 | |
Jim Ingham | d40f8a6 | 2010-07-06 22:46:59 +0000 | [diff] [blame] | 471 | if (num_user_matches == 1) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 472 | { |
| 473 | cmd.assign (matches->GetStringAtIndex (num_cmd_matches + num_alias_matches)); |
| 474 | |
| 475 | pos = m_user_dict.find (cmd); |
| 476 | if (pos != m_user_dict.end()) |
Jim Ingham | d40f8a6 | 2010-07-06 22:46:59 +0000 | [diff] [blame] | 477 | user_match_sp = pos->second; |
| 478 | } |
| 479 | |
| 480 | // If we got exactly one match, return that, otherwise return the match list. |
| 481 | |
| 482 | if (num_user_matches + num_cmd_matches + num_alias_matches == 1) |
| 483 | { |
| 484 | if (num_cmd_matches) |
| 485 | return real_match_sp; |
| 486 | else if (num_alias_matches) |
| 487 | return alias_match_sp; |
| 488 | else |
| 489 | return user_match_sp; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 490 | } |
| 491 | } |
Jim Ingham | d40f8a6 | 2010-07-06 22:46:59 +0000 | [diff] [blame] | 492 | else if (matches && ret_val != NULL) |
| 493 | { |
| 494 | matches->AppendString (cmd_cstr); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 495 | } |
| 496 | |
| 497 | |
| 498 | return ret_val; |
| 499 | } |
| 500 | |
Greg Clayton | d12aeab | 2011-04-20 16:37:46 +0000 | [diff] [blame] | 501 | bool |
| 502 | CommandInterpreter::AddCommand (const char *name, const lldb::CommandObjectSP &cmd_sp, bool can_replace) |
| 503 | { |
| 504 | if (name && name[0]) |
| 505 | { |
| 506 | std::string name_sstr(name); |
| 507 | if (!can_replace) |
| 508 | { |
| 509 | if (m_command_dict.find (name_sstr) != m_command_dict.end()) |
| 510 | return false; |
| 511 | } |
| 512 | m_command_dict[name_sstr] = cmd_sp; |
| 513 | return true; |
| 514 | } |
| 515 | return false; |
| 516 | } |
| 517 | |
Enrico Granata | 6b1596d | 2011-08-16 23:24:13 +0000 | [diff] [blame] | 518 | bool |
| 519 | CommandInterpreter::AddUserCommand (const char *name, |
| 520 | const lldb::CommandObjectSP &cmd_sp, |
| 521 | bool can_replace) |
| 522 | { |
| 523 | if (name && name[0]) |
| 524 | { |
| 525 | std::string name_sstr(name); |
| 526 | if (!can_replace) |
| 527 | { |
| 528 | if (m_user_dict.find (name_sstr) != m_user_dict.end()) |
| 529 | return false; |
| 530 | } |
| 531 | m_user_dict[name_sstr] = cmd_sp; |
| 532 | return true; |
| 533 | } |
| 534 | return false; |
| 535 | } |
Greg Clayton | d12aeab | 2011-04-20 16:37:46 +0000 | [diff] [blame] | 536 | |
Jim Ingham | d40f8a6 | 2010-07-06 22:46:59 +0000 | [diff] [blame] | 537 | CommandObjectSP |
| 538 | CommandInterpreter::GetCommandSPExact (const char *cmd_cstr, bool include_aliases) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 539 | { |
Caroline Tice | 56d2fc4 | 2010-12-14 18:51:39 +0000 | [diff] [blame] | 540 | Args cmd_words (cmd_cstr); // Break up the command string into words, in case it's a multi-word command. |
| 541 | CommandObjectSP ret_val; // Possibly empty return value. |
| 542 | |
| 543 | if (cmd_cstr == NULL) |
| 544 | return ret_val; |
| 545 | |
| 546 | if (cmd_words.GetArgumentCount() == 1) |
| 547 | return GetCommandSP(cmd_cstr, include_aliases, true, NULL); |
| 548 | else |
| 549 | { |
| 550 | // We have a multi-word command (seemingly), so we need to do more work. |
| 551 | // First, get the cmd_obj_sp for the first word in the command. |
| 552 | CommandObjectSP cmd_obj_sp = GetCommandSP (cmd_words.GetArgumentAtIndex (0), include_aliases, true, NULL); |
| 553 | if (cmd_obj_sp.get() != NULL) |
| 554 | { |
| 555 | // Loop through the rest of the words in the command (everything passed in was supposed to be part of a |
| 556 | // command name), and find the appropriate sub-command SP for each command word.... |
| 557 | size_t end = cmd_words.GetArgumentCount(); |
| 558 | for (size_t j= 1; j < end; ++j) |
| 559 | { |
| 560 | if (cmd_obj_sp->IsMultiwordObject()) |
| 561 | { |
| 562 | cmd_obj_sp = ((CommandObjectMultiword *) cmd_obj_sp.get())->GetSubcommandSP |
| 563 | (cmd_words.GetArgumentAtIndex (j)); |
| 564 | if (cmd_obj_sp.get() == NULL) |
| 565 | // The sub-command name was invalid. Fail and return the empty 'ret_val'. |
| 566 | return ret_val; |
| 567 | } |
| 568 | else |
| 569 | // We have more words in the command name, but we don't have a multiword object. Fail and return |
| 570 | // empty 'ret_val'. |
| 571 | return ret_val; |
| 572 | } |
| 573 | // We successfully looped through all the command words and got valid command objects for them. Assign the |
| 574 | // last object retrieved to 'ret_val'. |
| 575 | ret_val = cmd_obj_sp; |
| 576 | } |
| 577 | } |
| 578 | return ret_val; |
Jim Ingham | d40f8a6 | 2010-07-06 22:46:59 +0000 | [diff] [blame] | 579 | } |
| 580 | |
| 581 | CommandObject * |
| 582 | CommandInterpreter::GetCommandObjectExact (const char *cmd_cstr, bool include_aliases) |
| 583 | { |
| 584 | return GetCommandSPExact (cmd_cstr, include_aliases).get(); |
| 585 | } |
| 586 | |
| 587 | CommandObject * |
| 588 | CommandInterpreter::GetCommandObject (const char *cmd_cstr, StringList *matches) |
| 589 | { |
| 590 | CommandObject *command_obj = GetCommandSP (cmd_cstr, false, true, matches).get(); |
| 591 | |
| 592 | // If we didn't find an exact match to the command string in the commands, look in |
| 593 | // the aliases. |
| 594 | |
| 595 | if (command_obj == NULL) |
| 596 | { |
| 597 | command_obj = GetCommandSP (cmd_cstr, true, true, matches).get(); |
| 598 | } |
| 599 | |
| 600 | // Finally, if there wasn't an exact match among the aliases, look for an inexact match |
| 601 | // in both the commands and the aliases. |
| 602 | |
| 603 | if (command_obj == NULL) |
| 604 | command_obj = GetCommandSP(cmd_cstr, true, false, matches).get(); |
| 605 | |
| 606 | return command_obj; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 607 | } |
| 608 | |
| 609 | bool |
| 610 | CommandInterpreter::CommandExists (const char *cmd) |
| 611 | { |
| 612 | return m_command_dict.find(cmd) != m_command_dict.end(); |
| 613 | } |
| 614 | |
| 615 | bool |
Caroline Tice | 5ddbe21 | 2011-05-06 21:37:15 +0000 | [diff] [blame] | 616 | CommandInterpreter::ProcessAliasOptionsArgs (lldb::CommandObjectSP &cmd_obj_sp, |
| 617 | const char *options_args, |
| 618 | OptionArgVectorSP &option_arg_vector_sp) |
| 619 | { |
| 620 | bool success = true; |
| 621 | OptionArgVector *option_arg_vector = option_arg_vector_sp.get(); |
| 622 | |
| 623 | if (!options_args || (strlen (options_args) < 1)) |
| 624 | return true; |
| 625 | |
| 626 | std::string options_string (options_args); |
| 627 | Args args (options_args); |
| 628 | CommandReturnObject result; |
| 629 | // Check to see if the command being aliased can take any command options. |
| 630 | Options *options = cmd_obj_sp->GetOptions (); |
| 631 | if (options) |
| 632 | { |
| 633 | // See if any options were specified as part of the alias; if so, handle them appropriately. |
| 634 | options->NotifyOptionParsingStarting (); |
| 635 | args.Unshift ("dummy_arg"); |
| 636 | args.ParseAliasOptions (*options, result, option_arg_vector, options_string); |
| 637 | args.Shift (); |
| 638 | if (result.Succeeded()) |
| 639 | options->VerifyPartialOptions (result); |
| 640 | if (!result.Succeeded() && result.GetStatus() != lldb::eReturnStatusStarted) |
| 641 | { |
| 642 | result.AppendError ("Unable to create requested alias.\n"); |
| 643 | return false; |
| 644 | } |
| 645 | } |
| 646 | |
Greg Clayton | 7268b4c | 2011-10-28 21:38:01 +0000 | [diff] [blame] | 647 | if (!options_string.empty()) |
Caroline Tice | 5ddbe21 | 2011-05-06 21:37:15 +0000 | [diff] [blame] | 648 | { |
| 649 | if (cmd_obj_sp->WantsRawCommandString ()) |
| 650 | option_arg_vector->push_back (OptionArgPair ("<argument>", |
| 651 | OptionArgValue (-1, |
| 652 | options_string))); |
| 653 | else |
| 654 | { |
| 655 | int argc = args.GetArgumentCount(); |
| 656 | for (size_t i = 0; i < argc; ++i) |
| 657 | if (strcmp (args.GetArgumentAtIndex (i), "") != 0) |
| 658 | option_arg_vector->push_back |
| 659 | (OptionArgPair ("<argument>", |
| 660 | OptionArgValue (-1, |
| 661 | std::string (args.GetArgumentAtIndex (i))))); |
| 662 | } |
| 663 | } |
| 664 | |
| 665 | return success; |
| 666 | } |
| 667 | |
| 668 | bool |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 669 | CommandInterpreter::AliasExists (const char *cmd) |
| 670 | { |
| 671 | return m_alias_dict.find(cmd) != m_alias_dict.end(); |
| 672 | } |
| 673 | |
| 674 | bool |
| 675 | CommandInterpreter::UserCommandExists (const char *cmd) |
| 676 | { |
| 677 | return m_user_dict.find(cmd) != m_user_dict.end(); |
| 678 | } |
| 679 | |
| 680 | void |
| 681 | CommandInterpreter::AddAlias (const char *alias_name, CommandObjectSP& command_obj_sp) |
| 682 | { |
Jim Ingham | d40f8a6 | 2010-07-06 22:46:59 +0000 | [diff] [blame] | 683 | command_obj_sp->SetIsAlias (true); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 684 | m_alias_dict[alias_name] = command_obj_sp; |
| 685 | } |
| 686 | |
| 687 | bool |
| 688 | CommandInterpreter::RemoveAlias (const char *alias_name) |
| 689 | { |
| 690 | CommandObject::CommandMap::iterator pos = m_alias_dict.find(alias_name); |
| 691 | if (pos != m_alias_dict.end()) |
| 692 | { |
| 693 | m_alias_dict.erase(pos); |
| 694 | return true; |
| 695 | } |
| 696 | return false; |
| 697 | } |
| 698 | bool |
| 699 | CommandInterpreter::RemoveUser (const char *alias_name) |
| 700 | { |
| 701 | CommandObject::CommandMap::iterator pos = m_user_dict.find(alias_name); |
| 702 | if (pos != m_user_dict.end()) |
| 703 | { |
| 704 | m_user_dict.erase(pos); |
| 705 | return true; |
| 706 | } |
| 707 | return false; |
| 708 | } |
| 709 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 710 | void |
| 711 | CommandInterpreter::GetAliasHelp (const char *alias_name, const char *command_name, StreamString &help_string) |
| 712 | { |
| 713 | help_string.Printf ("'%s", command_name); |
| 714 | OptionArgVectorSP option_arg_vector_sp = GetAliasOptions (alias_name); |
| 715 | |
| 716 | if (option_arg_vector_sp != NULL) |
| 717 | { |
| 718 | OptionArgVector *options = option_arg_vector_sp.get(); |
| 719 | for (int i = 0; i < options->size(); ++i) |
| 720 | { |
| 721 | OptionArgPair cur_option = (*options)[i]; |
| 722 | std::string opt = cur_option.first; |
Caroline Tice | 44c841d | 2010-12-07 19:58:26 +0000 | [diff] [blame] | 723 | OptionArgValue value_pair = cur_option.second; |
| 724 | std::string value = value_pair.second; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 725 | if (opt.compare("<argument>") == 0) |
| 726 | { |
| 727 | help_string.Printf (" %s", value.c_str()); |
| 728 | } |
| 729 | else |
| 730 | { |
| 731 | help_string.Printf (" %s", opt.c_str()); |
| 732 | if ((value.compare ("<no-argument>") != 0) |
| 733 | && (value.compare ("<need-argument") != 0)) |
| 734 | { |
| 735 | help_string.Printf (" %s", value.c_str()); |
| 736 | } |
| 737 | } |
| 738 | } |
| 739 | } |
| 740 | |
| 741 | help_string.Printf ("'"); |
| 742 | } |
| 743 | |
Greg Clayton | 65124ea | 2010-08-26 22:05:43 +0000 | [diff] [blame] | 744 | size_t |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 745 | CommandInterpreter::FindLongestCommandWord (CommandObject::CommandMap &dict) |
| 746 | { |
| 747 | CommandObject::CommandMap::const_iterator pos; |
Greg Clayton | 65124ea | 2010-08-26 22:05:43 +0000 | [diff] [blame] | 748 | CommandObject::CommandMap::const_iterator end = dict.end(); |
| 749 | size_t max_len = 0; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 750 | |
Greg Clayton | 65124ea | 2010-08-26 22:05:43 +0000 | [diff] [blame] | 751 | for (pos = dict.begin(); pos != end; ++pos) |
| 752 | { |
| 753 | size_t len = pos->first.size(); |
| 754 | if (max_len < len) |
| 755 | max_len = len; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 756 | } |
Greg Clayton | 65124ea | 2010-08-26 22:05:43 +0000 | [diff] [blame] | 757 | return max_len; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 758 | } |
| 759 | |
| 760 | void |
Enrico Granata | 6b1596d | 2011-08-16 23:24:13 +0000 | [diff] [blame] | 761 | CommandInterpreter::GetHelp (CommandReturnObject &result, |
Enrico Granata | 1ac6d1f | 2011-09-09 17:49:36 +0000 | [diff] [blame] | 762 | uint32_t cmd_types) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 763 | { |
| 764 | CommandObject::CommandMap::const_iterator pos; |
Greg Clayton | 65124ea | 2010-08-26 22:05:43 +0000 | [diff] [blame] | 765 | uint32_t max_len = FindLongestCommandWord (m_command_dict); |
Enrico Granata | 6b1596d | 2011-08-16 23:24:13 +0000 | [diff] [blame] | 766 | |
| 767 | if ( (cmd_types & eCommandTypesBuiltin) == eCommandTypesBuiltin ) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 768 | { |
Enrico Granata | 6b1596d | 2011-08-16 23:24:13 +0000 | [diff] [blame] | 769 | |
| 770 | result.AppendMessage("The following is a list of built-in, permanent debugger commands:"); |
| 771 | result.AppendMessage(""); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 772 | |
Enrico Granata | 6b1596d | 2011-08-16 23:24:13 +0000 | [diff] [blame] | 773 | for (pos = m_command_dict.begin(); pos != m_command_dict.end(); ++pos) |
| 774 | { |
| 775 | OutputFormattedHelpText (result.GetOutputStream(), pos->first.c_str(), "--", pos->second->GetHelp(), |
| 776 | max_len); |
| 777 | } |
| 778 | result.AppendMessage(""); |
| 779 | |
| 780 | } |
| 781 | |
Greg Clayton | 7268b4c | 2011-10-28 21:38:01 +0000 | [diff] [blame] | 782 | if (!m_alias_dict.empty() && ( (cmd_types & eCommandTypesAliases) == eCommandTypesAliases )) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 783 | { |
Jim Ingham | e3663e8 | 2010-10-22 18:47:16 +0000 | [diff] [blame] | 784 | result.AppendMessage("The following is a list of your current command abbreviations " |
Johnny Chen | 9e4c3d7 | 2011-04-21 00:39:18 +0000 | [diff] [blame] | 785 | "(see 'help command alias' for more info):"); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 786 | result.AppendMessage(""); |
Greg Clayton | 65124ea | 2010-08-26 22:05:43 +0000 | [diff] [blame] | 787 | max_len = FindLongestCommandWord (m_alias_dict); |
| 788 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 789 | for (pos = m_alias_dict.begin(); pos != m_alias_dict.end(); ++pos) |
| 790 | { |
| 791 | StreamString sstr; |
| 792 | StreamString translation_and_help; |
| 793 | std::string entry_name = pos->first; |
| 794 | std::string second_entry = pos->second.get()->GetCommandName(); |
| 795 | GetAliasHelp (pos->first.c_str(), pos->second->GetCommandName(), sstr); |
| 796 | |
| 797 | translation_and_help.Printf ("(%s) %s", sstr.GetData(), pos->second->GetHelp()); |
| 798 | OutputFormattedHelpText (result.GetOutputStream(), pos->first.c_str(), "--", |
| 799 | translation_and_help.GetData(), max_len); |
| 800 | } |
| 801 | result.AppendMessage(""); |
| 802 | } |
| 803 | |
Greg Clayton | 7268b4c | 2011-10-28 21:38:01 +0000 | [diff] [blame] | 804 | if (!m_user_dict.empty() && ( (cmd_types & eCommandTypesUserDef) == eCommandTypesUserDef )) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 805 | { |
| 806 | result.AppendMessage ("The following is a list of your current user-defined commands:"); |
| 807 | result.AppendMessage(""); |
Enrico Granata | 6b1596d | 2011-08-16 23:24:13 +0000 | [diff] [blame] | 808 | max_len = FindLongestCommandWord (m_user_dict); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 809 | for (pos = m_user_dict.begin(); pos != m_user_dict.end(); ++pos) |
| 810 | { |
Enrico Granata | 6b1596d | 2011-08-16 23:24:13 +0000 | [diff] [blame] | 811 | OutputFormattedHelpText (result.GetOutputStream(), pos->first.c_str(), "--", pos->second->GetHelp(), |
| 812 | max_len); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 813 | } |
| 814 | result.AppendMessage(""); |
| 815 | } |
| 816 | |
| 817 | result.AppendMessage("For more information on any particular command, try 'help <command-name>'."); |
| 818 | } |
| 819 | |
Caroline Tice | e0da7a5 | 2010-12-09 22:52:49 +0000 | [diff] [blame] | 820 | CommandObject * |
| 821 | CommandInterpreter::GetCommandObjectForCommand (std::string &command_string) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 822 | { |
Caroline Tice | e0da7a5 | 2010-12-09 22:52:49 +0000 | [diff] [blame] | 823 | // This function finds the final, lowest-level, alias-resolved command object whose 'Execute' function will |
| 824 | // eventually be invoked by the given command line. |
| 825 | |
| 826 | CommandObject *cmd_obj = NULL; |
| 827 | std::string white_space (" \t\v"); |
| 828 | size_t start = command_string.find_first_not_of (white_space); |
| 829 | size_t end = 0; |
| 830 | bool done = false; |
| 831 | while (!done) |
| 832 | { |
| 833 | if (start != std::string::npos) |
| 834 | { |
| 835 | // Get the next word from command_string. |
| 836 | end = command_string.find_first_of (white_space, start); |
| 837 | if (end == std::string::npos) |
| 838 | end = command_string.size(); |
| 839 | std::string cmd_word = command_string.substr (start, end - start); |
| 840 | |
| 841 | if (cmd_obj == NULL) |
| 842 | // Since cmd_obj is NULL we are on our first time through this loop. Check to see if cmd_word is a valid |
| 843 | // command or alias. |
| 844 | cmd_obj = GetCommandObject (cmd_word.c_str()); |
| 845 | else if (cmd_obj->IsMultiwordObject ()) |
| 846 | { |
| 847 | // Our current object is a multi-word object; see if the cmd_word is a valid sub-command for our object. |
| 848 | CommandObject *sub_cmd_obj = |
| 849 | ((CommandObjectMultiword *) cmd_obj)->GetSubcommandObject (cmd_word.c_str()); |
| 850 | if (sub_cmd_obj) |
| 851 | cmd_obj = sub_cmd_obj; |
| 852 | else // cmd_word was not a valid sub-command word, so we are donee |
| 853 | done = true; |
| 854 | } |
| 855 | else |
| 856 | // We have a cmd_obj and it is not a multi-word object, so we are done. |
| 857 | done = true; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 858 | |
Caroline Tice | e0da7a5 | 2010-12-09 22:52:49 +0000 | [diff] [blame] | 859 | // If we didn't find a valid command object, or our command object is not a multi-word object, or |
| 860 | // we are at the end of the command_string, then we are done. Otherwise, find the start of the |
| 861 | // next word. |
| 862 | |
| 863 | if (!cmd_obj || !cmd_obj->IsMultiwordObject() || end >= command_string.size()) |
| 864 | done = true; |
| 865 | else |
| 866 | start = command_string.find_first_not_of (white_space, end); |
| 867 | } |
| 868 | else |
| 869 | // Unable to find any more words. |
| 870 | done = true; |
| 871 | } |
| 872 | |
| 873 | if (end == command_string.size()) |
| 874 | command_string.clear(); |
| 875 | else |
| 876 | command_string = command_string.substr(end); |
| 877 | |
| 878 | return cmd_obj; |
| 879 | } |
| 880 | |
Greg Clayton | 9d855c6 | 2011-10-25 00:36:27 +0000 | [diff] [blame] | 881 | static const char *k_white_space = " \t\v"; |
Greg Clayton | 7268b4c | 2011-10-28 21:38:01 +0000 | [diff] [blame] | 882 | static const char *k_valid_command_chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_"; |
Greg Clayton | 9d855c6 | 2011-10-25 00:36:27 +0000 | [diff] [blame] | 883 | static void |
| 884 | StripLeadingSpaces (std::string &s) |
Caroline Tice | e0da7a5 | 2010-12-09 22:52:49 +0000 | [diff] [blame] | 885 | { |
Greg Clayton | 9d855c6 | 2011-10-25 00:36:27 +0000 | [diff] [blame] | 886 | if (!s.empty()) |
Caroline Tice | e0da7a5 | 2010-12-09 22:52:49 +0000 | [diff] [blame] | 887 | { |
Greg Clayton | 9d855c6 | 2011-10-25 00:36:27 +0000 | [diff] [blame] | 888 | size_t pos = s.find_first_not_of (k_white_space); |
| 889 | if (pos == std::string::npos) |
| 890 | s.clear(); |
| 891 | else if (pos == 0) |
| 892 | return; |
| 893 | s.erase (0, pos); |
| 894 | } |
| 895 | } |
| 896 | |
| 897 | static bool |
Greg Clayton | 7268b4c | 2011-10-28 21:38:01 +0000 | [diff] [blame] | 898 | ExtractCommand (std::string &command_string, std::string &command, std::string &suffix, char "e_char) |
Greg Clayton | 9d855c6 | 2011-10-25 00:36:27 +0000 | [diff] [blame] | 899 | { |
Greg Clayton | 7268b4c | 2011-10-28 21:38:01 +0000 | [diff] [blame] | 900 | command.clear(); |
| 901 | suffix.clear(); |
Greg Clayton | 9d855c6 | 2011-10-25 00:36:27 +0000 | [diff] [blame] | 902 | StripLeadingSpaces (command_string); |
| 903 | |
| 904 | bool result = false; |
| 905 | quote_char = '\0'; |
| 906 | |
| 907 | if (!command_string.empty()) |
| 908 | { |
| 909 | const char first_char = command_string[0]; |
| 910 | if (first_char == '\'' || first_char == '"') |
Caroline Tice | e0da7a5 | 2010-12-09 22:52:49 +0000 | [diff] [blame] | 911 | { |
Greg Clayton | 9d855c6 | 2011-10-25 00:36:27 +0000 | [diff] [blame] | 912 | quote_char = first_char; |
| 913 | const size_t end_quote_pos = command_string.find (quote_char, 1); |
| 914 | if (end_quote_pos == std::string::npos) |
Caroline Tice | 649116c | 2011-05-11 16:07:06 +0000 | [diff] [blame] | 915 | { |
Greg Clayton | 7268b4c | 2011-10-28 21:38:01 +0000 | [diff] [blame] | 916 | command.swap (command_string); |
Greg Clayton | 9d855c6 | 2011-10-25 00:36:27 +0000 | [diff] [blame] | 917 | command_string.erase (); |
Caroline Tice | 649116c | 2011-05-11 16:07:06 +0000 | [diff] [blame] | 918 | } |
| 919 | else |
| 920 | { |
Greg Clayton | 7268b4c | 2011-10-28 21:38:01 +0000 | [diff] [blame] | 921 | command.assign (command_string, 1, end_quote_pos - 1); |
Greg Clayton | 9d855c6 | 2011-10-25 00:36:27 +0000 | [diff] [blame] | 922 | if (end_quote_pos + 1 < command_string.size()) |
| 923 | command_string.erase (0, command_string.find_first_not_of (k_white_space, end_quote_pos + 1)); |
| 924 | else |
| 925 | command_string.erase (); |
Caroline Tice | 649116c | 2011-05-11 16:07:06 +0000 | [diff] [blame] | 926 | } |
Caroline Tice | e0da7a5 | 2010-12-09 22:52:49 +0000 | [diff] [blame] | 927 | } |
| 928 | else |
| 929 | { |
Greg Clayton | 9d855c6 | 2011-10-25 00:36:27 +0000 | [diff] [blame] | 930 | const size_t first_space_pos = command_string.find_first_of (k_white_space); |
| 931 | if (first_space_pos == std::string::npos) |
Caroline Tice | 649116c | 2011-05-11 16:07:06 +0000 | [diff] [blame] | 932 | { |
Greg Clayton | 7268b4c | 2011-10-28 21:38:01 +0000 | [diff] [blame] | 933 | command.swap (command_string); |
Greg Clayton | 9d855c6 | 2011-10-25 00:36:27 +0000 | [diff] [blame] | 934 | command_string.erase(); |
Caroline Tice | 649116c | 2011-05-11 16:07:06 +0000 | [diff] [blame] | 935 | } |
| 936 | else |
| 937 | { |
Greg Clayton | 7268b4c | 2011-10-28 21:38:01 +0000 | [diff] [blame] | 938 | command.assign (command_string, 0, first_space_pos); |
| 939 | command_string.erase(0, command_string.find_first_not_of (k_white_space, first_space_pos)); |
Caroline Tice | 649116c | 2011-05-11 16:07:06 +0000 | [diff] [blame] | 940 | } |
Caroline Tice | e0da7a5 | 2010-12-09 22:52:49 +0000 | [diff] [blame] | 941 | } |
Greg Clayton | 9d855c6 | 2011-10-25 00:36:27 +0000 | [diff] [blame] | 942 | result = true; |
Caroline Tice | e0da7a5 | 2010-12-09 22:52:49 +0000 | [diff] [blame] | 943 | } |
Greg Clayton | 7268b4c | 2011-10-28 21:38:01 +0000 | [diff] [blame] | 944 | |
| 945 | |
| 946 | if (!command.empty()) |
| 947 | { |
| 948 | // actual commands can't start with '-' or '_' |
| 949 | if (command[0] != '-' && command[0] != '_') |
| 950 | { |
| 951 | size_t pos = command.find_first_not_of(k_valid_command_chars); |
| 952 | if (pos > 0 && pos != std::string::npos) |
| 953 | { |
| 954 | suffix.assign (command.begin() + pos, command.end()); |
| 955 | command.erase (pos); |
| 956 | } |
| 957 | } |
| 958 | } |
Greg Clayton | 9d855c6 | 2011-10-25 00:36:27 +0000 | [diff] [blame] | 959 | |
| 960 | return result; |
Caroline Tice | e0da7a5 | 2010-12-09 22:52:49 +0000 | [diff] [blame] | 961 | } |
| 962 | |
Greg Clayton | 7268b4c | 2011-10-28 21:38:01 +0000 | [diff] [blame] | 963 | CommandObject * |
| 964 | CommandInterpreter::BuildAliasResult (const char *alias_name, |
| 965 | std::string &raw_input_string, |
| 966 | std::string &alias_result, |
| 967 | CommandReturnObject &result) |
Caroline Tice | e0da7a5 | 2010-12-09 22:52:49 +0000 | [diff] [blame] | 968 | { |
Greg Clayton | 7268b4c | 2011-10-28 21:38:01 +0000 | [diff] [blame] | 969 | CommandObject *alias_cmd_obj = NULL; |
Caroline Tice | e0da7a5 | 2010-12-09 22:52:49 +0000 | [diff] [blame] | 970 | Args cmd_args (raw_input_string.c_str()); |
| 971 | alias_cmd_obj = GetCommandObject (alias_name); |
| 972 | StreamString result_str; |
| 973 | |
| 974 | if (alias_cmd_obj) |
| 975 | { |
| 976 | std::string alias_name_str = alias_name; |
| 977 | if ((cmd_args.GetArgumentCount() == 0) |
| 978 | || (alias_name_str.compare (cmd_args.GetArgumentAtIndex(0)) != 0)) |
| 979 | cmd_args.Unshift (alias_name); |
| 980 | |
| 981 | result_str.Printf ("%s", alias_cmd_obj->GetCommandName ()); |
| 982 | OptionArgVectorSP option_arg_vector_sp = GetAliasOptions (alias_name); |
| 983 | |
| 984 | if (option_arg_vector_sp.get()) |
| 985 | { |
| 986 | OptionArgVector *option_arg_vector = option_arg_vector_sp.get(); |
| 987 | |
| 988 | for (int i = 0; i < option_arg_vector->size(); ++i) |
| 989 | { |
| 990 | OptionArgPair option_pair = (*option_arg_vector)[i]; |
| 991 | OptionArgValue value_pair = option_pair.second; |
| 992 | int value_type = value_pair.first; |
| 993 | std::string option = option_pair.first; |
| 994 | std::string value = value_pair.second; |
| 995 | if (option.compare ("<argument>") == 0) |
| 996 | result_str.Printf (" %s", value.c_str()); |
| 997 | else |
| 998 | { |
| 999 | result_str.Printf (" %s", option.c_str()); |
| 1000 | if (value_type != optional_argument) |
| 1001 | result_str.Printf (" "); |
| 1002 | if (value.compare ("<no_argument>") != 0) |
| 1003 | { |
| 1004 | int index = GetOptionArgumentPosition (value.c_str()); |
| 1005 | if (index == 0) |
| 1006 | result_str.Printf ("%s", value.c_str()); |
| 1007 | else if (index >= cmd_args.GetArgumentCount()) |
| 1008 | { |
| 1009 | |
| 1010 | result.AppendErrorWithFormat |
| 1011 | ("Not enough arguments provided; you need at least %d arguments to use this alias.\n", |
| 1012 | index); |
| 1013 | result.SetStatus (eReturnStatusFailed); |
Greg Clayton | 7268b4c | 2011-10-28 21:38:01 +0000 | [diff] [blame] | 1014 | return alias_cmd_obj; |
Caroline Tice | e0da7a5 | 2010-12-09 22:52:49 +0000 | [diff] [blame] | 1015 | } |
| 1016 | else |
| 1017 | { |
| 1018 | size_t strpos = raw_input_string.find (cmd_args.GetArgumentAtIndex (index)); |
| 1019 | if (strpos != std::string::npos) |
| 1020 | raw_input_string = raw_input_string.erase (strpos, |
| 1021 | strlen (cmd_args.GetArgumentAtIndex (index))); |
| 1022 | result_str.Printf ("%s", cmd_args.GetArgumentAtIndex (index)); |
| 1023 | } |
| 1024 | } |
| 1025 | } |
| 1026 | } |
| 1027 | } |
| 1028 | |
| 1029 | alias_result = result_str.GetData(); |
| 1030 | } |
Greg Clayton | 7268b4c | 2011-10-28 21:38:01 +0000 | [diff] [blame] | 1031 | return alias_cmd_obj; |
Caroline Tice | e0da7a5 | 2010-12-09 22:52:49 +0000 | [diff] [blame] | 1032 | } |
| 1033 | |
Greg Clayton | f5c0c72 | 2011-10-14 07:41:33 +0000 | [diff] [blame] | 1034 | Error |
| 1035 | CommandInterpreter::PreprocessCommand (std::string &command) |
| 1036 | { |
| 1037 | // The command preprocessor needs to do things to the command |
| 1038 | // line before any parsing of arguments or anything else is done. |
| 1039 | // The only current stuff that gets proprocessed is anyting enclosed |
| 1040 | // in backtick ('`') characters is evaluated as an expression and |
| 1041 | // the result of the expression must be a scalar that can be substituted |
| 1042 | // into the command. An example would be: |
| 1043 | // (lldb) memory read `$rsp + 20` |
| 1044 | Error error; // Error for any expressions that might not evaluate |
| 1045 | size_t start_backtick; |
| 1046 | size_t pos = 0; |
| 1047 | while ((start_backtick = command.find ('`', pos)) != std::string::npos) |
| 1048 | { |
| 1049 | if (start_backtick > 0 && command[start_backtick-1] == '\\') |
| 1050 | { |
| 1051 | // The backtick was preceeded by a '\' character, remove the slash |
| 1052 | // and don't treat the backtick as the start of an expression |
| 1053 | command.erase(start_backtick-1, 1); |
| 1054 | // No need to add one to start_backtick since we just deleted a char |
| 1055 | pos = start_backtick; |
| 1056 | } |
| 1057 | else |
| 1058 | { |
| 1059 | const size_t expr_content_start = start_backtick + 1; |
| 1060 | const size_t end_backtick = command.find ('`', expr_content_start); |
| 1061 | if (end_backtick == std::string::npos) |
| 1062 | return error; |
| 1063 | else if (end_backtick == expr_content_start) |
| 1064 | { |
| 1065 | // Empty expression (two backticks in a row) |
| 1066 | command.erase (start_backtick, 2); |
| 1067 | } |
| 1068 | else |
| 1069 | { |
| 1070 | std::string expr_str (command, expr_content_start, end_backtick - expr_content_start); |
| 1071 | |
| 1072 | Target *target = m_exe_ctx.GetTargetPtr(); |
Johnny Chen | b09f847 | 2011-10-29 00:21:50 +0000 | [diff] [blame] | 1073 | // Get a dummy target to allow for calculator mode while processing backticks. |
| 1074 | // This also helps break the infinite loop caused when target is null. |
| 1075 | if (!target) |
| 1076 | target = Host::GetDummyTarget(GetDebugger()).get(); |
Greg Clayton | f5c0c72 | 2011-10-14 07:41:33 +0000 | [diff] [blame] | 1077 | if (target) |
| 1078 | { |
| 1079 | const bool unwind_on_error = true; |
| 1080 | const bool keep_in_memory = false; |
| 1081 | ValueObjectSP expr_result_valobj_sp; |
| 1082 | ExecutionResults expr_result = target->EvaluateExpression (expr_str.c_str(), |
| 1083 | m_exe_ctx.GetFramePtr(), |
| 1084 | eExecutionPolicyOnlyWhenNeeded, |
| 1085 | unwind_on_error, keep_in_memory, |
| 1086 | eNoDynamicValues, |
| 1087 | expr_result_valobj_sp); |
| 1088 | if (expr_result == eExecutionCompleted) |
| 1089 | { |
| 1090 | Scalar scalar; |
| 1091 | if (expr_result_valobj_sp->ResolveValue (scalar)) |
| 1092 | { |
| 1093 | command.erase (start_backtick, end_backtick - start_backtick + 1); |
| 1094 | StreamString value_strm; |
| 1095 | const bool show_type = false; |
| 1096 | scalar.GetValue (&value_strm, show_type); |
| 1097 | size_t value_string_size = value_strm.GetSize(); |
| 1098 | if (value_string_size) |
| 1099 | { |
| 1100 | command.insert (start_backtick, value_strm.GetData(), value_string_size); |
| 1101 | pos = start_backtick + value_string_size; |
| 1102 | continue; |
| 1103 | } |
| 1104 | else |
| 1105 | { |
| 1106 | error.SetErrorStringWithFormat("expression value didn't result in a scalar value for the expression '%s'", expr_str.c_str()); |
| 1107 | } |
| 1108 | } |
| 1109 | else |
| 1110 | { |
| 1111 | error.SetErrorStringWithFormat("expression value didn't result in a scalar value for the expression '%s'", expr_str.c_str()); |
| 1112 | } |
| 1113 | } |
| 1114 | else |
| 1115 | { |
| 1116 | if (expr_result_valobj_sp) |
| 1117 | error = expr_result_valobj_sp->GetError(); |
| 1118 | if (error.Success()) |
| 1119 | { |
| 1120 | |
| 1121 | switch (expr_result) |
| 1122 | { |
| 1123 | case eExecutionSetupError: |
| 1124 | error.SetErrorStringWithFormat("expression setup error for the expression '%s'", expr_str.c_str()); |
| 1125 | break; |
| 1126 | case eExecutionCompleted: |
| 1127 | break; |
| 1128 | case eExecutionDiscarded: |
| 1129 | error.SetErrorStringWithFormat("expression discarded for the expression '%s'", expr_str.c_str()); |
| 1130 | break; |
| 1131 | case eExecutionInterrupted: |
| 1132 | error.SetErrorStringWithFormat("expression interrupted for the expression '%s'", expr_str.c_str()); |
| 1133 | break; |
| 1134 | case eExecutionTimedOut: |
| 1135 | error.SetErrorStringWithFormat("expression timed out for the expression '%s'", expr_str.c_str()); |
| 1136 | break; |
| 1137 | } |
| 1138 | } |
| 1139 | } |
| 1140 | } |
| 1141 | } |
| 1142 | if (error.Fail()) |
| 1143 | break; |
| 1144 | } |
| 1145 | } |
| 1146 | return error; |
| 1147 | } |
| 1148 | |
| 1149 | |
Caroline Tice | e0da7a5 | 2010-12-09 22:52:49 +0000 | [diff] [blame] | 1150 | bool |
| 1151 | CommandInterpreter::HandleCommand (const char *command_line, |
| 1152 | bool add_to_history, |
| 1153 | CommandReturnObject &result, |
Jim Ingham | 949d5ac | 2011-02-18 00:54:25 +0000 | [diff] [blame] | 1154 | ExecutionContext *override_context, |
Johnny Chen | 8bdf57c | 2011-10-05 00:42:59 +0000 | [diff] [blame] | 1155 | bool repeat_on_empty_command, |
| 1156 | bool no_context_switching) |
Jim Ingham | 949d5ac | 2011-02-18 00:54:25 +0000 | [diff] [blame] | 1157 | |
Caroline Tice | e0da7a5 | 2010-12-09 22:52:49 +0000 | [diff] [blame] | 1158 | { |
Jim Ingham | 949d5ac | 2011-02-18 00:54:25 +0000 | [diff] [blame] | 1159 | |
Caroline Tice | e0da7a5 | 2010-12-09 22:52:49 +0000 | [diff] [blame] | 1160 | bool done = false; |
| 1161 | CommandObject *cmd_obj = NULL; |
Caroline Tice | e0da7a5 | 2010-12-09 22:52:49 +0000 | [diff] [blame] | 1162 | bool wants_raw_input = false; |
| 1163 | std::string command_string (command_line); |
Jim Ingham | 6247dbe | 2011-07-12 03:12:18 +0000 | [diff] [blame] | 1164 | std::string original_command_string (command_line); |
Caroline Tice | e0da7a5 | 2010-12-09 22:52:49 +0000 | [diff] [blame] | 1165 | |
| 1166 | LogSP log (lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_COMMANDS)); |
Greg Clayton | e98ac25 | 2010-11-10 04:57:04 +0000 | [diff] [blame] | 1167 | Host::SetCrashDescriptionWithFormat ("HandleCommand(command = \"%s\")", command_line); |
| 1168 | |
| 1169 | // Make a scoped cleanup object that will clear the crash description string |
| 1170 | // on exit of this function. |
Enrico Granata | 1a10208 | 2011-07-12 00:18:11 +0000 | [diff] [blame] | 1171 | lldb_utility::CleanUp <const char *> crash_description_cleanup(NULL, Host::SetCrashDescription); |
Greg Clayton | e98ac25 | 2010-11-10 04:57:04 +0000 | [diff] [blame] | 1172 | |
Caroline Tice | e0da7a5 | 2010-12-09 22:52:49 +0000 | [diff] [blame] | 1173 | if (log) |
| 1174 | log->Printf ("Processing command: %s", command_line); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1175 | |
Jim Ingham | abab14b | 2010-11-04 23:08:45 +0000 | [diff] [blame] | 1176 | Timer scoped_timer (__PRETTY_FUNCTION__, "Handling command: %s.", command_line); |
| 1177 | |
Johnny Chen | 8bdf57c | 2011-10-05 00:42:59 +0000 | [diff] [blame] | 1178 | if (!no_context_switching) |
| 1179 | UpdateExecutionContext (override_context); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1180 | |
Jim Ingham | 949d5ac | 2011-02-18 00:54:25 +0000 | [diff] [blame] | 1181 | bool empty_command = false; |
| 1182 | bool comment_command = false; |
| 1183 | if (command_string.empty()) |
| 1184 | empty_command = true; |
| 1185 | else |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1186 | { |
Jim Ingham | 949d5ac | 2011-02-18 00:54:25 +0000 | [diff] [blame] | 1187 | const char *k_space_characters = "\t\n\v\f\r "; |
| 1188 | |
| 1189 | size_t non_space = command_string.find_first_not_of (k_space_characters); |
| 1190 | // Check for empty line or comment line (lines whose first |
| 1191 | // non-space character is the comment character for this interpreter) |
| 1192 | if (non_space == std::string::npos) |
| 1193 | empty_command = true; |
| 1194 | else if (command_string[non_space] == m_comment_char) |
| 1195 | comment_command = true; |
Jim Ingham | 6247dbe | 2011-07-12 03:12:18 +0000 | [diff] [blame] | 1196 | else if (command_string[non_space] == m_repeat_char) |
| 1197 | { |
| 1198 | const char *history_string = FindHistoryString (command_string.c_str() + non_space); |
| 1199 | if (history_string == NULL) |
| 1200 | { |
| 1201 | result.AppendErrorWithFormat ("Could not find entry: %s in history", command_string.c_str()); |
| 1202 | result.SetStatus(eReturnStatusFailed); |
| 1203 | return false; |
| 1204 | } |
| 1205 | add_to_history = false; |
| 1206 | command_string = history_string; |
| 1207 | original_command_string = history_string; |
| 1208 | } |
Jim Ingham | 949d5ac | 2011-02-18 00:54:25 +0000 | [diff] [blame] | 1209 | } |
| 1210 | |
| 1211 | if (empty_command) |
| 1212 | { |
| 1213 | if (repeat_on_empty_command) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1214 | { |
Jim Ingham | 949d5ac | 2011-02-18 00:54:25 +0000 | [diff] [blame] | 1215 | if (m_command_history.empty()) |
| 1216 | { |
| 1217 | result.AppendError ("empty command"); |
| 1218 | result.SetStatus(eReturnStatusFailed); |
| 1219 | return false; |
| 1220 | } |
| 1221 | else |
| 1222 | { |
| 1223 | command_line = m_repeat_command.c_str(); |
| 1224 | command_string = command_line; |
Jim Ingham | 6247dbe | 2011-07-12 03:12:18 +0000 | [diff] [blame] | 1225 | original_command_string = command_line; |
Jim Ingham | 949d5ac | 2011-02-18 00:54:25 +0000 | [diff] [blame] | 1226 | if (m_repeat_command.empty()) |
| 1227 | { |
| 1228 | result.AppendErrorWithFormat("No auto repeat.\n"); |
| 1229 | result.SetStatus (eReturnStatusFailed); |
| 1230 | return false; |
| 1231 | } |
| 1232 | } |
| 1233 | add_to_history = false; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1234 | } |
| 1235 | else |
| 1236 | { |
Jim Ingham | 949d5ac | 2011-02-18 00:54:25 +0000 | [diff] [blame] | 1237 | result.SetStatus (eReturnStatusSuccessFinishNoResult); |
| 1238 | return true; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1239 | } |
Jim Ingham | 949d5ac | 2011-02-18 00:54:25 +0000 | [diff] [blame] | 1240 | } |
| 1241 | else if (comment_command) |
| 1242 | { |
| 1243 | result.SetStatus (eReturnStatusSuccessFinishNoResult); |
| 1244 | return true; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1245 | } |
Caroline Tice | 649116c | 2011-05-11 16:07:06 +0000 | [diff] [blame] | 1246 | |
Greg Clayton | f5c0c72 | 2011-10-14 07:41:33 +0000 | [diff] [blame] | 1247 | |
| 1248 | Error error (PreprocessCommand (command_string)); |
| 1249 | |
| 1250 | if (error.Fail()) |
| 1251 | { |
| 1252 | result.AppendError (error.AsCString()); |
| 1253 | result.SetStatus(eReturnStatusFailed); |
| 1254 | return false; |
| 1255 | } |
Caroline Tice | e0da7a5 | 2010-12-09 22:52:49 +0000 | [diff] [blame] | 1256 | // Phase 1. |
| 1257 | |
| 1258 | // Before we do ANY kind of argument processing, etc. we need to figure out what the real/final command object |
| 1259 | // is for the specified command, and whether or not it wants raw input. This gets complicated by the fact that |
| 1260 | // the user could have specified an alias, and in translating the alias there may also be command options and/or |
| 1261 | // even data (including raw text strings) that need to be found and inserted into the command line as part of |
| 1262 | // the translation. So this first step is plain look-up & replacement, resulting in three things: 1). the command |
Greg Clayton | 5d187e5 | 2011-01-08 20:28:42 +0000 | [diff] [blame] | 1263 | // object whose Execute method will actually be called; 2). a revised command string, with all substitutions & |
Caroline Tice | e0da7a5 | 2010-12-09 22:52:49 +0000 | [diff] [blame] | 1264 | // replacements taken care of; 3). whether or not the Execute function wants raw input or not. |
Caroline Tice | 44c841d | 2010-12-07 19:58:26 +0000 | [diff] [blame] | 1265 | |
Caroline Tice | e0da7a5 | 2010-12-09 22:52:49 +0000 | [diff] [blame] | 1266 | StreamString revised_command_line; |
Caroline Tice | ea6e3df | 2010-12-11 08:16:56 +0000 | [diff] [blame] | 1267 | size_t actual_cmd_name_len = 0; |
Greg Clayton | 7268b4c | 2011-10-28 21:38:01 +0000 | [diff] [blame] | 1268 | std::string next_word; |
Caroline Tice | e0da7a5 | 2010-12-09 22:52:49 +0000 | [diff] [blame] | 1269 | while (!done) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1270 | { |
Caroline Tice | 649116c | 2011-05-11 16:07:06 +0000 | [diff] [blame] | 1271 | char quote_char = '\0'; |
Greg Clayton | 7268b4c | 2011-10-28 21:38:01 +0000 | [diff] [blame] | 1272 | std::string suffix; |
| 1273 | ExtractCommand (command_string, next_word, suffix, quote_char); |
| 1274 | if (cmd_obj == NULL) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1275 | { |
Greg Clayton | 7268b4c | 2011-10-28 21:38:01 +0000 | [diff] [blame] | 1276 | if (AliasExists (next_word.c_str())) |
Caroline Tice | 56d2fc4 | 2010-12-14 18:51:39 +0000 | [diff] [blame] | 1277 | { |
Greg Clayton | 7268b4c | 2011-10-28 21:38:01 +0000 | [diff] [blame] | 1278 | std::string alias_result; |
| 1279 | cmd_obj = BuildAliasResult (next_word.c_str(), command_string, alias_result, result); |
| 1280 | revised_command_line.Printf ("%s", alias_result.c_str()); |
| 1281 | if (cmd_obj) |
| 1282 | { |
| 1283 | wants_raw_input = cmd_obj->WantsRawCommandString (); |
| 1284 | actual_cmd_name_len = strlen (cmd_obj->GetCommandName()); |
| 1285 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1286 | } |
| 1287 | else |
| 1288 | { |
Greg Clayton | 7268b4c | 2011-10-28 21:38:01 +0000 | [diff] [blame] | 1289 | cmd_obj = GetCommandObject (next_word.c_str()); |
| 1290 | if (cmd_obj) |
| 1291 | { |
| 1292 | actual_cmd_name_len += next_word.length(); |
| 1293 | revised_command_line.Printf ("%s", next_word.c_str()); |
| 1294 | wants_raw_input = cmd_obj->WantsRawCommandString (); |
| 1295 | } |
Caroline Tice | 649116c | 2011-05-11 16:07:06 +0000 | [diff] [blame] | 1296 | else |
Greg Clayton | 7268b4c | 2011-10-28 21:38:01 +0000 | [diff] [blame] | 1297 | { |
| 1298 | revised_command_line.Printf ("%s", next_word.c_str()); |
| 1299 | } |
Caroline Tice | e0da7a5 | 2010-12-09 22:52:49 +0000 | [diff] [blame] | 1300 | } |
| 1301 | } |
| 1302 | else |
| 1303 | { |
Greg Clayton | 7268b4c | 2011-10-28 21:38:01 +0000 | [diff] [blame] | 1304 | if (cmd_obj->IsMultiwordObject ()) |
| 1305 | { |
| 1306 | CommandObject *sub_cmd_obj = ((CommandObjectMultiword *) cmd_obj)->GetSubcommandObject (next_word.c_str()); |
| 1307 | if (sub_cmd_obj) |
| 1308 | { |
| 1309 | actual_cmd_name_len += next_word.length() + 1; |
| 1310 | revised_command_line.Printf (" %s", next_word.c_str()); |
| 1311 | cmd_obj = sub_cmd_obj; |
| 1312 | wants_raw_input = cmd_obj->WantsRawCommandString (); |
| 1313 | } |
| 1314 | else |
| 1315 | { |
| 1316 | if (quote_char) |
| 1317 | revised_command_line.Printf (" %c%s%s%c", quote_char, next_word.c_str(), suffix.c_str(), quote_char); |
| 1318 | else |
| 1319 | revised_command_line.Printf (" %s%s", next_word.c_str(), suffix.c_str()); |
| 1320 | done = true; |
| 1321 | } |
| 1322 | } |
Caroline Tice | 649116c | 2011-05-11 16:07:06 +0000 | [diff] [blame] | 1323 | else |
Greg Clayton | 7268b4c | 2011-10-28 21:38:01 +0000 | [diff] [blame] | 1324 | { |
| 1325 | if (quote_char) |
| 1326 | revised_command_line.Printf (" %c%s%s%c", quote_char, next_word.c_str(), suffix.c_str(), quote_char); |
| 1327 | else |
| 1328 | revised_command_line.Printf (" %s%s", next_word.c_str(), suffix.c_str()); |
| 1329 | done = true; |
| 1330 | } |
Caroline Tice | e0da7a5 | 2010-12-09 22:52:49 +0000 | [diff] [blame] | 1331 | } |
| 1332 | |
| 1333 | if (cmd_obj == NULL) |
| 1334 | { |
| 1335 | result.AppendErrorWithFormat ("'%s' is not a valid command.\n", next_word.c_str()); |
| 1336 | result.SetStatus (eReturnStatusFailed); |
| 1337 | return false; |
| 1338 | } |
| 1339 | |
Greg Clayton | 7268b4c | 2011-10-28 21:38:01 +0000 | [diff] [blame] | 1340 | if (cmd_obj->IsMultiwordObject ()) |
| 1341 | { |
| 1342 | if (!suffix.empty()) |
| 1343 | { |
| 1344 | |
| 1345 | result.AppendErrorWithFormat ("multi-word commands ('%s') can't have shorthand suffixes: '%s'\n", |
| 1346 | next_word.c_str(), |
| 1347 | suffix.c_str()); |
| 1348 | result.SetStatus (eReturnStatusFailed); |
| 1349 | return false; |
| 1350 | } |
| 1351 | } |
| 1352 | else |
| 1353 | { |
| 1354 | // If we found a normal command, we are done |
| 1355 | done = true; |
| 1356 | if (!suffix.empty()) |
| 1357 | { |
| 1358 | switch (suffix[0]) |
| 1359 | { |
| 1360 | case '/': |
| 1361 | // GDB format suffixes |
Greg Clayton | d8a218d | 2011-10-29 00:57:28 +0000 | [diff] [blame^] | 1362 | { |
| 1363 | Options *command_options = cmd_obj->GetOptions(); |
| 1364 | if (command_options && command_options->SupportsLongOption("gdb-format")) |
| 1365 | { |
| 1366 | revised_command_line.Printf (" --gdb-format=%s", suffix.c_str() + 1); |
| 1367 | if (wants_raw_input && command_string.find ("-- ") == std::string::npos) |
| 1368 | revised_command_line.Printf (" --"); |
| 1369 | } |
| 1370 | else |
| 1371 | { |
| 1372 | result.AppendErrorWithFormat ("the '%s' command doesn't support the --gdb-format option\n", |
| 1373 | cmd_obj->GetCommandName()); |
| 1374 | result.SetStatus (eReturnStatusFailed); |
| 1375 | return false; |
| 1376 | } |
| 1377 | } |
Greg Clayton | 7268b4c | 2011-10-28 21:38:01 +0000 | [diff] [blame] | 1378 | break; |
Greg Clayton | d8a218d | 2011-10-29 00:57:28 +0000 | [diff] [blame^] | 1379 | |
| 1380 | default: |
| 1381 | result.AppendErrorWithFormat ("unknown command shorthand suffix: '%s'\n", |
| 1382 | suffix.c_str()); |
| 1383 | result.SetStatus (eReturnStatusFailed); |
| 1384 | return false; |
| 1385 | |
Greg Clayton | 7268b4c | 2011-10-28 21:38:01 +0000 | [diff] [blame] | 1386 | } |
| 1387 | } |
| 1388 | } |
Caroline Tice | e0da7a5 | 2010-12-09 22:52:49 +0000 | [diff] [blame] | 1389 | if (command_string.length() == 0) |
| 1390 | done = true; |
| 1391 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1392 | } |
Caroline Tice | e0da7a5 | 2010-12-09 22:52:49 +0000 | [diff] [blame] | 1393 | |
Greg Clayton | 7268b4c | 2011-10-28 21:38:01 +0000 | [diff] [blame] | 1394 | if (!command_string.empty()) |
Caroline Tice | e0da7a5 | 2010-12-09 22:52:49 +0000 | [diff] [blame] | 1395 | revised_command_line.Printf (" %s", command_string.c_str()); |
| 1396 | |
| 1397 | // End of Phase 1. |
| 1398 | // At this point cmd_obj should contain the CommandObject whose Execute method will be called, if the command |
| 1399 | // specified was valid; revised_command_line contains the complete command line (including command name(s)), |
| 1400 | // fully translated with all substitutions & translations taken care of (still in raw text format); and |
| 1401 | // wants_raw_input specifies whether the Execute method expects raw input or not. |
| 1402 | |
| 1403 | |
| 1404 | if (log) |
| 1405 | { |
| 1406 | log->Printf ("HandleCommand, cmd_obj : '%s'", cmd_obj ? cmd_obj->GetCommandName() : "<not found>"); |
| 1407 | log->Printf ("HandleCommand, revised_command_line: '%s'", revised_command_line.GetData()); |
| 1408 | log->Printf ("HandleCommand, wants_raw_input:'%s'", wants_raw_input ? "True" : "False"); |
| 1409 | } |
| 1410 | |
| 1411 | // Phase 2. |
| 1412 | // Take care of things like setting up the history command & calling the appropriate Execute method on the |
| 1413 | // CommandObject, with the appropriate arguments. |
| 1414 | |
| 1415 | if (cmd_obj != NULL) |
| 1416 | { |
| 1417 | if (add_to_history) |
| 1418 | { |
| 1419 | Args command_args (revised_command_line.GetData()); |
| 1420 | const char *repeat_command = cmd_obj->GetRepeatCommand(command_args, 0); |
| 1421 | if (repeat_command != NULL) |
| 1422 | m_repeat_command.assign(repeat_command); |
| 1423 | else |
Jim Ingham | 6247dbe | 2011-07-12 03:12:18 +0000 | [diff] [blame] | 1424 | m_repeat_command.assign(original_command_string.c_str()); |
Caroline Tice | e0da7a5 | 2010-12-09 22:52:49 +0000 | [diff] [blame] | 1425 | |
Jim Ingham | 6247dbe | 2011-07-12 03:12:18 +0000 | [diff] [blame] | 1426 | // Don't keep pushing the same command onto the history... |
Greg Clayton | 7268b4c | 2011-10-28 21:38:01 +0000 | [diff] [blame] | 1427 | if (m_command_history.empty() || m_command_history.back() != original_command_string) |
Jim Ingham | 6247dbe | 2011-07-12 03:12:18 +0000 | [diff] [blame] | 1428 | m_command_history.push_back (original_command_string); |
Caroline Tice | e0da7a5 | 2010-12-09 22:52:49 +0000 | [diff] [blame] | 1429 | } |
| 1430 | |
| 1431 | command_string = revised_command_line.GetData(); |
| 1432 | std::string command_name (cmd_obj->GetCommandName()); |
Caroline Tice | ea6e3df | 2010-12-11 08:16:56 +0000 | [diff] [blame] | 1433 | std::string remainder; |
| 1434 | if (actual_cmd_name_len < command_string.length()) |
| 1435 | remainder = command_string.substr (actual_cmd_name_len); // Note: 'actual_cmd_name_len' may be considerably shorter |
| 1436 | // than cmd_obj->GetCommandName(), because name completion |
| 1437 | // allows users to enter short versions of the names, |
| 1438 | // e.g. 'br s' for 'breakpoint set'. |
Caroline Tice | e0da7a5 | 2010-12-09 22:52:49 +0000 | [diff] [blame] | 1439 | |
| 1440 | // Remove any initial spaces |
| 1441 | std::string white_space (" \t\v"); |
| 1442 | size_t pos = remainder.find_first_not_of (white_space); |
| 1443 | if (pos != 0 && pos != std::string::npos) |
Greg Clayton | 91c9dcf | 2011-04-22 20:58:45 +0000 | [diff] [blame] | 1444 | remainder.erase(0, pos); |
Caroline Tice | e0da7a5 | 2010-12-09 22:52:49 +0000 | [diff] [blame] | 1445 | |
| 1446 | if (log) |
Jason Molenda | 24c991c | 2011-08-25 00:20:04 +0000 | [diff] [blame] | 1447 | log->Printf ("HandleCommand, command line after removing command name(s): '%s'", remainder.c_str()); |
Caroline Tice | e0da7a5 | 2010-12-09 22:52:49 +0000 | [diff] [blame] | 1448 | |
| 1449 | |
| 1450 | if (wants_raw_input) |
| 1451 | cmd_obj->ExecuteRawCommandString (remainder.c_str(), result); |
| 1452 | else |
| 1453 | { |
| 1454 | Args cmd_args (remainder.c_str()); |
| 1455 | cmd_obj->ExecuteWithOptions (cmd_args, result); |
| 1456 | } |
| 1457 | } |
| 1458 | else |
| 1459 | { |
| 1460 | // We didn't find the first command object, so complete the first argument. |
| 1461 | Args command_args (revised_command_line.GetData()); |
| 1462 | StringList matches; |
| 1463 | int num_matches; |
| 1464 | int cursor_index = 0; |
| 1465 | int cursor_char_position = strlen (command_args.GetArgumentAtIndex(0)); |
| 1466 | bool word_complete; |
| 1467 | num_matches = HandleCompletionMatches (command_args, |
| 1468 | cursor_index, |
| 1469 | cursor_char_position, |
| 1470 | 0, |
| 1471 | -1, |
| 1472 | word_complete, |
| 1473 | matches); |
| 1474 | |
| 1475 | if (num_matches > 0) |
| 1476 | { |
| 1477 | std::string error_msg; |
| 1478 | error_msg.assign ("ambiguous command '"); |
| 1479 | error_msg.append(command_args.GetArgumentAtIndex(0)); |
| 1480 | error_msg.append ("'."); |
| 1481 | |
| 1482 | error_msg.append (" Possible completions:"); |
| 1483 | for (int i = 0; i < num_matches; i++) |
| 1484 | { |
| 1485 | error_msg.append ("\n\t"); |
| 1486 | error_msg.append (matches.GetStringAtIndex (i)); |
| 1487 | } |
| 1488 | error_msg.append ("\n"); |
| 1489 | result.AppendRawError (error_msg.c_str(), error_msg.size()); |
| 1490 | } |
| 1491 | else |
| 1492 | result.AppendErrorWithFormat ("Unrecognized command '%s'.\n", command_args.GetArgumentAtIndex (0)); |
| 1493 | |
| 1494 | result.SetStatus (eReturnStatusFailed); |
| 1495 | } |
| 1496 | |
Jason Molenda | 24c991c | 2011-08-25 00:20:04 +0000 | [diff] [blame] | 1497 | if (log) |
| 1498 | log->Printf ("HandleCommand, command %s", (result.Succeeded() ? "succeeded" : "did not succeed")); |
| 1499 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1500 | return result.Succeeded(); |
| 1501 | } |
| 1502 | |
| 1503 | int |
| 1504 | CommandInterpreter::HandleCompletionMatches (Args &parsed_line, |
| 1505 | int &cursor_index, |
| 1506 | int &cursor_char_position, |
| 1507 | int match_start_point, |
| 1508 | int max_return_elements, |
Jim Ingham | 802f8b0 | 2010-06-30 05:02:46 +0000 | [diff] [blame] | 1509 | bool &word_complete, |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1510 | StringList &matches) |
| 1511 | { |
| 1512 | int num_command_matches = 0; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1513 | bool look_for_subcommand = false; |
Jim Ingham | 802f8b0 | 2010-06-30 05:02:46 +0000 | [diff] [blame] | 1514 | |
| 1515 | // For any of the command completions a unique match will be a complete word. |
| 1516 | word_complete = true; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1517 | |
| 1518 | if (cursor_index == -1) |
| 1519 | { |
| 1520 | // We got nothing on the command line, so return the list of commands |
Jim Ingham | d40f8a6 | 2010-07-06 22:46:59 +0000 | [diff] [blame] | 1521 | bool include_aliases = true; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1522 | num_command_matches = GetCommandNamesMatchingPartialString ("", include_aliases, matches); |
| 1523 | } |
| 1524 | else if (cursor_index == 0) |
| 1525 | { |
| 1526 | // The cursor is in the first argument, so just do a lookup in the dictionary. |
Jim Ingham | d40f8a6 | 2010-07-06 22:46:59 +0000 | [diff] [blame] | 1527 | CommandObject *cmd_obj = GetCommandObject (parsed_line.GetArgumentAtIndex(0), &matches); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1528 | num_command_matches = matches.GetSize(); |
| 1529 | |
| 1530 | if (num_command_matches == 1 |
| 1531 | && cmd_obj && cmd_obj->IsMultiwordObject() |
| 1532 | && matches.GetStringAtIndex(0) != NULL |
| 1533 | && strcmp (parsed_line.GetArgumentAtIndex(0), matches.GetStringAtIndex(0)) == 0) |
| 1534 | { |
| 1535 | look_for_subcommand = true; |
| 1536 | num_command_matches = 0; |
| 1537 | matches.DeleteStringAtIndex(0); |
| 1538 | parsed_line.AppendArgument (""); |
| 1539 | cursor_index++; |
| 1540 | cursor_char_position = 0; |
| 1541 | } |
| 1542 | } |
| 1543 | |
| 1544 | if (cursor_index > 0 || look_for_subcommand) |
| 1545 | { |
| 1546 | // We are completing further on into a commands arguments, so find the command and tell it |
| 1547 | // to complete the command. |
| 1548 | // First see if there is a matching initial command: |
Jim Ingham | d40f8a6 | 2010-07-06 22:46:59 +0000 | [diff] [blame] | 1549 | CommandObject *command_object = GetCommandObject (parsed_line.GetArgumentAtIndex(0)); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1550 | if (command_object == NULL) |
| 1551 | { |
| 1552 | return 0; |
| 1553 | } |
| 1554 | else |
| 1555 | { |
| 1556 | parsed_line.Shift(); |
| 1557 | cursor_index--; |
Greg Clayton | 238c0a1 | 2010-09-18 01:14:36 +0000 | [diff] [blame] | 1558 | num_command_matches = command_object->HandleCompletion (parsed_line, |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 1559 | cursor_index, |
| 1560 | cursor_char_position, |
| 1561 | match_start_point, |
Jim Ingham | 802f8b0 | 2010-06-30 05:02:46 +0000 | [diff] [blame] | 1562 | max_return_elements, |
| 1563 | word_complete, |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1564 | matches); |
| 1565 | } |
| 1566 | } |
| 1567 | |
| 1568 | return num_command_matches; |
| 1569 | |
| 1570 | } |
| 1571 | |
| 1572 | int |
| 1573 | CommandInterpreter::HandleCompletion (const char *current_line, |
| 1574 | const char *cursor, |
| 1575 | const char *last_char, |
| 1576 | int match_start_point, |
| 1577 | int max_return_elements, |
| 1578 | StringList &matches) |
| 1579 | { |
| 1580 | // We parse the argument up to the cursor, so the last argument in parsed_line is |
| 1581 | // the one containing the cursor, and the cursor is after the last character. |
| 1582 | |
| 1583 | Args parsed_line(current_line, last_char - current_line); |
| 1584 | Args partial_parsed_line(current_line, cursor - current_line); |
| 1585 | |
Jim Ingham | 6247dbe | 2011-07-12 03:12:18 +0000 | [diff] [blame] | 1586 | // Don't complete comments, and if the line we are completing is just the history repeat character, |
| 1587 | // substitute the appropriate history line. |
| 1588 | const char *first_arg = parsed_line.GetArgumentAtIndex(0); |
| 1589 | if (first_arg) |
| 1590 | { |
| 1591 | if (first_arg[0] == m_comment_char) |
| 1592 | return 0; |
| 1593 | else if (first_arg[0] == m_repeat_char) |
| 1594 | { |
| 1595 | const char *history_string = FindHistoryString (first_arg); |
| 1596 | if (history_string != NULL) |
| 1597 | { |
| 1598 | matches.Clear(); |
| 1599 | matches.InsertStringAtIndex(0, history_string); |
| 1600 | return -2; |
| 1601 | } |
| 1602 | else |
| 1603 | return 0; |
| 1604 | |
| 1605 | } |
| 1606 | } |
| 1607 | |
| 1608 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1609 | int num_args = partial_parsed_line.GetArgumentCount(); |
| 1610 | int cursor_index = partial_parsed_line.GetArgumentCount() - 1; |
| 1611 | int cursor_char_position; |
| 1612 | |
| 1613 | if (cursor_index == -1) |
| 1614 | cursor_char_position = 0; |
| 1615 | else |
| 1616 | cursor_char_position = strlen (partial_parsed_line.GetArgumentAtIndex(cursor_index)); |
Jim Ingham | cf03765 | 2010-12-14 19:56:01 +0000 | [diff] [blame] | 1617 | |
| 1618 | if (cursor > current_line && cursor[-1] == ' ') |
| 1619 | { |
| 1620 | // We are just after a space. If we are in an argument, then we will continue |
| 1621 | // parsing, but if we are between arguments, then we have to complete whatever the next |
| 1622 | // element would be. |
| 1623 | // We can distinguish the two cases because if we are in an argument (e.g. because the space is |
| 1624 | // protected by a quote) then the space will also be in the parsed argument... |
| 1625 | |
| 1626 | const char *current_elem = partial_parsed_line.GetArgumentAtIndex(cursor_index); |
| 1627 | if (cursor_char_position == 0 || current_elem[cursor_char_position - 1] != ' ') |
| 1628 | { |
| 1629 | parsed_line.InsertArgumentAtIndex(cursor_index + 1, "", '"'); |
| 1630 | cursor_index++; |
| 1631 | cursor_char_position = 0; |
| 1632 | } |
| 1633 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1634 | |
| 1635 | int num_command_matches; |
| 1636 | |
| 1637 | matches.Clear(); |
| 1638 | |
| 1639 | // Only max_return_elements == -1 is supported at present: |
| 1640 | assert (max_return_elements == -1); |
Jim Ingham | 802f8b0 | 2010-06-30 05:02:46 +0000 | [diff] [blame] | 1641 | bool word_complete; |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 1642 | num_command_matches = HandleCompletionMatches (parsed_line, |
| 1643 | cursor_index, |
| 1644 | cursor_char_position, |
| 1645 | match_start_point, |
Jim Ingham | 802f8b0 | 2010-06-30 05:02:46 +0000 | [diff] [blame] | 1646 | max_return_elements, |
| 1647 | word_complete, |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 1648 | matches); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1649 | |
| 1650 | if (num_command_matches <= 0) |
| 1651 | return num_command_matches; |
| 1652 | |
| 1653 | if (num_args == 0) |
| 1654 | { |
| 1655 | // If we got an empty string, insert nothing. |
| 1656 | matches.InsertStringAtIndex(0, ""); |
| 1657 | } |
| 1658 | else |
| 1659 | { |
| 1660 | // Now figure out if there is a common substring, and if so put that in element 0, otherwise |
| 1661 | // put an empty string in element 0. |
| 1662 | std::string command_partial_str; |
| 1663 | if (cursor_index >= 0) |
Jim Ingham | e3663e8 | 2010-10-22 18:47:16 +0000 | [diff] [blame] | 1664 | command_partial_str.assign(parsed_line.GetArgumentAtIndex(cursor_index), |
| 1665 | parsed_line.GetArgumentAtIndex(cursor_index) + cursor_char_position); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1666 | |
| 1667 | std::string common_prefix; |
| 1668 | matches.LongestCommonPrefix (common_prefix); |
| 1669 | int partial_name_len = command_partial_str.size(); |
| 1670 | |
| 1671 | // If we matched a unique single command, add a space... |
Jim Ingham | 802f8b0 | 2010-06-30 05:02:46 +0000 | [diff] [blame] | 1672 | // Only do this if the completer told us this was a complete word, however... |
| 1673 | if (num_command_matches == 1 && word_complete) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1674 | { |
| 1675 | char quote_char = parsed_line.GetArgumentQuoteCharAtIndex(cursor_index); |
| 1676 | if (quote_char != '\0') |
| 1677 | common_prefix.push_back(quote_char); |
| 1678 | |
| 1679 | common_prefix.push_back(' '); |
| 1680 | } |
| 1681 | common_prefix.erase (0, partial_name_len); |
| 1682 | matches.InsertStringAtIndex(0, common_prefix.c_str()); |
| 1683 | } |
| 1684 | return num_command_matches; |
| 1685 | } |
| 1686 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1687 | |
| 1688 | CommandInterpreter::~CommandInterpreter () |
| 1689 | { |
| 1690 | } |
| 1691 | |
| 1692 | const char * |
| 1693 | CommandInterpreter::GetPrompt () |
| 1694 | { |
Caroline Tice | 5bc8c97 | 2010-09-20 20:44:43 +0000 | [diff] [blame] | 1695 | return m_debugger.GetPrompt(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1696 | } |
| 1697 | |
| 1698 | void |
| 1699 | CommandInterpreter::SetPrompt (const char *new_prompt) |
| 1700 | { |
Caroline Tice | 5bc8c97 | 2010-09-20 20:44:43 +0000 | [diff] [blame] | 1701 | m_debugger.SetPrompt (new_prompt); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1702 | } |
| 1703 | |
Jim Ingham | 5e16ef5 | 2010-10-04 19:49:29 +0000 | [diff] [blame] | 1704 | size_t |
Greg Clayton | 5892856 | 2011-02-09 01:08:52 +0000 | [diff] [blame] | 1705 | CommandInterpreter::GetConfirmationInputReaderCallback |
| 1706 | ( |
| 1707 | void *baton, |
| 1708 | InputReader &reader, |
| 1709 | lldb::InputReaderAction action, |
| 1710 | const char *bytes, |
| 1711 | size_t bytes_len |
| 1712 | ) |
Jim Ingham | 5e16ef5 | 2010-10-04 19:49:29 +0000 | [diff] [blame] | 1713 | { |
Greg Clayton | 5892856 | 2011-02-09 01:08:52 +0000 | [diff] [blame] | 1714 | File &out_file = reader.GetDebugger().GetOutputFile(); |
Jim Ingham | 5e16ef5 | 2010-10-04 19:49:29 +0000 | [diff] [blame] | 1715 | bool *response_ptr = (bool *) baton; |
| 1716 | |
| 1717 | switch (action) |
| 1718 | { |
| 1719 | case eInputReaderActivate: |
Greg Clayton | 5892856 | 2011-02-09 01:08:52 +0000 | [diff] [blame] | 1720 | if (out_file.IsValid()) |
Jim Ingham | 5e16ef5 | 2010-10-04 19:49:29 +0000 | [diff] [blame] | 1721 | { |
| 1722 | if (reader.GetPrompt()) |
Caroline Tice | 22a6009 | 2011-02-02 01:17:56 +0000 | [diff] [blame] | 1723 | { |
Greg Clayton | 5892856 | 2011-02-09 01:08:52 +0000 | [diff] [blame] | 1724 | out_file.Printf ("%s", reader.GetPrompt()); |
| 1725 | out_file.Flush (); |
Caroline Tice | 22a6009 | 2011-02-02 01:17:56 +0000 | [diff] [blame] | 1726 | } |
Jim Ingham | 5e16ef5 | 2010-10-04 19:49:29 +0000 | [diff] [blame] | 1727 | } |
| 1728 | break; |
| 1729 | |
| 1730 | case eInputReaderDeactivate: |
| 1731 | break; |
| 1732 | |
| 1733 | case eInputReaderReactivate: |
Greg Clayton | 5892856 | 2011-02-09 01:08:52 +0000 | [diff] [blame] | 1734 | if (out_file.IsValid() && reader.GetPrompt()) |
Caroline Tice | 22a6009 | 2011-02-02 01:17:56 +0000 | [diff] [blame] | 1735 | { |
Greg Clayton | 5892856 | 2011-02-09 01:08:52 +0000 | [diff] [blame] | 1736 | out_file.Printf ("%s", reader.GetPrompt()); |
| 1737 | out_file.Flush (); |
Caroline Tice | 22a6009 | 2011-02-02 01:17:56 +0000 | [diff] [blame] | 1738 | } |
Jim Ingham | 5e16ef5 | 2010-10-04 19:49:29 +0000 | [diff] [blame] | 1739 | break; |
Caroline Tice | 4a34808 | 2011-05-02 20:41:46 +0000 | [diff] [blame] | 1740 | |
| 1741 | case eInputReaderAsynchronousOutputWritten: |
| 1742 | break; |
| 1743 | |
Jim Ingham | 5e16ef5 | 2010-10-04 19:49:29 +0000 | [diff] [blame] | 1744 | case eInputReaderGotToken: |
| 1745 | if (bytes_len == 0) |
| 1746 | { |
| 1747 | reader.SetIsDone(true); |
| 1748 | } |
| 1749 | else if (bytes[0] == 'y') |
| 1750 | { |
| 1751 | *response_ptr = true; |
| 1752 | reader.SetIsDone(true); |
| 1753 | } |
| 1754 | else if (bytes[0] == 'n') |
| 1755 | { |
| 1756 | *response_ptr = false; |
| 1757 | reader.SetIsDone(true); |
| 1758 | } |
| 1759 | else |
| 1760 | { |
Greg Clayton | 5892856 | 2011-02-09 01:08:52 +0000 | [diff] [blame] | 1761 | if (out_file.IsValid() && !reader.IsDone() && reader.GetPrompt()) |
Jim Ingham | 5e16ef5 | 2010-10-04 19:49:29 +0000 | [diff] [blame] | 1762 | { |
Greg Clayton | 5892856 | 2011-02-09 01:08:52 +0000 | [diff] [blame] | 1763 | out_file.Printf ("Please answer \"y\" or \"n\"\n%s", reader.GetPrompt()); |
| 1764 | out_file.Flush (); |
Jim Ingham | 5e16ef5 | 2010-10-04 19:49:29 +0000 | [diff] [blame] | 1765 | } |
| 1766 | } |
| 1767 | break; |
| 1768 | |
Caroline Tice | c4f55fe | 2010-11-19 20:47:54 +0000 | [diff] [blame] | 1769 | case eInputReaderInterrupt: |
| 1770 | case eInputReaderEndOfFile: |
| 1771 | *response_ptr = false; // Assume ^C or ^D means cancel the proposed action |
| 1772 | reader.SetIsDone (true); |
| 1773 | break; |
| 1774 | |
Jim Ingham | 5e16ef5 | 2010-10-04 19:49:29 +0000 | [diff] [blame] | 1775 | case eInputReaderDone: |
| 1776 | break; |
| 1777 | } |
| 1778 | |
| 1779 | return bytes_len; |
| 1780 | |
| 1781 | } |
| 1782 | |
| 1783 | bool |
| 1784 | CommandInterpreter::Confirm (const char *message, bool default_answer) |
| 1785 | { |
Jim Ingham | 9305747 | 2010-10-04 22:44:14 +0000 | [diff] [blame] | 1786 | // Check AutoConfirm first: |
| 1787 | if (m_debugger.GetAutoConfirm()) |
| 1788 | return default_answer; |
| 1789 | |
Jim Ingham | 5e16ef5 | 2010-10-04 19:49:29 +0000 | [diff] [blame] | 1790 | InputReaderSP reader_sp (new InputReader(GetDebugger())); |
| 1791 | bool response = default_answer; |
| 1792 | if (reader_sp) |
| 1793 | { |
| 1794 | std::string prompt(message); |
| 1795 | prompt.append(": ["); |
| 1796 | if (default_answer) |
| 1797 | prompt.append ("Y/n] "); |
| 1798 | else |
| 1799 | prompt.append ("y/N] "); |
| 1800 | |
| 1801 | Error err (reader_sp->Initialize (CommandInterpreter::GetConfirmationInputReaderCallback, |
| 1802 | &response, // baton |
| 1803 | eInputReaderGranularityLine, // token size, to pass to callback function |
| 1804 | NULL, // end token |
| 1805 | prompt.c_str(), // prompt |
| 1806 | true)); // echo input |
| 1807 | if (err.Success()) |
| 1808 | { |
| 1809 | GetDebugger().PushInputReader (reader_sp); |
| 1810 | } |
| 1811 | reader_sp->WaitOnReaderIsDone(); |
| 1812 | } |
| 1813 | return response; |
| 1814 | } |
| 1815 | |
| 1816 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1817 | void |
| 1818 | CommandInterpreter::CrossRegisterCommand (const char * dest_cmd, const char * object_type) |
| 1819 | { |
Jim Ingham | d40f8a6 | 2010-07-06 22:46:59 +0000 | [diff] [blame] | 1820 | CommandObjectSP cmd_obj_sp = GetCommandSPExact (dest_cmd, true); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1821 | |
| 1822 | if (cmd_obj_sp != NULL) |
| 1823 | { |
| 1824 | CommandObject *cmd_obj = cmd_obj_sp.get(); |
| 1825 | if (cmd_obj->IsCrossRefObject ()) |
| 1826 | cmd_obj->AddObject (object_type); |
| 1827 | } |
| 1828 | } |
| 1829 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1830 | OptionArgVectorSP |
| 1831 | CommandInterpreter::GetAliasOptions (const char *alias_name) |
| 1832 | { |
| 1833 | OptionArgMap::iterator pos; |
| 1834 | OptionArgVectorSP ret_val; |
| 1835 | |
| 1836 | std::string alias (alias_name); |
| 1837 | |
| 1838 | if (HasAliasOptions()) |
| 1839 | { |
| 1840 | pos = m_alias_options.find (alias); |
| 1841 | if (pos != m_alias_options.end()) |
| 1842 | ret_val = pos->second; |
| 1843 | } |
| 1844 | |
| 1845 | return ret_val; |
| 1846 | } |
| 1847 | |
| 1848 | void |
| 1849 | CommandInterpreter::RemoveAliasOptions (const char *alias_name) |
| 1850 | { |
| 1851 | OptionArgMap::iterator pos = m_alias_options.find(alias_name); |
| 1852 | if (pos != m_alias_options.end()) |
| 1853 | { |
| 1854 | m_alias_options.erase (pos); |
| 1855 | } |
| 1856 | } |
| 1857 | |
| 1858 | void |
| 1859 | CommandInterpreter::AddOrReplaceAliasOptions (const char *alias_name, OptionArgVectorSP &option_arg_vector_sp) |
| 1860 | { |
| 1861 | m_alias_options[alias_name] = option_arg_vector_sp; |
| 1862 | } |
| 1863 | |
| 1864 | bool |
| 1865 | CommandInterpreter::HasCommands () |
| 1866 | { |
| 1867 | return (!m_command_dict.empty()); |
| 1868 | } |
| 1869 | |
| 1870 | bool |
| 1871 | CommandInterpreter::HasAliases () |
| 1872 | { |
| 1873 | return (!m_alias_dict.empty()); |
| 1874 | } |
| 1875 | |
| 1876 | bool |
| 1877 | CommandInterpreter::HasUserCommands () |
| 1878 | { |
| 1879 | return (!m_user_dict.empty()); |
| 1880 | } |
| 1881 | |
| 1882 | bool |
| 1883 | CommandInterpreter::HasAliasOptions () |
| 1884 | { |
| 1885 | return (!m_alias_options.empty()); |
| 1886 | } |
| 1887 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1888 | void |
Caroline Tice | bd5c63e | 2010-10-12 21:57:09 +0000 | [diff] [blame] | 1889 | CommandInterpreter::BuildAliasCommandArgs (CommandObject *alias_cmd_obj, |
| 1890 | const char *alias_name, |
| 1891 | Args &cmd_args, |
Caroline Tice | 44c841d | 2010-12-07 19:58:26 +0000 | [diff] [blame] | 1892 | std::string &raw_input_string, |
Caroline Tice | bd5c63e | 2010-10-12 21:57:09 +0000 | [diff] [blame] | 1893 | CommandReturnObject &result) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1894 | { |
| 1895 | OptionArgVectorSP option_arg_vector_sp = GetAliasOptions (alias_name); |
Caroline Tice | 44c841d | 2010-12-07 19:58:26 +0000 | [diff] [blame] | 1896 | |
| 1897 | bool wants_raw_input = alias_cmd_obj->WantsRawCommandString(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1898 | |
Caroline Tice | 44c841d | 2010-12-07 19:58:26 +0000 | [diff] [blame] | 1899 | // Make sure that the alias name is the 0th element in cmd_args |
| 1900 | std::string alias_name_str = alias_name; |
| 1901 | if (alias_name_str.compare (cmd_args.GetArgumentAtIndex(0)) != 0) |
| 1902 | cmd_args.Unshift (alias_name); |
| 1903 | |
| 1904 | Args new_args (alias_cmd_obj->GetCommandName()); |
| 1905 | if (new_args.GetArgumentCount() == 2) |
| 1906 | new_args.Shift(); |
| 1907 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1908 | if (option_arg_vector_sp.get()) |
| 1909 | { |
Caroline Tice | 44c841d | 2010-12-07 19:58:26 +0000 | [diff] [blame] | 1910 | if (wants_raw_input) |
| 1911 | { |
| 1912 | // We have a command that both has command options and takes raw input. Make *sure* it has a |
| 1913 | // " -- " in the right place in the raw_input_string. |
| 1914 | size_t pos = raw_input_string.find(" -- "); |
| 1915 | if (pos == std::string::npos) |
| 1916 | { |
| 1917 | // None found; assume it goes at the beginning of the raw input string |
| 1918 | raw_input_string.insert (0, " -- "); |
| 1919 | } |
| 1920 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1921 | |
| 1922 | OptionArgVector *option_arg_vector = option_arg_vector_sp.get(); |
| 1923 | int old_size = cmd_args.GetArgumentCount(); |
Caroline Tice | bd5c63e | 2010-10-12 21:57:09 +0000 | [diff] [blame] | 1924 | std::vector<bool> used (old_size + 1, false); |
| 1925 | |
| 1926 | used[0] = true; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1927 | |
| 1928 | for (int i = 0; i < option_arg_vector->size(); ++i) |
| 1929 | { |
| 1930 | OptionArgPair option_pair = (*option_arg_vector)[i]; |
Caroline Tice | 44c841d | 2010-12-07 19:58:26 +0000 | [diff] [blame] | 1931 | OptionArgValue value_pair = option_pair.second; |
| 1932 | int value_type = value_pair.first; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1933 | std::string option = option_pair.first; |
Caroline Tice | 44c841d | 2010-12-07 19:58:26 +0000 | [diff] [blame] | 1934 | std::string value = value_pair.second; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1935 | if (option.compare ("<argument>") == 0) |
Caroline Tice | 44c841d | 2010-12-07 19:58:26 +0000 | [diff] [blame] | 1936 | { |
| 1937 | if (!wants_raw_input |
| 1938 | || (value.compare("--") != 0)) // Since we inserted this above, make sure we don't insert it twice |
| 1939 | new_args.AppendArgument (value.c_str()); |
| 1940 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1941 | else |
| 1942 | { |
Caroline Tice | 44c841d | 2010-12-07 19:58:26 +0000 | [diff] [blame] | 1943 | if (value_type != optional_argument) |
| 1944 | new_args.AppendArgument (option.c_str()); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1945 | if (value.compare ("<no-argument>") != 0) |
| 1946 | { |
| 1947 | int index = GetOptionArgumentPosition (value.c_str()); |
| 1948 | if (index == 0) |
Caroline Tice | 44c841d | 2010-12-07 19:58:26 +0000 | [diff] [blame] | 1949 | { |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1950 | // value was NOT a positional argument; must be a real value |
Caroline Tice | 44c841d | 2010-12-07 19:58:26 +0000 | [diff] [blame] | 1951 | if (value_type != optional_argument) |
| 1952 | new_args.AppendArgument (value.c_str()); |
| 1953 | else |
| 1954 | { |
| 1955 | char buffer[255]; |
| 1956 | ::snprintf (buffer, sizeof (buffer), "%s%s", option.c_str(), value.c_str()); |
| 1957 | new_args.AppendArgument (buffer); |
| 1958 | } |
| 1959 | |
| 1960 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1961 | else if (index >= cmd_args.GetArgumentCount()) |
| 1962 | { |
| 1963 | result.AppendErrorWithFormat |
| 1964 | ("Not enough arguments provided; you need at least %d arguments to use this alias.\n", |
| 1965 | index); |
| 1966 | result.SetStatus (eReturnStatusFailed); |
| 1967 | return; |
| 1968 | } |
| 1969 | else |
| 1970 | { |
Caroline Tice | 44c841d | 2010-12-07 19:58:26 +0000 | [diff] [blame] | 1971 | // Find and remove cmd_args.GetArgumentAtIndex(i) from raw_input_string |
| 1972 | size_t strpos = raw_input_string.find (cmd_args.GetArgumentAtIndex (index)); |
| 1973 | if (strpos != std::string::npos) |
| 1974 | { |
| 1975 | raw_input_string = raw_input_string.erase (strpos, strlen (cmd_args.GetArgumentAtIndex (index))); |
| 1976 | } |
| 1977 | |
| 1978 | if (value_type != optional_argument) |
| 1979 | new_args.AppendArgument (cmd_args.GetArgumentAtIndex (index)); |
| 1980 | else |
| 1981 | { |
| 1982 | char buffer[255]; |
| 1983 | ::snprintf (buffer, sizeof(buffer), "%s%s", option.c_str(), |
| 1984 | cmd_args.GetArgumentAtIndex (index)); |
| 1985 | new_args.AppendArgument (buffer); |
| 1986 | } |
Caroline Tice | bd5c63e | 2010-10-12 21:57:09 +0000 | [diff] [blame] | 1987 | used[index] = true; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1988 | } |
| 1989 | } |
| 1990 | } |
| 1991 | } |
| 1992 | |
| 1993 | for (int j = 0; j < cmd_args.GetArgumentCount(); ++j) |
| 1994 | { |
Caroline Tice | 44c841d | 2010-12-07 19:58:26 +0000 | [diff] [blame] | 1995 | if (!used[j] && !wants_raw_input) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1996 | new_args.AppendArgument (cmd_args.GetArgumentAtIndex (j)); |
| 1997 | } |
| 1998 | |
| 1999 | cmd_args.Clear(); |
| 2000 | cmd_args.SetArguments (new_args.GetArgumentCount(), (const char **) new_args.GetArgumentVector()); |
| 2001 | } |
| 2002 | else |
| 2003 | { |
| 2004 | result.SetStatus (eReturnStatusSuccessFinishNoResult); |
Caroline Tice | 44c841d | 2010-12-07 19:58:26 +0000 | [diff] [blame] | 2005 | // This alias was not created with any options; nothing further needs to be done, unless it is a command that |
| 2006 | // wants raw input, in which case we need to clear the rest of the data from cmd_args, since its in the raw |
| 2007 | // input string. |
| 2008 | if (wants_raw_input) |
| 2009 | { |
| 2010 | cmd_args.Clear(); |
| 2011 | cmd_args.SetArguments (new_args.GetArgumentCount(), (const char **) new_args.GetArgumentVector()); |
| 2012 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2013 | return; |
| 2014 | } |
| 2015 | |
| 2016 | result.SetStatus (eReturnStatusSuccessFinishNoResult); |
| 2017 | return; |
| 2018 | } |
| 2019 | |
| 2020 | |
| 2021 | int |
| 2022 | CommandInterpreter::GetOptionArgumentPosition (const char *in_string) |
| 2023 | { |
| 2024 | int position = 0; // Any string that isn't an argument position, i.e. '%' followed by an integer, gets a position |
| 2025 | // of zero. |
| 2026 | |
| 2027 | char *cptr = (char *) in_string; |
| 2028 | |
| 2029 | // Does it start with '%' |
| 2030 | if (cptr[0] == '%') |
| 2031 | { |
| 2032 | ++cptr; |
| 2033 | |
| 2034 | // Is the rest of it entirely digits? |
| 2035 | if (isdigit (cptr[0])) |
| 2036 | { |
| 2037 | const char *start = cptr; |
| 2038 | while (isdigit (cptr[0])) |
| 2039 | ++cptr; |
| 2040 | |
| 2041 | // We've gotten to the end of the digits; are we at the end of the string? |
| 2042 | if (cptr[0] == '\0') |
| 2043 | position = atoi (start); |
| 2044 | } |
| 2045 | } |
| 2046 | |
| 2047 | return position; |
| 2048 | } |
| 2049 | |
| 2050 | void |
| 2051 | CommandInterpreter::SourceInitFile (bool in_cwd, CommandReturnObject &result) |
| 2052 | { |
Jim Ingham | 574c3d6 | 2011-08-12 23:34:31 +0000 | [diff] [blame] | 2053 | FileSpec init_file; |
Greg Clayton | d6edcb5 | 2011-09-11 00:01:44 +0000 | [diff] [blame] | 2054 | if (in_cwd) |
Jim Ingham | 574c3d6 | 2011-08-12 23:34:31 +0000 | [diff] [blame] | 2055 | { |
Greg Clayton | d6edcb5 | 2011-09-11 00:01:44 +0000 | [diff] [blame] | 2056 | // In the current working directory we don't load any program specific |
| 2057 | // .lldbinit files, we only look for a "./.lldbinit" file. |
| 2058 | if (m_skip_lldbinit_files) |
| 2059 | return; |
| 2060 | |
| 2061 | init_file.SetFile ("./.lldbinit", true); |
Jim Ingham | 574c3d6 | 2011-08-12 23:34:31 +0000 | [diff] [blame] | 2062 | } |
Greg Clayton | d6edcb5 | 2011-09-11 00:01:44 +0000 | [diff] [blame] | 2063 | else |
Jim Ingham | 574c3d6 | 2011-08-12 23:34:31 +0000 | [diff] [blame] | 2064 | { |
Greg Clayton | d6edcb5 | 2011-09-11 00:01:44 +0000 | [diff] [blame] | 2065 | // If we aren't looking in the current working directory we are looking |
| 2066 | // in the home directory. We will first see if there is an application |
| 2067 | // specific ".lldbinit" file whose name is "~/.lldbinit" followed by a |
| 2068 | // "-" and the name of the program. If this file doesn't exist, we fall |
| 2069 | // back to just the "~/.lldbinit" file. We also obey any requests to not |
| 2070 | // load the init files. |
| 2071 | const char *init_file_path = "~/.lldbinit"; |
| 2072 | |
| 2073 | if (m_skip_app_init_files == false) |
| 2074 | { |
| 2075 | FileSpec program_file_spec (Host::GetProgramFileSpec()); |
| 2076 | const char *program_name = program_file_spec.GetFilename().AsCString(); |
Jim Ingham | 574c3d6 | 2011-08-12 23:34:31 +0000 | [diff] [blame] | 2077 | |
Greg Clayton | d6edcb5 | 2011-09-11 00:01:44 +0000 | [diff] [blame] | 2078 | if (program_name) |
| 2079 | { |
| 2080 | char program_init_file_name[PATH_MAX]; |
| 2081 | ::snprintf (program_init_file_name, sizeof(program_init_file_name), "%s-%s", init_file_path, program_name); |
| 2082 | init_file.SetFile (program_init_file_name, true); |
| 2083 | if (!init_file.Exists()) |
| 2084 | init_file.Clear(); |
| 2085 | } |
| 2086 | } |
| 2087 | |
| 2088 | if (!init_file && !m_skip_lldbinit_files) |
| 2089 | init_file.SetFile (init_file_path, true); |
| 2090 | } |
| 2091 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2092 | // If the file exists, tell HandleCommand to 'source' it; this will do the actual broadcasting |
| 2093 | // of the commands back to any appropriate listener (see CommandObjectSource::Execute for more details). |
| 2094 | |
| 2095 | if (init_file.Exists()) |
| 2096 | { |
Jim Ingham | 949d5ac | 2011-02-18 00:54:25 +0000 | [diff] [blame] | 2097 | ExecutionContext *exe_ctx = NULL; // We don't have any context yet. |
| 2098 | bool stop_on_continue = true; |
| 2099 | bool stop_on_error = false; |
| 2100 | bool echo_commands = false; |
| 2101 | bool print_results = false; |
| 2102 | |
| 2103 | HandleCommandsFromFile (init_file, exe_ctx, stop_on_continue, stop_on_error, echo_commands, print_results, result); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2104 | } |
| 2105 | else |
| 2106 | { |
| 2107 | // nothing to be done if the file doesn't exist |
| 2108 | result.SetStatus(eReturnStatusSuccessFinishNoResult); |
| 2109 | } |
| 2110 | } |
| 2111 | |
Greg Clayton | b72d0f0 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 2112 | PlatformSP |
| 2113 | CommandInterpreter::GetPlatform (bool prefer_target_platform) |
| 2114 | { |
| 2115 | PlatformSP platform_sp; |
Greg Clayton | 567e7f3 | 2011-09-22 04:58:26 +0000 | [diff] [blame] | 2116 | if (prefer_target_platform) |
| 2117 | { |
| 2118 | Target *target = m_exe_ctx.GetTargetPtr(); |
| 2119 | if (target) |
| 2120 | platform_sp = target->GetPlatform(); |
| 2121 | } |
Greg Clayton | b72d0f0 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 2122 | |
| 2123 | if (!platform_sp) |
| 2124 | platform_sp = m_debugger.GetPlatformList().GetSelectedPlatform(); |
| 2125 | return platform_sp; |
| 2126 | } |
| 2127 | |
Jim Ingham | 949d5ac | 2011-02-18 00:54:25 +0000 | [diff] [blame] | 2128 | void |
Jim Ingham | a4fede3 | 2011-03-11 01:51:49 +0000 | [diff] [blame] | 2129 | CommandInterpreter::HandleCommands (const StringList &commands, |
Jim Ingham | 949d5ac | 2011-02-18 00:54:25 +0000 | [diff] [blame] | 2130 | ExecutionContext *override_context, |
| 2131 | bool stop_on_continue, |
| 2132 | bool stop_on_error, |
| 2133 | bool echo_commands, |
| 2134 | bool print_results, |
| 2135 | CommandReturnObject &result) |
| 2136 | { |
| 2137 | size_t num_lines = commands.GetSize(); |
Jim Ingham | 949d5ac | 2011-02-18 00:54:25 +0000 | [diff] [blame] | 2138 | |
| 2139 | // If we are going to continue past a "continue" then we need to run the commands synchronously. |
| 2140 | // Make sure you reset this value anywhere you return from the function. |
| 2141 | |
| 2142 | bool old_async_execution = m_debugger.GetAsyncExecution(); |
| 2143 | |
| 2144 | // If we've been given an execution context, set it at the start, but don't keep resetting it or we will |
| 2145 | // cause series of commands that change the context, then do an operation that relies on that context to fail. |
| 2146 | |
| 2147 | if (override_context != NULL) |
Greg Clayton | b72d0f0 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 2148 | UpdateExecutionContext (override_context); |
Jim Ingham | 949d5ac | 2011-02-18 00:54:25 +0000 | [diff] [blame] | 2149 | |
| 2150 | if (!stop_on_continue) |
| 2151 | { |
| 2152 | m_debugger.SetAsyncExecution (false); |
| 2153 | } |
| 2154 | |
| 2155 | for (int idx = 0; idx < num_lines; idx++) |
| 2156 | { |
| 2157 | const char *cmd = commands.GetStringAtIndex(idx); |
| 2158 | if (cmd[0] == '\0') |
| 2159 | continue; |
| 2160 | |
Jim Ingham | 949d5ac | 2011-02-18 00:54:25 +0000 | [diff] [blame] | 2161 | if (echo_commands) |
| 2162 | { |
| 2163 | result.AppendMessageWithFormat ("%s %s\n", |
| 2164 | GetPrompt(), |
| 2165 | cmd); |
| 2166 | } |
| 2167 | |
Greg Clayton | aa378b1 | 2011-02-20 02:15:07 +0000 | [diff] [blame] | 2168 | CommandReturnObject tmp_result; |
Johnny Chen | 8bdf57c | 2011-10-05 00:42:59 +0000 | [diff] [blame] | 2169 | // If override_context is not NULL, pass no_context_switching = true for |
| 2170 | // HandleCommand() since we updated our context already. |
| 2171 | bool success = HandleCommand(cmd, false, tmp_result, |
| 2172 | NULL, /* override_context */ |
| 2173 | true, /* repeat_on_empty_command */ |
| 2174 | override_context != NULL /* no_context_switching */); |
Jim Ingham | 949d5ac | 2011-02-18 00:54:25 +0000 | [diff] [blame] | 2175 | |
| 2176 | if (print_results) |
| 2177 | { |
| 2178 | if (tmp_result.Succeeded()) |
Jim Ingham | 2e8cb8a | 2011-02-19 02:53:09 +0000 | [diff] [blame] | 2179 | result.AppendMessageWithFormat("%s", tmp_result.GetOutputData()); |
Jim Ingham | 949d5ac | 2011-02-18 00:54:25 +0000 | [diff] [blame] | 2180 | } |
| 2181 | |
| 2182 | if (!success || !tmp_result.Succeeded()) |
| 2183 | { |
| 2184 | if (stop_on_error) |
| 2185 | { |
| 2186 | result.AppendErrorWithFormat("Aborting reading of commands after command #%d: '%s' failed.\n", |
| 2187 | idx, cmd); |
| 2188 | result.SetStatus (eReturnStatusFailed); |
| 2189 | m_debugger.SetAsyncExecution (old_async_execution); |
| 2190 | return; |
| 2191 | } |
| 2192 | else if (print_results) |
| 2193 | { |
| 2194 | result.AppendMessageWithFormat ("Command #%d '%s' failed with error: %s.\n", |
| 2195 | idx + 1, |
| 2196 | cmd, |
Jim Ingham | 2e8cb8a | 2011-02-19 02:53:09 +0000 | [diff] [blame] | 2197 | tmp_result.GetErrorData()); |
Jim Ingham | 949d5ac | 2011-02-18 00:54:25 +0000 | [diff] [blame] | 2198 | } |
| 2199 | } |
| 2200 | |
Caroline Tice | 4a34808 | 2011-05-02 20:41:46 +0000 | [diff] [blame] | 2201 | if (result.GetImmediateOutputStream()) |
| 2202 | result.GetImmediateOutputStream()->Flush(); |
| 2203 | |
| 2204 | if (result.GetImmediateErrorStream()) |
| 2205 | result.GetImmediateErrorStream()->Flush(); |
| 2206 | |
Jim Ingham | 949d5ac | 2011-02-18 00:54:25 +0000 | [diff] [blame] | 2207 | // N.B. Can't depend on DidChangeProcessState, because the state coming into the command execution |
| 2208 | // could be running (for instance in Breakpoint Commands. |
| 2209 | // So we check the return value to see if it is has running in it. |
| 2210 | if ((tmp_result.GetStatus() == eReturnStatusSuccessContinuingNoResult) |
| 2211 | || (tmp_result.GetStatus() == eReturnStatusSuccessContinuingResult)) |
| 2212 | { |
| 2213 | if (stop_on_continue) |
| 2214 | { |
| 2215 | // If we caused the target to proceed, and we're going to stop in that case, set the |
| 2216 | // status in our real result before returning. This is an error if the continue was not the |
| 2217 | // last command in the set of commands to be run. |
| 2218 | if (idx != num_lines - 1) |
| 2219 | result.AppendErrorWithFormat("Aborting reading of commands after command #%d: '%s' continued the target.\n", |
| 2220 | idx + 1, cmd); |
| 2221 | else |
| 2222 | result.AppendMessageWithFormat ("Command #%d '%s' continued the target.\n", idx + 1, cmd); |
| 2223 | |
| 2224 | result.SetStatus(tmp_result.GetStatus()); |
| 2225 | m_debugger.SetAsyncExecution (old_async_execution); |
| 2226 | |
| 2227 | return; |
| 2228 | } |
| 2229 | } |
| 2230 | |
| 2231 | } |
| 2232 | |
| 2233 | result.SetStatus (eReturnStatusSuccessFinishResult); |
| 2234 | m_debugger.SetAsyncExecution (old_async_execution); |
| 2235 | |
| 2236 | return; |
| 2237 | } |
| 2238 | |
| 2239 | void |
| 2240 | CommandInterpreter::HandleCommandsFromFile (FileSpec &cmd_file, |
| 2241 | ExecutionContext *context, |
| 2242 | bool stop_on_continue, |
| 2243 | bool stop_on_error, |
| 2244 | bool echo_command, |
| 2245 | bool print_result, |
| 2246 | CommandReturnObject &result) |
| 2247 | { |
| 2248 | if (cmd_file.Exists()) |
| 2249 | { |
| 2250 | bool success; |
| 2251 | StringList commands; |
| 2252 | success = commands.ReadFileLines(cmd_file); |
| 2253 | if (!success) |
| 2254 | { |
| 2255 | result.AppendErrorWithFormat ("Error reading commands from file: %s.\n", cmd_file.GetFilename().AsCString()); |
| 2256 | result.SetStatus (eReturnStatusFailed); |
| 2257 | return; |
| 2258 | } |
| 2259 | HandleCommands (commands, context, stop_on_continue, stop_on_error, echo_command, print_result, result); |
| 2260 | } |
| 2261 | else |
| 2262 | { |
| 2263 | result.AppendErrorWithFormat ("Error reading commands from file %s - file not found.\n", |
| 2264 | cmd_file.GetFilename().AsCString()); |
| 2265 | result.SetStatus (eReturnStatusFailed); |
| 2266 | return; |
| 2267 | } |
| 2268 | } |
| 2269 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2270 | ScriptInterpreter * |
| 2271 | CommandInterpreter::GetScriptInterpreter () |
| 2272 | { |
Caroline Tice | 0aa2e55 | 2011-01-14 00:29:16 +0000 | [diff] [blame] | 2273 | if (m_script_interpreter_ap.get() != NULL) |
| 2274 | return m_script_interpreter_ap.get(); |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 2275 | |
Caroline Tice | 0aa2e55 | 2011-01-14 00:29:16 +0000 | [diff] [blame] | 2276 | lldb::ScriptLanguage script_lang = GetDebugger().GetScriptLanguage(); |
| 2277 | switch (script_lang) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2278 | { |
Caroline Tice | 0aa2e55 | 2011-01-14 00:29:16 +0000 | [diff] [blame] | 2279 | case eScriptLanguageNone: |
| 2280 | m_script_interpreter_ap.reset (new ScriptInterpreterNone (*this)); |
| 2281 | break; |
| 2282 | case eScriptLanguagePython: |
| 2283 | m_script_interpreter_ap.reset (new ScriptInterpreterPython (*this)); |
| 2284 | break; |
| 2285 | default: |
| 2286 | break; |
| 2287 | }; |
| 2288 | |
| 2289 | return m_script_interpreter_ap.get(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2290 | } |
| 2291 | |
| 2292 | |
| 2293 | |
| 2294 | bool |
| 2295 | CommandInterpreter::GetSynchronous () |
| 2296 | { |
| 2297 | return m_synchronous_execution; |
| 2298 | } |
| 2299 | |
| 2300 | void |
| 2301 | CommandInterpreter::SetSynchronous (bool value) |
| 2302 | { |
Johnny Chen | d7a4eb0 | 2010-10-14 01:22:03 +0000 | [diff] [blame] | 2303 | m_synchronous_execution = value; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2304 | } |
| 2305 | |
| 2306 | void |
| 2307 | CommandInterpreter::OutputFormattedHelpText (Stream &strm, |
| 2308 | const char *word_text, |
| 2309 | const char *separator, |
| 2310 | const char *help_text, |
| 2311 | uint32_t max_word_len) |
| 2312 | { |
Greg Clayton | 238c0a1 | 2010-09-18 01:14:36 +0000 | [diff] [blame] | 2313 | const uint32_t max_columns = m_debugger.GetTerminalWidth(); |
| 2314 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2315 | int indent_size = max_word_len + strlen (separator) + 2; |
| 2316 | |
| 2317 | strm.IndentMore (indent_size); |
Greg Clayton | d284b66 | 2011-02-18 01:44:25 +0000 | [diff] [blame] | 2318 | |
| 2319 | StreamString text_strm; |
| 2320 | text_strm.Printf ("%-*s %s %s", max_word_len, word_text, separator, help_text); |
| 2321 | |
| 2322 | size_t len = text_strm.GetSize(); |
| 2323 | const char *text = text_strm.GetData(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2324 | if (text[len - 1] == '\n') |
Greg Clayton | d284b66 | 2011-02-18 01:44:25 +0000 | [diff] [blame] | 2325 | { |
| 2326 | text_strm.EOL(); |
| 2327 | len = text_strm.GetSize(); |
| 2328 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2329 | |
| 2330 | if (len < max_columns) |
| 2331 | { |
| 2332 | // Output it as a single line. |
| 2333 | strm.Printf ("%s", text); |
| 2334 | } |
| 2335 | else |
| 2336 | { |
| 2337 | // We need to break it up into multiple lines. |
| 2338 | bool first_line = true; |
| 2339 | int text_width; |
| 2340 | int start = 0; |
| 2341 | int end = start; |
| 2342 | int final_end = strlen (text); |
| 2343 | int sub_len; |
| 2344 | |
| 2345 | while (end < final_end) |
| 2346 | { |
| 2347 | if (first_line) |
| 2348 | text_width = max_columns - 1; |
| 2349 | else |
| 2350 | text_width = max_columns - indent_size - 1; |
| 2351 | |
| 2352 | // Don't start the 'text' on a space, since we're already outputting the indentation. |
| 2353 | if (!first_line) |
| 2354 | { |
| 2355 | while ((start < final_end) && (text[start] == ' ')) |
| 2356 | start++; |
| 2357 | } |
| 2358 | |
| 2359 | end = start + text_width; |
| 2360 | if (end > final_end) |
| 2361 | end = final_end; |
| 2362 | else |
| 2363 | { |
| 2364 | // If we're not at the end of the text, make sure we break the line on white space. |
| 2365 | while (end > start |
| 2366 | && text[end] != ' ' && text[end] != '\t' && text[end] != '\n') |
| 2367 | end--; |
| 2368 | } |
| 2369 | |
| 2370 | sub_len = end - start; |
| 2371 | if (start != 0) |
| 2372 | strm.EOL(); |
| 2373 | if (!first_line) |
| 2374 | strm.Indent(); |
| 2375 | else |
| 2376 | first_line = false; |
| 2377 | assert (start <= final_end); |
| 2378 | assert (start + sub_len <= final_end); |
| 2379 | if (sub_len > 0) |
| 2380 | strm.Write (text + start, sub_len); |
| 2381 | start = end + 1; |
| 2382 | } |
| 2383 | } |
| 2384 | strm.EOL(); |
| 2385 | strm.IndentLess(indent_size); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2386 | } |
| 2387 | |
| 2388 | void |
Enrico Granata | 1bba6e5 | 2011-07-07 00:38:40 +0000 | [diff] [blame] | 2389 | CommandInterpreter::OutputHelpText (Stream &strm, |
| 2390 | const char *word_text, |
| 2391 | const char *separator, |
| 2392 | const char *help_text, |
| 2393 | uint32_t max_word_len) |
| 2394 | { |
| 2395 | int indent_size = max_word_len + strlen (separator) + 2; |
| 2396 | |
| 2397 | strm.IndentMore (indent_size); |
| 2398 | |
| 2399 | StreamString text_strm; |
| 2400 | text_strm.Printf ("%-*s %s %s", max_word_len, word_text, separator, help_text); |
| 2401 | |
| 2402 | const uint32_t max_columns = m_debugger.GetTerminalWidth(); |
| 2403 | bool first_line = true; |
| 2404 | |
| 2405 | size_t len = text_strm.GetSize(); |
| 2406 | const char *text = text_strm.GetData(); |
| 2407 | |
| 2408 | uint32_t chars_left = max_columns; |
| 2409 | |
| 2410 | for (uint32_t i = 0; i < len; i++) |
| 2411 | { |
| 2412 | if ((text[i] == ' ' && ::strchr((text+i+1), ' ') && chars_left < ::strchr((text+i+1), ' ')-(text+i)) || text[i] == '\n') |
| 2413 | { |
| 2414 | first_line = false; |
| 2415 | chars_left = max_columns - indent_size; |
| 2416 | strm.EOL(); |
| 2417 | strm.Indent(); |
| 2418 | } |
| 2419 | else |
| 2420 | { |
| 2421 | strm.PutChar(text[i]); |
| 2422 | chars_left--; |
| 2423 | } |
| 2424 | |
| 2425 | } |
| 2426 | |
| 2427 | strm.EOL(); |
| 2428 | strm.IndentLess(indent_size); |
| 2429 | } |
| 2430 | |
| 2431 | void |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2432 | CommandInterpreter::AproposAllSubCommands (CommandObject *cmd_obj, const char *prefix, const char *search_word, |
| 2433 | StringList &commands_found, StringList &commands_help) |
| 2434 | { |
| 2435 | CommandObject::CommandMap::const_iterator pos; |
| 2436 | CommandObject::CommandMap sub_cmd_dict = ((CommandObjectMultiword *) cmd_obj)->m_subcommand_dict; |
| 2437 | CommandObject *sub_cmd_obj; |
| 2438 | |
| 2439 | for (pos = sub_cmd_dict.begin(); pos != sub_cmd_dict.end(); ++pos) |
| 2440 | { |
| 2441 | const char * command_name = pos->first.c_str(); |
| 2442 | sub_cmd_obj = pos->second.get(); |
| 2443 | StreamString complete_command_name; |
| 2444 | |
| 2445 | complete_command_name.Printf ("%s %s", prefix, command_name); |
| 2446 | |
Greg Clayton | 238c0a1 | 2010-09-18 01:14:36 +0000 | [diff] [blame] | 2447 | if (sub_cmd_obj->HelpTextContainsWord (search_word)) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2448 | { |
| 2449 | commands_found.AppendString (complete_command_name.GetData()); |
| 2450 | commands_help.AppendString (sub_cmd_obj->GetHelp()); |
| 2451 | } |
| 2452 | |
| 2453 | if (sub_cmd_obj->IsMultiwordObject()) |
| 2454 | AproposAllSubCommands (sub_cmd_obj, complete_command_name.GetData(), search_word, commands_found, |
| 2455 | commands_help); |
| 2456 | } |
| 2457 | |
| 2458 | } |
| 2459 | |
| 2460 | void |
| 2461 | CommandInterpreter::FindCommandsForApropos (const char *search_word, StringList &commands_found, |
| 2462 | StringList &commands_help) |
| 2463 | { |
| 2464 | CommandObject::CommandMap::const_iterator pos; |
| 2465 | |
| 2466 | for (pos = m_command_dict.begin(); pos != m_command_dict.end(); ++pos) |
| 2467 | { |
| 2468 | const char *command_name = pos->first.c_str(); |
| 2469 | CommandObject *cmd_obj = pos->second.get(); |
| 2470 | |
Greg Clayton | 238c0a1 | 2010-09-18 01:14:36 +0000 | [diff] [blame] | 2471 | if (cmd_obj->HelpTextContainsWord (search_word)) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2472 | { |
| 2473 | commands_found.AppendString (command_name); |
| 2474 | commands_help.AppendString (cmd_obj->GetHelp()); |
| 2475 | } |
| 2476 | |
| 2477 | if (cmd_obj->IsMultiwordObject()) |
| 2478 | AproposAllSubCommands (cmd_obj, command_name, search_word, commands_found, commands_help); |
| 2479 | |
| 2480 | } |
| 2481 | } |
Greg Clayton | b72d0f0 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 2482 | |
| 2483 | |
| 2484 | void |
| 2485 | CommandInterpreter::UpdateExecutionContext (ExecutionContext *override_context) |
| 2486 | { |
| 2487 | m_exe_ctx.Clear(); |
| 2488 | |
| 2489 | if (override_context != NULL) |
| 2490 | { |
Greg Clayton | 567e7f3 | 2011-09-22 04:58:26 +0000 | [diff] [blame] | 2491 | m_exe_ctx = *override_context; |
Greg Clayton | b72d0f0 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 2492 | } |
| 2493 | else |
| 2494 | { |
| 2495 | TargetSP target_sp (m_debugger.GetSelectedTarget()); |
| 2496 | if (target_sp) |
| 2497 | { |
Greg Clayton | 567e7f3 | 2011-09-22 04:58:26 +0000 | [diff] [blame] | 2498 | m_exe_ctx.SetTargetSP (target_sp); |
| 2499 | ProcessSP process_sp (target_sp->GetProcessSP()); |
| 2500 | m_exe_ctx.SetProcessSP (process_sp); |
| 2501 | if (process_sp && process_sp->IsAlive() && !process_sp->IsRunning()) |
Greg Clayton | b72d0f0 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 2502 | { |
Greg Clayton | 567e7f3 | 2011-09-22 04:58:26 +0000 | [diff] [blame] | 2503 | ThreadSP thread_sp (process_sp->GetThreadList().GetSelectedThread().get()); |
| 2504 | if (thread_sp) |
Greg Clayton | b72d0f0 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 2505 | { |
Greg Clayton | 567e7f3 | 2011-09-22 04:58:26 +0000 | [diff] [blame] | 2506 | m_exe_ctx.SetThreadSP (thread_sp); |
| 2507 | StackFrameSP frame_sp (thread_sp->GetSelectedFrame()); |
| 2508 | if (!frame_sp) |
Greg Clayton | b72d0f0 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 2509 | { |
Greg Clayton | 567e7f3 | 2011-09-22 04:58:26 +0000 | [diff] [blame] | 2510 | frame_sp = thread_sp->GetStackFrameAtIndex (0); |
Greg Clayton | b72d0f0 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 2511 | // If we didn't have a selected frame select one here. |
Greg Clayton | 567e7f3 | 2011-09-22 04:58:26 +0000 | [diff] [blame] | 2512 | if (frame_sp) |
| 2513 | thread_sp->SetSelectedFrame(frame_sp.get()); |
Greg Clayton | b72d0f0 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 2514 | } |
Greg Clayton | 567e7f3 | 2011-09-22 04:58:26 +0000 | [diff] [blame] | 2515 | if (frame_sp) |
| 2516 | m_exe_ctx.SetFrameSP (frame_sp); |
Greg Clayton | b72d0f0 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 2517 | } |
| 2518 | } |
| 2519 | } |
| 2520 | } |
| 2521 | } |
| 2522 | |
Jim Ingham | 6247dbe | 2011-07-12 03:12:18 +0000 | [diff] [blame] | 2523 | void |
| 2524 | CommandInterpreter::DumpHistory (Stream &stream, uint32_t count) const |
| 2525 | { |
| 2526 | DumpHistory (stream, 0, count - 1); |
| 2527 | } |
| 2528 | |
| 2529 | void |
| 2530 | CommandInterpreter::DumpHistory (Stream &stream, uint32_t start, uint32_t end) const |
| 2531 | { |
Greg Clayton | 7268b4c | 2011-10-28 21:38:01 +0000 | [diff] [blame] | 2532 | const size_t last_idx = std::min<size_t>(m_command_history.size(), end + 1); |
| 2533 | for (size_t i = start; i < last_idx; i++) |
Jim Ingham | 6247dbe | 2011-07-12 03:12:18 +0000 | [diff] [blame] | 2534 | { |
| 2535 | if (!m_command_history[i].empty()) |
| 2536 | { |
| 2537 | stream.Indent(); |
Greg Clayton | 7268b4c | 2011-10-28 21:38:01 +0000 | [diff] [blame] | 2538 | stream.Printf ("%4zu: %s\n", i, m_command_history[i].c_str()); |
Jim Ingham | 6247dbe | 2011-07-12 03:12:18 +0000 | [diff] [blame] | 2539 | } |
| 2540 | } |
| 2541 | } |
| 2542 | |
| 2543 | const char * |
| 2544 | CommandInterpreter::FindHistoryString (const char *input_str) const |
| 2545 | { |
| 2546 | if (input_str[0] != m_repeat_char) |
| 2547 | return NULL; |
| 2548 | if (input_str[1] == '-') |
| 2549 | { |
| 2550 | bool success; |
| 2551 | uint32_t idx = Args::StringToUInt32 (input_str+2, 0, 0, &success); |
| 2552 | if (!success) |
| 2553 | return NULL; |
| 2554 | if (idx > m_command_history.size()) |
| 2555 | return NULL; |
| 2556 | idx = m_command_history.size() - idx; |
| 2557 | return m_command_history[idx].c_str(); |
| 2558 | |
| 2559 | } |
| 2560 | else if (input_str[1] == m_repeat_char) |
| 2561 | { |
| 2562 | if (m_command_history.empty()) |
| 2563 | return NULL; |
| 2564 | else |
| 2565 | return m_command_history.back().c_str(); |
| 2566 | } |
| 2567 | else |
| 2568 | { |
| 2569 | bool success; |
| 2570 | uint32_t idx = Args::StringToUInt32 (input_str+1, 0, 0, &success); |
| 2571 | if (!success) |
| 2572 | return NULL; |
| 2573 | if (idx >= m_command_history.size()) |
| 2574 | return NULL; |
| 2575 | return m_command_history[idx].c_str(); |
| 2576 | } |
| 2577 | } |