blob: b828f40379741fecad46c34e62bcdfce34122414 [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
Daniel Malea93a64302012-12-05 00:20:57 +000010#include "lldb/lldb-python.h"
11
Chris Lattner30fdc8d2010-06-08 16:52:24 +000012#include <string>
Caroline Tice4ab31c92010-10-12 21:57:09 +000013#include <vector>
Chris Lattner30fdc8d2010-06-08 16:52:24 +000014#include <stdlib.h>
15
Greg Clayton4a33d312011-06-23 17:59:56 +000016#include "CommandObjectScript.h"
Peter Collingbourne08405b62011-06-23 20:37:26 +000017#include "lldb/Interpreter/CommandObjectRegexCommand.h"
Greg Clayton4a33d312011-06-23 17:59:56 +000018
Eli Friedman3afb70c2010-06-13 02:17:17 +000019#include "../Commands/CommandObjectApropos.h"
20#include "../Commands/CommandObjectArgs.h"
21#include "../Commands/CommandObjectBreakpoint.h"
Eli Friedman3afb70c2010-06-13 02:17:17 +000022#include "../Commands/CommandObjectDisassemble.h"
23#include "../Commands/CommandObjectExpression.h"
Eli Friedman3afb70c2010-06-13 02:17:17 +000024#include "../Commands/CommandObjectFrame.h"
Greg Clayton44d93782014-01-27 23:43:24 +000025#include "../Commands/CommandObjectGUI.h"
Eli Friedman3afb70c2010-06-13 02:17:17 +000026#include "../Commands/CommandObjectHelp.h"
Eli Friedman3afb70c2010-06-13 02:17:17 +000027#include "../Commands/CommandObjectLog.h"
28#include "../Commands/CommandObjectMemory.h"
Greg Claytonded470d2011-03-19 01:12:21 +000029#include "../Commands/CommandObjectPlatform.h"
Enrico Granata21dfcd92012-09-28 23:57:51 +000030#include "../Commands/CommandObjectPlugin.h"
Eli Friedman3afb70c2010-06-13 02:17:17 +000031#include "../Commands/CommandObjectProcess.h"
32#include "../Commands/CommandObjectQuit.h"
Eli Friedman3afb70c2010-06-13 02:17:17 +000033#include "../Commands/CommandObjectRegister.h"
Eli Friedman3afb70c2010-06-13 02:17:17 +000034#include "../Commands/CommandObjectSettings.h"
Eli Friedman3afb70c2010-06-13 02:17:17 +000035#include "../Commands/CommandObjectSource.h"
Jim Inghamebc09c32010-07-07 03:36:20 +000036#include "../Commands/CommandObjectCommands.h"
Eli Friedman3afb70c2010-06-13 02:17:17 +000037#include "../Commands/CommandObjectSyntax.h"
38#include "../Commands/CommandObjectTarget.h"
39#include "../Commands/CommandObjectThread.h"
Greg Clayton4a33d312011-06-23 17:59:56 +000040#include "../Commands/CommandObjectType.h"
Johnny Chen31c39da2010-12-23 20:21:44 +000041#include "../Commands/CommandObjectVersion.h"
Johnny Chenf04ee932011-09-22 18:04:58 +000042#include "../Commands/CommandObjectWatchpoint.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"
63#include "lldb/Interpreter/ScriptInterpreterNone.h"
64#include "lldb/Interpreter/ScriptInterpreterPython.h"
65
66
Chris Lattner30fdc8d2010-06-08 16:52:24 +000067#include "lldb/Target/Process.h"
68#include "lldb/Target/Thread.h"
69#include "lldb/Target/TargetList.h"
70
Enrico Granatab5887262012-10-29 21:18:03 +000071#include "lldb/Utility/CleanUp.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000072
Zachary Turnera21fee02014-08-21 21:49:24 +000073#include "llvm/ADT/SmallString.h"
Saleem Abdulrasool28606952014-06-27 05:17:41 +000074#include "llvm/ADT/STLExtras.h"
Zachary Turnera21fee02014-08-21 21:49:24 +000075#include "llvm/Support/Path.h"
Saleem Abdulrasool28606952014-06-27 05:17:41 +000076
Chris Lattner30fdc8d2010-06-08 16:52:24 +000077using namespace lldb;
78using namespace lldb_private;
79
Greg Clayton754a9362012-08-23 00:22:02 +000080
81static PropertyDefinition
82g_properties[] =
83{
Ed Masted78c9572014-04-20 00:31:37 +000084 { "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." },
85 { "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." },
86 { "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." },
87 { nullptr , OptionValue::eTypeInvalid, true, 0 , nullptr, nullptr, nullptr }
Greg Clayton754a9362012-08-23 00:22:02 +000088};
89
90enum
91{
Enrico Granatabcba2b22013-01-17 21:36:19 +000092 ePropertyExpandRegexAliases = 0,
Enrico Granata012d4fc2013-06-11 01:26:35 +000093 ePropertyPromptOnQuit = 1,
94 ePropertyStopCmdSourceOnError = 2
Greg Clayton754a9362012-08-23 00:22:02 +000095};
96
Jim Ingham4bddaeb2012-02-16 06:50:00 +000097ConstString &
98CommandInterpreter::GetStaticBroadcasterClass ()
99{
100 static ConstString class_name ("lldb.commandInterpreter");
101 return class_name;
102}
103
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000104CommandInterpreter::CommandInterpreter
105(
Greg Clayton66111032010-06-23 01:19:29 +0000106 Debugger &debugger,
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000107 ScriptLanguage script_language,
Greg Clayton66111032010-06-23 01:19:29 +0000108 bool synchronous_execution
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000109) :
Jim Ingham4bddaeb2012-02-16 06:50:00 +0000110 Broadcaster (&debugger, "lldb.command-interpreter"),
Greg Clayton754a9362012-08-23 00:22:02 +0000111 Properties(OptionValuePropertiesSP(new OptionValueProperties(ConstString("interpreter")))),
Greg Clayton44d93782014-01-27 23:43:24 +0000112 IOHandlerDelegate (IOHandlerDelegate::Completion::LLDBCommand),
Greg Clayton66111032010-06-23 01:19:29 +0000113 m_debugger (debugger),
Greg Clayton6eee5aa2010-10-11 01:05:37 +0000114 m_synchronous_execution (synchronous_execution),
Caroline Tice2f88aad2011-01-14 00:29:16 +0000115 m_skip_lldbinit_files (false),
Jim Ingham16e0c682011-08-12 23:34:31 +0000116 m_skip_app_init_files (false),
Jim Inghame16c50a2011-02-18 00:54:25 +0000117 m_script_interpreter_ap (),
Greg Clayton44d93782014-01-27 23:43:24 +0000118 m_command_io_handler_sp (),
Caroline Ticed61c10b2011-06-16 16:27:19 +0000119 m_comment_char ('#'),
Johnny Chen4ac1d9e2012-08-09 22:06:10 +0000120 m_batch_command_mode (false),
Enrico Granata5f5ab602012-05-31 01:09:06 +0000121 m_truncation_warning(eNoTruncation),
122 m_command_source_depth (0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000123{
Greg Clayton67cc0632012-08-22 17:17:09 +0000124 debugger.SetScriptLanguage (script_language);
Greg Claytoncfd1ace2010-10-31 03:01:06 +0000125 SetEventName (eBroadcastBitThreadShouldExit, "thread-should-exit");
126 SetEventName (eBroadcastBitResetPrompt, "reset-prompt");
Greg Clayton67cc0632012-08-22 17:17:09 +0000127 SetEventName (eBroadcastBitQuitCommandReceived, "quit");
Jim Ingham4bddaeb2012-02-16 06:50:00 +0000128 CheckInWithManager ();
Greg Clayton754a9362012-08-23 00:22:02 +0000129 m_collection_sp->Initialize (g_properties);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000130}
131
Greg Clayton754a9362012-08-23 00:22:02 +0000132bool
133CommandInterpreter::GetExpandRegexAliases () const
134{
135 const uint32_t idx = ePropertyExpandRegexAliases;
Ed Masted78c9572014-04-20 00:31:37 +0000136 return m_collection_sp->GetPropertyAtIndexAsBoolean (nullptr, idx, g_properties[idx].default_uint_value != 0);
Greg Clayton754a9362012-08-23 00:22:02 +0000137}
138
Enrico Granatabcba2b22013-01-17 21:36:19 +0000139bool
140CommandInterpreter::GetPromptOnQuit () const
141{
142 const uint32_t idx = ePropertyPromptOnQuit;
Ed Masted78c9572014-04-20 00:31:37 +0000143 return m_collection_sp->GetPropertyAtIndexAsBoolean (nullptr, idx, g_properties[idx].default_uint_value != 0);
Enrico Granatabcba2b22013-01-17 21:36:19 +0000144}
Greg Clayton754a9362012-08-23 00:22:02 +0000145
Enrico Granata012d4fc2013-06-11 01:26:35 +0000146bool
147CommandInterpreter::GetStopCmdSourceOnError () const
148{
149 const uint32_t idx = ePropertyStopCmdSourceOnError;
Ed Masted78c9572014-04-20 00:31:37 +0000150 return m_collection_sp->GetPropertyAtIndexAsBoolean (nullptr, idx, g_properties[idx].default_uint_value != 0);
Enrico Granata012d4fc2013-06-11 01:26:35 +0000151}
152
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000153void
154CommandInterpreter::Initialize ()
155{
156 Timer scoped_timer (__PRETTY_FUNCTION__, __PRETTY_FUNCTION__);
157
158 CommandReturnObject result;
159
160 LoadCommandDictionary ();
161
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000162 // Set up some initial aliases.
Caroline Ticeca90c472011-05-06 21:37:15 +0000163 CommandObjectSP cmd_obj_sp = GetCommandSPExact ("quit", false);
164 if (cmd_obj_sp)
165 {
166 AddAlias ("q", cmd_obj_sp);
167 AddAlias ("exit", cmd_obj_sp);
168 }
Sean Callanan247e62a2012-05-04 23:15:02 +0000169
Johnny Chen6d675242012-08-24 18:15:45 +0000170 cmd_obj_sp = GetCommandSPExact ("_regexp-attach",false);
Sean Callanan247e62a2012-05-04 23:15:02 +0000171 if (cmd_obj_sp)
172 {
173 AddAlias ("attach", cmd_obj_sp);
174 }
Caroline Ticeca90c472011-05-06 21:37:15 +0000175
Johnny Chen6d675242012-08-24 18:15:45 +0000176 cmd_obj_sp = GetCommandSPExact ("process detach",false);
177 if (cmd_obj_sp)
178 {
179 AddAlias ("detach", cmd_obj_sp);
180 }
181
Caroline Ticeca90c472011-05-06 21:37:15 +0000182 cmd_obj_sp = GetCommandSPExact ("process continue", false);
183 if (cmd_obj_sp)
184 {
185 AddAlias ("c", cmd_obj_sp);
186 AddAlias ("continue", cmd_obj_sp);
187 }
188
189 cmd_obj_sp = GetCommandSPExact ("_regexp-break",false);
190 if (cmd_obj_sp)
191 AddAlias ("b", cmd_obj_sp);
192
Jim Inghamca36cd12012-10-05 19:16:31 +0000193 cmd_obj_sp = GetCommandSPExact ("_regexp-tbreak",false);
194 if (cmd_obj_sp)
195 AddAlias ("tbreak", cmd_obj_sp);
196
Caroline Ticeca90c472011-05-06 21:37:15 +0000197 cmd_obj_sp = GetCommandSPExact ("thread step-inst", false);
198 if (cmd_obj_sp)
Jason Molendaf385f122011-10-22 00:47:41 +0000199 {
200 AddAlias ("stepi", cmd_obj_sp);
Caroline Ticeca90c472011-05-06 21:37:15 +0000201 AddAlias ("si", cmd_obj_sp);
Jason Molendaf385f122011-10-22 00:47:41 +0000202 }
203
204 cmd_obj_sp = GetCommandSPExact ("thread step-inst-over", false);
205 if (cmd_obj_sp)
206 {
207 AddAlias ("nexti", cmd_obj_sp);
208 AddAlias ("ni", cmd_obj_sp);
209 }
Caroline Ticeca90c472011-05-06 21:37:15 +0000210
211 cmd_obj_sp = GetCommandSPExact ("thread step-in", false);
212 if (cmd_obj_sp)
213 {
214 AddAlias ("s", cmd_obj_sp);
215 AddAlias ("step", cmd_obj_sp);
216 }
217
218 cmd_obj_sp = GetCommandSPExact ("thread step-over", false);
219 if (cmd_obj_sp)
220 {
221 AddAlias ("n", cmd_obj_sp);
222 AddAlias ("next", cmd_obj_sp);
223 }
224
225 cmd_obj_sp = GetCommandSPExact ("thread step-out", false);
226 if (cmd_obj_sp)
227 {
Caroline Ticeca90c472011-05-06 21:37:15 +0000228 AddAlias ("finish", cmd_obj_sp);
229 }
230
Jim Ingham6d6d1072011-12-02 01:12:59 +0000231 cmd_obj_sp = GetCommandSPExact ("frame select", false);
232 if (cmd_obj_sp)
233 {
234 AddAlias ("f", cmd_obj_sp);
235 }
236
Jim Inghamca36cd12012-10-05 19:16:31 +0000237 cmd_obj_sp = GetCommandSPExact ("thread select", false);
238 if (cmd_obj_sp)
239 {
240 AddAlias ("t", cmd_obj_sp);
241 }
242
Richard Mittonf86248d2013-09-12 02:20:34 +0000243 cmd_obj_sp = GetCommandSPExact ("_regexp-jump",false);
244 if (cmd_obj_sp)
245 {
246 AddAlias ("j", cmd_obj_sp);
247 AddAlias ("jump", cmd_obj_sp);
248 }
249
Greg Clayton6bade322013-02-01 23:33:03 +0000250 cmd_obj_sp = GetCommandSPExact ("_regexp-list", false);
Caroline Ticeca90c472011-05-06 21:37:15 +0000251 if (cmd_obj_sp)
252 {
253 AddAlias ("l", cmd_obj_sp);
254 AddAlias ("list", cmd_obj_sp);
255 }
256
Greg Claytonef5651d2013-02-12 18:52:24 +0000257 cmd_obj_sp = GetCommandSPExact ("_regexp-env", false);
258 if (cmd_obj_sp)
259 {
260 AddAlias ("env", cmd_obj_sp);
261 }
262
Caroline Ticeca90c472011-05-06 21:37:15 +0000263 cmd_obj_sp = GetCommandSPExact ("memory read", false);
264 if (cmd_obj_sp)
265 AddAlias ("x", cmd_obj_sp);
266
267 cmd_obj_sp = GetCommandSPExact ("_regexp-up", false);
268 if (cmd_obj_sp)
269 AddAlias ("up", cmd_obj_sp);
270
271 cmd_obj_sp = GetCommandSPExact ("_regexp-down", false);
272 if (cmd_obj_sp)
273 AddAlias ("down", cmd_obj_sp);
274
Jason Molenda0c8e0062011-10-25 02:11:20 +0000275 cmd_obj_sp = GetCommandSPExact ("_regexp-display", false);
Jason Molendabc7748b2011-10-22 01:30:52 +0000276 if (cmd_obj_sp)
277 AddAlias ("display", cmd_obj_sp);
Jim Ingham7e18e422011-10-24 18:37:00 +0000278
279 cmd_obj_sp = GetCommandSPExact ("disassemble", false);
280 if (cmd_obj_sp)
281 AddAlias ("dis", cmd_obj_sp);
282
283 cmd_obj_sp = GetCommandSPExact ("disassemble", false);
284 if (cmd_obj_sp)
285 AddAlias ("di", cmd_obj_sp);
286
287
Jason Molendabc7748b2011-10-22 01:30:52 +0000288
Jason Molenda0c8e0062011-10-25 02:11:20 +0000289 cmd_obj_sp = GetCommandSPExact ("_regexp-undisplay", false);
Jason Molendabc7748b2011-10-22 01:30:52 +0000290 if (cmd_obj_sp)
291 AddAlias ("undisplay", cmd_obj_sp);
292
Jim Ingham71bf2992012-10-10 16:51:31 +0000293 cmd_obj_sp = GetCommandSPExact ("_regexp-bt", false);
294 if (cmd_obj_sp)
295 AddAlias ("bt", cmd_obj_sp);
296
Caroline Ticeca90c472011-05-06 21:37:15 +0000297 cmd_obj_sp = GetCommandSPExact ("target create", false);
298 if (cmd_obj_sp)
299 AddAlias ("file", cmd_obj_sp);
300
301 cmd_obj_sp = GetCommandSPExact ("target modules", false);
302 if (cmd_obj_sp)
303 AddAlias ("image", cmd_obj_sp);
304
305
306 OptionArgVectorSP alias_arguments_vector_sp (new OptionArgVector);
Jim Inghamffba2292011-03-22 02:29:32 +0000307
Caroline Ticeca90c472011-05-06 21:37:15 +0000308 cmd_obj_sp = GetCommandSPExact ("expression", false);
309 if (cmd_obj_sp)
Sean Callanan90e579f2013-04-17 17:23:58 +0000310 {
Caroline Ticeca90c472011-05-06 21:37:15 +0000311 ProcessAliasOptionsArgs (cmd_obj_sp, "--", alias_arguments_vector_sp);
312 AddAlias ("p", cmd_obj_sp);
313 AddAlias ("print", cmd_obj_sp);
Sean Callanan316d5e42012-08-08 01:30:34 +0000314 AddAlias ("call", cmd_obj_sp);
Caroline Ticeca90c472011-05-06 21:37:15 +0000315 AddOrReplaceAliasOptions ("p", alias_arguments_vector_sp);
316 AddOrReplaceAliasOptions ("print", alias_arguments_vector_sp);
Sean Callanan316d5e42012-08-08 01:30:34 +0000317 AddOrReplaceAliasOptions ("call", alias_arguments_vector_sp);
Caroline Ticeca90c472011-05-06 21:37:15 +0000318
319 alias_arguments_vector_sp.reset (new OptionArgVector);
Greg Clayton9e57dcd2013-05-15 01:03:08 +0000320 ProcessAliasOptionsArgs (cmd_obj_sp, "-O -- ", alias_arguments_vector_sp);
Caroline Ticeca90c472011-05-06 21:37:15 +0000321 AddAlias ("po", cmd_obj_sp);
322 AddOrReplaceAliasOptions ("po", alias_arguments_vector_sp);
323 }
324
Sean Callanan2e1d9ba2012-06-01 23:29:32 +0000325 cmd_obj_sp = GetCommandSPExact ("process kill", false);
326 if (cmd_obj_sp)
Greg Claytone86fd742012-09-27 00:02:27 +0000327 {
Sean Callanan2e1d9ba2012-06-01 23:29:32 +0000328 AddAlias ("kill", cmd_obj_sp);
Greg Claytone86fd742012-09-27 00:02:27 +0000329 }
Sean Callanan2e1d9ba2012-06-01 23:29:32 +0000330
Caroline Ticeca90c472011-05-06 21:37:15 +0000331 cmd_obj_sp = GetCommandSPExact ("process launch", false);
332 if (cmd_obj_sp)
333 {
334 alias_arguments_vector_sp.reset (new OptionArgVector);
Todd Fiala013434e2014-07-09 01:29:05 +0000335#if defined (__arm__) || defined (__arm64__) || defined (__aarch64__)
Jason Molenda85da3122012-07-06 02:46:23 +0000336 ProcessAliasOptionsArgs (cmd_obj_sp, "--", alias_arguments_vector_sp);
337#else
Ed Maste7c566672013-09-05 21:38:45 +0000338 ProcessAliasOptionsArgs (cmd_obj_sp, "--shell=" LLDB_DEFAULT_SHELL " --", alias_arguments_vector_sp);
Jason Molenda85da3122012-07-06 02:46:23 +0000339#endif
Caroline Ticeca90c472011-05-06 21:37:15 +0000340 AddAlias ("r", cmd_obj_sp);
341 AddAlias ("run", cmd_obj_sp);
342 AddOrReplaceAliasOptions ("r", alias_arguments_vector_sp);
343 AddOrReplaceAliasOptions ("run", alias_arguments_vector_sp);
344 }
Greg Clayton843d62d2012-03-29 21:47:51 +0000345
346 cmd_obj_sp = GetCommandSPExact ("target symbols add", false);
347 if (cmd_obj_sp)
348 {
349 AddAlias ("add-dsym", cmd_obj_sp);
350 }
Sean Callananfc732752012-05-21 18:25:19 +0000351
352 cmd_obj_sp = GetCommandSPExact ("breakpoint set", false);
353 if (cmd_obj_sp)
354 {
355 alias_arguments_vector_sp.reset (new OptionArgVector);
356 ProcessAliasOptionsArgs (cmd_obj_sp, "--func-regex %1", alias_arguments_vector_sp);
Jim Ingham06d282d2012-10-18 23:24:12 +0000357 AddAlias ("rbreak", cmd_obj_sp);
358 AddOrReplaceAliasOptions("rbreak", alias_arguments_vector_sp);
Sean Callananfc732752012-05-21 18:25:19 +0000359 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000360}
361
Greg Clayton0c4129f2014-04-25 00:35:14 +0000362void
363CommandInterpreter::Clear()
364{
365 m_command_io_handler_sp.reset();
Greg Claytoned6499f2014-04-25 23:55:12 +0000366
367 if (m_script_interpreter_ap)
368 m_script_interpreter_ap->Clear();
Greg Clayton0c4129f2014-04-25 00:35:14 +0000369}
370
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000371const char *
372CommandInterpreter::ProcessEmbeddedScriptCommands (const char *arg)
373{
374 // This function has not yet been implemented.
375
376 // Look for any embedded script command
377 // If found,
378 // get interpreter object from the command dictionary,
379 // call execute_one_command on it,
380 // get the results as a string,
381 // substitute that string for current stuff.
382
383 return arg;
384}
385
386
387void
388CommandInterpreter::LoadCommandDictionary ()
389{
390 Timer scoped_timer (__PRETTY_FUNCTION__, __PRETTY_FUNCTION__);
391
Caroline Ticedaccaa92010-09-20 20:44:43 +0000392 lldb::ScriptLanguage script_language = m_debugger.GetScriptLanguage();
Caroline Tice3df9a8d2010-09-04 00:03:46 +0000393
Greg Claytona7015092010-09-18 01:14:36 +0000394 m_command_dict["apropos"] = CommandObjectSP (new CommandObjectApropos (*this));
Greg Clayton66111032010-06-23 01:19:29 +0000395 m_command_dict["breakpoint"]= CommandObjectSP (new CommandObjectMultiwordBreakpoint (*this));
Johnny Chenb89982d2011-04-21 00:39:18 +0000396 m_command_dict["command"] = CommandObjectSP (new CommandObjectMultiwordCommands (*this));
Greg Claytona7015092010-09-18 01:14:36 +0000397 m_command_dict["disassemble"] = CommandObjectSP (new CommandObjectDisassemble (*this));
398 m_command_dict["expression"]= CommandObjectSP (new CommandObjectExpression (*this));
Greg Clayton66111032010-06-23 01:19:29 +0000399 m_command_dict["frame"] = CommandObjectSP (new CommandObjectMultiwordFrame (*this));
Greg Clayton44d93782014-01-27 23:43:24 +0000400 m_command_dict["gui"] = CommandObjectSP (new CommandObjectGUI (*this));
Greg Claytona7015092010-09-18 01:14:36 +0000401 m_command_dict["help"] = CommandObjectSP (new CommandObjectHelp (*this));
Greg Clayton66111032010-06-23 01:19:29 +0000402 m_command_dict["log"] = CommandObjectSP (new CommandObjectLog (*this));
403 m_command_dict["memory"] = CommandObjectSP (new CommandObjectMemory (*this));
Greg Claytonded470d2011-03-19 01:12:21 +0000404 m_command_dict["platform"] = CommandObjectSP (new CommandObjectPlatform (*this));
Enrico Granata21dfcd92012-09-28 23:57:51 +0000405 m_command_dict["plugin"] = CommandObjectSP (new CommandObjectPlugin (*this));
Greg Clayton66111032010-06-23 01:19:29 +0000406 m_command_dict["process"] = CommandObjectSP (new CommandObjectMultiwordProcess (*this));
Greg Claytona7015092010-09-18 01:14:36 +0000407 m_command_dict["quit"] = CommandObjectSP (new CommandObjectQuit (*this));
Greg Clayton66111032010-06-23 01:19:29 +0000408 m_command_dict["register"] = CommandObjectSP (new CommandObjectRegister (*this));
Greg Claytona7015092010-09-18 01:14:36 +0000409 m_command_dict["script"] = CommandObjectSP (new CommandObjectScript (*this, script_language));
Caroline Tice3df9a8d2010-09-04 00:03:46 +0000410 m_command_dict["settings"] = CommandObjectSP (new CommandObjectMultiwordSettings (*this));
Jim Inghamebc09c32010-07-07 03:36:20 +0000411 m_command_dict["source"] = CommandObjectSP (new CommandObjectMultiwordSource (*this));
Greg Clayton66111032010-06-23 01:19:29 +0000412 m_command_dict["target"] = CommandObjectSP (new CommandObjectMultiwordTarget (*this));
413 m_command_dict["thread"] = CommandObjectSP (new CommandObjectMultiwordThread (*this));
Enrico Granata223383e2011-08-16 23:24:13 +0000414 m_command_dict["type"] = CommandObjectSP (new CommandObjectType (*this));
Johnny Chen31c39da2010-12-23 20:21:44 +0000415 m_command_dict["version"] = CommandObjectSP (new CommandObjectVersion (*this));
Johnny Chenf04ee932011-09-22 18:04:58 +0000416 m_command_dict["watchpoint"]= CommandObjectSP (new CommandObjectMultiwordWatchpoint (*this));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000417
Jim Inghamca36cd12012-10-05 19:16:31 +0000418 const char *break_regexes[][2] = {{"^(.*[^[:space:]])[[:space:]]*:[[:space:]]*([[:digit:]]+)[[:space:]]*$", "breakpoint set --file '%1' --line %2"},
419 {"^([[:digit:]]+)[[:space:]]*$", "breakpoint set --line %1"},
Greg Clayton722e8852013-02-08 02:54:24 +0000420 {"^\\*?(0x[[:xdigit:]]+)[[:space:]]*$", "breakpoint set --address %1"},
Greg Clayton1b3815c2013-01-30 00:18:29 +0000421 {"^[\"']?([-+]?\\[.*\\])[\"']?[[:space:]]*$", "breakpoint set --name '%1'"},
Jim Inghamca36cd12012-10-05 19:16:31 +0000422 {"^(-.*)$", "breakpoint set %1"},
423 {"^(.*[^[:space:]])`(.*[^[:space:]])[[:space:]]*$", "breakpoint set --name '%2' --shlib '%1'"},
Greg Clayton722e8852013-02-08 02:54:24 +0000424 {"^\\&(.*[^[:space:]])[[:space:]]*$", "breakpoint set --name '%1' --skip-prologue=0"},
Greg Clayton2d4b5122014-08-14 17:58:33 +0000425 {"^[\"']?(.*[^[:space:]\"'])[\"']?[[:space:]]*$", "breakpoint set --name '%1'"}};
Jim Inghamca36cd12012-10-05 19:16:31 +0000426
Saleem Abdulrasool28606952014-06-27 05:17:41 +0000427 size_t num_regexes = llvm::array_lengthof(break_regexes);
Jim Inghamca36cd12012-10-05 19:16:31 +0000428
Greg Clayton7b0992d2013-04-18 22:45:39 +0000429 std::unique_ptr<CommandObjectRegexCommand>
Greg Claytona7015092010-09-18 01:14:36 +0000430 break_regex_cmd_ap(new CommandObjectRegexCommand (*this,
Greg Clayton8b82f082011-04-12 05:54:46 +0000431 "_regexp-break",
Johnny Chenb417dcd2012-08-23 00:32:22 +0000432 "Set a 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 +0000433 "_regexp-break [<filename>:<linenum>]\n_regexp-break [<linenum>]\n_regexp-break [<address>]\n_regexp-break <...>",
434 2,
435 CommandCompletions::eSymbolCompletion |
436 CommandCompletions::eSourceFileCompletion));
Jim Inghamca36cd12012-10-05 19:16:31 +0000437
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000438 if (break_regex_cmd_ap.get())
439 {
Jim Inghamca36cd12012-10-05 19:16:31 +0000440 bool success = true;
441 for (size_t i = 0; i < num_regexes; i++)
442 {
443 success = break_regex_cmd_ap->AddRegexCommand (break_regexes[i][0], break_regexes[i][1]);
444 if (!success)
445 break;
446 }
447 success = break_regex_cmd_ap->AddRegexCommand("^$", "breakpoint list --full");
448
449 if (success)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000450 {
451 CommandObjectSP break_regex_cmd_sp(break_regex_cmd_ap.release());
452 m_command_dict[break_regex_cmd_sp->GetCommandName ()] = break_regex_cmd_sp;
453 }
454 }
Jim Inghamffba2292011-03-22 02:29:32 +0000455
Greg Clayton7b0992d2013-04-18 22:45:39 +0000456 std::unique_ptr<CommandObjectRegexCommand>
Jim Inghamca36cd12012-10-05 19:16:31 +0000457 tbreak_regex_cmd_ap(new CommandObjectRegexCommand (*this,
458 "_regexp-tbreak",
459 "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 +0000460 "_regexp-tbreak [<filename>:<linenum>]\n_regexp-break [<linenum>]\n_regexp-break [<address>]\n_regexp-break <...>",
461 2,
462 CommandCompletions::eSymbolCompletion |
463 CommandCompletions::eSourceFileCompletion));
Jim Inghamca36cd12012-10-05 19:16:31 +0000464
465 if (tbreak_regex_cmd_ap.get())
466 {
467 bool success = true;
468 for (size_t i = 0; i < num_regexes; i++)
469 {
470 // If you add a resultant command string longer than 1024 characters be sure to increase the size of this buffer.
471 char buffer[1024];
472 int num_printed = snprintf(buffer, 1024, "%s %s", break_regexes[i][1], "-o");
473 assert (num_printed < 1024);
Eric Christopher5c91d5c12014-09-09 19:26:45 +0000474 // Quiet unused variable warning for release builds.
Eric Christopher9ad83942014-09-09 08:57:33 +0000475 (void) num_printed;
Jim Inghamca36cd12012-10-05 19:16:31 +0000476 success = tbreak_regex_cmd_ap->AddRegexCommand (break_regexes[i][0], buffer);
477 if (!success)
478 break;
479 }
480 success = tbreak_regex_cmd_ap->AddRegexCommand("^$", "breakpoint list --full");
481
482 if (success)
483 {
484 CommandObjectSP tbreak_regex_cmd_sp(tbreak_regex_cmd_ap.release());
485 m_command_dict[tbreak_regex_cmd_sp->GetCommandName ()] = tbreak_regex_cmd_sp;
486 }
487 }
488
Greg Clayton7b0992d2013-04-18 22:45:39 +0000489 std::unique_ptr<CommandObjectRegexCommand>
Johnny Chen6d675242012-08-24 18:15:45 +0000490 attach_regex_cmd_ap(new CommandObjectRegexCommand (*this,
491 "_regexp-attach",
492 "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 +0000493 "_regexp-attach [<pid>]\n_regexp-attach [<process-name>]",
494 2));
Johnny Chen6d675242012-08-24 18:15:45 +0000495 if (attach_regex_cmd_ap.get())
496 {
Greg Clayton3cb4c7d2012-12-15 01:19:07 +0000497 if (attach_regex_cmd_ap->AddRegexCommand("^([0-9]+)[[:space:]]*$", "process attach --pid %1") &&
498 attach_regex_cmd_ap->AddRegexCommand("^(-.*|.* -.*)$", "process attach %1") && // Any options that are specified get passed to 'process attach'
499 attach_regex_cmd_ap->AddRegexCommand("^(.+)$", "process attach --name '%1'") &&
500 attach_regex_cmd_ap->AddRegexCommand("^$", "process attach"))
Johnny Chen6d675242012-08-24 18:15:45 +0000501 {
502 CommandObjectSP attach_regex_cmd_sp(attach_regex_cmd_ap.release());
503 m_command_dict[attach_regex_cmd_sp->GetCommandName ()] = attach_regex_cmd_sp;
504 }
505 }
506
Greg Clayton7b0992d2013-04-18 22:45:39 +0000507 std::unique_ptr<CommandObjectRegexCommand>
Jim Inghamffba2292011-03-22 02:29:32 +0000508 down_regex_cmd_ap(new CommandObjectRegexCommand (*this,
Greg Clayton8b82f082011-04-12 05:54:46 +0000509 "_regexp-down",
510 "Go down \"n\" frames in the stack (1 frame by default).",
511 "_regexp-down [n]", 2));
Jim Inghamffba2292011-03-22 02:29:32 +0000512 if (down_regex_cmd_ap.get())
513 {
514 if (down_regex_cmd_ap->AddRegexCommand("^$", "frame select -r -1") &&
515 down_regex_cmd_ap->AddRegexCommand("^([0-9]+)$", "frame select -r -%1"))
516 {
517 CommandObjectSP down_regex_cmd_sp(down_regex_cmd_ap.release());
518 m_command_dict[down_regex_cmd_sp->GetCommandName ()] = down_regex_cmd_sp;
519 }
520 }
521
Greg Clayton7b0992d2013-04-18 22:45:39 +0000522 std::unique_ptr<CommandObjectRegexCommand>
Jim Inghamffba2292011-03-22 02:29:32 +0000523 up_regex_cmd_ap(new CommandObjectRegexCommand (*this,
Greg Clayton8b82f082011-04-12 05:54:46 +0000524 "_regexp-up",
525 "Go up \"n\" frames in the stack (1 frame by default).",
526 "_regexp-up [n]", 2));
Jim Inghamffba2292011-03-22 02:29:32 +0000527 if (up_regex_cmd_ap.get())
528 {
529 if (up_regex_cmd_ap->AddRegexCommand("^$", "frame select -r 1") &&
530 up_regex_cmd_ap->AddRegexCommand("^([0-9]+)$", "frame select -r %1"))
531 {
532 CommandObjectSP up_regex_cmd_sp(up_regex_cmd_ap.release());
533 m_command_dict[up_regex_cmd_sp->GetCommandName ()] = up_regex_cmd_sp;
534 }
535 }
Jason Molendabc7748b2011-10-22 01:30:52 +0000536
Greg Clayton7b0992d2013-04-18 22:45:39 +0000537 std::unique_ptr<CommandObjectRegexCommand>
Jason Molendabc7748b2011-10-22 01:30:52 +0000538 display_regex_cmd_ap(new CommandObjectRegexCommand (*this,
Greg Clayton7d2ef162013-03-29 17:03:23 +0000539 "_regexp-display",
540 "Add an expression evaluation stop-hook.",
541 "_regexp-display expression", 2));
Jason Molendabc7748b2011-10-22 01:30:52 +0000542 if (display_regex_cmd_ap.get())
543 {
544 if (display_regex_cmd_ap->AddRegexCommand("^(.+)$", "target stop-hook add -o \"expr -- %1\""))
545 {
546 CommandObjectSP display_regex_cmd_sp(display_regex_cmd_ap.release());
547 m_command_dict[display_regex_cmd_sp->GetCommandName ()] = display_regex_cmd_sp;
548 }
549 }
550
Greg Clayton7b0992d2013-04-18 22:45:39 +0000551 std::unique_ptr<CommandObjectRegexCommand>
Jason Molendabc7748b2011-10-22 01:30:52 +0000552 undisplay_regex_cmd_ap(new CommandObjectRegexCommand (*this,
Greg Clayton7d2ef162013-03-29 17:03:23 +0000553 "_regexp-undisplay",
554 "Remove an expression evaluation stop-hook.",
555 "_regexp-undisplay stop-hook-number", 2));
Jason Molendabc7748b2011-10-22 01:30:52 +0000556 if (undisplay_regex_cmd_ap.get())
557 {
558 if (undisplay_regex_cmd_ap->AddRegexCommand("^([0-9]+)$", "target stop-hook delete %1"))
559 {
560 CommandObjectSP undisplay_regex_cmd_sp(undisplay_regex_cmd_ap.release());
561 m_command_dict[undisplay_regex_cmd_sp->GetCommandName ()] = undisplay_regex_cmd_sp;
562 }
563 }
564
Greg Clayton7b0992d2013-04-18 22:45:39 +0000565 std::unique_ptr<CommandObjectRegexCommand>
Greg Clayton30c0a1c2012-09-26 22:26:47 +0000566 connect_gdb_remote_cmd_ap(new CommandObjectRegexCommand (*this,
Greg Clayton7d2ef162013-03-29 17:03:23 +0000567 "gdb-remote",
568 "Connect to a remote GDB server. If no hostname is provided, localhost is assumed.",
569 "gdb-remote [<hostname>:]<portnum>", 2));
Greg Clayton30c0a1c2012-09-26 22:26:47 +0000570 if (connect_gdb_remote_cmd_ap.get())
571 {
572 if (connect_gdb_remote_cmd_ap->AddRegexCommand("^([^:]+:[[:digit:]]+)$", "process connect --plugin gdb-remote connect://%1") &&
573 connect_gdb_remote_cmd_ap->AddRegexCommand("^([[:digit:]]+)$", "process connect --plugin gdb-remote connect://localhost:%1"))
574 {
575 CommandObjectSP command_sp(connect_gdb_remote_cmd_ap.release());
576 m_command_dict[command_sp->GetCommandName ()] = command_sp;
577 }
578 }
579
Greg Clayton7b0992d2013-04-18 22:45:39 +0000580 std::unique_ptr<CommandObjectRegexCommand>
Greg Clayton30c0a1c2012-09-26 22:26:47 +0000581 connect_kdp_remote_cmd_ap(new CommandObjectRegexCommand (*this,
582 "kdp-remote",
Jason Molendaa7dcb332012-10-23 03:05:16 +0000583 "Connect to a remote KDP server. udp port 41139 is the default port number.",
584 "kdp-remote <hostname>[:<portnum>]", 2));
Greg Clayton30c0a1c2012-09-26 22:26:47 +0000585 if (connect_kdp_remote_cmd_ap.get())
586 {
587 if (connect_kdp_remote_cmd_ap->AddRegexCommand("^([^:]+:[[:digit:]]+)$", "process connect --plugin kdp-remote udp://%1") &&
Jason Molendac36b1842012-09-27 02:47:55 +0000588 connect_kdp_remote_cmd_ap->AddRegexCommand("^(.+)$", "process connect --plugin kdp-remote udp://%1:41139"))
Greg Clayton30c0a1c2012-09-26 22:26:47 +0000589 {
590 CommandObjectSP command_sp(connect_kdp_remote_cmd_ap.release());
591 m_command_dict[command_sp->GetCommandName ()] = command_sp;
592 }
593 }
594
Greg Clayton7b0992d2013-04-18 22:45:39 +0000595 std::unique_ptr<CommandObjectRegexCommand>
Jason Molenda4cddfed2012-10-05 05:29:32 +0000596 bt_regex_cmd_ap(new CommandObjectRegexCommand (*this,
Jim Ingham71bf2992012-10-10 16:51:31 +0000597 "_regexp-bt",
Jason Molenda4cddfed2012-10-05 05:29:32 +0000598 "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.",
599 "bt [<digit>|all]", 2));
600 if (bt_regex_cmd_ap.get())
601 {
602 // accept but don't document "bt -c <number>" -- before bt was a regex command if you wanted to backtrace
603 // three frames you would do "bt -c 3" but the intention is to have this emulate the gdb "bt" command and
604 // so now "bt 3" is the preferred form, in line with gdb.
605 if (bt_regex_cmd_ap->AddRegexCommand("^([[:digit:]]+)$", "thread backtrace -c %1") &&
606 bt_regex_cmd_ap->AddRegexCommand("^-c ([[:digit:]]+)$", "thread backtrace -c %1") &&
607 bt_regex_cmd_ap->AddRegexCommand("^all$", "thread backtrace all") &&
608 bt_regex_cmd_ap->AddRegexCommand("^$", "thread backtrace"))
609 {
610 CommandObjectSP command_sp(bt_regex_cmd_ap.release());
611 m_command_dict[command_sp->GetCommandName ()] = command_sp;
612 }
613 }
614
Greg Clayton7b0992d2013-04-18 22:45:39 +0000615 std::unique_ptr<CommandObjectRegexCommand>
Greg Clayton6bade322013-02-01 23:33:03 +0000616 list_regex_cmd_ap(new CommandObjectRegexCommand (*this,
617 "_regexp-list",
618 "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 +0000619 "_regexp-list [<line>]\n_regexp-list [<file>:<line>]\n_regexp-list [<file>:<line>]",
Greg Clayton7d2ef162013-03-29 17:03:23 +0000620 2,
621 CommandCompletions::eSourceFileCompletion));
Greg Clayton6bade322013-02-01 23:33:03 +0000622 if (list_regex_cmd_ap.get())
623 {
624 if (list_regex_cmd_ap->AddRegexCommand("^([0-9]+)[[:space:]]*$", "source list --line %1") &&
625 list_regex_cmd_ap->AddRegexCommand("^(.*[^[:space:]])[[:space:]]*:[[:space:]]*([[:digit:]]+)[[:space:]]*$", "source list --file '%1' --line %2") &&
626 list_regex_cmd_ap->AddRegexCommand("^\\*?(0x[[:xdigit:]]+)[[:space:]]*$", "source list --address %1") &&
Greg Claytond9010962013-02-12 18:42:05 +0000627 list_regex_cmd_ap->AddRegexCommand("^-[[:space:]]*$", "source list --reverse") &&
Greg Claytone4ca5152013-03-13 18:25:49 +0000628 list_regex_cmd_ap->AddRegexCommand("^-([[:digit:]]+)[[:space:]]*$", "source list --reverse --count %1") &&
Greg Clayton6bade322013-02-01 23:33:03 +0000629 list_regex_cmd_ap->AddRegexCommand("^(.+)$", "source list --name \"%1\"") &&
630 list_regex_cmd_ap->AddRegexCommand("^$", "source list"))
631 {
632 CommandObjectSP list_regex_cmd_sp(list_regex_cmd_ap.release());
633 m_command_dict[list_regex_cmd_sp->GetCommandName ()] = list_regex_cmd_sp;
634 }
635 }
636
Greg Clayton7b0992d2013-04-18 22:45:39 +0000637 std::unique_ptr<CommandObjectRegexCommand>
Greg Claytonef5651d2013-02-12 18:52:24 +0000638 env_regex_cmd_ap(new CommandObjectRegexCommand (*this,
Greg Clayton7d2ef162013-03-29 17:03:23 +0000639 "_regexp-env",
640 "Implements a shortcut to viewing and setting environment variables.",
641 "_regexp-env\n_regexp-env FOO=BAR", 2));
Greg Claytonef5651d2013-02-12 18:52:24 +0000642 if (env_regex_cmd_ap.get())
643 {
644 if (env_regex_cmd_ap->AddRegexCommand("^$", "settings show target.env-vars") &&
645 env_regex_cmd_ap->AddRegexCommand("^([A-Za-z_][A-Za-z_0-9]*=.*)$", "settings set target.env-vars %1"))
646 {
647 CommandObjectSP env_regex_cmd_sp(env_regex_cmd_ap.release());
648 m_command_dict[env_regex_cmd_sp->GetCommandName ()] = env_regex_cmd_sp;
649 }
650 }
651
Richard Mittonf86248d2013-09-12 02:20:34 +0000652 std::unique_ptr<CommandObjectRegexCommand>
653 jump_regex_cmd_ap(new CommandObjectRegexCommand (*this,
654 "_regexp-jump",
655 "Sets the program counter to a new address.",
656 "_regexp-jump [<line>]\n"
657 "_regexp-jump [<+-lineoffset>]\n"
658 "_regexp-jump [<file>:<line>]\n"
659 "_regexp-jump [*<addr>]\n", 2));
660 if (jump_regex_cmd_ap.get())
661 {
662 if (jump_regex_cmd_ap->AddRegexCommand("^\\*(.*)$", "thread jump --addr %1") &&
663 jump_regex_cmd_ap->AddRegexCommand("^([0-9]+)$", "thread jump --line %1") &&
664 jump_regex_cmd_ap->AddRegexCommand("^([^:]+):([0-9]+)$", "thread jump --file %1 --line %2") &&
665 jump_regex_cmd_ap->AddRegexCommand("^([+\\-][0-9]+)$", "thread jump --by %1"))
666 {
667 CommandObjectSP jump_regex_cmd_sp(jump_regex_cmd_ap.release());
668 m_command_dict[jump_regex_cmd_sp->GetCommandName ()] = jump_regex_cmd_sp;
669 }
670 }
671
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000672}
673
674int
675CommandInterpreter::GetCommandNamesMatchingPartialString (const char *cmd_str, bool include_aliases,
676 StringList &matches)
677{
678 CommandObject::AddNamesMatchingPartialString (m_command_dict, cmd_str, matches);
679
680 if (include_aliases)
681 {
682 CommandObject::AddNamesMatchingPartialString (m_alias_dict, cmd_str, matches);
683 }
684
685 return matches.GetSize();
686}
687
688CommandObjectSP
689CommandInterpreter::GetCommandSP (const char *cmd_cstr, bool include_aliases, bool exact, StringList *matches)
690{
691 CommandObject::CommandMap::iterator pos;
Greg Claytonc7bece562013-01-25 18:06:21 +0000692 CommandObjectSP command_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000693
694 std::string cmd(cmd_cstr);
695
696 if (HasCommands())
697 {
698 pos = m_command_dict.find(cmd);
699 if (pos != m_command_dict.end())
Greg Claytonc7bece562013-01-25 18:06:21 +0000700 command_sp = pos->second;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000701 }
702
703 if (include_aliases && HasAliases())
704 {
705 pos = m_alias_dict.find(cmd);
706 if (pos != m_alias_dict.end())
Greg Claytonc7bece562013-01-25 18:06:21 +0000707 command_sp = pos->second;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000708 }
709
710 if (HasUserCommands())
711 {
712 pos = m_user_dict.find(cmd);
713 if (pos != m_user_dict.end())
Greg Claytonc7bece562013-01-25 18:06:21 +0000714 command_sp = pos->second;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000715 }
716
Greg Claytonc7bece562013-01-25 18:06:21 +0000717 if (!exact && !command_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000718 {
Jim Ingham279a6c22010-07-06 22:46:59 +0000719 // We will only get into here if we didn't find any exact matches.
720
721 CommandObjectSP user_match_sp, alias_match_sp, real_match_sp;
722
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000723 StringList local_matches;
Ed Masted78c9572014-04-20 00:31:37 +0000724 if (matches == nullptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000725 matches = &local_matches;
726
Jim Ingham279a6c22010-07-06 22:46:59 +0000727 unsigned int num_cmd_matches = 0;
728 unsigned int num_alias_matches = 0;
729 unsigned int num_user_matches = 0;
730
731 // Look through the command dictionaries one by one, and if we get only one match from any of
732 // them in toto, then return that, otherwise return an empty CommandObjectSP and the list of matches.
733
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000734 if (HasCommands())
735 {
736 num_cmd_matches = CommandObject::AddNamesMatchingPartialString (m_command_dict, cmd_cstr, *matches);
737 }
738
739 if (num_cmd_matches == 1)
740 {
741 cmd.assign(matches->GetStringAtIndex(0));
742 pos = m_command_dict.find(cmd);
743 if (pos != m_command_dict.end())
Jim Ingham279a6c22010-07-06 22:46:59 +0000744 real_match_sp = pos->second;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000745 }
746
Jim Ingham490ac552010-06-24 20:28:42 +0000747 if (include_aliases && HasAliases())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000748 {
749 num_alias_matches = CommandObject::AddNamesMatchingPartialString (m_alias_dict, cmd_cstr, *matches);
750
751 }
752
Jim Ingham279a6c22010-07-06 22:46:59 +0000753 if (num_alias_matches == 1)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000754 {
755 cmd.assign(matches->GetStringAtIndex (num_cmd_matches));
756 pos = m_alias_dict.find(cmd);
757 if (pos != m_alias_dict.end())
Jim Ingham279a6c22010-07-06 22:46:59 +0000758 alias_match_sp = pos->second;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000759 }
760
Jim Ingham490ac552010-06-24 20:28:42 +0000761 if (HasUserCommands())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000762 {
763 num_user_matches = CommandObject::AddNamesMatchingPartialString (m_user_dict, cmd_cstr, *matches);
764 }
765
Jim Ingham279a6c22010-07-06 22:46:59 +0000766 if (num_user_matches == 1)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000767 {
768 cmd.assign (matches->GetStringAtIndex (num_cmd_matches + num_alias_matches));
769
770 pos = m_user_dict.find (cmd);
771 if (pos != m_user_dict.end())
Jim Ingham279a6c22010-07-06 22:46:59 +0000772 user_match_sp = pos->second;
773 }
774
775 // If we got exactly one match, return that, otherwise return the match list.
776
777 if (num_user_matches + num_cmd_matches + num_alias_matches == 1)
778 {
779 if (num_cmd_matches)
780 return real_match_sp;
781 else if (num_alias_matches)
782 return alias_match_sp;
783 else
784 return user_match_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000785 }
786 }
Greg Claytonc7bece562013-01-25 18:06:21 +0000787 else if (matches && command_sp)
Jim Ingham279a6c22010-07-06 22:46:59 +0000788 {
789 matches->AppendString (cmd_cstr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000790 }
791
792
Greg Claytonc7bece562013-01-25 18:06:21 +0000793 return command_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000794}
795
Greg Claytonde164aa2011-04-20 16:37:46 +0000796bool
797CommandInterpreter::AddCommand (const char *name, const lldb::CommandObjectSP &cmd_sp, bool can_replace)
798{
799 if (name && name[0])
800 {
801 std::string name_sstr(name);
Enrico Granatae00af802012-10-01 17:19:37 +0000802 bool found = (m_command_dict.find (name_sstr) != m_command_dict.end());
803 if (found && !can_replace)
804 return false;
805 if (found && m_command_dict[name_sstr]->IsRemovable() == false)
Enrico Granata21dfcd92012-09-28 23:57:51 +0000806 return false;
Greg Claytonde164aa2011-04-20 16:37:46 +0000807 m_command_dict[name_sstr] = cmd_sp;
808 return true;
809 }
810 return false;
811}
812
Enrico Granata223383e2011-08-16 23:24:13 +0000813bool
Enrico Granata0a305db2011-11-07 22:57:04 +0000814CommandInterpreter::AddUserCommand (std::string name,
Enrico Granata223383e2011-08-16 23:24:13 +0000815 const lldb::CommandObjectSP &cmd_sp,
816 bool can_replace)
817{
Enrico Granata0a305db2011-11-07 22:57:04 +0000818 if (!name.empty())
Enrico Granata223383e2011-08-16 23:24:13 +0000819 {
Enrico Granata0a305db2011-11-07 22:57:04 +0000820
821 const char* name_cstr = name.c_str();
822
823 // do not allow replacement of internal commands
824 if (CommandExists(name_cstr))
Enrico Granata21dfcd92012-09-28 23:57:51 +0000825 {
826 if (can_replace == false)
827 return false;
828 if (m_command_dict[name]->IsRemovable() == false)
829 return false;
830 }
Enrico Granata0a305db2011-11-07 22:57:04 +0000831
Enrico Granata21dfcd92012-09-28 23:57:51 +0000832 if (UserCommandExists(name_cstr))
833 {
834 if (can_replace == false)
835 return false;
836 if (m_user_dict[name]->IsRemovable() == false)
837 return false;
838 }
839
Enrico Granata0a305db2011-11-07 22:57:04 +0000840 m_user_dict[name] = cmd_sp;
Enrico Granata223383e2011-08-16 23:24:13 +0000841 return true;
842 }
843 return false;
844}
Greg Claytonde164aa2011-04-20 16:37:46 +0000845
Jim Ingham279a6c22010-07-06 22:46:59 +0000846CommandObjectSP
847CommandInterpreter::GetCommandSPExact (const char *cmd_cstr, bool include_aliases)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000848{
Caroline Tice472362e2010-12-14 18:51:39 +0000849 Args cmd_words (cmd_cstr); // Break up the command string into words, in case it's a multi-word command.
850 CommandObjectSP ret_val; // Possibly empty return value.
851
Ed Masted78c9572014-04-20 00:31:37 +0000852 if (cmd_cstr == nullptr)
Caroline Tice472362e2010-12-14 18:51:39 +0000853 return ret_val;
854
855 if (cmd_words.GetArgumentCount() == 1)
Ed Masted78c9572014-04-20 00:31:37 +0000856 return GetCommandSP(cmd_cstr, include_aliases, true, nullptr);
Caroline Tice472362e2010-12-14 18:51:39 +0000857 else
858 {
859 // We have a multi-word command (seemingly), so we need to do more work.
860 // First, get the cmd_obj_sp for the first word in the command.
Ed Masted78c9572014-04-20 00:31:37 +0000861 CommandObjectSP cmd_obj_sp = GetCommandSP (cmd_words.GetArgumentAtIndex (0), include_aliases, true, nullptr);
862 if (cmd_obj_sp.get() != nullptr)
Caroline Tice472362e2010-12-14 18:51:39 +0000863 {
864 // Loop through the rest of the words in the command (everything passed in was supposed to be part of a
865 // command name), and find the appropriate sub-command SP for each command word....
866 size_t end = cmd_words.GetArgumentCount();
867 for (size_t j= 1; j < end; ++j)
868 {
869 if (cmd_obj_sp->IsMultiwordObject())
870 {
Greg Clayton998255b2012-10-13 02:07:45 +0000871 cmd_obj_sp = cmd_obj_sp->GetSubcommandSP (cmd_words.GetArgumentAtIndex (j));
Ed Masted78c9572014-04-20 00:31:37 +0000872 if (cmd_obj_sp.get() == nullptr)
Caroline Tice472362e2010-12-14 18:51:39 +0000873 // The sub-command name was invalid. Fail and return the empty 'ret_val'.
874 return ret_val;
875 }
876 else
877 // We have more words in the command name, but we don't have a multiword object. Fail and return
878 // empty 'ret_val'.
879 return ret_val;
880 }
881 // We successfully looped through all the command words and got valid command objects for them. Assign the
882 // last object retrieved to 'ret_val'.
883 ret_val = cmd_obj_sp;
884 }
885 }
886 return ret_val;
Jim Ingham279a6c22010-07-06 22:46:59 +0000887}
888
889CommandObject *
890CommandInterpreter::GetCommandObjectExact (const char *cmd_cstr, bool include_aliases)
891{
892 return GetCommandSPExact (cmd_cstr, include_aliases).get();
893}
894
895CommandObject *
896CommandInterpreter::GetCommandObject (const char *cmd_cstr, StringList *matches)
897{
898 CommandObject *command_obj = GetCommandSP (cmd_cstr, false, true, matches).get();
899
900 // If we didn't find an exact match to the command string in the commands, look in
901 // the aliases.
Enrico Granata5342c442013-06-18 18:01:08 +0000902
903 if (command_obj)
904 return command_obj;
Jim Ingham279a6c22010-07-06 22:46:59 +0000905
Enrico Granata5342c442013-06-18 18:01:08 +0000906 command_obj = GetCommandSP (cmd_cstr, true, true, matches).get();
Jim Ingham279a6c22010-07-06 22:46:59 +0000907
Enrico Granata5342c442013-06-18 18:01:08 +0000908 if (command_obj)
909 return command_obj;
910
911 // If there wasn't an exact match then look for an inexact one in just the commands
Ed Masted78c9572014-04-20 00:31:37 +0000912 command_obj = GetCommandSP(cmd_cstr, false, false, nullptr).get();
Matt Kopec038ff812013-04-23 16:17:32 +0000913
914 // Finally, if there wasn't an inexact match among the commands, look for an inexact
915 // match in both the commands and aliases.
Enrico Granata5342c442013-06-18 18:01:08 +0000916
917 if (command_obj)
918 {
919 if (matches)
920 matches->AppendString(command_obj->GetCommandName());
921 return command_obj;
922 }
923
924 return GetCommandSP(cmd_cstr, true, false, matches).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000925}
926
927bool
928CommandInterpreter::CommandExists (const char *cmd)
929{
930 return m_command_dict.find(cmd) != m_command_dict.end();
931}
932
933bool
Caroline Ticeca90c472011-05-06 21:37:15 +0000934CommandInterpreter::ProcessAliasOptionsArgs (lldb::CommandObjectSP &cmd_obj_sp,
935 const char *options_args,
936 OptionArgVectorSP &option_arg_vector_sp)
937{
938 bool success = true;
939 OptionArgVector *option_arg_vector = option_arg_vector_sp.get();
940
941 if (!options_args || (strlen (options_args) < 1))
942 return true;
943
944 std::string options_string (options_args);
945 Args args (options_args);
946 CommandReturnObject result;
947 // Check to see if the command being aliased can take any command options.
948 Options *options = cmd_obj_sp->GetOptions ();
949 if (options)
950 {
951 // See if any options were specified as part of the alias; if so, handle them appropriately.
952 options->NotifyOptionParsingStarting ();
953 args.Unshift ("dummy_arg");
954 args.ParseAliasOptions (*options, result, option_arg_vector, options_string);
955 args.Shift ();
956 if (result.Succeeded())
957 options->VerifyPartialOptions (result);
958 if (!result.Succeeded() && result.GetStatus() != lldb::eReturnStatusStarted)
959 {
960 result.AppendError ("Unable to create requested alias.\n");
961 return false;
962 }
963 }
964
Greg Clayton5521f992011-10-28 21:38:01 +0000965 if (!options_string.empty())
Caroline Ticeca90c472011-05-06 21:37:15 +0000966 {
967 if (cmd_obj_sp->WantsRawCommandString ())
968 option_arg_vector->push_back (OptionArgPair ("<argument>",
969 OptionArgValue (-1,
970 options_string)));
971 else
972 {
Greg Claytonc7bece562013-01-25 18:06:21 +0000973 const size_t argc = args.GetArgumentCount();
Caroline Ticeca90c472011-05-06 21:37:15 +0000974 for (size_t i = 0; i < argc; ++i)
975 if (strcmp (args.GetArgumentAtIndex (i), "") != 0)
976 option_arg_vector->push_back
977 (OptionArgPair ("<argument>",
978 OptionArgValue (-1,
979 std::string (args.GetArgumentAtIndex (i)))));
980 }
981 }
982
983 return success;
984}
985
986bool
Jim Ingham298f3782013-04-03 00:25:49 +0000987CommandInterpreter::GetAliasFullName (const char *cmd, std::string &full_name)
988{
989 bool exact_match = (m_alias_dict.find(cmd) != m_alias_dict.end());
990 if (exact_match)
991 {
992 full_name.assign(cmd);
993 return exact_match;
994 }
995 else
996 {
997 StringList matches;
998 size_t num_alias_matches;
999 num_alias_matches = CommandObject::AddNamesMatchingPartialString (m_alias_dict, cmd, matches);
1000 if (num_alias_matches == 1)
1001 {
1002 // Make sure this isn't shadowing a command in the regular command space:
1003 StringList regular_matches;
1004 const bool include_aliases = false;
1005 const bool exact = false;
1006 CommandObjectSP cmd_obj_sp(GetCommandSP (cmd, include_aliases, exact, &regular_matches));
1007 if (cmd_obj_sp || regular_matches.GetSize() > 0)
1008 return false;
1009 else
1010 {
1011 full_name.assign (matches.GetStringAtIndex(0));
1012 return true;
1013 }
1014 }
1015 else
1016 return false;
1017 }
1018}
1019
1020bool
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001021CommandInterpreter::AliasExists (const char *cmd)
1022{
1023 return m_alias_dict.find(cmd) != m_alias_dict.end();
1024}
1025
1026bool
1027CommandInterpreter::UserCommandExists (const char *cmd)
1028{
1029 return m_user_dict.find(cmd) != m_user_dict.end();
1030}
1031
1032void
1033CommandInterpreter::AddAlias (const char *alias_name, CommandObjectSP& command_obj_sp)
1034{
Jim Ingham279a6c22010-07-06 22:46:59 +00001035 command_obj_sp->SetIsAlias (true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001036 m_alias_dict[alias_name] = command_obj_sp;
1037}
1038
1039bool
1040CommandInterpreter::RemoveAlias (const char *alias_name)
1041{
1042 CommandObject::CommandMap::iterator pos = m_alias_dict.find(alias_name);
1043 if (pos != m_alias_dict.end())
1044 {
1045 m_alias_dict.erase(pos);
1046 return true;
1047 }
1048 return false;
1049}
1050bool
1051CommandInterpreter::RemoveUser (const char *alias_name)
1052{
1053 CommandObject::CommandMap::iterator pos = m_user_dict.find(alias_name);
1054 if (pos != m_user_dict.end())
1055 {
1056 m_user_dict.erase(pos);
1057 return true;
1058 }
1059 return false;
1060}
1061
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001062void
1063CommandInterpreter::GetAliasHelp (const char *alias_name, const char *command_name, StreamString &help_string)
1064{
1065 help_string.Printf ("'%s", command_name);
1066 OptionArgVectorSP option_arg_vector_sp = GetAliasOptions (alias_name);
1067
Sean Callanan9a028512012-08-09 00:50:26 +00001068 if (option_arg_vector_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001069 {
1070 OptionArgVector *options = option_arg_vector_sp.get();
Andy Gibbsa297a972013-06-19 19:04:53 +00001071 for (size_t i = 0; i < options->size(); ++i)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001072 {
1073 OptionArgPair cur_option = (*options)[i];
1074 std::string opt = cur_option.first;
Caroline Ticed9d63362010-12-07 19:58:26 +00001075 OptionArgValue value_pair = cur_option.second;
1076 std::string value = value_pair.second;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001077 if (opt.compare("<argument>") == 0)
1078 {
1079 help_string.Printf (" %s", value.c_str());
1080 }
1081 else
1082 {
1083 help_string.Printf (" %s", opt.c_str());
1084 if ((value.compare ("<no-argument>") != 0)
1085 && (value.compare ("<need-argument") != 0))
1086 {
1087 help_string.Printf (" %s", value.c_str());
1088 }
1089 }
1090 }
1091 }
1092
1093 help_string.Printf ("'");
1094}
1095
Greg Clayton12fc3e02010-08-26 22:05:43 +00001096size_t
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001097CommandInterpreter::FindLongestCommandWord (CommandObject::CommandMap &dict)
1098{
1099 CommandObject::CommandMap::const_iterator pos;
Greg Clayton12fc3e02010-08-26 22:05:43 +00001100 CommandObject::CommandMap::const_iterator end = dict.end();
1101 size_t max_len = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001102
Greg Clayton12fc3e02010-08-26 22:05:43 +00001103 for (pos = dict.begin(); pos != end; ++pos)
1104 {
1105 size_t len = pos->first.size();
1106 if (max_len < len)
1107 max_len = len;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001108 }
Greg Clayton12fc3e02010-08-26 22:05:43 +00001109 return max_len;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001110}
1111
1112void
Enrico Granata223383e2011-08-16 23:24:13 +00001113CommandInterpreter::GetHelp (CommandReturnObject &result,
Enrico Granata08633ee2011-09-09 17:49:36 +00001114 uint32_t cmd_types)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001115{
1116 CommandObject::CommandMap::const_iterator pos;
Greg Claytonc7bece562013-01-25 18:06:21 +00001117 size_t max_len = FindLongestCommandWord (m_command_dict);
Enrico Granata223383e2011-08-16 23:24:13 +00001118
1119 if ( (cmd_types & eCommandTypesBuiltin) == eCommandTypesBuiltin )
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001120 {
Enrico Granata223383e2011-08-16 23:24:13 +00001121
1122 result.AppendMessage("The following is a list of built-in, permanent debugger commands:");
1123 result.AppendMessage("");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001124
Enrico Granata223383e2011-08-16 23:24:13 +00001125 for (pos = m_command_dict.begin(); pos != m_command_dict.end(); ++pos)
1126 {
1127 OutputFormattedHelpText (result.GetOutputStream(), pos->first.c_str(), "--", pos->second->GetHelp(),
1128 max_len);
1129 }
1130 result.AppendMessage("");
1131
1132 }
1133
Greg Clayton5521f992011-10-28 21:38:01 +00001134 if (!m_alias_dict.empty() && ( (cmd_types & eCommandTypesAliases) == eCommandTypesAliases ))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001135 {
Jim Ingham49e80a12010-10-22 18:47:16 +00001136 result.AppendMessage("The following is a list of your current command abbreviations "
Johnny Chenb89982d2011-04-21 00:39:18 +00001137 "(see 'help command alias' for more info):");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001138 result.AppendMessage("");
Greg Clayton12fc3e02010-08-26 22:05:43 +00001139 max_len = FindLongestCommandWord (m_alias_dict);
1140
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001141 for (pos = m_alias_dict.begin(); pos != m_alias_dict.end(); ++pos)
1142 {
1143 StreamString sstr;
1144 StreamString translation_and_help;
1145 std::string entry_name = pos->first;
1146 std::string second_entry = pos->second.get()->GetCommandName();
1147 GetAliasHelp (pos->first.c_str(), pos->second->GetCommandName(), sstr);
1148
1149 translation_and_help.Printf ("(%s) %s", sstr.GetData(), pos->second->GetHelp());
1150 OutputFormattedHelpText (result.GetOutputStream(), pos->first.c_str(), "--",
1151 translation_and_help.GetData(), max_len);
1152 }
1153 result.AppendMessage("");
1154 }
1155
Greg Clayton5521f992011-10-28 21:38:01 +00001156 if (!m_user_dict.empty() && ( (cmd_types & eCommandTypesUserDef) == eCommandTypesUserDef ))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001157 {
1158 result.AppendMessage ("The following is a list of your current user-defined commands:");
1159 result.AppendMessage("");
Enrico Granata223383e2011-08-16 23:24:13 +00001160 max_len = FindLongestCommandWord (m_user_dict);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001161 for (pos = m_user_dict.begin(); pos != m_user_dict.end(); ++pos)
1162 {
Enrico Granata223383e2011-08-16 23:24:13 +00001163 OutputFormattedHelpText (result.GetOutputStream(), pos->first.c_str(), "--", pos->second->GetHelp(),
1164 max_len);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001165 }
1166 result.AppendMessage("");
1167 }
1168
1169 result.AppendMessage("For more information on any particular command, try 'help <command-name>'.");
1170}
1171
Caroline Tice844d2302010-12-09 22:52:49 +00001172CommandObject *
1173CommandInterpreter::GetCommandObjectForCommand (std::string &command_string)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001174{
Caroline Tice844d2302010-12-09 22:52:49 +00001175 // This function finds the final, lowest-level, alias-resolved command object whose 'Execute' function will
1176 // eventually be invoked by the given command line.
1177
Ed Masted78c9572014-04-20 00:31:37 +00001178 CommandObject *cmd_obj = nullptr;
Caroline Tice844d2302010-12-09 22:52:49 +00001179 std::string white_space (" \t\v");
1180 size_t start = command_string.find_first_not_of (white_space);
1181 size_t end = 0;
1182 bool done = false;
1183 while (!done)
1184 {
1185 if (start != std::string::npos)
1186 {
1187 // Get the next word from command_string.
1188 end = command_string.find_first_of (white_space, start);
1189 if (end == std::string::npos)
1190 end = command_string.size();
1191 std::string cmd_word = command_string.substr (start, end - start);
1192
Ed Masted78c9572014-04-20 00:31:37 +00001193 if (cmd_obj == nullptr)
Caroline Tice844d2302010-12-09 22:52:49 +00001194 // Since cmd_obj is NULL we are on our first time through this loop. Check to see if cmd_word is a valid
1195 // command or alias.
1196 cmd_obj = GetCommandObject (cmd_word.c_str());
1197 else if (cmd_obj->IsMultiwordObject ())
1198 {
1199 // 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 +00001200 CommandObject *sub_cmd_obj = cmd_obj->GetSubcommandObject (cmd_word.c_str());
Caroline Tice844d2302010-12-09 22:52:49 +00001201 if (sub_cmd_obj)
1202 cmd_obj = sub_cmd_obj;
1203 else // cmd_word was not a valid sub-command word, so we are donee
1204 done = true;
1205 }
1206 else
1207 // We have a cmd_obj and it is not a multi-word object, so we are done.
1208 done = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001209
Caroline Tice844d2302010-12-09 22:52:49 +00001210 // If we didn't find a valid command object, or our command object is not a multi-word object, or
1211 // we are at the end of the command_string, then we are done. Otherwise, find the start of the
1212 // next word.
1213
1214 if (!cmd_obj || !cmd_obj->IsMultiwordObject() || end >= command_string.size())
1215 done = true;
1216 else
1217 start = command_string.find_first_not_of (white_space, end);
1218 }
1219 else
1220 // Unable to find any more words.
1221 done = true;
1222 }
1223
1224 if (end == command_string.size())
1225 command_string.clear();
1226 else
1227 command_string = command_string.substr(end);
1228
1229 return cmd_obj;
1230}
1231
Greg Clayton51964162011-10-25 00:36:27 +00001232static const char *k_white_space = " \t\v";
Greg Clayton5521f992011-10-28 21:38:01 +00001233static const char *k_valid_command_chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_";
Greg Clayton51964162011-10-25 00:36:27 +00001234static void
1235StripLeadingSpaces (std::string &s)
Caroline Tice844d2302010-12-09 22:52:49 +00001236{
Greg Clayton51964162011-10-25 00:36:27 +00001237 if (!s.empty())
Caroline Tice844d2302010-12-09 22:52:49 +00001238 {
Greg Clayton51964162011-10-25 00:36:27 +00001239 size_t pos = s.find_first_not_of (k_white_space);
1240 if (pos == std::string::npos)
1241 s.clear();
1242 else if (pos == 0)
1243 return;
1244 s.erase (0, pos);
1245 }
1246}
1247
Greg Clayton93c62e62011-11-09 23:25:03 +00001248static size_t
1249FindArgumentTerminator (const std::string &s)
1250{
Greg Clayton93c62e62011-11-09 23:25:03 +00001251 const size_t s_len = s.size();
1252 size_t offset = 0;
1253 while (offset < s_len)
1254 {
1255 size_t pos = s.find ("--", offset);
1256 if (pos == std::string::npos)
1257 break;
1258 if (pos > 0)
1259 {
1260 if (isspace(s[pos-1]))
1261 {
1262 // Check if the string ends "\s--" (where \s is a space character)
1263 // or if we have "\s--\s".
1264 if ((pos + 2 >= s_len) || isspace(s[pos+2]))
1265 {
Greg Clayton93c62e62011-11-09 23:25:03 +00001266 return pos;
1267 }
1268 }
1269 }
1270 offset = pos + 2;
1271 }
Greg Clayton93c62e62011-11-09 23:25:03 +00001272 return std::string::npos;
1273}
1274
Greg Clayton51964162011-10-25 00:36:27 +00001275static bool
Greg Clayton5521f992011-10-28 21:38:01 +00001276ExtractCommand (std::string &command_string, std::string &command, std::string &suffix, char &quote_char)
Greg Clayton51964162011-10-25 00:36:27 +00001277{
Greg Clayton5521f992011-10-28 21:38:01 +00001278 command.clear();
1279 suffix.clear();
Greg Clayton51964162011-10-25 00:36:27 +00001280 StripLeadingSpaces (command_string);
1281
1282 bool result = false;
1283 quote_char = '\0';
1284
1285 if (!command_string.empty())
1286 {
1287 const char first_char = command_string[0];
1288 if (first_char == '\'' || first_char == '"')
Caroline Tice844d2302010-12-09 22:52:49 +00001289 {
Greg Clayton51964162011-10-25 00:36:27 +00001290 quote_char = first_char;
1291 const size_t end_quote_pos = command_string.find (quote_char, 1);
1292 if (end_quote_pos == std::string::npos)
Caroline Tice2b5e8502011-05-11 16:07:06 +00001293 {
Greg Clayton5521f992011-10-28 21:38:01 +00001294 command.swap (command_string);
Greg Clayton51964162011-10-25 00:36:27 +00001295 command_string.erase ();
Caroline Tice2b5e8502011-05-11 16:07:06 +00001296 }
1297 else
1298 {
Greg Clayton5521f992011-10-28 21:38:01 +00001299 command.assign (command_string, 1, end_quote_pos - 1);
Greg Clayton51964162011-10-25 00:36:27 +00001300 if (end_quote_pos + 1 < command_string.size())
1301 command_string.erase (0, command_string.find_first_not_of (k_white_space, end_quote_pos + 1));
1302 else
1303 command_string.erase ();
Caroline Tice2b5e8502011-05-11 16:07:06 +00001304 }
Caroline Tice844d2302010-12-09 22:52:49 +00001305 }
1306 else
1307 {
Greg Clayton51964162011-10-25 00:36:27 +00001308 const size_t first_space_pos = command_string.find_first_of (k_white_space);
1309 if (first_space_pos == std::string::npos)
Caroline Tice2b5e8502011-05-11 16:07:06 +00001310 {
Greg Clayton5521f992011-10-28 21:38:01 +00001311 command.swap (command_string);
Greg Clayton51964162011-10-25 00:36:27 +00001312 command_string.erase();
Caroline Tice2b5e8502011-05-11 16:07:06 +00001313 }
1314 else
1315 {
Greg Clayton5521f992011-10-28 21:38:01 +00001316 command.assign (command_string, 0, first_space_pos);
1317 command_string.erase(0, command_string.find_first_not_of (k_white_space, first_space_pos));
Caroline Tice2b5e8502011-05-11 16:07:06 +00001318 }
Caroline Tice844d2302010-12-09 22:52:49 +00001319 }
Greg Clayton51964162011-10-25 00:36:27 +00001320 result = true;
Caroline Tice844d2302010-12-09 22:52:49 +00001321 }
Greg Clayton5521f992011-10-28 21:38:01 +00001322
1323
1324 if (!command.empty())
1325 {
1326 // actual commands can't start with '-' or '_'
1327 if (command[0] != '-' && command[0] != '_')
1328 {
1329 size_t pos = command.find_first_not_of(k_valid_command_chars);
1330 if (pos > 0 && pos != std::string::npos)
1331 {
1332 suffix.assign (command.begin() + pos, command.end());
1333 command.erase (pos);
1334 }
1335 }
1336 }
Greg Clayton51964162011-10-25 00:36:27 +00001337
1338 return result;
Caroline Tice844d2302010-12-09 22:52:49 +00001339}
1340
Greg Clayton5521f992011-10-28 21:38:01 +00001341CommandObject *
1342CommandInterpreter::BuildAliasResult (const char *alias_name,
1343 std::string &raw_input_string,
1344 std::string &alias_result,
1345 CommandReturnObject &result)
Caroline Tice844d2302010-12-09 22:52:49 +00001346{
Ed Masted78c9572014-04-20 00:31:37 +00001347 CommandObject *alias_cmd_obj = nullptr;
Caroline Tice844d2302010-12-09 22:52:49 +00001348 Args cmd_args (raw_input_string.c_str());
1349 alias_cmd_obj = GetCommandObject (alias_name);
1350 StreamString result_str;
1351
1352 if (alias_cmd_obj)
1353 {
1354 std::string alias_name_str = alias_name;
1355 if ((cmd_args.GetArgumentCount() == 0)
1356 || (alias_name_str.compare (cmd_args.GetArgumentAtIndex(0)) != 0))
1357 cmd_args.Unshift (alias_name);
1358
1359 result_str.Printf ("%s", alias_cmd_obj->GetCommandName ());
1360 OptionArgVectorSP option_arg_vector_sp = GetAliasOptions (alias_name);
1361
1362 if (option_arg_vector_sp.get())
1363 {
1364 OptionArgVector *option_arg_vector = option_arg_vector_sp.get();
1365
Andy Gibbsa297a972013-06-19 19:04:53 +00001366 for (size_t i = 0; i < option_arg_vector->size(); ++i)
Caroline Tice844d2302010-12-09 22:52:49 +00001367 {
1368 OptionArgPair option_pair = (*option_arg_vector)[i];
1369 OptionArgValue value_pair = option_pair.second;
1370 int value_type = value_pair.first;
1371 std::string option = option_pair.first;
1372 std::string value = value_pair.second;
1373 if (option.compare ("<argument>") == 0)
1374 result_str.Printf (" %s", value.c_str());
1375 else
1376 {
1377 result_str.Printf (" %s", option.c_str());
Virgile Belloe2607b52013-09-05 16:42:23 +00001378 if (value_type != OptionParser::eOptionalArgument)
Caroline Tice844d2302010-12-09 22:52:49 +00001379 result_str.Printf (" ");
Virgile Belloe2607b52013-09-05 16:42:23 +00001380 if (value.compare ("<OptionParser::eNoArgument>") != 0)
Caroline Tice844d2302010-12-09 22:52:49 +00001381 {
1382 int index = GetOptionArgumentPosition (value.c_str());
1383 if (index == 0)
1384 result_str.Printf ("%s", value.c_str());
Saleem Abdulrasool3985c8c2014-04-02 03:51:35 +00001385 else if (static_cast<size_t>(index) >= cmd_args.GetArgumentCount())
Caroline Tice844d2302010-12-09 22:52:49 +00001386 {
1387
1388 result.AppendErrorWithFormat
1389 ("Not enough arguments provided; you need at least %d arguments to use this alias.\n",
1390 index);
1391 result.SetStatus (eReturnStatusFailed);
Greg Clayton5521f992011-10-28 21:38:01 +00001392 return alias_cmd_obj;
Caroline Tice844d2302010-12-09 22:52:49 +00001393 }
1394 else
1395 {
1396 size_t strpos = raw_input_string.find (cmd_args.GetArgumentAtIndex (index));
1397 if (strpos != std::string::npos)
1398 raw_input_string = raw_input_string.erase (strpos,
1399 strlen (cmd_args.GetArgumentAtIndex (index)));
1400 result_str.Printf ("%s", cmd_args.GetArgumentAtIndex (index));
1401 }
1402 }
1403 }
1404 }
1405 }
1406
1407 alias_result = result_str.GetData();
1408 }
Greg Clayton5521f992011-10-28 21:38:01 +00001409 return alias_cmd_obj;
Caroline Tice844d2302010-12-09 22:52:49 +00001410}
1411
Greg Clayton5a314712011-10-14 07:41:33 +00001412Error
1413CommandInterpreter::PreprocessCommand (std::string &command)
1414{
1415 // The command preprocessor needs to do things to the command
1416 // line before any parsing of arguments or anything else is done.
1417 // The only current stuff that gets proprocessed is anyting enclosed
1418 // in backtick ('`') characters is evaluated as an expression and
1419 // the result of the expression must be a scalar that can be substituted
1420 // into the command. An example would be:
1421 // (lldb) memory read `$rsp + 20`
1422 Error error; // Error for any expressions that might not evaluate
1423 size_t start_backtick;
1424 size_t pos = 0;
1425 while ((start_backtick = command.find ('`', pos)) != std::string::npos)
1426 {
1427 if (start_backtick > 0 && command[start_backtick-1] == '\\')
1428 {
1429 // The backtick was preceeded by a '\' character, remove the slash
1430 // and don't treat the backtick as the start of an expression
1431 command.erase(start_backtick-1, 1);
1432 // No need to add one to start_backtick since we just deleted a char
1433 pos = start_backtick;
1434 }
1435 else
1436 {
1437 const size_t expr_content_start = start_backtick + 1;
1438 const size_t end_backtick = command.find ('`', expr_content_start);
1439 if (end_backtick == std::string::npos)
1440 return error;
1441 else if (end_backtick == expr_content_start)
1442 {
1443 // Empty expression (two backticks in a row)
1444 command.erase (start_backtick, 2);
1445 }
1446 else
1447 {
1448 std::string expr_str (command, expr_content_start, end_backtick - expr_content_start);
1449
Greg Clayton4e0fe8a2012-07-12 20:32:19 +00001450 ExecutionContext exe_ctx(GetExecutionContext());
1451 Target *target = exe_ctx.GetTargetPtr();
Johnny Chen51ea0ad2011-10-29 00:21:50 +00001452 // Get a dummy target to allow for calculator mode while processing backticks.
1453 // This also helps break the infinite loop caused when target is null.
1454 if (!target)
1455 target = Host::GetDummyTarget(GetDebugger()).get();
Greg Clayton5a314712011-10-14 07:41:33 +00001456 if (target)
1457 {
Greg Clayton5a314712011-10-14 07:41:33 +00001458 ValueObjectSP expr_result_valobj_sp;
Enrico Granatad4439aa2012-09-05 20:41:26 +00001459
Jim Ingham35e1bda2012-10-16 21:41:58 +00001460 EvaluateExpressionOptions options;
Jim Ingham6fbc48b2013-11-07 00:11:47 +00001461 options.SetCoerceToId(false);
1462 options.SetUnwindOnError(true);
1463 options.SetIgnoreBreakpoints(true);
1464 options.SetKeepInMemory(false);
1465 options.SetTryAllThreads(true);
1466 options.SetTimeoutUsec(0);
Enrico Granatad4439aa2012-09-05 20:41:26 +00001467
Jim Ingham1624a2d2014-05-05 02:26:40 +00001468 ExpressionResults expr_result = target->EvaluateExpression (expr_str.c_str(),
Enrico Granatad4439aa2012-09-05 20:41:26 +00001469 exe_ctx.GetFramePtr(),
Enrico Granata3372f582012-07-16 23:10:35 +00001470 expr_result_valobj_sp,
Enrico Granatad4439aa2012-09-05 20:41:26 +00001471 options);
1472
Jim Ingham8646d3c2014-05-05 02:47:44 +00001473 if (expr_result == eExpressionCompleted)
Greg Clayton5a314712011-10-14 07:41:33 +00001474 {
1475 Scalar scalar;
1476 if (expr_result_valobj_sp->ResolveValue (scalar))
1477 {
1478 command.erase (start_backtick, end_backtick - start_backtick + 1);
1479 StreamString value_strm;
1480 const bool show_type = false;
1481 scalar.GetValue (&value_strm, show_type);
1482 size_t value_string_size = value_strm.GetSize();
1483 if (value_string_size)
1484 {
1485 command.insert (start_backtick, value_strm.GetData(), value_string_size);
1486 pos = start_backtick + value_string_size;
1487 continue;
1488 }
1489 else
1490 {
1491 error.SetErrorStringWithFormat("expression value didn't result in a scalar value for the expression '%s'", expr_str.c_str());
1492 }
1493 }
1494 else
1495 {
1496 error.SetErrorStringWithFormat("expression value didn't result in a scalar value for the expression '%s'", expr_str.c_str());
1497 }
1498 }
1499 else
1500 {
1501 if (expr_result_valobj_sp)
1502 error = expr_result_valobj_sp->GetError();
1503 if (error.Success())
1504 {
1505
1506 switch (expr_result)
1507 {
Jim Ingham8646d3c2014-05-05 02:47:44 +00001508 case eExpressionSetupError:
Greg Clayton5a314712011-10-14 07:41:33 +00001509 error.SetErrorStringWithFormat("expression setup error for the expression '%s'", expr_str.c_str());
1510 break;
Jim Ingham8646d3c2014-05-05 02:47:44 +00001511 case eExpressionParseError:
Jim Ingham1624a2d2014-05-05 02:26:40 +00001512 error.SetErrorStringWithFormat ("expression parse error for the expression '%s'", expr_str.c_str());
1513 break;
Jim Ingham8646d3c2014-05-05 02:47:44 +00001514 case eExpressionResultUnavailable:
Jim Ingham1624a2d2014-05-05 02:26:40 +00001515 error.SetErrorStringWithFormat ("expression error fetching result for the expression '%s'", expr_str.c_str());
Jim Ingham8646d3c2014-05-05 02:47:44 +00001516 case eExpressionCompleted:
Greg Clayton5a314712011-10-14 07:41:33 +00001517 break;
Jim Ingham8646d3c2014-05-05 02:47:44 +00001518 case eExpressionDiscarded:
Greg Clayton5a314712011-10-14 07:41:33 +00001519 error.SetErrorStringWithFormat("expression discarded for the expression '%s'", expr_str.c_str());
1520 break;
Jim Ingham8646d3c2014-05-05 02:47:44 +00001521 case eExpressionInterrupted:
Greg Clayton5a314712011-10-14 07:41:33 +00001522 error.SetErrorStringWithFormat("expression interrupted for the expression '%s'", expr_str.c_str());
1523 break;
Jim Ingham8646d3c2014-05-05 02:47:44 +00001524 case eExpressionHitBreakpoint:
Jim Ingham184e9812013-01-15 02:47:48 +00001525 error.SetErrorStringWithFormat("expression hit breakpoint for the expression '%s'", expr_str.c_str());
1526 break;
Jim Ingham8646d3c2014-05-05 02:47:44 +00001527 case eExpressionTimedOut:
Greg Clayton5a314712011-10-14 07:41:33 +00001528 error.SetErrorStringWithFormat("expression timed out for the expression '%s'", expr_str.c_str());
1529 break;
Jim Ingham8646d3c2014-05-05 02:47:44 +00001530 case eExpressionStoppedForDebug:
Greg Claytonc28ce782013-11-08 23:38:26 +00001531 error.SetErrorStringWithFormat("expression stop at entry point for debugging for the expression '%s'", expr_str.c_str());
1532 break;
Greg Clayton5a314712011-10-14 07:41:33 +00001533 }
1534 }
1535 }
1536 }
1537 }
1538 if (error.Fail())
1539 break;
1540 }
1541 }
1542 return error;
1543}
1544
1545
Caroline Tice844d2302010-12-09 22:52:49 +00001546bool
1547CommandInterpreter::HandleCommand (const char *command_line,
Enrico Granata5f5ab602012-05-31 01:09:06 +00001548 LazyBool lazy_add_to_history,
Caroline Tice844d2302010-12-09 22:52:49 +00001549 CommandReturnObject &result,
Jim Inghame16c50a2011-02-18 00:54:25 +00001550 ExecutionContext *override_context,
Johnny Chen80fdd7c2011-10-05 00:42:59 +00001551 bool repeat_on_empty_command,
1552 bool no_context_switching)
Jim Inghame16c50a2011-02-18 00:54:25 +00001553
Caroline Tice844d2302010-12-09 22:52:49 +00001554{
Jim Inghame16c50a2011-02-18 00:54:25 +00001555
Caroline Tice844d2302010-12-09 22:52:49 +00001556 bool done = false;
Ed Masted78c9572014-04-20 00:31:37 +00001557 CommandObject *cmd_obj = nullptr;
Caroline Tice844d2302010-12-09 22:52:49 +00001558 bool wants_raw_input = false;
1559 std::string command_string (command_line);
Jim Inghama5a97eb2011-07-12 03:12:18 +00001560 std::string original_command_string (command_line);
Caroline Tice844d2302010-12-09 22:52:49 +00001561
Greg Clayton5160ce52013-03-27 23:08:40 +00001562 Log *log (lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_COMMANDS));
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00001563 Host::SetCrashDescriptionWithFormat ("HandleCommand(command = \"%s\")", command_line);
1564
1565 // Make a scoped cleanup object that will clear the crash description string
1566 // on exit of this function.
Ed Masted78c9572014-04-20 00:31:37 +00001567 lldb_utility::CleanUp <const char *> crash_description_cleanup(nullptr, Host::SetCrashDescription);
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00001568
Caroline Tice844d2302010-12-09 22:52:49 +00001569 if (log)
1570 log->Printf ("Processing command: %s", command_line);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001571
Jim Ingham30244832010-11-04 23:08:45 +00001572 Timer scoped_timer (__PRETTY_FUNCTION__, "Handling command: %s.", command_line);
1573
Johnny Chen80fdd7c2011-10-05 00:42:59 +00001574 if (!no_context_switching)
1575 UpdateExecutionContext (override_context);
Enrico Granata5f5ab602012-05-31 01:09:06 +00001576
Enrico Granata5f5ab602012-05-31 01:09:06 +00001577 bool add_to_history;
1578 if (lazy_add_to_history == eLazyBoolCalculate)
1579 add_to_history = (m_command_source_depth == 0);
1580 else
1581 add_to_history = (lazy_add_to_history == eLazyBoolYes);
1582
Jim Inghame16c50a2011-02-18 00:54:25 +00001583 bool empty_command = false;
1584 bool comment_command = false;
1585 if (command_string.empty())
1586 empty_command = true;
1587 else
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001588 {
Jim Inghame16c50a2011-02-18 00:54:25 +00001589 const char *k_space_characters = "\t\n\v\f\r ";
1590
1591 size_t non_space = command_string.find_first_not_of (k_space_characters);
1592 // Check for empty line or comment line (lines whose first
1593 // non-space character is the comment character for this interpreter)
1594 if (non_space == std::string::npos)
1595 empty_command = true;
1596 else if (command_string[non_space] == m_comment_char)
1597 comment_command = true;
Enrico Granata7594f142013-06-17 22:51:50 +00001598 else if (command_string[non_space] == CommandHistory::g_repeat_char)
Jim Inghama5a97eb2011-07-12 03:12:18 +00001599 {
Enrico Granata7594f142013-06-17 22:51:50 +00001600 const char *history_string = m_command_history.FindString(command_string.c_str() + non_space);
Ed Masted78c9572014-04-20 00:31:37 +00001601 if (history_string == nullptr)
Jim Inghama5a97eb2011-07-12 03:12:18 +00001602 {
1603 result.AppendErrorWithFormat ("Could not find entry: %s in history", command_string.c_str());
1604 result.SetStatus(eReturnStatusFailed);
1605 return false;
1606 }
1607 add_to_history = false;
1608 command_string = history_string;
1609 original_command_string = history_string;
1610 }
Jim Inghame16c50a2011-02-18 00:54:25 +00001611 }
1612
1613 if (empty_command)
1614 {
1615 if (repeat_on_empty_command)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001616 {
Enrico Granata7594f142013-06-17 22:51:50 +00001617 if (m_command_history.IsEmpty())
Jim Inghame16c50a2011-02-18 00:54:25 +00001618 {
1619 result.AppendError ("empty command");
1620 result.SetStatus(eReturnStatusFailed);
1621 return false;
1622 }
1623 else
1624 {
1625 command_line = m_repeat_command.c_str();
1626 command_string = command_line;
Jim Inghama5a97eb2011-07-12 03:12:18 +00001627 original_command_string = command_line;
Jim Inghame16c50a2011-02-18 00:54:25 +00001628 if (m_repeat_command.empty())
1629 {
1630 result.AppendErrorWithFormat("No auto repeat.\n");
1631 result.SetStatus (eReturnStatusFailed);
1632 return false;
1633 }
1634 }
1635 add_to_history = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001636 }
1637 else
1638 {
Jim Inghame16c50a2011-02-18 00:54:25 +00001639 result.SetStatus (eReturnStatusSuccessFinishNoResult);
1640 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001641 }
Jim Inghame16c50a2011-02-18 00:54:25 +00001642 }
1643 else if (comment_command)
1644 {
1645 result.SetStatus (eReturnStatusSuccessFinishNoResult);
1646 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001647 }
Caroline Tice2b5e8502011-05-11 16:07:06 +00001648
Greg Clayton5a314712011-10-14 07:41:33 +00001649
1650 Error error (PreprocessCommand (command_string));
1651
1652 if (error.Fail())
1653 {
1654 result.AppendError (error.AsCString());
1655 result.SetStatus(eReturnStatusFailed);
1656 return false;
1657 }
Caroline Tice844d2302010-12-09 22:52:49 +00001658 // Phase 1.
1659
1660 // Before we do ANY kind of argument processing, etc. we need to figure out what the real/final command object
1661 // is for the specified command, and whether or not it wants raw input. This gets complicated by the fact that
1662 // the user could have specified an alias, and in translating the alias there may also be command options and/or
1663 // even data (including raw text strings) that need to be found and inserted into the command line as part of
1664 // the translation. So this first step is plain look-up & replacement, resulting in three things: 1). the command
Greg Clayton710dd5a2011-01-08 20:28:42 +00001665 // object whose Execute method will actually be called; 2). a revised command string, with all substitutions &
Caroline Tice844d2302010-12-09 22:52:49 +00001666 // replacements taken care of; 3). whether or not the Execute function wants raw input or not.
Caroline Ticed9d63362010-12-07 19:58:26 +00001667
Caroline Tice844d2302010-12-09 22:52:49 +00001668 StreamString revised_command_line;
Caroline Tice01274c02010-12-11 08:16:56 +00001669 size_t actual_cmd_name_len = 0;
Greg Clayton5521f992011-10-28 21:38:01 +00001670 std::string next_word;
Filipe Cabecinhasaf1537f2012-05-16 23:25:54 +00001671 StringList matches;
Caroline Tice844d2302010-12-09 22:52:49 +00001672 while (!done)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001673 {
Caroline Tice2b5e8502011-05-11 16:07:06 +00001674 char quote_char = '\0';
Greg Clayton5521f992011-10-28 21:38:01 +00001675 std::string suffix;
1676 ExtractCommand (command_string, next_word, suffix, quote_char);
Ed Masted78c9572014-04-20 00:31:37 +00001677 if (cmd_obj == nullptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001678 {
Jim Ingham298f3782013-04-03 00:25:49 +00001679 std::string full_name;
1680 if (GetAliasFullName(next_word.c_str(), full_name))
Caroline Tice472362e2010-12-14 18:51:39 +00001681 {
Greg Clayton5521f992011-10-28 21:38:01 +00001682 std::string alias_result;
Jim Ingham298f3782013-04-03 00:25:49 +00001683 cmd_obj = BuildAliasResult (full_name.c_str(), command_string, alias_result, result);
Greg Clayton5521f992011-10-28 21:38:01 +00001684 revised_command_line.Printf ("%s", alias_result.c_str());
1685 if (cmd_obj)
1686 {
1687 wants_raw_input = cmd_obj->WantsRawCommandString ();
1688 actual_cmd_name_len = strlen (cmd_obj->GetCommandName());
1689 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001690 }
1691 else
1692 {
Filipe Cabecinhasaf1537f2012-05-16 23:25:54 +00001693 cmd_obj = GetCommandObject (next_word.c_str(), &matches);
Greg Clayton5521f992011-10-28 21:38:01 +00001694 if (cmd_obj)
1695 {
1696 actual_cmd_name_len += next_word.length();
1697 revised_command_line.Printf ("%s", next_word.c_str());
1698 wants_raw_input = cmd_obj->WantsRawCommandString ();
1699 }
Caroline Tice2b5e8502011-05-11 16:07:06 +00001700 else
Greg Clayton5521f992011-10-28 21:38:01 +00001701 {
1702 revised_command_line.Printf ("%s", next_word.c_str());
1703 }
Caroline Tice844d2302010-12-09 22:52:49 +00001704 }
1705 }
1706 else
1707 {
Greg Clayton5521f992011-10-28 21:38:01 +00001708 if (cmd_obj->IsMultiwordObject ())
1709 {
Greg Clayton998255b2012-10-13 02:07:45 +00001710 CommandObject *sub_cmd_obj = cmd_obj->GetSubcommandObject (next_word.c_str());
Greg Clayton5521f992011-10-28 21:38:01 +00001711 if (sub_cmd_obj)
1712 {
1713 actual_cmd_name_len += next_word.length() + 1;
1714 revised_command_line.Printf (" %s", next_word.c_str());
1715 cmd_obj = sub_cmd_obj;
1716 wants_raw_input = cmd_obj->WantsRawCommandString ();
1717 }
1718 else
1719 {
1720 if (quote_char)
1721 revised_command_line.Printf (" %c%s%s%c", quote_char, next_word.c_str(), suffix.c_str(), quote_char);
1722 else
1723 revised_command_line.Printf (" %s%s", next_word.c_str(), suffix.c_str());
1724 done = true;
1725 }
1726 }
Caroline Tice2b5e8502011-05-11 16:07:06 +00001727 else
Greg Clayton5521f992011-10-28 21:38:01 +00001728 {
1729 if (quote_char)
1730 revised_command_line.Printf (" %c%s%s%c", quote_char, next_word.c_str(), suffix.c_str(), quote_char);
1731 else
1732 revised_command_line.Printf (" %s%s", next_word.c_str(), suffix.c_str());
1733 done = true;
1734 }
Caroline Tice844d2302010-12-09 22:52:49 +00001735 }
1736
Ed Masted78c9572014-04-20 00:31:37 +00001737 if (cmd_obj == nullptr)
Caroline Tice844d2302010-12-09 22:52:49 +00001738 {
Greg Claytonc7bece562013-01-25 18:06:21 +00001739 const size_t num_matches = matches.GetSize();
Filipe Cabecinhasaf1537f2012-05-16 23:25:54 +00001740 if (matches.GetSize() > 1) {
Greg Claytonc7bece562013-01-25 18:06:21 +00001741 StreamString error_msg;
1742 error_msg.Printf ("Ambiguous command '%s'. Possible matches:\n", next_word.c_str());
Filipe Cabecinhasaf1537f2012-05-16 23:25:54 +00001743
1744 for (uint32_t i = 0; i < num_matches; ++i) {
Greg Claytonc7bece562013-01-25 18:06:21 +00001745 error_msg.Printf ("\t%s\n", matches.GetStringAtIndex(i));
Filipe Cabecinhasaf1537f2012-05-16 23:25:54 +00001746 }
Greg Claytonc7bece562013-01-25 18:06:21 +00001747 result.AppendRawError (error_msg.GetString().c_str());
Filipe Cabecinhasaf1537f2012-05-16 23:25:54 +00001748 } else {
1749 // We didn't have only one match, otherwise we wouldn't get here.
1750 assert(num_matches == 0);
1751 result.AppendErrorWithFormat ("'%s' is not a valid command.\n", next_word.c_str());
1752 }
Caroline Tice844d2302010-12-09 22:52:49 +00001753 result.SetStatus (eReturnStatusFailed);
1754 return false;
1755 }
1756
Greg Clayton5521f992011-10-28 21:38:01 +00001757 if (cmd_obj->IsMultiwordObject ())
1758 {
1759 if (!suffix.empty())
1760 {
1761
Enrico Granataaded51d2013-06-12 00:44:43 +00001762 result.AppendErrorWithFormat ("command '%s' did not recognize '%s%s%s' as valid (subcommand might be invalid).\n",
1763 cmd_obj->GetCommandName(),
1764 next_word.empty() ? "" : next_word.c_str(),
1765 next_word.empty() ? " -- " : " ",
Greg Clayton5521f992011-10-28 21:38:01 +00001766 suffix.c_str());
1767 result.SetStatus (eReturnStatusFailed);
1768 return false;
1769 }
1770 }
1771 else
1772 {
1773 // If we found a normal command, we are done
1774 done = true;
1775 if (!suffix.empty())
1776 {
1777 switch (suffix[0])
1778 {
1779 case '/':
1780 // GDB format suffixes
Greg Clayton52ec56c2011-10-29 00:57:28 +00001781 {
1782 Options *command_options = cmd_obj->GetOptions();
1783 if (command_options && command_options->SupportsLongOption("gdb-format"))
1784 {
Greg Clayton93c62e62011-11-09 23:25:03 +00001785 std::string gdb_format_option ("--gdb-format=");
1786 gdb_format_option += (suffix.c_str() + 1);
1787
1788 bool inserted = false;
1789 std::string &cmd = revised_command_line.GetString();
1790 size_t arg_terminator_idx = FindArgumentTerminator (cmd);
1791 if (arg_terminator_idx != std::string::npos)
1792 {
1793 // Insert the gdb format option before the "--" that terminates options
1794 gdb_format_option.append(1,' ');
1795 cmd.insert(arg_terminator_idx, gdb_format_option);
1796 inserted = true;
1797 }
1798
1799 if (!inserted)
1800 revised_command_line.Printf (" %s", gdb_format_option.c_str());
1801
1802 if (wants_raw_input && FindArgumentTerminator(cmd) == std::string::npos)
1803 revised_command_line.PutCString (" --");
Greg Clayton52ec56c2011-10-29 00:57:28 +00001804 }
1805 else
1806 {
1807 result.AppendErrorWithFormat ("the '%s' command doesn't support the --gdb-format option\n",
1808 cmd_obj->GetCommandName());
1809 result.SetStatus (eReturnStatusFailed);
1810 return false;
1811 }
1812 }
Greg Clayton5521f992011-10-28 21:38:01 +00001813 break;
Johnny Chen8e9383d2011-10-31 22:22:06 +00001814
1815 default:
1816 result.AppendErrorWithFormat ("unknown command shorthand suffix: '%s'\n",
1817 suffix.c_str());
1818 result.SetStatus (eReturnStatusFailed);
1819 return false;
1820
Greg Clayton5521f992011-10-28 21:38:01 +00001821 }
1822 }
1823 }
Caroline Tice844d2302010-12-09 22:52:49 +00001824 if (command_string.length() == 0)
1825 done = true;
1826
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001827 }
Caroline Tice844d2302010-12-09 22:52:49 +00001828
Greg Clayton5521f992011-10-28 21:38:01 +00001829 if (!command_string.empty())
Caroline Tice844d2302010-12-09 22:52:49 +00001830 revised_command_line.Printf (" %s", command_string.c_str());
1831
1832 // End of Phase 1.
1833 // At this point cmd_obj should contain the CommandObject whose Execute method will be called, if the command
1834 // specified was valid; revised_command_line contains the complete command line (including command name(s)),
1835 // fully translated with all substitutions & translations taken care of (still in raw text format); and
1836 // wants_raw_input specifies whether the Execute method expects raw input or not.
1837
1838
1839 if (log)
1840 {
1841 log->Printf ("HandleCommand, cmd_obj : '%s'", cmd_obj ? cmd_obj->GetCommandName() : "<not found>");
1842 log->Printf ("HandleCommand, revised_command_line: '%s'", revised_command_line.GetData());
1843 log->Printf ("HandleCommand, wants_raw_input:'%s'", wants_raw_input ? "True" : "False");
1844 }
1845
1846 // Phase 2.
1847 // Take care of things like setting up the history command & calling the appropriate Execute method on the
1848 // CommandObject, with the appropriate arguments.
1849
Ed Masted78c9572014-04-20 00:31:37 +00001850 if (cmd_obj != nullptr)
Caroline Tice844d2302010-12-09 22:52:49 +00001851 {
1852 if (add_to_history)
1853 {
1854 Args command_args (revised_command_line.GetData());
1855 const char *repeat_command = cmd_obj->GetRepeatCommand(command_args, 0);
Ed Masted78c9572014-04-20 00:31:37 +00001856 if (repeat_command != nullptr)
Caroline Tice844d2302010-12-09 22:52:49 +00001857 m_repeat_command.assign(repeat_command);
1858 else
Jim Inghama5a97eb2011-07-12 03:12:18 +00001859 m_repeat_command.assign(original_command_string.c_str());
Caroline Tice844d2302010-12-09 22:52:49 +00001860
Enrico Granata7594f142013-06-17 22:51:50 +00001861 m_command_history.AppendString (original_command_string);
Caroline Tice844d2302010-12-09 22:52:49 +00001862 }
1863
1864 command_string = revised_command_line.GetData();
1865 std::string command_name (cmd_obj->GetCommandName());
Caroline Tice01274c02010-12-11 08:16:56 +00001866 std::string remainder;
1867 if (actual_cmd_name_len < command_string.length())
1868 remainder = command_string.substr (actual_cmd_name_len); // Note: 'actual_cmd_name_len' may be considerably shorter
1869 // than cmd_obj->GetCommandName(), because name completion
1870 // allows users to enter short versions of the names,
1871 // e.g. 'br s' for 'breakpoint set'.
Caroline Tice844d2302010-12-09 22:52:49 +00001872
1873 // Remove any initial spaces
1874 std::string white_space (" \t\v");
1875 size_t pos = remainder.find_first_not_of (white_space);
1876 if (pos != 0 && pos != std::string::npos)
Greg Claytona3482592011-04-22 20:58:45 +00001877 remainder.erase(0, pos);
Caroline Tice844d2302010-12-09 22:52:49 +00001878
1879 if (log)
Jason Molendabfb36ff2011-08-25 00:20:04 +00001880 log->Printf ("HandleCommand, command line after removing command name(s): '%s'", remainder.c_str());
Caroline Tice844d2302010-12-09 22:52:49 +00001881
Jim Ingham5a988412012-06-08 21:56:10 +00001882 cmd_obj->Execute (remainder.c_str(), result);
Caroline Tice844d2302010-12-09 22:52:49 +00001883 }
1884 else
1885 {
1886 // We didn't find the first command object, so complete the first argument.
1887 Args command_args (revised_command_line.GetData());
1888 StringList matches;
1889 int num_matches;
1890 int cursor_index = 0;
1891 int cursor_char_position = strlen (command_args.GetArgumentAtIndex(0));
1892 bool word_complete;
1893 num_matches = HandleCompletionMatches (command_args,
1894 cursor_index,
1895 cursor_char_position,
1896 0,
1897 -1,
1898 word_complete,
1899 matches);
1900
1901 if (num_matches > 0)
1902 {
1903 std::string error_msg;
1904 error_msg.assign ("ambiguous command '");
1905 error_msg.append(command_args.GetArgumentAtIndex(0));
1906 error_msg.append ("'.");
1907
1908 error_msg.append (" Possible completions:");
1909 for (int i = 0; i < num_matches; i++)
1910 {
1911 error_msg.append ("\n\t");
1912 error_msg.append (matches.GetStringAtIndex (i));
1913 }
1914 error_msg.append ("\n");
Greg Claytonc7bece562013-01-25 18:06:21 +00001915 result.AppendRawError (error_msg.c_str());
Caroline Tice844d2302010-12-09 22:52:49 +00001916 }
1917 else
1918 result.AppendErrorWithFormat ("Unrecognized command '%s'.\n", command_args.GetArgumentAtIndex (0));
1919
1920 result.SetStatus (eReturnStatusFailed);
1921 }
1922
Jason Molendabfb36ff2011-08-25 00:20:04 +00001923 if (log)
1924 log->Printf ("HandleCommand, command %s", (result.Succeeded() ? "succeeded" : "did not succeed"));
1925
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001926 return result.Succeeded();
1927}
1928
1929int
1930CommandInterpreter::HandleCompletionMatches (Args &parsed_line,
1931 int &cursor_index,
1932 int &cursor_char_position,
1933 int match_start_point,
1934 int max_return_elements,
Jim Ingham558ce122010-06-30 05:02:46 +00001935 bool &word_complete,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001936 StringList &matches)
1937{
1938 int num_command_matches = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001939 bool look_for_subcommand = false;
Jim Ingham558ce122010-06-30 05:02:46 +00001940
1941 // For any of the command completions a unique match will be a complete word.
1942 word_complete = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001943
1944 if (cursor_index == -1)
1945 {
1946 // We got nothing on the command line, so return the list of commands
Jim Ingham279a6c22010-07-06 22:46:59 +00001947 bool include_aliases = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001948 num_command_matches = GetCommandNamesMatchingPartialString ("", include_aliases, matches);
1949 }
1950 else if (cursor_index == 0)
1951 {
1952 // The cursor is in the first argument, so just do a lookup in the dictionary.
Jim Ingham279a6c22010-07-06 22:46:59 +00001953 CommandObject *cmd_obj = GetCommandObject (parsed_line.GetArgumentAtIndex(0), &matches);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001954 num_command_matches = matches.GetSize();
1955
1956 if (num_command_matches == 1
1957 && cmd_obj && cmd_obj->IsMultiwordObject()
Ed Masted78c9572014-04-20 00:31:37 +00001958 && matches.GetStringAtIndex(0) != nullptr
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001959 && strcmp (parsed_line.GetArgumentAtIndex(0), matches.GetStringAtIndex(0)) == 0)
1960 {
Greg Clayton765d2e22013-12-10 19:14:04 +00001961 if (parsed_line.GetArgumentCount() == 1)
1962 {
1963 word_complete = true;
1964 }
1965 else
1966 {
1967 look_for_subcommand = true;
1968 num_command_matches = 0;
1969 matches.DeleteStringAtIndex(0);
1970 parsed_line.AppendArgument ("");
1971 cursor_index++;
1972 cursor_char_position = 0;
1973 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001974 }
1975 }
1976
1977 if (cursor_index > 0 || look_for_subcommand)
1978 {
1979 // We are completing further on into a commands arguments, so find the command and tell it
1980 // to complete the command.
1981 // First see if there is a matching initial command:
Jim Ingham279a6c22010-07-06 22:46:59 +00001982 CommandObject *command_object = GetCommandObject (parsed_line.GetArgumentAtIndex(0));
Ed Masted78c9572014-04-20 00:31:37 +00001983 if (command_object == nullptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001984 {
1985 return 0;
1986 }
1987 else
1988 {
1989 parsed_line.Shift();
1990 cursor_index--;
Greg Claytona7015092010-09-18 01:14:36 +00001991 num_command_matches = command_object->HandleCompletion (parsed_line,
Greg Clayton66111032010-06-23 01:19:29 +00001992 cursor_index,
1993 cursor_char_position,
1994 match_start_point,
Jim Ingham558ce122010-06-30 05:02:46 +00001995 max_return_elements,
1996 word_complete,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001997 matches);
1998 }
1999 }
2000
2001 return num_command_matches;
2002
2003}
2004
2005int
2006CommandInterpreter::HandleCompletion (const char *current_line,
2007 const char *cursor,
2008 const char *last_char,
2009 int match_start_point,
2010 int max_return_elements,
2011 StringList &matches)
2012{
2013 // We parse the argument up to the cursor, so the last argument in parsed_line is
2014 // the one containing the cursor, and the cursor is after the last character.
2015
2016 Args parsed_line(current_line, last_char - current_line);
2017 Args partial_parsed_line(current_line, cursor - current_line);
2018
Jim Inghama5a97eb2011-07-12 03:12:18 +00002019 // Don't complete comments, and if the line we are completing is just the history repeat character,
2020 // substitute the appropriate history line.
2021 const char *first_arg = parsed_line.GetArgumentAtIndex(0);
2022 if (first_arg)
2023 {
2024 if (first_arg[0] == m_comment_char)
2025 return 0;
Enrico Granata7594f142013-06-17 22:51:50 +00002026 else if (first_arg[0] == CommandHistory::g_repeat_char)
Jim Inghama5a97eb2011-07-12 03:12:18 +00002027 {
Enrico Granata7594f142013-06-17 22:51:50 +00002028 const char *history_string = m_command_history.FindString (first_arg);
Ed Masted78c9572014-04-20 00:31:37 +00002029 if (history_string != nullptr)
Jim Inghama5a97eb2011-07-12 03:12:18 +00002030 {
2031 matches.Clear();
2032 matches.InsertStringAtIndex(0, history_string);
2033 return -2;
2034 }
2035 else
2036 return 0;
2037
2038 }
2039 }
2040
2041
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002042 int num_args = partial_parsed_line.GetArgumentCount();
2043 int cursor_index = partial_parsed_line.GetArgumentCount() - 1;
2044 int cursor_char_position;
2045
2046 if (cursor_index == -1)
2047 cursor_char_position = 0;
2048 else
2049 cursor_char_position = strlen (partial_parsed_line.GetArgumentAtIndex(cursor_index));
Jim Inghamfe0c4252010-12-14 19:56:01 +00002050
2051 if (cursor > current_line && cursor[-1] == ' ')
2052 {
2053 // We are just after a space. If we are in an argument, then we will continue
2054 // parsing, but if we are between arguments, then we have to complete whatever the next
2055 // element would be.
2056 // We can distinguish the two cases because if we are in an argument (e.g. because the space is
2057 // protected by a quote) then the space will also be in the parsed argument...
2058
2059 const char *current_elem = partial_parsed_line.GetArgumentAtIndex(cursor_index);
2060 if (cursor_char_position == 0 || current_elem[cursor_char_position - 1] != ' ')
2061 {
Greg Clayton765d2e22013-12-10 19:14:04 +00002062 parsed_line.InsertArgumentAtIndex(cursor_index + 1, "", '\0');
Jim Inghamfe0c4252010-12-14 19:56:01 +00002063 cursor_index++;
2064 cursor_char_position = 0;
2065 }
2066 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002067
2068 int num_command_matches;
2069
2070 matches.Clear();
2071
2072 // Only max_return_elements == -1 is supported at present:
2073 assert (max_return_elements == -1);
Jim Ingham558ce122010-06-30 05:02:46 +00002074 bool word_complete;
Greg Clayton66111032010-06-23 01:19:29 +00002075 num_command_matches = HandleCompletionMatches (parsed_line,
2076 cursor_index,
2077 cursor_char_position,
2078 match_start_point,
Jim Ingham558ce122010-06-30 05:02:46 +00002079 max_return_elements,
2080 word_complete,
Greg Clayton66111032010-06-23 01:19:29 +00002081 matches);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002082
2083 if (num_command_matches <= 0)
2084 return num_command_matches;
2085
2086 if (num_args == 0)
2087 {
2088 // If we got an empty string, insert nothing.
2089 matches.InsertStringAtIndex(0, "");
2090 }
2091 else
2092 {
2093 // Now figure out if there is a common substring, and if so put that in element 0, otherwise
2094 // put an empty string in element 0.
2095 std::string command_partial_str;
2096 if (cursor_index >= 0)
Jim Ingham49e80a12010-10-22 18:47:16 +00002097 command_partial_str.assign(parsed_line.GetArgumentAtIndex(cursor_index),
2098 parsed_line.GetArgumentAtIndex(cursor_index) + cursor_char_position);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002099
2100 std::string common_prefix;
2101 matches.LongestCommonPrefix (common_prefix);
Greg Claytonc7bece562013-01-25 18:06:21 +00002102 const size_t partial_name_len = command_partial_str.size();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002103
2104 // If we matched a unique single command, add a space...
Jim Ingham558ce122010-06-30 05:02:46 +00002105 // Only do this if the completer told us this was a complete word, however...
2106 if (num_command_matches == 1 && word_complete)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002107 {
2108 char quote_char = parsed_line.GetArgumentQuoteCharAtIndex(cursor_index);
2109 if (quote_char != '\0')
2110 common_prefix.push_back(quote_char);
2111
2112 common_prefix.push_back(' ');
2113 }
2114 common_prefix.erase (0, partial_name_len);
2115 matches.InsertStringAtIndex(0, common_prefix.c_str());
2116 }
2117 return num_command_matches;
2118}
2119
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002120
2121CommandInterpreter::~CommandInterpreter ()
2122{
2123}
2124
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002125void
Greg Clayton44d93782014-01-27 23:43:24 +00002126CommandInterpreter::UpdatePrompt (const char *new_prompt)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002127{
Greg Clayton44d93782014-01-27 23:43:24 +00002128 EventSP prompt_change_event_sp (new Event(eBroadcastBitResetPrompt, new EventDataBytes (new_prompt)));;
2129 BroadcastEvent (prompt_change_event_sp);
2130 if (m_command_io_handler_sp)
2131 m_command_io_handler_sp->SetPrompt(new_prompt);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002132}
2133
Jim Ingham97a6dc72010-10-04 19:49:29 +00002134
2135bool
2136CommandInterpreter::Confirm (const char *message, bool default_answer)
2137{
Jim Ingham3bcdb292010-10-04 22:44:14 +00002138 // Check AutoConfirm first:
2139 if (m_debugger.GetAutoConfirm())
2140 return default_answer;
Greg Clayton44d93782014-01-27 23:43:24 +00002141
2142 IOHandlerConfirm *confirm = new IOHandlerConfirm(m_debugger,
2143 message,
2144 default_answer);
2145 IOHandlerSP io_handler_sp (confirm);
2146 m_debugger.RunIOHandler (io_handler_sp);
2147 return confirm->GetResponse();
Jim Ingham97a6dc72010-10-04 19:49:29 +00002148}
2149
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002150OptionArgVectorSP
2151CommandInterpreter::GetAliasOptions (const char *alias_name)
2152{
2153 OptionArgMap::iterator pos;
2154 OptionArgVectorSP ret_val;
2155
2156 std::string alias (alias_name);
2157
2158 if (HasAliasOptions())
2159 {
2160 pos = m_alias_options.find (alias);
2161 if (pos != m_alias_options.end())
2162 ret_val = pos->second;
2163 }
2164
2165 return ret_val;
2166}
2167
2168void
2169CommandInterpreter::RemoveAliasOptions (const char *alias_name)
2170{
2171 OptionArgMap::iterator pos = m_alias_options.find(alias_name);
2172 if (pos != m_alias_options.end())
2173 {
2174 m_alias_options.erase (pos);
2175 }
2176}
2177
2178void
2179CommandInterpreter::AddOrReplaceAliasOptions (const char *alias_name, OptionArgVectorSP &option_arg_vector_sp)
2180{
2181 m_alias_options[alias_name] = option_arg_vector_sp;
2182}
2183
2184bool
2185CommandInterpreter::HasCommands ()
2186{
2187 return (!m_command_dict.empty());
2188}
2189
2190bool
2191CommandInterpreter::HasAliases ()
2192{
2193 return (!m_alias_dict.empty());
2194}
2195
2196bool
2197CommandInterpreter::HasUserCommands ()
2198{
2199 return (!m_user_dict.empty());
2200}
2201
2202bool
2203CommandInterpreter::HasAliasOptions ()
2204{
2205 return (!m_alias_options.empty());
2206}
2207
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002208void
Caroline Tice4ab31c92010-10-12 21:57:09 +00002209CommandInterpreter::BuildAliasCommandArgs (CommandObject *alias_cmd_obj,
2210 const char *alias_name,
2211 Args &cmd_args,
Caroline Ticed9d63362010-12-07 19:58:26 +00002212 std::string &raw_input_string,
Caroline Tice4ab31c92010-10-12 21:57:09 +00002213 CommandReturnObject &result)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002214{
2215 OptionArgVectorSP option_arg_vector_sp = GetAliasOptions (alias_name);
Caroline Ticed9d63362010-12-07 19:58:26 +00002216
2217 bool wants_raw_input = alias_cmd_obj->WantsRawCommandString();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002218
Caroline Ticed9d63362010-12-07 19:58:26 +00002219 // Make sure that the alias name is the 0th element in cmd_args
2220 std::string alias_name_str = alias_name;
2221 if (alias_name_str.compare (cmd_args.GetArgumentAtIndex(0)) != 0)
2222 cmd_args.Unshift (alias_name);
2223
2224 Args new_args (alias_cmd_obj->GetCommandName());
2225 if (new_args.GetArgumentCount() == 2)
2226 new_args.Shift();
2227
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002228 if (option_arg_vector_sp.get())
2229 {
Caroline Ticed9d63362010-12-07 19:58:26 +00002230 if (wants_raw_input)
2231 {
2232 // We have a command that both has command options and takes raw input. Make *sure* it has a
2233 // " -- " in the right place in the raw_input_string.
2234 size_t pos = raw_input_string.find(" -- ");
2235 if (pos == std::string::npos)
2236 {
2237 // None found; assume it goes at the beginning of the raw input string
2238 raw_input_string.insert (0, " -- ");
2239 }
2240 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002241
2242 OptionArgVector *option_arg_vector = option_arg_vector_sp.get();
Greg Claytonc7bece562013-01-25 18:06:21 +00002243 const size_t old_size = cmd_args.GetArgumentCount();
Caroline Tice4ab31c92010-10-12 21:57:09 +00002244 std::vector<bool> used (old_size + 1, false);
2245
2246 used[0] = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002247
Andy Gibbsa297a972013-06-19 19:04:53 +00002248 for (size_t i = 0; i < option_arg_vector->size(); ++i)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002249 {
2250 OptionArgPair option_pair = (*option_arg_vector)[i];
Caroline Ticed9d63362010-12-07 19:58:26 +00002251 OptionArgValue value_pair = option_pair.second;
2252 int value_type = value_pair.first;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002253 std::string option = option_pair.first;
Caroline Ticed9d63362010-12-07 19:58:26 +00002254 std::string value = value_pair.second;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002255 if (option.compare ("<argument>") == 0)
Caroline Ticed9d63362010-12-07 19:58:26 +00002256 {
2257 if (!wants_raw_input
2258 || (value.compare("--") != 0)) // Since we inserted this above, make sure we don't insert it twice
2259 new_args.AppendArgument (value.c_str());
2260 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002261 else
2262 {
Virgile Belloe2607b52013-09-05 16:42:23 +00002263 if (value_type != OptionParser::eOptionalArgument)
Caroline Ticed9d63362010-12-07 19:58:26 +00002264 new_args.AppendArgument (option.c_str());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002265 if (value.compare ("<no-argument>") != 0)
2266 {
2267 int index = GetOptionArgumentPosition (value.c_str());
2268 if (index == 0)
Caroline Ticed9d63362010-12-07 19:58:26 +00002269 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002270 // value was NOT a positional argument; must be a real value
Virgile Belloe2607b52013-09-05 16:42:23 +00002271 if (value_type != OptionParser::eOptionalArgument)
Caroline Ticed9d63362010-12-07 19:58:26 +00002272 new_args.AppendArgument (value.c_str());
2273 else
2274 {
2275 char buffer[255];
2276 ::snprintf (buffer, sizeof (buffer), "%s%s", option.c_str(), value.c_str());
2277 new_args.AppendArgument (buffer);
2278 }
2279
2280 }
Saleem Abdulrasool3985c8c2014-04-02 03:51:35 +00002281 else if (static_cast<size_t>(index) >= cmd_args.GetArgumentCount())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002282 {
2283 result.AppendErrorWithFormat
2284 ("Not enough arguments provided; you need at least %d arguments to use this alias.\n",
2285 index);
2286 result.SetStatus (eReturnStatusFailed);
2287 return;
2288 }
2289 else
2290 {
Caroline Ticed9d63362010-12-07 19:58:26 +00002291 // Find and remove cmd_args.GetArgumentAtIndex(i) from raw_input_string
2292 size_t strpos = raw_input_string.find (cmd_args.GetArgumentAtIndex (index));
2293 if (strpos != std::string::npos)
2294 {
2295 raw_input_string = raw_input_string.erase (strpos, strlen (cmd_args.GetArgumentAtIndex (index)));
2296 }
2297
Virgile Belloe2607b52013-09-05 16:42:23 +00002298 if (value_type != OptionParser::eOptionalArgument)
Caroline Ticed9d63362010-12-07 19:58:26 +00002299 new_args.AppendArgument (cmd_args.GetArgumentAtIndex (index));
2300 else
2301 {
2302 char buffer[255];
2303 ::snprintf (buffer, sizeof(buffer), "%s%s", option.c_str(),
2304 cmd_args.GetArgumentAtIndex (index));
2305 new_args.AppendArgument (buffer);
2306 }
Caroline Tice4ab31c92010-10-12 21:57:09 +00002307 used[index] = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002308 }
2309 }
2310 }
2311 }
2312
Andy Gibbsa297a972013-06-19 19:04:53 +00002313 for (size_t j = 0; j < cmd_args.GetArgumentCount(); ++j)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002314 {
Caroline Ticed9d63362010-12-07 19:58:26 +00002315 if (!used[j] && !wants_raw_input)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002316 new_args.AppendArgument (cmd_args.GetArgumentAtIndex (j));
2317 }
2318
2319 cmd_args.Clear();
2320 cmd_args.SetArguments (new_args.GetArgumentCount(), (const char **) new_args.GetArgumentVector());
2321 }
2322 else
2323 {
2324 result.SetStatus (eReturnStatusSuccessFinishNoResult);
Caroline Ticed9d63362010-12-07 19:58:26 +00002325 // This alias was not created with any options; nothing further needs to be done, unless it is a command that
2326 // wants raw input, in which case we need to clear the rest of the data from cmd_args, since its in the raw
2327 // input string.
2328 if (wants_raw_input)
2329 {
2330 cmd_args.Clear();
2331 cmd_args.SetArguments (new_args.GetArgumentCount(), (const char **) new_args.GetArgumentVector());
2332 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002333 return;
2334 }
2335
2336 result.SetStatus (eReturnStatusSuccessFinishNoResult);
2337 return;
2338}
2339
2340
2341int
2342CommandInterpreter::GetOptionArgumentPosition (const char *in_string)
2343{
2344 int position = 0; // Any string that isn't an argument position, i.e. '%' followed by an integer, gets a position
2345 // of zero.
2346
2347 char *cptr = (char *) in_string;
2348
2349 // Does it start with '%'
2350 if (cptr[0] == '%')
2351 {
2352 ++cptr;
2353
2354 // Is the rest of it entirely digits?
2355 if (isdigit (cptr[0]))
2356 {
2357 const char *start = cptr;
2358 while (isdigit (cptr[0]))
2359 ++cptr;
2360
2361 // We've gotten to the end of the digits; are we at the end of the string?
2362 if (cptr[0] == '\0')
2363 position = atoi (start);
2364 }
2365 }
2366
2367 return position;
2368}
2369
2370void
2371CommandInterpreter::SourceInitFile (bool in_cwd, CommandReturnObject &result)
2372{
Jim Ingham16e0c682011-08-12 23:34:31 +00002373 FileSpec init_file;
Greg Clayton14a35512011-09-11 00:01:44 +00002374 if (in_cwd)
Jim Ingham16e0c682011-08-12 23:34:31 +00002375 {
Greg Clayton14a35512011-09-11 00:01:44 +00002376 // In the current working directory we don't load any program specific
2377 // .lldbinit files, we only look for a "./.lldbinit" file.
2378 if (m_skip_lldbinit_files)
2379 return;
2380
2381 init_file.SetFile ("./.lldbinit", true);
Jim Ingham16e0c682011-08-12 23:34:31 +00002382 }
Greg Clayton14a35512011-09-11 00:01:44 +00002383 else
Jim Ingham16e0c682011-08-12 23:34:31 +00002384 {
Greg Clayton14a35512011-09-11 00:01:44 +00002385 // If we aren't looking in the current working directory we are looking
2386 // in the home directory. We will first see if there is an application
2387 // specific ".lldbinit" file whose name is "~/.lldbinit" followed by a
2388 // "-" and the name of the program. If this file doesn't exist, we fall
2389 // back to just the "~/.lldbinit" file. We also obey any requests to not
2390 // load the init files.
Zachary Turnera21fee02014-08-21 21:49:24 +00002391 llvm::SmallString<64> home_dir_path;
2392 llvm::sys::path::home_directory(home_dir_path);
2393 FileSpec profilePath(home_dir_path.c_str(), false);
Zachary Turner9e757b72014-07-28 16:45:05 +00002394 profilePath.AppendPathComponent(".lldbinit");
2395 std::string init_file_path = profilePath.GetPath();
Greg Clayton14a35512011-09-11 00:01:44 +00002396
2397 if (m_skip_app_init_files == false)
2398 {
Zachary Turnera21fee02014-08-21 21:49:24 +00002399 FileSpec program_file_spec(HostInfo::GetProgramFileSpec());
Greg Clayton14a35512011-09-11 00:01:44 +00002400 const char *program_name = program_file_spec.GetFilename().AsCString();
Jim Ingham16e0c682011-08-12 23:34:31 +00002401
Greg Clayton14a35512011-09-11 00:01:44 +00002402 if (program_name)
2403 {
2404 char program_init_file_name[PATH_MAX];
Zachary Turner9e757b72014-07-28 16:45:05 +00002405 ::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 +00002406 init_file.SetFile (program_init_file_name, true);
2407 if (!init_file.Exists())
2408 init_file.Clear();
2409 }
2410 }
2411
2412 if (!init_file && !m_skip_lldbinit_files)
Zachary Turner9e757b72014-07-28 16:45:05 +00002413 init_file.SetFile (init_file_path.c_str(), false);
Greg Clayton14a35512011-09-11 00:01:44 +00002414 }
2415
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002416 // If the file exists, tell HandleCommand to 'source' it; this will do the actual broadcasting
2417 // of the commands back to any appropriate listener (see CommandObjectSource::Execute for more details).
2418
2419 if (init_file.Exists())
2420 {
Greg Clayton44d93782014-01-27 23:43:24 +00002421 const bool saved_batch = SetBatchCommandMode (true);
Greg Clayton340b0302014-02-05 17:57:57 +00002422 HandleCommandsFromFile (init_file,
Ed Masted78c9572014-04-20 00:31:37 +00002423 nullptr, // Execution context
Greg Clayton340b0302014-02-05 17:57:57 +00002424 eLazyBoolYes, // Stop on continue
2425 eLazyBoolNo, // Stop on error
2426 eLazyBoolNo, // Don't echo commands
2427 eLazyBoolNo, // Don't print command output
2428 eLazyBoolNo, // Don't add the commands that are sourced into the history buffer
2429 result);
Greg Clayton44d93782014-01-27 23:43:24 +00002430 SetBatchCommandMode (saved_batch);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002431 }
2432 else
2433 {
2434 // nothing to be done if the file doesn't exist
2435 result.SetStatus(eReturnStatusSuccessFinishNoResult);
2436 }
2437}
2438
Greg Clayton8b82f082011-04-12 05:54:46 +00002439PlatformSP
2440CommandInterpreter::GetPlatform (bool prefer_target_platform)
2441{
2442 PlatformSP platform_sp;
Greg Claytonc14ee322011-09-22 04:58:26 +00002443 if (prefer_target_platform)
2444 {
Greg Clayton4e0fe8a2012-07-12 20:32:19 +00002445 ExecutionContext exe_ctx(GetExecutionContext());
2446 Target *target = exe_ctx.GetTargetPtr();
Greg Claytonc14ee322011-09-22 04:58:26 +00002447 if (target)
2448 platform_sp = target->GetPlatform();
2449 }
Greg Clayton8b82f082011-04-12 05:54:46 +00002450
2451 if (!platform_sp)
2452 platform_sp = m_debugger.GetPlatformList().GetSelectedPlatform();
2453 return platform_sp;
2454}
2455
Jim Inghame16c50a2011-02-18 00:54:25 +00002456void
Jim Inghambad87fe2011-03-11 01:51:49 +00002457CommandInterpreter::HandleCommands (const StringList &commands,
Jim Inghame16c50a2011-02-18 00:54:25 +00002458 ExecutionContext *override_context,
2459 bool stop_on_continue,
2460 bool stop_on_error,
2461 bool echo_commands,
2462 bool print_results,
Enrico Granata5f5ab602012-05-31 01:09:06 +00002463 LazyBool add_to_history,
Jim Inghame16c50a2011-02-18 00:54:25 +00002464 CommandReturnObject &result)
2465{
2466 size_t num_lines = commands.GetSize();
Jim Inghame16c50a2011-02-18 00:54:25 +00002467
2468 // If we are going to continue past a "continue" then we need to run the commands synchronously.
2469 // Make sure you reset this value anywhere you return from the function.
2470
2471 bool old_async_execution = m_debugger.GetAsyncExecution();
2472
2473 // If we've been given an execution context, set it at the start, but don't keep resetting it or we will
2474 // cause series of commands that change the context, then do an operation that relies on that context to fail.
2475
Ed Masted78c9572014-04-20 00:31:37 +00002476 if (override_context != nullptr)
Greg Clayton8b82f082011-04-12 05:54:46 +00002477 UpdateExecutionContext (override_context);
Jim Inghame16c50a2011-02-18 00:54:25 +00002478
2479 if (!stop_on_continue)
2480 {
2481 m_debugger.SetAsyncExecution (false);
2482 }
2483
Andy Gibbsa297a972013-06-19 19:04:53 +00002484 for (size_t idx = 0; idx < num_lines; idx++)
Jim Inghame16c50a2011-02-18 00:54:25 +00002485 {
2486 const char *cmd = commands.GetStringAtIndex(idx);
2487 if (cmd[0] == '\0')
2488 continue;
2489
Jim Inghame16c50a2011-02-18 00:54:25 +00002490 if (echo_commands)
2491 {
2492 result.AppendMessageWithFormat ("%s %s\n",
Greg Clayton44d93782014-01-27 23:43:24 +00002493 m_debugger.GetPrompt(),
2494 cmd);
Jim Inghame16c50a2011-02-18 00:54:25 +00002495 }
2496
Greg Clayton9d0402b2011-02-20 02:15:07 +00002497 CommandReturnObject tmp_result;
Johnny Chen80fdd7c2011-10-05 00:42:59 +00002498 // If override_context is not NULL, pass no_context_switching = true for
2499 // HandleCommand() since we updated our context already.
Jim Ingham076b7fc2013-05-02 23:15:37 +00002500
2501 // We might call into a regex or alias command, in which case the add_to_history will get lost. This
2502 // m_command_source_depth dingus is the way we turn off adding to the history in that case, so set it up here.
2503 if (!add_to_history)
2504 m_command_source_depth++;
Enrico Granata5f5ab602012-05-31 01:09:06 +00002505 bool success = HandleCommand(cmd, add_to_history, tmp_result,
Ed Masted78c9572014-04-20 00:31:37 +00002506 nullptr, /* override_context */
Johnny Chen80fdd7c2011-10-05 00:42:59 +00002507 true, /* repeat_on_empty_command */
Ed Masted78c9572014-04-20 00:31:37 +00002508 override_context != nullptr /* no_context_switching */);
Jim Ingham076b7fc2013-05-02 23:15:37 +00002509 if (!add_to_history)
2510 m_command_source_depth--;
Jim Inghame16c50a2011-02-18 00:54:25 +00002511
2512 if (print_results)
2513 {
2514 if (tmp_result.Succeeded())
Jim Ingham85e8b812011-02-19 02:53:09 +00002515 result.AppendMessageWithFormat("%s", tmp_result.GetOutputData());
Jim Inghame16c50a2011-02-18 00:54:25 +00002516 }
2517
2518 if (!success || !tmp_result.Succeeded())
2519 {
Jim Inghama5038812012-04-24 02:25:07 +00002520 const char *error_msg = tmp_result.GetErrorData();
Ed Masted78c9572014-04-20 00:31:37 +00002521 if (error_msg == nullptr || error_msg[0] == '\0')
Jim Inghama5038812012-04-24 02:25:07 +00002522 error_msg = "<unknown error>.\n";
Jim Inghame16c50a2011-02-18 00:54:25 +00002523 if (stop_on_error)
2524 {
Deepak Panickal99fbc072014-03-03 15:39:47 +00002525 result.AppendErrorWithFormat("Aborting reading of commands after command #%" PRIu64 ": '%s' failed with %s",
2526 (uint64_t)idx, cmd, error_msg);
Jim Inghame16c50a2011-02-18 00:54:25 +00002527 result.SetStatus (eReturnStatusFailed);
2528 m_debugger.SetAsyncExecution (old_async_execution);
2529 return;
2530 }
2531 else if (print_results)
2532 {
Deepak Panickal99fbc072014-03-03 15:39:47 +00002533 result.AppendMessageWithFormat ("Command #%" PRIu64 " '%s' failed with %s",
2534 (uint64_t)idx + 1,
Jim Inghame16c50a2011-02-18 00:54:25 +00002535 cmd,
Jim Inghama5038812012-04-24 02:25:07 +00002536 error_msg);
Jim Inghame16c50a2011-02-18 00:54:25 +00002537 }
2538 }
2539
Caroline Tice969ed3d2011-05-02 20:41:46 +00002540 if (result.GetImmediateOutputStream())
2541 result.GetImmediateOutputStream()->Flush();
2542
2543 if (result.GetImmediateErrorStream())
2544 result.GetImmediateErrorStream()->Flush();
2545
Jim Inghame16c50a2011-02-18 00:54:25 +00002546 // N.B. Can't depend on DidChangeProcessState, because the state coming into the command execution
2547 // could be running (for instance in Breakpoint Commands.
2548 // So we check the return value to see if it is has running in it.
2549 if ((tmp_result.GetStatus() == eReturnStatusSuccessContinuingNoResult)
2550 || (tmp_result.GetStatus() == eReturnStatusSuccessContinuingResult))
2551 {
2552 if (stop_on_continue)
2553 {
2554 // If we caused the target to proceed, and we're going to stop in that case, set the
2555 // status in our real result before returning. This is an error if the continue was not the
2556 // last command in the set of commands to be run.
2557 if (idx != num_lines - 1)
Deepak Panickal99fbc072014-03-03 15:39:47 +00002558 result.AppendErrorWithFormat("Aborting reading of commands after command #%" PRIu64 ": '%s' continued the target.\n",
2559 (uint64_t)idx + 1, cmd);
Jim Inghame16c50a2011-02-18 00:54:25 +00002560 else
Deepak Panickal99fbc072014-03-03 15:39:47 +00002561 result.AppendMessageWithFormat("Command #%" PRIu64 " '%s' continued the target.\n", (uint64_t)idx + 1, cmd);
Jim Inghame16c50a2011-02-18 00:54:25 +00002562
2563 result.SetStatus(tmp_result.GetStatus());
2564 m_debugger.SetAsyncExecution (old_async_execution);
2565
2566 return;
2567 }
2568 }
2569
2570 }
2571
2572 result.SetStatus (eReturnStatusSuccessFinishResult);
2573 m_debugger.SetAsyncExecution (old_async_execution);
2574
2575 return;
2576}
2577
Greg Clayton340b0302014-02-05 17:57:57 +00002578// Make flags that we can pass into the IOHandler so our delegates can do the right thing
2579enum {
2580 eHandleCommandFlagStopOnContinue = (1u << 0),
2581 eHandleCommandFlagStopOnError = (1u << 1),
2582 eHandleCommandFlagEchoCommand = (1u << 2),
2583 eHandleCommandFlagPrintResult = (1u << 3)
2584};
2585
Jim Inghame16c50a2011-02-18 00:54:25 +00002586void
2587CommandInterpreter::HandleCommandsFromFile (FileSpec &cmd_file,
2588 ExecutionContext *context,
Greg Clayton340b0302014-02-05 17:57:57 +00002589 LazyBool stop_on_continue,
2590 LazyBool stop_on_error,
2591 LazyBool echo_command,
2592 LazyBool print_result,
Enrico Granata5f5ab602012-05-31 01:09:06 +00002593 LazyBool add_to_history,
Jim Inghame16c50a2011-02-18 00:54:25 +00002594 CommandReturnObject &result)
2595{
2596 if (cmd_file.Exists())
2597 {
Greg Clayton44d93782014-01-27 23:43:24 +00002598 StreamFileSP input_file_sp (new StreamFile());
2599
2600 std::string cmd_file_path = cmd_file.GetPath();
2601 Error error = input_file_sp->GetFile().Open(cmd_file_path.c_str(), File::eOpenOptionRead);
2602
2603 if (error.Success())
2604 {
2605 Debugger &debugger = GetDebugger();
2606
Greg Clayton340b0302014-02-05 17:57:57 +00002607 uint32_t flags = 0;
2608
2609 if (stop_on_continue == eLazyBoolCalculate)
2610 {
2611 if (m_command_source_flags.empty())
2612 {
Greg Claytone4e462c2014-02-05 21:03:22 +00002613 // Stop on continue by default
Greg Clayton340b0302014-02-05 17:57:57 +00002614 flags |= eHandleCommandFlagStopOnContinue;
2615 }
2616 else if (m_command_source_flags.back() & eHandleCommandFlagStopOnContinue)
2617 {
2618 flags |= eHandleCommandFlagStopOnContinue;
2619 }
2620 }
2621 else if (stop_on_continue == eLazyBoolYes)
2622 {
2623 flags |= eHandleCommandFlagStopOnContinue;
2624 }
2625
2626 if (stop_on_error == eLazyBoolCalculate)
2627 {
2628 if (m_command_source_flags.empty())
2629 {
2630 if (GetStopCmdSourceOnError())
2631 flags |= eHandleCommandFlagStopOnError;
2632 }
2633 else if (m_command_source_flags.back() & eHandleCommandFlagStopOnError)
2634 {
2635 flags |= eHandleCommandFlagStopOnError;
2636 }
2637 }
2638 else if (stop_on_error == eLazyBoolYes)
2639 {
2640 flags |= eHandleCommandFlagStopOnError;
2641 }
2642
2643 if (echo_command == eLazyBoolCalculate)
2644 {
2645 if (m_command_source_flags.empty())
2646 {
2647 // Echo command by default
2648 flags |= eHandleCommandFlagEchoCommand;
2649 }
2650 else if (m_command_source_flags.back() & eHandleCommandFlagEchoCommand)
2651 {
2652 flags |= eHandleCommandFlagEchoCommand;
2653 }
2654 }
2655 else if (echo_command == eLazyBoolYes)
2656 {
2657 flags |= eHandleCommandFlagEchoCommand;
2658 }
2659
2660 if (print_result == eLazyBoolCalculate)
2661 {
2662 if (m_command_source_flags.empty())
2663 {
Greg Claytone4e462c2014-02-05 21:03:22 +00002664 // Print output by default
2665 flags |= eHandleCommandFlagPrintResult;
Greg Clayton340b0302014-02-05 17:57:57 +00002666 }
Greg Claytone4e462c2014-02-05 21:03:22 +00002667 else if (m_command_source_flags.back() & eHandleCommandFlagPrintResult)
Greg Clayton340b0302014-02-05 17:57:57 +00002668 {
Greg Claytone4e462c2014-02-05 21:03:22 +00002669 flags |= eHandleCommandFlagPrintResult;
Greg Clayton340b0302014-02-05 17:57:57 +00002670 }
2671 }
2672 else if (print_result == eLazyBoolYes)
2673 {
Greg Claytone4e462c2014-02-05 21:03:22 +00002674 flags |= eHandleCommandFlagPrintResult;
2675 }
2676
2677 if (flags & eHandleCommandFlagPrintResult)
2678 {
Greg Clayton8ee67312014-02-05 21:46:20 +00002679 debugger.GetOutputFile()->Printf("Executing commands in '%s'.\n", cmd_file_path.c_str());
Greg Clayton340b0302014-02-05 17:57:57 +00002680 }
2681
2682 // Used for inheriting the right settings when "command source" might have
2683 // nested "command source" commands
Greg Claytone4e462c2014-02-05 21:03:22 +00002684 lldb::StreamFileSP empty_stream_sp;
Greg Clayton340b0302014-02-05 17:57:57 +00002685 m_command_source_flags.push_back(flags);
Greg Clayton44d93782014-01-27 23:43:24 +00002686 IOHandlerSP io_handler_sp (new IOHandlerEditline (debugger,
2687 input_file_sp,
Greg Claytone4e462c2014-02-05 21:03:22 +00002688 empty_stream_sp, // Pass in an empty stream so we inherit the top input reader output stream
2689 empty_stream_sp, // Pass in an empty stream so we inherit the top input reader error stream
Greg Clayton340b0302014-02-05 17:57:57 +00002690 flags,
Ed Masted78c9572014-04-20 00:31:37 +00002691 nullptr, // Pass in NULL for "editline_name" so no history is saved, or written
Greg Clayton8ee67312014-02-05 21:46:20 +00002692 debugger.GetPrompt(),
Greg Clayton44d93782014-01-27 23:43:24 +00002693 false, // Not multi-line
Greg Claytonf6913cd2014-03-07 00:53:24 +00002694 0,
Greg Clayton44d93782014-01-27 23:43:24 +00002695 *this));
Greg Clayton8ee67312014-02-05 21:46:20 +00002696 const bool old_async_execution = debugger.GetAsyncExecution();
2697
2698 // Set synchronous execution if we not stopping when we continue
2699 if ((flags & eHandleCommandFlagStopOnContinue) == 0)
2700 debugger.SetAsyncExecution (false);
2701
Greg Clayton340b0302014-02-05 17:57:57 +00002702 m_command_source_depth++;
Greg Clayton8ee67312014-02-05 21:46:20 +00002703
2704 debugger.RunIOHandler(io_handler_sp);
Greg Clayton340b0302014-02-05 17:57:57 +00002705 if (!m_command_source_flags.empty())
2706 m_command_source_flags.pop_back();
2707 m_command_source_depth--;
Greg Clayton44d93782014-01-27 23:43:24 +00002708 result.SetStatus (eReturnStatusSuccessFinishNoResult);
Greg Clayton8ee67312014-02-05 21:46:20 +00002709 debugger.SetAsyncExecution (old_async_execution);
Greg Clayton44d93782014-01-27 23:43:24 +00002710 }
2711 else
2712 {
2713 result.AppendErrorWithFormat ("error: an error occurred read file '%s': %s\n", cmd_file_path.c_str(), error.AsCString());
2714 result.SetStatus (eReturnStatusFailed);
2715 }
Greg Clayton8ee67312014-02-05 21:46:20 +00002716
2717
Jim Inghame16c50a2011-02-18 00:54:25 +00002718 }
2719 else
2720 {
2721 result.AppendErrorWithFormat ("Error reading commands from file %s - file not found.\n",
2722 cmd_file.GetFilename().AsCString());
2723 result.SetStatus (eReturnStatusFailed);
2724 return;
2725 }
2726}
2727
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002728ScriptInterpreter *
Enrico Granatab5887262012-10-29 21:18:03 +00002729CommandInterpreter::GetScriptInterpreter (bool can_create)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002730{
Ed Masted78c9572014-04-20 00:31:37 +00002731 if (m_script_interpreter_ap.get() != nullptr)
Enrico Granatab5887262012-10-29 21:18:03 +00002732 return m_script_interpreter_ap.get();
2733
2734 if (!can_create)
Ed Masted78c9572014-04-20 00:31:37 +00002735 return nullptr;
Enrico Granatab5887262012-10-29 21:18:03 +00002736
Enrico Granataa29bdad2012-07-10 18:23:48 +00002737 // <rdar://problem/11751427>
2738 // we need to protect the initialization of the script interpreter
2739 // otherwise we could end up with two threads both trying to create
2740 // their instance of it, and for some languages (e.g. Python)
2741 // this is a bulletproof recipe for disaster!
2742 // this needs to be a function-level static because multiple Debugger instances living in the same process
2743 // still need to be isolated and not try to initialize Python concurrently
Enrico Granata8b95df22012-07-10 19:04:14 +00002744 static Mutex g_interpreter_mutex(Mutex::eMutexTypeRecursive);
2745 Mutex::Locker interpreter_lock(g_interpreter_mutex);
Enrico Granataa29bdad2012-07-10 18:23:48 +00002746
Greg Clayton5160ce52013-03-27 23:08:40 +00002747 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Enrico Granatab5887262012-10-29 21:18:03 +00002748 if (log)
2749 log->Printf("Initializing the ScriptInterpreter now\n");
Greg Clayton66111032010-06-23 01:19:29 +00002750
Caroline Tice2f88aad2011-01-14 00:29:16 +00002751 lldb::ScriptLanguage script_lang = GetDebugger().GetScriptLanguage();
2752 switch (script_lang)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002753 {
Greg Claytondce502e2011-11-04 03:34:56 +00002754 case eScriptLanguagePython:
2755#ifndef LLDB_DISABLE_PYTHON
2756 m_script_interpreter_ap.reset (new ScriptInterpreterPython (*this));
2757 break;
2758#else
2759 // Fall through to the None case when python is disabled
2760#endif
Caroline Tice2f88aad2011-01-14 00:29:16 +00002761 case eScriptLanguageNone:
2762 m_script_interpreter_ap.reset (new ScriptInterpreterNone (*this));
2763 break;
Caroline Tice2f88aad2011-01-14 00:29:16 +00002764 };
2765
2766 return m_script_interpreter_ap.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002767}
2768
2769
2770
2771bool
2772CommandInterpreter::GetSynchronous ()
2773{
2774 return m_synchronous_execution;
2775}
2776
2777void
2778CommandInterpreter::SetSynchronous (bool value)
2779{
Johnny Chenc066ab42010-10-14 01:22:03 +00002780 m_synchronous_execution = value;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002781}
2782
2783void
2784CommandInterpreter::OutputFormattedHelpText (Stream &strm,
2785 const char *word_text,
2786 const char *separator,
2787 const char *help_text,
Greg Claytonc7bece562013-01-25 18:06:21 +00002788 size_t max_word_len)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002789{
Greg Claytona7015092010-09-18 01:14:36 +00002790 const uint32_t max_columns = m_debugger.GetTerminalWidth();
2791
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002792 int indent_size = max_word_len + strlen (separator) + 2;
2793
2794 strm.IndentMore (indent_size);
Greg Claytonbfe5f3b2011-02-18 01:44:25 +00002795
2796 StreamString text_strm;
Greg Claytonc7bece562013-01-25 18:06:21 +00002797 text_strm.Printf ("%-*s %s %s", (int)max_word_len, word_text, separator, help_text);
Greg Claytonbfe5f3b2011-02-18 01:44:25 +00002798
2799 size_t len = text_strm.GetSize();
2800 const char *text = text_strm.GetData();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002801 if (text[len - 1] == '\n')
Greg Claytonbfe5f3b2011-02-18 01:44:25 +00002802 {
2803 text_strm.EOL();
2804 len = text_strm.GetSize();
2805 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002806
2807 if (len < max_columns)
2808 {
2809 // Output it as a single line.
2810 strm.Printf ("%s", text);
2811 }
2812 else
2813 {
2814 // We need to break it up into multiple lines.
2815 bool first_line = true;
2816 int text_width;
Greg Claytonc7bece562013-01-25 18:06:21 +00002817 size_t start = 0;
2818 size_t end = start;
2819 const size_t final_end = strlen (text);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002820
2821 while (end < final_end)
2822 {
2823 if (first_line)
2824 text_width = max_columns - 1;
2825 else
2826 text_width = max_columns - indent_size - 1;
2827
2828 // Don't start the 'text' on a space, since we're already outputting the indentation.
2829 if (!first_line)
2830 {
2831 while ((start < final_end) && (text[start] == ' '))
2832 start++;
2833 }
2834
2835 end = start + text_width;
2836 if (end > final_end)
2837 end = final_end;
2838 else
2839 {
2840 // If we're not at the end of the text, make sure we break the line on white space.
2841 while (end > start
2842 && text[end] != ' ' && text[end] != '\t' && text[end] != '\n')
2843 end--;
Greg Clayton67cc0632012-08-22 17:17:09 +00002844 assert (end > 0);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002845 }
2846
Greg Claytonc7bece562013-01-25 18:06:21 +00002847 const size_t sub_len = end - start;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002848 if (start != 0)
2849 strm.EOL();
2850 if (!first_line)
2851 strm.Indent();
2852 else
2853 first_line = false;
2854 assert (start <= final_end);
2855 assert (start + sub_len <= final_end);
2856 if (sub_len > 0)
2857 strm.Write (text + start, sub_len);
2858 start = end + 1;
2859 }
2860 }
2861 strm.EOL();
2862 strm.IndentLess(indent_size);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002863}
2864
2865void
Enrico Granata82a7d982011-07-07 00:38:40 +00002866CommandInterpreter::OutputHelpText (Stream &strm,
2867 const char *word_text,
2868 const char *separator,
2869 const char *help_text,
2870 uint32_t max_word_len)
2871{
2872 int indent_size = max_word_len + strlen (separator) + 2;
2873
2874 strm.IndentMore (indent_size);
2875
2876 StreamString text_strm;
2877 text_strm.Printf ("%-*s %s %s", max_word_len, word_text, separator, help_text);
2878
2879 const uint32_t max_columns = m_debugger.GetTerminalWidth();
Enrico Granata82a7d982011-07-07 00:38:40 +00002880
2881 size_t len = text_strm.GetSize();
2882 const char *text = text_strm.GetData();
2883
2884 uint32_t chars_left = max_columns;
2885
2886 for (uint32_t i = 0; i < len; i++)
2887 {
2888 if ((text[i] == ' ' && ::strchr((text+i+1), ' ') && chars_left < ::strchr((text+i+1), ' ')-(text+i)) || text[i] == '\n')
2889 {
Enrico Granata82a7d982011-07-07 00:38:40 +00002890 chars_left = max_columns - indent_size;
2891 strm.EOL();
2892 strm.Indent();
2893 }
2894 else
2895 {
2896 strm.PutChar(text[i]);
2897 chars_left--;
2898 }
2899
2900 }
2901
2902 strm.EOL();
2903 strm.IndentLess(indent_size);
2904}
2905
2906void
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002907CommandInterpreter::FindCommandsForApropos (const char *search_word, StringList &commands_found,
Jim Inghamaf3753e2013-05-17 01:30:37 +00002908 StringList &commands_help, bool search_builtin_commands, bool search_user_commands)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002909{
2910 CommandObject::CommandMap::const_iterator pos;
2911
Jim Inghamaf3753e2013-05-17 01:30:37 +00002912 if (search_builtin_commands)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002913 {
Jim Inghamaf3753e2013-05-17 01:30:37 +00002914 for (pos = m_command_dict.begin(); pos != m_command_dict.end(); ++pos)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002915 {
Jim Inghamaf3753e2013-05-17 01:30:37 +00002916 const char *command_name = pos->first.c_str();
2917 CommandObject *cmd_obj = pos->second.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002918
Jim Inghamaf3753e2013-05-17 01:30:37 +00002919 if (cmd_obj->HelpTextContainsWord (search_word))
2920 {
2921 commands_found.AppendString (command_name);
2922 commands_help.AppendString (cmd_obj->GetHelp());
2923 }
2924
2925 if (cmd_obj->IsMultiwordObject())
2926 cmd_obj->AproposAllSubCommands (command_name,
2927 search_word,
2928 commands_found,
2929 commands_help);
2930
2931 }
2932 }
2933
2934 if (search_user_commands)
2935 {
2936 for (pos = m_user_dict.begin(); pos != m_user_dict.end(); ++pos)
2937 {
2938 const char *command_name = pos->first.c_str();
2939 CommandObject *cmd_obj = pos->second.get();
2940
2941 if (cmd_obj->HelpTextContainsWord (search_word))
2942 {
2943 commands_found.AppendString (command_name);
2944 commands_help.AppendString (cmd_obj->GetHelp());
2945 }
2946
2947 if (cmd_obj->IsMultiwordObject())
2948 cmd_obj->AproposAllSubCommands (command_name,
2949 search_word,
2950 commands_found,
2951 commands_help);
2952
2953 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002954 }
2955}
Greg Clayton8b82f082011-04-12 05:54:46 +00002956
Greg Clayton8b82f082011-04-12 05:54:46 +00002957void
2958CommandInterpreter::UpdateExecutionContext (ExecutionContext *override_context)
2959{
Ed Masted78c9572014-04-20 00:31:37 +00002960 if (override_context != nullptr)
Greg Clayton8b82f082011-04-12 05:54:46 +00002961 {
Greg Clayton4e0fe8a2012-07-12 20:32:19 +00002962 m_exe_ctx_ref = *override_context;
Greg Clayton8b82f082011-04-12 05:54:46 +00002963 }
2964 else
2965 {
Greg Clayton4e0fe8a2012-07-12 20:32:19 +00002966 const bool adopt_selected = true;
2967 m_exe_ctx_ref.SetTargetPtr (m_debugger.GetSelectedTarget().get(), adopt_selected);
Greg Clayton8b82f082011-04-12 05:54:46 +00002968 }
2969}
Greg Clayton44d93782014-01-27 23:43:24 +00002970
2971
2972size_t
2973CommandInterpreter::GetProcessOutput ()
2974{
2975 // The process has stuff waiting for stderr; get it and write it out to the appropriate place.
2976 char stdio_buffer[1024];
2977 size_t len;
2978 size_t total_bytes = 0;
2979 Error error;
2980 TargetSP target_sp (m_debugger.GetTargetList().GetSelectedTarget());
2981 if (target_sp)
2982 {
2983 ProcessSP process_sp (target_sp->GetProcessSP());
2984 if (process_sp)
2985 {
2986 while ((len = process_sp->GetSTDOUT (stdio_buffer, sizeof (stdio_buffer), error)) > 0)
2987 {
2988 size_t bytes_written = len;
2989 m_debugger.GetOutputFile()->Write (stdio_buffer, bytes_written);
2990 total_bytes += len;
2991 }
2992 while ((len = process_sp->GetSTDERR (stdio_buffer, sizeof (stdio_buffer), error)) > 0)
2993 {
2994 size_t bytes_written = len;
2995 m_debugger.GetErrorFile()->Write (stdio_buffer, bytes_written);
2996 total_bytes += len;
2997 }
2998 }
2999 }
3000 return total_bytes;
3001}
3002
3003void
3004CommandInterpreter::IOHandlerInputComplete (IOHandler &io_handler, std::string &line)
3005{
Greg Clayton340b0302014-02-05 17:57:57 +00003006 const bool is_interactive = io_handler.GetIsInteractive();
3007 if (is_interactive == false)
3008 {
3009 // When we are not interactive, don't execute blank lines. This will happen
3010 // sourcing a commands file. We don't want blank lines to repeat the previous
3011 // command and cause any errors to occur (like redefining an alias, get an error
3012 // and stop parsing the commands file).
3013 if (line.empty())
3014 return;
3015
3016 // When using a non-interactive file handle (like when sourcing commands from a file)
3017 // we need to echo the command out so we don't just see the command output and no
3018 // command...
3019 if (io_handler.GetFlags().Test(eHandleCommandFlagEchoCommand))
3020 io_handler.GetOutputStreamFile()->Printf("%s%s\n", io_handler.GetPrompt(), line.c_str());
3021 }
3022
Greg Clayton44d93782014-01-27 23:43:24 +00003023 lldb_private::CommandReturnObject result;
3024 HandleCommand(line.c_str(), eLazyBoolCalculate, result);
3025
Greg Clayton44d93782014-01-27 23:43:24 +00003026 // Now emit the command output text from the command we just executed
Greg Clayton340b0302014-02-05 17:57:57 +00003027 if (io_handler.GetFlags().Test(eHandleCommandFlagPrintResult))
3028 {
3029 // Display any STDOUT/STDERR _prior_ to emitting the command result text
3030 GetProcessOutput ();
3031
Greg Clayton8ee67312014-02-05 21:46:20 +00003032 if (!result.GetImmediateOutputStream())
3033 {
3034 const char *output = result.GetOutputData();
3035 if (output && output[0])
3036 io_handler.GetOutputStreamFile()->PutCString(output);
3037 }
Greg Clayton44d93782014-01-27 23:43:24 +00003038
Greg Clayton340b0302014-02-05 17:57:57 +00003039 // Now emit the command error text from the command we just executed
Greg Clayton8ee67312014-02-05 21:46:20 +00003040 if (!result.GetImmediateErrorStream())
3041 {
3042 const char *error = result.GetErrorData();
3043 if (error && error[0])
3044 io_handler.GetErrorStreamFile()->PutCString(error);
3045 }
Greg Clayton340b0302014-02-05 17:57:57 +00003046 }
Greg Clayton44d93782014-01-27 23:43:24 +00003047
3048 switch (result.GetStatus())
3049 {
3050 case eReturnStatusInvalid:
3051 case eReturnStatusSuccessFinishNoResult:
3052 case eReturnStatusSuccessFinishResult:
Greg Clayton340b0302014-02-05 17:57:57 +00003053 case eReturnStatusStarted:
3054 break;
3055
Greg Clayton44d93782014-01-27 23:43:24 +00003056 case eReturnStatusSuccessContinuingNoResult:
3057 case eReturnStatusSuccessContinuingResult:
Greg Clayton340b0302014-02-05 17:57:57 +00003058 if (io_handler.GetFlags().Test(eHandleCommandFlagStopOnContinue))
3059 io_handler.SetIsDone(true);
3060 break;
3061
Greg Clayton44d93782014-01-27 23:43:24 +00003062 case eReturnStatusFailed:
Greg Clayton340b0302014-02-05 17:57:57 +00003063 if (io_handler.GetFlags().Test(eHandleCommandFlagStopOnError))
3064 io_handler.SetIsDone(true);
Greg Clayton44d93782014-01-27 23:43:24 +00003065 break;
3066
3067 case eReturnStatusQuit:
3068 io_handler.SetIsDone(true);
3069 break;
3070 }
3071}
3072
Greg Claytonf0066ad2014-05-02 00:45:31 +00003073bool
3074CommandInterpreter::IOHandlerInterrupt (IOHandler &io_handler)
3075{
3076 ExecutionContext exe_ctx (GetExecutionContext());
3077 Process *process = exe_ctx.GetProcessPtr();
3078
3079 if (process)
3080 {
3081 StateType state = process->GetState();
3082 if (StateIsRunningState(state))
3083 {
3084 process->Halt();
3085 return true; // Don't do any updating when we are running
3086 }
3087 }
3088 return false;
3089}
3090
Greg Clayton44d93782014-01-27 23:43:24 +00003091void
3092CommandInterpreter::GetLLDBCommandsFromIOHandler (const char *prompt,
3093 IOHandlerDelegate &delegate,
3094 bool asynchronously,
3095 void *baton)
3096{
3097 Debugger &debugger = GetDebugger();
3098 IOHandlerSP io_handler_sp (new IOHandlerEditline (debugger,
3099 "lldb", // Name of input reader for history
3100 prompt, // Prompt
3101 true, // Get multiple lines
Greg Claytonf6913cd2014-03-07 00:53:24 +00003102 0, // Don't show line numbers
Greg Clayton44d93782014-01-27 23:43:24 +00003103 delegate)); // IOHandlerDelegate
3104
3105 if (io_handler_sp)
3106 {
3107 io_handler_sp->SetUserData (baton);
3108 if (asynchronously)
3109 debugger.PushIOHandler(io_handler_sp);
3110 else
3111 debugger.RunIOHandler(io_handler_sp);
3112 }
3113
3114}
3115
3116
3117void
3118CommandInterpreter::GetPythonCommandsFromIOHandler (const char *prompt,
3119 IOHandlerDelegate &delegate,
3120 bool asynchronously,
3121 void *baton)
3122{
3123 Debugger &debugger = GetDebugger();
3124 IOHandlerSP io_handler_sp (new IOHandlerEditline (debugger,
3125 "lldb-python", // Name of input reader for history
3126 prompt, // Prompt
3127 true, // Get multiple lines
Greg Claytonf6913cd2014-03-07 00:53:24 +00003128 0, // Don't show line numbers
Greg Clayton44d93782014-01-27 23:43:24 +00003129 delegate)); // IOHandlerDelegate
3130
3131 if (io_handler_sp)
3132 {
3133 io_handler_sp->SetUserData (baton);
3134 if (asynchronously)
3135 debugger.PushIOHandler(io_handler_sp);
3136 else
3137 debugger.RunIOHandler(io_handler_sp);
3138 }
3139
3140}
3141
3142bool
3143CommandInterpreter::IsActive ()
3144{
3145 return m_debugger.IsTopIOHandler (m_command_io_handler_sp);
3146}
3147
3148void
3149CommandInterpreter::RunCommandInterpreter(bool auto_handle_events,
3150 bool spawn_thread)
3151{
Greg Clayton380f3d82014-07-31 19:46:19 +00003152 // Only get one line at a time
3153 const bool multiple_lines = false;
3154
3155 // Always re-create the IOHandlerEditline in case the input
3156 // changed. The old instance might have had a non-interactive
3157 // input and now it does or vice versa.
3158 m_command_io_handler_sp.reset(new IOHandlerEditline (m_debugger,
3159 m_debugger.GetInputFile(),
3160 m_debugger.GetOutputFile(),
3161 m_debugger.GetErrorFile(),
3162 eHandleCommandFlagEchoCommand | eHandleCommandFlagPrintResult,
3163 "lldb",
3164 m_debugger.GetPrompt(),
3165 multiple_lines,
3166 0, // Don't show line numbers
3167 *this));
Greg Clayton06357c92014-07-30 17:38:47 +00003168
Greg Clayton44d93782014-01-27 23:43:24 +00003169 m_debugger.PushIOHandler(m_command_io_handler_sp);
3170
3171 if (auto_handle_events)
3172 m_debugger.StartEventHandlerThread();
3173
3174 if (spawn_thread)
3175 {
3176 m_debugger.StartIOHandlerThread();
3177 }
3178 else
3179 {
3180 m_debugger.ExecuteIOHanders();
3181
3182 if (auto_handle_events)
3183 m_debugger.StopEventHandlerThread();
3184 }
3185
3186}
3187