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