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