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. |
| 853 | |
| 854 | if (command_obj == NULL) |
| 855 | { |
| 856 | command_obj = GetCommandSP (cmd_cstr, true, true, matches).get(); |
| 857 | } |
| 858 | |
Matt Kopec | 038ff81 | 2013-04-23 16:17:32 +0000 | [diff] [blame] | 859 | // If there wasn't an exact match among the aliases, look for an inexact match |
| 860 | // in just the commands. |
Jim Ingham | 279a6c2 | 2010-07-06 22:46:59 +0000 | [diff] [blame] | 861 | |
| 862 | if (command_obj == NULL) |
Matt Kopec | 038ff81 | 2013-04-23 16:17:32 +0000 | [diff] [blame] | 863 | command_obj = GetCommandSP(cmd_cstr, false, false, matches).get(); |
| 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. |
| 867 | if (command_obj == NULL) |
Jim Ingham | 279a6c2 | 2010-07-06 22:46:59 +0000 | [diff] [blame] | 868 | command_obj = GetCommandSP(cmd_cstr, true, false, matches).get(); |
| 869 | |
| 870 | return command_obj; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 871 | } |
| 872 | |
| 873 | bool |
| 874 | CommandInterpreter::CommandExists (const char *cmd) |
| 875 | { |
| 876 | return m_command_dict.find(cmd) != m_command_dict.end(); |
| 877 | } |
| 878 | |
| 879 | bool |
Caroline Tice | ca90c47 | 2011-05-06 21:37:15 +0000 | [diff] [blame] | 880 | CommandInterpreter::ProcessAliasOptionsArgs (lldb::CommandObjectSP &cmd_obj_sp, |
| 881 | const char *options_args, |
| 882 | OptionArgVectorSP &option_arg_vector_sp) |
| 883 | { |
| 884 | bool success = true; |
| 885 | OptionArgVector *option_arg_vector = option_arg_vector_sp.get(); |
| 886 | |
| 887 | if (!options_args || (strlen (options_args) < 1)) |
| 888 | return true; |
| 889 | |
| 890 | std::string options_string (options_args); |
| 891 | Args args (options_args); |
| 892 | CommandReturnObject result; |
| 893 | // Check to see if the command being aliased can take any command options. |
| 894 | Options *options = cmd_obj_sp->GetOptions (); |
| 895 | if (options) |
| 896 | { |
| 897 | // See if any options were specified as part of the alias; if so, handle them appropriately. |
| 898 | options->NotifyOptionParsingStarting (); |
| 899 | args.Unshift ("dummy_arg"); |
| 900 | args.ParseAliasOptions (*options, result, option_arg_vector, options_string); |
| 901 | args.Shift (); |
| 902 | if (result.Succeeded()) |
| 903 | options->VerifyPartialOptions (result); |
| 904 | if (!result.Succeeded() && result.GetStatus() != lldb::eReturnStatusStarted) |
| 905 | { |
| 906 | result.AppendError ("Unable to create requested alias.\n"); |
| 907 | return false; |
| 908 | } |
| 909 | } |
| 910 | |
Greg Clayton | 5521f99 | 2011-10-28 21:38:01 +0000 | [diff] [blame] | 911 | if (!options_string.empty()) |
Caroline Tice | ca90c47 | 2011-05-06 21:37:15 +0000 | [diff] [blame] | 912 | { |
| 913 | if (cmd_obj_sp->WantsRawCommandString ()) |
| 914 | option_arg_vector->push_back (OptionArgPair ("<argument>", |
| 915 | OptionArgValue (-1, |
| 916 | options_string))); |
| 917 | else |
| 918 | { |
Greg Clayton | c7bece56 | 2013-01-25 18:06:21 +0000 | [diff] [blame] | 919 | const size_t argc = args.GetArgumentCount(); |
Caroline Tice | ca90c47 | 2011-05-06 21:37:15 +0000 | [diff] [blame] | 920 | for (size_t i = 0; i < argc; ++i) |
| 921 | if (strcmp (args.GetArgumentAtIndex (i), "") != 0) |
| 922 | option_arg_vector->push_back |
| 923 | (OptionArgPair ("<argument>", |
| 924 | OptionArgValue (-1, |
| 925 | std::string (args.GetArgumentAtIndex (i))))); |
| 926 | } |
| 927 | } |
| 928 | |
| 929 | return success; |
| 930 | } |
| 931 | |
| 932 | bool |
Jim Ingham | 298f378 | 2013-04-03 00:25:49 +0000 | [diff] [blame] | 933 | CommandInterpreter::GetAliasFullName (const char *cmd, std::string &full_name) |
| 934 | { |
| 935 | bool exact_match = (m_alias_dict.find(cmd) != m_alias_dict.end()); |
| 936 | if (exact_match) |
| 937 | { |
| 938 | full_name.assign(cmd); |
| 939 | return exact_match; |
| 940 | } |
| 941 | else |
| 942 | { |
| 943 | StringList matches; |
| 944 | size_t num_alias_matches; |
| 945 | num_alias_matches = CommandObject::AddNamesMatchingPartialString (m_alias_dict, cmd, matches); |
| 946 | if (num_alias_matches == 1) |
| 947 | { |
| 948 | // Make sure this isn't shadowing a command in the regular command space: |
| 949 | StringList regular_matches; |
| 950 | const bool include_aliases = false; |
| 951 | const bool exact = false; |
| 952 | CommandObjectSP cmd_obj_sp(GetCommandSP (cmd, include_aliases, exact, ®ular_matches)); |
| 953 | if (cmd_obj_sp || regular_matches.GetSize() > 0) |
| 954 | return false; |
| 955 | else |
| 956 | { |
| 957 | full_name.assign (matches.GetStringAtIndex(0)); |
| 958 | return true; |
| 959 | } |
| 960 | } |
| 961 | else |
| 962 | return false; |
| 963 | } |
| 964 | } |
| 965 | |
| 966 | bool |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 967 | CommandInterpreter::AliasExists (const char *cmd) |
| 968 | { |
| 969 | return m_alias_dict.find(cmd) != m_alias_dict.end(); |
| 970 | } |
| 971 | |
| 972 | bool |
| 973 | CommandInterpreter::UserCommandExists (const char *cmd) |
| 974 | { |
| 975 | return m_user_dict.find(cmd) != m_user_dict.end(); |
| 976 | } |
| 977 | |
| 978 | void |
| 979 | CommandInterpreter::AddAlias (const char *alias_name, CommandObjectSP& command_obj_sp) |
| 980 | { |
Jim Ingham | 279a6c2 | 2010-07-06 22:46:59 +0000 | [diff] [blame] | 981 | command_obj_sp->SetIsAlias (true); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 982 | m_alias_dict[alias_name] = command_obj_sp; |
| 983 | } |
| 984 | |
| 985 | bool |
| 986 | CommandInterpreter::RemoveAlias (const char *alias_name) |
| 987 | { |
| 988 | CommandObject::CommandMap::iterator pos = m_alias_dict.find(alias_name); |
| 989 | if (pos != m_alias_dict.end()) |
| 990 | { |
| 991 | m_alias_dict.erase(pos); |
| 992 | return true; |
| 993 | } |
| 994 | return false; |
| 995 | } |
| 996 | bool |
| 997 | CommandInterpreter::RemoveUser (const char *alias_name) |
| 998 | { |
| 999 | CommandObject::CommandMap::iterator pos = m_user_dict.find(alias_name); |
| 1000 | if (pos != m_user_dict.end()) |
| 1001 | { |
| 1002 | m_user_dict.erase(pos); |
| 1003 | return true; |
| 1004 | } |
| 1005 | return false; |
| 1006 | } |
| 1007 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1008 | void |
| 1009 | CommandInterpreter::GetAliasHelp (const char *alias_name, const char *command_name, StreamString &help_string) |
| 1010 | { |
| 1011 | help_string.Printf ("'%s", command_name); |
| 1012 | OptionArgVectorSP option_arg_vector_sp = GetAliasOptions (alias_name); |
| 1013 | |
Sean Callanan | 9a02851 | 2012-08-09 00:50:26 +0000 | [diff] [blame] | 1014 | if (option_arg_vector_sp) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1015 | { |
| 1016 | OptionArgVector *options = option_arg_vector_sp.get(); |
| 1017 | for (int i = 0; i < options->size(); ++i) |
| 1018 | { |
| 1019 | OptionArgPair cur_option = (*options)[i]; |
| 1020 | std::string opt = cur_option.first; |
Caroline Tice | d9d6336 | 2010-12-07 19:58:26 +0000 | [diff] [blame] | 1021 | OptionArgValue value_pair = cur_option.second; |
| 1022 | std::string value = value_pair.second; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1023 | if (opt.compare("<argument>") == 0) |
| 1024 | { |
| 1025 | help_string.Printf (" %s", value.c_str()); |
| 1026 | } |
| 1027 | else |
| 1028 | { |
| 1029 | help_string.Printf (" %s", opt.c_str()); |
| 1030 | if ((value.compare ("<no-argument>") != 0) |
| 1031 | && (value.compare ("<need-argument") != 0)) |
| 1032 | { |
| 1033 | help_string.Printf (" %s", value.c_str()); |
| 1034 | } |
| 1035 | } |
| 1036 | } |
| 1037 | } |
| 1038 | |
| 1039 | help_string.Printf ("'"); |
| 1040 | } |
| 1041 | |
Greg Clayton | 12fc3e0 | 2010-08-26 22:05:43 +0000 | [diff] [blame] | 1042 | size_t |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1043 | CommandInterpreter::FindLongestCommandWord (CommandObject::CommandMap &dict) |
| 1044 | { |
| 1045 | CommandObject::CommandMap::const_iterator pos; |
Greg Clayton | 12fc3e0 | 2010-08-26 22:05:43 +0000 | [diff] [blame] | 1046 | CommandObject::CommandMap::const_iterator end = dict.end(); |
| 1047 | size_t max_len = 0; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1048 | |
Greg Clayton | 12fc3e0 | 2010-08-26 22:05:43 +0000 | [diff] [blame] | 1049 | for (pos = dict.begin(); pos != end; ++pos) |
| 1050 | { |
| 1051 | size_t len = pos->first.size(); |
| 1052 | if (max_len < len) |
| 1053 | max_len = len; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1054 | } |
Greg Clayton | 12fc3e0 | 2010-08-26 22:05:43 +0000 | [diff] [blame] | 1055 | return max_len; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1056 | } |
| 1057 | |
| 1058 | void |
Enrico Granata | 223383e | 2011-08-16 23:24:13 +0000 | [diff] [blame] | 1059 | CommandInterpreter::GetHelp (CommandReturnObject &result, |
Enrico Granata | 08633ee | 2011-09-09 17:49:36 +0000 | [diff] [blame] | 1060 | uint32_t cmd_types) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1061 | { |
| 1062 | CommandObject::CommandMap::const_iterator pos; |
Greg Clayton | c7bece56 | 2013-01-25 18:06:21 +0000 | [diff] [blame] | 1063 | size_t max_len = FindLongestCommandWord (m_command_dict); |
Enrico Granata | 223383e | 2011-08-16 23:24:13 +0000 | [diff] [blame] | 1064 | |
| 1065 | if ( (cmd_types & eCommandTypesBuiltin) == eCommandTypesBuiltin ) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1066 | { |
Enrico Granata | 223383e | 2011-08-16 23:24:13 +0000 | [diff] [blame] | 1067 | |
| 1068 | result.AppendMessage("The following is a list of built-in, permanent debugger commands:"); |
| 1069 | result.AppendMessage(""); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1070 | |
Enrico Granata | 223383e | 2011-08-16 23:24:13 +0000 | [diff] [blame] | 1071 | for (pos = m_command_dict.begin(); pos != m_command_dict.end(); ++pos) |
| 1072 | { |
| 1073 | OutputFormattedHelpText (result.GetOutputStream(), pos->first.c_str(), "--", pos->second->GetHelp(), |
| 1074 | max_len); |
| 1075 | } |
| 1076 | result.AppendMessage(""); |
| 1077 | |
| 1078 | } |
| 1079 | |
Greg Clayton | 5521f99 | 2011-10-28 21:38:01 +0000 | [diff] [blame] | 1080 | if (!m_alias_dict.empty() && ( (cmd_types & eCommandTypesAliases) == eCommandTypesAliases )) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1081 | { |
Jim Ingham | 49e80a1 | 2010-10-22 18:47:16 +0000 | [diff] [blame] | 1082 | result.AppendMessage("The following is a list of your current command abbreviations " |
Johnny Chen | b89982d | 2011-04-21 00:39:18 +0000 | [diff] [blame] | 1083 | "(see 'help command alias' for more info):"); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1084 | result.AppendMessage(""); |
Greg Clayton | 12fc3e0 | 2010-08-26 22:05:43 +0000 | [diff] [blame] | 1085 | max_len = FindLongestCommandWord (m_alias_dict); |
| 1086 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1087 | for (pos = m_alias_dict.begin(); pos != m_alias_dict.end(); ++pos) |
| 1088 | { |
| 1089 | StreamString sstr; |
| 1090 | StreamString translation_and_help; |
| 1091 | std::string entry_name = pos->first; |
| 1092 | std::string second_entry = pos->second.get()->GetCommandName(); |
| 1093 | GetAliasHelp (pos->first.c_str(), pos->second->GetCommandName(), sstr); |
| 1094 | |
| 1095 | translation_and_help.Printf ("(%s) %s", sstr.GetData(), pos->second->GetHelp()); |
| 1096 | OutputFormattedHelpText (result.GetOutputStream(), pos->first.c_str(), "--", |
| 1097 | translation_and_help.GetData(), max_len); |
| 1098 | } |
| 1099 | result.AppendMessage(""); |
| 1100 | } |
| 1101 | |
Greg Clayton | 5521f99 | 2011-10-28 21:38:01 +0000 | [diff] [blame] | 1102 | if (!m_user_dict.empty() && ( (cmd_types & eCommandTypesUserDef) == eCommandTypesUserDef )) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1103 | { |
| 1104 | result.AppendMessage ("The following is a list of your current user-defined commands:"); |
| 1105 | result.AppendMessage(""); |
Enrico Granata | 223383e | 2011-08-16 23:24:13 +0000 | [diff] [blame] | 1106 | max_len = FindLongestCommandWord (m_user_dict); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1107 | for (pos = m_user_dict.begin(); pos != m_user_dict.end(); ++pos) |
| 1108 | { |
Enrico Granata | 223383e | 2011-08-16 23:24:13 +0000 | [diff] [blame] | 1109 | OutputFormattedHelpText (result.GetOutputStream(), pos->first.c_str(), "--", pos->second->GetHelp(), |
| 1110 | max_len); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1111 | } |
| 1112 | result.AppendMessage(""); |
| 1113 | } |
| 1114 | |
| 1115 | result.AppendMessage("For more information on any particular command, try 'help <command-name>'."); |
| 1116 | } |
| 1117 | |
Caroline Tice | 844d230 | 2010-12-09 22:52:49 +0000 | [diff] [blame] | 1118 | CommandObject * |
| 1119 | CommandInterpreter::GetCommandObjectForCommand (std::string &command_string) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1120 | { |
Caroline Tice | 844d230 | 2010-12-09 22:52:49 +0000 | [diff] [blame] | 1121 | // This function finds the final, lowest-level, alias-resolved command object whose 'Execute' function will |
| 1122 | // eventually be invoked by the given command line. |
| 1123 | |
| 1124 | CommandObject *cmd_obj = NULL; |
| 1125 | std::string white_space (" \t\v"); |
| 1126 | size_t start = command_string.find_first_not_of (white_space); |
| 1127 | size_t end = 0; |
| 1128 | bool done = false; |
| 1129 | while (!done) |
| 1130 | { |
| 1131 | if (start != std::string::npos) |
| 1132 | { |
| 1133 | // Get the next word from command_string. |
| 1134 | end = command_string.find_first_of (white_space, start); |
| 1135 | if (end == std::string::npos) |
| 1136 | end = command_string.size(); |
| 1137 | std::string cmd_word = command_string.substr (start, end - start); |
| 1138 | |
| 1139 | if (cmd_obj == NULL) |
| 1140 | // Since cmd_obj is NULL we are on our first time through this loop. Check to see if cmd_word is a valid |
| 1141 | // command or alias. |
| 1142 | cmd_obj = GetCommandObject (cmd_word.c_str()); |
| 1143 | else if (cmd_obj->IsMultiwordObject ()) |
| 1144 | { |
| 1145 | // 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] | 1146 | CommandObject *sub_cmd_obj = cmd_obj->GetSubcommandObject (cmd_word.c_str()); |
Caroline Tice | 844d230 | 2010-12-09 22:52:49 +0000 | [diff] [blame] | 1147 | if (sub_cmd_obj) |
| 1148 | cmd_obj = sub_cmd_obj; |
| 1149 | else // cmd_word was not a valid sub-command word, so we are donee |
| 1150 | done = true; |
| 1151 | } |
| 1152 | else |
| 1153 | // We have a cmd_obj and it is not a multi-word object, so we are done. |
| 1154 | done = true; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1155 | |
Caroline Tice | 844d230 | 2010-12-09 22:52:49 +0000 | [diff] [blame] | 1156 | // If we didn't find a valid command object, or our command object is not a multi-word object, or |
| 1157 | // we are at the end of the command_string, then we are done. Otherwise, find the start of the |
| 1158 | // next word. |
| 1159 | |
| 1160 | if (!cmd_obj || !cmd_obj->IsMultiwordObject() || end >= command_string.size()) |
| 1161 | done = true; |
| 1162 | else |
| 1163 | start = command_string.find_first_not_of (white_space, end); |
| 1164 | } |
| 1165 | else |
| 1166 | // Unable to find any more words. |
| 1167 | done = true; |
| 1168 | } |
| 1169 | |
| 1170 | if (end == command_string.size()) |
| 1171 | command_string.clear(); |
| 1172 | else |
| 1173 | command_string = command_string.substr(end); |
| 1174 | |
| 1175 | return cmd_obj; |
| 1176 | } |
| 1177 | |
Greg Clayton | 5196416 | 2011-10-25 00:36:27 +0000 | [diff] [blame] | 1178 | static const char *k_white_space = " \t\v"; |
Greg Clayton | 5521f99 | 2011-10-28 21:38:01 +0000 | [diff] [blame] | 1179 | static const char *k_valid_command_chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_"; |
Greg Clayton | 5196416 | 2011-10-25 00:36:27 +0000 | [diff] [blame] | 1180 | static void |
| 1181 | StripLeadingSpaces (std::string &s) |
Caroline Tice | 844d230 | 2010-12-09 22:52:49 +0000 | [diff] [blame] | 1182 | { |
Greg Clayton | 5196416 | 2011-10-25 00:36:27 +0000 | [diff] [blame] | 1183 | if (!s.empty()) |
Caroline Tice | 844d230 | 2010-12-09 22:52:49 +0000 | [diff] [blame] | 1184 | { |
Greg Clayton | 5196416 | 2011-10-25 00:36:27 +0000 | [diff] [blame] | 1185 | size_t pos = s.find_first_not_of (k_white_space); |
| 1186 | if (pos == std::string::npos) |
| 1187 | s.clear(); |
| 1188 | else if (pos == 0) |
| 1189 | return; |
| 1190 | s.erase (0, pos); |
| 1191 | } |
| 1192 | } |
| 1193 | |
Greg Clayton | 93c62e6 | 2011-11-09 23:25:03 +0000 | [diff] [blame] | 1194 | static size_t |
| 1195 | FindArgumentTerminator (const std::string &s) |
| 1196 | { |
Greg Clayton | 93c62e6 | 2011-11-09 23:25:03 +0000 | [diff] [blame] | 1197 | const size_t s_len = s.size(); |
| 1198 | size_t offset = 0; |
| 1199 | while (offset < s_len) |
| 1200 | { |
| 1201 | size_t pos = s.find ("--", offset); |
| 1202 | if (pos == std::string::npos) |
| 1203 | break; |
| 1204 | if (pos > 0) |
| 1205 | { |
| 1206 | if (isspace(s[pos-1])) |
| 1207 | { |
| 1208 | // Check if the string ends "\s--" (where \s is a space character) |
| 1209 | // or if we have "\s--\s". |
| 1210 | if ((pos + 2 >= s_len) || isspace(s[pos+2])) |
| 1211 | { |
Greg Clayton | 93c62e6 | 2011-11-09 23:25:03 +0000 | [diff] [blame] | 1212 | return pos; |
| 1213 | } |
| 1214 | } |
| 1215 | } |
| 1216 | offset = pos + 2; |
| 1217 | } |
Greg Clayton | 93c62e6 | 2011-11-09 23:25:03 +0000 | [diff] [blame] | 1218 | return std::string::npos; |
| 1219 | } |
| 1220 | |
Greg Clayton | 5196416 | 2011-10-25 00:36:27 +0000 | [diff] [blame] | 1221 | static bool |
Greg Clayton | 5521f99 | 2011-10-28 21:38:01 +0000 | [diff] [blame] | 1222 | 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] | 1223 | { |
Greg Clayton | 5521f99 | 2011-10-28 21:38:01 +0000 | [diff] [blame] | 1224 | command.clear(); |
| 1225 | suffix.clear(); |
Greg Clayton | 5196416 | 2011-10-25 00:36:27 +0000 | [diff] [blame] | 1226 | StripLeadingSpaces (command_string); |
| 1227 | |
| 1228 | bool result = false; |
| 1229 | quote_char = '\0'; |
| 1230 | |
| 1231 | if (!command_string.empty()) |
| 1232 | { |
| 1233 | const char first_char = command_string[0]; |
| 1234 | if (first_char == '\'' || first_char == '"') |
Caroline Tice | 844d230 | 2010-12-09 22:52:49 +0000 | [diff] [blame] | 1235 | { |
Greg Clayton | 5196416 | 2011-10-25 00:36:27 +0000 | [diff] [blame] | 1236 | quote_char = first_char; |
| 1237 | const size_t end_quote_pos = command_string.find (quote_char, 1); |
| 1238 | if (end_quote_pos == std::string::npos) |
Caroline Tice | 2b5e850 | 2011-05-11 16:07:06 +0000 | [diff] [blame] | 1239 | { |
Greg Clayton | 5521f99 | 2011-10-28 21:38:01 +0000 | [diff] [blame] | 1240 | command.swap (command_string); |
Greg Clayton | 5196416 | 2011-10-25 00:36:27 +0000 | [diff] [blame] | 1241 | command_string.erase (); |
Caroline Tice | 2b5e850 | 2011-05-11 16:07:06 +0000 | [diff] [blame] | 1242 | } |
| 1243 | else |
| 1244 | { |
Greg Clayton | 5521f99 | 2011-10-28 21:38:01 +0000 | [diff] [blame] | 1245 | command.assign (command_string, 1, end_quote_pos - 1); |
Greg Clayton | 5196416 | 2011-10-25 00:36:27 +0000 | [diff] [blame] | 1246 | if (end_quote_pos + 1 < command_string.size()) |
| 1247 | command_string.erase (0, command_string.find_first_not_of (k_white_space, end_quote_pos + 1)); |
| 1248 | else |
| 1249 | command_string.erase (); |
Caroline Tice | 2b5e850 | 2011-05-11 16:07:06 +0000 | [diff] [blame] | 1250 | } |
Caroline Tice | 844d230 | 2010-12-09 22:52:49 +0000 | [diff] [blame] | 1251 | } |
| 1252 | else |
| 1253 | { |
Greg Clayton | 5196416 | 2011-10-25 00:36:27 +0000 | [diff] [blame] | 1254 | const size_t first_space_pos = command_string.find_first_of (k_white_space); |
| 1255 | if (first_space_pos == std::string::npos) |
Caroline Tice | 2b5e850 | 2011-05-11 16:07:06 +0000 | [diff] [blame] | 1256 | { |
Greg Clayton | 5521f99 | 2011-10-28 21:38:01 +0000 | [diff] [blame] | 1257 | command.swap (command_string); |
Greg Clayton | 5196416 | 2011-10-25 00:36:27 +0000 | [diff] [blame] | 1258 | command_string.erase(); |
Caroline Tice | 2b5e850 | 2011-05-11 16:07:06 +0000 | [diff] [blame] | 1259 | } |
| 1260 | else |
| 1261 | { |
Greg Clayton | 5521f99 | 2011-10-28 21:38:01 +0000 | [diff] [blame] | 1262 | command.assign (command_string, 0, first_space_pos); |
| 1263 | 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] | 1264 | } |
Caroline Tice | 844d230 | 2010-12-09 22:52:49 +0000 | [diff] [blame] | 1265 | } |
Greg Clayton | 5196416 | 2011-10-25 00:36:27 +0000 | [diff] [blame] | 1266 | result = true; |
Caroline Tice | 844d230 | 2010-12-09 22:52:49 +0000 | [diff] [blame] | 1267 | } |
Greg Clayton | 5521f99 | 2011-10-28 21:38:01 +0000 | [diff] [blame] | 1268 | |
| 1269 | |
| 1270 | if (!command.empty()) |
| 1271 | { |
| 1272 | // actual commands can't start with '-' or '_' |
| 1273 | if (command[0] != '-' && command[0] != '_') |
| 1274 | { |
| 1275 | size_t pos = command.find_first_not_of(k_valid_command_chars); |
| 1276 | if (pos > 0 && pos != std::string::npos) |
| 1277 | { |
| 1278 | suffix.assign (command.begin() + pos, command.end()); |
| 1279 | command.erase (pos); |
| 1280 | } |
| 1281 | } |
| 1282 | } |
Greg Clayton | 5196416 | 2011-10-25 00:36:27 +0000 | [diff] [blame] | 1283 | |
| 1284 | return result; |
Caroline Tice | 844d230 | 2010-12-09 22:52:49 +0000 | [diff] [blame] | 1285 | } |
| 1286 | |
Greg Clayton | 5521f99 | 2011-10-28 21:38:01 +0000 | [diff] [blame] | 1287 | CommandObject * |
| 1288 | CommandInterpreter::BuildAliasResult (const char *alias_name, |
| 1289 | std::string &raw_input_string, |
| 1290 | std::string &alias_result, |
| 1291 | CommandReturnObject &result) |
Caroline Tice | 844d230 | 2010-12-09 22:52:49 +0000 | [diff] [blame] | 1292 | { |
Greg Clayton | 5521f99 | 2011-10-28 21:38:01 +0000 | [diff] [blame] | 1293 | CommandObject *alias_cmd_obj = NULL; |
Caroline Tice | 844d230 | 2010-12-09 22:52:49 +0000 | [diff] [blame] | 1294 | Args cmd_args (raw_input_string.c_str()); |
| 1295 | alias_cmd_obj = GetCommandObject (alias_name); |
| 1296 | StreamString result_str; |
| 1297 | |
| 1298 | if (alias_cmd_obj) |
| 1299 | { |
| 1300 | std::string alias_name_str = alias_name; |
| 1301 | if ((cmd_args.GetArgumentCount() == 0) |
| 1302 | || (alias_name_str.compare (cmd_args.GetArgumentAtIndex(0)) != 0)) |
| 1303 | cmd_args.Unshift (alias_name); |
| 1304 | |
| 1305 | result_str.Printf ("%s", alias_cmd_obj->GetCommandName ()); |
| 1306 | OptionArgVectorSP option_arg_vector_sp = GetAliasOptions (alias_name); |
| 1307 | |
| 1308 | if (option_arg_vector_sp.get()) |
| 1309 | { |
| 1310 | OptionArgVector *option_arg_vector = option_arg_vector_sp.get(); |
| 1311 | |
| 1312 | for (int i = 0; i < option_arg_vector->size(); ++i) |
| 1313 | { |
| 1314 | OptionArgPair option_pair = (*option_arg_vector)[i]; |
| 1315 | OptionArgValue value_pair = option_pair.second; |
| 1316 | int value_type = value_pair.first; |
| 1317 | std::string option = option_pair.first; |
| 1318 | std::string value = value_pair.second; |
| 1319 | if (option.compare ("<argument>") == 0) |
| 1320 | result_str.Printf (" %s", value.c_str()); |
| 1321 | else |
| 1322 | { |
| 1323 | result_str.Printf (" %s", option.c_str()); |
| 1324 | if (value_type != optional_argument) |
| 1325 | result_str.Printf (" "); |
| 1326 | if (value.compare ("<no_argument>") != 0) |
| 1327 | { |
| 1328 | int index = GetOptionArgumentPosition (value.c_str()); |
| 1329 | if (index == 0) |
| 1330 | result_str.Printf ("%s", value.c_str()); |
| 1331 | else if (index >= cmd_args.GetArgumentCount()) |
| 1332 | { |
| 1333 | |
| 1334 | result.AppendErrorWithFormat |
| 1335 | ("Not enough arguments provided; you need at least %d arguments to use this alias.\n", |
| 1336 | index); |
| 1337 | result.SetStatus (eReturnStatusFailed); |
Greg Clayton | 5521f99 | 2011-10-28 21:38:01 +0000 | [diff] [blame] | 1338 | return alias_cmd_obj; |
Caroline Tice | 844d230 | 2010-12-09 22:52:49 +0000 | [diff] [blame] | 1339 | } |
| 1340 | else |
| 1341 | { |
| 1342 | size_t strpos = raw_input_string.find (cmd_args.GetArgumentAtIndex (index)); |
| 1343 | if (strpos != std::string::npos) |
| 1344 | raw_input_string = raw_input_string.erase (strpos, |
| 1345 | strlen (cmd_args.GetArgumentAtIndex (index))); |
| 1346 | result_str.Printf ("%s", cmd_args.GetArgumentAtIndex (index)); |
| 1347 | } |
| 1348 | } |
| 1349 | } |
| 1350 | } |
| 1351 | } |
| 1352 | |
| 1353 | alias_result = result_str.GetData(); |
| 1354 | } |
Greg Clayton | 5521f99 | 2011-10-28 21:38:01 +0000 | [diff] [blame] | 1355 | return alias_cmd_obj; |
Caroline Tice | 844d230 | 2010-12-09 22:52:49 +0000 | [diff] [blame] | 1356 | } |
| 1357 | |
Greg Clayton | 5a31471 | 2011-10-14 07:41:33 +0000 | [diff] [blame] | 1358 | Error |
| 1359 | CommandInterpreter::PreprocessCommand (std::string &command) |
| 1360 | { |
| 1361 | // The command preprocessor needs to do things to the command |
| 1362 | // line before any parsing of arguments or anything else is done. |
| 1363 | // The only current stuff that gets proprocessed is anyting enclosed |
| 1364 | // in backtick ('`') characters is evaluated as an expression and |
| 1365 | // the result of the expression must be a scalar that can be substituted |
| 1366 | // into the command. An example would be: |
| 1367 | // (lldb) memory read `$rsp + 20` |
| 1368 | Error error; // Error for any expressions that might not evaluate |
| 1369 | size_t start_backtick; |
| 1370 | size_t pos = 0; |
| 1371 | while ((start_backtick = command.find ('`', pos)) != std::string::npos) |
| 1372 | { |
| 1373 | if (start_backtick > 0 && command[start_backtick-1] == '\\') |
| 1374 | { |
| 1375 | // The backtick was preceeded by a '\' character, remove the slash |
| 1376 | // and don't treat the backtick as the start of an expression |
| 1377 | command.erase(start_backtick-1, 1); |
| 1378 | // No need to add one to start_backtick since we just deleted a char |
| 1379 | pos = start_backtick; |
| 1380 | } |
| 1381 | else |
| 1382 | { |
| 1383 | const size_t expr_content_start = start_backtick + 1; |
| 1384 | const size_t end_backtick = command.find ('`', expr_content_start); |
| 1385 | if (end_backtick == std::string::npos) |
| 1386 | return error; |
| 1387 | else if (end_backtick == expr_content_start) |
| 1388 | { |
| 1389 | // Empty expression (two backticks in a row) |
| 1390 | command.erase (start_backtick, 2); |
| 1391 | } |
| 1392 | else |
| 1393 | { |
| 1394 | std::string expr_str (command, expr_content_start, end_backtick - expr_content_start); |
| 1395 | |
Greg Clayton | 4e0fe8a | 2012-07-12 20:32:19 +0000 | [diff] [blame] | 1396 | ExecutionContext exe_ctx(GetExecutionContext()); |
| 1397 | Target *target = exe_ctx.GetTargetPtr(); |
Johnny Chen | 51ea0ad | 2011-10-29 00:21:50 +0000 | [diff] [blame] | 1398 | // Get a dummy target to allow for calculator mode while processing backticks. |
| 1399 | // This also helps break the infinite loop caused when target is null. |
| 1400 | if (!target) |
| 1401 | target = Host::GetDummyTarget(GetDebugger()).get(); |
Greg Clayton | 5a31471 | 2011-10-14 07:41:33 +0000 | [diff] [blame] | 1402 | if (target) |
| 1403 | { |
Greg Clayton | 5a31471 | 2011-10-14 07:41:33 +0000 | [diff] [blame] | 1404 | ValueObjectSP expr_result_valobj_sp; |
Enrico Granata | d4439aa | 2012-09-05 20:41:26 +0000 | [diff] [blame] | 1405 | |
Jim Ingham | 35e1bda | 2012-10-16 21:41:58 +0000 | [diff] [blame] | 1406 | EvaluateExpressionOptions options; |
Enrico Granata | d4439aa | 2012-09-05 20:41:26 +0000 | [diff] [blame] | 1407 | options.SetCoerceToId(false) |
| 1408 | .SetUnwindOnError(true) |
Jim Ingham | 184e981 | 2013-01-15 02:47:48 +0000 | [diff] [blame] | 1409 | .SetIgnoreBreakpoints(true) |
Enrico Granata | d4439aa | 2012-09-05 20:41:26 +0000 | [diff] [blame] | 1410 | .SetKeepInMemory(false) |
Jim Ingham | 35e1bda | 2012-10-16 21:41:58 +0000 | [diff] [blame] | 1411 | .SetRunOthers(true) |
| 1412 | .SetTimeoutUsec(0); |
Enrico Granata | d4439aa | 2012-09-05 20:41:26 +0000 | [diff] [blame] | 1413 | |
Greg Clayton | 5a31471 | 2011-10-14 07:41:33 +0000 | [diff] [blame] | 1414 | ExecutionResults expr_result = target->EvaluateExpression (expr_str.c_str(), |
Enrico Granata | d4439aa | 2012-09-05 20:41:26 +0000 | [diff] [blame] | 1415 | exe_ctx.GetFramePtr(), |
Enrico Granata | 3372f58 | 2012-07-16 23:10:35 +0000 | [diff] [blame] | 1416 | expr_result_valobj_sp, |
Enrico Granata | d4439aa | 2012-09-05 20:41:26 +0000 | [diff] [blame] | 1417 | options); |
| 1418 | |
Greg Clayton | 5a31471 | 2011-10-14 07:41:33 +0000 | [diff] [blame] | 1419 | if (expr_result == eExecutionCompleted) |
| 1420 | { |
| 1421 | Scalar scalar; |
| 1422 | if (expr_result_valobj_sp->ResolveValue (scalar)) |
| 1423 | { |
| 1424 | command.erase (start_backtick, end_backtick - start_backtick + 1); |
| 1425 | StreamString value_strm; |
| 1426 | const bool show_type = false; |
| 1427 | scalar.GetValue (&value_strm, show_type); |
| 1428 | size_t value_string_size = value_strm.GetSize(); |
| 1429 | if (value_string_size) |
| 1430 | { |
| 1431 | command.insert (start_backtick, value_strm.GetData(), value_string_size); |
| 1432 | pos = start_backtick + value_string_size; |
| 1433 | continue; |
| 1434 | } |
| 1435 | else |
| 1436 | { |
| 1437 | error.SetErrorStringWithFormat("expression value didn't result in a scalar value for the expression '%s'", expr_str.c_str()); |
| 1438 | } |
| 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 | if (expr_result_valobj_sp) |
| 1448 | error = expr_result_valobj_sp->GetError(); |
| 1449 | if (error.Success()) |
| 1450 | { |
| 1451 | |
| 1452 | switch (expr_result) |
| 1453 | { |
| 1454 | case eExecutionSetupError: |
| 1455 | error.SetErrorStringWithFormat("expression setup error for the expression '%s'", expr_str.c_str()); |
| 1456 | break; |
| 1457 | case eExecutionCompleted: |
| 1458 | break; |
| 1459 | case eExecutionDiscarded: |
| 1460 | error.SetErrorStringWithFormat("expression discarded for the expression '%s'", expr_str.c_str()); |
| 1461 | break; |
| 1462 | case eExecutionInterrupted: |
| 1463 | error.SetErrorStringWithFormat("expression interrupted for the expression '%s'", expr_str.c_str()); |
| 1464 | break; |
Jim Ingham | 184e981 | 2013-01-15 02:47:48 +0000 | [diff] [blame] | 1465 | case eExecutionHitBreakpoint: |
| 1466 | error.SetErrorStringWithFormat("expression hit breakpoint for the expression '%s'", expr_str.c_str()); |
| 1467 | break; |
Greg Clayton | 5a31471 | 2011-10-14 07:41:33 +0000 | [diff] [blame] | 1468 | case eExecutionTimedOut: |
| 1469 | error.SetErrorStringWithFormat("expression timed out for the expression '%s'", expr_str.c_str()); |
| 1470 | break; |
| 1471 | } |
| 1472 | } |
| 1473 | } |
| 1474 | } |
| 1475 | } |
| 1476 | if (error.Fail()) |
| 1477 | break; |
| 1478 | } |
| 1479 | } |
| 1480 | return error; |
| 1481 | } |
| 1482 | |
| 1483 | |
Caroline Tice | 844d230 | 2010-12-09 22:52:49 +0000 | [diff] [blame] | 1484 | bool |
| 1485 | CommandInterpreter::HandleCommand (const char *command_line, |
Enrico Granata | 5f5ab60 | 2012-05-31 01:09:06 +0000 | [diff] [blame] | 1486 | LazyBool lazy_add_to_history, |
Caroline Tice | 844d230 | 2010-12-09 22:52:49 +0000 | [diff] [blame] | 1487 | CommandReturnObject &result, |
Jim Ingham | e16c50a | 2011-02-18 00:54:25 +0000 | [diff] [blame] | 1488 | ExecutionContext *override_context, |
Johnny Chen | 80fdd7c | 2011-10-05 00:42:59 +0000 | [diff] [blame] | 1489 | bool repeat_on_empty_command, |
| 1490 | bool no_context_switching) |
Jim Ingham | e16c50a | 2011-02-18 00:54:25 +0000 | [diff] [blame] | 1491 | |
Caroline Tice | 844d230 | 2010-12-09 22:52:49 +0000 | [diff] [blame] | 1492 | { |
Jim Ingham | e16c50a | 2011-02-18 00:54:25 +0000 | [diff] [blame] | 1493 | |
Caroline Tice | 844d230 | 2010-12-09 22:52:49 +0000 | [diff] [blame] | 1494 | bool done = false; |
| 1495 | CommandObject *cmd_obj = NULL; |
Caroline Tice | 844d230 | 2010-12-09 22:52:49 +0000 | [diff] [blame] | 1496 | bool wants_raw_input = false; |
| 1497 | std::string command_string (command_line); |
Jim Ingham | a5a97eb | 2011-07-12 03:12:18 +0000 | [diff] [blame] | 1498 | std::string original_command_string (command_line); |
Caroline Tice | 844d230 | 2010-12-09 22:52:49 +0000 | [diff] [blame] | 1499 | |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 1500 | Log *log (lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_COMMANDS)); |
Greg Clayton | 2d95dc9b | 2010-11-10 04:57:04 +0000 | [diff] [blame] | 1501 | Host::SetCrashDescriptionWithFormat ("HandleCommand(command = \"%s\")", command_line); |
| 1502 | |
| 1503 | // Make a scoped cleanup object that will clear the crash description string |
| 1504 | // on exit of this function. |
Enrico Granata | f9fa6ee | 2011-07-12 00:18:11 +0000 | [diff] [blame] | 1505 | lldb_utility::CleanUp <const char *> crash_description_cleanup(NULL, Host::SetCrashDescription); |
Greg Clayton | 2d95dc9b | 2010-11-10 04:57:04 +0000 | [diff] [blame] | 1506 | |
Caroline Tice | 844d230 | 2010-12-09 22:52:49 +0000 | [diff] [blame] | 1507 | if (log) |
| 1508 | log->Printf ("Processing command: %s", command_line); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1509 | |
Jim Ingham | 3024483 | 2010-11-04 23:08:45 +0000 | [diff] [blame] | 1510 | Timer scoped_timer (__PRETTY_FUNCTION__, "Handling command: %s.", command_line); |
| 1511 | |
Johnny Chen | 80fdd7c | 2011-10-05 00:42:59 +0000 | [diff] [blame] | 1512 | if (!no_context_switching) |
| 1513 | UpdateExecutionContext (override_context); |
Enrico Granata | 5f5ab60 | 2012-05-31 01:09:06 +0000 | [diff] [blame] | 1514 | |
Enrico Granata | 5f5ab60 | 2012-05-31 01:09:06 +0000 | [diff] [blame] | 1515 | bool add_to_history; |
| 1516 | if (lazy_add_to_history == eLazyBoolCalculate) |
| 1517 | add_to_history = (m_command_source_depth == 0); |
| 1518 | else |
| 1519 | add_to_history = (lazy_add_to_history == eLazyBoolYes); |
| 1520 | |
Jim Ingham | e16c50a | 2011-02-18 00:54:25 +0000 | [diff] [blame] | 1521 | bool empty_command = false; |
| 1522 | bool comment_command = false; |
| 1523 | if (command_string.empty()) |
| 1524 | empty_command = true; |
| 1525 | else |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1526 | { |
Jim Ingham | e16c50a | 2011-02-18 00:54:25 +0000 | [diff] [blame] | 1527 | const char *k_space_characters = "\t\n\v\f\r "; |
| 1528 | |
| 1529 | size_t non_space = command_string.find_first_not_of (k_space_characters); |
| 1530 | // Check for empty line or comment line (lines whose first |
| 1531 | // non-space character is the comment character for this interpreter) |
| 1532 | if (non_space == std::string::npos) |
| 1533 | empty_command = true; |
| 1534 | else if (command_string[non_space] == m_comment_char) |
| 1535 | comment_command = true; |
Enrico Granata | 7594f14 | 2013-06-17 22:51:50 +0000 | [diff] [blame^] | 1536 | else if (command_string[non_space] == CommandHistory::g_repeat_char) |
Jim Ingham | a5a97eb | 2011-07-12 03:12:18 +0000 | [diff] [blame] | 1537 | { |
Enrico Granata | 7594f14 | 2013-06-17 22:51:50 +0000 | [diff] [blame^] | 1538 | 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] | 1539 | if (history_string == NULL) |
| 1540 | { |
| 1541 | result.AppendErrorWithFormat ("Could not find entry: %s in history", command_string.c_str()); |
| 1542 | result.SetStatus(eReturnStatusFailed); |
| 1543 | return false; |
| 1544 | } |
| 1545 | add_to_history = false; |
| 1546 | command_string = history_string; |
| 1547 | original_command_string = history_string; |
| 1548 | } |
Jim Ingham | e16c50a | 2011-02-18 00:54:25 +0000 | [diff] [blame] | 1549 | } |
| 1550 | |
| 1551 | if (empty_command) |
| 1552 | { |
| 1553 | if (repeat_on_empty_command) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1554 | { |
Enrico Granata | 7594f14 | 2013-06-17 22:51:50 +0000 | [diff] [blame^] | 1555 | if (m_command_history.IsEmpty()) |
Jim Ingham | e16c50a | 2011-02-18 00:54:25 +0000 | [diff] [blame] | 1556 | { |
| 1557 | result.AppendError ("empty command"); |
| 1558 | result.SetStatus(eReturnStatusFailed); |
| 1559 | return false; |
| 1560 | } |
| 1561 | else |
| 1562 | { |
| 1563 | command_line = m_repeat_command.c_str(); |
| 1564 | command_string = command_line; |
Jim Ingham | a5a97eb | 2011-07-12 03:12:18 +0000 | [diff] [blame] | 1565 | original_command_string = command_line; |
Jim Ingham | e16c50a | 2011-02-18 00:54:25 +0000 | [diff] [blame] | 1566 | if (m_repeat_command.empty()) |
| 1567 | { |
| 1568 | result.AppendErrorWithFormat("No auto repeat.\n"); |
| 1569 | result.SetStatus (eReturnStatusFailed); |
| 1570 | return false; |
| 1571 | } |
| 1572 | } |
| 1573 | add_to_history = false; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1574 | } |
| 1575 | else |
| 1576 | { |
Jim Ingham | e16c50a | 2011-02-18 00:54:25 +0000 | [diff] [blame] | 1577 | result.SetStatus (eReturnStatusSuccessFinishNoResult); |
| 1578 | return true; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1579 | } |
Jim Ingham | e16c50a | 2011-02-18 00:54:25 +0000 | [diff] [blame] | 1580 | } |
| 1581 | else if (comment_command) |
| 1582 | { |
| 1583 | result.SetStatus (eReturnStatusSuccessFinishNoResult); |
| 1584 | return true; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1585 | } |
Caroline Tice | 2b5e850 | 2011-05-11 16:07:06 +0000 | [diff] [blame] | 1586 | |
Greg Clayton | 5a31471 | 2011-10-14 07:41:33 +0000 | [diff] [blame] | 1587 | |
| 1588 | Error error (PreprocessCommand (command_string)); |
| 1589 | |
| 1590 | if (error.Fail()) |
| 1591 | { |
| 1592 | result.AppendError (error.AsCString()); |
| 1593 | result.SetStatus(eReturnStatusFailed); |
| 1594 | return false; |
| 1595 | } |
Caroline Tice | 844d230 | 2010-12-09 22:52:49 +0000 | [diff] [blame] | 1596 | // Phase 1. |
| 1597 | |
| 1598 | // Before we do ANY kind of argument processing, etc. we need to figure out what the real/final command object |
| 1599 | // is for the specified command, and whether or not it wants raw input. This gets complicated by the fact that |
| 1600 | // the user could have specified an alias, and in translating the alias there may also be command options and/or |
| 1601 | // even data (including raw text strings) that need to be found and inserted into the command line as part of |
| 1602 | // 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] | 1603 | // 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] | 1604 | // 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] | 1605 | |
Caroline Tice | 844d230 | 2010-12-09 22:52:49 +0000 | [diff] [blame] | 1606 | StreamString revised_command_line; |
Caroline Tice | 01274c0 | 2010-12-11 08:16:56 +0000 | [diff] [blame] | 1607 | size_t actual_cmd_name_len = 0; |
Greg Clayton | 5521f99 | 2011-10-28 21:38:01 +0000 | [diff] [blame] | 1608 | std::string next_word; |
Filipe Cabecinhas | af1537f | 2012-05-16 23:25:54 +0000 | [diff] [blame] | 1609 | StringList matches; |
Caroline Tice | 844d230 | 2010-12-09 22:52:49 +0000 | [diff] [blame] | 1610 | while (!done) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1611 | { |
Caroline Tice | 2b5e850 | 2011-05-11 16:07:06 +0000 | [diff] [blame] | 1612 | char quote_char = '\0'; |
Greg Clayton | 5521f99 | 2011-10-28 21:38:01 +0000 | [diff] [blame] | 1613 | std::string suffix; |
| 1614 | ExtractCommand (command_string, next_word, suffix, quote_char); |
| 1615 | if (cmd_obj == NULL) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1616 | { |
Jim Ingham | 298f378 | 2013-04-03 00:25:49 +0000 | [diff] [blame] | 1617 | std::string full_name; |
| 1618 | if (GetAliasFullName(next_word.c_str(), full_name)) |
Caroline Tice | 472362e | 2010-12-14 18:51:39 +0000 | [diff] [blame] | 1619 | { |
Greg Clayton | 5521f99 | 2011-10-28 21:38:01 +0000 | [diff] [blame] | 1620 | std::string alias_result; |
Jim Ingham | 298f378 | 2013-04-03 00:25:49 +0000 | [diff] [blame] | 1621 | cmd_obj = BuildAliasResult (full_name.c_str(), command_string, alias_result, result); |
Greg Clayton | 5521f99 | 2011-10-28 21:38:01 +0000 | [diff] [blame] | 1622 | revised_command_line.Printf ("%s", alias_result.c_str()); |
| 1623 | if (cmd_obj) |
| 1624 | { |
| 1625 | wants_raw_input = cmd_obj->WantsRawCommandString (); |
| 1626 | actual_cmd_name_len = strlen (cmd_obj->GetCommandName()); |
| 1627 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1628 | } |
| 1629 | else |
| 1630 | { |
Filipe Cabecinhas | af1537f | 2012-05-16 23:25:54 +0000 | [diff] [blame] | 1631 | cmd_obj = GetCommandObject (next_word.c_str(), &matches); |
Greg Clayton | 5521f99 | 2011-10-28 21:38:01 +0000 | [diff] [blame] | 1632 | if (cmd_obj) |
| 1633 | { |
| 1634 | actual_cmd_name_len += next_word.length(); |
| 1635 | revised_command_line.Printf ("%s", next_word.c_str()); |
| 1636 | wants_raw_input = cmd_obj->WantsRawCommandString (); |
| 1637 | } |
Caroline Tice | 2b5e850 | 2011-05-11 16:07:06 +0000 | [diff] [blame] | 1638 | else |
Greg Clayton | 5521f99 | 2011-10-28 21:38:01 +0000 | [diff] [blame] | 1639 | { |
| 1640 | revised_command_line.Printf ("%s", next_word.c_str()); |
| 1641 | } |
Caroline Tice | 844d230 | 2010-12-09 22:52:49 +0000 | [diff] [blame] | 1642 | } |
| 1643 | } |
| 1644 | else |
| 1645 | { |
Greg Clayton | 5521f99 | 2011-10-28 21:38:01 +0000 | [diff] [blame] | 1646 | if (cmd_obj->IsMultiwordObject ()) |
| 1647 | { |
Greg Clayton | 998255b | 2012-10-13 02:07:45 +0000 | [diff] [blame] | 1648 | CommandObject *sub_cmd_obj = cmd_obj->GetSubcommandObject (next_word.c_str()); |
Greg Clayton | 5521f99 | 2011-10-28 21:38:01 +0000 | [diff] [blame] | 1649 | if (sub_cmd_obj) |
| 1650 | { |
| 1651 | actual_cmd_name_len += next_word.length() + 1; |
| 1652 | revised_command_line.Printf (" %s", next_word.c_str()); |
| 1653 | cmd_obj = sub_cmd_obj; |
| 1654 | wants_raw_input = cmd_obj->WantsRawCommandString (); |
| 1655 | } |
| 1656 | else |
| 1657 | { |
| 1658 | if (quote_char) |
| 1659 | revised_command_line.Printf (" %c%s%s%c", quote_char, next_word.c_str(), suffix.c_str(), quote_char); |
| 1660 | else |
| 1661 | revised_command_line.Printf (" %s%s", next_word.c_str(), suffix.c_str()); |
| 1662 | done = true; |
| 1663 | } |
| 1664 | } |
Caroline Tice | 2b5e850 | 2011-05-11 16:07:06 +0000 | [diff] [blame] | 1665 | else |
Greg Clayton | 5521f99 | 2011-10-28 21:38:01 +0000 | [diff] [blame] | 1666 | { |
| 1667 | if (quote_char) |
| 1668 | revised_command_line.Printf (" %c%s%s%c", quote_char, next_word.c_str(), suffix.c_str(), quote_char); |
| 1669 | else |
| 1670 | revised_command_line.Printf (" %s%s", next_word.c_str(), suffix.c_str()); |
| 1671 | done = true; |
| 1672 | } |
Caroline Tice | 844d230 | 2010-12-09 22:52:49 +0000 | [diff] [blame] | 1673 | } |
| 1674 | |
| 1675 | if (cmd_obj == NULL) |
| 1676 | { |
Greg Clayton | c7bece56 | 2013-01-25 18:06:21 +0000 | [diff] [blame] | 1677 | const size_t num_matches = matches.GetSize(); |
Filipe Cabecinhas | af1537f | 2012-05-16 23:25:54 +0000 | [diff] [blame] | 1678 | if (matches.GetSize() > 1) { |
Greg Clayton | c7bece56 | 2013-01-25 18:06:21 +0000 | [diff] [blame] | 1679 | StreamString error_msg; |
| 1680 | 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] | 1681 | |
| 1682 | for (uint32_t i = 0; i < num_matches; ++i) { |
Greg Clayton | c7bece56 | 2013-01-25 18:06:21 +0000 | [diff] [blame] | 1683 | error_msg.Printf ("\t%s\n", matches.GetStringAtIndex(i)); |
Filipe Cabecinhas | af1537f | 2012-05-16 23:25:54 +0000 | [diff] [blame] | 1684 | } |
Greg Clayton | c7bece56 | 2013-01-25 18:06:21 +0000 | [diff] [blame] | 1685 | result.AppendRawError (error_msg.GetString().c_str()); |
Filipe Cabecinhas | af1537f | 2012-05-16 23:25:54 +0000 | [diff] [blame] | 1686 | } else { |
| 1687 | // We didn't have only one match, otherwise we wouldn't get here. |
| 1688 | assert(num_matches == 0); |
| 1689 | result.AppendErrorWithFormat ("'%s' is not a valid command.\n", next_word.c_str()); |
| 1690 | } |
Caroline Tice | 844d230 | 2010-12-09 22:52:49 +0000 | [diff] [blame] | 1691 | result.SetStatus (eReturnStatusFailed); |
| 1692 | return false; |
| 1693 | } |
| 1694 | |
Greg Clayton | 5521f99 | 2011-10-28 21:38:01 +0000 | [diff] [blame] | 1695 | if (cmd_obj->IsMultiwordObject ()) |
| 1696 | { |
| 1697 | if (!suffix.empty()) |
| 1698 | { |
| 1699 | |
Enrico Granata | aded51d | 2013-06-12 00:44:43 +0000 | [diff] [blame] | 1700 | result.AppendErrorWithFormat ("command '%s' did not recognize '%s%s%s' as valid (subcommand might be invalid).\n", |
| 1701 | cmd_obj->GetCommandName(), |
| 1702 | next_word.empty() ? "" : next_word.c_str(), |
| 1703 | next_word.empty() ? " -- " : " ", |
Greg Clayton | 5521f99 | 2011-10-28 21:38:01 +0000 | [diff] [blame] | 1704 | suffix.c_str()); |
| 1705 | result.SetStatus (eReturnStatusFailed); |
| 1706 | return false; |
| 1707 | } |
| 1708 | } |
| 1709 | else |
| 1710 | { |
| 1711 | // If we found a normal command, we are done |
| 1712 | done = true; |
| 1713 | if (!suffix.empty()) |
| 1714 | { |
| 1715 | switch (suffix[0]) |
| 1716 | { |
| 1717 | case '/': |
| 1718 | // GDB format suffixes |
Greg Clayton | 52ec56c | 2011-10-29 00:57:28 +0000 | [diff] [blame] | 1719 | { |
| 1720 | Options *command_options = cmd_obj->GetOptions(); |
| 1721 | if (command_options && command_options->SupportsLongOption("gdb-format")) |
| 1722 | { |
Greg Clayton | 93c62e6 | 2011-11-09 23:25:03 +0000 | [diff] [blame] | 1723 | std::string gdb_format_option ("--gdb-format="); |
| 1724 | gdb_format_option += (suffix.c_str() + 1); |
| 1725 | |
| 1726 | bool inserted = false; |
| 1727 | std::string &cmd = revised_command_line.GetString(); |
| 1728 | size_t arg_terminator_idx = FindArgumentTerminator (cmd); |
| 1729 | if (arg_terminator_idx != std::string::npos) |
| 1730 | { |
| 1731 | // Insert the gdb format option before the "--" that terminates options |
| 1732 | gdb_format_option.append(1,' '); |
| 1733 | cmd.insert(arg_terminator_idx, gdb_format_option); |
| 1734 | inserted = true; |
| 1735 | } |
| 1736 | |
| 1737 | if (!inserted) |
| 1738 | revised_command_line.Printf (" %s", gdb_format_option.c_str()); |
| 1739 | |
| 1740 | if (wants_raw_input && FindArgumentTerminator(cmd) == std::string::npos) |
| 1741 | revised_command_line.PutCString (" --"); |
Greg Clayton | 52ec56c | 2011-10-29 00:57:28 +0000 | [diff] [blame] | 1742 | } |
| 1743 | else |
| 1744 | { |
| 1745 | result.AppendErrorWithFormat ("the '%s' command doesn't support the --gdb-format option\n", |
| 1746 | cmd_obj->GetCommandName()); |
| 1747 | result.SetStatus (eReturnStatusFailed); |
| 1748 | return false; |
| 1749 | } |
| 1750 | } |
Greg Clayton | 5521f99 | 2011-10-28 21:38:01 +0000 | [diff] [blame] | 1751 | break; |
Johnny Chen | 8e9383d | 2011-10-31 22:22:06 +0000 | [diff] [blame] | 1752 | |
| 1753 | default: |
| 1754 | result.AppendErrorWithFormat ("unknown command shorthand suffix: '%s'\n", |
| 1755 | suffix.c_str()); |
| 1756 | result.SetStatus (eReturnStatusFailed); |
| 1757 | return false; |
| 1758 | |
Greg Clayton | 5521f99 | 2011-10-28 21:38:01 +0000 | [diff] [blame] | 1759 | } |
| 1760 | } |
| 1761 | } |
Caroline Tice | 844d230 | 2010-12-09 22:52:49 +0000 | [diff] [blame] | 1762 | if (command_string.length() == 0) |
| 1763 | done = true; |
| 1764 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1765 | } |
Caroline Tice | 844d230 | 2010-12-09 22:52:49 +0000 | [diff] [blame] | 1766 | |
Greg Clayton | 5521f99 | 2011-10-28 21:38:01 +0000 | [diff] [blame] | 1767 | if (!command_string.empty()) |
Caroline Tice | 844d230 | 2010-12-09 22:52:49 +0000 | [diff] [blame] | 1768 | revised_command_line.Printf (" %s", command_string.c_str()); |
| 1769 | |
| 1770 | // End of Phase 1. |
| 1771 | // At this point cmd_obj should contain the CommandObject whose Execute method will be called, if the command |
| 1772 | // specified was valid; revised_command_line contains the complete command line (including command name(s)), |
| 1773 | // fully translated with all substitutions & translations taken care of (still in raw text format); and |
| 1774 | // wants_raw_input specifies whether the Execute method expects raw input or not. |
| 1775 | |
| 1776 | |
| 1777 | if (log) |
| 1778 | { |
| 1779 | log->Printf ("HandleCommand, cmd_obj : '%s'", cmd_obj ? cmd_obj->GetCommandName() : "<not found>"); |
| 1780 | log->Printf ("HandleCommand, revised_command_line: '%s'", revised_command_line.GetData()); |
| 1781 | log->Printf ("HandleCommand, wants_raw_input:'%s'", wants_raw_input ? "True" : "False"); |
| 1782 | } |
| 1783 | |
| 1784 | // Phase 2. |
| 1785 | // Take care of things like setting up the history command & calling the appropriate Execute method on the |
| 1786 | // CommandObject, with the appropriate arguments. |
| 1787 | |
| 1788 | if (cmd_obj != NULL) |
| 1789 | { |
| 1790 | if (add_to_history) |
| 1791 | { |
| 1792 | Args command_args (revised_command_line.GetData()); |
| 1793 | const char *repeat_command = cmd_obj->GetRepeatCommand(command_args, 0); |
| 1794 | if (repeat_command != NULL) |
| 1795 | m_repeat_command.assign(repeat_command); |
| 1796 | else |
Jim Ingham | a5a97eb | 2011-07-12 03:12:18 +0000 | [diff] [blame] | 1797 | m_repeat_command.assign(original_command_string.c_str()); |
Caroline Tice | 844d230 | 2010-12-09 22:52:49 +0000 | [diff] [blame] | 1798 | |
Enrico Granata | 7594f14 | 2013-06-17 22:51:50 +0000 | [diff] [blame^] | 1799 | m_command_history.AppendString (original_command_string); |
Caroline Tice | 844d230 | 2010-12-09 22:52:49 +0000 | [diff] [blame] | 1800 | } |
| 1801 | |
| 1802 | command_string = revised_command_line.GetData(); |
| 1803 | std::string command_name (cmd_obj->GetCommandName()); |
Caroline Tice | 01274c0 | 2010-12-11 08:16:56 +0000 | [diff] [blame] | 1804 | std::string remainder; |
| 1805 | if (actual_cmd_name_len < command_string.length()) |
| 1806 | remainder = command_string.substr (actual_cmd_name_len); // Note: 'actual_cmd_name_len' may be considerably shorter |
| 1807 | // than cmd_obj->GetCommandName(), because name completion |
| 1808 | // allows users to enter short versions of the names, |
| 1809 | // e.g. 'br s' for 'breakpoint set'. |
Caroline Tice | 844d230 | 2010-12-09 22:52:49 +0000 | [diff] [blame] | 1810 | |
| 1811 | // Remove any initial spaces |
| 1812 | std::string white_space (" \t\v"); |
| 1813 | size_t pos = remainder.find_first_not_of (white_space); |
| 1814 | if (pos != 0 && pos != std::string::npos) |
Greg Clayton | a348259 | 2011-04-22 20:58:45 +0000 | [diff] [blame] | 1815 | remainder.erase(0, pos); |
Caroline Tice | 844d230 | 2010-12-09 22:52:49 +0000 | [diff] [blame] | 1816 | |
| 1817 | if (log) |
Jason Molenda | bfb36ff | 2011-08-25 00:20:04 +0000 | [diff] [blame] | 1818 | 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] | 1819 | |
Jim Ingham | 5a98841 | 2012-06-08 21:56:10 +0000 | [diff] [blame] | 1820 | cmd_obj->Execute (remainder.c_str(), result); |
Caroline Tice | 844d230 | 2010-12-09 22:52:49 +0000 | [diff] [blame] | 1821 | } |
| 1822 | else |
| 1823 | { |
| 1824 | // We didn't find the first command object, so complete the first argument. |
| 1825 | Args command_args (revised_command_line.GetData()); |
| 1826 | StringList matches; |
| 1827 | int num_matches; |
| 1828 | int cursor_index = 0; |
| 1829 | int cursor_char_position = strlen (command_args.GetArgumentAtIndex(0)); |
| 1830 | bool word_complete; |
| 1831 | num_matches = HandleCompletionMatches (command_args, |
| 1832 | cursor_index, |
| 1833 | cursor_char_position, |
| 1834 | 0, |
| 1835 | -1, |
| 1836 | word_complete, |
| 1837 | matches); |
| 1838 | |
| 1839 | if (num_matches > 0) |
| 1840 | { |
| 1841 | std::string error_msg; |
| 1842 | error_msg.assign ("ambiguous command '"); |
| 1843 | error_msg.append(command_args.GetArgumentAtIndex(0)); |
| 1844 | error_msg.append ("'."); |
| 1845 | |
| 1846 | error_msg.append (" Possible completions:"); |
| 1847 | for (int i = 0; i < num_matches; i++) |
| 1848 | { |
| 1849 | error_msg.append ("\n\t"); |
| 1850 | error_msg.append (matches.GetStringAtIndex (i)); |
| 1851 | } |
| 1852 | error_msg.append ("\n"); |
Greg Clayton | c7bece56 | 2013-01-25 18:06:21 +0000 | [diff] [blame] | 1853 | result.AppendRawError (error_msg.c_str()); |
Caroline Tice | 844d230 | 2010-12-09 22:52:49 +0000 | [diff] [blame] | 1854 | } |
| 1855 | else |
| 1856 | result.AppendErrorWithFormat ("Unrecognized command '%s'.\n", command_args.GetArgumentAtIndex (0)); |
| 1857 | |
| 1858 | result.SetStatus (eReturnStatusFailed); |
| 1859 | } |
| 1860 | |
Jason Molenda | bfb36ff | 2011-08-25 00:20:04 +0000 | [diff] [blame] | 1861 | if (log) |
| 1862 | log->Printf ("HandleCommand, command %s", (result.Succeeded() ? "succeeded" : "did not succeed")); |
| 1863 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1864 | return result.Succeeded(); |
| 1865 | } |
| 1866 | |
| 1867 | int |
| 1868 | CommandInterpreter::HandleCompletionMatches (Args &parsed_line, |
| 1869 | int &cursor_index, |
| 1870 | int &cursor_char_position, |
| 1871 | int match_start_point, |
| 1872 | int max_return_elements, |
Jim Ingham | 558ce12 | 2010-06-30 05:02:46 +0000 | [diff] [blame] | 1873 | bool &word_complete, |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1874 | StringList &matches) |
| 1875 | { |
| 1876 | int num_command_matches = 0; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1877 | bool look_for_subcommand = false; |
Jim Ingham | 558ce12 | 2010-06-30 05:02:46 +0000 | [diff] [blame] | 1878 | |
| 1879 | // For any of the command completions a unique match will be a complete word. |
| 1880 | word_complete = true; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1881 | |
| 1882 | if (cursor_index == -1) |
| 1883 | { |
| 1884 | // 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] | 1885 | bool include_aliases = true; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1886 | num_command_matches = GetCommandNamesMatchingPartialString ("", include_aliases, matches); |
| 1887 | } |
| 1888 | else if (cursor_index == 0) |
| 1889 | { |
| 1890 | // 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] | 1891 | CommandObject *cmd_obj = GetCommandObject (parsed_line.GetArgumentAtIndex(0), &matches); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1892 | num_command_matches = matches.GetSize(); |
| 1893 | |
| 1894 | if (num_command_matches == 1 |
| 1895 | && cmd_obj && cmd_obj->IsMultiwordObject() |
| 1896 | && matches.GetStringAtIndex(0) != NULL |
| 1897 | && strcmp (parsed_line.GetArgumentAtIndex(0), matches.GetStringAtIndex(0)) == 0) |
| 1898 | { |
| 1899 | look_for_subcommand = true; |
| 1900 | num_command_matches = 0; |
| 1901 | matches.DeleteStringAtIndex(0); |
| 1902 | parsed_line.AppendArgument (""); |
| 1903 | cursor_index++; |
| 1904 | cursor_char_position = 0; |
| 1905 | } |
| 1906 | } |
| 1907 | |
| 1908 | if (cursor_index > 0 || look_for_subcommand) |
| 1909 | { |
| 1910 | // We are completing further on into a commands arguments, so find the command and tell it |
| 1911 | // to complete the command. |
| 1912 | // First see if there is a matching initial command: |
Jim Ingham | 279a6c2 | 2010-07-06 22:46:59 +0000 | [diff] [blame] | 1913 | CommandObject *command_object = GetCommandObject (parsed_line.GetArgumentAtIndex(0)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1914 | if (command_object == NULL) |
| 1915 | { |
| 1916 | return 0; |
| 1917 | } |
| 1918 | else |
| 1919 | { |
| 1920 | parsed_line.Shift(); |
| 1921 | cursor_index--; |
Greg Clayton | a701509 | 2010-09-18 01:14:36 +0000 | [diff] [blame] | 1922 | num_command_matches = command_object->HandleCompletion (parsed_line, |
Greg Clayton | 6611103 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 1923 | cursor_index, |
| 1924 | cursor_char_position, |
| 1925 | match_start_point, |
Jim Ingham | 558ce12 | 2010-06-30 05:02:46 +0000 | [diff] [blame] | 1926 | max_return_elements, |
| 1927 | word_complete, |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1928 | matches); |
| 1929 | } |
| 1930 | } |
| 1931 | |
| 1932 | return num_command_matches; |
| 1933 | |
| 1934 | } |
| 1935 | |
| 1936 | int |
| 1937 | CommandInterpreter::HandleCompletion (const char *current_line, |
| 1938 | const char *cursor, |
| 1939 | const char *last_char, |
| 1940 | int match_start_point, |
| 1941 | int max_return_elements, |
| 1942 | StringList &matches) |
| 1943 | { |
| 1944 | // We parse the argument up to the cursor, so the last argument in parsed_line is |
| 1945 | // the one containing the cursor, and the cursor is after the last character. |
| 1946 | |
| 1947 | Args parsed_line(current_line, last_char - current_line); |
| 1948 | Args partial_parsed_line(current_line, cursor - current_line); |
| 1949 | |
Jim Ingham | a5a97eb | 2011-07-12 03:12:18 +0000 | [diff] [blame] | 1950 | // Don't complete comments, and if the line we are completing is just the history repeat character, |
| 1951 | // substitute the appropriate history line. |
| 1952 | const char *first_arg = parsed_line.GetArgumentAtIndex(0); |
| 1953 | if (first_arg) |
| 1954 | { |
| 1955 | if (first_arg[0] == m_comment_char) |
| 1956 | return 0; |
Enrico Granata | 7594f14 | 2013-06-17 22:51:50 +0000 | [diff] [blame^] | 1957 | else if (first_arg[0] == CommandHistory::g_repeat_char) |
Jim Ingham | a5a97eb | 2011-07-12 03:12:18 +0000 | [diff] [blame] | 1958 | { |
Enrico Granata | 7594f14 | 2013-06-17 22:51:50 +0000 | [diff] [blame^] | 1959 | const char *history_string = m_command_history.FindString (first_arg); |
Jim Ingham | a5a97eb | 2011-07-12 03:12:18 +0000 | [diff] [blame] | 1960 | if (history_string != NULL) |
| 1961 | { |
| 1962 | matches.Clear(); |
| 1963 | matches.InsertStringAtIndex(0, history_string); |
| 1964 | return -2; |
| 1965 | } |
| 1966 | else |
| 1967 | return 0; |
| 1968 | |
| 1969 | } |
| 1970 | } |
| 1971 | |
| 1972 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1973 | int num_args = partial_parsed_line.GetArgumentCount(); |
| 1974 | int cursor_index = partial_parsed_line.GetArgumentCount() - 1; |
| 1975 | int cursor_char_position; |
| 1976 | |
| 1977 | if (cursor_index == -1) |
| 1978 | cursor_char_position = 0; |
| 1979 | else |
| 1980 | cursor_char_position = strlen (partial_parsed_line.GetArgumentAtIndex(cursor_index)); |
Jim Ingham | fe0c425 | 2010-12-14 19:56:01 +0000 | [diff] [blame] | 1981 | |
| 1982 | if (cursor > current_line && cursor[-1] == ' ') |
| 1983 | { |
| 1984 | // We are just after a space. If we are in an argument, then we will continue |
| 1985 | // parsing, but if we are between arguments, then we have to complete whatever the next |
| 1986 | // element would be. |
| 1987 | // We can distinguish the two cases because if we are in an argument (e.g. because the space is |
| 1988 | // protected by a quote) then the space will also be in the parsed argument... |
| 1989 | |
| 1990 | const char *current_elem = partial_parsed_line.GetArgumentAtIndex(cursor_index); |
| 1991 | if (cursor_char_position == 0 || current_elem[cursor_char_position - 1] != ' ') |
| 1992 | { |
| 1993 | parsed_line.InsertArgumentAtIndex(cursor_index + 1, "", '"'); |
| 1994 | cursor_index++; |
| 1995 | cursor_char_position = 0; |
| 1996 | } |
| 1997 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1998 | |
| 1999 | int num_command_matches; |
| 2000 | |
| 2001 | matches.Clear(); |
| 2002 | |
| 2003 | // Only max_return_elements == -1 is supported at present: |
| 2004 | assert (max_return_elements == -1); |
Jim Ingham | 558ce12 | 2010-06-30 05:02:46 +0000 | [diff] [blame] | 2005 | bool word_complete; |
Greg Clayton | 6611103 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 2006 | num_command_matches = HandleCompletionMatches (parsed_line, |
| 2007 | cursor_index, |
| 2008 | cursor_char_position, |
| 2009 | match_start_point, |
Jim Ingham | 558ce12 | 2010-06-30 05:02:46 +0000 | [diff] [blame] | 2010 | max_return_elements, |
| 2011 | word_complete, |
Greg Clayton | 6611103 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 2012 | matches); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2013 | |
| 2014 | if (num_command_matches <= 0) |
| 2015 | return num_command_matches; |
| 2016 | |
| 2017 | if (num_args == 0) |
| 2018 | { |
| 2019 | // If we got an empty string, insert nothing. |
| 2020 | matches.InsertStringAtIndex(0, ""); |
| 2021 | } |
| 2022 | else |
| 2023 | { |
| 2024 | // Now figure out if there is a common substring, and if so put that in element 0, otherwise |
| 2025 | // put an empty string in element 0. |
| 2026 | std::string command_partial_str; |
| 2027 | if (cursor_index >= 0) |
Jim Ingham | 49e80a1 | 2010-10-22 18:47:16 +0000 | [diff] [blame] | 2028 | command_partial_str.assign(parsed_line.GetArgumentAtIndex(cursor_index), |
| 2029 | parsed_line.GetArgumentAtIndex(cursor_index) + cursor_char_position); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2030 | |
| 2031 | std::string common_prefix; |
| 2032 | matches.LongestCommonPrefix (common_prefix); |
Greg Clayton | c7bece56 | 2013-01-25 18:06:21 +0000 | [diff] [blame] | 2033 | const size_t partial_name_len = command_partial_str.size(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2034 | |
| 2035 | // If we matched a unique single command, add a space... |
Jim Ingham | 558ce12 | 2010-06-30 05:02:46 +0000 | [diff] [blame] | 2036 | // Only do this if the completer told us this was a complete word, however... |
| 2037 | if (num_command_matches == 1 && word_complete) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2038 | { |
| 2039 | char quote_char = parsed_line.GetArgumentQuoteCharAtIndex(cursor_index); |
| 2040 | if (quote_char != '\0') |
| 2041 | common_prefix.push_back(quote_char); |
| 2042 | |
| 2043 | common_prefix.push_back(' '); |
| 2044 | } |
| 2045 | common_prefix.erase (0, partial_name_len); |
| 2046 | matches.InsertStringAtIndex(0, common_prefix.c_str()); |
| 2047 | } |
| 2048 | return num_command_matches; |
| 2049 | } |
| 2050 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2051 | |
| 2052 | CommandInterpreter::~CommandInterpreter () |
| 2053 | { |
| 2054 | } |
| 2055 | |
| 2056 | const char * |
| 2057 | CommandInterpreter::GetPrompt () |
| 2058 | { |
Caroline Tice | daccaa9 | 2010-09-20 20:44:43 +0000 | [diff] [blame] | 2059 | return m_debugger.GetPrompt(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2060 | } |
| 2061 | |
| 2062 | void |
| 2063 | CommandInterpreter::SetPrompt (const char *new_prompt) |
| 2064 | { |
Caroline Tice | daccaa9 | 2010-09-20 20:44:43 +0000 | [diff] [blame] | 2065 | m_debugger.SetPrompt (new_prompt); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2066 | } |
| 2067 | |
Jim Ingham | 97a6dc7 | 2010-10-04 19:49:29 +0000 | [diff] [blame] | 2068 | size_t |
Greg Clayton | 51b1e2d | 2011-02-09 01:08:52 +0000 | [diff] [blame] | 2069 | CommandInterpreter::GetConfirmationInputReaderCallback |
| 2070 | ( |
| 2071 | void *baton, |
| 2072 | InputReader &reader, |
| 2073 | lldb::InputReaderAction action, |
| 2074 | const char *bytes, |
| 2075 | size_t bytes_len |
| 2076 | ) |
Jim Ingham | 97a6dc7 | 2010-10-04 19:49:29 +0000 | [diff] [blame] | 2077 | { |
Greg Clayton | 51b1e2d | 2011-02-09 01:08:52 +0000 | [diff] [blame] | 2078 | File &out_file = reader.GetDebugger().GetOutputFile(); |
Jim Ingham | 97a6dc7 | 2010-10-04 19:49:29 +0000 | [diff] [blame] | 2079 | bool *response_ptr = (bool *) baton; |
| 2080 | |
| 2081 | switch (action) |
| 2082 | { |
| 2083 | case eInputReaderActivate: |
Greg Clayton | 51b1e2d | 2011-02-09 01:08:52 +0000 | [diff] [blame] | 2084 | if (out_file.IsValid()) |
Jim Ingham | 97a6dc7 | 2010-10-04 19:49:29 +0000 | [diff] [blame] | 2085 | { |
| 2086 | if (reader.GetPrompt()) |
Caroline Tice | 31f7d46 | 2011-02-02 01:17:56 +0000 | [diff] [blame] | 2087 | { |
Greg Clayton | 51b1e2d | 2011-02-09 01:08:52 +0000 | [diff] [blame] | 2088 | out_file.Printf ("%s", reader.GetPrompt()); |
| 2089 | out_file.Flush (); |
Caroline Tice | 31f7d46 | 2011-02-02 01:17:56 +0000 | [diff] [blame] | 2090 | } |
Jim Ingham | 97a6dc7 | 2010-10-04 19:49:29 +0000 | [diff] [blame] | 2091 | } |
| 2092 | break; |
| 2093 | |
| 2094 | case eInputReaderDeactivate: |
| 2095 | break; |
| 2096 | |
| 2097 | case eInputReaderReactivate: |
Greg Clayton | 51b1e2d | 2011-02-09 01:08:52 +0000 | [diff] [blame] | 2098 | if (out_file.IsValid() && reader.GetPrompt()) |
Caroline Tice | 31f7d46 | 2011-02-02 01:17:56 +0000 | [diff] [blame] | 2099 | { |
Greg Clayton | 51b1e2d | 2011-02-09 01:08:52 +0000 | [diff] [blame] | 2100 | out_file.Printf ("%s", reader.GetPrompt()); |
| 2101 | out_file.Flush (); |
Caroline Tice | 31f7d46 | 2011-02-02 01:17:56 +0000 | [diff] [blame] | 2102 | } |
Jim Ingham | 97a6dc7 | 2010-10-04 19:49:29 +0000 | [diff] [blame] | 2103 | break; |
Caroline Tice | 969ed3d | 2011-05-02 20:41:46 +0000 | [diff] [blame] | 2104 | |
| 2105 | case eInputReaderAsynchronousOutputWritten: |
| 2106 | break; |
| 2107 | |
Jim Ingham | 97a6dc7 | 2010-10-04 19:49:29 +0000 | [diff] [blame] | 2108 | case eInputReaderGotToken: |
| 2109 | if (bytes_len == 0) |
| 2110 | { |
| 2111 | reader.SetIsDone(true); |
| 2112 | } |
Jim Ingham | c8b4758 | 2011-11-14 20:02:01 +0000 | [diff] [blame] | 2113 | else if (bytes[0] == 'y' || bytes[0] == 'Y') |
Jim Ingham | 97a6dc7 | 2010-10-04 19:49:29 +0000 | [diff] [blame] | 2114 | { |
| 2115 | *response_ptr = true; |
| 2116 | reader.SetIsDone(true); |
| 2117 | } |
Jim Ingham | c8b4758 | 2011-11-14 20:02:01 +0000 | [diff] [blame] | 2118 | else if (bytes[0] == 'n' || bytes[0] == 'N') |
Jim Ingham | 97a6dc7 | 2010-10-04 19:49:29 +0000 | [diff] [blame] | 2119 | { |
| 2120 | *response_ptr = false; |
| 2121 | reader.SetIsDone(true); |
| 2122 | } |
| 2123 | else |
| 2124 | { |
Greg Clayton | 51b1e2d | 2011-02-09 01:08:52 +0000 | [diff] [blame] | 2125 | if (out_file.IsValid() && !reader.IsDone() && reader.GetPrompt()) |
Jim Ingham | 97a6dc7 | 2010-10-04 19:49:29 +0000 | [diff] [blame] | 2126 | { |
Jim Ingham | 78d6148 | 2011-11-17 01:22:00 +0000 | [diff] [blame] | 2127 | out_file.Printf ("Please answer \"y\" or \"n\".\n%s", reader.GetPrompt()); |
Greg Clayton | 51b1e2d | 2011-02-09 01:08:52 +0000 | [diff] [blame] | 2128 | out_file.Flush (); |
Jim Ingham | 97a6dc7 | 2010-10-04 19:49:29 +0000 | [diff] [blame] | 2129 | } |
| 2130 | } |
| 2131 | break; |
| 2132 | |
Caroline Tice | efed613 | 2010-11-19 20:47:54 +0000 | [diff] [blame] | 2133 | case eInputReaderInterrupt: |
| 2134 | case eInputReaderEndOfFile: |
| 2135 | *response_ptr = false; // Assume ^C or ^D means cancel the proposed action |
| 2136 | reader.SetIsDone (true); |
| 2137 | break; |
| 2138 | |
Jim Ingham | 97a6dc7 | 2010-10-04 19:49:29 +0000 | [diff] [blame] | 2139 | case eInputReaderDone: |
| 2140 | break; |
| 2141 | } |
| 2142 | |
| 2143 | return bytes_len; |
| 2144 | |
| 2145 | } |
| 2146 | |
| 2147 | bool |
| 2148 | CommandInterpreter::Confirm (const char *message, bool default_answer) |
| 2149 | { |
Jim Ingham | 3bcdb29 | 2010-10-04 22:44:14 +0000 | [diff] [blame] | 2150 | // Check AutoConfirm first: |
| 2151 | if (m_debugger.GetAutoConfirm()) |
| 2152 | return default_answer; |
| 2153 | |
Jim Ingham | 97a6dc7 | 2010-10-04 19:49:29 +0000 | [diff] [blame] | 2154 | InputReaderSP reader_sp (new InputReader(GetDebugger())); |
| 2155 | bool response = default_answer; |
| 2156 | if (reader_sp) |
| 2157 | { |
| 2158 | std::string prompt(message); |
| 2159 | prompt.append(": ["); |
| 2160 | if (default_answer) |
| 2161 | prompt.append ("Y/n] "); |
| 2162 | else |
| 2163 | prompt.append ("y/N] "); |
| 2164 | |
| 2165 | Error err (reader_sp->Initialize (CommandInterpreter::GetConfirmationInputReaderCallback, |
| 2166 | &response, // baton |
| 2167 | eInputReaderGranularityLine, // token size, to pass to callback function |
| 2168 | NULL, // end token |
| 2169 | prompt.c_str(), // prompt |
| 2170 | true)); // echo input |
| 2171 | if (err.Success()) |
| 2172 | { |
| 2173 | GetDebugger().PushInputReader (reader_sp); |
| 2174 | } |
| 2175 | reader_sp->WaitOnReaderIsDone(); |
| 2176 | } |
| 2177 | return response; |
| 2178 | } |
| 2179 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2180 | OptionArgVectorSP |
| 2181 | CommandInterpreter::GetAliasOptions (const char *alias_name) |
| 2182 | { |
| 2183 | OptionArgMap::iterator pos; |
| 2184 | OptionArgVectorSP ret_val; |
| 2185 | |
| 2186 | std::string alias (alias_name); |
| 2187 | |
| 2188 | if (HasAliasOptions()) |
| 2189 | { |
| 2190 | pos = m_alias_options.find (alias); |
| 2191 | if (pos != m_alias_options.end()) |
| 2192 | ret_val = pos->second; |
| 2193 | } |
| 2194 | |
| 2195 | return ret_val; |
| 2196 | } |
| 2197 | |
| 2198 | void |
| 2199 | CommandInterpreter::RemoveAliasOptions (const char *alias_name) |
| 2200 | { |
| 2201 | OptionArgMap::iterator pos = m_alias_options.find(alias_name); |
| 2202 | if (pos != m_alias_options.end()) |
| 2203 | { |
| 2204 | m_alias_options.erase (pos); |
| 2205 | } |
| 2206 | } |
| 2207 | |
| 2208 | void |
| 2209 | CommandInterpreter::AddOrReplaceAliasOptions (const char *alias_name, OptionArgVectorSP &option_arg_vector_sp) |
| 2210 | { |
| 2211 | m_alias_options[alias_name] = option_arg_vector_sp; |
| 2212 | } |
| 2213 | |
| 2214 | bool |
| 2215 | CommandInterpreter::HasCommands () |
| 2216 | { |
| 2217 | return (!m_command_dict.empty()); |
| 2218 | } |
| 2219 | |
| 2220 | bool |
| 2221 | CommandInterpreter::HasAliases () |
| 2222 | { |
| 2223 | return (!m_alias_dict.empty()); |
| 2224 | } |
| 2225 | |
| 2226 | bool |
| 2227 | CommandInterpreter::HasUserCommands () |
| 2228 | { |
| 2229 | return (!m_user_dict.empty()); |
| 2230 | } |
| 2231 | |
| 2232 | bool |
| 2233 | CommandInterpreter::HasAliasOptions () |
| 2234 | { |
| 2235 | return (!m_alias_options.empty()); |
| 2236 | } |
| 2237 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2238 | void |
Caroline Tice | 4ab31c9 | 2010-10-12 21:57:09 +0000 | [diff] [blame] | 2239 | CommandInterpreter::BuildAliasCommandArgs (CommandObject *alias_cmd_obj, |
| 2240 | const char *alias_name, |
| 2241 | Args &cmd_args, |
Caroline Tice | d9d6336 | 2010-12-07 19:58:26 +0000 | [diff] [blame] | 2242 | std::string &raw_input_string, |
Caroline Tice | 4ab31c9 | 2010-10-12 21:57:09 +0000 | [diff] [blame] | 2243 | CommandReturnObject &result) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2244 | { |
| 2245 | OptionArgVectorSP option_arg_vector_sp = GetAliasOptions (alias_name); |
Caroline Tice | d9d6336 | 2010-12-07 19:58:26 +0000 | [diff] [blame] | 2246 | |
| 2247 | bool wants_raw_input = alias_cmd_obj->WantsRawCommandString(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2248 | |
Caroline Tice | d9d6336 | 2010-12-07 19:58:26 +0000 | [diff] [blame] | 2249 | // Make sure that the alias name is the 0th element in cmd_args |
| 2250 | std::string alias_name_str = alias_name; |
| 2251 | if (alias_name_str.compare (cmd_args.GetArgumentAtIndex(0)) != 0) |
| 2252 | cmd_args.Unshift (alias_name); |
| 2253 | |
| 2254 | Args new_args (alias_cmd_obj->GetCommandName()); |
| 2255 | if (new_args.GetArgumentCount() == 2) |
| 2256 | new_args.Shift(); |
| 2257 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2258 | if (option_arg_vector_sp.get()) |
| 2259 | { |
Caroline Tice | d9d6336 | 2010-12-07 19:58:26 +0000 | [diff] [blame] | 2260 | if (wants_raw_input) |
| 2261 | { |
| 2262 | // We have a command that both has command options and takes raw input. Make *sure* it has a |
| 2263 | // " -- " in the right place in the raw_input_string. |
| 2264 | size_t pos = raw_input_string.find(" -- "); |
| 2265 | if (pos == std::string::npos) |
| 2266 | { |
| 2267 | // None found; assume it goes at the beginning of the raw input string |
| 2268 | raw_input_string.insert (0, " -- "); |
| 2269 | } |
| 2270 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2271 | |
| 2272 | OptionArgVector *option_arg_vector = option_arg_vector_sp.get(); |
Greg Clayton | c7bece56 | 2013-01-25 18:06:21 +0000 | [diff] [blame] | 2273 | const size_t old_size = cmd_args.GetArgumentCount(); |
Caroline Tice | 4ab31c9 | 2010-10-12 21:57:09 +0000 | [diff] [blame] | 2274 | std::vector<bool> used (old_size + 1, false); |
| 2275 | |
| 2276 | used[0] = true; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2277 | |
| 2278 | for (int i = 0; i < option_arg_vector->size(); ++i) |
| 2279 | { |
| 2280 | OptionArgPair option_pair = (*option_arg_vector)[i]; |
Caroline Tice | d9d6336 | 2010-12-07 19:58:26 +0000 | [diff] [blame] | 2281 | OptionArgValue value_pair = option_pair.second; |
| 2282 | int value_type = value_pair.first; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2283 | std::string option = option_pair.first; |
Caroline Tice | d9d6336 | 2010-12-07 19:58:26 +0000 | [diff] [blame] | 2284 | std::string value = value_pair.second; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2285 | if (option.compare ("<argument>") == 0) |
Caroline Tice | d9d6336 | 2010-12-07 19:58:26 +0000 | [diff] [blame] | 2286 | { |
| 2287 | if (!wants_raw_input |
| 2288 | || (value.compare("--") != 0)) // Since we inserted this above, make sure we don't insert it twice |
| 2289 | new_args.AppendArgument (value.c_str()); |
| 2290 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2291 | else |
| 2292 | { |
Caroline Tice | d9d6336 | 2010-12-07 19:58:26 +0000 | [diff] [blame] | 2293 | if (value_type != optional_argument) |
| 2294 | new_args.AppendArgument (option.c_str()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2295 | if (value.compare ("<no-argument>") != 0) |
| 2296 | { |
| 2297 | int index = GetOptionArgumentPosition (value.c_str()); |
| 2298 | if (index == 0) |
Caroline Tice | d9d6336 | 2010-12-07 19:58:26 +0000 | [diff] [blame] | 2299 | { |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2300 | // value was NOT a positional argument; must be a real value |
Caroline Tice | d9d6336 | 2010-12-07 19:58:26 +0000 | [diff] [blame] | 2301 | if (value_type != optional_argument) |
| 2302 | new_args.AppendArgument (value.c_str()); |
| 2303 | else |
| 2304 | { |
| 2305 | char buffer[255]; |
| 2306 | ::snprintf (buffer, sizeof (buffer), "%s%s", option.c_str(), value.c_str()); |
| 2307 | new_args.AppendArgument (buffer); |
| 2308 | } |
| 2309 | |
| 2310 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2311 | else if (index >= cmd_args.GetArgumentCount()) |
| 2312 | { |
| 2313 | result.AppendErrorWithFormat |
| 2314 | ("Not enough arguments provided; you need at least %d arguments to use this alias.\n", |
| 2315 | index); |
| 2316 | result.SetStatus (eReturnStatusFailed); |
| 2317 | return; |
| 2318 | } |
| 2319 | else |
| 2320 | { |
Caroline Tice | d9d6336 | 2010-12-07 19:58:26 +0000 | [diff] [blame] | 2321 | // Find and remove cmd_args.GetArgumentAtIndex(i) from raw_input_string |
| 2322 | size_t strpos = raw_input_string.find (cmd_args.GetArgumentAtIndex (index)); |
| 2323 | if (strpos != std::string::npos) |
| 2324 | { |
| 2325 | raw_input_string = raw_input_string.erase (strpos, strlen (cmd_args.GetArgumentAtIndex (index))); |
| 2326 | } |
| 2327 | |
| 2328 | if (value_type != optional_argument) |
| 2329 | new_args.AppendArgument (cmd_args.GetArgumentAtIndex (index)); |
| 2330 | else |
| 2331 | { |
| 2332 | char buffer[255]; |
| 2333 | ::snprintf (buffer, sizeof(buffer), "%s%s", option.c_str(), |
| 2334 | cmd_args.GetArgumentAtIndex (index)); |
| 2335 | new_args.AppendArgument (buffer); |
| 2336 | } |
Caroline Tice | 4ab31c9 | 2010-10-12 21:57:09 +0000 | [diff] [blame] | 2337 | used[index] = true; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2338 | } |
| 2339 | } |
| 2340 | } |
| 2341 | } |
| 2342 | |
| 2343 | for (int j = 0; j < cmd_args.GetArgumentCount(); ++j) |
| 2344 | { |
Caroline Tice | d9d6336 | 2010-12-07 19:58:26 +0000 | [diff] [blame] | 2345 | if (!used[j] && !wants_raw_input) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2346 | new_args.AppendArgument (cmd_args.GetArgumentAtIndex (j)); |
| 2347 | } |
| 2348 | |
| 2349 | cmd_args.Clear(); |
| 2350 | cmd_args.SetArguments (new_args.GetArgumentCount(), (const char **) new_args.GetArgumentVector()); |
| 2351 | } |
| 2352 | else |
| 2353 | { |
| 2354 | result.SetStatus (eReturnStatusSuccessFinishNoResult); |
Caroline Tice | d9d6336 | 2010-12-07 19:58:26 +0000 | [diff] [blame] | 2355 | // This alias was not created with any options; nothing further needs to be done, unless it is a command that |
| 2356 | // wants raw input, in which case we need to clear the rest of the data from cmd_args, since its in the raw |
| 2357 | // input string. |
| 2358 | if (wants_raw_input) |
| 2359 | { |
| 2360 | cmd_args.Clear(); |
| 2361 | cmd_args.SetArguments (new_args.GetArgumentCount(), (const char **) new_args.GetArgumentVector()); |
| 2362 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2363 | return; |
| 2364 | } |
| 2365 | |
| 2366 | result.SetStatus (eReturnStatusSuccessFinishNoResult); |
| 2367 | return; |
| 2368 | } |
| 2369 | |
| 2370 | |
| 2371 | int |
| 2372 | CommandInterpreter::GetOptionArgumentPosition (const char *in_string) |
| 2373 | { |
| 2374 | int position = 0; // Any string that isn't an argument position, i.e. '%' followed by an integer, gets a position |
| 2375 | // of zero. |
| 2376 | |
| 2377 | char *cptr = (char *) in_string; |
| 2378 | |
| 2379 | // Does it start with '%' |
| 2380 | if (cptr[0] == '%') |
| 2381 | { |
| 2382 | ++cptr; |
| 2383 | |
| 2384 | // Is the rest of it entirely digits? |
| 2385 | if (isdigit (cptr[0])) |
| 2386 | { |
| 2387 | const char *start = cptr; |
| 2388 | while (isdigit (cptr[0])) |
| 2389 | ++cptr; |
| 2390 | |
| 2391 | // We've gotten to the end of the digits; are we at the end of the string? |
| 2392 | if (cptr[0] == '\0') |
| 2393 | position = atoi (start); |
| 2394 | } |
| 2395 | } |
| 2396 | |
| 2397 | return position; |
| 2398 | } |
| 2399 | |
| 2400 | void |
| 2401 | CommandInterpreter::SourceInitFile (bool in_cwd, CommandReturnObject &result) |
| 2402 | { |
Jim Ingham | 16e0c68 | 2011-08-12 23:34:31 +0000 | [diff] [blame] | 2403 | FileSpec init_file; |
Greg Clayton | 14a3551 | 2011-09-11 00:01:44 +0000 | [diff] [blame] | 2404 | if (in_cwd) |
Jim Ingham | 16e0c68 | 2011-08-12 23:34:31 +0000 | [diff] [blame] | 2405 | { |
Greg Clayton | 14a3551 | 2011-09-11 00:01:44 +0000 | [diff] [blame] | 2406 | // In the current working directory we don't load any program specific |
| 2407 | // .lldbinit files, we only look for a "./.lldbinit" file. |
| 2408 | if (m_skip_lldbinit_files) |
| 2409 | return; |
| 2410 | |
| 2411 | init_file.SetFile ("./.lldbinit", true); |
Jim Ingham | 16e0c68 | 2011-08-12 23:34:31 +0000 | [diff] [blame] | 2412 | } |
Greg Clayton | 14a3551 | 2011-09-11 00:01:44 +0000 | [diff] [blame] | 2413 | else |
Jim Ingham | 16e0c68 | 2011-08-12 23:34:31 +0000 | [diff] [blame] | 2414 | { |
Greg Clayton | 14a3551 | 2011-09-11 00:01:44 +0000 | [diff] [blame] | 2415 | // If we aren't looking in the current working directory we are looking |
| 2416 | // in the home directory. We will first see if there is an application |
| 2417 | // specific ".lldbinit" file whose name is "~/.lldbinit" followed by a |
| 2418 | // "-" and the name of the program. If this file doesn't exist, we fall |
| 2419 | // back to just the "~/.lldbinit" file. We also obey any requests to not |
| 2420 | // load the init files. |
| 2421 | const char *init_file_path = "~/.lldbinit"; |
| 2422 | |
| 2423 | if (m_skip_app_init_files == false) |
| 2424 | { |
| 2425 | FileSpec program_file_spec (Host::GetProgramFileSpec()); |
| 2426 | const char *program_name = program_file_spec.GetFilename().AsCString(); |
Jim Ingham | 16e0c68 | 2011-08-12 23:34:31 +0000 | [diff] [blame] | 2427 | |
Greg Clayton | 14a3551 | 2011-09-11 00:01:44 +0000 | [diff] [blame] | 2428 | if (program_name) |
| 2429 | { |
| 2430 | char program_init_file_name[PATH_MAX]; |
| 2431 | ::snprintf (program_init_file_name, sizeof(program_init_file_name), "%s-%s", init_file_path, program_name); |
| 2432 | init_file.SetFile (program_init_file_name, true); |
| 2433 | if (!init_file.Exists()) |
| 2434 | init_file.Clear(); |
| 2435 | } |
| 2436 | } |
| 2437 | |
| 2438 | if (!init_file && !m_skip_lldbinit_files) |
| 2439 | init_file.SetFile (init_file_path, true); |
| 2440 | } |
| 2441 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2442 | // If the file exists, tell HandleCommand to 'source' it; this will do the actual broadcasting |
| 2443 | // of the commands back to any appropriate listener (see CommandObjectSource::Execute for more details). |
| 2444 | |
| 2445 | if (init_file.Exists()) |
| 2446 | { |
Jim Ingham | e16c50a | 2011-02-18 00:54:25 +0000 | [diff] [blame] | 2447 | ExecutionContext *exe_ctx = NULL; // We don't have any context yet. |
| 2448 | bool stop_on_continue = true; |
| 2449 | bool stop_on_error = false; |
| 2450 | bool echo_commands = false; |
| 2451 | bool print_results = false; |
| 2452 | |
Enrico Granata | 5f5ab60 | 2012-05-31 01:09:06 +0000 | [diff] [blame] | 2453 | 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] | 2454 | } |
| 2455 | else |
| 2456 | { |
| 2457 | // nothing to be done if the file doesn't exist |
| 2458 | result.SetStatus(eReturnStatusSuccessFinishNoResult); |
| 2459 | } |
| 2460 | } |
| 2461 | |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 2462 | PlatformSP |
| 2463 | CommandInterpreter::GetPlatform (bool prefer_target_platform) |
| 2464 | { |
| 2465 | PlatformSP platform_sp; |
Greg Clayton | c14ee32 | 2011-09-22 04:58:26 +0000 | [diff] [blame] | 2466 | if (prefer_target_platform) |
| 2467 | { |
Greg Clayton | 4e0fe8a | 2012-07-12 20:32:19 +0000 | [diff] [blame] | 2468 | ExecutionContext exe_ctx(GetExecutionContext()); |
| 2469 | Target *target = exe_ctx.GetTargetPtr(); |
Greg Clayton | c14ee32 | 2011-09-22 04:58:26 +0000 | [diff] [blame] | 2470 | if (target) |
| 2471 | platform_sp = target->GetPlatform(); |
| 2472 | } |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 2473 | |
| 2474 | if (!platform_sp) |
| 2475 | platform_sp = m_debugger.GetPlatformList().GetSelectedPlatform(); |
| 2476 | return platform_sp; |
| 2477 | } |
| 2478 | |
Jim Ingham | e16c50a | 2011-02-18 00:54:25 +0000 | [diff] [blame] | 2479 | void |
Jim Ingham | bad87fe | 2011-03-11 01:51:49 +0000 | [diff] [blame] | 2480 | CommandInterpreter::HandleCommands (const StringList &commands, |
Jim Ingham | e16c50a | 2011-02-18 00:54:25 +0000 | [diff] [blame] | 2481 | ExecutionContext *override_context, |
| 2482 | bool stop_on_continue, |
| 2483 | bool stop_on_error, |
| 2484 | bool echo_commands, |
| 2485 | bool print_results, |
Enrico Granata | 5f5ab60 | 2012-05-31 01:09:06 +0000 | [diff] [blame] | 2486 | LazyBool add_to_history, |
Jim Ingham | e16c50a | 2011-02-18 00:54:25 +0000 | [diff] [blame] | 2487 | CommandReturnObject &result) |
| 2488 | { |
| 2489 | size_t num_lines = commands.GetSize(); |
Jim Ingham | e16c50a | 2011-02-18 00:54:25 +0000 | [diff] [blame] | 2490 | |
| 2491 | // If we are going to continue past a "continue" then we need to run the commands synchronously. |
| 2492 | // Make sure you reset this value anywhere you return from the function. |
| 2493 | |
| 2494 | bool old_async_execution = m_debugger.GetAsyncExecution(); |
| 2495 | |
| 2496 | // If we've been given an execution context, set it at the start, but don't keep resetting it or we will |
| 2497 | // cause series of commands that change the context, then do an operation that relies on that context to fail. |
| 2498 | |
| 2499 | if (override_context != NULL) |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 2500 | UpdateExecutionContext (override_context); |
Jim Ingham | e16c50a | 2011-02-18 00:54:25 +0000 | [diff] [blame] | 2501 | |
| 2502 | if (!stop_on_continue) |
| 2503 | { |
| 2504 | m_debugger.SetAsyncExecution (false); |
| 2505 | } |
| 2506 | |
| 2507 | for (int idx = 0; idx < num_lines; idx++) |
| 2508 | { |
| 2509 | const char *cmd = commands.GetStringAtIndex(idx); |
| 2510 | if (cmd[0] == '\0') |
| 2511 | continue; |
| 2512 | |
Jim Ingham | e16c50a | 2011-02-18 00:54:25 +0000 | [diff] [blame] | 2513 | if (echo_commands) |
| 2514 | { |
| 2515 | result.AppendMessageWithFormat ("%s %s\n", |
| 2516 | GetPrompt(), |
| 2517 | cmd); |
| 2518 | } |
| 2519 | |
Greg Clayton | 9d0402b | 2011-02-20 02:15:07 +0000 | [diff] [blame] | 2520 | CommandReturnObject tmp_result; |
Johnny Chen | 80fdd7c | 2011-10-05 00:42:59 +0000 | [diff] [blame] | 2521 | // If override_context is not NULL, pass no_context_switching = true for |
| 2522 | // HandleCommand() since we updated our context already. |
Jim Ingham | 076b7fc | 2013-05-02 23:15:37 +0000 | [diff] [blame] | 2523 | |
| 2524 | // We might call into a regex or alias command, in which case the add_to_history will get lost. This |
| 2525 | // m_command_source_depth dingus is the way we turn off adding to the history in that case, so set it up here. |
| 2526 | if (!add_to_history) |
| 2527 | m_command_source_depth++; |
Enrico Granata | 5f5ab60 | 2012-05-31 01:09:06 +0000 | [diff] [blame] | 2528 | bool success = HandleCommand(cmd, add_to_history, tmp_result, |
Johnny Chen | 80fdd7c | 2011-10-05 00:42:59 +0000 | [diff] [blame] | 2529 | NULL, /* override_context */ |
| 2530 | true, /* repeat_on_empty_command */ |
| 2531 | override_context != NULL /* no_context_switching */); |
Jim Ingham | 076b7fc | 2013-05-02 23:15:37 +0000 | [diff] [blame] | 2532 | if (!add_to_history) |
| 2533 | m_command_source_depth--; |
Jim Ingham | e16c50a | 2011-02-18 00:54:25 +0000 | [diff] [blame] | 2534 | |
| 2535 | if (print_results) |
| 2536 | { |
| 2537 | if (tmp_result.Succeeded()) |
Jim Ingham | 85e8b81 | 2011-02-19 02:53:09 +0000 | [diff] [blame] | 2538 | result.AppendMessageWithFormat("%s", tmp_result.GetOutputData()); |
Jim Ingham | e16c50a | 2011-02-18 00:54:25 +0000 | [diff] [blame] | 2539 | } |
| 2540 | |
| 2541 | if (!success || !tmp_result.Succeeded()) |
| 2542 | { |
Jim Ingham | a503881 | 2012-04-24 02:25:07 +0000 | [diff] [blame] | 2543 | const char *error_msg = tmp_result.GetErrorData(); |
| 2544 | if (error_msg == NULL || error_msg[0] == '\0') |
| 2545 | error_msg = "<unknown error>.\n"; |
Jim Ingham | e16c50a | 2011-02-18 00:54:25 +0000 | [diff] [blame] | 2546 | if (stop_on_error) |
| 2547 | { |
Jim Ingham | a503881 | 2012-04-24 02:25:07 +0000 | [diff] [blame] | 2548 | result.AppendErrorWithFormat("Aborting reading of commands after command #%d: '%s' failed with %s", |
| 2549 | idx, cmd, error_msg); |
Jim Ingham | e16c50a | 2011-02-18 00:54:25 +0000 | [diff] [blame] | 2550 | result.SetStatus (eReturnStatusFailed); |
| 2551 | m_debugger.SetAsyncExecution (old_async_execution); |
| 2552 | return; |
| 2553 | } |
| 2554 | else if (print_results) |
| 2555 | { |
Jim Ingham | a503881 | 2012-04-24 02:25:07 +0000 | [diff] [blame] | 2556 | result.AppendMessageWithFormat ("Command #%d '%s' failed with %s", |
Jim Ingham | e16c50a | 2011-02-18 00:54:25 +0000 | [diff] [blame] | 2557 | idx + 1, |
| 2558 | cmd, |
Jim Ingham | a503881 | 2012-04-24 02:25:07 +0000 | [diff] [blame] | 2559 | error_msg); |
Jim Ingham | e16c50a | 2011-02-18 00:54:25 +0000 | [diff] [blame] | 2560 | } |
| 2561 | } |
| 2562 | |
Caroline Tice | 969ed3d | 2011-05-02 20:41:46 +0000 | [diff] [blame] | 2563 | if (result.GetImmediateOutputStream()) |
| 2564 | result.GetImmediateOutputStream()->Flush(); |
| 2565 | |
| 2566 | if (result.GetImmediateErrorStream()) |
| 2567 | result.GetImmediateErrorStream()->Flush(); |
| 2568 | |
Jim Ingham | e16c50a | 2011-02-18 00:54:25 +0000 | [diff] [blame] | 2569 | // N.B. Can't depend on DidChangeProcessState, because the state coming into the command execution |
| 2570 | // could be running (for instance in Breakpoint Commands. |
| 2571 | // So we check the return value to see if it is has running in it. |
| 2572 | if ((tmp_result.GetStatus() == eReturnStatusSuccessContinuingNoResult) |
| 2573 | || (tmp_result.GetStatus() == eReturnStatusSuccessContinuingResult)) |
| 2574 | { |
| 2575 | if (stop_on_continue) |
| 2576 | { |
| 2577 | // If we caused the target to proceed, and we're going to stop in that case, set the |
| 2578 | // status in our real result before returning. This is an error if the continue was not the |
| 2579 | // last command in the set of commands to be run. |
| 2580 | if (idx != num_lines - 1) |
| 2581 | result.AppendErrorWithFormat("Aborting reading of commands after command #%d: '%s' continued the target.\n", |
| 2582 | idx + 1, cmd); |
| 2583 | else |
| 2584 | result.AppendMessageWithFormat ("Command #%d '%s' continued the target.\n", idx + 1, cmd); |
| 2585 | |
| 2586 | result.SetStatus(tmp_result.GetStatus()); |
| 2587 | m_debugger.SetAsyncExecution (old_async_execution); |
| 2588 | |
| 2589 | return; |
| 2590 | } |
| 2591 | } |
| 2592 | |
| 2593 | } |
| 2594 | |
| 2595 | result.SetStatus (eReturnStatusSuccessFinishResult); |
| 2596 | m_debugger.SetAsyncExecution (old_async_execution); |
| 2597 | |
| 2598 | return; |
| 2599 | } |
| 2600 | |
| 2601 | void |
| 2602 | CommandInterpreter::HandleCommandsFromFile (FileSpec &cmd_file, |
| 2603 | ExecutionContext *context, |
| 2604 | bool stop_on_continue, |
| 2605 | bool stop_on_error, |
| 2606 | bool echo_command, |
| 2607 | bool print_result, |
Enrico Granata | 5f5ab60 | 2012-05-31 01:09:06 +0000 | [diff] [blame] | 2608 | LazyBool add_to_history, |
Jim Ingham | e16c50a | 2011-02-18 00:54:25 +0000 | [diff] [blame] | 2609 | CommandReturnObject &result) |
| 2610 | { |
| 2611 | if (cmd_file.Exists()) |
| 2612 | { |
| 2613 | bool success; |
| 2614 | StringList commands; |
| 2615 | success = commands.ReadFileLines(cmd_file); |
| 2616 | if (!success) |
| 2617 | { |
| 2618 | result.AppendErrorWithFormat ("Error reading commands from file: %s.\n", cmd_file.GetFilename().AsCString()); |
| 2619 | result.SetStatus (eReturnStatusFailed); |
| 2620 | return; |
| 2621 | } |
Enrico Granata | 5f5ab60 | 2012-05-31 01:09:06 +0000 | [diff] [blame] | 2622 | m_command_source_depth++; |
| 2623 | HandleCommands (commands, context, stop_on_continue, stop_on_error, echo_command, print_result, add_to_history, result); |
| 2624 | m_command_source_depth--; |
Jim Ingham | e16c50a | 2011-02-18 00:54:25 +0000 | [diff] [blame] | 2625 | } |
| 2626 | else |
| 2627 | { |
| 2628 | result.AppendErrorWithFormat ("Error reading commands from file %s - file not found.\n", |
| 2629 | cmd_file.GetFilename().AsCString()); |
| 2630 | result.SetStatus (eReturnStatusFailed); |
| 2631 | return; |
| 2632 | } |
| 2633 | } |
| 2634 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2635 | ScriptInterpreter * |
Enrico Granata | b588726 | 2012-10-29 21:18:03 +0000 | [diff] [blame] | 2636 | CommandInterpreter::GetScriptInterpreter (bool can_create) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2637 | { |
Enrico Granata | b588726 | 2012-10-29 21:18:03 +0000 | [diff] [blame] | 2638 | if (m_script_interpreter_ap.get() != NULL) |
| 2639 | return m_script_interpreter_ap.get(); |
| 2640 | |
| 2641 | if (!can_create) |
| 2642 | return NULL; |
| 2643 | |
Enrico Granata | a29bdad | 2012-07-10 18:23:48 +0000 | [diff] [blame] | 2644 | // <rdar://problem/11751427> |
| 2645 | // we need to protect the initialization of the script interpreter |
| 2646 | // otherwise we could end up with two threads both trying to create |
| 2647 | // their instance of it, and for some languages (e.g. Python) |
| 2648 | // this is a bulletproof recipe for disaster! |
| 2649 | // this needs to be a function-level static because multiple Debugger instances living in the same process |
| 2650 | // still need to be isolated and not try to initialize Python concurrently |
Enrico Granata | 8b95df2 | 2012-07-10 19:04:14 +0000 | [diff] [blame] | 2651 | static Mutex g_interpreter_mutex(Mutex::eMutexTypeRecursive); |
| 2652 | Mutex::Locker interpreter_lock(g_interpreter_mutex); |
Enrico Granata | a29bdad | 2012-07-10 18:23:48 +0000 | [diff] [blame] | 2653 | |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 2654 | Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT)); |
Enrico Granata | b588726 | 2012-10-29 21:18:03 +0000 | [diff] [blame] | 2655 | if (log) |
| 2656 | log->Printf("Initializing the ScriptInterpreter now\n"); |
Greg Clayton | 6611103 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 2657 | |
Caroline Tice | 2f88aad | 2011-01-14 00:29:16 +0000 | [diff] [blame] | 2658 | lldb::ScriptLanguage script_lang = GetDebugger().GetScriptLanguage(); |
| 2659 | switch (script_lang) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2660 | { |
Greg Clayton | dce502e | 2011-11-04 03:34:56 +0000 | [diff] [blame] | 2661 | case eScriptLanguagePython: |
| 2662 | #ifndef LLDB_DISABLE_PYTHON |
| 2663 | m_script_interpreter_ap.reset (new ScriptInterpreterPython (*this)); |
| 2664 | break; |
| 2665 | #else |
| 2666 | // Fall through to the None case when python is disabled |
| 2667 | #endif |
Caroline Tice | 2f88aad | 2011-01-14 00:29:16 +0000 | [diff] [blame] | 2668 | case eScriptLanguageNone: |
| 2669 | m_script_interpreter_ap.reset (new ScriptInterpreterNone (*this)); |
| 2670 | break; |
Caroline Tice | 2f88aad | 2011-01-14 00:29:16 +0000 | [diff] [blame] | 2671 | }; |
| 2672 | |
| 2673 | return m_script_interpreter_ap.get(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2674 | } |
| 2675 | |
| 2676 | |
| 2677 | |
| 2678 | bool |
| 2679 | CommandInterpreter::GetSynchronous () |
| 2680 | { |
| 2681 | return m_synchronous_execution; |
| 2682 | } |
| 2683 | |
| 2684 | void |
| 2685 | CommandInterpreter::SetSynchronous (bool value) |
| 2686 | { |
Johnny Chen | c066ab4 | 2010-10-14 01:22:03 +0000 | [diff] [blame] | 2687 | m_synchronous_execution = value; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2688 | } |
| 2689 | |
| 2690 | void |
| 2691 | CommandInterpreter::OutputFormattedHelpText (Stream &strm, |
| 2692 | const char *word_text, |
| 2693 | const char *separator, |
| 2694 | const char *help_text, |
Greg Clayton | c7bece56 | 2013-01-25 18:06:21 +0000 | [diff] [blame] | 2695 | size_t max_word_len) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2696 | { |
Greg Clayton | a701509 | 2010-09-18 01:14:36 +0000 | [diff] [blame] | 2697 | const uint32_t max_columns = m_debugger.GetTerminalWidth(); |
| 2698 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2699 | int indent_size = max_word_len + strlen (separator) + 2; |
| 2700 | |
| 2701 | strm.IndentMore (indent_size); |
Greg Clayton | bfe5f3b | 2011-02-18 01:44:25 +0000 | [diff] [blame] | 2702 | |
| 2703 | StreamString text_strm; |
Greg Clayton | c7bece56 | 2013-01-25 18:06:21 +0000 | [diff] [blame] | 2704 | 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] | 2705 | |
| 2706 | size_t len = text_strm.GetSize(); |
| 2707 | const char *text = text_strm.GetData(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2708 | if (text[len - 1] == '\n') |
Greg Clayton | bfe5f3b | 2011-02-18 01:44:25 +0000 | [diff] [blame] | 2709 | { |
| 2710 | text_strm.EOL(); |
| 2711 | len = text_strm.GetSize(); |
| 2712 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2713 | |
| 2714 | if (len < max_columns) |
| 2715 | { |
| 2716 | // Output it as a single line. |
| 2717 | strm.Printf ("%s", text); |
| 2718 | } |
| 2719 | else |
| 2720 | { |
| 2721 | // We need to break it up into multiple lines. |
| 2722 | bool first_line = true; |
| 2723 | int text_width; |
Greg Clayton | c7bece56 | 2013-01-25 18:06:21 +0000 | [diff] [blame] | 2724 | size_t start = 0; |
| 2725 | size_t end = start; |
| 2726 | const size_t final_end = strlen (text); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2727 | |
| 2728 | while (end < final_end) |
| 2729 | { |
| 2730 | if (first_line) |
| 2731 | text_width = max_columns - 1; |
| 2732 | else |
| 2733 | text_width = max_columns - indent_size - 1; |
| 2734 | |
| 2735 | // Don't start the 'text' on a space, since we're already outputting the indentation. |
| 2736 | if (!first_line) |
| 2737 | { |
| 2738 | while ((start < final_end) && (text[start] == ' ')) |
| 2739 | start++; |
| 2740 | } |
| 2741 | |
| 2742 | end = start + text_width; |
| 2743 | if (end > final_end) |
| 2744 | end = final_end; |
| 2745 | else |
| 2746 | { |
| 2747 | // If we're not at the end of the text, make sure we break the line on white space. |
| 2748 | while (end > start |
| 2749 | && text[end] != ' ' && text[end] != '\t' && text[end] != '\n') |
| 2750 | end--; |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 2751 | assert (end > 0); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2752 | } |
| 2753 | |
Greg Clayton | c7bece56 | 2013-01-25 18:06:21 +0000 | [diff] [blame] | 2754 | const size_t sub_len = end - start; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2755 | if (start != 0) |
| 2756 | strm.EOL(); |
| 2757 | if (!first_line) |
| 2758 | strm.Indent(); |
| 2759 | else |
| 2760 | first_line = false; |
| 2761 | assert (start <= final_end); |
| 2762 | assert (start + sub_len <= final_end); |
| 2763 | if (sub_len > 0) |
| 2764 | strm.Write (text + start, sub_len); |
| 2765 | start = end + 1; |
| 2766 | } |
| 2767 | } |
| 2768 | strm.EOL(); |
| 2769 | strm.IndentLess(indent_size); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2770 | } |
| 2771 | |
| 2772 | void |
Enrico Granata | 82a7d98 | 2011-07-07 00:38:40 +0000 | [diff] [blame] | 2773 | CommandInterpreter::OutputHelpText (Stream &strm, |
| 2774 | const char *word_text, |
| 2775 | const char *separator, |
| 2776 | const char *help_text, |
| 2777 | uint32_t max_word_len) |
| 2778 | { |
| 2779 | int indent_size = max_word_len + strlen (separator) + 2; |
| 2780 | |
| 2781 | strm.IndentMore (indent_size); |
| 2782 | |
| 2783 | StreamString text_strm; |
| 2784 | text_strm.Printf ("%-*s %s %s", max_word_len, word_text, separator, help_text); |
| 2785 | |
| 2786 | const uint32_t max_columns = m_debugger.GetTerminalWidth(); |
Enrico Granata | 82a7d98 | 2011-07-07 00:38:40 +0000 | [diff] [blame] | 2787 | |
| 2788 | size_t len = text_strm.GetSize(); |
| 2789 | const char *text = text_strm.GetData(); |
| 2790 | |
| 2791 | uint32_t chars_left = max_columns; |
| 2792 | |
| 2793 | for (uint32_t i = 0; i < len; i++) |
| 2794 | { |
| 2795 | if ((text[i] == ' ' && ::strchr((text+i+1), ' ') && chars_left < ::strchr((text+i+1), ' ')-(text+i)) || text[i] == '\n') |
| 2796 | { |
Enrico Granata | 82a7d98 | 2011-07-07 00:38:40 +0000 | [diff] [blame] | 2797 | chars_left = max_columns - indent_size; |
| 2798 | strm.EOL(); |
| 2799 | strm.Indent(); |
| 2800 | } |
| 2801 | else |
| 2802 | { |
| 2803 | strm.PutChar(text[i]); |
| 2804 | chars_left--; |
| 2805 | } |
| 2806 | |
| 2807 | } |
| 2808 | |
| 2809 | strm.EOL(); |
| 2810 | strm.IndentLess(indent_size); |
| 2811 | } |
| 2812 | |
| 2813 | void |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2814 | CommandInterpreter::FindCommandsForApropos (const char *search_word, StringList &commands_found, |
Jim Ingham | af3753e | 2013-05-17 01:30:37 +0000 | [diff] [blame] | 2815 | StringList &commands_help, bool search_builtin_commands, bool search_user_commands) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2816 | { |
| 2817 | CommandObject::CommandMap::const_iterator pos; |
| 2818 | |
Jim Ingham | af3753e | 2013-05-17 01:30:37 +0000 | [diff] [blame] | 2819 | if (search_builtin_commands) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2820 | { |
Jim Ingham | af3753e | 2013-05-17 01:30:37 +0000 | [diff] [blame] | 2821 | for (pos = m_command_dict.begin(); pos != m_command_dict.end(); ++pos) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2822 | { |
Jim Ingham | af3753e | 2013-05-17 01:30:37 +0000 | [diff] [blame] | 2823 | const char *command_name = pos->first.c_str(); |
| 2824 | CommandObject *cmd_obj = pos->second.get(); |
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 | if (cmd_obj->HelpTextContainsWord (search_word)) |
| 2827 | { |
| 2828 | commands_found.AppendString (command_name); |
| 2829 | commands_help.AppendString (cmd_obj->GetHelp()); |
| 2830 | } |
| 2831 | |
| 2832 | if (cmd_obj->IsMultiwordObject()) |
| 2833 | cmd_obj->AproposAllSubCommands (command_name, |
| 2834 | search_word, |
| 2835 | commands_found, |
| 2836 | commands_help); |
| 2837 | |
| 2838 | } |
| 2839 | } |
| 2840 | |
| 2841 | if (search_user_commands) |
| 2842 | { |
| 2843 | for (pos = m_user_dict.begin(); pos != m_user_dict.end(); ++pos) |
| 2844 | { |
| 2845 | const char *command_name = pos->first.c_str(); |
| 2846 | CommandObject *cmd_obj = pos->second.get(); |
| 2847 | |
| 2848 | if (cmd_obj->HelpTextContainsWord (search_word)) |
| 2849 | { |
| 2850 | commands_found.AppendString (command_name); |
| 2851 | commands_help.AppendString (cmd_obj->GetHelp()); |
| 2852 | } |
| 2853 | |
| 2854 | if (cmd_obj->IsMultiwordObject()) |
| 2855 | cmd_obj->AproposAllSubCommands (command_name, |
| 2856 | search_word, |
| 2857 | commands_found, |
| 2858 | commands_help); |
| 2859 | |
| 2860 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2861 | } |
| 2862 | } |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 2863 | |
| 2864 | |
| 2865 | void |
| 2866 | CommandInterpreter::UpdateExecutionContext (ExecutionContext *override_context) |
| 2867 | { |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 2868 | if (override_context != NULL) |
| 2869 | { |
Greg Clayton | 4e0fe8a | 2012-07-12 20:32:19 +0000 | [diff] [blame] | 2870 | m_exe_ctx_ref = *override_context; |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 2871 | } |
| 2872 | else |
| 2873 | { |
Greg Clayton | 4e0fe8a | 2012-07-12 20:32:19 +0000 | [diff] [blame] | 2874 | const bool adopt_selected = true; |
| 2875 | m_exe_ctx_ref.SetTargetPtr (m_debugger.GetSelectedTarget().get(), adopt_selected); |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 2876 | } |
| 2877 | } |