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