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