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