blob: 965ba5b7dc146184d8e6c3f7f1cf3db898da3686 [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- CommandInterpreter.cpp ----------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include <string>
Caroline Tice4ab31c92010-10-12 21:57:09 +000011#include <vector>
Chris Lattner30fdc8d2010-06-08 16:52:24 +000012#include <stdlib.h>
13
Greg Clayton4a33d312011-06-23 17:59:56 +000014#include "CommandObjectScript.h"
Peter Collingbourne08405b62011-06-23 20:37:26 +000015#include "lldb/Interpreter/CommandObjectRegexCommand.h"
Greg Clayton4a33d312011-06-23 17:59:56 +000016
Eli Friedman3afb70c2010-06-13 02:17:17 +000017#include "../Commands/CommandObjectApropos.h"
18#include "../Commands/CommandObjectArgs.h"
19#include "../Commands/CommandObjectBreakpoint.h"
Tamas Berghammer3937bc62015-07-02 10:03:37 +000020#include "../Commands/CommandObjectBugreport.h"
Eli Friedman3afb70c2010-06-13 02:17:17 +000021#include "../Commands/CommandObjectDisassemble.h"
22#include "../Commands/CommandObjectExpression.h"
Eli Friedman3afb70c2010-06-13 02:17:17 +000023#include "../Commands/CommandObjectFrame.h"
Greg Clayton44d93782014-01-27 23:43:24 +000024#include "../Commands/CommandObjectGUI.h"
Eli Friedman3afb70c2010-06-13 02:17:17 +000025#include "../Commands/CommandObjectHelp.h"
Eli Friedman3afb70c2010-06-13 02:17:17 +000026#include "../Commands/CommandObjectLog.h"
27#include "../Commands/CommandObjectMemory.h"
Greg Claytonded470d2011-03-19 01:12:21 +000028#include "../Commands/CommandObjectPlatform.h"
Enrico Granata21dfcd92012-09-28 23:57:51 +000029#include "../Commands/CommandObjectPlugin.h"
Eli Friedman3afb70c2010-06-13 02:17:17 +000030#include "../Commands/CommandObjectProcess.h"
31#include "../Commands/CommandObjectQuit.h"
Eli Friedman3afb70c2010-06-13 02:17:17 +000032#include "../Commands/CommandObjectRegister.h"
Eli Friedman3afb70c2010-06-13 02:17:17 +000033#include "../Commands/CommandObjectSettings.h"
Eli Friedman3afb70c2010-06-13 02:17:17 +000034#include "../Commands/CommandObjectSource.h"
Jim Inghamebc09c32010-07-07 03:36:20 +000035#include "../Commands/CommandObjectCommands.h"
Eli Friedman3afb70c2010-06-13 02:17:17 +000036#include "../Commands/CommandObjectSyntax.h"
37#include "../Commands/CommandObjectTarget.h"
38#include "../Commands/CommandObjectThread.h"
Greg Clayton4a33d312011-06-23 17:59:56 +000039#include "../Commands/CommandObjectType.h"
Johnny Chen31c39da2010-12-23 20:21:44 +000040#include "../Commands/CommandObjectVersion.h"
Johnny Chenf04ee932011-09-22 18:04:58 +000041#include "../Commands/CommandObjectWatchpoint.h"
Colin Rileyc9c55a22015-05-04 18:39:38 +000042#include "../Commands/CommandObjectLanguage.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000043
Greg Clayton7d2ef162013-03-29 17:03:23 +000044
Chris Lattner30fdc8d2010-06-08 16:52:24 +000045#include "lldb/Core/Debugger.h"
Enrico Granatab5887262012-10-29 21:18:03 +000046#include "lldb/Core/Log.h"
Greg Claytonf0066ad2014-05-02 00:45:31 +000047#include "lldb/Core/State.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000048#include "lldb/Core/Stream.h"
Greg Clayton44d93782014-01-27 23:43:24 +000049#include "lldb/Core/StreamFile.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000050#include "lldb/Core/Timer.h"
Enrico Granatab5887262012-10-29 21:18:03 +000051
Todd Fialacacde7d2014-09-27 16:54:22 +000052#ifndef LLDB_DISABLE_LIBEDIT
Greg Clayton44d93782014-01-27 23:43:24 +000053#include "lldb/Host/Editline.h"
Todd Fialacacde7d2014-09-27 16:54:22 +000054#endif
Greg Clayton7fb56d02011-02-01 01:31:41 +000055#include "lldb/Host/Host.h"
Zachary Turnera21fee02014-08-21 21:49:24 +000056#include "lldb/Host/HostInfo.h"
Enrico Granatab5887262012-10-29 21:18:03 +000057
58#include "lldb/Interpreter/Args.h"
Greg Clayton7d2ef162013-03-29 17:03:23 +000059#include "lldb/Interpreter/CommandCompletions.h"
Enrico Granatab5887262012-10-29 21:18:03 +000060#include "lldb/Interpreter/CommandInterpreter.h"
Greg Clayton7d2ef162013-03-29 17:03:23 +000061#include "lldb/Interpreter/CommandReturnObject.h"
Enrico Granatab5887262012-10-29 21:18:03 +000062#include "lldb/Interpreter/Options.h"
Zachary Turner633a29c2015-03-04 01:58:01 +000063#include "lldb/Interpreter/OptionValueProperties.h"
64#include "lldb/Interpreter/Property.h"
Enrico Granatab5887262012-10-29 21:18:03 +000065#include "lldb/Interpreter/ScriptInterpreterNone.h"
66#include "lldb/Interpreter/ScriptInterpreterPython.h"
67
68
Chris Lattner30fdc8d2010-06-08 16:52:24 +000069#include "lldb/Target/Process.h"
70#include "lldb/Target/Thread.h"
71#include "lldb/Target/TargetList.h"
72
Enrico Granatab5887262012-10-29 21:18:03 +000073#include "lldb/Utility/CleanUp.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000074
Zachary Turnera21fee02014-08-21 21:49:24 +000075#include "llvm/ADT/SmallString.h"
Saleem Abdulrasool28606952014-06-27 05:17:41 +000076#include "llvm/ADT/STLExtras.h"
Zachary Turnera21fee02014-08-21 21:49:24 +000077#include "llvm/Support/Path.h"
Saleem Abdulrasool28606952014-06-27 05:17:41 +000078
Chris Lattner30fdc8d2010-06-08 16:52:24 +000079using namespace lldb;
80using namespace lldb_private;
81
Adrian McCarthy2304b6f2015-04-23 20:00:25 +000082static const char *k_white_space = " \t\v";
Greg Clayton754a9362012-08-23 00:22:02 +000083
84static PropertyDefinition
85g_properties[] =
86{
Ed Masted78c9572014-04-20 00:31:37 +000087 { "expand-regex-aliases", OptionValue::eTypeBoolean, true, false, nullptr, nullptr, "If true, regular expression alias commands will show the expanded command that will be executed. This can be used to debug new regular expression alias commands." },
88 { "prompt-on-quit", OptionValue::eTypeBoolean, true, true, nullptr, nullptr, "If true, LLDB will prompt you before quitting if there are any live processes being debugged. If false, LLDB will quit without asking in any case." },
89 { "stop-command-source-on-error", OptionValue::eTypeBoolean, true, true, nullptr, nullptr, "If true, LLDB will stop running a 'command source' script upon encountering an error." },
90 { nullptr , OptionValue::eTypeInvalid, true, 0 , nullptr, nullptr, nullptr }
Greg Clayton754a9362012-08-23 00:22:02 +000091};
92
93enum
94{
Enrico Granatabcba2b22013-01-17 21:36:19 +000095 ePropertyExpandRegexAliases = 0,
Enrico Granata012d4fc2013-06-11 01:26:35 +000096 ePropertyPromptOnQuit = 1,
97 ePropertyStopCmdSourceOnError = 2
Greg Clayton754a9362012-08-23 00:22:02 +000098};
99
Jim Ingham4bddaeb2012-02-16 06:50:00 +0000100ConstString &
101CommandInterpreter::GetStaticBroadcasterClass ()
102{
103 static ConstString class_name ("lldb.commandInterpreter");
104 return class_name;
105}
106
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000107CommandInterpreter::CommandInterpreter
108(
Greg Clayton66111032010-06-23 01:19:29 +0000109 Debugger &debugger,
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000110 ScriptLanguage script_language,
Greg Clayton66111032010-06-23 01:19:29 +0000111 bool synchronous_execution
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000112) :
Ilia K8a00a562015-03-17 16:54:52 +0000113 Broadcaster (&debugger, CommandInterpreter::GetStaticBroadcasterClass().AsCString()),
Greg Clayton754a9362012-08-23 00:22:02 +0000114 Properties(OptionValuePropertiesSP(new OptionValueProperties(ConstString("interpreter")))),
Greg Clayton44d93782014-01-27 23:43:24 +0000115 IOHandlerDelegate (IOHandlerDelegate::Completion::LLDBCommand),
Greg Clayton66111032010-06-23 01:19:29 +0000116 m_debugger (debugger),
Greg Clayton6eee5aa2010-10-11 01:05:37 +0000117 m_synchronous_execution (synchronous_execution),
Caroline Tice2f88aad2011-01-14 00:29:16 +0000118 m_skip_lldbinit_files (false),
Jim Ingham16e0c682011-08-12 23:34:31 +0000119 m_skip_app_init_files (false),
Jim Inghame16c50a2011-02-18 00:54:25 +0000120 m_script_interpreter_ap (),
Greg Clayton44d93782014-01-27 23:43:24 +0000121 m_command_io_handler_sp (),
Caroline Ticed61c10b2011-06-16 16:27:19 +0000122 m_comment_char ('#'),
Johnny Chen4ac1d9e2012-08-09 22:06:10 +0000123 m_batch_command_mode (false),
Enrico Granata5f5ab602012-05-31 01:09:06 +0000124 m_truncation_warning(eNoTruncation),
Jim Ingham26c7bf92014-10-11 00:38:27 +0000125 m_command_source_depth (0),
126 m_num_errors(0),
Jim Inghamffc9f1d2014-10-14 01:20:07 +0000127 m_quit_requested(false),
128 m_stopped_for_crash(false)
Jim Ingham26c7bf92014-10-11 00:38:27 +0000129
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000130{
Greg Clayton67cc0632012-08-22 17:17:09 +0000131 debugger.SetScriptLanguage (script_language);
Greg Claytoncfd1ace2010-10-31 03:01:06 +0000132 SetEventName (eBroadcastBitThreadShouldExit, "thread-should-exit");
133 SetEventName (eBroadcastBitResetPrompt, "reset-prompt");
Greg Clayton67cc0632012-08-22 17:17:09 +0000134 SetEventName (eBroadcastBitQuitCommandReceived, "quit");
Jim Ingham4bddaeb2012-02-16 06:50:00 +0000135 CheckInWithManager ();
Greg Clayton754a9362012-08-23 00:22:02 +0000136 m_collection_sp->Initialize (g_properties);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000137}
138
Greg Clayton754a9362012-08-23 00:22:02 +0000139bool
140CommandInterpreter::GetExpandRegexAliases () const
141{
142 const uint32_t idx = ePropertyExpandRegexAliases;
Ed Masted78c9572014-04-20 00:31:37 +0000143 return m_collection_sp->GetPropertyAtIndexAsBoolean (nullptr, idx, g_properties[idx].default_uint_value != 0);
Greg Clayton754a9362012-08-23 00:22:02 +0000144}
145
Enrico Granatabcba2b22013-01-17 21:36:19 +0000146bool
147CommandInterpreter::GetPromptOnQuit () const
148{
149 const uint32_t idx = ePropertyPromptOnQuit;
Ed Masted78c9572014-04-20 00:31:37 +0000150 return m_collection_sp->GetPropertyAtIndexAsBoolean (nullptr, idx, g_properties[idx].default_uint_value != 0);
Enrico Granatabcba2b22013-01-17 21:36:19 +0000151}
Greg Clayton754a9362012-08-23 00:22:02 +0000152
Ilia Kacf28be2015-03-23 22:45:13 +0000153void
154CommandInterpreter::SetPromptOnQuit (bool b)
155{
156 const uint32_t idx = ePropertyPromptOnQuit;
157 m_collection_sp->SetPropertyAtIndexAsBoolean (nullptr, idx, b);
158}
159
Adrian McCarthy2304b6f2015-04-23 20:00:25 +0000160void
161CommandInterpreter::ResolveCommand(const char *command_line, CommandReturnObject &result)
162{
163 std::string command = command_line;
164 if (ResolveCommandImpl(command, result) != nullptr) {
165 result.AppendMessageWithFormat("%s", command.c_str());
166 result.SetStatus(eReturnStatusSuccessFinishResult);
167 }
168}
169
170
Enrico Granata012d4fc2013-06-11 01:26:35 +0000171bool
172CommandInterpreter::GetStopCmdSourceOnError () const
173{
174 const uint32_t idx = ePropertyStopCmdSourceOnError;
Ed Masted78c9572014-04-20 00:31:37 +0000175 return m_collection_sp->GetPropertyAtIndexAsBoolean (nullptr, idx, g_properties[idx].default_uint_value != 0);
Enrico Granata012d4fc2013-06-11 01:26:35 +0000176}
177
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000178void
179CommandInterpreter::Initialize ()
180{
181 Timer scoped_timer (__PRETTY_FUNCTION__, __PRETTY_FUNCTION__);
182
183 CommandReturnObject result;
184
185 LoadCommandDictionary ();
186
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000187 // Set up some initial aliases.
Caroline Ticeca90c472011-05-06 21:37:15 +0000188 CommandObjectSP cmd_obj_sp = GetCommandSPExact ("quit", false);
189 if (cmd_obj_sp)
190 {
191 AddAlias ("q", cmd_obj_sp);
192 AddAlias ("exit", cmd_obj_sp);
193 }
Sean Callanan247e62a2012-05-04 23:15:02 +0000194
Johnny Chen6d675242012-08-24 18:15:45 +0000195 cmd_obj_sp = GetCommandSPExact ("_regexp-attach",false);
Sean Callanan247e62a2012-05-04 23:15:02 +0000196 if (cmd_obj_sp)
197 {
198 AddAlias ("attach", cmd_obj_sp);
199 }
Caroline Ticeca90c472011-05-06 21:37:15 +0000200
Johnny Chen6d675242012-08-24 18:15:45 +0000201 cmd_obj_sp = GetCommandSPExact ("process detach",false);
202 if (cmd_obj_sp)
203 {
204 AddAlias ("detach", cmd_obj_sp);
205 }
206
Caroline Ticeca90c472011-05-06 21:37:15 +0000207 cmd_obj_sp = GetCommandSPExact ("process continue", false);
208 if (cmd_obj_sp)
209 {
210 AddAlias ("c", cmd_obj_sp);
211 AddAlias ("continue", cmd_obj_sp);
212 }
213
214 cmd_obj_sp = GetCommandSPExact ("_regexp-break",false);
215 if (cmd_obj_sp)
216 AddAlias ("b", cmd_obj_sp);
217
Jim Inghamca36cd12012-10-05 19:16:31 +0000218 cmd_obj_sp = GetCommandSPExact ("_regexp-tbreak",false);
219 if (cmd_obj_sp)
220 AddAlias ("tbreak", cmd_obj_sp);
221
Caroline Ticeca90c472011-05-06 21:37:15 +0000222 cmd_obj_sp = GetCommandSPExact ("thread step-inst", false);
223 if (cmd_obj_sp)
Jason Molendaf385f122011-10-22 00:47:41 +0000224 {
225 AddAlias ("stepi", cmd_obj_sp);
Caroline Ticeca90c472011-05-06 21:37:15 +0000226 AddAlias ("si", cmd_obj_sp);
Jason Molendaf385f122011-10-22 00:47:41 +0000227 }
228
229 cmd_obj_sp = GetCommandSPExact ("thread step-inst-over", false);
230 if (cmd_obj_sp)
231 {
232 AddAlias ("nexti", cmd_obj_sp);
233 AddAlias ("ni", cmd_obj_sp);
234 }
Caroline Ticeca90c472011-05-06 21:37:15 +0000235
236 cmd_obj_sp = GetCommandSPExact ("thread step-in", false);
237 if (cmd_obj_sp)
238 {
239 AddAlias ("s", cmd_obj_sp);
240 AddAlias ("step", cmd_obj_sp);
241 }
242
243 cmd_obj_sp = GetCommandSPExact ("thread step-over", false);
244 if (cmd_obj_sp)
245 {
246 AddAlias ("n", cmd_obj_sp);
247 AddAlias ("next", cmd_obj_sp);
248 }
249
250 cmd_obj_sp = GetCommandSPExact ("thread step-out", false);
251 if (cmd_obj_sp)
252 {
Caroline Ticeca90c472011-05-06 21:37:15 +0000253 AddAlias ("finish", cmd_obj_sp);
254 }
255
Jim Ingham6d6d1072011-12-02 01:12:59 +0000256 cmd_obj_sp = GetCommandSPExact ("frame select", false);
257 if (cmd_obj_sp)
258 {
259 AddAlias ("f", cmd_obj_sp);
260 }
261
Jim Inghamca36cd12012-10-05 19:16:31 +0000262 cmd_obj_sp = GetCommandSPExact ("thread select", false);
263 if (cmd_obj_sp)
264 {
265 AddAlias ("t", cmd_obj_sp);
266 }
267
Richard Mittonf86248d2013-09-12 02:20:34 +0000268 cmd_obj_sp = GetCommandSPExact ("_regexp-jump",false);
269 if (cmd_obj_sp)
270 {
271 AddAlias ("j", cmd_obj_sp);
272 AddAlias ("jump", cmd_obj_sp);
273 }
274
Greg Clayton6bade322013-02-01 23:33:03 +0000275 cmd_obj_sp = GetCommandSPExact ("_regexp-list", false);
Caroline Ticeca90c472011-05-06 21:37:15 +0000276 if (cmd_obj_sp)
277 {
278 AddAlias ("l", cmd_obj_sp);
279 AddAlias ("list", cmd_obj_sp);
280 }
281
Greg Claytonef5651d2013-02-12 18:52:24 +0000282 cmd_obj_sp = GetCommandSPExact ("_regexp-env", false);
283 if (cmd_obj_sp)
284 {
285 AddAlias ("env", cmd_obj_sp);
286 }
287
Caroline Ticeca90c472011-05-06 21:37:15 +0000288 cmd_obj_sp = GetCommandSPExact ("memory read", false);
289 if (cmd_obj_sp)
290 AddAlias ("x", cmd_obj_sp);
291
292 cmd_obj_sp = GetCommandSPExact ("_regexp-up", false);
293 if (cmd_obj_sp)
294 AddAlias ("up", cmd_obj_sp);
295
296 cmd_obj_sp = GetCommandSPExact ("_regexp-down", false);
297 if (cmd_obj_sp)
298 AddAlias ("down", cmd_obj_sp);
299
Jason Molenda0c8e0062011-10-25 02:11:20 +0000300 cmd_obj_sp = GetCommandSPExact ("_regexp-display", false);
Jason Molendabc7748b2011-10-22 01:30:52 +0000301 if (cmd_obj_sp)
302 AddAlias ("display", cmd_obj_sp);
Jim Ingham7e18e422011-10-24 18:37:00 +0000303
304 cmd_obj_sp = GetCommandSPExact ("disassemble", false);
305 if (cmd_obj_sp)
306 AddAlias ("dis", cmd_obj_sp);
307
308 cmd_obj_sp = GetCommandSPExact ("disassemble", false);
309 if (cmd_obj_sp)
310 AddAlias ("di", cmd_obj_sp);
311
312
Jason Molendabc7748b2011-10-22 01:30:52 +0000313
Jason Molenda0c8e0062011-10-25 02:11:20 +0000314 cmd_obj_sp = GetCommandSPExact ("_regexp-undisplay", false);
Jason Molendabc7748b2011-10-22 01:30:52 +0000315 if (cmd_obj_sp)
316 AddAlias ("undisplay", cmd_obj_sp);
317
Jim Ingham71bf2992012-10-10 16:51:31 +0000318 cmd_obj_sp = GetCommandSPExact ("_regexp-bt", false);
319 if (cmd_obj_sp)
320 AddAlias ("bt", cmd_obj_sp);
321
Caroline Ticeca90c472011-05-06 21:37:15 +0000322 cmd_obj_sp = GetCommandSPExact ("target create", false);
323 if (cmd_obj_sp)
324 AddAlias ("file", cmd_obj_sp);
325
326 cmd_obj_sp = GetCommandSPExact ("target modules", false);
327 if (cmd_obj_sp)
328 AddAlias ("image", cmd_obj_sp);
329
330
331 OptionArgVectorSP alias_arguments_vector_sp (new OptionArgVector);
Jim Inghamffba2292011-03-22 02:29:32 +0000332
Caroline Ticeca90c472011-05-06 21:37:15 +0000333 cmd_obj_sp = GetCommandSPExact ("expression", false);
334 if (cmd_obj_sp)
Sean Callanan90e579f2013-04-17 17:23:58 +0000335 {
Caroline Ticeca90c472011-05-06 21:37:15 +0000336 ProcessAliasOptionsArgs (cmd_obj_sp, "--", alias_arguments_vector_sp);
337 AddAlias ("p", cmd_obj_sp);
338 AddAlias ("print", cmd_obj_sp);
Sean Callanan316d5e42012-08-08 01:30:34 +0000339 AddAlias ("call", cmd_obj_sp);
Caroline Ticeca90c472011-05-06 21:37:15 +0000340 AddOrReplaceAliasOptions ("p", alias_arguments_vector_sp);
341 AddOrReplaceAliasOptions ("print", alias_arguments_vector_sp);
Sean Callanan316d5e42012-08-08 01:30:34 +0000342 AddOrReplaceAliasOptions ("call", alias_arguments_vector_sp);
Caroline Ticeca90c472011-05-06 21:37:15 +0000343
344 alias_arguments_vector_sp.reset (new OptionArgVector);
Greg Clayton9e57dcd2013-05-15 01:03:08 +0000345 ProcessAliasOptionsArgs (cmd_obj_sp, "-O -- ", alias_arguments_vector_sp);
Caroline Ticeca90c472011-05-06 21:37:15 +0000346 AddAlias ("po", cmd_obj_sp);
347 AddOrReplaceAliasOptions ("po", alias_arguments_vector_sp);
348 }
349
Sean Callanan2e1d9ba2012-06-01 23:29:32 +0000350 cmd_obj_sp = GetCommandSPExact ("process kill", false);
351 if (cmd_obj_sp)
Greg Claytone86fd742012-09-27 00:02:27 +0000352 {
Sean Callanan2e1d9ba2012-06-01 23:29:32 +0000353 AddAlias ("kill", cmd_obj_sp);
Greg Claytone86fd742012-09-27 00:02:27 +0000354 }
Sean Callanan2e1d9ba2012-06-01 23:29:32 +0000355
Caroline Ticeca90c472011-05-06 21:37:15 +0000356 cmd_obj_sp = GetCommandSPExact ("process launch", false);
357 if (cmd_obj_sp)
358 {
359 alias_arguments_vector_sp.reset (new OptionArgVector);
Todd Fiala013434e2014-07-09 01:29:05 +0000360#if defined (__arm__) || defined (__arm64__) || defined (__aarch64__)
Jason Molenda85da3122012-07-06 02:46:23 +0000361 ProcessAliasOptionsArgs (cmd_obj_sp, "--", alias_arguments_vector_sp);
362#else
Zachary Turner10687b02014-10-20 17:46:43 +0000363 std::string shell_option;
364 shell_option.append("--shell=");
365 shell_option.append(HostInfo::GetDefaultShell().GetPath());
366 shell_option.append(" --");
367 ProcessAliasOptionsArgs (cmd_obj_sp, shell_option.c_str(), alias_arguments_vector_sp);
Jason Molenda85da3122012-07-06 02:46:23 +0000368#endif
Caroline Ticeca90c472011-05-06 21:37:15 +0000369 AddAlias ("r", cmd_obj_sp);
370 AddAlias ("run", cmd_obj_sp);
371 AddOrReplaceAliasOptions ("r", alias_arguments_vector_sp);
372 AddOrReplaceAliasOptions ("run", alias_arguments_vector_sp);
373 }
Greg Clayton843d62d2012-03-29 21:47:51 +0000374
375 cmd_obj_sp = GetCommandSPExact ("target symbols add", false);
376 if (cmd_obj_sp)
377 {
378 AddAlias ("add-dsym", cmd_obj_sp);
379 }
Sean Callananfc732752012-05-21 18:25:19 +0000380
381 cmd_obj_sp = GetCommandSPExact ("breakpoint set", false);
382 if (cmd_obj_sp)
383 {
384 alias_arguments_vector_sp.reset (new OptionArgVector);
385 ProcessAliasOptionsArgs (cmd_obj_sp, "--func-regex %1", alias_arguments_vector_sp);
Jim Ingham06d282d2012-10-18 23:24:12 +0000386 AddAlias ("rbreak", cmd_obj_sp);
387 AddOrReplaceAliasOptions("rbreak", alias_arguments_vector_sp);
Sean Callananfc732752012-05-21 18:25:19 +0000388 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000389}
390
Greg Clayton0c4129f2014-04-25 00:35:14 +0000391void
392CommandInterpreter::Clear()
393{
394 m_command_io_handler_sp.reset();
Greg Claytoned6499f2014-04-25 23:55:12 +0000395
396 if (m_script_interpreter_ap)
397 m_script_interpreter_ap->Clear();
Greg Clayton0c4129f2014-04-25 00:35:14 +0000398}
399
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000400const char *
401CommandInterpreter::ProcessEmbeddedScriptCommands (const char *arg)
402{
403 // This function has not yet been implemented.
404
405 // Look for any embedded script command
406 // If found,
407 // get interpreter object from the command dictionary,
408 // call execute_one_command on it,
409 // get the results as a string,
410 // substitute that string for current stuff.
411
412 return arg;
413}
414
415
416void
417CommandInterpreter::LoadCommandDictionary ()
418{
419 Timer scoped_timer (__PRETTY_FUNCTION__, __PRETTY_FUNCTION__);
420
Caroline Ticedaccaa92010-09-20 20:44:43 +0000421 lldb::ScriptLanguage script_language = m_debugger.GetScriptLanguage();
Caroline Tice3df9a8d2010-09-04 00:03:46 +0000422
Greg Claytona7015092010-09-18 01:14:36 +0000423 m_command_dict["apropos"] = CommandObjectSP (new CommandObjectApropos (*this));
Greg Clayton66111032010-06-23 01:19:29 +0000424 m_command_dict["breakpoint"]= CommandObjectSP (new CommandObjectMultiwordBreakpoint (*this));
Tamas Berghammer3937bc62015-07-02 10:03:37 +0000425 m_command_dict["bugreport"] = CommandObjectSP (new CommandObjectMultiwordBugreport (*this));
Johnny Chenb89982d2011-04-21 00:39:18 +0000426 m_command_dict["command"] = CommandObjectSP (new CommandObjectMultiwordCommands (*this));
Greg Claytona7015092010-09-18 01:14:36 +0000427 m_command_dict["disassemble"] = CommandObjectSP (new CommandObjectDisassemble (*this));
428 m_command_dict["expression"]= CommandObjectSP (new CommandObjectExpression (*this));
Greg Clayton66111032010-06-23 01:19:29 +0000429 m_command_dict["frame"] = CommandObjectSP (new CommandObjectMultiwordFrame (*this));
Greg Clayton44d93782014-01-27 23:43:24 +0000430 m_command_dict["gui"] = CommandObjectSP (new CommandObjectGUI (*this));
Greg Claytona7015092010-09-18 01:14:36 +0000431 m_command_dict["help"] = CommandObjectSP (new CommandObjectHelp (*this));
Greg Clayton66111032010-06-23 01:19:29 +0000432 m_command_dict["log"] = CommandObjectSP (new CommandObjectLog (*this));
433 m_command_dict["memory"] = CommandObjectSP (new CommandObjectMemory (*this));
Greg Claytonded470d2011-03-19 01:12:21 +0000434 m_command_dict["platform"] = CommandObjectSP (new CommandObjectPlatform (*this));
Enrico Granata21dfcd92012-09-28 23:57:51 +0000435 m_command_dict["plugin"] = CommandObjectSP (new CommandObjectPlugin (*this));
Greg Clayton66111032010-06-23 01:19:29 +0000436 m_command_dict["process"] = CommandObjectSP (new CommandObjectMultiwordProcess (*this));
Greg Claytona7015092010-09-18 01:14:36 +0000437 m_command_dict["quit"] = CommandObjectSP (new CommandObjectQuit (*this));
Greg Clayton66111032010-06-23 01:19:29 +0000438 m_command_dict["register"] = CommandObjectSP (new CommandObjectRegister (*this));
Greg Claytona7015092010-09-18 01:14:36 +0000439 m_command_dict["script"] = CommandObjectSP (new CommandObjectScript (*this, script_language));
Caroline Tice3df9a8d2010-09-04 00:03:46 +0000440 m_command_dict["settings"] = CommandObjectSP (new CommandObjectMultiwordSettings (*this));
Jim Inghamebc09c32010-07-07 03:36:20 +0000441 m_command_dict["source"] = CommandObjectSP (new CommandObjectMultiwordSource (*this));
Greg Clayton66111032010-06-23 01:19:29 +0000442 m_command_dict["target"] = CommandObjectSP (new CommandObjectMultiwordTarget (*this));
443 m_command_dict["thread"] = CommandObjectSP (new CommandObjectMultiwordThread (*this));
Enrico Granata223383e2011-08-16 23:24:13 +0000444 m_command_dict["type"] = CommandObjectSP (new CommandObjectType (*this));
Johnny Chen31c39da2010-12-23 20:21:44 +0000445 m_command_dict["version"] = CommandObjectSP (new CommandObjectVersion (*this));
Johnny Chenf04ee932011-09-22 18:04:58 +0000446 m_command_dict["watchpoint"]= CommandObjectSP (new CommandObjectMultiwordWatchpoint (*this));
Colin Rileyc9c55a22015-05-04 18:39:38 +0000447 m_command_dict["language"] = CommandObjectSP (new CommandObjectLanguage(*this));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000448
Jim Inghamca36cd12012-10-05 19:16:31 +0000449 const char *break_regexes[][2] = {{"^(.*[^[:space:]])[[:space:]]*:[[:space:]]*([[:digit:]]+)[[:space:]]*$", "breakpoint set --file '%1' --line %2"},
Greg Claytond90ac932014-12-01 22:34:03 +0000450 {"^/([^/]+)/$", "breakpoint set --source-pattern-regexp '%1'"},
Jim Inghamca36cd12012-10-05 19:16:31 +0000451 {"^([[:digit:]]+)[[:space:]]*$", "breakpoint set --line %1"},
Greg Clayton722e8852013-02-08 02:54:24 +0000452 {"^\\*?(0x[[:xdigit:]]+)[[:space:]]*$", "breakpoint set --address %1"},
Greg Clayton1b3815c2013-01-30 00:18:29 +0000453 {"^[\"']?([-+]?\\[.*\\])[\"']?[[:space:]]*$", "breakpoint set --name '%1'"},
Jim Inghamca36cd12012-10-05 19:16:31 +0000454 {"^(-.*)$", "breakpoint set %1"},
455 {"^(.*[^[:space:]])`(.*[^[:space:]])[[:space:]]*$", "breakpoint set --name '%2' --shlib '%1'"},
Greg Clayton722e8852013-02-08 02:54:24 +0000456 {"^\\&(.*[^[:space:]])[[:space:]]*$", "breakpoint set --name '%1' --skip-prologue=0"},
Greg Clayton2d4b5122014-08-14 17:58:33 +0000457 {"^[\"']?(.*[^[:space:]\"'])[\"']?[[:space:]]*$", "breakpoint set --name '%1'"}};
Jim Inghamca36cd12012-10-05 19:16:31 +0000458
Saleem Abdulrasool28606952014-06-27 05:17:41 +0000459 size_t num_regexes = llvm::array_lengthof(break_regexes);
Jim Inghamca36cd12012-10-05 19:16:31 +0000460
Greg Clayton7b0992d2013-04-18 22:45:39 +0000461 std::unique_ptr<CommandObjectRegexCommand>
Greg Claytona7015092010-09-18 01:14:36 +0000462 break_regex_cmd_ap(new CommandObjectRegexCommand (*this,
Greg Clayton8b82f082011-04-12 05:54:46 +0000463 "_regexp-break",
Greg Clayton910db5c2015-03-07 00:01:46 +0000464 "Set a breakpoint using a regular expression to specify the location, where <linenum> is in decimal and <address> is in hex.\n",
465 "\n_regexp-break <filename>:<linenum> # _regexp-break main.c:12 // Break on line 12 of main.c\n"
466 "_regexp-break <linenum> # _regexp-break 12 // Break on line 12 of current file\n"
467 "_regexp-break <address> # _regexp-break 0x1234000 // Break on address 0x1234000\n"
468 "_regexp-break <name> # _regexp-break main // Break in 'main' after the prologue\n"
469 "_regexp-break &<name> # _regexp-break &main // Break on the first instruction in 'main'\n"
470 "_regexp-break <module>`<name> # _regexp-break libc.so`malloc // Break in 'malloc' only in the 'libc.so' shared library\n"
471 "_regexp-break /<source-regex>/ # _regexp-break /break here/ // Break on all lines that match the regular expression 'break here' in the current file.\n",
Greg Clayton7d2ef162013-03-29 17:03:23 +0000472 2,
473 CommandCompletions::eSymbolCompletion |
Greg Claytonb5472782015-01-09 19:08:20 +0000474 CommandCompletions::eSourceFileCompletion,
475 false));
Jim Inghamca36cd12012-10-05 19:16:31 +0000476
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000477 if (break_regex_cmd_ap.get())
478 {
Jim Inghamca36cd12012-10-05 19:16:31 +0000479 bool success = true;
480 for (size_t i = 0; i < num_regexes; i++)
481 {
482 success = break_regex_cmd_ap->AddRegexCommand (break_regexes[i][0], break_regexes[i][1]);
483 if (!success)
484 break;
485 }
486 success = break_regex_cmd_ap->AddRegexCommand("^$", "breakpoint list --full");
487
488 if (success)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000489 {
490 CommandObjectSP break_regex_cmd_sp(break_regex_cmd_ap.release());
491 m_command_dict[break_regex_cmd_sp->GetCommandName ()] = break_regex_cmd_sp;
492 }
493 }
Jim Inghamffba2292011-03-22 02:29:32 +0000494
Greg Clayton7b0992d2013-04-18 22:45:39 +0000495 std::unique_ptr<CommandObjectRegexCommand>
Jim Inghamca36cd12012-10-05 19:16:31 +0000496 tbreak_regex_cmd_ap(new CommandObjectRegexCommand (*this,
497 "_regexp-tbreak",
498 "Set a one shot breakpoint using a regular expression to specify the location, where <linenum> is in decimal and <address> is in hex.",
Greg Clayton7d2ef162013-03-29 17:03:23 +0000499 "_regexp-tbreak [<filename>:<linenum>]\n_regexp-break [<linenum>]\n_regexp-break [<address>]\n_regexp-break <...>",
500 2,
501 CommandCompletions::eSymbolCompletion |
Greg Claytonb5472782015-01-09 19:08:20 +0000502 CommandCompletions::eSourceFileCompletion,
503 false));
Jim Inghamca36cd12012-10-05 19:16:31 +0000504
505 if (tbreak_regex_cmd_ap.get())
506 {
507 bool success = true;
508 for (size_t i = 0; i < num_regexes; i++)
509 {
510 // If you add a resultant command string longer than 1024 characters be sure to increase the size of this buffer.
511 char buffer[1024];
512 int num_printed = snprintf(buffer, 1024, "%s %s", break_regexes[i][1], "-o");
513 assert (num_printed < 1024);
Bruce Mitchener8a67bf72015-07-24 00:23:29 +0000514 UNUSED_IF_ASSERT_DISABLED(num_printed);
Jim Inghamca36cd12012-10-05 19:16:31 +0000515 success = tbreak_regex_cmd_ap->AddRegexCommand (break_regexes[i][0], buffer);
516 if (!success)
517 break;
518 }
519 success = tbreak_regex_cmd_ap->AddRegexCommand("^$", "breakpoint list --full");
520
521 if (success)
522 {
523 CommandObjectSP tbreak_regex_cmd_sp(tbreak_regex_cmd_ap.release());
524 m_command_dict[tbreak_regex_cmd_sp->GetCommandName ()] = tbreak_regex_cmd_sp;
525 }
526 }
527
Greg Clayton7b0992d2013-04-18 22:45:39 +0000528 std::unique_ptr<CommandObjectRegexCommand>
Johnny Chen6d675242012-08-24 18:15:45 +0000529 attach_regex_cmd_ap(new CommandObjectRegexCommand (*this,
530 "_regexp-attach",
531 "Attach to a process id if in decimal, otherwise treat the argument as a process name to attach to.",
Greg Clayton7d2ef162013-03-29 17:03:23 +0000532 "_regexp-attach [<pid>]\n_regexp-attach [<process-name>]",
Greg Claytonb5472782015-01-09 19:08:20 +0000533 2,
534 0,
535 false));
Johnny Chen6d675242012-08-24 18:15:45 +0000536 if (attach_regex_cmd_ap.get())
537 {
Greg Clayton3cb4c7d2012-12-15 01:19:07 +0000538 if (attach_regex_cmd_ap->AddRegexCommand("^([0-9]+)[[:space:]]*$", "process attach --pid %1") &&
539 attach_regex_cmd_ap->AddRegexCommand("^(-.*|.* -.*)$", "process attach %1") && // Any options that are specified get passed to 'process attach'
540 attach_regex_cmd_ap->AddRegexCommand("^(.+)$", "process attach --name '%1'") &&
541 attach_regex_cmd_ap->AddRegexCommand("^$", "process attach"))
Johnny Chen6d675242012-08-24 18:15:45 +0000542 {
543 CommandObjectSP attach_regex_cmd_sp(attach_regex_cmd_ap.release());
544 m_command_dict[attach_regex_cmd_sp->GetCommandName ()] = attach_regex_cmd_sp;
545 }
546 }
547
Greg Clayton7b0992d2013-04-18 22:45:39 +0000548 std::unique_ptr<CommandObjectRegexCommand>
Jim Inghamffba2292011-03-22 02:29:32 +0000549 down_regex_cmd_ap(new CommandObjectRegexCommand (*this,
Greg Clayton8b82f082011-04-12 05:54:46 +0000550 "_regexp-down",
551 "Go down \"n\" frames in the stack (1 frame by default).",
Greg Claytonb5472782015-01-09 19:08:20 +0000552 "_regexp-down [n]",
553 2,
554 0,
555 false));
Jim Inghamffba2292011-03-22 02:29:32 +0000556 if (down_regex_cmd_ap.get())
557 {
558 if (down_regex_cmd_ap->AddRegexCommand("^$", "frame select -r -1") &&
559 down_regex_cmd_ap->AddRegexCommand("^([0-9]+)$", "frame select -r -%1"))
560 {
561 CommandObjectSP down_regex_cmd_sp(down_regex_cmd_ap.release());
562 m_command_dict[down_regex_cmd_sp->GetCommandName ()] = down_regex_cmd_sp;
563 }
564 }
565
Greg Clayton7b0992d2013-04-18 22:45:39 +0000566 std::unique_ptr<CommandObjectRegexCommand>
Jim Inghamffba2292011-03-22 02:29:32 +0000567 up_regex_cmd_ap(new CommandObjectRegexCommand (*this,
Greg Clayton8b82f082011-04-12 05:54:46 +0000568 "_regexp-up",
569 "Go up \"n\" frames in the stack (1 frame by default).",
Greg Claytonb5472782015-01-09 19:08:20 +0000570 "_regexp-up [n]",
571 2,
572 0,
573 false));
Jim Inghamffba2292011-03-22 02:29:32 +0000574 if (up_regex_cmd_ap.get())
575 {
576 if (up_regex_cmd_ap->AddRegexCommand("^$", "frame select -r 1") &&
577 up_regex_cmd_ap->AddRegexCommand("^([0-9]+)$", "frame select -r %1"))
578 {
579 CommandObjectSP up_regex_cmd_sp(up_regex_cmd_ap.release());
580 m_command_dict[up_regex_cmd_sp->GetCommandName ()] = up_regex_cmd_sp;
581 }
582 }
Jason Molendabc7748b2011-10-22 01:30:52 +0000583
Greg Clayton7b0992d2013-04-18 22:45:39 +0000584 std::unique_ptr<CommandObjectRegexCommand>
Jason Molendabc7748b2011-10-22 01:30:52 +0000585 display_regex_cmd_ap(new CommandObjectRegexCommand (*this,
Greg Clayton7d2ef162013-03-29 17:03:23 +0000586 "_regexp-display",
587 "Add an expression evaluation stop-hook.",
Greg Claytonb5472782015-01-09 19:08:20 +0000588 "_regexp-display expression",
589 2,
590 0,
591 false));
Jason Molendabc7748b2011-10-22 01:30:52 +0000592 if (display_regex_cmd_ap.get())
593 {
594 if (display_regex_cmd_ap->AddRegexCommand("^(.+)$", "target stop-hook add -o \"expr -- %1\""))
595 {
596 CommandObjectSP display_regex_cmd_sp(display_regex_cmd_ap.release());
597 m_command_dict[display_regex_cmd_sp->GetCommandName ()] = display_regex_cmd_sp;
598 }
599 }
600
Greg Clayton7b0992d2013-04-18 22:45:39 +0000601 std::unique_ptr<CommandObjectRegexCommand>
Jason Molendabc7748b2011-10-22 01:30:52 +0000602 undisplay_regex_cmd_ap(new CommandObjectRegexCommand (*this,
Greg Clayton7d2ef162013-03-29 17:03:23 +0000603 "_regexp-undisplay",
604 "Remove an expression evaluation stop-hook.",
Greg Claytonb5472782015-01-09 19:08:20 +0000605 "_regexp-undisplay stop-hook-number",
606 2,
607 0,
608 false));
Jason Molendabc7748b2011-10-22 01:30:52 +0000609 if (undisplay_regex_cmd_ap.get())
610 {
611 if (undisplay_regex_cmd_ap->AddRegexCommand("^([0-9]+)$", "target stop-hook delete %1"))
612 {
613 CommandObjectSP undisplay_regex_cmd_sp(undisplay_regex_cmd_ap.release());
614 m_command_dict[undisplay_regex_cmd_sp->GetCommandName ()] = undisplay_regex_cmd_sp;
615 }
616 }
617
Greg Clayton7b0992d2013-04-18 22:45:39 +0000618 std::unique_ptr<CommandObjectRegexCommand>
Greg Clayton30c0a1c2012-09-26 22:26:47 +0000619 connect_gdb_remote_cmd_ap(new CommandObjectRegexCommand (*this,
Greg Clayton7d2ef162013-03-29 17:03:23 +0000620 "gdb-remote",
621 "Connect to a remote GDB server. If no hostname is provided, localhost is assumed.",
Greg Claytonb5472782015-01-09 19:08:20 +0000622 "gdb-remote [<hostname>:]<portnum>",
623 2,
624 0,
625 false));
Greg Clayton30c0a1c2012-09-26 22:26:47 +0000626 if (connect_gdb_remote_cmd_ap.get())
627 {
628 if (connect_gdb_remote_cmd_ap->AddRegexCommand("^([^:]+:[[:digit:]]+)$", "process connect --plugin gdb-remote connect://%1") &&
629 connect_gdb_remote_cmd_ap->AddRegexCommand("^([[:digit:]]+)$", "process connect --plugin gdb-remote connect://localhost:%1"))
630 {
631 CommandObjectSP command_sp(connect_gdb_remote_cmd_ap.release());
632 m_command_dict[command_sp->GetCommandName ()] = command_sp;
633 }
634 }
635
Greg Clayton7b0992d2013-04-18 22:45:39 +0000636 std::unique_ptr<CommandObjectRegexCommand>
Greg Clayton30c0a1c2012-09-26 22:26:47 +0000637 connect_kdp_remote_cmd_ap(new CommandObjectRegexCommand (*this,
638 "kdp-remote",
Jason Molendaa7dcb332012-10-23 03:05:16 +0000639 "Connect to a remote KDP server. udp port 41139 is the default port number.",
Greg Claytonb5472782015-01-09 19:08:20 +0000640 "kdp-remote <hostname>[:<portnum>]",
641 2,
642 0,
643 false));
Greg Clayton30c0a1c2012-09-26 22:26:47 +0000644 if (connect_kdp_remote_cmd_ap.get())
645 {
646 if (connect_kdp_remote_cmd_ap->AddRegexCommand("^([^:]+:[[:digit:]]+)$", "process connect --plugin kdp-remote udp://%1") &&
Jason Molendac36b1842012-09-27 02:47:55 +0000647 connect_kdp_remote_cmd_ap->AddRegexCommand("^(.+)$", "process connect --plugin kdp-remote udp://%1:41139"))
Greg Clayton30c0a1c2012-09-26 22:26:47 +0000648 {
649 CommandObjectSP command_sp(connect_kdp_remote_cmd_ap.release());
650 m_command_dict[command_sp->GetCommandName ()] = command_sp;
651 }
652 }
653
Greg Clayton7b0992d2013-04-18 22:45:39 +0000654 std::unique_ptr<CommandObjectRegexCommand>
Jason Molenda4cddfed2012-10-05 05:29:32 +0000655 bt_regex_cmd_ap(new CommandObjectRegexCommand (*this,
Greg Claytonb5472782015-01-09 19:08:20 +0000656 "_regexp-bt",
657 "Show a backtrace. An optional argument is accepted; if that argument is a number, it specifies the number of frames to display. If that argument is 'all', full backtraces of all threads are displayed.",
658 "bt [<digit>|all]",
659 2,
660 0,
661 false));
Jason Molenda4cddfed2012-10-05 05:29:32 +0000662 if (bt_regex_cmd_ap.get())
663 {
664 // accept but don't document "bt -c <number>" -- before bt was a regex command if you wanted to backtrace
665 // three frames you would do "bt -c 3" but the intention is to have this emulate the gdb "bt" command and
666 // so now "bt 3" is the preferred form, in line with gdb.
667 if (bt_regex_cmd_ap->AddRegexCommand("^([[:digit:]]+)$", "thread backtrace -c %1") &&
668 bt_regex_cmd_ap->AddRegexCommand("^-c ([[:digit:]]+)$", "thread backtrace -c %1") &&
669 bt_regex_cmd_ap->AddRegexCommand("^all$", "thread backtrace all") &&
670 bt_regex_cmd_ap->AddRegexCommand("^$", "thread backtrace"))
671 {
672 CommandObjectSP command_sp(bt_regex_cmd_ap.release());
673 m_command_dict[command_sp->GetCommandName ()] = command_sp;
674 }
675 }
676
Greg Clayton7b0992d2013-04-18 22:45:39 +0000677 std::unique_ptr<CommandObjectRegexCommand>
Greg Clayton6bade322013-02-01 23:33:03 +0000678 list_regex_cmd_ap(new CommandObjectRegexCommand (*this,
679 "_regexp-list",
680 "Implements the GDB 'list' command in all of its forms except FILE:FUNCTION and maps them to the appropriate 'source list' commands.",
Ben Langmuiredb3b212013-09-26 20:00:01 +0000681 "_regexp-list [<line>]\n_regexp-list [<file>:<line>]\n_regexp-list [<file>:<line>]",
Greg Clayton7d2ef162013-03-29 17:03:23 +0000682 2,
Greg Claytonb5472782015-01-09 19:08:20 +0000683 CommandCompletions::eSourceFileCompletion,
684 false));
Greg Clayton6bade322013-02-01 23:33:03 +0000685 if (list_regex_cmd_ap.get())
686 {
687 if (list_regex_cmd_ap->AddRegexCommand("^([0-9]+)[[:space:]]*$", "source list --line %1") &&
688 list_regex_cmd_ap->AddRegexCommand("^(.*[^[:space:]])[[:space:]]*:[[:space:]]*([[:digit:]]+)[[:space:]]*$", "source list --file '%1' --line %2") &&
689 list_regex_cmd_ap->AddRegexCommand("^\\*?(0x[[:xdigit:]]+)[[:space:]]*$", "source list --address %1") &&
Greg Claytond9010962013-02-12 18:42:05 +0000690 list_regex_cmd_ap->AddRegexCommand("^-[[:space:]]*$", "source list --reverse") &&
Greg Claytone4ca5152013-03-13 18:25:49 +0000691 list_regex_cmd_ap->AddRegexCommand("^-([[:digit:]]+)[[:space:]]*$", "source list --reverse --count %1") &&
Greg Clayton6bade322013-02-01 23:33:03 +0000692 list_regex_cmd_ap->AddRegexCommand("^(.+)$", "source list --name \"%1\"") &&
693 list_regex_cmd_ap->AddRegexCommand("^$", "source list"))
694 {
695 CommandObjectSP list_regex_cmd_sp(list_regex_cmd_ap.release());
696 m_command_dict[list_regex_cmd_sp->GetCommandName ()] = list_regex_cmd_sp;
697 }
698 }
699
Greg Clayton7b0992d2013-04-18 22:45:39 +0000700 std::unique_ptr<CommandObjectRegexCommand>
Greg Claytonef5651d2013-02-12 18:52:24 +0000701 env_regex_cmd_ap(new CommandObjectRegexCommand (*this,
Greg Clayton7d2ef162013-03-29 17:03:23 +0000702 "_regexp-env",
703 "Implements a shortcut to viewing and setting environment variables.",
Greg Claytonb5472782015-01-09 19:08:20 +0000704 "_regexp-env\n_regexp-env FOO=BAR",
705 2,
706 0,
707 false));
Greg Claytonef5651d2013-02-12 18:52:24 +0000708 if (env_regex_cmd_ap.get())
709 {
710 if (env_regex_cmd_ap->AddRegexCommand("^$", "settings show target.env-vars") &&
711 env_regex_cmd_ap->AddRegexCommand("^([A-Za-z_][A-Za-z_0-9]*=.*)$", "settings set target.env-vars %1"))
712 {
713 CommandObjectSP env_regex_cmd_sp(env_regex_cmd_ap.release());
714 m_command_dict[env_regex_cmd_sp->GetCommandName ()] = env_regex_cmd_sp;
715 }
716 }
717
Richard Mittonf86248d2013-09-12 02:20:34 +0000718 std::unique_ptr<CommandObjectRegexCommand>
719 jump_regex_cmd_ap(new CommandObjectRegexCommand (*this,
720 "_regexp-jump",
721 "Sets the program counter to a new address.",
722 "_regexp-jump [<line>]\n"
723 "_regexp-jump [<+-lineoffset>]\n"
724 "_regexp-jump [<file>:<line>]\n"
Greg Claytonb5472782015-01-09 19:08:20 +0000725 "_regexp-jump [*<addr>]\n",
726 2,
727 0,
728 false));
Richard Mittonf86248d2013-09-12 02:20:34 +0000729 if (jump_regex_cmd_ap.get())
730 {
731 if (jump_regex_cmd_ap->AddRegexCommand("^\\*(.*)$", "thread jump --addr %1") &&
732 jump_regex_cmd_ap->AddRegexCommand("^([0-9]+)$", "thread jump --line %1") &&
733 jump_regex_cmd_ap->AddRegexCommand("^([^:]+):([0-9]+)$", "thread jump --file %1 --line %2") &&
734 jump_regex_cmd_ap->AddRegexCommand("^([+\\-][0-9]+)$", "thread jump --by %1"))
735 {
736 CommandObjectSP jump_regex_cmd_sp(jump_regex_cmd_ap.release());
737 m_command_dict[jump_regex_cmd_sp->GetCommandName ()] = jump_regex_cmd_sp;
738 }
739 }
740
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000741}
742
743int
744CommandInterpreter::GetCommandNamesMatchingPartialString (const char *cmd_str, bool include_aliases,
745 StringList &matches)
746{
747 CommandObject::AddNamesMatchingPartialString (m_command_dict, cmd_str, matches);
748
749 if (include_aliases)
750 {
751 CommandObject::AddNamesMatchingPartialString (m_alias_dict, cmd_str, matches);
752 }
753
754 return matches.GetSize();
755}
756
757CommandObjectSP
758CommandInterpreter::GetCommandSP (const char *cmd_cstr, bool include_aliases, bool exact, StringList *matches)
759{
760 CommandObject::CommandMap::iterator pos;
Greg Claytonc7bece562013-01-25 18:06:21 +0000761 CommandObjectSP command_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000762
763 std::string cmd(cmd_cstr);
764
765 if (HasCommands())
766 {
767 pos = m_command_dict.find(cmd);
768 if (pos != m_command_dict.end())
Greg Claytonc7bece562013-01-25 18:06:21 +0000769 command_sp = pos->second;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000770 }
771
772 if (include_aliases && HasAliases())
773 {
774 pos = m_alias_dict.find(cmd);
775 if (pos != m_alias_dict.end())
Greg Claytonc7bece562013-01-25 18:06:21 +0000776 command_sp = pos->second;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000777 }
778
779 if (HasUserCommands())
780 {
781 pos = m_user_dict.find(cmd);
782 if (pos != m_user_dict.end())
Greg Claytonc7bece562013-01-25 18:06:21 +0000783 command_sp = pos->second;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000784 }
785
Greg Claytonc7bece562013-01-25 18:06:21 +0000786 if (!exact && !command_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000787 {
Jim Ingham279a6c22010-07-06 22:46:59 +0000788 // We will only get into here if we didn't find any exact matches.
789
790 CommandObjectSP user_match_sp, alias_match_sp, real_match_sp;
791
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000792 StringList local_matches;
Ed Masted78c9572014-04-20 00:31:37 +0000793 if (matches == nullptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000794 matches = &local_matches;
795
Jim Ingham279a6c22010-07-06 22:46:59 +0000796 unsigned int num_cmd_matches = 0;
797 unsigned int num_alias_matches = 0;
798 unsigned int num_user_matches = 0;
799
800 // Look through the command dictionaries one by one, and if we get only one match from any of
801 // them in toto, then return that, otherwise return an empty CommandObjectSP and the list of matches.
802
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000803 if (HasCommands())
804 {
805 num_cmd_matches = CommandObject::AddNamesMatchingPartialString (m_command_dict, cmd_cstr, *matches);
806 }
807
808 if (num_cmd_matches == 1)
809 {
810 cmd.assign(matches->GetStringAtIndex(0));
811 pos = m_command_dict.find(cmd);
812 if (pos != m_command_dict.end())
Jim Ingham279a6c22010-07-06 22:46:59 +0000813 real_match_sp = pos->second;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000814 }
815
Jim Ingham490ac552010-06-24 20:28:42 +0000816 if (include_aliases && HasAliases())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000817 {
818 num_alias_matches = CommandObject::AddNamesMatchingPartialString (m_alias_dict, cmd_cstr, *matches);
819
820 }
821
Jim Ingham279a6c22010-07-06 22:46:59 +0000822 if (num_alias_matches == 1)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000823 {
824 cmd.assign(matches->GetStringAtIndex (num_cmd_matches));
825 pos = m_alias_dict.find(cmd);
826 if (pos != m_alias_dict.end())
Jim Ingham279a6c22010-07-06 22:46:59 +0000827 alias_match_sp = pos->second;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000828 }
829
Jim Ingham490ac552010-06-24 20:28:42 +0000830 if (HasUserCommands())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000831 {
832 num_user_matches = CommandObject::AddNamesMatchingPartialString (m_user_dict, cmd_cstr, *matches);
833 }
834
Jim Ingham279a6c22010-07-06 22:46:59 +0000835 if (num_user_matches == 1)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000836 {
837 cmd.assign (matches->GetStringAtIndex (num_cmd_matches + num_alias_matches));
838
839 pos = m_user_dict.find (cmd);
840 if (pos != m_user_dict.end())
Jim Ingham279a6c22010-07-06 22:46:59 +0000841 user_match_sp = pos->second;
842 }
843
844 // If we got exactly one match, return that, otherwise return the match list.
845
846 if (num_user_matches + num_cmd_matches + num_alias_matches == 1)
847 {
848 if (num_cmd_matches)
849 return real_match_sp;
850 else if (num_alias_matches)
851 return alias_match_sp;
852 else
853 return user_match_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000854 }
855 }
Greg Claytonc7bece562013-01-25 18:06:21 +0000856 else if (matches && command_sp)
Jim Ingham279a6c22010-07-06 22:46:59 +0000857 {
858 matches->AppendString (cmd_cstr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000859 }
860
861
Greg Claytonc7bece562013-01-25 18:06:21 +0000862 return command_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000863}
864
Greg Claytonde164aa2011-04-20 16:37:46 +0000865bool
866CommandInterpreter::AddCommand (const char *name, const lldb::CommandObjectSP &cmd_sp, bool can_replace)
867{
868 if (name && name[0])
869 {
870 std::string name_sstr(name);
Enrico Granatae00af802012-10-01 17:19:37 +0000871 bool found = (m_command_dict.find (name_sstr) != m_command_dict.end());
872 if (found && !can_replace)
873 return false;
874 if (found && m_command_dict[name_sstr]->IsRemovable() == false)
Enrico Granata21dfcd92012-09-28 23:57:51 +0000875 return false;
Greg Claytonde164aa2011-04-20 16:37:46 +0000876 m_command_dict[name_sstr] = cmd_sp;
877 return true;
878 }
879 return false;
880}
881
Enrico Granata223383e2011-08-16 23:24:13 +0000882bool
Enrico Granata0a305db2011-11-07 22:57:04 +0000883CommandInterpreter::AddUserCommand (std::string name,
Enrico Granata223383e2011-08-16 23:24:13 +0000884 const lldb::CommandObjectSP &cmd_sp,
885 bool can_replace)
886{
Enrico Granata0a305db2011-11-07 22:57:04 +0000887 if (!name.empty())
Enrico Granata223383e2011-08-16 23:24:13 +0000888 {
Enrico Granata0a305db2011-11-07 22:57:04 +0000889
890 const char* name_cstr = name.c_str();
891
892 // do not allow replacement of internal commands
893 if (CommandExists(name_cstr))
Enrico Granata21dfcd92012-09-28 23:57:51 +0000894 {
895 if (can_replace == false)
896 return false;
897 if (m_command_dict[name]->IsRemovable() == false)
898 return false;
899 }
Enrico Granata0a305db2011-11-07 22:57:04 +0000900
Enrico Granata21dfcd92012-09-28 23:57:51 +0000901 if (UserCommandExists(name_cstr))
902 {
903 if (can_replace == false)
904 return false;
905 if (m_user_dict[name]->IsRemovable() == false)
906 return false;
907 }
908
Enrico Granata0a305db2011-11-07 22:57:04 +0000909 m_user_dict[name] = cmd_sp;
Enrico Granata223383e2011-08-16 23:24:13 +0000910 return true;
911 }
912 return false;
913}
Greg Claytonde164aa2011-04-20 16:37:46 +0000914
Jim Ingham279a6c22010-07-06 22:46:59 +0000915CommandObjectSP
916CommandInterpreter::GetCommandSPExact (const char *cmd_cstr, bool include_aliases)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000917{
Caroline Tice472362e2010-12-14 18:51:39 +0000918 Args cmd_words (cmd_cstr); // Break up the command string into words, in case it's a multi-word command.
919 CommandObjectSP ret_val; // Possibly empty return value.
920
Ed Masted78c9572014-04-20 00:31:37 +0000921 if (cmd_cstr == nullptr)
Caroline Tice472362e2010-12-14 18:51:39 +0000922 return ret_val;
923
924 if (cmd_words.GetArgumentCount() == 1)
Ed Masted78c9572014-04-20 00:31:37 +0000925 return GetCommandSP(cmd_cstr, include_aliases, true, nullptr);
Caroline Tice472362e2010-12-14 18:51:39 +0000926 else
927 {
928 // We have a multi-word command (seemingly), so we need to do more work.
929 // First, get the cmd_obj_sp for the first word in the command.
Ed Masted78c9572014-04-20 00:31:37 +0000930 CommandObjectSP cmd_obj_sp = GetCommandSP (cmd_words.GetArgumentAtIndex (0), include_aliases, true, nullptr);
931 if (cmd_obj_sp.get() != nullptr)
Caroline Tice472362e2010-12-14 18:51:39 +0000932 {
933 // Loop through the rest of the words in the command (everything passed in was supposed to be part of a
934 // command name), and find the appropriate sub-command SP for each command word....
935 size_t end = cmd_words.GetArgumentCount();
936 for (size_t j= 1; j < end; ++j)
937 {
938 if (cmd_obj_sp->IsMultiwordObject())
939 {
Greg Clayton998255b2012-10-13 02:07:45 +0000940 cmd_obj_sp = cmd_obj_sp->GetSubcommandSP (cmd_words.GetArgumentAtIndex (j));
Ed Masted78c9572014-04-20 00:31:37 +0000941 if (cmd_obj_sp.get() == nullptr)
Caroline Tice472362e2010-12-14 18:51:39 +0000942 // The sub-command name was invalid. Fail and return the empty 'ret_val'.
943 return ret_val;
944 }
945 else
946 // We have more words in the command name, but we don't have a multiword object. Fail and return
947 // empty 'ret_val'.
948 return ret_val;
949 }
950 // We successfully looped through all the command words and got valid command objects for them. Assign the
951 // last object retrieved to 'ret_val'.
952 ret_val = cmd_obj_sp;
953 }
954 }
955 return ret_val;
Jim Ingham279a6c22010-07-06 22:46:59 +0000956}
957
958CommandObject *
959CommandInterpreter::GetCommandObjectExact (const char *cmd_cstr, bool include_aliases)
960{
961 return GetCommandSPExact (cmd_cstr, include_aliases).get();
962}
963
964CommandObject *
965CommandInterpreter::GetCommandObject (const char *cmd_cstr, StringList *matches)
966{
967 CommandObject *command_obj = GetCommandSP (cmd_cstr, false, true, matches).get();
968
969 // If we didn't find an exact match to the command string in the commands, look in
970 // the aliases.
Enrico Granata5342c442013-06-18 18:01:08 +0000971
972 if (command_obj)
973 return command_obj;
Jim Ingham279a6c22010-07-06 22:46:59 +0000974
Enrico Granata5342c442013-06-18 18:01:08 +0000975 command_obj = GetCommandSP (cmd_cstr, true, true, matches).get();
Jim Ingham279a6c22010-07-06 22:46:59 +0000976
Enrico Granata5342c442013-06-18 18:01:08 +0000977 if (command_obj)
978 return command_obj;
979
980 // If there wasn't an exact match then look for an inexact one in just the commands
Ed Masted78c9572014-04-20 00:31:37 +0000981 command_obj = GetCommandSP(cmd_cstr, false, false, nullptr).get();
Matt Kopec038ff812013-04-23 16:17:32 +0000982
983 // Finally, if there wasn't an inexact match among the commands, look for an inexact
984 // match in both the commands and aliases.
Enrico Granata5342c442013-06-18 18:01:08 +0000985
986 if (command_obj)
987 {
988 if (matches)
989 matches->AppendString(command_obj->GetCommandName());
990 return command_obj;
991 }
992
993 return GetCommandSP(cmd_cstr, true, false, matches).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000994}
995
996bool
997CommandInterpreter::CommandExists (const char *cmd)
998{
999 return m_command_dict.find(cmd) != m_command_dict.end();
1000}
1001
1002bool
Caroline Ticeca90c472011-05-06 21:37:15 +00001003CommandInterpreter::ProcessAliasOptionsArgs (lldb::CommandObjectSP &cmd_obj_sp,
1004 const char *options_args,
1005 OptionArgVectorSP &option_arg_vector_sp)
1006{
1007 bool success = true;
1008 OptionArgVector *option_arg_vector = option_arg_vector_sp.get();
1009
1010 if (!options_args || (strlen (options_args) < 1))
1011 return true;
1012
1013 std::string options_string (options_args);
1014 Args args (options_args);
1015 CommandReturnObject result;
1016 // Check to see if the command being aliased can take any command options.
1017 Options *options = cmd_obj_sp->GetOptions ();
1018 if (options)
1019 {
1020 // See if any options were specified as part of the alias; if so, handle them appropriately.
1021 options->NotifyOptionParsingStarting ();
1022 args.Unshift ("dummy_arg");
1023 args.ParseAliasOptions (*options, result, option_arg_vector, options_string);
1024 args.Shift ();
1025 if (result.Succeeded())
1026 options->VerifyPartialOptions (result);
1027 if (!result.Succeeded() && result.GetStatus() != lldb::eReturnStatusStarted)
1028 {
1029 result.AppendError ("Unable to create requested alias.\n");
1030 return false;
1031 }
1032 }
1033
Greg Clayton5521f992011-10-28 21:38:01 +00001034 if (!options_string.empty())
Caroline Ticeca90c472011-05-06 21:37:15 +00001035 {
1036 if (cmd_obj_sp->WantsRawCommandString ())
1037 option_arg_vector->push_back (OptionArgPair ("<argument>",
1038 OptionArgValue (-1,
1039 options_string)));
1040 else
1041 {
Greg Claytonc7bece562013-01-25 18:06:21 +00001042 const size_t argc = args.GetArgumentCount();
Caroline Ticeca90c472011-05-06 21:37:15 +00001043 for (size_t i = 0; i < argc; ++i)
1044 if (strcmp (args.GetArgumentAtIndex (i), "") != 0)
1045 option_arg_vector->push_back
1046 (OptionArgPair ("<argument>",
1047 OptionArgValue (-1,
1048 std::string (args.GetArgumentAtIndex (i)))));
1049 }
1050 }
1051
1052 return success;
1053}
1054
1055bool
Jim Ingham298f3782013-04-03 00:25:49 +00001056CommandInterpreter::GetAliasFullName (const char *cmd, std::string &full_name)
1057{
1058 bool exact_match = (m_alias_dict.find(cmd) != m_alias_dict.end());
1059 if (exact_match)
1060 {
1061 full_name.assign(cmd);
1062 return exact_match;
1063 }
1064 else
1065 {
1066 StringList matches;
1067 size_t num_alias_matches;
1068 num_alias_matches = CommandObject::AddNamesMatchingPartialString (m_alias_dict, cmd, matches);
1069 if (num_alias_matches == 1)
1070 {
1071 // Make sure this isn't shadowing a command in the regular command space:
1072 StringList regular_matches;
1073 const bool include_aliases = false;
1074 const bool exact = false;
1075 CommandObjectSP cmd_obj_sp(GetCommandSP (cmd, include_aliases, exact, &regular_matches));
1076 if (cmd_obj_sp || regular_matches.GetSize() > 0)
1077 return false;
1078 else
1079 {
1080 full_name.assign (matches.GetStringAtIndex(0));
1081 return true;
1082 }
1083 }
1084 else
1085 return false;
1086 }
1087}
1088
1089bool
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001090CommandInterpreter::AliasExists (const char *cmd)
1091{
1092 return m_alias_dict.find(cmd) != m_alias_dict.end();
1093}
1094
1095bool
1096CommandInterpreter::UserCommandExists (const char *cmd)
1097{
1098 return m_user_dict.find(cmd) != m_user_dict.end();
1099}
1100
1101void
1102CommandInterpreter::AddAlias (const char *alias_name, CommandObjectSP& command_obj_sp)
1103{
Jim Ingham279a6c22010-07-06 22:46:59 +00001104 command_obj_sp->SetIsAlias (true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001105 m_alias_dict[alias_name] = command_obj_sp;
1106}
1107
1108bool
1109CommandInterpreter::RemoveAlias (const char *alias_name)
1110{
1111 CommandObject::CommandMap::iterator pos = m_alias_dict.find(alias_name);
1112 if (pos != m_alias_dict.end())
1113 {
1114 m_alias_dict.erase(pos);
1115 return true;
1116 }
1117 return false;
1118}
Greg Claytonb5472782015-01-09 19:08:20 +00001119
1120bool
1121CommandInterpreter::RemoveCommand (const char *cmd)
1122{
1123 auto pos = m_command_dict.find(cmd);
1124 if (pos != m_command_dict.end())
1125 {
1126 if (pos->second->IsRemovable())
1127 {
1128 // Only regular expression objects or python commands are removable
1129 m_command_dict.erase(pos);
1130 return true;
1131 }
1132 }
1133 return false;
1134}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001135bool
1136CommandInterpreter::RemoveUser (const char *alias_name)
1137{
1138 CommandObject::CommandMap::iterator pos = m_user_dict.find(alias_name);
1139 if (pos != m_user_dict.end())
1140 {
1141 m_user_dict.erase(pos);
1142 return true;
1143 }
1144 return false;
1145}
1146
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001147void
1148CommandInterpreter::GetAliasHelp (const char *alias_name, const char *command_name, StreamString &help_string)
1149{
1150 help_string.Printf ("'%s", command_name);
1151 OptionArgVectorSP option_arg_vector_sp = GetAliasOptions (alias_name);
1152
Sean Callanan9a028512012-08-09 00:50:26 +00001153 if (option_arg_vector_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001154 {
1155 OptionArgVector *options = option_arg_vector_sp.get();
Andy Gibbsa297a972013-06-19 19:04:53 +00001156 for (size_t i = 0; i < options->size(); ++i)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001157 {
1158 OptionArgPair cur_option = (*options)[i];
1159 std::string opt = cur_option.first;
Caroline Ticed9d63362010-12-07 19:58:26 +00001160 OptionArgValue value_pair = cur_option.second;
1161 std::string value = value_pair.second;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001162 if (opt.compare("<argument>") == 0)
1163 {
1164 help_string.Printf (" %s", value.c_str());
1165 }
1166 else
1167 {
1168 help_string.Printf (" %s", opt.c_str());
1169 if ((value.compare ("<no-argument>") != 0)
1170 && (value.compare ("<need-argument") != 0))
1171 {
1172 help_string.Printf (" %s", value.c_str());
1173 }
1174 }
1175 }
1176 }
1177
1178 help_string.Printf ("'");
1179}
1180
Greg Clayton12fc3e02010-08-26 22:05:43 +00001181size_t
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001182CommandInterpreter::FindLongestCommandWord (CommandObject::CommandMap &dict)
1183{
1184 CommandObject::CommandMap::const_iterator pos;
Greg Clayton12fc3e02010-08-26 22:05:43 +00001185 CommandObject::CommandMap::const_iterator end = dict.end();
1186 size_t max_len = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001187
Greg Clayton12fc3e02010-08-26 22:05:43 +00001188 for (pos = dict.begin(); pos != end; ++pos)
1189 {
1190 size_t len = pos->first.size();
1191 if (max_len < len)
1192 max_len = len;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001193 }
Greg Clayton12fc3e02010-08-26 22:05:43 +00001194 return max_len;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001195}
1196
1197void
Enrico Granata223383e2011-08-16 23:24:13 +00001198CommandInterpreter::GetHelp (CommandReturnObject &result,
Enrico Granata08633ee2011-09-09 17:49:36 +00001199 uint32_t cmd_types)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001200{
Kate Stonea487aa42015-01-15 00:52:41 +00001201 const char * help_prologue = GetDebugger().GetIOHandlerHelpPrologue();
1202 if (help_prologue != NULL)
1203 {
1204 OutputFormattedHelpText(result.GetOutputStream(), NULL, help_prologue);
1205 }
1206
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001207 CommandObject::CommandMap::const_iterator pos;
Greg Claytonc7bece562013-01-25 18:06:21 +00001208 size_t max_len = FindLongestCommandWord (m_command_dict);
Enrico Granata223383e2011-08-16 23:24:13 +00001209
1210 if ( (cmd_types & eCommandTypesBuiltin) == eCommandTypesBuiltin )
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001211 {
Kate Stonea487aa42015-01-15 00:52:41 +00001212 result.AppendMessage("Debugger commands:");
Enrico Granata223383e2011-08-16 23:24:13 +00001213 result.AppendMessage("");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001214
Enrico Granata223383e2011-08-16 23:24:13 +00001215 for (pos = m_command_dict.begin(); pos != m_command_dict.end(); ++pos)
1216 {
Kate Stonea487aa42015-01-15 00:52:41 +00001217 if (!(cmd_types & eCommandTypesHidden) && (pos->first.compare(0, 1, "_") == 0))
1218 continue;
1219
Enrico Granata223383e2011-08-16 23:24:13 +00001220 OutputFormattedHelpText (result.GetOutputStream(), pos->first.c_str(), "--", pos->second->GetHelp(),
1221 max_len);
1222 }
1223 result.AppendMessage("");
1224
1225 }
1226
Greg Clayton5521f992011-10-28 21:38:01 +00001227 if (!m_alias_dict.empty() && ( (cmd_types & eCommandTypesAliases) == eCommandTypesAliases ))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001228 {
Kate Stonea487aa42015-01-15 00:52:41 +00001229 result.AppendMessageWithFormat("Current command abbreviations "
1230 "(type '%shelp command alias' for more info):\n",
1231 GetCommandPrefix());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001232 result.AppendMessage("");
Greg Clayton12fc3e02010-08-26 22:05:43 +00001233 max_len = FindLongestCommandWord (m_alias_dict);
1234
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001235 for (pos = m_alias_dict.begin(); pos != m_alias_dict.end(); ++pos)
1236 {
1237 StreamString sstr;
1238 StreamString translation_and_help;
1239 std::string entry_name = pos->first;
1240 std::string second_entry = pos->second.get()->GetCommandName();
1241 GetAliasHelp (pos->first.c_str(), pos->second->GetCommandName(), sstr);
1242
1243 translation_and_help.Printf ("(%s) %s", sstr.GetData(), pos->second->GetHelp());
1244 OutputFormattedHelpText (result.GetOutputStream(), pos->first.c_str(), "--",
1245 translation_and_help.GetData(), max_len);
1246 }
1247 result.AppendMessage("");
1248 }
1249
Greg Clayton5521f992011-10-28 21:38:01 +00001250 if (!m_user_dict.empty() && ( (cmd_types & eCommandTypesUserDef) == eCommandTypesUserDef ))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001251 {
Kate Stonea487aa42015-01-15 00:52:41 +00001252 result.AppendMessage ("Current user-defined commands:");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001253 result.AppendMessage("");
Enrico Granata223383e2011-08-16 23:24:13 +00001254 max_len = FindLongestCommandWord (m_user_dict);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001255 for (pos = m_user_dict.begin(); pos != m_user_dict.end(); ++pos)
1256 {
Enrico Granata223383e2011-08-16 23:24:13 +00001257 OutputFormattedHelpText (result.GetOutputStream(), pos->first.c_str(), "--", pos->second->GetHelp(),
1258 max_len);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001259 }
1260 result.AppendMessage("");
1261 }
1262
Kate Stonea487aa42015-01-15 00:52:41 +00001263 result.AppendMessageWithFormat("For more information on any command, type '%shelp <command-name>'.\n",
1264 GetCommandPrefix());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001265}
1266
Caroline Tice844d2302010-12-09 22:52:49 +00001267CommandObject *
1268CommandInterpreter::GetCommandObjectForCommand (std::string &command_string)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001269{
Caroline Tice844d2302010-12-09 22:52:49 +00001270 // This function finds the final, lowest-level, alias-resolved command object whose 'Execute' function will
1271 // eventually be invoked by the given command line.
1272
Ed Masted78c9572014-04-20 00:31:37 +00001273 CommandObject *cmd_obj = nullptr;
Adrian McCarthy2304b6f2015-04-23 20:00:25 +00001274 size_t start = command_string.find_first_not_of (k_white_space);
Caroline Tice844d2302010-12-09 22:52:49 +00001275 size_t end = 0;
1276 bool done = false;
1277 while (!done)
1278 {
1279 if (start != std::string::npos)
1280 {
1281 // Get the next word from command_string.
Adrian McCarthy2304b6f2015-04-23 20:00:25 +00001282 end = command_string.find_first_of (k_white_space, start);
Caroline Tice844d2302010-12-09 22:52:49 +00001283 if (end == std::string::npos)
1284 end = command_string.size();
1285 std::string cmd_word = command_string.substr (start, end - start);
1286
Ed Masted78c9572014-04-20 00:31:37 +00001287 if (cmd_obj == nullptr)
Caroline Tice844d2302010-12-09 22:52:49 +00001288 // Since cmd_obj is NULL we are on our first time through this loop. Check to see if cmd_word is a valid
1289 // command or alias.
1290 cmd_obj = GetCommandObject (cmd_word.c_str());
1291 else if (cmd_obj->IsMultiwordObject ())
1292 {
1293 // Our current object is a multi-word object; see if the cmd_word is a valid sub-command for our object.
Greg Clayton998255b2012-10-13 02:07:45 +00001294 CommandObject *sub_cmd_obj = cmd_obj->GetSubcommandObject (cmd_word.c_str());
Caroline Tice844d2302010-12-09 22:52:49 +00001295 if (sub_cmd_obj)
1296 cmd_obj = sub_cmd_obj;
Bruce Mitchener58ef3912015-06-18 05:27:05 +00001297 else // cmd_word was not a valid sub-command word, so we are done
Caroline Tice844d2302010-12-09 22:52:49 +00001298 done = true;
1299 }
1300 else
1301 // We have a cmd_obj and it is not a multi-word object, so we are done.
1302 done = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001303
Caroline Tice844d2302010-12-09 22:52:49 +00001304 // If we didn't find a valid command object, or our command object is not a multi-word object, or
1305 // we are at the end of the command_string, then we are done. Otherwise, find the start of the
1306 // next word.
1307
1308 if (!cmd_obj || !cmd_obj->IsMultiwordObject() || end >= command_string.size())
1309 done = true;
1310 else
Adrian McCarthy2304b6f2015-04-23 20:00:25 +00001311 start = command_string.find_first_not_of (k_white_space, end);
Caroline Tice844d2302010-12-09 22:52:49 +00001312 }
1313 else
1314 // Unable to find any more words.
1315 done = true;
1316 }
1317
1318 if (end == command_string.size())
1319 command_string.clear();
1320 else
1321 command_string = command_string.substr(end);
1322
1323 return cmd_obj;
1324}
1325
Greg Clayton5521f992011-10-28 21:38:01 +00001326static const char *k_valid_command_chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_";
Greg Clayton51964162011-10-25 00:36:27 +00001327static void
1328StripLeadingSpaces (std::string &s)
Caroline Tice844d2302010-12-09 22:52:49 +00001329{
Greg Clayton51964162011-10-25 00:36:27 +00001330 if (!s.empty())
Caroline Tice844d2302010-12-09 22:52:49 +00001331 {
Greg Clayton51964162011-10-25 00:36:27 +00001332 size_t pos = s.find_first_not_of (k_white_space);
1333 if (pos == std::string::npos)
1334 s.clear();
1335 else if (pos == 0)
1336 return;
1337 s.erase (0, pos);
1338 }
1339}
1340
Greg Clayton93c62e62011-11-09 23:25:03 +00001341static size_t
1342FindArgumentTerminator (const std::string &s)
1343{
Greg Clayton93c62e62011-11-09 23:25:03 +00001344 const size_t s_len = s.size();
1345 size_t offset = 0;
1346 while (offset < s_len)
1347 {
1348 size_t pos = s.find ("--", offset);
1349 if (pos == std::string::npos)
1350 break;
1351 if (pos > 0)
1352 {
1353 if (isspace(s[pos-1]))
1354 {
1355 // Check if the string ends "\s--" (where \s is a space character)
1356 // or if we have "\s--\s".
1357 if ((pos + 2 >= s_len) || isspace(s[pos+2]))
1358 {
Greg Clayton93c62e62011-11-09 23:25:03 +00001359 return pos;
1360 }
1361 }
1362 }
1363 offset = pos + 2;
1364 }
Greg Clayton93c62e62011-11-09 23:25:03 +00001365 return std::string::npos;
1366}
1367
Greg Clayton51964162011-10-25 00:36:27 +00001368static bool
Greg Clayton5521f992011-10-28 21:38:01 +00001369ExtractCommand (std::string &command_string, std::string &command, std::string &suffix, char &quote_char)
Greg Clayton51964162011-10-25 00:36:27 +00001370{
Greg Clayton5521f992011-10-28 21:38:01 +00001371 command.clear();
1372 suffix.clear();
Greg Clayton51964162011-10-25 00:36:27 +00001373 StripLeadingSpaces (command_string);
1374
1375 bool result = false;
1376 quote_char = '\0';
1377
1378 if (!command_string.empty())
1379 {
1380 const char first_char = command_string[0];
1381 if (first_char == '\'' || first_char == '"')
Caroline Tice844d2302010-12-09 22:52:49 +00001382 {
Greg Clayton51964162011-10-25 00:36:27 +00001383 quote_char = first_char;
1384 const size_t end_quote_pos = command_string.find (quote_char, 1);
1385 if (end_quote_pos == std::string::npos)
Caroline Tice2b5e8502011-05-11 16:07:06 +00001386 {
Greg Clayton5521f992011-10-28 21:38:01 +00001387 command.swap (command_string);
Greg Clayton51964162011-10-25 00:36:27 +00001388 command_string.erase ();
Caroline Tice2b5e8502011-05-11 16:07:06 +00001389 }
1390 else
1391 {
Greg Clayton5521f992011-10-28 21:38:01 +00001392 command.assign (command_string, 1, end_quote_pos - 1);
Greg Clayton51964162011-10-25 00:36:27 +00001393 if (end_quote_pos + 1 < command_string.size())
1394 command_string.erase (0, command_string.find_first_not_of (k_white_space, end_quote_pos + 1));
1395 else
1396 command_string.erase ();
Caroline Tice2b5e8502011-05-11 16:07:06 +00001397 }
Caroline Tice844d2302010-12-09 22:52:49 +00001398 }
1399 else
1400 {
Greg Clayton51964162011-10-25 00:36:27 +00001401 const size_t first_space_pos = command_string.find_first_of (k_white_space);
1402 if (first_space_pos == std::string::npos)
Caroline Tice2b5e8502011-05-11 16:07:06 +00001403 {
Greg Clayton5521f992011-10-28 21:38:01 +00001404 command.swap (command_string);
Greg Clayton51964162011-10-25 00:36:27 +00001405 command_string.erase();
Caroline Tice2b5e8502011-05-11 16:07:06 +00001406 }
1407 else
1408 {
Greg Clayton5521f992011-10-28 21:38:01 +00001409 command.assign (command_string, 0, first_space_pos);
1410 command_string.erase(0, command_string.find_first_not_of (k_white_space, first_space_pos));
Caroline Tice2b5e8502011-05-11 16:07:06 +00001411 }
Caroline Tice844d2302010-12-09 22:52:49 +00001412 }
Greg Clayton51964162011-10-25 00:36:27 +00001413 result = true;
Caroline Tice844d2302010-12-09 22:52:49 +00001414 }
Greg Clayton5521f992011-10-28 21:38:01 +00001415
1416
1417 if (!command.empty())
1418 {
1419 // actual commands can't start with '-' or '_'
1420 if (command[0] != '-' && command[0] != '_')
1421 {
1422 size_t pos = command.find_first_not_of(k_valid_command_chars);
1423 if (pos > 0 && pos != std::string::npos)
1424 {
1425 suffix.assign (command.begin() + pos, command.end());
1426 command.erase (pos);
1427 }
1428 }
1429 }
Greg Clayton51964162011-10-25 00:36:27 +00001430
1431 return result;
Caroline Tice844d2302010-12-09 22:52:49 +00001432}
1433
Greg Clayton5521f992011-10-28 21:38:01 +00001434CommandObject *
1435CommandInterpreter::BuildAliasResult (const char *alias_name,
1436 std::string &raw_input_string,
1437 std::string &alias_result,
1438 CommandReturnObject &result)
Caroline Tice844d2302010-12-09 22:52:49 +00001439{
Ed Masted78c9572014-04-20 00:31:37 +00001440 CommandObject *alias_cmd_obj = nullptr;
Pavel Labath00b7f952015-03-02 12:46:22 +00001441 Args cmd_args (raw_input_string);
Caroline Tice844d2302010-12-09 22:52:49 +00001442 alias_cmd_obj = GetCommandObject (alias_name);
1443 StreamString result_str;
1444
1445 if (alias_cmd_obj)
1446 {
1447 std::string alias_name_str = alias_name;
1448 if ((cmd_args.GetArgumentCount() == 0)
1449 || (alias_name_str.compare (cmd_args.GetArgumentAtIndex(0)) != 0))
1450 cmd_args.Unshift (alias_name);
1451
1452 result_str.Printf ("%s", alias_cmd_obj->GetCommandName ());
1453 OptionArgVectorSP option_arg_vector_sp = GetAliasOptions (alias_name);
1454
1455 if (option_arg_vector_sp.get())
1456 {
1457 OptionArgVector *option_arg_vector = option_arg_vector_sp.get();
1458
Andy Gibbsa297a972013-06-19 19:04:53 +00001459 for (size_t i = 0; i < option_arg_vector->size(); ++i)
Caroline Tice844d2302010-12-09 22:52:49 +00001460 {
1461 OptionArgPair option_pair = (*option_arg_vector)[i];
1462 OptionArgValue value_pair = option_pair.second;
1463 int value_type = value_pair.first;
1464 std::string option = option_pair.first;
1465 std::string value = value_pair.second;
1466 if (option.compare ("<argument>") == 0)
1467 result_str.Printf (" %s", value.c_str());
1468 else
1469 {
1470 result_str.Printf (" %s", option.c_str());
Ted Woodwarde7e8e3c2015-04-06 21:55:14 +00001471 if (value_type != OptionParser::eNoArgument)
Caroline Tice844d2302010-12-09 22:52:49 +00001472 {
Ted Woodwarde7e8e3c2015-04-06 21:55:14 +00001473 if (value_type != OptionParser::eOptionalArgument)
1474 result_str.Printf (" ");
Caroline Tice844d2302010-12-09 22:52:49 +00001475 int index = GetOptionArgumentPosition (value.c_str());
1476 if (index == 0)
1477 result_str.Printf ("%s", value.c_str());
Saleem Abdulrasool3985c8c2014-04-02 03:51:35 +00001478 else if (static_cast<size_t>(index) >= cmd_args.GetArgumentCount())
Caroline Tice844d2302010-12-09 22:52:49 +00001479 {
1480
1481 result.AppendErrorWithFormat
1482 ("Not enough arguments provided; you need at least %d arguments to use this alias.\n",
1483 index);
1484 result.SetStatus (eReturnStatusFailed);
Adrian McCarthy2304b6f2015-04-23 20:00:25 +00001485 return nullptr;
Caroline Tice844d2302010-12-09 22:52:49 +00001486 }
1487 else
1488 {
1489 size_t strpos = raw_input_string.find (cmd_args.GetArgumentAtIndex (index));
1490 if (strpos != std::string::npos)
1491 raw_input_string = raw_input_string.erase (strpos,
1492 strlen (cmd_args.GetArgumentAtIndex (index)));
1493 result_str.Printf ("%s", cmd_args.GetArgumentAtIndex (index));
1494 }
1495 }
1496 }
1497 }
1498 }
1499
1500 alias_result = result_str.GetData();
1501 }
Greg Clayton5521f992011-10-28 21:38:01 +00001502 return alias_cmd_obj;
Caroline Tice844d2302010-12-09 22:52:49 +00001503}
1504
Greg Clayton5a314712011-10-14 07:41:33 +00001505Error
1506CommandInterpreter::PreprocessCommand (std::string &command)
1507{
1508 // The command preprocessor needs to do things to the command
1509 // line before any parsing of arguments or anything else is done.
Adrian McCarthy2304b6f2015-04-23 20:00:25 +00001510 // The only current stuff that gets preprocessed is anything enclosed
Greg Clayton5a314712011-10-14 07:41:33 +00001511 // in backtick ('`') characters is evaluated as an expression and
1512 // the result of the expression must be a scalar that can be substituted
1513 // into the command. An example would be:
1514 // (lldb) memory read `$rsp + 20`
1515 Error error; // Error for any expressions that might not evaluate
1516 size_t start_backtick;
1517 size_t pos = 0;
1518 while ((start_backtick = command.find ('`', pos)) != std::string::npos)
1519 {
1520 if (start_backtick > 0 && command[start_backtick-1] == '\\')
1521 {
Bruce Mitchener58ef3912015-06-18 05:27:05 +00001522 // The backtick was preceded by a '\' character, remove the slash
Greg Clayton5a314712011-10-14 07:41:33 +00001523 // and don't treat the backtick as the start of an expression
1524 command.erase(start_backtick-1, 1);
1525 // No need to add one to start_backtick since we just deleted a char
1526 pos = start_backtick;
1527 }
1528 else
1529 {
1530 const size_t expr_content_start = start_backtick + 1;
1531 const size_t end_backtick = command.find ('`', expr_content_start);
1532 if (end_backtick == std::string::npos)
1533 return error;
1534 else if (end_backtick == expr_content_start)
1535 {
1536 // Empty expression (two backticks in a row)
1537 command.erase (start_backtick, 2);
1538 }
1539 else
1540 {
1541 std::string expr_str (command, expr_content_start, end_backtick - expr_content_start);
1542
Greg Clayton4e0fe8a2012-07-12 20:32:19 +00001543 ExecutionContext exe_ctx(GetExecutionContext());
1544 Target *target = exe_ctx.GetTargetPtr();
Johnny Chen51ea0ad2011-10-29 00:21:50 +00001545 // Get a dummy target to allow for calculator mode while processing backticks.
1546 // This also helps break the infinite loop caused when target is null.
1547 if (!target)
Jim Ingham893c9322014-11-22 01:42:44 +00001548 target = m_debugger.GetDummyTarget();
Greg Clayton5a314712011-10-14 07:41:33 +00001549 if (target)
1550 {
Greg Clayton5a314712011-10-14 07:41:33 +00001551 ValueObjectSP expr_result_valobj_sp;
Enrico Granatad4439aa2012-09-05 20:41:26 +00001552
Jim Ingham35e1bda2012-10-16 21:41:58 +00001553 EvaluateExpressionOptions options;
Jim Ingham6fbc48b2013-11-07 00:11:47 +00001554 options.SetCoerceToId(false);
1555 options.SetUnwindOnError(true);
1556 options.SetIgnoreBreakpoints(true);
1557 options.SetKeepInMemory(false);
1558 options.SetTryAllThreads(true);
1559 options.SetTimeoutUsec(0);
Enrico Granatad4439aa2012-09-05 20:41:26 +00001560
Jim Ingham1624a2d2014-05-05 02:26:40 +00001561 ExpressionResults expr_result = target->EvaluateExpression (expr_str.c_str(),
Enrico Granataca0e5ad2014-10-09 23:09:40 +00001562 exe_ctx.GetFramePtr(),
1563 expr_result_valobj_sp,
1564 options);
Enrico Granatad4439aa2012-09-05 20:41:26 +00001565
Jim Ingham8646d3c2014-05-05 02:47:44 +00001566 if (expr_result == eExpressionCompleted)
Greg Clayton5a314712011-10-14 07:41:33 +00001567 {
1568 Scalar scalar;
Enrico Granataca0e5ad2014-10-09 23:09:40 +00001569 if (expr_result_valobj_sp)
1570 expr_result_valobj_sp = expr_result_valobj_sp->GetQualifiedRepresentationIfAvailable(expr_result_valobj_sp->GetDynamicValueType(), true);
Greg Clayton5a314712011-10-14 07:41:33 +00001571 if (expr_result_valobj_sp->ResolveValue (scalar))
1572 {
1573 command.erase (start_backtick, end_backtick - start_backtick + 1);
1574 StreamString value_strm;
1575 const bool show_type = false;
1576 scalar.GetValue (&value_strm, show_type);
1577 size_t value_string_size = value_strm.GetSize();
1578 if (value_string_size)
1579 {
1580 command.insert (start_backtick, value_strm.GetData(), value_string_size);
1581 pos = start_backtick + value_string_size;
1582 continue;
1583 }
1584 else
1585 {
1586 error.SetErrorStringWithFormat("expression value didn't result in a scalar value for the expression '%s'", expr_str.c_str());
1587 }
1588 }
1589 else
1590 {
1591 error.SetErrorStringWithFormat("expression value didn't result in a scalar value for the expression '%s'", expr_str.c_str());
1592 }
1593 }
1594 else
1595 {
1596 if (expr_result_valobj_sp)
1597 error = expr_result_valobj_sp->GetError();
1598 if (error.Success())
1599 {
1600
1601 switch (expr_result)
1602 {
Jim Ingham8646d3c2014-05-05 02:47:44 +00001603 case eExpressionSetupError:
Greg Clayton5a314712011-10-14 07:41:33 +00001604 error.SetErrorStringWithFormat("expression setup error for the expression '%s'", expr_str.c_str());
1605 break;
Jim Ingham8646d3c2014-05-05 02:47:44 +00001606 case eExpressionParseError:
Jim Ingham1624a2d2014-05-05 02:26:40 +00001607 error.SetErrorStringWithFormat ("expression parse error for the expression '%s'", expr_str.c_str());
1608 break;
Jim Ingham8646d3c2014-05-05 02:47:44 +00001609 case eExpressionResultUnavailable:
Jim Ingham1624a2d2014-05-05 02:26:40 +00001610 error.SetErrorStringWithFormat ("expression error fetching result for the expression '%s'", expr_str.c_str());
Jim Ingham8646d3c2014-05-05 02:47:44 +00001611 case eExpressionCompleted:
Greg Clayton5a314712011-10-14 07:41:33 +00001612 break;
Jim Ingham8646d3c2014-05-05 02:47:44 +00001613 case eExpressionDiscarded:
Greg Clayton5a314712011-10-14 07:41:33 +00001614 error.SetErrorStringWithFormat("expression discarded for the expression '%s'", expr_str.c_str());
1615 break;
Jim Ingham8646d3c2014-05-05 02:47:44 +00001616 case eExpressionInterrupted:
Greg Clayton5a314712011-10-14 07:41:33 +00001617 error.SetErrorStringWithFormat("expression interrupted for the expression '%s'", expr_str.c_str());
1618 break;
Jim Ingham8646d3c2014-05-05 02:47:44 +00001619 case eExpressionHitBreakpoint:
Jim Ingham184e9812013-01-15 02:47:48 +00001620 error.SetErrorStringWithFormat("expression hit breakpoint for the expression '%s'", expr_str.c_str());
1621 break;
Jim Ingham8646d3c2014-05-05 02:47:44 +00001622 case eExpressionTimedOut:
Greg Clayton5a314712011-10-14 07:41:33 +00001623 error.SetErrorStringWithFormat("expression timed out for the expression '%s'", expr_str.c_str());
1624 break;
Jim Ingham8646d3c2014-05-05 02:47:44 +00001625 case eExpressionStoppedForDebug:
Greg Claytonc28ce782013-11-08 23:38:26 +00001626 error.SetErrorStringWithFormat("expression stop at entry point for debugging for the expression '%s'", expr_str.c_str());
1627 break;
Greg Clayton5a314712011-10-14 07:41:33 +00001628 }
1629 }
1630 }
1631 }
1632 }
1633 if (error.Fail())
1634 break;
1635 }
1636 }
1637 return error;
1638}
1639
1640
Caroline Tice844d2302010-12-09 22:52:49 +00001641bool
1642CommandInterpreter::HandleCommand (const char *command_line,
Enrico Granata5f5ab602012-05-31 01:09:06 +00001643 LazyBool lazy_add_to_history,
Caroline Tice844d2302010-12-09 22:52:49 +00001644 CommandReturnObject &result,
Jim Inghame16c50a2011-02-18 00:54:25 +00001645 ExecutionContext *override_context,
Johnny Chen80fdd7c2011-10-05 00:42:59 +00001646 bool repeat_on_empty_command,
1647 bool no_context_switching)
Jim Inghame16c50a2011-02-18 00:54:25 +00001648
Caroline Tice844d2302010-12-09 22:52:49 +00001649{
Jim Inghame16c50a2011-02-18 00:54:25 +00001650
Caroline Tice844d2302010-12-09 22:52:49 +00001651 std::string command_string (command_line);
Jim Inghama5a97eb2011-07-12 03:12:18 +00001652 std::string original_command_string (command_line);
Caroline Tice844d2302010-12-09 22:52:49 +00001653
Greg Clayton5160ce52013-03-27 23:08:40 +00001654 Log *log (lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_COMMANDS));
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00001655 Host::SetCrashDescriptionWithFormat ("HandleCommand(command = \"%s\")", command_line);
1656
1657 // Make a scoped cleanup object that will clear the crash description string
1658 // on exit of this function.
Ed Masted78c9572014-04-20 00:31:37 +00001659 lldb_utility::CleanUp <const char *> crash_description_cleanup(nullptr, Host::SetCrashDescription);
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00001660
Caroline Tice844d2302010-12-09 22:52:49 +00001661 if (log)
1662 log->Printf ("Processing command: %s", command_line);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001663
Jim Ingham30244832010-11-04 23:08:45 +00001664 Timer scoped_timer (__PRETTY_FUNCTION__, "Handling command: %s.", command_line);
1665
Johnny Chen80fdd7c2011-10-05 00:42:59 +00001666 if (!no_context_switching)
1667 UpdateExecutionContext (override_context);
Enrico Granata5f5ab602012-05-31 01:09:06 +00001668
Enrico Granata5f5ab602012-05-31 01:09:06 +00001669 bool add_to_history;
1670 if (lazy_add_to_history == eLazyBoolCalculate)
1671 add_to_history = (m_command_source_depth == 0);
1672 else
1673 add_to_history = (lazy_add_to_history == eLazyBoolYes);
1674
Jim Inghame16c50a2011-02-18 00:54:25 +00001675 bool empty_command = false;
1676 bool comment_command = false;
1677 if (command_string.empty())
1678 empty_command = true;
1679 else
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001680 {
Jim Inghame16c50a2011-02-18 00:54:25 +00001681 const char *k_space_characters = "\t\n\v\f\r ";
1682
1683 size_t non_space = command_string.find_first_not_of (k_space_characters);
1684 // Check for empty line or comment line (lines whose first
1685 // non-space character is the comment character for this interpreter)
1686 if (non_space == std::string::npos)
1687 empty_command = true;
1688 else if (command_string[non_space] == m_comment_char)
1689 comment_command = true;
Enrico Granata7594f142013-06-17 22:51:50 +00001690 else if (command_string[non_space] == CommandHistory::g_repeat_char)
Jim Inghama5a97eb2011-07-12 03:12:18 +00001691 {
Enrico Granata7594f142013-06-17 22:51:50 +00001692 const char *history_string = m_command_history.FindString(command_string.c_str() + non_space);
Ed Masted78c9572014-04-20 00:31:37 +00001693 if (history_string == nullptr)
Jim Inghama5a97eb2011-07-12 03:12:18 +00001694 {
1695 result.AppendErrorWithFormat ("Could not find entry: %s in history", command_string.c_str());
1696 result.SetStatus(eReturnStatusFailed);
1697 return false;
1698 }
1699 add_to_history = false;
1700 command_string = history_string;
1701 original_command_string = history_string;
1702 }
Jim Inghame16c50a2011-02-18 00:54:25 +00001703 }
1704
1705 if (empty_command)
1706 {
1707 if (repeat_on_empty_command)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001708 {
Enrico Granata7594f142013-06-17 22:51:50 +00001709 if (m_command_history.IsEmpty())
Jim Inghame16c50a2011-02-18 00:54:25 +00001710 {
1711 result.AppendError ("empty command");
1712 result.SetStatus(eReturnStatusFailed);
1713 return false;
1714 }
1715 else
1716 {
1717 command_line = m_repeat_command.c_str();
1718 command_string = command_line;
Jim Inghama5a97eb2011-07-12 03:12:18 +00001719 original_command_string = command_line;
Jim Inghame16c50a2011-02-18 00:54:25 +00001720 if (m_repeat_command.empty())
1721 {
1722 result.AppendErrorWithFormat("No auto repeat.\n");
1723 result.SetStatus (eReturnStatusFailed);
1724 return false;
1725 }
1726 }
1727 add_to_history = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001728 }
1729 else
1730 {
Jim Inghame16c50a2011-02-18 00:54:25 +00001731 result.SetStatus (eReturnStatusSuccessFinishNoResult);
1732 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001733 }
Jim Inghame16c50a2011-02-18 00:54:25 +00001734 }
1735 else if (comment_command)
1736 {
1737 result.SetStatus (eReturnStatusSuccessFinishNoResult);
1738 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001739 }
Caroline Tice2b5e8502011-05-11 16:07:06 +00001740
Greg Clayton5a314712011-10-14 07:41:33 +00001741
1742 Error error (PreprocessCommand (command_string));
1743
1744 if (error.Fail())
1745 {
1746 result.AppendError (error.AsCString());
1747 result.SetStatus(eReturnStatusFailed);
1748 return false;
1749 }
Adrian McCarthy2304b6f2015-04-23 20:00:25 +00001750
Caroline Tice844d2302010-12-09 22:52:49 +00001751 // Phase 1.
1752
Adrian McCarthy2304b6f2015-04-23 20:00:25 +00001753 // Before we do ANY kind of argument processing, we need to figure out what
1754 // the real/final command object is for the specified command. This gets
1755 // complicated by the fact that the user could have specified an alias, and,
1756 // in translating the alias, there may also be command options and/or even
1757 // data (including raw text strings) that need to be found and inserted into
1758 // the command line as part of the translation. So this first step is plain
1759 // look-up and replacement, resulting in:
1760 // 1. the command object whose Execute method will actually be called
1761 // 2. a revised command string, with all substitutions and replacements
1762 // taken care of
1763 // From 1 above, we can determine whether the Execute function wants raw
1764 // input or not.
Caroline Ticed9d63362010-12-07 19:58:26 +00001765
Adrian McCarthy2304b6f2015-04-23 20:00:25 +00001766 CommandObject *cmd_obj = ResolveCommandImpl(command_string, result);
Caroline Tice844d2302010-12-09 22:52:49 +00001767
Adrian McCarthy2304b6f2015-04-23 20:00:25 +00001768 // Although the user may have abbreviated the command, the command_string now
1769 // has the command expanded to the full name. For example, if the input
1770 // was "br s -n main", command_string is now "breakpoint set -n main".
Filipe Cabecinhasaf1537f2012-05-16 23:25:54 +00001771
Caroline Tice844d2302010-12-09 22:52:49 +00001772 if (log)
1773 {
Adrian McCarthy2304b6f2015-04-23 20:00:25 +00001774 log->Printf("HandleCommand, cmd_obj : '%s'", cmd_obj ? cmd_obj->GetCommandName() : "<not found>");
1775 log->Printf("HandleCommand, (revised) command_string: '%s'", command_string.c_str());
1776 const bool wants_raw_input = (cmd_obj != NULL) ? cmd_obj->WantsRawCommandString() : false;
1777 log->Printf("HandleCommand, wants_raw_input:'%s'", wants_raw_input ? "True" : "False");
Caroline Tice844d2302010-12-09 22:52:49 +00001778 }
1779
1780 // Phase 2.
1781 // Take care of things like setting up the history command & calling the appropriate Execute method on the
1782 // CommandObject, with the appropriate arguments.
1783
Ed Masted78c9572014-04-20 00:31:37 +00001784 if (cmd_obj != nullptr)
Caroline Tice844d2302010-12-09 22:52:49 +00001785 {
1786 if (add_to_history)
1787 {
Adrian McCarthy2304b6f2015-04-23 20:00:25 +00001788 Args command_args (command_string);
Caroline Tice844d2302010-12-09 22:52:49 +00001789 const char *repeat_command = cmd_obj->GetRepeatCommand(command_args, 0);
Ed Masted78c9572014-04-20 00:31:37 +00001790 if (repeat_command != nullptr)
Caroline Tice844d2302010-12-09 22:52:49 +00001791 m_repeat_command.assign(repeat_command);
1792 else
Jim Inghama5a97eb2011-07-12 03:12:18 +00001793 m_repeat_command.assign(original_command_string.c_str());
Caroline Tice844d2302010-12-09 22:52:49 +00001794
Enrico Granata7594f142013-06-17 22:51:50 +00001795 m_command_history.AppendString (original_command_string);
Caroline Tice844d2302010-12-09 22:52:49 +00001796 }
1797
Caroline Tice01274c02010-12-11 08:16:56 +00001798 std::string remainder;
Adrian McCarthy2304b6f2015-04-23 20:00:25 +00001799 const std::size_t actual_cmd_name_len = strlen (cmd_obj->GetCommandName());
Caroline Tice01274c02010-12-11 08:16:56 +00001800 if (actual_cmd_name_len < command_string.length())
Adrian McCarthy2304b6f2015-04-23 20:00:25 +00001801 remainder = command_string.substr (actual_cmd_name_len);
Caroline Tice844d2302010-12-09 22:52:49 +00001802
1803 // Remove any initial spaces
Adrian McCarthy2304b6f2015-04-23 20:00:25 +00001804 size_t pos = remainder.find_first_not_of (k_white_space);
Caroline Tice844d2302010-12-09 22:52:49 +00001805 if (pos != 0 && pos != std::string::npos)
Greg Claytona3482592011-04-22 20:58:45 +00001806 remainder.erase(0, pos);
Caroline Tice844d2302010-12-09 22:52:49 +00001807
1808 if (log)
Jason Molendabfb36ff2011-08-25 00:20:04 +00001809 log->Printf ("HandleCommand, command line after removing command name(s): '%s'", remainder.c_str());
Caroline Tice844d2302010-12-09 22:52:49 +00001810
Jim Ingham5a988412012-06-08 21:56:10 +00001811 cmd_obj->Execute (remainder.c_str(), result);
Caroline Tice844d2302010-12-09 22:52:49 +00001812 }
1813 else
1814 {
1815 // We didn't find the first command object, so complete the first argument.
Adrian McCarthy2304b6f2015-04-23 20:00:25 +00001816 Args command_args (command_string);
Caroline Tice844d2302010-12-09 22:52:49 +00001817 StringList matches;
1818 int num_matches;
1819 int cursor_index = 0;
1820 int cursor_char_position = strlen (command_args.GetArgumentAtIndex(0));
1821 bool word_complete;
1822 num_matches = HandleCompletionMatches (command_args,
1823 cursor_index,
1824 cursor_char_position,
1825 0,
1826 -1,
1827 word_complete,
1828 matches);
1829
1830 if (num_matches > 0)
1831 {
1832 std::string error_msg;
1833 error_msg.assign ("ambiguous command '");
1834 error_msg.append(command_args.GetArgumentAtIndex(0));
1835 error_msg.append ("'.");
1836
1837 error_msg.append (" Possible completions:");
1838 for (int i = 0; i < num_matches; i++)
1839 {
1840 error_msg.append ("\n\t");
1841 error_msg.append (matches.GetStringAtIndex (i));
1842 }
1843 error_msg.append ("\n");
Greg Claytonc7bece562013-01-25 18:06:21 +00001844 result.AppendRawError (error_msg.c_str());
Caroline Tice844d2302010-12-09 22:52:49 +00001845 }
1846 else
1847 result.AppendErrorWithFormat ("Unrecognized command '%s'.\n", command_args.GetArgumentAtIndex (0));
1848
1849 result.SetStatus (eReturnStatusFailed);
1850 }
1851
Jason Molendabfb36ff2011-08-25 00:20:04 +00001852 if (log)
1853 log->Printf ("HandleCommand, command %s", (result.Succeeded() ? "succeeded" : "did not succeed"));
1854
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001855 return result.Succeeded();
1856}
1857
1858int
1859CommandInterpreter::HandleCompletionMatches (Args &parsed_line,
1860 int &cursor_index,
1861 int &cursor_char_position,
1862 int match_start_point,
1863 int max_return_elements,
Jim Ingham558ce122010-06-30 05:02:46 +00001864 bool &word_complete,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001865 StringList &matches)
1866{
1867 int num_command_matches = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001868 bool look_for_subcommand = false;
Jim Ingham558ce122010-06-30 05:02:46 +00001869
1870 // For any of the command completions a unique match will be a complete word.
1871 word_complete = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001872
1873 if (cursor_index == -1)
1874 {
1875 // We got nothing on the command line, so return the list of commands
Jim Ingham279a6c22010-07-06 22:46:59 +00001876 bool include_aliases = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001877 num_command_matches = GetCommandNamesMatchingPartialString ("", include_aliases, matches);
1878 }
1879 else if (cursor_index == 0)
1880 {
1881 // The cursor is in the first argument, so just do a lookup in the dictionary.
Jim Ingham279a6c22010-07-06 22:46:59 +00001882 CommandObject *cmd_obj = GetCommandObject (parsed_line.GetArgumentAtIndex(0), &matches);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001883 num_command_matches = matches.GetSize();
1884
1885 if (num_command_matches == 1
1886 && cmd_obj && cmd_obj->IsMultiwordObject()
Ed Masted78c9572014-04-20 00:31:37 +00001887 && matches.GetStringAtIndex(0) != nullptr
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001888 && strcmp (parsed_line.GetArgumentAtIndex(0), matches.GetStringAtIndex(0)) == 0)
1889 {
Greg Clayton765d2e22013-12-10 19:14:04 +00001890 if (parsed_line.GetArgumentCount() == 1)
1891 {
1892 word_complete = true;
1893 }
1894 else
1895 {
1896 look_for_subcommand = true;
1897 num_command_matches = 0;
1898 matches.DeleteStringAtIndex(0);
1899 parsed_line.AppendArgument ("");
1900 cursor_index++;
1901 cursor_char_position = 0;
1902 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001903 }
1904 }
1905
1906 if (cursor_index > 0 || look_for_subcommand)
1907 {
1908 // We are completing further on into a commands arguments, so find the command and tell it
1909 // to complete the command.
1910 // First see if there is a matching initial command:
Jim Ingham279a6c22010-07-06 22:46:59 +00001911 CommandObject *command_object = GetCommandObject (parsed_line.GetArgumentAtIndex(0));
Ed Masted78c9572014-04-20 00:31:37 +00001912 if (command_object == nullptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001913 {
1914 return 0;
1915 }
1916 else
1917 {
1918 parsed_line.Shift();
1919 cursor_index--;
Greg Claytona7015092010-09-18 01:14:36 +00001920 num_command_matches = command_object->HandleCompletion (parsed_line,
Greg Clayton66111032010-06-23 01:19:29 +00001921 cursor_index,
1922 cursor_char_position,
1923 match_start_point,
Jim Ingham558ce122010-06-30 05:02:46 +00001924 max_return_elements,
1925 word_complete,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001926 matches);
1927 }
1928 }
1929
1930 return num_command_matches;
1931
1932}
1933
1934int
1935CommandInterpreter::HandleCompletion (const char *current_line,
1936 const char *cursor,
1937 const char *last_char,
1938 int match_start_point,
1939 int max_return_elements,
1940 StringList &matches)
1941{
1942 // We parse the argument up to the cursor, so the last argument in parsed_line is
1943 // the one containing the cursor, and the cursor is after the last character.
1944
Pavel Labath00b7f952015-03-02 12:46:22 +00001945 Args parsed_line(llvm::StringRef(current_line, last_char - current_line));
1946 Args partial_parsed_line(llvm::StringRef(current_line, cursor - current_line));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001947
Jim Inghama5a97eb2011-07-12 03:12:18 +00001948 // Don't complete comments, and if the line we are completing is just the history repeat character,
1949 // substitute the appropriate history line.
1950 const char *first_arg = parsed_line.GetArgumentAtIndex(0);
1951 if (first_arg)
1952 {
1953 if (first_arg[0] == m_comment_char)
1954 return 0;
Enrico Granata7594f142013-06-17 22:51:50 +00001955 else if (first_arg[0] == CommandHistory::g_repeat_char)
Jim Inghama5a97eb2011-07-12 03:12:18 +00001956 {
Enrico Granata7594f142013-06-17 22:51:50 +00001957 const char *history_string = m_command_history.FindString (first_arg);
Ed Masted78c9572014-04-20 00:31:37 +00001958 if (history_string != nullptr)
Jim Inghama5a97eb2011-07-12 03:12:18 +00001959 {
1960 matches.Clear();
1961 matches.InsertStringAtIndex(0, history_string);
1962 return -2;
1963 }
1964 else
1965 return 0;
1966
1967 }
1968 }
1969
1970
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001971 int num_args = partial_parsed_line.GetArgumentCount();
1972 int cursor_index = partial_parsed_line.GetArgumentCount() - 1;
1973 int cursor_char_position;
1974
1975 if (cursor_index == -1)
1976 cursor_char_position = 0;
1977 else
1978 cursor_char_position = strlen (partial_parsed_line.GetArgumentAtIndex(cursor_index));
Jim Inghamfe0c4252010-12-14 19:56:01 +00001979
1980 if (cursor > current_line && cursor[-1] == ' ')
1981 {
1982 // We are just after a space. If we are in an argument, then we will continue
1983 // parsing, but if we are between arguments, then we have to complete whatever the next
1984 // element would be.
1985 // We can distinguish the two cases because if we are in an argument (e.g. because the space is
1986 // protected by a quote) then the space will also be in the parsed argument...
1987
1988 const char *current_elem = partial_parsed_line.GetArgumentAtIndex(cursor_index);
1989 if (cursor_char_position == 0 || current_elem[cursor_char_position - 1] != ' ')
1990 {
Greg Clayton765d2e22013-12-10 19:14:04 +00001991 parsed_line.InsertArgumentAtIndex(cursor_index + 1, "", '\0');
Jim Inghamfe0c4252010-12-14 19:56:01 +00001992 cursor_index++;
1993 cursor_char_position = 0;
1994 }
1995 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001996
1997 int num_command_matches;
1998
1999 matches.Clear();
2000
2001 // Only max_return_elements == -1 is supported at present:
2002 assert (max_return_elements == -1);
Jim Ingham558ce122010-06-30 05:02:46 +00002003 bool word_complete;
Greg Clayton66111032010-06-23 01:19:29 +00002004 num_command_matches = HandleCompletionMatches (parsed_line,
2005 cursor_index,
2006 cursor_char_position,
2007 match_start_point,
Jim Ingham558ce122010-06-30 05:02:46 +00002008 max_return_elements,
2009 word_complete,
Greg Clayton66111032010-06-23 01:19:29 +00002010 matches);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002011
2012 if (num_command_matches <= 0)
2013 return num_command_matches;
2014
2015 if (num_args == 0)
2016 {
2017 // If we got an empty string, insert nothing.
2018 matches.InsertStringAtIndex(0, "");
2019 }
2020 else
2021 {
2022 // Now figure out if there is a common substring, and if so put that in element 0, otherwise
2023 // put an empty string in element 0.
2024 std::string command_partial_str;
2025 if (cursor_index >= 0)
Jim Ingham49e80a12010-10-22 18:47:16 +00002026 command_partial_str.assign(parsed_line.GetArgumentAtIndex(cursor_index),
2027 parsed_line.GetArgumentAtIndex(cursor_index) + cursor_char_position);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002028
2029 std::string common_prefix;
2030 matches.LongestCommonPrefix (common_prefix);
Greg Claytonc7bece562013-01-25 18:06:21 +00002031 const size_t partial_name_len = command_partial_str.size();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002032
2033 // If we matched a unique single command, add a space...
Jim Ingham558ce122010-06-30 05:02:46 +00002034 // Only do this if the completer told us this was a complete word, however...
2035 if (num_command_matches == 1 && word_complete)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002036 {
2037 char quote_char = parsed_line.GetArgumentQuoteCharAtIndex(cursor_index);
2038 if (quote_char != '\0')
2039 common_prefix.push_back(quote_char);
2040
2041 common_prefix.push_back(' ');
2042 }
2043 common_prefix.erase (0, partial_name_len);
2044 matches.InsertStringAtIndex(0, common_prefix.c_str());
2045 }
2046 return num_command_matches;
2047}
2048
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002049
2050CommandInterpreter::~CommandInterpreter ()
2051{
2052}
2053
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002054void
Greg Clayton44d93782014-01-27 23:43:24 +00002055CommandInterpreter::UpdatePrompt (const char *new_prompt)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002056{
Greg Clayton44d93782014-01-27 23:43:24 +00002057 EventSP prompt_change_event_sp (new Event(eBroadcastBitResetPrompt, new EventDataBytes (new_prompt)));;
2058 BroadcastEvent (prompt_change_event_sp);
2059 if (m_command_io_handler_sp)
2060 m_command_io_handler_sp->SetPrompt(new_prompt);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002061}
2062
Jim Ingham97a6dc72010-10-04 19:49:29 +00002063
2064bool
2065CommandInterpreter::Confirm (const char *message, bool default_answer)
2066{
Jim Ingham3bcdb292010-10-04 22:44:14 +00002067 // Check AutoConfirm first:
2068 if (m_debugger.GetAutoConfirm())
2069 return default_answer;
Greg Clayton44d93782014-01-27 23:43:24 +00002070
2071 IOHandlerConfirm *confirm = new IOHandlerConfirm(m_debugger,
2072 message,
2073 default_answer);
2074 IOHandlerSP io_handler_sp (confirm);
2075 m_debugger.RunIOHandler (io_handler_sp);
2076 return confirm->GetResponse();
Jim Ingham97a6dc72010-10-04 19:49:29 +00002077}
2078
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002079OptionArgVectorSP
2080CommandInterpreter::GetAliasOptions (const char *alias_name)
2081{
2082 OptionArgMap::iterator pos;
2083 OptionArgVectorSP ret_val;
2084
2085 std::string alias (alias_name);
2086
2087 if (HasAliasOptions())
2088 {
2089 pos = m_alias_options.find (alias);
2090 if (pos != m_alias_options.end())
2091 ret_val = pos->second;
2092 }
2093
2094 return ret_val;
2095}
2096
2097void
2098CommandInterpreter::RemoveAliasOptions (const char *alias_name)
2099{
2100 OptionArgMap::iterator pos = m_alias_options.find(alias_name);
2101 if (pos != m_alias_options.end())
2102 {
2103 m_alias_options.erase (pos);
2104 }
2105}
2106
2107void
2108CommandInterpreter::AddOrReplaceAliasOptions (const char *alias_name, OptionArgVectorSP &option_arg_vector_sp)
2109{
2110 m_alias_options[alias_name] = option_arg_vector_sp;
2111}
2112
2113bool
2114CommandInterpreter::HasCommands ()
2115{
2116 return (!m_command_dict.empty());
2117}
2118
2119bool
2120CommandInterpreter::HasAliases ()
2121{
2122 return (!m_alias_dict.empty());
2123}
2124
2125bool
2126CommandInterpreter::HasUserCommands ()
2127{
2128 return (!m_user_dict.empty());
2129}
2130
2131bool
2132CommandInterpreter::HasAliasOptions ()
2133{
2134 return (!m_alias_options.empty());
2135}
2136
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002137void
Caroline Tice4ab31c92010-10-12 21:57:09 +00002138CommandInterpreter::BuildAliasCommandArgs (CommandObject *alias_cmd_obj,
2139 const char *alias_name,
2140 Args &cmd_args,
Caroline Ticed9d63362010-12-07 19:58:26 +00002141 std::string &raw_input_string,
Caroline Tice4ab31c92010-10-12 21:57:09 +00002142 CommandReturnObject &result)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002143{
2144 OptionArgVectorSP option_arg_vector_sp = GetAliasOptions (alias_name);
Caroline Ticed9d63362010-12-07 19:58:26 +00002145
2146 bool wants_raw_input = alias_cmd_obj->WantsRawCommandString();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002147
Caroline Ticed9d63362010-12-07 19:58:26 +00002148 // Make sure that the alias name is the 0th element in cmd_args
2149 std::string alias_name_str = alias_name;
2150 if (alias_name_str.compare (cmd_args.GetArgumentAtIndex(0)) != 0)
2151 cmd_args.Unshift (alias_name);
2152
2153 Args new_args (alias_cmd_obj->GetCommandName());
2154 if (new_args.GetArgumentCount() == 2)
2155 new_args.Shift();
2156
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002157 if (option_arg_vector_sp.get())
2158 {
Caroline Ticed9d63362010-12-07 19:58:26 +00002159 if (wants_raw_input)
2160 {
2161 // We have a command that both has command options and takes raw input. Make *sure* it has a
2162 // " -- " in the right place in the raw_input_string.
2163 size_t pos = raw_input_string.find(" -- ");
2164 if (pos == std::string::npos)
2165 {
2166 // None found; assume it goes at the beginning of the raw input string
2167 raw_input_string.insert (0, " -- ");
2168 }
2169 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002170
2171 OptionArgVector *option_arg_vector = option_arg_vector_sp.get();
Greg Claytonc7bece562013-01-25 18:06:21 +00002172 const size_t old_size = cmd_args.GetArgumentCount();
Caroline Tice4ab31c92010-10-12 21:57:09 +00002173 std::vector<bool> used (old_size + 1, false);
2174
2175 used[0] = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002176
Andy Gibbsa297a972013-06-19 19:04:53 +00002177 for (size_t i = 0; i < option_arg_vector->size(); ++i)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002178 {
2179 OptionArgPair option_pair = (*option_arg_vector)[i];
Caroline Ticed9d63362010-12-07 19:58:26 +00002180 OptionArgValue value_pair = option_pair.second;
2181 int value_type = value_pair.first;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002182 std::string option = option_pair.first;
Caroline Ticed9d63362010-12-07 19:58:26 +00002183 std::string value = value_pair.second;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002184 if (option.compare ("<argument>") == 0)
Caroline Ticed9d63362010-12-07 19:58:26 +00002185 {
2186 if (!wants_raw_input
2187 || (value.compare("--") != 0)) // Since we inserted this above, make sure we don't insert it twice
2188 new_args.AppendArgument (value.c_str());
2189 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002190 else
2191 {
Virgile Belloe2607b52013-09-05 16:42:23 +00002192 if (value_type != OptionParser::eOptionalArgument)
Caroline Ticed9d63362010-12-07 19:58:26 +00002193 new_args.AppendArgument (option.c_str());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002194 if (value.compare ("<no-argument>") != 0)
2195 {
2196 int index = GetOptionArgumentPosition (value.c_str());
2197 if (index == 0)
Caroline Ticed9d63362010-12-07 19:58:26 +00002198 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002199 // value was NOT a positional argument; must be a real value
Virgile Belloe2607b52013-09-05 16:42:23 +00002200 if (value_type != OptionParser::eOptionalArgument)
Caroline Ticed9d63362010-12-07 19:58:26 +00002201 new_args.AppendArgument (value.c_str());
2202 else
2203 {
2204 char buffer[255];
2205 ::snprintf (buffer, sizeof (buffer), "%s%s", option.c_str(), value.c_str());
2206 new_args.AppendArgument (buffer);
2207 }
2208
2209 }
Saleem Abdulrasool3985c8c2014-04-02 03:51:35 +00002210 else if (static_cast<size_t>(index) >= cmd_args.GetArgumentCount())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002211 {
2212 result.AppendErrorWithFormat
2213 ("Not enough arguments provided; you need at least %d arguments to use this alias.\n",
2214 index);
2215 result.SetStatus (eReturnStatusFailed);
2216 return;
2217 }
2218 else
2219 {
Caroline Ticed9d63362010-12-07 19:58:26 +00002220 // Find and remove cmd_args.GetArgumentAtIndex(i) from raw_input_string
2221 size_t strpos = raw_input_string.find (cmd_args.GetArgumentAtIndex (index));
2222 if (strpos != std::string::npos)
2223 {
2224 raw_input_string = raw_input_string.erase (strpos, strlen (cmd_args.GetArgumentAtIndex (index)));
2225 }
2226
Virgile Belloe2607b52013-09-05 16:42:23 +00002227 if (value_type != OptionParser::eOptionalArgument)
Caroline Ticed9d63362010-12-07 19:58:26 +00002228 new_args.AppendArgument (cmd_args.GetArgumentAtIndex (index));
2229 else
2230 {
2231 char buffer[255];
2232 ::snprintf (buffer, sizeof(buffer), "%s%s", option.c_str(),
2233 cmd_args.GetArgumentAtIndex (index));
2234 new_args.AppendArgument (buffer);
2235 }
Caroline Tice4ab31c92010-10-12 21:57:09 +00002236 used[index] = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002237 }
2238 }
2239 }
2240 }
2241
Andy Gibbsa297a972013-06-19 19:04:53 +00002242 for (size_t j = 0; j < cmd_args.GetArgumentCount(); ++j)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002243 {
Caroline Ticed9d63362010-12-07 19:58:26 +00002244 if (!used[j] && !wants_raw_input)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002245 new_args.AppendArgument (cmd_args.GetArgumentAtIndex (j));
2246 }
2247
2248 cmd_args.Clear();
Vince Harrond7e6a4f2015-05-13 00:25:54 +00002249 cmd_args.SetArguments (new_args.GetArgumentCount(), new_args.GetConstArgumentVector());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002250 }
2251 else
2252 {
2253 result.SetStatus (eReturnStatusSuccessFinishNoResult);
Caroline Ticed9d63362010-12-07 19:58:26 +00002254 // This alias was not created with any options; nothing further needs to be done, unless it is a command that
2255 // wants raw input, in which case we need to clear the rest of the data from cmd_args, since its in the raw
2256 // input string.
2257 if (wants_raw_input)
2258 {
2259 cmd_args.Clear();
Vince Harrond7e6a4f2015-05-13 00:25:54 +00002260 cmd_args.SetArguments (new_args.GetArgumentCount(), new_args.GetConstArgumentVector());
Caroline Ticed9d63362010-12-07 19:58:26 +00002261 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002262 return;
2263 }
2264
2265 result.SetStatus (eReturnStatusSuccessFinishNoResult);
2266 return;
2267}
2268
2269
2270int
2271CommandInterpreter::GetOptionArgumentPosition (const char *in_string)
2272{
2273 int position = 0; // Any string that isn't an argument position, i.e. '%' followed by an integer, gets a position
2274 // of zero.
2275
Vince Harrond7e6a4f2015-05-13 00:25:54 +00002276 const char *cptr = in_string;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002277
2278 // Does it start with '%'
2279 if (cptr[0] == '%')
2280 {
2281 ++cptr;
2282
2283 // Is the rest of it entirely digits?
2284 if (isdigit (cptr[0]))
2285 {
2286 const char *start = cptr;
2287 while (isdigit (cptr[0]))
2288 ++cptr;
2289
2290 // We've gotten to the end of the digits; are we at the end of the string?
2291 if (cptr[0] == '\0')
2292 position = atoi (start);
2293 }
2294 }
2295
2296 return position;
2297}
2298
2299void
2300CommandInterpreter::SourceInitFile (bool in_cwd, CommandReturnObject &result)
2301{
Jim Ingham16e0c682011-08-12 23:34:31 +00002302 FileSpec init_file;
Greg Clayton14a35512011-09-11 00:01:44 +00002303 if (in_cwd)
Jim Ingham16e0c682011-08-12 23:34:31 +00002304 {
Greg Clayton14a35512011-09-11 00:01:44 +00002305 // In the current working directory we don't load any program specific
2306 // .lldbinit files, we only look for a "./.lldbinit" file.
2307 if (m_skip_lldbinit_files)
2308 return;
2309
2310 init_file.SetFile ("./.lldbinit", true);
Jim Ingham16e0c682011-08-12 23:34:31 +00002311 }
Greg Clayton14a35512011-09-11 00:01:44 +00002312 else
Jim Ingham16e0c682011-08-12 23:34:31 +00002313 {
Greg Clayton14a35512011-09-11 00:01:44 +00002314 // If we aren't looking in the current working directory we are looking
2315 // in the home directory. We will first see if there is an application
2316 // specific ".lldbinit" file whose name is "~/.lldbinit" followed by a
2317 // "-" and the name of the program. If this file doesn't exist, we fall
2318 // back to just the "~/.lldbinit" file. We also obey any requests to not
2319 // load the init files.
Zachary Turnera21fee02014-08-21 21:49:24 +00002320 llvm::SmallString<64> home_dir_path;
2321 llvm::sys::path::home_directory(home_dir_path);
2322 FileSpec profilePath(home_dir_path.c_str(), false);
Zachary Turner9e757b72014-07-28 16:45:05 +00002323 profilePath.AppendPathComponent(".lldbinit");
2324 std::string init_file_path = profilePath.GetPath();
Greg Clayton14a35512011-09-11 00:01:44 +00002325
2326 if (m_skip_app_init_files == false)
2327 {
Zachary Turnera21fee02014-08-21 21:49:24 +00002328 FileSpec program_file_spec(HostInfo::GetProgramFileSpec());
Greg Clayton14a35512011-09-11 00:01:44 +00002329 const char *program_name = program_file_spec.GetFilename().AsCString();
Jim Ingham16e0c682011-08-12 23:34:31 +00002330
Greg Clayton14a35512011-09-11 00:01:44 +00002331 if (program_name)
2332 {
2333 char program_init_file_name[PATH_MAX];
Zachary Turner9e757b72014-07-28 16:45:05 +00002334 ::snprintf (program_init_file_name, sizeof(program_init_file_name), "%s-%s", init_file_path.c_str(), program_name);
Greg Clayton14a35512011-09-11 00:01:44 +00002335 init_file.SetFile (program_init_file_name, true);
2336 if (!init_file.Exists())
2337 init_file.Clear();
2338 }
2339 }
2340
2341 if (!init_file && !m_skip_lldbinit_files)
Zachary Turner9e757b72014-07-28 16:45:05 +00002342 init_file.SetFile (init_file_path.c_str(), false);
Greg Clayton14a35512011-09-11 00:01:44 +00002343 }
2344
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002345 // If the file exists, tell HandleCommand to 'source' it; this will do the actual broadcasting
2346 // of the commands back to any appropriate listener (see CommandObjectSource::Execute for more details).
2347
2348 if (init_file.Exists())
2349 {
Greg Clayton44d93782014-01-27 23:43:24 +00002350 const bool saved_batch = SetBatchCommandMode (true);
Jim Ingham26c7bf92014-10-11 00:38:27 +00002351 CommandInterpreterRunOptions options;
2352 options.SetSilent (true);
2353 options.SetStopOnError (false);
2354 options.SetStopOnContinue (true);
2355
Greg Clayton340b0302014-02-05 17:57:57 +00002356 HandleCommandsFromFile (init_file,
Ed Masted78c9572014-04-20 00:31:37 +00002357 nullptr, // Execution context
Jim Ingham26c7bf92014-10-11 00:38:27 +00002358 options,
Greg Clayton340b0302014-02-05 17:57:57 +00002359 result);
Greg Clayton44d93782014-01-27 23:43:24 +00002360 SetBatchCommandMode (saved_batch);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002361 }
2362 else
2363 {
2364 // nothing to be done if the file doesn't exist
2365 result.SetStatus(eReturnStatusSuccessFinishNoResult);
2366 }
2367}
2368
Kate Stonea487aa42015-01-15 00:52:41 +00002369const char *
2370CommandInterpreter::GetCommandPrefix()
2371{
2372 const char * prefix = GetDebugger().GetIOHandlerCommandPrefix();
2373 return prefix == NULL ? "" : prefix;
2374}
2375
Greg Clayton8b82f082011-04-12 05:54:46 +00002376PlatformSP
2377CommandInterpreter::GetPlatform (bool prefer_target_platform)
2378{
2379 PlatformSP platform_sp;
Greg Claytonc14ee322011-09-22 04:58:26 +00002380 if (prefer_target_platform)
2381 {
Greg Clayton4e0fe8a2012-07-12 20:32:19 +00002382 ExecutionContext exe_ctx(GetExecutionContext());
2383 Target *target = exe_ctx.GetTargetPtr();
Greg Claytonc14ee322011-09-22 04:58:26 +00002384 if (target)
2385 platform_sp = target->GetPlatform();
2386 }
Greg Clayton8b82f082011-04-12 05:54:46 +00002387
2388 if (!platform_sp)
2389 platform_sp = m_debugger.GetPlatformList().GetSelectedPlatform();
2390 return platform_sp;
2391}
2392
Jim Inghame16c50a2011-02-18 00:54:25 +00002393void
Jim Inghambad87fe2011-03-11 01:51:49 +00002394CommandInterpreter::HandleCommands (const StringList &commands,
Jim Ingham26c7bf92014-10-11 00:38:27 +00002395 ExecutionContext *override_context,
2396 CommandInterpreterRunOptions &options,
Jim Inghame16c50a2011-02-18 00:54:25 +00002397 CommandReturnObject &result)
2398{
2399 size_t num_lines = commands.GetSize();
Jim Inghame16c50a2011-02-18 00:54:25 +00002400
2401 // If we are going to continue past a "continue" then we need to run the commands synchronously.
2402 // Make sure you reset this value anywhere you return from the function.
2403
2404 bool old_async_execution = m_debugger.GetAsyncExecution();
2405
2406 // If we've been given an execution context, set it at the start, but don't keep resetting it or we will
2407 // cause series of commands that change the context, then do an operation that relies on that context to fail.
2408
Ed Masted78c9572014-04-20 00:31:37 +00002409 if (override_context != nullptr)
Greg Clayton8b82f082011-04-12 05:54:46 +00002410 UpdateExecutionContext (override_context);
Jim Inghame16c50a2011-02-18 00:54:25 +00002411
Jim Ingham26c7bf92014-10-11 00:38:27 +00002412 if (!options.GetStopOnContinue())
Jim Inghame16c50a2011-02-18 00:54:25 +00002413 {
2414 m_debugger.SetAsyncExecution (false);
2415 }
2416
Andy Gibbsa297a972013-06-19 19:04:53 +00002417 for (size_t idx = 0; idx < num_lines; idx++)
Jim Inghame16c50a2011-02-18 00:54:25 +00002418 {
2419 const char *cmd = commands.GetStringAtIndex(idx);
2420 if (cmd[0] == '\0')
2421 continue;
2422
Jim Ingham26c7bf92014-10-11 00:38:27 +00002423 if (options.GetEchoCommands())
Jim Inghame16c50a2011-02-18 00:54:25 +00002424 {
2425 result.AppendMessageWithFormat ("%s %s\n",
Greg Clayton44d93782014-01-27 23:43:24 +00002426 m_debugger.GetPrompt(),
2427 cmd);
Jim Inghame16c50a2011-02-18 00:54:25 +00002428 }
2429
Greg Clayton9d0402b2011-02-20 02:15:07 +00002430 CommandReturnObject tmp_result;
Johnny Chen80fdd7c2011-10-05 00:42:59 +00002431 // If override_context is not NULL, pass no_context_switching = true for
2432 // HandleCommand() since we updated our context already.
Jim Ingham076b7fc2013-05-02 23:15:37 +00002433
2434 // We might call into a regex or alias command, in which case the add_to_history will get lost. This
2435 // m_command_source_depth dingus is the way we turn off adding to the history in that case, so set it up here.
Jim Ingham26c7bf92014-10-11 00:38:27 +00002436 if (!options.GetAddToHistory())
Jim Ingham076b7fc2013-05-02 23:15:37 +00002437 m_command_source_depth++;
Jim Ingham26c7bf92014-10-11 00:38:27 +00002438 bool success = HandleCommand(cmd, options.m_add_to_history, tmp_result,
Ed Masted78c9572014-04-20 00:31:37 +00002439 nullptr, /* override_context */
Johnny Chen80fdd7c2011-10-05 00:42:59 +00002440 true, /* repeat_on_empty_command */
Ed Masted78c9572014-04-20 00:31:37 +00002441 override_context != nullptr /* no_context_switching */);
Jim Ingham26c7bf92014-10-11 00:38:27 +00002442 if (!options.GetAddToHistory())
Jim Ingham076b7fc2013-05-02 23:15:37 +00002443 m_command_source_depth--;
Jim Inghame16c50a2011-02-18 00:54:25 +00002444
Jim Ingham26c7bf92014-10-11 00:38:27 +00002445 if (options.GetPrintResults())
Jim Inghame16c50a2011-02-18 00:54:25 +00002446 {
2447 if (tmp_result.Succeeded())
Jim Ingham85e8b812011-02-19 02:53:09 +00002448 result.AppendMessageWithFormat("%s", tmp_result.GetOutputData());
Jim Inghame16c50a2011-02-18 00:54:25 +00002449 }
2450
2451 if (!success || !tmp_result.Succeeded())
2452 {
Jim Inghama5038812012-04-24 02:25:07 +00002453 const char *error_msg = tmp_result.GetErrorData();
Ed Masted78c9572014-04-20 00:31:37 +00002454 if (error_msg == nullptr || error_msg[0] == '\0')
Jim Inghama5038812012-04-24 02:25:07 +00002455 error_msg = "<unknown error>.\n";
Jim Ingham26c7bf92014-10-11 00:38:27 +00002456 if (options.GetStopOnError())
Jim Inghame16c50a2011-02-18 00:54:25 +00002457 {
Deepak Panickal99fbc072014-03-03 15:39:47 +00002458 result.AppendErrorWithFormat("Aborting reading of commands after command #%" PRIu64 ": '%s' failed with %s",
2459 (uint64_t)idx, cmd, error_msg);
Jim Inghame16c50a2011-02-18 00:54:25 +00002460 result.SetStatus (eReturnStatusFailed);
2461 m_debugger.SetAsyncExecution (old_async_execution);
2462 return;
2463 }
Jim Ingham26c7bf92014-10-11 00:38:27 +00002464 else if (options.GetPrintResults())
Jim Inghame16c50a2011-02-18 00:54:25 +00002465 {
Deepak Panickal99fbc072014-03-03 15:39:47 +00002466 result.AppendMessageWithFormat ("Command #%" PRIu64 " '%s' failed with %s",
2467 (uint64_t)idx + 1,
Jim Inghame16c50a2011-02-18 00:54:25 +00002468 cmd,
Jim Inghama5038812012-04-24 02:25:07 +00002469 error_msg);
Jim Inghame16c50a2011-02-18 00:54:25 +00002470 }
2471 }
2472
Caroline Tice969ed3d2011-05-02 20:41:46 +00002473 if (result.GetImmediateOutputStream())
2474 result.GetImmediateOutputStream()->Flush();
2475
2476 if (result.GetImmediateErrorStream())
2477 result.GetImmediateErrorStream()->Flush();
2478
Jim Inghame16c50a2011-02-18 00:54:25 +00002479 // N.B. Can't depend on DidChangeProcessState, because the state coming into the command execution
2480 // could be running (for instance in Breakpoint Commands.
2481 // So we check the return value to see if it is has running in it.
2482 if ((tmp_result.GetStatus() == eReturnStatusSuccessContinuingNoResult)
2483 || (tmp_result.GetStatus() == eReturnStatusSuccessContinuingResult))
2484 {
Jim Ingham26c7bf92014-10-11 00:38:27 +00002485 if (options.GetStopOnContinue())
Jim Inghame16c50a2011-02-18 00:54:25 +00002486 {
2487 // If we caused the target to proceed, and we're going to stop in that case, set the
2488 // status in our real result before returning. This is an error if the continue was not the
2489 // last command in the set of commands to be run.
2490 if (idx != num_lines - 1)
Deepak Panickal99fbc072014-03-03 15:39:47 +00002491 result.AppendErrorWithFormat("Aborting reading of commands after command #%" PRIu64 ": '%s' continued the target.\n",
2492 (uint64_t)idx + 1, cmd);
Jim Inghame16c50a2011-02-18 00:54:25 +00002493 else
Deepak Panickal99fbc072014-03-03 15:39:47 +00002494 result.AppendMessageWithFormat("Command #%" PRIu64 " '%s' continued the target.\n", (uint64_t)idx + 1, cmd);
Jim Inghame16c50a2011-02-18 00:54:25 +00002495
2496 result.SetStatus(tmp_result.GetStatus());
2497 m_debugger.SetAsyncExecution (old_async_execution);
2498
2499 return;
2500 }
2501 }
Jim Inghamffc9f1d2014-10-14 01:20:07 +00002502
2503 // Also check for "stop on crash here:
2504 bool should_stop = false;
2505 if (tmp_result.GetDidChangeProcessState() && options.GetStopOnCrash())
2506 {
2507 TargetSP target_sp (m_debugger.GetTargetList().GetSelectedTarget());
2508 if (target_sp)
2509 {
2510 ProcessSP process_sp (target_sp->GetProcessSP());
2511 if (process_sp)
2512 {
2513 for (ThreadSP thread_sp : process_sp->GetThreadList().Threads())
2514 {
2515 StopReason reason = thread_sp->GetStopReason();
2516 if (reason == eStopReasonSignal || reason == eStopReasonException || reason == eStopReasonInstrumentation)
2517 {
2518 should_stop = true;
2519 break;
2520 }
2521 }
2522 }
2523 }
2524 if (should_stop)
2525 {
2526 if (idx != num_lines - 1)
2527 result.AppendErrorWithFormat("Aborting reading of commands after command #%" PRIu64 ": '%s' stopped with a signal or exception.\n",
2528 (uint64_t)idx + 1, cmd);
2529 else
2530 result.AppendMessageWithFormat("Command #%" PRIu64 " '%s' stopped with a signal or exception.\n", (uint64_t)idx + 1, cmd);
2531
2532 result.SetStatus(tmp_result.GetStatus());
2533 m_debugger.SetAsyncExecution (old_async_execution);
2534
2535 return;
2536 }
2537 }
Jim Inghame16c50a2011-02-18 00:54:25 +00002538
2539 }
2540
2541 result.SetStatus (eReturnStatusSuccessFinishResult);
2542 m_debugger.SetAsyncExecution (old_async_execution);
2543
2544 return;
2545}
2546
Greg Clayton340b0302014-02-05 17:57:57 +00002547// Make flags that we can pass into the IOHandler so our delegates can do the right thing
2548enum {
2549 eHandleCommandFlagStopOnContinue = (1u << 0),
2550 eHandleCommandFlagStopOnError = (1u << 1),
2551 eHandleCommandFlagEchoCommand = (1u << 2),
Jim Ingham26c7bf92014-10-11 00:38:27 +00002552 eHandleCommandFlagPrintResult = (1u << 3),
2553 eHandleCommandFlagStopOnCrash = (1u << 4)
Greg Clayton340b0302014-02-05 17:57:57 +00002554};
2555
Jim Inghame16c50a2011-02-18 00:54:25 +00002556void
2557CommandInterpreter::HandleCommandsFromFile (FileSpec &cmd_file,
2558 ExecutionContext *context,
Jim Ingham26c7bf92014-10-11 00:38:27 +00002559 CommandInterpreterRunOptions &options,
Jim Inghame16c50a2011-02-18 00:54:25 +00002560 CommandReturnObject &result)
2561{
2562 if (cmd_file.Exists())
2563 {
Greg Clayton44d93782014-01-27 23:43:24 +00002564 StreamFileSP input_file_sp (new StreamFile());
2565
2566 std::string cmd_file_path = cmd_file.GetPath();
2567 Error error = input_file_sp->GetFile().Open(cmd_file_path.c_str(), File::eOpenOptionRead);
2568
2569 if (error.Success())
2570 {
2571 Debugger &debugger = GetDebugger();
2572
Greg Clayton340b0302014-02-05 17:57:57 +00002573 uint32_t flags = 0;
2574
Jim Ingham26c7bf92014-10-11 00:38:27 +00002575 if (options.m_stop_on_continue == eLazyBoolCalculate)
Greg Clayton340b0302014-02-05 17:57:57 +00002576 {
2577 if (m_command_source_flags.empty())
2578 {
Greg Claytone4e462c2014-02-05 21:03:22 +00002579 // Stop on continue by default
Greg Clayton340b0302014-02-05 17:57:57 +00002580 flags |= eHandleCommandFlagStopOnContinue;
2581 }
2582 else if (m_command_source_flags.back() & eHandleCommandFlagStopOnContinue)
2583 {
2584 flags |= eHandleCommandFlagStopOnContinue;
2585 }
2586 }
Jim Ingham26c7bf92014-10-11 00:38:27 +00002587 else if (options.m_stop_on_continue == eLazyBoolYes)
Greg Clayton340b0302014-02-05 17:57:57 +00002588 {
2589 flags |= eHandleCommandFlagStopOnContinue;
2590 }
2591
Jim Ingham26c7bf92014-10-11 00:38:27 +00002592 if (options.m_stop_on_error == eLazyBoolCalculate)
Greg Clayton340b0302014-02-05 17:57:57 +00002593 {
2594 if (m_command_source_flags.empty())
2595 {
2596 if (GetStopCmdSourceOnError())
2597 flags |= eHandleCommandFlagStopOnError;
2598 }
2599 else if (m_command_source_flags.back() & eHandleCommandFlagStopOnError)
2600 {
2601 flags |= eHandleCommandFlagStopOnError;
2602 }
2603 }
Jim Ingham26c7bf92014-10-11 00:38:27 +00002604 else if (options.m_stop_on_error == eLazyBoolYes)
Greg Clayton340b0302014-02-05 17:57:57 +00002605 {
2606 flags |= eHandleCommandFlagStopOnError;
2607 }
2608
Jim Inghamffc9f1d2014-10-14 01:20:07 +00002609 if (options.GetStopOnCrash())
2610 {
2611 if (m_command_source_flags.empty())
2612 {
2613 // Echo command by default
2614 flags |= eHandleCommandFlagStopOnCrash;
2615 }
2616 else if (m_command_source_flags.back() & eHandleCommandFlagStopOnCrash)
2617 {
2618 flags |= eHandleCommandFlagStopOnCrash;
2619 }
2620 }
2621
Jim Ingham26c7bf92014-10-11 00:38:27 +00002622 if (options.m_echo_commands == eLazyBoolCalculate)
Greg Clayton340b0302014-02-05 17:57:57 +00002623 {
2624 if (m_command_source_flags.empty())
2625 {
2626 // Echo command by default
2627 flags |= eHandleCommandFlagEchoCommand;
2628 }
2629 else if (m_command_source_flags.back() & eHandleCommandFlagEchoCommand)
2630 {
2631 flags |= eHandleCommandFlagEchoCommand;
2632 }
2633 }
Jim Ingham26c7bf92014-10-11 00:38:27 +00002634 else if (options.m_echo_commands == eLazyBoolYes)
Greg Clayton340b0302014-02-05 17:57:57 +00002635 {
2636 flags |= eHandleCommandFlagEchoCommand;
2637 }
2638
Jim Ingham26c7bf92014-10-11 00:38:27 +00002639 if (options.m_print_results == eLazyBoolCalculate)
Greg Clayton340b0302014-02-05 17:57:57 +00002640 {
2641 if (m_command_source_flags.empty())
2642 {
Greg Claytone4e462c2014-02-05 21:03:22 +00002643 // Print output by default
2644 flags |= eHandleCommandFlagPrintResult;
Greg Clayton340b0302014-02-05 17:57:57 +00002645 }
Greg Claytone4e462c2014-02-05 21:03:22 +00002646 else if (m_command_source_flags.back() & eHandleCommandFlagPrintResult)
Greg Clayton340b0302014-02-05 17:57:57 +00002647 {
Greg Claytone4e462c2014-02-05 21:03:22 +00002648 flags |= eHandleCommandFlagPrintResult;
Greg Clayton340b0302014-02-05 17:57:57 +00002649 }
2650 }
Jim Ingham26c7bf92014-10-11 00:38:27 +00002651 else if (options.m_print_results == eLazyBoolYes)
Greg Clayton340b0302014-02-05 17:57:57 +00002652 {
Greg Claytone4e462c2014-02-05 21:03:22 +00002653 flags |= eHandleCommandFlagPrintResult;
2654 }
2655
2656 if (flags & eHandleCommandFlagPrintResult)
2657 {
Greg Clayton8ee67312014-02-05 21:46:20 +00002658 debugger.GetOutputFile()->Printf("Executing commands in '%s'.\n", cmd_file_path.c_str());
Greg Clayton340b0302014-02-05 17:57:57 +00002659 }
2660
2661 // Used for inheriting the right settings when "command source" might have
2662 // nested "command source" commands
Greg Claytone4e462c2014-02-05 21:03:22 +00002663 lldb::StreamFileSP empty_stream_sp;
Greg Clayton340b0302014-02-05 17:57:57 +00002664 m_command_source_flags.push_back(flags);
Greg Clayton44d93782014-01-27 23:43:24 +00002665 IOHandlerSP io_handler_sp (new IOHandlerEditline (debugger,
Kate Stonee30f11d2014-11-17 19:06:59 +00002666 IOHandler::Type::CommandInterpreter,
Greg Clayton44d93782014-01-27 23:43:24 +00002667 input_file_sp,
Greg Claytone4e462c2014-02-05 21:03:22 +00002668 empty_stream_sp, // Pass in an empty stream so we inherit the top input reader output stream
2669 empty_stream_sp, // Pass in an empty stream so we inherit the top input reader error stream
Greg Clayton340b0302014-02-05 17:57:57 +00002670 flags,
Ed Masted78c9572014-04-20 00:31:37 +00002671 nullptr, // Pass in NULL for "editline_name" so no history is saved, or written
Greg Clayton8ee67312014-02-05 21:46:20 +00002672 debugger.GetPrompt(),
Kate Stonee30f11d2014-11-17 19:06:59 +00002673 NULL,
Greg Clayton44d93782014-01-27 23:43:24 +00002674 false, // Not multi-line
Kate Stonee30f11d2014-11-17 19:06:59 +00002675 debugger.GetUseColor(),
Greg Claytonf6913cd2014-03-07 00:53:24 +00002676 0,
Greg Clayton44d93782014-01-27 23:43:24 +00002677 *this));
Greg Clayton8ee67312014-02-05 21:46:20 +00002678 const bool old_async_execution = debugger.GetAsyncExecution();
2679
Jim Inghamffc9f1d2014-10-14 01:20:07 +00002680 // Set synchronous execution if we are not stopping on continue
Greg Clayton8ee67312014-02-05 21:46:20 +00002681 if ((flags & eHandleCommandFlagStopOnContinue) == 0)
2682 debugger.SetAsyncExecution (false);
2683
Greg Clayton340b0302014-02-05 17:57:57 +00002684 m_command_source_depth++;
Greg Clayton8ee67312014-02-05 21:46:20 +00002685
2686 debugger.RunIOHandler(io_handler_sp);
Greg Clayton340b0302014-02-05 17:57:57 +00002687 if (!m_command_source_flags.empty())
2688 m_command_source_flags.pop_back();
2689 m_command_source_depth--;
Greg Clayton44d93782014-01-27 23:43:24 +00002690 result.SetStatus (eReturnStatusSuccessFinishNoResult);
Greg Clayton8ee67312014-02-05 21:46:20 +00002691 debugger.SetAsyncExecution (old_async_execution);
Greg Clayton44d93782014-01-27 23:43:24 +00002692 }
2693 else
2694 {
2695 result.AppendErrorWithFormat ("error: an error occurred read file '%s': %s\n", cmd_file_path.c_str(), error.AsCString());
2696 result.SetStatus (eReturnStatusFailed);
2697 }
Greg Clayton8ee67312014-02-05 21:46:20 +00002698
2699
Jim Inghame16c50a2011-02-18 00:54:25 +00002700 }
2701 else
2702 {
2703 result.AppendErrorWithFormat ("Error reading commands from file %s - file not found.\n",
Jim Ingham4af59612014-12-19 19:20:44 +00002704 cmd_file.GetFilename().AsCString("<Unknown>"));
Jim Inghame16c50a2011-02-18 00:54:25 +00002705 result.SetStatus (eReturnStatusFailed);
2706 return;
2707 }
2708}
2709
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002710ScriptInterpreter *
Enrico Granatab5887262012-10-29 21:18:03 +00002711CommandInterpreter::GetScriptInterpreter (bool can_create)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002712{
Ed Masted78c9572014-04-20 00:31:37 +00002713 if (m_script_interpreter_ap.get() != nullptr)
Enrico Granatab5887262012-10-29 21:18:03 +00002714 return m_script_interpreter_ap.get();
2715
2716 if (!can_create)
Ed Masted78c9572014-04-20 00:31:37 +00002717 return nullptr;
Enrico Granatab5887262012-10-29 21:18:03 +00002718
Enrico Granataa29bdad2012-07-10 18:23:48 +00002719 // <rdar://problem/11751427>
2720 // we need to protect the initialization of the script interpreter
2721 // otherwise we could end up with two threads both trying to create
2722 // their instance of it, and for some languages (e.g. Python)
2723 // this is a bulletproof recipe for disaster!
2724 // this needs to be a function-level static because multiple Debugger instances living in the same process
2725 // still need to be isolated and not try to initialize Python concurrently
Enrico Granata8b95df22012-07-10 19:04:14 +00002726 static Mutex g_interpreter_mutex(Mutex::eMutexTypeRecursive);
2727 Mutex::Locker interpreter_lock(g_interpreter_mutex);
Enrico Granataa29bdad2012-07-10 18:23:48 +00002728
Greg Clayton5160ce52013-03-27 23:08:40 +00002729 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Enrico Granatab5887262012-10-29 21:18:03 +00002730 if (log)
2731 log->Printf("Initializing the ScriptInterpreter now\n");
Greg Clayton66111032010-06-23 01:19:29 +00002732
Caroline Tice2f88aad2011-01-14 00:29:16 +00002733 lldb::ScriptLanguage script_lang = GetDebugger().GetScriptLanguage();
2734 switch (script_lang)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002735 {
Greg Claytondce502e2011-11-04 03:34:56 +00002736 case eScriptLanguagePython:
2737#ifndef LLDB_DISABLE_PYTHON
2738 m_script_interpreter_ap.reset (new ScriptInterpreterPython (*this));
2739 break;
2740#else
2741 // Fall through to the None case when python is disabled
2742#endif
Caroline Tice2f88aad2011-01-14 00:29:16 +00002743 case eScriptLanguageNone:
2744 m_script_interpreter_ap.reset (new ScriptInterpreterNone (*this));
2745 break;
Caroline Tice2f88aad2011-01-14 00:29:16 +00002746 };
2747
2748 return m_script_interpreter_ap.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002749}
2750
2751
2752
2753bool
2754CommandInterpreter::GetSynchronous ()
2755{
2756 return m_synchronous_execution;
2757}
2758
2759void
2760CommandInterpreter::SetSynchronous (bool value)
2761{
Johnny Chenc066ab42010-10-14 01:22:03 +00002762 m_synchronous_execution = value;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002763}
2764
2765void
2766CommandInterpreter::OutputFormattedHelpText (Stream &strm,
Kate Stonea487aa42015-01-15 00:52:41 +00002767 const char *prefix,
2768 const char *help_text)
2769{
2770 const uint32_t max_columns = m_debugger.GetTerminalWidth();
2771 if (prefix == NULL)
2772 prefix = "";
2773
2774 size_t prefix_width = strlen(prefix);
2775 size_t line_width_max = max_columns - prefix_width;
2776 const char *help_text_end = help_text + strlen(help_text);
2777 const char *line_start = help_text;
2778 if (line_width_max < 16)
2779 line_width_max = help_text_end - help_text + prefix_width;
2780
2781 strm.IndentMore (prefix_width);
2782 while (line_start < help_text_end)
2783 {
2784 // Break each line at the first newline or last space/tab before
2785 // the maximum number of characters that fit on a line. Lines with no
2786 // natural break are left unbroken to wrap.
2787 const char *line_end = help_text_end;
2788 const char *line_scan = line_start;
2789 const char *line_scan_end = help_text_end;
2790 while (line_scan < line_scan_end)
2791 {
2792 char next = *line_scan;
2793 if (next == '\t' || next == ' ')
2794 {
2795 line_end = line_scan;
2796 line_scan_end = line_start + line_width_max;
2797 }
2798 else if (next == '\n' || next == '\0')
2799 {
2800 line_end = line_scan;
2801 break;
2802 }
2803 ++line_scan;
2804 }
2805
2806 // Prefix the first line, indent subsequent lines to line up
2807 if (line_start == help_text)
2808 strm.Write (prefix, prefix_width);
2809 else
2810 strm.Indent();
2811 strm.Write (line_start, line_end - line_start);
2812 strm.EOL();
2813
2814 // When a line breaks at whitespace consume it before continuing
2815 line_start = line_end;
2816 char next = *line_start;
2817 if (next == '\n')
2818 ++line_start;
2819 else while (next == ' ' || next == '\t')
2820 next = *(++line_start);
2821 }
2822 strm.IndentLess (prefix_width);
2823}
2824
2825void
2826CommandInterpreter::OutputFormattedHelpText (Stream &strm,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002827 const char *word_text,
2828 const char *separator,
2829 const char *help_text,
Greg Claytonc7bece562013-01-25 18:06:21 +00002830 size_t max_word_len)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002831{
Kate Stonea487aa42015-01-15 00:52:41 +00002832 StreamString prefix_stream;
2833 prefix_stream.Printf (" %-*s %s ", (int)max_word_len, word_text, separator);
2834 OutputFormattedHelpText (strm, prefix_stream.GetData(), help_text);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002835}
2836
2837void
Enrico Granata82a7d982011-07-07 00:38:40 +00002838CommandInterpreter::OutputHelpText (Stream &strm,
2839 const char *word_text,
2840 const char *separator,
2841 const char *help_text,
2842 uint32_t max_word_len)
2843{
2844 int indent_size = max_word_len + strlen (separator) + 2;
2845
2846 strm.IndentMore (indent_size);
2847
2848 StreamString text_strm;
2849 text_strm.Printf ("%-*s %s %s", max_word_len, word_text, separator, help_text);
2850
2851 const uint32_t max_columns = m_debugger.GetTerminalWidth();
Enrico Granata82a7d982011-07-07 00:38:40 +00002852
2853 size_t len = text_strm.GetSize();
2854 const char *text = text_strm.GetData();
2855
2856 uint32_t chars_left = max_columns;
2857
2858 for (uint32_t i = 0; i < len; i++)
2859 {
2860 if ((text[i] == ' ' && ::strchr((text+i+1), ' ') && chars_left < ::strchr((text+i+1), ' ')-(text+i)) || text[i] == '\n')
2861 {
Enrico Granata82a7d982011-07-07 00:38:40 +00002862 chars_left = max_columns - indent_size;
2863 strm.EOL();
2864 strm.Indent();
2865 }
2866 else
2867 {
2868 strm.PutChar(text[i]);
2869 chars_left--;
2870 }
2871
2872 }
2873
2874 strm.EOL();
2875 strm.IndentLess(indent_size);
2876}
2877
2878void
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002879CommandInterpreter::FindCommandsForApropos (const char *search_word, StringList &commands_found,
Jim Inghamaf3753e2013-05-17 01:30:37 +00002880 StringList &commands_help, bool search_builtin_commands, bool search_user_commands)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002881{
2882 CommandObject::CommandMap::const_iterator pos;
2883
Jim Inghamaf3753e2013-05-17 01:30:37 +00002884 if (search_builtin_commands)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002885 {
Jim Inghamaf3753e2013-05-17 01:30:37 +00002886 for (pos = m_command_dict.begin(); pos != m_command_dict.end(); ++pos)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002887 {
Jim Inghamaf3753e2013-05-17 01:30:37 +00002888 const char *command_name = pos->first.c_str();
2889 CommandObject *cmd_obj = pos->second.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002890
Jim Inghamaf3753e2013-05-17 01:30:37 +00002891 if (cmd_obj->HelpTextContainsWord (search_word))
2892 {
2893 commands_found.AppendString (command_name);
2894 commands_help.AppendString (cmd_obj->GetHelp());
2895 }
2896
2897 if (cmd_obj->IsMultiwordObject())
2898 cmd_obj->AproposAllSubCommands (command_name,
2899 search_word,
2900 commands_found,
2901 commands_help);
2902
2903 }
2904 }
2905
2906 if (search_user_commands)
2907 {
2908 for (pos = m_user_dict.begin(); pos != m_user_dict.end(); ++pos)
2909 {
2910 const char *command_name = pos->first.c_str();
2911 CommandObject *cmd_obj = pos->second.get();
2912
2913 if (cmd_obj->HelpTextContainsWord (search_word))
2914 {
2915 commands_found.AppendString (command_name);
2916 commands_help.AppendString (cmd_obj->GetHelp());
2917 }
2918
2919 if (cmd_obj->IsMultiwordObject())
2920 cmd_obj->AproposAllSubCommands (command_name,
2921 search_word,
2922 commands_found,
2923 commands_help);
2924
2925 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002926 }
2927}
Greg Clayton8b82f082011-04-12 05:54:46 +00002928
Greg Clayton8b82f082011-04-12 05:54:46 +00002929void
2930CommandInterpreter::UpdateExecutionContext (ExecutionContext *override_context)
2931{
Ed Masted78c9572014-04-20 00:31:37 +00002932 if (override_context != nullptr)
Greg Clayton8b82f082011-04-12 05:54:46 +00002933 {
Greg Clayton4e0fe8a2012-07-12 20:32:19 +00002934 m_exe_ctx_ref = *override_context;
Greg Clayton8b82f082011-04-12 05:54:46 +00002935 }
2936 else
2937 {
Greg Clayton4e0fe8a2012-07-12 20:32:19 +00002938 const bool adopt_selected = true;
2939 m_exe_ctx_ref.SetTargetPtr (m_debugger.GetSelectedTarget().get(), adopt_selected);
Greg Clayton8b82f082011-04-12 05:54:46 +00002940 }
2941}
Greg Clayton44d93782014-01-27 23:43:24 +00002942
2943
2944size_t
2945CommandInterpreter::GetProcessOutput ()
2946{
2947 // The process has stuff waiting for stderr; get it and write it out to the appropriate place.
2948 char stdio_buffer[1024];
2949 size_t len;
2950 size_t total_bytes = 0;
2951 Error error;
2952 TargetSP target_sp (m_debugger.GetTargetList().GetSelectedTarget());
2953 if (target_sp)
2954 {
2955 ProcessSP process_sp (target_sp->GetProcessSP());
2956 if (process_sp)
2957 {
2958 while ((len = process_sp->GetSTDOUT (stdio_buffer, sizeof (stdio_buffer), error)) > 0)
2959 {
2960 size_t bytes_written = len;
2961 m_debugger.GetOutputFile()->Write (stdio_buffer, bytes_written);
2962 total_bytes += len;
2963 }
2964 while ((len = process_sp->GetSTDERR (stdio_buffer, sizeof (stdio_buffer), error)) > 0)
2965 {
2966 size_t bytes_written = len;
2967 m_debugger.GetErrorFile()->Write (stdio_buffer, bytes_written);
2968 total_bytes += len;
2969 }
2970 }
2971 }
2972 return total_bytes;
2973}
2974
2975void
2976CommandInterpreter::IOHandlerInputComplete (IOHandler &io_handler, std::string &line)
2977{
Greg Clayton340b0302014-02-05 17:57:57 +00002978 const bool is_interactive = io_handler.GetIsInteractive();
2979 if (is_interactive == false)
2980 {
2981 // When we are not interactive, don't execute blank lines. This will happen
2982 // sourcing a commands file. We don't want blank lines to repeat the previous
2983 // command and cause any errors to occur (like redefining an alias, get an error
2984 // and stop parsing the commands file).
2985 if (line.empty())
2986 return;
2987
2988 // When using a non-interactive file handle (like when sourcing commands from a file)
2989 // we need to echo the command out so we don't just see the command output and no
2990 // command...
2991 if (io_handler.GetFlags().Test(eHandleCommandFlagEchoCommand))
2992 io_handler.GetOutputStreamFile()->Printf("%s%s\n", io_handler.GetPrompt(), line.c_str());
2993 }
2994
Greg Clayton44d93782014-01-27 23:43:24 +00002995 lldb_private::CommandReturnObject result;
2996 HandleCommand(line.c_str(), eLazyBoolCalculate, result);
2997
Greg Clayton44d93782014-01-27 23:43:24 +00002998 // Now emit the command output text from the command we just executed
Greg Clayton340b0302014-02-05 17:57:57 +00002999 if (io_handler.GetFlags().Test(eHandleCommandFlagPrintResult))
3000 {
3001 // Display any STDOUT/STDERR _prior_ to emitting the command result text
3002 GetProcessOutput ();
3003
Greg Clayton8ee67312014-02-05 21:46:20 +00003004 if (!result.GetImmediateOutputStream())
3005 {
3006 const char *output = result.GetOutputData();
3007 if (output && output[0])
3008 io_handler.GetOutputStreamFile()->PutCString(output);
3009 }
Greg Clayton44d93782014-01-27 23:43:24 +00003010
Greg Clayton340b0302014-02-05 17:57:57 +00003011 // Now emit the command error text from the command we just executed
Greg Clayton8ee67312014-02-05 21:46:20 +00003012 if (!result.GetImmediateErrorStream())
3013 {
3014 const char *error = result.GetErrorData();
3015 if (error && error[0])
3016 io_handler.GetErrorStreamFile()->PutCString(error);
3017 }
Greg Clayton340b0302014-02-05 17:57:57 +00003018 }
Greg Clayton44d93782014-01-27 23:43:24 +00003019
3020 switch (result.GetStatus())
3021 {
3022 case eReturnStatusInvalid:
3023 case eReturnStatusSuccessFinishNoResult:
3024 case eReturnStatusSuccessFinishResult:
Greg Clayton340b0302014-02-05 17:57:57 +00003025 case eReturnStatusStarted:
3026 break;
3027
Greg Clayton44d93782014-01-27 23:43:24 +00003028 case eReturnStatusSuccessContinuingNoResult:
3029 case eReturnStatusSuccessContinuingResult:
Greg Clayton340b0302014-02-05 17:57:57 +00003030 if (io_handler.GetFlags().Test(eHandleCommandFlagStopOnContinue))
3031 io_handler.SetIsDone(true);
3032 break;
3033
Greg Clayton44d93782014-01-27 23:43:24 +00003034 case eReturnStatusFailed:
Jim Ingham26c7bf92014-10-11 00:38:27 +00003035 m_num_errors++;
Greg Clayton340b0302014-02-05 17:57:57 +00003036 if (io_handler.GetFlags().Test(eHandleCommandFlagStopOnError))
3037 io_handler.SetIsDone(true);
Greg Clayton44d93782014-01-27 23:43:24 +00003038 break;
3039
3040 case eReturnStatusQuit:
Jim Ingham26c7bf92014-10-11 00:38:27 +00003041 m_quit_requested = true;
Greg Clayton44d93782014-01-27 23:43:24 +00003042 io_handler.SetIsDone(true);
3043 break;
3044 }
Jim Inghamffc9f1d2014-10-14 01:20:07 +00003045
3046 // Finally, if we're going to stop on crash, check that here:
3047 if (!m_quit_requested
3048 && result.GetDidChangeProcessState()
3049 && io_handler.GetFlags().Test(eHandleCommandFlagStopOnCrash))
3050 {
3051 bool should_stop = false;
3052 TargetSP target_sp (m_debugger.GetTargetList().GetSelectedTarget());
3053 if (target_sp)
3054 {
3055 ProcessSP process_sp (target_sp->GetProcessSP());
3056 if (process_sp)
3057 {
3058 for (ThreadSP thread_sp : process_sp->GetThreadList().Threads())
3059 {
3060 StopReason reason = thread_sp->GetStopReason();
3061 if (reason == eStopReasonSignal || reason == eStopReasonException || reason == eStopReasonInstrumentation)
3062 {
Jim Inghamffc9f1d2014-10-14 01:20:07 +00003063 should_stop = true;
3064 break;
3065 }
3066 }
3067 }
3068 }
3069 if (should_stop)
3070 {
3071 io_handler.SetIsDone(true);
3072 m_stopped_for_crash = true;
3073 }
3074 }
Greg Clayton44d93782014-01-27 23:43:24 +00003075}
3076
Greg Claytonf0066ad2014-05-02 00:45:31 +00003077bool
3078CommandInterpreter::IOHandlerInterrupt (IOHandler &io_handler)
3079{
3080 ExecutionContext exe_ctx (GetExecutionContext());
3081 Process *process = exe_ctx.GetProcessPtr();
3082
3083 if (process)
3084 {
3085 StateType state = process->GetState();
3086 if (StateIsRunningState(state))
3087 {
3088 process->Halt();
3089 return true; // Don't do any updating when we are running
3090 }
3091 }
Greg Claytone507bce2015-01-27 01:58:22 +00003092
3093 ScriptInterpreter *script_interpreter = GetScriptInterpreter (false);
3094 if (script_interpreter)
3095 {
3096 if (script_interpreter->Interrupt())
3097 return true;
3098 }
Greg Claytonf0066ad2014-05-02 00:45:31 +00003099 return false;
3100}
3101
Greg Clayton44d93782014-01-27 23:43:24 +00003102void
3103CommandInterpreter::GetLLDBCommandsFromIOHandler (const char *prompt,
3104 IOHandlerDelegate &delegate,
3105 bool asynchronously,
3106 void *baton)
3107{
3108 Debugger &debugger = GetDebugger();
3109 IOHandlerSP io_handler_sp (new IOHandlerEditline (debugger,
Kate Stonee30f11d2014-11-17 19:06:59 +00003110 IOHandler::Type::CommandList,
Greg Clayton44d93782014-01-27 23:43:24 +00003111 "lldb", // Name of input reader for history
3112 prompt, // Prompt
Kate Stonee30f11d2014-11-17 19:06:59 +00003113 NULL, // Continuation prompt
Greg Clayton44d93782014-01-27 23:43:24 +00003114 true, // Get multiple lines
Kate Stonee30f11d2014-11-17 19:06:59 +00003115 debugger.GetUseColor(),
Greg Claytonf6913cd2014-03-07 00:53:24 +00003116 0, // Don't show line numbers
Greg Clayton44d93782014-01-27 23:43:24 +00003117 delegate)); // IOHandlerDelegate
3118
3119 if (io_handler_sp)
3120 {
3121 io_handler_sp->SetUserData (baton);
3122 if (asynchronously)
3123 debugger.PushIOHandler(io_handler_sp);
3124 else
3125 debugger.RunIOHandler(io_handler_sp);
3126 }
3127
3128}
3129
3130
3131void
3132CommandInterpreter::GetPythonCommandsFromIOHandler (const char *prompt,
3133 IOHandlerDelegate &delegate,
3134 bool asynchronously,
3135 void *baton)
3136{
3137 Debugger &debugger = GetDebugger();
3138 IOHandlerSP io_handler_sp (new IOHandlerEditline (debugger,
Kate Stonee30f11d2014-11-17 19:06:59 +00003139 IOHandler::Type::PythonCode,
Greg Clayton44d93782014-01-27 23:43:24 +00003140 "lldb-python", // Name of input reader for history
3141 prompt, // Prompt
Kate Stonee30f11d2014-11-17 19:06:59 +00003142 NULL, // Continuation prompt
Greg Clayton44d93782014-01-27 23:43:24 +00003143 true, // Get multiple lines
Kate Stonee30f11d2014-11-17 19:06:59 +00003144 debugger.GetUseColor(),
Greg Claytonf6913cd2014-03-07 00:53:24 +00003145 0, // Don't show line numbers
Greg Clayton44d93782014-01-27 23:43:24 +00003146 delegate)); // IOHandlerDelegate
3147
3148 if (io_handler_sp)
3149 {
3150 io_handler_sp->SetUserData (baton);
3151 if (asynchronously)
3152 debugger.PushIOHandler(io_handler_sp);
3153 else
3154 debugger.RunIOHandler(io_handler_sp);
3155 }
3156
3157}
3158
3159bool
3160CommandInterpreter::IsActive ()
3161{
3162 return m_debugger.IsTopIOHandler (m_command_io_handler_sp);
3163}
3164
Kate Stonee30f11d2014-11-17 19:06:59 +00003165lldb::IOHandlerSP
3166CommandInterpreter::GetIOHandler(bool force_create, CommandInterpreterRunOptions *options)
3167{
3168 // Always re-create the IOHandlerEditline in case the input
3169 // changed. The old instance might have had a non-interactive
3170 // input and now it does or vice versa.
3171 if (force_create || !m_command_io_handler_sp)
3172 {
3173 // Always re-create the IOHandlerEditline in case the input
3174 // changed. The old instance might have had a non-interactive
3175 // input and now it does or vice versa.
3176 uint32_t flags = 0;
3177
3178 if (options)
3179 {
3180 if (options->m_stop_on_continue == eLazyBoolYes)
3181 flags |= eHandleCommandFlagStopOnContinue;
3182 if (options->m_stop_on_error == eLazyBoolYes)
3183 flags |= eHandleCommandFlagStopOnError;
3184 if (options->m_stop_on_crash == eLazyBoolYes)
3185 flags |= eHandleCommandFlagStopOnCrash;
3186 if (options->m_echo_commands != eLazyBoolNo)
3187 flags |= eHandleCommandFlagEchoCommand;
3188 if (options->m_print_results != eLazyBoolNo)
3189 flags |= eHandleCommandFlagPrintResult;
3190 }
3191 else
3192 {
3193 flags = eHandleCommandFlagEchoCommand | eHandleCommandFlagPrintResult;
3194 }
3195
3196 m_command_io_handler_sp.reset(new IOHandlerEditline (m_debugger,
3197 IOHandler::Type::CommandInterpreter,
3198 m_debugger.GetInputFile(),
3199 m_debugger.GetOutputFile(),
3200 m_debugger.GetErrorFile(),
3201 flags,
3202 "lldb",
3203 m_debugger.GetPrompt(),
3204 NULL, // Continuation prompt
3205 false, // Don't enable multiple line input, just single line commands
3206 m_debugger.GetUseColor(),
3207 0, // Don't show line numbers
3208 *this));
3209 }
3210 return m_command_io_handler_sp;
3211}
3212
Greg Clayton44d93782014-01-27 23:43:24 +00003213void
3214CommandInterpreter::RunCommandInterpreter(bool auto_handle_events,
Jim Ingham26c7bf92014-10-11 00:38:27 +00003215 bool spawn_thread,
3216 CommandInterpreterRunOptions &options)
Greg Clayton44d93782014-01-27 23:43:24 +00003217{
Bruce Mitchener58ef3912015-06-18 05:27:05 +00003218 // Always re-create the command interpreter when we run it in case
Kate Stonee30f11d2014-11-17 19:06:59 +00003219 // any file handles have changed.
3220 bool force_create = true;
3221 m_debugger.PushIOHandler(GetIOHandler(force_create, &options));
Jim Inghamffc9f1d2014-10-14 01:20:07 +00003222 m_stopped_for_crash = false;
Greg Clayton380f3d82014-07-31 19:46:19 +00003223
Greg Clayton44d93782014-01-27 23:43:24 +00003224 if (auto_handle_events)
3225 m_debugger.StartEventHandlerThread();
3226
3227 if (spawn_thread)
3228 {
3229 m_debugger.StartIOHandlerThread();
3230 }
3231 else
3232 {
Siva Chandra9aaab552015-02-26 19:26:36 +00003233 m_debugger.ExecuteIOHandlers();
Kate Stonee30f11d2014-11-17 19:06:59 +00003234
Greg Clayton44d93782014-01-27 23:43:24 +00003235 if (auto_handle_events)
3236 m_debugger.StopEventHandlerThread();
3237 }
Kate Stonee30f11d2014-11-17 19:06:59 +00003238
Greg Clayton44d93782014-01-27 23:43:24 +00003239}
3240
Adrian McCarthy2304b6f2015-04-23 20:00:25 +00003241CommandObject *
3242CommandInterpreter::ResolveCommandImpl(std::string &command_line, CommandReturnObject &result)
3243{
3244 std::string scratch_command(command_line); // working copy so we don't modify command_line unless we succeed
3245 CommandObject *cmd_obj = nullptr;
3246 StreamString revised_command_line;
3247 bool wants_raw_input = false;
3248 size_t actual_cmd_name_len = 0;
3249 std::string next_word;
3250 StringList matches;
3251 bool done = false;
3252 while (!done)
3253 {
3254 char quote_char = '\0';
3255 std::string suffix;
3256 ExtractCommand(scratch_command, next_word, suffix, quote_char);
3257 if (cmd_obj == nullptr)
3258 {
3259 std::string full_name;
3260 if (GetAliasFullName(next_word.c_str(), full_name))
3261 {
3262 std::string alias_result;
3263 cmd_obj = BuildAliasResult(full_name.c_str(), scratch_command, alias_result, result);
3264 revised_command_line.Printf("%s", alias_result.c_str());
3265 if (cmd_obj)
3266 {
3267 wants_raw_input = cmd_obj->WantsRawCommandString();
3268 actual_cmd_name_len = strlen(cmd_obj->GetCommandName());
3269 }
3270 }
3271 else
3272 {
3273 cmd_obj = GetCommandObject(next_word.c_str(), &matches);
3274 if (cmd_obj)
3275 {
3276 actual_cmd_name_len += strlen(cmd_obj->GetCommandName());
3277 revised_command_line.Printf("%s", cmd_obj->GetCommandName());
3278 wants_raw_input = cmd_obj->WantsRawCommandString();
3279 }
3280 else
3281 {
3282 revised_command_line.Printf ("%s", next_word.c_str());
3283 }
3284 }
3285 }
3286 else
3287 {
3288 if (cmd_obj->IsMultiwordObject ())
3289 {
3290 CommandObject *sub_cmd_obj = cmd_obj->GetSubcommandObject(next_word.c_str());
3291 if (sub_cmd_obj)
3292 {
3293 // The subcommand's name includes the parent command's name,
3294 // so restart rather than append to the revised_command_line.
3295 actual_cmd_name_len = strlen(sub_cmd_obj->GetCommandName()) + 1;
3296 revised_command_line.Clear();
3297 revised_command_line.Printf("%s", sub_cmd_obj->GetCommandName());
3298 cmd_obj = sub_cmd_obj;
3299 wants_raw_input = cmd_obj->WantsRawCommandString();
3300 }
3301 else
3302 {
3303 if (quote_char)
3304 revised_command_line.Printf(" %c%s%s%c", quote_char, next_word.c_str(), suffix.c_str(), quote_char);
3305 else
3306 revised_command_line.Printf(" %s%s", next_word.c_str(), suffix.c_str());
3307 done = true;
3308 }
3309 }
3310 else
3311 {
3312 if (quote_char)
3313 revised_command_line.Printf(" %c%s%s%c", quote_char, next_word.c_str(), suffix.c_str(), quote_char);
3314 else
3315 revised_command_line.Printf(" %s%s", next_word.c_str(), suffix.c_str());
3316 done = true;
3317 }
3318 }
3319
3320 if (cmd_obj == nullptr)
3321 {
3322 const size_t num_matches = matches.GetSize();
3323 if (matches.GetSize() > 1) {
3324 StreamString error_msg;
3325 error_msg.Printf("Ambiguous command '%s'. Possible matches:\n", next_word.c_str());
3326
3327 for (uint32_t i = 0; i < num_matches; ++i) {
3328 error_msg.Printf("\t%s\n", matches.GetStringAtIndex(i));
3329 }
3330 result.AppendRawError(error_msg.GetString().c_str());
3331 } else {
3332 // We didn't have only one match, otherwise we wouldn't get here.
3333 assert(num_matches == 0);
3334 result.AppendErrorWithFormat("'%s' is not a valid command.\n", next_word.c_str());
3335 }
3336 result.SetStatus(eReturnStatusFailed);
3337 return nullptr;
3338 }
3339
3340 if (cmd_obj->IsMultiwordObject())
3341 {
3342 if (!suffix.empty())
3343 {
3344 result.AppendErrorWithFormat("command '%s' did not recognize '%s%s%s' as valid (subcommand might be invalid).\n",
3345 cmd_obj->GetCommandName(),
3346 next_word.empty() ? "" : next_word.c_str(),
3347 next_word.empty() ? " -- " : " ",
3348 suffix.c_str());
3349 result.SetStatus(eReturnStatusFailed);
3350 return nullptr;
3351 }
3352 }
3353 else
3354 {
3355 // If we found a normal command, we are done
3356 done = true;
3357 if (!suffix.empty())
3358 {
3359 switch (suffix[0])
3360 {
3361 case '/':
3362 // GDB format suffixes
3363 {
3364 Options *command_options = cmd_obj->GetOptions();
3365 if (command_options && command_options->SupportsLongOption("gdb-format"))
3366 {
3367 std::string gdb_format_option("--gdb-format=");
3368 gdb_format_option += (suffix.c_str() + 1);
3369
3370 bool inserted = false;
3371 std::string &cmd = revised_command_line.GetString();
3372 size_t arg_terminator_idx = FindArgumentTerminator(cmd);
3373 if (arg_terminator_idx != std::string::npos)
3374 {
3375 // Insert the gdb format option before the "--" that terminates options
3376 gdb_format_option.append(1,' ');
3377 cmd.insert(arg_terminator_idx, gdb_format_option);
3378 inserted = true;
3379 }
3380
3381 if (!inserted)
3382 revised_command_line.Printf(" %s", gdb_format_option.c_str());
3383
3384 if (wants_raw_input && FindArgumentTerminator(cmd) == std::string::npos)
3385 revised_command_line.PutCString(" --");
3386 }
3387 else
3388 {
3389 result.AppendErrorWithFormat("the '%s' command doesn't support the --gdb-format option\n",
3390 cmd_obj->GetCommandName());
3391 result.SetStatus(eReturnStatusFailed);
3392 return nullptr;
3393 }
3394 }
3395 break;
3396
3397 default:
3398 result.AppendErrorWithFormat("unknown command shorthand suffix: '%s'\n",
3399 suffix.c_str());
3400 result.SetStatus(eReturnStatusFailed);
3401 return nullptr;
3402 }
3403 }
3404 }
3405 if (scratch_command.empty())
3406 done = true;
3407 }
3408
3409 if (!scratch_command.empty())
3410 revised_command_line.Printf(" %s", scratch_command.c_str());
3411
3412 if (cmd_obj != NULL)
3413 command_line = revised_command_line.GetData();
3414
3415 return cmd_obj;
3416}